5 * "One Ring to rule them all, One Ring to find them..."
8 /* This file contains functions for executing a regular expression. See
9 * also regcomp.c which funnily enough, contains functions for compiling
10 * a regular expression.
12 * This file is also copied at build time to ext/re/re_exec.c, where
13 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
14 * This causes the main functions to be compiled under new names and with
15 * debugging support added, which makes "use re 'debug'" work.
18 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
19 * confused with the original package (see point 3 below). Thanks, Henry!
22 /* Additional note: this code is very heavily munged from Henry's version
23 * in places. In some spots I've traded clarity for efficiency, so don't
24 * blame Henry for some of the lack of readability.
27 /* The names of the functions have been changed from regcomp and
28 * regexec to pregcomp and pregexec in order to avoid conflicts
29 * with the POSIX routines of the same names.
32 #ifdef PERL_EXT_RE_BUILD
37 * pregcomp and pregexec -- regsub and regerror are not used in perl
39 * Copyright (c) 1986 by University of Toronto.
40 * Written by Henry Spencer. Not derived from licensed software.
42 * Permission is granted to anyone to use this software for any
43 * purpose on any computer system, and to redistribute it freely,
44 * subject to the following restrictions:
46 * 1. The author is not responsible for the consequences of use of
47 * this software, no matter how awful, even if they arise
50 * 2. The origin of this software must not be misrepresented, either
51 * by explicit claim or by omission.
53 * 3. Altered versions must be plainly marked as such, and must not
54 * be misrepresented as being the original software.
56 **** Alterations to Henry's code are...
58 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
59 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
60 **** by Larry Wall and others
62 **** You may distribute under the terms of either the GNU General Public
63 **** License or the Artistic License, as specified in the README file.
65 * Beware that some of this code is subtly aware of the way operator
66 * precedence is structured in regular expressions. Serious changes in
67 * regular-expression syntax might require a total rethink.
70 #define PERL_IN_REGEXEC_C
73 #ifdef PERL_IN_XSUB_RE
79 #define RF_tainted 1 /* tainted information used? */
80 #define RF_warned 2 /* warned about big count? */
82 #define RF_utf8 8 /* Pattern contains multibyte chars? */
84 #define UTF ((PL_reg_flags & RF_utf8) != 0)
86 #define RS_init 1 /* eval environment created */
87 #define RS_set 2 /* replsv value is set */
93 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
99 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
100 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
102 #define HOPc(pos,off) \
103 (char *)(PL_reg_match_utf8 \
104 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
106 #define HOPBACKc(pos, off) \
107 (char*)(PL_reg_match_utf8\
108 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
109 : (pos - off >= PL_bostr) \
113 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
114 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
116 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
117 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
118 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
119 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
120 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
121 #define LOAD_UTF8_CHARCLASS_MARK() LOAD_UTF8_CHARCLASS(mark, "\xcd\x86")
123 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
125 /* for use after a quantifier and before an EXACT-like node -- japhy */
126 /* it would be nice to rework regcomp.sym to generate this stuff. sigh */
127 #define JUMPABLE(rn) ( \
129 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
131 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
132 OP(rn) == PLUS || OP(rn) == MINMOD || \
133 OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \
134 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
136 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
138 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
141 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
142 we don't need this definition. */
143 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
144 #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
145 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
148 /* ... so we use this as its faster. */
149 #define IS_TEXT(rn) ( OP(rn)==EXACT )
150 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
151 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
156 Search for mandatory following text node; for lookahead, the text must
157 follow but for lookbehind (rn->flags != 0) we skip to the next step.
159 #define FIND_NEXT_IMPT(rn) STMT_START { \
160 while (JUMPABLE(rn)) { \
161 const OPCODE type = OP(rn); \
162 if (type == SUSPEND || PL_regkind[type] == CURLY) \
163 rn = NEXTOPER(NEXTOPER(rn)); \
164 else if (type == PLUS) \
166 else if (type == IFMATCH) \
167 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
168 else rn += NEXT_OFF(rn); \
173 static void restore_pos(pTHX_ void *arg);
176 S_regcppush(pTHX_ I32 parenfloor)
179 const int retval = PL_savestack_ix;
180 #define REGCP_PAREN_ELEMS 4
181 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
183 GET_RE_DEBUG_FLAGS_DECL;
185 if (paren_elems_to_push < 0)
186 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
188 #define REGCP_OTHER_ELEMS 7
189 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
191 for (p = PL_regsize; p > parenfloor; p--) {
192 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
193 SSPUSHINT(PL_regoffs[p].end);
194 SSPUSHINT(PL_regoffs[p].start);
195 SSPUSHPTR(PL_reg_start_tmp[p]);
197 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
198 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
199 (UV)p, (IV)PL_regoffs[p].start,
200 (IV)(PL_reg_start_tmp[p] - PL_bostr),
201 (IV)PL_regoffs[p].end
204 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
205 SSPUSHPTR(PL_regoffs);
206 SSPUSHINT(PL_regsize);
207 SSPUSHINT(*PL_reglastparen);
208 SSPUSHINT(*PL_reglastcloseparen);
209 SSPUSHPTR(PL_reginput);
210 #define REGCP_FRAME_ELEMS 2
211 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
212 * are needed for the regexp context stack bookkeeping. */
213 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
214 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
219 /* These are needed since we do not localize EVAL nodes: */
220 #define REGCP_SET(cp) \
222 PerlIO_printf(Perl_debug_log, \
223 " Setting an EVAL scope, savestack=%"IVdf"\n", \
224 (IV)PL_savestack_ix)); \
227 #define REGCP_UNWIND(cp) \
229 if (cp != PL_savestack_ix) \
230 PerlIO_printf(Perl_debug_log, \
231 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
232 (IV)(cp), (IV)PL_savestack_ix)); \
236 S_regcppop(pTHX_ const regexp *rex)
241 GET_RE_DEBUG_FLAGS_DECL;
243 PERL_ARGS_ASSERT_REGCPPOP;
245 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
247 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
248 i = SSPOPINT; /* Parentheses elements to pop. */
249 input = (char *) SSPOPPTR;
250 *PL_reglastcloseparen = SSPOPINT;
251 *PL_reglastparen = SSPOPINT;
252 PL_regsize = SSPOPINT;
253 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
256 /* Now restore the parentheses context. */
257 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
258 i > 0; i -= REGCP_PAREN_ELEMS) {
260 U32 paren = (U32)SSPOPINT;
261 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
262 PL_regoffs[paren].start = SSPOPINT;
264 if (paren <= *PL_reglastparen)
265 PL_regoffs[paren].end = tmps;
267 PerlIO_printf(Perl_debug_log,
268 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
269 (UV)paren, (IV)PL_regoffs[paren].start,
270 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
271 (IV)PL_regoffs[paren].end,
272 (paren > *PL_reglastparen ? "(no)" : ""));
276 if (*PL_reglastparen + 1 <= rex->nparens) {
277 PerlIO_printf(Perl_debug_log,
278 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
279 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
283 /* It would seem that the similar code in regtry()
284 * already takes care of this, and in fact it is in
285 * a better location to since this code can #if 0-ed out
286 * but the code in regtry() is needed or otherwise tests
287 * requiring null fields (pat.t#187 and split.t#{13,14}
288 * (as of patchlevel 7877) will fail. Then again,
289 * this code seems to be necessary or otherwise
290 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
291 * --jhi updated by dapm */
292 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
294 PL_regoffs[i].start = -1;
295 PL_regoffs[i].end = -1;
301 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
304 * pregexec and friends
307 #ifndef PERL_IN_XSUB_RE
309 - pregexec - match a regexp against a string
312 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
313 char *strbeg, I32 minend, SV *screamer, U32 nosave)
314 /* strend: pointer to null at end of string */
315 /* strbeg: real beginning of string */
316 /* minend: end of match must be >=minend after stringarg. */
317 /* nosave: For optimizations. */
319 PERL_ARGS_ASSERT_PREGEXEC;
322 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
323 nosave ? 0 : REXEC_COPY_STR);
328 * Need to implement the following flags for reg_anch:
330 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
332 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
333 * INTUIT_AUTORITATIVE_ML
334 * INTUIT_ONCE_NOML - Intuit can match in one location only.
337 * Another flag for this function: SECOND_TIME (so that float substrs
338 * with giant delta may be not rechecked).
341 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
343 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
344 Otherwise, only SvCUR(sv) is used to get strbeg. */
346 /* XXXX We assume that strpos is strbeg unless sv. */
348 /* XXXX Some places assume that there is a fixed substring.
349 An update may be needed if optimizer marks as "INTUITable"
350 RExen without fixed substrings. Similarly, it is assumed that
351 lengths of all the strings are no more than minlen, thus they
352 cannot come from lookahead.
353 (Or minlen should take into account lookahead.)
354 NOTE: Some of this comment is not correct. minlen does now take account
355 of lookahead/behind. Further research is required. -- demerphq
359 /* A failure to find a constant substring means that there is no need to make
360 an expensive call to REx engine, thus we celebrate a failure. Similarly,
361 finding a substring too deep into the string means that less calls to
362 regtry() should be needed.
364 REx compiler's optimizer found 4 possible hints:
365 a) Anchored substring;
367 c) Whether we are anchored (beginning-of-line or \G);
368 d) First node (of those at offset 0) which may distingush positions;
369 We use a)b)d) and multiline-part of c), and try to find a position in the
370 string which does not contradict any of them.
373 /* Most of decisions we do here should have been done at compile time.
374 The nodes of the REx which we used for the search should have been
375 deleted from the finite automaton. */
378 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
379 char *strend, const U32 flags, re_scream_pos_data *data)
382 struct regexp *const prog = (struct regexp *)SvANY(rx);
383 register I32 start_shift = 0;
384 /* Should be nonnegative! */
385 register I32 end_shift = 0;
390 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
392 register char *other_last = NULL; /* other substr checked before this */
393 char *check_at = NULL; /* check substr found at this pos */
394 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
395 RXi_GET_DECL(prog,progi);
397 const char * const i_strpos = strpos;
399 GET_RE_DEBUG_FLAGS_DECL;
401 PERL_ARGS_ASSERT_RE_INTUIT_START;
403 RX_MATCH_UTF8_set(rx,do_utf8);
406 PL_reg_flags |= RF_utf8;
409 debug_start_match(rx, do_utf8, strpos, strend,
410 sv ? "Guessing start of match in sv for"
411 : "Guessing start of match in string for");
414 /* CHR_DIST() would be more correct here but it makes things slow. */
415 if (prog->minlen > strend - strpos) {
416 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
417 "String too short... [re_intuit_start]\n"));
421 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
424 if (!prog->check_utf8 && prog->check_substr)
425 to_utf8_substr(prog);
426 check = prog->check_utf8;
428 if (!prog->check_substr && prog->check_utf8)
429 to_byte_substr(prog);
430 check = prog->check_substr;
432 if (check == &PL_sv_undef) {
433 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
434 "Non-utf8 string cannot match utf8 check string\n"));
437 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
438 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
439 || ( (prog->extflags & RXf_ANCH_BOL)
440 && !multiline ) ); /* Check after \n? */
443 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
444 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
445 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
447 && (strpos != strbeg)) {
448 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
451 if (prog->check_offset_min == prog->check_offset_max &&
452 !(prog->extflags & RXf_CANY_SEEN)) {
453 /* Substring at constant offset from beg-of-str... */
456 s = HOP3c(strpos, prog->check_offset_min, strend);
459 slen = SvCUR(check); /* >= 1 */
461 if ( strend - s > slen || strend - s < slen - 1
462 || (strend - s == slen && strend[-1] != '\n')) {
463 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
466 /* Now should match s[0..slen-2] */
468 if (slen && (*SvPVX_const(check) != *s
470 && memNE(SvPVX_const(check), s, slen)))) {
472 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
476 else if (*SvPVX_const(check) != *s
477 || ((slen = SvCUR(check)) > 1
478 && memNE(SvPVX_const(check), s, slen)))
481 goto success_at_start;
484 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
486 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
487 end_shift = prog->check_end_shift;
490 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
491 - (SvTAIL(check) != 0);
492 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
494 if (end_shift < eshift)
498 else { /* Can match at random position */
501 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
502 end_shift = prog->check_end_shift;
504 /* end shift should be non negative here */
507 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
509 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
510 (IV)end_shift, RX_PRECOMP(prog));
514 /* Find a possible match in the region s..strend by looking for
515 the "check" substring in the region corrected by start/end_shift. */
518 I32 srch_start_shift = start_shift;
519 I32 srch_end_shift = end_shift;
520 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
521 srch_end_shift -= ((strbeg - s) - srch_start_shift);
522 srch_start_shift = strbeg - s;
524 DEBUG_OPTIMISE_MORE_r({
525 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
526 (IV)prog->check_offset_min,
527 (IV)srch_start_shift,
529 (IV)prog->check_end_shift);
532 if (flags & REXEC_SCREAM) {
533 I32 p = -1; /* Internal iterator of scream. */
534 I32 * const pp = data ? data->scream_pos : &p;
536 if (PL_screamfirst[BmRARE(check)] >= 0
537 || ( BmRARE(check) == '\n'
538 && (BmPREVIOUS(check) == SvCUR(check) - 1)
540 s = screaminstr(sv, check,
541 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
544 /* we may be pointing at the wrong string */
545 if (s && RXp_MATCH_COPIED(prog))
546 s = strbeg + (s - SvPVX_const(sv));
548 *data->scream_olds = s;
553 if (prog->extflags & RXf_CANY_SEEN) {
554 start_point= (U8*)(s + srch_start_shift);
555 end_point= (U8*)(strend - srch_end_shift);
557 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
558 end_point= HOP3(strend, -srch_end_shift, strbeg);
560 DEBUG_OPTIMISE_MORE_r({
561 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
562 (int)(end_point - start_point),
563 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
567 s = fbm_instr( start_point, end_point,
568 check, multiline ? FBMrf_MULTILINE : 0);
571 /* Update the count-of-usability, remove useless subpatterns,
575 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
576 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
577 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
578 (s ? "Found" : "Did not find"),
579 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
580 ? "anchored" : "floating"),
583 (s ? " at offset " : "...\n") );
588 /* Finish the diagnostic message */
589 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
591 /* XXX dmq: first branch is for positive lookbehind...
592 Our check string is offset from the beginning of the pattern.
593 So we need to do any stclass tests offset forward from that
602 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
603 Start with the other substr.
604 XXXX no SCREAM optimization yet - and a very coarse implementation
605 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
606 *always* match. Probably should be marked during compile...
607 Probably it is right to do no SCREAM here...
610 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
611 : (prog->float_substr && prog->anchored_substr))
613 /* Take into account the "other" substring. */
614 /* XXXX May be hopelessly wrong for UTF... */
617 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
620 char * const last = HOP3c(s, -start_shift, strbeg);
622 char * const saved_s = s;
625 t = s - prog->check_offset_max;
626 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
628 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
633 t = HOP3c(t, prog->anchored_offset, strend);
634 if (t < other_last) /* These positions already checked */
636 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
639 /* XXXX It is not documented what units *_offsets are in.
640 We assume bytes, but this is clearly wrong.
641 Meaning this code needs to be carefully reviewed for errors.
645 /* On end-of-str: see comment below. */
646 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
647 if (must == &PL_sv_undef) {
649 DEBUG_r(must = prog->anchored_utf8); /* for debug */
654 HOP3(HOP3(last1, prog->anchored_offset, strend)
655 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
657 multiline ? FBMrf_MULTILINE : 0
660 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
661 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
662 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
663 (s ? "Found" : "Contradicts"),
664 quoted, RE_SV_TAIL(must));
669 if (last1 >= last2) {
670 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
671 ", giving up...\n"));
674 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
675 ", trying floating at offset %ld...\n",
676 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
677 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
678 s = HOP3c(last, 1, strend);
682 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
683 (long)(s - i_strpos)));
684 t = HOP3c(s, -prog->anchored_offset, strbeg);
685 other_last = HOP3c(s, 1, strend);
693 else { /* Take into account the floating substring. */
695 char * const saved_s = s;
698 t = HOP3c(s, -start_shift, strbeg);
700 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
701 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
702 last = HOP3c(t, prog->float_max_offset, strend);
703 s = HOP3c(t, prog->float_min_offset, strend);
706 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
707 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
708 /* fbm_instr() takes into account exact value of end-of-str
709 if the check is SvTAIL(ed). Since false positives are OK,
710 and end-of-str is not later than strend we are OK. */
711 if (must == &PL_sv_undef) {
713 DEBUG_r(must = prog->float_utf8); /* for debug message */
716 s = fbm_instr((unsigned char*)s,
717 (unsigned char*)last + SvCUR(must)
719 must, multiline ? FBMrf_MULTILINE : 0);
721 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
722 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
723 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
724 (s ? "Found" : "Contradicts"),
725 quoted, RE_SV_TAIL(must));
729 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
730 ", giving up...\n"));
733 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
734 ", trying anchored starting at offset %ld...\n",
735 (long)(saved_s + 1 - i_strpos)));
737 s = HOP3c(t, 1, strend);
741 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
742 (long)(s - i_strpos)));
743 other_last = s; /* Fix this later. --Hugo */
753 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
755 DEBUG_OPTIMISE_MORE_r(
756 PerlIO_printf(Perl_debug_log,
757 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
758 (IV)prog->check_offset_min,
759 (IV)prog->check_offset_max,
767 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
769 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
772 /* Fixed substring is found far enough so that the match
773 cannot start at strpos. */
775 if (ml_anch && t[-1] != '\n') {
776 /* Eventually fbm_*() should handle this, but often
777 anchored_offset is not 0, so this check will not be wasted. */
778 /* XXXX In the code below we prefer to look for "^" even in
779 presence of anchored substrings. And we search even
780 beyond the found float position. These pessimizations
781 are historical artefacts only. */
783 while (t < strend - prog->minlen) {
785 if (t < check_at - prog->check_offset_min) {
786 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
787 /* Since we moved from the found position,
788 we definitely contradict the found anchored
789 substr. Due to the above check we do not
790 contradict "check" substr.
791 Thus we can arrive here only if check substr
792 is float. Redo checking for "other"=="fixed".
795 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
796 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
797 goto do_other_anchored;
799 /* We don't contradict the found floating substring. */
800 /* XXXX Why not check for STCLASS? */
802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
803 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
806 /* Position contradicts check-string */
807 /* XXXX probably better to look for check-string
808 than for "\n", so one should lower the limit for t? */
809 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
810 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
811 other_last = strpos = s = t + 1;
816 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
817 PL_colors[0], PL_colors[1]));
821 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
822 PL_colors[0], PL_colors[1]));
826 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
829 /* The found string does not prohibit matching at strpos,
830 - no optimization of calling REx engine can be performed,
831 unless it was an MBOL and we are not after MBOL,
832 or a future STCLASS check will fail this. */
834 /* Even in this situation we may use MBOL flag if strpos is offset
835 wrt the start of the string. */
836 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
837 && (strpos != strbeg) && strpos[-1] != '\n'
838 /* May be due to an implicit anchor of m{.*foo} */
839 && !(prog->intflags & PREGf_IMPLICIT))
844 DEBUG_EXECUTE_r( if (ml_anch)
845 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
846 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
849 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
851 prog->check_utf8 /* Could be deleted already */
852 && --BmUSEFUL(prog->check_utf8) < 0
853 && (prog->check_utf8 == prog->float_utf8)
855 prog->check_substr /* Could be deleted already */
856 && --BmUSEFUL(prog->check_substr) < 0
857 && (prog->check_substr == prog->float_substr)
860 /* If flags & SOMETHING - do not do it many times on the same match */
861 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
862 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
863 if (do_utf8 ? prog->check_substr : prog->check_utf8)
864 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
865 prog->check_substr = prog->check_utf8 = NULL; /* disable */
866 prog->float_substr = prog->float_utf8 = NULL; /* clear */
867 check = NULL; /* abort */
869 /* XXXX This is a remnant of the old implementation. It
870 looks wasteful, since now INTUIT can use many
872 prog->extflags &= ~RXf_USE_INTUIT;
879 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
880 /* trie stclasses are too expensive to use here, we are better off to
881 leave it to regmatch itself */
882 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
883 /* minlen == 0 is possible if regstclass is \b or \B,
884 and the fixed substr is ''$.
885 Since minlen is already taken into account, s+1 is before strend;
886 accidentally, minlen >= 1 guaranties no false positives at s + 1
887 even for \b or \B. But (minlen? 1 : 0) below assumes that
888 regstclass does not come from lookahead... */
889 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
890 This leaves EXACTF only, which is dealt with in find_byclass(). */
891 const U8* const str = (U8*)STRING(progi->regstclass);
892 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
893 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
896 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
897 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
898 else if (prog->float_substr || prog->float_utf8)
899 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
903 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
904 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
907 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
910 const char *what = NULL;
912 if (endpos == strend) {
913 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
914 "Could not match STCLASS...\n") );
917 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
918 "This position contradicts STCLASS...\n") );
919 if ((prog->extflags & RXf_ANCH) && !ml_anch)
921 /* Contradict one of substrings */
922 if (prog->anchored_substr || prog->anchored_utf8) {
923 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
924 DEBUG_EXECUTE_r( what = "anchored" );
926 s = HOP3c(t, 1, strend);
927 if (s + start_shift + end_shift > strend) {
928 /* XXXX Should be taken into account earlier? */
929 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
930 "Could not match STCLASS...\n") );
935 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
936 "Looking for %s substr starting at offset %ld...\n",
937 what, (long)(s + start_shift - i_strpos)) );
940 /* Have both, check_string is floating */
941 if (t + start_shift >= check_at) /* Contradicts floating=check */
942 goto retry_floating_check;
943 /* Recheck anchored substring, but not floating... */
947 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
948 "Looking for anchored substr starting at offset %ld...\n",
949 (long)(other_last - i_strpos)) );
950 goto do_other_anchored;
952 /* Another way we could have checked stclass at the
953 current position only: */
958 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
959 "Looking for /%s^%s/m starting at offset %ld...\n",
960 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
963 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
965 /* Check is floating subtring. */
966 retry_floating_check:
967 t = check_at - start_shift;
968 DEBUG_EXECUTE_r( what = "floating" );
969 goto hop_and_restart;
972 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
973 "By STCLASS: moving %ld --> %ld\n",
974 (long)(t - i_strpos), (long)(s - i_strpos))
978 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
979 "Does not contradict STCLASS...\n");
984 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
985 PL_colors[4], (check ? "Guessed" : "Giving up"),
986 PL_colors[5], (long)(s - i_strpos)) );
989 fail_finish: /* Substring not found */
990 if (prog->check_substr || prog->check_utf8) /* could be removed already */
991 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
993 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
994 PL_colors[4], PL_colors[5]));
998 #define DECL_TRIE_TYPE(scan) \
999 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1000 trie_type = (scan->flags != EXACT) \
1001 ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \
1002 : (do_utf8 ? trie_utf8 : trie_plain)
1004 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1005 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1006 switch (trie_type) { \
1007 case trie_utf8_fold: \
1008 if ( foldlen>0 ) { \
1009 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1014 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1015 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1016 foldlen -= UNISKIP( uvc ); \
1017 uscan = foldbuf + UNISKIP( uvc ); \
1020 case trie_latin_utf8_fold: \
1021 if ( foldlen>0 ) { \
1022 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1028 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1029 foldlen -= UNISKIP( uvc ); \
1030 uscan = foldbuf + UNISKIP( uvc ); \
1034 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1042 charid = trie->charmap[ uvc ]; \
1046 if (widecharmap) { \
1047 SV** const svpp = hv_fetch(widecharmap, \
1048 (char*)&uvc, sizeof(UV), 0); \
1050 charid = (U16)SvIV(*svpp); \
1055 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1057 char *my_strend= (char *)strend; \
1060 !ibcmp_utf8(s, &my_strend, 0, do_utf8, \
1061 m, NULL, ln, (bool)UTF)) \
1062 && (!reginfo || regtry(reginfo, &s)) ) \
1065 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1066 uvchr_to_utf8(tmpbuf, c); \
1067 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1069 && (f == c1 || f == c2) \
1071 !ibcmp_utf8(s, &my_strend, 0, do_utf8,\
1072 m, NULL, ln, (bool)UTF)) \
1073 && (!reginfo || regtry(reginfo, &s)) ) \
1079 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1083 && (ln == 1 || !(OP(c) == EXACTF \
1085 : ibcmp_locale(s, m, ln))) \
1086 && (!reginfo || regtry(reginfo, &s)) ) \
1092 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1094 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1100 #define REXEC_FBC_SCAN(CoDe) \
1102 while (s < strend) { \
1108 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1109 REXEC_FBC_UTF8_SCAN( \
1111 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1120 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1123 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1132 #define REXEC_FBC_TRYIT \
1133 if ((!reginfo || regtry(reginfo, &s))) \
1136 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1138 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1141 REXEC_FBC_CLASS_SCAN(CoNd); \
1145 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1148 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1151 REXEC_FBC_CLASS_SCAN(CoNd); \
1155 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1156 PL_reg_flags |= RF_tainted; \
1158 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1161 REXEC_FBC_CLASS_SCAN(CoNd); \
1165 #define DUMP_EXEC_POS(li,s,doutf8) \
1166 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1168 /* We know what class REx starts with. Try to find this position... */
1169 /* if reginfo is NULL, its a dryrun */
1170 /* annoyingly all the vars in this routine have different names from their counterparts
1171 in regmatch. /grrr */
1174 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1175 const char *strend, regmatch_info *reginfo)
1178 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1182 register STRLEN uskip;
1186 register I32 tmp = 1; /* Scratch variable? */
1187 register const bool do_utf8 = PL_reg_match_utf8;
1188 RXi_GET_DECL(prog,progi);
1190 PERL_ARGS_ASSERT_FIND_BYCLASS;
1192 /* We know what class it must start with. */
1196 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1197 !UTF8_IS_INVARIANT((U8)s[0]) ?
1198 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1199 REGINCLASS(prog, c, (U8*)s));
1202 while (s < strend) {
1205 if (REGINCLASS(prog, c, (U8*)s) ||
1206 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1207 /* The assignment of 2 is intentional:
1208 * for the folded sharp s, the skip is 2. */
1209 (skip = SHARP_S_SKIP))) {
1210 if (tmp && (!reginfo || regtry(reginfo, &s)))
1223 if (tmp && (!reginfo || regtry(reginfo, &s)))
1231 ln = STR_LEN(c); /* length to match in octets/bytes */
1232 lnc = (I32) ln; /* length to match in characters */
1234 STRLEN ulen1, ulen2;
1236 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1237 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1238 /* used by commented-out code below */
1239 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1241 /* XXX: Since the node will be case folded at compile
1242 time this logic is a little odd, although im not
1243 sure that its actually wrong. --dmq */
1245 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1246 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1248 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1249 codepoint of the first character in the converted
1250 form, yet originally we did the extra step.
1251 No tests fail by commenting this code out however
1252 so Ive left it out. -- dmq.
1254 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1256 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1261 while (sm < ((U8 *) m + ln)) {
1276 c2 = PL_fold_locale[c1];
1278 e = HOP3c(strend, -((I32)lnc), s);
1280 if (!reginfo && e < s)
1281 e = s; /* Due to minlen logic of intuit() */
1283 /* The idea in the EXACTF* cases is to first find the
1284 * first character of the EXACTF* node and then, if
1285 * necessary, case-insensitively compare the full
1286 * text of the node. The c1 and c2 are the first
1287 * characters (though in Unicode it gets a bit
1288 * more complicated because there are more cases
1289 * than just upper and lower: one needs to use
1290 * the so-called folding case for case-insensitive
1291 * matching (called "loose matching" in Unicode).
1292 * ibcmp_utf8() will do just that. */
1294 if (do_utf8 || UTF) {
1296 U8 tmpbuf [UTF8_MAXBYTES+1];
1299 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1301 /* Upper and lower of 1st char are equal -
1302 * probably not a "letter". */
1305 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1310 REXEC_FBC_EXACTISH_CHECK(c == c1);
1316 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1322 /* Handle some of the three Greek sigmas cases.
1323 * Note that not all the possible combinations
1324 * are handled here: some of them are handled
1325 * by the standard folding rules, and some of
1326 * them (the character class or ANYOF cases)
1327 * are handled during compiletime in
1328 * regexec.c:S_regclass(). */
1329 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1330 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1331 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1333 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1338 /* Neither pattern nor string are UTF8 */
1340 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1342 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1346 PL_reg_flags |= RF_tainted;
1353 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1354 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1356 tmp = ((OP(c) == BOUND ?
1357 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1358 LOAD_UTF8_CHARCLASS_ALNUM();
1359 REXEC_FBC_UTF8_SCAN(
1360 if (tmp == !(OP(c) == BOUND ?
1361 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1362 isALNUM_LC_utf8((U8*)s)))
1370 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1371 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1374 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1380 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1384 PL_reg_flags |= RF_tainted;
1391 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1392 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1394 tmp = ((OP(c) == NBOUND ?
1395 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1396 LOAD_UTF8_CHARCLASS_ALNUM();
1397 REXEC_FBC_UTF8_SCAN(
1398 if (tmp == !(OP(c) == NBOUND ?
1399 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1400 isALNUM_LC_utf8((U8*)s)))
1402 else REXEC_FBC_TRYIT;
1406 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1407 tmp = ((OP(c) == NBOUND ?
1408 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1411 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1413 else REXEC_FBC_TRYIT;
1416 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1420 REXEC_FBC_CSCAN_PRELOAD(
1421 LOAD_UTF8_CHARCLASS_ALNUM(),
1422 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1426 REXEC_FBC_CSCAN_TAINT(
1427 isALNUM_LC_utf8((U8*)s),
1431 REXEC_FBC_CSCAN_PRELOAD(
1432 LOAD_UTF8_CHARCLASS_ALNUM(),
1433 !swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1437 REXEC_FBC_CSCAN_TAINT(
1438 !isALNUM_LC_utf8((U8*)s),
1442 REXEC_FBC_CSCAN_PRELOAD(
1443 LOAD_UTF8_CHARCLASS_SPACE(),
1444 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8),
1448 REXEC_FBC_CSCAN_TAINT(
1449 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1453 REXEC_FBC_CSCAN_PRELOAD(
1454 LOAD_UTF8_CHARCLASS_SPACE(),
1455 !(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)),
1459 REXEC_FBC_CSCAN_TAINT(
1460 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1464 REXEC_FBC_CSCAN_PRELOAD(
1465 LOAD_UTF8_CHARCLASS_DIGIT(),
1466 swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1470 REXEC_FBC_CSCAN_TAINT(
1471 isDIGIT_LC_utf8((U8*)s),
1475 REXEC_FBC_CSCAN_PRELOAD(
1476 LOAD_UTF8_CHARCLASS_DIGIT(),
1477 !swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1481 REXEC_FBC_CSCAN_TAINT(
1482 !isDIGIT_LC_utf8((U8*)s),
1488 is_LNBREAK_latin1(s)
1498 !is_VERTWS_latin1(s)
1503 is_HORIZWS_latin1(s)
1507 !is_HORIZWS_utf8(s),
1508 !is_HORIZWS_latin1(s)
1514 /* what trie are we using right now */
1516 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1518 = (reg_trie_data*)progi->data->data[ aho->trie ];
1519 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1521 const char *last_start = strend - trie->minlen;
1523 const char *real_start = s;
1525 STRLEN maxlen = trie->maxlen;
1527 U8 **points; /* map of where we were in the input string
1528 when reading a given char. For ASCII this
1529 is unnecessary overhead as the relationship
1530 is always 1:1, but for Unicode, especially
1531 case folded Unicode this is not true. */
1532 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1536 GET_RE_DEBUG_FLAGS_DECL;
1538 /* We can't just allocate points here. We need to wrap it in
1539 * an SV so it gets freed properly if there is a croak while
1540 * running the match */
1543 sv_points=newSV(maxlen * sizeof(U8 *));
1544 SvCUR_set(sv_points,
1545 maxlen * sizeof(U8 *));
1546 SvPOK_on(sv_points);
1547 sv_2mortal(sv_points);
1548 points=(U8**)SvPV_nolen(sv_points );
1549 if ( trie_type != trie_utf8_fold
1550 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1553 bitmap=(U8*)trie->bitmap;
1555 bitmap=(U8*)ANYOF_BITMAP(c);
1557 /* this is the Aho-Corasick algorithm modified a touch
1558 to include special handling for long "unknown char"
1559 sequences. The basic idea being that we use AC as long
1560 as we are dealing with a possible matching char, when
1561 we encounter an unknown char (and we have not encountered
1562 an accepting state) we scan forward until we find a legal
1564 AC matching is basically that of trie matching, except
1565 that when we encounter a failing transition, we fall back
1566 to the current states "fail state", and try the current char
1567 again, a process we repeat until we reach the root state,
1568 state 1, or a legal transition. If we fail on the root state
1569 then we can either terminate if we have reached an accepting
1570 state previously, or restart the entire process from the beginning
1574 while (s <= last_start) {
1575 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1583 U8 *uscan = (U8*)NULL;
1584 U8 *leftmost = NULL;
1586 U32 accepted_word= 0;
1590 while ( state && uc <= (U8*)strend ) {
1592 U32 word = aho->states[ state ].wordnum;
1596 DEBUG_TRIE_EXECUTE_r(
1597 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1598 dump_exec_pos( (char *)uc, c, strend, real_start,
1599 (char *)uc, do_utf8 );
1600 PerlIO_printf( Perl_debug_log,
1601 " Scanning for legal start char...\n");
1604 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1609 if (uc >(U8*)last_start) break;
1613 U8 *lpos= points[ (pointpos - trie->wordlen[word-1] ) % maxlen ];
1614 if (!leftmost || lpos < leftmost) {
1615 DEBUG_r(accepted_word=word);
1621 points[pointpos++ % maxlen]= uc;
1622 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1623 uscan, len, uvc, charid, foldlen,
1625 DEBUG_TRIE_EXECUTE_r({
1626 dump_exec_pos( (char *)uc, c, strend, real_start,
1628 PerlIO_printf(Perl_debug_log,
1629 " Charid:%3u CP:%4"UVxf" ",
1635 word = aho->states[ state ].wordnum;
1637 base = aho->states[ state ].trans.base;
1639 DEBUG_TRIE_EXECUTE_r({
1641 dump_exec_pos( (char *)uc, c, strend, real_start,
1643 PerlIO_printf( Perl_debug_log,
1644 "%sState: %4"UVxf", word=%"UVxf,
1645 failed ? " Fail transition to " : "",
1646 (UV)state, (UV)word);
1651 (base + charid > trie->uniquecharcount )
1652 && (base + charid - 1 - trie->uniquecharcount
1654 && trie->trans[base + charid - 1 -
1655 trie->uniquecharcount].check == state
1656 && (tmp=trie->trans[base + charid - 1 -
1657 trie->uniquecharcount ].next))
1659 DEBUG_TRIE_EXECUTE_r(
1660 PerlIO_printf( Perl_debug_log," - legal\n"));
1665 DEBUG_TRIE_EXECUTE_r(
1666 PerlIO_printf( Perl_debug_log," - fail\n"));
1668 state = aho->fail[state];
1672 /* we must be accepting here */
1673 DEBUG_TRIE_EXECUTE_r(
1674 PerlIO_printf( Perl_debug_log," - accepting\n"));
1683 if (!state) state = 1;
1686 if ( aho->states[ state ].wordnum ) {
1687 U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ];
1688 if (!leftmost || lpos < leftmost) {
1689 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1694 s = (char*)leftmost;
1695 DEBUG_TRIE_EXECUTE_r({
1697 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1698 (UV)accepted_word, (IV)(s - real_start)
1701 if (!reginfo || regtry(reginfo, &s)) {
1707 DEBUG_TRIE_EXECUTE_r({
1708 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1711 DEBUG_TRIE_EXECUTE_r(
1712 PerlIO_printf( Perl_debug_log,"No match.\n"));
1721 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1730 S_swap_match_buff (pTHX_ regexp *prog)
1732 regexp_paren_pair *t;
1734 PERL_ARGS_ASSERT_SWAP_MATCH_BUFF;
1737 /* We have to be careful. If the previous successful match
1738 was from this regex we don't want a subsequent paritally
1739 successful match to clobber the old results.
1740 So when we detect this possibility we add a swap buffer
1741 to the re, and switch the buffer each match. If we fail
1742 we switch it back, otherwise we leave it swapped.
1744 Newxz(prog->swap, (prog->nparens + 1), regexp_paren_pair);
1747 prog->swap = prog->offs;
1753 - regexec_flags - match a regexp against a string
1756 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1757 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1758 /* strend: pointer to null at end of string */
1759 /* strbeg: real beginning of string */
1760 /* minend: end of match must be >=minend after stringarg. */
1761 /* data: May be used for some additional optimizations.
1762 Currently its only used, with a U32 cast, for transmitting
1763 the ganch offset when doing a /g match. This will change */
1764 /* nosave: For optimizations. */
1767 struct regexp *const prog = (struct regexp *)SvANY(rx);
1768 /*register*/ char *s;
1769 register regnode *c;
1770 /*register*/ char *startpos = stringarg;
1771 I32 minlen; /* must match at least this many chars */
1772 I32 dontbother = 0; /* how many characters not to try at end */
1773 I32 end_shift = 0; /* Same for the end. */ /* CC */
1774 I32 scream_pos = -1; /* Internal iterator of scream. */
1775 char *scream_olds = NULL;
1776 const bool do_utf8 = (bool)DO_UTF8(sv);
1778 RXi_GET_DECL(prog,progi);
1779 regmatch_info reginfo; /* create some info to pass to regtry etc */
1780 bool swap_on_fail = 0;
1781 GET_RE_DEBUG_FLAGS_DECL;
1783 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1784 PERL_UNUSED_ARG(data);
1786 /* Be paranoid... */
1787 if (prog == NULL || startpos == NULL) {
1788 Perl_croak(aTHX_ "NULL regexp parameter");
1792 multiline = prog->extflags & RXf_PMf_MULTILINE;
1793 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1795 RX_MATCH_UTF8_set(rx, do_utf8);
1797 debug_start_match(rx, do_utf8, startpos, strend,
1801 minlen = prog->minlen;
1803 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1804 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1805 "String too short [regexec_flags]...\n"));
1810 /* Check validity of program. */
1811 if (UCHARAT(progi->program) != REG_MAGIC) {
1812 Perl_croak(aTHX_ "corrupted regexp program");
1816 PL_reg_eval_set = 0;
1820 PL_reg_flags |= RF_utf8;
1822 /* Mark beginning of line for ^ and lookbehind. */
1823 reginfo.bol = startpos; /* XXX not used ??? */
1827 /* Mark end of line for $ (and such) */
1830 /* see how far we have to get to not match where we matched before */
1831 reginfo.till = startpos+minend;
1833 /* If there is a "must appear" string, look for it. */
1836 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1839 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1840 reginfo.ganch = startpos + prog->gofs;
1841 else if (sv && SvTYPE(sv) >= SVt_PVMG
1843 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1844 && mg->mg_len >= 0) {
1845 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1846 if (prog->extflags & RXf_ANCH_GPOS) {
1847 if (s > reginfo.ganch)
1849 s = reginfo.ganch - prog->gofs;
1853 reginfo.ganch = strbeg + PTR2UV(data);
1854 } else /* pos() not defined */
1855 reginfo.ganch = strbeg;
1857 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1859 swap_match_buff(prog); /* do we need a save destructor here for
1862 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1863 re_scream_pos_data d;
1865 d.scream_olds = &scream_olds;
1866 d.scream_pos = &scream_pos;
1867 s = re_intuit_start(rx, sv, s, strend, flags, &d);
1869 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1870 goto phooey; /* not present */
1876 /* Simplest case: anchored match need be tried only once. */
1877 /* [unless only anchor is BOL and multiline is set] */
1878 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
1879 if (s == startpos && regtry(®info, &startpos))
1881 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
1882 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
1887 dontbother = minlen - 1;
1888 end = HOP3c(strend, -dontbother, strbeg) - 1;
1889 /* for multiline we only have to try after newlines */
1890 if (prog->check_substr || prog->check_utf8) {
1894 if (regtry(®info, &s))
1899 if (prog->extflags & RXf_USE_INTUIT) {
1900 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
1911 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1912 if (regtry(®info, &s))
1919 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
1921 /* the warning about reginfo.ganch being used without intialization
1922 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
1923 and we only enter this block when the same bit is set. */
1924 char *tmp_s = reginfo.ganch - prog->gofs;
1925 if (regtry(®info, &tmp_s))
1930 /* Messy cases: unanchored match. */
1931 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
1932 /* we have /x+whatever/ */
1933 /* it must be a one character string (XXXX Except UTF?) */
1938 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1939 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1940 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1945 DEBUG_EXECUTE_r( did_match = 1 );
1946 if (regtry(®info, &s)) goto got_it;
1948 while (s < strend && *s == ch)
1956 DEBUG_EXECUTE_r( did_match = 1 );
1957 if (regtry(®info, &s)) goto got_it;
1959 while (s < strend && *s == ch)
1964 DEBUG_EXECUTE_r(if (!did_match)
1965 PerlIO_printf(Perl_debug_log,
1966 "Did not find anchored character...\n")
1969 else if (prog->anchored_substr != NULL
1970 || prog->anchored_utf8 != NULL
1971 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
1972 && prog->float_max_offset < strend - s)) {
1977 char *last1; /* Last position checked before */
1981 if (prog->anchored_substr || prog->anchored_utf8) {
1982 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1983 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1984 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1985 back_max = back_min = prog->anchored_offset;
1987 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1988 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1989 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1990 back_max = prog->float_max_offset;
1991 back_min = prog->float_min_offset;
1995 if (must == &PL_sv_undef)
1996 /* could not downgrade utf8 check substring, so must fail */
2002 last = HOP3c(strend, /* Cannot start after this */
2003 -(I32)(CHR_SVLEN(must)
2004 - (SvTAIL(must) != 0) + back_min), strbeg);
2007 last1 = HOPc(s, -1);
2009 last1 = s - 1; /* bogus */
2011 /* XXXX check_substr already used to find "s", can optimize if
2012 check_substr==must. */
2014 dontbother = end_shift;
2015 strend = HOPc(strend, -dontbother);
2016 while ( (s <= last) &&
2017 ((flags & REXEC_SCREAM)
2018 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2019 end_shift, &scream_pos, 0))
2020 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2021 (unsigned char*)strend, must,
2022 multiline ? FBMrf_MULTILINE : 0))) ) {
2023 /* we may be pointing at the wrong string */
2024 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2025 s = strbeg + (s - SvPVX_const(sv));
2026 DEBUG_EXECUTE_r( did_match = 1 );
2027 if (HOPc(s, -back_max) > last1) {
2028 last1 = HOPc(s, -back_min);
2029 s = HOPc(s, -back_max);
2032 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2034 last1 = HOPc(s, -back_min);
2038 while (s <= last1) {
2039 if (regtry(®info, &s))
2045 while (s <= last1) {
2046 if (regtry(®info, &s))
2052 DEBUG_EXECUTE_r(if (!did_match) {
2053 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
2054 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2055 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2056 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2057 ? "anchored" : "floating"),
2058 quoted, RE_SV_TAIL(must));
2062 else if ( (c = progi->regstclass) ) {
2064 const OPCODE op = OP(progi->regstclass);
2065 /* don't bother with what can't match */
2066 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2067 strend = HOPc(strend, -(minlen - 1));
2070 SV * const prop = sv_newmortal();
2071 regprop(prog, prop, c);
2073 RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1),
2075 PerlIO_printf(Perl_debug_log,
2076 "Matching stclass %.*s against %s (%d chars)\n",
2077 (int)SvCUR(prop), SvPVX_const(prop),
2078 quoted, (int)(strend - s));
2081 if (find_byclass(prog, c, s, strend, ®info))
2083 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2087 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2092 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2093 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2094 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
2096 if (flags & REXEC_SCREAM) {
2097 last = screaminstr(sv, float_real, s - strbeg,
2098 end_shift, &scream_pos, 1); /* last one */
2100 last = scream_olds; /* Only one occurrence. */
2101 /* we may be pointing at the wrong string */
2102 else if (RXp_MATCH_COPIED(prog))
2103 s = strbeg + (s - SvPVX_const(sv));
2107 const char * const little = SvPV_const(float_real, len);
2109 if (SvTAIL(float_real)) {
2110 if (memEQ(strend - len + 1, little, len - 1))
2111 last = strend - len + 1;
2112 else if (!multiline)
2113 last = memEQ(strend - len, little, len)
2114 ? strend - len : NULL;
2120 last = rninstr(s, strend, little, little + len);
2122 last = strend; /* matching "$" */
2127 PerlIO_printf(Perl_debug_log,
2128 "%sCan't trim the tail, match fails (should not happen)%s\n",
2129 PL_colors[4], PL_colors[5]));
2130 goto phooey; /* Should not happen! */
2132 dontbother = strend - last + prog->float_min_offset;
2134 if (minlen && (dontbother < minlen))
2135 dontbother = minlen - 1;
2136 strend -= dontbother; /* this one's always in bytes! */
2137 /* We don't know much -- general case. */
2140 if (regtry(®info, &s))
2149 if (regtry(®info, &s))
2151 } while (s++ < strend);
2159 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2161 if (PL_reg_eval_set)
2162 restore_pos(aTHX_ prog);
2163 if (RXp_PAREN_NAMES(prog))
2164 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2166 /* make sure $`, $&, $', and $digit will work later */
2167 if ( !(flags & REXEC_NOT_FIRST) ) {
2168 RX_MATCH_COPY_FREE(rx);
2169 if (flags & REXEC_COPY_STR) {
2170 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2171 #ifdef PERL_OLD_COPY_ON_WRITE
2173 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2175 PerlIO_printf(Perl_debug_log,
2176 "Copy on write: regexp capture, type %d\n",
2179 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2180 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2181 assert (SvPOKp(prog->saved_copy));
2185 RX_MATCH_COPIED_on(rx);
2186 s = savepvn(strbeg, i);
2192 prog->subbeg = strbeg;
2193 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2200 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2201 PL_colors[4], PL_colors[5]));
2202 if (PL_reg_eval_set)
2203 restore_pos(aTHX_ prog);
2205 /* we failed :-( roll it back */
2206 swap_match_buff(prog);
2213 - regtry - try match at specific point
2215 STATIC I32 /* 0 failure, 1 success */
2216 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2220 REGEXP *const rx = reginfo->prog;
2221 regexp *const prog = (struct regexp *)SvANY(rx);
2222 RXi_GET_DECL(prog,progi);
2223 GET_RE_DEBUG_FLAGS_DECL;
2225 PERL_ARGS_ASSERT_REGTRY;
2227 reginfo->cutpoint=NULL;
2229 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2232 PL_reg_eval_set = RS_init;
2233 DEBUG_EXECUTE_r(DEBUG_s(
2234 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2235 (IV)(PL_stack_sp - PL_stack_base));
2238 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2239 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2241 /* Apparently this is not needed, judging by wantarray. */
2242 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2243 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2246 /* Make $_ available to executed code. */
2247 if (reginfo->sv != DEFSV) {
2249 DEFSV = reginfo->sv;
2252 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2253 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2254 /* prepare for quick setting of pos */
2255 #ifdef PERL_OLD_COPY_ON_WRITE
2256 if (SvIsCOW(reginfo->sv))
2257 sv_force_normal_flags(reginfo->sv, 0);
2259 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2260 &PL_vtbl_mglob, NULL, 0);
2264 PL_reg_oldpos = mg->mg_len;
2265 SAVEDESTRUCTOR_X(restore_pos, prog);
2267 if (!PL_reg_curpm) {
2268 Newxz(PL_reg_curpm, 1, PMOP);
2271 SV* const repointer = &PL_sv_undef;
2272 /* this regexp is also owned by the new PL_reg_curpm, which
2273 will try to free it. */
2274 av_push(PL_regex_padav, repointer);
2275 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2276 PL_regex_pad = AvARRAY(PL_regex_padav);
2281 /* It seems that non-ithreads works both with and without this code.
2282 So for efficiency reasons it seems best not to have the code
2283 compiled when it is not needed. */
2284 /* This is safe against NULLs: */
2285 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2286 /* PM_reg_curpm owns a reference to this regexp. */
2289 PM_SETRE(PL_reg_curpm, rx);
2290 PL_reg_oldcurpm = PL_curpm;
2291 PL_curpm = PL_reg_curpm;
2292 if (RXp_MATCH_COPIED(prog)) {
2293 /* Here is a serious problem: we cannot rewrite subbeg,
2294 since it may be needed if this match fails. Thus
2295 $` inside (?{}) could fail... */
2296 PL_reg_oldsaved = prog->subbeg;
2297 PL_reg_oldsavedlen = prog->sublen;
2298 #ifdef PERL_OLD_COPY_ON_WRITE
2299 PL_nrs = prog->saved_copy;
2301 RXp_MATCH_COPIED_off(prog);
2304 PL_reg_oldsaved = NULL;
2305 prog->subbeg = PL_bostr;
2306 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2308 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2309 prog->offs[0].start = *startpos - PL_bostr;
2310 PL_reginput = *startpos;
2311 PL_reglastparen = &prog->lastparen;
2312 PL_reglastcloseparen = &prog->lastcloseparen;
2313 prog->lastparen = 0;
2314 prog->lastcloseparen = 0;
2316 PL_regoffs = prog->offs;
2317 if (PL_reg_start_tmpl <= prog->nparens) {
2318 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2319 if(PL_reg_start_tmp)
2320 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2322 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2325 /* XXXX What this code is doing here?!!! There should be no need
2326 to do this again and again, PL_reglastparen should take care of
2329 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2330 * Actually, the code in regcppop() (which Ilya may be meaning by
2331 * PL_reglastparen), is not needed at all by the test suite
2332 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2333 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2334 * Meanwhile, this code *is* needed for the
2335 * above-mentioned test suite tests to succeed. The common theme
2336 * on those tests seems to be returning null fields from matches.
2337 * --jhi updated by dapm */
2339 if (prog->nparens) {
2340 regexp_paren_pair *pp = PL_regoffs;
2342 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2350 if (regmatch(reginfo, progi->program + 1)) {
2351 PL_regoffs[0].end = PL_reginput - PL_bostr;
2354 if (reginfo->cutpoint)
2355 *startpos= reginfo->cutpoint;
2356 REGCP_UNWIND(lastcp);
2361 #define sayYES goto yes
2362 #define sayNO goto no
2363 #define sayNO_SILENT goto no_silent
2365 /* we dont use STMT_START/END here because it leads to
2366 "unreachable code" warnings, which are bogus, but distracting. */
2367 #define CACHEsayNO \
2368 if (ST.cache_mask) \
2369 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2372 /* this is used to determine how far from the left messages like
2373 'failed...' are printed. It should be set such that messages
2374 are inline with the regop output that created them.
2376 #define REPORT_CODE_OFF 32
2379 /* Make sure there is a test for this +1 options in re_tests */
2380 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2382 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2383 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2385 #define SLAB_FIRST(s) (&(s)->states[0])
2386 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2388 /* grab a new slab and return the first slot in it */
2390 STATIC regmatch_state *
2393 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2396 regmatch_slab *s = PL_regmatch_slab->next;
2398 Newx(s, 1, regmatch_slab);
2399 s->prev = PL_regmatch_slab;
2401 PL_regmatch_slab->next = s;
2403 PL_regmatch_slab = s;
2404 return SLAB_FIRST(s);
2408 /* push a new state then goto it */
2410 #define PUSH_STATE_GOTO(state, node) \
2412 st->resume_state = state; \
2415 /* push a new state with success backtracking, then goto it */
2417 #define PUSH_YES_STATE_GOTO(state, node) \
2419 st->resume_state = state; \
2420 goto push_yes_state;
2426 regmatch() - main matching routine
2428 This is basically one big switch statement in a loop. We execute an op,
2429 set 'next' to point the next op, and continue. If we come to a point which
2430 we may need to backtrack to on failure such as (A|B|C), we push a
2431 backtrack state onto the backtrack stack. On failure, we pop the top
2432 state, and re-enter the loop at the state indicated. If there are no more
2433 states to pop, we return failure.
2435 Sometimes we also need to backtrack on success; for example /A+/, where
2436 after successfully matching one A, we need to go back and try to
2437 match another one; similarly for lookahead assertions: if the assertion
2438 completes successfully, we backtrack to the state just before the assertion
2439 and then carry on. In these cases, the pushed state is marked as
2440 'backtrack on success too'. This marking is in fact done by a chain of
2441 pointers, each pointing to the previous 'yes' state. On success, we pop to
2442 the nearest yes state, discarding any intermediate failure-only states.
2443 Sometimes a yes state is pushed just to force some cleanup code to be
2444 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2445 it to free the inner regex.
2447 Note that failure backtracking rewinds the cursor position, while
2448 success backtracking leaves it alone.
2450 A pattern is complete when the END op is executed, while a subpattern
2451 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2452 ops trigger the "pop to last yes state if any, otherwise return true"
2455 A common convention in this function is to use A and B to refer to the two
2456 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2457 the subpattern to be matched possibly multiple times, while B is the entire
2458 rest of the pattern. Variable and state names reflect this convention.
2460 The states in the main switch are the union of ops and failure/success of
2461 substates associated with with that op. For example, IFMATCH is the op
2462 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2463 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2464 successfully matched A and IFMATCH_A_fail is a state saying that we have
2465 just failed to match A. Resume states always come in pairs. The backtrack
2466 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2467 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2468 on success or failure.
2470 The struct that holds a backtracking state is actually a big union, with
2471 one variant for each major type of op. The variable st points to the
2472 top-most backtrack struct. To make the code clearer, within each
2473 block of code we #define ST to alias the relevant union.
2475 Here's a concrete example of a (vastly oversimplified) IFMATCH
2481 #define ST st->u.ifmatch
2483 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2484 ST.foo = ...; // some state we wish to save
2486 // push a yes backtrack state with a resume value of
2487 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2489 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2492 case IFMATCH_A: // we have successfully executed A; now continue with B
2494 bar = ST.foo; // do something with the preserved value
2497 case IFMATCH_A_fail: // A failed, so the assertion failed
2498 ...; // do some housekeeping, then ...
2499 sayNO; // propagate the failure
2506 For any old-timers reading this who are familiar with the old recursive
2507 approach, the code above is equivalent to:
2509 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2518 ...; // do some housekeeping, then ...
2519 sayNO; // propagate the failure
2522 The topmost backtrack state, pointed to by st, is usually free. If you
2523 want to claim it, populate any ST.foo fields in it with values you wish to
2524 save, then do one of
2526 PUSH_STATE_GOTO(resume_state, node);
2527 PUSH_YES_STATE_GOTO(resume_state, node);
2529 which sets that backtrack state's resume value to 'resume_state', pushes a
2530 new free entry to the top of the backtrack stack, then goes to 'node'.
2531 On backtracking, the free slot is popped, and the saved state becomes the
2532 new free state. An ST.foo field in this new top state can be temporarily
2533 accessed to retrieve values, but once the main loop is re-entered, it
2534 becomes available for reuse.
2536 Note that the depth of the backtrack stack constantly increases during the
2537 left-to-right execution of the pattern, rather than going up and down with
2538 the pattern nesting. For example the stack is at its maximum at Z at the
2539 end of the pattern, rather than at X in the following:
2541 /(((X)+)+)+....(Y)+....Z/
2543 The only exceptions to this are lookahead/behind assertions and the cut,
2544 (?>A), which pop all the backtrack states associated with A before
2547 Bascktrack state structs are allocated in slabs of about 4K in size.
2548 PL_regmatch_state and st always point to the currently active state,
2549 and PL_regmatch_slab points to the slab currently containing
2550 PL_regmatch_state. The first time regmatch() is called, the first slab is
2551 allocated, and is never freed until interpreter destruction. When the slab
2552 is full, a new one is allocated and chained to the end. At exit from
2553 regmatch(), slabs allocated since entry are freed.
2558 #define DEBUG_STATE_pp(pp) \
2560 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2561 PerlIO_printf(Perl_debug_log, \
2562 " %*s"pp" %s%s%s%s%s\n", \
2564 PL_reg_name[st->resume_state], \
2565 ((st==yes_state||st==mark_state) ? "[" : ""), \
2566 ((st==yes_state) ? "Y" : ""), \
2567 ((st==mark_state) ? "M" : ""), \
2568 ((st==yes_state||st==mark_state) ? "]" : "") \
2573 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2578 S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8,
2579 const char *start, const char *end, const char *blurb)
2581 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2583 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2588 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2589 RX_PRECOMP(prog), RX_PRELEN(prog), 60);
2591 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2592 start, end - start, 60);
2594 PerlIO_printf(Perl_debug_log,
2595 "%s%s REx%s %s against %s\n",
2596 PL_colors[4], blurb, PL_colors[5], s0, s1);
2598 if (do_utf8||utf8_pat)
2599 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2600 utf8_pat ? "pattern" : "",
2601 utf8_pat && do_utf8 ? " and " : "",
2602 do_utf8 ? "string" : ""
2608 S_dump_exec_pos(pTHX_ const char *locinput,
2609 const regnode *scan,
2610 const char *loc_regeol,
2611 const char *loc_bostr,
2612 const char *loc_reg_starttry,
2615 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2616 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2617 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2618 /* The part of the string before starttry has one color
2619 (pref0_len chars), between starttry and current
2620 position another one (pref_len - pref0_len chars),
2621 after the current position the third one.
2622 We assume that pref0_len <= pref_len, otherwise we
2623 decrease pref0_len. */
2624 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2625 ? (5 + taill) - l : locinput - loc_bostr;
2628 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2630 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2632 pref0_len = pref_len - (locinput - loc_reg_starttry);
2633 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2634 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2635 ? (5 + taill) - pref_len : loc_regeol - locinput);
2636 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2640 if (pref0_len > pref_len)
2641 pref0_len = pref_len;
2643 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2645 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2646 (locinput - pref_len),pref0_len, 60, 4, 5);
2648 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2649 (locinput - pref_len + pref0_len),
2650 pref_len - pref0_len, 60, 2, 3);
2652 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2653 locinput, loc_regeol - locinput, 10, 0, 1);
2655 const STRLEN tlen=len0+len1+len2;
2656 PerlIO_printf(Perl_debug_log,
2657 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2658 (IV)(locinput - loc_bostr),
2661 (docolor ? "" : "> <"),
2663 (int)(tlen > 19 ? 0 : 19 - tlen),
2670 /* reg_check_named_buff_matched()
2671 * Checks to see if a named buffer has matched. The data array of
2672 * buffer numbers corresponding to the buffer is expected to reside
2673 * in the regexp->data->data array in the slot stored in the ARG() of
2674 * node involved. Note that this routine doesn't actually care about the
2675 * name, that information is not preserved from compilation to execution.
2676 * Returns the index of the leftmost defined buffer with the given name
2677 * or 0 if non of the buffers matched.
2680 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2683 RXi_GET_DECL(rex,rexi);
2684 SV *sv_dat=(SV*)rexi->data->data[ ARG( scan ) ];
2685 I32 *nums=(I32*)SvPVX(sv_dat);
2687 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2689 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2690 if ((I32)*PL_reglastparen >= nums[n] &&
2691 PL_regoffs[nums[n]].end != -1)
2700 /* free all slabs above current one - called during LEAVE_SCOPE */
2703 S_clear_backtrack_stack(pTHX_ void *p)
2705 regmatch_slab *s = PL_regmatch_slab->next;
2710 PL_regmatch_slab->next = NULL;
2712 regmatch_slab * const osl = s;
2719 #define SETREX(Re1,Re2) \
2720 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2723 STATIC I32 /* 0 failure, 1 success */
2724 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2726 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2730 register const bool do_utf8 = PL_reg_match_utf8;
2731 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2732 REGEXP *rex_sv = reginfo->prog;
2733 regexp *rex = (struct regexp *)SvANY(rex_sv);
2734 RXi_GET_DECL(rex,rexi);
2736 /* the current state. This is a cached copy of PL_regmatch_state */
2737 register regmatch_state *st;
2738 /* cache heavy used fields of st in registers */
2739 register regnode *scan;
2740 register regnode *next;
2741 register U32 n = 0; /* general value; init to avoid compiler warning */
2742 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2743 register char *locinput = PL_reginput;
2744 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2746 bool result = 0; /* return value of S_regmatch */
2747 int depth = 0; /* depth of backtrack stack */
2748 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2749 const U32 max_nochange_depth =
2750 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2751 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2752 regmatch_state *yes_state = NULL; /* state to pop to on success of
2754 /* mark_state piggy backs on the yes_state logic so that when we unwind
2755 the stack on success we can update the mark_state as we go */
2756 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2757 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2758 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2760 bool no_final = 0; /* prevent failure from backtracking? */
2761 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2762 char *startpoint = PL_reginput;
2763 SV *popmark = NULL; /* are we looking for a mark? */
2764 SV *sv_commit = NULL; /* last mark name seen in failure */
2765 SV *sv_yes_mark = NULL; /* last mark name we have seen
2766 during a successfull match */
2767 U32 lastopen = 0; /* last open we saw */
2768 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2769 SV* const oreplsv = GvSV(PL_replgv);
2770 /* these three flags are set by various ops to signal information to
2771 * the very next op. They have a useful lifetime of exactly one loop
2772 * iteration, and are not preserved or restored by state pushes/pops
2774 bool sw = 0; /* the condition value in (?(cond)a|b) */
2775 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2776 int logical = 0; /* the following EVAL is:
2780 or the following IFMATCH/UNLESSM is:
2781 false: plain (?=foo)
2782 true: used as a condition: (?(?=foo))
2785 GET_RE_DEBUG_FLAGS_DECL;
2788 PERL_ARGS_ASSERT_REGMATCH;
2790 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2791 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2793 /* on first ever call to regmatch, allocate first slab */
2794 if (!PL_regmatch_slab) {
2795 Newx(PL_regmatch_slab, 1, regmatch_slab);
2796 PL_regmatch_slab->prev = NULL;
2797 PL_regmatch_slab->next = NULL;
2798 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2801 oldsave = PL_savestack_ix;
2802 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2803 SAVEVPTR(PL_regmatch_slab);
2804 SAVEVPTR(PL_regmatch_state);
2806 /* grab next free state slot */
2807 st = ++PL_regmatch_state;
2808 if (st > SLAB_LAST(PL_regmatch_slab))
2809 st = PL_regmatch_state = S_push_slab(aTHX);
2811 /* Note that nextchr is a byte even in UTF */
2812 nextchr = UCHARAT(locinput);
2814 while (scan != NULL) {
2817 SV * const prop = sv_newmortal();
2818 regnode *rnext=regnext(scan);
2819 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2820 regprop(rex, prop, scan);
2822 PerlIO_printf(Perl_debug_log,
2823 "%3"IVdf":%*s%s(%"IVdf")\n",
2824 (IV)(scan - rexi->program), depth*2, "",
2826 (PL_regkind[OP(scan)] == END || !rnext) ?
2827 0 : (IV)(rnext - rexi->program));
2830 next = scan + NEXT_OFF(scan);
2833 state_num = OP(scan);
2836 switch (state_num) {
2838 if (locinput == PL_bostr)
2840 /* reginfo->till = reginfo->bol; */
2845 if (locinput == PL_bostr ||
2846 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2852 if (locinput == PL_bostr)
2856 if (locinput == reginfo->ganch)
2861 /* update the startpoint */
2862 st->u.keeper.val = PL_regoffs[0].start;
2863 PL_reginput = locinput;
2864 PL_regoffs[0].start = locinput - PL_bostr;
2865 PUSH_STATE_GOTO(KEEPS_next, next);
2867 case KEEPS_next_fail:
2868 /* rollback the start point change */
2869 PL_regoffs[0].start = st->u.keeper.val;
2875 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2880 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2882 if (PL_regeol - locinput > 1)
2886 if (PL_regeol != locinput)
2890 if (!nextchr && locinput >= PL_regeol)
2893 locinput += PL_utf8skip[nextchr];
2894 if (locinput > PL_regeol)
2896 nextchr = UCHARAT(locinput);
2899 nextchr = UCHARAT(++locinput);
2902 if (!nextchr && locinput >= PL_regeol)
2904 nextchr = UCHARAT(++locinput);
2907 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2910 locinput += PL_utf8skip[nextchr];
2911 if (locinput > PL_regeol)
2913 nextchr = UCHARAT(locinput);
2916 nextchr = UCHARAT(++locinput);
2920 #define ST st->u.trie
2922 /* In this case the charclass data is available inline so
2923 we can fail fast without a lot of extra overhead.
2925 if (scan->flags == EXACT || !do_utf8) {
2926 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
2928 PerlIO_printf(Perl_debug_log,
2929 "%*s %sfailed to match trie start class...%s\n",
2930 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2939 /* what type of TRIE am I? (utf8 makes this contextual) */
2940 DECL_TRIE_TYPE(scan);
2942 /* what trie are we using right now */
2943 reg_trie_data * const trie
2944 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
2945 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
2946 U32 state = trie->startstate;
2948 if (trie->bitmap && trie_type != trie_utf8_fold &&
2949 !TRIE_BITMAP_TEST(trie,*locinput)
2951 if (trie->states[ state ].wordnum) {
2953 PerlIO_printf(Perl_debug_log,
2954 "%*s %smatched empty string...%s\n",
2955 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2960 PerlIO_printf(Perl_debug_log,
2961 "%*s %sfailed to match trie start class...%s\n",
2962 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2969 U8 *uc = ( U8* )locinput;
2973 U8 *uscan = (U8*)NULL;
2975 SV *sv_accept_buff = NULL;
2976 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2978 ST.accepted = 0; /* how many accepting states we have seen */
2980 ST.jump = trie->jump;
2983 traverse the TRIE keeping track of all accepting states
2984 we transition through until we get to a failing node.
2987 while ( state && uc <= (U8*)PL_regeol ) {
2988 U32 base = trie->states[ state ].trans.base;
2991 /* We use charid to hold the wordnum as we don't use it
2992 for charid until after we have done the wordnum logic.
2993 We define an alias just so that the wordnum logic reads
2996 #define got_wordnum charid
2997 got_wordnum = trie->states[ state ].wordnum;
2999 if ( got_wordnum ) {
3000 if ( ! ST.accepted ) {
3002 /* SAVETMPS; */ /* XXX is this necessary? dmq */
3003 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
3004 sv_accept_buff=newSV(bufflen *
3005 sizeof(reg_trie_accepted) - 1);
3006 SvCUR_set(sv_accept_buff, 0);
3007 SvPOK_on(sv_accept_buff);
3008 sv_2mortal(sv_accept_buff);
3011 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
3014 if (ST.accepted >= bufflen) {
3016 ST.accept_buff =(reg_trie_accepted*)
3017 SvGROW(sv_accept_buff,
3018 bufflen * sizeof(reg_trie_accepted));
3020 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
3021 + sizeof(reg_trie_accepted));
3024 ST.accept_buff[ST.accepted].wordnum = got_wordnum;
3025 ST.accept_buff[ST.accepted].endpos = uc;
3027 } while (trie->nextword && (got_wordnum= trie->nextword[got_wordnum]));
3031 DEBUG_TRIE_EXECUTE_r({
3032 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
3033 PerlIO_printf( Perl_debug_log,
3034 "%*s %sState: %4"UVxf" Accepted: %4"UVxf" ",
3035 2+depth * 2, "", PL_colors[4],
3036 (UV)state, (UV)ST.accepted );
3040 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3041 uscan, len, uvc, charid, foldlen,
3045 (base + charid > trie->uniquecharcount )
3046 && (base + charid - 1 - trie->uniquecharcount
3048 && trie->trans[base + charid - 1 -
3049 trie->uniquecharcount].check == state)
3051 state = trie->trans[base + charid - 1 -
3052 trie->uniquecharcount ].next;
3063 DEBUG_TRIE_EXECUTE_r(
3064 PerlIO_printf( Perl_debug_log,
3065 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3066 charid, uvc, (UV)state, PL_colors[5] );
3073 PerlIO_printf( Perl_debug_log,
3074 "%*s %sgot %"IVdf" possible matches%s\n",
3075 REPORT_CODE_OFF + depth * 2, "",
3076 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3079 goto trie_first_try; /* jump into the fail handler */
3081 case TRIE_next_fail: /* we failed - try next alterative */
3083 REGCP_UNWIND(ST.cp);
3084 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3085 PL_regoffs[n].end = -1;
3086 *PL_reglastparen = n;
3095 ST.lastparen = *PL_reglastparen;
3098 if ( ST.accepted == 1 ) {
3099 /* only one choice left - just continue */
3101 AV *const trie_words
3102 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3103 SV ** const tmp = av_fetch( trie_words,
3104 ST.accept_buff[ 0 ].wordnum-1, 0 );
3105 SV *sv= tmp ? sv_newmortal() : NULL;
3107 PerlIO_printf( Perl_debug_log,
3108 "%*s %sonly one match left: #%d <%s>%s\n",
3109 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3110 ST.accept_buff[ 0 ].wordnum,
3111 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3112 PL_colors[0], PL_colors[1],
3113 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3115 : "not compiled under -Dr",
3118 PL_reginput = (char *)ST.accept_buff[ 0 ].endpos;
3119 /* in this case we free tmps/leave before we call regmatch
3120 as we wont be using accept_buff again. */
3122 locinput = PL_reginput;
3123 nextchr = UCHARAT(locinput);
3124 if ( !ST.jump || !ST.jump[ST.accept_buff[0].wordnum])
3127 scan = ST.me + ST.jump[ST.accept_buff[0].wordnum];
3128 if (!has_cutgroup) {
3133 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3136 continue; /* execute rest of RE */
3139 if ( !ST.accepted-- ) {
3141 PerlIO_printf( Perl_debug_log,
3142 "%*s %sTRIE failed...%s\n",
3143 REPORT_CODE_OFF+depth*2, "",
3154 There are at least two accepting states left. Presumably
3155 the number of accepting states is going to be low,
3156 typically two. So we simply scan through to find the one
3157 with lowest wordnum. Once we find it, we swap the last
3158 state into its place and decrement the size. We then try to
3159 match the rest of the pattern at the point where the word
3160 ends. If we succeed, control just continues along the
3161 regex; if we fail we return here to try the next accepting
3168 for( cur = 1 ; cur <= ST.accepted ; cur++ ) {
3169 DEBUG_TRIE_EXECUTE_r(
3170 PerlIO_printf( Perl_debug_log,
3171 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
3172 REPORT_CODE_OFF + depth * 2, "", PL_colors[4],
3173 (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur,
3174 ST.accept_buff[ cur ].wordnum, PL_colors[5] );
3177 if (ST.accept_buff[cur].wordnum <
3178 ST.accept_buff[best].wordnum)
3183 AV *const trie_words
3184 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3185 SV ** const tmp = av_fetch( trie_words,
3186 ST.accept_buff[ best ].wordnum - 1, 0 );
3187 regnode *nextop=(!ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) ?
3189 ST.me + ST.jump[ST.accept_buff[best].wordnum];
3190 SV *sv= tmp ? sv_newmortal() : NULL;
3192 PerlIO_printf( Perl_debug_log,
3193 "%*s %strying alternation #%d <%s> at node #%d %s\n",
3194 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3195 ST.accept_buff[best].wordnum,
3196 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3197 PL_colors[0], PL_colors[1],
3198 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3199 ) : "not compiled under -Dr",
3200 REG_NODE_NUM(nextop),
3204 if ( best<ST.accepted ) {
3205 reg_trie_accepted tmp = ST.accept_buff[ best ];
3206 ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ];
3207 ST.accept_buff[ ST.accepted ] = tmp;
3210 PL_reginput = (char *)ST.accept_buff[ best ].endpos;
3211 if ( !ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) {
3214 scan = ST.me + ST.jump[ST.accept_buff[best].wordnum];
3216 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3227 char *s = STRING(scan);
3229 if (do_utf8 != UTF) {
3230 /* The target and the pattern have differing utf8ness. */
3232 const char * const e = s + ln;
3235 /* The target is utf8, the pattern is not utf8. */
3240 if (NATIVE_TO_UNI(*(U8*)s) !=
3241 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3249 /* The target is not utf8, the pattern is utf8. */
3254 if (NATIVE_TO_UNI(*((U8*)l)) !=
3255 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3263 nextchr = UCHARAT(locinput);
3266 /* The target and the pattern have the same utf8ness. */
3267 /* Inline the first character, for speed. */
3268 if (UCHARAT(s) != nextchr)
3270 if (PL_regeol - locinput < ln)
3272 if (ln > 1 && memNE(s, locinput, ln))
3275 nextchr = UCHARAT(locinput);
3279 PL_reg_flags |= RF_tainted;
3282 char * const s = STRING(scan);
3285 if (do_utf8 || UTF) {
3286 /* Either target or the pattern are utf8. */
3287 const char * const l = locinput;
3288 char *e = PL_regeol;
3290 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
3291 l, &e, 0, do_utf8)) {
3292 /* One more case for the sharp s:
3293 * pack("U0U*", 0xDF) =~ /ss/i,
3294 * the 0xC3 0x9F are the UTF-8
3295 * byte sequence for the U+00DF. */
3298 toLOWER(s[0]) == 's' &&
3300 toLOWER(s[1]) == 's' &&
3307 nextchr = UCHARAT(locinput);
3311 /* Neither the target and the pattern are utf8. */
3313 /* Inline the first character, for speed. */
3314 if (UCHARAT(s) != nextchr &&
3315 UCHARAT(s) != ((OP(scan) == EXACTF)
3316 ? PL_fold : PL_fold_locale)[nextchr])
3318 if (PL_regeol - locinput < ln)
3320 if (ln > 1 && (OP(scan) == EXACTF
3321 ? ibcmp(s, locinput, ln)
3322 : ibcmp_locale(s, locinput, ln)))
3325 nextchr = UCHARAT(locinput);
3330 STRLEN inclasslen = PL_regeol - locinput;
3332 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3334 if (locinput >= PL_regeol)
3336 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3337 nextchr = UCHARAT(locinput);
3342 nextchr = UCHARAT(locinput);
3343 if (!REGINCLASS(rex, scan, (U8*)locinput))
3345 if (!nextchr && locinput >= PL_regeol)
3347 nextchr = UCHARAT(++locinput);
3351 /* If we might have the case of the German sharp s
3352 * in a casefolding Unicode character class. */
3354 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3355 locinput += SHARP_S_SKIP;
3356 nextchr = UCHARAT(locinput);
3362 PL_reg_flags |= RF_tainted;
3368 LOAD_UTF8_CHARCLASS_ALNUM();
3369 if (!(OP(scan) == ALNUM
3370 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3371 : isALNUM_LC_utf8((U8*)locinput)))
3375 locinput += PL_utf8skip[nextchr];
3376 nextchr = UCHARAT(locinput);
3379 if (!(OP(scan) == ALNUM
3380 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
3382 nextchr = UCHARAT(++locinput);
3385 PL_reg_flags |= RF_tainted;
3388 if (!nextchr && locinput >= PL_regeol)
3391 LOAD_UTF8_CHARCLASS_ALNUM();
3392 if (OP(scan) == NALNUM
3393 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3394 : isALNUM_LC_utf8((U8*)locinput))
3398 locinput += PL_utf8skip[nextchr];
3399 nextchr = UCHARAT(locinput);
3402 if (OP(scan) == NALNUM
3403 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
3405 nextchr = UCHARAT(++locinput);
3409 PL_reg_flags |= RF_tainted;
3413 /* was last char in word? */
3415 if (locinput == PL_bostr)
3418 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3420 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3422 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3423 ln = isALNUM_uni(ln);
3424 LOAD_UTF8_CHARCLASS_ALNUM();
3425 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3428 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3429 n = isALNUM_LC_utf8((U8*)locinput);
3433 ln = (locinput != PL_bostr) ?
3434 UCHARAT(locinput - 1) : '\n';
3435 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3437 n = isALNUM(nextchr);
3440 ln = isALNUM_LC(ln);
3441 n = isALNUM_LC(nextchr);
3444 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3445 OP(scan) == BOUNDL))
3449 PL_reg_flags |= RF_tainted;
3455 if (UTF8_IS_CONTINUED(nextchr)) {
3456 LOAD_UTF8_CHARCLASS_SPACE();
3457 if (!(OP(scan) == SPACE
3458 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3459 : isSPACE_LC_utf8((U8*)locinput)))
3463 locinput += PL_utf8skip[nextchr];
3464 nextchr = UCHARAT(locinput);
3467 if (!(OP(scan) == SPACE
3468 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3470 nextchr = UCHARAT(++locinput);
3473 if (!(OP(scan) == SPACE
3474 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3476 nextchr = UCHARAT(++locinput);
3480 PL_reg_flags |= RF_tainted;
3483 if (!nextchr && locinput >= PL_regeol)
3486 LOAD_UTF8_CHARCLASS_SPACE();
3487 if (OP(scan) == NSPACE
3488 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3489 : isSPACE_LC_utf8((U8*)locinput))
3493 locinput += PL_utf8skip[nextchr];
3494 nextchr = UCHARAT(locinput);
3497 if (OP(scan) == NSPACE
3498 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
3500 nextchr = UCHARAT(++locinput);
3503 PL_reg_flags |= RF_tainted;
3509 LOAD_UTF8_CHARCLASS_DIGIT();
3510 if (!(OP(scan) == DIGIT
3511 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3512 : isDIGIT_LC_utf8((U8*)locinput)))
3516 locinput += PL_utf8skip[nextchr];
3517 nextchr = UCHARAT(locinput);
3520 if (!(OP(scan) == DIGIT
3521 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
3523 nextchr = UCHARAT(++locinput);
3526 PL_reg_flags |= RF_tainted;
3529 if (!nextchr && locinput >= PL_regeol)
3532 LOAD_UTF8_CHARCLASS_DIGIT();
3533 if (OP(scan) == NDIGIT
3534 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3535 : isDIGIT_LC_utf8((U8*)locinput))
3539 locinput += PL_utf8skip[nextchr];
3540 nextchr = UCHARAT(locinput);
3543 if (OP(scan) == NDIGIT
3544 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
3546 nextchr = UCHARAT(++locinput);
3549 if (locinput >= PL_regeol)
3552 LOAD_UTF8_CHARCLASS_MARK();
3553 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3555 locinput += PL_utf8skip[nextchr];
3556 while (locinput < PL_regeol &&
3557 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3558 locinput += UTF8SKIP(locinput);
3559 if (locinput > PL_regeol)
3564 nextchr = UCHARAT(locinput);
3571 PL_reg_flags |= RF_tainted;
3576 n = reg_check_named_buff_matched(rex,scan);
3579 type = REF + ( type - NREF );
3586 PL_reg_flags |= RF_tainted;
3590 n = ARG(scan); /* which paren pair */
3593 ln = PL_regoffs[n].start;
3594 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3595 if (*PL_reglastparen < n || ln == -1)
3596 sayNO; /* Do not match unless seen CLOSEn. */
3597 if (ln == PL_regoffs[n].end)
3601 if (do_utf8 && type != REF) { /* REF can do byte comparison */
3603 const char *e = PL_bostr + PL_regoffs[n].end;
3605 * Note that we can't do the "other character" lookup trick as
3606 * in the 8-bit case (no pun intended) because in Unicode we
3607 * have to map both upper and title case to lower case.
3611 STRLEN ulen1, ulen2;
3612 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3613 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3617 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3618 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3619 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3626 nextchr = UCHARAT(locinput);
3630 /* Inline the first character, for speed. */
3631 if (UCHARAT(s) != nextchr &&
3633 (UCHARAT(s) != (type == REFF
3634 ? PL_fold : PL_fold_locale)[nextchr])))
3636 ln = PL_regoffs[n].end - ln;
3637 if (locinput + ln > PL_regeol)
3639 if (ln > 1 && (type == REF
3640 ? memNE(s, locinput, ln)
3642 ? ibcmp(s, locinput, ln)
3643 : ibcmp_locale(s, locinput, ln))))
3646 nextchr = UCHARAT(locinput);
3656 #define ST st->u.eval
3661 regexp_internal *rei;
3662 regnode *startpoint;
3665 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3666 if (cur_eval && cur_eval->locinput==locinput) {
3667 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3668 Perl_croak(aTHX_ "Infinite recursion in regex");
3669 if ( ++nochange_depth > max_nochange_depth )
3671 "Pattern subroutine nesting without pos change"
3672 " exceeded limit in regex");
3679 (void)ReREFCNT_inc(rex_sv);
3680 if (OP(scan)==GOSUB) {
3681 startpoint = scan + ARG2L(scan);
3682 ST.close_paren = ARG(scan);
3684 startpoint = rei->program+1;
3687 goto eval_recurse_doit;
3689 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3690 if (cur_eval && cur_eval->locinput==locinput) {
3691 if ( ++nochange_depth > max_nochange_depth )
3692 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3697 /* execute the code in the {...} */
3699 SV ** const before = SP;
3700 OP_4tree * const oop = PL_op;
3701 COP * const ocurcop = PL_curcop;
3705 PL_op = (OP_4tree*)rexi->data->data[n];
3706 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3707 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3708 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3709 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3712 SV *sv_mrk = get_sv("REGMARK", 1);
3713 sv_setsv(sv_mrk, sv_yes_mark);
3716 CALLRUNOPS(aTHX); /* Scalar context. */
3719 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3726 PAD_RESTORE_LOCAL(old_comppad);
3727 PL_curcop = ocurcop;
3730 sv_setsv(save_scalar(PL_replgv), ret);
3734 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
3737 /* extract RE object from returned value; compiling if
3743 SV *const sv = SvRV(ret);
3745 if (SvTYPE(sv) == SVt_REGEXP) {
3747 } else if (SvSMAGICAL(sv)) {
3748 mg = mg_find(sv, PERL_MAGIC_qr);
3751 } else if (SvTYPE(ret) == SVt_REGEXP) {
3753 } else if (SvSMAGICAL(ret)) {
3754 if (SvGMAGICAL(ret)) {
3755 /* I don't believe that there is ever qr magic
3757 assert(!mg_find(ret, PERL_MAGIC_qr));
3758 sv_unmagic(ret, PERL_MAGIC_qr);
3761 mg = mg_find(ret, PERL_MAGIC_qr);
3762 /* testing suggests mg only ends up non-NULL for
3763 scalars who were upgraded and compiled in the
3764 else block below. In turn, this is only
3765 triggered in the "postponed utf8 string" tests
3771 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
3775 rx = reg_temp_copy(rx);
3779 const I32 osize = PL_regsize;
3782 assert (SvUTF8(ret));
3783 } else if (SvUTF8(ret)) {
3784 /* Not doing UTF-8, despite what the SV says. Is
3785 this only if we're trapped in use 'bytes'? */
3786 /* Make a copy of the octet sequence, but without
3787 the flag on, as the compiler now honours the
3788 SvUTF8 flag on ret. */
3790 const char *const p = SvPV(ret, len);
3791 ret = newSVpvn_flags(p, len, SVs_TEMP);
3793 rx = CALLREGCOMP(ret, pm_flags);
3795 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3797 /* This isn't a first class regexp. Instead, it's
3798 caching a regexp onto an existing, Perl visible
3800 sv_magic(ret, (SV*) rx, PERL_MAGIC_qr, 0, 0);
3805 re = (struct regexp *)SvANY(rx);
3807 RXp_MATCH_COPIED_off(re);
3808 re->subbeg = rex->subbeg;
3809 re->sublen = rex->sublen;
3812 debug_start_match(re_sv, do_utf8, locinput, PL_regeol,
3813 "Matching embedded");
3815 startpoint = rei->program + 1;
3816 ST.close_paren = 0; /* only used for GOSUB */
3817 /* borrowed from regtry */
3818 if (PL_reg_start_tmpl <= re->nparens) {
3819 PL_reg_start_tmpl = re->nparens*3/2 + 3;
3820 if(PL_reg_start_tmp)
3821 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
3823 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
3826 eval_recurse_doit: /* Share code with GOSUB below this line */
3827 /* run the pattern returned from (??{...}) */
3828 ST.cp = regcppush(0); /* Save *all* the positions. */
3829 REGCP_SET(ST.lastcp);
3831 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
3833 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
3834 PL_reglastparen = &re->lastparen;
3835 PL_reglastcloseparen = &re->lastcloseparen;
3837 re->lastcloseparen = 0;
3839 PL_reginput = locinput;
3842 /* XXXX This is too dramatic a measure... */
3845 ST.toggle_reg_flags = PL_reg_flags;
3847 PL_reg_flags |= RF_utf8;
3849 PL_reg_flags &= ~RF_utf8;
3850 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
3852 ST.prev_rex = rex_sv;
3853 ST.prev_curlyx = cur_curlyx;
3854 SETREX(rex_sv,re_sv);
3859 ST.prev_eval = cur_eval;
3861 /* now continue from first node in postoned RE */
3862 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
3865 /* logical is 1, /(?(?{...})X|Y)/ */
3866 sw = (bool)SvTRUE(ret);
3871 case EVAL_AB: /* cleanup after a successful (??{A})B */
3872 /* note: this is called twice; first after popping B, then A */
3873 PL_reg_flags ^= ST.toggle_reg_flags;
3874 ReREFCNT_dec(rex_sv);
3875 SETREX(rex_sv,ST.prev_rex);
3876 rex = (struct regexp *)SvANY(rex_sv);
3877 rexi = RXi_GET(rex);
3879 cur_eval = ST.prev_eval;
3880 cur_curlyx = ST.prev_curlyx;
3882 PL_reglastparen = &rex->lastparen;
3883 PL_reglastcloseparen = &rex->lastcloseparen;
3885 /* XXXX This is too dramatic a measure... */
3887 if ( nochange_depth )
3892 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
3893 /* note: this is called twice; first after popping B, then A */
3894 PL_reg_flags ^= ST.toggle_reg_flags;
3895 ReREFCNT_dec(rex_sv);
3896 SETREX(rex_sv,ST.prev_rex);
3897 rex = (struct regexp *)SvANY(rex_sv);
3898 rexi = RXi_GET(rex);
3899 PL_reglastparen = &rex->lastparen;
3900 PL_reglastcloseparen = &rex->lastcloseparen;
3902 PL_reginput = locinput;
3903 REGCP_UNWIND(ST.lastcp);
3905 cur_eval = ST.prev_eval;
3906 cur_curlyx = ST.prev_curlyx;
3907 /* XXXX This is too dramatic a measure... */
3909 if ( nochange_depth )
3915 n = ARG(scan); /* which paren pair */
3916 PL_reg_start_tmp[n] = locinput;
3922 n = ARG(scan); /* which paren pair */
3923 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
3924 PL_regoffs[n].end = locinput - PL_bostr;
3925 /*if (n > PL_regsize)
3927 if (n > *PL_reglastparen)
3928 *PL_reglastparen = n;
3929 *PL_reglastcloseparen = n;
3930 if (cur_eval && cur_eval->u.eval.close_paren == n) {
3938 cursor && OP(cursor)!=END;
3939 cursor=regnext(cursor))
3941 if ( OP(cursor)==CLOSE ){
3943 if ( n <= lastopen ) {
3945 = PL_reg_start_tmp[n] - PL_bostr;
3946 PL_regoffs[n].end = locinput - PL_bostr;
3947 /*if (n > PL_regsize)
3949 if (n > *PL_reglastparen)
3950 *PL_reglastparen = n;
3951 *PL_reglastcloseparen = n;
3952 if ( n == ARG(scan) || (cur_eval &&
3953 cur_eval->u.eval.close_paren == n))
3962 n = ARG(scan); /* which paren pair */
3963 sw = (bool)(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
3966 /* reg_check_named_buff_matched returns 0 for no match */
3967 sw = (bool)(0 < reg_check_named_buff_matched(rex,scan));
3971 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
3977 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3979 next = NEXTOPER(NEXTOPER(scan));
3981 next = scan + ARG(scan);
3982 if (OP(next) == IFTHEN) /* Fake one. */
3983 next = NEXTOPER(NEXTOPER(next));
3987 logical = scan->flags;
3990 /*******************************************************************
3992 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
3993 pattern, where A and B are subpatterns. (For simple A, CURLYM or
3994 STAR/PLUS/CURLY/CURLYN are used instead.)
3996 A*B is compiled as <CURLYX><A><WHILEM><B>
3998 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
3999 state, which contains the current count, initialised to -1. It also sets
4000 cur_curlyx to point to this state, with any previous value saved in the
4003 CURLYX then jumps straight to the WHILEM op, rather than executing A,
4004 since the pattern may possibly match zero times (i.e. it's a while {} loop
4005 rather than a do {} while loop).
4007 Each entry to WHILEM represents a successful match of A. The count in the
4008 CURLYX block is incremented, another WHILEM state is pushed, and execution
4009 passes to A or B depending on greediness and the current count.
4011 For example, if matching against the string a1a2a3b (where the aN are
4012 substrings that match /A/), then the match progresses as follows: (the
4013 pushed states are interspersed with the bits of strings matched so far):
4016 <CURLYX cnt=0><WHILEM>
4017 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4018 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4019 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4020 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
4022 (Contrast this with something like CURLYM, which maintains only a single
4026 a1 <CURLYM cnt=1> a2
4027 a1 a2 <CURLYM cnt=2> a3
4028 a1 a2 a3 <CURLYM cnt=3> b
4031 Each WHILEM state block marks a point to backtrack to upon partial failure
4032 of A or B, and also contains some minor state data related to that
4033 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4034 overall state, such as the count, and pointers to the A and B ops.
4036 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4037 must always point to the *current* CURLYX block, the rules are:
4039 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4040 and set cur_curlyx to point the new block.
4042 When popping the CURLYX block after a successful or unsuccessful match,
4043 restore the previous cur_curlyx.
4045 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4046 to the outer one saved in the CURLYX block.
4048 When popping the WHILEM block after a successful or unsuccessful B match,
4049 restore the previous cur_curlyx.
4051 Here's an example for the pattern (AI* BI)*BO
4052 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4055 curlyx backtrack stack
4056 ------ ---------------
4058 CO <CO prev=NULL> <WO>
4059 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4060 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4061 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4063 At this point the pattern succeeds, and we work back down the stack to
4064 clean up, restoring as we go:
4066 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4067 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4068 CO <CO prev=NULL> <WO>
4071 *******************************************************************/
4073 #define ST st->u.curlyx
4075 case CURLYX: /* start of /A*B/ (for complex A) */
4077 /* No need to save/restore up to this paren */
4078 I32 parenfloor = scan->flags;
4080 assert(next); /* keep Coverity happy */
4081 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4084 /* XXXX Probably it is better to teach regpush to support
4085 parenfloor > PL_regsize... */
4086 if (parenfloor > (I32)*PL_reglastparen)
4087 parenfloor = *PL_reglastparen; /* Pessimization... */
4089 ST.prev_curlyx= cur_curlyx;
4091 ST.cp = PL_savestack_ix;
4093 /* these fields contain the state of the current curly.
4094 * they are accessed by subsequent WHILEMs */
4095 ST.parenfloor = parenfloor;
4096 ST.min = ARG1(scan);
4097 ST.max = ARG2(scan);
4098 ST.A = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
4102 ST.count = -1; /* this will be updated by WHILEM */
4103 ST.lastloc = NULL; /* this will be updated by WHILEM */
4105 PL_reginput = locinput;
4106 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4110 case CURLYX_end: /* just finished matching all of A*B */
4111 cur_curlyx = ST.prev_curlyx;
4115 case CURLYX_end_fail: /* just failed to match all of A*B */
4117 cur_curlyx = ST.prev_curlyx;
4123 #define ST st->u.whilem
4125 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4127 /* see the discussion above about CURLYX/WHILEM */
4129 assert(cur_curlyx); /* keep Coverity happy */
4130 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4131 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4132 ST.cache_offset = 0;
4135 PL_reginput = locinput;
4137 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4138 "%*s whilem: matched %ld out of %ld..%ld\n",
4139 REPORT_CODE_OFF+depth*2, "", (long)n,
4140 (long)cur_curlyx->u.curlyx.min,
4141 (long)cur_curlyx->u.curlyx.max)
4144 /* First just match a string of min A's. */
4146 if (n < cur_curlyx->u.curlyx.min) {
4147 cur_curlyx->u.curlyx.lastloc = locinput;
4148 PUSH_STATE_GOTO(WHILEM_A_pre, cur_curlyx->u.curlyx.A);
4152 /* If degenerate A matches "", assume A done. */
4154 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4155 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4156 "%*s whilem: empty match detected, trying continuation...\n",
4157 REPORT_CODE_OFF+depth*2, "")
4159 goto do_whilem_B_max;
4162 /* super-linear cache processing */
4166 if (!PL_reg_maxiter) {
4167 /* start the countdown: Postpone detection until we
4168 * know the match is not *that* much linear. */
4169 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4170 /* possible overflow for long strings and many CURLYX's */
4171 if (PL_reg_maxiter < 0)
4172 PL_reg_maxiter = I32_MAX;
4173 PL_reg_leftiter = PL_reg_maxiter;
4176 if (PL_reg_leftiter-- == 0) {
4177 /* initialise cache */
4178 const I32 size = (PL_reg_maxiter + 7)/8;
4179 if (PL_reg_poscache) {
4180 if ((I32)PL_reg_poscache_size < size) {
4181 Renew(PL_reg_poscache, size, char);
4182 PL_reg_poscache_size = size;
4184 Zero(PL_reg_poscache, size, char);
4187 PL_reg_poscache_size = size;
4188 Newxz(PL_reg_poscache, size, char);
4190 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4191 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4192 PL_colors[4], PL_colors[5])
4196 if (PL_reg_leftiter < 0) {
4197 /* have we already failed at this position? */
4199 offset = (scan->flags & 0xf) - 1
4200 + (locinput - PL_bostr) * (scan->flags>>4);
4201 mask = 1 << (offset % 8);
4203 if (PL_reg_poscache[offset] & mask) {
4204 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4205 "%*s whilem: (cache) already tried at this position...\n",
4206 REPORT_CODE_OFF+depth*2, "")
4208 sayNO; /* cache records failure */
4210 ST.cache_offset = offset;
4211 ST.cache_mask = mask;
4215 /* Prefer B over A for minimal matching. */
4217 if (cur_curlyx->u.curlyx.minmod) {
4218 ST.save_curlyx = cur_curlyx;
4219 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4220 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4221 REGCP_SET(ST.lastcp);
4222 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4226 /* Prefer A over B for maximal matching. */
4228 if (n < cur_curlyx->u.curlyx.max) { /* More greed allowed? */
4229 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4230 cur_curlyx->u.curlyx.lastloc = locinput;
4231 REGCP_SET(ST.lastcp);
4232 PUSH_STATE_GOTO(WHILEM_A_max, cur_curlyx->u.curlyx.A);
4235 goto do_whilem_B_max;
4239 case WHILEM_B_min: /* just matched B in a minimal match */
4240 case WHILEM_B_max: /* just matched B in a maximal match */
4241 cur_curlyx = ST.save_curlyx;
4245 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4246 cur_curlyx = ST.save_curlyx;
4247 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4248 cur_curlyx->u.curlyx.count--;
4252 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4253 REGCP_UNWIND(ST.lastcp);
4256 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4257 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4258 cur_curlyx->u.curlyx.count--;
4262 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4263 REGCP_UNWIND(ST.lastcp);
4264 regcppop(rex); /* Restore some previous $<digit>s? */
4265 PL_reginput = locinput;
4266 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4267 "%*s whilem: failed, trying continuation...\n",
4268 REPORT_CODE_OFF+depth*2, "")
4271 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4272 && ckWARN(WARN_REGEXP)
4273 && !(PL_reg_flags & RF_warned))
4275 PL_reg_flags |= RF_warned;
4276 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4277 "Complex regular subexpression recursion",
4282 ST.save_curlyx = cur_curlyx;
4283 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4284 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4287 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4288 cur_curlyx = ST.save_curlyx;
4289 REGCP_UNWIND(ST.lastcp);
4292 if (cur_curlyx->u.curlyx.count >= cur_curlyx->u.curlyx.max) {
4293 /* Maximum greed exceeded */
4294 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4295 && ckWARN(WARN_REGEXP)
4296 && !(PL_reg_flags & RF_warned))
4298 PL_reg_flags |= RF_warned;
4299 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4300 "%s limit (%d) exceeded",
4301 "Complex regular subexpression recursion",
4304 cur_curlyx->u.curlyx.count--;
4308 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4309 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4311 /* Try grabbing another A and see if it helps. */
4312 PL_reginput = locinput;
4313 cur_curlyx->u.curlyx.lastloc = locinput;
4314 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4315 REGCP_SET(ST.lastcp);
4316 PUSH_STATE_GOTO(WHILEM_A_min, ST.save_curlyx->u.curlyx.A);
4320 #define ST st->u.branch
4322 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4323 next = scan + ARG(scan);
4326 scan = NEXTOPER(scan);
4329 case BRANCH: /* /(...|A|...)/ */
4330 scan = NEXTOPER(scan); /* scan now points to inner node */
4331 ST.lastparen = *PL_reglastparen;
4332 ST.next_branch = next;
4334 PL_reginput = locinput;
4336 /* Now go into the branch */
4338 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4340 PUSH_STATE_GOTO(BRANCH_next, scan);
4344 PL_reginput = locinput;
4345 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4346 (SV*)rexi->data->data[ ARG( scan ) ];
4347 PUSH_STATE_GOTO(CUTGROUP_next,next);
4349 case CUTGROUP_next_fail:
4352 if (st->u.mark.mark_name)
4353 sv_commit = st->u.mark.mark_name;
4359 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4364 REGCP_UNWIND(ST.cp);
4365 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4366 PL_regoffs[n].end = -1;
4367 *PL_reglastparen = n;
4368 /*dmq: *PL_reglastcloseparen = n; */
4369 scan = ST.next_branch;
4370 /* no more branches? */
4371 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4373 PerlIO_printf( Perl_debug_log,
4374 "%*s %sBRANCH failed...%s\n",
4375 REPORT_CODE_OFF+depth*2, "",
4381 continue; /* execute next BRANCH[J] op */
4389 #define ST st->u.curlym
4391 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4393 /* This is an optimisation of CURLYX that enables us to push
4394 * only a single backtracking state, no matter now many matches
4395 * there are in {m,n}. It relies on the pattern being constant
4396 * length, with no parens to influence future backrefs
4400 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4402 /* if paren positive, emulate an OPEN/CLOSE around A */
4404 U32 paren = ST.me->flags;
4405 if (paren > PL_regsize)
4407 if (paren > *PL_reglastparen)
4408 *PL_reglastparen = paren;
4409 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4417 ST.c1 = CHRTEST_UNINIT;
4420 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4423 curlym_do_A: /* execute the A in /A{m,n}B/ */
4424 PL_reginput = locinput;
4425 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4428 case CURLYM_A: /* we've just matched an A */
4429 locinput = st->locinput;
4430 nextchr = UCHARAT(locinput);
4433 /* after first match, determine A's length: u.curlym.alen */
4434 if (ST.count == 1) {
4435 if (PL_reg_match_utf8) {
4437 while (s < PL_reginput) {
4443 ST.alen = PL_reginput - locinput;
4446 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4449 PerlIO_printf(Perl_debug_log,
4450 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4451 (int)(REPORT_CODE_OFF+(depth*2)), "",
4452 (IV) ST.count, (IV)ST.alen)
4455 locinput = PL_reginput;
4457 if (cur_eval && cur_eval->u.eval.close_paren &&
4458 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4461 if ( ST.count < (ST.minmod ? ARG1(ST.me) : ARG2(ST.me)) )
4462 goto curlym_do_A; /* try to match another A */
4463 goto curlym_do_B; /* try to match B */
4465 case CURLYM_A_fail: /* just failed to match an A */
4466 REGCP_UNWIND(ST.cp);
4468 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4469 || (cur_eval && cur_eval->u.eval.close_paren &&
4470 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4473 curlym_do_B: /* execute the B in /A{m,n}B/ */
4474 PL_reginput = locinput;
4475 if (ST.c1 == CHRTEST_UNINIT) {
4476 /* calculate c1 and c2 for possible match of 1st char
4477 * following curly */
4478 ST.c1 = ST.c2 = CHRTEST_VOID;
4479 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4480 regnode *text_node = ST.B;
4481 if (! HAS_TEXT(text_node))
4482 FIND_NEXT_IMPT(text_node);
4485 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4487 But the former is redundant in light of the latter.
4489 if this changes back then the macro for
4490 IS_TEXT and friends need to change.
4492 if (PL_regkind[OP(text_node)] == EXACT)
4495 ST.c1 = (U8)*STRING(text_node);
4497 (IS_TEXTF(text_node))
4499 : (IS_TEXTFL(text_node))
4500 ? PL_fold_locale[ST.c1]
4507 PerlIO_printf(Perl_debug_log,
4508 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4509 (int)(REPORT_CODE_OFF+(depth*2)),
4512 if (ST.c1 != CHRTEST_VOID
4513 && UCHARAT(PL_reginput) != ST.c1
4514 && UCHARAT(PL_reginput) != ST.c2)
4516 /* simulate B failing */
4518 PerlIO_printf(Perl_debug_log,
4519 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4520 (int)(REPORT_CODE_OFF+(depth*2)),"",
4523 state_num = CURLYM_B_fail;
4524 goto reenter_switch;
4528 /* mark current A as captured */
4529 I32 paren = ST.me->flags;
4531 PL_regoffs[paren].start
4532 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4533 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4534 /*dmq: *PL_reglastcloseparen = paren; */
4537 PL_regoffs[paren].end = -1;
4538 if (cur_eval && cur_eval->u.eval.close_paren &&
4539 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4548 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4551 case CURLYM_B_fail: /* just failed to match a B */
4552 REGCP_UNWIND(ST.cp);
4554 if (ST.count == ARG2(ST.me) /* max */)
4556 goto curlym_do_A; /* try to match a further A */
4558 /* backtrack one A */
4559 if (ST.count == ARG1(ST.me) /* min */)
4562 locinput = HOPc(locinput, -ST.alen);
4563 goto curlym_do_B; /* try to match B */
4566 #define ST st->u.curly
4568 #define CURLY_SETPAREN(paren, success) \
4571 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4572 PL_regoffs[paren].end = locinput - PL_bostr; \
4573 *PL_reglastcloseparen = paren; \
4576 PL_regoffs[paren].end = -1; \
4579 case STAR: /* /A*B/ where A is width 1 */
4583 scan = NEXTOPER(scan);
4585 case PLUS: /* /A+B/ where A is width 1 */
4589 scan = NEXTOPER(scan);
4591 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4592 ST.paren = scan->flags; /* Which paren to set */
4593 if (ST.paren > PL_regsize)
4594 PL_regsize = ST.paren;
4595 if (ST.paren > *PL_reglastparen)
4596 *PL_reglastparen = ST.paren;
4597 ST.min = ARG1(scan); /* min to match */
4598 ST.max = ARG2(scan); /* max to match */
4599 if (cur_eval && cur_eval->u.eval.close_paren &&
4600 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4604 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4606 case CURLY: /* /A{m,n}B/ where A is width 1 */
4608 ST.min = ARG1(scan); /* min to match */
4609 ST.max = ARG2(scan); /* max to match */
4610 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4613 * Lookahead to avoid useless match attempts
4614 * when we know what character comes next.
4616 * Used to only do .*x and .*?x, but now it allows
4617 * for )'s, ('s and (?{ ... })'s to be in the way
4618 * of the quantifier and the EXACT-like node. -- japhy
4621 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4623 if (HAS_TEXT(next) || JUMPABLE(next)) {
4625 regnode *text_node = next;
4627 if (! HAS_TEXT(text_node))
4628 FIND_NEXT_IMPT(text_node);
4630 if (! HAS_TEXT(text_node))
4631 ST.c1 = ST.c2 = CHRTEST_VOID;
4633 if ( PL_regkind[OP(text_node)] != EXACT ) {
4634 ST.c1 = ST.c2 = CHRTEST_VOID;
4635 goto assume_ok_easy;
4638 s = (U8*)STRING(text_node);
4640 /* Currently we only get here when
4642 PL_rekind[OP(text_node)] == EXACT
4644 if this changes back then the macro for IS_TEXT and
4645 friends need to change. */
4648 if (IS_TEXTF(text_node))
4649 ST.c2 = PL_fold[ST.c1];
4650 else if (IS_TEXTFL(text_node))
4651 ST.c2 = PL_fold_locale[ST.c1];
4654 if (IS_TEXTF(text_node)) {
4655 STRLEN ulen1, ulen2;
4656 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4657 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4659 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4660 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4662 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4664 0 : UTF8_ALLOW_ANY);
4665 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4667 0 : UTF8_ALLOW_ANY);
4669 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4671 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4676 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4683 ST.c1 = ST.c2 = CHRTEST_VOID;
4688 PL_reginput = locinput;
4691 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4694 locinput = PL_reginput;
4696 if (ST.c1 == CHRTEST_VOID)
4697 goto curly_try_B_min;
4699 ST.oldloc = locinput;
4701 /* set ST.maxpos to the furthest point along the
4702 * string that could possibly match */
4703 if (ST.max == REG_INFTY) {
4704 ST.maxpos = PL_regeol - 1;
4706 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4710 int m = ST.max - ST.min;
4711 for (ST.maxpos = locinput;
4712 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4713 ST.maxpos += UTF8SKIP(ST.maxpos);
4716 ST.maxpos = locinput + ST.max - ST.min;
4717 if (ST.maxpos >= PL_regeol)
4718 ST.maxpos = PL_regeol - 1;
4720 goto curly_try_B_min_known;
4724 ST.count = regrepeat(rex, ST.A, ST.max, depth);
4725 locinput = PL_reginput;
4726 if (ST.count < ST.min)
4728 if ((ST.count > ST.min)
4729 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4731 /* A{m,n} must come at the end of the string, there's
4732 * no point in backing off ... */
4734 /* ...except that $ and \Z can match before *and* after
4735 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4736 We may back off by one in this case. */
4737 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4741 goto curly_try_B_max;
4746 case CURLY_B_min_known_fail:
4747 /* failed to find B in a non-greedy match where c1,c2 valid */
4748 if (ST.paren && ST.count)
4749 PL_regoffs[ST.paren].end = -1;
4751 PL_reginput = locinput; /* Could be reset... */
4752 REGCP_UNWIND(ST.cp);
4753 /* Couldn't or didn't -- move forward. */
4754 ST.oldloc = locinput;
4756 locinput += UTF8SKIP(locinput);
4760 curly_try_B_min_known:
4761 /* find the next place where 'B' could work, then call B */
4765 n = (ST.oldloc == locinput) ? 0 : 1;
4766 if (ST.c1 == ST.c2) {
4768 /* set n to utf8_distance(oldloc, locinput) */
4769 while (locinput <= ST.maxpos &&
4770 utf8n_to_uvchr((U8*)locinput,
4771 UTF8_MAXBYTES, &len,
4772 uniflags) != (UV)ST.c1) {
4778 /* set n to utf8_distance(oldloc, locinput) */
4779 while (locinput <= ST.maxpos) {
4781 const UV c = utf8n_to_uvchr((U8*)locinput,
4782 UTF8_MAXBYTES, &len,
4784 if (c == (UV)ST.c1 || c == (UV)ST.c2)
4792 if (ST.c1 == ST.c2) {
4793 while (locinput <= ST.maxpos &&
4794 UCHARAT(locinput) != ST.c1)
4798 while (locinput <= ST.maxpos
4799 && UCHARAT(locinput) != ST.c1
4800 && UCHARAT(locinput) != ST.c2)
4803 n = locinput - ST.oldloc;
4805 if (locinput > ST.maxpos)
4807 /* PL_reginput == oldloc now */
4810 if (regrepeat(rex, ST.A, n, depth) < n)
4813 PL_reginput = locinput;
4814 CURLY_SETPAREN(ST.paren, ST.count);
4815 if (cur_eval && cur_eval->u.eval.close_paren &&
4816 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4819 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
4824 case CURLY_B_min_fail:
4825 /* failed to find B in a non-greedy match where c1,c2 invalid */
4826 if (ST.paren && ST.count)
4827 PL_regoffs[ST.paren].end = -1;
4829 REGCP_UNWIND(ST.cp);
4830 /* failed -- move forward one */
4831 PL_reginput = locinput;
4832 if (regrepeat(rex, ST.A, 1, depth)) {
4834 locinput = PL_reginput;
4835 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
4836 ST.count > 0)) /* count overflow ? */
4839 CURLY_SETPAREN(ST.paren, ST.count);
4840 if (cur_eval && cur_eval->u.eval.close_paren &&
4841 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4844 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
4852 /* a successful greedy match: now try to match B */
4853 if (cur_eval && cur_eval->u.eval.close_paren &&
4854 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4859 if (ST.c1 != CHRTEST_VOID)
4860 c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput,
4861 UTF8_MAXBYTES, 0, uniflags)
4862 : (UV) UCHARAT(PL_reginput);
4863 /* If it could work, try it. */
4864 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
4865 CURLY_SETPAREN(ST.paren, ST.count);
4866 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
4871 case CURLY_B_max_fail:
4872 /* failed to find B in a greedy match */
4873 if (ST.paren && ST.count)
4874 PL_regoffs[ST.paren].end = -1;
4876 REGCP_UNWIND(ST.cp);
4878 if (--ST.count < ST.min)
4880 PL_reginput = locinput = HOPc(locinput, -1);
4881 goto curly_try_B_max;
4888 /* we've just finished A in /(??{A})B/; now continue with B */
4890 st->u.eval.toggle_reg_flags
4891 = cur_eval->u.eval.toggle_reg_flags;
4892 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
4894 st->u.eval.prev_rex = rex_sv; /* inner */
4895 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
4896 rex = (struct regexp *)SvANY(rex_sv);
4897 rexi = RXi_GET(rex);
4898 cur_curlyx = cur_eval->u.eval.prev_curlyx;
4899 ReREFCNT_inc(rex_sv);
4900 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
4901 REGCP_SET(st->u.eval.lastcp);
4902 PL_reginput = locinput;
4904 /* Restore parens of the outer rex without popping the
4906 tmpix = PL_savestack_ix;
4907 PL_savestack_ix = cur_eval->u.eval.lastcp;
4909 PL_savestack_ix = tmpix;
4911 st->u.eval.prev_eval = cur_eval;
4912 cur_eval = cur_eval->u.eval.prev_eval;
4914 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
4915 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
4916 if ( nochange_depth )
4919 PUSH_YES_STATE_GOTO(EVAL_AB,
4920 st->u.eval.prev_eval->u.eval.B); /* match B */
4923 if (locinput < reginfo->till) {
4924 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4925 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
4927 (long)(locinput - PL_reg_starttry),
4928 (long)(reginfo->till - PL_reg_starttry),
4931 sayNO_SILENT; /* Cannot match: too short. */
4933 PL_reginput = locinput; /* put where regtry can find it */
4934 sayYES; /* Success! */
4936 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
4938 PerlIO_printf(Perl_debug_log,
4939 "%*s %ssubpattern success...%s\n",
4940 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
4941 PL_reginput = locinput; /* put where regtry can find it */
4942 sayYES; /* Success! */
4945 #define ST st->u.ifmatch
4947 case SUSPEND: /* (?>A) */
4949 PL_reginput = locinput;
4952 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
4954 goto ifmatch_trivial_fail_test;
4956 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
4958 ifmatch_trivial_fail_test:
4960 char * const s = HOPBACKc(locinput, scan->flags);
4965 sw = 1 - (bool)ST.wanted;
4969 next = scan + ARG(scan);
4977 PL_reginput = locinput;
4981 ST.logical = logical;
4982 /* execute body of (?...A) */
4983 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
4986 case IFMATCH_A_fail: /* body of (?...A) failed */
4987 ST.wanted = !ST.wanted;
4990 case IFMATCH_A: /* body of (?...A) succeeded */
4992 sw = (bool)ST.wanted;
4994 else if (!ST.wanted)
4997 if (OP(ST.me) == SUSPEND)
4998 locinput = PL_reginput;
5000 locinput = PL_reginput = st->locinput;
5001 nextchr = UCHARAT(locinput);
5003 scan = ST.me + ARG(ST.me);
5006 continue; /* execute B */
5011 next = scan + ARG(scan);
5016 reginfo->cutpoint = PL_regeol;
5019 PL_reginput = locinput;
5021 sv_yes_mark = sv_commit = (SV*)rexi->data->data[ ARG( scan ) ];
5022 PUSH_STATE_GOTO(COMMIT_next,next);
5024 case COMMIT_next_fail:
5031 #define ST st->u.mark
5033 ST.prev_mark = mark_state;
5034 ST.mark_name = sv_commit = sv_yes_mark
5035 = (SV*)rexi->data->data[ ARG( scan ) ];
5037 ST.mark_loc = PL_reginput = locinput;
5038 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5040 case MARKPOINT_next:
5041 mark_state = ST.prev_mark;
5044 case MARKPOINT_next_fail:
5045 if (popmark && sv_eq(ST.mark_name,popmark))
5047 if (ST.mark_loc > startpoint)
5048 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5049 popmark = NULL; /* we found our mark */
5050 sv_commit = ST.mark_name;
5053 PerlIO_printf(Perl_debug_log,
5054 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5055 REPORT_CODE_OFF+depth*2, "",
5056 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5059 mark_state = ST.prev_mark;
5060 sv_yes_mark = mark_state ?
5061 mark_state->u.mark.mark_name : NULL;
5065 PL_reginput = locinput;
5067 /* (*SKIP) : if we fail we cut here*/
5068 ST.mark_name = NULL;
5069 ST.mark_loc = locinput;
5070 PUSH_STATE_GOTO(SKIP_next,next);
5072 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5073 otherwise do nothing. Meaning we need to scan
5075 regmatch_state *cur = mark_state;
5076 SV *find = (SV*)rexi->data->data[ ARG( scan ) ];
5079 if ( sv_eq( cur->u.mark.mark_name,
5082 ST.mark_name = find;
5083 PUSH_STATE_GOTO( SKIP_next, next );
5085 cur = cur->u.mark.prev_mark;
5088 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5090 case SKIP_next_fail:
5092 /* (*CUT:NAME) - Set up to search for the name as we
5093 collapse the stack*/
5094 popmark = ST.mark_name;
5096 /* (*CUT) - No name, we cut here.*/
5097 if (ST.mark_loc > startpoint)
5098 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5099 /* but we set sv_commit to latest mark_name if there
5100 is one so they can test to see how things lead to this
5103 sv_commit=mark_state->u.mark.mark_name;
5111 if ( n == (U32)what_len_TRICKYFOLD(locinput,do_utf8,ln) ) {
5113 } else if ( 0xDF == n && !do_utf8 && !UTF ) {
5116 U8 folded[UTF8_MAXBYTES_CASE+1];
5118 const char * const l = locinput;
5119 char *e = PL_regeol;
5120 to_uni_fold(n, folded, &foldlen);
5122 if (ibcmp_utf8((const char*) folded, 0, foldlen, 1,
5123 l, &e, 0, do_utf8)) {
5128 nextchr = UCHARAT(locinput);
5131 if ((n=is_LNBREAK(locinput,do_utf8))) {
5133 nextchr = UCHARAT(locinput);
5138 #define CASE_CLASS(nAmE) \
5140 if ((n=is_##nAmE(locinput,do_utf8))) { \
5142 nextchr = UCHARAT(locinput); \
5147 if ((n=is_##nAmE(locinput,do_utf8))) { \
5150 locinput += UTF8SKIP(locinput); \
5151 nextchr = UCHARAT(locinput); \
5156 CASE_CLASS(HORIZWS);
5160 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5161 PTR2UV(scan), OP(scan));
5162 Perl_croak(aTHX_ "regexp memory corruption");
5166 /* switch break jumps here */
5167 scan = next; /* prepare to execute the next op and ... */
5168 continue; /* ... jump back to the top, reusing st */
5172 /* push a state that backtracks on success */
5173 st->u.yes.prev_yes_state = yes_state;
5177 /* push a new regex state, then continue at scan */
5179 regmatch_state *newst;
5182 regmatch_state *cur = st;
5183 regmatch_state *curyes = yes_state;
5185 regmatch_slab *slab = PL_regmatch_slab;
5186 for (;curd > -1;cur--,curd--) {
5187 if (cur < SLAB_FIRST(slab)) {
5189 cur = SLAB_LAST(slab);
5191 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5192 REPORT_CODE_OFF + 2 + depth * 2,"",
5193 curd, PL_reg_name[cur->resume_state],
5194 (curyes == cur) ? "yes" : ""
5197 curyes = cur->u.yes.prev_yes_state;
5200 DEBUG_STATE_pp("push")
5203 st->locinput = locinput;
5205 if (newst > SLAB_LAST(PL_regmatch_slab))
5206 newst = S_push_slab(aTHX);
5207 PL_regmatch_state = newst;
5209 locinput = PL_reginput;
5210 nextchr = UCHARAT(locinput);
5218 * We get here only if there's trouble -- normally "case END" is
5219 * the terminating point.
5221 Perl_croak(aTHX_ "corrupted regexp pointers");
5227 /* we have successfully completed a subexpression, but we must now
5228 * pop to the state marked by yes_state and continue from there */
5229 assert(st != yes_state);
5231 while (st != yes_state) {
5233 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5234 PL_regmatch_slab = PL_regmatch_slab->prev;
5235 st = SLAB_LAST(PL_regmatch_slab);
5239 DEBUG_STATE_pp("pop (no final)");
5241 DEBUG_STATE_pp("pop (yes)");
5247 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5248 || yes_state > SLAB_LAST(PL_regmatch_slab))
5250 /* not in this slab, pop slab */
5251 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5252 PL_regmatch_slab = PL_regmatch_slab->prev;
5253 st = SLAB_LAST(PL_regmatch_slab);
5255 depth -= (st - yes_state);
5258 yes_state = st->u.yes.prev_yes_state;
5259 PL_regmatch_state = st;
5262 locinput= st->locinput;
5263 nextchr = UCHARAT(locinput);
5265 state_num = st->resume_state + no_final;
5266 goto reenter_switch;
5269 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5270 PL_colors[4], PL_colors[5]));
5272 if (PL_reg_eval_set) {
5273 /* each successfully executed (?{...}) block does the equivalent of
5274 * local $^R = do {...}
5275 * When popping the save stack, all these locals would be undone;
5276 * bypass this by setting the outermost saved $^R to the latest
5278 if (oreplsv != GvSV(PL_replgv))
5279 sv_setsv(oreplsv, GvSV(PL_replgv));
5286 PerlIO_printf(Perl_debug_log,
5287 "%*s %sfailed...%s\n",
5288 REPORT_CODE_OFF+depth*2, "",
5289 PL_colors[4], PL_colors[5])
5301 /* there's a previous state to backtrack to */
5303 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5304 PL_regmatch_slab = PL_regmatch_slab->prev;
5305 st = SLAB_LAST(PL_regmatch_slab);
5307 PL_regmatch_state = st;
5308 locinput= st->locinput;
5309 nextchr = UCHARAT(locinput);
5311 DEBUG_STATE_pp("pop");
5313 if (yes_state == st)
5314 yes_state = st->u.yes.prev_yes_state;
5316 state_num = st->resume_state + 1; /* failure = success + 1 */
5317 goto reenter_switch;
5322 if (rex->intflags & PREGf_VERBARG_SEEN) {
5323 SV *sv_err = get_sv("REGERROR", 1);
5324 SV *sv_mrk = get_sv("REGMARK", 1);
5326 sv_commit = &PL_sv_no;
5328 sv_yes_mark = &PL_sv_yes;
5331 sv_commit = &PL_sv_yes;
5332 sv_yes_mark = &PL_sv_no;
5334 sv_setsv(sv_err, sv_commit);
5335 sv_setsv(sv_mrk, sv_yes_mark);
5338 /* clean up; in particular, free all slabs above current one */
5339 LEAVE_SCOPE(oldsave);
5345 - regrepeat - repeatedly match something simple, report how many
5348 * [This routine now assumes that it will only match on things of length 1.
5349 * That was true before, but now we assume scan - reginput is the count,
5350 * rather than incrementing count on every character. [Er, except utf8.]]
5353 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5356 register char *scan;
5358 register char *loceol = PL_regeol;
5359 register I32 hardcount = 0;
5360 register bool do_utf8 = PL_reg_match_utf8;
5362 PERL_UNUSED_ARG(depth);
5365 PERL_ARGS_ASSERT_REGREPEAT;
5368 if (max == REG_INFTY)
5370 else if (max < loceol - scan)
5371 loceol = scan + max;
5376 while (scan < loceol && hardcount < max && *scan != '\n') {
5377 scan += UTF8SKIP(scan);
5381 while (scan < loceol && *scan != '\n')
5388 while (scan < loceol && hardcount < max) {
5389 scan += UTF8SKIP(scan);
5399 case EXACT: /* length of string is 1 */
5401 while (scan < loceol && UCHARAT(scan) == c)
5404 case EXACTF: /* length of string is 1 */
5406 while (scan < loceol &&
5407 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5410 case EXACTFL: /* length of string is 1 */
5411 PL_reg_flags |= RF_tainted;
5413 while (scan < loceol &&
5414 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5420 while (hardcount < max && scan < loceol &&
5421 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
5422 scan += UTF8SKIP(scan);
5426 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5433 LOAD_UTF8_CHARCLASS_ALNUM();
5434 while (hardcount < max && scan < loceol &&
5435 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5436 scan += UTF8SKIP(scan);
5440 while (scan < loceol && isALNUM(*scan))
5445 PL_reg_flags |= RF_tainted;
5448 while (hardcount < max && scan < loceol &&
5449 isALNUM_LC_utf8((U8*)scan)) {
5450 scan += UTF8SKIP(scan);
5454 while (scan < loceol && isALNUM_LC(*scan))
5461 LOAD_UTF8_CHARCLASS_ALNUM();
5462 while (hardcount < max && scan < loceol &&
5463 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5464 scan += UTF8SKIP(scan);
5468 while (scan < loceol && !isALNUM(*scan))
5473 PL_reg_flags |= RF_tainted;
5476 while (hardcount < max && scan < loceol &&
5477 !isALNUM_LC_utf8((U8*)scan)) {
5478 scan += UTF8SKIP(scan);
5482 while (scan < loceol && !isALNUM_LC(*scan))
5489 LOAD_UTF8_CHARCLASS_SPACE();
5490 while (hardcount < max && scan < loceol &&
5492 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5493 scan += UTF8SKIP(scan);
5497 while (scan < loceol && isSPACE(*scan))
5502 PL_reg_flags |= RF_tainted;
5505 while (hardcount < max && scan < loceol &&
5506 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5507 scan += UTF8SKIP(scan);
5511 while (scan < loceol && isSPACE_LC(*scan))
5518 LOAD_UTF8_CHARCLASS_SPACE();
5519 while (hardcount < max && scan < loceol &&
5521 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5522 scan += UTF8SKIP(scan);
5526 while (scan < loceol && !isSPACE(*scan))
5531 PL_reg_flags |= RF_tainted;
5534 while (hardcount < max && scan < loceol &&
5535 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5536 scan += UTF8SKIP(scan);
5540 while (scan < loceol && !isSPACE_LC(*scan))
5547 LOAD_UTF8_CHARCLASS_DIGIT();
5548 while (hardcount < max && scan < loceol &&
5549 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5550 scan += UTF8SKIP(scan);
5554 while (scan < loceol && isDIGIT(*scan))
5561 LOAD_UTF8_CHARCLASS_DIGIT();
5562 while (hardcount < max && scan < loceol &&
5563 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5564 scan += UTF8SKIP(scan);
5568 while (scan < loceol && !isDIGIT(*scan))
5574 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5580 LNBREAK can match two latin chars, which is ok,
5581 because we have a null terminated string, but we
5582 have to use hardcount in this situation
5584 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5593 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5598 while (scan < loceol && is_HORIZWS_latin1(scan))
5605 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5606 scan += UTF8SKIP(scan);
5610 while (scan < loceol && !is_HORIZWS_latin1(scan))
5618 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5623 while (scan < loceol && is_VERTWS_latin1(scan))
5631 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5632 scan += UTF8SKIP(scan);
5636 while (scan < loceol && !is_VERTWS_latin1(scan))
5642 default: /* Called on something of 0 width. */
5643 break; /* So match right here or not at all. */
5649 c = scan - PL_reginput;
5653 GET_RE_DEBUG_FLAGS_DECL;
5655 SV * const prop = sv_newmortal();
5656 regprop(prog, prop, p);
5657 PerlIO_printf(Perl_debug_log,
5658 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5659 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5667 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5669 - regclass_swash - prepare the utf8 swash
5673 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5679 RXi_GET_DECL(prog,progi);
5680 const struct reg_data * const data = prog ? progi->data : NULL;
5682 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5684 if (data && data->count) {
5685 const U32 n = ARG(node);
5687 if (data->what[n] == 's') {
5688 SV * const rv = (SV*)data->data[n];
5689 AV * const av = MUTABLE_AV(SvRV((SV*)rv));
5690 SV **const ary = AvARRAY(av);
5693 /* See the end of regcomp.c:S_regclass() for
5694 * documentation of these array elements. */
5697 a = SvROK(ary[1]) ? &ary[1] : NULL;
5698 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5702 else if (si && doinit) {
5703 sw = swash_init("utf8", "", si, 1, 0);
5704 (void)av_store(av, 1, sw);
5721 - reginclass - determine if a character falls into a character class
5723 The n is the ANYOF regnode, the p is the target string, lenp
5724 is pointer to the maximum length of how far to go in the p
5725 (if the lenp is zero, UTF8SKIP(p) is used),
5726 do_utf8 tells whether the target string is in UTF-8.
5731 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
5734 const char flags = ANYOF_FLAGS(n);
5740 PERL_ARGS_ASSERT_REGINCLASS;
5742 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
5743 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5744 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) | UTF8_CHECK_ONLY);
5745 /* see [perl #37836] for UTF8_ALLOW_ANYUV */
5746 if (len == (STRLEN)-1)
5747 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
5750 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
5751 if (do_utf8 || (flags & ANYOF_UNICODE)) {
5754 if (do_utf8 && !ANYOF_RUNTIME(n)) {
5755 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
5758 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
5762 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
5765 if (swash_fetch(sw, p, do_utf8))
5767 else if (flags & ANYOF_FOLD) {
5768 if (!match && lenp && av) {
5770 for (i = 0; i <= av_len(av); i++) {
5771 SV* const sv = *av_fetch(av, i, FALSE);
5773 const char * const s = SvPV_const(sv, len);
5775 if (len <= plen && memEQ(s, (char*)p, len)) {
5783 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
5786 to_utf8_fold(p, tmpbuf, &tmplen);
5787 if (swash_fetch(sw, tmpbuf, do_utf8))
5793 if (match && lenp && *lenp == 0)
5794 *lenp = UNISKIP(NATIVE_TO_UNI(c));
5796 if (!match && c < 256) {
5797 if (ANYOF_BITMAP_TEST(n, c))
5799 else if (flags & ANYOF_FOLD) {
5802 if (flags & ANYOF_LOCALE) {
5803 PL_reg_flags |= RF_tainted;
5804 f = PL_fold_locale[c];
5808 if (f != c && ANYOF_BITMAP_TEST(n, f))
5812 if (!match && (flags & ANYOF_CLASS)) {
5813 PL_reg_flags |= RF_tainted;
5815 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
5816 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
5817 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
5818 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
5819 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
5820 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
5821 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
5822 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
5823 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
5824 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
5825 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
5826 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
5827 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
5828 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
5829 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
5830 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
5831 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
5832 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
5833 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
5834 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
5835 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
5836 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
5837 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
5838 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
5839 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
5840 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
5841 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
5842 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
5843 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
5844 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
5845 ) /* How's that for a conditional? */
5852 return (flags & ANYOF_INVERT) ? !match : match;
5856 S_reghop3(U8 *s, I32 off, const U8* lim)
5860 PERL_ARGS_ASSERT_REGHOP3;
5863 while (off-- && s < lim) {
5864 /* XXX could check well-formedness here */
5869 while (off++ && s > lim) {
5871 if (UTF8_IS_CONTINUED(*s)) {
5872 while (s > lim && UTF8_IS_CONTINUATION(*s))
5875 /* XXX could check well-formedness here */
5882 /* there are a bunch of places where we use two reghop3's that should
5883 be replaced with this routine. but since thats not done yet
5884 we ifdef it out - dmq
5887 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
5891 PERL_ARGS_ASSERT_REGHOP4;
5894 while (off-- && s < rlim) {
5895 /* XXX could check well-formedness here */
5900 while (off++ && s > llim) {
5902 if (UTF8_IS_CONTINUED(*s)) {
5903 while (s > llim && UTF8_IS_CONTINUATION(*s))
5906 /* XXX could check well-formedness here */
5914 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
5918 PERL_ARGS_ASSERT_REGHOPMAYBE3;
5921 while (off-- && s < lim) {
5922 /* XXX could check well-formedness here */
5929 while (off++ && s > lim) {
5931 if (UTF8_IS_CONTINUED(*s)) {
5932 while (s > lim && UTF8_IS_CONTINUATION(*s))
5935 /* XXX could check well-formedness here */
5944 restore_pos(pTHX_ void *arg)
5947 regexp * const rex = (regexp *)arg;
5948 if (PL_reg_eval_set) {
5949 if (PL_reg_oldsaved) {
5950 rex->subbeg = PL_reg_oldsaved;
5951 rex->sublen = PL_reg_oldsavedlen;
5952 #ifdef PERL_OLD_COPY_ON_WRITE
5953 rex->saved_copy = PL_nrs;
5955 RXp_MATCH_COPIED_on(rex);
5957 PL_reg_magic->mg_len = PL_reg_oldpos;
5958 PL_reg_eval_set = 0;
5959 PL_curpm = PL_reg_oldcurpm;
5964 S_to_utf8_substr(pTHX_ register regexp *prog)
5968 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
5971 if (prog->substrs->data[i].substr
5972 && !prog->substrs->data[i].utf8_substr) {
5973 SV* const sv = newSVsv(prog->substrs->data[i].substr);
5974 prog->substrs->data[i].utf8_substr = sv;
5975 sv_utf8_upgrade(sv);
5976 if (SvVALID(prog->substrs->data[i].substr)) {
5977 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
5978 if (flags & FBMcf_TAIL) {
5979 /* Trim the trailing \n that fbm_compile added last
5981 SvCUR_set(sv, SvCUR(sv) - 1);
5982 /* Whilst this makes the SV technically "invalid" (as its
5983 buffer is no longer followed by "\0") when fbm_compile()
5984 adds the "\n" back, a "\0" is restored. */
5986 fbm_compile(sv, flags);
5988 if (prog->substrs->data[i].substr == prog->check_substr)
5989 prog->check_utf8 = sv;
5995 S_to_byte_substr(pTHX_ register regexp *prog)
6000 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6003 if (prog->substrs->data[i].utf8_substr
6004 && !prog->substrs->data[i].substr) {
6005 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6006 if (sv_utf8_downgrade(sv, TRUE)) {
6007 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6009 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6010 if (flags & FBMcf_TAIL) {
6011 /* Trim the trailing \n that fbm_compile added last
6013 SvCUR_set(sv, SvCUR(sv) - 1);
6015 fbm_compile(sv, flags);
6021 prog->substrs->data[i].substr = sv;
6022 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6023 prog->check_substr = sv;
6030 * c-indentation-style: bsd
6032 * indent-tabs-mode: t
6035 * ex: set ts=8 sts=4 sw=4 noet: