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.
24 * pregcomp and pregexec -- regsub and regerror are not used in perl
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
29 * Permission is granted to anyone to use this software for any
30 * purpose on any computer system, and to redistribute it freely,
31 * subject to the following restrictions:
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
43 **** Alterations to Henry's code are...
45 **** Copyright (c) 1991-1997, Larry Wall
47 **** You may distribute under the terms of either the GNU General Public
48 **** License or the Artistic License, as specified in the README file.
50 * Beware that some of this code is subtly aware of the way operator
51 * precedence is structured in regular expressions. Serious changes in
52 * regular-expression syntax might require a total rethink.
58 static char * reginput; /* String-input pointer. */
59 static char * regbol; /* Beginning of input, for ^ check. */
60 static char * regeol; /* End of input, for $ check. */
61 static char ** regstartp; /* Pointer to startp array. */
62 static char ** regendp; /* Ditto for endp. */
63 static U32 * reglastparen; /* Similarly for lastparen. */
64 static char * regtill; /* How far we are required to go. */
65 static char regprev; /* char before regbol, \n if none */
67 static char * regprecomp; /* uncompiled string. */
68 static I32 regnpar; /* () count. */
69 static I32 regsize; /* Largest OPEN seens. */
70 static char ** reg_start_tmp;
71 static U32 reg_start_tmpl;
72 static struct reg_data *data;
75 static U32 reg_flags; /* tainted/warned */
76 static I32 reg_eval_set;
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? */
87 static I32 regnarrate = 0;
88 static regnode* regprogram = 0;
91 /* Current curly descriptor */
92 typedef struct curcur CURCUR;
94 int parenfloor; /* how far back to strip paren data */
95 int cur; /* how many instances of scan we've matched */
96 int min; /* the minimal number of scans to match */
97 int max; /* the maximal number of scans to match */
98 int minmod; /* whether to work our way up or down */
99 regnode * scan; /* the thing to match */
100 regnode * next; /* what has to match after it */
101 char * lastloc; /* where we started matching this scan */
102 CURCUR * oldcc; /* current curly before we started this one */
105 static CURCUR* regcc;
108 typedef I32 CHECKPOINT;
114 static I32 regmatch _((regnode *prog));
115 static I32 regrepeat _((regnode *p, I32 max));
116 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
117 static I32 regtry _((regexp *prog, char *startpos));
118 static bool reginclass _((char *p, I32 c));
119 static CHECKPOINT regcppush _((I32 parenfloor));
120 static char * regcppop _((void));
124 regcppush(I32 parenfloor)
127 int retval = savestack_ix;
128 int i = (regsize - parenfloor) * 4;
132 for (p = regsize; p > parenfloor; p--) {
133 SSPUSHPTR(regendp[p]);
134 SSPUSHPTR(regstartp[p]);
135 SSPUSHPTR(reg_start_tmp[p]);
139 SSPUSHINT(*reglastparen);
142 SSPUSHINT(SAVEt_REGCONTEXT);
146 /* These are needed since we do not localize EVAL nodes: */
147 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
148 # define REGCP_UNWIND DEBUG_r(lastcp != savestack_ix ? PerlIO_printf(Perl_debug_log," Clearing an EVAL scope, savestack=%i..%i\n", lastcp, savestack_ix) : 0); regcpblow(lastcp)
158 assert(i == SAVEt_REGCONTEXT);
160 input = (char *) SSPOPPTR;
161 *reglastparen = SSPOPINT;
163 for (i -= 3; i > 0; i -= 4) {
164 paren = (U32)SSPOPINT;
165 reg_start_tmp[paren] = (char *) SSPOPPTR;
166 regstartp[paren] = (char *) SSPOPPTR;
167 tmps = (char*)SSPOPPTR;
168 if (paren <= *reglastparen)
169 regendp[paren] = tmps;
171 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
172 paren, regstartp[paren] - regbol,
173 reg_start_tmp[paren] - regbol,
174 regendp[paren] - regbol,
175 (paren > *reglastparen ? "(no)" : ""));
179 if (*reglastparen + 1 <= regnpar) {
180 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
181 *reglastparen + 1, regnpar);
184 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
186 regstartp[paren] = Nullch;
187 regendp[paren] = Nullch;
192 #define regcpblow(cp) LEAVE_SCOPE(cp)
195 * pregexec and friends
199 - pregexec - match a regexp against a string
202 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
203 /* strend: pointer to null at end of string */
204 /* strbeg: real beginning of string */
205 /* minend: end of match must be >=minend after stringarg. */
206 /* nosave: For optimizations. */
209 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
210 nosave ? 0 : REXEC_COPY_STR);
214 - regexec_flags - match a regexp against a string
217 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
218 /* strend: pointer to null at end of string */
219 /* strbeg: real beginning of string */
220 /* minend: end of match must be >=minend after stringarg. */
221 /* data: May be used for some additional optimizations. */
222 /* nosave: For optimizations. */
226 register char *startpos = stringarg;
228 I32 minlen; /* must match at least this many chars */
229 I32 dontbother = 0; /* how many characters not to try at end */
231 I32 start_shift = 0; /* Offset of the start to find
233 I32 end_shift = 0; /* Same for the end. */
234 I32 scream_pos = -1; /* Internal iterator of scream. */
241 regprecomp = prog->precomp; /* Needed for error messages. */
243 regnarrate = debug & 512;
244 regprogram = prog->program;
248 if (prog == NULL || startpos == NULL) {
249 croak("NULL regexp parameter");
253 minlen = prog->minlen;
254 if (strend - startpos < minlen) goto phooey;
256 if (startpos == strbeg) /* is ^ valid at stringarg? */
259 regprev = stringarg[-1];
260 if (!multiline && regprev == '\n')
261 regprev = '\0'; /* force ^ to NOT match */
264 /* Check validity of program. */
265 if (UCHARAT(prog->program) != MAGIC) {
266 FAIL("corrupted regexp program");
269 regnpar = prog->nparens;
273 /* If there is a "must appear" string, look for it. */
275 if (!(flags & REXEC_CHECKED)
276 && prog->check_substr != Nullsv &&
277 !(prog->reganch & ROPT_ANCH_GPOS) &&
278 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
279 || (multiline && prog->check_substr == prog->anchored_substr)) )
281 start_shift = prog->check_offset_min;
282 /* Should be nonnegative! */
283 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
285 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
286 s = screaminstr(screamer, prog->check_substr,
287 start_shift + (stringarg - strbeg),
288 end_shift, &scream_pos, 0);
294 s = fbm_instr((unsigned char*)s + start_shift,
295 (unsigned char*)strend - end_shift,
298 ++BmUSEFUL(prog->check_substr); /* hooray */
299 goto phooey; /* not present */
300 } else if ((s - stringarg) > prog->check_offset_max) {
301 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
302 s -= prog->check_offset_max;
303 } else if (!prog->naughty
304 && --BmUSEFUL(prog->check_substr) < 0
305 && prog->check_substr == prog->float_substr) { /* boo */
306 SvREFCNT_dec(prog->check_substr);
307 prog->check_substr = Nullsv; /* disable */
308 prog->float_substr = Nullsv; /* clear */
313 /* Mark beginning of line for ^ and lookbehind. */
317 /* Mark end of line for $ (and such) */
320 /* see how far we have to get to not match where we matched before */
321 regtill = startpos+minend;
324 PerlIO_printf(Perl_debug_log,
325 "Matching `%.60s%s' against `%.*s%s'\n",
327 (strlen(prog->precomp) > 60 ? "..." : ""),
328 (strend - startpos > 60 ? 60 : strend - startpos),
330 (strend - startpos > 60 ? "..." : ""))
333 /* Simplest case: anchored match need be tried only once. */
334 /* [unless only anchor is BOL and multiline is set] */
335 if (prog->reganch & ROPT_ANCH) {
336 if (regtry(prog, startpos))
338 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
339 (multiline || (prog->reganch & ROPT_IMPLICIT)
340 || (prog->reganch & ROPT_ANCH_MBOL)))
343 dontbother = minlen - 1;
344 strend -= dontbother;
345 /* for multiline we only have to try after newlines */
350 if (s < strend && regtry(prog, s))
358 /* Messy cases: unanchored match. */
359 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
360 /* we have /x+whatever/ */
361 /* it must be a one character string */
362 char ch = SvPVX(prog->anchored_substr)[0];
365 if (regtry(prog, s)) goto got_it;
367 while (s < strend && *s == ch)
374 else if (prog->anchored_substr != Nullsv
375 || (prog->float_substr != Nullsv
376 && prog->float_max_offset < strend - s)) {
377 SV *must = prog->anchored_substr
378 ? prog->anchored_substr : prog->float_substr;
380 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
382 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
383 I32 delta = back_max - back_min;
384 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
385 char *last1 = s - 1; /* Last position checked before */
387 /* XXXX check_substr already used to find `s', can optimize if
388 check_substr==must. */
390 dontbother = end_shift;
391 strend -= dontbother;
392 while ( (s <= last) &&
394 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
395 end_shift, &scream_pos, 0))
396 : (s = fbm_instr((unsigned char*)s + back_min,
397 (unsigned char*)strend, must))) ) {
398 if (s - back_max > last1) {
399 last1 = s - back_min;
404 last1 = s - back_min;
414 } else if (c = prog->regstclass) {
415 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
419 dontbother = minlen - 1;
420 strend -= dontbother; /* don't bother with what can't match */
422 /* We know what class it must start with. */
425 Class = (char *) OPERAND(c);
427 if (reginclass(Class, *s)) {
428 if (tmp && regtry(prog, s))
439 reg_flags |= RF_tainted;
443 dontbother++,strend--;
444 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
445 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
447 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
454 if ((minlen || tmp) && regtry(prog,s))
458 reg_flags |= RF_tainted;
462 dontbother++,strend--;
463 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
464 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
466 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
468 else if (regtry(prog, s))
472 if ((minlen || !tmp) && regtry(prog,s))
478 if (tmp && regtry(prog, s))
489 reg_flags |= RF_tainted;
491 if (isALNUM_LC(*s)) {
492 if (tmp && regtry(prog, s))
505 if (tmp && regtry(prog, s))
516 reg_flags |= RF_tainted;
518 if (!isALNUM_LC(*s)) {
519 if (tmp && regtry(prog, s))
532 if (tmp && regtry(prog, s))
543 reg_flags |= RF_tainted;
545 if (isSPACE_LC(*s)) {
546 if (tmp && regtry(prog, s))
559 if (tmp && regtry(prog, s))
570 reg_flags |= RF_tainted;
572 if (!isSPACE_LC(*s)) {
573 if (tmp && regtry(prog, s))
586 if (tmp && regtry(prog, s))
599 if (tmp && regtry(prog, s))
613 if (prog->float_substr != Nullsv) { /* Trim the end. */
615 I32 oldpos = scream_pos;
618 last = screaminstr(screamer, prog->float_substr, s - strbeg,
619 end_shift, &scream_pos, 1); /* last one */
621 last = scream_olds; /* Only one occurence. */
625 char *little = SvPV(prog->float_substr, len);
626 last = rninstr(s, strend, little, little + len);
628 if (last == NULL) goto phooey; /* Should not happen! */
629 dontbother = strend - last - 1;
631 if (minlen && (dontbother < minlen))
632 dontbother = minlen - 1;
633 strend -= dontbother;
634 /* We don't know much -- general case. */
638 } while (s++ < strend);
645 strend += dontbother; /* uncheat */
646 prog->subbeg = strbeg;
647 prog->subend = strend;
648 RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
650 /* make sure $`, $&, $', and $digit will work later */
651 if (strbeg != prog->subbase) { /* second+ //g match. */
652 if (!(flags & REXEC_COPY_STR)) {
654 Safefree(prog->subbase);
655 prog->subbase = Nullch;
659 I32 i = strend - startpos + (stringarg - strbeg);
660 s = savepvn(strbeg, i);
661 Safefree(prog->subbase);
663 prog->subbeg = prog->subbase;
664 prog->subend = prog->subbase + i;
665 s = prog->subbase + (stringarg - strbeg);
666 for (i = 0; i <= prog->nparens; i++) {
668 prog->startp[i] = s + (prog->startp[i] - startpos);
669 prog->endp[i] = s + (prog->endp[i] - startpos);
681 - regtry - try match at specific point
683 STATIC I32 /* 0 failure, 1 success */
684 regtry(regexp *prog, char *startpos)
693 regstartp = prog->startp;
694 regendp = prog->endp;
695 reglastparen = &prog->lastparen;
698 if (reg_start_tmpl <= prog->nparens) {
699 reg_start_tmpl = prog->nparens*3/2 + 3;
701 Renew(reg_start_tmp, reg_start_tmpl, char*);
703 New(22,reg_start_tmp, reg_start_tmpl, char*);
710 for (i = prog->nparens; i >= 0; i--) {
716 if (regmatch(prog->program + 1) && reginput >= regtill) {
717 prog->startp[0] = startpos;
718 prog->endp[0] = reginput;
726 - regmatch - main matching routine
728 * Conceptually the strategy is simple: check to see whether the current
729 * node matches, call self recursively to see whether the rest matches,
730 * and then act accordingly. In practice we make some effort to avoid
731 * recursion, in particular by going through "ordinary" nodes (that don't
732 * need to know whether the rest of the match failed) by a loop instead of
735 /* [lwall] I've hoisted the register declarations to the outer block in order to
736 * maybe save a little bit of pushing and popping on the stack. It also takes
737 * advantage of machines that use a register save mask on subroutine entry.
739 STATIC I32 /* 0 failure, 1 success */
740 regmatch(regnode *prog)
743 register regnode *scan; /* Current node. */
744 regnode *next; /* Next node. */
745 regnode *inner; /* Next node in internal branch. */
746 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
747 register I32 n; /* no or next */
748 register I32 ln; /* len or last */
749 register char *s; /* operand or save */
750 register char *locinput = reginput;
751 register I32 c1, c2, paren; /* case fold search, parenth */
752 int minmod = 0, sw = 0, logical = 0;
754 static int regindent = 0;
758 nextchr = UCHARAT(locinput);
760 while (scan != NULL) {
761 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
763 # define sayYES goto yes
764 # define sayNO goto no
765 # define saySAME(x) if (x) goto yes; else goto no
766 # define REPORT_CODE_OFF 24
768 # define sayYES return 1
769 # define sayNO return 0
770 # define saySAME(x) return x
773 SV *prop = sv_newmortal();
774 int docolor = *colors[0];
775 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
776 int l = (regeol - locinput > taill ? taill : regeol - locinput);
777 int pref_len = (locinput - bostr > (5 + taill) - l
778 ? (5 + taill) - l : locinput - bostr);
780 if (l + pref_len < (5 + taill) && l < regeol - locinput)
781 l = ( regeol - locinput > (5 + taill) - pref_len
782 ? (5 + taill) - pref_len : regeol - locinput);
784 PerlIO_printf(Perl_debug_log,
785 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
787 colors[2], pref_len, locinput - pref_len, colors[3],
788 (docolor ? "" : "> <"),
789 colors[0], l, locinput, colors[1],
790 15 - l - pref_len + 1,
792 regindent*2, "", scan - regprogram,
797 next = scan + NEXT_OFF(scan);
801 next = regnext(scan);
806 if (locinput == regbol
809 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
811 /* regtill = regbol; */
816 if (locinput == regbol
818 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
824 if (locinput == regbol && regprev == '\n')
828 if (locinput == regbol)
838 if ((nextchr || locinput < regeol) && nextchr != '\n')
843 if ((nextchr || locinput < regeol) && nextchr != '\n')
845 if (regeol - locinput > 1)
849 if (!nextchr && locinput >= regeol)
851 nextchr = UCHARAT(++locinput);
854 if (!nextchr && locinput >= regeol || nextchr == '\n')
856 nextchr = UCHARAT(++locinput);
859 s = (char *) OPERAND(scan);
861 /* Inline the first character, for speed. */
862 if (UCHARAT(s) != nextchr)
864 if (regeol - locinput < ln)
866 if (ln > 1 && memNE(s, locinput, ln))
869 nextchr = UCHARAT(locinput);
872 reg_flags |= RF_tainted;
875 s = (char *) OPERAND(scan);
877 /* Inline the first character, for speed. */
878 if (UCHARAT(s) != nextchr &&
879 UCHARAT(s) != ((OP(scan) == EXACTF)
880 ? fold : fold_locale)[nextchr])
882 if (regeol - locinput < ln)
884 if (ln > 1 && (OP(scan) == EXACTF
885 ? ibcmp(s, locinput, ln)
886 : ibcmp_locale(s, locinput, ln)))
889 nextchr = UCHARAT(locinput);
892 s = (char *) OPERAND(scan);
894 nextchr = UCHARAT(locinput);
895 if (!reginclass(s, nextchr))
897 if (!nextchr && locinput >= regeol)
899 nextchr = UCHARAT(++locinput);
902 reg_flags |= RF_tainted;
907 if (!(OP(scan) == ALNUM
908 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
910 nextchr = UCHARAT(++locinput);
913 reg_flags |= RF_tainted;
916 if (!nextchr && locinput >= regeol)
918 if (OP(scan) == NALNUM
919 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
921 nextchr = UCHARAT(++locinput);
925 reg_flags |= RF_tainted;
929 /* was last char in word? */
930 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
931 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
933 n = isALNUM(nextchr);
937 n = isALNUM_LC(nextchr);
939 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
943 reg_flags |= RF_tainted;
946 if (!nextchr && locinput >= regeol)
948 if (!(OP(scan) == SPACE
949 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
951 nextchr = UCHARAT(++locinput);
954 reg_flags |= RF_tainted;
959 if (OP(scan) == SPACE
960 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
962 nextchr = UCHARAT(++locinput);
965 if (!isDIGIT(nextchr))
967 nextchr = UCHARAT(++locinput);
970 if (!nextchr && locinput >= regeol)
972 if (isDIGIT(nextchr))
974 nextchr = UCHARAT(++locinput);
977 reg_flags |= RF_tainted;
981 n = ARG(scan); /* which paren pair */
983 if (*reglastparen < n || !s)
984 break; /* Zero length always matches */
987 /* Inline the first character, for speed. */
988 if (UCHARAT(s) != nextchr &&
990 (UCHARAT(s) != ((OP(scan) == REFF
991 ? fold : fold_locale)[nextchr]))))
994 if (locinput + ln > regeol)
996 if (ln > 1 && (OP(scan) == REF
997 ? memNE(s, locinput, ln)
999 ? ibcmp(s, locinput, ln)
1000 : ibcmp_locale(s, locinput, ln))))
1003 nextchr = UCHARAT(locinput);
1015 COP *ocurcop = curcop;
1016 SV **ocurpad = curpad;
1020 op = (OP_4tree*)data->data[n];
1021 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1022 curpad = AvARRAY((AV*)data->data[n + 1]);
1023 if (!reg_eval_set) {
1024 /* Preserve whatever is on stack now, otherwise
1025 OP_NEXTSTATE will overwrite it. */
1026 SAVEINT(reg_eval_set); /* Protect against unwinding. */
1029 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
1031 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1032 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
1033 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1035 /* Apparently this is not needed, judging by wantarray. */
1036 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1037 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1040 CALLRUNOPS(); /* Scalar context. */
1055 n = ARG(scan); /* which paren pair */
1056 reg_start_tmp[n] = locinput;
1061 n = ARG(scan); /* which paren pair */
1062 regstartp[n] = reg_start_tmp[n];
1063 regendp[n] = locinput;
1064 if (n > *reglastparen)
1068 n = ARG(scan); /* which paren pair */
1069 sw = (*reglastparen >= n && regendp[n] != NULL);
1073 next = NEXTOPER(NEXTOPER(scan));
1075 next = scan + ARG(scan);
1076 if (OP(next) == IFTHEN) /* Fake one. */
1077 next = NEXTOPER(NEXTOPER(next));
1085 CHECKPOINT cp = savestack_ix;
1087 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1091 cc.parenfloor = *reglastparen;
1093 cc.min = ARG1(scan);
1094 cc.max = ARG2(scan);
1095 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1099 reginput = locinput;
1100 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1108 * This is really hard to understand, because after we match
1109 * what we're trying to match, we must make sure the rest of
1110 * the RE is going to match for sure, and to do that we have
1111 * to go back UP the parse tree by recursing ever deeper. And
1112 * if it fails, we have to reset our parent's current state
1113 * that we can try again after backing off.
1116 CHECKPOINT cp, lastcp;
1118 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1120 n = cc->cur + 1; /* how many we know we matched */
1121 reginput = locinput;
1124 PerlIO_printf(Perl_debug_log,
1125 "%*s %ld out of %ld..%ld cc=%lx\n",
1126 REPORT_CODE_OFF+regindent*2, "",
1127 (long)n, (long)cc->min,
1128 (long)cc->max, (long)cc)
1131 /* If degenerate scan matches "", assume scan done. */
1133 if (locinput == cc->lastloc && n >= cc->min) {
1137 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1139 if (regmatch(cc->next))
1142 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1149 /* First just match a string of min scans. */
1153 cc->lastloc = locinput;
1154 if (regmatch(cc->scan))
1157 cc->lastloc = lastloc;
1159 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1164 /* Prefer next over scan for minimal matching. */
1169 cp = regcppush(cc->parenfloor);
1171 if (regmatch(cc->next)) {
1173 sayYES; /* All done. */
1180 if (n >= cc->max) { /* Maximum greed exceeded? */
1181 if (dowarn && n >= REG_INFTY
1182 && !(reg_flags & RF_warned)) {
1183 reg_flags |= RF_warned;
1184 warn("count exceeded %d", REG_INFTY - 1);
1190 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1192 /* Try scanning more and see if it helps. */
1193 reginput = locinput;
1195 cc->lastloc = locinput;
1196 cp = regcppush(cc->parenfloor);
1198 if (regmatch(cc->scan)) {
1203 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1208 cc->lastloc = lastloc;
1212 /* Prefer scan over next for maximal matching. */
1214 if (n < cc->max) { /* More greed allowed? */
1215 cp = regcppush(cc->parenfloor);
1217 cc->lastloc = locinput;
1219 if (regmatch(cc->scan)) {
1224 regcppop(); /* Restore some previous $<digit>s? */
1225 reginput = locinput;
1227 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1230 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1231 reg_flags |= RF_warned;
1232 warn("count exceeded %d", REG_INFTY - 1);
1235 /* Failed deeper matches of scan, so see if this one works. */
1238 if (regmatch(cc->next))
1241 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1246 cc->lastloc = lastloc;
1251 next = scan + ARG(scan);
1254 inner = NEXTOPER(NEXTOPER(scan));
1257 inner = NEXTOPER(scan);
1262 if (OP(next) != c1) /* No choice. */
1263 next = inner; /* Avoid recursion. */
1265 int lastparen = *reglastparen;
1269 reginput = locinput;
1270 if (regmatch(inner))
1273 for (n = *reglastparen; n > lastparen; n--)
1279 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1284 next = regnext(next);
1286 inner = NEXTOPER(scan);
1287 if (c1 == BRANCHJ) {
1288 inner = NEXTOPER(inner);
1290 } while (scan != NULL && OP(scan) == c1);
1304 /* We suppose that the next guy does not need
1305 backtracking: in particular, it is of constant length,
1306 and has no parenths to influence future backrefs. */
1307 ln = ARG1(scan); /* min to match */
1308 n = ARG2(scan); /* max to match */
1309 #ifdef REGALIGN_STRUCT
1310 paren = scan->flags;
1312 if (paren > regsize)
1314 if (paren > *reglastparen)
1315 *reglastparen = paren;
1318 scan = NEXTOPER(scan) + 4/sizeof(regnode);
1320 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1321 reginput = locinput;
1324 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1326 if (l == 0 && n >= ln
1327 /* In fact, this is tricky. If paren, then the
1328 fact that we did/didnot match may influence
1329 future execution. */
1330 && !(paren && ln == 0))
1332 locinput = reginput;
1333 if (regkind[(U8)OP(next)] == EXACT) {
1334 c1 = UCHARAT(OPERAND(next) + 1);
1335 if (OP(next) == EXACTF)
1337 else if (OP(next) == EXACTFL)
1338 c2 = fold_locale[c1];
1344 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1345 /* If it could work, try it. */
1347 UCHARAT(reginput) == c1 ||
1348 UCHARAT(reginput) == c2)
1352 regstartp[paren] = reginput - l;
1353 regendp[paren] = reginput;
1355 regendp[paren] = NULL;
1361 /* Couldn't or didn't -- move forward. */
1362 reginput = locinput;
1363 if (regrepeat_hard(scan, 1, &l)) {
1365 locinput = reginput;
1371 n = regrepeat_hard(scan, n, &l);
1372 if (n != 0 && l == 0
1373 /* In fact, this is tricky. If paren, then the
1374 fact that we did/didnot match may influence
1375 future execution. */
1376 && !(paren && ln == 0))
1378 locinput = reginput;
1380 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1383 if (regkind[(U8)OP(next)] == EXACT) {
1384 c1 = UCHARAT(OPERAND(next) + 1);
1385 if (OP(next) == EXACTF)
1387 else if (OP(next) == EXACTFL)
1388 c2 = fold_locale[c1];
1396 /* If it could work, try it. */
1398 UCHARAT(reginput) == c1 ||
1399 UCHARAT(reginput) == c2)
1402 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1406 regstartp[paren] = reginput - l;
1407 regendp[paren] = reginput;
1409 regendp[paren] = NULL;
1415 /* Couldn't or didn't -- back up. */
1418 reginput = locinput;
1425 paren = scan->flags; /* Which paren to set */
1426 if (paren > regsize)
1428 if (paren > *reglastparen)
1429 *reglastparen = paren;
1430 ln = ARG1(scan); /* min to match */
1431 n = ARG2(scan); /* max to match */
1432 scan = regnext(NEXTOPER(scan) + 4/sizeof(regnode));
1436 ln = ARG1(scan); /* min to match */
1437 n = ARG2(scan); /* max to match */
1438 scan = NEXTOPER(scan) + 4/sizeof(regnode);
1443 scan = NEXTOPER(scan);
1449 scan = NEXTOPER(scan);
1453 * Lookahead to avoid useless match attempts
1454 * when we know what character comes next.
1456 if (regkind[(U8)OP(next)] == EXACT) {
1457 c1 = UCHARAT(OPERAND(next) + 1);
1458 if (OP(next) == EXACTF)
1460 else if (OP(next) == EXACTFL)
1461 c2 = fold_locale[c1];
1467 reginput = locinput;
1471 if (ln && regrepeat(scan, ln) < ln)
1474 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1475 /* If it could work, try it. */
1477 UCHARAT(reginput) == c1 ||
1478 UCHARAT(reginput) == c2)
1482 regstartp[paren] = reginput - 1;
1483 regendp[paren] = reginput;
1485 regendp[paren] = NULL;
1491 /* Couldn't or didn't -- move forward. */
1492 reginput = locinput + ln;
1493 if (regrepeat(scan, 1)) {
1495 reginput = locinput + ln;
1502 n = regrepeat(scan, n);
1503 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1504 (!multiline || OP(next) == SEOL))
1505 ln = n; /* why back off? */
1509 /* If it could work, try it. */
1511 UCHARAT(reginput) == c1 ||
1512 UCHARAT(reginput) == c2)
1516 regstartp[paren] = reginput - 1;
1517 regendp[paren] = reginput;
1519 regendp[paren] = NULL;
1525 /* Couldn't or didn't -- back up. */
1527 reginput = locinput + n;
1531 /* If it could work, try it. */
1533 UCHARAT(reginput) == c1 ||
1534 UCHARAT(reginput) == c2)
1540 /* Couldn't or didn't -- back up. */
1542 reginput = locinput + n;
1550 reginput = locinput; /* put where regtry can find it */
1551 sayYES; /* Success! */
1557 if (locinput < bostr + scan->flags)
1562 if (locinput < bostr + scan->flags)
1565 reginput = locinput - scan->flags;
1566 inner = NEXTOPER(NEXTOPER(scan));
1567 if (regmatch(inner) != n) {
1581 if (OP(scan) == SUSPEND) {
1582 locinput = reginput;
1583 nextchr = UCHARAT(locinput);
1588 next = scan + ARG(scan);
1593 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1594 (unsigned long)scan, OP(scan));
1595 FAIL("regexp memory corruption");
1601 * We get here only if there's trouble -- normally "case END" is
1602 * the terminating point.
1604 FAIL("corrupted regexp pointers");
1622 - regrepeat - repeatedly match something simple, report how many
1625 * [This routine now assumes that it will only match on things of length 1.
1626 * That was true before, but now we assume scan - reginput is the count,
1627 * rather than incrementing count on every character.]
1630 regrepeat(regnode *p, I32 max)
1632 register char *scan;
1633 register char *opnd;
1635 register char *loceol = regeol;
1638 if (max != REG_INFTY && max < loceol - scan)
1639 loceol = scan + max;
1640 opnd = (char *) OPERAND(p);
1643 while (scan < loceol && *scan != '\n')
1649 case EXACT: /* length of string is 1 */
1650 c = UCHARAT(++opnd);
1651 while (scan < loceol && UCHARAT(scan) == c)
1654 case EXACTF: /* length of string is 1 */
1655 c = UCHARAT(++opnd);
1656 while (scan < loceol &&
1657 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1660 case EXACTFL: /* length of string is 1 */
1661 reg_flags |= RF_tainted;
1662 c = UCHARAT(++opnd);
1663 while (scan < loceol &&
1664 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1668 while (scan < loceol && reginclass(opnd, *scan))
1672 while (scan < loceol && isALNUM(*scan))
1676 reg_flags |= RF_tainted;
1677 while (scan < loceol && isALNUM_LC(*scan))
1681 while (scan < loceol && !isALNUM(*scan))
1685 reg_flags |= RF_tainted;
1686 while (scan < loceol && !isALNUM_LC(*scan))
1690 while (scan < loceol && isSPACE(*scan))
1694 reg_flags |= RF_tainted;
1695 while (scan < loceol && isSPACE_LC(*scan))
1699 while (scan < loceol && !isSPACE(*scan))
1703 reg_flags |= RF_tainted;
1704 while (scan < loceol && !isSPACE_LC(*scan))
1708 while (scan < loceol && isDIGIT(*scan))
1712 while (scan < loceol && !isDIGIT(*scan))
1715 default: /* Called on something of 0 width. */
1716 break; /* So match right here or not at all. */
1719 c = scan - reginput;
1724 SV *prop = sv_newmortal();
1727 PerlIO_printf(Perl_debug_log,
1728 "%*s %s can match %ld times out of %ld...\n",
1729 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1736 - regrepeat_hard - repeatedly match something, report total lenth and length
1738 * The repeater is supposed to have constant length.
1742 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1744 register char *scan;
1745 register char *start;
1746 register char *loceol = regeol;
1750 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1752 *lp = l = reginput - start;
1753 if (max != REG_INFTY && l*max < loceol - scan)
1754 loceol = scan + l*max;
1760 if (reginput < loceol)
1765 return (scan - start)/l;
1769 - regclass - determine if a character falls into a character class
1773 reginclass(register char *p, register I32 c)
1779 if (p[1 + (c >> 3)] & (1 << (c & 7)))
1781 else if (flags & ANYOF_FOLD) {
1783 if (flags & ANYOF_LOCALE) {
1784 reg_flags |= RF_tainted;
1785 cf = fold_locale[c];
1789 if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1793 if (!match && (flags & ANYOF_ISA)) {
1794 reg_flags |= RF_tainted;
1796 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1797 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1798 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1799 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1805 return match ^ ((flags & ANYOF_INVERT) != 0);