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,
261 prog->check_substr, 0);
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, 0))) ) {
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)) {
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 (regeol != locinput)
832 if (!nextchr && locinput >= regeol)
834 nextchr = UCHARAT(++locinput);
837 if (!nextchr && locinput >= regeol || nextchr == '\n')
839 nextchr = UCHARAT(++locinput);
842 s = (char *) OPERAND(scan);
844 /* Inline the first character, for speed. */
845 if (UCHARAT(s) != nextchr)
847 if (regeol - locinput < ln)
849 if (ln > 1 && memNE(s, locinput, ln))
852 nextchr = UCHARAT(locinput);
855 reg_flags |= RF_tainted;
858 s = (char *) OPERAND(scan);
860 /* Inline the first character, for speed. */
861 if (UCHARAT(s) != nextchr &&
862 UCHARAT(s) != ((OP(scan) == EXACTF)
863 ? fold : fold_locale)[nextchr])
865 if (regeol - locinput < ln)
867 if (ln > 1 && (OP(scan) == EXACTF
868 ? ibcmp(s, locinput, ln)
869 : ibcmp_locale(s, locinput, ln)))
872 nextchr = UCHARAT(locinput);
875 s = (char *) OPERAND(scan);
877 nextchr = UCHARAT(locinput);
878 if (!REGINCLASS(s, nextchr))
880 if (!nextchr && locinput >= regeol)
882 nextchr = UCHARAT(++locinput);
885 reg_flags |= RF_tainted;
890 if (!(OP(scan) == ALNUM
891 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
893 nextchr = UCHARAT(++locinput);
896 reg_flags |= RF_tainted;
899 if (!nextchr && locinput >= regeol)
901 if (OP(scan) == NALNUM
902 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
904 nextchr = UCHARAT(++locinput);
908 reg_flags |= RF_tainted;
912 /* was last char in word? */
913 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
914 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
916 n = isALNUM(nextchr);
920 n = isALNUM_LC(nextchr);
922 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
926 reg_flags |= RF_tainted;
929 if (!nextchr && locinput >= regeol)
931 if (!(OP(scan) == SPACE
932 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
934 nextchr = UCHARAT(++locinput);
937 reg_flags |= RF_tainted;
942 if (OP(scan) == SPACE
943 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
945 nextchr = UCHARAT(++locinput);
948 if (!isDIGIT(nextchr))
950 nextchr = UCHARAT(++locinput);
953 if (!nextchr && locinput >= regeol)
955 if (isDIGIT(nextchr))
957 nextchr = UCHARAT(++locinput);
960 reg_flags |= RF_tainted;
964 n = ARG(scan); /* which paren pair */
966 if (*reglastparen < n || !s)
967 sayNO; /* Do not match unless seen CLOSEn. */
970 /* Inline the first character, for speed. */
971 if (UCHARAT(s) != nextchr &&
973 (UCHARAT(s) != ((OP(scan) == REFF
974 ? fold : fold_locale)[nextchr]))))
977 if (locinput + ln > regeol)
979 if (ln > 1 && (OP(scan) == REF
980 ? memNE(s, locinput, ln)
982 ? ibcmp(s, locinput, ln)
983 : ibcmp_locale(s, locinput, ln))))
986 nextchr = UCHARAT(locinput);
998 COP *ocurcop = curcop;
999 SV **ocurpad = curpad;
1003 op = (OP_4tree*)regdata->data[n];
1004 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1005 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1007 CALLRUNOPS(); /* Scalar context. */
1016 sv_setsv(save_scalar(replgv), ret);
1023 n = ARG(scan); /* which paren pair */
1024 reg_start_tmp[n] = locinput;
1029 n = ARG(scan); /* which paren pair */
1030 regstartp[n] = reg_start_tmp[n];
1031 regendp[n] = locinput;
1032 if (n > *reglastparen)
1036 n = ARG(scan); /* which paren pair */
1037 sw = (*reglastparen >= n && regendp[n] != NULL);
1041 next = NEXTOPER(NEXTOPER(scan));
1043 next = scan + ARG(scan);
1044 if (OP(next) == IFTHEN) /* Fake one. */
1045 next = NEXTOPER(NEXTOPER(next));
1053 CHECKPOINT cp = savestack_ix;
1055 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1059 cc.parenfloor = *reglastparen;
1061 cc.min = ARG1(scan);
1062 cc.max = ARG2(scan);
1063 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1067 reginput = locinput;
1068 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1076 * This is really hard to understand, because after we match
1077 * what we're trying to match, we must make sure the rest of
1078 * the RE is going to match for sure, and to do that we have
1079 * to go back UP the parse tree by recursing ever deeper. And
1080 * if it fails, we have to reset our parent's current state
1081 * that we can try again after backing off.
1084 CHECKPOINT cp, lastcp;
1086 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1088 n = cc->cur + 1; /* how many we know we matched */
1089 reginput = locinput;
1092 PerlIO_printf(Perl_debug_log,
1093 "%*s %ld out of %ld..%ld cc=%lx\n",
1094 REPORT_CODE_OFF+regindent*2, "",
1095 (long)n, (long)cc->min,
1096 (long)cc->max, (long)cc)
1099 /* If degenerate scan matches "", assume scan done. */
1101 if (locinput == cc->lastloc && n >= cc->min) {
1105 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1107 if (regmatch(cc->next))
1110 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1117 /* First just match a string of min scans. */
1121 cc->lastloc = locinput;
1122 if (regmatch(cc->scan))
1125 cc->lastloc = lastloc;
1127 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1132 /* Prefer next over scan for minimal matching. */
1137 cp = regcppush(cc->parenfloor);
1139 if (regmatch(cc->next)) {
1141 sayYES; /* All done. */
1148 if (n >= cc->max) { /* Maximum greed exceeded? */
1149 if (dowarn && n >= REG_INFTY
1150 && !(reg_flags & RF_warned)) {
1151 reg_flags |= RF_warned;
1152 warn("count exceeded %d", REG_INFTY - 1);
1158 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1160 /* Try scanning more and see if it helps. */
1161 reginput = locinput;
1163 cc->lastloc = locinput;
1164 cp = regcppush(cc->parenfloor);
1166 if (regmatch(cc->scan)) {
1171 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1176 cc->lastloc = lastloc;
1180 /* Prefer scan over next for maximal matching. */
1182 if (n < cc->max) { /* More greed allowed? */
1183 cp = regcppush(cc->parenfloor);
1185 cc->lastloc = locinput;
1187 if (regmatch(cc->scan)) {
1192 regcppop(); /* Restore some previous $<digit>s? */
1193 reginput = locinput;
1195 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1198 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1199 reg_flags |= RF_warned;
1200 warn("count exceeded %d", REG_INFTY - 1);
1203 /* Failed deeper matches of scan, so see if this one works. */
1206 if (regmatch(cc->next))
1209 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1214 cc->lastloc = lastloc;
1219 next = scan + ARG(scan);
1222 inner = NEXTOPER(NEXTOPER(scan));
1225 inner = NEXTOPER(scan);
1230 if (OP(next) != c1) /* No choice. */
1231 next = inner; /* Avoid recursion. */
1233 int lastparen = *reglastparen;
1237 reginput = locinput;
1238 if (regmatch(inner))
1241 for (n = *reglastparen; n > lastparen; n--)
1246 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1250 inner = NEXTOPER(scan);
1251 if (c1 == BRANCHJ) {
1252 inner = NEXTOPER(inner);
1254 } while (scan != NULL && OP(scan) == c1);
1268 /* We suppose that the next guy does not need
1269 backtracking: in particular, it is of constant length,
1270 and has no parenths to influence future backrefs. */
1271 ln = ARG1(scan); /* min to match */
1272 n = ARG2(scan); /* max to match */
1273 paren = scan->flags;
1275 if (paren > regsize)
1277 if (paren > *reglastparen)
1278 *reglastparen = paren;
1280 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1282 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1283 reginput = locinput;
1286 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1288 if (ln && l == 0 && n >= ln
1289 /* In fact, this is tricky. If paren, then the
1290 fact that we did/didnot match may influence
1291 future execution. */
1292 && !(paren && ln == 0))
1294 locinput = reginput;
1295 if (regkind[(U8)OP(next)] == EXACT) {
1296 c1 = UCHARAT(OPERAND(next) + 1);
1297 if (OP(next) == EXACTF)
1299 else if (OP(next) == EXACTFL)
1300 c2 = fold_locale[c1];
1306 /* This may be improved if l == 0. */
1307 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1308 /* If it could work, try it. */
1310 UCHARAT(reginput) == c1 ||
1311 UCHARAT(reginput) == c2)
1315 regstartp[paren] = reginput - l;
1316 regendp[paren] = reginput;
1318 regendp[paren] = NULL;
1324 /* Couldn't or didn't -- move forward. */
1325 reginput = locinput;
1326 if (regrepeat_hard(scan, 1, &l)) {
1328 locinput = reginput;
1334 n = regrepeat_hard(scan, n, &l);
1335 if (n != 0 && l == 0
1336 /* In fact, this is tricky. If paren, then the
1337 fact that we did/didnot match may influence
1338 future execution. */
1339 && !(paren && ln == 0))
1341 locinput = reginput;
1343 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1346 if (regkind[(U8)OP(next)] == EXACT) {
1347 c1 = UCHARAT(OPERAND(next) + 1);
1348 if (OP(next) == EXACTF)
1350 else if (OP(next) == EXACTFL)
1351 c2 = fold_locale[c1];
1359 /* If it could work, try it. */
1361 UCHARAT(reginput) == c1 ||
1362 UCHARAT(reginput) == c2)
1365 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1369 regstartp[paren] = reginput - l;
1370 regendp[paren] = reginput;
1372 regendp[paren] = NULL;
1378 /* Couldn't or didn't -- back up. */
1381 reginput = locinput;
1388 paren = scan->flags; /* Which paren to set */
1389 if (paren > regsize)
1391 if (paren > *reglastparen)
1392 *reglastparen = paren;
1393 ln = ARG1(scan); /* min to match */
1394 n = ARG2(scan); /* max to match */
1395 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1399 ln = ARG1(scan); /* min to match */
1400 n = ARG2(scan); /* max to match */
1401 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1406 scan = NEXTOPER(scan);
1412 scan = NEXTOPER(scan);
1416 * Lookahead to avoid useless match attempts
1417 * when we know what character comes next.
1419 if (regkind[(U8)OP(next)] == EXACT) {
1420 c1 = UCHARAT(OPERAND(next) + 1);
1421 if (OP(next) == EXACTF)
1423 else if (OP(next) == EXACTFL)
1424 c2 = fold_locale[c1];
1430 reginput = locinput;
1434 if (ln && regrepeat(scan, ln) < ln)
1437 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1438 /* If it could work, try it. */
1440 UCHARAT(reginput) == c1 ||
1441 UCHARAT(reginput) == c2)
1445 regstartp[paren] = reginput - 1;
1446 regendp[paren] = reginput;
1448 regendp[paren] = NULL;
1454 /* Couldn't or didn't -- move forward. */
1455 reginput = locinput + ln;
1456 if (regrepeat(scan, 1)) {
1458 reginput = locinput + ln;
1465 n = regrepeat(scan, n);
1466 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1467 (!multiline || OP(next) == SEOL))
1468 ln = n; /* why back off? */
1472 /* If it could work, try it. */
1474 UCHARAT(reginput) == c1 ||
1475 UCHARAT(reginput) == c2)
1479 regstartp[paren] = reginput - 1;
1480 regendp[paren] = reginput;
1482 regendp[paren] = NULL;
1488 /* Couldn't or didn't -- back up. */
1490 reginput = locinput + n;
1494 /* If it could work, try it. */
1496 UCHARAT(reginput) == c1 ||
1497 UCHARAT(reginput) == c2)
1503 /* Couldn't or didn't -- back up. */
1505 reginput = locinput + n;
1512 if (locinput < regtill)
1513 sayNO; /* Cannot match: too short. */
1516 reginput = locinput; /* put where regtry can find it */
1517 sayYES; /* Success! */
1523 if (locinput < bostr + scan->flags)
1528 if (locinput < bostr + scan->flags)
1531 reginput = locinput - scan->flags;
1532 inner = NEXTOPER(NEXTOPER(scan));
1533 if (regmatch(inner) != n) {
1547 if (OP(scan) == SUSPEND) {
1548 locinput = reginput;
1549 nextchr = UCHARAT(locinput);
1554 next = scan + ARG(scan);
1559 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1560 (unsigned long)scan, OP(scan));
1561 FAIL("regexp memory corruption");
1567 * We get here only if there's trouble -- normally "case END" is
1568 * the terminating point.
1570 FAIL("corrupted regexp pointers");
1588 - regrepeat - repeatedly match something simple, report how many
1591 * [This routine now assumes that it will only match on things of length 1.
1592 * That was true before, but now we assume scan - reginput is the count,
1593 * rather than incrementing count on every character.]
1596 regrepeat(regnode *p, I32 max)
1598 register char *scan;
1599 register char *opnd;
1601 register char *loceol = regeol;
1604 if (max != REG_INFTY && max < loceol - scan)
1605 loceol = scan + max;
1606 opnd = (char *) OPERAND(p);
1609 while (scan < loceol && *scan != '\n')
1615 case EXACT: /* length of string is 1 */
1616 c = UCHARAT(++opnd);
1617 while (scan < loceol && UCHARAT(scan) == c)
1620 case EXACTF: /* length of string is 1 */
1621 c = UCHARAT(++opnd);
1622 while (scan < loceol &&
1623 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1626 case EXACTFL: /* length of string is 1 */
1627 reg_flags |= RF_tainted;
1628 c = UCHARAT(++opnd);
1629 while (scan < loceol &&
1630 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1634 while (scan < loceol && REGINCLASS(opnd, *scan))
1638 while (scan < loceol && isALNUM(*scan))
1642 reg_flags |= RF_tainted;
1643 while (scan < loceol && isALNUM_LC(*scan))
1647 while (scan < loceol && !isALNUM(*scan))
1651 reg_flags |= RF_tainted;
1652 while (scan < loceol && !isALNUM_LC(*scan))
1656 while (scan < loceol && isSPACE(*scan))
1660 reg_flags |= RF_tainted;
1661 while (scan < loceol && isSPACE_LC(*scan))
1665 while (scan < loceol && !isSPACE(*scan))
1669 reg_flags |= RF_tainted;
1670 while (scan < loceol && !isSPACE_LC(*scan))
1674 while (scan < loceol && isDIGIT(*scan))
1678 while (scan < loceol && !isDIGIT(*scan))
1681 default: /* Called on something of 0 width. */
1682 break; /* So match right here or not at all. */
1685 c = scan - reginput;
1690 SV *prop = sv_newmortal();
1693 PerlIO_printf(Perl_debug_log,
1694 "%*s %s can match %ld times out of %ld...\n",
1695 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1702 - regrepeat_hard - repeatedly match something, report total lenth and length
1704 * The repeater is supposed to have constant length.
1708 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1710 register char *scan;
1711 register char *start;
1712 register char *loceol = regeol;
1716 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1718 *lp = l = reginput - start;
1719 if (max != REG_INFTY && l*max < loceol - scan)
1720 loceol = scan + l*max;
1726 if (reginput < loceol)
1731 return (scan - start)/l;
1735 - regclass - determine if a character falls into a character class
1739 reginclass(register char *p, register I32 c)
1745 if (ANYOF_TEST(p, c))
1747 else if (flags & ANYOF_FOLD) {
1749 if (flags & ANYOF_LOCALE) {
1750 reg_flags |= RF_tainted;
1751 cf = fold_locale[c];
1755 if (ANYOF_TEST(p, cf))
1759 if (!match && (flags & ANYOF_ISA)) {
1760 reg_flags |= RF_tainted;
1762 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1763 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1764 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1765 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1771 return (flags & ANYOF_INVERT) ? !match : match;