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 # define Perl_regexec_flags my_regexec
24 # define Perl_regdump my_regdump
25 # define Perl_regprop my_regprop
30 * pregcomp and pregexec -- regsub and regerror are not used in perl
32 * Copyright (c) 1986 by University of Toronto.
33 * Written by Henry Spencer. Not derived from licensed software.
35 * Permission is granted to anyone to use this software for any
36 * purpose on any computer system, and to redistribute it freely,
37 * subject to the following restrictions:
39 * 1. The author is not responsible for the consequences of use of
40 * this software, no matter how awful, even if they arise
43 * 2. The origin of this software must not be misrepresented, either
44 * by explicit claim or by omission.
46 * 3. Altered versions must be plainly marked as such, and must not
47 * be misrepresented as being the original software.
49 **** Alterations to Henry's code are...
51 **** Copyright (c) 1991-1997, Larry Wall
53 **** You may distribute under the terms of either the GNU General Public
54 **** License or the Artistic License, as specified in the README file.
56 * Beware that some of this code is subtly aware of the way operator
57 * precedence is structured in regular expressions. Serious changes in
58 * regular-expression syntax might require a total rethink.
64 #define RF_tainted 1 /* tainted information used? */
65 #define RF_warned 2 /* warned about big count? */
66 #define RF_evaled 4 /* Did an EVAL with setting? */
68 #define RS_init 1 /* eval environment created */
69 #define RS_set 2 /* replsv value is set */
76 typedef I32 CHECKPOINT;
82 static I32 regmatch _((regnode *prog));
83 static I32 regrepeat _((regnode *p, I32 max));
84 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
85 static I32 regtry _((regexp *prog, char *startpos));
87 static bool reginclass _((char *p, I32 c));
88 static CHECKPOINT regcppush _((I32 parenfloor));
89 static char * regcppop _((void));
91 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
94 regcppush(I32 parenfloor)
97 int retval = savestack_ix;
98 int i = (regsize - parenfloor) * 4;
102 for (p = regsize; p > parenfloor; p--) {
103 SSPUSHPTR(regendp[p]);
104 SSPUSHPTR(regstartp[p]);
105 SSPUSHPTR(reg_start_tmp[p]);
109 SSPUSHINT(*reglastparen);
112 SSPUSHINT(SAVEt_REGCONTEXT);
116 /* These are needed since we do not localize EVAL nodes: */
117 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
118 # 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)
128 assert(i == SAVEt_REGCONTEXT);
130 input = (char *) SSPOPPTR;
131 *reglastparen = SSPOPINT;
133 for (i -= 3; i > 0; i -= 4) {
134 paren = (U32)SSPOPINT;
135 reg_start_tmp[paren] = (char *) SSPOPPTR;
136 regstartp[paren] = (char *) SSPOPPTR;
137 tmps = (char*)SSPOPPTR;
138 if (paren <= *reglastparen)
139 regendp[paren] = tmps;
141 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
142 paren, regstartp[paren] - regbol,
143 reg_start_tmp[paren] - regbol,
144 regendp[paren] - regbol,
145 (paren > *reglastparen ? "(no)" : ""));
149 if (*reglastparen + 1 <= regnpar) {
150 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
151 *reglastparen + 1, regnpar);
154 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
156 regstartp[paren] = Nullch;
157 regendp[paren] = Nullch;
162 #define regcpblow(cp) LEAVE_SCOPE(cp)
165 * pregexec and friends
169 - pregexec - match a regexp against a string
172 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
173 /* strend: pointer to null at end of string */
174 /* strbeg: real beginning of string */
175 /* minend: end of match must be >=minend after stringarg. */
176 /* nosave: For optimizations. */
179 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
180 nosave ? 0 : REXEC_COPY_STR);
184 - regexec_flags - match a regexp against a string
187 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
188 /* strend: pointer to null at end of string */
189 /* strbeg: real beginning of string */
190 /* minend: end of match must be >=minend after stringarg. */
191 /* data: May be used for some additional optimizations. */
192 /* nosave: For optimizations. */
196 register char *startpos = stringarg;
198 I32 minlen; /* must match at least this many chars */
199 I32 dontbother = 0; /* how many characters not to try at end */
201 I32 start_shift = 0; /* Offset of the start to find
203 I32 end_shift = 0; /* Same for the end. */
204 I32 scream_pos = -1; /* Internal iterator of scream. */
206 SV* oreplsv = GvSV(replgv);
212 regprecomp = prog->precomp; /* Needed for error messages. */
214 regnarrate = debug & 512;
215 regprogram = prog->program;
219 if (prog == NULL || startpos == NULL) {
220 croak("NULL regexp parameter");
224 minlen = prog->minlen;
225 if (strend - startpos < minlen) goto phooey;
227 if (startpos == strbeg) /* is ^ valid at stringarg? */
230 regprev = stringarg[-1];
231 if (!multiline && regprev == '\n')
232 regprev = '\0'; /* force ^ to NOT match */
235 /* Check validity of program. */
236 if (UCHARAT(prog->program) != MAGIC) {
237 FAIL("corrupted regexp program");
240 regnpar = prog->nparens;
244 /* If there is a "must appear" string, look for it. */
246 if (!(flags & REXEC_CHECKED)
247 && prog->check_substr != Nullsv &&
248 !(prog->reganch & ROPT_ANCH_GPOS) &&
249 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
250 || (multiline && prog->check_substr == prog->anchored_substr)) )
252 start_shift = prog->check_offset_min;
253 /* Should be nonnegative! */
254 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
256 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
257 s = screaminstr(screamer, prog->check_substr,
258 start_shift + (stringarg - strbeg),
259 end_shift, &scream_pos, 0);
265 s = fbm_instr((unsigned char*)s + start_shift,
266 (unsigned char*)strend - end_shift,
267 prog->check_substr, 0);
269 ++BmUSEFUL(prog->check_substr); /* hooray */
270 goto phooey; /* not present */
271 } else if ((s - stringarg) > prog->check_offset_max) {
272 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
273 s -= prog->check_offset_max;
274 } else if (!prog->naughty
275 && --BmUSEFUL(prog->check_substr) < 0
276 && prog->check_substr == prog->float_substr) { /* boo */
277 SvREFCNT_dec(prog->check_substr);
278 prog->check_substr = Nullsv; /* disable */
279 prog->float_substr = Nullsv; /* clear */
284 /* Mark beginning of line for ^ and lookbehind. */
288 /* Mark end of line for $ (and such) */
291 /* see how far we have to get to not match where we matched before */
292 regtill = startpos+minend;
295 PerlIO_printf(Perl_debug_log,
296 "Matching `%.60s%s' against `%.*s%s'\n",
298 (strlen(prog->precomp) > 60 ? "..." : ""),
299 (strend - startpos > 60 ? 60 : strend - startpos),
301 (strend - startpos > 60 ? "..." : ""))
304 /* Simplest case: anchored match need be tried only once. */
305 /* [unless only anchor is BOL and multiline is set] */
306 if (prog->reganch & ROPT_ANCH) {
307 if (regtry(prog, startpos))
309 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
310 (multiline || (prog->reganch & ROPT_IMPLICIT)
311 || (prog->reganch & ROPT_ANCH_MBOL)))
314 dontbother = minlen - 1;
315 strend -= dontbother;
316 /* for multiline we only have to try after newlines */
321 if (s < strend && regtry(prog, s))
329 /* Messy cases: unanchored match. */
330 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
331 /* we have /x+whatever/ */
332 /* it must be a one character string */
333 char ch = SvPVX(prog->anchored_substr)[0];
336 if (regtry(prog, s)) goto got_it;
338 while (s < strend && *s == ch)
345 else if (prog->anchored_substr != Nullsv
346 || (prog->float_substr != Nullsv
347 && prog->float_max_offset < strend - s)) {
348 SV *must = prog->anchored_substr
349 ? prog->anchored_substr : prog->float_substr;
351 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
353 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
354 I32 delta = back_max - back_min;
355 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
356 char *last1 = s - 1; /* Last position checked before */
358 /* XXXX check_substr already used to find `s', can optimize if
359 check_substr==must. */
361 dontbother = end_shift;
362 strend -= dontbother;
363 while ( (s <= last) &&
365 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
366 end_shift, &scream_pos, 0))
367 : (s = fbm_instr((unsigned char*)s + back_min,
368 (unsigned char*)strend, must, 0))) ) {
369 if (s - back_max > last1) {
370 last1 = s - back_min;
375 last1 = s - back_min;
385 } else if (c = prog->regstclass) {
386 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
390 dontbother = minlen - 1;
391 strend -= dontbother; /* don't bother with what can't match */
393 /* We know what class it must start with. */
396 Class = (char *) OPERAND(c);
398 if (REGINCLASS(Class, *s)) {
399 if (tmp && regtry(prog, s))
410 reg_flags |= RF_tainted;
414 dontbother++,strend--;
415 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
416 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
418 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
425 if ((minlen || tmp) && regtry(prog,s))
429 reg_flags |= RF_tainted;
433 dontbother++,strend--;
434 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
435 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
437 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
439 else if (regtry(prog, s))
443 if ((minlen || !tmp) && regtry(prog,s))
449 if (tmp && regtry(prog, s))
460 reg_flags |= RF_tainted;
462 if (isALNUM_LC(*s)) {
463 if (tmp && regtry(prog, s))
476 if (tmp && regtry(prog, s))
487 reg_flags |= RF_tainted;
489 if (!isALNUM_LC(*s)) {
490 if (tmp && regtry(prog, s))
503 if (tmp && regtry(prog, s))
514 reg_flags |= RF_tainted;
516 if (isSPACE_LC(*s)) {
517 if (tmp && regtry(prog, s))
530 if (tmp && regtry(prog, s))
541 reg_flags |= RF_tainted;
543 if (!isSPACE_LC(*s)) {
544 if (tmp && regtry(prog, s))
557 if (tmp && regtry(prog, s))
570 if (tmp && regtry(prog, s))
584 if (prog->float_substr != Nullsv) { /* Trim the end. */
586 I32 oldpos = scream_pos;
589 last = screaminstr(screamer, prog->float_substr, s - strbeg,
590 end_shift, &scream_pos, 1); /* last one */
592 last = scream_olds; /* Only one occurence. */
596 char *little = SvPV(prog->float_substr, len);
597 last = rninstr(s, strend, little, little + len);
599 if (last == NULL) goto phooey; /* Should not happen! */
600 dontbother = strend - last - 1;
602 if (minlen && (dontbother < minlen))
603 dontbother = minlen - 1;
604 strend -= dontbother;
605 /* We don't know much -- general case. */
609 } while (s++ < strend);
616 strend += dontbother; /* uncheat */
617 prog->subbeg = strbeg;
618 prog->subend = strend;
619 RX_MATCH_TAINTED_set(prog, reg_flags & RF_tainted);
621 /* make sure $`, $&, $', and $digit will work later */
622 if (strbeg != prog->subbase) { /* second+ //g match. */
623 if (!(flags & REXEC_COPY_STR)) {
625 Safefree(prog->subbase);
626 prog->subbase = Nullch;
630 I32 i = strend - startpos + (stringarg - strbeg);
631 s = savepvn(strbeg, i);
632 Safefree(prog->subbase);
634 prog->subbeg = prog->subbase;
635 prog->subend = prog->subbase + i;
636 s = prog->subbase + (stringarg - strbeg);
637 for (i = 0; i <= prog->nparens; i++) {
639 prog->startp[i] = s + (prog->startp[i] - startpos);
640 prog->endp[i] = s + (prog->endp[i] - startpos);
645 /* Preserve the current value of $^R */
646 if (oreplsv != GvSV(replgv)) {
647 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
648 restored, the value remains
658 - regtry - try match at specific point
660 STATIC I32 /* 0 failure, 1 success */
661 regtry(regexp *prog, char *startpos)
669 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
670 reg_eval_set = RS_init;
672 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
674 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
675 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
676 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
678 /* Apparently this is not needed, judging by wantarray. */
679 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
680 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
683 regstartp = prog->startp;
684 regendp = prog->endp;
685 reglastparen = &prog->lastparen;
688 if (reg_start_tmpl <= prog->nparens) {
689 reg_start_tmpl = prog->nparens*3/2 + 3;
691 Renew(reg_start_tmp, reg_start_tmpl, char*);
693 New(22,reg_start_tmp, reg_start_tmpl, char*);
698 regdata = prog->data;
700 for (i = prog->nparens; i >= 0; i--) {
706 if (regmatch(prog->program + 1)) {
707 prog->startp[0] = startpos;
708 prog->endp[0] = reginput;
716 - regmatch - main matching routine
718 * Conceptually the strategy is simple: check to see whether the current
719 * node matches, call self recursively to see whether the rest matches,
720 * and then act accordingly. In practice we make some effort to avoid
721 * recursion, in particular by going through "ordinary" nodes (that don't
722 * need to know whether the rest of the match failed) by a loop instead of
725 /* [lwall] I've hoisted the register declarations to the outer block in order to
726 * maybe save a little bit of pushing and popping on the stack. It also takes
727 * advantage of machines that use a register save mask on subroutine entry.
729 STATIC I32 /* 0 failure, 1 success */
730 regmatch(regnode *prog)
733 register regnode *scan; /* Current node. */
734 regnode *next; /* Next node. */
735 regnode *inner; /* Next node in internal branch. */
736 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
737 register I32 n; /* no or next */
738 register I32 ln; /* len or last */
739 register char *s; /* operand or save */
740 register char *locinput = reginput;
741 register I32 c1, c2, paren; /* case fold search, parenth */
742 int minmod = 0, sw = 0, logical = 0;
747 nextchr = UCHARAT(locinput);
749 while (scan != NULL) {
750 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
752 # define sayYES goto yes
753 # define sayNO goto no
754 # define saySAME(x) if (x) goto yes; else goto no
755 # define REPORT_CODE_OFF 24
757 # define sayYES return 1
758 # define sayNO return 0
759 # define saySAME(x) return x
762 SV *prop = sv_newmortal();
763 int docolor = *colors[0];
764 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
765 int l = (regeol - locinput > taill ? taill : regeol - locinput);
766 int pref_len = (locinput - bostr > (5 + taill) - l
767 ? (5 + taill) - l : locinput - bostr);
769 if (l + pref_len < (5 + taill) && l < regeol - locinput)
770 l = ( regeol - locinput > (5 + taill) - pref_len
771 ? (5 + taill) - pref_len : regeol - locinput);
773 PerlIO_printf(Perl_debug_log,
774 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
776 colors[2], pref_len, locinput - pref_len, colors[3],
777 (docolor ? "" : "> <"),
778 colors[0], l, locinput, colors[1],
779 15 - l - pref_len + 1,
781 regindent*2, "", scan - regprogram,
785 next = scan + NEXT_OFF(scan);
791 if (locinput == bostr
794 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
796 /* regtill = regbol; */
801 if (locinput == bostr
803 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
809 if (locinput == regbol && regprev == '\n')
813 if (locinput == regbol)
823 if ((nextchr || locinput < regeol) && nextchr != '\n')
828 if ((nextchr || locinput < regeol) && nextchr != '\n')
830 if (regeol - locinput > 1)
834 if (regeol != locinput)
838 if (!nextchr && locinput >= regeol)
840 nextchr = UCHARAT(++locinput);
843 if (!nextchr && locinput >= regeol || nextchr == '\n')
845 nextchr = UCHARAT(++locinput);
848 s = (char *) OPERAND(scan);
850 /* Inline the first character, for speed. */
851 if (UCHARAT(s) != nextchr)
853 if (regeol - locinput < ln)
855 if (ln > 1 && memNE(s, locinput, ln))
858 nextchr = UCHARAT(locinput);
861 reg_flags |= RF_tainted;
864 s = (char *) OPERAND(scan);
866 /* Inline the first character, for speed. */
867 if (UCHARAT(s) != nextchr &&
868 UCHARAT(s) != ((OP(scan) == EXACTF)
869 ? fold : fold_locale)[nextchr])
871 if (regeol - locinput < ln)
873 if (ln > 1 && (OP(scan) == EXACTF
874 ? ibcmp(s, locinput, ln)
875 : ibcmp_locale(s, locinput, ln)))
878 nextchr = UCHARAT(locinput);
881 s = (char *) OPERAND(scan);
883 nextchr = UCHARAT(locinput);
884 if (!REGINCLASS(s, nextchr))
886 if (!nextchr && locinput >= regeol)
888 nextchr = UCHARAT(++locinput);
891 reg_flags |= RF_tainted;
896 if (!(OP(scan) == ALNUM
897 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
899 nextchr = UCHARAT(++locinput);
902 reg_flags |= RF_tainted;
905 if (!nextchr && locinput >= regeol)
907 if (OP(scan) == NALNUM
908 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
910 nextchr = UCHARAT(++locinput);
914 reg_flags |= RF_tainted;
918 /* was last char in word? */
919 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
920 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
922 n = isALNUM(nextchr);
926 n = isALNUM_LC(nextchr);
928 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
932 reg_flags |= RF_tainted;
935 if (!nextchr && locinput >= regeol)
937 if (!(OP(scan) == SPACE
938 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
940 nextchr = UCHARAT(++locinput);
943 reg_flags |= RF_tainted;
948 if (OP(scan) == SPACE
949 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
951 nextchr = UCHARAT(++locinput);
954 if (!isDIGIT(nextchr))
956 nextchr = UCHARAT(++locinput);
959 if (!nextchr && locinput >= regeol)
961 if (isDIGIT(nextchr))
963 nextchr = UCHARAT(++locinput);
966 reg_flags |= RF_tainted;
970 n = ARG(scan); /* which paren pair */
972 if (*reglastparen < n || !s)
973 sayNO; /* Do not match unless seen CLOSEn. */
976 /* Inline the first character, for speed. */
977 if (UCHARAT(s) != nextchr &&
979 (UCHARAT(s) != ((OP(scan) == REFF
980 ? fold : fold_locale)[nextchr]))))
983 if (locinput + ln > regeol)
985 if (ln > 1 && (OP(scan) == REF
986 ? memNE(s, locinput, ln)
988 ? ibcmp(s, locinput, ln)
989 : ibcmp_locale(s, locinput, ln))))
992 nextchr = UCHARAT(locinput);
1004 COP *ocurcop = curcop;
1005 SV **ocurpad = curpad;
1009 op = (OP_4tree*)regdata->data[n];
1010 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1011 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1013 CALLRUNOPS(); /* Scalar context. */
1022 sv_setsv(save_scalar(replgv), ret);
1029 n = ARG(scan); /* which paren pair */
1030 reg_start_tmp[n] = locinput;
1035 n = ARG(scan); /* which paren pair */
1036 regstartp[n] = reg_start_tmp[n];
1037 regendp[n] = locinput;
1038 if (n > *reglastparen)
1042 n = ARG(scan); /* which paren pair */
1043 sw = (*reglastparen >= n && regendp[n] != NULL);
1047 next = NEXTOPER(NEXTOPER(scan));
1049 next = scan + ARG(scan);
1050 if (OP(next) == IFTHEN) /* Fake one. */
1051 next = NEXTOPER(NEXTOPER(next));
1059 CHECKPOINT cp = savestack_ix;
1061 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1065 cc.parenfloor = *reglastparen;
1067 cc.min = ARG1(scan);
1068 cc.max = ARG2(scan);
1069 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1073 reginput = locinput;
1074 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1082 * This is really hard to understand, because after we match
1083 * what we're trying to match, we must make sure the rest of
1084 * the RE is going to match for sure, and to do that we have
1085 * to go back UP the parse tree by recursing ever deeper. And
1086 * if it fails, we have to reset our parent's current state
1087 * that we can try again after backing off.
1090 CHECKPOINT cp, lastcp;
1092 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1094 n = cc->cur + 1; /* how many we know we matched */
1095 reginput = locinput;
1098 PerlIO_printf(Perl_debug_log,
1099 "%*s %ld out of %ld..%ld cc=%lx\n",
1100 REPORT_CODE_OFF+regindent*2, "",
1101 (long)n, (long)cc->min,
1102 (long)cc->max, (long)cc)
1105 /* If degenerate scan matches "", assume scan done. */
1107 if (locinput == cc->lastloc && n >= cc->min) {
1111 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1113 if (regmatch(cc->next))
1116 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1123 /* First just match a string of min scans. */
1127 cc->lastloc = locinput;
1128 if (regmatch(cc->scan))
1131 cc->lastloc = lastloc;
1133 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1138 /* Prefer next over scan for minimal matching. */
1143 cp = regcppush(cc->parenfloor);
1145 if (regmatch(cc->next)) {
1147 sayYES; /* All done. */
1154 if (n >= cc->max) { /* Maximum greed exceeded? */
1155 if (dowarn && n >= REG_INFTY
1156 && !(reg_flags & RF_warned)) {
1157 reg_flags |= RF_warned;
1158 warn("count exceeded %d", REG_INFTY - 1);
1164 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1166 /* Try scanning more and see if it helps. */
1167 reginput = locinput;
1169 cc->lastloc = locinput;
1170 cp = regcppush(cc->parenfloor);
1172 if (regmatch(cc->scan)) {
1177 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1182 cc->lastloc = lastloc;
1186 /* Prefer scan over next for maximal matching. */
1188 if (n < cc->max) { /* More greed allowed? */
1189 cp = regcppush(cc->parenfloor);
1191 cc->lastloc = locinput;
1193 if (regmatch(cc->scan)) {
1198 regcppop(); /* Restore some previous $<digit>s? */
1199 reginput = locinput;
1201 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1204 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1205 reg_flags |= RF_warned;
1206 warn("count exceeded %d", REG_INFTY - 1);
1209 /* Failed deeper matches of scan, so see if this one works. */
1212 if (regmatch(cc->next))
1215 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1220 cc->lastloc = lastloc;
1225 next = scan + ARG(scan);
1228 inner = NEXTOPER(NEXTOPER(scan));
1231 inner = NEXTOPER(scan);
1236 if (OP(next) != c1) /* No choice. */
1237 next = inner; /* Avoid recursion. */
1239 int lastparen = *reglastparen;
1243 reginput = locinput;
1244 if (regmatch(inner))
1247 for (n = *reglastparen; n > lastparen; n--)
1252 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1256 inner = NEXTOPER(scan);
1257 if (c1 == BRANCHJ) {
1258 inner = NEXTOPER(inner);
1260 } while (scan != NULL && OP(scan) == c1);
1274 /* We suppose that the next guy does not need
1275 backtracking: in particular, it is of constant length,
1276 and has no parenths to influence future backrefs. */
1277 ln = ARG1(scan); /* min to match */
1278 n = ARG2(scan); /* max to match */
1279 paren = scan->flags;
1281 if (paren > regsize)
1283 if (paren > *reglastparen)
1284 *reglastparen = paren;
1286 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1288 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1289 reginput = locinput;
1292 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1294 if (ln && l == 0 && n >= ln
1295 /* In fact, this is tricky. If paren, then the
1296 fact that we did/didnot match may influence
1297 future execution. */
1298 && !(paren && ln == 0))
1300 locinput = reginput;
1301 if (regkind[(U8)OP(next)] == EXACT) {
1302 c1 = UCHARAT(OPERAND(next) + 1);
1303 if (OP(next) == EXACTF)
1305 else if (OP(next) == EXACTFL)
1306 c2 = fold_locale[c1];
1312 /* This may be improved if l == 0. */
1313 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1314 /* If it could work, try it. */
1316 UCHARAT(reginput) == c1 ||
1317 UCHARAT(reginput) == c2)
1321 regstartp[paren] = reginput - l;
1322 regendp[paren] = reginput;
1324 regendp[paren] = NULL;
1330 /* Couldn't or didn't -- move forward. */
1331 reginput = locinput;
1332 if (regrepeat_hard(scan, 1, &l)) {
1334 locinput = reginput;
1340 n = regrepeat_hard(scan, n, &l);
1341 if (n != 0 && l == 0
1342 /* In fact, this is tricky. If paren, then the
1343 fact that we did/didnot match may influence
1344 future execution. */
1345 && !(paren && ln == 0))
1347 locinput = reginput;
1349 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1352 if (regkind[(U8)OP(next)] == EXACT) {
1353 c1 = UCHARAT(OPERAND(next) + 1);
1354 if (OP(next) == EXACTF)
1356 else if (OP(next) == EXACTFL)
1357 c2 = fold_locale[c1];
1365 /* If it could work, try it. */
1367 UCHARAT(reginput) == c1 ||
1368 UCHARAT(reginput) == c2)
1371 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1375 regstartp[paren] = reginput - l;
1376 regendp[paren] = reginput;
1378 regendp[paren] = NULL;
1384 /* Couldn't or didn't -- back up. */
1387 reginput = locinput;
1394 paren = scan->flags; /* Which paren to set */
1395 if (paren > regsize)
1397 if (paren > *reglastparen)
1398 *reglastparen = paren;
1399 ln = ARG1(scan); /* min to match */
1400 n = ARG2(scan); /* max to match */
1401 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1405 ln = ARG1(scan); /* min to match */
1406 n = ARG2(scan); /* max to match */
1407 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1412 scan = NEXTOPER(scan);
1418 scan = NEXTOPER(scan);
1422 * Lookahead to avoid useless match attempts
1423 * when we know what character comes next.
1425 if (regkind[(U8)OP(next)] == EXACT) {
1426 c1 = UCHARAT(OPERAND(next) + 1);
1427 if (OP(next) == EXACTF)
1429 else if (OP(next) == EXACTFL)
1430 c2 = fold_locale[c1];
1436 reginput = locinput;
1440 if (ln && regrepeat(scan, ln) < ln)
1443 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1444 /* If it could work, try it. */
1446 UCHARAT(reginput) == c1 ||
1447 UCHARAT(reginput) == c2)
1451 regstartp[paren] = reginput - 1;
1452 regendp[paren] = reginput;
1454 regendp[paren] = NULL;
1460 /* Couldn't or didn't -- move forward. */
1461 reginput = locinput + ln;
1462 if (regrepeat(scan, 1)) {
1464 reginput = locinput + ln;
1471 n = regrepeat(scan, n);
1472 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1473 (!multiline || OP(next) == SEOL))
1474 ln = n; /* why back off? */
1478 /* If it could work, try it. */
1480 UCHARAT(reginput) == c1 ||
1481 UCHARAT(reginput) == c2)
1485 regstartp[paren] = reginput - 1;
1486 regendp[paren] = reginput;
1488 regendp[paren] = NULL;
1494 /* Couldn't or didn't -- back up. */
1496 reginput = locinput + n;
1500 /* If it could work, try it. */
1502 UCHARAT(reginput) == c1 ||
1503 UCHARAT(reginput) == c2)
1509 /* Couldn't or didn't -- back up. */
1511 reginput = locinput + n;
1518 if (locinput < regtill)
1519 sayNO; /* Cannot match: too short. */
1522 reginput = locinput; /* put where regtry can find it */
1523 sayYES; /* Success! */
1529 if (locinput < bostr + scan->flags)
1534 if (locinput < bostr + scan->flags)
1537 reginput = locinput - scan->flags;
1538 inner = NEXTOPER(NEXTOPER(scan));
1539 if (regmatch(inner) != n) {
1553 if (OP(scan) == SUSPEND) {
1554 locinput = reginput;
1555 nextchr = UCHARAT(locinput);
1560 next = scan + ARG(scan);
1565 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1566 (unsigned long)scan, OP(scan));
1567 FAIL("regexp memory corruption");
1573 * We get here only if there's trouble -- normally "case END" is
1574 * the terminating point.
1576 FAIL("corrupted regexp pointers");
1594 - regrepeat - repeatedly match something simple, report how many
1597 * [This routine now assumes that it will only match on things of length 1.
1598 * That was true before, but now we assume scan - reginput is the count,
1599 * rather than incrementing count on every character.]
1602 regrepeat(regnode *p, I32 max)
1604 register char *scan;
1605 register char *opnd;
1607 register char *loceol = regeol;
1610 if (max != REG_INFTY && max < loceol - scan)
1611 loceol = scan + max;
1612 opnd = (char *) OPERAND(p);
1615 while (scan < loceol && *scan != '\n')
1621 case EXACT: /* length of string is 1 */
1622 c = UCHARAT(++opnd);
1623 while (scan < loceol && UCHARAT(scan) == c)
1626 case EXACTF: /* length of string is 1 */
1627 c = UCHARAT(++opnd);
1628 while (scan < loceol &&
1629 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1632 case EXACTFL: /* length of string is 1 */
1633 reg_flags |= RF_tainted;
1634 c = UCHARAT(++opnd);
1635 while (scan < loceol &&
1636 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1640 while (scan < loceol && REGINCLASS(opnd, *scan))
1644 while (scan < loceol && isALNUM(*scan))
1648 reg_flags |= RF_tainted;
1649 while (scan < loceol && isALNUM_LC(*scan))
1653 while (scan < loceol && !isALNUM(*scan))
1657 reg_flags |= RF_tainted;
1658 while (scan < loceol && !isALNUM_LC(*scan))
1662 while (scan < loceol && isSPACE(*scan))
1666 reg_flags |= RF_tainted;
1667 while (scan < loceol && isSPACE_LC(*scan))
1671 while (scan < loceol && !isSPACE(*scan))
1675 reg_flags |= RF_tainted;
1676 while (scan < loceol && !isSPACE_LC(*scan))
1680 while (scan < loceol && isDIGIT(*scan))
1684 while (scan < loceol && !isDIGIT(*scan))
1687 default: /* Called on something of 0 width. */
1688 break; /* So match right here or not at all. */
1691 c = scan - reginput;
1696 SV *prop = sv_newmortal();
1699 PerlIO_printf(Perl_debug_log,
1700 "%*s %s can match %ld times out of %ld...\n",
1701 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1708 - regrepeat_hard - repeatedly match something, report total lenth and length
1710 * The repeater is supposed to have constant length.
1714 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1716 register char *scan;
1717 register char *start;
1718 register char *loceol = regeol;
1722 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1724 *lp = l = reginput - start;
1725 if (max != REG_INFTY && l*max < loceol - scan)
1726 loceol = scan + l*max;
1732 if (reginput < loceol)
1737 return (scan - start)/l;
1741 - regclass - determine if a character falls into a character class
1745 reginclass(register char *p, register I32 c)
1751 if (ANYOF_TEST(p, c))
1753 else if (flags & ANYOF_FOLD) {
1755 if (flags & ANYOF_LOCALE) {
1756 reg_flags |= RF_tainted;
1757 cf = fold_locale[c];
1761 if (ANYOF_TEST(p, cf))
1765 if (!match && (flags & ANYOF_ISA)) {
1766 reg_flags |= RF_tainted;
1768 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1769 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1770 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1771 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1777 return (flags & ANYOF_INVERT) ? !match : match;