5 * One Ring to rule them all, One Ring to find them
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
12 /* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
14 * a regular expression.
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
26 /* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
31 /* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
36 #ifdef PERL_EXT_RE_BUILD
41 * pregcomp and pregexec -- regsub and regerror are not used in perl
43 * Copyright (c) 1986 by University of Toronto.
44 * Written by Henry Spencer. Not derived from licensed software.
46 * Permission is granted to anyone to use this software for any
47 * purpose on any computer system, and to redistribute it freely,
48 * subject to the following restrictions:
50 * 1. The author is not responsible for the consequences of use of
51 * this software, no matter how awful, even if they arise
54 * 2. The origin of this software must not be misrepresented, either
55 * by explicit claim or by omission.
57 * 3. Altered versions must be plainly marked as such, and must not
58 * be misrepresented as being the original software.
60 **** Alterations to Henry's code are...
62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
66 **** You may distribute under the terms of either the GNU General Public
67 **** License or the Artistic License, as specified in the README file.
69 * Beware that some of this code is subtly aware of the way operator
70 * precedence is structured in regular expressions. Serious changes in
71 * regular-expression syntax might require a total rethink.
74 #define PERL_IN_REGEXEC_C
78 #ifdef PERL_IN_XSUB_RE
84 #define RF_tainted 1 /* tainted information used? */
85 #define RF_warned 2 /* warned about big count? */
87 #define RF_utf8 8 /* Pattern contains multibyte chars? */
89 #define UTF ((PL_reg_flags & RF_utf8) != 0)
91 #define RS_init 1 /* eval environment created */
92 #define RS_set 2 /* replsv value is set */
98 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
104 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
105 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
107 #define HOPc(pos,off) \
108 (char *)(PL_reg_match_utf8 \
109 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
111 #define HOPBACKc(pos, off) \
112 (char*)(PL_reg_match_utf8\
113 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
114 : (pos - off >= PL_bostr) \
118 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
119 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
121 /* these are unrolled below in the CCC_TRY_XXX defined */
122 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
123 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
125 /* Doesn't do an assert to verify that is correct */
126 #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
127 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END
129 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
130 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
131 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
133 #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
134 LOAD_UTF8_CHARCLASS(X_begin, " "); \
135 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
136 /* These are utf8 constants, and not utf-ebcdic constants, so the \
137 * assert should likely and hopefully fail on an EBCDIC machine */ \
138 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
140 /* No asserts are done for these, in case called on an early \
141 * Unicode version in which they map to nothing */ \
142 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
143 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
144 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
145 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
146 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
147 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
148 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
151 We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test
152 so that it is possible to override the option here without having to
153 rebuild the entire core. as we are required to do if we change regcomp.h
154 which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined.
156 #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS
157 #define BROKEN_UNICODE_CHARCLASS_MAPPINGS
160 #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS
161 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM()
162 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE()
163 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT()
164 #define RE_utf8_perl_word PL_utf8_alnum
165 #define RE_utf8_perl_space PL_utf8_space
166 #define RE_utf8_posix_digit PL_utf8_digit
167 #define perl_word alnum
168 #define perl_space space
169 #define posix_digit digit
171 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a")
172 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ")
173 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0")
174 #define RE_utf8_perl_word PL_utf8_perl_word
175 #define RE_utf8_perl_space PL_utf8_perl_space
176 #define RE_utf8_posix_digit PL_utf8_posix_digit
180 #define CCC_TRY_AFF(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
182 PL_reg_flags |= RF_tainted; \
187 if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \
188 if (!CAT2(PL_utf8_,CLASS)) { \
192 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
196 if (!(OP(scan) == NAME \
197 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \
198 : LCFUNC_utf8((U8*)locinput))) \
202 locinput += PL_utf8skip[nextchr]; \
203 nextchr = UCHARAT(locinput); \
206 if (!(OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
208 nextchr = UCHARAT(++locinput); \
211 #define CCC_TRY_NEG(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
213 PL_reg_flags |= RF_tainted; \
216 if (!nextchr && locinput >= PL_regeol) \
218 if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \
219 if (!CAT2(PL_utf8_,CLASS)) { \
223 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
227 if ((OP(scan) == NAME \
228 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \
229 : LCFUNC_utf8((U8*)locinput))) \
233 locinput += PL_utf8skip[nextchr]; \
234 nextchr = UCHARAT(locinput); \
237 if ((OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
239 nextchr = UCHARAT(++locinput); \
246 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
248 /* for use after a quantifier and before an EXACT-like node -- japhy */
249 /* it would be nice to rework regcomp.sym to generate this stuff. sigh */
250 #define JUMPABLE(rn) ( \
252 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
254 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
255 OP(rn) == PLUS || OP(rn) == MINMOD || \
256 OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \
257 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
259 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
261 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
264 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
265 we don't need this definition. */
266 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
267 #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
268 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
271 /* ... so we use this as its faster. */
272 #define IS_TEXT(rn) ( OP(rn)==EXACT )
273 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
274 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
279 Search for mandatory following text node; for lookahead, the text must
280 follow but for lookbehind (rn->flags != 0) we skip to the next step.
282 #define FIND_NEXT_IMPT(rn) STMT_START { \
283 while (JUMPABLE(rn)) { \
284 const OPCODE type = OP(rn); \
285 if (type == SUSPEND || PL_regkind[type] == CURLY) \
286 rn = NEXTOPER(NEXTOPER(rn)); \
287 else if (type == PLUS) \
289 else if (type == IFMATCH) \
290 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
291 else rn += NEXT_OFF(rn); \
296 static void restore_pos(pTHX_ void *arg);
298 #define REGCP_PAREN_ELEMS 4
299 #define REGCP_OTHER_ELEMS 5
300 #define REGCP_FRAME_ELEMS 1
301 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
302 * are needed for the regexp context stack bookkeeping. */
305 S_regcppush(pTHX_ I32 parenfloor)
308 const int retval = PL_savestack_ix;
309 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
310 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
311 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
313 GET_RE_DEBUG_FLAGS_DECL;
315 if (paren_elems_to_push < 0)
316 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
318 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
319 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
320 " out of range (%d-%d)", total_elems, PL_regsize, parenfloor);
322 SSGROW(total_elems + REGCP_FRAME_ELEMS);
324 for (p = PL_regsize; p > parenfloor; p--) {
325 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
326 SSPUSHINT(PL_regoffs[p].end);
327 SSPUSHINT(PL_regoffs[p].start);
328 SSPUSHPTR(PL_reg_start_tmp[p]);
330 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
331 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
332 (UV)p, (IV)PL_regoffs[p].start,
333 (IV)(PL_reg_start_tmp[p] - PL_bostr),
334 (IV)PL_regoffs[p].end
337 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
338 SSPUSHPTR(PL_regoffs);
339 SSPUSHINT(PL_regsize);
340 SSPUSHINT(*PL_reglastparen);
341 SSPUSHINT(*PL_reglastcloseparen);
342 SSPUSHPTR(PL_reginput);
343 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
348 /* These are needed since we do not localize EVAL nodes: */
349 #define REGCP_SET(cp) \
351 PerlIO_printf(Perl_debug_log, \
352 " Setting an EVAL scope, savestack=%"IVdf"\n", \
353 (IV)PL_savestack_ix)); \
356 #define REGCP_UNWIND(cp) \
358 if (cp != PL_savestack_ix) \
359 PerlIO_printf(Perl_debug_log, \
360 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
361 (IV)(cp), (IV)PL_savestack_ix)); \
365 S_regcppop(pTHX_ const regexp *rex)
370 GET_RE_DEBUG_FLAGS_DECL;
372 PERL_ARGS_ASSERT_REGCPPOP;
374 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
376 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
377 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
378 input = (char *) SSPOPPTR;
379 *PL_reglastcloseparen = SSPOPINT;
380 *PL_reglastparen = SSPOPINT;
381 PL_regsize = SSPOPINT;
382 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
384 i -= REGCP_OTHER_ELEMS;
385 /* Now restore the parentheses context. */
386 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
388 U32 paren = (U32)SSPOPINT;
389 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
390 PL_regoffs[paren].start = SSPOPINT;
392 if (paren <= *PL_reglastparen)
393 PL_regoffs[paren].end = tmps;
395 PerlIO_printf(Perl_debug_log,
396 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
397 (UV)paren, (IV)PL_regoffs[paren].start,
398 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
399 (IV)PL_regoffs[paren].end,
400 (paren > *PL_reglastparen ? "(no)" : ""));
404 if (*PL_reglastparen + 1 <= rex->nparens) {
405 PerlIO_printf(Perl_debug_log,
406 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
407 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
411 /* It would seem that the similar code in regtry()
412 * already takes care of this, and in fact it is in
413 * a better location to since this code can #if 0-ed out
414 * but the code in regtry() is needed or otherwise tests
415 * requiring null fields (pat.t#187 and split.t#{13,14}
416 * (as of patchlevel 7877) will fail. Then again,
417 * this code seems to be necessary or otherwise
418 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
419 * --jhi updated by dapm */
420 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
422 PL_regoffs[i].start = -1;
423 PL_regoffs[i].end = -1;
429 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
432 * pregexec and friends
435 #ifndef PERL_IN_XSUB_RE
437 - pregexec - match a regexp against a string
440 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
441 char *strbeg, I32 minend, SV *screamer, U32 nosave)
442 /* strend: pointer to null at end of string */
443 /* strbeg: real beginning of string */
444 /* minend: end of match must be >=minend after stringarg. */
445 /* nosave: For optimizations. */
447 PERL_ARGS_ASSERT_PREGEXEC;
450 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
451 nosave ? 0 : REXEC_COPY_STR);
456 * Need to implement the following flags for reg_anch:
458 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
460 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
461 * INTUIT_AUTORITATIVE_ML
462 * INTUIT_ONCE_NOML - Intuit can match in one location only.
465 * Another flag for this function: SECOND_TIME (so that float substrs
466 * with giant delta may be not rechecked).
469 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
471 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
472 Otherwise, only SvCUR(sv) is used to get strbeg. */
474 /* XXXX We assume that strpos is strbeg unless sv. */
476 /* XXXX Some places assume that there is a fixed substring.
477 An update may be needed if optimizer marks as "INTUITable"
478 RExen without fixed substrings. Similarly, it is assumed that
479 lengths of all the strings are no more than minlen, thus they
480 cannot come from lookahead.
481 (Or minlen should take into account lookahead.)
482 NOTE: Some of this comment is not correct. minlen does now take account
483 of lookahead/behind. Further research is required. -- demerphq
487 /* A failure to find a constant substring means that there is no need to make
488 an expensive call to REx engine, thus we celebrate a failure. Similarly,
489 finding a substring too deep into the string means that less calls to
490 regtry() should be needed.
492 REx compiler's optimizer found 4 possible hints:
493 a) Anchored substring;
495 c) Whether we are anchored (beginning-of-line or \G);
496 d) First node (of those at offset 0) which may distingush positions;
497 We use a)b)d) and multiline-part of c), and try to find a position in the
498 string which does not contradict any of them.
501 /* Most of decisions we do here should have been done at compile time.
502 The nodes of the REx which we used for the search should have been
503 deleted from the finite automaton. */
506 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
507 char *strend, const U32 flags, re_scream_pos_data *data)
510 struct regexp *const prog = (struct regexp *)SvANY(rx);
511 register I32 start_shift = 0;
512 /* Should be nonnegative! */
513 register I32 end_shift = 0;
518 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
520 register char *other_last = NULL; /* other substr checked before this */
521 char *check_at = NULL; /* check substr found at this pos */
522 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
523 RXi_GET_DECL(prog,progi);
525 const char * const i_strpos = strpos;
527 GET_RE_DEBUG_FLAGS_DECL;
529 PERL_ARGS_ASSERT_RE_INTUIT_START;
531 RX_MATCH_UTF8_set(rx,do_utf8);
534 PL_reg_flags |= RF_utf8;
537 debug_start_match(rx, do_utf8, strpos, strend,
538 sv ? "Guessing start of match in sv for"
539 : "Guessing start of match in string for");
542 /* CHR_DIST() would be more correct here but it makes things slow. */
543 if (prog->minlen > strend - strpos) {
544 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
545 "String too short... [re_intuit_start]\n"));
549 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
552 if (!prog->check_utf8 && prog->check_substr)
553 to_utf8_substr(prog);
554 check = prog->check_utf8;
556 if (!prog->check_substr && prog->check_utf8)
557 to_byte_substr(prog);
558 check = prog->check_substr;
560 if (check == &PL_sv_undef) {
561 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
562 "Non-utf8 string cannot match utf8 check string\n"));
565 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
566 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
567 || ( (prog->extflags & RXf_ANCH_BOL)
568 && !multiline ) ); /* Check after \n? */
571 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
572 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
573 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
575 && (strpos != strbeg)) {
576 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
579 if (prog->check_offset_min == prog->check_offset_max &&
580 !(prog->extflags & RXf_CANY_SEEN)) {
581 /* Substring at constant offset from beg-of-str... */
584 s = HOP3c(strpos, prog->check_offset_min, strend);
587 slen = SvCUR(check); /* >= 1 */
589 if ( strend - s > slen || strend - s < slen - 1
590 || (strend - s == slen && strend[-1] != '\n')) {
591 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
594 /* Now should match s[0..slen-2] */
596 if (slen && (*SvPVX_const(check) != *s
598 && memNE(SvPVX_const(check), s, slen)))) {
600 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
604 else if (*SvPVX_const(check) != *s
605 || ((slen = SvCUR(check)) > 1
606 && memNE(SvPVX_const(check), s, slen)))
609 goto success_at_start;
612 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
614 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
615 end_shift = prog->check_end_shift;
618 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
619 - (SvTAIL(check) != 0);
620 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
622 if (end_shift < eshift)
626 else { /* Can match at random position */
629 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
630 end_shift = prog->check_end_shift;
632 /* end shift should be non negative here */
635 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
637 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
638 (IV)end_shift, RX_PRECOMP(prog));
642 /* Find a possible match in the region s..strend by looking for
643 the "check" substring in the region corrected by start/end_shift. */
646 I32 srch_start_shift = start_shift;
647 I32 srch_end_shift = end_shift;
648 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
649 srch_end_shift -= ((strbeg - s) - srch_start_shift);
650 srch_start_shift = strbeg - s;
652 DEBUG_OPTIMISE_MORE_r({
653 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
654 (IV)prog->check_offset_min,
655 (IV)srch_start_shift,
657 (IV)prog->check_end_shift);
660 if (flags & REXEC_SCREAM) {
661 I32 p = -1; /* Internal iterator of scream. */
662 I32 * const pp = data ? data->scream_pos : &p;
664 if (PL_screamfirst[BmRARE(check)] >= 0
665 || ( BmRARE(check) == '\n'
666 && (BmPREVIOUS(check) == SvCUR(check) - 1)
668 s = screaminstr(sv, check,
669 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
672 /* we may be pointing at the wrong string */
673 if (s && RXp_MATCH_COPIED(prog))
674 s = strbeg + (s - SvPVX_const(sv));
676 *data->scream_olds = s;
681 if (prog->extflags & RXf_CANY_SEEN) {
682 start_point= (U8*)(s + srch_start_shift);
683 end_point= (U8*)(strend - srch_end_shift);
685 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
686 end_point= HOP3(strend, -srch_end_shift, strbeg);
688 DEBUG_OPTIMISE_MORE_r({
689 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
690 (int)(end_point - start_point),
691 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
695 s = fbm_instr( start_point, end_point,
696 check, multiline ? FBMrf_MULTILINE : 0);
699 /* Update the count-of-usability, remove useless subpatterns,
703 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
704 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
705 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
706 (s ? "Found" : "Did not find"),
707 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
708 ? "anchored" : "floating"),
711 (s ? " at offset " : "...\n") );
716 /* Finish the diagnostic message */
717 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
719 /* XXX dmq: first branch is for positive lookbehind...
720 Our check string is offset from the beginning of the pattern.
721 So we need to do any stclass tests offset forward from that
730 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
731 Start with the other substr.
732 XXXX no SCREAM optimization yet - and a very coarse implementation
733 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
734 *always* match. Probably should be marked during compile...
735 Probably it is right to do no SCREAM here...
738 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
739 : (prog->float_substr && prog->anchored_substr))
741 /* Take into account the "other" substring. */
742 /* XXXX May be hopelessly wrong for UTF... */
745 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
748 char * const last = HOP3c(s, -start_shift, strbeg);
750 char * const saved_s = s;
753 t = s - prog->check_offset_max;
754 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
756 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
761 t = HOP3c(t, prog->anchored_offset, strend);
762 if (t < other_last) /* These positions already checked */
764 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
767 /* XXXX It is not documented what units *_offsets are in.
768 We assume bytes, but this is clearly wrong.
769 Meaning this code needs to be carefully reviewed for errors.
773 /* On end-of-str: see comment below. */
774 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
775 if (must == &PL_sv_undef) {
777 DEBUG_r(must = prog->anchored_utf8); /* for debug */
782 HOP3(HOP3(last1, prog->anchored_offset, strend)
783 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
785 multiline ? FBMrf_MULTILINE : 0
788 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
789 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
790 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
791 (s ? "Found" : "Contradicts"),
792 quoted, RE_SV_TAIL(must));
797 if (last1 >= last2) {
798 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
799 ", giving up...\n"));
802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
803 ", trying floating at offset %ld...\n",
804 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
805 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
806 s = HOP3c(last, 1, strend);
810 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
811 (long)(s - i_strpos)));
812 t = HOP3c(s, -prog->anchored_offset, strbeg);
813 other_last = HOP3c(s, 1, strend);
821 else { /* Take into account the floating substring. */
823 char * const saved_s = s;
826 t = HOP3c(s, -start_shift, strbeg);
828 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
829 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
830 last = HOP3c(t, prog->float_max_offset, strend);
831 s = HOP3c(t, prog->float_min_offset, strend);
834 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
835 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
836 /* fbm_instr() takes into account exact value of end-of-str
837 if the check is SvTAIL(ed). Since false positives are OK,
838 and end-of-str is not later than strend we are OK. */
839 if (must == &PL_sv_undef) {
841 DEBUG_r(must = prog->float_utf8); /* for debug message */
844 s = fbm_instr((unsigned char*)s,
845 (unsigned char*)last + SvCUR(must)
847 must, multiline ? FBMrf_MULTILINE : 0);
849 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
850 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
851 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
852 (s ? "Found" : "Contradicts"),
853 quoted, RE_SV_TAIL(must));
857 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
858 ", giving up...\n"));
861 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
862 ", trying anchored starting at offset %ld...\n",
863 (long)(saved_s + 1 - i_strpos)));
865 s = HOP3c(t, 1, strend);
869 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
870 (long)(s - i_strpos)));
871 other_last = s; /* Fix this later. --Hugo */
881 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
883 DEBUG_OPTIMISE_MORE_r(
884 PerlIO_printf(Perl_debug_log,
885 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
886 (IV)prog->check_offset_min,
887 (IV)prog->check_offset_max,
895 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
897 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
900 /* Fixed substring is found far enough so that the match
901 cannot start at strpos. */
903 if (ml_anch && t[-1] != '\n') {
904 /* Eventually fbm_*() should handle this, but often
905 anchored_offset is not 0, so this check will not be wasted. */
906 /* XXXX In the code below we prefer to look for "^" even in
907 presence of anchored substrings. And we search even
908 beyond the found float position. These pessimizations
909 are historical artefacts only. */
911 while (t < strend - prog->minlen) {
913 if (t < check_at - prog->check_offset_min) {
914 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
915 /* Since we moved from the found position,
916 we definitely contradict the found anchored
917 substr. Due to the above check we do not
918 contradict "check" substr.
919 Thus we can arrive here only if check substr
920 is float. Redo checking for "other"=="fixed".
923 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
924 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
925 goto do_other_anchored;
927 /* We don't contradict the found floating substring. */
928 /* XXXX Why not check for STCLASS? */
930 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
931 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
934 /* Position contradicts check-string */
935 /* XXXX probably better to look for check-string
936 than for "\n", so one should lower the limit for t? */
937 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
938 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
939 other_last = strpos = s = t + 1;
944 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
945 PL_colors[0], PL_colors[1]));
949 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
950 PL_colors[0], PL_colors[1]));
954 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
957 /* The found string does not prohibit matching at strpos,
958 - no optimization of calling REx engine can be performed,
959 unless it was an MBOL and we are not after MBOL,
960 or a future STCLASS check will fail this. */
962 /* Even in this situation we may use MBOL flag if strpos is offset
963 wrt the start of the string. */
964 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
965 && (strpos != strbeg) && strpos[-1] != '\n'
966 /* May be due to an implicit anchor of m{.*foo} */
967 && !(prog->intflags & PREGf_IMPLICIT))
972 DEBUG_EXECUTE_r( if (ml_anch)
973 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
974 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
977 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
979 prog->check_utf8 /* Could be deleted already */
980 && --BmUSEFUL(prog->check_utf8) < 0
981 && (prog->check_utf8 == prog->float_utf8)
983 prog->check_substr /* Could be deleted already */
984 && --BmUSEFUL(prog->check_substr) < 0
985 && (prog->check_substr == prog->float_substr)
988 /* If flags & SOMETHING - do not do it many times on the same match */
989 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
990 /* XXX Does the destruction order has to change with do_utf8? */
991 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
992 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
993 prog->check_substr = prog->check_utf8 = NULL; /* disable */
994 prog->float_substr = prog->float_utf8 = NULL; /* clear */
995 check = NULL; /* abort */
997 /* XXXX This is a remnant of the old implementation. It
998 looks wasteful, since now INTUIT can use many
1000 prog->extflags &= ~RXf_USE_INTUIT;
1006 /* Last resort... */
1007 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1008 /* trie stclasses are too expensive to use here, we are better off to
1009 leave it to regmatch itself */
1010 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1011 /* minlen == 0 is possible if regstclass is \b or \B,
1012 and the fixed substr is ''$.
1013 Since minlen is already taken into account, s+1 is before strend;
1014 accidentally, minlen >= 1 guaranties no false positives at s + 1
1015 even for \b or \B. But (minlen? 1 : 0) below assumes that
1016 regstclass does not come from lookahead... */
1017 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1018 This leaves EXACTF only, which is dealt with in find_byclass(). */
1019 const U8* const str = (U8*)STRING(progi->regstclass);
1020 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1021 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1024 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1025 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1026 else if (prog->float_substr || prog->float_utf8)
1027 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1031 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1032 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1035 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1038 const char *what = NULL;
1040 if (endpos == strend) {
1041 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1042 "Could not match STCLASS...\n") );
1045 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1046 "This position contradicts STCLASS...\n") );
1047 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1049 /* Contradict one of substrings */
1050 if (prog->anchored_substr || prog->anchored_utf8) {
1051 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1052 DEBUG_EXECUTE_r( what = "anchored" );
1054 s = HOP3c(t, 1, strend);
1055 if (s + start_shift + end_shift > strend) {
1056 /* XXXX Should be taken into account earlier? */
1057 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1058 "Could not match STCLASS...\n") );
1063 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1064 "Looking for %s substr starting at offset %ld...\n",
1065 what, (long)(s + start_shift - i_strpos)) );
1068 /* Have both, check_string is floating */
1069 if (t + start_shift >= check_at) /* Contradicts floating=check */
1070 goto retry_floating_check;
1071 /* Recheck anchored substring, but not floating... */
1075 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1076 "Looking for anchored substr starting at offset %ld...\n",
1077 (long)(other_last - i_strpos)) );
1078 goto do_other_anchored;
1080 /* Another way we could have checked stclass at the
1081 current position only: */
1086 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1087 "Looking for /%s^%s/m starting at offset %ld...\n",
1088 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1091 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1093 /* Check is floating subtring. */
1094 retry_floating_check:
1095 t = check_at - start_shift;
1096 DEBUG_EXECUTE_r( what = "floating" );
1097 goto hop_and_restart;
1100 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1101 "By STCLASS: moving %ld --> %ld\n",
1102 (long)(t - i_strpos), (long)(s - i_strpos))
1106 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1107 "Does not contradict STCLASS...\n");
1112 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1113 PL_colors[4], (check ? "Guessed" : "Giving up"),
1114 PL_colors[5], (long)(s - i_strpos)) );
1117 fail_finish: /* Substring not found */
1118 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1119 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1121 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1122 PL_colors[4], PL_colors[5]));
1126 #define DECL_TRIE_TYPE(scan) \
1127 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1128 trie_type = (scan->flags != EXACT) \
1129 ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \
1130 : (do_utf8 ? trie_utf8 : trie_plain)
1132 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1133 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1134 switch (trie_type) { \
1135 case trie_utf8_fold: \
1136 if ( foldlen>0 ) { \
1137 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1142 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1143 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1144 foldlen -= UNISKIP( uvc ); \
1145 uscan = foldbuf + UNISKIP( uvc ); \
1148 case trie_latin_utf8_fold: \
1149 if ( foldlen>0 ) { \
1150 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1156 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1157 foldlen -= UNISKIP( uvc ); \
1158 uscan = foldbuf + UNISKIP( uvc ); \
1162 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1169 charid = trie->charmap[ uvc ]; \
1173 if (widecharmap) { \
1174 SV** const svpp = hv_fetch(widecharmap, \
1175 (char*)&uvc, sizeof(UV), 0); \
1177 charid = (U16)SvIV(*svpp); \
1182 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1184 char *my_strend= (char *)strend; \
1187 !ibcmp_utf8(s, &my_strend, 0, do_utf8, \
1188 m, NULL, ln, cBOOL(UTF))) \
1189 && (!reginfo || regtry(reginfo, &s)) ) \
1192 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1193 uvchr_to_utf8(tmpbuf, c); \
1194 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1196 && (f == c1 || f == c2) \
1198 !ibcmp_utf8(s, &my_strend, 0, do_utf8,\
1199 m, NULL, ln, cBOOL(UTF)))\
1200 && (!reginfo || regtry(reginfo, &s)) ) \
1206 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1210 && (ln == 1 || !(OP(c) == EXACTF \
1212 : ibcmp_locale(s, m, ln))) \
1213 && (!reginfo || regtry(reginfo, &s)) ) \
1219 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1221 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1227 #define REXEC_FBC_SCAN(CoDe) \
1229 while (s < strend) { \
1235 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1236 REXEC_FBC_UTF8_SCAN( \
1238 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1247 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1250 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1259 #define REXEC_FBC_TRYIT \
1260 if ((!reginfo || regtry(reginfo, &s))) \
1263 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1265 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1268 REXEC_FBC_CLASS_SCAN(CoNd); \
1272 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1275 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1278 REXEC_FBC_CLASS_SCAN(CoNd); \
1282 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1283 PL_reg_flags |= RF_tainted; \
1285 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1288 REXEC_FBC_CLASS_SCAN(CoNd); \
1292 #define DUMP_EXEC_POS(li,s,doutf8) \
1293 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1295 /* We know what class REx starts with. Try to find this position... */
1296 /* if reginfo is NULL, its a dryrun */
1297 /* annoyingly all the vars in this routine have different names from their counterparts
1298 in regmatch. /grrr */
1301 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1302 const char *strend, regmatch_info *reginfo)
1305 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1309 register STRLEN uskip;
1313 register I32 tmp = 1; /* Scratch variable? */
1314 register const bool do_utf8 = PL_reg_match_utf8;
1315 RXi_GET_DECL(prog,progi);
1317 PERL_ARGS_ASSERT_FIND_BYCLASS;
1319 /* We know what class it must start with. */
1323 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1324 !UTF8_IS_INVARIANT((U8)s[0]) ?
1325 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1326 REGINCLASS(prog, c, (U8*)s));
1329 while (s < strend) {
1332 if (REGINCLASS(prog, c, (U8*)s) ||
1333 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1334 /* The assignment of 2 is intentional:
1335 * for the folded sharp s, the skip is 2. */
1336 (skip = SHARP_S_SKIP))) {
1337 if (tmp && (!reginfo || regtry(reginfo, &s)))
1350 if (tmp && (!reginfo || regtry(reginfo, &s)))
1358 ln = STR_LEN(c); /* length to match in octets/bytes */
1359 lnc = (I32) ln; /* length to match in characters */
1361 STRLEN ulen1, ulen2;
1363 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1364 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1365 /* used by commented-out code below */
1366 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1368 /* XXX: Since the node will be case folded at compile
1369 time this logic is a little odd, although im not
1370 sure that its actually wrong. --dmq */
1372 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1373 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1375 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1376 codepoint of the first character in the converted
1377 form, yet originally we did the extra step.
1378 No tests fail by commenting this code out however
1379 so Ive left it out. -- dmq.
1381 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1383 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1388 while (sm < ((U8 *) m + ln)) {
1403 c2 = PL_fold_locale[c1];
1405 e = HOP3c(strend, -((I32)lnc), s);
1407 if (!reginfo && e < s)
1408 e = s; /* Due to minlen logic of intuit() */
1410 /* The idea in the EXACTF* cases is to first find the
1411 * first character of the EXACTF* node and then, if
1412 * necessary, case-insensitively compare the full
1413 * text of the node. The c1 and c2 are the first
1414 * characters (though in Unicode it gets a bit
1415 * more complicated because there are more cases
1416 * than just upper and lower: one needs to use
1417 * the so-called folding case for case-insensitive
1418 * matching (called "loose matching" in Unicode).
1419 * ibcmp_utf8() will do just that. */
1421 if (do_utf8 || UTF) {
1423 U8 tmpbuf [UTF8_MAXBYTES+1];
1426 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1428 /* Upper and lower of 1st char are equal -
1429 * probably not a "letter". */
1432 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1437 REXEC_FBC_EXACTISH_CHECK(c == c1);
1443 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1449 /* Handle some of the three Greek sigmas cases.
1450 * Note that not all the possible combinations
1451 * are handled here: some of them are handled
1452 * by the standard folding rules, and some of
1453 * them (the character class or ANYOF cases)
1454 * are handled during compiletime in
1455 * regexec.c:S_regclass(). */
1456 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1457 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1458 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1460 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1465 /* Neither pattern nor string are UTF8 */
1467 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1469 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1473 PL_reg_flags |= RF_tainted;
1480 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1481 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1483 tmp = ((OP(c) == BOUND ?
1484 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1485 LOAD_UTF8_CHARCLASS_ALNUM();
1486 REXEC_FBC_UTF8_SCAN(
1487 if (tmp == !(OP(c) == BOUND ?
1488 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) :
1489 isALNUM_LC_utf8((U8*)s)))
1497 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1498 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1501 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1507 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1511 PL_reg_flags |= RF_tainted;
1518 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1519 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1521 tmp = ((OP(c) == NBOUND ?
1522 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1523 LOAD_UTF8_CHARCLASS_ALNUM();
1524 REXEC_FBC_UTF8_SCAN(
1525 if (tmp == !(OP(c) == NBOUND ?
1526 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) :
1527 isALNUM_LC_utf8((U8*)s)))
1529 else REXEC_FBC_TRYIT;
1533 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1534 tmp = ((OP(c) == NBOUND ?
1535 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1538 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1540 else REXEC_FBC_TRYIT;
1543 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1547 REXEC_FBC_CSCAN_PRELOAD(
1548 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1549 swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1553 REXEC_FBC_CSCAN_TAINT(
1554 isALNUM_LC_utf8((U8*)s),
1558 REXEC_FBC_CSCAN_PRELOAD(
1559 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1560 !swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1564 REXEC_FBC_CSCAN_TAINT(
1565 !isALNUM_LC_utf8((U8*)s),
1569 REXEC_FBC_CSCAN_PRELOAD(
1570 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1571 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8),
1575 REXEC_FBC_CSCAN_TAINT(
1576 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1580 REXEC_FBC_CSCAN_PRELOAD(
1581 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1582 !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8)),
1586 REXEC_FBC_CSCAN_TAINT(
1587 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1591 REXEC_FBC_CSCAN_PRELOAD(
1592 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1593 swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1597 REXEC_FBC_CSCAN_TAINT(
1598 isDIGIT_LC_utf8((U8*)s),
1602 REXEC_FBC_CSCAN_PRELOAD(
1603 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1604 !swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1608 REXEC_FBC_CSCAN_TAINT(
1609 !isDIGIT_LC_utf8((U8*)s),
1615 is_LNBREAK_latin1(s)
1625 !is_VERTWS_latin1(s)
1630 is_HORIZWS_latin1(s)
1634 !is_HORIZWS_utf8(s),
1635 !is_HORIZWS_latin1(s)
1641 /* what trie are we using right now */
1643 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1645 = (reg_trie_data*)progi->data->data[ aho->trie ];
1646 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1648 const char *last_start = strend - trie->minlen;
1650 const char *real_start = s;
1652 STRLEN maxlen = trie->maxlen;
1654 U8 **points; /* map of where we were in the input string
1655 when reading a given char. For ASCII this
1656 is unnecessary overhead as the relationship
1657 is always 1:1, but for Unicode, especially
1658 case folded Unicode this is not true. */
1659 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1663 GET_RE_DEBUG_FLAGS_DECL;
1665 /* We can't just allocate points here. We need to wrap it in
1666 * an SV so it gets freed properly if there is a croak while
1667 * running the match */
1670 sv_points=newSV(maxlen * sizeof(U8 *));
1671 SvCUR_set(sv_points,
1672 maxlen * sizeof(U8 *));
1673 SvPOK_on(sv_points);
1674 sv_2mortal(sv_points);
1675 points=(U8**)SvPV_nolen(sv_points );
1676 if ( trie_type != trie_utf8_fold
1677 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1680 bitmap=(U8*)trie->bitmap;
1682 bitmap=(U8*)ANYOF_BITMAP(c);
1684 /* this is the Aho-Corasick algorithm modified a touch
1685 to include special handling for long "unknown char"
1686 sequences. The basic idea being that we use AC as long
1687 as we are dealing with a possible matching char, when
1688 we encounter an unknown char (and we have not encountered
1689 an accepting state) we scan forward until we find a legal
1691 AC matching is basically that of trie matching, except
1692 that when we encounter a failing transition, we fall back
1693 to the current states "fail state", and try the current char
1694 again, a process we repeat until we reach the root state,
1695 state 1, or a legal transition. If we fail on the root state
1696 then we can either terminate if we have reached an accepting
1697 state previously, or restart the entire process from the beginning
1701 while (s <= last_start) {
1702 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1710 U8 *uscan = (U8*)NULL;
1711 U8 *leftmost = NULL;
1713 U32 accepted_word= 0;
1717 while ( state && uc <= (U8*)strend ) {
1719 U32 word = aho->states[ state ].wordnum;
1723 DEBUG_TRIE_EXECUTE_r(
1724 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1725 dump_exec_pos( (char *)uc, c, strend, real_start,
1726 (char *)uc, do_utf8 );
1727 PerlIO_printf( Perl_debug_log,
1728 " Scanning for legal start char...\n");
1731 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1736 if (uc >(U8*)last_start) break;
1740 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
1741 if (!leftmost || lpos < leftmost) {
1742 DEBUG_r(accepted_word=word);
1748 points[pointpos++ % maxlen]= uc;
1749 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1750 uscan, len, uvc, charid, foldlen,
1752 DEBUG_TRIE_EXECUTE_r({
1753 dump_exec_pos( (char *)uc, c, strend, real_start,
1755 PerlIO_printf(Perl_debug_log,
1756 " Charid:%3u CP:%4"UVxf" ",
1762 word = aho->states[ state ].wordnum;
1764 base = aho->states[ state ].trans.base;
1766 DEBUG_TRIE_EXECUTE_r({
1768 dump_exec_pos( (char *)uc, c, strend, real_start,
1770 PerlIO_printf( Perl_debug_log,
1771 "%sState: %4"UVxf", word=%"UVxf,
1772 failed ? " Fail transition to " : "",
1773 (UV)state, (UV)word);
1778 (base + charid > trie->uniquecharcount )
1779 && (base + charid - 1 - trie->uniquecharcount
1781 && trie->trans[base + charid - 1 -
1782 trie->uniquecharcount].check == state
1783 && (tmp=trie->trans[base + charid - 1 -
1784 trie->uniquecharcount ].next))
1786 DEBUG_TRIE_EXECUTE_r(
1787 PerlIO_printf( Perl_debug_log," - legal\n"));
1792 DEBUG_TRIE_EXECUTE_r(
1793 PerlIO_printf( Perl_debug_log," - fail\n"));
1795 state = aho->fail[state];
1799 /* we must be accepting here */
1800 DEBUG_TRIE_EXECUTE_r(
1801 PerlIO_printf( Perl_debug_log," - accepting\n"));
1810 if (!state) state = 1;
1813 if ( aho->states[ state ].wordnum ) {
1814 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
1815 if (!leftmost || lpos < leftmost) {
1816 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1821 s = (char*)leftmost;
1822 DEBUG_TRIE_EXECUTE_r({
1824 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1825 (UV)accepted_word, (IV)(s - real_start)
1828 if (!reginfo || regtry(reginfo, &s)) {
1834 DEBUG_TRIE_EXECUTE_r({
1835 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1838 DEBUG_TRIE_EXECUTE_r(
1839 PerlIO_printf( Perl_debug_log,"No match.\n"));
1848 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1858 - regexec_flags - match a regexp against a string
1861 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1862 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1863 /* strend: pointer to null at end of string */
1864 /* strbeg: real beginning of string */
1865 /* minend: end of match must be >=minend after stringarg. */
1866 /* data: May be used for some additional optimizations.
1867 Currently its only used, with a U32 cast, for transmitting
1868 the ganch offset when doing a /g match. This will change */
1869 /* nosave: For optimizations. */
1872 struct regexp *const prog = (struct regexp *)SvANY(rx);
1873 /*register*/ char *s;
1874 register regnode *c;
1875 /*register*/ char *startpos = stringarg;
1876 I32 minlen; /* must match at least this many chars */
1877 I32 dontbother = 0; /* how many characters not to try at end */
1878 I32 end_shift = 0; /* Same for the end. */ /* CC */
1879 I32 scream_pos = -1; /* Internal iterator of scream. */
1880 char *scream_olds = NULL;
1881 const bool do_utf8 = cBOOL(DO_UTF8(sv));
1883 RXi_GET_DECL(prog,progi);
1884 regmatch_info reginfo; /* create some info to pass to regtry etc */
1885 regexp_paren_pair *swap = NULL;
1886 GET_RE_DEBUG_FLAGS_DECL;
1888 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1889 PERL_UNUSED_ARG(data);
1891 /* Be paranoid... */
1892 if (prog == NULL || startpos == NULL) {
1893 Perl_croak(aTHX_ "NULL regexp parameter");
1897 multiline = prog->extflags & RXf_PMf_MULTILINE;
1898 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1900 RX_MATCH_UTF8_set(rx, do_utf8);
1902 debug_start_match(rx, do_utf8, startpos, strend,
1906 minlen = prog->minlen;
1908 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1909 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1910 "String too short [regexec_flags]...\n"));
1915 /* Check validity of program. */
1916 if (UCHARAT(progi->program) != REG_MAGIC) {
1917 Perl_croak(aTHX_ "corrupted regexp program");
1921 PL_reg_eval_set = 0;
1925 PL_reg_flags |= RF_utf8;
1927 /* Mark beginning of line for ^ and lookbehind. */
1928 reginfo.bol = startpos; /* XXX not used ??? */
1932 /* Mark end of line for $ (and such) */
1935 /* see how far we have to get to not match where we matched before */
1936 reginfo.till = startpos+minend;
1938 /* If there is a "must appear" string, look for it. */
1941 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1943 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
1944 reginfo.ganch = startpos + prog->gofs;
1945 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1946 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
1947 } else if (sv && SvTYPE(sv) >= SVt_PVMG
1949 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1950 && mg->mg_len >= 0) {
1951 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1952 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1953 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
1955 if (prog->extflags & RXf_ANCH_GPOS) {
1956 if (s > reginfo.ganch)
1958 s = reginfo.ganch - prog->gofs;
1959 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1960 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
1966 reginfo.ganch = strbeg + PTR2UV(data);
1967 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1968 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
1970 } else { /* pos() not defined */
1971 reginfo.ganch = strbeg;
1972 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1973 "GPOS: reginfo.ganch = strbeg\n"));
1976 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1977 /* We have to be careful. If the previous successful match
1978 was from this regex we don't want a subsequent partially
1979 successful match to clobber the old results.
1980 So when we detect this possibility we add a swap buffer
1981 to the re, and switch the buffer each match. If we fail
1982 we switch it back, otherwise we leave it swapped.
1985 /* do we need a save destructor here for eval dies? */
1986 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
1988 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1989 re_scream_pos_data d;
1991 d.scream_olds = &scream_olds;
1992 d.scream_pos = &scream_pos;
1993 s = re_intuit_start(rx, sv, s, strend, flags, &d);
1995 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1996 goto phooey; /* not present */
2002 /* Simplest case: anchored match need be tried only once. */
2003 /* [unless only anchor is BOL and multiline is set] */
2004 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2005 if (s == startpos && regtry(®info, &startpos))
2007 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2008 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2013 dontbother = minlen - 1;
2014 end = HOP3c(strend, -dontbother, strbeg) - 1;
2015 /* for multiline we only have to try after newlines */
2016 if (prog->check_substr || prog->check_utf8) {
2020 if (regtry(®info, &s))
2025 if (prog->extflags & RXf_USE_INTUIT) {
2026 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2037 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2038 if (regtry(®info, &s))
2045 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2047 /* the warning about reginfo.ganch being used without intialization
2048 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2049 and we only enter this block when the same bit is set. */
2050 char *tmp_s = reginfo.ganch - prog->gofs;
2052 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2057 /* Messy cases: unanchored match. */
2058 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2059 /* we have /x+whatever/ */
2060 /* it must be a one character string (XXXX Except UTF?) */
2065 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2066 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2067 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
2072 DEBUG_EXECUTE_r( did_match = 1 );
2073 if (regtry(®info, &s)) goto got_it;
2075 while (s < strend && *s == ch)
2083 DEBUG_EXECUTE_r( did_match = 1 );
2084 if (regtry(®info, &s)) goto got_it;
2086 while (s < strend && *s == ch)
2091 DEBUG_EXECUTE_r(if (!did_match)
2092 PerlIO_printf(Perl_debug_log,
2093 "Did not find anchored character...\n")
2096 else if (prog->anchored_substr != NULL
2097 || prog->anchored_utf8 != NULL
2098 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2099 && prog->float_max_offset < strend - s)) {
2104 char *last1; /* Last position checked before */
2108 if (prog->anchored_substr || prog->anchored_utf8) {
2109 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2110 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2111 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
2112 back_max = back_min = prog->anchored_offset;
2114 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2115 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2116 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
2117 back_max = prog->float_max_offset;
2118 back_min = prog->float_min_offset;
2122 if (must == &PL_sv_undef)
2123 /* could not downgrade utf8 check substring, so must fail */
2129 last = HOP3c(strend, /* Cannot start after this */
2130 -(I32)(CHR_SVLEN(must)
2131 - (SvTAIL(must) != 0) + back_min), strbeg);
2134 last1 = HOPc(s, -1);
2136 last1 = s - 1; /* bogus */
2138 /* XXXX check_substr already used to find "s", can optimize if
2139 check_substr==must. */
2141 dontbother = end_shift;
2142 strend = HOPc(strend, -dontbother);
2143 while ( (s <= last) &&
2144 ((flags & REXEC_SCREAM)
2145 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2146 end_shift, &scream_pos, 0))
2147 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2148 (unsigned char*)strend, must,
2149 multiline ? FBMrf_MULTILINE : 0))) ) {
2150 /* we may be pointing at the wrong string */
2151 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2152 s = strbeg + (s - SvPVX_const(sv));
2153 DEBUG_EXECUTE_r( did_match = 1 );
2154 if (HOPc(s, -back_max) > last1) {
2155 last1 = HOPc(s, -back_min);
2156 s = HOPc(s, -back_max);
2159 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2161 last1 = HOPc(s, -back_min);
2165 while (s <= last1) {
2166 if (regtry(®info, &s))
2172 while (s <= last1) {
2173 if (regtry(®info, &s))
2179 DEBUG_EXECUTE_r(if (!did_match) {
2180 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
2181 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2182 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2183 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2184 ? "anchored" : "floating"),
2185 quoted, RE_SV_TAIL(must));
2189 else if ( (c = progi->regstclass) ) {
2191 const OPCODE op = OP(progi->regstclass);
2192 /* don't bother with what can't match */
2193 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2194 strend = HOPc(strend, -(minlen - 1));
2197 SV * const prop = sv_newmortal();
2198 regprop(prog, prop, c);
2200 RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1),
2202 PerlIO_printf(Perl_debug_log,
2203 "Matching stclass %.*s against %s (%d chars)\n",
2204 (int)SvCUR(prop), SvPVX_const(prop),
2205 quoted, (int)(strend - s));
2208 if (find_byclass(prog, c, s, strend, ®info))
2210 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2214 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2219 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2220 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2221 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
2223 if (flags & REXEC_SCREAM) {
2224 last = screaminstr(sv, float_real, s - strbeg,
2225 end_shift, &scream_pos, 1); /* last one */
2227 last = scream_olds; /* Only one occurrence. */
2228 /* we may be pointing at the wrong string */
2229 else if (RXp_MATCH_COPIED(prog))
2230 s = strbeg + (s - SvPVX_const(sv));
2234 const char * const little = SvPV_const(float_real, len);
2236 if (SvTAIL(float_real)) {
2237 if (memEQ(strend - len + 1, little, len - 1))
2238 last = strend - len + 1;
2239 else if (!multiline)
2240 last = memEQ(strend - len, little, len)
2241 ? strend - len : NULL;
2247 last = rninstr(s, strend, little, little + len);
2249 last = strend; /* matching "$" */
2254 PerlIO_printf(Perl_debug_log,
2255 "%sCan't trim the tail, match fails (should not happen)%s\n",
2256 PL_colors[4], PL_colors[5]));
2257 goto phooey; /* Should not happen! */
2259 dontbother = strend - last + prog->float_min_offset;
2261 if (minlen && (dontbother < minlen))
2262 dontbother = minlen - 1;
2263 strend -= dontbother; /* this one's always in bytes! */
2264 /* We don't know much -- general case. */
2267 if (regtry(®info, &s))
2276 if (regtry(®info, &s))
2278 } while (s++ < strend);
2287 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2289 if (PL_reg_eval_set)
2290 restore_pos(aTHX_ prog);
2291 if (RXp_PAREN_NAMES(prog))
2292 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2294 /* make sure $`, $&, $', and $digit will work later */
2295 if ( !(flags & REXEC_NOT_FIRST) ) {
2296 RX_MATCH_COPY_FREE(rx);
2297 if (flags & REXEC_COPY_STR) {
2298 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2299 #ifdef PERL_OLD_COPY_ON_WRITE
2301 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2303 PerlIO_printf(Perl_debug_log,
2304 "Copy on write: regexp capture, type %d\n",
2307 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2308 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2309 assert (SvPOKp(prog->saved_copy));
2313 RX_MATCH_COPIED_on(rx);
2314 s = savepvn(strbeg, i);
2320 prog->subbeg = strbeg;
2321 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2328 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2329 PL_colors[4], PL_colors[5]));
2330 if (PL_reg_eval_set)
2331 restore_pos(aTHX_ prog);
2333 /* we failed :-( roll it back */
2334 Safefree(prog->offs);
2343 - regtry - try match at specific point
2345 STATIC I32 /* 0 failure, 1 success */
2346 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2350 REGEXP *const rx = reginfo->prog;
2351 regexp *const prog = (struct regexp *)SvANY(rx);
2352 RXi_GET_DECL(prog,progi);
2353 GET_RE_DEBUG_FLAGS_DECL;
2355 PERL_ARGS_ASSERT_REGTRY;
2357 reginfo->cutpoint=NULL;
2359 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2362 PL_reg_eval_set = RS_init;
2363 DEBUG_EXECUTE_r(DEBUG_s(
2364 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2365 (IV)(PL_stack_sp - PL_stack_base));
2368 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2369 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2371 /* Apparently this is not needed, judging by wantarray. */
2372 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2373 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2376 /* Make $_ available to executed code. */
2377 if (reginfo->sv != DEFSV) {
2379 DEFSV_set(reginfo->sv);
2382 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2383 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2384 /* prepare for quick setting of pos */
2385 #ifdef PERL_OLD_COPY_ON_WRITE
2386 if (SvIsCOW(reginfo->sv))
2387 sv_force_normal_flags(reginfo->sv, 0);
2389 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2390 &PL_vtbl_mglob, NULL, 0);
2394 PL_reg_oldpos = mg->mg_len;
2395 SAVEDESTRUCTOR_X(restore_pos, prog);
2397 if (!PL_reg_curpm) {
2398 Newxz(PL_reg_curpm, 1, PMOP);
2401 SV* const repointer = &PL_sv_undef;
2402 /* this regexp is also owned by the new PL_reg_curpm, which
2403 will try to free it. */
2404 av_push(PL_regex_padav, repointer);
2405 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2406 PL_regex_pad = AvARRAY(PL_regex_padav);
2411 /* It seems that non-ithreads works both with and without this code.
2412 So for efficiency reasons it seems best not to have the code
2413 compiled when it is not needed. */
2414 /* This is safe against NULLs: */
2415 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2416 /* PM_reg_curpm owns a reference to this regexp. */
2419 PM_SETRE(PL_reg_curpm, rx);
2420 PL_reg_oldcurpm = PL_curpm;
2421 PL_curpm = PL_reg_curpm;
2422 if (RXp_MATCH_COPIED(prog)) {
2423 /* Here is a serious problem: we cannot rewrite subbeg,
2424 since it may be needed if this match fails. Thus
2425 $` inside (?{}) could fail... */
2426 PL_reg_oldsaved = prog->subbeg;
2427 PL_reg_oldsavedlen = prog->sublen;
2428 #ifdef PERL_OLD_COPY_ON_WRITE
2429 PL_nrs = prog->saved_copy;
2431 RXp_MATCH_COPIED_off(prog);
2434 PL_reg_oldsaved = NULL;
2435 prog->subbeg = PL_bostr;
2436 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2438 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2439 prog->offs[0].start = *startpos - PL_bostr;
2440 PL_reginput = *startpos;
2441 PL_reglastparen = &prog->lastparen;
2442 PL_reglastcloseparen = &prog->lastcloseparen;
2443 prog->lastparen = 0;
2444 prog->lastcloseparen = 0;
2446 PL_regoffs = prog->offs;
2447 if (PL_reg_start_tmpl <= prog->nparens) {
2448 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2449 if(PL_reg_start_tmp)
2450 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2452 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2455 /* XXXX What this code is doing here?!!! There should be no need
2456 to do this again and again, PL_reglastparen should take care of
2459 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2460 * Actually, the code in regcppop() (which Ilya may be meaning by
2461 * PL_reglastparen), is not needed at all by the test suite
2462 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2463 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2464 * Meanwhile, this code *is* needed for the
2465 * above-mentioned test suite tests to succeed. The common theme
2466 * on those tests seems to be returning null fields from matches.
2467 * --jhi updated by dapm */
2469 if (prog->nparens) {
2470 regexp_paren_pair *pp = PL_regoffs;
2472 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2480 if (regmatch(reginfo, progi->program + 1)) {
2481 PL_regoffs[0].end = PL_reginput - PL_bostr;
2484 if (reginfo->cutpoint)
2485 *startpos= reginfo->cutpoint;
2486 REGCP_UNWIND(lastcp);
2491 #define sayYES goto yes
2492 #define sayNO goto no
2493 #define sayNO_SILENT goto no_silent
2495 /* we dont use STMT_START/END here because it leads to
2496 "unreachable code" warnings, which are bogus, but distracting. */
2497 #define CACHEsayNO \
2498 if (ST.cache_mask) \
2499 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2502 /* this is used to determine how far from the left messages like
2503 'failed...' are printed. It should be set such that messages
2504 are inline with the regop output that created them.
2506 #define REPORT_CODE_OFF 32
2509 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2510 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2512 #define SLAB_FIRST(s) (&(s)->states[0])
2513 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2515 /* grab a new slab and return the first slot in it */
2517 STATIC regmatch_state *
2520 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2523 regmatch_slab *s = PL_regmatch_slab->next;
2525 Newx(s, 1, regmatch_slab);
2526 s->prev = PL_regmatch_slab;
2528 PL_regmatch_slab->next = s;
2530 PL_regmatch_slab = s;
2531 return SLAB_FIRST(s);
2535 /* push a new state then goto it */
2537 #define PUSH_STATE_GOTO(state, node) \
2539 st->resume_state = state; \
2542 /* push a new state with success backtracking, then goto it */
2544 #define PUSH_YES_STATE_GOTO(state, node) \
2546 st->resume_state = state; \
2547 goto push_yes_state;
2553 regmatch() - main matching routine
2555 This is basically one big switch statement in a loop. We execute an op,
2556 set 'next' to point the next op, and continue. If we come to a point which
2557 we may need to backtrack to on failure such as (A|B|C), we push a
2558 backtrack state onto the backtrack stack. On failure, we pop the top
2559 state, and re-enter the loop at the state indicated. If there are no more
2560 states to pop, we return failure.
2562 Sometimes we also need to backtrack on success; for example /A+/, where
2563 after successfully matching one A, we need to go back and try to
2564 match another one; similarly for lookahead assertions: if the assertion
2565 completes successfully, we backtrack to the state just before the assertion
2566 and then carry on. In these cases, the pushed state is marked as
2567 'backtrack on success too'. This marking is in fact done by a chain of
2568 pointers, each pointing to the previous 'yes' state. On success, we pop to
2569 the nearest yes state, discarding any intermediate failure-only states.
2570 Sometimes a yes state is pushed just to force some cleanup code to be
2571 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2572 it to free the inner regex.
2574 Note that failure backtracking rewinds the cursor position, while
2575 success backtracking leaves it alone.
2577 A pattern is complete when the END op is executed, while a subpattern
2578 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2579 ops trigger the "pop to last yes state if any, otherwise return true"
2582 A common convention in this function is to use A and B to refer to the two
2583 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2584 the subpattern to be matched possibly multiple times, while B is the entire
2585 rest of the pattern. Variable and state names reflect this convention.
2587 The states in the main switch are the union of ops and failure/success of
2588 substates associated with with that op. For example, IFMATCH is the op
2589 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2590 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2591 successfully matched A and IFMATCH_A_fail is a state saying that we have
2592 just failed to match A. Resume states always come in pairs. The backtrack
2593 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2594 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2595 on success or failure.
2597 The struct that holds a backtracking state is actually a big union, with
2598 one variant for each major type of op. The variable st points to the
2599 top-most backtrack struct. To make the code clearer, within each
2600 block of code we #define ST to alias the relevant union.
2602 Here's a concrete example of a (vastly oversimplified) IFMATCH
2608 #define ST st->u.ifmatch
2610 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2611 ST.foo = ...; // some state we wish to save
2613 // push a yes backtrack state with a resume value of
2614 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2616 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2619 case IFMATCH_A: // we have successfully executed A; now continue with B
2621 bar = ST.foo; // do something with the preserved value
2624 case IFMATCH_A_fail: // A failed, so the assertion failed
2625 ...; // do some housekeeping, then ...
2626 sayNO; // propagate the failure
2633 For any old-timers reading this who are familiar with the old recursive
2634 approach, the code above is equivalent to:
2636 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2645 ...; // do some housekeeping, then ...
2646 sayNO; // propagate the failure
2649 The topmost backtrack state, pointed to by st, is usually free. If you
2650 want to claim it, populate any ST.foo fields in it with values you wish to
2651 save, then do one of
2653 PUSH_STATE_GOTO(resume_state, node);
2654 PUSH_YES_STATE_GOTO(resume_state, node);
2656 which sets that backtrack state's resume value to 'resume_state', pushes a
2657 new free entry to the top of the backtrack stack, then goes to 'node'.
2658 On backtracking, the free slot is popped, and the saved state becomes the
2659 new free state. An ST.foo field in this new top state can be temporarily
2660 accessed to retrieve values, but once the main loop is re-entered, it
2661 becomes available for reuse.
2663 Note that the depth of the backtrack stack constantly increases during the
2664 left-to-right execution of the pattern, rather than going up and down with
2665 the pattern nesting. For example the stack is at its maximum at Z at the
2666 end of the pattern, rather than at X in the following:
2668 /(((X)+)+)+....(Y)+....Z/
2670 The only exceptions to this are lookahead/behind assertions and the cut,
2671 (?>A), which pop all the backtrack states associated with A before
2674 Bascktrack state structs are allocated in slabs of about 4K in size.
2675 PL_regmatch_state and st always point to the currently active state,
2676 and PL_regmatch_slab points to the slab currently containing
2677 PL_regmatch_state. The first time regmatch() is called, the first slab is
2678 allocated, and is never freed until interpreter destruction. When the slab
2679 is full, a new one is allocated and chained to the end. At exit from
2680 regmatch(), slabs allocated since entry are freed.
2685 #define DEBUG_STATE_pp(pp) \
2687 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2688 PerlIO_printf(Perl_debug_log, \
2689 " %*s"pp" %s%s%s%s%s\n", \
2691 PL_reg_name[st->resume_state], \
2692 ((st==yes_state||st==mark_state) ? "[" : ""), \
2693 ((st==yes_state) ? "Y" : ""), \
2694 ((st==mark_state) ? "M" : ""), \
2695 ((st==yes_state||st==mark_state) ? "]" : "") \
2700 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2705 S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8,
2706 const char *start, const char *end, const char *blurb)
2708 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2710 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2715 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2716 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2718 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2719 start, end - start, 60);
2721 PerlIO_printf(Perl_debug_log,
2722 "%s%s REx%s %s against %s\n",
2723 PL_colors[4], blurb, PL_colors[5], s0, s1);
2725 if (do_utf8||utf8_pat)
2726 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2727 utf8_pat ? "pattern" : "",
2728 utf8_pat && do_utf8 ? " and " : "",
2729 do_utf8 ? "string" : ""
2735 S_dump_exec_pos(pTHX_ const char *locinput,
2736 const regnode *scan,
2737 const char *loc_regeol,
2738 const char *loc_bostr,
2739 const char *loc_reg_starttry,
2742 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2743 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2744 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2745 /* The part of the string before starttry has one color
2746 (pref0_len chars), between starttry and current
2747 position another one (pref_len - pref0_len chars),
2748 after the current position the third one.
2749 We assume that pref0_len <= pref_len, otherwise we
2750 decrease pref0_len. */
2751 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2752 ? (5 + taill) - l : locinput - loc_bostr;
2755 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2757 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2759 pref0_len = pref_len - (locinput - loc_reg_starttry);
2760 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2761 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2762 ? (5 + taill) - pref_len : loc_regeol - locinput);
2763 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2767 if (pref0_len > pref_len)
2768 pref0_len = pref_len;
2770 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2772 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2773 (locinput - pref_len),pref0_len, 60, 4, 5);
2775 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2776 (locinput - pref_len + pref0_len),
2777 pref_len - pref0_len, 60, 2, 3);
2779 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2780 locinput, loc_regeol - locinput, 10, 0, 1);
2782 const STRLEN tlen=len0+len1+len2;
2783 PerlIO_printf(Perl_debug_log,
2784 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2785 (IV)(locinput - loc_bostr),
2788 (docolor ? "" : "> <"),
2790 (int)(tlen > 19 ? 0 : 19 - tlen),
2797 /* reg_check_named_buff_matched()
2798 * Checks to see if a named buffer has matched. The data array of
2799 * buffer numbers corresponding to the buffer is expected to reside
2800 * in the regexp->data->data array in the slot stored in the ARG() of
2801 * node involved. Note that this routine doesn't actually care about the
2802 * name, that information is not preserved from compilation to execution.
2803 * Returns the index of the leftmost defined buffer with the given name
2804 * or 0 if non of the buffers matched.
2807 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2810 RXi_GET_DECL(rex,rexi);
2811 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2812 I32 *nums=(I32*)SvPVX(sv_dat);
2814 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2816 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2817 if ((I32)*PL_reglastparen >= nums[n] &&
2818 PL_regoffs[nums[n]].end != -1)
2827 /* free all slabs above current one - called during LEAVE_SCOPE */
2830 S_clear_backtrack_stack(pTHX_ void *p)
2832 regmatch_slab *s = PL_regmatch_slab->next;
2837 PL_regmatch_slab->next = NULL;
2839 regmatch_slab * const osl = s;
2846 #define SETREX(Re1,Re2) \
2847 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2850 STATIC I32 /* 0 failure, 1 success */
2851 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2853 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2857 register const bool do_utf8 = PL_reg_match_utf8;
2858 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2859 REGEXP *rex_sv = reginfo->prog;
2860 regexp *rex = (struct regexp *)SvANY(rex_sv);
2861 RXi_GET_DECL(rex,rexi);
2863 /* the current state. This is a cached copy of PL_regmatch_state */
2864 register regmatch_state *st;
2865 /* cache heavy used fields of st in registers */
2866 register regnode *scan;
2867 register regnode *next;
2868 register U32 n = 0; /* general value; init to avoid compiler warning */
2869 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2870 register char *locinput = PL_reginput;
2871 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2873 bool result = 0; /* return value of S_regmatch */
2874 int depth = 0; /* depth of backtrack stack */
2875 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2876 const U32 max_nochange_depth =
2877 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2878 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2879 regmatch_state *yes_state = NULL; /* state to pop to on success of
2881 /* mark_state piggy backs on the yes_state logic so that when we unwind
2882 the stack on success we can update the mark_state as we go */
2883 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2884 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2885 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2887 bool no_final = 0; /* prevent failure from backtracking? */
2888 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2889 char *startpoint = PL_reginput;
2890 SV *popmark = NULL; /* are we looking for a mark? */
2891 SV *sv_commit = NULL; /* last mark name seen in failure */
2892 SV *sv_yes_mark = NULL; /* last mark name we have seen
2893 during a successfull match */
2894 U32 lastopen = 0; /* last open we saw */
2895 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2896 SV* const oreplsv = GvSV(PL_replgv);
2897 /* these three flags are set by various ops to signal information to
2898 * the very next op. They have a useful lifetime of exactly one loop
2899 * iteration, and are not preserved or restored by state pushes/pops
2901 bool sw = 0; /* the condition value in (?(cond)a|b) */
2902 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2903 int logical = 0; /* the following EVAL is:
2907 or the following IFMATCH/UNLESSM is:
2908 false: plain (?=foo)
2909 true: used as a condition: (?(?=foo))
2912 GET_RE_DEBUG_FLAGS_DECL;
2915 PERL_ARGS_ASSERT_REGMATCH;
2917 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2918 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2920 /* on first ever call to regmatch, allocate first slab */
2921 if (!PL_regmatch_slab) {
2922 Newx(PL_regmatch_slab, 1, regmatch_slab);
2923 PL_regmatch_slab->prev = NULL;
2924 PL_regmatch_slab->next = NULL;
2925 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2928 oldsave = PL_savestack_ix;
2929 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2930 SAVEVPTR(PL_regmatch_slab);
2931 SAVEVPTR(PL_regmatch_state);
2933 /* grab next free state slot */
2934 st = ++PL_regmatch_state;
2935 if (st > SLAB_LAST(PL_regmatch_slab))
2936 st = PL_regmatch_state = S_push_slab(aTHX);
2938 /* Note that nextchr is a byte even in UTF */
2939 nextchr = UCHARAT(locinput);
2941 while (scan != NULL) {
2944 SV * const prop = sv_newmortal();
2945 regnode *rnext=regnext(scan);
2946 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2947 regprop(rex, prop, scan);
2949 PerlIO_printf(Perl_debug_log,
2950 "%3"IVdf":%*s%s(%"IVdf")\n",
2951 (IV)(scan - rexi->program), depth*2, "",
2953 (PL_regkind[OP(scan)] == END || !rnext) ?
2954 0 : (IV)(rnext - rexi->program));
2957 next = scan + NEXT_OFF(scan);
2960 state_num = OP(scan);
2962 REH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);
2965 assert(PL_reglastparen == &rex->lastparen);
2966 assert(PL_reglastcloseparen == &rex->lastcloseparen);
2967 assert(PL_regoffs == rex->offs);
2969 switch (state_num) {
2971 if (locinput == PL_bostr)
2973 /* reginfo->till = reginfo->bol; */
2978 if (locinput == PL_bostr ||
2979 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2985 if (locinput == PL_bostr)
2989 if (locinput == reginfo->ganch)
2994 /* update the startpoint */
2995 st->u.keeper.val = PL_regoffs[0].start;
2996 PL_reginput = locinput;
2997 PL_regoffs[0].start = locinput - PL_bostr;
2998 PUSH_STATE_GOTO(KEEPS_next, next);
3000 case KEEPS_next_fail:
3001 /* rollback the start point change */
3002 PL_regoffs[0].start = st->u.keeper.val;
3008 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3013 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3015 if (PL_regeol - locinput > 1)
3019 if (PL_regeol != locinput)
3023 if (!nextchr && locinput >= PL_regeol)
3026 locinput += PL_utf8skip[nextchr];
3027 if (locinput > PL_regeol)
3029 nextchr = UCHARAT(locinput);
3032 nextchr = UCHARAT(++locinput);
3035 if (!nextchr && locinput >= PL_regeol)
3037 nextchr = UCHARAT(++locinput);
3040 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3043 locinput += PL_utf8skip[nextchr];
3044 if (locinput > PL_regeol)
3046 nextchr = UCHARAT(locinput);
3049 nextchr = UCHARAT(++locinput);
3053 #define ST st->u.trie
3055 /* In this case the charclass data is available inline so
3056 we can fail fast without a lot of extra overhead.
3058 if (scan->flags == EXACT || !do_utf8) {
3059 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3061 PerlIO_printf(Perl_debug_log,
3062 "%*s %sfailed to match trie start class...%s\n",
3063 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3071 /* the basic plan of execution of the trie is:
3072 * At the beginning, run though all the states, and
3073 * find the longest-matching word. Also remember the position
3074 * of the shortest matching word. For example, this pattern:
3077 * when matched against the string "abcde", will generate
3078 * accept states for all words except 3, with the longest
3079 * matching word being 4, and the shortest being 1 (with
3080 * the position being after char 1 of the string).
3082 * Then for each matching word, in word order (i.e. 1,2,4,5),
3083 * we run the remainder of the pattern; on each try setting
3084 * the current position to the character following the word,
3085 * returning to try the next word on failure.
3087 * We avoid having to build a list of words at runtime by
3088 * using a compile-time structure, wordinfo[].prev, which
3089 * gives, for each word, the previous accepting word (if any).
3090 * In the case above it would contain the mappings 1->2, 2->0,
3091 * 3->0, 4->5, 5->1. We can use this table to generate, from
3092 * the longest word (4 above), a list of all words, by
3093 * following the list of prev pointers; this gives us the
3094 * unordered list 4,5,1,2. Then given the current word we have
3095 * just tried, we can go through the list and find the
3096 * next-biggest word to try (so if we just failed on word 2,
3097 * the next in the list is 4).
3099 * Since at runtime we don't record the matching position in
3100 * the string for each word, we have to work that out for
3101 * each word we're about to process. The wordinfo table holds
3102 * the character length of each word; given that we recorded
3103 * at the start: the position of the shortest word and its
3104 * length in chars, we just need to move the pointer the
3105 * difference between the two char lengths. Depending on
3106 * Unicode status and folding, that's cheap or expensive.
3108 * This algorithm is optimised for the case where are only a
3109 * small number of accept states, i.e. 0,1, or maybe 2.
3110 * With lots of accepts states, and having to try all of them,
3111 * it becomes quadratic on number of accept states to find all
3116 /* what type of TRIE am I? (utf8 makes this contextual) */
3117 DECL_TRIE_TYPE(scan);
3119 /* what trie are we using right now */
3120 reg_trie_data * const trie
3121 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3122 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3123 U32 state = trie->startstate;
3125 if (trie->bitmap && trie_type != trie_utf8_fold &&
3126 !TRIE_BITMAP_TEST(trie,*locinput)
3128 if (trie->states[ state ].wordnum) {
3130 PerlIO_printf(Perl_debug_log,
3131 "%*s %smatched empty string...%s\n",
3132 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3137 PerlIO_printf(Perl_debug_log,
3138 "%*s %sfailed to match trie start class...%s\n",
3139 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3146 U8 *uc = ( U8* )locinput;
3150 U8 *uscan = (U8*)NULL;
3151 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3152 U32 charcount = 0; /* how many input chars we have matched */
3153 U32 accepted = 0; /* have we seen any accepting states? */
3156 ST.jump = trie->jump;
3159 ST.longfold = FALSE; /* char longer if folded => it's harder */
3162 /* fully traverse the TRIE; note the position of the
3163 shortest accept state and the wordnum of the longest
3166 while ( state && uc <= (U8*)PL_regeol ) {
3167 U32 base = trie->states[ state ].trans.base;
3171 wordnum = trie->states[ state ].wordnum;
3173 if (wordnum) { /* it's an accept state */
3176 /* record first match position */
3178 ST.firstpos = (U8*)locinput;
3183 ST.firstchars = charcount;
3186 if (!ST.nextword || wordnum < ST.nextword)
3187 ST.nextword = wordnum;
3188 ST.topword = wordnum;
3191 DEBUG_TRIE_EXECUTE_r({
3192 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
3193 PerlIO_printf( Perl_debug_log,
3194 "%*s %sState: %4"UVxf" Accepted: %c ",
3195 2+depth * 2, "", PL_colors[4],
3196 (UV)state, (accepted ? 'Y' : 'N'));
3199 /* read a char and goto next state */
3201 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3202 uscan, len, uvc, charid, foldlen,
3208 (base + charid > trie->uniquecharcount )
3209 && (base + charid - 1 - trie->uniquecharcount
3211 && trie->trans[base + charid - 1 -
3212 trie->uniquecharcount].check == state)
3214 state = trie->trans[base + charid - 1 -
3215 trie->uniquecharcount ].next;
3226 DEBUG_TRIE_EXECUTE_r(
3227 PerlIO_printf( Perl_debug_log,
3228 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3229 charid, uvc, (UV)state, PL_colors[5] );
3235 /* calculate total number of accept states */
3240 w = trie->wordinfo[w].prev;
3243 ST.accepted = accepted;
3247 PerlIO_printf( Perl_debug_log,
3248 "%*s %sgot %"IVdf" possible matches%s\n",
3249 REPORT_CODE_OFF + depth * 2, "",
3250 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3252 goto trie_first_try; /* jump into the fail handler */
3256 case TRIE_next_fail: /* we failed - try next alternative */
3258 REGCP_UNWIND(ST.cp);
3259 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3260 PL_regoffs[n].end = -1;
3261 *PL_reglastparen = n;
3263 if (!--ST.accepted) {
3265 PerlIO_printf( Perl_debug_log,
3266 "%*s %sTRIE failed...%s\n",
3267 REPORT_CODE_OFF+depth*2, "",
3274 /* Find next-highest word to process. Note that this code
3275 * is O(N^2) per trie run (O(N) per branch), so keep tight */
3276 register U16 min = 0;
3278 register U16 const nextword = ST.nextword;
3279 register reg_trie_wordinfo * const wordinfo
3280 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3281 for (word=ST.topword; word; word=wordinfo[word].prev) {
3282 if (word > nextword && (!min || word < min))
3295 ST.lastparen = *PL_reglastparen;
3299 /* find start char of end of current word */
3301 U32 chars; /* how many chars to skip */
3302 U8 *uc = ST.firstpos;
3303 reg_trie_data * const trie
3304 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3306 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3308 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3312 /* the hard option - fold each char in turn and find
3313 * its folded length (which may be different */
3314 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3322 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3330 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3335 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3349 PL_reginput = (char *)uc;
3352 scan = (ST.jump && ST.jump[ST.nextword])
3353 ? ST.me + ST.jump[ST.nextword]
3357 PerlIO_printf( Perl_debug_log,
3358 "%*s %sTRIE matched word #%d, continuing%s\n",
3359 REPORT_CODE_OFF+depth*2, "",
3366 if (ST.accepted > 1 || has_cutgroup) {
3367 PUSH_STATE_GOTO(TRIE_next, scan);
3370 /* only one choice left - just continue */
3372 AV *const trie_words
3373 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3374 SV ** const tmp = av_fetch( trie_words,
3376 SV *sv= tmp ? sv_newmortal() : NULL;
3378 PerlIO_printf( Perl_debug_log,
3379 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3380 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3382 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3383 PL_colors[0], PL_colors[1],
3384 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3386 : "not compiled under -Dr",
3390 locinput = PL_reginput;
3391 nextchr = UCHARAT(locinput);
3392 continue; /* execute rest of RE */
3397 char *s = STRING(scan);
3399 if (do_utf8 != UTF) {
3400 /* The target and the pattern have differing utf8ness. */
3402 const char * const e = s + ln;
3405 /* The target is utf8, the pattern is not utf8. */
3410 if (NATIVE_TO_UNI(*(U8*)s) !=
3411 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3419 /* The target is not utf8, the pattern is utf8. */
3424 if (NATIVE_TO_UNI(*((U8*)l)) !=
3425 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3433 nextchr = UCHARAT(locinput);
3436 /* The target and the pattern have the same utf8ness. */
3437 /* Inline the first character, for speed. */
3438 if (UCHARAT(s) != nextchr)
3440 if (PL_regeol - locinput < ln)
3442 if (ln > 1 && memNE(s, locinput, ln))
3445 nextchr = UCHARAT(locinput);
3449 PL_reg_flags |= RF_tainted;
3452 char * const s = STRING(scan);
3455 if (do_utf8 || UTF) {
3456 /* Either target or the pattern are utf8. */
3457 const char * const l = locinput;
3458 char *e = PL_regeol;
3460 if (ibcmp_utf8(s, 0, ln, cBOOL(UTF),
3461 l, &e, 0, do_utf8)) {
3462 /* One more case for the sharp s:
3463 * pack("U0U*", 0xDF) =~ /ss/i,
3464 * the 0xC3 0x9F are the UTF-8
3465 * byte sequence for the U+00DF. */
3468 toLOWER(s[0]) == 's' &&
3470 toLOWER(s[1]) == 's' &&
3477 nextchr = UCHARAT(locinput);
3481 /* Neither the target and the pattern are utf8. */
3483 /* Inline the first character, for speed. */
3484 if (UCHARAT(s) != nextchr &&
3485 UCHARAT(s) != ((OP(scan) == EXACTF)
3486 ? PL_fold : PL_fold_locale)[nextchr])
3488 if (PL_regeol - locinput < ln)
3490 if (ln > 1 && (OP(scan) == EXACTF
3491 ? ibcmp(s, locinput, ln)
3492 : ibcmp_locale(s, locinput, ln)))
3495 nextchr = UCHARAT(locinput);
3500 PL_reg_flags |= RF_tainted;
3504 /* was last char in word? */
3506 if (locinput == PL_bostr)
3509 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3511 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3513 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3514 ln = isALNUM_uni(ln);
3515 LOAD_UTF8_CHARCLASS_ALNUM();
3516 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3519 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3520 n = isALNUM_LC_utf8((U8*)locinput);
3524 ln = (locinput != PL_bostr) ?
3525 UCHARAT(locinput - 1) : '\n';
3526 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3528 n = isALNUM(nextchr);
3531 ln = isALNUM_LC(ln);
3532 n = isALNUM_LC(nextchr);
3535 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3536 OP(scan) == BOUNDL))
3541 STRLEN inclasslen = PL_regeol - locinput;
3543 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3545 if (locinput >= PL_regeol)
3547 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3548 nextchr = UCHARAT(locinput);
3553 nextchr = UCHARAT(locinput);
3554 if (!REGINCLASS(rex, scan, (U8*)locinput))
3556 if (!nextchr && locinput >= PL_regeol)
3558 nextchr = UCHARAT(++locinput);
3562 /* If we might have the case of the German sharp s
3563 * in a casefolding Unicode character class. */
3565 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3566 locinput += SHARP_S_SKIP;
3567 nextchr = UCHARAT(locinput);
3572 /* Special char classes - The defines start on line 129 or so */
3573 CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3574 CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3576 CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3577 CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3579 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3580 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3582 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3583 a Unicode extended Grapheme Cluster */
3584 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3585 extended Grapheme Cluster is:
3588 | Prepend* Begin Extend*
3591 Begin is (Hangul-syllable | ! Control)
3592 Extend is (Grapheme_Extend | Spacing_Mark)
3593 Control is [ GCB_Control CR LF ]
3595 The discussion below shows how the code for CLUMP is derived
3596 from this regex. Note that most of these concepts are from
3597 property values of the Grapheme Cluster Boundary (GCB) property.
3598 No code point can have multiple property values for a given
3599 property. Thus a code point in Prepend can't be in Control, but
3600 it must be in !Control. This is why Control above includes
3601 GCB_Control plus CR plus LF. The latter two are used in the GCB
3602 property separately, and so can't be in GCB_Control, even though
3603 they logically are controls. Control is not the same as gc=cc,
3604 but includes format and other characters as well.
3606 The Unicode definition of Hangul-syllable is:
3608 | (L* ( ( V | LV ) V* | LVT ) T*)
3611 Each of these is a value for the GCB property, and hence must be
3612 disjoint, so the order they are tested is immaterial, so the
3613 above can safely be changed to
3616 | (L* ( LVT | ( V | LV ) V*) T*)
3618 The last two terms can be combined like this:
3620 | (( LVT | ( V | LV ) V*) T*))
3622 And refactored into this:
3623 L* (L | LVT T* | V V* T* | LV V* T*)
3625 That means that if we have seen any L's at all we can quit
3626 there, but if the next character is a LVT, a V or and LV we
3629 There is a subtlety with Prepend* which showed up in testing.
3630 Note that the Begin, and only the Begin is required in:
3631 | Prepend* Begin Extend*
3632 Also, Begin contains '! Control'. A Prepend must be a '!
3633 Control', which means it must be a Begin. What it comes down to
3634 is that if we match Prepend* and then find no suitable Begin
3635 afterwards, that if we backtrack the last Prepend, that one will
3636 be a suitable Begin.
3639 if (locinput >= PL_regeol)
3643 /* Match either CR LF or '.', as all the other possibilities
3645 locinput++; /* Match the . or CR */
3647 && locinput < PL_regeol
3648 && UCHARAT(locinput) == '\n') locinput++;
3652 /* Utf8: See if is ( CR LF ); already know that locinput <
3653 * PL_regeol, so locinput+1 is in bounds */
3654 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3658 /* In case have to backtrack to beginning, then match '.' */
3659 char *starting = locinput;
3661 /* In case have to backtrack the last prepend */
3662 char *previous_prepend = 0;
3664 LOAD_UTF8_CHARCLASS_GCB();
3666 /* Match (prepend)* */
3667 while (locinput < PL_regeol
3668 && swash_fetch(PL_utf8_X_prepend,
3669 (U8*)locinput, do_utf8))
3671 previous_prepend = locinput;
3672 locinput += UTF8SKIP(locinput);
3675 /* As noted above, if we matched a prepend character, but
3676 * the next thing won't match, back off the last prepend we
3677 * matched, as it is guaranteed to match the begin */
3678 if (previous_prepend
3679 && (locinput >= PL_regeol
3680 || ! swash_fetch(PL_utf8_X_begin,
3681 (U8*)locinput, do_utf8)))
3683 locinput = previous_prepend;
3686 /* Note that here we know PL_regeol > locinput, as we
3687 * tested that upon input to this switch case, and if we
3688 * moved locinput forward, we tested the result just above
3689 * and it either passed, or we backed off so that it will
3691 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, do_utf8)) {
3693 /* Here did not match the required 'Begin' in the
3694 * second term. So just match the very first
3695 * character, the '.' of the final term of the regex */
3696 locinput = starting + UTF8SKIP(starting);
3699 /* Here is the beginning of a character that can have
3700 * an extender. It is either a hangul syllable, or a
3702 if (swash_fetch(PL_utf8_X_non_hangul,
3703 (U8*)locinput, do_utf8))
3706 /* Here not a Hangul syllable, must be a
3707 * ('! * Control') */
3708 locinput += UTF8SKIP(locinput);
3711 /* Here is a Hangul syllable. It can be composed
3712 * of several individual characters. One
3713 * possibility is T+ */
3714 if (swash_fetch(PL_utf8_X_T,
3715 (U8*)locinput, do_utf8))
3717 while (locinput < PL_regeol
3718 && swash_fetch(PL_utf8_X_T,
3719 (U8*)locinput, do_utf8))
3721 locinput += UTF8SKIP(locinput);
3725 /* Here, not T+, but is a Hangul. That means
3726 * it is one of the others: L, LV, LVT or V,
3728 * L* (L | LVT T* | V V* T* | LV V* T*) */
3731 while (locinput < PL_regeol
3732 && swash_fetch(PL_utf8_X_L,
3733 (U8*)locinput, do_utf8))
3735 locinput += UTF8SKIP(locinput);
3738 /* Here, have exhausted L*. If the next
3739 * character is not an LV, LVT nor V, it means
3740 * we had to have at least one L, so matches L+
3741 * in the original equation, we have a complete
3742 * hangul syllable. Are done. */
3744 if (locinput < PL_regeol
3745 && swash_fetch(PL_utf8_X_LV_LVT_V,
3746 (U8*)locinput, do_utf8))
3749 /* Otherwise keep going. Must be LV, LVT
3750 * or V. See if LVT */
3751 if (swash_fetch(PL_utf8_X_LVT,
3752 (U8*)locinput, do_utf8))
3754 locinput += UTF8SKIP(locinput);
3757 /* Must be V or LV. Take it, then
3759 locinput += UTF8SKIP(locinput);
3760 while (locinput < PL_regeol
3761 && swash_fetch(PL_utf8_X_V,
3762 (U8*)locinput, do_utf8))
3764 locinput += UTF8SKIP(locinput);
3768 /* And any of LV, LVT, or V can be followed
3770 while (locinput < PL_regeol
3771 && swash_fetch(PL_utf8_X_T,
3775 locinput += UTF8SKIP(locinput);
3781 /* Match any extender */
3782 while (locinput < PL_regeol
3783 && swash_fetch(PL_utf8_X_extend,
3784 (U8*)locinput, do_utf8))
3786 locinput += UTF8SKIP(locinput);
3790 if (locinput > PL_regeol) sayNO;
3792 nextchr = UCHARAT(locinput);
3799 PL_reg_flags |= RF_tainted;
3804 n = reg_check_named_buff_matched(rex,scan);
3807 type = REF + ( type - NREF );
3814 PL_reg_flags |= RF_tainted;
3818 n = ARG(scan); /* which paren pair */
3821 ln = PL_regoffs[n].start;
3822 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3823 if (*PL_reglastparen < n || ln == -1)
3824 sayNO; /* Do not match unless seen CLOSEn. */
3825 if (ln == PL_regoffs[n].end)
3829 if (do_utf8 && type != REF) { /* REF can do byte comparison */
3831 const char *e = PL_bostr + PL_regoffs[n].end;
3833 * Note that we can't do the "other character" lookup trick as
3834 * in the 8-bit case (no pun intended) because in Unicode we
3835 * have to map both upper and title case to lower case.
3839 STRLEN ulen1, ulen2;
3840 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3841 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3845 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3846 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3847 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3854 nextchr = UCHARAT(locinput);
3858 /* Inline the first character, for speed. */
3859 if (UCHARAT(s) != nextchr &&
3861 (UCHARAT(s) != (type == REFF
3862 ? PL_fold : PL_fold_locale)[nextchr])))
3864 ln = PL_regoffs[n].end - ln;
3865 if (locinput + ln > PL_regeol)
3867 if (ln > 1 && (type == REF
3868 ? memNE(s, locinput, ln)
3870 ? ibcmp(s, locinput, ln)
3871 : ibcmp_locale(s, locinput, ln))))
3874 nextchr = UCHARAT(locinput);
3884 #define ST st->u.eval
3889 regexp_internal *rei;
3890 regnode *startpoint;
3893 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3894 if (cur_eval && cur_eval->locinput==locinput) {
3895 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3896 Perl_croak(aTHX_ "Infinite recursion in regex");
3897 if ( ++nochange_depth > max_nochange_depth )
3899 "Pattern subroutine nesting without pos change"
3900 " exceeded limit in regex");
3907 (void)ReREFCNT_inc(rex_sv);
3908 if (OP(scan)==GOSUB) {
3909 startpoint = scan + ARG2L(scan);
3910 ST.close_paren = ARG(scan);
3912 startpoint = rei->program+1;
3915 goto eval_recurse_doit;
3917 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3918 if (cur_eval && cur_eval->locinput==locinput) {
3919 if ( ++nochange_depth > max_nochange_depth )
3920 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3925 /* execute the code in the {...} */
3927 SV ** const before = SP;
3928 OP_4tree * const oop = PL_op;
3929 COP * const ocurcop = PL_curcop;
3931 char *saved_regeol = PL_regeol;
3934 PL_op = (OP_4tree*)rexi->data->data[n];
3935 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3936 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3937 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3938 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3941 SV *sv_mrk = get_sv("REGMARK", 1);
3942 sv_setsv(sv_mrk, sv_yes_mark);
3945 CALLRUNOPS(aTHX); /* Scalar context. */
3948 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3955 PAD_RESTORE_LOCAL(old_comppad);
3956 PL_curcop = ocurcop;
3957 PL_regeol = saved_regeol;
3960 sv_setsv(save_scalar(PL_replgv), ret);
3964 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
3967 /* extract RE object from returned value; compiling if
3973 SV *const sv = SvRV(ret);
3975 if (SvTYPE(sv) == SVt_REGEXP) {
3977 } else if (SvSMAGICAL(sv)) {
3978 mg = mg_find(sv, PERL_MAGIC_qr);
3981 } else if (SvTYPE(ret) == SVt_REGEXP) {
3983 } else if (SvSMAGICAL(ret)) {
3984 if (SvGMAGICAL(ret)) {
3985 /* I don't believe that there is ever qr magic
3987 assert(!mg_find(ret, PERL_MAGIC_qr));
3988 sv_unmagic(ret, PERL_MAGIC_qr);
3991 mg = mg_find(ret, PERL_MAGIC_qr);
3992 /* testing suggests mg only ends up non-NULL for
3993 scalars who were upgraded and compiled in the
3994 else block below. In turn, this is only
3995 triggered in the "postponed utf8 string" tests
4001 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
4005 rx = reg_temp_copy(NULL, rx);
4009 const I32 osize = PL_regsize;
4012 assert (SvUTF8(ret));
4013 } else if (SvUTF8(ret)) {
4014 /* Not doing UTF-8, despite what the SV says. Is
4015 this only if we're trapped in use 'bytes'? */
4016 /* Make a copy of the octet sequence, but without
4017 the flag on, as the compiler now honours the
4018 SvUTF8 flag on ret. */
4020 const char *const p = SvPV(ret, len);
4021 ret = newSVpvn_flags(p, len, SVs_TEMP);
4023 rx = CALLREGCOMP(ret, pm_flags);
4025 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
4027 /* This isn't a first class regexp. Instead, it's
4028 caching a regexp onto an existing, Perl visible
4030 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
4035 re = (struct regexp *)SvANY(rx);
4037 RXp_MATCH_COPIED_off(re);
4038 re->subbeg = rex->subbeg;
4039 re->sublen = rex->sublen;
4042 debug_start_match(re_sv, do_utf8, locinput, PL_regeol,
4043 "Matching embedded");
4045 startpoint = rei->program + 1;
4046 ST.close_paren = 0; /* only used for GOSUB */
4047 /* borrowed from regtry */
4048 if (PL_reg_start_tmpl <= re->nparens) {
4049 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4050 if(PL_reg_start_tmp)
4051 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4053 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4056 eval_recurse_doit: /* Share code with GOSUB below this line */
4057 /* run the pattern returned from (??{...}) */
4058 ST.cp = regcppush(0); /* Save *all* the positions. */
4059 REGCP_SET(ST.lastcp);
4061 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
4063 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4064 PL_reglastparen = &re->lastparen;
4065 PL_reglastcloseparen = &re->lastcloseparen;
4067 re->lastcloseparen = 0;
4069 PL_reginput = locinput;
4072 /* XXXX This is too dramatic a measure... */
4075 ST.toggle_reg_flags = PL_reg_flags;
4077 PL_reg_flags |= RF_utf8;
4079 PL_reg_flags &= ~RF_utf8;
4080 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4082 ST.prev_rex = rex_sv;
4083 ST.prev_curlyx = cur_curlyx;
4084 SETREX(rex_sv,re_sv);
4089 ST.prev_eval = cur_eval;
4091 /* now continue from first node in postoned RE */
4092 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4095 /* logical is 1, /(?(?{...})X|Y)/ */
4096 sw = cBOOL(SvTRUE(ret));
4101 case EVAL_AB: /* cleanup after a successful (??{A})B */
4102 /* note: this is called twice; first after popping B, then A */
4103 PL_reg_flags ^= ST.toggle_reg_flags;
4104 ReREFCNT_dec(rex_sv);
4105 SETREX(rex_sv,ST.prev_rex);
4106 rex = (struct regexp *)SvANY(rex_sv);
4107 rexi = RXi_GET(rex);
4109 cur_eval = ST.prev_eval;
4110 cur_curlyx = ST.prev_curlyx;
4112 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4113 PL_reglastparen = &rex->lastparen;
4114 PL_reglastcloseparen = &rex->lastcloseparen;
4115 /* also update PL_regoffs */
4116 PL_regoffs = rex->offs;
4118 /* XXXX This is too dramatic a measure... */
4120 if ( nochange_depth )
4125 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4126 /* note: this is called twice; first after popping B, then A */
4127 PL_reg_flags ^= ST.toggle_reg_flags;
4128 ReREFCNT_dec(rex_sv);
4129 SETREX(rex_sv,ST.prev_rex);
4130 rex = (struct regexp *)SvANY(rex_sv);
4131 rexi = RXi_GET(rex);
4132 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4133 PL_reglastparen = &rex->lastparen;
4134 PL_reglastcloseparen = &rex->lastcloseparen;
4136 PL_reginput = locinput;
4137 REGCP_UNWIND(ST.lastcp);
4139 cur_eval = ST.prev_eval;
4140 cur_curlyx = ST.prev_curlyx;
4141 /* XXXX This is too dramatic a measure... */
4143 if ( nochange_depth )
4149 n = ARG(scan); /* which paren pair */
4150 PL_reg_start_tmp[n] = locinput;
4156 n = ARG(scan); /* which paren pair */
4157 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4158 PL_regoffs[n].end = locinput - PL_bostr;
4159 /*if (n > PL_regsize)
4161 if (n > *PL_reglastparen)
4162 *PL_reglastparen = n;
4163 *PL_reglastcloseparen = n;
4164 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4172 cursor && OP(cursor)!=END;
4173 cursor=regnext(cursor))
4175 if ( OP(cursor)==CLOSE ){
4177 if ( n <= lastopen ) {
4179 = PL_reg_start_tmp[n] - PL_bostr;
4180 PL_regoffs[n].end = locinput - PL_bostr;
4181 /*if (n > PL_regsize)
4183 if (n > *PL_reglastparen)
4184 *PL_reglastparen = n;
4185 *PL_reglastcloseparen = n;
4186 if ( n == ARG(scan) || (cur_eval &&
4187 cur_eval->u.eval.close_paren == n))
4196 n = ARG(scan); /* which paren pair */
4197 sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
4200 /* reg_check_named_buff_matched returns 0 for no match */
4201 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
4205 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4211 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4213 next = NEXTOPER(NEXTOPER(scan));
4215 next = scan + ARG(scan);
4216 if (OP(next) == IFTHEN) /* Fake one. */
4217 next = NEXTOPER(NEXTOPER(next));
4221 logical = scan->flags;
4224 /*******************************************************************
4226 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4227 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4228 STAR/PLUS/CURLY/CURLYN are used instead.)
4230 A*B is compiled as <CURLYX><A><WHILEM><B>
4232 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4233 state, which contains the current count, initialised to -1. It also sets
4234 cur_curlyx to point to this state, with any previous value saved in the
4237 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4238 since the pattern may possibly match zero times (i.e. it's a while {} loop
4239 rather than a do {} while loop).
4241 Each entry to WHILEM represents a successful match of A. The count in the
4242 CURLYX block is incremented, another WHILEM state is pushed, and execution
4243 passes to A or B depending on greediness and the current count.
4245 For example, if matching against the string a1a2a3b (where the aN are
4246 substrings that match /A/), then the match progresses as follows: (the
4247 pushed states are interspersed with the bits of strings matched so far):
4250 <CURLYX cnt=0><WHILEM>
4251 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4252 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4253 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4254 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4256 (Contrast this with something like CURLYM, which maintains only a single
4260 a1 <CURLYM cnt=1> a2
4261 a1 a2 <CURLYM cnt=2> a3
4262 a1 a2 a3 <CURLYM cnt=3> b
4265 Each WHILEM state block marks a point to backtrack to upon partial failure
4266 of A or B, and also contains some minor state data related to that
4267 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4268 overall state, such as the count, and pointers to the A and B ops.
4270 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4271 must always point to the *current* CURLYX block, the rules are:
4273 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4274 and set cur_curlyx to point the new block.
4276 When popping the CURLYX block after a successful or unsuccessful match,
4277 restore the previous cur_curlyx.
4279 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4280 to the outer one saved in the CURLYX block.
4282 When popping the WHILEM block after a successful or unsuccessful B match,
4283 restore the previous cur_curlyx.
4285 Here's an example for the pattern (AI* BI)*BO
4286 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4289 curlyx backtrack stack
4290 ------ ---------------
4292 CO <CO prev=NULL> <WO>
4293 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4294 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4295 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4297 At this point the pattern succeeds, and we work back down the stack to
4298 clean up, restoring as we go:
4300 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4301 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4302 CO <CO prev=NULL> <WO>
4305 *******************************************************************/
4307 #define ST st->u.curlyx
4309 case CURLYX: /* start of /A*B/ (for complex A) */
4311 /* No need to save/restore up to this paren */
4312 I32 parenfloor = scan->flags;
4314 assert(next); /* keep Coverity happy */
4315 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4318 /* XXXX Probably it is better to teach regpush to support
4319 parenfloor > PL_regsize... */
4320 if (parenfloor > (I32)*PL_reglastparen)
4321 parenfloor = *PL_reglastparen; /* Pessimization... */
4323 ST.prev_curlyx= cur_curlyx;
4325 ST.cp = PL_savestack_ix;
4327 /* these fields contain the state of the current curly.
4328 * they are accessed by subsequent WHILEMs */
4329 ST.parenfloor = parenfloor;
4330 ST.min = ARG1(scan);
4331 ST.max = ARG2(scan);
4332 ST.A = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
4336 ST.count = -1; /* this will be updated by WHILEM */
4337 ST.lastloc = NULL; /* this will be updated by WHILEM */
4339 PL_reginput = locinput;
4340 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4344 case CURLYX_end: /* just finished matching all of A*B */
4345 cur_curlyx = ST.prev_curlyx;
4349 case CURLYX_end_fail: /* just failed to match all of A*B */
4351 cur_curlyx = ST.prev_curlyx;
4357 #define ST st->u.whilem
4359 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4361 /* see the discussion above about CURLYX/WHILEM */
4363 assert(cur_curlyx); /* keep Coverity happy */
4364 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4365 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4366 ST.cache_offset = 0;
4369 PL_reginput = locinput;
4371 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4372 "%*s whilem: matched %ld out of %ld..%ld\n",
4373 REPORT_CODE_OFF+depth*2, "", (long)n,
4374 (long)cur_curlyx->u.curlyx.min,
4375 (long)cur_curlyx->u.curlyx.max)
4378 /* First just match a string of min A's. */
4380 if (n < cur_curlyx->u.curlyx.min) {
4381 cur_curlyx->u.curlyx.lastloc = locinput;
4382 PUSH_STATE_GOTO(WHILEM_A_pre, cur_curlyx->u.curlyx.A);
4386 /* If degenerate A matches "", assume A done. */
4388 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4389 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4390 "%*s whilem: empty match detected, trying continuation...\n",
4391 REPORT_CODE_OFF+depth*2, "")
4393 goto do_whilem_B_max;
4396 /* super-linear cache processing */
4400 if (!PL_reg_maxiter) {
4401 /* start the countdown: Postpone detection until we
4402 * know the match is not *that* much linear. */
4403 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4404 /* possible overflow for long strings and many CURLYX's */
4405 if (PL_reg_maxiter < 0)
4406 PL_reg_maxiter = I32_MAX;
4407 PL_reg_leftiter = PL_reg_maxiter;
4410 if (PL_reg_leftiter-- == 0) {
4411 /* initialise cache */
4412 const I32 size = (PL_reg_maxiter + 7)/8;
4413 if (PL_reg_poscache) {
4414 if ((I32)PL_reg_poscache_size < size) {
4415 Renew(PL_reg_poscache, size, char);
4416 PL_reg_poscache_size = size;
4418 Zero(PL_reg_poscache, size, char);
4421 PL_reg_poscache_size = size;
4422 Newxz(PL_reg_poscache, size, char);
4424 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4425 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4426 PL_colors[4], PL_colors[5])
4430 if (PL_reg_leftiter < 0) {
4431 /* have we already failed at this position? */
4433 offset = (scan->flags & 0xf) - 1
4434 + (locinput - PL_bostr) * (scan->flags>>4);
4435 mask = 1 << (offset % 8);
4437 if (PL_reg_poscache[offset] & mask) {
4438 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4439 "%*s whilem: (cache) already tried at this position...\n",
4440 REPORT_CODE_OFF+depth*2, "")
4442 sayNO; /* cache records failure */
4444 ST.cache_offset = offset;
4445 ST.cache_mask = mask;
4449 /* Prefer B over A for minimal matching. */
4451 if (cur_curlyx->u.curlyx.minmod) {
4452 ST.save_curlyx = cur_curlyx;
4453 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4454 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4455 REGCP_SET(ST.lastcp);
4456 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4460 /* Prefer A over B for maximal matching. */
4462 if (n < cur_curlyx->u.curlyx.max) { /* More greed allowed? */
4463 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4464 cur_curlyx->u.curlyx.lastloc = locinput;
4465 REGCP_SET(ST.lastcp);
4466 PUSH_STATE_GOTO(WHILEM_A_max, cur_curlyx->u.curlyx.A);
4469 goto do_whilem_B_max;
4473 case WHILEM_B_min: /* just matched B in a minimal match */
4474 case WHILEM_B_max: /* just matched B in a maximal match */
4475 cur_curlyx = ST.save_curlyx;
4479 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4480 cur_curlyx = ST.save_curlyx;
4481 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4482 cur_curlyx->u.curlyx.count--;
4486 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4487 REGCP_UNWIND(ST.lastcp);
4490 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4491 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4492 cur_curlyx->u.curlyx.count--;
4496 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4497 REGCP_UNWIND(ST.lastcp);
4498 regcppop(rex); /* Restore some previous $<digit>s? */
4499 PL_reginput = locinput;
4500 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4501 "%*s whilem: failed, trying continuation...\n",
4502 REPORT_CODE_OFF+depth*2, "")
4505 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4506 && ckWARN(WARN_REGEXP)
4507 && !(PL_reg_flags & RF_warned))
4509 PL_reg_flags |= RF_warned;
4510 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4511 "Complex regular subexpression recursion",
4516 ST.save_curlyx = cur_curlyx;
4517 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4518 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4521 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4522 cur_curlyx = ST.save_curlyx;
4523 REGCP_UNWIND(ST.lastcp);
4526 if (cur_curlyx->u.curlyx.count >= cur_curlyx->u.curlyx.max) {
4527 /* Maximum greed exceeded */
4528 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4529 && ckWARN(WARN_REGEXP)
4530 && !(PL_reg_flags & RF_warned))
4532 PL_reg_flags |= RF_warned;
4533 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4534 "%s limit (%d) exceeded",
4535 "Complex regular subexpression recursion",
4538 cur_curlyx->u.curlyx.count--;
4542 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4543 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4545 /* Try grabbing another A and see if it helps. */
4546 PL_reginput = locinput;
4547 cur_curlyx->u.curlyx.lastloc = locinput;
4548 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4549 REGCP_SET(ST.lastcp);
4550 PUSH_STATE_GOTO(WHILEM_A_min, ST.save_curlyx->u.curlyx.A);
4554 #define ST st->u.branch
4556 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4557 next = scan + ARG(scan);
4560 scan = NEXTOPER(scan);
4563 case BRANCH: /* /(...|A|...)/ */
4564 scan = NEXTOPER(scan); /* scan now points to inner node */
4565 ST.lastparen = *PL_reglastparen;
4566 ST.next_branch = next;
4568 PL_reginput = locinput;
4570 /* Now go into the branch */
4572 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4574 PUSH_STATE_GOTO(BRANCH_next, scan);
4578 PL_reginput = locinput;
4579 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4580 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4581 PUSH_STATE_GOTO(CUTGROUP_next,next);
4583 case CUTGROUP_next_fail:
4586 if (st->u.mark.mark_name)
4587 sv_commit = st->u.mark.mark_name;
4593 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4598 REGCP_UNWIND(ST.cp);
4599 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4600 PL_regoffs[n].end = -1;
4601 *PL_reglastparen = n;
4602 /*dmq: *PL_reglastcloseparen = n; */
4603 scan = ST.next_branch;
4604 /* no more branches? */
4605 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4607 PerlIO_printf( Perl_debug_log,
4608 "%*s %sBRANCH failed...%s\n",
4609 REPORT_CODE_OFF+depth*2, "",
4615 continue; /* execute next BRANCH[J] op */
4623 #define ST st->u.curlym
4625 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4627 /* This is an optimisation of CURLYX that enables us to push
4628 * only a single backtracking state, no matter how many matches
4629 * there are in {m,n}. It relies on the pattern being constant
4630 * length, with no parens to influence future backrefs
4634 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4636 /* if paren positive, emulate an OPEN/CLOSE around A */
4638 U32 paren = ST.me->flags;
4639 if (paren > PL_regsize)
4641 if (paren > *PL_reglastparen)
4642 *PL_reglastparen = paren;
4643 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4651 ST.c1 = CHRTEST_UNINIT;
4654 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4657 curlym_do_A: /* execute the A in /A{m,n}B/ */
4658 PL_reginput = locinput;
4659 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4662 case CURLYM_A: /* we've just matched an A */
4663 locinput = st->locinput;
4664 nextchr = UCHARAT(locinput);
4667 /* after first match, determine A's length: u.curlym.alen */
4668 if (ST.count == 1) {
4669 if (PL_reg_match_utf8) {
4671 while (s < PL_reginput) {
4677 ST.alen = PL_reginput - locinput;
4680 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4683 PerlIO_printf(Perl_debug_log,
4684 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4685 (int)(REPORT_CODE_OFF+(depth*2)), "",
4686 (IV) ST.count, (IV)ST.alen)
4689 locinput = PL_reginput;
4691 if (cur_eval && cur_eval->u.eval.close_paren &&
4692 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4696 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4697 if ( max == REG_INFTY || ST.count < max )
4698 goto curlym_do_A; /* try to match another A */
4700 goto curlym_do_B; /* try to match B */
4702 case CURLYM_A_fail: /* just failed to match an A */
4703 REGCP_UNWIND(ST.cp);
4705 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4706 || (cur_eval && cur_eval->u.eval.close_paren &&
4707 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4710 curlym_do_B: /* execute the B in /A{m,n}B/ */
4711 PL_reginput = locinput;
4712 if (ST.c1 == CHRTEST_UNINIT) {
4713 /* calculate c1 and c2 for possible match of 1st char
4714 * following curly */
4715 ST.c1 = ST.c2 = CHRTEST_VOID;
4716 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4717 regnode *text_node = ST.B;
4718 if (! HAS_TEXT(text_node))
4719 FIND_NEXT_IMPT(text_node);
4722 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4724 But the former is redundant in light of the latter.
4726 if this changes back then the macro for
4727 IS_TEXT and friends need to change.
4729 if (PL_regkind[OP(text_node)] == EXACT)
4732 ST.c1 = (U8)*STRING(text_node);
4734 (IS_TEXTF(text_node))
4736 : (IS_TEXTFL(text_node))
4737 ? PL_fold_locale[ST.c1]
4744 PerlIO_printf(Perl_debug_log,
4745 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4746 (int)(REPORT_CODE_OFF+(depth*2)),
4749 if (ST.c1 != CHRTEST_VOID
4750 && UCHARAT(PL_reginput) != ST.c1
4751 && UCHARAT(PL_reginput) != ST.c2)
4753 /* simulate B failing */
4755 PerlIO_printf(Perl_debug_log,
4756 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4757 (int)(REPORT_CODE_OFF+(depth*2)),"",
4760 state_num = CURLYM_B_fail;
4761 goto reenter_switch;
4765 /* mark current A as captured */
4766 I32 paren = ST.me->flags;
4768 PL_regoffs[paren].start
4769 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4770 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4771 /*dmq: *PL_reglastcloseparen = paren; */
4774 PL_regoffs[paren].end = -1;
4775 if (cur_eval && cur_eval->u.eval.close_paren &&
4776 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4785 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4788 case CURLYM_B_fail: /* just failed to match a B */
4789 REGCP_UNWIND(ST.cp);
4791 I32 max = ARG2(ST.me);
4792 if (max != REG_INFTY && ST.count == max)
4794 goto curlym_do_A; /* try to match a further A */
4796 /* backtrack one A */
4797 if (ST.count == ARG1(ST.me) /* min */)
4800 locinput = HOPc(locinput, -ST.alen);
4801 goto curlym_do_B; /* try to match B */
4804 #define ST st->u.curly
4806 #define CURLY_SETPAREN(paren, success) \
4809 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4810 PL_regoffs[paren].end = locinput - PL_bostr; \
4811 *PL_reglastcloseparen = paren; \
4814 PL_regoffs[paren].end = -1; \
4817 case STAR: /* /A*B/ where A is width 1 */
4821 scan = NEXTOPER(scan);
4823 case PLUS: /* /A+B/ where A is width 1 */
4827 scan = NEXTOPER(scan);
4829 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4830 ST.paren = scan->flags; /* Which paren to set */
4831 if (ST.paren > PL_regsize)
4832 PL_regsize = ST.paren;
4833 if (ST.paren > *PL_reglastparen)
4834 *PL_reglastparen = ST.paren;
4835 ST.min = ARG1(scan); /* min to match */
4836 ST.max = ARG2(scan); /* max to match */
4837 if (cur_eval && cur_eval->u.eval.close_paren &&
4838 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4842 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4844 case CURLY: /* /A{m,n}B/ where A is width 1 */
4846 ST.min = ARG1(scan); /* min to match */
4847 ST.max = ARG2(scan); /* max to match */
4848 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4851 * Lookahead to avoid useless match attempts
4852 * when we know what character comes next.
4854 * Used to only do .*x and .*?x, but now it allows
4855 * for )'s, ('s and (?{ ... })'s to be in the way
4856 * of the quantifier and the EXACT-like node. -- japhy
4859 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4861 if (HAS_TEXT(next) || JUMPABLE(next)) {
4863 regnode *text_node = next;
4865 if (! HAS_TEXT(text_node))
4866 FIND_NEXT_IMPT(text_node);
4868 if (! HAS_TEXT(text_node))
4869 ST.c1 = ST.c2 = CHRTEST_VOID;
4871 if ( PL_regkind[OP(text_node)] != EXACT ) {
4872 ST.c1 = ST.c2 = CHRTEST_VOID;
4873 goto assume_ok_easy;
4876 s = (U8*)STRING(text_node);
4878 /* Currently we only get here when
4880 PL_rekind[OP(text_node)] == EXACT
4882 if this changes back then the macro for IS_TEXT and
4883 friends need to change. */
4886 if (IS_TEXTF(text_node))
4887 ST.c2 = PL_fold[ST.c1];
4888 else if (IS_TEXTFL(text_node))
4889 ST.c2 = PL_fold_locale[ST.c1];
4892 if (IS_TEXTF(text_node)) {
4893 STRLEN ulen1, ulen2;
4894 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4895 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4897 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4898 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4900 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4902 0 : UTF8_ALLOW_ANY);
4903 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4905 0 : UTF8_ALLOW_ANY);
4907 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4909 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4914 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4921 ST.c1 = ST.c2 = CHRTEST_VOID;
4926 PL_reginput = locinput;
4929 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4932 locinput = PL_reginput;
4934 if (ST.c1 == CHRTEST_VOID)
4935 goto curly_try_B_min;
4937 ST.oldloc = locinput;
4939 /* set ST.maxpos to the furthest point along the
4940 * string that could possibly match */
4941 if (ST.max == REG_INFTY) {
4942 ST.maxpos = PL_regeol - 1;
4944 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4948 int m = ST.max - ST.min;
4949 for (ST.maxpos = locinput;
4950 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4951 ST.maxpos += UTF8SKIP(ST.maxpos);
4954 ST.maxpos = locinput + ST.max - ST.min;
4955 if (ST.maxpos >= PL_regeol)
4956 ST.maxpos = PL_regeol - 1;
4958 goto curly_try_B_min_known;
4962 ST.count = regrepeat(rex, ST.A, ST.max, depth);
4963 locinput = PL_reginput;
4964 if (ST.count < ST.min)
4966 if ((ST.count > ST.min)
4967 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4969 /* A{m,n} must come at the end of the string, there's
4970 * no point in backing off ... */
4972 /* ...except that $ and \Z can match before *and* after
4973 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4974 We may back off by one in this case. */
4975 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4979 goto curly_try_B_max;
4984 case CURLY_B_min_known_fail:
4985 /* failed to find B in a non-greedy match where c1,c2 valid */
4986 if (ST.paren && ST.count)
4987 PL_regoffs[ST.paren].end = -1;
4989 PL_reginput = locinput; /* Could be reset... */
4990 REGCP_UNWIND(ST.cp);
4991 /* Couldn't or didn't -- move forward. */
4992 ST.oldloc = locinput;
4994 locinput += UTF8SKIP(locinput);
4998 curly_try_B_min_known:
4999 /* find the next place where 'B' could work, then call B */
5003 n = (ST.oldloc == locinput) ? 0 : 1;
5004 if (ST.c1 == ST.c2) {
5006 /* set n to utf8_distance(oldloc, locinput) */
5007 while (locinput <= ST.maxpos &&
5008 utf8n_to_uvchr((U8*)locinput,
5009 UTF8_MAXBYTES, &len,
5010 uniflags) != (UV)ST.c1) {
5016 /* set n to utf8_distance(oldloc, locinput) */
5017 while (locinput <= ST.maxpos) {
5019 const UV c = utf8n_to_uvchr((U8*)locinput,
5020 UTF8_MAXBYTES, &len,
5022 if (c == (UV)ST.c1 || c == (UV)ST.c2)
5030 if (ST.c1 == ST.c2) {
5031 while (locinput <= ST.maxpos &&
5032 UCHARAT(locinput) != ST.c1)
5036 while (locinput <= ST.maxpos
5037 && UCHARAT(locinput) != ST.c1
5038 && UCHARAT(locinput) != ST.c2)
5041 n = locinput - ST.oldloc;
5043 if (locinput > ST.maxpos)
5045 /* PL_reginput == oldloc now */
5048 if (regrepeat(rex, ST.A, n, depth) < n)
5051 PL_reginput = locinput;
5052 CURLY_SETPAREN(ST.paren, ST.count);
5053 if (cur_eval && cur_eval->u.eval.close_paren &&
5054 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5057 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5062 case CURLY_B_min_fail:
5063 /* failed to find B in a non-greedy match where c1,c2 invalid */
5064 if (ST.paren && ST.count)
5065 PL_regoffs[ST.paren].end = -1;
5067 REGCP_UNWIND(ST.cp);
5068 /* failed -- move forward one */
5069 PL_reginput = locinput;
5070 if (regrepeat(rex, ST.A, 1, depth)) {
5072 locinput = PL_reginput;
5073 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5074 ST.count > 0)) /* count overflow ? */
5077 CURLY_SETPAREN(ST.paren, ST.count);
5078 if (cur_eval && cur_eval->u.eval.close_paren &&
5079 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5082 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5090 /* a successful greedy match: now try to match B */
5091 if (cur_eval && cur_eval->u.eval.close_paren &&
5092 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5097 if (ST.c1 != CHRTEST_VOID)
5098 c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput,
5099 UTF8_MAXBYTES, 0, uniflags)
5100 : (UV) UCHARAT(PL_reginput);
5101 /* If it could work, try it. */
5102 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5103 CURLY_SETPAREN(ST.paren, ST.count);
5104 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5109 case CURLY_B_max_fail:
5110 /* failed to find B in a greedy match */
5111 if (ST.paren && ST.count)
5112 PL_regoffs[ST.paren].end = -1;
5114 REGCP_UNWIND(ST.cp);
5116 if (--ST.count < ST.min)
5118 PL_reginput = locinput = HOPc(locinput, -1);
5119 goto curly_try_B_max;
5126 /* we've just finished A in /(??{A})B/; now continue with B */
5128 st->u.eval.toggle_reg_flags
5129 = cur_eval->u.eval.toggle_reg_flags;
5130 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5132 st->u.eval.prev_rex = rex_sv; /* inner */
5133 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5134 rex = (struct regexp *)SvANY(rex_sv);
5135 rexi = RXi_GET(rex);
5136 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5137 ReREFCNT_inc(rex_sv);
5138 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
5140 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5141 PL_reglastparen = &rex->lastparen;
5142 PL_reglastcloseparen = &rex->lastcloseparen;
5144 REGCP_SET(st->u.eval.lastcp);
5145 PL_reginput = locinput;
5147 /* Restore parens of the outer rex without popping the
5149 tmpix = PL_savestack_ix;
5150 PL_savestack_ix = cur_eval->u.eval.lastcp;
5152 PL_savestack_ix = tmpix;
5154 st->u.eval.prev_eval = cur_eval;
5155 cur_eval = cur_eval->u.eval.prev_eval;
5157 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5158 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5159 if ( nochange_depth )
5162 PUSH_YES_STATE_GOTO(EVAL_AB,
5163 st->u.eval.prev_eval->u.eval.B); /* match B */
5166 if (locinput < reginfo->till) {
5167 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5168 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5170 (long)(locinput - PL_reg_starttry),
5171 (long)(reginfo->till - PL_reg_starttry),
5174 sayNO_SILENT; /* Cannot match: too short. */
5176 PL_reginput = locinput; /* put where regtry can find it */
5177 sayYES; /* Success! */
5179 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5181 PerlIO_printf(Perl_debug_log,
5182 "%*s %ssubpattern success...%s\n",
5183 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5184 PL_reginput = locinput; /* put where regtry can find it */
5185 sayYES; /* Success! */
5188 #define ST st->u.ifmatch
5190 case SUSPEND: /* (?>A) */
5192 PL_reginput = locinput;
5195 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5197 goto ifmatch_trivial_fail_test;
5199 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5201 ifmatch_trivial_fail_test:
5203 char * const s = HOPBACKc(locinput, scan->flags);
5208 sw = 1 - cBOOL(ST.wanted);
5212 next = scan + ARG(scan);
5220 PL_reginput = locinput;
5224 ST.logical = logical;
5225 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5227 /* execute body of (?...A) */
5228 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5231 case IFMATCH_A_fail: /* body of (?...A) failed */
5232 ST.wanted = !ST.wanted;
5235 case IFMATCH_A: /* body of (?...A) succeeded */
5237 sw = cBOOL(ST.wanted);
5239 else if (!ST.wanted)
5242 if (OP(ST.me) == SUSPEND)
5243 locinput = PL_reginput;
5245 locinput = PL_reginput = st->locinput;
5246 nextchr = UCHARAT(locinput);
5248 scan = ST.me + ARG(ST.me);
5251 continue; /* execute B */
5256 next = scan + ARG(scan);
5261 reginfo->cutpoint = PL_regeol;
5264 PL_reginput = locinput;
5266 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5267 PUSH_STATE_GOTO(COMMIT_next,next);
5269 case COMMIT_next_fail:
5276 #define ST st->u.mark
5278 ST.prev_mark = mark_state;
5279 ST.mark_name = sv_commit = sv_yes_mark
5280 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5282 ST.mark_loc = PL_reginput = locinput;
5283 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5285 case MARKPOINT_next:
5286 mark_state = ST.prev_mark;
5289 case MARKPOINT_next_fail:
5290 if (popmark && sv_eq(ST.mark_name,popmark))
5292 if (ST.mark_loc > startpoint)
5293 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5294 popmark = NULL; /* we found our mark */
5295 sv_commit = ST.mark_name;
5298 PerlIO_printf(Perl_debug_log,
5299 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5300 REPORT_CODE_OFF+depth*2, "",
5301 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5304 mark_state = ST.prev_mark;
5305 sv_yes_mark = mark_state ?
5306 mark_state->u.mark.mark_name : NULL;
5310 PL_reginput = locinput;
5312 /* (*SKIP) : if we fail we cut here*/
5313 ST.mark_name = NULL;
5314 ST.mark_loc = locinput;
5315 PUSH_STATE_GOTO(SKIP_next,next);
5317 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5318 otherwise do nothing. Meaning we need to scan
5320 regmatch_state *cur = mark_state;
5321 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5324 if ( sv_eq( cur->u.mark.mark_name,
5327 ST.mark_name = find;
5328 PUSH_STATE_GOTO( SKIP_next, next );
5330 cur = cur->u.mark.prev_mark;
5333 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5335 case SKIP_next_fail:
5337 /* (*CUT:NAME) - Set up to search for the name as we
5338 collapse the stack*/
5339 popmark = ST.mark_name;
5341 /* (*CUT) - No name, we cut here.*/
5342 if (ST.mark_loc > startpoint)
5343 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5344 /* but we set sv_commit to latest mark_name if there
5345 is one so they can test to see how things lead to this
5348 sv_commit=mark_state->u.mark.mark_name;
5356 if ( n == (U32)what_len_TRICKYFOLD(locinput,do_utf8,ln) ) {
5358 } else if ( 0xDF == n && !do_utf8 && !UTF ) {
5361 U8 folded[UTF8_MAXBYTES_CASE+1];
5363 const char * const l = locinput;
5364 char *e = PL_regeol;
5365 to_uni_fold(n, folded, &foldlen);
5367 if (ibcmp_utf8((const char*) folded, 0, foldlen, 1,
5368 l, &e, 0, do_utf8)) {
5373 nextchr = UCHARAT(locinput);
5376 if ((n=is_LNBREAK(locinput,do_utf8))) {
5378 nextchr = UCHARAT(locinput);
5383 #define CASE_CLASS(nAmE) \
5385 if ((n=is_##nAmE(locinput,do_utf8))) { \
5387 nextchr = UCHARAT(locinput); \
5392 if ((n=is_##nAmE(locinput,do_utf8))) { \
5395 locinput += UTF8SKIP(locinput); \
5396 nextchr = UCHARAT(locinput); \
5401 CASE_CLASS(HORIZWS);
5405 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5406 PTR2UV(scan), OP(scan));
5407 Perl_croak(aTHX_ "regexp memory corruption");
5411 /* switch break jumps here */
5412 scan = next; /* prepare to execute the next op and ... */
5413 continue; /* ... jump back to the top, reusing st */
5417 /* push a state that backtracks on success */
5418 st->u.yes.prev_yes_state = yes_state;
5422 /* push a new regex state, then continue at scan */
5424 regmatch_state *newst;
5427 regmatch_state *cur = st;
5428 regmatch_state *curyes = yes_state;
5430 regmatch_slab *slab = PL_regmatch_slab;
5431 for (;curd > -1;cur--,curd--) {
5432 if (cur < SLAB_FIRST(slab)) {
5434 cur = SLAB_LAST(slab);
5436 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5437 REPORT_CODE_OFF + 2 + depth * 2,"",
5438 curd, PL_reg_name[cur->resume_state],
5439 (curyes == cur) ? "yes" : ""
5442 curyes = cur->u.yes.prev_yes_state;
5445 DEBUG_STATE_pp("push")
5448 st->locinput = locinput;
5450 if (newst > SLAB_LAST(PL_regmatch_slab))
5451 newst = S_push_slab(aTHX);
5452 PL_regmatch_state = newst;
5454 locinput = PL_reginput;
5455 nextchr = UCHARAT(locinput);
5463 * We get here only if there's trouble -- normally "case END" is
5464 * the terminating point.
5466 Perl_croak(aTHX_ "corrupted regexp pointers");
5472 /* we have successfully completed a subexpression, but we must now
5473 * pop to the state marked by yes_state and continue from there */
5474 assert(st != yes_state);
5476 while (st != yes_state) {
5478 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5479 PL_regmatch_slab = PL_regmatch_slab->prev;
5480 st = SLAB_LAST(PL_regmatch_slab);
5484 DEBUG_STATE_pp("pop (no final)");
5486 DEBUG_STATE_pp("pop (yes)");
5492 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5493 || yes_state > SLAB_LAST(PL_regmatch_slab))
5495 /* not in this slab, pop slab */
5496 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5497 PL_regmatch_slab = PL_regmatch_slab->prev;
5498 st = SLAB_LAST(PL_regmatch_slab);
5500 depth -= (st - yes_state);
5503 yes_state = st->u.yes.prev_yes_state;
5504 PL_regmatch_state = st;
5507 locinput= st->locinput;
5508 nextchr = UCHARAT(locinput);
5510 state_num = st->resume_state + no_final;
5511 goto reenter_switch;
5514 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5515 PL_colors[4], PL_colors[5]));
5517 if (PL_reg_eval_set) {
5518 /* each successfully executed (?{...}) block does the equivalent of
5519 * local $^R = do {...}
5520 * When popping the save stack, all these locals would be undone;
5521 * bypass this by setting the outermost saved $^R to the latest
5523 if (oreplsv != GvSV(PL_replgv))
5524 sv_setsv(oreplsv, GvSV(PL_replgv));
5531 PerlIO_printf(Perl_debug_log,
5532 "%*s %sfailed...%s\n",
5533 REPORT_CODE_OFF+depth*2, "",
5534 PL_colors[4], PL_colors[5])
5546 /* there's a previous state to backtrack to */
5548 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5549 PL_regmatch_slab = PL_regmatch_slab->prev;
5550 st = SLAB_LAST(PL_regmatch_slab);
5552 PL_regmatch_state = st;
5553 locinput= st->locinput;
5554 nextchr = UCHARAT(locinput);
5556 DEBUG_STATE_pp("pop");
5558 if (yes_state == st)
5559 yes_state = st->u.yes.prev_yes_state;
5561 state_num = st->resume_state + 1; /* failure = success + 1 */
5562 goto reenter_switch;
5567 if (rex->intflags & PREGf_VERBARG_SEEN) {
5568 SV *sv_err = get_sv("REGERROR", 1);
5569 SV *sv_mrk = get_sv("REGMARK", 1);
5571 sv_commit = &PL_sv_no;
5573 sv_yes_mark = &PL_sv_yes;
5576 sv_commit = &PL_sv_yes;
5577 sv_yes_mark = &PL_sv_no;
5579 sv_setsv(sv_err, sv_commit);
5580 sv_setsv(sv_mrk, sv_yes_mark);
5583 /* clean up; in particular, free all slabs above current one */
5584 LEAVE_SCOPE(oldsave);
5590 - regrepeat - repeatedly match something simple, report how many
5593 * [This routine now assumes that it will only match on things of length 1.
5594 * That was true before, but now we assume scan - reginput is the count,
5595 * rather than incrementing count on every character. [Er, except utf8.]]
5598 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5601 register char *scan;
5603 register char *loceol = PL_regeol;
5604 register I32 hardcount = 0;
5605 register bool do_utf8 = PL_reg_match_utf8;
5607 PERL_UNUSED_ARG(depth);
5610 PERL_ARGS_ASSERT_REGREPEAT;
5613 if (max == REG_INFTY)
5615 else if (max < loceol - scan)
5616 loceol = scan + max;
5621 while (scan < loceol && hardcount < max && *scan != '\n') {
5622 scan += UTF8SKIP(scan);
5626 while (scan < loceol && *scan != '\n')
5633 while (scan < loceol && hardcount < max) {
5634 scan += UTF8SKIP(scan);
5644 case EXACT: /* length of string is 1 */
5646 while (scan < loceol && UCHARAT(scan) == c)
5649 case EXACTF: /* length of string is 1 */
5651 while (scan < loceol &&
5652 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5655 case EXACTFL: /* length of string is 1 */
5656 PL_reg_flags |= RF_tainted;
5658 while (scan < loceol &&
5659 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5665 while (hardcount < max && scan < loceol &&
5666 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
5667 scan += UTF8SKIP(scan);
5671 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5678 LOAD_UTF8_CHARCLASS_ALNUM();
5679 while (hardcount < max && scan < loceol &&
5680 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5681 scan += UTF8SKIP(scan);
5685 while (scan < loceol && isALNUM(*scan))
5690 PL_reg_flags |= RF_tainted;
5693 while (hardcount < max && scan < loceol &&
5694 isALNUM_LC_utf8((U8*)scan)) {
5695 scan += UTF8SKIP(scan);
5699 while (scan < loceol && isALNUM_LC(*scan))
5706 LOAD_UTF8_CHARCLASS_ALNUM();
5707 while (hardcount < max && scan < loceol &&
5708 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5709 scan += UTF8SKIP(scan);
5713 while (scan < loceol && !isALNUM(*scan))
5718 PL_reg_flags |= RF_tainted;
5721 while (hardcount < max && scan < loceol &&
5722 !isALNUM_LC_utf8((U8*)scan)) {
5723 scan += UTF8SKIP(scan);
5727 while (scan < loceol && !isALNUM_LC(*scan))
5734 LOAD_UTF8_CHARCLASS_SPACE();
5735 while (hardcount < max && scan < loceol &&
5737 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5738 scan += UTF8SKIP(scan);
5742 while (scan < loceol && isSPACE(*scan))
5747 PL_reg_flags |= RF_tainted;
5750 while (hardcount < max && scan < loceol &&
5751 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5752 scan += UTF8SKIP(scan);
5756 while (scan < loceol && isSPACE_LC(*scan))
5763 LOAD_UTF8_CHARCLASS_SPACE();
5764 while (hardcount < max && scan < loceol &&
5766 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5767 scan += UTF8SKIP(scan);
5771 while (scan < loceol && !isSPACE(*scan))
5776 PL_reg_flags |= RF_tainted;
5779 while (hardcount < max && scan < loceol &&
5780 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5781 scan += UTF8SKIP(scan);
5785 while (scan < loceol && !isSPACE_LC(*scan))
5792 LOAD_UTF8_CHARCLASS_DIGIT();
5793 while (hardcount < max && scan < loceol &&
5794 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5795 scan += UTF8SKIP(scan);
5799 while (scan < loceol && isDIGIT(*scan))
5806 LOAD_UTF8_CHARCLASS_DIGIT();
5807 while (hardcount < max && scan < loceol &&
5808 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5809 scan += UTF8SKIP(scan);
5813 while (scan < loceol && !isDIGIT(*scan))
5819 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5825 LNBREAK can match two latin chars, which is ok,
5826 because we have a null terminated string, but we
5827 have to use hardcount in this situation
5829 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5838 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5843 while (scan < loceol && is_HORIZWS_latin1(scan))
5850 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5851 scan += UTF8SKIP(scan);
5855 while (scan < loceol && !is_HORIZWS_latin1(scan))
5863 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5868 while (scan < loceol && is_VERTWS_latin1(scan))
5876 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5877 scan += UTF8SKIP(scan);
5881 while (scan < loceol && !is_VERTWS_latin1(scan))
5887 default: /* Called on something of 0 width. */
5888 break; /* So match right here or not at all. */
5894 c = scan - PL_reginput;
5898 GET_RE_DEBUG_FLAGS_DECL;
5900 SV * const prop = sv_newmortal();
5901 regprop(prog, prop, p);
5902 PerlIO_printf(Perl_debug_log,
5903 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5904 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5912 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5914 - regclass_swash - prepare the utf8 swash
5918 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5924 RXi_GET_DECL(prog,progi);
5925 const struct reg_data * const data = prog ? progi->data : NULL;
5927 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5929 if (data && data->count) {
5930 const U32 n = ARG(node);
5932 if (data->what[n] == 's') {
5933 SV * const rv = MUTABLE_SV(data->data[n]);
5934 AV * const av = MUTABLE_AV(SvRV(rv));
5935 SV **const ary = AvARRAY(av);
5938 /* See the end of regcomp.c:S_regclass() for
5939 * documentation of these array elements. */
5942 a = SvROK(ary[1]) ? &ary[1] : NULL;
5943 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5947 else if (si && doinit) {
5948 sw = swash_init("utf8", "", si, 1, 0);
5949 (void)av_store(av, 1, sw);
5966 - reginclass - determine if a character falls into a character class
5968 The n is the ANYOF regnode, the p is the target string, lenp
5969 is pointer to the maximum length of how far to go in the p
5970 (if the lenp is zero, UTF8SKIP(p) is used),
5971 do_utf8 tells whether the target string is in UTF-8.
5976 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
5979 const char flags = ANYOF_FLAGS(n);
5985 PERL_ARGS_ASSERT_REGINCLASS;
5987 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
5988 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5989 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
5990 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
5991 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
5992 * UTF8_ALLOW_FFFF */
5993 if (len == (STRLEN)-1)
5994 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
5997 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
5998 if (do_utf8 || (flags & ANYOF_UNICODE)) {
6001 if (do_utf8 && !ANYOF_RUNTIME(n)) {
6002 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
6005 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
6009 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
6017 utf8_p = bytes_to_utf8(p, &len);
6019 if (swash_fetch(sw, utf8_p, 1))
6021 else if (flags & ANYOF_FOLD) {
6022 if (!match && lenp && av) {
6024 for (i = 0; i <= av_len(av); i++) {
6025 SV* const sv = *av_fetch(av, i, FALSE);
6027 const char * const s = SvPV_const(sv, len);
6028 if (len <= plen && memEQ(s, (char*)utf8_p, len)) {
6036 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
6039 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
6040 if (swash_fetch(sw, tmpbuf, 1))
6045 /* If we allocated a string above, free it */
6046 if (! do_utf8) Safefree(utf8_p);
6049 if (match && lenp && *lenp == 0)
6050 *lenp = UNISKIP(NATIVE_TO_UNI(c));
6052 if (!match && c < 256) {
6053 if (ANYOF_BITMAP_TEST(n, c))
6055 else if (flags & ANYOF_FOLD) {
6058 if (flags & ANYOF_LOCALE) {
6059 PL_reg_flags |= RF_tainted;
6060 f = PL_fold_locale[c];
6064 if (f != c && ANYOF_BITMAP_TEST(n, f))
6068 if (!match && (flags & ANYOF_CLASS)) {
6069 PL_reg_flags |= RF_tainted;
6071 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6072 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6073 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6074 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6075 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6076 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6077 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6078 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6079 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6080 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6081 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6082 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6083 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6084 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6085 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6086 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6087 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6088 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6089 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6090 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6091 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6092 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6093 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6094 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6095 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6096 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6097 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6098 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6099 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6100 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6101 ) /* How's that for a conditional? */
6108 return (flags & ANYOF_INVERT) ? !match : match;
6112 S_reghop3(U8 *s, I32 off, const U8* lim)
6116 PERL_ARGS_ASSERT_REGHOP3;
6119 while (off-- && s < lim) {
6120 /* XXX could check well-formedness here */
6125 while (off++ && s > lim) {
6127 if (UTF8_IS_CONTINUED(*s)) {
6128 while (s > lim && UTF8_IS_CONTINUATION(*s))
6131 /* XXX could check well-formedness here */
6138 /* there are a bunch of places where we use two reghop3's that should
6139 be replaced with this routine. but since thats not done yet
6140 we ifdef it out - dmq
6143 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6147 PERL_ARGS_ASSERT_REGHOP4;
6150 while (off-- && s < rlim) {
6151 /* XXX could check well-formedness here */
6156 while (off++ && s > llim) {
6158 if (UTF8_IS_CONTINUED(*s)) {
6159 while (s > llim && UTF8_IS_CONTINUATION(*s))
6162 /* XXX could check well-formedness here */
6170 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
6174 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6177 while (off-- && s < lim) {
6178 /* XXX could check well-formedness here */
6185 while (off++ && s > lim) {
6187 if (UTF8_IS_CONTINUED(*s)) {
6188 while (s > lim && UTF8_IS_CONTINUATION(*s))
6191 /* XXX could check well-formedness here */
6200 restore_pos(pTHX_ void *arg)
6203 regexp * const rex = (regexp *)arg;
6204 if (PL_reg_eval_set) {
6205 if (PL_reg_oldsaved) {
6206 rex->subbeg = PL_reg_oldsaved;
6207 rex->sublen = PL_reg_oldsavedlen;
6208 #ifdef PERL_OLD_COPY_ON_WRITE
6209 rex->saved_copy = PL_nrs;
6211 RXp_MATCH_COPIED_on(rex);
6213 PL_reg_magic->mg_len = PL_reg_oldpos;
6214 PL_reg_eval_set = 0;
6215 PL_curpm = PL_reg_oldcurpm;
6220 S_to_utf8_substr(pTHX_ register regexp *prog)
6224 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6227 if (prog->substrs->data[i].substr
6228 && !prog->substrs->data[i].utf8_substr) {
6229 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6230 prog->substrs->data[i].utf8_substr = sv;
6231 sv_utf8_upgrade(sv);
6232 if (SvVALID(prog->substrs->data[i].substr)) {
6233 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
6234 if (flags & FBMcf_TAIL) {
6235 /* Trim the trailing \n that fbm_compile added last
6237 SvCUR_set(sv, SvCUR(sv) - 1);
6238 /* Whilst this makes the SV technically "invalid" (as its
6239 buffer is no longer followed by "\0") when fbm_compile()
6240 adds the "\n" back, a "\0" is restored. */
6242 fbm_compile(sv, flags);
6244 if (prog->substrs->data[i].substr == prog->check_substr)
6245 prog->check_utf8 = sv;
6251 S_to_byte_substr(pTHX_ register regexp *prog)
6256 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6259 if (prog->substrs->data[i].utf8_substr
6260 && !prog->substrs->data[i].substr) {
6261 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6262 if (sv_utf8_downgrade(sv, TRUE)) {
6263 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6265 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6266 if (flags & FBMcf_TAIL) {
6267 /* Trim the trailing \n that fbm_compile added last
6269 SvCUR_set(sv, SvCUR(sv) - 1);
6271 fbm_compile(sv, flags);
6277 prog->substrs->data[i].substr = sv;
6278 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6279 prog->check_substr = sv;
6286 * c-indentation-style: bsd
6288 * indent-tabs-mode: t
6291 * ex: set ts=8 sts=4 sw=4 noet: