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;
107 typedef I32 CHECKPOINT;
113 static I32 regmatch _((regnode *prog));
114 static I32 regrepeat _((regnode *p, I32 max));
115 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
116 static I32 regtry _((regexp *prog, char *startpos));
117 static bool reginclass _((char *p, I32 c));
118 static CHECKPOINT regcppush _((I32 parenfloor));
119 static char * regcppop _((void));
122 regcppush(I32 parenfloor)
125 int retval = savestack_ix;
126 int i = (regsize - parenfloor) * 4;
130 for (p = regsize; p > parenfloor; p--) {
131 SSPUSHPTR(regendp[p]);
132 SSPUSHPTR(regstartp[p]);
133 SSPUSHPTR(reg_start_tmp[p]);
137 SSPUSHINT(*reglastparen);
140 SSPUSHINT(SAVEt_REGCONTEXT);
144 /* These are needed since we do not localize EVAL nodes: */
145 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
146 # 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)
156 assert(i == SAVEt_REGCONTEXT);
158 input = (char *) SSPOPPTR;
159 *reglastparen = SSPOPINT;
161 for (i -= 3; i > 0; i -= 4) {
162 paren = (U32)SSPOPINT;
163 reg_start_tmp[paren] = (char *) SSPOPPTR;
164 regstartp[paren] = (char *) SSPOPPTR;
165 tmps = (char*)SSPOPPTR;
166 if (paren <= *reglastparen)
167 regendp[paren] = tmps;
169 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
170 paren, regstartp[paren] - regbol,
171 reg_start_tmp[paren] - regbol,
172 regendp[paren] - regbol,
173 (paren > *reglastparen ? "(no)" : ""));
177 if (*reglastparen + 1 <= regnpar) {
178 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
179 *reglastparen + 1, regnpar);
182 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
184 regstartp[paren] = Nullch;
185 regendp[paren] = Nullch;
190 #define regcpblow(cp) LEAVE_SCOPE(cp)
193 * pregexec and friends
197 - pregexec - match a regexp against a string
200 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
201 /* strend: pointer to null at end of string */
202 /* strbeg: real beginning of string */
203 /* minend: end of match must be >=minend after stringarg. */
204 /* nosave: For optimizations. */
207 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
208 nosave ? 0 : REXEC_COPY_STR);
212 - regexec_flags - match a regexp against a string
215 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
216 /* strend: pointer to null at end of string */
217 /* strbeg: real beginning of string */
218 /* minend: end of match must be >=minend after stringarg. */
219 /* data: May be used for some additional optimizations. */
220 /* nosave: For optimizations. */
224 register char *startpos = stringarg;
226 I32 minlen; /* must match at least this many chars */
227 I32 dontbother = 0; /* how many characters not to try at end */
229 I32 start_shift = 0; /* Offset of the start to find
231 I32 end_shift = 0; /* Same for the end. */
232 I32 scream_pos = -1; /* Internal iterator of scream. */
239 regprecomp = prog->precomp; /* Needed for error messages. */
241 regnarrate = debug & 512;
242 regprogram = prog->program;
246 if (prog == NULL || startpos == NULL) {
247 croak("NULL regexp parameter");
251 minlen = prog->minlen;
252 if (strend - startpos < minlen) goto phooey;
254 if (startpos == strbeg) /* is ^ valid at stringarg? */
257 regprev = stringarg[-1];
258 if (!multiline && regprev == '\n')
259 regprev = '\0'; /* force ^ to NOT match */
262 /* Check validity of program. */
263 if (UCHARAT(prog->program) != MAGIC) {
264 FAIL("corrupted regexp program");
267 regnpar = prog->nparens;
271 /* If there is a "must appear" string, look for it. */
273 if (!(flags & REXEC_CHECKED)
274 && prog->check_substr != Nullsv &&
275 !(prog->reganch & ROPT_ANCH_GPOS) &&
276 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
277 || (multiline && prog->check_substr == prog->anchored_substr)) )
279 start_shift = prog->check_offset_min;
280 /* Should be nonnegative! */
281 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
283 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
284 s = screaminstr(screamer, prog->check_substr,
285 start_shift + (stringarg - strbeg),
286 end_shift, &scream_pos, 0);
292 s = fbm_instr((unsigned char*)s + start_shift,
293 (unsigned char*)strend - end_shift,
296 ++BmUSEFUL(prog->check_substr); /* hooray */
297 goto phooey; /* not present */
298 } else if ((s - stringarg) > prog->check_offset_max) {
299 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
300 s -= prog->check_offset_max;
301 } else if (!prog->naughty
302 && --BmUSEFUL(prog->check_substr) < 0
303 && prog->check_substr == prog->float_substr) { /* boo */
304 SvREFCNT_dec(prog->check_substr);
305 prog->check_substr = Nullsv; /* disable */
306 prog->float_substr = Nullsv; /* clear */
311 /* Mark beginning of line for ^ and lookbehind. */
315 /* Mark end of line for $ (and such) */
318 /* see how far we have to get to not match where we matched before */
319 regtill = startpos+minend;
322 PerlIO_printf(Perl_debug_log,
323 "Matching `%.60s%s' against `%.*s%s'\n",
325 (strlen(prog->precomp) > 60 ? "..." : ""),
326 (strend - startpos > 60 ? 60 : strend - startpos),
328 (strend - startpos > 60 ? "..." : ""))
331 /* Simplest case: anchored match need be tried only once. */
332 /* [unless only anchor is BOL and multiline is set] */
333 if (prog->reganch & ROPT_ANCH) {
334 if (regtry(prog, startpos))
336 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
337 (multiline || (prog->reganch & ROPT_IMPLICIT)
338 || (prog->reganch & ROPT_ANCH_MBOL)))
341 dontbother = minlen - 1;
342 strend -= dontbother;
343 /* for multiline we only have to try after newlines */
348 if (s < strend && regtry(prog, s))
356 /* Messy cases: unanchored match. */
357 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
358 /* we have /x+whatever/ */
359 /* it must be a one character string */
360 char ch = SvPVX(prog->anchored_substr)[0];
363 if (regtry(prog, s)) goto got_it;
365 while (s < strend && *s == ch)
372 else if (prog->anchored_substr != Nullsv
373 || (prog->float_substr != Nullsv
374 && prog->float_max_offset < strend - s)) {
375 SV *must = prog->anchored_substr
376 ? prog->anchored_substr : prog->float_substr;
378 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
380 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
381 I32 delta = back_max - back_min;
382 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
383 char *last1 = s - 1; /* Last position checked before */
385 /* XXXX check_substr already used to find `s', can optimize if
386 check_substr==must. */
388 dontbother = end_shift;
389 strend -= dontbother;
390 while ( (s <= last) &&
392 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
393 end_shift, &scream_pos, 0))
394 : (s = fbm_instr((unsigned char*)s + back_min,
395 (unsigned char*)strend, must))) ) {
396 if (s - back_max > last1) {
397 last1 = s - back_min;
402 last1 = s - back_min;
412 } else if (c = prog->regstclass) {
413 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
417 dontbother = minlen - 1;
418 strend -= dontbother; /* don't bother with what can't match */
420 /* We know what class it must start with. */
423 Class = (char *) OPERAND(c);
425 if (reginclass(Class, *s)) {
426 if (tmp && regtry(prog, s))
437 reg_flags |= RF_tainted;
441 dontbother++,strend--;
442 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
443 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
445 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
452 if ((minlen || tmp) && regtry(prog,s))
456 reg_flags |= RF_tainted;
460 dontbother++,strend--;
461 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
462 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
464 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
466 else if (regtry(prog, s))
470 if ((minlen || !tmp) && regtry(prog,s))
476 if (tmp && regtry(prog, s))
487 reg_flags |= RF_tainted;
489 if (isALNUM_LC(*s)) {
490 if (tmp && regtry(prog, s))
503 if (tmp && regtry(prog, s))
514 reg_flags |= RF_tainted;
516 if (!isALNUM_LC(*s)) {
517 if (tmp && regtry(prog, s))
530 if (tmp && regtry(prog, s))
541 reg_flags |= RF_tainted;
543 if (isSPACE_LC(*s)) {
544 if (tmp && regtry(prog, s))
557 if (tmp && regtry(prog, s))
568 reg_flags |= RF_tainted;
570 if (!isSPACE_LC(*s)) {
571 if (tmp && regtry(prog, s))
584 if (tmp && regtry(prog, s))
597 if (tmp && regtry(prog, s))
611 if (prog->float_substr != Nullsv) { /* Trim the end. */
613 I32 oldpos = scream_pos;
616 last = screaminstr(screamer, prog->float_substr, s - strbeg,
617 end_shift, &scream_pos, 1); /* last one */
619 last = scream_olds; /* Only one occurence. */
623 char *little = SvPV(prog->float_substr, len);
624 last = rninstr(s, strend, little, little + len);
626 if (last == NULL) goto phooey; /* Should not happen! */
627 dontbother = strend - last - 1;
629 if (minlen && (dontbother < minlen))
630 dontbother = minlen - 1;
631 strend -= dontbother;
632 /* We don't know much -- general case. */
636 } while (s++ < strend);
643 strend += dontbother; /* uncheat */
644 prog->subbeg = strbeg;
645 prog->subend = strend;
646 RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
648 /* make sure $`, $&, $', and $digit will work later */
649 if (strbeg != prog->subbase) { /* second+ //g match. */
650 if (!(flags & REXEC_COPY_STR)) {
652 Safefree(prog->subbase);
653 prog->subbase = Nullch;
657 I32 i = strend - startpos + (stringarg - strbeg);
658 s = savepvn(strbeg, i);
659 Safefree(prog->subbase);
661 prog->subbeg = prog->subbase;
662 prog->subend = prog->subbase + i;
663 s = prog->subbase + (stringarg - strbeg);
664 for (i = 0; i <= prog->nparens; i++) {
666 prog->startp[i] = s + (prog->startp[i] - startpos);
667 prog->endp[i] = s + (prog->endp[i] - startpos);
679 - regtry - try match at specific point
681 static I32 /* 0 failure, 1 success */
682 regtry(regexp *prog, char *startpos)
691 regstartp = prog->startp;
692 regendp = prog->endp;
693 reglastparen = &prog->lastparen;
696 if (reg_start_tmpl <= prog->nparens) {
697 reg_start_tmpl = prog->nparens*3/2 + 3;
699 Renew(reg_start_tmp, reg_start_tmpl, char*);
701 New(22,reg_start_tmp, reg_start_tmpl, char*);
708 for (i = prog->nparens; i >= 0; i--) {
714 if (regmatch(prog->program + 1) && reginput >= regtill) {
715 prog->startp[0] = startpos;
716 prog->endp[0] = reginput;
724 - regmatch - main matching routine
726 * Conceptually the strategy is simple: check to see whether the current
727 * node matches, call self recursively to see whether the rest matches,
728 * and then act accordingly. In practice we make some effort to avoid
729 * recursion, in particular by going through "ordinary" nodes (that don't
730 * need to know whether the rest of the match failed) by a loop instead of
733 /* [lwall] I've hoisted the register declarations to the outer block in order to
734 * maybe save a little bit of pushing and popping on the stack. It also takes
735 * advantage of machines that use a register save mask on subroutine entry.
737 static I32 /* 0 failure, 1 success */
738 regmatch(regnode *prog)
741 register regnode *scan; /* Current node. */
742 regnode *next; /* Next node. */
743 regnode *inner; /* Next node in internal branch. */
744 register I32 nextchar;
745 register I32 n; /* no or next */
746 register I32 ln; /* len or last */
747 register char *s; /* operand or save */
748 register char *locinput = reginput;
749 register I32 c1, c2, paren; /* case fold search, parenth */
750 int minmod = 0, sw = 0, logical = 0;
752 static int regindent = 0;
756 nextchar = UCHARAT(locinput);
758 while (scan != NULL) {
759 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
761 # define sayYES goto yes
762 # define sayNO goto no
763 # define saySAME(x) if (x) goto yes; else goto no
764 # define REPORT_CODE_OFF 24
766 # define sayYES return 1
767 # define sayNO return 0
768 # define saySAME(x) return x
771 SV *prop = sv_newmortal();
772 int docolor = *colors[0];
773 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
774 int l = (regeol - locinput > taill ? taill : regeol - locinput);
775 int pref_len = (locinput - bostr > (5 + taill) - l
776 ? (5 + taill) - l : locinput - bostr);
778 if (l + pref_len < (5 + taill) && l < regeol - locinput)
779 l = ( regeol - locinput > (5 + taill) - pref_len
780 ? (5 + taill) - pref_len : regeol - locinput);
782 PerlIO_printf(Perl_debug_log,
783 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
785 colors[2], pref_len, locinput - pref_len, colors[3],
786 (docolor ? "" : "> <"),
787 colors[0], l, locinput, colors[1],
788 15 - l - pref_len + 1,
790 regindent*2, "", scan - regprogram,
795 next = scan + NEXT_OFF(scan);
799 next = regnext(scan);
804 if (locinput == regbol
807 (nextchar || locinput < regeol) && locinput[-1] == '\n') )
809 /* regtill = regbol; */
814 if (locinput == regbol
816 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
822 if (locinput == regbol && regprev == '\n')
826 if (locinput == regbol)
836 if ((nextchar || locinput < regeol) && nextchar != '\n')
841 if ((nextchar || locinput < regeol) && nextchar != '\n')
843 if (regeol - locinput > 1)
847 if (!nextchar && locinput >= regeol)
849 nextchar = UCHARAT(++locinput);
852 if (!nextchar && locinput >= regeol || nextchar == '\n')
854 nextchar = UCHARAT(++locinput);
857 s = (char *) OPERAND(scan);
859 /* Inline the first character, for speed. */
860 if (UCHARAT(s) != nextchar)
862 if (regeol - locinput < ln)
864 if (ln > 1 && memNE(s, locinput, ln))
867 nextchar = UCHARAT(locinput);
870 reg_flags |= RF_tainted;
873 s = (char *) OPERAND(scan);
875 /* Inline the first character, for speed. */
876 if (UCHARAT(s) != nextchar &&
877 UCHARAT(s) != ((OP(scan) == EXACTF)
878 ? fold : fold_locale)[nextchar])
880 if (regeol - locinput < ln)
882 if (ln > 1 && (OP(scan) == EXACTF
883 ? ibcmp(s, locinput, ln)
884 : ibcmp_locale(s, locinput, ln)))
887 nextchar = UCHARAT(locinput);
890 s = (char *) OPERAND(scan);
892 nextchar = UCHARAT(locinput);
893 if (!reginclass(s, nextchar))
895 if (!nextchar && locinput >= regeol)
897 nextchar = UCHARAT(++locinput);
900 reg_flags |= RF_tainted;
905 if (!(OP(scan) == ALNUM
906 ? isALNUM(nextchar) : isALNUM_LC(nextchar)))
908 nextchar = UCHARAT(++locinput);
911 reg_flags |= RF_tainted;
914 if (!nextchar && locinput >= regeol)
916 if (OP(scan) == NALNUM
917 ? isALNUM(nextchar) : isALNUM_LC(nextchar))
919 nextchar = UCHARAT(++locinput);
923 reg_flags |= RF_tainted;
927 /* was last char in word? */
928 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
929 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
931 n = isALNUM(nextchar);
935 n = isALNUM_LC(nextchar);
937 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
941 reg_flags |= RF_tainted;
944 if (!nextchar && locinput >= regeol)
946 if (!(OP(scan) == SPACE
947 ? isSPACE(nextchar) : isSPACE_LC(nextchar)))
949 nextchar = UCHARAT(++locinput);
952 reg_flags |= RF_tainted;
957 if (OP(scan) == SPACE
958 ? isSPACE(nextchar) : isSPACE_LC(nextchar))
960 nextchar = UCHARAT(++locinput);
963 if (!isDIGIT(nextchar))
965 nextchar = UCHARAT(++locinput);
968 if (!nextchar && locinput >= regeol)
970 if (isDIGIT(nextchar))
972 nextchar = UCHARAT(++locinput);
975 reg_flags |= RF_tainted;
979 n = ARG(scan); /* which paren pair */
981 if (*reglastparen < n || !s)
982 break; /* Zero length always matches */
985 /* Inline the first character, for speed. */
986 if (UCHARAT(s) != nextchar &&
988 (UCHARAT(s) != ((OP(scan) == REFF
989 ? fold : fold_locale)[nextchar]))))
992 if (locinput + ln > regeol)
994 if (ln > 1 && (OP(scan) == REF
995 ? memNE(s, locinput, ln)
997 ? ibcmp(s, locinput, ln)
998 : ibcmp_locale(s, locinput, ln))))
1001 nextchar = UCHARAT(locinput);
1013 COP *ocurcop = curcop;
1014 SV **ocurpad = curpad;
1018 op = (OP_4tree*)data->data[n];
1019 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1020 curpad = AvARRAY((AV*)data->data[n + 1]);
1021 if (!reg_eval_set) {
1022 /* Preserve whatever is on stack now, otherwise
1023 OP_NEXTSTATE will overwrite it. */
1024 SAVEINT(reg_eval_set); /* Protect against unwinding. */
1027 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
1029 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1030 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
1031 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1033 /* Apparently this is not needed, judging by wantarray. */
1034 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1035 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1038 runops(); /* Scalar context. */
1053 n = ARG(scan); /* which paren pair */
1054 reg_start_tmp[n] = locinput;
1059 n = ARG(scan); /* which paren pair */
1060 regstartp[n] = reg_start_tmp[n];
1061 regendp[n] = locinput;
1062 if (n > *reglastparen)
1066 n = ARG(scan); /* which paren pair */
1067 sw = (*reglastparen >= n && regendp[n] != NULL);
1071 next = NEXTOPER(NEXTOPER(scan));
1073 next = scan + ARG(scan);
1074 if (OP(next) == IFTHEN) /* Fake one. */
1075 next = NEXTOPER(NEXTOPER(next));
1083 CHECKPOINT cp = savestack_ix;
1085 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1089 cc.parenfloor = *reglastparen;
1091 cc.min = ARG1(scan);
1092 cc.max = ARG2(scan);
1093 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1097 reginput = locinput;
1098 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1106 * This is really hard to understand, because after we match
1107 * what we're trying to match, we must make sure the rest of
1108 * the RE is going to match for sure, and to do that we have
1109 * to go back UP the parse tree by recursing ever deeper. And
1110 * if it fails, we have to reset our parent's current state
1111 * that we can try again after backing off.
1114 CHECKPOINT cp, lastcp;
1116 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1118 n = cc->cur + 1; /* how many we know we matched */
1119 reginput = locinput;
1122 PerlIO_printf(Perl_debug_log,
1123 "%*s %ld out of %ld..%ld cc=%lx\n",
1124 REPORT_CODE_OFF+regindent*2, "",
1125 (long)n, (long)cc->min,
1126 (long)cc->max, (long)cc)
1129 /* If degenerate scan matches "", assume scan done. */
1131 if (locinput == cc->lastloc && n >= cc->min) {
1135 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1137 if (regmatch(cc->next))
1140 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1147 /* First just match a string of min scans. */
1151 cc->lastloc = locinput;
1152 if (regmatch(cc->scan))
1155 cc->lastloc = lastloc;
1157 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1162 /* Prefer next over scan for minimal matching. */
1167 cp = regcppush(cc->parenfloor);
1169 if (regmatch(cc->next)) {
1171 sayYES; /* All done. */
1178 if (n >= cc->max) { /* Maximum greed exceeded? */
1179 if (dowarn && n >= REG_INFTY
1180 && !(reg_flags & RF_warned)) {
1181 reg_flags |= RF_warned;
1182 warn("count exceeded %d", REG_INFTY - 1);
1188 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1190 /* Try scanning more and see if it helps. */
1191 reginput = locinput;
1193 cc->lastloc = locinput;
1194 cp = regcppush(cc->parenfloor);
1196 if (regmatch(cc->scan)) {
1201 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1206 cc->lastloc = lastloc;
1210 /* Prefer scan over next for maximal matching. */
1212 if (n < cc->max) { /* More greed allowed? */
1213 cp = regcppush(cc->parenfloor);
1215 cc->lastloc = locinput;
1217 if (regmatch(cc->scan)) {
1222 regcppop(); /* Restore some previous $<digit>s? */
1223 reginput = locinput;
1225 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1228 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1229 reg_flags |= RF_warned;
1230 warn("count exceeded %d", REG_INFTY - 1);
1233 /* Failed deeper matches of scan, so see if this one works. */
1236 if (regmatch(cc->next))
1239 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1244 cc->lastloc = lastloc;
1249 next = scan + ARG(scan);
1252 inner = NEXTOPER(NEXTOPER(scan));
1255 inner = NEXTOPER(scan);
1260 if (OP(next) != c1) /* No choice. */
1261 next = inner; /* Avoid recursion. */
1263 int lastparen = *reglastparen;
1267 reginput = locinput;
1268 if (regmatch(inner))
1271 for (n = *reglastparen; n > lastparen; n--)
1277 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1282 next = regnext(next);
1284 inner = NEXTOPER(scan);
1285 if (c1 == BRANCHJ) {
1286 inner = NEXTOPER(inner);
1288 } while (scan != NULL && OP(scan) == c1);
1302 /* We suppose that the next guy does not need
1303 backtracking: in particular, it is of constant length,
1304 and has no parenths to influence future backrefs. */
1305 ln = ARG1(scan); /* min to match */
1306 n = ARG2(scan); /* max to match */
1307 #ifdef REGALIGN_STRUCT
1308 paren = scan->flags;
1310 if (paren > regsize)
1312 if (paren > *reglastparen)
1313 *reglastparen = paren;
1316 scan = NEXTOPER(scan) + 4/sizeof(regnode);
1318 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1319 reginput = locinput;
1322 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1324 if (l == 0 && n >= ln
1325 /* In fact, this is tricky. If paren, then the
1326 fact that we did/didnot match may influence
1327 future execution. */
1328 && !(paren && ln == 0))
1330 locinput = reginput;
1331 if (regkind[(U8)OP(next)] == EXACT) {
1332 c1 = UCHARAT(OPERAND(next) + 1);
1333 if (OP(next) == EXACTF)
1335 else if (OP(next) == EXACTFL)
1336 c2 = fold_locale[c1];
1342 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1343 /* If it could work, try it. */
1345 UCHARAT(reginput) == c1 ||
1346 UCHARAT(reginput) == c2)
1350 regstartp[paren] = reginput - l;
1351 regendp[paren] = reginput;
1353 regendp[paren] = NULL;
1359 /* Couldn't or didn't -- move forward. */
1360 reginput = locinput;
1361 if (regrepeat_hard(scan, 1, &l)) {
1363 locinput = reginput;
1369 n = regrepeat_hard(scan, n, &l);
1370 if (n != 0 && l == 0
1371 /* In fact, this is tricky. If paren, then the
1372 fact that we did/didnot match may influence
1373 future execution. */
1374 && !(paren && ln == 0))
1376 locinput = reginput;
1378 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1381 if (regkind[(U8)OP(next)] == EXACT) {
1382 c1 = UCHARAT(OPERAND(next) + 1);
1383 if (OP(next) == EXACTF)
1385 else if (OP(next) == EXACTFL)
1386 c2 = fold_locale[c1];
1394 /* If it could work, try it. */
1396 UCHARAT(reginput) == c1 ||
1397 UCHARAT(reginput) == c2)
1400 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1404 regstartp[paren] = reginput - l;
1405 regendp[paren] = reginput;
1407 regendp[paren] = NULL;
1413 /* Couldn't or didn't -- back up. */
1416 reginput = locinput;
1423 paren = scan->flags; /* Which paren to set */
1424 if (paren > regsize)
1426 if (paren > *reglastparen)
1427 *reglastparen = paren;
1428 ln = ARG1(scan); /* min to match */
1429 n = ARG2(scan); /* max to match */
1430 scan = regnext(NEXTOPER(scan) + 4/sizeof(regnode));
1434 ln = ARG1(scan); /* min to match */
1435 n = ARG2(scan); /* max to match */
1436 scan = NEXTOPER(scan) + 4/sizeof(regnode);
1441 scan = NEXTOPER(scan);
1447 scan = NEXTOPER(scan);
1451 * Lookahead to avoid useless match attempts
1452 * when we know what character comes next.
1454 if (regkind[(U8)OP(next)] == EXACT) {
1455 c1 = UCHARAT(OPERAND(next) + 1);
1456 if (OP(next) == EXACTF)
1458 else if (OP(next) == EXACTFL)
1459 c2 = fold_locale[c1];
1465 reginput = locinput;
1469 if (ln && regrepeat(scan, ln) < ln)
1472 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1473 /* If it could work, try it. */
1475 UCHARAT(reginput) == c1 ||
1476 UCHARAT(reginput) == c2)
1480 regstartp[paren] = reginput - 1;
1481 regendp[paren] = reginput;
1483 regendp[paren] = NULL;
1489 /* Couldn't or didn't -- move forward. */
1490 reginput = locinput + ln;
1491 if (regrepeat(scan, 1)) {
1493 reginput = locinput + ln;
1500 n = regrepeat(scan, n);
1501 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1502 (!multiline || OP(next) == SEOL))
1503 ln = n; /* why back off? */
1507 /* If it could work, try it. */
1509 UCHARAT(reginput) == c1 ||
1510 UCHARAT(reginput) == c2)
1514 regstartp[paren] = reginput - 1;
1515 regendp[paren] = reginput;
1517 regendp[paren] = NULL;
1523 /* Couldn't or didn't -- back up. */
1525 reginput = locinput + n;
1529 /* If it could work, try it. */
1531 UCHARAT(reginput) == c1 ||
1532 UCHARAT(reginput) == c2)
1538 /* Couldn't or didn't -- back up. */
1540 reginput = locinput + n;
1548 reginput = locinput; /* put where regtry can find it */
1549 sayYES; /* Success! */
1555 if (locinput < bostr + scan->flags)
1560 if (locinput < bostr + scan->flags)
1563 reginput = locinput - scan->flags;
1564 inner = NEXTOPER(NEXTOPER(scan));
1565 if (regmatch(inner) != n) {
1579 if (OP(scan) == SUSPEND) {
1580 locinput = reginput;
1581 nextchar = UCHARAT(locinput);
1586 next = scan + ARG(scan);
1591 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1592 (unsigned long)scan, OP(scan));
1593 FAIL("regexp memory corruption");
1599 * We get here only if there's trouble -- normally "case END" is
1600 * the terminating point.
1602 FAIL("corrupted regexp pointers");
1620 - regrepeat - repeatedly match something simple, report how many
1623 * [This routine now assumes that it will only match on things of length 1.
1624 * That was true before, but now we assume scan - reginput is the count,
1625 * rather than incrementing count on every character.]
1628 regrepeat(regnode *p, I32 max)
1630 register char *scan;
1631 register char *opnd;
1633 register char *loceol = regeol;
1636 if (max != REG_INFTY && max < loceol - scan)
1637 loceol = scan + max;
1638 opnd = (char *) OPERAND(p);
1641 while (scan < loceol && *scan != '\n')
1647 case EXACT: /* length of string is 1 */
1648 c = UCHARAT(++opnd);
1649 while (scan < loceol && UCHARAT(scan) == c)
1652 case EXACTF: /* length of string is 1 */
1653 c = UCHARAT(++opnd);
1654 while (scan < loceol &&
1655 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1658 case EXACTFL: /* length of string is 1 */
1659 reg_flags |= RF_tainted;
1660 c = UCHARAT(++opnd);
1661 while (scan < loceol &&
1662 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1666 while (scan < loceol && reginclass(opnd, *scan))
1670 while (scan < loceol && isALNUM(*scan))
1674 reg_flags |= RF_tainted;
1675 while (scan < loceol && isALNUM_LC(*scan))
1679 while (scan < loceol && !isALNUM(*scan))
1683 reg_flags |= RF_tainted;
1684 while (scan < loceol && !isALNUM_LC(*scan))
1688 while (scan < loceol && isSPACE(*scan))
1692 reg_flags |= RF_tainted;
1693 while (scan < loceol && isSPACE_LC(*scan))
1697 while (scan < loceol && !isSPACE(*scan))
1701 reg_flags |= RF_tainted;
1702 while (scan < loceol && !isSPACE_LC(*scan))
1706 while (scan < loceol && isDIGIT(*scan))
1710 while (scan < loceol && !isDIGIT(*scan))
1713 default: /* Called on something of 0 width. */
1714 break; /* So match right here or not at all. */
1717 c = scan - reginput;
1722 SV *prop = sv_newmortal();
1725 PerlIO_printf(Perl_debug_log,
1726 "%*s %s can match %ld times out of %ld...\n",
1727 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1734 - regrepeat_hard - repeatedly match something, report total lenth and length
1736 * The repeater is supposed to have constant length.
1740 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1742 register char *scan;
1743 register char *start;
1744 register char *loceol = regeol;
1748 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1750 *lp = l = reginput - start;
1751 if (max != REG_INFTY && l*max < loceol - scan)
1752 loceol = scan + l*max;
1758 if (reginput < loceol)
1763 return (scan - start)/l;
1767 - regclass - determine if a character falls into a character class
1771 reginclass(register char *p, register I32 c)
1777 if (p[1 + (c >> 3)] & (1 << (c & 7)))
1779 else if (flags & ANYOF_FOLD) {
1781 if (flags & ANYOF_LOCALE) {
1782 reg_flags |= RF_tainted;
1783 cf = fold_locale[c];
1787 if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1791 if (!match && (flags & ANYOF_ISA)) {
1792 reg_flags |= RF_tainted;
1794 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1795 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1796 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1797 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1803 return match ^ ((flags & ANYOF_INVERT) != 0);