5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
43 # define PERL_NO_GET_CONTEXT
48 * pregcomp and pregexec -- regsub and regerror are not used in perl
50 * Copyright (c) 1986 by University of Toronto.
51 * Written by Henry Spencer. Not derived from licensed software.
53 * Permission is granted to anyone to use this software for any
54 * purpose on any computer system, and to redistribute it freely,
55 * subject to the following restrictions:
57 * 1. The author is not responsible for the consequences of use of
58 * this software, no matter how awful, even if they arise
61 * 2. The origin of this software must not be misrepresented, either
62 * by explicit claim or by omission.
64 * 3. Altered versions must be plainly marked as such, and must not
65 * be misrepresented as being the original software.
67 **** Alterations to Henry's code are...
69 **** Copyright (c) 1991-1999, Larry Wall
71 **** You may distribute under the terms of either the GNU General Public
72 **** License or the Artistic License, as specified in the README file.
74 * Beware that some of this code is subtly aware of the way operator
75 * precedence is structured in regular expressions. Serious changes in
76 * regular-expression syntax might require a total rethink.
79 #define PERL_IN_REGEXEC_C
82 #ifdef PERL_IN_XSUB_RE
83 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
90 #define RF_tainted 1 /* tainted information used? */
91 #define RF_warned 2 /* warned about big count? */
92 #define RF_evaled 4 /* Did an EVAL with setting? */
93 #define RF_utf8 8 /* String contains multibyte chars? */
95 #define UTF (PL_reg_flags & RF_utf8)
97 #define RS_init 1 /* eval environment created */
98 #define RS_set 2 /* replsv value is set */
101 #define STATIC static
108 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c) : ANYOF_BITMAP_TEST(p,c))
109 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
111 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
112 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
114 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
115 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
116 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
117 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
118 #define HOPc(pos,off) ((char*)HOP(pos,off))
119 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
121 static void restore_pos(pTHXo_ void *arg);
125 S_regcppush(pTHX_ I32 parenfloor)
128 int retval = PL_savestack_ix;
129 int i = (PL_regsize - parenfloor) * 4;
133 for (p = PL_regsize; p > parenfloor; p--) {
134 SSPUSHINT(PL_regendp[p]);
135 SSPUSHINT(PL_regstartp[p]);
136 SSPUSHPTR(PL_reg_start_tmp[p]);
139 SSPUSHINT(PL_regsize);
140 SSPUSHINT(*PL_reglastparen);
141 SSPUSHPTR(PL_reginput);
143 SSPUSHINT(SAVEt_REGCONTEXT);
147 /* These are needed since we do not localize EVAL nodes: */
148 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
149 " Setting an EVAL scope, savestack=%"IVdf"\n", \
150 (IV)PL_savestack_ix)); lastcp = PL_savestack_ix
152 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
153 PerlIO_printf(Perl_debug_log, \
154 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
155 (IV)lastcp, (IV)PL_savestack_ix) : 0); regcpblow(lastcp)
165 assert(i == SAVEt_REGCONTEXT);
167 input = (char *) SSPOPPTR;
168 *PL_reglastparen = SSPOPINT;
169 PL_regsize = SSPOPINT;
170 for (i -= 3; i > 0; i -= 4) {
171 paren = (U32)SSPOPINT;
172 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
173 PL_regstartp[paren] = SSPOPINT;
175 if (paren <= *PL_reglastparen)
176 PL_regendp[paren] = tmps;
178 PerlIO_printf(Perl_debug_log,
179 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
180 (UV)paren, (IV)PL_regstartp[paren],
181 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
182 (IV)PL_regendp[paren],
183 (paren > *PL_reglastparen ? "(no)" : ""));
187 if (*PL_reglastparen + 1 <= PL_regnpar) {
188 PerlIO_printf(Perl_debug_log,
189 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
190 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
193 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
194 if (paren > PL_regsize)
195 PL_regstartp[paren] = -1;
196 PL_regendp[paren] = -1;
202 S_regcp_set_to(pTHX_ I32 ss)
205 I32 tmp = PL_savestack_ix;
207 PL_savestack_ix = ss;
209 PL_savestack_ix = tmp;
213 typedef struct re_cc_state
217 struct re_cc_state *prev;
222 #define regcpblow(cp) LEAVE_SCOPE(cp)
225 * pregexec and friends
229 - pregexec - match a regexp against a string
232 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
233 char *strbeg, I32 minend, SV *screamer, U32 nosave)
234 /* strend: pointer to null at end of string */
235 /* strbeg: real beginning of string */
236 /* minend: end of match must be >=minend after stringarg. */
237 /* nosave: For optimizations. */
240 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
241 nosave ? 0 : REXEC_COPY_STR);
245 S_cache_re(pTHX_ regexp *prog)
248 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
250 PL_regprogram = prog->program;
252 PL_regnpar = prog->nparens;
253 PL_regdata = prog->data;
258 * Need to implement the following flags for reg_anch:
260 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
262 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
263 * INTUIT_AUTORITATIVE_ML
264 * INTUIT_ONCE_NOML - Intuit can match in one location only.
267 * Another flag for this function: SECOND_TIME (so that float substrs
268 * with giant delta may be not rechecked).
271 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
273 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
274 Otherwise, only SvCUR(sv) is used to get strbeg. */
276 /* XXXX We assume that strpos is strbeg unless sv. */
278 /* XXXX Some places assume that there is a fixed substring.
279 An update may be needed if optimizer marks as "INTUITable"
280 RExen without fixed substrings. Similarly, it is assumed that
281 lengths of all the strings are no more than minlen, thus they
282 cannot come from lookahead.
283 (Or minlen should take into account lookahead.) */
285 /* A failure to find a constant substring means that there is no need to make
286 an expensive call to REx engine, thus we celebrate a failure. Similarly,
287 finding a substring too deep into the string means that less calls to
288 regtry() should be needed.
290 REx compiler's optimizer found 4 possible hints:
291 a) Anchored substring;
293 c) Whether we are anchored (beginning-of-line or \G);
294 d) First node (of those at offset 0) which may distingush positions;
295 We use a)b)d) and multiline-part of c), and try to find a position in the
296 string which does not contradict any of them.
299 /* Most of decisions we do here should have been done at compile time.
300 The nodes of the REx which we used for the search should have been
301 deleted from the finite automaton. */
304 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
305 char *strend, U32 flags, re_scream_pos_data *data)
307 register I32 start_shift;
308 /* Should be nonnegative! */
309 register I32 end_shift;
315 register char *other_last = Nullch; /* other substr checked before this */
316 char *check_at; /* check substr found at this pos */
318 char *i_strpos = strpos;
321 DEBUG_r( if (!PL_colorset) reginitcolors() );
322 DEBUG_r(PerlIO_printf(Perl_debug_log,
323 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
324 PL_colors[4],PL_colors[5],PL_colors[0],
327 (strlen(prog->precomp) > 60 ? "..." : ""),
329 (int)(strend - strpos > 60 ? 60 : strend - strpos),
330 strpos, PL_colors[1],
331 (strend - strpos > 60 ? "..." : ""))
334 if (prog->minlen > strend - strpos) {
335 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
338 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
339 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
340 || ( (prog->reganch & ROPT_ANCH_BOL)
341 && !PL_multiline ) ); /* Check after \n? */
343 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
344 /* Substring at constant offset from beg-of-str... */
347 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
348 && (sv && (strpos + SvCUR(sv) != strend)) ) {
349 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
352 PL_regeol = strend; /* Used in HOP() */
353 s = HOPc(strpos, prog->check_offset_min);
354 if (SvTAIL(prog->check_substr)) {
355 slen = SvCUR(prog->check_substr); /* >= 1 */
357 if ( strend - s > slen || strend - s < slen - 1
358 || (strend - s == slen && strend[-1] != '\n')) {
359 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
362 /* Now should match s[0..slen-2] */
364 if (slen && (*SvPVX(prog->check_substr) != *s
366 && memNE(SvPVX(prog->check_substr), s, slen)))) {
368 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
372 else if (*SvPVX(prog->check_substr) != *s
373 || ((slen = SvCUR(prog->check_substr)) > 1
374 && memNE(SvPVX(prog->check_substr), s, slen)))
376 goto success_at_start;
378 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
380 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
381 /* Should be nonnegative! */
382 end_shift = prog->minlen - start_shift -
383 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
385 I32 end = prog->check_offset_max + CHR_SVLEN(prog->check_substr)
386 - (SvTAIL(prog->check_substr) != 0);
387 I32 eshift = strend - s - end;
389 if (end_shift < eshift)
393 else { /* Can match at random position */
396 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
397 /* Should be nonnegative! */
398 end_shift = prog->minlen - start_shift -
399 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
402 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
404 Perl_croak(aTHX_ "panic: end_shift");
407 check = prog->check_substr;
409 /* Find a possible match in the region s..strend by looking for
410 the "check" substring in the region corrected by start/end_shift. */
411 if (flags & REXEC_SCREAM) {
412 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
413 I32 p = -1; /* Internal iterator of scream. */
414 I32 *pp = data ? data->scream_pos : &p;
416 if (PL_screamfirst[BmRARE(check)] >= 0
417 || ( BmRARE(check) == '\n'
418 && (BmPREVIOUS(check) == SvCUR(check) - 1)
420 s = screaminstr(sv, check,
421 start_shift + (s - strbeg), end_shift, pp, 0);
425 *data->scream_olds = s;
428 s = fbm_instr((unsigned char*)s + start_shift,
429 (unsigned char*)strend - end_shift,
430 check, PL_multiline ? FBMrf_MULTILINE : 0);
432 /* Update the count-of-usability, remove useless subpatterns,
435 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
436 (s ? "Found" : "Did not find"),
437 ((check == prog->anchored_substr) ? "anchored" : "floating"),
439 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
441 PL_colors[1], (SvTAIL(check) ? "$" : ""),
442 (s ? " at offset " : "...\n") ) );
449 /* Finish the diagnostic message */
450 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
452 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
453 Start with the other substr.
454 XXXX no SCREAM optimization yet - and a very coarse implementation
455 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
456 *always* match. Probably should be marked during compile...
457 Probably it is right to do no SCREAM here...
460 if (prog->float_substr && prog->anchored_substr) {
461 /* Take into account the "other" substring. */
462 /* XXXX May be hopelessly wrong for UTF... */
465 if (check == prog->float_substr) {
468 char *last = s - start_shift, *last1, *last2;
472 t = s - prog->check_offset_max;
473 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
474 && (!(prog->reganch & ROPT_UTF8)
475 || (PL_bostr = strpos, /* Used in regcopmaybe() */
476 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
481 t += prog->anchored_offset;
482 if (t < other_last) /* These positions already checked */
485 last2 = last1 = strend - prog->minlen;
488 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
489 /* On end-of-str: see comment below. */
490 s = fbm_instr((unsigned char*)t,
491 (unsigned char*)last1 + prog->anchored_offset
492 + SvCUR(prog->anchored_substr)
493 - (SvTAIL(prog->anchored_substr)!=0),
494 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
495 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
496 (s ? "Found" : "Contradicts"),
498 (int)(SvCUR(prog->anchored_substr)
499 - (SvTAIL(prog->anchored_substr)!=0)),
500 SvPVX(prog->anchored_substr),
501 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
503 if (last1 >= last2) {
504 DEBUG_r(PerlIO_printf(Perl_debug_log,
505 ", giving up...\n"));
508 DEBUG_r(PerlIO_printf(Perl_debug_log,
509 ", trying floating at offset %ld...\n",
510 (long)(s1 + 1 - i_strpos)));
511 PL_regeol = strend; /* Used in HOP() */
512 other_last = last1 + prog->anchored_offset + 1;
517 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
518 (long)(s - i_strpos)));
519 t = s - prog->anchored_offset;
528 else { /* Take into account the floating substring. */
533 last1 = last = strend - prog->minlen + prog->float_min_offset;
534 if (last - t > prog->float_max_offset)
535 last = t + prog->float_max_offset;
536 s = t + prog->float_min_offset;
539 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
540 /* fbm_instr() takes into account exact value of end-of-str
541 if the check is SvTAIL(ed). Since false positives are OK,
542 and end-of-str is not later than strend we are OK. */
543 s = fbm_instr((unsigned char*)s,
544 (unsigned char*)last + SvCUR(prog->float_substr)
545 - (SvTAIL(prog->float_substr)!=0),
546 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
547 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
548 (s ? "Found" : "Contradicts"),
550 (int)(SvCUR(prog->float_substr)
551 - (SvTAIL(prog->float_substr)!=0)),
552 SvPVX(prog->float_substr),
553 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
556 DEBUG_r(PerlIO_printf(Perl_debug_log,
557 ", giving up...\n"));
560 DEBUG_r(PerlIO_printf(Perl_debug_log,
561 ", trying anchored starting at offset %ld...\n",
562 (long)(s1 + 1 - i_strpos)));
563 other_last = last + 1;
564 PL_regeol = strend; /* Used in HOP() */
569 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
570 (long)(s - i_strpos)));
580 t = s - prog->check_offset_max;
582 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
583 && (!(prog->reganch & ROPT_UTF8)
584 || (PL_bostr = strpos, /* Used in regcopmaybe() */
585 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
588 /* Fixed substring is found far enough so that the match
589 cannot start at strpos. */
591 if (ml_anch && t[-1] != '\n') {
592 /* Eventually fbm_*() should handle this, but often
593 anchored_offset is not 0, so this check will not be wasted. */
594 /* XXXX In the code below we prefer to look for "^" even in
595 presence of anchored substrings. And we search even
596 beyond the found float position. These pessimizations
597 are historical artefacts only. */
599 while (t < strend - prog->minlen) {
601 if (t < s - prog->check_offset_min) {
602 if (prog->anchored_substr) {
603 /* We definitely contradict the found anchored
604 substr. Due to the above check we do not
605 contradict "check" substr.
606 Thus we can arrive here only if check substr
607 is float. Redo checking for "other"=="fixed".
610 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
611 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
612 goto do_other_anchored;
615 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
616 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
619 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting at offset %ld...\n",
620 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
626 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
627 PL_colors[0],PL_colors[1]));
632 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
636 /* The found string does not prohibit matching at beg-of-str
637 - no optimization of calling REx engine can be performed,
638 unless it was an MBOL and we are not after MBOL. */
640 /* Even in this situation we may use MBOL flag if strpos is offset
641 wrt the start of the string. */
643 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
647 DEBUG_r( if (ml_anch)
648 PerlIO_printf(Perl_debug_log, "Does not contradict /%s^%s/m...\n",
649 PL_colors[0],PL_colors[1]);
652 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
653 && prog->check_substr /* Could be deleted already */
654 && --BmUSEFUL(prog->check_substr) < 0
655 && prog->check_substr == prog->float_substr)
657 /* If flags & SOMETHING - do not do it many times on the same match */
658 SvREFCNT_dec(prog->check_substr);
659 prog->check_substr = Nullsv; /* disable */
660 prog->float_substr = Nullsv; /* clear */
662 /* XXXX This is a remnant of the old implementation. It
663 looks wasteful, since now INTUIT can use many
665 prog->reganch &= ~RE_USE_INTUIT;
672 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
673 if (prog->regstclass) {
674 /* minlen == 0 is possible if regstclass is \b or \B,
675 and the fixed substr is ''$.
676 Since minlen is already taken into account, s+1 is before strend;
677 accidentally, minlen >= 1 guaranties no false positives at s + 1
678 even for \b or \B. But (minlen? 1 : 0) below assumes that
679 regstclass does not come from lookahead... */
680 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
681 This leaves EXACTF only, which is dealt with in find_byclass(). */
682 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
683 ? STR_LEN(prog->regstclass)
685 char *endpos = (prog->anchored_substr || ml_anch)
686 ? s + (prog->minlen? cl_l : 0)
687 : (prog->float_substr ? check_at - start_shift + cl_l
689 char *startpos = sv ? strend - SvCUR(sv) : s;
692 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
697 if (endpos == strend) {
698 DEBUG_r( PerlIO_printf(Perl_debug_log,
699 "Could not match STCLASS...\n") );
702 DEBUG_r( PerlIO_printf(Perl_debug_log,
703 "This position contradicts STCLASS...\n") );
704 /* Contradict one of substrings */
705 if (prog->anchored_substr) {
706 if (prog->anchored_substr == check) {
707 DEBUG_r( what = "anchored" );
709 PL_regeol = strend; /* Used in HOP() */
711 if (s + start_shift + end_shift > strend) {
712 /* XXXX Should be taken into account earlier? */
713 DEBUG_r( PerlIO_printf(Perl_debug_log,
714 "Could not match STCLASS...\n") );
717 DEBUG_r( PerlIO_printf(Perl_debug_log,
718 "Trying %s substr starting at offset %ld...\n",
719 what, (long)(s + start_shift - i_strpos)) );
722 /* Have both, check_string is floating */
723 if (t + start_shift >= check_at) /* Contradicts floating=check */
724 goto retry_floating_check;
725 /* Recheck anchored substring, but not floating... */
727 DEBUG_r( PerlIO_printf(Perl_debug_log,
728 "Trying anchored substr starting at offset %ld...\n",
729 (long)(other_last - i_strpos)) );
730 goto do_other_anchored;
732 if (!prog->float_substr) { /* Could have been deleted */
739 /* Check is floating subtring. */
740 retry_floating_check:
741 t = check_at - start_shift;
742 DEBUG_r( what = "floating" );
743 goto hop_and_restart;
746 PerlIO_printf(Perl_debug_log,
747 "By STCLASS: moving %ld --> %ld\n",
748 (long)(t - i_strpos), (long)(s - i_strpos));
750 PerlIO_printf(Perl_debug_log,
751 "Does not contradict STCLASS...\n") );
753 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sGuessed:%s match at offset %ld\n",
754 PL_colors[4], PL_colors[5], (long)(s - i_strpos)) );
757 fail_finish: /* Substring not found */
758 if (prog->check_substr) /* could be removed already */
759 BmUSEFUL(prog->check_substr) += 5; /* hooray */
761 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
762 PL_colors[4],PL_colors[5]));
766 /* We know what class REx starts with. Try to find this position... */
768 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
770 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
776 register I32 tmp = 1; /* Scratch variable? */
778 /* We know what class it must start with. */
782 if (REGINCLASSUTF8(c, (U8*)s)) {
783 if (tmp && (norun || regtry(prog, s)))
795 if (REGINCLASS(c, *s)) {
796 if (tmp && (norun || regtry(prog, s)))
816 c2 = PL_fold_locale[c1];
821 e = s; /* Due to minlen logic of intuit() */
822 /* Here it is NOT UTF! */
826 && (ln == 1 || !(OP(c) == EXACTF
828 : ibcmp_locale(s, m, ln)))
829 && (norun || regtry(prog, s)) )
835 if ( (*s == c1 || *s == c2)
836 && (ln == 1 || !(OP(c) == EXACTF
838 : ibcmp_locale(s, m, ln)))
839 && (norun || regtry(prog, s)) )
846 PL_reg_flags |= RF_tainted;
849 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
850 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
852 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
854 if ((norun || regtry(prog, s)))
859 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
863 PL_reg_flags |= RF_tainted;
866 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
867 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
869 if (tmp == !(OP(c) == BOUND ?
870 swash_fetch(PL_utf8_alnum, (U8*)s) :
871 isALNUM_LC_utf8((U8*)s)))
874 if ((norun || regtry(prog, s)))
879 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
883 PL_reg_flags |= RF_tainted;
886 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
887 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
889 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
891 else if ((norun || regtry(prog, s)))
895 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
899 PL_reg_flags |= RF_tainted;
903 strend = reghop_c(strend, -1);
904 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
905 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
907 if (tmp == !(OP(c) == NBOUND ?
908 swash_fetch(PL_utf8_alnum, (U8*)s) :
909 isALNUM_LC_utf8((U8*)s)))
911 else if ((norun || regtry(prog, s)))
915 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
921 if (tmp && (norun || regtry(prog, s)))
933 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
934 if (tmp && (norun || regtry(prog, s)))
945 PL_reg_flags |= RF_tainted;
947 if (isALNUM_LC(*s)) {
948 if (tmp && (norun || regtry(prog, s)))
959 PL_reg_flags |= RF_tainted;
961 if (isALNUM_LC_utf8((U8*)s)) {
962 if (tmp && (norun || regtry(prog, s)))
975 if (tmp && (norun || regtry(prog, s)))
987 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
988 if (tmp && (norun || regtry(prog, s)))
999 PL_reg_flags |= RF_tainted;
1000 while (s < strend) {
1001 if (!isALNUM_LC(*s)) {
1002 if (tmp && (norun || regtry(prog, s)))
1013 PL_reg_flags |= RF_tainted;
1014 while (s < strend) {
1015 if (!isALNUM_LC_utf8((U8*)s)) {
1016 if (tmp && (norun || regtry(prog, s)))
1027 while (s < strend) {
1029 if (tmp && (norun || regtry(prog, s)))
1040 while (s < strend) {
1041 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1042 if (tmp && (norun || regtry(prog, s)))
1053 PL_reg_flags |= RF_tainted;
1054 while (s < strend) {
1055 if (isSPACE_LC(*s)) {
1056 if (tmp && (norun || regtry(prog, s)))
1067 PL_reg_flags |= RF_tainted;
1068 while (s < strend) {
1069 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1070 if (tmp && (norun || regtry(prog, s)))
1081 while (s < strend) {
1083 if (tmp && (norun || regtry(prog, s)))
1094 while (s < strend) {
1095 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1096 if (tmp && (norun || regtry(prog, s)))
1107 PL_reg_flags |= RF_tainted;
1108 while (s < strend) {
1109 if (!isSPACE_LC(*s)) {
1110 if (tmp && (norun || regtry(prog, s)))
1121 PL_reg_flags |= RF_tainted;
1122 while (s < strend) {
1123 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1124 if (tmp && (norun || regtry(prog, s)))
1135 while (s < strend) {
1137 if (tmp && (norun || regtry(prog, s)))
1148 while (s < strend) {
1149 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1150 if (tmp && (norun || regtry(prog, s)))
1161 PL_reg_flags |= RF_tainted;
1162 while (s < strend) {
1163 if (isDIGIT_LC(*s)) {
1164 if (tmp && (norun || regtry(prog, s)))
1175 PL_reg_flags |= RF_tainted;
1176 while (s < strend) {
1177 if (isDIGIT_LC_utf8((U8*)s)) {
1178 if (tmp && (norun || regtry(prog, s)))
1189 while (s < strend) {
1191 if (tmp && (norun || regtry(prog, s)))
1202 while (s < strend) {
1203 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1204 if (tmp && (norun || regtry(prog, s)))
1215 PL_reg_flags |= RF_tainted;
1216 while (s < strend) {
1217 if (!isDIGIT_LC(*s)) {
1218 if (tmp && (norun || regtry(prog, s)))
1229 PL_reg_flags |= RF_tainted;
1230 while (s < strend) {
1231 if (!isDIGIT_LC_utf8((U8*)s)) {
1232 if (tmp && (norun || regtry(prog, s)))
1243 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1252 - regexec_flags - match a regexp against a string
1255 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1256 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1257 /* strend: pointer to null at end of string */
1258 /* strbeg: real beginning of string */
1259 /* minend: end of match must be >=minend after stringarg. */
1260 /* data: May be used for some additional optimizations. */
1261 /* nosave: For optimizations. */
1265 register regnode *c;
1266 register char *startpos = stringarg;
1268 I32 minlen; /* must match at least this many chars */
1269 I32 dontbother = 0; /* how many characters not to try at end */
1270 I32 start_shift = 0; /* Offset of the start to find
1271 constant substr. */ /* CC */
1272 I32 end_shift = 0; /* Same for the end. */ /* CC */
1273 I32 scream_pos = -1; /* Internal iterator of scream. */
1275 SV* oreplsv = GvSV(PL_replgv);
1281 PL_regnarrate = PL_debug & 512;
1284 /* Be paranoid... */
1285 if (prog == NULL || startpos == NULL) {
1286 Perl_croak(aTHX_ "NULL regexp parameter");
1290 minlen = prog->minlen;
1291 if (strend - startpos < minlen) goto phooey;
1293 if (startpos == strbeg) /* is ^ valid at stringarg? */
1296 PL_regprev = (U32)stringarg[-1];
1297 if (!PL_multiline && PL_regprev == '\n')
1298 PL_regprev = '\0'; /* force ^ to NOT match */
1301 /* Check validity of program. */
1302 if (UCHARAT(prog->program) != REG_MAGIC) {
1303 Perl_croak(aTHX_ "corrupted regexp program");
1307 PL_reg_eval_set = 0;
1310 if (prog->reganch & ROPT_UTF8)
1311 PL_reg_flags |= RF_utf8;
1313 /* Mark beginning of line for ^ and lookbehind. */
1314 PL_regbol = startpos;
1318 /* Mark end of line for $ (and such) */
1321 /* see how far we have to get to not match where we matched before */
1322 PL_regtill = startpos+minend;
1324 /* We start without call_cc context. */
1327 /* If there is a "must appear" string, look for it. */
1330 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1333 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1334 PL_reg_ganch = startpos;
1335 else if (sv && SvTYPE(sv) >= SVt_PVMG
1337 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1338 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1339 if (prog->reganch & ROPT_ANCH_GPOS) {
1340 if (s > PL_reg_ganch)
1345 else /* pos() not defined */
1346 PL_reg_ganch = strbeg;
1349 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1350 re_scream_pos_data d;
1352 d.scream_olds = &scream_olds;
1353 d.scream_pos = &scream_pos;
1354 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1356 goto phooey; /* not present */
1359 DEBUG_r( if (!PL_colorset) reginitcolors() );
1360 DEBUG_r(PerlIO_printf(Perl_debug_log,
1361 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1362 PL_colors[4],PL_colors[5],PL_colors[0],
1365 (strlen(prog->precomp) > 60 ? "..." : ""),
1367 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1368 startpos, PL_colors[1],
1369 (strend - startpos > 60 ? "..." : ""))
1372 /* Simplest case: anchored match need be tried only once. */
1373 /* [unless only anchor is BOL and multiline is set] */
1374 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1375 if (s == startpos && regtry(prog, startpos))
1377 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1378 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1383 dontbother = minlen - 1;
1384 end = HOPc(strend, -dontbother) - 1;
1385 /* for multiline we only have to try after newlines */
1386 if (prog->check_substr) {
1390 if (regtry(prog, s))
1395 if (prog->reganch & RE_USE_INTUIT) {
1396 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1407 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1408 if (regtry(prog, s))
1415 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1416 if (regtry(prog, PL_reg_ganch))
1421 /* Messy cases: unanchored match. */
1422 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1423 /* we have /x+whatever/ */
1424 /* it must be a one character string (XXXX Except UTF?) */
1425 char ch = SvPVX(prog->anchored_substr)[0];
1427 while (s < strend) {
1429 if (regtry(prog, s)) goto got_it;
1431 while (s < strend && *s == ch)
1438 while (s < strend) {
1440 if (regtry(prog, s)) goto got_it;
1442 while (s < strend && *s == ch)
1450 else if (prog->anchored_substr != Nullsv
1451 || (prog->float_substr != Nullsv
1452 && prog->float_max_offset < strend - s)) {
1453 SV *must = prog->anchored_substr
1454 ? prog->anchored_substr : prog->float_substr;
1456 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1458 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1459 I32 delta = back_max - back_min;
1460 char *last = HOPc(strend, /* Cannot start after this */
1461 -(I32)(CHR_SVLEN(must)
1462 - (SvTAIL(must) != 0) + back_min));
1463 char *last1; /* Last position checked before */
1466 last1 = HOPc(s, -1);
1468 last1 = s - 1; /* bogus */
1470 /* XXXX check_substr already used to find `s', can optimize if
1471 check_substr==must. */
1473 dontbother = end_shift;
1474 strend = HOPc(strend, -dontbother);
1475 while ( (s <= last) &&
1476 ((flags & REXEC_SCREAM)
1477 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1478 end_shift, &scream_pos, 0))
1479 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1480 (unsigned char*)strend, must,
1481 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1482 if (HOPc(s, -back_max) > last1) {
1483 last1 = HOPc(s, -back_min);
1484 s = HOPc(s, -back_max);
1487 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1489 last1 = HOPc(s, -back_min);
1493 while (s <= last1) {
1494 if (regtry(prog, s))
1500 while (s <= last1) {
1501 if (regtry(prog, s))
1509 else if (c = prog->regstclass) {
1510 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1511 /* don't bother with what can't match */
1512 strend = HOPc(strend, -(minlen - 1));
1513 if (find_byclass(prog, c, s, strend, startpos, 0))
1518 if (prog->float_substr != Nullsv) { /* Trim the end. */
1520 I32 oldpos = scream_pos;
1522 if (flags & REXEC_SCREAM) {
1523 last = screaminstr(sv, prog->float_substr, s - strbeg,
1524 end_shift, &scream_pos, 1); /* last one */
1526 last = scream_olds; /* Only one occurence. */
1530 char *little = SvPV(prog->float_substr, len);
1532 if (SvTAIL(prog->float_substr)) {
1533 if (memEQ(strend - len + 1, little, len - 1))
1534 last = strend - len + 1;
1535 else if (!PL_multiline)
1536 last = memEQ(strend - len, little, len)
1537 ? strend - len : Nullch;
1543 last = rninstr(s, strend, little, little + len);
1545 last = strend; /* matching `$' */
1548 if (last == NULL) goto phooey; /* Should not happen! */
1549 dontbother = strend - last + prog->float_min_offset;
1551 if (minlen && (dontbother < minlen))
1552 dontbother = minlen - 1;
1553 strend -= dontbother; /* this one's always in bytes! */
1554 /* We don't know much -- general case. */
1557 if (regtry(prog, s))
1566 if (regtry(prog, s))
1568 } while (s++ < strend);
1576 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1578 if (PL_reg_eval_set) {
1579 /* Preserve the current value of $^R */
1580 if (oreplsv != GvSV(PL_replgv))
1581 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1582 restored, the value remains
1584 restore_pos(aTHXo_ 0);
1587 /* make sure $`, $&, $', and $digit will work later */
1588 if ( !(flags & REXEC_NOT_FIRST) ) {
1589 if (RX_MATCH_COPIED(prog)) {
1590 Safefree(prog->subbeg);
1591 RX_MATCH_COPIED_off(prog);
1593 if (flags & REXEC_COPY_STR) {
1594 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1596 s = savepvn(strbeg, i);
1599 RX_MATCH_COPIED_on(prog);
1602 prog->subbeg = strbeg;
1603 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1610 if (PL_reg_eval_set)
1611 restore_pos(aTHXo_ 0);
1616 - regtry - try match at specific point
1618 STATIC I32 /* 0 failure, 1 success */
1619 S_regtry(pTHX_ regexp *prog, char *startpos)
1627 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1630 PL_reg_eval_set = RS_init;
1632 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1633 (IV)(PL_stack_sp - PL_stack_base));
1635 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1636 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1637 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1639 /* Apparently this is not needed, judging by wantarray. */
1640 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1641 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1644 /* Make $_ available to executed code. */
1645 if (PL_reg_sv != DEFSV) {
1646 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1651 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1652 && (mg = mg_find(PL_reg_sv, 'g')))) {
1653 /* prepare for quick setting of pos */
1654 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1655 mg = mg_find(PL_reg_sv, 'g');
1659 PL_reg_oldpos = mg->mg_len;
1660 SAVEDESTRUCTOR_X(restore_pos, 0);
1663 New(22,PL_reg_curpm, 1, PMOP);
1664 PL_reg_curpm->op_pmregexp = prog;
1665 PL_reg_oldcurpm = PL_curpm;
1666 PL_curpm = PL_reg_curpm;
1667 if (RX_MATCH_COPIED(prog)) {
1668 /* Here is a serious problem: we cannot rewrite subbeg,
1669 since it may be needed if this match fails. Thus
1670 $` inside (?{}) could fail... */
1671 PL_reg_oldsaved = prog->subbeg;
1672 PL_reg_oldsavedlen = prog->sublen;
1673 RX_MATCH_COPIED_off(prog);
1676 PL_reg_oldsaved = Nullch;
1677 prog->subbeg = PL_bostr;
1678 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1680 prog->startp[0] = startpos - PL_bostr;
1681 PL_reginput = startpos;
1682 PL_regstartp = prog->startp;
1683 PL_regendp = prog->endp;
1684 PL_reglastparen = &prog->lastparen;
1685 prog->lastparen = 0;
1687 DEBUG_r(PL_reg_starttry = startpos);
1688 if (PL_reg_start_tmpl <= prog->nparens) {
1689 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1690 if(PL_reg_start_tmp)
1691 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1693 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1696 /* XXXX What this code is doing here?!!! There should be no need
1697 to do this again and again, PL_reglastparen should take care of
1701 if (prog->nparens) {
1702 for (i = prog->nparens; i >= 1; i--) {
1708 if (regmatch(prog->program + 1)) {
1709 prog->endp[0] = PL_reginput - PL_bostr;
1717 - regmatch - main matching routine
1719 * Conceptually the strategy is simple: check to see whether the current
1720 * node matches, call self recursively to see whether the rest matches,
1721 * and then act accordingly. In practice we make some effort to avoid
1722 * recursion, in particular by going through "ordinary" nodes (that don't
1723 * need to know whether the rest of the match failed) by a loop instead of
1726 /* [lwall] I've hoisted the register declarations to the outer block in order to
1727 * maybe save a little bit of pushing and popping on the stack. It also takes
1728 * advantage of machines that use a register save mask on subroutine entry.
1730 STATIC I32 /* 0 failure, 1 success */
1731 S_regmatch(pTHX_ regnode *prog)
1734 register regnode *scan; /* Current node. */
1735 regnode *next; /* Next node. */
1736 regnode *inner; /* Next node in internal branch. */
1737 register I32 nextchr; /* renamed nextchr - nextchar colides with
1738 function of same name */
1739 register I32 n; /* no or next */
1740 register I32 ln; /* len or last */
1741 register char *s; /* operand or save */
1742 register char *locinput = PL_reginput;
1743 register I32 c1, c2, paren; /* case fold search, parenth */
1744 int minmod = 0, sw = 0, logical = 0;
1749 /* Note that nextchr is a byte even in UTF */
1750 nextchr = UCHARAT(locinput);
1752 while (scan != NULL) {
1753 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1755 # define sayYES goto yes
1756 # define sayNO goto no
1757 # define sayYES_FINAL goto yes_final
1758 # define sayYES_LOUD goto yes_loud
1759 # define sayNO_FINAL goto no_final
1760 # define sayNO_SILENT goto do_no
1761 # define saySAME(x) if (x) goto yes; else goto no
1762 # define REPORT_CODE_OFF 24
1764 # define sayYES return 1
1765 # define sayNO return 0
1766 # define sayYES_FINAL return 1
1767 # define sayYES_LOUD return 1
1768 # define sayNO_FINAL return 0
1769 # define sayNO_SILENT return 0
1770 # define saySAME(x) return x
1773 SV *prop = sv_newmortal();
1774 int docolor = *PL_colors[0];
1775 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1776 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1777 /* The part of the string before starttry has one color
1778 (pref0_len chars), between starttry and current
1779 position another one (pref_len - pref0_len chars),
1780 after the current position the third one.
1781 We assume that pref0_len <= pref_len, otherwise we
1782 decrease pref0_len. */
1783 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1784 ? (5 + taill) - l : locinput - PL_bostr);
1785 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1787 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1788 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1789 ? (5 + taill) - pref_len : PL_regeol - locinput);
1792 if (pref0_len > pref_len)
1793 pref0_len = pref_len;
1794 regprop(prop, scan);
1795 PerlIO_printf(Perl_debug_log,
1796 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1797 (IV)(locinput - PL_bostr),
1798 PL_colors[4], pref0_len,
1799 locinput - pref_len, PL_colors[5],
1800 PL_colors[2], pref_len - pref0_len,
1801 locinput - pref_len + pref0_len, PL_colors[3],
1802 (docolor ? "" : "> <"),
1803 PL_colors[0], l, locinput, PL_colors[1],
1804 15 - l - pref_len + 1,
1806 (IV)(scan - PL_regprogram), PL_regindent*2, "",
1810 next = scan + NEXT_OFF(scan);
1816 if (locinput == PL_bostr
1817 ? PL_regprev == '\n'
1819 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1821 /* regtill = regbol; */
1826 if (locinput == PL_bostr
1827 ? PL_regprev == '\n'
1828 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1834 if (locinput == PL_regbol && PL_regprev == '\n')
1838 if (locinput == PL_reg_ganch)
1848 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1853 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1855 if (PL_regeol - locinput > 1)
1859 if (PL_regeol != locinput)
1863 if (nextchr & 0x80) {
1864 locinput += PL_utf8skip[nextchr];
1865 if (locinput > PL_regeol)
1867 nextchr = UCHARAT(locinput);
1870 if (!nextchr && locinput >= PL_regeol)
1872 nextchr = UCHARAT(++locinput);
1875 if (!nextchr && locinput >= PL_regeol)
1877 nextchr = UCHARAT(++locinput);
1880 if (nextchr & 0x80) {
1881 locinput += PL_utf8skip[nextchr];
1882 if (locinput > PL_regeol)
1884 nextchr = UCHARAT(locinput);
1887 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1889 nextchr = UCHARAT(++locinput);
1892 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1894 nextchr = UCHARAT(++locinput);
1899 /* Inline the first character, for speed. */
1900 if (UCHARAT(s) != nextchr)
1902 if (PL_regeol - locinput < ln)
1904 if (ln > 1 && memNE(s, locinput, ln))
1907 nextchr = UCHARAT(locinput);
1910 PL_reg_flags |= RF_tainted;
1919 c1 = OP(scan) == EXACTF;
1923 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1924 toLOWER_utf8((U8*)l) :
1925 toLOWER_LC_utf8((U8*)l)))
1933 nextchr = UCHARAT(locinput);
1937 /* Inline the first character, for speed. */
1938 if (UCHARAT(s) != nextchr &&
1939 UCHARAT(s) != ((OP(scan) == EXACTF)
1940 ? PL_fold : PL_fold_locale)[nextchr])
1942 if (PL_regeol - locinput < ln)
1944 if (ln > 1 && (OP(scan) == EXACTF
1945 ? ibcmp(s, locinput, ln)
1946 : ibcmp_locale(s, locinput, ln)))
1949 nextchr = UCHARAT(locinput);
1952 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1954 if (locinput >= PL_regeol)
1956 locinput += PL_utf8skip[nextchr];
1957 nextchr = UCHARAT(locinput);
1961 nextchr = UCHARAT(locinput);
1962 if (!REGINCLASS(scan, nextchr))
1964 if (!nextchr && locinput >= PL_regeol)
1966 nextchr = UCHARAT(++locinput);
1969 PL_reg_flags |= RF_tainted;
1974 if (!(OP(scan) == ALNUM
1975 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1977 nextchr = UCHARAT(++locinput);
1980 PL_reg_flags |= RF_tainted;
1985 if (nextchr & 0x80) {
1986 if (!(OP(scan) == ALNUMUTF8
1987 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1988 : isALNUM_LC_utf8((U8*)locinput)))
1992 locinput += PL_utf8skip[nextchr];
1993 nextchr = UCHARAT(locinput);
1996 if (!(OP(scan) == ALNUMUTF8
1997 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1999 nextchr = UCHARAT(++locinput);
2002 PL_reg_flags |= RF_tainted;
2005 if (!nextchr && locinput >= PL_regeol)
2007 if (OP(scan) == NALNUM
2008 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2010 nextchr = UCHARAT(++locinput);
2013 PL_reg_flags |= RF_tainted;
2016 if (!nextchr && locinput >= PL_regeol)
2018 if (nextchr & 0x80) {
2019 if (OP(scan) == NALNUMUTF8
2020 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2021 : isALNUM_LC_utf8((U8*)locinput))
2025 locinput += PL_utf8skip[nextchr];
2026 nextchr = UCHARAT(locinput);
2029 if (OP(scan) == NALNUMUTF8
2030 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2032 nextchr = UCHARAT(++locinput);
2036 PL_reg_flags |= RF_tainted;
2040 /* was last char in word? */
2041 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2042 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2044 n = isALNUM(nextchr);
2047 ln = isALNUM_LC(ln);
2048 n = isALNUM_LC(nextchr);
2050 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2055 PL_reg_flags |= RF_tainted;
2059 /* was last char in word? */
2060 ln = (locinput != PL_regbol)
2061 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2062 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2063 ln = isALNUM_uni(ln);
2064 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2067 ln = isALNUM_LC_uni(ln);
2068 n = isALNUM_LC_utf8((U8*)locinput);
2070 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2074 PL_reg_flags |= RF_tainted;
2077 if (!nextchr && locinput >= PL_regeol)
2079 if (!(OP(scan) == SPACE
2080 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2082 nextchr = UCHARAT(++locinput);
2085 PL_reg_flags |= RF_tainted;
2088 if (!nextchr && locinput >= PL_regeol)
2090 if (nextchr & 0x80) {
2091 if (!(OP(scan) == SPACEUTF8
2092 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2093 : isSPACE_LC_utf8((U8*)locinput)))
2097 locinput += PL_utf8skip[nextchr];
2098 nextchr = UCHARAT(locinput);
2101 if (!(OP(scan) == SPACEUTF8
2102 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2104 nextchr = UCHARAT(++locinput);
2107 PL_reg_flags |= RF_tainted;
2112 if (OP(scan) == SPACE
2113 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2115 nextchr = UCHARAT(++locinput);
2118 PL_reg_flags |= RF_tainted;
2123 if (nextchr & 0x80) {
2124 if (OP(scan) == NSPACEUTF8
2125 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2126 : isSPACE_LC_utf8((U8*)locinput))
2130 locinput += PL_utf8skip[nextchr];
2131 nextchr = UCHARAT(locinput);
2134 if (OP(scan) == NSPACEUTF8
2135 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2137 nextchr = UCHARAT(++locinput);
2140 PL_reg_flags |= RF_tainted;
2143 if (!nextchr && locinput >= PL_regeol)
2145 if (!(OP(scan) == DIGIT
2146 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2148 nextchr = UCHARAT(++locinput);
2151 PL_reg_flags |= RF_tainted;
2156 if (nextchr & 0x80) {
2157 if (OP(scan) == NDIGITUTF8
2158 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2159 : isDIGIT_LC_utf8((U8*)locinput))
2163 locinput += PL_utf8skip[nextchr];
2164 nextchr = UCHARAT(locinput);
2167 if (!isDIGIT(nextchr))
2169 nextchr = UCHARAT(++locinput);
2172 PL_reg_flags |= RF_tainted;
2177 if (OP(scan) == DIGIT
2178 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2180 nextchr = UCHARAT(++locinput);
2183 PL_reg_flags |= RF_tainted;
2186 if (!nextchr && locinput >= PL_regeol)
2188 if (nextchr & 0x80) {
2189 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2191 locinput += PL_utf8skip[nextchr];
2192 nextchr = UCHARAT(locinput);
2195 if (isDIGIT(nextchr))
2197 nextchr = UCHARAT(++locinput);
2200 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2202 locinput += PL_utf8skip[nextchr];
2203 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2204 locinput += UTF8SKIP(locinput);
2205 if (locinput > PL_regeol)
2207 nextchr = UCHARAT(locinput);
2210 PL_reg_flags |= RF_tainted;
2214 n = ARG(scan); /* which paren pair */
2215 ln = PL_regstartp[n];
2216 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2217 if (*PL_reglastparen < n || ln == -1)
2218 sayNO; /* Do not match unless seen CLOSEn. */
2219 if (ln == PL_regendp[n])
2223 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2225 char *e = PL_bostr + PL_regendp[n];
2227 * Note that we can't do the "other character" lookup trick as
2228 * in the 8-bit case (no pun intended) because in Unicode we
2229 * have to map both upper and title case to lower case.
2231 if (OP(scan) == REFF) {
2235 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2245 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2252 nextchr = UCHARAT(locinput);
2256 /* Inline the first character, for speed. */
2257 if (UCHARAT(s) != nextchr &&
2259 (UCHARAT(s) != ((OP(scan) == REFF
2260 ? PL_fold : PL_fold_locale)[nextchr]))))
2262 ln = PL_regendp[n] - ln;
2263 if (locinput + ln > PL_regeol)
2265 if (ln > 1 && (OP(scan) == REF
2266 ? memNE(s, locinput, ln)
2268 ? ibcmp(s, locinput, ln)
2269 : ibcmp_locale(s, locinput, ln))))
2272 nextchr = UCHARAT(locinput);
2283 OP_4tree *oop = PL_op;
2284 COP *ocurcop = PL_curcop;
2285 SV **ocurpad = PL_curpad;
2289 PL_op = (OP_4tree*)PL_regdata->data[n];
2290 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2291 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2292 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2294 CALLRUNOPS(aTHX); /* Scalar context. */
2300 PL_curpad = ocurpad;
2301 PL_curcop = ocurcop;
2303 if (logical == 2) { /* Postponed subexpression. */
2305 MAGIC *mg = Null(MAGIC*);
2307 CHECKPOINT cp, lastcp;
2309 if(SvROK(ret) || SvRMAGICAL(ret)) {
2310 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2313 mg = mg_find(sv, 'r');
2316 re = (regexp *)mg->mg_obj;
2317 (void)ReREFCNT_inc(re);
2321 char *t = SvPV(ret, len);
2323 char *oprecomp = PL_regprecomp;
2324 I32 osize = PL_regsize;
2325 I32 onpar = PL_regnpar;
2328 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2330 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2331 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2332 PL_regprecomp = oprecomp;
2337 PerlIO_printf(Perl_debug_log,
2338 "Entering embedded `%s%.60s%s%s'\n",
2342 (strlen(re->precomp) > 60 ? "..." : ""))
2345 state.prev = PL_reg_call_cc;
2346 state.cc = PL_regcc;
2347 state.re = PL_reg_re;
2351 cp = regcppush(0); /* Save *all* the positions. */
2354 state.ss = PL_savestack_ix;
2355 *PL_reglastparen = 0;
2356 PL_reg_call_cc = &state;
2357 PL_reginput = locinput;
2359 /* XXXX This is too dramatic a measure... */
2362 if (regmatch(re->program + 1)) {
2363 /* Even though we succeeded, we need to restore
2364 global variables, since we may be wrapped inside
2365 SUSPEND, thus the match may be not finished yet. */
2367 /* XXXX Do this only if SUSPENDed? */
2368 PL_reg_call_cc = state.prev;
2369 PL_regcc = state.cc;
2370 PL_reg_re = state.re;
2371 cache_re(PL_reg_re);
2373 /* XXXX This is too dramatic a measure... */
2376 /* These are needed even if not SUSPEND. */
2384 PL_reg_call_cc = state.prev;
2385 PL_regcc = state.cc;
2386 PL_reg_re = state.re;
2387 cache_re(PL_reg_re);
2389 /* XXXX This is too dramatic a measure... */
2398 sv_setsv(save_scalar(PL_replgv), ret);
2402 n = ARG(scan); /* which paren pair */
2403 PL_reg_start_tmp[n] = locinput;
2408 n = ARG(scan); /* which paren pair */
2409 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2410 PL_regendp[n] = locinput - PL_bostr;
2411 if (n > *PL_reglastparen)
2412 *PL_reglastparen = n;
2415 n = ARG(scan); /* which paren pair */
2416 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2419 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2421 next = NEXTOPER(NEXTOPER(scan));
2423 next = scan + ARG(scan);
2424 if (OP(next) == IFTHEN) /* Fake one. */
2425 next = NEXTOPER(NEXTOPER(next));
2429 logical = scan->flags;
2431 /*******************************************************************
2432 PL_regcc contains infoblock about the innermost (...)* loop, and
2433 a pointer to the next outer infoblock.
2435 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2437 1) After matching X, regnode for CURLYX is processed;
2439 2) This regnode creates infoblock on the stack, and calls
2440 regmatch() recursively with the starting point at WHILEM node;
2442 3) Each hit of WHILEM node tries to match A and Z (in the order
2443 depending on the current iteration, min/max of {min,max} and
2444 greediness). The information about where are nodes for "A"
2445 and "Z" is read from the infoblock, as is info on how many times "A"
2446 was already matched, and greediness.
2448 4) After A matches, the same WHILEM node is hit again.
2450 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2451 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2452 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2453 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2454 of the external loop.
2456 Currently present infoblocks form a tree with a stem formed by PL_curcc
2457 and whatever it mentions via ->next, and additional attached trees
2458 corresponding to temporarily unset infoblocks as in "5" above.
2460 In the following picture infoblocks for outer loop of
2461 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2462 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2463 infoblocks are drawn below the "reset" infoblock.
2465 In fact in the picture below we do not show failed matches for Z and T
2466 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2467 more obvious *why* one needs to *temporary* unset infoblocks.]
2469 Matched REx position InfoBlocks Comment
2473 Y A)*?Z)*?T x <- O <- I
2474 YA )*?Z)*?T x <- O <- I
2475 YA A)*?Z)*?T x <- O <- I
2476 YAA )*?Z)*?T x <- O <- I
2477 YAA Z)*?T x <- O # Temporary unset I
2480 YAAZ Y(A)*?Z)*?T x <- O
2483 YAAZY (A)*?Z)*?T x <- O
2486 YAAZY A)*?Z)*?T x <- O <- I
2489 YAAZYA )*?Z)*?T x <- O <- I
2492 YAAZYA Z)*?T x <- O # Temporary unset I
2498 YAAZYAZ T x # Temporary unset O
2505 *******************************************************************/
2508 CHECKPOINT cp = PL_savestack_ix;
2510 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2512 cc.oldcc = PL_regcc;
2514 cc.parenfloor = *PL_reglastparen;
2516 cc.min = ARG1(scan);
2517 cc.max = ARG2(scan);
2518 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2522 PL_reginput = locinput;
2523 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2525 PL_regcc = cc.oldcc;
2531 * This is really hard to understand, because after we match
2532 * what we're trying to match, we must make sure the rest of
2533 * the REx is going to match for sure, and to do that we have
2534 * to go back UP the parse tree by recursing ever deeper. And
2535 * if it fails, we have to reset our parent's current state
2536 * that we can try again after backing off.
2539 CHECKPOINT cp, lastcp;
2540 CURCUR* cc = PL_regcc;
2541 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2543 n = cc->cur + 1; /* how many we know we matched */
2544 PL_reginput = locinput;
2547 PerlIO_printf(Perl_debug_log,
2548 "%*s %ld out of %ld..%ld cc=%lx\n",
2549 REPORT_CODE_OFF+PL_regindent*2, "",
2550 (long)n, (long)cc->min,
2551 (long)cc->max, (long)cc)
2554 /* If degenerate scan matches "", assume scan done. */
2556 if (locinput == cc->lastloc && n >= cc->min) {
2557 PL_regcc = cc->oldcc;
2561 PerlIO_printf(Perl_debug_log,
2562 "%*s empty match detected, try continuation...\n",
2563 REPORT_CODE_OFF+PL_regindent*2, "")
2565 if (regmatch(cc->next))
2573 /* First just match a string of min scans. */
2577 cc->lastloc = locinput;
2578 if (regmatch(cc->scan))
2581 cc->lastloc = lastloc;
2586 /* Check whether we already were at this position.
2587 Postpone detection until we know the match is not
2588 *that* much linear. */
2589 if (!PL_reg_maxiter) {
2590 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2591 PL_reg_leftiter = PL_reg_maxiter;
2593 if (PL_reg_leftiter-- == 0) {
2594 I32 size = (PL_reg_maxiter + 7)/8;
2595 if (PL_reg_poscache) {
2596 if (PL_reg_poscache_size < size) {
2597 Renew(PL_reg_poscache, size, char);
2598 PL_reg_poscache_size = size;
2600 Zero(PL_reg_poscache, size, char);
2603 PL_reg_poscache_size = size;
2604 Newz(29, PL_reg_poscache, size, char);
2607 PerlIO_printf(Perl_debug_log,
2608 "%sDetected a super-linear match, switching on caching%s...\n",
2609 PL_colors[4], PL_colors[5])
2612 if (PL_reg_leftiter < 0) {
2613 I32 o = locinput - PL_bostr, b;
2615 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2618 if (PL_reg_poscache[o] & (1<<b)) {
2620 PerlIO_printf(Perl_debug_log,
2621 "%*s already tried at this position...\n",
2622 REPORT_CODE_OFF+PL_regindent*2, "")
2626 PL_reg_poscache[o] |= (1<<b);
2630 /* Prefer next over scan for minimal matching. */
2633 PL_regcc = cc->oldcc;
2636 cp = regcppush(cc->parenfloor);
2638 if (regmatch(cc->next)) {
2640 sayYES; /* All done. */
2648 if (n >= cc->max) { /* Maximum greed exceeded? */
2649 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2650 && !(PL_reg_flags & RF_warned)) {
2651 PL_reg_flags |= RF_warned;
2652 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2653 "Complex regular subexpression recursion",
2660 PerlIO_printf(Perl_debug_log,
2661 "%*s trying longer...\n",
2662 REPORT_CODE_OFF+PL_regindent*2, "")
2664 /* Try scanning more and see if it helps. */
2665 PL_reginput = locinput;
2667 cc->lastloc = locinput;
2668 cp = regcppush(cc->parenfloor);
2670 if (regmatch(cc->scan)) {
2677 cc->lastloc = lastloc;
2681 /* Prefer scan over next for maximal matching. */
2683 if (n < cc->max) { /* More greed allowed? */
2684 cp = regcppush(cc->parenfloor);
2686 cc->lastloc = locinput;
2688 if (regmatch(cc->scan)) {
2693 regcppop(); /* Restore some previous $<digit>s? */
2694 PL_reginput = locinput;
2696 PerlIO_printf(Perl_debug_log,
2697 "%*s failed, try continuation...\n",
2698 REPORT_CODE_OFF+PL_regindent*2, "")
2701 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2702 && !(PL_reg_flags & RF_warned)) {
2703 PL_reg_flags |= RF_warned;
2704 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2705 "Complex regular subexpression recursion",
2709 /* Failed deeper matches of scan, so see if this one works. */
2710 PL_regcc = cc->oldcc;
2713 if (regmatch(cc->next))
2719 cc->lastloc = lastloc;
2724 next = scan + ARG(scan);
2727 inner = NEXTOPER(NEXTOPER(scan));
2730 inner = NEXTOPER(scan);
2735 if (OP(next) != c1) /* No choice. */
2736 next = inner; /* Avoid recursion. */
2738 int lastparen = *PL_reglastparen;
2742 PL_reginput = locinput;
2743 if (regmatch(inner))
2746 for (n = *PL_reglastparen; n > lastparen; n--)
2748 *PL_reglastparen = n;
2751 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2755 inner = NEXTOPER(scan);
2756 if (c1 == BRANCHJ) {
2757 inner = NEXTOPER(inner);
2759 } while (scan != NULL && OP(scan) == c1);
2773 /* We suppose that the next guy does not need
2774 backtracking: in particular, it is of constant length,
2775 and has no parenths to influence future backrefs. */
2776 ln = ARG1(scan); /* min to match */
2777 n = ARG2(scan); /* max to match */
2778 paren = scan->flags;
2780 if (paren > PL_regsize)
2782 if (paren > *PL_reglastparen)
2783 *PL_reglastparen = paren;
2785 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2787 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2788 PL_reginput = locinput;
2791 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2793 if (ln && l == 0 && n >= ln
2794 /* In fact, this is tricky. If paren, then the
2795 fact that we did/didnot match may influence
2796 future execution. */
2797 && !(paren && ln == 0))
2799 locinput = PL_reginput;
2800 if (PL_regkind[(U8)OP(next)] == EXACT) {
2801 c1 = (U8)*STRING(next);
2802 if (OP(next) == EXACTF)
2804 else if (OP(next) == EXACTFL)
2805 c2 = PL_fold_locale[c1];
2812 /* This may be improved if l == 0. */
2813 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2814 /* If it could work, try it. */
2816 UCHARAT(PL_reginput) == c1 ||
2817 UCHARAT(PL_reginput) == c2)
2821 PL_regstartp[paren] =
2822 HOPc(PL_reginput, -l) - PL_bostr;
2823 PL_regendp[paren] = PL_reginput - PL_bostr;
2826 PL_regendp[paren] = -1;
2832 /* Couldn't or didn't -- move forward. */
2833 PL_reginput = locinput;
2834 if (regrepeat_hard(scan, 1, &l)) {
2836 locinput = PL_reginput;
2843 n = regrepeat_hard(scan, n, &l);
2844 if (n != 0 && l == 0
2845 /* In fact, this is tricky. If paren, then the
2846 fact that we did/didnot match may influence
2847 future execution. */
2848 && !(paren && ln == 0))
2850 locinput = PL_reginput;
2852 PerlIO_printf(Perl_debug_log,
2853 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
2854 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
2858 if (PL_regkind[(U8)OP(next)] == EXACT) {
2859 c1 = (U8)*STRING(next);
2860 if (OP(next) == EXACTF)
2862 else if (OP(next) == EXACTFL)
2863 c2 = PL_fold_locale[c1];
2872 /* If it could work, try it. */
2874 UCHARAT(PL_reginput) == c1 ||
2875 UCHARAT(PL_reginput) == c2)
2878 PerlIO_printf(Perl_debug_log,
2879 "%*s trying tail with n=%"IVdf"...\n",
2880 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
2884 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2885 PL_regendp[paren] = PL_reginput - PL_bostr;
2888 PL_regendp[paren] = -1;
2894 /* Couldn't or didn't -- back up. */
2896 locinput = HOPc(locinput, -l);
2897 PL_reginput = locinput;
2904 paren = scan->flags; /* Which paren to set */
2905 if (paren > PL_regsize)
2907 if (paren > *PL_reglastparen)
2908 *PL_reglastparen = paren;
2909 ln = ARG1(scan); /* min to match */
2910 n = ARG2(scan); /* max to match */
2911 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2915 ln = ARG1(scan); /* min to match */
2916 n = ARG2(scan); /* max to match */
2917 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2922 scan = NEXTOPER(scan);
2928 scan = NEXTOPER(scan);
2932 * Lookahead to avoid useless match attempts
2933 * when we know what character comes next.
2935 if (PL_regkind[(U8)OP(next)] == EXACT) {
2936 c1 = (U8)*STRING(next);
2937 if (OP(next) == EXACTF)
2939 else if (OP(next) == EXACTFL)
2940 c2 = PL_fold_locale[c1];
2946 PL_reginput = locinput;
2950 if (ln && regrepeat(scan, ln) < ln)
2952 locinput = PL_reginput;
2955 char *e = locinput + n - ln; /* Should not check after this */
2956 char *old = locinput;
2958 if (e >= PL_regeol || (n == REG_INFTY))
2961 /* Find place 'next' could work */
2963 while (locinput <= e && *locinput != c1)
2966 while (locinput <= e
2973 /* PL_reginput == old now */
2974 if (locinput != old) {
2975 ln = 1; /* Did some */
2976 if (regrepeat(scan, locinput - old) <
2980 /* PL_reginput == locinput now */
2983 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2984 PL_regendp[paren] = locinput - PL_bostr;
2987 PL_regendp[paren] = -1;
2991 PL_reginput = locinput; /* Could be reset... */
2993 /* Couldn't or didn't -- move forward. */
2998 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2999 /* If it could work, try it. */
3001 UCHARAT(PL_reginput) == c1 ||
3002 UCHARAT(PL_reginput) == c2)
3006 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3007 PL_regendp[paren] = PL_reginput - PL_bostr;
3010 PL_regendp[paren] = -1;
3016 /* Couldn't or didn't -- move forward. */
3017 PL_reginput = locinput;
3018 if (regrepeat(scan, 1)) {
3020 locinput = PL_reginput;
3028 n = regrepeat(scan, n);
3029 locinput = PL_reginput;
3030 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3031 (!PL_multiline || OP(next) == SEOL))
3032 ln = n; /* why back off? */
3036 /* If it could work, try it. */
3038 UCHARAT(PL_reginput) == c1 ||
3039 UCHARAT(PL_reginput) == c2)
3043 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3044 PL_regendp[paren] = PL_reginput - PL_bostr;
3047 PL_regendp[paren] = -1;
3053 /* Couldn't or didn't -- back up. */
3055 PL_reginput = locinput = HOPc(locinput, -1);
3060 /* If it could work, try it. */
3062 UCHARAT(PL_reginput) == c1 ||
3063 UCHARAT(PL_reginput) == c2)
3069 /* Couldn't or didn't -- back up. */
3071 PL_reginput = locinput = HOPc(locinput, -1);
3078 if (PL_reg_call_cc) {
3079 re_cc_state *cur_call_cc = PL_reg_call_cc;
3080 CURCUR *cctmp = PL_regcc;
3081 regexp *re = PL_reg_re;
3082 CHECKPOINT cp, lastcp;
3084 cp = regcppush(0); /* Save *all* the positions. */
3086 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3088 PL_reginput = locinput; /* Make position available to
3090 cache_re(PL_reg_call_cc->re);
3091 PL_regcc = PL_reg_call_cc->cc;
3092 PL_reg_call_cc = PL_reg_call_cc->prev;
3093 if (regmatch(cur_call_cc->node)) {
3094 PL_reg_call_cc = cur_call_cc;
3100 PL_reg_call_cc = cur_call_cc;
3106 PerlIO_printf(Perl_debug_log,
3107 "%*s continuation failed...\n",
3108 REPORT_CODE_OFF+PL_regindent*2, "")
3112 if (locinput < PL_regtill) {
3113 DEBUG_r(PerlIO_printf(Perl_debug_log,
3114 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3116 (long)(locinput - PL_reg_starttry),
3117 (long)(PL_regtill - PL_reg_starttry),
3119 sayNO_FINAL; /* Cannot match: too short. */
3121 PL_reginput = locinput; /* put where regtry can find it */
3122 sayYES_FINAL; /* Success! */
3124 PL_reginput = locinput; /* put where regtry can find it */
3125 sayYES_LOUD; /* Success! */
3128 PL_reginput = locinput;
3133 if (UTF) { /* XXXX This is absolutely
3134 broken, we read before
3136 s = HOPMAYBEc(locinput, -scan->flags);
3142 if (locinput < PL_bostr + scan->flags)
3144 PL_reginput = locinput - scan->flags;
3149 PL_reginput = locinput;
3154 if (UTF) { /* XXXX This is absolutely
3155 broken, we read before
3157 s = HOPMAYBEc(locinput, -scan->flags);
3158 if (!s || s < PL_bostr)
3163 if (locinput < PL_bostr + scan->flags)
3165 PL_reginput = locinput - scan->flags;
3170 PL_reginput = locinput;
3173 inner = NEXTOPER(NEXTOPER(scan));
3174 if (regmatch(inner) != n) {
3189 if (OP(scan) == SUSPEND) {
3190 locinput = PL_reginput;
3191 nextchr = UCHARAT(locinput);
3196 next = scan + ARG(scan);
3201 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3202 PTR2UV(scan), OP(scan));
3203 Perl_croak(aTHX_ "regexp memory corruption");
3209 * We get here only if there's trouble -- normally "case END" is
3210 * the terminating point.
3212 Perl_croak(aTHX_ "corrupted regexp pointers");
3218 PerlIO_printf(Perl_debug_log,
3219 "%*s %scould match...%s\n",
3220 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3224 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3225 PL_colors[4],PL_colors[5]));
3234 PerlIO_printf(Perl_debug_log,
3235 "%*s %sfailed...%s\n",
3236 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3248 - regrepeat - repeatedly match something simple, report how many
3251 * [This routine now assumes that it will only match on things of length 1.
3252 * That was true before, but now we assume scan - reginput is the count,
3253 * rather than incrementing count on every character. [Er, except utf8.]]
3256 S_regrepeat(pTHX_ regnode *p, I32 max)
3259 register char *scan;
3261 register char *loceol = PL_regeol;
3262 register I32 hardcount = 0;
3265 if (max != REG_INFTY && max < loceol - scan)
3266 loceol = scan + max;
3269 while (scan < loceol && *scan != '\n')
3277 while (scan < loceol && *scan != '\n') {
3278 scan += UTF8SKIP(scan);
3284 while (scan < loceol) {
3285 scan += UTF8SKIP(scan);
3289 case EXACT: /* length of string is 1 */
3291 while (scan < loceol && UCHARAT(scan) == c)
3294 case EXACTF: /* length of string is 1 */
3296 while (scan < loceol &&
3297 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3300 case EXACTFL: /* length of string is 1 */
3301 PL_reg_flags |= RF_tainted;
3303 while (scan < loceol &&
3304 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3309 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3310 scan += UTF8SKIP(scan);
3315 while (scan < loceol && REGINCLASS(p, *scan))
3319 while (scan < loceol && isALNUM(*scan))
3324 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3325 scan += UTF8SKIP(scan);
3330 PL_reg_flags |= RF_tainted;
3331 while (scan < loceol && isALNUM_LC(*scan))
3335 PL_reg_flags |= RF_tainted;
3337 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3338 scan += UTF8SKIP(scan);
3344 while (scan < loceol && !isALNUM(*scan))
3349 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3350 scan += UTF8SKIP(scan);
3355 PL_reg_flags |= RF_tainted;
3356 while (scan < loceol && !isALNUM_LC(*scan))
3360 PL_reg_flags |= RF_tainted;
3362 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3363 scan += UTF8SKIP(scan);
3368 while (scan < loceol && isSPACE(*scan))
3373 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3374 scan += UTF8SKIP(scan);
3379 PL_reg_flags |= RF_tainted;
3380 while (scan < loceol && isSPACE_LC(*scan))
3384 PL_reg_flags |= RF_tainted;
3386 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3387 scan += UTF8SKIP(scan);
3392 while (scan < loceol && !isSPACE(*scan))
3397 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3398 scan += UTF8SKIP(scan);
3403 PL_reg_flags |= RF_tainted;
3404 while (scan < loceol && !isSPACE_LC(*scan))
3408 PL_reg_flags |= RF_tainted;
3410 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3411 scan += UTF8SKIP(scan);
3416 while (scan < loceol && isDIGIT(*scan))
3421 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3422 scan += UTF8SKIP(scan);
3428 while (scan < loceol && !isDIGIT(*scan))
3433 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3434 scan += UTF8SKIP(scan);
3438 default: /* Called on something of 0 width. */
3439 break; /* So match right here or not at all. */
3445 c = scan - PL_reginput;
3450 SV *prop = sv_newmortal();
3453 PerlIO_printf(Perl_debug_log,
3454 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3455 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3462 - regrepeat_hard - repeatedly match something, report total lenth and length
3464 * The repeater is supposed to have constant length.
3468 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3471 register char *scan;
3472 register char *start;
3473 register char *loceol = PL_regeol;
3475 I32 count = 0, res = 1;
3480 start = PL_reginput;
3482 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3485 while (start < PL_reginput) {
3487 start += UTF8SKIP(start);
3498 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3500 *lp = l = PL_reginput - start;
3501 if (max != REG_INFTY && l*max < loceol - scan)
3502 loceol = scan + l*max;
3515 - reginclass - determine if a character falls into a character class
3519 S_reginclass(pTHX_ register regnode *p, register I32 c)
3522 char flags = ANYOF_FLAGS(p);
3526 if (ANYOF_BITMAP_TEST(p, c))
3528 else if (flags & ANYOF_FOLD) {
3530 if (flags & ANYOF_LOCALE) {
3531 PL_reg_flags |= RF_tainted;
3532 cf = PL_fold_locale[c];
3536 if (ANYOF_BITMAP_TEST(p, cf))
3540 if (!match && (flags & ANYOF_CLASS)) {
3541 PL_reg_flags |= RF_tainted;
3543 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3544 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3545 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3546 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3547 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3548 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3549 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3550 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3551 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3552 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3553 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3554 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3555 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3556 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3557 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3558 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3559 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3560 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3561 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3562 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3563 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3564 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3565 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3566 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3567 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3568 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3569 ) /* How's that for a conditional? */
3575 return (flags & ANYOF_INVERT) ? !match : match;
3579 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3582 char flags = ARG1(f);
3584 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3586 if (swash_fetch(sv, p))
3588 else if (flags & ANYOF_FOLD) {
3591 if (flags & ANYOF_LOCALE) {
3592 PL_reg_flags |= RF_tainted;
3593 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3596 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3597 if (swash_fetch(sv, tmpbuf))
3601 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3603 return (flags & ANYOF_INVERT) ? !match : match;
3607 S_reghop(pTHX_ U8 *s, I32 off)
3611 while (off-- && s < (U8*)PL_regeol)
3616 if (s > (U8*)PL_bostr) {
3619 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3621 } /* XXX could check well-formedness here */
3629 S_reghopmaybe(pTHX_ U8* s, I32 off)
3633 while (off-- && s < (U8*)PL_regeol)
3640 if (s > (U8*)PL_bostr) {
3643 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3645 } /* XXX could check well-formedness here */
3661 restore_pos(pTHXo_ void *arg)
3664 if (PL_reg_eval_set) {
3665 if (PL_reg_oldsaved) {
3666 PL_reg_re->subbeg = PL_reg_oldsaved;
3667 PL_reg_re->sublen = PL_reg_oldsavedlen;
3668 RX_MATCH_COPIED_on(PL_reg_re);
3670 PL_reg_magic->mg_len = PL_reg_oldpos;
3671 PL_reg_eval_set = 0;
3672 PL_curpm = PL_reg_oldcurpm;