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-2002, 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 \
144 Search for mandatory following text node; for lookahead, the text must
145 follow but for lookbehind (rn->flags != 0) we skip to the next step.
147 #define FIND_NEXT_IMPT(rn) STMT_START { \
148 while (JUMPABLE(rn)) \
149 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
150 rn = NEXTOPER(NEXTOPER(rn)); \
151 else if (OP(rn) == PLUS) \
153 else if (OP(rn) == IFMATCH) \
154 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
155 else rn += NEXT_OFF(rn); \
158 static void restore_pos(pTHX_ void *arg);
161 S_regcppush(pTHX_ I32 parenfloor)
163 int retval = PL_savestack_ix;
164 #define REGCP_PAREN_ELEMS 4
165 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
168 if (paren_elems_to_push < 0)
169 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
171 #define REGCP_OTHER_ELEMS 6
172 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
173 for (p = PL_regsize; p > parenfloor; p--) {
174 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
175 SSPUSHINT(PL_regendp[p]);
176 SSPUSHINT(PL_regstartp[p]);
177 SSPUSHPTR(PL_reg_start_tmp[p]);
180 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
181 SSPUSHINT(PL_regsize);
182 SSPUSHINT(*PL_reglastparen);
183 SSPUSHINT(*PL_reglastcloseparen);
184 SSPUSHPTR(PL_reginput);
185 #define REGCP_FRAME_ELEMS 2
186 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
187 * are needed for the regexp context stack bookkeeping. */
188 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
189 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
194 /* These are needed since we do not localize EVAL nodes: */
195 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
196 " Setting an EVAL scope, savestack=%"IVdf"\n", \
197 (IV)PL_savestack_ix)); cp = PL_savestack_ix
199 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
200 PerlIO_printf(Perl_debug_log, \
201 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
202 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
212 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
214 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
215 i = SSPOPINT; /* Parentheses elements to pop. */
216 input = (char *) SSPOPPTR;
217 *PL_reglastcloseparen = SSPOPINT;
218 *PL_reglastparen = SSPOPINT;
219 PL_regsize = SSPOPINT;
221 /* Now restore the parentheses context. */
222 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
223 i > 0; i -= REGCP_PAREN_ELEMS) {
224 paren = (U32)SSPOPINT;
225 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
226 PL_regstartp[paren] = SSPOPINT;
228 if (paren <= *PL_reglastparen)
229 PL_regendp[paren] = tmps;
231 PerlIO_printf(Perl_debug_log,
232 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
233 (UV)paren, (IV)PL_regstartp[paren],
234 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
235 (IV)PL_regendp[paren],
236 (paren > *PL_reglastparen ? "(no)" : ""));
240 if (*PL_reglastparen + 1 <= PL_regnpar) {
241 PerlIO_printf(Perl_debug_log,
242 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
243 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
247 /* It would seem that the similar code in regtry()
248 * already takes care of this, and in fact it is in
249 * a better location to since this code can #if 0-ed out
250 * but the code in regtry() is needed or otherwise tests
251 * requiring null fields (pat.t#187 and split.t#{13,14}
252 * (as of patchlevel 7877) will fail. Then again,
253 * this code seems to be necessary or otherwise
254 * building DynaLoader will fail:
255 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
257 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
258 if (paren > PL_regsize)
259 PL_regstartp[paren] = -1;
260 PL_regendp[paren] = -1;
267 S_regcp_set_to(pTHX_ I32 ss)
269 I32 tmp = PL_savestack_ix;
271 PL_savestack_ix = ss;
273 PL_savestack_ix = tmp;
277 typedef struct re_cc_state
281 struct re_cc_state *prev;
286 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
288 #define TRYPAREN(paren, n, input) { \
291 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
292 PL_regendp[paren] = input - PL_bostr; \
295 PL_regendp[paren] = -1; \
297 if (regmatch(next)) \
300 PL_regendp[paren] = -1; \
305 * pregexec and friends
309 - pregexec - match a regexp against a string
312 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
313 char *strbeg, I32 minend, SV *screamer, U32 nosave)
314 /* strend: pointer to null at end of string */
315 /* strbeg: real beginning of string */
316 /* minend: end of match must be >=minend after stringarg. */
317 /* nosave: For optimizations. */
320 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
321 nosave ? 0 : REXEC_COPY_STR);
325 S_cache_re(pTHX_ regexp *prog)
327 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
329 PL_regprogram = prog->program;
331 PL_regnpar = prog->nparens;
332 PL_regdata = prog->data;
337 * Need to implement the following flags for reg_anch:
339 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
341 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
342 * INTUIT_AUTORITATIVE_ML
343 * INTUIT_ONCE_NOML - Intuit can match in one location only.
346 * Another flag for this function: SECOND_TIME (so that float substrs
347 * with giant delta may be not rechecked).
350 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
352 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
353 Otherwise, only SvCUR(sv) is used to get strbeg. */
355 /* XXXX We assume that strpos is strbeg unless sv. */
357 /* XXXX Some places assume that there is a fixed substring.
358 An update may be needed if optimizer marks as "INTUITable"
359 RExen without fixed substrings. Similarly, it is assumed that
360 lengths of all the strings are no more than minlen, thus they
361 cannot come from lookahead.
362 (Or minlen should take into account lookahead.) */
364 /* A failure to find a constant substring means that there is no need to make
365 an expensive call to REx engine, thus we celebrate a failure. Similarly,
366 finding a substring too deep into the string means that less calls to
367 regtry() should be needed.
369 REx compiler's optimizer found 4 possible hints:
370 a) Anchored substring;
372 c) Whether we are anchored (beginning-of-line or \G);
373 d) First node (of those at offset 0) which may distingush positions;
374 We use a)b)d) and multiline-part of c), and try to find a position in the
375 string which does not contradict any of them.
378 /* Most of decisions we do here should have been done at compile time.
379 The nodes of the REx which we used for the search should have been
380 deleted from the finite automaton. */
383 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
384 char *strend, U32 flags, re_scream_pos_data *data)
386 register I32 start_shift = 0;
387 /* Should be nonnegative! */
388 register I32 end_shift = 0;
394 register char *other_last = Nullch; /* other substr checked before this */
395 char *check_at = Nullch; /* check substr found at this pos */
397 char *i_strpos = strpos;
398 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
401 if (prog->reganch & ROPT_UTF8) {
402 DEBUG_r(PerlIO_printf(Perl_debug_log,
403 "UTF-8 regex...\n"));
404 PL_reg_flags |= RF_utf8;
408 char *s = PL_reg_match_utf8 ?
409 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
411 int len = PL_reg_match_utf8 ?
412 strlen(s) : strend - strpos;
415 if (PL_reg_match_utf8)
416 DEBUG_r(PerlIO_printf(Perl_debug_log,
417 "UTF-8 target...\n"));
418 PerlIO_printf(Perl_debug_log,
419 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
420 PL_colors[4],PL_colors[5],PL_colors[0],
423 (strlen(prog->precomp) > 60 ? "..." : ""),
425 (int)(len > 60 ? 60 : len),
427 (len > 60 ? "..." : "")
431 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
432 DEBUG_r(PerlIO_printf(Perl_debug_log,
433 "String too short... [re_intuit_start]\n"));
436 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
438 check = prog->check_substr;
439 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
440 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
441 || ( (prog->reganch & ROPT_ANCH_BOL)
442 && !PL_multiline ) ); /* Check after \n? */
445 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
446 | ROPT_IMPLICIT)) /* not a real BOL */
447 /* SvCUR is not set on references: SvRV and SvPVX overlap */
449 && (strpos != strbeg)) {
450 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
453 if (prog->check_offset_min == prog->check_offset_max &&
454 !(prog->reganch & ROPT_CANY_SEEN)) {
455 /* Substring at constant offset from beg-of-str... */
458 s = HOP3c(strpos, prog->check_offset_min, strend);
460 slen = SvCUR(check); /* >= 1 */
462 if ( strend - s > slen || strend - s < slen - 1
463 || (strend - s == slen && strend[-1] != '\n')) {
464 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
467 /* Now should match s[0..slen-2] */
469 if (slen && (*SvPVX(check) != *s
471 && memNE(SvPVX(check), s, slen)))) {
473 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
477 else if (*SvPVX(check) != *s
478 || ((slen = SvCUR(check)) > 1
479 && memNE(SvPVX(check), s, slen)))
481 goto success_at_start;
484 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
486 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
487 end_shift = prog->minlen - start_shift -
488 CHR_SVLEN(check) + (SvTAIL(check) != 0);
490 I32 end = prog->check_offset_max + CHR_SVLEN(check)
491 - (SvTAIL(check) != 0);
492 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
494 if (end_shift < eshift)
498 else { /* Can match at random position */
501 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
502 /* Should be nonnegative! */
503 end_shift = prog->minlen - start_shift -
504 CHR_SVLEN(check) + (SvTAIL(check) != 0);
507 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
509 Perl_croak(aTHX_ "panic: end_shift");
513 /* Find a possible match in the region s..strend by looking for
514 the "check" substring in the region corrected by start/end_shift. */
515 if (flags & REXEC_SCREAM) {
516 I32 p = -1; /* Internal iterator of scream. */
517 I32 *pp = data ? data->scream_pos : &p;
519 if (PL_screamfirst[BmRARE(check)] >= 0
520 || ( BmRARE(check) == '\n'
521 && (BmPREVIOUS(check) == SvCUR(check) - 1)
523 s = screaminstr(sv, check,
524 start_shift + (s - strbeg), end_shift, pp, 0);
528 *data->scream_olds = s;
530 else if (prog->reganch & ROPT_CANY_SEEN)
531 s = fbm_instr((U8*)(s + start_shift),
532 (U8*)(strend - end_shift),
533 check, PL_multiline ? FBMrf_MULTILINE : 0);
535 s = fbm_instr(HOP3(s, start_shift, strend),
536 HOP3(strend, -end_shift, strbeg),
537 check, PL_multiline ? FBMrf_MULTILINE : 0);
539 /* Update the count-of-usability, remove useless subpatterns,
542 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
543 (s ? "Found" : "Did not find"),
544 ((check == prog->anchored_substr) ? "anchored" : "floating"),
546 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
548 PL_colors[1], (SvTAIL(check) ? "$" : ""),
549 (s ? " at offset " : "...\n") ) );
556 /* Finish the diagnostic message */
557 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
559 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
560 Start with the other substr.
561 XXXX no SCREAM optimization yet - and a very coarse implementation
562 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
563 *always* match. Probably should be marked during compile...
564 Probably it is right to do no SCREAM here...
567 if (prog->float_substr && prog->anchored_substr) {
568 /* Take into account the "other" substring. */
569 /* XXXX May be hopelessly wrong for UTF... */
572 if (check == prog->float_substr) {
575 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
578 t = s - prog->check_offset_max;
579 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
580 && (!(prog->reganch & ROPT_UTF8)
581 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
586 t = HOP3c(t, prog->anchored_offset, strend);
587 if (t < other_last) /* These positions already checked */
589 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
592 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
593 /* On end-of-str: see comment below. */
594 s = fbm_instr((unsigned char*)t,
595 HOP3(HOP3(last1, prog->anchored_offset, strend)
596 + SvCUR(prog->anchored_substr),
597 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
598 prog->anchored_substr,
599 PL_multiline ? FBMrf_MULTILINE : 0);
600 DEBUG_r(PerlIO_printf(Perl_debug_log,
601 "%s anchored substr `%s%.*s%s'%s",
602 (s ? "Found" : "Contradicts"),
604 (int)(SvCUR(prog->anchored_substr)
605 - (SvTAIL(prog->anchored_substr)!=0)),
606 SvPVX(prog->anchored_substr),
607 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
609 if (last1 >= last2) {
610 DEBUG_r(PerlIO_printf(Perl_debug_log,
611 ", giving up...\n"));
614 DEBUG_r(PerlIO_printf(Perl_debug_log,
615 ", trying floating at offset %ld...\n",
616 (long)(HOP3c(s1, 1, strend) - i_strpos)));
617 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
618 s = HOP3c(last, 1, strend);
622 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
623 (long)(s - i_strpos)));
624 t = HOP3c(s, -prog->anchored_offset, strbeg);
625 other_last = HOP3c(s, 1, strend);
633 else { /* Take into account the floating substring. */
637 t = HOP3c(s, -start_shift, strbeg);
639 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
640 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
641 last = HOP3c(t, prog->float_max_offset, strend);
642 s = HOP3c(t, prog->float_min_offset, strend);
645 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
646 /* fbm_instr() takes into account exact value of end-of-str
647 if the check is SvTAIL(ed). Since false positives are OK,
648 and end-of-str is not later than strend we are OK. */
649 s = fbm_instr((unsigned char*)s,
650 (unsigned char*)last + SvCUR(prog->float_substr)
651 - (SvTAIL(prog->float_substr)!=0),
652 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
653 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
654 (s ? "Found" : "Contradicts"),
656 (int)(SvCUR(prog->float_substr)
657 - (SvTAIL(prog->float_substr)!=0)),
658 SvPVX(prog->float_substr),
659 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
662 DEBUG_r(PerlIO_printf(Perl_debug_log,
663 ", giving up...\n"));
666 DEBUG_r(PerlIO_printf(Perl_debug_log,
667 ", trying anchored starting at offset %ld...\n",
668 (long)(s1 + 1 - i_strpos)));
670 s = HOP3c(t, 1, strend);
674 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
675 (long)(s - i_strpos)));
676 other_last = s; /* Fix this later. --Hugo */
685 t = s - prog->check_offset_max;
686 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
687 && (!(prog->reganch & ROPT_UTF8)
688 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
690 /* Fixed substring is found far enough so that the match
691 cannot start at strpos. */
693 if (ml_anch && t[-1] != '\n') {
694 /* Eventually fbm_*() should handle this, but often
695 anchored_offset is not 0, so this check will not be wasted. */
696 /* XXXX In the code below we prefer to look for "^" even in
697 presence of anchored substrings. And we search even
698 beyond the found float position. These pessimizations
699 are historical artefacts only. */
701 while (t < strend - prog->minlen) {
703 if (t < check_at - prog->check_offset_min) {
704 if (prog->anchored_substr) {
705 /* Since we moved from the found position,
706 we definitely contradict the found anchored
707 substr. Due to the above check we do not
708 contradict "check" substr.
709 Thus we can arrive here only if check substr
710 is float. Redo checking for "other"=="fixed".
713 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
714 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
715 goto do_other_anchored;
717 /* We don't contradict the found floating substring. */
718 /* XXXX Why not check for STCLASS? */
720 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
721 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
724 /* Position contradicts check-string */
725 /* XXXX probably better to look for check-string
726 than for "\n", so one should lower the limit for t? */
727 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
728 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
729 other_last = strpos = s = t + 1;
734 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
735 PL_colors[0],PL_colors[1]));
739 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
740 PL_colors[0],PL_colors[1]));
744 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
747 /* The found string does not prohibit matching at strpos,
748 - no optimization of calling REx engine can be performed,
749 unless it was an MBOL and we are not after MBOL,
750 or a future STCLASS check will fail this. */
752 /* Even in this situation we may use MBOL flag if strpos is offset
753 wrt the start of the string. */
754 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
755 && (strpos != strbeg) && strpos[-1] != '\n'
756 /* May be due to an implicit anchor of m{.*foo} */
757 && !(prog->reganch & ROPT_IMPLICIT))
762 DEBUG_r( if (ml_anch)
763 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
764 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
767 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
768 && prog->check_substr /* Could be deleted already */
769 && --BmUSEFUL(prog->check_substr) < 0
770 && prog->check_substr == prog->float_substr)
772 /* If flags & SOMETHING - do not do it many times on the same match */
773 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
774 SvREFCNT_dec(prog->check_substr);
775 prog->check_substr = Nullsv; /* disable */
776 prog->float_substr = Nullsv; /* clear */
777 check = Nullsv; /* abort */
779 /* XXXX This is a remnant of the old implementation. It
780 looks wasteful, since now INTUIT can use many
782 prog->reganch &= ~RE_USE_INTUIT;
789 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
790 if (prog->regstclass) {
791 /* minlen == 0 is possible if regstclass is \b or \B,
792 and the fixed substr is ''$.
793 Since minlen is already taken into account, s+1 is before strend;
794 accidentally, minlen >= 1 guaranties no false positives at s + 1
795 even for \b or \B. But (minlen? 1 : 0) below assumes that
796 regstclass does not come from lookahead... */
797 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
798 This leaves EXACTF only, which is dealt with in find_byclass(). */
799 U8* str = (U8*)STRING(prog->regstclass);
800 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
801 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
803 char *endpos = (prog->anchored_substr || ml_anch)
804 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
805 : (prog->float_substr
806 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
809 char *startpos = strbeg;
812 if (prog->reganch & ROPT_UTF8) {
813 PL_regdata = prog->data;
816 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
821 if (endpos == strend) {
822 DEBUG_r( PerlIO_printf(Perl_debug_log,
823 "Could not match STCLASS...\n") );
826 DEBUG_r( PerlIO_printf(Perl_debug_log,
827 "This position contradicts STCLASS...\n") );
828 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
830 /* Contradict one of substrings */
831 if (prog->anchored_substr) {
832 if (prog->anchored_substr == check) {
833 DEBUG_r( what = "anchored" );
835 s = HOP3c(t, 1, strend);
836 if (s + start_shift + end_shift > strend) {
837 /* XXXX Should be taken into account earlier? */
838 DEBUG_r( PerlIO_printf(Perl_debug_log,
839 "Could not match STCLASS...\n") );
844 DEBUG_r( PerlIO_printf(Perl_debug_log,
845 "Looking for %s substr starting at offset %ld...\n",
846 what, (long)(s + start_shift - i_strpos)) );
849 /* Have both, check_string is floating */
850 if (t + start_shift >= check_at) /* Contradicts floating=check */
851 goto retry_floating_check;
852 /* Recheck anchored substring, but not floating... */
856 DEBUG_r( PerlIO_printf(Perl_debug_log,
857 "Looking for anchored substr starting at offset %ld...\n",
858 (long)(other_last - i_strpos)) );
859 goto do_other_anchored;
861 /* Another way we could have checked stclass at the
862 current position only: */
867 DEBUG_r( PerlIO_printf(Perl_debug_log,
868 "Looking for /%s^%s/m starting at offset %ld...\n",
869 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
872 if (!prog->float_substr) /* Could have been deleted */
874 /* Check is floating subtring. */
875 retry_floating_check:
876 t = check_at - start_shift;
877 DEBUG_r( what = "floating" );
878 goto hop_and_restart;
881 DEBUG_r(PerlIO_printf(Perl_debug_log,
882 "By STCLASS: moving %ld --> %ld\n",
883 (long)(t - i_strpos), (long)(s - i_strpos))
887 DEBUG_r(PerlIO_printf(Perl_debug_log,
888 "Does not contradict STCLASS...\n");
893 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
894 PL_colors[4], (check ? "Guessed" : "Giving up"),
895 PL_colors[5], (long)(s - i_strpos)) );
898 fail_finish: /* Substring not found */
899 if (prog->check_substr) /* could be removed already */
900 BmUSEFUL(prog->check_substr) += 5; /* hooray */
902 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
903 PL_colors[4],PL_colors[5]));
907 /* We know what class REx starts with. Try to find this position... */
909 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
911 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
917 register I32 tmp = 1; /* Scratch variable? */
918 register bool do_utf8 = PL_reg_match_utf8;
920 /* We know what class it must start with. */
924 STRLEN skip = do_utf8 ? UTF8SKIP(s) : 1;
926 if (reginclass(c, (U8*)s, do_utf8) ||
927 (ANYOF_UNICODE_FOLD_SHARP_S(c, s, strend) &&
929 if (tmp && (norun || regtry(prog, s)))
941 if (tmp && (norun || regtry(prog, s)))
953 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
954 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
956 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
957 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
959 c1 = utf8_to_uvuni(tmpbuf1, 0);
960 c2 = utf8_to_uvuni(tmpbuf2, 0);
971 c2 = PL_fold_locale[c1];
973 e = do_utf8 ? s + ln : strend - ln;
976 e = s; /* Due to minlen logic of intuit() */
978 /* The idea in the EXACTF* cases is to first find the
979 * first character of the EXACTF* node and then, if
980 * necessary, case-insensitively compare the full
981 * text of the node. The c1 and c2 are the first
982 * characters (though in Unicode it gets a bit
983 * more complicated because there are more cases
984 * than just upper and lower: one is really supposed
985 * to use the so-called folding case for case-insensitive
986 * matching (called "loose matching" in Unicode). */
990 U8 tmpbuf [UTF8_MAXLEN+1];
991 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
996 c = utf8_to_uvchr((U8*)s, &len);
999 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1000 m, (char **)0, ln, UTF))
1001 && (norun || regtry(prog, s)) )
1004 uvchr_to_utf8(tmpbuf, c);
1005 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1007 && (f == c1 || f == c2)
1008 && (ln == foldlen ||
1009 !ibcmp_utf8((char *) foldbuf,
1010 (char **)0, foldlen, do_utf8,
1012 (char **)0, ln, UTF))
1013 && (norun || regtry(prog, s)) )
1021 c = utf8_to_uvchr((U8*)s, &len);
1023 /* Handle some of the three Greek sigmas cases.
1024 * Note that not all the possible combinations
1025 * are handled here: some of them are handled
1026 * by the standard folding rules, and some of
1027 * them (the character class or ANYOF cases)
1028 * are handled during compiletime in
1029 * regexec.c:S_regclass(). */
1030 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1031 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1032 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1034 if ( (c == c1 || c == c2)
1036 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1037 m, (char **)0, ln, UTF))
1038 && (norun || regtry(prog, s)) )
1041 uvchr_to_utf8(tmpbuf, c);
1042 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1044 && (f == c1 || f == c2)
1045 && (ln == foldlen ||
1046 !ibcmp_utf8((char *)foldbuf,
1047 (char **)0, foldlen, do_utf8,
1049 (char **)0, ln, UTF))
1050 && (norun || regtry(prog, s)) )
1061 && (ln == 1 || !(OP(c) == EXACTF
1063 : ibcmp_locale(s, m, ln)))
1064 && (norun || regtry(prog, s)) )
1070 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1071 && (ln == 1 || !(OP(c) == EXACTF
1073 : ibcmp_locale(s, m, ln)))
1074 && (norun || regtry(prog, s)) )
1081 PL_reg_flags |= RF_tainted;
1088 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1091 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1093 tmp = ((OP(c) == BOUND ?
1094 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1095 LOAD_UTF8_CHARCLASS(alnum,"a");
1096 while (s < strend) {
1097 if (tmp == !(OP(c) == BOUND ?
1098 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1099 isALNUM_LC_utf8((U8*)s)))
1102 if ((norun || regtry(prog, s)))
1109 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1110 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1111 while (s < strend) {
1113 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1115 if ((norun || regtry(prog, s)))
1121 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1125 PL_reg_flags |= RF_tainted;
1132 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1135 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1137 tmp = ((OP(c) == NBOUND ?
1138 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1139 LOAD_UTF8_CHARCLASS(alnum,"a");
1140 while (s < strend) {
1141 if (tmp == !(OP(c) == NBOUND ?
1142 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1143 isALNUM_LC_utf8((U8*)s)))
1145 else if ((norun || regtry(prog, s)))
1151 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1152 tmp = ((OP(c) == NBOUND ?
1153 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1154 while (s < strend) {
1156 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1158 else if ((norun || regtry(prog, s)))
1163 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1168 LOAD_UTF8_CHARCLASS(alnum,"a");
1169 while (s < strend) {
1170 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1171 if (tmp && (norun || regtry(prog, s)))
1182 while (s < strend) {
1184 if (tmp && (norun || regtry(prog, s)))
1196 PL_reg_flags |= RF_tainted;
1198 while (s < strend) {
1199 if (isALNUM_LC_utf8((U8*)s)) {
1200 if (tmp && (norun || regtry(prog, s)))
1211 while (s < strend) {
1212 if (isALNUM_LC(*s)) {
1213 if (tmp && (norun || regtry(prog, s)))
1226 LOAD_UTF8_CHARCLASS(alnum,"a");
1227 while (s < strend) {
1228 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1229 if (tmp && (norun || regtry(prog, s)))
1240 while (s < strend) {
1242 if (tmp && (norun || regtry(prog, s)))
1254 PL_reg_flags |= RF_tainted;
1256 while (s < strend) {
1257 if (!isALNUM_LC_utf8((U8*)s)) {
1258 if (tmp && (norun || regtry(prog, s)))
1269 while (s < strend) {
1270 if (!isALNUM_LC(*s)) {
1271 if (tmp && (norun || regtry(prog, s)))
1284 LOAD_UTF8_CHARCLASS(space," ");
1285 while (s < strend) {
1286 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1287 if (tmp && (norun || regtry(prog, s)))
1298 while (s < strend) {
1300 if (tmp && (norun || regtry(prog, s)))
1312 PL_reg_flags |= RF_tainted;
1314 while (s < strend) {
1315 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1316 if (tmp && (norun || regtry(prog, s)))
1327 while (s < strend) {
1328 if (isSPACE_LC(*s)) {
1329 if (tmp && (norun || regtry(prog, s)))
1342 LOAD_UTF8_CHARCLASS(space," ");
1343 while (s < strend) {
1344 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1345 if (tmp && (norun || regtry(prog, s)))
1356 while (s < strend) {
1358 if (tmp && (norun || regtry(prog, s)))
1370 PL_reg_flags |= RF_tainted;
1372 while (s < strend) {
1373 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1374 if (tmp && (norun || regtry(prog, s)))
1385 while (s < strend) {
1386 if (!isSPACE_LC(*s)) {
1387 if (tmp && (norun || regtry(prog, s)))
1400 LOAD_UTF8_CHARCLASS(digit,"0");
1401 while (s < strend) {
1402 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1403 if (tmp && (norun || regtry(prog, s)))
1414 while (s < strend) {
1416 if (tmp && (norun || regtry(prog, s)))
1428 PL_reg_flags |= RF_tainted;
1430 while (s < strend) {
1431 if (isDIGIT_LC_utf8((U8*)s)) {
1432 if (tmp && (norun || regtry(prog, s)))
1443 while (s < strend) {
1444 if (isDIGIT_LC(*s)) {
1445 if (tmp && (norun || regtry(prog, s)))
1458 LOAD_UTF8_CHARCLASS(digit,"0");
1459 while (s < strend) {
1460 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1461 if (tmp && (norun || regtry(prog, s)))
1472 while (s < strend) {
1474 if (tmp && (norun || regtry(prog, s)))
1486 PL_reg_flags |= RF_tainted;
1488 while (s < strend) {
1489 if (!isDIGIT_LC_utf8((U8*)s)) {
1490 if (tmp && (norun || regtry(prog, s)))
1501 while (s < strend) {
1502 if (!isDIGIT_LC(*s)) {
1503 if (tmp && (norun || regtry(prog, s)))
1515 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1524 - regexec_flags - match a regexp against a string
1527 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1528 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1529 /* strend: pointer to null at end of string */
1530 /* strbeg: real beginning of string */
1531 /* minend: end of match must be >=minend after stringarg. */
1532 /* data: May be used for some additional optimizations. */
1533 /* nosave: For optimizations. */
1536 register regnode *c;
1537 register char *startpos = stringarg;
1538 I32 minlen; /* must match at least this many chars */
1539 I32 dontbother = 0; /* how many characters not to try at end */
1540 /* I32 start_shift = 0; */ /* Offset of the start to find
1541 constant substr. */ /* CC */
1542 I32 end_shift = 0; /* Same for the end. */ /* CC */
1543 I32 scream_pos = -1; /* Internal iterator of scream. */
1545 SV* oreplsv = GvSV(PL_replgv);
1546 bool do_utf8 = DO_UTF8(sv);
1548 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1549 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1556 PL_regnarrate = DEBUG_r_TEST;
1559 /* Be paranoid... */
1560 if (prog == NULL || startpos == NULL) {
1561 Perl_croak(aTHX_ "NULL regexp parameter");
1565 minlen = prog->minlen;
1566 if (strend - startpos < minlen) {
1567 DEBUG_r(PerlIO_printf(Perl_debug_log,
1568 "String too short [regexec_flags]...\n"));
1572 /* Check validity of program. */
1573 if (UCHARAT(prog->program) != REG_MAGIC) {
1574 Perl_croak(aTHX_ "corrupted regexp program");
1578 PL_reg_eval_set = 0;
1581 if (prog->reganch & ROPT_UTF8)
1582 PL_reg_flags |= RF_utf8;
1584 /* Mark beginning of line for ^ and lookbehind. */
1585 PL_regbol = startpos;
1589 /* Mark end of line for $ (and such) */
1592 /* see how far we have to get to not match where we matched before */
1593 PL_regtill = startpos+minend;
1595 /* We start without call_cc context. */
1598 /* If there is a "must appear" string, look for it. */
1601 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1604 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1605 PL_reg_ganch = startpos;
1606 else if (sv && SvTYPE(sv) >= SVt_PVMG
1608 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1609 && mg->mg_len >= 0) {
1610 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1611 if (prog->reganch & ROPT_ANCH_GPOS) {
1612 if (s > PL_reg_ganch)
1617 else /* pos() not defined */
1618 PL_reg_ganch = strbeg;
1621 if (do_utf8 == (UTF!=0) &&
1622 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1623 re_scream_pos_data d;
1625 d.scream_olds = &scream_olds;
1626 d.scream_pos = &scream_pos;
1627 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1629 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1630 goto phooey; /* not present */
1636 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1637 UNI_DISPLAY_REGEX) :
1639 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1640 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1641 UNI_DISPLAY_REGEX) : startpos;
1642 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1645 PerlIO_printf(Perl_debug_log,
1646 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1647 PL_colors[4],PL_colors[5],PL_colors[0],
1650 len0 > 60 ? "..." : "",
1652 (int)(len1 > 60 ? 60 : len1),
1654 (len1 > 60 ? "..." : "")
1658 /* Simplest case: anchored match need be tried only once. */
1659 /* [unless only anchor is BOL and multiline is set] */
1660 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1661 if (s == startpos && regtry(prog, startpos))
1663 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1664 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1669 dontbother = minlen - 1;
1670 end = HOP3c(strend, -dontbother, strbeg) - 1;
1671 /* for multiline we only have to try after newlines */
1672 if (prog->check_substr) {
1676 if (regtry(prog, s))
1681 if (prog->reganch & RE_USE_INTUIT) {
1682 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1693 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1694 if (regtry(prog, s))
1701 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1702 if (regtry(prog, PL_reg_ganch))
1707 /* Messy cases: unanchored match. */
1708 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1709 /* we have /x+whatever/ */
1710 /* it must be a one character string (XXXX Except UTF?) */
1711 char ch = SvPVX(prog->anchored_substr)[0];
1717 while (s < strend) {
1719 DEBUG_r( did_match = 1 );
1720 if (regtry(prog, s)) goto got_it;
1722 while (s < strend && *s == ch)
1729 while (s < strend) {
1731 DEBUG_r( did_match = 1 );
1732 if (regtry(prog, s)) goto got_it;
1734 while (s < strend && *s == ch)
1740 DEBUG_r(if (!did_match)
1741 PerlIO_printf(Perl_debug_log,
1742 "Did not find anchored character...\n")
1746 else if (do_utf8 == (UTF!=0) &&
1747 (prog->anchored_substr != Nullsv
1748 || (prog->float_substr != Nullsv
1749 && prog->float_max_offset < strend - s))) {
1750 SV *must = prog->anchored_substr
1751 ? prog->anchored_substr : prog->float_substr;
1753 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1755 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1756 char *last = HOP3c(strend, /* Cannot start after this */
1757 -(I32)(CHR_SVLEN(must)
1758 - (SvTAIL(must) != 0) + back_min), strbeg);
1759 char *last1; /* Last position checked before */
1765 last1 = HOPc(s, -1);
1767 last1 = s - 1; /* bogus */
1769 /* XXXX check_substr already used to find `s', can optimize if
1770 check_substr==must. */
1772 dontbother = end_shift;
1773 strend = HOPc(strend, -dontbother);
1774 while ( (s <= last) &&
1775 ((flags & REXEC_SCREAM)
1776 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1777 end_shift, &scream_pos, 0))
1778 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1779 (unsigned char*)strend, must,
1780 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1781 DEBUG_r( did_match = 1 );
1782 if (HOPc(s, -back_max) > last1) {
1783 last1 = HOPc(s, -back_min);
1784 s = HOPc(s, -back_max);
1787 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1789 last1 = HOPc(s, -back_min);
1793 while (s <= last1) {
1794 if (regtry(prog, s))
1800 while (s <= last1) {
1801 if (regtry(prog, s))
1807 DEBUG_r(if (!did_match)
1808 PerlIO_printf(Perl_debug_log,
1809 "Did not find %s substr `%s%.*s%s'%s...\n",
1810 ((must == prog->anchored_substr)
1811 ? "anchored" : "floating"),
1813 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1815 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1819 else if ((c = prog->regstclass)) {
1820 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1821 /* don't bother with what can't match */
1822 strend = HOPc(strend, -(minlen - 1));
1824 SV *prop = sv_newmortal();
1832 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1833 UNI_DISPLAY_REGEX) :
1835 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1837 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1838 len1 = UTF ? SvCUR(dsv1) : strend - s;
1839 PerlIO_printf(Perl_debug_log,
1840 "Matching stclass `%*.*s' against `%*.*s'\n",
1844 if (find_byclass(prog, c, s, strend, startpos, 0))
1846 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1850 if (prog->float_substr != Nullsv) { /* Trim the end. */
1853 if (flags & REXEC_SCREAM) {
1854 last = screaminstr(sv, prog->float_substr, s - strbeg,
1855 end_shift, &scream_pos, 1); /* last one */
1857 last = scream_olds; /* Only one occurrence. */
1861 char *little = SvPV(prog->float_substr, len);
1863 if (SvTAIL(prog->float_substr)) {
1864 if (memEQ(strend - len + 1, little, len - 1))
1865 last = strend - len + 1;
1866 else if (!PL_multiline)
1867 last = memEQ(strend - len, little, len)
1868 ? strend - len : Nullch;
1874 last = rninstr(s, strend, little, little + len);
1876 last = strend; /* matching `$' */
1880 DEBUG_r(PerlIO_printf(Perl_debug_log,
1881 "%sCan't trim the tail, match fails (should not happen)%s\n",
1882 PL_colors[4],PL_colors[5]));
1883 goto phooey; /* Should not happen! */
1885 dontbother = strend - last + prog->float_min_offset;
1887 if (minlen && (dontbother < minlen))
1888 dontbother = minlen - 1;
1889 strend -= dontbother; /* this one's always in bytes! */
1890 /* We don't know much -- general case. */
1893 if (regtry(prog, s))
1902 if (regtry(prog, s))
1904 } while (s++ < strend);
1912 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1914 if (PL_reg_eval_set) {
1915 /* Preserve the current value of $^R */
1916 if (oreplsv != GvSV(PL_replgv))
1917 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1918 restored, the value remains
1920 restore_pos(aTHX_ 0);
1923 /* make sure $`, $&, $', and $digit will work later */
1924 if ( !(flags & REXEC_NOT_FIRST) ) {
1925 if (RX_MATCH_COPIED(prog)) {
1926 Safefree(prog->subbeg);
1927 RX_MATCH_COPIED_off(prog);
1929 if (flags & REXEC_COPY_STR) {
1930 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1932 s = savepvn(strbeg, i);
1935 RX_MATCH_COPIED_on(prog);
1938 prog->subbeg = strbeg;
1939 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1946 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1947 PL_colors[4],PL_colors[5]));
1948 if (PL_reg_eval_set)
1949 restore_pos(aTHX_ 0);
1954 - regtry - try match at specific point
1956 STATIC I32 /* 0 failure, 1 success */
1957 S_regtry(pTHX_ regexp *prog, char *startpos)
1965 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1967 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1970 PL_reg_eval_set = RS_init;
1972 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1973 (IV)(PL_stack_sp - PL_stack_base));
1975 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1976 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1977 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1979 /* Apparently this is not needed, judging by wantarray. */
1980 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1981 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1984 /* Make $_ available to executed code. */
1985 if (PL_reg_sv != DEFSV) {
1986 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1991 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1992 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1993 /* prepare for quick setting of pos */
1994 sv_magic(PL_reg_sv, (SV*)0,
1995 PERL_MAGIC_regex_global, Nullch, 0);
1996 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2000 PL_reg_oldpos = mg->mg_len;
2001 SAVEDESTRUCTOR_X(restore_pos, 0);
2003 if (!PL_reg_curpm) {
2004 Newz(22,PL_reg_curpm, 1, PMOP);
2007 SV* repointer = newSViv(0);
2008 /* so we know which PL_regex_padav element is PL_reg_curpm */
2009 SvFLAGS(repointer) |= SVf_BREAK;
2010 av_push(PL_regex_padav,repointer);
2011 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2012 PL_regex_pad = AvARRAY(PL_regex_padav);
2016 PM_SETRE(PL_reg_curpm, prog);
2017 PL_reg_oldcurpm = PL_curpm;
2018 PL_curpm = PL_reg_curpm;
2019 if (RX_MATCH_COPIED(prog)) {
2020 /* Here is a serious problem: we cannot rewrite subbeg,
2021 since it may be needed if this match fails. Thus
2022 $` inside (?{}) could fail... */
2023 PL_reg_oldsaved = prog->subbeg;
2024 PL_reg_oldsavedlen = prog->sublen;
2025 RX_MATCH_COPIED_off(prog);
2028 PL_reg_oldsaved = Nullch;
2029 prog->subbeg = PL_bostr;
2030 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2032 prog->startp[0] = startpos - PL_bostr;
2033 PL_reginput = startpos;
2034 PL_regstartp = prog->startp;
2035 PL_regendp = prog->endp;
2036 PL_reglastparen = &prog->lastparen;
2037 PL_reglastcloseparen = &prog->lastcloseparen;
2038 prog->lastparen = 0;
2040 DEBUG_r(PL_reg_starttry = startpos);
2041 if (PL_reg_start_tmpl <= prog->nparens) {
2042 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2043 if(PL_reg_start_tmp)
2044 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2046 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2050 sv_setpvn(PERL_DEBUG_PAD(0), "", 0);
2051 sv_setpvn(PERL_DEBUG_PAD(1), "", 0);
2052 sv_setpvn(PERL_DEBUG_PAD(2), "", 0);
2055 /* XXXX What this code is doing here?!!! There should be no need
2056 to do this again and again, PL_reglastparen should take care of
2059 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2060 * Actually, the code in regcppop() (which Ilya may be meaning by
2061 * PL_reglastparen), is not needed at all by the test suite
2062 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2063 * enough, for building DynaLoader, or otherwise this
2064 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2065 * will happen. Meanwhile, this code *is* needed for the
2066 * above-mentioned test suite tests to succeed. The common theme
2067 * on those tests seems to be returning null fields from matches.
2072 if (prog->nparens) {
2073 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2080 if (regmatch(prog->program + 1)) {
2081 prog->endp[0] = PL_reginput - PL_bostr;
2084 REGCP_UNWIND(lastcp);
2088 #define RE_UNWIND_BRANCH 1
2089 #define RE_UNWIND_BRANCHJ 2
2093 typedef struct { /* XX: makes sense to enlarge it... */
2097 } re_unwind_generic_t;
2110 } re_unwind_branch_t;
2112 typedef union re_unwind_t {
2114 re_unwind_generic_t generic;
2115 re_unwind_branch_t branch;
2118 #define sayYES goto yes
2119 #define sayNO goto no
2120 #define sayNO_ANYOF goto no_anyof
2121 #define sayYES_FINAL goto yes_final
2122 #define sayYES_LOUD goto yes_loud
2123 #define sayNO_FINAL goto no_final
2124 #define sayNO_SILENT goto do_no
2125 #define saySAME(x) if (x) goto yes; else goto no
2127 #define REPORT_CODE_OFF 24
2130 - regmatch - main matching routine
2132 * Conceptually the strategy is simple: check to see whether the current
2133 * node matches, call self recursively to see whether the rest matches,
2134 * and then act accordingly. In practice we make some effort to avoid
2135 * recursion, in particular by going through "ordinary" nodes (that don't
2136 * need to know whether the rest of the match failed) by a loop instead of
2139 /* [lwall] I've hoisted the register declarations to the outer block in order to
2140 * maybe save a little bit of pushing and popping on the stack. It also takes
2141 * advantage of machines that use a register save mask on subroutine entry.
2143 STATIC I32 /* 0 failure, 1 success */
2144 S_regmatch(pTHX_ regnode *prog)
2146 register regnode *scan; /* Current node. */
2147 regnode *next; /* Next node. */
2148 regnode *inner; /* Next node in internal branch. */
2149 register I32 nextchr; /* renamed nextchr - nextchar colides with
2150 function of same name */
2151 register I32 n; /* no or next */
2152 register I32 ln = 0; /* len or last */
2153 register char *s = Nullch; /* operand or save */
2154 register char *locinput = PL_reginput;
2155 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2156 int minmod = 0, sw = 0, logical = 0;
2159 I32 firstcp = PL_savestack_ix;
2161 register bool do_utf8 = PL_reg_match_utf8;
2163 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2164 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2165 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2172 /* Note that nextchr is a byte even in UTF */
2173 nextchr = UCHARAT(locinput);
2175 while (scan != NULL) {
2178 SV *prop = sv_newmortal();
2179 int docolor = *PL_colors[0];
2180 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2181 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2182 /* The part of the string before starttry has one color
2183 (pref0_len chars), between starttry and current
2184 position another one (pref_len - pref0_len chars),
2185 after the current position the third one.
2186 We assume that pref0_len <= pref_len, otherwise we
2187 decrease pref0_len. */
2188 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2189 ? (5 + taill) - l : locinput - PL_bostr;
2192 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2194 pref0_len = pref_len - (locinput - PL_reg_starttry);
2195 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2196 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2197 ? (5 + taill) - pref_len : PL_regeol - locinput);
2198 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2202 if (pref0_len > pref_len)
2203 pref0_len = pref_len;
2204 regprop(prop, scan);
2208 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2209 pref0_len, 60, UNI_DISPLAY_REGEX) :
2210 locinput - pref_len;
2211 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2212 char *s1 = do_utf8 ?
2213 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2214 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2215 locinput - pref_len + pref0_len;
2216 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2217 char *s2 = do_utf8 ?
2218 pv_uni_display(dsv2, (U8*)locinput,
2219 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2221 int len2 = do_utf8 ? strlen(s2) : l;
2222 PerlIO_printf(Perl_debug_log,
2223 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2224 (IV)(locinput - PL_bostr),
2231 (docolor ? "" : "> <"),
2235 15 - l - pref_len + 1,
2237 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2242 next = scan + NEXT_OFF(scan);
2248 if (locinput == PL_bostr || (PL_multiline &&
2249 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2251 /* regtill = regbol; */
2256 if (locinput == PL_bostr ||
2257 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2263 if (locinput == PL_bostr)
2267 if (locinput == PL_reg_ganch)
2277 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2282 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2284 if (PL_regeol - locinput > 1)
2288 if (PL_regeol != locinput)
2292 if (!nextchr && locinput >= PL_regeol)
2295 locinput += PL_utf8skip[nextchr];
2296 if (locinput > PL_regeol)
2298 nextchr = UCHARAT(locinput);
2301 nextchr = UCHARAT(++locinput);
2304 if (!nextchr && locinput >= PL_regeol)
2306 nextchr = UCHARAT(++locinput);
2309 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2312 locinput += PL_utf8skip[nextchr];
2313 if (locinput > PL_regeol)
2315 nextchr = UCHARAT(locinput);
2318 nextchr = UCHARAT(++locinput);
2323 if (do_utf8 != (UTF!=0)) {
2324 /* The target and the pattern have differing utf8ness. */
2330 /* The target is utf8, the pattern is not utf8. */
2334 if (NATIVE_TO_UNI(*(U8*)s) !=
2335 utf8_to_uvchr((U8*)l, &ulen))
2342 /* The target is not utf8, the pattern is utf8. */
2346 if (NATIVE_TO_UNI(*((U8*)l)) !=
2347 utf8_to_uvchr((U8*)s, &ulen))
2354 nextchr = UCHARAT(locinput);
2357 /* The target and the pattern have the same utf8ness. */
2358 /* Inline the first character, for speed. */
2359 if (UCHARAT(s) != nextchr)
2361 if (PL_regeol - locinput < ln)
2363 if (ln > 1 && memNE(s, locinput, ln))
2366 nextchr = UCHARAT(locinput);
2369 PL_reg_flags |= RF_tainted;
2375 if (do_utf8 || UTF) {
2376 /* Either target or the pattern are utf8. */
2378 char *e = PL_regeol;
2380 if (ibcmp_utf8(s, 0, ln, do_utf8,
2382 /* One more case for the sharp s:
2383 * pack("U0U*", 0xDF) =~ /ss/i,
2384 * the 0xC3 0x9F are the UTF-8
2385 * byte sequence for the U+00DF. */
2387 toLOWER(s[0]) == 's' &&
2389 toLOWER(s[1]) == 's' &&
2396 nextchr = UCHARAT(locinput);
2400 /* Neither the target and the pattern are utf8. */
2402 /* Inline the first character, for speed. */
2403 if (UCHARAT(s) != nextchr &&
2404 UCHARAT(s) != ((OP(scan) == EXACTF)
2405 ? PL_fold : PL_fold_locale)[nextchr])
2407 if (PL_regeol - locinput < ln)
2409 if (ln > 1 && (OP(scan) == EXACTF
2410 ? ibcmp(s, locinput, ln)
2411 : ibcmp_locale(s, locinput, ln)))
2414 nextchr = UCHARAT(locinput);
2418 STRLEN inclasslen = PL_regeol - locinput;
2420 if (!reginclasslen(scan, (U8*)locinput, &inclasslen, do_utf8))
2422 if (locinput >= PL_regeol)
2424 locinput += inclasslen;
2425 nextchr = UCHARAT(locinput);
2430 nextchr = UCHARAT(locinput);
2431 if (!reginclass(scan, (U8*)locinput, do_utf8))
2433 if (!nextchr && locinput >= PL_regeol)
2435 nextchr = UCHARAT(++locinput);
2439 /* If we might have the case of the German sharp s
2440 * in a casefolding Unicode character class. */
2442 if (ANYOF_UNICODE_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2444 nextchr = UCHARAT(locinput);
2450 PL_reg_flags |= RF_tainted;
2456 LOAD_UTF8_CHARCLASS(alnum,"a");
2457 if (!(OP(scan) == ALNUM
2458 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2459 : isALNUM_LC_utf8((U8*)locinput)))
2463 locinput += PL_utf8skip[nextchr];
2464 nextchr = UCHARAT(locinput);
2467 if (!(OP(scan) == ALNUM
2468 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2470 nextchr = UCHARAT(++locinput);
2473 PL_reg_flags |= RF_tainted;
2476 if (!nextchr && locinput >= PL_regeol)
2479 LOAD_UTF8_CHARCLASS(alnum,"a");
2480 if (OP(scan) == NALNUM
2481 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2482 : isALNUM_LC_utf8((U8*)locinput))
2486 locinput += PL_utf8skip[nextchr];
2487 nextchr = UCHARAT(locinput);
2490 if (OP(scan) == NALNUM
2491 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2493 nextchr = UCHARAT(++locinput);
2497 PL_reg_flags |= RF_tainted;
2501 /* was last char in word? */
2503 if (locinput == PL_bostr)
2506 U8 *r = reghop((U8*)locinput, -1);
2508 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2510 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2511 ln = isALNUM_uni(ln);
2512 LOAD_UTF8_CHARCLASS(alnum,"a");
2513 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2516 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2517 n = isALNUM_LC_utf8((U8*)locinput);
2521 ln = (locinput != PL_bostr) ?
2522 UCHARAT(locinput - 1) : '\n';
2523 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2525 n = isALNUM(nextchr);
2528 ln = isALNUM_LC(ln);
2529 n = isALNUM_LC(nextchr);
2532 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2533 OP(scan) == BOUNDL))
2537 PL_reg_flags |= RF_tainted;
2543 if (UTF8_IS_CONTINUED(nextchr)) {
2544 LOAD_UTF8_CHARCLASS(space," ");
2545 if (!(OP(scan) == SPACE
2546 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2547 : isSPACE_LC_utf8((U8*)locinput)))
2551 locinput += PL_utf8skip[nextchr];
2552 nextchr = UCHARAT(locinput);
2555 if (!(OP(scan) == SPACE
2556 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2558 nextchr = UCHARAT(++locinput);
2561 if (!(OP(scan) == SPACE
2562 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2564 nextchr = UCHARAT(++locinput);
2568 PL_reg_flags |= RF_tainted;
2571 if (!nextchr && locinput >= PL_regeol)
2574 LOAD_UTF8_CHARCLASS(space," ");
2575 if (OP(scan) == NSPACE
2576 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2577 : isSPACE_LC_utf8((U8*)locinput))
2581 locinput += PL_utf8skip[nextchr];
2582 nextchr = UCHARAT(locinput);
2585 if (OP(scan) == NSPACE
2586 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2588 nextchr = UCHARAT(++locinput);
2591 PL_reg_flags |= RF_tainted;
2597 LOAD_UTF8_CHARCLASS(digit,"0");
2598 if (!(OP(scan) == DIGIT
2599 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2600 : isDIGIT_LC_utf8((U8*)locinput)))
2604 locinput += PL_utf8skip[nextchr];
2605 nextchr = UCHARAT(locinput);
2608 if (!(OP(scan) == DIGIT
2609 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2611 nextchr = UCHARAT(++locinput);
2614 PL_reg_flags |= RF_tainted;
2617 if (!nextchr && locinput >= PL_regeol)
2620 LOAD_UTF8_CHARCLASS(digit,"0");
2621 if (OP(scan) == NDIGIT
2622 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2623 : isDIGIT_LC_utf8((U8*)locinput))
2627 locinput += PL_utf8skip[nextchr];
2628 nextchr = UCHARAT(locinput);
2631 if (OP(scan) == NDIGIT
2632 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2634 nextchr = UCHARAT(++locinput);
2637 if (locinput >= PL_regeol)
2640 LOAD_UTF8_CHARCLASS(mark,"~");
2641 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2643 locinput += PL_utf8skip[nextchr];
2644 while (locinput < PL_regeol &&
2645 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2646 locinput += UTF8SKIP(locinput);
2647 if (locinput > PL_regeol)
2652 nextchr = UCHARAT(locinput);
2655 PL_reg_flags |= RF_tainted;
2659 n = ARG(scan); /* which paren pair */
2660 ln = PL_regstartp[n];
2661 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2662 if (*PL_reglastparen < n || ln == -1)
2663 sayNO; /* Do not match unless seen CLOSEn. */
2664 if (ln == PL_regendp[n])
2668 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2670 char *e = PL_bostr + PL_regendp[n];
2672 * Note that we can't do the "other character" lookup trick as
2673 * in the 8-bit case (no pun intended) because in Unicode we
2674 * have to map both upper and title case to lower case.
2676 if (OP(scan) == REFF) {
2677 STRLEN ulen1, ulen2;
2678 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2679 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2683 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2684 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2685 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2692 nextchr = UCHARAT(locinput);
2696 /* Inline the first character, for speed. */
2697 if (UCHARAT(s) != nextchr &&
2699 (UCHARAT(s) != ((OP(scan) == REFF
2700 ? PL_fold : PL_fold_locale)[nextchr]))))
2702 ln = PL_regendp[n] - ln;
2703 if (locinput + ln > PL_regeol)
2705 if (ln > 1 && (OP(scan) == REF
2706 ? memNE(s, locinput, ln)
2708 ? ibcmp(s, locinput, ln)
2709 : ibcmp_locale(s, locinput, ln))))
2712 nextchr = UCHARAT(locinput);
2723 OP_4tree *oop = PL_op;
2724 COP *ocurcop = PL_curcop;
2725 SV **ocurpad = PL_curpad;
2729 PL_op = (OP_4tree*)PL_regdata->data[n];
2730 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2731 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2732 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2736 CALLRUNOPS(aTHX); /* Scalar context. */
2739 ret = Nullsv; /* protect against empty (?{}) blocks. */
2747 PL_curpad = ocurpad;
2748 PL_curcop = ocurcop;
2750 if (logical == 2) { /* Postponed subexpression. */
2752 MAGIC *mg = Null(MAGIC*);
2754 CHECKPOINT cp, lastcp;
2756 if(SvROK(ret) || SvRMAGICAL(ret)) {
2757 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2760 mg = mg_find(sv, PERL_MAGIC_qr);
2763 re = (regexp *)mg->mg_obj;
2764 (void)ReREFCNT_inc(re);
2768 char *t = SvPV(ret, len);
2770 char *oprecomp = PL_regprecomp;
2771 I32 osize = PL_regsize;
2772 I32 onpar = PL_regnpar;
2775 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2777 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2778 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2780 PL_regprecomp = oprecomp;
2785 PerlIO_printf(Perl_debug_log,
2786 "Entering embedded `%s%.60s%s%s'\n",
2790 (strlen(re->precomp) > 60 ? "..." : ""))
2793 state.prev = PL_reg_call_cc;
2794 state.cc = PL_regcc;
2795 state.re = PL_reg_re;
2799 cp = regcppush(0); /* Save *all* the positions. */
2802 state.ss = PL_savestack_ix;
2803 *PL_reglastparen = 0;
2804 *PL_reglastcloseparen = 0;
2805 PL_reg_call_cc = &state;
2806 PL_reginput = locinput;
2808 /* XXXX This is too dramatic a measure... */
2811 if (regmatch(re->program + 1)) {
2812 /* Even though we succeeded, we need to restore
2813 global variables, since we may be wrapped inside
2814 SUSPEND, thus the match may be not finished yet. */
2816 /* XXXX Do this only if SUSPENDed? */
2817 PL_reg_call_cc = state.prev;
2818 PL_regcc = state.cc;
2819 PL_reg_re = state.re;
2820 cache_re(PL_reg_re);
2822 /* XXXX This is too dramatic a measure... */
2825 /* These are needed even if not SUSPEND. */
2831 REGCP_UNWIND(lastcp);
2833 PL_reg_call_cc = state.prev;
2834 PL_regcc = state.cc;
2835 PL_reg_re = state.re;
2836 cache_re(PL_reg_re);
2838 /* XXXX This is too dramatic a measure... */
2848 sv_setsv(save_scalar(PL_replgv), ret);
2852 n = ARG(scan); /* which paren pair */
2853 PL_reg_start_tmp[n] = locinput;
2858 n = ARG(scan); /* which paren pair */
2859 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2860 PL_regendp[n] = locinput - PL_bostr;
2861 if (n > *PL_reglastparen)
2862 *PL_reglastparen = n;
2863 *PL_reglastcloseparen = n;
2866 n = ARG(scan); /* which paren pair */
2867 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2870 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2872 next = NEXTOPER(NEXTOPER(scan));
2874 next = scan + ARG(scan);
2875 if (OP(next) == IFTHEN) /* Fake one. */
2876 next = NEXTOPER(NEXTOPER(next));
2880 logical = scan->flags;
2882 /*******************************************************************
2883 PL_regcc contains infoblock about the innermost (...)* loop, and
2884 a pointer to the next outer infoblock.
2886 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2888 1) After matching X, regnode for CURLYX is processed;
2890 2) This regnode creates infoblock on the stack, and calls
2891 regmatch() recursively with the starting point at WHILEM node;
2893 3) Each hit of WHILEM node tries to match A and Z (in the order
2894 depending on the current iteration, min/max of {min,max} and
2895 greediness). The information about where are nodes for "A"
2896 and "Z" is read from the infoblock, as is info on how many times "A"
2897 was already matched, and greediness.
2899 4) After A matches, the same WHILEM node is hit again.
2901 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2902 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2903 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2904 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2905 of the external loop.
2907 Currently present infoblocks form a tree with a stem formed by PL_curcc
2908 and whatever it mentions via ->next, and additional attached trees
2909 corresponding to temporarily unset infoblocks as in "5" above.
2911 In the following picture infoblocks for outer loop of
2912 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2913 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2914 infoblocks are drawn below the "reset" infoblock.
2916 In fact in the picture below we do not show failed matches for Z and T
2917 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2918 more obvious *why* one needs to *temporary* unset infoblocks.]
2920 Matched REx position InfoBlocks Comment
2924 Y A)*?Z)*?T x <- O <- I
2925 YA )*?Z)*?T x <- O <- I
2926 YA A)*?Z)*?T x <- O <- I
2927 YAA )*?Z)*?T x <- O <- I
2928 YAA Z)*?T x <- O # Temporary unset I
2931 YAAZ Y(A)*?Z)*?T x <- O
2934 YAAZY (A)*?Z)*?T x <- O
2937 YAAZY A)*?Z)*?T x <- O <- I
2940 YAAZYA )*?Z)*?T x <- O <- I
2943 YAAZYA Z)*?T x <- O # Temporary unset I
2949 YAAZYAZ T x # Temporary unset O
2956 *******************************************************************/
2959 CHECKPOINT cp = PL_savestack_ix;
2960 /* No need to save/restore up to this paren */
2961 I32 parenfloor = scan->flags;
2963 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2965 cc.oldcc = PL_regcc;
2967 /* XXXX Probably it is better to teach regpush to support
2968 parenfloor > PL_regsize... */
2969 if (parenfloor > *PL_reglastparen)
2970 parenfloor = *PL_reglastparen; /* Pessimization... */
2971 cc.parenfloor = parenfloor;
2973 cc.min = ARG1(scan);
2974 cc.max = ARG2(scan);
2975 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2979 PL_reginput = locinput;
2980 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2982 PL_regcc = cc.oldcc;
2988 * This is really hard to understand, because after we match
2989 * what we're trying to match, we must make sure the rest of
2990 * the REx is going to match for sure, and to do that we have
2991 * to go back UP the parse tree by recursing ever deeper. And
2992 * if it fails, we have to reset our parent's current state
2993 * that we can try again after backing off.
2996 CHECKPOINT cp, lastcp;
2997 CURCUR* cc = PL_regcc;
2998 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3000 n = cc->cur + 1; /* how many we know we matched */
3001 PL_reginput = locinput;
3004 PerlIO_printf(Perl_debug_log,
3005 "%*s %ld out of %ld..%ld cc=%lx\n",
3006 REPORT_CODE_OFF+PL_regindent*2, "",
3007 (long)n, (long)cc->min,
3008 (long)cc->max, (long)cc)
3011 /* If degenerate scan matches "", assume scan done. */
3013 if (locinput == cc->lastloc && n >= cc->min) {
3014 PL_regcc = cc->oldcc;
3018 PerlIO_printf(Perl_debug_log,
3019 "%*s empty match detected, try continuation...\n",
3020 REPORT_CODE_OFF+PL_regindent*2, "")
3022 if (regmatch(cc->next))
3030 /* First just match a string of min scans. */
3034 cc->lastloc = locinput;
3035 if (regmatch(cc->scan))
3038 cc->lastloc = lastloc;
3043 /* Check whether we already were at this position.
3044 Postpone detection until we know the match is not
3045 *that* much linear. */
3046 if (!PL_reg_maxiter) {
3047 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3048 PL_reg_leftiter = PL_reg_maxiter;
3050 if (PL_reg_leftiter-- == 0) {
3051 I32 size = (PL_reg_maxiter + 7)/8;
3052 if (PL_reg_poscache) {
3053 if (PL_reg_poscache_size < size) {
3054 Renew(PL_reg_poscache, size, char);
3055 PL_reg_poscache_size = size;
3057 Zero(PL_reg_poscache, size, char);
3060 PL_reg_poscache_size = size;
3061 Newz(29, PL_reg_poscache, size, char);
3064 PerlIO_printf(Perl_debug_log,
3065 "%sDetected a super-linear match, switching on caching%s...\n",
3066 PL_colors[4], PL_colors[5])
3069 if (PL_reg_leftiter < 0) {
3070 I32 o = locinput - PL_bostr, b;
3072 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3075 if (PL_reg_poscache[o] & (1<<b)) {
3077 PerlIO_printf(Perl_debug_log,
3078 "%*s already tried at this position...\n",
3079 REPORT_CODE_OFF+PL_regindent*2, "")
3083 PL_reg_poscache[o] |= (1<<b);
3087 /* Prefer next over scan for minimal matching. */
3090 PL_regcc = cc->oldcc;
3093 cp = regcppush(cc->parenfloor);
3095 if (regmatch(cc->next)) {
3097 sayYES; /* All done. */
3099 REGCP_UNWIND(lastcp);
3105 if (n >= cc->max) { /* Maximum greed exceeded? */
3106 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3107 && !(PL_reg_flags & RF_warned)) {
3108 PL_reg_flags |= RF_warned;
3109 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3110 "Complex regular subexpression recursion",
3117 PerlIO_printf(Perl_debug_log,
3118 "%*s trying longer...\n",
3119 REPORT_CODE_OFF+PL_regindent*2, "")
3121 /* Try scanning more and see if it helps. */
3122 PL_reginput = locinput;
3124 cc->lastloc = locinput;
3125 cp = regcppush(cc->parenfloor);
3127 if (regmatch(cc->scan)) {
3131 REGCP_UNWIND(lastcp);
3134 cc->lastloc = lastloc;
3138 /* Prefer scan over next for maximal matching. */
3140 if (n < cc->max) { /* More greed allowed? */
3141 cp = regcppush(cc->parenfloor);
3143 cc->lastloc = locinput;
3145 if (regmatch(cc->scan)) {
3149 REGCP_UNWIND(lastcp);
3150 regcppop(); /* Restore some previous $<digit>s? */
3151 PL_reginput = locinput;
3153 PerlIO_printf(Perl_debug_log,
3154 "%*s failed, try continuation...\n",
3155 REPORT_CODE_OFF+PL_regindent*2, "")
3158 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3159 && !(PL_reg_flags & RF_warned)) {
3160 PL_reg_flags |= RF_warned;
3161 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3162 "Complex regular subexpression recursion",
3166 /* Failed deeper matches of scan, so see if this one works. */
3167 PL_regcc = cc->oldcc;
3170 if (regmatch(cc->next))
3176 cc->lastloc = lastloc;
3181 next = scan + ARG(scan);
3184 inner = NEXTOPER(NEXTOPER(scan));
3187 inner = NEXTOPER(scan);
3191 if (OP(next) != c1) /* No choice. */
3192 next = inner; /* Avoid recursion. */
3194 I32 lastparen = *PL_reglastparen;
3196 re_unwind_branch_t *uw;
3198 /* Put unwinding data on stack */
3199 unwind1 = SSNEWt(1,re_unwind_branch_t);
3200 uw = SSPTRt(unwind1,re_unwind_branch_t);
3203 uw->type = ((c1 == BRANCH)
3205 : RE_UNWIND_BRANCHJ);
3206 uw->lastparen = lastparen;
3208 uw->locinput = locinput;
3209 uw->nextchr = nextchr;
3211 uw->regindent = ++PL_regindent;
3214 REGCP_SET(uw->lastcp);
3216 /* Now go into the first branch */
3229 /* We suppose that the next guy does not need
3230 backtracking: in particular, it is of constant length,
3231 and has no parenths to influence future backrefs. */
3232 ln = ARG1(scan); /* min to match */
3233 n = ARG2(scan); /* max to match */
3234 paren = scan->flags;
3236 if (paren > PL_regsize)
3238 if (paren > *PL_reglastparen)
3239 *PL_reglastparen = paren;
3241 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3243 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3244 PL_reginput = locinput;
3247 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3249 /* if we matched something zero-length we don't need to
3250 backtrack - capturing parens are already defined, so
3251 the caveat in the maximal case doesn't apply
3253 XXXX if ln == 0, we can redo this check first time
3254 through the following loop
3257 n = ln; /* don't backtrack */
3258 locinput = PL_reginput;
3259 if (HAS_TEXT(next) || JUMPABLE(next)) {
3260 regnode *text_node = next;
3262 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3264 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3266 if (PL_regkind[(U8)OP(text_node)] == REF) {
3268 n = ARG(text_node); /* which paren pair */
3269 ln = PL_regstartp[n];
3270 /* assume yes if we haven't seen CLOSEn */
3272 *PL_reglastparen < n ||
3279 c1 = *(PL_bostr + ln);
3281 else { c1 = (U8)*STRING(text_node); }
3282 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3284 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3285 c2 = PL_fold_locale[c1];
3294 /* This may be improved if l == 0. */
3295 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3296 /* If it could work, try it. */
3298 UCHARAT(PL_reginput) == c1 ||
3299 UCHARAT(PL_reginput) == c2)
3303 PL_regstartp[paren] =
3304 HOPc(PL_reginput, -l) - PL_bostr;
3305 PL_regendp[paren] = PL_reginput - PL_bostr;
3308 PL_regendp[paren] = -1;
3312 REGCP_UNWIND(lastcp);
3314 /* Couldn't or didn't -- move forward. */
3315 PL_reginput = locinput;
3316 if (regrepeat_hard(scan, 1, &l)) {
3318 locinput = PL_reginput;
3325 n = regrepeat_hard(scan, n, &l);
3326 /* if we matched something zero-length we don't need to
3327 backtrack, unless the minimum count is zero and we
3328 are capturing the result - in that case the capture
3329 being defined or not may affect later execution
3331 if (n != 0 && l == 0 && !(paren && ln == 0))
3332 ln = n; /* don't backtrack */
3333 locinput = PL_reginput;
3335 PerlIO_printf(Perl_debug_log,
3336 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3337 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3341 if (HAS_TEXT(next) || JUMPABLE(next)) {
3342 regnode *text_node = next;
3344 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3346 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3348 if (PL_regkind[(U8)OP(text_node)] == REF) {
3350 n = ARG(text_node); /* which paren pair */
3351 ln = PL_regstartp[n];
3352 /* assume yes if we haven't seen CLOSEn */
3354 *PL_reglastparen < n ||
3361 c1 = *(PL_bostr + ln);
3363 else { c1 = (U8)*STRING(text_node); }
3365 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3367 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3368 c2 = PL_fold_locale[c1];
3379 /* If it could work, try it. */
3381 UCHARAT(PL_reginput) == c1 ||
3382 UCHARAT(PL_reginput) == c2)
3385 PerlIO_printf(Perl_debug_log,
3386 "%*s trying tail with n=%"IVdf"...\n",
3387 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3391 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3392 PL_regendp[paren] = PL_reginput - PL_bostr;
3395 PL_regendp[paren] = -1;
3399 REGCP_UNWIND(lastcp);
3401 /* Couldn't or didn't -- back up. */
3403 locinput = HOPc(locinput, -l);
3404 PL_reginput = locinput;
3411 paren = scan->flags; /* Which paren to set */
3412 if (paren > PL_regsize)
3414 if (paren > *PL_reglastparen)
3415 *PL_reglastparen = paren;
3416 ln = ARG1(scan); /* min to match */
3417 n = ARG2(scan); /* max to match */
3418 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3422 ln = ARG1(scan); /* min to match */
3423 n = ARG2(scan); /* max to match */
3424 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3429 scan = NEXTOPER(scan);
3435 scan = NEXTOPER(scan);
3439 * Lookahead to avoid useless match attempts
3440 * when we know what character comes next.
3444 * Used to only do .*x and .*?x, but now it allows
3445 * for )'s, ('s and (?{ ... })'s to be in the way
3446 * of the quantifier and the EXACT-like node. -- japhy
3449 if (HAS_TEXT(next) || JUMPABLE(next)) {
3451 regnode *text_node = next;
3453 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3455 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3457 if (PL_regkind[(U8)OP(text_node)] == REF) {
3459 n = ARG(text_node); /* which paren pair */
3460 ln = PL_regstartp[n];
3461 /* assume yes if we haven't seen CLOSEn */
3463 *PL_reglastparen < n ||
3468 goto assume_ok_easy;
3470 s = (U8*)PL_bostr + ln;
3472 else { s = (U8*)STRING(text_node); }
3476 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3478 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3479 c2 = PL_fold_locale[c1];
3482 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3483 STRLEN ulen1, ulen2;
3484 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3485 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3487 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3488 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3490 c1 = utf8_to_uvuni(tmpbuf1, 0);
3491 c2 = utf8_to_uvuni(tmpbuf2, 0);
3494 c2 = c1 = utf8_to_uvchr(s, NULL);
3502 PL_reginput = locinput;
3506 if (ln && regrepeat(scan, ln) < ln)
3508 locinput = PL_reginput;
3511 char *e; /* Should not check after this */
3512 char *old = locinput;
3514 if (n == REG_INFTY) {
3517 while (UTF8_IS_CONTINUATION(*(U8*)e))
3523 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3527 e = locinput + n - ln;
3533 /* Find place 'next' could work */
3536 while (locinput <= e &&
3537 UCHARAT(locinput) != c1)
3540 while (locinput <= e
3541 && UCHARAT(locinput) != c1
3542 && UCHARAT(locinput) != c2)
3545 count = locinput - old;
3552 utf8_to_uvchr((U8*)locinput, &len) != c1;
3557 for (count = 0; locinput <= e; count++) {
3558 UV c = utf8_to_uvchr((U8*)locinput, &len);
3559 if (c == c1 || c == c2)
3567 /* PL_reginput == old now */
3568 if (locinput != old) {
3569 ln = 1; /* Did some */
3570 if (regrepeat(scan, count) < count)
3573 /* PL_reginput == locinput now */
3574 TRYPAREN(paren, ln, locinput);
3575 PL_reginput = locinput; /* Could be reset... */
3576 REGCP_UNWIND(lastcp);
3577 /* Couldn't or didn't -- move forward. */
3580 locinput += UTF8SKIP(locinput);
3586 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3590 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3592 c = UCHARAT(PL_reginput);
3593 /* If it could work, try it. */
3594 if (c == c1 || c == c2)
3596 TRYPAREN(paren, n, PL_reginput);
3597 REGCP_UNWIND(lastcp);
3600 /* If it could work, try it. */
3601 else if (c1 == -1000)
3603 TRYPAREN(paren, n, PL_reginput);
3604 REGCP_UNWIND(lastcp);
3606 /* Couldn't or didn't -- move forward. */
3607 PL_reginput = locinput;
3608 if (regrepeat(scan, 1)) {
3610 locinput = PL_reginput;
3618 n = regrepeat(scan, n);
3619 locinput = PL_reginput;
3620 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3621 ((!PL_multiline && OP(next) != MEOL) ||
3622 OP(next) == SEOL || OP(next) == EOS))
3624 ln = n; /* why back off? */
3625 /* ...because $ and \Z can match before *and* after
3626 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3627 We should back off by one in this case. */
3628 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3637 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3639 c = UCHARAT(PL_reginput);
3641 /* If it could work, try it. */
3642 if (c1 == -1000 || c == c1 || c == c2)
3644 TRYPAREN(paren, n, PL_reginput);
3645 REGCP_UNWIND(lastcp);
3647 /* Couldn't or didn't -- back up. */
3649 PL_reginput = locinput = HOPc(locinput, -1);
3657 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3659 c = UCHARAT(PL_reginput);
3661 /* If it could work, try it. */
3662 if (c1 == -1000 || c == c1 || c == c2)
3664 TRYPAREN(paren, n, PL_reginput);
3665 REGCP_UNWIND(lastcp);
3667 /* Couldn't or didn't -- back up. */
3669 PL_reginput = locinput = HOPc(locinput, -1);
3676 if (PL_reg_call_cc) {
3677 re_cc_state *cur_call_cc = PL_reg_call_cc;
3678 CURCUR *cctmp = PL_regcc;
3679 regexp *re = PL_reg_re;
3680 CHECKPOINT cp, lastcp;
3682 cp = regcppush(0); /* Save *all* the positions. */
3684 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3686 PL_reginput = locinput; /* Make position available to
3688 cache_re(PL_reg_call_cc->re);
3689 PL_regcc = PL_reg_call_cc->cc;
3690 PL_reg_call_cc = PL_reg_call_cc->prev;
3691 if (regmatch(cur_call_cc->node)) {
3692 PL_reg_call_cc = cur_call_cc;
3696 REGCP_UNWIND(lastcp);
3698 PL_reg_call_cc = cur_call_cc;
3704 PerlIO_printf(Perl_debug_log,
3705 "%*s continuation failed...\n",
3706 REPORT_CODE_OFF+PL_regindent*2, "")
3710 if (locinput < PL_regtill) {
3711 DEBUG_r(PerlIO_printf(Perl_debug_log,
3712 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3714 (long)(locinput - PL_reg_starttry),
3715 (long)(PL_regtill - PL_reg_starttry),
3717 sayNO_FINAL; /* Cannot match: too short. */
3719 PL_reginput = locinput; /* put where regtry can find it */
3720 sayYES_FINAL; /* Success! */
3722 PL_reginput = locinput; /* put where regtry can find it */
3723 sayYES_LOUD; /* Success! */
3726 PL_reginput = locinput;
3731 s = HOPBACKc(locinput, scan->flags);
3737 PL_reginput = locinput;
3742 s = HOPBACKc(locinput, scan->flags);
3748 PL_reginput = locinput;
3751 inner = NEXTOPER(NEXTOPER(scan));
3752 if (regmatch(inner) != n) {
3767 if (OP(scan) == SUSPEND) {
3768 locinput = PL_reginput;
3769 nextchr = UCHARAT(locinput);
3774 next = scan + ARG(scan);
3779 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3780 PTR2UV(scan), OP(scan));
3781 Perl_croak(aTHX_ "regexp memory corruption");
3788 * We get here only if there's trouble -- normally "case END" is
3789 * the terminating point.
3791 Perl_croak(aTHX_ "corrupted regexp pointers");
3797 PerlIO_printf(Perl_debug_log,
3798 "%*s %scould match...%s\n",
3799 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3803 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3804 PL_colors[4],PL_colors[5]));
3810 #if 0 /* Breaks $^R */
3818 PerlIO_printf(Perl_debug_log,
3819 "%*s %sfailed...%s\n",
3820 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3826 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3829 case RE_UNWIND_BRANCH:
3830 case RE_UNWIND_BRANCHJ:
3832 re_unwind_branch_t *uwb = &(uw->branch);
3833 I32 lastparen = uwb->lastparen;
3835 REGCP_UNWIND(uwb->lastcp);
3836 for (n = *PL_reglastparen; n > lastparen; n--)
3838 *PL_reglastparen = n;
3839 scan = next = uwb->next;
3841 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3842 ? BRANCH : BRANCHJ) ) { /* Failure */
3849 /* Have more choice yet. Reuse the same uwb. */
3851 if ((n = (uwb->type == RE_UNWIND_BRANCH
3852 ? NEXT_OFF(next) : ARG(next))))
3855 next = NULL; /* XXXX Needn't unwinding in this case... */
3857 next = NEXTOPER(scan);
3858 if (uwb->type == RE_UNWIND_BRANCHJ)
3859 next = NEXTOPER(next);
3860 locinput = uwb->locinput;
3861 nextchr = uwb->nextchr;
3863 PL_regindent = uwb->regindent;
3870 Perl_croak(aTHX_ "regexp unwind memory corruption");
3881 - regrepeat - repeatedly match something simple, report how many
3884 * [This routine now assumes that it will only match on things of length 1.
3885 * That was true before, but now we assume scan - reginput is the count,
3886 * rather than incrementing count on every character. [Er, except utf8.]]
3889 S_regrepeat(pTHX_ regnode *p, I32 max)
3891 register char *scan;
3893 register char *loceol = PL_regeol;
3894 register I32 hardcount = 0;
3895 register bool do_utf8 = PL_reg_match_utf8;