5 * "One Ring to rule them all, One Ring to find them..."
8 /* This file contains functions for executing a regular expression. See
9 * also regcomp.c which funnily enough, contains functions for compiling
10 * a regular expression.
12 * This file is also copied at build time to ext/re/re_exec.c, where
13 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
14 * This causes the main functions to be compiled under new names and with
15 * debugging support added, which makes "use re 'debug'" work.
18 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
19 * confused with the original package (see point 3 below). Thanks, Henry!
22 /* Additional note: this code is very heavily munged from Henry's version
23 * in places. In some spots I've traded clarity for efficiency, so don't
24 * blame Henry for some of the lack of readability.
27 /* The names of the functions have been changed from regcomp and
28 * regexec to pregcomp and pregexec in order to avoid conflicts
29 * with the POSIX routines of the same names.
32 #ifdef PERL_EXT_RE_BUILD
37 * pregcomp and pregexec -- regsub and regerror are not used in perl
39 * Copyright (c) 1986 by University of Toronto.
40 * Written by Henry Spencer. Not derived from licensed software.
42 * Permission is granted to anyone to use this software for any
43 * purpose on any computer system, and to redistribute it freely,
44 * subject to the following restrictions:
46 * 1. The author is not responsible for the consequences of use of
47 * this software, no matter how awful, even if they arise
50 * 2. The origin of this software must not be misrepresented, either
51 * by explicit claim or by omission.
53 * 3. Altered versions must be plainly marked as such, and must not
54 * be misrepresented as being the original software.
56 **** Alterations to Henry's code are...
58 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
59 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
61 **** You may distribute under the terms of either the GNU General Public
62 **** License or the Artistic License, as specified in the README file.
64 * Beware that some of this code is subtly aware of the way operator
65 * precedence is structured in regular expressions. Serious changes in
66 * regular-expression syntax might require a total rethink.
69 #define PERL_IN_REGEXEC_C
72 #ifdef PERL_IN_XSUB_RE
78 #define RF_tainted 1 /* tainted information used? */
79 #define RF_warned 2 /* warned about big count? */
81 #define RF_utf8 8 /* Pattern contains multibyte chars? */
83 #define UTF ((PL_reg_flags & RF_utf8) != 0)
85 #define RS_init 1 /* eval environment created */
86 #define RS_set 2 /* replsv value is set */
92 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
98 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
99 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
101 #define HOPc(pos,off) \
102 (char *)(PL_reg_match_utf8 \
103 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
105 #define HOPBACKc(pos, off) \
106 (char*)(PL_reg_match_utf8\
107 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
108 : (pos - off >= PL_bostr) \
112 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
113 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
115 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
116 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
117 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
118 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
119 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
120 #define LOAD_UTF8_CHARCLASS_MARK() LOAD_UTF8_CHARCLASS(mark, "\xcd\x86")
122 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
124 /* for use after a quantifier and before an EXACT-like node -- japhy */
125 #define JUMPABLE(rn) ( \
126 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
127 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
128 OP(rn) == PLUS || OP(rn) == MINMOD || \
129 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
132 #define HAS_TEXT(rn) ( \
133 PL_regkind[OP(rn)] == EXACT || PL_regkind[OP(rn)] == REF \
137 Search for mandatory following text node; for lookahead, the text must
138 follow but for lookbehind (rn->flags != 0) we skip to the next step.
140 #define FIND_NEXT_IMPT(rn) STMT_START { \
141 while (JUMPABLE(rn)) { \
142 const OPCODE type = OP(rn); \
143 if (type == SUSPEND || PL_regkind[type] == CURLY) \
144 rn = NEXTOPER(NEXTOPER(rn)); \
145 else if (type == PLUS) \
147 else if (type == IFMATCH) \
148 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
149 else rn += NEXT_OFF(rn); \
153 static void restore_pos(pTHX_ void *arg);
156 S_regcppush(pTHX_ I32 parenfloor)
159 const int retval = PL_savestack_ix;
160 #define REGCP_PAREN_ELEMS 4
161 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
163 GET_RE_DEBUG_FLAGS_DECL;
165 if (paren_elems_to_push < 0)
166 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
168 #define REGCP_OTHER_ELEMS 6
169 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
170 for (p = PL_regsize; p > parenfloor; p--) {
171 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
172 SSPUSHINT(PL_regendp[p]);
173 SSPUSHINT(PL_regstartp[p]);
174 SSPUSHPTR(PL_reg_start_tmp[p]);
176 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
177 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
178 (UV)p, (IV)PL_regstartp[p],
179 (IV)(PL_reg_start_tmp[p] - PL_bostr),
183 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
184 SSPUSHINT(PL_regsize);
185 SSPUSHINT(*PL_reglastparen);
186 SSPUSHINT(*PL_reglastcloseparen);
187 SSPUSHPTR(PL_reginput);
188 #define REGCP_FRAME_ELEMS 2
189 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
190 * are needed for the regexp context stack bookkeeping. */
191 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
192 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
197 /* These are needed since we do not localize EVAL nodes: */
198 #define REGCP_SET(cp) \
200 PerlIO_printf(Perl_debug_log, \
201 " Setting an EVAL scope, savestack=%"IVdf"\n", \
202 (IV)PL_savestack_ix)); \
205 #define REGCP_UNWIND(cp) \
207 if (cp != PL_savestack_ix) \
208 PerlIO_printf(Perl_debug_log, \
209 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
210 (IV)(cp), (IV)PL_savestack_ix)); \
214 S_regcppop(pTHX_ const regexp *rex)
220 GET_RE_DEBUG_FLAGS_DECL;
222 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
224 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
225 i = SSPOPINT; /* Parentheses elements to pop. */
226 input = (char *) SSPOPPTR;
227 *PL_reglastcloseparen = SSPOPINT;
228 *PL_reglastparen = SSPOPINT;
229 PL_regsize = SSPOPINT;
231 /* Now restore the parentheses context. */
232 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
233 i > 0; i -= REGCP_PAREN_ELEMS) {
235 U32 paren = (U32)SSPOPINT;
236 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
237 PL_regstartp[paren] = SSPOPINT;
239 if (paren <= *PL_reglastparen)
240 PL_regendp[paren] = tmps;
242 PerlIO_printf(Perl_debug_log,
243 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
244 (UV)paren, (IV)PL_regstartp[paren],
245 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
246 (IV)PL_regendp[paren],
247 (paren > *PL_reglastparen ? "(no)" : ""));
251 if (*PL_reglastparen + 1 <= rex->nparens) {
252 PerlIO_printf(Perl_debug_log,
253 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
254 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
258 /* It would seem that the similar code in regtry()
259 * already takes care of this, and in fact it is in
260 * a better location to since this code can #if 0-ed out
261 * but the code in regtry() is needed or otherwise tests
262 * requiring null fields (pat.t#187 and split.t#{13,14}
263 * (as of patchlevel 7877) will fail. Then again,
264 * this code seems to be necessary or otherwise
265 * building DynaLoader will fail:
266 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
268 for (i = *PL_reglastparen + 1; (U32)i <= rex->nparens; i++) {
270 PL_regstartp[i] = -1;
277 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
280 * pregexec and friends
283 #ifndef PERL_IN_XSUB_RE
285 - pregexec - match a regexp against a string
288 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
289 char *strbeg, I32 minend, SV *screamer, U32 nosave)
290 /* strend: pointer to null at end of string */
291 /* strbeg: real beginning of string */
292 /* minend: end of match must be >=minend after stringarg. */
293 /* nosave: For optimizations. */
296 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
297 nosave ? 0 : REXEC_COPY_STR);
302 * Need to implement the following flags for reg_anch:
304 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
306 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
307 * INTUIT_AUTORITATIVE_ML
308 * INTUIT_ONCE_NOML - Intuit can match in one location only.
311 * Another flag for this function: SECOND_TIME (so that float substrs
312 * with giant delta may be not rechecked).
315 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
317 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
318 Otherwise, only SvCUR(sv) is used to get strbeg. */
320 /* XXXX We assume that strpos is strbeg unless sv. */
322 /* XXXX Some places assume that there is a fixed substring.
323 An update may be needed if optimizer marks as "INTUITable"
324 RExen without fixed substrings. Similarly, it is assumed that
325 lengths of all the strings are no more than minlen, thus they
326 cannot come from lookahead.
327 (Or minlen should take into account lookahead.) */
329 /* A failure to find a constant substring means that there is no need to make
330 an expensive call to REx engine, thus we celebrate a failure. Similarly,
331 finding a substring too deep into the string means that less calls to
332 regtry() should be needed.
334 REx compiler's optimizer found 4 possible hints:
335 a) Anchored substring;
337 c) Whether we are anchored (beginning-of-line or \G);
338 d) First node (of those at offset 0) which may distingush positions;
339 We use a)b)d) and multiline-part of c), and try to find a position in the
340 string which does not contradict any of them.
343 /* Most of decisions we do here should have been done at compile time.
344 The nodes of the REx which we used for the search should have been
345 deleted from the finite automaton. */
348 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
349 char *strend, U32 flags, re_scream_pos_data *data)
352 register I32 start_shift = 0;
353 /* Should be nonnegative! */
354 register I32 end_shift = 0;
359 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
361 register char *other_last = NULL; /* other substr checked before this */
362 char *check_at = NULL; /* check substr found at this pos */
363 const I32 multiline = prog->reganch & PMf_MULTILINE;
365 const char * const i_strpos = strpos;
368 GET_RE_DEBUG_FLAGS_DECL;
370 RX_MATCH_UTF8_set(prog,do_utf8);
372 if (prog->reganch & ROPT_UTF8) {
373 PL_reg_flags |= RF_utf8;
376 debug_start_match(prog, do_utf8, strpos, strend,
377 sv ? "Guessing start of match in sv for"
378 : "Guessing start of match in string for");
381 /* CHR_DIST() would be more correct here but it makes things slow. */
382 if (prog->minlen > strend - strpos) {
383 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
384 "String too short... [re_intuit_start]\n"));
388 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
391 if (!prog->check_utf8 && prog->check_substr)
392 to_utf8_substr(prog);
393 check = prog->check_utf8;
395 if (!prog->check_substr && prog->check_utf8)
396 to_byte_substr(prog);
397 check = prog->check_substr;
399 if (check == &PL_sv_undef) {
400 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
401 "Non-utf8 string cannot match utf8 check string\n"));
404 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
405 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
406 || ( (prog->reganch & ROPT_ANCH_BOL)
407 && !multiline ) ); /* Check after \n? */
410 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
411 | ROPT_IMPLICIT)) /* not a real BOL */
412 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
414 && (strpos != strbeg)) {
415 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
418 if (prog->check_offset_min == prog->check_offset_max &&
419 !(prog->reganch & ROPT_CANY_SEEN)) {
420 /* Substring at constant offset from beg-of-str... */
423 s = HOP3c(strpos, prog->check_offset_min, strend);
426 slen = SvCUR(check); /* >= 1 */
428 if ( strend - s > slen || strend - s < slen - 1
429 || (strend - s == slen && strend[-1] != '\n')) {
430 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
433 /* Now should match s[0..slen-2] */
435 if (slen && (*SvPVX_const(check) != *s
437 && memNE(SvPVX_const(check), s, slen)))) {
439 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
443 else if (*SvPVX_const(check) != *s
444 || ((slen = SvCUR(check)) > 1
445 && memNE(SvPVX_const(check), s, slen)))
448 goto success_at_start;
451 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
453 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
454 end_shift = prog->check_end_shift;
457 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
458 - (SvTAIL(check) != 0);
459 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
461 if (end_shift < eshift)
465 else { /* Can match at random position */
468 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
469 end_shift = prog->check_end_shift;
471 /* end shift should be non negative here */
474 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
476 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
477 (IV)end_shift, prog->precomp);
481 /* Find a possible match in the region s..strend by looking for
482 the "check" substring in the region corrected by start/end_shift. */
485 I32 srch_start_shift = start_shift;
486 I32 srch_end_shift = end_shift;
487 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
488 srch_end_shift -= ((strbeg - s) - srch_start_shift);
489 srch_start_shift = strbeg - s;
492 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
493 (IV)prog->check_offset_min,
494 (IV)srch_start_shift,
496 (IV)prog->check_end_shift);
499 if (flags & REXEC_SCREAM) {
500 I32 p = -1; /* Internal iterator of scream. */
501 I32 * const pp = data ? data->scream_pos : &p;
503 if (PL_screamfirst[BmRARE(check)] >= 0
504 || ( BmRARE(check) == '\n'
505 && (BmPREVIOUS(check) == SvCUR(check) - 1)
507 s = screaminstr(sv, check,
508 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
511 /* we may be pointing at the wrong string */
512 if (s && RX_MATCH_COPIED(prog))
513 s = strbeg + (s - SvPVX_const(sv));
515 *data->scream_olds = s;
520 if (prog->reganch & ROPT_CANY_SEEN) {
521 start_point= (U8*)(s + srch_start_shift);
522 end_point= (U8*)(strend - srch_end_shift);
524 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
525 end_point= HOP3(strend, -srch_end_shift, strbeg);
528 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
529 (int)(end_point - start_point),
530 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
534 s = fbm_instr( start_point, end_point,
535 check, multiline ? FBMrf_MULTILINE : 0);
538 /* Update the count-of-usability, remove useless subpatterns,
542 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
543 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
544 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
545 (s ? "Found" : "Did not find"),
546 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
547 ? "anchored" : "floating"),
550 (s ? " at offset " : "...\n") );
555 /* Finish the diagnostic message */
556 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
558 /* XXX dmq: first branch is for positive lookbehind...
559 Our check string is offset from the beginning of the pattern.
560 So we need to do any stclass tests offset forward from that
569 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
570 Start with the other substr.
571 XXXX no SCREAM optimization yet - and a very coarse implementation
572 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
573 *always* match. Probably should be marked during compile...
574 Probably it is right to do no SCREAM here...
577 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
578 : (prog->float_substr && prog->anchored_substr))
580 /* Take into account the "other" substring. */
581 /* XXXX May be hopelessly wrong for UTF... */
584 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
587 char * const last = HOP3c(s, -start_shift, strbeg);
589 char * const saved_s = s;
592 t = s - prog->check_offset_max;
593 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
595 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
600 t = HOP3c(t, prog->anchored_offset, strend);
601 if (t < other_last) /* These positions already checked */
603 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
606 /* XXXX It is not documented what units *_offsets are in.
607 We assume bytes, but this is clearly wrong.
608 Meaning this code needs to be carefully reviewed for errors.
612 /* On end-of-str: see comment below. */
613 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
614 if (must == &PL_sv_undef) {
616 DEBUG_r(must = prog->anchored_utf8); /* for debug */
621 HOP3(HOP3(last1, prog->anchored_offset, strend)
622 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
624 multiline ? FBMrf_MULTILINE : 0
627 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
628 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
629 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
630 (s ? "Found" : "Contradicts"),
631 quoted, RE_SV_TAIL(must));
636 if (last1 >= last2) {
637 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
638 ", giving up...\n"));
641 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
642 ", trying floating at offset %ld...\n",
643 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
644 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
645 s = HOP3c(last, 1, strend);
649 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
650 (long)(s - i_strpos)));
651 t = HOP3c(s, -prog->anchored_offset, strbeg);
652 other_last = HOP3c(s, 1, strend);
660 else { /* Take into account the floating substring. */
662 char * const saved_s = s;
665 t = HOP3c(s, -start_shift, strbeg);
667 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
668 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
669 last = HOP3c(t, prog->float_max_offset, strend);
670 s = HOP3c(t, prog->float_min_offset, strend);
673 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
674 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
675 /* fbm_instr() takes into account exact value of end-of-str
676 if the check is SvTAIL(ed). Since false positives are OK,
677 and end-of-str is not later than strend we are OK. */
678 if (must == &PL_sv_undef) {
680 DEBUG_r(must = prog->float_utf8); /* for debug message */
683 s = fbm_instr((unsigned char*)s,
684 (unsigned char*)last + SvCUR(must)
686 must, multiline ? FBMrf_MULTILINE : 0);
688 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
689 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
690 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
691 (s ? "Found" : "Contradicts"),
692 quoted, RE_SV_TAIL(must));
696 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
697 ", giving up...\n"));
700 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
701 ", trying anchored starting at offset %ld...\n",
702 (long)(saved_s + 1 - i_strpos)));
704 s = HOP3c(t, 1, strend);
708 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
709 (long)(s - i_strpos)));
710 other_last = s; /* Fix this later. --Hugo */
720 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
723 PerlIO_printf(Perl_debug_log,
724 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
725 (IV)prog->check_offset_min,
726 (IV)prog->check_offset_max,
734 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
736 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
739 /* Fixed substring is found far enough so that the match
740 cannot start at strpos. */
742 if (ml_anch && t[-1] != '\n') {
743 /* Eventually fbm_*() should handle this, but often
744 anchored_offset is not 0, so this check will not be wasted. */
745 /* XXXX In the code below we prefer to look for "^" even in
746 presence of anchored substrings. And we search even
747 beyond the found float position. These pessimizations
748 are historical artefacts only. */
750 while (t < strend - prog->minlen) {
752 if (t < check_at - prog->check_offset_min) {
753 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
754 /* Since we moved from the found position,
755 we definitely contradict the found anchored
756 substr. Due to the above check we do not
757 contradict "check" substr.
758 Thus we can arrive here only if check substr
759 is float. Redo checking for "other"=="fixed".
762 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
763 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
764 goto do_other_anchored;
766 /* We don't contradict the found floating substring. */
767 /* XXXX Why not check for STCLASS? */
769 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
770 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
773 /* Position contradicts check-string */
774 /* XXXX probably better to look for check-string
775 than for "\n", so one should lower the limit for t? */
776 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
777 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
778 other_last = strpos = s = t + 1;
783 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
784 PL_colors[0], PL_colors[1]));
788 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
789 PL_colors[0], PL_colors[1]));
793 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
796 /* The found string does not prohibit matching at strpos,
797 - no optimization of calling REx engine can be performed,
798 unless it was an MBOL and we are not after MBOL,
799 or a future STCLASS check will fail this. */
801 /* Even in this situation we may use MBOL flag if strpos is offset
802 wrt the start of the string. */
803 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
804 && (strpos != strbeg) && strpos[-1] != '\n'
805 /* May be due to an implicit anchor of m{.*foo} */
806 && !(prog->reganch & ROPT_IMPLICIT))
811 DEBUG_EXECUTE_r( if (ml_anch)
812 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
813 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
816 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
818 prog->check_utf8 /* Could be deleted already */
819 && --BmUSEFUL(prog->check_utf8) < 0
820 && (prog->check_utf8 == prog->float_utf8)
822 prog->check_substr /* Could be deleted already */
823 && --BmUSEFUL(prog->check_substr) < 0
824 && (prog->check_substr == prog->float_substr)
827 /* If flags & SOMETHING - do not do it many times on the same match */
828 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
829 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
830 if (do_utf8 ? prog->check_substr : prog->check_utf8)
831 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
832 prog->check_substr = prog->check_utf8 = NULL; /* disable */
833 prog->float_substr = prog->float_utf8 = NULL; /* clear */
834 check = NULL; /* abort */
836 /* XXXX This is a remnant of the old implementation. It
837 looks wasteful, since now INTUIT can use many
839 prog->reganch &= ~RE_USE_INTUIT;
846 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
847 /* trie stclasses are too expensive to use here, we are better off to
848 leave it to regmatch itself */
849 if (prog->regstclass && PL_regkind[OP(prog->regstclass)]!=TRIE) {
850 /* minlen == 0 is possible if regstclass is \b or \B,
851 and the fixed substr is ''$.
852 Since minlen is already taken into account, s+1 is before strend;
853 accidentally, minlen >= 1 guaranties no false positives at s + 1
854 even for \b or \B. But (minlen? 1 : 0) below assumes that
855 regstclass does not come from lookahead... */
856 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
857 This leaves EXACTF only, which is dealt with in find_byclass(). */
858 const U8* const str = (U8*)STRING(prog->regstclass);
859 const int cl_l = (PL_regkind[OP(prog->regstclass)] == EXACT
860 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
863 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
864 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
865 else if (prog->float_substr || prog->float_utf8)
866 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
870 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %d s: %d endpos: %d\n",
871 (IV)start_shift, check_at - strbeg, s - strbeg, endpos - strbeg));
874 s = find_byclass(prog, prog->regstclass, s, endpos, NULL);
877 const char *what = NULL;
879 if (endpos == strend) {
880 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
881 "Could not match STCLASS...\n") );
884 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
885 "This position contradicts STCLASS...\n") );
886 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
888 /* Contradict one of substrings */
889 if (prog->anchored_substr || prog->anchored_utf8) {
890 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
891 DEBUG_EXECUTE_r( what = "anchored" );
893 s = HOP3c(t, 1, strend);
894 if (s + start_shift + end_shift > strend) {
895 /* XXXX Should be taken into account earlier? */
896 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
897 "Could not match STCLASS...\n") );
902 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
903 "Looking for %s substr starting at offset %ld...\n",
904 what, (long)(s + start_shift - i_strpos)) );
907 /* Have both, check_string is floating */
908 if (t + start_shift >= check_at) /* Contradicts floating=check */
909 goto retry_floating_check;
910 /* Recheck anchored substring, but not floating... */
914 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
915 "Looking for anchored substr starting at offset %ld...\n",
916 (long)(other_last - i_strpos)) );
917 goto do_other_anchored;
919 /* Another way we could have checked stclass at the
920 current position only: */
925 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
926 "Looking for /%s^%s/m starting at offset %ld...\n",
927 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
930 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
932 /* Check is floating subtring. */
933 retry_floating_check:
934 t = check_at - start_shift;
935 DEBUG_EXECUTE_r( what = "floating" );
936 goto hop_and_restart;
939 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
940 "By STCLASS: moving %ld --> %ld\n",
941 (long)(t - i_strpos), (long)(s - i_strpos))
945 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
946 "Does not contradict STCLASS...\n");
951 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
952 PL_colors[4], (check ? "Guessed" : "Giving up"),
953 PL_colors[5], (long)(s - i_strpos)) );
956 fail_finish: /* Substring not found */
957 if (prog->check_substr || prog->check_utf8) /* could be removed already */
958 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
960 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
961 PL_colors[4], PL_colors[5]));
967 #define REXEC_TRIE_READ_CHAR(trie_type, trie, uc, uscan, len, uvc, charid, \
968 foldlen, foldbuf, uniflags) STMT_START { \
969 switch (trie_type) { \
970 case trie_utf8_fold: \
972 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
977 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
978 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
979 foldlen -= UNISKIP( uvc ); \
980 uscan = foldbuf + UNISKIP( uvc ); \
984 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
992 charid = trie->charmap[ uvc ]; \
996 if (trie->widecharmap) { \
997 SV** const svpp = hv_fetch(trie->widecharmap, \
998 (char*)&uvc, sizeof(UV), 0); \
1000 charid = (U16)SvIV(*svpp); \
1005 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1008 ibcmp_utf8(s, NULL, 0, do_utf8, \
1009 m, NULL, ln, (bool)UTF)) \
1010 && (!reginfo || regtry(reginfo, s)) ) \
1013 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1014 uvchr_to_utf8(tmpbuf, c); \
1015 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1017 && (f == c1 || f == c2) \
1018 && (ln == foldlen || \
1019 !ibcmp_utf8((char *) foldbuf, \
1020 NULL, foldlen, do_utf8, \
1022 NULL, ln, (bool)UTF)) \
1023 && (!reginfo || regtry(reginfo, s)) ) \
1028 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1032 && (ln == 1 || !(OP(c) == EXACTF \
1034 : ibcmp_locale(s, m, ln))) \
1035 && (!reginfo || regtry(reginfo, s)) ) \
1041 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1043 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1049 #define REXEC_FBC_SCAN(CoDe) \
1051 while (s < strend) { \
1057 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1058 REXEC_FBC_UTF8_SCAN( \
1060 if (tmp && (!reginfo || regtry(reginfo, s))) \
1069 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1072 if (tmp && (!reginfo || regtry(reginfo, s))) \
1081 #define REXEC_FBC_TRYIT \
1082 if ((!reginfo || regtry(reginfo, s))) \
1085 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1088 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1091 REXEC_FBC_CLASS_SCAN(CoNd); \
1095 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1096 PL_reg_flags |= RF_tainted; \
1098 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1101 REXEC_FBC_CLASS_SCAN(CoNd); \
1105 #define DUMP_EXEC_POS(li,s,doutf8) \
1106 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1108 /* We know what class REx starts with. Try to find this position... */
1109 /* if reginfo is NULL, its a dryrun */
1110 /* annoyingly all the vars in this routine have different names from their counterparts
1111 in regmatch. /grrr */
1114 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1115 const char *strend, const regmatch_info *reginfo)
1118 const I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
1122 register STRLEN uskip;
1126 register I32 tmp = 1; /* Scratch variable? */
1127 register const bool do_utf8 = PL_reg_match_utf8;
1129 /* We know what class it must start with. */
1133 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1134 !UTF8_IS_INVARIANT((U8)s[0]) ?
1135 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1136 REGINCLASS(prog, c, (U8*)s));
1139 while (s < strend) {
1142 if (REGINCLASS(prog, c, (U8*)s) ||
1143 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1144 /* The assignment of 2 is intentional:
1145 * for the folded sharp s, the skip is 2. */
1146 (skip = SHARP_S_SKIP))) {
1147 if (tmp && (!reginfo || regtry(reginfo, s)))
1160 if (tmp && (!reginfo || regtry(reginfo, s)))
1168 ln = STR_LEN(c); /* length to match in octets/bytes */
1169 lnc = (I32) ln; /* length to match in characters */
1171 STRLEN ulen1, ulen2;
1173 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1174 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1175 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1177 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1178 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1180 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1182 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1185 while (sm < ((U8 *) m + ln)) {
1200 c2 = PL_fold_locale[c1];
1202 e = HOP3c(strend, -((I32)lnc), s);
1204 if (!reginfo && e < s)
1205 e = s; /* Due to minlen logic of intuit() */
1207 /* The idea in the EXACTF* cases is to first find the
1208 * first character of the EXACTF* node and then, if
1209 * necessary, case-insensitively compare the full
1210 * text of the node. The c1 and c2 are the first
1211 * characters (though in Unicode it gets a bit
1212 * more complicated because there are more cases
1213 * than just upper and lower: one needs to use
1214 * the so-called folding case for case-insensitive
1215 * matching (called "loose matching" in Unicode).
1216 * ibcmp_utf8() will do just that. */
1220 U8 tmpbuf [UTF8_MAXBYTES+1];
1221 STRLEN len, foldlen;
1222 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1224 /* Upper and lower of 1st char are equal -
1225 * probably not a "letter". */
1227 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1229 REXEC_FBC_EXACTISH_CHECK(c == c1);
1234 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1237 /* Handle some of the three Greek sigmas cases.
1238 * Note that not all the possible combinations
1239 * are handled here: some of them are handled
1240 * by the standard folding rules, and some of
1241 * them (the character class or ANYOF cases)
1242 * are handled during compiletime in
1243 * regexec.c:S_regclass(). */
1244 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1245 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1246 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1248 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1254 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1256 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1260 PL_reg_flags |= RF_tainted;
1267 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1268 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1270 tmp = ((OP(c) == BOUND ?
1271 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1272 LOAD_UTF8_CHARCLASS_ALNUM();
1273 REXEC_FBC_UTF8_SCAN(
1274 if (tmp == !(OP(c) == BOUND ?
1275 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1276 isALNUM_LC_utf8((U8*)s)))
1284 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1285 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1288 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1294 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, s)))
1298 PL_reg_flags |= RF_tainted;
1305 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1306 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1308 tmp = ((OP(c) == NBOUND ?
1309 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1310 LOAD_UTF8_CHARCLASS_ALNUM();
1311 REXEC_FBC_UTF8_SCAN(
1312 if (tmp == !(OP(c) == NBOUND ?
1313 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1314 isALNUM_LC_utf8((U8*)s)))
1316 else REXEC_FBC_TRYIT;
1320 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1321 tmp = ((OP(c) == NBOUND ?
1322 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1325 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1327 else REXEC_FBC_TRYIT;
1330 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, s)))
1334 REXEC_FBC_CSCAN_PRELOAD(
1335 LOAD_UTF8_CHARCLASS_ALNUM(),
1336 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1340 REXEC_FBC_CSCAN_TAINT(
1341 isALNUM_LC_utf8((U8*)s),
1345 REXEC_FBC_CSCAN_PRELOAD(
1346 LOAD_UTF8_CHARCLASS_ALNUM(),
1347 !swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1351 REXEC_FBC_CSCAN_TAINT(
1352 !isALNUM_LC_utf8((U8*)s),
1356 REXEC_FBC_CSCAN_PRELOAD(
1357 LOAD_UTF8_CHARCLASS_SPACE(),
1358 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8),
1362 REXEC_FBC_CSCAN_TAINT(
1363 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1367 REXEC_FBC_CSCAN_PRELOAD(
1368 LOAD_UTF8_CHARCLASS_SPACE(),
1369 !(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)),
1373 REXEC_FBC_CSCAN_TAINT(
1374 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1378 REXEC_FBC_CSCAN_PRELOAD(
1379 LOAD_UTF8_CHARCLASS_DIGIT(),
1380 swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1384 REXEC_FBC_CSCAN_TAINT(
1385 isDIGIT_LC_utf8((U8*)s),
1389 REXEC_FBC_CSCAN_PRELOAD(
1390 LOAD_UTF8_CHARCLASS_DIGIT(),
1391 !swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1395 REXEC_FBC_CSCAN_TAINT(
1396 !isDIGIT_LC_utf8((U8*)s),
1402 const enum { trie_plain, trie_utf8, trie_utf8_fold }
1403 trie_type = do_utf8 ?
1404 (c->flags == EXACT ? trie_utf8 : trie_utf8_fold)
1406 /* what trie are we using right now */
1408 = (reg_ac_data*)prog->data->data[ ARG( c ) ];
1409 reg_trie_data *trie=aho->trie;
1411 const char *last_start = strend - trie->minlen;
1413 const char *real_start = s;
1415 STRLEN maxlen = trie->maxlen;
1417 U8 **points; /* map of where we were in the input string
1418 when reading a given char. For ASCII this
1419 is unnecessary overhead as the relationship
1420 is always 1:1, but for unicode, especially
1421 case folded unicode this is not true. */
1422 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1426 GET_RE_DEBUG_FLAGS_DECL;
1428 /* We can't just allocate points here. We need to wrap it in
1429 * an SV so it gets freed properly if there is a croak while
1430 * running the match */
1433 sv_points=newSV(maxlen * sizeof(U8 *));
1434 SvCUR_set(sv_points,
1435 maxlen * sizeof(U8 *));
1436 SvPOK_on(sv_points);
1437 sv_2mortal(sv_points);
1438 points=(U8**)SvPV_nolen(sv_points );
1439 if ( trie_type != trie_utf8_fold
1440 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1443 bitmap=(U8*)trie->bitmap;
1445 bitmap=(U8*)ANYOF_BITMAP(c);
1447 /* this is the Aho-Corasick algorithm modified a touch
1448 to include special handling for long "unknown char"
1449 sequences. The basic idea being that we use AC as long
1450 as we are dealing with a possible matching char, when
1451 we encounter an unknown char (and we have not encountered
1452 an accepting state) we scan forward until we find a legal
1454 AC matching is basically that of trie matching, except
1455 that when we encounter a failing transition, we fall back
1456 to the current states "fail state", and try the current char
1457 again, a process we repeat until we reach the root state,
1458 state 1, or a legal transition. If we fail on the root state
1459 then we can either terminate if we have reached an accepting
1460 state previously, or restart the entire process from the beginning
1464 while (s <= last_start) {
1465 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1473 U8 *uscan = (U8*)NULL;
1474 U8 *leftmost = NULL;
1476 U32 accepted_word= 0;
1480 while ( state && uc <= (U8*)strend ) {
1482 U32 word = aho->states[ state ].wordnum;
1486 DEBUG_TRIE_EXECUTE_r(
1487 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1488 dump_exec_pos( (char *)uc, c, strend, real_start,
1489 (char *)uc, do_utf8 );
1490 PerlIO_printf( Perl_debug_log,
1491 " Scanning for legal start char...\n");
1494 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1499 if (uc >(U8*)last_start) break;
1503 U8 *lpos= points[ (pointpos - trie->wordlen[word-1] ) % maxlen ];
1504 if (!leftmost || lpos < leftmost) {
1505 DEBUG_r(accepted_word=word);
1511 points[pointpos++ % maxlen]= uc;
1512 REXEC_TRIE_READ_CHAR(trie_type, trie, uc, uscan, len,
1513 uvc, charid, foldlen, foldbuf, uniflags);
1514 DEBUG_TRIE_EXECUTE_r({
1515 dump_exec_pos( (char *)uc, c, strend, real_start,
1517 PerlIO_printf(Perl_debug_log,
1518 " Charid:%3u CP:%4"UVxf" ",
1524 word = aho->states[ state ].wordnum;
1526 base = aho->states[ state ].trans.base;
1528 DEBUG_TRIE_EXECUTE_r({
1530 dump_exec_pos( (char *)uc, c, strend, real_start,
1532 PerlIO_printf( Perl_debug_log,
1533 "%sState: %4"UVxf", word=%"UVxf,
1534 failed ? " Fail transition to " : "",
1535 (UV)state, (UV)word);
1540 (base + charid > trie->uniquecharcount )
1541 && (base + charid - 1 - trie->uniquecharcount
1543 && trie->trans[base + charid - 1 -
1544 trie->uniquecharcount].check == state
1545 && (tmp=trie->trans[base + charid - 1 -
1546 trie->uniquecharcount ].next))
1548 DEBUG_TRIE_EXECUTE_r(
1549 PerlIO_printf( Perl_debug_log," - legal\n"));
1554 DEBUG_TRIE_EXECUTE_r(
1555 PerlIO_printf( Perl_debug_log," - fail\n"));
1557 state = aho->fail[state];
1561 /* we must be accepting here */
1562 DEBUG_TRIE_EXECUTE_r(
1563 PerlIO_printf( Perl_debug_log," - accepting\n"));
1572 if (!state) state = 1;
1575 if ( aho->states[ state ].wordnum ) {
1576 U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ];
1577 if (!leftmost || lpos < leftmost) {
1578 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1583 s = (char*)leftmost;
1584 DEBUG_TRIE_EXECUTE_r({
1586 Perl_debug_log,"Matches word #%"UVxf" at position %d. Trying full pattern...\n",
1587 (UV)accepted_word, s - real_start
1590 if (!reginfo || regtry(reginfo, s)) {
1596 DEBUG_TRIE_EXECUTE_r({
1597 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1600 DEBUG_TRIE_EXECUTE_r(
1601 PerlIO_printf( Perl_debug_log,"No match.\n"));
1610 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1619 - regexec_flags - match a regexp against a string
1622 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1623 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1624 /* strend: pointer to null at end of string */
1625 /* strbeg: real beginning of string */
1626 /* minend: end of match must be >=minend after stringarg. */
1627 /* data: May be used for some additional optimizations. */
1628 /* nosave: For optimizations. */
1632 register regnode *c;
1633 register char *startpos = stringarg;
1634 I32 minlen; /* must match at least this many chars */
1635 I32 dontbother = 0; /* how many characters not to try at end */
1636 I32 end_shift = 0; /* Same for the end. */ /* CC */
1637 I32 scream_pos = -1; /* Internal iterator of scream. */
1638 char *scream_olds = NULL;
1639 SV* const oreplsv = GvSV(PL_replgv);
1640 const bool do_utf8 = DO_UTF8(sv);
1643 regmatch_info reginfo; /* create some info to pass to regtry etc */
1645 GET_RE_DEBUG_FLAGS_DECL;
1647 PERL_UNUSED_ARG(data);
1649 /* Be paranoid... */
1650 if (prog == NULL || startpos == NULL) {
1651 Perl_croak(aTHX_ "NULL regexp parameter");
1655 multiline = prog->reganch & PMf_MULTILINE;
1656 reginfo.prog = prog;
1658 RX_MATCH_UTF8_set(prog, do_utf8);
1660 debug_start_match(prog, do_utf8, startpos, strend,
1664 minlen = prog->minlen;
1666 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1667 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1668 "String too short [regexec_flags]...\n"));
1673 /* Check validity of program. */
1674 if (UCHARAT(prog->program) != REG_MAGIC) {
1675 Perl_croak(aTHX_ "corrupted regexp program");
1679 PL_reg_eval_set = 0;
1682 if (prog->reganch & ROPT_UTF8)
1683 PL_reg_flags |= RF_utf8;
1685 /* Mark beginning of line for ^ and lookbehind. */
1686 reginfo.bol = startpos; /* XXX not used ??? */
1690 /* Mark end of line for $ (and such) */
1693 /* see how far we have to get to not match where we matched before */
1694 reginfo.till = startpos+minend;
1696 /* If there is a "must appear" string, look for it. */
1699 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to set reginfo->ganch */
1702 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1703 reginfo.ganch = startpos;
1704 else if (sv && SvTYPE(sv) >= SVt_PVMG
1706 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1707 && mg->mg_len >= 0) {
1708 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1709 if (prog->reganch & ROPT_ANCH_GPOS) {
1710 if (s > reginfo.ganch)
1715 else /* pos() not defined */
1716 reginfo.ganch = strbeg;
1719 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1720 re_scream_pos_data d;
1722 d.scream_olds = &scream_olds;
1723 d.scream_pos = &scream_pos;
1724 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1726 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1727 goto phooey; /* not present */
1733 /* Simplest case: anchored match need be tried only once. */
1734 /* [unless only anchor is BOL and multiline is set] */
1735 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1736 if (s == startpos && regtry(®info, startpos))
1738 else if (multiline || (prog->reganch & ROPT_IMPLICIT)
1739 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1744 dontbother = minlen - 1;
1745 end = HOP3c(strend, -dontbother, strbeg) - 1;
1746 /* for multiline we only have to try after newlines */
1747 if (prog->check_substr || prog->check_utf8) {
1751 if (regtry(®info, s))
1756 if (prog->reganch & RE_USE_INTUIT) {
1757 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1768 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1769 if (regtry(®info, s))
1776 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1777 if (regtry(®info, reginfo.ganch))
1782 /* Messy cases: unanchored match. */
1783 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1784 /* we have /x+whatever/ */
1785 /* it must be a one character string (XXXX Except UTF?) */
1790 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1791 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1792 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1797 DEBUG_EXECUTE_r( did_match = 1 );
1798 if (regtry(®info, s)) goto got_it;
1800 while (s < strend && *s == ch)
1808 DEBUG_EXECUTE_r( did_match = 1 );
1809 if (regtry(®info, s)) goto got_it;
1811 while (s < strend && *s == ch)
1816 DEBUG_EXECUTE_r(if (!did_match)
1817 PerlIO_printf(Perl_debug_log,
1818 "Did not find anchored character...\n")
1821 else if (prog->anchored_substr != NULL
1822 || prog->anchored_utf8 != NULL
1823 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
1824 && prog->float_max_offset < strend - s)) {
1829 char *last1; /* Last position checked before */
1833 if (prog->anchored_substr || prog->anchored_utf8) {
1834 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1835 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1836 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1837 back_max = back_min = prog->anchored_offset;
1839 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1840 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1841 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1842 back_max = prog->float_max_offset;
1843 back_min = prog->float_min_offset;
1847 if (must == &PL_sv_undef)
1848 /* could not downgrade utf8 check substring, so must fail */
1854 last = HOP3c(strend, /* Cannot start after this */
1855 -(I32)(CHR_SVLEN(must)
1856 - (SvTAIL(must) != 0) + back_min), strbeg);
1859 last1 = HOPc(s, -1);
1861 last1 = s - 1; /* bogus */
1863 /* XXXX check_substr already used to find "s", can optimize if
1864 check_substr==must. */
1866 dontbother = end_shift;
1867 strend = HOPc(strend, -dontbother);
1868 while ( (s <= last) &&
1869 ((flags & REXEC_SCREAM)
1870 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
1871 end_shift, &scream_pos, 0))
1872 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
1873 (unsigned char*)strend, must,
1874 multiline ? FBMrf_MULTILINE : 0))) ) {
1875 /* we may be pointing at the wrong string */
1876 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1877 s = strbeg + (s - SvPVX_const(sv));
1878 DEBUG_EXECUTE_r( did_match = 1 );
1879 if (HOPc(s, -back_max) > last1) {
1880 last1 = HOPc(s, -back_min);
1881 s = HOPc(s, -back_max);
1884 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1886 last1 = HOPc(s, -back_min);
1890 while (s <= last1) {
1891 if (regtry(®info, s))
1897 while (s <= last1) {
1898 if (regtry(®info, s))
1904 DEBUG_EXECUTE_r(if (!did_match) {
1905 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
1906 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
1907 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
1908 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1909 ? "anchored" : "floating"),
1910 quoted, RE_SV_TAIL(must));
1914 else if ( (c = prog->regstclass) ) {
1916 const OPCODE op = OP(prog->regstclass);
1917 /* don't bother with what can't match */
1918 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
1919 strend = HOPc(strend, -(minlen - 1));
1922 SV * const prop = sv_newmortal();
1923 regprop(prog, prop, c);
1925 RE_PV_QUOTED_DECL(quoted,UTF,PERL_DEBUG_PAD_ZERO(1),
1927 PerlIO_printf(Perl_debug_log,
1928 "Matching stclass %.*s against %s (%d chars)\n",
1929 (int)SvCUR(prop), SvPVX_const(prop),
1930 quoted, (int)(strend - s));
1933 if (find_byclass(prog, c, s, strend, ®info))
1935 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
1939 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
1944 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1945 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1946 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1948 if (flags & REXEC_SCREAM) {
1949 last = screaminstr(sv, float_real, s - strbeg,
1950 end_shift, &scream_pos, 1); /* last one */
1952 last = scream_olds; /* Only one occurrence. */
1953 /* we may be pointing at the wrong string */
1954 else if (RX_MATCH_COPIED(prog))
1955 s = strbeg + (s - SvPVX_const(sv));
1959 const char * const little = SvPV_const(float_real, len);
1961 if (SvTAIL(float_real)) {
1962 if (memEQ(strend - len + 1, little, len - 1))
1963 last = strend - len + 1;
1964 else if (!multiline)
1965 last = memEQ(strend - len, little, len)
1966 ? strend - len : NULL;
1972 last = rninstr(s, strend, little, little + len);
1974 last = strend; /* matching "$" */
1978 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1979 "%sCan't trim the tail, match fails (should not happen)%s\n",
1980 PL_colors[4], PL_colors[5]));
1981 goto phooey; /* Should not happen! */
1983 dontbother = strend - last + prog->float_min_offset;
1985 if (minlen && (dontbother < minlen))
1986 dontbother = minlen - 1;
1987 strend -= dontbother; /* this one's always in bytes! */
1988 /* We don't know much -- general case. */
1991 if (regtry(®info, s))
2000 if (regtry(®info, s))
2002 } while (s++ < strend);
2010 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2012 if (PL_reg_eval_set) {
2013 /* Preserve the current value of $^R */
2014 if (oreplsv != GvSV(PL_replgv))
2015 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2016 restored, the value remains
2018 restore_pos(aTHX_ prog);
2021 /* make sure $`, $&, $', and $digit will work later */
2022 if ( !(flags & REXEC_NOT_FIRST) ) {
2023 RX_MATCH_COPY_FREE(prog);
2024 if (flags & REXEC_COPY_STR) {
2025 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2026 #ifdef PERL_OLD_COPY_ON_WRITE
2028 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2030 PerlIO_printf(Perl_debug_log,
2031 "Copy on write: regexp capture, type %d\n",
2034 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2035 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2036 assert (SvPOKp(prog->saved_copy));
2040 RX_MATCH_COPIED_on(prog);
2041 s = savepvn(strbeg, i);
2047 prog->subbeg = strbeg;
2048 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2055 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2056 PL_colors[4], PL_colors[5]));
2057 if (PL_reg_eval_set)
2058 restore_pos(aTHX_ prog);
2063 - regtry - try match at specific point
2065 STATIC I32 /* 0 failure, 1 success */
2066 S_regtry(pTHX_ const regmatch_info *reginfo, char *startpos)
2072 regexp *prog = reginfo->prog;
2073 GET_RE_DEBUG_FLAGS_DECL;
2075 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2078 PL_reg_eval_set = RS_init;
2079 DEBUG_EXECUTE_r(DEBUG_s(
2080 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2081 (IV)(PL_stack_sp - PL_stack_base));
2083 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2084 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2085 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2087 /* Apparently this is not needed, judging by wantarray. */
2088 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2089 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2092 /* Make $_ available to executed code. */
2093 if (reginfo->sv != DEFSV) {
2095 DEFSV = reginfo->sv;
2098 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2099 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2100 /* prepare for quick setting of pos */
2101 #ifdef PERL_OLD_COPY_ON_WRITE
2103 sv_force_normal_flags(sv, 0);
2105 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2106 &PL_vtbl_mglob, NULL, 0);
2110 PL_reg_oldpos = mg->mg_len;
2111 SAVEDESTRUCTOR_X(restore_pos, prog);
2113 if (!PL_reg_curpm) {
2114 Newxz(PL_reg_curpm, 1, PMOP);
2117 SV* const repointer = newSViv(0);
2118 /* so we know which PL_regex_padav element is PL_reg_curpm */
2119 SvFLAGS(repointer) |= SVf_BREAK;
2120 av_push(PL_regex_padav,repointer);
2121 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2122 PL_regex_pad = AvARRAY(PL_regex_padav);
2126 PM_SETRE(PL_reg_curpm, prog);
2127 PL_reg_oldcurpm = PL_curpm;
2128 PL_curpm = PL_reg_curpm;
2129 if (RX_MATCH_COPIED(prog)) {
2130 /* Here is a serious problem: we cannot rewrite subbeg,
2131 since it may be needed if this match fails. Thus
2132 $` inside (?{}) could fail... */
2133 PL_reg_oldsaved = prog->subbeg;
2134 PL_reg_oldsavedlen = prog->sublen;
2135 #ifdef PERL_OLD_COPY_ON_WRITE
2136 PL_nrs = prog->saved_copy;
2138 RX_MATCH_COPIED_off(prog);
2141 PL_reg_oldsaved = NULL;
2142 prog->subbeg = PL_bostr;
2143 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2145 prog->startp[0] = startpos - PL_bostr;
2146 PL_reginput = startpos;
2147 PL_regstartp = prog->startp;
2148 PL_regendp = prog->endp;
2149 PL_reglastparen = &prog->lastparen;
2150 PL_reglastcloseparen = &prog->lastcloseparen;
2151 prog->lastparen = 0;
2152 prog->lastcloseparen = 0;
2154 DEBUG_EXECUTE_r(PL_reg_starttry = startpos);
2155 if (PL_reg_start_tmpl <= prog->nparens) {
2156 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2157 if(PL_reg_start_tmp)
2158 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2160 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2163 /* XXXX What this code is doing here?!!! There should be no need
2164 to do this again and again, PL_reglastparen should take care of
2167 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2168 * Actually, the code in regcppop() (which Ilya may be meaning by
2169 * PL_reglastparen), is not needed at all by the test suite
2170 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2171 * enough, for building DynaLoader, or otherwise this
2172 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2173 * will happen. Meanwhile, this code *is* needed for the
2174 * above-mentioned test suite tests to succeed. The common theme
2175 * on those tests seems to be returning null fields from matches.
2180 if (prog->nparens) {
2182 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2189 if (regmatch(reginfo, prog->program + 1)) {
2190 prog->endp[0] = PL_reginput - PL_bostr;
2193 REGCP_UNWIND(lastcp);
2198 #define sayYES goto yes
2199 #define sayNO goto no
2200 #define sayNO_ANYOF goto no_anyof
2201 #define sayYES_FINAL goto yes_final
2202 #define sayNO_FINAL goto no_final
2203 #define sayNO_SILENT goto do_no
2204 #define saySAME(x) if (x) goto yes; else goto no
2206 #define CACHEsayNO STMT_START { \
2207 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) \
2208 PL_reg_poscache[st->u.whilem.cache_offset] |= \
2209 (1<<st->u.whilem.cache_bit); \
2214 /* this is used to determine how far from the left messages like
2215 'failed...' are printed. Currently 29 makes these messages line
2216 up with the opcode they refer to. Earlier perls used 25 which
2217 left these messages outdented making reviewing a debug output
2220 #define REPORT_CODE_OFF 29
2223 /* Make sure there is a test for this +1 options in re_tests */
2224 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2226 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2227 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2229 #define SLAB_FIRST(s) (&(s)->states[0])
2230 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2232 /* grab a new slab and return the first slot in it */
2234 STATIC regmatch_state *
2237 #if PERL_VERSION < 9
2240 regmatch_slab *s = PL_regmatch_slab->next;
2242 Newx(s, 1, regmatch_slab);
2243 s->prev = PL_regmatch_slab;
2245 PL_regmatch_slab->next = s;
2247 PL_regmatch_slab = s;
2248 return SLAB_FIRST(s);
2251 /* simulate a recursive call to regmatch */
2253 #define REGMATCH(ns, where) \
2256 st->resume_state = resume_##where; \
2257 goto start_recurse; \
2258 resume_point_##where:
2260 /* push a new state then goto it */
2262 #define PUSH_STATE_GOTO(state, node) \
2264 st->resume_state = state; \
2267 /* push a new state with success backtracking, then goto it */
2269 #define PUSH_YES_STATE_GOTO(state, node) \
2271 st->resume_state = state; \
2272 goto push_yes_state;
2277 - regmatch - main matching routine
2279 * Conceptually the strategy is simple: check to see whether the current
2280 * node matches, call self recursively to see whether the rest matches,
2281 * and then act accordingly. In practice we make some effort to avoid
2282 * recursion, in particular by going through "ordinary" nodes (that don't
2283 * need to know whether the rest of the match failed) by a loop instead of
2286 /* [lwall] I've hoisted the register declarations to the outer block in order to
2287 * maybe save a little bit of pushing and popping on the stack. It also takes
2288 * advantage of machines that use a register save mask on subroutine entry.
2290 * This function used to be heavily recursive, but since this had the
2291 * effect of blowing the CPU stack on complex regexes, it has been
2292 * restructured to be iterative, and to save state onto the heap rather
2293 * than the stack. Essentially whereever regmatch() used to be called, it
2294 * pushes the current state, notes where to return, then jumps back into
2297 * Originally the structure of this function used to look something like
2302 while (scan != NULL) {
2303 a++; // do stuff with a and b
2309 if (regmatch(...)) // recurse
2319 * Now it looks something like this:
2327 regmatch_state *st = new();
2329 st->a++; // do stuff with a and b
2331 while (scan != NULL) {
2339 st->resume_state = resume_FOO;
2340 goto start_recurse; // recurse
2349 st = new(); push a new state
2350 st->a = 1; st->b = 2;
2357 switch (resume_state) {
2359 goto resume_point_FOO;
2366 * WARNING: this means that any line in this function that contains a
2367 * REGMATCH() or TRYPAREN() is actually simulating a recursive call to
2368 * regmatch() using gotos instead. Thus the values of any local variables
2369 * not saved in the regmatch_state structure will have been lost when
2370 * execution resumes on the next line .
2372 * States (ie the st pointer) are allocated in slabs of about 4K in size.
2373 * PL_regmatch_state always points to the currently active state, and
2374 * PL_regmatch_slab points to the slab currently containing PL_regmatch_state.
2375 * The first time regmatch is called, the first slab is allocated, and is
2376 * never freed until interpreter desctruction. When the slab is full,
2377 * a new one is allocated chained to the end. At exit from regmatch, slabs
2378 * allocated since entry are freed.
2381 /* *** every FOO_fail should = FOO+1 */
2382 #define TRIE_next (REGNODE_MAX+1)
2383 #define TRIE_next_fail (REGNODE_MAX+2)
2384 #define EVAL_AB (REGNODE_MAX+3)
2385 #define EVAL_AB_fail (REGNODE_MAX+4)
2386 #define resume_CURLYX (REGNODE_MAX+5)
2387 #define resume_WHILEM1 (REGNODE_MAX+6)
2388 #define resume_WHILEM2 (REGNODE_MAX+7)
2389 #define resume_WHILEM3 (REGNODE_MAX+8)
2390 #define resume_WHILEM4 (REGNODE_MAX+9)
2391 #define resume_WHILEM5 (REGNODE_MAX+10)
2392 #define resume_WHILEM6 (REGNODE_MAX+11)
2393 #define BRANCH_next (REGNODE_MAX+12)
2394 #define BRANCH_next_fail (REGNODE_MAX+13)
2395 #define CURLYM_A (REGNODE_MAX+14)
2396 #define CURLYM_A_fail (REGNODE_MAX+15)
2397 #define CURLYM_B (REGNODE_MAX+16)
2398 #define CURLYM_B_fail (REGNODE_MAX+17)
2399 #define IFMATCH_A (REGNODE_MAX+18)
2400 #define IFMATCH_A_fail (REGNODE_MAX+19)
2401 #define CURLY_B_min_known (REGNODE_MAX+20)
2402 #define CURLY_B_min_known_fail (REGNODE_MAX+21)
2403 #define CURLY_B_min (REGNODE_MAX+22)
2404 #define CURLY_B_min_fail (REGNODE_MAX+23)
2405 #define CURLY_B_max (REGNODE_MAX+24)
2406 #define CURLY_B_max_fail (REGNODE_MAX+25)
2408 #define DEBUG_STATE_pp(pp) \
2410 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2411 PerlIO_printf(Perl_debug_log, \
2414 state_names[st->resume_state-REGNODE_MAX-1] ) \
2418 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2421 static const char * const state_names[] = {
2441 "CURLY_B_min_known",
2442 "CURLY_B_min_known_fail",
2450 S_debug_start_match(pTHX_ const regexp *prog, const bool do_utf8,
2451 const char *start, const char *end, const char *blurb)
2453 const bool utf8_pat= prog->reganch & ROPT_UTF8 ? 1 : 0;
2457 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2458 prog->precomp, prog->prelen, 60);
2460 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2461 start, end - start, 60);
2463 PerlIO_printf(Perl_debug_log,
2464 "%s%s REx%s %s against %s\n",
2465 PL_colors[4], blurb, PL_colors[5], s0, s1);
2467 if (do_utf8||utf8_pat)
2468 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2469 utf8_pat ? "pattern" : "",
2470 utf8_pat && do_utf8 ? " and " : "",
2471 do_utf8 ? "string" : ""
2477 S_dump_exec_pos(pTHX_ const char *locinput,
2478 const regnode *scan,
2479 const char *loc_regeol,
2480 const char *loc_bostr,
2481 const char *loc_reg_starttry,
2484 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2485 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2486 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2487 /* The part of the string before starttry has one color
2488 (pref0_len chars), between starttry and current
2489 position another one (pref_len - pref0_len chars),
2490 after the current position the third one.
2491 We assume that pref0_len <= pref_len, otherwise we
2492 decrease pref0_len. */
2493 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2494 ? (5 + taill) - l : locinput - loc_bostr;
2497 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2499 pref0_len = pref_len - (locinput - loc_reg_starttry);
2500 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2501 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2502 ? (5 + taill) - pref_len : loc_regeol - locinput);
2503 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2507 if (pref0_len > pref_len)
2508 pref0_len = pref_len;
2510 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2512 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2513 (locinput - pref_len),pref0_len, 60, 4, 5);
2515 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2516 (locinput - pref_len + pref0_len),
2517 pref_len - pref0_len, 60, 2, 3);
2519 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2520 locinput, loc_regeol - locinput, 10, 0, 1);
2522 const STRLEN tlen=len0+len1+len2;
2523 PerlIO_printf(Perl_debug_log,
2524 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2525 (IV)(locinput - loc_bostr),
2528 (docolor ? "" : "> <"),
2530 tlen > 19 ? 0 : 19 - tlen,
2537 STATIC I32 /* 0 failure, 1 success */
2538 S_regmatch(pTHX_ const regmatch_info *reginfo, regnode *prog)
2540 #if PERL_VERSION < 9
2544 register const bool do_utf8 = PL_reg_match_utf8;
2545 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2547 regexp *rex = reginfo->prog;
2549 regmatch_slab *orig_slab;
2550 regmatch_state *orig_state;
2552 /* the current state. This is a cached copy of PL_regmatch_state */
2553 register regmatch_state *st;
2555 /* cache heavy used fields of st in registers */
2556 register regnode *scan;
2557 register regnode *next;
2558 register I32 n = 0; /* initialize to shut up compiler warning */
2559 register char *locinput = PL_reginput;
2561 /* these variables are NOT saved during a recusive RFEGMATCH: */
2562 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2563 bool result = 0; /* return value of S_regmatch */
2564 int depth = 0; /* depth of recursion */
2565 regmatch_state *yes_state = NULL; /* state to pop to on success of
2567 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2568 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2574 GET_RE_DEBUG_FLAGS_DECL;
2577 /* on first ever call to regmatch, allocate first slab */
2578 if (!PL_regmatch_slab) {
2579 Newx(PL_regmatch_slab, 1, regmatch_slab);
2580 PL_regmatch_slab->prev = NULL;
2581 PL_regmatch_slab->next = NULL;
2582 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2585 /* remember current high-water mark for exit */
2586 /* XXX this should be done with SAVE* instead */
2587 orig_slab = PL_regmatch_slab;
2588 orig_state = PL_regmatch_state;
2590 /* grab next free state slot */
2591 st = ++PL_regmatch_state;
2592 if (st > SLAB_LAST(PL_regmatch_slab))
2593 st = PL_regmatch_state = S_push_slab(aTHX);
2600 /* Note that nextchr is a byte even in UTF */
2601 nextchr = UCHARAT(locinput);
2603 while (scan != NULL) {
2606 SV * const prop = sv_newmortal();
2607 regnode *rnext=regnext(scan);
2608 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2609 regprop(rex, prop, scan);
2611 PerlIO_printf(Perl_debug_log,
2612 "%3"IVdf":%*s%s(%"IVdf")\n",
2613 (IV)(scan - rex->program), depth*2, "",
2615 (PL_regkind[OP(scan)] == END || !rnext) ?
2616 0 : (IV)(rnext - rex->program));
2619 next = scan + NEXT_OFF(scan);
2622 state_num = OP(scan);
2625 switch (state_num) {
2627 if (locinput == PL_bostr)
2629 /* reginfo->till = reginfo->bol; */
2634 if (locinput == PL_bostr ||
2635 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2641 if (locinput == PL_bostr)
2645 if (locinput == reginfo->ganch)
2651 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2656 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2658 if (PL_regeol - locinput > 1)
2662 if (PL_regeol != locinput)
2666 if (!nextchr && locinput >= PL_regeol)
2669 locinput += PL_utf8skip[nextchr];
2670 if (locinput > PL_regeol)
2672 nextchr = UCHARAT(locinput);
2675 nextchr = UCHARAT(++locinput);
2678 if (!nextchr && locinput >= PL_regeol)
2680 nextchr = UCHARAT(++locinput);
2683 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2686 locinput += PL_utf8skip[nextchr];
2687 if (locinput > PL_regeol)
2689 nextchr = UCHARAT(locinput);
2692 nextchr = UCHARAT(++locinput);
2696 #define ST st->u.trie
2698 /* In this case the charclass data is available inline so
2699 we can fail fast without a lot of extra overhead.
2701 if (scan->flags == EXACT || !do_utf8) {
2702 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
2704 PerlIO_printf(Perl_debug_log,
2705 "%*s %sfailed to match trie start class...%s\n",
2706 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2715 /* what type of TRIE am I? (utf8 makes this contextual) */
2716 const enum { trie_plain, trie_utf8, trie_utf8_fold }
2717 trie_type = do_utf8 ?
2718 (scan->flags == EXACT ? trie_utf8 : trie_utf8_fold)
2721 /* what trie are we using right now */
2722 reg_trie_data * const trie
2723 = (reg_trie_data*)rex->data->data[ ARG( scan ) ];
2724 U32 state = trie->startstate;
2726 if (trie->bitmap && trie_type != trie_utf8_fold &&
2727 !TRIE_BITMAP_TEST(trie,*locinput)
2729 if (trie->states[ state ].wordnum) {
2731 PerlIO_printf(Perl_debug_log,
2732 "%*s %smatched empty string...%s\n",
2733 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2738 PerlIO_printf(Perl_debug_log,
2739 "%*s %sfailed to match trie start class...%s\n",
2740 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2747 U8 *uc = ( U8* )locinput;
2751 U8 *uscan = (U8*)NULL;
2753 SV *sv_accept_buff = NULL;
2754 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2756 ST.accepted = 0; /* how many accepting states we have seen */
2758 ST.jump = trie->jump;
2767 traverse the TRIE keeping track of all accepting states
2768 we transition through until we get to a failing node.
2771 while ( state && uc <= (U8*)PL_regeol ) {
2772 U32 base = trie->states[ state ].trans.base;
2775 /* We use charid to hold the wordnum as we don't use it
2776 for charid until after we have done the wordnum logic.
2777 We define an alias just so that the wordnum logic reads
2780 #define got_wordnum charid
2781 got_wordnum = trie->states[ state ].wordnum;
2783 if ( got_wordnum ) {
2784 if ( ! ST.accepted ) {
2787 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
2788 sv_accept_buff=newSV(bufflen *
2789 sizeof(reg_trie_accepted) - 1);
2790 SvCUR_set(sv_accept_buff, 0);
2791 SvPOK_on(sv_accept_buff);
2792 sv_2mortal(sv_accept_buff);
2795 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
2798 if (ST.accepted >= bufflen) {
2800 ST.accept_buff =(reg_trie_accepted*)
2801 SvGROW(sv_accept_buff,
2802 bufflen * sizeof(reg_trie_accepted));
2804 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
2805 + sizeof(reg_trie_accepted));
2808 ST.accept_buff[ST.accepted].wordnum = got_wordnum;
2809 ST.accept_buff[ST.accepted].endpos = uc;
2811 } while (trie->nextword && (got_wordnum= trie->nextword[got_wordnum]));
2815 DEBUG_TRIE_EXECUTE_r({
2816 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
2817 PerlIO_printf( Perl_debug_log,
2818 "%*s %sState: %4"UVxf" Accepted: %4"UVxf" ",
2819 2+depth * 2, "", PL_colors[4],
2820 (UV)state, (UV)ST.accepted );
2824 REXEC_TRIE_READ_CHAR(trie_type, trie, uc, uscan, len,
2825 uvc, charid, foldlen, foldbuf, uniflags);
2828 (base + charid > trie->uniquecharcount )
2829 && (base + charid - 1 - trie->uniquecharcount
2831 && trie->trans[base + charid - 1 -
2832 trie->uniquecharcount].check == state)
2834 state = trie->trans[base + charid - 1 -
2835 trie->uniquecharcount ].next;
2846 DEBUG_TRIE_EXECUTE_r(
2847 PerlIO_printf( Perl_debug_log,
2848 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
2849 charid, uvc, (UV)state, PL_colors[5] );
2856 PerlIO_printf( Perl_debug_log,
2857 "%*s %sgot %"IVdf" possible matches%s\n",
2858 REPORT_CODE_OFF + depth * 2, "",
2859 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
2865 case TRIE_next_fail: /* we failed - try next alterative */
2867 if ( ST.accepted == 1 ) {
2868 /* only one choice left - just continue */
2870 reg_trie_data * const trie
2871 = (reg_trie_data*)rex->data->data[ ARG(ST.me) ];
2872 SV ** const tmp = RX_DEBUG(reginfo->prog)
2873 ? av_fetch( trie->words, ST.accept_buff[ 0 ].wordnum-1, 0 )
2875 PerlIO_printf( Perl_debug_log,
2876 "%*s %sonly one match left: #%d <%s>%s\n",
2877 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
2878 ST.accept_buff[ 0 ].wordnum,
2879 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",
2882 PL_reginput = (char *)ST.accept_buff[ 0 ].endpos;
2883 /* in this case we free tmps/leave before we call regmatch
2884 as we wont be using accept_buff again. */
2887 locinput = PL_reginput;
2888 nextchr = UCHARAT(locinput);
2893 scan = ST.B - ST.jump[ST.accept_buff[0].wordnum];
2895 continue; /* execute rest of RE */
2898 if (!ST.accepted-- ) {
2905 There are at least two accepting states left. Presumably
2906 the number of accepting states is going to be low,
2907 typically two. So we simply scan through to find the one
2908 with lowest wordnum. Once we find it, we swap the last
2909 state into its place and decrement the size. We then try to
2910 match the rest of the pattern at the point where the word
2911 ends. If we succeed, control just continues along the
2912 regex; if we fail we return here to try the next accepting
2919 for( cur = 1 ; cur <= ST.accepted ; cur++ ) {
2920 DEBUG_TRIE_EXECUTE_r(
2921 PerlIO_printf( Perl_debug_log,
2922 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
2923 REPORT_CODE_OFF + depth * 2, "", PL_colors[4],
2924 (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur,
2925 ST.accept_buff[ cur ].wordnum, PL_colors[5] );
2928 if (ST.accept_buff[cur].wordnum <
2929 ST.accept_buff[best].wordnum)
2934 reg_trie_data * const trie
2935 = (reg_trie_data*)rex->data->data[ ARG(ST.me) ];
2936 SV ** const tmp = RX_DEBUG(reginfo->prog)
2937 ? av_fetch( trie->words, ST.accept_buff[ best ].wordnum - 1, 0 )
2939 PerlIO_printf( Perl_debug_log, "%*s %strying alternation #%d <%s> at node #%d %s\n",
2940 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
2941 ST.accept_buff[best].wordnum,
2942 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr", REG_NODE_NUM(scan),
2946 if ( best<ST.accepted ) {
2947 reg_trie_accepted tmp = ST.accept_buff[ best ];
2948 ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ];
2949 ST.accept_buff[ ST.accepted ] = tmp;
2952 PL_reginput = (char *)ST.accept_buff[ best ].endpos;
2954 PUSH_STATE_GOTO(TRIE_next, ST.B);
2957 PUSH_STATE_GOTO(TRIE_next, ST.B - ST.jump[ST.accept_buff[best].wordnum]);
2967 char *s = STRING(scan);
2968 st->ln = STR_LEN(scan);
2969 if (do_utf8 != UTF) {
2970 /* The target and the pattern have differing utf8ness. */
2972 const char * const e = s + st->ln;
2975 /* The target is utf8, the pattern is not utf8. */
2980 if (NATIVE_TO_UNI(*(U8*)s) !=
2981 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
2989 /* The target is not utf8, the pattern is utf8. */
2994 if (NATIVE_TO_UNI(*((U8*)l)) !=
2995 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3003 nextchr = UCHARAT(locinput);
3006 /* The target and the pattern have the same utf8ness. */
3007 /* Inline the first character, for speed. */
3008 if (UCHARAT(s) != nextchr)
3010 if (PL_regeol - locinput < st->ln)
3012 if (st->ln > 1 && memNE(s, locinput, st->ln))
3015 nextchr = UCHARAT(locinput);
3019 PL_reg_flags |= RF_tainted;
3022 char * const s = STRING(scan);
3023 st->ln = STR_LEN(scan);
3025 if (do_utf8 || UTF) {
3026 /* Either target or the pattern are utf8. */
3027 const char * const l = locinput;
3028 char *e = PL_regeol;
3030 if (ibcmp_utf8(s, 0, st->ln, (bool)UTF,
3031 l, &e, 0, do_utf8)) {
3032 /* One more case for the sharp s:
3033 * pack("U0U*", 0xDF) =~ /ss/i,
3034 * the 0xC3 0x9F are the UTF-8
3035 * byte sequence for the U+00DF. */
3037 toLOWER(s[0]) == 's' &&
3039 toLOWER(s[1]) == 's' &&
3046 nextchr = UCHARAT(locinput);
3050 /* Neither the target and the pattern are utf8. */
3052 /* Inline the first character, for speed. */
3053 if (UCHARAT(s) != nextchr &&
3054 UCHARAT(s) != ((OP(scan) == EXACTF)
3055 ? PL_fold : PL_fold_locale)[nextchr])
3057 if (PL_regeol - locinput < st->ln)
3059 if (st->ln > 1 && (OP(scan) == EXACTF
3060 ? ibcmp(s, locinput, st->ln)
3061 : ibcmp_locale(s, locinput, st->ln)))
3064 nextchr = UCHARAT(locinput);
3069 STRLEN inclasslen = PL_regeol - locinput;
3071 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3073 if (locinput >= PL_regeol)
3075 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3076 nextchr = UCHARAT(locinput);
3081 nextchr = UCHARAT(locinput);
3082 if (!REGINCLASS(rex, scan, (U8*)locinput))
3084 if (!nextchr && locinput >= PL_regeol)
3086 nextchr = UCHARAT(++locinput);
3090 /* If we might have the case of the German sharp s
3091 * in a casefolding Unicode character class. */
3093 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3094 locinput += SHARP_S_SKIP;
3095 nextchr = UCHARAT(locinput);
3101 PL_reg_flags |= RF_tainted;
3107 LOAD_UTF8_CHARCLASS_ALNUM();
3108 if (!(OP(scan) == ALNUM
3109 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3110 : isALNUM_LC_utf8((U8*)locinput)))
3114 locinput += PL_utf8skip[nextchr];
3115 nextchr = UCHARAT(locinput);
3118 if (!(OP(scan) == ALNUM
3119 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
3121 nextchr = UCHARAT(++locinput);
3124 PL_reg_flags |= RF_tainted;
3127 if (!nextchr && locinput >= PL_regeol)
3130 LOAD_UTF8_CHARCLASS_ALNUM();
3131 if (OP(scan) == NALNUM
3132 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3133 : isALNUM_LC_utf8((U8*)locinput))
3137 locinput += PL_utf8skip[nextchr];
3138 nextchr = UCHARAT(locinput);
3141 if (OP(scan) == NALNUM
3142 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
3144 nextchr = UCHARAT(++locinput);
3148 PL_reg_flags |= RF_tainted;
3152 /* was last char in word? */
3154 if (locinput == PL_bostr)
3157 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3159 st->ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3161 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3162 st->ln = isALNUM_uni(st->ln);
3163 LOAD_UTF8_CHARCLASS_ALNUM();
3164 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3167 st->ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(st->ln));
3168 n = isALNUM_LC_utf8((U8*)locinput);
3172 st->ln = (locinput != PL_bostr) ?
3173 UCHARAT(locinput - 1) : '\n';
3174 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3175 st->ln = isALNUM(st->ln);
3176 n = isALNUM(nextchr);
3179 st->ln = isALNUM_LC(st->ln);
3180 n = isALNUM_LC(nextchr);
3183 if (((!st->ln) == (!n)) == (OP(scan) == BOUND ||
3184 OP(scan) == BOUNDL))
3188 PL_reg_flags |= RF_tainted;
3194 if (UTF8_IS_CONTINUED(nextchr)) {
3195 LOAD_UTF8_CHARCLASS_SPACE();
3196 if (!(OP(scan) == SPACE
3197 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3198 : isSPACE_LC_utf8((U8*)locinput)))
3202 locinput += PL_utf8skip[nextchr];
3203 nextchr = UCHARAT(locinput);
3206 if (!(OP(scan) == SPACE
3207 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3209 nextchr = UCHARAT(++locinput);
3212 if (!(OP(scan) == SPACE
3213 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3215 nextchr = UCHARAT(++locinput);
3219 PL_reg_flags |= RF_tainted;
3222 if (!nextchr && locinput >= PL_regeol)
3225 LOAD_UTF8_CHARCLASS_SPACE();
3226 if (OP(scan) == NSPACE
3227 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3228 : isSPACE_LC_utf8((U8*)locinput))
3232 locinput += PL_utf8skip[nextchr];
3233 nextchr = UCHARAT(locinput);
3236 if (OP(scan) == NSPACE
3237 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
3239 nextchr = UCHARAT(++locinput);
3242 PL_reg_flags |= RF_tainted;
3248 LOAD_UTF8_CHARCLASS_DIGIT();
3249 if (!(OP(scan) == DIGIT
3250 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3251 : isDIGIT_LC_utf8((U8*)locinput)))
3255 locinput += PL_utf8skip[nextchr];
3256 nextchr = UCHARAT(locinput);
3259 if (!(OP(scan) == DIGIT
3260 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
3262 nextchr = UCHARAT(++locinput);
3265 PL_reg_flags |= RF_tainted;
3268 if (!nextchr && locinput >= PL_regeol)
3271 LOAD_UTF8_CHARCLASS_DIGIT();
3272 if (OP(scan) == NDIGIT
3273 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3274 : isDIGIT_LC_utf8((U8*)locinput))
3278 locinput += PL_utf8skip[nextchr];
3279 nextchr = UCHARAT(locinput);
3282 if (OP(scan) == NDIGIT
3283 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
3285 nextchr = UCHARAT(++locinput);
3288 if (locinput >= PL_regeol)
3291 LOAD_UTF8_CHARCLASS_MARK();
3292 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3294 locinput += PL_utf8skip[nextchr];
3295 while (locinput < PL_regeol &&
3296 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3297 locinput += UTF8SKIP(locinput);
3298 if (locinput > PL_regeol)
3303 nextchr = UCHARAT(locinput);
3306 PL_reg_flags |= RF_tainted;
3311 n = ARG(scan); /* which paren pair */
3312 st->ln = PL_regstartp[n];
3313 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3314 if ((I32)*PL_reglastparen < n || st->ln == -1)
3315 sayNO; /* Do not match unless seen CLOSEn. */
3316 if (st->ln == PL_regendp[n])
3319 s = PL_bostr + st->ln;
3320 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
3322 const char *e = PL_bostr + PL_regendp[n];
3324 * Note that we can't do the "other character" lookup trick as
3325 * in the 8-bit case (no pun intended) because in Unicode we
3326 * have to map both upper and title case to lower case.
3328 if (OP(scan) == REFF) {
3330 STRLEN ulen1, ulen2;
3331 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3332 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3336 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3337 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3338 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3345 nextchr = UCHARAT(locinput);
3349 /* Inline the first character, for speed. */
3350 if (UCHARAT(s) != nextchr &&
3352 (UCHARAT(s) != ((OP(scan) == REFF
3353 ? PL_fold : PL_fold_locale)[nextchr]))))
3355 st->ln = PL_regendp[n] - st->ln;
3356 if (locinput + st->ln > PL_regeol)
3358 if (st->ln > 1 && (OP(scan) == REF
3359 ? memNE(s, locinput, st->ln)
3361 ? ibcmp(s, locinput, st->ln)
3362 : ibcmp_locale(s, locinput, st->ln))))
3365 nextchr = UCHARAT(locinput);
3376 #define ST st->u.eval
3378 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3382 /* execute the code in the {...} */
3384 SV ** const before = SP;
3385 OP_4tree * const oop = PL_op;
3386 COP * const ocurcop = PL_curcop;
3390 PL_op = (OP_4tree*)rex->data->data[n];
3391 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3392 PAD_SAVE_LOCAL(old_comppad, (PAD*)rex->data->data[n + 2]);
3393 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3395 CALLRUNOPS(aTHX); /* Scalar context. */
3398 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3405 PAD_RESTORE_LOCAL(old_comppad);
3406 PL_curcop = ocurcop;
3409 sv_setsv(save_scalar(PL_replgv), ret);
3413 if (st->logical == 2) { /* Postponed subexpression: /(??{...})/ */
3416 /* extract RE object from returned value; compiling if
3421 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
3422 mg = mg_find(sv, PERL_MAGIC_qr);
3423 else if (SvSMAGICAL(ret)) {
3424 if (SvGMAGICAL(ret))
3425 sv_unmagic(ret, PERL_MAGIC_qr);
3427 mg = mg_find(ret, PERL_MAGIC_qr);
3431 re = (regexp *)mg->mg_obj;
3432 (void)ReREFCNT_inc(re);
3436 const char * const t = SvPV_const(ret, len);
3438 const I32 osize = PL_regsize;
3441 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
3442 re = CALLREGCOMP(aTHX_ (char*)t, (char*)t + len, &pm);
3444 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3446 sv_magic(ret,(SV*)ReREFCNT_inc(re),
3452 /* run the pattern returned from (??{...}) */
3453 ST.cp = regcppush(0); /* Save *all* the positions. */
3454 REGCP_SET(ST.lastcp);
3455 *PL_reglastparen = 0;
3456 *PL_reglastcloseparen = 0;
3457 PL_reginput = locinput;
3459 /* XXXX This is too dramatic a measure... */
3463 ST.toggle_reg_flags = PL_reg_flags;
3464 if (re->reganch & ROPT_UTF8)
3465 PL_reg_flags |= RF_utf8;
3467 PL_reg_flags &= ~RF_utf8;
3468 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
3471 ST.prev_curlyx = cur_curlyx;
3475 ST.prev_eval = cur_eval;
3479 debug_start_match(re, do_utf8, locinput, PL_regeol,
3480 "Matching embedded");
3482 /* now continue from first node in postoned RE */
3483 PUSH_YES_STATE_GOTO(EVAL_AB, re->program + 1);
3486 /* /(?(?{...})X|Y)/ */
3487 st->sw = SvTRUE(ret);
3492 case EVAL_AB: /* cleanup after a successful (??{A})B */
3493 /* note: this is called twice; first after popping B, then A */
3494 PL_reg_flags ^= ST.toggle_reg_flags;
3498 cur_eval = ST.prev_eval;
3499 cur_curlyx = ST.prev_curlyx;
3500 /* XXXX This is too dramatic a measure... */
3505 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
3506 /* note: this is called twice; first after popping B, then A */
3507 PL_reg_flags ^= ST.toggle_reg_flags;
3510 PL_reginput = locinput;
3511 REGCP_UNWIND(ST.lastcp);
3513 cur_eval = ST.prev_eval;
3514 cur_curlyx = ST.prev_curlyx;
3515 /* XXXX This is too dramatic a measure... */
3522 n = ARG(scan); /* which paren pair */
3523 PL_reg_start_tmp[n] = locinput;
3528 n = ARG(scan); /* which paren pair */
3529 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3530 PL_regendp[n] = locinput - PL_bostr;
3531 if (n > (I32)*PL_reglastparen)
3532 *PL_reglastparen = n;
3533 *PL_reglastcloseparen = n;
3536 n = ARG(scan); /* which paren pair */
3537 st->sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
3540 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3542 next = NEXTOPER(NEXTOPER(scan));
3544 next = scan + ARG(scan);
3545 if (OP(next) == IFTHEN) /* Fake one. */
3546 next = NEXTOPER(NEXTOPER(next));
3550 st->logical = scan->flags;
3552 /*******************************************************************
3553 cur_curlyx points to the regmatch_state associated with the most recent CURLYX.
3554 This struct contains info about the innermost (...)* loop (an
3555 "infoblock"), and a pointer to the next outer cur_curlyx.
3557 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3559 1) After matching Y, regnode for CURLYX is processed;
3561 2) This regnode populates cur_curlyx, and calls regmatch() recursively
3562 with the starting point at WHILEM node;
3564 3) Each hit of WHILEM node tries to match A and Z (in the order
3565 depending on the current iteration, min/max of {min,max} and
3566 greediness). The information about where are nodes for "A"
3567 and "Z" is read from cur_curlyx, as is info on how many times "A"
3568 was already matched, and greediness.
3570 4) After A matches, the same WHILEM node is hit again.
3572 5) Each time WHILEM is hit, cur_curlyx is the infoblock created by CURLYX
3573 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3574 resets cur_curlyx, since this Y(A)*Z can be a part of some other loop:
3575 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3576 of the external loop.
3578 Currently present infoblocks form a tree with a stem formed by cur_curlyx
3579 and whatever it mentions via ->next, and additional attached trees
3580 corresponding to temporarily unset infoblocks as in "5" above.
3582 In the following picture, infoblocks for outer loop of
3583 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3584 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3585 infoblocks are drawn below the "reset" infoblock.
3587 In fact in the picture below we do not show failed matches for Z and T
3588 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3589 more obvious *why* one needs to *temporary* unset infoblocks.]
3591 Matched REx position InfoBlocks Comment
3595 Y A)*?Z)*?T x <- O <- I
3596 YA )*?Z)*?T x <- O <- I
3597 YA A)*?Z)*?T x <- O <- I
3598 YAA )*?Z)*?T x <- O <- I
3599 YAA Z)*?T x <- O # Temporary unset I
3602 YAAZ Y(A)*?Z)*?T x <- O
3605 YAAZY (A)*?Z)*?T x <- O
3608 YAAZY A)*?Z)*?T x <- O <- I
3611 YAAZYA )*?Z)*?T x <- O <- I
3614 YAAZYA Z)*?T x <- O # Temporary unset I
3620 YAAZYAZ T x # Temporary unset O
3627 *******************************************************************/
3630 /* No need to save/restore up to this paren */
3631 parenfloor = scan->flags;
3635 CURLYX and WHILEM are always paired: they're the moral
3636 equivalent of pp_enteriter anbd pp_iter.
3638 The only time next could be null is if the node tree is
3639 corrupt. This was mentioned on p5p a few days ago.
3641 See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-04/msg00556.html
3642 So we'll assert that this is true:
3645 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3647 /* XXXX Probably it is better to teach regpush to support
3648 parenfloor > PL_regsize... */
3649 if (parenfloor > (I32)*PL_reglastparen)
3650 parenfloor = *PL_reglastparen; /* Pessimization... */
3652 st->u.curlyx.cp = PL_savestack_ix;
3653 st->u.curlyx.outercc = cur_curlyx;
3655 /* these fields contain the state of the current curly.
3656 * they are accessed by subsequent WHILEMs;
3657 * cur and lastloc are also updated by WHILEM */
3658 st->u.curlyx.parenfloor = parenfloor;
3659 st->u.curlyx.cur = -1; /* this will be updated by WHILEM */
3660 st->u.curlyx.min = ARG1(scan);
3661 st->u.curlyx.max = ARG2(scan);
3662 st->u.curlyx.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3663 st->u.curlyx.lastloc = 0;
3664 /* st->next and st->minmod are also read by WHILEM */
3666 PL_reginput = locinput;
3667 REGMATCH(PREVOPER(next), CURLYX); /* start on the WHILEM */
3668 /*** all unsaved local vars undefined at this point */
3669 regcpblow(st->u.curlyx.cp);
3670 cur_curlyx = st->u.curlyx.outercc;
3676 * This is really hard to understand, because after we match
3677 * what we're trying to match, we must make sure the rest of
3678 * the REx is going to match for sure, and to do that we have
3679 * to go back UP the parse tree by recursing ever deeper. And
3680 * if it fails, we have to reset our parent's current state
3681 * that we can try again after backing off.
3686 cur_curlyx gets initialised by CURLYX ready for use by WHILEM.
3687 So again, unless somethings been corrupted, cur_curlyx cannot
3688 be null at that point in WHILEM.
3690 See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-04/msg00556.html
3691 So we'll assert that this is true:
3694 st->u.whilem.lastloc = cur_curlyx->u.curlyx.lastloc; /* Detection of 0-len. */
3695 st->u.whilem.cache_offset = 0;
3696 st->u.whilem.cache_bit = 0;
3698 n = cur_curlyx->u.curlyx.cur + 1; /* how many we know we matched */
3699 PL_reginput = locinput;
3702 PerlIO_printf(Perl_debug_log,
3703 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3704 REPORT_CODE_OFF+depth*2, "",
3705 (long)n, (long)cur_curlyx->u.curlyx.min,
3706 (long)cur_curlyx->u.curlyx.max,
3710 /* If degenerate scan matches "", assume scan done. */
3712 if (locinput == cur_curlyx->u.curlyx.lastloc && n >=
3713 cur_curlyx->u.curlyx.min)
3715 st->u.whilem.savecc = cur_curlyx;
3716 cur_curlyx = cur_curlyx->u.curlyx.outercc;
3718 st->ln = cur_curlyx->u.curlyx.cur;
3720 PerlIO_printf(Perl_debug_log,
3721 "%*s empty match detected, try continuation...\n",
3722 REPORT_CODE_OFF+depth*2, "")
3724 REGMATCH(st->u.whilem.savecc->next, WHILEM1);
3725 /*** all unsaved local vars undefined at this point */
3726 cur_curlyx = st->u.whilem.savecc;
3729 if (cur_curlyx->u.curlyx.outercc)
3730 cur_curlyx->u.curlyx.outercc->u.curlyx.cur = st->ln;
3734 /* First just match a string of min scans. */
3736 if (n < cur_curlyx->u.curlyx.min) {
3737 cur_curlyx->u.curlyx.cur = n;
3738 cur_curlyx->u.curlyx.lastloc = locinput;
3739 REGMATCH(cur_curlyx->u.curlyx.scan, WHILEM2);
3740 /*** all unsaved local vars undefined at this point */
3743 cur_curlyx->u.curlyx.cur = n - 1;
3744 cur_curlyx->u.curlyx.lastloc = st->u.whilem.lastloc;
3749 /* Check whether we already were at this position.
3750 Postpone detection until we know the match is not
3751 *that* much linear. */
3752 if (!PL_reg_maxiter) {
3753 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3754 /* possible overflow for long strings and many CURLYX's */
3755 if (PL_reg_maxiter < 0)
3756 PL_reg_maxiter = I32_MAX;
3757 PL_reg_leftiter = PL_reg_maxiter;
3759 if (PL_reg_leftiter-- == 0) {
3760 const I32 size = (PL_reg_maxiter + 7)/8;
3761 if (PL_reg_poscache) {
3762 if ((I32)PL_reg_poscache_size < size) {
3763 Renew(PL_reg_poscache, size, char);
3764 PL_reg_poscache_size = size;
3766 Zero(PL_reg_poscache, size, char);
3769 PL_reg_poscache_size = size;
3770 Newxz(PL_reg_poscache, size, char);
3773 PerlIO_printf(Perl_debug_log,
3774 "%sDetected a super-linear match, switching on caching%s...\n",
3775 PL_colors[4], PL_colors[5])
3778 if (PL_reg_leftiter < 0) {
3779 st->u.whilem.cache_offset = locinput - PL_bostr;
3781 st->u.whilem.cache_offset = (scan->flags & 0xf) - 1
3782 + st->u.whilem.cache_offset * (scan->flags>>4);
3783 st->u.whilem.cache_bit = st->u.whilem.cache_offset % 8;
3784 st->u.whilem.cache_offset /= 8;
3785 if (PL_reg_poscache[st->u.whilem.cache_offset] & (1<<st->u.whilem.cache_bit)) {
3787 PerlIO_printf(Perl_debug_log,
3788 "%*s already tried at this position...\n",
3789 REPORT_CODE_OFF+depth*2, "")
3791 sayNO; /* cache records failure */
3796 /* Prefer next over scan for minimal matching. */
3798 if (cur_curlyx->minmod) {
3799 st->u.whilem.savecc = cur_curlyx;
3800 cur_curlyx = cur_curlyx->u.curlyx.outercc;
3802 st->ln = cur_curlyx->u.curlyx.cur;
3803 st->u.whilem.cp = regcppush(st->u.whilem.savecc->u.curlyx.parenfloor);
3804 REGCP_SET(st->u.whilem.lastcp);
3805 REGMATCH(st->u.whilem.savecc->next, WHILEM3);
3806 /*** all unsaved local vars undefined at this point */
3807 cur_curlyx = st->u.whilem.savecc;
3809 regcpblow(st->u.whilem.cp);
3810 sayYES; /* All done. */
3812 REGCP_UNWIND(st->u.whilem.lastcp);
3814 if (cur_curlyx->u.curlyx.outercc)
3815 cur_curlyx->u.curlyx.outercc->u.curlyx.cur = st->ln;
3817 if (n >= cur_curlyx->u.curlyx.max) { /* Maximum greed exceeded? */
3818 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3819 && !(PL_reg_flags & RF_warned)) {
3820 PL_reg_flags |= RF_warned;
3821 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3822 "Complex regular subexpression recursion",
3829 PerlIO_printf(Perl_debug_log,
3830 "%*s trying longer...\n",
3831 REPORT_CODE_OFF+depth*2, "")
3833 /* Try scanning more and see if it helps. */
3834 PL_reginput = locinput;
3835 cur_curlyx->u.curlyx.cur = n;
3836 cur_curlyx->u.curlyx.lastloc = locinput;
3837 st->u.whilem.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
3838 REGCP_SET(st->u.whilem.lastcp);
3839 REGMATCH(cur_curlyx->u.curlyx.scan, WHILEM4);
3840 /*** all unsaved local vars undefined at this point */
3842 regcpblow(st->u.whilem.cp);
3845 REGCP_UNWIND(st->u.whilem.lastcp);
3847 cur_curlyx->u.curlyx.cur = n - 1;
3848 cur_curlyx->u.curlyx.lastloc = st->u.whilem.lastloc;
3852 /* Prefer scan over next for maximal matching. */
3854 if (n < cur_curlyx->u.curlyx.max) { /* More greed allowed? */
3855 st->u.whilem.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
3856 cur_curlyx->u.curlyx.cur = n;
3857 cur_curlyx->u.curlyx.lastloc = locinput;
3858 REGCP_SET(st->u.whilem.lastcp);
3859 REGMATCH(cur_curlyx->u.curlyx.scan, WHILEM5);
3860 /*** all unsaved local vars undefined at this point */
3862 regcpblow(st->u.whilem.cp);
3865 REGCP_UNWIND(st->u.whilem.lastcp);
3866 regcppop(rex); /* Restore some previous $<digit>s? */
3867 PL_reginput = locinput;
3869 PerlIO_printf(Perl_debug_log,
3870 "%*s failed, try continuation...\n",
3871 REPORT_CODE_OFF+depth*2, "")
3874 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3875 && !(PL_reg_flags & RF_warned)) {
3876 PL_reg_flags |= RF_warned;
3877 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3878 "Complex regular subexpression recursion",
3882 /* Failed deeper matches of scan, so see if this one works. */
3883 st->u.whilem.savecc = cur_curlyx;
3884 cur_curlyx = cur_curlyx->u.curlyx.outercc;
3886 st->ln = cur_curlyx->u.curlyx.cur;
3887 REGMATCH(st->u.whilem.savecc->next, WHILEM6);
3888 /*** all unsaved local vars undefined at this point */
3889 cur_curlyx = st->u.whilem.savecc;
3892 if (cur_curlyx->u.curlyx.outercc)
3893 cur_curlyx->u.curlyx.outercc->u.curlyx.cur = st->ln;
3894 cur_curlyx->u.curlyx.cur = n - 1;
3895 cur_curlyx->u.curlyx.lastloc = st->u.whilem.lastloc;
3901 #define ST st->u.branch
3903 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
3904 next = scan + ARG(scan);
3907 scan = NEXTOPER(scan);
3910 case BRANCH: /* /(...|A|...)/ */
3911 scan = NEXTOPER(scan); /* scan now points to inner node */
3912 if (!next || (OP(next) != BRANCH && OP(next) != BRANCHJ))
3913 /* last branch; skip state push and jump direct to node */