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_PATTERN ((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) (utf8_target ? 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 (utf8_target && 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, utf8_target)) \
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 (utf8_target && 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, utf8_target)) \
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 (%lu-%ld)",
321 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
323 SSGROW(total_elems + REGCP_FRAME_ELEMS);
325 for (p = PL_regsize; p > parenfloor; p--) {
326 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
327 SSPUSHINT(PL_regoffs[p].end);
328 SSPUSHINT(PL_regoffs[p].start);
329 SSPUSHPTR(PL_reg_start_tmp[p]);
331 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
332 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
333 (UV)p, (IV)PL_regoffs[p].start,
334 (IV)(PL_reg_start_tmp[p] - PL_bostr),
335 (IV)PL_regoffs[p].end
338 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
339 SSPUSHPTR(PL_regoffs);
340 SSPUSHINT(PL_regsize);
341 SSPUSHINT(*PL_reglastparen);
342 SSPUSHINT(*PL_reglastcloseparen);
343 SSPUSHPTR(PL_reginput);
344 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
349 /* These are needed since we do not localize EVAL nodes: */
350 #define REGCP_SET(cp) \
352 PerlIO_printf(Perl_debug_log, \
353 " Setting an EVAL scope, savestack=%"IVdf"\n", \
354 (IV)PL_savestack_ix)); \
357 #define REGCP_UNWIND(cp) \
359 if (cp != PL_savestack_ix) \
360 PerlIO_printf(Perl_debug_log, \
361 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
362 (IV)(cp), (IV)PL_savestack_ix)); \
366 S_regcppop(pTHX_ const regexp *rex)
371 GET_RE_DEBUG_FLAGS_DECL;
373 PERL_ARGS_ASSERT_REGCPPOP;
375 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
377 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
378 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
379 input = (char *) SSPOPPTR;
380 *PL_reglastcloseparen = SSPOPINT;
381 *PL_reglastparen = SSPOPINT;
382 PL_regsize = SSPOPINT;
383 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
385 i -= REGCP_OTHER_ELEMS;
386 /* Now restore the parentheses context. */
387 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
389 U32 paren = (U32)SSPOPINT;
390 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
391 PL_regoffs[paren].start = SSPOPINT;
393 if (paren <= *PL_reglastparen)
394 PL_regoffs[paren].end = tmps;
396 PerlIO_printf(Perl_debug_log,
397 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
398 (UV)paren, (IV)PL_regoffs[paren].start,
399 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
400 (IV)PL_regoffs[paren].end,
401 (paren > *PL_reglastparen ? "(no)" : ""));
405 if (*PL_reglastparen + 1 <= rex->nparens) {
406 PerlIO_printf(Perl_debug_log,
407 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
408 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
412 /* It would seem that the similar code in regtry()
413 * already takes care of this, and in fact it is in
414 * a better location to since this code can #if 0-ed out
415 * but the code in regtry() is needed or otherwise tests
416 * requiring null fields (pat.t#187 and split.t#{13,14}
417 * (as of patchlevel 7877) will fail. Then again,
418 * this code seems to be necessary or otherwise
419 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
420 * --jhi updated by dapm */
421 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
423 PL_regoffs[i].start = -1;
424 PL_regoffs[i].end = -1;
430 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
433 * pregexec and friends
436 #ifndef PERL_IN_XSUB_RE
438 - pregexec - match a regexp against a string
441 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
442 char *strbeg, I32 minend, SV *screamer, U32 nosave)
443 /* strend: pointer to null at end of string */
444 /* strbeg: real beginning of string */
445 /* minend: end of match must be >=minend after stringarg. */
446 /* nosave: For optimizations. */
448 PERL_ARGS_ASSERT_PREGEXEC;
451 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
452 nosave ? 0 : REXEC_COPY_STR);
457 * Need to implement the following flags for reg_anch:
459 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
461 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
462 * INTUIT_AUTORITATIVE_ML
463 * INTUIT_ONCE_NOML - Intuit can match in one location only.
466 * Another flag for this function: SECOND_TIME (so that float substrs
467 * with giant delta may be not rechecked).
470 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
472 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
473 Otherwise, only SvCUR(sv) is used to get strbeg. */
475 /* XXXX We assume that strpos is strbeg unless sv. */
477 /* XXXX Some places assume that there is a fixed substring.
478 An update may be needed if optimizer marks as "INTUITable"
479 RExen without fixed substrings. Similarly, it is assumed that
480 lengths of all the strings are no more than minlen, thus they
481 cannot come from lookahead.
482 (Or minlen should take into account lookahead.)
483 NOTE: Some of this comment is not correct. minlen does now take account
484 of lookahead/behind. Further research is required. -- demerphq
488 /* A failure to find a constant substring means that there is no need to make
489 an expensive call to REx engine, thus we celebrate a failure. Similarly,
490 finding a substring too deep into the string means that less calls to
491 regtry() should be needed.
493 REx compiler's optimizer found 4 possible hints:
494 a) Anchored substring;
496 c) Whether we are anchored (beginning-of-line or \G);
497 d) First node (of those at offset 0) which may distingush positions;
498 We use a)b)d) and multiline-part of c), and try to find a position in the
499 string which does not contradict any of them.
502 /* Most of decisions we do here should have been done at compile time.
503 The nodes of the REx which we used for the search should have been
504 deleted from the finite automaton. */
507 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
508 char *strend, const U32 flags, re_scream_pos_data *data)
511 struct regexp *const prog = (struct regexp *)SvANY(rx);
512 register I32 start_shift = 0;
513 /* Should be nonnegative! */
514 register I32 end_shift = 0;
519 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
521 register char *other_last = NULL; /* other substr checked before this */
522 char *check_at = NULL; /* check substr found at this pos */
523 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
524 RXi_GET_DECL(prog,progi);
526 const char * const i_strpos = strpos;
528 GET_RE_DEBUG_FLAGS_DECL;
530 PERL_ARGS_ASSERT_RE_INTUIT_START;
532 RX_MATCH_UTF8_set(rx,utf8_target);
535 PL_reg_flags |= RF_utf8;
538 debug_start_match(rx, utf8_target, strpos, strend,
539 sv ? "Guessing start of match in sv for"
540 : "Guessing start of match in string for");
543 /* CHR_DIST() would be more correct here but it makes things slow. */
544 if (prog->minlen > strend - strpos) {
545 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
546 "String too short... [re_intuit_start]\n"));
550 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
553 if (!prog->check_utf8 && prog->check_substr)
554 to_utf8_substr(prog);
555 check = prog->check_utf8;
557 if (!prog->check_substr && prog->check_utf8)
558 to_byte_substr(prog);
559 check = prog->check_substr;
561 if (check == &PL_sv_undef) {
562 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
563 "Non-utf8 string cannot match utf8 check string\n"));
566 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
567 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
568 || ( (prog->extflags & RXf_ANCH_BOL)
569 && !multiline ) ); /* Check after \n? */
572 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
573 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
574 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
576 && (strpos != strbeg)) {
577 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
580 if (prog->check_offset_min == prog->check_offset_max &&
581 !(prog->extflags & RXf_CANY_SEEN)) {
582 /* Substring at constant offset from beg-of-str... */
585 s = HOP3c(strpos, prog->check_offset_min, strend);
588 slen = SvCUR(check); /* >= 1 */
590 if ( strend - s > slen || strend - s < slen - 1
591 || (strend - s == slen && strend[-1] != '\n')) {
592 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
595 /* Now should match s[0..slen-2] */
597 if (slen && (*SvPVX_const(check) != *s
599 && memNE(SvPVX_const(check), s, slen)))) {
601 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
605 else if (*SvPVX_const(check) != *s
606 || ((slen = SvCUR(check)) > 1
607 && memNE(SvPVX_const(check), s, slen)))
610 goto success_at_start;
613 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
615 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
616 end_shift = prog->check_end_shift;
619 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
620 - (SvTAIL(check) != 0);
621 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
623 if (end_shift < eshift)
627 else { /* Can match at random position */
630 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
631 end_shift = prog->check_end_shift;
633 /* end shift should be non negative here */
636 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
638 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
639 (IV)end_shift, RX_PRECOMP(prog));
643 /* Find a possible match in the region s..strend by looking for
644 the "check" substring in the region corrected by start/end_shift. */
647 I32 srch_start_shift = start_shift;
648 I32 srch_end_shift = end_shift;
649 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
650 srch_end_shift -= ((strbeg - s) - srch_start_shift);
651 srch_start_shift = strbeg - s;
653 DEBUG_OPTIMISE_MORE_r({
654 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
655 (IV)prog->check_offset_min,
656 (IV)srch_start_shift,
658 (IV)prog->check_end_shift);
661 if (flags & REXEC_SCREAM) {
662 I32 p = -1; /* Internal iterator of scream. */
663 I32 * const pp = data ? data->scream_pos : &p;
665 if (PL_screamfirst[BmRARE(check)] >= 0
666 || ( BmRARE(check) == '\n'
667 && (BmPREVIOUS(check) == SvCUR(check) - 1)
669 s = screaminstr(sv, check,
670 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
673 /* we may be pointing at the wrong string */
674 if (s && RXp_MATCH_COPIED(prog))
675 s = strbeg + (s - SvPVX_const(sv));
677 *data->scream_olds = s;
682 if (prog->extflags & RXf_CANY_SEEN) {
683 start_point= (U8*)(s + srch_start_shift);
684 end_point= (U8*)(strend - srch_end_shift);
686 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
687 end_point= HOP3(strend, -srch_end_shift, strbeg);
689 DEBUG_OPTIMISE_MORE_r({
690 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
691 (int)(end_point - start_point),
692 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
696 s = fbm_instr( start_point, end_point,
697 check, multiline ? FBMrf_MULTILINE : 0);
700 /* Update the count-of-usability, remove useless subpatterns,
704 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
705 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
706 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
707 (s ? "Found" : "Did not find"),
708 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
709 ? "anchored" : "floating"),
712 (s ? " at offset " : "...\n") );
717 /* Finish the diagnostic message */
718 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
720 /* XXX dmq: first branch is for positive lookbehind...
721 Our check string is offset from the beginning of the pattern.
722 So we need to do any stclass tests offset forward from that
731 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
732 Start with the other substr.
733 XXXX no SCREAM optimization yet - and a very coarse implementation
734 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
735 *always* match. Probably should be marked during compile...
736 Probably it is right to do no SCREAM here...
739 if (utf8_target ? (prog->float_utf8 && prog->anchored_utf8)
740 : (prog->float_substr && prog->anchored_substr))
742 /* Take into account the "other" substring. */
743 /* XXXX May be hopelessly wrong for UTF... */
746 if (check == (utf8_target ? prog->float_utf8 : prog->float_substr)) {
749 char * const last = HOP3c(s, -start_shift, strbeg);
751 char * const saved_s = s;
754 t = s - prog->check_offset_max;
755 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
757 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
762 t = HOP3c(t, prog->anchored_offset, strend);
763 if (t < other_last) /* These positions already checked */
765 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
768 /* XXXX It is not documented what units *_offsets are in.
769 We assume bytes, but this is clearly wrong.
770 Meaning this code needs to be carefully reviewed for errors.
774 /* On end-of-str: see comment below. */
775 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
776 if (must == &PL_sv_undef) {
778 DEBUG_r(must = prog->anchored_utf8); /* for debug */
783 HOP3(HOP3(last1, prog->anchored_offset, strend)
784 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
786 multiline ? FBMrf_MULTILINE : 0
789 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
790 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
791 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
792 (s ? "Found" : "Contradicts"),
793 quoted, RE_SV_TAIL(must));
798 if (last1 >= last2) {
799 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
800 ", giving up...\n"));
803 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
804 ", trying floating at offset %ld...\n",
805 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
806 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
807 s = HOP3c(last, 1, strend);
811 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
812 (long)(s - i_strpos)));
813 t = HOP3c(s, -prog->anchored_offset, strbeg);
814 other_last = HOP3c(s, 1, strend);
822 else { /* Take into account the floating substring. */
824 char * const saved_s = s;
827 t = HOP3c(s, -start_shift, strbeg);
829 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
830 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
831 last = HOP3c(t, prog->float_max_offset, strend);
832 s = HOP3c(t, prog->float_min_offset, strend);
835 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
836 must = utf8_target ? prog->float_utf8 : prog->float_substr;
837 /* fbm_instr() takes into account exact value of end-of-str
838 if the check is SvTAIL(ed). Since false positives are OK,
839 and end-of-str is not later than strend we are OK. */
840 if (must == &PL_sv_undef) {
842 DEBUG_r(must = prog->float_utf8); /* for debug message */
845 s = fbm_instr((unsigned char*)s,
846 (unsigned char*)last + SvCUR(must)
848 must, multiline ? FBMrf_MULTILINE : 0);
850 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
851 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
852 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
853 (s ? "Found" : "Contradicts"),
854 quoted, RE_SV_TAIL(must));
858 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
859 ", giving up...\n"));
862 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
863 ", trying anchored starting at offset %ld...\n",
864 (long)(saved_s + 1 - i_strpos)));
866 s = HOP3c(t, 1, strend);
870 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
871 (long)(s - i_strpos)));
872 other_last = s; /* Fix this later. --Hugo */
882 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
884 DEBUG_OPTIMISE_MORE_r(
885 PerlIO_printf(Perl_debug_log,
886 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
887 (IV)prog->check_offset_min,
888 (IV)prog->check_offset_max,
896 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
898 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
901 /* Fixed substring is found far enough so that the match
902 cannot start at strpos. */
904 if (ml_anch && t[-1] != '\n') {
905 /* Eventually fbm_*() should handle this, but often
906 anchored_offset is not 0, so this check will not be wasted. */
907 /* XXXX In the code below we prefer to look for "^" even in
908 presence of anchored substrings. And we search even
909 beyond the found float position. These pessimizations
910 are historical artefacts only. */
912 while (t < strend - prog->minlen) {
914 if (t < check_at - prog->check_offset_min) {
915 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
916 /* Since we moved from the found position,
917 we definitely contradict the found anchored
918 substr. Due to the above check we do not
919 contradict "check" substr.
920 Thus we can arrive here only if check substr
921 is float. Redo checking for "other"=="fixed".
924 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
925 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
926 goto do_other_anchored;
928 /* We don't contradict the found floating substring. */
929 /* XXXX Why not check for STCLASS? */
931 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
932 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
935 /* Position contradicts check-string */
936 /* XXXX probably better to look for check-string
937 than for "\n", so one should lower the limit for t? */
938 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
939 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
940 other_last = strpos = s = t + 1;
945 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
946 PL_colors[0], PL_colors[1]));
950 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
951 PL_colors[0], PL_colors[1]));
955 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
958 /* The found string does not prohibit matching at strpos,
959 - no optimization of calling REx engine can be performed,
960 unless it was an MBOL and we are not after MBOL,
961 or a future STCLASS check will fail this. */
963 /* Even in this situation we may use MBOL flag if strpos is offset
964 wrt the start of the string. */
965 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
966 && (strpos != strbeg) && strpos[-1] != '\n'
967 /* May be due to an implicit anchor of m{.*foo} */
968 && !(prog->intflags & PREGf_IMPLICIT))
973 DEBUG_EXECUTE_r( if (ml_anch)
974 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
975 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
978 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
980 prog->check_utf8 /* Could be deleted already */
981 && --BmUSEFUL(prog->check_utf8) < 0
982 && (prog->check_utf8 == prog->float_utf8)
984 prog->check_substr /* Could be deleted already */
985 && --BmUSEFUL(prog->check_substr) < 0
986 && (prog->check_substr == prog->float_substr)
989 /* If flags & SOMETHING - do not do it many times on the same match */
990 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
991 /* XXX Does the destruction order has to change with utf8_target? */
992 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
993 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
994 prog->check_substr = prog->check_utf8 = NULL; /* disable */
995 prog->float_substr = prog->float_utf8 = NULL; /* clear */
996 check = NULL; /* abort */
998 /* XXXX If the check string was an implicit check MBOL, then we need to unset the relevent flag
999 see http://bugs.activestate.com/show_bug.cgi?id=87173 */
1000 if (prog->intflags & PREGf_IMPLICIT)
1001 prog->extflags &= ~RXf_ANCH_MBOL;
1002 /* XXXX This is a remnant of the old implementation. It
1003 looks wasteful, since now INTUIT can use many
1004 other heuristics. */
1005 prog->extflags &= ~RXf_USE_INTUIT;
1006 /* XXXX What other flags might need to be cleared in this branch? */
1012 /* Last resort... */
1013 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1014 /* trie stclasses are too expensive to use here, we are better off to
1015 leave it to regmatch itself */
1016 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1017 /* minlen == 0 is possible if regstclass is \b or \B,
1018 and the fixed substr is ''$.
1019 Since minlen is already taken into account, s+1 is before strend;
1020 accidentally, minlen >= 1 guaranties no false positives at s + 1
1021 even for \b or \B. But (minlen? 1 : 0) below assumes that
1022 regstclass does not come from lookahead... */
1023 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1024 This leaves EXACTF only, which is dealt with in find_byclass(). */
1025 const U8* const str = (U8*)STRING(progi->regstclass);
1026 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1027 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1030 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1031 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1032 else if (prog->float_substr || prog->float_utf8)
1033 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1037 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1038 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1041 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1044 const char *what = NULL;
1046 if (endpos == strend) {
1047 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1048 "Could not match STCLASS...\n") );
1051 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1052 "This position contradicts STCLASS...\n") );
1053 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1055 /* Contradict one of substrings */
1056 if (prog->anchored_substr || prog->anchored_utf8) {
1057 if ((utf8_target ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1058 DEBUG_EXECUTE_r( what = "anchored" );
1060 s = HOP3c(t, 1, strend);
1061 if (s + start_shift + end_shift > strend) {
1062 /* XXXX Should be taken into account earlier? */
1063 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1064 "Could not match STCLASS...\n") );
1069 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1070 "Looking for %s substr starting at offset %ld...\n",
1071 what, (long)(s + start_shift - i_strpos)) );
1074 /* Have both, check_string is floating */
1075 if (t + start_shift >= check_at) /* Contradicts floating=check */
1076 goto retry_floating_check;
1077 /* Recheck anchored substring, but not floating... */
1081 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1082 "Looking for anchored substr starting at offset %ld...\n",
1083 (long)(other_last - i_strpos)) );
1084 goto do_other_anchored;
1086 /* Another way we could have checked stclass at the
1087 current position only: */
1092 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1093 "Looking for /%s^%s/m starting at offset %ld...\n",
1094 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1097 if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1099 /* Check is floating subtring. */
1100 retry_floating_check:
1101 t = check_at - start_shift;
1102 DEBUG_EXECUTE_r( what = "floating" );
1103 goto hop_and_restart;
1106 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1107 "By STCLASS: moving %ld --> %ld\n",
1108 (long)(t - i_strpos), (long)(s - i_strpos))
1112 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1113 "Does not contradict STCLASS...\n");
1118 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1119 PL_colors[4], (check ? "Guessed" : "Giving up"),
1120 PL_colors[5], (long)(s - i_strpos)) );
1123 fail_finish: /* Substring not found */
1124 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1125 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1127 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1128 PL_colors[4], PL_colors[5]));
1132 #define DECL_TRIE_TYPE(scan) \
1133 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1134 trie_type = (scan->flags != EXACT) \
1135 ? (utf8_target ? trie_utf8_fold : (UTF_PATTERN ? trie_latin_utf8_fold : trie_plain)) \
1136 : (utf8_target ? trie_utf8 : trie_plain)
1138 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1139 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1140 switch (trie_type) { \
1141 case trie_utf8_fold: \
1142 if ( foldlen>0 ) { \
1143 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1148 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1149 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1150 foldlen -= UNISKIP( uvc ); \
1151 uscan = foldbuf + UNISKIP( uvc ); \
1154 case trie_latin_utf8_fold: \
1155 if ( foldlen>0 ) { \
1156 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1162 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1163 foldlen -= UNISKIP( uvc ); \
1164 uscan = foldbuf + UNISKIP( uvc ); \
1168 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1175 charid = trie->charmap[ uvc ]; \
1179 if (widecharmap) { \
1180 SV** const svpp = hv_fetch(widecharmap, \
1181 (char*)&uvc, sizeof(UV), 0); \
1183 charid = (U16)SvIV(*svpp); \
1188 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1190 char *my_strend= (char *)strend; \
1193 foldEQ_utf8(s, &my_strend, 0, utf8_target, \
1194 m, NULL, ln, cBOOL(UTF_PATTERN))) \
1195 && (!reginfo || regtry(reginfo, &s)) ) \
1198 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1199 uvchr_to_utf8(tmpbuf, c); \
1200 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1202 && (f == c1 || f == c2) \
1204 foldEQ_utf8(s, &my_strend, 0, utf8_target,\
1205 m, NULL, ln, cBOOL(UTF_PATTERN)))\
1206 && (!reginfo || regtry(reginfo, &s)) ) \
1212 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1216 && (ln == 1 || (OP(c) == EXACTF \
1217 ? foldEQ(s, m, ln) \
1218 : foldEQ_locale(s, m, ln))) \
1219 && (!reginfo || regtry(reginfo, &s)) ) \
1225 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1227 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1233 #define REXEC_FBC_SCAN(CoDe) \
1235 while (s < strend) { \
1241 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1242 REXEC_FBC_UTF8_SCAN( \
1244 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1253 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1256 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1265 #define REXEC_FBC_TRYIT \
1266 if ((!reginfo || regtry(reginfo, &s))) \
1269 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1270 if (utf8_target) { \
1271 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1274 REXEC_FBC_CLASS_SCAN(CoNd); \
1278 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1279 if (utf8_target) { \
1281 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1284 REXEC_FBC_CLASS_SCAN(CoNd); \
1288 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1289 PL_reg_flags |= RF_tainted; \
1290 if (utf8_target) { \
1291 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1294 REXEC_FBC_CLASS_SCAN(CoNd); \
1298 #define DUMP_EXEC_POS(li,s,doutf8) \
1299 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1301 /* We know what class REx starts with. Try to find this position... */
1302 /* if reginfo is NULL, its a dryrun */
1303 /* annoyingly all the vars in this routine have different names from their counterparts
1304 in regmatch. /grrr */
1307 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1308 const char *strend, regmatch_info *reginfo)
1311 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1315 register STRLEN uskip;
1319 register I32 tmp = 1; /* Scratch variable? */
1320 register const bool utf8_target = PL_reg_match_utf8;
1321 RXi_GET_DECL(prog,progi);
1323 PERL_ARGS_ASSERT_FIND_BYCLASS;
1325 /* We know what class it must start with. */
1329 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1330 !UTF8_IS_INVARIANT((U8)s[0]) ?
1331 reginclass(prog, c, (U8*)s, 0, utf8_target) :
1332 REGINCLASS(prog, c, (U8*)s));
1335 while (s < strend) {
1338 if (REGINCLASS(prog, c, (U8*)s) ||
1339 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1340 /* The assignment of 2 is intentional:
1341 * for the folded sharp s, the skip is 2. */
1342 (skip = SHARP_S_SKIP))) {
1343 if (tmp && (!reginfo || regtry(reginfo, &s)))
1356 if (tmp && (!reginfo || regtry(reginfo, &s)))
1364 ln = STR_LEN(c); /* length to match in octets/bytes */
1365 lnc = (I32) ln; /* length to match in characters */
1367 STRLEN ulen1, ulen2;
1369 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1370 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1371 /* used by commented-out code below */
1372 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1374 /* XXX: Since the node will be case folded at compile
1375 time this logic is a little odd, although im not
1376 sure that its actually wrong. --dmq */
1378 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1379 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1381 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1382 codepoint of the first character in the converted
1383 form, yet originally we did the extra step.
1384 No tests fail by commenting this code out however
1385 so Ive left it out. -- dmq.
1387 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1389 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1394 while (sm < ((U8 *) m + ln)) {
1409 c2 = PL_fold_locale[c1];
1411 e = HOP3c(strend, -((I32)lnc), s);
1413 if (!reginfo && e < s)
1414 e = s; /* Due to minlen logic of intuit() */
1416 /* The idea in the EXACTF* cases is to first find the
1417 * first character of the EXACTF* node and then, if
1418 * necessary, case-insensitively compare the full
1419 * text of the node. The c1 and c2 are the first
1420 * characters (though in Unicode it gets a bit
1421 * more complicated because there are more cases
1422 * than just upper and lower: one needs to use
1423 * the so-called folding case for case-insensitive
1424 * matching (called "loose matching" in Unicode).
1425 * foldEQ_utf8() will do just that. */
1427 if (utf8_target || UTF_PATTERN) {
1429 U8 tmpbuf [UTF8_MAXBYTES+1];
1432 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1434 /* Upper and lower of 1st char are equal -
1435 * probably not a "letter". */
1438 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1443 REXEC_FBC_EXACTISH_CHECK(c == c1);
1449 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1455 /* Handle some of the three Greek sigmas cases.
1456 * Note that not all the possible combinations
1457 * are handled here: some of them are handled
1458 * by the standard folding rules, and some of
1459 * them (the character class or ANYOF cases)
1460 * are handled during compiletime in
1461 * regexec.c:S_regclass(). */
1462 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1463 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1464 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1466 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1471 /* Neither pattern nor string are UTF8 */
1473 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1475 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1479 PL_reg_flags |= RF_tainted;
1486 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1487 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1489 tmp = ((OP(c) == BOUND ?
1490 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1491 LOAD_UTF8_CHARCLASS_ALNUM();
1492 REXEC_FBC_UTF8_SCAN(
1493 if (tmp == !(OP(c) == BOUND ?
1494 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)) :
1495 isALNUM_LC_utf8((U8*)s)))
1503 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1504 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1507 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1513 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1517 PL_reg_flags |= RF_tainted;
1524 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1525 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1527 tmp = ((OP(c) == NBOUND ?
1528 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1529 LOAD_UTF8_CHARCLASS_ALNUM();
1530 REXEC_FBC_UTF8_SCAN(
1531 if (tmp == !(OP(c) == NBOUND ?
1532 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)) :
1533 isALNUM_LC_utf8((U8*)s)))
1535 else REXEC_FBC_TRYIT;
1539 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1540 tmp = ((OP(c) == NBOUND ?
1541 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1544 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1546 else REXEC_FBC_TRYIT;
1549 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1553 REXEC_FBC_CSCAN_PRELOAD(
1554 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1555 swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
1559 REXEC_FBC_CSCAN_TAINT(
1560 isALNUM_LC_utf8((U8*)s),
1564 REXEC_FBC_CSCAN_PRELOAD(
1565 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1566 !swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
1570 REXEC_FBC_CSCAN_TAINT(
1571 !isALNUM_LC_utf8((U8*)s),
1575 REXEC_FBC_CSCAN_PRELOAD(
1576 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1577 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target),
1581 REXEC_FBC_CSCAN_TAINT(
1582 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1586 REXEC_FBC_CSCAN_PRELOAD(
1587 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1588 !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target)),
1592 REXEC_FBC_CSCAN_TAINT(
1593 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1597 REXEC_FBC_CSCAN_PRELOAD(
1598 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1599 swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
1603 REXEC_FBC_CSCAN_TAINT(
1604 isDIGIT_LC_utf8((U8*)s),
1608 REXEC_FBC_CSCAN_PRELOAD(
1609 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1610 !swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
1614 REXEC_FBC_CSCAN_TAINT(
1615 !isDIGIT_LC_utf8((U8*)s),
1621 is_LNBREAK_latin1(s)
1631 !is_VERTWS_latin1(s)
1636 is_HORIZWS_latin1(s)
1640 !is_HORIZWS_utf8(s),
1641 !is_HORIZWS_latin1(s)
1647 /* what trie are we using right now */
1649 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1651 = (reg_trie_data*)progi->data->data[ aho->trie ];
1652 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1654 const char *last_start = strend - trie->minlen;
1656 const char *real_start = s;
1658 STRLEN maxlen = trie->maxlen;
1660 U8 **points; /* map of where we were in the input string
1661 when reading a given char. For ASCII this
1662 is unnecessary overhead as the relationship
1663 is always 1:1, but for Unicode, especially
1664 case folded Unicode this is not true. */
1665 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1669 GET_RE_DEBUG_FLAGS_DECL;
1671 /* We can't just allocate points here. We need to wrap it in
1672 * an SV so it gets freed properly if there is a croak while
1673 * running the match */
1676 sv_points=newSV(maxlen * sizeof(U8 *));
1677 SvCUR_set(sv_points,
1678 maxlen * sizeof(U8 *));
1679 SvPOK_on(sv_points);
1680 sv_2mortal(sv_points);
1681 points=(U8**)SvPV_nolen(sv_points );
1682 if ( trie_type != trie_utf8_fold
1683 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1686 bitmap=(U8*)trie->bitmap;
1688 bitmap=(U8*)ANYOF_BITMAP(c);
1690 /* this is the Aho-Corasick algorithm modified a touch
1691 to include special handling for long "unknown char"
1692 sequences. The basic idea being that we use AC as long
1693 as we are dealing with a possible matching char, when
1694 we encounter an unknown char (and we have not encountered
1695 an accepting state) we scan forward until we find a legal
1697 AC matching is basically that of trie matching, except
1698 that when we encounter a failing transition, we fall back
1699 to the current states "fail state", and try the current char
1700 again, a process we repeat until we reach the root state,
1701 state 1, or a legal transition. If we fail on the root state
1702 then we can either terminate if we have reached an accepting
1703 state previously, or restart the entire process from the beginning
1707 while (s <= last_start) {
1708 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1716 U8 *uscan = (U8*)NULL;
1717 U8 *leftmost = NULL;
1719 U32 accepted_word= 0;
1723 while ( state && uc <= (U8*)strend ) {
1725 U32 word = aho->states[ state ].wordnum;
1729 DEBUG_TRIE_EXECUTE_r(
1730 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1731 dump_exec_pos( (char *)uc, c, strend, real_start,
1732 (char *)uc, utf8_target );
1733 PerlIO_printf( Perl_debug_log,
1734 " Scanning for legal start char...\n");
1737 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1742 if (uc >(U8*)last_start) break;
1746 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
1747 if (!leftmost || lpos < leftmost) {
1748 DEBUG_r(accepted_word=word);
1754 points[pointpos++ % maxlen]= uc;
1755 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1756 uscan, len, uvc, charid, foldlen,
1758 DEBUG_TRIE_EXECUTE_r({
1759 dump_exec_pos( (char *)uc, c, strend, real_start,
1761 PerlIO_printf(Perl_debug_log,
1762 " Charid:%3u CP:%4"UVxf" ",
1768 word = aho->states[ state ].wordnum;
1770 base = aho->states[ state ].trans.base;
1772 DEBUG_TRIE_EXECUTE_r({
1774 dump_exec_pos( (char *)uc, c, strend, real_start,
1776 PerlIO_printf( Perl_debug_log,
1777 "%sState: %4"UVxf", word=%"UVxf,
1778 failed ? " Fail transition to " : "",
1779 (UV)state, (UV)word);
1785 ( ((offset = base + charid
1786 - 1 - trie->uniquecharcount)) >= 0)
1787 && ((U32)offset < trie->lasttrans)
1788 && trie->trans[offset].check == state
1789 && (tmp=trie->trans[offset].next))
1791 DEBUG_TRIE_EXECUTE_r(
1792 PerlIO_printf( Perl_debug_log," - legal\n"));
1797 DEBUG_TRIE_EXECUTE_r(
1798 PerlIO_printf( Perl_debug_log," - fail\n"));
1800 state = aho->fail[state];
1804 /* we must be accepting here */
1805 DEBUG_TRIE_EXECUTE_r(
1806 PerlIO_printf( Perl_debug_log," - accepting\n"));
1815 if (!state) state = 1;
1818 if ( aho->states[ state ].wordnum ) {
1819 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
1820 if (!leftmost || lpos < leftmost) {
1821 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1826 s = (char*)leftmost;
1827 DEBUG_TRIE_EXECUTE_r({
1829 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1830 (UV)accepted_word, (IV)(s - real_start)
1833 if (!reginfo || regtry(reginfo, &s)) {
1839 DEBUG_TRIE_EXECUTE_r({
1840 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1843 DEBUG_TRIE_EXECUTE_r(
1844 PerlIO_printf( Perl_debug_log,"No match.\n"));
1853 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1863 - regexec_flags - match a regexp against a string
1866 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1867 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1868 /* strend: pointer to null at end of string */
1869 /* strbeg: real beginning of string */
1870 /* minend: end of match must be >=minend after stringarg. */
1871 /* data: May be used for some additional optimizations.
1872 Currently its only used, with a U32 cast, for transmitting
1873 the ganch offset when doing a /g match. This will change */
1874 /* nosave: For optimizations. */
1877 struct regexp *const prog = (struct regexp *)SvANY(rx);
1878 /*register*/ char *s;
1879 register regnode *c;
1880 /*register*/ char *startpos = stringarg;
1881 I32 minlen; /* must match at least this many chars */
1882 I32 dontbother = 0; /* how many characters not to try at end */
1883 I32 end_shift = 0; /* Same for the end. */ /* CC */
1884 I32 scream_pos = -1; /* Internal iterator of scream. */
1885 char *scream_olds = NULL;
1886 const bool utf8_target = cBOOL(DO_UTF8(sv));
1888 RXi_GET_DECL(prog,progi);
1889 regmatch_info reginfo; /* create some info to pass to regtry etc */
1890 regexp_paren_pair *swap = NULL;
1891 GET_RE_DEBUG_FLAGS_DECL;
1893 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1894 PERL_UNUSED_ARG(data);
1896 /* Be paranoid... */
1897 if (prog == NULL || startpos == NULL) {
1898 Perl_croak(aTHX_ "NULL regexp parameter");
1902 multiline = prog->extflags & RXf_PMf_MULTILINE;
1903 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1905 RX_MATCH_UTF8_set(rx, utf8_target);
1907 debug_start_match(rx, utf8_target, startpos, strend,
1911 minlen = prog->minlen;
1913 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1914 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1915 "String too short [regexec_flags]...\n"));
1920 /* Check validity of program. */
1921 if (UCHARAT(progi->program) != REG_MAGIC) {
1922 Perl_croak(aTHX_ "corrupted regexp program");
1926 PL_reg_eval_set = 0;
1930 PL_reg_flags |= RF_utf8;
1932 /* Mark beginning of line for ^ and lookbehind. */
1933 reginfo.bol = startpos; /* XXX not used ??? */
1937 /* Mark end of line for $ (and such) */
1940 /* see how far we have to get to not match where we matched before */
1941 reginfo.till = startpos+minend;
1943 /* If there is a "must appear" string, look for it. */
1946 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1948 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
1949 reginfo.ganch = startpos + prog->gofs;
1950 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1951 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
1952 } else if (sv && SvTYPE(sv) >= SVt_PVMG
1954 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1955 && mg->mg_len >= 0) {
1956 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1957 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1958 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
1960 if (prog->extflags & RXf_ANCH_GPOS) {
1961 if (s > reginfo.ganch)
1963 s = reginfo.ganch - prog->gofs;
1964 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1965 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
1971 reginfo.ganch = strbeg + PTR2UV(data);
1972 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1973 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
1975 } else { /* pos() not defined */
1976 reginfo.ganch = strbeg;
1977 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1978 "GPOS: reginfo.ganch = strbeg\n"));
1981 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1982 /* We have to be careful. If the previous successful match
1983 was from this regex we don't want a subsequent partially
1984 successful match to clobber the old results.
1985 So when we detect this possibility we add a swap buffer
1986 to the re, and switch the buffer each match. If we fail
1987 we switch it back, otherwise we leave it swapped.
1990 /* do we need a save destructor here for eval dies? */
1991 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
1993 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1994 re_scream_pos_data d;
1996 d.scream_olds = &scream_olds;
1997 d.scream_pos = &scream_pos;
1998 s = re_intuit_start(rx, sv, s, strend, flags, &d);
2000 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
2001 goto phooey; /* not present */
2007 /* Simplest case: anchored match need be tried only once. */
2008 /* [unless only anchor is BOL and multiline is set] */
2009 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2010 if (s == startpos && regtry(®info, &startpos))
2012 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2013 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2018 dontbother = minlen - 1;
2019 end = HOP3c(strend, -dontbother, strbeg) - 1;
2020 /* for multiline we only have to try after newlines */
2021 if (prog->check_substr || prog->check_utf8) {
2025 if (regtry(®info, &s))
2030 if (prog->extflags & RXf_USE_INTUIT) {
2031 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2042 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2043 if (regtry(®info, &s))
2050 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2052 /* the warning about reginfo.ganch being used without intialization
2053 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2054 and we only enter this block when the same bit is set. */
2055 char *tmp_s = reginfo.ganch - prog->gofs;
2057 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2062 /* Messy cases: unanchored match. */
2063 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2064 /* we have /x+whatever/ */
2065 /* it must be a one character string (XXXX Except UTF_PATTERN?) */
2070 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2071 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2072 ch = SvPVX_const(utf8_target ? prog->anchored_utf8 : prog->anchored_substr)[0];
2077 DEBUG_EXECUTE_r( did_match = 1 );
2078 if (regtry(®info, &s)) goto got_it;
2080 while (s < strend && *s == ch)
2088 DEBUG_EXECUTE_r( did_match = 1 );
2089 if (regtry(®info, &s)) goto got_it;
2091 while (s < strend && *s == ch)
2096 DEBUG_EXECUTE_r(if (!did_match)
2097 PerlIO_printf(Perl_debug_log,
2098 "Did not find anchored character...\n")
2101 else if (prog->anchored_substr != NULL
2102 || prog->anchored_utf8 != NULL
2103 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2104 && prog->float_max_offset < strend - s)) {
2109 char *last1; /* Last position checked before */
2113 if (prog->anchored_substr || prog->anchored_utf8) {
2114 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2115 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2116 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
2117 back_max = back_min = prog->anchored_offset;
2119 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2120 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2121 must = utf8_target ? prog->float_utf8 : prog->float_substr;
2122 back_max = prog->float_max_offset;
2123 back_min = prog->float_min_offset;
2127 if (must == &PL_sv_undef)
2128 /* could not downgrade utf8 check substring, so must fail */
2134 last = HOP3c(strend, /* Cannot start after this */
2135 -(I32)(CHR_SVLEN(must)
2136 - (SvTAIL(must) != 0) + back_min), strbeg);
2139 last1 = HOPc(s, -1);
2141 last1 = s - 1; /* bogus */
2143 /* XXXX check_substr already used to find "s", can optimize if
2144 check_substr==must. */
2146 dontbother = end_shift;
2147 strend = HOPc(strend, -dontbother);
2148 while ( (s <= last) &&
2149 ((flags & REXEC_SCREAM)
2150 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2151 end_shift, &scream_pos, 0))
2152 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2153 (unsigned char*)strend, must,
2154 multiline ? FBMrf_MULTILINE : 0))) ) {
2155 /* we may be pointing at the wrong string */
2156 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2157 s = strbeg + (s - SvPVX_const(sv));
2158 DEBUG_EXECUTE_r( did_match = 1 );
2159 if (HOPc(s, -back_max) > last1) {
2160 last1 = HOPc(s, -back_min);
2161 s = HOPc(s, -back_max);
2164 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2166 last1 = HOPc(s, -back_min);
2170 while (s <= last1) {
2171 if (regtry(®info, &s))
2177 while (s <= last1) {
2178 if (regtry(®info, &s))
2184 DEBUG_EXECUTE_r(if (!did_match) {
2185 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
2186 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2187 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2188 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2189 ? "anchored" : "floating"),
2190 quoted, RE_SV_TAIL(must));
2194 else if ( (c = progi->regstclass) ) {
2196 const OPCODE op = OP(progi->regstclass);
2197 /* don't bother with what can't match */
2198 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2199 strend = HOPc(strend, -(minlen - 1));
2202 SV * const prop = sv_newmortal();
2203 regprop(prog, prop, c);
2205 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
2207 PerlIO_printf(Perl_debug_log,
2208 "Matching stclass %.*s against %s (%d bytes)\n",
2209 (int)SvCUR(prop), SvPVX_const(prop),
2210 quoted, (int)(strend - s));
2213 if (find_byclass(prog, c, s, strend, ®info))
2215 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2219 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2224 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2225 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2226 float_real = utf8_target ? prog->float_utf8 : prog->float_substr;
2228 if (flags & REXEC_SCREAM) {
2229 last = screaminstr(sv, float_real, s - strbeg,
2230 end_shift, &scream_pos, 1); /* last one */
2232 last = scream_olds; /* Only one occurrence. */
2233 /* we may be pointing at the wrong string */
2234 else if (RXp_MATCH_COPIED(prog))
2235 s = strbeg + (s - SvPVX_const(sv));
2239 const char * const little = SvPV_const(float_real, len);
2241 if (SvTAIL(float_real)) {
2242 if (memEQ(strend - len + 1, little, len - 1))
2243 last = strend - len + 1;
2244 else if (!multiline)
2245 last = memEQ(strend - len, little, len)
2246 ? strend - len : NULL;
2252 last = rninstr(s, strend, little, little + len);
2254 last = strend; /* matching "$" */
2259 PerlIO_printf(Perl_debug_log,
2260 "%sCan't trim the tail, match fails (should not happen)%s\n",
2261 PL_colors[4], PL_colors[5]));
2262 goto phooey; /* Should not happen! */
2264 dontbother = strend - last + prog->float_min_offset;
2266 if (minlen && (dontbother < minlen))
2267 dontbother = minlen - 1;
2268 strend -= dontbother; /* this one's always in bytes! */
2269 /* We don't know much -- general case. */
2272 if (regtry(®info, &s))
2281 if (regtry(®info, &s))
2283 } while (s++ < strend);
2292 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2294 if (PL_reg_eval_set)
2295 restore_pos(aTHX_ prog);
2296 if (RXp_PAREN_NAMES(prog))
2297 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2299 /* make sure $`, $&, $', and $digit will work later */
2300 if ( !(flags & REXEC_NOT_FIRST) ) {
2301 RX_MATCH_COPY_FREE(rx);
2302 if (flags & REXEC_COPY_STR) {
2303 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2304 #ifdef PERL_OLD_COPY_ON_WRITE
2306 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2308 PerlIO_printf(Perl_debug_log,
2309 "Copy on write: regexp capture, type %d\n",
2312 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2313 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2314 assert (SvPOKp(prog->saved_copy));
2318 RX_MATCH_COPIED_on(rx);
2319 s = savepvn(strbeg, i);
2325 prog->subbeg = strbeg;
2326 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2333 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2334 PL_colors[4], PL_colors[5]));
2335 if (PL_reg_eval_set)
2336 restore_pos(aTHX_ prog);
2338 /* we failed :-( roll it back */
2339 Safefree(prog->offs);
2348 - regtry - try match at specific point
2350 STATIC I32 /* 0 failure, 1 success */
2351 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2355 REGEXP *const rx = reginfo->prog;
2356 regexp *const prog = (struct regexp *)SvANY(rx);
2357 RXi_GET_DECL(prog,progi);
2358 GET_RE_DEBUG_FLAGS_DECL;
2360 PERL_ARGS_ASSERT_REGTRY;
2362 reginfo->cutpoint=NULL;
2364 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2367 PL_reg_eval_set = RS_init;
2368 DEBUG_EXECUTE_r(DEBUG_s(
2369 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2370 (IV)(PL_stack_sp - PL_stack_base));
2373 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2374 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2376 /* Apparently this is not needed, judging by wantarray. */
2377 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2378 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2381 /* Make $_ available to executed code. */
2382 if (reginfo->sv != DEFSV) {
2384 DEFSV_set(reginfo->sv);
2387 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2388 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2389 /* prepare for quick setting of pos */
2390 #ifdef PERL_OLD_COPY_ON_WRITE
2391 if (SvIsCOW(reginfo->sv))
2392 sv_force_normal_flags(reginfo->sv, 0);
2394 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2395 &PL_vtbl_mglob, NULL, 0);
2399 PL_reg_oldpos = mg->mg_len;
2400 SAVEDESTRUCTOR_X(restore_pos, prog);
2402 if (!PL_reg_curpm) {
2403 Newxz(PL_reg_curpm, 1, PMOP);
2406 SV* const repointer = &PL_sv_undef;
2407 /* this regexp is also owned by the new PL_reg_curpm, which
2408 will try to free it. */
2409 av_push(PL_regex_padav, repointer);
2410 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2411 PL_regex_pad = AvARRAY(PL_regex_padav);
2416 /* It seems that non-ithreads works both with and without this code.
2417 So for efficiency reasons it seems best not to have the code
2418 compiled when it is not needed. */
2419 /* This is safe against NULLs: */
2420 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2421 /* PM_reg_curpm owns a reference to this regexp. */
2424 PM_SETRE(PL_reg_curpm, rx);
2425 PL_reg_oldcurpm = PL_curpm;
2426 PL_curpm = PL_reg_curpm;
2427 if (RXp_MATCH_COPIED(prog)) {
2428 /* Here is a serious problem: we cannot rewrite subbeg,
2429 since it may be needed if this match fails. Thus
2430 $` inside (?{}) could fail... */
2431 PL_reg_oldsaved = prog->subbeg;
2432 PL_reg_oldsavedlen = prog->sublen;
2433 #ifdef PERL_OLD_COPY_ON_WRITE
2434 PL_nrs = prog->saved_copy;
2436 RXp_MATCH_COPIED_off(prog);
2439 PL_reg_oldsaved = NULL;
2440 prog->subbeg = PL_bostr;
2441 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2443 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2444 prog->offs[0].start = *startpos - PL_bostr;
2445 PL_reginput = *startpos;
2446 PL_reglastparen = &prog->lastparen;
2447 PL_reglastcloseparen = &prog->lastcloseparen;
2448 prog->lastparen = 0;
2449 prog->lastcloseparen = 0;
2451 PL_regoffs = prog->offs;
2452 if (PL_reg_start_tmpl <= prog->nparens) {
2453 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2454 if(PL_reg_start_tmp)
2455 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2457 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2460 /* XXXX What this code is doing here?!!! There should be no need
2461 to do this again and again, PL_reglastparen should take care of
2464 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2465 * Actually, the code in regcppop() (which Ilya may be meaning by
2466 * PL_reglastparen), is not needed at all by the test suite
2467 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2468 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2469 * Meanwhile, this code *is* needed for the
2470 * above-mentioned test suite tests to succeed. The common theme
2471 * on those tests seems to be returning null fields from matches.
2472 * --jhi updated by dapm */
2474 if (prog->nparens) {
2475 regexp_paren_pair *pp = PL_regoffs;
2477 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2485 if (regmatch(reginfo, progi->program + 1)) {
2486 PL_regoffs[0].end = PL_reginput - PL_bostr;
2489 if (reginfo->cutpoint)
2490 *startpos= reginfo->cutpoint;
2491 REGCP_UNWIND(lastcp);
2496 #define sayYES goto yes
2497 #define sayNO goto no
2498 #define sayNO_SILENT goto no_silent
2500 /* we dont use STMT_START/END here because it leads to
2501 "unreachable code" warnings, which are bogus, but distracting. */
2502 #define CACHEsayNO \
2503 if (ST.cache_mask) \
2504 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2507 /* this is used to determine how far from the left messages like
2508 'failed...' are printed. It should be set such that messages
2509 are inline with the regop output that created them.
2511 #define REPORT_CODE_OFF 32
2514 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2515 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2517 #define SLAB_FIRST(s) (&(s)->states[0])
2518 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2520 /* grab a new slab and return the first slot in it */
2522 STATIC regmatch_state *
2525 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2528 regmatch_slab *s = PL_regmatch_slab->next;
2530 Newx(s, 1, regmatch_slab);
2531 s->prev = PL_regmatch_slab;
2533 PL_regmatch_slab->next = s;
2535 PL_regmatch_slab = s;
2536 return SLAB_FIRST(s);
2540 /* push a new state then goto it */
2542 #define PUSH_STATE_GOTO(state, node) \
2544 st->resume_state = state; \
2547 /* push a new state with success backtracking, then goto it */
2549 #define PUSH_YES_STATE_GOTO(state, node) \
2551 st->resume_state = state; \
2552 goto push_yes_state;
2558 regmatch() - main matching routine
2560 This is basically one big switch statement in a loop. We execute an op,
2561 set 'next' to point the next op, and continue. If we come to a point which
2562 we may need to backtrack to on failure such as (A|B|C), we push a
2563 backtrack state onto the backtrack stack. On failure, we pop the top
2564 state, and re-enter the loop at the state indicated. If there are no more
2565 states to pop, we return failure.
2567 Sometimes we also need to backtrack on success; for example /A+/, where
2568 after successfully matching one A, we need to go back and try to
2569 match another one; similarly for lookahead assertions: if the assertion
2570 completes successfully, we backtrack to the state just before the assertion
2571 and then carry on. In these cases, the pushed state is marked as
2572 'backtrack on success too'. This marking is in fact done by a chain of
2573 pointers, each pointing to the previous 'yes' state. On success, we pop to
2574 the nearest yes state, discarding any intermediate failure-only states.
2575 Sometimes a yes state is pushed just to force some cleanup code to be
2576 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2577 it to free the inner regex.
2579 Note that failure backtracking rewinds the cursor position, while
2580 success backtracking leaves it alone.
2582 A pattern is complete when the END op is executed, while a subpattern
2583 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2584 ops trigger the "pop to last yes state if any, otherwise return true"
2587 A common convention in this function is to use A and B to refer to the two
2588 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2589 the subpattern to be matched possibly multiple times, while B is the entire
2590 rest of the pattern. Variable and state names reflect this convention.
2592 The states in the main switch are the union of ops and failure/success of
2593 substates associated with with that op. For example, IFMATCH is the op
2594 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2595 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2596 successfully matched A and IFMATCH_A_fail is a state saying that we have
2597 just failed to match A. Resume states always come in pairs. The backtrack
2598 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2599 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2600 on success or failure.
2602 The struct that holds a backtracking state is actually a big union, with
2603 one variant for each major type of op. The variable st points to the
2604 top-most backtrack struct. To make the code clearer, within each
2605 block of code we #define ST to alias the relevant union.
2607 Here's a concrete example of a (vastly oversimplified) IFMATCH
2613 #define ST st->u.ifmatch
2615 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2616 ST.foo = ...; // some state we wish to save
2618 // push a yes backtrack state with a resume value of
2619 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2621 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2624 case IFMATCH_A: // we have successfully executed A; now continue with B
2626 bar = ST.foo; // do something with the preserved value
2629 case IFMATCH_A_fail: // A failed, so the assertion failed
2630 ...; // do some housekeeping, then ...
2631 sayNO; // propagate the failure
2638 For any old-timers reading this who are familiar with the old recursive
2639 approach, the code above is equivalent to:
2641 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2650 ...; // do some housekeeping, then ...
2651 sayNO; // propagate the failure
2654 The topmost backtrack state, pointed to by st, is usually free. If you
2655 want to claim it, populate any ST.foo fields in it with values you wish to
2656 save, then do one of
2658 PUSH_STATE_GOTO(resume_state, node);
2659 PUSH_YES_STATE_GOTO(resume_state, node);
2661 which sets that backtrack state's resume value to 'resume_state', pushes a
2662 new free entry to the top of the backtrack stack, then goes to 'node'.
2663 On backtracking, the free slot is popped, and the saved state becomes the
2664 new free state. An ST.foo field in this new top state can be temporarily
2665 accessed to retrieve values, but once the main loop is re-entered, it
2666 becomes available for reuse.
2668 Note that the depth of the backtrack stack constantly increases during the
2669 left-to-right execution of the pattern, rather than going up and down with
2670 the pattern nesting. For example the stack is at its maximum at Z at the
2671 end of the pattern, rather than at X in the following:
2673 /(((X)+)+)+....(Y)+....Z/
2675 The only exceptions to this are lookahead/behind assertions and the cut,
2676 (?>A), which pop all the backtrack states associated with A before
2679 Bascktrack state structs are allocated in slabs of about 4K in size.
2680 PL_regmatch_state and st always point to the currently active state,
2681 and PL_regmatch_slab points to the slab currently containing
2682 PL_regmatch_state. The first time regmatch() is called, the first slab is
2683 allocated, and is never freed until interpreter destruction. When the slab
2684 is full, a new one is allocated and chained to the end. At exit from
2685 regmatch(), slabs allocated since entry are freed.
2690 #define DEBUG_STATE_pp(pp) \
2692 DUMP_EXEC_POS(locinput, scan, utf8_target); \
2693 PerlIO_printf(Perl_debug_log, \
2694 " %*s"pp" %s%s%s%s%s\n", \
2696 PL_reg_name[st->resume_state], \
2697 ((st==yes_state||st==mark_state) ? "[" : ""), \
2698 ((st==yes_state) ? "Y" : ""), \
2699 ((st==mark_state) ? "M" : ""), \
2700 ((st==yes_state||st==mark_state) ? "]" : "") \
2705 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2710 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
2711 const char *start, const char *end, const char *blurb)
2713 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2715 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2720 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2721 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2723 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
2724 start, end - start, 60);
2726 PerlIO_printf(Perl_debug_log,
2727 "%s%s REx%s %s against %s\n",
2728 PL_colors[4], blurb, PL_colors[5], s0, s1);
2730 if (utf8_target||utf8_pat)
2731 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2732 utf8_pat ? "pattern" : "",
2733 utf8_pat && utf8_target ? " and " : "",
2734 utf8_target ? "string" : ""
2740 S_dump_exec_pos(pTHX_ const char *locinput,
2741 const regnode *scan,
2742 const char *loc_regeol,
2743 const char *loc_bostr,
2744 const char *loc_reg_starttry,
2745 const bool utf8_target)
2747 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2748 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2749 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2750 /* The part of the string before starttry has one color
2751 (pref0_len chars), between starttry and current
2752 position another one (pref_len - pref0_len chars),
2753 after the current position the third one.
2754 We assume that pref0_len <= pref_len, otherwise we
2755 decrease pref0_len. */
2756 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2757 ? (5 + taill) - l : locinput - loc_bostr;
2760 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2762 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2764 pref0_len = pref_len - (locinput - loc_reg_starttry);
2765 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2766 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2767 ? (5 + taill) - pref_len : loc_regeol - locinput);
2768 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2772 if (pref0_len > pref_len)
2773 pref0_len = pref_len;
2775 const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
2777 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2778 (locinput - pref_len),pref0_len, 60, 4, 5);
2780 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2781 (locinput - pref_len + pref0_len),
2782 pref_len - pref0_len, 60, 2, 3);
2784 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2785 locinput, loc_regeol - locinput, 10, 0, 1);
2787 const STRLEN tlen=len0+len1+len2;
2788 PerlIO_printf(Perl_debug_log,
2789 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2790 (IV)(locinput - loc_bostr),
2793 (docolor ? "" : "> <"),
2795 (int)(tlen > 19 ? 0 : 19 - tlen),
2802 /* reg_check_named_buff_matched()
2803 * Checks to see if a named buffer has matched. The data array of
2804 * buffer numbers corresponding to the buffer is expected to reside
2805 * in the regexp->data->data array in the slot stored in the ARG() of
2806 * node involved. Note that this routine doesn't actually care about the
2807 * name, that information is not preserved from compilation to execution.
2808 * Returns the index of the leftmost defined buffer with the given name
2809 * or 0 if non of the buffers matched.
2812 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2815 RXi_GET_DECL(rex,rexi);
2816 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2817 I32 *nums=(I32*)SvPVX(sv_dat);
2819 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2821 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2822 if ((I32)*PL_reglastparen >= nums[n] &&
2823 PL_regoffs[nums[n]].end != -1)
2832 /* free all slabs above current one - called during LEAVE_SCOPE */
2835 S_clear_backtrack_stack(pTHX_ void *p)
2837 regmatch_slab *s = PL_regmatch_slab->next;
2842 PL_regmatch_slab->next = NULL;
2844 regmatch_slab * const osl = s;
2851 #define SETREX(Re1,Re2) \
2852 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2855 STATIC I32 /* 0 failure, 1 success */
2856 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2858 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2862 register const bool utf8_target = PL_reg_match_utf8;
2863 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2864 REGEXP *rex_sv = reginfo->prog;
2865 regexp *rex = (struct regexp *)SvANY(rex_sv);
2866 RXi_GET_DECL(rex,rexi);
2868 /* the current state. This is a cached copy of PL_regmatch_state */
2869 register regmatch_state *st;
2870 /* cache heavy used fields of st in registers */
2871 register regnode *scan;
2872 register regnode *next;
2873 register U32 n = 0; /* general value; init to avoid compiler warning */
2874 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2875 register char *locinput = PL_reginput;
2876 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2878 bool result = 0; /* return value of S_regmatch */
2879 int depth = 0; /* depth of backtrack stack */
2880 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2881 const U32 max_nochange_depth =
2882 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2883 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2884 regmatch_state *yes_state = NULL; /* state to pop to on success of
2886 /* mark_state piggy backs on the yes_state logic so that when we unwind
2887 the stack on success we can update the mark_state as we go */
2888 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2889 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2890 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2892 bool no_final = 0; /* prevent failure from backtracking? */
2893 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2894 char *startpoint = PL_reginput;
2895 SV *popmark = NULL; /* are we looking for a mark? */
2896 SV *sv_commit = NULL; /* last mark name seen in failure */
2897 SV *sv_yes_mark = NULL; /* last mark name we have seen
2898 during a successfull match */
2899 U32 lastopen = 0; /* last open we saw */
2900 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2901 SV* const oreplsv = GvSV(PL_replgv);
2902 /* these three flags are set by various ops to signal information to
2903 * the very next op. They have a useful lifetime of exactly one loop
2904 * iteration, and are not preserved or restored by state pushes/pops
2906 bool sw = 0; /* the condition value in (?(cond)a|b) */
2907 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2908 int logical = 0; /* the following EVAL is:
2912 or the following IFMATCH/UNLESSM is:
2913 false: plain (?=foo)
2914 true: used as a condition: (?(?=foo))
2917 GET_RE_DEBUG_FLAGS_DECL;
2920 PERL_ARGS_ASSERT_REGMATCH;
2922 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2923 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2925 /* on first ever call to regmatch, allocate first slab */
2926 if (!PL_regmatch_slab) {
2927 Newx(PL_regmatch_slab, 1, regmatch_slab);
2928 PL_regmatch_slab->prev = NULL;
2929 PL_regmatch_slab->next = NULL;
2930 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2933 oldsave = PL_savestack_ix;
2934 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2935 SAVEVPTR(PL_regmatch_slab);
2936 SAVEVPTR(PL_regmatch_state);
2938 /* grab next free state slot */
2939 st = ++PL_regmatch_state;
2940 if (st > SLAB_LAST(PL_regmatch_slab))
2941 st = PL_regmatch_state = S_push_slab(aTHX);
2943 /* Note that nextchr is a byte even in UTF */
2944 nextchr = UCHARAT(locinput);
2946 while (scan != NULL) {
2949 SV * const prop = sv_newmortal();
2950 regnode *rnext=regnext(scan);
2951 DUMP_EXEC_POS( locinput, scan, utf8_target );
2952 regprop(rex, prop, scan);
2954 PerlIO_printf(Perl_debug_log,
2955 "%3"IVdf":%*s%s(%"IVdf")\n",
2956 (IV)(scan - rexi->program), depth*2, "",
2958 (PL_regkind[OP(scan)] == END || !rnext) ?
2959 0 : (IV)(rnext - rexi->program));
2962 next = scan + NEXT_OFF(scan);
2965 state_num = OP(scan);
2967 REH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);
2970 assert(PL_reglastparen == &rex->lastparen);
2971 assert(PL_reglastcloseparen == &rex->lastcloseparen);
2972 assert(PL_regoffs == rex->offs);
2974 switch (state_num) {
2976 if (locinput == PL_bostr)
2978 /* reginfo->till = reginfo->bol; */
2983 if (locinput == PL_bostr ||
2984 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2990 if (locinput == PL_bostr)
2994 if (locinput == reginfo->ganch)
2999 /* update the startpoint */
3000 st->u.keeper.val = PL_regoffs[0].start;
3001 PL_reginput = locinput;
3002 PL_regoffs[0].start = locinput - PL_bostr;
3003 PUSH_STATE_GOTO(KEEPS_next, next);
3005 case KEEPS_next_fail:
3006 /* rollback the start point change */
3007 PL_regoffs[0].start = st->u.keeper.val;
3013 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3018 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3020 if (PL_regeol - locinput > 1)
3024 if (PL_regeol != locinput)
3028 if (!nextchr && locinput >= PL_regeol)
3031 locinput += PL_utf8skip[nextchr];
3032 if (locinput > PL_regeol)
3034 nextchr = UCHARAT(locinput);
3037 nextchr = UCHARAT(++locinput);
3040 if (!nextchr && locinput >= PL_regeol)
3042 nextchr = UCHARAT(++locinput);
3045 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3048 locinput += PL_utf8skip[nextchr];
3049 if (locinput > PL_regeol)
3051 nextchr = UCHARAT(locinput);
3054 nextchr = UCHARAT(++locinput);
3058 #define ST st->u.trie
3060 /* In this case the charclass data is available inline so
3061 we can fail fast without a lot of extra overhead.
3063 if (scan->flags == EXACT || !utf8_target) {
3064 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3066 PerlIO_printf(Perl_debug_log,
3067 "%*s %sfailed to match trie start class...%s\n",
3068 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3076 /* the basic plan of execution of the trie is:
3077 * At the beginning, run though all the states, and
3078 * find the longest-matching word. Also remember the position
3079 * of the shortest matching word. For example, this pattern:
3082 * when matched against the string "abcde", will generate
3083 * accept states for all words except 3, with the longest
3084 * matching word being 4, and the shortest being 1 (with
3085 * the position being after char 1 of the string).
3087 * Then for each matching word, in word order (i.e. 1,2,4,5),
3088 * we run the remainder of the pattern; on each try setting
3089 * the current position to the character following the word,
3090 * returning to try the next word on failure.
3092 * We avoid having to build a list of words at runtime by
3093 * using a compile-time structure, wordinfo[].prev, which
3094 * gives, for each word, the previous accepting word (if any).
3095 * In the case above it would contain the mappings 1->2, 2->0,
3096 * 3->0, 4->5, 5->1. We can use this table to generate, from
3097 * the longest word (4 above), a list of all words, by
3098 * following the list of prev pointers; this gives us the
3099 * unordered list 4,5,1,2. Then given the current word we have
3100 * just tried, we can go through the list and find the
3101 * next-biggest word to try (so if we just failed on word 2,
3102 * the next in the list is 4).
3104 * Since at runtime we don't record the matching position in
3105 * the string for each word, we have to work that out for
3106 * each word we're about to process. The wordinfo table holds
3107 * the character length of each word; given that we recorded
3108 * at the start: the position of the shortest word and its
3109 * length in chars, we just need to move the pointer the
3110 * difference between the two char lengths. Depending on
3111 * Unicode status and folding, that's cheap or expensive.
3113 * This algorithm is optimised for the case where are only a
3114 * small number of accept states, i.e. 0,1, or maybe 2.
3115 * With lots of accepts states, and having to try all of them,
3116 * it becomes quadratic on number of accept states to find all
3121 /* what type of TRIE am I? (utf8 makes this contextual) */
3122 DECL_TRIE_TYPE(scan);
3124 /* what trie are we using right now */
3125 reg_trie_data * const trie
3126 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3127 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3128 U32 state = trie->startstate;
3130 if (trie->bitmap && trie_type != trie_utf8_fold &&
3131 !TRIE_BITMAP_TEST(trie,*locinput)
3133 if (trie->states[ state ].wordnum) {
3135 PerlIO_printf(Perl_debug_log,
3136 "%*s %smatched empty string...%s\n",
3137 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3142 PerlIO_printf(Perl_debug_log,
3143 "%*s %sfailed to match trie start class...%s\n",
3144 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3151 U8 *uc = ( U8* )locinput;
3155 U8 *uscan = (U8*)NULL;
3156 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3157 U32 charcount = 0; /* how many input chars we have matched */
3158 U32 accepted = 0; /* have we seen any accepting states? */
3161 ST.jump = trie->jump;
3164 ST.longfold = FALSE; /* char longer if folded => it's harder */
3167 /* fully traverse the TRIE; note the position of the
3168 shortest accept state and the wordnum of the longest
3171 while ( state && uc <= (U8*)PL_regeol ) {
3172 U32 base = trie->states[ state ].trans.base;
3176 wordnum = trie->states[ state ].wordnum;
3178 if (wordnum) { /* it's an accept state */
3181 /* record first match position */
3183 ST.firstpos = (U8*)locinput;
3188 ST.firstchars = charcount;
3191 if (!ST.nextword || wordnum < ST.nextword)
3192 ST.nextword = wordnum;
3193 ST.topword = wordnum;
3196 DEBUG_TRIE_EXECUTE_r({
3197 DUMP_EXEC_POS( (char *)uc, scan, utf8_target );
3198 PerlIO_printf( Perl_debug_log,
3199 "%*s %sState: %4"UVxf" Accepted: %c ",
3200 2+depth * 2, "", PL_colors[4],
3201 (UV)state, (accepted ? 'Y' : 'N'));
3204 /* read a char and goto next state */
3207 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3208 uscan, len, uvc, charid, foldlen,
3215 base + charid - 1 - trie->uniquecharcount)) >= 0)
3217 && ((U32)offset < trie->lasttrans)
3218 && trie->trans[offset].check == state)
3220 state = trie->trans[offset].next;
3231 DEBUG_TRIE_EXECUTE_r(
3232 PerlIO_printf( Perl_debug_log,
3233 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3234 charid, uvc, (UV)state, PL_colors[5] );
3240 /* calculate total number of accept states */
3245 w = trie->wordinfo[w].prev;
3248 ST.accepted = accepted;
3252 PerlIO_printf( Perl_debug_log,
3253 "%*s %sgot %"IVdf" possible matches%s\n",
3254 REPORT_CODE_OFF + depth * 2, "",
3255 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3257 goto trie_first_try; /* jump into the fail handler */
3261 case TRIE_next_fail: /* we failed - try next alternative */
3263 REGCP_UNWIND(ST.cp);
3264 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3265 PL_regoffs[n].end = -1;
3266 *PL_reglastparen = n;
3268 if (!--ST.accepted) {
3270 PerlIO_printf( Perl_debug_log,
3271 "%*s %sTRIE failed...%s\n",
3272 REPORT_CODE_OFF+depth*2, "",
3279 /* Find next-highest word to process. Note that this code
3280 * is O(N^2) per trie run (O(N) per branch), so keep tight */
3281 register U16 min = 0;
3283 register U16 const nextword = ST.nextword;
3284 register reg_trie_wordinfo * const wordinfo
3285 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3286 for (word=ST.topword; word; word=wordinfo[word].prev) {
3287 if (word > nextword && (!min || word < min))
3300 ST.lastparen = *PL_reglastparen;
3304 /* find start char of end of current word */
3306 U32 chars; /* how many chars to skip */
3307 U8 *uc = ST.firstpos;
3308 reg_trie_data * const trie
3309 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3311 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3313 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3317 /* the hard option - fold each char in turn and find
3318 * its folded length (which may be different */
3319 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3327 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3335 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3340 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3354 PL_reginput = (char *)uc;
3357 scan = (ST.jump && ST.jump[ST.nextword])
3358 ? ST.me + ST.jump[ST.nextword]
3362 PerlIO_printf( Perl_debug_log,
3363 "%*s %sTRIE matched word #%d, continuing%s\n",
3364 REPORT_CODE_OFF+depth*2, "",
3371 if (ST.accepted > 1 || has_cutgroup) {
3372 PUSH_STATE_GOTO(TRIE_next, scan);
3375 /* only one choice left - just continue */
3377 AV *const trie_words
3378 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3379 SV ** const tmp = av_fetch( trie_words,
3381 SV *sv= tmp ? sv_newmortal() : NULL;
3383 PerlIO_printf( Perl_debug_log,
3384 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3385 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3387 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3388 PL_colors[0], PL_colors[1],
3389 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3391 : "not compiled under -Dr",
3395 locinput = PL_reginput;
3396 nextchr = UCHARAT(locinput);
3397 continue; /* execute rest of RE */
3402 char *s = STRING(scan);
3404 if (utf8_target != UTF_PATTERN) {
3405 /* The target and the pattern have differing utf8ness. */
3407 const char * const e = s + ln;
3410 /* The target is utf8, the pattern is not utf8. */
3415 if (NATIVE_TO_UNI(*(U8*)s) !=
3416 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3424 /* The target is not utf8, the pattern is utf8. */
3429 if (NATIVE_TO_UNI(*((U8*)l)) !=
3430 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3438 nextchr = UCHARAT(locinput);
3441 /* The target and the pattern have the same utf8ness. */
3442 /* Inline the first character, for speed. */
3443 if (UCHARAT(s) != nextchr)
3445 if (PL_regeol - locinput < ln)
3447 if (ln > 1 && memNE(s, locinput, ln))
3450 nextchr = UCHARAT(locinput);
3454 PL_reg_flags |= RF_tainted;
3457 char * const s = STRING(scan);
3460 if (utf8_target || UTF_PATTERN) {
3461 /* Either target or the pattern are utf8. */
3462 const char * const l = locinput;
3463 char *e = PL_regeol;
3465 if (! foldEQ_utf8(s, 0, ln, cBOOL(UTF_PATTERN),
3466 l, &e, 0, utf8_target)) {
3467 /* One more case for the sharp s:
3468 * pack("U0U*", 0xDF) =~ /ss/i,
3469 * the 0xC3 0x9F are the UTF-8
3470 * byte sequence for the U+00DF. */
3472 if (!(utf8_target &&
3473 toLOWER(s[0]) == 's' &&
3475 toLOWER(s[1]) == 's' &&
3482 nextchr = UCHARAT(locinput);
3486 /* Neither the target and the pattern are utf8. */
3488 /* Inline the first character, for speed. */
3489 if (UCHARAT(s) != nextchr &&
3490 UCHARAT(s) != ((OP(scan) == EXACTF)
3491 ? PL_fold : PL_fold_locale)[nextchr])
3493 if (PL_regeol - locinput < ln)
3495 if (ln > 1 && (OP(scan) == EXACTF
3496 ? ! foldEQ(s, locinput, ln)
3497 : ! foldEQ_locale(s, locinput, ln)))
3500 nextchr = UCHARAT(locinput);
3505 PL_reg_flags |= RF_tainted;
3509 /* was last char in word? */
3511 if (locinput == PL_bostr)
3514 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3516 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3518 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3519 ln = isALNUM_uni(ln);
3520 LOAD_UTF8_CHARCLASS_ALNUM();
3521 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, utf8_target);
3524 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3525 n = isALNUM_LC_utf8((U8*)locinput);
3529 ln = (locinput != PL_bostr) ?
3530 UCHARAT(locinput - 1) : '\n';
3531 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3533 n = isALNUM(nextchr);
3536 ln = isALNUM_LC(ln);
3537 n = isALNUM_LC(nextchr);
3540 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3541 OP(scan) == BOUNDL))
3546 STRLEN inclasslen = PL_regeol - locinput;
3548 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, utf8_target))
3550 if (locinput >= PL_regeol)
3552 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3553 nextchr = UCHARAT(locinput);
3558 nextchr = UCHARAT(locinput);
3559 if (!REGINCLASS(rex, scan, (U8*)locinput))
3561 if (!nextchr && locinput >= PL_regeol)
3563 nextchr = UCHARAT(++locinput);
3567 /* If we might have the case of the German sharp s
3568 * in a casefolding Unicode character class. */
3570 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3571 locinput += SHARP_S_SKIP;
3572 nextchr = UCHARAT(locinput);
3577 /* Special char classes - The defines start on line 129 or so */
3578 CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3579 CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3581 CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3582 CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3584 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3585 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3587 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3588 a Unicode extended Grapheme Cluster */
3589 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3590 extended Grapheme Cluster is:
3593 | Prepend* Begin Extend*
3596 Begin is (Hangul-syllable | ! Control)
3597 Extend is (Grapheme_Extend | Spacing_Mark)
3598 Control is [ GCB_Control CR LF ]
3600 The discussion below shows how the code for CLUMP is derived
3601 from this regex. Note that most of these concepts are from
3602 property values of the Grapheme Cluster Boundary (GCB) property.
3603 No code point can have multiple property values for a given
3604 property. Thus a code point in Prepend can't be in Control, but
3605 it must be in !Control. This is why Control above includes
3606 GCB_Control plus CR plus LF. The latter two are used in the GCB
3607 property separately, and so can't be in GCB_Control, even though
3608 they logically are controls. Control is not the same as gc=cc,
3609 but includes format and other characters as well.
3611 The Unicode definition of Hangul-syllable is:
3613 | (L* ( ( V | LV ) V* | LVT ) T*)
3616 Each of these is a value for the GCB property, and hence must be
3617 disjoint, so the order they are tested is immaterial, so the
3618 above can safely be changed to
3621 | (L* ( LVT | ( V | LV ) V*) T*)
3623 The last two terms can be combined like this:
3625 | (( LVT | ( V | LV ) V*) T*))
3627 And refactored into this:
3628 L* (L | LVT T* | V V* T* | LV V* T*)
3630 That means that if we have seen any L's at all we can quit
3631 there, but if the next character is a LVT, a V or and LV we
3634 There is a subtlety with Prepend* which showed up in testing.
3635 Note that the Begin, and only the Begin is required in:
3636 | Prepend* Begin Extend*
3637 Also, Begin contains '! Control'. A Prepend must be a '!
3638 Control', which means it must be a Begin. What it comes down to
3639 is that if we match Prepend* and then find no suitable Begin
3640 afterwards, that if we backtrack the last Prepend, that one will
3641 be a suitable Begin.
3644 if (locinput >= PL_regeol)
3646 if (! utf8_target) {
3648 /* Match either CR LF or '.', as all the other possibilities
3650 locinput++; /* Match the . or CR */
3652 && locinput < PL_regeol
3653 && UCHARAT(locinput) == '\n') locinput++;
3657 /* Utf8: See if is ( CR LF ); already know that locinput <
3658 * PL_regeol, so locinput+1 is in bounds */
3659 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3663 /* In case have to backtrack to beginning, then match '.' */
3664 char *starting = locinput;
3666 /* In case have to backtrack the last prepend */
3667 char *previous_prepend = 0;
3669 LOAD_UTF8_CHARCLASS_GCB();
3671 /* Match (prepend)* */
3672 while (locinput < PL_regeol
3673 && swash_fetch(PL_utf8_X_prepend,
3674 (U8*)locinput, utf8_target))
3676 previous_prepend = locinput;
3677 locinput += UTF8SKIP(locinput);
3680 /* As noted above, if we matched a prepend character, but
3681 * the next thing won't match, back off the last prepend we
3682 * matched, as it is guaranteed to match the begin */
3683 if (previous_prepend
3684 && (locinput >= PL_regeol
3685 || ! swash_fetch(PL_utf8_X_begin,
3686 (U8*)locinput, utf8_target)))
3688 locinput = previous_prepend;
3691 /* Note that here we know PL_regeol > locinput, as we
3692 * tested that upon input to this switch case, and if we
3693 * moved locinput forward, we tested the result just above
3694 * and it either passed, or we backed off so that it will
3696 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, utf8_target)) {
3698 /* Here did not match the required 'Begin' in the
3699 * second term. So just match the very first
3700 * character, the '.' of the final term of the regex */
3701 locinput = starting + UTF8SKIP(starting);
3704 /* Here is the beginning of a character that can have
3705 * an extender. It is either a hangul syllable, or a
3707 if (swash_fetch(PL_utf8_X_non_hangul,
3708 (U8*)locinput, utf8_target))
3711 /* Here not a Hangul syllable, must be a
3712 * ('! * Control') */
3713 locinput += UTF8SKIP(locinput);
3716 /* Here is a Hangul syllable. It can be composed
3717 * of several individual characters. One
3718 * possibility is T+ */
3719 if (swash_fetch(PL_utf8_X_T,
3720 (U8*)locinput, utf8_target))
3722 while (locinput < PL_regeol
3723 && swash_fetch(PL_utf8_X_T,
3724 (U8*)locinput, utf8_target))
3726 locinput += UTF8SKIP(locinput);
3730 /* Here, not T+, but is a Hangul. That means
3731 * it is one of the others: L, LV, LVT or V,
3733 * L* (L | LVT T* | V V* T* | LV V* T*) */
3736 while (locinput < PL_regeol
3737 && swash_fetch(PL_utf8_X_L,
3738 (U8*)locinput, utf8_target))
3740 locinput += UTF8SKIP(locinput);
3743 /* Here, have exhausted L*. If the next
3744 * character is not an LV, LVT nor V, it means
3745 * we had to have at least one L, so matches L+
3746 * in the original equation, we have a complete
3747 * hangul syllable. Are done. */
3749 if (locinput < PL_regeol
3750 && swash_fetch(PL_utf8_X_LV_LVT_V,
3751 (U8*)locinput, utf8_target))
3754 /* Otherwise keep going. Must be LV, LVT
3755 * or V. See if LVT */
3756 if (swash_fetch(PL_utf8_X_LVT,
3757 (U8*)locinput, utf8_target))
3759 locinput += UTF8SKIP(locinput);
3762 /* Must be V or LV. Take it, then
3764 locinput += UTF8SKIP(locinput);
3765 while (locinput < PL_regeol
3766 && swash_fetch(PL_utf8_X_V,
3767 (U8*)locinput, utf8_target))
3769 locinput += UTF8SKIP(locinput);
3773 /* And any of LV, LVT, or V can be followed
3775 while (locinput < PL_regeol
3776 && swash_fetch(PL_utf8_X_T,
3780 locinput += UTF8SKIP(locinput);
3786 /* Match any extender */
3787 while (locinput < PL_regeol
3788 && swash_fetch(PL_utf8_X_extend,
3789 (U8*)locinput, utf8_target))
3791 locinput += UTF8SKIP(locinput);
3795 if (locinput > PL_regeol) sayNO;
3797 nextchr = UCHARAT(locinput);
3804 PL_reg_flags |= RF_tainted;
3809 n = reg_check_named_buff_matched(rex,scan);
3812 type = REF + ( type - NREF );
3819 PL_reg_flags |= RF_tainted;
3823 n = ARG(scan); /* which paren pair */
3826 ln = PL_regoffs[n].start;
3827 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3828 if (*PL_reglastparen < n || ln == -1)
3829 sayNO; /* Do not match unless seen CLOSEn. */
3830 if (ln == PL_regoffs[n].end)
3834 if (utf8_target && type != REF) { /* REF can do byte comparison */
3836 const char *e = PL_bostr + PL_regoffs[n].end;
3838 * Note that we can't do the "other character" lookup trick as
3839 * in the 8-bit case (no pun intended) because in Unicode we
3840 * have to map both upper and title case to lower case.
3844 STRLEN ulen1, ulen2;
3845 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3846 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3850 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3851 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3852 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3859 nextchr = UCHARAT(locinput);
3863 /* Inline the first character, for speed. */
3864 if (UCHARAT(s) != nextchr &&
3866 (UCHARAT(s) != (type == REFF
3867 ? PL_fold : PL_fold_locale)[nextchr])))
3869 ln = PL_regoffs[n].end - ln;
3870 if (locinput + ln > PL_regeol)
3872 if (ln > 1 && (type == REF
3873 ? memNE(s, locinput, ln)
3875 ? ! foldEQ(s, locinput, ln)
3876 : ! foldEQ_locale(s, locinput, ln))))
3879 nextchr = UCHARAT(locinput);
3889 #define ST st->u.eval
3894 regexp_internal *rei;
3895 regnode *startpoint;
3898 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3899 if (cur_eval && cur_eval->locinput==locinput) {
3900 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3901 Perl_croak(aTHX_ "Infinite recursion in regex");
3902 if ( ++nochange_depth > max_nochange_depth )
3904 "Pattern subroutine nesting without pos change"
3905 " exceeded limit in regex");
3912 (void)ReREFCNT_inc(rex_sv);
3913 if (OP(scan)==GOSUB) {
3914 startpoint = scan + ARG2L(scan);
3915 ST.close_paren = ARG(scan);
3917 startpoint = rei->program+1;
3920 goto eval_recurse_doit;
3922 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3923 if (cur_eval && cur_eval->locinput==locinput) {
3924 if ( ++nochange_depth > max_nochange_depth )
3925 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3930 /* execute the code in the {...} */
3932 SV ** const before = SP;
3933 OP_4tree * const oop = PL_op;
3934 COP * const ocurcop = PL_curcop;
3936 char *saved_regeol = PL_regeol;
3939 PL_op = (OP_4tree*)rexi->data->data[n];
3940 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3941 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3942 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3943 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3946 SV *sv_mrk = get_sv("REGMARK", 1);
3947 sv_setsv(sv_mrk, sv_yes_mark);
3950 CALLRUNOPS(aTHX); /* Scalar context. */
3953 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3960 PAD_RESTORE_LOCAL(old_comppad);
3961 PL_curcop = ocurcop;
3962 PL_regeol = saved_regeol;
3965 sv_setsv(save_scalar(PL_replgv), ret);
3969 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
3972 /* extract RE object from returned value; compiling if
3978 SV *const sv = SvRV(ret);
3980 if (SvTYPE(sv) == SVt_REGEXP) {
3982 } else if (SvSMAGICAL(sv)) {
3983 mg = mg_find(sv, PERL_MAGIC_qr);
3986 } else if (SvTYPE(ret) == SVt_REGEXP) {
3988 } else if (SvSMAGICAL(ret)) {
3989 if (SvGMAGICAL(ret)) {
3990 /* I don't believe that there is ever qr magic
3992 assert(!mg_find(ret, PERL_MAGIC_qr));
3993 sv_unmagic(ret, PERL_MAGIC_qr);
3996 mg = mg_find(ret, PERL_MAGIC_qr);
3997 /* testing suggests mg only ends up non-NULL for
3998 scalars who were upgraded and compiled in the
3999 else block below. In turn, this is only
4000 triggered in the "postponed utf8 string" tests
4006 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
4010 rx = reg_temp_copy(NULL, rx);
4014 const I32 osize = PL_regsize;
4017 assert (SvUTF8(ret));
4018 } else if (SvUTF8(ret)) {
4019 /* Not doing UTF-8, despite what the SV says. Is
4020 this only if we're trapped in use 'bytes'? */
4021 /* Make a copy of the octet sequence, but without
4022 the flag on, as the compiler now honours the
4023 SvUTF8 flag on ret. */
4025 const char *const p = SvPV(ret, len);
4026 ret = newSVpvn_flags(p, len, SVs_TEMP);
4028 rx = CALLREGCOMP(ret, pm_flags);
4030 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
4032 /* This isn't a first class regexp. Instead, it's
4033 caching a regexp onto an existing, Perl visible
4035 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
4040 re = (struct regexp *)SvANY(rx);
4042 RXp_MATCH_COPIED_off(re);
4043 re->subbeg = rex->subbeg;
4044 re->sublen = rex->sublen;
4047 debug_start_match(re_sv, utf8_target, locinput, PL_regeol,
4048 "Matching embedded");
4050 startpoint = rei->program + 1;
4051 ST.close_paren = 0; /* only used for GOSUB */
4052 /* borrowed from regtry */
4053 if (PL_reg_start_tmpl <= re->nparens) {
4054 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4055 if(PL_reg_start_tmp)
4056 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4058 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4061 eval_recurse_doit: /* Share code with GOSUB below this line */
4062 /* run the pattern returned from (??{...}) */
4063 ST.cp = regcppush(0); /* Save *all* the positions. */
4064 REGCP_SET(ST.lastcp);
4066 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
4068 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4069 PL_reglastparen = &re->lastparen;
4070 PL_reglastcloseparen = &re->lastcloseparen;
4072 re->lastcloseparen = 0;
4074 PL_reginput = locinput;
4077 /* XXXX This is too dramatic a measure... */
4080 ST.toggle_reg_flags = PL_reg_flags;
4082 PL_reg_flags |= RF_utf8;
4084 PL_reg_flags &= ~RF_utf8;
4085 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4087 ST.prev_rex = rex_sv;
4088 ST.prev_curlyx = cur_curlyx;
4089 SETREX(rex_sv,re_sv);
4094 ST.prev_eval = cur_eval;
4096 /* now continue from first node in postoned RE */
4097 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4100 /* logical is 1, /(?(?{...})X|Y)/ */
4101 sw = cBOOL(SvTRUE(ret));
4106 case EVAL_AB: /* cleanup after a successful (??{A})B */
4107 /* note: this is called twice; first after popping B, then A */
4108 PL_reg_flags ^= ST.toggle_reg_flags;
4109 ReREFCNT_dec(rex_sv);
4110 SETREX(rex_sv,ST.prev_rex);
4111 rex = (struct regexp *)SvANY(rex_sv);
4112 rexi = RXi_GET(rex);
4114 cur_eval = ST.prev_eval;
4115 cur_curlyx = ST.prev_curlyx;
4117 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4118 PL_reglastparen = &rex->lastparen;
4119 PL_reglastcloseparen = &rex->lastcloseparen;
4120 /* also update PL_regoffs */
4121 PL_regoffs = rex->offs;
4123 /* XXXX This is too dramatic a measure... */
4125 if ( nochange_depth )
4130 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4131 /* note: this is called twice; first after popping B, then A */
4132 PL_reg_flags ^= ST.toggle_reg_flags;
4133 ReREFCNT_dec(rex_sv);
4134 SETREX(rex_sv,ST.prev_rex);
4135 rex = (struct regexp *)SvANY(rex_sv);
4136 rexi = RXi_GET(rex);
4137 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4138 PL_reglastparen = &rex->lastparen;
4139 PL_reglastcloseparen = &rex->lastcloseparen;
4141 PL_reginput = locinput;
4142 REGCP_UNWIND(ST.lastcp);
4144 cur_eval = ST.prev_eval;
4145 cur_curlyx = ST.prev_curlyx;
4146 /* XXXX This is too dramatic a measure... */
4148 if ( nochange_depth )
4154 n = ARG(scan); /* which paren pair */
4155 PL_reg_start_tmp[n] = locinput;
4161 n = ARG(scan); /* which paren pair */
4162 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4163 PL_regoffs[n].end = locinput - PL_bostr;
4164 /*if (n > PL_regsize)
4166 if (n > *PL_reglastparen)
4167 *PL_reglastparen = n;
4168 *PL_reglastcloseparen = n;
4169 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4177 cursor && OP(cursor)!=END;
4178 cursor=regnext(cursor))
4180 if ( OP(cursor)==CLOSE ){
4182 if ( n <= lastopen ) {
4184 = PL_reg_start_tmp[n] - PL_bostr;
4185 PL_regoffs[n].end = locinput - PL_bostr;
4186 /*if (n > PL_regsize)
4188 if (n > *PL_reglastparen)
4189 *PL_reglastparen = n;
4190 *PL_reglastcloseparen = n;
4191 if ( n == ARG(scan) || (cur_eval &&
4192 cur_eval->u.eval.close_paren == n))
4201 n = ARG(scan); /* which paren pair */
4202 sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
4205 /* reg_check_named_buff_matched returns 0 for no match */
4206 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
4210 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4216 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4218 next = NEXTOPER(NEXTOPER(scan));
4220 next = scan + ARG(scan);
4221 if (OP(next) == IFTHEN) /* Fake one. */
4222 next = NEXTOPER(NEXTOPER(next));
4226 logical = scan->flags;
4229 /*******************************************************************
4231 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4232 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4233 STAR/PLUS/CURLY/CURLYN are used instead.)
4235 A*B is compiled as <CURLYX><A><WHILEM><B>
4237 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4238 state, which contains the current count, initialised to -1. It also sets
4239 cur_curlyx to point to this state, with any previous value saved in the
4242 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4243 since the pattern may possibly match zero times (i.e. it's a while {} loop
4244 rather than a do {} while loop).
4246 Each entry to WHILEM represents a successful match of A. The count in the
4247 CURLYX block is incremented, another WHILEM state is pushed, and execution
4248 passes to A or B depending on greediness and the current count.
4250 For example, if matching against the string a1a2a3b (where the aN are
4251 substrings that match /A/), then the match progresses as follows: (the
4252 pushed states are interspersed with the bits of strings matched so far):
4255 <CURLYX cnt=0><WHILEM>
4256 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4257 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4258 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4259 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4261 (Contrast this with something like CURLYM, which maintains only a single
4265 a1 <CURLYM cnt=1> a2
4266 a1 a2 <CURLYM cnt=2> a3
4267 a1 a2 a3 <CURLYM cnt=3> b
4270 Each WHILEM state block marks a point to backtrack to upon partial failure
4271 of A or B, and also contains some minor state data related to that
4272 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4273 overall state, such as the count, and pointers to the A and B ops.
4275 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4276 must always point to the *current* CURLYX block, the rules are:
4278 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4279 and set cur_curlyx to point the new block.
4281 When popping the CURLYX block after a successful or unsuccessful match,
4282 restore the previous cur_curlyx.
4284 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4285 to the outer one saved in the CURLYX block.
4287 When popping the WHILEM block after a successful or unsuccessful B match,
4288 restore the previous cur_curlyx.
4290 Here's an example for the pattern (AI* BI)*BO
4291 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4294 curlyx backtrack stack
4295 ------ ---------------
4297 CO <CO prev=NULL> <WO>
4298 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4299 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4300 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4302 At this point the pattern succeeds, and we work back down the stack to
4303 clean up, restoring as we go:
4305 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4306 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4307 CO <CO prev=NULL> <WO>
4310 *******************************************************************/
4312 #define ST st->u.curlyx
4314 case CURLYX: /* start of /A*B/ (for complex A) */
4316 /* No need to save/restore up to this paren */
4317 I32 parenfloor = scan->flags;
4319 assert(next); /* keep Coverity happy */
4320 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4323 /* XXXX Probably it is better to teach regpush to support
4324 parenfloor > PL_regsize... */
4325 if (parenfloor > (I32)*PL_reglastparen)
4326 parenfloor = *PL_reglastparen; /* Pessimization... */
4328 ST.prev_curlyx= cur_curlyx;
4330 ST.cp = PL_savestack_ix;
4332 /* these fields contain the state of the current curly.
4333 * they are accessed by subsequent WHILEMs */
4334 ST.parenfloor = parenfloor;
4339 ST.count = -1; /* this will be updated by WHILEM */
4340 ST.lastloc = NULL; /* this will be updated by WHILEM */
4342 PL_reginput = locinput;
4343 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4347 case CURLYX_end: /* just finished matching all of A*B */
4348 cur_curlyx = ST.prev_curlyx;
4352 case CURLYX_end_fail: /* just failed to match all of A*B */
4354 cur_curlyx = ST.prev_curlyx;
4360 #define ST st->u.whilem
4362 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4364 /* see the discussion above about CURLYX/WHILEM */
4366 int min = ARG1(cur_curlyx->u.curlyx.me);
4367 int max = ARG2(cur_curlyx->u.curlyx.me);
4368 regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
4370 assert(cur_curlyx); /* keep Coverity happy */
4371 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4372 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4373 ST.cache_offset = 0;
4376 PL_reginput = locinput;
4378 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4379 "%*s whilem: matched %ld out of %d..%d\n",
4380 REPORT_CODE_OFF+depth*2, "", (long)n, min, max)
4383 /* First just match a string of min A's. */
4386 cur_curlyx->u.curlyx.lastloc = locinput;
4387 PUSH_STATE_GOTO(WHILEM_A_pre, A);
4391 /* If degenerate A matches "", assume A done. */
4393 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4394 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4395 "%*s whilem: empty match detected, trying continuation...\n",
4396 REPORT_CODE_OFF+depth*2, "")
4398 goto do_whilem_B_max;
4401 /* super-linear cache processing */
4405 if (!PL_reg_maxiter) {
4406 /* start the countdown: Postpone detection until we
4407 * know the match is not *that* much linear. */
4408 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4409 /* possible overflow for long strings and many CURLYX's */
4410 if (PL_reg_maxiter < 0)
4411 PL_reg_maxiter = I32_MAX;
4412 PL_reg_leftiter = PL_reg_maxiter;
4415 if (PL_reg_leftiter-- == 0) {
4416 /* initialise cache */
4417 const I32 size = (PL_reg_maxiter + 7)/8;
4418 if (PL_reg_poscache) {
4419 if ((I32)PL_reg_poscache_size < size) {
4420 Renew(PL_reg_poscache, size, char);
4421 PL_reg_poscache_size = size;
4423 Zero(PL_reg_poscache, size, char);
4426 PL_reg_poscache_size = size;
4427 Newxz(PL_reg_poscache, size, char);
4429 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4430 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4431 PL_colors[4], PL_colors[5])
4435 if (PL_reg_leftiter < 0) {
4436 /* have we already failed at this position? */
4438 offset = (scan->flags & 0xf) - 1
4439 + (locinput - PL_bostr) * (scan->flags>>4);
4440 mask = 1 << (offset % 8);
4442 if (PL_reg_poscache[offset] & mask) {
4443 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4444 "%*s whilem: (cache) already tried at this position...\n",
4445 REPORT_CODE_OFF+depth*2, "")
4447 sayNO; /* cache records failure */
4449 ST.cache_offset = offset;
4450 ST.cache_mask = mask;
4454 /* Prefer B over A for minimal matching. */
4456 if (cur_curlyx->u.curlyx.minmod) {
4457 ST.save_curlyx = cur_curlyx;
4458 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4459 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4460 REGCP_SET(ST.lastcp);
4461 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4465 /* Prefer A over B for maximal matching. */
4467 if (n < max) { /* More greed allowed? */
4468 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4469 cur_curlyx->u.curlyx.lastloc = locinput;
4470 REGCP_SET(ST.lastcp);
4471 PUSH_STATE_GOTO(WHILEM_A_max, A);
4474 goto do_whilem_B_max;
4478 case WHILEM_B_min: /* just matched B in a minimal match */
4479 case WHILEM_B_max: /* just matched B in a maximal match */
4480 cur_curlyx = ST.save_curlyx;
4484 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4485 cur_curlyx = ST.save_curlyx;
4486 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4487 cur_curlyx->u.curlyx.count--;
4491 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4492 REGCP_UNWIND(ST.lastcp);
4495 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4496 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4497 cur_curlyx->u.curlyx.count--;
4501 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4502 REGCP_UNWIND(ST.lastcp);
4503 regcppop(rex); /* Restore some previous $<digit>s? */
4504 PL_reginput = locinput;
4505 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4506 "%*s whilem: failed, trying continuation...\n",
4507 REPORT_CODE_OFF+depth*2, "")
4510 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4511 && ckWARN(WARN_REGEXP)
4512 && !(PL_reg_flags & RF_warned))
4514 PL_reg_flags |= RF_warned;
4515 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4516 "Complex regular subexpression recursion",
4521 ST.save_curlyx = cur_curlyx;
4522 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4523 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4526 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4527 cur_curlyx = ST.save_curlyx;
4528 REGCP_UNWIND(ST.lastcp);
4531 if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
4532 /* Maximum greed exceeded */
4533 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4534 && ckWARN(WARN_REGEXP)
4535 && !(PL_reg_flags & RF_warned))
4537 PL_reg_flags |= RF_warned;
4538 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4539 "%s limit (%d) exceeded",
4540 "Complex regular subexpression recursion",
4543 cur_curlyx->u.curlyx.count--;
4547 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4548 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4550 /* Try grabbing another A and see if it helps. */
4551 PL_reginput = locinput;
4552 cur_curlyx->u.curlyx.lastloc = locinput;
4553 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4554 REGCP_SET(ST.lastcp);
4555 PUSH_STATE_GOTO(WHILEM_A_min,
4556 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS);
4560 #define ST st->u.branch
4562 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4563 next = scan + ARG(scan);
4566 scan = NEXTOPER(scan);
4569 case BRANCH: /* /(...|A|...)/ */
4570 scan = NEXTOPER(scan); /* scan now points to inner node */
4571 ST.lastparen = *PL_reglastparen;
4572 ST.next_branch = next;
4574 PL_reginput = locinput;
4576 /* Now go into the branch */
4578 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4580 PUSH_STATE_GOTO(BRANCH_next, scan);
4584 PL_reginput = locinput;
4585 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4586 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4587 PUSH_STATE_GOTO(CUTGROUP_next,next);
4589 case CUTGROUP_next_fail:
4592 if (st->u.mark.mark_name)
4593 sv_commit = st->u.mark.mark_name;
4599 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4604 REGCP_UNWIND(ST.cp);
4605 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4606 PL_regoffs[n].end = -1;
4607 *PL_reglastparen = n;
4608 /*dmq: *PL_reglastcloseparen = n; */
4609 scan = ST.next_branch;
4610 /* no more branches? */
4611 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4613 PerlIO_printf( Perl_debug_log,
4614 "%*s %sBRANCH failed...%s\n",
4615 REPORT_CODE_OFF+depth*2, "",
4621 continue; /* execute next BRANCH[J] op */
4629 #define ST st->u.curlym
4631 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4633 /* This is an optimisation of CURLYX that enables us to push
4634 * only a single backtracking state, no matter how many matches
4635 * there are in {m,n}. It relies on the pattern being constant
4636 * length, with no parens to influence future backrefs
4640 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4642 /* if paren positive, emulate an OPEN/CLOSE around A */
4644 U32 paren = ST.me->flags;
4645 if (paren > PL_regsize)
4647 if (paren > *PL_reglastparen)
4648 *PL_reglastparen = paren;
4649 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4657 ST.c1 = CHRTEST_UNINIT;
4660 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4663 curlym_do_A: /* execute the A in /A{m,n}B/ */
4664 PL_reginput = locinput;
4665 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4668 case CURLYM_A: /* we've just matched an A */
4669 locinput = st->locinput;
4670 nextchr = UCHARAT(locinput);
4673 /* after first match, determine A's length: u.curlym.alen */
4674 if (ST.count == 1) {
4675 if (PL_reg_match_utf8) {
4677 while (s < PL_reginput) {
4683 ST.alen = PL_reginput - locinput;
4686 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4689 PerlIO_printf(Perl_debug_log,
4690 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4691 (int)(REPORT_CODE_OFF+(depth*2)), "",
4692 (IV) ST.count, (IV)ST.alen)
4695 locinput = PL_reginput;
4697 if (cur_eval && cur_eval->u.eval.close_paren &&
4698 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4702 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4703 if ( max == REG_INFTY || ST.count < max )
4704 goto curlym_do_A; /* try to match another A */
4706 goto curlym_do_B; /* try to match B */
4708 case CURLYM_A_fail: /* just failed to match an A */
4709 REGCP_UNWIND(ST.cp);
4711 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4712 || (cur_eval && cur_eval->u.eval.close_paren &&
4713 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4716 curlym_do_B: /* execute the B in /A{m,n}B/ */
4717 PL_reginput = locinput;
4718 if (ST.c1 == CHRTEST_UNINIT) {
4719 /* calculate c1 and c2 for possible match of 1st char
4720 * following curly */
4721 ST.c1 = ST.c2 = CHRTEST_VOID;
4722 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4723 regnode *text_node = ST.B;
4724 if (! HAS_TEXT(text_node))
4725 FIND_NEXT_IMPT(text_node);
4728 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4730 But the former is redundant in light of the latter.
4732 if this changes back then the macro for
4733 IS_TEXT and friends need to change.
4735 if (PL_regkind[OP(text_node)] == EXACT)
4738 ST.c1 = (U8)*STRING(text_node);
4740 (IS_TEXTF(text_node))
4742 : (IS_TEXTFL(text_node))
4743 ? PL_fold_locale[ST.c1]
4750 PerlIO_printf(Perl_debug_log,
4751 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4752 (int)(REPORT_CODE_OFF+(depth*2)),
4755 if (ST.c1 != CHRTEST_VOID
4756 && UCHARAT(PL_reginput) != ST.c1
4757 && UCHARAT(PL_reginput) != ST.c2)
4759 /* simulate B failing */
4761 PerlIO_printf(Perl_debug_log,
4762 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4763 (int)(REPORT_CODE_OFF+(depth*2)),"",
4766 state_num = CURLYM_B_fail;
4767 goto reenter_switch;
4771 /* mark current A as captured */
4772 I32 paren = ST.me->flags;
4774 PL_regoffs[paren].start
4775 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4776 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4777 /*dmq: *PL_reglastcloseparen = paren; */
4780 PL_regoffs[paren].end = -1;
4781 if (cur_eval && cur_eval->u.eval.close_paren &&
4782 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4791 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4794 case CURLYM_B_fail: /* just failed to match a B */
4795 REGCP_UNWIND(ST.cp);
4797 I32 max = ARG2(ST.me);
4798 if (max != REG_INFTY && ST.count == max)
4800 goto curlym_do_A; /* try to match a further A */
4802 /* backtrack one A */
4803 if (ST.count == ARG1(ST.me) /* min */)
4806 locinput = HOPc(locinput, -ST.alen);
4807 goto curlym_do_B; /* try to match B */
4810 #define ST st->u.curly
4812 #define CURLY_SETPAREN(paren, success) \
4815 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4816 PL_regoffs[paren].end = locinput - PL_bostr; \
4817 *PL_reglastcloseparen = paren; \
4820 PL_regoffs[paren].end = -1; \
4823 case STAR: /* /A*B/ where A is width 1 */
4827 scan = NEXTOPER(scan);
4829 case PLUS: /* /A+B/ where A is width 1 */
4833 scan = NEXTOPER(scan);
4835 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4836 ST.paren = scan->flags; /* Which paren to set */
4837 if (ST.paren > PL_regsize)
4838 PL_regsize = ST.paren;
4839 if (ST.paren > *PL_reglastparen)
4840 *PL_reglastparen = ST.paren;
4841 ST.min = ARG1(scan); /* min to match */
4842 ST.max = ARG2(scan); /* max to match */
4843 if (cur_eval && cur_eval->u.eval.close_paren &&
4844 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4848 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4850 case CURLY: /* /A{m,n}B/ where A is width 1 */
4852 ST.min = ARG1(scan); /* min to match */
4853 ST.max = ARG2(scan); /* max to match */
4854 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4857 * Lookahead to avoid useless match attempts
4858 * when we know what character comes next.
4860 * Used to only do .*x and .*?x, but now it allows
4861 * for )'s, ('s and (?{ ... })'s to be in the way
4862 * of the quantifier and the EXACT-like node. -- japhy
4865 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4867 if (HAS_TEXT(next) || JUMPABLE(next)) {
4869 regnode *text_node = next;
4871 if (! HAS_TEXT(text_node))
4872 FIND_NEXT_IMPT(text_node);
4874 if (! HAS_TEXT(text_node))
4875 ST.c1 = ST.c2 = CHRTEST_VOID;
4877 if ( PL_regkind[OP(text_node)] != EXACT ) {
4878 ST.c1 = ST.c2 = CHRTEST_VOID;
4879 goto assume_ok_easy;
4882 s = (U8*)STRING(text_node);
4884 /* Currently we only get here when
4886 PL_rekind[OP(text_node)] == EXACT
4888 if this changes back then the macro for IS_TEXT and
4889 friends need to change. */
4892 if (IS_TEXTF(text_node))
4893 ST.c2 = PL_fold[ST.c1];
4894 else if (IS_TEXTFL(text_node))
4895 ST.c2 = PL_fold_locale[ST.c1];
4897 else { /* UTF_PATTERN */
4898 if (IS_TEXTF(text_node)) {
4899 STRLEN ulen1, ulen2;
4900 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4901 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4903 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4904 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4906 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4908 0 : UTF8_ALLOW_ANY);
4909 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4911 0 : UTF8_ALLOW_ANY);
4913 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4915 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4920 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4927 ST.c1 = ST.c2 = CHRTEST_VOID;
4932 PL_reginput = locinput;
4935 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4938 locinput = PL_reginput;
4940 if (ST.c1 == CHRTEST_VOID)
4941 goto curly_try_B_min;
4943 ST.oldloc = locinput;
4945 /* set ST.maxpos to the furthest point along the
4946 * string that could possibly match */
4947 if (ST.max == REG_INFTY) {
4948 ST.maxpos = PL_regeol - 1;
4950 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4953 else if (utf8_target) {
4954 int m = ST.max - ST.min;
4955 for (ST.maxpos = locinput;
4956 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4957 ST.maxpos += UTF8SKIP(ST.maxpos);
4960 ST.maxpos = locinput + ST.max - ST.min;
4961 if (ST.maxpos >= PL_regeol)
4962 ST.maxpos = PL_regeol - 1;
4964 goto curly_try_B_min_known;
4968 ST.count = regrepeat(rex, ST.A, ST.max, depth);
4969 locinput = PL_reginput;
4970 if (ST.count < ST.min)
4972 if ((ST.count > ST.min)
4973 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4975 /* A{m,n} must come at the end of the string, there's
4976 * no point in backing off ... */
4978 /* ...except that $ and \Z can match before *and* after
4979 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4980 We may back off by one in this case. */
4981 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4985 goto curly_try_B_max;
4990 case CURLY_B_min_known_fail:
4991 /* failed to find B in a non-greedy match where c1,c2 valid */
4992 if (ST.paren && ST.count)
4993 PL_regoffs[ST.paren].end = -1;
4995 PL_reginput = locinput; /* Could be reset... */
4996 REGCP_UNWIND(ST.cp);
4997 /* Couldn't or didn't -- move forward. */
4998 ST.oldloc = locinput;
5000 locinput += UTF8SKIP(locinput);
5004 curly_try_B_min_known:
5005 /* find the next place where 'B' could work, then call B */
5009 n = (ST.oldloc == locinput) ? 0 : 1;
5010 if (ST.c1 == ST.c2) {
5012 /* set n to utf8_distance(oldloc, locinput) */
5013 while (locinput <= ST.maxpos &&
5014 utf8n_to_uvchr((U8*)locinput,
5015 UTF8_MAXBYTES, &len,
5016 uniflags) != (UV)ST.c1) {
5022 /* set n to utf8_distance(oldloc, locinput) */
5023 while (locinput <= ST.maxpos) {
5025 const UV c = utf8n_to_uvchr((U8*)locinput,
5026 UTF8_MAXBYTES, &len,
5028 if (c == (UV)ST.c1 || c == (UV)ST.c2)
5036 if (ST.c1 == ST.c2) {
5037 while (locinput <= ST.maxpos &&
5038 UCHARAT(locinput) != ST.c1)
5042 while (locinput <= ST.maxpos
5043 && UCHARAT(locinput) != ST.c1
5044 && UCHARAT(locinput) != ST.c2)
5047 n = locinput - ST.oldloc;
5049 if (locinput > ST.maxpos)
5051 /* PL_reginput == oldloc now */
5054 if (regrepeat(rex, ST.A, n, depth) < n)
5057 PL_reginput = locinput;
5058 CURLY_SETPAREN(ST.paren, ST.count);
5059 if (cur_eval && cur_eval->u.eval.close_paren &&
5060 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5063 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5068 case CURLY_B_min_fail:
5069 /* failed to find B in a non-greedy match where c1,c2 invalid */
5070 if (ST.paren && ST.count)
5071 PL_regoffs[ST.paren].end = -1;
5073 REGCP_UNWIND(ST.cp);
5074 /* failed -- move forward one */
5075 PL_reginput = locinput;
5076 if (regrepeat(rex, ST.A, 1, depth)) {
5078 locinput = PL_reginput;
5079 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5080 ST.count > 0)) /* count overflow ? */
5083 CURLY_SETPAREN(ST.paren, ST.count);
5084 if (cur_eval && cur_eval->u.eval.close_paren &&
5085 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5088 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5096 /* a successful greedy match: now try to match B */
5097 if (cur_eval && cur_eval->u.eval.close_paren &&
5098 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5103 if (ST.c1 != CHRTEST_VOID)
5104 c = utf8_target ? utf8n_to_uvchr((U8*)PL_reginput,
5105 UTF8_MAXBYTES, 0, uniflags)
5106 : (UV) UCHARAT(PL_reginput);
5107 /* If it could work, try it. */
5108 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5109 CURLY_SETPAREN(ST.paren, ST.count);
5110 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5115 case CURLY_B_max_fail:
5116 /* failed to find B in a greedy match */
5117 if (ST.paren && ST.count)
5118 PL_regoffs[ST.paren].end = -1;
5120 REGCP_UNWIND(ST.cp);
5122 if (--ST.count < ST.min)
5124 PL_reginput = locinput = HOPc(locinput, -1);
5125 goto curly_try_B_max;
5132 /* we've just finished A in /(??{A})B/; now continue with B */
5134 st->u.eval.toggle_reg_flags
5135 = cur_eval->u.eval.toggle_reg_flags;
5136 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5138 st->u.eval.prev_rex = rex_sv; /* inner */
5139 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5140 rex = (struct regexp *)SvANY(rex_sv);
5141 rexi = RXi_GET(rex);
5142 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5143 ReREFCNT_inc(rex_sv);
5144 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
5146 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5147 PL_reglastparen = &rex->lastparen;
5148 PL_reglastcloseparen = &rex->lastcloseparen;
5150 REGCP_SET(st->u.eval.lastcp);
5151 PL_reginput = locinput;
5153 /* Restore parens of the outer rex without popping the
5155 tmpix = PL_savestack_ix;
5156 PL_savestack_ix = cur_eval->u.eval.lastcp;
5158 PL_savestack_ix = tmpix;
5160 st->u.eval.prev_eval = cur_eval;
5161 cur_eval = cur_eval->u.eval.prev_eval;
5163 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5164 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5165 if ( nochange_depth )
5168 PUSH_YES_STATE_GOTO(EVAL_AB,
5169 st->u.eval.prev_eval->u.eval.B); /* match B */
5172 if (locinput < reginfo->till) {
5173 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5174 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5176 (long)(locinput - PL_reg_starttry),
5177 (long)(reginfo->till - PL_reg_starttry),
5180 sayNO_SILENT; /* Cannot match: too short. */
5182 PL_reginput = locinput; /* put where regtry can find it */
5183 sayYES; /* Success! */
5185 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5187 PerlIO_printf(Perl_debug_log,
5188 "%*s %ssubpattern success...%s\n",
5189 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5190 PL_reginput = locinput; /* put where regtry can find it */
5191 sayYES; /* Success! */
5194 #define ST st->u.ifmatch
5196 case SUSPEND: /* (?>A) */
5198 PL_reginput = locinput;
5201 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5203 goto ifmatch_trivial_fail_test;
5205 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5207 ifmatch_trivial_fail_test:
5209 char * const s = HOPBACKc(locinput, scan->flags);
5214 sw = 1 - cBOOL(ST.wanted);
5218 next = scan + ARG(scan);
5226 PL_reginput = locinput;
5230 ST.logical = logical;
5231 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5233 /* execute body of (?...A) */
5234 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5237 case IFMATCH_A_fail: /* body of (?...A) failed */
5238 ST.wanted = !ST.wanted;
5241 case IFMATCH_A: /* body of (?...A) succeeded */
5243 sw = cBOOL(ST.wanted);
5245 else if (!ST.wanted)
5248 if (OP(ST.me) == SUSPEND)
5249 locinput = PL_reginput;
5251 locinput = PL_reginput = st->locinput;
5252 nextchr = UCHARAT(locinput);
5254 scan = ST.me + ARG(ST.me);
5257 continue; /* execute B */
5262 next = scan + ARG(scan);
5267 reginfo->cutpoint = PL_regeol;
5270 PL_reginput = locinput;
5272 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5273 PUSH_STATE_GOTO(COMMIT_next,next);
5275 case COMMIT_next_fail:
5282 #define ST st->u.mark
5284 ST.prev_mark = mark_state;
5285 ST.mark_name = sv_commit = sv_yes_mark
5286 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5288 ST.mark_loc = PL_reginput = locinput;
5289 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5291 case MARKPOINT_next:
5292 mark_state = ST.prev_mark;
5295 case MARKPOINT_next_fail:
5296 if (popmark && sv_eq(ST.mark_name,popmark))
5298 if (ST.mark_loc > startpoint)
5299 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5300 popmark = NULL; /* we found our mark */
5301 sv_commit = ST.mark_name;
5304 PerlIO_printf(Perl_debug_log,
5305 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5306 REPORT_CODE_OFF+depth*2, "",
5307 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5310 mark_state = ST.prev_mark;
5311 sv_yes_mark = mark_state ?
5312 mark_state->u.mark.mark_name : NULL;
5316 PL_reginput = locinput;
5318 /* (*SKIP) : if we fail we cut here*/
5319 ST.mark_name = NULL;
5320 ST.mark_loc = locinput;
5321 PUSH_STATE_GOTO(SKIP_next,next);
5323 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5324 otherwise do nothing. Meaning we need to scan
5326 regmatch_state *cur = mark_state;
5327 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5330 if ( sv_eq( cur->u.mark.mark_name,
5333 ST.mark_name = find;
5334 PUSH_STATE_GOTO( SKIP_next, next );
5336 cur = cur->u.mark.prev_mark;
5339 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5341 case SKIP_next_fail:
5343 /* (*CUT:NAME) - Set up to search for the name as we
5344 collapse the stack*/
5345 popmark = ST.mark_name;
5347 /* (*CUT) - No name, we cut here.*/
5348 if (ST.mark_loc > startpoint)
5349 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5350 /* but we set sv_commit to latest mark_name if there
5351 is one so they can test to see how things lead to this
5354 sv_commit=mark_state->u.mark.mark_name;
5362 if ( n == (U32)what_len_TRICKYFOLD(locinput,utf8_target,ln) ) {
5364 } else if ( 0xDF == n && !utf8_target && !UTF_PATTERN ) {
5367 U8 folded[UTF8_MAXBYTES_CASE+1];
5369 const char * const l = locinput;
5370 char *e = PL_regeol;
5371 to_uni_fold(n, folded, &foldlen);
5373 if (! foldEQ_utf8((const char*) folded, 0, foldlen, 1,
5374 l, &e, 0, utf8_target)) {
5379 nextchr = UCHARAT(locinput);
5382 if ((n=is_LNBREAK(locinput,utf8_target))) {
5384 nextchr = UCHARAT(locinput);
5389 #define CASE_CLASS(nAmE) \
5391 if ((n=is_##nAmE(locinput,utf8_target))) { \
5393 nextchr = UCHARAT(locinput); \
5398 if ((n=is_##nAmE(locinput,utf8_target))) { \
5401 locinput += UTF8SKIP(locinput); \
5402 nextchr = UCHARAT(locinput); \
5407 CASE_CLASS(HORIZWS);
5411 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5412 PTR2UV(scan), OP(scan));
5413 Perl_croak(aTHX_ "regexp memory corruption");
5417 /* switch break jumps here */
5418 scan = next; /* prepare to execute the next op and ... */
5419 continue; /* ... jump back to the top, reusing st */
5423 /* push a state that backtracks on success */
5424 st->u.yes.prev_yes_state = yes_state;
5428 /* push a new regex state, then continue at scan */
5430 regmatch_state *newst;
5433 regmatch_state *cur = st;
5434 regmatch_state *curyes = yes_state;
5436 regmatch_slab *slab = PL_regmatch_slab;
5437 for (;curd > -1;cur--,curd--) {
5438 if (cur < SLAB_FIRST(slab)) {
5440 cur = SLAB_LAST(slab);
5442 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5443 REPORT_CODE_OFF + 2 + depth * 2,"",
5444 curd, PL_reg_name[cur->resume_state],
5445 (curyes == cur) ? "yes" : ""
5448 curyes = cur->u.yes.prev_yes_state;
5451 DEBUG_STATE_pp("push")
5454 st->locinput = locinput;
5456 if (newst > SLAB_LAST(PL_regmatch_slab))
5457 newst = S_push_slab(aTHX);
5458 PL_regmatch_state = newst;
5460 locinput = PL_reginput;
5461 nextchr = UCHARAT(locinput);
5469 * We get here only if there's trouble -- normally "case END" is
5470 * the terminating point.
5472 Perl_croak(aTHX_ "corrupted regexp pointers");
5478 /* we have successfully completed a subexpression, but we must now
5479 * pop to the state marked by yes_state and continue from there */
5480 assert(st != yes_state);
5482 while (st != yes_state) {
5484 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5485 PL_regmatch_slab = PL_regmatch_slab->prev;
5486 st = SLAB_LAST(PL_regmatch_slab);
5490 DEBUG_STATE_pp("pop (no final)");
5492 DEBUG_STATE_pp("pop (yes)");
5498 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5499 || yes_state > SLAB_LAST(PL_regmatch_slab))
5501 /* not in this slab, pop slab */
5502 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5503 PL_regmatch_slab = PL_regmatch_slab->prev;
5504 st = SLAB_LAST(PL_regmatch_slab);
5506 depth -= (st - yes_state);
5509 yes_state = st->u.yes.prev_yes_state;
5510 PL_regmatch_state = st;
5513 locinput= st->locinput;
5514 nextchr = UCHARAT(locinput);
5516 state_num = st->resume_state + no_final;
5517 goto reenter_switch;
5520 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5521 PL_colors[4], PL_colors[5]));
5523 if (PL_reg_eval_set) {
5524 /* each successfully executed (?{...}) block does the equivalent of
5525 * local $^R = do {...}
5526 * When popping the save stack, all these locals would be undone;
5527 * bypass this by setting the outermost saved $^R to the latest
5529 if (oreplsv != GvSV(PL_replgv))
5530 sv_setsv(oreplsv, GvSV(PL_replgv));
5537 PerlIO_printf(Perl_debug_log,
5538 "%*s %sfailed...%s\n",
5539 REPORT_CODE_OFF+depth*2, "",
5540 PL_colors[4], PL_colors[5])
5552 /* there's a previous state to backtrack to */
5554 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5555 PL_regmatch_slab = PL_regmatch_slab->prev;
5556 st = SLAB_LAST(PL_regmatch_slab);
5558 PL_regmatch_state = st;
5559 locinput= st->locinput;
5560 nextchr = UCHARAT(locinput);
5562 DEBUG_STATE_pp("pop");
5564 if (yes_state == st)
5565 yes_state = st->u.yes.prev_yes_state;
5567 state_num = st->resume_state + 1; /* failure = success + 1 */
5568 goto reenter_switch;
5573 if (rex->intflags & PREGf_VERBARG_SEEN) {
5574 SV *sv_err = get_sv("REGERROR", 1);
5575 SV *sv_mrk = get_sv("REGMARK", 1);
5577 sv_commit = &PL_sv_no;
5579 sv_yes_mark = &PL_sv_yes;
5582 sv_commit = &PL_sv_yes;
5583 sv_yes_mark = &PL_sv_no;
5585 sv_setsv(sv_err, sv_commit);
5586 sv_setsv(sv_mrk, sv_yes_mark);
5589 /* clean up; in particular, free all slabs above current one */
5590 LEAVE_SCOPE(oldsave);
5596 - regrepeat - repeatedly match something simple, report how many
5599 * [This routine now assumes that it will only match on things of length 1.
5600 * That was true before, but now we assume scan - reginput is the count,
5601 * rather than incrementing count on every character. [Er, except utf8.]]
5604 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5607 register char *scan;
5609 register char *loceol = PL_regeol;
5610 register I32 hardcount = 0;
5611 register bool utf8_target = PL_reg_match_utf8;
5613 PERL_UNUSED_ARG(depth);
5616 PERL_ARGS_ASSERT_REGREPEAT;
5619 if (max == REG_INFTY)
5621 else if (max < loceol - scan)
5622 loceol = scan + max;
5627 while (scan < loceol && hardcount < max && *scan != '\n') {
5628 scan += UTF8SKIP(scan);
5632 while (scan < loceol && *scan != '\n')
5639 while (scan < loceol && hardcount < max) {
5640 scan += UTF8SKIP(scan);
5650 case EXACT: /* length of string is 1 */
5652 while (scan < loceol && UCHARAT(scan) == c)
5655 case EXACTF: /* length of string is 1 */
5657 while (scan < loceol &&
5658 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5661 case EXACTFL: /* length of string is 1 */
5662 PL_reg_flags |= RF_tainted;
5664 while (scan < loceol &&
5665 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5671 while (hardcount < max && scan < loceol &&
5672 reginclass(prog, p, (U8*)scan, 0, utf8_target)) {
5673 scan += UTF8SKIP(scan);
5677 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5684 LOAD_UTF8_CHARCLASS_ALNUM();
5685 while (hardcount < max && scan < loceol &&
5686 swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target)) {
5687 scan += UTF8SKIP(scan);
5691 while (scan < loceol && isALNUM(*scan))
5696 PL_reg_flags |= RF_tainted;
5699 while (hardcount < max && scan < loceol &&
5700 isALNUM_LC_utf8((U8*)scan)) {
5701 scan += UTF8SKIP(scan);
5705 while (scan < loceol && isALNUM_LC(*scan))
5712 LOAD_UTF8_CHARCLASS_ALNUM();
5713 while (hardcount < max && scan < loceol &&
5714 !swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target)) {
5715 scan += UTF8SKIP(scan);
5719 while (scan < loceol && !isALNUM(*scan))
5724 PL_reg_flags |= RF_tainted;
5727 while (hardcount < max && scan < loceol &&
5728 !isALNUM_LC_utf8((U8*)scan)) {
5729 scan += UTF8SKIP(scan);
5733 while (scan < loceol && !isALNUM_LC(*scan))
5740 LOAD_UTF8_CHARCLASS_SPACE();
5741 while (hardcount < max && scan < loceol &&
5743 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target))) {
5744 scan += UTF8SKIP(scan);
5748 while (scan < loceol && isSPACE(*scan))
5753 PL_reg_flags |= RF_tainted;
5756 while (hardcount < max && scan < loceol &&
5757 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5758 scan += UTF8SKIP(scan);
5762 while (scan < loceol && isSPACE_LC(*scan))
5769 LOAD_UTF8_CHARCLASS_SPACE();
5770 while (hardcount < max && scan < loceol &&
5772 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target))) {
5773 scan += UTF8SKIP(scan);
5777 while (scan < loceol && !isSPACE(*scan))
5782 PL_reg_flags |= RF_tainted;
5785 while (hardcount < max && scan < loceol &&
5786 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5787 scan += UTF8SKIP(scan);
5791 while (scan < loceol && !isSPACE_LC(*scan))
5798 LOAD_UTF8_CHARCLASS_DIGIT();
5799 while (hardcount < max && scan < loceol &&
5800 swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
5801 scan += UTF8SKIP(scan);
5805 while (scan < loceol && isDIGIT(*scan))
5812 LOAD_UTF8_CHARCLASS_DIGIT();
5813 while (hardcount < max && scan < loceol &&
5814 !swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
5815 scan += UTF8SKIP(scan);
5819 while (scan < loceol && !isDIGIT(*scan))
5825 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5831 LNBREAK can match two latin chars, which is ok,
5832 because we have a null terminated string, but we
5833 have to use hardcount in this situation
5835 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5844 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5849 while (scan < loceol && is_HORIZWS_latin1(scan))
5856 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5857 scan += UTF8SKIP(scan);
5861 while (scan < loceol && !is_HORIZWS_latin1(scan))
5869 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5874 while (scan < loceol && is_VERTWS_latin1(scan))
5882 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5883 scan += UTF8SKIP(scan);
5887 while (scan < loceol && !is_VERTWS_latin1(scan))
5893 default: /* Called on something of 0 width. */
5894 break; /* So match right here or not at all. */
5900 c = scan - PL_reginput;
5904 GET_RE_DEBUG_FLAGS_DECL;
5906 SV * const prop = sv_newmortal();
5907 regprop(prog, prop, p);
5908 PerlIO_printf(Perl_debug_log,
5909 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5910 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5918 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5920 - regclass_swash - prepare the utf8 swash
5924 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5930 RXi_GET_DECL(prog,progi);
5931 const struct reg_data * const data = prog ? progi->data : NULL;
5933 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5935 if (data && data->count) {
5936 const U32 n = ARG(node);
5938 if (data->what[n] == 's') {
5939 SV * const rv = MUTABLE_SV(data->data[n]);
5940 AV * const av = MUTABLE_AV(SvRV(rv));
5941 SV **const ary = AvARRAY(av);
5944 /* See the end of regcomp.c:S_regclass() for
5945 * documentation of these array elements. */
5948 a = SvROK(ary[1]) ? &ary[1] : NULL;
5949 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5953 else if (si && doinit) {
5954 sw = swash_init("utf8", "", si, 1, 0);
5955 (void)av_store(av, 1, sw);
5972 - reginclass - determine if a character falls into a character class
5974 The n is the ANYOF regnode, the p is the target string, lenp
5975 is pointer to the maximum length of how far to go in the p
5976 (if the lenp is zero, UTF8SKIP(p) is used),
5977 utf8_target tells whether the target string is in UTF-8.
5982 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool utf8_target)
5985 const char flags = ANYOF_FLAGS(n);
5991 PERL_ARGS_ASSERT_REGINCLASS;
5993 if (utf8_target && !UTF8_IS_INVARIANT(c)) {
5994 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5995 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
5996 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
5997 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
5998 * UTF8_ALLOW_FFFF */
5999 if (len == (STRLEN)-1)
6000 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
6003 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
6004 if (utf8_target || (flags & ANYOF_UNICODE)) {
6007 if (utf8_target && !ANYOF_RUNTIME(n)) {
6008 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
6011 if (!match && utf8_target && (flags & ANYOF_UNICODE_ALL) && c >= 256)
6015 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
6023 utf8_p = bytes_to_utf8(p, &len);
6025 if (swash_fetch(sw, utf8_p, 1))
6027 else if (flags & ANYOF_FOLD) {
6028 if (!match && lenp && av) {
6030 for (i = 0; i <= av_len(av); i++) {
6031 SV* const sv = *av_fetch(av, i, FALSE);
6033 const char * const s = SvPV_const(sv, len);
6034 if (len <= plen && memEQ(s, (char*)utf8_p, len)) {
6042 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
6045 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
6046 if (swash_fetch(sw, tmpbuf, 1))
6051 /* If we allocated a string above, free it */
6052 if (! utf8_target) Safefree(utf8_p);
6055 if (match && lenp && *lenp == 0)
6056 *lenp = UNISKIP(NATIVE_TO_UNI(c));
6058 if (!match && c < 256) {
6059 if (ANYOF_BITMAP_TEST(n, c))
6061 else if (flags & ANYOF_FOLD) {
6064 if (flags & ANYOF_LOCALE) {
6065 PL_reg_flags |= RF_tainted;
6066 f = PL_fold_locale[c];
6070 if (f != c && ANYOF_BITMAP_TEST(n, f))
6074 if (!match && (flags & ANYOF_CLASS)) {
6075 PL_reg_flags |= RF_tainted;
6077 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6078 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6079 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6080 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6081 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6082 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6083 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6084 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6085 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6086 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6087 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6088 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6089 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6090 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6091 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6092 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6093 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6094 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6095 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6096 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6097 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6098 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6099 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6100 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6101 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6102 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6103 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6104 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6105 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6106 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6107 ) /* How's that for a conditional? */
6114 return (flags & ANYOF_INVERT) ? !match : match;
6118 S_reghop3(U8 *s, I32 off, const U8* lim)
6122 PERL_ARGS_ASSERT_REGHOP3;
6125 while (off-- && s < lim) {
6126 /* XXX could check well-formedness here */
6131 while (off++ && s > lim) {
6133 if (UTF8_IS_CONTINUED(*s)) {
6134 while (s > lim && UTF8_IS_CONTINUATION(*s))
6137 /* XXX could check well-formedness here */
6144 /* there are a bunch of places where we use two reghop3's that should
6145 be replaced with this routine. but since thats not done yet
6146 we ifdef it out - dmq
6149 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6153 PERL_ARGS_ASSERT_REGHOP4;
6156 while (off-- && s < rlim) {
6157 /* XXX could check well-formedness here */
6162 while (off++ && s > llim) {
6164 if (UTF8_IS_CONTINUED(*s)) {
6165 while (s > llim && UTF8_IS_CONTINUATION(*s))
6168 /* XXX could check well-formedness here */
6176 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
6180 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6183 while (off-- && s < lim) {
6184 /* XXX could check well-formedness here */
6191 while (off++ && s > lim) {
6193 if (UTF8_IS_CONTINUED(*s)) {
6194 while (s > lim && UTF8_IS_CONTINUATION(*s))
6197 /* XXX could check well-formedness here */
6206 restore_pos(pTHX_ void *arg)
6209 regexp * const rex = (regexp *)arg;
6210 if (PL_reg_eval_set) {
6211 if (PL_reg_oldsaved) {
6212 rex->subbeg = PL_reg_oldsaved;
6213 rex->sublen = PL_reg_oldsavedlen;
6214 #ifdef PERL_OLD_COPY_ON_WRITE
6215 rex->saved_copy = PL_nrs;
6217 RXp_MATCH_COPIED_on(rex);
6219 PL_reg_magic->mg_len = PL_reg_oldpos;
6220 PL_reg_eval_set = 0;
6221 PL_curpm = PL_reg_oldcurpm;
6226 S_to_utf8_substr(pTHX_ register regexp *prog)
6230 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6233 if (prog->substrs->data[i].substr
6234 && !prog->substrs->data[i].utf8_substr) {
6235 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6236 prog->substrs->data[i].utf8_substr = sv;
6237 sv_utf8_upgrade(sv);
6238 if (SvVALID(prog->substrs->data[i].substr)) {
6239 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
6240 if (flags & FBMcf_TAIL) {
6241 /* Trim the trailing \n that fbm_compile added last
6243 SvCUR_set(sv, SvCUR(sv) - 1);
6244 /* Whilst this makes the SV technically "invalid" (as its
6245 buffer is no longer followed by "\0") when fbm_compile()
6246 adds the "\n" back, a "\0" is restored. */
6248 fbm_compile(sv, flags);
6250 if (prog->substrs->data[i].substr == prog->check_substr)
6251 prog->check_utf8 = sv;
6257 S_to_byte_substr(pTHX_ register regexp *prog)
6262 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6265 if (prog->substrs->data[i].utf8_substr
6266 && !prog->substrs->data[i].substr) {
6267 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6268 if (sv_utf8_downgrade(sv, TRUE)) {
6269 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6271 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6272 if (flags & FBMcf_TAIL) {
6273 /* Trim the trailing \n that fbm_compile added last
6275 SvCUR_set(sv, SvCUR(sv) - 1);
6277 fbm_compile(sv, flags);
6283 prog->substrs->data[i].substr = sv;
6284 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6285 prog->check_substr = sv;
6292 * c-indentation-style: bsd
6294 * indent-tabs-mode: t
6297 * ex: set ts=8 sts=4 sw=4 noet: