5 * One Ring to rule them all, One Ring to find them
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
12 /* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
14 * a regular expression.
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
26 /* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
31 /* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
36 #ifdef PERL_EXT_RE_BUILD
41 * pregcomp and pregexec -- regsub and regerror are not used in perl
43 * Copyright (c) 1986 by University of Toronto.
44 * Written by Henry Spencer. Not derived from licensed software.
46 * Permission is granted to anyone to use this software for any
47 * purpose on any computer system, and to redistribute it freely,
48 * subject to the following restrictions:
50 * 1. The author is not responsible for the consequences of use of
51 * this software, no matter how awful, even if they arise
54 * 2. The origin of this software must not be misrepresented, either
55 * by explicit claim or by omission.
57 * 3. Altered versions must be plainly marked as such, and must not
58 * be misrepresented as being the original software.
60 **** Alterations to Henry's code are...
62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
66 **** You may distribute under the terms of either the GNU General Public
67 **** License or the Artistic License, as specified in the README file.
69 * Beware that some of this code is subtly aware of the way operator
70 * precedence is structured in regular expressions. Serious changes in
71 * regular-expression syntax might require a total rethink.
74 #define PERL_IN_REGEXEC_C
77 #ifdef PERL_IN_XSUB_RE
83 #define RF_tainted 1 /* tainted information used? */
84 #define RF_warned 2 /* warned about big count? */
86 #define RF_utf8 8 /* Pattern contains multibyte chars? */
88 #define UTF_PATTERN ((PL_reg_flags & RF_utf8) != 0)
90 #define RS_init 1 /* eval environment created */
91 #define RS_set 2 /* replsv value is set */
97 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
103 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define HOPc(pos,off) \
107 (char *)(PL_reg_match_utf8 \
108 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
110 #define HOPBACKc(pos, off) \
111 (char*)(PL_reg_match_utf8\
112 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
113 : (pos - off >= PL_bostr) \
117 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
118 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
120 /* these are unrolled below in the CCC_TRY_XXX defined */
121 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
122 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
124 /* Doesn't do an assert to verify that is correct */
125 #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
126 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END
128 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
129 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
130 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
132 #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
133 LOAD_UTF8_CHARCLASS(X_begin, " "); \
134 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
135 /* These are utf8 constants, and not utf-ebcdic constants, so the \
136 * assert should likely and hopefully fail on an EBCDIC machine */ \
137 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
139 /* No asserts are done for these, in case called on an early \
140 * Unicode version in which they map to nothing */ \
141 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
142 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
143 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
144 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
145 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
146 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
147 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
150 We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test
151 so that it is possible to override the option here without having to
152 rebuild the entire core. as we are required to do if we change regcomp.h
153 which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined.
155 #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS
156 #define BROKEN_UNICODE_CHARCLASS_MAPPINGS
159 #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS
160 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM()
161 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE()
162 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT()
163 #define RE_utf8_perl_word PL_utf8_alnum
164 #define RE_utf8_perl_space PL_utf8_space
165 #define RE_utf8_posix_digit PL_utf8_digit
166 #define perl_word alnum
167 #define perl_space space
168 #define posix_digit digit
170 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a")
171 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ")
172 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0")
173 #define RE_utf8_perl_word PL_utf8_perl_word
174 #define RE_utf8_perl_space PL_utf8_perl_space
175 #define RE_utf8_posix_digit PL_utf8_posix_digit
179 #define CCC_TRY_AFF(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
181 PL_reg_flags |= RF_tainted; \
186 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
187 if (!CAT2(PL_utf8_,CLASS)) { \
191 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
195 if (!(OP(scan) == NAME \
196 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, utf8_target)) \
197 : LCFUNC_utf8((U8*)locinput))) \
201 locinput += PL_utf8skip[nextchr]; \
202 nextchr = UCHARAT(locinput); \
205 if (!(OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
207 nextchr = UCHARAT(++locinput); \
210 #define CCC_TRY_NEG(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
212 PL_reg_flags |= RF_tainted; \
215 if (!nextchr && locinput >= PL_regeol) \
217 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
218 if (!CAT2(PL_utf8_,CLASS)) { \
222 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
226 if ((OP(scan) == NAME \
227 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, utf8_target)) \
228 : LCFUNC_utf8((U8*)locinput))) \
232 locinput += PL_utf8skip[nextchr]; \
233 nextchr = UCHARAT(locinput); \
236 if ((OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
238 nextchr = UCHARAT(++locinput); \
245 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
247 /* for use after a quantifier and before an EXACT-like node -- japhy */
248 /* it would be nice to rework regcomp.sym to generate this stuff. sigh */
249 #define JUMPABLE(rn) ( \
251 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
253 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
254 OP(rn) == PLUS || OP(rn) == MINMOD || \
255 OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \
256 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
258 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
260 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
263 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
264 we don't need this definition. */
265 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
266 #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
267 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
270 /* ... so we use this as its faster. */
271 #define IS_TEXT(rn) ( OP(rn)==EXACT )
272 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
273 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
278 Search for mandatory following text node; for lookahead, the text must
279 follow but for lookbehind (rn->flags != 0) we skip to the next step.
281 #define FIND_NEXT_IMPT(rn) STMT_START { \
282 while (JUMPABLE(rn)) { \
283 const OPCODE type = OP(rn); \
284 if (type == SUSPEND || PL_regkind[type] == CURLY) \
285 rn = NEXTOPER(NEXTOPER(rn)); \
286 else if (type == PLUS) \
288 else if (type == IFMATCH) \
289 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
290 else rn += NEXT_OFF(rn); \
295 static void restore_pos(pTHX_ void *arg);
297 #define REGCP_PAREN_ELEMS 4
298 #define REGCP_OTHER_ELEMS 5
299 #define REGCP_FRAME_ELEMS 1
300 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
301 * are needed for the regexp context stack bookkeeping. */
304 S_regcppush(pTHX_ I32 parenfloor)
307 const int retval = PL_savestack_ix;
308 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
309 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
310 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
312 GET_RE_DEBUG_FLAGS_DECL;
314 if (paren_elems_to_push < 0)
315 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
317 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
318 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
319 " out of range (%lu-%ld)",
320 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
322 SSGROW(total_elems + REGCP_FRAME_ELEMS);
324 for (p = PL_regsize; p > parenfloor; p--) {
325 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
326 SSPUSHINT(PL_regoffs[p].end);
327 SSPUSHINT(PL_regoffs[p].start);
328 SSPUSHPTR(PL_reg_start_tmp[p]);
330 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
331 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
332 (UV)p, (IV)PL_regoffs[p].start,
333 (IV)(PL_reg_start_tmp[p] - PL_bostr),
334 (IV)PL_regoffs[p].end
337 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
338 SSPUSHPTR(PL_regoffs);
339 SSPUSHINT(PL_regsize);
340 SSPUSHINT(*PL_reglastparen);
341 SSPUSHINT(*PL_reglastcloseparen);
342 SSPUSHPTR(PL_reginput);
343 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
348 /* These are needed since we do not localize EVAL nodes: */
349 #define REGCP_SET(cp) \
351 PerlIO_printf(Perl_debug_log, \
352 " Setting an EVAL scope, savestack=%"IVdf"\n", \
353 (IV)PL_savestack_ix)); \
356 #define REGCP_UNWIND(cp) \
358 if (cp != PL_savestack_ix) \
359 PerlIO_printf(Perl_debug_log, \
360 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
361 (IV)(cp), (IV)PL_savestack_ix)); \
365 S_regcppop(pTHX_ const regexp *rex)
370 GET_RE_DEBUG_FLAGS_DECL;
372 PERL_ARGS_ASSERT_REGCPPOP;
374 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
376 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
377 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
378 input = (char *) SSPOPPTR;
379 *PL_reglastcloseparen = SSPOPINT;
380 *PL_reglastparen = SSPOPINT;
381 PL_regsize = SSPOPINT;
382 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
384 i -= REGCP_OTHER_ELEMS;
385 /* Now restore the parentheses context. */
386 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
388 U32 paren = (U32)SSPOPINT;
389 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
390 PL_regoffs[paren].start = SSPOPINT;
392 if (paren <= *PL_reglastparen)
393 PL_regoffs[paren].end = tmps;
395 PerlIO_printf(Perl_debug_log,
396 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
397 (UV)paren, (IV)PL_regoffs[paren].start,
398 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
399 (IV)PL_regoffs[paren].end,
400 (paren > *PL_reglastparen ? "(no)" : ""));
404 if (*PL_reglastparen + 1 <= rex->nparens) {
405 PerlIO_printf(Perl_debug_log,
406 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
407 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
411 /* It would seem that the similar code in regtry()
412 * already takes care of this, and in fact it is in
413 * a better location to since this code can #if 0-ed out
414 * but the code in regtry() is needed or otherwise tests
415 * requiring null fields (pat.t#187 and split.t#{13,14}
416 * (as of patchlevel 7877) will fail. Then again,
417 * this code seems to be necessary or otherwise
418 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
419 * --jhi updated by dapm */
420 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
422 PL_regoffs[i].start = -1;
423 PL_regoffs[i].end = -1;
429 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
432 * pregexec and friends
435 #ifndef PERL_IN_XSUB_RE
437 - pregexec - match a regexp against a string
440 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
441 char *strbeg, I32 minend, SV *screamer, U32 nosave)
442 /* strend: pointer to null at end of string */
443 /* strbeg: real beginning of string */
444 /* minend: end of match must be >=minend after stringarg. */
445 /* nosave: For optimizations. */
447 PERL_ARGS_ASSERT_PREGEXEC;
450 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
451 nosave ? 0 : REXEC_COPY_STR);
456 * Need to implement the following flags for reg_anch:
458 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
460 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
461 * INTUIT_AUTORITATIVE_ML
462 * INTUIT_ONCE_NOML - Intuit can match in one location only.
465 * Another flag for this function: SECOND_TIME (so that float substrs
466 * with giant delta may be not rechecked).
469 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
471 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
472 Otherwise, only SvCUR(sv) is used to get strbeg. */
474 /* XXXX We assume that strpos is strbeg unless sv. */
476 /* XXXX Some places assume that there is a fixed substring.
477 An update may be needed if optimizer marks as "INTUITable"
478 RExen without fixed substrings. Similarly, it is assumed that
479 lengths of all the strings are no more than minlen, thus they
480 cannot come from lookahead.
481 (Or minlen should take into account lookahead.)
482 NOTE: Some of this comment is not correct. minlen does now take account
483 of lookahead/behind. Further research is required. -- demerphq
487 /* A failure to find a constant substring means that there is no need to make
488 an expensive call to REx engine, thus we celebrate a failure. Similarly,
489 finding a substring too deep into the string means that less calls to
490 regtry() should be needed.
492 REx compiler's optimizer found 4 possible hints:
493 a) Anchored substring;
495 c) Whether we are anchored (beginning-of-line or \G);
496 d) First node (of those at offset 0) which may distingush positions;
497 We use a)b)d) and multiline-part of c), and try to find a position in the
498 string which does not contradict any of them.
501 /* Most of decisions we do here should have been done at compile time.
502 The nodes of the REx which we used for the search should have been
503 deleted from the finite automaton. */
506 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
507 char *strend, const U32 flags, re_scream_pos_data *data)
510 struct regexp *const prog = (struct regexp *)SvANY(rx);
511 register I32 start_shift = 0;
512 /* Should be nonnegative! */
513 register I32 end_shift = 0;
518 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
520 register char *other_last = NULL; /* other substr checked before this */
521 char *check_at = NULL; /* check substr found at this pos */
522 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
523 RXi_GET_DECL(prog,progi);
525 const char * const i_strpos = strpos;
527 GET_RE_DEBUG_FLAGS_DECL;
529 PERL_ARGS_ASSERT_RE_INTUIT_START;
531 RX_MATCH_UTF8_set(rx,utf8_target);
534 PL_reg_flags |= RF_utf8;
537 debug_start_match(rx, utf8_target, strpos, strend,
538 sv ? "Guessing start of match in sv for"
539 : "Guessing start of match in string for");
542 /* CHR_DIST() would be more correct here but it makes things slow. */
543 if (prog->minlen > strend - strpos) {
544 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
545 "String too short... [re_intuit_start]\n"));
549 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
552 if (!prog->check_utf8 && prog->check_substr)
553 to_utf8_substr(prog);
554 check = prog->check_utf8;
556 if (!prog->check_substr && prog->check_utf8)
557 to_byte_substr(prog);
558 check = prog->check_substr;
560 if (check == &PL_sv_undef) {
561 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
562 "Non-utf8 string cannot match utf8 check string\n"));
565 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
566 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
567 || ( (prog->extflags & RXf_ANCH_BOL)
568 && !multiline ) ); /* Check after \n? */
571 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
572 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
573 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
575 && (strpos != strbeg)) {
576 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
579 if (prog->check_offset_min == prog->check_offset_max &&
580 !(prog->extflags & RXf_CANY_SEEN)) {
581 /* Substring at constant offset from beg-of-str... */
584 s = HOP3c(strpos, prog->check_offset_min, strend);
587 slen = SvCUR(check); /* >= 1 */
589 if ( strend - s > slen || strend - s < slen - 1
590 || (strend - s == slen && strend[-1] != '\n')) {
591 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
594 /* Now should match s[0..slen-2] */
596 if (slen && (*SvPVX_const(check) != *s
598 && memNE(SvPVX_const(check), s, slen)))) {
600 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
604 else if (*SvPVX_const(check) != *s
605 || ((slen = SvCUR(check)) > 1
606 && memNE(SvPVX_const(check), s, slen)))
609 goto success_at_start;
612 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
614 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
615 end_shift = prog->check_end_shift;
618 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
619 - (SvTAIL(check) != 0);
620 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
622 if (end_shift < eshift)
626 else { /* Can match at random position */
629 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
630 end_shift = prog->check_end_shift;
632 /* end shift should be non negative here */
635 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
637 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
638 (IV)end_shift, RX_PRECOMP(prog));
642 /* Find a possible match in the region s..strend by looking for
643 the "check" substring in the region corrected by start/end_shift. */
646 I32 srch_start_shift = start_shift;
647 I32 srch_end_shift = end_shift;
648 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
649 srch_end_shift -= ((strbeg - s) - srch_start_shift);
650 srch_start_shift = strbeg - s;
652 DEBUG_OPTIMISE_MORE_r({
653 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
654 (IV)prog->check_offset_min,
655 (IV)srch_start_shift,
657 (IV)prog->check_end_shift);
660 if (flags & REXEC_SCREAM) {
661 I32 p = -1; /* Internal iterator of scream. */
662 I32 * const pp = data ? data->scream_pos : &p;
664 if (PL_screamfirst[BmRARE(check)] >= 0
665 || ( BmRARE(check) == '\n'
666 && (BmPREVIOUS(check) == SvCUR(check) - 1)
668 s = screaminstr(sv, check,
669 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
672 /* we may be pointing at the wrong string */
673 if (s && RXp_MATCH_COPIED(prog))
674 s = strbeg + (s - SvPVX_const(sv));
676 *data->scream_olds = s;
681 if (prog->extflags & RXf_CANY_SEEN) {
682 start_point= (U8*)(s + srch_start_shift);
683 end_point= (U8*)(strend - srch_end_shift);
685 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
686 end_point= HOP3(strend, -srch_end_shift, strbeg);
688 DEBUG_OPTIMISE_MORE_r({
689 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
690 (int)(end_point - start_point),
691 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
695 s = fbm_instr( start_point, end_point,
696 check, multiline ? FBMrf_MULTILINE : 0);
699 /* Update the count-of-usability, remove useless subpatterns,
703 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
704 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
705 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
706 (s ? "Found" : "Did not find"),
707 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
708 ? "anchored" : "floating"),
711 (s ? " at offset " : "...\n") );
716 /* Finish the diagnostic message */
717 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
719 /* XXX dmq: first branch is for positive lookbehind...
720 Our check string is offset from the beginning of the pattern.
721 So we need to do any stclass tests offset forward from that
730 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
731 Start with the other substr.
732 XXXX no SCREAM optimization yet - and a very coarse implementation
733 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
734 *always* match. Probably should be marked during compile...
735 Probably it is right to do no SCREAM here...
738 if (utf8_target ? (prog->float_utf8 && prog->anchored_utf8)
739 : (prog->float_substr && prog->anchored_substr))
741 /* Take into account the "other" substring. */
742 /* XXXX May be hopelessly wrong for UTF... */
745 if (check == (utf8_target ? prog->float_utf8 : prog->float_substr)) {
748 char * const last = HOP3c(s, -start_shift, strbeg);
750 char * const saved_s = s;
753 t = s - prog->check_offset_max;
754 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
756 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
761 t = HOP3c(t, prog->anchored_offset, strend);
762 if (t < other_last) /* These positions already checked */
764 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
767 /* XXXX It is not documented what units *_offsets are in.
768 We assume bytes, but this is clearly wrong.
769 Meaning this code needs to be carefully reviewed for errors.
773 /* On end-of-str: see comment below. */
774 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
775 if (must == &PL_sv_undef) {
777 DEBUG_r(must = prog->anchored_utf8); /* for debug */
782 HOP3(HOP3(last1, prog->anchored_offset, strend)
783 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
785 multiline ? FBMrf_MULTILINE : 0
788 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
789 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
790 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
791 (s ? "Found" : "Contradicts"),
792 quoted, RE_SV_TAIL(must));
797 if (last1 >= last2) {
798 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
799 ", giving up...\n"));
802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
803 ", trying floating at offset %ld...\n",
804 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
805 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
806 s = HOP3c(last, 1, strend);
810 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
811 (long)(s - i_strpos)));
812 t = HOP3c(s, -prog->anchored_offset, strbeg);
813 other_last = HOP3c(s, 1, strend);
821 else { /* Take into account the floating substring. */
823 char * const saved_s = s;
826 t = HOP3c(s, -start_shift, strbeg);
828 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
829 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
830 last = HOP3c(t, prog->float_max_offset, strend);
831 s = HOP3c(t, prog->float_min_offset, strend);
834 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
835 must = utf8_target ? prog->float_utf8 : prog->float_substr;
836 /* fbm_instr() takes into account exact value of end-of-str
837 if the check is SvTAIL(ed). Since false positives are OK,
838 and end-of-str is not later than strend we are OK. */
839 if (must == &PL_sv_undef) {
841 DEBUG_r(must = prog->float_utf8); /* for debug message */
844 s = fbm_instr((unsigned char*)s,
845 (unsigned char*)last + SvCUR(must)
847 must, multiline ? FBMrf_MULTILINE : 0);
849 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
850 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
851 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
852 (s ? "Found" : "Contradicts"),
853 quoted, RE_SV_TAIL(must));
857 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
858 ", giving up...\n"));
861 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
862 ", trying anchored starting at offset %ld...\n",
863 (long)(saved_s + 1 - i_strpos)));
865 s = HOP3c(t, 1, strend);
869 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
870 (long)(s - i_strpos)));
871 other_last = s; /* Fix this later. --Hugo */
881 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
883 DEBUG_OPTIMISE_MORE_r(
884 PerlIO_printf(Perl_debug_log,
885 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
886 (IV)prog->check_offset_min,
887 (IV)prog->check_offset_max,
895 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
897 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
900 /* Fixed substring is found far enough so that the match
901 cannot start at strpos. */
903 if (ml_anch && t[-1] != '\n') {
904 /* Eventually fbm_*() should handle this, but often
905 anchored_offset is not 0, so this check will not be wasted. */
906 /* XXXX In the code below we prefer to look for "^" even in
907 presence of anchored substrings. And we search even
908 beyond the found float position. These pessimizations
909 are historical artefacts only. */
911 while (t < strend - prog->minlen) {
913 if (t < check_at - prog->check_offset_min) {
914 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
915 /* Since we moved from the found position,
916 we definitely contradict the found anchored
917 substr. Due to the above check we do not
918 contradict "check" substr.
919 Thus we can arrive here only if check substr
920 is float. Redo checking for "other"=="fixed".
923 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
924 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
925 goto do_other_anchored;
927 /* We don't contradict the found floating substring. */
928 /* XXXX Why not check for STCLASS? */
930 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
931 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
934 /* Position contradicts check-string */
935 /* XXXX probably better to look for check-string
936 than for "\n", so one should lower the limit for t? */
937 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
938 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
939 other_last = strpos = s = t + 1;
944 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
945 PL_colors[0], PL_colors[1]));
949 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
950 PL_colors[0], PL_colors[1]));
954 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
957 /* The found string does not prohibit matching at strpos,
958 - no optimization of calling REx engine can be performed,
959 unless it was an MBOL and we are not after MBOL,
960 or a future STCLASS check will fail this. */
962 /* Even in this situation we may use MBOL flag if strpos is offset
963 wrt the start of the string. */
964 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
965 && (strpos != strbeg) && strpos[-1] != '\n'
966 /* May be due to an implicit anchor of m{.*foo} */
967 && !(prog->intflags & PREGf_IMPLICIT))
972 DEBUG_EXECUTE_r( if (ml_anch)
973 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
974 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
977 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
979 prog->check_utf8 /* Could be deleted already */
980 && --BmUSEFUL(prog->check_utf8) < 0
981 && (prog->check_utf8 == prog->float_utf8)
983 prog->check_substr /* Could be deleted already */
984 && --BmUSEFUL(prog->check_substr) < 0
985 && (prog->check_substr == prog->float_substr)
988 /* If flags & SOMETHING - do not do it many times on the same match */
989 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
990 /* XXX Does the destruction order has to change with utf8_target? */
991 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
992 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
993 prog->check_substr = prog->check_utf8 = NULL; /* disable */
994 prog->float_substr = prog->float_utf8 = NULL; /* clear */
995 check = NULL; /* abort */
997 /* XXXX If the check string was an implicit check MBOL, then we need to unset the relevent flag
998 see http://bugs.activestate.com/show_bug.cgi?id=87173 */
999 if (prog->intflags & PREGf_IMPLICIT)
1000 prog->extflags &= ~RXf_ANCH_MBOL;
1001 /* XXXX This is a remnant of the old implementation. It
1002 looks wasteful, since now INTUIT can use many
1003 other heuristics. */
1004 prog->extflags &= ~RXf_USE_INTUIT;
1005 /* XXXX What other flags might need to be cleared in this branch? */
1011 /* Last resort... */
1012 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1013 /* trie stclasses are too expensive to use here, we are better off to
1014 leave it to regmatch itself */
1015 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1016 /* minlen == 0 is possible if regstclass is \b or \B,
1017 and the fixed substr is ''$.
1018 Since minlen is already taken into account, s+1 is before strend;
1019 accidentally, minlen >= 1 guaranties no false positives at s + 1
1020 even for \b or \B. But (minlen? 1 : 0) below assumes that
1021 regstclass does not come from lookahead... */
1022 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1023 This leaves EXACTF only, which is dealt with in find_byclass(). */
1024 const U8* const str = (U8*)STRING(progi->regstclass);
1025 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1026 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1029 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1030 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1031 else if (prog->float_substr || prog->float_utf8)
1032 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1036 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1037 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1040 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1043 const char *what = NULL;
1045 if (endpos == strend) {
1046 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1047 "Could not match STCLASS...\n") );
1050 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1051 "This position contradicts STCLASS...\n") );
1052 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1054 /* Contradict one of substrings */
1055 if (prog->anchored_substr || prog->anchored_utf8) {
1056 if ((utf8_target ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1057 DEBUG_EXECUTE_r( what = "anchored" );
1059 s = HOP3c(t, 1, strend);
1060 if (s + start_shift + end_shift > strend) {
1061 /* XXXX Should be taken into account earlier? */
1062 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1063 "Could not match STCLASS...\n") );
1068 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1069 "Looking for %s substr starting at offset %ld...\n",
1070 what, (long)(s + start_shift - i_strpos)) );
1073 /* Have both, check_string is floating */
1074 if (t + start_shift >= check_at) /* Contradicts floating=check */
1075 goto retry_floating_check;
1076 /* Recheck anchored substring, but not floating... */
1080 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1081 "Looking for anchored substr starting at offset %ld...\n",
1082 (long)(other_last - i_strpos)) );
1083 goto do_other_anchored;
1085 /* Another way we could have checked stclass at the
1086 current position only: */
1091 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1092 "Looking for /%s^%s/m starting at offset %ld...\n",
1093 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1096 if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1098 /* Check is floating subtring. */
1099 retry_floating_check:
1100 t = check_at - start_shift;
1101 DEBUG_EXECUTE_r( what = "floating" );
1102 goto hop_and_restart;
1105 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1106 "By STCLASS: moving %ld --> %ld\n",
1107 (long)(t - i_strpos), (long)(s - i_strpos))
1111 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1112 "Does not contradict STCLASS...\n");
1117 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1118 PL_colors[4], (check ? "Guessed" : "Giving up"),
1119 PL_colors[5], (long)(s - i_strpos)) );
1122 fail_finish: /* Substring not found */
1123 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1124 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1126 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1127 PL_colors[4], PL_colors[5]));
1131 #define DECL_TRIE_TYPE(scan) \
1132 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1133 trie_type = (scan->flags != EXACT) \
1134 ? (utf8_target ? trie_utf8_fold : (UTF_PATTERN ? trie_latin_utf8_fold : trie_plain)) \
1135 : (utf8_target ? trie_utf8 : trie_plain)
1137 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1138 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1139 switch (trie_type) { \
1140 case trie_utf8_fold: \
1141 if ( foldlen>0 ) { \
1142 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1147 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1148 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1149 foldlen -= UNISKIP( uvc ); \
1150 uscan = foldbuf + UNISKIP( uvc ); \
1153 case trie_latin_utf8_fold: \
1154 if ( foldlen>0 ) { \
1155 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1161 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1162 foldlen -= UNISKIP( uvc ); \
1163 uscan = foldbuf + UNISKIP( uvc ); \
1167 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1174 charid = trie->charmap[ uvc ]; \
1178 if (widecharmap) { \
1179 SV** const svpp = hv_fetch(widecharmap, \
1180 (char*)&uvc, sizeof(UV), 0); \
1182 charid = (U16)SvIV(*svpp); \
1187 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1189 char *my_strend= (char *)strend; \
1192 foldEQ_utf8(s, &my_strend, 0, utf8_target, \
1193 m, NULL, ln, cBOOL(UTF_PATTERN))) \
1194 && (!reginfo || regtry(reginfo, &s)) ) \
1197 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1198 uvchr_to_utf8(tmpbuf, c); \
1199 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1201 && (f == c1 || f == c2) \
1203 foldEQ_utf8(s, &my_strend, 0, utf8_target,\
1204 m, NULL, ln, cBOOL(UTF_PATTERN)))\
1205 && (!reginfo || regtry(reginfo, &s)) ) \
1211 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1215 && (ln == 1 || (OP(c) == EXACTF \
1216 ? foldEQ(s, m, ln) \
1217 : foldEQ_locale(s, m, ln))) \
1218 && (!reginfo || regtry(reginfo, &s)) ) \
1224 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1226 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1232 #define REXEC_FBC_SCAN(CoDe) \
1234 while (s < strend) { \
1240 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1241 REXEC_FBC_UTF8_SCAN( \
1243 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1252 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1255 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1264 #define REXEC_FBC_TRYIT \
1265 if ((!reginfo || regtry(reginfo, &s))) \
1268 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1269 if (utf8_target) { \
1270 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1273 REXEC_FBC_CLASS_SCAN(CoNd); \
1277 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1278 if (utf8_target) { \
1280 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1283 REXEC_FBC_CLASS_SCAN(CoNd); \
1287 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1288 PL_reg_flags |= RF_tainted; \
1289 if (utf8_target) { \
1290 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1293 REXEC_FBC_CLASS_SCAN(CoNd); \
1297 #define DUMP_EXEC_POS(li,s,doutf8) \
1298 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1300 /* We know what class REx starts with. Try to find this position... */
1301 /* if reginfo is NULL, its a dryrun */
1302 /* annoyingly all the vars in this routine have different names from their counterparts
1303 in regmatch. /grrr */
1306 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1307 const char *strend, regmatch_info *reginfo)
1310 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1314 register STRLEN uskip;
1318 register I32 tmp = 1; /* Scratch variable? */
1319 register const bool utf8_target = PL_reg_match_utf8;
1320 RXi_GET_DECL(prog,progi);
1322 PERL_ARGS_ASSERT_FIND_BYCLASS;
1324 /* We know what class it must start with. */
1328 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1329 !UTF8_IS_INVARIANT((U8)s[0]) ?
1330 reginclass(prog, c, (U8*)s, 0, utf8_target) :
1331 REGINCLASS(prog, c, (U8*)s));
1334 while (s < strend) {
1337 if (REGINCLASS(prog, c, (U8*)s) ||
1338 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1339 /* The assignment of 2 is intentional:
1340 * for the folded sharp s, the skip is 2. */
1341 (skip = SHARP_S_SKIP))) {
1342 if (tmp && (!reginfo || regtry(reginfo, &s)))
1355 if (tmp && (!reginfo || regtry(reginfo, &s)))
1363 ln = STR_LEN(c); /* length to match in octets/bytes */
1364 lnc = (I32) ln; /* length to match in characters */
1366 STRLEN ulen1, ulen2;
1368 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1369 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1370 /* used by commented-out code below */
1371 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1373 /* XXX: Since the node will be case folded at compile
1374 time this logic is a little odd, although im not
1375 sure that its actually wrong. --dmq */
1377 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1378 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1380 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1381 codepoint of the first character in the converted
1382 form, yet originally we did the extra step.
1383 No tests fail by commenting this code out however
1384 so Ive left it out. -- dmq.
1386 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1388 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1393 while (sm < ((U8 *) m + ln)) {
1408 c2 = PL_fold_locale[c1];
1410 e = HOP3c(strend, -((I32)lnc), s);
1412 if (!reginfo && e < s)
1413 e = s; /* Due to minlen logic of intuit() */
1415 /* The idea in the EXACTF* cases is to first find the
1416 * first character of the EXACTF* node and then, if
1417 * necessary, case-insensitively compare the full
1418 * text of the node. The c1 and c2 are the first
1419 * characters (though in Unicode it gets a bit
1420 * more complicated because there are more cases
1421 * than just upper and lower: one needs to use
1422 * the so-called folding case for case-insensitive
1423 * matching (called "loose matching" in Unicode).
1424 * foldEQ_utf8() will do just that. */
1426 if (utf8_target || UTF_PATTERN) {
1428 U8 tmpbuf [UTF8_MAXBYTES+1];
1431 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1433 /* Upper and lower of 1st char are equal -
1434 * probably not a "letter". */
1437 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1442 REXEC_FBC_EXACTISH_CHECK(c == c1);
1448 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1454 /* Handle some of the three Greek sigmas cases.
1455 * Note that not all the possible combinations
1456 * are handled here: some of them are handled
1457 * by the standard folding rules, and some of
1458 * them (the character class or ANYOF cases)
1459 * are handled during compiletime in
1460 * regexec.c:S_regclass(). */
1461 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1462 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1463 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1465 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1470 /* Neither pattern nor string are UTF8 */
1472 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1474 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1478 PL_reg_flags |= RF_tainted;
1485 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1486 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1488 tmp = ((OP(c) == BOUND ?
1489 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1490 LOAD_UTF8_CHARCLASS_ALNUM();
1491 REXEC_FBC_UTF8_SCAN(
1492 if (tmp == !(OP(c) == BOUND ?
1493 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)) :
1494 isALNUM_LC_utf8((U8*)s)))
1502 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1503 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1506 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1512 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1516 PL_reg_flags |= RF_tainted;
1523 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1524 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1526 tmp = ((OP(c) == NBOUND ?
1527 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1528 LOAD_UTF8_CHARCLASS_ALNUM();
1529 REXEC_FBC_UTF8_SCAN(
1530 if (tmp == !(OP(c) == NBOUND ?
1531 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)) :
1532 isALNUM_LC_utf8((U8*)s)))
1534 else REXEC_FBC_TRYIT;
1538 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1539 tmp = ((OP(c) == NBOUND ?
1540 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1543 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1545 else REXEC_FBC_TRYIT;
1548 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1552 REXEC_FBC_CSCAN_PRELOAD(
1553 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1554 swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
1558 REXEC_FBC_CSCAN_TAINT(
1559 isALNUM_LC_utf8((U8*)s),
1563 REXEC_FBC_CSCAN_PRELOAD(
1564 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1565 !swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
1569 REXEC_FBC_CSCAN_TAINT(
1570 !isALNUM_LC_utf8((U8*)s),
1574 REXEC_FBC_CSCAN_PRELOAD(
1575 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1576 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target),
1580 REXEC_FBC_CSCAN_TAINT(
1581 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1585 REXEC_FBC_CSCAN_PRELOAD(
1586 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1587 !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target)),
1591 REXEC_FBC_CSCAN_TAINT(
1592 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1596 REXEC_FBC_CSCAN_PRELOAD(
1597 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1598 swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
1602 REXEC_FBC_CSCAN_TAINT(
1603 isDIGIT_LC_utf8((U8*)s),
1607 REXEC_FBC_CSCAN_PRELOAD(
1608 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1609 !swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
1613 REXEC_FBC_CSCAN_TAINT(
1614 !isDIGIT_LC_utf8((U8*)s),
1620 is_LNBREAK_latin1(s)
1630 !is_VERTWS_latin1(s)
1635 is_HORIZWS_latin1(s)
1639 !is_HORIZWS_utf8(s),
1640 !is_HORIZWS_latin1(s)
1646 /* what trie are we using right now */
1648 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1650 = (reg_trie_data*)progi->data->data[ aho->trie ];
1651 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1653 const char *last_start = strend - trie->minlen;
1655 const char *real_start = s;
1657 STRLEN maxlen = trie->maxlen;
1659 U8 **points; /* map of where we were in the input string
1660 when reading a given char. For ASCII this
1661 is unnecessary overhead as the relationship
1662 is always 1:1, but for Unicode, especially
1663 case folded Unicode this is not true. */
1664 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1668 GET_RE_DEBUG_FLAGS_DECL;
1670 /* We can't just allocate points here. We need to wrap it in
1671 * an SV so it gets freed properly if there is a croak while
1672 * running the match */
1675 sv_points=newSV(maxlen * sizeof(U8 *));
1676 SvCUR_set(sv_points,
1677 maxlen * sizeof(U8 *));
1678 SvPOK_on(sv_points);
1679 sv_2mortal(sv_points);
1680 points=(U8**)SvPV_nolen(sv_points );
1681 if ( trie_type != trie_utf8_fold
1682 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1685 bitmap=(U8*)trie->bitmap;
1687 bitmap=(U8*)ANYOF_BITMAP(c);
1689 /* this is the Aho-Corasick algorithm modified a touch
1690 to include special handling for long "unknown char"
1691 sequences. The basic idea being that we use AC as long
1692 as we are dealing with a possible matching char, when
1693 we encounter an unknown char (and we have not encountered
1694 an accepting state) we scan forward until we find a legal
1696 AC matching is basically that of trie matching, except
1697 that when we encounter a failing transition, we fall back
1698 to the current states "fail state", and try the current char
1699 again, a process we repeat until we reach the root state,
1700 state 1, or a legal transition. If we fail on the root state
1701 then we can either terminate if we have reached an accepting
1702 state previously, or restart the entire process from the beginning
1706 while (s <= last_start) {
1707 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1715 U8 *uscan = (U8*)NULL;
1716 U8 *leftmost = NULL;
1718 U32 accepted_word= 0;
1722 while ( state && uc <= (U8*)strend ) {
1724 U32 word = aho->states[ state ].wordnum;
1728 DEBUG_TRIE_EXECUTE_r(
1729 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1730 dump_exec_pos( (char *)uc, c, strend, real_start,
1731 (char *)uc, utf8_target );
1732 PerlIO_printf( Perl_debug_log,
1733 " Scanning for legal start char...\n");
1736 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1741 if (uc >(U8*)last_start) break;
1745 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
1746 if (!leftmost || lpos < leftmost) {
1747 DEBUG_r(accepted_word=word);
1753 points[pointpos++ % maxlen]= uc;
1754 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1755 uscan, len, uvc, charid, foldlen,
1757 DEBUG_TRIE_EXECUTE_r({
1758 dump_exec_pos( (char *)uc, c, strend, real_start,
1760 PerlIO_printf(Perl_debug_log,
1761 " Charid:%3u CP:%4"UVxf" ",
1767 word = aho->states[ state ].wordnum;
1769 base = aho->states[ state ].trans.base;
1771 DEBUG_TRIE_EXECUTE_r({
1773 dump_exec_pos( (char *)uc, c, strend, real_start,
1775 PerlIO_printf( Perl_debug_log,
1776 "%sState: %4"UVxf", word=%"UVxf,
1777 failed ? " Fail transition to " : "",
1778 (UV)state, (UV)word);
1784 ( ((offset = base + charid
1785 - 1 - trie->uniquecharcount)) >= 0)
1786 && ((U32)offset < trie->lasttrans)
1787 && trie->trans[offset].check == state
1788 && (tmp=trie->trans[offset].next))
1790 DEBUG_TRIE_EXECUTE_r(
1791 PerlIO_printf( Perl_debug_log," - legal\n"));
1796 DEBUG_TRIE_EXECUTE_r(
1797 PerlIO_printf( Perl_debug_log," - fail\n"));
1799 state = aho->fail[state];
1803 /* we must be accepting here */
1804 DEBUG_TRIE_EXECUTE_r(
1805 PerlIO_printf( Perl_debug_log," - accepting\n"));
1814 if (!state) state = 1;
1817 if ( aho->states[ state ].wordnum ) {
1818 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
1819 if (!leftmost || lpos < leftmost) {
1820 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1825 s = (char*)leftmost;
1826 DEBUG_TRIE_EXECUTE_r({
1828 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1829 (UV)accepted_word, (IV)(s - real_start)
1832 if (!reginfo || regtry(reginfo, &s)) {
1838 DEBUG_TRIE_EXECUTE_r({
1839 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1842 DEBUG_TRIE_EXECUTE_r(
1843 PerlIO_printf( Perl_debug_log,"No match.\n"));
1852 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1862 - regexec_flags - match a regexp against a string
1865 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1866 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1867 /* strend: pointer to null at end of string */
1868 /* strbeg: real beginning of string */
1869 /* minend: end of match must be >=minend after stringarg. */
1870 /* data: May be used for some additional optimizations.
1871 Currently its only used, with a U32 cast, for transmitting
1872 the ganch offset when doing a /g match. This will change */
1873 /* nosave: For optimizations. */
1876 struct regexp *const prog = (struct regexp *)SvANY(rx);
1877 /*register*/ char *s;
1878 register regnode *c;
1879 /*register*/ char *startpos = stringarg;
1880 I32 minlen; /* must match at least this many chars */
1881 I32 dontbother = 0; /* how many characters not to try at end */
1882 I32 end_shift = 0; /* Same for the end. */ /* CC */
1883 I32 scream_pos = -1; /* Internal iterator of scream. */
1884 char *scream_olds = NULL;
1885 const bool utf8_target = cBOOL(DO_UTF8(sv));
1887 RXi_GET_DECL(prog,progi);
1888 regmatch_info reginfo; /* create some info to pass to regtry etc */
1889 regexp_paren_pair *swap = NULL;
1890 GET_RE_DEBUG_FLAGS_DECL;
1892 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1893 PERL_UNUSED_ARG(data);
1895 /* Be paranoid... */
1896 if (prog == NULL || startpos == NULL) {
1897 Perl_croak(aTHX_ "NULL regexp parameter");
1901 multiline = prog->extflags & RXf_PMf_MULTILINE;
1902 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1904 RX_MATCH_UTF8_set(rx, utf8_target);
1906 debug_start_match(rx, utf8_target, startpos, strend,
1910 minlen = prog->minlen;
1912 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1913 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1914 "String too short [regexec_flags]...\n"));
1919 /* Check validity of program. */
1920 if (UCHARAT(progi->program) != REG_MAGIC) {
1921 Perl_croak(aTHX_ "corrupted regexp program");
1925 PL_reg_eval_set = 0;
1929 PL_reg_flags |= RF_utf8;
1931 /* Mark beginning of line for ^ and lookbehind. */
1932 reginfo.bol = startpos; /* XXX not used ??? */
1936 /* Mark end of line for $ (and such) */
1939 /* see how far we have to get to not match where we matched before */
1940 reginfo.till = startpos+minend;
1942 /* If there is a "must appear" string, look for it. */
1945 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1947 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
1948 reginfo.ganch = startpos + prog->gofs;
1949 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1950 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
1951 } else if (sv && SvTYPE(sv) >= SVt_PVMG
1953 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1954 && mg->mg_len >= 0) {
1955 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1956 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1957 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
1959 if (prog->extflags & RXf_ANCH_GPOS) {
1960 if (s > reginfo.ganch)
1962 s = reginfo.ganch - prog->gofs;
1963 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1964 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
1970 reginfo.ganch = strbeg + PTR2UV(data);
1971 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1972 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
1974 } else { /* pos() not defined */
1975 reginfo.ganch = strbeg;
1976 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1977 "GPOS: reginfo.ganch = strbeg\n"));
1980 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1981 /* We have to be careful. If the previous successful match
1982 was from this regex we don't want a subsequent partially
1983 successful match to clobber the old results.
1984 So when we detect this possibility we add a swap buffer
1985 to the re, and switch the buffer each match. If we fail
1986 we switch it back, otherwise we leave it swapped.
1989 /* do we need a save destructor here for eval dies? */
1990 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
1992 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1993 re_scream_pos_data d;
1995 d.scream_olds = &scream_olds;
1996 d.scream_pos = &scream_pos;
1997 s = re_intuit_start(rx, sv, s, strend, flags, &d);
1999 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
2000 goto phooey; /* not present */
2006 /* Simplest case: anchored match need be tried only once. */
2007 /* [unless only anchor is BOL and multiline is set] */
2008 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2009 if (s == startpos && regtry(®info, &startpos))
2011 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2012 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2017 dontbother = minlen - 1;
2018 end = HOP3c(strend, -dontbother, strbeg) - 1;
2019 /* for multiline we only have to try after newlines */
2020 if (prog->check_substr || prog->check_utf8) {
2021 /* because of the goto we can not easily reuse the macros for bifurcating the
2022 unicode/non-unicode match modes here like we do elsewhere - demerphq */
2025 goto after_try_utf8;
2027 if (regtry(®info, &s)) {
2034 if (prog->extflags & RXf_USE_INTUIT) {
2035 s = re_intuit_start(rx, sv, s + UTF8SKIP(s), strend, flags, NULL);
2044 } /* end search for check string in unicode */
2046 if (s == startpos) {
2047 goto after_try_latin;
2050 if (regtry(®info, &s)) {
2057 if (prog->extflags & RXf_USE_INTUIT) {
2058 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2067 } /* end search for check string in latin*/
2068 } /* end search for check string */
2069 else { /* search for newline */
2071 /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
2074 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
2076 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2077 if (regtry(®info, &s))
2081 } /* end search for newline */
2082 } /* end anchored/multiline check string search */
2084 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2086 /* the warning about reginfo.ganch being used without intialization
2087 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2088 and we only enter this block when the same bit is set. */
2089 char *tmp_s = reginfo.ganch - prog->gofs;
2091 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2096 /* Messy cases: unanchored match. */
2097 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2098 /* we have /x+whatever/ */
2099 /* it must be a one character string (XXXX Except UTF_PATTERN?) */
2104 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2105 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2106 ch = SvPVX_const(utf8_target ? prog->anchored_utf8 : prog->anchored_substr)[0];
2111 DEBUG_EXECUTE_r( did_match = 1 );
2112 if (regtry(®info, &s)) goto got_it;
2114 while (s < strend && *s == ch)
2122 DEBUG_EXECUTE_r( did_match = 1 );
2123 if (regtry(®info, &s)) goto got_it;
2125 while (s < strend && *s == ch)
2130 DEBUG_EXECUTE_r(if (!did_match)
2131 PerlIO_printf(Perl_debug_log,
2132 "Did not find anchored character...\n")
2135 else if (prog->anchored_substr != NULL
2136 || prog->anchored_utf8 != NULL
2137 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2138 && prog->float_max_offset < strend - s)) {
2143 char *last1; /* Last position checked before */
2147 if (prog->anchored_substr || prog->anchored_utf8) {
2148 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2149 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2150 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
2151 back_max = back_min = prog->anchored_offset;
2153 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2154 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2155 must = utf8_target ? prog->float_utf8 : prog->float_substr;
2156 back_max = prog->float_max_offset;
2157 back_min = prog->float_min_offset;
2161 if (must == &PL_sv_undef)
2162 /* could not downgrade utf8 check substring, so must fail */
2168 last = HOP3c(strend, /* Cannot start after this */
2169 -(I32)(CHR_SVLEN(must)
2170 - (SvTAIL(must) != 0) + back_min), strbeg);
2173 last1 = HOPc(s, -1);
2175 last1 = s - 1; /* bogus */
2177 /* XXXX check_substr already used to find "s", can optimize if
2178 check_substr==must. */
2180 dontbother = end_shift;
2181 strend = HOPc(strend, -dontbother);
2182 while ( (s <= last) &&
2183 ((flags & REXEC_SCREAM)
2184 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2185 end_shift, &scream_pos, 0))
2186 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2187 (unsigned char*)strend, must,
2188 multiline ? FBMrf_MULTILINE : 0))) ) {
2189 /* we may be pointing at the wrong string */
2190 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2191 s = strbeg + (s - SvPVX_const(sv));
2192 DEBUG_EXECUTE_r( did_match = 1 );
2193 if (HOPc(s, -back_max) > last1) {
2194 last1 = HOPc(s, -back_min);
2195 s = HOPc(s, -back_max);
2198 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2200 last1 = HOPc(s, -back_min);
2204 while (s <= last1) {
2205 if (regtry(®info, &s))
2211 while (s <= last1) {
2212 if (regtry(®info, &s))
2218 DEBUG_EXECUTE_r(if (!did_match) {
2219 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
2220 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2221 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2222 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2223 ? "anchored" : "floating"),
2224 quoted, RE_SV_TAIL(must));
2228 else if ( (c = progi->regstclass) ) {
2230 const OPCODE op = OP(progi->regstclass);
2231 /* don't bother with what can't match */
2232 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2233 strend = HOPc(strend, -(minlen - 1));
2236 SV * const prop = sv_newmortal();
2237 regprop(prog, prop, c);
2239 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
2241 PerlIO_printf(Perl_debug_log,
2242 "Matching stclass %.*s against %s (%d bytes)\n",
2243 (int)SvCUR(prop), SvPVX_const(prop),
2244 quoted, (int)(strend - s));
2247 if (find_byclass(prog, c, s, strend, ®info))
2249 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2253 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2258 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2259 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2260 float_real = utf8_target ? prog->float_utf8 : prog->float_substr;
2262 if (flags & REXEC_SCREAM) {
2263 last = screaminstr(sv, float_real, s - strbeg,
2264 end_shift, &scream_pos, 1); /* last one */
2266 last = scream_olds; /* Only one occurrence. */
2267 /* we may be pointing at the wrong string */
2268 else if (RXp_MATCH_COPIED(prog))
2269 s = strbeg + (s - SvPVX_const(sv));
2273 const char * const little = SvPV_const(float_real, len);
2275 if (SvTAIL(float_real)) {
2276 if (memEQ(strend - len + 1, little, len - 1))
2277 last = strend - len + 1;
2278 else if (!multiline)
2279 last = memEQ(strend - len, little, len)
2280 ? strend - len : NULL;
2286 last = rninstr(s, strend, little, little + len);
2288 last = strend; /* matching "$" */
2293 PerlIO_printf(Perl_debug_log,
2294 "%sCan't trim the tail, match fails (should not happen)%s\n",
2295 PL_colors[4], PL_colors[5]));
2296 goto phooey; /* Should not happen! */
2298 dontbother = strend - last + prog->float_min_offset;
2300 if (minlen && (dontbother < minlen))
2301 dontbother = minlen - 1;
2302 strend -= dontbother; /* this one's always in bytes! */
2303 /* We don't know much -- general case. */
2306 if (regtry(®info, &s))
2315 if (regtry(®info, &s))
2317 } while (s++ < strend);
2326 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2328 if (PL_reg_eval_set)
2329 restore_pos(aTHX_ prog);
2330 if (RXp_PAREN_NAMES(prog))
2331 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2333 /* make sure $`, $&, $', and $digit will work later */
2334 if ( !(flags & REXEC_NOT_FIRST) ) {
2335 RX_MATCH_COPY_FREE(rx);
2336 if (flags & REXEC_COPY_STR) {
2337 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2338 #ifdef PERL_OLD_COPY_ON_WRITE
2340 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2342 PerlIO_printf(Perl_debug_log,
2343 "Copy on write: regexp capture, type %d\n",
2346 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2347 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2348 assert (SvPOKp(prog->saved_copy));
2352 RX_MATCH_COPIED_on(rx);
2353 s = savepvn(strbeg, i);
2359 prog->subbeg = strbeg;
2360 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2367 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2368 PL_colors[4], PL_colors[5]));
2369 if (PL_reg_eval_set)
2370 restore_pos(aTHX_ prog);
2372 /* we failed :-( roll it back */
2373 Safefree(prog->offs);
2382 - regtry - try match at specific point
2384 STATIC I32 /* 0 failure, 1 success */
2385 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2389 REGEXP *const rx = reginfo->prog;
2390 regexp *const prog = (struct regexp *)SvANY(rx);
2391 RXi_GET_DECL(prog,progi);
2392 GET_RE_DEBUG_FLAGS_DECL;
2394 PERL_ARGS_ASSERT_REGTRY;
2396 reginfo->cutpoint=NULL;
2398 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2401 PL_reg_eval_set = RS_init;
2402 DEBUG_EXECUTE_r(DEBUG_s(
2403 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2404 (IV)(PL_stack_sp - PL_stack_base));
2407 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2408 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2410 /* Apparently this is not needed, judging by wantarray. */
2411 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2412 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2415 /* Make $_ available to executed code. */
2416 if (reginfo->sv != DEFSV) {
2418 DEFSV_set(reginfo->sv);
2421 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2422 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2423 /* prepare for quick setting of pos */
2424 #ifdef PERL_OLD_COPY_ON_WRITE
2425 if (SvIsCOW(reginfo->sv))
2426 sv_force_normal_flags(reginfo->sv, 0);
2428 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2429 &PL_vtbl_mglob, NULL, 0);
2433 PL_reg_oldpos = mg->mg_len;
2434 SAVEDESTRUCTOR_X(restore_pos, prog);
2436 if (!PL_reg_curpm) {
2437 Newxz(PL_reg_curpm, 1, PMOP);
2440 SV* const repointer = &PL_sv_undef;
2441 /* this regexp is also owned by the new PL_reg_curpm, which
2442 will try to free it. */
2443 av_push(PL_regex_padav, repointer);
2444 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2445 PL_regex_pad = AvARRAY(PL_regex_padav);
2450 /* It seems that non-ithreads works both with and without this code.
2451 So for efficiency reasons it seems best not to have the code
2452 compiled when it is not needed. */
2453 /* This is safe against NULLs: */
2454 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2455 /* PM_reg_curpm owns a reference to this regexp. */
2458 PM_SETRE(PL_reg_curpm, rx);
2459 PL_reg_oldcurpm = PL_curpm;
2460 PL_curpm = PL_reg_curpm;
2461 if (RXp_MATCH_COPIED(prog)) {
2462 /* Here is a serious problem: we cannot rewrite subbeg,
2463 since it may be needed if this match fails. Thus
2464 $` inside (?{}) could fail... */
2465 PL_reg_oldsaved = prog->subbeg;
2466 PL_reg_oldsavedlen = prog->sublen;
2467 #ifdef PERL_OLD_COPY_ON_WRITE
2468 PL_nrs = prog->saved_copy;
2470 RXp_MATCH_COPIED_off(prog);
2473 PL_reg_oldsaved = NULL;
2474 prog->subbeg = PL_bostr;
2475 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2477 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2478 prog->offs[0].start = *startpos - PL_bostr;
2479 PL_reginput = *startpos;
2480 PL_reglastparen = &prog->lastparen;
2481 PL_reglastcloseparen = &prog->lastcloseparen;
2482 prog->lastparen = 0;
2483 prog->lastcloseparen = 0;
2485 PL_regoffs = prog->offs;
2486 if (PL_reg_start_tmpl <= prog->nparens) {
2487 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2488 if(PL_reg_start_tmp)
2489 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2491 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2494 /* XXXX What this code is doing here?!!! There should be no need
2495 to do this again and again, PL_reglastparen should take care of
2498 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2499 * Actually, the code in regcppop() (which Ilya may be meaning by
2500 * PL_reglastparen), is not needed at all by the test suite
2501 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2502 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2503 * Meanwhile, this code *is* needed for the
2504 * above-mentioned test suite tests to succeed. The common theme
2505 * on those tests seems to be returning null fields from matches.
2506 * --jhi updated by dapm */
2508 if (prog->nparens) {
2509 regexp_paren_pair *pp = PL_regoffs;
2511 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2519 if (regmatch(reginfo, progi->program + 1)) {
2520 PL_regoffs[0].end = PL_reginput - PL_bostr;
2523 if (reginfo->cutpoint)
2524 *startpos= reginfo->cutpoint;
2525 REGCP_UNWIND(lastcp);
2530 #define sayYES goto yes
2531 #define sayNO goto no
2532 #define sayNO_SILENT goto no_silent
2534 /* we dont use STMT_START/END here because it leads to
2535 "unreachable code" warnings, which are bogus, but distracting. */
2536 #define CACHEsayNO \
2537 if (ST.cache_mask) \
2538 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2541 /* this is used to determine how far from the left messages like
2542 'failed...' are printed. It should be set such that messages
2543 are inline with the regop output that created them.
2545 #define REPORT_CODE_OFF 32
2548 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2549 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2551 #define SLAB_FIRST(s) (&(s)->states[0])
2552 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2554 /* grab a new slab and return the first slot in it */
2556 STATIC regmatch_state *
2559 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2562 regmatch_slab *s = PL_regmatch_slab->next;
2564 Newx(s, 1, regmatch_slab);
2565 s->prev = PL_regmatch_slab;
2567 PL_regmatch_slab->next = s;
2569 PL_regmatch_slab = s;
2570 return SLAB_FIRST(s);
2574 /* push a new state then goto it */
2576 #define PUSH_STATE_GOTO(state, node) \
2578 st->resume_state = state; \
2581 /* push a new state with success backtracking, then goto it */
2583 #define PUSH_YES_STATE_GOTO(state, node) \
2585 st->resume_state = state; \
2586 goto push_yes_state;
2592 regmatch() - main matching routine
2594 This is basically one big switch statement in a loop. We execute an op,
2595 set 'next' to point the next op, and continue. If we come to a point which
2596 we may need to backtrack to on failure such as (A|B|C), we push a
2597 backtrack state onto the backtrack stack. On failure, we pop the top
2598 state, and re-enter the loop at the state indicated. If there are no more
2599 states to pop, we return failure.
2601 Sometimes we also need to backtrack on success; for example /A+/, where
2602 after successfully matching one A, we need to go back and try to
2603 match another one; similarly for lookahead assertions: if the assertion
2604 completes successfully, we backtrack to the state just before the assertion
2605 and then carry on. In these cases, the pushed state is marked as
2606 'backtrack on success too'. This marking is in fact done by a chain of
2607 pointers, each pointing to the previous 'yes' state. On success, we pop to
2608 the nearest yes state, discarding any intermediate failure-only states.
2609 Sometimes a yes state is pushed just to force some cleanup code to be
2610 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2611 it to free the inner regex.
2613 Note that failure backtracking rewinds the cursor position, while
2614 success backtracking leaves it alone.
2616 A pattern is complete when the END op is executed, while a subpattern
2617 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2618 ops trigger the "pop to last yes state if any, otherwise return true"
2621 A common convention in this function is to use A and B to refer to the two
2622 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2623 the subpattern to be matched possibly multiple times, while B is the entire
2624 rest of the pattern. Variable and state names reflect this convention.
2626 The states in the main switch are the union of ops and failure/success of
2627 substates associated with with that op. For example, IFMATCH is the op
2628 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2629 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2630 successfully matched A and IFMATCH_A_fail is a state saying that we have
2631 just failed to match A. Resume states always come in pairs. The backtrack
2632 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2633 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2634 on success or failure.
2636 The struct that holds a backtracking state is actually a big union, with
2637 one variant for each major type of op. The variable st points to the
2638 top-most backtrack struct. To make the code clearer, within each
2639 block of code we #define ST to alias the relevant union.
2641 Here's a concrete example of a (vastly oversimplified) IFMATCH
2647 #define ST st->u.ifmatch
2649 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2650 ST.foo = ...; // some state we wish to save
2652 // push a yes backtrack state with a resume value of
2653 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2655 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2658 case IFMATCH_A: // we have successfully executed A; now continue with B
2660 bar = ST.foo; // do something with the preserved value
2663 case IFMATCH_A_fail: // A failed, so the assertion failed
2664 ...; // do some housekeeping, then ...
2665 sayNO; // propagate the failure
2672 For any old-timers reading this who are familiar with the old recursive
2673 approach, the code above is equivalent to:
2675 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2684 ...; // do some housekeeping, then ...
2685 sayNO; // propagate the failure
2688 The topmost backtrack state, pointed to by st, is usually free. If you
2689 want to claim it, populate any ST.foo fields in it with values you wish to
2690 save, then do one of
2692 PUSH_STATE_GOTO(resume_state, node);
2693 PUSH_YES_STATE_GOTO(resume_state, node);
2695 which sets that backtrack state's resume value to 'resume_state', pushes a
2696 new free entry to the top of the backtrack stack, then goes to 'node'.
2697 On backtracking, the free slot is popped, and the saved state becomes the
2698 new free state. An ST.foo field in this new top state can be temporarily
2699 accessed to retrieve values, but once the main loop is re-entered, it
2700 becomes available for reuse.
2702 Note that the depth of the backtrack stack constantly increases during the
2703 left-to-right execution of the pattern, rather than going up and down with
2704 the pattern nesting. For example the stack is at its maximum at Z at the
2705 end of the pattern, rather than at X in the following:
2707 /(((X)+)+)+....(Y)+....Z/
2709 The only exceptions to this are lookahead/behind assertions and the cut,
2710 (?>A), which pop all the backtrack states associated with A before
2713 Bascktrack state structs are allocated in slabs of about 4K in size.
2714 PL_regmatch_state and st always point to the currently active state,
2715 and PL_regmatch_slab points to the slab currently containing
2716 PL_regmatch_state. The first time regmatch() is called, the first slab is
2717 allocated, and is never freed until interpreter destruction. When the slab
2718 is full, a new one is allocated and chained to the end. At exit from
2719 regmatch(), slabs allocated since entry are freed.
2724 #define DEBUG_STATE_pp(pp) \
2726 DUMP_EXEC_POS(locinput, scan, utf8_target); \
2727 PerlIO_printf(Perl_debug_log, \
2728 " %*s"pp" %s%s%s%s%s\n", \
2730 PL_reg_name[st->resume_state], \
2731 ((st==yes_state||st==mark_state) ? "[" : ""), \
2732 ((st==yes_state) ? "Y" : ""), \
2733 ((st==mark_state) ? "M" : ""), \
2734 ((st==yes_state||st==mark_state) ? "]" : "") \
2739 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2744 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
2745 const char *start, const char *end, const char *blurb)
2747 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2749 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2754 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2755 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2757 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
2758 start, end - start, 60);
2760 PerlIO_printf(Perl_debug_log,
2761 "%s%s REx%s %s against %s\n",
2762 PL_colors[4], blurb, PL_colors[5], s0, s1);
2764 if (utf8_target||utf8_pat)
2765 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2766 utf8_pat ? "pattern" : "",
2767 utf8_pat && utf8_target ? " and " : "",
2768 utf8_target ? "string" : ""
2774 S_dump_exec_pos(pTHX_ const char *locinput,
2775 const regnode *scan,
2776 const char *loc_regeol,
2777 const char *loc_bostr,
2778 const char *loc_reg_starttry,
2779 const bool utf8_target)
2781 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2782 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2783 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2784 /* The part of the string before starttry has one color
2785 (pref0_len chars), between starttry and current
2786 position another one (pref_len - pref0_len chars),
2787 after the current position the third one.
2788 We assume that pref0_len <= pref_len, otherwise we
2789 decrease pref0_len. */
2790 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2791 ? (5 + taill) - l : locinput - loc_bostr;
2794 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2796 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2798 pref0_len = pref_len - (locinput - loc_reg_starttry);
2799 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2800 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2801 ? (5 + taill) - pref_len : loc_regeol - locinput);
2802 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2806 if (pref0_len > pref_len)
2807 pref0_len = pref_len;
2809 const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
2811 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2812 (locinput - pref_len),pref0_len, 60, 4, 5);
2814 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2815 (locinput - pref_len + pref0_len),
2816 pref_len - pref0_len, 60, 2, 3);
2818 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2819 locinput, loc_regeol - locinput, 10, 0, 1);
2821 const STRLEN tlen=len0+len1+len2;
2822 PerlIO_printf(Perl_debug_log,
2823 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2824 (IV)(locinput - loc_bostr),
2827 (docolor ? "" : "> <"),
2829 (int)(tlen > 19 ? 0 : 19 - tlen),
2836 /* reg_check_named_buff_matched()
2837 * Checks to see if a named buffer has matched. The data array of
2838 * buffer numbers corresponding to the buffer is expected to reside
2839 * in the regexp->data->data array in the slot stored in the ARG() of
2840 * node involved. Note that this routine doesn't actually care about the
2841 * name, that information is not preserved from compilation to execution.
2842 * Returns the index of the leftmost defined buffer with the given name
2843 * or 0 if non of the buffers matched.
2846 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2849 RXi_GET_DECL(rex,rexi);
2850 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2851 I32 *nums=(I32*)SvPVX(sv_dat);
2853 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2855 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2856 if ((I32)*PL_reglastparen >= nums[n] &&
2857 PL_regoffs[nums[n]].end != -1)
2866 /* free all slabs above current one - called during LEAVE_SCOPE */
2869 S_clear_backtrack_stack(pTHX_ void *p)
2871 regmatch_slab *s = PL_regmatch_slab->next;
2876 PL_regmatch_slab->next = NULL;
2878 regmatch_slab * const osl = s;
2885 #define SETREX(Re1,Re2) \
2886 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2889 STATIC I32 /* 0 failure, 1 success */
2890 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2892 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2896 register const bool utf8_target = PL_reg_match_utf8;
2897 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2898 REGEXP *rex_sv = reginfo->prog;
2899 regexp *rex = (struct regexp *)SvANY(rex_sv);
2900 RXi_GET_DECL(rex,rexi);
2902 /* the current state. This is a cached copy of PL_regmatch_state */
2903 register regmatch_state *st;
2904 /* cache heavy used fields of st in registers */
2905 register regnode *scan;
2906 register regnode *next;
2907 register U32 n = 0; /* general value; init to avoid compiler warning */
2908 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2909 register char *locinput = PL_reginput;
2910 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2912 bool result = 0; /* return value of S_regmatch */
2913 int depth = 0; /* depth of backtrack stack */
2914 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2915 const U32 max_nochange_depth =
2916 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2917 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2918 regmatch_state *yes_state = NULL; /* state to pop to on success of
2920 /* mark_state piggy backs on the yes_state logic so that when we unwind
2921 the stack on success we can update the mark_state as we go */
2922 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2923 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2924 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2926 bool no_final = 0; /* prevent failure from backtracking? */
2927 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2928 char *startpoint = PL_reginput;
2929 SV *popmark = NULL; /* are we looking for a mark? */
2930 SV *sv_commit = NULL; /* last mark name seen in failure */
2931 SV *sv_yes_mark = NULL; /* last mark name we have seen
2932 during a successfull match */
2933 U32 lastopen = 0; /* last open we saw */
2934 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2935 SV* const oreplsv = GvSV(PL_replgv);
2936 /* these three flags are set by various ops to signal information to
2937 * the very next op. They have a useful lifetime of exactly one loop
2938 * iteration, and are not preserved or restored by state pushes/pops
2940 bool sw = 0; /* the condition value in (?(cond)a|b) */
2941 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2942 int logical = 0; /* the following EVAL is:
2946 or the following IFMATCH/UNLESSM is:
2947 false: plain (?=foo)
2948 true: used as a condition: (?(?=foo))
2951 GET_RE_DEBUG_FLAGS_DECL;
2954 PERL_ARGS_ASSERT_REGMATCH;
2956 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2957 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2959 /* on first ever call to regmatch, allocate first slab */
2960 if (!PL_regmatch_slab) {
2961 Newx(PL_regmatch_slab, 1, regmatch_slab);
2962 PL_regmatch_slab->prev = NULL;
2963 PL_regmatch_slab->next = NULL;
2964 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2967 oldsave = PL_savestack_ix;
2968 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2969 SAVEVPTR(PL_regmatch_slab);
2970 SAVEVPTR(PL_regmatch_state);
2972 /* grab next free state slot */
2973 st = ++PL_regmatch_state;
2974 if (st > SLAB_LAST(PL_regmatch_slab))
2975 st = PL_regmatch_state = S_push_slab(aTHX);
2977 /* Note that nextchr is a byte even in UTF */
2978 nextchr = UCHARAT(locinput);
2980 while (scan != NULL) {
2983 SV * const prop = sv_newmortal();
2984 regnode *rnext=regnext(scan);
2985 DUMP_EXEC_POS( locinput, scan, utf8_target );
2986 regprop(rex, prop, scan);
2988 PerlIO_printf(Perl_debug_log,
2989 "%3"IVdf":%*s%s(%"IVdf")\n",
2990 (IV)(scan - rexi->program), depth*2, "",
2992 (PL_regkind[OP(scan)] == END || !rnext) ?
2993 0 : (IV)(rnext - rexi->program));
2996 next = scan + NEXT_OFF(scan);
2999 state_num = OP(scan);
3003 assert(PL_reglastparen == &rex->lastparen);
3004 assert(PL_reglastcloseparen == &rex->lastcloseparen);
3005 assert(PL_regoffs == rex->offs);
3007 switch (state_num) {
3009 if (locinput == PL_bostr)
3011 /* reginfo->till = reginfo->bol; */
3016 if (locinput == PL_bostr ||
3017 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
3023 if (locinput == PL_bostr)
3027 if (locinput == reginfo->ganch)
3032 /* update the startpoint */
3033 st->u.keeper.val = PL_regoffs[0].start;
3034 PL_reginput = locinput;
3035 PL_regoffs[0].start = locinput - PL_bostr;
3036 PUSH_STATE_GOTO(KEEPS_next, next);
3038 case KEEPS_next_fail:
3039 /* rollback the start point change */
3040 PL_regoffs[0].start = st->u.keeper.val;
3046 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3051 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3053 if (PL_regeol - locinput > 1)
3057 if (PL_regeol != locinput)
3061 if (!nextchr && locinput >= PL_regeol)
3064 locinput += PL_utf8skip[nextchr];
3065 if (locinput > PL_regeol)
3067 nextchr = UCHARAT(locinput);
3070 nextchr = UCHARAT(++locinput);
3073 if (!nextchr && locinput >= PL_regeol)
3075 nextchr = UCHARAT(++locinput);
3078 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3081 locinput += PL_utf8skip[nextchr];
3082 if (locinput > PL_regeol)
3084 nextchr = UCHARAT(locinput);
3087 nextchr = UCHARAT(++locinput);
3091 #define ST st->u.trie
3093 /* In this case the charclass data is available inline so
3094 we can fail fast without a lot of extra overhead.
3096 if (scan->flags == EXACT || !utf8_target) {
3097 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3099 PerlIO_printf(Perl_debug_log,
3100 "%*s %sfailed to match trie start class...%s\n",
3101 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3109 /* the basic plan of execution of the trie is:
3110 * At the beginning, run though all the states, and
3111 * find the longest-matching word. Also remember the position
3112 * of the shortest matching word. For example, this pattern:
3115 * when matched against the string "abcde", will generate
3116 * accept states for all words except 3, with the longest
3117 * matching word being 4, and the shortest being 1 (with
3118 * the position being after char 1 of the string).
3120 * Then for each matching word, in word order (i.e. 1,2,4,5),
3121 * we run the remainder of the pattern; on each try setting
3122 * the current position to the character following the word,
3123 * returning to try the next word on failure.
3125 * We avoid having to build a list of words at runtime by
3126 * using a compile-time structure, wordinfo[].prev, which
3127 * gives, for each word, the previous accepting word (if any).
3128 * In the case above it would contain the mappings 1->2, 2->0,
3129 * 3->0, 4->5, 5->1. We can use this table to generate, from
3130 * the longest word (4 above), a list of all words, by
3131 * following the list of prev pointers; this gives us the
3132 * unordered list 4,5,1,2. Then given the current word we have
3133 * just tried, we can go through the list and find the
3134 * next-biggest word to try (so if we just failed on word 2,
3135 * the next in the list is 4).
3137 * Since at runtime we don't record the matching position in
3138 * the string for each word, we have to work that out for
3139 * each word we're about to process. The wordinfo table holds
3140 * the character length of each word; given that we recorded
3141 * at the start: the position of the shortest word and its
3142 * length in chars, we just need to move the pointer the
3143 * difference between the two char lengths. Depending on
3144 * Unicode status and folding, that's cheap or expensive.
3146 * This algorithm is optimised for the case where are only a
3147 * small number of accept states, i.e. 0,1, or maybe 2.
3148 * With lots of accepts states, and having to try all of them,
3149 * it becomes quadratic on number of accept states to find all
3154 /* what type of TRIE am I? (utf8 makes this contextual) */
3155 DECL_TRIE_TYPE(scan);
3157 /* what trie are we using right now */
3158 reg_trie_data * const trie
3159 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3160 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3161 U32 state = trie->startstate;
3163 if (trie->bitmap && trie_type != trie_utf8_fold &&
3164 !TRIE_BITMAP_TEST(trie,*locinput)
3166 if (trie->states[ state ].wordnum) {
3168 PerlIO_printf(Perl_debug_log,
3169 "%*s %smatched empty string...%s\n",
3170 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3175 PerlIO_printf(Perl_debug_log,
3176 "%*s %sfailed to match trie start class...%s\n",
3177 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3184 U8 *uc = ( U8* )locinput;
3188 U8 *uscan = (U8*)NULL;
3189 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3190 U32 charcount = 0; /* how many input chars we have matched */
3191 U32 accepted = 0; /* have we seen any accepting states? */
3194 ST.jump = trie->jump;
3197 ST.longfold = FALSE; /* char longer if folded => it's harder */
3200 /* fully traverse the TRIE; note the position of the
3201 shortest accept state and the wordnum of the longest
3204 while ( state && uc <= (U8*)PL_regeol ) {
3205 U32 base = trie->states[ state ].trans.base;
3209 wordnum = trie->states[ state ].wordnum;
3211 if (wordnum) { /* it's an accept state */
3214 /* record first match position */
3216 ST.firstpos = (U8*)locinput;
3221 ST.firstchars = charcount;
3224 if (!ST.nextword || wordnum < ST.nextword)
3225 ST.nextword = wordnum;
3226 ST.topword = wordnum;
3229 DEBUG_TRIE_EXECUTE_r({
3230 DUMP_EXEC_POS( (char *)uc, scan, utf8_target );
3231 PerlIO_printf( Perl_debug_log,
3232 "%*s %sState: %4"UVxf" Accepted: %c ",
3233 2+depth * 2, "", PL_colors[4],
3234 (UV)state, (accepted ? 'Y' : 'N'));
3237 /* read a char and goto next state */
3240 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3241 uscan, len, uvc, charid, foldlen,
3248 base + charid - 1 - trie->uniquecharcount)) >= 0)
3250 && ((U32)offset < trie->lasttrans)
3251 && trie->trans[offset].check == state)
3253 state = trie->trans[offset].next;
3264 DEBUG_TRIE_EXECUTE_r(
3265 PerlIO_printf( Perl_debug_log,
3266 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3267 charid, uvc, (UV)state, PL_colors[5] );
3273 /* calculate total number of accept states */
3278 w = trie->wordinfo[w].prev;
3281 ST.accepted = accepted;
3285 PerlIO_printf( Perl_debug_log,
3286 "%*s %sgot %"IVdf" possible matches%s\n",
3287 REPORT_CODE_OFF + depth * 2, "",
3288 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3290 goto trie_first_try; /* jump into the fail handler */
3294 case TRIE_next_fail: /* we failed - try next alternative */
3296 REGCP_UNWIND(ST.cp);
3297 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3298 PL_regoffs[n].end = -1;
3299 *PL_reglastparen = n;
3301 if (!--ST.accepted) {
3303 PerlIO_printf( Perl_debug_log,
3304 "%*s %sTRIE failed...%s\n",
3305 REPORT_CODE_OFF+depth*2, "",
3312 /* Find next-highest word to process. Note that this code
3313 * is O(N^2) per trie run (O(N) per branch), so keep tight */
3314 register U16 min = 0;
3316 register U16 const nextword = ST.nextword;
3317 register reg_trie_wordinfo * const wordinfo
3318 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3319 for (word=ST.topword; word; word=wordinfo[word].prev) {
3320 if (word > nextword && (!min || word < min))
3333 ST.lastparen = *PL_reglastparen;
3337 /* find start char of end of current word */
3339 U32 chars; /* how many chars to skip */
3340 U8 *uc = ST.firstpos;
3341 reg_trie_data * const trie
3342 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3344 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3346 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3350 /* the hard option - fold each char in turn and find
3351 * its folded length (which may be different */
3352 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3360 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3368 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3373 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3387 PL_reginput = (char *)uc;
3390 scan = (ST.jump && ST.jump[ST.nextword])
3391 ? ST.me + ST.jump[ST.nextword]
3395 PerlIO_printf( Perl_debug_log,
3396 "%*s %sTRIE matched word #%d, continuing%s\n",
3397 REPORT_CODE_OFF+depth*2, "",
3404 if (ST.accepted > 1 || has_cutgroup) {
3405 PUSH_STATE_GOTO(TRIE_next, scan);
3408 /* only one choice left - just continue */
3410 AV *const trie_words
3411 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3412 SV ** const tmp = av_fetch( trie_words,
3414 SV *sv= tmp ? sv_newmortal() : NULL;
3416 PerlIO_printf( Perl_debug_log,
3417 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3418 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3420 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3421 PL_colors[0], PL_colors[1],
3422 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3424 : "not compiled under -Dr",
3428 locinput = PL_reginput;
3429 nextchr = UCHARAT(locinput);
3430 continue; /* execute rest of RE */
3435 char *s = STRING(scan);
3437 if (utf8_target != UTF_PATTERN) {
3438 /* The target and the pattern have differing utf8ness. */
3440 const char * const e = s + ln;
3443 /* The target is utf8, the pattern is not utf8. */
3448 if (NATIVE_TO_UNI(*(U8*)s) !=
3449 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3457 /* The target is not utf8, the pattern is utf8. */
3462 if (NATIVE_TO_UNI(*((U8*)l)) !=
3463 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3471 nextchr = UCHARAT(locinput);
3474 /* The target and the pattern have the same utf8ness. */
3475 /* Inline the first character, for speed. */
3476 if (UCHARAT(s) != nextchr)
3478 if (PL_regeol - locinput < ln)
3480 if (ln > 1 && memNE(s, locinput, ln))
3483 nextchr = UCHARAT(locinput);
3487 PL_reg_flags |= RF_tainted;
3490 char * const s = STRING(scan);
3493 if (utf8_target || UTF_PATTERN) {
3494 /* Either target or the pattern are utf8. */
3495 const char * const l = locinput;
3496 char *e = PL_regeol;
3498 if (! foldEQ_utf8(s, 0, ln, cBOOL(UTF_PATTERN),
3499 l, &e, 0, utf8_target)) {
3500 /* One more case for the sharp s:
3501 * pack("U0U*", 0xDF) =~ /ss/i,
3502 * the 0xC3 0x9F are the UTF-8
3503 * byte sequence for the U+00DF. */
3505 if (!(utf8_target &&
3506 toLOWER(s[0]) == 's' &&
3508 toLOWER(s[1]) == 's' &&
3515 nextchr = UCHARAT(locinput);
3519 /* Neither the target and the pattern are utf8. */
3521 /* Inline the first character, for speed. */
3522 if (UCHARAT(s) != nextchr &&
3523 UCHARAT(s) != ((OP(scan) == EXACTF)
3524 ? PL_fold : PL_fold_locale)[nextchr])
3526 if (PL_regeol - locinput < ln)
3528 if (ln > 1 && (OP(scan) == EXACTF
3529 ? ! foldEQ(s, locinput, ln)
3530 : ! foldEQ_locale(s, locinput, ln)))
3533 nextchr = UCHARAT(locinput);
3538 PL_reg_flags |= RF_tainted;
3542 /* was last char in word? */
3544 if (locinput == PL_bostr)
3547 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3549 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3551 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3552 ln = isALNUM_uni(ln);
3553 LOAD_UTF8_CHARCLASS_ALNUM();
3554 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, utf8_target);
3557 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3558 n = isALNUM_LC_utf8((U8*)locinput);
3562 ln = (locinput != PL_bostr) ?
3563 UCHARAT(locinput - 1) : '\n';
3564 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3566 n = isALNUM(nextchr);
3569 ln = isALNUM_LC(ln);
3570 n = isALNUM_LC(nextchr);
3573 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3574 OP(scan) == BOUNDL))
3579 STRLEN inclasslen = PL_regeol - locinput;
3581 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, utf8_target))
3583 if (locinput >= PL_regeol)
3585 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3586 nextchr = UCHARAT(locinput);
3591 nextchr = UCHARAT(locinput);
3592 if (!REGINCLASS(rex, scan, (U8*)locinput))
3594 if (!nextchr && locinput >= PL_regeol)
3596 nextchr = UCHARAT(++locinput);
3600 /* If we might have the case of the German sharp s
3601 * in a casefolding Unicode character class. */
3603 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3604 locinput += SHARP_S_SKIP;
3605 nextchr = UCHARAT(locinput);
3610 /* Special char classes - The defines start on line 129 or so */
3611 CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3612 CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3614 CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3615 CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3617 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3618 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3620 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3621 a Unicode extended Grapheme Cluster */
3622 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3623 extended Grapheme Cluster is:
3626 | Prepend* Begin Extend*
3629 Begin is (Hangul-syllable | ! Control)
3630 Extend is (Grapheme_Extend | Spacing_Mark)
3631 Control is [ GCB_Control CR LF ]
3633 The discussion below shows how the code for CLUMP is derived
3634 from this regex. Note that most of these concepts are from
3635 property values of the Grapheme Cluster Boundary (GCB) property.
3636 No code point can have multiple property values for a given
3637 property. Thus a code point in Prepend can't be in Control, but
3638 it must be in !Control. This is why Control above includes
3639 GCB_Control plus CR plus LF. The latter two are used in the GCB
3640 property separately, and so can't be in GCB_Control, even though
3641 they logically are controls. Control is not the same as gc=cc,
3642 but includes format and other characters as well.
3644 The Unicode definition of Hangul-syllable is:
3646 | (L* ( ( V | LV ) V* | LVT ) T*)
3649 Each of these is a value for the GCB property, and hence must be
3650 disjoint, so the order they are tested is immaterial, so the
3651 above can safely be changed to
3654 | (L* ( LVT | ( V | LV ) V*) T*)
3656 The last two terms can be combined like this:
3658 | (( LVT | ( V | LV ) V*) T*))
3660 And refactored into this:
3661 L* (L | LVT T* | V V* T* | LV V* T*)
3663 That means that if we have seen any L's at all we can quit
3664 there, but if the next character is a LVT, a V or and LV we
3667 There is a subtlety with Prepend* which showed up in testing.
3668 Note that the Begin, and only the Begin is required in:
3669 | Prepend* Begin Extend*
3670 Also, Begin contains '! Control'. A Prepend must be a '!
3671 Control', which means it must be a Begin. What it comes down to
3672 is that if we match Prepend* and then find no suitable Begin
3673 afterwards, that if we backtrack the last Prepend, that one will
3674 be a suitable Begin.
3677 if (locinput >= PL_regeol)
3679 if (! utf8_target) {
3681 /* Match either CR LF or '.', as all the other possibilities
3683 locinput++; /* Match the . or CR */
3685 && locinput < PL_regeol
3686 && UCHARAT(locinput) == '\n') locinput++;
3690 /* Utf8: See if is ( CR LF ); already know that locinput <
3691 * PL_regeol, so locinput+1 is in bounds */
3692 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3696 /* In case have to backtrack to beginning, then match '.' */
3697 char *starting = locinput;
3699 /* In case have to backtrack the last prepend */
3700 char *previous_prepend = 0;
3702 LOAD_UTF8_CHARCLASS_GCB();
3704 /* Match (prepend)* */
3705 while (locinput < PL_regeol
3706 && swash_fetch(PL_utf8_X_prepend,
3707 (U8*)locinput, utf8_target))
3709 previous_prepend = locinput;
3710 locinput += UTF8SKIP(locinput);
3713 /* As noted above, if we matched a prepend character, but
3714 * the next thing won't match, back off the last prepend we
3715 * matched, as it is guaranteed to match the begin */
3716 if (previous_prepend
3717 && (locinput >= PL_regeol
3718 || ! swash_fetch(PL_utf8_X_begin,
3719 (U8*)locinput, utf8_target)))
3721 locinput = previous_prepend;
3724 /* Note that here we know PL_regeol > locinput, as we
3725 * tested that upon input to this switch case, and if we
3726 * moved locinput forward, we tested the result just above
3727 * and it either passed, or we backed off so that it will
3729 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, utf8_target)) {
3731 /* Here did not match the required 'Begin' in the
3732 * second term. So just match the very first
3733 * character, the '.' of the final term of the regex */
3734 locinput = starting + UTF8SKIP(starting);
3737 /* Here is the beginning of a character that can have
3738 * an extender. It is either a hangul syllable, or a
3740 if (swash_fetch(PL_utf8_X_non_hangul,
3741 (U8*)locinput, utf8_target))
3744 /* Here not a Hangul syllable, must be a
3745 * ('! * Control') */
3746 locinput += UTF8SKIP(locinput);
3749 /* Here is a Hangul syllable. It can be composed
3750 * of several individual characters. One
3751 * possibility is T+ */
3752 if (swash_fetch(PL_utf8_X_T,
3753 (U8*)locinput, utf8_target))
3755 while (locinput < PL_regeol
3756 && swash_fetch(PL_utf8_X_T,
3757 (U8*)locinput, utf8_target))
3759 locinput += UTF8SKIP(locinput);
3763 /* Here, not T+, but is a Hangul. That means
3764 * it is one of the others: L, LV, LVT or V,
3766 * L* (L | LVT T* | V V* T* | LV V* T*) */
3769 while (locinput < PL_regeol
3770 && swash_fetch(PL_utf8_X_L,
3771 (U8*)locinput, utf8_target))
3773 locinput += UTF8SKIP(locinput);
3776 /* Here, have exhausted L*. If the next
3777 * character is not an LV, LVT nor V, it means
3778 * we had to have at least one L, so matches L+
3779 * in the original equation, we have a complete
3780 * hangul syllable. Are done. */
3782 if (locinput < PL_regeol
3783 && swash_fetch(PL_utf8_X_LV_LVT_V,
3784 (U8*)locinput, utf8_target))
3787 /* Otherwise keep going. Must be LV, LVT
3788 * or V. See if LVT */
3789 if (swash_fetch(PL_utf8_X_LVT,
3790 (U8*)locinput, utf8_target))
3792 locinput += UTF8SKIP(locinput);
3795 /* Must be V or LV. Take it, then
3797 locinput += UTF8SKIP(locinput);
3798 while (locinput < PL_regeol
3799 && swash_fetch(PL_utf8_X_V,
3800 (U8*)locinput, utf8_target))
3802 locinput += UTF8SKIP(locinput);
3806 /* And any of LV, LVT, or V can be followed
3808 while (locinput < PL_regeol
3809 && swash_fetch(PL_utf8_X_T,
3813 locinput += UTF8SKIP(locinput);
3819 /* Match any extender */
3820 while (locinput < PL_regeol
3821 && swash_fetch(PL_utf8_X_extend,
3822 (U8*)locinput, utf8_target))
3824 locinput += UTF8SKIP(locinput);
3828 if (locinput > PL_regeol) sayNO;
3830 nextchr = UCHARAT(locinput);
3837 PL_reg_flags |= RF_tainted;
3842 n = reg_check_named_buff_matched(rex,scan);
3845 type = REF + ( type - NREF );
3852 PL_reg_flags |= RF_tainted;
3856 n = ARG(scan); /* which paren pair */
3859 ln = PL_regoffs[n].start;
3860 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3861 if (*PL_reglastparen < n || ln == -1)
3862 sayNO; /* Do not match unless seen CLOSEn. */
3863 if (ln == PL_regoffs[n].end)
3867 if (utf8_target && type != REF) { /* REF can do byte comparison */
3869 const char *e = PL_bostr + PL_regoffs[n].end;
3871 * Note that we can't do the "other character" lookup trick as
3872 * in the 8-bit case (no pun intended) because in Unicode we
3873 * have to map both upper and title case to lower case.
3877 STRLEN ulen1, ulen2;
3878 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3879 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3883 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3884 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3885 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3892 nextchr = UCHARAT(locinput);
3896 /* Inline the first character, for speed. */
3897 if (UCHARAT(s) != nextchr &&
3899 (UCHARAT(s) != (type == REFF
3900 ? PL_fold : PL_fold_locale)[nextchr])))
3902 ln = PL_regoffs[n].end - ln;
3903 if (locinput + ln > PL_regeol)
3905 if (ln > 1 && (type == REF
3906 ? memNE(s, locinput, ln)
3908 ? ! foldEQ(s, locinput, ln)
3909 : ! foldEQ_locale(s, locinput, ln))))
3912 nextchr = UCHARAT(locinput);
3922 #define ST st->u.eval
3927 regexp_internal *rei;
3928 regnode *startpoint;
3931 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3932 if (cur_eval && cur_eval->locinput==locinput) {
3933 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3934 Perl_croak(aTHX_ "Infinite recursion in regex");
3935 if ( ++nochange_depth > max_nochange_depth )
3937 "Pattern subroutine nesting without pos change"
3938 " exceeded limit in regex");
3945 (void)ReREFCNT_inc(rex_sv);
3946 if (OP(scan)==GOSUB) {
3947 startpoint = scan + ARG2L(scan);
3948 ST.close_paren = ARG(scan);
3950 startpoint = rei->program+1;
3953 goto eval_recurse_doit;
3955 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3956 if (cur_eval && cur_eval->locinput==locinput) {
3957 if ( ++nochange_depth > max_nochange_depth )
3958 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3963 /* execute the code in the {...} */
3965 SV ** const before = SP;
3966 OP_4tree * const oop = PL_op;
3967 COP * const ocurcop = PL_curcop;
3969 char *saved_regeol = PL_regeol;
3972 PL_op = (OP_4tree*)rexi->data->data[n];
3973 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3974 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3975 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3976 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3979 SV *sv_mrk = get_sv("REGMARK", 1);
3980 sv_setsv(sv_mrk, sv_yes_mark);
3983 CALLRUNOPS(aTHX); /* Scalar context. */
3986 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3993 PAD_RESTORE_LOCAL(old_comppad);
3994 PL_curcop = ocurcop;
3995 PL_regeol = saved_regeol;
3998 sv_setsv(save_scalar(PL_replgv), ret);
4002 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
4005 /* extract RE object from returned value; compiling if
4011 SV *const sv = SvRV(ret);
4013 if (SvTYPE(sv) == SVt_REGEXP) {
4015 } else if (SvSMAGICAL(sv)) {
4016 mg = mg_find(sv, PERL_MAGIC_qr);
4019 } else if (SvTYPE(ret) == SVt_REGEXP) {
4021 } else if (SvSMAGICAL(ret)) {
4022 if (SvGMAGICAL(ret)) {
4023 /* I don't believe that there is ever qr magic
4025 assert(!mg_find(ret, PERL_MAGIC_qr));
4026 sv_unmagic(ret, PERL_MAGIC_qr);
4029 mg = mg_find(ret, PERL_MAGIC_qr);
4030 /* testing suggests mg only ends up non-NULL for
4031 scalars who were upgraded and compiled in the
4032 else block below. In turn, this is only
4033 triggered in the "postponed utf8 string" tests
4039 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
4043 rx = reg_temp_copy(NULL, rx);
4047 const I32 osize = PL_regsize;
4050 assert (SvUTF8(ret));
4051 } else if (SvUTF8(ret)) {
4052 /* Not doing UTF-8, despite what the SV says. Is
4053 this only if we're trapped in use 'bytes'? */
4054 /* Make a copy of the octet sequence, but without
4055 the flag on, as the compiler now honours the
4056 SvUTF8 flag on ret. */
4058 const char *const p = SvPV(ret, len);
4059 ret = newSVpvn_flags(p, len, SVs_TEMP);
4061 rx = CALLREGCOMP(ret, pm_flags);
4063 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
4065 /* This isn't a first class regexp. Instead, it's
4066 caching a regexp onto an existing, Perl visible
4068 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
4073 re = (struct regexp *)SvANY(rx);
4075 RXp_MATCH_COPIED_off(re);
4076 re->subbeg = rex->subbeg;
4077 re->sublen = rex->sublen;
4080 debug_start_match(re_sv, utf8_target, locinput, PL_regeol,
4081 "Matching embedded");
4083 startpoint = rei->program + 1;
4084 ST.close_paren = 0; /* only used for GOSUB */
4085 /* borrowed from regtry */
4086 if (PL_reg_start_tmpl <= re->nparens) {
4087 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4088 if(PL_reg_start_tmp)
4089 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4091 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4094 eval_recurse_doit: /* Share code with GOSUB below this line */
4095 /* run the pattern returned from (??{...}) */
4096 ST.cp = regcppush(0); /* Save *all* the positions. */
4097 REGCP_SET(ST.lastcp);
4099 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
4101 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4102 PL_reglastparen = &re->lastparen;
4103 PL_reglastcloseparen = &re->lastcloseparen;
4105 re->lastcloseparen = 0;
4107 PL_reginput = locinput;
4110 /* XXXX This is too dramatic a measure... */
4113 ST.toggle_reg_flags = PL_reg_flags;
4115 PL_reg_flags |= RF_utf8;
4117 PL_reg_flags &= ~RF_utf8;
4118 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4120 ST.prev_rex = rex_sv;
4121 ST.prev_curlyx = cur_curlyx;
4122 SETREX(rex_sv,re_sv);
4127 ST.prev_eval = cur_eval;
4129 /* now continue from first node in postoned RE */
4130 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4133 /* logical is 1, /(?(?{...})X|Y)/ */
4134 sw = cBOOL(SvTRUE(ret));
4139 case EVAL_AB: /* cleanup after a successful (??{A})B */
4140 /* note: this is called twice; first after popping B, then A */
4141 PL_reg_flags ^= ST.toggle_reg_flags;
4142 ReREFCNT_dec(rex_sv);
4143 SETREX(rex_sv,ST.prev_rex);
4144 rex = (struct regexp *)SvANY(rex_sv);
4145 rexi = RXi_GET(rex);
4147 cur_eval = ST.prev_eval;
4148 cur_curlyx = ST.prev_curlyx;
4150 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4151 PL_reglastparen = &rex->lastparen;
4152 PL_reglastcloseparen = &rex->lastcloseparen;
4153 /* also update PL_regoffs */
4154 PL_regoffs = rex->offs;
4156 /* XXXX This is too dramatic a measure... */
4158 if ( nochange_depth )
4163 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4164 /* note: this is called twice; first after popping B, then A */
4165 PL_reg_flags ^= ST.toggle_reg_flags;
4166 ReREFCNT_dec(rex_sv);
4167 SETREX(rex_sv,ST.prev_rex);
4168 rex = (struct regexp *)SvANY(rex_sv);
4169 rexi = RXi_GET(rex);
4170 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4171 PL_reglastparen = &rex->lastparen;
4172 PL_reglastcloseparen = &rex->lastcloseparen;
4174 PL_reginput = locinput;
4175 REGCP_UNWIND(ST.lastcp);
4177 cur_eval = ST.prev_eval;
4178 cur_curlyx = ST.prev_curlyx;
4179 /* XXXX This is too dramatic a measure... */
4181 if ( nochange_depth )
4187 n = ARG(scan); /* which paren pair */
4188 PL_reg_start_tmp[n] = locinput;
4194 n = ARG(scan); /* which paren pair */
4195 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4196 PL_regoffs[n].end = locinput - PL_bostr;
4197 /*if (n > PL_regsize)
4199 if (n > *PL_reglastparen)
4200 *PL_reglastparen = n;
4201 *PL_reglastcloseparen = n;
4202 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4210 cursor && OP(cursor)!=END;
4211 cursor=regnext(cursor))
4213 if ( OP(cursor)==CLOSE ){
4215 if ( n <= lastopen ) {
4217 = PL_reg_start_tmp[n] - PL_bostr;
4218 PL_regoffs[n].end = locinput - PL_bostr;
4219 /*if (n > PL_regsize)
4221 if (n > *PL_reglastparen)
4222 *PL_reglastparen = n;
4223 *PL_reglastcloseparen = n;
4224 if ( n == ARG(scan) || (cur_eval &&
4225 cur_eval->u.eval.close_paren == n))
4234 n = ARG(scan); /* which paren pair */
4235 sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
4238 /* reg_check_named_buff_matched returns 0 for no match */
4239 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
4243 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4249 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4251 next = NEXTOPER(NEXTOPER(scan));
4253 next = scan + ARG(scan);
4254 if (OP(next) == IFTHEN) /* Fake one. */
4255 next = NEXTOPER(NEXTOPER(next));
4259 logical = scan->flags;
4262 /*******************************************************************
4264 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4265 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4266 STAR/PLUS/CURLY/CURLYN are used instead.)
4268 A*B is compiled as <CURLYX><A><WHILEM><B>
4270 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4271 state, which contains the current count, initialised to -1. It also sets
4272 cur_curlyx to point to this state, with any previous value saved in the
4275 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4276 since the pattern may possibly match zero times (i.e. it's a while {} loop
4277 rather than a do {} while loop).
4279 Each entry to WHILEM represents a successful match of A. The count in the
4280 CURLYX block is incremented, another WHILEM state is pushed, and execution
4281 passes to A or B depending on greediness and the current count.
4283 For example, if matching against the string a1a2a3b (where the aN are
4284 substrings that match /A/), then the match progresses as follows: (the
4285 pushed states are interspersed with the bits of strings matched so far):
4288 <CURLYX cnt=0><WHILEM>
4289 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4290 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4291 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4292 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4294 (Contrast this with something like CURLYM, which maintains only a single
4298 a1 <CURLYM cnt=1> a2
4299 a1 a2 <CURLYM cnt=2> a3
4300 a1 a2 a3 <CURLYM cnt=3> b
4303 Each WHILEM state block marks a point to backtrack to upon partial failure
4304 of A or B, and also contains some minor state data related to that
4305 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4306 overall state, such as the count, and pointers to the A and B ops.
4308 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4309 must always point to the *current* CURLYX block, the rules are:
4311 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4312 and set cur_curlyx to point the new block.
4314 When popping the CURLYX block after a successful or unsuccessful match,
4315 restore the previous cur_curlyx.
4317 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4318 to the outer one saved in the CURLYX block.
4320 When popping the WHILEM block after a successful or unsuccessful B match,
4321 restore the previous cur_curlyx.
4323 Here's an example for the pattern (AI* BI)*BO
4324 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4327 curlyx backtrack stack
4328 ------ ---------------
4330 CO <CO prev=NULL> <WO>
4331 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4332 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4333 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4335 At this point the pattern succeeds, and we work back down the stack to
4336 clean up, restoring as we go:
4338 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4339 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4340 CO <CO prev=NULL> <WO>
4343 *******************************************************************/
4345 #define ST st->u.curlyx
4347 case CURLYX: /* start of /A*B/ (for complex A) */
4349 /* No need to save/restore up to this paren */
4350 I32 parenfloor = scan->flags;
4352 assert(next); /* keep Coverity happy */
4353 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4356 /* XXXX Probably it is better to teach regpush to support
4357 parenfloor > PL_regsize... */
4358 if (parenfloor > (I32)*PL_reglastparen)
4359 parenfloor = *PL_reglastparen; /* Pessimization... */
4361 ST.prev_curlyx= cur_curlyx;
4363 ST.cp = PL_savestack_ix;
4365 /* these fields contain the state of the current curly.
4366 * they are accessed by subsequent WHILEMs */
4367 ST.parenfloor = parenfloor;
4372 ST.count = -1; /* this will be updated by WHILEM */
4373 ST.lastloc = NULL; /* this will be updated by WHILEM */
4375 PL_reginput = locinput;
4376 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4380 case CURLYX_end: /* just finished matching all of A*B */
4381 cur_curlyx = ST.prev_curlyx;
4385 case CURLYX_end_fail: /* just failed to match all of A*B */
4387 cur_curlyx = ST.prev_curlyx;
4393 #define ST st->u.whilem
4395 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4397 /* see the discussion above about CURLYX/WHILEM */
4399 int min = ARG1(cur_curlyx->u.curlyx.me);
4400 int max = ARG2(cur_curlyx->u.curlyx.me);
4401 regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
4403 assert(cur_curlyx); /* keep Coverity happy */
4404 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4405 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4406 ST.cache_offset = 0;
4409 PL_reginput = locinput;
4411 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4412 "%*s whilem: matched %ld out of %d..%d\n",
4413 REPORT_CODE_OFF+depth*2, "", (long)n, min, max)
4416 /* First just match a string of min A's. */
4419 cur_curlyx->u.curlyx.lastloc = locinput;
4420 PUSH_STATE_GOTO(WHILEM_A_pre, A);
4424 /* If degenerate A matches "", assume A done. */
4426 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4427 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4428 "%*s whilem: empty match detected, trying continuation...\n",
4429 REPORT_CODE_OFF+depth*2, "")
4431 goto do_whilem_B_max;
4434 /* super-linear cache processing */
4438 if (!PL_reg_maxiter) {
4439 /* start the countdown: Postpone detection until we
4440 * know the match is not *that* much linear. */
4441 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4442 /* possible overflow for long strings and many CURLYX's */
4443 if (PL_reg_maxiter < 0)
4444 PL_reg_maxiter = I32_MAX;
4445 PL_reg_leftiter = PL_reg_maxiter;
4448 if (PL_reg_leftiter-- == 0) {
4449 /* initialise cache */
4450 const I32 size = (PL_reg_maxiter + 7)/8;
4451 if (PL_reg_poscache) {
4452 if ((I32)PL_reg_poscache_size < size) {
4453 Renew(PL_reg_poscache, size, char);
4454 PL_reg_poscache_size = size;
4456 Zero(PL_reg_poscache, size, char);
4459 PL_reg_poscache_size = size;
4460 Newxz(PL_reg_poscache, size, char);
4462 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4463 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4464 PL_colors[4], PL_colors[5])
4468 if (PL_reg_leftiter < 0) {
4469 /* have we already failed at this position? */
4471 offset = (scan->flags & 0xf) - 1
4472 + (locinput - PL_bostr) * (scan->flags>>4);
4473 mask = 1 << (offset % 8);
4475 if (PL_reg_poscache[offset] & mask) {
4476 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4477 "%*s whilem: (cache) already tried at this position...\n",
4478 REPORT_CODE_OFF+depth*2, "")
4480 sayNO; /* cache records failure */
4482 ST.cache_offset = offset;
4483 ST.cache_mask = mask;
4487 /* Prefer B over A for minimal matching. */
4489 if (cur_curlyx->u.curlyx.minmod) {
4490 ST.save_curlyx = cur_curlyx;
4491 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4492 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4493 REGCP_SET(ST.lastcp);
4494 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4498 /* Prefer A over B for maximal matching. */
4500 if (n < max) { /* More greed allowed? */
4501 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4502 cur_curlyx->u.curlyx.lastloc = locinput;
4503 REGCP_SET(ST.lastcp);
4504 PUSH_STATE_GOTO(WHILEM_A_max, A);
4507 goto do_whilem_B_max;
4511 case WHILEM_B_min: /* just matched B in a minimal match */
4512 case WHILEM_B_max: /* just matched B in a maximal match */
4513 cur_curlyx = ST.save_curlyx;
4517 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4518 cur_curlyx = ST.save_curlyx;
4519 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4520 cur_curlyx->u.curlyx.count--;
4524 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4525 REGCP_UNWIND(ST.lastcp);
4528 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4529 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4530 cur_curlyx->u.curlyx.count--;
4534 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4535 REGCP_UNWIND(ST.lastcp);
4536 regcppop(rex); /* Restore some previous $<digit>s? */
4537 PL_reginput = locinput;
4538 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4539 "%*s whilem: failed, trying continuation...\n",
4540 REPORT_CODE_OFF+depth*2, "")
4543 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4544 && ckWARN(WARN_REGEXP)
4545 && !(PL_reg_flags & RF_warned))
4547 PL_reg_flags |= RF_warned;
4548 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4549 "Complex regular subexpression recursion",
4554 ST.save_curlyx = cur_curlyx;
4555 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4556 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4559 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4560 cur_curlyx = ST.save_curlyx;
4561 REGCP_UNWIND(ST.lastcp);
4564 if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
4565 /* Maximum greed exceeded */
4566 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4567 && ckWARN(WARN_REGEXP)
4568 && !(PL_reg_flags & RF_warned))
4570 PL_reg_flags |= RF_warned;
4571 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4572 "%s limit (%d) exceeded",
4573 "Complex regular subexpression recursion",
4576 cur_curlyx->u.curlyx.count--;
4580 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4581 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4583 /* Try grabbing another A and see if it helps. */
4584 PL_reginput = locinput;
4585 cur_curlyx->u.curlyx.lastloc = locinput;
4586 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4587 REGCP_SET(ST.lastcp);
4588 PUSH_STATE_GOTO(WHILEM_A_min,
4589 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS);
4593 #define ST st->u.branch
4595 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4596 next = scan + ARG(scan);
4599 scan = NEXTOPER(scan);
4602 case BRANCH: /* /(...|A|...)/ */
4603 scan = NEXTOPER(scan); /* scan now points to inner node */
4604 ST.lastparen = *PL_reglastparen;
4605 ST.next_branch = next;
4607 PL_reginput = locinput;
4609 /* Now go into the branch */
4611 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4613 PUSH_STATE_GOTO(BRANCH_next, scan);
4617 PL_reginput = locinput;
4618 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4619 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4620 PUSH_STATE_GOTO(CUTGROUP_next,next);
4622 case CUTGROUP_next_fail:
4625 if (st->u.mark.mark_name)
4626 sv_commit = st->u.mark.mark_name;
4632 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4637 REGCP_UNWIND(ST.cp);
4638 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4639 PL_regoffs[n].end = -1;
4640 *PL_reglastparen = n;
4641 /*dmq: *PL_reglastcloseparen = n; */
4642 scan = ST.next_branch;
4643 /* no more branches? */
4644 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4646 PerlIO_printf( Perl_debug_log,
4647 "%*s %sBRANCH failed...%s\n",
4648 REPORT_CODE_OFF+depth*2, "",
4654 continue; /* execute next BRANCH[J] op */
4662 #define ST st->u.curlym
4664 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4666 /* This is an optimisation of CURLYX that enables us to push
4667 * only a single backtracking state, no matter how many matches
4668 * there are in {m,n}. It relies on the pattern being constant
4669 * length, with no parens to influence future backrefs
4673 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4675 /* if paren positive, emulate an OPEN/CLOSE around A */
4677 U32 paren = ST.me->flags;
4678 if (paren > PL_regsize)
4680 if (paren > *PL_reglastparen)
4681 *PL_reglastparen = paren;
4682 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4690 ST.c1 = CHRTEST_UNINIT;
4693 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4696 curlym_do_A: /* execute the A in /A{m,n}B/ */
4697 PL_reginput = locinput;
4698 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4701 case CURLYM_A: /* we've just matched an A */
4702 locinput = st->locinput;
4703 nextchr = UCHARAT(locinput);
4706 /* after first match, determine A's length: u.curlym.alen */
4707 if (ST.count == 1) {
4708 if (PL_reg_match_utf8) {
4710 while (s < PL_reginput) {
4716 ST.alen = PL_reginput - locinput;
4719 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4722 PerlIO_printf(Perl_debug_log,
4723 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4724 (int)(REPORT_CODE_OFF+(depth*2)), "",
4725 (IV) ST.count, (IV)ST.alen)
4728 locinput = PL_reginput;
4730 if (cur_eval && cur_eval->u.eval.close_paren &&
4731 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4735 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4736 if ( max == REG_INFTY || ST.count < max )
4737 goto curlym_do_A; /* try to match another A */
4739 goto curlym_do_B; /* try to match B */
4741 case CURLYM_A_fail: /* just failed to match an A */
4742 REGCP_UNWIND(ST.cp);
4744 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4745 || (cur_eval && cur_eval->u.eval.close_paren &&
4746 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4749 curlym_do_B: /* execute the B in /A{m,n}B/ */
4750 PL_reginput = locinput;
4751 if (ST.c1 == CHRTEST_UNINIT) {
4752 /* calculate c1 and c2 for possible match of 1st char
4753 * following curly */
4754 ST.c1 = ST.c2 = CHRTEST_VOID;
4755 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4756 regnode *text_node = ST.B;
4757 if (! HAS_TEXT(text_node))
4758 FIND_NEXT_IMPT(text_node);
4761 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4763 But the former is redundant in light of the latter.
4765 if this changes back then the macro for
4766 IS_TEXT and friends need to change.
4768 if (PL_regkind[OP(text_node)] == EXACT)
4771 ST.c1 = (U8)*STRING(text_node);
4773 (IS_TEXTF(text_node))
4775 : (IS_TEXTFL(text_node))
4776 ? PL_fold_locale[ST.c1]
4783 PerlIO_printf(Perl_debug_log,
4784 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4785 (int)(REPORT_CODE_OFF+(depth*2)),
4788 if (ST.c1 != CHRTEST_VOID
4789 && UCHARAT(PL_reginput) != ST.c1
4790 && UCHARAT(PL_reginput) != ST.c2)
4792 /* simulate B failing */
4794 PerlIO_printf(Perl_debug_log,
4795 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4796 (int)(REPORT_CODE_OFF+(depth*2)),"",
4799 state_num = CURLYM_B_fail;
4800 goto reenter_switch;
4804 /* mark current A as captured */
4805 I32 paren = ST.me->flags;
4807 PL_regoffs[paren].start
4808 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4809 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4810 /*dmq: *PL_reglastcloseparen = paren; */
4813 PL_regoffs[paren].end = -1;
4814 if (cur_eval && cur_eval->u.eval.close_paren &&
4815 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4824 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4827 case CURLYM_B_fail: /* just failed to match a B */
4828 REGCP_UNWIND(ST.cp);
4830 I32 max = ARG2(ST.me);
4831 if (max != REG_INFTY && ST.count == max)
4833 goto curlym_do_A; /* try to match a further A */
4835 /* backtrack one A */
4836 if (ST.count == ARG1(ST.me) /* min */)
4839 locinput = HOPc(locinput, -ST.alen);
4840 goto curlym_do_B; /* try to match B */
4843 #define ST st->u.curly
4845 #define CURLY_SETPAREN(paren, success) \
4848 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4849 PL_regoffs[paren].end = locinput - PL_bostr; \
4850 *PL_reglastcloseparen = paren; \
4853 PL_regoffs[paren].end = -1; \
4856 case STAR: /* /A*B/ where A is width 1 */
4860 scan = NEXTOPER(scan);
4862 case PLUS: /* /A+B/ where A is width 1 */
4866 scan = NEXTOPER(scan);
4868 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4869 ST.paren = scan->flags; /* Which paren to set */
4870 if (ST.paren > PL_regsize)
4871 PL_regsize = ST.paren;
4872 if (ST.paren > *PL_reglastparen)
4873 *PL_reglastparen = ST.paren;
4874 ST.min = ARG1(scan); /* min to match */
4875 ST.max = ARG2(scan); /* max to match */
4876 if (cur_eval && cur_eval->u.eval.close_paren &&
4877 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4881 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4883 case CURLY: /* /A{m,n}B/ where A is width 1 */
4885 ST.min = ARG1(scan); /* min to match */
4886 ST.max = ARG2(scan); /* max to match */
4887 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4890 * Lookahead to avoid useless match attempts
4891 * when we know what character comes next.
4893 * Used to only do .*x and .*?x, but now it allows
4894 * for )'s, ('s and (?{ ... })'s to be in the way
4895 * of the quantifier and the EXACT-like node. -- japhy
4898 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4900 if (HAS_TEXT(next) || JUMPABLE(next)) {
4902 regnode *text_node = next;
4904 if (! HAS_TEXT(text_node))
4905 FIND_NEXT_IMPT(text_node);
4907 if (! HAS_TEXT(text_node))
4908 ST.c1 = ST.c2 = CHRTEST_VOID;
4910 if ( PL_regkind[OP(text_node)] != EXACT ) {
4911 ST.c1 = ST.c2 = CHRTEST_VOID;
4912 goto assume_ok_easy;
4915 s = (U8*)STRING(text_node);
4917 /* Currently we only get here when
4919 PL_rekind[OP(text_node)] == EXACT
4921 if this changes back then the macro for IS_TEXT and
4922 friends need to change. */
4925 if (IS_TEXTF(text_node))
4926 ST.c2 = PL_fold[ST.c1];
4927 else if (IS_TEXTFL(text_node))
4928 ST.c2 = PL_fold_locale[ST.c1];
4930 else { /* UTF_PATTERN */
4931 if (IS_TEXTF(text_node)) {
4932 STRLEN ulen1, ulen2;
4933 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4934 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4936 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4937 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4939 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4941 0 : UTF8_ALLOW_ANY);
4942 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4944 0 : UTF8_ALLOW_ANY);
4946 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4948 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4953 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4960 ST.c1 = ST.c2 = CHRTEST_VOID;
4965 PL_reginput = locinput;
4968 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4971 locinput = PL_reginput;
4973 if (ST.c1 == CHRTEST_VOID)
4974 goto curly_try_B_min;
4976 ST.oldloc = locinput;
4978 /* set ST.maxpos to the furthest point along the
4979 * string that could possibly match */
4980 if (ST.max == REG_INFTY) {
4981 ST.maxpos = PL_regeol - 1;
4983 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4986 else if (utf8_target) {
4987 int m = ST.max - ST.min;
4988 for (ST.maxpos = locinput;
4989 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4990 ST.maxpos += UTF8SKIP(ST.maxpos);
4993 ST.maxpos = locinput + ST.max - ST.min;
4994 if (ST.maxpos >= PL_regeol)
4995 ST.maxpos = PL_regeol - 1;
4997 goto curly_try_B_min_known;
5001 ST.count = regrepeat(rex, ST.A, ST.max, depth);
5002 locinput = PL_reginput;
5003 if (ST.count < ST.min)
5005 if ((ST.count > ST.min)
5006 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
5008 /* A{m,n} must come at the end of the string, there's
5009 * no point in backing off ... */
5011 /* ...except that $ and \Z can match before *and* after
5012 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
5013 We may back off by one in this case. */
5014 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
5018 goto curly_try_B_max;
5023 case CURLY_B_min_known_fail:
5024 /* failed to find B in a non-greedy match where c1,c2 valid */
5025 if (ST.paren && ST.count)
5026 PL_regoffs[ST.paren].end = -1;
5028 PL_reginput = locinput; /* Could be reset... */
5029 REGCP_UNWIND(ST.cp);
5030 /* Couldn't or didn't -- move forward. */
5031 ST.oldloc = locinput;
5033 locinput += UTF8SKIP(locinput);
5037 curly_try_B_min_known:
5038 /* find the next place where 'B' could work, then call B */
5042 n = (ST.oldloc == locinput) ? 0 : 1;
5043 if (ST.c1 == ST.c2) {
5045 /* set n to utf8_distance(oldloc, locinput) */
5046 while (locinput <= ST.maxpos &&
5047 utf8n_to_uvchr((U8*)locinput,
5048 UTF8_MAXBYTES, &len,
5049 uniflags) != (UV)ST.c1) {
5055 /* set n to utf8_distance(oldloc, locinput) */
5056 while (locinput <= ST.maxpos) {
5058 const UV c = utf8n_to_uvchr((U8*)locinput,
5059 UTF8_MAXBYTES, &len,
5061 if (c == (UV)ST.c1 || c == (UV)ST.c2)
5069 if (ST.c1 == ST.c2) {
5070 while (locinput <= ST.maxpos &&
5071 UCHARAT(locinput) != ST.c1)
5075 while (locinput <= ST.maxpos
5076 && UCHARAT(locinput) != ST.c1
5077 && UCHARAT(locinput) != ST.c2)
5080 n = locinput - ST.oldloc;
5082 if (locinput > ST.maxpos)
5084 /* PL_reginput == oldloc now */
5087 if (regrepeat(rex, ST.A, n, depth) < n)
5090 PL_reginput = locinput;
5091 CURLY_SETPAREN(ST.paren, ST.count);
5092 if (cur_eval && cur_eval->u.eval.close_paren &&
5093 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5096 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5101 case CURLY_B_min_fail:
5102 /* failed to find B in a non-greedy match where c1,c2 invalid */
5103 if (ST.paren && ST.count)
5104 PL_regoffs[ST.paren].end = -1;
5106 REGCP_UNWIND(ST.cp);
5107 /* failed -- move forward one */
5108 PL_reginput = locinput;
5109 if (regrepeat(rex, ST.A, 1, depth)) {
5111 locinput = PL_reginput;
5112 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5113 ST.count > 0)) /* count overflow ? */
5116 CURLY_SETPAREN(ST.paren, ST.count);
5117 if (cur_eval && cur_eval->u.eval.close_paren &&
5118 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5121 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5129 /* a successful greedy match: now try to match B */
5130 if (cur_eval && cur_eval->u.eval.close_paren &&
5131 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5136 if (ST.c1 != CHRTEST_VOID)
5137 c = utf8_target ? utf8n_to_uvchr((U8*)PL_reginput,
5138 UTF8_MAXBYTES, 0, uniflags)
5139 : (UV) UCHARAT(PL_reginput);
5140 /* If it could work, try it. */
5141 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5142 CURLY_SETPAREN(ST.paren, ST.count);
5143 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5148 case CURLY_B_max_fail:
5149 /* failed to find B in a greedy match */
5150 if (ST.paren && ST.count)
5151 PL_regoffs[ST.paren].end = -1;
5153 REGCP_UNWIND(ST.cp);
5155 if (--ST.count < ST.min)
5157 PL_reginput = locinput = HOPc(locinput, -1);
5158 goto curly_try_B_max;
5165 /* we've just finished A in /(??{A})B/; now continue with B */
5167 st->u.eval.toggle_reg_flags
5168 = cur_eval->u.eval.toggle_reg_flags;
5169 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5171 st->u.eval.prev_rex = rex_sv; /* inner */
5172 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5173 rex = (struct regexp *)SvANY(rex_sv);
5174 rexi = RXi_GET(rex);
5175 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5176 ReREFCNT_inc(rex_sv);
5177 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
5179 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5180 PL_reglastparen = &rex->lastparen;
5181 PL_reglastcloseparen = &rex->lastcloseparen;
5183 REGCP_SET(st->u.eval.lastcp);
5184 PL_reginput = locinput;
5186 /* Restore parens of the outer rex without popping the
5188 tmpix = PL_savestack_ix;
5189 PL_savestack_ix = cur_eval->u.eval.lastcp;
5191 PL_savestack_ix = tmpix;
5193 st->u.eval.prev_eval = cur_eval;
5194 cur_eval = cur_eval->u.eval.prev_eval;
5196 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5197 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5198 if ( nochange_depth )
5201 PUSH_YES_STATE_GOTO(EVAL_AB,
5202 st->u.eval.prev_eval->u.eval.B); /* match B */
5205 if (locinput < reginfo->till) {
5206 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5207 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5209 (long)(locinput - PL_reg_starttry),
5210 (long)(reginfo->till - PL_reg_starttry),
5213 sayNO_SILENT; /* Cannot match: too short. */
5215 PL_reginput = locinput; /* put where regtry can find it */
5216 sayYES; /* Success! */
5218 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5220 PerlIO_printf(Perl_debug_log,
5221 "%*s %ssubpattern success...%s\n",
5222 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5223 PL_reginput = locinput; /* put where regtry can find it */
5224 sayYES; /* Success! */
5227 #define ST st->u.ifmatch
5229 case SUSPEND: /* (?>A) */
5231 PL_reginput = locinput;
5234 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5236 goto ifmatch_trivial_fail_test;
5238 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5240 ifmatch_trivial_fail_test:
5242 char * const s = HOPBACKc(locinput, scan->flags);
5247 sw = 1 - cBOOL(ST.wanted);
5251 next = scan + ARG(scan);
5259 PL_reginput = locinput;
5263 ST.logical = logical;
5264 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5266 /* execute body of (?...A) */
5267 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5270 case IFMATCH_A_fail: /* body of (?...A) failed */
5271 ST.wanted = !ST.wanted;
5274 case IFMATCH_A: /* body of (?...A) succeeded */
5276 sw = cBOOL(ST.wanted);
5278 else if (!ST.wanted)
5281 if (OP(ST.me) == SUSPEND)
5282 locinput = PL_reginput;
5284 locinput = PL_reginput = st->locinput;
5285 nextchr = UCHARAT(locinput);
5287 scan = ST.me + ARG(ST.me);
5290 continue; /* execute B */
5295 next = scan + ARG(scan);
5300 reginfo->cutpoint = PL_regeol;
5303 PL_reginput = locinput;
5305 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5306 PUSH_STATE_GOTO(COMMIT_next,next);
5308 case COMMIT_next_fail:
5315 #define ST st->u.mark
5317 ST.prev_mark = mark_state;
5318 ST.mark_name = sv_commit = sv_yes_mark
5319 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5321 ST.mark_loc = PL_reginput = locinput;
5322 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5324 case MARKPOINT_next:
5325 mark_state = ST.prev_mark;
5328 case MARKPOINT_next_fail:
5329 if (popmark && sv_eq(ST.mark_name,popmark))
5331 if (ST.mark_loc > startpoint)
5332 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5333 popmark = NULL; /* we found our mark */
5334 sv_commit = ST.mark_name;
5337 PerlIO_printf(Perl_debug_log,
5338 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5339 REPORT_CODE_OFF+depth*2, "",
5340 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5343 mark_state = ST.prev_mark;
5344 sv_yes_mark = mark_state ?
5345 mark_state->u.mark.mark_name : NULL;
5349 PL_reginput = locinput;
5351 /* (*SKIP) : if we fail we cut here*/
5352 ST.mark_name = NULL;
5353 ST.mark_loc = locinput;
5354 PUSH_STATE_GOTO(SKIP_next,next);
5356 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5357 otherwise do nothing. Meaning we need to scan
5359 regmatch_state *cur = mark_state;
5360 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5363 if ( sv_eq( cur->u.mark.mark_name,
5366 ST.mark_name = find;
5367 PUSH_STATE_GOTO( SKIP_next, next );
5369 cur = cur->u.mark.prev_mark;
5372 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5374 case SKIP_next_fail:
5376 /* (*CUT:NAME) - Set up to search for the name as we
5377 collapse the stack*/
5378 popmark = ST.mark_name;
5380 /* (*CUT) - No name, we cut here.*/
5381 if (ST.mark_loc > startpoint)
5382 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5383 /* but we set sv_commit to latest mark_name if there
5384 is one so they can test to see how things lead to this
5387 sv_commit=mark_state->u.mark.mark_name;
5395 if ( n == (U32)what_len_TRICKYFOLD(locinput,utf8_target,ln) ) {
5397 } else if ( 0xDF == n && !utf8_target && !UTF_PATTERN ) {
5400 U8 folded[UTF8_MAXBYTES_CASE+1];
5402 const char * const l = locinput;
5403 char *e = PL_regeol;
5404 to_uni_fold(n, folded, &foldlen);
5406 if (! foldEQ_utf8((const char*) folded, 0, foldlen, 1,
5407 l, &e, 0, utf8_target)) {
5412 nextchr = UCHARAT(locinput);
5415 if ((n=is_LNBREAK(locinput,utf8_target))) {
5417 nextchr = UCHARAT(locinput);
5422 #define CASE_CLASS(nAmE) \
5424 if ((n=is_##nAmE(locinput,utf8_target))) { \
5426 nextchr = UCHARAT(locinput); \
5431 if ((n=is_##nAmE(locinput,utf8_target))) { \
5434 locinput += UTF8SKIP(locinput); \
5435 nextchr = UCHARAT(locinput); \
5440 CASE_CLASS(HORIZWS);
5444 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5445 PTR2UV(scan), OP(scan));
5446 Perl_croak(aTHX_ "regexp memory corruption");
5450 /* switch break jumps here */
5451 scan = next; /* prepare to execute the next op and ... */
5452 continue; /* ... jump back to the top, reusing st */
5456 /* push a state that backtracks on success */
5457 st->u.yes.prev_yes_state = yes_state;
5461 /* push a new regex state, then continue at scan */
5463 regmatch_state *newst;
5466 regmatch_state *cur = st;
5467 regmatch_state *curyes = yes_state;
5469 regmatch_slab *slab = PL_regmatch_slab;
5470 for (;curd > -1;cur--,curd--) {
5471 if (cur < SLAB_FIRST(slab)) {
5473 cur = SLAB_LAST(slab);
5475 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5476 REPORT_CODE_OFF + 2 + depth * 2,"",
5477 curd, PL_reg_name[cur->resume_state],
5478 (curyes == cur) ? "yes" : ""
5481 curyes = cur->u.yes.prev_yes_state;
5484 DEBUG_STATE_pp("push")
5487 st->locinput = locinput;
5489 if (newst > SLAB_LAST(PL_regmatch_slab))
5490 newst = S_push_slab(aTHX);
5491 PL_regmatch_state = newst;
5493 locinput = PL_reginput;
5494 nextchr = UCHARAT(locinput);
5502 * We get here only if there's trouble -- normally "case END" is
5503 * the terminating point.
5505 Perl_croak(aTHX_ "corrupted regexp pointers");
5511 /* we have successfully completed a subexpression, but we must now
5512 * pop to the state marked by yes_state and continue from there */
5513 assert(st != yes_state);
5515 while (st != yes_state) {
5517 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5518 PL_regmatch_slab = PL_regmatch_slab->prev;
5519 st = SLAB_LAST(PL_regmatch_slab);
5523 DEBUG_STATE_pp("pop (no final)");
5525 DEBUG_STATE_pp("pop (yes)");
5531 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5532 || yes_state > SLAB_LAST(PL_regmatch_slab))
5534 /* not in this slab, pop slab */
5535 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5536 PL_regmatch_slab = PL_regmatch_slab->prev;
5537 st = SLAB_LAST(PL_regmatch_slab);
5539 depth -= (st - yes_state);
5542 yes_state = st->u.yes.prev_yes_state;
5543 PL_regmatch_state = st;
5546 locinput= st->locinput;
5547 nextchr = UCHARAT(locinput);
5549 state_num = st->resume_state + no_final;
5550 goto reenter_switch;
5553 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5554 PL_colors[4], PL_colors[5]));
5556 if (PL_reg_eval_set) {
5557 /* each successfully executed (?{...}) block does the equivalent of
5558 * local $^R = do {...}
5559 * When popping the save stack, all these locals would be undone;
5560 * bypass this by setting the outermost saved $^R to the latest
5562 if (oreplsv != GvSV(PL_replgv))
5563 sv_setsv(oreplsv, GvSV(PL_replgv));
5570 PerlIO_printf(Perl_debug_log,
5571 "%*s %sfailed...%s\n",
5572 REPORT_CODE_OFF+depth*2, "",
5573 PL_colors[4], PL_colors[5])
5585 /* there's a previous state to backtrack to */
5587 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5588 PL_regmatch_slab = PL_regmatch_slab->prev;
5589 st = SLAB_LAST(PL_regmatch_slab);
5591 PL_regmatch_state = st;
5592 locinput= st->locinput;
5593 nextchr = UCHARAT(locinput);
5595 DEBUG_STATE_pp("pop");
5597 if (yes_state == st)
5598 yes_state = st->u.yes.prev_yes_state;
5600 state_num = st->resume_state + 1; /* failure = success + 1 */
5601 goto reenter_switch;
5606 if (rex->intflags & PREGf_VERBARG_SEEN) {
5607 SV *sv_err = get_sv("REGERROR", 1);
5608 SV *sv_mrk = get_sv("REGMARK", 1);
5610 sv_commit = &PL_sv_no;
5612 sv_yes_mark = &PL_sv_yes;
5615 sv_commit = &PL_sv_yes;
5616 sv_yes_mark = &PL_sv_no;
5618 sv_setsv(sv_err, sv_commit);
5619 sv_setsv(sv_mrk, sv_yes_mark);
5622 /* clean up; in particular, free all slabs above current one */
5623 LEAVE_SCOPE(oldsave);
5629 - regrepeat - repeatedly match something simple, report how many
5632 * [This routine now assumes that it will only match on things of length 1.
5633 * That was true before, but now we assume scan - reginput is the count,
5634 * rather than incrementing count on every character. [Er, except utf8.]]
5637 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5640 register char *scan;
5642 register char *loceol = PL_regeol;
5643 register I32 hardcount = 0;
5644 register bool utf8_target = PL_reg_match_utf8;
5646 PERL_UNUSED_ARG(depth);
5649 PERL_ARGS_ASSERT_REGREPEAT;
5652 if (max == REG_INFTY)
5654 else if (max < loceol - scan)
5655 loceol = scan + max;
5660 while (scan < loceol && hardcount < max && *scan != '\n') {
5661 scan += UTF8SKIP(scan);
5665 while (scan < loceol && *scan != '\n')
5672 while (scan < loceol && hardcount < max) {
5673 scan += UTF8SKIP(scan);
5683 case EXACT: /* length of string is 1 */
5685 while (scan < loceol && UCHARAT(scan) == c)
5688 case EXACTF: /* length of string is 1 */
5690 while (scan < loceol &&
5691 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5694 case EXACTFL: /* length of string is 1 */
5695 PL_reg_flags |= RF_tainted;
5697 while (scan < loceol &&
5698 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5704 while (hardcount < max && scan < loceol &&
5705 reginclass(prog, p, (U8*)scan, 0, utf8_target)) {
5706 scan += UTF8SKIP(scan);
5710 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5717 LOAD_UTF8_CHARCLASS_ALNUM();
5718 while (hardcount < max && scan < loceol &&
5719 swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target)) {
5720 scan += UTF8SKIP(scan);
5724 while (scan < loceol && isALNUM(*scan))
5729 PL_reg_flags |= RF_tainted;
5732 while (hardcount < max && scan < loceol &&
5733 isALNUM_LC_utf8((U8*)scan)) {
5734 scan += UTF8SKIP(scan);
5738 while (scan < loceol && isALNUM_LC(*scan))
5745 LOAD_UTF8_CHARCLASS_ALNUM();
5746 while (hardcount < max && scan < loceol &&
5747 !swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target)) {
5748 scan += UTF8SKIP(scan);
5752 while (scan < loceol && !isALNUM(*scan))
5757 PL_reg_flags |= RF_tainted;
5760 while (hardcount < max && scan < loceol &&
5761 !isALNUM_LC_utf8((U8*)scan)) {
5762 scan += UTF8SKIP(scan);
5766 while (scan < loceol && !isALNUM_LC(*scan))
5773 LOAD_UTF8_CHARCLASS_SPACE();
5774 while (hardcount < max && scan < loceol &&
5776 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target))) {
5777 scan += UTF8SKIP(scan);
5781 while (scan < loceol && isSPACE(*scan))
5786 PL_reg_flags |= RF_tainted;
5789 while (hardcount < max && scan < loceol &&
5790 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5791 scan += UTF8SKIP(scan);
5795 while (scan < loceol && isSPACE_LC(*scan))
5802 LOAD_UTF8_CHARCLASS_SPACE();
5803 while (hardcount < max && scan < loceol &&
5805 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target))) {
5806 scan += UTF8SKIP(scan);
5810 while (scan < loceol && !isSPACE(*scan))
5815 PL_reg_flags |= RF_tainted;
5818 while (hardcount < max && scan < loceol &&
5819 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5820 scan += UTF8SKIP(scan);
5824 while (scan < loceol && !isSPACE_LC(*scan))
5831 LOAD_UTF8_CHARCLASS_DIGIT();
5832 while (hardcount < max && scan < loceol &&
5833 swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
5834 scan += UTF8SKIP(scan);
5838 while (scan < loceol && isDIGIT(*scan))
5845 LOAD_UTF8_CHARCLASS_DIGIT();
5846 while (hardcount < max && scan < loceol &&
5847 !swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
5848 scan += UTF8SKIP(scan);
5852 while (scan < loceol && !isDIGIT(*scan))
5858 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5864 LNBREAK can match two latin chars, which is ok,
5865 because we have a null terminated string, but we
5866 have to use hardcount in this situation
5868 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5877 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5882 while (scan < loceol && is_HORIZWS_latin1(scan))
5889 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5890 scan += UTF8SKIP(scan);
5894 while (scan < loceol && !is_HORIZWS_latin1(scan))
5902 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5907 while (scan < loceol && is_VERTWS_latin1(scan))
5915 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5916 scan += UTF8SKIP(scan);
5920 while (scan < loceol && !is_VERTWS_latin1(scan))
5926 default: /* Called on something of 0 width. */
5927 break; /* So match right here or not at all. */
5933 c = scan - PL_reginput;
5937 GET_RE_DEBUG_FLAGS_DECL;
5939 SV * const prop = sv_newmortal();
5940 regprop(prog, prop, p);
5941 PerlIO_printf(Perl_debug_log,
5942 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5943 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5951 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5953 - regclass_swash - prepare the utf8 swash
5957 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5963 RXi_GET_DECL(prog,progi);
5964 const struct reg_data * const data = prog ? progi->data : NULL;
5966 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5968 if (data && data->count) {
5969 const U32 n = ARG(node);
5971 if (data->what[n] == 's') {
5972 SV * const rv = MUTABLE_SV(data->data[n]);
5973 AV * const av = MUTABLE_AV(SvRV(rv));
5974 SV **const ary = AvARRAY(av);
5977 /* See the end of regcomp.c:S_regclass() for
5978 * documentation of these array elements. */
5981 a = SvROK(ary[1]) ? &ary[1] : NULL;
5982 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5986 else if (si && doinit) {
5987 sw = swash_init("utf8", "", si, 1, 0);
5988 (void)av_store(av, 1, sw);
6005 - reginclass - determine if a character falls into a character class
6007 The n is the ANYOF regnode, the p is the target string, lenp
6008 is pointer to the maximum length of how far to go in the p
6009 (if the lenp is zero, UTF8SKIP(p) is used),
6010 utf8_target tells whether the target string is in UTF-8.
6015 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool utf8_target)
6018 const char flags = ANYOF_FLAGS(n);
6024 PERL_ARGS_ASSERT_REGINCLASS;
6026 if (utf8_target && !UTF8_IS_INVARIANT(c)) {
6027 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
6028 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
6029 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
6030 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
6031 * UTF8_ALLOW_FFFF */
6032 if (len == (STRLEN)-1)
6033 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
6036 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
6037 if (utf8_target || (flags & ANYOF_UNICODE)) {
6040 if (utf8_target && !ANYOF_RUNTIME(n)) {
6041 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
6044 if (!match && utf8_target && (flags & ANYOF_UNICODE_ALL) && c >= 256)
6048 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
6056 utf8_p = bytes_to_utf8(p, &len);
6058 if (swash_fetch(sw, utf8_p, 1))
6060 else if (flags & ANYOF_FOLD) {
6061 if (!match && lenp && av) {
6063 for (i = 0; i <= av_len(av); i++) {
6064 SV* const sv = *av_fetch(av, i, FALSE);
6066 const char * const s = SvPV_const(sv, len);
6067 if (len <= plen && memEQ(s, (char*)utf8_p, len)) {
6075 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
6078 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
6079 if (swash_fetch(sw, tmpbuf, 1))
6084 /* If we allocated a string above, free it */
6085 if (! utf8_target) Safefree(utf8_p);
6088 if (match && lenp && *lenp == 0)
6089 *lenp = UNISKIP(NATIVE_TO_UNI(c));
6091 if (!match && c < 256) {
6092 if (ANYOF_BITMAP_TEST(n, c))
6094 else if (flags & ANYOF_FOLD) {
6097 if (flags & ANYOF_LOCALE) {
6098 PL_reg_flags |= RF_tainted;
6099 f = PL_fold_locale[c];
6103 if (f != c && ANYOF_BITMAP_TEST(n, f))
6107 if (!match && (flags & ANYOF_CLASS)) {
6108 PL_reg_flags |= RF_tainted;
6110 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6111 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6112 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6113 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6114 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6115 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6116 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6117 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6118 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6119 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6120 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6121 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6122 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6123 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6124 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6125 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6126 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6127 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6128 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6129 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6130 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6131 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6132 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6133 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6134 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6135 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6136 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6137 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6138 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6139 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6140 ) /* How's that for a conditional? */
6147 return (flags & ANYOF_INVERT) ? !match : match;
6151 S_reghop3(U8 *s, I32 off, const U8* lim)
6155 PERL_ARGS_ASSERT_REGHOP3;
6158 while (off-- && s < lim) {
6159 /* XXX could check well-formedness here */
6164 while (off++ && s > lim) {
6166 if (UTF8_IS_CONTINUED(*s)) {
6167 while (s > lim && UTF8_IS_CONTINUATION(*s))
6170 /* XXX could check well-formedness here */
6177 /* there are a bunch of places where we use two reghop3's that should
6178 be replaced with this routine. but since thats not done yet
6179 we ifdef it out - dmq
6182 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6186 PERL_ARGS_ASSERT_REGHOP4;
6189 while (off-- && s < rlim) {
6190 /* XXX could check well-formedness here */
6195 while (off++ && s > llim) {
6197 if (UTF8_IS_CONTINUED(*s)) {
6198 while (s > llim && UTF8_IS_CONTINUATION(*s))
6201 /* XXX could check well-formedness here */
6209 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
6213 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6216 while (off-- && s < lim) {
6217 /* XXX could check well-formedness here */
6224 while (off++ && s > lim) {
6226 if (UTF8_IS_CONTINUED(*s)) {
6227 while (s > lim && UTF8_IS_CONTINUATION(*s))
6230 /* XXX could check well-formedness here */
6239 restore_pos(pTHX_ void *arg)
6242 regexp * const rex = (regexp *)arg;
6243 if (PL_reg_eval_set) {
6244 if (PL_reg_oldsaved) {
6245 rex->subbeg = PL_reg_oldsaved;
6246 rex->sublen = PL_reg_oldsavedlen;
6247 #ifdef PERL_OLD_COPY_ON_WRITE
6248 rex->saved_copy = PL_nrs;
6250 RXp_MATCH_COPIED_on(rex);
6252 PL_reg_magic->mg_len = PL_reg_oldpos;
6253 PL_reg_eval_set = 0;
6254 PL_curpm = PL_reg_oldcurpm;
6259 S_to_utf8_substr(pTHX_ register regexp *prog)
6263 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6266 if (prog->substrs->data[i].substr
6267 && !prog->substrs->data[i].utf8_substr) {
6268 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6269 prog->substrs->data[i].utf8_substr = sv;
6270 sv_utf8_upgrade(sv);
6271 if (SvVALID(prog->substrs->data[i].substr)) {
6272 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
6273 if (flags & FBMcf_TAIL) {
6274 /* Trim the trailing \n that fbm_compile added last
6276 SvCUR_set(sv, SvCUR(sv) - 1);
6277 /* Whilst this makes the SV technically "invalid" (as its
6278 buffer is no longer followed by "\0") when fbm_compile()
6279 adds the "\n" back, a "\0" is restored. */
6281 fbm_compile(sv, flags);
6283 if (prog->substrs->data[i].substr == prog->check_substr)
6284 prog->check_utf8 = sv;
6290 S_to_byte_substr(pTHX_ register regexp *prog)
6295 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6298 if (prog->substrs->data[i].utf8_substr
6299 && !prog->substrs->data[i].substr) {
6300 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6301 if (sv_utf8_downgrade(sv, TRUE)) {
6302 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6304 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6305 if (flags & FBMcf_TAIL) {
6306 /* Trim the trailing \n that fbm_compile added last
6308 SvCUR_set(sv, SvCUR(sv) - 1);
6310 fbm_compile(sv, flags);
6316 prog->substrs->data[i].substr = sv;
6317 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6318 prog->check_substr = sv;
6325 * c-indentation-style: bsd
6327 * indent-tabs-mode: t
6330 * ex: set ts=8 sts=4 sw=4 noet: