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 */
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 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregexec my_pregexec
44 * pregcomp and pregexec -- regsub and regerror are not used in perl
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
63 **** Alterations to Henry's code are...
65 **** Copyright (c) 1991-1997, Larry Wall
67 **** You may distribute under the terms of either the GNU General Public
68 **** License or the Artistic License, as specified in the README file.
70 * Beware that some of this code is subtly aware of the way operator
71 * precedence is structured in regular expressions. Serious changes in
72 * regular-expression syntax might require a total rethink.
78 #define RF_tainted 1 /* tainted information used? */
79 #define RF_warned 2 /* warned about big count? */
80 #define RF_evaled 4 /* Did an EVAL with setting? */
82 #define RS_init 1 /* eval environment created */
83 #define RS_set 2 /* replsv value is set */
90 typedef I32 CHECKPOINT;
96 static I32 regmatch _((regnode *prog));
97 static I32 regrepeat _((regnode *p, I32 max));
98 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
99 static I32 regtry _((regexp *prog, char *startpos));
101 static bool reginclass _((char *p, I32 c));
102 static CHECKPOINT regcppush _((I32 parenfloor));
103 static char * regcppop _((void));
105 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
108 regcppush(I32 parenfloor)
111 int retval = savestack_ix;
112 int i = (regsize - parenfloor) * 4;
116 for (p = regsize; p > parenfloor; p--) {
117 SSPUSHPTR(regendp[p]);
118 SSPUSHPTR(regstartp[p]);
119 SSPUSHPTR(reg_start_tmp[p]);
123 SSPUSHINT(*reglastparen);
126 SSPUSHINT(SAVEt_REGCONTEXT);
130 /* These are needed since we do not localize EVAL nodes: */
131 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
132 " Setting an EVAL scope, savestack=%i\n", \
133 savestack_ix)); lastcp = savestack_ix
135 # define REGCP_UNWIND DEBUG_r(lastcp != savestack_ix ? \
136 PerlIO_printf(Perl_debug_log, \
137 " Clearing an EVAL scope, savestack=%i..%i\n", \
138 lastcp, savestack_ix) : 0); regcpblow(lastcp)
148 assert(i == SAVEt_REGCONTEXT);
150 input = (char *) SSPOPPTR;
151 *reglastparen = SSPOPINT;
153 for (i -= 3; i > 0; i -= 4) {
154 paren = (U32)SSPOPINT;
155 reg_start_tmp[paren] = (char *) SSPOPPTR;
156 regstartp[paren] = (char *) SSPOPPTR;
157 tmps = (char*)SSPOPPTR;
158 if (paren <= *reglastparen)
159 regendp[paren] = tmps;
161 PerlIO_printf(Perl_debug_log,
162 " restoring \\%d to %d(%d)..%d%s\n",
163 paren, regstartp[paren] - regbol,
164 reg_start_tmp[paren] - regbol,
165 regendp[paren] - regbol,
166 (paren > *reglastparen ? "(no)" : ""));
170 if (*reglastparen + 1 <= regnpar) {
171 PerlIO_printf(Perl_debug_log,
172 " restoring \\%d..\\%d to undef\n",
173 *reglastparen + 1, regnpar);
176 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
178 regstartp[paren] = Nullch;
179 regendp[paren] = Nullch;
184 #define regcpblow(cp) LEAVE_SCOPE(cp)
187 * pregexec and friends
191 - pregexec - match a regexp against a string
194 pregexec(register regexp *prog, char *stringarg, register char *strend,
195 char *strbeg, I32 minend, SV *screamer, U32 nosave)
196 /* strend: pointer to null at end of string */
197 /* strbeg: real beginning of string */
198 /* minend: end of match must be >=minend after stringarg. */
199 /* nosave: For optimizations. */
202 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
203 nosave ? 0 : REXEC_COPY_STR);
207 - regexec_flags - match a regexp against a string
210 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
211 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
212 /* strend: pointer to null at end of string */
213 /* strbeg: real beginning of string */
214 /* minend: end of match must be >=minend after stringarg. */
215 /* data: May be used for some additional optimizations. */
216 /* nosave: For optimizations. */
221 register char *startpos = stringarg;
223 I32 minlen; /* must match at least this many chars */
224 I32 dontbother = 0; /* how many characters not to try at end */
226 I32 start_shift = 0; /* Offset of the start to find
228 I32 end_shift = 0; /* Same for the end. */
229 I32 scream_pos = -1; /* Internal iterator of scream. */
231 SV* oreplsv = GvSV(replgv);
237 regprecomp = prog->precomp; /* Needed for error messages. */
239 regnarrate = debug & 512;
240 regprogram = prog->program;
244 if (prog == NULL || startpos == NULL) {
245 croak("NULL regexp parameter");
249 minlen = prog->minlen;
250 if (strend - startpos < minlen) goto phooey;
252 if (startpos == strbeg) /* is ^ valid at stringarg? */
255 regprev = stringarg[-1];
256 if (!multiline && regprev == '\n')
257 regprev = '\0'; /* force ^ to NOT match */
260 /* Check validity of program. */
261 if (UCHARAT(prog->program) != MAGIC) {
262 FAIL("corrupted regexp program");
265 regnpar = prog->nparens;
269 /* If there is a "must appear" string, look for it. */
271 if (!(flags & REXEC_CHECKED)
272 && prog->check_substr != Nullsv &&
273 !(prog->reganch & ROPT_ANCH_GPOS) &&
274 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
275 || (multiline && prog->check_substr == prog->anchored_substr)) )
277 start_shift = prog->check_offset_min;
278 /* Should be nonnegative! */
279 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
281 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
282 s = screaminstr(screamer, prog->check_substr,
283 start_shift + (stringarg - strbeg),
284 end_shift, &scream_pos, 0);
290 s = fbm_instr((unsigned char*)s + start_shift,
291 (unsigned char*)strend - end_shift,
292 prog->check_substr, 0);
294 ++BmUSEFUL(prog->check_substr); /* hooray */
295 goto phooey; /* not present */
296 } else if ((s - stringarg) > prog->check_offset_max) {
297 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
298 s -= prog->check_offset_max;
299 } else if (!prog->naughty
300 && --BmUSEFUL(prog->check_substr) < 0
301 && prog->check_substr == prog->float_substr) { /* boo */
302 SvREFCNT_dec(prog->check_substr);
303 prog->check_substr = Nullsv; /* disable */
304 prog->float_substr = Nullsv; /* clear */
309 /* Mark beginning of line for ^ and lookbehind. */
313 /* Mark end of line for $ (and such) */
316 /* see how far we have to get to not match where we matched before */
317 regtill = startpos+minend;
320 PerlIO_printf(Perl_debug_log,
321 "Matching `%.60s%s' against `%.*s%s'\n",
323 (strlen(prog->precomp) > 60 ? "..." : ""),
324 (strend - startpos > 60 ? 60 : strend - startpos),
326 (strend - startpos > 60 ? "..." : ""))
329 /* Simplest case: anchored match need be tried only once. */
330 /* [unless only anchor is BOL and multiline is set] */
331 if (prog->reganch & ROPT_ANCH) {
332 if (regtry(prog, startpos))
334 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
335 (multiline || (prog->reganch & ROPT_IMPLICIT)
336 || (prog->reganch & ROPT_ANCH_MBOL)))
339 dontbother = minlen - 1;
340 strend -= dontbother;
341 /* for multiline we only have to try after newlines */
346 if (s < strend && regtry(prog, s))
354 /* Messy cases: unanchored match. */
355 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
356 /* we have /x+whatever/ */
357 /* it must be a one character string */
358 char ch = SvPVX(prog->anchored_substr)[0];
361 if (regtry(prog, s)) goto got_it;
363 while (s < strend && *s == ch)
370 else if (prog->anchored_substr != Nullsv
371 || (prog->float_substr != Nullsv
372 && prog->float_max_offset < strend - s)) {
373 SV *must = prog->anchored_substr
374 ? prog->anchored_substr : prog->float_substr;
376 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
378 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
379 I32 delta = back_max - back_min;
380 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
381 char *last1 = s - 1; /* Last position checked before */
383 /* XXXX check_substr already used to find `s', can optimize if
384 check_substr==must. */
386 dontbother = end_shift;
387 strend -= dontbother;
388 while ( (s <= last) &&
390 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
391 end_shift, &scream_pos, 0))
392 : (s = fbm_instr((unsigned char*)s + back_min,
393 (unsigned char*)strend, must, 0))) ) {
394 if (s - back_max > last1) {
395 last1 = s - back_min;
400 last1 = s - back_min;
410 } else if (c = prog->regstclass) {
411 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
415 dontbother = minlen - 1;
416 strend -= dontbother; /* don't bother with what can't match */
418 /* We know what class it must start with. */
421 Class = (char *) OPERAND(c);
423 if (REGINCLASS(Class, *s)) {
424 if (tmp && regtry(prog, s))
435 reg_flags |= RF_tainted;
439 dontbother++,strend--;
440 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
441 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
443 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
450 if ((minlen || tmp) && regtry(prog,s))
454 reg_flags |= RF_tainted;
458 dontbother++,strend--;
459 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
460 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
462 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
464 else if (regtry(prog, s))
468 if ((minlen || !tmp) && regtry(prog,s))
474 if (tmp && regtry(prog, s))
485 reg_flags |= RF_tainted;
487 if (isALNUM_LC(*s)) {
488 if (tmp && regtry(prog, s))
501 if (tmp && regtry(prog, s))
512 reg_flags |= RF_tainted;
514 if (!isALNUM_LC(*s)) {
515 if (tmp && regtry(prog, s))
528 if (tmp && regtry(prog, s))
539 reg_flags |= RF_tainted;
541 if (isSPACE_LC(*s)) {
542 if (tmp && regtry(prog, s))
555 if (tmp && regtry(prog, s))
566 reg_flags |= RF_tainted;
568 if (!isSPACE_LC(*s)) {
569 if (tmp && regtry(prog, s))
582 if (tmp && regtry(prog, s))
595 if (tmp && regtry(prog, s))
609 if (prog->float_substr != Nullsv) { /* Trim the end. */
611 I32 oldpos = scream_pos;
614 last = screaminstr(screamer, prog->float_substr, s - strbeg,
615 end_shift, &scream_pos, 1); /* last one */
617 last = scream_olds; /* Only one occurence. */
621 char *little = SvPV(prog->float_substr, len);
622 last = rninstr(s, strend, little, little + len);
624 if (last == NULL) goto phooey; /* Should not happen! */
625 dontbother = strend - last - 1;
627 if (minlen && (dontbother < minlen))
628 dontbother = minlen - 1;
629 strend -= dontbother;
630 /* We don't know much -- general case. */
634 } while (s++ < strend);
641 strend += dontbother; /* uncheat */
642 prog->subbeg = strbeg;
643 prog->subend = strend;
644 RX_MATCH_TAINTED_set(prog, reg_flags & RF_tainted);
646 /* make sure $`, $&, $', and $digit will work later */
647 if (strbeg != prog->subbase) { /* second+ //g match. */
648 if (!(flags & REXEC_COPY_STR)) {
650 Safefree(prog->subbase);
651 prog->subbase = Nullch;
655 I32 i = strend - startpos + (stringarg - strbeg);
656 s = savepvn(strbeg, i);
657 Safefree(prog->subbase);
659 prog->subbeg = prog->subbase;
660 prog->subend = prog->subbase + i;
661 s = prog->subbase + (stringarg - strbeg);
662 for (i = 0; i <= prog->nparens; i++) {
664 prog->startp[i] = s + (prog->startp[i] - startpos);
665 prog->endp[i] = s + (prog->endp[i] - startpos);
670 /* Preserve the current value of $^R */
671 if (oreplsv != GvSV(replgv)) {
672 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
673 restored, the value remains
683 - regtry - try match at specific point
685 STATIC I32 /* 0 failure, 1 success */
686 regtry(regexp *prog, char *startpos)
694 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
695 reg_eval_set = RS_init;
697 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
698 stack_sp - stack_base);
700 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
701 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
702 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
704 /* Apparently this is not needed, judging by wantarray. */
705 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
706 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
709 regstartp = prog->startp;
710 regendp = prog->endp;
711 reglastparen = &prog->lastparen;
714 if (reg_start_tmpl <= prog->nparens) {
715 reg_start_tmpl = prog->nparens*3/2 + 3;
717 Renew(reg_start_tmp, reg_start_tmpl, char*);
719 New(22,reg_start_tmp, reg_start_tmpl, char*);
724 regdata = prog->data;
726 for (i = prog->nparens; i >= 0; i--) {
732 if (regmatch(prog->program + 1)) {
733 prog->startp[0] = startpos;
734 prog->endp[0] = reginput;
742 - regmatch - main matching routine
744 * Conceptually the strategy is simple: check to see whether the current
745 * node matches, call self recursively to see whether the rest matches,
746 * and then act accordingly. In practice we make some effort to avoid
747 * recursion, in particular by going through "ordinary" nodes (that don't
748 * need to know whether the rest of the match failed) by a loop instead of
751 /* [lwall] I've hoisted the register declarations to the outer block in order to
752 * maybe save a little bit of pushing and popping on the stack. It also takes
753 * advantage of machines that use a register save mask on subroutine entry.
755 STATIC I32 /* 0 failure, 1 success */
756 regmatch(regnode *prog)
759 register regnode *scan; /* Current node. */
760 regnode *next; /* Next node. */
761 regnode *inner; /* Next node in internal branch. */
762 register I32 nextchr; /* renamed nextchr - nextchar colides with
763 function of same name */
764 register I32 n; /* no or next */
765 register I32 ln; /* len or last */
766 register char *s; /* operand or save */
767 register char *locinput = reginput;
768 register I32 c1, c2, paren; /* case fold search, parenth */
769 int minmod = 0, sw = 0, logical = 0;
774 nextchr = UCHARAT(locinput);
776 while (scan != NULL) {
777 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
779 # define sayYES goto yes
780 # define sayNO goto no
781 # define saySAME(x) if (x) goto yes; else goto no
782 # define REPORT_CODE_OFF 24
784 # define sayYES return 1
785 # define sayNO return 0
786 # define saySAME(x) return x
789 SV *prop = sv_newmortal();
790 int docolor = *colors[0];
791 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
792 int l = (regeol - locinput > taill ? taill : regeol - locinput);
793 int pref_len = (locinput - bostr > (5 + taill) - l
794 ? (5 + taill) - l : locinput - bostr);
796 if (l + pref_len < (5 + taill) && l < regeol - locinput)
797 l = ( regeol - locinput > (5 + taill) - pref_len
798 ? (5 + taill) - pref_len : regeol - locinput);
800 PerlIO_printf(Perl_debug_log,
801 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
803 colors[2], pref_len, locinput - pref_len, colors[3],
804 (docolor ? "" : "> <"),
805 colors[0], l, locinput, colors[1],
806 15 - l - pref_len + 1,
808 scan - regprogram, regindent*2, "",
812 next = scan + NEXT_OFF(scan);
818 if (locinput == bostr
821 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
823 /* regtill = regbol; */
828 if (locinput == bostr
830 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
836 if (locinput == regbol && regprev == '\n')
840 if (locinput == regbol)
850 if ((nextchr || locinput < regeol) && nextchr != '\n')
855 if ((nextchr || locinput < regeol) && nextchr != '\n')
857 if (regeol - locinput > 1)
861 if (regeol != locinput)
865 if (!nextchr && locinput >= regeol)
867 nextchr = UCHARAT(++locinput);
870 if (!nextchr && locinput >= regeol || nextchr == '\n')
872 nextchr = UCHARAT(++locinput);
875 s = (char *) OPERAND(scan);
877 /* Inline the first character, for speed. */
878 if (UCHARAT(s) != nextchr)
880 if (regeol - locinput < ln)
882 if (ln > 1 && memNE(s, locinput, ln))
885 nextchr = UCHARAT(locinput);
888 reg_flags |= RF_tainted;
891 s = (char *) OPERAND(scan);
893 /* Inline the first character, for speed. */
894 if (UCHARAT(s) != nextchr &&
895 UCHARAT(s) != ((OP(scan) == EXACTF)
896 ? fold : fold_locale)[nextchr])
898 if (regeol - locinput < ln)
900 if (ln > 1 && (OP(scan) == EXACTF
901 ? ibcmp(s, locinput, ln)
902 : ibcmp_locale(s, locinput, ln)))
905 nextchr = UCHARAT(locinput);
908 s = (char *) OPERAND(scan);
910 nextchr = UCHARAT(locinput);
911 if (!REGINCLASS(s, nextchr))
913 if (!nextchr && locinput >= regeol)
915 nextchr = UCHARAT(++locinput);
918 reg_flags |= RF_tainted;
923 if (!(OP(scan) == ALNUM
924 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
926 nextchr = UCHARAT(++locinput);
929 reg_flags |= RF_tainted;
932 if (!nextchr && locinput >= regeol)
934 if (OP(scan) == NALNUM
935 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
937 nextchr = UCHARAT(++locinput);
941 reg_flags |= RF_tainted;
945 /* was last char in word? */
946 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
947 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
949 n = isALNUM(nextchr);
953 n = isALNUM_LC(nextchr);
955 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
959 reg_flags |= RF_tainted;
962 if (!nextchr && locinput >= regeol)
964 if (!(OP(scan) == SPACE
965 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
967 nextchr = UCHARAT(++locinput);
970 reg_flags |= RF_tainted;
975 if (OP(scan) == SPACE
976 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
978 nextchr = UCHARAT(++locinput);
981 if (!isDIGIT(nextchr))
983 nextchr = UCHARAT(++locinput);
986 if (!nextchr && locinput >= regeol)
988 if (isDIGIT(nextchr))
990 nextchr = UCHARAT(++locinput);
993 reg_flags |= RF_tainted;
997 n = ARG(scan); /* which paren pair */
999 if (*reglastparen < n || !s)
1000 sayNO; /* Do not match unless seen CLOSEn. */
1001 if (s == regendp[n])
1003 /* Inline the first character, for speed. */
1004 if (UCHARAT(s) != nextchr &&
1006 (UCHARAT(s) != ((OP(scan) == REFF
1007 ? fold : fold_locale)[nextchr]))))
1009 ln = regendp[n] - s;
1010 if (locinput + ln > regeol)
1012 if (ln > 1 && (OP(scan) == REF
1013 ? memNE(s, locinput, ln)
1015 ? ibcmp(s, locinput, ln)
1016 : ibcmp_locale(s, locinput, ln))))
1019 nextchr = UCHARAT(locinput);
1031 COP *ocurcop = curcop;
1032 SV **ocurpad = curpad;
1036 op = (OP_4tree*)regdata->data[n];
1037 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1038 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1040 CALLRUNOPS(); /* Scalar context. */
1049 sv_setsv(save_scalar(replgv), ret);
1056 n = ARG(scan); /* which paren pair */
1057 reg_start_tmp[n] = locinput;
1062 n = ARG(scan); /* which paren pair */
1063 regstartp[n] = reg_start_tmp[n];
1064 regendp[n] = locinput;
1065 if (n > *reglastparen)
1069 n = ARG(scan); /* which paren pair */
1070 sw = (*reglastparen >= n && regendp[n] != NULL);
1074 next = NEXTOPER(NEXTOPER(scan));
1076 next = scan + ARG(scan);
1077 if (OP(next) == IFTHEN) /* Fake one. */
1078 next = NEXTOPER(NEXTOPER(next));
1086 CHECKPOINT cp = savestack_ix;
1088 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1092 cc.parenfloor = *reglastparen;
1094 cc.min = ARG1(scan);
1095 cc.max = ARG2(scan);
1096 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1100 reginput = locinput;
1101 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1109 * This is really hard to understand, because after we match
1110 * what we're trying to match, we must make sure the rest of
1111 * the RE is going to match for sure, and to do that we have
1112 * to go back UP the parse tree by recursing ever deeper. And
1113 * if it fails, we have to reset our parent's current state
1114 * that we can try again after backing off.
1117 CHECKPOINT cp, lastcp;
1119 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1121 n = cc->cur + 1; /* how many we know we matched */
1122 reginput = locinput;
1125 PerlIO_printf(Perl_debug_log,
1126 "%*s %ld out of %ld..%ld cc=%lx\n",
1127 REPORT_CODE_OFF+regindent*2, "",
1128 (long)n, (long)cc->min,
1129 (long)cc->max, (long)cc)
1132 /* If degenerate scan matches "", assume scan done. */
1134 if (locinput == cc->lastloc && n >= cc->min) {
1138 PerlIO_printf(Perl_debug_log,
1139 "%*s empty match detected, try continuation...\n",
1140 REPORT_CODE_OFF+regindent*2, "")
1142 if (regmatch(cc->next))
1145 PerlIO_printf(Perl_debug_log,
1147 REPORT_CODE_OFF+regindent*2, "")
1154 /* First just match a string of min scans. */
1158 cc->lastloc = locinput;
1159 if (regmatch(cc->scan))
1162 cc->lastloc = lastloc;
1164 PerlIO_printf(Perl_debug_log,
1166 REPORT_CODE_OFF+regindent*2, "")
1171 /* Prefer next over scan for minimal matching. */
1176 cp = regcppush(cc->parenfloor);
1178 if (regmatch(cc->next)) {
1180 sayYES; /* All done. */
1187 if (n >= cc->max) { /* Maximum greed exceeded? */
1188 if (dowarn && n >= REG_INFTY
1189 && !(reg_flags & RF_warned)) {
1190 reg_flags |= RF_warned;
1191 warn("Complex regular subexpression recursion "
1192 "limit (%d) exceeded", REG_INFTY - 1);
1198 PerlIO_printf(Perl_debug_log,
1199 "%*s trying longer...\n",
1200 REPORT_CODE_OFF+regindent*2, "")
1202 /* Try scanning more and see if it helps. */
1203 reginput = locinput;
1205 cc->lastloc = locinput;
1206 cp = regcppush(cc->parenfloor);
1208 if (regmatch(cc->scan)) {
1213 PerlIO_printf(Perl_debug_log,
1215 REPORT_CODE_OFF+regindent*2, "")
1220 cc->lastloc = lastloc;
1224 /* Prefer scan over next for maximal matching. */
1226 if (n < cc->max) { /* More greed allowed? */
1227 cp = regcppush(cc->parenfloor);
1229 cc->lastloc = locinput;
1231 if (regmatch(cc->scan)) {
1236 regcppop(); /* Restore some previous $<digit>s? */
1237 reginput = locinput;
1239 PerlIO_printf(Perl_debug_log,
1240 "%*s failed, try continuation...\n",
1241 REPORT_CODE_OFF+regindent*2, "")
1244 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1245 reg_flags |= RF_warned;
1246 warn("count exceeded %d", REG_INFTY - 1);
1249 /* Failed deeper matches of scan, so see if this one works. */
1252 if (regmatch(cc->next))
1255 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1256 REPORT_CODE_OFF+regindent*2, "")
1261 cc->lastloc = lastloc;
1266 next = scan + ARG(scan);
1269 inner = NEXTOPER(NEXTOPER(scan));
1272 inner = NEXTOPER(scan);
1277 if (OP(next) != c1) /* No choice. */
1278 next = inner; /* Avoid recursion. */
1280 int lastparen = *reglastparen;
1284 reginput = locinput;
1285 if (regmatch(inner))
1288 for (n = *reglastparen; n > lastparen; n--)
1293 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1297 inner = NEXTOPER(scan);
1298 if (c1 == BRANCHJ) {
1299 inner = NEXTOPER(inner);
1301 } while (scan != NULL && OP(scan) == c1);
1315 /* We suppose that the next guy does not need
1316 backtracking: in particular, it is of constant length,
1317 and has no parenths to influence future backrefs. */
1318 ln = ARG1(scan); /* min to match */
1319 n = ARG2(scan); /* max to match */
1320 paren = scan->flags;
1322 if (paren > regsize)
1324 if (paren > *reglastparen)
1325 *reglastparen = paren;
1327 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1329 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1330 reginput = locinput;
1333 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1335 if (ln && l == 0 && n >= ln
1336 /* In fact, this is tricky. If paren, then the
1337 fact that we did/didnot match may influence
1338 future execution. */
1339 && !(paren && ln == 0))
1341 locinput = reginput;
1342 if (regkind[(U8)OP(next)] == EXACT) {
1343 c1 = UCHARAT(OPERAND(next) + 1);
1344 if (OP(next) == EXACTF)
1346 else if (OP(next) == EXACTFL)
1347 c2 = fold_locale[c1];
1353 /* This may be improved if l == 0. */
1354 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1355 /* If it could work, try it. */
1357 UCHARAT(reginput) == c1 ||
1358 UCHARAT(reginput) == c2)
1362 regstartp[paren] = reginput - l;
1363 regendp[paren] = reginput;
1365 regendp[paren] = NULL;
1371 /* Couldn't or didn't -- move forward. */
1372 reginput = locinput;
1373 if (regrepeat_hard(scan, 1, &l)) {
1375 locinput = reginput;
1381 n = regrepeat_hard(scan, n, &l);
1382 if (n != 0 && l == 0
1383 /* In fact, this is tricky. If paren, then the
1384 fact that we did/didnot match may influence
1385 future execution. */
1386 && !(paren && ln == 0))
1388 locinput = reginput;
1390 PerlIO_printf(Perl_debug_log,
1391 "%*s matched %ld times, len=%ld...\n",
1392 REPORT_CODE_OFF+regindent*2, "", n, l)
1395 if (regkind[(U8)OP(next)] == EXACT) {
1396 c1 = UCHARAT(OPERAND(next) + 1);
1397 if (OP(next) == EXACTF)
1399 else if (OP(next) == EXACTFL)
1400 c2 = fold_locale[c1];
1408 /* If it could work, try it. */
1410 UCHARAT(reginput) == c1 ||
1411 UCHARAT(reginput) == c2)
1414 PerlIO_printf(Perl_debug_log,
1415 "%*s trying tail with n=%ld...\n",
1416 REPORT_CODE_OFF+regindent*2, "", n)
1420 regstartp[paren] = reginput - l;
1421 regendp[paren] = reginput;
1423 regendp[paren] = NULL;
1429 /* Couldn't or didn't -- back up. */
1432 reginput = locinput;
1439 paren = scan->flags; /* Which paren to set */
1440 if (paren > regsize)
1442 if (paren > *reglastparen)
1443 *reglastparen = paren;
1444 ln = ARG1(scan); /* min to match */
1445 n = ARG2(scan); /* max to match */
1446 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1450 ln = ARG1(scan); /* min to match */
1451 n = ARG2(scan); /* max to match */
1452 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1457 scan = NEXTOPER(scan);
1463 scan = NEXTOPER(scan);
1467 * Lookahead to avoid useless match attempts
1468 * when we know what character comes next.
1470 if (regkind[(U8)OP(next)] == EXACT) {
1471 c1 = UCHARAT(OPERAND(next) + 1);
1472 if (OP(next) == EXACTF)
1474 else if (OP(next) == EXACTFL)
1475 c2 = fold_locale[c1];
1481 reginput = locinput;
1485 if (ln && regrepeat(scan, ln) < ln)
1488 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1489 /* If it could work, try it. */
1491 UCHARAT(reginput) == c1 ||
1492 UCHARAT(reginput) == c2)
1496 regstartp[paren] = reginput - 1;
1497 regendp[paren] = reginput;
1499 regendp[paren] = NULL;
1505 /* Couldn't or didn't -- move forward. */
1506 reginput = locinput + ln;
1507 if (regrepeat(scan, 1)) {
1509 reginput = locinput + ln;
1516 n = regrepeat(scan, n);
1517 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1518 (!multiline || OP(next) == SEOL))
1519 ln = n; /* why back off? */
1523 /* If it could work, try it. */
1525 UCHARAT(reginput) == c1 ||
1526 UCHARAT(reginput) == c2)
1530 regstartp[paren] = reginput - 1;
1531 regendp[paren] = reginput;
1533 regendp[paren] = NULL;
1539 /* Couldn't or didn't -- back up. */
1541 reginput = locinput + n;
1545 /* If it could work, try it. */
1547 UCHARAT(reginput) == c1 ||
1548 UCHARAT(reginput) == c2)
1554 /* Couldn't or didn't -- back up. */
1556 reginput = locinput + n;
1563 if (locinput < regtill)
1564 sayNO; /* Cannot match: too short. */
1567 reginput = locinput; /* put where regtry can find it */
1568 sayYES; /* Success! */
1574 if (locinput < bostr + scan->flags)
1579 if (locinput < bostr + scan->flags)
1582 reginput = locinput - scan->flags;
1583 inner = NEXTOPER(NEXTOPER(scan));
1584 if (regmatch(inner) != n) {
1598 if (OP(scan) == SUSPEND) {
1599 locinput = reginput;
1600 nextchr = UCHARAT(locinput);
1605 next = scan + ARG(scan);
1610 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1611 (unsigned long)scan, OP(scan));
1612 FAIL("regexp memory corruption");
1618 * We get here only if there's trouble -- normally "case END" is
1619 * the terminating point.
1621 FAIL("corrupted regexp pointers");
1639 - regrepeat - repeatedly match something simple, report how many
1642 * [This routine now assumes that it will only match on things of length 1.
1643 * That was true before, but now we assume scan - reginput is the count,
1644 * rather than incrementing count on every character.]
1647 regrepeat(regnode *p, I32 max)
1650 register char *scan;
1651 register char *opnd;
1653 register char *loceol = regeol;
1656 if (max != REG_INFTY && max < loceol - scan)
1657 loceol = scan + max;
1658 opnd = (char *) OPERAND(p);
1661 while (scan < loceol && *scan != '\n')
1667 case EXACT: /* length of string is 1 */
1668 c = UCHARAT(++opnd);
1669 while (scan < loceol && UCHARAT(scan) == c)
1672 case EXACTF: /* length of string is 1 */
1673 c = UCHARAT(++opnd);
1674 while (scan < loceol &&
1675 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1678 case EXACTFL: /* length of string is 1 */
1679 reg_flags |= RF_tainted;
1680 c = UCHARAT(++opnd);
1681 while (scan < loceol &&
1682 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1686 while (scan < loceol && REGINCLASS(opnd, *scan))
1690 while (scan < loceol && isALNUM(*scan))
1694 reg_flags |= RF_tainted;
1695 while (scan < loceol && isALNUM_LC(*scan))
1699 while (scan < loceol && !isALNUM(*scan))
1703 reg_flags |= RF_tainted;
1704 while (scan < loceol && !isALNUM_LC(*scan))
1708 while (scan < loceol && isSPACE(*scan))
1712 reg_flags |= RF_tainted;
1713 while (scan < loceol && isSPACE_LC(*scan))
1717 while (scan < loceol && !isSPACE(*scan))
1721 reg_flags |= RF_tainted;
1722 while (scan < loceol && !isSPACE_LC(*scan))
1726 while (scan < loceol && isDIGIT(*scan))
1730 while (scan < loceol && !isDIGIT(*scan))
1733 default: /* Called on something of 0 width. */
1734 break; /* So match right here or not at all. */
1737 c = scan - reginput;
1742 SV *prop = sv_newmortal();
1745 PerlIO_printf(Perl_debug_log,
1746 "%*s %s can match %ld times out of %ld...\n",
1747 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1754 - regrepeat_hard - repeatedly match something, report total lenth and length
1756 * The repeater is supposed to have constant length.
1760 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1763 register char *scan;
1764 register char *start;
1765 register char *loceol = regeol;
1769 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1771 *lp = l = reginput - start;
1772 if (max != REG_INFTY && l*max < loceol - scan)
1773 loceol = scan + l*max;
1779 if (reginput < loceol)
1784 return (scan - start)/l;
1788 - regclass - determine if a character falls into a character class
1792 reginclass(register char *p, register I32 c)
1799 if (ANYOF_TEST(p, c))
1801 else if (flags & ANYOF_FOLD) {
1803 if (flags & ANYOF_LOCALE) {
1804 reg_flags |= RF_tainted;
1805 cf = fold_locale[c];
1809 if (ANYOF_TEST(p, cf))
1813 if (!match && (flags & ANYOF_ISA)) {
1814 reg_flags |= RF_tainted;
1816 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1817 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1818 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1819 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1825 return (flags & ANYOF_INVERT) ? !match : match;