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
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-2001, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGEXEC_C
85 #define RF_tainted 1 /* tainted information used? */
86 #define RF_warned 2 /* warned about big count? */
87 #define RF_evaled 4 /* Did an EVAL with setting? */
88 #define RF_utf8 8 /* String contains multibyte chars? */
90 #define UTF (PL_reg_flags & RF_utf8)
92 #define RS_init 1 /* eval environment created */
93 #define RS_set 2 /* replsv value is set */
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
107 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
108 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
110 #define HOPc(pos,off) ((char*)HOP(pos,off))
111 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
113 #define HOPBACK(pos, off) ( \
114 (UTF && PL_reg_match_utf8) \
115 ? reghopmaybe((U8*)pos, -off) \
116 : (pos - off >= PL_bostr) \
120 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
122 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
123 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
124 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
125 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
126 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
127 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
129 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
131 /* for use after a quantifier and before an EXACT-like node -- japhy */
132 #define JUMPABLE(rn) ( \
133 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
134 OP(rn) == SUSPEND || OP(rn) == IFMATCH \
137 #define NEAR_EXACT(rn) (PL_regkind[(U8)OP(rn)] == EXACT || JUMPABLE(rn))
139 #define NEXT_IMPT(rn) STMT_START { \
140 while (JUMPABLE(rn)) \
141 if (OP(rn) == SUSPEND || OP(rn) == IFMATCH) \
142 rn = NEXTOPER(NEXTOPER(rn)); \
143 else rn += NEXT_OFF(rn); \
146 static void restore_pos(pTHX_ void *arg);
149 S_regcppush(pTHX_ I32 parenfloor)
151 int retval = PL_savestack_ix;
152 #define REGCP_PAREN_ELEMS 4
153 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
156 if (paren_elems_to_push < 0)
157 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
159 #define REGCP_OTHER_ELEMS 6
160 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
161 for (p = PL_regsize; p > parenfloor; p--) {
162 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
163 SSPUSHINT(PL_regendp[p]);
164 SSPUSHINT(PL_regstartp[p]);
165 SSPUSHPTR(PL_reg_start_tmp[p]);
168 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
169 SSPUSHINT(PL_regsize);
170 SSPUSHINT(*PL_reglastparen);
171 SSPUSHINT(*PL_reglastcloseparen);
172 SSPUSHPTR(PL_reginput);
173 #define REGCP_FRAME_ELEMS 2
174 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
175 * are needed for the regexp context stack bookkeeping. */
176 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
177 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
182 /* These are needed since we do not localize EVAL nodes: */
183 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
184 " Setting an EVAL scope, savestack=%"IVdf"\n", \
185 (IV)PL_savestack_ix)); cp = PL_savestack_ix
187 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
188 PerlIO_printf(Perl_debug_log, \
189 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
190 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
200 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
202 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
203 i = SSPOPINT; /* Parentheses elements to pop. */
204 input = (char *) SSPOPPTR;
205 *PL_reglastcloseparen = SSPOPINT;
206 *PL_reglastparen = SSPOPINT;
207 PL_regsize = SSPOPINT;
209 /* Now restore the parentheses context. */
210 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
211 i > 0; i -= REGCP_PAREN_ELEMS) {
212 paren = (U32)SSPOPINT;
213 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
214 PL_regstartp[paren] = SSPOPINT;
216 if (paren <= *PL_reglastparen)
217 PL_regendp[paren] = tmps;
219 PerlIO_printf(Perl_debug_log,
220 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
221 (UV)paren, (IV)PL_regstartp[paren],
222 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
223 (IV)PL_regendp[paren],
224 (paren > *PL_reglastparen ? "(no)" : ""));
228 if (*PL_reglastparen + 1 <= PL_regnpar) {
229 PerlIO_printf(Perl_debug_log,
230 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
231 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
235 /* It would seem that the similar code in regtry()
236 * already takes care of this, and in fact it is in
237 * a better location to since this code can #if 0-ed out
238 * but the code in regtry() is needed or otherwise tests
239 * requiring null fields (pat.t#187 and split.t#{13,14}
240 * (as of patchlevel 7877) will fail. Then again,
241 * this code seems to be necessary or otherwise
242 * building DynaLoader will fail:
243 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
245 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
246 if (paren > PL_regsize)
247 PL_regstartp[paren] = -1;
248 PL_regendp[paren] = -1;
255 S_regcp_set_to(pTHX_ I32 ss)
257 I32 tmp = PL_savestack_ix;
259 PL_savestack_ix = ss;
261 PL_savestack_ix = tmp;
265 typedef struct re_cc_state
269 struct re_cc_state *prev;
274 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
276 #define TRYPAREN(paren, n, input) { \
279 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
280 PL_regendp[paren] = input - PL_bostr; \
283 PL_regendp[paren] = -1; \
285 if (regmatch(next)) \
288 PL_regendp[paren] = -1; \
293 * pregexec and friends
297 - pregexec - match a regexp against a string
300 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
301 char *strbeg, I32 minend, SV *screamer, U32 nosave)
302 /* strend: pointer to null at end of string */
303 /* strbeg: real beginning of string */
304 /* minend: end of match must be >=minend after stringarg. */
305 /* nosave: For optimizations. */
308 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
309 nosave ? 0 : REXEC_COPY_STR);
313 S_cache_re(pTHX_ regexp *prog)
315 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
317 PL_regprogram = prog->program;
319 PL_regnpar = prog->nparens;
320 PL_regdata = prog->data;
325 * Need to implement the following flags for reg_anch:
327 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
329 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
330 * INTUIT_AUTORITATIVE_ML
331 * INTUIT_ONCE_NOML - Intuit can match in one location only.
334 * Another flag for this function: SECOND_TIME (so that float substrs
335 * with giant delta may be not rechecked).
338 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
340 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
341 Otherwise, only SvCUR(sv) is used to get strbeg. */
343 /* XXXX We assume that strpos is strbeg unless sv. */
345 /* XXXX Some places assume that there is a fixed substring.
346 An update may be needed if optimizer marks as "INTUITable"
347 RExen without fixed substrings. Similarly, it is assumed that
348 lengths of all the strings are no more than minlen, thus they
349 cannot come from lookahead.
350 (Or minlen should take into account lookahead.) */
352 /* A failure to find a constant substring means that there is no need to make
353 an expensive call to REx engine, thus we celebrate a failure. Similarly,
354 finding a substring too deep into the string means that less calls to
355 regtry() should be needed.
357 REx compiler's optimizer found 4 possible hints:
358 a) Anchored substring;
360 c) Whether we are anchored (beginning-of-line or \G);
361 d) First node (of those at offset 0) which may distingush positions;
362 We use a)b)d) and multiline-part of c), and try to find a position in the
363 string which does not contradict any of them.
366 /* Most of decisions we do here should have been done at compile time.
367 The nodes of the REx which we used for the search should have been
368 deleted from the finite automaton. */
371 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
372 char *strend, U32 flags, re_scream_pos_data *data)
374 register I32 start_shift = 0;
375 /* Should be nonnegative! */
376 register I32 end_shift = 0;
382 register char *other_last = Nullch; /* other substr checked before this */
383 char *check_at = Nullch; /* check substr found at this pos */
385 char *i_strpos = strpos;
386 SV *dsv = sv_2mortal(newSVpvn("", 0));
390 char*s = UTF ? sv_uni_display(dsv, sv, 60, 0) : strpos;
391 int len = UTF ? strlen(s) : strend - strpos;
394 PerlIO_printf(Perl_debug_log,
395 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
396 PL_colors[4],PL_colors[5],PL_colors[0],
399 (strlen(prog->precomp) > 60 ? "..." : ""),
401 (int)(len > 60 ? 60 : len),
403 (len > 60 ? "..." : "")
407 if (prog->reganch & ROPT_UTF8)
408 PL_reg_flags |= RF_utf8;
410 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
411 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
414 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
416 check = prog->check_substr;
417 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
418 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
419 || ( (prog->reganch & ROPT_ANCH_BOL)
420 && !PL_multiline ) ); /* Check after \n? */
423 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
424 | ROPT_IMPLICIT)) /* not a real BOL */
425 /* SvCUR is not set on references: SvRV and SvPVX overlap */
427 && (strpos != strbeg)) {
428 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
431 if (prog->check_offset_min == prog->check_offset_max &&
432 !(prog->reganch & ROPT_CANY_SEEN)) {
433 /* Substring at constant offset from beg-of-str... */
436 s = HOP3c(strpos, prog->check_offset_min, strend);
438 slen = SvCUR(check); /* >= 1 */
440 if ( strend - s > slen || strend - s < slen - 1
441 || (strend - s == slen && strend[-1] != '\n')) {
442 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
445 /* Now should match s[0..slen-2] */
447 if (slen && (*SvPVX(check) != *s
449 && memNE(SvPVX(check), s, slen)))) {
451 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
455 else if (*SvPVX(check) != *s
456 || ((slen = SvCUR(check)) > 1
457 && memNE(SvPVX(check), s, slen)))
459 goto success_at_start;
462 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
464 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
465 end_shift = prog->minlen - start_shift -
466 CHR_SVLEN(check) + (SvTAIL(check) != 0);
468 I32 end = prog->check_offset_max + CHR_SVLEN(check)
469 - (SvTAIL(check) != 0);
470 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
472 if (end_shift < eshift)
476 else { /* Can match at random position */
479 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
480 /* Should be nonnegative! */
481 end_shift = prog->minlen - start_shift -
482 CHR_SVLEN(check) + (SvTAIL(check) != 0);
485 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
487 Perl_croak(aTHX_ "panic: end_shift");
491 /* Find a possible match in the region s..strend by looking for
492 the "check" substring in the region corrected by start/end_shift. */
493 if (flags & REXEC_SCREAM) {
494 I32 p = -1; /* Internal iterator of scream. */
495 I32 *pp = data ? data->scream_pos : &p;
497 if (PL_screamfirst[BmRARE(check)] >= 0
498 || ( BmRARE(check) == '\n'
499 && (BmPREVIOUS(check) == SvCUR(check) - 1)
501 s = screaminstr(sv, check,
502 start_shift + (s - strbeg), end_shift, pp, 0);
506 *data->scream_olds = s;
508 else if (prog->reganch & ROPT_CANY_SEEN)
509 s = fbm_instr((U8*)(s + start_shift),
510 (U8*)(strend - end_shift),
511 check, PL_multiline ? FBMrf_MULTILINE : 0);
513 s = fbm_instr(HOP3(s, start_shift, strend),
514 HOP3(strend, -end_shift, strbeg),
515 check, PL_multiline ? FBMrf_MULTILINE : 0);
517 /* Update the count-of-usability, remove useless subpatterns,
520 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
521 (s ? "Found" : "Did not find"),
522 ((check == prog->anchored_substr) ? "anchored" : "floating"),
524 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
526 PL_colors[1], (SvTAIL(check) ? "$" : ""),
527 (s ? " at offset " : "...\n") ) );
534 /* Finish the diagnostic message */
535 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
537 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
538 Start with the other substr.
539 XXXX no SCREAM optimization yet - and a very coarse implementation
540 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
541 *always* match. Probably should be marked during compile...
542 Probably it is right to do no SCREAM here...
545 if (prog->float_substr && prog->anchored_substr) {
546 /* Take into account the "other" substring. */
547 /* XXXX May be hopelessly wrong for UTF... */
550 if (check == prog->float_substr) {
553 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
556 t = s - prog->check_offset_max;
557 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
558 && (!(prog->reganch & ROPT_UTF8)
559 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
564 t = HOP3c(t, prog->anchored_offset, strend);
565 if (t < other_last) /* These positions already checked */
567 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
570 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
571 /* On end-of-str: see comment below. */
572 s = fbm_instr((unsigned char*)t,
573 HOP3(HOP3(last1, prog->anchored_offset, strend)
574 + SvCUR(prog->anchored_substr),
575 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
576 prog->anchored_substr,
577 PL_multiline ? FBMrf_MULTILINE : 0);
578 DEBUG_r(PerlIO_printf(Perl_debug_log,
579 "%s anchored substr `%s%.*s%s'%s",
580 (s ? "Found" : "Contradicts"),
582 (int)(SvCUR(prog->anchored_substr)
583 - (SvTAIL(prog->anchored_substr)!=0)),
584 SvPVX(prog->anchored_substr),
585 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
587 if (last1 >= last2) {
588 DEBUG_r(PerlIO_printf(Perl_debug_log,
589 ", giving up...\n"));
592 DEBUG_r(PerlIO_printf(Perl_debug_log,
593 ", trying floating at offset %ld...\n",
594 (long)(HOP3c(s1, 1, strend) - i_strpos)));
595 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
596 s = HOP3c(last, 1, strend);
600 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
601 (long)(s - i_strpos)));
602 t = HOP3c(s, -prog->anchored_offset, strbeg);
603 other_last = HOP3c(s, 1, strend);
611 else { /* Take into account the floating substring. */
615 t = HOP3c(s, -start_shift, strbeg);
617 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
618 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
619 last = HOP3c(t, prog->float_max_offset, strend);
620 s = HOP3c(t, prog->float_min_offset, strend);
623 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
624 /* fbm_instr() takes into account exact value of end-of-str
625 if the check is SvTAIL(ed). Since false positives are OK,
626 and end-of-str is not later than strend we are OK. */
627 s = fbm_instr((unsigned char*)s,
628 (unsigned char*)last + SvCUR(prog->float_substr)
629 - (SvTAIL(prog->float_substr)!=0),
630 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
631 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
632 (s ? "Found" : "Contradicts"),
634 (int)(SvCUR(prog->float_substr)
635 - (SvTAIL(prog->float_substr)!=0)),
636 SvPVX(prog->float_substr),
637 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
640 DEBUG_r(PerlIO_printf(Perl_debug_log,
641 ", giving up...\n"));
644 DEBUG_r(PerlIO_printf(Perl_debug_log,
645 ", trying anchored starting at offset %ld...\n",
646 (long)(s1 + 1 - i_strpos)));
648 s = HOP3c(t, 1, strend);
652 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
653 (long)(s - i_strpos)));
654 other_last = s; /* Fix this later. --Hugo */
663 t = s - prog->check_offset_max;
664 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
665 && (!(prog->reganch & ROPT_UTF8)
666 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
668 /* Fixed substring is found far enough so that the match
669 cannot start at strpos. */
671 if (ml_anch && t[-1] != '\n') {
672 /* Eventually fbm_*() should handle this, but often
673 anchored_offset is not 0, so this check will not be wasted. */
674 /* XXXX In the code below we prefer to look for "^" even in
675 presence of anchored substrings. And we search even
676 beyond the found float position. These pessimizations
677 are historical artefacts only. */
679 while (t < strend - prog->minlen) {
681 if (t < check_at - prog->check_offset_min) {
682 if (prog->anchored_substr) {
683 /* Since we moved from the found position,
684 we definitely contradict the found anchored
685 substr. Due to the above check we do not
686 contradict "check" substr.
687 Thus we can arrive here only if check substr
688 is float. Redo checking for "other"=="fixed".
691 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
692 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
693 goto do_other_anchored;
695 /* We don't contradict the found floating substring. */
696 /* XXXX Why not check for STCLASS? */
698 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
699 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
702 /* Position contradicts check-string */
703 /* XXXX probably better to look for check-string
704 than for "\n", so one should lower the limit for t? */
705 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
706 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
707 other_last = strpos = s = t + 1;
712 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
713 PL_colors[0],PL_colors[1]));
717 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
718 PL_colors[0],PL_colors[1]));
722 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
725 /* The found string does not prohibit matching at strpos,
726 - no optimization of calling REx engine can be performed,
727 unless it was an MBOL and we are not after MBOL,
728 or a future STCLASS check will fail this. */
730 /* Even in this situation we may use MBOL flag if strpos is offset
731 wrt the start of the string. */
732 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
733 && (strpos != strbeg) && strpos[-1] != '\n'
734 /* May be due to an implicit anchor of m{.*foo} */
735 && !(prog->reganch & ROPT_IMPLICIT))
740 DEBUG_r( if (ml_anch)
741 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
742 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
745 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
746 && prog->check_substr /* Could be deleted already */
747 && --BmUSEFUL(prog->check_substr) < 0
748 && prog->check_substr == prog->float_substr)
750 /* If flags & SOMETHING - do not do it many times on the same match */
751 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
752 SvREFCNT_dec(prog->check_substr);
753 prog->check_substr = Nullsv; /* disable */
754 prog->float_substr = Nullsv; /* clear */
755 check = Nullsv; /* abort */
757 /* XXXX This is a remnant of the old implementation. It
758 looks wasteful, since now INTUIT can use many
760 prog->reganch &= ~RE_USE_INTUIT;
767 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
768 if (prog->regstclass) {
769 /* minlen == 0 is possible if regstclass is \b or \B,
770 and the fixed substr is ''$.
771 Since minlen is already taken into account, s+1 is before strend;
772 accidentally, minlen >= 1 guaranties no false positives at s + 1
773 even for \b or \B. But (minlen? 1 : 0) below assumes that
774 regstclass does not come from lookahead... */
775 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
776 This leaves EXACTF only, which is dealt with in find_byclass(). */
777 U8* str = (U8*)STRING(prog->regstclass);
778 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
779 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
781 char *endpos = (prog->anchored_substr || ml_anch)
782 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
783 : (prog->float_substr
784 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
787 char *startpos = strbeg;
790 if (prog->reganch & ROPT_UTF8) {
791 PL_regdata = prog->data;
794 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
799 if (endpos == strend) {
800 DEBUG_r( PerlIO_printf(Perl_debug_log,
801 "Could not match STCLASS...\n") );
804 DEBUG_r( PerlIO_printf(Perl_debug_log,
805 "This position contradicts STCLASS...\n") );
806 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
808 /* Contradict one of substrings */
809 if (prog->anchored_substr) {
810 if (prog->anchored_substr == check) {
811 DEBUG_r( what = "anchored" );
813 s = HOP3c(t, 1, strend);
814 if (s + start_shift + end_shift > strend) {
815 /* XXXX Should be taken into account earlier? */
816 DEBUG_r( PerlIO_printf(Perl_debug_log,
817 "Could not match STCLASS...\n") );
822 DEBUG_r( PerlIO_printf(Perl_debug_log,
823 "Looking for %s substr starting at offset %ld...\n",
824 what, (long)(s + start_shift - i_strpos)) );
827 /* Have both, check_string is floating */
828 if (t + start_shift >= check_at) /* Contradicts floating=check */
829 goto retry_floating_check;
830 /* Recheck anchored substring, but not floating... */
834 DEBUG_r( PerlIO_printf(Perl_debug_log,
835 "Looking for anchored substr starting at offset %ld...\n",
836 (long)(other_last - i_strpos)) );
837 goto do_other_anchored;
839 /* Another way we could have checked stclass at the
840 current position only: */
845 DEBUG_r( PerlIO_printf(Perl_debug_log,
846 "Looking for /%s^%s/m starting at offset %ld...\n",
847 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
850 if (!prog->float_substr) /* Could have been deleted */
852 /* Check is floating subtring. */
853 retry_floating_check:
854 t = check_at - start_shift;
855 DEBUG_r( what = "floating" );
856 goto hop_and_restart;
859 DEBUG_r(PerlIO_printf(Perl_debug_log,
860 "By STCLASS: moving %ld --> %ld\n",
861 (long)(t - i_strpos), (long)(s - i_strpos))
865 DEBUG_r(PerlIO_printf(Perl_debug_log,
866 "Does not contradict STCLASS...\n");
871 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
872 PL_colors[4], (check ? "Guessed" : "Giving up"),
873 PL_colors[5], (long)(s - i_strpos)) );
876 fail_finish: /* Substring not found */
877 if (prog->check_substr) /* could be removed already */
878 BmUSEFUL(prog->check_substr) += 5; /* hooray */
880 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
881 PL_colors[4],PL_colors[5]));
885 /* We know what class REx starts with. Try to find this position... */
887 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
889 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
895 register I32 tmp = 1; /* Scratch variable? */
896 register bool do_utf8 = PL_reg_match_utf8;
898 /* We know what class it must start with. */
902 if (reginclass(c, (U8*)s, do_utf8)) {
903 if (tmp && (norun || regtry(prog, s)))
910 s += do_utf8 ? UTF8SKIP(s) : 1;
915 if (tmp && (norun || regtry(prog, s)))
927 U8 tmpbuf1[UTF8_MAXLEN*2+1];
928 U8 tmpbuf2[UTF8_MAXLEN*2+1];
930 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
931 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
933 c1 = utf8_to_uvuni(tmpbuf1, 0);
934 c2 = utf8_to_uvuni(tmpbuf2, 0);
945 c2 = PL_fold_locale[c1];
950 e = s; /* Due to minlen logic of intuit() */
956 if ( utf8_to_uvchr((U8*)s, &len) == c1
963 UV c = utf8_to_uvchr((U8*)s, &len);
964 if ( (c == c1 || c == c2) && regtry(prog, s) )
973 && (ln == 1 || !(OP(c) == EXACTF
975 : ibcmp_locale(s, m, ln)))
976 && (norun || regtry(prog, s)) )
982 if ( (*(U8*)s == c1 || *(U8*)s == c2)
983 && (ln == 1 || !(OP(c) == EXACTF
985 : ibcmp_locale(s, m, ln)))
986 && (norun || regtry(prog, s)) )
993 PL_reg_flags |= RF_tainted;
1000 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1003 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1005 tmp = ((OP(c) == BOUND ?
1006 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1007 LOAD_UTF8_CHARCLASS(alnum,"a");
1008 while (s < strend) {
1009 if (tmp == !(OP(c) == BOUND ?
1010 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1011 isALNUM_LC_utf8((U8*)s)))
1014 if ((norun || regtry(prog, s)))
1021 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1022 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1023 while (s < strend) {
1025 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1027 if ((norun || regtry(prog, s)))
1033 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1037 PL_reg_flags |= RF_tainted;
1044 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1047 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1049 tmp = ((OP(c) == NBOUND ?
1050 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1051 LOAD_UTF8_CHARCLASS(alnum,"a");
1052 while (s < strend) {
1053 if (tmp == !(OP(c) == NBOUND ?
1054 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1055 isALNUM_LC_utf8((U8*)s)))
1057 else if ((norun || regtry(prog, s)))
1063 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1064 tmp = ((OP(c) == NBOUND ?
1065 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1066 while (s < strend) {
1068 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1070 else if ((norun || regtry(prog, s)))
1075 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1080 LOAD_UTF8_CHARCLASS(alnum,"a");
1081 while (s < strend) {
1082 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1083 if (tmp && (norun || regtry(prog, s)))
1094 while (s < strend) {
1096 if (tmp && (norun || regtry(prog, s)))
1108 PL_reg_flags |= RF_tainted;
1110 while (s < strend) {
1111 if (isALNUM_LC_utf8((U8*)s)) {
1112 if (tmp && (norun || regtry(prog, s)))
1123 while (s < strend) {
1124 if (isALNUM_LC(*s)) {
1125 if (tmp && (norun || regtry(prog, s)))
1138 LOAD_UTF8_CHARCLASS(alnum,"a");
1139 while (s < strend) {
1140 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1141 if (tmp && (norun || regtry(prog, s)))
1152 while (s < strend) {
1154 if (tmp && (norun || regtry(prog, s)))
1166 PL_reg_flags |= RF_tainted;
1168 while (s < strend) {
1169 if (!isALNUM_LC_utf8((U8*)s)) {
1170 if (tmp && (norun || regtry(prog, s)))
1181 while (s < strend) {
1182 if (!isALNUM_LC(*s)) {
1183 if (tmp && (norun || regtry(prog, s)))
1196 LOAD_UTF8_CHARCLASS(space," ");
1197 while (s < strend) {
1198 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1199 if (tmp && (norun || regtry(prog, s)))
1210 while (s < strend) {
1212 if (tmp && (norun || regtry(prog, s)))
1224 PL_reg_flags |= RF_tainted;
1226 while (s < strend) {
1227 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1228 if (tmp && (norun || regtry(prog, s)))
1239 while (s < strend) {
1240 if (isSPACE_LC(*s)) {
1241 if (tmp && (norun || regtry(prog, s)))
1254 LOAD_UTF8_CHARCLASS(space," ");
1255 while (s < strend) {
1256 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1257 if (tmp && (norun || regtry(prog, s)))
1268 while (s < strend) {
1270 if (tmp && (norun || regtry(prog, s)))
1282 PL_reg_flags |= RF_tainted;
1284 while (s < strend) {
1285 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1286 if (tmp && (norun || regtry(prog, s)))
1297 while (s < strend) {
1298 if (!isSPACE_LC(*s)) {
1299 if (tmp && (norun || regtry(prog, s)))
1312 LOAD_UTF8_CHARCLASS(digit,"0");
1313 while (s < strend) {
1314 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1315 if (tmp && (norun || regtry(prog, s)))
1326 while (s < strend) {
1328 if (tmp && (norun || regtry(prog, s)))
1340 PL_reg_flags |= RF_tainted;
1342 while (s < strend) {
1343 if (isDIGIT_LC_utf8((U8*)s)) {
1344 if (tmp && (norun || regtry(prog, s)))
1355 while (s < strend) {
1356 if (isDIGIT_LC(*s)) {
1357 if (tmp && (norun || regtry(prog, s)))
1370 LOAD_UTF8_CHARCLASS(digit,"0");
1371 while (s < strend) {
1372 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1373 if (tmp && (norun || regtry(prog, s)))
1384 while (s < strend) {
1386 if (tmp && (norun || regtry(prog, s)))
1398 PL_reg_flags |= RF_tainted;
1400 while (s < strend) {
1401 if (!isDIGIT_LC_utf8((U8*)s)) {
1402 if (tmp && (norun || regtry(prog, s)))
1413 while (s < strend) {
1414 if (!isDIGIT_LC(*s)) {
1415 if (tmp && (norun || regtry(prog, s)))
1427 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1436 - regexec_flags - match a regexp against a string
1439 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1440 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1441 /* strend: pointer to null at end of string */
1442 /* strbeg: real beginning of string */
1443 /* minend: end of match must be >=minend after stringarg. */
1444 /* data: May be used for some additional optimizations. */
1445 /* nosave: For optimizations. */
1448 register regnode *c;
1449 register char *startpos = stringarg;
1450 I32 minlen; /* must match at least this many chars */
1451 I32 dontbother = 0; /* how many characters not to try at end */
1452 /* I32 start_shift = 0; */ /* Offset of the start to find
1453 constant substr. */ /* CC */
1454 I32 end_shift = 0; /* Same for the end. */ /* CC */
1455 I32 scream_pos = -1; /* Internal iterator of scream. */
1457 SV* oreplsv = GvSV(PL_replgv);
1458 bool do_utf8 = DO_UTF8(sv);
1460 SV *dsv = sv_2mortal(newSVpvn("", 0));
1467 PL_regnarrate = DEBUG_r_TEST;
1470 /* Be paranoid... */
1471 if (prog == NULL || startpos == NULL) {
1472 Perl_croak(aTHX_ "NULL regexp parameter");
1476 minlen = prog->minlen;
1477 if (do_utf8 && !(prog->reganch & ROPT_CANY_SEEN)) {
1478 if (utf8_distance((U8*)strend, (U8*)startpos) < minlen) {
1479 DEBUG_r(PerlIO_printf(Perl_debug_log,
1480 "Too short (in characters)...\n"));
1485 if (strend - startpos < minlen) {
1486 DEBUG_r(PerlIO_printf(Perl_debug_log,
1487 "Too short (in bytes)...\n"));
1492 /* Check validity of program. */
1493 if (UCHARAT(prog->program) != REG_MAGIC) {
1494 Perl_croak(aTHX_ "corrupted regexp program");
1498 PL_reg_eval_set = 0;
1501 if (prog->reganch & ROPT_UTF8)
1502 PL_reg_flags |= RF_utf8;
1504 /* Mark beginning of line for ^ and lookbehind. */
1505 PL_regbol = startpos;
1509 /* Mark end of line for $ (and such) */
1512 /* see how far we have to get to not match where we matched before */
1513 PL_regtill = startpos+minend;
1515 /* We start without call_cc context. */
1518 /* If there is a "must appear" string, look for it. */
1521 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1524 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1525 PL_reg_ganch = startpos;
1526 else if (sv && SvTYPE(sv) >= SVt_PVMG
1528 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1529 && mg->mg_len >= 0) {
1530 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1531 if (prog->reganch & ROPT_ANCH_GPOS) {
1532 if (s > PL_reg_ganch)
1537 else /* pos() not defined */
1538 PL_reg_ganch = strbeg;
1541 if (do_utf8 == (UTF!=0) &&
1542 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1543 re_scream_pos_data d;
1545 d.scream_olds = &scream_olds;
1546 d.scream_pos = &scream_pos;
1547 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1549 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1550 goto phooey; /* not present */
1555 char *s = do_utf8 ? sv_uni_display(dsv, sv, 60, 0) : startpos;
1556 int len = do_utf8 ? strlen(s) : strend - startpos;
1559 PerlIO_printf(Perl_debug_log,
1560 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1561 PL_colors[4],PL_colors[5],PL_colors[0],
1564 (strlen(prog->precomp) > 60 ? "..." : ""),
1566 (int)(len > 60 ? 60 : len),
1568 (len > 60 ? "..." : "")
1572 /* Simplest case: anchored match need be tried only once. */
1573 /* [unless only anchor is BOL and multiline is set] */
1574 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1575 if (s == startpos && regtry(prog, startpos))
1577 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1578 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1583 dontbother = minlen - 1;
1584 end = HOP3c(strend, -dontbother, strbeg) - 1;
1585 /* for multiline we only have to try after newlines */
1586 if (prog->check_substr) {
1590 if (regtry(prog, s))
1595 if (prog->reganch & RE_USE_INTUIT) {
1596 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1607 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1608 if (regtry(prog, s))
1615 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1616 if (regtry(prog, PL_reg_ganch))
1621 /* Messy cases: unanchored match. */
1622 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1623 /* we have /x+whatever/ */
1624 /* it must be a one character string (XXXX Except UTF?) */
1625 char ch = SvPVX(prog->anchored_substr)[0];
1631 while (s < strend) {
1633 DEBUG_r( did_match = 1 );
1634 if (regtry(prog, s)) goto got_it;
1636 while (s < strend && *s == ch)
1643 while (s < strend) {
1645 DEBUG_r( did_match = 1 );
1646 if (regtry(prog, s)) goto got_it;
1648 while (s < strend && *s == ch)
1654 DEBUG_r(if (!did_match)
1655 PerlIO_printf(Perl_debug_log,
1656 "Did not find anchored character...\n")
1660 else if (do_utf8 == (UTF!=0) &&
1661 (prog->anchored_substr != Nullsv
1662 || (prog->float_substr != Nullsv
1663 && prog->float_max_offset < strend - s))) {
1664 SV *must = prog->anchored_substr
1665 ? prog->anchored_substr : prog->float_substr;
1667 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1669 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1670 char *last = HOP3c(strend, /* Cannot start after this */
1671 -(I32)(CHR_SVLEN(must)
1672 - (SvTAIL(must) != 0) + back_min), strbeg);
1673 char *last1; /* Last position checked before */
1679 last1 = HOPc(s, -1);
1681 last1 = s - 1; /* bogus */
1683 /* XXXX check_substr already used to find `s', can optimize if
1684 check_substr==must. */
1686 dontbother = end_shift;
1687 strend = HOPc(strend, -dontbother);
1688 while ( (s <= last) &&
1689 ((flags & REXEC_SCREAM)
1690 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1691 end_shift, &scream_pos, 0))
1692 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1693 (unsigned char*)strend, must,
1694 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1695 DEBUG_r( did_match = 1 );
1696 if (HOPc(s, -back_max) > last1) {
1697 last1 = HOPc(s, -back_min);
1698 s = HOPc(s, -back_max);
1701 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1703 last1 = HOPc(s, -back_min);
1707 while (s <= last1) {
1708 if (regtry(prog, s))
1714 while (s <= last1) {
1715 if (regtry(prog, s))
1721 DEBUG_r(if (!did_match)
1722 PerlIO_printf(Perl_debug_log,
1723 "Did not find %s substr `%s%.*s%s'%s...\n",
1724 ((must == prog->anchored_substr)
1725 ? "anchored" : "floating"),
1727 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1729 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1733 else if ((c = prog->regstclass)) {
1734 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1735 /* don't bother with what can't match */
1736 strend = HOPc(strend, -(minlen - 1));
1738 SV *prop = sv_newmortal();
1740 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), UTF ? sv_uni_display(dsv, sv, 60, 0) : s);
1742 if (find_byclass(prog, c, s, strend, startpos, 0))
1744 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1748 if (prog->float_substr != Nullsv) { /* Trim the end. */
1751 if (flags & REXEC_SCREAM) {
1752 last = screaminstr(sv, prog->float_substr, s - strbeg,
1753 end_shift, &scream_pos, 1); /* last one */
1755 last = scream_olds; /* Only one occurrence. */
1759 char *little = SvPV(prog->float_substr, len);
1761 if (SvTAIL(prog->float_substr)) {
1762 if (memEQ(strend - len + 1, little, len - 1))
1763 last = strend - len + 1;
1764 else if (!PL_multiline)
1765 last = memEQ(strend - len, little, len)
1766 ? strend - len : Nullch;
1772 last = rninstr(s, strend, little, little + len);
1774 last = strend; /* matching `$' */
1778 DEBUG_r(PerlIO_printf(Perl_debug_log,
1779 "%sCan't trim the tail, match fails (should not happen)%s\n",
1780 PL_colors[4],PL_colors[5]));
1781 goto phooey; /* Should not happen! */
1783 dontbother = strend - last + prog->float_min_offset;
1785 if (minlen && (dontbother < minlen))
1786 dontbother = minlen - 1;
1787 strend -= dontbother; /* this one's always in bytes! */
1788 /* We don't know much -- general case. */
1791 if (regtry(prog, s))
1800 if (regtry(prog, s))
1802 } while (s++ < strend);
1810 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1812 if (PL_reg_eval_set) {
1813 /* Preserve the current value of $^R */
1814 if (oreplsv != GvSV(PL_replgv))
1815 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1816 restored, the value remains
1818 restore_pos(aTHX_ 0);
1821 /* make sure $`, $&, $', and $digit will work later */
1822 if ( !(flags & REXEC_NOT_FIRST) ) {
1823 if (RX_MATCH_COPIED(prog)) {
1824 Safefree(prog->subbeg);
1825 RX_MATCH_COPIED_off(prog);
1827 if (flags & REXEC_COPY_STR) {
1828 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1830 s = savepvn(strbeg, i);
1833 RX_MATCH_COPIED_on(prog);
1836 prog->subbeg = strbeg;
1837 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1844 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1845 PL_colors[4],PL_colors[5]));
1846 if (PL_reg_eval_set)
1847 restore_pos(aTHX_ 0);
1852 - regtry - try match at specific point
1854 STATIC I32 /* 0 failure, 1 success */
1855 S_regtry(pTHX_ regexp *prog, char *startpos)
1863 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1865 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1868 PL_reg_eval_set = RS_init;
1870 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1871 (IV)(PL_stack_sp - PL_stack_base));
1873 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1874 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1875 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1877 /* Apparently this is not needed, judging by wantarray. */
1878 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1879 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1882 /* Make $_ available to executed code. */
1883 if (PL_reg_sv != DEFSV) {
1884 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1889 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1890 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1891 /* prepare for quick setting of pos */
1892 sv_magic(PL_reg_sv, (SV*)0,
1893 PERL_MAGIC_regex_global, Nullch, 0);
1894 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1898 PL_reg_oldpos = mg->mg_len;
1899 SAVEDESTRUCTOR_X(restore_pos, 0);
1901 if (!PL_reg_curpm) {
1902 Newz(22,PL_reg_curpm, 1, PMOP);
1905 SV* repointer = newSViv(0);
1906 /* so we know which PL_regex_padav element is PL_reg_curpm */
1907 SvFLAGS(repointer) |= SVf_BREAK;
1908 av_push(PL_regex_padav,repointer);
1909 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
1910 PL_regex_pad = AvARRAY(PL_regex_padav);
1914 PM_SETRE(PL_reg_curpm, prog);
1915 PL_reg_oldcurpm = PL_curpm;
1916 PL_curpm = PL_reg_curpm;
1917 if (RX_MATCH_COPIED(prog)) {
1918 /* Here is a serious problem: we cannot rewrite subbeg,
1919 since it may be needed if this match fails. Thus
1920 $` inside (?{}) could fail... */
1921 PL_reg_oldsaved = prog->subbeg;
1922 PL_reg_oldsavedlen = prog->sublen;
1923 RX_MATCH_COPIED_off(prog);
1926 PL_reg_oldsaved = Nullch;
1927 prog->subbeg = PL_bostr;
1928 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1930 prog->startp[0] = startpos - PL_bostr;
1931 PL_reginput = startpos;
1932 PL_regstartp = prog->startp;
1933 PL_regendp = prog->endp;
1934 PL_reglastparen = &prog->lastparen;
1935 PL_reglastcloseparen = &prog->lastcloseparen;
1936 prog->lastparen = 0;
1938 DEBUG_r(PL_reg_starttry = startpos);
1939 if (PL_reg_start_tmpl <= prog->nparens) {
1940 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1941 if(PL_reg_start_tmp)
1942 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1944 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1947 /* XXXX What this code is doing here?!!! There should be no need
1948 to do this again and again, PL_reglastparen should take care of
1951 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1952 * Actually, the code in regcppop() (which Ilya may be meaning by
1953 * PL_reglastparen), is not needed at all by the test suite
1954 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1955 * enough, for building DynaLoader, or otherwise this
1956 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1957 * will happen. Meanwhile, this code *is* needed for the
1958 * above-mentioned test suite tests to succeed. The common theme
1959 * on those tests seems to be returning null fields from matches.
1964 if (prog->nparens) {
1965 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1972 if (regmatch(prog->program + 1)) {
1973 prog->endp[0] = PL_reginput - PL_bostr;
1976 REGCP_UNWIND(lastcp);
1980 #define RE_UNWIND_BRANCH 1
1981 #define RE_UNWIND_BRANCHJ 2
1985 typedef struct { /* XX: makes sense to enlarge it... */
1989 } re_unwind_generic_t;
2002 } re_unwind_branch_t;
2004 typedef union re_unwind_t {
2006 re_unwind_generic_t generic;
2007 re_unwind_branch_t branch;
2010 #define sayYES goto yes
2011 #define sayNO goto no
2012 #define sayYES_FINAL goto yes_final
2013 #define sayYES_LOUD goto yes_loud
2014 #define sayNO_FINAL goto no_final
2015 #define sayNO_SILENT goto do_no
2016 #define saySAME(x) if (x) goto yes; else goto no
2018 #define REPORT_CODE_OFF 24
2021 - regmatch - main matching routine
2023 * Conceptually the strategy is simple: check to see whether the current
2024 * node matches, call self recursively to see whether the rest matches,
2025 * and then act accordingly. In practice we make some effort to avoid
2026 * recursion, in particular by going through "ordinary" nodes (that don't
2027 * need to know whether the rest of the match failed) by a loop instead of
2030 /* [lwall] I've hoisted the register declarations to the outer block in order to
2031 * maybe save a little bit of pushing and popping on the stack. It also takes
2032 * advantage of machines that use a register save mask on subroutine entry.
2034 STATIC I32 /* 0 failure, 1 success */
2035 S_regmatch(pTHX_ regnode *prog)
2037 register regnode *scan; /* Current node. */
2038 regnode *next; /* Next node. */
2039 regnode *inner; /* Next node in internal branch. */
2040 register I32 nextchr; /* renamed nextchr - nextchar colides with
2041 function of same name */
2042 register I32 n; /* no or next */
2043 register I32 ln = 0; /* len or last */
2044 register char *s = Nullch; /* operand or save */
2045 register char *locinput = PL_reginput;
2046 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2047 int minmod = 0, sw = 0, logical = 0;
2050 I32 firstcp = PL_savestack_ix;
2052 register bool do_utf8 = PL_reg_match_utf8;
2054 SV *dsv0 = sv_2mortal(newSVpvn("", 0));
2055 SV *dsv1 = sv_2mortal(newSVpvn("", 0));
2056 SV *dsv2 = sv_2mortal(newSVpvn("", 0));
2063 /* Note that nextchr is a byte even in UTF */
2064 nextchr = UCHARAT(locinput);
2066 while (scan != NULL) {
2069 SV *prop = sv_newmortal();
2070 int docolor = *PL_colors[0];
2071 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2072 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2073 /* The part of the string before starttry has one color
2074 (pref0_len chars), between starttry and current
2075 position another one (pref_len - pref0_len chars),
2076 after the current position the third one.
2077 We assume that pref0_len <= pref_len, otherwise we
2078 decrease pref0_len. */
2079 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2080 ? (5 + taill) - l : locinput - PL_bostr;
2083 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2085 pref0_len = pref_len - (locinput - PL_reg_starttry);
2086 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2087 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2088 ? (5 + taill) - pref_len : PL_regeol - locinput);
2089 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2093 if (pref0_len > pref_len)
2094 pref0_len = pref_len;
2095 regprop(prop, scan);
2099 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2101 locinput - pref_len;
2102 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2103 char *s1 = do_utf8 ?
2104 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2105 pref_len - pref0_len, 60, 0) :
2106 locinput - pref_len + pref0_len;
2107 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2108 char *s2 = do_utf8 ?
2109 pv_uni_display(dsv2, (U8*)locinput,
2110 PL_regeol - locinput, 60, 0) :
2112 int len2 = do_utf8 ? strlen(s2) : l;
2113 PerlIO_printf(Perl_debug_log,
2114 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2115 (IV)(locinput - PL_bostr),
2122 (docolor ? "" : "> <"),
2126 15 - l - pref_len + 1,
2128 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2133 next = scan + NEXT_OFF(scan);
2139 if (locinput == PL_bostr || (PL_multiline &&
2140 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2142 /* regtill = regbol; */
2147 if (locinput == PL_bostr ||
2148 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2154 if (locinput == PL_bostr)
2158 if (locinput == PL_reg_ganch)
2168 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2173 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2175 if (PL_regeol - locinput > 1)
2179 if (PL_regeol != locinput)
2183 if (!nextchr && locinput >= PL_regeol)
2186 locinput += PL_utf8skip[nextchr];
2187 if (locinput > PL_regeol)
2189 nextchr = UCHARAT(locinput);
2192 nextchr = UCHARAT(++locinput);
2195 if (!nextchr && locinput >= PL_regeol)
2197 nextchr = UCHARAT(++locinput);
2200 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2203 locinput += PL_utf8skip[nextchr];
2204 if (locinput > PL_regeol)
2206 nextchr = UCHARAT(locinput);
2209 nextchr = UCHARAT(++locinput);
2214 if (do_utf8 != (UTF!=0)) {
2222 if (*((U8*)s) != utf8_to_uvchr((U8*)l, &len))
2231 if (*((U8*)l) != utf8_to_uvchr((U8*)s, &len))
2237 nextchr = UCHARAT(locinput);
2240 /* Inline the first character, for speed. */
2241 if (UCHARAT(s) != nextchr)
2243 if (PL_regeol - locinput < ln)
2245 if (ln > 1 && memNE(s, locinput, ln))
2248 nextchr = UCHARAT(locinput);
2251 PL_reg_flags |= RF_tainted;
2261 U8 tmpbuf[UTF8_MAXLEN*2+1];
2266 toLOWER_utf8((U8*)l, tmpbuf, &ulen);
2267 if (memNE(s, (char*)tmpbuf, ulen))
2273 nextchr = UCHARAT(locinput);
2277 /* Inline the first character, for speed. */
2278 if (UCHARAT(s) != nextchr &&
2279 UCHARAT(s) != ((OP(scan) == EXACTF)
2280 ? PL_fold : PL_fold_locale)[nextchr])
2282 if (PL_regeol - locinput < ln)
2284 if (ln > 1 && (OP(scan) == EXACTF
2285 ? ibcmp(s, locinput, ln)
2286 : ibcmp_locale(s, locinput, ln)))
2289 nextchr = UCHARAT(locinput);
2293 if (!reginclass(scan, (U8*)locinput, do_utf8))
2295 if (locinput >= PL_regeol)
2297 locinput += PL_utf8skip[nextchr];
2298 nextchr = UCHARAT(locinput);
2302 nextchr = UCHARAT(locinput);
2303 if (!reginclass(scan, (U8*)locinput, do_utf8))
2305 if (!nextchr && locinput >= PL_regeol)
2307 nextchr = UCHARAT(++locinput);
2311 PL_reg_flags |= RF_tainted;
2317 LOAD_UTF8_CHARCLASS(alnum,"a");
2318 if (!(OP(scan) == ALNUM
2319 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2320 : isALNUM_LC_utf8((U8*)locinput)))
2324 locinput += PL_utf8skip[nextchr];
2325 nextchr = UCHARAT(locinput);
2328 if (!(OP(scan) == ALNUM
2329 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2331 nextchr = UCHARAT(++locinput);
2334 PL_reg_flags |= RF_tainted;
2337 if (!nextchr && locinput >= PL_regeol)
2340 LOAD_UTF8_CHARCLASS(alnum,"a");
2341 if (OP(scan) == NALNUM
2342 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2343 : isALNUM_LC_utf8((U8*)locinput))
2347 locinput += PL_utf8skip[nextchr];
2348 nextchr = UCHARAT(locinput);
2351 if (OP(scan) == NALNUM
2352 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2354 nextchr = UCHARAT(++locinput);
2358 PL_reg_flags |= RF_tainted;
2362 /* was last char in word? */
2364 if (locinput == PL_bostr)
2367 U8 *r = reghop((U8*)locinput, -1);
2369 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2371 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2372 ln = isALNUM_uni(ln);
2373 LOAD_UTF8_CHARCLASS(alnum,"a");
2374 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2377 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2378 n = isALNUM_LC_utf8((U8*)locinput);
2382 ln = (locinput != PL_bostr) ?
2383 UCHARAT(locinput - 1) : '\n';
2384 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2386 n = isALNUM(nextchr);
2389 ln = isALNUM_LC(ln);
2390 n = isALNUM_LC(nextchr);
2393 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2394 OP(scan) == BOUNDL))
2398 PL_reg_flags |= RF_tainted;
2404 if (UTF8_IS_CONTINUED(nextchr)) {
2405 LOAD_UTF8_CHARCLASS(space," ");
2406 if (!(OP(scan) == SPACE
2407 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2408 : isSPACE_LC_utf8((U8*)locinput)))
2412 locinput += PL_utf8skip[nextchr];
2413 nextchr = UCHARAT(locinput);
2416 if (!(OP(scan) == SPACE
2417 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2419 nextchr = UCHARAT(++locinput);
2422 if (!(OP(scan) == SPACE
2423 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2425 nextchr = UCHARAT(++locinput);
2429 PL_reg_flags |= RF_tainted;
2432 if (!nextchr && locinput >= PL_regeol)
2435 LOAD_UTF8_CHARCLASS(space," ");
2436 if (OP(scan) == NSPACE
2437 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2438 : isSPACE_LC_utf8((U8*)locinput))
2442 locinput += PL_utf8skip[nextchr];
2443 nextchr = UCHARAT(locinput);
2446 if (OP(scan) == NSPACE
2447 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2449 nextchr = UCHARAT(++locinput);
2452 PL_reg_flags |= RF_tainted;
2458 LOAD_UTF8_CHARCLASS(digit,"0");
2459 if (!(OP(scan) == DIGIT
2460 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2461 : isDIGIT_LC_utf8((U8*)locinput)))
2465 locinput += PL_utf8skip[nextchr];
2466 nextchr = UCHARAT(locinput);
2469 if (!(OP(scan) == DIGIT
2470 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2472 nextchr = UCHARAT(++locinput);
2475 PL_reg_flags |= RF_tainted;
2478 if (!nextchr && locinput >= PL_regeol)
2481 LOAD_UTF8_CHARCLASS(digit,"0");
2482 if (OP(scan) == NDIGIT
2483 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2484 : isDIGIT_LC_utf8((U8*)locinput))
2488 locinput += PL_utf8skip[nextchr];
2489 nextchr = UCHARAT(locinput);
2492 if (OP(scan) == NDIGIT
2493 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2495 nextchr = UCHARAT(++locinput);
2498 LOAD_UTF8_CHARCLASS(mark,"~");
2499 if (locinput >= PL_regeol ||
2500 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2502 locinput += PL_utf8skip[nextchr];
2503 while (locinput < PL_regeol &&
2504 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2505 locinput += UTF8SKIP(locinput);
2506 if (locinput > PL_regeol)
2508 nextchr = UCHARAT(locinput);
2511 PL_reg_flags |= RF_tainted;
2515 n = ARG(scan); /* which paren pair */
2516 ln = PL_regstartp[n];
2517 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2518 if (*PL_reglastparen < n || ln == -1)
2519 sayNO; /* Do not match unless seen CLOSEn. */
2520 if (ln == PL_regendp[n])
2524 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2526 char *e = PL_bostr + PL_regendp[n];
2528 * Note that we can't do the "other character" lookup trick as
2529 * in the 8-bit case (no pun intended) because in Unicode we
2530 * have to map both upper and title case to lower case.
2532 if (OP(scan) == REFF) {
2533 STRLEN ulen1, ulen2;
2534 U8 tmpbuf1[UTF8_MAXLEN*2+1];
2535 U8 tmpbuf2[UTF8_MAXLEN*2+1];
2539 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2540 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2541 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2548 nextchr = UCHARAT(locinput);
2552 /* Inline the first character, for speed. */
2553 if (UCHARAT(s) != nextchr &&
2555 (UCHARAT(s) != ((OP(scan) == REFF
2556 ? PL_fold : PL_fold_locale)[nextchr]))))
2558 ln = PL_regendp[n] - ln;
2559 if (locinput + ln > PL_regeol)
2561 if (ln > 1 && (OP(scan) == REF
2562 ? memNE(s, locinput, ln)
2564 ? ibcmp(s, locinput, ln)
2565 : ibcmp_locale(s, locinput, ln))))
2568 nextchr = UCHARAT(locinput);
2579 OP_4tree *oop = PL_op;
2580 COP *ocurcop = PL_curcop;
2581 SV **ocurpad = PL_curpad;
2585 PL_op = (OP_4tree*)PL_regdata->data[n];
2586 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2587 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2588 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2592 CALLRUNOPS(aTHX); /* Scalar context. */
2595 ret = Nullsv; /* protect against empty (?{}) blocks. */
2603 PL_curpad = ocurpad;
2604 PL_curcop = ocurcop;
2606 if (logical == 2) { /* Postponed subexpression. */
2608 MAGIC *mg = Null(MAGIC*);
2610 CHECKPOINT cp, lastcp;
2612 if(SvROK(ret) || SvRMAGICAL(ret)) {
2613 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2616 mg = mg_find(sv, PERL_MAGIC_qr);
2619 re = (regexp *)mg->mg_obj;
2620 (void)ReREFCNT_inc(re);
2624 char *t = SvPV(ret, len);
2626 char *oprecomp = PL_regprecomp;
2627 I32 osize = PL_regsize;
2628 I32 onpar = PL_regnpar;
2631 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2633 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2634 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2636 PL_regprecomp = oprecomp;
2641 PerlIO_printf(Perl_debug_log,
2642 "Entering embedded `%s%.60s%s%s'\n",
2646 (strlen(re->precomp) > 60 ? "..." : ""))
2649 state.prev = PL_reg_call_cc;
2650 state.cc = PL_regcc;
2651 state.re = PL_reg_re;
2655 cp = regcppush(0); /* Save *all* the positions. */
2658 state.ss = PL_savestack_ix;
2659 *PL_reglastparen = 0;
2660 *PL_reglastcloseparen = 0;
2661 PL_reg_call_cc = &state;
2662 PL_reginput = locinput;
2664 /* XXXX This is too dramatic a measure... */
2667 if (regmatch(re->program + 1)) {
2668 /* Even though we succeeded, we need to restore
2669 global variables, since we may be wrapped inside
2670 SUSPEND, thus the match may be not finished yet. */
2672 /* XXXX Do this only if SUSPENDed? */
2673 PL_reg_call_cc = state.prev;
2674 PL_regcc = state.cc;
2675 PL_reg_re = state.re;
2676 cache_re(PL_reg_re);
2678 /* XXXX This is too dramatic a measure... */
2681 /* These are needed even if not SUSPEND. */
2687 REGCP_UNWIND(lastcp);
2689 PL_reg_call_cc = state.prev;
2690 PL_regcc = state.cc;
2691 PL_reg_re = state.re;
2692 cache_re(PL_reg_re);
2694 /* XXXX This is too dramatic a measure... */
2704 sv_setsv(save_scalar(PL_replgv), ret);
2708 n = ARG(scan); /* which paren pair */
2709 PL_reg_start_tmp[n] = locinput;
2714 n = ARG(scan); /* which paren pair */
2715 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2716 PL_regendp[n] = locinput - PL_bostr;
2717 if (n > *PL_reglastparen)
2718 *PL_reglastparen = n;
2719 *PL_reglastcloseparen = n;
2722 n = ARG(scan); /* which paren pair */
2723 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2726 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2728 next = NEXTOPER(NEXTOPER(scan));
2730 next = scan + ARG(scan);
2731 if (OP(next) == IFTHEN) /* Fake one. */
2732 next = NEXTOPER(NEXTOPER(next));
2736 logical = scan->flags;
2738 /*******************************************************************
2739 PL_regcc contains infoblock about the innermost (...)* loop, and
2740 a pointer to the next outer infoblock.
2742 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2744 1) After matching X, regnode for CURLYX is processed;
2746 2) This regnode creates infoblock on the stack, and calls
2747 regmatch() recursively with the starting point at WHILEM node;
2749 3) Each hit of WHILEM node tries to match A and Z (in the order
2750 depending on the current iteration, min/max of {min,max} and
2751 greediness). The information about where are nodes for "A"
2752 and "Z" is read from the infoblock, as is info on how many times "A"
2753 was already matched, and greediness.
2755 4) After A matches, the same WHILEM node is hit again.
2757 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2758 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2759 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2760 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2761 of the external loop.
2763 Currently present infoblocks form a tree with a stem formed by PL_curcc
2764 and whatever it mentions via ->next, and additional attached trees
2765 corresponding to temporarily unset infoblocks as in "5" above.
2767 In the following picture infoblocks for outer loop of
2768 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2769 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2770 infoblocks are drawn below the "reset" infoblock.
2772 In fact in the picture below we do not show failed matches for Z and T
2773 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2774 more obvious *why* one needs to *temporary* unset infoblocks.]
2776 Matched REx position InfoBlocks Comment
2780 Y A)*?Z)*?T x <- O <- I
2781 YA )*?Z)*?T x <- O <- I
2782 YA A)*?Z)*?T x <- O <- I
2783 YAA )*?Z)*?T x <- O <- I
2784 YAA Z)*?T x <- O # Temporary unset I
2787 YAAZ Y(A)*?Z)*?T x <- O
2790 YAAZY (A)*?Z)*?T x <- O
2793 YAAZY A)*?Z)*?T x <- O <- I
2796 YAAZYA )*?Z)*?T x <- O <- I
2799 YAAZYA Z)*?T x <- O # Temporary unset I
2805 YAAZYAZ T x # Temporary unset O
2812 *******************************************************************/
2815 CHECKPOINT cp = PL_savestack_ix;
2816 /* No need to save/restore up to this paren */
2817 I32 parenfloor = scan->flags;
2819 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2821 cc.oldcc = PL_regcc;
2823 /* XXXX Probably it is better to teach regpush to support
2824 parenfloor > PL_regsize... */
2825 if (parenfloor > *PL_reglastparen)
2826 parenfloor = *PL_reglastparen; /* Pessimization... */
2827 cc.parenfloor = parenfloor;
2829 cc.min = ARG1(scan);
2830 cc.max = ARG2(scan);
2831 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2835 PL_reginput = locinput;
2836 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2838 PL_regcc = cc.oldcc;
2844 * This is really hard to understand, because after we match
2845 * what we're trying to match, we must make sure the rest of
2846 * the REx is going to match for sure, and to do that we have
2847 * to go back UP the parse tree by recursing ever deeper. And
2848 * if it fails, we have to reset our parent's current state
2849 * that we can try again after backing off.
2852 CHECKPOINT cp, lastcp;
2853 CURCUR* cc = PL_regcc;
2854 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2856 n = cc->cur + 1; /* how many we know we matched */
2857 PL_reginput = locinput;
2860 PerlIO_printf(Perl_debug_log,
2861 "%*s %ld out of %ld..%ld cc=%lx\n",
2862 REPORT_CODE_OFF+PL_regindent*2, "",
2863 (long)n, (long)cc->min,
2864 (long)cc->max, (long)cc)
2867 /* If degenerate scan matches "", assume scan done. */
2869 if (locinput == cc->lastloc && n >= cc->min) {
2870 PL_regcc = cc->oldcc;
2874 PerlIO_printf(Perl_debug_log,
2875 "%*s empty match detected, try continuation...\n",
2876 REPORT_CODE_OFF+PL_regindent*2, "")
2878 if (regmatch(cc->next))
2886 /* First just match a string of min scans. */
2890 cc->lastloc = locinput;
2891 if (regmatch(cc->scan))
2894 cc->lastloc = lastloc;
2899 /* Check whether we already were at this position.
2900 Postpone detection until we know the match is not
2901 *that* much linear. */
2902 if (!PL_reg_maxiter) {
2903 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2904 PL_reg_leftiter = PL_reg_maxiter;
2906 if (PL_reg_leftiter-- == 0) {
2907 I32 size = (PL_reg_maxiter + 7)/8;
2908 if (PL_reg_poscache) {
2909 if (PL_reg_poscache_size < size) {
2910 Renew(PL_reg_poscache, size, char);
2911 PL_reg_poscache_size = size;
2913 Zero(PL_reg_poscache, size, char);
2916 PL_reg_poscache_size = size;
2917 Newz(29, PL_reg_poscache, size, char);
2920 PerlIO_printf(Perl_debug_log,
2921 "%sDetected a super-linear match, switching on caching%s...\n",
2922 PL_colors[4], PL_colors[5])
2925 if (PL_reg_leftiter < 0) {
2926 I32 o = locinput - PL_bostr, b;
2928 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2931 if (PL_reg_poscache[o] & (1<<b)) {
2933 PerlIO_printf(Perl_debug_log,
2934 "%*s already tried at this position...\n",
2935 REPORT_CODE_OFF+PL_regindent*2, "")
2939 PL_reg_poscache[o] |= (1<<b);
2943 /* Prefer next over scan for minimal matching. */
2946 PL_regcc = cc->oldcc;
2949 cp = regcppush(cc->parenfloor);
2951 if (regmatch(cc->next)) {
2953 sayYES; /* All done. */
2955 REGCP_UNWIND(lastcp);
2961 if (n >= cc->max) { /* Maximum greed exceeded? */
2962 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2963 && !(PL_reg_flags & RF_warned)) {
2964 PL_reg_flags |= RF_warned;
2965 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2966 "Complex regular subexpression recursion",
2973 PerlIO_printf(Perl_debug_log,
2974 "%*s trying longer...\n",
2975 REPORT_CODE_OFF+PL_regindent*2, "")
2977 /* Try scanning more and see if it helps. */
2978 PL_reginput = locinput;
2980 cc->lastloc = locinput;
2981 cp = regcppush(cc->parenfloor);
2983 if (regmatch(cc->scan)) {
2987 REGCP_UNWIND(lastcp);
2990 cc->lastloc = lastloc;
2994 /* Prefer scan over next for maximal matching. */
2996 if (n < cc->max) { /* More greed allowed? */
2997 cp = regcppush(cc->parenfloor);
2999 cc->lastloc = locinput;
3001 if (regmatch(cc->scan)) {
3005 REGCP_UNWIND(lastcp);
3006 regcppop(); /* Restore some previous $<digit>s? */
3007 PL_reginput = locinput;
3009 PerlIO_printf(Perl_debug_log,
3010 "%*s failed, try continuation...\n",
3011 REPORT_CODE_OFF+PL_regindent*2, "")
3014 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3015 && !(PL_reg_flags & RF_warned)) {
3016 PL_reg_flags |= RF_warned;
3017 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3018 "Complex regular subexpression recursion",
3022 /* Failed deeper matches of scan, so see if this one works. */
3023 PL_regcc = cc->oldcc;
3026 if (regmatch(cc->next))
3032 cc->lastloc = lastloc;
3037 next = scan + ARG(scan);
3040 inner = NEXTOPER(NEXTOPER(scan));
3043 inner = NEXTOPER(scan);
3047 if (OP(next) != c1) /* No choice. */
3048 next = inner; /* Avoid recursion. */
3050 I32 lastparen = *PL_reglastparen;
3052 re_unwind_branch_t *uw;
3054 /* Put unwinding data on stack */
3055 unwind1 = SSNEWt(1,re_unwind_branch_t);
3056 uw = SSPTRt(unwind1,re_unwind_branch_t);
3059 uw->type = ((c1 == BRANCH)
3061 : RE_UNWIND_BRANCHJ);
3062 uw->lastparen = lastparen;
3064 uw->locinput = locinput;
3065 uw->nextchr = nextchr;
3067 uw->regindent = ++PL_regindent;
3070 REGCP_SET(uw->lastcp);
3072 /* Now go into the first branch */
3085 /* We suppose that the next guy does not need
3086 backtracking: in particular, it is of constant length,
3087 and has no parenths to influence future backrefs. */
3088 ln = ARG1(scan); /* min to match */
3089 n = ARG2(scan); /* max to match */
3090 paren = scan->flags;
3092 if (paren > PL_regsize)
3094 if (paren > *PL_reglastparen)
3095 *PL_reglastparen = paren;
3097 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3099 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3100 PL_reginput = locinput;
3103 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3105 /* if we matched something zero-length we don't need to
3106 backtrack - capturing parens are already defined, so
3107 the caveat in the maximal case doesn't apply
3109 XXXX if ln == 0, we can redo this check first time
3110 through the following loop
3113 n = ln; /* don't backtrack */
3114 locinput = PL_reginput;
3115 if (NEAR_EXACT(next)) {
3116 regnode *text_node = next;
3118 if (PL_regkind[(U8)OP(next)] != EXACT)
3119 NEXT_IMPT(text_node);
3121 if (PL_regkind[(U8)OP(text_node)] != EXACT) {
3125 c1 = (U8)*STRING(text_node);
3126 if (OP(next) == EXACTF)
3128 else if (OP(text_node) == EXACTFL)
3129 c2 = PL_fold_locale[c1];
3137 /* This may be improved if l == 0. */
3138 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3139 /* If it could work, try it. */
3141 UCHARAT(PL_reginput) == c1 ||
3142 UCHARAT(PL_reginput) == c2)
3146 PL_regstartp[paren] =
3147 HOPc(PL_reginput, -l) - PL_bostr;
3148 PL_regendp[paren] = PL_reginput - PL_bostr;
3151 PL_regendp[paren] = -1;
3155 REGCP_UNWIND(lastcp);
3157 /* Couldn't or didn't -- move forward. */
3158 PL_reginput = locinput;
3159 if (regrepeat_hard(scan, 1, &l)) {
3161 locinput = PL_reginput;
3168 n = regrepeat_hard(scan, n, &l);
3169 /* if we matched something zero-length we don't need to
3170 backtrack, unless the minimum count is zero and we
3171 are capturing the result - in that case the capture
3172 being defined or not may affect later execution
3174 if (n != 0 && l == 0 && !(paren && ln == 0))
3175 ln = n; /* don't backtrack */
3176 locinput = PL_reginput;
3178 PerlIO_printf(Perl_debug_log,
3179 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3180 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3184 if (NEAR_EXACT(next)) {
3185 regnode *text_node = next;
3187 if (PL_regkind[(U8)OP(next)] != EXACT)
3188 NEXT_IMPT(text_node);
3190 if (PL_regkind[(U8)OP(text_node)] != EXACT) {
3194 c1 = (U8)*STRING(text_node);
3195 if (OP(text_node) == EXACTF)
3197 else if (OP(text_node) == EXACTFL)
3198 c2 = PL_fold_locale[c1];
3208 /* If it could work, try it. */
3210 UCHARAT(PL_reginput) == c1 ||
3211 UCHARAT(PL_reginput) == c2)
3214 PerlIO_printf(Perl_debug_log,
3215 "%*s trying tail with n=%"IVdf"...\n",
3216 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3220 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3221 PL_regendp[paren] = PL_reginput - PL_bostr;
3224 PL_regendp[paren] = -1;
3228 REGCP_UNWIND(lastcp);
3230 /* Couldn't or didn't -- back up. */
3232 locinput = HOPc(locinput, -l);
3233 PL_reginput = locinput;
3240 paren = scan->flags; /* Which paren to set */
3241 if (paren > PL_regsize)
3243 if (paren > *PL_reglastparen)
3244 *PL_reglastparen = paren;
3245 ln = ARG1(scan); /* min to match */
3246 n = ARG2(scan); /* max to match */
3247 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3251 ln = ARG1(scan); /* min to match */
3252 n = ARG2(scan); /* max to match */
3253 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3258 scan = NEXTOPER(scan);
3264 scan = NEXTOPER(scan);
3268 * Lookahead to avoid useless match attempts
3269 * when we know what character comes next.
3273 * Used to only do .*x and .*?x, but now it allows
3274 * for )'s, ('s and (?{ ... })'s to be in the way
3275 * of the quantifier and the EXACT-like node. -- japhy
3278 if (NEAR_EXACT(next)) {
3280 regnode *text_node = next;
3282 if (PL_regkind[(U8)OP(next)] != EXACT)
3283 NEXT_IMPT(text_node);
3285 if (PL_regkind[(U8)OP(text_node)] != EXACT) {
3289 s = (U8*)STRING(text_node);
3293 if (OP(text_node) == EXACTF)
3295 else if (OP(text_node) == EXACTFL)
3296 c2 = PL_fold_locale[c1];
3299 if (OP(text_node) == EXACTF) {
3300 STRLEN ulen1, ulen2;
3301 U8 tmpbuf1[UTF8_MAXLEN*2+1];
3302 U8 tmpbuf2[UTF8_MAXLEN*2+1];
3304 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3305 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3307 c1 = utf8_to_uvuni(tmpbuf1, 0);
3308 c2 = utf8_to_uvuni(tmpbuf2, 0);
3311 c2 = c1 = utf8_to_uvchr(s, NULL);
3318 PL_reginput = locinput;
3322 if (ln && regrepeat(scan, ln) < ln)
3324 locinput = PL_reginput;
3327 char *e; /* Should not check after this */
3328 char *old = locinput;
3330 if (n == REG_INFTY) {
3333 while (UTF8_IS_CONTINUATION(*(U8*)e))
3339 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3343 e = locinput + n - ln;
3349 /* Find place 'next' could work */
3352 while (locinput <= e &&
3353 UCHARAT(locinput) != c1)
3356 while (locinput <= e
3357 && UCHARAT(locinput) != c1
3358 && UCHARAT(locinput) != c2)
3361 count = locinput - old;
3368 utf8_to_uvchr((U8*)locinput, &len) != c1;
3373 for (count = 0; locinput <= e; count++) {
3374 UV c = utf8_to_uvchr((U8*)locinput, &len);
3375 if (c == c1 || c == c2)
3383 /* PL_reginput == old now */
3384 if (locinput != old) {
3385 ln = 1; /* Did some */
3386 if (regrepeat(scan, count) < count)
3389 /* PL_reginput == locinput now */
3390 TRYPAREN(paren, ln, locinput);
3391 PL_reginput = locinput; /* Could be reset... */
3392 REGCP_UNWIND(lastcp);
3393 /* Couldn't or didn't -- move forward. */
3396 locinput += UTF8SKIP(locinput);
3402 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3406 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3408 c = UCHARAT(PL_reginput);
3409 /* If it could work, try it. */
3410 if (c == c1 || c == c2)
3412 TRYPAREN(paren, n, PL_reginput);
3413 REGCP_UNWIND(lastcp);
3416 /* If it could work, try it. */
3417 else if (c1 == -1000)
3419 TRYPAREN(paren, n, PL_reginput);
3420 REGCP_UNWIND(lastcp);
3422 /* Couldn't or didn't -- move forward. */
3423 PL_reginput = locinput;
3424 if (regrepeat(scan, 1)) {
3426 locinput = PL_reginput;
3434 n = regrepeat(scan, n);
3435 locinput = PL_reginput;
3436 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3437 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3438 ln = n; /* why back off? */
3439 /* ...because $ and \Z can match before *and* after
3440 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3441 We should back off by one in this case. */
3442 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3451 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3453 c = UCHARAT(PL_reginput);
3455 /* If it could work, try it. */
3456 if (c1 == -1000 || c == c1 || c == c2)
3458 TRYPAREN(paren, n, PL_reginput);
3459 REGCP_UNWIND(lastcp);
3461 /* Couldn't or didn't -- back up. */
3463 PL_reginput = locinput = HOPc(locinput, -1);
3471 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3473 c = UCHARAT(PL_reginput);
3475 /* If it could work, try it. */
3476 if (c1 == -1000 || c == c1 || c == c2)
3478 TRYPAREN(paren, n, PL_reginput);
3479 REGCP_UNWIND(lastcp);
3481 /* Couldn't or didn't -- back up. */
3483 PL_reginput = locinput = HOPc(locinput, -1);
3490 if (PL_reg_call_cc) {
3491 re_cc_state *cur_call_cc = PL_reg_call_cc;
3492 CURCUR *cctmp = PL_regcc;
3493 regexp *re = PL_reg_re;
3494 CHECKPOINT cp, lastcp;
3496 cp = regcppush(0); /* Save *all* the positions. */
3498 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3500 PL_reginput = locinput; /* Make position available to
3502 cache_re(PL_reg_call_cc->re);
3503 PL_regcc = PL_reg_call_cc->cc;
3504 PL_reg_call_cc = PL_reg_call_cc->prev;
3505 if (regmatch(cur_call_cc->node)) {
3506 PL_reg_call_cc = cur_call_cc;
3510 REGCP_UNWIND(lastcp);
3512 PL_reg_call_cc = cur_call_cc;
3518 PerlIO_printf(Perl_debug_log,
3519 "%*s continuation failed...\n",
3520 REPORT_CODE_OFF+PL_regindent*2, "")
3524 if (locinput < PL_regtill) {
3525 DEBUG_r(PerlIO_printf(Perl_debug_log,
3526 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3528 (long)(locinput - PL_reg_starttry),
3529 (long)(PL_regtill - PL_reg_starttry),
3531 sayNO_FINAL; /* Cannot match: too short. */
3533 PL_reginput = locinput; /* put where regtry can find it */
3534 sayYES_FINAL; /* Success! */
3536 PL_reginput = locinput; /* put where regtry can find it */
3537 sayYES_LOUD; /* Success! */
3540 PL_reginput = locinput;
3545 s = HOPBACKc(locinput, scan->flags);
3551 PL_reginput = locinput;
3556 s = HOPBACKc(locinput, scan->flags);
3562 PL_reginput = locinput;
3565 inner = NEXTOPER(NEXTOPER(scan));
3566 if (regmatch(inner) != n) {
3581 if (OP(scan) == SUSPEND) {
3582 locinput = PL_reginput;
3583 nextchr = UCHARAT(locinput);
3588 next = scan + ARG(scan);
3593 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3594 PTR2UV(scan), OP(scan));
3595 Perl_croak(aTHX_ "regexp memory corruption");
3602 * We get here only if there's trouble -- normally "case END" is
3603 * the terminating point.
3605 Perl_croak(aTHX_ "corrupted regexp pointers");
3611 PerlIO_printf(Perl_debug_log,
3612 "%*s %scould match...%s\n",
3613 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3617 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3618 PL_colors[4],PL_colors[5]));
3624 #if 0 /* Breaks $^R */
3632 PerlIO_printf(Perl_debug_log,
3633 "%*s %sfailed...%s\n",
3634 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3640 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3643 case RE_UNWIND_BRANCH:
3644 case RE_UNWIND_BRANCHJ:
3646 re_unwind_branch_t *uwb = &(uw->branch);
3647 I32 lastparen = uwb->lastparen;
3649 REGCP_UNWIND(uwb->lastcp);
3650 for (n = *PL_reglastparen; n > lastparen; n--)
3652 *PL_reglastparen = n;
3653 scan = next = uwb->next;
3655 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3656 ? BRANCH : BRANCHJ) ) { /* Failure */
3663 /* Have more choice yet. Reuse the same uwb. */
3665 if ((n = (uwb->type == RE_UNWIND_BRANCH
3666 ? NEXT_OFF(next) : ARG(next))))
3669 next = NULL; /* XXXX Needn't unwinding in this case... */
3671 next = NEXTOPER(scan);
3672 if (uwb->type == RE_UNWIND_BRANCHJ)
3673 next = NEXTOPER(next);
3674 locinput = uwb->locinput;
3675 nextchr = uwb->nextchr;
3677 PL_regindent = uwb->regindent;
3684 Perl_croak(aTHX_ "regexp unwind memory corruption");
3695 - regrepeat - repeatedly match something simple, report how many
3698 * [This routine now assumes that it will only match on things of length 1.
3699 * That was true before, but now we assume scan - reginput is the count,
3700 * rather than incrementing count on every character. [Er, except utf8.]]
3703 S_regrepeat(pTHX_ regnode *p, I32 max)
3705 register char *scan;
3707 register char *loceol = PL_regeol;
3708 register I32 hardcount = 0;
3709 register bool do_utf8 = PL_reg_match_utf8;
3712 if (max != REG_INFTY && max < loceol - scan)
3713 loceol = scan + max;
3718 while (scan < loceol && hardcount < max && *scan != '\n') {
3719 scan += UTF8SKIP(scan);
3723 while (scan < loceol && *scan != '\n')
3733 case EXACT: /* length of string is 1 */
3735 while (scan < loceol && UCHARAT(scan) == c)
3738 case EXACTF: /* length of string is 1 */
3740 while (scan < loceol &&
3741 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3744 case EXACTFL: /* length of string is 1 */
3745 PL_reg_flags |= RF_tainted;
3747 while (scan < loceol &&
3748 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3754 while (hardcount < max && scan < loceol &&
3755 reginclass(p, (U8*)scan, do_utf8)) {
3756 scan += UTF8SKIP(scan);
3760 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3767 LOAD_UTF8_CHARCLASS(alnum,"a");
3768 while (hardcount < max && scan < loceol &&
3769 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3770 scan += UTF8SKIP(scan);
3774 while (scan < loceol && isALNUM(*scan))
3779 PL_reg_flags |= RF_tainted;
3782 while (hardcount < max && scan < loceol &&
3783 isALNUM_LC_utf8((U8*)scan)) {
3784 scan += UTF8SKIP(scan);
3788 while (scan < loceol && isALNUM_LC(*scan))
3795 LOAD_UTF8_CHARCLASS(alnum,"a");
3796 while (hardcount < max && scan < loceol &&
3797 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3798 scan += UTF8SKIP(scan);
3802 while (scan < loceol && !isALNUM(*scan))
3807 PL_reg_flags |= RF_tainted;
3810 while (hardcount < max && scan < loceol &&
3811 !isALNUM_LC_utf8((U8*)scan)) {
3812 scan += UTF8SKIP(scan);
3816 while (scan < loceol && !isALNUM_LC(*scan))
3823 LOAD_UTF8_CHARCLASS(space," ");
3824 while (hardcount < max && scan < loceol &&
3826 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3827 scan += UTF8SKIP(scan);
3831 while (scan < loceol && isSPACE(*scan))
3836 PL_reg_flags |= RF_tainted;
3839 while (hardcount < max && scan < loceol &&
3840 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3841 scan += UTF8SKIP(scan);
3845 while (scan < loceol && isSPACE_LC(*scan))
3852 LOAD_UTF8_CHARCLASS(space," ");
3853 while (hardcount < max && scan < loceol &&
3855 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3856 scan += UTF8SKIP(scan);
3860 while (scan < loceol && !isSPACE(*scan))
3865 PL_reg_flags |= RF_tainted;
3868 while (hardcount < max && scan < loceol &&
3869 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3870 scan += UTF8SKIP(scan);
3874 while (scan < loceol && !isSPACE_LC(*scan))
3881 LOAD_UTF8_CHARCLASS(digit,"0");
3882 while (hardcount < max && scan < loceol &&
3883 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3884 scan += UTF8SKIP(scan);
3888 while (scan < loceol && isDIGIT(*scan))
3895 LOAD_UTF8_CHARCLASS(digit,"0");
3896 while (hardcount < max && scan < loceol &&
3897 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3898 scan += UTF8SKIP(scan);
3902 while (scan < loceol && !isDIGIT(*scan))
3906 default: /* Called on something of 0 width. */
3907 break; /* So match right here or not at all. */
3913 c = scan - PL_reginput;
3918 SV *prop = sv_newmortal();
3921 PerlIO_printf(Perl_debug_log,
3922 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3923 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3930 - regrepeat_hard - repeatedly match something, report total lenth and length
3932 * The repeater is supposed to have constant length.
3936 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3938 register char *scan = Nullch;
3939 register char *start;
3940 register char *loceol = PL_regeol;
3942 I32 count = 0, res = 1;
3947 start = PL_reginput;
3948 if (PL_reg_match_utf8) {
3949 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3952 while (start < PL_reginput) {
3954 start += UTF8SKIP(start);
3965 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3967 *lp = l = PL_reginput - start;
3968 if (max != REG_INFTY && l*max < loceol - scan)
3969 loceol = scan + l*max;
3982 - regclass_swash - prepare the utf8 swash
3986 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
3991 if (PL_regdata && PL_regdata->count) {
3994 if (PL_regdata->what[n] == 's') {
3995 SV *rv = (SV*)PL_regdata->data[n];
3996 AV *av = (AV*)SvRV((SV*)rv);
3999 si = *av_fetch(av, 0, FALSE);
4000 a = av_fetch(av, 1, FALSE);
4004 else if (si && doinit) {
4005 sw = swash_init("utf8", "", si, 1, 0);
4006 (void)av_store(av, 1, sw);
4018 - reginclass - determine if a character falls into a character class
4022 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
4024 char flags = ANYOF_FLAGS(n);
4029 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4031 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4032 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4033 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4036 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4039 SV *sw = regclass_swash(n, TRUE, 0);
4042 if (swash_fetch(sw, p, do_utf8))
4044 else if (flags & ANYOF_FOLD) {
4046 U8 tmpbuf[UTF8_MAXLEN*2+1];
4048 toLOWER_utf8(p, tmpbuf, &ulen);
4049 if (swash_fetch(sw, tmpbuf, do_utf8))
4055 if (!match && c < 256) {
4056 if (ANYOF_BITMAP_TEST(n, c))
4058 else if (flags & ANYOF_FOLD) {
4061 if (flags & ANYOF_LOCALE) {
4062 PL_reg_flags |= RF_tainted;
4063 f = PL_fold_locale[c];
4067 if (f != c && ANYOF_BITMAP_TEST(n, f))
4071 if (!match && (flags & ANYOF_CLASS)) {
4072 PL_reg_flags |= RF_tainted;
4074 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4075 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4076 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4077 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4078 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4079 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4080 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4081 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4082 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4083 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4084 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4085 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4086 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4087 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4088 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4089 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4090 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4091 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4092 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4093 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4094 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4095 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4096 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4097 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4098 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4099 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4100 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4101 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4102 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4103 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4104 ) /* How's that for a conditional? */
4111 return (flags & ANYOF_INVERT) ? !match : match;
4115 S_reghop(pTHX_ U8 *s, I32 off)
4117 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4121 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4124 while (off-- && s < lim) {
4125 /* XXX could check well-formedness here */
4133 if (UTF8_IS_CONTINUED(*s)) {
4134 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4137 /* XXX could check well-formedness here */
4145 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4147 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4151 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4154 while (off-- && s < lim) {
4155 /* XXX could check well-formedness here */
4165 if (UTF8_IS_CONTINUED(*s)) {
4166 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4169 /* XXX could check well-formedness here */
4181 restore_pos(pTHX_ void *arg)
4183 if (PL_reg_eval_set) {
4184 if (PL_reg_oldsaved) {
4185 PL_reg_re->subbeg = PL_reg_oldsaved;
4186 PL_reg_re->sublen = PL_reg_oldsavedlen;
4187 RX_MATCH_COPIED_on(PL_reg_re);
4189 PL_reg_magic->mg_len = PL_reg_oldpos;
4190 PL_reg_eval_set = 0;
4191 PL_curpm = PL_reg_oldcurpm;