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 #define RF_tainted 1 /* tainted information used? */
59 #define RF_warned 2 /* warned about big count? */
60 #define RF_evaled 4 /* Did an EVAL? */
67 typedef I32 CHECKPOINT;
73 static I32 regmatch _((regnode *prog));
74 static I32 regrepeat _((regnode *p, I32 max));
75 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
76 static I32 regtry _((regexp *prog, char *startpos));
77 static bool reginclass _((char *p, I32 c));
78 static CHECKPOINT regcppush _((I32 parenfloor));
79 static char * regcppop _((void));
83 regcppush(I32 parenfloor)
86 int retval = savestack_ix;
87 int i = (regsize - parenfloor) * 4;
91 for (p = regsize; p > parenfloor; p--) {
92 SSPUSHPTR(regendp[p]);
93 SSPUSHPTR(regstartp[p]);
94 SSPUSHPTR(reg_start_tmp[p]);
98 SSPUSHINT(*reglastparen);
101 SSPUSHINT(SAVEt_REGCONTEXT);
105 /* These are needed since we do not localize EVAL nodes: */
106 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
107 # 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)
117 assert(i == SAVEt_REGCONTEXT);
119 input = (char *) SSPOPPTR;
120 *reglastparen = SSPOPINT;
122 for (i -= 3; i > 0; i -= 4) {
123 paren = (U32)SSPOPINT;
124 reg_start_tmp[paren] = (char *) SSPOPPTR;
125 regstartp[paren] = (char *) SSPOPPTR;
126 tmps = (char*)SSPOPPTR;
127 if (paren <= *reglastparen)
128 regendp[paren] = tmps;
130 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
131 paren, regstartp[paren] - regbol,
132 reg_start_tmp[paren] - regbol,
133 regendp[paren] - regbol,
134 (paren > *reglastparen ? "(no)" : ""));
138 if (*reglastparen + 1 <= regnpar) {
139 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
140 *reglastparen + 1, regnpar);
143 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
145 regstartp[paren] = Nullch;
146 regendp[paren] = Nullch;
151 #define regcpblow(cp) LEAVE_SCOPE(cp)
154 * pregexec and friends
158 - pregexec - match a regexp against a string
161 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
162 /* strend: pointer to null at end of string */
163 /* strbeg: real beginning of string */
164 /* minend: end of match must be >=minend after stringarg. */
165 /* nosave: For optimizations. */
168 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
169 nosave ? 0 : REXEC_COPY_STR);
173 - regexec_flags - match a regexp against a string
176 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
177 /* strend: pointer to null at end of string */
178 /* strbeg: real beginning of string */
179 /* minend: end of match must be >=minend after stringarg. */
180 /* data: May be used for some additional optimizations. */
181 /* nosave: For optimizations. */
185 register char *startpos = stringarg;
187 I32 minlen; /* must match at least this many chars */
188 I32 dontbother = 0; /* how many characters not to try at end */
190 I32 start_shift = 0; /* Offset of the start to find
192 I32 end_shift = 0; /* Same for the end. */
193 I32 scream_pos = -1; /* Internal iterator of scream. */
200 regprecomp = prog->precomp; /* Needed for error messages. */
202 regnarrate = debug & 512;
203 regprogram = prog->program;
207 if (prog == NULL || startpos == NULL) {
208 croak("NULL regexp parameter");
212 minlen = prog->minlen;
213 if (strend - startpos < minlen) goto phooey;
215 if (startpos == strbeg) /* is ^ valid at stringarg? */
218 regprev = stringarg[-1];
219 if (!multiline && regprev == '\n')
220 regprev = '\0'; /* force ^ to NOT match */
223 /* Check validity of program. */
224 if (UCHARAT(prog->program) != MAGIC) {
225 FAIL("corrupted regexp program");
228 regnpar = prog->nparens;
232 /* If there is a "must appear" string, look for it. */
234 if (!(flags & REXEC_CHECKED)
235 && prog->check_substr != Nullsv &&
236 !(prog->reganch & ROPT_ANCH_GPOS) &&
237 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
238 || (multiline && prog->check_substr == prog->anchored_substr)) )
240 start_shift = prog->check_offset_min;
241 /* Should be nonnegative! */
242 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
244 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
245 s = screaminstr(screamer, prog->check_substr,
246 start_shift + (stringarg - strbeg),
247 end_shift, &scream_pos, 0);
253 s = fbm_instr((unsigned char*)s + start_shift,
254 (unsigned char*)strend - end_shift,
257 ++BmUSEFUL(prog->check_substr); /* hooray */
258 goto phooey; /* not present */
259 } else if ((s - stringarg) > prog->check_offset_max) {
260 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
261 s -= prog->check_offset_max;
262 } else if (!prog->naughty
263 && --BmUSEFUL(prog->check_substr) < 0
264 && prog->check_substr == prog->float_substr) { /* boo */
265 SvREFCNT_dec(prog->check_substr);
266 prog->check_substr = Nullsv; /* disable */
267 prog->float_substr = Nullsv; /* clear */
272 /* Mark beginning of line for ^ and lookbehind. */
276 /* Mark end of line for $ (and such) */
279 /* see how far we have to get to not match where we matched before */
280 regtill = startpos+minend;
283 PerlIO_printf(Perl_debug_log,
284 "Matching `%.60s%s' against `%.*s%s'\n",
286 (strlen(prog->precomp) > 60 ? "..." : ""),
287 (strend - startpos > 60 ? 60 : strend - startpos),
289 (strend - startpos > 60 ? "..." : ""))
292 /* Simplest case: anchored match need be tried only once. */
293 /* [unless only anchor is BOL and multiline is set] */
294 if (prog->reganch & ROPT_ANCH) {
295 if (regtry(prog, startpos))
297 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
298 (multiline || (prog->reganch & ROPT_IMPLICIT)
299 || (prog->reganch & ROPT_ANCH_MBOL)))
302 dontbother = minlen - 1;
303 strend -= dontbother;
304 /* for multiline we only have to try after newlines */
309 if (s < strend && regtry(prog, s))
317 /* Messy cases: unanchored match. */
318 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
319 /* we have /x+whatever/ */
320 /* it must be a one character string */
321 char ch = SvPVX(prog->anchored_substr)[0];
324 if (regtry(prog, s)) goto got_it;
326 while (s < strend && *s == ch)
333 else if (prog->anchored_substr != Nullsv
334 || (prog->float_substr != Nullsv
335 && prog->float_max_offset < strend - s)) {
336 SV *must = prog->anchored_substr
337 ? prog->anchored_substr : prog->float_substr;
339 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
341 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
342 I32 delta = back_max - back_min;
343 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
344 char *last1 = s - 1; /* Last position checked before */
346 /* XXXX check_substr already used to find `s', can optimize if
347 check_substr==must. */
349 dontbother = end_shift;
350 strend -= dontbother;
351 while ( (s <= last) &&
353 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
354 end_shift, &scream_pos, 0))
355 : (s = fbm_instr((unsigned char*)s + back_min,
356 (unsigned char*)strend, must))) ) {
357 if (s - back_max > last1) {
358 last1 = s - back_min;
363 last1 = s - back_min;
373 } else if (c = prog->regstclass) {
374 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
378 dontbother = minlen - 1;
379 strend -= dontbother; /* don't bother with what can't match */
381 /* We know what class it must start with. */
384 Class = (char *) OPERAND(c);
386 if (reginclass(Class, *s)) {
387 if (tmp && regtry(prog, s))
398 reg_flags |= RF_tainted;
402 dontbother++,strend--;
403 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
404 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
406 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
413 if ((minlen || tmp) && regtry(prog,s))
417 reg_flags |= RF_tainted;
421 dontbother++,strend--;
422 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
423 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
425 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
427 else if (regtry(prog, s))
431 if ((minlen || !tmp) && regtry(prog,s))
437 if (tmp && regtry(prog, s))
448 reg_flags |= RF_tainted;
450 if (isALNUM_LC(*s)) {
451 if (tmp && regtry(prog, s))
464 if (tmp && regtry(prog, s))
475 reg_flags |= RF_tainted;
477 if (!isALNUM_LC(*s)) {
478 if (tmp && regtry(prog, s))
491 if (tmp && regtry(prog, s))
502 reg_flags |= RF_tainted;
504 if (isSPACE_LC(*s)) {
505 if (tmp && regtry(prog, s))
518 if (tmp && regtry(prog, s))
529 reg_flags |= RF_tainted;
531 if (!isSPACE_LC(*s)) {
532 if (tmp && regtry(prog, s))
545 if (tmp && regtry(prog, s))
558 if (tmp && regtry(prog, s))
572 if (prog->float_substr != Nullsv) { /* Trim the end. */
574 I32 oldpos = scream_pos;
577 last = screaminstr(screamer, prog->float_substr, s - strbeg,
578 end_shift, &scream_pos, 1); /* last one */
580 last = scream_olds; /* Only one occurence. */
584 char *little = SvPV(prog->float_substr, len);
585 last = rninstr(s, strend, little, little + len);
587 if (last == NULL) goto phooey; /* Should not happen! */
588 dontbother = strend - last - 1;
590 if (minlen && (dontbother < minlen))
591 dontbother = minlen - 1;
592 strend -= dontbother;
593 /* We don't know much -- general case. */
597 } while (s++ < strend);
604 strend += dontbother; /* uncheat */
605 prog->subbeg = strbeg;
606 prog->subend = strend;
607 RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
609 /* make sure $`, $&, $', and $digit will work later */
610 if (strbeg != prog->subbase) { /* second+ //g match. */
611 if (!(flags & REXEC_COPY_STR)) {
613 Safefree(prog->subbase);
614 prog->subbase = Nullch;
618 I32 i = strend - startpos + (stringarg - strbeg);
619 s = savepvn(strbeg, i);
620 Safefree(prog->subbase);
622 prog->subbeg = prog->subbase;
623 prog->subend = prog->subbase + i;
624 s = prog->subbase + (stringarg - strbeg);
625 for (i = 0; i <= prog->nparens; i++) {
627 prog->startp[i] = s + (prog->startp[i] - startpos);
628 prog->endp[i] = s + (prog->endp[i] - startpos);
640 - regtry - try match at specific point
642 STATIC I32 /* 0 failure, 1 success */
643 regtry(regexp *prog, char *startpos)
652 regstartp = prog->startp;
653 regendp = prog->endp;
654 reglastparen = &prog->lastparen;
657 if (reg_start_tmpl <= prog->nparens) {
658 reg_start_tmpl = prog->nparens*3/2 + 3;
660 Renew(reg_start_tmp, reg_start_tmpl, char*);
662 New(22,reg_start_tmp, reg_start_tmpl, char*);
667 regdata = prog->data;
669 for (i = prog->nparens; i >= 0; i--) {
675 if (regmatch(prog->program + 1) && reginput >= regtill) {
676 prog->startp[0] = startpos;
677 prog->endp[0] = reginput;
685 - regmatch - main matching routine
687 * Conceptually the strategy is simple: check to see whether the current
688 * node matches, call self recursively to see whether the rest matches,
689 * and then act accordingly. In practice we make some effort to avoid
690 * recursion, in particular by going through "ordinary" nodes (that don't
691 * need to know whether the rest of the match failed) by a loop instead of
694 /* [lwall] I've hoisted the register declarations to the outer block in order to
695 * maybe save a little bit of pushing and popping on the stack. It also takes
696 * advantage of machines that use a register save mask on subroutine entry.
698 STATIC I32 /* 0 failure, 1 success */
699 regmatch(regnode *prog)
702 register regnode *scan; /* Current node. */
703 regnode *next; /* Next node. */
704 regnode *inner; /* Next node in internal branch. */
705 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
706 register I32 n; /* no or next */
707 register I32 ln; /* len or last */
708 register char *s; /* operand or save */
709 register char *locinput = reginput;
710 register I32 c1, c2, paren; /* case fold search, parenth */
711 int minmod = 0, sw = 0, logical = 0;
716 nextchr = UCHARAT(locinput);
718 while (scan != NULL) {
719 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
721 # define sayYES goto yes
722 # define sayNO goto no
723 # define saySAME(x) if (x) goto yes; else goto no
724 # define REPORT_CODE_OFF 24
726 # define sayYES return 1
727 # define sayNO return 0
728 # define saySAME(x) return x
731 SV *prop = sv_newmortal();
732 int docolor = *colors[0];
733 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
734 int l = (regeol - locinput > taill ? taill : regeol - locinput);
735 int pref_len = (locinput - bostr > (5 + taill) - l
736 ? (5 + taill) - l : locinput - bostr);
738 if (l + pref_len < (5 + taill) && l < regeol - locinput)
739 l = ( regeol - locinput > (5 + taill) - pref_len
740 ? (5 + taill) - pref_len : regeol - locinput);
742 PerlIO_printf(Perl_debug_log,
743 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
745 colors[2], pref_len, locinput - pref_len, colors[3],
746 (docolor ? "" : "> <"),
747 colors[0], l, locinput, colors[1],
748 15 - l - pref_len + 1,
750 regindent*2, "", scan - regprogram,
755 next = scan + NEXT_OFF(scan);
759 next = regnext(scan);
764 if (locinput == regbol
767 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
769 /* regtill = regbol; */
774 if (locinput == regbol
776 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
782 if (locinput == regbol && regprev == '\n')
786 if (locinput == regbol)
796 if ((nextchr || locinput < regeol) && nextchr != '\n')
801 if ((nextchr || locinput < regeol) && nextchr != '\n')
803 if (regeol - locinput > 1)
807 if (!nextchr && locinput >= regeol)
809 nextchr = UCHARAT(++locinput);
812 if (!nextchr && locinput >= regeol || nextchr == '\n')
814 nextchr = UCHARAT(++locinput);
817 s = (char *) OPERAND(scan);
819 /* Inline the first character, for speed. */
820 if (UCHARAT(s) != nextchr)
822 if (regeol - locinput < ln)
824 if (ln > 1 && memNE(s, locinput, ln))
827 nextchr = UCHARAT(locinput);
830 reg_flags |= RF_tainted;
833 s = (char *) OPERAND(scan);
835 /* Inline the first character, for speed. */
836 if (UCHARAT(s) != nextchr &&
837 UCHARAT(s) != ((OP(scan) == EXACTF)
838 ? fold : fold_locale)[nextchr])
840 if (regeol - locinput < ln)
842 if (ln > 1 && (OP(scan) == EXACTF
843 ? ibcmp(s, locinput, ln)
844 : ibcmp_locale(s, locinput, ln)))
847 nextchr = UCHARAT(locinput);
850 s = (char *) OPERAND(scan);
852 nextchr = UCHARAT(locinput);
853 if (!reginclass(s, nextchr))
855 if (!nextchr && locinput >= regeol)
857 nextchr = UCHARAT(++locinput);
860 reg_flags |= RF_tainted;
865 if (!(OP(scan) == ALNUM
866 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
868 nextchr = UCHARAT(++locinput);
871 reg_flags |= RF_tainted;
874 if (!nextchr && locinput >= regeol)
876 if (OP(scan) == NALNUM
877 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
879 nextchr = UCHARAT(++locinput);
883 reg_flags |= RF_tainted;
887 /* was last char in word? */
888 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
889 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
891 n = isALNUM(nextchr);
895 n = isALNUM_LC(nextchr);
897 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
901 reg_flags |= RF_tainted;
904 if (!nextchr && locinput >= regeol)
906 if (!(OP(scan) == SPACE
907 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
909 nextchr = UCHARAT(++locinput);
912 reg_flags |= RF_tainted;
917 if (OP(scan) == SPACE
918 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
920 nextchr = UCHARAT(++locinput);
923 if (!isDIGIT(nextchr))
925 nextchr = UCHARAT(++locinput);
928 if (!nextchr && locinput >= regeol)
930 if (isDIGIT(nextchr))
932 nextchr = UCHARAT(++locinput);
935 reg_flags |= RF_tainted;
939 n = ARG(scan); /* which paren pair */
941 if (*reglastparen < n || !s)
942 sayNO; /* Do not match unless seen CLOSEn. */
945 /* Inline the first character, for speed. */
946 if (UCHARAT(s) != nextchr &&
948 (UCHARAT(s) != ((OP(scan) == REFF
949 ? fold : fold_locale)[nextchr]))))
952 if (locinput + ln > regeol)
954 if (ln > 1 && (OP(scan) == REF
955 ? memNE(s, locinput, ln)
957 ? ibcmp(s, locinput, ln)
958 : ibcmp_locale(s, locinput, ln))))
961 nextchr = UCHARAT(locinput);
973 COP *ocurcop = curcop;
974 SV **ocurpad = curpad;
978 op = (OP_4tree*)regdata->data[n];
979 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
980 curpad = AvARRAY((AV*)regdata->data[n + 1]);
982 /* Preserve whatever is on stack now, otherwise
983 OP_NEXTSTATE will overwrite it. */
984 SAVEINT(reg_eval_set); /* Protect against unwinding. */
987 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
989 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
990 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
991 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
993 /* Apparently this is not needed, judging by wantarray. */
994 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
995 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
998 CALLRUNOPS(); /* Scalar context. */
1013 n = ARG(scan); /* which paren pair */
1014 reg_start_tmp[n] = locinput;
1019 n = ARG(scan); /* which paren pair */
1020 regstartp[n] = reg_start_tmp[n];
1021 regendp[n] = locinput;
1022 if (n > *reglastparen)
1026 n = ARG(scan); /* which paren pair */
1027 sw = (*reglastparen >= n && regendp[n] != NULL);
1031 next = NEXTOPER(NEXTOPER(scan));
1033 next = scan + ARG(scan);
1034 if (OP(next) == IFTHEN) /* Fake one. */
1035 next = NEXTOPER(NEXTOPER(next));
1043 CHECKPOINT cp = savestack_ix;
1045 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1049 cc.parenfloor = *reglastparen;
1051 cc.min = ARG1(scan);
1052 cc.max = ARG2(scan);
1053 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1057 reginput = locinput;
1058 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1066 * This is really hard to understand, because after we match
1067 * what we're trying to match, we must make sure the rest of
1068 * the RE is going to match for sure, and to do that we have
1069 * to go back UP the parse tree by recursing ever deeper. And
1070 * if it fails, we have to reset our parent's current state
1071 * that we can try again after backing off.
1074 CHECKPOINT cp, lastcp;
1076 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1078 n = cc->cur + 1; /* how many we know we matched */
1079 reginput = locinput;
1082 PerlIO_printf(Perl_debug_log,
1083 "%*s %ld out of %ld..%ld cc=%lx\n",
1084 REPORT_CODE_OFF+regindent*2, "",
1085 (long)n, (long)cc->min,
1086 (long)cc->max, (long)cc)
1089 /* If degenerate scan matches "", assume scan done. */
1091 if (locinput == cc->lastloc && n >= cc->min) {
1095 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1097 if (regmatch(cc->next))
1100 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1107 /* First just match a string of min scans. */
1111 cc->lastloc = locinput;
1112 if (regmatch(cc->scan))
1115 cc->lastloc = lastloc;
1117 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1122 /* Prefer next over scan for minimal matching. */
1127 cp = regcppush(cc->parenfloor);
1129 if (regmatch(cc->next)) {
1131 sayYES; /* All done. */
1138 if (n >= cc->max) { /* Maximum greed exceeded? */
1139 if (dowarn && n >= REG_INFTY
1140 && !(reg_flags & RF_warned)) {
1141 reg_flags |= RF_warned;
1142 warn("count exceeded %d", REG_INFTY - 1);
1148 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1150 /* Try scanning more and see if it helps. */
1151 reginput = locinput;
1153 cc->lastloc = locinput;
1154 cp = regcppush(cc->parenfloor);
1156 if (regmatch(cc->scan)) {
1161 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1166 cc->lastloc = lastloc;
1170 /* Prefer scan over next for maximal matching. */
1172 if (n < cc->max) { /* More greed allowed? */
1173 cp = regcppush(cc->parenfloor);
1175 cc->lastloc = locinput;
1177 if (regmatch(cc->scan)) {
1182 regcppop(); /* Restore some previous $<digit>s? */
1183 reginput = locinput;
1185 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1188 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1189 reg_flags |= RF_warned;
1190 warn("count exceeded %d", REG_INFTY - 1);
1193 /* Failed deeper matches of scan, so see if this one works. */
1196 if (regmatch(cc->next))
1199 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1204 cc->lastloc = lastloc;
1209 next = scan + ARG(scan);
1212 inner = NEXTOPER(NEXTOPER(scan));
1215 inner = NEXTOPER(scan);
1220 if (OP(next) != c1) /* No choice. */
1221 next = inner; /* Avoid recursion. */
1223 int lastparen = *reglastparen;
1227 reginput = locinput;
1228 if (regmatch(inner))
1231 for (n = *reglastparen; n > lastparen; n--)
1237 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1242 next = regnext(next);
1244 inner = NEXTOPER(scan);
1245 if (c1 == BRANCHJ) {
1246 inner = NEXTOPER(inner);
1248 } while (scan != NULL && OP(scan) == c1);
1262 /* We suppose that the next guy does not need
1263 backtracking: in particular, it is of constant length,
1264 and has no parenths to influence future backrefs. */
1265 ln = ARG1(scan); /* min to match */
1266 n = ARG2(scan); /* max to match */
1267 #ifdef REGALIGN_STRUCT
1268 paren = scan->flags;
1270 if (paren > regsize)
1272 if (paren > *reglastparen)
1273 *reglastparen = paren;
1276 scan = NEXTOPER(scan) + 4/sizeof(regnode);
1278 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1279 reginput = locinput;
1282 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1284 if (l == 0 && n >= ln
1285 /* In fact, this is tricky. If paren, then the
1286 fact that we did/didnot match may influence
1287 future execution. */
1288 && !(paren && ln == 0))
1290 locinput = reginput;
1291 if (regkind[(U8)OP(next)] == EXACT) {
1292 c1 = UCHARAT(OPERAND(next) + 1);
1293 if (OP(next) == EXACTF)
1295 else if (OP(next) == EXACTFL)
1296 c2 = fold_locale[c1];
1302 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1303 /* If it could work, try it. */
1305 UCHARAT(reginput) == c1 ||
1306 UCHARAT(reginput) == c2)
1310 regstartp[paren] = reginput - l;
1311 regendp[paren] = reginput;
1313 regendp[paren] = NULL;
1319 /* Couldn't or didn't -- move forward. */
1320 reginput = locinput;
1321 if (regrepeat_hard(scan, 1, &l)) {
1323 locinput = reginput;
1329 n = regrepeat_hard(scan, n, &l);
1330 if (n != 0 && l == 0
1331 /* In fact, this is tricky. If paren, then the
1332 fact that we did/didnot match may influence
1333 future execution. */
1334 && !(paren && ln == 0))
1336 locinput = reginput;
1338 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1341 if (regkind[(U8)OP(next)] == EXACT) {
1342 c1 = UCHARAT(OPERAND(next) + 1);
1343 if (OP(next) == EXACTF)
1345 else if (OP(next) == EXACTFL)
1346 c2 = fold_locale[c1];
1354 /* If it could work, try it. */
1356 UCHARAT(reginput) == c1 ||
1357 UCHARAT(reginput) == c2)
1360 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1364 regstartp[paren] = reginput - l;
1365 regendp[paren] = reginput;
1367 regendp[paren] = NULL;
1373 /* Couldn't or didn't -- back up. */
1376 reginput = locinput;
1383 paren = scan->flags; /* Which paren to set */
1384 if (paren > regsize)
1386 if (paren > *reglastparen)
1387 *reglastparen = paren;
1388 ln = ARG1(scan); /* min to match */
1389 n = ARG2(scan); /* max to match */
1390 scan = regnext(NEXTOPER(scan) + 4/sizeof(regnode));
1394 ln = ARG1(scan); /* min to match */
1395 n = ARG2(scan); /* max to match */
1396 scan = NEXTOPER(scan) + 4/sizeof(regnode);
1401 scan = NEXTOPER(scan);
1407 scan = NEXTOPER(scan);
1411 * Lookahead to avoid useless match attempts
1412 * when we know what character comes next.
1414 if (regkind[(U8)OP(next)] == EXACT) {
1415 c1 = UCHARAT(OPERAND(next) + 1);
1416 if (OP(next) == EXACTF)
1418 else if (OP(next) == EXACTFL)
1419 c2 = fold_locale[c1];
1425 reginput = locinput;
1429 if (ln && regrepeat(scan, ln) < ln)
1432 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1433 /* If it could work, try it. */
1435 UCHARAT(reginput) == c1 ||
1436 UCHARAT(reginput) == c2)
1440 regstartp[paren] = reginput - 1;
1441 regendp[paren] = reginput;
1443 regendp[paren] = NULL;
1449 /* Couldn't or didn't -- move forward. */
1450 reginput = locinput + ln;
1451 if (regrepeat(scan, 1)) {
1453 reginput = locinput + ln;
1460 n = regrepeat(scan, n);
1461 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1462 (!multiline || OP(next) == SEOL))
1463 ln = n; /* why back off? */
1467 /* If it could work, try it. */
1469 UCHARAT(reginput) == c1 ||
1470 UCHARAT(reginput) == c2)
1474 regstartp[paren] = reginput - 1;
1475 regendp[paren] = reginput;
1477 regendp[paren] = NULL;
1483 /* Couldn't or didn't -- back up. */
1485 reginput = locinput + n;
1489 /* If it could work, try it. */
1491 UCHARAT(reginput) == c1 ||
1492 UCHARAT(reginput) == c2)
1498 /* Couldn't or didn't -- back up. */
1500 reginput = locinput + n;
1508 reginput = locinput; /* put where regtry can find it */
1509 sayYES; /* Success! */
1515 if (locinput < bostr + scan->flags)
1520 if (locinput < bostr + scan->flags)
1523 reginput = locinput - scan->flags;
1524 inner = NEXTOPER(NEXTOPER(scan));
1525 if (regmatch(inner) != n) {
1539 if (OP(scan) == SUSPEND) {
1540 locinput = reginput;
1541 nextchr = UCHARAT(locinput);
1546 next = scan + ARG(scan);
1551 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1552 (unsigned long)scan, OP(scan));
1553 FAIL("regexp memory corruption");
1559 * We get here only if there's trouble -- normally "case END" is
1560 * the terminating point.
1562 FAIL("corrupted regexp pointers");
1580 - regrepeat - repeatedly match something simple, report how many
1583 * [This routine now assumes that it will only match on things of length 1.
1584 * That was true before, but now we assume scan - reginput is the count,
1585 * rather than incrementing count on every character.]
1588 regrepeat(regnode *p, I32 max)
1590 register char *scan;
1591 register char *opnd;
1593 register char *loceol = regeol;
1596 if (max != REG_INFTY && max < loceol - scan)
1597 loceol = scan + max;
1598 opnd = (char *) OPERAND(p);
1601 while (scan < loceol && *scan != '\n')
1607 case EXACT: /* length of string is 1 */
1608 c = UCHARAT(++opnd);
1609 while (scan < loceol && UCHARAT(scan) == c)
1612 case EXACTF: /* length of string is 1 */
1613 c = UCHARAT(++opnd);
1614 while (scan < loceol &&
1615 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1618 case EXACTFL: /* length of string is 1 */
1619 reg_flags |= RF_tainted;
1620 c = UCHARAT(++opnd);
1621 while (scan < loceol &&
1622 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1626 while (scan < loceol && reginclass(opnd, *scan))
1630 while (scan < loceol && isALNUM(*scan))
1634 reg_flags |= RF_tainted;
1635 while (scan < loceol && isALNUM_LC(*scan))
1639 while (scan < loceol && !isALNUM(*scan))
1643 reg_flags |= RF_tainted;
1644 while (scan < loceol && !isALNUM_LC(*scan))
1648 while (scan < loceol && isSPACE(*scan))
1652 reg_flags |= RF_tainted;
1653 while (scan < loceol && isSPACE_LC(*scan))
1657 while (scan < loceol && !isSPACE(*scan))
1661 reg_flags |= RF_tainted;
1662 while (scan < loceol && !isSPACE_LC(*scan))
1666 while (scan < loceol && isDIGIT(*scan))
1670 while (scan < loceol && !isDIGIT(*scan))
1673 default: /* Called on something of 0 width. */
1674 break; /* So match right here or not at all. */
1677 c = scan - reginput;
1682 SV *prop = sv_newmortal();
1685 PerlIO_printf(Perl_debug_log,
1686 "%*s %s can match %ld times out of %ld...\n",
1687 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1694 - regrepeat_hard - repeatedly match something, report total lenth and length
1696 * The repeater is supposed to have constant length.
1700 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1702 register char *scan;
1703 register char *start;
1704 register char *loceol = regeol;
1708 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1710 *lp = l = reginput - start;
1711 if (max != REG_INFTY && l*max < loceol - scan)
1712 loceol = scan + l*max;
1718 if (reginput < loceol)
1723 return (scan - start)/l;
1727 - regclass - determine if a character falls into a character class
1731 reginclass(register char *p, register I32 c)
1737 if (p[1 + (c >> 3)] & (1 << (c & 7)))
1739 else if (flags & ANYOF_FOLD) {
1741 if (flags & ANYOF_LOCALE) {
1742 reg_flags |= RF_tainted;
1743 cf = fold_locale[c];
1747 if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1751 if (!match && (flags & ANYOF_ISA)) {
1752 reg_flags |= RF_tainted;
1754 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1755 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1756 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1757 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1763 return match ^ ((flags & ANYOF_INVERT) != 0);