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 ((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) (do_utf8 ? 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 (do_utf8 && 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 ? (bool)swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8) \
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 (do_utf8 && 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 ? (bool)swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8) \
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);
298 S_regcppush(pTHX_ I32 parenfloor)
301 const int retval = PL_savestack_ix;
302 #define REGCP_PAREN_ELEMS 4
303 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
305 GET_RE_DEBUG_FLAGS_DECL;
307 if (paren_elems_to_push < 0)
308 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
310 #define REGCP_OTHER_ELEMS 7
311 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
313 for (p = PL_regsize; p > parenfloor; p--) {
314 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
315 SSPUSHINT(PL_regoffs[p].end);
316 SSPUSHINT(PL_regoffs[p].start);
317 SSPUSHPTR(PL_reg_start_tmp[p]);
319 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
320 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
321 (UV)p, (IV)PL_regoffs[p].start,
322 (IV)(PL_reg_start_tmp[p] - PL_bostr),
323 (IV)PL_regoffs[p].end
326 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
327 SSPUSHPTR(PL_regoffs);
328 SSPUSHINT(PL_regsize);
329 SSPUSHINT(*PL_reglastparen);
330 SSPUSHINT(*PL_reglastcloseparen);
331 SSPUSHPTR(PL_reginput);
332 #define REGCP_FRAME_ELEMS 2
333 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
334 * are needed for the regexp context stack bookkeeping. */
335 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
336 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
341 /* These are needed since we do not localize EVAL nodes: */
342 #define REGCP_SET(cp) \
344 PerlIO_printf(Perl_debug_log, \
345 " Setting an EVAL scope, savestack=%"IVdf"\n", \
346 (IV)PL_savestack_ix)); \
349 #define REGCP_UNWIND(cp) \
351 if (cp != PL_savestack_ix) \
352 PerlIO_printf(Perl_debug_log, \
353 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
354 (IV)(cp), (IV)PL_savestack_ix)); \
358 S_regcppop(pTHX_ const regexp *rex)
363 GET_RE_DEBUG_FLAGS_DECL;
365 PERL_ARGS_ASSERT_REGCPPOP;
367 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
369 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
370 i = SSPOPINT; /* Parentheses elements to pop. */
371 input = (char *) SSPOPPTR;
372 *PL_reglastcloseparen = SSPOPINT;
373 *PL_reglastparen = SSPOPINT;
374 PL_regsize = SSPOPINT;
375 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
378 /* Now restore the parentheses context. */
379 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
380 i > 0; i -= REGCP_PAREN_ELEMS) {
382 U32 paren = (U32)SSPOPINT;
383 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
384 PL_regoffs[paren].start = SSPOPINT;
386 if (paren <= *PL_reglastparen)
387 PL_regoffs[paren].end = tmps;
389 PerlIO_printf(Perl_debug_log,
390 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
391 (UV)paren, (IV)PL_regoffs[paren].start,
392 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
393 (IV)PL_regoffs[paren].end,
394 (paren > *PL_reglastparen ? "(no)" : ""));
398 if (*PL_reglastparen + 1 <= rex->nparens) {
399 PerlIO_printf(Perl_debug_log,
400 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
401 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
405 /* It would seem that the similar code in regtry()
406 * already takes care of this, and in fact it is in
407 * a better location to since this code can #if 0-ed out
408 * but the code in regtry() is needed or otherwise tests
409 * requiring null fields (pat.t#187 and split.t#{13,14}
410 * (as of patchlevel 7877) will fail. Then again,
411 * this code seems to be necessary or otherwise
412 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
413 * --jhi updated by dapm */
414 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
416 PL_regoffs[i].start = -1;
417 PL_regoffs[i].end = -1;
423 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
426 * pregexec and friends
429 #ifndef PERL_IN_XSUB_RE
431 - pregexec - match a regexp against a string
434 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
435 char *strbeg, I32 minend, SV *screamer, U32 nosave)
436 /* strend: pointer to null at end of string */
437 /* strbeg: real beginning of string */
438 /* minend: end of match must be >=minend after stringarg. */
439 /* nosave: For optimizations. */
441 PERL_ARGS_ASSERT_PREGEXEC;
444 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
445 nosave ? 0 : REXEC_COPY_STR);
450 * Need to implement the following flags for reg_anch:
452 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
454 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
455 * INTUIT_AUTORITATIVE_ML
456 * INTUIT_ONCE_NOML - Intuit can match in one location only.
459 * Another flag for this function: SECOND_TIME (so that float substrs
460 * with giant delta may be not rechecked).
463 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
465 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
466 Otherwise, only SvCUR(sv) is used to get strbeg. */
468 /* XXXX We assume that strpos is strbeg unless sv. */
470 /* XXXX Some places assume that there is a fixed substring.
471 An update may be needed if optimizer marks as "INTUITable"
472 RExen without fixed substrings. Similarly, it is assumed that
473 lengths of all the strings are no more than minlen, thus they
474 cannot come from lookahead.
475 (Or minlen should take into account lookahead.)
476 NOTE: Some of this comment is not correct. minlen does now take account
477 of lookahead/behind. Further research is required. -- demerphq
481 /* A failure to find a constant substring means that there is no need to make
482 an expensive call to REx engine, thus we celebrate a failure. Similarly,
483 finding a substring too deep into the string means that less calls to
484 regtry() should be needed.
486 REx compiler's optimizer found 4 possible hints:
487 a) Anchored substring;
489 c) Whether we are anchored (beginning-of-line or \G);
490 d) First node (of those at offset 0) which may distingush positions;
491 We use a)b)d) and multiline-part of c), and try to find a position in the
492 string which does not contradict any of them.
495 /* Most of decisions we do here should have been done at compile time.
496 The nodes of the REx which we used for the search should have been
497 deleted from the finite automaton. */
500 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
501 char *strend, const U32 flags, re_scream_pos_data *data)
504 struct regexp *const prog = (struct regexp *)SvANY(rx);
505 register I32 start_shift = 0;
506 /* Should be nonnegative! */
507 register I32 end_shift = 0;
512 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
514 register char *other_last = NULL; /* other substr checked before this */
515 char *check_at = NULL; /* check substr found at this pos */
516 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
517 RXi_GET_DECL(prog,progi);
519 const char * const i_strpos = strpos;
521 GET_RE_DEBUG_FLAGS_DECL;
523 PERL_ARGS_ASSERT_RE_INTUIT_START;
525 RX_MATCH_UTF8_set(rx,do_utf8);
528 PL_reg_flags |= RF_utf8;
531 debug_start_match(rx, do_utf8, strpos, strend,
532 sv ? "Guessing start of match in sv for"
533 : "Guessing start of match in string for");
536 /* CHR_DIST() would be more correct here but it makes things slow. */
537 if (prog->minlen > strend - strpos) {
538 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
539 "String too short... [re_intuit_start]\n"));
543 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
546 if (!prog->check_utf8 && prog->check_substr)
547 to_utf8_substr(prog);
548 check = prog->check_utf8;
550 if (!prog->check_substr && prog->check_utf8)
551 to_byte_substr(prog);
552 check = prog->check_substr;
554 if (check == &PL_sv_undef) {
555 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
556 "Non-utf8 string cannot match utf8 check string\n"));
559 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
560 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
561 || ( (prog->extflags & RXf_ANCH_BOL)
562 && !multiline ) ); /* Check after \n? */
565 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
566 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
567 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
569 && (strpos != strbeg)) {
570 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
573 if (prog->check_offset_min == prog->check_offset_max &&
574 !(prog->extflags & RXf_CANY_SEEN)) {
575 /* Substring at constant offset from beg-of-str... */
578 s = HOP3c(strpos, prog->check_offset_min, strend);
581 slen = SvCUR(check); /* >= 1 */
583 if ( strend - s > slen || strend - s < slen - 1
584 || (strend - s == slen && strend[-1] != '\n')) {
585 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
588 /* Now should match s[0..slen-2] */
590 if (slen && (*SvPVX_const(check) != *s
592 && memNE(SvPVX_const(check), s, slen)))) {
594 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
598 else if (*SvPVX_const(check) != *s
599 || ((slen = SvCUR(check)) > 1
600 && memNE(SvPVX_const(check), s, slen)))
603 goto success_at_start;
606 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
608 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
609 end_shift = prog->check_end_shift;
612 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
613 - (SvTAIL(check) != 0);
614 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
616 if (end_shift < eshift)
620 else { /* Can match at random position */
623 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
624 end_shift = prog->check_end_shift;
626 /* end shift should be non negative here */
629 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
631 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
632 (IV)end_shift, RX_PRECOMP(prog));
636 /* Find a possible match in the region s..strend by looking for
637 the "check" substring in the region corrected by start/end_shift. */
640 I32 srch_start_shift = start_shift;
641 I32 srch_end_shift = end_shift;
642 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
643 srch_end_shift -= ((strbeg - s) - srch_start_shift);
644 srch_start_shift = strbeg - s;
646 DEBUG_OPTIMISE_MORE_r({
647 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
648 (IV)prog->check_offset_min,
649 (IV)srch_start_shift,
651 (IV)prog->check_end_shift);
654 if (flags & REXEC_SCREAM) {
655 I32 p = -1; /* Internal iterator of scream. */
656 I32 * const pp = data ? data->scream_pos : &p;
658 if (PL_screamfirst[BmRARE(check)] >= 0
659 || ( BmRARE(check) == '\n'
660 && (BmPREVIOUS(check) == SvCUR(check) - 1)
662 s = screaminstr(sv, check,
663 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
666 /* we may be pointing at the wrong string */
667 if (s && RXp_MATCH_COPIED(prog))
668 s = strbeg + (s - SvPVX_const(sv));
670 *data->scream_olds = s;
675 if (prog->extflags & RXf_CANY_SEEN) {
676 start_point= (U8*)(s + srch_start_shift);
677 end_point= (U8*)(strend - srch_end_shift);
679 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
680 end_point= HOP3(strend, -srch_end_shift, strbeg);
682 DEBUG_OPTIMISE_MORE_r({
683 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
684 (int)(end_point - start_point),
685 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
689 s = fbm_instr( start_point, end_point,
690 check, multiline ? FBMrf_MULTILINE : 0);
693 /* Update the count-of-usability, remove useless subpatterns,
697 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
698 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
699 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
700 (s ? "Found" : "Did not find"),
701 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
702 ? "anchored" : "floating"),
705 (s ? " at offset " : "...\n") );
710 /* Finish the diagnostic message */
711 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
713 /* XXX dmq: first branch is for positive lookbehind...
714 Our check string is offset from the beginning of the pattern.
715 So we need to do any stclass tests offset forward from that
724 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
725 Start with the other substr.
726 XXXX no SCREAM optimization yet - and a very coarse implementation
727 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
728 *always* match. Probably should be marked during compile...
729 Probably it is right to do no SCREAM here...
732 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
733 : (prog->float_substr && prog->anchored_substr))
735 /* Take into account the "other" substring. */
736 /* XXXX May be hopelessly wrong for UTF... */
739 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
742 char * const last = HOP3c(s, -start_shift, strbeg);
744 char * const saved_s = s;
747 t = s - prog->check_offset_max;
748 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
750 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
755 t = HOP3c(t, prog->anchored_offset, strend);
756 if (t < other_last) /* These positions already checked */
758 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
761 /* XXXX It is not documented what units *_offsets are in.
762 We assume bytes, but this is clearly wrong.
763 Meaning this code needs to be carefully reviewed for errors.
767 /* On end-of-str: see comment below. */
768 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
769 if (must == &PL_sv_undef) {
771 DEBUG_r(must = prog->anchored_utf8); /* for debug */
776 HOP3(HOP3(last1, prog->anchored_offset, strend)
777 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
779 multiline ? FBMrf_MULTILINE : 0
782 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
783 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
784 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
785 (s ? "Found" : "Contradicts"),
786 quoted, RE_SV_TAIL(must));
791 if (last1 >= last2) {
792 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
793 ", giving up...\n"));
796 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
797 ", trying floating at offset %ld...\n",
798 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
799 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
800 s = HOP3c(last, 1, strend);
804 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
805 (long)(s - i_strpos)));
806 t = HOP3c(s, -prog->anchored_offset, strbeg);
807 other_last = HOP3c(s, 1, strend);
815 else { /* Take into account the floating substring. */
817 char * const saved_s = s;
820 t = HOP3c(s, -start_shift, strbeg);
822 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
823 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
824 last = HOP3c(t, prog->float_max_offset, strend);
825 s = HOP3c(t, prog->float_min_offset, strend);
828 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
829 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
830 /* fbm_instr() takes into account exact value of end-of-str
831 if the check is SvTAIL(ed). Since false positives are OK,
832 and end-of-str is not later than strend we are OK. */
833 if (must == &PL_sv_undef) {
835 DEBUG_r(must = prog->float_utf8); /* for debug message */
838 s = fbm_instr((unsigned char*)s,
839 (unsigned char*)last + SvCUR(must)
841 must, multiline ? FBMrf_MULTILINE : 0);
843 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
844 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
845 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
846 (s ? "Found" : "Contradicts"),
847 quoted, RE_SV_TAIL(must));
851 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
852 ", giving up...\n"));
855 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
856 ", trying anchored starting at offset %ld...\n",
857 (long)(saved_s + 1 - i_strpos)));
859 s = HOP3c(t, 1, strend);
863 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
864 (long)(s - i_strpos)));
865 other_last = s; /* Fix this later. --Hugo */
875 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
877 DEBUG_OPTIMISE_MORE_r(
878 PerlIO_printf(Perl_debug_log,
879 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
880 (IV)prog->check_offset_min,
881 (IV)prog->check_offset_max,
889 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
891 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
894 /* Fixed substring is found far enough so that the match
895 cannot start at strpos. */
897 if (ml_anch && t[-1] != '\n') {
898 /* Eventually fbm_*() should handle this, but often
899 anchored_offset is not 0, so this check will not be wasted. */
900 /* XXXX In the code below we prefer to look for "^" even in
901 presence of anchored substrings. And we search even
902 beyond the found float position. These pessimizations
903 are historical artefacts only. */
905 while (t < strend - prog->minlen) {
907 if (t < check_at - prog->check_offset_min) {
908 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
909 /* Since we moved from the found position,
910 we definitely contradict the found anchored
911 substr. Due to the above check we do not
912 contradict "check" substr.
913 Thus we can arrive here only if check substr
914 is float. Redo checking for "other"=="fixed".
917 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
918 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
919 goto do_other_anchored;
921 /* We don't contradict the found floating substring. */
922 /* XXXX Why not check for STCLASS? */
924 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
925 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
928 /* Position contradicts check-string */
929 /* XXXX probably better to look for check-string
930 than for "\n", so one should lower the limit for t? */
931 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
932 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
933 other_last = strpos = s = t + 1;
938 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
939 PL_colors[0], PL_colors[1]));
943 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
944 PL_colors[0], PL_colors[1]));
948 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
951 /* The found string does not prohibit matching at strpos,
952 - no optimization of calling REx engine can be performed,
953 unless it was an MBOL and we are not after MBOL,
954 or a future STCLASS check will fail this. */
956 /* Even in this situation we may use MBOL flag if strpos is offset
957 wrt the start of the string. */
958 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
959 && (strpos != strbeg) && strpos[-1] != '\n'
960 /* May be due to an implicit anchor of m{.*foo} */
961 && !(prog->intflags & PREGf_IMPLICIT))
966 DEBUG_EXECUTE_r( if (ml_anch)
967 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
968 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
971 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
973 prog->check_utf8 /* Could be deleted already */
974 && --BmUSEFUL(prog->check_utf8) < 0
975 && (prog->check_utf8 == prog->float_utf8)
977 prog->check_substr /* Could be deleted already */
978 && --BmUSEFUL(prog->check_substr) < 0
979 && (prog->check_substr == prog->float_substr)
982 /* If flags & SOMETHING - do not do it many times on the same match */
983 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
984 /* XXX Does the destruction order has to change with do_utf8? */
985 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
986 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
987 prog->check_substr = prog->check_utf8 = NULL; /* disable */
988 prog->float_substr = prog->float_utf8 = NULL; /* clear */
989 check = NULL; /* abort */
991 /* XXXX This is a remnant of the old implementation. It
992 looks wasteful, since now INTUIT can use many
994 prog->extflags &= ~RXf_USE_INTUIT;
1000 /* Last resort... */
1001 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1002 /* trie stclasses are too expensive to use here, we are better off to
1003 leave it to regmatch itself */
1004 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1005 /* minlen == 0 is possible if regstclass is \b or \B,
1006 and the fixed substr is ''$.
1007 Since minlen is already taken into account, s+1 is before strend;
1008 accidentally, minlen >= 1 guaranties no false positives at s + 1
1009 even for \b or \B. But (minlen? 1 : 0) below assumes that
1010 regstclass does not come from lookahead... */
1011 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1012 This leaves EXACTF only, which is dealt with in find_byclass(). */
1013 const U8* const str = (U8*)STRING(progi->regstclass);
1014 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1015 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1018 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1019 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1020 else if (prog->float_substr || prog->float_utf8)
1021 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1025 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1026 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1029 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1032 const char *what = NULL;
1034 if (endpos == strend) {
1035 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1036 "Could not match STCLASS...\n") );
1039 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1040 "This position contradicts STCLASS...\n") );
1041 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1043 /* Contradict one of substrings */
1044 if (prog->anchored_substr || prog->anchored_utf8) {
1045 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1046 DEBUG_EXECUTE_r( what = "anchored" );
1048 s = HOP3c(t, 1, strend);
1049 if (s + start_shift + end_shift > strend) {
1050 /* XXXX Should be taken into account earlier? */
1051 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1052 "Could not match STCLASS...\n") );
1057 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1058 "Looking for %s substr starting at offset %ld...\n",
1059 what, (long)(s + start_shift - i_strpos)) );
1062 /* Have both, check_string is floating */
1063 if (t + start_shift >= check_at) /* Contradicts floating=check */
1064 goto retry_floating_check;
1065 /* Recheck anchored substring, but not floating... */
1069 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1070 "Looking for anchored substr starting at offset %ld...\n",
1071 (long)(other_last - i_strpos)) );
1072 goto do_other_anchored;
1074 /* Another way we could have checked stclass at the
1075 current position only: */
1080 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1081 "Looking for /%s^%s/m starting at offset %ld...\n",
1082 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1085 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1087 /* Check is floating subtring. */
1088 retry_floating_check:
1089 t = check_at - start_shift;
1090 DEBUG_EXECUTE_r( what = "floating" );
1091 goto hop_and_restart;
1094 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1095 "By STCLASS: moving %ld --> %ld\n",
1096 (long)(t - i_strpos), (long)(s - i_strpos))
1100 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1101 "Does not contradict STCLASS...\n");
1106 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1107 PL_colors[4], (check ? "Guessed" : "Giving up"),
1108 PL_colors[5], (long)(s - i_strpos)) );
1111 fail_finish: /* Substring not found */
1112 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1113 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1115 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1116 PL_colors[4], PL_colors[5]));
1120 #define DECL_TRIE_TYPE(scan) \
1121 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1122 trie_type = (scan->flags != EXACT) \
1123 ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \
1124 : (do_utf8 ? trie_utf8 : trie_plain)
1126 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1127 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1128 switch (trie_type) { \
1129 case trie_utf8_fold: \
1130 if ( foldlen>0 ) { \
1131 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1136 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1137 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1138 foldlen -= UNISKIP( uvc ); \
1139 uscan = foldbuf + UNISKIP( uvc ); \
1142 case trie_latin_utf8_fold: \
1143 if ( foldlen>0 ) { \
1144 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1150 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1151 foldlen -= UNISKIP( uvc ); \
1152 uscan = foldbuf + UNISKIP( uvc ); \
1156 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1163 charid = trie->charmap[ uvc ]; \
1167 if (widecharmap) { \
1168 SV** const svpp = hv_fetch(widecharmap, \
1169 (char*)&uvc, sizeof(UV), 0); \
1171 charid = (U16)SvIV(*svpp); \
1176 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1178 char *my_strend= (char *)strend; \
1181 !ibcmp_utf8(s, &my_strend, 0, do_utf8, \
1182 m, NULL, ln, (bool)UTF)) \
1183 && (!reginfo || regtry(reginfo, &s)) ) \
1186 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1187 uvchr_to_utf8(tmpbuf, c); \
1188 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1190 && (f == c1 || f == c2) \
1192 !ibcmp_utf8(s, &my_strend, 0, do_utf8,\
1193 m, NULL, ln, (bool)UTF)) \
1194 && (!reginfo || regtry(reginfo, &s)) ) \
1200 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1204 && (ln == 1 || !(OP(c) == EXACTF \
1206 : ibcmp_locale(s, m, ln))) \
1207 && (!reginfo || regtry(reginfo, &s)) ) \
1213 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1215 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1221 #define REXEC_FBC_SCAN(CoDe) \
1223 while (s < strend) { \
1229 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1230 REXEC_FBC_UTF8_SCAN( \
1232 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1241 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1244 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1253 #define REXEC_FBC_TRYIT \
1254 if ((!reginfo || regtry(reginfo, &s))) \
1257 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1259 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1262 REXEC_FBC_CLASS_SCAN(CoNd); \
1266 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1269 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1272 REXEC_FBC_CLASS_SCAN(CoNd); \
1276 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1277 PL_reg_flags |= RF_tainted; \
1279 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1282 REXEC_FBC_CLASS_SCAN(CoNd); \
1286 #define DUMP_EXEC_POS(li,s,doutf8) \
1287 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1289 /* We know what class REx starts with. Try to find this position... */
1290 /* if reginfo is NULL, its a dryrun */
1291 /* annoyingly all the vars in this routine have different names from their counterparts
1292 in regmatch. /grrr */
1295 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1296 const char *strend, regmatch_info *reginfo)
1299 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1303 register STRLEN uskip;
1307 register I32 tmp = 1; /* Scratch variable? */
1308 register const bool do_utf8 = PL_reg_match_utf8;
1309 RXi_GET_DECL(prog,progi);
1311 PERL_ARGS_ASSERT_FIND_BYCLASS;
1313 /* We know what class it must start with. */
1317 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1318 !UTF8_IS_INVARIANT((U8)s[0]) ?
1319 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1320 REGINCLASS(prog, c, (U8*)s));
1323 while (s < strend) {
1326 if (REGINCLASS(prog, c, (U8*)s) ||
1327 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1328 /* The assignment of 2 is intentional:
1329 * for the folded sharp s, the skip is 2. */
1330 (skip = SHARP_S_SKIP))) {
1331 if (tmp && (!reginfo || regtry(reginfo, &s)))
1344 if (tmp && (!reginfo || regtry(reginfo, &s)))
1352 ln = STR_LEN(c); /* length to match in octets/bytes */
1353 lnc = (I32) ln; /* length to match in characters */
1355 STRLEN ulen1, ulen2;
1357 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1358 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1359 /* used by commented-out code below */
1360 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1362 /* XXX: Since the node will be case folded at compile
1363 time this logic is a little odd, although im not
1364 sure that its actually wrong. --dmq */
1366 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1367 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1369 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1370 codepoint of the first character in the converted
1371 form, yet originally we did the extra step.
1372 No tests fail by commenting this code out however
1373 so Ive left it out. -- dmq.
1375 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1377 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1382 while (sm < ((U8 *) m + ln)) {
1397 c2 = PL_fold_locale[c1];
1399 e = HOP3c(strend, -((I32)lnc), s);
1401 if (!reginfo && e < s)
1402 e = s; /* Due to minlen logic of intuit() */
1404 /* The idea in the EXACTF* cases is to first find the
1405 * first character of the EXACTF* node and then, if
1406 * necessary, case-insensitively compare the full
1407 * text of the node. The c1 and c2 are the first
1408 * characters (though in Unicode it gets a bit
1409 * more complicated because there are more cases
1410 * than just upper and lower: one needs to use
1411 * the so-called folding case for case-insensitive
1412 * matching (called "loose matching" in Unicode).
1413 * ibcmp_utf8() will do just that. */
1415 if (do_utf8 || UTF) {
1417 U8 tmpbuf [UTF8_MAXBYTES+1];
1420 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1422 /* Upper and lower of 1st char are equal -
1423 * probably not a "letter". */
1426 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1431 REXEC_FBC_EXACTISH_CHECK(c == c1);
1437 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1443 /* Handle some of the three Greek sigmas cases.
1444 * Note that not all the possible combinations
1445 * are handled here: some of them are handled
1446 * by the standard folding rules, and some of
1447 * them (the character class or ANYOF cases)
1448 * are handled during compiletime in
1449 * regexec.c:S_regclass(). */
1450 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1451 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1452 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1454 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1459 /* Neither pattern nor string are UTF8 */
1461 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1463 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1467 PL_reg_flags |= RF_tainted;
1474 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1475 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1477 tmp = ((OP(c) == BOUND ?
1478 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1479 LOAD_UTF8_CHARCLASS_ALNUM();
1480 REXEC_FBC_UTF8_SCAN(
1481 if (tmp == !(OP(c) == BOUND ?
1482 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1483 isALNUM_LC_utf8((U8*)s)))
1491 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1492 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1495 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1501 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1505 PL_reg_flags |= RF_tainted;
1512 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1513 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1515 tmp = ((OP(c) == NBOUND ?
1516 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1517 LOAD_UTF8_CHARCLASS_ALNUM();
1518 REXEC_FBC_UTF8_SCAN(
1519 if (tmp == !(OP(c) == NBOUND ?
1520 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1521 isALNUM_LC_utf8((U8*)s)))
1523 else REXEC_FBC_TRYIT;
1527 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1528 tmp = ((OP(c) == NBOUND ?
1529 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1532 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1534 else REXEC_FBC_TRYIT;
1537 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1541 REXEC_FBC_CSCAN_PRELOAD(
1542 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1543 swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1547 REXEC_FBC_CSCAN_TAINT(
1548 isALNUM_LC_utf8((U8*)s),
1552 REXEC_FBC_CSCAN_PRELOAD(
1553 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1554 !swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1558 REXEC_FBC_CSCAN_TAINT(
1559 !isALNUM_LC_utf8((U8*)s),
1563 REXEC_FBC_CSCAN_PRELOAD(
1564 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1565 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8),
1569 REXEC_FBC_CSCAN_TAINT(
1570 *s == ' ' || isSPACE_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, do_utf8)),
1580 REXEC_FBC_CSCAN_TAINT(
1581 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1585 REXEC_FBC_CSCAN_PRELOAD(
1586 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1587 swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1591 REXEC_FBC_CSCAN_TAINT(
1592 isDIGIT_LC_utf8((U8*)s),
1596 REXEC_FBC_CSCAN_PRELOAD(
1597 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1598 !swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1602 REXEC_FBC_CSCAN_TAINT(
1603 !isDIGIT_LC_utf8((U8*)s),
1609 is_LNBREAK_latin1(s)
1619 !is_VERTWS_latin1(s)
1624 is_HORIZWS_latin1(s)
1628 !is_HORIZWS_utf8(s),
1629 !is_HORIZWS_latin1(s)
1635 /* what trie are we using right now */
1637 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1639 = (reg_trie_data*)progi->data->data[ aho->trie ];
1640 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1642 const char *last_start = strend - trie->minlen;
1644 const char *real_start = s;
1646 STRLEN maxlen = trie->maxlen;
1648 U8 **points; /* map of where we were in the input string
1649 when reading a given char. For ASCII this
1650 is unnecessary overhead as the relationship
1651 is always 1:1, but for Unicode, especially
1652 case folded Unicode this is not true. */
1653 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1657 GET_RE_DEBUG_FLAGS_DECL;
1659 /* We can't just allocate points here. We need to wrap it in
1660 * an SV so it gets freed properly if there is a croak while
1661 * running the match */
1664 sv_points=newSV(maxlen * sizeof(U8 *));
1665 SvCUR_set(sv_points,
1666 maxlen * sizeof(U8 *));
1667 SvPOK_on(sv_points);
1668 sv_2mortal(sv_points);
1669 points=(U8**)SvPV_nolen(sv_points );
1670 if ( trie_type != trie_utf8_fold
1671 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1674 bitmap=(U8*)trie->bitmap;
1676 bitmap=(U8*)ANYOF_BITMAP(c);
1678 /* this is the Aho-Corasick algorithm modified a touch
1679 to include special handling for long "unknown char"
1680 sequences. The basic idea being that we use AC as long
1681 as we are dealing with a possible matching char, when
1682 we encounter an unknown char (and we have not encountered
1683 an accepting state) we scan forward until we find a legal
1685 AC matching is basically that of trie matching, except
1686 that when we encounter a failing transition, we fall back
1687 to the current states "fail state", and try the current char
1688 again, a process we repeat until we reach the root state,
1689 state 1, or a legal transition. If we fail on the root state
1690 then we can either terminate if we have reached an accepting
1691 state previously, or restart the entire process from the beginning
1695 while (s <= last_start) {
1696 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1704 U8 *uscan = (U8*)NULL;
1705 U8 *leftmost = NULL;
1707 U32 accepted_word= 0;
1711 while ( state && uc <= (U8*)strend ) {
1713 U32 word = aho->states[ state ].wordnum;
1717 DEBUG_TRIE_EXECUTE_r(
1718 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1719 dump_exec_pos( (char *)uc, c, strend, real_start,
1720 (char *)uc, do_utf8 );
1721 PerlIO_printf( Perl_debug_log,
1722 " Scanning for legal start char...\n");
1725 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1730 if (uc >(U8*)last_start) break;
1734 U8 *lpos= points[ (pointpos - trie->wordlen[word-1] ) % maxlen ];
1735 if (!leftmost || lpos < leftmost) {
1736 DEBUG_r(accepted_word=word);
1742 points[pointpos++ % maxlen]= uc;
1743 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1744 uscan, len, uvc, charid, foldlen,
1746 DEBUG_TRIE_EXECUTE_r({
1747 dump_exec_pos( (char *)uc, c, strend, real_start,
1749 PerlIO_printf(Perl_debug_log,
1750 " Charid:%3u CP:%4"UVxf" ",
1756 word = aho->states[ state ].wordnum;
1758 base = aho->states[ state ].trans.base;
1760 DEBUG_TRIE_EXECUTE_r({
1762 dump_exec_pos( (char *)uc, c, strend, real_start,
1764 PerlIO_printf( Perl_debug_log,
1765 "%sState: %4"UVxf", word=%"UVxf,
1766 failed ? " Fail transition to " : "",
1767 (UV)state, (UV)word);
1772 (base + charid > trie->uniquecharcount )
1773 && (base + charid - 1 - trie->uniquecharcount
1775 && trie->trans[base + charid - 1 -
1776 trie->uniquecharcount].check == state
1777 && (tmp=trie->trans[base + charid - 1 -
1778 trie->uniquecharcount ].next))
1780 DEBUG_TRIE_EXECUTE_r(
1781 PerlIO_printf( Perl_debug_log," - legal\n"));
1786 DEBUG_TRIE_EXECUTE_r(
1787 PerlIO_printf( Perl_debug_log," - fail\n"));
1789 state = aho->fail[state];
1793 /* we must be accepting here */
1794 DEBUG_TRIE_EXECUTE_r(
1795 PerlIO_printf( Perl_debug_log," - accepting\n"));
1804 if (!state) state = 1;
1807 if ( aho->states[ state ].wordnum ) {
1808 U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ];
1809 if (!leftmost || lpos < leftmost) {
1810 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1815 s = (char*)leftmost;
1816 DEBUG_TRIE_EXECUTE_r({
1818 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1819 (UV)accepted_word, (IV)(s - real_start)
1822 if (!reginfo || regtry(reginfo, &s)) {
1828 DEBUG_TRIE_EXECUTE_r({
1829 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1832 DEBUG_TRIE_EXECUTE_r(
1833 PerlIO_printf( Perl_debug_log,"No match.\n"));
1842 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1852 - regexec_flags - match a regexp against a string
1855 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1856 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1857 /* strend: pointer to null at end of string */
1858 /* strbeg: real beginning of string */
1859 /* minend: end of match must be >=minend after stringarg. */
1860 /* data: May be used for some additional optimizations.
1861 Currently its only used, with a U32 cast, for transmitting
1862 the ganch offset when doing a /g match. This will change */
1863 /* nosave: For optimizations. */
1866 struct regexp *const prog = (struct regexp *)SvANY(rx);
1867 /*register*/ char *s;
1868 register regnode *c;
1869 /*register*/ char *startpos = stringarg;
1870 I32 minlen; /* must match at least this many chars */
1871 I32 dontbother = 0; /* how many characters not to try at end */
1872 I32 end_shift = 0; /* Same for the end. */ /* CC */
1873 I32 scream_pos = -1; /* Internal iterator of scream. */
1874 char *scream_olds = NULL;
1875 const bool do_utf8 = (bool)DO_UTF8(sv);
1877 RXi_GET_DECL(prog,progi);
1878 regmatch_info reginfo; /* create some info to pass to regtry etc */
1879 regexp_paren_pair *swap = NULL;
1880 GET_RE_DEBUG_FLAGS_DECL;
1882 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1883 PERL_UNUSED_ARG(data);
1885 /* Be paranoid... */
1886 if (prog == NULL || startpos == NULL) {
1887 Perl_croak(aTHX_ "NULL regexp parameter");
1891 multiline = prog->extflags & RXf_PMf_MULTILINE;
1892 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1894 RX_MATCH_UTF8_set(rx, do_utf8);
1896 debug_start_match(rx, do_utf8, startpos, strend,
1900 minlen = prog->minlen;
1902 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1903 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1904 "String too short [regexec_flags]...\n"));
1909 /* Check validity of program. */
1910 if (UCHARAT(progi->program) != REG_MAGIC) {
1911 Perl_croak(aTHX_ "corrupted regexp program");
1915 PL_reg_eval_set = 0;
1919 PL_reg_flags |= RF_utf8;
1921 /* Mark beginning of line for ^ and lookbehind. */
1922 reginfo.bol = startpos; /* XXX not used ??? */
1926 /* Mark end of line for $ (and such) */
1929 /* see how far we have to get to not match where we matched before */
1930 reginfo.till = startpos+minend;
1932 /* If there is a "must appear" string, look for it. */
1935 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1937 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
1938 reginfo.ganch = startpos + prog->gofs;
1939 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1940 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
1941 } else if (sv && SvTYPE(sv) >= SVt_PVMG
1943 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1944 && mg->mg_len >= 0) {
1945 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1946 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1947 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
1949 if (prog->extflags & RXf_ANCH_GPOS) {
1950 if (s > reginfo.ganch)
1952 s = reginfo.ganch - prog->gofs;
1953 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1954 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
1960 reginfo.ganch = strbeg + PTR2UV(data);
1961 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1962 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
1964 } else { /* pos() not defined */
1965 reginfo.ganch = strbeg;
1966 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1967 "GPOS: reginfo.ganch = strbeg\n"));
1970 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1971 /* We have to be careful. If the previous successful match
1972 was from this regex we don't want a subsequent partially
1973 successful match to clobber the old results.
1974 So when we detect this possibility we add a swap buffer
1975 to the re, and switch the buffer each match. If we fail
1976 we switch it back, otherwise we leave it swapped.
1979 /* do we need a save destructor here for eval dies? */
1980 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
1982 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1983 re_scream_pos_data d;
1985 d.scream_olds = &scream_olds;
1986 d.scream_pos = &scream_pos;
1987 s = re_intuit_start(rx, sv, s, strend, flags, &d);
1989 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1990 goto phooey; /* not present */
1996 /* Simplest case: anchored match need be tried only once. */
1997 /* [unless only anchor is BOL and multiline is set] */
1998 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
1999 if (s == startpos && regtry(®info, &startpos))
2001 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2002 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2007 dontbother = minlen - 1;
2008 end = HOP3c(strend, -dontbother, strbeg) - 1;
2009 /* for multiline we only have to try after newlines */
2010 if (prog->check_substr || prog->check_utf8) {
2014 if (regtry(®info, &s))
2019 if (prog->extflags & RXf_USE_INTUIT) {
2020 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2031 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2032 if (regtry(®info, &s))
2039 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2041 /* the warning about reginfo.ganch being used without intialization
2042 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2043 and we only enter this block when the same bit is set. */
2044 char *tmp_s = reginfo.ganch - prog->gofs;
2046 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2051 /* Messy cases: unanchored match. */
2052 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2053 /* we have /x+whatever/ */
2054 /* it must be a one character string (XXXX Except UTF?) */
2059 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2060 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2061 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
2066 DEBUG_EXECUTE_r( did_match = 1 );
2067 if (regtry(®info, &s)) goto got_it;
2069 while (s < strend && *s == ch)
2077 DEBUG_EXECUTE_r( did_match = 1 );
2078 if (regtry(®info, &s)) goto got_it;
2080 while (s < strend && *s == ch)
2085 DEBUG_EXECUTE_r(if (!did_match)
2086 PerlIO_printf(Perl_debug_log,
2087 "Did not find anchored character...\n")
2090 else if (prog->anchored_substr != NULL
2091 || prog->anchored_utf8 != NULL
2092 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2093 && prog->float_max_offset < strend - s)) {
2098 char *last1; /* Last position checked before */
2102 if (prog->anchored_substr || prog->anchored_utf8) {
2103 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2104 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2105 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
2106 back_max = back_min = prog->anchored_offset;
2108 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2109 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2110 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
2111 back_max = prog->float_max_offset;
2112 back_min = prog->float_min_offset;
2116 if (must == &PL_sv_undef)
2117 /* could not downgrade utf8 check substring, so must fail */
2123 last = HOP3c(strend, /* Cannot start after this */
2124 -(I32)(CHR_SVLEN(must)
2125 - (SvTAIL(must) != 0) + back_min), strbeg);
2128 last1 = HOPc(s, -1);
2130 last1 = s - 1; /* bogus */
2132 /* XXXX check_substr already used to find "s", can optimize if
2133 check_substr==must. */
2135 dontbother = end_shift;
2136 strend = HOPc(strend, -dontbother);
2137 while ( (s <= last) &&
2138 ((flags & REXEC_SCREAM)
2139 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2140 end_shift, &scream_pos, 0))
2141 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2142 (unsigned char*)strend, must,
2143 multiline ? FBMrf_MULTILINE : 0))) ) {
2144 /* we may be pointing at the wrong string */
2145 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2146 s = strbeg + (s - SvPVX_const(sv));
2147 DEBUG_EXECUTE_r( did_match = 1 );
2148 if (HOPc(s, -back_max) > last1) {
2149 last1 = HOPc(s, -back_min);
2150 s = HOPc(s, -back_max);
2153 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2155 last1 = HOPc(s, -back_min);
2159 while (s <= last1) {
2160 if (regtry(®info, &s))
2166 while (s <= last1) {
2167 if (regtry(®info, &s))
2173 DEBUG_EXECUTE_r(if (!did_match) {
2174 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
2175 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2176 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2177 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2178 ? "anchored" : "floating"),
2179 quoted, RE_SV_TAIL(must));
2183 else if ( (c = progi->regstclass) ) {
2185 const OPCODE op = OP(progi->regstclass);
2186 /* don't bother with what can't match */
2187 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2188 strend = HOPc(strend, -(minlen - 1));
2191 SV * const prop = sv_newmortal();
2192 regprop(prog, prop, c);
2194 RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1),
2196 PerlIO_printf(Perl_debug_log,
2197 "Matching stclass %.*s against %s (%d chars)\n",
2198 (int)SvCUR(prop), SvPVX_const(prop),
2199 quoted, (int)(strend - s));
2202 if (find_byclass(prog, c, s, strend, ®info))
2204 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2208 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2213 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2214 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2215 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
2217 if (flags & REXEC_SCREAM) {
2218 last = screaminstr(sv, float_real, s - strbeg,
2219 end_shift, &scream_pos, 1); /* last one */
2221 last = scream_olds; /* Only one occurrence. */
2222 /* we may be pointing at the wrong string */
2223 else if (RXp_MATCH_COPIED(prog))
2224 s = strbeg + (s - SvPVX_const(sv));
2228 const char * const little = SvPV_const(float_real, len);
2230 if (SvTAIL(float_real)) {
2231 if (memEQ(strend - len + 1, little, len - 1))
2232 last = strend - len + 1;
2233 else if (!multiline)
2234 last = memEQ(strend - len, little, len)
2235 ? strend - len : NULL;
2241 last = rninstr(s, strend, little, little + len);
2243 last = strend; /* matching "$" */
2248 PerlIO_printf(Perl_debug_log,
2249 "%sCan't trim the tail, match fails (should not happen)%s\n",
2250 PL_colors[4], PL_colors[5]));
2251 goto phooey; /* Should not happen! */
2253 dontbother = strend - last + prog->float_min_offset;
2255 if (minlen && (dontbother < minlen))
2256 dontbother = minlen - 1;
2257 strend -= dontbother; /* this one's always in bytes! */
2258 /* We don't know much -- general case. */
2261 if (regtry(®info, &s))
2270 if (regtry(®info, &s))
2272 } while (s++ < strend);
2281 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2283 if (PL_reg_eval_set)
2284 restore_pos(aTHX_ prog);
2285 if (RXp_PAREN_NAMES(prog))
2286 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2288 /* make sure $`, $&, $', and $digit will work later */
2289 if ( !(flags & REXEC_NOT_FIRST) ) {
2290 RX_MATCH_COPY_FREE(rx);
2291 if (flags & REXEC_COPY_STR) {
2292 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2293 #ifdef PERL_OLD_COPY_ON_WRITE
2295 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2297 PerlIO_printf(Perl_debug_log,
2298 "Copy on write: regexp capture, type %d\n",
2301 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2302 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2303 assert (SvPOKp(prog->saved_copy));
2307 RX_MATCH_COPIED_on(rx);
2308 s = savepvn(strbeg, i);
2314 prog->subbeg = strbeg;
2315 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2322 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2323 PL_colors[4], PL_colors[5]));
2324 if (PL_reg_eval_set)
2325 restore_pos(aTHX_ prog);
2327 /* we failed :-( roll it back */
2328 Safefree(prog->offs);
2337 - regtry - try match at specific point
2339 STATIC I32 /* 0 failure, 1 success */
2340 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2344 REGEXP *const rx = reginfo->prog;
2345 regexp *const prog = (struct regexp *)SvANY(rx);
2346 RXi_GET_DECL(prog,progi);
2347 GET_RE_DEBUG_FLAGS_DECL;
2349 PERL_ARGS_ASSERT_REGTRY;
2351 reginfo->cutpoint=NULL;
2353 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2356 PL_reg_eval_set = RS_init;
2357 DEBUG_EXECUTE_r(DEBUG_s(
2358 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2359 (IV)(PL_stack_sp - PL_stack_base));
2362 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2363 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2365 /* Apparently this is not needed, judging by wantarray. */
2366 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2367 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2370 /* Make $_ available to executed code. */
2371 if (reginfo->sv != DEFSV) {
2373 DEFSV_set(reginfo->sv);
2376 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2377 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2378 /* prepare for quick setting of pos */
2379 #ifdef PERL_OLD_COPY_ON_WRITE
2380 if (SvIsCOW(reginfo->sv))
2381 sv_force_normal_flags(reginfo->sv, 0);
2383 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2384 &PL_vtbl_mglob, NULL, 0);
2388 PL_reg_oldpos = mg->mg_len;
2389 SAVEDESTRUCTOR_X(restore_pos, prog);
2391 if (!PL_reg_curpm) {
2392 Newxz(PL_reg_curpm, 1, PMOP);
2395 SV* const repointer = &PL_sv_undef;
2396 /* this regexp is also owned by the new PL_reg_curpm, which
2397 will try to free it. */
2398 av_push(PL_regex_padav, repointer);
2399 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2400 PL_regex_pad = AvARRAY(PL_regex_padav);
2405 /* It seems that non-ithreads works both with and without this code.
2406 So for efficiency reasons it seems best not to have the code
2407 compiled when it is not needed. */
2408 /* This is safe against NULLs: */
2409 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2410 /* PM_reg_curpm owns a reference to this regexp. */
2413 PM_SETRE(PL_reg_curpm, rx);
2414 PL_reg_oldcurpm = PL_curpm;
2415 PL_curpm = PL_reg_curpm;
2416 if (RXp_MATCH_COPIED(prog)) {
2417 /* Here is a serious problem: we cannot rewrite subbeg,
2418 since it may be needed if this match fails. Thus
2419 $` inside (?{}) could fail... */
2420 PL_reg_oldsaved = prog->subbeg;
2421 PL_reg_oldsavedlen = prog->sublen;
2422 #ifdef PERL_OLD_COPY_ON_WRITE
2423 PL_nrs = prog->saved_copy;
2425 RXp_MATCH_COPIED_off(prog);
2428 PL_reg_oldsaved = NULL;
2429 prog->subbeg = PL_bostr;
2430 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2432 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2433 prog->offs[0].start = *startpos - PL_bostr;
2434 PL_reginput = *startpos;
2435 PL_reglastparen = &prog->lastparen;
2436 PL_reglastcloseparen = &prog->lastcloseparen;
2437 prog->lastparen = 0;
2438 prog->lastcloseparen = 0;
2440 PL_regoffs = prog->offs;
2441 if (PL_reg_start_tmpl <= prog->nparens) {
2442 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2443 if(PL_reg_start_tmp)
2444 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2446 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2449 /* XXXX What this code is doing here?!!! There should be no need
2450 to do this again and again, PL_reglastparen should take care of
2453 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2454 * Actually, the code in regcppop() (which Ilya may be meaning by
2455 * PL_reglastparen), is not needed at all by the test suite
2456 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2457 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2458 * Meanwhile, this code *is* needed for the
2459 * above-mentioned test suite tests to succeed. The common theme
2460 * on those tests seems to be returning null fields from matches.
2461 * --jhi updated by dapm */
2463 if (prog->nparens) {
2464 regexp_paren_pair *pp = PL_regoffs;
2466 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2474 if (regmatch(reginfo, progi->program + 1)) {
2475 PL_regoffs[0].end = PL_reginput - PL_bostr;
2478 if (reginfo->cutpoint)
2479 *startpos= reginfo->cutpoint;
2480 REGCP_UNWIND(lastcp);
2485 #define sayYES goto yes
2486 #define sayNO goto no
2487 #define sayNO_SILENT goto no_silent
2489 /* we dont use STMT_START/END here because it leads to
2490 "unreachable code" warnings, which are bogus, but distracting. */
2491 #define CACHEsayNO \
2492 if (ST.cache_mask) \
2493 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2496 /* this is used to determine how far from the left messages like
2497 'failed...' are printed. It should be set such that messages
2498 are inline with the regop output that created them.
2500 #define REPORT_CODE_OFF 32
2503 /* Make sure there is a test for this +1 options in re_tests */
2504 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2506 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2507 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2509 #define SLAB_FIRST(s) (&(s)->states[0])
2510 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2512 /* grab a new slab and return the first slot in it */
2514 STATIC regmatch_state *
2517 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2520 regmatch_slab *s = PL_regmatch_slab->next;
2522 Newx(s, 1, regmatch_slab);
2523 s->prev = PL_regmatch_slab;
2525 PL_regmatch_slab->next = s;
2527 PL_regmatch_slab = s;
2528 return SLAB_FIRST(s);
2532 /* push a new state then goto it */
2534 #define PUSH_STATE_GOTO(state, node) \
2536 st->resume_state = state; \
2539 /* push a new state with success backtracking, then goto it */
2541 #define PUSH_YES_STATE_GOTO(state, node) \
2543 st->resume_state = state; \
2544 goto push_yes_state;
2550 regmatch() - main matching routine
2552 This is basically one big switch statement in a loop. We execute an op,
2553 set 'next' to point the next op, and continue. If we come to a point which
2554 we may need to backtrack to on failure such as (A|B|C), we push a
2555 backtrack state onto the backtrack stack. On failure, we pop the top
2556 state, and re-enter the loop at the state indicated. If there are no more
2557 states to pop, we return failure.
2559 Sometimes we also need to backtrack on success; for example /A+/, where
2560 after successfully matching one A, we need to go back and try to
2561 match another one; similarly for lookahead assertions: if the assertion
2562 completes successfully, we backtrack to the state just before the assertion
2563 and then carry on. In these cases, the pushed state is marked as
2564 'backtrack on success too'. This marking is in fact done by a chain of
2565 pointers, each pointing to the previous 'yes' state. On success, we pop to
2566 the nearest yes state, discarding any intermediate failure-only states.
2567 Sometimes a yes state is pushed just to force some cleanup code to be
2568 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2569 it to free the inner regex.
2571 Note that failure backtracking rewinds the cursor position, while
2572 success backtracking leaves it alone.
2574 A pattern is complete when the END op is executed, while a subpattern
2575 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2576 ops trigger the "pop to last yes state if any, otherwise return true"
2579 A common convention in this function is to use A and B to refer to the two
2580 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2581 the subpattern to be matched possibly multiple times, while B is the entire
2582 rest of the pattern. Variable and state names reflect this convention.
2584 The states in the main switch are the union of ops and failure/success of
2585 substates associated with with that op. For example, IFMATCH is the op
2586 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2587 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2588 successfully matched A and IFMATCH_A_fail is a state saying that we have
2589 just failed to match A. Resume states always come in pairs. The backtrack
2590 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2591 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2592 on success or failure.
2594 The struct that holds a backtracking state is actually a big union, with
2595 one variant for each major type of op. The variable st points to the
2596 top-most backtrack struct. To make the code clearer, within each
2597 block of code we #define ST to alias the relevant union.
2599 Here's a concrete example of a (vastly oversimplified) IFMATCH
2605 #define ST st->u.ifmatch
2607 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2608 ST.foo = ...; // some state we wish to save
2610 // push a yes backtrack state with a resume value of
2611 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2613 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2616 case IFMATCH_A: // we have successfully executed A; now continue with B
2618 bar = ST.foo; // do something with the preserved value
2621 case IFMATCH_A_fail: // A failed, so the assertion failed
2622 ...; // do some housekeeping, then ...
2623 sayNO; // propagate the failure
2630 For any old-timers reading this who are familiar with the old recursive
2631 approach, the code above is equivalent to:
2633 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2642 ...; // do some housekeeping, then ...
2643 sayNO; // propagate the failure
2646 The topmost backtrack state, pointed to by st, is usually free. If you
2647 want to claim it, populate any ST.foo fields in it with values you wish to
2648 save, then do one of
2650 PUSH_STATE_GOTO(resume_state, node);
2651 PUSH_YES_STATE_GOTO(resume_state, node);
2653 which sets that backtrack state's resume value to 'resume_state', pushes a
2654 new free entry to the top of the backtrack stack, then goes to 'node'.
2655 On backtracking, the free slot is popped, and the saved state becomes the
2656 new free state. An ST.foo field in this new top state can be temporarily
2657 accessed to retrieve values, but once the main loop is re-entered, it
2658 becomes available for reuse.
2660 Note that the depth of the backtrack stack constantly increases during the
2661 left-to-right execution of the pattern, rather than going up and down with
2662 the pattern nesting. For example the stack is at its maximum at Z at the
2663 end of the pattern, rather than at X in the following:
2665 /(((X)+)+)+....(Y)+....Z/
2667 The only exceptions to this are lookahead/behind assertions and the cut,
2668 (?>A), which pop all the backtrack states associated with A before
2671 Bascktrack state structs are allocated in slabs of about 4K in size.
2672 PL_regmatch_state and st always point to the currently active state,
2673 and PL_regmatch_slab points to the slab currently containing
2674 PL_regmatch_state. The first time regmatch() is called, the first slab is
2675 allocated, and is never freed until interpreter destruction. When the slab
2676 is full, a new one is allocated and chained to the end. At exit from
2677 regmatch(), slabs allocated since entry are freed.
2682 #define DEBUG_STATE_pp(pp) \
2684 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2685 PerlIO_printf(Perl_debug_log, \
2686 " %*s"pp" %s%s%s%s%s\n", \
2688 PL_reg_name[st->resume_state], \
2689 ((st==yes_state||st==mark_state) ? "[" : ""), \
2690 ((st==yes_state) ? "Y" : ""), \
2691 ((st==mark_state) ? "M" : ""), \
2692 ((st==yes_state||st==mark_state) ? "]" : "") \
2697 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2702 S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8,
2703 const char *start, const char *end, const char *blurb)
2705 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2707 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2712 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2713 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2715 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2716 start, end - start, 60);
2718 PerlIO_printf(Perl_debug_log,
2719 "%s%s REx%s %s against %s\n",
2720 PL_colors[4], blurb, PL_colors[5], s0, s1);
2722 if (do_utf8||utf8_pat)
2723 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2724 utf8_pat ? "pattern" : "",
2725 utf8_pat && do_utf8 ? " and " : "",
2726 do_utf8 ? "string" : ""
2732 S_dump_exec_pos(pTHX_ const char *locinput,
2733 const regnode *scan,
2734 const char *loc_regeol,
2735 const char *loc_bostr,
2736 const char *loc_reg_starttry,
2739 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2740 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2741 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2742 /* The part of the string before starttry has one color
2743 (pref0_len chars), between starttry and current
2744 position another one (pref_len - pref0_len chars),
2745 after the current position the third one.
2746 We assume that pref0_len <= pref_len, otherwise we
2747 decrease pref0_len. */
2748 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2749 ? (5 + taill) - l : locinput - loc_bostr;
2752 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2754 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2756 pref0_len = pref_len - (locinput - loc_reg_starttry);
2757 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2758 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2759 ? (5 + taill) - pref_len : loc_regeol - locinput);
2760 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2764 if (pref0_len > pref_len)
2765 pref0_len = pref_len;
2767 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2769 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2770 (locinput - pref_len),pref0_len, 60, 4, 5);
2772 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2773 (locinput - pref_len + pref0_len),
2774 pref_len - pref0_len, 60, 2, 3);
2776 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2777 locinput, loc_regeol - locinput, 10, 0, 1);
2779 const STRLEN tlen=len0+len1+len2;
2780 PerlIO_printf(Perl_debug_log,
2781 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2782 (IV)(locinput - loc_bostr),
2785 (docolor ? "" : "> <"),
2787 (int)(tlen > 19 ? 0 : 19 - tlen),
2794 /* reg_check_named_buff_matched()
2795 * Checks to see if a named buffer has matched. The data array of
2796 * buffer numbers corresponding to the buffer is expected to reside
2797 * in the regexp->data->data array in the slot stored in the ARG() of
2798 * node involved. Note that this routine doesn't actually care about the
2799 * name, that information is not preserved from compilation to execution.
2800 * Returns the index of the leftmost defined buffer with the given name
2801 * or 0 if non of the buffers matched.
2804 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2807 RXi_GET_DECL(rex,rexi);
2808 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2809 I32 *nums=(I32*)SvPVX(sv_dat);
2811 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2813 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2814 if ((I32)*PL_reglastparen >= nums[n] &&
2815 PL_regoffs[nums[n]].end != -1)
2824 /* free all slabs above current one - called during LEAVE_SCOPE */
2827 S_clear_backtrack_stack(pTHX_ void *p)
2829 regmatch_slab *s = PL_regmatch_slab->next;
2834 PL_regmatch_slab->next = NULL;
2836 regmatch_slab * const osl = s;
2843 #define SETREX(Re1,Re2) \
2844 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2847 STATIC I32 /* 0 failure, 1 success */
2848 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2850 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2854 register const bool do_utf8 = PL_reg_match_utf8;
2855 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2856 REGEXP *rex_sv = reginfo->prog;
2857 regexp *rex = (struct regexp *)SvANY(rex_sv);
2858 RXi_GET_DECL(rex,rexi);
2860 /* the current state. This is a cached copy of PL_regmatch_state */
2861 register regmatch_state *st;
2862 /* cache heavy used fields of st in registers */
2863 register regnode *scan;
2864 register regnode *next;
2865 register U32 n = 0; /* general value; init to avoid compiler warning */
2866 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2867 register char *locinput = PL_reginput;
2868 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2870 bool result = 0; /* return value of S_regmatch */
2871 int depth = 0; /* depth of backtrack stack */
2872 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2873 const U32 max_nochange_depth =
2874 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2875 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2876 regmatch_state *yes_state = NULL; /* state to pop to on success of
2878 /* mark_state piggy backs on the yes_state logic so that when we unwind
2879 the stack on success we can update the mark_state as we go */
2880 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2881 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2882 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2884 bool no_final = 0; /* prevent failure from backtracking? */
2885 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2886 char *startpoint = PL_reginput;
2887 SV *popmark = NULL; /* are we looking for a mark? */
2888 SV *sv_commit = NULL; /* last mark name seen in failure */
2889 SV *sv_yes_mark = NULL; /* last mark name we have seen
2890 during a successfull match */
2891 U32 lastopen = 0; /* last open we saw */
2892 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2893 SV* const oreplsv = GvSV(PL_replgv);
2894 /* these three flags are set by various ops to signal information to
2895 * the very next op. They have a useful lifetime of exactly one loop
2896 * iteration, and are not preserved or restored by state pushes/pops
2898 bool sw = 0; /* the condition value in (?(cond)a|b) */
2899 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2900 int logical = 0; /* the following EVAL is:
2904 or the following IFMATCH/UNLESSM is:
2905 false: plain (?=foo)
2906 true: used as a condition: (?(?=foo))
2909 GET_RE_DEBUG_FLAGS_DECL;
2912 PERL_ARGS_ASSERT_REGMATCH;
2914 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2915 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2917 /* on first ever call to regmatch, allocate first slab */
2918 if (!PL_regmatch_slab) {
2919 Newx(PL_regmatch_slab, 1, regmatch_slab);
2920 PL_regmatch_slab->prev = NULL;
2921 PL_regmatch_slab->next = NULL;
2922 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2925 oldsave = PL_savestack_ix;
2926 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2927 SAVEVPTR(PL_regmatch_slab);
2928 SAVEVPTR(PL_regmatch_state);
2930 /* grab next free state slot */
2931 st = ++PL_regmatch_state;
2932 if (st > SLAB_LAST(PL_regmatch_slab))
2933 st = PL_regmatch_state = S_push_slab(aTHX);
2935 /* Note that nextchr is a byte even in UTF */
2936 nextchr = UCHARAT(locinput);
2938 while (scan != NULL) {
2941 SV * const prop = sv_newmortal();
2942 regnode *rnext=regnext(scan);
2943 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2944 regprop(rex, prop, scan);
2946 PerlIO_printf(Perl_debug_log,
2947 "%3"IVdf":%*s%s(%"IVdf")\n",
2948 (IV)(scan - rexi->program), depth*2, "",
2950 (PL_regkind[OP(scan)] == END || !rnext) ?
2951 0 : (IV)(rnext - rexi->program));
2954 next = scan + NEXT_OFF(scan);
2957 state_num = OP(scan);
2961 assert(PL_reglastparen == &rex->lastparen);
2962 assert(PL_reglastcloseparen == &rex->lastcloseparen);
2963 assert(PL_regoffs == rex->offs);
2965 switch (state_num) {
2967 if (locinput == PL_bostr)
2969 /* reginfo->till = reginfo->bol; */
2974 if (locinput == PL_bostr ||
2975 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2981 if (locinput == PL_bostr)
2985 if (locinput == reginfo->ganch)
2990 /* update the startpoint */
2991 st->u.keeper.val = PL_regoffs[0].start;
2992 PL_reginput = locinput;
2993 PL_regoffs[0].start = locinput - PL_bostr;
2994 PUSH_STATE_GOTO(KEEPS_next, next);
2996 case KEEPS_next_fail:
2997 /* rollback the start point change */
2998 PL_regoffs[0].start = st->u.keeper.val;
3004 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3009 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3011 if (PL_regeol - locinput > 1)
3015 if (PL_regeol != locinput)
3019 if (!nextchr && locinput >= PL_regeol)
3022 locinput += PL_utf8skip[nextchr];
3023 if (locinput > PL_regeol)
3025 nextchr = UCHARAT(locinput);
3028 nextchr = UCHARAT(++locinput);
3031 if (!nextchr && locinput >= PL_regeol)
3033 nextchr = UCHARAT(++locinput);
3036 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3039 locinput += PL_utf8skip[nextchr];
3040 if (locinput > PL_regeol)
3042 nextchr = UCHARAT(locinput);
3045 nextchr = UCHARAT(++locinput);
3049 #define ST st->u.trie
3051 /* In this case the charclass data is available inline so
3052 we can fail fast without a lot of extra overhead.
3054 if (scan->flags == EXACT || !do_utf8) {
3055 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3057 PerlIO_printf(Perl_debug_log,
3058 "%*s %sfailed to match trie start class...%s\n",
3059 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3068 /* what type of TRIE am I? (utf8 makes this contextual) */
3069 DECL_TRIE_TYPE(scan);
3071 /* what trie are we using right now */
3072 reg_trie_data * const trie
3073 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3074 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3075 U32 state = trie->startstate;
3077 if (trie->bitmap && trie_type != trie_utf8_fold &&
3078 !TRIE_BITMAP_TEST(trie,*locinput)
3080 if (trie->states[ state ].wordnum) {
3082 PerlIO_printf(Perl_debug_log,
3083 "%*s %smatched empty string...%s\n",
3084 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3089 PerlIO_printf(Perl_debug_log,
3090 "%*s %sfailed to match trie start class...%s\n",
3091 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3098 U8 *uc = ( U8* )locinput;
3102 U8 *uscan = (U8*)NULL;
3104 SV *sv_accept_buff = NULL;
3105 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3107 ST.accepted = 0; /* how many accepting states we have seen */
3109 ST.jump = trie->jump;
3112 traverse the TRIE keeping track of all accepting states
3113 we transition through until we get to a failing node.
3116 while ( state && uc <= (U8*)PL_regeol ) {
3117 U32 base = trie->states[ state ].trans.base;
3120 /* We use charid to hold the wordnum as we don't use it
3121 for charid until after we have done the wordnum logic.
3122 We define an alias just so that the wordnum logic reads
3125 #define got_wordnum charid
3126 got_wordnum = trie->states[ state ].wordnum;
3128 if ( got_wordnum ) {
3129 if ( ! ST.accepted ) {
3131 SAVETMPS; /* XXX is this necessary? dmq */
3132 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
3133 sv_accept_buff=newSV(bufflen *
3134 sizeof(reg_trie_accepted) - 1);
3135 SvCUR_set(sv_accept_buff, 0);
3136 SvPOK_on(sv_accept_buff);
3137 sv_2mortal(sv_accept_buff);
3140 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
3143 if (ST.accepted >= bufflen) {
3145 ST.accept_buff =(reg_trie_accepted*)
3146 SvGROW(sv_accept_buff,
3147 bufflen * sizeof(reg_trie_accepted));
3149 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
3150 + sizeof(reg_trie_accepted));
3153 ST.accept_buff[ST.accepted].wordnum = got_wordnum;
3154 ST.accept_buff[ST.accepted].endpos = uc;
3156 } while (trie->nextword && (got_wordnum= trie->nextword[got_wordnum]));
3160 DEBUG_TRIE_EXECUTE_r({
3161 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
3162 PerlIO_printf( Perl_debug_log,
3163 "%*s %sState: %4"UVxf" Accepted: %4"UVxf" ",
3164 2+depth * 2, "", PL_colors[4],
3165 (UV)state, (UV)ST.accepted );
3169 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3170 uscan, len, uvc, charid, foldlen,
3174 (base + charid > trie->uniquecharcount )
3175 && (base + charid - 1 - trie->uniquecharcount
3177 && trie->trans[base + charid - 1 -
3178 trie->uniquecharcount].check == state)
3180 state = trie->trans[base + charid - 1 -
3181 trie->uniquecharcount ].next;
3192 DEBUG_TRIE_EXECUTE_r(
3193 PerlIO_printf( Perl_debug_log,
3194 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3195 charid, uvc, (UV)state, PL_colors[5] );
3202 PerlIO_printf( Perl_debug_log,
3203 "%*s %sgot %"IVdf" possible matches%s\n",
3204 REPORT_CODE_OFF + depth * 2, "",
3205 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3208 goto trie_first_try; /* jump into the fail handler */
3210 case TRIE_next_fail: /* we failed - try next alterative */
3212 REGCP_UNWIND(ST.cp);
3213 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3214 PL_regoffs[n].end = -1;
3215 *PL_reglastparen = n;
3224 ST.lastparen = *PL_reglastparen;
3227 if ( ST.accepted == 1 ) {
3228 /* only one choice left - just continue */
3230 AV *const trie_words
3231 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3232 SV ** const tmp = av_fetch( trie_words,
3233 ST.accept_buff[ 0 ].wordnum-1, 0 );
3234 SV *sv= tmp ? sv_newmortal() : NULL;
3236 PerlIO_printf( Perl_debug_log,
3237 "%*s %sonly one match left: #%d <%s>%s\n",
3238 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3239 ST.accept_buff[ 0 ].wordnum,
3240 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3241 PL_colors[0], PL_colors[1],
3242 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3244 : "not compiled under -Dr",
3247 PL_reginput = (char *)ST.accept_buff[ 0 ].endpos;
3248 /* in this case we free tmps/leave before we call regmatch
3249 as we wont be using accept_buff again. */
3251 locinput = PL_reginput;
3252 nextchr = UCHARAT(locinput);
3253 if ( !ST.jump || !ST.jump[ST.accept_buff[0].wordnum])
3256 scan = ST.me + ST.jump[ST.accept_buff[0].wordnum];
3257 if (!has_cutgroup) {
3262 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3265 continue; /* execute rest of RE */
3268 if ( !ST.accepted-- ) {
3270 PerlIO_printf( Perl_debug_log,
3271 "%*s %sTRIE failed...%s\n",
3272 REPORT_CODE_OFF+depth*2, "",
3283 There are at least two accepting states left. Presumably
3284 the number of accepting states is going to be low,
3285 typically two. So we simply scan through to find the one
3286 with lowest wordnum. Once we find it, we swap the last
3287 state into its place and decrement the size. We then try to
3288 match the rest of the pattern at the point where the word
3289 ends. If we succeed, control just continues along the
3290 regex; if we fail we return here to try the next accepting
3297 for( cur = 1 ; cur <= ST.accepted ; cur++ ) {
3298 DEBUG_TRIE_EXECUTE_r(
3299 PerlIO_printf( Perl_debug_log,
3300 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
3301 REPORT_CODE_OFF + depth * 2, "", PL_colors[4],
3302 (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur,
3303 ST.accept_buff[ cur ].wordnum, PL_colors[5] );
3306 if (ST.accept_buff[cur].wordnum <
3307 ST.accept_buff[best].wordnum)
3312 AV *const trie_words
3313 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3314 SV ** const tmp = av_fetch( trie_words,
3315 ST.accept_buff[ best ].wordnum - 1, 0 );
3316 regnode *nextop=(!ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) ?
3318 ST.me + ST.jump[ST.accept_buff[best].wordnum];
3319 SV *sv= tmp ? sv_newmortal() : NULL;
3321 PerlIO_printf( Perl_debug_log,
3322 "%*s %strying alternation #%d <%s> at node #%d %s\n",
3323 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3324 ST.accept_buff[best].wordnum,
3325 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3326 PL_colors[0], PL_colors[1],
3327 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3328 ) : "not compiled under -Dr",
3329 REG_NODE_NUM(nextop),
3333 if ( best<ST.accepted ) {
3334 reg_trie_accepted tmp = ST.accept_buff[ best ];
3335 ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ];
3336 ST.accept_buff[ ST.accepted ] = tmp;
3339 PL_reginput = (char *)ST.accept_buff[ best ].endpos;
3340 if ( !ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) {
3343 scan = ST.me + ST.jump[ST.accept_buff[best].wordnum];
3345 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3350 /* we dont want to throw this away, see bug 57042*/
3351 if (oreplsv != GvSV(PL_replgv))
3352 sv_setsv(oreplsv, GvSV(PL_replgv));
3359 char *s = STRING(scan);
3361 if (do_utf8 != UTF) {
3362 /* The target and the pattern have differing utf8ness. */
3364 const char * const e = s + ln;
3367 /* The target is utf8, the pattern is not utf8. */
3372 if (NATIVE_TO_UNI(*(U8*)s) !=
3373 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3381 /* The target is not utf8, the pattern is utf8. */
3386 if (NATIVE_TO_UNI(*((U8*)l)) !=
3387 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3395 nextchr = UCHARAT(locinput);
3398 /* The target and the pattern have the same utf8ness. */
3399 /* Inline the first character, for speed. */
3400 if (UCHARAT(s) != nextchr)
3402 if (PL_regeol - locinput < ln)
3404 if (ln > 1 && memNE(s, locinput, ln))
3407 nextchr = UCHARAT(locinput);
3411 PL_reg_flags |= RF_tainted;
3414 char * const s = STRING(scan);
3417 if (do_utf8 || UTF) {
3418 /* Either target or the pattern are utf8. */
3419 const char * const l = locinput;
3420 char *e = PL_regeol;
3422 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
3423 l, &e, 0, do_utf8)) {
3424 /* One more case for the sharp s:
3425 * pack("U0U*", 0xDF) =~ /ss/i,
3426 * the 0xC3 0x9F are the UTF-8
3427 * byte sequence for the U+00DF. */
3430 toLOWER(s[0]) == 's' &&
3432 toLOWER(s[1]) == 's' &&
3439 nextchr = UCHARAT(locinput);
3443 /* Neither the target and the pattern are utf8. */
3445 /* Inline the first character, for speed. */
3446 if (UCHARAT(s) != nextchr &&
3447 UCHARAT(s) != ((OP(scan) == EXACTF)
3448 ? PL_fold : PL_fold_locale)[nextchr])
3450 if (PL_regeol - locinput < ln)
3452 if (ln > 1 && (OP(scan) == EXACTF
3453 ? ibcmp(s, locinput, ln)
3454 : ibcmp_locale(s, locinput, ln)))
3457 nextchr = UCHARAT(locinput);
3462 PL_reg_flags |= RF_tainted;
3466 /* was last char in word? */
3468 if (locinput == PL_bostr)
3471 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3473 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3475 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3476 ln = isALNUM_uni(ln);
3477 LOAD_UTF8_CHARCLASS_ALNUM();
3478 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3481 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3482 n = isALNUM_LC_utf8((U8*)locinput);
3486 ln = (locinput != PL_bostr) ?
3487 UCHARAT(locinput - 1) : '\n';
3488 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3490 n = isALNUM(nextchr);
3493 ln = isALNUM_LC(ln);
3494 n = isALNUM_LC(nextchr);
3497 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3498 OP(scan) == BOUNDL))
3503 STRLEN inclasslen = PL_regeol - locinput;
3505 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3507 if (locinput >= PL_regeol)
3509 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3510 nextchr = UCHARAT(locinput);
3515 nextchr = UCHARAT(locinput);
3516 if (!REGINCLASS(rex, scan, (U8*)locinput))
3518 if (!nextchr && locinput >= PL_regeol)
3520 nextchr = UCHARAT(++locinput);
3524 /* If we might have the case of the German sharp s
3525 * in a casefolding Unicode character class. */
3527 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3528 locinput += SHARP_S_SKIP;
3529 nextchr = UCHARAT(locinput);
3534 /* Special char classes - The defines start on line 129 or so */
3535 CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3536 CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3538 CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3539 CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3541 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3542 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3544 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3545 a Unicode extended Grapheme Cluster */
3546 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3547 extended Grapheme Cluster is:
3550 | Prepend* Begin Extend*
3553 Begin is (Hangul-syllable | ! Control)
3554 Extend is (Grapheme_Extend | Spacing_Mark)
3555 Control is [ GCB_Control CR LF ]
3557 The discussion below shows how the code for CLUMP is derived
3558 from this regex. Note that most of these concepts are from
3559 property values of the Grapheme Cluster Boundary (GCB) property.
3560 No code point can have multiple property values for a given
3561 property. Thus a code point in Prepend can't be in Control, but
3562 it must be in !Control. This is why Control above includes
3563 GCB_Control plus CR plus LF. The latter two are used in the GCB
3564 property separately, and so can't be in GCB_Control, even though
3565 they logically are controls. Control is not the same as gc=cc,
3566 but includes format and other characters as well.
3568 The Unicode definition of Hangul-syllable is:
3570 | (L* ( ( V | LV ) V* | LVT ) T*)
3573 Each of these is a value for the GCB property, and hence must be
3574 disjoint, so the order they are tested is immaterial, so the
3575 above can safely be changed to
3578 | (L* ( LVT | ( V | LV ) V*) T*)
3580 The last two terms can be combined like this:
3582 | (( LVT | ( V | LV ) V*) T*))
3584 And refactored into this:
3585 L* (L | LVT T* | V V* T* | LV V* T*)
3587 That means that if we have seen any L's at all we can quit
3588 there, but if the next character is a LVT, a V or and LV we
3591 There is a subtlety with Prepend* which showed up in testing.
3592 Note that the Begin, and only the Begin is required in:
3593 | Prepend* Begin Extend*
3594 Also, Begin contains '! Control'. A Prepend must be a '!
3595 Control', which means it must be a Begin. What it comes down to
3596 is that if we match Prepend* and then find no suitable Begin
3597 afterwards, that if we backtrack the last Prepend, that one will
3598 be a suitable Begin.
3601 if (locinput >= PL_regeol)
3605 /* Match either CR LF or '.', as all the other possibilities
3607 locinput++; /* Match the . or CR */
3609 && locinput < PL_regeol
3610 && UCHARAT(locinput) == '\n') locinput++;
3614 /* Utf8: See if is ( CR LF ); already know that locinput <
3615 * PL_regeol, so locinput+1 is in bounds */
3616 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3620 /* In case have to backtrack to beginning, then match '.' */
3621 char *starting = locinput;
3623 /* In case have to backtrack the last prepend */
3624 char *previous_prepend = 0;
3626 LOAD_UTF8_CHARCLASS_GCB();
3628 /* Match (prepend)* */
3629 while (locinput < PL_regeol
3630 && swash_fetch(PL_utf8_X_prepend,
3631 (U8*)locinput, do_utf8))
3633 previous_prepend = locinput;
3634 locinput += UTF8SKIP(locinput);
3637 /* As noted above, if we matched a prepend character, but
3638 * the next thing won't match, back off the last prepend we
3639 * matched, as it is guaranteed to match the begin */
3640 if (previous_prepend
3641 && (locinput >= PL_regeol
3642 || ! swash_fetch(PL_utf8_X_begin,
3643 (U8*)locinput, do_utf8)))
3645 locinput = previous_prepend;
3648 /* Note that here we know PL_regeol > locinput, as we
3649 * tested that upon input to this switch case, and if we
3650 * moved locinput forward, we tested the result just above
3651 * and it either passed, or we backed off so that it will
3653 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, do_utf8)) {
3655 /* Here did not match the required 'Begin' in the
3656 * second term. So just match the very first
3657 * character, the '.' of the final term of the regex */
3658 locinput = starting + UTF8SKIP(starting);
3661 /* Here is the beginning of a character that can have
3662 * an extender. It is either a hangul syllable, or a
3664 if (swash_fetch(PL_utf8_X_non_hangul,
3665 (U8*)locinput, do_utf8))
3668 /* Here not a Hangul syllable, must be a
3669 * ('! * Control') */
3670 locinput += UTF8SKIP(locinput);
3673 /* Here is a Hangul syllable. It can be composed
3674 * of several individual characters. One
3675 * possibility is T+ */
3676 if (swash_fetch(PL_utf8_X_T,
3677 (U8*)locinput, do_utf8))
3679 while (locinput < PL_regeol
3680 && swash_fetch(PL_utf8_X_T,
3681 (U8*)locinput, do_utf8))
3683 locinput += UTF8SKIP(locinput);
3687 /* Here, not T+, but is a Hangul. That means
3688 * it is one of the others: L, LV, LVT or V,
3690 * L* (L | LVT T* | V V* T* | LV V* T*) */
3693 while (locinput < PL_regeol
3694 && swash_fetch(PL_utf8_X_L,
3695 (U8*)locinput, do_utf8))
3697 locinput += UTF8SKIP(locinput);
3700 /* Here, have exhausted L*. If the next
3701 * character is not an LV, LVT nor V, it means
3702 * we had to have at least one L, so matches L+
3703 * in the original equation, we have a complete
3704 * hangul syllable. Are done. */
3706 if (locinput < PL_regeol
3707 && swash_fetch(PL_utf8_X_LV_LVT_V,
3708 (U8*)locinput, do_utf8))
3711 /* Otherwise keep going. Must be LV, LVT
3712 * or V. See if LVT */
3713 if (swash_fetch(PL_utf8_X_LVT,
3714 (U8*)locinput, do_utf8))
3716 locinput += UTF8SKIP(locinput);
3719 /* Must be V or LV. Take it, then
3721 locinput += UTF8SKIP(locinput);
3722 while (locinput < PL_regeol
3723 && swash_fetch(PL_utf8_X_V,
3724 (U8*)locinput, do_utf8))
3726 locinput += UTF8SKIP(locinput);
3730 /* And any of LV, LVT, or V can be followed
3732 while (locinput < PL_regeol
3733 && swash_fetch(PL_utf8_X_T,
3737 locinput += UTF8SKIP(locinput);
3743 /* Match any extender */
3744 while (locinput < PL_regeol
3745 && swash_fetch(PL_utf8_X_extend,
3746 (U8*)locinput, do_utf8))
3748 locinput += UTF8SKIP(locinput);
3752 if (locinput > PL_regeol) sayNO;
3754 nextchr = UCHARAT(locinput);
3761 PL_reg_flags |= RF_tainted;
3766 n = reg_check_named_buff_matched(rex,scan);
3769 type = REF + ( type - NREF );
3776 PL_reg_flags |= RF_tainted;
3780 n = ARG(scan); /* which paren pair */
3783 ln = PL_regoffs[n].start;
3784 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3785 if (*PL_reglastparen < n || ln == -1)
3786 sayNO; /* Do not match unless seen CLOSEn. */
3787 if (ln == PL_regoffs[n].end)
3791 if (do_utf8 && type != REF) { /* REF can do byte comparison */
3793 const char *e = PL_bostr + PL_regoffs[n].end;
3795 * Note that we can't do the "other character" lookup trick as
3796 * in the 8-bit case (no pun intended) because in Unicode we
3797 * have to map both upper and title case to lower case.
3801 STRLEN ulen1, ulen2;
3802 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3803 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3807 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3808 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3809 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3816 nextchr = UCHARAT(locinput);
3820 /* Inline the first character, for speed. */
3821 if (UCHARAT(s) != nextchr &&
3823 (UCHARAT(s) != (type == REFF
3824 ? PL_fold : PL_fold_locale)[nextchr])))
3826 ln = PL_regoffs[n].end - ln;
3827 if (locinput + ln > PL_regeol)
3829 if (ln > 1 && (type == REF
3830 ? memNE(s, locinput, ln)
3832 ? ibcmp(s, locinput, ln)
3833 : ibcmp_locale(s, locinput, ln))))
3836 nextchr = UCHARAT(locinput);
3846 #define ST st->u.eval
3851 regexp_internal *rei;
3852 regnode *startpoint;
3855 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3856 if (cur_eval && cur_eval->locinput==locinput) {
3857 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3858 Perl_croak(aTHX_ "Infinite recursion in regex");
3859 if ( ++nochange_depth > max_nochange_depth )
3861 "Pattern subroutine nesting without pos change"
3862 " exceeded limit in regex");
3869 (void)ReREFCNT_inc(rex_sv);
3870 if (OP(scan)==GOSUB) {
3871 startpoint = scan + ARG2L(scan);
3872 ST.close_paren = ARG(scan);
3874 startpoint = rei->program+1;
3877 goto eval_recurse_doit;
3879 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3880 if (cur_eval && cur_eval->locinput==locinput) {
3881 if ( ++nochange_depth > max_nochange_depth )
3882 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3887 /* execute the code in the {...} */
3889 SV ** const before = SP;
3890 OP_4tree * const oop = PL_op;
3891 COP * const ocurcop = PL_curcop;
3893 char *saved_regeol = PL_regeol;
3896 PL_op = (OP_4tree*)rexi->data->data[n];
3897 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3898 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3899 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3900 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3903 SV *sv_mrk = get_sv("REGMARK", 1);
3904 sv_setsv(sv_mrk, sv_yes_mark);
3907 CALLRUNOPS(aTHX); /* Scalar context. */
3910 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3917 PAD_RESTORE_LOCAL(old_comppad);
3918 PL_curcop = ocurcop;
3919 PL_regeol = saved_regeol;
3922 sv_setsv(save_scalar(PL_replgv), ret);
3926 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
3929 /* extract RE object from returned value; compiling if
3935 SV *const sv = SvRV(ret);
3937 if (SvTYPE(sv) == SVt_REGEXP) {
3939 } else if (SvSMAGICAL(sv)) {
3940 mg = mg_find(sv, PERL_MAGIC_qr);
3943 } else if (SvTYPE(ret) == SVt_REGEXP) {
3945 } else if (SvSMAGICAL(ret)) {
3946 if (SvGMAGICAL(ret)) {
3947 /* I don't believe that there is ever qr magic
3949 assert(!mg_find(ret, PERL_MAGIC_qr));
3950 sv_unmagic(ret, PERL_MAGIC_qr);
3953 mg = mg_find(ret, PERL_MAGIC_qr);
3954 /* testing suggests mg only ends up non-NULL for
3955 scalars who were upgraded and compiled in the
3956 else block below. In turn, this is only
3957 triggered in the "postponed utf8 string" tests
3963 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
3967 rx = reg_temp_copy(NULL, rx);
3971 const I32 osize = PL_regsize;
3974 assert (SvUTF8(ret));
3975 } else if (SvUTF8(ret)) {
3976 /* Not doing UTF-8, despite what the SV says. Is
3977 this only if we're trapped in use 'bytes'? */
3978 /* Make a copy of the octet sequence, but without
3979 the flag on, as the compiler now honours the
3980 SvUTF8 flag on ret. */
3982 const char *const p = SvPV(ret, len);
3983 ret = newSVpvn_flags(p, len, SVs_TEMP);
3985 rx = CALLREGCOMP(ret, pm_flags);
3987 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3989 /* This isn't a first class regexp. Instead, it's
3990 caching a regexp onto an existing, Perl visible
3992 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
3997 re = (struct regexp *)SvANY(rx);
3999 RXp_MATCH_COPIED_off(re);
4000 re->subbeg = rex->subbeg;
4001 re->sublen = rex->sublen;
4004 debug_start_match(re_sv, do_utf8, locinput, PL_regeol,
4005 "Matching embedded");
4007 startpoint = rei->program + 1;
4008 ST.close_paren = 0; /* only used for GOSUB */
4009 /* borrowed from regtry */
4010 if (PL_reg_start_tmpl <= re->nparens) {
4011 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4012 if(PL_reg_start_tmp)
4013 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4015 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4018 eval_recurse_doit: /* Share code with GOSUB below this line */
4019 /* run the pattern returned from (??{...}) */
4020 ST.cp = regcppush(0); /* Save *all* the positions. */
4021 REGCP_SET(ST.lastcp);
4023 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
4025 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4026 PL_reglastparen = &re->lastparen;
4027 PL_reglastcloseparen = &re->lastcloseparen;
4029 re->lastcloseparen = 0;
4031 PL_reginput = locinput;
4034 /* XXXX This is too dramatic a measure... */
4037 ST.toggle_reg_flags = PL_reg_flags;
4039 PL_reg_flags |= RF_utf8;
4041 PL_reg_flags &= ~RF_utf8;
4042 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4044 ST.prev_rex = rex_sv;
4045 ST.prev_curlyx = cur_curlyx;
4046 SETREX(rex_sv,re_sv);
4051 ST.prev_eval = cur_eval;
4053 /* now continue from first node in postoned RE */
4054 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4057 /* logical is 1, /(?(?{...})X|Y)/ */
4058 sw = (bool)SvTRUE(ret);
4063 case EVAL_AB: /* cleanup after a successful (??{A})B */
4064 /* note: this is called twice; first after popping B, then A */
4065 PL_reg_flags ^= ST.toggle_reg_flags;
4066 ReREFCNT_dec(rex_sv);
4067 SETREX(rex_sv,ST.prev_rex);
4068 rex = (struct regexp *)SvANY(rex_sv);
4069 rexi = RXi_GET(rex);
4071 cur_eval = ST.prev_eval;
4072 cur_curlyx = ST.prev_curlyx;
4074 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4075 PL_reglastparen = &rex->lastparen;
4076 PL_reglastcloseparen = &rex->lastcloseparen;
4077 /* also update PL_regoffs */
4078 PL_regoffs = rex->offs;
4080 /* XXXX This is too dramatic a measure... */
4082 if ( nochange_depth )
4087 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4088 /* note: this is called twice; first after popping B, then A */
4089 PL_reg_flags ^= ST.toggle_reg_flags;
4090 ReREFCNT_dec(rex_sv);
4091 SETREX(rex_sv,ST.prev_rex);
4092 rex = (struct regexp *)SvANY(rex_sv);
4093 rexi = RXi_GET(rex);
4094 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4095 PL_reglastparen = &rex->lastparen;
4096 PL_reglastcloseparen = &rex->lastcloseparen;
4098 PL_reginput = locinput;
4099 REGCP_UNWIND(ST.lastcp);
4101 cur_eval = ST.prev_eval;
4102 cur_curlyx = ST.prev_curlyx;
4103 /* XXXX This is too dramatic a measure... */
4105 if ( nochange_depth )
4111 n = ARG(scan); /* which paren pair */
4112 PL_reg_start_tmp[n] = locinput;
4118 n = ARG(scan); /* which paren pair */
4119 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4120 PL_regoffs[n].end = locinput - PL_bostr;
4121 /*if (n > PL_regsize)
4123 if (n > *PL_reglastparen)
4124 *PL_reglastparen = n;
4125 *PL_reglastcloseparen = n;
4126 if (cur_eval && cur_eval->u.eval.close_paren == n) {
4134 cursor && OP(cursor)!=END;
4135 cursor=regnext(cursor))
4137 if ( OP(cursor)==CLOSE ){
4139 if ( n <= lastopen ) {
4141 = PL_reg_start_tmp[n] - PL_bostr;
4142 PL_regoffs[n].end = locinput - PL_bostr;
4143 /*if (n > PL_regsize)
4145 if (n > *PL_reglastparen)
4146 *PL_reglastparen = n;
4147 *PL_reglastcloseparen = n;
4148 if ( n == ARG(scan) || (cur_eval &&
4149 cur_eval->u.eval.close_paren == n))
4158 n = ARG(scan); /* which paren pair */
4159 sw = (bool)(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
4162 /* reg_check_named_buff_matched returns 0 for no match */
4163 sw = (bool)(0 < reg_check_named_buff_matched(rex,scan));
4167 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
4173 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
4175 next = NEXTOPER(NEXTOPER(scan));
4177 next = scan + ARG(scan);
4178 if (OP(next) == IFTHEN) /* Fake one. */
4179 next = NEXTOPER(NEXTOPER(next));
4183 logical = scan->flags;
4186 /*******************************************************************
4188 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4189 pattern, where A and B are subpatterns. (For simple A, CURLYM or
4190 STAR/PLUS/CURLY/CURLYN are used instead.)
4192 A*B is compiled as <CURLYX><A><WHILEM><B>
4194 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4195 state, which contains the current count, initialised to -1. It also sets
4196 cur_curlyx to point to this state, with any previous value saved in the
4199 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4200 since the pattern may possibly match zero times (i.e. it's a while {} loop
4201 rather than a do {} while loop).
4203 Each entry to WHILEM represents a successful match of A. The count in the
4204 CURLYX block is incremented, another WHILEM state is pushed, and execution
4205 passes to A or B depending on greediness and the current count.
4207 For example, if matching against the string a1a2a3b (where the aN are
4208 substrings that match /A/), then the match progresses as follows: (the
4209 pushed states are interspersed with the bits of strings matched so far):
4212 <CURLYX cnt=0><WHILEM>
4213 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4214 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4215 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4216 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4218 (Contrast this with something like CURLYM, which maintains only a single
4222 a1 <CURLYM cnt=1> a2
4223 a1 a2 <CURLYM cnt=2> a3
4224 a1 a2 a3 <CURLYM cnt=3> b
4227 Each WHILEM state block marks a point to backtrack to upon partial failure
4228 of A or B, and also contains some minor state data related to that
4229 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4230 overall state, such as the count, and pointers to the A and B ops.
4232 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4233 must always point to the *current* CURLYX block, the rules are:
4235 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4236 and set cur_curlyx to point the new block.
4238 When popping the CURLYX block after a successful or unsuccessful match,
4239 restore the previous cur_curlyx.
4241 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4242 to the outer one saved in the CURLYX block.
4244 When popping the WHILEM block after a successful or unsuccessful B match,
4245 restore the previous cur_curlyx.
4247 Here's an example for the pattern (AI* BI)*BO
4248 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4251 curlyx backtrack stack
4252 ------ ---------------
4254 CO <CO prev=NULL> <WO>
4255 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4256 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4257 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4259 At this point the pattern succeeds, and we work back down the stack to
4260 clean up, restoring as we go:
4262 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4263 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4264 CO <CO prev=NULL> <WO>
4267 *******************************************************************/
4269 #define ST st->u.curlyx
4271 case CURLYX: /* start of /A*B/ (for complex A) */
4273 /* No need to save/restore up to this paren */
4274 I32 parenfloor = scan->flags;
4276 assert(next); /* keep Coverity happy */
4277 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4280 /* XXXX Probably it is better to teach regpush to support
4281 parenfloor > PL_regsize... */
4282 if (parenfloor > (I32)*PL_reglastparen)
4283 parenfloor = *PL_reglastparen; /* Pessimization... */
4285 ST.prev_curlyx= cur_curlyx;
4287 ST.cp = PL_savestack_ix;
4289 /* these fields contain the state of the current curly.
4290 * they are accessed by subsequent WHILEMs */
4291 ST.parenfloor = parenfloor;
4292 ST.min = ARG1(scan);
4293 ST.max = ARG2(scan);
4294 ST.A = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
4298 ST.count = -1; /* this will be updated by WHILEM */
4299 ST.lastloc = NULL; /* this will be updated by WHILEM */
4301 PL_reginput = locinput;
4302 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4306 case CURLYX_end: /* just finished matching all of A*B */
4307 cur_curlyx = ST.prev_curlyx;
4311 case CURLYX_end_fail: /* just failed to match all of A*B */
4313 cur_curlyx = ST.prev_curlyx;
4319 #define ST st->u.whilem
4321 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4323 /* see the discussion above about CURLYX/WHILEM */
4325 assert(cur_curlyx); /* keep Coverity happy */
4326 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4327 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4328 ST.cache_offset = 0;
4331 PL_reginput = locinput;
4333 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4334 "%*s whilem: matched %ld out of %ld..%ld\n",
4335 REPORT_CODE_OFF+depth*2, "", (long)n,
4336 (long)cur_curlyx->u.curlyx.min,
4337 (long)cur_curlyx->u.curlyx.max)
4340 /* First just match a string of min A's. */
4342 if (n < cur_curlyx->u.curlyx.min) {
4343 cur_curlyx->u.curlyx.lastloc = locinput;
4344 PUSH_STATE_GOTO(WHILEM_A_pre, cur_curlyx->u.curlyx.A);
4348 /* If degenerate A matches "", assume A done. */
4350 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4351 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4352 "%*s whilem: empty match detected, trying continuation...\n",
4353 REPORT_CODE_OFF+depth*2, "")
4355 goto do_whilem_B_max;
4358 /* super-linear cache processing */
4362 if (!PL_reg_maxiter) {
4363 /* start the countdown: Postpone detection until we
4364 * know the match is not *that* much linear. */
4365 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4366 /* possible overflow for long strings and many CURLYX's */
4367 if (PL_reg_maxiter < 0)
4368 PL_reg_maxiter = I32_MAX;
4369 PL_reg_leftiter = PL_reg_maxiter;
4372 if (PL_reg_leftiter-- == 0) {
4373 /* initialise cache */
4374 const I32 size = (PL_reg_maxiter + 7)/8;
4375 if (PL_reg_poscache) {
4376 if ((I32)PL_reg_poscache_size < size) {
4377 Renew(PL_reg_poscache, size, char);
4378 PL_reg_poscache_size = size;
4380 Zero(PL_reg_poscache, size, char);
4383 PL_reg_poscache_size = size;
4384 Newxz(PL_reg_poscache, size, char);
4386 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4387 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4388 PL_colors[4], PL_colors[5])
4392 if (PL_reg_leftiter < 0) {
4393 /* have we already failed at this position? */
4395 offset = (scan->flags & 0xf) - 1
4396 + (locinput - PL_bostr) * (scan->flags>>4);
4397 mask = 1 << (offset % 8);
4399 if (PL_reg_poscache[offset] & mask) {
4400 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4401 "%*s whilem: (cache) already tried at this position...\n",
4402 REPORT_CODE_OFF+depth*2, "")
4404 sayNO; /* cache records failure */
4406 ST.cache_offset = offset;
4407 ST.cache_mask = mask;
4411 /* Prefer B over A for minimal matching. */
4413 if (cur_curlyx->u.curlyx.minmod) {
4414 ST.save_curlyx = cur_curlyx;
4415 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4416 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4417 REGCP_SET(ST.lastcp);
4418 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4422 /* Prefer A over B for maximal matching. */
4424 if (n < cur_curlyx->u.curlyx.max) { /* More greed allowed? */
4425 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4426 cur_curlyx->u.curlyx.lastloc = locinput;
4427 REGCP_SET(ST.lastcp);
4428 PUSH_STATE_GOTO(WHILEM_A_max, cur_curlyx->u.curlyx.A);
4431 goto do_whilem_B_max;
4435 case WHILEM_B_min: /* just matched B in a minimal match */
4436 case WHILEM_B_max: /* just matched B in a maximal match */
4437 cur_curlyx = ST.save_curlyx;
4441 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4442 cur_curlyx = ST.save_curlyx;
4443 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4444 cur_curlyx->u.curlyx.count--;
4448 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4449 REGCP_UNWIND(ST.lastcp);
4452 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4453 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4454 cur_curlyx->u.curlyx.count--;
4458 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4459 REGCP_UNWIND(ST.lastcp);
4460 regcppop(rex); /* Restore some previous $<digit>s? */
4461 PL_reginput = locinput;
4462 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4463 "%*s whilem: failed, trying continuation...\n",
4464 REPORT_CODE_OFF+depth*2, "")
4467 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4468 && ckWARN(WARN_REGEXP)
4469 && !(PL_reg_flags & RF_warned))
4471 PL_reg_flags |= RF_warned;
4472 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4473 "Complex regular subexpression recursion",
4478 ST.save_curlyx = cur_curlyx;
4479 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4480 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4483 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4484 cur_curlyx = ST.save_curlyx;
4485 REGCP_UNWIND(ST.lastcp);
4488 if (cur_curlyx->u.curlyx.count >= cur_curlyx->u.curlyx.max) {
4489 /* Maximum greed exceeded */
4490 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4491 && ckWARN(WARN_REGEXP)
4492 && !(PL_reg_flags & RF_warned))
4494 PL_reg_flags |= RF_warned;
4495 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4496 "%s limit (%d) exceeded",
4497 "Complex regular subexpression recursion",
4500 cur_curlyx->u.curlyx.count--;
4504 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4505 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4507 /* Try grabbing another A and see if it helps. */
4508 PL_reginput = locinput;
4509 cur_curlyx->u.curlyx.lastloc = locinput;
4510 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4511 REGCP_SET(ST.lastcp);
4512 PUSH_STATE_GOTO(WHILEM_A_min, ST.save_curlyx->u.curlyx.A);
4516 #define ST st->u.branch
4518 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4519 next = scan + ARG(scan);
4522 scan = NEXTOPER(scan);
4525 case BRANCH: /* /(...|A|...)/ */
4526 scan = NEXTOPER(scan); /* scan now points to inner node */
4527 ST.lastparen = *PL_reglastparen;
4528 ST.next_branch = next;
4530 PL_reginput = locinput;
4532 /* Now go into the branch */
4534 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4536 PUSH_STATE_GOTO(BRANCH_next, scan);
4540 PL_reginput = locinput;
4541 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4542 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4543 PUSH_STATE_GOTO(CUTGROUP_next,next);
4545 case CUTGROUP_next_fail:
4548 if (st->u.mark.mark_name)
4549 sv_commit = st->u.mark.mark_name;
4555 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4560 REGCP_UNWIND(ST.cp);
4561 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4562 PL_regoffs[n].end = -1;
4563 *PL_reglastparen = n;
4564 /*dmq: *PL_reglastcloseparen = n; */
4565 scan = ST.next_branch;
4566 /* no more branches? */
4567 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4569 PerlIO_printf( Perl_debug_log,
4570 "%*s %sBRANCH failed...%s\n",
4571 REPORT_CODE_OFF+depth*2, "",
4577 continue; /* execute next BRANCH[J] op */
4585 #define ST st->u.curlym
4587 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4589 /* This is an optimisation of CURLYX that enables us to push
4590 * only a single backtracking state, no matter how many matches
4591 * there are in {m,n}. It relies on the pattern being constant
4592 * length, with no parens to influence future backrefs
4596 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4598 /* if paren positive, emulate an OPEN/CLOSE around A */
4600 U32 paren = ST.me->flags;
4601 if (paren > PL_regsize)
4603 if (paren > *PL_reglastparen)
4604 *PL_reglastparen = paren;
4605 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4613 ST.c1 = CHRTEST_UNINIT;
4616 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4619 curlym_do_A: /* execute the A in /A{m,n}B/ */
4620 PL_reginput = locinput;
4621 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4624 case CURLYM_A: /* we've just matched an A */
4625 locinput = st->locinput;
4626 nextchr = UCHARAT(locinput);
4629 /* after first match, determine A's length: u.curlym.alen */
4630 if (ST.count == 1) {
4631 if (PL_reg_match_utf8) {
4633 while (s < PL_reginput) {
4639 ST.alen = PL_reginput - locinput;
4642 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4645 PerlIO_printf(Perl_debug_log,
4646 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4647 (int)(REPORT_CODE_OFF+(depth*2)), "",
4648 (IV) ST.count, (IV)ST.alen)
4651 locinput = PL_reginput;
4653 if (cur_eval && cur_eval->u.eval.close_paren &&
4654 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4658 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4659 if ( max == REG_INFTY || ST.count < max )
4660 goto curlym_do_A; /* try to match another A */
4662 goto curlym_do_B; /* try to match B */
4664 case CURLYM_A_fail: /* just failed to match an A */
4665 REGCP_UNWIND(ST.cp);
4667 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4668 || (cur_eval && cur_eval->u.eval.close_paren &&
4669 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4672 curlym_do_B: /* execute the B in /A{m,n}B/ */
4673 PL_reginput = locinput;
4674 if (ST.c1 == CHRTEST_UNINIT) {
4675 /* calculate c1 and c2 for possible match of 1st char
4676 * following curly */
4677 ST.c1 = ST.c2 = CHRTEST_VOID;
4678 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4679 regnode *text_node = ST.B;
4680 if (! HAS_TEXT(text_node))
4681 FIND_NEXT_IMPT(text_node);
4684 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4686 But the former is redundant in light of the latter.
4688 if this changes back then the macro for
4689 IS_TEXT and friends need to change.
4691 if (PL_regkind[OP(text_node)] == EXACT)
4694 ST.c1 = (U8)*STRING(text_node);
4696 (IS_TEXTF(text_node))
4698 : (IS_TEXTFL(text_node))
4699 ? PL_fold_locale[ST.c1]
4706 PerlIO_printf(Perl_debug_log,
4707 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4708 (int)(REPORT_CODE_OFF+(depth*2)),
4711 if (ST.c1 != CHRTEST_VOID
4712 && UCHARAT(PL_reginput) != ST.c1
4713 && UCHARAT(PL_reginput) != ST.c2)
4715 /* simulate B failing */
4717 PerlIO_printf(Perl_debug_log,
4718 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4719 (int)(REPORT_CODE_OFF+(depth*2)),"",
4722 state_num = CURLYM_B_fail;
4723 goto reenter_switch;
4727 /* mark current A as captured */
4728 I32 paren = ST.me->flags;
4730 PL_regoffs[paren].start
4731 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4732 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4733 /*dmq: *PL_reglastcloseparen = paren; */
4736 PL_regoffs[paren].end = -1;
4737 if (cur_eval && cur_eval->u.eval.close_paren &&
4738 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4747 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4750 case CURLYM_B_fail: /* just failed to match a B */
4751 REGCP_UNWIND(ST.cp);
4753 I32 max = ARG2(ST.me);
4754 if (max != REG_INFTY && ST.count == max)
4756 goto curlym_do_A; /* try to match a further A */
4758 /* backtrack one A */
4759 if (ST.count == ARG1(ST.me) /* min */)
4762 locinput = HOPc(locinput, -ST.alen);
4763 goto curlym_do_B; /* try to match B */
4766 #define ST st->u.curly
4768 #define CURLY_SETPAREN(paren, success) \
4771 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4772 PL_regoffs[paren].end = locinput - PL_bostr; \
4773 *PL_reglastcloseparen = paren; \
4776 PL_regoffs[paren].end = -1; \
4779 case STAR: /* /A*B/ where A is width 1 */
4783 scan = NEXTOPER(scan);
4785 case PLUS: /* /A+B/ where A is width 1 */
4789 scan = NEXTOPER(scan);
4791 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4792 ST.paren = scan->flags; /* Which paren to set */
4793 if (ST.paren > PL_regsize)
4794 PL_regsize = ST.paren;
4795 if (ST.paren > *PL_reglastparen)
4796 *PL_reglastparen = ST.paren;
4797 ST.min = ARG1(scan); /* min to match */
4798 ST.max = ARG2(scan); /* max to match */
4799 if (cur_eval && cur_eval->u.eval.close_paren &&
4800 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4804 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4806 case CURLY: /* /A{m,n}B/ where A is width 1 */
4808 ST.min = ARG1(scan); /* min to match */
4809 ST.max = ARG2(scan); /* max to match */
4810 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4813 * Lookahead to avoid useless match attempts
4814 * when we know what character comes next.
4816 * Used to only do .*x and .*?x, but now it allows
4817 * for )'s, ('s and (?{ ... })'s to be in the way
4818 * of the quantifier and the EXACT-like node. -- japhy
4821 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4823 if (HAS_TEXT(next) || JUMPABLE(next)) {
4825 regnode *text_node = next;
4827 if (! HAS_TEXT(text_node))
4828 FIND_NEXT_IMPT(text_node);
4830 if (! HAS_TEXT(text_node))
4831 ST.c1 = ST.c2 = CHRTEST_VOID;
4833 if ( PL_regkind[OP(text_node)] != EXACT ) {
4834 ST.c1 = ST.c2 = CHRTEST_VOID;
4835 goto assume_ok_easy;
4838 s = (U8*)STRING(text_node);
4840 /* Currently we only get here when
4842 PL_rekind[OP(text_node)] == EXACT
4844 if this changes back then the macro for IS_TEXT and
4845 friends need to change. */
4848 if (IS_TEXTF(text_node))
4849 ST.c2 = PL_fold[ST.c1];
4850 else if (IS_TEXTFL(text_node))
4851 ST.c2 = PL_fold_locale[ST.c1];
4854 if (IS_TEXTF(text_node)) {
4855 STRLEN ulen1, ulen2;
4856 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4857 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4859 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4860 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4862 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4864 0 : UTF8_ALLOW_ANY);
4865 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4867 0 : UTF8_ALLOW_ANY);
4869 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4871 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4876 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4883 ST.c1 = ST.c2 = CHRTEST_VOID;
4888 PL_reginput = locinput;
4891 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4894 locinput = PL_reginput;
4896 if (ST.c1 == CHRTEST_VOID)
4897 goto curly_try_B_min;
4899 ST.oldloc = locinput;
4901 /* set ST.maxpos to the furthest point along the
4902 * string that could possibly match */
4903 if (ST.max == REG_INFTY) {
4904 ST.maxpos = PL_regeol - 1;
4906 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4910 int m = ST.max - ST.min;
4911 for (ST.maxpos = locinput;
4912 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4913 ST.maxpos += UTF8SKIP(ST.maxpos);
4916 ST.maxpos = locinput + ST.max - ST.min;
4917 if (ST.maxpos >= PL_regeol)
4918 ST.maxpos = PL_regeol - 1;
4920 goto curly_try_B_min_known;
4924 ST.count = regrepeat(rex, ST.A, ST.max, depth);
4925 locinput = PL_reginput;
4926 if (ST.count < ST.min)
4928 if ((ST.count > ST.min)
4929 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4931 /* A{m,n} must come at the end of the string, there's
4932 * no point in backing off ... */
4934 /* ...except that $ and \Z can match before *and* after
4935 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4936 We may back off by one in this case. */
4937 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4941 goto curly_try_B_max;
4946 case CURLY_B_min_known_fail:
4947 /* failed to find B in a non-greedy match where c1,c2 valid */
4948 if (ST.paren && ST.count)
4949 PL_regoffs[ST.paren].end = -1;
4951 PL_reginput = locinput; /* Could be reset... */
4952 REGCP_UNWIND(ST.cp);
4953 /* Couldn't or didn't -- move forward. */
4954 ST.oldloc = locinput;
4956 locinput += UTF8SKIP(locinput);
4960 curly_try_B_min_known:
4961 /* find the next place where 'B' could work, then call B */
4965 n = (ST.oldloc == locinput) ? 0 : 1;
4966 if (ST.c1 == ST.c2) {
4968 /* set n to utf8_distance(oldloc, locinput) */
4969 while (locinput <= ST.maxpos &&
4970 utf8n_to_uvchr((U8*)locinput,
4971 UTF8_MAXBYTES, &len,
4972 uniflags) != (UV)ST.c1) {
4978 /* set n to utf8_distance(oldloc, locinput) */
4979 while (locinput <= ST.maxpos) {
4981 const UV c = utf8n_to_uvchr((U8*)locinput,
4982 UTF8_MAXBYTES, &len,
4984 if (c == (UV)ST.c1 || c == (UV)ST.c2)
4992 if (ST.c1 == ST.c2) {
4993 while (locinput <= ST.maxpos &&
4994 UCHARAT(locinput) != ST.c1)
4998 while (locinput <= ST.maxpos
4999 && UCHARAT(locinput) != ST.c1
5000 && UCHARAT(locinput) != ST.c2)
5003 n = locinput - ST.oldloc;
5005 if (locinput > ST.maxpos)
5007 /* PL_reginput == oldloc now */
5010 if (regrepeat(rex, ST.A, n, depth) < n)
5013 PL_reginput = locinput;
5014 CURLY_SETPAREN(ST.paren, ST.count);
5015 if (cur_eval && cur_eval->u.eval.close_paren &&
5016 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5019 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
5024 case CURLY_B_min_fail:
5025 /* failed to find B in a non-greedy match where c1,c2 invalid */
5026 if (ST.paren && ST.count)
5027 PL_regoffs[ST.paren].end = -1;
5029 REGCP_UNWIND(ST.cp);
5030 /* failed -- move forward one */
5031 PL_reginput = locinput;
5032 if (regrepeat(rex, ST.A, 1, depth)) {
5034 locinput = PL_reginput;
5035 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5036 ST.count > 0)) /* count overflow ? */
5039 CURLY_SETPAREN(ST.paren, ST.count);
5040 if (cur_eval && cur_eval->u.eval.close_paren &&
5041 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5044 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
5052 /* a successful greedy match: now try to match B */
5053 if (cur_eval && cur_eval->u.eval.close_paren &&
5054 cur_eval->u.eval.close_paren == (U32)ST.paren) {
5059 if (ST.c1 != CHRTEST_VOID)
5060 c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput,
5061 UTF8_MAXBYTES, 0, uniflags)
5062 : (UV) UCHARAT(PL_reginput);
5063 /* If it could work, try it. */
5064 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5065 CURLY_SETPAREN(ST.paren, ST.count);
5066 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5071 case CURLY_B_max_fail:
5072 /* failed to find B in a greedy match */
5073 if (ST.paren && ST.count)
5074 PL_regoffs[ST.paren].end = -1;
5076 REGCP_UNWIND(ST.cp);
5078 if (--ST.count < ST.min)
5080 PL_reginput = locinput = HOPc(locinput, -1);
5081 goto curly_try_B_max;
5088 /* we've just finished A in /(??{A})B/; now continue with B */
5090 st->u.eval.toggle_reg_flags
5091 = cur_eval->u.eval.toggle_reg_flags;
5092 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5094 st->u.eval.prev_rex = rex_sv; /* inner */
5095 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5096 rex = (struct regexp *)SvANY(rex_sv);
5097 rexi = RXi_GET(rex);
5098 cur_curlyx = cur_eval->u.eval.prev_curlyx;
5099 ReREFCNT_inc(rex_sv);
5100 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
5102 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5103 PL_reglastparen = &rex->lastparen;
5104 PL_reglastcloseparen = &rex->lastcloseparen;
5106 REGCP_SET(st->u.eval.lastcp);
5107 PL_reginput = locinput;
5109 /* Restore parens of the outer rex without popping the
5111 tmpix = PL_savestack_ix;
5112 PL_savestack_ix = cur_eval->u.eval.lastcp;
5114 PL_savestack_ix = tmpix;
5116 st->u.eval.prev_eval = cur_eval;
5117 cur_eval = cur_eval->u.eval.prev_eval;
5119 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5120 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
5121 if ( nochange_depth )
5124 PUSH_YES_STATE_GOTO(EVAL_AB,
5125 st->u.eval.prev_eval->u.eval.B); /* match B */
5128 if (locinput < reginfo->till) {
5129 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5130 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5132 (long)(locinput - PL_reg_starttry),
5133 (long)(reginfo->till - PL_reg_starttry),
5136 sayNO_SILENT; /* Cannot match: too short. */
5138 PL_reginput = locinput; /* put where regtry can find it */
5139 sayYES; /* Success! */
5141 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5143 PerlIO_printf(Perl_debug_log,
5144 "%*s %ssubpattern success...%s\n",
5145 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
5146 PL_reginput = locinput; /* put where regtry can find it */
5147 sayYES; /* Success! */
5150 #define ST st->u.ifmatch
5152 case SUSPEND: /* (?>A) */
5154 PL_reginput = locinput;
5157 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5159 goto ifmatch_trivial_fail_test;
5161 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5163 ifmatch_trivial_fail_test:
5165 char * const s = HOPBACKc(locinput, scan->flags);
5170 sw = 1 - (bool)ST.wanted;
5174 next = scan + ARG(scan);
5182 PL_reginput = locinput;
5186 ST.logical = logical;
5187 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5189 /* execute body of (?...A) */
5190 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5193 case IFMATCH_A_fail: /* body of (?...A) failed */
5194 ST.wanted = !ST.wanted;
5197 case IFMATCH_A: /* body of (?...A) succeeded */
5199 sw = (bool)ST.wanted;
5201 else if (!ST.wanted)
5204 if (OP(ST.me) == SUSPEND)
5205 locinput = PL_reginput;
5207 locinput = PL_reginput = st->locinput;
5208 nextchr = UCHARAT(locinput);
5210 scan = ST.me + ARG(ST.me);
5213 continue; /* execute B */
5218 next = scan + ARG(scan);
5223 reginfo->cutpoint = PL_regeol;
5226 PL_reginput = locinput;
5228 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5229 PUSH_STATE_GOTO(COMMIT_next,next);
5231 case COMMIT_next_fail:
5238 #define ST st->u.mark
5240 ST.prev_mark = mark_state;
5241 ST.mark_name = sv_commit = sv_yes_mark
5242 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5244 ST.mark_loc = PL_reginput = locinput;
5245 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5247 case MARKPOINT_next:
5248 mark_state = ST.prev_mark;
5251 case MARKPOINT_next_fail:
5252 if (popmark && sv_eq(ST.mark_name,popmark))
5254 if (ST.mark_loc > startpoint)
5255 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5256 popmark = NULL; /* we found our mark */
5257 sv_commit = ST.mark_name;
5260 PerlIO_printf(Perl_debug_log,
5261 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5262 REPORT_CODE_OFF+depth*2, "",
5263 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5266 mark_state = ST.prev_mark;
5267 sv_yes_mark = mark_state ?
5268 mark_state->u.mark.mark_name : NULL;
5272 PL_reginput = locinput;
5274 /* (*SKIP) : if we fail we cut here*/
5275 ST.mark_name = NULL;
5276 ST.mark_loc = locinput;
5277 PUSH_STATE_GOTO(SKIP_next,next);
5279 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5280 otherwise do nothing. Meaning we need to scan
5282 regmatch_state *cur = mark_state;
5283 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5286 if ( sv_eq( cur->u.mark.mark_name,
5289 ST.mark_name = find;
5290 PUSH_STATE_GOTO( SKIP_next, next );
5292 cur = cur->u.mark.prev_mark;
5295 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5297 case SKIP_next_fail:
5299 /* (*CUT:NAME) - Set up to search for the name as we
5300 collapse the stack*/
5301 popmark = ST.mark_name;
5303 /* (*CUT) - No name, we cut here.*/
5304 if (ST.mark_loc > startpoint)
5305 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5306 /* but we set sv_commit to latest mark_name if there
5307 is one so they can test to see how things lead to this
5310 sv_commit=mark_state->u.mark.mark_name;
5318 if ( n == (U32)what_len_TRICKYFOLD(locinput,do_utf8,ln) ) {
5320 } else if ( 0xDF == n && !do_utf8 && !UTF ) {
5323 U8 folded[UTF8_MAXBYTES_CASE+1];
5325 const char * const l = locinput;
5326 char *e = PL_regeol;
5327 to_uni_fold(n, folded, &foldlen);
5329 if (ibcmp_utf8((const char*) folded, 0, foldlen, 1,
5330 l, &e, 0, do_utf8)) {
5335 nextchr = UCHARAT(locinput);
5338 if ((n=is_LNBREAK(locinput,do_utf8))) {
5340 nextchr = UCHARAT(locinput);
5345 #define CASE_CLASS(nAmE) \
5347 if ((n=is_##nAmE(locinput,do_utf8))) { \
5349 nextchr = UCHARAT(locinput); \
5354 if ((n=is_##nAmE(locinput,do_utf8))) { \
5357 locinput += UTF8SKIP(locinput); \
5358 nextchr = UCHARAT(locinput); \
5363 CASE_CLASS(HORIZWS);
5367 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5368 PTR2UV(scan), OP(scan));
5369 Perl_croak(aTHX_ "regexp memory corruption");
5373 /* switch break jumps here */
5374 scan = next; /* prepare to execute the next op and ... */
5375 continue; /* ... jump back to the top, reusing st */
5379 /* push a state that backtracks on success */
5380 st->u.yes.prev_yes_state = yes_state;
5384 /* push a new regex state, then continue at scan */
5386 regmatch_state *newst;
5389 regmatch_state *cur = st;
5390 regmatch_state *curyes = yes_state;
5392 regmatch_slab *slab = PL_regmatch_slab;
5393 for (;curd > -1;cur--,curd--) {
5394 if (cur < SLAB_FIRST(slab)) {
5396 cur = SLAB_LAST(slab);
5398 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5399 REPORT_CODE_OFF + 2 + depth * 2,"",
5400 curd, PL_reg_name[cur->resume_state],
5401 (curyes == cur) ? "yes" : ""
5404 curyes = cur->u.yes.prev_yes_state;
5407 DEBUG_STATE_pp("push")
5410 st->locinput = locinput;
5412 if (newst > SLAB_LAST(PL_regmatch_slab))
5413 newst = S_push_slab(aTHX);
5414 PL_regmatch_state = newst;
5416 locinput = PL_reginput;
5417 nextchr = UCHARAT(locinput);
5425 * We get here only if there's trouble -- normally "case END" is
5426 * the terminating point.
5428 Perl_croak(aTHX_ "corrupted regexp pointers");
5434 /* we have successfully completed a subexpression, but we must now
5435 * pop to the state marked by yes_state and continue from there */
5436 assert(st != yes_state);
5438 while (st != yes_state) {
5440 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5441 PL_regmatch_slab = PL_regmatch_slab->prev;
5442 st = SLAB_LAST(PL_regmatch_slab);
5446 DEBUG_STATE_pp("pop (no final)");
5448 DEBUG_STATE_pp("pop (yes)");
5454 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5455 || yes_state > SLAB_LAST(PL_regmatch_slab))
5457 /* not in this slab, pop slab */
5458 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5459 PL_regmatch_slab = PL_regmatch_slab->prev;
5460 st = SLAB_LAST(PL_regmatch_slab);
5462 depth -= (st - yes_state);
5465 yes_state = st->u.yes.prev_yes_state;
5466 PL_regmatch_state = st;
5469 locinput= st->locinput;
5470 nextchr = UCHARAT(locinput);
5472 state_num = st->resume_state + no_final;
5473 goto reenter_switch;
5476 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5477 PL_colors[4], PL_colors[5]));
5479 if (PL_reg_eval_set) {
5480 /* each successfully executed (?{...}) block does the equivalent of
5481 * local $^R = do {...}
5482 * When popping the save stack, all these locals would be undone;
5483 * bypass this by setting the outermost saved $^R to the latest
5485 if (oreplsv != GvSV(PL_replgv))
5486 sv_setsv(oreplsv, GvSV(PL_replgv));
5493 PerlIO_printf(Perl_debug_log,
5494 "%*s %sfailed...%s\n",
5495 REPORT_CODE_OFF+depth*2, "",
5496 PL_colors[4], PL_colors[5])
5508 /* there's a previous state to backtrack to */
5510 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5511 PL_regmatch_slab = PL_regmatch_slab->prev;
5512 st = SLAB_LAST(PL_regmatch_slab);
5514 PL_regmatch_state = st;
5515 locinput= st->locinput;
5516 nextchr = UCHARAT(locinput);
5518 DEBUG_STATE_pp("pop");
5520 if (yes_state == st)
5521 yes_state = st->u.yes.prev_yes_state;
5523 state_num = st->resume_state + 1; /* failure = success + 1 */
5524 goto reenter_switch;
5529 if (rex->intflags & PREGf_VERBARG_SEEN) {
5530 SV *sv_err = get_sv("REGERROR", 1);
5531 SV *sv_mrk = get_sv("REGMARK", 1);
5533 sv_commit = &PL_sv_no;
5535 sv_yes_mark = &PL_sv_yes;
5538 sv_commit = &PL_sv_yes;
5539 sv_yes_mark = &PL_sv_no;
5541 sv_setsv(sv_err, sv_commit);
5542 sv_setsv(sv_mrk, sv_yes_mark);
5545 /* clean up; in particular, free all slabs above current one */
5546 LEAVE_SCOPE(oldsave);
5552 - regrepeat - repeatedly match something simple, report how many
5555 * [This routine now assumes that it will only match on things of length 1.
5556 * That was true before, but now we assume scan - reginput is the count,
5557 * rather than incrementing count on every character. [Er, except utf8.]]
5560 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5563 register char *scan;
5565 register char *loceol = PL_regeol;
5566 register I32 hardcount = 0;
5567 register bool do_utf8 = PL_reg_match_utf8;
5569 PERL_UNUSED_ARG(depth);
5572 PERL_ARGS_ASSERT_REGREPEAT;
5575 if (max == REG_INFTY)
5577 else if (max < loceol - scan)
5578 loceol = scan + max;
5583 while (scan < loceol && hardcount < max && *scan != '\n') {
5584 scan += UTF8SKIP(scan);
5588 while (scan < loceol && *scan != '\n')
5595 while (scan < loceol && hardcount < max) {
5596 scan += UTF8SKIP(scan);
5606 case EXACT: /* length of string is 1 */
5608 while (scan < loceol && UCHARAT(scan) == c)
5611 case EXACTF: /* length of string is 1 */
5613 while (scan < loceol &&
5614 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5617 case EXACTFL: /* length of string is 1 */
5618 PL_reg_flags |= RF_tainted;
5620 while (scan < loceol &&
5621 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5627 while (hardcount < max && scan < loceol &&
5628 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
5629 scan += UTF8SKIP(scan);
5633 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5640 LOAD_UTF8_CHARCLASS_ALNUM();
5641 while (hardcount < max && scan < loceol &&
5642 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5643 scan += UTF8SKIP(scan);
5647 while (scan < loceol && isALNUM(*scan))
5652 PL_reg_flags |= RF_tainted;
5655 while (hardcount < max && scan < loceol &&
5656 isALNUM_LC_utf8((U8*)scan)) {
5657 scan += UTF8SKIP(scan);
5661 while (scan < loceol && isALNUM_LC(*scan))
5668 LOAD_UTF8_CHARCLASS_ALNUM();
5669 while (hardcount < max && scan < loceol &&
5670 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5671 scan += UTF8SKIP(scan);
5675 while (scan < loceol && !isALNUM(*scan))
5680 PL_reg_flags |= RF_tainted;
5683 while (hardcount < max && scan < loceol &&
5684 !isALNUM_LC_utf8((U8*)scan)) {
5685 scan += UTF8SKIP(scan);
5689 while (scan < loceol && !isALNUM_LC(*scan))
5696 LOAD_UTF8_CHARCLASS_SPACE();
5697 while (hardcount < max && scan < loceol &&
5699 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5700 scan += UTF8SKIP(scan);
5704 while (scan < loceol && isSPACE(*scan))
5709 PL_reg_flags |= RF_tainted;
5712 while (hardcount < max && scan < loceol &&
5713 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5714 scan += UTF8SKIP(scan);
5718 while (scan < loceol && isSPACE_LC(*scan))
5725 LOAD_UTF8_CHARCLASS_SPACE();
5726 while (hardcount < max && scan < loceol &&
5728 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5729 scan += UTF8SKIP(scan);
5733 while (scan < loceol && !isSPACE(*scan))
5738 PL_reg_flags |= RF_tainted;
5741 while (hardcount < max && scan < loceol &&
5742 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5743 scan += UTF8SKIP(scan);
5747 while (scan < loceol && !isSPACE_LC(*scan))
5754 LOAD_UTF8_CHARCLASS_DIGIT();
5755 while (hardcount < max && scan < loceol &&
5756 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5757 scan += UTF8SKIP(scan);
5761 while (scan < loceol && isDIGIT(*scan))
5768 LOAD_UTF8_CHARCLASS_DIGIT();
5769 while (hardcount < max && scan < loceol &&
5770 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5771 scan += UTF8SKIP(scan);
5775 while (scan < loceol && !isDIGIT(*scan))
5781 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5787 LNBREAK can match two latin chars, which is ok,
5788 because we have a null terminated string, but we
5789 have to use hardcount in this situation
5791 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5800 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5805 while (scan < loceol && is_HORIZWS_latin1(scan))
5812 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5813 scan += UTF8SKIP(scan);
5817 while (scan < loceol && !is_HORIZWS_latin1(scan))
5825 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5830 while (scan < loceol && is_VERTWS_latin1(scan))
5838 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5839 scan += UTF8SKIP(scan);
5843 while (scan < loceol && !is_VERTWS_latin1(scan))
5849 default: /* Called on something of 0 width. */
5850 break; /* So match right here or not at all. */
5856 c = scan - PL_reginput;
5860 GET_RE_DEBUG_FLAGS_DECL;
5862 SV * const prop = sv_newmortal();
5863 regprop(prog, prop, p);
5864 PerlIO_printf(Perl_debug_log,
5865 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5866 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5874 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5876 - regclass_swash - prepare the utf8 swash
5880 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5886 RXi_GET_DECL(prog,progi);
5887 const struct reg_data * const data = prog ? progi->data : NULL;
5889 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5891 if (data && data->count) {
5892 const U32 n = ARG(node);
5894 if (data->what[n] == 's') {
5895 SV * const rv = MUTABLE_SV(data->data[n]);
5896 AV * const av = MUTABLE_AV(SvRV(rv));
5897 SV **const ary = AvARRAY(av);
5900 /* See the end of regcomp.c:S_regclass() for
5901 * documentation of these array elements. */
5904 a = SvROK(ary[1]) ? &ary[1] : NULL;
5905 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5909 else if (si && doinit) {
5910 sw = swash_init("utf8", "", si, 1, 0);
5911 (void)av_store(av, 1, sw);
5928 - reginclass - determine if a character falls into a character class
5930 The n is the ANYOF regnode, the p is the target string, lenp
5931 is pointer to the maximum length of how far to go in the p
5932 (if the lenp is zero, UTF8SKIP(p) is used),
5933 do_utf8 tells whether the target string is in UTF-8.
5938 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
5941 const char flags = ANYOF_FLAGS(n);
5947 PERL_ARGS_ASSERT_REGINCLASS;
5949 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
5950 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5951 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
5952 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
5953 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
5954 * UTF8_ALLOW_FFFF */
5955 if (len == (STRLEN)-1)
5956 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
5959 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
5960 if (do_utf8 || (flags & ANYOF_UNICODE)) {
5963 if (do_utf8 && !ANYOF_RUNTIME(n)) {
5964 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
5967 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
5971 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
5979 utf8_p = bytes_to_utf8(p, &len);
5981 if (swash_fetch(sw, utf8_p, 1))
5983 else if (flags & ANYOF_FOLD) {
5984 if (!match && lenp && av) {
5986 for (i = 0; i <= av_len(av); i++) {
5987 SV* const sv = *av_fetch(av, i, FALSE);
5989 const char * const s = SvPV_const(sv, len);
5990 if (len <= plen && memEQ(s, (char*)utf8_p, len)) {
5998 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
6001 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
6002 if (swash_fetch(sw, tmpbuf, 1))
6007 /* If we allocated a string above, free it */
6008 if (! do_utf8) Safefree(utf8_p);
6011 if (match && lenp && *lenp == 0)
6012 *lenp = UNISKIP(NATIVE_TO_UNI(c));
6014 if (!match && c < 256) {
6015 if (ANYOF_BITMAP_TEST(n, c))
6017 else if (flags & ANYOF_FOLD) {
6020 if (flags & ANYOF_LOCALE) {
6021 PL_reg_flags |= RF_tainted;
6022 f = PL_fold_locale[c];
6026 if (f != c && ANYOF_BITMAP_TEST(n, f))
6030 if (!match && (flags & ANYOF_CLASS)) {
6031 PL_reg_flags |= RF_tainted;
6033 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6034 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6035 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6036 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6037 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6038 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6039 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6040 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6041 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6042 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6043 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6044 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6045 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6046 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6047 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6048 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6049 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6050 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6051 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6052 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6053 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6054 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6055 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6056 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6057 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6058 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6059 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6060 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6061 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6062 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6063 ) /* How's that for a conditional? */
6070 return (flags & ANYOF_INVERT) ? !match : match;
6074 S_reghop3(U8 *s, I32 off, const U8* lim)
6078 PERL_ARGS_ASSERT_REGHOP3;
6081 while (off-- && s < lim) {
6082 /* XXX could check well-formedness here */
6087 while (off++ && s > lim) {
6089 if (UTF8_IS_CONTINUED(*s)) {
6090 while (s > lim && UTF8_IS_CONTINUATION(*s))
6093 /* XXX could check well-formedness here */
6100 /* there are a bunch of places where we use two reghop3's that should
6101 be replaced with this routine. but since thats not done yet
6102 we ifdef it out - dmq
6105 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6109 PERL_ARGS_ASSERT_REGHOP4;
6112 while (off-- && s < rlim) {
6113 /* XXX could check well-formedness here */
6118 while (off++ && s > llim) {
6120 if (UTF8_IS_CONTINUED(*s)) {
6121 while (s > llim && UTF8_IS_CONTINUATION(*s))
6124 /* XXX could check well-formedness here */
6132 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
6136 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6139 while (off-- && s < lim) {
6140 /* XXX could check well-formedness here */
6147 while (off++ && s > lim) {
6149 if (UTF8_IS_CONTINUED(*s)) {
6150 while (s > lim && UTF8_IS_CONTINUATION(*s))
6153 /* XXX could check well-formedness here */
6162 restore_pos(pTHX_ void *arg)
6165 regexp * const rex = (regexp *)arg;
6166 if (PL_reg_eval_set) {
6167 if (PL_reg_oldsaved) {
6168 rex->subbeg = PL_reg_oldsaved;
6169 rex->sublen = PL_reg_oldsavedlen;
6170 #ifdef PERL_OLD_COPY_ON_WRITE
6171 rex->saved_copy = PL_nrs;
6173 RXp_MATCH_COPIED_on(rex);
6175 PL_reg_magic->mg_len = PL_reg_oldpos;
6176 PL_reg_eval_set = 0;
6177 PL_curpm = PL_reg_oldcurpm;
6182 S_to_utf8_substr(pTHX_ register regexp *prog)
6186 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6189 if (prog->substrs->data[i].substr
6190 && !prog->substrs->data[i].utf8_substr) {
6191 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6192 prog->substrs->data[i].utf8_substr = sv;
6193 sv_utf8_upgrade(sv);
6194 if (SvVALID(prog->substrs->data[i].substr)) {
6195 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
6196 if (flags & FBMcf_TAIL) {
6197 /* Trim the trailing \n that fbm_compile added last
6199 SvCUR_set(sv, SvCUR(sv) - 1);
6200 /* Whilst this makes the SV technically "invalid" (as its
6201 buffer is no longer followed by "\0") when fbm_compile()
6202 adds the "\n" back, a "\0" is restored. */
6204 fbm_compile(sv, flags);
6206 if (prog->substrs->data[i].substr == prog->check_substr)
6207 prog->check_utf8 = sv;
6213 S_to_byte_substr(pTHX_ register regexp *prog)
6218 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6221 if (prog->substrs->data[i].utf8_substr
6222 && !prog->substrs->data[i].substr) {
6223 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6224 if (sv_utf8_downgrade(sv, TRUE)) {
6225 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6227 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6228 if (flags & FBMcf_TAIL) {
6229 /* Trim the trailing \n that fbm_compile added last
6231 SvCUR_set(sv, SvCUR(sv) - 1);
6233 fbm_compile(sv, flags);
6239 prog->substrs->data[i].substr = sv;
6240 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6241 prog->check_substr = sv;
6248 * c-indentation-style: bsd
6250 * indent-tabs-mode: t
6253 * ex: set ts=8 sts=4 sw=4 noet: