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 with setting? */
62 #define RS_init 1 /* eval environment created */
63 #define RS_set 2 /* replsv value is set */
70 typedef I32 CHECKPOINT;
76 static I32 regmatch _((regnode *prog));
77 static I32 regrepeat _((regnode *p, I32 max));
78 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
79 static I32 regtry _((regexp *prog, char *startpos));
81 static bool reginclass _((char *p, I32 c));
82 static CHECKPOINT regcppush _((I32 parenfloor));
83 static char * regcppop _((void));
85 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
88 regcppush(I32 parenfloor)
91 int retval = savestack_ix;
92 int i = (regsize - parenfloor) * 4;
96 for (p = regsize; p > parenfloor; p--) {
97 SSPUSHPTR(regendp[p]);
98 SSPUSHPTR(regstartp[p]);
99 SSPUSHPTR(reg_start_tmp[p]);
103 SSPUSHINT(*reglastparen);
106 SSPUSHINT(SAVEt_REGCONTEXT);
110 /* These are needed since we do not localize EVAL nodes: */
111 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
112 # 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)
122 assert(i == SAVEt_REGCONTEXT);
124 input = (char *) SSPOPPTR;
125 *reglastparen = SSPOPINT;
127 for (i -= 3; i > 0; i -= 4) {
128 paren = (U32)SSPOPINT;
129 reg_start_tmp[paren] = (char *) SSPOPPTR;
130 regstartp[paren] = (char *) SSPOPPTR;
131 tmps = (char*)SSPOPPTR;
132 if (paren <= *reglastparen)
133 regendp[paren] = tmps;
135 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
136 paren, regstartp[paren] - regbol,
137 reg_start_tmp[paren] - regbol,
138 regendp[paren] - regbol,
139 (paren > *reglastparen ? "(no)" : ""));
143 if (*reglastparen + 1 <= regnpar) {
144 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
145 *reglastparen + 1, regnpar);
148 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
150 regstartp[paren] = Nullch;
151 regendp[paren] = Nullch;
156 #define regcpblow(cp) LEAVE_SCOPE(cp)
159 * pregexec and friends
163 - pregexec - match a regexp against a string
166 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
167 /* strend: pointer to null at end of string */
168 /* strbeg: real beginning of string */
169 /* minend: end of match must be >=minend after stringarg. */
170 /* nosave: For optimizations. */
173 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
174 nosave ? 0 : REXEC_COPY_STR);
178 - regexec_flags - match a regexp against a string
181 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
182 /* strend: pointer to null at end of string */
183 /* strbeg: real beginning of string */
184 /* minend: end of match must be >=minend after stringarg. */
185 /* data: May be used for some additional optimizations. */
186 /* nosave: For optimizations. */
190 register char *startpos = stringarg;
192 I32 minlen; /* must match at least this many chars */
193 I32 dontbother = 0; /* how many characters not to try at end */
195 I32 start_shift = 0; /* Offset of the start to find
197 I32 end_shift = 0; /* Same for the end. */
198 I32 scream_pos = -1; /* Internal iterator of scream. */
200 SV* oreplsv = GvSV(replgv);
206 regprecomp = prog->precomp; /* Needed for error messages. */
208 regnarrate = debug & 512;
209 regprogram = prog->program;
213 if (prog == NULL || startpos == NULL) {
214 croak("NULL regexp parameter");
218 minlen = prog->minlen;
219 if (strend - startpos < minlen) goto phooey;
221 if (startpos == strbeg) /* is ^ valid at stringarg? */
224 regprev = stringarg[-1];
225 if (!multiline && regprev == '\n')
226 regprev = '\0'; /* force ^ to NOT match */
229 /* Check validity of program. */
230 if (UCHARAT(prog->program) != MAGIC) {
231 FAIL("corrupted regexp program");
234 regnpar = prog->nparens;
238 /* If there is a "must appear" string, look for it. */
240 if (!(flags & REXEC_CHECKED)
241 && prog->check_substr != Nullsv &&
242 !(prog->reganch & ROPT_ANCH_GPOS) &&
243 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
244 || (multiline && prog->check_substr == prog->anchored_substr)) )
246 start_shift = prog->check_offset_min;
247 /* Should be nonnegative! */
248 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
250 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
251 s = screaminstr(screamer, prog->check_substr,
252 start_shift + (stringarg - strbeg),
253 end_shift, &scream_pos, 0);
259 s = fbm_instr((unsigned char*)s + start_shift,
260 (unsigned char*)strend - end_shift,
263 ++BmUSEFUL(prog->check_substr); /* hooray */
264 goto phooey; /* not present */
265 } else if ((s - stringarg) > prog->check_offset_max) {
266 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
267 s -= prog->check_offset_max;
268 } else if (!prog->naughty
269 && --BmUSEFUL(prog->check_substr) < 0
270 && prog->check_substr == prog->float_substr) { /* boo */
271 SvREFCNT_dec(prog->check_substr);
272 prog->check_substr = Nullsv; /* disable */
273 prog->float_substr = Nullsv; /* clear */
278 /* Mark beginning of line for ^ and lookbehind. */
282 /* Mark end of line for $ (and such) */
285 /* see how far we have to get to not match where we matched before */
286 regtill = startpos+minend;
289 PerlIO_printf(Perl_debug_log,
290 "Matching `%.60s%s' against `%.*s%s'\n",
292 (strlen(prog->precomp) > 60 ? "..." : ""),
293 (strend - startpos > 60 ? 60 : strend - startpos),
295 (strend - startpos > 60 ? "..." : ""))
298 /* Simplest case: anchored match need be tried only once. */
299 /* [unless only anchor is BOL and multiline is set] */
300 if (prog->reganch & ROPT_ANCH) {
301 if (regtry(prog, startpos))
303 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
304 (multiline || (prog->reganch & ROPT_IMPLICIT)
305 || (prog->reganch & ROPT_ANCH_MBOL)))
308 dontbother = minlen - 1;
309 strend -= dontbother;
310 /* for multiline we only have to try after newlines */
315 if (s < strend && regtry(prog, s))
323 /* Messy cases: unanchored match. */
324 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
325 /* we have /x+whatever/ */
326 /* it must be a one character string */
327 char ch = SvPVX(prog->anchored_substr)[0];
330 if (regtry(prog, s)) goto got_it;
332 while (s < strend && *s == ch)
339 else if (prog->anchored_substr != Nullsv
340 || (prog->float_substr != Nullsv
341 && prog->float_max_offset < strend - s)) {
342 SV *must = prog->anchored_substr
343 ? prog->anchored_substr : prog->float_substr;
345 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
347 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
348 I32 delta = back_max - back_min;
349 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
350 char *last1 = s - 1; /* Last position checked before */
352 /* XXXX check_substr already used to find `s', can optimize if
353 check_substr==must. */
355 dontbother = end_shift;
356 strend -= dontbother;
357 while ( (s <= last) &&
359 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
360 end_shift, &scream_pos, 0))
361 : (s = fbm_instr((unsigned char*)s + back_min,
362 (unsigned char*)strend, must))) ) {
363 if (s - back_max > last1) {
364 last1 = s - back_min;
369 last1 = s - back_min;
379 } else if (c = prog->regstclass) {
380 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
384 dontbother = minlen - 1;
385 strend -= dontbother; /* don't bother with what can't match */
387 /* We know what class it must start with. */
390 Class = (char *) OPERAND(c);
392 if (REGINCLASS(Class, *s)) {
393 if (tmp && regtry(prog, s))
404 reg_flags |= RF_tainted;
408 dontbother++,strend--;
409 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
410 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
412 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
419 if ((minlen || tmp) && regtry(prog,s))
423 reg_flags |= RF_tainted;
427 dontbother++,strend--;
428 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
429 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
431 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
433 else if (regtry(prog, s))
437 if ((minlen || !tmp) && regtry(prog,s))
443 if (tmp && regtry(prog, s))
454 reg_flags |= RF_tainted;
456 if (isALNUM_LC(*s)) {
457 if (tmp && regtry(prog, s))
470 if (tmp && regtry(prog, s))
481 reg_flags |= RF_tainted;
483 if (!isALNUM_LC(*s)) {
484 if (tmp && regtry(prog, s))
497 if (tmp && regtry(prog, s))
508 reg_flags |= RF_tainted;
510 if (isSPACE_LC(*s)) {
511 if (tmp && regtry(prog, s))
524 if (tmp && regtry(prog, s))
535 reg_flags |= RF_tainted;
537 if (!isSPACE_LC(*s)) {
538 if (tmp && regtry(prog, s))
551 if (tmp && regtry(prog, s))
564 if (tmp && regtry(prog, s))
578 if (prog->float_substr != Nullsv) { /* Trim the end. */
580 I32 oldpos = scream_pos;
583 last = screaminstr(screamer, prog->float_substr, s - strbeg,
584 end_shift, &scream_pos, 1); /* last one */
586 last = scream_olds; /* Only one occurence. */
590 char *little = SvPV(prog->float_substr, len);
591 last = rninstr(s, strend, little, little + len);
593 if (last == NULL) goto phooey; /* Should not happen! */
594 dontbother = strend - last - 1;
596 if (minlen && (dontbother < minlen))
597 dontbother = minlen - 1;
598 strend -= dontbother;
599 /* We don't know much -- general case. */
603 } while (s++ < strend);
610 strend += dontbother; /* uncheat */
611 prog->subbeg = strbeg;
612 prog->subend = strend;
613 RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
615 /* make sure $`, $&, $', and $digit will work later */
616 if (strbeg != prog->subbase) { /* second+ //g match. */
617 if (!(flags & REXEC_COPY_STR)) {
619 Safefree(prog->subbase);
620 prog->subbase = Nullch;
624 I32 i = strend - startpos + (stringarg - strbeg);
625 s = savepvn(strbeg, i);
626 Safefree(prog->subbase);
628 prog->subbeg = prog->subbase;
629 prog->subend = prog->subbase + i;
630 s = prog->subbase + (stringarg - strbeg);
631 for (i = 0; i <= prog->nparens; i++) {
633 prog->startp[i] = s + (prog->startp[i] - startpos);
634 prog->endp[i] = s + (prog->endp[i] - startpos);
639 /* Preserve the current value of $^R */
640 if (oreplsv != GvSV(replgv)) {
641 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
642 restored, the value remains
652 - regtry - try match at specific point
654 STATIC I32 /* 0 failure, 1 success */
655 regtry(regexp *prog, char *startpos)
663 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
664 reg_eval_set = RS_init;
666 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
668 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
669 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
670 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
672 /* Apparently this is not needed, judging by wantarray. */
673 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
674 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
677 regstartp = prog->startp;
678 regendp = prog->endp;
679 reglastparen = &prog->lastparen;
682 if (reg_start_tmpl <= prog->nparens) {
683 reg_start_tmpl = prog->nparens*3/2 + 3;
685 Renew(reg_start_tmp, reg_start_tmpl, char*);
687 New(22,reg_start_tmp, reg_start_tmpl, char*);
692 regdata = prog->data;
694 for (i = prog->nparens; i >= 0; i--) {
700 if (regmatch(prog->program + 1) && reginput >= regtill) {
701 prog->startp[0] = startpos;
702 prog->endp[0] = reginput;
710 - regmatch - main matching routine
712 * Conceptually the strategy is simple: check to see whether the current
713 * node matches, call self recursively to see whether the rest matches,
714 * and then act accordingly. In practice we make some effort to avoid
715 * recursion, in particular by going through "ordinary" nodes (that don't
716 * need to know whether the rest of the match failed) by a loop instead of
719 /* [lwall] I've hoisted the register declarations to the outer block in order to
720 * maybe save a little bit of pushing and popping on the stack. It also takes
721 * advantage of machines that use a register save mask on subroutine entry.
723 STATIC I32 /* 0 failure, 1 success */
724 regmatch(regnode *prog)
727 register regnode *scan; /* Current node. */
728 regnode *next; /* Next node. */
729 regnode *inner; /* Next node in internal branch. */
730 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
731 register I32 n; /* no or next */
732 register I32 ln; /* len or last */
733 register char *s; /* operand or save */
734 register char *locinput = reginput;
735 register I32 c1, c2, paren; /* case fold search, parenth */
736 int minmod = 0, sw = 0, logical = 0;
741 nextchr = UCHARAT(locinput);
743 while (scan != NULL) {
744 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
746 # define sayYES goto yes
747 # define sayNO goto no
748 # define saySAME(x) if (x) goto yes; else goto no
749 # define REPORT_CODE_OFF 24
751 # define sayYES return 1
752 # define sayNO return 0
753 # define saySAME(x) return x
756 SV *prop = sv_newmortal();
757 int docolor = *colors[0];
758 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
759 int l = (regeol - locinput > taill ? taill : regeol - locinput);
760 int pref_len = (locinput - bostr > (5 + taill) - l
761 ? (5 + taill) - l : locinput - bostr);
763 if (l + pref_len < (5 + taill) && l < regeol - locinput)
764 l = ( regeol - locinput > (5 + taill) - pref_len
765 ? (5 + taill) - pref_len : regeol - locinput);
767 PerlIO_printf(Perl_debug_log,
768 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
770 colors[2], pref_len, locinput - pref_len, colors[3],
771 (docolor ? "" : "> <"),
772 colors[0], l, locinput, colors[1],
773 15 - l - pref_len + 1,
775 regindent*2, "", scan - regprogram,
779 next = scan + NEXT_OFF(scan);
785 if (locinput == regbol
788 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
790 /* regtill = regbol; */
795 if (locinput == regbol
797 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
803 if (locinput == regbol && regprev == '\n')
807 if (locinput == regbol)
817 if ((nextchr || locinput < regeol) && nextchr != '\n')
822 if ((nextchr || locinput < regeol) && nextchr != '\n')
824 if (regeol - locinput > 1)
828 if (!nextchr && locinput >= regeol)
830 nextchr = UCHARAT(++locinput);
833 if (!nextchr && locinput >= regeol || nextchr == '\n')
835 nextchr = UCHARAT(++locinput);
838 s = (char *) OPERAND(scan);
840 /* Inline the first character, for speed. */
841 if (UCHARAT(s) != nextchr)
843 if (regeol - locinput < ln)
845 if (ln > 1 && memNE(s, locinput, ln))
848 nextchr = UCHARAT(locinput);
851 reg_flags |= RF_tainted;
854 s = (char *) OPERAND(scan);
856 /* Inline the first character, for speed. */
857 if (UCHARAT(s) != nextchr &&
858 UCHARAT(s) != ((OP(scan) == EXACTF)
859 ? fold : fold_locale)[nextchr])
861 if (regeol - locinput < ln)
863 if (ln > 1 && (OP(scan) == EXACTF
864 ? ibcmp(s, locinput, ln)
865 : ibcmp_locale(s, locinput, ln)))
868 nextchr = UCHARAT(locinput);
871 s = (char *) OPERAND(scan);
873 nextchr = UCHARAT(locinput);
874 if (!REGINCLASS(s, nextchr))
876 if (!nextchr && locinput >= regeol)
878 nextchr = UCHARAT(++locinput);
881 reg_flags |= RF_tainted;
886 if (!(OP(scan) == ALNUM
887 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
889 nextchr = UCHARAT(++locinput);
892 reg_flags |= RF_tainted;
895 if (!nextchr && locinput >= regeol)
897 if (OP(scan) == NALNUM
898 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
900 nextchr = UCHARAT(++locinput);
904 reg_flags |= RF_tainted;
908 /* was last char in word? */
909 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
910 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
912 n = isALNUM(nextchr);
916 n = isALNUM_LC(nextchr);
918 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
922 reg_flags |= RF_tainted;
925 if (!nextchr && locinput >= regeol)
927 if (!(OP(scan) == SPACE
928 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
930 nextchr = UCHARAT(++locinput);
933 reg_flags |= RF_tainted;
938 if (OP(scan) == SPACE
939 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
941 nextchr = UCHARAT(++locinput);
944 if (!isDIGIT(nextchr))
946 nextchr = UCHARAT(++locinput);
949 if (!nextchr && locinput >= regeol)
951 if (isDIGIT(nextchr))
953 nextchr = UCHARAT(++locinput);
956 reg_flags |= RF_tainted;
960 n = ARG(scan); /* which paren pair */
962 if (*reglastparen < n || !s)
963 sayNO; /* Do not match unless seen CLOSEn. */
966 /* Inline the first character, for speed. */
967 if (UCHARAT(s) != nextchr &&
969 (UCHARAT(s) != ((OP(scan) == REFF
970 ? fold : fold_locale)[nextchr]))))
973 if (locinput + ln > regeol)
975 if (ln > 1 && (OP(scan) == REF
976 ? memNE(s, locinput, ln)
978 ? ibcmp(s, locinput, ln)
979 : ibcmp_locale(s, locinput, ln))))
982 nextchr = UCHARAT(locinput);
994 COP *ocurcop = curcop;
995 SV **ocurpad = curpad;
999 op = (OP_4tree*)regdata->data[n];
1000 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1001 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1003 CALLRUNOPS(); /* Scalar context. */
1012 sv_setsv(save_scalar(replgv), ret);
1019 n = ARG(scan); /* which paren pair */
1020 reg_start_tmp[n] = locinput;
1025 n = ARG(scan); /* which paren pair */
1026 regstartp[n] = reg_start_tmp[n];
1027 regendp[n] = locinput;
1028 if (n > *reglastparen)
1032 n = ARG(scan); /* which paren pair */
1033 sw = (*reglastparen >= n && regendp[n] != NULL);
1037 next = NEXTOPER(NEXTOPER(scan));
1039 next = scan + ARG(scan);
1040 if (OP(next) == IFTHEN) /* Fake one. */
1041 next = NEXTOPER(NEXTOPER(next));
1049 CHECKPOINT cp = savestack_ix;
1051 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1055 cc.parenfloor = *reglastparen;
1057 cc.min = ARG1(scan);
1058 cc.max = ARG2(scan);
1059 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1063 reginput = locinput;
1064 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1072 * This is really hard to understand, because after we match
1073 * what we're trying to match, we must make sure the rest of
1074 * the RE is going to match for sure, and to do that we have
1075 * to go back UP the parse tree by recursing ever deeper. And
1076 * if it fails, we have to reset our parent's current state
1077 * that we can try again after backing off.
1080 CHECKPOINT cp, lastcp;
1082 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1084 n = cc->cur + 1; /* how many we know we matched */
1085 reginput = locinput;
1088 PerlIO_printf(Perl_debug_log,
1089 "%*s %ld out of %ld..%ld cc=%lx\n",
1090 REPORT_CODE_OFF+regindent*2, "",
1091 (long)n, (long)cc->min,
1092 (long)cc->max, (long)cc)
1095 /* If degenerate scan matches "", assume scan done. */
1097 if (locinput == cc->lastloc && n >= cc->min) {
1101 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1103 if (regmatch(cc->next))
1106 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1113 /* First just match a string of min scans. */
1117 cc->lastloc = locinput;
1118 if (regmatch(cc->scan))
1121 cc->lastloc = lastloc;
1123 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1128 /* Prefer next over scan for minimal matching. */
1133 cp = regcppush(cc->parenfloor);
1135 if (regmatch(cc->next)) {
1137 sayYES; /* All done. */
1144 if (n >= cc->max) { /* Maximum greed exceeded? */
1145 if (dowarn && n >= REG_INFTY
1146 && !(reg_flags & RF_warned)) {
1147 reg_flags |= RF_warned;
1148 warn("count exceeded %d", REG_INFTY - 1);
1154 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1156 /* Try scanning more and see if it helps. */
1157 reginput = locinput;
1159 cc->lastloc = locinput;
1160 cp = regcppush(cc->parenfloor);
1162 if (regmatch(cc->scan)) {
1167 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1172 cc->lastloc = lastloc;
1176 /* Prefer scan over next for maximal matching. */
1178 if (n < cc->max) { /* More greed allowed? */
1179 cp = regcppush(cc->parenfloor);
1181 cc->lastloc = locinput;
1183 if (regmatch(cc->scan)) {
1188 regcppop(); /* Restore some previous $<digit>s? */
1189 reginput = locinput;
1191 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1194 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1195 reg_flags |= RF_warned;
1196 warn("count exceeded %d", REG_INFTY - 1);
1199 /* Failed deeper matches of scan, so see if this one works. */
1202 if (regmatch(cc->next))
1205 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1210 cc->lastloc = lastloc;
1215 next = scan + ARG(scan);
1218 inner = NEXTOPER(NEXTOPER(scan));
1221 inner = NEXTOPER(scan);
1226 if (OP(next) != c1) /* No choice. */
1227 next = inner; /* Avoid recursion. */
1229 int lastparen = *reglastparen;
1233 reginput = locinput;
1234 if (regmatch(inner))
1237 for (n = *reglastparen; n > lastparen; n--)
1242 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1246 inner = NEXTOPER(scan);
1247 if (c1 == BRANCHJ) {
1248 inner = NEXTOPER(inner);
1250 } while (scan != NULL && OP(scan) == c1);
1264 /* We suppose that the next guy does not need
1265 backtracking: in particular, it is of constant length,
1266 and has no parenths to influence future backrefs. */
1267 ln = ARG1(scan); /* min to match */
1268 n = ARG2(scan); /* max to match */
1269 paren = scan->flags;
1271 if (paren > regsize)
1273 if (paren > *reglastparen)
1274 *reglastparen = paren;
1276 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1278 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1279 reginput = locinput;
1282 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1284 if (ln && 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 /* This may be improved if l == 0. */
1303 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1304 /* If it could work, try it. */
1306 UCHARAT(reginput) == c1 ||
1307 UCHARAT(reginput) == c2)
1311 regstartp[paren] = reginput - l;
1312 regendp[paren] = reginput;
1314 regendp[paren] = NULL;
1320 /* Couldn't or didn't -- move forward. */
1321 reginput = locinput;
1322 if (regrepeat_hard(scan, 1, &l)) {
1324 locinput = reginput;
1330 n = regrepeat_hard(scan, n, &l);
1331 if (n != 0 && l == 0
1332 /* In fact, this is tricky. If paren, then the
1333 fact that we did/didnot match may influence
1334 future execution. */
1335 && !(paren && ln == 0))
1337 locinput = reginput;
1339 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1342 if (regkind[(U8)OP(next)] == EXACT) {
1343 c1 = UCHARAT(OPERAND(next) + 1);
1344 if (OP(next) == EXACTF)
1346 else if (OP(next) == EXACTFL)
1347 c2 = fold_locale[c1];
1355 /* If it could work, try it. */
1357 UCHARAT(reginput) == c1 ||
1358 UCHARAT(reginput) == c2)
1361 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1365 regstartp[paren] = reginput - l;
1366 regendp[paren] = reginput;
1368 regendp[paren] = NULL;
1374 /* Couldn't or didn't -- back up. */
1377 reginput = locinput;
1384 paren = scan->flags; /* Which paren to set */
1385 if (paren > regsize)
1387 if (paren > *reglastparen)
1388 *reglastparen = paren;
1389 ln = ARG1(scan); /* min to match */
1390 n = ARG2(scan); /* max to match */
1391 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1395 ln = ARG1(scan); /* min to match */
1396 n = ARG2(scan); /* max to match */
1397 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1402 scan = NEXTOPER(scan);
1408 scan = NEXTOPER(scan);
1412 * Lookahead to avoid useless match attempts
1413 * when we know what character comes next.
1415 if (regkind[(U8)OP(next)] == EXACT) {
1416 c1 = UCHARAT(OPERAND(next) + 1);
1417 if (OP(next) == EXACTF)
1419 else if (OP(next) == EXACTFL)
1420 c2 = fold_locale[c1];
1426 reginput = locinput;
1430 if (ln && regrepeat(scan, ln) < ln)
1433 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1434 /* If it could work, try it. */
1436 UCHARAT(reginput) == c1 ||
1437 UCHARAT(reginput) == c2)
1441 regstartp[paren] = reginput - 1;
1442 regendp[paren] = reginput;
1444 regendp[paren] = NULL;
1450 /* Couldn't or didn't -- move forward. */
1451 reginput = locinput + ln;
1452 if (regrepeat(scan, 1)) {
1454 reginput = locinput + ln;
1461 n = regrepeat(scan, n);
1462 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1463 (!multiline || OP(next) == SEOL))
1464 ln = n; /* why back off? */
1468 /* If it could work, try it. */
1470 UCHARAT(reginput) == c1 ||
1471 UCHARAT(reginput) == c2)
1475 regstartp[paren] = reginput - 1;
1476 regendp[paren] = reginput;
1478 regendp[paren] = NULL;
1484 /* Couldn't or didn't -- back up. */
1486 reginput = locinput + n;
1490 /* If it could work, try it. */
1492 UCHARAT(reginput) == c1 ||
1493 UCHARAT(reginput) == c2)
1499 /* Couldn't or didn't -- back up. */
1501 reginput = locinput + n;
1509 reginput = locinput; /* put where regtry can find it */
1510 sayYES; /* Success! */
1516 if (locinput < bostr + scan->flags)
1521 if (locinput < bostr + scan->flags)
1524 reginput = locinput - scan->flags;
1525 inner = NEXTOPER(NEXTOPER(scan));
1526 if (regmatch(inner) != n) {
1540 if (OP(scan) == SUSPEND) {
1541 locinput = reginput;
1542 nextchr = UCHARAT(locinput);
1547 next = scan + ARG(scan);
1552 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1553 (unsigned long)scan, OP(scan));
1554 FAIL("regexp memory corruption");
1560 * We get here only if there's trouble -- normally "case END" is
1561 * the terminating point.
1563 FAIL("corrupted regexp pointers");
1581 - regrepeat - repeatedly match something simple, report how many
1584 * [This routine now assumes that it will only match on things of length 1.
1585 * That was true before, but now we assume scan - reginput is the count,
1586 * rather than incrementing count on every character.]
1589 regrepeat(regnode *p, I32 max)
1591 register char *scan;
1592 register char *opnd;
1594 register char *loceol = regeol;
1597 if (max != REG_INFTY && max < loceol - scan)
1598 loceol = scan + max;
1599 opnd = (char *) OPERAND(p);
1602 while (scan < loceol && *scan != '\n')
1608 case EXACT: /* length of string is 1 */
1609 c = UCHARAT(++opnd);
1610 while (scan < loceol && UCHARAT(scan) == c)
1613 case EXACTF: /* length of string is 1 */
1614 c = UCHARAT(++opnd);
1615 while (scan < loceol &&
1616 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1619 case EXACTFL: /* length of string is 1 */
1620 reg_flags |= RF_tainted;
1621 c = UCHARAT(++opnd);
1622 while (scan < loceol &&
1623 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1627 while (scan < loceol && REGINCLASS(opnd, *scan))
1631 while (scan < loceol && isALNUM(*scan))
1635 reg_flags |= RF_tainted;
1636 while (scan < loceol && isALNUM_LC(*scan))
1640 while (scan < loceol && !isALNUM(*scan))
1644 reg_flags |= RF_tainted;
1645 while (scan < loceol && !isALNUM_LC(*scan))
1649 while (scan < loceol && isSPACE(*scan))
1653 reg_flags |= RF_tainted;
1654 while (scan < loceol && isSPACE_LC(*scan))
1658 while (scan < loceol && !isSPACE(*scan))
1662 reg_flags |= RF_tainted;
1663 while (scan < loceol && !isSPACE_LC(*scan))
1667 while (scan < loceol && isDIGIT(*scan))
1671 while (scan < loceol && !isDIGIT(*scan))
1674 default: /* Called on something of 0 width. */
1675 break; /* So match right here or not at all. */
1678 c = scan - reginput;
1683 SV *prop = sv_newmortal();
1686 PerlIO_printf(Perl_debug_log,
1687 "%*s %s can match %ld times out of %ld...\n",
1688 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1695 - regrepeat_hard - repeatedly match something, report total lenth and length
1697 * The repeater is supposed to have constant length.
1701 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1703 register char *scan;
1704 register char *start;
1705 register char *loceol = regeol;
1709 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1711 *lp = l = reginput - start;
1712 if (max != REG_INFTY && l*max < loceol - scan)
1713 loceol = scan + l*max;
1719 if (reginput < loceol)
1724 return (scan - start)/l;
1728 - regclass - determine if a character falls into a character class
1732 reginclass(register char *p, register I32 c)
1738 if (ANYOF_TEST(p, c))
1740 else if (flags & ANYOF_FOLD) {
1742 if (flags & ANYOF_LOCALE) {
1743 reg_flags |= RF_tainted;
1744 cf = fold_locale[c];
1748 if (ANYOF_TEST(p, cf))
1752 if (!match && (flags & ANYOF_ISA)) {
1753 reg_flags |= RF_tainted;
1755 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1756 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1757 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1758 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1764 return (flags & ANYOF_INVERT) ? !match : match;