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, \
121 " Setting an EVAL scope, savestack=%i\n", \
122 savestack_ix)); lastcp = savestack_ix
124 # define REGCP_UNWIND DEBUG_r(lastcp != savestack_ix ? \
125 PerlIO_printf(Perl_debug_log, \
126 " Clearing an EVAL scope, savestack=%i..%i\n", \
127 lastcp, savestack_ix) : 0); regcpblow(lastcp)
137 assert(i == SAVEt_REGCONTEXT);
139 input = (char *) SSPOPPTR;
140 *reglastparen = SSPOPINT;
142 for (i -= 3; i > 0; i -= 4) {
143 paren = (U32)SSPOPINT;
144 reg_start_tmp[paren] = (char *) SSPOPPTR;
145 regstartp[paren] = (char *) SSPOPPTR;
146 tmps = (char*)SSPOPPTR;
147 if (paren <= *reglastparen)
148 regendp[paren] = tmps;
150 PerlIO_printf(Perl_debug_log,
151 " restoring \\%d to %d(%d)..%d%s\n",
152 paren, regstartp[paren] - regbol,
153 reg_start_tmp[paren] - regbol,
154 regendp[paren] - regbol,
155 (paren > *reglastparen ? "(no)" : ""));
159 if (*reglastparen + 1 <= regnpar) {
160 PerlIO_printf(Perl_debug_log,
161 " restoring \\%d..\\%d to undef\n",
162 *reglastparen + 1, regnpar);
165 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
167 regstartp[paren] = Nullch;
168 regendp[paren] = Nullch;
173 #define regcpblow(cp) LEAVE_SCOPE(cp)
176 * pregexec and friends
180 - pregexec - match a regexp against a string
183 pregexec(register regexp *prog, char *stringarg, register char *strend,
184 char *strbeg, I32 minend, SV *screamer, U32 nosave)
185 /* strend: pointer to null at end of string */
186 /* strbeg: real beginning of string */
187 /* minend: end of match must be >=minend after stringarg. */
188 /* nosave: For optimizations. */
191 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
192 nosave ? 0 : REXEC_COPY_STR);
196 - regexec_flags - match a regexp against a string
199 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
200 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
201 /* strend: pointer to null at end of string */
202 /* strbeg: real beginning of string */
203 /* minend: end of match must be >=minend after stringarg. */
204 /* data: May be used for some additional optimizations. */
205 /* nosave: For optimizations. */
209 register char *startpos = stringarg;
211 I32 minlen; /* must match at least this many chars */
212 I32 dontbother = 0; /* how many characters not to try at end */
214 I32 start_shift = 0; /* Offset of the start to find
216 I32 end_shift = 0; /* Same for the end. */
217 I32 scream_pos = -1; /* Internal iterator of scream. */
219 SV* oreplsv = GvSV(replgv);
225 regprecomp = prog->precomp; /* Needed for error messages. */
227 regnarrate = debug & 512;
228 regprogram = prog->program;
232 if (prog == NULL || startpos == NULL) {
233 croak("NULL regexp parameter");
237 minlen = prog->minlen;
238 if (strend - startpos < minlen) goto phooey;
240 if (startpos == strbeg) /* is ^ valid at stringarg? */
243 regprev = stringarg[-1];
244 if (!multiline && regprev == '\n')
245 regprev = '\0'; /* force ^ to NOT match */
248 /* Check validity of program. */
249 if (UCHARAT(prog->program) != MAGIC) {
250 FAIL("corrupted regexp program");
253 regnpar = prog->nparens;
257 /* If there is a "must appear" string, look for it. */
259 if (!(flags & REXEC_CHECKED)
260 && prog->check_substr != Nullsv &&
261 !(prog->reganch & ROPT_ANCH_GPOS) &&
262 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
263 || (multiline && prog->check_substr == prog->anchored_substr)) )
265 start_shift = prog->check_offset_min;
266 /* Should be nonnegative! */
267 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
269 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
270 s = screaminstr(screamer, prog->check_substr,
271 start_shift + (stringarg - strbeg),
272 end_shift, &scream_pos, 0);
278 s = fbm_instr((unsigned char*)s + start_shift,
279 (unsigned char*)strend - end_shift,
280 prog->check_substr, 0);
282 ++BmUSEFUL(prog->check_substr); /* hooray */
283 goto phooey; /* not present */
284 } else if ((s - stringarg) > prog->check_offset_max) {
285 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
286 s -= prog->check_offset_max;
287 } else if (!prog->naughty
288 && --BmUSEFUL(prog->check_substr) < 0
289 && prog->check_substr == prog->float_substr) { /* boo */
290 SvREFCNT_dec(prog->check_substr);
291 prog->check_substr = Nullsv; /* disable */
292 prog->float_substr = Nullsv; /* clear */
297 /* Mark beginning of line for ^ and lookbehind. */
301 /* Mark end of line for $ (and such) */
304 /* see how far we have to get to not match where we matched before */
305 regtill = startpos+minend;
308 PerlIO_printf(Perl_debug_log,
309 "Matching `%.60s%s' against `%.*s%s'\n",
311 (strlen(prog->precomp) > 60 ? "..." : ""),
312 (strend - startpos > 60 ? 60 : strend - startpos),
314 (strend - startpos > 60 ? "..." : ""))
317 /* Simplest case: anchored match need be tried only once. */
318 /* [unless only anchor is BOL and multiline is set] */
319 if (prog->reganch & ROPT_ANCH) {
320 if (regtry(prog, startpos))
322 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
323 (multiline || (prog->reganch & ROPT_IMPLICIT)
324 || (prog->reganch & ROPT_ANCH_MBOL)))
327 dontbother = minlen - 1;
328 strend -= dontbother;
329 /* for multiline we only have to try after newlines */
334 if (s < strend && regtry(prog, s))
342 /* Messy cases: unanchored match. */
343 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
344 /* we have /x+whatever/ */
345 /* it must be a one character string */
346 char ch = SvPVX(prog->anchored_substr)[0];
349 if (regtry(prog, s)) goto got_it;
351 while (s < strend && *s == ch)
358 else if (prog->anchored_substr != Nullsv
359 || (prog->float_substr != Nullsv
360 && prog->float_max_offset < strend - s)) {
361 SV *must = prog->anchored_substr
362 ? prog->anchored_substr : prog->float_substr;
364 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
366 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
367 I32 delta = back_max - back_min;
368 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
369 char *last1 = s - 1; /* Last position checked before */
371 /* XXXX check_substr already used to find `s', can optimize if
372 check_substr==must. */
374 dontbother = end_shift;
375 strend -= dontbother;
376 while ( (s <= last) &&
378 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
379 end_shift, &scream_pos, 0))
380 : (s = fbm_instr((unsigned char*)s + back_min,
381 (unsigned char*)strend, must, 0))) ) {
382 if (s - back_max > last1) {
383 last1 = s - back_min;
388 last1 = s - back_min;
398 } else if (c = prog->regstclass) {
399 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
403 dontbother = minlen - 1;
404 strend -= dontbother; /* don't bother with what can't match */
406 /* We know what class it must start with. */
409 Class = (char *) OPERAND(c);
411 if (REGINCLASS(Class, *s)) {
412 if (tmp && regtry(prog, s))
423 reg_flags |= RF_tainted;
427 dontbother++,strend--;
428 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
429 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
431 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
438 if ((minlen || tmp) && regtry(prog,s))
442 reg_flags |= RF_tainted;
446 dontbother++,strend--;
447 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
448 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
450 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
452 else if (regtry(prog, s))
456 if ((minlen || !tmp) && regtry(prog,s))
462 if (tmp && regtry(prog, s))
473 reg_flags |= RF_tainted;
475 if (isALNUM_LC(*s)) {
476 if (tmp && regtry(prog, s))
489 if (tmp && regtry(prog, s))
500 reg_flags |= RF_tainted;
502 if (!isALNUM_LC(*s)) {
503 if (tmp && regtry(prog, s))
516 if (tmp && regtry(prog, s))
527 reg_flags |= RF_tainted;
529 if (isSPACE_LC(*s)) {
530 if (tmp && regtry(prog, s))
543 if (tmp && regtry(prog, s))
554 reg_flags |= RF_tainted;
556 if (!isSPACE_LC(*s)) {
557 if (tmp && regtry(prog, s))
570 if (tmp && regtry(prog, s))
583 if (tmp && regtry(prog, s))
597 if (prog->float_substr != Nullsv) { /* Trim the end. */
599 I32 oldpos = scream_pos;
602 last = screaminstr(screamer, prog->float_substr, s - strbeg,
603 end_shift, &scream_pos, 1); /* last one */
605 last = scream_olds; /* Only one occurence. */
609 char *little = SvPV(prog->float_substr, len);
610 last = rninstr(s, strend, little, little + len);
612 if (last == NULL) goto phooey; /* Should not happen! */
613 dontbother = strend - last - 1;
615 if (minlen && (dontbother < minlen))
616 dontbother = minlen - 1;
617 strend -= dontbother;
618 /* We don't know much -- general case. */
622 } while (s++ < strend);
629 strend += dontbother; /* uncheat */
630 prog->subbeg = strbeg;
631 prog->subend = strend;
632 RX_MATCH_TAINTED_set(prog, reg_flags & RF_tainted);
634 /* make sure $`, $&, $', and $digit will work later */
635 if (strbeg != prog->subbase) { /* second+ //g match. */
636 if (!(flags & REXEC_COPY_STR)) {
638 Safefree(prog->subbase);
639 prog->subbase = Nullch;
643 I32 i = strend - startpos + (stringarg - strbeg);
644 s = savepvn(strbeg, i);
645 Safefree(prog->subbase);
647 prog->subbeg = prog->subbase;
648 prog->subend = prog->subbase + i;
649 s = prog->subbase + (stringarg - strbeg);
650 for (i = 0; i <= prog->nparens; i++) {
652 prog->startp[i] = s + (prog->startp[i] - startpos);
653 prog->endp[i] = s + (prog->endp[i] - startpos);
658 /* Preserve the current value of $^R */
659 if (oreplsv != GvSV(replgv)) {
660 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
661 restored, the value remains
671 - regtry - try match at specific point
673 STATIC I32 /* 0 failure, 1 success */
674 regtry(regexp *prog, char *startpos)
682 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
683 reg_eval_set = RS_init;
685 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
686 stack_sp - stack_base);
688 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
689 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
690 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
692 /* Apparently this is not needed, judging by wantarray. */
693 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
694 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
697 regstartp = prog->startp;
698 regendp = prog->endp;
699 reglastparen = &prog->lastparen;
702 if (reg_start_tmpl <= prog->nparens) {
703 reg_start_tmpl = prog->nparens*3/2 + 3;
705 Renew(reg_start_tmp, reg_start_tmpl, char*);
707 New(22,reg_start_tmp, reg_start_tmpl, char*);
712 regdata = prog->data;
714 for (i = prog->nparens; i >= 0; i--) {
720 if (regmatch(prog->program + 1)) {
721 prog->startp[0] = startpos;
722 prog->endp[0] = reginput;
730 - regmatch - main matching routine
732 * Conceptually the strategy is simple: check to see whether the current
733 * node matches, call self recursively to see whether the rest matches,
734 * and then act accordingly. In practice we make some effort to avoid
735 * recursion, in particular by going through "ordinary" nodes (that don't
736 * need to know whether the rest of the match failed) by a loop instead of
739 /* [lwall] I've hoisted the register declarations to the outer block in order to
740 * maybe save a little bit of pushing and popping on the stack. It also takes
741 * advantage of machines that use a register save mask on subroutine entry.
743 STATIC I32 /* 0 failure, 1 success */
744 regmatch(regnode *prog)
747 register regnode *scan; /* Current node. */
748 regnode *next; /* Next node. */
749 regnode *inner; /* Next node in internal branch. */
750 register I32 nextchr; /* renamed nextchr - nextchar colides with
751 function of same name */
752 register I32 n; /* no or next */
753 register I32 ln; /* len or last */
754 register char *s; /* operand or save */
755 register char *locinput = reginput;
756 register I32 c1, c2, paren; /* case fold search, parenth */
757 int minmod = 0, sw = 0, logical = 0;
762 nextchr = UCHARAT(locinput);
764 while (scan != NULL) {
765 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
767 # define sayYES goto yes
768 # define sayNO goto no
769 # define saySAME(x) if (x) goto yes; else goto no
770 # define REPORT_CODE_OFF 24
772 # define sayYES return 1
773 # define sayNO return 0
774 # define saySAME(x) return x
777 SV *prop = sv_newmortal();
778 int docolor = *colors[0];
779 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
780 int l = (regeol - locinput > taill ? taill : regeol - locinput);
781 int pref_len = (locinput - bostr > (5 + taill) - l
782 ? (5 + taill) - l : locinput - bostr);
784 if (l + pref_len < (5 + taill) && l < regeol - locinput)
785 l = ( regeol - locinput > (5 + taill) - pref_len
786 ? (5 + taill) - pref_len : regeol - locinput);
788 PerlIO_printf(Perl_debug_log,
789 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
791 colors[2], pref_len, locinput - pref_len, colors[3],
792 (docolor ? "" : "> <"),
793 colors[0], l, locinput, colors[1],
794 15 - l - pref_len + 1,
796 scan - regprogram, regindent*2, "",
800 next = scan + NEXT_OFF(scan);
806 if (locinput == bostr
809 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
811 /* regtill = regbol; */
816 if (locinput == bostr
818 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
824 if (locinput == regbol && regprev == '\n')
828 if (locinput == regbol)
838 if ((nextchr || locinput < regeol) && nextchr != '\n')
843 if ((nextchr || locinput < regeol) && nextchr != '\n')
845 if (regeol - locinput > 1)
849 if (regeol != locinput)
853 if (!nextchr && locinput >= regeol)
855 nextchr = UCHARAT(++locinput);
858 if (!nextchr && locinput >= regeol || nextchr == '\n')
860 nextchr = UCHARAT(++locinput);
863 s = (char *) OPERAND(scan);
865 /* Inline the first character, for speed. */
866 if (UCHARAT(s) != nextchr)
868 if (regeol - locinput < ln)
870 if (ln > 1 && memNE(s, locinput, ln))
873 nextchr = UCHARAT(locinput);
876 reg_flags |= RF_tainted;
879 s = (char *) OPERAND(scan);
881 /* Inline the first character, for speed. */
882 if (UCHARAT(s) != nextchr &&
883 UCHARAT(s) != ((OP(scan) == EXACTF)
884 ? fold : fold_locale)[nextchr])
886 if (regeol - locinput < ln)
888 if (ln > 1 && (OP(scan) == EXACTF
889 ? ibcmp(s, locinput, ln)
890 : ibcmp_locale(s, locinput, ln)))
893 nextchr = UCHARAT(locinput);
896 s = (char *) OPERAND(scan);
898 nextchr = UCHARAT(locinput);
899 if (!REGINCLASS(s, nextchr))
901 if (!nextchr && locinput >= regeol)
903 nextchr = UCHARAT(++locinput);
906 reg_flags |= RF_tainted;
911 if (!(OP(scan) == ALNUM
912 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
914 nextchr = UCHARAT(++locinput);
917 reg_flags |= RF_tainted;
920 if (!nextchr && locinput >= regeol)
922 if (OP(scan) == NALNUM
923 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
925 nextchr = UCHARAT(++locinput);
929 reg_flags |= RF_tainted;
933 /* was last char in word? */
934 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
935 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
937 n = isALNUM(nextchr);
941 n = isALNUM_LC(nextchr);
943 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
947 reg_flags |= RF_tainted;
950 if (!nextchr && locinput >= regeol)
952 if (!(OP(scan) == SPACE
953 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
955 nextchr = UCHARAT(++locinput);
958 reg_flags |= RF_tainted;
963 if (OP(scan) == SPACE
964 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
966 nextchr = UCHARAT(++locinput);
969 if (!isDIGIT(nextchr))
971 nextchr = UCHARAT(++locinput);
974 if (!nextchr && locinput >= regeol)
976 if (isDIGIT(nextchr))
978 nextchr = UCHARAT(++locinput);
981 reg_flags |= RF_tainted;
985 n = ARG(scan); /* which paren pair */
987 if (*reglastparen < n || !s)
988 sayNO; /* Do not match unless seen CLOSEn. */
991 /* Inline the first character, for speed. */
992 if (UCHARAT(s) != nextchr &&
994 (UCHARAT(s) != ((OP(scan) == REFF
995 ? fold : fold_locale)[nextchr]))))
998 if (locinput + ln > regeol)
1000 if (ln > 1 && (OP(scan) == REF
1001 ? memNE(s, locinput, ln)
1003 ? ibcmp(s, locinput, ln)
1004 : ibcmp_locale(s, locinput, ln))))
1007 nextchr = UCHARAT(locinput);
1019 COP *ocurcop = curcop;
1020 SV **ocurpad = curpad;
1024 op = (OP_4tree*)regdata->data[n];
1025 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1026 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1028 CALLRUNOPS(); /* Scalar context. */
1037 sv_setsv(save_scalar(replgv), ret);
1044 n = ARG(scan); /* which paren pair */
1045 reg_start_tmp[n] = locinput;
1050 n = ARG(scan); /* which paren pair */
1051 regstartp[n] = reg_start_tmp[n];
1052 regendp[n] = locinput;
1053 if (n > *reglastparen)
1057 n = ARG(scan); /* which paren pair */
1058 sw = (*reglastparen >= n && regendp[n] != NULL);
1062 next = NEXTOPER(NEXTOPER(scan));
1064 next = scan + ARG(scan);
1065 if (OP(next) == IFTHEN) /* Fake one. */
1066 next = NEXTOPER(NEXTOPER(next));
1074 CHECKPOINT cp = savestack_ix;
1076 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1080 cc.parenfloor = *reglastparen;
1082 cc.min = ARG1(scan);
1083 cc.max = ARG2(scan);
1084 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1088 reginput = locinput;
1089 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1097 * This is really hard to understand, because after we match
1098 * what we're trying to match, we must make sure the rest of
1099 * the RE is going to match for sure, and to do that we have
1100 * to go back UP the parse tree by recursing ever deeper. And
1101 * if it fails, we have to reset our parent's current state
1102 * that we can try again after backing off.
1105 CHECKPOINT cp, lastcp;
1107 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1109 n = cc->cur + 1; /* how many we know we matched */
1110 reginput = locinput;
1113 PerlIO_printf(Perl_debug_log,
1114 "%*s %ld out of %ld..%ld cc=%lx\n",
1115 REPORT_CODE_OFF+regindent*2, "",
1116 (long)n, (long)cc->min,
1117 (long)cc->max, (long)cc)
1120 /* If degenerate scan matches "", assume scan done. */
1122 if (locinput == cc->lastloc && n >= cc->min) {
1126 PerlIO_printf(Perl_debug_log,
1127 "%*s empty match detected, try continuation...\n",
1128 REPORT_CODE_OFF+regindent*2, "")
1130 if (regmatch(cc->next))
1133 PerlIO_printf(Perl_debug_log,
1135 REPORT_CODE_OFF+regindent*2, "")
1142 /* First just match a string of min scans. */
1146 cc->lastloc = locinput;
1147 if (regmatch(cc->scan))
1150 cc->lastloc = lastloc;
1152 PerlIO_printf(Perl_debug_log,
1154 REPORT_CODE_OFF+regindent*2, "")
1159 /* Prefer next over scan for minimal matching. */
1164 cp = regcppush(cc->parenfloor);
1166 if (regmatch(cc->next)) {
1168 sayYES; /* All done. */
1175 if (n >= cc->max) { /* Maximum greed exceeded? */
1176 if (dowarn && n >= REG_INFTY
1177 && !(reg_flags & RF_warned)) {
1178 reg_flags |= RF_warned;
1179 warn("Complex regular subexpression recursion "
1180 "limit (%d) exceeded", REG_INFTY - 1);
1186 PerlIO_printf(Perl_debug_log,
1187 "%*s trying longer...\n",
1188 REPORT_CODE_OFF+regindent*2, "")
1190 /* Try scanning more and see if it helps. */
1191 reginput = locinput;
1193 cc->lastloc = locinput;
1194 cp = regcppush(cc->parenfloor);
1196 if (regmatch(cc->scan)) {
1201 PerlIO_printf(Perl_debug_log,
1203 REPORT_CODE_OFF+regindent*2, "")
1208 cc->lastloc = lastloc;
1212 /* Prefer scan over next for maximal matching. */
1214 if (n < cc->max) { /* More greed allowed? */
1215 cp = regcppush(cc->parenfloor);
1217 cc->lastloc = locinput;
1219 if (regmatch(cc->scan)) {
1224 regcppop(); /* Restore some previous $<digit>s? */
1225 reginput = locinput;
1227 PerlIO_printf(Perl_debug_log,
1228 "%*s failed, try continuation...\n",
1229 REPORT_CODE_OFF+regindent*2, "")
1232 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1233 reg_flags |= RF_warned;
1234 warn("count exceeded %d", REG_INFTY - 1);
1237 /* Failed deeper matches of scan, so see if this one works. */
1240 if (regmatch(cc->next))
1243 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1244 REPORT_CODE_OFF+regindent*2, "")
1249 cc->lastloc = lastloc;
1254 next = scan + ARG(scan);
1257 inner = NEXTOPER(NEXTOPER(scan));
1260 inner = NEXTOPER(scan);
1265 if (OP(next) != c1) /* No choice. */
1266 next = inner; /* Avoid recursion. */
1268 int lastparen = *reglastparen;
1272 reginput = locinput;
1273 if (regmatch(inner))
1276 for (n = *reglastparen; n > lastparen; n--)
1281 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1285 inner = NEXTOPER(scan);
1286 if (c1 == BRANCHJ) {
1287 inner = NEXTOPER(inner);
1289 } while (scan != NULL && OP(scan) == c1);
1303 /* We suppose that the next guy does not need
1304 backtracking: in particular, it is of constant length,
1305 and has no parenths to influence future backrefs. */
1306 ln = ARG1(scan); /* min to match */
1307 n = ARG2(scan); /* max to match */
1308 paren = scan->flags;
1310 if (paren > regsize)
1312 if (paren > *reglastparen)
1313 *reglastparen = paren;
1315 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1317 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1318 reginput = locinput;
1321 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1323 if (ln && l == 0 && n >= ln
1324 /* In fact, this is tricky. If paren, then the
1325 fact that we did/didnot match may influence
1326 future execution. */
1327 && !(paren && ln == 0))
1329 locinput = reginput;
1330 if (regkind[(U8)OP(next)] == EXACT) {
1331 c1 = UCHARAT(OPERAND(next) + 1);
1332 if (OP(next) == EXACTF)
1334 else if (OP(next) == EXACTFL)
1335 c2 = fold_locale[c1];
1341 /* This may be improved if l == 0. */
1342 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1343 /* If it could work, try it. */
1345 UCHARAT(reginput) == c1 ||
1346 UCHARAT(reginput) == c2)
1350 regstartp[paren] = reginput - l;
1351 regendp[paren] = reginput;
1353 regendp[paren] = NULL;
1359 /* Couldn't or didn't -- move forward. */
1360 reginput = locinput;
1361 if (regrepeat_hard(scan, 1, &l)) {
1363 locinput = reginput;
1369 n = regrepeat_hard(scan, n, &l);
1370 if (n != 0 && l == 0
1371 /* In fact, this is tricky. If paren, then the
1372 fact that we did/didnot match may influence
1373 future execution. */
1374 && !(paren && ln == 0))
1376 locinput = reginput;
1378 PerlIO_printf(Perl_debug_log, "%*s matched %ld times,
1380 REPORT_CODE_OFF+regindent*2, "", n, l)
1383 if (regkind[(U8)OP(next)] == EXACT) {
1384 c1 = UCHARAT(OPERAND(next) + 1);
1385 if (OP(next) == EXACTF)
1387 else if (OP(next) == EXACTFL)
1388 c2 = fold_locale[c1];
1396 /* If it could work, try it. */
1398 UCHARAT(reginput) == c1 ||
1399 UCHARAT(reginput) == c2)
1402 PerlIO_printf(Perl_debug_log,
1403 "%*s trying tail with n=%ld...\n",
1404 REPORT_CODE_OFF+regindent*2, "", n)
1408 regstartp[paren] = reginput - l;
1409 regendp[paren] = reginput;
1411 regendp[paren] = NULL;
1417 /* Couldn't or didn't -- back up. */
1420 reginput = locinput;
1427 paren = scan->flags; /* Which paren to set */
1428 if (paren > regsize)
1430 if (paren > *reglastparen)
1431 *reglastparen = paren;
1432 ln = ARG1(scan); /* min to match */
1433 n = ARG2(scan); /* max to match */
1434 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1438 ln = ARG1(scan); /* min to match */
1439 n = ARG2(scan); /* max to match */
1440 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1445 scan = NEXTOPER(scan);
1451 scan = NEXTOPER(scan);
1455 * Lookahead to avoid useless match attempts
1456 * when we know what character comes next.
1458 if (regkind[(U8)OP(next)] == EXACT) {
1459 c1 = UCHARAT(OPERAND(next) + 1);
1460 if (OP(next) == EXACTF)
1462 else if (OP(next) == EXACTFL)
1463 c2 = fold_locale[c1];
1469 reginput = locinput;
1473 if (ln && regrepeat(scan, ln) < ln)
1476 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1477 /* If it could work, try it. */
1479 UCHARAT(reginput) == c1 ||
1480 UCHARAT(reginput) == c2)
1484 regstartp[paren] = reginput - 1;
1485 regendp[paren] = reginput;
1487 regendp[paren] = NULL;
1493 /* Couldn't or didn't -- move forward. */
1494 reginput = locinput + ln;
1495 if (regrepeat(scan, 1)) {
1497 reginput = locinput + ln;
1504 n = regrepeat(scan, n);
1505 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1506 (!multiline || OP(next) == SEOL))
1507 ln = n; /* why back off? */
1511 /* If it could work, try it. */
1513 UCHARAT(reginput) == c1 ||
1514 UCHARAT(reginput) == c2)
1518 regstartp[paren] = reginput - 1;
1519 regendp[paren] = reginput;
1521 regendp[paren] = NULL;
1527 /* Couldn't or didn't -- back up. */
1529 reginput = locinput + n;
1533 /* If it could work, try it. */
1535 UCHARAT(reginput) == c1 ||
1536 UCHARAT(reginput) == c2)
1542 /* Couldn't or didn't -- back up. */
1544 reginput = locinput + n;
1551 if (locinput < regtill)
1552 sayNO; /* Cannot match: too short. */
1555 reginput = locinput; /* put where regtry can find it */
1556 sayYES; /* Success! */
1562 if (locinput < bostr + scan->flags)
1567 if (locinput < bostr + scan->flags)
1570 reginput = locinput - scan->flags;
1571 inner = NEXTOPER(NEXTOPER(scan));
1572 if (regmatch(inner) != n) {
1586 if (OP(scan) == SUSPEND) {
1587 locinput = reginput;
1588 nextchr = UCHARAT(locinput);
1593 next = scan + ARG(scan);
1598 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1599 (unsigned long)scan, OP(scan));
1600 FAIL("regexp memory corruption");
1606 * We get here only if there's trouble -- normally "case END" is
1607 * the terminating point.
1609 FAIL("corrupted regexp pointers");
1627 - regrepeat - repeatedly match something simple, report how many
1630 * [This routine now assumes that it will only match on things of length 1.
1631 * That was true before, but now we assume scan - reginput is the count,
1632 * rather than incrementing count on every character.]
1635 regrepeat(regnode *p, I32 max)
1637 register char *scan;
1638 register char *opnd;
1640 register char *loceol = regeol;
1643 if (max != REG_INFTY && max < loceol - scan)
1644 loceol = scan + max;
1645 opnd = (char *) OPERAND(p);
1648 while (scan < loceol && *scan != '\n')
1654 case EXACT: /* length of string is 1 */
1655 c = UCHARAT(++opnd);
1656 while (scan < loceol && UCHARAT(scan) == c)
1659 case EXACTF: /* length of string is 1 */
1660 c = UCHARAT(++opnd);
1661 while (scan < loceol &&
1662 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1665 case EXACTFL: /* length of string is 1 */
1666 reg_flags |= RF_tainted;
1667 c = UCHARAT(++opnd);
1668 while (scan < loceol &&
1669 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1673 while (scan < loceol && REGINCLASS(opnd, *scan))
1677 while (scan < loceol && isALNUM(*scan))
1681 reg_flags |= RF_tainted;
1682 while (scan < loceol && isALNUM_LC(*scan))
1686 while (scan < loceol && !isALNUM(*scan))
1690 reg_flags |= RF_tainted;
1691 while (scan < loceol && !isALNUM_LC(*scan))
1695 while (scan < loceol && isSPACE(*scan))
1699 reg_flags |= RF_tainted;
1700 while (scan < loceol && isSPACE_LC(*scan))
1704 while (scan < loceol && !isSPACE(*scan))
1708 reg_flags |= RF_tainted;
1709 while (scan < loceol && !isSPACE_LC(*scan))
1713 while (scan < loceol && isDIGIT(*scan))
1717 while (scan < loceol && !isDIGIT(*scan))
1720 default: /* Called on something of 0 width. */
1721 break; /* So match right here or not at all. */
1724 c = scan - reginput;
1729 SV *prop = sv_newmortal();
1732 PerlIO_printf(Perl_debug_log,
1733 "%*s %s can match %ld times out of %ld...\n",
1734 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1741 - regrepeat_hard - repeatedly match something, report total lenth and length
1743 * The repeater is supposed to have constant length.
1747 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1749 register char *scan;
1750 register char *start;
1751 register char *loceol = regeol;
1755 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1757 *lp = l = reginput - start;
1758 if (max != REG_INFTY && l*max < loceol - scan)
1759 loceol = scan + l*max;
1765 if (reginput < loceol)
1770 return (scan - start)/l;
1774 - regclass - determine if a character falls into a character class
1778 reginclass(register char *p, register I32 c)
1784 if (ANYOF_TEST(p, c))
1786 else if (flags & ANYOF_FOLD) {
1788 if (flags & ANYOF_LOCALE) {
1789 reg_flags |= RF_tainted;
1790 cf = fold_locale[c];
1794 if (ANYOF_TEST(p, cf))
1798 if (!match && (flags & ANYOF_ISA)) {
1799 reg_flags |= RF_tainted;
1801 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1802 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1803 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1804 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1810 return (flags & ANYOF_INVERT) ? !match : match;