5 * One Ring to rule them all, One Ring to find them
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
12 /* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
14 * a regular expression.
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
26 /* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
31 /* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
36 #ifdef PERL_EXT_RE_BUILD
41 * pregcomp and pregexec -- regsub and regerror are not used in perl
43 * Copyright (c) 1986 by University of Toronto.
44 * Written by Henry Spencer. Not derived from licensed software.
46 * Permission is granted to anyone to use this software for any
47 * purpose on any computer system, and to redistribute it freely,
48 * subject to the following restrictions:
50 * 1. The author is not responsible for the consequences of use of
51 * this software, no matter how awful, even if they arise
54 * 2. The origin of this software must not be misrepresented, either
55 * by explicit claim or by omission.
57 * 3. Altered versions must be plainly marked as such, and must not
58 * be misrepresented as being the original software.
60 **** Alterations to Henry's code are...
62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
66 **** You may distribute under the terms of either the GNU General Public
67 **** License or the Artistic License, as specified in the README file.
69 * Beware that some of this code is subtly aware of the way operator
70 * precedence is structured in regular expressions. Serious changes in
71 * regular-expression syntax might require a total rethink.
74 #define PERL_IN_REGEXEC_C
78 #ifdef PERL_IN_XSUB_RE
84 #define RF_tainted 1 /* tainted information used? */
85 #define RF_warned 2 /* warned about big count? */
87 #define RF_utf8 8 /* Pattern contains multibyte chars? */
89 #define UTF ((PL_reg_flags & RF_utf8) != 0)
91 #define RS_init 1 /* eval environment created */
92 #define RS_set 2 /* replsv value is set */
98 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
104 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
105 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
107 #define HOPc(pos,off) \
108 (char *)(PL_reg_match_utf8 \
109 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
111 #define HOPBACKc(pos, off) \
112 (char*)(PL_reg_match_utf8\
113 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
114 : (pos - off >= PL_bostr) \
118 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
119 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
121 /* these are unrolled below in the CCC_TRY_XXX defined */
122 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
123 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
125 /* Doesn't do an assert to verify that is correct */
126 #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
127 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END
129 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
130 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
131 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
133 #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
134 LOAD_UTF8_CHARCLASS(X_begin, " "); \
135 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
136 /* These are utf8 constants, and not utf-ebcdic constants, so the \
137 * assert should likely and hopefully fail on an EBCDIC machine */ \
138 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
140 /* No asserts are done for these, in case called on an early \
141 * Unicode version in which they map to nothing */ \
142 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
143 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
144 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
145 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
146 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
147 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
148 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
151 We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test
152 so that it is possible to override the option here without having to
153 rebuild the entire core. as we are required to do if we change regcomp.h
154 which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined.
156 #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS
157 #define BROKEN_UNICODE_CHARCLASS_MAPPINGS
160 #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS
161 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM()
162 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE()
163 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT()
164 #define RE_utf8_perl_word PL_utf8_alnum
165 #define RE_utf8_perl_space PL_utf8_space
166 #define RE_utf8_posix_digit PL_utf8_digit
167 #define perl_word alnum
168 #define perl_space space
169 #define posix_digit digit
171 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a")
172 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ")
173 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0")
174 #define RE_utf8_perl_word PL_utf8_perl_word
175 #define RE_utf8_perl_space PL_utf8_perl_space
176 #define RE_utf8_posix_digit PL_utf8_posix_digit
180 #define CCC_TRY_AFF(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
182 PL_reg_flags |= RF_tainted; \
187 if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \
188 if (!CAT2(PL_utf8_,CLASS)) { \
192 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
196 if (!(OP(scan) == NAME \
197 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \
198 : LCFUNC_utf8((U8*)locinput))) \
202 locinput += PL_utf8skip[nextchr]; \
203 nextchr = UCHARAT(locinput); \
206 if (!(OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
208 nextchr = UCHARAT(++locinput); \
211 #define CCC_TRY_NEG(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
213 PL_reg_flags |= RF_tainted; \
216 if (!nextchr && locinput >= PL_regeol) \
218 if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \
219 if (!CAT2(PL_utf8_,CLASS)) { \
223 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
227 if ((OP(scan) == NAME \
228 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \
229 : LCFUNC_utf8((U8*)locinput))) \
233 locinput += PL_utf8skip[nextchr]; \
234 nextchr = UCHARAT(locinput); \
237 if ((OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
239 nextchr = UCHARAT(++locinput); \
246 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
248 /* for use after a quantifier and before an EXACT-like node -- japhy */
249 /* it would be nice to rework regcomp.sym to generate this stuff. sigh */
250 #define JUMPABLE(rn) ( \
252 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
254 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
255 OP(rn) == PLUS || OP(rn) == MINMOD || \
256 OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \
257 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
259 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
261 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
264 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
265 we don't need this definition. */
266 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
267 #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
268 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
271 /* ... so we use this as its faster. */
272 #define IS_TEXT(rn) ( OP(rn)==EXACT )
273 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
274 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
279 Search for mandatory following text node; for lookahead, the text must
280 follow but for lookbehind (rn->flags != 0) we skip to the next step.
282 #define FIND_NEXT_IMPT(rn) STMT_START { \
283 while (JUMPABLE(rn)) { \
284 const OPCODE type = OP(rn); \
285 if (type == SUSPEND || PL_regkind[type] == CURLY) \
286 rn = NEXTOPER(NEXTOPER(rn)); \
287 else if (type == PLUS) \
289 else if (type == IFMATCH) \
290 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
291 else rn += NEXT_OFF(rn); \
296 static void restore_pos(pTHX_ void *arg);
299 S_regcppush(pTHX_ I32 parenfloor)
302 const int retval = PL_savestack_ix;
303 #define REGCP_PAREN_ELEMS 4
304 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
306 GET_RE_DEBUG_FLAGS_DECL;
308 if (paren_elems_to_push < 0)
309 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
311 #define REGCP_OTHER_ELEMS 7
312 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
314 for (p = PL_regsize; p > parenfloor; p--) {
315 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
316 SSPUSHINT(PL_regoffs[p].end);
317 SSPUSHINT(PL_regoffs[p].start);
318 SSPUSHPTR(PL_reg_start_tmp[p]);
320 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
321 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
322 (UV)p, (IV)PL_regoffs[p].start,
323 (IV)(PL_reg_start_tmp[p] - PL_bostr),
324 (IV)PL_regoffs[p].end
327 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
328 SSPUSHPTR(PL_regoffs);
329 SSPUSHINT(PL_regsize);
330 SSPUSHINT(*PL_reglastparen);
331 SSPUSHINT(*PL_reglastcloseparen);
332 SSPUSHPTR(PL_reginput);
333 #define REGCP_FRAME_ELEMS 2
334 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
335 * are needed for the regexp context stack bookkeeping. */
336 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
337 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
342 /* These are needed since we do not localize EVAL nodes: */
343 #define REGCP_SET(cp) \
345 PerlIO_printf(Perl_debug_log, \
346 " Setting an EVAL scope, savestack=%"IVdf"\n", \
347 (IV)PL_savestack_ix)); \
350 #define REGCP_UNWIND(cp) \
352 if (cp != PL_savestack_ix) \
353 PerlIO_printf(Perl_debug_log, \
354 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
355 (IV)(cp), (IV)PL_savestack_ix)); \
359 S_regcppop(pTHX_ const regexp *rex)
364 GET_RE_DEBUG_FLAGS_DECL;
366 PERL_ARGS_ASSERT_REGCPPOP;
368 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
370 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
371 i = SSPOPINT; /* Parentheses elements to pop. */
372 input = (char *) SSPOPPTR;
373 *PL_reglastcloseparen = SSPOPINT;
374 *PL_reglastparen = SSPOPINT;
375 PL_regsize = SSPOPINT;
376 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
379 /* Now restore the parentheses context. */
380 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
381 i > 0; i -= REGCP_PAREN_ELEMS) {
383 U32 paren = (U32)SSPOPINT;
384 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
385 PL_regoffs[paren].start = SSPOPINT;
387 if (paren <= *PL_reglastparen)
388 PL_regoffs[paren].end = tmps;
390 PerlIO_printf(Perl_debug_log,
391 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
392 (UV)paren, (IV)PL_regoffs[paren].start,
393 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
394 (IV)PL_regoffs[paren].end,
395 (paren > *PL_reglastparen ? "(no)" : ""));
399 if (*PL_reglastparen + 1 <= rex->nparens) {
400 PerlIO_printf(Perl_debug_log,
401 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
402 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
406 /* It would seem that the similar code in regtry()
407 * already takes care of this, and in fact it is in
408 * a better location to since this code can #if 0-ed out
409 * but the code in regtry() is needed or otherwise tests
410 * requiring null fields (pat.t#187 and split.t#{13,14}
411 * (as of patchlevel 7877) will fail. Then again,
412 * this code seems to be necessary or otherwise
413 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
414 * --jhi updated by dapm */
415 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
417 PL_regoffs[i].start = -1;
418 PL_regoffs[i].end = -1;
424 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
427 * pregexec and friends
430 #ifndef PERL_IN_XSUB_RE
432 - pregexec - match a regexp against a string
435 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
436 char *strbeg, I32 minend, SV *screamer, U32 nosave)
437 /* strend: pointer to null at end of string */
438 /* strbeg: real beginning of string */
439 /* minend: end of match must be >=minend after stringarg. */
440 /* nosave: For optimizations. */
442 PERL_ARGS_ASSERT_PREGEXEC;
445 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
446 nosave ? 0 : REXEC_COPY_STR);
451 * Need to implement the following flags for reg_anch:
453 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
455 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
456 * INTUIT_AUTORITATIVE_ML
457 * INTUIT_ONCE_NOML - Intuit can match in one location only.
460 * Another flag for this function: SECOND_TIME (so that float substrs
461 * with giant delta may be not rechecked).
464 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
466 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
467 Otherwise, only SvCUR(sv) is used to get strbeg. */
469 /* XXXX We assume that strpos is strbeg unless sv. */
471 /* XXXX Some places assume that there is a fixed substring.
472 An update may be needed if optimizer marks as "INTUITable"
473 RExen without fixed substrings. Similarly, it is assumed that
474 lengths of all the strings are no more than minlen, thus they
475 cannot come from lookahead.
476 (Or minlen should take into account lookahead.)
477 NOTE: Some of this comment is not correct. minlen does now take account
478 of lookahead/behind. Further research is required. -- demerphq
482 /* A failure to find a constant substring means that there is no need to make
483 an expensive call to REx engine, thus we celebrate a failure. Similarly,
484 finding a substring too deep into the string means that less calls to
485 regtry() should be needed.
487 REx compiler's optimizer found 4 possible hints:
488 a) Anchored substring;
490 c) Whether we are anchored (beginning-of-line or \G);
491 d) First node (of those at offset 0) which may distingush positions;
492 We use a)b)d) and multiline-part of c), and try to find a position in the
493 string which does not contradict any of them.
496 /* Most of decisions we do here should have been done at compile time.
497 The nodes of the REx which we used for the search should have been
498 deleted from the finite automaton. */
501 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
502 char *strend, const U32 flags, re_scream_pos_data *data)
505 struct regexp *const prog = (struct regexp *)SvANY(rx);
506 register I32 start_shift = 0;
507 /* Should be nonnegative! */
508 register I32 end_shift = 0;
513 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
515 register char *other_last = NULL; /* other substr checked before this */
516 char *check_at = NULL; /* check substr found at this pos */
517 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
518 RXi_GET_DECL(prog,progi);
520 const char * const i_strpos = strpos;
522 GET_RE_DEBUG_FLAGS_DECL;
524 PERL_ARGS_ASSERT_RE_INTUIT_START;
526 RX_MATCH_UTF8_set(rx,do_utf8);
529 PL_reg_flags |= RF_utf8;
532 debug_start_match(rx, do_utf8, strpos, strend,
533 sv ? "Guessing start of match in sv for"
534 : "Guessing start of match in string for");
537 /* CHR_DIST() would be more correct here but it makes things slow. */
538 if (prog->minlen > strend - strpos) {
539 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
540 "String too short... [re_intuit_start]\n"));
544 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
547 if (!prog->check_utf8 && prog->check_substr)
548 to_utf8_substr(prog);
549 check = prog->check_utf8;
551 if (!prog->check_substr && prog->check_utf8)
552 to_byte_substr(prog);
553 check = prog->check_substr;
555 if (check == &PL_sv_undef) {
556 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
557 "Non-utf8 string cannot match utf8 check string\n"));
560 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
561 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
562 || ( (prog->extflags & RXf_ANCH_BOL)
563 && !multiline ) ); /* Check after \n? */
566 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
567 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
568 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
570 && (strpos != strbeg)) {
571 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
574 if (prog->check_offset_min == prog->check_offset_max &&
575 !(prog->extflags & RXf_CANY_SEEN)) {
576 /* Substring at constant offset from beg-of-str... */
579 s = HOP3c(strpos, prog->check_offset_min, strend);
582 slen = SvCUR(check); /* >= 1 */
584 if ( strend - s > slen || strend - s < slen - 1
585 || (strend - s == slen && strend[-1] != '\n')) {
586 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
589 /* Now should match s[0..slen-2] */
591 if (slen && (*SvPVX_const(check) != *s
593 && memNE(SvPVX_const(check), s, slen)))) {
595 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
599 else if (*SvPVX_const(check) != *s
600 || ((slen = SvCUR(check)) > 1
601 && memNE(SvPVX_const(check), s, slen)))
604 goto success_at_start;
607 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
609 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
610 end_shift = prog->check_end_shift;
613 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
614 - (SvTAIL(check) != 0);
615 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
617 if (end_shift < eshift)
621 else { /* Can match at random position */
624 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
625 end_shift = prog->check_end_shift;
627 /* end shift should be non negative here */
630 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
632 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
633 (IV)end_shift, RX_PRECOMP(prog));
637 /* Find a possible match in the region s..strend by looking for
638 the "check" substring in the region corrected by start/end_shift. */
641 I32 srch_start_shift = start_shift;
642 I32 srch_end_shift = end_shift;
643 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
644 srch_end_shift -= ((strbeg - s) - srch_start_shift);
645 srch_start_shift = strbeg - s;
647 DEBUG_OPTIMISE_MORE_r({
648 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
649 (IV)prog->check_offset_min,
650 (IV)srch_start_shift,
652 (IV)prog->check_end_shift);
655 if (flags & REXEC_SCREAM) {
656 I32 p = -1; /* Internal iterator of scream. */
657 I32 * const pp = data ? data->scream_pos : &p;
659 if (PL_screamfirst[BmRARE(check)] >= 0
660 || ( BmRARE(check) == '\n'
661 && (BmPREVIOUS(check) == SvCUR(check) - 1)
663 s = screaminstr(sv, check,
664 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
667 /* we may be pointing at the wrong string */
668 if (s && RXp_MATCH_COPIED(prog))
669 s = strbeg + (s - SvPVX_const(sv));
671 *data->scream_olds = s;
676 if (prog->extflags & RXf_CANY_SEEN) {
677 start_point= (U8*)(s + srch_start_shift);
678 end_point= (U8*)(strend - srch_end_shift);
680 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
681 end_point= HOP3(strend, -srch_end_shift, strbeg);
683 DEBUG_OPTIMISE_MORE_r({
684 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
685 (int)(end_point - start_point),
686 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
690 s = fbm_instr( start_point, end_point,
691 check, multiline ? FBMrf_MULTILINE : 0);
694 /* Update the count-of-usability, remove useless subpatterns,
698 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
699 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
700 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
701 (s ? "Found" : "Did not find"),
702 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
703 ? "anchored" : "floating"),
706 (s ? " at offset " : "...\n") );
711 /* Finish the diagnostic message */
712 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
714 /* XXX dmq: first branch is for positive lookbehind...
715 Our check string is offset from the beginning of the pattern.
716 So we need to do any stclass tests offset forward from that
725 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
726 Start with the other substr.
727 XXXX no SCREAM optimization yet - and a very coarse implementation
728 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
729 *always* match. Probably should be marked during compile...
730 Probably it is right to do no SCREAM here...
733 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
734 : (prog->float_substr && prog->anchored_substr))
736 /* Take into account the "other" substring. */
737 /* XXXX May be hopelessly wrong for UTF... */
740 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
743 char * const last = HOP3c(s, -start_shift, strbeg);
745 char * const saved_s = s;
748 t = s - prog->check_offset_max;
749 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
751 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
756 t = HOP3c(t, prog->anchored_offset, strend);
757 if (t < other_last) /* These positions already checked */
759 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
762 /* XXXX It is not documented what units *_offsets are in.
763 We assume bytes, but this is clearly wrong.
764 Meaning this code needs to be carefully reviewed for errors.
768 /* On end-of-str: see comment below. */
769 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
770 if (must == &PL_sv_undef) {
772 DEBUG_r(must = prog->anchored_utf8); /* for debug */
777 HOP3(HOP3(last1, prog->anchored_offset, strend)
778 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
780 multiline ? FBMrf_MULTILINE : 0
783 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
784 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
785 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
786 (s ? "Found" : "Contradicts"),
787 quoted, RE_SV_TAIL(must));
792 if (last1 >= last2) {
793 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
794 ", giving up...\n"));
797 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
798 ", trying floating at offset %ld...\n",
799 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
800 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
801 s = HOP3c(last, 1, strend);
805 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
806 (long)(s - i_strpos)));
807 t = HOP3c(s, -prog->anchored_offset, strbeg);
808 other_last = HOP3c(s, 1, strend);
816 else { /* Take into account the floating substring. */
818 char * const saved_s = s;
821 t = HOP3c(s, -start_shift, strbeg);
823 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
824 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
825 last = HOP3c(t, prog->float_max_offset, strend);
826 s = HOP3c(t, prog->float_min_offset, strend);
829 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
830 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
831 /* fbm_instr() takes into account exact value of end-of-str
832 if the check is SvTAIL(ed). Since false positives are OK,
833 and end-of-str is not later than strend we are OK. */
834 if (must == &PL_sv_undef) {
836 DEBUG_r(must = prog->float_utf8); /* for debug message */
839 s = fbm_instr((unsigned char*)s,
840 (unsigned char*)last + SvCUR(must)
842 must, multiline ? FBMrf_MULTILINE : 0);
844 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
845 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
846 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
847 (s ? "Found" : "Contradicts"),
848 quoted, RE_SV_TAIL(must));
852 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
853 ", giving up...\n"));
856 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
857 ", trying anchored starting at offset %ld...\n",
858 (long)(saved_s + 1 - i_strpos)));
860 s = HOP3c(t, 1, strend);
864 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
865 (long)(s - i_strpos)));
866 other_last = s; /* Fix this later. --Hugo */
876 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
878 DEBUG_OPTIMISE_MORE_r(
879 PerlIO_printf(Perl_debug_log,
880 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
881 (IV)prog->check_offset_min,
882 (IV)prog->check_offset_max,
890 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
892 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
895 /* Fixed substring is found far enough so that the match
896 cannot start at strpos. */
898 if (ml_anch && t[-1] != '\n') {
899 /* Eventually fbm_*() should handle this, but often
900 anchored_offset is not 0, so this check will not be wasted. */
901 /* XXXX In the code below we prefer to look for "^" even in
902 presence of anchored substrings. And we search even
903 beyond the found float position. These pessimizations
904 are historical artefacts only. */
906 while (t < strend - prog->minlen) {
908 if (t < check_at - prog->check_offset_min) {
909 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
910 /* Since we moved from the found position,
911 we definitely contradict the found anchored
912 substr. Due to the above check we do not
913 contradict "check" substr.
914 Thus we can arrive here only if check substr
915 is float. Redo checking for "other"=="fixed".
918 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
919 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
920 goto do_other_anchored;
922 /* We don't contradict the found floating substring. */
923 /* XXXX Why not check for STCLASS? */
925 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
926 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
929 /* Position contradicts check-string */
930 /* XXXX probably better to look for check-string
931 than for "\n", so one should lower the limit for t? */
932 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
933 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
934 other_last = strpos = s = t + 1;
939 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
940 PL_colors[0], PL_colors[1]));
944 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
945 PL_colors[0], PL_colors[1]));
949 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
952 /* The found string does not prohibit matching at strpos,
953 - no optimization of calling REx engine can be performed,
954 unless it was an MBOL and we are not after MBOL,
955 or a future STCLASS check will fail this. */
957 /* Even in this situation we may use MBOL flag if strpos is offset
958 wrt the start of the string. */
959 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
960 && (strpos != strbeg) && strpos[-1] != '\n'
961 /* May be due to an implicit anchor of m{.*foo} */
962 && !(prog->intflags & PREGf_IMPLICIT))
967 DEBUG_EXECUTE_r( if (ml_anch)
968 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
969 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
972 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
974 prog->check_utf8 /* Could be deleted already */
975 && --BmUSEFUL(prog->check_utf8) < 0
976 && (prog->check_utf8 == prog->float_utf8)
978 prog->check_substr /* Could be deleted already */
979 && --BmUSEFUL(prog->check_substr) < 0
980 && (prog->check_substr == prog->float_substr)
983 /* If flags & SOMETHING - do not do it many times on the same match */
984 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
985 /* XXX Does the destruction order has to change with do_utf8? */
986 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
987 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
988 prog->check_substr = prog->check_utf8 = NULL; /* disable */
989 prog->float_substr = prog->float_utf8 = NULL; /* clear */
990 check = NULL; /* abort */
992 /* XXXX This is a remnant of the old implementation. It
993 looks wasteful, since now INTUIT can use many
995 prog->extflags &= ~RXf_USE_INTUIT;
1001 /* Last resort... */
1002 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1003 /* trie stclasses are too expensive to use here, we are better off to
1004 leave it to regmatch itself */
1005 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1006 /* minlen == 0 is possible if regstclass is \b or \B,
1007 and the fixed substr is ''$.
1008 Since minlen is already taken into account, s+1 is before strend;
1009 accidentally, minlen >= 1 guaranties no false positives at s + 1
1010 even for \b or \B. But (minlen? 1 : 0) below assumes that
1011 regstclass does not come from lookahead... */
1012 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1013 This leaves EXACTF only, which is dealt with in find_byclass(). */
1014 const U8* const str = (U8*)STRING(progi->regstclass);
1015 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1016 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1019 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1020 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1021 else if (prog->float_substr || prog->float_utf8)
1022 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1026 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1027 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1030 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1033 const char *what = NULL;
1035 if (endpos == strend) {
1036 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1037 "Could not match STCLASS...\n") );
1040 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1041 "This position contradicts STCLASS...\n") );
1042 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1044 /* Contradict one of substrings */
1045 if (prog->anchored_substr || prog->anchored_utf8) {
1046 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1047 DEBUG_EXECUTE_r( what = "anchored" );
1049 s = HOP3c(t, 1, strend);
1050 if (s + start_shift + end_shift > strend) {
1051 /* XXXX Should be taken into account earlier? */
1052 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1053 "Could not match STCLASS...\n") );
1058 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1059 "Looking for %s substr starting at offset %ld...\n",
1060 what, (long)(s + start_shift - i_strpos)) );
1063 /* Have both, check_string is floating */
1064 if (t + start_shift >= check_at) /* Contradicts floating=check */
1065 goto retry_floating_check;
1066 /* Recheck anchored substring, but not floating... */
1070 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1071 "Looking for anchored substr starting at offset %ld...\n",
1072 (long)(other_last - i_strpos)) );
1073 goto do_other_anchored;
1075 /* Another way we could have checked stclass at the
1076 current position only: */
1081 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1082 "Looking for /%s^%s/m starting at offset %ld...\n",
1083 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1086 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1088 /* Check is floating subtring. */
1089 retry_floating_check:
1090 t = check_at - start_shift;
1091 DEBUG_EXECUTE_r( what = "floating" );
1092 goto hop_and_restart;
1095 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1096 "By STCLASS: moving %ld --> %ld\n",
1097 (long)(t - i_strpos), (long)(s - i_strpos))
1101 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1102 "Does not contradict STCLASS...\n");
1107 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1108 PL_colors[4], (check ? "Guessed" : "Giving up"),
1109 PL_colors[5], (long)(s - i_strpos)) );
1112 fail_finish: /* Substring not found */
1113 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1114 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1116 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1117 PL_colors[4], PL_colors[5]));
1121 #define DECL_TRIE_TYPE(scan) \
1122 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1123 trie_type = (scan->flags != EXACT) \
1124 ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \
1125 : (do_utf8 ? trie_utf8 : trie_plain)
1127 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1128 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1129 switch (trie_type) { \
1130 case trie_utf8_fold: \
1131 if ( foldlen>0 ) { \
1132 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1137 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1138 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1139 foldlen -= UNISKIP( uvc ); \
1140 uscan = foldbuf + UNISKIP( uvc ); \
1143 case trie_latin_utf8_fold: \
1144 if ( foldlen>0 ) { \
1145 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1151 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1152 foldlen -= UNISKIP( uvc ); \
1153 uscan = foldbuf + UNISKIP( uvc ); \
1157 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1164 charid = trie->charmap[ uvc ]; \
1168 if (widecharmap) { \
1169 SV** const svpp = hv_fetch(widecharmap, \
1170 (char*)&uvc, sizeof(UV), 0); \
1172 charid = (U16)SvIV(*svpp); \
1177 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1179 char *my_strend= (char *)strend; \
1182 !ibcmp_utf8(s, &my_strend, 0, do_utf8, \
1183 m, NULL, ln, cBOOL(UTF))) \
1184 && (!reginfo || regtry(reginfo, &s)) ) \
1187 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1188 uvchr_to_utf8(tmpbuf, c); \
1189 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1191 && (f == c1 || f == c2) \
1193 !ibcmp_utf8(s, &my_strend, 0, do_utf8,\
1194 m, NULL, ln, cBOOL(UTF)))\
1195 && (!reginfo || regtry(reginfo, &s)) ) \
1201 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1205 && (ln == 1 || !(OP(c) == EXACTF \
1207 : ibcmp_locale(s, m, ln))) \
1208 && (!reginfo || regtry(reginfo, &s)) ) \
1214 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1216 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1222 #define REXEC_FBC_SCAN(CoDe) \
1224 while (s < strend) { \
1230 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1231 REXEC_FBC_UTF8_SCAN( \
1233 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1242 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1245 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1254 #define REXEC_FBC_TRYIT \
1255 if ((!reginfo || regtry(reginfo, &s))) \
1258 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1260 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1263 REXEC_FBC_CLASS_SCAN(CoNd); \
1267 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1270 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1273 REXEC_FBC_CLASS_SCAN(CoNd); \
1277 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1278 PL_reg_flags |= RF_tainted; \
1280 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1283 REXEC_FBC_CLASS_SCAN(CoNd); \
1287 #define DUMP_EXEC_POS(li,s,doutf8) \
1288 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1290 /* We know what class REx starts with. Try to find this position... */
1291 /* if reginfo is NULL, its a dryrun */
1292 /* annoyingly all the vars in this routine have different names from their counterparts
1293 in regmatch. /grrr */
1296 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1297 const char *strend, regmatch_info *reginfo)
1300 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1304 register STRLEN uskip;
1308 register I32 tmp = 1; /* Scratch variable? */
1309 register const bool do_utf8 = PL_reg_match_utf8;
1310 RXi_GET_DECL(prog,progi);
1312 PERL_ARGS_ASSERT_FIND_BYCLASS;
1314 /* We know what class it must start with. */
1318 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1319 !UTF8_IS_INVARIANT((U8)s[0]) ?
1320 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1321 REGINCLASS(prog, c, (U8*)s));
1324 while (s < strend) {
1327 if (REGINCLASS(prog, c, (U8*)s) ||
1328 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1329 /* The assignment of 2 is intentional:
1330 * for the folded sharp s, the skip is 2. */
1331 (skip = SHARP_S_SKIP))) {
1332 if (tmp && (!reginfo || regtry(reginfo, &s)))
1345 if (tmp && (!reginfo || regtry(reginfo, &s)))
1353 ln = STR_LEN(c); /* length to match in octets/bytes */
1354 lnc = (I32) ln; /* length to match in characters */
1356 STRLEN ulen1, ulen2;
1358 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1359 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1360 /* used by commented-out code below */
1361 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1363 /* XXX: Since the node will be case folded at compile
1364 time this logic is a little odd, although im not
1365 sure that its actually wrong. --dmq */
1367 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1368 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1370 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1371 codepoint of the first character in the converted
1372 form, yet originally we did the extra step.
1373 No tests fail by commenting this code out however
1374 so Ive left it out. -- dmq.
1376 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1378 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1383 while (sm < ((U8 *) m + ln)) {
1398 c2 = PL_fold_locale[c1];
1400 e = HOP3c(strend, -((I32)lnc), s);
1402 if (!reginfo && e < s)
1403 e = s; /* Due to minlen logic of intuit() */
1405 /* The idea in the EXACTF* cases is to first find the
1406 * first character of the EXACTF* node and then, if
1407 * necessary, case-insensitively compare the full
1408 * text of the node. The c1 and c2 are the first
1409 * characters (though in Unicode it gets a bit
1410 * more complicated because there are more cases
1411 * than just upper and lower: one needs to use
1412 * the so-called folding case for case-insensitive
1413 * matching (called "loose matching" in Unicode).
1414 * ibcmp_utf8() will do just that. */
1416 if (do_utf8 || UTF) {
1418 U8 tmpbuf [UTF8_MAXBYTES+1];
1421 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1423 /* Upper and lower of 1st char are equal -
1424 * probably not a "letter". */
1427 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1432 REXEC_FBC_EXACTISH_CHECK(c == c1);
1438 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1444 /* Handle some of the three Greek sigmas cases.
1445 * Note that not all the possible combinations
1446 * are handled here: some of them are handled
1447 * by the standard folding rules, and some of
1448 * them (the character class or ANYOF cases)
1449 * are handled during compiletime in
1450 * regexec.c:S_regclass(). */
1451 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1452 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1453 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1455 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1460 /* Neither pattern nor string are UTF8 */
1462 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1464 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1468 PL_reg_flags |= RF_tainted;
1475 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1476 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1478 tmp = ((OP(c) == BOUND ?
1479 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1480 LOAD_UTF8_CHARCLASS_ALNUM();
1481 REXEC_FBC_UTF8_SCAN(
1482 if (tmp == !(OP(c) == BOUND ?
1483 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) :
1484 isALNUM_LC_utf8((U8*)s)))
1492 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1493 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1496 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1502 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1506 PL_reg_flags |= RF_tainted;
1513 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1514 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1516 tmp = ((OP(c) == NBOUND ?
1517 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1518 LOAD_UTF8_CHARCLASS_ALNUM();
1519 REXEC_FBC_UTF8_SCAN(
1520 if (tmp == !(OP(c) == NBOUND ?
1521 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) :
1522 isALNUM_LC_utf8((U8*)s)))
1524 else REXEC_FBC_TRYIT;
1528 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1529 tmp = ((OP(c) == NBOUND ?
1530 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1533 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1535 else REXEC_FBC_TRYIT;
1538 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1542 REXEC_FBC_CSCAN_PRELOAD(
1543 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1544 swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1548 REXEC_FBC_CSCAN_TAINT(
1549 isALNUM_LC_utf8((U8*)s),
1553 REXEC_FBC_CSCAN_PRELOAD(
1554 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1555 !swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1559 REXEC_FBC_CSCAN_TAINT(
1560 !isALNUM_LC_utf8((U8*)s),
1564 REXEC_FBC_CSCAN_PRELOAD(
1565 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1566 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8),
1570 REXEC_FBC_CSCAN_TAINT(
1571 *s == ' ' || isSPACE_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, do_utf8)),
1581 REXEC_FBC_CSCAN_TAINT(
1582 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1586 REXEC_FBC_CSCAN_PRELOAD(
1587 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1588 swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1592 REXEC_FBC_CSCAN_TAINT(
1593 isDIGIT_LC_utf8((U8*)s),
1597 REXEC_FBC_CSCAN_PRELOAD(
1598 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1599 !swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1603 REXEC_FBC_CSCAN_TAINT(
1604 !isDIGIT_LC_utf8((U8*)s),
1610 is_LNBREAK_latin1(s)
1620 !is_VERTWS_latin1(s)
1625 is_HORIZWS_latin1(s)
1629 !is_HORIZWS_utf8(s),
1630 !is_HORIZWS_latin1(s)
1636 /* what trie are we using right now */
1638 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1640 = (reg_trie_data*)progi->data->data[ aho->trie ];
1641 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1643 const char *last_start = strend - trie->minlen;
1645 const char *real_start = s;
1647 STRLEN maxlen = trie->maxlen;
1649 U8 **points; /* map of where we were in the input string
1650 when reading a given char. For ASCII this
1651 is unnecessary overhead as the relationship
1652 is always 1:1, but for Unicode, especially
1653 case folded Unicode this is not true. */
1654 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1658 GET_RE_DEBUG_FLAGS_DECL;
1660 /* We can't just allocate points here. We need to wrap it in
1661 * an SV so it gets freed properly if there is a croak while
1662 * running the match */
1665 sv_points=newSV(maxlen * sizeof(U8 *));
1666 SvCUR_set(sv_points,
1667 maxlen * sizeof(U8 *));
1668 SvPOK_on(sv_points);
1669 sv_2mortal(sv_points);
1670 points=(U8**)SvPV_nolen(sv_points );
1671 if ( trie_type != trie_utf8_fold
1672 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1675 bitmap=(U8*)trie->bitmap;
1677 bitmap=(U8*)ANYOF_BITMAP(c);
1679 /* this is the Aho-Corasick algorithm modified a touch
1680 to include special handling for long "unknown char"
1681 sequences. The basic idea being that we use AC as long
1682 as we are dealing with a possible matching char, when
1683 we encounter an unknown char (and we have not encountered
1684 an accepting state) we scan forward until we find a legal
1686 AC matching is basically that of trie matching, except
1687 that when we encounter a failing transition, we fall back
1688 to the current states "fail state", and try the current char
1689 again, a process we repeat until we reach the root state,
1690 state 1, or a legal transition. If we fail on the root state
1691 then we can either terminate if we have reached an accepting
1692 state previously, or restart the entire process from the beginning
1696 while (s <= last_start) {
1697 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1705 U8 *uscan = (U8*)NULL;
1706 U8 *leftmost = NULL;
1708 U32 accepted_word= 0;
1712 while ( state && uc <= (U8*)strend ) {
1714 U32 word = aho->states[ state ].wordnum;
1718 DEBUG_TRIE_EXECUTE_r(
1719 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1720 dump_exec_pos( (char *)uc, c, strend, real_start,
1721 (char *)uc, do_utf8 );
1722 PerlIO_printf( Perl_debug_log,
1723 " Scanning for legal start char...\n");
1726 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1731 if (uc >(U8*)last_start) break;
1735 U8 *lpos= points[ (pointpos - trie->wordlen[word-1] ) % maxlen ];
1736 if (!leftmost || lpos < leftmost) {
1737 DEBUG_r(accepted_word=word);
1743 points[pointpos++ % maxlen]= uc;
1744 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1745 uscan, len, uvc, charid, foldlen,
1747 DEBUG_TRIE_EXECUTE_r({
1748 dump_exec_pos( (char *)uc, c, strend, real_start,
1750 PerlIO_printf(Perl_debug_log,
1751 " Charid:%3u CP:%4"UVxf" ",
1757 word = aho->states[ state ].wordnum;
1759 base = aho->states[ state ].trans.base;
1761 DEBUG_TRIE_EXECUTE_r({
1763 dump_exec_pos( (char *)uc, c, strend, real_start,
1765 PerlIO_printf( Perl_debug_log,
1766 "%sState: %4"UVxf", word=%"UVxf,
1767 failed ? " Fail transition to " : "",
1768 (UV)state, (UV)word);
1773 (base + charid > trie->uniquecharcount )
1774 && (base + charid - 1 - trie->uniquecharcount
1776 && trie->trans[base + charid - 1 -
1777 trie->uniquecharcount].check == state
1778 && (tmp=trie->trans[base + charid - 1 -
1779 trie->uniquecharcount ].next))
1781 DEBUG_TRIE_EXECUTE_r(
1782 PerlIO_printf( Perl_debug_log," - legal\n"));
1787 DEBUG_TRIE_EXECUTE_r(
1788 PerlIO_printf( Perl_debug_log," - fail\n"));
1790 state = aho->fail[state];
1794 /* we must be accepting here */
1795 DEBUG_TRIE_EXECUTE_r(
1796 PerlIO_printf( Perl_debug_log," - accepting\n"));
1805 if (!state) state = 1;
1808 if ( aho->states[ state ].wordnum ) {
1809 U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ];
1810 if (!leftmost || lpos < leftmost) {
1811 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1816 s = (char*)leftmost;
1817 DEBUG_TRIE_EXECUTE_r({
1819 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1820 (UV)accepted_word, (IV)(s - real_start)
1823 if (!reginfo || regtry(reginfo, &s)) {
1829 DEBUG_TRIE_EXECUTE_r({
1830 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1833 DEBUG_TRIE_EXECUTE_r(
1834 PerlIO_printf( Perl_debug_log,"No match.\n"));
1843 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1853 - regexec_flags - match a regexp against a string
1856 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1857 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1858 /* strend: pointer to null at end of string */
1859 /* strbeg: real beginning of string */
1860 /* minend: end of match must be >=minend after stringarg. */
1861 /* data: May be used for some additional optimizations.
1862 Currently its only used, with a U32 cast, for transmitting
1863 the ganch offset when doing a /g match. This will change */
1864 /* nosave: For optimizations. */
1867 struct regexp *const prog = (struct regexp *)SvANY(rx);
1868 /*register*/ char *s;
1869 register regnode *c;
1870 /*register*/ char *startpos = stringarg;
1871 I32 minlen; /* must match at least this many chars */
1872 I32 dontbother = 0; /* how many characters not to try at end */
1873 I32 end_shift = 0; /* Same for the end. */ /* CC */
1874 I32 scream_pos = -1; /* Internal iterator of scream. */
1875 char *scream_olds = NULL;
1876 const bool do_utf8 = cBOOL(DO_UTF8(sv));
1878 RXi_GET_DECL(prog,progi);
1879 regmatch_info reginfo; /* create some info to pass to regtry etc */
1880 regexp_paren_pair *swap = NULL;
1881 GET_RE_DEBUG_FLAGS_DECL;
1883 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1884 PERL_UNUSED_ARG(data);
1886 /* Be paranoid... */
1887 if (prog == NULL || startpos == NULL) {
1888 Perl_croak(aTHX_ "NULL regexp parameter");
1892 multiline = prog->extflags & RXf_PMf_MULTILINE;
1893 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1895 RX_MATCH_UTF8_set(rx, do_utf8);
1897 debug_start_match(rx, do_utf8, startpos, strend,
1901 minlen = prog->minlen;
1903 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1904 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1905 "String too short [regexec_flags]...\n"));
1910 /* Check validity of program. */
1911 if (UCHARAT(progi->program) != REG_MAGIC) {
1912 Perl_croak(aTHX_ "corrupted regexp program");
1916 PL_reg_eval_set = 0;
1920 PL_reg_flags |= RF_utf8;
1922 /* Mark beginning of line for ^ and lookbehind. */
1923 reginfo.bol = startpos; /* XXX not used ??? */
1927 /* Mark end of line for $ (and such) */
1930 /* see how far we have to get to not match where we matched before */
1931 reginfo.till = startpos+minend;
1933 /* If there is a "must appear" string, look for it. */
1936 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1938 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
1939 reginfo.ganch = startpos + prog->gofs;
1940 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1941 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
1942 } else if (sv && SvTYPE(sv) >= SVt_PVMG
1944 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1945 && mg->mg_len >= 0) {
1946 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1947 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1948 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
1950 if (prog->extflags & RXf_ANCH_GPOS) {
1951 if (s > reginfo.ganch)
1953 s = reginfo.ganch - prog->gofs;
1954 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1955 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
1961 reginfo.ganch = strbeg + PTR2UV(data);
1962 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1963 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
1965 } else { /* pos() not defined */
1966 reginfo.ganch = strbeg;
1967 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1968 "GPOS: reginfo.ganch = strbeg\n"));
1971 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1972 /* We have to be careful. If the previous successful match
1973 was from this regex we don't want a subsequent partially
1974 successful match to clobber the old results.
1975 So when we detect this possibility we add a swap buffer
1976 to the re, and switch the buffer each match. If we fail
1977 we switch it back, otherwise we leave it swapped.
1980 /* do we need a save destructor here for eval dies? */
1981 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
1983 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1984 re_scream_pos_data d;
1986 d.scream_olds = &scream_olds;
1987 d.scream_pos = &scream_pos;
1988 s = re_intuit_start(rx, sv, s, strend, flags, &d);
1990 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1991 goto phooey; /* not present */
1997 /* Simplest case: anchored match need be tried only once. */
1998 /* [unless only anchor is BOL and multiline is set] */
1999 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2000 if (s == startpos && regtry(®info, &startpos))
2002 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2003 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2008 dontbother = minlen - 1;
2009 end = HOP3c(strend, -dontbother, strbeg) - 1;
2010 /* for multiline we only have to try after newlines */
2011 if (prog->check_substr || prog->check_utf8) {
2015 if (regtry(®info, &s))
2020 if (prog->extflags & RXf_USE_INTUIT) {
2021 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2032 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2033 if (regtry(®info, &s))
2040 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2042 /* the warning about reginfo.ganch being used without intialization
2043 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2044 and we only enter this block when the same bit is set. */
2045 char *tmp_s = reginfo.ganch - prog->gofs;
2047 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2052 /* Messy cases: unanchored match. */
2053 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2054 /* we have /x+whatever/ */
2055 /* it must be a one character string (XXXX Except UTF?) */
2060 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2061 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2062 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
2067 DEBUG_EXECUTE_r( did_match = 1 );
2068 if (regtry(®info, &s)) goto got_it;
2070 while (s < strend && *s == ch)
2078 DEBUG_EXECUTE_r( did_match = 1 );
2079 if (regtry(®info, &s)) goto got_it;
2081 while (s < strend && *s == ch)
2086 DEBUG_EXECUTE_r(if (!did_match)
2087 PerlIO_printf(Perl_debug_log,
2088 "Did not find anchored character...\n")
2091 else if (prog->anchored_substr != NULL
2092 || prog->anchored_utf8 != NULL
2093 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2094 && prog->float_max_offset < strend - s)) {
2099 char *last1; /* Last position checked before */
2103 if (prog->anchored_substr || prog->anchored_utf8) {
2104 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2105 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2106 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
2107 back_max = back_min = prog->anchored_offset;
2109 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2110 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2111 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
2112 back_max = prog->float_max_offset;
2113 back_min = prog->float_min_offset;
2117 if (must == &PL_sv_undef)
2118 /* could not downgrade utf8 check substring, so must fail */
2124 last = HOP3c(strend, /* Cannot start after this */
2125 -(I32)(CHR_SVLEN(must)
2126 - (SvTAIL(must) != 0) + back_min), strbeg);
2129 last1 = HOPc(s, -1);
2131 last1 = s - 1; /* bogus */
2133 /* XXXX check_substr already used to find "s", can optimize if
2134 check_substr==must. */
2136 dontbother = end_shift;
2137 strend = HOPc(strend, -dontbother);
2138 while ( (s <= last) &&
2139 ((flags & REXEC_SCREAM)
2140 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2141 end_shift, &scream_pos, 0))
2142 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2143 (unsigned char*)strend, must,
2144 multiline ? FBMrf_MULTILINE : 0))) ) {
2145 /* we may be pointing at the wrong string */
2146 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2147 s = strbeg + (s - SvPVX_const(sv));
2148 DEBUG_EXECUTE_r( did_match = 1 );
2149 if (HOPc(s, -back_max) > last1) {
2150 last1 = HOPc(s, -back_min);
2151 s = HOPc(s, -back_max);
2154 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2156 last1 = HOPc(s, -back_min);
2160 while (s <= last1) {
2161 if (regtry(®info, &s))
2167 while (s <= last1) {
2168 if (regtry(®info, &s))
2174 DEBUG_EXECUTE_r(if (!did_match) {
2175 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
2176 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2177 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2178 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2179 ? "anchored" : "floating"),
2180 quoted, RE_SV_TAIL(must));
2184 else if ( (c = progi->regstclass) ) {
2186 const OPCODE op = OP(progi->regstclass);
2187 /* don't bother with what can't match */
2188 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2189 strend = HOPc(strend, -(minlen - 1));
2192 SV * const prop = sv_newmortal();
2193 regprop(prog, prop, c);
2195 RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1),
2197 PerlIO_printf(Perl_debug_log,
2198 "Matching stclass %.*s against %s (%d chars)\n",
2199 (int)SvCUR(prop), SvPVX_const(prop),
2200 quoted, (int)(strend - s));
2203 if (find_byclass(prog, c, s, strend, ®info))
2205 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2209 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2214 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2215 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2216 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
2218 if (flags & REXEC_SCREAM) {
2219 last = screaminstr(sv, float_real, s - strbeg,
2220 end_shift, &scream_pos, 1); /* last one */
2222 last = scream_olds; /* Only one occurrence. */
2223 /* we may be pointing at the wrong string */
2224 else if (RXp_MATCH_COPIED(prog))
2225 s = strbeg + (s - SvPVX_const(sv));
2229 const char * const little = SvPV_const(float_real, len);
2231 if (SvTAIL(float_real)) {
2232 if (memEQ(strend - len + 1, little, len - 1))
2233 last = strend - len + 1;
2234 else if (!multiline)
2235 last = memEQ(strend - len, little, len)
2236 ? strend - len : NULL;
2242 last = rninstr(s, strend, little, little + len);
2244 last = strend; /* matching "$" */
2249 PerlIO_printf(Perl_debug_log,
2250 "%sCan't trim the tail, match fails (should not happen)%s\n",
2251 PL_colors[4], PL_colors[5]));
2252 goto phooey; /* Should not happen! */
2254 dontbother = strend - last + prog->float_min_offset;
2256 if (minlen && (dontbother < minlen))
2257 dontbother = minlen - 1;
2258 strend -= dontbother; /* this one's always in bytes! */
2259 /* We don't know much -- general case. */
2262 if (regtry(®info, &s))
2271 if (regtry(®info, &s))
2273 } while (s++ < strend);
2282 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2284 if (PL_reg_eval_set)
2285 restore_pos(aTHX_ prog);
2286 if (RXp_PAREN_NAMES(prog))
2287 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2289 /* make sure $`, $&, $', and $digit will work later */
2290 if ( !(flags & REXEC_NOT_FIRST) ) {
2291 RX_MATCH_COPY_FREE(rx);
2292 if (flags & REXEC_COPY_STR) {
2293 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2294 #ifdef PERL_OLD_COPY_ON_WRITE
2296 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2298 PerlIO_printf(Perl_debug_log,
2299 "Copy on write: regexp capture, type %d\n",
2302 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2303 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2304 assert (SvPOKp(prog->saved_copy));
2308 RX_MATCH_COPIED_on(rx);
2309 s = savepvn(strbeg, i);
2315 prog->subbeg = strbeg;
2316 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2323 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2324 PL_colors[4], PL_colors[5]));
2325 if (PL_reg_eval_set)
2326 restore_pos(aTHX_ prog);
2328 /* we failed :-( roll it back */
2329 Safefree(prog->offs);
2338 - regtry - try match at specific point
2340 STATIC I32 /* 0 failure, 1 success */
2341 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2345 REGEXP *const rx = reginfo->prog;
2346 regexp *const prog = (struct regexp *)SvANY(rx);
2347 RXi_GET_DECL(prog,progi);
2348 GET_RE_DEBUG_FLAGS_DECL;
2350 PERL_ARGS_ASSERT_REGTRY;
2352 reginfo->cutpoint=NULL;
2354 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2357 PL_reg_eval_set = RS_init;
2358 DEBUG_EXECUTE_r(DEBUG_s(
2359 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2360 (IV)(PL_stack_sp - PL_stack_base));
2363 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2364 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2366 /* Apparently this is not needed, judging by wantarray. */
2367 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2368 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2371 /* Make $_ available to executed code. */
2372 if (reginfo->sv != DEFSV) {
2374 DEFSV_set(reginfo->sv);
2377 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2378 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2379 /* prepare for quick setting of pos */
2380 #ifdef PERL_OLD_COPY_ON_WRITE
2381 if (SvIsCOW(reginfo->sv))
2382 sv_force_normal_flags(reginfo->sv, 0);
2384 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2385 &PL_vtbl_mglob, NULL, 0);
2389 PL_reg_oldpos = mg->mg_len;
2390 SAVEDESTRUCTOR_X(restore_pos, prog);
2392 if (!PL_reg_curpm) {
2393 Newxz(PL_reg_curpm, 1, PMOP);
2396 SV* const repointer = &PL_sv_undef;
2397 /* this regexp is also owned by the new PL_reg_curpm, which
2398 will try to free it. */
2399 av_push(PL_regex_padav, repointer);
2400 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2401 PL_regex_pad = AvARRAY(PL_regex_padav);
2406 /* It seems that non-ithreads works both with and without this code.
2407 So for efficiency reasons it seems best not to have the code
2408 compiled when it is not needed. */
2409 /* This is safe against NULLs: */
2410 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2411 /* PM_reg_curpm owns a reference to this regexp. */
2414 PM_SETRE(PL_reg_curpm, rx);
2415 PL_reg_oldcurpm = PL_curpm;
2416 PL_curpm = PL_reg_curpm;
2417 if (RXp_MATCH_COPIED(prog)) {
2418 /* Here is a serious problem: we cannot rewrite subbeg,
2419 since it may be needed if this match fails. Thus
2420 $` inside (?{}) could fail... */
2421 PL_reg_oldsaved = prog->subbeg;
2422 PL_reg_oldsavedlen = prog->sublen;
2423 #ifdef PERL_OLD_COPY_ON_WRITE
2424 PL_nrs = prog->saved_copy;
2426 RXp_MATCH_COPIED_off(prog);
2429 PL_reg_oldsaved = NULL;
2430 prog->subbeg = PL_bostr;
2431 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2433 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2434 prog->offs[0].start = *startpos - PL_bostr;
2435 PL_reginput = *startpos;
2436 PL_reglastparen = &prog->lastparen;
2437 PL_reglastcloseparen = &prog->lastcloseparen;
2438 prog->lastparen = 0;
2439 prog->lastcloseparen = 0;
2441 PL_regoffs = prog->offs;
2442 if (PL_reg_start_tmpl <= prog->nparens) {
2443 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2444 if(PL_reg_start_tmp)
2445 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2447 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2450 /* XXXX What this code is doing here?!!! There should be no need
2451 to do this again and again, PL_reglastparen should take care of
2454 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2455 * Actually, the code in regcppop() (which Ilya may be meaning by
2456 * PL_reglastparen), is not needed at all by the test suite
2457 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2458 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2459 * Meanwhile, this code *is* needed for the
2460 * above-mentioned test suite tests to succeed. The common theme
2461 * on those tests seems to be returning null fields from matches.
2462 * --jhi updated by dapm */
2464 if (prog->nparens) {
2465 regexp_paren_pair *pp = PL_regoffs;
2467 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2475 if (regmatch(reginfo, progi->program + 1)) {
2476 PL_regoffs[0].end = PL_reginput - PL_bostr;
2479 if (reginfo->cutpoint)
2480 *startpos= reginfo->cutpoint;
2481 REGCP_UNWIND(lastcp);
2486 #define sayYES goto yes
2487 #define sayNO goto no
2488 #define sayNO_SILENT goto no_silent
2490 /* we dont use STMT_START/END here because it leads to
2491 "unreachable code" warnings, which are bogus, but distracting. */
2492 #define CACHEsayNO \
2493 if (ST.cache_mask) \
2494 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2497 /* this is used to determine how far from the left messages like
2498 'failed...' are printed. It should be set such that messages
2499 are inline with the regop output that created them.
2501 #define REPORT_CODE_OFF 32
2504 /* Make sure there is a test for this +1 options in re_tests */
2505 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2507 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2508 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2510 #define SLAB_FIRST(s) (&(s)->states[0])
2511 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2513 /* grab a new slab and return the first slot in it */
2515 STATIC regmatch_state *
2518 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2521 regmatch_slab *s = PL_regmatch_slab->next;
2523 Newx(s, 1, regmatch_slab);
2524 s->prev = PL_regmatch_slab;
2526 PL_regmatch_slab->next = s;
2528 PL_regmatch_slab = s;
2529 return SLAB_FIRST(s);
2533 /* push a new state then goto it */
2535 #define PUSH_STATE_GOTO(state, node) \
2537 st->resume_state = state; \
2540 /* push a new state with success backtracking, then goto it */
2542 #define PUSH_YES_STATE_GOTO(state, node) \
2544 st->resume_state = state; \
2545 goto push_yes_state;
2551 regmatch() - main matching routine
2553 This is basically one big switch statement in a loop. We execute an op,
2554 set 'next' to point the next op, and continue. If we come to a point which
2555 we may need to backtrack to on failure such as (A|B|C), we push a
2556 backtrack state onto the backtrack stack. On failure, we pop the top
2557 state, and re-enter the loop at the state indicated. If there are no more
2558 states to pop, we return failure.
2560 Sometimes we also need to backtrack on success; for example /A+/, where
2561 after successfully matching one A, we need to go back and try to
2562 match another one; similarly for lookahead assertions: if the assertion
2563 completes successfully, we backtrack to the state just before the assertion
2564 and then carry on. In these cases, the pushed state is marked as
2565 'backtrack on success too'. This marking is in fact done by a chain of
2566 pointers, each pointing to the previous 'yes' state. On success, we pop to
2567 the nearest yes state, discarding any intermediate failure-only states.
2568 Sometimes a yes state is pushed just to force some cleanup code to be
2569 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2570 it to free the inner regex.
2572 Note that failure backtracking rewinds the cursor position, while
2573 success backtracking leaves it alone.
2575 A pattern is complete when the END op is executed, while a subpattern
2576 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2577 ops trigger the "pop to last yes state if any, otherwise return true"
2580 A common convention in this function is to use A and B to refer to the two
2581 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2582 the subpattern to be matched possibly multiple times, while B is the entire
2583 rest of the pattern. Variable and state names reflect this convention.
2585 The states in the main switch are the union of ops and failure/success of
2586 substates associated with with that op. For example, IFMATCH is the op
2587 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2588 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2589 successfully matched A and IFMATCH_A_fail is a state saying that we have
2590 just failed to match A. Resume states always come in pairs. The backtrack
2591 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2592 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2593 on success or failure.
2595 The struct that holds a backtracking state is actually a big union, with
2596 one variant for each major type of op. The variable st points to the
2597 top-most backtrack struct. To make the code clearer, within each
2598 block of code we #define ST to alias the relevant union.
2600 Here's a concrete example of a (vastly oversimplified) IFMATCH
2606 #define ST st->u.ifmatch
2608 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2609 ST.foo = ...; // some state we wish to save
2611 // push a yes backtrack state with a resume value of
2612 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2614 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2617 case IFMATCH_A: // we have successfully executed A; now continue with B
2619 bar = ST.foo; // do something with the preserved value
2622 case IFMATCH_A_fail: // A failed, so the assertion failed
2623 ...; // do some housekeeping, then ...
2624 sayNO; // propagate the failure
2631 For any old-timers reading this who are familiar with the old recursive
2632 approach, the code above is equivalent to:
2634 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2643 ...; // do some housekeeping, then ...
2644 sayNO; // propagate the failure
2647 The topmost backtrack state, pointed to by st, is usually free. If you
2648 want to claim it, populate any ST.foo fields in it with values you wish to
2649 save, then do one of
2651 PUSH_STATE_GOTO(resume_state, node);
2652 PUSH_YES_STATE_GOTO(resume_state, node);
2654 which sets that backtrack state's resume value to 'resume_state', pushes a
2655 new free entry to the top of the backtrack stack, then goes to 'node'.
2656 On backtracking, the free slot is popped, and the saved state becomes the
2657 new free state. An ST.foo field in this new top state can be temporarily
2658 accessed to retrieve values, but once the main loop is re-entered, it
2659 becomes available for reuse.
2661 Note that the depth of the backtrack stack constantly increases during the
2662 left-to-right execution of the pattern, rather than going up and down with
2663 the pattern nesting. For example the stack is at its maximum at Z at the
2664 end of the pattern, rather than at X in the following:
2666 /(((X)+)+)+....(Y)+....Z/
2668 The only exceptions to this are lookahead/behind assertions and the cut,
2669 (?>A), which pop all the backtrack states associated with A before
2672 Bascktrack state structs are allocated in slabs of about 4K in size.
2673 PL_regmatch_state and st always point to the currently active state,
2674 and PL_regmatch_slab points to the slab currently containing
2675 PL_regmatch_state. The first time regmatch() is called, the first slab is
2676 allocated, and is never freed until interpreter destruction. When the slab
2677 is full, a new one is allocated and chained to the end. At exit from
2678 regmatch(), slabs allocated since entry are freed.
2683 #define DEBUG_STATE_pp(pp) \
2685 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2686 PerlIO_printf(Perl_debug_log, \
2687 " %*s"pp" %s%s%s%s%s\n", \
2689 PL_reg_name[st->resume_state], \
2690 ((st==yes_state||st==mark_state) ? "[" : ""), \
2691 ((st==yes_state) ? "Y" : ""), \
2692 ((st==mark_state) ? "M" : ""), \
2693 ((st==yes_state||st==mark_state) ? "]" : "") \
2698 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2703 S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8,
2704 const char *start, const char *end, const char *blurb)
2706 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2708 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2713 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2714 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2716 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2717 start, end - start, 60);
2719 PerlIO_printf(Perl_debug_log,
2720 "%s%s REx%s %s against %s\n",
2721 PL_colors[4], blurb, PL_colors[5], s0, s1);
2723 if (do_utf8||utf8_pat)
2724 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2725 utf8_pat ? "pattern" : "",
2726 utf8_pat && do_utf8 ? " and " : "",
2727 do_utf8 ? "string" : ""
2733 S_dump_exec_pos(pTHX_ const char *locinput,
2734 const regnode *scan,
2735 const char *loc_regeol,
2736 const char *loc_bostr,
2737 const char *loc_reg_starttry,
2740 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2741 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2742 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2743 /* The part of the string before starttry has one color
2744 (pref0_len chars), between starttry and current
2745 position another one (pref_len - pref0_len chars),
2746 after the current position the third one.
2747 We assume that pref0_len <= pref_len, otherwise we
2748 decrease pref0_len. */
2749 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2750 ? (5 + taill) - l : locinput - loc_bostr;
2753 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2755 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2757 pref0_len = pref_len - (locinput - loc_reg_starttry);
2758 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2759 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2760 ? (5 + taill) - pref_len : loc_regeol - locinput);
2761 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2765 if (pref0_len > pref_len)
2766 pref0_len = pref_len;
2768 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2770 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2771 (locinput - pref_len),pref0_len, 60, 4, 5);
2773 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2774 (locinput - pref_len + pref0_len),
2775 pref_len - pref0_len, 60, 2, 3);
2777 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2778 locinput, loc_regeol - locinput, 10, 0, 1);
2780 const STRLEN tlen=len0+len1+len2;
2781 PerlIO_printf(Perl_debug_log,
2782 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2783 (IV)(locinput - loc_bostr),
2786 (docolor ? "" : "> <"),
2788 (int)(tlen > 19 ? 0 : 19 - tlen),
2795 /* reg_check_named_buff_matched()
2796 * Checks to see if a named buffer has matched. The data array of
2797 * buffer numbers corresponding to the buffer is expected to reside
2798 * in the regexp->data->data array in the slot stored in the ARG() of
2799 * node involved. Note that this routine doesn't actually care about the
2800 * name, that information is not preserved from compilation to execution.
2801 * Returns the index of the leftmost defined buffer with the given name
2802 * or 0 if non of the buffers matched.
2805 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2808 RXi_GET_DECL(rex,rexi);
2809 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2810 I32 *nums=(I32*)SvPVX(sv_dat);
2812 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2814 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2815 if ((I32)*PL_reglastparen >= nums[n] &&
2816 PL_regoffs[nums[n]].end != -1)
2825 /* free all slabs above current one - called during LEAVE_SCOPE */
2828 S_clear_backtrack_stack(pTHX_ void *p)
2830 regmatch_slab *s = PL_regmatch_slab->next;
2835 PL_regmatch_slab->next = NULL;
2837 regmatch_slab * const osl = s;
2844 #define SETREX(Re1,Re2) \
2845 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2848 STATIC I32 /* 0 failure, 1 success */
2849 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2851 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2855 register const bool do_utf8 = PL_reg_match_utf8;
2856 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2857 REGEXP *rex_sv = reginfo->prog;
2858 regexp *rex = (struct regexp *)SvANY(rex_sv);
2859 RXi_GET_DECL(rex,rexi);
2861 /* the current state. This is a cached copy of PL_regmatch_state */
2862 register regmatch_state *st;
2863 /* cache heavy used fields of st in registers */
2864 register regnode *scan;
2865 register regnode *next;
2866 register U32 n = 0; /* general value; init to avoid compiler warning */
2867 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2868 register char *locinput = PL_reginput;
2869 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2871 bool result = 0; /* return value of S_regmatch */
2872 int depth = 0; /* depth of backtrack stack */
2873 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2874 const U32 max_nochange_depth =
2875 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2876 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2877 regmatch_state *yes_state = NULL; /* state to pop to on success of
2879 /* mark_state piggy backs on the yes_state logic so that when we unwind
2880 the stack on success we can update the mark_state as we go */
2881 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2882 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2883 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2885 bool no_final = 0; /* prevent failure from backtracking? */
2886 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2887 char *startpoint = PL_reginput;
2888 SV *popmark = NULL; /* are we looking for a mark? */
2889 SV *sv_commit = NULL; /* last mark name seen in failure */
2890 SV *sv_yes_mark = NULL; /* last mark name we have seen
2891 during a successfull match */
2892 U32 lastopen = 0; /* last open we saw */
2893 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2894 SV* const oreplsv = GvSV(PL_replgv);
2895 /* these three flags are set by various ops to signal information to
2896 * the very next op. They have a useful lifetime of exactly one loop
2897 * iteration, and are not preserved or restored by state pushes/pops
2899 bool sw = 0; /* the condition value in (?(cond)a|b) */
2900 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2901 int logical = 0; /* the following EVAL is:
2905 or the following IFMATCH/UNLESSM is:
2906 false: plain (?=foo)
2907 true: used as a condition: (?(?=foo))
2910 GET_RE_DEBUG_FLAGS_DECL;
2913 PERL_ARGS_ASSERT_REGMATCH;
2915 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2916 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2918 /* on first ever call to regmatch, allocate first slab */
2919 if (!PL_regmatch_slab) {
2920 Newx(PL_regmatch_slab, 1, regmatch_slab);
2921 PL_regmatch_slab->prev = NULL;
2922 PL_regmatch_slab->next = NULL;
2923 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2926 oldsave = PL_savestack_ix;
2927 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2928 SAVEVPTR(PL_regmatch_slab);
2929 SAVEVPTR(PL_regmatch_state);
2931 /* grab next free state slot */
2932 st = ++PL_regmatch_state;
2933 if (st > SLAB_LAST(PL_regmatch_slab))
2934 st = PL_regmatch_state = S_push_slab(aTHX);
2936 /* Note that nextchr is a byte even in UTF */
2937 nextchr = UCHARAT(locinput);
2939 while (scan != NULL) {
2942 SV * const prop = sv_newmortal();
2943 regnode *rnext=regnext(scan);
2944 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2945 regprop(rex, prop, scan);
2947 PerlIO_printf(Perl_debug_log,
2948 "%3"IVdf":%*s%s(%"IVdf")\n",
2949 (IV)(scan - rexi->program), depth*2, "",
2951 (PL_regkind[OP(scan)] == END || !rnext) ?
2952 0 : (IV)(rnext - rexi->program));
2955 next = scan + NEXT_OFF(scan);
2958 state_num = OP(scan);
2960 REH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);
2963 assert(PL_reglastparen == &rex->lastparen);
2964 assert(PL_reglastcloseparen == &rex->lastcloseparen);
2965 assert(PL_regoffs == rex->offs);
2967 switch (state_num) {
2969 if (locinput == PL_bostr)
2971 /* reginfo->till = reginfo->bol; */
2976 if (locinput == PL_bostr ||
2977 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2983 if (locinput == PL_bostr)
2987 if (locinput == reginfo->ganch)
2992 /* update the startpoint */
2993 st->u.keeper.val = PL_regoffs[0].start;
2994 PL_reginput = locinput;
2995 PL_regoffs[0].start = locinput - PL_bostr;
2996 PUSH_STATE_GOTO(KEEPS_next, next);
2998 case KEEPS_next_fail:
2999 /* rollback the start point change */
3000 PL_regoffs[0].start = st->u.keeper.val;
3006 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3011 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3013 if (PL_regeol - locinput > 1)
3017 if (PL_regeol != locinput)
3021 if (!nextchr && locinput >= PL_regeol)
3024 locinput += PL_utf8skip[nextchr];
3025 if (locinput > PL_regeol)
3027 nextchr = UCHARAT(locinput);
3030 nextchr = UCHARAT(++locinput);
3033 if (!nextchr && locinput >= PL_regeol)
3035 nextchr = UCHARAT(++locinput);
3038 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3041 locinput += PL_utf8skip[nextchr];
3042 if (locinput > PL_regeol)
3044 nextchr = UCHARAT(locinput);
3047 nextchr = UCHARAT(++locinput);
3051 #define ST st->u.trie
3053 /* In this case the charclass data is available inline so
3054 we can fail fast without a lot of extra overhead.
3056 if (scan->flags == EXACT || !do_utf8) {
3057 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3059 PerlIO_printf(Perl_debug_log,
3060 "%*s %sfailed to match trie start class...%s\n",
3061 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3070 /* what type of TRIE am I? (utf8 makes this contextual) */
3071 DECL_TRIE_TYPE(scan);
3073 /* what trie are we using right now */
3074 reg_trie_data * const trie
3075 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3076 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3077 U32 state = trie->startstate;
3079 if (trie->bitmap && trie_type != trie_utf8_fold &&
3080 !TRIE_BITMAP_TEST(trie,*locinput)
3082 if (trie->states[ state ].wordnum) {
3084 PerlIO_printf(Perl_debug_log,
3085 "%*s %smatched empty string...%s\n",
3086 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3091 PerlIO_printf(Perl_debug_log,
3092 "%*s %sfailed to match trie start class...%s\n",
3093 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3100 U8 *uc = ( U8* )locinput;
3104 U8 *uscan = (U8*)NULL;
3106 SV *sv_accept_buff = NULL;
3107 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3109 ST.accepted = 0; /* how many accepting states we have seen */
3111 ST.jump = trie->jump;
3114 traverse the TRIE keeping track of all accepting states
3115 we transition through until we get to a failing node.
3118 while ( state && uc <= (U8*)PL_regeol ) {
3119 U32 base = trie->states[ state ].trans.base;
3122 /* We use charid to hold the wordnum as we don't use it
3123 for charid until after we have done the wordnum logic.
3124 We define an alias just so that the wordnum logic reads
3127 #define got_wordnum charid
3128 got_wordnum = trie->states[ state ].wordnum;
3130 if ( got_wordnum ) {
3131 if ( ! ST.accepted ) {
3133 SAVETMPS; /* XXX is this necessary? dmq */
3134 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
3135 sv_accept_buff=newSV(bufflen *
3136 sizeof(reg_trie_accepted) - 1);
3137 SvCUR_set(sv_accept_buff, 0);
3138 SvPOK_on(sv_accept_buff);
3139 sv_2mortal(sv_accept_buff);
3142 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
3145 if (ST.accepted >= bufflen) {
3147 ST.accept_buff =(reg_trie_accepted*)
3148 SvGROW(sv_accept_buff,
3149 bufflen * sizeof(reg_trie_accepted));
3151 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
3152 + sizeof(reg_trie_accepted));
3155 ST.accept_buff[ST.accepted].wordnum = got_wordnum;
3156 ST.accept_buff[ST.accepted].endpos = uc;
3158 } while (trie->nextword && (got_wordnum= trie->nextword[got_wordnum]));
3162 DEBUG_TRIE_EXECUTE_r({
3163 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
3164 PerlIO_printf( Perl_debug_log,
3165 "%*s %sState: %4"UVxf" Accepted: %4"UVxf" ",
3166 2+depth * 2, "", PL_colors[4],
3167 (UV)state, (UV)ST.accepted );
3171 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3172 uscan, len, uvc, charid, foldlen,
3176 (base + charid > trie->uniquecharcount )
3177 && (base + charid - 1 - trie->uniquecharcount
3179 && trie->trans[base + charid - 1 -
3180 trie->uniquecharcount].check == state)
3182 state = trie->trans[base + charid - 1 -
3183 trie->uniquecharcount ].next;
3194 DEBUG_TRIE_EXECUTE_r(
3195 PerlIO_printf( Perl_debug_log,
3196 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3197 charid, uvc, (UV)state, PL_colors[5] );
3204 PerlIO_printf( Perl_debug_log,
3205 "%*s %sgot %"IVdf" possible matches%s\n",
3206 REPORT_CODE_OFF + depth * 2, "",
3207 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3210 goto trie_first_try; /* jump into the fail handler */
3212 case TRIE_next_fail: /* we failed - try next alterative */
3214 REGCP_UNWIND(ST.cp);
3215 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3216 PL_regoffs[n].end = -1;
3217 *PL_reglastparen = n;
3226 ST.lastparen = *PL_reglastparen;
3229 if ( ST.accepted == 1 ) {
3230 /* only one choice left - just continue */
3232 AV *const trie_words
3233 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3234 SV ** const tmp = av_fetch( trie_words,
3235 ST.accept_buff[ 0 ].wordnum-1, 0 );
3236 SV *sv= tmp ? sv_newmortal() : NULL;
3238 PerlIO_printf( Perl_debug_log,
3239 "%*s %sonly one match left: #%d <%s>%s\n",
3240 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3241 ST.accept_buff[ 0 ].wordnum,
3242 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3243 PL_colors[0], PL_colors[1],
3244 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3246 : "not compiled under -Dr",
3249 PL_reginput = (char *)ST.accept_buff[ 0 ].endpos;
3250 /* in this case we free tmps/leave before we call regmatch
3251 as we wont be using accept_buff again. */
3253 locinput = PL_reginput;
3254 nextchr = UCHARAT(locinput);
3255 if ( !ST.jump || !ST.jump[ST.accept_buff[0].wordnum])
3258 scan = ST.me + ST.jump[ST.accept_buff[0].wordnum];
3259 if (!has_cutgroup) {
3264 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3267 continue; /* execute rest of RE */
3270 if ( !ST.accepted-- ) {
3272 PerlIO_printf( Perl_debug_log,
3273 "%*s %sTRIE failed...%s\n",
3274 REPORT_CODE_OFF+depth*2, "",
3285 There are at least two accepting states left. Presumably
3286 the number of accepting states is going to be low,
3287 typically two. So we simply scan through to find the one
3288 with lowest wordnum. Once we find it, we swap the last
3289 state into its place and decrement the size. We then try to
3290 match the rest of the pattern at the point where the word
3291 ends. If we succeed, control just continues along the
3292 regex; if we fail we return here to try the next accepting
3299 for( cur = 1 ; cur <= ST.accepted ; cur++ ) {
3300 DEBUG_TRIE_EXECUTE_r(
3301 PerlIO_printf( Perl_debug_log,
3302 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
3303 REPORT_CODE_OFF + depth * 2, "", PL_colors[4],
3304 (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur,
3305 ST.accept_buff[ cur ].wordnum, PL_colors[5] );
3308 if (ST.accept_buff[cur].wordnum <
3309 ST.accept_buff[best].wordnum)
3314 AV *const trie_words
3315 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3316 SV ** const tmp = av_fetch( trie_words,
3317 ST.accept_buff[ best ].wordnum - 1, 0 );
3318 regnode *nextop=(!ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) ?
3320 ST.me + ST.jump[ST.accept_buff[best].wordnum];
3321 SV *sv= tmp ? sv_newmortal() : NULL;
3323 PerlIO_printf( Perl_debug_log,
3324 "%*s %strying alternation #%d <%s> at node #%d %s\n",
3325 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3326 ST.accept_buff[best].wordnum,
3327 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3328 PL_colors[0], PL_colors[1],
3329 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3330 ) : "not compiled under -Dr",
3331 REG_NODE_NUM(nextop),
3335 if ( best<ST.accepted ) {
3336 reg_trie_accepted tmp = ST.accept_buff[ best ];
3337 ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ];
3338 ST.accept_buff[ ST.accepted ] = tmp;
3341 PL_reginput = (char *)ST.accept_buff[ best ].endpos;
3342 if ( !ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) {
3345 scan = ST.me + ST.jump[ST.accept_buff[best].wordnum];
3347 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3352 /* we dont want to throw this away, see bug 57042*/
3353 if (oreplsv != GvSV(PL_replgv))
3354 sv_setsv(oreplsv, GvSV(PL_replgv));
3361 char *s = STRING(scan);
3363 if (do_utf8 != UTF) {
3364 /* The target and the pattern have differing utf8ness. */
3366 const char * const e = s + ln;
3369 /* The target is utf8, the pattern is not utf8. */
3374 if (NATIVE_TO_UNI(*(U8*)s) !=
3375 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3383 /* The target is not utf8, the pattern is utf8. */
3388 if (NATIVE_TO_UNI(*((U8*)l)) !=
3389 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3397 nextchr = UCHARAT(locinput);
3400 /* The target and the pattern have the same utf8ness. */
3401 /* Inline the first character, for speed. */
3402 if (UCHARAT(s) != nextchr)
3404 if (PL_regeol - locinput < ln)
3406 if (ln > 1 && memNE(s, locinput, ln))
3409 nextchr = UCHARAT(locinput);
3413 PL_reg_flags |= RF_tainted;
3416 char * const s = STRING(scan);
3419 if (do_utf8 || UTF) {
3420 /* Either target or the pattern are utf8. */
3421 const char * const l = locinput;
3422 char *e = PL_regeol;
3424 if (ibcmp_utf8(s, 0, ln, cBOOL(UTF),
3425 l, &e, 0, do_utf8)) {
3426 /* One more case for the sharp s:
3427 * pack("U0U*", 0xDF) =~ /ss/i,
3428 * the 0xC3 0x9F are the UTF-8
3429 * byte sequence for the U+00DF. */
3432 toLOWER(s[0]) == 's' &&
3434 toLOWER(s[1]) == 's' &&
3441 nextchr = UCHARAT(locinput);
3445 /* Neither the target and the pattern are utf8. */
3447 /* Inline the first character, for speed. */
3448 if (UCHARAT(s) != nextchr &&
3449 UCHARAT(s) != ((OP(scan) == EXACTF)
3450 ? PL_fold : PL_fold_locale)[nextchr])
3452 if (PL_regeol - locinput < ln)
3454 if (ln > 1 && (OP(scan) == EXACTF
3455 ? ibcmp(s, locinput, ln)
3456 : ibcmp_locale(s, locinput, ln)))
3459 nextchr = UCHARAT(locinput);
3464 PL_reg_flags |= RF_tainted;
3468 /* was last char in word? */
3470 if (locinput == PL_bostr)
3473 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3475 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3477 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3478 ln = isALNUM_uni(ln);
3479 LOAD_UTF8_CHARCLASS_ALNUM();
3480 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3483 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3484 n = isALNUM_LC_utf8((U8*)locinput);
3488 ln = (locinput != PL_bostr) ?
3489 UCHARAT(locinput - 1) : '\n';
3490 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3492 n = isALNUM(nextchr);
3495 ln = isALNUM_LC(ln);
3496 n = isALNUM_LC(nextchr);
3499 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3500 OP(scan) == BOUNDL))
3505 STRLEN inclasslen = PL_regeol - locinput;
3507 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3509 if (locinput >= PL_regeol)
3511 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3512 nextchr = UCHARAT(locinput);
3517 nextchr = UCHARAT(locinput);
3518 if (!REGINCLASS(rex, scan, (U8*)locinput))
3520 if (!nextchr && locinput >= PL_regeol)
3522 nextchr = UCHARAT(++locinput);
3526 /* If we might have the case of the German sharp s
3527 * in a casefolding Unicode character class. */
3529 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3530 locinput += SHARP_S_SKIP;
3531 nextchr = UCHARAT(locinput);
3536 /* Special char classes - The defines start on line 129 or so */
3537 CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3538 CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3540 CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3541 CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3543 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3544 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3546 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3547 a Unicode extended Grapheme Cluster */
3548 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3549 extended Grapheme Cluster is:
3552 | Prepend* Begin Extend*
3555 Begin is (Hangul-syllable | ! Control)
3556 Extend is (Grapheme_Extend | Spacing_Mark)
3557 Control is [ GCB_Control CR LF ]
3559 The discussion below shows how the code for CLUMP is derived
3560 from this regex. Note that most of these concepts are from
3561 property values of the Grapheme Cluster Boundary (GCB) property.
3562 No code point can have multiple property values for a given
3563 property. Thus a code point in Prepend can't be in Control, but
3564 it must be in !Control. This is why Control above includes
3565 GCB_Control plus CR plus LF. The latter two are used in the GCB
3566 property separately, and so can't be in GCB_Control, even though
3567 they logically are controls. Control is not the same as gc=cc,
3568 but includes format and other characters as well.
3570 The Unicode definition of Hangul-syllable is:
3572 | (L* ( ( V | LV ) V* | LVT ) T*)
3575 Each of these is a value for the GCB property, and hence must be
3576 disjoint, so the order they are tested is immaterial, so the
3577 above can safely be changed to
3580 | (L* ( LVT | ( V | LV ) V*) T*)
3582 The last two terms can be combined like this:
3584 | (( LVT | ( V | LV ) V*) T*))
3586 And refactored into this:
3587 L* (L | LVT T* | V V* T* | LV V* T*)
3589 That means that if we have seen any L's at all we can quit
3590 there, but if the next character is a LVT, a V or and LV we
3593 There is a subtlety with Prepend* which showed up in testing.
3594 Note that the Begin, and only the Begin is required in:
3595 | Prepend* Begin Extend*
3596 Also, Begin contains '! Control'. A Prepend must be a '!
3597 Control', which means it must be a Begin. What it comes down to
3598 is that if we match Prepend* and then find no suitable Begin
3599 afterwards, that if we backtrack the last Prepend, that one will
3600 be a suitable Begin.
3603 if (locinput >= PL_regeol)
3607 /* Match either CR LF or '.', as all the other possibilities
3609 locinput++; /* Match the . or CR */
3611 && locinput < PL_regeol
3612 && UCHARAT(locinput) == '\n') locinput++;
3616 /* Utf8: See if is ( CR LF ); already know that locinput <
3617 * PL_regeol, so locinput+1 is in bounds */
3618 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3622 /* In case have to backtrack to beginning, then match '.' */
3623 char *starting = locinput;
3625 /* In case have to backtrack the last prepend */
3626 char *previous_prepend = 0;
3628 LOAD_UTF8_CHARCLASS_GCB();
3630 /* Match (prepend)* */
3631 while (locinput < PL_regeol
3632 && swash_fetch(PL_utf8_X_prepend,
3633 (U8*)locinput, do_utf8))
3635 previous_prepend = locinput;
3636 locinput += UTF8SKIP(locinput);
3639 /* As noted above, if we matched a prepend character, but
3640 * the next thing won't match, back off the last prepend we
3641 * matched, as it is guaranteed to match the begin */
3642 if (previous_prepend
3643 && (locinput >= PL_regeol
3644 || ! swash_fetch(PL_utf8_X_begin,
3645 (U8*)locinput, do_utf8)))
3647 locinput = previous_prepend;
3650 /* Note that here we know PL_regeol > locinput, as we
3651 * tested that upon input to this switch case, and if we
3652 * moved locinput forward, we tested the result just above
3653 * and it either passed, or we backed off so that it will
3655 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, do_utf8)) {
3657 /* Here did not match the required 'Begin' in the
3658 * second term. So just match the very first
3659 * character, the '.' of the final term of the regex */
3660 locinput = starting + UTF8SKIP(starting);
3663 /* Here is the beginning of a character that can have
3664 * an extender. It is either a hangul syllable, or a
3666 if (swash_fetch(PL_utf8_X_non_hangul,
3667 (U8*)locinput, do_utf8))
3670 /* Here not a Hangul syllable, must be a
3671 * ('! * Control') */
3672 locinput += UTF8SKIP(locinput);
3675 /* Here is a Hangul syllable. It can be composed
3676 * of several individual characters. One
3677 * possibility is T+ */
3678 if (swash_fetch(PL_utf8_X_T,
3679 (U8*)locinput, do_utf8))
3681 while (locinput < PL_regeol
3682 && swash_fetch(PL_utf8_X_T,
3683 (U8*)locinput, do_utf8))
3685 locinput += UTF8SKIP(locinput);
3689 /* Here, not T+, but is a Hangul. That means
3690 * it is one of the others: L, LV, LVT or V,
3692 * L* (L | LVT T* | V V* T* | LV V* T*) */
3695 while (locinput < PL_regeol
3696 && swash_fetch(PL_utf8_X_L,
3697 (U8*)locinput, do_utf8))
3699 locinput += UTF8SKIP(locinput);
3702 /* Here, have exhausted L*. If the next
3703 * character is not an LV, LVT nor V, it means
3704 * we had to have at least one L, so matches L+
3705 * in the original equation, we have a complete
3706 * hangul syllable. Are done. */
3708 if (locinput < PL_regeol
3709 && swash_fetch(PL_utf8_X_LV_LVT_V,
3710 (U8*)locinput, do_utf8))
3713 /* Otherwise keep going. Must be LV, LVT
3714 * or V. See if LVT */
3715 if (swash_fetch(PL_utf8_X_LVT,
3716 (U8*)locinput, do_utf8))
3718 locinput += UTF8SKIP(locinput);
3721 /* Must be V or LV. Take it, then
3723 locinput += UTF8SKIP(locinput);
3724 while (locinput < PL_regeol
3725 && swash_fetch(PL_utf8_X_V,
3726 (U8*)locinput, do_utf8))
3728 locinput += UTF8SKIP(locinput);
3732 /* And any of LV, LVT, or V can be followed
3734 while (locinput < PL_regeol
3735 && swash_fetch(PL_utf8_X_T,
3739 locinput += UTF8SKIP(locinput);
3745 /* Match any extender */
3746 while (locinput < PL_regeol
3747 && swash_fetch(PL_utf8_X_extend,
3748 (U8*)locinput, do_utf8))
3750 locinput += UTF8SKIP(locinput);
3754 if (locinput > PL_regeol) sayNO;
3756 nextchr = UCHARAT(locinput);
3763 PL_reg_flags |= RF_tainted;
3768 n = reg_check_named_buff_matched(rex,scan);
3771 type = REF + ( type - NREF );
3778 PL_reg_flags |= RF_tainted;
3782 n = ARG(scan); /* which paren pair */
3785 ln = PL_regoffs[n].start;
3786 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3787 if (*PL_reglastparen < n || ln == -1)
3788 sayNO; /* Do not match unless seen CLOSEn. */
3789 if (ln == PL_regoffs[n].end)
3793 if (do_utf8 && type != REF) { /* REF can do byte comparison */
3795 const char *e = PL_bostr + PL_regoffs[n].end;
3797 * Note that we can't do the "other character" lookup trick as
3798 * in the 8-bit case (no pun intended) because in Unicode we
3799 * have to map both upper and title case to lower case.
3803 STRLEN ulen1, ulen2;
3804 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3805 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3809 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3810 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3811 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3818 nextchr = UCHARAT(locinput);
3822 /* Inline the first character, for speed. */
3823 if (UCHARAT(s) != nextchr &&
3825 (UCHARAT(s) != (type == REFF
3826 ? PL_fold : PL_fold_locale)[nextchr])))
3828 ln = PL_regoffs[n].end - ln;
3829 if (locinput + ln > PL_regeol)
3831 if (ln > 1 && (type == REF
3832 ? memNE(s, locinput, ln)
3834 ? ibcmp(s, locinput, ln)
3835 : ibcmp_locale(s, locinput, ln))))
3838 nextchr = UCHARAT(locinput);
3848 #define ST st->u.eval
3853 regexp_internal *rei;
3854 regnode *startpoint;
3857 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3858 if (cur_eval && cur_eval->locinput==locinput) {
3859 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3860 Perl_croak(aTHX_ "Infinite recursion in regex");
3861 if ( ++nochange_depth > max_nochange_depth )
3863 "Pattern subroutine nesting without pos change"
3864 " exceeded limit in regex");
3871 (void)ReREFCNT_inc(rex_sv);
3872 if (OP(scan)==GOSUB) {
3873 startpoint = scan + ARG2L(scan);
3874 ST.close_paren = ARG(scan);
3876 startpoint = rei->program+1;
3879 goto eval_recurse_doit;
3881 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3882 if (cur_eval && cur_eval->locinput==locinput) {
3883 if ( ++nochange_depth > max_nochange_depth )
3884 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3889 /* execute the code in the {...} */
3891 SV ** const before = SP;
3892 OP_4tree * const oop = PL_op;
3893 COP * const ocurcop = PL_curcop;
3895 char *saved_regeol = PL_regeol;
3898 PL_op = (OP_4tree*)rexi->data->data[n];
3899 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3900 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3901 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3902 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3905 SV *sv_mrk = get_sv("REGMARK", 1);
3906 sv_setsv(sv_mrk, sv_yes_mark);
3909 CALLRUNOPS(aTHX); /* Scalar context. */
3912 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3919 PAD_RESTORE_LOCAL(old_comppad);
3920 PL_curcop = ocurcop;
3921 PL_regeol = saved_regeol;
3924 sv_setsv(save_scalar(PL_replgv), ret);
3928 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
3931 /* extract RE object from returned value; compiling if
3937 SV *const sv = SvRV(ret);
3939 if (SvTYPE(sv) == SVt_REGEXP) {
3941 } else if (SvSMAGICAL(sv)) {
3942 mg = mg_find(sv, PERL_MAGIC_qr);
3945 } else if (SvTYPE(ret) == SVt_REGEXP) {
3947 } else if (SvSMAGICAL(ret)) {
3948 if (SvGMAGICAL(ret)) {
3949 /* I don't believe that there is ever qr magic
3951 assert(!mg_find(ret, PERL_MAGIC_qr));
3952 sv_unmagic(ret, PERL_MAGIC_qr);
3955 mg = mg_find(ret, PERL_MAGIC_qr);
3956 /* testing suggests mg only ends up non-NULL for
3957 scalars who were upgraded and compiled in the
3958 else block below. In turn, this is only
3959 triggered in the "postponed utf8 string" tests
3965 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
3969 rx = reg_temp_copy(NULL, rx);
3973 const I32 osize = PL_regsize;
3976 assert (SvUTF8(ret));
3977 } else if (SvUTF8(ret)) {
3978 /* Not doing UTF-8, despite what the SV says. Is
3979 this only if we're trapped in use 'bytes'? */
3980 /* Make a copy of the octet sequence, but without
3981 the flag on, as the compiler now honours the
3982 SvUTF8 flag on ret. */
3984 const char *const p = SvPV(ret, len);
3985 ret = newSVpvn_flags(p, len, SVs_TEMP);
3987 rx = CALLREGCOMP(ret, pm_flags);
3989 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3991 /* This isn't a first class regexp. Instead, it's
3992 caching a regexp onto an existing, Perl visible
3994 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
3999 re = (struct regexp *)SvANY(rx);
4001 RXp_MATCH_COPIED_off(re);
4002 re->subbeg = rex->subbeg;
4003 re->sublen = rex->sublen;
4006 debug_start_match(re_sv, do_utf8, locinput, PL_regeol,
4007 "Matching embedded");
4009 startpoint = rei->program + 1;
4010 ST.close_paren = 0; /* only used for GOSUB */
4011 /* borrowed from regtry */
4012 if (PL_reg_start_tmpl <= re->nparens) {
4013 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4014 if(PL_reg_start_tmp)
4015 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4017 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4020 eval_recurse_doit: /* Share code with GOSUB below this line */
4021 /* run the pattern returned from (??{...}) */
4022 ST.cp = regcppush(0); /* Save *all* the positions. */
4023 REGCP_SET(ST.lastcp);
4025 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
4027 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4028 PL_reglastparen = &re->lastparen;
4029 PL_reglastcloseparen = &re->lastcloseparen;
4031 re->lastcloseparen = 0;
4033 PL_reginput = locinput;
4036 /* XXXX This is too dramatic a measure... */
4039 ST.toggle_reg_flags = PL_reg_flags;
4041 PL_reg_flags |= RF_utf8;
4043 PL_reg_flags &= ~RF_utf8;
4044 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4046 ST.prev_rex = rex_sv;
4047 ST.prev_curlyx = cur_curlyx;
4048 SETREX(rex_sv,re_sv);
4053 ST.prev_eval = cur_eval;
4055 /* now continue from first node in postoned RE */
4056 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4059 /* logical is 1, /(?(?{...})X|Y)/ */
4060 sw = cBOOL(SvTRUE(ret));
4065 case EVAL_AB: /* cleanup after a successful (??{A})B */
4066 /* note: this is called twice; first after popping B, then A */
4067 PL_reg_flags ^= ST.toggle_reg_flags;
4068 ReREFCNT_dec(rex_sv);
4069 SETREX(rex_sv,ST.prev_rex);
4070 rex = (struct regexp *)SvANY(rex_sv);
4071 rexi = RXi_GET(rex);
4073 cur_eval = ST.prev_eval;
4074 cur_curlyx = ST.prev_curlyx;
4076 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4077 PL_reglastparen = &rex->lastparen;
4078 PL_reglastcloseparen = &rex->lastcloseparen;
4079 /* also update PL_regoffs */
4080 PL_regoffs = rex->offs;
4082 /* XXXX This is too dramatic a measure... */
4084 if ( nochange_depth )
4089 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4090 /* note: this is called twice; first after popping B, then A */
4091 PL_reg_flags ^= ST.toggle_reg_flags;
4092 ReREFCNT_dec(rex_sv);
4093 SETREX(rex_sv,ST.prev_rex);
4094 rex = (struct regexp *)SvANY(rex_sv);
4095 rexi = RXi_GET(rex);
4096 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4097 PL_reglastparen = &rex->lastparen;
4098 PL_reglastcloseparen = &rex->lastcloseparen;
4100 PL_reginput = locinput;
4101 REGCP_UNWIND(ST.lastcp);
4103 cur_eval = ST.prev_eval;
4104 cur_curlyx = ST.prev_curlyx;
4105 /* XXXX This is too dramatic a measure... */
4107 if ( nochange_depth )
4113 n = ARG(scan); /* which paren pair */
4114 PL_reg_start_tmp[n] = locinput;
4120 n = ARG(scan); /* which paren pair */
4121 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4122 PL_regoffs[n].end = locinput - PL_bostr;
4123 /*if (n > PL_regsize)
4125 if (n > *PL_reglastparen)
4126 *PL_reglastparen = n;
4127 *PL_reglastcloseparen = n;
4128 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4136 cursor && OP(cursor)!=END;
4137 cursor=regnext(cursor))
4139 if ( OP(cursor)==CLOSE ){
4141 if ( n <= lastopen ) {
4143 = PL_reg_start_tmp[n] - PL_bostr;
4144 PL_regoffs[n].end = locinput - PL_bostr;
4145 /*if (n > PL_regsize)
4147 if (n > *PL_reglastparen)
4148 *PL_reglastparen = n;
4149 *PL_reglastcloseparen = n;
4150 if ( n == ARG(scan) || (cur_eval &&
4151 cur_eval->u.eval.close_paren == n))
4160 n = ARG(scan); /* which paren pair */
4161 sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
4164 /* reg_check_named_buff_matched returns 0 for no match */
4165 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
4169 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4175 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4177 next = NEXTOPER(NEXTOPER(scan));
4179 next = scan + ARG(scan);
4180 if (OP(next) == IFTHEN) /* Fake one. */
4181 next = NEXTOPER(NEXTOPER(next));
4185 logical = scan->flags;
4188 /*******************************************************************
4190 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4191 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4192 STAR/PLUS/CURLY/CURLYN are used instead.)
4194 A*B is compiled as <CURLYX><A><WHILEM><B>
4196 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4197 state, which contains the current count, initialised to -1. It also sets
4198 cur_curlyx to point to this state, with any previous value saved in the
4201 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4202 since the pattern may possibly match zero times (i.e. it's a while {} loop
4203 rather than a do {} while loop).
4205 Each entry to WHILEM represents a successful match of A. The count in the
4206 CURLYX block is incremented, another WHILEM state is pushed, and execution
4207 passes to A or B depending on greediness and the current count.
4209 For example, if matching against the string a1a2a3b (where the aN are
4210 substrings that match /A/), then the match progresses as follows: (the
4211 pushed states are interspersed with the bits of strings matched so far):
4214 <CURLYX cnt=0><WHILEM>
4215 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4216 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4217 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4218 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4220 (Contrast this with something like CURLYM, which maintains only a single
4224 a1 <CURLYM cnt=1> a2
4225 a1 a2 <CURLYM cnt=2> a3
4226 a1 a2 a3 <CURLYM cnt=3> b
4229 Each WHILEM state block marks a point to backtrack to upon partial failure
4230 of A or B, and also contains some minor state data related to that
4231 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4232 overall state, such as the count, and pointers to the A and B ops.
4234 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4235 must always point to the *current* CURLYX block, the rules are:
4237 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4238 and set cur_curlyx to point the new block.
4240 When popping the CURLYX block after a successful or unsuccessful match,
4241 restore the previous cur_curlyx.
4243 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4244 to the outer one saved in the CURLYX block.
4246 When popping the WHILEM block after a successful or unsuccessful B match,
4247 restore the previous cur_curlyx.
4249 Here's an example for the pattern (AI* BI)*BO
4250 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4253 curlyx backtrack stack
4254 ------ ---------------
4256 CO <CO prev=NULL> <WO>
4257 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4258 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4259 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4261 At this point the pattern succeeds, and we work back down the stack to
4262 clean up, restoring as we go:
4264 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4265 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4266 CO <CO prev=NULL> <WO>
4269 *******************************************************************/
4271 #define ST st->u.curlyx
4273 case CURLYX: /* start of /A*B/ (for complex A) */
4275 /* No need to save/restore up to this paren */
4276 I32 parenfloor = scan->flags;
4278 assert(next); /* keep Coverity happy */
4279 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4282 /* XXXX Probably it is better to teach regpush to support
4283 parenfloor > PL_regsize... */
4284 if (parenfloor > (I32)*PL_reglastparen)
4285 parenfloor = *PL_reglastparen; /* Pessimization... */
4287 ST.prev_curlyx= cur_curlyx;
4289 ST.cp = PL_savestack_ix;
4291 /* these fields contain the state of the current curly.
4292 * they are accessed by subsequent WHILEMs */
4293 ST.parenfloor = parenfloor;
4294 ST.min = ARG1(scan);
4295 ST.max = ARG2(scan);
4296 ST.A = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
4300 ST.count = -1; /* this will be updated by WHILEM */
4301 ST.lastloc = NULL; /* this will be updated by WHILEM */
4303 PL_reginput = locinput;
4304 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4308 case CURLYX_end: /* just finished matching all of A*B */
4309 cur_curlyx = ST.prev_curlyx;
4313 case CURLYX_end_fail: /* just failed to match all of A*B */
4315 cur_curlyx = ST.prev_curlyx;
4321 #define ST st->u.whilem
4323 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4325 /* see the discussion above about CURLYX/WHILEM */
4327 assert(cur_curlyx); /* keep Coverity happy */
4328 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4329 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4330 ST.cache_offset = 0;
4333 PL_reginput = locinput;
4335 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4336 "%*s whilem: matched %ld out of %ld..%ld\n",
4337 REPORT_CODE_OFF+depth*2, "", (long)n,
4338 (long)cur_curlyx->u.curlyx.min,
4339 (long)cur_curlyx->u.curlyx.max)
4342 /* First just match a string of min A's. */
4344 if (n < cur_curlyx->u.curlyx.min) {
4345 cur_curlyx->u.curlyx.lastloc = locinput;
4346 PUSH_STATE_GOTO(WHILEM_A_pre, cur_curlyx->u.curlyx.A);
4350 /* If degenerate A matches "", assume A done. */
4352 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4353 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4354 "%*s whilem: empty match detected, trying continuation...\n",
4355 REPORT_CODE_OFF+depth*2, "")
4357 goto do_whilem_B_max;
4360 /* super-linear cache processing */
4364 if (!PL_reg_maxiter) {
4365 /* start the countdown: Postpone detection until we
4366 * know the match is not *that* much linear. */
4367 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4368 /* possible overflow for long strings and many CURLYX's */
4369 if (PL_reg_maxiter < 0)
4370 PL_reg_maxiter = I32_MAX;
4371 PL_reg_leftiter = PL_reg_maxiter;
4374 if (PL_reg_leftiter-- == 0) {
4375 /* initialise cache */
4376 const I32 size = (PL_reg_maxiter + 7)/8;
4377 if (PL_reg_poscache) {
4378 if ((I32)PL_reg_poscache_size < size) {
4379 Renew(PL_reg_poscache, size, char);
4380 PL_reg_poscache_size = size;
4382 Zero(PL_reg_poscache, size, char);
4385 PL_reg_poscache_size = size;
4386 Newxz(PL_reg_poscache, size, char);
4388 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4389 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4390 PL_colors[4], PL_colors[5])
4394 if (PL_reg_leftiter < 0) {
4395 /* have we already failed at this position? */
4397 offset = (scan->flags & 0xf) - 1
4398 + (locinput - PL_bostr) * (scan->flags>>4);
4399 mask = 1 << (offset % 8);
4401 if (PL_reg_poscache[offset] & mask) {
4402 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4403 "%*s whilem: (cache) already tried at this position...\n",
4404 REPORT_CODE_OFF+depth*2, "")
4406 sayNO; /* cache records failure */
4408 ST.cache_offset = offset;
4409 ST.cache_mask = mask;
4413 /* Prefer B over A for minimal matching. */
4415 if (cur_curlyx->u.curlyx.minmod) {
4416 ST.save_curlyx = cur_curlyx;
4417 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4418 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4419 REGCP_SET(ST.lastcp);
4420 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4424 /* Prefer A over B for maximal matching. */
4426 if (n < cur_curlyx->u.curlyx.max) { /* More greed allowed? */
4427 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4428 cur_curlyx->u.curlyx.lastloc = locinput;
4429 REGCP_SET(ST.lastcp);
4430 PUSH_STATE_GOTO(WHILEM_A_max, cur_curlyx->u.curlyx.A);
4433 goto do_whilem_B_max;
4437 case WHILEM_B_min: /* just matched B in a minimal match */
4438 case WHILEM_B_max: /* just matched B in a maximal match */
4439 cur_curlyx = ST.save_curlyx;
4443 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4444 cur_curlyx = ST.save_curlyx;
4445 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4446 cur_curlyx->u.curlyx.count--;
4450 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4451 REGCP_UNWIND(ST.lastcp);
4454 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4455 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4456 cur_curlyx->u.curlyx.count--;
4460 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4461 REGCP_UNWIND(ST.lastcp);
4462 regcppop(rex); /* Restore some previous $<digit>s? */
4463 PL_reginput = locinput;
4464 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4465 "%*s whilem: failed, trying continuation...\n",
4466 REPORT_CODE_OFF+depth*2, "")
4469 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4470 && ckWARN(WARN_REGEXP)
4471 && !(PL_reg_flags & RF_warned))
4473 PL_reg_flags |= RF_warned;
4474 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4475 "Complex regular subexpression recursion",
4480 ST.save_curlyx = cur_curlyx;
4481 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4482 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4485 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4486 cur_curlyx = ST.save_curlyx;
4487 REGCP_UNWIND(ST.lastcp);
4490 if (cur_curlyx->u.curlyx.count >= cur_curlyx->u.curlyx.max) {
4491 /* Maximum greed exceeded */
4492 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4493 && ckWARN(WARN_REGEXP)
4494 && !(PL_reg_flags & RF_warned))
4496 PL_reg_flags |= RF_warned;
4497 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4498 "%s limit (%d) exceeded",
4499 "Complex regular subexpression recursion",
4502 cur_curlyx->u.curlyx.count--;
4506 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4507 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4509 /* Try grabbing another A and see if it helps. */
4510 PL_reginput = locinput;
4511 cur_curlyx->u.curlyx.lastloc = locinput;
4512 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4513 REGCP_SET(ST.lastcp);
4514 PUSH_STATE_GOTO(WHILEM_A_min, ST.save_curlyx->u.curlyx.A);
4518 #define ST st->u.branch
4520 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4521 next = scan + ARG(scan);
4524 scan = NEXTOPER(scan);
4527 case BRANCH: /* /(...|A|...)/ */
4528 scan = NEXTOPER(scan); /* scan now points to inner node */
4529 ST.lastparen = *PL_reglastparen;
4530 ST.next_branch = next;
4532 PL_reginput = locinput;
4534 /* Now go into the branch */
4536 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4538 PUSH_STATE_GOTO(BRANCH_next, scan);
4542 PL_reginput = locinput;
4543 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4544 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4545 PUSH_STATE_GOTO(CUTGROUP_next,next);
4547 case CUTGROUP_next_fail:
4550 if (st->u.mark.mark_name)
4551 sv_commit = st->u.mark.mark_name;
4557 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4562 REGCP_UNWIND(ST.cp);
4563 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4564 PL_regoffs[n].end = -1;
4565 *PL_reglastparen = n;
4566 /*dmq: *PL_reglastcloseparen = n; */
4567 scan = ST.next_branch;
4568 /* no more branches? */
4569 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4571 PerlIO_printf( Perl_debug_log,
4572 "%*s %sBRANCH failed...%s\n",
4573 REPORT_CODE_OFF+depth*2, "",
4579 continue; /* execute next BRANCH[J] op */
4587 #define ST st->u.curlym
4589 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4591 /* This is an optimisation of CURLYX that enables us to push
4592 * only a single backtracking state, no matter how many matches
4593 * there are in {m,n}. It relies on the pattern being constant
4594 * length, with no parens to influence future backrefs
4598 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4600 /* if paren positive, emulate an OPEN/CLOSE around A */
4602 U32 paren = ST.me->flags;
4603 if (paren > PL_regsize)
4605 if (paren > *PL_reglastparen)
4606 *PL_reglastparen = paren;
4607 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4615 ST.c1 = CHRTEST_UNINIT;
4618 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4621 curlym_do_A: /* execute the A in /A{m,n}B/ */
4622 PL_reginput = locinput;
4623 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4626 case CURLYM_A: /* we've just matched an A */
4627 locinput = st->locinput;
4628 nextchr = UCHARAT(locinput);
4631 /* after first match, determine A's length: u.curlym.alen */
4632 if (ST.count == 1) {
4633 if (PL_reg_match_utf8) {
4635 while (s < PL_reginput) {
4641 ST.alen = PL_reginput - locinput;
4644 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4647 PerlIO_printf(Perl_debug_log,
4648 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4649 (int)(REPORT_CODE_OFF+(depth*2)), "",
4650 (IV) ST.count, (IV)ST.alen)
4653 locinput = PL_reginput;
4655 if (cur_eval && cur_eval->u.eval.close_paren &&
4656 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4660 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4661 if ( max == REG_INFTY || ST.count < max )
4662 goto curlym_do_A; /* try to match another A */
4664 goto curlym_do_B; /* try to match B */
4666 case CURLYM_A_fail: /* just failed to match an A */
4667 REGCP_UNWIND(ST.cp);
4669 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4670 || (cur_eval && cur_eval->u.eval.close_paren &&
4671 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4674 curlym_do_B: /* execute the B in /A{m,n}B/ */
4675 PL_reginput = locinput;
4676 if (ST.c1 == CHRTEST_UNINIT) {
4677 /* calculate c1 and c2 for possible match of 1st char
4678 * following curly */
4679 ST.c1 = ST.c2 = CHRTEST_VOID;
4680 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4681 regnode *text_node = ST.B;
4682 if (! HAS_TEXT(text_node))
4683 FIND_NEXT_IMPT(text_node);
4686 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4688 But the former is redundant in light of the latter.
4690 if this changes back then the macro for
4691 IS_TEXT and friends need to change.
4693 if (PL_regkind[OP(text_node)] == EXACT)
4696 ST.c1 = (U8)*STRING(text_node);
4698 (IS_TEXTF(text_node))
4700 : (IS_TEXTFL(text_node))
4701 ? PL_fold_locale[ST.c1]
4708 PerlIO_printf(Perl_debug_log,
4709 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4710 (int)(REPORT_CODE_OFF+(depth*2)),
4713 if (ST.c1 != CHRTEST_VOID
4714 && UCHARAT(PL_reginput) != ST.c1
4715 && UCHARAT(PL_reginput) != ST.c2)
4717 /* simulate B failing */
4719 PerlIO_printf(Perl_debug_log,
4720 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4721 (int)(REPORT_CODE_OFF+(depth*2)),"",
4724 state_num = CURLYM_B_fail;
4725 goto reenter_switch;
4729 /* mark current A as captured */
4730 I32 paren = ST.me->flags;
4732 PL_regoffs[paren].start
4733 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4734 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4735 /*dmq: *PL_reglastcloseparen = paren; */
4738 PL_regoffs[paren].end = -1;
4739 if (cur_eval && cur_eval->u.eval.close_paren &&
4740 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4749 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4752 case CURLYM_B_fail: /* just failed to match a B */
4753 REGCP_UNWIND(ST.cp);
4755 I32 max = ARG2(ST.me);
4756 if (max != REG_INFTY && ST.count == max)
4758 goto curlym_do_A; /* try to match a further A */
4760 /* backtrack one A */
4761 if (ST.count == ARG1(ST.me) /* min */)
4764 locinput = HOPc(locinput, -ST.alen);
4765 goto curlym_do_B; /* try to match B */
4768 #define ST st->u.curly
4770 #define CURLY_SETPAREN(paren, success) \
4773 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4774 PL_regoffs[paren].end = locinput - PL_bostr; \
4775 *PL_reglastcloseparen = paren; \
4778 PL_regoffs[paren].end = -1; \
4781 case STAR: /* /A*B/ where A is width 1 */
4785 scan = NEXTOPER(scan);
4787 case PLUS: /* /A+B/ where A is width 1 */
4791 scan = NEXTOPER(scan);
4793 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4794 ST.paren = scan->flags; /* Which paren to set */
4795 if (ST.paren > PL_regsize)
4796 PL_regsize = ST.paren;
4797 if (ST.paren > *PL_reglastparen)
4798 *PL_reglastparen = ST.paren;
4799 ST.min = ARG1(scan); /* min to match */
4800 ST.max = ARG2(scan); /* max to match */
4801 if (cur_eval && cur_eval->u.eval.close_paren &&
4802 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4806 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4808 case CURLY: /* /A{m,n}B/ where A is width 1 */
4810 ST.min = ARG1(scan); /* min to match */
4811 ST.max = ARG2(scan); /* max to match */
4812 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4815 * Lookahead to avoid useless match attempts
4816 * when we know what character comes next.
4818 * Used to only do .*x and .*?x, but now it allows
4819 * for )'s, ('s and (?{ ... })'s to be in the way
4820 * of the quantifier and the EXACT-like node. -- japhy
4823 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4825 if (HAS_TEXT(next) || JUMPABLE(next)) {
4827 regnode *text_node = next;
4829 if (! HAS_TEXT(text_node))
4830 FIND_NEXT_IMPT(text_node);
4832 if (! HAS_TEXT(text_node))
4833 ST.c1 = ST.c2 = CHRTEST_VOID;
4835 if ( PL_regkind[OP(text_node)] != EXACT ) {
4836 ST.c1 = ST.c2 = CHRTEST_VOID;
4837 goto assume_ok_easy;
4840 s = (U8*)STRING(text_node);
4842 /* Currently we only get here when
4844 PL_rekind[OP(text_node)] == EXACT
4846 if this changes back then the macro for IS_TEXT and
4847 friends need to change. */
4850 if (IS_TEXTF(text_node))
4851 ST.c2 = PL_fold[ST.c1];
4852 else if (IS_TEXTFL(text_node))
4853 ST.c2 = PL_fold_locale[ST.c1];
4856 if (IS_TEXTF(text_node)) {
4857 STRLEN ulen1, ulen2;
4858 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4859 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4861 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4862 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4864 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4866 0 : UTF8_ALLOW_ANY);
4867 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4869 0 : UTF8_ALLOW_ANY);
4871 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4873 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4878 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4885 ST.c1 = ST.c2 = CHRTEST_VOID;
4890 PL_reginput = locinput;
4893 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4896 locinput = PL_reginput;
4898 if (ST.c1 == CHRTEST_VOID)
4899 goto curly_try_B_min;
4901 ST.oldloc = locinput;
4903 /* set ST.maxpos to the furthest point along the
4904 * string that could possibly match */
4905 if (ST.max == REG_INFTY) {
4906 ST.maxpos = PL_regeol - 1;
4908 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4912 int m = ST.max - ST.min;
4913 for (ST.maxpos = locinput;
4914 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4915 ST.maxpos += UTF8SKIP(ST.maxpos);
4918 ST.maxpos = locinput + ST.max - ST.min;
4919 if (ST.maxpos >= PL_regeol)
4920 ST.maxpos = PL_regeol - 1;
4922 goto curly_try_B_min_known;
4926 ST.count = regrepeat(rex, ST.A, ST.max, depth);
4927 locinput = PL_reginput;
4928 if (ST.count < ST.min)
4930 if ((ST.count > ST.min)
4931 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4933 /* A{m,n} must come at the end of the string, there's
4934 * no point in backing off ... */
4936 /* ...except that $ and \Z can match before *and* after
4937 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4938 We may back off by one in this case. */
4939 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4943 goto curly_try_B_max;
4948 case CURLY_B_min_known_fail:
4949 /* failed to find B in a non-greedy match where c1,c2 valid */
4950 if (ST.paren && ST.count)
4951 PL_regoffs[ST.paren].end = -1;
4953 PL_reginput = locinput; /* Could be reset... */
4954 REGCP_UNWIND(ST.cp);
4955 /* Couldn't or didn't -- move forward. */
4956 ST.oldloc = locinput;
4958 locinput += UTF8SKIP(locinput);
4962 curly_try_B_min_known:
4963 /* find the next place where 'B' could work, then call B */
4967 n = (ST.oldloc == locinput) ? 0 : 1;
4968 if (ST.c1 == ST.c2) {
4970 /* set n to utf8_distance(oldloc, locinput) */
4971 while (locinput <= ST.maxpos &&
4972 utf8n_to_uvchr((U8*)locinput,
4973 UTF8_MAXBYTES, &len,
4974 uniflags) != (UV)ST.c1) {
4980 /* set n to utf8_distance(oldloc, locinput) */
4981 while (locinput <= ST.maxpos) {
4983 const UV c = utf8n_to_uvchr((U8*)locinput,
4984 UTF8_MAXBYTES, &len,
4986 if (c == (UV)ST.c1 || c == (UV)ST.c2)
4994 if (ST.c1 == ST.c2) {
4995 while (locinput <= ST.maxpos &&
4996 UCHARAT(locinput) != ST.c1)
5000 while (locinput <= ST.maxpos
5001 && UCHARAT(locinput) != ST.c1
5002 && UCHARAT(locinput) != ST.c2)
5005 n = locinput - ST.oldloc;
5007 if (locinput > ST.maxpos)
5009 /* PL_reginput == oldloc now */
5012 if (regrepeat(rex, ST.A, n, depth) < n)
5015 PL_reginput = locinput;
5016 CURLY_SETPAREN(ST.paren, ST.count);
5017 if (cur_eval && cur_eval->u.eval.close_paren &&
5018 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5021 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5026 case CURLY_B_min_fail:
5027 /* failed to find B in a non-greedy match where c1,c2 invalid */
5028 if (ST.paren && ST.count)
5029 PL_regoffs[ST.paren].end = -1;
5031 REGCP_UNWIND(ST.cp);
5032 /* failed -- move forward one */
5033 PL_reginput = locinput;
5034 if (regrepeat(rex, ST.A, 1, depth)) {
5036 locinput = PL_reginput;
5037 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5038 ST.count > 0)) /* count overflow ? */
5041 CURLY_SETPAREN(ST.paren, ST.count);
5042 if (cur_eval && cur_eval->u.eval.close_paren &&
5043 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5046 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5054 /* a successful greedy match: now try to match B */
5055 if (cur_eval && cur_eval->u.eval.close_paren &&
5056 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5061 if (ST.c1 != CHRTEST_VOID)
5062 c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput,
5063 UTF8_MAXBYTES, 0, uniflags)
5064 : (UV) UCHARAT(PL_reginput);
5065 /* If it could work, try it. */
5066 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5067 CURLY_SETPAREN(ST.paren, ST.count);
5068 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5073 case CURLY_B_max_fail:
5074 /* failed to find B in a greedy match */
5075 if (ST.paren && ST.count)
5076 PL_regoffs[ST.paren].end = -1;
5078 REGCP_UNWIND(ST.cp);
5080 if (--ST.count < ST.min)
5082 PL_reginput = locinput = HOPc(locinput, -1);
5083 goto curly_try_B_max;
5090 /* we've just finished A in /(??{A})B/; now continue with B */
5092 st->u.eval.toggle_reg_flags
5093 = cur_eval->u.eval.toggle_reg_flags;
5094 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5096 st->u.eval.prev_rex = rex_sv; /* inner */
5097 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5098 rex = (struct regexp *)SvANY(rex_sv);
5099 rexi = RXi_GET(rex);
5100 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5101 ReREFCNT_inc(rex_sv);
5102 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
5104 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5105 PL_reglastparen = &rex->lastparen;
5106 PL_reglastcloseparen = &rex->lastcloseparen;
5108 REGCP_SET(st->u.eval.lastcp);
5109 PL_reginput = locinput;
5111 /* Restore parens of the outer rex without popping the
5113 tmpix = PL_savestack_ix;
5114 PL_savestack_ix = cur_eval->u.eval.lastcp;
5116 PL_savestack_ix = tmpix;
5118 st->u.eval.prev_eval = cur_eval;
5119 cur_eval = cur_eval->u.eval.prev_eval;
5121 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5122 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5123 if ( nochange_depth )
5126 PUSH_YES_STATE_GOTO(EVAL_AB,
5127 st->u.eval.prev_eval->u.eval.B); /* match B */
5130 if (locinput < reginfo->till) {
5131 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5132 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5134 (long)(locinput - PL_reg_starttry),
5135 (long)(reginfo->till - PL_reg_starttry),
5138 sayNO_SILENT; /* Cannot match: too short. */
5140 PL_reginput = locinput; /* put where regtry can find it */
5141 sayYES; /* Success! */
5143 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5145 PerlIO_printf(Perl_debug_log,
5146 "%*s %ssubpattern success...%s\n",
5147 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5148 PL_reginput = locinput; /* put where regtry can find it */
5149 sayYES; /* Success! */
5152 #define ST st->u.ifmatch
5154 case SUSPEND: /* (?>A) */
5156 PL_reginput = locinput;
5159 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5161 goto ifmatch_trivial_fail_test;
5163 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5165 ifmatch_trivial_fail_test:
5167 char * const s = HOPBACKc(locinput, scan->flags);
5172 sw = 1 - cBOOL(ST.wanted);
5176 next = scan + ARG(scan);
5184 PL_reginput = locinput;
5188 ST.logical = logical;
5189 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5191 /* execute body of (?...A) */
5192 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5195 case IFMATCH_A_fail: /* body of (?...A) failed */
5196 ST.wanted = !ST.wanted;
5199 case IFMATCH_A: /* body of (?...A) succeeded */
5201 sw = cBOOL(ST.wanted);
5203 else if (!ST.wanted)
5206 if (OP(ST.me) == SUSPEND)
5207 locinput = PL_reginput;
5209 locinput = PL_reginput = st->locinput;
5210 nextchr = UCHARAT(locinput);
5212 scan = ST.me + ARG(ST.me);
5215 continue; /* execute B */
5220 next = scan + ARG(scan);
5225 reginfo->cutpoint = PL_regeol;
5228 PL_reginput = locinput;
5230 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5231 PUSH_STATE_GOTO(COMMIT_next,next);
5233 case COMMIT_next_fail:
5240 #define ST st->u.mark
5242 ST.prev_mark = mark_state;
5243 ST.mark_name = sv_commit = sv_yes_mark
5244 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5246 ST.mark_loc = PL_reginput = locinput;
5247 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5249 case MARKPOINT_next:
5250 mark_state = ST.prev_mark;
5253 case MARKPOINT_next_fail:
5254 if (popmark && sv_eq(ST.mark_name,popmark))
5256 if (ST.mark_loc > startpoint)
5257 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5258 popmark = NULL; /* we found our mark */
5259 sv_commit = ST.mark_name;
5262 PerlIO_printf(Perl_debug_log,
5263 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5264 REPORT_CODE_OFF+depth*2, "",
5265 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5268 mark_state = ST.prev_mark;
5269 sv_yes_mark = mark_state ?
5270 mark_state->u.mark.mark_name : NULL;
5274 PL_reginput = locinput;
5276 /* (*SKIP) : if we fail we cut here*/
5277 ST.mark_name = NULL;
5278 ST.mark_loc = locinput;
5279 PUSH_STATE_GOTO(SKIP_next,next);
5281 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5282 otherwise do nothing. Meaning we need to scan
5284 regmatch_state *cur = mark_state;
5285 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5288 if ( sv_eq( cur->u.mark.mark_name,
5291 ST.mark_name = find;
5292 PUSH_STATE_GOTO( SKIP_next, next );
5294 cur = cur->u.mark.prev_mark;
5297 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5299 case SKIP_next_fail:
5301 /* (*CUT:NAME) - Set up to search for the name as we
5302 collapse the stack*/
5303 popmark = ST.mark_name;
5305 /* (*CUT) - No name, we cut here.*/
5306 if (ST.mark_loc > startpoint)
5307 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5308 /* but we set sv_commit to latest mark_name if there
5309 is one so they can test to see how things lead to this
5312 sv_commit=mark_state->u.mark.mark_name;
5320 if ( n == (U32)what_len_TRICKYFOLD(locinput,do_utf8,ln) ) {
5322 } else if ( 0xDF == n && !do_utf8 && !UTF ) {
5325 U8 folded[UTF8_MAXBYTES_CASE+1];
5327 const char * const l = locinput;
5328 char *e = PL_regeol;
5329 to_uni_fold(n, folded, &foldlen);
5331 if (ibcmp_utf8((const char*) folded, 0, foldlen, 1,
5332 l, &e, 0, do_utf8)) {
5337 nextchr = UCHARAT(locinput);
5340 if ((n=is_LNBREAK(locinput,do_utf8))) {
5342 nextchr = UCHARAT(locinput);
5347 #define CASE_CLASS(nAmE) \
5349 if ((n=is_##nAmE(locinput,do_utf8))) { \
5351 nextchr = UCHARAT(locinput); \
5356 if ((n=is_##nAmE(locinput,do_utf8))) { \
5359 locinput += UTF8SKIP(locinput); \
5360 nextchr = UCHARAT(locinput); \
5365 CASE_CLASS(HORIZWS);
5369 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5370 PTR2UV(scan), OP(scan));
5371 Perl_croak(aTHX_ "regexp memory corruption");
5375 /* switch break jumps here */
5376 scan = next; /* prepare to execute the next op and ... */
5377 continue; /* ... jump back to the top, reusing st */
5381 /* push a state that backtracks on success */
5382 st->u.yes.prev_yes_state = yes_state;
5386 /* push a new regex state, then continue at scan */
5388 regmatch_state *newst;
5391 regmatch_state *cur = st;
5392 regmatch_state *curyes = yes_state;
5394 regmatch_slab *slab = PL_regmatch_slab;
5395 for (;curd > -1;cur--,curd--) {
5396 if (cur < SLAB_FIRST(slab)) {
5398 cur = SLAB_LAST(slab);
5400 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5401 REPORT_CODE_OFF + 2 + depth * 2,"",
5402 curd, PL_reg_name[cur->resume_state],
5403 (curyes == cur) ? "yes" : ""
5406 curyes = cur->u.yes.prev_yes_state;
5409 DEBUG_STATE_pp("push")
5412 st->locinput = locinput;
5414 if (newst > SLAB_LAST(PL_regmatch_slab))
5415 newst = S_push_slab(aTHX);
5416 PL_regmatch_state = newst;
5418 locinput = PL_reginput;
5419 nextchr = UCHARAT(locinput);
5427 * We get here only if there's trouble -- normally "case END" is
5428 * the terminating point.
5430 Perl_croak(aTHX_ "corrupted regexp pointers");
5436 /* we have successfully completed a subexpression, but we must now
5437 * pop to the state marked by yes_state and continue from there */
5438 assert(st != yes_state);
5440 while (st != yes_state) {
5442 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5443 PL_regmatch_slab = PL_regmatch_slab->prev;
5444 st = SLAB_LAST(PL_regmatch_slab);
5448 DEBUG_STATE_pp("pop (no final)");
5450 DEBUG_STATE_pp("pop (yes)");
5456 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5457 || yes_state > SLAB_LAST(PL_regmatch_slab))
5459 /* not in this slab, pop slab */
5460 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5461 PL_regmatch_slab = PL_regmatch_slab->prev;
5462 st = SLAB_LAST(PL_regmatch_slab);
5464 depth -= (st - yes_state);
5467 yes_state = st->u.yes.prev_yes_state;
5468 PL_regmatch_state = st;
5471 locinput= st->locinput;
5472 nextchr = UCHARAT(locinput);
5474 state_num = st->resume_state + no_final;
5475 goto reenter_switch;
5478 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5479 PL_colors[4], PL_colors[5]));
5481 if (PL_reg_eval_set) {
5482 /* each successfully executed (?{...}) block does the equivalent of
5483 * local $^R = do {...}
5484 * When popping the save stack, all these locals would be undone;
5485 * bypass this by setting the outermost saved $^R to the latest
5487 if (oreplsv != GvSV(PL_replgv))
5488 sv_setsv(oreplsv, GvSV(PL_replgv));
5495 PerlIO_printf(Perl_debug_log,
5496 "%*s %sfailed...%s\n",
5497 REPORT_CODE_OFF+depth*2, "",
5498 PL_colors[4], PL_colors[5])
5510 /* there's a previous state to backtrack to */
5512 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5513 PL_regmatch_slab = PL_regmatch_slab->prev;
5514 st = SLAB_LAST(PL_regmatch_slab);
5516 PL_regmatch_state = st;
5517 locinput= st->locinput;
5518 nextchr = UCHARAT(locinput);
5520 DEBUG_STATE_pp("pop");
5522 if (yes_state == st)
5523 yes_state = st->u.yes.prev_yes_state;
5525 state_num = st->resume_state + 1; /* failure = success + 1 */
5526 goto reenter_switch;
5531 if (rex->intflags & PREGf_VERBARG_SEEN) {
5532 SV *sv_err = get_sv("REGERROR", 1);
5533 SV *sv_mrk = get_sv("REGMARK", 1);
5535 sv_commit = &PL_sv_no;
5537 sv_yes_mark = &PL_sv_yes;
5540 sv_commit = &PL_sv_yes;
5541 sv_yes_mark = &PL_sv_no;
5543 sv_setsv(sv_err, sv_commit);
5544 sv_setsv(sv_mrk, sv_yes_mark);
5547 /* clean up; in particular, free all slabs above current one */
5548 LEAVE_SCOPE(oldsave);
5554 - regrepeat - repeatedly match something simple, report how many
5557 * [This routine now assumes that it will only match on things of length 1.
5558 * That was true before, but now we assume scan - reginput is the count,
5559 * rather than incrementing count on every character. [Er, except utf8.]]
5562 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5565 register char *scan;
5567 register char *loceol = PL_regeol;
5568 register I32 hardcount = 0;
5569 register bool do_utf8 = PL_reg_match_utf8;
5571 PERL_UNUSED_ARG(depth);
5574 PERL_ARGS_ASSERT_REGREPEAT;
5577 if (max == REG_INFTY)
5579 else if (max < loceol - scan)
5580 loceol = scan + max;
5585 while (scan < loceol && hardcount < max && *scan != '\n') {
5586 scan += UTF8SKIP(scan);
5590 while (scan < loceol && *scan != '\n')
5597 while (scan < loceol && hardcount < max) {
5598 scan += UTF8SKIP(scan);
5608 case EXACT: /* length of string is 1 */
5610 while (scan < loceol && UCHARAT(scan) == c)
5613 case EXACTF: /* length of string is 1 */
5615 while (scan < loceol &&
5616 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5619 case EXACTFL: /* length of string is 1 */
5620 PL_reg_flags |= RF_tainted;
5622 while (scan < loceol &&
5623 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5629 while (hardcount < max && scan < loceol &&
5630 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
5631 scan += UTF8SKIP(scan);
5635 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5642 LOAD_UTF8_CHARCLASS_ALNUM();
5643 while (hardcount < max && scan < loceol &&
5644 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5645 scan += UTF8SKIP(scan);
5649 while (scan < loceol && isALNUM(*scan))
5654 PL_reg_flags |= RF_tainted;
5657 while (hardcount < max && scan < loceol &&
5658 isALNUM_LC_utf8((U8*)scan)) {
5659 scan += UTF8SKIP(scan);
5663 while (scan < loceol && isALNUM_LC(*scan))
5670 LOAD_UTF8_CHARCLASS_ALNUM();
5671 while (hardcount < max && scan < loceol &&
5672 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5673 scan += UTF8SKIP(scan);
5677 while (scan < loceol && !isALNUM(*scan))
5682 PL_reg_flags |= RF_tainted;
5685 while (hardcount < max && scan < loceol &&
5686 !isALNUM_LC_utf8((U8*)scan)) {
5687 scan += UTF8SKIP(scan);
5691 while (scan < loceol && !isALNUM_LC(*scan))
5698 LOAD_UTF8_CHARCLASS_SPACE();
5699 while (hardcount < max && scan < loceol &&
5701 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5702 scan += UTF8SKIP(scan);
5706 while (scan < loceol && isSPACE(*scan))
5711 PL_reg_flags |= RF_tainted;
5714 while (hardcount < max && scan < loceol &&
5715 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5716 scan += UTF8SKIP(scan);
5720 while (scan < loceol && isSPACE_LC(*scan))
5727 LOAD_UTF8_CHARCLASS_SPACE();
5728 while (hardcount < max && scan < loceol &&
5730 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5731 scan += UTF8SKIP(scan);
5735 while (scan < loceol && !isSPACE(*scan))
5740 PL_reg_flags |= RF_tainted;
5743 while (hardcount < max && scan < loceol &&
5744 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5745 scan += UTF8SKIP(scan);
5749 while (scan < loceol && !isSPACE_LC(*scan))
5756 LOAD_UTF8_CHARCLASS_DIGIT();
5757 while (hardcount < max && scan < loceol &&
5758 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5759 scan += UTF8SKIP(scan);
5763 while (scan < loceol && isDIGIT(*scan))
5770 LOAD_UTF8_CHARCLASS_DIGIT();
5771 while (hardcount < max && scan < loceol &&
5772 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5773 scan += UTF8SKIP(scan);
5777 while (scan < loceol && !isDIGIT(*scan))
5783 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5789 LNBREAK can match two latin chars, which is ok,
5790 because we have a null terminated string, but we
5791 have to use hardcount in this situation
5793 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5802 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5807 while (scan < loceol && is_HORIZWS_latin1(scan))
5814 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5815 scan += UTF8SKIP(scan);
5819 while (scan < loceol && !is_HORIZWS_latin1(scan))
5827 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5832 while (scan < loceol && is_VERTWS_latin1(scan))
5840 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5841 scan += UTF8SKIP(scan);
5845 while (scan < loceol && !is_VERTWS_latin1(scan))
5851 default: /* Called on something of 0 width. */
5852 break; /* So match right here or not at all. */
5858 c = scan - PL_reginput;
5862 GET_RE_DEBUG_FLAGS_DECL;
5864 SV * const prop = sv_newmortal();
5865 regprop(prog, prop, p);
5866 PerlIO_printf(Perl_debug_log,
5867 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5868 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5876 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5878 - regclass_swash - prepare the utf8 swash
5882 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5888 RXi_GET_DECL(prog,progi);
5889 const struct reg_data * const data = prog ? progi->data : NULL;
5891 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5893 if (data && data->count) {
5894 const U32 n = ARG(node);
5896 if (data->what[n] == 's') {
5897 SV * const rv = MUTABLE_SV(data->data[n]);
5898 AV * const av = MUTABLE_AV(SvRV(rv));
5899 SV **const ary = AvARRAY(av);
5902 /* See the end of regcomp.c:S_regclass() for
5903 * documentation of these array elements. */
5906 a = SvROK(ary[1]) ? &ary[1] : NULL;
5907 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5911 else if (si && doinit) {
5912 sw = swash_init("utf8", "", si, 1, 0);
5913 (void)av_store(av, 1, sw);
5930 - reginclass - determine if a character falls into a character class
5932 The n is the ANYOF regnode, the p is the target string, lenp
5933 is pointer to the maximum length of how far to go in the p
5934 (if the lenp is zero, UTF8SKIP(p) is used),
5935 do_utf8 tells whether the target string is in UTF-8.
5940 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
5943 const char flags = ANYOF_FLAGS(n);
5949 PERL_ARGS_ASSERT_REGINCLASS;
5951 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
5952 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5953 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
5954 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
5955 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
5956 * UTF8_ALLOW_FFFF */
5957 if (len == (STRLEN)-1)
5958 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
5961 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
5962 if (do_utf8 || (flags & ANYOF_UNICODE)) {
5965 if (do_utf8 && !ANYOF_RUNTIME(n)) {
5966 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
5969 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
5973 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
5981 utf8_p = bytes_to_utf8(p, &len);
5983 if (swash_fetch(sw, utf8_p, 1))
5985 else if (flags & ANYOF_FOLD) {
5986 if (!match && lenp && av) {
5988 for (i = 0; i <= av_len(av); i++) {
5989 SV* const sv = *av_fetch(av, i, FALSE);
5991 const char * const s = SvPV_const(sv, len);
5992 if (len <= plen && memEQ(s, (char*)utf8_p, len)) {
6000 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
6003 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
6004 if (swash_fetch(sw, tmpbuf, 1))
6009 /* If we allocated a string above, free it */
6010 if (! do_utf8) Safefree(utf8_p);
6013 if (match && lenp && *lenp == 0)
6014 *lenp = UNISKIP(NATIVE_TO_UNI(c));
6016 if (!match && c < 256) {
6017 if (ANYOF_BITMAP_TEST(n, c))
6019 else if (flags & ANYOF_FOLD) {
6022 if (flags & ANYOF_LOCALE) {
6023 PL_reg_flags |= RF_tainted;
6024 f = PL_fold_locale[c];
6028 if (f != c && ANYOF_BITMAP_TEST(n, f))
6032 if (!match && (flags & ANYOF_CLASS)) {
6033 PL_reg_flags |= RF_tainted;
6035 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6036 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6037 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6038 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6039 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6040 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6041 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6042 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6043 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6044 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6045 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6046 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6047 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6048 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6049 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6050 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6051 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6052 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6053 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6054 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6055 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6056 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6057 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6058 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6059 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6060 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6061 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6062 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6063 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6064 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6065 ) /* How's that for a conditional? */
6072 return (flags & ANYOF_INVERT) ? !match : match;
6076 S_reghop3(U8 *s, I32 off, const U8* lim)
6080 PERL_ARGS_ASSERT_REGHOP3;
6083 while (off-- && s < lim) {
6084 /* XXX could check well-formedness here */
6089 while (off++ && s > lim) {
6091 if (UTF8_IS_CONTINUED(*s)) {
6092 while (s > lim && UTF8_IS_CONTINUATION(*s))
6095 /* XXX could check well-formedness here */
6102 /* there are a bunch of places where we use two reghop3's that should
6103 be replaced with this routine. but since thats not done yet
6104 we ifdef it out - dmq
6107 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6111 PERL_ARGS_ASSERT_REGHOP4;
6114 while (off-- && s < rlim) {
6115 /* XXX could check well-formedness here */
6120 while (off++ && s > llim) {
6122 if (UTF8_IS_CONTINUED(*s)) {
6123 while (s > llim && UTF8_IS_CONTINUATION(*s))
6126 /* XXX could check well-formedness here */
6134 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
6138 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6141 while (off-- && s < lim) {
6142 /* XXX could check well-formedness here */
6149 while (off++ && s > lim) {
6151 if (UTF8_IS_CONTINUED(*s)) {
6152 while (s > lim && UTF8_IS_CONTINUATION(*s))
6155 /* XXX could check well-formedness here */
6164 restore_pos(pTHX_ void *arg)
6167 regexp * const rex = (regexp *)arg;
6168 if (PL_reg_eval_set) {
6169 if (PL_reg_oldsaved) {
6170 rex->subbeg = PL_reg_oldsaved;
6171 rex->sublen = PL_reg_oldsavedlen;
6172 #ifdef PERL_OLD_COPY_ON_WRITE
6173 rex->saved_copy = PL_nrs;
6175 RXp_MATCH_COPIED_on(rex);
6177 PL_reg_magic->mg_len = PL_reg_oldpos;
6178 PL_reg_eval_set = 0;
6179 PL_curpm = PL_reg_oldcurpm;
6184 S_to_utf8_substr(pTHX_ register regexp *prog)
6188 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6191 if (prog->substrs->data[i].substr
6192 && !prog->substrs->data[i].utf8_substr) {
6193 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6194 prog->substrs->data[i].utf8_substr = sv;
6195 sv_utf8_upgrade(sv);
6196 if (SvVALID(prog->substrs->data[i].substr)) {
6197 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
6198 if (flags & FBMcf_TAIL) {
6199 /* Trim the trailing \n that fbm_compile added last
6201 SvCUR_set(sv, SvCUR(sv) - 1);
6202 /* Whilst this makes the SV technically "invalid" (as its
6203 buffer is no longer followed by "\0") when fbm_compile()
6204 adds the "\n" back, a "\0" is restored. */
6206 fbm_compile(sv, flags);
6208 if (prog->substrs->data[i].substr == prog->check_substr)
6209 prog->check_utf8 = sv;
6215 S_to_byte_substr(pTHX_ register regexp *prog)
6220 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6223 if (prog->substrs->data[i].utf8_substr
6224 && !prog->substrs->data[i].substr) {
6225 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6226 if (sv_utf8_downgrade(sv, TRUE)) {
6227 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6229 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6230 if (flags & FBMcf_TAIL) {
6231 /* Trim the trailing \n that fbm_compile added last
6233 SvCUR_set(sv, SvCUR(sv) - 1);
6235 fbm_compile(sv, flags);
6241 prog->substrs->data[i].substr = sv;
6242 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6243 prog->check_substr = sv;
6250 * c-indentation-style: bsd
6252 * indent-tabs-mode: t
6255 * ex: set ts=8 sts=4 sw=4 noet: