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.
19 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
20 * confused with the original package (see point 3 below). Thanks, Henry!
23 /* Additional note: this code is very heavily munged from Henry's version
24 * in places. In some spots I've traded clarity for efficiency, so don't
25 * blame Henry for some of the lack of readability.
28 /* The names of the functions have been changed from regcomp and
29 * regexec to pregcomp and pregexec in order to avoid conflicts
30 * with the POSIX routines of the same names.
33 #ifdef PERL_EXT_RE_BUILD
34 /* need to replace pregcomp et al, so enable that */
35 # ifndef PERL_IN_XSUB_RE
36 # define PERL_IN_XSUB_RE
38 /* need access to debugger hooks */
39 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
44 #ifdef PERL_IN_XSUB_RE
45 /* We *really* need to overwrite these symbols: */
46 # define Perl_regexec_flags my_regexec
47 # define Perl_regdump my_regdump
48 # define Perl_regprop my_regprop
49 # define Perl_re_intuit_start my_re_intuit_start
50 /* *These* symbols are masked to allow static link. */
51 # define Perl_pregexec my_pregexec
52 # define Perl_reginitcolors my_reginitcolors
53 # define Perl_regclass_swash my_regclass_swash
55 # define PERL_NO_GET_CONTEXT
59 * pregcomp and pregexec -- regsub and regerror are not used in perl
61 * Copyright (c) 1986 by University of Toronto.
62 * Written by Henry Spencer. Not derived from licensed software.
64 * Permission is granted to anyone to use this software for any
65 * purpose on any computer system, and to redistribute it freely,
66 * subject to the following restrictions:
68 * 1. The author is not responsible for the consequences of use of
69 * this software, no matter how awful, even if they arise
72 * 2. The origin of this software must not be misrepresented, either
73 * by explicit claim or by omission.
75 * 3. Altered versions must be plainly marked as such, and must not
76 * be misrepresented as being the original software.
78 **** Alterations to Henry's code are...
80 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
81 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
83 **** You may distribute under the terms of either the GNU General Public
84 **** License or the Artistic License, as specified in the README file.
86 * Beware that some of this code is subtly aware of the way operator
87 * precedence is structured in regular expressions. Serious changes in
88 * regular-expression syntax might require a total rethink.
91 #define PERL_IN_REGEXEC_C
96 #define RF_tainted 1 /* tainted information used? */
97 #define RF_warned 2 /* warned about big count? */
98 #define RF_evaled 4 /* Did an EVAL with setting? */
99 #define RF_utf8 8 /* String contains multibyte chars? */
101 #define UTF ((PL_reg_flags & RF_utf8) != 0)
103 #define RS_init 1 /* eval environment created */
104 #define RS_set 2 /* replsv value is set */
107 #define STATIC static
110 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
116 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
117 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
119 #define HOPc(pos,off) ((char *)(PL_reg_match_utf8 \
120 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
122 #define HOPBACKc(pos, off) ((char*) \
123 ((PL_reg_match_utf8) \
124 ? reghopmaybe3((U8*)pos, -off, ((U8*)(off < 0 ? PL_regeol : PL_bostr))) \
125 : (pos - off >= PL_bostr) \
130 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
131 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
132 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
134 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
135 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
136 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
137 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
138 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
139 #define LOAD_UTF8_CHARCLASS_MARK() LOAD_UTF8_CHARCLASS(mark, "\xcd\x86")
141 /* for use after a quantifier and before an EXACT-like node -- japhy */
142 #define JUMPABLE(rn) ( \
143 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
144 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
145 OP(rn) == PLUS || OP(rn) == MINMOD || \
146 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
149 #define HAS_TEXT(rn) ( \
150 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
154 Search for mandatory following text node; for lookahead, the text must
155 follow but for lookbehind (rn->flags != 0) we skip to the next step.
157 #define FIND_NEXT_IMPT(rn) STMT_START { \
158 while (JUMPABLE(rn)) \
159 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
160 rn = NEXTOPER(NEXTOPER(rn)); \
161 else if (OP(rn) == PLUS) \
163 else if (OP(rn) == IFMATCH) \
164 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
165 else rn += NEXT_OFF(rn); \
168 static void restore_pos(pTHX_ void *arg);
171 S_regcppush(pTHX_ I32 parenfloor)
174 const int retval = PL_savestack_ix;
175 #define REGCP_PAREN_ELEMS 4
176 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
179 if (paren_elems_to_push < 0)
180 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
182 #define REGCP_OTHER_ELEMS 6
183 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
184 for (p = PL_regsize; p > parenfloor; p--) {
185 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
186 SSPUSHINT(PL_regendp[p]);
187 SSPUSHINT(PL_regstartp[p]);
188 SSPUSHPTR(PL_reg_start_tmp[p]);
191 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
192 SSPUSHINT(PL_regsize);
193 SSPUSHINT(*PL_reglastparen);
194 SSPUSHINT(*PL_reglastcloseparen);
195 SSPUSHPTR(PL_reginput);
196 #define REGCP_FRAME_ELEMS 2
197 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
198 * are needed for the regexp context stack bookkeeping. */
199 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
200 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
205 /* These are needed since we do not localize EVAL nodes: */
206 # define REGCP_SET(cp) DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, \
207 " Setting an EVAL scope, savestack=%"IVdf"\n", \
208 (IV)PL_savestack_ix)); cp = PL_savestack_ix
210 # define REGCP_UNWIND(cp) DEBUG_EXECUTE_r(cp != PL_savestack_ix ? \
211 PerlIO_printf(Perl_debug_log, \
212 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
213 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
216 S_regcppop(pTHX_ const regexp *rex)
222 GET_RE_DEBUG_FLAGS_DECL;
224 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
226 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
227 i = SSPOPINT; /* Parentheses elements to pop. */
228 input = (char *) SSPOPPTR;
229 *PL_reglastcloseparen = SSPOPINT;
230 *PL_reglastparen = SSPOPINT;
231 PL_regsize = SSPOPINT;
233 /* Now restore the parentheses context. */
234 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
235 i > 0; i -= REGCP_PAREN_ELEMS) {
237 U32 paren = (U32)SSPOPINT;
238 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
239 PL_regstartp[paren] = SSPOPINT;
241 if (paren <= *PL_reglastparen)
242 PL_regendp[paren] = tmps;
244 PerlIO_printf(Perl_debug_log,
245 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
246 (UV)paren, (IV)PL_regstartp[paren],
247 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
248 (IV)PL_regendp[paren],
249 (paren > *PL_reglastparen ? "(no)" : ""));
253 if ((I32)(*PL_reglastparen + 1) <= rex->nparens) {
254 PerlIO_printf(Perl_debug_log,
255 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
256 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
260 /* It would seem that the similar code in regtry()
261 * already takes care of this, and in fact it is in
262 * a better location to since this code can #if 0-ed out
263 * but the code in regtry() is needed or otherwise tests
264 * requiring null fields (pat.t#187 and split.t#{13,14}
265 * (as of patchlevel 7877) will fail. Then again,
266 * this code seems to be necessary or otherwise
267 * building DynaLoader will fail:
268 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
270 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
272 PL_regstartp[i] = -1;
279 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
281 #define TRYPAREN(paren, n, input, where) { \
284 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
285 PL_regendp[paren] = input - PL_bostr; \
288 PL_regendp[paren] = -1; \
290 REGMATCH(next, where); \
294 PL_regendp[paren] = -1; \
299 * pregexec and friends
303 - pregexec - match a regexp against a string
306 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
307 char *strbeg, I32 minend, SV *screamer, U32 nosave)
308 /* strend: pointer to null at end of string */
309 /* strbeg: real beginning of string */
310 /* minend: end of match must be >=minend after stringarg. */
311 /* nosave: For optimizations. */
314 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
315 nosave ? 0 : REXEC_COPY_STR);
320 * Need to implement the following flags for reg_anch:
322 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
324 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
325 * INTUIT_AUTORITATIVE_ML
326 * INTUIT_ONCE_NOML - Intuit can match in one location only.
329 * Another flag for this function: SECOND_TIME (so that float substrs
330 * with giant delta may be not rechecked).
333 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
335 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
336 Otherwise, only SvCUR(sv) is used to get strbeg. */
338 /* XXXX We assume that strpos is strbeg unless sv. */
340 /* XXXX Some places assume that there is a fixed substring.
341 An update may be needed if optimizer marks as "INTUITable"
342 RExen without fixed substrings. Similarly, it is assumed that
343 lengths of all the strings are no more than minlen, thus they
344 cannot come from lookahead.
345 (Or minlen should take into account lookahead.) */
347 /* A failure to find a constant substring means that there is no need to make
348 an expensive call to REx engine, thus we celebrate a failure. Similarly,
349 finding a substring too deep into the string means that less calls to
350 regtry() should be needed.
352 REx compiler's optimizer found 4 possible hints:
353 a) Anchored substring;
355 c) Whether we are anchored (beginning-of-line or \G);
356 d) First node (of those at offset 0) which may distingush positions;
357 We use a)b)d) and multiline-part of c), and try to find a position in the
358 string which does not contradict any of them.
361 /* Most of decisions we do here should have been done at compile time.
362 The nodes of the REx which we used for the search should have been
363 deleted from the finite automaton. */
366 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
367 char *strend, U32 flags, re_scream_pos_data *data)
370 register I32 start_shift = 0;
371 /* Should be nonnegative! */
372 register I32 end_shift = 0;
377 const int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
379 register char *other_last = NULL; /* other substr checked before this */
380 char *check_at = NULL; /* check substr found at this pos */
381 const I32 multiline = prog->reganch & PMf_MULTILINE;
383 const char * const i_strpos = strpos;
384 SV * const dsv = PERL_DEBUG_PAD_ZERO(0);
387 GET_RE_DEBUG_FLAGS_DECL;
389 RX_MATCH_UTF8_set(prog,do_utf8);
391 if (prog->reganch & ROPT_UTF8) {
392 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
393 "UTF-8 regex...\n"));
394 PL_reg_flags |= RF_utf8;
398 const char *s = PL_reg_match_utf8 ?
399 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
401 const int len = PL_reg_match_utf8 ?
402 strlen(s) : strend - strpos;
405 if (PL_reg_match_utf8)
406 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
407 "UTF-8 target...\n"));
408 PerlIO_printf(Perl_debug_log,
409 "%sGuessing start of match, REx%s \"%s%.60s%s%s\" against \"%s%.*s%s%s\"...\n",
410 PL_colors[4], PL_colors[5], PL_colors[0],
413 (strlen(prog->precomp) > 60 ? "..." : ""),
415 (int)(len > 60 ? 60 : len),
417 (len > 60 ? "..." : "")
421 /* CHR_DIST() would be more correct here but it makes things slow. */
422 if (prog->minlen > strend - strpos) {
423 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
424 "String too short... [re_intuit_start]\n"));
427 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
430 if (!prog->check_utf8 && prog->check_substr)
431 to_utf8_substr(prog);
432 check = prog->check_utf8;
434 if (!prog->check_substr && prog->check_utf8)
435 to_byte_substr(prog);
436 check = prog->check_substr;
438 if (check == &PL_sv_undef) {
439 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
440 "Non-utf string cannot match utf check string\n"));
443 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
444 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
445 || ( (prog->reganch & ROPT_ANCH_BOL)
446 && !multiline ) ); /* Check after \n? */
449 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
450 | ROPT_IMPLICIT)) /* not a real BOL */
451 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
453 && (strpos != strbeg)) {
454 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
457 if (prog->check_offset_min == prog->check_offset_max &&
458 !(prog->reganch & ROPT_CANY_SEEN)) {
459 /* Substring at constant offset from beg-of-str... */
462 s = HOP3c(strpos, prog->check_offset_min, strend);
464 slen = SvCUR(check); /* >= 1 */
466 if ( strend - s > slen || strend - s < slen - 1
467 || (strend - s == slen && strend[-1] != '\n')) {
468 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
471 /* Now should match s[0..slen-2] */
473 if (slen && (*SvPVX_const(check) != *s
475 && memNE(SvPVX_const(check), s, slen)))) {
477 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
481 else if (*SvPVX_const(check) != *s
482 || ((slen = SvCUR(check)) > 1
483 && memNE(SvPVX_const(check), s, slen)))
486 goto success_at_start;
489 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
491 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
492 end_shift = prog->minlen - start_shift -
493 CHR_SVLEN(check) + (SvTAIL(check) != 0);
495 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
496 - (SvTAIL(check) != 0);
497 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
499 if (end_shift < eshift)
503 else { /* Can match at random position */
506 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
507 /* Should be nonnegative! */
508 end_shift = prog->minlen - start_shift -
509 CHR_SVLEN(check) + (SvTAIL(check) != 0);
512 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
514 Perl_croak(aTHX_ "panic: end_shift");
518 /* Find a possible match in the region s..strend by looking for
519 the "check" substring in the region corrected by start/end_shift. */
520 if (flags & REXEC_SCREAM) {
521 I32 p = -1; /* Internal iterator of scream. */
522 I32 * const pp = data ? data->scream_pos : &p;
524 if (PL_screamfirst[BmRARE(check)] >= 0
525 || ( BmRARE(check) == '\n'
526 && (BmPREVIOUS(check) == SvCUR(check) - 1)
528 s = screaminstr(sv, check,
529 start_shift + (s - strbeg), end_shift, pp, 0);
532 /* we may be pointing at the wrong string */
533 if (s && RX_MATCH_COPIED(prog))
534 s = strbeg + (s - SvPVX_const(sv));
536 *data->scream_olds = s;
538 else if (prog->reganch & ROPT_CANY_SEEN)
539 s = fbm_instr((U8*)(s + start_shift),
540 (U8*)(strend - end_shift),
541 check, multiline ? FBMrf_MULTILINE : 0);
543 s = fbm_instr(HOP3(s, start_shift, strend),
544 HOP3(strend, -end_shift, strbeg),
545 check, multiline ? FBMrf_MULTILINE : 0);
547 /* Update the count-of-usability, remove useless subpatterns,
550 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s %s substr \"%s%.*s%s\"%s%s",
551 (s ? "Found" : "Did not find"),
552 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
554 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
556 PL_colors[1], (SvTAIL(check) ? "$" : ""),
557 (s ? " at offset " : "...\n") ) );
564 /* Finish the diagnostic message */
565 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
567 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
568 Start with the other substr.
569 XXXX no SCREAM optimization yet - and a very coarse implementation
570 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
571 *always* match. Probably should be marked during compile...
572 Probably it is right to do no SCREAM here...
575 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
576 /* Take into account the "other" substring. */
577 /* XXXX May be hopelessly wrong for UTF... */
580 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
583 char * const last = HOP3c(s, -start_shift, strbeg);
588 t = s - prog->check_offset_max;
589 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
591 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
596 t = HOP3c(t, prog->anchored_offset, strend);
597 if (t < other_last) /* These positions already checked */
599 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
602 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
603 /* On end-of-str: see comment below. */
604 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
605 if (must == &PL_sv_undef) {
607 DEBUG_EXECUTE_r(must = prog->anchored_utf8); /* for debug */
612 HOP3(HOP3(last1, prog->anchored_offset, strend)
613 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
615 multiline ? FBMrf_MULTILINE : 0
617 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
618 "%s anchored substr \"%s%.*s%s\"%s",
619 (s ? "Found" : "Contradicts"),
622 - (SvTAIL(must)!=0)),
624 PL_colors[1], (SvTAIL(must) ? "$" : "")));
626 if (last1 >= last2) {
627 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
628 ", giving up...\n"));
631 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
632 ", trying floating at offset %ld...\n",
633 (long)(HOP3c(s1, 1, strend) - i_strpos)));
634 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
635 s = HOP3c(last, 1, strend);
639 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
640 (long)(s - i_strpos)));
641 t = HOP3c(s, -prog->anchored_offset, strbeg);
642 other_last = HOP3c(s, 1, strend);
650 else { /* Take into account the floating substring. */
655 t = HOP3c(s, -start_shift, strbeg);
657 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
658 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
659 last = HOP3c(t, prog->float_max_offset, strend);
660 s = HOP3c(t, prog->float_min_offset, strend);
663 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
664 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
665 /* fbm_instr() takes into account exact value of end-of-str
666 if the check is SvTAIL(ed). Since false positives are OK,
667 and end-of-str is not later than strend we are OK. */
668 if (must == &PL_sv_undef) {
670 DEBUG_EXECUTE_r(must = prog->float_utf8); /* for debug message */
673 s = fbm_instr((unsigned char*)s,
674 (unsigned char*)last + SvCUR(must)
676 must, multiline ? FBMrf_MULTILINE : 0);
677 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s floating substr \"%s%.*s%s\"%s",
678 (s ? "Found" : "Contradicts"),
680 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
682 PL_colors[1], (SvTAIL(must) ? "$" : "")));
685 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
686 ", giving up...\n"));
689 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
690 ", trying anchored starting at offset %ld...\n",
691 (long)(s1 + 1 - i_strpos)));
693 s = HOP3c(t, 1, strend);
697 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
698 (long)(s - i_strpos)));
699 other_last = s; /* Fix this later. --Hugo */
708 t = s - prog->check_offset_max;
709 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
711 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
713 /* Fixed substring is found far enough so that the match
714 cannot start at strpos. */
716 if (ml_anch && t[-1] != '\n') {
717 /* Eventually fbm_*() should handle this, but often
718 anchored_offset is not 0, so this check will not be wasted. */
719 /* XXXX In the code below we prefer to look for "^" even in
720 presence of anchored substrings. And we search even
721 beyond the found float position. These pessimizations
722 are historical artefacts only. */
724 while (t < strend - prog->minlen) {
726 if (t < check_at - prog->check_offset_min) {
727 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
728 /* Since we moved from the found position,
729 we definitely contradict the found anchored
730 substr. Due to the above check we do not
731 contradict "check" substr.
732 Thus we can arrive here only if check substr
733 is float. Redo checking for "other"=="fixed".
736 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
737 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
738 goto do_other_anchored;
740 /* We don't contradict the found floating substring. */
741 /* XXXX Why not check for STCLASS? */
743 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
744 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
747 /* Position contradicts check-string */
748 /* XXXX probably better to look for check-string
749 than for "\n", so one should lower the limit for t? */
750 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
751 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
752 other_last = strpos = s = t + 1;
757 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
758 PL_colors[0], PL_colors[1]));
762 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
763 PL_colors[0], PL_colors[1]));
767 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
770 /* The found string does not prohibit matching at strpos,
771 - no optimization of calling REx engine can be performed,
772 unless it was an MBOL and we are not after MBOL,
773 or a future STCLASS check will fail this. */
775 /* Even in this situation we may use MBOL flag if strpos is offset
776 wrt the start of the string. */
777 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
778 && (strpos != strbeg) && strpos[-1] != '\n'
779 /* May be due to an implicit anchor of m{.*foo} */
780 && !(prog->reganch & ROPT_IMPLICIT))
785 DEBUG_EXECUTE_r( if (ml_anch)
786 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
787 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
790 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
792 prog->check_utf8 /* Could be deleted already */
793 && --BmUSEFUL(prog->check_utf8) < 0
794 && (prog->check_utf8 == prog->float_utf8)
796 prog->check_substr /* Could be deleted already */
797 && --BmUSEFUL(prog->check_substr) < 0
798 && (prog->check_substr == prog->float_substr)
801 /* If flags & SOMETHING - do not do it many times on the same match */
802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
803 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
804 if (do_utf8 ? prog->check_substr : prog->check_utf8)
805 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
806 prog->check_substr = prog->check_utf8 = NULL; /* disable */
807 prog->float_substr = prog->float_utf8 = NULL; /* clear */
808 check = NULL; /* abort */
810 /* XXXX This is a remnant of the old implementation. It
811 looks wasteful, since now INTUIT can use many
813 prog->reganch &= ~RE_USE_INTUIT;
820 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
821 if (prog->regstclass) {
822 /* minlen == 0 is possible if regstclass is \b or \B,
823 and the fixed substr is ''$.
824 Since minlen is already taken into account, s+1 is before strend;
825 accidentally, minlen >= 1 guaranties no false positives at s + 1
826 even for \b or \B. But (minlen? 1 : 0) below assumes that
827 regstclass does not come from lookahead... */
828 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
829 This leaves EXACTF only, which is dealt with in find_byclass(). */
830 const U8* const str = (U8*)STRING(prog->regstclass);
831 const int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
832 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
834 const char * const endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
835 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
836 : (prog->float_substr || prog->float_utf8
837 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
842 s = find_byclass(prog, prog->regstclass, s, endpos, NULL);
845 const char *what = NULL;
847 if (endpos == strend) {
848 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
849 "Could not match STCLASS...\n") );
852 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
853 "This position contradicts STCLASS...\n") );
854 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
856 /* Contradict one of substrings */
857 if (prog->anchored_substr || prog->anchored_utf8) {
858 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
859 DEBUG_EXECUTE_r( what = "anchored" );
861 s = HOP3c(t, 1, strend);
862 if (s + start_shift + end_shift > strend) {
863 /* XXXX Should be taken into account earlier? */
864 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
865 "Could not match STCLASS...\n") );
870 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
871 "Looking for %s substr starting at offset %ld...\n",
872 what, (long)(s + start_shift - i_strpos)) );
875 /* Have both, check_string is floating */
876 if (t + start_shift >= check_at) /* Contradicts floating=check */
877 goto retry_floating_check;
878 /* Recheck anchored substring, but not floating... */
882 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
883 "Looking for anchored substr starting at offset %ld...\n",
884 (long)(other_last - i_strpos)) );
885 goto do_other_anchored;
887 /* Another way we could have checked stclass at the
888 current position only: */
893 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
894 "Looking for /%s^%s/m starting at offset %ld...\n",
895 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
898 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
900 /* Check is floating subtring. */
901 retry_floating_check:
902 t = check_at - start_shift;
903 DEBUG_EXECUTE_r( what = "floating" );
904 goto hop_and_restart;
907 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
908 "By STCLASS: moving %ld --> %ld\n",
909 (long)(t - i_strpos), (long)(s - i_strpos))
913 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
914 "Does not contradict STCLASS...\n");
919 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
920 PL_colors[4], (check ? "Guessed" : "Giving up"),
921 PL_colors[5], (long)(s - i_strpos)) );
924 fail_finish: /* Substring not found */
925 if (prog->check_substr || prog->check_utf8) /* could be removed already */
926 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
928 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
929 PL_colors[4], PL_colors[5]));
933 /* We know what class REx starts with. Try to find this position... */
934 /* if reginfo is NULL, its a dryrun */
937 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, const char
938 *strend, const regmatch_info *reginfo)
941 const I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
945 register STRLEN uskip;
949 register I32 tmp = 1; /* Scratch variable? */
950 register const bool do_utf8 = PL_reg_match_utf8;
952 /* We know what class it must start with. */
956 while (s + (uskip = UTF8SKIP(s)) <= strend) {
957 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
958 !UTF8_IS_INVARIANT((U8)s[0]) ?
959 reginclass(prog, c, (U8*)s, 0, do_utf8) :
960 REGINCLASS(prog, c, (U8*)s)) {
961 if (tmp && (!reginfo || regtry(reginfo, s)))
975 if (REGINCLASS(prog, c, (U8*)s) ||
976 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
977 /* The assignment of 2 is intentional:
978 * for the folded sharp s, the skip is 2. */
979 (skip = SHARP_S_SKIP))) {
980 if (tmp && (!reginfo || regtry(reginfo, s)))
993 if (tmp && (!reginfo || regtry(reginfo, s)))
1002 ln = STR_LEN(c); /* length to match in octets/bytes */
1003 lnc = (I32) ln; /* length to match in characters */
1005 STRLEN ulen1, ulen2;
1007 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1008 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1009 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1011 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1012 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1014 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1016 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1019 while (sm < ((U8 *) m + ln)) {
1034 c2 = PL_fold_locale[c1];
1036 e = HOP3c(strend, -((I32)lnc), s);
1038 if (!reginfo && e < s)
1039 e = s; /* Due to minlen logic of intuit() */
1041 /* The idea in the EXACTF* cases is to first find the
1042 * first character of the EXACTF* node and then, if
1043 * necessary, case-insensitively compare the full
1044 * text of the node. The c1 and c2 are the first
1045 * characters (though in Unicode it gets a bit
1046 * more complicated because there are more cases
1047 * than just upper and lower: one needs to use
1048 * the so-called folding case for case-insensitive
1049 * matching (called "loose matching" in Unicode).
1050 * ibcmp_utf8() will do just that. */
1054 U8 tmpbuf [UTF8_MAXBYTES+1];
1055 STRLEN len, foldlen;
1056 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1058 /* Upper and lower of 1st char are equal -
1059 * probably not a "letter". */
1061 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1065 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1066 m, (char **)0, ln, (bool)UTF))
1067 && (!reginfo || regtry(reginfo, s)) )
1070 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1071 uvchr_to_utf8(tmpbuf, c);
1072 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1074 && (f == c1 || f == c2)
1075 && (ln == foldlen ||
1076 !ibcmp_utf8((char *) foldbuf,
1077 (char **)0, foldlen, do_utf8,
1079 (char **)0, ln, (bool)UTF))
1080 && (!reginfo || regtry(reginfo, s)) )
1088 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1091 /* Handle some of the three Greek sigmas cases.
1092 * Note that not all the possible combinations
1093 * are handled here: some of them are handled
1094 * by the standard folding rules, and some of
1095 * them (the character class or ANYOF cases)
1096 * are handled during compiletime in
1097 * regexec.c:S_regclass(). */
1098 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1099 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1100 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1102 if ( (c == c1 || c == c2)
1104 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1105 m, (char **)0, ln, (bool)UTF))
1106 && (!reginfo || regtry(reginfo, s)) )
1109 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1110 uvchr_to_utf8(tmpbuf, c);
1111 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1113 && (f == c1 || f == c2)
1114 && (ln == foldlen ||
1115 !ibcmp_utf8((char *) foldbuf,
1116 (char **)0, foldlen, do_utf8,
1118 (char **)0, ln, (bool)UTF))
1119 && (!reginfo || regtry(reginfo, s)) )
1130 && (ln == 1 || !(OP(c) == EXACTF
1132 : ibcmp_locale(s, m, ln)))
1133 && (!reginfo || regtry(reginfo, s)) )
1139 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1140 && (ln == 1 || !(OP(c) == EXACTF
1142 : ibcmp_locale(s, m, ln)))
1143 && (!reginfo || regtry(reginfo, s)) )
1150 PL_reg_flags |= RF_tainted;
1157 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1158 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1160 tmp = ((OP(c) == BOUND ?
1161 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1162 LOAD_UTF8_CHARCLASS_ALNUM();
1163 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1164 if (tmp == !(OP(c) == BOUND ?
1165 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1166 isALNUM_LC_utf8((U8*)s)))
1169 if ((!reginfo || regtry(reginfo, s)))
1176 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1177 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1178 while (s < strend) {
1180 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1182 if ((!reginfo || regtry(reginfo, s)))
1188 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, s)))
1192 PL_reg_flags |= RF_tainted;
1199 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1200 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1202 tmp = ((OP(c) == NBOUND ?
1203 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1204 LOAD_UTF8_CHARCLASS_ALNUM();
1205 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1206 if (tmp == !(OP(c) == NBOUND ?
1207 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1208 isALNUM_LC_utf8((U8*)s)))
1210 else if ((!reginfo || regtry(reginfo, s)))
1216 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1217 tmp = ((OP(c) == NBOUND ?
1218 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1219 while (s < strend) {
1221 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1223 else if ((!reginfo || regtry(reginfo, s)))
1228 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, s)))
1233 LOAD_UTF8_CHARCLASS_ALNUM();
1234 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1235 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1236 if (tmp && (!reginfo || regtry(reginfo, s)))
1247 while (s < strend) {
1249 if (tmp && (!reginfo || regtry(reginfo, s)))
1261 PL_reg_flags |= RF_tainted;
1263 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1264 if (isALNUM_LC_utf8((U8*)s)) {
1265 if (tmp && (!reginfo || regtry(reginfo, s)))
1276 while (s < strend) {
1277 if (isALNUM_LC(*s)) {
1278 if (tmp && (!reginfo || regtry(reginfo, s)))
1291 LOAD_UTF8_CHARCLASS_ALNUM();
1292 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1293 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1294 if (tmp && (!reginfo || regtry(reginfo, s)))
1305 while (s < strend) {
1307 if (tmp && (!reginfo || regtry(reginfo, s)))
1319 PL_reg_flags |= RF_tainted;
1321 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1322 if (!isALNUM_LC_utf8((U8*)s)) {
1323 if (tmp && (!reginfo || regtry(reginfo, s)))
1334 while (s < strend) {
1335 if (!isALNUM_LC(*s)) {
1336 if (tmp && (!reginfo || regtry(reginfo, s)))
1349 LOAD_UTF8_CHARCLASS_SPACE();
1350 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1351 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1352 if (tmp && (!reginfo || regtry(reginfo, s)))
1363 while (s < strend) {
1365 if (tmp && (!reginfo || regtry(reginfo, s)))
1377 PL_reg_flags |= RF_tainted;
1379 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1380 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1381 if (tmp && (!reginfo || regtry(reginfo, s)))
1392 while (s < strend) {
1393 if (isSPACE_LC(*s)) {
1394 if (tmp && (!reginfo || regtry(reginfo, s)))
1407 LOAD_UTF8_CHARCLASS_SPACE();
1408 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1409 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1410 if (tmp && (!reginfo || regtry(reginfo, s)))
1421 while (s < strend) {
1423 if (tmp && (!reginfo || regtry(reginfo, s)))
1435 PL_reg_flags |= RF_tainted;
1437 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1438 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1439 if (tmp && (!reginfo || regtry(reginfo, s)))
1450 while (s < strend) {
1451 if (!isSPACE_LC(*s)) {
1452 if (tmp && (!reginfo || regtry(reginfo, s)))
1465 LOAD_UTF8_CHARCLASS_DIGIT();
1466 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1467 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1468 if (tmp && (!reginfo || regtry(reginfo, s)))
1479 while (s < strend) {
1481 if (tmp && (!reginfo || regtry(reginfo, s)))
1493 PL_reg_flags |= RF_tainted;
1495 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1496 if (isDIGIT_LC_utf8((U8*)s)) {
1497 if (tmp && (!reginfo || regtry(reginfo, s)))
1508 while (s < strend) {
1509 if (isDIGIT_LC(*s)) {
1510 if (tmp && (!reginfo || regtry(reginfo, s)))
1523 LOAD_UTF8_CHARCLASS_DIGIT();
1524 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1525 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1526 if (tmp && (!reginfo || regtry(reginfo, s)))
1537 while (s < strend) {
1539 if (tmp && (!reginfo || regtry(reginfo, s)))
1551 PL_reg_flags |= RF_tainted;
1553 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1554 if (!isDIGIT_LC_utf8((U8*)s)) {
1555 if (tmp && (!reginfo || regtry(reginfo, s)))
1566 while (s < strend) {
1567 if (!isDIGIT_LC(*s)) {
1568 if (tmp && (!reginfo || regtry(reginfo, s)))
1580 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1589 - regexec_flags - match a regexp against a string
1592 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1593 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1594 /* strend: pointer to null at end of string */
1595 /* strbeg: real beginning of string */
1596 /* minend: end of match must be >=minend after stringarg. */
1597 /* data: May be used for some additional optimizations. */
1598 /* nosave: For optimizations. */
1602 register regnode *c;
1603 register char *startpos = stringarg;
1604 I32 minlen; /* must match at least this many chars */
1605 I32 dontbother = 0; /* how many characters not to try at end */
1606 I32 end_shift = 0; /* Same for the end. */ /* CC */
1607 I32 scream_pos = -1; /* Internal iterator of scream. */
1608 char *scream_olds = NULL;
1609 SV* oreplsv = GvSV(PL_replgv);
1610 const bool do_utf8 = DO_UTF8(sv);
1616 regmatch_info reginfo; /* create some info to pass to regtry etc */
1618 GET_RE_DEBUG_FLAGS_DECL;
1620 PERL_UNUSED_ARG(data);
1622 /* Be paranoid... */
1623 if (prog == NULL || startpos == NULL) {
1624 Perl_croak(aTHX_ "NULL regexp parameter");
1628 multiline = prog->reganch & PMf_MULTILINE;
1629 reginfo.prog = prog;
1632 dsv0 = PERL_DEBUG_PAD_ZERO(0);
1633 dsv1 = PERL_DEBUG_PAD_ZERO(1);
1636 RX_MATCH_UTF8_set(prog, do_utf8);
1638 minlen = prog->minlen;
1639 if (strend - startpos < minlen) {
1640 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1641 "String too short [regexec_flags]...\n"));
1645 /* Check validity of program. */
1646 if (UCHARAT(prog->program) != REG_MAGIC) {
1647 Perl_croak(aTHX_ "corrupted regexp program");
1651 PL_reg_eval_set = 0;
1654 if (prog->reganch & ROPT_UTF8)
1655 PL_reg_flags |= RF_utf8;
1657 /* Mark beginning of line for ^ and lookbehind. */
1658 reginfo.bol = startpos; /* XXX not used ??? */
1662 /* Mark end of line for $ (and such) */
1665 /* see how far we have to get to not match where we matched before */
1666 reginfo.till = startpos+minend;
1668 /* If there is a "must appear" string, look for it. */
1671 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to set reginfo->ganch */
1674 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1675 reginfo.ganch = startpos;
1676 else if (sv && SvTYPE(sv) >= SVt_PVMG
1678 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1679 && mg->mg_len >= 0) {
1680 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1681 if (prog->reganch & ROPT_ANCH_GPOS) {
1682 if (s > reginfo.ganch)
1687 else /* pos() not defined */
1688 reginfo.ganch = strbeg;
1691 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1692 re_scream_pos_data d;
1694 d.scream_olds = &scream_olds;
1695 d.scream_pos = &scream_pos;
1696 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1698 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1699 goto phooey; /* not present */
1704 const char * const s0 = UTF
1705 ? pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1708 const int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1709 const char * const s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1710 UNI_DISPLAY_REGEX) : startpos;
1711 const int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1714 PerlIO_printf(Perl_debug_log,
1715 "%sMatching REx%s \"%s%*.*s%s%s\" against \"%s%.*s%s%s\"\n",
1716 PL_colors[4], PL_colors[5], PL_colors[0],
1719 len0 > 60 ? "..." : "",
1721 (int)(len1 > 60 ? 60 : len1),
1723 (len1 > 60 ? "..." : "")
1727 /* Simplest case: anchored match need be tried only once. */
1728 /* [unless only anchor is BOL and multiline is set] */
1729 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1730 if (s == startpos && regtry(®info, startpos))
1732 else if (multiline || (prog->reganch & ROPT_IMPLICIT)
1733 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1738 dontbother = minlen - 1;
1739 end = HOP3c(strend, -dontbother, strbeg) - 1;
1740 /* for multiline we only have to try after newlines */
1741 if (prog->check_substr || prog->check_utf8) {
1745 if (regtry(®info, s))
1750 if (prog->reganch & RE_USE_INTUIT) {
1751 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1762 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1763 if (regtry(®info, s))
1770 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1771 if (regtry(®info, reginfo.ganch))
1776 /* Messy cases: unanchored match. */
1777 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1778 /* we have /x+whatever/ */
1779 /* it must be a one character string (XXXX Except UTF?) */
1784 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1785 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1786 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1789 while (s < strend) {
1791 DEBUG_EXECUTE_r( did_match = 1 );
1792 if (regtry(®info, s)) goto got_it;
1794 while (s < strend && *s == ch)
1801 while (s < strend) {
1803 DEBUG_EXECUTE_r( did_match = 1 );
1804 if (regtry(®info, s)) goto got_it;
1806 while (s < strend && *s == ch)
1812 DEBUG_EXECUTE_r(if (!did_match)
1813 PerlIO_printf(Perl_debug_log,
1814 "Did not find anchored character...\n")
1817 else if (prog->anchored_substr != NULL
1818 || prog->anchored_utf8 != NULL
1819 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
1820 && prog->float_max_offset < strend - s)) {
1825 char *last1; /* Last position checked before */
1829 if (prog->anchored_substr || prog->anchored_utf8) {
1830 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1831 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1832 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1833 back_max = back_min = prog->anchored_offset;
1835 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1836 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1837 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1838 back_max = prog->float_max_offset;
1839 back_min = prog->float_min_offset;
1841 if (must == &PL_sv_undef)
1842 /* could not downgrade utf8 check substring, so must fail */
1845 last = HOP3c(strend, /* Cannot start after this */
1846 -(I32)(CHR_SVLEN(must)
1847 - (SvTAIL(must) != 0) + back_min), strbeg);
1850 last1 = HOPc(s, -1);
1852 last1 = s - 1; /* bogus */
1854 /* XXXX check_substr already used to find "s", can optimize if
1855 check_substr==must. */
1857 dontbother = end_shift;
1858 strend = HOPc(strend, -dontbother);
1859 while ( (s <= last) &&
1860 ((flags & REXEC_SCREAM)
1861 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1862 end_shift, &scream_pos, 0))
1863 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1864 (unsigned char*)strend, must,
1865 multiline ? FBMrf_MULTILINE : 0))) ) {
1866 /* we may be pointing at the wrong string */
1867 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1868 s = strbeg + (s - SvPVX_const(sv));
1869 DEBUG_EXECUTE_r( did_match = 1 );
1870 if (HOPc(s, -back_max) > last1) {
1871 last1 = HOPc(s, -back_min);
1872 s = HOPc(s, -back_max);
1875 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1877 last1 = HOPc(s, -back_min);
1881 while (s <= last1) {
1882 if (regtry(®info, s))
1888 while (s <= last1) {
1889 if (regtry(®info, s))
1895 DEBUG_EXECUTE_r(if (!did_match)
1896 PerlIO_printf(Perl_debug_log,
1897 "Did not find %s substr \"%s%.*s%s\"%s...\n",
1898 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1899 ? "anchored" : "floating"),
1901 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1903 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1907 else if ((c = prog->regstclass)) {
1909 I32 op = (U8)OP(prog->regstclass);
1910 /* don't bother with what can't match */
1911 if (PL_regkind[op] != EXACT && op != CANY)
1912 strend = HOPc(strend, -(minlen - 1));
1915 SV *prop = sv_newmortal();
1921 regprop(prog, prop, c);
1923 pv_uni_display(dsv0, (U8*)SvPVX_const(prop), SvCUR(prop), 60,
1924 UNI_DISPLAY_REGEX) :
1926 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1928 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1929 len1 = UTF ? SvCUR(dsv1) : strend - s;
1930 PerlIO_printf(Perl_debug_log,
1931 "Matching stclass \"%*.*s\" against \"%*.*s\"\n",
1935 if (find_byclass(prog, c, s, strend, ®info))
1937 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1941 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
1946 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1947 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1948 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1950 if (flags & REXEC_SCREAM) {
1951 last = screaminstr(sv, float_real, s - strbeg,
1952 end_shift, &scream_pos, 1); /* last one */
1954 last = scream_olds; /* Only one occurrence. */
1955 /* we may be pointing at the wrong string */
1956 else if (RX_MATCH_COPIED(prog))
1957 s = strbeg + (s - SvPVX_const(sv));
1961 const char * const little = SvPV_const(float_real, len);
1963 if (SvTAIL(float_real)) {
1964 if (memEQ(strend - len + 1, little, len - 1))
1965 last = strend - len + 1;
1966 else if (!multiline)
1967 last = memEQ(strend - len, little, len)
1968 ? strend - len : NULL;
1974 last = rninstr(s, strend, little, little + len);
1976 last = strend; /* matching "$" */
1980 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1981 "%sCan't trim the tail, match fails (should not happen)%s\n",
1982 PL_colors[4], PL_colors[5]));
1983 goto phooey; /* Should not happen! */
1985 dontbother = strend - last + prog->float_min_offset;
1987 if (minlen && (dontbother < minlen))
1988 dontbother = minlen - 1;
1989 strend -= dontbother; /* this one's always in bytes! */
1990 /* We don't know much -- general case. */
1993 if (regtry(®info, s))
2002 if (regtry(®info, s))
2004 } while (s++ < strend);
2012 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2014 if (PL_reg_eval_set) {
2015 /* Preserve the current value of $^R */
2016 if (oreplsv != GvSV(PL_replgv))
2017 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2018 restored, the value remains
2020 restore_pos(aTHX_ prog);
2023 /* make sure $`, $&, $', and $digit will work later */
2024 if ( !(flags & REXEC_NOT_FIRST) ) {
2025 RX_MATCH_COPY_FREE(prog);
2026 if (flags & REXEC_COPY_STR) {
2027 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2028 #ifdef PERL_OLD_COPY_ON_WRITE
2030 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2032 PerlIO_printf(Perl_debug_log,
2033 "Copy on write: regexp capture, type %d\n",
2036 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2037 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2038 assert (SvPOKp(prog->saved_copy));
2042 RX_MATCH_COPIED_on(prog);
2043 s = savepvn(strbeg, i);
2049 prog->subbeg = strbeg;
2050 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2057 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2058 PL_colors[4], PL_colors[5]));
2059 if (PL_reg_eval_set)
2060 restore_pos(aTHX_ prog);
2065 - regtry - try match at specific point
2067 STATIC I32 /* 0 failure, 1 success */
2068 S_regtry(pTHX_ const regmatch_info *reginfo, char *startpos)
2074 regexp *prog = reginfo->prog;
2075 GET_RE_DEBUG_FLAGS_DECL;
2078 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2080 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2083 PL_reg_eval_set = RS_init;
2084 DEBUG_EXECUTE_r(DEBUG_s(
2085 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2086 (IV)(PL_stack_sp - PL_stack_base));
2088 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2089 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2090 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2092 /* Apparently this is not needed, judging by wantarray. */
2093 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2094 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2097 /* Make $_ available to executed code. */
2098 if (reginfo->sv != DEFSV) {
2100 DEFSV = reginfo->sv;
2103 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2104 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2105 /* prepare for quick setting of pos */
2106 #ifdef PERL_OLD_COPY_ON_WRITE
2108 sv_force_normal_flags(sv, 0);
2110 mg = sv_magicext(reginfo->sv, (SV*)0, PERL_MAGIC_regex_global,
2111 &PL_vtbl_mglob, NULL, 0);
2115 PL_reg_oldpos = mg->mg_len;
2116 SAVEDESTRUCTOR_X(restore_pos, prog);
2118 if (!PL_reg_curpm) {
2119 Newxz(PL_reg_curpm, 1, PMOP);
2122 SV* repointer = newSViv(0);
2123 /* so we know which PL_regex_padav element is PL_reg_curpm */
2124 SvFLAGS(repointer) |= SVf_BREAK;
2125 av_push(PL_regex_padav,repointer);
2126 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2127 PL_regex_pad = AvARRAY(PL_regex_padav);
2131 PM_SETRE(PL_reg_curpm, prog);
2132 PL_reg_oldcurpm = PL_curpm;
2133 PL_curpm = PL_reg_curpm;
2134 if (RX_MATCH_COPIED(prog)) {
2135 /* Here is a serious problem: we cannot rewrite subbeg,
2136 since it may be needed if this match fails. Thus
2137 $` inside (?{}) could fail... */
2138 PL_reg_oldsaved = prog->subbeg;
2139 PL_reg_oldsavedlen = prog->sublen;
2140 #ifdef PERL_OLD_COPY_ON_WRITE
2141 PL_nrs = prog->saved_copy;
2143 RX_MATCH_COPIED_off(prog);
2146 PL_reg_oldsaved = NULL;
2147 prog->subbeg = PL_bostr;
2148 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2150 prog->startp[0] = startpos - PL_bostr;
2151 PL_reginput = startpos;
2152 PL_regstartp = prog->startp;
2153 PL_regendp = prog->endp;
2154 PL_reglastparen = &prog->lastparen;
2155 PL_reglastcloseparen = &prog->lastcloseparen;
2156 prog->lastparen = 0;
2157 prog->lastcloseparen = 0;
2159 DEBUG_EXECUTE_r(PL_reg_starttry = startpos);
2160 if (PL_reg_start_tmpl <= prog->nparens) {
2161 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2162 if(PL_reg_start_tmp)
2163 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2165 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2168 /* XXXX What this code is doing here?!!! There should be no need
2169 to do this again and again, PL_reglastparen should take care of
2172 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2173 * Actually, the code in regcppop() (which Ilya may be meaning by
2174 * PL_reglastparen), is not needed at all by the test suite
2175 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2176 * enough, for building DynaLoader, or otherwise this
2177 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2178 * will happen. Meanwhile, this code *is* needed for the
2179 * above-mentioned test suite tests to succeed. The common theme
2180 * on those tests seems to be returning null fields from matches.
2185 if (prog->nparens) {
2187 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2194 if (regmatch(reginfo, prog->program + 1)) {
2195 prog->endp[0] = PL_reginput - PL_bostr;
2198 REGCP_UNWIND(lastcp);
2202 #define RE_UNWIND_BRANCH 1
2203 #define RE_UNWIND_BRANCHJ 2
2207 typedef struct { /* XX: makes sense to enlarge it... */
2211 } re_unwind_generic_t;
2225 } re_unwind_branch_t;
2227 typedef union re_unwind_t {
2229 re_unwind_generic_t generic;
2230 re_unwind_branch_t branch;
2233 #define sayYES goto yes
2234 #define sayNO goto no
2235 #define sayNO_ANYOF goto no_anyof
2236 #define sayYES_FINAL goto yes_final
2237 #define sayNO_FINAL goto no_final
2238 #define sayNO_SILENT goto do_no
2239 #define saySAME(x) if (x) goto yes; else goto no
2241 #define POSCACHE_SUCCESS 0 /* caching success rather than failure */
2242 #define POSCACHE_SEEN 1 /* we know what we're caching */
2243 #define POSCACHE_START 2 /* the real cache: this bit maps to pos 0 */
2245 #define CACHEsayYES STMT_START { \
2246 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) { \
2247 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) { \
2248 PL_reg_poscache[0] |= (1<<POSCACHE_SUCCESS) | (1<<POSCACHE_SEEN); \
2249 PL_reg_poscache[st->u.whilem.cache_offset] |= (1<<st->u.whilem.cache_bit); \
2251 else if (PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS)) { \
2252 PL_reg_poscache[st->u.whilem.cache_offset] |= (1<<st->u.whilem.cache_bit); \
2255 /* cache records failure, but this is success */ \
2257 PerlIO_printf(Perl_debug_log, \
2258 "%*s (remove success from failure cache)\n", \
2259 REPORT_CODE_OFF+PL_regindent*2, "") \
2261 PL_reg_poscache[st->u.whilem.cache_offset] &= ~(1<<st->u.whilem.cache_bit); \
2267 #define CACHEsayNO STMT_START { \
2268 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) { \
2269 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) { \
2270 PL_reg_poscache[0] |= (1<<POSCACHE_SEEN); \
2271 PL_reg_poscache[st->u.whilem.cache_offset] |= (1<<st->u.whilem.cache_bit); \
2273 else if (!(PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2274 PL_reg_poscache[st->u.whilem.cache_offset] |= (1<<st->u.whilem.cache_bit); \
2277 /* cache records success, but this is failure */ \
2279 PerlIO_printf(Perl_debug_log, \
2280 "%*s (remove failure from success cache)\n", \
2281 REPORT_CODE_OFF+PL_regindent*2, "") \
2283 PL_reg_poscache[st->u.whilem.cache_offset] &= ~(1<<st->u.whilem.cache_bit); \
2289 /* this is used to determine how far from the left messages like
2290 'failed...' are printed. Currently 29 makes these messages line
2291 up with the opcode they refer to. Earlier perls used 25 which
2292 left these messages outdented making reviewing a debug output
2295 #define REPORT_CODE_OFF 29
2298 /* Make sure there is a test for this +1 options in re_tests */
2299 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2301 #define SLAB_FIRST(s) (&(s)->states[0])
2302 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2304 /* grab a new slab and return the first slot in it */
2306 STATIC regmatch_state *
2309 regmatch_slab *s = PL_regmatch_slab->next;
2311 Newx(s, 1, regmatch_slab);
2312 s->prev = PL_regmatch_slab;
2314 PL_regmatch_slab->next = s;
2316 PL_regmatch_slab = s;
2317 return SLAB_FIRST(s);
2320 /* simulate a recursive call to regmatch */
2322 #define REGMATCH(ns, where) \
2325 st->resume_state = resume_##where; \
2326 goto start_recurse; \
2327 resume_point_##where:
2330 /* push a new regex state. Set newst to point to it */
2332 #define PUSH_STATE(newst, resume) \
2334 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "PUSH STATE(%d)\n", depth)); \
2338 st->locinput = locinput; \
2339 st->resume_state = resume; \
2341 if (newst > SLAB_LAST(PL_regmatch_slab)) \
2342 newst = S_push_slab(aTHX); \
2343 PL_regmatch_state = newst; \
2345 newst->minmod = 0; \
2347 newst->logical = 0; \
2348 newst->unwind = 0; \
2349 locinput = PL_reginput; \
2350 nextchr = UCHARAT(locinput);
2353 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "POP STATE(%d)\n", depth)); \
2356 if (st < SLAB_FIRST(PL_regmatch_slab)) { \
2357 PL_regmatch_slab = PL_regmatch_slab->prev; \
2358 st = SLAB_LAST(PL_regmatch_slab); \
2360 PL_regmatch_state = st; \
2364 locinput = st->locinput; \
2365 nextchr = UCHARAT(locinput);
2368 - regmatch - main matching routine
2370 * Conceptually the strategy is simple: check to see whether the current
2371 * node matches, call self recursively to see whether the rest matches,
2372 * and then act accordingly. In practice we make some effort to avoid
2373 * recursion, in particular by going through "ordinary" nodes (that don't
2374 * need to know whether the rest of the match failed) by a loop instead of
2377 /* [lwall] I've hoisted the register declarations to the outer block in order to
2378 * maybe save a little bit of pushing and popping on the stack. It also takes
2379 * advantage of machines that use a register save mask on subroutine entry.
2381 * This function used to be heavily recursive, but since this had the
2382 * effect of blowing the CPU stack on complex regexes, it has been
2383 * restructured to be iterative, and to save state onto the heap rather
2384 * than the stack. Essentially whereever regmatch() used to be called, it
2385 * pushes the current state, notes where to return, then jumps back into
2388 * Originally the structure of this function used to look something like
2393 while (scan != NULL) {
2394 a++; // do stuff with a and b
2400 if (regmatch(...)) // recurse
2410 * Now it looks something like this:
2418 regmatch_state *st = new();
2420 st->a++; // do stuff with a and b
2422 while (scan != NULL) {
2430 st->resume_state = resume_FOO;
2431 goto start_recurse; // recurse
2440 st = new(); push a new state
2441 st->a = 1; st->b = 2;
2448 switch (resume_state) {
2450 goto resume_point_FOO;
2457 * WARNING: this means that any line in this function that contains a
2458 * REGMATCH() or TRYPAREN() is actually simulating a recursive call to
2459 * regmatch() using gotos instead. Thus the values of any local variables
2460 * not saved in the regmatch_state structure will have been lost when
2461 * execution resumes on the next line .
2463 * States (ie the st pointer) are allocated in slabs of about 4K in size.
2464 * PL_regmatch_state always points to the currently active state, and
2465 * PL_regmatch_slab points to the slab currently containing PL_regmatch_state.
2466 * The first time regmatch is called, the first slab is allocated, and is
2467 * never freed until interpreter desctruction. When the slab is full,
2468 * a new one is allocated chained to the end. At exit from regmatch, slabs
2469 * allocated since entry are freed.
2473 STATIC I32 /* 0 failure, 1 success */
2474 S_regmatch(pTHX_ const regmatch_info *reginfo, regnode *prog)
2477 register const bool do_utf8 = PL_reg_match_utf8;
2478 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2480 regexp *rex = reginfo->prog;
2482 regmatch_slab *orig_slab;
2483 regmatch_state *orig_state;
2485 /* the current state. This is a cached copy of PL_regmatch_state */
2486 register regmatch_state *st;
2488 /* cache heavy used fields of st in registers */
2489 register regnode *scan;
2490 register regnode *next;
2491 register I32 n = 0; /* initialize to shut up compiler warning */
2492 register char *locinput = PL_reginput;
2494 /* these variables are NOT saved during a recusive RFEGMATCH: */
2495 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2496 bool result; /* return value of S_regmatch */
2497 regnode *inner; /* Next node in internal branch. */
2498 int depth = 0; /* depth of recursion */
2499 regmatch_state *newst; /* when pushing a state, this is the new one */
2500 regmatch_state *yes_state = NULL; /* state to pop to on success of
2504 SV *re_debug_flags = NULL;
2509 /* on first ever call to regmatch, allocate first slab */
2510 if (!PL_regmatch_slab) {
2511 Newx(PL_regmatch_slab, 1, regmatch_slab);
2512 PL_regmatch_slab->prev = NULL;
2513 PL_regmatch_slab->next = NULL;
2514 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2517 /* remember current high-water mark for exit */
2518 /* XXX this should be done with SAVE* instead */
2519 orig_slab = PL_regmatch_slab;
2520 orig_state = PL_regmatch_state;
2522 /* grab next free state slot */
2523 st = ++PL_regmatch_state;
2524 if (st > SLAB_LAST(PL_regmatch_slab))
2525 st = PL_regmatch_state = S_push_slab(aTHX);
2532 /* Note that nextchr is a byte even in UTF */
2533 nextchr = UCHARAT(locinput);
2535 while (scan != NULL) {
2538 SV * const prop = sv_newmortal();
2539 const int docolor = *PL_colors[0];
2540 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2541 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2542 /* The part of the string before starttry has one color
2543 (pref0_len chars), between starttry and current
2544 position another one (pref_len - pref0_len chars),
2545 after the current position the third one.
2546 We assume that pref0_len <= pref_len, otherwise we
2547 decrease pref0_len. */
2548 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2549 ? (5 + taill) - l : locinput - PL_bostr;
2552 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2554 pref0_len = pref_len - (locinput - PL_reg_starttry);
2555 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2556 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2557 ? (5 + taill) - pref_len : PL_regeol - locinput);
2558 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2562 if (pref0_len > pref_len)
2563 pref0_len = pref_len;
2564 regprop(rex, prop, scan);
2566 const char * const s0 =
2567 do_utf8 && OP(scan) != CANY ?
2568 pv_uni_display(PERL_DEBUG_PAD(0), (U8*)(locinput - pref_len),
2569 pref0_len, 60, UNI_DISPLAY_REGEX) :
2570 locinput - pref_len;
2571 const int len0 = do_utf8 ? strlen(s0) : pref0_len;
2572 const char * const s1 = do_utf8 && OP(scan) != CANY ?
2573 pv_uni_display(PERL_DEBUG_PAD(1),
2574 (U8*)(locinput - pref_len + pref0_len),
2575 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2576 locinput - pref_len + pref0_len;
2577 const int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2578 const char * const s2 = do_utf8 && OP(scan) != CANY ?
2579 pv_uni_display(PERL_DEBUG_PAD(2), (U8*)locinput,
2580 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2582 const int len2 = do_utf8 ? strlen(s2) : l;
2583 PerlIO_printf(Perl_debug_log,
2584 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2585 (IV)(locinput - PL_bostr),
2592 (docolor ? "" : "> <"),
2596 15 - l - pref_len + 1,
2598 (IV)(scan - rex->program), PL_regindent*2, "",
2603 next = scan + NEXT_OFF(scan);
2609 if (locinput == PL_bostr)
2611 /* reginfo->till = reginfo->bol; */
2616 if (locinput == PL_bostr ||
2617 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2623 if (locinput == PL_bostr)
2627 if (locinput == reginfo->ganch)
2633 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2638 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2640 if (PL_regeol - locinput > 1)
2644 if (PL_regeol != locinput)
2648 if (!nextchr && locinput >= PL_regeol)
2651 locinput += PL_utf8skip[nextchr];
2652 if (locinput > PL_regeol)
2654 nextchr = UCHARAT(locinput);
2657 nextchr = UCHARAT(++locinput);
2660 if (!nextchr && locinput >= PL_regeol)
2662 nextchr = UCHARAT(++locinput);
2665 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2668 locinput += PL_utf8skip[nextchr];
2669 if (locinput > PL_regeol)
2671 nextchr = UCHARAT(locinput);
2674 nextchr = UCHARAT(++locinput);
2680 traverse the TRIE keeping track of all accepting states
2681 we transition through until we get to a failing node.
2689 U8 *uc = ( U8* )locinput;
2696 U8 *uscan = (U8*)NULL;
2698 SV *sv_accept_buff = NULL;
2699 const enum { trie_plain, trie_utf8, trie_uft8_fold }
2700 trie_type = do_utf8 ?
2701 (OP(scan) == TRIE ? trie_utf8 : trie_uft8_fold)
2704 /* what trie are we using right now */
2706 = (reg_trie_data*)rex->data->data[ ARG( scan ) ];
2707 st->u.trie.accepted = 0; /* how many accepting states we have seen */
2710 while ( state && uc <= (U8*)PL_regeol ) {
2712 if (trie->states[ state ].wordnum) {
2713 if (!st->u.trie.accepted ) {
2716 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
2717 sv_accept_buff=newSV(bufflen *
2718 sizeof(reg_trie_accepted) - 1);
2719 SvCUR_set(sv_accept_buff,
2720 sizeof(reg_trie_accepted));
2721 SvPOK_on(sv_accept_buff);
2722 sv_2mortal(sv_accept_buff);
2723 st->u.trie.accept_buff =
2724 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
2727 if (st->u.trie.accepted >= bufflen) {
2729 st->u.trie.accept_buff =(reg_trie_accepted*)
2730 SvGROW(sv_accept_buff,
2731 bufflen * sizeof(reg_trie_accepted));
2733 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
2734 + sizeof(reg_trie_accepted));
2736 st->u.trie.accept_buff[st->u.trie.accepted].wordnum = trie->states[state].wordnum;
2737 st->u.trie.accept_buff[st->u.trie.accepted].endpos = uc;
2738 ++st->u.trie.accepted;
2741 base = trie->states[ state ].trans.base;
2743 DEBUG_TRIE_EXECUTE_r(
2744 PerlIO_printf( Perl_debug_log,
2745 "%*s %sState: %4"UVxf", Base: %4"UVxf", Accepted: %4"UVxf" ",
2746 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
2747 (UV)state, (UV)base, (UV)st->u.trie.accepted );
2751 switch (trie_type) {
2752 case trie_uft8_fold:
2754 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags );
2759 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2760 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags );
2761 uvc = to_uni_fold( uvc, foldbuf, &foldlen );
2762 foldlen -= UNISKIP( uvc );
2763 uscan = foldbuf + UNISKIP( uvc );
2767 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN,
2776 charid = trie->charmap[ uvc ];
2780 if (trie->widecharmap) {
2781 SV** svpp = (SV**)NULL;
2782 svpp = hv_fetch(trie->widecharmap,
2783 (char*)&uvc, sizeof(UV), 0);
2785 charid = (U16)SvIV(*svpp);
2790 (base + charid > trie->uniquecharcount )
2791 && (base + charid - 1 - trie->uniquecharcount
2793 && trie->trans[base + charid - 1 -
2794 trie->uniquecharcount].check == state)
2796 state = trie->trans[base + charid - 1 -
2797 trie->uniquecharcount ].next;
2808 DEBUG_TRIE_EXECUTE_r(
2809 PerlIO_printf( Perl_debug_log,
2810 "Charid:%3x CV:%4"UVxf" After State: %4"UVxf"%s\n",
2811 charid, uvc, (UV)state, PL_colors[5] );
2814 if (!st->u.trie.accepted )
2818 There was at least one accepting state that we
2819 transitioned through. Presumably the number of accepting
2820 states is going to be low, typically one or two. So we
2821 simply scan through to find the one with lowest wordnum.
2822 Once we find it, we swap the last state into its place
2823 and decrement the size. We then try to match the rest of
2824 the pattern at the point where the word ends, if we
2825 succeed then we end the loop, otherwise the loop
2826 eventually terminates once all of the accepting states
2830 if ( st->u.trie.accepted == 1 ) {
2832 SV ** const tmp = av_fetch( trie->words, st->u.trie.accept_buff[ 0 ].wordnum-1, 0 );
2833 PerlIO_printf( Perl_debug_log,
2834 "%*s %sonly one match : #%d <%s>%s\n",
2835 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
2836 st->u.trie.accept_buff[ 0 ].wordnum,
2837 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",
2840 PL_reginput = (char *)st->u.trie.accept_buff[ 0 ].endpos;
2841 /* in this case we free tmps/leave before we call regmatch
2842 as we wont be using accept_buff again. */
2845 REGMATCH(scan + NEXT_OFF(scan), TRIE1);
2846 /*** all unsaved local vars undefined at this point */
2849 PerlIO_printf( Perl_debug_log,"%*s %sgot %"IVdf" possible matches%s\n",
2850 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4], (IV)st->u.trie.accepted,
2853 while ( !result && st->u.trie.accepted-- ) {
2856 for( cur = 1 ; cur <= st->u.trie.accepted ; cur++ ) {
2857 DEBUG_TRIE_EXECUTE_r(
2858 PerlIO_printf( Perl_debug_log,
2859 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
2860 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
2861 (IV)best, st->u.trie.accept_buff[ best ].wordnum, (IV)cur,
2862 st->u.trie.accept_buff[ cur ].wordnum, PL_colors[5] );
2865 if (st->u.trie.accept_buff[cur].wordnum <
2866 st->u.trie.accept_buff[best].wordnum)
2870 reg_trie_data * const trie = (reg_trie_data*)
2871 rex->data->data[ARG(scan)];
2872 SV ** const tmp = av_fetch( trie->words, st->u.trie.accept_buff[ best ].wordnum - 1, 0 );
2873 PerlIO_printf( Perl_debug_log, "%*s %strying alternation #%d <%s> at 0x%p%s\n",
2874 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
2875 st->u.trie.accept_buff[best].wordnum,
2876 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",scan,
2879 if ( best<st->u.trie.accepted ) {
2880 reg_trie_accepted tmp = st->u.trie.accept_buff[ best ];
2881 st->u.trie.accept_buff[ best ] = st->u.trie.accept_buff[ st->u.trie.accepted ];
2882 st->u.trie.accept_buff[ st->u.trie.accepted ] = tmp;
2883 best = st->u.trie.accepted;
2885 PL_reginput = (char *)st->u.trie.accept_buff[ best ].endpos;
2888 as far as I can tell we only need the SAVETMPS/FREETMPS
2889 for re's with EVAL in them but I'm leaving them in for
2890 all until I can be sure.
2893 REGMATCH(scan + NEXT_OFF(scan), TRIE2);
2894 /*** all unsaved local vars undefined at this point */
2907 /* unreached codepoint */
2909 char *s = STRING(scan);
2910 st->ln = STR_LEN(scan);
2911 if (do_utf8 != UTF) {
2912 /* The target and the pattern have differing utf8ness. */
2914 const char *e = s + st->ln;
2917 /* The target is utf8, the pattern is not utf8. */
2922 if (NATIVE_TO_UNI(*(U8*)s) !=
2923 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
2931 /* The target is not utf8, the pattern is utf8. */
2936 if (NATIVE_TO_UNI(*((U8*)l)) !=
2937 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
2945 nextchr = UCHARAT(locinput);
2948 /* The target and the pattern have the same utf8ness. */
2949 /* Inline the first character, for speed. */
2950 if (UCHARAT(s) != nextchr)
2952 if (PL_regeol - locinput < st->ln)
2954 if (st->ln > 1 && memNE(s, locinput, st->ln))
2957 nextchr = UCHARAT(locinput);
2961 PL_reg_flags |= RF_tainted;
2964 char *s = STRING(scan);
2965 st->ln = STR_LEN(scan);
2967 if (do_utf8 || UTF) {
2968 /* Either target or the pattern are utf8. */
2970 char *e = PL_regeol;
2972 if (ibcmp_utf8(s, 0, st->ln, (bool)UTF,
2973 l, &e, 0, do_utf8)) {
2974 /* One more case for the sharp s:
2975 * pack("U0U*", 0xDF) =~ /ss/i,
2976 * the 0xC3 0x9F are the UTF-8
2977 * byte sequence for the U+00DF. */
2979 toLOWER(s[0]) == 's' &&
2981 toLOWER(s[1]) == 's' &&
2988 nextchr = UCHARAT(locinput);
2992 /* Neither the target and the pattern are utf8. */
2994 /* Inline the first character, for speed. */
2995 if (UCHARAT(s) != nextchr &&
2996 UCHARAT(s) != ((OP(scan) == EXACTF)
2997 ? PL_fold : PL_fold_locale)[nextchr])
2999 if (PL_regeol - locinput < st->ln)
3001 if (st->ln > 1 && (OP(scan) == EXACTF
3002 ? ibcmp(s, locinput, st->ln)
3003 : ibcmp_locale(s, locinput, st->ln)))
3006 nextchr = UCHARAT(locinput);
3011 STRLEN inclasslen = PL_regeol - locinput;
3013 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3015 if (locinput >= PL_regeol)
3017 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3018 nextchr = UCHARAT(locinput);
3023 nextchr = UCHARAT(locinput);
3024 if (!REGINCLASS(rex, scan, (U8*)locinput))
3026 if (!nextchr && locinput >= PL_regeol)
3028 nextchr = UCHARAT(++locinput);
3032 /* If we might have the case of the German sharp s
3033 * in a casefolding Unicode character class. */
3035 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3036 locinput += SHARP_S_SKIP;
3037 nextchr = UCHARAT(locinput);
3043 PL_reg_flags |= RF_tainted;
3049 LOAD_UTF8_CHARCLASS_ALNUM();
3050 if (!(OP(scan) == ALNUM
3051 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3052 : isALNUM_LC_utf8((U8*)locinput)))
3056 locinput += PL_utf8skip[nextchr];
3057 nextchr = UCHARAT(locinput);
3060 if (!(OP(scan) == ALNUM
3061 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
3063 nextchr = UCHARAT(++locinput);
3066 PL_reg_flags |= RF_tainted;
3069 if (!nextchr && locinput >= PL_regeol)
3072 LOAD_UTF8_CHARCLASS_ALNUM();
3073 if (OP(scan) == NALNUM
3074 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3075 : isALNUM_LC_utf8((U8*)locinput))
3079 locinput += PL_utf8skip[nextchr];
3080 nextchr = UCHARAT(locinput);
3083 if (OP(scan) == NALNUM
3084 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
3086 nextchr = UCHARAT(++locinput);
3090 PL_reg_flags |= RF_tainted;
3094 /* was last char in word? */
3096 if (locinput == PL_bostr)
3099 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3101 st->ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3103 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3104 st->ln = isALNUM_uni(st->ln);
3105 LOAD_UTF8_CHARCLASS_ALNUM();
3106 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3109 st->ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(st->ln));
3110 n = isALNUM_LC_utf8((U8*)locinput);
3114 st->ln = (locinput != PL_bostr) ?
3115 UCHARAT(locinput - 1) : '\n';
3116 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3117 st->ln = isALNUM(st->ln);
3118 n = isALNUM(nextchr);
3121 st->ln = isALNUM_LC(st->ln);
3122 n = isALNUM_LC(nextchr);
3125 if (((!st->ln) == (!n)) == (OP(scan) == BOUND ||
3126 OP(scan) == BOUNDL))
3130 PL_reg_flags |= RF_tainted;
3136 if (UTF8_IS_CONTINUED(nextchr)) {
3137 LOAD_UTF8_CHARCLASS_SPACE();
3138 if (!(OP(scan) == SPACE
3139 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3140 : isSPACE_LC_utf8((U8*)locinput)))
3144 locinput += PL_utf8skip[nextchr];
3145 nextchr = UCHARAT(locinput);
3148 if (!(OP(scan) == SPACE
3149 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3151 nextchr = UCHARAT(++locinput);
3154 if (!(OP(scan) == SPACE
3155 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3157 nextchr = UCHARAT(++locinput);
3161 PL_reg_flags |= RF_tainted;
3164 if (!nextchr && locinput >= PL_regeol)
3167 LOAD_UTF8_CHARCLASS_SPACE();
3168 if (OP(scan) == NSPACE
3169 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3170 : isSPACE_LC_utf8((U8*)locinput))
3174 locinput += PL_utf8skip[nextchr];
3175 nextchr = UCHARAT(locinput);
3178 if (OP(scan) == NSPACE
3179 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
3181 nextchr = UCHARAT(++locinput);
3184 PL_reg_flags |= RF_tainted;
3190 LOAD_UTF8_CHARCLASS_DIGIT();
3191 if (!(OP(scan) == DIGIT
3192 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3193 : isDIGIT_LC_utf8((U8*)locinput)))
3197 locinput += PL_utf8skip[nextchr];
3198 nextchr = UCHARAT(locinput);
3201 if (!(OP(scan) == DIGIT
3202 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
3204 nextchr = UCHARAT(++locinput);
3207 PL_reg_flags |= RF_tainted;
3210 if (!nextchr && locinput >= PL_regeol)
3213 LOAD_UTF8_CHARCLASS_DIGIT();
3214 if (OP(scan) == NDIGIT
3215 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3216 : isDIGIT_LC_utf8((U8*)locinput))
3220 locinput += PL_utf8skip[nextchr];
3221 nextchr = UCHARAT(locinput);
3224 if (OP(scan) == NDIGIT
3225 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
3227 nextchr = UCHARAT(++locinput);
3230 if (locinput >= PL_regeol)
3233 LOAD_UTF8_CHARCLASS_MARK();
3234 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3236 locinput += PL_utf8skip[nextchr];
3237 while (locinput < PL_regeol &&
3238 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3239 locinput += UTF8SKIP(locinput);
3240 if (locinput > PL_regeol)
3245 nextchr = UCHARAT(locinput);
3248 PL_reg_flags |= RF_tainted;
3253 n = ARG(scan); /* which paren pair */
3254 st->ln = PL_regstartp[n];
3255 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3256 if ((I32)*PL_reglastparen < n || st->ln == -1)
3257 sayNO; /* Do not match unless seen CLOSEn. */
3258 if (st->ln == PL_regendp[n])
3261 s = PL_bostr + st->ln;
3262 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
3264 const char *e = PL_bostr + PL_regendp[n];
3266 * Note that we can't do the "other character" lookup trick as
3267 * in the 8-bit case (no pun intended) because in Unicode we
3268 * have to map both upper and title case to lower case.
3270 if (OP(scan) == REFF) {
3272 STRLEN ulen1, ulen2;
3273 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3274 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3278 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3279 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3280 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3287 nextchr = UCHARAT(locinput);
3291 /* Inline the first character, for speed. */
3292 if (UCHARAT(s) != nextchr &&
3294 (UCHARAT(s) != ((OP(scan) == REFF
3295 ? PL_fold : PL_fold_locale)[nextchr]))))
3297 st->ln = PL_regendp[n] - st->ln;
3298 if (locinput + st->ln > PL_regeol)
3300 if (st->ln > 1 && (OP(scan) == REF
3301 ? memNE(s, locinput, st->ln)
3303 ? ibcmp(s, locinput, st->ln)
3304 : ibcmp_locale(s, locinput, st->ln))))
3307 nextchr = UCHARAT(locinput);
3320 /* execute the code in the {...} */
3322 SV ** const before = SP;
3323 OP_4tree * const oop = PL_op;
3324 COP * const ocurcop = PL_curcop;
3328 PL_op = (OP_4tree*)rex->data->data[n];
3329 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3330 PAD_SAVE_LOCAL(old_comppad, (PAD*)rex->data->data[n + 2]);
3331 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3333 CALLRUNOPS(aTHX); /* Scalar context. */
3336 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3343 PAD_RESTORE_LOCAL(old_comppad);
3344 PL_curcop = ocurcop;
3347 sv_setsv(save_scalar(PL_replgv), ret);
3351 if (st->logical == 2) { /* Postponed subexpression: /(??{...})/ */
3354 /* extract RE object from returned value; compiling if
3359 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
3360 mg = mg_find(sv, PERL_MAGIC_qr);
3361 else if (SvSMAGICAL(ret)) {
3362 if (SvGMAGICAL(ret))
3363 sv_unmagic(ret, PERL_MAGIC_qr);
3365 mg = mg_find(ret, PERL_MAGIC_qr);
3369 re = (regexp *)mg->mg_obj;
3370 (void)ReREFCNT_inc(re);
3374 const char * const t = SvPV_const(ret, len);
3376 const I32 osize = PL_regsize;
3379 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
3380 re = CALLREGCOMP(aTHX_ (char*)t, (char*)t + len, &pm);
3382 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3384 sv_magic(ret,(SV*)ReREFCNT_inc(re),
3390 /* run the pattern returned from (??{...}) */
3393 PerlIO_printf(Perl_debug_log,
3394 "Entering embedded \"%s%.60s%s%s\"\n",
3398 (strlen(re->precomp) > 60 ? "..." : ""))
3401 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
3402 REGCP_SET(st->u.eval.lastcp);
3403 *PL_reglastparen = 0;
3404 *PL_reglastcloseparen = 0;
3405 PL_reginput = locinput;
3407 /* XXXX This is too dramatic a measure... */
3411 st->u.eval.toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
3412 ((re->reganch & ROPT_UTF8) != 0);
3413 if (st->u.eval.toggleutf) PL_reg_flags ^= RF_utf8;
3414 st->u.eval.prev_rex = rex;
3417 /* resume to current state on success */
3418 st->u.yes.prev_yes_state = yes_state;
3420 PUSH_STATE(newst, resume_EVAL);
3423 /* now continue from first node in postoned RE */
3424 next = re->program + 1;
3428 /* /(?(?{...})X|Y)/ */
3429 st->sw = SvTRUE(ret);
3434 n = ARG(scan); /* which paren pair */
3435 PL_reg_start_tmp[n] = locinput;
3440 n = ARG(scan); /* which paren pair */
3441 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3442 PL_regendp[n] = locinput - PL_bostr;
3443 if (n > (I32)*PL_reglastparen)
3444 *PL_reglastparen = n;
3445 *PL_reglastcloseparen = n;
3448 n = ARG(scan); /* which paren pair */
3449 st->sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
3452 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3454 next = NEXTOPER(NEXTOPER(scan));
3456 next = scan + ARG(scan);
3457 if (OP(next) == IFTHEN) /* Fake one. */
3458 next = NEXTOPER(NEXTOPER(next));
3462 st->logical = scan->flags;
3464 /*******************************************************************
3465 cc points to the regmatch_state associated with the most recent CURLYX.
3466 This struct contains info about the innermost (...)* loop (an
3467 "infoblock"), and a pointer to the next outer cc.
3469 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3471 1) After matching Y, regnode for CURLYX is processed;
3473 2) This regnode populates cc, and calls regmatch() recursively
3474 with the starting point at WHILEM node;
3476 3) Each hit of WHILEM node tries to match A and Z (in the order
3477 depending on the current iteration, min/max of {min,max} and
3478 greediness). The information about where are nodes for "A"
3479 and "Z" is read from cc, as is info on how many times "A"
3480 was already matched, and greediness.
3482 4) After A matches, the same WHILEM node is hit again.
3484 5) Each time WHILEM is hit, cc is the infoblock created by CURLYX
3485 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3486 resets cc, since this Y(A)*Z can be a part of some other loop:
3487 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3488 of the external loop.
3490 Currently present infoblocks form a tree with a stem formed by st->cc
3491 and whatever it mentions via ->next, and additional attached trees
3492 corresponding to temporarily unset infoblocks as in "5" above.
3494 In the following picture, infoblocks for outer loop of
3495 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3496 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3497 infoblocks are drawn below the "reset" infoblock.
3499 In fact in the picture below we do not show failed matches for Z and T
3500 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3501 more obvious *why* one needs to *temporary* unset infoblocks.]
3503 Matched REx position InfoBlocks Comment
3507 Y A)*?Z)*?T x <- O <- I
3508 YA )*?Z)*?T x <- O <- I
3509 YA A)*?Z)*?T x <- O <- I
3510 YAA )*?Z)*?T x <- O <- I
3511 YAA Z)*?T x <- O # Temporary unset I
3514 YAAZ Y(A)*?Z)*?T x <- O
3517 YAAZY (A)*?Z)*?T x <- O
3520 YAAZY A)*?Z)*?T x <- O <- I
3523 YAAZYA )*?Z)*?T x <- O <- I
3526 YAAZYA Z)*?T x <- O # Temporary unset I
3532 YAAZYAZ T x # Temporary unset O
3539 *******************************************************************/
3542 /* No need to save/restore up to this paren */
3543 I32 parenfloor = scan->flags;
3547 CURLYX and WHILEM are always paired: they're the moral
3548 equivalent of pp_enteriter anbd pp_iter.
3550 The only time next could be null is if the node tree is
3551 corrupt. This was mentioned on p5p a few days ago.
3553 See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-04/msg00556.html
3554 So we'll assert that this is true:
3557 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3559 /* XXXX Probably it is better to teach regpush to support
3560 parenfloor > PL_regsize... */
3561 if (parenfloor > (I32)*PL_reglastparen)
3562 parenfloor = *PL_reglastparen; /* Pessimization... */
3564 st->u.curlyx.cp = PL_savestack_ix;
3565 st->u.curlyx.outercc = st->cc;
3567 /* these fields contain the state of the current curly.
3568 * they are accessed by subsequent WHILEMs;
3569 * cur and lastloc are also updated by WHILEM */
3570 st->u.curlyx.parenfloor = parenfloor;
3571 st->u.curlyx.cur = -1; /* this will be updated by WHILEM */
3572 st->u.curlyx.min = ARG1(scan);
3573 st->u.curlyx.max = ARG2(scan);
3574 st->u.curlyx.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3575 st->u.curlyx.lastloc = 0;
3576 /* st->next and st->minmod are also read by WHILEM */
3578 PL_reginput = locinput;
3579 REGMATCH(PREVOPER(next), CURLYX); /* start on the WHILEM */
3580 /*** all unsaved local vars undefined at this point */
3581 regcpblow(st->u.curlyx.cp);
3582 st->cc = st->u.curlyx.outercc;
3588 * This is really hard to understand, because after we match
3589 * what we're trying to match, we must make sure the rest of
3590 * the REx is going to match for sure, and to do that we have
3591 * to go back UP the parse tree by recursing ever deeper. And
3592 * if it fails, we have to reset our parent's current state
3593 * that we can try again after backing off.
3598 st->cc gets initialised by CURLYX ready for use by WHILEM.
3599 So again, unless somethings been corrupted, st->cc cannot
3600 be null at that point in WHILEM.
3602 See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-04/msg00556.html
3603 So we'll assert that this is true:
3606 st->u.whilem.lastloc = st->cc->u.curlyx.lastloc; /* Detection of 0-len. */
3607 st->u.whilem.cache_offset = 0;
3608 st->u.whilem.cache_bit = 0;
3610 n = st->cc->u.curlyx.cur + 1; /* how many we know we matched */
3611 PL_reginput = locinput;
3614 PerlIO_printf(Perl_debug_log,
3615 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3616 REPORT_CODE_OFF+PL_regindent*2, "",
3617 (long)n, (long)st->cc->u.curlyx.min,
3618 (long)st->cc->u.curlyx.max, PTR2UV(st->cc))
3621 /* If degenerate scan matches "", assume scan done. */
3623 if (locinput == st->cc->u.curlyx.lastloc && n >= st->cc->u.curlyx.min) {
3624 st->u.whilem.savecc = st->cc;
3625 st->cc = st->cc->u.curlyx.outercc;
3627 st->ln = st->cc->u.curlyx.cur;
3629 PerlIO_printf(Perl_debug_log,
3630 "%*s empty match detected, try continuation...\n",
3631 REPORT_CODE_OFF+PL_regindent*2, "")
3633 REGMATCH(st->u.whilem.savecc->next, WHILEM1);
3634 /*** all unsaved local vars undefined at this point */
3635 st->cc = st->u.whilem.savecc;
3638 if (st->cc->u.curlyx.outercc)
3639 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3643 /* First just match a string of min scans. */
3645 if (n < st->cc->u.curlyx.min) {
3646 st->cc->u.curlyx.cur = n;
3647 st->cc->u.curlyx.lastloc = locinput;
3648 REGMATCH(st->cc->u.curlyx.scan, WHILEM2);
3649 /*** all unsaved local vars undefined at this point */
3652 st->cc->u.curlyx.cur = n - 1;
3653 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3658 /* Check whether we already were at this position.
3659 Postpone detection until we know the match is not
3660 *that* much linear. */
3661 if (!PL_reg_maxiter) {
3662 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3663 PL_reg_leftiter = PL_reg_maxiter;
3665 if (PL_reg_leftiter-- == 0) {
3666 const I32 size = (PL_reg_maxiter + 7 + POSCACHE_START)/8;
3667 if (PL_reg_poscache) {
3668 if ((I32)PL_reg_poscache_size < size) {
3669 Renew(PL_reg_poscache, size, char);
3670 PL_reg_poscache_size = size;
3672 Zero(PL_reg_poscache, size, char);
3675 PL_reg_poscache_size = size;
3676 Newxz(PL_reg_poscache, size, char);
3679 PerlIO_printf(Perl_debug_log,
3680 "%sDetected a super-linear match, switching on caching%s...\n",
3681 PL_colors[4], PL_colors[5])
3684 if (PL_reg_leftiter < 0) {
3685 st->u.whilem.cache_offset = locinput - PL_bostr;
3687 st->u.whilem.cache_offset = (scan->flags & 0xf) - 1 + POSCACHE_START
3688 + st->u.whilem.cache_offset * (scan->flags>>4);
3689 st->u.whilem.cache_bit = st->u.whilem.cache_offset % 8;
3690 st->u.whilem.cache_offset /= 8;
3691 if (PL_reg_poscache[st->u.whilem.cache_offset] & (1<<st->u.whilem.cache_bit)) {
3693 PerlIO_printf(Perl_debug_log,
3694 "%*s already tried at this position...\n",
3695 REPORT_CODE_OFF+PL_regindent*2, "")
3697 if (PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))
3698 /* cache records success */
3701 /* cache records failure */
3707 /* Prefer next over scan for minimal matching. */
3709 if (st->cc->minmod) {
3710 st->u.whilem.savecc = st->cc;
3711 st->cc = st->cc->u.curlyx.outercc;
3713 st->ln = st->cc->u.curlyx.cur;
3714 st->u.whilem.cp = regcppush(st->u.whilem.savecc->u.curlyx.parenfloor);
3715 REGCP_SET(st->u.whilem.lastcp);
3716 REGMATCH(st->u.whilem.savecc->next, WHILEM3);
3717 /*** all unsaved local vars undefined at this point */
3718 st->cc = st->u.whilem.savecc;
3720 regcpblow(st->u.whilem.cp);
3721 CACHEsayYES; /* All done. */
3723 REGCP_UNWIND(st->u.whilem.lastcp);
3725 if (st->cc->u.curlyx.outercc)
3726 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3728 if (n >= st->cc->u.curlyx.max) { /* Maximum greed exceeded? */
3729 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3730 && !(PL_reg_flags & RF_warned)) {
3731 PL_reg_flags |= RF_warned;
3732 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3733 "Complex regular subexpression recursion",
3740 PerlIO_printf(Perl_debug_log,
3741 "%*s trying longer...\n",
3742 REPORT_CODE_OFF+PL_regindent*2, "")
3744 /* Try scanning more and see if it helps. */
3745 PL_reginput = locinput;
3746 st->cc->u.curlyx.cur = n;
3747 st->cc->u.curlyx.lastloc = locinput;
3748 st->u.whilem.cp = regcppush(st->cc->u.curlyx.parenfloor);
3749 REGCP_SET(st->u.whilem.lastcp);
3750 REGMATCH(st->cc->u.curlyx.scan, WHILEM4);
3751 /*** all unsaved local vars undefined at this point */
3753 regcpblow(st->u.whilem.cp);
3756 REGCP_UNWIND(st->u.whilem.lastcp);
3758 st->cc->u.curlyx.cur = n - 1;
3759 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3763 /* Prefer scan over next for maximal matching. */
3765 if (n < st->cc->u.curlyx.max) { /* More greed allowed? */
3766 st->u.whilem.cp = regcppush(st->cc->u.curlyx.parenfloor);
3767 st->cc->u.curlyx.cur = n;
3768 st->cc->u.curlyx.lastloc = locinput;
3769 REGCP_SET(st->u.whilem.lastcp);
3770 REGMATCH(st->cc->u.curlyx.scan, WHILEM5);
3771 /*** all unsaved local vars undefined at this point */
3773 regcpblow(st->u.whilem.cp);
3776 REGCP_UNWIND(st->u.whilem.lastcp);
3777 regcppop(rex); /* Restore some previous $<digit>s? */
3778 PL_reginput = locinput;
3780 PerlIO_printf(Perl_debug_log,
3781 "%*s failed, try continuation...\n",
3782 REPORT_CODE_OFF+PL_regindent*2, "")
3785 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3786 && !(PL_reg_flags & RF_warned)) {
3787 PL_reg_flags |= RF_warned;
3788 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3789 "Complex regular subexpression recursion",
3793 /* Failed deeper matches of scan, so see if this one works. */
3794 st->u.whilem.savecc = st->cc;
3795 st->cc = st->cc->u.curlyx.outercc;
3797 st->ln = st->cc->u.curlyx.cur;
3798 REGMATCH(st->u.whilem.savecc->next, WHILEM6);
3799 /*** all unsaved local vars undefined at this point */
3800 st->cc = st->u.whilem.savecc;
3803 if (st->cc->u.curlyx.outercc)
3804 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3805 st->cc->u.curlyx.cur = n - 1;
3806 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3811 next = scan + ARG(scan);
3814 inner = NEXTOPER(NEXTOPER(scan));
3817 inner = NEXTOPER(scan);
3822 if (!next || OP(next) != type) /* No choice. */
3823 next = inner; /* Avoid recursion. */
3825 const I32 lastparen = *PL_reglastparen;
3826 /* Put unwinding data on stack */
3827 const I32 unwind1 = SSNEWt(1,re_unwind_branch_t);
3828 re_unwind_branch_t * const uw = SSPTRt(unwind1,re_unwind_branch_t);
3830 uw->prev = st->unwind;
3831 st->unwind = unwind1;
3832 uw->type = ((type == BRANCH)
3834 : RE_UNWIND_BRANCHJ);
3835 uw->lastparen = lastparen;
3837 uw->locinput = locinput;
3838 uw->nextchr = nextchr;
3839 uw->minmod = st->minmod;
3841 uw->regindent = ++PL_regindent;
3844 REGCP_SET(uw->lastcp);
3846 /* Now go into the first branch */
3856 st->u.curlym.l = st->u.curlym.matches = 0;
3858 /* We suppose that the next guy does not need
3859 backtracking: in particular, it is of constant non-zero length,
3860 and has no parenths to influence future backrefs. */
3861 st->ln = ARG1(scan); /* min to match */
3862 n = ARG2(scan); /* max to match */
3863 st->u.curlym.paren = scan->flags;
3864 if (st->u.curlym.paren) {
3865 if (st->u.curlym.paren > PL_regsize)
3866 PL_regsize = st->u.curlym.paren;
3867 if (st->u.curlym.paren > (I32)*PL_reglastparen)
3868 *PL_reglastparen = st->u.curlym.paren;
3870 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3871 if (st->u.curlym.paren)
3872 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3873 PL_reginput = locinput;
3874 st->u.curlym.maxwanted = st->minmod ? st->ln : n;
3875 if (st->u.curlym.maxwanted) {
3876 while (PL_reginput < PL_regeol && st->u.curlym.matches < st->u.curlym.maxwanted) {
3877 /* resume to current state on success */
3878 st->u.yes.prev_yes_state = yes_state;
3880 REGMATCH(scan, CURLYM1);
3881 yes_state = st->u.yes.prev_yes_state;
3882 /*** all unsaved local vars undefined at this point */
3885 /* on first match, determine length, u.curlym.l */
3886 if (!st->u.curlym.matches++) {
3887 if (PL_reg_match_utf8) {
3889 while (s < PL_reginput) {
3895 st->u.curlym.l = PL_reginput - locinput;
3897 if (st->u.curlym.l == 0) {
3898 st->u.curlym.matches = st->u.curlym.maxwanted;
3902 locinput = PL_reginput;
3906 PL_reginput = locinput;
3910 if (st->ln && st->u.curlym.matches < st->ln)
3912 if (HAS_TEXT(next) || JUMPABLE(next)) {
3913 regnode *text_node = next;
3915 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3917 if (! HAS_TEXT(text_node)) st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3919 if (PL_regkind[(U8)OP(text_node)] == REF) {
3920 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3923 else { st->u.curlym.c1 = (U8)*STRING(text_node); }
3924 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3925 st->u.curlym.c2 = PL_fold[st->u.curlym.c1];
3926 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3927 st->u.curlym.c2 = PL_fold_locale[st->u.curlym.c1];
3929 st->u.curlym.c2 = st->u.curlym.c1;
3933 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3935 REGCP_SET(st->u.curlym.lastcp);
3936 while (n >= st->ln || (n == REG_INFTY && st->ln > 0)) { /* ln overflow ? */
3937 /* If it could work, try it. */
3938 if (st->u.curlym.c1 == -1000 ||
3939 UCHARAT(PL_reginput) == st->u.curlym.c1 ||
3940 UCHARAT(PL_reginput) == st->u.curlym.c2)
3942 if (st->u.curlym.paren) {
3944 PL_regstartp[st->u.curlym.paren] =
3945 HOPc(PL_reginput, -st->u.curlym.l) - PL_bostr;
3946 PL_regendp[st->u.curlym.paren] = PL_reginput - PL_bostr;
3949 PL_regendp[st->u.curlym.paren] = -1;
3951 /* resume to current state on success */
3952 st->u.yes.prev_yes_state = yes_state;
3954 REGMATCH(next, CURLYM2);
3955 yes_state = st->u.yes.prev_yes_state;
3956 /*** all unsaved local vars undefined at this point */
3958 /* XXX tmp sayYES; */
3960 REGCP_UNWIND(st->u.curlym.lastcp);
3962 /* Couldn't or didn't -- move forward. */
3963 PL_reginput = locinput;
3964 /* resume to current state on success */
3965 st->u.yes.prev_yes_state = yes_state;
3967 REGMATCH(scan, CURLYM3);
3968 yes_state = st->u.yes.prev_yes_state;
3969 /*** all unsaved local vars undefined at this point */
3972 locinput = PL_reginput;
3980 PerlIO_printf(Perl_debug_log,
3981 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3982 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3983 (IV) st->u.curlym.matches, (IV)st->u.curlym.l)
3985 if (st->u.curlym.matches >= st->ln) {
3986 if (HAS_TEXT(next) || JUMPABLE(next)) {
3987 regnode *text_node = next;
3989 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3991 if (! HAS_TEXT(text_node)) st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3993 if (PL_regkind[(U8)OP(text_node)] == REF) {
3994 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3997 else { st->u.curlym.c1 = (U8)*STRING(text_node); }
3999 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
4000 st->u.curlym.c2 = PL_fold[st->u.curlym.c1];
4001 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
4002 st->u.curlym.c2 = PL_fold_locale[st->u.curlym.c1];
4004 st->u.curlym.c2 = st->u.curlym.c1;
4008 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
4011 REGCP_SET(st->u.curlym.lastcp);
4012 while (st->u.curlym.matches >= st->ln) {
4013 /* If it could work, try it. */
4014 if (st->u.curlym.c1 == -1000 ||
4015 UCHARAT(PL_reginput) == st->u.curlym.c1 ||
4016 UCHARAT(PL_reginput) == st->u.curlym.c2)
4019 PerlIO_printf(Perl_debug_log,
4020 "%*s trying tail with matches=%"IVdf"...\n",
4021 (int)(REPORT_CODE_OFF+PL_regindent*2),
4022 "", (IV)st->u.curlym.matches)
4024 if (st->u.curlym.paren) {
4025 if (st->u.curlym.matches) {
4026 PL_regstartp[st->u.curlym.paren]
4027 = HOPc(PL_reginput, -st->u.curlym.l) - PL_bostr;
4028 PL_regendp[st->u.curlym.paren] = PL_reginput - PL_bostr;
4031 PL_regendp[st->u.curlym.paren] = -1;
4033 /* resume to current state on success */
4034 st->u.yes.prev_yes_state = yes_state;
4036 REGMATCH(next, CURLYM4);
4037 yes_state = st->u.yes.prev_yes_state;
4038 /*** all unsaved local vars undefined at this point */
4040 /* XXX tmp sayYES; */
4042 REGCP_UNWIND(st->u.curlym.lastcp);
4044 /* Couldn't or didn't -- back up. */
4045 st->u.curlym.matches--;
4046 locinput = HOPc(locinput, -st->u.curlym.l);
4047 PL_reginput = locinput;
4055 st->u.plus.paren = scan->flags; /* Which paren to set */
4056 if (st->u.plus.paren > PL_regsize)
4057 PL_regsize = st->u.plus.paren;
4058 if (st->u.plus.paren > (I32)*PL_reglastparen)
4059 *PL_reglastparen = st->u.plus.paren;
4060 st->ln = ARG1(scan); /* min to match */
4061 n = ARG2(scan); /* max to match */
4062 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4065 st->u.plus.paren = 0;
4066 st->ln = ARG1(scan); /* min to match */
4067 n = ARG2(scan); /* max to match */
4068 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4073 scan = NEXTOPER(scan);
4074 st->u.plus.paren = 0;
4079 scan = NEXTOPER(scan);
4080 st->u.plus.paren = 0;
4083 * Lookahead to avoid useless match attempts
4084 * when we know what character comes next.
4088 * Used to only do .*x and .*?x, but now it allows
4089 * for )'s, ('s and (?{ ... })'s to be in the way
4090 * of the quantifier and the EXACT-like node. -- japhy
4093 if (HAS_TEXT(next) || JUMPABLE(next)) {
4095 regnode *text_node = next;
4097 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
4099 if (! HAS_TEXT(text_node)) st->u.plus.c1 = st->u.plus.c2 = -1000;
4101 if (PL_regkind[(U8)OP(text_node)] == REF) {
4102 st->u.plus.c1 = st->u.plus.c2 = -1000;
4103 goto assume_ok_easy;
4105 else { s = (U8*)STRING(text_node); }
4108 st->u.plus.c2 = st->u.plus.c1 = *s;
4109 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
4110 st->u.plus.c2 = PL_fold[st->u.plus.c1];
4111 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
4112 st->u.plus.c2 = PL_fold_locale[st->u.plus.c1];
4115 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
4116 STRLEN ulen1, ulen2;
4117 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4118 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4120 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4121 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4123 st->u.plus.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4125 st->u.plus.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4129 st->u.plus.c2 = st->u.plus.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4136 st->u.plus.c1 = st->u.plus.c2 = -1000;
4138 PL_reginput = locinput;
4141 if (st->ln && regrepeat(rex, scan, st->ln) < st->ln)
4143 locinput = PL_reginput;
4144 REGCP_SET(st->u.plus.lastcp);
4145 if (st->u.plus.c1 != -1000) {
4146 st->u.plus.old = locinput;
4147 st->u.plus.count = 0;
4149 if (n == REG_INFTY) {
4150 st->u.plus.e = PL_regeol - 1;
4152 while (UTF8_IS_CONTINUATION(*(U8*)st->u.plus.e))
4157 for (st->u.plus.e = locinput;
4158 m >0 && st->u.plus.e + UTF8SKIP(st->u.plus.e) <= PL_regeol; m--)
4159 st->u.plus.e += UTF8SKIP(st->u.plus.e);
4162 st->u.plus.e = locinput + n - st->ln;
4163 if (st->u.plus.e >= PL_regeol)
4164 st->u.plus.e = PL_regeol - 1;
4167 /* Find place 'next' could work */
4169 if (st->u.plus.c1 == st->u.plus.c2) {
4170 while (locinput <= st->u.plus.e &&
4171 UCHARAT(locinput) != st->u.plus.c1)
4174 while (locinput <= st->u.plus.e
4175 && UCHARAT(locinput) != st->u.plus.c1
4176 && UCHARAT(locinput) != st->u.plus.c2)
4179 st->u.plus.count = locinput - st->u.plus.old;
4182 if (st->u.plus.c1 == st->u.plus.c2) {
4184 /* count initialised to
4185 * utf8_distance(old, locinput) */
4186 while (locinput <= st->u.plus.e &&
4187 utf8n_to_uvchr((U8*)locinput,
4188 UTF8_MAXBYTES, &len,
4189 uniflags) != (UV)st->u.plus.c1) {
4195 /* count initialised to
4196 * utf8_distance(old, locinput) */
4197 while (locinput <= st->u.plus.e) {
4198 UV c = utf8n_to_uvchr((U8*)locinput,
4199 UTF8_MAXBYTES, &len,
4201 if (c == (UV)st->u.plus.c1 || c == (UV)st->u.plus.c2)
4208 if (locinput > st->u.plus.e)
4210 /* PL_reginput == old now */
4211 if (locinput != st->u.plus.old) {
4212 st->ln = 1; /* Did some */
4213 if (regrepeat(rex, scan, st->u.plus.count) < st->u.plus.count)
4216 /* PL_reginput == locinput now */
4217 TRYPAREN(st->u.plus.paren, st->ln, locinput, PLUS1);
4218 /*** all unsaved local vars undefined at this point */
4219 PL_reginput = locinput; /* Could be reset... */
4220 REGCP_UNWIND(st->u.plus.lastcp);
4221 /* Couldn't or didn't -- move forward. */
4222 st->u.plus.old = locinput;
4224 locinput += UTF8SKIP(locinput);
4227 st->u.plus.count = 1;
4231 while (n >= st->ln || (n == REG_INFTY && st->ln > 0)) { /* ln overflow ? */
4233 if (st->u.plus.c1 != -1000) {
4235 c = utf8n_to_uvchr((U8*)PL_reginput,
4239 c = UCHARAT(PL_reginput);
4240 /* If it could work, try it. */
4241 if (c == (UV)st->u.plus.c1 || c == (UV)st->u.plus.c2)
4243 TRYPAREN(st->u.plus.paren, st->ln, PL_reginput, PLUS2);
4244 /*** all unsaved local vars undefined at this point */
4245 REGCP_UNWIND(st->u.plus.lastcp);
4248 /* If it could work, try it. */
4249 else if (st->u.plus.c1 == -1000)
4251 TRYPAREN(st->u.plus.paren, st->ln, PL_reginput, PLUS3);
4252 /*** all unsaved local vars undefined at this point */
4253 REGCP_UNWIND(st->u.plus.lastcp);
4255 /* Couldn't or didn't -- move forward. */
4256 PL_reginput = locinput;
4257 if (regrepeat(rex, scan, 1)) {
4259 locinput = PL_reginput;
4266 n = regrepeat(rex, scan, n);
4267 locinput = PL_reginput;
4268 if (st->ln < n && PL_regkind[(U8)OP(next)] == EOL &&
4269 (OP(next) != MEOL ||
4270 OP(next) == SEOL || OP(next) == EOS))
4272 st->ln = n; /* why back off? */
4273 /* ...because $ and \Z can match before *and* after
4274 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4275 We should back off by one in this case. */
4276 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
4279 REGCP_SET(st->u.plus.lastcp);
4282 while (n >= st->ln) {
4283 if (st->u.plus.c1 != -1000) {
4285 c = utf8n_to_uvchr((U8*)PL_reginput,
4289 c = UCHARAT(PL_reginput);
4291 /* If it could work, try it. */
4292 if (st->u.plus.c1 == -1000 || c == (UV)st->u.plus.c1 || c == (UV)st->u.plus.c2)
4294 TRYPAREN(st->u.plus.paren, n, PL_reginput, PLUS4);
4295 /*** all unsaved local vars undefined at this point */
4296 REGCP_UNWIND(st->u.plus.lastcp);
4298 /* Couldn't or didn't -- back up. */
4300 PL_reginput = locinput = HOPc(locinput, -1);
4307 if (locinput < reginfo->till) {
4308 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4309 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
4311 (long)(locinput - PL_reg_starttry),
4312 (long)(reginfo->till - PL_reg_starttry),
4314 sayNO_FINAL; /* Cannot match: too short. */
4316 PL_reginput = locinput; /* put where regtry can find it */
4317 sayYES_FINAL; /* Success! */
4319 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
4321 PerlIO_printf(Perl_debug_log,
4322 "%*s %ssubpattern success...%s\n",
4323 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5]));
4324 PL_reginput = locinput; /* put where regtry can find it */
4325 sayYES_FINAL; /* Success! */
4327 case SUSPEND: /* (?>FOO) */
4328 st->u.ifmatch.wanted = 1;
4329 PL_reginput = locinput;
4332 case UNLESSM: /* -ve lookaround: (?!FOO), or with flags, (?<!foo) */
4333 st->u.ifmatch.wanted = 0;
4334 goto ifmatch_trivial_fail_test;
4336 case IFMATCH: /* +ve lookaround: (?=FOO), or with flags, (?<=foo) */
4337 st->u.ifmatch.wanted = 1;
4338 ifmatch_trivial_fail_test:
4340 char * const s = HOPBACKc(locinput, scan->flags);
4345 st->sw = 1 - st->u.ifmatch.wanted;
4347 else if (st->u.ifmatch.wanted)
4349 next = scan + ARG(scan);
4357 PL_reginput = locinput;
4360 /* resume to current state on success */
4361 st->u.yes.prev_yes_state = yes_state;
4363 PUSH_STATE(newst, resume_IFMATCH);
4365 next = NEXTOPER(NEXTOPER(scan));
4369 next = scan + ARG(scan);
4374 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
4375 PTR2UV(scan), OP(scan));
4376 Perl_croak(aTHX_ "regexp memory corruption");
4384 /* simulate recursively calling regmatch(), but without actually
4385 * recursing - ie save the current state on the heap rather than on
4386 * the stack, then re-enter the loop. This avoids complex regexes
4387 * blowing the processor stack */
4391 /* push new state */
4392 regmatch_state *oldst = st;
4396 /* grab the next free state slot */
4398 if (st > SLAB_LAST(PL_regmatch_slab))
4399 st = S_push_slab(aTHX);
4400 PL_regmatch_state = st;
4404 oldst->locinput = locinput;
4407 locinput = PL_reginput;
4408 nextchr = UCHARAT(locinput);
4422 * We get here only if there's trouble -- normally "case END" is
4423 * the terminating point.
4425 Perl_croak(aTHX_ "corrupted regexp pointers");
4432 /* we have successfully completed a subexpression, but we must now
4433 * pop to the state marked by yes_state and continue from there */
4435 /*XXX tmp for CURLYM*/
4436 regmatch_slab *oslab = PL_regmatch_slab;
4437 regmatch_state *ost = st, *oys=yes_state;
4440 assert(st != yes_state);
4441 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
4442 || yes_state > SLAB_LAST(PL_regmatch_slab))
4444 /* not in this slab, pop slab */
4445 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
4446 PL_regmatch_slab = PL_regmatch_slab->prev;
4447 st = SLAB_LAST(PL_regmatch_slab);
4449 depth -= (st - yes_state);
4450 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "POP STATE TO (%d)\n", depth));
4452 yes_state = st->u.yes.prev_yes_state;
4453 PL_regmatch_state = st;
4455 switch (st->resume_state) {
4457 if (st->u.eval.toggleutf)
4458 PL_reg_flags ^= RF_utf8;
4460 rex = st->u.eval.prev_rex;
4461 /* XXXX This is too dramatic a measure... */
4463 /* Restore parens of the caller without popping the
4466 I32 tmp = PL_savestack_ix;
4467 PL_savestack_ix = st->u.eval.lastcp;
4469 PL_savestack_ix = tmp;
4471 PL_reginput = locinput;
4472 /* continue at the node following the (??{...}) */
4476 case resume_IFMATCH:
4479 st->sw = st->u.ifmatch.wanted;
4481 else if (!st->u.ifmatch.wanted)
4484 if (OP(st->scan) == SUSPEND)
4485 locinput = PL_reginput;
4487 locinput = PL_reginput = st->locinput;
4488 nextchr = UCHARAT(locinput);
4490 next = st->scan + ARG(st->scan);
4491 if (next == st->scan)
4495 /* XXX tmp don't handle yes_state yet */
4496 case resume_CURLYM1:
4497 case resume_CURLYM2:
4498 case resume_CURLYM3:
4499 case resume_CURLYM4:
4500 PL_regmatch_slab =oslab;
4502 PL_regmatch_state = st;
4505 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "XXX revering a CURLYM\n"));
4509 Perl_croak(aTHX_ "unexpected yes reume state");
4513 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
4514 PL_colors[4], PL_colors[5]));
4521 /* XXX this is duplicate(ish) code to that in the do_no section.
4522 * eventually a yes should just pop the stack back to the current
4525 /* restore previous state and re-enter */
4528 switch (st->resume_state) {
4530 goto resume_point_TRIE1;
4532 goto resume_point_TRIE2;
4534 goto resume_point_CURLYX;
4535 case resume_WHILEM1:
4536 goto resume_point_WHILEM1;
4537 case resume_WHILEM2:
4538 goto resume_point_WHILEM2;
4539 case resume_WHILEM3:
4540 goto resume_point_WHILEM3;
4541 case resume_WHILEM4:
4542 goto resume_point_WHILEM4;
4543 case resume_WHILEM5:
4544 goto resume_point_WHILEM5;
4545 case resume_WHILEM6:
4546 goto resume_point_WHILEM6;
4547 case resume_CURLYM1:
4548 goto resume_point_CURLYM1;
4549 case resume_CURLYM2:
4550 goto resume_point_CURLYM2;
4551 case resume_CURLYM3:
4552 goto resume_point_CURLYM3;
4553 case resume_CURLYM4:
4554 goto resume_point_CURLYM4;
4556 goto resume_point_PLUS1;
4558 goto resume_point_PLUS2;
4560 goto resume_point_PLUS3;
4562 goto resume_point_PLUS4;
4564 case resume_IFMATCH:
4567 Perl_croak(aTHX_ "regexp resume memory corruption");
4574 PerlIO_printf(Perl_debug_log,
4575 "%*s %sfailed...%s\n",
4576 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5])
4582 re_unwind_t * const uw = SSPTRt(st->unwind,re_unwind_t);
4585 case RE_UNWIND_BRANCH:
4586 case RE_UNWIND_BRANCHJ:
4588 re_unwind_branch_t * const uwb = &(uw->branch);
4589 const I32 lastparen = uwb->lastparen;
4591 REGCP_UNWIND(uwb->lastcp);
4592 for (n = *PL_reglastparen; n > lastparen; n--)
4594 *PL_reglastparen = n;
4595 scan = next = uwb->next;
4596 st->minmod = uwb->minmod;
4598 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
4599 ? BRANCH : BRANCHJ) ) { /* Failure */
4600 st->unwind = uwb->prev;
4606 /* Have more choice yet. Reuse the same uwb. */
4607 if ((n = (uwb->type == RE_UNWIND_BRANCH
4608 ? NEXT_OFF(next) : ARG(next))))
4611 next = NULL; /* XXXX Needn't unwinding in this case... */
4613 next = NEXTOPER(scan);
4614 if (uwb->type == RE_UNWIND_BRANCHJ)
4615 next = NEXTOPER(next);
4616 locinput = uwb->locinput;
4617 nextchr = uwb->nextchr;
4619 PL_regindent = uwb->regindent;
4626 Perl_croak(aTHX_ "regexp unwind memory corruption");
4637 /* there's a previous state to backtrack to */
4639 switch (st->resume_state) {
4641 goto resume_point_TRIE1;
4643 goto resume_point_TRIE2;
4645 /* we have failed an (??{...}). Restore state to the outer re
4646 * then re-throw the failure */
4647 if (st->u.eval.toggleutf)
4648 PL_reg_flags ^= RF_utf8;
4650 rex = st->u.eval.prev_rex;
4651 yes_state = st->u.yes.prev_yes_state;
4653 /* XXXX This is too dramatic a measure... */
4656 PL_reginput = locinput;
4657 REGCP_UNWIND(st->u.eval.lastcp);
4662 goto resume_point_CURLYX;
4663 case resume_WHILEM1:
4664 goto resume_point_WHILEM1;
4665 case resume_WHILEM2:
4666 goto resume_point_WHILEM2;
4667 case resume_WHILEM3:
4668 goto resume_point_WHILEM3;
4669 case resume_WHILEM4:
4670 goto resume_point_WHILEM4;
4671 case resume_WHILEM5:
4672 goto resume_point_WHILEM5;
4673 case resume_WHILEM6:
4674 goto resume_point_WHILEM6;
4675 case resume_CURLYM1:
4676 goto resume_point_CURLYM1;
4677 case resume_CURLYM2:
4678 goto resume_point_CURLYM2;
4679 case resume_CURLYM3:
4680 goto resume_point_CURLYM3;
4681 case resume_CURLYM4:
4682 goto resume_point_CURLYM4;
4683 case resume_IFMATCH:
4684 yes_state = st->u.yes.prev_yes_state;
4687 st->sw = !st->u.ifmatch.wanted;
4689 else if (st->u.ifmatch.wanted)
4692 assert(OP(scan) != SUSPEND); /* XXX DAPM tmp */
4693 locinput = PL_reginput = st->locinput;
4694 nextchr = UCHARAT(locinput);
4695 next = scan + ARG(scan);
4701 goto resume_point_PLUS1;
4703 goto resume_point_PLUS2;
4705 goto resume_point_PLUS3;
4707 goto resume_point_PLUS4;
4709 Perl_croak(aTHX_ "regexp resume memory corruption");
4715 /* restore original high-water mark */
4716 PL_regmatch_slab = orig_slab;
4717 PL_regmatch_state = orig_state;
4719 /* free all slabs above current one */
4720 if (orig_slab->next) {
4721 regmatch_slab *osl, *sl = orig_slab->next;
4722 orig_slab->next = NULL;
4735 - regrepeat - repeatedly match something simple, report how many
4738 * [This routine now assumes that it will only match on things of length 1.
4739 * That was true before, but now we assume scan - reginput is the count,
4740 * rather than incrementing count on every character. [Er, except utf8.]]
4743 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max)
4746 register char *scan;
4748 register char *loceol = PL_regeol;
4749 register I32 hardcount = 0;
4750 register bool do_utf8 = PL_reg_match_utf8;
4753 if (max == REG_INFTY)
4755 else if (max < loceol - scan)
4756 loceol = scan + max;
4761 while (scan < loceol && hardcount < max && *scan != '\n') {
4762 scan += UTF8SKIP(scan);
4766 while (scan < loceol && *scan != '\n')
4773 while (scan < loceol && hardcount < max) {
4774 scan += UTF8SKIP(scan);
4784 case EXACT: /* length of string is 1 */
4786 while (scan < loceol && UCHARAT(scan) == c)
4789 case EXACTF: /* length of string is 1 */
4791 while (scan < loceol &&
4792 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4795 case EXACTFL: /* length of string is 1 */
4796 PL_reg_flags |= RF_tainted;
4798 while (scan < loceol &&
4799 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4805 while (hardcount < max && scan < loceol &&
4806 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
4807 scan += UTF8SKIP(scan);
4811 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
4818 LOAD_UTF8_CHARCLASS_ALNUM();
4819 while (hardcount < max && scan < loceol &&
4820 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4821 scan += UTF8SKIP(scan);
4825 while (scan < loceol && isALNUM(*scan))
4830 PL_reg_flags |= RF_tainted;
4833 while (hardcount < max && scan < loceol &&
4834 isALNUM_LC_utf8((U8*)scan)) {
4835 scan += UTF8SKIP(scan);
4839 while (scan < loceol && isALNUM_LC(*scan))
4846 LOAD_UTF8_CHARCLASS_ALNUM();
4847 while (hardcount < max && scan < loceol &&
4848 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4849 scan += UTF8SKIP(scan);
4853 while (scan < loceol && !isALNUM(*scan))
4858 PL_reg_flags |= RF_tainted;
4861 while (hardcount < max && scan < loceol &&
4862 !isALNUM_LC_utf8((U8*)scan)) {
4863 scan += UTF8SKIP(scan);
4867 while (scan < loceol && !isALNUM_LC(*scan))
4874 LOAD_UTF8_CHARCLASS_SPACE();
4875 while (hardcount < max && scan < loceol &&
4877 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4878 scan += UTF8SKIP(scan);
4882 while (scan < loceol && isSPACE(*scan))
4887 PL_reg_flags |= RF_tainted;
4890 while (hardcount < max && scan < loceol &&
4891 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4892 scan += UTF8SKIP(scan);
4896 while (scan < loceol && isSPACE_LC(*scan))
4903 LOAD_UTF8_CHARCLASS_SPACE();
4904 while (hardcount < max && scan < loceol &&
4906 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4907 scan += UTF8SKIP(scan);
4911 while (scan < loceol && !isSPACE(*scan))
4916 PL_reg_flags |= RF_tainted;
4919 while (hardcount < max && scan < loceol &&
4920 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4921 scan += UTF8SKIP(scan);
4925 while (scan < loceol && !isSPACE_LC(*scan))
4932 LOAD_UTF8_CHARCLASS_DIGIT();
4933 while (hardcount < max && scan < loceol &&
4934 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4935 scan += UTF8SKIP(scan);
4939 while (scan < loceol && isDIGIT(*scan))
4946 LOAD_UTF8_CHARCLASS_DIGIT();
4947 while (hardcount < max && scan < loceol &&
4948 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4949 scan += UTF8SKIP(scan);
4953 while (scan < loceol && !isDIGIT(*scan))
4957 default: /* Called on something of 0 width. */
4958 break; /* So match right here or not at all. */
4964 c = scan - PL_reginput;
4968 SV *re_debug_flags = NULL;
4969 SV * const prop = sv_newmortal();
4972 regprop(prog, prop, p);
4973 PerlIO_printf(Perl_debug_log,
4974 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4975 REPORT_CODE_OFF+1, "", SvPVX_const(prop),(IV)c,(IV)max);
4984 - regclass_swash - prepare the utf8 swash
4988 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4994 const struct reg_data *data = prog ? prog->data : NULL;
4996 if (data && data->count) {
4997 const U32 n = ARG(node);
4999 if (data->what[n] == 's') {
5000 SV * const rv = (SV*)data->data[n];
5001 AV * const av = (AV*)SvRV((SV*)rv);
5002 SV **const ary = AvARRAY(av);
5005 /* See the end of regcomp.c:S_regclass() for
5006 * documentation of these array elements. */
5009 a = SvROK(ary[1]) ? &ary[1] : 0;
5010 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
5014 else if (si && doinit) {
5015 sw = swash_init("utf8", "", si, 1, 0);
5016 (void)av_store(av, 1, sw);
5032 - reginclass - determine if a character falls into a character class
5034 The n is the ANYOF regnode, the p is the target string, lenp
5035 is pointer to the maximum length of how far to go in the p
5036 (if the lenp is zero, UTF8SKIP(p) is used),
5037 do_utf8 tells whether the target string is in UTF-8.
5042 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
5045 const char flags = ANYOF_FLAGS(n);
5051 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
5052 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5053 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) | UTF8_CHECK_ONLY);
5054 /* see [perl #37836] for UTF8_ALLOW_ANYUV */
5055 if (len == (STRLEN)-1)
5056 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
5059 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
5060 if (do_utf8 || (flags & ANYOF_UNICODE)) {
5063 if (do_utf8 && !ANYOF_RUNTIME(n)) {
5064 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
5067 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
5071 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
5074 if (swash_fetch(sw, p, do_utf8))
5076 else if (flags & ANYOF_FOLD) {
5077 if (!match && lenp && av) {
5079 for (i = 0; i <= av_len(av); i++) {
5080 SV* const sv = *av_fetch(av, i, FALSE);
5082 const char * const s = SvPV_const(sv, len);
5084 if (len <= plen && memEQ(s, (char*)p, len)) {
5092 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
5095 to_utf8_fold(p, tmpbuf, &tmplen);
5096 if (swash_fetch(sw, tmpbuf, do_utf8))
5102 if (match && lenp && *lenp == 0)
5103 *lenp = UNISKIP(NATIVE_TO_UNI(c));
5105 if (!match && c < 256) {
5106 if (ANYOF_BITMAP_TEST(n, c))
5108 else if (flags & ANYOF_FOLD) {
5111 if (flags & ANYOF_LOCALE) {
5112 PL_reg_flags |= RF_tainted;
5113 f = PL_fold_locale[c];
5117 if (f != c && ANYOF_BITMAP_TEST(n, f))
5121 if (!match && (flags & ANYOF_CLASS)) {
5122 PL_reg_flags |= RF_tainted;
5124 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
5125 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
5126 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
5127 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
5128 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
5129 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
5130 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
5131 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
5132 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
5133 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
5134 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
5135 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
5136 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
5137 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
5138 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
5139 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
5140 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
5141 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
5142 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
5143 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
5144 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
5145 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
5146 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
5147 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
5148 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
5149 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
5150 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
5151 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
5152 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
5153 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
5154 ) /* How's that for a conditional? */
5161 return (flags & ANYOF_INVERT) ? !match : match;
5165 S_reghop3(U8 *s, I32 off, U8* lim)
5169 while (off-- && s < lim) {
5170 /* XXX could check well-formedness here */
5178 if (UTF8_IS_CONTINUED(*s)) {
5179 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
5182 /* XXX could check well-formedness here */
5190 S_reghopmaybe3(U8* s, I32 off, U8* lim)
5194 while (off-- && s < lim) {
5195 /* XXX could check well-formedness here */
5205 if (UTF8_IS_CONTINUED(*s)) {
5206 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
5209 /* XXX could check well-formedness here */
5221 restore_pos(pTHX_ void *arg)
5224 regexp * const rex = (regexp *)arg;
5225 if (PL_reg_eval_set) {
5226 if (PL_reg_oldsaved) {
5227 rex->subbeg = PL_reg_oldsaved;
5228 rex->sublen = PL_reg_oldsavedlen;
5229 #ifdef PERL_OLD_COPY_ON_WRITE
5230 rex->saved_copy = PL_nrs;
5232 RX_MATCH_COPIED_on(rex);
5234 PL_reg_magic->mg_len = PL_reg_oldpos;
5235 PL_reg_eval_set = 0;
5236 PL_curpm = PL_reg_oldcurpm;
5241 S_to_utf8_substr(pTHX_ register regexp *prog)
5243 if (prog->float_substr && !prog->float_utf8) {
5244 SV* const sv = newSVsv(prog->float_substr);
5245 prog->float_utf8 = sv;
5246 sv_utf8_upgrade(sv);
5247 if (SvTAIL(prog->float_substr))
5249 if (prog->float_substr == prog->check_substr)
5250 prog->check_utf8 = sv;
5252 if (prog->anchored_substr && !prog->anchored_utf8) {
5253 SV* const sv = newSVsv(prog->anchored_substr);
5254 prog->anchored_utf8 = sv;
5255 sv_utf8_upgrade(sv);
5256 if (SvTAIL(prog->anchored_substr))
5258 if (prog->anchored_substr == prog->check_substr)
5259 prog->check_utf8 = sv;
5264 S_to_byte_substr(pTHX_ register regexp *prog)
5267 if (prog->float_utf8 && !prog->float_substr) {
5268 SV* sv = newSVsv(prog->float_utf8);
5269 prog->float_substr = sv;
5270 if (sv_utf8_downgrade(sv, TRUE)) {
5271 if (SvTAIL(prog->float_utf8))
5275 prog->float_substr = sv = &PL_sv_undef;
5277 if (prog->float_utf8 == prog->check_utf8)
5278 prog->check_substr = sv;
5280 if (prog->anchored_utf8 && !prog->anchored_substr) {
5281 SV* sv = newSVsv(prog->anchored_utf8);
5282 prog->anchored_substr = sv;
5283 if (sv_utf8_downgrade(sv, TRUE)) {
5284 if (SvTAIL(prog->anchored_utf8))
5288 prog->anchored_substr = sv = &PL_sv_undef;
5290 if (prog->anchored_utf8 == prog->check_utf8)
5291 prog->check_substr = sv;
5297 * c-indentation-style: bsd
5299 * indent-tabs-mode: t
5302 * ex: set ts=8 sts=4 sw=4 noet: