5 * One Ring to rule them all, One Ring to find them
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
12 /* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
14 * a regular expression.
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
26 /* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
31 /* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
36 #ifdef PERL_EXT_RE_BUILD
41 * pregcomp and pregexec -- regsub and regerror are not used in perl
43 * Copyright (c) 1986 by University of Toronto.
44 * Written by Henry Spencer. Not derived from licensed software.
46 * Permission is granted to anyone to use this software for any
47 * purpose on any computer system, and to redistribute it freely,
48 * subject to the following restrictions:
50 * 1. The author is not responsible for the consequences of use of
51 * this software, no matter how awful, even if they arise
54 * 2. The origin of this software must not be misrepresented, either
55 * by explicit claim or by omission.
57 * 3. Altered versions must be plainly marked as such, and must not
58 * be misrepresented as being the original software.
60 **** Alterations to Henry's code are...
62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
66 **** You may distribute under the terms of either the GNU General Public
67 **** License or the Artistic License, as specified in the README file.
69 * Beware that some of this code is subtly aware of the way operator
70 * precedence is structured in regular expressions. Serious changes in
71 * regular-expression syntax might require a total rethink.
74 #define PERL_IN_REGEXEC_C
77 #ifdef PERL_IN_XSUB_RE
83 #define RF_tainted 1 /* tainted information used? */
84 #define RF_warned 2 /* warned about big count? */
86 #define RF_utf8 8 /* Pattern contains multibyte chars? */
88 #define UTF ((PL_reg_flags & RF_utf8) != 0)
90 #define RS_init 1 /* eval environment created */
91 #define RS_set 2 /* replsv value is set */
97 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
103 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define HOPc(pos,off) \
107 (char *)(PL_reg_match_utf8 \
108 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
110 #define HOPBACKc(pos, off) \
111 (char*)(PL_reg_match_utf8\
112 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
113 : (pos - off >= PL_bostr) \
117 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
118 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
120 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
121 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
122 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
123 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
124 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
125 #define LOAD_UTF8_CHARCLASS_MARK() LOAD_UTF8_CHARCLASS(mark, "\xcd\x86")
127 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
129 /* for use after a quantifier and before an EXACT-like node -- japhy */
130 /* it would be nice to rework regcomp.sym to generate this stuff. sigh */
131 #define JUMPABLE(rn) ( \
133 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
135 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
136 OP(rn) == PLUS || OP(rn) == MINMOD || \
137 OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \
138 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
140 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
142 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
145 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
146 we don't need this definition. */
147 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
148 #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
149 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
152 /* ... so we use this as its faster. */
153 #define IS_TEXT(rn) ( OP(rn)==EXACT )
154 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
155 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
160 Search for mandatory following text node; for lookahead, the text must
161 follow but for lookbehind (rn->flags != 0) we skip to the next step.
163 #define FIND_NEXT_IMPT(rn) STMT_START { \
164 while (JUMPABLE(rn)) { \
165 const OPCODE type = OP(rn); \
166 if (type == SUSPEND || PL_regkind[type] == CURLY) \
167 rn = NEXTOPER(NEXTOPER(rn)); \
168 else if (type == PLUS) \
170 else if (type == IFMATCH) \
171 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
172 else rn += NEXT_OFF(rn); \
177 static void restore_pos(pTHX_ void *arg);
180 S_regcppush(pTHX_ I32 parenfloor)
183 const int retval = PL_savestack_ix;
184 #define REGCP_PAREN_ELEMS 4
185 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
187 GET_RE_DEBUG_FLAGS_DECL;
189 if (paren_elems_to_push < 0)
190 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
192 #define REGCP_OTHER_ELEMS 7
193 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
195 for (p = PL_regsize; p > parenfloor; p--) {
196 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
197 SSPUSHINT(PL_regoffs[p].end);
198 SSPUSHINT(PL_regoffs[p].start);
199 SSPUSHPTR(PL_reg_start_tmp[p]);
201 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
202 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
203 (UV)p, (IV)PL_regoffs[p].start,
204 (IV)(PL_reg_start_tmp[p] - PL_bostr),
205 (IV)PL_regoffs[p].end
208 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
209 SSPUSHPTR(PL_regoffs);
210 SSPUSHINT(PL_regsize);
211 SSPUSHINT(*PL_reglastparen);
212 SSPUSHINT(*PL_reglastcloseparen);
213 SSPUSHPTR(PL_reginput);
214 #define REGCP_FRAME_ELEMS 2
215 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
216 * are needed for the regexp context stack bookkeeping. */
217 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
218 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
223 /* These are needed since we do not localize EVAL nodes: */
224 #define REGCP_SET(cp) \
226 PerlIO_printf(Perl_debug_log, \
227 " Setting an EVAL scope, savestack=%"IVdf"\n", \
228 (IV)PL_savestack_ix)); \
231 #define REGCP_UNWIND(cp) \
233 if (cp != PL_savestack_ix) \
234 PerlIO_printf(Perl_debug_log, \
235 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
236 (IV)(cp), (IV)PL_savestack_ix)); \
240 S_regcppop(pTHX_ const regexp *rex)
245 GET_RE_DEBUG_FLAGS_DECL;
247 PERL_ARGS_ASSERT_REGCPPOP;
249 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
251 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
252 i = SSPOPINT; /* Parentheses elements to pop. */
253 input = (char *) SSPOPPTR;
254 *PL_reglastcloseparen = SSPOPINT;
255 *PL_reglastparen = SSPOPINT;
256 PL_regsize = SSPOPINT;
257 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
260 /* Now restore the parentheses context. */
261 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
262 i > 0; i -= REGCP_PAREN_ELEMS) {
264 U32 paren = (U32)SSPOPINT;
265 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
266 PL_regoffs[paren].start = SSPOPINT;
268 if (paren <= *PL_reglastparen)
269 PL_regoffs[paren].end = tmps;
271 PerlIO_printf(Perl_debug_log,
272 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
273 (UV)paren, (IV)PL_regoffs[paren].start,
274 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
275 (IV)PL_regoffs[paren].end,
276 (paren > *PL_reglastparen ? "(no)" : ""));
280 if (*PL_reglastparen + 1 <= rex->nparens) {
281 PerlIO_printf(Perl_debug_log,
282 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
283 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
287 /* It would seem that the similar code in regtry()
288 * already takes care of this, and in fact it is in
289 * a better location to since this code can #if 0-ed out
290 * but the code in regtry() is needed or otherwise tests
291 * requiring null fields (pat.t#187 and split.t#{13,14}
292 * (as of patchlevel 7877) will fail. Then again,
293 * this code seems to be necessary or otherwise
294 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
295 * --jhi updated by dapm */
296 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
298 PL_regoffs[i].start = -1;
299 PL_regoffs[i].end = -1;
305 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
308 * pregexec and friends
311 #ifndef PERL_IN_XSUB_RE
313 - pregexec - match a regexp against a string
316 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
317 char *strbeg, I32 minend, SV *screamer, U32 nosave)
318 /* strend: pointer to null at end of string */
319 /* strbeg: real beginning of string */
320 /* minend: end of match must be >=minend after stringarg. */
321 /* nosave: For optimizations. */
323 PERL_ARGS_ASSERT_PREGEXEC;
326 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
327 nosave ? 0 : REXEC_COPY_STR);
332 * Need to implement the following flags for reg_anch:
334 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
336 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
337 * INTUIT_AUTORITATIVE_ML
338 * INTUIT_ONCE_NOML - Intuit can match in one location only.
341 * Another flag for this function: SECOND_TIME (so that float substrs
342 * with giant delta may be not rechecked).
345 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
347 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
348 Otherwise, only SvCUR(sv) is used to get strbeg. */
350 /* XXXX We assume that strpos is strbeg unless sv. */
352 /* XXXX Some places assume that there is a fixed substring.
353 An update may be needed if optimizer marks as "INTUITable"
354 RExen without fixed substrings. Similarly, it is assumed that
355 lengths of all the strings are no more than minlen, thus they
356 cannot come from lookahead.
357 (Or minlen should take into account lookahead.)
358 NOTE: Some of this comment is not correct. minlen does now take account
359 of lookahead/behind. Further research is required. -- demerphq
363 /* A failure to find a constant substring means that there is no need to make
364 an expensive call to REx engine, thus we celebrate a failure. Similarly,
365 finding a substring too deep into the string means that less calls to
366 regtry() should be needed.
368 REx compiler's optimizer found 4 possible hints:
369 a) Anchored substring;
371 c) Whether we are anchored (beginning-of-line or \G);
372 d) First node (of those at offset 0) which may distingush positions;
373 We use a)b)d) and multiline-part of c), and try to find a position in the
374 string which does not contradict any of them.
377 /* Most of decisions we do here should have been done at compile time.
378 The nodes of the REx which we used for the search should have been
379 deleted from the finite automaton. */
382 Perl_re_intuit_start(pTHX_ REGEXP * const prog, SV *sv, char *strpos,
383 char *strend, const U32 flags, re_scream_pos_data *data)
386 register I32 start_shift = 0;
387 /* Should be nonnegative! */
388 register I32 end_shift = 0;
393 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
395 register char *other_last = NULL; /* other substr checked before this */
396 char *check_at = NULL; /* check substr found at this pos */
397 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
398 RXi_GET_DECL(prog,progi);
400 const char * const i_strpos = strpos;
402 GET_RE_DEBUG_FLAGS_DECL;
404 PERL_ARGS_ASSERT_RE_INTUIT_START;
406 RX_MATCH_UTF8_set(prog,do_utf8);
409 PL_reg_flags |= RF_utf8;
412 debug_start_match(prog, do_utf8, strpos, strend,
413 sv ? "Guessing start of match in sv for"
414 : "Guessing start of match in string for");
417 /* CHR_DIST() would be more correct here but it makes things slow. */
418 if (prog->minlen > strend - strpos) {
419 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
420 "String too short... [re_intuit_start]\n"));
424 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
427 if (!prog->check_utf8 && prog->check_substr)
428 to_utf8_substr(prog);
429 check = prog->check_utf8;
431 if (!prog->check_substr && prog->check_utf8)
432 to_byte_substr(prog);
433 check = prog->check_substr;
435 if (check == &PL_sv_undef) {
436 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
437 "Non-utf8 string cannot match utf8 check string\n"));
440 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
441 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
442 || ( (prog->extflags & RXf_ANCH_BOL)
443 && !multiline ) ); /* Check after \n? */
446 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
447 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
448 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
450 && (strpos != strbeg)) {
451 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
454 if (prog->check_offset_min == prog->check_offset_max &&
455 !(prog->extflags & RXf_CANY_SEEN)) {
456 /* Substring at constant offset from beg-of-str... */
459 s = HOP3c(strpos, prog->check_offset_min, strend);
462 slen = SvCUR(check); /* >= 1 */
464 if ( strend - s > slen || strend - s < slen - 1
465 || (strend - s == slen && strend[-1] != '\n')) {
466 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
469 /* Now should match s[0..slen-2] */
471 if (slen && (*SvPVX_const(check) != *s
473 && memNE(SvPVX_const(check), s, slen)))) {
475 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
479 else if (*SvPVX_const(check) != *s
480 || ((slen = SvCUR(check)) > 1
481 && memNE(SvPVX_const(check), s, slen)))
484 goto success_at_start;
487 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
489 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
490 end_shift = prog->check_end_shift;
493 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
494 - (SvTAIL(check) != 0);
495 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
497 if (end_shift < eshift)
501 else { /* Can match at random position */
504 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
505 end_shift = prog->check_end_shift;
507 /* end shift should be non negative here */
510 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
512 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
513 (IV)end_shift, RX_PRECOMP(prog));
517 /* Find a possible match in the region s..strend by looking for
518 the "check" substring in the region corrected by start/end_shift. */
521 I32 srch_start_shift = start_shift;
522 I32 srch_end_shift = end_shift;
523 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
524 srch_end_shift -= ((strbeg - s) - srch_start_shift);
525 srch_start_shift = strbeg - s;
527 DEBUG_OPTIMISE_MORE_r({
528 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
529 (IV)prog->check_offset_min,
530 (IV)srch_start_shift,
532 (IV)prog->check_end_shift);
535 if (flags & REXEC_SCREAM) {
536 I32 p = -1; /* Internal iterator of scream. */
537 I32 * const pp = data ? data->scream_pos : &p;
539 if (PL_screamfirst[BmRARE(check)] >= 0
540 || ( BmRARE(check) == '\n'
541 && (BmPREVIOUS(check) == SvCUR(check) - 1)
543 s = screaminstr(sv, check,
544 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
547 /* we may be pointing at the wrong string */
548 if (s && RXp_MATCH_COPIED(prog))
549 s = strbeg + (s - SvPVX_const(sv));
551 *data->scream_olds = s;
556 if (prog->extflags & RXf_CANY_SEEN) {
557 start_point= (U8*)(s + srch_start_shift);
558 end_point= (U8*)(strend - srch_end_shift);
560 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
561 end_point= HOP3(strend, -srch_end_shift, strbeg);
563 DEBUG_OPTIMISE_MORE_r({
564 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
565 (int)(end_point - start_point),
566 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
570 s = fbm_instr( start_point, end_point,
571 check, multiline ? FBMrf_MULTILINE : 0);
574 /* Update the count-of-usability, remove useless subpatterns,
578 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
579 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
580 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
581 (s ? "Found" : "Did not find"),
582 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
583 ? "anchored" : "floating"),
586 (s ? " at offset " : "...\n") );
591 /* Finish the diagnostic message */
592 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
594 /* XXX dmq: first branch is for positive lookbehind...
595 Our check string is offset from the beginning of the pattern.
596 So we need to do any stclass tests offset forward from that
605 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
606 Start with the other substr.
607 XXXX no SCREAM optimization yet - and a very coarse implementation
608 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
609 *always* match. Probably should be marked during compile...
610 Probably it is right to do no SCREAM here...
613 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
614 : (prog->float_substr && prog->anchored_substr))
616 /* Take into account the "other" substring. */
617 /* XXXX May be hopelessly wrong for UTF... */
620 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
623 char * const last = HOP3c(s, -start_shift, strbeg);
625 char * const saved_s = s;
628 t = s - prog->check_offset_max;
629 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
631 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
636 t = HOP3c(t, prog->anchored_offset, strend);
637 if (t < other_last) /* These positions already checked */
639 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
642 /* XXXX It is not documented what units *_offsets are in.
643 We assume bytes, but this is clearly wrong.
644 Meaning this code needs to be carefully reviewed for errors.
648 /* On end-of-str: see comment below. */
649 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
650 if (must == &PL_sv_undef) {
652 DEBUG_r(must = prog->anchored_utf8); /* for debug */
657 HOP3(HOP3(last1, prog->anchored_offset, strend)
658 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
660 multiline ? FBMrf_MULTILINE : 0
663 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
664 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
665 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
666 (s ? "Found" : "Contradicts"),
667 quoted, RE_SV_TAIL(must));
672 if (last1 >= last2) {
673 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
674 ", giving up...\n"));
677 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
678 ", trying floating at offset %ld...\n",
679 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
680 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
681 s = HOP3c(last, 1, strend);
685 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
686 (long)(s - i_strpos)));
687 t = HOP3c(s, -prog->anchored_offset, strbeg);
688 other_last = HOP3c(s, 1, strend);
696 else { /* Take into account the floating substring. */
698 char * const saved_s = s;
701 t = HOP3c(s, -start_shift, strbeg);
703 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
704 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
705 last = HOP3c(t, prog->float_max_offset, strend);
706 s = HOP3c(t, prog->float_min_offset, strend);
709 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
710 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
711 /* fbm_instr() takes into account exact value of end-of-str
712 if the check is SvTAIL(ed). Since false positives are OK,
713 and end-of-str is not later than strend we are OK. */
714 if (must == &PL_sv_undef) {
716 DEBUG_r(must = prog->float_utf8); /* for debug message */
719 s = fbm_instr((unsigned char*)s,
720 (unsigned char*)last + SvCUR(must)
722 must, multiline ? FBMrf_MULTILINE : 0);
724 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
725 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
726 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
727 (s ? "Found" : "Contradicts"),
728 quoted, RE_SV_TAIL(must));
732 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
733 ", giving up...\n"));
736 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
737 ", trying anchored starting at offset %ld...\n",
738 (long)(saved_s + 1 - i_strpos)));
740 s = HOP3c(t, 1, strend);
744 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
745 (long)(s - i_strpos)));
746 other_last = s; /* Fix this later. --Hugo */
756 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
758 DEBUG_OPTIMISE_MORE_r(
759 PerlIO_printf(Perl_debug_log,
760 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
761 (IV)prog->check_offset_min,
762 (IV)prog->check_offset_max,
770 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
772 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
775 /* Fixed substring is found far enough so that the match
776 cannot start at strpos. */
778 if (ml_anch && t[-1] != '\n') {
779 /* Eventually fbm_*() should handle this, but often
780 anchored_offset is not 0, so this check will not be wasted. */
781 /* XXXX In the code below we prefer to look for "^" even in
782 presence of anchored substrings. And we search even
783 beyond the found float position. These pessimizations
784 are historical artefacts only. */
786 while (t < strend - prog->minlen) {
788 if (t < check_at - prog->check_offset_min) {
789 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
790 /* Since we moved from the found position,
791 we definitely contradict the found anchored
792 substr. Due to the above check we do not
793 contradict "check" substr.
794 Thus we can arrive here only if check substr
795 is float. Redo checking for "other"=="fixed".
798 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
799 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
800 goto do_other_anchored;
802 /* We don't contradict the found floating substring. */
803 /* XXXX Why not check for STCLASS? */
805 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
806 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
809 /* Position contradicts check-string */
810 /* XXXX probably better to look for check-string
811 than for "\n", so one should lower the limit for t? */
812 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
813 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
814 other_last = strpos = s = t + 1;
819 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
820 PL_colors[0], PL_colors[1]));
824 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
825 PL_colors[0], PL_colors[1]));
829 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
832 /* The found string does not prohibit matching at strpos,
833 - no optimization of calling REx engine can be performed,
834 unless it was an MBOL and we are not after MBOL,
835 or a future STCLASS check will fail this. */
837 /* Even in this situation we may use MBOL flag if strpos is offset
838 wrt the start of the string. */
839 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
840 && (strpos != strbeg) && strpos[-1] != '\n'
841 /* May be due to an implicit anchor of m{.*foo} */
842 && !(prog->intflags & PREGf_IMPLICIT))
847 DEBUG_EXECUTE_r( if (ml_anch)
848 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
849 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
852 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
854 prog->check_utf8 /* Could be deleted already */
855 && --BmUSEFUL(prog->check_utf8) < 0
856 && (prog->check_utf8 == prog->float_utf8)
858 prog->check_substr /* Could be deleted already */
859 && --BmUSEFUL(prog->check_substr) < 0
860 && (prog->check_substr == prog->float_substr)
863 /* If flags & SOMETHING - do not do it many times on the same match */
864 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
865 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
866 if (do_utf8 ? prog->check_substr : prog->check_utf8)
867 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
868 prog->check_substr = prog->check_utf8 = NULL; /* disable */
869 prog->float_substr = prog->float_utf8 = NULL; /* clear */
870 check = NULL; /* abort */
872 /* XXXX This is a remnant of the old implementation. It
873 looks wasteful, since now INTUIT can use many
875 prog->extflags &= ~RXf_USE_INTUIT;
882 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
883 /* trie stclasses are too expensive to use here, we are better off to
884 leave it to regmatch itself */
885 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
886 /* minlen == 0 is possible if regstclass is \b or \B,
887 and the fixed substr is ''$.
888 Since minlen is already taken into account, s+1 is before strend;
889 accidentally, minlen >= 1 guaranties no false positives at s + 1
890 even for \b or \B. But (minlen? 1 : 0) below assumes that
891 regstclass does not come from lookahead... */
892 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
893 This leaves EXACTF only, which is dealt with in find_byclass(). */
894 const U8* const str = (U8*)STRING(progi->regstclass);
895 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
896 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
899 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
900 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
901 else if (prog->float_substr || prog->float_utf8)
902 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
906 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
907 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
910 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
913 const char *what = NULL;
915 if (endpos == strend) {
916 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
917 "Could not match STCLASS...\n") );
920 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
921 "This position contradicts STCLASS...\n") );
922 if ((prog->extflags & RXf_ANCH) && !ml_anch)
924 /* Contradict one of substrings */
925 if (prog->anchored_substr || prog->anchored_utf8) {
926 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
927 DEBUG_EXECUTE_r( what = "anchored" );
929 s = HOP3c(t, 1, strend);
930 if (s + start_shift + end_shift > strend) {
931 /* XXXX Should be taken into account earlier? */
932 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
933 "Could not match STCLASS...\n") );
938 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
939 "Looking for %s substr starting at offset %ld...\n",
940 what, (long)(s + start_shift - i_strpos)) );
943 /* Have both, check_string is floating */
944 if (t + start_shift >= check_at) /* Contradicts floating=check */
945 goto retry_floating_check;
946 /* Recheck anchored substring, but not floating... */
950 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
951 "Looking for anchored substr starting at offset %ld...\n",
952 (long)(other_last - i_strpos)) );
953 goto do_other_anchored;
955 /* Another way we could have checked stclass at the
956 current position only: */
961 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
962 "Looking for /%s^%s/m starting at offset %ld...\n",
963 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
966 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
968 /* Check is floating subtring. */
969 retry_floating_check:
970 t = check_at - start_shift;
971 DEBUG_EXECUTE_r( what = "floating" );
972 goto hop_and_restart;
975 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
976 "By STCLASS: moving %ld --> %ld\n",
977 (long)(t - i_strpos), (long)(s - i_strpos))
981 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
982 "Does not contradict STCLASS...\n");
987 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
988 PL_colors[4], (check ? "Guessed" : "Giving up"),
989 PL_colors[5], (long)(s - i_strpos)) );
992 fail_finish: /* Substring not found */
993 if (prog->check_substr || prog->check_utf8) /* could be removed already */
994 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
996 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
997 PL_colors[4], PL_colors[5]));
1001 #define DECL_TRIE_TYPE(scan) \
1002 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1003 trie_type = (scan->flags != EXACT) \
1004 ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \
1005 : (do_utf8 ? trie_utf8 : trie_plain)
1007 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1008 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1009 UV uvc_unfolded = 0; \
1010 switch (trie_type) { \
1011 case trie_utf8_fold: \
1012 if ( foldlen>0 ) { \
1013 uvc_unfolded = uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1018 uvc_unfolded = uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1019 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1020 foldlen -= UNISKIP( uvc ); \
1021 uscan = foldbuf + UNISKIP( uvc ); \
1024 case trie_latin_utf8_fold: \
1025 if ( foldlen>0 ) { \
1026 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1032 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1033 foldlen -= UNISKIP( uvc ); \
1034 uscan = foldbuf + UNISKIP( uvc ); \
1038 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1046 charid = trie->charmap[ uvc ]; \
1050 if (widecharmap) { \
1051 SV** const svpp = hv_fetch(widecharmap, \
1052 (char*)&uvc, sizeof(UV), 0); \
1054 charid = (U16)SvIV(*svpp); \
1057 if (!charid && trie_type == trie_utf8_fold && !UTF) { \
1058 charid = trie->charmap[uvc_unfolded]; \
1062 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1064 char *my_strend= (char *)strend; \
1067 !ibcmp_utf8(s, &my_strend, 0, do_utf8, \
1068 m, NULL, ln, (bool)UTF)) \
1069 && (!reginfo || regtry(reginfo, &s)) ) \
1072 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1073 uvchr_to_utf8(tmpbuf, c); \
1074 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1076 && (f == c1 || f == c2) \
1078 !ibcmp_utf8(s, &my_strend, 0, do_utf8,\
1079 m, NULL, ln, (bool)UTF)) \
1080 && (!reginfo || regtry(reginfo, &s)) ) \
1086 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1090 && (ln == 1 || !(OP(c) == EXACTF \
1092 : ibcmp_locale(s, m, ln))) \
1093 && (!reginfo || regtry(reginfo, &s)) ) \
1099 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1101 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1107 #define REXEC_FBC_SCAN(CoDe) \
1109 while (s < strend) { \
1115 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1116 REXEC_FBC_UTF8_SCAN( \
1118 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1127 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1130 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1139 #define REXEC_FBC_TRYIT \
1140 if ((!reginfo || regtry(reginfo, &s))) \
1143 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1145 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1148 REXEC_FBC_CLASS_SCAN(CoNd); \
1152 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1155 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1158 REXEC_FBC_CLASS_SCAN(CoNd); \
1162 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1163 PL_reg_flags |= RF_tainted; \
1165 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1168 REXEC_FBC_CLASS_SCAN(CoNd); \
1172 #define DUMP_EXEC_POS(li,s,doutf8) \
1173 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1175 /* We know what class REx starts with. Try to find this position... */
1176 /* if reginfo is NULL, its a dryrun */
1177 /* annoyingly all the vars in this routine have different names from their counterparts
1178 in regmatch. /grrr */
1181 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1182 const char *strend, regmatch_info *reginfo)
1185 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1189 register STRLEN uskip;
1193 register I32 tmp = 1; /* Scratch variable? */
1194 register const bool do_utf8 = PL_reg_match_utf8;
1195 RXi_GET_DECL(prog,progi);
1197 PERL_ARGS_ASSERT_FIND_BYCLASS;
1199 /* We know what class it must start with. */
1203 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1204 !UTF8_IS_INVARIANT((U8)s[0]) ?
1205 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1206 REGINCLASS(prog, c, (U8*)s));
1209 while (s < strend) {
1212 if (REGINCLASS(prog, c, (U8*)s) ||
1213 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1214 /* The assignment of 2 is intentional:
1215 * for the folded sharp s, the skip is 2. */
1216 (skip = SHARP_S_SKIP))) {
1217 if (tmp && (!reginfo || regtry(reginfo, &s)))
1230 if (tmp && (!reginfo || regtry(reginfo, &s)))
1238 ln = STR_LEN(c); /* length to match in octets/bytes */
1239 lnc = (I32) ln; /* length to match in characters */
1241 STRLEN ulen1, ulen2;
1243 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1244 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1245 /* used by commented-out code below */
1246 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1248 /* XXX: Since the node will be case folded at compile
1249 time this logic is a little odd, although im not
1250 sure that its actually wrong. --dmq */
1252 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1253 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1255 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1256 codepoint of the first character in the converted
1257 form, yet originally we did the extra step.
1258 No tests fail by commenting this code out however
1259 so Ive left it out. -- dmq.
1261 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1263 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1268 while (sm < ((U8 *) m + ln)) {
1283 c2 = PL_fold_locale[c1];
1285 e = HOP3c(strend, -((I32)lnc), s);
1287 if (!reginfo && e < s)
1288 e = s; /* Due to minlen logic of intuit() */
1290 /* The idea in the EXACTF* cases is to first find the
1291 * first character of the EXACTF* node and then, if
1292 * necessary, case-insensitively compare the full
1293 * text of the node. The c1 and c2 are the first
1294 * characters (though in Unicode it gets a bit
1295 * more complicated because there are more cases
1296 * than just upper and lower: one needs to use
1297 * the so-called folding case for case-insensitive
1298 * matching (called "loose matching" in Unicode).
1299 * ibcmp_utf8() will do just that. */
1301 if (do_utf8 || UTF) {
1303 U8 tmpbuf [UTF8_MAXBYTES+1];
1306 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1308 /* Upper and lower of 1st char are equal -
1309 * probably not a "letter". */
1312 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1317 REXEC_FBC_EXACTISH_CHECK(c == c1);
1323 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1329 /* Handle some of the three Greek sigmas cases.
1330 * Note that not all the possible combinations
1331 * are handled here: some of them are handled
1332 * by the standard folding rules, and some of
1333 * them (the character class or ANYOF cases)
1334 * are handled during compiletime in
1335 * regexec.c:S_regclass(). */
1336 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1337 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1338 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1340 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1345 /* Neither pattern nor string are UTF8 */
1347 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1349 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1353 PL_reg_flags |= RF_tainted;
1360 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1361 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1363 tmp = ((OP(c) == BOUND ?
1364 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1365 LOAD_UTF8_CHARCLASS_ALNUM();
1366 REXEC_FBC_UTF8_SCAN(
1367 if (tmp == !(OP(c) == BOUND ?
1368 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1369 isALNUM_LC_utf8((U8*)s)))
1377 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1378 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1381 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1387 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1391 PL_reg_flags |= RF_tainted;
1398 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1399 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1401 tmp = ((OP(c) == NBOUND ?
1402 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1403 LOAD_UTF8_CHARCLASS_ALNUM();
1404 REXEC_FBC_UTF8_SCAN(
1405 if (tmp == !(OP(c) == NBOUND ?
1406 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1407 isALNUM_LC_utf8((U8*)s)))
1409 else REXEC_FBC_TRYIT;
1413 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1414 tmp = ((OP(c) == NBOUND ?
1415 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1418 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1420 else REXEC_FBC_TRYIT;
1423 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1427 REXEC_FBC_CSCAN_PRELOAD(
1428 LOAD_UTF8_CHARCLASS_ALNUM(),
1429 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1433 REXEC_FBC_CSCAN_TAINT(
1434 isALNUM_LC_utf8((U8*)s),
1438 REXEC_FBC_CSCAN_PRELOAD(
1439 LOAD_UTF8_CHARCLASS_ALNUM(),
1440 !swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1444 REXEC_FBC_CSCAN_TAINT(
1445 !isALNUM_LC_utf8((U8*)s),
1449 REXEC_FBC_CSCAN_PRELOAD(
1450 LOAD_UTF8_CHARCLASS_SPACE(),
1451 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8),
1455 REXEC_FBC_CSCAN_TAINT(
1456 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1460 REXEC_FBC_CSCAN_PRELOAD(
1461 LOAD_UTF8_CHARCLASS_SPACE(),
1462 !(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)),
1466 REXEC_FBC_CSCAN_TAINT(
1467 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1471 REXEC_FBC_CSCAN_PRELOAD(
1472 LOAD_UTF8_CHARCLASS_DIGIT(),
1473 swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1477 REXEC_FBC_CSCAN_TAINT(
1478 isDIGIT_LC_utf8((U8*)s),
1482 REXEC_FBC_CSCAN_PRELOAD(
1483 LOAD_UTF8_CHARCLASS_DIGIT(),
1484 !swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1488 REXEC_FBC_CSCAN_TAINT(
1489 !isDIGIT_LC_utf8((U8*)s),
1495 is_LNBREAK_latin1(s)
1505 !is_VERTWS_latin1(s)
1510 is_HORIZWS_latin1(s)
1514 !is_HORIZWS_utf8(s),
1515 !is_HORIZWS_latin1(s)
1521 /* what trie are we using right now */
1523 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1525 = (reg_trie_data*)progi->data->data[ aho->trie ];
1526 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1528 const char *last_start = strend - trie->minlen;
1530 const char *real_start = s;
1532 STRLEN maxlen = trie->maxlen;
1534 U8 **points; /* map of where we were in the input string
1535 when reading a given char. For ASCII this
1536 is unnecessary overhead as the relationship
1537 is always 1:1, but for Unicode, especially
1538 case folded Unicode this is not true. */
1539 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1543 GET_RE_DEBUG_FLAGS_DECL;
1545 /* We can't just allocate points here. We need to wrap it in
1546 * an SV so it gets freed properly if there is a croak while
1547 * running the match */
1550 sv_points=newSV(maxlen * sizeof(U8 *));
1551 SvCUR_set(sv_points,
1552 maxlen * sizeof(U8 *));
1553 SvPOK_on(sv_points);
1554 sv_2mortal(sv_points);
1555 points=(U8**)SvPV_nolen(sv_points );
1556 if ( trie_type != trie_utf8_fold
1557 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1560 bitmap=(U8*)trie->bitmap;
1562 bitmap=(U8*)ANYOF_BITMAP(c);
1564 /* this is the Aho-Corasick algorithm modified a touch
1565 to include special handling for long "unknown char"
1566 sequences. The basic idea being that we use AC as long
1567 as we are dealing with a possible matching char, when
1568 we encounter an unknown char (and we have not encountered
1569 an accepting state) we scan forward until we find a legal
1571 AC matching is basically that of trie matching, except
1572 that when we encounter a failing transition, we fall back
1573 to the current states "fail state", and try the current char
1574 again, a process we repeat until we reach the root state,
1575 state 1, or a legal transition. If we fail on the root state
1576 then we can either terminate if we have reached an accepting
1577 state previously, or restart the entire process from the beginning
1581 while (s <= last_start) {
1582 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1590 U8 *uscan = (U8*)NULL;
1591 U8 *leftmost = NULL;
1593 U32 accepted_word= 0;
1597 while ( state && uc <= (U8*)strend ) {
1599 U32 word = aho->states[ state ].wordnum;
1603 DEBUG_TRIE_EXECUTE_r(
1604 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1605 dump_exec_pos( (char *)uc, c, strend, real_start,
1606 (char *)uc, do_utf8 );
1607 PerlIO_printf( Perl_debug_log,
1608 " Scanning for legal start char...\n");
1611 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1616 if (uc >(U8*)last_start) break;
1620 U8 *lpos= points[ (pointpos - trie->wordlen[word-1] ) % maxlen ];
1621 if (!leftmost || lpos < leftmost) {
1622 DEBUG_r(accepted_word=word);
1628 points[pointpos++ % maxlen]= uc;
1629 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1630 uscan, len, uvc, charid, foldlen,
1632 DEBUG_TRIE_EXECUTE_r({
1633 dump_exec_pos( (char *)uc, c, strend, real_start,
1635 PerlIO_printf(Perl_debug_log,
1636 " Charid:%3u CP:%4"UVxf" ",
1642 word = aho->states[ state ].wordnum;
1644 base = aho->states[ state ].trans.base;
1646 DEBUG_TRIE_EXECUTE_r({
1648 dump_exec_pos( (char *)uc, c, strend, real_start,
1650 PerlIO_printf( Perl_debug_log,
1651 "%sState: %4"UVxf", word=%"UVxf,
1652 failed ? " Fail transition to " : "",
1653 (UV)state, (UV)word);
1658 (base + charid > trie->uniquecharcount )
1659 && (base + charid - 1 - trie->uniquecharcount
1661 && trie->trans[base + charid - 1 -
1662 trie->uniquecharcount].check == state
1663 && (tmp=trie->trans[base + charid - 1 -
1664 trie->uniquecharcount ].next))
1666 DEBUG_TRIE_EXECUTE_r(
1667 PerlIO_printf( Perl_debug_log," - legal\n"));
1672 DEBUG_TRIE_EXECUTE_r(
1673 PerlIO_printf( Perl_debug_log," - fail\n"));
1675 state = aho->fail[state];
1679 /* we must be accepting here */
1680 DEBUG_TRIE_EXECUTE_r(
1681 PerlIO_printf( Perl_debug_log," - accepting\n"));
1690 if (!state) state = 1;
1693 if ( aho->states[ state ].wordnum ) {
1694 U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ];
1695 if (!leftmost || lpos < leftmost) {
1696 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1701 s = (char*)leftmost;
1702 DEBUG_TRIE_EXECUTE_r({
1704 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1705 (UV)accepted_word, (IV)(s - real_start)
1708 if (!reginfo || regtry(reginfo, &s)) {
1714 DEBUG_TRIE_EXECUTE_r({
1715 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1718 DEBUG_TRIE_EXECUTE_r(
1719 PerlIO_printf( Perl_debug_log,"No match.\n"));
1728 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1737 S_swap_match_buff (pTHX_ regexp *prog)
1739 regexp_paren_pair *t;
1741 PERL_ARGS_ASSERT_SWAP_MATCH_BUFF;
1744 /* We have to be careful. If the previous successful match
1745 was from this regex we don't want a subsequent paritally
1746 successful match to clobber the old results.
1747 So when we detect this possibility we add a swap buffer
1748 to the re, and switch the buffer each match. If we fail
1749 we switch it back, otherwise we leave it swapped.
1751 Newxz(prog->swap, (prog->nparens + 1), regexp_paren_pair);
1754 prog->swap = prog->offs;
1760 - regexec_flags - match a regexp against a string
1763 Perl_regexec_flags(pTHX_ REGEXP * const prog, char *stringarg, register char *strend,
1764 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1765 /* strend: pointer to null at end of string */
1766 /* strbeg: real beginning of string */
1767 /* minend: end of match must be >=minend after stringarg. */
1768 /* data: May be used for some additional optimizations.
1769 Currently its only used, with a U32 cast, for transmitting
1770 the ganch offset when doing a /g match. This will change */
1771 /* nosave: For optimizations. */
1774 /*register*/ char *s;
1775 register regnode *c;
1776 /*register*/ char *startpos = stringarg;
1777 I32 minlen; /* must match at least this many chars */
1778 I32 dontbother = 0; /* how many characters not to try at end */
1779 I32 end_shift = 0; /* Same for the end. */ /* CC */
1780 I32 scream_pos = -1; /* Internal iterator of scream. */
1781 char *scream_olds = NULL;
1782 const bool do_utf8 = (bool)DO_UTF8(sv);
1784 RXi_GET_DECL(prog,progi);
1785 regmatch_info reginfo; /* create some info to pass to regtry etc */
1786 bool swap_on_fail = 0;
1787 GET_RE_DEBUG_FLAGS_DECL;
1789 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1790 PERL_UNUSED_ARG(data);
1792 /* Be paranoid... */
1793 if (prog == NULL || startpos == NULL) {
1794 Perl_croak(aTHX_ "NULL regexp parameter");
1798 multiline = prog->extflags & RXf_PMf_MULTILINE;
1799 reginfo.prog = prog;
1801 RX_MATCH_UTF8_set(prog, do_utf8);
1803 debug_start_match(prog, do_utf8, startpos, strend,
1807 minlen = prog->minlen;
1809 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1810 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1811 "String too short [regexec_flags]...\n"));
1816 /* Check validity of program. */
1817 if (UCHARAT(progi->program) != REG_MAGIC) {
1818 Perl_croak(aTHX_ "corrupted regexp program");
1822 PL_reg_eval_set = 0;
1826 PL_reg_flags |= RF_utf8;
1828 /* Mark beginning of line for ^ and lookbehind. */
1829 reginfo.bol = startpos; /* XXX not used ??? */
1833 /* Mark end of line for $ (and such) */
1836 /* see how far we have to get to not match where we matched before */
1837 reginfo.till = startpos+minend;
1839 /* If there is a "must appear" string, look for it. */
1842 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1845 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1846 reginfo.ganch = startpos + prog->gofs;
1847 else if (sv && SvTYPE(sv) >= SVt_PVMG
1849 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1850 && mg->mg_len >= 0) {
1851 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1852 if (prog->extflags & RXf_ANCH_GPOS) {
1853 if (s > reginfo.ganch)
1855 s = reginfo.ganch - prog->gofs;
1859 reginfo.ganch = strbeg + PTR2UV(data);
1860 } else /* pos() not defined */
1861 reginfo.ganch = strbeg;
1863 if (PL_curpm && (PM_GETRE(PL_curpm) == prog)) {
1865 swap_match_buff(prog); /* do we need a save destructor here for
1868 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1869 re_scream_pos_data d;
1871 d.scream_olds = &scream_olds;
1872 d.scream_pos = &scream_pos;
1873 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1875 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1876 goto phooey; /* not present */
1882 /* Simplest case: anchored match need be tried only once. */
1883 /* [unless only anchor is BOL and multiline is set] */
1884 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
1885 if (s == startpos && regtry(®info, &startpos))
1887 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
1888 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
1893 dontbother = minlen - 1;
1894 end = HOP3c(strend, -dontbother, strbeg) - 1;
1895 /* for multiline we only have to try after newlines */
1896 if (prog->check_substr || prog->check_utf8) {
1900 if (regtry(®info, &s))
1905 if (prog->extflags & RXf_USE_INTUIT) {
1906 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1917 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1918 if (regtry(®info, &s))
1925 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
1927 /* the warning about reginfo.ganch being used without intialization
1928 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
1929 and we only enter this block when the same bit is set. */
1930 char *tmp_s = reginfo.ganch - prog->gofs;
1931 if (regtry(®info, &tmp_s))
1936 /* Messy cases: unanchored match. */
1937 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
1938 /* we have /x+whatever/ */
1939 /* it must be a one character string (XXXX Except UTF?) */
1944 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1945 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1946 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1951 DEBUG_EXECUTE_r( did_match = 1 );
1952 if (regtry(®info, &s)) goto got_it;
1954 while (s < strend && *s == ch)
1962 DEBUG_EXECUTE_r( did_match = 1 );
1963 if (regtry(®info, &s)) goto got_it;
1965 while (s < strend && *s == ch)
1970 DEBUG_EXECUTE_r(if (!did_match)
1971 PerlIO_printf(Perl_debug_log,
1972 "Did not find anchored character...\n")
1975 else if (prog->anchored_substr != NULL
1976 || prog->anchored_utf8 != NULL
1977 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
1978 && prog->float_max_offset < strend - s)) {
1983 char *last1; /* Last position checked before */
1987 if (prog->anchored_substr || prog->anchored_utf8) {
1988 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1989 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1990 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1991 back_max = back_min = prog->anchored_offset;
1993 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1994 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1995 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1996 back_max = prog->float_max_offset;
1997 back_min = prog->float_min_offset;
2001 if (must == &PL_sv_undef)
2002 /* could not downgrade utf8 check substring, so must fail */
2008 last = HOP3c(strend, /* Cannot start after this */
2009 -(I32)(CHR_SVLEN(must)
2010 - (SvTAIL(must) != 0) + back_min), strbeg);
2013 last1 = HOPc(s, -1);
2015 last1 = s - 1; /* bogus */
2017 /* XXXX check_substr already used to find "s", can optimize if
2018 check_substr==must. */
2020 dontbother = end_shift;
2021 strend = HOPc(strend, -dontbother);
2022 while ( (s <= last) &&
2023 ((flags & REXEC_SCREAM)
2024 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2025 end_shift, &scream_pos, 0))
2026 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2027 (unsigned char*)strend, must,
2028 multiline ? FBMrf_MULTILINE : 0))) ) {
2029 /* we may be pointing at the wrong string */
2030 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2031 s = strbeg + (s - SvPVX_const(sv));
2032 DEBUG_EXECUTE_r( did_match = 1 );
2033 if (HOPc(s, -back_max) > last1) {
2034 last1 = HOPc(s, -back_min);
2035 s = HOPc(s, -back_max);
2038 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2040 last1 = HOPc(s, -back_min);
2044 while (s <= last1) {
2045 if (regtry(®info, &s))
2051 while (s <= last1) {
2052 if (regtry(®info, &s))
2058 DEBUG_EXECUTE_r(if (!did_match) {
2059 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
2060 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2061 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2062 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2063 ? "anchored" : "floating"),
2064 quoted, RE_SV_TAIL(must));
2068 else if ( (c = progi->regstclass) ) {
2070 const OPCODE op = OP(progi->regstclass);
2071 /* don't bother with what can't match */
2072 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2073 strend = HOPc(strend, -(minlen - 1));
2076 SV * const prop = sv_newmortal();
2077 regprop(prog, prop, c);
2079 RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1),
2081 PerlIO_printf(Perl_debug_log,
2082 "Matching stclass %.*s against %s (%d chars)\n",
2083 (int)SvCUR(prop), SvPVX_const(prop),
2084 quoted, (int)(strend - s));
2087 if (find_byclass(prog, c, s, strend, ®info))
2089 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2093 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2098 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2099 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2100 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
2102 if (flags & REXEC_SCREAM) {
2103 last = screaminstr(sv, float_real, s - strbeg,
2104 end_shift, &scream_pos, 1); /* last one */
2106 last = scream_olds; /* Only one occurrence. */
2107 /* we may be pointing at the wrong string */
2108 else if (RXp_MATCH_COPIED(prog))
2109 s = strbeg + (s - SvPVX_const(sv));
2113 const char * const little = SvPV_const(float_real, len);
2115 if (SvTAIL(float_real)) {
2116 if (memEQ(strend - len + 1, little, len - 1))
2117 last = strend - len + 1;
2118 else if (!multiline)
2119 last = memEQ(strend - len, little, len)
2120 ? strend - len : NULL;
2126 last = rninstr(s, strend, little, little + len);
2128 last = strend; /* matching "$" */
2133 PerlIO_printf(Perl_debug_log,
2134 "%sCan't trim the tail, match fails (should not happen)%s\n",
2135 PL_colors[4], PL_colors[5]));
2136 goto phooey; /* Should not happen! */
2138 dontbother = strend - last + prog->float_min_offset;
2140 if (minlen && (dontbother < minlen))
2141 dontbother = minlen - 1;
2142 strend -= dontbother; /* this one's always in bytes! */
2143 /* We don't know much -- general case. */
2146 if (regtry(®info, &s))
2155 if (regtry(®info, &s))
2157 } while (s++ < strend);
2165 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2167 if (PL_reg_eval_set)
2168 restore_pos(aTHX_ prog);
2169 if (RXp_PAREN_NAMES(prog))
2170 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2172 /* make sure $`, $&, $', and $digit will work later */
2173 if ( !(flags & REXEC_NOT_FIRST) ) {
2174 RX_MATCH_COPY_FREE(prog);
2175 if (flags & REXEC_COPY_STR) {
2176 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2177 #ifdef PERL_OLD_COPY_ON_WRITE
2179 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2181 PerlIO_printf(Perl_debug_log,
2182 "Copy on write: regexp capture, type %d\n",
2185 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2186 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2187 assert (SvPOKp(prog->saved_copy));
2191 RX_MATCH_COPIED_on(prog);
2192 s = savepvn(strbeg, i);
2198 prog->subbeg = strbeg;
2199 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2206 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2207 PL_colors[4], PL_colors[5]));
2208 if (PL_reg_eval_set)
2209 restore_pos(aTHX_ prog);
2211 /* we failed :-( roll it back */
2212 swap_match_buff(prog);
2219 - regtry - try match at specific point
2221 STATIC I32 /* 0 failure, 1 success */
2222 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2226 regexp *prog = reginfo->prog;
2227 RXi_GET_DECL(prog,progi);
2228 GET_RE_DEBUG_FLAGS_DECL;
2230 PERL_ARGS_ASSERT_REGTRY;
2232 reginfo->cutpoint=NULL;
2234 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2237 PL_reg_eval_set = RS_init;
2238 DEBUG_EXECUTE_r(DEBUG_s(
2239 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2240 (IV)(PL_stack_sp - PL_stack_base));
2243 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2244 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2246 /* Apparently this is not needed, judging by wantarray. */
2247 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2248 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2251 /* Make $_ available to executed code. */
2252 if (reginfo->sv != DEFSV) {
2254 DEFSV_set(reginfo->sv);
2257 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2258 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2259 /* prepare for quick setting of pos */
2260 #ifdef PERL_OLD_COPY_ON_WRITE
2261 if (SvIsCOW(reginfo->sv))
2262 sv_force_normal_flags(reginfo->sv, 0);
2264 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2265 &PL_vtbl_mglob, NULL, 0);
2269 PL_reg_oldpos = mg->mg_len;
2270 SAVEDESTRUCTOR_X(restore_pos, prog);
2272 if (!PL_reg_curpm) {
2273 Newxz(PL_reg_curpm, 1, PMOP);
2276 SV* const repointer = newSViv(0);
2277 /* this regexp is also owned by the new PL_reg_curpm, which
2278 will try to free it. */
2279 av_push(PL_regex_padav,repointer);
2280 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2281 PL_regex_pad = AvARRAY(PL_regex_padav);
2286 /* It seems that non-ithreads works both with and without this code.
2287 So for efficiency reasons it seems best not to have the code
2288 compiled when it is not needed. */
2289 /* This is safe against NULLs: */
2290 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2291 /* PM_reg_curpm owns a reference to this regexp. */
2294 PM_SETRE(PL_reg_curpm, prog);
2295 PL_reg_oldcurpm = PL_curpm;
2296 PL_curpm = PL_reg_curpm;
2297 if (RXp_MATCH_COPIED(prog)) {
2298 /* Here is a serious problem: we cannot rewrite subbeg,
2299 since it may be needed if this match fails. Thus
2300 $` inside (?{}) could fail... */
2301 PL_reg_oldsaved = prog->subbeg;
2302 PL_reg_oldsavedlen = prog->sublen;
2303 #ifdef PERL_OLD_COPY_ON_WRITE
2304 PL_nrs = prog->saved_copy;
2306 RXp_MATCH_COPIED_off(prog);
2309 PL_reg_oldsaved = NULL;
2310 prog->subbeg = PL_bostr;
2311 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2313 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2314 prog->offs[0].start = *startpos - PL_bostr;
2315 PL_reginput = *startpos;
2316 PL_reglastparen = &prog->lastparen;
2317 PL_reglastcloseparen = &prog->lastcloseparen;
2318 prog->lastparen = 0;
2319 prog->lastcloseparen = 0;
2321 PL_regoffs = prog->offs;
2322 if (PL_reg_start_tmpl <= prog->nparens) {
2323 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2324 if(PL_reg_start_tmp)
2325 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2327 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2330 /* XXXX What this code is doing here?!!! There should be no need
2331 to do this again and again, PL_reglastparen should take care of
2334 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2335 * Actually, the code in regcppop() (which Ilya may be meaning by
2336 * PL_reglastparen), is not needed at all by the test suite
2337 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2338 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2339 * Meanwhile, this code *is* needed for the
2340 * above-mentioned test suite tests to succeed. The common theme
2341 * on those tests seems to be returning null fields from matches.
2342 * --jhi updated by dapm */
2344 if (prog->nparens) {
2345 regexp_paren_pair *pp = PL_regoffs;
2347 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2355 if (regmatch(reginfo, progi->program + 1)) {
2356 PL_regoffs[0].end = PL_reginput - PL_bostr;
2359 if (reginfo->cutpoint)
2360 *startpos= reginfo->cutpoint;
2361 REGCP_UNWIND(lastcp);
2366 #define sayYES goto yes
2367 #define sayNO goto no
2368 #define sayNO_SILENT goto no_silent
2370 /* we dont use STMT_START/END here because it leads to
2371 "unreachable code" warnings, which are bogus, but distracting. */
2372 #define CACHEsayNO \
2373 if (ST.cache_mask) \
2374 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2377 /* this is used to determine how far from the left messages like
2378 'failed...' are printed. It should be set such that messages
2379 are inline with the regop output that created them.
2381 #define REPORT_CODE_OFF 32
2384 /* Make sure there is a test for this +1 options in re_tests */
2385 #define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2387 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2388 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2390 #define SLAB_FIRST(s) (&(s)->states[0])
2391 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2393 /* grab a new slab and return the first slot in it */
2395 STATIC regmatch_state *
2398 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2401 regmatch_slab *s = PL_regmatch_slab->next;
2403 Newx(s, 1, regmatch_slab);
2404 s->prev = PL_regmatch_slab;
2406 PL_regmatch_slab->next = s;
2408 PL_regmatch_slab = s;
2409 return SLAB_FIRST(s);
2413 /* push a new state then goto it */
2415 #define PUSH_STATE_GOTO(state, node) \
2417 st->resume_state = state; \
2420 /* push a new state with success backtracking, then goto it */
2422 #define PUSH_YES_STATE_GOTO(state, node) \
2424 st->resume_state = state; \
2425 goto push_yes_state;
2431 regmatch() - main matching routine
2433 This is basically one big switch statement in a loop. We execute an op,
2434 set 'next' to point the next op, and continue. If we come to a point which
2435 we may need to backtrack to on failure such as (A|B|C), we push a
2436 backtrack state onto the backtrack stack. On failure, we pop the top
2437 state, and re-enter the loop at the state indicated. If there are no more
2438 states to pop, we return failure.
2440 Sometimes we also need to backtrack on success; for example /A+/, where
2441 after successfully matching one A, we need to go back and try to
2442 match another one; similarly for lookahead assertions: if the assertion
2443 completes successfully, we backtrack to the state just before the assertion
2444 and then carry on. In these cases, the pushed state is marked as
2445 'backtrack on success too'. This marking is in fact done by a chain of
2446 pointers, each pointing to the previous 'yes' state. On success, we pop to
2447 the nearest yes state, discarding any intermediate failure-only states.
2448 Sometimes a yes state is pushed just to force some cleanup code to be
2449 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2450 it to free the inner regex.
2452 Note that failure backtracking rewinds the cursor position, while
2453 success backtracking leaves it alone.
2455 A pattern is complete when the END op is executed, while a subpattern
2456 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2457 ops trigger the "pop to last yes state if any, otherwise return true"
2460 A common convention in this function is to use A and B to refer to the two
2461 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2462 the subpattern to be matched possibly multiple times, while B is the entire
2463 rest of the pattern. Variable and state names reflect this convention.
2465 The states in the main switch are the union of ops and failure/success of
2466 substates associated with with that op. For example, IFMATCH is the op
2467 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2468 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2469 successfully matched A and IFMATCH_A_fail is a state saying that we have
2470 just failed to match A. Resume states always come in pairs. The backtrack
2471 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2472 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2473 on success or failure.
2475 The struct that holds a backtracking state is actually a big union, with
2476 one variant for each major type of op. The variable st points to the
2477 top-most backtrack struct. To make the code clearer, within each
2478 block of code we #define ST to alias the relevant union.
2480 Here's a concrete example of a (vastly oversimplified) IFMATCH
2486 #define ST st->u.ifmatch
2488 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2489 ST.foo = ...; // some state we wish to save
2491 // push a yes backtrack state with a resume value of
2492 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2494 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2497 case IFMATCH_A: // we have successfully executed A; now continue with B
2499 bar = ST.foo; // do something with the preserved value
2502 case IFMATCH_A_fail: // A failed, so the assertion failed
2503 ...; // do some housekeeping, then ...
2504 sayNO; // propagate the failure
2511 For any old-timers reading this who are familiar with the old recursive
2512 approach, the code above is equivalent to:
2514 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2523 ...; // do some housekeeping, then ...
2524 sayNO; // propagate the failure
2527 The topmost backtrack state, pointed to by st, is usually free. If you
2528 want to claim it, populate any ST.foo fields in it with values you wish to
2529 save, then do one of
2531 PUSH_STATE_GOTO(resume_state, node);
2532 PUSH_YES_STATE_GOTO(resume_state, node);
2534 which sets that backtrack state's resume value to 'resume_state', pushes a
2535 new free entry to the top of the backtrack stack, then goes to 'node'.
2536 On backtracking, the free slot is popped, and the saved state becomes the
2537 new free state. An ST.foo field in this new top state can be temporarily
2538 accessed to retrieve values, but once the main loop is re-entered, it
2539 becomes available for reuse.
2541 Note that the depth of the backtrack stack constantly increases during the
2542 left-to-right execution of the pattern, rather than going up and down with
2543 the pattern nesting. For example the stack is at its maximum at Z at the
2544 end of the pattern, rather than at X in the following:
2546 /(((X)+)+)+....(Y)+....Z/
2548 The only exceptions to this are lookahead/behind assertions and the cut,
2549 (?>A), which pop all the backtrack states associated with A before
2552 Bascktrack state structs are allocated in slabs of about 4K in size.
2553 PL_regmatch_state and st always point to the currently active state,
2554 and PL_regmatch_slab points to the slab currently containing
2555 PL_regmatch_state. The first time regmatch() is called, the first slab is
2556 allocated, and is never freed until interpreter destruction. When the slab
2557 is full, a new one is allocated and chained to the end. At exit from
2558 regmatch(), slabs allocated since entry are freed.
2563 #define DEBUG_STATE_pp(pp) \
2565 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2566 PerlIO_printf(Perl_debug_log, \
2567 " %*s"pp" %s%s%s%s%s\n", \
2569 PL_reg_name[st->resume_state], \
2570 ((st==yes_state||st==mark_state) ? "[" : ""), \
2571 ((st==yes_state) ? "Y" : ""), \
2572 ((st==mark_state) ? "M" : ""), \
2573 ((st==yes_state||st==mark_state) ? "]" : "") \
2578 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2583 S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8,
2584 const char *start, const char *end, const char *blurb)
2586 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2588 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2593 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2594 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2596 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2597 start, end - start, 60);
2599 PerlIO_printf(Perl_debug_log,
2600 "%s%s REx%s %s against %s\n",
2601 PL_colors[4], blurb, PL_colors[5], s0, s1);
2603 if (do_utf8||utf8_pat)
2604 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2605 utf8_pat ? "pattern" : "",
2606 utf8_pat && do_utf8 ? " and " : "",
2607 do_utf8 ? "string" : ""
2613 S_dump_exec_pos(pTHX_ const char *locinput,
2614 const regnode *scan,
2615 const char *loc_regeol,
2616 const char *loc_bostr,
2617 const char *loc_reg_starttry,
2620 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2621 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2622 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2623 /* The part of the string before starttry has one color
2624 (pref0_len chars), between starttry and current
2625 position another one (pref_len - pref0_len chars),
2626 after the current position the third one.
2627 We assume that pref0_len <= pref_len, otherwise we
2628 decrease pref0_len. */
2629 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2630 ? (5 + taill) - l : locinput - loc_bostr;
2633 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2635 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2637 pref0_len = pref_len - (locinput - loc_reg_starttry);
2638 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2639 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2640 ? (5 + taill) - pref_len : loc_regeol - locinput);
2641 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2645 if (pref0_len > pref_len)
2646 pref0_len = pref_len;
2648 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2650 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2651 (locinput - pref_len),pref0_len, 60, 4, 5);
2653 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2654 (locinput - pref_len + pref0_len),
2655 pref_len - pref0_len, 60, 2, 3);
2657 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2658 locinput, loc_regeol - locinput, 10, 0, 1);
2660 const STRLEN tlen=len0+len1+len2;
2661 PerlIO_printf(Perl_debug_log,
2662 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2663 (IV)(locinput - loc_bostr),
2666 (docolor ? "" : "> <"),
2668 (int)(tlen > 19 ? 0 : 19 - tlen),
2675 /* reg_check_named_buff_matched()
2676 * Checks to see if a named buffer has matched. The data array of
2677 * buffer numbers corresponding to the buffer is expected to reside
2678 * in the regexp->data->data array in the slot stored in the ARG() of
2679 * node involved. Note that this routine doesn't actually care about the
2680 * name, that information is not preserved from compilation to execution.
2681 * Returns the index of the leftmost defined buffer with the given name
2682 * or 0 if non of the buffers matched.
2685 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2688 RXi_GET_DECL(rex,rexi);
2689 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2690 I32 *nums=(I32*)SvPVX(sv_dat);
2692 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2694 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2695 if ((I32)*PL_reglastparen >= nums[n] &&
2696 PL_regoffs[nums[n]].end != -1)
2705 /* free all slabs above current one - called during LEAVE_SCOPE */
2708 S_clear_backtrack_stack(pTHX_ void *p)
2710 regmatch_slab *s = PL_regmatch_slab->next;
2715 PL_regmatch_slab->next = NULL;
2717 regmatch_slab * const osl = s;
2724 #define SETREX(Re1,Re2) \
2725 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2728 STATIC I32 /* 0 failure, 1 success */
2729 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2731 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2735 register const bool do_utf8 = PL_reg_match_utf8;
2736 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2737 regexp *rex = reginfo->prog;
2738 RXi_GET_DECL(rex,rexi);
2740 /* the current state. This is a cached copy of PL_regmatch_state */
2741 register regmatch_state *st;
2742 /* cache heavy used fields of st in registers */
2743 register regnode *scan;
2744 register regnode *next;
2745 register U32 n = 0; /* general value; init to avoid compiler warning */
2746 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2747 register char *locinput = PL_reginput;
2748 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2750 bool result = 0; /* return value of S_regmatch */
2751 int depth = 0; /* depth of backtrack stack */
2752 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2753 const U32 max_nochange_depth =
2754 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2755 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2756 regmatch_state *yes_state = NULL; /* state to pop to on success of
2758 /* mark_state piggy backs on the yes_state logic so that when we unwind
2759 the stack on success we can update the mark_state as we go */
2760 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2761 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2762 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2764 bool no_final = 0; /* prevent failure from backtracking? */
2765 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2766 char *startpoint = PL_reginput;
2767 SV *popmark = NULL; /* are we looking for a mark? */
2768 SV *sv_commit = NULL; /* last mark name seen in failure */
2769 SV *sv_yes_mark = NULL; /* last mark name we have seen
2770 during a successfull match */
2771 U32 lastopen = 0; /* last open we saw */
2772 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2773 SV* const oreplsv = GvSV(PL_replgv);
2774 /* these three flags are set by various ops to signal information to
2775 * the very next op. They have a useful lifetime of exactly one loop
2776 * iteration, and are not preserved or restored by state pushes/pops
2778 bool sw = 0; /* the condition value in (?(cond)a|b) */
2779 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2780 int logical = 0; /* the following EVAL is:
2784 or the following IFMATCH/UNLESSM is:
2785 false: plain (?=foo)
2786 true: used as a condition: (?(?=foo))
2789 GET_RE_DEBUG_FLAGS_DECL;
2792 PERL_ARGS_ASSERT_REGMATCH;
2794 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2795 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2797 /* on first ever call to regmatch, allocate first slab */
2798 if (!PL_regmatch_slab) {
2799 Newx(PL_regmatch_slab, 1, regmatch_slab);
2800 PL_regmatch_slab->prev = NULL;
2801 PL_regmatch_slab->next = NULL;
2802 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2805 oldsave = PL_savestack_ix;
2806 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2807 SAVEVPTR(PL_regmatch_slab);
2808 SAVEVPTR(PL_regmatch_state);
2810 /* grab next free state slot */
2811 st = ++PL_regmatch_state;
2812 if (st > SLAB_LAST(PL_regmatch_slab))
2813 st = PL_regmatch_state = S_push_slab(aTHX);
2815 /* Note that nextchr is a byte even in UTF */
2816 nextchr = UCHARAT(locinput);
2818 while (scan != NULL) {
2821 SV * const prop = sv_newmortal();
2822 regnode *rnext=regnext(scan);
2823 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2824 regprop(rex, prop, scan);
2826 PerlIO_printf(Perl_debug_log,
2827 "%3"IVdf":%*s%s(%"IVdf")\n",
2828 (IV)(scan - rexi->program), depth*2, "",
2830 (PL_regkind[OP(scan)] == END || !rnext) ?
2831 0 : (IV)(rnext - rexi->program));
2834 next = scan + NEXT_OFF(scan);
2837 state_num = OP(scan);
2841 assert(PL_reglastparen == &rex->lastparen);
2842 assert(PL_reglastcloseparen == &rex->lastcloseparen);
2843 assert(PL_regoffs == rex->offs);
2845 switch (state_num) {
2847 if (locinput == PL_bostr)
2849 /* reginfo->till = reginfo->bol; */
2854 if (locinput == PL_bostr ||
2855 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2861 if (locinput == PL_bostr)
2865 if (locinput == reginfo->ganch)
2870 /* update the startpoint */
2871 st->u.keeper.val = PL_regoffs[0].start;
2872 PL_reginput = locinput;
2873 PL_regoffs[0].start = locinput - PL_bostr;
2874 PUSH_STATE_GOTO(KEEPS_next, next);
2876 case KEEPS_next_fail:
2877 /* rollback the start point change */
2878 PL_regoffs[0].start = st->u.keeper.val;
2884 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2889 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2891 if (PL_regeol - locinput > 1)
2895 if (PL_regeol != locinput)
2899 if (!nextchr && locinput >= PL_regeol)
2902 locinput += PL_utf8skip[nextchr];
2903 if (locinput > PL_regeol)
2905 nextchr = UCHARAT(locinput);
2908 nextchr = UCHARAT(++locinput);
2911 if (!nextchr && locinput >= PL_regeol)
2913 nextchr = UCHARAT(++locinput);
2916 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2919 locinput += PL_utf8skip[nextchr];
2920 if (locinput > PL_regeol)
2922 nextchr = UCHARAT(locinput);
2925 nextchr = UCHARAT(++locinput);
2929 #define ST st->u.trie
2931 /* In this case the charclass data is available inline so
2932 we can fail fast without a lot of extra overhead.
2934 if (scan->flags == EXACT || !do_utf8) {
2935 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
2937 PerlIO_printf(Perl_debug_log,
2938 "%*s %sfailed to match trie start class...%s\n",
2939 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2948 /* what type of TRIE am I? (utf8 makes this contextual) */
2949 DECL_TRIE_TYPE(scan);
2951 /* what trie are we using right now */
2952 reg_trie_data * const trie
2953 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
2954 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
2955 U32 state = trie->startstate;
2957 if (trie->bitmap && trie_type != trie_utf8_fold &&
2958 !TRIE_BITMAP_TEST(trie,*locinput)
2960 if (trie->states[ state ].wordnum) {
2962 PerlIO_printf(Perl_debug_log,
2963 "%*s %smatched empty string...%s\n",
2964 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2969 PerlIO_printf(Perl_debug_log,
2970 "%*s %sfailed to match trie start class...%s\n",
2971 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
2978 U8 *uc = ( U8* )locinput;
2982 U8 *uscan = (U8*)NULL;
2984 SV *sv_accept_buff = NULL;
2985 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2987 ST.accepted = 0; /* how many accepting states we have seen */
2989 ST.jump = trie->jump;
2992 traverse the TRIE keeping track of all accepting states
2993 we transition through until we get to a failing node.
2996 while ( state && uc <= (U8*)PL_regeol ) {
2997 U32 base = trie->states[ state ].trans.base;
3000 /* We use charid to hold the wordnum as we don't use it
3001 for charid until after we have done the wordnum logic.
3002 We define an alias just so that the wordnum logic reads
3005 #define got_wordnum charid
3006 got_wordnum = trie->states[ state ].wordnum;
3008 if ( got_wordnum ) {
3009 if ( ! ST.accepted ) {
3011 SAVETMPS; /* XXX is this necessary? dmq */
3012 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
3013 sv_accept_buff=newSV(bufflen *
3014 sizeof(reg_trie_accepted) - 1);
3015 SvCUR_set(sv_accept_buff, 0);
3016 SvPOK_on(sv_accept_buff);
3017 sv_2mortal(sv_accept_buff);
3020 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
3023 if (ST.accepted >= bufflen) {
3025 ST.accept_buff =(reg_trie_accepted*)
3026 SvGROW(sv_accept_buff,
3027 bufflen * sizeof(reg_trie_accepted));
3029 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
3030 + sizeof(reg_trie_accepted));
3033 ST.accept_buff[ST.accepted].wordnum = got_wordnum;
3034 ST.accept_buff[ST.accepted].endpos = uc;
3036 } while (trie->nextword && (got_wordnum= trie->nextword[got_wordnum]));
3040 DEBUG_TRIE_EXECUTE_r({
3041 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
3042 PerlIO_printf( Perl_debug_log,
3043 "%*s %sState: %4"UVxf" Accepted: %4"UVxf" ",
3044 2+depth * 2, "", PL_colors[4],
3045 (UV)state, (UV)ST.accepted );
3049 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3050 uscan, len, uvc, charid, foldlen,
3054 (base + charid > trie->uniquecharcount )
3055 && (base + charid - 1 - trie->uniquecharcount
3057 && trie->trans[base + charid - 1 -
3058 trie->uniquecharcount].check == state)
3060 state = trie->trans[base + charid - 1 -
3061 trie->uniquecharcount ].next;
3072 DEBUG_TRIE_EXECUTE_r(
3073 PerlIO_printf( Perl_debug_log,
3074 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3075 charid, uvc, (UV)state, PL_colors[5] );
3082 PerlIO_printf( Perl_debug_log,
3083 "%*s %sgot %"IVdf" possible matches%s\n",
3084 REPORT_CODE_OFF + depth * 2, "",
3085 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3088 goto trie_first_try; /* jump into the fail handler */
3090 case TRIE_next_fail: /* we failed - try next alterative */
3092 REGCP_UNWIND(ST.cp);
3093 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3094 PL_regoffs[n].end = -1;
3095 *PL_reglastparen = n;
3104 ST.lastparen = *PL_reglastparen;
3107 if ( ST.accepted == 1 ) {
3108 /* only one choice left - just continue */
3110 AV *const trie_words
3111 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3112 SV ** const tmp = av_fetch( trie_words,
3113 ST.accept_buff[ 0 ].wordnum-1, 0 );
3114 SV *sv= tmp ? sv_newmortal() : NULL;
3116 PerlIO_printf( Perl_debug_log,
3117 "%*s %sonly one match left: #%d <%s>%s\n",
3118 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3119 ST.accept_buff[ 0 ].wordnum,
3120 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3121 PL_colors[0], PL_colors[1],
3122 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3124 : "not compiled under -Dr",
3127 PL_reginput = (char *)ST.accept_buff[ 0 ].endpos;
3128 /* in this case we free tmps/leave before we call regmatch
3129 as we wont be using accept_buff again. */
3131 locinput = PL_reginput;
3132 nextchr = UCHARAT(locinput);
3133 if ( !ST.jump || !ST.jump[ST.accept_buff[0].wordnum])
3136 scan = ST.me + ST.jump[ST.accept_buff[0].wordnum];
3137 if (!has_cutgroup) {
3142 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3145 continue; /* execute rest of RE */
3148 if ( !ST.accepted-- ) {
3150 PerlIO_printf( Perl_debug_log,
3151 "%*s %sTRIE failed...%s\n",
3152 REPORT_CODE_OFF+depth*2, "",
3163 There are at least two accepting states left. Presumably
3164 the number of accepting states is going to be low,
3165 typically two. So we simply scan through to find the one
3166 with lowest wordnum. Once we find it, we swap the last
3167 state into its place and decrement the size. We then try to
3168 match the rest of the pattern at the point where the word
3169 ends. If we succeed, control just continues along the
3170 regex; if we fail we return here to try the next accepting
3177 for( cur = 1 ; cur <= ST.accepted ; cur++ ) {
3178 DEBUG_TRIE_EXECUTE_r(
3179 PerlIO_printf( Perl_debug_log,
3180 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
3181 REPORT_CODE_OFF + depth * 2, "", PL_colors[4],
3182 (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur,
3183 ST.accept_buff[ cur ].wordnum, PL_colors[5] );
3186 if (ST.accept_buff[cur].wordnum <
3187 ST.accept_buff[best].wordnum)
3192 AV *const trie_words
3193 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3194 SV ** const tmp = av_fetch( trie_words,
3195 ST.accept_buff[ best ].wordnum - 1, 0 );
3196 regnode *nextop=(!ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) ?
3198 ST.me + ST.jump[ST.accept_buff[best].wordnum];
3199 SV *sv= tmp ? sv_newmortal() : NULL;
3201 PerlIO_printf( Perl_debug_log,
3202 "%*s %strying alternation #%d <%s> at node #%d %s\n",
3203 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3204 ST.accept_buff[best].wordnum,
3205 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3206 PL_colors[0], PL_colors[1],
3207 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3208 ) : "not compiled under -Dr",
3209 REG_NODE_NUM(nextop),
3213 if ( best<ST.accepted ) {
3214 reg_trie_accepted tmp = ST.accept_buff[ best ];
3215 ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ];
3216 ST.accept_buff[ ST.accepted ] = tmp;
3219 PL_reginput = (char *)ST.accept_buff[ best ].endpos;
3220 if ( !ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) {
3223 scan = ST.me + ST.jump[ST.accept_buff[best].wordnum];
3225 PUSH_YES_STATE_GOTO(TRIE_next, scan);
3230 /* we dont want to throw this away, see bug 57042*/
3231 if (oreplsv != GvSV(PL_replgv))
3232 sv_setsv(oreplsv, GvSV(PL_replgv));
3239 char *s = STRING(scan);
3241 if (do_utf8 != UTF) {
3242 /* The target and the pattern have differing utf8ness. */
3244 const char * const e = s + ln;
3247 /* The target is utf8, the pattern is not utf8. */
3252 if (NATIVE_TO_UNI(*(U8*)s) !=
3253 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3261 /* The target is not utf8, the pattern is utf8. */
3266 if (NATIVE_TO_UNI(*((U8*)l)) !=
3267 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3275 nextchr = UCHARAT(locinput);
3278 /* The target and the pattern have the same utf8ness. */
3279 /* Inline the first character, for speed. */
3280 if (UCHARAT(s) != nextchr)
3282 if (PL_regeol - locinput < ln)
3284 if (ln > 1 && memNE(s, locinput, ln))
3287 nextchr = UCHARAT(locinput);
3291 PL_reg_flags |= RF_tainted;
3294 char * const s = STRING(scan);
3297 if (do_utf8 || UTF) {
3298 /* Either target or the pattern are utf8. */
3299 const char * const l = locinput;
3300 char *e = PL_regeol;
3302 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
3303 l, &e, 0, do_utf8)) {
3304 /* One more case for the sharp s:
3305 * pack("U0U*", 0xDF) =~ /ss/i,
3306 * the 0xC3 0x9F are the UTF-8
3307 * byte sequence for the U+00DF. */
3310 toLOWER(s[0]) == 's' &&
3312 toLOWER(s[1]) == 's' &&
3319 nextchr = UCHARAT(locinput);
3323 /* Neither the target and the pattern are utf8. */
3325 /* Inline the first character, for speed. */
3326 if (UCHARAT(s) != nextchr &&
3327 UCHARAT(s) != ((OP(scan) == EXACTF)
3328 ? PL_fold : PL_fold_locale)[nextchr])
3330 if (PL_regeol - locinput < ln)
3332 if (ln > 1 && (OP(scan) == EXACTF
3333 ? ibcmp(s, locinput, ln)
3334 : ibcmp_locale(s, locinput, ln)))
3337 nextchr = UCHARAT(locinput);
3342 STRLEN inclasslen = PL_regeol - locinput;
3344 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3346 if (locinput >= PL_regeol)
3348 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3349 nextchr = UCHARAT(locinput);
3354 nextchr = UCHARAT(locinput);
3355 if (!REGINCLASS(rex, scan, (U8*)locinput))
3357 if (!nextchr && locinput >= PL_regeol)
3359 nextchr = UCHARAT(++locinput);
3363 /* If we might have the case of the German sharp s
3364 * in a casefolding Unicode character class. */
3366 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3367 locinput += SHARP_S_SKIP;
3368 nextchr = UCHARAT(locinput);
3374 PL_reg_flags |= RF_tainted;
3380 LOAD_UTF8_CHARCLASS_ALNUM();
3381 if (!(OP(scan) == ALNUM
3382 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3383 : isALNUM_LC_utf8((U8*)locinput)))
3387 locinput += PL_utf8skip[nextchr];
3388 nextchr = UCHARAT(locinput);
3391 if (!(OP(scan) == ALNUM
3392 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
3394 nextchr = UCHARAT(++locinput);
3397 PL_reg_flags |= RF_tainted;
3400 if (!nextchr && locinput >= PL_regeol)
3403 LOAD_UTF8_CHARCLASS_ALNUM();
3404 if (OP(scan) == NALNUM
3405 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
3406 : isALNUM_LC_utf8((U8*)locinput))
3410 locinput += PL_utf8skip[nextchr];
3411 nextchr = UCHARAT(locinput);
3414 if (OP(scan) == NALNUM
3415 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
3417 nextchr = UCHARAT(++locinput);
3421 PL_reg_flags |= RF_tainted;
3425 /* was last char in word? */
3427 if (locinput == PL_bostr)
3430 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3432 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3434 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3435 ln = isALNUM_uni(ln);
3436 LOAD_UTF8_CHARCLASS_ALNUM();
3437 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3440 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3441 n = isALNUM_LC_utf8((U8*)locinput);
3445 ln = (locinput != PL_bostr) ?
3446 UCHARAT(locinput - 1) : '\n';
3447 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3449 n = isALNUM(nextchr);
3452 ln = isALNUM_LC(ln);
3453 n = isALNUM_LC(nextchr);
3456 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3457 OP(scan) == BOUNDL))
3461 PL_reg_flags |= RF_tainted;
3467 if (UTF8_IS_CONTINUED(nextchr)) {
3468 LOAD_UTF8_CHARCLASS_SPACE();
3469 if (!(OP(scan) == SPACE
3470 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3471 : isSPACE_LC_utf8((U8*)locinput)))
3475 locinput += PL_utf8skip[nextchr];
3476 nextchr = UCHARAT(locinput);
3479 if (!(OP(scan) == SPACE
3480 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3482 nextchr = UCHARAT(++locinput);
3485 if (!(OP(scan) == SPACE
3486 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3488 nextchr = UCHARAT(++locinput);
3492 PL_reg_flags |= RF_tainted;
3495 if (!nextchr && locinput >= PL_regeol)
3498 LOAD_UTF8_CHARCLASS_SPACE();
3499 if (OP(scan) == NSPACE
3500 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
3501 : isSPACE_LC_utf8((U8*)locinput))
3505 locinput += PL_utf8skip[nextchr];
3506 nextchr = UCHARAT(locinput);
3509 if (OP(scan) == NSPACE
3510 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
3512 nextchr = UCHARAT(++locinput);
3515 PL_reg_flags |= RF_tainted;
3521 LOAD_UTF8_CHARCLASS_DIGIT();
3522 if (!(OP(scan) == DIGIT
3523 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3524 : isDIGIT_LC_utf8((U8*)locinput)))
3528 locinput += PL_utf8skip[nextchr];
3529 nextchr = UCHARAT(locinput);
3532 if (!(OP(scan) == DIGIT
3533 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
3535 nextchr = UCHARAT(++locinput);
3538 PL_reg_flags |= RF_tainted;
3541 if (!nextchr && locinput >= PL_regeol)
3544 LOAD_UTF8_CHARCLASS_DIGIT();
3545 if (OP(scan) == NDIGIT
3546 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
3547 : isDIGIT_LC_utf8((U8*)locinput))
3551 locinput += PL_utf8skip[nextchr];
3552 nextchr = UCHARAT(locinput);
3555 if (OP(scan) == NDIGIT
3556 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
3558 nextchr = UCHARAT(++locinput);
3561 if (locinput >= PL_regeol)
3564 LOAD_UTF8_CHARCLASS_MARK();
3565 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3567 locinput += PL_utf8skip[nextchr];
3568 while (locinput < PL_regeol &&
3569 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3570 locinput += UTF8SKIP(locinput);
3571 if (locinput > PL_regeol)
3576 nextchr = UCHARAT(locinput);
3583 PL_reg_flags |= RF_tainted;
3588 n = reg_check_named_buff_matched(rex,scan);
3591 type = REF + ( type - NREF );
3598 PL_reg_flags |= RF_tainted;
3602 n = ARG(scan); /* which paren pair */
3605 ln = PL_regoffs[n].start;
3606 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3607 if (*PL_reglastparen < n || ln == -1)
3608 sayNO; /* Do not match unless seen CLOSEn. */
3609 if (ln == PL_regoffs[n].end)
3613 if (do_utf8 && type != REF) { /* REF can do byte comparison */
3615 const char *e = PL_bostr + PL_regoffs[n].end;
3617 * Note that we can't do the "other character" lookup trick as
3618 * in the 8-bit case (no pun intended) because in Unicode we
3619 * have to map both upper and title case to lower case.
3623 STRLEN ulen1, ulen2;
3624 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3625 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3629 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3630 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
3631 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
3638 nextchr = UCHARAT(locinput);
3642 /* Inline the first character, for speed. */
3643 if (UCHARAT(s) != nextchr &&
3645 (UCHARAT(s) != (type == REFF
3646 ? PL_fold : PL_fold_locale)[nextchr])))
3648 ln = PL_regoffs[n].end - ln;
3649 if (locinput + ln > PL_regeol)
3651 if (ln > 1 && (type == REF
3652 ? memNE(s, locinput, ln)
3654 ? ibcmp(s, locinput, ln)
3655 : ibcmp_locale(s, locinput, ln))))
3658 nextchr = UCHARAT(locinput);
3668 #define ST st->u.eval
3672 regexp_internal *rei;
3673 regnode *startpoint;
3676 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3677 if (cur_eval && cur_eval->locinput==locinput) {
3678 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
3679 Perl_croak(aTHX_ "Infinite recursion in regex");
3680 if ( ++nochange_depth > max_nochange_depth )
3682 "Pattern subroutine nesting without pos change"
3683 " exceeded limit in regex");
3689 (void)ReREFCNT_inc(rex);
3690 if (OP(scan)==GOSUB) {
3691 startpoint = scan + ARG2L(scan);
3692 ST.close_paren = ARG(scan);
3694 startpoint = rei->program+1;
3697 goto eval_recurse_doit;
3699 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
3700 if (cur_eval && cur_eval->locinput==locinput) {
3701 if ( ++nochange_depth > max_nochange_depth )
3702 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
3707 /* execute the code in the {...} */
3709 SV ** const before = SP;
3710 OP_4tree * const oop = PL_op;
3711 COP * const ocurcop = PL_curcop;
3715 PL_op = (OP_4tree*)rexi->data->data[n];
3716 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
3717 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
3718 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
3719 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
3722 SV *sv_mrk = get_sv("REGMARK", 1);
3723 sv_setsv(sv_mrk, sv_yes_mark);
3726 CALLRUNOPS(aTHX); /* Scalar context. */
3729 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
3736 PAD_RESTORE_LOCAL(old_comppad);
3737 PL_curcop = ocurcop;
3740 sv_setsv(save_scalar(PL_replgv), ret);
3744 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
3747 /* extract RE object from returned value; compiling if
3752 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
3753 mg = mg_find(sv, PERL_MAGIC_qr);
3754 else if (SvSMAGICAL(ret)) {
3755 if (SvGMAGICAL(ret))
3756 sv_unmagic(ret, PERL_MAGIC_qr);
3758 mg = mg_find(ret, PERL_MAGIC_qr);
3762 re = reg_temp_copy((regexp *)mg->mg_obj); /*XXX:dmq*/
3766 const I32 osize = PL_regsize;
3768 if (DO_UTF8(ret)) pm_flags |= RXf_UTF8;
3769 re = CALLREGCOMP(ret, pm_flags);
3771 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3773 sv_magic(ret,MUTABLE_SV(ReREFCNT_inc(re)),
3778 RXp_MATCH_COPIED_off(re);
3779 re->subbeg = rex->subbeg;
3780 re->sublen = rex->sublen;
3783 debug_start_match(re, do_utf8, locinput, PL_regeol,
3784 "Matching embedded");
3786 startpoint = rei->program + 1;
3787 ST.close_paren = 0; /* only used for GOSUB */
3788 /* borrowed from regtry */
3789 if (PL_reg_start_tmpl <= re->nparens) {
3790 PL_reg_start_tmpl = re->nparens*3/2 + 3;
3791 if(PL_reg_start_tmp)
3792 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
3794 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
3797 eval_recurse_doit: /* Share code with GOSUB below this line */
3798 /* run the pattern returned from (??{...}) */
3799 ST.cp = regcppush(0); /* Save *all* the positions. */
3800 REGCP_SET(ST.lastcp);
3802 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
3804 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
3805 PL_reglastparen = &re->lastparen;
3806 PL_reglastcloseparen = &re->lastcloseparen;
3808 re->lastcloseparen = 0;
3810 PL_reginput = locinput;
3813 /* XXXX This is too dramatic a measure... */
3816 ST.toggle_reg_flags = PL_reg_flags;
3818 PL_reg_flags |= RF_utf8;
3820 PL_reg_flags &= ~RF_utf8;
3821 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
3824 ST.prev_curlyx = cur_curlyx;
3829 ST.prev_eval = cur_eval;
3831 /* now continue from first node in postoned RE */
3832 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
3835 /* logical is 1, /(?(?{...})X|Y)/ */
3836 sw = (bool)SvTRUE(ret);
3841 case EVAL_AB: /* cleanup after a successful (??{A})B */
3842 /* note: this is called twice; first after popping B, then A */
3843 PL_reg_flags ^= ST.toggle_reg_flags;
3845 SETREX(rex,ST.prev_rex);
3846 rexi = RXi_GET(rex);
3848 cur_eval = ST.prev_eval;
3849 cur_curlyx = ST.prev_curlyx;
3851 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
3852 PL_reglastparen = &rex->lastparen;
3853 PL_reglastcloseparen = &rex->lastcloseparen;
3854 /* also update PL_regoffs */
3855 PL_regoffs = rex->offs;
3857 /* XXXX This is too dramatic a measure... */
3859 if ( nochange_depth )
3864 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
3865 /* note: this is called twice; first after popping B, then A */
3866 PL_reg_flags ^= ST.toggle_reg_flags;
3868 SETREX(rex,ST.prev_rex);
3869 rexi = RXi_GET(rex);
3870 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
3871 PL_reglastparen = &rex->lastparen;
3872 PL_reglastcloseparen = &rex->lastcloseparen;
3874 PL_reginput = locinput;
3875 REGCP_UNWIND(ST.lastcp);
3877 cur_eval = ST.prev_eval;
3878 cur_curlyx = ST.prev_curlyx;
3879 /* XXXX This is too dramatic a measure... */
3881 if ( nochange_depth )
3887 n = ARG(scan); /* which paren pair */
3888 PL_reg_start_tmp[n] = locinput;
3894 n = ARG(scan); /* which paren pair */
3895 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
3896 PL_regoffs[n].end = locinput - PL_bostr;
3897 /*if (n > PL_regsize)
3899 if (n > *PL_reglastparen)
3900 *PL_reglastparen = n;
3901 *PL_reglastcloseparen = n;
3902 if (cur_eval && cur_eval->u.eval.close_paren == n) {
3910 cursor && OP(cursor)!=END;
3911 cursor=regnext(cursor))
3913 if ( OP(cursor)==CLOSE ){
3915 if ( n <= lastopen ) {
3917 = PL_reg_start_tmp[n] - PL_bostr;
3918 PL_regoffs[n].end = locinput - PL_bostr;
3919 /*if (n > PL_regsize)
3921 if (n > *PL_reglastparen)
3922 *PL_reglastparen = n;
3923 *PL_reglastcloseparen = n;
3924 if ( n == ARG(scan) || (cur_eval &&
3925 cur_eval->u.eval.close_paren == n))
3934 n = ARG(scan); /* which paren pair */
3935 sw = (bool)(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
3938 /* reg_check_named_buff_matched returns 0 for no match */
3939 sw = (bool)(0 < reg_check_named_buff_matched(rex,scan));
3943 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
3949 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3951 next = NEXTOPER(NEXTOPER(scan));
3953 next = scan + ARG(scan);
3954 if (OP(next) == IFTHEN) /* Fake one. */
3955 next = NEXTOPER(NEXTOPER(next));
3959 logical = scan->flags;
3962 /*******************************************************************
3964 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
3965 pattern, where A and B are subpatterns. (For simple A, CURLYM or
3966 STAR/PLUS/CURLY/CURLYN are used instead.)
3968 A*B is compiled as <CURLYX><A><WHILEM><B>
3970 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
3971 state, which contains the current count, initialised to -1. It also sets
3972 cur_curlyx to point to this state, with any previous value saved in the
3975 CURLYX then jumps straight to the WHILEM op, rather than executing A,
3976 since the pattern may possibly match zero times (i.e. it's a while {} loop
3977 rather than a do {} while loop).
3979 Each entry to WHILEM represents a successful match of A. The count in the
3980 CURLYX block is incremented, another WHILEM state is pushed, and execution
3981 passes to A or B depending on greediness and the current count.
3983 For example, if matching against the string a1a2a3b (where the aN are
3984 substrings that match /A/), then the match progresses as follows: (the
3985 pushed states are interspersed with the bits of strings matched so far):
3988 <CURLYX cnt=0><WHILEM>
3989 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
3990 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
3991 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
3992 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
3994 (Contrast this with something like CURLYM, which maintains only a single
3998 a1 <CURLYM cnt=1> a2
3999 a1 a2 <CURLYM cnt=2> a3
4000 a1 a2 a3 <CURLYM cnt=3> b
4003 Each WHILEM state block marks a point to backtrack to upon partial failure
4004 of A or B, and also contains some minor state data related to that
4005 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4006 overall state, such as the count, and pointers to the A and B ops.
4008 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4009 must always point to the *current* CURLYX block, the rules are:
4011 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4012 and set cur_curlyx to point the new block.
4014 When popping the CURLYX block after a successful or unsuccessful match,
4015 restore the previous cur_curlyx.
4017 When WHILEM is about to execute B, save the current cur_curlyx, and set it
4018 to the outer one saved in the CURLYX block.
4020 When popping the WHILEM block after a successful or unsuccessful B match,
4021 restore the previous cur_curlyx.
4023 Here's an example for the pattern (AI* BI)*BO
4024 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
4027 curlyx backtrack stack
4028 ------ ---------------
4030 CO <CO prev=NULL> <WO>
4031 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4032 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4033 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
4035 At this point the pattern succeeds, and we work back down the stack to
4036 clean up, restoring as we go:
4038 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4039 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4040 CO <CO prev=NULL> <WO>
4043 *******************************************************************/
4045 #define ST st->u.curlyx
4047 case CURLYX: /* start of /A*B/ (for complex A) */
4049 /* No need to save/restore up to this paren */
4050 I32 parenfloor = scan->flags;
4052 assert(next); /* keep Coverity happy */
4053 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4056 /* XXXX Probably it is better to teach regpush to support
4057 parenfloor > PL_regsize... */
4058 if (parenfloor > (I32)*PL_reglastparen)
4059 parenfloor = *PL_reglastparen; /* Pessimization... */
4061 ST.prev_curlyx= cur_curlyx;
4063 ST.cp = PL_savestack_ix;
4065 /* these fields contain the state of the current curly.
4066 * they are accessed by subsequent WHILEMs */
4067 ST.parenfloor = parenfloor;
4068 ST.min = ARG1(scan);
4069 ST.max = ARG2(scan);
4070 ST.A = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
4074 ST.count = -1; /* this will be updated by WHILEM */
4075 ST.lastloc = NULL; /* this will be updated by WHILEM */
4077 PL_reginput = locinput;
4078 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
4082 case CURLYX_end: /* just finished matching all of A*B */
4083 cur_curlyx = ST.prev_curlyx;
4087 case CURLYX_end_fail: /* just failed to match all of A*B */
4089 cur_curlyx = ST.prev_curlyx;
4095 #define ST st->u.whilem
4097 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4099 /* see the discussion above about CURLYX/WHILEM */
4101 assert(cur_curlyx); /* keep Coverity happy */
4102 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4103 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4104 ST.cache_offset = 0;
4107 PL_reginput = locinput;
4109 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4110 "%*s whilem: matched %ld out of %ld..%ld\n",
4111 REPORT_CODE_OFF+depth*2, "", (long)n,
4112 (long)cur_curlyx->u.curlyx.min,
4113 (long)cur_curlyx->u.curlyx.max)
4116 /* First just match a string of min A's. */
4118 if (n < cur_curlyx->u.curlyx.min) {
4119 cur_curlyx->u.curlyx.lastloc = locinput;
4120 PUSH_STATE_GOTO(WHILEM_A_pre, cur_curlyx->u.curlyx.A);
4124 /* If degenerate A matches "", assume A done. */
4126 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4127 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4128 "%*s whilem: empty match detected, trying continuation...\n",
4129 REPORT_CODE_OFF+depth*2, "")
4131 goto do_whilem_B_max;
4134 /* super-linear cache processing */
4138 if (!PL_reg_maxiter) {
4139 /* start the countdown: Postpone detection until we
4140 * know the match is not *that* much linear. */
4141 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
4142 /* possible overflow for long strings and many CURLYX's */
4143 if (PL_reg_maxiter < 0)
4144 PL_reg_maxiter = I32_MAX;
4145 PL_reg_leftiter = PL_reg_maxiter;
4148 if (PL_reg_leftiter-- == 0) {
4149 /* initialise cache */
4150 const I32 size = (PL_reg_maxiter + 7)/8;
4151 if (PL_reg_poscache) {
4152 if ((I32)PL_reg_poscache_size < size) {
4153 Renew(PL_reg_poscache, size, char);
4154 PL_reg_poscache_size = size;
4156 Zero(PL_reg_poscache, size, char);
4159 PL_reg_poscache_size = size;
4160 Newxz(PL_reg_poscache, size, char);
4162 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4163 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4164 PL_colors[4], PL_colors[5])
4168 if (PL_reg_leftiter < 0) {
4169 /* have we already failed at this position? */
4171 offset = (scan->flags & 0xf) - 1
4172 + (locinput - PL_bostr) * (scan->flags>>4);
4173 mask = 1 << (offset % 8);
4175 if (PL_reg_poscache[offset] & mask) {
4176 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4177 "%*s whilem: (cache) already tried at this position...\n",
4178 REPORT_CODE_OFF+depth*2, "")
4180 sayNO; /* cache records failure */
4182 ST.cache_offset = offset;
4183 ST.cache_mask = mask;
4187 /* Prefer B over A for minimal matching. */
4189 if (cur_curlyx->u.curlyx.minmod) {
4190 ST.save_curlyx = cur_curlyx;
4191 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4192 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4193 REGCP_SET(ST.lastcp);
4194 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4198 /* Prefer A over B for maximal matching. */
4200 if (n < cur_curlyx->u.curlyx.max) { /* More greed allowed? */
4201 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4202 cur_curlyx->u.curlyx.lastloc = locinput;
4203 REGCP_SET(ST.lastcp);
4204 PUSH_STATE_GOTO(WHILEM_A_max, cur_curlyx->u.curlyx.A);
4207 goto do_whilem_B_max;
4211 case WHILEM_B_min: /* just matched B in a minimal match */
4212 case WHILEM_B_max: /* just matched B in a maximal match */
4213 cur_curlyx = ST.save_curlyx;
4217 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4218 cur_curlyx = ST.save_curlyx;
4219 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4220 cur_curlyx->u.curlyx.count--;
4224 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4225 REGCP_UNWIND(ST.lastcp);
4228 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4229 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4230 cur_curlyx->u.curlyx.count--;
4234 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4235 REGCP_UNWIND(ST.lastcp);
4236 regcppop(rex); /* Restore some previous $<digit>s? */
4237 PL_reginput = locinput;
4238 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4239 "%*s whilem: failed, trying continuation...\n",
4240 REPORT_CODE_OFF+depth*2, "")
4243 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4244 && ckWARN(WARN_REGEXP)
4245 && !(PL_reg_flags & RF_warned))
4247 PL_reg_flags |= RF_warned;
4248 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4249 "Complex regular subexpression recursion",
4254 ST.save_curlyx = cur_curlyx;
4255 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4256 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4259 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4260 cur_curlyx = ST.save_curlyx;
4261 REGCP_UNWIND(ST.lastcp);
4264 if (cur_curlyx->u.curlyx.count >= cur_curlyx->u.curlyx.max) {
4265 /* Maximum greed exceeded */
4266 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4267 && ckWARN(WARN_REGEXP)
4268 && !(PL_reg_flags & RF_warned))
4270 PL_reg_flags |= RF_warned;
4271 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4272 "%s limit (%d) exceeded",
4273 "Complex regular subexpression recursion",
4276 cur_curlyx->u.curlyx.count--;
4280 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4281 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4283 /* Try grabbing another A and see if it helps. */
4284 PL_reginput = locinput;
4285 cur_curlyx->u.curlyx.lastloc = locinput;
4286 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4287 REGCP_SET(ST.lastcp);
4288 PUSH_STATE_GOTO(WHILEM_A_min, ST.save_curlyx->u.curlyx.A);
4292 #define ST st->u.branch
4294 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
4295 next = scan + ARG(scan);
4298 scan = NEXTOPER(scan);
4301 case BRANCH: /* /(...|A|...)/ */
4302 scan = NEXTOPER(scan); /* scan now points to inner node */
4303 ST.lastparen = *PL_reglastparen;
4304 ST.next_branch = next;
4306 PL_reginput = locinput;
4308 /* Now go into the branch */
4310 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4312 PUSH_STATE_GOTO(BRANCH_next, scan);
4316 PL_reginput = locinput;
4317 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
4318 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4319 PUSH_STATE_GOTO(CUTGROUP_next,next);
4321 case CUTGROUP_next_fail:
4324 if (st->u.mark.mark_name)
4325 sv_commit = st->u.mark.mark_name;
4331 case BRANCH_next_fail: /* that branch failed; try the next, if any */
4336 REGCP_UNWIND(ST.cp);
4337 for (n = *PL_reglastparen; n > ST.lastparen; n--)
4338 PL_regoffs[n].end = -1;
4339 *PL_reglastparen = n;
4340 /*dmq: *PL_reglastcloseparen = n; */
4341 scan = ST.next_branch;
4342 /* no more branches? */
4343 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4345 PerlIO_printf( Perl_debug_log,
4346 "%*s %sBRANCH failed...%s\n",
4347 REPORT_CODE_OFF+depth*2, "",
4353 continue; /* execute next BRANCH[J] op */
4361 #define ST st->u.curlym
4363 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4365 /* This is an optimisation of CURLYX that enables us to push
4366 * only a single backtracking state, no matter how many matches
4367 * there are in {m,n}. It relies on the pattern being constant
4368 * length, with no parens to influence future backrefs
4372 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4374 /* if paren positive, emulate an OPEN/CLOSE around A */
4376 U32 paren = ST.me->flags;
4377 if (paren > PL_regsize)
4379 if (paren > *PL_reglastparen)
4380 *PL_reglastparen = paren;
4381 scan += NEXT_OFF(scan); /* Skip former OPEN. */
4389 ST.c1 = CHRTEST_UNINIT;
4392 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4395 curlym_do_A: /* execute the A in /A{m,n}B/ */
4396 PL_reginput = locinput;
4397 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4400 case CURLYM_A: /* we've just matched an A */
4401 locinput = st->locinput;
4402 nextchr = UCHARAT(locinput);
4405 /* after first match, determine A's length: u.curlym.alen */
4406 if (ST.count == 1) {
4407 if (PL_reg_match_utf8) {
4409 while (s < PL_reginput) {
4415 ST.alen = PL_reginput - locinput;
4418 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4421 PerlIO_printf(Perl_debug_log,
4422 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
4423 (int)(REPORT_CODE_OFF+(depth*2)), "",
4424 (IV) ST.count, (IV)ST.alen)
4427 locinput = PL_reginput;
4429 if (cur_eval && cur_eval->u.eval.close_paren &&
4430 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4434 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4435 if ( max == REG_INFTY || ST.count < max )
4436 goto curlym_do_A; /* try to match another A */
4438 goto curlym_do_B; /* try to match B */
4440 case CURLYM_A_fail: /* just failed to match an A */
4441 REGCP_UNWIND(ST.cp);
4443 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4444 || (cur_eval && cur_eval->u.eval.close_paren &&
4445 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
4448 curlym_do_B: /* execute the B in /A{m,n}B/ */
4449 PL_reginput = locinput;
4450 if (ST.c1 == CHRTEST_UNINIT) {
4451 /* calculate c1 and c2 for possible match of 1st char
4452 * following curly */
4453 ST.c1 = ST.c2 = CHRTEST_VOID;
4454 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4455 regnode *text_node = ST.B;
4456 if (! HAS_TEXT(text_node))
4457 FIND_NEXT_IMPT(text_node);
4460 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4462 But the former is redundant in light of the latter.
4464 if this changes back then the macro for
4465 IS_TEXT and friends need to change.
4467 if (PL_regkind[OP(text_node)] == EXACT)
4470 ST.c1 = (U8)*STRING(text_node);
4472 (IS_TEXTF(text_node))
4474 : (IS_TEXTFL(text_node))
4475 ? PL_fold_locale[ST.c1]
4482 PerlIO_printf(Perl_debug_log,
4483 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
4484 (int)(REPORT_CODE_OFF+(depth*2)),
4487 if (ST.c1 != CHRTEST_VOID
4488 && UCHARAT(PL_reginput) != ST.c1
4489 && UCHARAT(PL_reginput) != ST.c2)
4491 /* simulate B failing */
4493 PerlIO_printf(Perl_debug_log,
4494 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4495 (int)(REPORT_CODE_OFF+(depth*2)),"",
4498 state_num = CURLYM_B_fail;
4499 goto reenter_switch;
4503 /* mark current A as captured */
4504 I32 paren = ST.me->flags;
4506 PL_regoffs[paren].start
4507 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
4508 PL_regoffs[paren].end = PL_reginput - PL_bostr;
4509 /*dmq: *PL_reglastcloseparen = paren; */
4512 PL_regoffs[paren].end = -1;
4513 if (cur_eval && cur_eval->u.eval.close_paren &&
4514 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
4523 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
4526 case CURLYM_B_fail: /* just failed to match a B */
4527 REGCP_UNWIND(ST.cp);
4529 I32 max = ARG2(ST.me);
4530 if (max != REG_INFTY && ST.count == max)
4532 goto curlym_do_A; /* try to match a further A */
4534 /* backtrack one A */
4535 if (ST.count == ARG1(ST.me) /* min */)
4538 locinput = HOPc(locinput, -ST.alen);
4539 goto curlym_do_B; /* try to match B */
4542 #define ST st->u.curly
4544 #define CURLY_SETPAREN(paren, success) \
4547 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4548 PL_regoffs[paren].end = locinput - PL_bostr; \
4549 *PL_reglastcloseparen = paren; \
4552 PL_regoffs[paren].end = -1; \
4555 case STAR: /* /A*B/ where A is width 1 */
4559 scan = NEXTOPER(scan);
4561 case PLUS: /* /A+B/ where A is width 1 */
4565 scan = NEXTOPER(scan);
4567 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4568 ST.paren = scan->flags; /* Which paren to set */
4569 if (ST.paren > PL_regsize)
4570 PL_regsize = ST.paren;
4571 if (ST.paren > *PL_reglastparen)
4572 *PL_reglastparen = ST.paren;
4573 ST.min = ARG1(scan); /* min to match */
4574 ST.max = ARG2(scan); /* max to match */
4575 if (cur_eval && cur_eval->u.eval.close_paren &&
4576 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4580 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4582 case CURLY: /* /A{m,n}B/ where A is width 1 */
4584 ST.min = ARG1(scan); /* min to match */
4585 ST.max = ARG2(scan); /* max to match */
4586 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
4589 * Lookahead to avoid useless match attempts
4590 * when we know what character comes next.
4592 * Used to only do .*x and .*?x, but now it allows
4593 * for )'s, ('s and (?{ ... })'s to be in the way
4594 * of the quantifier and the EXACT-like node. -- japhy
4597 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4599 if (HAS_TEXT(next) || JUMPABLE(next)) {
4601 regnode *text_node = next;
4603 if (! HAS_TEXT(text_node))
4604 FIND_NEXT_IMPT(text_node);
4606 if (! HAS_TEXT(text_node))
4607 ST.c1 = ST.c2 = CHRTEST_VOID;
4609 if ( PL_regkind[OP(text_node)] != EXACT ) {
4610 ST.c1 = ST.c2 = CHRTEST_VOID;
4611 goto assume_ok_easy;
4614 s = (U8*)STRING(text_node);
4616 /* Currently we only get here when
4618 PL_rekind[OP(text_node)] == EXACT
4620 if this changes back then the macro for IS_TEXT and
4621 friends need to change. */
4624 if (IS_TEXTF(text_node))
4625 ST.c2 = PL_fold[ST.c1];
4626 else if (IS_TEXTFL(text_node))
4627 ST.c2 = PL_fold_locale[ST.c1];
4630 if (IS_TEXTF(text_node)) {
4631 STRLEN ulen1, ulen2;
4632 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
4633 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4635 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
4636 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
4638 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
4640 0 : UTF8_ALLOW_ANY);
4641 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
4643 0 : UTF8_ALLOW_ANY);
4645 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
4647 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
4652 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
4659 ST.c1 = ST.c2 = CHRTEST_VOID;
4664 PL_reginput = locinput;
4667 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4670 locinput = PL_reginput;
4672 if (ST.c1 == CHRTEST_VOID)
4673 goto curly_try_B_min;
4675 ST.oldloc = locinput;
4677 /* set ST.maxpos to the furthest point along the
4678 * string that could possibly match */
4679 if (ST.max == REG_INFTY) {
4680 ST.maxpos = PL_regeol - 1;
4682 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
4686 int m = ST.max - ST.min;
4687 for (ST.maxpos = locinput;
4688 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4689 ST.maxpos += UTF8SKIP(ST.maxpos);
4692 ST.maxpos = locinput + ST.max - ST.min;
4693 if (ST.maxpos >= PL_regeol)
4694 ST.maxpos = PL_regeol - 1;
4696 goto curly_try_B_min_known;
4700 ST.count = regrepeat(rex, ST.A, ST.max, depth);
4701 locinput = PL_reginput;
4702 if (ST.count < ST.min)
4704 if ((ST.count > ST.min)
4705 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4707 /* A{m,n} must come at the end of the string, there's
4708 * no point in backing off ... */
4710 /* ...except that $ and \Z can match before *and* after
4711 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4712 We may back off by one in this case. */
4713 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4717 goto curly_try_B_max;
4722 case CURLY_B_min_known_fail:
4723 /* failed to find B in a non-greedy match where c1,c2 valid */
4724 if (ST.paren && ST.count)
4725 PL_regoffs[ST.paren].end = -1;
4727 PL_reginput = locinput; /* Could be reset... */
4728 REGCP_UNWIND(ST.cp);
4729 /* Couldn't or didn't -- move forward. */
4730 ST.oldloc = locinput;
4732 locinput += UTF8SKIP(locinput);
4736 curly_try_B_min_known:
4737 /* find the next place where 'B' could work, then call B */
4741 n = (ST.oldloc == locinput) ? 0 : 1;
4742 if (ST.c1 == ST.c2) {
4744 /* set n to utf8_distance(oldloc, locinput) */
4745 while (locinput <= ST.maxpos &&
4746 utf8n_to_uvchr((U8*)locinput,
4747 UTF8_MAXBYTES, &len,
4748 uniflags) != (UV)ST.c1) {
4754 /* set n to utf8_distance(oldloc, locinput) */
4755 while (locinput <= ST.maxpos) {
4757 const UV c = utf8n_to_uvchr((U8*)locinput,
4758 UTF8_MAXBYTES, &len,
4760 if (c == (UV)ST.c1 || c == (UV)ST.c2)
4768 if (ST.c1 == ST.c2) {
4769 while (locinput <= ST.maxpos &&
4770 UCHARAT(locinput) != ST.c1)
4774 while (locinput <= ST.maxpos
4775 && UCHARAT(locinput) != ST.c1
4776 && UCHARAT(locinput) != ST.c2)
4779 n = locinput - ST.oldloc;
4781 if (locinput > ST.maxpos)
4783 /* PL_reginput == oldloc now */
4786 if (regrepeat(rex, ST.A, n, depth) < n)
4789 PL_reginput = locinput;
4790 CURLY_SETPAREN(ST.paren, ST.count);
4791 if (cur_eval && cur_eval->u.eval.close_paren &&
4792 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4795 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
4800 case CURLY_B_min_fail:
4801 /* failed to find B in a non-greedy match where c1,c2 invalid */
4802 if (ST.paren && ST.count)
4803 PL_regoffs[ST.paren].end = -1;
4805 REGCP_UNWIND(ST.cp);
4806 /* failed -- move forward one */
4807 PL_reginput = locinput;
4808 if (regrepeat(rex, ST.A, 1, depth)) {
4810 locinput = PL_reginput;
4811 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
4812 ST.count > 0)) /* count overflow ? */
4815 CURLY_SETPAREN(ST.paren, ST.count);
4816 if (cur_eval && cur_eval->u.eval.close_paren &&
4817 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4820 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
4828 /* a successful greedy match: now try to match B */
4829 if (cur_eval && cur_eval->u.eval.close_paren &&
4830 cur_eval->u.eval.close_paren == (U32)ST.paren) {
4835 if (ST.c1 != CHRTEST_VOID)
4836 c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput,
4837 UTF8_MAXBYTES, 0, uniflags)
4838 : (UV) UCHARAT(PL_reginput);
4839 /* If it could work, try it. */
4840 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
4841 CURLY_SETPAREN(ST.paren, ST.count);
4842 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
4847 case CURLY_B_max_fail:
4848 /* failed to find B in a greedy match */
4849 if (ST.paren && ST.count)
4850 PL_regoffs[ST.paren].end = -1;
4852 REGCP_UNWIND(ST.cp);
4854 if (--ST.count < ST.min)
4856 PL_reginput = locinput = HOPc(locinput, -1);
4857 goto curly_try_B_max;
4864 /* we've just finished A in /(??{A})B/; now continue with B */
4866 st->u.eval.toggle_reg_flags
4867 = cur_eval->u.eval.toggle_reg_flags;
4868 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
4870 st->u.eval.prev_rex = rex; /* inner */
4871 SETREX(rex,cur_eval->u.eval.prev_rex);
4872 rexi = RXi_GET(rex);
4873 cur_curlyx = cur_eval->u.eval.prev_curlyx;
4875 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
4877 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
4878 PL_reglastparen = &rex->lastparen;
4879 PL_reglastcloseparen = &rex->lastcloseparen;
4881 REGCP_SET(st->u.eval.lastcp);
4882 PL_reginput = locinput;
4884 /* Restore parens of the outer rex without popping the
4886 tmpix = PL_savestack_ix;
4887 PL_savestack_ix = cur_eval->u.eval.lastcp;
4889 PL_savestack_ix = tmpix;
4891 st->u.eval.prev_eval = cur_eval;
4892 cur_eval = cur_eval->u.eval.prev_eval;
4894 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
4895 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
4896 if ( nochange_depth )
4899 PUSH_YES_STATE_GOTO(EVAL_AB,
4900 st->u.eval.prev_eval->u.eval.B); /* match B */
4903 if (locinput < reginfo->till) {
4904 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4905 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
4907 (long)(locinput - PL_reg_starttry),
4908 (long)(reginfo->till - PL_reg_starttry),
4911 sayNO_SILENT; /* Cannot match: too short. */
4913 PL_reginput = locinput; /* put where regtry can find it */
4914 sayYES; /* Success! */
4916 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
4918 PerlIO_printf(Perl_debug_log,
4919 "%*s %ssubpattern success...%s\n",
4920 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
4921 PL_reginput = locinput; /* put where regtry can find it */
4922 sayYES; /* Success! */
4925 #define ST st->u.ifmatch
4927 case SUSPEND: /* (?>A) */
4929 PL_reginput = locinput;
4932 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
4934 goto ifmatch_trivial_fail_test;
4936 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
4938 ifmatch_trivial_fail_test:
4940 char * const s = HOPBACKc(locinput, scan->flags);
4945 sw = 1 - (bool)ST.wanted;
4949 next = scan + ARG(scan);
4957 PL_reginput = locinput;
4961 ST.logical = logical;
4962 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
4964 /* execute body of (?...A) */
4965 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
4968 case IFMATCH_A_fail: /* body of (?...A) failed */
4969 ST.wanted = !ST.wanted;
4972 case IFMATCH_A: /* body of (?...A) succeeded */
4974 sw = (bool)ST.wanted;
4976 else if (!ST.wanted)
4979 if (OP(ST.me) == SUSPEND)
4980 locinput = PL_reginput;
4982 locinput = PL_reginput = st->locinput;
4983 nextchr = UCHARAT(locinput);
4985 scan = ST.me + ARG(ST.me);
4988 continue; /* execute B */
4993 next = scan + ARG(scan);
4998 reginfo->cutpoint = PL_regeol;
5001 PL_reginput = locinput;
5003 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5004 PUSH_STATE_GOTO(COMMIT_next,next);
5006 case COMMIT_next_fail:
5013 #define ST st->u.mark
5015 ST.prev_mark = mark_state;
5016 ST.mark_name = sv_commit = sv_yes_mark
5017 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5019 ST.mark_loc = PL_reginput = locinput;
5020 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5022 case MARKPOINT_next:
5023 mark_state = ST.prev_mark;
5026 case MARKPOINT_next_fail:
5027 if (popmark && sv_eq(ST.mark_name,popmark))
5029 if (ST.mark_loc > startpoint)
5030 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5031 popmark = NULL; /* we found our mark */
5032 sv_commit = ST.mark_name;
5035 PerlIO_printf(Perl_debug_log,
5036 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5037 REPORT_CODE_OFF+depth*2, "",
5038 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
5041 mark_state = ST.prev_mark;
5042 sv_yes_mark = mark_state ?
5043 mark_state->u.mark.mark_name : NULL;
5047 PL_reginput = locinput;
5049 /* (*SKIP) : if we fail we cut here*/
5050 ST.mark_name = NULL;
5051 ST.mark_loc = locinput;
5052 PUSH_STATE_GOTO(SKIP_next,next);
5054 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5055 otherwise do nothing. Meaning we need to scan
5057 regmatch_state *cur = mark_state;
5058 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5061 if ( sv_eq( cur->u.mark.mark_name,
5064 ST.mark_name = find;
5065 PUSH_STATE_GOTO( SKIP_next, next );
5067 cur = cur->u.mark.prev_mark;
5070 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5072 case SKIP_next_fail:
5074 /* (*CUT:NAME) - Set up to search for the name as we
5075 collapse the stack*/
5076 popmark = ST.mark_name;
5078 /* (*CUT) - No name, we cut here.*/
5079 if (ST.mark_loc > startpoint)
5080 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5081 /* but we set sv_commit to latest mark_name if there
5082 is one so they can test to see how things lead to this
5085 sv_commit=mark_state->u.mark.mark_name;
5093 if ( n == (U32)what_len_TRICKYFOLD(locinput,do_utf8,ln) ) {
5095 } else if ( 0xDF == n && !do_utf8 && !UTF ) {
5098 U8 folded[UTF8_MAXBYTES_CASE+1];
5100 const char * const l = locinput;
5101 char *e = PL_regeol;
5102 to_uni_fold(n, folded, &foldlen);
5104 if (ibcmp_utf8((const char*) folded, 0, foldlen, 1,
5105 l, &e, 0, do_utf8)) {
5110 nextchr = UCHARAT(locinput);
5113 if ((n=is_LNBREAK(locinput,do_utf8))) {
5115 nextchr = UCHARAT(locinput);
5120 #define CASE_CLASS(nAmE) \
5122 if ((n=is_##nAmE(locinput,do_utf8))) { \
5124 nextchr = UCHARAT(locinput); \
5129 if ((n=is_##nAmE(locinput,do_utf8))) { \
5132 locinput += UTF8SKIP(locinput); \
5133 nextchr = UCHARAT(locinput); \
5138 CASE_CLASS(HORIZWS);
5142 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
5143 PTR2UV(scan), OP(scan));
5144 Perl_croak(aTHX_ "regexp memory corruption");
5148 /* switch break jumps here */
5149 scan = next; /* prepare to execute the next op and ... */
5150 continue; /* ... jump back to the top, reusing st */
5154 /* push a state that backtracks on success */
5155 st->u.yes.prev_yes_state = yes_state;
5159 /* push a new regex state, then continue at scan */
5161 regmatch_state *newst;
5164 regmatch_state *cur = st;
5165 regmatch_state *curyes = yes_state;
5167 regmatch_slab *slab = PL_regmatch_slab;
5168 for (;curd > -1;cur--,curd--) {
5169 if (cur < SLAB_FIRST(slab)) {
5171 cur = SLAB_LAST(slab);
5173 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5174 REPORT_CODE_OFF + 2 + depth * 2,"",
5175 curd, PL_reg_name[cur->resume_state],
5176 (curyes == cur) ? "yes" : ""
5179 curyes = cur->u.yes.prev_yes_state;
5182 DEBUG_STATE_pp("push")
5185 st->locinput = locinput;
5187 if (newst > SLAB_LAST(PL_regmatch_slab))
5188 newst = S_push_slab(aTHX);
5189 PL_regmatch_state = newst;
5191 locinput = PL_reginput;
5192 nextchr = UCHARAT(locinput);
5200 * We get here only if there's trouble -- normally "case END" is
5201 * the terminating point.
5203 Perl_croak(aTHX_ "corrupted regexp pointers");
5209 /* we have successfully completed a subexpression, but we must now
5210 * pop to the state marked by yes_state and continue from there */
5211 assert(st != yes_state);
5213 while (st != yes_state) {
5215 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5216 PL_regmatch_slab = PL_regmatch_slab->prev;
5217 st = SLAB_LAST(PL_regmatch_slab);
5221 DEBUG_STATE_pp("pop (no final)");
5223 DEBUG_STATE_pp("pop (yes)");
5229 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5230 || yes_state > SLAB_LAST(PL_regmatch_slab))
5232 /* not in this slab, pop slab */
5233 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5234 PL_regmatch_slab = PL_regmatch_slab->prev;
5235 st = SLAB_LAST(PL_regmatch_slab);
5237 depth -= (st - yes_state);
5240 yes_state = st->u.yes.prev_yes_state;
5241 PL_regmatch_state = st;
5244 locinput= st->locinput;
5245 nextchr = UCHARAT(locinput);
5247 state_num = st->resume_state + no_final;
5248 goto reenter_switch;
5251 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
5252 PL_colors[4], PL_colors[5]));
5254 if (PL_reg_eval_set) {
5255 /* each successfully executed (?{...}) block does the equivalent of
5256 * local $^R = do {...}
5257 * When popping the save stack, all these locals would be undone;
5258 * bypass this by setting the outermost saved $^R to the latest
5260 if (oreplsv != GvSV(PL_replgv))
5261 sv_setsv(oreplsv, GvSV(PL_replgv));
5268 PerlIO_printf(Perl_debug_log,
5269 "%*s %sfailed...%s\n",
5270 REPORT_CODE_OFF+depth*2, "",
5271 PL_colors[4], PL_colors[5])
5283 /* there's a previous state to backtrack to */
5285 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5286 PL_regmatch_slab = PL_regmatch_slab->prev;
5287 st = SLAB_LAST(PL_regmatch_slab);
5289 PL_regmatch_state = st;
5290 locinput= st->locinput;
5291 nextchr = UCHARAT(locinput);
5293 DEBUG_STATE_pp("pop");
5295 if (yes_state == st)
5296 yes_state = st->u.yes.prev_yes_state;
5298 state_num = st->resume_state + 1; /* failure = success + 1 */
5299 goto reenter_switch;
5304 if (rex->intflags & PREGf_VERBARG_SEEN) {
5305 SV *sv_err = get_sv("REGERROR", 1);
5306 SV *sv_mrk = get_sv("REGMARK", 1);
5308 sv_commit = &PL_sv_no;
5310 sv_yes_mark = &PL_sv_yes;
5313 sv_commit = &PL_sv_yes;
5314 sv_yes_mark = &PL_sv_no;
5316 sv_setsv(sv_err, sv_commit);
5317 sv_setsv(sv_mrk, sv_yes_mark);
5320 /* clean up; in particular, free all slabs above current one */
5321 LEAVE_SCOPE(oldsave);
5327 - regrepeat - repeatedly match something simple, report how many
5330 * [This routine now assumes that it will only match on things of length 1.
5331 * That was true before, but now we assume scan - reginput is the count,
5332 * rather than incrementing count on every character. [Er, except utf8.]]
5335 S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
5338 register char *scan;
5340 register char *loceol = PL_regeol;
5341 register I32 hardcount = 0;
5342 register bool do_utf8 = PL_reg_match_utf8;
5344 PERL_UNUSED_ARG(depth);
5347 PERL_ARGS_ASSERT_REGREPEAT;
5350 if (max == REG_INFTY)
5352 else if (max < loceol - scan)
5353 loceol = scan + max;
5358 while (scan < loceol && hardcount < max && *scan != '\n') {
5359 scan += UTF8SKIP(scan);
5363 while (scan < loceol && *scan != '\n')
5370 while (scan < loceol && hardcount < max) {
5371 scan += UTF8SKIP(scan);
5381 case EXACT: /* length of string is 1 */
5383 while (scan < loceol && UCHARAT(scan) == c)
5386 case EXACTF: /* length of string is 1 */
5388 while (scan < loceol &&
5389 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5392 case EXACTFL: /* length of string is 1 */
5393 PL_reg_flags |= RF_tainted;
5395 while (scan < loceol &&
5396 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5402 while (hardcount < max && scan < loceol &&
5403 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
5404 scan += UTF8SKIP(scan);
5408 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
5415 LOAD_UTF8_CHARCLASS_ALNUM();
5416 while (hardcount < max && scan < loceol &&
5417 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5418 scan += UTF8SKIP(scan);
5422 while (scan < loceol && isALNUM(*scan))
5427 PL_reg_flags |= RF_tainted;
5430 while (hardcount < max && scan < loceol &&
5431 isALNUM_LC_utf8((U8*)scan)) {
5432 scan += UTF8SKIP(scan);
5436 while (scan < loceol && isALNUM_LC(*scan))
5443 LOAD_UTF8_CHARCLASS_ALNUM();
5444 while (hardcount < max && scan < loceol &&
5445 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
5446 scan += UTF8SKIP(scan);
5450 while (scan < loceol && !isALNUM(*scan))
5455 PL_reg_flags |= RF_tainted;
5458 while (hardcount < max && scan < loceol &&
5459 !isALNUM_LC_utf8((U8*)scan)) {
5460 scan += UTF8SKIP(scan);
5464 while (scan < loceol && !isALNUM_LC(*scan))
5471 LOAD_UTF8_CHARCLASS_SPACE();
5472 while (hardcount < max && scan < loceol &&
5474 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5475 scan += UTF8SKIP(scan);
5479 while (scan < loceol && isSPACE(*scan))
5484 PL_reg_flags |= RF_tainted;
5487 while (hardcount < max && scan < loceol &&
5488 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5489 scan += UTF8SKIP(scan);
5493 while (scan < loceol && isSPACE_LC(*scan))
5500 LOAD_UTF8_CHARCLASS_SPACE();
5501 while (hardcount < max && scan < loceol &&
5503 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
5504 scan += UTF8SKIP(scan);
5508 while (scan < loceol && !isSPACE(*scan))
5513 PL_reg_flags |= RF_tainted;
5516 while (hardcount < max && scan < loceol &&
5517 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5518 scan += UTF8SKIP(scan);
5522 while (scan < loceol && !isSPACE_LC(*scan))
5529 LOAD_UTF8_CHARCLASS_DIGIT();
5530 while (hardcount < max && scan < loceol &&
5531 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5532 scan += UTF8SKIP(scan);
5536 while (scan < loceol && isDIGIT(*scan))
5543 LOAD_UTF8_CHARCLASS_DIGIT();
5544 while (hardcount < max && scan < loceol &&
5545 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
5546 scan += UTF8SKIP(scan);
5550 while (scan < loceol && !isDIGIT(*scan))
5556 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
5562 LNBREAK can match two latin chars, which is ok,
5563 because we have a null terminated string, but we
5564 have to use hardcount in this situation
5566 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
5575 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
5580 while (scan < loceol && is_HORIZWS_latin1(scan))
5587 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
5588 scan += UTF8SKIP(scan);
5592 while (scan < loceol && !is_HORIZWS_latin1(scan))
5600 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
5605 while (scan < loceol && is_VERTWS_latin1(scan))
5613 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
5614 scan += UTF8SKIP(scan);
5618 while (scan < loceol && !is_VERTWS_latin1(scan))
5624 default: /* Called on something of 0 width. */
5625 break; /* So match right here or not at all. */
5631 c = scan - PL_reginput;
5635 GET_RE_DEBUG_FLAGS_DECL;
5637 SV * const prop = sv_newmortal();
5638 regprop(prog, prop, p);
5639 PerlIO_printf(Perl_debug_log,
5640 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
5641 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
5649 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
5651 - regclass_swash - prepare the utf8 swash
5655 Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
5661 RXi_GET_DECL(prog,progi);
5662 const struct reg_data * const data = prog ? progi->data : NULL;
5664 PERL_ARGS_ASSERT_REGCLASS_SWASH;
5666 if (data && data->count) {
5667 const U32 n = ARG(node);
5669 if (data->what[n] == 's') {
5670 SV * const rv = MUTABLE_SV(data->data[n]);
5671 AV * const av = MUTABLE_AV(SvRV(rv));
5672 SV **const ary = AvARRAY(av);
5675 /* See the end of regcomp.c:S_regclass() for
5676 * documentation of these array elements. */
5679 a = SvROK(ary[1]) ? &ary[1] : NULL;
5680 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
5684 else if (si && doinit) {
5685 sw = swash_init("utf8", "", si, 1, 0);
5686 (void)av_store(av, 1, sw);
5703 - reginclass - determine if a character falls into a character class
5705 The n is the ANYOF regnode, the p is the target string, lenp
5706 is pointer to the maximum length of how far to go in the p
5707 (if the lenp is zero, UTF8SKIP(p) is used),
5708 do_utf8 tells whether the target string is in UTF-8.
5713 S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
5716 const char flags = ANYOF_FLAGS(n);
5722 PERL_ARGS_ASSERT_REGINCLASS;
5724 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
5725 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
5726 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) | UTF8_CHECK_ONLY);
5727 /* see [perl #37836] for UTF8_ALLOW_ANYUV */
5728 if (len == (STRLEN)-1)
5729 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
5732 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
5733 if (do_utf8 || (flags & ANYOF_UNICODE)) {
5736 if (do_utf8 && !ANYOF_RUNTIME(n)) {
5737 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
5740 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
5744 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
5752 utf8_p = bytes_to_utf8(p, &len);
5754 if (swash_fetch(sw, utf8_p, 1))
5756 else if (flags & ANYOF_FOLD) {
5757 if (!match && lenp && av) {
5759 for (i = 0; i <= av_len(av); i++) {
5760 SV* const sv = *av_fetch(av, i, FALSE);
5762 const char * const s = SvPV_const(sv, len);
5763 if (len <= plen && memEQ(s, (char*)utf8_p, len)) {
5771 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
5774 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
5775 if (swash_fetch(sw, tmpbuf, 1))
5780 /* If we allocated a string above, free it */
5781 if (! do_utf8) Safefree(utf8_p);
5784 if (match && lenp && *lenp == 0)
5785 *lenp = UNISKIP(NATIVE_TO_UNI(c));
5787 if (!match && c < 256) {
5788 if (ANYOF_BITMAP_TEST(n, c))
5790 else if (flags & ANYOF_FOLD) {
5793 if (flags & ANYOF_LOCALE) {
5794 PL_reg_flags |= RF_tainted;
5795 f = PL_fold_locale[c];
5799 if (f != c && ANYOF_BITMAP_TEST(n, f))
5803 if (!match && (flags & ANYOF_CLASS)) {
5804 PL_reg_flags |= RF_tainted;
5806 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
5807 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
5808 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
5809 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
5810 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
5811 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
5812 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
5813 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
5814 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
5815 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
5816 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
5817 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
5818 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
5819 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
5820 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
5821 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
5822 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
5823 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
5824 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
5825 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
5826 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
5827 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
5828 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
5829 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
5830 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
5831 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
5832 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
5833 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
5834 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
5835 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
5836 ) /* How's that for a conditional? */
5843 return (flags & ANYOF_INVERT) ? !match : match;
5847 S_reghop3(U8 *s, I32 off, const U8* lim)
5851 PERL_ARGS_ASSERT_REGHOP3;
5854 while (off-- && s < lim) {
5855 /* XXX could check well-formedness here */
5860 while (off++ && s > lim) {
5862 if (UTF8_IS_CONTINUED(*s)) {
5863 while (s > lim && UTF8_IS_CONTINUATION(*s))
5866 /* XXX could check well-formedness here */
5873 /* there are a bunch of places where we use two reghop3's that should
5874 be replaced with this routine. but since thats not done yet
5875 we ifdef it out - dmq
5878 S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
5882 PERL_ARGS_ASSERT_REGHOP4;
5885 while (off-- && s < rlim) {
5886 /* XXX could check well-formedness here */
5891 while (off++ && s > llim) {
5893 if (UTF8_IS_CONTINUED(*s)) {
5894 while (s > llim && UTF8_IS_CONTINUATION(*s))
5897 /* XXX could check well-formedness here */
5905 S_reghopmaybe3(U8* s, I32 off, const U8* lim)
5909 PERL_ARGS_ASSERT_REGHOPMAYBE3;
5912 while (off-- && s < lim) {
5913 /* XXX could check well-formedness here */
5920 while (off++ && s > lim) {
5922 if (UTF8_IS_CONTINUED(*s)) {
5923 while (s > lim && UTF8_IS_CONTINUATION(*s))
5926 /* XXX could check well-formedness here */
5935 restore_pos(pTHX_ void *arg)
5938 regexp * const rex = (regexp *)arg;
5939 if (PL_reg_eval_set) {
5940 if (PL_reg_oldsaved) {
5941 rex->subbeg = PL_reg_oldsaved;
5942 rex->sublen = PL_reg_oldsavedlen;
5943 #ifdef PERL_OLD_COPY_ON_WRITE
5944 rex->saved_copy = PL_nrs;
5946 RXp_MATCH_COPIED_on(rex);
5948 PL_reg_magic->mg_len = PL_reg_oldpos;
5949 PL_reg_eval_set = 0;
5950 PL_curpm = PL_reg_oldcurpm;
5955 S_to_utf8_substr(pTHX_ register regexp *prog)
5959 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
5962 if (prog->substrs->data[i].substr
5963 && !prog->substrs->data[i].utf8_substr) {
5964 SV* const sv = newSVsv(prog->substrs->data[i].substr);
5965 prog->substrs->data[i].utf8_substr = sv;
5966 sv_utf8_upgrade(sv);
5967 if (SvVALID(prog->substrs->data[i].substr)) {
5968 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
5969 if (flags & FBMcf_TAIL) {
5970 /* Trim the trailing \n that fbm_compile added last
5972 SvCUR_set(sv, SvCUR(sv) - 1);
5973 /* Whilst this makes the SV technically "invalid" (as its
5974 buffer is no longer followed by "\0") when fbm_compile()
5975 adds the "\n" back, a "\0" is restored. */
5977 fbm_compile(sv, flags);
5979 if (prog->substrs->data[i].substr == prog->check_substr)
5980 prog->check_utf8 = sv;
5986 S_to_byte_substr(pTHX_ register regexp *prog)
5991 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
5994 if (prog->substrs->data[i].utf8_substr
5995 && !prog->substrs->data[i].substr) {
5996 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
5997 if (sv_utf8_downgrade(sv, TRUE)) {
5998 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6000 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6001 if (flags & FBMcf_TAIL) {
6002 /* Trim the trailing \n that fbm_compile added last
6004 SvCUR_set(sv, SvCUR(sv) - 1);
6006 fbm_compile(sv, flags);
6012 prog->substrs->data[i].substr = sv;
6013 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6014 prog->check_substr = sv;
6021 * c-indentation-style: bsd
6023 * indent-tabs-mode: t
6026 * ex: set ts=8 sts=4 sw=4 noet: