5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-2001, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGEXEC_C
85 #define RF_tainted 1 /* tainted information used? */
86 #define RF_warned 2 /* warned about big count? */
87 #define RF_evaled 4 /* Did an EVAL with setting? */
88 #define RF_utf8 8 /* String contains multibyte chars? */
90 #define UTF (PL_reg_flags & RF_utf8)
92 #define RS_init 1 /* eval environment created */
93 #define RS_set 2 /* replsv value is set */
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
107 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
108 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
110 #define HOPc(pos,off) ((char*)HOP(pos,off))
111 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
113 #define HOPBACK(pos, off) ( \
114 (PL_reg_match_utf8) \
115 ? reghopmaybe((U8*)pos, -off) \
116 : (pos - off >= PL_bostr) \
120 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
122 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
123 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
124 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
125 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
126 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
127 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
129 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
131 /* for use after a quantifier and before an EXACT-like node -- japhy */
132 #define JUMPABLE(rn) ( \
133 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
134 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
135 OP(rn) == PLUS || OP(rn) == MINMOD || \
136 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
139 #define HAS_TEXT(rn) ( \
140 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
143 #define FIND_NEXT_IMPT(rn) STMT_START { \
144 while (JUMPABLE(rn)) \
145 if (OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
146 PL_regkind[(U8)OP(rn)] == CURLY) \
147 rn = NEXTOPER(NEXTOPER(rn)); \
148 else if (OP(rn) == PLUS) \
150 else rn += NEXT_OFF(rn); \
153 static void restore_pos(pTHX_ void *arg);
156 S_regcppush(pTHX_ I32 parenfloor)
158 int retval = PL_savestack_ix;
159 #define REGCP_PAREN_ELEMS 4
160 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
163 if (paren_elems_to_push < 0)
164 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
166 #define REGCP_OTHER_ELEMS 6
167 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
168 for (p = PL_regsize; p > parenfloor; p--) {
169 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
170 SSPUSHINT(PL_regendp[p]);
171 SSPUSHINT(PL_regstartp[p]);
172 SSPUSHPTR(PL_reg_start_tmp[p]);
175 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
176 SSPUSHINT(PL_regsize);
177 SSPUSHINT(*PL_reglastparen);
178 SSPUSHINT(*PL_reglastcloseparen);
179 SSPUSHPTR(PL_reginput);
180 #define REGCP_FRAME_ELEMS 2
181 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
182 * are needed for the regexp context stack bookkeeping. */
183 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
184 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
189 /* These are needed since we do not localize EVAL nodes: */
190 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
191 " Setting an EVAL scope, savestack=%"IVdf"\n", \
192 (IV)PL_savestack_ix)); cp = PL_savestack_ix
194 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
195 PerlIO_printf(Perl_debug_log, \
196 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
197 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
207 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
209 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
210 i = SSPOPINT; /* Parentheses elements to pop. */
211 input = (char *) SSPOPPTR;
212 *PL_reglastcloseparen = SSPOPINT;
213 *PL_reglastparen = SSPOPINT;
214 PL_regsize = SSPOPINT;
216 /* Now restore the parentheses context. */
217 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
218 i > 0; i -= REGCP_PAREN_ELEMS) {
219 paren = (U32)SSPOPINT;
220 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
221 PL_regstartp[paren] = SSPOPINT;
223 if (paren <= *PL_reglastparen)
224 PL_regendp[paren] = tmps;
226 PerlIO_printf(Perl_debug_log,
227 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
228 (UV)paren, (IV)PL_regstartp[paren],
229 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
230 (IV)PL_regendp[paren],
231 (paren > *PL_reglastparen ? "(no)" : ""));
235 if (*PL_reglastparen + 1 <= PL_regnpar) {
236 PerlIO_printf(Perl_debug_log,
237 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
238 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
242 /* It would seem that the similar code in regtry()
243 * already takes care of this, and in fact it is in
244 * a better location to since this code can #if 0-ed out
245 * but the code in regtry() is needed or otherwise tests
246 * requiring null fields (pat.t#187 and split.t#{13,14}
247 * (as of patchlevel 7877) will fail. Then again,
248 * this code seems to be necessary or otherwise
249 * building DynaLoader will fail:
250 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
252 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
253 if (paren > PL_regsize)
254 PL_regstartp[paren] = -1;
255 PL_regendp[paren] = -1;
262 S_regcp_set_to(pTHX_ I32 ss)
264 I32 tmp = PL_savestack_ix;
266 PL_savestack_ix = ss;
268 PL_savestack_ix = tmp;
272 typedef struct re_cc_state
276 struct re_cc_state *prev;
281 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
283 #define TRYPAREN(paren, n, input) { \
286 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
287 PL_regendp[paren] = input - PL_bostr; \
290 PL_regendp[paren] = -1; \
292 if (regmatch(next)) \
295 PL_regendp[paren] = -1; \
300 * pregexec and friends
304 - pregexec - match a regexp against a string
307 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
308 char *strbeg, I32 minend, SV *screamer, U32 nosave)
309 /* strend: pointer to null at end of string */
310 /* strbeg: real beginning of string */
311 /* minend: end of match must be >=minend after stringarg. */
312 /* nosave: For optimizations. */
315 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
316 nosave ? 0 : REXEC_COPY_STR);
320 S_cache_re(pTHX_ regexp *prog)
322 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
324 PL_regprogram = prog->program;
326 PL_regnpar = prog->nparens;
327 PL_regdata = prog->data;
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(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.) */
359 /* A failure to find a constant substring means that there is no need to make
360 an expensive call to REx engine, thus we celebrate a failure. Similarly,
361 finding a substring too deep into the string means that less calls to
362 regtry() should be needed.
364 REx compiler's optimizer found 4 possible hints:
365 a) Anchored substring;
367 c) Whether we are anchored (beginning-of-line or \G);
368 d) First node (of those at offset 0) which may distingush positions;
369 We use a)b)d) and multiline-part of c), and try to find a position in the
370 string which does not contradict any of them.
373 /* Most of decisions we do here should have been done at compile time.
374 The nodes of the REx which we used for the search should have been
375 deleted from the finite automaton. */
378 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
379 char *strend, U32 flags, re_scream_pos_data *data)
381 register I32 start_shift = 0;
382 /* Should be nonnegative! */
383 register I32 end_shift = 0;
389 register char *other_last = Nullch; /* other substr checked before this */
390 char *check_at = Nullch; /* check substr found at this pos */
392 char *i_strpos = strpos;
393 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
396 if (prog->reganch & ROPT_UTF8) {
397 DEBUG_r(PerlIO_printf(Perl_debug_log,
398 "UTF-8 regex...\n"));
399 PL_reg_flags |= RF_utf8;
403 char *s = PL_reg_match_utf8 ?
404 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
406 int len = PL_reg_match_utf8 ?
407 strlen(s) : strend - strpos;
410 if (PL_reg_match_utf8)
411 DEBUG_r(PerlIO_printf(Perl_debug_log,
412 "UTF-8 target...\n"));
413 PerlIO_printf(Perl_debug_log,
414 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
415 PL_colors[4],PL_colors[5],PL_colors[0],
418 (strlen(prog->precomp) > 60 ? "..." : ""),
420 (int)(len > 60 ? 60 : len),
422 (len > 60 ? "..." : "")
426 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
427 DEBUG_r(PerlIO_printf(Perl_debug_log,
428 "String too short... [re_intuit_start]\n"));
431 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
433 check = prog->check_substr;
434 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
435 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
436 || ( (prog->reganch & ROPT_ANCH_BOL)
437 && !PL_multiline ) ); /* Check after \n? */
440 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
441 | ROPT_IMPLICIT)) /* not a real BOL */
442 /* SvCUR is not set on references: SvRV and SvPVX overlap */
444 && (strpos != strbeg)) {
445 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
448 if (prog->check_offset_min == prog->check_offset_max &&
449 !(prog->reganch & ROPT_CANY_SEEN)) {
450 /* Substring at constant offset from beg-of-str... */
453 s = HOP3c(strpos, prog->check_offset_min, strend);
455 slen = SvCUR(check); /* >= 1 */
457 if ( strend - s > slen || strend - s < slen - 1
458 || (strend - s == slen && strend[-1] != '\n')) {
459 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
462 /* Now should match s[0..slen-2] */
464 if (slen && (*SvPVX(check) != *s
466 && memNE(SvPVX(check), s, slen)))) {
468 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
472 else if (*SvPVX(check) != *s
473 || ((slen = SvCUR(check)) > 1
474 && memNE(SvPVX(check), s, slen)))
476 goto success_at_start;
479 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
481 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
482 end_shift = prog->minlen - start_shift -
483 CHR_SVLEN(check) + (SvTAIL(check) != 0);
485 I32 end = prog->check_offset_max + CHR_SVLEN(check)
486 - (SvTAIL(check) != 0);
487 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
489 if (end_shift < eshift)
493 else { /* Can match at random position */
496 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
497 /* Should be nonnegative! */
498 end_shift = prog->minlen - start_shift -
499 CHR_SVLEN(check) + (SvTAIL(check) != 0);
502 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
504 Perl_croak(aTHX_ "panic: end_shift");
508 /* Find a possible match in the region s..strend by looking for
509 the "check" substring in the region corrected by start/end_shift. */
510 if (flags & REXEC_SCREAM) {
511 I32 p = -1; /* Internal iterator of scream. */
512 I32 *pp = data ? data->scream_pos : &p;
514 if (PL_screamfirst[BmRARE(check)] >= 0
515 || ( BmRARE(check) == '\n'
516 && (BmPREVIOUS(check) == SvCUR(check) - 1)
518 s = screaminstr(sv, check,
519 start_shift + (s - strbeg), end_shift, pp, 0);
523 *data->scream_olds = s;
525 else if (prog->reganch & ROPT_CANY_SEEN)
526 s = fbm_instr((U8*)(s + start_shift),
527 (U8*)(strend - end_shift),
528 check, PL_multiline ? FBMrf_MULTILINE : 0);
530 s = fbm_instr(HOP3(s, start_shift, strend),
531 HOP3(strend, -end_shift, strbeg),
532 check, PL_multiline ? FBMrf_MULTILINE : 0);
534 /* Update the count-of-usability, remove useless subpatterns,
537 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
538 (s ? "Found" : "Did not find"),
539 ((check == prog->anchored_substr) ? "anchored" : "floating"),
541 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
543 PL_colors[1], (SvTAIL(check) ? "$" : ""),
544 (s ? " at offset " : "...\n") ) );
551 /* Finish the diagnostic message */
552 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
554 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
555 Start with the other substr.
556 XXXX no SCREAM optimization yet - and a very coarse implementation
557 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
558 *always* match. Probably should be marked during compile...
559 Probably it is right to do no SCREAM here...
562 if (prog->float_substr && prog->anchored_substr) {
563 /* Take into account the "other" substring. */
564 /* XXXX May be hopelessly wrong for UTF... */
567 if (check == prog->float_substr) {
570 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
573 t = s - prog->check_offset_max;
574 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
575 && (!(prog->reganch & ROPT_UTF8)
576 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
581 t = HOP3c(t, prog->anchored_offset, strend);
582 if (t < other_last) /* These positions already checked */
584 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
587 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
588 /* On end-of-str: see comment below. */
589 s = fbm_instr((unsigned char*)t,
590 HOP3(HOP3(last1, prog->anchored_offset, strend)
591 + SvCUR(prog->anchored_substr),
592 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
593 prog->anchored_substr,
594 PL_multiline ? FBMrf_MULTILINE : 0);
595 DEBUG_r(PerlIO_printf(Perl_debug_log,
596 "%s anchored substr `%s%.*s%s'%s",
597 (s ? "Found" : "Contradicts"),
599 (int)(SvCUR(prog->anchored_substr)
600 - (SvTAIL(prog->anchored_substr)!=0)),
601 SvPVX(prog->anchored_substr),
602 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
604 if (last1 >= last2) {
605 DEBUG_r(PerlIO_printf(Perl_debug_log,
606 ", giving up...\n"));
609 DEBUG_r(PerlIO_printf(Perl_debug_log,
610 ", trying floating at offset %ld...\n",
611 (long)(HOP3c(s1, 1, strend) - i_strpos)));
612 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
613 s = HOP3c(last, 1, strend);
617 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
618 (long)(s - i_strpos)));
619 t = HOP3c(s, -prog->anchored_offset, strbeg);
620 other_last = HOP3c(s, 1, strend);
628 else { /* Take into account the floating substring. */
632 t = HOP3c(s, -start_shift, strbeg);
634 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
635 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
636 last = HOP3c(t, prog->float_max_offset, strend);
637 s = HOP3c(t, prog->float_min_offset, strend);
640 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
641 /* fbm_instr() takes into account exact value of end-of-str
642 if the check is SvTAIL(ed). Since false positives are OK,
643 and end-of-str is not later than strend we are OK. */
644 s = fbm_instr((unsigned char*)s,
645 (unsigned char*)last + SvCUR(prog->float_substr)
646 - (SvTAIL(prog->float_substr)!=0),
647 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
648 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
649 (s ? "Found" : "Contradicts"),
651 (int)(SvCUR(prog->float_substr)
652 - (SvTAIL(prog->float_substr)!=0)),
653 SvPVX(prog->float_substr),
654 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
657 DEBUG_r(PerlIO_printf(Perl_debug_log,
658 ", giving up...\n"));
661 DEBUG_r(PerlIO_printf(Perl_debug_log,
662 ", trying anchored starting at offset %ld...\n",
663 (long)(s1 + 1 - i_strpos)));
665 s = HOP3c(t, 1, strend);
669 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
670 (long)(s - i_strpos)));
671 other_last = s; /* Fix this later. --Hugo */
680 t = s - prog->check_offset_max;
681 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
682 && (!(prog->reganch & ROPT_UTF8)
683 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
685 /* Fixed substring is found far enough so that the match
686 cannot start at strpos. */
688 if (ml_anch && t[-1] != '\n') {
689 /* Eventually fbm_*() should handle this, but often
690 anchored_offset is not 0, so this check will not be wasted. */
691 /* XXXX In the code below we prefer to look for "^" even in
692 presence of anchored substrings. And we search even
693 beyond the found float position. These pessimizations
694 are historical artefacts only. */
696 while (t < strend - prog->minlen) {
698 if (t < check_at - prog->check_offset_min) {
699 if (prog->anchored_substr) {
700 /* Since we moved from the found position,
701 we definitely contradict the found anchored
702 substr. Due to the above check we do not
703 contradict "check" substr.
704 Thus we can arrive here only if check substr
705 is float. Redo checking for "other"=="fixed".
708 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
709 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
710 goto do_other_anchored;
712 /* We don't contradict the found floating substring. */
713 /* XXXX Why not check for STCLASS? */
715 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
716 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
719 /* Position contradicts check-string */
720 /* XXXX probably better to look for check-string
721 than for "\n", so one should lower the limit for t? */
722 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
723 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
724 other_last = strpos = s = t + 1;
729 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
730 PL_colors[0],PL_colors[1]));
734 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
735 PL_colors[0],PL_colors[1]));
739 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
742 /* The found string does not prohibit matching at strpos,
743 - no optimization of calling REx engine can be performed,
744 unless it was an MBOL and we are not after MBOL,
745 or a future STCLASS check will fail this. */
747 /* Even in this situation we may use MBOL flag if strpos is offset
748 wrt the start of the string. */
749 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
750 && (strpos != strbeg) && strpos[-1] != '\n'
751 /* May be due to an implicit anchor of m{.*foo} */
752 && !(prog->reganch & ROPT_IMPLICIT))
757 DEBUG_r( if (ml_anch)
758 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
759 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
762 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
763 && prog->check_substr /* Could be deleted already */
764 && --BmUSEFUL(prog->check_substr) < 0
765 && prog->check_substr == prog->float_substr)
767 /* If flags & SOMETHING - do not do it many times on the same match */
768 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
769 SvREFCNT_dec(prog->check_substr);
770 prog->check_substr = Nullsv; /* disable */
771 prog->float_substr = Nullsv; /* clear */
772 check = Nullsv; /* abort */
774 /* XXXX This is a remnant of the old implementation. It
775 looks wasteful, since now INTUIT can use many
777 prog->reganch &= ~RE_USE_INTUIT;
784 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
785 if (prog->regstclass) {
786 /* minlen == 0 is possible if regstclass is \b or \B,
787 and the fixed substr is ''$.
788 Since minlen is already taken into account, s+1 is before strend;
789 accidentally, minlen >= 1 guaranties no false positives at s + 1
790 even for \b or \B. But (minlen? 1 : 0) below assumes that
791 regstclass does not come from lookahead... */
792 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
793 This leaves EXACTF only, which is dealt with in find_byclass(). */
794 U8* str = (U8*)STRING(prog->regstclass);
795 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
796 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
798 char *endpos = (prog->anchored_substr || ml_anch)
799 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
800 : (prog->float_substr
801 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
804 char *startpos = strbeg;
807 if (prog->reganch & ROPT_UTF8) {
808 PL_regdata = prog->data;
811 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
816 if (endpos == strend) {
817 DEBUG_r( PerlIO_printf(Perl_debug_log,
818 "Could not match STCLASS...\n") );
821 DEBUG_r( PerlIO_printf(Perl_debug_log,
822 "This position contradicts STCLASS...\n") );
823 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
825 /* Contradict one of substrings */
826 if (prog->anchored_substr) {
827 if (prog->anchored_substr == check) {
828 DEBUG_r( what = "anchored" );
830 s = HOP3c(t, 1, strend);
831 if (s + start_shift + end_shift > strend) {
832 /* XXXX Should be taken into account earlier? */
833 DEBUG_r( PerlIO_printf(Perl_debug_log,
834 "Could not match STCLASS...\n") );
839 DEBUG_r( PerlIO_printf(Perl_debug_log,
840 "Looking for %s substr starting at offset %ld...\n",
841 what, (long)(s + start_shift - i_strpos)) );
844 /* Have both, check_string is floating */
845 if (t + start_shift >= check_at) /* Contradicts floating=check */
846 goto retry_floating_check;
847 /* Recheck anchored substring, but not floating... */
851 DEBUG_r( PerlIO_printf(Perl_debug_log,
852 "Looking for anchored substr starting at offset %ld...\n",
853 (long)(other_last - i_strpos)) );
854 goto do_other_anchored;
856 /* Another way we could have checked stclass at the
857 current position only: */
862 DEBUG_r( PerlIO_printf(Perl_debug_log,
863 "Looking for /%s^%s/m starting at offset %ld...\n",
864 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
867 if (!prog->float_substr) /* Could have been deleted */
869 /* Check is floating subtring. */
870 retry_floating_check:
871 t = check_at - start_shift;
872 DEBUG_r( what = "floating" );
873 goto hop_and_restart;
876 DEBUG_r(PerlIO_printf(Perl_debug_log,
877 "By STCLASS: moving %ld --> %ld\n",
878 (long)(t - i_strpos), (long)(s - i_strpos))
882 DEBUG_r(PerlIO_printf(Perl_debug_log,
883 "Does not contradict STCLASS...\n");
888 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
889 PL_colors[4], (check ? "Guessed" : "Giving up"),
890 PL_colors[5], (long)(s - i_strpos)) );
893 fail_finish: /* Substring not found */
894 if (prog->check_substr) /* could be removed already */
895 BmUSEFUL(prog->check_substr) += 5; /* hooray */
897 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
898 PL_colors[4],PL_colors[5]));
902 /* We know what class REx starts with. Try to find this position... */
904 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
906 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
912 register I32 tmp = 1; /* Scratch variable? */
913 register bool do_utf8 = PL_reg_match_utf8;
915 /* We know what class it must start with. */
919 STRLEN skip = do_utf8 ? UTF8SKIP(s) : 1;
921 if (reginclass(c, (U8*)s, do_utf8) ||
922 (ANYOF_UNICODE_FOLD_SHARP_S(c, s, strend) &&
924 if (tmp && (norun || regtry(prog, s)))
936 if (tmp && (norun || regtry(prog, s)))
948 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
949 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
951 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
952 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
954 c1 = utf8_to_uvuni(tmpbuf1, 0);
955 c2 = utf8_to_uvuni(tmpbuf2, 0);
966 c2 = PL_fold_locale[c1];
968 e = do_utf8 ? s + ln : strend - ln;
971 e = s; /* Due to minlen logic of intuit() */
973 /* The idea in the EXACTF* cases is to first find the
974 * first character of the EXACTF* node and then, if
975 * necessary, case-insensitively compare the full
976 * text of the node. The c1 and c2 are the first
977 * characters (though in Unicode it gets a bit
978 * more complicated because there are more cases
979 * than just upper and lower: one is really supposed
980 * to use the so-called folding case for case-insensitive
981 * matching (called "loose matching" in Unicode). */
985 U8 tmpbuf [UTF8_MAXLEN+1];
986 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
991 c = utf8_to_uvchr((U8*)s, &len);
994 ibcmp_utf8(s, (char **)0, 0, do_utf8,
995 m, (char **)0, ln, UTF))
996 && (norun || regtry(prog, s)) )
999 uvchr_to_utf8(tmpbuf, c);
1000 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1002 && (f == c1 || f == c2)
1003 && (ln == foldlen ||
1004 !ibcmp_utf8((char *) foldbuf,
1005 (char **)0, foldlen, do_utf8,
1007 (char **)0, ln, UTF))
1008 && (norun || regtry(prog, s)) )
1016 c = utf8_to_uvchr((U8*)s, &len);
1018 /* Handle some of the three Greek sigmas cases.
1019 * Note that not all the possible combinations
1020 * are handled here: some of them are handled
1021 * by the standard folding rules, and some of
1022 * them (the character class or ANYOF cases)
1023 * are handled during compiletime in
1024 * regexec.c:S_regclass(). */
1025 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1026 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1027 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1029 if ( (c == c1 || c == c2)
1031 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1032 m, (char **)0, ln, UTF))
1033 && (norun || regtry(prog, s)) )
1036 uvchr_to_utf8(tmpbuf, c);
1037 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1039 && (f == c1 || f == c2)
1040 && (ln == foldlen ||
1041 !ibcmp_utf8((char *)foldbuf,
1042 (char **)0, foldlen, do_utf8,
1044 (char **)0, ln, UTF))
1045 && (norun || regtry(prog, s)) )
1056 && (ln == 1 || !(OP(c) == EXACTF
1058 : ibcmp_locale(s, m, ln)))
1059 && (norun || regtry(prog, s)) )
1065 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1066 && (ln == 1 || !(OP(c) == EXACTF
1068 : ibcmp_locale(s, m, ln)))
1069 && (norun || regtry(prog, s)) )
1076 PL_reg_flags |= RF_tainted;
1083 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1086 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1088 tmp = ((OP(c) == BOUND ?
1089 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1090 LOAD_UTF8_CHARCLASS(alnum,"a");
1091 while (s < strend) {
1092 if (tmp == !(OP(c) == BOUND ?
1093 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1094 isALNUM_LC_utf8((U8*)s)))
1097 if ((norun || regtry(prog, s)))
1104 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1105 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1106 while (s < strend) {
1108 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1110 if ((norun || regtry(prog, s)))
1116 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1120 PL_reg_flags |= RF_tainted;
1127 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1130 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1132 tmp = ((OP(c) == NBOUND ?
1133 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1134 LOAD_UTF8_CHARCLASS(alnum,"a");
1135 while (s < strend) {
1136 if (tmp == !(OP(c) == NBOUND ?
1137 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1138 isALNUM_LC_utf8((U8*)s)))
1140 else if ((norun || regtry(prog, s)))
1146 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1147 tmp = ((OP(c) == NBOUND ?
1148 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1149 while (s < strend) {
1151 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1153 else if ((norun || regtry(prog, s)))
1158 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1163 LOAD_UTF8_CHARCLASS(alnum,"a");
1164 while (s < strend) {
1165 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1166 if (tmp && (norun || regtry(prog, s)))
1177 while (s < strend) {
1179 if (tmp && (norun || regtry(prog, s)))
1191 PL_reg_flags |= RF_tainted;
1193 while (s < strend) {
1194 if (isALNUM_LC_utf8((U8*)s)) {
1195 if (tmp && (norun || regtry(prog, s)))
1206 while (s < strend) {
1207 if (isALNUM_LC(*s)) {
1208 if (tmp && (norun || regtry(prog, s)))
1221 LOAD_UTF8_CHARCLASS(alnum,"a");
1222 while (s < strend) {
1223 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1224 if (tmp && (norun || regtry(prog, s)))
1235 while (s < strend) {
1237 if (tmp && (norun || regtry(prog, s)))
1249 PL_reg_flags |= RF_tainted;
1251 while (s < strend) {
1252 if (!isALNUM_LC_utf8((U8*)s)) {
1253 if (tmp && (norun || regtry(prog, s)))
1264 while (s < strend) {
1265 if (!isALNUM_LC(*s)) {
1266 if (tmp && (norun || regtry(prog, s)))
1279 LOAD_UTF8_CHARCLASS(space," ");
1280 while (s < strend) {
1281 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1282 if (tmp && (norun || regtry(prog, s)))
1293 while (s < strend) {
1295 if (tmp && (norun || regtry(prog, s)))
1307 PL_reg_flags |= RF_tainted;
1309 while (s < strend) {
1310 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1311 if (tmp && (norun || regtry(prog, s)))
1322 while (s < strend) {
1323 if (isSPACE_LC(*s)) {
1324 if (tmp && (norun || regtry(prog, s)))
1337 LOAD_UTF8_CHARCLASS(space," ");
1338 while (s < strend) {
1339 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1340 if (tmp && (norun || regtry(prog, s)))
1351 while (s < strend) {
1353 if (tmp && (norun || regtry(prog, s)))
1365 PL_reg_flags |= RF_tainted;
1367 while (s < strend) {
1368 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1369 if (tmp && (norun || regtry(prog, s)))
1380 while (s < strend) {
1381 if (!isSPACE_LC(*s)) {
1382 if (tmp && (norun || regtry(prog, s)))
1395 LOAD_UTF8_CHARCLASS(digit,"0");
1396 while (s < strend) {
1397 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1398 if (tmp && (norun || regtry(prog, s)))
1409 while (s < strend) {
1411 if (tmp && (norun || regtry(prog, s)))
1423 PL_reg_flags |= RF_tainted;
1425 while (s < strend) {
1426 if (isDIGIT_LC_utf8((U8*)s)) {
1427 if (tmp && (norun || regtry(prog, s)))
1438 while (s < strend) {
1439 if (isDIGIT_LC(*s)) {
1440 if (tmp && (norun || regtry(prog, s)))
1453 LOAD_UTF8_CHARCLASS(digit,"0");
1454 while (s < strend) {
1455 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1456 if (tmp && (norun || regtry(prog, s)))
1467 while (s < strend) {
1469 if (tmp && (norun || regtry(prog, s)))
1481 PL_reg_flags |= RF_tainted;
1483 while (s < strend) {
1484 if (!isDIGIT_LC_utf8((U8*)s)) {
1485 if (tmp && (norun || regtry(prog, s)))
1496 while (s < strend) {
1497 if (!isDIGIT_LC(*s)) {
1498 if (tmp && (norun || regtry(prog, s)))
1510 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1519 - regexec_flags - match a regexp against a string
1522 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1523 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1524 /* strend: pointer to null at end of string */
1525 /* strbeg: real beginning of string */
1526 /* minend: end of match must be >=minend after stringarg. */
1527 /* data: May be used for some additional optimizations. */
1528 /* nosave: For optimizations. */
1531 register regnode *c;
1532 register char *startpos = stringarg;
1533 I32 minlen; /* must match at least this many chars */
1534 I32 dontbother = 0; /* how many characters not to try at end */
1535 /* I32 start_shift = 0; */ /* Offset of the start to find
1536 constant substr. */ /* CC */
1537 I32 end_shift = 0; /* Same for the end. */ /* CC */
1538 I32 scream_pos = -1; /* Internal iterator of scream. */
1540 SV* oreplsv = GvSV(PL_replgv);
1541 bool do_utf8 = DO_UTF8(sv);
1543 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1544 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1551 PL_regnarrate = DEBUG_r_TEST;
1554 /* Be paranoid... */
1555 if (prog == NULL || startpos == NULL) {
1556 Perl_croak(aTHX_ "NULL regexp parameter");
1560 minlen = prog->minlen;
1561 if (strend - startpos < minlen) {
1562 DEBUG_r(PerlIO_printf(Perl_debug_log,
1563 "String too short [regexec_flags]...\n"));
1567 /* Check validity of program. */
1568 if (UCHARAT(prog->program) != REG_MAGIC) {
1569 Perl_croak(aTHX_ "corrupted regexp program");
1573 PL_reg_eval_set = 0;
1576 if (prog->reganch & ROPT_UTF8)
1577 PL_reg_flags |= RF_utf8;
1579 /* Mark beginning of line for ^ and lookbehind. */
1580 PL_regbol = startpos;
1584 /* Mark end of line for $ (and such) */
1587 /* see how far we have to get to not match where we matched before */
1588 PL_regtill = startpos+minend;
1590 /* We start without call_cc context. */
1593 /* If there is a "must appear" string, look for it. */
1596 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1599 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1600 PL_reg_ganch = startpos;
1601 else if (sv && SvTYPE(sv) >= SVt_PVMG
1603 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1604 && mg->mg_len >= 0) {
1605 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1606 if (prog->reganch & ROPT_ANCH_GPOS) {
1607 if (s > PL_reg_ganch)
1612 else /* pos() not defined */
1613 PL_reg_ganch = strbeg;
1616 if (do_utf8 == (UTF!=0) &&
1617 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1618 re_scream_pos_data d;
1620 d.scream_olds = &scream_olds;
1621 d.scream_pos = &scream_pos;
1622 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1624 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1625 goto phooey; /* not present */
1631 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1632 UNI_DISPLAY_REGEX) :
1634 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1635 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1636 UNI_DISPLAY_REGEX) : startpos;
1637 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1640 PerlIO_printf(Perl_debug_log,
1641 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1642 PL_colors[4],PL_colors[5],PL_colors[0],
1645 len0 > 60 ? "..." : "",
1647 (int)(len1 > 60 ? 60 : len1),
1649 (len1 > 60 ? "..." : "")
1653 /* Simplest case: anchored match need be tried only once. */
1654 /* [unless only anchor is BOL and multiline is set] */
1655 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1656 if (s == startpos && regtry(prog, startpos))
1658 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1659 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1664 dontbother = minlen - 1;
1665 end = HOP3c(strend, -dontbother, strbeg) - 1;
1666 /* for multiline we only have to try after newlines */
1667 if (prog->check_substr) {
1671 if (regtry(prog, s))
1676 if (prog->reganch & RE_USE_INTUIT) {
1677 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1688 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1689 if (regtry(prog, s))
1696 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1697 if (regtry(prog, PL_reg_ganch))
1702 /* Messy cases: unanchored match. */
1703 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1704 /* we have /x+whatever/ */
1705 /* it must be a one character string (XXXX Except UTF?) */
1706 char ch = SvPVX(prog->anchored_substr)[0];
1712 while (s < strend) {
1714 DEBUG_r( did_match = 1 );
1715 if (regtry(prog, s)) goto got_it;
1717 while (s < strend && *s == ch)
1724 while (s < strend) {
1726 DEBUG_r( did_match = 1 );
1727 if (regtry(prog, s)) goto got_it;
1729 while (s < strend && *s == ch)
1735 DEBUG_r(if (!did_match)
1736 PerlIO_printf(Perl_debug_log,
1737 "Did not find anchored character...\n")
1741 else if (do_utf8 == (UTF!=0) &&
1742 (prog->anchored_substr != Nullsv
1743 || (prog->float_substr != Nullsv
1744 && prog->float_max_offset < strend - s))) {
1745 SV *must = prog->anchored_substr
1746 ? prog->anchored_substr : prog->float_substr;
1748 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1750 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1751 char *last = HOP3c(strend, /* Cannot start after this */
1752 -(I32)(CHR_SVLEN(must)
1753 - (SvTAIL(must) != 0) + back_min), strbeg);
1754 char *last1; /* Last position checked before */
1760 last1 = HOPc(s, -1);
1762 last1 = s - 1; /* bogus */
1764 /* XXXX check_substr already used to find `s', can optimize if
1765 check_substr==must. */
1767 dontbother = end_shift;
1768 strend = HOPc(strend, -dontbother);
1769 while ( (s <= last) &&
1770 ((flags & REXEC_SCREAM)
1771 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1772 end_shift, &scream_pos, 0))
1773 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1774 (unsigned char*)strend, must,
1775 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1776 DEBUG_r( did_match = 1 );
1777 if (HOPc(s, -back_max) > last1) {
1778 last1 = HOPc(s, -back_min);
1779 s = HOPc(s, -back_max);
1782 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1784 last1 = HOPc(s, -back_min);
1788 while (s <= last1) {
1789 if (regtry(prog, s))
1795 while (s <= last1) {
1796 if (regtry(prog, s))
1802 DEBUG_r(if (!did_match)
1803 PerlIO_printf(Perl_debug_log,
1804 "Did not find %s substr `%s%.*s%s'%s...\n",
1805 ((must == prog->anchored_substr)
1806 ? "anchored" : "floating"),
1808 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1810 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1814 else if ((c = prog->regstclass)) {
1815 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1816 /* don't bother with what can't match */
1817 strend = HOPc(strend, -(minlen - 1));
1819 SV *prop = sv_newmortal();
1827 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1828 UNI_DISPLAY_REGEX) :
1830 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1832 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1833 len1 = UTF ? SvCUR(dsv1) : strend - s;
1834 PerlIO_printf(Perl_debug_log,
1835 "Matching stclass `%*.*s' against `%*.*s'\n",
1839 if (find_byclass(prog, c, s, strend, startpos, 0))
1841 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1845 if (prog->float_substr != Nullsv) { /* Trim the end. */
1848 if (flags & REXEC_SCREAM) {
1849 last = screaminstr(sv, prog->float_substr, s - strbeg,
1850 end_shift, &scream_pos, 1); /* last one */
1852 last = scream_olds; /* Only one occurrence. */
1856 char *little = SvPV(prog->float_substr, len);
1858 if (SvTAIL(prog->float_substr)) {
1859 if (memEQ(strend - len + 1, little, len - 1))
1860 last = strend - len + 1;
1861 else if (!PL_multiline)
1862 last = memEQ(strend - len, little, len)
1863 ? strend - len : Nullch;
1869 last = rninstr(s, strend, little, little + len);
1871 last = strend; /* matching `$' */
1875 DEBUG_r(PerlIO_printf(Perl_debug_log,
1876 "%sCan't trim the tail, match fails (should not happen)%s\n",
1877 PL_colors[4],PL_colors[5]));
1878 goto phooey; /* Should not happen! */
1880 dontbother = strend - last + prog->float_min_offset;
1882 if (minlen && (dontbother < minlen))
1883 dontbother = minlen - 1;
1884 strend -= dontbother; /* this one's always in bytes! */
1885 /* We don't know much -- general case. */
1888 if (regtry(prog, s))
1897 if (regtry(prog, s))
1899 } while (s++ < strend);
1907 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1909 if (PL_reg_eval_set) {
1910 /* Preserve the current value of $^R */
1911 if (oreplsv != GvSV(PL_replgv))
1912 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1913 restored, the value remains
1915 restore_pos(aTHX_ 0);
1918 /* make sure $`, $&, $', and $digit will work later */
1919 if ( !(flags & REXEC_NOT_FIRST) ) {
1920 if (RX_MATCH_COPIED(prog)) {
1921 Safefree(prog->subbeg);
1922 RX_MATCH_COPIED_off(prog);
1924 if (flags & REXEC_COPY_STR) {
1925 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1927 s = savepvn(strbeg, i);
1930 RX_MATCH_COPIED_on(prog);
1933 prog->subbeg = strbeg;
1934 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1941 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1942 PL_colors[4],PL_colors[5]));
1943 if (PL_reg_eval_set)
1944 restore_pos(aTHX_ 0);
1949 - regtry - try match at specific point
1951 STATIC I32 /* 0 failure, 1 success */
1952 S_regtry(pTHX_ regexp *prog, char *startpos)
1960 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1962 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1965 PL_reg_eval_set = RS_init;
1967 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1968 (IV)(PL_stack_sp - PL_stack_base));
1970 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1971 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1972 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1974 /* Apparently this is not needed, judging by wantarray. */
1975 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1976 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1979 /* Make $_ available to executed code. */
1980 if (PL_reg_sv != DEFSV) {
1981 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1986 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1987 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1988 /* prepare for quick setting of pos */
1989 sv_magic(PL_reg_sv, (SV*)0,
1990 PERL_MAGIC_regex_global, Nullch, 0);
1991 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1995 PL_reg_oldpos = mg->mg_len;
1996 SAVEDESTRUCTOR_X(restore_pos, 0);
1998 if (!PL_reg_curpm) {
1999 Newz(22,PL_reg_curpm, 1, PMOP);
2002 SV* repointer = newSViv(0);
2003 /* so we know which PL_regex_padav element is PL_reg_curpm */
2004 SvFLAGS(repointer) |= SVf_BREAK;
2005 av_push(PL_regex_padav,repointer);
2006 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2007 PL_regex_pad = AvARRAY(PL_regex_padav);
2011 PM_SETRE(PL_reg_curpm, prog);
2012 PL_reg_oldcurpm = PL_curpm;
2013 PL_curpm = PL_reg_curpm;
2014 if (RX_MATCH_COPIED(prog)) {
2015 /* Here is a serious problem: we cannot rewrite subbeg,
2016 since it may be needed if this match fails. Thus
2017 $` inside (?{}) could fail... */
2018 PL_reg_oldsaved = prog->subbeg;
2019 PL_reg_oldsavedlen = prog->sublen;
2020 RX_MATCH_COPIED_off(prog);
2023 PL_reg_oldsaved = Nullch;
2024 prog->subbeg = PL_bostr;
2025 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2027 prog->startp[0] = startpos - PL_bostr;
2028 PL_reginput = startpos;
2029 PL_regstartp = prog->startp;
2030 PL_regendp = prog->endp;
2031 PL_reglastparen = &prog->lastparen;
2032 PL_reglastcloseparen = &prog->lastcloseparen;
2033 prog->lastparen = 0;
2035 DEBUG_r(PL_reg_starttry = startpos);
2036 if (PL_reg_start_tmpl <= prog->nparens) {
2037 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2038 if(PL_reg_start_tmp)
2039 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2041 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2045 sv_setpvn(PERL_DEBUG_PAD(0), "", 0);
2046 sv_setpvn(PERL_DEBUG_PAD(1), "", 0);
2047 sv_setpvn(PERL_DEBUG_PAD(2), "", 0);
2050 /* XXXX What this code is doing here?!!! There should be no need
2051 to do this again and again, PL_reglastparen should take care of
2054 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2055 * Actually, the code in regcppop() (which Ilya may be meaning by
2056 * PL_reglastparen), is not needed at all by the test suite
2057 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2058 * enough, for building DynaLoader, or otherwise this
2059 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2060 * will happen. Meanwhile, this code *is* needed for the
2061 * above-mentioned test suite tests to succeed. The common theme
2062 * on those tests seems to be returning null fields from matches.
2067 if (prog->nparens) {
2068 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2075 if (regmatch(prog->program + 1)) {
2076 prog->endp[0] = PL_reginput - PL_bostr;
2079 REGCP_UNWIND(lastcp);
2083 #define RE_UNWIND_BRANCH 1
2084 #define RE_UNWIND_BRANCHJ 2
2088 typedef struct { /* XX: makes sense to enlarge it... */
2092 } re_unwind_generic_t;
2105 } re_unwind_branch_t;
2107 typedef union re_unwind_t {
2109 re_unwind_generic_t generic;
2110 re_unwind_branch_t branch;
2113 #define sayYES goto yes
2114 #define sayNO goto no
2115 #define sayNO_ANYOF goto no_anyof
2116 #define sayYES_FINAL goto yes_final
2117 #define sayYES_LOUD goto yes_loud
2118 #define sayNO_FINAL goto no_final
2119 #define sayNO_SILENT goto do_no
2120 #define saySAME(x) if (x) goto yes; else goto no
2122 #define REPORT_CODE_OFF 24
2125 - regmatch - main matching routine
2127 * Conceptually the strategy is simple: check to see whether the current
2128 * node matches, call self recursively to see whether the rest matches,
2129 * and then act accordingly. In practice we make some effort to avoid
2130 * recursion, in particular by going through "ordinary" nodes (that don't
2131 * need to know whether the rest of the match failed) by a loop instead of
2134 /* [lwall] I've hoisted the register declarations to the outer block in order to
2135 * maybe save a little bit of pushing and popping on the stack. It also takes
2136 * advantage of machines that use a register save mask on subroutine entry.
2138 STATIC I32 /* 0 failure, 1 success */
2139 S_regmatch(pTHX_ regnode *prog)
2141 register regnode *scan; /* Current node. */
2142 regnode *next; /* Next node. */
2143 regnode *inner; /* Next node in internal branch. */
2144 register I32 nextchr; /* renamed nextchr - nextchar colides with
2145 function of same name */
2146 register I32 n; /* no or next */
2147 register I32 ln = 0; /* len or last */
2148 register char *s = Nullch; /* operand or save */
2149 register char *locinput = PL_reginput;
2150 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2151 int minmod = 0, sw = 0, logical = 0;
2154 I32 firstcp = PL_savestack_ix;
2156 register bool do_utf8 = PL_reg_match_utf8;
2158 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2159 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2160 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2167 /* Note that nextchr is a byte even in UTF */
2168 nextchr = UCHARAT(locinput);
2170 while (scan != NULL) {
2173 SV *prop = sv_newmortal();
2174 int docolor = *PL_colors[0];
2175 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2176 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2177 /* The part of the string before starttry has one color
2178 (pref0_len chars), between starttry and current
2179 position another one (pref_len - pref0_len chars),
2180 after the current position the third one.
2181 We assume that pref0_len <= pref_len, otherwise we
2182 decrease pref0_len. */
2183 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2184 ? (5 + taill) - l : locinput - PL_bostr;
2187 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2189 pref0_len = pref_len - (locinput - PL_reg_starttry);
2190 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2191 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2192 ? (5 + taill) - pref_len : PL_regeol - locinput);
2193 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2197 if (pref0_len > pref_len)
2198 pref0_len = pref_len;
2199 regprop(prop, scan);
2203 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2204 pref0_len, 60, UNI_DISPLAY_REGEX) :
2205 locinput - pref_len;
2206 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2207 char *s1 = do_utf8 ?
2208 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2209 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2210 locinput - pref_len + pref0_len;
2211 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2212 char *s2 = do_utf8 ?
2213 pv_uni_display(dsv2, (U8*)locinput,
2214 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2216 int len2 = do_utf8 ? strlen(s2) : l;
2217 PerlIO_printf(Perl_debug_log,
2218 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2219 (IV)(locinput - PL_bostr),
2226 (docolor ? "" : "> <"),
2230 15 - l - pref_len + 1,
2232 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2237 next = scan + NEXT_OFF(scan);
2243 if (locinput == PL_bostr || (PL_multiline &&
2244 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2246 /* regtill = regbol; */
2251 if (locinput == PL_bostr ||
2252 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2258 if (locinput == PL_bostr)
2262 if (locinput == PL_reg_ganch)
2272 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2277 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2279 if (PL_regeol - locinput > 1)
2283 if (PL_regeol != locinput)
2287 if (!nextchr && locinput >= PL_regeol)
2290 locinput += PL_utf8skip[nextchr];
2291 if (locinput > PL_regeol)
2293 nextchr = UCHARAT(locinput);
2296 nextchr = UCHARAT(++locinput);
2299 if (!nextchr && locinput >= PL_regeol)
2301 nextchr = UCHARAT(++locinput);
2304 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2307 locinput += PL_utf8skip[nextchr];
2308 if (locinput > PL_regeol)
2310 nextchr = UCHARAT(locinput);
2313 nextchr = UCHARAT(++locinput);
2318 if (do_utf8 != (UTF!=0)) {
2319 /* The target and the pattern have differing utf8ness. */
2325 /* The target is utf8, the pattern is not utf8. */
2329 if (NATIVE_TO_UNI(*(U8*)s) !=
2330 utf8_to_uvchr((U8*)l, &ulen))
2337 /* The target is not utf8, the pattern is utf8. */
2341 if (NATIVE_TO_UNI(*((U8*)l)) !=
2342 utf8_to_uvchr((U8*)s, &ulen))
2349 nextchr = UCHARAT(locinput);
2352 /* The target and the pattern have the same utf8ness. */
2353 /* Inline the first character, for speed. */
2354 if (UCHARAT(s) != nextchr)
2356 if (PL_regeol - locinput < ln)
2358 if (ln > 1 && memNE(s, locinput, ln))
2361 nextchr = UCHARAT(locinput);
2364 PL_reg_flags |= RF_tainted;
2370 if (do_utf8 || UTF) {
2371 /* Either target or the pattern are utf8. */
2373 char *e = PL_regeol;
2375 if (ibcmp_utf8(s, 0, ln, do_utf8,
2377 /* One more case for the sharp s:
2378 * pack("U0U*", 0xDF) =~ /ss/i,
2379 * the 0xC3 0x9F are the UTF-8
2380 * byte sequence for the U+00DF. */
2382 toLOWER(s[0]) == 's' &&
2384 toLOWER(s[1]) == 's' &&
2391 nextchr = UCHARAT(locinput);
2395 /* Neither the target and the pattern are utf8. */
2397 /* Inline the first character, for speed. */
2398 if (UCHARAT(s) != nextchr &&
2399 UCHARAT(s) != ((OP(scan) == EXACTF)
2400 ? PL_fold : PL_fold_locale)[nextchr])
2402 if (PL_regeol - locinput < ln)
2404 if (ln > 1 && (OP(scan) == EXACTF
2405 ? ibcmp(s, locinput, ln)
2406 : ibcmp_locale(s, locinput, ln)))
2409 nextchr = UCHARAT(locinput);
2413 STRLEN inclasslen = PL_regeol - locinput;
2415 if (!reginclasslen(scan, (U8*)locinput, &inclasslen, do_utf8))
2417 if (locinput >= PL_regeol)
2419 locinput += inclasslen;
2420 nextchr = UCHARAT(locinput);
2425 nextchr = UCHARAT(locinput);
2426 if (!reginclass(scan, (U8*)locinput, do_utf8))
2428 if (!nextchr && locinput >= PL_regeol)
2430 nextchr = UCHARAT(++locinput);
2434 /* If we might have the case of the German sharp s
2435 * in a casefolding Unicode character class. */
2437 if (ANYOF_UNICODE_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2439 nextchr = UCHARAT(locinput);
2445 PL_reg_flags |= RF_tainted;
2451 LOAD_UTF8_CHARCLASS(alnum,"a");
2452 if (!(OP(scan) == ALNUM
2453 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2454 : isALNUM_LC_utf8((U8*)locinput)))
2458 locinput += PL_utf8skip[nextchr];
2459 nextchr = UCHARAT(locinput);
2462 if (!(OP(scan) == ALNUM
2463 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2465 nextchr = UCHARAT(++locinput);
2468 PL_reg_flags |= RF_tainted;
2471 if (!nextchr && locinput >= PL_regeol)
2474 LOAD_UTF8_CHARCLASS(alnum,"a");
2475 if (OP(scan) == NALNUM
2476 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2477 : isALNUM_LC_utf8((U8*)locinput))
2481 locinput += PL_utf8skip[nextchr];
2482 nextchr = UCHARAT(locinput);
2485 if (OP(scan) == NALNUM
2486 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2488 nextchr = UCHARAT(++locinput);
2492 PL_reg_flags |= RF_tainted;
2496 /* was last char in word? */
2498 if (locinput == PL_bostr)
2501 U8 *r = reghop((U8*)locinput, -1);
2503 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2505 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2506 ln = isALNUM_uni(ln);
2507 LOAD_UTF8_CHARCLASS(alnum,"a");
2508 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2511 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2512 n = isALNUM_LC_utf8((U8*)locinput);
2516 ln = (locinput != PL_bostr) ?
2517 UCHARAT(locinput - 1) : '\n';
2518 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2520 n = isALNUM(nextchr);
2523 ln = isALNUM_LC(ln);
2524 n = isALNUM_LC(nextchr);
2527 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2528 OP(scan) == BOUNDL))
2532 PL_reg_flags |= RF_tainted;
2538 if (UTF8_IS_CONTINUED(nextchr)) {
2539 LOAD_UTF8_CHARCLASS(space," ");
2540 if (!(OP(scan) == SPACE
2541 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2542 : isSPACE_LC_utf8((U8*)locinput)))
2546 locinput += PL_utf8skip[nextchr];
2547 nextchr = UCHARAT(locinput);
2550 if (!(OP(scan) == SPACE
2551 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2553 nextchr = UCHARAT(++locinput);
2556 if (!(OP(scan) == SPACE
2557 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2559 nextchr = UCHARAT(++locinput);
2563 PL_reg_flags |= RF_tainted;
2566 if (!nextchr && locinput >= PL_regeol)
2569 LOAD_UTF8_CHARCLASS(space," ");
2570 if (OP(scan) == NSPACE
2571 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2572 : isSPACE_LC_utf8((U8*)locinput))
2576 locinput += PL_utf8skip[nextchr];
2577 nextchr = UCHARAT(locinput);
2580 if (OP(scan) == NSPACE
2581 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2583 nextchr = UCHARAT(++locinput);
2586 PL_reg_flags |= RF_tainted;
2592 LOAD_UTF8_CHARCLASS(digit,"0");
2593 if (!(OP(scan) == DIGIT
2594 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2595 : isDIGIT_LC_utf8((U8*)locinput)))
2599 locinput += PL_utf8skip[nextchr];
2600 nextchr = UCHARAT(locinput);
2603 if (!(OP(scan) == DIGIT
2604 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2606 nextchr = UCHARAT(++locinput);
2609 PL_reg_flags |= RF_tainted;
2612 if (!nextchr && locinput >= PL_regeol)
2615 LOAD_UTF8_CHARCLASS(digit,"0");
2616 if (OP(scan) == NDIGIT
2617 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2618 : isDIGIT_LC_utf8((U8*)locinput))
2622 locinput += PL_utf8skip[nextchr];
2623 nextchr = UCHARAT(locinput);
2626 if (OP(scan) == NDIGIT
2627 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2629 nextchr = UCHARAT(++locinput);
2632 if (locinput >= PL_regeol)
2635 LOAD_UTF8_CHARCLASS(mark,"~");
2636 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2638 locinput += PL_utf8skip[nextchr];
2639 while (locinput < PL_regeol &&
2640 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2641 locinput += UTF8SKIP(locinput);
2642 if (locinput > PL_regeol)
2647 nextchr = UCHARAT(locinput);
2650 PL_reg_flags |= RF_tainted;
2654 n = ARG(scan); /* which paren pair */
2655 ln = PL_regstartp[n];
2656 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2657 if (*PL_reglastparen < n || ln == -1)
2658 sayNO; /* Do not match unless seen CLOSEn. */
2659 if (ln == PL_regendp[n])
2663 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2665 char *e = PL_bostr + PL_regendp[n];
2667 * Note that we can't do the "other character" lookup trick as
2668 * in the 8-bit case (no pun intended) because in Unicode we
2669 * have to map both upper and title case to lower case.
2671 if (OP(scan) == REFF) {
2672 STRLEN ulen1, ulen2;
2673 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2674 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2678 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2679 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2680 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2687 nextchr = UCHARAT(locinput);
2691 /* Inline the first character, for speed. */
2692 if (UCHARAT(s) != nextchr &&
2694 (UCHARAT(s) != ((OP(scan) == REFF
2695 ? PL_fold : PL_fold_locale)[nextchr]))))
2697 ln = PL_regendp[n] - ln;
2698 if (locinput + ln > PL_regeol)
2700 if (ln > 1 && (OP(scan) == REF
2701 ? memNE(s, locinput, ln)
2703 ? ibcmp(s, locinput, ln)
2704 : ibcmp_locale(s, locinput, ln))))
2707 nextchr = UCHARAT(locinput);
2718 OP_4tree *oop = PL_op;
2719 COP *ocurcop = PL_curcop;
2720 SV **ocurpad = PL_curpad;
2724 PL_op = (OP_4tree*)PL_regdata->data[n];
2725 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2726 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2727 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2731 CALLRUNOPS(aTHX); /* Scalar context. */
2734 ret = Nullsv; /* protect against empty (?{}) blocks. */
2742 PL_curpad = ocurpad;
2743 PL_curcop = ocurcop;
2745 if (logical == 2) { /* Postponed subexpression. */
2747 MAGIC *mg = Null(MAGIC*);
2749 CHECKPOINT cp, lastcp;
2751 if(SvROK(ret) || SvRMAGICAL(ret)) {
2752 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2755 mg = mg_find(sv, PERL_MAGIC_qr);
2758 re = (regexp *)mg->mg_obj;
2759 (void)ReREFCNT_inc(re);
2763 char *t = SvPV(ret, len);
2765 char *oprecomp = PL_regprecomp;
2766 I32 osize = PL_regsize;
2767 I32 onpar = PL_regnpar;
2770 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2772 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2773 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2775 PL_regprecomp = oprecomp;
2780 PerlIO_printf(Perl_debug_log,
2781 "Entering embedded `%s%.60s%s%s'\n",
2785 (strlen(re->precomp) > 60 ? "..." : ""))
2788 state.prev = PL_reg_call_cc;
2789 state.cc = PL_regcc;
2790 state.re = PL_reg_re;
2794 cp = regcppush(0); /* Save *all* the positions. */
2797 state.ss = PL_savestack_ix;
2798 *PL_reglastparen = 0;
2799 *PL_reglastcloseparen = 0;
2800 PL_reg_call_cc = &state;
2801 PL_reginput = locinput;
2803 /* XXXX This is too dramatic a measure... */
2806 if (regmatch(re->program + 1)) {
2807 /* Even though we succeeded, we need to restore
2808 global variables, since we may be wrapped inside
2809 SUSPEND, thus the match may be not finished yet. */
2811 /* XXXX Do this only if SUSPENDed? */
2812 PL_reg_call_cc = state.prev;
2813 PL_regcc = state.cc;
2814 PL_reg_re = state.re;
2815 cache_re(PL_reg_re);
2817 /* XXXX This is too dramatic a measure... */
2820 /* These are needed even if not SUSPEND. */
2826 REGCP_UNWIND(lastcp);
2828 PL_reg_call_cc = state.prev;
2829 PL_regcc = state.cc;
2830 PL_reg_re = state.re;
2831 cache_re(PL_reg_re);
2833 /* XXXX This is too dramatic a measure... */
2843 sv_setsv(save_scalar(PL_replgv), ret);
2847 n = ARG(scan); /* which paren pair */
2848 PL_reg_start_tmp[n] = locinput;
2853 n = ARG(scan); /* which paren pair */
2854 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2855 PL_regendp[n] = locinput - PL_bostr;
2856 if (n > *PL_reglastparen)
2857 *PL_reglastparen = n;
2858 *PL_reglastcloseparen = n;
2861 n = ARG(scan); /* which paren pair */
2862 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2865 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2867 next = NEXTOPER(NEXTOPER(scan));
2869 next = scan + ARG(scan);
2870 if (OP(next) == IFTHEN) /* Fake one. */
2871 next = NEXTOPER(NEXTOPER(next));
2875 logical = scan->flags;
2877 /*******************************************************************
2878 PL_regcc contains infoblock about the innermost (...)* loop, and
2879 a pointer to the next outer infoblock.
2881 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2883 1) After matching X, regnode for CURLYX is processed;
2885 2) This regnode creates infoblock on the stack, and calls
2886 regmatch() recursively with the starting point at WHILEM node;
2888 3) Each hit of WHILEM node tries to match A and Z (in the order
2889 depending on the current iteration, min/max of {min,max} and
2890 greediness). The information about where are nodes for "A"
2891 and "Z" is read from the infoblock, as is info on how many times "A"
2892 was already matched, and greediness.
2894 4) After A matches, the same WHILEM node is hit again.
2896 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2897 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2898 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2899 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2900 of the external loop.
2902 Currently present infoblocks form a tree with a stem formed by PL_curcc
2903 and whatever it mentions via ->next, and additional attached trees
2904 corresponding to temporarily unset infoblocks as in "5" above.
2906 In the following picture infoblocks for outer loop of
2907 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2908 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2909 infoblocks are drawn below the "reset" infoblock.
2911 In fact in the picture below we do not show failed matches for Z and T
2912 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2913 more obvious *why* one needs to *temporary* unset infoblocks.]
2915 Matched REx position InfoBlocks Comment
2919 Y A)*?Z)*?T x <- O <- I
2920 YA )*?Z)*?T x <- O <- I
2921 YA A)*?Z)*?T x <- O <- I
2922 YAA )*?Z)*?T x <- O <- I
2923 YAA Z)*?T x <- O # Temporary unset I
2926 YAAZ Y(A)*?Z)*?T x <- O
2929 YAAZY (A)*?Z)*?T x <- O
2932 YAAZY A)*?Z)*?T x <- O <- I
2935 YAAZYA )*?Z)*?T x <- O <- I
2938 YAAZYA Z)*?T x <- O # Temporary unset I
2944 YAAZYAZ T x # Temporary unset O
2951 *******************************************************************/
2954 CHECKPOINT cp = PL_savestack_ix;
2955 /* No need to save/restore up to this paren */
2956 I32 parenfloor = scan->flags;
2958 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2960 cc.oldcc = PL_regcc;
2962 /* XXXX Probably it is better to teach regpush to support
2963 parenfloor > PL_regsize... */
2964 if (parenfloor > *PL_reglastparen)
2965 parenfloor = *PL_reglastparen; /* Pessimization... */
2966 cc.parenfloor = parenfloor;
2968 cc.min = ARG1(scan);
2969 cc.max = ARG2(scan);
2970 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2974 PL_reginput = locinput;
2975 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2977 PL_regcc = cc.oldcc;
2983 * This is really hard to understand, because after we match
2984 * what we're trying to match, we must make sure the rest of
2985 * the REx is going to match for sure, and to do that we have
2986 * to go back UP the parse tree by recursing ever deeper. And
2987 * if it fails, we have to reset our parent's current state
2988 * that we can try again after backing off.
2991 CHECKPOINT cp, lastcp;
2992 CURCUR* cc = PL_regcc;
2993 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2995 n = cc->cur + 1; /* how many we know we matched */
2996 PL_reginput = locinput;
2999 PerlIO_printf(Perl_debug_log,
3000 "%*s %ld out of %ld..%ld cc=%lx\n",
3001 REPORT_CODE_OFF+PL_regindent*2, "",
3002 (long)n, (long)cc->min,
3003 (long)cc->max, (long)cc)
3006 /* If degenerate scan matches "", assume scan done. */
3008 if (locinput == cc->lastloc && n >= cc->min) {
3009 PL_regcc = cc->oldcc;
3013 PerlIO_printf(Perl_debug_log,
3014 "%*s empty match detected, try continuation...\n",
3015 REPORT_CODE_OFF+PL_regindent*2, "")
3017 if (regmatch(cc->next))
3025 /* First just match a string of min scans. */
3029 cc->lastloc = locinput;
3030 if (regmatch(cc->scan))
3033 cc->lastloc = lastloc;
3038 /* Check whether we already were at this position.
3039 Postpone detection until we know the match is not
3040 *that* much linear. */
3041 if (!PL_reg_maxiter) {
3042 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3043 PL_reg_leftiter = PL_reg_maxiter;
3045 if (PL_reg_leftiter-- == 0) {
3046 I32 size = (PL_reg_maxiter + 7)/8;
3047 if (PL_reg_poscache) {
3048 if (PL_reg_poscache_size < size) {
3049 Renew(PL_reg_poscache, size, char);
3050 PL_reg_poscache_size = size;
3052 Zero(PL_reg_poscache, size, char);
3055 PL_reg_poscache_size = size;
3056 Newz(29, PL_reg_poscache, size, char);
3059 PerlIO_printf(Perl_debug_log,
3060 "%sDetected a super-linear match, switching on caching%s...\n",
3061 PL_colors[4], PL_colors[5])
3064 if (PL_reg_leftiter < 0) {
3065 I32 o = locinput - PL_bostr, b;
3067 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3070 if (PL_reg_poscache[o] & (1<<b)) {
3072 PerlIO_printf(Perl_debug_log,
3073 "%*s already tried at this position...\n",
3074 REPORT_CODE_OFF+PL_regindent*2, "")
3078 PL_reg_poscache[o] |= (1<<b);
3082 /* Prefer next over scan for minimal matching. */
3085 PL_regcc = cc->oldcc;
3088 cp = regcppush(cc->parenfloor);
3090 if (regmatch(cc->next)) {
3092 sayYES; /* All done. */
3094 REGCP_UNWIND(lastcp);
3100 if (n >= cc->max) { /* Maximum greed exceeded? */
3101 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3102 && !(PL_reg_flags & RF_warned)) {
3103 PL_reg_flags |= RF_warned;
3104 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3105 "Complex regular subexpression recursion",
3112 PerlIO_printf(Perl_debug_log,
3113 "%*s trying longer...\n",
3114 REPORT_CODE_OFF+PL_regindent*2, "")
3116 /* Try scanning more and see if it helps. */
3117 PL_reginput = locinput;
3119 cc->lastloc = locinput;
3120 cp = regcppush(cc->parenfloor);
3122 if (regmatch(cc->scan)) {
3126 REGCP_UNWIND(lastcp);
3129 cc->lastloc = lastloc;
3133 /* Prefer scan over next for maximal matching. */
3135 if (n < cc->max) { /* More greed allowed? */
3136 cp = regcppush(cc->parenfloor);
3138 cc->lastloc = locinput;
3140 if (regmatch(cc->scan)) {
3144 REGCP_UNWIND(lastcp);
3145 regcppop(); /* Restore some previous $<digit>s? */
3146 PL_reginput = locinput;
3148 PerlIO_printf(Perl_debug_log,
3149 "%*s failed, try continuation...\n",
3150 REPORT_CODE_OFF+PL_regindent*2, "")
3153 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3154 && !(PL_reg_flags & RF_warned)) {
3155 PL_reg_flags |= RF_warned;
3156 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3157 "Complex regular subexpression recursion",
3161 /* Failed deeper matches of scan, so see if this one works. */
3162 PL_regcc = cc->oldcc;
3165 if (regmatch(cc->next))
3171 cc->lastloc = lastloc;
3176 next = scan + ARG(scan);
3179 inner = NEXTOPER(NEXTOPER(scan));
3182 inner = NEXTOPER(scan);
3186 if (OP(next) != c1) /* No choice. */
3187 next = inner; /* Avoid recursion. */
3189 I32 lastparen = *PL_reglastparen;
3191 re_unwind_branch_t *uw;
3193 /* Put unwinding data on stack */
3194 unwind1 = SSNEWt(1,re_unwind_branch_t);
3195 uw = SSPTRt(unwind1,re_unwind_branch_t);
3198 uw->type = ((c1 == BRANCH)
3200 : RE_UNWIND_BRANCHJ);
3201 uw->lastparen = lastparen;
3203 uw->locinput = locinput;
3204 uw->nextchr = nextchr;
3206 uw->regindent = ++PL_regindent;
3209 REGCP_SET(uw->lastcp);
3211 /* Now go into the first branch */
3224 /* We suppose that the next guy does not need
3225 backtracking: in particular, it is of constant length,
3226 and has no parenths to influence future backrefs. */
3227 ln = ARG1(scan); /* min to match */
3228 n = ARG2(scan); /* max to match */
3229 paren = scan->flags;
3231 if (paren > PL_regsize)
3233 if (paren > *PL_reglastparen)
3234 *PL_reglastparen = paren;
3236 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3238 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3239 PL_reginput = locinput;
3242 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3244 /* if we matched something zero-length we don't need to
3245 backtrack - capturing parens are already defined, so
3246 the caveat in the maximal case doesn't apply
3248 XXXX if ln == 0, we can redo this check first time
3249 through the following loop
3252 n = ln; /* don't backtrack */
3253 locinput = PL_reginput;
3254 if (HAS_TEXT(next) || JUMPABLE(next)) {
3255 regnode *text_node = next;
3257 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3259 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3261 if (PL_regkind[(U8)OP(text_node)] == REF) {
3263 n = ARG(text_node); /* which paren pair */
3264 ln = PL_regstartp[n];
3265 /* assume yes if we haven't seen CLOSEn */
3267 *PL_reglastparen < n ||
3274 c1 = *(PL_bostr + ln);
3276 else { c1 = (U8)*STRING(text_node); }
3277 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3279 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3280 c2 = PL_fold_locale[c1];
3289 /* This may be improved if l == 0. */
3290 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3291 /* If it could work, try it. */
3293 UCHARAT(PL_reginput) == c1 ||
3294 UCHARAT(PL_reginput) == c2)
3298 PL_regstartp[paren] =
3299 HOPc(PL_reginput, -l) - PL_bostr;
3300 PL_regendp[paren] = PL_reginput - PL_bostr;
3303 PL_regendp[paren] = -1;
3307 REGCP_UNWIND(lastcp);
3309 /* Couldn't or didn't -- move forward. */
3310 PL_reginput = locinput;
3311 if (regrepeat_hard(scan, 1, &l)) {
3313 locinput = PL_reginput;
3320 n = regrepeat_hard(scan, n, &l);
3321 /* if we matched something zero-length we don't need to
3322 backtrack, unless the minimum count is zero and we
3323 are capturing the result - in that case the capture
3324 being defined or not may affect later execution
3326 if (n != 0 && l == 0 && !(paren && ln == 0))
3327 ln = n; /* don't backtrack */
3328 locinput = PL_reginput;
3330 PerlIO_printf(Perl_debug_log,
3331 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3332 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3336 if (HAS_TEXT(next) || JUMPABLE(next)) {
3337 regnode *text_node = next;
3339 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3341 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3343 if (PL_regkind[(U8)OP(text_node)] == REF) {
3345 n = ARG(text_node); /* which paren pair */
3346 ln = PL_regstartp[n];
3347 /* assume yes if we haven't seen CLOSEn */
3349 *PL_reglastparen < n ||
3356 c1 = *(PL_bostr + ln);
3358 else { c1 = (U8)*STRING(text_node); }
3360 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3362 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3363 c2 = PL_fold_locale[c1];
3374 /* If it could work, try it. */
3376 UCHARAT(PL_reginput) == c1 ||
3377 UCHARAT(PL_reginput) == c2)
3380 PerlIO_printf(Perl_debug_log,
3381 "%*s trying tail with n=%"IVdf"...\n",
3382 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3386 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3387 PL_regendp[paren] = PL_reginput - PL_bostr;
3390 PL_regendp[paren] = -1;
3394 REGCP_UNWIND(lastcp);
3396 /* Couldn't or didn't -- back up. */
3398 locinput = HOPc(locinput, -l);
3399 PL_reginput = locinput;
3406 paren = scan->flags; /* Which paren to set */
3407 if (paren > PL_regsize)
3409 if (paren > *PL_reglastparen)
3410 *PL_reglastparen = paren;
3411 ln = ARG1(scan); /* min to match */
3412 n = ARG2(scan); /* max to match */
3413 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3417 ln = ARG1(scan); /* min to match */
3418 n = ARG2(scan); /* max to match */
3419 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3424 scan = NEXTOPER(scan);
3430 scan = NEXTOPER(scan);
3434 * Lookahead to avoid useless match attempts
3435 * when we know what character comes next.
3439 * Used to only do .*x and .*?x, but now it allows
3440 * for )'s, ('s and (?{ ... })'s to be in the way
3441 * of the quantifier and the EXACT-like node. -- japhy
3444 if (HAS_TEXT(next) || JUMPABLE(next)) {
3446 regnode *text_node = next;
3448 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3450 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3452 if (PL_regkind[(U8)OP(text_node)] == REF) {
3454 n = ARG(text_node); /* which paren pair */
3455 ln = PL_regstartp[n];
3456 /* assume yes if we haven't seen CLOSEn */
3458 *PL_reglastparen < n ||
3463 goto assume_ok_easy;
3465 s = (U8*)PL_bostr + ln;
3467 else { s = (U8*)STRING(text_node); }
3471 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3473 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3474 c2 = PL_fold_locale[c1];
3477 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3478 STRLEN ulen1, ulen2;
3479 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3480 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3482 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3483 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3485 c1 = utf8_to_uvuni(tmpbuf1, 0);
3486 c2 = utf8_to_uvuni(tmpbuf2, 0);
3489 c2 = c1 = utf8_to_uvchr(s, NULL);
3497 PL_reginput = locinput;
3501 if (ln && regrepeat(scan, ln) < ln)
3503 locinput = PL_reginput;
3506 char *e; /* Should not check after this */
3507 char *old = locinput;
3509 if (n == REG_INFTY) {
3512 while (UTF8_IS_CONTINUATION(*(U8*)e))
3518 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3522 e = locinput + n - ln;
3528 /* Find place 'next' could work */
3531 while (locinput <= e &&
3532 UCHARAT(locinput) != c1)
3535 while (locinput <= e
3536 && UCHARAT(locinput) != c1
3537 && UCHARAT(locinput) != c2)
3540 count = locinput - old;
3547 utf8_to_uvchr((U8*)locinput, &len) != c1;
3552 for (count = 0; locinput <= e; count++) {
3553 UV c = utf8_to_uvchr((U8*)locinput, &len);
3554 if (c == c1 || c == c2)
3562 /* PL_reginput == old now */
3563 if (locinput != old) {
3564 ln = 1; /* Did some */
3565 if (regrepeat(scan, count) < count)
3568 /* PL_reginput == locinput now */
3569 TRYPAREN(paren, ln, locinput);
3570 PL_reginput = locinput; /* Could be reset... */
3571 REGCP_UNWIND(lastcp);
3572 /* Couldn't or didn't -- move forward. */
3575 locinput += UTF8SKIP(locinput);
3581 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3585 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3587 c = UCHARAT(PL_reginput);
3588 /* If it could work, try it. */
3589 if (c == c1 || c == c2)
3591 TRYPAREN(paren, n, PL_reginput);
3592 REGCP_UNWIND(lastcp);
3595 /* If it could work, try it. */
3596 else if (c1 == -1000)
3598 TRYPAREN(paren, n, PL_reginput);
3599 REGCP_UNWIND(lastcp);
3601 /* Couldn't or didn't -- move forward. */
3602 PL_reginput = locinput;
3603 if (regrepeat(scan, 1)) {
3605 locinput = PL_reginput;
3613 n = regrepeat(scan, n);
3614 locinput = PL_reginput;
3615 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3616 ((!PL_multiline && OP(next) != MEOL) ||
3617 OP(next) == SEOL || OP(next) == EOS))
3619 ln = n; /* why back off? */
3620 /* ...because $ and \Z can match before *and* after
3621 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3622 We should back off by one in this case. */
3623 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3632 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3634 c = UCHARAT(PL_reginput);
3636 /* If it could work, try it. */
3637 if (c1 == -1000 || c == c1 || c == c2)
3639 TRYPAREN(paren, n, PL_reginput);
3640 REGCP_UNWIND(lastcp);
3642 /* Couldn't or didn't -- back up. */
3644 PL_reginput = locinput = HOPc(locinput, -1);
3652 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3654 c = UCHARAT(PL_reginput);
3656 /* If it could work, try it. */
3657 if (c1 == -1000 || c == c1 || c == c2)
3659 TRYPAREN(paren, n, PL_reginput);
3660 REGCP_UNWIND(lastcp);
3662 /* Couldn't or didn't -- back up. */
3664 PL_reginput = locinput = HOPc(locinput, -1);
3671 if (PL_reg_call_cc) {
3672 re_cc_state *cur_call_cc = PL_reg_call_cc;
3673 CURCUR *cctmp = PL_regcc;
3674 regexp *re = PL_reg_re;
3675 CHECKPOINT cp, lastcp;
3677 cp = regcppush(0); /* Save *all* the positions. */
3679 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3681 PL_reginput = locinput; /* Make position available to
3683 cache_re(PL_reg_call_cc->re);
3684 PL_regcc = PL_reg_call_cc->cc;
3685 PL_reg_call_cc = PL_reg_call_cc->prev;
3686 if (regmatch(cur_call_cc->node)) {
3687 PL_reg_call_cc = cur_call_cc;
3691 REGCP_UNWIND(lastcp);
3693 PL_reg_call_cc = cur_call_cc;
3699 PerlIO_printf(Perl_debug_log,
3700 "%*s continuation failed...\n",
3701 REPORT_CODE_OFF+PL_regindent*2, "")
3705 if (locinput < PL_regtill) {
3706 DEBUG_r(PerlIO_printf(Perl_debug_log,
3707 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3709 (long)(locinput - PL_reg_starttry),
3710 (long)(PL_regtill - PL_reg_starttry),
3712 sayNO_FINAL; /* Cannot match: too short. */
3714 PL_reginput = locinput; /* put where regtry can find it */
3715 sayYES_FINAL; /* Success! */
3717 PL_reginput = locinput; /* put where regtry can find it */
3718 sayYES_LOUD; /* Success! */
3721 PL_reginput = locinput;
3726 s = HOPBACKc(locinput, scan->flags);
3732 PL_reginput = locinput;
3737 s = HOPBACKc(locinput, scan->flags);
3743 PL_reginput = locinput;
3746 inner = NEXTOPER(NEXTOPER(scan));
3747 if (regmatch(inner) != n) {
3762 if (OP(scan) == SUSPEND) {
3763 locinput = PL_reginput;
3764 nextchr = UCHARAT(locinput);
3769 next = scan + ARG(scan);
3774 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3775 PTR2UV(scan), OP(scan));
3776 Perl_croak(aTHX_ "regexp memory corruption");
3783 * We get here only if there's trouble -- normally "case END" is
3784 * the terminating point.
3786 Perl_croak(aTHX_ "corrupted regexp pointers");
3792 PerlIO_printf(Perl_debug_log,
3793 "%*s %scould match...%s\n",
3794 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3798 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3799 PL_colors[4],PL_colors[5]));
3805 #if 0 /* Breaks $^R */
3813 PerlIO_printf(Perl_debug_log,
3814 "%*s %sfailed...%s\n",
3815 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3821 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3824 case RE_UNWIND_BRANCH:
3825 case RE_UNWIND_BRANCHJ:
3827 re_unwind_branch_t *uwb = &(uw->branch);
3828 I32 lastparen = uwb->lastparen;
3830 REGCP_UNWIND(uwb->lastcp);
3831 for (n = *PL_reglastparen; n > lastparen; n--)
3833 *PL_reglastparen = n;
3834 scan = next = uwb->next;
3836 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3837 ? BRANCH : BRANCHJ) ) { /* Failure */
3844 /* Have more choice yet. Reuse the same uwb. */
3846 if ((n = (uwb->type == RE_UNWIND_BRANCH
3847 ? NEXT_OFF(next) : ARG(next))))
3850 next = NULL; /* XXXX Needn't unwinding in this case... */
3852 next = NEXTOPER(scan);
3853 if (uwb->type == RE_UNWIND_BRANCHJ)
3854 next = NEXTOPER(next);
3855 locinput = uwb->locinput;
3856 nextchr = uwb->nextchr;
3858 PL_regindent = uwb->regindent;
3865 Perl_croak(aTHX_ "regexp unwind memory corruption");
3876 - regrepeat - repeatedly match something simple, report how many
3879 * [This routine now assumes that it will only match on things of length 1.
3880 * That was true before, but now we assume scan - reginput is the count,
3881 * rather than incrementing count on every character. [Er, except utf8.]]
3884 S_regrepeat(pTHX_ regnode *p, I32 max)
3886 register char *scan;
3888 register char *loceol = PL_regeol;
3889 register I32 hardcount = 0;
3890 register bool do_utf8 = PL_reg_match_utf8;
3893 if (max != REG_INFTY && max < loceol - scan)
3894 loceol = scan + max;
3899 while (scan < loceol && hardcount < max && *scan != '\n') {
3900 scan += UTF8SKIP(scan);
3904 while (scan < loceol && *scan != '\n')
3911 while (scan < loceol && hardcount < max) {
3912 scan += UTF8SKIP(scan);
3922 case EXACT: /* length of string is 1 */
3924 while (scan < loceol && UCHARAT(scan) == c)
3927 case EXACTF: /* length of string is 1 */
3929 while (scan < loceol &&
3930 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3933 case EXACTFL: /* length of string is 1 */
3934 PL_reg_flags |= RF_tainted;
3936 while (scan < loceol &&
3937 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3943 while (hardcount < max && scan < loceol &&
3944 reginclass(p, (U8*)scan, do_utf8)) {
3945 scan += UTF8SKIP(scan);
3949 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3956 LOAD_UTF8_CHARCLASS(alnum,"a");
3957 while (hardcount < max && scan < loceol &&
3958 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3959 scan += UTF8SKIP(scan);
3963 while (scan < loceol && isALNUM(*scan))
3968 PL_reg_flags |= RF_tainted;
3971 while (hardcount < max && scan < loceol &&
3972 isALNUM_LC_utf8((U8*)scan)) {
3973 scan += UTF8SKIP(scan);
3977 while (scan < loceol && isALNUM_LC(*scan))
3984 LOAD_UTF8_CHARCLASS(alnum,"a");
3985 while (hardcount < max && scan < loceol &&
3986 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3987 scan += UTF8SKIP(scan);
3991 while (scan < loceol && !isALNUM(*scan))
3996 PL_reg_flags |= RF_tainted;
3999 while (hardcount < max && scan < loceol &&
4000 !isALNUM_LC_utf8((U8*)scan)) {
4001 scan += UTF8SKIP(scan);
4005 while (scan < loceol && !isALNUM_LC(*scan))
4012 LOAD_UTF8_CHARCLASS(space," ");
4013 while (hardcount < max && scan < loceol &&
4015 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4016 scan += UTF8SKIP(scan);
4020 while (scan < loceol && isSPACE(*scan))
4025 PL_reg_flags |= RF_tainted;
4028 while (hardcount < max && scan < loceol &&
4029 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4030 scan += UTF8SKIP(scan);
4034 while (scan < loceol && isSPACE_LC(*scan))
4041 LOAD_UTF8_CHARCLASS(space," ");
4042 while (hardcount < max && scan < loceol &&
4044 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4045 scan += UTF8SKIP(scan);
4049 while (scan < loceol && !isSPACE(*scan))
4054 PL_reg_flags |= RF_tainted;
4057 while (hardcount < max && scan < loceol &&
4058 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4059 scan += UTF8SKIP(scan);
4063 while (scan < loceol && !isSPACE_LC(*scan))
4070 LOAD_UTF8_CHARCLASS(digit,"0");
4071 while (hardcount < max && scan < loceol &&
4072 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4073 scan += UTF8SKIP(scan);
4077 while (scan < loceol && isDIGIT(*scan))
4084 LOAD_UTF8_CHARCLASS(digit,"0");
4085 while (hardcount < max && scan < loceol &&
4086 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4087 scan += UTF8SKIP(scan);
4091 while (scan < loceol && !isDIGIT(*scan))
4095 default: /* Called on something of 0 width. */
4096 break; /* So match right here or not at all. */
4102 c = scan - PL_reginput;
4107 SV *prop = sv_newmortal();
4110 PerlIO_printf(Perl_debug_log,
4111 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4112 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4119 - regrepeat_hard - repeatedly match something, report total lenth and length
4121 * The repeater is supposed to have constant length.
4125 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4127 register char *scan = Nullch;
4128 register char *start;
4129 register char *loceol = PL_regeol;
4131 I32 count = 0, res = 1;
4136 start = PL_reginput;
4137 if (PL_reg_match_utf8) {
4138 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4141 while (start < PL_reginput) {
4143 start += UTF8SKIP(start);
4154 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4156 *lp = l = PL_reginput - start;
4157 if (max != REG_INFTY && l*max < loceol - scan)
4158 loceol = scan + l*max;
4171 - regclass_swash - prepare the utf8 swash
4175 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4181 if (PL_regdata && PL_regdata->count) {
4184 if (PL_regdata->what[n] == 's') {
4185 SV *rv = (SV*)PL_regdata->data[n];
4186 AV *av = (AV*)SvRV((SV*)rv);
4189 /* See the end of regcomp.c:S_reglass() for
4190 * documentation of these array elements. */
4192 si = *av_fetch(av, 0, FALSE);
4193 a = av_fetch(av, 1, FALSE);
4194 b = av_fetch(av, 2, FALSE);
4198 else if (si && doinit) {
4199 sw = swash_init("utf8", "", si, 1, 0);
4200 (void)av_store(av, 1, sw);
4216 - reginclasslen - determine if a character falls into a character class
4218 The n is the ANYOF regnode, the p is the target string, lenp
4219 is pointer to the maximum length of how far to go in the p
4220 (if the lenp is zero, UTF8SKIP(p) is used),
4221 do_utf8 tells whether the target string is in UTF-8.
4226 S_reginclasslen(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4228 char flags = ANYOF_FLAGS(n);
4234 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4236 plen = lenp ? *lenp : UNISKIP(c);
4237 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4240 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4241 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4244 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4248 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4251 if (swash_fetch(sw, p, do_utf8))
4253 else if (flags & ANYOF_FOLD) {
4254 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4257 if (!match && lenp && av) {
4260 for (i = 0; i <= av_len(av); i++) {
4261 SV* sv = *av_fetch(av, i, FALSE);
4263 char *s = SvPV(sv, len);
4265 if (len <= plen && memEQ(s, p, len)) {
4273 to_utf8_fold(p, tmpbuf, &tmplen);
4274 if (swash_fetch(sw, tmpbuf, do_utf8))
4278 to_utf8_upper(p, tmpbuf, &tmplen);
4279 if (swash_fetch(sw, tmpbuf, do_utf8))
4285 if (match && lenp && *lenp == 0)
4288 if (!match && c < 256) {
4289 if (ANYOF_BITMAP_TEST(n, c))
4291 else if (flags & ANYOF_FOLD) {
4294 if (flags & ANYOF_LOCALE) {
4295 PL_reg_flags |= RF_tainted;
4296 f = PL_fold_locale[c];
4300 if (f != c && ANYOF_BITMAP_TEST(n, f))
4304 if (!match && (flags & ANYOF_CLASS)) {
4305 PL_reg_flags |= RF_tainted;
4307 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4308 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4309 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4310 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4311 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4312 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4313 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4314 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4315 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4316 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4317 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4318 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4319 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4320 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4321 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4322 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4323 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4324 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4325 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4326 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4327 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4328 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4329 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4330 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4331 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4332 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4333 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4334 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4335 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4336 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4337 ) /* How's that for a conditional? */
4344 return (flags & ANYOF_INVERT) ? !match : match;
4348 - reginclass - determine if a character falls into a character class
4350 The n is the ANYOF regnode, the p is the target string, do_utf8 tells
4351 whether the target string is in UTF-8.
4356 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
4358 return S_reginclasslen(aTHX_ n, p, 0, do_utf8);
4362 S_reghop(pTHX_ U8 *s, I32 off)
4364 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4368 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4371 while (off-- && s < lim) {
4372 /* XXX could check well-formedness here */
4380 if (UTF8_IS_CONTINUED(*s)) {
4381 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4384 /* XXX could check well-formedness here */
4392 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4394 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4398 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4401 while (off-- && s < lim) {
4402 /* XXX could check well-formedness here */
4412 if (UTF8_IS_CONTINUED(*s)) {
4413 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4416 /* XXX could check well-formedness here */
4428 restore_pos(pTHX_ void *arg)
4430 if (PL_reg_eval_set) {
4431 if (PL_reg_oldsaved) {
4432 PL_reg_re->subbeg = PL_reg_oldsaved;
4433 PL_reg_re->sublen = PL_reg_oldsavedlen;
4434 RX_MATCH_COPIED_on(PL_reg_re);
4436 PL_reg_magic->mg_len = PL_reg_oldpos;
4437 PL_reg_eval_set = 0;
4438 PL_curpm = PL_reg_oldcurpm;