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(p,c) (ANYOF_FLAGS(p) ? reginclass(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_ regexp *rex)
223 GET_RE_DEBUG_FLAGS_DECL;
225 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
227 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
228 i = SSPOPINT; /* Parentheses elements to pop. */
229 input = (char *) SSPOPPTR;
230 *PL_reglastcloseparen = SSPOPINT;
231 *PL_reglastparen = SSPOPINT;
232 PL_regsize = SSPOPINT;
234 /* Now restore the parentheses context. */
235 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
236 i > 0; i -= REGCP_PAREN_ELEMS) {
238 paren = (U32)SSPOPINT;
239 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
240 PL_regstartp[paren] = SSPOPINT;
242 if (paren <= *PL_reglastparen)
243 PL_regendp[paren] = tmps;
245 PerlIO_printf(Perl_debug_log,
246 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
247 (UV)paren, (IV)PL_regstartp[paren],
248 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
249 (IV)PL_regendp[paren],
250 (paren > *PL_reglastparen ? "(no)" : ""));
254 if ((I32)(*PL_reglastparen + 1) <= rex->nparens) {
255 PerlIO_printf(Perl_debug_log,
256 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
257 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
261 /* It would seem that the similar code in regtry()
262 * already takes care of this, and in fact it is in
263 * a better location to since this code can #if 0-ed out
264 * but the code in regtry() is needed or otherwise tests
265 * requiring null fields (pat.t#187 and split.t#{13,14}
266 * (as of patchlevel 7877) will fail. Then again,
267 * this code seems to be necessary or otherwise
268 * building DynaLoader will fail:
269 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
271 for (paren = *PL_reglastparen + 1; (I32)paren <= rex->nparens; paren++) {
272 if ((I32)paren > PL_regsize)
273 PL_regstartp[paren] = -1;
274 PL_regendp[paren] = -1;
280 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
282 #define TRYPAREN(paren, n, input, where) { \
285 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
286 PL_regendp[paren] = input - PL_bostr; \
289 PL_regendp[paren] = -1; \
291 REGMATCH(next, where); \
295 PL_regendp[paren] = -1; \
300 * pregexec and friends
304 - pregexec - match a regexp against a string
307 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
308 char *strbeg, I32 minend, SV *screamer, U32 nosave)
309 /* strend: pointer to null at end of string */
310 /* strbeg: real beginning of string */
311 /* minend: end of match must be >=minend after stringarg. */
312 /* nosave: For optimizations. */
315 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
316 nosave ? 0 : REXEC_COPY_STR);
321 * Need to implement the following flags for reg_anch:
323 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
325 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
326 * INTUIT_AUTORITATIVE_ML
327 * INTUIT_ONCE_NOML - Intuit can match in one location only.
330 * Another flag for this function: SECOND_TIME (so that float substrs
331 * with giant delta may be not rechecked).
334 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
336 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
337 Otherwise, only SvCUR(sv) is used to get strbeg. */
339 /* XXXX We assume that strpos is strbeg unless sv. */
341 /* XXXX Some places assume that there is a fixed substring.
342 An update may be needed if optimizer marks as "INTUITable"
343 RExen without fixed substrings. Similarly, it is assumed that
344 lengths of all the strings are no more than minlen, thus they
345 cannot come from lookahead.
346 (Or minlen should take into account lookahead.) */
348 /* A failure to find a constant substring means that there is no need to make
349 an expensive call to REx engine, thus we celebrate a failure. Similarly,
350 finding a substring too deep into the string means that less calls to
351 regtry() should be needed.
353 REx compiler's optimizer found 4 possible hints:
354 a) Anchored substring;
356 c) Whether we are anchored (beginning-of-line or \G);
357 d) First node (of those at offset 0) which may distingush positions;
358 We use a)b)d) and multiline-part of c), and try to find a position in the
359 string which does not contradict any of them.
362 /* Most of decisions we do here should have been done at compile time.
363 The nodes of the REx which we used for the search should have been
364 deleted from the finite automaton. */
367 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
368 char *strend, U32 flags, re_scream_pos_data *data)
371 register I32 start_shift = 0;
372 /* Should be nonnegative! */
373 register I32 end_shift = 0;
378 const int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
380 register char *other_last = NULL; /* other substr checked before this */
381 char *check_at = NULL; /* check substr found at this pos */
382 const I32 multiline = prog->reganch & PMf_MULTILINE;
384 const char * const i_strpos = strpos;
385 SV * const dsv = PERL_DEBUG_PAD_ZERO(0);
388 GET_RE_DEBUG_FLAGS_DECL;
390 RX_MATCH_UTF8_set(prog,do_utf8);
392 if (prog->reganch & ROPT_UTF8) {
393 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
394 "UTF-8 regex...\n"));
395 PL_reg_flags |= RF_utf8;
399 const char *s = PL_reg_match_utf8 ?
400 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
402 const int len = PL_reg_match_utf8 ?
403 strlen(s) : strend - strpos;
406 if (PL_reg_match_utf8)
407 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
408 "UTF-8 target...\n"));
409 PerlIO_printf(Perl_debug_log,
410 "%sGuessing start of match, REx%s \"%s%.60s%s%s\" against \"%s%.*s%s%s\"...\n",
411 PL_colors[4], PL_colors[5], PL_colors[0],
414 (strlen(prog->precomp) > 60 ? "..." : ""),
416 (int)(len > 60 ? 60 : len),
418 (len > 60 ? "..." : "")
422 /* CHR_DIST() would be more correct here but it makes things slow. */
423 if (prog->minlen > strend - strpos) {
424 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
425 "String too short... [re_intuit_start]\n"));
428 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
431 if (!prog->check_utf8 && prog->check_substr)
432 to_utf8_substr(prog);
433 check = prog->check_utf8;
435 if (!prog->check_substr && prog->check_utf8)
436 to_byte_substr(prog);
437 check = prog->check_substr;
439 if (check == &PL_sv_undef) {
440 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
441 "Non-utf string cannot match utf check string\n"));
444 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
445 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
446 || ( (prog->reganch & ROPT_ANCH_BOL)
447 && !multiline ) ); /* Check after \n? */
450 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
451 | ROPT_IMPLICIT)) /* not a real BOL */
452 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
454 && (strpos != strbeg)) {
455 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
458 if (prog->check_offset_min == prog->check_offset_max &&
459 !(prog->reganch & ROPT_CANY_SEEN)) {
460 /* Substring at constant offset from beg-of-str... */
463 s = HOP3c(strpos, prog->check_offset_min, strend);
465 slen = SvCUR(check); /* >= 1 */
467 if ( strend - s > slen || strend - s < slen - 1
468 || (strend - s == slen && strend[-1] != '\n')) {
469 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
472 /* Now should match s[0..slen-2] */
474 if (slen && (*SvPVX_const(check) != *s
476 && memNE(SvPVX_const(check), s, slen)))) {
478 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
482 else if (*SvPVX_const(check) != *s
483 || ((slen = SvCUR(check)) > 1
484 && memNE(SvPVX_const(check), s, slen)))
487 goto success_at_start;
490 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
492 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
493 end_shift = prog->minlen - start_shift -
494 CHR_SVLEN(check) + (SvTAIL(check) != 0);
496 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
497 - (SvTAIL(check) != 0);
498 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
500 if (end_shift < eshift)
504 else { /* Can match at random position */
507 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
508 /* Should be nonnegative! */
509 end_shift = prog->minlen - start_shift -
510 CHR_SVLEN(check) + (SvTAIL(check) != 0);
513 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
515 Perl_croak(aTHX_ "panic: end_shift");
519 /* Find a possible match in the region s..strend by looking for
520 the "check" substring in the region corrected by start/end_shift. */
521 if (flags & REXEC_SCREAM) {
522 I32 p = -1; /* Internal iterator of scream. */
523 I32 * const pp = data ? data->scream_pos : &p;
525 if (PL_screamfirst[BmRARE(check)] >= 0
526 || ( BmRARE(check) == '\n'
527 && (BmPREVIOUS(check) == SvCUR(check) - 1)
529 s = screaminstr(sv, check,
530 start_shift + (s - strbeg), end_shift, pp, 0);
533 /* we may be pointing at the wrong string */
534 if (s && RX_MATCH_COPIED(prog))
535 s = strbeg + (s - SvPVX_const(sv));
537 *data->scream_olds = s;
539 else if (prog->reganch & ROPT_CANY_SEEN)
540 s = fbm_instr((U8*)(s + start_shift),
541 (U8*)(strend - end_shift),
542 check, multiline ? FBMrf_MULTILINE : 0);
544 s = fbm_instr(HOP3(s, start_shift, strend),
545 HOP3(strend, -end_shift, strbeg),
546 check, multiline ? FBMrf_MULTILINE : 0);
548 /* Update the count-of-usability, remove useless subpatterns,
551 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s %s substr \"%s%.*s%s\"%s%s",
552 (s ? "Found" : "Did not find"),
553 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
555 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
557 PL_colors[1], (SvTAIL(check) ? "$" : ""),
558 (s ? " at offset " : "...\n") ) );
565 /* Finish the diagnostic message */
566 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
568 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
569 Start with the other substr.
570 XXXX no SCREAM optimization yet - and a very coarse implementation
571 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
572 *always* match. Probably should be marked during compile...
573 Probably it is right to do no SCREAM here...
576 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
577 /* Take into account the "other" substring. */
578 /* XXXX May be hopelessly wrong for UTF... */
581 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
584 char * const last = HOP3c(s, -start_shift, strbeg);
589 t = s - prog->check_offset_max;
590 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
592 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
597 t = HOP3c(t, prog->anchored_offset, strend);
598 if (t < other_last) /* These positions already checked */
600 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
603 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
604 /* On end-of-str: see comment below. */
605 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
606 if (must == &PL_sv_undef) {
608 DEBUG_EXECUTE_r(must = prog->anchored_utf8); /* for debug */
613 HOP3(HOP3(last1, prog->anchored_offset, strend)
614 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
616 multiline ? FBMrf_MULTILINE : 0
618 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
619 "%s anchored substr \"%s%.*s%s\"%s",
620 (s ? "Found" : "Contradicts"),
623 - (SvTAIL(must)!=0)),
625 PL_colors[1], (SvTAIL(must) ? "$" : "")));
627 if (last1 >= last2) {
628 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
629 ", giving up...\n"));
632 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
633 ", trying floating at offset %ld...\n",
634 (long)(HOP3c(s1, 1, strend) - i_strpos)));
635 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
636 s = HOP3c(last, 1, strend);
640 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
641 (long)(s - i_strpos)));
642 t = HOP3c(s, -prog->anchored_offset, strbeg);
643 other_last = HOP3c(s, 1, strend);
651 else { /* Take into account the floating substring. */
656 t = HOP3c(s, -start_shift, strbeg);
658 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
659 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
660 last = HOP3c(t, prog->float_max_offset, strend);
661 s = HOP3c(t, prog->float_min_offset, strend);
664 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
665 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
666 /* fbm_instr() takes into account exact value of end-of-str
667 if the check is SvTAIL(ed). Since false positives are OK,
668 and end-of-str is not later than strend we are OK. */
669 if (must == &PL_sv_undef) {
671 DEBUG_EXECUTE_r(must = prog->float_utf8); /* for debug message */
674 s = fbm_instr((unsigned char*)s,
675 (unsigned char*)last + SvCUR(must)
677 must, multiline ? FBMrf_MULTILINE : 0);
678 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s floating substr \"%s%.*s%s\"%s",
679 (s ? "Found" : "Contradicts"),
681 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
683 PL_colors[1], (SvTAIL(must) ? "$" : "")));
686 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
687 ", giving up...\n"));
690 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
691 ", trying anchored starting at offset %ld...\n",
692 (long)(s1 + 1 - i_strpos)));
694 s = HOP3c(t, 1, strend);
698 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
699 (long)(s - i_strpos)));
700 other_last = s; /* Fix this later. --Hugo */
709 t = s - prog->check_offset_max;
710 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
712 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
714 /* Fixed substring is found far enough so that the match
715 cannot start at strpos. */
717 if (ml_anch && t[-1] != '\n') {
718 /* Eventually fbm_*() should handle this, but often
719 anchored_offset is not 0, so this check will not be wasted. */
720 /* XXXX In the code below we prefer to look for "^" even in
721 presence of anchored substrings. And we search even
722 beyond the found float position. These pessimizations
723 are historical artefacts only. */
725 while (t < strend - prog->minlen) {
727 if (t < check_at - prog->check_offset_min) {
728 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
729 /* Since we moved from the found position,
730 we definitely contradict the found anchored
731 substr. Due to the above check we do not
732 contradict "check" substr.
733 Thus we can arrive here only if check substr
734 is float. Redo checking for "other"=="fixed".
737 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
738 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
739 goto do_other_anchored;
741 /* We don't contradict the found floating substring. */
742 /* XXXX Why not check for STCLASS? */
744 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
745 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
748 /* Position contradicts check-string */
749 /* XXXX probably better to look for check-string
750 than for "\n", so one should lower the limit for t? */
751 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
752 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
753 other_last = strpos = s = t + 1;
758 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
759 PL_colors[0], PL_colors[1]));
763 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
764 PL_colors[0], PL_colors[1]));
768 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
771 /* The found string does not prohibit matching at strpos,
772 - no optimization of calling REx engine can be performed,
773 unless it was an MBOL and we are not after MBOL,
774 or a future STCLASS check will fail this. */
776 /* Even in this situation we may use MBOL flag if strpos is offset
777 wrt the start of the string. */
778 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
779 && (strpos != strbeg) && strpos[-1] != '\n'
780 /* May be due to an implicit anchor of m{.*foo} */
781 && !(prog->reganch & ROPT_IMPLICIT))
786 DEBUG_EXECUTE_r( if (ml_anch)
787 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
788 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
791 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
793 prog->check_utf8 /* Could be deleted already */
794 && --BmUSEFUL(prog->check_utf8) < 0
795 && (prog->check_utf8 == prog->float_utf8)
797 prog->check_substr /* Could be deleted already */
798 && --BmUSEFUL(prog->check_substr) < 0
799 && (prog->check_substr == prog->float_substr)
802 /* If flags & SOMETHING - do not do it many times on the same match */
803 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
804 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
805 if (do_utf8 ? prog->check_substr : prog->check_utf8)
806 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
807 prog->check_substr = prog->check_utf8 = NULL; /* disable */
808 prog->float_substr = prog->float_utf8 = NULL; /* clear */
809 check = NULL; /* abort */
811 /* XXXX This is a remnant of the old implementation. It
812 looks wasteful, since now INTUIT can use many
814 prog->reganch &= ~RE_USE_INTUIT;
821 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
822 if (prog->regstclass) {
823 /* minlen == 0 is possible if regstclass is \b or \B,
824 and the fixed substr is ''$.
825 Since minlen is already taken into account, s+1 is before strend;
826 accidentally, minlen >= 1 guaranties no false positives at s + 1
827 even for \b or \B. But (minlen? 1 : 0) below assumes that
828 regstclass does not come from lookahead... */
829 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
830 This leaves EXACTF only, which is dealt with in find_byclass(). */
831 const U8* const str = (U8*)STRING(prog->regstclass);
832 const int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
833 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
835 const char * const endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
836 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
837 : (prog->float_substr || prog->float_utf8
838 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
844 s = find_byclass(prog, prog->regstclass, s, endpos, 1);
847 const char *what = NULL;
849 if (endpos == strend) {
850 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
851 "Could not match STCLASS...\n") );
854 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
855 "This position contradicts STCLASS...\n") );
856 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
858 /* Contradict one of substrings */
859 if (prog->anchored_substr || prog->anchored_utf8) {
860 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
861 DEBUG_EXECUTE_r( what = "anchored" );
863 s = HOP3c(t, 1, strend);
864 if (s + start_shift + end_shift > strend) {
865 /* XXXX Should be taken into account earlier? */
866 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
867 "Could not match STCLASS...\n") );
872 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
873 "Looking for %s substr starting at offset %ld...\n",
874 what, (long)(s + start_shift - i_strpos)) );
877 /* Have both, check_string is floating */
878 if (t + start_shift >= check_at) /* Contradicts floating=check */
879 goto retry_floating_check;
880 /* Recheck anchored substring, but not floating... */
884 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
885 "Looking for anchored substr starting at offset %ld...\n",
886 (long)(other_last - i_strpos)) );
887 goto do_other_anchored;
889 /* Another way we could have checked stclass at the
890 current position only: */
895 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
896 "Looking for /%s^%s/m starting at offset %ld...\n",
897 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
900 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
902 /* Check is floating subtring. */
903 retry_floating_check:
904 t = check_at - start_shift;
905 DEBUG_EXECUTE_r( what = "floating" );
906 goto hop_and_restart;
909 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
910 "By STCLASS: moving %ld --> %ld\n",
911 (long)(t - i_strpos), (long)(s - i_strpos))
915 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
916 "Does not contradict STCLASS...\n");
921 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
922 PL_colors[4], (check ? "Guessed" : "Giving up"),
923 PL_colors[5], (long)(s - i_strpos)) );
926 fail_finish: /* Substring not found */
927 if (prog->check_substr || prog->check_utf8) /* could be removed already */
928 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
930 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
931 PL_colors[4], PL_colors[5]));
935 /* We know what class REx starts with. Try to find this position... */
937 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32 norun)
940 const I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
944 register STRLEN uskip;
948 register I32 tmp = 1; /* Scratch variable? */
949 register const bool do_utf8 = PL_reg_match_utf8;
951 /* We know what class it must start with. */
955 while (s + (uskip = UTF8SKIP(s)) <= strend) {
956 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
957 !UTF8_IS_INVARIANT((U8)s[0]) ?
958 reginclass(c, (U8*)s, 0, do_utf8) :
959 REGINCLASS(c, (U8*)s)) {
960 if (tmp && (norun || regtry(prog, s)))
974 if (REGINCLASS(c, (U8*)s) ||
975 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
976 /* The assignment of 2 is intentional:
977 * for the folded sharp s, the skip is 2. */
978 (skip = SHARP_S_SKIP))) {
979 if (tmp && (norun || regtry(prog, s)))
992 if (tmp && (norun || regtry(prog, s)))
1001 ln = STR_LEN(c); /* length to match in octets/bytes */
1002 lnc = (I32) ln; /* length to match in characters */
1004 STRLEN ulen1, ulen2;
1006 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1007 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1008 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1010 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1011 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1013 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1015 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1018 while (sm < ((U8 *) m + ln)) {
1033 c2 = PL_fold_locale[c1];
1035 e = HOP3c(strend, -((I32)lnc), s);
1038 e = s; /* Due to minlen logic of intuit() */
1040 /* The idea in the EXACTF* cases is to first find the
1041 * first character of the EXACTF* node and then, if
1042 * necessary, case-insensitively compare the full
1043 * text of the node. The c1 and c2 are the first
1044 * characters (though in Unicode it gets a bit
1045 * more complicated because there are more cases
1046 * than just upper and lower: one needs to use
1047 * the so-called folding case for case-insensitive
1048 * matching (called "loose matching" in Unicode).
1049 * ibcmp_utf8() will do just that. */
1053 U8 tmpbuf [UTF8_MAXBYTES+1];
1054 STRLEN len, foldlen;
1055 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1057 /* Upper and lower of 1st char are equal -
1058 * probably not a "letter". */
1060 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1064 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1065 m, (char **)0, ln, (bool)UTF))
1066 && (norun || regtry(prog, s)) )
1069 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1070 uvchr_to_utf8(tmpbuf, c);
1071 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1073 && (f == c1 || f == c2)
1074 && (ln == foldlen ||
1075 !ibcmp_utf8((char *) foldbuf,
1076 (char **)0, foldlen, do_utf8,
1078 (char **)0, ln, (bool)UTF))
1079 && (norun || regtry(prog, s)) )
1087 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1090 /* Handle some of the three Greek sigmas cases.
1091 * Note that not all the possible combinations
1092 * are handled here: some of them are handled
1093 * by the standard folding rules, and some of
1094 * them (the character class or ANYOF cases)
1095 * are handled during compiletime in
1096 * regexec.c:S_regclass(). */
1097 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1098 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1099 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1101 if ( (c == c1 || c == c2)
1103 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1104 m, (char **)0, ln, (bool)UTF))
1105 && (norun || regtry(prog, s)) )
1108 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1109 uvchr_to_utf8(tmpbuf, c);
1110 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1112 && (f == c1 || f == c2)
1113 && (ln == foldlen ||
1114 !ibcmp_utf8((char *) foldbuf,
1115 (char **)0, foldlen, do_utf8,
1117 (char **)0, ln, (bool)UTF))
1118 && (norun || regtry(prog, s)) )
1129 && (ln == 1 || !(OP(c) == EXACTF
1131 : ibcmp_locale(s, m, ln)))
1132 && (norun || regtry(prog, s)) )
1138 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1139 && (ln == 1 || !(OP(c) == EXACTF
1141 : ibcmp_locale(s, m, ln)))
1142 && (norun || regtry(prog, s)) )
1149 PL_reg_flags |= RF_tainted;
1156 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1157 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1159 tmp = ((OP(c) == BOUND ?
1160 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1161 LOAD_UTF8_CHARCLASS_ALNUM();
1162 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1163 if (tmp == !(OP(c) == BOUND ?
1164 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1165 isALNUM_LC_utf8((U8*)s)))
1168 if ((norun || regtry(prog, s)))
1175 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1176 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1177 while (s < strend) {
1179 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1181 if ((norun || regtry(prog, s)))
1187 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1191 PL_reg_flags |= RF_tainted;
1198 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1199 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1201 tmp = ((OP(c) == NBOUND ?
1202 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1203 LOAD_UTF8_CHARCLASS_ALNUM();
1204 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1205 if (tmp == !(OP(c) == NBOUND ?
1206 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1207 isALNUM_LC_utf8((U8*)s)))
1209 else if ((norun || regtry(prog, s)))
1215 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1216 tmp = ((OP(c) == NBOUND ?
1217 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1218 while (s < strend) {
1220 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1222 else if ((norun || regtry(prog, s)))
1227 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1232 LOAD_UTF8_CHARCLASS_ALNUM();
1233 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1234 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1235 if (tmp && (norun || regtry(prog, s)))
1246 while (s < strend) {
1248 if (tmp && (norun || regtry(prog, s)))
1260 PL_reg_flags |= RF_tainted;
1262 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1263 if (isALNUM_LC_utf8((U8*)s)) {
1264 if (tmp && (norun || regtry(prog, s)))
1275 while (s < strend) {
1276 if (isALNUM_LC(*s)) {
1277 if (tmp && (norun || regtry(prog, s)))
1290 LOAD_UTF8_CHARCLASS_ALNUM();
1291 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1292 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1293 if (tmp && (norun || regtry(prog, s)))
1304 while (s < strend) {
1306 if (tmp && (norun || regtry(prog, s)))
1318 PL_reg_flags |= RF_tainted;
1320 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1321 if (!isALNUM_LC_utf8((U8*)s)) {
1322 if (tmp && (norun || regtry(prog, s)))
1333 while (s < strend) {
1334 if (!isALNUM_LC(*s)) {
1335 if (tmp && (norun || regtry(prog, s)))
1348 LOAD_UTF8_CHARCLASS_SPACE();
1349 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1350 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1351 if (tmp && (norun || regtry(prog, s)))
1362 while (s < strend) {
1364 if (tmp && (norun || regtry(prog, s)))
1376 PL_reg_flags |= RF_tainted;
1378 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1379 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1380 if (tmp && (norun || regtry(prog, s)))
1391 while (s < strend) {
1392 if (isSPACE_LC(*s)) {
1393 if (tmp && (norun || regtry(prog, s)))
1406 LOAD_UTF8_CHARCLASS_SPACE();
1407 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1408 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1409 if (tmp && (norun || regtry(prog, s)))
1420 while (s < strend) {
1422 if (tmp && (norun || regtry(prog, s)))
1434 PL_reg_flags |= RF_tainted;
1436 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1437 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1438 if (tmp && (norun || regtry(prog, s)))
1449 while (s < strend) {
1450 if (!isSPACE_LC(*s)) {
1451 if (tmp && (norun || regtry(prog, s)))
1464 LOAD_UTF8_CHARCLASS_DIGIT();
1465 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1466 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1467 if (tmp && (norun || regtry(prog, s)))
1478 while (s < strend) {
1480 if (tmp && (norun || regtry(prog, s)))
1492 PL_reg_flags |= RF_tainted;
1494 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1495 if (isDIGIT_LC_utf8((U8*)s)) {
1496 if (tmp && (norun || regtry(prog, s)))
1507 while (s < strend) {
1508 if (isDIGIT_LC(*s)) {
1509 if (tmp && (norun || regtry(prog, s)))
1522 LOAD_UTF8_CHARCLASS_DIGIT();
1523 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1524 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1525 if (tmp && (norun || regtry(prog, s)))
1536 while (s < strend) {
1538 if (tmp && (norun || regtry(prog, s)))
1550 PL_reg_flags |= RF_tainted;
1552 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1553 if (!isDIGIT_LC_utf8((U8*)s)) {
1554 if (tmp && (norun || regtry(prog, s)))
1565 while (s < strend) {
1566 if (!isDIGIT_LC(*s)) {
1567 if (tmp && (norun || regtry(prog, s)))
1579 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1588 - regexec_flags - match a regexp against a string
1591 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1592 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1593 /* strend: pointer to null at end of string */
1594 /* strbeg: real beginning of string */
1595 /* minend: end of match must be >=minend after stringarg. */
1596 /* data: May be used for some additional optimizations. */
1597 /* nosave: For optimizations. */
1601 register regnode *c;
1602 register char *startpos = stringarg;
1603 I32 minlen; /* must match at least this many chars */
1604 I32 dontbother = 0; /* how many characters not to try at end */
1605 I32 end_shift = 0; /* Same for the end. */ /* CC */
1606 I32 scream_pos = -1; /* Internal iterator of scream. */
1607 char *scream_olds = NULL;
1608 SV* oreplsv = GvSV(PL_replgv);
1609 const bool do_utf8 = DO_UTF8(sv);
1610 const I32 multiline = prog->reganch & PMf_MULTILINE;
1612 SV * const dsv0 = PERL_DEBUG_PAD_ZERO(0);
1613 SV * const dsv1 = PERL_DEBUG_PAD_ZERO(1);
1616 GET_RE_DEBUG_FLAGS_DECL;
1618 PERL_UNUSED_ARG(data);
1619 RX_MATCH_UTF8_set(prog,do_utf8);
1623 PL_regnarrate = DEBUG_r_TEST;
1626 /* Be paranoid... */
1627 if (prog == NULL || startpos == NULL) {
1628 Perl_croak(aTHX_ "NULL regexp parameter");
1632 minlen = prog->minlen;
1633 if (strend - startpos < minlen) {
1634 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1635 "String too short [regexec_flags]...\n"));
1639 /* Check validity of program. */
1640 if (UCHARAT(prog->program) != REG_MAGIC) {
1641 Perl_croak(aTHX_ "corrupted regexp program");
1645 PL_reg_eval_set = 0;
1648 if (prog->reganch & ROPT_UTF8)
1649 PL_reg_flags |= RF_utf8;
1651 /* Mark beginning of line for ^ and lookbehind. */
1652 PL_regbol = startpos;
1656 /* Mark end of line for $ (and such) */
1659 /* see how far we have to get to not match where we matched before */
1660 PL_regtill = startpos+minend;
1662 /* We start without call_cc context. */
1665 /* If there is a "must appear" string, look for it. */
1668 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1671 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1672 PL_reg_ganch = startpos;
1673 else if (sv && SvTYPE(sv) >= SVt_PVMG
1675 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1676 && mg->mg_len >= 0) {
1677 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1678 if (prog->reganch & ROPT_ANCH_GPOS) {
1679 if (s > PL_reg_ganch)
1684 else /* pos() not defined */
1685 PL_reg_ganch = strbeg;
1688 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1689 re_scream_pos_data d;
1691 d.scream_olds = &scream_olds;
1692 d.scream_pos = &scream_pos;
1693 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1695 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1696 goto phooey; /* not present */
1701 const char * const s0 = UTF
1702 ? pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1705 const int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1706 const char * const s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1707 UNI_DISPLAY_REGEX) : startpos;
1708 const int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1711 PerlIO_printf(Perl_debug_log,
1712 "%sMatching REx%s \"%s%*.*s%s%s\" against \"%s%.*s%s%s\"\n",
1713 PL_colors[4], PL_colors[5], PL_colors[0],
1716 len0 > 60 ? "..." : "",
1718 (int)(len1 > 60 ? 60 : len1),
1720 (len1 > 60 ? "..." : "")
1724 /* Simplest case: anchored match need be tried only once. */
1725 /* [unless only anchor is BOL and multiline is set] */
1726 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1727 if (s == startpos && regtry(prog, startpos))
1729 else if (multiline || (prog->reganch & ROPT_IMPLICIT)
1730 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1735 dontbother = minlen - 1;
1736 end = HOP3c(strend, -dontbother, strbeg) - 1;
1737 /* for multiline we only have to try after newlines */
1738 if (prog->check_substr || prog->check_utf8) {
1742 if (regtry(prog, s))
1747 if (prog->reganch & RE_USE_INTUIT) {
1748 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1759 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1760 if (regtry(prog, s))
1767 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1768 if (regtry(prog, PL_reg_ganch))
1773 /* Messy cases: unanchored match. */
1774 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1775 /* we have /x+whatever/ */
1776 /* it must be a one character string (XXXX Except UTF?) */
1781 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1782 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1783 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1786 while (s < strend) {
1788 DEBUG_EXECUTE_r( did_match = 1 );
1789 if (regtry(prog, s)) goto got_it;
1791 while (s < strend && *s == ch)
1798 while (s < strend) {
1800 DEBUG_EXECUTE_r( did_match = 1 );
1801 if (regtry(prog, s)) goto got_it;
1803 while (s < strend && *s == ch)
1809 DEBUG_EXECUTE_r(if (!did_match)
1810 PerlIO_printf(Perl_debug_log,
1811 "Did not find anchored character...\n")
1814 else if (prog->anchored_substr != NULL
1815 || prog->anchored_utf8 != NULL
1816 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
1817 && prog->float_max_offset < strend - s)) {
1822 char *last1; /* Last position checked before */
1826 if (prog->anchored_substr || prog->anchored_utf8) {
1827 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1828 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1829 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1830 back_max = back_min = prog->anchored_offset;
1832 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1833 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1834 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1835 back_max = prog->float_max_offset;
1836 back_min = prog->float_min_offset;
1838 if (must == &PL_sv_undef)
1839 /* could not downgrade utf8 check substring, so must fail */
1842 last = HOP3c(strend, /* Cannot start after this */
1843 -(I32)(CHR_SVLEN(must)
1844 - (SvTAIL(must) != 0) + back_min), strbeg);
1847 last1 = HOPc(s, -1);
1849 last1 = s - 1; /* bogus */
1851 /* XXXX check_substr already used to find "s", can optimize if
1852 check_substr==must. */
1854 dontbother = end_shift;
1855 strend = HOPc(strend, -dontbother);
1856 while ( (s <= last) &&
1857 ((flags & REXEC_SCREAM)
1858 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1859 end_shift, &scream_pos, 0))
1860 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1861 (unsigned char*)strend, must,
1862 multiline ? FBMrf_MULTILINE : 0))) ) {
1863 /* we may be pointing at the wrong string */
1864 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1865 s = strbeg + (s - SvPVX_const(sv));
1866 DEBUG_EXECUTE_r( did_match = 1 );
1867 if (HOPc(s, -back_max) > last1) {
1868 last1 = HOPc(s, -back_min);
1869 s = HOPc(s, -back_max);
1872 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1874 last1 = HOPc(s, -back_min);
1878 while (s <= last1) {
1879 if (regtry(prog, s))
1885 while (s <= last1) {
1886 if (regtry(prog, s))
1892 DEBUG_EXECUTE_r(if (!did_match)
1893 PerlIO_printf(Perl_debug_log,
1894 "Did not find %s substr \"%s%.*s%s\"%s...\n",
1895 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1896 ? "anchored" : "floating"),
1898 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1900 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1904 else if ((c = prog->regstclass)) {
1906 I32 op = (U8)OP(prog->regstclass);
1907 /* don't bother with what can't match */
1908 if (PL_regkind[op] != EXACT && op != CANY)
1909 strend = HOPc(strend, -(minlen - 1));
1912 SV *prop = sv_newmortal();
1920 pv_uni_display(dsv0, (U8*)SvPVX_const(prop), SvCUR(prop), 60,
1921 UNI_DISPLAY_REGEX) :
1923 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1925 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1926 len1 = UTF ? SvCUR(dsv1) : strend - s;
1927 PerlIO_printf(Perl_debug_log,
1928 "Matching stclass \"%*.*s\" against \"%*.*s\"\n",
1932 if (find_byclass(prog, c, s, strend, 0))
1934 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1938 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
1943 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1944 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1945 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1947 if (flags & REXEC_SCREAM) {
1948 last = screaminstr(sv, float_real, s - strbeg,
1949 end_shift, &scream_pos, 1); /* last one */
1951 last = scream_olds; /* Only one occurrence. */
1952 /* we may be pointing at the wrong string */
1953 else if (RX_MATCH_COPIED(prog))
1954 s = strbeg + (s - SvPVX_const(sv));
1958 const char * const little = SvPV_const(float_real, len);
1960 if (SvTAIL(float_real)) {
1961 if (memEQ(strend - len + 1, little, len - 1))
1962 last = strend - len + 1;
1963 else if (!multiline)
1964 last = memEQ(strend - len, little, len)
1965 ? strend - len : NULL;
1971 last = rninstr(s, strend, little, little + len);
1973 last = strend; /* matching "$" */
1977 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1978 "%sCan't trim the tail, match fails (should not happen)%s\n",
1979 PL_colors[4], PL_colors[5]));
1980 goto phooey; /* Should not happen! */
1982 dontbother = strend - last + prog->float_min_offset;
1984 if (minlen && (dontbother < minlen))
1985 dontbother = minlen - 1;
1986 strend -= dontbother; /* this one's always in bytes! */
1987 /* We don't know much -- general case. */
1990 if (regtry(prog, s))
1999 if (regtry(prog, s))
2001 } while (s++ < strend);
2009 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2011 if (PL_reg_eval_set) {
2012 /* Preserve the current value of $^R */
2013 if (oreplsv != GvSV(PL_replgv))
2014 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2015 restored, the value remains
2017 restore_pos(aTHX_ prog);
2020 /* make sure $`, $&, $', and $digit will work later */
2021 if ( !(flags & REXEC_NOT_FIRST) ) {
2022 RX_MATCH_COPY_FREE(prog);
2023 if (flags & REXEC_COPY_STR) {
2024 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2025 #ifdef PERL_OLD_COPY_ON_WRITE
2027 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2029 PerlIO_printf(Perl_debug_log,
2030 "Copy on write: regexp capture, type %d\n",
2033 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2034 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2035 assert (SvPOKp(prog->saved_copy));
2039 RX_MATCH_COPIED_on(prog);
2040 s = savepvn(strbeg, i);
2046 prog->subbeg = strbeg;
2047 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2054 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2055 PL_colors[4], PL_colors[5]));
2056 if (PL_reg_eval_set)
2057 restore_pos(aTHX_ prog);
2062 - regtry - try match at specific point
2064 STATIC I32 /* 0 failure, 1 success */
2065 S_regtry(pTHX_ regexp *prog, char *startpos)
2072 GET_RE_DEBUG_FLAGS_DECL;
2075 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2077 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2080 PL_reg_eval_set = RS_init;
2081 DEBUG_EXECUTE_r(DEBUG_s(
2082 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2083 (IV)(PL_stack_sp - PL_stack_base));
2085 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2086 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2087 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2089 /* Apparently this is not needed, judging by wantarray. */
2090 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2091 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2094 /* Make $_ available to executed code. */
2095 if (PL_reg_sv != DEFSV) {
2100 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2101 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2102 /* prepare for quick setting of pos */
2103 #ifdef PERL_OLD_COPY_ON_WRITE
2105 sv_force_normal_flags(sv, 0);
2107 mg = sv_magicext(PL_reg_sv, (SV*)0, PERL_MAGIC_regex_global,
2108 &PL_vtbl_mglob, NULL, 0);
2112 PL_reg_oldpos = mg->mg_len;
2113 SAVEDESTRUCTOR_X(restore_pos, prog);
2115 if (!PL_reg_curpm) {
2116 Newxz(PL_reg_curpm, 1, PMOP);
2119 SV* repointer = newSViv(0);
2120 /* so we know which PL_regex_padav element is PL_reg_curpm */
2121 SvFLAGS(repointer) |= SVf_BREAK;
2122 av_push(PL_regex_padav,repointer);
2123 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2124 PL_regex_pad = AvARRAY(PL_regex_padav);
2128 PM_SETRE(PL_reg_curpm, prog);
2129 PL_reg_oldcurpm = PL_curpm;
2130 PL_curpm = PL_reg_curpm;
2131 if (RX_MATCH_COPIED(prog)) {
2132 /* Here is a serious problem: we cannot rewrite subbeg,
2133 since it may be needed if this match fails. Thus
2134 $` inside (?{}) could fail... */
2135 PL_reg_oldsaved = prog->subbeg;
2136 PL_reg_oldsavedlen = prog->sublen;
2137 #ifdef PERL_OLD_COPY_ON_WRITE
2138 PL_nrs = prog->saved_copy;
2140 RX_MATCH_COPIED_off(prog);
2143 PL_reg_oldsaved = NULL;
2144 prog->subbeg = PL_bostr;
2145 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2147 prog->startp[0] = startpos - PL_bostr;
2148 PL_reginput = startpos;
2149 PL_regstartp = prog->startp;
2150 PL_regendp = prog->endp;
2151 PL_reglastparen = &prog->lastparen;
2152 PL_reglastcloseparen = &prog->lastcloseparen;
2153 prog->lastparen = 0;
2154 prog->lastcloseparen = 0;
2156 DEBUG_EXECUTE_r(PL_reg_starttry = startpos);
2157 if (PL_reg_start_tmpl <= prog->nparens) {
2158 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2159 if(PL_reg_start_tmp)
2160 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2162 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2165 /* XXXX What this code is doing here?!!! There should be no need
2166 to do this again and again, PL_reglastparen should take care of
2169 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2170 * Actually, the code in regcppop() (which Ilya may be meaning by
2171 * PL_reglastparen), is not needed at all by the test suite
2172 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2173 * enough, for building DynaLoader, or otherwise this
2174 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2175 * will happen. Meanwhile, this code *is* needed for the
2176 * above-mentioned test suite tests to succeed. The common theme
2177 * on those tests seems to be returning null fields from matches.
2182 if (prog->nparens) {
2183 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2190 if (regmatch(prog, prog->program + 1)) {
2191 prog->endp[0] = PL_reginput - PL_bostr;
2194 REGCP_UNWIND(lastcp);
2198 #define RE_UNWIND_BRANCH 1
2199 #define RE_UNWIND_BRANCHJ 2
2203 typedef struct { /* XX: makes sense to enlarge it... */
2207 } re_unwind_generic_t;
2220 } re_unwind_branch_t;
2222 typedef union re_unwind_t {
2224 re_unwind_generic_t generic;
2225 re_unwind_branch_t branch;
2228 #define sayYES goto yes
2229 #define sayNO goto no
2230 #define sayNO_ANYOF goto no_anyof
2231 #define sayYES_FINAL goto yes_final
2232 #define sayYES_LOUD goto yes_loud
2233 #define sayNO_FINAL goto no_final
2234 #define sayNO_SILENT goto do_no
2235 #define saySAME(x) if (x) goto yes; else goto no
2237 #define POSCACHE_SUCCESS 0 /* caching success rather than failure */
2238 #define POSCACHE_SEEN 1 /* we know what we're caching */
2239 #define POSCACHE_START 2 /* the real cache: this bit maps to pos 0 */
2240 #define CACHEsayYES STMT_START { \
2241 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) { \
2242 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) \
2243 PL_reg_poscache[0] |= (1<<POSCACHE_SUCCESS) || (1<<POSCACHE_SEEN); \
2244 else if (!(PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2245 /* cache records failure, but this is success */ \
2247 PerlIO_printf(Perl_debug_log, \
2248 "%*s (remove success from failure cache)\n", \
2249 REPORT_CODE_OFF+PL_regindent*2, "") \
2251 PL_reg_poscache[st->u.whilem.cache_offset] &= ~(1<<st->u.whilem.cache_bit); \
2256 #define CACHEsayNO STMT_START { \
2257 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) { \
2258 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) \
2259 PL_reg_poscache[0] |= (1<<POSCACHE_SEEN); \
2260 else if ((PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2261 /* cache records success, but this is failure */ \
2263 PerlIO_printf(Perl_debug_log, \
2264 "%*s (remove failure from success cache)\n", \
2265 REPORT_CODE_OFF+PL_regindent*2, "") \
2267 PL_reg_poscache[st->u.whilem.cache_offset] &= ~(1<<st->u.whilem.cache_bit); \
2273 /* this is used to determine how far from the left messages like
2274 'failed...' are printed. Currently 29 makes these messages line
2275 up with the opcode they refer to. Earlier perls used 25 which
2276 left these messages outdented making reviewing a debug output
2279 #define REPORT_CODE_OFF 29
2282 /* Make sure there is a test for this +1 options in re_tests */
2283 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2285 /* grab a new slab and return the first slot in it */
2287 STATIC regmatch_state *
2290 regmatch_slab *s = PL_regmatch_slab->next;
2292 Newx(s, 1, regmatch_slab);
2293 s->prev = PL_regmatch_slab;
2295 PL_regmatch_slab->next = s;
2297 PL_regmatch_slab = s;
2298 return &s->states[0];
2301 /* simulate a recursive call to regmatch */
2303 #define REGMATCH(ns, where) \
2306 st->resume_state = resume_##where; \
2307 goto start_recurse; \
2308 resume_point_##where:
2311 - regmatch - main matching routine
2313 * Conceptually the strategy is simple: check to see whether the current
2314 * node matches, call self recursively to see whether the rest matches,
2315 * and then act accordingly. In practice we make some effort to avoid
2316 * recursion, in particular by going through "ordinary" nodes (that don't
2317 * need to know whether the rest of the match failed) by a loop instead of
2320 /* [lwall] I've hoisted the register declarations to the outer block in order to
2321 * maybe save a little bit of pushing and popping on the stack. It also takes
2322 * advantage of machines that use a register save mask on subroutine entry.
2324 * This function used to be heavily recursive, but since this had the
2325 * effect of blowing the CPU stack on complex regexes, it has been
2326 * restructured to be iterative, and to save state onto the heap rather
2327 * than the stack. Essentially whereever regmatch() used to be called, it
2328 * pushes the current state, notes where to return, then jumps back into
2331 * Originally the structure of this function used to look something like
2336 while (scan != NULL) {
2337 a++; // do stuff with a and b
2343 if (regmatch(...)) // recurse
2353 * Now it looks something like this:
2361 regmatch_state *st = new();
2363 st->a++; // do stuff with a and b
2365 while (scan != NULL) {
2373 st->resume_state = resume_FOO;
2374 goto start_recurse; // recurse
2383 st = new(); push a new state
2384 st->a = 1; st->b = 2;
2391 switch (resume_state) {
2393 goto resume_point_FOO;
2400 * WARNING: this means that any line in this function that contains a
2401 * REGMATCH() or TRYPAREN() is actually simulating a recursive call to
2402 * regmatch() using gotos instead. Thus the values of any local variables
2403 * not saved in the regmatch_state structure will have been lost when
2404 * execution resumes on the next line .
2406 * States (ie the st pointer) are allocated in slabs of about 4K in size.
2407 * PL_regmatch_state always points to the currently active state, and
2408 * PL_regmatch_slab points to the slab currently containing PL_regmatch_state.
2409 * The first time regmatch is called, the first slab is allocated, and is
2410 * never freed until interpreter desctruction. When the slab is full,
2411 * a new one is allocated chained to the end. At exit from regmatch, slabs
2412 * allocated since entry are freed.
2416 STATIC I32 /* 0 failure, 1 success */
2417 S_regmatch(pTHX_ regexp *rex, regnode *prog)
2420 register const bool do_utf8 = PL_reg_match_utf8;
2421 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2423 regmatch_slab *orig_slab;
2424 regmatch_state *orig_state;
2426 /* the current state. This is a cached copy of PL_regmatch_state */
2427 register regmatch_state *st;
2429 /* cache heavy used fields of st in registers */
2430 register regnode *scan;
2431 register regnode *next;
2432 register I32 n = 0; /* initialize to shut up compiler warning */
2433 register char *locinput = PL_reginput;
2435 /* these variables are NOT saved during a recusive RFEGMATCH: */
2436 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2437 bool result; /* return value of S_regmatch */
2438 regnode *inner; /* Next node in internal branch. */
2439 int depth = 0; /* depth of recursion */
2442 SV *re_debug_flags = NULL;
2447 /* on first ever call to regmatch, allocate first slab */
2448 if (!PL_regmatch_slab) {
2449 Newx(PL_regmatch_slab, 1, regmatch_slab);
2450 PL_regmatch_slab->prev = NULL;
2451 PL_regmatch_slab->next = NULL;
2452 PL_regmatch_state = &PL_regmatch_slab->states[0] - 1;
2455 /* remember current high-water mark for exit */
2456 /* XXX this should be done with SAVE* instead */
2457 orig_slab = PL_regmatch_slab;
2458 orig_state = PL_regmatch_state;
2460 /* grab next free state slot */
2461 st = ++PL_regmatch_state;
2462 if (st > &(PL_regmatch_slab->states[PERL_REGMATCH_SLAB_SLOTS-1]))
2463 st = PL_regmatch_state = S_push_slab(aTHX);
2470 /* Note that nextchr is a byte even in UTF */
2471 nextchr = UCHARAT(locinput);
2473 while (scan != NULL) {
2476 SV * const prop = sv_newmortal();
2477 const int docolor = *PL_colors[0];
2478 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2479 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2480 /* The part of the string before starttry has one color
2481 (pref0_len chars), between starttry and current
2482 position another one (pref_len - pref0_len chars),
2483 after the current position the third one.
2484 We assume that pref0_len <= pref_len, otherwise we
2485 decrease pref0_len. */
2486 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2487 ? (5 + taill) - l : locinput - PL_bostr;
2490 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2492 pref0_len = pref_len - (locinput - PL_reg_starttry);
2493 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2494 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2495 ? (5 + taill) - pref_len : PL_regeol - locinput);
2496 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2500 if (pref0_len > pref_len)
2501 pref0_len = pref_len;
2502 regprop(prop, scan);
2504 const char * const s0 =
2505 do_utf8 && OP(scan) != CANY ?
2506 pv_uni_display(PERL_DEBUG_PAD(0), (U8*)(locinput - pref_len),
2507 pref0_len, 60, UNI_DISPLAY_REGEX) :
2508 locinput - pref_len;
2509 const int len0 = do_utf8 ? strlen(s0) : pref0_len;
2510 const char * const s1 = do_utf8 && OP(scan) != CANY ?
2511 pv_uni_display(PERL_DEBUG_PAD(1),
2512 (U8*)(locinput - pref_len + pref0_len),
2513 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2514 locinput - pref_len + pref0_len;
2515 const int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2516 const char * const s2 = do_utf8 && OP(scan) != CANY ?
2517 pv_uni_display(PERL_DEBUG_PAD(2), (U8*)locinput,
2518 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2520 const int len2 = do_utf8 ? strlen(s2) : l;
2521 PerlIO_printf(Perl_debug_log,
2522 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2523 (IV)(locinput - PL_bostr),
2530 (docolor ? "" : "> <"),
2534 15 - l - pref_len + 1,
2536 (IV)(scan - rex->program), PL_regindent*2, "",
2541 next = scan + NEXT_OFF(scan);
2547 if (locinput == PL_bostr)
2549 /* regtill = regbol; */
2554 if (locinput == PL_bostr ||
2555 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2561 if (locinput == PL_bostr)
2565 if (locinput == PL_reg_ganch)
2571 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2576 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2578 if (PL_regeol - locinput > 1)
2582 if (PL_regeol != locinput)
2586 if (!nextchr && locinput >= PL_regeol)
2589 locinput += PL_utf8skip[nextchr];
2590 if (locinput > PL_regeol)
2592 nextchr = UCHARAT(locinput);
2595 nextchr = UCHARAT(++locinput);
2598 if (!nextchr && locinput >= PL_regeol)
2600 nextchr = UCHARAT(++locinput);
2603 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2606 locinput += PL_utf8skip[nextchr];
2607 if (locinput > PL_regeol)
2609 nextchr = UCHARAT(locinput);
2612 nextchr = UCHARAT(++locinput);
2618 traverse the TRIE keeping track of all accepting states
2619 we transition through until we get to a failing node.
2627 U8 *uc = ( U8* )locinput;
2634 U8 *uscan = (U8*)NULL;
2636 SV *sv_accept_buff = NULL;
2637 const enum { trie_plain, trie_utf8, trie_uft8_fold }
2638 trie_type = do_utf8 ?
2639 (OP(scan) == TRIE ? trie_utf8 : trie_uft8_fold)
2642 /* what trie are we using right now */
2644 = (reg_trie_data*)PL_reg_re->data->data[ ARG( scan ) ];
2645 st->u.trie.accepted = 0; /* how many accepting states we have seen */
2648 while ( state && uc <= (U8*)PL_regeol ) {
2650 if (trie->states[ state ].wordnum) {
2651 if (!st->u.trie.accepted ) {
2654 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
2655 sv_accept_buff=newSV(bufflen *
2656 sizeof(reg_trie_accepted) - 1);
2657 SvCUR_set(sv_accept_buff,
2658 sizeof(reg_trie_accepted));
2659 SvPOK_on(sv_accept_buff);
2660 sv_2mortal(sv_accept_buff);
2661 st->u.trie.accept_buff =
2662 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
2665 if (st->u.trie.accepted >= bufflen) {
2667 st->u.trie.accept_buff =(reg_trie_accepted*)
2668 SvGROW(sv_accept_buff,
2669 bufflen * sizeof(reg_trie_accepted));
2671 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
2672 + sizeof(reg_trie_accepted));
2674 st->u.trie.accept_buff[st->u.trie.accepted].wordnum = trie->states[state].wordnum;
2675 st->u.trie.accept_buff[st->u.trie.accepted].endpos = uc;
2676 ++st->u.trie.accepted;
2679 base = trie->states[ state ].trans.base;
2681 DEBUG_TRIE_EXECUTE_r(
2682 PerlIO_printf( Perl_debug_log,
2683 "%*s %sState: %4"UVxf", Base: %4"UVxf", Accepted: %4"UVxf" ",
2684 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
2685 (UV)state, (UV)base, (UV)st->u.trie.accepted );
2689 switch (trie_type) {
2690 case trie_uft8_fold:
2692 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags );
2697 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2698 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags );
2699 uvc = to_uni_fold( uvc, foldbuf, &foldlen );
2700 foldlen -= UNISKIP( uvc );
2701 uscan = foldbuf + UNISKIP( uvc );
2705 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN,
2714 charid = trie->charmap[ uvc ];
2718 if (trie->widecharmap) {
2719 SV** svpp = (SV**)NULL;
2720 svpp = hv_fetch(trie->widecharmap,
2721 (char*)&uvc, sizeof(UV), 0);
2723 charid = (U16)SvIV(*svpp);
2728 (base + charid > trie->uniquecharcount )
2729 && (base + charid - 1 - trie->uniquecharcount
2731 && trie->trans[base + charid - 1 -
2732 trie->uniquecharcount].check == state)
2734 state = trie->trans[base + charid - 1 -
2735 trie->uniquecharcount ].next;
2746 DEBUG_TRIE_EXECUTE_r(
2747 PerlIO_printf( Perl_debug_log,
2748 "Charid:%3x CV:%4"UVxf" After State: %4"UVxf"%s\n",
2749 charid, uvc, (UV)state, PL_colors[5] );
2752 if (!st->u.trie.accepted )
2756 There was at least one accepting state that we
2757 transitioned through. Presumably the number of accepting
2758 states is going to be low, typically one or two. So we
2759 simply scan through to find the one with lowest wordnum.
2760 Once we find it, we swap the last state into its place
2761 and decrement the size. We then try to match the rest of
2762 the pattern at the point where the word ends, if we
2763 succeed then we end the loop, otherwise the loop
2764 eventually terminates once all of the accepting states
2768 if ( st->u.trie.accepted == 1 ) {
2770 SV **tmp = av_fetch( trie->words, st->u.trie.accept_buff[ 0 ].wordnum-1, 0 );
2771 PerlIO_printf( Perl_debug_log,
2772 "%*s %sonly one match : #%d <%s>%s\n",
2773 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
2774 st->u.trie.accept_buff[ 0 ].wordnum,
2775 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",
2778 PL_reginput = (char *)st->u.trie.accept_buff[ 0 ].endpos;
2779 /* in this case we free tmps/leave before we call regmatch
2780 as we wont be using accept_buff again. */
2783 REGMATCH(scan + NEXT_OFF(scan), TRIE1);
2784 /*** all unsaved local vars undefined at this point */
2787 PerlIO_printf( Perl_debug_log,"%*s %sgot %"IVdf" possible matches%s\n",
2788 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4], (IV)st->u.trie.accepted,
2791 while ( !result && st->u.trie.accepted-- ) {
2794 for( cur = 1 ; cur <= st->u.trie.accepted ; cur++ ) {
2795 DEBUG_TRIE_EXECUTE_r(
2796 PerlIO_printf( Perl_debug_log,
2797 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
2798 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
2799 (IV)best, st->u.trie.accept_buff[ best ].wordnum, (IV)cur,
2800 st->u.trie.accept_buff[ cur ].wordnum, PL_colors[5] );
2803 if (st->u.trie.accept_buff[cur].wordnum <
2804 st->u.trie.accept_buff[best].wordnum)
2808 reg_trie_data * const trie = (reg_trie_data*)
2809 PL_reg_re->data->data[ARG(scan)];
2810 SV ** const tmp = av_fetch( trie->words, st->u.trie.accept_buff[ best ].wordnum - 1, 0 );
2811 PerlIO_printf( Perl_debug_log, "%*s %strying alternation #%d <%s> at 0x%p%s\n",
2812 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
2813 st->u.trie.accept_buff[best].wordnum,
2814 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",scan,
2817 if ( best<st->u.trie.accepted ) {
2818 reg_trie_accepted tmp = st->u.trie.accept_buff[ best ];
2819 st->u.trie.accept_buff[ best ] = st->u.trie.accept_buff[ st->u.trie.accepted ];
2820 st->u.trie.accept_buff[ st->u.trie.accepted ] = tmp;
2821 best = st->u.trie.accepted;
2823 PL_reginput = (char *)st->u.trie.accept_buff[ best ].endpos;
2826 as far as I can tell we only need the SAVETMPS/FREETMPS
2827 for re's with EVAL in them but I'm leaving them in for
2828 all until I can be sure.
2831 REGMATCH(scan + NEXT_OFF(scan), TRIE2);
2832 /*** all unsaved local vars undefined at this point */
2845 /* unreached codepoint */
2847 char *s = STRING(scan);
2848 st->ln = STR_LEN(scan);
2849 if (do_utf8 != UTF) {
2850 /* The target and the pattern have differing utf8ness. */
2852 const char *e = s + st->ln;
2855 /* The target is utf8, the pattern is not utf8. */
2860 if (NATIVE_TO_UNI(*(U8*)s) !=
2861 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
2869 /* The target is not utf8, the pattern is utf8. */
2874 if (NATIVE_TO_UNI(*((U8*)l)) !=
2875 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
2883 nextchr = UCHARAT(locinput);
2886 /* The target and the pattern have the same utf8ness. */
2887 /* Inline the first character, for speed. */
2888 if (UCHARAT(s) != nextchr)
2890 if (PL_regeol - locinput < st->ln)
2892 if (st->ln > 1 && memNE(s, locinput, st->ln))
2895 nextchr = UCHARAT(locinput);
2899 PL_reg_flags |= RF_tainted;
2902 char *s = STRING(scan);
2903 st->ln = STR_LEN(scan);
2905 if (do_utf8 || UTF) {
2906 /* Either target or the pattern are utf8. */
2908 char *e = PL_regeol;
2910 if (ibcmp_utf8(s, 0, st->ln, (bool)UTF,
2911 l, &e, 0, do_utf8)) {
2912 /* One more case for the sharp s:
2913 * pack("U0U*", 0xDF) =~ /ss/i,
2914 * the 0xC3 0x9F are the UTF-8
2915 * byte sequence for the U+00DF. */
2917 toLOWER(s[0]) == 's' &&
2919 toLOWER(s[1]) == 's' &&
2926 nextchr = UCHARAT(locinput);
2930 /* Neither the target and the pattern are utf8. */
2932 /* Inline the first character, for speed. */
2933 if (UCHARAT(s) != nextchr &&
2934 UCHARAT(s) != ((OP(scan) == EXACTF)
2935 ? PL_fold : PL_fold_locale)[nextchr])
2937 if (PL_regeol - locinput < st->ln)
2939 if (st->ln > 1 && (OP(scan) == EXACTF
2940 ? ibcmp(s, locinput, st->ln)
2941 : ibcmp_locale(s, locinput, st->ln)))
2944 nextchr = UCHARAT(locinput);
2949 STRLEN inclasslen = PL_regeol - locinput;
2951 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2953 if (locinput >= PL_regeol)
2955 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2956 nextchr = UCHARAT(locinput);
2961 nextchr = UCHARAT(locinput);
2962 if (!REGINCLASS(scan, (U8*)locinput))
2964 if (!nextchr && locinput >= PL_regeol)
2966 nextchr = UCHARAT(++locinput);
2970 /* If we might have the case of the German sharp s
2971 * in a casefolding Unicode character class. */
2973 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2974 locinput += SHARP_S_SKIP;
2975 nextchr = UCHARAT(locinput);
2981 PL_reg_flags |= RF_tainted;
2987 LOAD_UTF8_CHARCLASS_ALNUM();
2988 if (!(OP(scan) == ALNUM
2989 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2990 : isALNUM_LC_utf8((U8*)locinput)))
2994 locinput += PL_utf8skip[nextchr];
2995 nextchr = UCHARAT(locinput);
2998 if (!(OP(scan) == ALNUM
2999 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
3001 nextchr = UCHARAT(++locinput);
3004 PL_reg_flags |= RF_tainted;
3007 if (!nextchr && locinput >= PL_regeol)
3010 LOAD_UTF8_CHARCLASS_ALNUM();
3011 if (OP(scan) == NALNUM
3012 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3013 : isALNUM_LC_utf8((U8*)locinput))
3017 locinput += PL_utf8skip[nextchr];
3018 nextchr = UCHARAT(locinput);
3021 if (OP(scan) == NALNUM
3022 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
3024 nextchr = UCHARAT(++locinput);
3028 PL_reg_flags |= RF_tainted;
3032 /* was last char in word? */
3034 if (locinput == PL_bostr)
3037 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3039 st->ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3041 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3042 st->ln = isALNUM_uni(st->ln);
3043 LOAD_UTF8_CHARCLASS_ALNUM();
3044 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3047 st->ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(st->ln));
3048 n = isALNUM_LC_utf8((U8*)locinput);
3052 st->ln = (locinput != PL_bostr) ?
3053 UCHARAT(locinput - 1) : '\n';
3054 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3055 st->ln = isALNUM(st->ln);
3056 n = isALNUM(nextchr);
3059 st->ln = isALNUM_LC(st->ln);
3060 n = isALNUM_LC(nextchr);
3063 if (((!st->ln) == (!n)) == (OP(scan) == BOUND ||
3064 OP(scan) == BOUNDL))
3068 PL_reg_flags |= RF_tainted;
3074 if (UTF8_IS_CONTINUED(nextchr)) {
3075 LOAD_UTF8_CHARCLASS_SPACE();
3076 if (!(OP(scan) == SPACE
3077 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3078 : isSPACE_LC_utf8((U8*)locinput)))
3082 locinput += PL_utf8skip[nextchr];
3083 nextchr = UCHARAT(locinput);
3086 if (!(OP(scan) == SPACE
3087 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3089 nextchr = UCHARAT(++locinput);
3092 if (!(OP(scan) == SPACE
3093 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3095 nextchr = UCHARAT(++locinput);
3099 PL_reg_flags |= RF_tainted;
3102 if (!nextchr && locinput >= PL_regeol)
3105 LOAD_UTF8_CHARCLASS_SPACE();
3106 if (OP(scan) == NSPACE
3107 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3108 : isSPACE_LC_utf8((U8*)locinput))
3112 locinput += PL_utf8skip[nextchr];
3113 nextchr = UCHARAT(locinput);
3116 if (OP(scan) == NSPACE
3117 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
3119 nextchr = UCHARAT(++locinput);
3122 PL_reg_flags |= RF_tainted;
3128 LOAD_UTF8_CHARCLASS_DIGIT();
3129 if (!(OP(scan) == DIGIT
3130 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3131 : isDIGIT_LC_utf8((U8*)locinput)))
3135 locinput += PL_utf8skip[nextchr];
3136 nextchr = UCHARAT(locinput);
3139 if (!(OP(scan) == DIGIT
3140 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
3142 nextchr = UCHARAT(++locinput);
3145 PL_reg_flags |= RF_tainted;
3148 if (!nextchr && locinput >= PL_regeol)
3151 LOAD_UTF8_CHARCLASS_DIGIT();
3152 if (OP(scan) == NDIGIT
3153 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3154 : isDIGIT_LC_utf8((U8*)locinput))
3158 locinput += PL_utf8skip[nextchr];
3159 nextchr = UCHARAT(locinput);
3162 if (OP(scan) == NDIGIT
3163 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
3165 nextchr = UCHARAT(++locinput);
3168 if (locinput >= PL_regeol)
3171 LOAD_UTF8_CHARCLASS_MARK();
3172 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3174 locinput += PL_utf8skip[nextchr];
3175 while (locinput < PL_regeol &&
3176 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3177 locinput += UTF8SKIP(locinput);
3178 if (locinput > PL_regeol)
3183 nextchr = UCHARAT(locinput);
3186 PL_reg_flags |= RF_tainted;
3191 n = ARG(scan); /* which paren pair */
3192 st->ln = PL_regstartp[n];
3193 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3194 if ((I32)*PL_reglastparen < n || st->ln == -1)
3195 sayNO; /* Do not match unless seen CLOSEn. */
3196 if (st->ln == PL_regendp[n])
3199 s = PL_bostr + st->ln;
3200 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
3202 const char *e = PL_bostr + PL_regendp[n];
3204 * Note that we can't do the "other character" lookup trick as
3205 * in the 8-bit case (no pun intended) because in Unicode we
3206 * have to map both upper and title case to lower case.
3208 if (OP(scan) == REFF) {
3210 STRLEN ulen1, ulen2;
3211 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3212 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3216 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3217 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3218 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3225 nextchr = UCHARAT(locinput);
3229 /* Inline the first character, for speed. */
3230 if (UCHARAT(s) != nextchr &&
3232 (UCHARAT(s) != ((OP(scan) == REFF
3233 ? PL_fold : PL_fold_locale)[nextchr]))))
3235 st->ln = PL_regendp[n] - st->ln;
3236 if (locinput + st->ln > PL_regeol)
3238 if (st->ln > 1 && (OP(scan) == REF
3239 ? memNE(s, locinput, st->ln)
3241 ? ibcmp(s, locinput, st->ln)
3242 : ibcmp_locale(s, locinput, st->ln))))
3245 nextchr = UCHARAT(locinput);
3258 /* execute the code in the {...} */
3260 SV ** const before = SP;
3261 OP_4tree * const oop = PL_op;
3262 COP * const ocurcop = PL_curcop;
3264 struct regexp * const oreg = PL_reg_re;
3267 PL_op = (OP_4tree*)PL_reg_re->data->data[n];
3268 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3269 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_reg_re->data->data[n + 2]);
3270 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3272 CALLRUNOPS(aTHX); /* Scalar context. */
3275 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3282 PAD_RESTORE_LOCAL(old_comppad);
3283 PL_curcop = ocurcop;
3287 sv_setsv(save_scalar(PL_replgv), ret);
3291 if (st->logical == 2) { /* Postponed subexpression: /(??{...})/ */
3297 /* extract RE object from returned value; compiling if
3302 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
3303 mg = mg_find(sv, PERL_MAGIC_qr);
3304 else if (SvSMAGICAL(ret)) {
3305 if (SvGMAGICAL(ret))
3306 sv_unmagic(ret, PERL_MAGIC_qr);
3308 mg = mg_find(ret, PERL_MAGIC_qr);
3312 re = (regexp *)mg->mg_obj;
3313 (void)ReREFCNT_inc(re);
3317 const char * const t = SvPV_const(ret, len);
3319 const I32 osize = PL_regsize;
3322 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
3323 re = CALLREGCOMP(aTHX_ (char*)t, (char*)t + len, &pm);
3325 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3327 sv_magic(ret,(SV*)ReREFCNT_inc(re),
3333 PerlIO_printf(Perl_debug_log,
3334 "Entering embedded \"%s%.60s%s%s\"\n",
3338 (strlen(re->precomp) > 60 ? "..." : ""))
3341 state.prev = PL_reg_call_cc;
3343 state.re = PL_reg_re;
3347 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
3348 REGCP_SET(st->u.eval.lastcp);
3350 state.ss = PL_savestack_ix;
3351 *PL_reglastparen = 0;
3352 *PL_reglastcloseparen = 0;
3353 PL_reg_call_cc = &state;
3354 PL_reginput = locinput;
3355 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
3356 ((re->reganch & ROPT_UTF8) != 0);
3357 if (toggleutf) PL_reg_flags ^= RF_utf8;
3359 /* XXXX This is too dramatic a measure... */
3362 /* XXX the only recursion left in regmatch() */
3363 if (regmatch(re, re->program + 1)) {
3364 /* Even though we succeeded, we need to restore
3365 global variables, since we may be wrapped inside
3366 SUSPEND, thus the match may be not finished yet. */
3368 /* XXXX Do this only if SUSPENDed? */
3369 PL_reg_call_cc = state.prev;
3371 PL_reg_re = state.re;
3373 if (toggleutf) PL_reg_flags ^= RF_utf8;
3375 /* XXXX This is too dramatic a measure... */
3378 /* These are needed even if not SUSPEND. */
3380 regcpblow(st->u.eval.cp);
3384 REGCP_UNWIND(st->u.eval.lastcp);
3386 PL_reg_call_cc = state.prev;
3388 PL_reg_re = state.re;
3389 if (toggleutf) PL_reg_flags ^= RF_utf8;
3391 /* XXXX This is too dramatic a measure... */
3398 /* /(?(?{...})X|Y)/ */
3399 st->sw = SvTRUE(ret);
3404 n = ARG(scan); /* which paren pair */
3405 PL_reg_start_tmp[n] = locinput;
3410 n = ARG(scan); /* which paren pair */
3411 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3412 PL_regendp[n] = locinput - PL_bostr;
3413 if (n > (I32)*PL_reglastparen)
3414 *PL_reglastparen = n;
3415 *PL_reglastcloseparen = n;
3418 n = ARG(scan); /* which paren pair */
3419 st->sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
3422 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3424 next = NEXTOPER(NEXTOPER(scan));
3426 next = scan + ARG(scan);
3427 if (OP(next) == IFTHEN) /* Fake one. */
3428 next = NEXTOPER(NEXTOPER(next));
3432 st->logical = scan->flags;
3434 /*******************************************************************
3435 cc points to the regmatch_state associated with the most recent CURLYX.
3436 This struct contains info about the innermost (...)* loop (an
3437 "infoblock"), and a pointer to the next outer cc.
3439 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3441 1) After matching Y, regnode for CURLYX is processed;
3443 2) This regnode populates cc, and calls regmatch() recursively
3444 with the starting point at WHILEM node;
3446 3) Each hit of WHILEM node tries to match A and Z (in the order
3447 depending on the current iteration, min/max of {min,max} and
3448 greediness). The information about where are nodes for "A"
3449 and "Z" is read from cc, as is info on how many times "A"
3450 was already matched, and greediness.
3452 4) After A matches, the same WHILEM node is hit again.
3454 5) Each time WHILEM is hit, cc is the infoblock created by CURLYX
3455 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3456 resets cc, since this Y(A)*Z can be a part of some other loop:
3457 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3458 of the external loop.
3460 Currently present infoblocks form a tree with a stem formed by st->cc
3461 and whatever it mentions via ->next, and additional attached trees
3462 corresponding to temporarily unset infoblocks as in "5" above.
3464 In the following picture, infoblocks for outer loop of
3465 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3466 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3467 infoblocks are drawn below the "reset" infoblock.
3469 In fact in the picture below we do not show failed matches for Z and T
3470 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3471 more obvious *why* one needs to *temporary* unset infoblocks.]
3473 Matched REx position InfoBlocks Comment
3477 Y A)*?Z)*?T x <- O <- I
3478 YA )*?Z)*?T x <- O <- I
3479 YA A)*?Z)*?T x <- O <- I
3480 YAA )*?Z)*?T x <- O <- I
3481 YAA Z)*?T x <- O # Temporary unset I
3484 YAAZ Y(A)*?Z)*?T x <- O
3487 YAAZY (A)*?Z)*?T x <- O
3490 YAAZY A)*?Z)*?T x <- O <- I
3493 YAAZYA )*?Z)*?T x <- O <- I
3496 YAAZYA Z)*?T x <- O # Temporary unset I
3502 YAAZYAZ T x # Temporary unset O
3509 *******************************************************************/
3512 /* No need to save/restore up to this paren */
3513 I32 parenfloor = scan->flags;
3515 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3517 /* XXXX Probably it is better to teach regpush to support
3518 parenfloor > PL_regsize... */
3519 if (parenfloor > (I32)*PL_reglastparen)
3520 parenfloor = *PL_reglastparen; /* Pessimization... */
3522 st->u.curlyx.cp = PL_savestack_ix;
3523 st->u.curlyx.outercc = st->cc;
3525 /* these fields contain the state of the current curly.
3526 * they are accessed by subsequent WHILEMs;
3527 * cur and lastloc are also updated by WHILEM */
3528 st->u.curlyx.parenfloor = parenfloor;
3529 st->u.curlyx.cur = -1; /* this will be updated by WHILEM */
3530 st->u.curlyx.min = ARG1(scan);
3531 st->u.curlyx.max = ARG2(scan);
3532 st->u.curlyx.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3533 st->u.curlyx.lastloc = 0;
3534 /* st->next and st->minmod are also read by WHILEM */
3536 PL_reginput = locinput;
3537 REGMATCH(PREVOPER(next), CURLYX); /* start on the WHILEM */
3538 /*** all unsaved local vars undefined at this point */
3539 regcpblow(st->u.curlyx.cp);
3540 st->cc = st->u.curlyx.outercc;
3546 * This is really hard to understand, because after we match
3547 * what we're trying to match, we must make sure the rest of
3548 * the REx is going to match for sure, and to do that we have
3549 * to go back UP the parse tree by recursing ever deeper. And
3550 * if it fails, we have to reset our parent's current state
3551 * that we can try again after backing off.
3554 st->u.whilem.lastloc = st->cc->u.curlyx.lastloc; /* Detection of 0-len. */
3555 st->u.whilem.cache_offset = 0;
3556 st->u.whilem.cache_bit = 0;
3558 n = st->cc->u.curlyx.cur + 1; /* how many we know we matched */
3559 PL_reginput = locinput;
3562 PerlIO_printf(Perl_debug_log,
3563 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3564 REPORT_CODE_OFF+PL_regindent*2, "",
3565 (long)n, (long)st->cc->u.curlyx.min,
3566 (long)st->cc->u.curlyx.max, PTR2UV(st->cc))
3569 /* If degenerate scan matches "", assume scan done. */
3571 if (locinput == st->cc->u.curlyx.lastloc && n >= st->cc->u.curlyx.min) {
3572 st->u.whilem.savecc = st->cc;
3573 st->cc = st->cc->u.curlyx.outercc;
3575 st->ln = st->cc->u.curlyx.cur;
3577 PerlIO_printf(Perl_debug_log,
3578 "%*s empty match detected, try continuation...\n",
3579 REPORT_CODE_OFF+PL_regindent*2, "")
3581 REGMATCH(st->u.whilem.savecc->next, WHILEM1);
3582 /*** all unsaved local vars undefined at this point */
3583 st->cc = st->u.whilem.savecc;
3586 if (st->cc->u.curlyx.outercc)
3587 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3591 /* First just match a string of min scans. */
3593 if (n < st->cc->u.curlyx.min) {
3594 st->cc->u.curlyx.cur = n;
3595 st->cc->u.curlyx.lastloc = locinput;
3596 REGMATCH(st->cc->u.curlyx.scan, WHILEM2);
3597 /*** all unsaved local vars undefined at this point */
3600 st->cc->u.curlyx.cur = n - 1;
3601 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3606 /* Check whether we already were at this position.
3607 Postpone detection until we know the match is not
3608 *that* much linear. */
3609 if (!PL_reg_maxiter) {
3610 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3611 PL_reg_leftiter = PL_reg_maxiter;
3613 if (PL_reg_leftiter-- == 0) {
3614 const I32 size = (PL_reg_maxiter + 7 + POSCACHE_START)/8;
3615 if (PL_reg_poscache) {
3616 if ((I32)PL_reg_poscache_size < size) {
3617 Renew(PL_reg_poscache, size, char);
3618 PL_reg_poscache_size = size;
3620 Zero(PL_reg_poscache, size, char);
3623 PL_reg_poscache_size = size;
3624 Newxz(PL_reg_poscache, size, char);
3627 PerlIO_printf(Perl_debug_log,
3628 "%sDetected a super-linear match, switching on caching%s...\n",
3629 PL_colors[4], PL_colors[5])
3632 if (PL_reg_leftiter < 0) {
3633 st->u.whilem.cache_offset = locinput - PL_bostr;
3635 st->u.whilem.cache_offset = (scan->flags & 0xf) - 1 + POSCACHE_START
3636 + st->u.whilem.cache_offset * (scan->flags>>4);
3637 st->u.whilem.cache_bit = st->u.whilem.cache_offset % 8;
3638 st->u.whilem.cache_offset /= 8;
3639 if (PL_reg_poscache[st->u.whilem.cache_offset] & (1<<st->u.whilem.cache_bit)) {
3641 PerlIO_printf(Perl_debug_log,
3642 "%*s already tried at this position...\n",
3643 REPORT_CODE_OFF+PL_regindent*2, "")
3645 if (PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))
3646 /* cache records success */
3649 /* cache records failure */
3652 PL_reg_poscache[st->u.whilem.cache_offset] |= (1<<st->u.whilem.cache_bit);
3656 /* Prefer next over scan for minimal matching. */
3658 if (st->cc->minmod) {
3659 st->u.whilem.savecc = st->cc;
3660 st->cc = st->cc->u.curlyx.outercc;
3662 st->ln = st->cc->u.curlyx.cur;
3663 st->u.whilem.cp = regcppush(st->u.whilem.savecc->u.curlyx.parenfloor);
3664 REGCP_SET(st->u.whilem.lastcp);
3665 REGMATCH(st->u.whilem.savecc->next, WHILEM3);
3666 /*** all unsaved local vars undefined at this point */
3667 st->cc = st->u.whilem.savecc;
3669 regcpblow(st->u.whilem.cp);
3670 CACHEsayYES; /* All done. */
3672 REGCP_UNWIND(st->u.whilem.lastcp);
3674 if (st->cc->u.curlyx.outercc)
3675 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3677 if (n >= st->cc->u.curlyx.max) { /* Maximum greed exceeded? */
3678 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3679 && !(PL_reg_flags & RF_warned)) {
3680 PL_reg_flags |= RF_warned;
3681 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3682 "Complex regular subexpression recursion",
3689 PerlIO_printf(Perl_debug_log,
3690 "%*s trying longer...\n",
3691 REPORT_CODE_OFF+PL_regindent*2, "")
3693 /* Try scanning more and see if it helps. */
3694 PL_reginput = locinput;
3695 st->cc->u.curlyx.cur = n;
3696 st->cc->u.curlyx.lastloc = locinput;
3697 st->u.whilem.cp = regcppush(st->cc->u.curlyx.parenfloor);
3698 REGCP_SET(st->u.whilem.lastcp);
3699 REGMATCH(st->cc->u.curlyx.scan, WHILEM4);
3700 /*** all unsaved local vars undefined at this point */
3702 regcpblow(st->u.whilem.cp);
3705 REGCP_UNWIND(st->u.whilem.lastcp);
3707 st->cc->u.curlyx.cur = n - 1;
3708 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3712 /* Prefer scan over next for maximal matching. */
3714 if (n < st->cc->u.curlyx.max) { /* More greed allowed? */
3715 st->u.whilem.cp = regcppush(st->cc->u.curlyx.parenfloor);
3716 st->cc->u.curlyx.cur = n;
3717 st->cc->u.curlyx.lastloc = locinput;
3718 REGCP_SET(st->u.whilem.lastcp);
3719 REGMATCH(st->cc->u.curlyx.scan, WHILEM5);
3720 /*** all unsaved local vars undefined at this point */
3722 regcpblow(st->u.whilem.cp);
3725 REGCP_UNWIND(st->u.whilem.lastcp);
3726 regcppop(rex); /* Restore some previous $<digit>s? */
3727 PL_reginput = locinput;
3729 PerlIO_printf(Perl_debug_log,
3730 "%*s failed, try continuation...\n",
3731 REPORT_CODE_OFF+PL_regindent*2, "")
3734 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3735 && !(PL_reg_flags & RF_warned)) {
3736 PL_reg_flags |= RF_warned;
3737 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3738 "Complex regular subexpression recursion",
3742 /* Failed deeper matches of scan, so see if this one works. */
3743 st->u.whilem.savecc = st->cc;
3744 st->cc = st->cc->u.curlyx.outercc;
3746 st->ln = st->cc->u.curlyx.cur;
3747 REGMATCH(st->u.whilem.savecc->next, WHILEM6);
3748 /*** all unsaved local vars undefined at this point */
3749 st->cc = st->u.whilem.savecc;
3752 if (st->cc->u.curlyx.outercc)
3753 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3754 st->cc->u.curlyx.cur = n - 1;
3755 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3760 next = scan + ARG(scan);
3763 inner = NEXTOPER(NEXTOPER(scan));
3766 inner = NEXTOPER(scan);
3771 if (OP(next) != type) /* No choice. */
3772 next = inner; /* Avoid recursion. */
3774 const I32 lastparen = *PL_reglastparen;
3775 /* Put unwinding data on stack */
3776 const I32 unwind1 = SSNEWt(1,re_unwind_branch_t);
3777 re_unwind_branch_t * const uw = SSPTRt(unwind1,re_unwind_branch_t);
3779 uw->prev = st->unwind;
3780 st->unwind = unwind1;
3781 uw->type = ((type == BRANCH)
3783 : RE_UNWIND_BRANCHJ);
3784 uw->lastparen = lastparen;
3786 uw->locinput = locinput;
3787 uw->nextchr = nextchr;
3789 uw->regindent = ++PL_regindent;
3792 REGCP_SET(uw->lastcp);
3794 /* Now go into the first branch */
3804 st->u.curlym.l = st->u.curlym.matches = 0;
3806 /* We suppose that the next guy does not need
3807 backtracking: in particular, it is of constant non-zero length,
3808 and has no parenths to influence future backrefs. */
3809 st->ln = ARG1(scan); /* min to match */
3810 n = ARG2(scan); /* max to match */
3811 st->u.curlym.paren = scan->flags;
3812 if (st->u.curlym.paren) {
3813 if (st->u.curlym.paren > PL_regsize)
3814 PL_regsize = st->u.curlym.paren;
3815 if (st->u.curlym.paren > (I32)*PL_reglastparen)
3816 *PL_reglastparen = st->u.curlym.paren;
3818 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3819 if (st->u.curlym.paren)
3820 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3821 PL_reginput = locinput;
3822 st->u.curlym.maxwanted = st->minmod ? st->ln : n;
3823 if (st->u.curlym.maxwanted) {
3824 while (PL_reginput < PL_regeol && st->u.curlym.matches < st->u.curlym.maxwanted) {
3825 REGMATCH(scan, CURLYM1);
3826 /*** all unsaved local vars undefined at this point */
3829 /* on first match, determine length, u.curlym.l */
3830 if (!st->u.curlym.matches++) {
3831 if (PL_reg_match_utf8) {
3833 while (s < PL_reginput) {
3839 st->u.curlym.l = PL_reginput - locinput;
3841 if (st->u.curlym.l == 0) {
3842 st->u.curlym.matches = st->u.curlym.maxwanted;
3846 locinput = PL_reginput;
3850 PL_reginput = locinput;
3854 if (st->ln && st->u.curlym.matches < st->ln)
3856 if (HAS_TEXT(next) || JUMPABLE(next)) {
3857 regnode *text_node = next;
3859 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3861 if (! HAS_TEXT(text_node)) st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3863 if (PL_regkind[(U8)OP(text_node)] == REF) {
3864 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3867 else { st->u.curlym.c1 = (U8)*STRING(text_node); }
3868 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3869 st->u.curlym.c2 = PL_fold[st->u.curlym.c1];
3870 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3871 st->u.curlym.c2 = PL_fold_locale[st->u.curlym.c1];
3873 st->u.curlym.c2 = st->u.curlym.c1;
3877 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3879 REGCP_SET(st->u.curlym.lastcp);
3880 while (n >= st->ln || (n == REG_INFTY && st->ln > 0)) { /* ln overflow ? */
3881 /* If it could work, try it. */
3882 if (st->u.curlym.c1 == -1000 ||
3883 UCHARAT(PL_reginput) == st->u.curlym.c1 ||
3884 UCHARAT(PL_reginput) == st->u.curlym.c2)
3886 if (st->u.curlym.paren) {
3888 PL_regstartp[st->u.curlym.paren] =
3889 HOPc(PL_reginput, -st->u.curlym.l) - PL_bostr;
3890 PL_regendp[st->u.curlym.paren] = PL_reginput - PL_bostr;
3893 PL_regendp[st->u.curlym.paren] = -1;
3895 REGMATCH(next, CURLYM2);
3896 /*** all unsaved local vars undefined at this point */
3899 REGCP_UNWIND(st->u.curlym.lastcp);
3901 /* Couldn't or didn't -- move forward. */
3902 PL_reginput = locinput;
3903 REGMATCH(scan, CURLYM3);
3904 /*** all unsaved local vars undefined at this point */
3907 locinput = PL_reginput;
3915 PerlIO_printf(Perl_debug_log,
3916 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3917 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3918 (IV) st->u.curlym.matches, (IV)st->u.curlym.l)
3920 if (st->u.curlym.matches >= st->ln) {
3921 if (HAS_TEXT(next) || JUMPABLE(next)) {
3922 regnode *text_node = next;
3924 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3926 if (! HAS_TEXT(text_node)) st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3928 if (PL_regkind[(U8)OP(text_node)] == REF) {
3929 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3932 else { st->u.curlym.c1 = (U8)*STRING(text_node); }
3934 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3935 st->u.curlym.c2 = PL_fold[st->u.curlym.c1];
3936 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3937 st->u.curlym.c2 = PL_fold_locale[st->u.curlym.c1];
3939 st->u.curlym.c2 = st->u.curlym.c1;
3943 st->u.curlym.c1 = st->u.curlym.c2 = -1000;
3946 REGCP_SET(st->u.curlym.lastcp);
3947 while (st->u.curlym.matches >= st->ln) {
3948 /* If it could work, try it. */
3949 if (st->u.curlym.c1 == -1000 ||
3950 UCHARAT(PL_reginput) == st->u.curlym.c1 ||
3951 UCHARAT(PL_reginput) == st->u.curlym.c2)
3954 PerlIO_printf(Perl_debug_log,
3955 "%*s trying tail with matches=%"IVdf"...\n",
3956 (int)(REPORT_CODE_OFF+PL_regindent*2),
3957 "", (IV)st->u.curlym.matches)
3959 if (st->u.curlym.paren) {
3960 if (st->u.curlym.matches) {
3961 PL_regstartp[st->u.curlym.paren]
3962 = HOPc(PL_reginput, -st->u.curlym.l) - PL_bostr;
3963 PL_regendp[st->u.curlym.paren] = PL_reginput - PL_bostr;
3966 PL_regendp[st->u.curlym.paren] = -1;
3968 REGMATCH(next, CURLYM4);
3969 /*** all unsaved local vars undefined at this point */
3972 REGCP_UNWIND(st->u.curlym.lastcp);
3974 /* Couldn't or didn't -- back up. */
3975 st->u.curlym.matches--;
3976 locinput = HOPc(locinput, -st->u.curlym.l);
3977 PL_reginput = locinput;
3985 st->u.plus.paren = scan->flags; /* Which paren to set */
3986 if (st->u.plus.paren > PL_regsize)
3987 PL_regsize = st->u.plus.paren;
3988 if (st->u.plus.paren > (I32)*PL_reglastparen)
3989 *PL_reglastparen = st->u.plus.paren;
3990 st->ln = ARG1(scan); /* min to match */
3991 n = ARG2(scan); /* max to match */
3992 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3995 st->u.plus.paren = 0;
3996 st->ln = ARG1(scan); /* min to match */
3997 n = ARG2(scan); /* max to match */
3998 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4003 scan = NEXTOPER(scan);
4004 st->u.plus.paren = 0;
4009 scan = NEXTOPER(scan);
4010 st->u.plus.paren = 0;
4013 * Lookahead to avoid useless match attempts
4014 * when we know what character comes next.
4018 * Used to only do .*x and .*?x, but now it allows
4019 * for )'s, ('s and (?{ ... })'s to be in the way
4020 * of the quantifier and the EXACT-like node. -- japhy
4023 if (HAS_TEXT(next) || JUMPABLE(next)) {
4025 regnode *text_node = next;
4027 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
4029 if (! HAS_TEXT(text_node)) st->u.plus.c1 = st->u.plus.c2 = -1000;
4031 if (PL_regkind[(U8)OP(text_node)] == REF) {
4032 st->u.plus.c1 = st->u.plus.c2 = -1000;
4033 goto assume_ok_easy;
4035 else { s = (U8*)STRING(text_node); }
4038 st->u.plus.c2 = st->u.plus.c1 = *s;
4039 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
4040 st->u.plus.c2 = PL_fold[st->u.plus.c1];
4041 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
4042 st->u.plus.c2 = PL_fold_locale[st->u.plus.c1];
4045 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
4046 STRLEN ulen1, ulen2;
4047 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4048 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4050 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4051 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4053 st->u.plus.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4055 st->u.plus.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4059 st->u.plus.c2 = st->u.plus.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4066 st->u.plus.c1 = st->u.plus.c2 = -1000;
4068 PL_reginput = locinput;
4071 if (st->ln && regrepeat(scan, st->ln) < st->ln)
4073 locinput = PL_reginput;
4074 REGCP_SET(st->u.plus.lastcp);
4075 if (st->u.plus.c1 != -1000) {
4076 st->u.plus.old = locinput;
4077 st->u.plus.count = 0;
4079 if (n == REG_INFTY) {
4080 st->u.plus.e = PL_regeol - 1;
4082 while (UTF8_IS_CONTINUATION(*(U8*)st->u.plus.e))
4087 for (st->u.plus.e = locinput;
4088 m >0 && st->u.plus.e + UTF8SKIP(st->u.plus.e) <= PL_regeol; m--)
4089 st->u.plus.e += UTF8SKIP(st->u.plus.e);
4092 st->u.plus.e = locinput + n - st->ln;
4093 if (st->u.plus.e >= PL_regeol)
4094 st->u.plus.e = PL_regeol - 1;
4097 /* Find place 'next' could work */
4099 if (st->u.plus.c1 == st->u.plus.c2) {
4100 while (locinput <= st->u.plus.e &&
4101 UCHARAT(locinput) != st->u.plus.c1)
4104 while (locinput <= st->u.plus.e
4105 && UCHARAT(locinput) != st->u.plus.c1
4106 && UCHARAT(locinput) != st->u.plus.c2)
4109 st->u.plus.count = locinput - st->u.plus.old;
4112 if (st->u.plus.c1 == st->u.plus.c2) {
4114 /* count initialised to
4115 * utf8_distance(old, locinput) */
4116 while (locinput <= st->u.plus.e &&
4117 utf8n_to_uvchr((U8*)locinput,
4118 UTF8_MAXBYTES, &len,
4119 uniflags) != (UV)st->u.plus.c1) {
4125 /* count initialised to
4126 * utf8_distance(old, locinput) */
4127 while (locinput <= st->u.plus.e) {
4128 UV c = utf8n_to_uvchr((U8*)locinput,
4129 UTF8_MAXBYTES, &len,
4131 if (c == (UV)st->u.plus.c1 || c == (UV)st->u.plus.c2)
4138 if (locinput > st->u.plus.e)
4140 /* PL_reginput == old now */
4141 if (locinput != st->u.plus.old) {
4142 st->ln = 1; /* Did some */
4143 if (regrepeat(scan, st->u.plus.count) < st->u.plus.count)
4146 /* PL_reginput == locinput now */
4147 TRYPAREN(st->u.plus.paren, st->ln, locinput, PLUS1);
4148 /*** all unsaved local vars undefined at this point */
4149 PL_reginput = locinput; /* Could be reset... */
4150 REGCP_UNWIND(st->u.plus.lastcp);
4151 /* Couldn't or didn't -- move forward. */
4152 st->u.plus.old = locinput;
4154 locinput += UTF8SKIP(locinput);
4157 st->u.plus.count = 1;
4161 while (n >= st->ln || (n == REG_INFTY && st->ln > 0)) { /* ln overflow ? */
4163 if (st->u.plus.c1 != -1000) {
4165 c = utf8n_to_uvchr((U8*)PL_reginput,
4169 c = UCHARAT(PL_reginput);
4170 /* If it could work, try it. */
4171 if (c == (UV)st->u.plus.c1 || c == (UV)st->u.plus.c2)
4173 TRYPAREN(st->u.plus.paren, st->ln, PL_reginput, PLUS2);
4174 /*** all unsaved local vars undefined at this point */
4175 REGCP_UNWIND(st->u.plus.lastcp);
4178 /* If it could work, try it. */
4179 else if (st->u.plus.c1 == -1000)
4181 TRYPAREN(st->u.plus.paren, st->ln, PL_reginput, PLUS3);
4182 /*** all unsaved local vars undefined at this point */
4183 REGCP_UNWIND(st->u.plus.lastcp);
4185 /* Couldn't or didn't -- move forward. */
4186 PL_reginput = locinput;
4187 if (regrepeat(scan, 1)) {
4189 locinput = PL_reginput;
4196 n = regrepeat(scan, n);
4197 locinput = PL_reginput;
4198 if (st->ln < n && PL_regkind[(U8)OP(next)] == EOL &&
4199 (OP(next) != MEOL ||
4200 OP(next) == SEOL || OP(next) == EOS))
4202 st->ln = n; /* why back off? */
4203 /* ...because $ and \Z can match before *and* after
4204 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4205 We should back off by one in this case. */
4206 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
4209 REGCP_SET(st->u.plus.lastcp);
4212 while (n >= st->ln) {
4213 if (st->u.plus.c1 != -1000) {
4215 c = utf8n_to_uvchr((U8*)PL_reginput,
4219 c = UCHARAT(PL_reginput);
4221 /* If it could work, try it. */
4222 if (st->u.plus.c1 == -1000 || c == (UV)st->u.plus.c1 || c == (UV)st->u.plus.c2)
4224 TRYPAREN(st->u.plus.paren, n, PL_reginput, PLUS4);
4225 /*** all unsaved local vars undefined at this point */
4226 REGCP_UNWIND(st->u.plus.lastcp);
4228 /* Couldn't or didn't -- back up. */
4230 PL_reginput = locinput = HOPc(locinput, -1);
4237 if (PL_reg_call_cc) {
4238 st->u.end.cur_call_cc = PL_reg_call_cc;
4239 st->u.end.end_re = PL_reg_re;
4241 /* Save *all* the positions. */
4242 st->u.end.cp = regcppush(0);
4243 REGCP_SET(st->u.end.lastcp);
4245 /* Restore parens of the caller. */
4247 I32 tmp = PL_savestack_ix;
4248 PL_savestack_ix = PL_reg_call_cc->ss;
4250 PL_savestack_ix = tmp;
4253 /* Make position available to the callcc. */
4254 PL_reginput = locinput;
4256 PL_reg_re = PL_reg_call_cc->re;
4257 st->u.end.savecc = st->cc;
4258 st->cc = PL_reg_call_cc->cc;
4259 PL_reg_call_cc = PL_reg_call_cc->prev;
4260 REGMATCH(st->u.end.cur_call_cc->node, END);
4261 /*** all unsaved local vars undefined at this point */
4263 PL_reg_call_cc = st->u.end.cur_call_cc;
4264 regcpblow(st->u.end.cp);
4267 REGCP_UNWIND(st->u.end.lastcp);
4269 PL_reg_call_cc = st->u.end.cur_call_cc;
4270 st->cc = st->u.end.savecc;
4271 PL_reg_re = st->u.end.end_re;
4274 PerlIO_printf(Perl_debug_log,
4275 "%*s continuation failed...\n",
4276 REPORT_CODE_OFF+PL_regindent*2, "")
4280 if (locinput < PL_regtill) {
4281 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4282 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
4284 (long)(locinput - PL_reg_starttry),
4285 (long)(PL_regtill - PL_reg_starttry),
4287 sayNO_FINAL; /* Cannot match: too short. */
4289 PL_reginput = locinput; /* put where regtry can find it */
4290 sayYES_FINAL; /* Success! */
4292 PL_reginput = locinput; /* put where regtry can find it */
4293 sayYES_LOUD; /* Success! */
4296 PL_reginput = locinput;
4301 char * const s = HOPBACKc(locinput, scan->flags);
4307 PL_reginput = locinput;
4312 char * const s = HOPBACKc(locinput, scan->flags);
4318 PL_reginput = locinput;
4321 REGMATCH(NEXTOPER(NEXTOPER(scan)), IFMATCH);
4322 /*** all unsaved local vars undefined at this point */
4338 if (OP(scan) == SUSPEND) {
4339 locinput = PL_reginput;
4340 nextchr = UCHARAT(locinput);
4345 next = scan + ARG(scan);
4350 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
4351 PTR2UV(scan), OP(scan));
4352 Perl_croak(aTHX_ "regexp memory corruption");
4360 /* simulate recursively calling regmatch(), but without actually
4361 * recursing - ie save the current state on the heap rather than on
4362 * the stack, then re-enter the loop. This avoids complex regexes
4363 * blowing the processor stack */
4367 /* push new state */
4368 regmatch_state *oldst = st;
4372 /* grab the next free state slot */
4374 if (st > &(PL_regmatch_slab->states[PERL_REGMATCH_SLAB_SLOTS-1]))
4375 st = S_push_slab(aTHX);
4376 PL_regmatch_state = st;
4380 oldst->locinput = locinput;
4381 oldst->reg_call_cc = PL_reg_call_cc;
4384 locinput = PL_reginput;
4385 nextchr = UCHARAT(locinput);
4397 * We get here only if there's trouble -- normally "case END" is
4398 * the terminating point.
4400 Perl_croak(aTHX_ "corrupted regexp pointers");
4406 PerlIO_printf(Perl_debug_log,
4407 "%*s %scould match...%s\n",
4408 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5])
4412 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
4413 PL_colors[4], PL_colors[5]));
4424 PerlIO_printf(Perl_debug_log,
4425 "%*s %sfailed...%s\n",
4426 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5])
4432 re_unwind_t * const uw = SSPTRt(st->unwind,re_unwind_t);
4435 case RE_UNWIND_BRANCH:
4436 case RE_UNWIND_BRANCHJ:
4438 re_unwind_branch_t * const uwb = &(uw->branch);
4439 const I32 lastparen = uwb->lastparen;
4441 REGCP_UNWIND(uwb->lastcp);
4442 for (n = *PL_reglastparen; n > lastparen; n--)
4444 *PL_reglastparen = n;
4445 scan = next = uwb->next;
4447 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
4448 ? BRANCH : BRANCHJ) ) { /* Failure */
4449 st->unwind = uwb->prev;
4455 /* Have more choice yet. Reuse the same uwb. */
4456 if ((n = (uwb->type == RE_UNWIND_BRANCH
4457 ? NEXT_OFF(next) : ARG(next))))
4460 next = NULL; /* XXXX Needn't unwinding in this case... */
4462 next = NEXTOPER(scan);
4463 if (uwb->type == RE_UNWIND_BRANCHJ)
4464 next = NEXTOPER(next);
4465 locinput = uwb->locinput;
4466 nextchr = uwb->nextchr;
4468 PL_regindent = uwb->regindent;
4475 Perl_croak(aTHX_ "regexp unwind memory corruption");
4486 /* restore previous state and re-enter */
4488 if (st < &PL_regmatch_slab->states[0]) {
4489 PL_regmatch_slab = PL_regmatch_slab->prev;
4490 st = &PL_regmatch_slab->states[PERL_REGMATCH_SLAB_SLOTS-1];
4492 PL_regmatch_state = st;
4494 PL_reg_call_cc = st->reg_call_cc;
4498 locinput = st->locinput;
4499 nextchr = UCHARAT(locinput);
4501 switch (st->resume_state) {
4503 goto resume_point_TRIE1;
4505 goto resume_point_TRIE2;
4507 goto resume_point_CURLYX;
4508 case resume_WHILEM1:
4509 goto resume_point_WHILEM1;
4510 case resume_WHILEM2:
4511 goto resume_point_WHILEM2;
4512 case resume_WHILEM3:
4513 goto resume_point_WHILEM3;
4514 case resume_WHILEM4:
4515 goto resume_point_WHILEM4;
4516 case resume_WHILEM5:
4517 goto resume_point_WHILEM5;
4518 case resume_WHILEM6:
4519 goto resume_point_WHILEM6;
4520 case resume_CURLYM1:
4521 goto resume_point_CURLYM1;
4522 case resume_CURLYM2:
4523 goto resume_point_CURLYM2;
4524 case resume_CURLYM3:
4525 goto resume_point_CURLYM3;
4526 case resume_CURLYM4:
4527 goto resume_point_CURLYM4;
4528 case resume_IFMATCH:
4529 goto resume_point_IFMATCH;
4531 goto resume_point_PLUS1;
4533 goto resume_point_PLUS2;
4535 goto resume_point_PLUS3;
4537 goto resume_point_PLUS4;
4539 goto resume_point_END;
4541 Perl_croak(aTHX_ "regexp resume memory corruption");
4545 /* restore original high-water mark */
4546 PL_regmatch_slab = orig_slab;
4547 PL_regmatch_state = orig_state;
4549 /* free all slabs above current one */
4550 if (orig_slab->next) {
4551 regmatch_slab *osl, *sl = orig_slab->next;
4552 orig_slab->next = NULL;
4565 - regrepeat - repeatedly match something simple, report how many
4568 * [This routine now assumes that it will only match on things of length 1.
4569 * That was true before, but now we assume scan - reginput is the count,
4570 * rather than incrementing count on every character. [Er, except utf8.]]
4573 S_regrepeat(pTHX_ const regnode *p, I32 max)
4576 register char *scan;
4578 register char *loceol = PL_regeol;
4579 register I32 hardcount = 0;
4580 register bool do_utf8 = PL_reg_match_utf8;
4583 if (max == REG_INFTY)
4585 else if (max < loceol - scan)
4586 loceol = scan + max;
4591 while (scan < loceol && hardcount < max && *scan != '\n') {
4592 scan += UTF8SKIP(scan);
4596 while (scan < loceol && *scan != '\n')
4603 while (scan < loceol && hardcount < max) {
4604 scan += UTF8SKIP(scan);
4614 case EXACT: /* length of string is 1 */
4616 while (scan < loceol && UCHARAT(scan) == c)
4619 case EXACTF: /* length of string is 1 */
4621 while (scan < loceol &&
4622 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4625 case EXACTFL: /* length of string is 1 */
4626 PL_reg_flags |= RF_tainted;
4628 while (scan < loceol &&
4629 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4635 while (hardcount < max && scan < loceol &&
4636 reginclass(p, (U8*)scan, 0, do_utf8)) {
4637 scan += UTF8SKIP(scan);
4641 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4648 LOAD_UTF8_CHARCLASS_ALNUM();
4649 while (hardcount < max && scan < loceol &&
4650 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4651 scan += UTF8SKIP(scan);
4655 while (scan < loceol && isALNUM(*scan))
4660 PL_reg_flags |= RF_tainted;
4663 while (hardcount < max && scan < loceol &&
4664 isALNUM_LC_utf8((U8*)scan)) {
4665 scan += UTF8SKIP(scan);
4669 while (scan < loceol && isALNUM_LC(*scan))
4676 LOAD_UTF8_CHARCLASS_ALNUM();
4677 while (hardcount < max && scan < loceol &&
4678 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4679 scan += UTF8SKIP(scan);
4683 while (scan < loceol && !isALNUM(*scan))
4688 PL_reg_flags |= RF_tainted;
4691 while (hardcount < max && scan < loceol &&
4692 !isALNUM_LC_utf8((U8*)scan)) {
4693 scan += UTF8SKIP(scan);
4697 while (scan < loceol && !isALNUM_LC(*scan))
4704 LOAD_UTF8_CHARCLASS_SPACE();
4705 while (hardcount < max && scan < loceol &&
4707 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4708 scan += UTF8SKIP(scan);
4712 while (scan < loceol && isSPACE(*scan))
4717 PL_reg_flags |= RF_tainted;
4720 while (hardcount < max && scan < loceol &&
4721 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4722 scan += UTF8SKIP(scan);
4726 while (scan < loceol && isSPACE_LC(*scan))
4733 LOAD_UTF8_CHARCLASS_SPACE();
4734 while (hardcount < max && scan < loceol &&
4736 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4737 scan += UTF8SKIP(scan);
4741 while (scan < loceol && !isSPACE(*scan))
4746 PL_reg_flags |= RF_tainted;
4749 while (hardcount < max && scan < loceol &&
4750 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4751 scan += UTF8SKIP(scan);
4755 while (scan < loceol && !isSPACE_LC(*scan))
4762 LOAD_UTF8_CHARCLASS_DIGIT();
4763 while (hardcount < max && scan < loceol &&
4764 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4765 scan += UTF8SKIP(scan);
4769 while (scan < loceol && isDIGIT(*scan))
4776 LOAD_UTF8_CHARCLASS_DIGIT();
4777 while (hardcount < max && scan < loceol &&
4778 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4779 scan += UTF8SKIP(scan);
4783 while (scan < loceol && !isDIGIT(*scan))
4787 default: /* Called on something of 0 width. */
4788 break; /* So match right here or not at all. */
4794 c = scan - PL_reginput;
4798 SV *re_debug_flags = NULL;
4799 SV * const prop = sv_newmortal();
4803 PerlIO_printf(Perl_debug_log,
4804 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4805 REPORT_CODE_OFF+1, "", SvPVX_const(prop),(IV)c,(IV)max);
4814 - regclass_swash - prepare the utf8 swash
4818 Perl_regclass_swash(pTHX_ register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4824 const struct reg_data *data = PL_reg_re ? PL_reg_re->data : NULL;
4826 if (data && data->count) {
4827 const U32 n = ARG(node);
4829 if (data->what[n] == 's') {
4830 SV * const rv = (SV*)data->data[n];
4831 AV * const av = (AV*)SvRV((SV*)rv);
4832 SV **const ary = AvARRAY(av);
4835 /* See the end of regcomp.c:S_regclass() for
4836 * documentation of these array elements. */
4839 a = SvROK(ary[1]) ? &ary[1] : 0;
4840 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4844 else if (si && doinit) {
4845 sw = swash_init("utf8", "", si, 1, 0);
4846 (void)av_store(av, 1, sw);
4862 - reginclass - determine if a character falls into a character class
4864 The n is the ANYOF regnode, the p is the target string, lenp
4865 is pointer to the maximum length of how far to go in the p
4866 (if the lenp is zero, UTF8SKIP(p) is used),
4867 do_utf8 tells whether the target string is in UTF-8.
4872 S_reginclass(pTHX_ register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
4875 const char flags = ANYOF_FLAGS(n);
4881 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
4882 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
4883 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) | UTF8_CHECK_ONLY);
4884 /* see [perl #37836] for UTF8_ALLOW_ANYUV */
4885 if (len == (STRLEN)-1)
4886 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
4889 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4890 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4893 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4894 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4897 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4901 SV * const sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4904 if (swash_fetch(sw, p, do_utf8))
4906 else if (flags & ANYOF_FOLD) {
4907 if (!match && lenp && av) {
4909 for (i = 0; i <= av_len(av); i++) {
4910 SV* const sv = *av_fetch(av, i, FALSE);
4912 const char * const s = SvPV_const(sv, len);
4914 if (len <= plen && memEQ(s, (char*)p, len)) {
4922 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4925 to_utf8_fold(p, tmpbuf, &tmplen);
4926 if (swash_fetch(sw, tmpbuf, do_utf8))
4932 if (match && lenp && *lenp == 0)
4933 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4935 if (!match && c < 256) {
4936 if (ANYOF_BITMAP_TEST(n, c))
4938 else if (flags & ANYOF_FOLD) {
4941 if (flags & ANYOF_LOCALE) {
4942 PL_reg_flags |= RF_tainted;
4943 f = PL_fold_locale[c];
4947 if (f != c && ANYOF_BITMAP_TEST(n, f))
4951 if (!match && (flags & ANYOF_CLASS)) {
4952 PL_reg_flags |= RF_tainted;
4954 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4955 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4956 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4957 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4958 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4959 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4960 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4961 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4962 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4963 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4964 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4965 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4966 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4967 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4968 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4969 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4970 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4971 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4972 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4973 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4974 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4975 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4976 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4977 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4978 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4979 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4980 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4981 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4982 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4983 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4984 ) /* How's that for a conditional? */
4991 return (flags & ANYOF_INVERT) ? !match : match;
4995 S_reghop3(U8 *s, I32 off, U8* lim)
4999 while (off-- && s < lim) {
5000 /* XXX could check well-formedness here */
5008 if (UTF8_IS_CONTINUED(*s)) {
5009 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
5012 /* XXX could check well-formedness here */
5020 S_reghopmaybe3(U8* s, I32 off, U8* lim)
5024 while (off-- && s < lim) {
5025 /* XXX could check well-formedness here */
5035 if (UTF8_IS_CONTINUED(*s)) {
5036 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
5039 /* XXX could check well-formedness here */
5051 restore_pos(pTHX_ void *arg)
5054 regexp *rex = (regexp *)arg;
5055 if (PL_reg_eval_set) {
5056 if (PL_reg_oldsaved) {
5057 rex->subbeg = PL_reg_oldsaved;
5058 rex->sublen = PL_reg_oldsavedlen;
5059 #ifdef PERL_OLD_COPY_ON_WRITE
5060 rex->saved_copy = PL_nrs;
5062 RX_MATCH_COPIED_on(rex);
5064 PL_reg_magic->mg_len = PL_reg_oldpos;
5065 PL_reg_eval_set = 0;
5066 PL_curpm = PL_reg_oldcurpm;
5071 S_to_utf8_substr(pTHX_ register regexp *prog)
5073 if (prog->float_substr && !prog->float_utf8) {
5075 prog->float_utf8 = sv = newSVsv(prog->float_substr);
5076 sv_utf8_upgrade(sv);
5077 if (SvTAIL(prog->float_substr))
5079 if (prog->float_substr == prog->check_substr)
5080 prog->check_utf8 = sv;
5082 if (prog->anchored_substr && !prog->anchored_utf8) {
5084 prog->anchored_utf8 = sv = newSVsv(prog->anchored_substr);
5085 sv_utf8_upgrade(sv);
5086 if (SvTAIL(prog->anchored_substr))
5088 if (prog->anchored_substr == prog->check_substr)
5089 prog->check_utf8 = sv;
5094 S_to_byte_substr(pTHX_ register regexp *prog)
5097 if (prog->float_utf8 && !prog->float_substr) {
5099 prog->float_substr = sv = newSVsv(prog->float_utf8);
5100 if (sv_utf8_downgrade(sv, TRUE)) {
5101 if (SvTAIL(prog->float_utf8))
5105 prog->float_substr = sv = &PL_sv_undef;
5107 if (prog->float_utf8 == prog->check_utf8)
5108 prog->check_substr = sv;
5110 if (prog->anchored_utf8 && !prog->anchored_substr) {
5112 prog->anchored_substr = sv = newSVsv(prog->anchored_utf8);
5113 if (sv_utf8_downgrade(sv, TRUE)) {
5114 if (SvTAIL(prog->anchored_utf8))
5118 prog->anchored_substr = sv = &PL_sv_undef;
5120 if (prog->anchored_utf8 == prog->check_utf8)
5121 prog->check_substr = sv;
5127 * c-indentation-style: bsd
5129 * indent-tabs-mode: t
5132 * ex: set ts=8 sts=4 sw=4 noet: