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
77 #ifdef PERL_IN_XSUB_RE
83 #define RF_tainted 1 /* tainted information used? e.g. locale */
84 #define RF_warned 2 /* warned about big count? */
86 #define RF_utf8 8 /* Pattern contains multibyte chars? */
88 #define UTF_PATTERN ((PL_reg_flags & RF_utf8) != 0)
90 #define RS_init 1 /* eval environment created */
91 #define RS_set 2 /* replsv value is set */
97 /* Valid for non-utf8 strings, non-ANYOFV nodes only: avoids the reginclass
98 * call if there are no complications: i.e., if everything matchable is
99 * straight forward in the bitmap */
100 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) \
101 : ANYOF_BITMAP_TEST(p,*(c)))
107 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
108 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
110 #define HOPc(pos,off) \
111 (char *)(PL_reg_match_utf8 \
112 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
114 #define HOPBACKc(pos, off) \
115 (char*)(PL_reg_match_utf8\
116 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
117 : (pos - off >= PL_bostr) \
121 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
122 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
124 /* these are unrolled below in the CCC_TRY_XXX defined */
125 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
126 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
128 /* Doesn't do an assert to verify that is correct */
129 #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
130 if (!CAT2(PL_utf8_,class)) { bool throw_away; ENTER; save_re_context(); throw_away = CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END
132 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
133 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
134 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
136 #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
137 LOAD_UTF8_CHARCLASS(X_begin, " "); \
138 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
139 /* These are utf8 constants, and not utf-ebcdic constants, so the \
140 * assert should likely and hopefully fail on an EBCDIC machine */ \
141 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
143 /* No asserts are done for these, in case called on an early \
144 * Unicode version in which they map to nothing */ \
145 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
146 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
147 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
148 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
149 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
150 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
151 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
154 We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test
155 so that it is possible to override the option here without having to
156 rebuild the entire core. as we are required to do if we change regcomp.h
157 which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined.
159 #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS
160 #define BROKEN_UNICODE_CHARCLASS_MAPPINGS
163 #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS
164 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM()
165 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE()
166 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT()
167 #define RE_utf8_perl_word PL_utf8_alnum
168 #define RE_utf8_perl_space PL_utf8_space
169 #define RE_utf8_posix_digit PL_utf8_digit
170 #define perl_word alnum
171 #define perl_space space
172 #define posix_digit digit
174 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a")
175 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ")
176 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0")
177 #define RE_utf8_perl_word PL_utf8_perl_word
178 #define RE_utf8_perl_space PL_utf8_perl_space
179 #define RE_utf8_posix_digit PL_utf8_posix_digit
182 #define PLACEHOLDER /* Something for the preprocessor to grab onto */
184 /* The actual code for CCC_TRY, which uses several variables from the routine
185 * it's callable from. It is designed to be the bulk of a case statement.
186 * FUNC is the macro or function to call on non-utf8 targets that indicate if
187 * nextchr matches the class.
188 * UTF8_TEST is the whole test string to use for utf8 targets
189 * LOAD is what to use to test, and if not present to load in the swash for the
191 * POS_OR_NEG is either empty or ! to complement the results of FUNC or
193 * The logic is: Fail if we're at the end-of-string; otherwise if the target is
194 * utf8 and a variant, load the swash if necessary and test using the utf8
195 * test. Advance to the next character if test is ok, otherwise fail; If not
196 * utf8 or an invariant under utf8, use the non-utf8 test, and fail if it
197 * fails, or advance to the next character */
199 #define _CCC_TRY_CODE(POS_OR_NEG, FUNC, UTF8_TEST, CLASS, STR) \
200 if (locinput >= PL_regeol) { \
203 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
204 LOAD_UTF8_CHARCLASS(CLASS, STR); \
205 if (POS_OR_NEG (UTF8_TEST)) { \
208 locinput += PL_utf8skip[nextchr]; \
209 nextchr = UCHARAT(locinput); \
212 if (POS_OR_NEG (FUNC(nextchr))) { \
215 nextchr = UCHARAT(++locinput); \
218 /* Handle the non-locale cases for a character class and its complement. It
219 * calls _CCC_TRY_CODE with a ! to complement the test for the character class.
220 * This is because that code fails when the test succeeds, so we want to have
221 * the test fail so that the code succeeds. The swash is stored in a
222 * predictable PL_ place */
223 #define _CCC_TRY_NONLOCALE(NAME, NNAME, FUNC, \
226 _CCC_TRY_CODE( !, FUNC, \
227 cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), \
228 (U8*)locinput, TRUE)), \
231 _CCC_TRY_CODE( PLACEHOLDER , FUNC, \
232 cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), \
233 (U8*)locinput, TRUE)), \
236 /* Generate the case statements for both locale and non-locale character
237 * classes in regmatch for classes that don't have special unicode semantics.
238 * Locales don't use an immediate swash, but an intermediary special locale
239 * function that is called on the pointer to the current place in the input
240 * string. That function will resolve to needing the same swash. One might
241 * think that because we don't know what the locale will match, we shouldn't
242 * check with the swash loading function that it loaded properly; ie, that we
243 * should use LOAD_UTF8_CHARCLASS_NO_CHECK for those, but what is passed to the
244 * regular LOAD_UTF8_CHARCLASS is in non-locale terms, and so locale is
246 #define CCC_TRY(NAME, NNAME, FUNC, \
247 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
248 NAMEA, NNAMEA, FUNCA, \
251 PL_reg_flags |= RF_tainted; \
252 _CCC_TRY_CODE( !, LCFUNC, LCFUNC_utf8((U8*)locinput), CLASS, STR) \
254 PL_reg_flags |= RF_tainted; \
255 _CCC_TRY_CODE( PLACEHOLDER, LCFUNC, LCFUNC_utf8((U8*)locinput), \
258 if (locinput >= PL_regeol || ! FUNCA(nextchr)) { \
261 /* Matched a utf8-invariant, so don't have to worry about utf8 */ \
262 nextchr = UCHARAT(++locinput); \
265 if (locinput >= PL_regeol || FUNCA(nextchr)) { \
269 locinput += PL_utf8skip[nextchr]; \
270 nextchr = UCHARAT(locinput); \
273 nextchr = UCHARAT(++locinput); \
276 /* Generate the non-locale cases */ \
277 _CCC_TRY_NONLOCALE(NAME, NNAME, FUNC, CLASS, STR)
279 /* This is like CCC_TRY, but has an extra set of parameters for generating case
280 * statements to handle separate Unicode semantics nodes */
281 #define CCC_TRY_U(NAME, NNAME, FUNC, \
282 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
283 NAMEU, NNAMEU, FUNCU, \
284 NAMEA, NNAMEA, FUNCA, \
286 CCC_TRY(NAME, NNAME, FUNC, \
287 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
288 NAMEA, NNAMEA, FUNCA, \
290 _CCC_TRY_NONLOCALE(NAMEU, NNAMEU, FUNCU, CLASS, STR)
292 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
294 /* for use after a quantifier and before an EXACT-like node -- japhy */
295 /* it would be nice to rework regcomp.sym to generate this stuff. sigh
297 * NOTE that *nothing* that affects backtracking should be in here, specifically
298 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
299 * node that is in between two EXACT like nodes when ascertaining what the required
300 * "follow" character is. This should probably be moved to regex compile time
301 * although it may be done at run time beause of the REF possibility - more
302 * investigation required. -- demerphq
304 #define JUMPABLE(rn) ( \
306 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
308 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
309 OP(rn) == PLUS || OP(rn) == MINMOD || \
311 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
313 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
315 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
318 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
319 we don't need this definition. */
320 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
321 #define IS_TEXTF(rn) ( (OP(rn)==EXACTFU || OP(rn)==EXACTFA || OP(rn)==EXACTF) || OP(rn)==REFF || OP(rn)==NREFF )
322 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
325 /* ... so we use this as its faster. */
326 #define IS_TEXT(rn) ( OP(rn)==EXACT )
327 #define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn) == EXACTFA)
328 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
329 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
334 Search for mandatory following text node; for lookahead, the text must
335 follow but for lookbehind (rn->flags != 0) we skip to the next step.
337 #define FIND_NEXT_IMPT(rn) STMT_START { \
338 while (JUMPABLE(rn)) { \
339 const OPCODE type = OP(rn); \
340 if (type == SUSPEND || PL_regkind[type] == CURLY) \
341 rn = NEXTOPER(NEXTOPER(rn)); \
342 else if (type == PLUS) \
344 else if (type == IFMATCH) \
345 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
346 else rn += NEXT_OFF(rn); \
351 static void restore_pos(pTHX_ void *arg);
353 #define REGCP_PAREN_ELEMS 4
354 #define REGCP_OTHER_ELEMS 5
355 #define REGCP_FRAME_ELEMS 1
356 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
357 * are needed for the regexp context stack bookkeeping. */
360 S_regcppush(pTHX_ I32 parenfloor)
363 const int retval = PL_savestack_ix;
364 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
365 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
366 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
368 GET_RE_DEBUG_FLAGS_DECL;
370 if (paren_elems_to_push < 0)
371 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
373 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
374 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
375 " out of range (%lu-%ld)",
376 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
378 SSGROW(total_elems + REGCP_FRAME_ELEMS);
380 for (p = PL_regsize; p > parenfloor; p--) {
381 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
382 SSPUSHINT(PL_regoffs[p].end);
383 SSPUSHINT(PL_regoffs[p].start);
384 SSPUSHPTR(PL_reg_start_tmp[p]);
386 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
387 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
388 (UV)p, (IV)PL_regoffs[p].start,
389 (IV)(PL_reg_start_tmp[p] - PL_bostr),
390 (IV)PL_regoffs[p].end
393 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
394 SSPUSHPTR(PL_regoffs);
395 SSPUSHINT(PL_regsize);
396 SSPUSHINT(*PL_reglastparen);
397 SSPUSHINT(*PL_reglastcloseparen);
398 SSPUSHPTR(PL_reginput);
399 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
404 /* These are needed since we do not localize EVAL nodes: */
405 #define REGCP_SET(cp) \
407 PerlIO_printf(Perl_debug_log, \
408 " Setting an EVAL scope, savestack=%"IVdf"\n", \
409 (IV)PL_savestack_ix)); \
412 #define REGCP_UNWIND(cp) \
414 if (cp != PL_savestack_ix) \
415 PerlIO_printf(Perl_debug_log, \
416 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
417 (IV)(cp), (IV)PL_savestack_ix)); \
421 S_regcppop(pTHX_ const regexp *rex)
426 GET_RE_DEBUG_FLAGS_DECL;
428 PERL_ARGS_ASSERT_REGCPPOP;
430 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
432 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
433 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
434 input = (char *) SSPOPPTR;
435 *PL_reglastcloseparen = SSPOPINT;
436 *PL_reglastparen = SSPOPINT;
437 PL_regsize = SSPOPINT;
438 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
440 i -= REGCP_OTHER_ELEMS;
441 /* Now restore the parentheses context. */
442 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
444 U32 paren = (U32)SSPOPINT;
445 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
446 PL_regoffs[paren].start = SSPOPINT;
448 if (paren <= *PL_reglastparen)
449 PL_regoffs[paren].end = tmps;
451 PerlIO_printf(Perl_debug_log,
452 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
453 (UV)paren, (IV)PL_regoffs[paren].start,
454 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
455 (IV)PL_regoffs[paren].end,
456 (paren > *PL_reglastparen ? "(no)" : ""));
460 if (*PL_reglastparen + 1 <= rex->nparens) {
461 PerlIO_printf(Perl_debug_log,
462 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
463 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
467 /* It would seem that the similar code in regtry()
468 * already takes care of this, and in fact it is in
469 * a better location to since this code can #if 0-ed out
470 * but the code in regtry() is needed or otherwise tests
471 * requiring null fields (pat.t#187 and split.t#{13,14}
472 * (as of patchlevel 7877) will fail. Then again,
473 * this code seems to be necessary or otherwise
474 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
475 * --jhi updated by dapm */
476 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
478 PL_regoffs[i].start = -1;
479 PL_regoffs[i].end = -1;
485 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
488 * pregexec and friends
491 #ifndef PERL_IN_XSUB_RE
493 - pregexec - match a regexp against a string
496 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
497 char *strbeg, I32 minend, SV *screamer, U32 nosave)
498 /* strend: pointer to null at end of string */
499 /* strbeg: real beginning of string */
500 /* minend: end of match must be >=minend after stringarg. */
501 /* nosave: For optimizations. */
503 PERL_ARGS_ASSERT_PREGEXEC;
506 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
507 nosave ? 0 : REXEC_COPY_STR);
512 * Need to implement the following flags for reg_anch:
514 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
516 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
517 * INTUIT_AUTORITATIVE_ML
518 * INTUIT_ONCE_NOML - Intuit can match in one location only.
521 * Another flag for this function: SECOND_TIME (so that float substrs
522 * with giant delta may be not rechecked).
525 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
527 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
528 Otherwise, only SvCUR(sv) is used to get strbeg. */
530 /* XXXX We assume that strpos is strbeg unless sv. */
532 /* XXXX Some places assume that there is a fixed substring.
533 An update may be needed if optimizer marks as "INTUITable"
534 RExen without fixed substrings. Similarly, it is assumed that
535 lengths of all the strings are no more than minlen, thus they
536 cannot come from lookahead.
537 (Or minlen should take into account lookahead.)
538 NOTE: Some of this comment is not correct. minlen does now take account
539 of lookahead/behind. Further research is required. -- demerphq
543 /* A failure to find a constant substring means that there is no need to make
544 an expensive call to REx engine, thus we celebrate a failure. Similarly,
545 finding a substring too deep into the string means that less calls to
546 regtry() should be needed.
548 REx compiler's optimizer found 4 possible hints:
549 a) Anchored substring;
551 c) Whether we are anchored (beginning-of-line or \G);
552 d) First node (of those at offset 0) which may distinguish positions;
553 We use a)b)d) and multiline-part of c), and try to find a position in the
554 string which does not contradict any of them.
557 /* Most of decisions we do here should have been done at compile time.
558 The nodes of the REx which we used for the search should have been
559 deleted from the finite automaton. */
562 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
563 char *strend, const U32 flags, re_scream_pos_data *data)
566 struct regexp *const prog = (struct regexp *)SvANY(rx);
567 register I32 start_shift = 0;
568 /* Should be nonnegative! */
569 register I32 end_shift = 0;
574 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
576 register char *other_last = NULL; /* other substr checked before this */
577 char *check_at = NULL; /* check substr found at this pos */
578 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
579 RXi_GET_DECL(prog,progi);
581 const char * const i_strpos = strpos;
583 GET_RE_DEBUG_FLAGS_DECL;
585 PERL_ARGS_ASSERT_RE_INTUIT_START;
587 RX_MATCH_UTF8_set(rx,utf8_target);
590 PL_reg_flags |= RF_utf8;
593 debug_start_match(rx, utf8_target, strpos, strend,
594 sv ? "Guessing start of match in sv for"
595 : "Guessing start of match in string for");
598 /* CHR_DIST() would be more correct here but it makes things slow. */
599 if (prog->minlen > strend - strpos) {
600 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
601 "String too short... [re_intuit_start]\n"));
605 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
608 if (!prog->check_utf8 && prog->check_substr)
609 to_utf8_substr(prog);
610 check = prog->check_utf8;
612 if (!prog->check_substr && prog->check_utf8)
613 to_byte_substr(prog);
614 check = prog->check_substr;
616 if (check == &PL_sv_undef) {
617 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
618 "Non-utf8 string cannot match utf8 check string\n"));
621 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
622 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
623 || ( (prog->extflags & RXf_ANCH_BOL)
624 && !multiline ) ); /* Check after \n? */
627 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
628 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
629 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
631 && (strpos != strbeg)) {
632 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
635 if (prog->check_offset_min == prog->check_offset_max &&
636 !(prog->extflags & RXf_CANY_SEEN)) {
637 /* Substring at constant offset from beg-of-str... */
640 s = HOP3c(strpos, prog->check_offset_min, strend);
643 slen = SvCUR(check); /* >= 1 */
645 if ( strend - s > slen || strend - s < slen - 1
646 || (strend - s == slen && strend[-1] != '\n')) {
647 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
650 /* Now should match s[0..slen-2] */
652 if (slen && (*SvPVX_const(check) != *s
654 && memNE(SvPVX_const(check), s, slen)))) {
656 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
660 else if (*SvPVX_const(check) != *s
661 || ((slen = SvCUR(check)) > 1
662 && memNE(SvPVX_const(check), s, slen)))
665 goto success_at_start;
668 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
670 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
671 end_shift = prog->check_end_shift;
674 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
675 - (SvTAIL(check) != 0);
676 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
678 if (end_shift < eshift)
682 else { /* Can match at random position */
685 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
686 end_shift = prog->check_end_shift;
688 /* end shift should be non negative here */
691 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
693 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
694 (IV)end_shift, RX_PRECOMP(prog));
698 /* Find a possible match in the region s..strend by looking for
699 the "check" substring in the region corrected by start/end_shift. */
702 I32 srch_start_shift = start_shift;
703 I32 srch_end_shift = end_shift;
704 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
705 srch_end_shift -= ((strbeg - s) - srch_start_shift);
706 srch_start_shift = strbeg - s;
708 DEBUG_OPTIMISE_MORE_r({
709 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
710 (IV)prog->check_offset_min,
711 (IV)srch_start_shift,
713 (IV)prog->check_end_shift);
716 if (flags & REXEC_SCREAM) {
717 I32 p = -1; /* Internal iterator of scream. */
718 I32 * const pp = data ? data->scream_pos : &p;
720 if (PL_screamfirst[BmRARE(check)] >= 0
721 || ( BmRARE(check) == '\n'
722 && (BmPREVIOUS(check) == SvCUR(check) - 1)
724 s = screaminstr(sv, check,
725 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
728 /* we may be pointing at the wrong string */
729 if (s && RXp_MATCH_COPIED(prog))
730 s = strbeg + (s - SvPVX_const(sv));
732 *data->scream_olds = s;
737 if (prog->extflags & RXf_CANY_SEEN) {
738 start_point= (U8*)(s + srch_start_shift);
739 end_point= (U8*)(strend - srch_end_shift);
741 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
742 end_point= HOP3(strend, -srch_end_shift, strbeg);
744 DEBUG_OPTIMISE_MORE_r({
745 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
746 (int)(end_point - start_point),
747 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
751 s = fbm_instr( start_point, end_point,
752 check, multiline ? FBMrf_MULTILINE : 0);
755 /* Update the count-of-usability, remove useless subpatterns,
759 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
760 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
761 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
762 (s ? "Found" : "Did not find"),
763 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
764 ? "anchored" : "floating"),
767 (s ? " at offset " : "...\n") );
772 /* Finish the diagnostic message */
773 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
775 /* XXX dmq: first branch is for positive lookbehind...
776 Our check string is offset from the beginning of the pattern.
777 So we need to do any stclass tests offset forward from that
786 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
787 Start with the other substr.
788 XXXX no SCREAM optimization yet - and a very coarse implementation
789 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
790 *always* match. Probably should be marked during compile...
791 Probably it is right to do no SCREAM here...
794 if (utf8_target ? (prog->float_utf8 && prog->anchored_utf8)
795 : (prog->float_substr && prog->anchored_substr))
797 /* Take into account the "other" substring. */
798 /* XXXX May be hopelessly wrong for UTF... */
801 if (check == (utf8_target ? prog->float_utf8 : prog->float_substr)) {
804 char * const last = HOP3c(s, -start_shift, strbeg);
806 char * const saved_s = s;
809 t = s - prog->check_offset_max;
810 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
812 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
817 t = HOP3c(t, prog->anchored_offset, strend);
818 if (t < other_last) /* These positions already checked */
820 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
823 /* XXXX It is not documented what units *_offsets are in.
824 We assume bytes, but this is clearly wrong.
825 Meaning this code needs to be carefully reviewed for errors.
829 /* On end-of-str: see comment below. */
830 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
831 if (must == &PL_sv_undef) {
833 DEBUG_r(must = prog->anchored_utf8); /* for debug */
838 HOP3(HOP3(last1, prog->anchored_offset, strend)
839 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
841 multiline ? FBMrf_MULTILINE : 0
844 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
845 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
846 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
847 (s ? "Found" : "Contradicts"),
848 quoted, RE_SV_TAIL(must));
853 if (last1 >= last2) {
854 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
855 ", giving up...\n"));
858 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
859 ", trying floating at offset %ld...\n",
860 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
861 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
862 s = HOP3c(last, 1, strend);
866 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
867 (long)(s - i_strpos)));
868 t = HOP3c(s, -prog->anchored_offset, strbeg);
869 other_last = HOP3c(s, 1, strend);
877 else { /* Take into account the floating substring. */
879 char * const saved_s = s;
882 t = HOP3c(s, -start_shift, strbeg);
884 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
885 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
886 last = HOP3c(t, prog->float_max_offset, strend);
887 s = HOP3c(t, prog->float_min_offset, strend);
890 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
891 must = utf8_target ? prog->float_utf8 : prog->float_substr;
892 /* fbm_instr() takes into account exact value of end-of-str
893 if the check is SvTAIL(ed). Since false positives are OK,
894 and end-of-str is not later than strend we are OK. */
895 if (must == &PL_sv_undef) {
897 DEBUG_r(must = prog->float_utf8); /* for debug message */
900 s = fbm_instr((unsigned char*)s,
901 (unsigned char*)last + SvCUR(must)
903 must, multiline ? FBMrf_MULTILINE : 0);
905 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
906 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
907 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
908 (s ? "Found" : "Contradicts"),
909 quoted, RE_SV_TAIL(must));
913 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
914 ", giving up...\n"));
917 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
918 ", trying anchored starting at offset %ld...\n",
919 (long)(saved_s + 1 - i_strpos)));
921 s = HOP3c(t, 1, strend);
925 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
926 (long)(s - i_strpos)));
927 other_last = s; /* Fix this later. --Hugo */
937 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
939 DEBUG_OPTIMISE_MORE_r(
940 PerlIO_printf(Perl_debug_log,
941 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
942 (IV)prog->check_offset_min,
943 (IV)prog->check_offset_max,
951 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
953 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
956 /* Fixed substring is found far enough so that the match
957 cannot start at strpos. */
959 if (ml_anch && t[-1] != '\n') {
960 /* Eventually fbm_*() should handle this, but often
961 anchored_offset is not 0, so this check will not be wasted. */
962 /* XXXX In the code below we prefer to look for "^" even in
963 presence of anchored substrings. And we search even
964 beyond the found float position. These pessimizations
965 are historical artefacts only. */
967 while (t < strend - prog->minlen) {
969 if (t < check_at - prog->check_offset_min) {
970 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
971 /* Since we moved from the found position,
972 we definitely contradict the found anchored
973 substr. Due to the above check we do not
974 contradict "check" substr.
975 Thus we can arrive here only if check substr
976 is float. Redo checking for "other"=="fixed".
979 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
980 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
981 goto do_other_anchored;
983 /* We don't contradict the found floating substring. */
984 /* XXXX Why not check for STCLASS? */
986 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
987 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
990 /* Position contradicts check-string */
991 /* XXXX probably better to look for check-string
992 than for "\n", so one should lower the limit for t? */
993 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
994 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
995 other_last = strpos = s = t + 1;
1000 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
1001 PL_colors[0], PL_colors[1]));
1005 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
1006 PL_colors[0], PL_colors[1]));
1010 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
1013 /* The found string does not prohibit matching at strpos,
1014 - no optimization of calling REx engine can be performed,
1015 unless it was an MBOL and we are not after MBOL,
1016 or a future STCLASS check will fail this. */
1018 /* Even in this situation we may use MBOL flag if strpos is offset
1019 wrt the start of the string. */
1020 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
1021 && (strpos != strbeg) && strpos[-1] != '\n'
1022 /* May be due to an implicit anchor of m{.*foo} */
1023 && !(prog->intflags & PREGf_IMPLICIT))
1028 DEBUG_EXECUTE_r( if (ml_anch)
1029 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
1030 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
1033 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
1035 prog->check_utf8 /* Could be deleted already */
1036 && --BmUSEFUL(prog->check_utf8) < 0
1037 && (prog->check_utf8 == prog->float_utf8)
1039 prog->check_substr /* Could be deleted already */
1040 && --BmUSEFUL(prog->check_substr) < 0
1041 && (prog->check_substr == prog->float_substr)
1044 /* If flags & SOMETHING - do not do it many times on the same match */
1045 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
1046 /* XXX Does the destruction order has to change with utf8_target? */
1047 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1048 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1049 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1050 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1051 check = NULL; /* abort */
1053 /* XXXX If the check string was an implicit check MBOL, then we need to unset the relevant flag
1054 see http://bugs.activestate.com/show_bug.cgi?id=87173 */
1055 if (prog->intflags & PREGf_IMPLICIT)
1056 prog->extflags &= ~RXf_ANCH_MBOL;
1057 /* XXXX This is a remnant of the old implementation. It
1058 looks wasteful, since now INTUIT can use many
1059 other heuristics. */
1060 prog->extflags &= ~RXf_USE_INTUIT;
1061 /* XXXX What other flags might need to be cleared in this branch? */
1067 /* Last resort... */
1068 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1069 /* trie stclasses are too expensive to use here, we are better off to
1070 leave it to regmatch itself */
1071 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1072 /* minlen == 0 is possible if regstclass is \b or \B,
1073 and the fixed substr is ''$.
1074 Since minlen is already taken into account, s+1 is before strend;
1075 accidentally, minlen >= 1 guaranties no false positives at s + 1
1076 even for \b or \B. But (minlen? 1 : 0) below assumes that
1077 regstclass does not come from lookahead... */
1078 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1079 This leaves EXACTF-ish only, which are dealt with in find_byclass(). */
1080 const U8* const str = (U8*)STRING(progi->regstclass);
1081 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1082 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1085 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1086 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1087 else if (prog->float_substr || prog->float_utf8)
1088 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1092 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1093 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1096 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1099 const char *what = NULL;
1101 if (endpos == strend) {
1102 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1103 "Could not match STCLASS...\n") );
1106 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1107 "This position contradicts STCLASS...\n") );
1108 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1110 /* Contradict one of substrings */
1111 if (prog->anchored_substr || prog->anchored_utf8) {
1112 if ((utf8_target ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1113 DEBUG_EXECUTE_r( what = "anchored" );
1115 s = HOP3c(t, 1, strend);
1116 if (s + start_shift + end_shift > strend) {
1117 /* XXXX Should be taken into account earlier? */
1118 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1119 "Could not match STCLASS...\n") );
1124 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1125 "Looking for %s substr starting at offset %ld...\n",
1126 what, (long)(s + start_shift - i_strpos)) );
1129 /* Have both, check_string is floating */
1130 if (t + start_shift >= check_at) /* Contradicts floating=check */
1131 goto retry_floating_check;
1132 /* Recheck anchored substring, but not floating... */
1136 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1137 "Looking for anchored substr starting at offset %ld...\n",
1138 (long)(other_last - i_strpos)) );
1139 goto do_other_anchored;
1141 /* Another way we could have checked stclass at the
1142 current position only: */
1147 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1148 "Looking for /%s^%s/m starting at offset %ld...\n",
1149 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1152 if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1154 /* Check is floating substring. */
1155 retry_floating_check:
1156 t = check_at - start_shift;
1157 DEBUG_EXECUTE_r( what = "floating" );
1158 goto hop_and_restart;
1161 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1162 "By STCLASS: moving %ld --> %ld\n",
1163 (long)(t - i_strpos), (long)(s - i_strpos))
1167 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1168 "Does not contradict STCLASS...\n");
1173 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1174 PL_colors[4], (check ? "Guessed" : "Giving up"),
1175 PL_colors[5], (long)(s - i_strpos)) );
1178 fail_finish: /* Substring not found */
1179 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1180 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1182 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1183 PL_colors[4], PL_colors[5]));
1187 #define DECL_TRIE_TYPE(scan) \
1188 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1189 trie_type = (scan->flags != EXACT) \
1190 ? (utf8_target ? trie_utf8_fold : (UTF_PATTERN ? trie_latin_utf8_fold : trie_plain)) \
1191 : (utf8_target ? trie_utf8 : trie_plain)
1193 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1194 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1195 switch (trie_type) { \
1196 case trie_utf8_fold: \
1197 if ( foldlen>0 ) { \
1198 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1203 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1204 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1205 foldlen -= UNISKIP( uvc ); \
1206 uscan = foldbuf + UNISKIP( uvc ); \
1209 case trie_latin_utf8_fold: \
1210 if ( foldlen>0 ) { \
1211 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1217 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1218 foldlen -= UNISKIP( uvc ); \
1219 uscan = foldbuf + UNISKIP( uvc ); \
1223 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1230 charid = trie->charmap[ uvc ]; \
1234 if (widecharmap) { \
1235 SV** const svpp = hv_fetch(widecharmap, \
1236 (char*)&uvc, sizeof(UV), 0); \
1238 charid = (U16)SvIV(*svpp); \
1243 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1247 && (ln == 1 || folder(s, pat_string, ln)) \
1248 && (!reginfo || regtry(reginfo, &s)) ) \
1254 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1256 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1262 #define REXEC_FBC_SCAN(CoDe) \
1264 while (s < strend) { \
1270 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1271 REXEC_FBC_UTF8_SCAN( \
1273 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1282 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1285 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1294 #define REXEC_FBC_TRYIT \
1295 if ((!reginfo || regtry(reginfo, &s))) \
1298 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1299 if (utf8_target) { \
1300 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1303 REXEC_FBC_CLASS_SCAN(CoNd); \
1306 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1307 if (utf8_target) { \
1309 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1312 REXEC_FBC_CLASS_SCAN(CoNd); \
1315 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1316 PL_reg_flags |= RF_tainted; \
1317 if (utf8_target) { \
1318 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1321 REXEC_FBC_CLASS_SCAN(CoNd); \
1324 #define DUMP_EXEC_POS(li,s,doutf8) \
1325 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1328 #define UTF8_NOLOAD(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
1329 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; \
1330 tmp = TEST_NON_UTF8(tmp); \
1331 REXEC_FBC_UTF8_SCAN( \
1332 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1341 #define UTF8_LOAD(TeSt1_UtF8, TeSt2_UtF8, IF_SUCCESS, IF_FAIL) \
1342 if (s == PL_bostr) { \
1346 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); \
1347 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); \
1350 LOAD_UTF8_CHARCLASS_ALNUM(); \
1351 REXEC_FBC_UTF8_SCAN( \
1352 if (tmp == ! (TeSt2_UtF8)) { \
1361 /* The only difference between the BOUND and NBOUND cases is that
1362 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1363 * NBOUND. This is accomplished by passing it in either the if or else clause,
1364 * with the other one being empty */
1365 #define FBC_BOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1366 FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1368 #define FBC_BOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1369 FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1371 #define FBC_NBOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1372 FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1374 #define FBC_NBOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1375 FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1378 /* Common to the BOUND and NBOUND cases. Unfortunately the UTF8 tests need to
1379 * be passed in completely with the variable name being tested, which isn't
1380 * such a clean interface, but this is easier to read than it was before. We
1381 * are looking for the boundary (or non-boundary between a word and non-word
1382 * character. The utf8 and non-utf8 cases have the same logic, but the details
1383 * must be different. Find the "wordness" of the character just prior to this
1384 * one, and compare it with the wordness of this one. If they differ, we have
1385 * a boundary. At the beginning of the string, pretend that the previous
1386 * character was a new-line */
1387 #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
1388 if (utf8_target) { \
1391 else { /* Not utf8 */ \
1392 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; \
1393 tmp = TEST_NON_UTF8(tmp); \
1395 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1404 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s))) \
1407 /* We know what class REx starts with. Try to find this position... */
1408 /* if reginfo is NULL, its a dryrun */
1409 /* annoyingly all the vars in this routine have different names from their counterparts
1410 in regmatch. /grrr */
1413 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1414 const char *strend, regmatch_info *reginfo)
1417 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1418 char *pat_string; /* The pattern's exactish string */
1419 char *pat_end; /* ptr to end char of pat_string */
1420 re_fold_t folder; /* Function for computing non-utf8 folds */
1421 const U8 *fold_array; /* array for folding ords < 256 */
1424 register STRLEN uskip;
1428 register I32 tmp = 1; /* Scratch variable? */
1429 register const bool utf8_target = PL_reg_match_utf8;
1430 UV utf8_fold_flags = 0;
1431 RXi_GET_DECL(prog,progi);
1433 PERL_ARGS_ASSERT_FIND_BYCLASS;
1435 /* We know what class it must start with. */
1439 if (utf8_target || OP(c) == ANYOFV) {
1440 STRLEN inclasslen = strend - s;
1441 REXEC_FBC_UTF8_CLASS_SCAN(
1442 reginclass(prog, c, (U8*)s, &inclasslen, utf8_target));
1445 while (s < strend) {
1448 if (REGINCLASS(prog, c, (U8*)s) ||
1449 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1450 /* The assignment of 2 is intentional:
1451 * for the folded sharp s, the skip is 2. */
1452 (skip = SHARP_S_SKIP))) {
1453 if (tmp && (!reginfo || regtry(reginfo, &s)))
1466 if (tmp && (!reginfo || regtry(reginfo, &s)))
1474 if (UTF_PATTERN || utf8_target) {
1475 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1476 goto do_exactf_utf8;
1478 fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */
1479 folder = foldEQ_latin1; /* /a, except the sharp s one which */
1480 goto do_exactf_non_utf8; /* isn't dealt with by these */
1483 if (UTF_PATTERN || utf8_target) {
1484 utf8_fold_flags = 0;
1485 goto do_exactf_utf8;
1487 fold_array = PL_fold_latin1;
1488 folder = foldEQ_latin1;
1489 /* XXX This uses the full utf8 fold because if the pattern contains
1490 * 'ss' it could match LATIN_SMALL_LETTER SHARP_S in the string.
1491 * There could be a new node type, say EXACTFU_SS, which is
1492 * generated by regcomp only if there is an 'ss', and then every
1493 * other case could goto do_exactf_non_utf8;*/
1494 goto do_exactf_utf8;
1497 if (UTF_PATTERN || utf8_target) {
1498 utf8_fold_flags = 0;
1499 goto do_exactf_utf8;
1501 fold_array = PL_fold;
1503 goto do_exactf_non_utf8;
1506 if (UTF_PATTERN || utf8_target) {
1507 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
1508 goto do_exactf_utf8;
1510 fold_array = PL_fold_locale;
1511 folder = foldEQ_locale;
1515 do_exactf_non_utf8: /* Neither pattern nor string are UTF8 */
1517 /* The idea in the non-utf8 EXACTF* cases is to first find the
1518 * first character of the EXACTF* node and then, if necessary,
1519 * case-insensitively compare the full text of the node. c1 is the
1520 * first character. c2 is its fold. This logic will not work for
1521 * Unicode semantics and the german sharp ss, which hence should
1522 * not be compiled into a node that gets here. */
1523 pat_string = STRING(c);
1524 ln = STR_LEN(c); /* length to match in octets/bytes */
1526 e = HOP3c(strend, -((I32)ln), s);
1528 if (!reginfo && e < s) {
1529 e = s; /* Due to minlen logic of intuit() */
1533 c2 = fold_array[c1];
1534 if (c1 == c2) { /* If char and fold are the same */
1535 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1538 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1544 /* If one of the operands is in utf8, we can't use the simpler
1545 * folding above, due to the fact that many different characters
1546 * can have the same fold, or portion of a fold, or different-
1548 pat_string = STRING(c);
1549 ln = STR_LEN(c); /* length to match in octets/bytes */
1550 pat_end = pat_string + ln;
1551 lnc = (UTF_PATTERN) /* length to match in characters */
1552 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
1555 e = HOP3c(strend, -((I32)lnc), s);
1557 if (!reginfo && e < s) {
1558 e = s; /* Due to minlen logic of intuit() */
1562 char *my_strend= (char *)strend;
1563 if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
1564 pat_string, NULL, ln, cBOOL(UTF_PATTERN), utf8_fold_flags)
1565 && (!reginfo || regtry(reginfo, &s)) )
1573 PL_reg_flags |= RF_tainted;
1574 FBC_BOUND(isALNUM_LC,
1575 isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp)),
1576 isALNUM_LC_utf8((U8*)s));
1579 PL_reg_flags |= RF_tainted;
1580 FBC_NBOUND(isALNUM_LC,
1581 isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp)),
1582 isALNUM_LC_utf8((U8*)s));
1585 FBC_BOUND(isWORDCHAR,
1587 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1590 FBC_BOUND_NOLOAD(isWORDCHAR_A,
1592 isWORDCHAR_A((U8*)s));
1595 FBC_NBOUND(isWORDCHAR,
1597 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1600 FBC_NBOUND_NOLOAD(isWORDCHAR_A,
1602 isWORDCHAR_A((U8*)s));
1605 FBC_BOUND(isWORDCHAR_L1,
1607 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1610 FBC_NBOUND(isWORDCHAR_L1,
1612 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1615 REXEC_FBC_CSCAN_TAINT(
1616 isALNUM_LC_utf8((U8*)s),
1621 REXEC_FBC_CSCAN_PRELOAD(
1622 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1623 swash_fetch(RE_utf8_perl_word,(U8*)s, utf8_target),
1624 isWORDCHAR_L1((U8) *s)
1628 REXEC_FBC_CSCAN_PRELOAD(
1629 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1630 swash_fetch(RE_utf8_perl_word,(U8*)s, utf8_target),
1635 /* Don't need to worry about utf8, as it can match only a single
1636 * byte invariant character */
1637 REXEC_FBC_CLASS_SCAN( isWORDCHAR_A(*s));
1640 REXEC_FBC_CSCAN_PRELOAD(
1641 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1642 swash_fetch(RE_utf8_perl_word,(U8*)s, utf8_target),
1643 ! isWORDCHAR_L1((U8) *s)
1647 REXEC_FBC_CSCAN_PRELOAD(
1648 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1649 !swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
1660 REXEC_FBC_CSCAN_TAINT(
1661 !isALNUM_LC_utf8((U8*)s),
1666 REXEC_FBC_CSCAN_PRELOAD(
1667 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1668 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target),
1673 REXEC_FBC_CSCAN_PRELOAD(
1674 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1675 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target),
1680 /* Don't need to worry about utf8, as it can match only a single
1681 * byte invariant character */
1682 REXEC_FBC_CLASS_SCAN( isSPACE_A(*s));
1685 REXEC_FBC_CSCAN_TAINT(
1686 isSPACE_LC_utf8((U8*)s),
1691 REXEC_FBC_CSCAN_PRELOAD(
1692 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1693 !( *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target)),
1694 ! isSPACE_L1((U8) *s)
1698 REXEC_FBC_CSCAN_PRELOAD(
1699 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1700 !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target)),
1711 REXEC_FBC_CSCAN_TAINT(
1712 !isSPACE_LC_utf8((U8*)s),
1717 REXEC_FBC_CSCAN_PRELOAD(
1718 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1719 swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
1724 /* Don't need to worry about utf8, as it can match only a single
1725 * byte invariant character */
1726 REXEC_FBC_CLASS_SCAN( isDIGIT_A(*s));
1729 REXEC_FBC_CSCAN_TAINT(
1730 isDIGIT_LC_utf8((U8*)s),
1735 REXEC_FBC_CSCAN_PRELOAD(
1736 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1737 !swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
1748 REXEC_FBC_CSCAN_TAINT(
1749 !isDIGIT_LC_utf8((U8*)s),
1756 is_LNBREAK_latin1(s)
1768 !is_VERTWS_latin1(s)
1774 is_HORIZWS_latin1(s)
1779 !is_HORIZWS_utf8(s),
1780 !is_HORIZWS_latin1(s)
1787 /* what trie are we using right now */
1789 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1791 = (reg_trie_data*)progi->data->data[ aho->trie ];
1792 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1794 const char *last_start = strend - trie->minlen;
1796 const char *real_start = s;
1798 STRLEN maxlen = trie->maxlen;
1800 U8 **points; /* map of where we were in the input string
1801 when reading a given char. For ASCII this
1802 is unnecessary overhead as the relationship
1803 is always 1:1, but for Unicode, especially
1804 case folded Unicode this is not true. */
1805 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1809 GET_RE_DEBUG_FLAGS_DECL;
1811 /* We can't just allocate points here. We need to wrap it in
1812 * an SV so it gets freed properly if there is a croak while
1813 * running the match */
1816 sv_points=newSV(maxlen * sizeof(U8 *));
1817 SvCUR_set(sv_points,
1818 maxlen * sizeof(U8 *));
1819 SvPOK_on(sv_points);
1820 sv_2mortal(sv_points);
1821 points=(U8**)SvPV_nolen(sv_points );
1822 if ( trie_type != trie_utf8_fold
1823 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1826 bitmap=(U8*)trie->bitmap;
1828 bitmap=(U8*)ANYOF_BITMAP(c);
1830 /* this is the Aho-Corasick algorithm modified a touch
1831 to include special handling for long "unknown char"
1832 sequences. The basic idea being that we use AC as long
1833 as we are dealing with a possible matching char, when
1834 we encounter an unknown char (and we have not encountered
1835 an accepting state) we scan forward until we find a legal
1837 AC matching is basically that of trie matching, except
1838 that when we encounter a failing transition, we fall back
1839 to the current states "fail state", and try the current char
1840 again, a process we repeat until we reach the root state,
1841 state 1, or a legal transition. If we fail on the root state
1842 then we can either terminate if we have reached an accepting
1843 state previously, or restart the entire process from the beginning
1847 while (s <= last_start) {
1848 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1856 U8 *uscan = (U8*)NULL;
1857 U8 *leftmost = NULL;
1859 U32 accepted_word= 0;
1863 while ( state && uc <= (U8*)strend ) {
1865 U32 word = aho->states[ state ].wordnum;
1869 DEBUG_TRIE_EXECUTE_r(
1870 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1871 dump_exec_pos( (char *)uc, c, strend, real_start,
1872 (char *)uc, utf8_target );
1873 PerlIO_printf( Perl_debug_log,
1874 " Scanning for legal start char...\n");
1878 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1882 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1888 if (uc >(U8*)last_start) break;
1892 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
1893 if (!leftmost || lpos < leftmost) {
1894 DEBUG_r(accepted_word=word);
1900 points[pointpos++ % maxlen]= uc;
1901 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1902 uscan, len, uvc, charid, foldlen,
1904 DEBUG_TRIE_EXECUTE_r({
1905 dump_exec_pos( (char *)uc, c, strend, real_start,
1907 PerlIO_printf(Perl_debug_log,
1908 " Charid:%3u CP:%4"UVxf" ",
1914 word = aho->states[ state ].wordnum;
1916 base = aho->states[ state ].trans.base;
1918 DEBUG_TRIE_EXECUTE_r({
1920 dump_exec_pos( (char *)uc, c, strend, real_start,
1922 PerlIO_printf( Perl_debug_log,
1923 "%sState: %4"UVxf", word=%"UVxf,
1924 failed ? " Fail transition to " : "",
1925 (UV)state, (UV)word);
1931 ( ((offset = base + charid
1932 - 1 - trie->uniquecharcount)) >= 0)
1933 && ((U32)offset < trie->lasttrans)
1934 && trie->trans[offset].check == state
1935 && (tmp=trie->trans[offset].next))
1937 DEBUG_TRIE_EXECUTE_r(
1938 PerlIO_printf( Perl_debug_log," - legal\n"));
1943 DEBUG_TRIE_EXECUTE_r(
1944 PerlIO_printf( Perl_debug_log," - fail\n"));
1946 state = aho->fail[state];
1950 /* we must be accepting here */
1951 DEBUG_TRIE_EXECUTE_r(
1952 PerlIO_printf( Perl_debug_log," - accepting\n"));
1961 if (!state) state = 1;
1964 if ( aho->states[ state ].wordnum ) {
1965 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
1966 if (!leftmost || lpos < leftmost) {
1967 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1972 s = (char*)leftmost;
1973 DEBUG_TRIE_EXECUTE_r({
1975 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1976 (UV)accepted_word, (IV)(s - real_start)
1979 if (!reginfo || regtry(reginfo, &s)) {
1985 DEBUG_TRIE_EXECUTE_r({
1986 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1989 DEBUG_TRIE_EXECUTE_r(
1990 PerlIO_printf( Perl_debug_log,"No match.\n"));
1999 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
2009 - regexec_flags - match a regexp against a string
2012 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
2013 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
2014 /* strend: pointer to null at end of string */
2015 /* strbeg: real beginning of string */
2016 /* minend: end of match must be >=minend after stringarg. */
2017 /* data: May be used for some additional optimizations.
2018 Currently its only used, with a U32 cast, for transmitting
2019 the ganch offset when doing a /g match. This will change */
2020 /* nosave: For optimizations. */
2023 struct regexp *const prog = (struct regexp *)SvANY(rx);
2024 /*register*/ char *s;
2025 register regnode *c;
2026 /*register*/ char *startpos = stringarg;
2027 I32 minlen; /* must match at least this many chars */
2028 I32 dontbother = 0; /* how many characters not to try at end */
2029 I32 end_shift = 0; /* Same for the end. */ /* CC */
2030 I32 scream_pos = -1; /* Internal iterator of scream. */
2031 char *scream_olds = NULL;
2032 const bool utf8_target = cBOOL(DO_UTF8(sv));
2034 RXi_GET_DECL(prog,progi);
2035 regmatch_info reginfo; /* create some info to pass to regtry etc */
2036 regexp_paren_pair *swap = NULL;
2037 GET_RE_DEBUG_FLAGS_DECL;
2039 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
2040 PERL_UNUSED_ARG(data);
2042 /* Be paranoid... */
2043 if (prog == NULL || startpos == NULL) {
2044 Perl_croak(aTHX_ "NULL regexp parameter");
2048 multiline = prog->extflags & RXf_PMf_MULTILINE;
2049 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
2051 RX_MATCH_UTF8_set(rx, utf8_target);
2053 debug_start_match(rx, utf8_target, startpos, strend,
2057 minlen = prog->minlen;
2059 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
2060 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2061 "String too short [regexec_flags]...\n"));
2066 /* Check validity of program. */
2067 if (UCHARAT(progi->program) != REG_MAGIC) {
2068 Perl_croak(aTHX_ "corrupted regexp program");
2072 PL_reg_eval_set = 0;
2076 PL_reg_flags |= RF_utf8;
2078 /* Mark beginning of line for ^ and lookbehind. */
2079 reginfo.bol = startpos; /* XXX not used ??? */
2083 /* Mark end of line for $ (and such) */
2086 /* see how far we have to get to not match where we matched before */
2087 reginfo.till = startpos+minend;
2089 /* If there is a "must appear" string, look for it. */
2092 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
2094 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
2095 reginfo.ganch = startpos + prog->gofs;
2096 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2097 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
2098 } else if (sv && SvTYPE(sv) >= SVt_PVMG
2100 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
2101 && mg->mg_len >= 0) {
2102 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
2103 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2104 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
2106 if (prog->extflags & RXf_ANCH_GPOS) {
2107 if (s > reginfo.ganch)
2109 s = reginfo.ganch - prog->gofs;
2110 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2111 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
2117 reginfo.ganch = strbeg + PTR2UV(data);
2118 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2119 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
2121 } else { /* pos() not defined */
2122 reginfo.ganch = strbeg;
2123 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2124 "GPOS: reginfo.ganch = strbeg\n"));
2127 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
2128 /* We have to be careful. If the previous successful match
2129 was from this regex we don't want a subsequent partially
2130 successful match to clobber the old results.
2131 So when we detect this possibility we add a swap buffer
2132 to the re, and switch the buffer each match. If we fail
2133 we switch it back, otherwise we leave it swapped.
2136 /* do we need a save destructor here for eval dies? */
2137 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
2139 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
2140 re_scream_pos_data d;
2142 d.scream_olds = &scream_olds;
2143 d.scream_pos = &scream_pos;
2144 s = re_intuit_start(rx, sv, s, strend, flags, &d);
2146 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
2147 goto phooey; /* not present */
2153 /* Simplest case: anchored match need be tried only once. */
2154 /* [unless only anchor is BOL and multiline is set] */
2155 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2156 if (s == startpos && regtry(®info, &startpos))
2158 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2159 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2164 dontbother = minlen - 1;
2165 end = HOP3c(strend, -dontbother, strbeg) - 1;
2166 /* for multiline we only have to try after newlines */
2167 if (prog->check_substr || prog->check_utf8) {
2168 /* because of the goto we can not easily reuse the macros for bifurcating the
2169 unicode/non-unicode match modes here like we do elsewhere - demerphq */
2172 goto after_try_utf8;
2174 if (regtry(®info, &s)) {
2181 if (prog->extflags & RXf_USE_INTUIT) {
2182 s = re_intuit_start(rx, sv, s + UTF8SKIP(s), strend, flags, NULL);
2191 } /* end search for check string in unicode */
2193 if (s == startpos) {
2194 goto after_try_latin;
2197 if (regtry(®info, &s)) {
2204 if (prog->extflags & RXf_USE_INTUIT) {
2205 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2214 } /* end search for check string in latin*/
2215 } /* end search for check string */
2216 else { /* search for newline */
2218 /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
2221 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
2223 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2224 if (regtry(®info, &s))
2228 } /* end search for newline */
2229 } /* end anchored/multiline check string search */
2231 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2233 /* the warning about reginfo.ganch being used without initialization
2234 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2235 and we only enter this block when the same bit is set. */
2236 char *tmp_s = reginfo.ganch - prog->gofs;
2238 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2243 /* Messy cases: unanchored match. */
2244 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2245 /* we have /x+whatever/ */
2246 /* it must be a one character string (XXXX Except UTF_PATTERN?) */
2251 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2252 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2253 ch = SvPVX_const(utf8_target ? prog->anchored_utf8 : prog->anchored_substr)[0];
2258 DEBUG_EXECUTE_r( did_match = 1 );
2259 if (regtry(®info, &s)) goto got_it;
2261 while (s < strend && *s == ch)
2269 DEBUG_EXECUTE_r( did_match = 1 );
2270 if (regtry(®info, &s)) goto got_it;
2272 while (s < strend && *s == ch)
2277 DEBUG_EXECUTE_r(if (!did_match)
2278 PerlIO_printf(Perl_debug_log,
2279 "Did not find anchored character...\n")
2282 else if (prog->anchored_substr != NULL
2283 || prog->anchored_utf8 != NULL
2284 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2285 && prog->float_max_offset < strend - s)) {
2290 char *last1; /* Last position checked before */
2294 if (prog->anchored_substr || prog->anchored_utf8) {
2295 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2296 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2297 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
2298 back_max = back_min = prog->anchored_offset;
2300 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2301 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2302 must = utf8_target ? prog->float_utf8 : prog->float_substr;
2303 back_max = prog->float_max_offset;
2304 back_min = prog->float_min_offset;
2308 if (must == &PL_sv_undef)
2309 /* could not downgrade utf8 check substring, so must fail */
2315 last = HOP3c(strend, /* Cannot start after this */
2316 -(I32)(CHR_SVLEN(must)
2317 - (SvTAIL(must) != 0) + back_min), strbeg);
2320 last1 = HOPc(s, -1);
2322 last1 = s - 1; /* bogus */
2324 /* XXXX check_substr already used to find "s", can optimize if
2325 check_substr==must. */
2327 dontbother = end_shift;
2328 strend = HOPc(strend, -dontbother);
2329 while ( (s <= last) &&
2330 ((flags & REXEC_SCREAM)
2331 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2332 end_shift, &scream_pos, 0))
2333 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2334 (unsigned char*)strend, must,
2335 multiline ? FBMrf_MULTILINE : 0))) ) {
2336 /* we may be pointing at the wrong string */
2337 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2338 s = strbeg + (s - SvPVX_const(sv));
2339 DEBUG_EXECUTE_r( did_match = 1 );
2340 if (HOPc(s, -back_max) > last1) {
2341 last1 = HOPc(s, -back_min);
2342 s = HOPc(s, -back_max);
2345 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2347 last1 = HOPc(s, -back_min);
2351 while (s <= last1) {
2352 if (regtry(®info, &s))
2358 while (s <= last1) {
2359 if (regtry(®info, &s))
2365 DEBUG_EXECUTE_r(if (!did_match) {
2366 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
2367 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2368 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2369 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2370 ? "anchored" : "floating"),
2371 quoted, RE_SV_TAIL(must));
2375 else if ( (c = progi->regstclass) ) {
2377 const OPCODE op = OP(progi->regstclass);
2378 /* don't bother with what can't match */
2379 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2380 strend = HOPc(strend, -(minlen - 1));
2383 SV * const prop = sv_newmortal();
2384 regprop(prog, prop, c);
2386 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
2388 PerlIO_printf(Perl_debug_log,
2389 "Matching stclass %.*s against %s (%d bytes)\n",
2390 (int)SvCUR(prop), SvPVX_const(prop),
2391 quoted, (int)(strend - s));
2394 if (find_byclass(prog, c, s, strend, ®info))
2396 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2400 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2405 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2406 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2407 float_real = utf8_target ? prog->float_utf8 : prog->float_substr;
2409 if (flags & REXEC_SCREAM) {
2410 last = screaminstr(sv, float_real, s - strbeg,
2411 end_shift, &scream_pos, 1); /* last one */
2413 last = scream_olds; /* Only one occurrence. */
2414 /* we may be pointing at the wrong string */
2415 else if (RXp_MATCH_COPIED(prog))
2416 s = strbeg + (s - SvPVX_const(sv));
2420 const char * const little = SvPV_const(float_real, len);
2422 if (SvTAIL(float_real)) {
2423 if (memEQ(strend - len + 1, little, len - 1))
2424 last = strend - len + 1;
2425 else if (!multiline)
2426 last = memEQ(strend - len, little, len)
2427 ? strend - len : NULL;
2433 last = rninstr(s, strend, little, little + len);
2435 last = strend; /* matching "$" */
2440 PerlIO_printf(Perl_debug_log,
2441 "%sCan't trim the tail, match fails (should not happen)%s\n",
2442 PL_colors[4], PL_colors[5]));
2443 goto phooey; /* Should not happen! */
2445 dontbother = strend - last + prog->float_min_offset;
2447 if (minlen && (dontbother < minlen))
2448 dontbother = minlen - 1;
2449 strend -= dontbother; /* this one's always in bytes! */
2450 /* We don't know much -- general case. */
2453 if (regtry(®info, &s))
2462 if (regtry(®info, &s))
2464 } while (s++ < strend);
2473 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2475 if (PL_reg_eval_set)
2476 restore_pos(aTHX_ prog);
2477 if (RXp_PAREN_NAMES(prog))
2478 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2480 /* make sure $`, $&, $', and $digit will work later */
2481 if ( !(flags & REXEC_NOT_FIRST) ) {
2482 RX_MATCH_COPY_FREE(rx);
2483 if (flags & REXEC_COPY_STR) {
2484 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2485 #ifdef PERL_OLD_COPY_ON_WRITE
2487 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2489 PerlIO_printf(Perl_debug_log,
2490 "Copy on write: regexp capture, type %d\n",
2493 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2494 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2495 assert (SvPOKp(prog->saved_copy));
2499 RX_MATCH_COPIED_on(rx);
2500 s = savepvn(strbeg, i);
2506 prog->subbeg = strbeg;
2507 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2514 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2515 PL_colors[4], PL_colors[5]));
2516 if (PL_reg_eval_set)
2517 restore_pos(aTHX_ prog);
2519 /* we failed :-( roll it back */
2520 Safefree(prog->offs);
2529 - regtry - try match at specific point
2531 STATIC I32 /* 0 failure, 1 success */
2532 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2536 REGEXP *const rx = reginfo->prog;
2537 regexp *const prog = (struct regexp *)SvANY(rx);
2538 RXi_GET_DECL(prog,progi);
2539 GET_RE_DEBUG_FLAGS_DECL;
2541 PERL_ARGS_ASSERT_REGTRY;
2543 reginfo->cutpoint=NULL;
2545 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2548 PL_reg_eval_set = RS_init;
2549 DEBUG_EXECUTE_r(DEBUG_s(
2550 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2551 (IV)(PL_stack_sp - PL_stack_base));
2554 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2555 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2557 /* Apparently this is not needed, judging by wantarray. */
2558 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2559 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2562 /* Make $_ available to executed code. */
2563 if (reginfo->sv != DEFSV) {
2565 DEFSV_set(reginfo->sv);
2568 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2569 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2570 /* prepare for quick setting of pos */
2571 #ifdef PERL_OLD_COPY_ON_WRITE
2572 if (SvIsCOW(reginfo->sv))
2573 sv_force_normal_flags(reginfo->sv, 0);
2575 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2576 &PL_vtbl_mglob, NULL, 0);
2580 PL_reg_oldpos = mg->mg_len;
2581 SAVEDESTRUCTOR_X(restore_pos, prog);
2583 if (!PL_reg_curpm) {
2584 Newxz(PL_reg_curpm, 1, PMOP);
2587 SV* const repointer = &PL_sv_undef;
2588 /* this regexp is also owned by the new PL_reg_curpm, which
2589 will try to free it. */
2590 av_push(PL_regex_padav, repointer);
2591 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2592 PL_regex_pad = AvARRAY(PL_regex_padav);
2597 /* It seems that non-ithreads works both with and without this code.
2598 So for efficiency reasons it seems best not to have the code
2599 compiled when it is not needed. */
2600 /* This is safe against NULLs: */
2601 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2602 /* PM_reg_curpm owns a reference to this regexp. */
2605 PM_SETRE(PL_reg_curpm, rx);
2606 PL_reg_oldcurpm = PL_curpm;
2607 PL_curpm = PL_reg_curpm;
2608 if (RXp_MATCH_COPIED(prog)) {
2609 /* Here is a serious problem: we cannot rewrite subbeg,
2610 since it may be needed if this match fails. Thus
2611 $` inside (?{}) could fail... */
2612 PL_reg_oldsaved = prog->subbeg;
2613 PL_reg_oldsavedlen = prog->sublen;
2614 #ifdef PERL_OLD_COPY_ON_WRITE
2615 PL_nrs = prog->saved_copy;
2617 RXp_MATCH_COPIED_off(prog);
2620 PL_reg_oldsaved = NULL;
2621 prog->subbeg = PL_bostr;
2622 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2624 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2625 prog->offs[0].start = *startpos - PL_bostr;
2626 PL_reginput = *startpos;
2627 PL_reglastparen = &prog->lastparen;
2628 PL_reglastcloseparen = &prog->lastcloseparen;
2629 prog->lastparen = 0;
2630 prog->lastcloseparen = 0;
2632 PL_regoffs = prog->offs;
2633 if (PL_reg_start_tmpl <= prog->nparens) {
2634 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2635 if(PL_reg_start_tmp)
2636 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2638 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2641 /* XXXX What this code is doing here?!!! There should be no need
2642 to do this again and again, PL_reglastparen should take care of
2645 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2646 * Actually, the code in regcppop() (which Ilya may be meaning by
2647 * PL_reglastparen), is not needed at all by the test suite
2648 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2649 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2650 * Meanwhile, this code *is* needed for the
2651 * above-mentioned test suite tests to succeed. The common theme
2652 * on those tests seems to be returning null fields from matches.
2653 * --jhi updated by dapm */
2655 if (prog->nparens) {
2656 regexp_paren_pair *pp = PL_regoffs;
2658 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2666 if (regmatch(reginfo, progi->program + 1)) {
2667 PL_regoffs[0].end = PL_reginput - PL_bostr;
2670 if (reginfo->cutpoint)
2671 *startpos= reginfo->cutpoint;
2672 REGCP_UNWIND(lastcp);
2677 #define sayYES goto yes
2678 #define sayNO goto no
2679 #define sayNO_SILENT goto no_silent
2681 /* we dont use STMT_START/END here because it leads to
2682 "unreachable code" warnings, which are bogus, but distracting. */
2683 #define CACHEsayNO \
2684 if (ST.cache_mask) \
2685 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2688 /* this is used to determine how far from the left messages like
2689 'failed...' are printed. It should be set such that messages
2690 are inline with the regop output that created them.
2692 #define REPORT_CODE_OFF 32
2695 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2696 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2698 #define SLAB_FIRST(s) (&(s)->states[0])
2699 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2701 /* grab a new slab and return the first slot in it */
2703 STATIC regmatch_state *
2706 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2709 regmatch_slab *s = PL_regmatch_slab->next;
2711 Newx(s, 1, regmatch_slab);
2712 s->prev = PL_regmatch_slab;
2714 PL_regmatch_slab->next = s;
2716 PL_regmatch_slab = s;
2717 return SLAB_FIRST(s);
2721 /* push a new state then goto it */
2723 #define PUSH_STATE_GOTO(state, node) \
2725 st->resume_state = state; \
2728 /* push a new state with success backtracking, then goto it */
2730 #define PUSH_YES_STATE_GOTO(state, node) \
2732 st->resume_state = state; \
2733 goto push_yes_state;
2739 regmatch() - main matching routine
2741 This is basically one big switch statement in a loop. We execute an op,
2742 set 'next' to point the next op, and continue. If we come to a point which
2743 we may need to backtrack to on failure such as (A|B|C), we push a
2744 backtrack state onto the backtrack stack. On failure, we pop the top
2745 state, and re-enter the loop at the state indicated. If there are no more
2746 states to pop, we return failure.
2748 Sometimes we also need to backtrack on success; for example /A+/, where
2749 after successfully matching one A, we need to go back and try to
2750 match another one; similarly for lookahead assertions: if the assertion
2751 completes successfully, we backtrack to the state just before the assertion
2752 and then carry on. In these cases, the pushed state is marked as
2753 'backtrack on success too'. This marking is in fact done by a chain of
2754 pointers, each pointing to the previous 'yes' state. On success, we pop to
2755 the nearest yes state, discarding any intermediate failure-only states.
2756 Sometimes a yes state is pushed just to force some cleanup code to be
2757 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2758 it to free the inner regex.
2760 Note that failure backtracking rewinds the cursor position, while
2761 success backtracking leaves it alone.
2763 A pattern is complete when the END op is executed, while a subpattern
2764 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2765 ops trigger the "pop to last yes state if any, otherwise return true"
2768 A common convention in this function is to use A and B to refer to the two
2769 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2770 the subpattern to be matched possibly multiple times, while B is the entire
2771 rest of the pattern. Variable and state names reflect this convention.
2773 The states in the main switch are the union of ops and failure/success of
2774 substates associated with with that op. For example, IFMATCH is the op
2775 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2776 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2777 successfully matched A and IFMATCH_A_fail is a state saying that we have
2778 just failed to match A. Resume states always come in pairs. The backtrack
2779 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2780 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2781 on success or failure.
2783 The struct that holds a backtracking state is actually a big union, with
2784 one variant for each major type of op. The variable st points to the
2785 top-most backtrack struct. To make the code clearer, within each
2786 block of code we #define ST to alias the relevant union.
2788 Here's a concrete example of a (vastly oversimplified) IFMATCH
2794 #define ST st->u.ifmatch
2796 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2797 ST.foo = ...; // some state we wish to save
2799 // push a yes backtrack state with a resume value of
2800 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2802 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2805 case IFMATCH_A: // we have successfully executed A; now continue with B
2807 bar = ST.foo; // do something with the preserved value
2810 case IFMATCH_A_fail: // A failed, so the assertion failed
2811 ...; // do some housekeeping, then ...
2812 sayNO; // propagate the failure
2819 For any old-timers reading this who are familiar with the old recursive
2820 approach, the code above is equivalent to:
2822 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2831 ...; // do some housekeeping, then ...
2832 sayNO; // propagate the failure
2835 The topmost backtrack state, pointed to by st, is usually free. If you
2836 want to claim it, populate any ST.foo fields in it with values you wish to
2837 save, then do one of
2839 PUSH_STATE_GOTO(resume_state, node);
2840 PUSH_YES_STATE_GOTO(resume_state, node);
2842 which sets that backtrack state's resume value to 'resume_state', pushes a
2843 new free entry to the top of the backtrack stack, then goes to 'node'.
2844 On backtracking, the free slot is popped, and the saved state becomes the
2845 new free state. An ST.foo field in this new top state can be temporarily
2846 accessed to retrieve values, but once the main loop is re-entered, it
2847 becomes available for reuse.
2849 Note that the depth of the backtrack stack constantly increases during the
2850 left-to-right execution of the pattern, rather than going up and down with
2851 the pattern nesting. For example the stack is at its maximum at Z at the
2852 end of the pattern, rather than at X in the following:
2854 /(((X)+)+)+....(Y)+....Z/
2856 The only exceptions to this are lookahead/behind assertions and the cut,
2857 (?>A), which pop all the backtrack states associated with A before
2860 Backtrack state structs are allocated in slabs of about 4K in size.
2861 PL_regmatch_state and st always point to the currently active state,
2862 and PL_regmatch_slab points to the slab currently containing
2863 PL_regmatch_state. The first time regmatch() is called, the first slab is
2864 allocated, and is never freed until interpreter destruction. When the slab
2865 is full, a new one is allocated and chained to the end. At exit from
2866 regmatch(), slabs allocated since entry are freed.
2871 #define DEBUG_STATE_pp(pp) \
2873 DUMP_EXEC_POS(locinput, scan, utf8_target); \
2874 PerlIO_printf(Perl_debug_log, \
2875 " %*s"pp" %s%s%s%s%s\n", \
2877 PL_reg_name[st->resume_state], \
2878 ((st==yes_state||st==mark_state) ? "[" : ""), \
2879 ((st==yes_state) ? "Y" : ""), \
2880 ((st==mark_state) ? "M" : ""), \
2881 ((st==yes_state||st==mark_state) ? "]" : "") \
2886 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2891 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
2892 const char *start, const char *end, const char *blurb)
2894 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2896 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2901 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2902 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2904 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
2905 start, end - start, 60);
2907 PerlIO_printf(Perl_debug_log,
2908 "%s%s REx%s %s against %s\n",
2909 PL_colors[4], blurb, PL_colors[5], s0, s1);
2911 if (utf8_target||utf8_pat)
2912 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2913 utf8_pat ? "pattern" : "",
2914 utf8_pat && utf8_target ? " and " : "",
2915 utf8_target ? "string" : ""
2921 S_dump_exec_pos(pTHX_ const char *locinput,
2922 const regnode *scan,
2923 const char *loc_regeol,
2924 const char *loc_bostr,
2925 const char *loc_reg_starttry,
2926 const bool utf8_target)
2928 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2929 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2930 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2931 /* The part of the string before starttry has one color
2932 (pref0_len chars), between starttry and current
2933 position another one (pref_len - pref0_len chars),
2934 after the current position the third one.
2935 We assume that pref0_len <= pref_len, otherwise we
2936 decrease pref0_len. */
2937 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2938 ? (5 + taill) - l : locinput - loc_bostr;
2941 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2943 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2945 pref0_len = pref_len - (locinput - loc_reg_starttry);
2946 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2947 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2948 ? (5 + taill) - pref_len : loc_regeol - locinput);
2949 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2953 if (pref0_len > pref_len)
2954 pref0_len = pref_len;
2956 const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
2958 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2959 (locinput - pref_len),pref0_len, 60, 4, 5);
2961 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2962 (locinput - pref_len + pref0_len),
2963 pref_len - pref0_len, 60, 2, 3);
2965 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2966 locinput, loc_regeol - locinput, 10, 0, 1);
2968 const STRLEN tlen=len0+len1+len2;
2969 PerlIO_printf(Perl_debug_log,
2970 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2971 (IV)(locinput - loc_bostr),
2974 (docolor ? "" : "> <"),
2976 (int)(tlen > 19 ? 0 : 19 - tlen),
2983 /* reg_check_named_buff_matched()
2984 * Checks to see if a named buffer has matched. The data array of
2985 * buffer numbers corresponding to the buffer is expected to reside
2986 * in the regexp->data->data array in the slot stored in the ARG() of
2987 * node involved. Note that this routine doesn't actually care about the
2988 * name, that information is not preserved from compilation to execution.
2989 * Returns the index of the leftmost defined buffer with the given name
2990 * or 0 if non of the buffers matched.
2993 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2996 RXi_GET_DECL(rex,rexi);
2997 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2998 I32 *nums=(I32*)SvPVX(sv_dat);
3000 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
3002 for ( n=0; n<SvIVX(sv_dat); n++ ) {
3003 if ((I32)*PL_reglastparen >= nums[n] &&
3004 PL_regoffs[nums[n]].end != -1)
3013 /* free all slabs above current one - called during LEAVE_SCOPE */
3016 S_clear_backtrack_stack(pTHX_ void *p)
3018 regmatch_slab *s = PL_regmatch_slab->next;
3023 PL_regmatch_slab->next = NULL;
3025 regmatch_slab * const osl = s;
3032 #define SETREX(Re1,Re2) \
3033 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
3036 STATIC I32 /* 0 failure, 1 success */
3037 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
3039 #if PERL_VERSION < 9 && !defined(PERL_CORE)
3043 register const bool utf8_target = PL_reg_match_utf8;
3044 const U32 uniflags = UTF8_ALLOW_DEFAULT;
3045 REGEXP *rex_sv = reginfo->prog;
3046 regexp *rex = (struct regexp *)SvANY(rex_sv);
3047 RXi_GET_DECL(rex,rexi);
3049 /* the current state. This is a cached copy of PL_regmatch_state */
3050 register regmatch_state *st;
3051 /* cache heavy used fields of st in registers */
3052 register regnode *scan;
3053 register regnode *next;
3054 register U32 n = 0; /* general value; init to avoid compiler warning */
3055 register I32 ln = 0; /* len or last; init to avoid compiler warning */
3056 register char *locinput = PL_reginput;
3057 register I32 nextchr; /* is always set to UCHARAT(locinput) */
3059 bool result = 0; /* return value of S_regmatch */
3060 int depth = 0; /* depth of backtrack stack */
3061 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
3062 const U32 max_nochange_depth =
3063 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
3064 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
3065 regmatch_state *yes_state = NULL; /* state to pop to on success of
3067 /* mark_state piggy backs on the yes_state logic so that when we unwind
3068 the stack on success we can update the mark_state as we go */
3069 regmatch_state *mark_state = NULL; /* last mark state we have seen */
3070 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
3071 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
3073 bool no_final = 0; /* prevent failure from backtracking? */
3074 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
3075 char *startpoint = PL_reginput;
3076 SV *popmark = NULL; /* are we looking for a mark? */
3077 SV *sv_commit = NULL; /* last mark name seen in failure */
3078 SV *sv_yes_mark = NULL; /* last mark name we have seen
3079 during a successful match */
3080 U32 lastopen = 0; /* last open we saw */
3081 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
3082 SV* const oreplsv = GvSV(PL_replgv);
3083 /* these three flags are set by various ops to signal information to
3084 * the very next op. They have a useful lifetime of exactly one loop
3085 * iteration, and are not preserved or restored by state pushes/pops
3087 bool sw = 0; /* the condition value in (?(cond)a|b) */
3088 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
3089 int logical = 0; /* the following EVAL is:
3093 or the following IFMATCH/UNLESSM is:
3094 false: plain (?=foo)
3095 true: used as a condition: (?(?=foo))
3098 GET_RE_DEBUG_FLAGS_DECL;
3101 PERL_ARGS_ASSERT_REGMATCH;
3103 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
3104 PerlIO_printf(Perl_debug_log,"regmatch start\n");
3106 /* on first ever call to regmatch, allocate first slab */
3107 if (!PL_regmatch_slab) {
3108 Newx(PL_regmatch_slab, 1, regmatch_slab);
3109 PL_regmatch_slab->prev = NULL;
3110 PL_regmatch_slab->next = NULL;
3111 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
3114 oldsave = PL_savestack_ix;
3115 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
3116 SAVEVPTR(PL_regmatch_slab);
3117 SAVEVPTR(PL_regmatch_state);
3119 /* grab next free state slot */
3120 st = ++PL_regmatch_state;
3121 if (st > SLAB_LAST(PL_regmatch_slab))
3122 st = PL_regmatch_state = S_push_slab(aTHX);
3124 /* Note that nextchr is a byte even in UTF */
3125 nextchr = UCHARAT(locinput);
3127 while (scan != NULL) {
3130 SV * const prop = sv_newmortal();
3131 regnode *rnext=regnext(scan);
3132 DUMP_EXEC_POS( locinput, scan, utf8_target );
3133 regprop(rex, prop, scan);
3135 PerlIO_printf(Perl_debug_log,
3136 "%3"IVdf":%*s%s(%"IVdf")\n",
3137 (IV)(scan - rexi->program), depth*2, "",
3139 (PL_regkind[OP(scan)] == END || !rnext) ?
3140 0 : (IV)(rnext - rexi->program));
3143 next = scan + NEXT_OFF(scan);
3146 state_num = OP(scan);
3150 assert(PL_reglastparen == &rex->lastparen);
3151 assert(PL_reglastcloseparen == &rex->lastcloseparen);
3152 assert(PL_regoffs == rex->offs);
3154 switch (state_num) {
3156 if (locinput == PL_bostr)
3158 /* reginfo->till = reginfo->bol; */
3163 if (locinput == PL_bostr ||
3164 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
3170 if (locinput == PL_bostr)
3174 if (locinput == reginfo->ganch)
3179 /* update the startpoint */
3180 st->u.keeper.val = PL_regoffs[0].start;
3181 PL_reginput = locinput;
3182 PL_regoffs[0].start = locinput - PL_bostr;
3183 PUSH_STATE_GOTO(KEEPS_next, next);
3185 case KEEPS_next_fail:
3186 /* rollback the start point change */
3187 PL_regoffs[0].start = st->u.keeper.val;
3193 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3198 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3200 if (PL_regeol - locinput > 1)
3204 if (PL_regeol != locinput)
3208 if (!nextchr && locinput >= PL_regeol)
3211 locinput += PL_utf8skip[nextchr];
3212 if (locinput > PL_regeol)
3214 nextchr = UCHARAT(locinput);
3217 nextchr = UCHARAT(++locinput);
3220 if (!nextchr && locinput >= PL_regeol)
3222 nextchr = UCHARAT(++locinput);
3225 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3228 locinput += PL_utf8skip[nextchr];
3229 if (locinput > PL_regeol)
3231 nextchr = UCHARAT(locinput);
3234 nextchr = UCHARAT(++locinput);
3238 #define ST st->u.trie
3240 /* In this case the charclass data is available inline so
3241 we can fail fast without a lot of extra overhead.
3243 if (scan->flags == EXACT || !utf8_target) {
3244 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3246 PerlIO_printf(Perl_debug_log,
3247 "%*s %sfailed to match trie start class...%s\n",
3248 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3256 /* the basic plan of execution of the trie is:
3257 * At the beginning, run though all the states, and
3258 * find the longest-matching word. Also remember the position
3259 * of the shortest matching word. For example, this pattern:
3262 * when matched against the string "abcde", will generate
3263 * accept states for all words except 3, with the longest
3264 * matching word being 4, and the shortest being 1 (with
3265 * the position being after char 1 of the string).
3267 * Then for each matching word, in word order (i.e. 1,2,4,5),
3268 * we run the remainder of the pattern; on each try setting
3269 * the current position to the character following the word,
3270 * returning to try the next word on failure.
3272 * We avoid having to build a list of words at runtime by
3273 * using a compile-time structure, wordinfo[].prev, which
3274 * gives, for each word, the previous accepting word (if any).
3275 * In the case above it would contain the mappings 1->2, 2->0,
3276 * 3->0, 4->5, 5->1. We can use this table to generate, from
3277 * the longest word (4 above), a list of all words, by
3278 * following the list of prev pointers; this gives us the
3279 * unordered list 4,5,1,2. Then given the current word we have
3280 * just tried, we can go through the list and find the
3281 * next-biggest word to try (so if we just failed on word 2,
3282 * the next in the list is 4).
3284 * Since at runtime we don't record the matching position in
3285 * the string for each word, we have to work that out for
3286 * each word we're about to process. The wordinfo table holds
3287 * the character length of each word; given that we recorded
3288 * at the start: the position of the shortest word and its
3289 * length in chars, we just need to move the pointer the
3290 * difference between the two char lengths. Depending on
3291 * Unicode status and folding, that's cheap or expensive.
3293 * This algorithm is optimised for the case where are only a
3294 * small number of accept states, i.e. 0,1, or maybe 2.
3295 * With lots of accepts states, and having to try all of them,
3296 * it becomes quadratic on number of accept states to find all
3301 /* what type of TRIE am I? (utf8 makes this contextual) */
3302 DECL_TRIE_TYPE(scan);
3304 /* what trie are we using right now */
3305 reg_trie_data * const trie
3306 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3307 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3308 U32 state = trie->startstate;
3310 if (trie->bitmap && trie_type != trie_utf8_fold &&
3311 !TRIE_BITMAP_TEST(trie,*locinput)
3313 if (trie->states[ state ].wordnum) {
3315 PerlIO_printf(Perl_debug_log,
3316 "%*s %smatched empty string...%s\n",
3317 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3323 PerlIO_printf(Perl_debug_log,
3324 "%*s %sfailed to match trie start class...%s\n",
3325 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3332 U8 *uc = ( U8* )locinput;
3336 U8 *uscan = (U8*)NULL;
3337 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3338 U32 charcount = 0; /* how many input chars we have matched */
3339 U32 accepted = 0; /* have we seen any accepting states? */
3342 ST.jump = trie->jump;
3345 ST.longfold = FALSE; /* char longer if folded => it's harder */
3348 /* fully traverse the TRIE; note the position of the
3349 shortest accept state and the wordnum of the longest
3352 while ( state && uc <= (U8*)PL_regeol ) {
3353 U32 base = trie->states[ state ].trans.base;
3357 wordnum = trie->states[ state ].wordnum;
3359 if (wordnum) { /* it's an accept state */
3362 /* record first match position */
3364 ST.firstpos = (U8*)locinput;
3369 ST.firstchars = charcount;
3372 if (!ST.nextword || wordnum < ST.nextword)
3373 ST.nextword = wordnum;
3374 ST.topword = wordnum;
3377 DEBUG_TRIE_EXECUTE_r({
3378 DUMP_EXEC_POS( (char *)uc, scan, utf8_target );
3379 PerlIO_printf( Perl_debug_log,
3380 "%*s %sState: %4"UVxf" Accepted: %c ",
3381 2+depth * 2, "", PL_colors[4],
3382 (UV)state, (accepted ? 'Y' : 'N'));
3385 /* read a char and goto next state */
3388 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3389 uscan, len, uvc, charid, foldlen,
3396 base + charid - 1 - trie->uniquecharcount)) >= 0)
3398 && ((U32)offset < trie->lasttrans)
3399 && trie->trans[offset].check == state)
3401 state = trie->trans[offset].next;
3412 DEBUG_TRIE_EXECUTE_r(
3413 PerlIO_printf( Perl_debug_log,
3414 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3415 charid, uvc, (UV)state, PL_colors[5] );
3421 /* calculate total number of accept states */
3426 w = trie->wordinfo[w].prev;
3429 ST.accepted = accepted;
3433 PerlIO_printf( Perl_debug_log,
3434 "%*s %sgot %"IVdf" possible matches%s\n",
3435 REPORT_CODE_OFF + depth * 2, "",
3436 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3438 goto trie_first_try; /* jump into the fail handler */
3442 case TRIE_next_fail: /* we failed - try next alternative */
3444 REGCP_UNWIND(ST.cp);
3445 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3446 PL_regoffs[n].end = -1;
3447 *PL_reglastparen = n;
3449 if (!--ST.accepted) {
3451 PerlIO_printf( Perl_debug_log,
3452 "%*s %sTRIE failed...%s\n",
3453 REPORT_CODE_OFF+depth*2, "",
3460 /* Find next-highest word to process. Note that this code
3461 * is O(N^2) per trie run (O(N) per branch), so keep tight */
3462 register U16 min = 0;
3464 register U16 const nextword = ST.nextword;
3465 register reg_trie_wordinfo * const wordinfo
3466 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3467 for (word=ST.topword; word; word=wordinfo[word].prev) {
3468 if (word > nextword && (!min || word < min))
3481 ST.lastparen = *PL_reglastparen;
3485 /* find start char of end of current word */
3487 U32 chars; /* how many chars to skip */
3488 U8 *uc = ST.firstpos;
3489 reg_trie_data * const trie
3490 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3492 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3494 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3498 /* the hard option - fold each char in turn and find
3499 * its folded length (which may be different */
3500 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3508 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3516 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3521 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3535 PL_reginput = (char *)uc;
3538 scan = (ST.jump && ST.jump[ST.nextword])
3539 ? ST.me + ST.jump[ST.nextword]
3543 PerlIO_printf( Perl_debug_log,
3544 "%*s %sTRIE matched word #%d, continuing%s\n",
3545 REPORT_CODE_OFF+depth*2, "",
3552 if (ST.accepted > 1 || has_cutgroup) {
3553 PUSH_STATE_GOTO(TRIE_next, scan);
3556 /* only one choice left - just continue */
3558 AV *const trie_words
3559 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3560 SV ** const tmp = av_fetch( trie_words,
3562 SV *sv= tmp ? sv_newmortal() : NULL;
3564 PerlIO_printf( Perl_debug_log,
3565 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3566 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3568 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3569 PL_colors[0], PL_colors[1],
3570 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)|PERL_PV_ESCAPE_NONASCII
3572 : "not compiled under -Dr",
3576 locinput = PL_reginput;
3577 nextchr = UCHARAT(locinput);
3578 continue; /* execute rest of RE */
3583 char *s = STRING(scan);
3585 if (utf8_target != UTF_PATTERN) {
3586 /* The target and the pattern have differing utf8ness. */
3588 const char * const e = s + ln;
3591 /* The target is utf8, the pattern is not utf8. */
3596 if (NATIVE_TO_UNI(*(U8*)s) !=
3597 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3605 /* The target is not utf8, the pattern is utf8. */
3610 if (NATIVE_TO_UNI(*((U8*)l)) !=
3611 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3619 nextchr = UCHARAT(locinput);
3622 /* The target and the pattern have the same utf8ness. */
3623 /* Inline the first character, for speed. */
3624 if (UCHARAT(s) != nextchr)
3626 if (PL_regeol - locinput < ln)
3628 if (ln > 1 && memNE(s, locinput, ln))
3631 nextchr = UCHARAT(locinput);
3636 const U8 * fold_array;
3638 U32 fold_utf8_flags;
3640 PL_reg_flags |= RF_tainted;
3641 folder = foldEQ_locale;
3642 fold_array = PL_fold_locale;
3643 fold_utf8_flags = FOLDEQ_UTF8_LOCALE;
3647 folder = foldEQ_latin1;
3648 fold_array = PL_fold_latin1;
3649 fold_utf8_flags = 0;
3653 folder = foldEQ_latin1;
3654 fold_array = PL_fold_latin1;
3655 fold_utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
3660 fold_array = PL_fold;
3661 fold_utf8_flags = 0;
3667 if (utf8_target || UTF_PATTERN) {
3668 /* Either target or the pattern are utf8. */
3669 const char * const l = locinput;
3670 char *e = PL_regeol;
3672 if (! foldEQ_utf8_flags(s, 0, ln, cBOOL(UTF_PATTERN),
3673 l, &e, 0, utf8_target, fold_utf8_flags)) {
3674 /* One more case for the sharp s:
3675 * pack("U0U*", 0xDF) =~ /ss/i,
3676 * the 0xC3 0x9F are the UTF-8
3677 * byte sequence for the U+00DF. */
3679 if (!(utf8_target &&
3680 toLOWER(s[0]) == 's' &&
3682 toLOWER(s[1]) == 's' &&
3689 nextchr = UCHARAT(locinput);
3693 /* Neither the target nor the pattern are utf8 */
3694 if (UCHARAT(s) != nextchr &&
3695 UCHARAT(s) != fold_array[nextchr])
3699 if (PL_regeol - locinput < ln)
3701 if (ln > 1 && ! folder(s, locinput, ln))
3704 nextchr = UCHARAT(locinput);
3708 /* XXX Could improve efficiency by separating these all out using a
3709 * macro or in-line function. At that point regcomp.c would no longer
3710 * have to set the FLAGS fields of these */
3713 PL_reg_flags |= RF_tainted;
3721 /* was last char in word? */
3722 if (utf8_target && FLAGS(scan) != REGEX_ASCII_RESTRICTED_CHARSET) {
3723 if (locinput == PL_bostr)
3726 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3728 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3730 if (FLAGS(scan) != REGEX_LOCALE_CHARSET) {
3731 ln = isALNUM_uni(ln);
3732 LOAD_UTF8_CHARCLASS_ALNUM();
3733 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, utf8_target);
3736 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3737 n = isALNUM_LC_utf8((U8*)locinput);
3742 /* Here the string isn't utf8, or is utf8 and only ascii
3743 * characters are to match \w. In the latter case looking at
3744 * the byte just prior to the current one may be just the final
3745 * byte of a multi-byte character. This is ok. There are two
3747 * 1) it is a single byte character, and then the test is doing
3748 * just what it's supposed to.
3749 * 2) it is a multi-byte character, in which case the final
3750 * byte is never mistakable for ASCII, and so the test
3751 * will say it is not a word character, which is the
3752 * correct answer. */
3753 ln = (locinput != PL_bostr) ?
3754 UCHARAT(locinput - 1) : '\n';
3755 switch (FLAGS(scan)) {
3756 case REGEX_UNICODE_CHARSET:
3757 ln = isWORDCHAR_L1(ln);
3758 n = isWORDCHAR_L1(nextchr);
3760 case REGEX_LOCALE_CHARSET:
3761 ln = isALNUM_LC(ln);
3762 n = isALNUM_LC(nextchr);
3764 case REGEX_DEPENDS_CHARSET:
3766 n = isALNUM(nextchr);
3768 case REGEX_ASCII_RESTRICTED_CHARSET:
3769 ln = isWORDCHAR_A(ln);
3770 n = isWORDCHAR_A(nextchr);
3773 Perl_croak(aTHX_ "panic: Unexpected FLAGS %u in op %u", FLAGS(scan), OP(scan));
3777 /* Note requires that all BOUNDs be lower than all NBOUNDs in
3779 if (((!ln) == (!n)) == (OP(scan) < NBOUND))
3784 if (utf8_target || state_num == ANYOFV) {
3785 STRLEN inclasslen = PL_regeol - locinput;
3786 if (locinput >= PL_regeol)
3789 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, utf8_target))
3791 locinput += inclasslen;
3792 nextchr = UCHARAT(locinput);
3797 nextchr = UCHARAT(locinput);
3798 if (!nextchr && locinput >= PL_regeol)
3800 if (!REGINCLASS(rex, scan, (U8*)locinput))
3802 nextchr = UCHARAT(++locinput);
3806 /* Special char classes - The defines start on line 129 or so */
3807 CCC_TRY_U(ALNUM, NALNUM, isWORDCHAR,
3808 ALNUML, NALNUML, isALNUM_LC, isALNUM_LC_utf8,
3809 ALNUMU, NALNUMU, isWORDCHAR_L1,
3810 ALNUMA, NALNUMA, isWORDCHAR_A,
3813 CCC_TRY_U(SPACE, NSPACE, isSPACE,
3814 SPACEL, NSPACEL, isSPACE_LC, isSPACE_LC_utf8,
3815 SPACEU, NSPACEU, isSPACE_L1,
3816 SPACEA, NSPACEA, isSPACE_A,
3819 CCC_TRY(DIGIT, NDIGIT, isDIGIT,
3820 DIGITL, NDIGITL, isDIGIT_LC, isDIGIT_LC_utf8,
3821 DIGITA, NDIGITA, isDIGIT_A,
3824 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3825 a Unicode extended Grapheme Cluster */
3826 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3827 extended Grapheme Cluster is:
3830 | Prepend* Begin Extend*
3833 Begin is (Hangul-syllable | ! Control)
3834 Extend is (Grapheme_Extend | Spacing_Mark)
3835 Control is [ GCB_Control CR LF ]
3837 The discussion below shows how the code for CLUMP is derived
3838 from this regex. Note that most of these concepts are from
3839 property values of the Grapheme Cluster Boundary (GCB) property.
3840 No code point can have multiple property values for a given
3841 property. Thus a code point in Prepend can't be in Control, but
3842 it must be in !Control. This is why Control above includes
3843 GCB_Control plus CR plus LF. The latter two are used in the GCB
3844 property separately, and so can't be in GCB_Control, even though
3845 they logically are controls. Control is not the same as gc=cc,
3846 but includes format and other characters as well.
3848 The Unicode definition of Hangul-syllable is:
3850 | (L* ( ( V | LV ) V* | LVT ) T*)
3853 Each of these is a value for the GCB property, and hence must be
3854 disjoint, so the order they are tested is immaterial, so the
3855 above can safely be changed to
3858 | (L* ( LVT | ( V | LV ) V*) T*)
3860 The last two terms can be combined like this:
3862 | (( LVT | ( V | LV ) V*) T*))
3864 And refactored into this:
3865 L* (L | LVT T* | V V* T* | LV V* T*)
3867 That means that if we have seen any L's at all we can quit
3868 there, but if the next character is a LVT, a V or and LV we
3871 There is a subtlety with Prepend* which showed up in testing.
3872 Note that the Begin, and only the Begin is required in:
3873 | Prepend* Begin Extend*
3874 Also, Begin contains '! Control'. A Prepend must be a '!
3875 Control', which means it must be a Begin. What it comes down to
3876 is that if we match Prepend* and then find no suitable Begin
3877 afterwards, that if we backtrack the last Prepend, that one will
3878 be a suitable Begin.
3881 if (locinput >= PL_regeol)
3883 if (! utf8_target) {
3885 /* Match either CR LF or '.', as all the other possibilities
3887 locinput++; /* Match the . or CR */
3889 && locinput < PL_regeol
3890 && UCHARAT(locinput) == '\n') locinput++;
3894 /* Utf8: See if is ( CR LF ); already know that locinput <
3895 * PL_regeol, so locinput+1 is in bounds */
3896 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3900 /* In case have to backtrack to beginning, then match '.' */
3901 char *starting = locinput;
3903 /* In case have to backtrack the last prepend */
3904 char *previous_prepend = 0;
3906 LOAD_UTF8_CHARCLASS_GCB();
3908 /* Match (prepend)* */
3909 while (locinput < PL_regeol
3910 && swash_fetch(PL_utf8_X_prepend,
3911 (U8*)locinput, utf8_target))
3913 previous_prepend = locinput;
3914 locinput += UTF8SKIP(locinput);
3917 /* As noted above, if we matched a prepend character, but
3918 * the next thing won't match, back off the last prepend we
3919 * matched, as it is guaranteed to match the begin */
3920 if (previous_prepend
3921 && (locinput >= PL_regeol
3922 || ! swash_fetch(PL_utf8_X_begin,
3923 (U8*)locinput, utf8_target)))
3925 locinput = previous_prepend;
3928 /* Note that here we know PL_regeol > locinput, as we
3929 * tested that upon input to this switch case, and if we
3930 * moved locinput forward, we tested the result just above
3931 * and it either passed, or we backed off so that it will
3933 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, utf8_target)) {
3935 /* Here did not match the required 'Begin' in the
3936 * second term. So just match the very first
3937 * character, the '.' of the final term of the regex */
3938 locinput = starting + UTF8SKIP(starting);
3941 /* Here is the beginning of a character that can have
3942 * an extender. It is either a hangul syllable, or a
3944 if (swash_fetch(PL_utf8_X_non_hangul,
3945 (U8*)locinput, utf8_target))
3948 /* Here not a Hangul syllable, must be a
3949 * ('! * Control') */
3950 locinput += UTF8SKIP(locinput);
3953 /* Here is a Hangul syllable. It can be composed
3954 * of several individual characters. One
3955 * possibility is T+ */
3956 if (swash_fetch(PL_utf8_X_T,
3957 (U8*)locinput, utf8_target))
3959 while (locinput < PL_regeol
3960 && swash_fetch(PL_utf8_X_T,
3961 (U8*)locinput, utf8_target))
3963 locinput += UTF8SKIP(locinput);
3967 /* Here, not T+, but is a Hangul. That means
3968 * it is one of the others: L, LV, LVT or V,
3970 * L* (L | LVT T* | V V* T* | LV V* T*) */
3973 while (locinput < PL_regeol
3974 && swash_fetch(PL_utf8_X_L,
3975 (U8*)locinput, utf8_target))
3977 locinput += UTF8SKIP(locinput);
3980 /* Here, have exhausted L*. If the next
3981 * character is not an LV, LVT nor V, it means
3982 * we had to have at least one L, so matches L+
3983 * in the original equation, we have a complete
3984 * hangul syllable. Are done. */
3986 if (locinput < PL_regeol
3987 && swash_fetch(PL_utf8_X_LV_LVT_V,
3988 (U8*)locinput, utf8_target))
3991 /* Otherwise keep going. Must be LV, LVT
3992 * or V. See if LVT */
3993 if (swash_fetch(PL_utf8_X_LVT,
3994 (U8*)locinput, utf8_target))
3996 locinput += UTF8SKIP(locinput);
3999 /* Must be V or LV. Take it, then
4001 locinput += UTF8SKIP(locinput);
4002 while (locinput < PL_regeol
4003 && swash_fetch(PL_utf8_X_V,
4004 (U8*)locinput, utf8_target))
4006 locinput += UTF8SKIP(locinput);
4010 /* And any of LV, LVT, or V can be followed
4012 while (locinput < PL_regeol
4013 && swash_fetch(PL_utf8_X_T,
4017 locinput += UTF8SKIP(locinput);
4023 /* Match any extender */
4024 while (locinput < PL_regeol
4025 && swash_fetch(PL_utf8_X_extend,
4026 (U8*)locinput, utf8_target))
4028 locinput += UTF8SKIP(locinput);
4032 if (locinput > PL_regeol) sayNO;
4034 nextchr = UCHARAT(locinput);
4038 { /* The capture buffer cases. The ones beginning with N for the
4039 named buffers just convert to the equivalent numbered and
4040 pretend they were called as the corresponding numbered buffer
4042 /* don't initialize these in the declaration, it makes C++
4047 const U8 *fold_array;
4050 PL_reg_flags |= RF_tainted;
4051 folder = foldEQ_locale;
4052 fold_array = PL_fold_locale;
4054 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
4058 folder = foldEQ_latin1;
4059 fold_array = PL_fold_latin1;
4061 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
4065 folder = foldEQ_latin1;
4066 fold_array = PL_fold_latin1;
4068 utf8_fold_flags = 0;
4073 fold_array = PL_fold;
4075 utf8_fold_flags = 0;
4082 utf8_fold_flags = 0;
4085 /* For the named back references, find the corresponding buffer
4087 n = reg_check_named_buff_matched(rex,scan);
4092 goto do_nref_ref_common;
4095 PL_reg_flags |= RF_tainted;
4096 folder = foldEQ_locale;
4097 fold_array = PL_fold_locale;
4098 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
4102 folder = foldEQ_latin1;
4103 fold_array = PL_fold_latin1;
4104 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
4108 folder = foldEQ_latin1;
4109 fold_array = PL_fold_latin1;
4110 utf8_fold_flags = 0;
4115 fold_array = PL_fold;
4116 utf8_fold_flags = 0;
4122 utf8_fold_flags = 0;
4126 n = ARG(scan); /* which paren pair */
4129 ln = PL_regoffs[n].start;
4130 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4131 if (*PL_reglastparen < n || ln == -1)
4132 sayNO; /* Do not match unless seen CLOSEn. */
4133 if (ln == PL_regoffs[n].end)
4137 if (type != REF /* REF can do byte comparison */
4138 && (utf8_target || type == REFFU))
4139 { /* XXX handle REFFL better */
4140 char * limit = PL_regeol;
4142 /* This call case insensitively compares the entire buffer
4143 * at s, with the current input starting at locinput, but
4144 * not going off the end given by PL_regeol, and returns in
4145 * limit upon success, how much of the current input was
4147 if (! foldEQ_utf8_flags(s, NULL, PL_regoffs[n].end - ln, utf8_target,
4148 locinput, &limit, 0, utf8_target, utf8_fold_flags))
4153 nextchr = UCHARAT(locinput);
4157 /* Not utf8: Inline the first character, for speed. */
4158 if (UCHARAT(s) != nextchr &&
4160 UCHARAT(s) != fold_array[nextchr]))
4162 ln = PL_regoffs[n].end - ln;
4163 if (locinput + ln > PL_regeol)
4165 if (ln > 1 && (type == REF
4166 ? memNE(s, locinput, ln)
4167 : ! folder(s, locinput, ln)))
4170 nextchr = UCHARAT(locinput);
4180 #define ST st->u.eval
4185 regexp_internal *rei;
4186 regnode *startpoint;
4189 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
4190 if (cur_eval && cur_eval->locinput==locinput) {
4191 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
4192 Perl_croak(aTHX_ "Infinite recursion in regex");
4193 if ( ++nochange_depth > max_nochange_depth )
4195 "Pattern subroutine nesting without pos change"
4196 " exceeded limit in regex");
4203 (void)ReREFCNT_inc(rex_sv);
4204 if (OP(scan)==GOSUB) {
4205 startpoint = scan + ARG2L(scan);
4206 ST.close_paren = ARG(scan);
4208 startpoint = rei->program+1;
4211 goto eval_recurse_doit;
4213 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
4214 if (cur_eval && cur_eval->locinput==locinput) {
4215 if ( ++nochange_depth > max_nochange_depth )
4216 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
4221 /* execute the code in the {...} */
4223 SV ** const before = SP;
4224 OP_4tree * const oop = PL_op;
4225 COP * const ocurcop = PL_curcop;
4227 char *saved_regeol = PL_regeol;
4228 struct re_save_state saved_state;
4230 /* To not corrupt the existing regex state while executing the
4231 * eval we would normally put it on the save stack, like with
4232 * save_re_context. However, re-evals have a weird scoping so we
4233 * can't just add ENTER/LEAVE here. With that, things like
4235 * (?{$a=2})(a(?{local$a=$a+1}))*aak*c(?{$b=$a})
4237 * would break, as they expect the localisation to be unwound
4238 * only when the re-engine backtracks through the bit that
4241 * What we do instead is just saving the state in a local c
4244 Copy(&PL_reg_state, &saved_state, 1, struct re_save_state);
4247 PL_op = (OP_4tree*)rexi->data->data[n];
4248 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
4249 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
4250 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
4251 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
4254 SV *sv_mrk = get_sv("REGMARK", 1);
4255 sv_setsv(sv_mrk, sv_yes_mark);
4258 CALLRUNOPS(aTHX); /* Scalar context. */
4261 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
4267 Copy(&saved_state, &PL_reg_state, 1, struct re_save_state);
4270 PAD_RESTORE_LOCAL(old_comppad);
4271 PL_curcop = ocurcop;
4272 PL_regeol = saved_regeol;
4275 sv_setsv(save_scalar(PL_replgv), ret);
4279 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
4282 /* extract RE object from returned value; compiling if
4288 SV *const sv = SvRV(ret);
4290 if (SvTYPE(sv) == SVt_REGEXP) {
4292 } else if (SvSMAGICAL(sv)) {
4293 mg = mg_find(sv, PERL_MAGIC_qr);
4296 } else if (SvTYPE(ret) == SVt_REGEXP) {
4298 } else if (SvSMAGICAL(ret)) {
4299 if (SvGMAGICAL(ret)) {
4300 /* I don't believe that there is ever qr magic
4302 assert(!mg_find(ret, PERL_MAGIC_qr));
4303 sv_unmagic(ret, PERL_MAGIC_qr);
4306 mg = mg_find(ret, PERL_MAGIC_qr);
4307 /* testing suggests mg only ends up non-NULL for
4308 scalars who were upgraded and compiled in the
4309 else block below. In turn, this is only
4310 triggered in the "postponed utf8 string" tests
4316 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
4320 rx = reg_temp_copy(NULL, rx);
4324 const I32 osize = PL_regsize;
4327 assert (SvUTF8(ret));
4328 } else if (SvUTF8(ret)) {
4329 /* Not doing UTF-8, despite what the SV says. Is
4330 this only if we're trapped in use 'bytes'? */
4331 /* Make a copy of the octet sequence, but without
4332 the flag on, as the compiler now honours the
4333 SvUTF8 flag on ret. */
4335 const char *const p = SvPV(ret, len);
4336 ret = newSVpvn_flags(p, len, SVs_TEMP);
4338 rx = CALLREGCOMP(ret, pm_flags);
4340 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
4342 /* This isn't a first class regexp. Instead, it's
4343 caching a regexp onto an existing, Perl visible
4345 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
4350 re = (struct regexp *)SvANY(rx);
4352 RXp_MATCH_COPIED_off(re);
4353 re->subbeg = rex->subbeg;
4354 re->sublen = rex->sublen;
4357 debug_start_match(re_sv, utf8_target, locinput, PL_regeol,
4358 "Matching embedded");
4360 startpoint = rei->program + 1;
4361 ST.close_paren = 0; /* only used for GOSUB */
4362 /* borrowed from regtry */
4363 if (PL_reg_start_tmpl <= re->nparens) {
4364 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4365 if(PL_reg_start_tmp)
4366 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4368 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4371 eval_recurse_doit: /* Share code with GOSUB below this line */
4372 /* run the pattern returned from (??{...}) */
4373 ST.cp = regcppush(0); /* Save *all* the positions. */
4374 REGCP_SET(ST.lastcp);
4376 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
4378 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4379 PL_reglastparen = &re->lastparen;
4380 PL_reglastcloseparen = &re->lastcloseparen;
4382 re->lastcloseparen = 0;
4384 PL_reginput = locinput;
4387 /* XXXX This is too dramatic a measure... */
4390 ST.toggle_reg_flags = PL_reg_flags;
4392 PL_reg_flags |= RF_utf8;
4394 PL_reg_flags &= ~RF_utf8;
4395 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4397 ST.prev_rex = rex_sv;
4398 ST.prev_curlyx = cur_curlyx;
4399 SETREX(rex_sv,re_sv);
4404 ST.prev_eval = cur_eval;
4406 /* now continue from first node in postoned RE */
4407 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4410 /* logical is 1, /(?(?{...})X|Y)/ */
4411 sw = cBOOL(SvTRUE(ret));
4416 case EVAL_AB: /* cleanup after a successful (??{A})B */
4417 /* note: this is called twice; first after popping B, then A */
4418 PL_reg_flags ^= ST.toggle_reg_flags;
4419 ReREFCNT_dec(rex_sv);
4420 SETREX(rex_sv,ST.prev_rex);
4421 rex = (struct regexp *)SvANY(rex_sv);
4422 rexi = RXi_GET(rex);
4424 cur_eval = ST.prev_eval;
4425 cur_curlyx = ST.prev_curlyx;
4427 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4428 PL_reglastparen = &rex->lastparen;
4429 PL_reglastcloseparen = &rex->lastcloseparen;
4430 /* also update PL_regoffs */
4431 PL_regoffs = rex->offs;
4433 /* XXXX This is too dramatic a measure... */
4435 if ( nochange_depth )
4440 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4441 /* note: this is called twice; first after popping B, then A */
4442 PL_reg_flags ^= ST.toggle_reg_flags;
4443 ReREFCNT_dec(rex_sv);
4444 SETREX(rex_sv,ST.prev_rex);
4445 rex = (struct regexp *)SvANY(rex_sv);
4446 rexi = RXi_GET(rex);
4447 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4448 PL_reglastparen = &rex->lastparen;
4449 PL_reglastcloseparen = &rex->lastcloseparen;
4451 PL_reginput = locinput;
4452 REGCP_UNWIND(ST.lastcp);
4454 cur_eval = ST.prev_eval;
4455 cur_curlyx = ST.prev_curlyx;
4456 /* XXXX This is too dramatic a measure... */
4458 if ( nochange_depth )
4464 n = ARG(scan); /* which paren pair */
4465 PL_reg_start_tmp[n] = locinput;
4471 n = ARG(scan); /* which paren pair */
4472 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4473 PL_regoffs[n].end = locinput - PL_bostr;
4474 /*if (n > PL_regsize)
4476 if (n > *PL_reglastparen)
4477 *PL_reglastparen = n;
4478 *PL_reglastcloseparen = n;
4479 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4487 cursor && OP(cursor)!=END;
4488 cursor=regnext(cursor))
4490 if ( OP(cursor)==CLOSE ){
4492 if ( n <= lastopen ) {
4494 = PL_reg_start_tmp[n] - PL_bostr;
4495 PL_regoffs[n].end = locinput - PL_bostr;
4496 /*if (n > PL_regsize)
4498 if (n > *PL_reglastparen)
4499 *PL_reglastparen = n;
4500 *PL_reglastcloseparen = n;
4501 if ( n == ARG(scan) || (cur_eval &&
4502 cur_eval->u.eval.close_paren == n))
4511 n = ARG(scan); /* which paren pair */
4512 sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
4515 /* reg_check_named_buff_matched returns 0 for no match */
4516 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
4520 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4526 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4528 next = NEXTOPER(NEXTOPER(scan));
4530 next = scan + ARG(scan);
4531 if (OP(next) == IFTHEN) /* Fake one. */
4532 next = NEXTOPER(NEXTOPER(next));
4536 logical = scan->flags;
4539 /*******************************************************************
4541 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4542 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4543 STAR/PLUS/CURLY/CURLYN are used instead.)
4545 A*B is compiled as <CURLYX><A><WHILEM><B>
4547 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4548 state, which contains the current count, initialised to -1. It also sets
4549 cur_curlyx to point to this state, with any previous value saved in the
4552 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4553 since the pattern may possibly match zero times (i.e. it's a while {} loop
4554 rather than a do {} while loop).
4556 Each entry to WHILEM represents a successful match of A. The count in the
4557 CURLYX block is incremented, another WHILEM state is pushed, and execution
4558 passes to A or B depending on greediness and the current count.
4560 For example, if matching against the string a1a2a3b (where the aN are
4561 substrings that match /A/), then the match progresses as follows: (the
4562 pushed states are interspersed with the bits of strings matched so far):
4565 <CURLYX cnt=0><WHILEM>
4566 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4567 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4568 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4569 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4571 (Contrast this with something like CURLYM, which maintains only a single
4575 a1 <CURLYM cnt=1> a2
4576 a1 a2 <CURLYM cnt=2> a3
4577 a1 a2 a3 <CURLYM cnt=3> b
4580 Each WHILEM state block marks a point to backtrack to upon partial failure
4581 of A or B, and also contains some minor state data related to that
4582 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4583 overall state, such as the count, and pointers to the A and B ops.
4585 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4586 must always point to the *current* CURLYX block, the rules are:
4588 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4589 and set cur_curlyx to point the new block.
4591 When popping the CURLYX block after a successful or unsuccessful match,
4592 restore the previous cur_curlyx.
4594 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4595 to the outer one saved in the CURLYX block.
4597 When popping the WHILEM block after a successful or unsuccessful B match,
4598 restore the previous cur_curlyx.
4600 Here's an example for the pattern (AI* BI)*BO
4601 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4604 curlyx backtrack stack
4605 ------ ---------------
4607 CO <CO prev=NULL> <WO>
4608 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4609 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4610 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4612 At this point the pattern succeeds, and we work back down the stack to
4613 clean up, restoring as we go:
4615 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4616 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4617 CO <CO prev=NULL> <WO>
4620 *******************************************************************/
4622 #define ST st->u.curlyx
4624 case CURLYX: /* start of /A*B/ (for complex A) */
4626 /* No need to save/restore up to this paren */
4627 I32 parenfloor = scan->flags;
4629 assert(next); /* keep Coverity happy */
4630 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4633 /* XXXX Probably it is better to teach regpush to support
4634 parenfloor > PL_regsize... */
4635 if (parenfloor > (I32)*PL_reglastparen)
4636 parenfloor = *PL_reglastparen; /* Pessimization... */
4638 ST.prev_curlyx= cur_curlyx;
4640 ST.cp = PL_savestack_ix;
4642 /* these fields contain the state of the current curly.
4643 * they are accessed by subsequent WHILEMs */
4644 ST.parenfloor = parenfloor;
4649 ST.count = -1; /* this will be updated by WHILEM */
4650 ST.lastloc = NULL; /* this will be updated by WHILEM */
4652 PL_reginput = locinput;
4653 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4657 case CURLYX_end: /* just finished matching all of A*B */
4658 cur_curlyx = ST.prev_curlyx;
4662 case CURLYX_end_fail: /* just failed to match all of A*B */
4664 cur_curlyx = ST.prev_curlyx;
4670 #define ST st->u.whilem
4672 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4674 /* see the discussion above about CURLYX/WHILEM */
4676 int min = ARG1(cur_curlyx->u.curlyx.me);
4677 int max = ARG2(cur_curlyx->u.curlyx.me);
4678 regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
4680 assert(cur_curlyx); /* keep Coverity happy */
4681 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4682 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4683 ST.cache_offset = 0;
4686 PL_reginput = locinput;
4688 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4689 "%*s whilem: matched %ld out of %d..%d\n",
4690 REPORT_CODE_OFF+depth*2, "", (long)n, min, max)
4693 /* First just match a string of min A's. */
4696 cur_curlyx->u.curlyx.lastloc = locinput;
4697 PUSH_STATE_GOTO(WHILEM_A_pre, A);
4701 /* If degenerate A matches "", assume A done. */
4703 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4704 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4705 "%*s whilem: empty match detected, trying continuation...\n",
4706 REPORT_CODE_OFF+depth*2, "")
4708 goto do_whilem_B_max;
4711 /* super-linear cache processing */
4715 if (!PL_reg_maxiter) {
4716 /* start the countdown: Postpone detection until we
4717 * know the match is not *that* much linear. */
4718 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4719 /* possible overflow for long strings and many CURLYX's */
4720 if (PL_reg_maxiter < 0)
4721 PL_reg_maxiter = I32_MAX;
4722 PL_reg_leftiter = PL_reg_maxiter;
4725 if (PL_reg_leftiter-- == 0) {
4726 /* initialise cache */
4727 const I32 size = (PL_reg_maxiter + 7)/8;
4728 if (PL_reg_poscache) {
4729 if ((I32)PL_reg_poscache_size < size) {
4730 Renew(PL_reg_poscache, size, char);
4731 PL_reg_poscache_size = size;
4733 Zero(PL_reg_poscache, size, char);
4736 PL_reg_poscache_size = size;
4737 Newxz(PL_reg_poscache, size, char);
4739 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4740 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4741 PL_colors[4], PL_colors[5])
4745 if (PL_reg_leftiter < 0) {
4746 /* have we already failed at this position? */
4748 offset = (scan->flags & 0xf) - 1
4749 + (locinput - PL_bostr) * (scan->flags>>4);
4750 mask = 1 << (offset % 8);
4752 if (PL_reg_poscache[offset] & mask) {
4753 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4754 "%*s whilem: (cache) already tried at this position...\n",
4755 REPORT_CODE_OFF+depth*2, "")
4757 sayNO; /* cache records failure */
4759 ST.cache_offset = offset;
4760 ST.cache_mask = mask;
4764 /* Prefer B over A for minimal matching. */
4766 if (cur_curlyx->u.curlyx.minmod) {
4767 ST.save_curlyx = cur_curlyx;
4768 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4769 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4770 REGCP_SET(ST.lastcp);
4771 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4775 /* Prefer A over B for maximal matching. */
4777 if (n < max) { /* More greed allowed? */
4778 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4779 cur_curlyx->u.curlyx.lastloc = locinput;
4780 REGCP_SET(ST.lastcp);
4781 PUSH_STATE_GOTO(WHILEM_A_max, A);
4784 goto do_whilem_B_max;
4788 case WHILEM_B_min: /* just matched B in a minimal match */
4789 case WHILEM_B_max: /* just matched B in a maximal match */
4790 cur_curlyx = ST.save_curlyx;
4794 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4795 cur_curlyx = ST.save_curlyx;
4796 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4797 cur_curlyx->u.curlyx.count--;
4801 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4802 REGCP_UNWIND(ST.lastcp);
4805 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4806 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4807 cur_curlyx->u.curlyx.count--;
4811 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4812 REGCP_UNWIND(ST.lastcp);
4813 regcppop(rex); /* Restore some previous $<digit>s? */
4814 PL_reginput = locinput;
4815 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4816 "%*s whilem: failed, trying continuation...\n",
4817 REPORT_CODE_OFF+depth*2, "")
4820 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4821 && ckWARN(WARN_REGEXP)
4822 && !(PL_reg_flags & RF_warned))
4824 PL_reg_flags |= RF_warned;
4825 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4826 "Complex regular subexpression recursion",
4831 ST.save_curlyx = cur_curlyx;
4832 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4833 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4836 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4837 cur_curlyx = ST.save_curlyx;
4838 REGCP_UNWIND(ST.lastcp);
4841 if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
4842 /* Maximum greed exceeded */
4843 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4844 && ckWARN(WARN_REGEXP)
4845 && !(PL_reg_flags & RF_warned))
4847 PL_reg_flags |= RF_warned;
4848 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4849 "%s limit (%d) exceeded",
4850 "Complex regular subexpression recursion",
4853 cur_curlyx->u.curlyx.count--;
4857 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4858 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4860 /* Try grabbing another A and see if it helps. */
4861 PL_reginput = locinput;
4862 cur_curlyx->u.curlyx.lastloc = locinput;
4863 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4864 REGCP_SET(ST.lastcp);
4865 PUSH_STATE_GOTO(WHILEM_A_min,
4866 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS);
4870 #define ST st->u.branch
4872 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4873 next = scan + ARG(scan);
4876 scan = NEXTOPER(scan);
4879 case BRANCH: /* /(...|A|...)/ */
4880 scan = NEXTOPER(scan); /* scan now points to inner node */
4881 ST.lastparen = *PL_reglastparen;
4882 ST.next_branch = next;
4884 PL_reginput = locinput;
4886 /* Now go into the branch */
4888 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4890 PUSH_STATE_GOTO(BRANCH_next, scan);
4894 PL_reginput = locinput;
4895 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4896 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4897 PUSH_STATE_GOTO(CUTGROUP_next,next);
4899 case CUTGROUP_next_fail:
4902 if (st->u.mark.mark_name)
4903 sv_commit = st->u.mark.mark_name;
4909 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4914 REGCP_UNWIND(ST.cp);
4915 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4916 PL_regoffs[n].end = -1;
4917 *PL_reglastparen = n;
4918 /*dmq: *PL_reglastcloseparen = n; */
4919 scan = ST.next_branch;
4920 /* no more branches? */
4921 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4923 PerlIO_printf( Perl_debug_log,
4924 "%*s %sBRANCH failed...%s\n",
4925 REPORT_CODE_OFF+depth*2, "",
4931 continue; /* execute next BRANCH[J] op */
4939 #define ST st->u.curlym
4941 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4943 /* This is an optimisation of CURLYX that enables us to push
4944 * only a single backtracking state, no matter how many matches
4945 * there are in {m,n}. It relies on the pattern being constant
4946 * length, with no parens to influence future backrefs
4950 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4952 /* if paren positive, emulate an OPEN/CLOSE around A */
4954 U32 paren = ST.me->flags;
4955 if (paren > PL_regsize)
4957 if (paren > *PL_reglastparen)
4958 *PL_reglastparen = paren;
4959 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4967 ST.c1 = CHRTEST_UNINIT;
4970 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4973 curlym_do_A: /* execute the A in /A{m,n}B/ */
4974 PL_reginput = locinput;
4975 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4978 case CURLYM_A: /* we've just matched an A */
4979 locinput = st->locinput;
4980 nextchr = UCHARAT(locinput);
4983 /* after first match, determine A's length: u.curlym.alen */
4984 if (ST.count == 1) {
4985 if (PL_reg_match_utf8) {
4987 while (s < PL_reginput) {
4993 ST.alen = PL_reginput - locinput;
4996 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4999 PerlIO_printf(Perl_debug_log,
5000 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
5001 (int)(REPORT_CODE_OFF+(depth*2)), "",
5002 (IV) ST.count, (IV)ST.alen)
5005 locinput = PL_reginput;
5007 if (cur_eval && cur_eval->u.eval.close_paren &&
5008 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
5012 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
5013 if ( max == REG_INFTY || ST.count < max )
5014 goto curlym_do_A; /* try to match another A */
5016 goto curlym_do_B; /* try to match B */
5018 case CURLYM_A_fail: /* just failed to match an A */
5019 REGCP_UNWIND(ST.cp);
5021 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
5022 || (cur_eval && cur_eval->u.eval.close_paren &&
5023 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
5026 curlym_do_B: /* execute the B in /A{m,n}B/ */
5027 PL_reginput = locinput;
5028 if (ST.c1 == CHRTEST_UNINIT) {
5029 /* calculate c1 and c2 for possible match of 1st char
5030 * following curly */
5031 ST.c1 = ST.c2 = CHRTEST_VOID;
5032 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
5033 regnode *text_node = ST.B;
5034 if (! HAS_TEXT(text_node))
5035 FIND_NEXT_IMPT(text_node);
5038 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
5040 But the former is redundant in light of the latter.
5042 if this changes back then the macro for
5043 IS_TEXT and friends need to change.
5045 if (PL_regkind[OP(text_node)] == EXACT)
5048 ST.c1 = (U8)*STRING(text_node);
5049 switch (OP(text_node)) {
5050 case EXACTF: ST.c2 = PL_fold[ST.c1]; break;
5052 case EXACTFU: ST.c2 = PL_fold_latin1[ST.c1]; break;
5053 case EXACTFL: ST.c2 = PL_fold_locale[ST.c1]; break;
5054 default: ST.c2 = ST.c1;
5061 PerlIO_printf(Perl_debug_log,
5062 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
5063 (int)(REPORT_CODE_OFF+(depth*2)),
5066 if (ST.c1 != CHRTEST_VOID
5067 && UCHARAT(PL_reginput) != ST.c1
5068 && UCHARAT(PL_reginput) != ST.c2)
5070 /* simulate B failing */
5072 PerlIO_printf(Perl_debug_log,
5073 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
5074 (int)(REPORT_CODE_OFF+(depth*2)),"",
5077 state_num = CURLYM_B_fail;
5078 goto reenter_switch;
5082 /* mark current A as captured */
5083 I32 paren = ST.me->flags;
5085 PL_regoffs[paren].start
5086 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
5087 PL_regoffs[paren].end = PL_reginput - PL_bostr;
5088 /*dmq: *PL_reglastcloseparen = paren; */
5091 PL_regoffs[paren].end = -1;
5092 if (cur_eval && cur_eval->u.eval.close_paren &&
5093 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
5102 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
5105 case CURLYM_B_fail: /* just failed to match a B */
5106 REGCP_UNWIND(ST.cp);
5108 I32 max = ARG2(ST.me);
5109 if (max != REG_INFTY && ST.count == max)
5111 goto curlym_do_A; /* try to match a further A */
5113 /* backtrack one A */
5114 if (ST.count == ARG1(ST.me) /* min */)
5117 locinput = HOPc(locinput, -ST.alen);
5118 goto curlym_do_B; /* try to match B */
5121 #define ST st->u.curly
5123 #define CURLY_SETPAREN(paren, success) \
5126 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
5127 PL_regoffs[paren].end = locinput - PL_bostr; \
5128 *PL_reglastcloseparen = paren; \
5131 PL_regoffs[paren].end = -1; \
5134 case STAR: /* /A*B/ where A is width 1 */
5138 scan = NEXTOPER(scan);
5140 case PLUS: /* /A+B/ where A is width 1 */
5144 scan = NEXTOPER(scan);
5146 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
5147 ST.paren = scan->flags; /* Which paren to set */
5148 if (ST.paren > PL_regsize)
5149 PL_regsize = ST.paren;
5150 if (ST.paren > *PL_reglastparen)
5151 *PL_reglastparen = ST.paren;
5152 ST.min = ARG1(scan); /* min to match */
5153 ST.max = ARG2(scan); /* max to match */
5154 if (cur_eval && cur_eval->u.eval.close_paren &&
5155 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5159 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
5161 case CURLY: /* /A{m,n}B/ where A is width 1 */
5163 ST.min = ARG1(scan); /* min to match */
5164 ST.max = ARG2(scan); /* max to match */
5165 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
5168 * Lookahead to avoid useless match attempts
5169 * when we know what character comes next.
5171 * Used to only do .*x and .*?x, but now it allows
5172 * for )'s, ('s and (?{ ... })'s to be in the way
5173 * of the quantifier and the EXACT-like node. -- japhy
5176 if (ST.min > ST.max) /* XXX make this a compile-time check? */
5178 if (HAS_TEXT(next) || JUMPABLE(next)) {
5180 regnode *text_node = next;
5182 if (! HAS_TEXT(text_node))
5183 FIND_NEXT_IMPT(text_node);
5185 if (! HAS_TEXT(text_node))
5186 ST.c1 = ST.c2 = CHRTEST_VOID;
5188 if ( PL_regkind[OP(text_node)] != EXACT ) {
5189 ST.c1 = ST.c2 = CHRTEST_VOID;
5190 goto assume_ok_easy;
5193 s = (U8*)STRING(text_node);
5195 /* Currently we only get here when
5197 PL_rekind[OP(text_node)] == EXACT
5199 if this changes back then the macro for IS_TEXT and
5200 friends need to change. */
5203 switch (OP(text_node)) {
5204 case EXACTF: ST.c2 = PL_fold[ST.c1]; break;
5206 case EXACTFU: ST.c2 = PL_fold_latin1[ST.c1]; break;
5207 case EXACTFL: ST.c2 = PL_fold_locale[ST.c1]; break;
5208 default: ST.c2 = ST.c1; break;
5211 else { /* UTF_PATTERN */
5212 if (IS_TEXTFU(text_node) || IS_TEXTF(text_node)) {
5213 STRLEN ulen1, ulen2;
5214 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
5215 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
5217 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
5218 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
5220 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
5222 0 : UTF8_ALLOW_ANY);
5223 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
5225 0 : UTF8_ALLOW_ANY);
5227 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
5229 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
5234 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
5241 ST.c1 = ST.c2 = CHRTEST_VOID;
5246 PL_reginput = locinput;
5249 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
5252 locinput = PL_reginput;
5254 if (ST.c1 == CHRTEST_VOID)
5255 goto curly_try_B_min;
5257 ST.oldloc = locinput;
5259 /* set ST.maxpos to the furthest point along the
5260 * string that could possibly match */
5261 if (ST.max == REG_INFTY) {
5262 ST.maxpos = PL_regeol - 1;
5264 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
5267 else if (utf8_target) {
5268 int m = ST.max - ST.min;
5269 for (ST.maxpos = locinput;
5270 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
5271 ST.maxpos += UTF8SKIP(ST.maxpos);
5274 ST.maxpos = locinput + ST.max - ST.min;
5275 if (ST.maxpos >= PL_regeol)
5276 ST.maxpos = PL_regeol - 1;
5278 goto curly_try_B_min_known;
5282 ST.count = regrepeat(rex, ST.A, ST.max, depth);
5283 locinput = PL_reginput;
5284 if (ST.count < ST.min)
5286 if ((ST.count > ST.min)
5287 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
5289 /* A{m,n} must come at the end of the string, there's
5290 * no point in backing off ... */
5292 /* ...except that $ and \Z can match before *and* after
5293 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
5294 We may back off by one in this case. */
5295 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
5299 goto curly_try_B_max;
5304 case CURLY_B_min_known_fail:
5305 /* failed to find B in a non-greedy match where c1,c2 valid */
5306 if (ST.paren && ST.count)
5307 PL_regoffs[ST.paren].end = -1;
5309 PL_reginput = locinput; /* Could be reset... */
5310 REGCP_UNWIND(ST.cp);
5311 /* Couldn't or didn't -- move forward. */
5312 ST.oldloc = locinput;
5314 locinput += UTF8SKIP(locinput);
5318 curly_try_B_min_known:
5319 /* find the next place where 'B' could work, then call B */
5323 n = (ST.oldloc == locinput) ? 0 : 1;
5324 if (ST.c1 == ST.c2) {
5326 /* set n to utf8_distance(oldloc, locinput) */
5327 while (locinput <= ST.maxpos &&
5328 utf8n_to_uvchr((U8*)locinput,
5329 UTF8_MAXBYTES, &len,
5330 uniflags) != (UV)ST.c1) {
5336 /* set n to utf8_distance(oldloc, locinput) */
5337 while (locinput <= ST.maxpos) {
5339 const UV c = utf8n_to_uvchr((U8*)locinput,
5340 UTF8_MAXBYTES, &len,
5342 if (c == (UV)ST.c1 || c == (UV)ST.c2)
5350 if (ST.c1 == ST.c2) {
5351 while (locinput <= ST.maxpos &&
5352 UCHARAT(locinput) != ST.c1)
5356 while (locinput <= ST.maxpos
5357 && UCHARAT(locinput) != ST.c1
5358 && UCHARAT(locinput) != ST.c2)
5361 n = locinput - ST.oldloc;
5363 if (locinput > ST.maxpos)
5365 /* PL_reginput == oldloc now */
5368 if (regrepeat(rex, ST.A, n, depth) < n)
5371 PL_reginput = locinput;
5372 CURLY_SETPAREN(ST.paren, ST.count);
5373 if (cur_eval && cur_eval->u.eval.close_paren &&
5374 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5377 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5382 case CURLY_B_min_fail:
5383 /* failed to find B in a non-greedy match where c1,c2 invalid */
5384 if (ST.paren && ST.count)
5385 PL_regoffs[ST.paren].end = -1;
5387 REGCP_UNWIND(ST.cp);
5388 /* failed -- move forward one */
5389 PL_reginput = locinput;
5390 if (regrepeat(rex, ST.A, 1, depth)) {
5392 locinput = PL_reginput;
5393 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5394 ST.count > 0)) /* count overflow ? */
5397 CURLY_SETPAREN(ST.paren, ST.count);
5398 if (cur_eval && cur_eval->u.eval.close_paren &&
5399 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5402 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5410 /* a successful greedy match: now try to match B */
5411 if (cur_eval && cur_eval->u.eval.close_paren &&
5412 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5417 if (ST.c1 != CHRTEST_VOID)
5418 c = utf8_target ? utf8n_to_uvchr((U8*)PL_reginput,
5419 UTF8_MAXBYTES, 0, uniflags)
5420 : (UV) UCHARAT(PL_reginput);
5421 /* If it could work, try it. */
5422 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5423 CURLY_SETPAREN(ST.paren, ST.count);
5424 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5429 case CURLY_B_max_fail:
5430 /* failed to find B in a greedy match */
5431 if (ST.paren && ST.count)
5432 PL_regoffs[ST.paren].end = -1;
5434 REGCP_UNWIND(ST.cp);
5436 if (--ST.count < ST.min)
5438 PL_reginput = locinput = HOPc(locinput, -1);
5439 goto curly_try_B_max;
5446 /* we've just finished A in /(??{A})B/; now continue with B */
5448 st->u.eval.toggle_reg_flags
5449 = cur_eval->u.eval.toggle_reg_flags;
5450 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5452 st->u.eval.prev_rex = rex_sv; /* inner */
5453 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5454 rex = (struct regexp *)SvANY(rex_sv);
5455 rexi = RXi_GET(rex);
5456 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5457 ReREFCNT_inc(rex_sv);
5458 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
5460 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5461 PL_reglastparen = &rex->lastparen;
5462 PL_reglastcloseparen = &rex->lastcloseparen;
5464 REGCP_SET(st->u.eval.lastcp);
5465 PL_reginput = locinput;
5467 /* Restore parens of the outer rex without popping the
5469 tmpix = PL_savestack_ix;
5470 PL_savestack_ix = cur_eval->u.eval.lastcp;
5472 PL_savestack_ix = tmpix;
5474 st->u.eval.prev_eval = cur_eval;
5475 cur_eval = cur_eval->u.eval.prev_eval;
5477 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5478 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5479 if ( nochange_depth )
5482 PUSH_YES_STATE_GOTO(EVAL_AB,
5483 st->u.eval.prev_eval->u.eval.B); /* match B */
5486 if (locinput < reginfo->till) {
5487 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5488 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5490 (long)(locinput - PL_reg_starttry),
5491 (long)(reginfo->till - PL_reg_starttry),
5494 sayNO_SILENT; /* Cannot match: too short. */
5496 PL_reginput = locinput; /* put where regtry can find it */
5497 sayYES; /* Success! */
5499 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5501 PerlIO_printf(Perl_debug_log,
5502 "%*s %ssubpattern success...%s\n",
5503 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5504 PL_reginput = locinput; /* put where regtry can find it */
5505 sayYES; /* Success! */
5508 #define ST st->u.ifmatch
5510 case SUSPEND: /* (?>A) */
5512 PL_reginput = locinput;
5515 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5517 goto ifmatch_trivial_fail_test;
5519 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5521 ifmatch_trivial_fail_test:
5523 char * const s = HOPBACKc(locinput, scan->flags);
5528 sw = 1 - cBOOL(ST.wanted);
5532 next = scan + ARG(scan);
5540 PL_reginput = locinput;
5544 ST.logical = logical;
5545 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5547 /* execute body of (?...A) */
5548 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5551 case IFMATCH_A_fail: /* body of (?...A) failed */
5552 ST.wanted = !ST.wanted;
5555 case IFMATCH_A: /* body of (?...A) succeeded */
5557 sw = cBOOL(ST.wanted);
5559 else if (!ST.wanted)
5562 if (OP(ST.me) == SUSPEND)
5563 locinput = PL_reginput;
5565 locinput = PL_reginput = st->locinput;
5566 nextchr = UCHARAT(locinput);
5568 scan = ST.me + ARG(ST.me);
5571 continue; /* execute B */
5576 next = scan + ARG(scan);
5581 reginfo->cutpoint = PL_regeol;
5584 PL_reginput = locinput;
5586 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5587 PUSH_STATE_GOTO(COMMIT_next,next);
5589 case COMMIT_next_fail:
5596 #define ST st->u.mark
5598 ST.prev_mark = mark_state;
5599 ST.mark_name = sv_commit = sv_yes_mark
5600 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5602 ST.mark_loc = PL_reginput = locinput;
5603 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5605 case MARKPOINT_next:
5606 mark_state = ST.prev_mark;
5609 case MARKPOINT_next_fail:
5610 if (popmark && sv_eq(ST.mark_name,popmark))
5612 if (ST.mark_loc > startpoint)
5613 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5614 popmark = NULL; /* we found our mark */
5615 sv_commit = ST.mark_name;
5618 PerlIO_printf(Perl_debug_log,
5619 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5620 REPORT_CODE_OFF+depth*2, "",
5621 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5624 mark_state = ST.prev_mark;
5625 sv_yes_mark = mark_state ?
5626 mark_state->u.mark.mark_name : NULL;
5630 PL_reginput = locinput;
5632 /* (*SKIP) : if we fail we cut here*/
5633 ST.mark_name = NULL;
5634 ST.mark_loc = locinput;
5635 PUSH_STATE_GOTO(SKIP_next,next);
5637 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5638 otherwise do nothing. Meaning we need to scan
5640 regmatch_state *cur = mark_state;
5641 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5644 if ( sv_eq( cur->u.mark.mark_name,
5647 ST.mark_name = find;
5648 PUSH_STATE_GOTO( SKIP_next, next );
5650 cur = cur->u.mark.prev_mark;
5653 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5655 case SKIP_next_fail:
5657 /* (*CUT:NAME) - Set up to search for the name as we
5658 collapse the stack*/
5659 popmark = ST.mark_name;
5661 /* (*CUT) - No name, we cut here.*/
5662 if (ST.mark_loc > startpoint)
5663 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5664 /* but we set sv_commit to latest mark_name if there
5665 is one so they can test to see how things lead to this
5668 sv_commit=mark_state->u.mark.mark_name;
5676 if ( n == (U32)what_len_TRICKYFOLD(locinput,utf8_target,ln) ) {
5678 } else if ( LATIN_SMALL_LETTER_SHARP_S == n && !utf8_target && !UTF_PATTERN ) {
5681 U8 folded[UTF8_MAXBYTES_CASE+1];
5683 const char * const l = locinput;
5684 char *e = PL_regeol;
5685 to_uni_fold(n, folded, &foldlen);
5687 if (! foldEQ_utf8((const char*) folded, 0, foldlen, 1,
5688 l, &e, 0, utf8_target)) {
5693 nextchr = UCHARAT(locinput);
5696 if ((n=is_LNBREAK(locinput,utf8_target))) {
5698 nextchr = UCHARAT(locinput);
5703 #define CASE_CLASS(nAmE) \
5705 if ((n=is_##nAmE(locinput,utf8_target))) { \
5707 nextchr = UCHARAT(locinput); \
5712 if ((n=is_##nAmE(locinput,utf8_target))) { \
5715 locinput += UTF8SKIP(locinput); \
5716 nextchr = UCHARAT(locinput); \
5721 CASE_CLASS(HORIZWS);
5725 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5726 PTR2UV(scan), OP(scan));
5727 Perl_croak(aTHX_ "regexp memory corruption");
5731 /* switch break jumps here */
5732 scan = next; /* prepare to execute the next op and ... */
5733 continue; /* ... jump back to the top, reusing st */
5737 /* push a state that backtracks on success */
5738 st->u.yes.prev_yes_state = yes_state;
5742 /* push a new regex state, then continue at scan */
5744 regmatch_state *newst;
5747 regmatch_state *cur = st;
5748 regmatch_state *curyes = yes_state;
5750 regmatch_slab *slab = PL_regmatch_slab;
5751 for (;curd > -1;cur--,curd--) {
5752 if (cur < SLAB_FIRST(slab)) {
5754 cur = SLAB_LAST(slab);
5756 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5757 REPORT_CODE_OFF + 2 + depth * 2,"",
5758 curd, PL_reg_name[cur->resume_state],
5759 (curyes == cur) ? "yes" : ""
5762 curyes = cur->u.yes.prev_yes_state;
5765 DEBUG_STATE_pp("push")
5768 st->locinput = locinput;
5770 if (newst > SLAB_LAST(PL_regmatch_slab))
5771 newst = S_push_slab(aTHX);
5772 PL_regmatch_state = newst;
5774 locinput = PL_reginput;
5775 nextchr = UCHARAT(locinput);
5783 * We get here only if there's trouble -- normally "case END" is
5784 * the terminating point.
5786 Perl_croak(aTHX_ "corrupted regexp pointers");
5792 /* we have successfully completed a subexpression, but we must now
5793 * pop to the state marked by yes_state and continue from there */
5794 assert(st != yes_state);
5796 while (st != yes_state) {
5798 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5799 PL_regmatch_slab = PL_regmatch_slab->prev;
5800 st = SLAB_LAST(PL_regmatch_slab);
5804 DEBUG_STATE_pp("pop (no final)");
5806 DEBUG_STATE_pp("pop (yes)");
5812 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5813 || yes_state > SLAB_LAST(PL_regmatch_slab))
5815 /* not in this slab, pop slab */
5816 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5817 PL_regmatch_slab = PL_regmatch_slab->prev;
5818 st = SLAB_LAST(PL_regmatch_slab);
5820 depth -= (st - yes_state);
5823 yes_state = st->u.yes.prev_yes_state;
5824 PL_regmatch_state = st;
5827 locinput= st->locinput;
5828 nextchr = UCHARAT(locinput);
5830 state_num = st->resume_state + no_final;
5831 goto reenter_switch;
5834 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5835 PL_colors[4], PL_colors[5]));
5837 if (PL_reg_eval_set) {
5838 /* each successfully executed (?{...}) block does the equivalent of
5839 * local $^R = do {...}
5840 * When popping the save stack, all these locals would be undone;
5841 * bypass this by setting the outermost saved $^R to the latest
5843 if (oreplsv != GvSV(PL_replgv))
5844 sv_setsv(oreplsv, GvSV(PL_replgv));
5851 PerlIO_printf(Perl_debug_log,
5852 "%*s %sfailed...%s\n",
5853 REPORT_CODE_OFF+depth*2, "",
5854 PL_colors[4], PL_colors[5])
5866 /* there's a previous state to backtrack to */
5868 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5869 PL_regmatch_slab = PL_regmatch_slab->prev;
5870 st = SLAB_LAST(PL_regmatch_slab);
5872 PL_regmatch_state = st;
5873 locinput= st->locinput;
5874 nextchr = UCHARAT(locinput);
5876 DEBUG_STATE_pp("pop");
5878 if (yes_state == st)
5879 yes_state = st->u.yes.prev_yes_state;
5881 state_num = st->resume_state + 1; /* failure = success + 1 */
5882 goto reenter_switch;
5887 if (rex->intflags & PREGf_VERBARG_SEEN) {
5888 SV *sv_err = get_sv("REGERROR", 1);
5889 SV *sv_mrk = get_sv("REGMARK", 1);
5891 sv_commit = &PL_sv_no;
5893 sv_yes_mark = &PL_sv_yes;
5896 sv_commit = &PL_sv_yes;
5897 sv_yes_mark = &PL_sv_no;
5899 sv_setsv(sv_err, sv_commit);
5900 sv_setsv(sv_mrk, sv_yes_mark);
5903 /* clean up; in particular, free all slabs above current one */
5904 LEAVE_SCOPE(oldsave);
5910 - regrepeat - repeatedly match something simple, report how many
5913 * [This routine now assumes that it will only match on things of length 1.
5914 * That was true before, but now we assume scan - reginput is the count,
5915 * rather than incrementing count on every character. [Er, except utf8.]]
5918 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5921 register char *scan;
5923 register char *loceol = PL_regeol;
5924 register I32 hardcount = 0;
5925 register bool utf8_target = PL_reg_match_utf8;
5928 PERL_UNUSED_ARG(depth);
5931 PERL_ARGS_ASSERT_REGREPEAT;
5934 if (max == REG_INFTY)
5936 else if (max < loceol - scan)
5937 loceol = scan + max;
5942 while (scan < loceol && hardcount < max && *scan != '\n') {
5943 scan += UTF8SKIP(scan);
5947 while (scan < loceol && *scan != '\n')
5954 while (scan < loceol && hardcount < max) {
5955 scan += UTF8SKIP(scan);
5966 /* To get here, EXACTish nodes must have *byte* length == 1. That
5967 * means they match only characters in the string that can be expressed
5968 * as a single byte. For non-utf8 strings, that means a simple match.
5969 * For utf8 strings, the character matched must be an invariant, or
5970 * downgradable to a single byte. The pattern's utf8ness is
5971 * irrelevant, as since it's a single byte, it either isn't utf8, or if
5972 * it is, it's an invariant */
5975 assert(! UTF_PATTERN || UNI_IS_INVARIANT(c));
5977 if (! utf8_target || UNI_IS_INVARIANT(c)) {
5978 while (scan < loceol && UCHARAT(scan) == c) {
5984 /* Here, the string is utf8, and the pattern char is different
5985 * in utf8 than not, so can't compare them directly. Outside the
5986 * loop, find find the two utf8 bytes that represent c, and then
5987 * look for those in sequence in the utf8 string */
5988 U8 high = UTF8_TWO_BYTE_HI(c);
5989 U8 low = UTF8_TWO_BYTE_LO(c);
5992 while (hardcount < max
5993 && scan + 1 < loceol
5994 && UCHARAT(scan) == high
5995 && UCHARAT(scan + 1) == low)
6003 utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
6007 PL_reg_flags |= RF_tainted;
6008 utf8_flags = FOLDEQ_UTF8_LOCALE;
6015 /* The comments for the EXACT case above apply as well to these fold
6020 assert(! UTF_PATTERN || UNI_IS_INVARIANT(c));
6022 if (utf8_target) { /* Use full Unicode fold matching */
6023 char *tmpeol = loceol;
6024 while (hardcount < max
6025 && foldEQ_utf8_flags(scan, &tmpeol, 0, utf8_target,
6026 STRING(p), NULL, 1, cBOOL(UTF_PATTERN), utf8_flags))
6033 /* XXX Note that the above handles properly the German sharp s in
6034 * the pattern matching ss in the string. But it doesn't handle
6035 * properly cases where the string contains say 'LIGATURE ff' and
6036 * the pattern is 'f+'. This would require, say, a new function or
6037 * revised interface to foldEQ_utf8(), in which the maximum number
6038 * of characters to match could be passed and it would return how
6039 * many actually did. This is just one of many cases where
6040 * multi-char folds don't work properly, and so the fix is being
6046 /* Here, the string isn't utf8 and c is a single byte; and either
6047 * the pattern isn't utf8 or c is an invariant, so its utf8ness
6048 * doesn't affect c. Can just do simple comparisons for exact or
6051 case EXACTF: folded = PL_fold[c]; break;
6053 case EXACTFU: folded = PL_fold_latin1[c]; break;
6054 case EXACTFL: folded = PL_fold_locale[c]; break;
6055 default: Perl_croak(aTHX_ "panic: Unexpected op %u", OP(p));
6057 while (scan < loceol &&
6058 (UCHARAT(scan) == c || UCHARAT(scan) == folded))
6066 if (utf8_target || OP(p) == ANYOFV) {
6069 inclasslen = loceol - scan;
6070 while (hardcount < max
6071 && ((inclasslen = loceol - scan) > 0)
6072 && reginclass(prog, p, (U8*)scan, &inclasslen, utf8_target))
6078 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
6086 LOAD_UTF8_CHARCLASS_ALNUM();
6087 while (hardcount < max && scan < loceol &&
6088 swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target))
6090 scan += UTF8SKIP(scan);
6094 while (scan < loceol && isWORDCHAR_L1((U8) *scan)) {
6102 while (scan < loceol && isALNUM((U8) *scan)) {
6107 while (scan < loceol && isWORDCHAR_A((U8) *scan)) {
6112 PL_reg_flags |= RF_tainted;
6115 while (hardcount < max && scan < loceol &&
6116 isALNUM_LC_utf8((U8*)scan)) {
6117 scan += UTF8SKIP(scan);
6121 while (scan < loceol && isALNUM_LC(*scan))
6131 LOAD_UTF8_CHARCLASS_ALNUM();
6132 while (hardcount < max && scan < loceol &&
6133 ! swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target))
6135 scan += UTF8SKIP(scan);
6139 while (scan < loceol && ! isWORDCHAR_L1((U8) *scan)) {
6146 goto utf8_Nwordchar;
6147 while (scan < loceol && ! isALNUM((U8) *scan)) {
6153 while (scan < loceol && ! isWORDCHAR_A((U8) *scan)) {
6154 scan += UTF8SKIP(scan);
6158 while (scan < loceol && ! isWORDCHAR_A((U8) *scan)) {
6164 PL_reg_flags |= RF_tainted;
6167 while (hardcount < max && scan < loceol &&
6168 !isALNUM_LC_utf8((U8*)scan)) {
6169 scan += UTF8SKIP(scan);
6173 while (scan < loceol && !isALNUM_LC(*scan))
6183 LOAD_UTF8_CHARCLASS_SPACE();
6184 while (hardcount < max && scan < loceol &&
6186 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target)))
6188 scan += UTF8SKIP(scan);
6194 while (scan < loceol && isSPACE_L1((U8) *scan)) {
6203 while (scan < loceol && isSPACE((U8) *scan)) {
6208 while (scan < loceol && isSPACE_A((U8) *scan)) {
6213 PL_reg_flags |= RF_tainted;
6216 while (hardcount < max && scan < loceol &&
6217 isSPACE_LC_utf8((U8*)scan)) {
6218 scan += UTF8SKIP(scan);
6222 while (scan < loceol && isSPACE_LC(*scan))
6232 LOAD_UTF8_CHARCLASS_SPACE();
6233 while (hardcount < max && scan < loceol &&
6235 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target)))
6237 scan += UTF8SKIP(scan);
6243 while (scan < loceol && ! isSPACE_L1((U8) *scan)) {
6252 while (scan < loceol && ! isSPACE((U8) *scan)) {
6258 while (scan < loceol && ! isSPACE_A((U8) *scan)) {
6259 scan += UTF8SKIP(scan);
6263 while (scan < loceol && ! isSPACE_A((U8) *scan)) {
6269 PL_reg_flags |= RF_tainted;
6272 while (hardcount < max && scan < loceol &&
6273 !isSPACE_LC_utf8((U8*)scan)) {
6274 scan += UTF8SKIP(scan);
6278 while (scan < loceol && !isSPACE_LC(*scan))
6285 LOAD_UTF8_CHARCLASS_DIGIT();
6286 while (hardcount < max && scan < loceol &&
6287 swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
6288 scan += UTF8SKIP(scan);
6292 while (scan < loceol && isDIGIT(*scan))
6297 while (scan < loceol && isDIGIT_A((U8) *scan)) {
6302 PL_reg_flags |= RF_tainted;
6305 while (hardcount < max && scan < loceol &&
6306 isDIGIT_LC_utf8((U8*)scan)) {
6307 scan += UTF8SKIP(scan);
6311 while (scan < loceol && isDIGIT_LC(*scan))
6318 LOAD_UTF8_CHARCLASS_DIGIT();
6319 while (hardcount < max && scan < loceol &&
6320 !swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
6321 scan += UTF8SKIP(scan);
6325 while (scan < loceol && !isDIGIT(*scan))
6331 while (scan < loceol && ! isDIGIT_A((U8) *scan)) {
6332 scan += UTF8SKIP(scan);
6336 while (scan < loceol && ! isDIGIT_A((U8) *scan)) {
6342 PL_reg_flags |= RF_tainted;
6345 while (hardcount < max && scan < loceol &&
6346 !isDIGIT_LC_utf8((U8*)scan)) {
6347 scan += UTF8SKIP(scan);
6351 while (scan < loceol && !isDIGIT_LC(*scan))
6358 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
6364 LNBREAK can match two latin chars, which is ok,
6365 because we have a null terminated string, but we
6366 have to use hardcount in this situation
6368 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
6377 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
6382 while (scan < loceol && is_HORIZWS_latin1(scan))
6389 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
6390 scan += UTF8SKIP(scan);
6394 while (scan < loceol && !is_HORIZWS_latin1(scan))
6402 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
6407 while (scan < loceol && is_VERTWS_latin1(scan))
6415 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
6416 scan += UTF8SKIP(scan);
6420 while (scan < loceol && !is_VERTWS_latin1(scan))
6426 default: /* Called on something of 0 width. */
6427 break; /* So match right here or not at all. */
6433 c = scan - PL_reginput;
6437 GET_RE_DEBUG_FLAGS_DECL;
6439 SV * const prop = sv_newmortal();
6440 regprop(prog, prop, p);
6441 PerlIO_printf(Perl_debug_log,
6442 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
6443 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
6451 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
6453 - regclass_swash - prepare the utf8 swash
6457 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
6463 RXi_GET_DECL(prog,progi);
6464 const struct reg_data * const data = prog ? progi->data : NULL;
6466 PERL_ARGS_ASSERT_REGCLASS_SWASH;
6468 if (data && data->count) {
6469 const U32 n = ARG(node);
6471 if (data->what[n] == 's') {
6472 SV * const rv = MUTABLE_SV(data->data[n]);
6473 AV * const av = MUTABLE_AV(SvRV(rv));
6474 SV **const ary = AvARRAY(av);
6477 /* See the end of regcomp.c:S_regclass() for
6478 * documentation of these array elements. */
6481 a = SvROK(ary[1]) ? &ary[1] : NULL;
6482 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
6486 else if (si && doinit) {
6487 sw = swash_init("utf8", "", si, 1, 0);
6488 (void)av_store(av, 1, sw);
6505 - reginclass - determine if a character falls into a character class
6507 n is the ANYOF regnode
6508 p is the target string
6509 lenp is pointer to the maximum number of bytes of how far to go in p
6510 (This is assumed wthout checking to always be at least the current
6512 utf8_target tells whether p is in UTF-8.
6514 Returns true if matched; false otherwise. If lenp is not NULL, on return
6515 from a successful match, the value it points to will be updated to how many
6516 bytes in p were matched. If there was no match, the value is undefined,
6517 possibly changed from the input.
6519 Note that this can be a synthetic start class, a combination of various
6520 nodes, so things you think might be mutually exclusive, such as locale,
6521 aren't. It can match both locale and non-locale
6526 S_reginclass(pTHX_ const regexp * const prog, register const regnode * const n, register const U8* const p, STRLEN* lenp, register const bool utf8_target)
6529 const char flags = ANYOF_FLAGS(n);
6535 PERL_ARGS_ASSERT_REGINCLASS;
6537 /* If c is not already the code point, get it */
6538 if (utf8_target && !UTF8_IS_INVARIANT(c)) {
6539 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &c_len,
6540 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
6541 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
6542 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
6543 * UTF8_ALLOW_FFFF */
6544 if (c_len == (STRLEN)-1)
6545 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
6551 /* Use passed in max length, or one character if none passed in or less
6552 * than one character. And assume will match just one character. This is
6553 * overwritten later if matched more. */
6555 maxlen = (*lenp > c_len) ? *lenp : c_len;
6563 /* If this character is potentially in the bitmap, check it */
6565 if (ANYOF_BITMAP_TEST(n, c))
6567 else if (flags & ANYOF_NON_UTF8_LATIN1_ALL
6574 else if (flags & ANYOF_LOCALE) {
6575 PL_reg_flags |= RF_tainted;
6577 if ((flags & ANYOF_LOC_NONBITMAP_FOLD)
6578 && ANYOF_BITMAP_TEST(n, PL_fold_locale[c]))
6582 else if (ANYOF_CLASS_TEST_ANY_SET(n) &&
6583 ((ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6584 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6585 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6586 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6587 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6588 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6589 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6590 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6591 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6592 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6593 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6594 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6595 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6596 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6597 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6598 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6599 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6600 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6601 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6602 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6603 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6604 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6605 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6606 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6607 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6608 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6609 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6610 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6611 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6612 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6613 ) /* How's that for a conditional? */
6620 /* If the bitmap didn't (or couldn't) match, and something outside the
6621 * bitmap could match, try that. Locale nodes specifiy completely the
6622 * behavior of code points in the bit map (otherwise, a utf8 target would
6623 * cause them to be treated as Unicode and not locale), except XXX in
6624 * the very unlikely event when this node is a synthetic start class, which
6625 * could be a combination of locale and non-locale nodes */
6627 if (utf8_target && (flags & ANYOF_UNICODE_ALL) && c >= 256) {
6628 match = TRUE; /* Everything above 255 matches */
6630 else if ((flags & ANYOF_NONBITMAP_NON_UTF8
6631 || (utf8_target && flags & ANYOF_UTF8
6632 && (c >=256 || ! (flags & ANYOF_LOCALE)))))
6635 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
6643 /* Not utf8. Convert as much of the string as available up
6644 * to the limit of how far the (single) character in the
6645 * pattern can possibly match (no need to go further). If
6646 * the node is a straight ANYOF or not folding, it can't
6647 * match more than one. Otherwise, It can match up to how
6648 * far a single char can fold to. Since not utf8, each
6649 * character is a single byte, so the max it can be in
6650 * bytes is the same as the max it can be in characters */
6651 STRLEN len = (OP(n) == ANYOF
6652 || ! (flags & ANYOF_LOC_NONBITMAP_FOLD))
6654 : (maxlen < UTF8_MAX_FOLD_CHAR_EXPAND)
6656 : UTF8_MAX_FOLD_CHAR_EXPAND;
6657 utf8_p = bytes_to_utf8(p, &len);
6660 if (swash_fetch(sw, utf8_p, TRUE))
6662 else if (flags & ANYOF_LOC_NONBITMAP_FOLD) {
6664 /* Here, we need to test if the fold of the target string
6665 * matches. In the case of a multi-char fold that is
6666 * caught by regcomp.c, it has stored all such folds into
6667 * 'av'; we linearly check to see if any match the target
6668 * string (folded). We know that the originals were each
6669 * one character, but we don't currently know how many
6670 * characters/bytes each folded to, except we do know that
6671 * there are small limits imposed by Unicode. XXX A
6672 * performance enhancement would be to have regcomp.c store
6673 * the max number of chars/bytes that are in an av entry,
6674 * as, say the 0th element. Even better would be to have a
6675 * hash of the few characters that can start a multi-char
6676 * fold to the max number of chars of those folds.
6678 * Further down, if there isn't a
6679 * match in the av, we will check if there is another
6680 * fold-type match. For that, we also need the fold, but
6681 * only the first character. No sense in folding it twice,
6682 * so we do it here, even if there isn't any multi-char
6683 * fold, so we always fold at least the first character.
6684 * If the node is a straight ANYOF node, or there is only
6685 * one character available in the string, or if there isn't
6686 * any av, that's all we have to fold. In the case of a
6687 * multi-char fold, we do have guarantees in Unicode that
6688 * it can only expand up to so many characters and so many
6689 * bytes. We keep track so don't exceed either.
6691 * If there is a match, we will need to advance (if lenp is
6692 * specified) the match pointer in the target string. But
6693 * what we are comparing here isn't that string directly,
6694 * but its fold, whose length may differ from the original.
6695 * As we go along in constructing the fold, therefore, we
6696 * create a map so that we know how many bytes in the
6697 * source to advance given that we have matched a certain
6698 * number of bytes in the fold. This map is stored in
6699 * 'map_fold_len_back'. The first character in the fold
6700 * has array element 1 contain the number of bytes in the
6701 * source that folded to it; the 2nd is the cumulative
6702 * number to match it; ... */
6703 U8 map_fold_len_back[UTF8_MAX_FOLD_CHAR_EXPAND] = { 0 };
6704 U8 folded[UTF8_MAXBYTES_CASE+1];
6705 STRLEN foldlen = 0; /* num bytes in fold of 1st char */
6706 STRLEN foldlen_for_av; /* num bytes in fold of all chars */
6708 if (OP(n) == ANYOF || maxlen == 1 || ! lenp || ! av) {
6710 /* Here, only need to fold the first char of the target
6712 to_utf8_fold(utf8_p, folded, &foldlen);
6713 foldlen_for_av = foldlen;
6714 map_fold_len_back[1] = UTF8SKIP(utf8_p);
6718 /* Here, need to fold more than the first char. Do so
6719 * up to the limits */
6721 U8* source_ptr = utf8_p; /* The source for the fold
6724 U8* folded_ptr = folded;
6725 U8* e = utf8_p + maxlen; /* Can't go beyond last
6726 available byte in the
6728 while (which_char < UTF8_MAX_FOLD_CHAR_EXPAND
6732 /* Fold the next character */
6733 U8 this_char_folded[UTF8_MAXBYTES_CASE+1];
6734 STRLEN this_char_foldlen;
6735 to_utf8_fold(source_ptr,
6737 &this_char_foldlen);
6739 /* Bail if it would exceed the byte limit for
6740 * folding a single char. */
6741 if (this_char_foldlen + folded_ptr - folded >
6747 /* Save the first character's folded length, in
6748 * case we have to use it later */
6750 foldlen = this_char_foldlen;
6753 /* Here, add the fold of this character */
6754 Copy(this_char_folded,
6759 map_fold_len_back[which_char] =
6760 map_fold_len_back[which_char - 1]
6761 + UTF8SKIP(source_ptr);
6762 folded_ptr += this_char_foldlen;
6763 source_ptr += UTF8SKIP(source_ptr);
6766 foldlen_for_av = folded_ptr - folded;
6770 /* Do the linear search to see if the fold is in the list
6771 * of multi-char folds. (Useless to look if won't be able
6772 * to store that it is a multi-char fold in *lenp) */
6775 for (i = 0; i <= av_len(av); i++) {
6776 SV* const sv = *av_fetch(av, i, FALSE);
6778 const char * const s = SvPV_const(sv, len);
6779 if (len <= foldlen_for_av && memEQ(s,
6784 /* Advance the target string ptr to account for
6785 * this fold, but have to translate from the
6786 * folded length to the corresponding source
6787 * length. The array is indexed by how many
6788 * characters in the match */
6789 *lenp = map_fold_len_back[
6790 utf8_length(folded, folded + len)];
6797 if (!match) { /* See if the folded version matches */
6800 /* Consider "k" =~ /[K]/i. The line above would have
6801 * just folded the 'k' to itself, and that isn't going
6802 * to match 'K'. So we look through the closure of
6803 * everything that folds to 'k'. That will find the
6804 * 'K'. Initialize the list, if necessary */
6805 if (! PL_utf8_foldclosures) {
6807 /* If the folds haven't been read in, call a fold
6808 * function to force that */
6809 if (! PL_utf8_tofold) {
6810 U8 dummy[UTF8_MAXBYTES+1];
6812 to_utf8_fold((U8*) "A", dummy, &dummy_len);
6814 PL_utf8_foldclosures =
6815 _swash_inversion_hash(PL_utf8_tofold);
6818 /* The data structure is a hash with the keys every
6819 * character that is folded to, like 'k', and the
6820 * values each an array of everything that folds to its
6821 * key. e.g. [ 'k', 'K', KELVIN_SIGN ] */
6822 if ((listp = hv_fetch(PL_utf8_foldclosures,
6823 (char *) folded, foldlen, FALSE)))
6825 AV* list = (AV*) *listp;
6827 for (i = 0; i <= av_len(list); i++) {
6828 SV** try_p = av_fetch(list, i, FALSE);
6830 if (try_p == NULL) {
6831 Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
6833 /* Don't have to worry about embedded nulls
6834 * since NULL isn't folded or foldable */
6835 try_c = SvPVX(*try_p);
6837 /* The fold in a few cases of an above Latin1
6838 * char is in the Latin1 range, and hence may
6839 * be in the bitmap */
6840 if (UTF8_IS_INVARIANT(*try_c)
6841 && ANYOF_BITMAP_TEST(n,
6842 UNI_TO_NATIVE(*try_c)))
6848 (UTF8_IS_DOWNGRADEABLE_START(*try_c)
6849 && ANYOF_BITMAP_TEST(n, UNI_TO_NATIVE(
6850 TWO_BYTE_UTF8_TO_UNI(try_c[0],
6853 /* Since the fold comes from internally
6854 * generated data, we can safely assume it
6855 * is valid utf8 in the test above */
6858 } else if (swash_fetch(sw, (U8*) try_c, TRUE)) {
6868 /* If we allocated a string above, free it */
6869 if (! utf8_target) Safefree(utf8_p);
6874 return (flags & ANYOF_INVERT) ? !match : match;
6878 S_reghop3(U8 *s, I32 off, const U8* lim)
6882 PERL_ARGS_ASSERT_REGHOP3;
6885 while (off-- && s < lim) {
6886 /* XXX could check well-formedness here */
6891 while (off++ && s > lim) {
6893 if (UTF8_IS_CONTINUED(*s)) {
6894 while (s > lim && UTF8_IS_CONTINUATION(*s))
6897 /* XXX could check well-formedness here */
6904 /* there are a bunch of places where we use two reghop3's that should
6905 be replaced with this routine. but since thats not done yet
6906 we ifdef it out - dmq
6909 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6913 PERL_ARGS_ASSERT_REGHOP4;
6916 while (off-- && s < rlim) {
6917 /* XXX could check well-formedness here */
6922 while (off++ && s > llim) {
6924 if (UTF8_IS_CONTINUED(*s)) {
6925 while (s > llim && UTF8_IS_CONTINUATION(*s))
6928 /* XXX could check well-formedness here */
6936 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
6940 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6943 while (off-- && s < lim) {
6944 /* XXX could check well-formedness here */
6951 while (off++ && s > lim) {
6953 if (UTF8_IS_CONTINUED(*s)) {
6954 while (s > lim && UTF8_IS_CONTINUATION(*s))
6957 /* XXX could check well-formedness here */
6966 restore_pos(pTHX_ void *arg)
6969 regexp * const rex = (regexp *)arg;
6970 if (PL_reg_eval_set) {
6971 if (PL_reg_oldsaved) {
6972 rex->subbeg = PL_reg_oldsaved;
6973 rex->sublen = PL_reg_oldsavedlen;
6974 #ifdef PERL_OLD_COPY_ON_WRITE
6975 rex->saved_copy = PL_nrs;
6977 RXp_MATCH_COPIED_on(rex);
6979 PL_reg_magic->mg_len = PL_reg_oldpos;
6980 PL_reg_eval_set = 0;
6981 PL_curpm = PL_reg_oldcurpm;
6986 S_to_utf8_substr(pTHX_ register regexp *prog)
6990 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6993 if (prog->substrs->data[i].substr
6994 && !prog->substrs->data[i].utf8_substr) {
6995 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6996 prog->substrs->data[i].utf8_substr = sv;
6997 sv_utf8_upgrade(sv);
6998 if (SvVALID(prog->substrs->data[i].substr)) {
6999 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
7000 if (flags & FBMcf_TAIL) {
7001 /* Trim the trailing \n that fbm_compile added last
7003 SvCUR_set(sv, SvCUR(sv) - 1);
7004 /* Whilst this makes the SV technically "invalid" (as its
7005 buffer is no longer followed by "\0") when fbm_compile()
7006 adds the "\n" back, a "\0" is restored. */
7008 fbm_compile(sv, flags);
7010 if (prog->substrs->data[i].substr == prog->check_substr)
7011 prog->check_utf8 = sv;
7017 S_to_byte_substr(pTHX_ register regexp *prog)
7022 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
7025 if (prog->substrs->data[i].utf8_substr
7026 && !prog->substrs->data[i].substr) {
7027 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
7028 if (sv_utf8_downgrade(sv, TRUE)) {
7029 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
7031 = BmFLAGS(prog->substrs->data[i].utf8_substr);
7032 if (flags & FBMcf_TAIL) {
7033 /* Trim the trailing \n that fbm_compile added last
7035 SvCUR_set(sv, SvCUR(sv) - 1);
7037 fbm_compile(sv, flags);
7043 prog->substrs->data[i].substr = sv;
7044 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
7045 prog->check_substr = sv;
7052 * c-indentation-style: bsd
7054 * indent-tabs-mode: t
7057 * ex: set ts=8 sts=4 sw=4 noet: