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.
23 /* We *really* need to overwrite these symbols: */
24 # define Perl_regexec_flags my_regexec
25 # define Perl_regdump my_regdump
26 # define Perl_regprop my_regprop
27 /* *These* symbols are masked to allow static link. */
28 # define Perl_pregexec my_pregexec
33 * pregcomp and pregexec -- regsub and regerror are not used in perl
35 * Copyright (c) 1986 by University of Toronto.
36 * Written by Henry Spencer. Not derived from licensed software.
38 * Permission is granted to anyone to use this software for any
39 * purpose on any computer system, and to redistribute it freely,
40 * subject to the following restrictions:
42 * 1. The author is not responsible for the consequences of use of
43 * this software, no matter how awful, even if they arise
46 * 2. The origin of this software must not be misrepresented, either
47 * by explicit claim or by omission.
49 * 3. Altered versions must be plainly marked as such, and must not
50 * be misrepresented as being the original software.
52 **** Alterations to Henry's code are...
54 **** Copyright (c) 1991-1997, Larry Wall
56 **** You may distribute under the terms of either the GNU General Public
57 **** License or the Artistic License, as specified in the README file.
59 * Beware that some of this code is subtly aware of the way operator
60 * precedence is structured in regular expressions. Serious changes in
61 * regular-expression syntax might require a total rethink.
67 #define RF_tainted 1 /* tainted information used? */
68 #define RF_warned 2 /* warned about big count? */
69 #define RF_evaled 4 /* Did an EVAL with setting? */
71 #define RS_init 1 /* eval environment created */
72 #define RS_set 2 /* replsv value is set */
79 typedef I32 CHECKPOINT;
85 static I32 regmatch _((regnode *prog));
86 static I32 regrepeat _((regnode *p, I32 max));
87 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
88 static I32 regtry _((regexp *prog, char *startpos));
90 static bool reginclass _((char *p, I32 c));
91 static CHECKPOINT regcppush _((I32 parenfloor));
92 static char * regcppop _((void));
94 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
97 regcppush(I32 parenfloor)
100 int retval = savestack_ix;
101 int i = (regsize - parenfloor) * 4;
105 for (p = regsize; p > parenfloor; p--) {
106 SSPUSHPTR(regendp[p]);
107 SSPUSHPTR(regstartp[p]);
108 SSPUSHPTR(reg_start_tmp[p]);
112 SSPUSHINT(*reglastparen);
115 SSPUSHINT(SAVEt_REGCONTEXT);
119 /* These are needed since we do not localize EVAL nodes: */
120 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
121 # 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)
131 assert(i == SAVEt_REGCONTEXT);
133 input = (char *) SSPOPPTR;
134 *reglastparen = SSPOPINT;
136 for (i -= 3; i > 0; i -= 4) {
137 paren = (U32)SSPOPINT;
138 reg_start_tmp[paren] = (char *) SSPOPPTR;
139 regstartp[paren] = (char *) SSPOPPTR;
140 tmps = (char*)SSPOPPTR;
141 if (paren <= *reglastparen)
142 regendp[paren] = tmps;
144 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
145 paren, regstartp[paren] - regbol,
146 reg_start_tmp[paren] - regbol,
147 regendp[paren] - regbol,
148 (paren > *reglastparen ? "(no)" : ""));
152 if (*reglastparen + 1 <= regnpar) {
153 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
154 *reglastparen + 1, regnpar);
157 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
159 regstartp[paren] = Nullch;
160 regendp[paren] = Nullch;
165 #define regcpblow(cp) LEAVE_SCOPE(cp)
168 * pregexec and friends
172 - pregexec - match a regexp against a string
175 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
176 /* strend: pointer to null at end of string */
177 /* strbeg: real beginning of string */
178 /* minend: end of match must be >=minend after stringarg. */
179 /* nosave: For optimizations. */
182 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
183 nosave ? 0 : REXEC_COPY_STR);
187 - regexec_flags - match a regexp against a string
190 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
191 /* strend: pointer to null at end of string */
192 /* strbeg: real beginning of string */
193 /* minend: end of match must be >=minend after stringarg. */
194 /* data: May be used for some additional optimizations. */
195 /* nosave: For optimizations. */
199 register char *startpos = stringarg;
201 I32 minlen; /* must match at least this many chars */
202 I32 dontbother = 0; /* how many characters not to try at end */
204 I32 start_shift = 0; /* Offset of the start to find
206 I32 end_shift = 0; /* Same for the end. */
207 I32 scream_pos = -1; /* Internal iterator of scream. */
209 SV* oreplsv = GvSV(replgv);
215 regprecomp = prog->precomp; /* Needed for error messages. */
217 regnarrate = debug & 512;
218 regprogram = prog->program;
222 if (prog == NULL || startpos == NULL) {
223 croak("NULL regexp parameter");
227 minlen = prog->minlen;
228 if (strend - startpos < minlen) goto phooey;
230 if (startpos == strbeg) /* is ^ valid at stringarg? */
233 regprev = stringarg[-1];
234 if (!multiline && regprev == '\n')
235 regprev = '\0'; /* force ^ to NOT match */
238 /* Check validity of program. */
239 if (UCHARAT(prog->program) != MAGIC) {
240 FAIL("corrupted regexp program");
243 regnpar = prog->nparens;
247 /* If there is a "must appear" string, look for it. */
249 if (!(flags & REXEC_CHECKED)
250 && prog->check_substr != Nullsv &&
251 !(prog->reganch & ROPT_ANCH_GPOS) &&
252 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
253 || (multiline && prog->check_substr == prog->anchored_substr)) )
255 start_shift = prog->check_offset_min;
256 /* Should be nonnegative! */
257 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
259 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
260 s = screaminstr(screamer, prog->check_substr,
261 start_shift + (stringarg - strbeg),
262 end_shift, &scream_pos, 0);
268 s = fbm_instr((unsigned char*)s + start_shift,
269 (unsigned char*)strend - end_shift,
270 prog->check_substr, 0);
272 ++BmUSEFUL(prog->check_substr); /* hooray */
273 goto phooey; /* not present */
274 } else if ((s - stringarg) > prog->check_offset_max) {
275 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
276 s -= prog->check_offset_max;
277 } else if (!prog->naughty
278 && --BmUSEFUL(prog->check_substr) < 0
279 && prog->check_substr == prog->float_substr) { /* boo */
280 SvREFCNT_dec(prog->check_substr);
281 prog->check_substr = Nullsv; /* disable */
282 prog->float_substr = Nullsv; /* clear */
287 /* Mark beginning of line for ^ and lookbehind. */
291 /* Mark end of line for $ (and such) */
294 /* see how far we have to get to not match where we matched before */
295 regtill = startpos+minend;
298 PerlIO_printf(Perl_debug_log,
299 "Matching `%.60s%s' against `%.*s%s'\n",
301 (strlen(prog->precomp) > 60 ? "..." : ""),
302 (strend - startpos > 60 ? 60 : strend - startpos),
304 (strend - startpos > 60 ? "..." : ""))
307 /* Simplest case: anchored match need be tried only once. */
308 /* [unless only anchor is BOL and multiline is set] */
309 if (prog->reganch & ROPT_ANCH) {
310 if (regtry(prog, startpos))
312 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
313 (multiline || (prog->reganch & ROPT_IMPLICIT)
314 || (prog->reganch & ROPT_ANCH_MBOL)))
317 dontbother = minlen - 1;
318 strend -= dontbother;
319 /* for multiline we only have to try after newlines */
324 if (s < strend && regtry(prog, s))
332 /* Messy cases: unanchored match. */
333 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
334 /* we have /x+whatever/ */
335 /* it must be a one character string */
336 char ch = SvPVX(prog->anchored_substr)[0];
339 if (regtry(prog, s)) goto got_it;
341 while (s < strend && *s == ch)
348 else if (prog->anchored_substr != Nullsv
349 || (prog->float_substr != Nullsv
350 && prog->float_max_offset < strend - s)) {
351 SV *must = prog->anchored_substr
352 ? prog->anchored_substr : prog->float_substr;
354 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
356 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
357 I32 delta = back_max - back_min;
358 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
359 char *last1 = s - 1; /* Last position checked before */
361 /* XXXX check_substr already used to find `s', can optimize if
362 check_substr==must. */
364 dontbother = end_shift;
365 strend -= dontbother;
366 while ( (s <= last) &&
368 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
369 end_shift, &scream_pos, 0))
370 : (s = fbm_instr((unsigned char*)s + back_min,
371 (unsigned char*)strend, must, 0))) ) {
372 if (s - back_max > last1) {
373 last1 = s - back_min;
378 last1 = s - back_min;
388 } else if (c = prog->regstclass) {
389 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
393 dontbother = minlen - 1;
394 strend -= dontbother; /* don't bother with what can't match */
396 /* We know what class it must start with. */
399 Class = (char *) OPERAND(c);
401 if (REGINCLASS(Class, *s)) {
402 if (tmp && regtry(prog, s))
413 reg_flags |= RF_tainted;
417 dontbother++,strend--;
418 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
419 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
421 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
428 if ((minlen || tmp) && regtry(prog,s))
432 reg_flags |= RF_tainted;
436 dontbother++,strend--;
437 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
438 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
440 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
442 else if (regtry(prog, s))
446 if ((minlen || !tmp) && regtry(prog,s))
452 if (tmp && regtry(prog, s))
463 reg_flags |= RF_tainted;
465 if (isALNUM_LC(*s)) {
466 if (tmp && regtry(prog, s))
479 if (tmp && regtry(prog, s))
490 reg_flags |= RF_tainted;
492 if (!isALNUM_LC(*s)) {
493 if (tmp && regtry(prog, s))
506 if (tmp && regtry(prog, s))
517 reg_flags |= RF_tainted;
519 if (isSPACE_LC(*s)) {
520 if (tmp && regtry(prog, s))
533 if (tmp && regtry(prog, s))
544 reg_flags |= RF_tainted;
546 if (!isSPACE_LC(*s)) {
547 if (tmp && regtry(prog, s))
560 if (tmp && regtry(prog, s))
573 if (tmp && regtry(prog, s))
587 if (prog->float_substr != Nullsv) { /* Trim the end. */
589 I32 oldpos = scream_pos;
592 last = screaminstr(screamer, prog->float_substr, s - strbeg,
593 end_shift, &scream_pos, 1); /* last one */
595 last = scream_olds; /* Only one occurence. */
599 char *little = SvPV(prog->float_substr, len);
600 last = rninstr(s, strend, little, little + len);
602 if (last == NULL) goto phooey; /* Should not happen! */
603 dontbother = strend - last - 1;
605 if (minlen && (dontbother < minlen))
606 dontbother = minlen - 1;
607 strend -= dontbother;
608 /* We don't know much -- general case. */
612 } while (s++ < strend);
619 strend += dontbother; /* uncheat */
620 prog->subbeg = strbeg;
621 prog->subend = strend;
622 RX_MATCH_TAINTED_set(prog, reg_flags & RF_tainted);
624 /* make sure $`, $&, $', and $digit will work later */
625 if (strbeg != prog->subbase) { /* second+ //g match. */
626 if (!(flags & REXEC_COPY_STR)) {
628 Safefree(prog->subbase);
629 prog->subbase = Nullch;
633 I32 i = strend - startpos + (stringarg - strbeg);
634 s = savepvn(strbeg, i);
635 Safefree(prog->subbase);
637 prog->subbeg = prog->subbase;
638 prog->subend = prog->subbase + i;
639 s = prog->subbase + (stringarg - strbeg);
640 for (i = 0; i <= prog->nparens; i++) {
642 prog->startp[i] = s + (prog->startp[i] - startpos);
643 prog->endp[i] = s + (prog->endp[i] - startpos);
648 /* Preserve the current value of $^R */
649 if (oreplsv != GvSV(replgv)) {
650 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
651 restored, the value remains
661 - regtry - try match at specific point
663 STATIC I32 /* 0 failure, 1 success */
664 regtry(regexp *prog, char *startpos)
672 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
673 reg_eval_set = RS_init;
675 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
677 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
678 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
679 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
681 /* Apparently this is not needed, judging by wantarray. */
682 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
683 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
686 regstartp = prog->startp;
687 regendp = prog->endp;
688 reglastparen = &prog->lastparen;
691 if (reg_start_tmpl <= prog->nparens) {
692 reg_start_tmpl = prog->nparens*3/2 + 3;
694 Renew(reg_start_tmp, reg_start_tmpl, char*);
696 New(22,reg_start_tmp, reg_start_tmpl, char*);
701 regdata = prog->data;
703 for (i = prog->nparens; i >= 0; i--) {
709 if (regmatch(prog->program + 1)) {
710 prog->startp[0] = startpos;
711 prog->endp[0] = reginput;
719 - regmatch - main matching routine
721 * Conceptually the strategy is simple: check to see whether the current
722 * node matches, call self recursively to see whether the rest matches,
723 * and then act accordingly. In practice we make some effort to avoid
724 * recursion, in particular by going through "ordinary" nodes (that don't
725 * need to know whether the rest of the match failed) by a loop instead of
728 /* [lwall] I've hoisted the register declarations to the outer block in order to
729 * maybe save a little bit of pushing and popping on the stack. It also takes
730 * advantage of machines that use a register save mask on subroutine entry.
732 STATIC I32 /* 0 failure, 1 success */
733 regmatch(regnode *prog)
736 register regnode *scan; /* Current node. */
737 regnode *next; /* Next node. */
738 regnode *inner; /* Next node in internal branch. */
739 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
740 register I32 n; /* no or next */
741 register I32 ln; /* len or last */
742 register char *s; /* operand or save */
743 register char *locinput = reginput;
744 register I32 c1, c2, paren; /* case fold search, parenth */
745 int minmod = 0, sw = 0, logical = 0;
750 nextchr = UCHARAT(locinput);
752 while (scan != NULL) {
753 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
755 # define sayYES goto yes
756 # define sayNO goto no
757 # define saySAME(x) if (x) goto yes; else goto no
758 # define REPORT_CODE_OFF 24
760 # define sayYES return 1
761 # define sayNO return 0
762 # define saySAME(x) return x
765 SV *prop = sv_newmortal();
766 int docolor = *colors[0];
767 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
768 int l = (regeol - locinput > taill ? taill : regeol - locinput);
769 int pref_len = (locinput - bostr > (5 + taill) - l
770 ? (5 + taill) - l : locinput - bostr);
772 if (l + pref_len < (5 + taill) && l < regeol - locinput)
773 l = ( regeol - locinput > (5 + taill) - pref_len
774 ? (5 + taill) - pref_len : regeol - locinput);
776 PerlIO_printf(Perl_debug_log,
777 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
779 colors[2], pref_len, locinput - pref_len, colors[3],
780 (docolor ? "" : "> <"),
781 colors[0], l, locinput, colors[1],
782 15 - l - pref_len + 1,
784 regindent*2, "", scan - regprogram,
788 next = scan + NEXT_OFF(scan);
794 if (locinput == bostr
797 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
799 /* regtill = regbol; */
804 if (locinput == bostr
806 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
812 if (locinput == regbol && regprev == '\n')
816 if (locinput == regbol)
826 if ((nextchr || locinput < regeol) && nextchr != '\n')
831 if ((nextchr || locinput < regeol) && nextchr != '\n')
833 if (regeol - locinput > 1)
837 if (regeol != locinput)
841 if (!nextchr && locinput >= regeol)
843 nextchr = UCHARAT(++locinput);
846 if (!nextchr && locinput >= regeol || nextchr == '\n')
848 nextchr = UCHARAT(++locinput);
851 s = (char *) OPERAND(scan);
853 /* Inline the first character, for speed. */
854 if (UCHARAT(s) != nextchr)
856 if (regeol - locinput < ln)
858 if (ln > 1 && memNE(s, locinput, ln))
861 nextchr = UCHARAT(locinput);
864 reg_flags |= RF_tainted;
867 s = (char *) OPERAND(scan);
869 /* Inline the first character, for speed. */
870 if (UCHARAT(s) != nextchr &&
871 UCHARAT(s) != ((OP(scan) == EXACTF)
872 ? fold : fold_locale)[nextchr])
874 if (regeol - locinput < ln)
876 if (ln > 1 && (OP(scan) == EXACTF
877 ? ibcmp(s, locinput, ln)
878 : ibcmp_locale(s, locinput, ln)))
881 nextchr = UCHARAT(locinput);
884 s = (char *) OPERAND(scan);
886 nextchr = UCHARAT(locinput);
887 if (!REGINCLASS(s, nextchr))
889 if (!nextchr && locinput >= regeol)
891 nextchr = UCHARAT(++locinput);
894 reg_flags |= RF_tainted;
899 if (!(OP(scan) == ALNUM
900 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
902 nextchr = UCHARAT(++locinput);
905 reg_flags |= RF_tainted;
908 if (!nextchr && locinput >= regeol)
910 if (OP(scan) == NALNUM
911 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
913 nextchr = UCHARAT(++locinput);
917 reg_flags |= RF_tainted;
921 /* was last char in word? */
922 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
923 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
925 n = isALNUM(nextchr);
929 n = isALNUM_LC(nextchr);
931 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
935 reg_flags |= RF_tainted;
938 if (!nextchr && locinput >= regeol)
940 if (!(OP(scan) == SPACE
941 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
943 nextchr = UCHARAT(++locinput);
946 reg_flags |= RF_tainted;
951 if (OP(scan) == SPACE
952 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
954 nextchr = UCHARAT(++locinput);
957 if (!isDIGIT(nextchr))
959 nextchr = UCHARAT(++locinput);
962 if (!nextchr && locinput >= regeol)
964 if (isDIGIT(nextchr))
966 nextchr = UCHARAT(++locinput);
969 reg_flags |= RF_tainted;
973 n = ARG(scan); /* which paren pair */
975 if (*reglastparen < n || !s)
976 sayNO; /* Do not match unless seen CLOSEn. */
979 /* Inline the first character, for speed. */
980 if (UCHARAT(s) != nextchr &&
982 (UCHARAT(s) != ((OP(scan) == REFF
983 ? fold : fold_locale)[nextchr]))))
986 if (locinput + ln > regeol)
988 if (ln > 1 && (OP(scan) == REF
989 ? memNE(s, locinput, ln)
991 ? ibcmp(s, locinput, ln)
992 : ibcmp_locale(s, locinput, ln))))
995 nextchr = UCHARAT(locinput);
1007 COP *ocurcop = curcop;
1008 SV **ocurpad = curpad;
1012 op = (OP_4tree*)regdata->data[n];
1013 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1014 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1016 CALLRUNOPS(); /* Scalar context. */
1025 sv_setsv(save_scalar(replgv), ret);
1032 n = ARG(scan); /* which paren pair */
1033 reg_start_tmp[n] = locinput;
1038 n = ARG(scan); /* which paren pair */
1039 regstartp[n] = reg_start_tmp[n];
1040 regendp[n] = locinput;
1041 if (n > *reglastparen)
1045 n = ARG(scan); /* which paren pair */
1046 sw = (*reglastparen >= n && regendp[n] != NULL);
1050 next = NEXTOPER(NEXTOPER(scan));
1052 next = scan + ARG(scan);
1053 if (OP(next) == IFTHEN) /* Fake one. */
1054 next = NEXTOPER(NEXTOPER(next));
1062 CHECKPOINT cp = savestack_ix;
1064 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1068 cc.parenfloor = *reglastparen;
1070 cc.min = ARG1(scan);
1071 cc.max = ARG2(scan);
1072 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1076 reginput = locinput;
1077 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1085 * This is really hard to understand, because after we match
1086 * what we're trying to match, we must make sure the rest of
1087 * the RE is going to match for sure, and to do that we have
1088 * to go back UP the parse tree by recursing ever deeper. And
1089 * if it fails, we have to reset our parent's current state
1090 * that we can try again after backing off.
1093 CHECKPOINT cp, lastcp;
1095 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1097 n = cc->cur + 1; /* how many we know we matched */
1098 reginput = locinput;
1101 PerlIO_printf(Perl_debug_log,
1102 "%*s %ld out of %ld..%ld cc=%lx\n",
1103 REPORT_CODE_OFF+regindent*2, "",
1104 (long)n, (long)cc->min,
1105 (long)cc->max, (long)cc)
1108 /* If degenerate scan matches "", assume scan done. */
1110 if (locinput == cc->lastloc && n >= cc->min) {
1114 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1116 if (regmatch(cc->next))
1119 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1126 /* First just match a string of min scans. */
1130 cc->lastloc = locinput;
1131 if (regmatch(cc->scan))
1134 cc->lastloc = lastloc;
1136 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1141 /* Prefer next over scan for minimal matching. */
1146 cp = regcppush(cc->parenfloor);
1148 if (regmatch(cc->next)) {
1150 sayYES; /* All done. */
1157 if (n >= cc->max) { /* Maximum greed exceeded? */
1158 if (dowarn && n >= REG_INFTY
1159 && !(reg_flags & RF_warned)) {
1160 reg_flags |= RF_warned;
1161 warn("count exceeded %d", REG_INFTY - 1);
1167 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1169 /* Try scanning more and see if it helps. */
1170 reginput = locinput;
1172 cc->lastloc = locinput;
1173 cp = regcppush(cc->parenfloor);
1175 if (regmatch(cc->scan)) {
1180 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1185 cc->lastloc = lastloc;
1189 /* Prefer scan over next for maximal matching. */
1191 if (n < cc->max) { /* More greed allowed? */
1192 cp = regcppush(cc->parenfloor);
1194 cc->lastloc = locinput;
1196 if (regmatch(cc->scan)) {
1201 regcppop(); /* Restore some previous $<digit>s? */
1202 reginput = locinput;
1204 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1207 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1208 reg_flags |= RF_warned;
1209 warn("count exceeded %d", REG_INFTY - 1);
1212 /* Failed deeper matches of scan, so see if this one works. */
1215 if (regmatch(cc->next))
1218 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1223 cc->lastloc = lastloc;
1228 next = scan + ARG(scan);
1231 inner = NEXTOPER(NEXTOPER(scan));
1234 inner = NEXTOPER(scan);
1239 if (OP(next) != c1) /* No choice. */
1240 next = inner; /* Avoid recursion. */
1242 int lastparen = *reglastparen;
1246 reginput = locinput;
1247 if (regmatch(inner))
1250 for (n = *reglastparen; n > lastparen; n--)
1255 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1259 inner = NEXTOPER(scan);
1260 if (c1 == BRANCHJ) {
1261 inner = NEXTOPER(inner);
1263 } while (scan != NULL && OP(scan) == c1);
1277 /* We suppose that the next guy does not need
1278 backtracking: in particular, it is of constant length,
1279 and has no parenths to influence future backrefs. */
1280 ln = ARG1(scan); /* min to match */
1281 n = ARG2(scan); /* max to match */
1282 paren = scan->flags;
1284 if (paren > regsize)
1286 if (paren > *reglastparen)
1287 *reglastparen = paren;
1289 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1291 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1292 reginput = locinput;
1295 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1297 if (ln && l == 0 && n >= ln
1298 /* In fact, this is tricky. If paren, then the
1299 fact that we did/didnot match may influence
1300 future execution. */
1301 && !(paren && ln == 0))
1303 locinput = reginput;
1304 if (regkind[(U8)OP(next)] == EXACT) {
1305 c1 = UCHARAT(OPERAND(next) + 1);
1306 if (OP(next) == EXACTF)
1308 else if (OP(next) == EXACTFL)
1309 c2 = fold_locale[c1];
1315 /* This may be improved if l == 0. */
1316 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1317 /* If it could work, try it. */
1319 UCHARAT(reginput) == c1 ||
1320 UCHARAT(reginput) == c2)
1324 regstartp[paren] = reginput - l;
1325 regendp[paren] = reginput;
1327 regendp[paren] = NULL;
1333 /* Couldn't or didn't -- move forward. */
1334 reginput = locinput;
1335 if (regrepeat_hard(scan, 1, &l)) {
1337 locinput = reginput;
1343 n = regrepeat_hard(scan, n, &l);
1344 if (n != 0 && l == 0
1345 /* In fact, this is tricky. If paren, then the
1346 fact that we did/didnot match may influence
1347 future execution. */
1348 && !(paren && ln == 0))
1350 locinput = reginput;
1352 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1355 if (regkind[(U8)OP(next)] == EXACT) {
1356 c1 = UCHARAT(OPERAND(next) + 1);
1357 if (OP(next) == EXACTF)
1359 else if (OP(next) == EXACTFL)
1360 c2 = fold_locale[c1];
1368 /* If it could work, try it. */
1370 UCHARAT(reginput) == c1 ||
1371 UCHARAT(reginput) == c2)
1374 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1378 regstartp[paren] = reginput - l;
1379 regendp[paren] = reginput;
1381 regendp[paren] = NULL;
1387 /* Couldn't or didn't -- back up. */
1390 reginput = locinput;
1397 paren = scan->flags; /* Which paren to set */
1398 if (paren > regsize)
1400 if (paren > *reglastparen)
1401 *reglastparen = paren;
1402 ln = ARG1(scan); /* min to match */
1403 n = ARG2(scan); /* max to match */
1404 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1408 ln = ARG1(scan); /* min to match */
1409 n = ARG2(scan); /* max to match */
1410 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1415 scan = NEXTOPER(scan);
1421 scan = NEXTOPER(scan);
1425 * Lookahead to avoid useless match attempts
1426 * when we know what character comes next.
1428 if (regkind[(U8)OP(next)] == EXACT) {
1429 c1 = UCHARAT(OPERAND(next) + 1);
1430 if (OP(next) == EXACTF)
1432 else if (OP(next) == EXACTFL)
1433 c2 = fold_locale[c1];
1439 reginput = locinput;
1443 if (ln && regrepeat(scan, ln) < ln)
1446 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1447 /* If it could work, try it. */
1449 UCHARAT(reginput) == c1 ||
1450 UCHARAT(reginput) == c2)
1454 regstartp[paren] = reginput - 1;
1455 regendp[paren] = reginput;
1457 regendp[paren] = NULL;
1463 /* Couldn't or didn't -- move forward. */
1464 reginput = locinput + ln;
1465 if (regrepeat(scan, 1)) {
1467 reginput = locinput + ln;
1474 n = regrepeat(scan, n);
1475 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1476 (!multiline || OP(next) == SEOL))
1477 ln = n; /* why back off? */
1481 /* If it could work, try it. */
1483 UCHARAT(reginput) == c1 ||
1484 UCHARAT(reginput) == c2)
1488 regstartp[paren] = reginput - 1;
1489 regendp[paren] = reginput;
1491 regendp[paren] = NULL;
1497 /* Couldn't or didn't -- back up. */
1499 reginput = locinput + n;
1503 /* If it could work, try it. */
1505 UCHARAT(reginput) == c1 ||
1506 UCHARAT(reginput) == c2)
1512 /* Couldn't or didn't -- back up. */
1514 reginput = locinput + n;
1521 if (locinput < regtill)
1522 sayNO; /* Cannot match: too short. */
1525 reginput = locinput; /* put where regtry can find it */
1526 sayYES; /* Success! */
1532 if (locinput < bostr + scan->flags)
1537 if (locinput < bostr + scan->flags)
1540 reginput = locinput - scan->flags;
1541 inner = NEXTOPER(NEXTOPER(scan));
1542 if (regmatch(inner) != n) {
1556 if (OP(scan) == SUSPEND) {
1557 locinput = reginput;
1558 nextchr = UCHARAT(locinput);
1563 next = scan + ARG(scan);
1568 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1569 (unsigned long)scan, OP(scan));
1570 FAIL("regexp memory corruption");
1576 * We get here only if there's trouble -- normally "case END" is
1577 * the terminating point.
1579 FAIL("corrupted regexp pointers");
1597 - regrepeat - repeatedly match something simple, report how many
1600 * [This routine now assumes that it will only match on things of length 1.
1601 * That was true before, but now we assume scan - reginput is the count,
1602 * rather than incrementing count on every character.]
1605 regrepeat(regnode *p, I32 max)
1607 register char *scan;
1608 register char *opnd;
1610 register char *loceol = regeol;
1613 if (max != REG_INFTY && max < loceol - scan)
1614 loceol = scan + max;
1615 opnd = (char *) OPERAND(p);
1618 while (scan < loceol && *scan != '\n')
1624 case EXACT: /* length of string is 1 */
1625 c = UCHARAT(++opnd);
1626 while (scan < loceol && UCHARAT(scan) == c)
1629 case EXACTF: /* length of string is 1 */
1630 c = UCHARAT(++opnd);
1631 while (scan < loceol &&
1632 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1635 case EXACTFL: /* length of string is 1 */
1636 reg_flags |= RF_tainted;
1637 c = UCHARAT(++opnd);
1638 while (scan < loceol &&
1639 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1643 while (scan < loceol && REGINCLASS(opnd, *scan))
1647 while (scan < loceol && isALNUM(*scan))
1651 reg_flags |= RF_tainted;
1652 while (scan < loceol && isALNUM_LC(*scan))
1656 while (scan < loceol && !isALNUM(*scan))
1660 reg_flags |= RF_tainted;
1661 while (scan < loceol && !isALNUM_LC(*scan))
1665 while (scan < loceol && isSPACE(*scan))
1669 reg_flags |= RF_tainted;
1670 while (scan < loceol && isSPACE_LC(*scan))
1674 while (scan < loceol && !isSPACE(*scan))
1678 reg_flags |= RF_tainted;
1679 while (scan < loceol && !isSPACE_LC(*scan))
1683 while (scan < loceol && isDIGIT(*scan))
1687 while (scan < loceol && !isDIGIT(*scan))
1690 default: /* Called on something of 0 width. */
1691 break; /* So match right here or not at all. */
1694 c = scan - reginput;
1699 SV *prop = sv_newmortal();
1702 PerlIO_printf(Perl_debug_log,
1703 "%*s %s can match %ld times out of %ld...\n",
1704 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1711 - regrepeat_hard - repeatedly match something, report total lenth and length
1713 * The repeater is supposed to have constant length.
1717 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1719 register char *scan;
1720 register char *start;
1721 register char *loceol = regeol;
1725 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1727 *lp = l = reginput - start;
1728 if (max != REG_INFTY && l*max < loceol - scan)
1729 loceol = scan + l*max;
1735 if (reginput < loceol)
1740 return (scan - start)/l;
1744 - regclass - determine if a character falls into a character class
1748 reginclass(register char *p, register I32 c)
1754 if (ANYOF_TEST(p, c))
1756 else if (flags & ANYOF_FOLD) {
1758 if (flags & ANYOF_LOCALE) {
1759 reg_flags |= RF_tainted;
1760 cf = fold_locale[c];
1764 if (ANYOF_TEST(p, cf))
1768 if (!match && (flags & ANYOF_ISA)) {
1769 reg_flags |= RF_tainted;
1771 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1772 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1773 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1774 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1780 return (flags & ANYOF_INVERT) ? !match : match;