5 * One Ring to rule them all, One Ring to find them
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
12 /* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
14 * a regular expression.
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
26 /* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
31 /* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
36 #ifdef PERL_EXT_RE_BUILD
41 * pregcomp and pregexec -- regsub and regerror are not used in perl
43 * Copyright (c) 1986 by University of Toronto.
44 * Written by Henry Spencer. Not derived from licensed software.
46 * Permission is granted to anyone to use this software for any
47 * purpose on any computer system, and to redistribute it freely,
48 * subject to the following restrictions:
50 * 1. The author is not responsible for the consequences of use of
51 * this software, no matter how awful, even if they arise
54 * 2. The origin of this software must not be misrepresented, either
55 * by explicit claim or by omission.
57 * 3. Altered versions must be plainly marked as such, and must not
58 * be misrepresented as being the original software.
60 **** Alterations to Henry's code are...
62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
66 **** You may distribute under the terms of either the GNU General Public
67 **** License or the Artistic License, as specified in the README file.
69 * Beware that some of this code is subtly aware of the way operator
70 * precedence is structured in regular expressions. Serious changes in
71 * regular-expression syntax might require a total rethink.
74 #define PERL_IN_REGEXEC_C
78 #ifdef PERL_IN_XSUB_RE
84 #define RF_tainted 1 /* tainted information used? e.g. locale */
85 #define RF_warned 2 /* warned about big count? */
87 #define RF_utf8 8 /* Pattern contains multibyte chars? */
89 #define UTF_PATTERN ((PL_reg_flags & RF_utf8) != 0)
95 /* Valid for non-utf8 strings, non-ANYOFV nodes only: avoids the reginclass
96 * call if there are no complications: i.e., if everything matchable is
97 * straight forward in the bitmap */
98 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) \
99 : ANYOF_BITMAP_TEST(p,*(c)))
105 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
106 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
108 #define HOPc(pos,off) \
109 (char *)(PL_reg_match_utf8 \
110 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
112 #define HOPBACKc(pos, off) \
113 (char*)(PL_reg_match_utf8\
114 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
115 : (pos - off >= PL_bostr) \
119 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
120 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
122 /* these are unrolled below in the CCC_TRY_XXX defined */
124 /* Often 'str' is a hard-coded utf8 string instead of utfebcdic. so just
125 * skip the check on EBCDIC platforms */
126 # define LOAD_UTF8_CHARCLASS(class,str) LOAD_UTF8_CHARCLASS_NO_CHECK(class)
128 # define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
129 if (!CAT2(PL_utf8_,class)) { \
131 ENTER; save_re_context(); \
132 ok=CAT2(is_utf8_,class)((const U8*)str); \
133 PERL_UNUSED_VAR(ok); \
134 assert(ok); assert(CAT2(PL_utf8_,class)); LEAVE; } } STMT_END
137 /* Doesn't do an assert to verify that is correct */
138 #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
139 if (!CAT2(PL_utf8_,class)) { \
140 bool throw_away PERL_UNUSED_DECL; \
141 ENTER; save_re_context(); \
142 throw_away = CAT2(is_utf8_,class)((const U8*)" "); \
145 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
146 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
147 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
149 #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
150 LOAD_UTF8_CHARCLASS(X_begin, " "); \
151 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
152 /* These are utf8 constants, and not utf-ebcdic constants, so the \
153 * assert should likely and hopefully fail on an EBCDIC machine */ \
154 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
156 /* No asserts are done for these, in case called on an early \
157 * Unicode version in which they map to nothing */ \
158 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
159 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
160 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
161 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
162 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
163 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
164 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
166 #define PLACEHOLDER /* Something for the preprocessor to grab onto */
168 /* The actual code for CCC_TRY, which uses several variables from the routine
169 * it's callable from. It is designed to be the bulk of a case statement.
170 * FUNC is the macro or function to call on non-utf8 targets that indicate if
171 * nextchr matches the class.
172 * UTF8_TEST is the whole test string to use for utf8 targets
173 * LOAD is what to use to test, and if not present to load in the swash for the
175 * POS_OR_NEG is either empty or ! to complement the results of FUNC or
177 * The logic is: Fail if we're at the end-of-string; otherwise if the target is
178 * utf8 and a variant, load the swash if necessary and test using the utf8
179 * test. Advance to the next character if test is ok, otherwise fail; If not
180 * utf8 or an invariant under utf8, use the non-utf8 test, and fail if it
181 * fails, or advance to the next character */
183 #define _CCC_TRY_CODE(POS_OR_NEG, FUNC, UTF8_TEST, CLASS, STR) \
184 if (locinput >= PL_regeol) { \
187 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
188 LOAD_UTF8_CHARCLASS(CLASS, STR); \
189 if (POS_OR_NEG (UTF8_TEST)) { \
192 locinput += PL_utf8skip[nextchr]; \
193 nextchr = UCHARAT(locinput); \
196 if (POS_OR_NEG (FUNC(nextchr))) { \
199 nextchr = UCHARAT(++locinput); \
202 /* Handle the non-locale cases for a character class and its complement. It
203 * calls _CCC_TRY_CODE with a ! to complement the test for the character class.
204 * This is because that code fails when the test succeeds, so we want to have
205 * the test fail so that the code succeeds. The swash is stored in a
206 * predictable PL_ place */
207 #define _CCC_TRY_NONLOCALE(NAME, NNAME, FUNC, \
210 _CCC_TRY_CODE( !, FUNC, \
211 cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), \
212 (U8*)locinput, TRUE)), \
215 _CCC_TRY_CODE( PLACEHOLDER , FUNC, \
216 cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), \
217 (U8*)locinput, TRUE)), \
220 /* Generate the case statements for both locale and non-locale character
221 * classes in regmatch for classes that don't have special unicode semantics.
222 * Locales don't use an immediate swash, but an intermediary special locale
223 * function that is called on the pointer to the current place in the input
224 * string. That function will resolve to needing the same swash. One might
225 * think that because we don't know what the locale will match, we shouldn't
226 * check with the swash loading function that it loaded properly; ie, that we
227 * should use LOAD_UTF8_CHARCLASS_NO_CHECK for those, but what is passed to the
228 * regular LOAD_UTF8_CHARCLASS is in non-locale terms, and so locale is
230 #define CCC_TRY(NAME, NNAME, FUNC, \
231 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
232 NAMEA, NNAMEA, FUNCA, \
235 PL_reg_flags |= RF_tainted; \
236 _CCC_TRY_CODE( !, LCFUNC, LCFUNC_utf8((U8*)locinput), CLASS, STR) \
238 PL_reg_flags |= RF_tainted; \
239 _CCC_TRY_CODE( PLACEHOLDER, LCFUNC, LCFUNC_utf8((U8*)locinput), \
242 if (locinput >= PL_regeol || ! FUNCA(nextchr)) { \
245 /* Matched a utf8-invariant, so don't have to worry about utf8 */ \
246 nextchr = UCHARAT(++locinput); \
249 if (locinput >= PL_regeol || FUNCA(nextchr)) { \
253 locinput += PL_utf8skip[nextchr]; \
254 nextchr = UCHARAT(locinput); \
257 nextchr = UCHARAT(++locinput); \
260 /* Generate the non-locale cases */ \
261 _CCC_TRY_NONLOCALE(NAME, NNAME, FUNC, CLASS, STR)
263 /* This is like CCC_TRY, but has an extra set of parameters for generating case
264 * statements to handle separate Unicode semantics nodes */
265 #define CCC_TRY_U(NAME, NNAME, FUNC, \
266 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
267 NAMEU, NNAMEU, FUNCU, \
268 NAMEA, NNAMEA, FUNCA, \
270 CCC_TRY(NAME, NNAME, FUNC, \
271 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
272 NAMEA, NNAMEA, FUNCA, \
274 _CCC_TRY_NONLOCALE(NAMEU, NNAMEU, FUNCU, CLASS, STR)
276 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
278 /* for use after a quantifier and before an EXACT-like node -- japhy */
279 /* it would be nice to rework regcomp.sym to generate this stuff. sigh
281 * NOTE that *nothing* that affects backtracking should be in here, specifically
282 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
283 * node that is in between two EXACT like nodes when ascertaining what the required
284 * "follow" character is. This should probably be moved to regex compile time
285 * although it may be done at run time beause of the REF possibility - more
286 * investigation required. -- demerphq
288 #define JUMPABLE(rn) ( \
290 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
292 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
293 OP(rn) == PLUS || OP(rn) == MINMOD || \
295 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
297 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
299 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
302 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
303 we don't need this definition. */
304 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
305 #define IS_TEXTF(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFU_TRICKYFOLD || OP(rn)==EXACTFA || OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
306 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
309 /* ... so we use this as its faster. */
310 #define IS_TEXT(rn) ( OP(rn)==EXACT )
311 #define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFU_TRICKYFOLD || OP(rn) == EXACTFA)
312 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
313 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
318 Search for mandatory following text node; for lookahead, the text must
319 follow but for lookbehind (rn->flags != 0) we skip to the next step.
321 #define FIND_NEXT_IMPT(rn) STMT_START { \
322 while (JUMPABLE(rn)) { \
323 const OPCODE type = OP(rn); \
324 if (type == SUSPEND || PL_regkind[type] == CURLY) \
325 rn = NEXTOPER(NEXTOPER(rn)); \
326 else if (type == PLUS) \
328 else if (type == IFMATCH) \
329 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
330 else rn += NEXT_OFF(rn); \
335 static void restore_pos(pTHX_ void *arg);
337 #define REGCP_PAREN_ELEMS 3
338 #define REGCP_OTHER_ELEMS 3
339 #define REGCP_FRAME_ELEMS 1
340 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
341 * are needed for the regexp context stack bookkeeping. */
344 S_regcppush(pTHX_ const regexp *rex, I32 parenfloor)
347 const int retval = PL_savestack_ix;
348 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
349 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
350 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
352 GET_RE_DEBUG_FLAGS_DECL;
354 PERL_ARGS_ASSERT_REGCPPUSH;
356 if (paren_elems_to_push < 0)
357 Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0",
358 paren_elems_to_push);
360 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
361 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
362 " out of range (%lu-%ld)",
363 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
365 SSGROW(total_elems + REGCP_FRAME_ELEMS);
368 if ((int)PL_regsize > (int)parenfloor)
369 PerlIO_printf(Perl_debug_log,
370 "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n",
375 for (p = parenfloor+1; p <= (I32)PL_regsize; p++) {
376 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
377 SSPUSHINT(rex->offs[p].end);
378 SSPUSHINT(rex->offs[p].start);
379 SSPUSHINT(rex->offs[p].start_tmp);
380 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
381 " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n",
383 (IV)rex->offs[p].start,
384 (IV)rex->offs[p].start_tmp,
388 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
389 SSPUSHINT(PL_regsize);
390 SSPUSHINT(rex->lastparen);
391 SSPUSHINT(rex->lastcloseparen);
392 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
397 /* These are needed since we do not localize EVAL nodes: */
398 #define REGCP_SET(cp) \
400 PerlIO_printf(Perl_debug_log, \
401 " Setting an EVAL scope, savestack=%"IVdf"\n", \
402 (IV)PL_savestack_ix)); \
405 #define REGCP_UNWIND(cp) \
407 if (cp != PL_savestack_ix) \
408 PerlIO_printf(Perl_debug_log, \
409 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
410 (IV)(cp), (IV)PL_savestack_ix)); \
413 #define UNWIND_PAREN(lp, lcp) \
414 for (n = rex->lastparen; n > lp; n--) \
415 rex->offs[n].end = -1; \
416 rex->lastparen = n; \
417 rex->lastcloseparen = lcp;
421 S_regcppop(pTHX_ 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 rex->lastcloseparen = SSPOPINT;
435 rex->lastparen = SSPOPINT;
436 PL_regsize = SSPOPINT;
438 i -= REGCP_OTHER_ELEMS;
439 /* Now restore the parentheses context. */
441 if (i || rex->lastparen + 1 <= rex->nparens)
442 PerlIO_printf(Perl_debug_log,
443 "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n",
449 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
451 rex->offs[paren].start_tmp = SSPOPINT;
452 rex->offs[paren].start = SSPOPINT;
454 if (paren <= rex->lastparen)
455 rex->offs[paren].end = tmps;
456 DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
457 " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n",
459 (IV)rex->offs[paren].start,
460 (IV)rex->offs[paren].start_tmp,
461 (IV)rex->offs[paren].end,
462 (paren > rex->lastparen ? "(skipped)" : ""));
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 = rex->lastparen + 1; i <= rex->nparens; i++) {
478 rex->offs[i].start = -1;
479 rex->offs[i].end = -1;
480 DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
481 " \\%"UVuf": %s ..-1 undeffing\n",
483 (i > PL_regsize) ? "-1" : " "
489 /* restore the parens and associated vars at savestack position ix,
490 * but without popping the stack */
493 S_regcp_restore(pTHX_ regexp *rex, I32 ix)
495 I32 tmpix = PL_savestack_ix;
496 PL_savestack_ix = ix;
498 PL_savestack_ix = tmpix;
501 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
504 * pregexec and friends
507 #ifndef PERL_IN_XSUB_RE
509 - pregexec - match a regexp against a string
512 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
513 char *strbeg, I32 minend, SV *screamer, U32 nosave)
514 /* strend: pointer to null at end of string */
515 /* strbeg: real beginning of string */
516 /* minend: end of match must be >=minend after stringarg. */
517 /* nosave: For optimizations. */
519 PERL_ARGS_ASSERT_PREGEXEC;
522 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
523 nosave ? 0 : REXEC_COPY_STR);
528 * Need to implement the following flags for reg_anch:
530 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
532 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
533 * INTUIT_AUTORITATIVE_ML
534 * INTUIT_ONCE_NOML - Intuit can match in one location only.
537 * Another flag for this function: SECOND_TIME (so that float substrs
538 * with giant delta may be not rechecked).
541 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
543 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
544 Otherwise, only SvCUR(sv) is used to get strbeg. */
546 /* XXXX We assume that strpos is strbeg unless sv. */
548 /* XXXX Some places assume that there is a fixed substring.
549 An update may be needed if optimizer marks as "INTUITable"
550 RExen without fixed substrings. Similarly, it is assumed that
551 lengths of all the strings are no more than minlen, thus they
552 cannot come from lookahead.
553 (Or minlen should take into account lookahead.)
554 NOTE: Some of this comment is not correct. minlen does now take account
555 of lookahead/behind. Further research is required. -- demerphq
559 /* A failure to find a constant substring means that there is no need to make
560 an expensive call to REx engine, thus we celebrate a failure. Similarly,
561 finding a substring too deep into the string means that less calls to
562 regtry() should be needed.
564 REx compiler's optimizer found 4 possible hints:
565 a) Anchored substring;
567 c) Whether we are anchored (beginning-of-line or \G);
568 d) First node (of those at offset 0) which may distinguish positions;
569 We use a)b)d) and multiline-part of c), and try to find a position in the
570 string which does not contradict any of them.
573 /* Most of decisions we do here should have been done at compile time.
574 The nodes of the REx which we used for the search should have been
575 deleted from the finite automaton. */
578 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
579 char *strend, const U32 flags, re_scream_pos_data *data)
582 struct regexp *const prog = (struct regexp *)SvANY(rx);
583 register I32 start_shift = 0;
584 /* Should be nonnegative! */
585 register I32 end_shift = 0;
590 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
592 register char *other_last = NULL; /* other substr checked before this */
593 char *check_at = NULL; /* check substr found at this pos */
594 char *checked_upto = NULL; /* how far into the string we have already checked using find_byclass*/
595 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
596 RXi_GET_DECL(prog,progi);
598 const char * const i_strpos = strpos;
600 GET_RE_DEBUG_FLAGS_DECL;
602 PERL_ARGS_ASSERT_RE_INTUIT_START;
603 PERL_UNUSED_ARG(flags);
604 PERL_UNUSED_ARG(data);
606 RX_MATCH_UTF8_set(rx,utf8_target);
609 PL_reg_flags |= RF_utf8;
612 debug_start_match(rx, utf8_target, strpos, strend,
613 sv ? "Guessing start of match in sv for"
614 : "Guessing start of match in string for");
617 /* CHR_DIST() would be more correct here but it makes things slow. */
618 if (prog->minlen > strend - strpos) {
619 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
620 "String too short... [re_intuit_start]\n"));
624 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
627 if (!prog->check_utf8 && prog->check_substr)
628 to_utf8_substr(prog);
629 check = prog->check_utf8;
631 if (!prog->check_substr && prog->check_utf8)
632 to_byte_substr(prog);
633 check = prog->check_substr;
635 if (check == &PL_sv_undef) {
636 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
637 "Non-utf8 string cannot match utf8 check string\n"));
640 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
641 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
642 || ( (prog->extflags & RXf_ANCH_BOL)
643 && !multiline ) ); /* Check after \n? */
646 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
647 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
648 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
650 && (strpos != strbeg)) {
651 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
654 if (prog->check_offset_min == prog->check_offset_max &&
655 !(prog->extflags & RXf_CANY_SEEN)) {
656 /* Substring at constant offset from beg-of-str... */
659 s = HOP3c(strpos, prog->check_offset_min, strend);
662 slen = SvCUR(check); /* >= 1 */
664 if ( strend - s > slen || strend - s < slen - 1
665 || (strend - s == slen && strend[-1] != '\n')) {
666 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
669 /* Now should match s[0..slen-2] */
671 if (slen && (*SvPVX_const(check) != *s
673 && memNE(SvPVX_const(check), s, slen)))) {
675 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
679 else if (*SvPVX_const(check) != *s
680 || ((slen = SvCUR(check)) > 1
681 && memNE(SvPVX_const(check), s, slen)))
684 goto success_at_start;
687 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
689 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
690 end_shift = prog->check_end_shift;
693 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
694 - (SvTAIL(check) != 0);
695 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
697 if (end_shift < eshift)
701 else { /* Can match at random position */
704 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
705 end_shift = prog->check_end_shift;
707 /* end shift should be non negative here */
710 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
712 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
713 (IV)end_shift, RX_PRECOMP(prog));
717 /* Find a possible match in the region s..strend by looking for
718 the "check" substring in the region corrected by start/end_shift. */
721 I32 srch_start_shift = start_shift;
722 I32 srch_end_shift = end_shift;
725 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
726 srch_end_shift -= ((strbeg - s) - srch_start_shift);
727 srch_start_shift = strbeg - s;
729 DEBUG_OPTIMISE_MORE_r({
730 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
731 (IV)prog->check_offset_min,
732 (IV)srch_start_shift,
734 (IV)prog->check_end_shift);
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);
754 /* Update the count-of-usability, remove useless subpatterns,
758 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
759 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
760 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
761 (s ? "Found" : "Did not find"),
762 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
763 ? "anchored" : "floating"),
766 (s ? " at offset " : "...\n") );
771 /* Finish the diagnostic message */
772 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
774 /* XXX dmq: first branch is for positive lookbehind...
775 Our check string is offset from the beginning of the pattern.
776 So we need to do any stclass tests offset forward from that
785 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
786 Start with the other substr.
787 XXXX no SCREAM optimization yet - and a very coarse implementation
788 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
789 *always* match. Probably should be marked during compile...
790 Probably it is right to do no SCREAM here...
793 if (utf8_target ? (prog->float_utf8 && prog->anchored_utf8)
794 : (prog->float_substr && prog->anchored_substr))
796 /* Take into account the "other" substring. */
797 /* XXXX May be hopelessly wrong for UTF... */
800 if (check == (utf8_target ? prog->float_utf8 : prog->float_substr)) {
803 char * const last = HOP3c(s, -start_shift, strbeg);
805 char * const saved_s = s;
808 t = s - prog->check_offset_max;
809 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
811 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
816 t = HOP3c(t, prog->anchored_offset, strend);
817 if (t < other_last) /* These positions already checked */
819 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
822 /* XXXX It is not documented what units *_offsets are in.
823 We assume bytes, but this is clearly wrong.
824 Meaning this code needs to be carefully reviewed for errors.
828 /* On end-of-str: see comment below. */
829 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
830 if (must == &PL_sv_undef) {
832 DEBUG_r(must = prog->anchored_utf8); /* for debug */
837 HOP3(HOP3(last1, prog->anchored_offset, strend)
838 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
840 multiline ? FBMrf_MULTILINE : 0
843 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
844 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
845 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
846 (s ? "Found" : "Contradicts"),
847 quoted, RE_SV_TAIL(must));
852 if (last1 >= last2) {
853 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
854 ", giving up...\n"));
857 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
858 ", trying floating at offset %ld...\n",
859 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
860 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
861 s = HOP3c(last, 1, strend);
865 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
866 (long)(s - i_strpos)));
867 t = HOP3c(s, -prog->anchored_offset, strbeg);
868 other_last = HOP3c(s, 1, strend);
876 else { /* Take into account the floating substring. */
878 char * const saved_s = s;
881 t = HOP3c(s, -start_shift, strbeg);
883 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
884 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
885 last = HOP3c(t, prog->float_max_offset, strend);
886 s = HOP3c(t, prog->float_min_offset, strend);
889 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
890 must = utf8_target ? prog->float_utf8 : prog->float_substr;
891 /* fbm_instr() takes into account exact value of end-of-str
892 if the check is SvTAIL(ed). Since false positives are OK,
893 and end-of-str is not later than strend we are OK. */
894 if (must == &PL_sv_undef) {
896 DEBUG_r(must = prog->float_utf8); /* for debug message */
899 s = fbm_instr((unsigned char*)s,
900 (unsigned char*)last + SvCUR(must)
902 must, multiline ? FBMrf_MULTILINE : 0);
904 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
905 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
906 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
907 (s ? "Found" : "Contradicts"),
908 quoted, RE_SV_TAIL(must));
912 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
913 ", giving up...\n"));
916 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
917 ", trying anchored starting at offset %ld...\n",
918 (long)(saved_s + 1 - i_strpos)));
920 s = HOP3c(t, 1, strend);
924 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
925 (long)(s - i_strpos)));
926 other_last = s; /* Fix this later. --Hugo */
936 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
938 DEBUG_OPTIMISE_MORE_r(
939 PerlIO_printf(Perl_debug_log,
940 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
941 (IV)prog->check_offset_min,
942 (IV)prog->check_offset_max,
950 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
952 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
955 /* Fixed substring is found far enough so that the match
956 cannot start at strpos. */
958 if (ml_anch && t[-1] != '\n') {
959 /* Eventually fbm_*() should handle this, but often
960 anchored_offset is not 0, so this check will not be wasted. */
961 /* XXXX In the code below we prefer to look for "^" even in
962 presence of anchored substrings. And we search even
963 beyond the found float position. These pessimizations
964 are historical artefacts only. */
966 while (t < strend - prog->minlen) {
968 if (t < check_at - prog->check_offset_min) {
969 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
970 /* Since we moved from the found position,
971 we definitely contradict the found anchored
972 substr. Due to the above check we do not
973 contradict "check" substr.
974 Thus we can arrive here only if check substr
975 is float. Redo checking for "other"=="fixed".
978 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
979 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
980 goto do_other_anchored;
982 /* We don't contradict the found floating substring. */
983 /* XXXX Why not check for STCLASS? */
985 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
986 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
989 /* Position contradicts check-string */
990 /* XXXX probably better to look for check-string
991 than for "\n", so one should lower the limit for t? */
992 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
993 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
994 other_last = strpos = s = t + 1;
999 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
1000 PL_colors[0], PL_colors[1]));
1004 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
1005 PL_colors[0], PL_colors[1]));
1009 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
1012 /* The found string does not prohibit matching at strpos,
1013 - no optimization of calling REx engine can be performed,
1014 unless it was an MBOL and we are not after MBOL,
1015 or a future STCLASS check will fail this. */
1017 /* Even in this situation we may use MBOL flag if strpos is offset
1018 wrt the start of the string. */
1019 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
1020 && (strpos != strbeg) && strpos[-1] != '\n'
1021 /* May be due to an implicit anchor of m{.*foo} */
1022 && !(prog->intflags & PREGf_IMPLICIT))
1027 DEBUG_EXECUTE_r( if (ml_anch)
1028 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
1029 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
1032 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
1034 prog->check_utf8 /* Could be deleted already */
1035 && --BmUSEFUL(prog->check_utf8) < 0
1036 && (prog->check_utf8 == prog->float_utf8)
1038 prog->check_substr /* Could be deleted already */
1039 && --BmUSEFUL(prog->check_substr) < 0
1040 && (prog->check_substr == prog->float_substr)
1043 /* If flags & SOMETHING - do not do it many times on the same match */
1044 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
1045 /* XXX Does the destruction order has to change with utf8_target? */
1046 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1047 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1048 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1049 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1050 check = NULL; /* abort */
1052 /* XXXX If the check string was an implicit check MBOL, then we need to unset the relevant flag
1053 see http://bugs.activestate.com/show_bug.cgi?id=87173 */
1054 if (prog->intflags & PREGf_IMPLICIT)
1055 prog->extflags &= ~RXf_ANCH_MBOL;
1056 /* XXXX This is a remnant of the old implementation. It
1057 looks wasteful, since now INTUIT can use many
1058 other heuristics. */
1059 prog->extflags &= ~RXf_USE_INTUIT;
1060 /* XXXX What other flags might need to be cleared in this branch? */
1066 /* Last resort... */
1067 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1068 /* trie stclasses are too expensive to use here, we are better off to
1069 leave it to regmatch itself */
1070 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1071 /* minlen == 0 is possible if regstclass is \b or \B,
1072 and the fixed substr is ''$.
1073 Since minlen is already taken into account, s+1 is before strend;
1074 accidentally, minlen >= 1 guaranties no false positives at s + 1
1075 even for \b or \B. But (minlen? 1 : 0) below assumes that
1076 regstclass does not come from lookahead... */
1077 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1078 This leaves EXACTF-ish only, which are dealt with in find_byclass(). */
1079 const U8* const str = (U8*)STRING(progi->regstclass);
1080 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1081 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1084 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1085 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1086 else if (prog->float_substr || prog->float_utf8)
1087 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1091 if (checked_upto < s)
1093 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf" checked_upto: %"IVdf"\n",
1094 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg), (IV)(checked_upto- strbeg)));
1097 s = find_byclass(prog, progi->regstclass, checked_upto, endpos, NULL);
1102 const char *what = NULL;
1104 if (endpos == strend) {
1105 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1106 "Could not match STCLASS...\n") );
1109 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1110 "This position contradicts STCLASS...\n") );
1111 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1113 checked_upto = HOPBACKc(endpos, start_shift);
1114 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" endpos: %"IVdf" checked_upto: %"IVdf"\n",
1115 (IV)start_shift, (IV)(check_at - strbeg), (IV)(endpos - strbeg), (IV)(checked_upto- strbeg)));
1116 /* Contradict one of substrings */
1117 if (prog->anchored_substr || prog->anchored_utf8) {
1118 if ((utf8_target ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1119 DEBUG_EXECUTE_r( what = "anchored" );
1121 s = HOP3c(t, 1, strend);
1122 if (s + start_shift + end_shift > strend) {
1123 /* XXXX Should be taken into account earlier? */
1124 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1125 "Could not match STCLASS...\n") );
1130 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1131 "Looking for %s substr starting at offset %ld...\n",
1132 what, (long)(s + start_shift - i_strpos)) );
1135 /* Have both, check_string is floating */
1136 if (t + start_shift >= check_at) /* Contradicts floating=check */
1137 goto retry_floating_check;
1138 /* Recheck anchored substring, but not floating... */
1142 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1143 "Looking for anchored substr starting at offset %ld...\n",
1144 (long)(other_last - i_strpos)) );
1145 goto do_other_anchored;
1147 /* Another way we could have checked stclass at the
1148 current position only: */
1153 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1154 "Looking for /%s^%s/m starting at offset %ld...\n",
1155 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1158 if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1160 /* Check is floating substring. */
1161 retry_floating_check:
1162 t = check_at - start_shift;
1163 DEBUG_EXECUTE_r( what = "floating" );
1164 goto hop_and_restart;
1167 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1168 "By STCLASS: moving %ld --> %ld\n",
1169 (long)(t - i_strpos), (long)(s - i_strpos))
1173 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1174 "Does not contradict STCLASS...\n");
1179 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1180 PL_colors[4], (check ? "Guessed" : "Giving up"),
1181 PL_colors[5], (long)(s - i_strpos)) );
1184 fail_finish: /* Substring not found */
1185 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1186 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1188 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1189 PL_colors[4], PL_colors[5]));
1193 #define DECL_TRIE_TYPE(scan) \
1194 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1195 trie_type = ((scan->flags == EXACT) \
1196 ? (utf8_target ? trie_utf8 : trie_plain) \
1197 : (utf8_target ? trie_utf8_fold : trie_latin_utf8_fold))
1199 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1200 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1202 switch (trie_type) { \
1203 case trie_utf8_fold: \
1204 if ( foldlen>0 ) { \
1205 uvc = utf8n_to_uvuni( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1210 uvc = to_utf8_fold( (const U8*) uc, foldbuf, &foldlen ); \
1211 len = UTF8SKIP(uc); \
1212 skiplen = UNISKIP( uvc ); \
1213 foldlen -= skiplen; \
1214 uscan = foldbuf + skiplen; \
1217 case trie_latin_utf8_fold: \
1218 if ( foldlen>0 ) { \
1219 uvc = utf8n_to_uvuni( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1225 uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, 1); \
1226 skiplen = UNISKIP( uvc ); \
1227 foldlen -= skiplen; \
1228 uscan = foldbuf + skiplen; \
1232 uvc = utf8n_to_uvuni( (const U8*) uc, UTF8_MAXLEN, &len, uniflags ); \
1239 charid = trie->charmap[ uvc ]; \
1243 if (widecharmap) { \
1244 SV** const svpp = hv_fetch(widecharmap, \
1245 (char*)&uvc, sizeof(UV), 0); \
1247 charid = (U16)SvIV(*svpp); \
1252 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1256 && (ln == 1 || folder(s, pat_string, ln)) \
1257 && (!reginfo || regtry(reginfo, &s)) ) \
1263 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1265 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1271 #define REXEC_FBC_SCAN(CoDe) \
1273 while (s < strend) { \
1279 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1280 REXEC_FBC_UTF8_SCAN( \
1282 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1291 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1294 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1303 #define REXEC_FBC_TRYIT \
1304 if ((!reginfo || regtry(reginfo, &s))) \
1307 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1308 if (utf8_target) { \
1309 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1312 REXEC_FBC_CLASS_SCAN(CoNd); \
1315 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1316 if (utf8_target) { \
1318 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1321 REXEC_FBC_CLASS_SCAN(CoNd); \
1324 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1325 PL_reg_flags |= RF_tainted; \
1326 if (utf8_target) { \
1327 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1330 REXEC_FBC_CLASS_SCAN(CoNd); \
1333 #define DUMP_EXEC_POS(li,s,doutf8) \
1334 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1337 #define UTF8_NOLOAD(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
1338 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; \
1339 tmp = TEST_NON_UTF8(tmp); \
1340 REXEC_FBC_UTF8_SCAN( \
1341 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1350 #define UTF8_LOAD(TeSt1_UtF8, TeSt2_UtF8, IF_SUCCESS, IF_FAIL) \
1351 if (s == PL_bostr) { \
1355 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); \
1356 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); \
1359 LOAD_UTF8_CHARCLASS_ALNUM(); \
1360 REXEC_FBC_UTF8_SCAN( \
1361 if (tmp == ! (TeSt2_UtF8)) { \
1370 /* The only difference between the BOUND and NBOUND cases is that
1371 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1372 * NBOUND. This is accomplished by passing it in either the if or else clause,
1373 * with the other one being empty */
1374 #define FBC_BOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1375 FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1377 #define FBC_BOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1378 FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1380 #define FBC_NBOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1381 FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1383 #define FBC_NBOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1384 FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1387 /* Common to the BOUND and NBOUND cases. Unfortunately the UTF8 tests need to
1388 * be passed in completely with the variable name being tested, which isn't
1389 * such a clean interface, but this is easier to read than it was before. We
1390 * are looking for the boundary (or non-boundary between a word and non-word
1391 * character. The utf8 and non-utf8 cases have the same logic, but the details
1392 * must be different. Find the "wordness" of the character just prior to this
1393 * one, and compare it with the wordness of this one. If they differ, we have
1394 * a boundary. At the beginning of the string, pretend that the previous
1395 * character was a new-line */
1396 #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
1397 if (utf8_target) { \
1400 else { /* Not utf8 */ \
1401 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; \
1402 tmp = TEST_NON_UTF8(tmp); \
1404 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1413 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s))) \
1416 /* We know what class REx starts with. Try to find this position... */
1417 /* if reginfo is NULL, its a dryrun */
1418 /* annoyingly all the vars in this routine have different names from their counterparts
1419 in regmatch. /grrr */
1422 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1423 const char *strend, regmatch_info *reginfo)
1426 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1427 char *pat_string; /* The pattern's exactish string */
1428 char *pat_end; /* ptr to end char of pat_string */
1429 re_fold_t folder; /* Function for computing non-utf8 folds */
1430 const U8 *fold_array; /* array for folding ords < 256 */
1433 register STRLEN uskip;
1437 register I32 tmp = 1; /* Scratch variable? */
1438 register const bool utf8_target = PL_reg_match_utf8;
1439 UV utf8_fold_flags = 0;
1440 RXi_GET_DECL(prog,progi);
1442 PERL_ARGS_ASSERT_FIND_BYCLASS;
1444 /* We know what class it must start with. */
1448 if (utf8_target || OP(c) == ANYOFV) {
1449 STRLEN inclasslen = strend - s;
1450 REXEC_FBC_UTF8_CLASS_SCAN(
1451 reginclass(prog, c, (U8*)s, &inclasslen, utf8_target));
1454 REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s));
1459 if (tmp && (!reginfo || regtry(reginfo, &s)))
1467 if (UTF_PATTERN || utf8_target) {
1468 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1469 goto do_exactf_utf8;
1471 fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */
1472 folder = foldEQ_latin1; /* /a, except the sharp s one which */
1473 goto do_exactf_non_utf8; /* isn't dealt with by these */
1478 /* regcomp.c already folded this if pattern is in UTF-8 */
1479 utf8_fold_flags = 0;
1480 goto do_exactf_utf8;
1482 fold_array = PL_fold;
1484 goto do_exactf_non_utf8;
1487 if (UTF_PATTERN || utf8_target) {
1488 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
1489 goto do_exactf_utf8;
1491 fold_array = PL_fold_locale;
1492 folder = foldEQ_locale;
1493 goto do_exactf_non_utf8;
1497 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1499 goto do_exactf_utf8;
1501 case EXACTFU_TRICKYFOLD:
1503 if (UTF_PATTERN || utf8_target) {
1504 utf8_fold_flags = (UTF_PATTERN) ? FOLDEQ_S2_ALREADY_FOLDED : 0;
1505 goto do_exactf_utf8;
1508 /* Any 'ss' in the pattern should have been replaced by regcomp,
1509 * so we don't have to worry here about this single special case
1510 * in the Latin1 range */
1511 fold_array = PL_fold_latin1;
1512 folder = foldEQ_latin1;
1516 do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
1517 are no glitches with fold-length differences
1518 between the target string and pattern */
1520 /* The idea in the non-utf8 EXACTF* cases is to first find the
1521 * first character of the EXACTF* node and then, if necessary,
1522 * case-insensitively compare the full text of the node. c1 is the
1523 * first character. c2 is its fold. This logic will not work for
1524 * Unicode semantics and the german sharp ss, which hence should
1525 * not be compiled into a node that gets here. */
1526 pat_string = STRING(c);
1527 ln = STR_LEN(c); /* length to match in octets/bytes */
1529 /* We know that we have to match at least 'ln' bytes (which is the
1530 * same as characters, since not utf8). If we have to match 3
1531 * characters, and there are only 2 availabe, we know without
1532 * trying that it will fail; so don't start a match past the
1533 * required minimum number from the far end */
1534 e = HOP3c(strend, -((I32)ln), s);
1536 if (!reginfo && e < s) {
1537 e = s; /* Due to minlen logic of intuit() */
1541 c2 = fold_array[c1];
1542 if (c1 == c2) { /* If char and fold are the same */
1543 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1546 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1555 /* If one of the operands is in utf8, we can't use the simpler
1556 * folding above, due to the fact that many different characters
1557 * can have the same fold, or portion of a fold, or different-
1559 pat_string = STRING(c);
1560 ln = STR_LEN(c); /* length to match in octets/bytes */
1561 pat_end = pat_string + ln;
1562 lnc = (UTF_PATTERN) /* length to match in characters */
1563 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
1566 /* We have 'lnc' characters to match in the pattern, but because of
1567 * multi-character folding, each character in the target can match
1568 * up to 3 characters (Unicode guarantees it will never exceed
1569 * this) if it is utf8-encoded; and up to 2 if not (based on the
1570 * fact that the Latin 1 folds are already determined, and the
1571 * only multi-char fold in that range is the sharp-s folding to
1572 * 'ss'. Thus, a pattern character can match as little as 1/3 of a
1573 * string character. Adjust lnc accordingly, rounding up, so that
1574 * if we need to match at least 4+1/3 chars, that really is 5. */
1575 expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
1576 lnc = (lnc + expansion - 1) / expansion;
1578 /* As in the non-UTF8 case, if we have to match 3 characters, and
1579 * only 2 are left, it's guaranteed to fail, so don't start a
1580 * match that would require us to go beyond the end of the string
1582 e = HOP3c(strend, -((I32)lnc), s);
1584 if (!reginfo && e < s) {
1585 e = s; /* Due to minlen logic of intuit() */
1588 /* XXX Note that we could recalculate e to stop the loop earlier,
1589 * as the worst case expansion above will rarely be met, and as we
1590 * go along we would usually find that e moves further to the left.
1591 * This would happen only after we reached the point in the loop
1592 * where if there were no expansion we should fail. Unclear if
1593 * worth the expense */
1596 char *my_strend= (char *)strend;
1597 if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
1598 pat_string, NULL, ln, cBOOL(UTF_PATTERN), utf8_fold_flags)
1599 && (!reginfo || regtry(reginfo, &s)) )
1603 s += (utf8_target) ? UTF8SKIP(s) : 1;
1608 PL_reg_flags |= RF_tainted;
1609 FBC_BOUND(isALNUM_LC,
1610 isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp)),
1611 isALNUM_LC_utf8((U8*)s));
1614 PL_reg_flags |= RF_tainted;
1615 FBC_NBOUND(isALNUM_LC,
1616 isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp)),
1617 isALNUM_LC_utf8((U8*)s));
1620 FBC_BOUND(isWORDCHAR,
1622 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1625 FBC_BOUND_NOLOAD(isWORDCHAR_A,
1627 isWORDCHAR_A((U8*)s));
1630 FBC_NBOUND(isWORDCHAR,
1632 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1635 FBC_NBOUND_NOLOAD(isWORDCHAR_A,
1637 isWORDCHAR_A((U8*)s));
1640 FBC_BOUND(isWORDCHAR_L1,
1642 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1645 FBC_NBOUND(isWORDCHAR_L1,
1647 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1650 REXEC_FBC_CSCAN_TAINT(
1651 isALNUM_LC_utf8((U8*)s),
1656 REXEC_FBC_CSCAN_PRELOAD(
1657 LOAD_UTF8_CHARCLASS_ALNUM(),
1658 swash_fetch(PL_utf8_alnum,(U8*)s, utf8_target),
1659 isWORDCHAR_L1((U8) *s)
1663 REXEC_FBC_CSCAN_PRELOAD(
1664 LOAD_UTF8_CHARCLASS_ALNUM(),
1665 swash_fetch(PL_utf8_alnum,(U8*)s, utf8_target),
1670 /* Don't need to worry about utf8, as it can match only a single
1671 * byte invariant character */
1672 REXEC_FBC_CLASS_SCAN( isWORDCHAR_A(*s));
1675 REXEC_FBC_CSCAN_PRELOAD(
1676 LOAD_UTF8_CHARCLASS_ALNUM(),
1677 !swash_fetch(PL_utf8_alnum,(U8*)s, utf8_target),
1678 ! isWORDCHAR_L1((U8) *s)
1682 REXEC_FBC_CSCAN_PRELOAD(
1683 LOAD_UTF8_CHARCLASS_ALNUM(),
1684 !swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target),
1695 REXEC_FBC_CSCAN_TAINT(
1696 !isALNUM_LC_utf8((U8*)s),
1701 REXEC_FBC_CSCAN_PRELOAD(
1702 LOAD_UTF8_CHARCLASS_SPACE(),
1703 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target),
1708 REXEC_FBC_CSCAN_PRELOAD(
1709 LOAD_UTF8_CHARCLASS_SPACE(),
1710 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target),
1715 /* Don't need to worry about utf8, as it can match only a single
1716 * byte invariant character */
1717 REXEC_FBC_CLASS_SCAN( isSPACE_A(*s));
1720 REXEC_FBC_CSCAN_TAINT(
1721 isSPACE_LC_utf8((U8*)s),
1726 REXEC_FBC_CSCAN_PRELOAD(
1727 LOAD_UTF8_CHARCLASS_SPACE(),
1728 !( *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target)),
1729 ! isSPACE_L1((U8) *s)
1733 REXEC_FBC_CSCAN_PRELOAD(
1734 LOAD_UTF8_CHARCLASS_SPACE(),
1735 !(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target)),
1746 REXEC_FBC_CSCAN_TAINT(
1747 !isSPACE_LC_utf8((U8*)s),
1752 REXEC_FBC_CSCAN_PRELOAD(
1753 LOAD_UTF8_CHARCLASS_DIGIT(),
1754 swash_fetch(PL_utf8_digit,(U8*)s, utf8_target),
1759 /* Don't need to worry about utf8, as it can match only a single
1760 * byte invariant character */
1761 REXEC_FBC_CLASS_SCAN( isDIGIT_A(*s));
1764 REXEC_FBC_CSCAN_TAINT(
1765 isDIGIT_LC_utf8((U8*)s),
1770 REXEC_FBC_CSCAN_PRELOAD(
1771 LOAD_UTF8_CHARCLASS_DIGIT(),
1772 !swash_fetch(PL_utf8_digit,(U8*)s, utf8_target),
1783 REXEC_FBC_CSCAN_TAINT(
1784 !isDIGIT_LC_utf8((U8*)s),
1791 is_LNBREAK_latin1(s)
1803 !is_VERTWS_latin1(s)
1809 is_HORIZWS_latin1(s)
1814 !is_HORIZWS_utf8(s),
1815 !is_HORIZWS_latin1(s)
1822 /* what trie are we using right now */
1824 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1826 = (reg_trie_data*)progi->data->data[ aho->trie ];
1827 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1829 const char *last_start = strend - trie->minlen;
1831 const char *real_start = s;
1833 STRLEN maxlen = trie->maxlen;
1835 U8 **points; /* map of where we were in the input string
1836 when reading a given char. For ASCII this
1837 is unnecessary overhead as the relationship
1838 is always 1:1, but for Unicode, especially
1839 case folded Unicode this is not true. */
1840 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1844 GET_RE_DEBUG_FLAGS_DECL;
1846 /* We can't just allocate points here. We need to wrap it in
1847 * an SV so it gets freed properly if there is a croak while
1848 * running the match */
1851 sv_points=newSV(maxlen * sizeof(U8 *));
1852 SvCUR_set(sv_points,
1853 maxlen * sizeof(U8 *));
1854 SvPOK_on(sv_points);
1855 sv_2mortal(sv_points);
1856 points=(U8**)SvPV_nolen(sv_points );
1857 if ( trie_type != trie_utf8_fold
1858 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1861 bitmap=(U8*)trie->bitmap;
1863 bitmap=(U8*)ANYOF_BITMAP(c);
1865 /* this is the Aho-Corasick algorithm modified a touch
1866 to include special handling for long "unknown char"
1867 sequences. The basic idea being that we use AC as long
1868 as we are dealing with a possible matching char, when
1869 we encounter an unknown char (and we have not encountered
1870 an accepting state) we scan forward until we find a legal
1872 AC matching is basically that of trie matching, except
1873 that when we encounter a failing transition, we fall back
1874 to the current states "fail state", and try the current char
1875 again, a process we repeat until we reach the root state,
1876 state 1, or a legal transition. If we fail on the root state
1877 then we can either terminate if we have reached an accepting
1878 state previously, or restart the entire process from the beginning
1882 while (s <= last_start) {
1883 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1891 U8 *uscan = (U8*)NULL;
1892 U8 *leftmost = NULL;
1894 U32 accepted_word= 0;
1898 while ( state && uc <= (U8*)strend ) {
1900 U32 word = aho->states[ state ].wordnum;
1904 DEBUG_TRIE_EXECUTE_r(
1905 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1906 dump_exec_pos( (char *)uc, c, strend, real_start,
1907 (char *)uc, utf8_target );
1908 PerlIO_printf( Perl_debug_log,
1909 " Scanning for legal start char...\n");
1913 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1917 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1923 if (uc >(U8*)last_start) break;
1927 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
1928 if (!leftmost || lpos < leftmost) {
1929 DEBUG_r(accepted_word=word);
1935 points[pointpos++ % maxlen]= uc;
1936 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1937 uscan, len, uvc, charid, foldlen,
1939 DEBUG_TRIE_EXECUTE_r({
1940 dump_exec_pos( (char *)uc, c, strend, real_start,
1942 PerlIO_printf(Perl_debug_log,
1943 " Charid:%3u CP:%4"UVxf" ",
1949 word = aho->states[ state ].wordnum;
1951 base = aho->states[ state ].trans.base;
1953 DEBUG_TRIE_EXECUTE_r({
1955 dump_exec_pos( (char *)uc, c, strend, real_start,
1957 PerlIO_printf( Perl_debug_log,
1958 "%sState: %4"UVxf", word=%"UVxf,
1959 failed ? " Fail transition to " : "",
1960 (UV)state, (UV)word);
1966 ( ((offset = base + charid
1967 - 1 - trie->uniquecharcount)) >= 0)
1968 && ((U32)offset < trie->lasttrans)
1969 && trie->trans[offset].check == state
1970 && (tmp=trie->trans[offset].next))
1972 DEBUG_TRIE_EXECUTE_r(
1973 PerlIO_printf( Perl_debug_log," - legal\n"));
1978 DEBUG_TRIE_EXECUTE_r(
1979 PerlIO_printf( Perl_debug_log," - fail\n"));
1981 state = aho->fail[state];
1985 /* we must be accepting here */
1986 DEBUG_TRIE_EXECUTE_r(
1987 PerlIO_printf( Perl_debug_log," - accepting\n"));
1996 if (!state) state = 1;
1999 if ( aho->states[ state ].wordnum ) {
2000 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
2001 if (!leftmost || lpos < leftmost) {
2002 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
2007 s = (char*)leftmost;
2008 DEBUG_TRIE_EXECUTE_r({
2010 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
2011 (UV)accepted_word, (IV)(s - real_start)
2014 if (!reginfo || regtry(reginfo, &s)) {
2020 DEBUG_TRIE_EXECUTE_r({
2021 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
2024 DEBUG_TRIE_EXECUTE_r(
2025 PerlIO_printf( Perl_debug_log,"No match.\n"));
2034 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
2044 - regexec_flags - match a regexp against a string
2047 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
2048 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
2049 /* strend: pointer to null at end of string */
2050 /* strbeg: real beginning of string */
2051 /* minend: end of match must be >=minend after stringarg. */
2052 /* data: May be used for some additional optimizations.
2053 Currently its only used, with a U32 cast, for transmitting
2054 the ganch offset when doing a /g match. This will change */
2055 /* nosave: For optimizations. */
2058 struct regexp *const prog = (struct regexp *)SvANY(rx);
2059 /*register*/ char *s;
2060 register regnode *c;
2061 /*register*/ char *startpos = stringarg;
2062 I32 minlen; /* must match at least this many chars */
2063 I32 dontbother = 0; /* how many characters not to try at end */
2064 I32 end_shift = 0; /* Same for the end. */ /* CC */
2065 I32 scream_pos = -1; /* Internal iterator of scream. */
2066 char *scream_olds = NULL;
2067 const bool utf8_target = cBOOL(DO_UTF8(sv));
2069 RXi_GET_DECL(prog,progi);
2070 regmatch_info reginfo; /* create some info to pass to regtry etc */
2071 regexp_paren_pair *swap = NULL;
2072 GET_RE_DEBUG_FLAGS_DECL;
2074 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
2075 PERL_UNUSED_ARG(data);
2077 /* Be paranoid... */
2078 if (prog == NULL || startpos == NULL) {
2079 Perl_croak(aTHX_ "NULL regexp parameter");
2083 multiline = prog->extflags & RXf_PMf_MULTILINE;
2084 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
2086 RX_MATCH_UTF8_set(rx, utf8_target);
2088 debug_start_match(rx, utf8_target, startpos, strend,
2092 minlen = prog->minlen;
2094 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
2095 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2096 "String too short [regexec_flags]...\n"));
2101 /* Check validity of program. */
2102 if (UCHARAT(progi->program) != REG_MAGIC) {
2103 Perl_croak(aTHX_ "corrupted regexp program");
2107 PL_reg_state.re_state_eval_setup_done = FALSE;
2111 PL_reg_flags |= RF_utf8;
2113 /* Mark beginning of line for ^ and lookbehind. */
2114 reginfo.bol = startpos; /* XXX not used ??? */
2118 /* Mark end of line for $ (and such) */
2121 /* see how far we have to get to not match where we matched before */
2122 reginfo.till = startpos+minend;
2124 /* If there is a "must appear" string, look for it. */
2127 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
2129 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
2130 reginfo.ganch = startpos + prog->gofs;
2131 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2132 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
2133 } else if (sv && SvTYPE(sv) >= SVt_PVMG
2135 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
2136 && mg->mg_len >= 0) {
2137 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
2138 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2139 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
2141 if (prog->extflags & RXf_ANCH_GPOS) {
2142 if (s > reginfo.ganch)
2144 s = reginfo.ganch - prog->gofs;
2145 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2146 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
2152 reginfo.ganch = strbeg + PTR2UV(data);
2153 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2154 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
2156 } else { /* pos() not defined */
2157 reginfo.ganch = strbeg;
2158 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2159 "GPOS: reginfo.ganch = strbeg\n"));
2162 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
2163 /* We have to be careful. If the previous successful match
2164 was from this regex we don't want a subsequent partially
2165 successful match to clobber the old results.
2166 So when we detect this possibility we add a swap buffer
2167 to the re, and switch the buffer each match. If we fail
2168 we switch it back, otherwise we leave it swapped.
2171 /* do we need a save destructor here for eval dies? */
2172 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
2173 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
2174 "rex=0x%"UVxf" saving offs: orig=0x%"UVxf" new=0x%"UVxf"\n",
2180 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
2181 re_scream_pos_data d;
2183 d.scream_olds = &scream_olds;
2184 d.scream_pos = &scream_pos;
2185 s = re_intuit_start(rx, sv, s, strend, flags, &d);
2187 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
2188 goto phooey; /* not present */
2194 /* Simplest case: anchored match need be tried only once. */
2195 /* [unless only anchor is BOL and multiline is set] */
2196 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2197 if (s == startpos && regtry(®info, &startpos))
2199 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2200 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2205 dontbother = minlen - 1;
2206 end = HOP3c(strend, -dontbother, strbeg) - 1;
2207 /* for multiline we only have to try after newlines */
2208 if (prog->check_substr || prog->check_utf8) {
2209 /* because of the goto we can not easily reuse the macros for bifurcating the
2210 unicode/non-unicode match modes here like we do elsewhere - demerphq */
2213 goto after_try_utf8;
2215 if (regtry(®info, &s)) {
2222 if (prog->extflags & RXf_USE_INTUIT) {
2223 s = re_intuit_start(rx, sv, s + UTF8SKIP(s), strend, flags, NULL);
2232 } /* end search for check string in unicode */
2234 if (s == startpos) {
2235 goto after_try_latin;
2238 if (regtry(®info, &s)) {
2245 if (prog->extflags & RXf_USE_INTUIT) {
2246 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2255 } /* end search for check string in latin*/
2256 } /* end search for check string */
2257 else { /* search for newline */
2259 /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
2262 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
2263 while (s <= end) { /* note it could be possible to match at the end of the string */
2264 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2265 if (regtry(®info, &s))
2269 } /* end search for newline */
2270 } /* end anchored/multiline check string search */
2272 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2274 /* the warning about reginfo.ganch being used without initialization
2275 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2276 and we only enter this block when the same bit is set. */
2277 char *tmp_s = reginfo.ganch - prog->gofs;
2279 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2284 /* Messy cases: unanchored match. */
2285 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2286 /* we have /x+whatever/ */
2287 /* it must be a one character string (XXXX Except UTF_PATTERN?) */
2292 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2293 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2294 ch = SvPVX_const(utf8_target ? prog->anchored_utf8 : prog->anchored_substr)[0];
2299 DEBUG_EXECUTE_r( did_match = 1 );
2300 if (regtry(®info, &s)) goto got_it;
2302 while (s < strend && *s == ch)
2310 DEBUG_EXECUTE_r( did_match = 1 );
2311 if (regtry(®info, &s)) goto got_it;
2313 while (s < strend && *s == ch)
2318 DEBUG_EXECUTE_r(if (!did_match)
2319 PerlIO_printf(Perl_debug_log,
2320 "Did not find anchored character...\n")
2323 else if (prog->anchored_substr != NULL
2324 || prog->anchored_utf8 != NULL
2325 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2326 && prog->float_max_offset < strend - s)) {
2331 char *last1; /* Last position checked before */
2335 if (prog->anchored_substr || prog->anchored_utf8) {
2336 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2337 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2338 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
2339 back_max = back_min = prog->anchored_offset;
2341 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2342 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2343 must = utf8_target ? prog->float_utf8 : prog->float_substr;
2344 back_max = prog->float_max_offset;
2345 back_min = prog->float_min_offset;
2349 if (must == &PL_sv_undef)
2350 /* could not downgrade utf8 check substring, so must fail */
2356 last = HOP3c(strend, /* Cannot start after this */
2357 -(I32)(CHR_SVLEN(must)
2358 - (SvTAIL(must) != 0) + back_min), strbeg);
2361 last1 = HOPc(s, -1);
2363 last1 = s - 1; /* bogus */
2365 /* XXXX check_substr already used to find "s", can optimize if
2366 check_substr==must. */
2368 dontbother = end_shift;
2369 strend = HOPc(strend, -dontbother);
2370 while ( (s <= last) &&
2371 (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2372 (unsigned char*)strend, must,
2373 multiline ? FBMrf_MULTILINE : 0)) ) {
2374 DEBUG_EXECUTE_r( did_match = 1 );
2375 if (HOPc(s, -back_max) > last1) {
2376 last1 = HOPc(s, -back_min);
2377 s = HOPc(s, -back_max);
2380 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2382 last1 = HOPc(s, -back_min);
2386 while (s <= last1) {
2387 if (regtry(®info, &s))
2393 while (s <= last1) {
2394 if (regtry(®info, &s))
2400 DEBUG_EXECUTE_r(if (!did_match) {
2401 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
2402 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2403 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2404 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2405 ? "anchored" : "floating"),
2406 quoted, RE_SV_TAIL(must));
2410 else if ( (c = progi->regstclass) ) {
2412 const OPCODE op = OP(progi->regstclass);
2413 /* don't bother with what can't match */
2414 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2415 strend = HOPc(strend, -(minlen - 1));
2418 SV * const prop = sv_newmortal();
2419 regprop(prog, prop, c);
2421 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
2423 PerlIO_printf(Perl_debug_log,
2424 "Matching stclass %.*s against %s (%d bytes)\n",
2425 (int)SvCUR(prop), SvPVX_const(prop),
2426 quoted, (int)(strend - s));
2429 if (find_byclass(prog, c, s, strend, ®info))
2431 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2435 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2442 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2443 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2444 float_real = utf8_target ? prog->float_utf8 : prog->float_substr;
2446 little = SvPV_const(float_real, len);
2447 if (SvTAIL(float_real)) {
2448 /* This means that float_real contains an artificial \n on the end
2449 * due to the presence of something like this: /foo$/
2450 * where we can match both "foo" and "foo\n" at the end of the string.
2451 * So we have to compare the end of the string first against the float_real
2452 * without the \n and then against the full float_real with the string.
2453 * We have to watch out for cases where the string might be smaller
2454 * than the float_real or the float_real without the \n.
2456 char *checkpos= strend - len;
2458 PerlIO_printf(Perl_debug_log,
2459 "%sChecking for float_real.%s\n",
2460 PL_colors[4], PL_colors[5]));
2461 if (checkpos + 1 < strbeg) {
2462 /* can't match, even if we remove the trailing \n string is too short to match */
2464 PerlIO_printf(Perl_debug_log,
2465 "%sString shorter than required trailing substring, cannot match.%s\n",
2466 PL_colors[4], PL_colors[5]));
2468 } else if (memEQ(checkpos + 1, little, len - 1)) {
2469 /* can match, the end of the string matches without the "\n" */
2470 last = checkpos + 1;
2471 } else if (checkpos < strbeg) {
2472 /* cant match, string is too short when the "\n" is included */
2474 PerlIO_printf(Perl_debug_log,
2475 "%sString does not contain required trailing substring, cannot match.%s\n",
2476 PL_colors[4], PL_colors[5]));
2478 } else if (!multiline) {
2479 /* non multiline match, so compare with the "\n" at the end of the string */
2480 if (memEQ(checkpos, little, len)) {
2484 PerlIO_printf(Perl_debug_log,
2485 "%sString does not contain required trailing substring, cannot match.%s\n",
2486 PL_colors[4], PL_colors[5]));
2490 /* multiline match, so we have to search for a place where the full string is located */
2496 last = rninstr(s, strend, little, little + len);
2498 last = strend; /* matching "$" */
2501 /* at one point this block contained a comment which was probably
2502 * incorrect, which said that this was a "should not happen" case.
2503 * Even if it was true when it was written I am pretty sure it is
2504 * not anymore, so I have removed the comment and replaced it with
2507 PerlIO_printf(Perl_debug_log,
2508 "String does not contain required substring, cannot match.\n"
2512 dontbother = strend - last + prog->float_min_offset;
2514 if (minlen && (dontbother < minlen))
2515 dontbother = minlen - 1;
2516 strend -= dontbother; /* this one's always in bytes! */
2517 /* We don't know much -- general case. */
2520 if (regtry(®info, &s))
2529 if (regtry(®info, &s))
2531 } while (s++ < strend);
2541 PerlIO_printf(Perl_debug_log,
2542 "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n",
2548 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2550 if (PL_reg_state.re_state_eval_setup_done)
2551 restore_pos(aTHX_ prog);
2552 if (RXp_PAREN_NAMES(prog))
2553 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2555 /* make sure $`, $&, $', and $digit will work later */
2556 if ( !(flags & REXEC_NOT_FIRST) ) {
2557 RX_MATCH_COPY_FREE(rx);
2558 if (flags & REXEC_COPY_STR) {
2559 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2560 #ifdef PERL_OLD_COPY_ON_WRITE
2562 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2564 PerlIO_printf(Perl_debug_log,
2565 "Copy on write: regexp capture, type %d\n",
2568 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2569 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2570 assert (SvPOKp(prog->saved_copy));
2574 RX_MATCH_COPIED_on(rx);
2575 s = savepvn(strbeg, i);
2581 prog->subbeg = strbeg;
2582 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2589 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2590 PL_colors[4], PL_colors[5]));
2591 if (PL_reg_state.re_state_eval_setup_done)
2592 restore_pos(aTHX_ prog);
2594 /* we failed :-( roll it back */
2595 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
2596 "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n",
2601 Safefree(prog->offs);
2609 /* Set which rex is pointed to by PL_reg_state, handling ref counting.
2610 * Do inc before dec, in case old and new rex are the same */
2611 #define SET_reg_curpm(Re2) \
2612 if (PL_reg_state.re_state_eval_setup_done) { \
2613 (void)ReREFCNT_inc(Re2); \
2614 ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \
2615 PM_SETRE((PL_reg_curpm), (Re2)); \
2620 - regtry - try match at specific point
2622 STATIC I32 /* 0 failure, 1 success */
2623 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2627 REGEXP *const rx = reginfo->prog;
2628 regexp *const prog = (struct regexp *)SvANY(rx);
2629 RXi_GET_DECL(prog,progi);
2630 GET_RE_DEBUG_FLAGS_DECL;
2632 PERL_ARGS_ASSERT_REGTRY;
2634 reginfo->cutpoint=NULL;
2636 if ((prog->extflags & RXf_EVAL_SEEN)
2637 && !PL_reg_state.re_state_eval_setup_done)
2641 PL_reg_state.re_state_eval_setup_done = TRUE;
2643 /* Make $_ available to executed code. */
2644 if (reginfo->sv != DEFSV) {
2646 DEFSV_set(reginfo->sv);
2649 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2650 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2651 /* prepare for quick setting of pos */
2652 #ifdef PERL_OLD_COPY_ON_WRITE
2653 if (SvIsCOW(reginfo->sv))
2654 sv_force_normal_flags(reginfo->sv, 0);
2656 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2657 &PL_vtbl_mglob, NULL, 0);
2661 PL_reg_oldpos = mg->mg_len;
2662 SAVEDESTRUCTOR_X(restore_pos, prog);
2664 if (!PL_reg_curpm) {
2665 Newxz(PL_reg_curpm, 1, PMOP);
2668 SV* const repointer = &PL_sv_undef;
2669 /* this regexp is also owned by the new PL_reg_curpm, which
2670 will try to free it. */
2671 av_push(PL_regex_padav, repointer);
2672 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2673 PL_regex_pad = AvARRAY(PL_regex_padav);
2678 PL_reg_oldcurpm = PL_curpm;
2679 PL_curpm = PL_reg_curpm;
2680 if (RXp_MATCH_COPIED(prog)) {
2681 /* Here is a serious problem: we cannot rewrite subbeg,
2682 since it may be needed if this match fails. Thus
2683 $` inside (?{}) could fail... */
2684 PL_reg_oldsaved = prog->subbeg;
2685 PL_reg_oldsavedlen = prog->sublen;
2686 #ifdef PERL_OLD_COPY_ON_WRITE
2687 PL_nrs = prog->saved_copy;
2689 RXp_MATCH_COPIED_off(prog);
2692 PL_reg_oldsaved = NULL;
2693 prog->subbeg = PL_bostr;
2694 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2697 PL_reg_starttry = *startpos;
2699 prog->offs[0].start = *startpos - PL_bostr;
2700 PL_reginput = *startpos;
2701 prog->lastparen = 0;
2702 prog->lastcloseparen = 0;
2705 /* XXXX What this code is doing here?!!! There should be no need
2706 to do this again and again, prog->lastparen should take care of
2709 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2710 * Actually, the code in regcppop() (which Ilya may be meaning by
2711 * prog->lastparen), is not needed at all by the test suite
2712 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2713 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2714 * Meanwhile, this code *is* needed for the
2715 * above-mentioned test suite tests to succeed. The common theme
2716 * on those tests seems to be returning null fields from matches.
2717 * --jhi updated by dapm */
2719 if (prog->nparens) {
2720 regexp_paren_pair *pp = prog->offs;
2722 for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
2730 if (regmatch(reginfo, progi->program + 1)) {
2731 prog->offs[0].end = PL_reginput - PL_bostr;
2734 if (reginfo->cutpoint)
2735 *startpos= reginfo->cutpoint;
2736 REGCP_UNWIND(lastcp);
2741 #define sayYES goto yes
2742 #define sayNO goto no
2743 #define sayNO_SILENT goto no_silent
2745 /* we dont use STMT_START/END here because it leads to
2746 "unreachable code" warnings, which are bogus, but distracting. */
2747 #define CACHEsayNO \
2748 if (ST.cache_mask) \
2749 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2752 /* this is used to determine how far from the left messages like
2753 'failed...' are printed. It should be set such that messages
2754 are inline with the regop output that created them.
2756 #define REPORT_CODE_OFF 32
2759 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2760 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2762 #define SLAB_FIRST(s) (&(s)->states[0])
2763 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2765 /* grab a new slab and return the first slot in it */
2767 STATIC regmatch_state *
2770 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2773 regmatch_slab *s = PL_regmatch_slab->next;
2775 Newx(s, 1, regmatch_slab);
2776 s->prev = PL_regmatch_slab;
2778 PL_regmatch_slab->next = s;
2780 PL_regmatch_slab = s;
2781 return SLAB_FIRST(s);
2785 /* push a new state then goto it */
2787 #define PUSH_STATE_GOTO(state, node) \
2789 st->resume_state = state; \
2792 /* push a new state with success backtracking, then goto it */
2794 #define PUSH_YES_STATE_GOTO(state, node) \
2796 st->resume_state = state; \
2797 goto push_yes_state;
2803 regmatch() - main matching routine
2805 This is basically one big switch statement in a loop. We execute an op,
2806 set 'next' to point the next op, and continue. If we come to a point which
2807 we may need to backtrack to on failure such as (A|B|C), we push a
2808 backtrack state onto the backtrack stack. On failure, we pop the top
2809 state, and re-enter the loop at the state indicated. If there are no more
2810 states to pop, we return failure.
2812 Sometimes we also need to backtrack on success; for example /A+/, where
2813 after successfully matching one A, we need to go back and try to
2814 match another one; similarly for lookahead assertions: if the assertion
2815 completes successfully, we backtrack to the state just before the assertion
2816 and then carry on. In these cases, the pushed state is marked as
2817 'backtrack on success too'. This marking is in fact done by a chain of
2818 pointers, each pointing to the previous 'yes' state. On success, we pop to
2819 the nearest yes state, discarding any intermediate failure-only states.
2820 Sometimes a yes state is pushed just to force some cleanup code to be
2821 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2822 it to free the inner regex.
2824 Note that failure backtracking rewinds the cursor position, while
2825 success backtracking leaves it alone.
2827 A pattern is complete when the END op is executed, while a subpattern
2828 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2829 ops trigger the "pop to last yes state if any, otherwise return true"
2832 A common convention in this function is to use A and B to refer to the two
2833 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2834 the subpattern to be matched possibly multiple times, while B is the entire
2835 rest of the pattern. Variable and state names reflect this convention.
2837 The states in the main switch are the union of ops and failure/success of
2838 substates associated with with that op. For example, IFMATCH is the op
2839 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2840 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2841 successfully matched A and IFMATCH_A_fail is a state saying that we have
2842 just failed to match A. Resume states always come in pairs. The backtrack
2843 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2844 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2845 on success or failure.
2847 The struct that holds a backtracking state is actually a big union, with
2848 one variant for each major type of op. The variable st points to the
2849 top-most backtrack struct. To make the code clearer, within each
2850 block of code we #define ST to alias the relevant union.
2852 Here's a concrete example of a (vastly oversimplified) IFMATCH
2858 #define ST st->u.ifmatch
2860 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2861 ST.foo = ...; // some state we wish to save
2863 // push a yes backtrack state with a resume value of
2864 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2866 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2869 case IFMATCH_A: // we have successfully executed A; now continue with B
2871 bar = ST.foo; // do something with the preserved value
2874 case IFMATCH_A_fail: // A failed, so the assertion failed
2875 ...; // do some housekeeping, then ...
2876 sayNO; // propagate the failure
2883 For any old-timers reading this who are familiar with the old recursive
2884 approach, the code above is equivalent to:
2886 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2895 ...; // do some housekeeping, then ...
2896 sayNO; // propagate the failure
2899 The topmost backtrack state, pointed to by st, is usually free. If you
2900 want to claim it, populate any ST.foo fields in it with values you wish to
2901 save, then do one of
2903 PUSH_STATE_GOTO(resume_state, node);
2904 PUSH_YES_STATE_GOTO(resume_state, node);
2906 which sets that backtrack state's resume value to 'resume_state', pushes a
2907 new free entry to the top of the backtrack stack, then goes to 'node'.
2908 On backtracking, the free slot is popped, and the saved state becomes the
2909 new free state. An ST.foo field in this new top state can be temporarily
2910 accessed to retrieve values, but once the main loop is re-entered, it
2911 becomes available for reuse.
2913 Note that the depth of the backtrack stack constantly increases during the
2914 left-to-right execution of the pattern, rather than going up and down with
2915 the pattern nesting. For example the stack is at its maximum at Z at the
2916 end of the pattern, rather than at X in the following:
2918 /(((X)+)+)+....(Y)+....Z/
2920 The only exceptions to this are lookahead/behind assertions and the cut,
2921 (?>A), which pop all the backtrack states associated with A before
2924 Backtrack state structs are allocated in slabs of about 4K in size.
2925 PL_regmatch_state and st always point to the currently active state,
2926 and PL_regmatch_slab points to the slab currently containing
2927 PL_regmatch_state. The first time regmatch() is called, the first slab is
2928 allocated, and is never freed until interpreter destruction. When the slab
2929 is full, a new one is allocated and chained to the end. At exit from
2930 regmatch(), slabs allocated since entry are freed.
2935 #define DEBUG_STATE_pp(pp) \
2937 DUMP_EXEC_POS(locinput, scan, utf8_target); \
2938 PerlIO_printf(Perl_debug_log, \
2939 " %*s"pp" %s%s%s%s%s\n", \
2941 PL_reg_name[st->resume_state], \
2942 ((st==yes_state||st==mark_state) ? "[" : ""), \
2943 ((st==yes_state) ? "Y" : ""), \
2944 ((st==mark_state) ? "M" : ""), \
2945 ((st==yes_state||st==mark_state) ? "]" : "") \
2950 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2955 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
2956 const char *start, const char *end, const char *blurb)
2958 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2960 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2965 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2966 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2968 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
2969 start, end - start, 60);
2971 PerlIO_printf(Perl_debug_log,
2972 "%s%s REx%s %s against %s\n",
2973 PL_colors[4], blurb, PL_colors[5], s0, s1);
2975 if (utf8_target||utf8_pat)
2976 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2977 utf8_pat ? "pattern" : "",
2978 utf8_pat && utf8_target ? " and " : "",
2979 utf8_target ? "string" : ""
2985 S_dump_exec_pos(pTHX_ const char *locinput,
2986 const regnode *scan,
2987 const char *loc_regeol,
2988 const char *loc_bostr,
2989 const char *loc_reg_starttry,
2990 const bool utf8_target)
2992 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2993 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2994 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2995 /* The part of the string before starttry has one color
2996 (pref0_len chars), between starttry and current
2997 position another one (pref_len - pref0_len chars),
2998 after the current position the third one.
2999 We assume that pref0_len <= pref_len, otherwise we
3000 decrease pref0_len. */
3001 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
3002 ? (5 + taill) - l : locinput - loc_bostr;
3005 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
3007 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
3009 pref0_len = pref_len - (locinput - loc_reg_starttry);
3010 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
3011 l = ( loc_regeol - locinput > (5 + taill) - pref_len
3012 ? (5 + taill) - pref_len : loc_regeol - locinput);
3013 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
3017 if (pref0_len > pref_len)
3018 pref0_len = pref_len;
3020 const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
3022 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
3023 (locinput - pref_len),pref0_len, 60, 4, 5);
3025 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
3026 (locinput - pref_len + pref0_len),
3027 pref_len - pref0_len, 60, 2, 3);
3029 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
3030 locinput, loc_regeol - locinput, 10, 0, 1);
3032 const STRLEN tlen=len0+len1+len2;
3033 PerlIO_printf(Perl_debug_log,
3034 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
3035 (IV)(locinput - loc_bostr),
3038 (docolor ? "" : "> <"),
3040 (int)(tlen > 19 ? 0 : 19 - tlen),
3047 /* reg_check_named_buff_matched()
3048 * Checks to see if a named buffer has matched. The data array of
3049 * buffer numbers corresponding to the buffer is expected to reside
3050 * in the regexp->data->data array in the slot stored in the ARG() of
3051 * node involved. Note that this routine doesn't actually care about the
3052 * name, that information is not preserved from compilation to execution.
3053 * Returns the index of the leftmost defined buffer with the given name
3054 * or 0 if non of the buffers matched.
3057 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
3060 RXi_GET_DECL(rex,rexi);
3061 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
3062 I32 *nums=(I32*)SvPVX(sv_dat);
3064 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
3066 for ( n=0; n<SvIVX(sv_dat); n++ ) {
3067 if ((I32)rex->lastparen >= nums[n] &&
3068 rex->offs[nums[n]].end != -1)
3077 /* free all slabs above current one - called during LEAVE_SCOPE */
3080 S_clear_backtrack_stack(pTHX_ void *p)
3082 regmatch_slab *s = PL_regmatch_slab->next;
3087 PL_regmatch_slab->next = NULL;
3089 regmatch_slab * const osl = s;
3096 STATIC I32 /* 0 failure, 1 success */
3097 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
3099 #if PERL_VERSION < 9 && !defined(PERL_CORE)
3103 register const bool utf8_target = PL_reg_match_utf8;
3104 const U32 uniflags = UTF8_ALLOW_DEFAULT;
3105 REGEXP *rex_sv = reginfo->prog;
3106 regexp *rex = (struct regexp *)SvANY(rex_sv);
3107 RXi_GET_DECL(rex,rexi);
3109 /* the current state. This is a cached copy of PL_regmatch_state */
3110 register regmatch_state *st;
3111 /* cache heavy used fields of st in registers */
3112 register regnode *scan;
3113 register regnode *next;
3114 register U32 n = 0; /* general value; init to avoid compiler warning */
3115 register I32 ln = 0; /* len or last; init to avoid compiler warning */
3116 register char *locinput = PL_reginput;
3117 register I32 nextchr; /* is always set to UCHARAT(locinput) */
3119 bool result = 0; /* return value of S_regmatch */
3120 int depth = 0; /* depth of backtrack stack */
3121 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
3122 const U32 max_nochange_depth =
3123 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
3124 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
3125 regmatch_state *yes_state = NULL; /* state to pop to on success of
3127 /* mark_state piggy backs on the yes_state logic so that when we unwind
3128 the stack on success we can update the mark_state as we go */
3129 regmatch_state *mark_state = NULL; /* last mark state we have seen */
3130 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
3131 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
3133 bool no_final = 0; /* prevent failure from backtracking? */
3134 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
3135 char *startpoint = PL_reginput;
3136 SV *popmark = NULL; /* are we looking for a mark? */
3137 SV *sv_commit = NULL; /* last mark name seen in failure */
3138 SV *sv_yes_mark = NULL; /* last mark name we have seen
3139 during a successful match */
3140 U32 lastopen = 0; /* last open we saw */
3141 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
3142 SV* const oreplsv = GvSV(PL_replgv);
3143 /* these three flags are set by various ops to signal information to
3144 * the very next op. They have a useful lifetime of exactly one loop
3145 * iteration, and are not preserved or restored by state pushes/pops
3147 bool sw = 0; /* the condition value in (?(cond)a|b) */
3148 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
3149 int logical = 0; /* the following EVAL is:
3153 or the following IFMATCH/UNLESSM is:
3154 false: plain (?=foo)
3155 true: used as a condition: (?(?=foo))
3157 PAD* last_pad = NULL;
3159 I32 gimme = G_SCALAR;
3160 CV *caller_cv = NULL; /* who called us */
3161 CV *last_pushed_cv = NULL; /* most recently called (?{}) CV */
3162 CHECKPOINT runops_cp; /* savestack position before executing EVAL */
3165 GET_RE_DEBUG_FLAGS_DECL;
3168 /* shut up 'may be used uninitialized' compiler warnings for dMULTICALL */
3169 multicall_oldcatch = 0;
3170 multicall_cv = NULL;
3172 PERL_UNUSED_VAR(multicall_cop);
3173 PERL_UNUSED_VAR(newsp);
3176 PERL_ARGS_ASSERT_REGMATCH;
3178 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
3179 PerlIO_printf(Perl_debug_log,"regmatch start\n");
3181 /* on first ever call to regmatch, allocate first slab */
3182 if (!PL_regmatch_slab) {
3183 Newx(PL_regmatch_slab, 1, regmatch_slab);
3184 PL_regmatch_slab->prev = NULL;
3185 PL_regmatch_slab->next = NULL;
3186 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
3189 oldsave = PL_savestack_ix;
3190 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
3191 SAVEVPTR(PL_regmatch_slab);
3192 SAVEVPTR(PL_regmatch_state);
3194 /* grab next free state slot */
3195 st = ++PL_regmatch_state;
3196 if (st > SLAB_LAST(PL_regmatch_slab))
3197 st = PL_regmatch_state = S_push_slab(aTHX);
3199 /* Note that nextchr is a byte even in UTF */
3200 nextchr = UCHARAT(locinput);
3202 while (scan != NULL) {
3205 SV * const prop = sv_newmortal();
3206 regnode *rnext=regnext(scan);
3207 DUMP_EXEC_POS( locinput, scan, utf8_target );
3208 regprop(rex, prop, scan);
3210 PerlIO_printf(Perl_debug_log,
3211 "%3"IVdf":%*s%s(%"IVdf")\n",
3212 (IV)(scan - rexi->program), depth*2, "",
3214 (PL_regkind[OP(scan)] == END || !rnext) ?
3215 0 : (IV)(rnext - rexi->program));
3218 next = scan + NEXT_OFF(scan);
3221 state_num = OP(scan);
3223 REH_CALL_EXEC_NODE_HOOK(rex, scan, reginfo, st);
3226 switch (state_num) {
3228 if (locinput == PL_bostr)
3230 /* reginfo->till = reginfo->bol; */
3235 if (locinput == PL_bostr ||
3236 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
3242 if (locinput == PL_bostr)
3246 if (locinput == reginfo->ganch)
3251 /* update the startpoint */
3252 st->u.keeper.val = rex->offs[0].start;
3253 PL_reginput = locinput;
3254 rex->offs[0].start = locinput - PL_bostr;
3255 PUSH_STATE_GOTO(KEEPS_next, next);
3257 case KEEPS_next_fail:
3258 /* rollback the start point change */
3259 rex->offs[0].start = st->u.keeper.val;
3265 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3270 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3272 if (PL_regeol - locinput > 1)
3276 if (PL_regeol != locinput)
3280 if (!nextchr && locinput >= PL_regeol)
3283 locinput += PL_utf8skip[nextchr];
3284 if (locinput > PL_regeol)
3286 nextchr = UCHARAT(locinput);
3289 nextchr = UCHARAT(++locinput);
3292 if (!nextchr && locinput >= PL_regeol)
3294 nextchr = UCHARAT(++locinput);
3297 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3300 locinput += PL_utf8skip[nextchr];
3301 if (locinput > PL_regeol)
3303 nextchr = UCHARAT(locinput);
3306 nextchr = UCHARAT(++locinput);
3310 #define ST st->u.trie
3312 /* In this case the charclass data is available inline so
3313 we can fail fast without a lot of extra overhead.
3315 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3317 PerlIO_printf(Perl_debug_log,
3318 "%*s %sfailed to match trie start class...%s\n",
3319 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3322 assert(0); /* NOTREACHED */
3326 /* the basic plan of execution of the trie is:
3327 * At the beginning, run though all the states, and
3328 * find the longest-matching word. Also remember the position
3329 * of the shortest matching word. For example, this pattern:
3332 * when matched against the string "abcde", will generate
3333 * accept states for all words except 3, with the longest
3334 * matching word being 4, and the shortest being 1 (with
3335 * the position being after char 1 of the string).
3337 * Then for each matching word, in word order (i.e. 1,2,4,5),
3338 * we run the remainder of the pattern; on each try setting
3339 * the current position to the character following the word,
3340 * returning to try the next word on failure.
3342 * We avoid having to build a list of words at runtime by
3343 * using a compile-time structure, wordinfo[].prev, which
3344 * gives, for each word, the previous accepting word (if any).
3345 * In the case above it would contain the mappings 1->2, 2->0,
3346 * 3->0, 4->5, 5->1. We can use this table to generate, from
3347 * the longest word (4 above), a list of all words, by
3348 * following the list of prev pointers; this gives us the
3349 * unordered list 4,5,1,2. Then given the current word we have
3350 * just tried, we can go through the list and find the
3351 * next-biggest word to try (so if we just failed on word 2,
3352 * the next in the list is 4).
3354 * Since at runtime we don't record the matching position in
3355 * the string for each word, we have to work that out for
3356 * each word we're about to process. The wordinfo table holds
3357 * the character length of each word; given that we recorded
3358 * at the start: the position of the shortest word and its
3359 * length in chars, we just need to move the pointer the
3360 * difference between the two char lengths. Depending on
3361 * Unicode status and folding, that's cheap or expensive.
3363 * This algorithm is optimised for the case where are only a
3364 * small number of accept states, i.e. 0,1, or maybe 2.
3365 * With lots of accepts states, and having to try all of them,
3366 * it becomes quadratic on number of accept states to find all
3371 /* what type of TRIE am I? (utf8 makes this contextual) */
3372 DECL_TRIE_TYPE(scan);
3374 /* what trie are we using right now */
3375 reg_trie_data * const trie
3376 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3377 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3378 U32 state = trie->startstate;
3380 if (trie->bitmap && !TRIE_BITMAP_TEST(trie,*locinput) ) {
3381 if (trie->states[ state ].wordnum) {
3383 PerlIO_printf(Perl_debug_log,
3384 "%*s %smatched empty string...%s\n",
3385 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3391 PerlIO_printf(Perl_debug_log,
3392 "%*s %sfailed to match trie start class...%s\n",
3393 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3400 U8 *uc = ( U8* )locinput;
3404 U8 *uscan = (U8*)NULL;
3405 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3406 U32 charcount = 0; /* how many input chars we have matched */
3407 U32 accepted = 0; /* have we seen any accepting states? */
3409 ST.jump = trie->jump;
3412 ST.longfold = FALSE; /* char longer if folded => it's harder */
3415 /* fully traverse the TRIE; note the position of the
3416 shortest accept state and the wordnum of the longest
3419 while ( state && uc <= (U8*)PL_regeol ) {
3420 U32 base = trie->states[ state ].trans.base;
3424 wordnum = trie->states[ state ].wordnum;
3426 if (wordnum) { /* it's an accept state */
3429 /* record first match position */
3431 ST.firstpos = (U8*)locinput;
3436 ST.firstchars = charcount;
3439 if (!ST.nextword || wordnum < ST.nextword)
3440 ST.nextword = wordnum;
3441 ST.topword = wordnum;
3444 DEBUG_TRIE_EXECUTE_r({
3445 DUMP_EXEC_POS( (char *)uc, scan, utf8_target );
3446 PerlIO_printf( Perl_debug_log,
3447 "%*s %sState: %4"UVxf" Accepted: %c ",
3448 2+depth * 2, "", PL_colors[4],
3449 (UV)state, (accepted ? 'Y' : 'N'));
3452 /* read a char and goto next state */
3455 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3456 uscan, len, uvc, charid, foldlen,
3463 base + charid - 1 - trie->uniquecharcount)) >= 0)
3465 && ((U32)offset < trie->lasttrans)
3466 && trie->trans[offset].check == state)
3468 state = trie->trans[offset].next;
3479 DEBUG_TRIE_EXECUTE_r(
3480 PerlIO_printf( Perl_debug_log,
3481 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3482 charid, uvc, (UV)state, PL_colors[5] );
3488 /* calculate total number of accept states */
3493 w = trie->wordinfo[w].prev;
3496 ST.accepted = accepted;
3500 PerlIO_printf( Perl_debug_log,
3501 "%*s %sgot %"IVdf" possible matches%s\n",
3502 REPORT_CODE_OFF + depth * 2, "",
3503 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3505 goto trie_first_try; /* jump into the fail handler */
3507 assert(0); /* NOTREACHED */
3509 case TRIE_next_fail: /* we failed - try next alternative */
3511 REGCP_UNWIND(ST.cp);
3512 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
3514 if (!--ST.accepted) {
3516 PerlIO_printf( Perl_debug_log,
3517 "%*s %sTRIE failed...%s\n",
3518 REPORT_CODE_OFF+depth*2, "",
3525 /* Find next-highest word to process. Note that this code
3526 * is O(N^2) per trie run (O(N) per branch), so keep tight */
3527 register U16 min = 0;
3529 register U16 const nextword = ST.nextword;
3530 register reg_trie_wordinfo * const wordinfo
3531 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3532 for (word=ST.topword; word; word=wordinfo[word].prev) {
3533 if (word > nextword && (!min || word < min))
3546 ST.lastparen = rex->lastparen;
3547 ST.lastcloseparen = rex->lastcloseparen;
3551 /* find start char of end of current word */
3553 U32 chars; /* how many chars to skip */
3554 U8 *uc = ST.firstpos;
3555 reg_trie_data * const trie
3556 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3558 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3560 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3564 /* the hard option - fold each char in turn and find
3565 * its folded length (which may be different */
3566 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3574 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3582 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3587 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3601 PL_reginput = (char *)uc;
3604 scan = ST.me + ((ST.jump && ST.jump[ST.nextword])
3605 ? ST.jump[ST.nextword]
3609 PerlIO_printf( Perl_debug_log,
3610 "%*s %sTRIE matched word #%d, continuing%s\n",
3611 REPORT_CODE_OFF+depth*2, "",
3618 if (ST.accepted > 1 || has_cutgroup) {
3619 PUSH_STATE_GOTO(TRIE_next, scan);
3620 assert(0); /* NOTREACHED */
3622 /* only one choice left - just continue */
3624 AV *const trie_words
3625 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3626 SV ** const tmp = av_fetch( trie_words,
3628 SV *sv= tmp ? sv_newmortal() : NULL;
3630 PerlIO_printf( Perl_debug_log,
3631 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3632 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3634 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3635 PL_colors[0], PL_colors[1],
3636 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)|PERL_PV_ESCAPE_NONASCII
3638 : "not compiled under -Dr",
3642 locinput = PL_reginput;
3643 nextchr = UCHARAT(locinput);
3644 continue; /* execute rest of RE */
3645 assert(0); /* NOTREACHED */
3649 char *s = STRING(scan);
3651 if (utf8_target != UTF_PATTERN) {
3652 /* The target and the pattern have differing utf8ness. */
3654 const char * const e = s + ln;
3657 /* The target is utf8, the pattern is not utf8. */
3662 if (NATIVE_TO_UNI(*(U8*)s) !=
3663 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3671 /* The target is not utf8, the pattern is utf8. */
3676 if (NATIVE_TO_UNI(*((U8*)l)) !=
3677 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3685 nextchr = UCHARAT(locinput);
3688 /* The target and the pattern have the same utf8ness. */
3689 /* Inline the first character, for speed. */
3690 if (UCHARAT(s) != nextchr)
3692 if (PL_regeol - locinput < ln)
3694 if (ln > 1 && memNE(s, locinput, ln))
3697 nextchr = UCHARAT(locinput);
3702 const U8 * fold_array;
3704 U32 fold_utf8_flags;
3706 PL_reg_flags |= RF_tainted;
3707 folder = foldEQ_locale;
3708 fold_array = PL_fold_locale;
3709 fold_utf8_flags = FOLDEQ_UTF8_LOCALE;
3713 case EXACTFU_TRICKYFOLD:
3715 folder = foldEQ_latin1;
3716 fold_array = PL_fold_latin1;
3717 fold_utf8_flags = (UTF_PATTERN) ? FOLDEQ_S1_ALREADY_FOLDED : 0;
3721 folder = foldEQ_latin1;
3722 fold_array = PL_fold_latin1;
3723 fold_utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
3728 fold_array = PL_fold;
3729 fold_utf8_flags = 0;
3735 if (utf8_target || UTF_PATTERN || state_num == EXACTFU_SS) {
3736 /* Either target or the pattern are utf8, or has the issue where
3737 * the fold lengths may differ. */
3738 const char * const l = locinput;
3739 char *e = PL_regeol;
3741 if (! foldEQ_utf8_flags(s, 0, ln, cBOOL(UTF_PATTERN),
3742 l, &e, 0, utf8_target, fold_utf8_flags))
3747 nextchr = UCHARAT(locinput);
3751 /* Neither the target nor the pattern are utf8 */
3752 if (UCHARAT(s) != nextchr &&
3753 UCHARAT(s) != fold_array[nextchr])
3757 if (PL_regeol - locinput < ln)
3759 if (ln > 1 && ! folder(s, locinput, ln))
3762 nextchr = UCHARAT(locinput);
3766 /* XXX Could improve efficiency by separating these all out using a
3767 * macro or in-line function. At that point regcomp.c would no longer
3768 * have to set the FLAGS fields of these */
3771 PL_reg_flags |= RF_tainted;
3779 /* was last char in word? */
3781 && FLAGS(scan) != REGEX_ASCII_RESTRICTED_CHARSET
3782 && FLAGS(scan) != REGEX_ASCII_MORE_RESTRICTED_CHARSET)
3784 if (locinput == PL_bostr)
3787 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3789 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3791 if (FLAGS(scan) != REGEX_LOCALE_CHARSET) {
3792 ln = isALNUM_uni(ln);
3793 LOAD_UTF8_CHARCLASS_ALNUM();
3794 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, utf8_target);
3797 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3798 n = isALNUM_LC_utf8((U8*)locinput);
3803 /* Here the string isn't utf8, or is utf8 and only ascii
3804 * characters are to match \w. In the latter case looking at
3805 * the byte just prior to the current one may be just the final
3806 * byte of a multi-byte character. This is ok. There are two
3808 * 1) it is a single byte character, and then the test is doing
3809 * just what it's supposed to.
3810 * 2) it is a multi-byte character, in which case the final
3811 * byte is never mistakable for ASCII, and so the test
3812 * will say it is not a word character, which is the
3813 * correct answer. */
3814 ln = (locinput != PL_bostr) ?
3815 UCHARAT(locinput - 1) : '\n';
3816 switch (FLAGS(scan)) {
3817 case REGEX_UNICODE_CHARSET:
3818 ln = isWORDCHAR_L1(ln);
3819 n = isWORDCHAR_L1(nextchr);
3821 case REGEX_LOCALE_CHARSET:
3822 ln = isALNUM_LC(ln);
3823 n = isALNUM_LC(nextchr);
3825 case REGEX_DEPENDS_CHARSET:
3827 n = isALNUM(nextchr);
3829 case REGEX_ASCII_RESTRICTED_CHARSET:
3830 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
3831 ln = isWORDCHAR_A(ln);
3832 n = isWORDCHAR_A(nextchr);
3835 Perl_croak(aTHX_ "panic: Unexpected FLAGS %u in op %u", FLAGS(scan), OP(scan));
3839 /* Note requires that all BOUNDs be lower than all NBOUNDs in
3841 if (((!ln) == (!n)) == (OP(scan) < NBOUND))
3846 if (utf8_target || state_num == ANYOFV) {
3847 STRLEN inclasslen = PL_regeol - locinput;
3848 if (locinput >= PL_regeol)
3851 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, utf8_target))
3853 locinput += inclasslen;
3854 nextchr = UCHARAT(locinput);
3859 nextchr = UCHARAT(locinput);
3860 if (!nextchr && locinput >= PL_regeol)
3862 if (!REGINCLASS(rex, scan, (U8*)locinput))
3864 nextchr = UCHARAT(++locinput);
3868 /* Special char classes - The defines start on line 129 or so */
3869 CCC_TRY_U(ALNUM, NALNUM, isWORDCHAR,
3870 ALNUML, NALNUML, isALNUM_LC, isALNUM_LC_utf8,
3871 ALNUMU, NALNUMU, isWORDCHAR_L1,
3872 ALNUMA, NALNUMA, isWORDCHAR_A,
3875 CCC_TRY_U(SPACE, NSPACE, isSPACE,
3876 SPACEL, NSPACEL, isSPACE_LC, isSPACE_LC_utf8,
3877 SPACEU, NSPACEU, isSPACE_L1,
3878 SPACEA, NSPACEA, isSPACE_A,
3881 CCC_TRY(DIGIT, NDIGIT, isDIGIT,
3882 DIGITL, NDIGITL, isDIGIT_LC, isDIGIT_LC_utf8,
3883 DIGITA, NDIGITA, isDIGIT_A,
3886 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3887 a Unicode extended Grapheme Cluster */
3888 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3889 extended Grapheme Cluster is:
3892 | Prepend* Begin Extend*
3895 Begin is (Hangul-syllable | ! Control)
3896 Extend is (Grapheme_Extend | Spacing_Mark)
3897 Control is [ GCB_Control CR LF ]
3899 The discussion below shows how the code for CLUMP is derived
3900 from this regex. Note that most of these concepts are from
3901 property values of the Grapheme Cluster Boundary (GCB) property.
3902 No code point can have multiple property values for a given
3903 property. Thus a code point in Prepend can't be in Control, but
3904 it must be in !Control. This is why Control above includes
3905 GCB_Control plus CR plus LF. The latter two are used in the GCB
3906 property separately, and so can't be in GCB_Control, even though
3907 they logically are controls. Control is not the same as gc=cc,
3908 but includes format and other characters as well.
3910 The Unicode definition of Hangul-syllable is:
3912 | (L* ( ( V | LV ) V* | LVT ) T*)
3915 Each of these is a value for the GCB property, and hence must be
3916 disjoint, so the order they are tested is immaterial, so the
3917 above can safely be changed to
3920 | (L* ( LVT | ( V | LV ) V*) T*)
3922 The last two terms can be combined like this:
3924 | (( LVT | ( V | LV ) V*) T*))
3926 And refactored into this:
3927 L* (L | LVT T* | V V* T* | LV V* T*)
3929 That means that if we have seen any L's at all we can quit
3930 there, but if the next character is an LVT, a V, or an LV we
3933 There is a subtlety with Prepend* which showed up in testing.
3934 Note that the Begin, and only the Begin is required in:
3935 | Prepend* Begin Extend*
3936 Also, Begin contains '! Control'. A Prepend must be a
3937 '! Control', which means it must also be a Begin. What it
3938 comes down to is that if we match Prepend* and then find no
3939 suitable Begin afterwards, that if we backtrack the last
3940 Prepend, that one will be a suitable Begin.
3943 if (locinput >= PL_regeol)
3945 if (! utf8_target) {
3947 /* Match either CR LF or '.', as all the other possibilities
3949 locinput++; /* Match the . or CR */
3950 if (nextchr == '\r' /* And if it was CR, and the next is LF,
3952 && locinput < PL_regeol
3953 && UCHARAT(locinput) == '\n') locinput++;
3957 /* Utf8: See if is ( CR LF ); already know that locinput <
3958 * PL_regeol, so locinput+1 is in bounds */
3959 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3963 /* In case have to backtrack to beginning, then match '.' */
3964 char *starting = locinput;
3966 /* In case have to backtrack the last prepend */
3967 char *previous_prepend = 0;
3969 LOAD_UTF8_CHARCLASS_GCB();
3971 /* Match (prepend)* */
3972 while (locinput < PL_regeol
3973 && swash_fetch(PL_utf8_X_prepend,
3974 (U8*)locinput, utf8_target))
3976 previous_prepend = locinput;
3977 locinput += UTF8SKIP(locinput);
3980 /* As noted above, if we matched a prepend character, but
3981 * the next thing won't match, back off the last prepend we
3982 * matched, as it is guaranteed to match the begin */
3983 if (previous_prepend
3984 && (locinput >= PL_regeol
3985 || ! swash_fetch(PL_utf8_X_begin,
3986 (U8*)locinput, utf8_target)))
3988 locinput = previous_prepend;
3991 /* Note that here we know PL_regeol > locinput, as we
3992 * tested that upon input to this switch case, and if we
3993 * moved locinput forward, we tested the result just above
3994 * and it either passed, or we backed off so that it will
3996 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, utf8_target)) {
3998 /* Here did not match the required 'Begin' in the
3999 * second term. So just match the very first
4000 * character, the '.' of the final term of the regex */
4001 locinput = starting + UTF8SKIP(starting);
4004 /* Here is the beginning of a character that can have
4005 * an extender. It is either a hangul syllable, or a
4007 if (swash_fetch(PL_utf8_X_non_hangul,
4008 (U8*)locinput, utf8_target))
4011 /* Here not a Hangul syllable, must be a
4012 * ('! * Control') */
4013 locinput += UTF8SKIP(locinput);
4016 /* Here is a Hangul syllable. It can be composed
4017 * of several individual characters. One
4018 * possibility is T+ */
4019 if (swash_fetch(PL_utf8_X_T,
4020 (U8*)locinput, utf8_target))
4022 while (locinput < PL_regeol
4023 && swash_fetch(PL_utf8_X_T,
4024 (U8*)locinput, utf8_target))
4026 locinput += UTF8SKIP(locinput);
4030 /* Here, not T+, but is a Hangul. That means
4031 * it is one of the others: L, LV, LVT or V,
4033 * L* (L | LVT T* | V V* T* | LV V* T*) */
4036 while (locinput < PL_regeol
4037 && swash_fetch(PL_utf8_X_L,
4038 (U8*)locinput, utf8_target))
4040 locinput += UTF8SKIP(locinput);
4043 /* Here, have exhausted L*. If the next
4044 * character is not an LV, LVT nor V, it means
4045 * we had to have at least one L, so matches L+
4046 * in the original equation, we have a complete
4047 * hangul syllable. Are done. */
4049 if (locinput < PL_regeol
4050 && swash_fetch(PL_utf8_X_LV_LVT_V,
4051 (U8*)locinput, utf8_target))
4054 /* Otherwise keep going. Must be LV, LVT
4055 * or V. See if LVT */
4056 if (swash_fetch(PL_utf8_X_LVT,
4057 (U8*)locinput, utf8_target))
4059 locinput += UTF8SKIP(locinput);
4062 /* Must be V or LV. Take it, then
4064 locinput += UTF8SKIP(locinput);
4065 while (locinput < PL_regeol
4066 && swash_fetch(PL_utf8_X_V,
4067 (U8*)locinput, utf8_target))
4069 locinput += UTF8SKIP(locinput);
4073 /* And any of LV, LVT, or V can be followed
4075 while (locinput < PL_regeol
4076 && swash_fetch(PL_utf8_X_T,
4080 locinput += UTF8SKIP(locinput);
4086 /* Match any extender */
4087 while (locinput < PL_regeol
4088 && swash_fetch(PL_utf8_X_extend,
4089 (U8*)locinput, utf8_target))
4091 locinput += UTF8SKIP(locinput);
4095 if (locinput > PL_regeol) sayNO;
4097 nextchr = UCHARAT(locinput);
4101 { /* The capture buffer cases. The ones beginning with N for the
4102 named buffers just convert to the equivalent numbered and
4103 pretend they were called as the corresponding numbered buffer
4105 /* don't initialize these in the declaration, it makes C++
4110 const U8 *fold_array;
4113 PL_reg_flags |= RF_tainted;
4114 folder = foldEQ_locale;
4115 fold_array = PL_fold_locale;
4117 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
4121 folder = foldEQ_latin1;
4122 fold_array = PL_fold_latin1;
4124 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
4128 folder = foldEQ_latin1;
4129 fold_array = PL_fold_latin1;
4131 utf8_fold_flags = 0;
4136 fold_array = PL_fold;
4138 utf8_fold_flags = 0;
4145 utf8_fold_flags = 0;
4148 /* For the named back references, find the corresponding buffer
4150 n = reg_check_named_buff_matched(rex,scan);
4155 goto do_nref_ref_common;
4158 PL_reg_flags |= RF_tainted;
4159 folder = foldEQ_locale;
4160 fold_array = PL_fold_locale;
4161 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
4165 folder = foldEQ_latin1;
4166 fold_array = PL_fold_latin1;
4167 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
4171 folder = foldEQ_latin1;
4172 fold_array = PL_fold_latin1;
4173 utf8_fold_flags = 0;
4178 fold_array = PL_fold;
4179 utf8_fold_flags = 0;
4185 utf8_fold_flags = 0;
4189 n = ARG(scan); /* which paren pair */
4192 ln = rex->offs[n].start;
4193 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4194 if (rex->lastparen < n || ln == -1)
4195 sayNO; /* Do not match unless seen CLOSEn. */
4196 if (ln == rex->offs[n].end)
4200 if (type != REF /* REF can do byte comparison */
4201 && (utf8_target || type == REFFU))
4202 { /* XXX handle REFFL better */
4203 char * limit = PL_regeol;
4205 /* This call case insensitively compares the entire buffer
4206 * at s, with the current input starting at locinput, but
4207 * not going off the end given by PL_regeol, and returns in
4208 * limit upon success, how much of the current input was
4210 if (! foldEQ_utf8_flags(s, NULL, rex->offs[n].end - ln, utf8_target,
4211 locinput, &limit, 0, utf8_target, utf8_fold_flags))
4216 nextchr = UCHARAT(locinput);
4220 /* Not utf8: Inline the first character, for speed. */
4221 if (UCHARAT(s) != nextchr &&
4223 UCHARAT(s) != fold_array[nextchr]))
4225 ln = rex->offs[n].end - ln;
4226 if (locinput + ln > PL_regeol)
4228 if (ln > 1 && (type == REF
4229 ? memNE(s, locinput, ln)
4230 : ! folder(s, locinput, ln)))
4233 nextchr = UCHARAT(locinput);
4243 #define ST st->u.eval
4248 regexp_internal *rei;
4249 regnode *startpoint;
4252 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
4253 if (cur_eval && cur_eval->locinput==locinput) {
4254 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
4255 Perl_croak(aTHX_ "Infinite recursion in regex");
4256 if ( ++nochange_depth > max_nochange_depth )
4258 "Pattern subroutine nesting without pos change"
4259 " exceeded limit in regex");
4266 if (OP(scan)==GOSUB) {
4267 startpoint = scan + ARG2L(scan);
4268 ST.close_paren = ARG(scan);
4270 startpoint = rei->program+1;
4273 goto eval_recurse_doit;
4274 assert(0); /* NOTREACHED */
4275 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
4276 if (cur_eval && cur_eval->locinput==locinput) {
4277 if ( ++nochange_depth > max_nochange_depth )
4278 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
4283 /* execute the code in the {...} */
4287 OP * const oop = PL_op;
4288 COP * const ocurcop = PL_curcop;
4290 char *saved_regeol = PL_regeol;
4291 struct re_save_state saved_state;
4294 /* save *all* paren positions */
4296 REGCP_SET(runops_cp);
4298 /* To not corrupt the existing regex state while executing the
4299 * eval we would normally put it on the save stack, like with
4300 * save_re_context. However, re-evals have a weird scoping so we
4301 * can't just add ENTER/LEAVE here. With that, things like
4303 * (?{$a=2})(a(?{local$a=$a+1}))*aak*c(?{$b=$a})
4305 * would break, as they expect the localisation to be unwound
4306 * only when the re-engine backtracks through the bit that
4309 * What we do instead is just saving the state in a local c
4312 Copy(&PL_reg_state, &saved_state, 1, struct re_save_state);
4314 PL_reg_state.re_reparsing = FALSE;
4317 caller_cv = find_runcv(NULL);
4321 if (rexi->data->what[n] == 'r') { /* code from an external qr */
4322 newcv = ((struct regexp *)SvANY(
4323 (REGEXP*)(rexi->data->data[n])
4326 nop = (OP*)rexi->data->data[n+1];
4328 else if (rexi->data->what[n] == 'l') { /* literal code */
4330 nop = (OP*)rexi->data->data[n];
4331 assert(CvDEPTH(newcv));
4334 /* literal with own CV */
4335 assert(rexi->data->what[n] == 'L');
4336 newcv = rex->qr_anoncv;
4337 nop = (OP*)rexi->data->data[n];
4340 /* normally if we're about to execute code from the same
4341 * CV that we used previously, we just use the existing
4342 * CX stack entry. However, its possible that in the
4343 * meantime we may have backtracked, popped from the save
4344 * stack, and undone the SAVECOMPPAD(s) associated with
4345 * PUSH_MULTICALL; in which case PL_comppad no longer
4346 * points to newcv's pad. */
4347 if (newcv != last_pushed_cv || PL_comppad != last_pad)
4349 I32 depth = (newcv == caller_cv) ? 0 : 1;
4350 if (last_pushed_cv) {
4351 CHANGE_MULTICALL_WITHDEPTH(newcv, depth);
4354 PUSH_MULTICALL_WITHDEPTH(newcv, depth);
4356 last_pushed_cv = newcv;
4358 last_pad = PL_comppad;
4360 /* the initial nextstate you would normally execute
4361 * at the start of an eval (which would cause error
4362 * messages to come from the eval), may be optimised
4363 * away from the execution path in the regex code blocks;
4364 * so manually set PL_curcop to it initially */
4366 OP *o = cUNOPx(nop)->op_first;
4367 assert(o->op_type == OP_NULL);
4368 if (o->op_targ == OP_SCOPE) {
4369 o = cUNOPo->op_first;
4372 assert(o->op_targ == OP_LEAVE);
4373 o = cUNOPo->op_first;
4374 assert(o->op_type == OP_ENTER);
4378 if (o->op_type != OP_STUB) {
4379 assert( o->op_type == OP_NEXTSTATE
4380 || o->op_type == OP_DBSTATE
4381 || (o->op_type == OP_NULL
4382 && ( o->op_targ == OP_NEXTSTATE
4383 || o->op_targ == OP_DBSTATE
4387 PL_curcop = (COP*)o;
4392 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
4393 " re EVAL PL_op=0x%"UVxf"\n", PTR2UV(nop)) );
4395 rex->offs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
4398 SV *sv_mrk = get_sv("REGMARK", 1);
4399 sv_setsv(sv_mrk, sv_yes_mark);
4402 /* we don't use MULTICALL here as we want to call the
4403 * first op of the block of interest, rather than the
4404 * first op of the sub */
4407 CALLRUNOPS(aTHX); /* Scalar context. */
4410 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
4416 /* before restoring everything, evaluate the returned
4417 * value, so that 'uninit' warnings don't use the wrong
4418 * PL_op or pad. Also need to process any magic vars
4419 * (e.g. $1) *before* parentheses are restored */
4424 if (logical == 0) /* (?{})/ */
4425 sv_setsv(save_scalar(PL_replgv), ret); /* $^R */
4426 else if (logical == 1) { /* /(?(?{...})X|Y)/ */
4427 sw = cBOOL(SvTRUE(ret));
4430 else { /* /(??{}) */
4431 /* if its overloaded, let the regex compiler handle
4432 * it; otherwise extract regex, or stringify */
4433 if (!SvAMAGIC(ret)) {
4437 if (SvTYPE(sv) == SVt_REGEXP)
4438 re_sv = (REGEXP*) sv;
4439 else if (SvSMAGICAL(sv)) {
4440 MAGIC *mg = mg_find(sv, PERL_MAGIC_qr);
4442 re_sv = (REGEXP *) mg->mg_obj;
4445 /* force any magic, undef warnings here */
4447 ret = sv_mortalcopy(ret);
4448 (void) SvPV_force_nolen(ret);
4454 Copy(&saved_state, &PL_reg_state, 1, struct re_save_state);
4456 /* *** Note that at this point we don't restore
4457 * PL_comppad, (or pop the CxSUB) on the assumption it may
4458 * be used again soon. This is safe as long as nothing
4459 * in the regexp code uses the pad ! */
4461 PL_curcop = ocurcop;
4462 PL_regeol = saved_regeol;
4463 S_regcp_restore(aTHX_ rex, runops_cp);
4469 /* only /(??{})/ from now on */
4472 /* extract RE object from returned value; compiling if
4476 re_sv = reg_temp_copy(NULL, re_sv);
4480 const I32 osize = PL_regsize;
4482 if (SvUTF8(ret) && IN_BYTES) {
4483 /* In use 'bytes': make a copy of the octet
4484 * sequence, but without the flag on */
4486 const char *const p = SvPV(ret, len);
4487 ret = newSVpvn_flags(p, len, SVs_TEMP);
4489 if (rex->intflags & PREGf_USE_RE_EVAL)
4490 pm_flags |= PMf_USE_RE_EVAL;
4492 /* if we got here, it should be an engine which
4493 * supports compiling code blocks and stuff */
4494 assert(rex->engine && rex->engine->op_comp);
4495 assert(!(scan->flags & ~RXf_PMf_COMPILETIME));
4496 re_sv = rex->engine->op_comp(aTHX_ &ret, 1, NULL,
4497 rex->engine, NULL, NULL,
4498 /* copy /msix etc to inner pattern */
4503 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
4505 /* This isn't a first class regexp. Instead, it's
4506 caching a regexp onto an existing, Perl visible
4508 sv_magic(ret, MUTABLE_SV(re_sv), PERL_MAGIC_qr, 0, 0);
4511 /* safe to do now that any $1 etc has been
4512 * interpolated into the new pattern string and
4514 S_regcp_restore(aTHX_ rex, runops_cp);
4516 re = (struct regexp *)SvANY(re_sv);
4518 RXp_MATCH_COPIED_off(re);
4519 re->subbeg = rex->subbeg;
4520 re->sublen = rex->sublen;
4523 debug_start_match(re_sv, utf8_target, locinput, PL_regeol,
4524 "Matching embedded");
4526 startpoint = rei->program + 1;
4527 ST.close_paren = 0; /* only used for GOSUB */
4529 eval_recurse_doit: /* Share code with GOSUB below this line */
4530 /* run the pattern returned from (??{...}) */
4531 ST.cp = regcppush(rex, 0); /* Save *all* the positions. */
4532 REGCP_SET(ST.lastcp);
4535 re->lastcloseparen = 0;
4537 PL_reginput = locinput;
4540 /* XXXX This is too dramatic a measure... */
4543 ST.toggle_reg_flags = PL_reg_flags;
4545 PL_reg_flags |= RF_utf8;
4547 PL_reg_flags &= ~RF_utf8;
4548 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4550 ST.prev_rex = rex_sv;
4551 ST.prev_curlyx = cur_curlyx;
4553 SET_reg_curpm(rex_sv);
4558 ST.prev_eval = cur_eval;
4560 /* now continue from first node in postoned RE */
4561 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4562 assert(0); /* NOTREACHED */
4565 case EVAL_AB: /* cleanup after a successful (??{A})B */
4566 /* note: this is called twice; first after popping B, then A */
4567 PL_reg_flags ^= ST.toggle_reg_flags;
4568 rex_sv = ST.prev_rex;
4569 SET_reg_curpm(rex_sv);
4570 rex = (struct regexp *)SvANY(rex_sv);
4571 rexi = RXi_GET(rex);
4573 cur_eval = ST.prev_eval;
4574 cur_curlyx = ST.prev_curlyx;
4576 /* XXXX This is too dramatic a measure... */
4578 if ( nochange_depth )
4583 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4584 /* note: this is called twice; first after popping B, then A */
4585 PL_reg_flags ^= ST.toggle_reg_flags;
4586 rex_sv = ST.prev_rex;
4587 SET_reg_curpm(rex_sv);
4588 rex = (struct regexp *)SvANY(rex_sv);
4589 rexi = RXi_GET(rex);
4591 PL_reginput = locinput;
4592 REGCP_UNWIND(ST.lastcp);
4594 cur_eval = ST.prev_eval;
4595 cur_curlyx = ST.prev_curlyx;
4596 /* XXXX This is too dramatic a measure... */
4598 if ( nochange_depth )
4604 n = ARG(scan); /* which paren pair */
4605 rex->offs[n].start_tmp = locinput - PL_bostr;
4608 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
4609 "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf" tmp; regsize=%"UVuf"\n",
4613 (IV)rex->offs[n].start_tmp,
4619 /* XXX really need to log other places start/end are set too */
4620 #define CLOSE_CAPTURE \
4621 rex->offs[n].start = rex->offs[n].start_tmp; \
4622 rex->offs[n].end = locinput - PL_bostr; \
4623 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, \
4624 "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf"..%"IVdf"\n", \
4626 PTR2UV(rex->offs), \
4628 (IV)rex->offs[n].start, \
4629 (IV)rex->offs[n].end \
4633 n = ARG(scan); /* which paren pair */
4635 /*if (n > PL_regsize)
4637 if (n > rex->lastparen)
4639 rex->lastcloseparen = n;
4640 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4648 cursor && OP(cursor)!=END;
4649 cursor=regnext(cursor))
4651 if ( OP(cursor)==CLOSE ){
4653 if ( n <= lastopen ) {
4655 /*if (n > PL_regsize)
4657 if (n > rex->lastparen)
4659 rex->lastcloseparen = n;
4660 if ( n == ARG(scan) || (cur_eval &&
4661 cur_eval->u.eval.close_paren == n))
4670 n = ARG(scan); /* which paren pair */
4671 sw = cBOOL(rex->lastparen >= n && rex->offs[n].end != -1);
4674 /* reg_check_named_buff_matched returns 0 for no match */
4675 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
4679 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4685 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4687 next = NEXTOPER(NEXTOPER(scan));
4689 next = scan + ARG(scan);
4690 if (OP(next) == IFTHEN) /* Fake one. */
4691 next = NEXTOPER(NEXTOPER(next));
4695 logical = scan->flags;
4698 /*******************************************************************
4700 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4701 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4702 STAR/PLUS/CURLY/CURLYN are used instead.)
4704 A*B is compiled as <CURLYX><A><WHILEM><B>
4706 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4707 state, which contains the current count, initialised to -1. It also sets
4708 cur_curlyx to point to this state, with any previous value saved in the
4711 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4712 since the pattern may possibly match zero times (i.e. it's a while {} loop
4713 rather than a do {} while loop).
4715 Each entry to WHILEM represents a successful match of A. The count in the
4716 CURLYX block is incremented, another WHILEM state is pushed, and execution
4717 passes to A or B depending on greediness and the current count.
4719 For example, if matching against the string a1a2a3b (where the aN are
4720 substrings that match /A/), then the match progresses as follows: (the
4721 pushed states are interspersed with the bits of strings matched so far):
4724 <CURLYX cnt=0><WHILEM>
4725 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4726 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4727 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4728 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4730 (Contrast this with something like CURLYM, which maintains only a single
4734 a1 <CURLYM cnt=1> a2
4735 a1 a2 <CURLYM cnt=2> a3
4736 a1 a2 a3 <CURLYM cnt=3> b
4739 Each WHILEM state block marks a point to backtrack to upon partial failure
4740 of A or B, and also contains some minor state data related to that
4741 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4742 overall state, such as the count, and pointers to the A and B ops.
4744 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4745 must always point to the *current* CURLYX block, the rules are:
4747 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4748 and set cur_curlyx to point the new block.
4750 When popping the CURLYX block after a successful or unsuccessful match,
4751 restore the previous cur_curlyx.
4753 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4754 to the outer one saved in the CURLYX block.
4756 When popping the WHILEM block after a successful or unsuccessful B match,
4757 restore the previous cur_curlyx.
4759 Here's an example for the pattern (AI* BI)*BO
4760 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4763 curlyx backtrack stack
4764 ------ ---------------
4766 CO <CO prev=NULL> <WO>
4767 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4768 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4769 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4771 At this point the pattern succeeds, and we work back down the stack to
4772 clean up, restoring as we go:
4774 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4775 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4776 CO <CO prev=NULL> <WO>
4779 *******************************************************************/
4781 #define ST st->u.curlyx
4783 case CURLYX: /* start of /A*B/ (for complex A) */
4785 /* No need to save/restore up to this paren */
4786 I32 parenfloor = scan->flags;
4788 assert(next); /* keep Coverity happy */
4789 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4792 /* XXXX Probably it is better to teach regpush to support
4793 parenfloor > PL_regsize... */
4794 if (parenfloor > (I32)rex->lastparen)
4795 parenfloor = rex->lastparen; /* Pessimization... */
4797 ST.prev_curlyx= cur_curlyx;
4799 ST.cp = PL_savestack_ix;
4801 /* these fields contain the state of the current curly.
4802 * they are accessed by subsequent WHILEMs */
4803 ST.parenfloor = parenfloor;
4808 ST.count = -1; /* this will be updated by WHILEM */
4809 ST.lastloc = NULL; /* this will be updated by WHILEM */
4811 PL_reginput = locinput;
4812 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4813 assert(0); /* NOTREACHED */
4816 case CURLYX_end: /* just finished matching all of A*B */
4817 cur_curlyx = ST.prev_curlyx;
4819 assert(0); /* NOTREACHED */
4821 case CURLYX_end_fail: /* just failed to match all of A*B */
4823 cur_curlyx = ST.prev_curlyx;
4825 assert(0); /* NOTREACHED */
4829 #define ST st->u.whilem
4831 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4833 /* see the discussion above about CURLYX/WHILEM */
4835 int min = ARG1(cur_curlyx->u.curlyx.me);
4836 int max = ARG2(cur_curlyx->u.curlyx.me);
4837 regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
4839 assert(cur_curlyx); /* keep Coverity happy */
4840 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4841 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4842 ST.cache_offset = 0;
4845 PL_reginput = locinput;
4847 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4848 "%*s whilem: matched %ld out of %d..%d\n",
4849 REPORT_CODE_OFF+depth*2, "", (long)n, min, max)
4852 /* First just match a string of min A's. */
4855 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor);
4856 cur_curlyx->u.curlyx.lastloc = locinput;
4857 REGCP_SET(ST.lastcp);
4859 PUSH_STATE_GOTO(WHILEM_A_pre, A);
4860 assert(0); /* NOTREACHED */
4863 /* If degenerate A matches "", assume A done. */
4865 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4866 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4867 "%*s whilem: empty match detected, trying continuation...\n",
4868 REPORT_CODE_OFF+depth*2, "")
4870 goto do_whilem_B_max;
4873 /* super-linear cache processing */
4877 if (!PL_reg_maxiter) {
4878 /* start the countdown: Postpone detection until we
4879 * know the match is not *that* much linear. */
4880 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4881 /* possible overflow for long strings and many CURLYX's */
4882 if (PL_reg_maxiter < 0)
4883 PL_reg_maxiter = I32_MAX;
4884 PL_reg_leftiter = PL_reg_maxiter;
4887 if (PL_reg_leftiter-- == 0) {
4888 /* initialise cache */
4889 const I32 size = (PL_reg_maxiter + 7)/8;
4890 if (PL_reg_poscache) {
4891 if ((I32)PL_reg_poscache_size < size) {
4892 Renew(PL_reg_poscache, size, char);
4893 PL_reg_poscache_size = size;
4895 Zero(PL_reg_poscache, size, char);
4898 PL_reg_poscache_size = size;
4899 Newxz(PL_reg_poscache, size, char);
4901 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4902 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4903 PL_colors[4], PL_colors[5])
4907 if (PL_reg_leftiter < 0) {
4908 /* have we already failed at this position? */
4910 offset = (scan->flags & 0xf) - 1
4911 + (locinput - PL_bostr) * (scan->flags>>4);
4912 mask = 1 << (offset % 8);
4914 if (PL_reg_poscache[offset] & mask) {
4915 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4916 "%*s whilem: (cache) already tried at this position...\n",
4917 REPORT_CODE_OFF+depth*2, "")
4919 sayNO; /* cache records failure */
4921 ST.cache_offset = offset;
4922 ST.cache_mask = mask;
4926 /* Prefer B over A for minimal matching. */
4928 if (cur_curlyx->u.curlyx.minmod) {
4929 ST.save_curlyx = cur_curlyx;
4930 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4931 ST.cp = regcppush(rex, ST.save_curlyx->u.curlyx.parenfloor);
4932 REGCP_SET(ST.lastcp);
4933 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4934 assert(0); /* NOTREACHED */
4937 /* Prefer A over B for maximal matching. */
4939 if (n < max) { /* More greed allowed? */
4940 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor);
4941 cur_curlyx->u.curlyx.lastloc = locinput;
4942 REGCP_SET(ST.lastcp);
4943 PUSH_STATE_GOTO(WHILEM_A_max, A);
4944 assert(0); /* NOTREACHED */
4946 goto do_whilem_B_max;
4948 assert(0); /* NOTREACHED */
4950 case WHILEM_B_min: /* just matched B in a minimal match */
4951 case WHILEM_B_max: /* just matched B in a maximal match */
4952 cur_curlyx = ST.save_curlyx;
4954 assert(0); /* NOTREACHED */
4956 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4957 cur_curlyx = ST.save_curlyx;
4958 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4959 cur_curlyx->u.curlyx.count--;
4961 assert(0); /* NOTREACHED */
4963 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4965 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4966 REGCP_UNWIND(ST.lastcp);
4968 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4969 cur_curlyx->u.curlyx.count--;
4971 assert(0); /* NOTREACHED */
4973 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4974 REGCP_UNWIND(ST.lastcp);
4975 regcppop(rex); /* Restore some previous $<digit>s? */
4976 PL_reginput = locinput;
4977 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4978 "%*s whilem: failed, trying continuation...\n",
4979 REPORT_CODE_OFF+depth*2, "")
4982 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4983 && ckWARN(WARN_REGEXP)
4984 && !(PL_reg_flags & RF_warned))
4986 PL_reg_flags |= RF_warned;
4987 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4988 "Complex regular subexpression recursion limit (%d) "
4994 ST.save_curlyx = cur_curlyx;
4995 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4996 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4997 assert(0); /* NOTREACHED */
4999 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
5000 cur_curlyx = ST.save_curlyx;
5001 REGCP_UNWIND(ST.lastcp);
5004 if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
5005 /* Maximum greed exceeded */
5006 if (cur_curlyx->u.curlyx.count >= REG_INFTY
5007 && ckWARN(WARN_REGEXP)
5008 && !(PL_reg_flags & RF_warned))
5010 PL_reg_flags |= RF_warned;
5011 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
5012 "Complex regular subexpression recursion "
5013 "limit (%d) exceeded",
5016 cur_curlyx->u.curlyx.count--;
5020 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5021 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
5023 /* Try grabbing another A and see if it helps. */
5024 PL_reginput = locinput;
5025 cur_curlyx->u.curlyx.lastloc = locinput;
5026 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor);
5027 REGCP_SET(ST.lastcp);
5028 PUSH_STATE_GOTO(WHILEM_A_min,
5029 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS);
5030 assert(0); /* NOTREACHED */
5033 #define ST st->u.branch
5035 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
5036 next = scan + ARG(scan);
5039 scan = NEXTOPER(scan);
5042 case BRANCH: /* /(...|A|...)/ */
5043 scan = NEXTOPER(scan); /* scan now points to inner node */
5044 ST.lastparen = rex->lastparen;
5045 ST.lastcloseparen = rex->lastcloseparen;
5046 ST.next_branch = next;
5048 PL_reginput = locinput;
5050 /* Now go into the branch */
5052 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
5054 PUSH_STATE_GOTO(BRANCH_next, scan);
5056 assert(0); /* NOTREACHED */
5058 PL_reginput = locinput;
5059 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
5060 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5061 PUSH_STATE_GOTO(CUTGROUP_next,next);
5062 assert(0); /* NOTREACHED */
5063 case CUTGROUP_next_fail:
5066 if (st->u.mark.mark_name)
5067 sv_commit = st->u.mark.mark_name;
5069 assert(0); /* NOTREACHED */
5072 assert(0); /* NOTREACHED */
5073 case BRANCH_next_fail: /* that branch failed; try the next, if any */
5078 REGCP_UNWIND(ST.cp);
5079 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
5080 scan = ST.next_branch;
5081 /* no more branches? */
5082 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
5084 PerlIO_printf( Perl_debug_log,
5085 "%*s %sBRANCH failed...%s\n",
5086 REPORT_CODE_OFF+depth*2, "",
5092 continue; /* execute next BRANCH[J] op */
5093 assert(0); /* NOTREACHED */
5100 #define ST st->u.curlym
5102 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
5104 /* This is an optimisation of CURLYX that enables us to push
5105 * only a single backtracking state, no matter how many matches
5106 * there are in {m,n}. It relies on the pattern being constant
5107 * length, with no parens to influence future backrefs
5111 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
5113 ST.lastparen = rex->lastparen;
5114 ST.lastcloseparen = rex->lastcloseparen;
5116 /* if paren positive, emulate an OPEN/CLOSE around A */
5118 U32 paren = ST.me->flags;
5119 if (paren > PL_regsize)
5121 scan += NEXT_OFF(scan); /* Skip former OPEN. */
5129 ST.c1 = CHRTEST_UNINIT;
5132 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
5135 curlym_do_A: /* execute the A in /A{m,n}B/ */
5136 PL_reginput = locinput;
5137 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
5138 assert(0); /* NOTREACHED */
5140 case CURLYM_A: /* we've just matched an A */
5141 locinput = st->locinput;
5142 nextchr = UCHARAT(locinput);
5145 /* after first match, determine A's length: u.curlym.alen */
5146 if (ST.count == 1) {
5147 if (PL_reg_match_utf8) {
5149 while (s < PL_reginput) {
5155 ST.alen = PL_reginput - locinput;
5158 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
5161 PerlIO_printf(Perl_debug_log,
5162 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
5163 (int)(REPORT_CODE_OFF+(depth*2)), "",
5164 (IV) ST.count, (IV)ST.alen)
5167 locinput = PL_reginput;
5169 if (cur_eval && cur_eval->u.eval.close_paren &&
5170 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
5174 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
5175 if ( max == REG_INFTY || ST.count < max )
5176 goto curlym_do_A; /* try to match another A */
5178 goto curlym_do_B; /* try to match B */
5180 case CURLYM_A_fail: /* just failed to match an A */
5181 REGCP_UNWIND(ST.cp);
5183 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
5184 || (cur_eval && cur_eval->u.eval.close_paren &&
5185 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
5188 curlym_do_B: /* execute the B in /A{m,n}B/ */
5189 PL_reginput = locinput;
5190 if (ST.c1 == CHRTEST_UNINIT) {
5191 /* calculate c1 and c2 for possible match of 1st char
5192 * following curly */
5193 ST.c1 = ST.c2 = CHRTEST_VOID;
5194 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
5195 regnode *text_node = ST.B;
5196 if (! HAS_TEXT(text_node))
5197 FIND_NEXT_IMPT(text_node);
5200 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
5202 But the former is redundant in light of the latter.
5204 if this changes back then the macro for
5205 IS_TEXT and friends need to change.
5207 if (PL_regkind[OP(text_node)] == EXACT)
5210 ST.c1 = (U8)*STRING(text_node);
5211 switch (OP(text_node)) {
5212 case EXACTF: ST.c2 = PL_fold[ST.c1]; break;
5215 case EXACTFU_TRICKYFOLD:
5216 case EXACTFU: ST.c2 = PL_fold_latin1[ST.c1]; break;
5217 case EXACTFL: ST.c2 = PL_fold_locale[ST.c1]; break;
5218 default: ST.c2 = ST.c1;
5225 PerlIO_printf(Perl_debug_log,
5226 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
5227 (int)(REPORT_CODE_OFF+(depth*2)),
5230 if (ST.c1 != CHRTEST_VOID
5231 && UCHARAT(PL_reginput) != ST.c1
5232 && UCHARAT(PL_reginput) != ST.c2)
5234 /* simulate B failing */
5236 PerlIO_printf(Perl_debug_log,
5237 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
5238 (int)(REPORT_CODE_OFF+(depth*2)),"",
5241 state_num = CURLYM_B_fail;
5242 goto reenter_switch;
5246 /* emulate CLOSE: mark current A as captured */
5247 I32 paren = ST.me->flags;
5249 rex->offs[paren].start
5250 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
5251 rex->offs[paren].end = PL_reginput - PL_bostr;
5252 if ((U32)paren > rex->lastparen)
5253 rex->lastparen = paren;
5254 rex->lastcloseparen = paren;
5257 rex->offs[paren].end = -1;
5258 if (cur_eval && cur_eval->u.eval.close_paren &&
5259 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
5268 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
5269 assert(0); /* NOTREACHED */
5271 case CURLYM_B_fail: /* just failed to match a B */
5272 REGCP_UNWIND(ST.cp);
5273 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
5275 I32 max = ARG2(ST.me);
5276 if (max != REG_INFTY && ST.count == max)
5278 goto curlym_do_A; /* try to match a further A */
5280 /* backtrack one A */
5281 if (ST.count == ARG1(ST.me) /* min */)
5284 locinput = HOPc(locinput, -ST.alen);
5285 goto curlym_do_B; /* try to match B */
5288 #define ST st->u.curly
5290 #define CURLY_SETPAREN(paren, success) \
5293 rex->offs[paren].start = HOPc(locinput, -1) - PL_bostr; \
5294 rex->offs[paren].end = locinput - PL_bostr; \
5295 if (paren > rex->lastparen) \
5296 rex->lastparen = paren; \
5297 rex->lastcloseparen = paren; \
5300 rex->offs[paren].end = -1; \
5301 rex->lastparen = ST.lastparen; \
5302 rex->lastcloseparen = ST.lastcloseparen; \
5306 case STAR: /* /A*B/ where A is width 1 */
5310 scan = NEXTOPER(scan);
5312 case PLUS: /* /A+B/ where A is width 1 */
5316 scan = NEXTOPER(scan);
5318 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
5319 ST.paren = scan->flags; /* Which paren to set */
5320 ST.lastparen = rex->lastparen;
5321 ST.lastcloseparen = rex->lastcloseparen;
5322 if (ST.paren > PL_regsize)
5323 PL_regsize = ST.paren;
5324 ST.min = ARG1(scan); /* min to match */
5325 ST.max = ARG2(scan); /* max to match */
5326 if (cur_eval && cur_eval->u.eval.close_paren &&
5327 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5331 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
5333 case CURLY: /* /A{m,n}B/ where A is width 1 */
5335 ST.min = ARG1(scan); /* min to match */
5336 ST.max = ARG2(scan); /* max to match */
5337 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
5340 * Lookahead to avoid useless match attempts
5341 * when we know what character comes next.
5343 * Used to only do .*x and .*?x, but now it allows
5344 * for )'s, ('s and (?{ ... })'s to be in the way
5345 * of the quantifier and the EXACT-like node. -- japhy
5348 if (ST.min > ST.max) /* XXX make this a compile-time check? */
5350 if (HAS_TEXT(next) || JUMPABLE(next)) {
5352 regnode *text_node = next;
5354 if (! HAS_TEXT(text_node))
5355 FIND_NEXT_IMPT(text_node);
5357 if (! HAS_TEXT(text_node))
5358 ST.c1 = ST.c2 = CHRTEST_VOID;
5360 if ( PL_regkind[OP(text_node)] != EXACT ) {
5361 ST.c1 = ST.c2 = CHRTEST_VOID;
5362 goto assume_ok_easy;
5365 s = (U8*)STRING(text_node);
5367 /* Currently we only get here when
5369 PL_rekind[OP(text_node)] == EXACT
5371 if this changes back then the macro for IS_TEXT and
5372 friends need to change. */
5375 switch (OP(text_node)) {
5376 case EXACTF: ST.c2 = PL_fold[ST.c1]; break;
5379 case EXACTFU_TRICKYFOLD:
5380 case EXACTFU: ST.c2 = PL_fold_latin1[ST.c1]; break;
5381 case EXACTFL: ST.c2 = PL_fold_locale[ST.c1]; break;
5382 default: ST.c2 = ST.c1; break;
5385 else { /* UTF_PATTERN */
5386 if (IS_TEXTFU(text_node) || IS_TEXTF(text_node)) {
5388 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
5390 to_utf8_fold((U8*)s, tmpbuf, &ulen);
5391 ST.c1 = ST.c2 = utf8n_to_uvchr(tmpbuf, UTF8_MAXLEN, 0,
5395 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
5402 ST.c1 = ST.c2 = CHRTEST_VOID;
5407 PL_reginput = locinput;
5410 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
5413 locinput = PL_reginput;
5415 if (ST.c1 == CHRTEST_VOID)
5416 goto curly_try_B_min;
5418 ST.oldloc = locinput;
5420 /* set ST.maxpos to the furthest point along the
5421 * string that could possibly match */
5422 if (ST.max == REG_INFTY) {
5423 ST.maxpos = PL_regeol - 1;
5425 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
5428 else if (utf8_target) {
5429 int m = ST.max - ST.min;
5430 for (ST.maxpos = locinput;
5431 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
5432 ST.maxpos += UTF8SKIP(ST.maxpos);
5435 ST.maxpos = locinput + ST.max - ST.min;
5436 if (ST.maxpos >= PL_regeol)
5437 ST.maxpos = PL_regeol - 1;
5439 goto curly_try_B_min_known;
5443 ST.count = regrepeat(rex, ST.A, ST.max, depth);
5444 locinput = PL_reginput;
5445 if (ST.count < ST.min)
5447 if ((ST.count > ST.min)
5448 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
5450 /* A{m,n} must come at the end of the string, there's
5451 * no point in backing off ... */
5453 /* ...except that $ and \Z can match before *and* after
5454 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
5455 We may back off by one in this case. */
5456 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
5460 goto curly_try_B_max;
5462 assert(0); /* NOTREACHED */
5465 case CURLY_B_min_known_fail:
5466 /* failed to find B in a non-greedy match where c1,c2 valid */
5468 PL_reginput = locinput; /* Could be reset... */
5469 REGCP_UNWIND(ST.cp);
5471 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
5473 /* Couldn't or didn't -- move forward. */
5474 ST.oldloc = locinput;
5476 locinput += UTF8SKIP(locinput);
5480 curly_try_B_min_known:
5481 /* find the next place where 'B' could work, then call B */
5485 n = (ST.oldloc == locinput) ? 0 : 1;
5486 if (ST.c1 == ST.c2) {
5488 /* set n to utf8_distance(oldloc, locinput) */
5489 while (locinput <= ST.maxpos &&
5490 utf8n_to_uvchr((U8*)locinput,
5491 UTF8_MAXBYTES, &len,
5492 uniflags) != (UV)ST.c1) {
5498 /* set n to utf8_distance(oldloc, locinput) */
5499 while (locinput <= ST.maxpos) {
5501 const UV c = utf8n_to_uvchr((U8*)locinput,
5502 UTF8_MAXBYTES, &len,
5504 if (c == (UV)ST.c1 || c == (UV)ST.c2)
5512 if (ST.c1 == ST.c2) {
5513 while (locinput <= ST.maxpos &&
5514 UCHARAT(locinput) != ST.c1)
5518 while (locinput <= ST.maxpos
5519 && UCHARAT(locinput) != ST.c1
5520 && UCHARAT(locinput) != ST.c2)
5523 n = locinput - ST.oldloc;
5525 if (locinput > ST.maxpos)
5527 /* PL_reginput == oldloc now */
5530 if (regrepeat(rex, ST.A, n, depth) < n)
5533 PL_reginput = locinput;
5534 CURLY_SETPAREN(ST.paren, ST.count);
5535 if (cur_eval && cur_eval->u.eval.close_paren &&
5536 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5539 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5541 assert(0); /* NOTREACHED */
5544 case CURLY_B_min_fail:
5545 /* failed to find B in a non-greedy match where c1,c2 invalid */
5547 REGCP_UNWIND(ST.cp);
5549 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
5551 /* failed -- move forward one */
5552 PL_reginput = locinput;
5553 if (regrepeat(rex, ST.A, 1, depth)) {
5555 locinput = PL_reginput;
5556 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5557 ST.count > 0)) /* count overflow ? */
5560 CURLY_SETPAREN(ST.paren, ST.count);
5561 if (cur_eval && cur_eval->u.eval.close_paren &&
5562 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5565 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5569 assert(0); /* NOTREACHED */
5573 /* a successful greedy match: now try to match B */
5574 if (cur_eval && cur_eval->u.eval.close_paren &&
5575 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5580 if (ST.c1 != CHRTEST_VOID)
5581 c = utf8_target ? utf8n_to_uvchr((U8*)PL_reginput,
5582 UTF8_MAXBYTES, 0, uniflags)
5583 : (UV) UCHARAT(PL_reginput);
5584 /* If it could work, try it. */
5585 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5586 CURLY_SETPAREN(ST.paren, ST.count);
5587 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5588 assert(0); /* NOTREACHED */
5592 case CURLY_B_max_fail:
5593 /* failed to find B in a greedy match */
5595 REGCP_UNWIND(ST.cp);
5597 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
5600 if (--ST.count < ST.min)
5602 PL_reginput = locinput = HOPc(locinput, -1);
5603 goto curly_try_B_max;
5610 /* we've just finished A in /(??{A})B/; now continue with B */
5611 st->u.eval.toggle_reg_flags
5612 = cur_eval->u.eval.toggle_reg_flags;
5613 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5615 st->u.eval.prev_rex = rex_sv; /* inner */
5616 st->u.eval.cp = regcppush(rex, 0); /* Save *all* the positions. */
5617 rex_sv = cur_eval->u.eval.prev_rex;
5618 SET_reg_curpm(rex_sv);
5619 rex = (struct regexp *)SvANY(rex_sv);
5620 rexi = RXi_GET(rex);
5621 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5623 REGCP_SET(st->u.eval.lastcp);
5624 PL_reginput = locinput;
5626 /* Restore parens of the outer rex without popping the
5628 S_regcp_restore(aTHX_ rex, cur_eval->u.eval.lastcp);
5630 st->u.eval.prev_eval = cur_eval;
5631 cur_eval = cur_eval->u.eval.prev_eval;
5633 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5634 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5635 if ( nochange_depth )
5638 PUSH_YES_STATE_GOTO(EVAL_AB,
5639 st->u.eval.prev_eval->u.eval.B); /* match B */
5642 if (locinput < reginfo->till) {
5643 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5644 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5646 (long)(locinput - PL_reg_starttry),
5647 (long)(reginfo->till - PL_reg_starttry),
5650 sayNO_SILENT; /* Cannot match: too short. */
5652 PL_reginput = locinput; /* put where regtry can find it */
5653 sayYES; /* Success! */
5655 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5657 PerlIO_printf(Perl_debug_log,
5658 "%*s %ssubpattern success...%s\n",
5659 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5660 PL_reginput = locinput; /* put where regtry can find it */
5661 sayYES; /* Success! */
5664 #define ST st->u.ifmatch
5666 case SUSPEND: /* (?>A) */
5668 PL_reginput = locinput;
5671 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5673 goto ifmatch_trivial_fail_test;
5675 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5677 ifmatch_trivial_fail_test:
5679 char * const s = HOPBACKc(locinput, scan->flags);
5684 sw = 1 - cBOOL(ST.wanted);
5688 next = scan + ARG(scan);
5696 PL_reginput = locinput;
5700 ST.logical = logical;
5701 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5703 /* execute body of (?...A) */
5704 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5705 assert(0); /* NOTREACHED */
5707 case IFMATCH_A_fail: /* body of (?...A) failed */
5708 ST.wanted = !ST.wanted;
5711 case IFMATCH_A: /* body of (?...A) succeeded */
5713 sw = cBOOL(ST.wanted);
5715 else if (!ST.wanted)
5718 if (OP(ST.me) == SUSPEND)
5719 locinput = PL_reginput;
5721 locinput = PL_reginput = st->locinput;
5722 nextchr = UCHARAT(locinput);
5724 scan = ST.me + ARG(ST.me);
5727 continue; /* execute B */
5732 next = scan + ARG(scan);
5737 reginfo->cutpoint = PL_regeol;
5740 PL_reginput = locinput;
5742 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5743 PUSH_STATE_GOTO(COMMIT_next,next);
5744 assert(0); /* NOTREACHED */
5745 case COMMIT_next_fail:
5750 assert(0); /* NOTREACHED */
5752 #define ST st->u.mark
5754 ST.prev_mark = mark_state;
5755 ST.mark_name = sv_commit = sv_yes_mark
5756 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5758 ST.mark_loc = PL_reginput = locinput;
5759 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5760 assert(0); /* NOTREACHED */
5761 case MARKPOINT_next:
5762 mark_state = ST.prev_mark;
5764 assert(0); /* NOTREACHED */
5765 case MARKPOINT_next_fail:
5766 if (popmark && sv_eq(ST.mark_name,popmark))
5768 if (ST.mark_loc > startpoint)
5769 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5770 popmark = NULL; /* we found our mark */
5771 sv_commit = ST.mark_name;
5774 PerlIO_printf(Perl_debug_log,
5775 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5776 REPORT_CODE_OFF+depth*2, "",
5777 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5780 mark_state = ST.prev_mark;
5781 sv_yes_mark = mark_state ?
5782 mark_state->u.mark.mark_name : NULL;
5784 assert(0); /* NOTREACHED */
5786 PL_reginput = locinput;
5788 /* (*SKIP) : if we fail we cut here*/
5789 ST.mark_name = NULL;
5790 ST.mark_loc = locinput;
5791 PUSH_STATE_GOTO(SKIP_next,next);
5793 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5794 otherwise do nothing. Meaning we need to scan
5796 regmatch_state *cur = mark_state;
5797 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5800 if ( sv_eq( cur->u.mark.mark_name,
5803 ST.mark_name = find;
5804 PUSH_STATE_GOTO( SKIP_next, next );
5806 cur = cur->u.mark.prev_mark;
5809 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5811 case SKIP_next_fail:
5813 /* (*CUT:NAME) - Set up to search for the name as we
5814 collapse the stack*/
5815 popmark = ST.mark_name;
5817 /* (*CUT) - No name, we cut here.*/
5818 if (ST.mark_loc > startpoint)
5819 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5820 /* but we set sv_commit to latest mark_name if there
5821 is one so they can test to see how things lead to this
5824 sv_commit=mark_state->u.mark.mark_name;
5828 assert(0); /* NOTREACHED */
5831 if ((n=is_LNBREAK(locinput,utf8_target))) {
5833 nextchr = UCHARAT(locinput);
5838 #define CASE_CLASS(nAmE) \
5840 if (locinput >= PL_regeol) \
5842 if ((n=is_##nAmE(locinput,utf8_target))) { \
5844 nextchr = UCHARAT(locinput); \
5849 if (locinput >= PL_regeol) \
5851 if ((n=is_##nAmE(locinput,utf8_target))) { \
5854 locinput += UTF8SKIP(locinput); \
5855 nextchr = UCHARAT(locinput); \
5860 CASE_CLASS(HORIZWS);
5864 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5865 PTR2UV(scan), OP(scan));
5866 Perl_croak(aTHX_ "regexp memory corruption");
5870 /* switch break jumps here */
5871 scan = next; /* prepare to execute the next op and ... */
5872 continue; /* ... jump back to the top, reusing st */
5873 assert(0); /* NOTREACHED */
5876 /* push a state that backtracks on success */
5877 st->u.yes.prev_yes_state = yes_state;
5881 /* push a new regex state, then continue at scan */
5883 regmatch_state *newst;
5886 regmatch_state *cur = st;
5887 regmatch_state *curyes = yes_state;
5889 regmatch_slab *slab = PL_regmatch_slab;
5890 for (;curd > -1;cur--,curd--) {
5891 if (cur < SLAB_FIRST(slab)) {
5893 cur = SLAB_LAST(slab);
5895 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5896 REPORT_CODE_OFF + 2 + depth * 2,"",
5897 curd, PL_reg_name[cur->resume_state],
5898 (curyes == cur) ? "yes" : ""
5901 curyes = cur->u.yes.prev_yes_state;
5904 DEBUG_STATE_pp("push")
5907 st->locinput = locinput;
5909 if (newst > SLAB_LAST(PL_regmatch_slab))
5910 newst = S_push_slab(aTHX);
5911 PL_regmatch_state = newst;
5913 locinput = PL_reginput;
5914 nextchr = UCHARAT(locinput);
5917 assert(0); /* NOTREACHED */
5922 * We get here only if there's trouble -- normally "case END" is
5923 * the terminating point.
5925 Perl_croak(aTHX_ "corrupted regexp pointers");
5931 /* we have successfully completed a subexpression, but we must now
5932 * pop to the state marked by yes_state and continue from there */
5933 assert(st != yes_state);
5935 while (st != yes_state) {
5937 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5938 PL_regmatch_slab = PL_regmatch_slab->prev;
5939 st = SLAB_LAST(PL_regmatch_slab);
5943 DEBUG_STATE_pp("pop (no final)");
5945 DEBUG_STATE_pp("pop (yes)");
5951 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5952 || yes_state > SLAB_LAST(PL_regmatch_slab))
5954 /* not in this slab, pop slab */
5955 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5956 PL_regmatch_slab = PL_regmatch_slab->prev;
5957 st = SLAB_LAST(PL_regmatch_slab);
5959 depth -= (st - yes_state);
5962 yes_state = st->u.yes.prev_yes_state;
5963 PL_regmatch_state = st;
5966 locinput= st->locinput;
5967 nextchr = UCHARAT(locinput);
5969 state_num = st->resume_state + no_final;
5970 goto reenter_switch;
5973 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5974 PL_colors[4], PL_colors[5]));
5976 if (PL_reg_state.re_state_eval_setup_done) {
5977 /* each successfully executed (?{...}) block does the equivalent of
5978 * local $^R = do {...}
5979 * When popping the save stack, all these locals would be undone;
5980 * bypass this by setting the outermost saved $^R to the latest
5982 if (oreplsv != GvSV(PL_replgv))
5983 sv_setsv(oreplsv, GvSV(PL_replgv));
5990 PerlIO_printf(Perl_debug_log,
5991 "%*s %sfailed...%s\n",
5992 REPORT_CODE_OFF+depth*2, "",
5993 PL_colors[4], PL_colors[5])
6005 /* there's a previous state to backtrack to */
6007 if (st < SLAB_FIRST(PL_regmatch_slab)) {
6008 PL_regmatch_slab = PL_regmatch_slab->prev;
6009 st = SLAB_LAST(PL_regmatch_slab);
6011 PL_regmatch_state = st;
6012 locinput= st->locinput;
6013 nextchr = UCHARAT(locinput);
6015 DEBUG_STATE_pp("pop");
6017 if (yes_state == st)
6018 yes_state = st->u.yes.prev_yes_state;
6020 state_num = st->resume_state + 1; /* failure = success + 1 */
6021 goto reenter_switch;
6026 if (rex->intflags & PREGf_VERBARG_SEEN) {
6027 SV *sv_err = get_sv("REGERROR", 1);
6028 SV *sv_mrk = get_sv("REGMARK", 1);
6030 sv_commit = &PL_sv_no;
6032 sv_yes_mark = &PL_sv_yes;
6035 sv_commit = &PL_sv_yes;
6036 sv_yes_mark = &PL_sv_no;
6038 sv_setsv(sv_err, sv_commit);
6039 sv_setsv(sv_mrk, sv_yes_mark);
6043 if (last_pushed_cv) {
6046 PERL_UNUSED_VAR(SP);
6049 /* clean up; in particular, free all slabs above current one */
6050 LEAVE_SCOPE(oldsave);
6056 - regrepeat - repeatedly match something simple, report how many
6059 * [This routine now assumes that it will only match on things of length 1.
6060 * That was true before, but now we assume scan - reginput is the count,
6061 * rather than incrementing count on every character. [Er, except utf8.]]
6064 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
6067 register char *scan;
6069 register char *loceol = PL_regeol;
6070 register I32 hardcount = 0;
6071 register bool utf8_target = PL_reg_match_utf8;
6074 PERL_UNUSED_ARG(depth);
6077 PERL_ARGS_ASSERT_REGREPEAT;
6080 if (max == REG_INFTY)
6082 else if (max < loceol - scan)
6083 loceol = scan + max;
6088 while (scan < loceol && hardcount < max && *scan != '\n') {
6089 scan += UTF8SKIP(scan);
6093 while (scan < loceol && *scan != '\n')
6100 while (scan < loceol && hardcount < max) {
6101 scan += UTF8SKIP(scan);
6112 /* To get here, EXACTish nodes must have *byte* length == 1. That
6113 * means they match only characters in the string that can be expressed
6114 * as a single byte. For non-utf8 strings, that means a simple match.
6115 * For utf8 strings, the character matched must be an invariant, or
6116 * downgradable to a single byte. The pattern's utf8ness is
6117 * irrelevant, as since it's a single byte, it either isn't utf8, or if
6118 * it is, it's an invariant */
6121 assert(! UTF_PATTERN || UNI_IS_INVARIANT(c));
6123 if (! utf8_target || UNI_IS_INVARIANT(c)) {
6124 while (scan < loceol && UCHARAT(scan) == c) {
6130 /* Here, the string is utf8, and the pattern char is different
6131 * in utf8 than not, so can't compare them directly. Outside the
6132 * loop, find the two utf8 bytes that represent c, and then
6133 * look for those in sequence in the utf8 string */
6134 U8 high = UTF8_TWO_BYTE_HI(c);
6135 U8 low = UTF8_TWO_BYTE_LO(c);
6138 while (hardcount < max
6139 && scan + 1 < loceol
6140 && UCHARAT(scan) == high
6141 && UCHARAT(scan + 1) == low)
6149 utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
6153 PL_reg_flags |= RF_tainted;
6154 utf8_flags = FOLDEQ_UTF8_LOCALE;
6162 case EXACTFU_TRICKYFOLD:
6164 utf8_flags = (UTF_PATTERN) ? FOLDEQ_S2_ALREADY_FOLDED : 0;
6166 /* The comments for the EXACT case above apply as well to these fold
6171 assert(! UTF_PATTERN || UNI_IS_INVARIANT(c));
6173 if (utf8_target || OP(p) == EXACTFU_SS) { /* Use full Unicode fold matching */
6174 char *tmpeol = loceol;
6175 while (hardcount < max
6176 && foldEQ_utf8_flags(scan, &tmpeol, 0, utf8_target,
6177 STRING(p), NULL, 1, cBOOL(UTF_PATTERN), utf8_flags))
6184 /* XXX Note that the above handles properly the German sharp s in
6185 * the pattern matching ss in the string. But it doesn't handle
6186 * properly cases where the string contains say 'LIGATURE ff' and
6187 * the pattern is 'f+'. This would require, say, a new function or
6188 * revised interface to foldEQ_utf8(), in which the maximum number
6189 * of characters to match could be passed and it would return how
6190 * many actually did. This is just one of many cases where
6191 * multi-char folds don't work properly, and so the fix is being
6197 /* Here, the string isn't utf8 and c is a single byte; and either
6198 * the pattern isn't utf8 or c is an invariant, so its utf8ness
6199 * doesn't affect c. Can just do simple comparisons for exact or
6202 case EXACTF: folded = PL_fold[c]; break;
6204 case EXACTFU_TRICKYFOLD:
6205 case EXACTFU: folded = PL_fold_latin1[c]; break;
6206 case EXACTFL: folded = PL_fold_locale[c]; break;
6207 default: Perl_croak(aTHX_ "panic: Unexpected op %u", OP(p));
6209 while (scan < loceol &&
6210 (UCHARAT(scan) == c || UCHARAT(scan) == folded))
6218 if (utf8_target || OP(p) == ANYOFV) {
6221 inclasslen = loceol - scan;
6222 while (hardcount < max
6223 && ((inclasslen = loceol - scan) > 0)
6224 && reginclass(prog, p, (U8*)scan, &inclasslen, utf8_target))
6230 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
6238 LOAD_UTF8_CHARCLASS_ALNUM();
6239 while (hardcount < max && scan < loceol &&
6240 swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target))
6242 scan += UTF8SKIP(scan);
6246 while (scan < loceol && isWORDCHAR_L1((U8) *scan)) {
6254 while (scan < loceol && isALNUM((U8) *scan)) {
6259 while (scan < loceol && isWORDCHAR_A((U8) *scan)) {
6264 PL_reg_flags |= RF_tainted;
6267 while (hardcount < max && scan < loceol &&
6268 isALNUM_LC_utf8((U8*)scan)) {
6269 scan += UTF8SKIP(scan);
6273 while (scan < loceol && isALNUM_LC(*scan))
6283 LOAD_UTF8_CHARCLASS_ALNUM();
6284 while (hardcount < max && scan < loceol &&
6285 ! swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target))
6287 scan += UTF8SKIP(scan);
6291 while (scan < loceol && ! isWORDCHAR_L1((U8) *scan)) {
6298 goto utf8_Nwordchar;
6299 while (scan < loceol && ! isALNUM((U8) *scan)) {
6305 while (scan < loceol && ! isWORDCHAR_A((U8) *scan)) {
6306 scan += UTF8SKIP(scan);
6310 while (scan < loceol && ! isWORDCHAR_A((U8) *scan)) {
6316 PL_reg_flags |= RF_tainted;
6319 while (hardcount < max && scan < loceol &&
6320 !isALNUM_LC_utf8((U8*)scan)) {
6321 scan += UTF8SKIP(scan);
6325 while (scan < loceol && !isALNUM_LC(*scan))
6335 LOAD_UTF8_CHARCLASS_SPACE();
6336 while (hardcount < max && scan < loceol &&
6338 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target)))
6340 scan += UTF8SKIP(scan);
6346 while (scan < loceol && isSPACE_L1((U8) *scan)) {
6355 while (scan < loceol && isSPACE((U8) *scan)) {
6360 while (scan < loceol && isSPACE_A((U8) *scan)) {
6365 PL_reg_flags |= RF_tainted;
6368 while (hardcount < max && scan < loceol &&
6369 isSPACE_LC_utf8((U8*)scan)) {
6370 scan += UTF8SKIP(scan);
6374 while (scan < loceol && isSPACE_LC(*scan))
6384 LOAD_UTF8_CHARCLASS_SPACE();
6385 while (hardcount < max && scan < loceol &&
6387 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target)))
6389 scan += UTF8SKIP(scan);
6395 while (scan < loceol && ! isSPACE_L1((U8) *scan)) {
6404 while (scan < loceol && ! isSPACE((U8) *scan)) {
6410 while (scan < loceol && ! isSPACE_A((U8) *scan)) {
6411 scan += UTF8SKIP(scan);
6415 while (scan < loceol && ! isSPACE_A((U8) *scan)) {
6421 PL_reg_flags |= RF_tainted;
6424 while (hardcount < max && scan < loceol &&
6425 !isSPACE_LC_utf8((U8*)scan)) {
6426 scan += UTF8SKIP(scan);
6430 while (scan < loceol && !isSPACE_LC(*scan))
6437 LOAD_UTF8_CHARCLASS_DIGIT();
6438 while (hardcount < max && scan < loceol &&
6439 swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
6440 scan += UTF8SKIP(scan);
6444 while (scan < loceol && isDIGIT(*scan))
6449 while (scan < loceol && isDIGIT_A((U8) *scan)) {
6454 PL_reg_flags |= RF_tainted;
6457 while (hardcount < max && scan < loceol &&
6458 isDIGIT_LC_utf8((U8*)scan)) {
6459 scan += UTF8SKIP(scan);
6463 while (scan < loceol && isDIGIT_LC(*scan))
6470 LOAD_UTF8_CHARCLASS_DIGIT();
6471 while (hardcount < max && scan < loceol &&
6472 !swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
6473 scan += UTF8SKIP(scan);
6477 while (scan < loceol && !isDIGIT(*scan))
6483 while (scan < loceol && ! isDIGIT_A((U8) *scan)) {
6484 scan += UTF8SKIP(scan);
6488 while (scan < loceol && ! isDIGIT_A((U8) *scan)) {
6494 PL_reg_flags |= RF_tainted;
6497 while (hardcount < max && scan < loceol &&
6498 !isDIGIT_LC_utf8((U8*)scan)) {
6499 scan += UTF8SKIP(scan);
6503 while (scan < loceol && !isDIGIT_LC(*scan))
6510 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
6516 LNBREAK can match two latin chars, which is ok,
6517 because we have a null terminated string, but we
6518 have to use hardcount in this situation
6520 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
6529 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
6534 while (scan < loceol && is_HORIZWS_latin1(scan))
6541 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
6542 scan += UTF8SKIP(scan);
6546 while (scan < loceol && !is_HORIZWS_latin1(scan))
6554 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
6559 while (scan < loceol && is_VERTWS_latin1(scan))
6567 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
6568 scan += UTF8SKIP(scan);
6572 while (scan < loceol && !is_VERTWS_latin1(scan))
6578 default: /* Called on something of 0 width. */
6579 break; /* So match right here or not at all. */
6585 c = scan - PL_reginput;
6589 GET_RE_DEBUG_FLAGS_DECL;
6591 SV * const prop = sv_newmortal();
6592 regprop(prog, prop, p);
6593 PerlIO_printf(Perl_debug_log,
6594 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
6595 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
6603 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
6605 - regclass_swash - prepare the utf8 swash. Wraps the shared core version to
6606 create a copy so that changes the caller makes won't change the shared one
6609 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
6611 PERL_ARGS_ASSERT_REGCLASS_SWASH;
6612 return newSVsv(core_regclass_swash(prog, node, doinit, listsvp, altsvp));
6617 S_core_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
6619 /* Returns the swash for the input 'node' in the regex 'prog'.
6620 * If <doinit> is true, will attempt to create the swash if not already
6622 * If <listsvp> is non-null, will return the swash initialization string in
6624 * If <altsvp> is non-null, will return the alternates to the regular swash
6626 * Tied intimately to how regcomp.c sets up the data structure */
6634 RXi_GET_DECL(prog,progi);
6635 const struct reg_data * const data = prog ? progi->data : NULL;
6637 PERL_ARGS_ASSERT_CORE_REGCLASS_SWASH;
6639 assert(ANYOF_NONBITMAP(node));
6641 if (data && data->count) {
6642 const U32 n = ARG(node);
6644 if (data->what[n] == 's') {
6645 SV * const rv = MUTABLE_SV(data->data[n]);
6646 AV * const av = MUTABLE_AV(SvRV(rv));
6647 SV **const ary = AvARRAY(av);
6648 bool invlist_has_user_defined_property;
6650 si = *ary; /* ary[0] = the string to initialize the swash with */
6652 /* Elements 3 and 4 are either both present or both absent. [3] is
6653 * any inversion list generated at compile time; [4] indicates if
6654 * that inversion list has any user-defined properties in it. */
6655 if (av_len(av) >= 3) {
6657 invlist_has_user_defined_property = cBOOL(SvUV(ary[4]));
6661 invlist_has_user_defined_property = FALSE;
6664 /* Element [1] is reserved for the set-up swash. If already there,
6665 * return it; if not, create it and store it there */
6666 if (SvROK(ary[1])) {
6669 else if (si && doinit) {
6671 sw = _core_swash_init("utf8", /* the utf8 package */
6675 0, /* not from tr/// */
6676 FALSE, /* is error if can't find
6679 invlist_has_user_defined_property);
6680 (void)av_store(av, 1, sw);
6683 /* Element [2] is for any multi-char folds. Note that is a
6684 * fundamentally flawed design, because can't backtrack and try
6685 * again. See [perl #89774] */
6686 if (SvTYPE(ary[2]) == SVt_PVAV) {
6693 SV* matches_string = newSVpvn("", 0);
6696 /* Use the swash, if any, which has to have incorporated into it all
6700 && SvTYPE(SvRV(sw)) == SVt_PVHV
6701 && (invlistsvp = hv_fetchs(MUTABLE_HV(SvRV(sw)), "INVLIST", FALSE)))
6703 invlist = *invlistsvp;
6705 else if (si && si != &PL_sv_undef) {
6707 /* If no swash, use the input nitialization string, if available */
6708 sv_catsv(matches_string, si);
6711 /* Add the inversion list to whatever we have. This may have come from
6712 * the swash, or from an input parameter */
6714 sv_catsv(matches_string, _invlist_contents(invlist));
6716 *listsvp = matches_string;
6726 - reginclass - determine if a character falls into a character class
6728 n is the ANYOF regnode
6729 p is the target string
6730 lenp is pointer to the maximum number of bytes of how far to go in p
6731 (This is assumed wthout checking to always be at least the current
6733 utf8_target tells whether p is in UTF-8.
6735 Returns true if matched; false otherwise. If lenp is not NULL, on return
6736 from a successful match, the value it points to will be updated to how many
6737 bytes in p were matched. If there was no match, the value is undefined,
6738 possibly changed from the input.
6740 Note that this can be a synthetic start class, a combination of various
6741 nodes, so things you think might be mutually exclusive, such as locale,
6742 aren't. It can match both locale and non-locale
6747 S_reginclass(pTHX_ const regexp * const prog, register const regnode * const n, register const U8* const p, STRLEN* lenp, register const bool utf8_target)
6750 const char flags = ANYOF_FLAGS(n);
6756 PERL_ARGS_ASSERT_REGINCLASS;
6758 /* If c is not already the code point, get it */
6759 if (utf8_target && !UTF8_IS_INVARIANT(c)) {
6760 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &c_len,
6761 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
6762 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
6763 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
6764 * UTF8_ALLOW_FFFF */
6765 if (c_len == (STRLEN)-1)
6766 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
6772 /* Use passed in max length, or one character if none passed in or less
6773 * than one character. And assume will match just one character. This is
6774 * overwritten later if matched more. */
6776 maxlen = (*lenp > c_len) ? *lenp : c_len;
6784 /* If this character is potentially in the bitmap, check it */
6786 if (ANYOF_BITMAP_TEST(n, c))
6788 else if (flags & ANYOF_NON_UTF8_LATIN1_ALL
6795 else if (flags & ANYOF_LOCALE) {
6796 PL_reg_flags |= RF_tainted;
6798 if ((flags & ANYOF_LOC_NONBITMAP_FOLD)
6799 && ANYOF_BITMAP_TEST(n, PL_fold_locale[c]))
6803 else if (ANYOF_CLASS_TEST_ANY_SET(n) &&
6804 ((ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6805 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6806 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6807 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6808 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6809 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6810 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6811 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6812 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6813 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6814 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII_LC(c)) ||
6815 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII_LC(c)) ||
6816 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6817 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6818 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6819 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6820 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6821 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6822 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6823 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6824 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6825 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6826 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6827 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6828 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6829 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6830 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6831 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6832 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK_LC(c)) ||
6833 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK_LC(c))
6834 ) /* How's that for a conditional? */
6841 /* If the bitmap didn't (or couldn't) match, and something outside the
6842 * bitmap could match, try that. Locale nodes specifiy completely the
6843 * behavior of code points in the bit map (otherwise, a utf8 target would
6844 * cause them to be treated as Unicode and not locale), except in
6845 * the very unlikely event when this node is a synthetic start class, which
6846 * could be a combination of locale and non-locale nodes. So allow locale
6847 * to match for the synthetic start class, which will give a false
6848 * positive that will be resolved when the match is done again as not part
6849 * of the synthetic start class */
6851 if (utf8_target && (flags & ANYOF_UNICODE_ALL) && c >= 256) {
6852 match = TRUE; /* Everything above 255 matches */
6854 else if (ANYOF_NONBITMAP(n)
6855 && ((flags & ANYOF_NONBITMAP_NON_UTF8)
6858 || (! (flags & ANYOF_LOCALE))
6859 || (flags & ANYOF_IS_SYNTHETIC)))))
6862 SV * const sw = core_regclass_swash(prog, n, TRUE, 0, (SV**)&av);
6870 /* Not utf8. Convert as much of the string as available up
6871 * to the limit of how far the (single) character in the
6872 * pattern can possibly match (no need to go further). If
6873 * the node is a straight ANYOF or not folding, it can't
6874 * match more than one. Otherwise, It can match up to how
6875 * far a single char can fold to. Since not utf8, each
6876 * character is a single byte, so the max it can be in
6877 * bytes is the same as the max it can be in characters */
6878 STRLEN len = (OP(n) == ANYOF
6879 || ! (flags & ANYOF_LOC_NONBITMAP_FOLD))
6881 : (maxlen < UTF8_MAX_FOLD_CHAR_EXPAND)
6883 : UTF8_MAX_FOLD_CHAR_EXPAND;
6884 utf8_p = bytes_to_utf8(p, &len);
6887 if (swash_fetch(sw, utf8_p, TRUE))
6889 else if (flags & ANYOF_LOC_NONBITMAP_FOLD) {
6891 /* Here, we need to test if the fold of the target string
6892 * matches. The non-multi char folds have all been moved to
6893 * the compilation phase, and the multi-char folds have
6894 * been stored by regcomp into 'av'; we linearly check to
6895 * see if any match the target string (folded). We know
6896 * that the originals were each one character, but we don't
6897 * currently know how many characters/bytes each folded to,
6898 * except we do know that there are small limits imposed by
6899 * Unicode. XXX A performance enhancement would be to have
6900 * regcomp.c store the max number of chars/bytes that are
6901 * in an av entry, as, say the 0th element. Even better
6902 * would be to have a hash of the few characters that can
6903 * start a multi-char fold to the max number of chars of
6906 * If there is a match, we will need to advance (if lenp is
6907 * specified) the match pointer in the target string. But
6908 * what we are comparing here isn't that string directly,
6909 * but its fold, whose length may differ from the original.
6910 * As we go along in constructing the fold, therefore, we
6911 * create a map so that we know how many bytes in the
6912 * source to advance given that we have matched a certain
6913 * number of bytes in the fold. This map is stored in
6914 * 'map_fold_len_back'. Let n mean the number of bytes in
6915 * the fold of the first character that we are folding.
6916 * Then map_fold_len_back[n] is set to the number of bytes
6917 * in that first character. Similarly let m be the
6918 * corresponding number for the second character to be
6919 * folded. Then map_fold_len_back[n+m] is set to the
6920 * number of bytes occupied by the first two source
6921 * characters. ... */
6922 U8 map_fold_len_back[UTF8_MAXBYTES_CASE+1] = { 0 };
6923 U8 folded[UTF8_MAXBYTES_CASE+1];
6924 STRLEN foldlen = 0; /* num bytes in fold of 1st char */
6925 STRLEN total_foldlen = 0; /* num bytes in fold of all
6928 if (OP(n) == ANYOF || maxlen == 1 || ! lenp || ! av) {
6930 /* Here, only need to fold the first char of the target
6931 * string. It the source wasn't utf8, is 1 byte long */
6932 to_utf8_fold(utf8_p, folded, &foldlen);
6933 total_foldlen = foldlen;
6934 map_fold_len_back[foldlen] = (utf8_target)
6940 /* Here, need to fold more than the first char. Do so
6941 * up to the limits */
6942 U8* source_ptr = utf8_p; /* The source for the fold
6945 U8* folded_ptr = folded;
6946 U8* e = utf8_p + maxlen; /* Can't go beyond last
6947 available byte in the
6951 i < UTF8_MAX_FOLD_CHAR_EXPAND && source_ptr < e;
6955 /* Fold the next character */
6956 U8 this_char_folded[UTF8_MAXBYTES_CASE+1];
6957 STRLEN this_char_foldlen;
6958 to_utf8_fold(source_ptr,
6960 &this_char_foldlen);
6962 /* Bail if it would exceed the byte limit for
6963 * folding a single char. */
6964 if (this_char_foldlen + folded_ptr - folded >
6970 /* Add the fold of this character */
6971 Copy(this_char_folded,
6975 source_ptr += UTF8SKIP(source_ptr);
6976 folded_ptr += this_char_foldlen;
6977 total_foldlen = folded_ptr - folded;
6979 /* Create map from the number of bytes in the fold
6980 * back to the number of bytes in the source. If
6981 * the source isn't utf8, the byte count is just
6982 * the number of characters so far */
6983 map_fold_len_back[total_foldlen]
6985 ? source_ptr - utf8_p
6992 /* Do the linear search to see if the fold is in the list
6993 * of multi-char folds. */
6996 for (i = 0; i <= av_len(av); i++) {
6997 SV* const sv = *av_fetch(av, i, FALSE);
6999 const char * const s = SvPV_const(sv, len);
7001 if (len <= total_foldlen
7002 && memEQ(s, (char*)folded, len)
7004 /* If 0, means matched a partial char. See
7006 && map_fold_len_back[len])
7009 /* Advance the target string ptr to account for
7010 * this fold, but have to translate from the
7011 * folded length to the corresponding source
7014 *lenp = map_fold_len_back[len];
7023 /* If we allocated a string above, free it */
7024 if (! utf8_target) Safefree(utf8_p);
7028 if (UNICODE_IS_SUPER(c)
7029 && (flags & ANYOF_WARN_SUPER)
7030 && ckWARN_d(WARN_NON_UNICODE))
7032 Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE),
7033 "Code point 0x%04"UVXf" is not Unicode, all \\p{} matches fail; all \\P{} matches succeed", c);
7037 return (flags & ANYOF_INVERT) ? !match : match;
7041 S_reghop3(U8 *s, I32 off, const U8* lim)
7043 /* return the position 'off' UTF-8 characters away from 's', forward if
7044 * 'off' >= 0, backwards if negative. But don't go outside of position
7045 * 'lim', which better be < s if off < 0 */
7049 PERL_ARGS_ASSERT_REGHOP3;
7052 while (off-- && s < lim) {
7053 /* XXX could check well-formedness here */
7058 while (off++ && s > lim) {
7060 if (UTF8_IS_CONTINUED(*s)) {
7061 while (s > lim && UTF8_IS_CONTINUATION(*s))
7064 /* XXX could check well-formedness here */
7071 /* there are a bunch of places where we use two reghop3's that should
7072 be replaced with this routine. but since thats not done yet
7073 we ifdef it out - dmq
7076 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
7080 PERL_ARGS_ASSERT_REGHOP4;
7083 while (off-- && s < rlim) {
7084 /* XXX could check well-formedness here */
7089 while (off++ && s > llim) {
7091 if (UTF8_IS_CONTINUED(*s)) {
7092 while (s > llim && UTF8_IS_CONTINUATION(*s))
7095 /* XXX could check well-formedness here */
7103 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
7107 PERL_ARGS_ASSERT_REGHOPMAYBE3;
7110 while (off-- && s < lim) {
7111 /* XXX could check well-formedness here */
7118 while (off++ && s > lim) {
7120 if (UTF8_IS_CONTINUED(*s)) {
7121 while (s > lim && UTF8_IS_CONTINUATION(*s))
7124 /* XXX could check well-formedness here */
7133 restore_pos(pTHX_ void *arg)
7136 regexp * const rex = (regexp *)arg;
7137 if (PL_reg_state.re_state_eval_setup_done) {
7138 if (PL_reg_oldsaved) {
7139 rex->subbeg = PL_reg_oldsaved;
7140 rex->sublen = PL_reg_oldsavedlen;
7141 #ifdef PERL_OLD_COPY_ON_WRITE
7142 rex->saved_copy = PL_nrs;
7144 RXp_MATCH_COPIED_on(rex);
7146 PL_reg_magic->mg_len = PL_reg_oldpos;
7147 PL_reg_state.re_state_eval_setup_done = FALSE;
7148 PL_curpm = PL_reg_oldcurpm;
7153 S_to_utf8_substr(pTHX_ register regexp *prog)
7157 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
7160 if (prog->substrs->data[i].substr
7161 && !prog->substrs->data[i].utf8_substr) {
7162 SV* const sv = newSVsv(prog->substrs->data[i].substr);
7163 prog->substrs->data[i].utf8_substr = sv;
7164 sv_utf8_upgrade(sv);
7165 if (SvVALID(prog->substrs->data[i].substr)) {
7166 if (SvTAIL(prog->substrs->data[i].substr)) {
7167 /* Trim the trailing \n that fbm_compile added last
7169 SvCUR_set(sv, SvCUR(sv) - 1);
7170 /* Whilst this makes the SV technically "invalid" (as its
7171 buffer is no longer followed by "\0") when fbm_compile()
7172 adds the "\n" back, a "\0" is restored. */
7173 fbm_compile(sv, FBMcf_TAIL);
7177 if (prog->substrs->data[i].substr == prog->check_substr)
7178 prog->check_utf8 = sv;
7184 S_to_byte_substr(pTHX_ register regexp *prog)
7189 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
7192 if (prog->substrs->data[i].utf8_substr
7193 && !prog->substrs->data[i].substr) {
7194 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
7195 if (sv_utf8_downgrade(sv, TRUE)) {
7196 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
7197 if (SvTAIL(prog->substrs->data[i].utf8_substr)) {
7198 /* Trim the trailing \n that fbm_compile added last
7200 SvCUR_set(sv, SvCUR(sv) - 1);
7201 fbm_compile(sv, FBMcf_TAIL);
7209 prog->substrs->data[i].substr = sv;
7210 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
7211 prog->check_substr = sv;
7218 * c-indentation-style: bsd
7220 * indent-tabs-mode: nil
7223 * ex: set ts=8 sts=4 sw=4 et: