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. */
210 register char *startpos = stringarg;
212 I32 minlen; /* must match at least this many chars */
213 I32 dontbother = 0; /* how many characters not to try at end */
215 I32 start_shift = 0; /* Offset of the start to find
217 I32 end_shift = 0; /* Same for the end. */
218 I32 scream_pos = -1; /* Internal iterator of scream. */
220 SV* oreplsv = GvSV(replgv);
226 regprecomp = prog->precomp; /* Needed for error messages. */
228 regnarrate = debug & 512;
229 regprogram = prog->program;
233 if (prog == NULL || startpos == NULL) {
234 croak("NULL regexp parameter");
238 minlen = prog->minlen;
239 if (strend - startpos < minlen) goto phooey;
241 if (startpos == strbeg) /* is ^ valid at stringarg? */
244 regprev = stringarg[-1];
245 if (!multiline && regprev == '\n')
246 regprev = '\0'; /* force ^ to NOT match */
249 /* Check validity of program. */
250 if (UCHARAT(prog->program) != MAGIC) {
251 FAIL("corrupted regexp program");
254 regnpar = prog->nparens;
258 /* If there is a "must appear" string, look for it. */
260 if (!(flags & REXEC_CHECKED)
261 && prog->check_substr != Nullsv &&
262 !(prog->reganch & ROPT_ANCH_GPOS) &&
263 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
264 || (multiline && prog->check_substr == prog->anchored_substr)) )
266 start_shift = prog->check_offset_min;
267 /* Should be nonnegative! */
268 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
270 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
271 s = screaminstr(screamer, prog->check_substr,
272 start_shift + (stringarg - strbeg),
273 end_shift, &scream_pos, 0);
279 s = fbm_instr((unsigned char*)s + start_shift,
280 (unsigned char*)strend - end_shift,
281 prog->check_substr, 0);
283 ++BmUSEFUL(prog->check_substr); /* hooray */
284 goto phooey; /* not present */
285 } else if ((s - stringarg) > prog->check_offset_max) {
286 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
287 s -= prog->check_offset_max;
288 } else if (!prog->naughty
289 && --BmUSEFUL(prog->check_substr) < 0
290 && prog->check_substr == prog->float_substr) { /* boo */
291 SvREFCNT_dec(prog->check_substr);
292 prog->check_substr = Nullsv; /* disable */
293 prog->float_substr = Nullsv; /* clear */
298 /* Mark beginning of line for ^ and lookbehind. */
302 /* Mark end of line for $ (and such) */
305 /* see how far we have to get to not match where we matched before */
306 regtill = startpos+minend;
309 PerlIO_printf(Perl_debug_log,
310 "Matching `%.60s%s' against `%.*s%s'\n",
312 (strlen(prog->precomp) > 60 ? "..." : ""),
313 (strend - startpos > 60 ? 60 : strend - startpos),
315 (strend - startpos > 60 ? "..." : ""))
318 /* Simplest case: anchored match need be tried only once. */
319 /* [unless only anchor is BOL and multiline is set] */
320 if (prog->reganch & ROPT_ANCH) {
321 if (regtry(prog, startpos))
323 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
324 (multiline || (prog->reganch & ROPT_IMPLICIT)
325 || (prog->reganch & ROPT_ANCH_MBOL)))
328 dontbother = minlen - 1;
329 strend -= dontbother;
330 /* for multiline we only have to try after newlines */
335 if (s < strend && regtry(prog, s))
343 /* Messy cases: unanchored match. */
344 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
345 /* we have /x+whatever/ */
346 /* it must be a one character string */
347 char ch = SvPVX(prog->anchored_substr)[0];
350 if (regtry(prog, s)) goto got_it;
352 while (s < strend && *s == ch)
359 else if (prog->anchored_substr != Nullsv
360 || (prog->float_substr != Nullsv
361 && prog->float_max_offset < strend - s)) {
362 SV *must = prog->anchored_substr
363 ? prog->anchored_substr : prog->float_substr;
365 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
367 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
368 I32 delta = back_max - back_min;
369 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
370 char *last1 = s - 1; /* Last position checked before */
372 /* XXXX check_substr already used to find `s', can optimize if
373 check_substr==must. */
375 dontbother = end_shift;
376 strend -= dontbother;
377 while ( (s <= last) &&
379 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
380 end_shift, &scream_pos, 0))
381 : (s = fbm_instr((unsigned char*)s + back_min,
382 (unsigned char*)strend, must, 0))) ) {
383 if (s - back_max > last1) {
384 last1 = s - back_min;
389 last1 = s - back_min;
399 } else if (c = prog->regstclass) {
400 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
404 dontbother = minlen - 1;
405 strend -= dontbother; /* don't bother with what can't match */
407 /* We know what class it must start with. */
410 Class = (char *) OPERAND(c);
412 if (REGINCLASS(Class, *s)) {
413 if (tmp && regtry(prog, s))
424 reg_flags |= RF_tainted;
428 dontbother++,strend--;
429 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
430 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
432 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
439 if ((minlen || tmp) && regtry(prog,s))
443 reg_flags |= RF_tainted;
447 dontbother++,strend--;
448 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
449 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
451 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
453 else if (regtry(prog, s))
457 if ((minlen || !tmp) && regtry(prog,s))
463 if (tmp && regtry(prog, s))
474 reg_flags |= RF_tainted;
476 if (isALNUM_LC(*s)) {
477 if (tmp && regtry(prog, s))
490 if (tmp && regtry(prog, s))
501 reg_flags |= RF_tainted;
503 if (!isALNUM_LC(*s)) {
504 if (tmp && regtry(prog, s))
517 if (tmp && regtry(prog, s))
528 reg_flags |= RF_tainted;
530 if (isSPACE_LC(*s)) {
531 if (tmp && regtry(prog, s))
544 if (tmp && regtry(prog, s))
555 reg_flags |= RF_tainted;
557 if (!isSPACE_LC(*s)) {
558 if (tmp && regtry(prog, s))
571 if (tmp && regtry(prog, s))
584 if (tmp && regtry(prog, s))
598 if (prog->float_substr != Nullsv) { /* Trim the end. */
600 I32 oldpos = scream_pos;
603 last = screaminstr(screamer, prog->float_substr, s - strbeg,
604 end_shift, &scream_pos, 1); /* last one */
606 last = scream_olds; /* Only one occurence. */
610 char *little = SvPV(prog->float_substr, len);
611 last = rninstr(s, strend, little, little + len);
613 if (last == NULL) goto phooey; /* Should not happen! */
614 dontbother = strend - last - 1;
616 if (minlen && (dontbother < minlen))
617 dontbother = minlen - 1;
618 strend -= dontbother;
619 /* We don't know much -- general case. */
623 } while (s++ < strend);
630 strend += dontbother; /* uncheat */
631 prog->subbeg = strbeg;
632 prog->subend = strend;
633 RX_MATCH_TAINTED_set(prog, reg_flags & RF_tainted);
635 /* make sure $`, $&, $', and $digit will work later */
636 if (strbeg != prog->subbase) { /* second+ //g match. */
637 if (!(flags & REXEC_COPY_STR)) {
639 Safefree(prog->subbase);
640 prog->subbase = Nullch;
644 I32 i = strend - startpos + (stringarg - strbeg);
645 s = savepvn(strbeg, i);
646 Safefree(prog->subbase);
648 prog->subbeg = prog->subbase;
649 prog->subend = prog->subbase + i;
650 s = prog->subbase + (stringarg - strbeg);
651 for (i = 0; i <= prog->nparens; i++) {
653 prog->startp[i] = s + (prog->startp[i] - startpos);
654 prog->endp[i] = s + (prog->endp[i] - startpos);
659 /* Preserve the current value of $^R */
660 if (oreplsv != GvSV(replgv)) {
661 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
662 restored, the value remains
672 - regtry - try match at specific point
674 STATIC I32 /* 0 failure, 1 success */
675 regtry(regexp *prog, char *startpos)
683 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
684 reg_eval_set = RS_init;
686 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
687 stack_sp - stack_base);
689 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
690 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
691 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
693 /* Apparently this is not needed, judging by wantarray. */
694 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
695 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
698 regstartp = prog->startp;
699 regendp = prog->endp;
700 reglastparen = &prog->lastparen;
703 if (reg_start_tmpl <= prog->nparens) {
704 reg_start_tmpl = prog->nparens*3/2 + 3;
706 Renew(reg_start_tmp, reg_start_tmpl, char*);
708 New(22,reg_start_tmp, reg_start_tmpl, char*);
713 regdata = prog->data;
715 for (i = prog->nparens; i >= 0; i--) {
721 if (regmatch(prog->program + 1)) {
722 prog->startp[0] = startpos;
723 prog->endp[0] = reginput;
731 - regmatch - main matching routine
733 * Conceptually the strategy is simple: check to see whether the current
734 * node matches, call self recursively to see whether the rest matches,
735 * and then act accordingly. In practice we make some effort to avoid
736 * recursion, in particular by going through "ordinary" nodes (that don't
737 * need to know whether the rest of the match failed) by a loop instead of
740 /* [lwall] I've hoisted the register declarations to the outer block in order to
741 * maybe save a little bit of pushing and popping on the stack. It also takes
742 * advantage of machines that use a register save mask on subroutine entry.
744 STATIC I32 /* 0 failure, 1 success */
745 regmatch(regnode *prog)
748 register regnode *scan; /* Current node. */
749 regnode *next; /* Next node. */
750 regnode *inner; /* Next node in internal branch. */
751 register I32 nextchr; /* renamed nextchr - nextchar colides with
752 function of same name */
753 register I32 n; /* no or next */
754 register I32 ln; /* len or last */
755 register char *s; /* operand or save */
756 register char *locinput = reginput;
757 register I32 c1, c2, paren; /* case fold search, parenth */
758 int minmod = 0, sw = 0, logical = 0;
763 nextchr = UCHARAT(locinput);
765 while (scan != NULL) {
766 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
768 # define sayYES goto yes
769 # define sayNO goto no
770 # define saySAME(x) if (x) goto yes; else goto no
771 # define REPORT_CODE_OFF 24
773 # define sayYES return 1
774 # define sayNO return 0
775 # define saySAME(x) return x
778 SV *prop = sv_newmortal();
779 int docolor = *colors[0];
780 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
781 int l = (regeol - locinput > taill ? taill : regeol - locinput);
782 int pref_len = (locinput - bostr > (5 + taill) - l
783 ? (5 + taill) - l : locinput - bostr);
785 if (l + pref_len < (5 + taill) && l < regeol - locinput)
786 l = ( regeol - locinput > (5 + taill) - pref_len
787 ? (5 + taill) - pref_len : regeol - locinput);
789 PerlIO_printf(Perl_debug_log,
790 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
792 colors[2], pref_len, locinput - pref_len, colors[3],
793 (docolor ? "" : "> <"),
794 colors[0], l, locinput, colors[1],
795 15 - l - pref_len + 1,
797 scan - regprogram, regindent*2, "",
801 next = scan + NEXT_OFF(scan);
807 if (locinput == bostr
810 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
812 /* regtill = regbol; */
817 if (locinput == bostr
819 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
825 if (locinput == regbol && regprev == '\n')
829 if (locinput == regbol)
839 if ((nextchr || locinput < regeol) && nextchr != '\n')
844 if ((nextchr || locinput < regeol) && nextchr != '\n')
846 if (regeol - locinput > 1)
850 if (regeol != locinput)
854 if (!nextchr && locinput >= regeol)
856 nextchr = UCHARAT(++locinput);
859 if (!nextchr && locinput >= regeol || nextchr == '\n')
861 nextchr = UCHARAT(++locinput);
864 s = (char *) OPERAND(scan);
866 /* Inline the first character, for speed. */
867 if (UCHARAT(s) != nextchr)
869 if (regeol - locinput < ln)
871 if (ln > 1 && memNE(s, locinput, ln))
874 nextchr = UCHARAT(locinput);
877 reg_flags |= RF_tainted;
880 s = (char *) OPERAND(scan);
882 /* Inline the first character, for speed. */
883 if (UCHARAT(s) != nextchr &&
884 UCHARAT(s) != ((OP(scan) == EXACTF)
885 ? fold : fold_locale)[nextchr])
887 if (regeol - locinput < ln)
889 if (ln > 1 && (OP(scan) == EXACTF
890 ? ibcmp(s, locinput, ln)
891 : ibcmp_locale(s, locinput, ln)))
894 nextchr = UCHARAT(locinput);
897 s = (char *) OPERAND(scan);
899 nextchr = UCHARAT(locinput);
900 if (!REGINCLASS(s, nextchr))
902 if (!nextchr && locinput >= regeol)
904 nextchr = UCHARAT(++locinput);
907 reg_flags |= RF_tainted;
912 if (!(OP(scan) == ALNUM
913 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
915 nextchr = UCHARAT(++locinput);
918 reg_flags |= RF_tainted;
921 if (!nextchr && locinput >= regeol)
923 if (OP(scan) == NALNUM
924 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
926 nextchr = UCHARAT(++locinput);
930 reg_flags |= RF_tainted;
934 /* was last char in word? */
935 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
936 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
938 n = isALNUM(nextchr);
942 n = isALNUM_LC(nextchr);
944 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
948 reg_flags |= RF_tainted;
951 if (!nextchr && locinput >= regeol)
953 if (!(OP(scan) == SPACE
954 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
956 nextchr = UCHARAT(++locinput);
959 reg_flags |= RF_tainted;
964 if (OP(scan) == SPACE
965 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
967 nextchr = UCHARAT(++locinput);
970 if (!isDIGIT(nextchr))
972 nextchr = UCHARAT(++locinput);
975 if (!nextchr && locinput >= regeol)
977 if (isDIGIT(nextchr))
979 nextchr = UCHARAT(++locinput);
982 reg_flags |= RF_tainted;
986 n = ARG(scan); /* which paren pair */
988 if (*reglastparen < n || !s)
989 sayNO; /* Do not match unless seen CLOSEn. */
992 /* Inline the first character, for speed. */
993 if (UCHARAT(s) != nextchr &&
995 (UCHARAT(s) != ((OP(scan) == REFF
996 ? fold : fold_locale)[nextchr]))))
999 if (locinput + ln > regeol)
1001 if (ln > 1 && (OP(scan) == REF
1002 ? memNE(s, locinput, ln)
1004 ? ibcmp(s, locinput, ln)
1005 : ibcmp_locale(s, locinput, ln))))
1008 nextchr = UCHARAT(locinput);
1020 COP *ocurcop = curcop;
1021 SV **ocurpad = curpad;
1025 op = (OP_4tree*)regdata->data[n];
1026 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
1027 curpad = AvARRAY((AV*)regdata->data[n + 1]);
1029 CALLRUNOPS(); /* Scalar context. */
1038 sv_setsv(save_scalar(replgv), ret);
1045 n = ARG(scan); /* which paren pair */
1046 reg_start_tmp[n] = locinput;
1051 n = ARG(scan); /* which paren pair */
1052 regstartp[n] = reg_start_tmp[n];
1053 regendp[n] = locinput;
1054 if (n > *reglastparen)
1058 n = ARG(scan); /* which paren pair */
1059 sw = (*reglastparen >= n && regendp[n] != NULL);
1063 next = NEXTOPER(NEXTOPER(scan));
1065 next = scan + ARG(scan);
1066 if (OP(next) == IFTHEN) /* Fake one. */
1067 next = NEXTOPER(NEXTOPER(next));
1075 CHECKPOINT cp = savestack_ix;
1077 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1081 cc.parenfloor = *reglastparen;
1083 cc.min = ARG1(scan);
1084 cc.max = ARG2(scan);
1085 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1089 reginput = locinput;
1090 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1098 * This is really hard to understand, because after we match
1099 * what we're trying to match, we must make sure the rest of
1100 * the RE is going to match for sure, and to do that we have
1101 * to go back UP the parse tree by recursing ever deeper. And
1102 * if it fails, we have to reset our parent's current state
1103 * that we can try again after backing off.
1106 CHECKPOINT cp, lastcp;
1108 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1110 n = cc->cur + 1; /* how many we know we matched */
1111 reginput = locinput;
1114 PerlIO_printf(Perl_debug_log,
1115 "%*s %ld out of %ld..%ld cc=%lx\n",
1116 REPORT_CODE_OFF+regindent*2, "",
1117 (long)n, (long)cc->min,
1118 (long)cc->max, (long)cc)
1121 /* If degenerate scan matches "", assume scan done. */
1123 if (locinput == cc->lastloc && n >= cc->min) {
1127 PerlIO_printf(Perl_debug_log,
1128 "%*s empty match detected, try continuation...\n",
1129 REPORT_CODE_OFF+regindent*2, "")
1131 if (regmatch(cc->next))
1134 PerlIO_printf(Perl_debug_log,
1136 REPORT_CODE_OFF+regindent*2, "")
1143 /* First just match a string of min scans. */
1147 cc->lastloc = locinput;
1148 if (regmatch(cc->scan))
1151 cc->lastloc = lastloc;
1153 PerlIO_printf(Perl_debug_log,
1155 REPORT_CODE_OFF+regindent*2, "")
1160 /* Prefer next over scan for minimal matching. */
1165 cp = regcppush(cc->parenfloor);
1167 if (regmatch(cc->next)) {
1169 sayYES; /* All done. */
1176 if (n >= cc->max) { /* Maximum greed exceeded? */
1177 if (dowarn && n >= REG_INFTY
1178 && !(reg_flags & RF_warned)) {
1179 reg_flags |= RF_warned;
1180 warn("Complex regular subexpression recursion "
1181 "limit (%d) exceeded", REG_INFTY - 1);
1187 PerlIO_printf(Perl_debug_log,
1188 "%*s trying longer...\n",
1189 REPORT_CODE_OFF+regindent*2, "")
1191 /* Try scanning more and see if it helps. */
1192 reginput = locinput;
1194 cc->lastloc = locinput;
1195 cp = regcppush(cc->parenfloor);
1197 if (regmatch(cc->scan)) {
1202 PerlIO_printf(Perl_debug_log,
1204 REPORT_CODE_OFF+regindent*2, "")
1209 cc->lastloc = lastloc;
1213 /* Prefer scan over next for maximal matching. */
1215 if (n < cc->max) { /* More greed allowed? */
1216 cp = regcppush(cc->parenfloor);
1218 cc->lastloc = locinput;
1220 if (regmatch(cc->scan)) {
1225 regcppop(); /* Restore some previous $<digit>s? */
1226 reginput = locinput;
1228 PerlIO_printf(Perl_debug_log,
1229 "%*s failed, try continuation...\n",
1230 REPORT_CODE_OFF+regindent*2, "")
1233 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1234 reg_flags |= RF_warned;
1235 warn("count exceeded %d", REG_INFTY - 1);
1238 /* Failed deeper matches of scan, so see if this one works. */
1241 if (regmatch(cc->next))
1244 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1245 REPORT_CODE_OFF+regindent*2, "")
1250 cc->lastloc = lastloc;
1255 next = scan + ARG(scan);
1258 inner = NEXTOPER(NEXTOPER(scan));
1261 inner = NEXTOPER(scan);
1266 if (OP(next) != c1) /* No choice. */
1267 next = inner; /* Avoid recursion. */
1269 int lastparen = *reglastparen;
1273 reginput = locinput;
1274 if (regmatch(inner))
1277 for (n = *reglastparen; n > lastparen; n--)
1282 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1286 inner = NEXTOPER(scan);
1287 if (c1 == BRANCHJ) {
1288 inner = NEXTOPER(inner);
1290 } while (scan != NULL && OP(scan) == c1);
1304 /* We suppose that the next guy does not need
1305 backtracking: in particular, it is of constant length,
1306 and has no parenths to influence future backrefs. */
1307 ln = ARG1(scan); /* min to match */
1308 n = ARG2(scan); /* max to match */
1309 paren = scan->flags;
1311 if (paren > regsize)
1313 if (paren > *reglastparen)
1314 *reglastparen = paren;
1316 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1318 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1319 reginput = locinput;
1322 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1324 if (ln && l == 0 && n >= ln
1325 /* In fact, this is tricky. If paren, then the
1326 fact that we did/didnot match may influence
1327 future execution. */
1328 && !(paren && ln == 0))
1330 locinput = reginput;
1331 if (regkind[(U8)OP(next)] == EXACT) {
1332 c1 = UCHARAT(OPERAND(next) + 1);
1333 if (OP(next) == EXACTF)
1335 else if (OP(next) == EXACTFL)
1336 c2 = fold_locale[c1];
1342 /* This may be improved if l == 0. */
1343 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1344 /* If it could work, try it. */
1346 UCHARAT(reginput) == c1 ||
1347 UCHARAT(reginput) == c2)
1351 regstartp[paren] = reginput - l;
1352 regendp[paren] = reginput;
1354 regendp[paren] = NULL;
1360 /* Couldn't or didn't -- move forward. */
1361 reginput = locinput;
1362 if (regrepeat_hard(scan, 1, &l)) {
1364 locinput = reginput;
1370 n = regrepeat_hard(scan, n, &l);
1371 if (n != 0 && l == 0
1372 /* In fact, this is tricky. If paren, then the
1373 fact that we did/didnot match may influence
1374 future execution. */
1375 && !(paren && ln == 0))
1377 locinput = reginput;
1379 PerlIO_printf(Perl_debug_log,
1380 "%*s matched %ld times, len=%ld...\n",
1381 REPORT_CODE_OFF+regindent*2, "", n, l)
1384 if (regkind[(U8)OP(next)] == EXACT) {
1385 c1 = UCHARAT(OPERAND(next) + 1);
1386 if (OP(next) == EXACTF)
1388 else if (OP(next) == EXACTFL)
1389 c2 = fold_locale[c1];
1397 /* If it could work, try it. */
1399 UCHARAT(reginput) == c1 ||
1400 UCHARAT(reginput) == c2)
1403 PerlIO_printf(Perl_debug_log,
1404 "%*s trying tail with n=%ld...\n",
1405 REPORT_CODE_OFF+regindent*2, "", n)
1409 regstartp[paren] = reginput - l;
1410 regendp[paren] = reginput;
1412 regendp[paren] = NULL;
1418 /* Couldn't or didn't -- back up. */
1421 reginput = locinput;
1428 paren = scan->flags; /* Which paren to set */
1429 if (paren > regsize)
1431 if (paren > *reglastparen)
1432 *reglastparen = paren;
1433 ln = ARG1(scan); /* min to match */
1434 n = ARG2(scan); /* max to match */
1435 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1439 ln = ARG1(scan); /* min to match */
1440 n = ARG2(scan); /* max to match */
1441 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1446 scan = NEXTOPER(scan);
1452 scan = NEXTOPER(scan);
1456 * Lookahead to avoid useless match attempts
1457 * when we know what character comes next.
1459 if (regkind[(U8)OP(next)] == EXACT) {
1460 c1 = UCHARAT(OPERAND(next) + 1);
1461 if (OP(next) == EXACTF)
1463 else if (OP(next) == EXACTFL)
1464 c2 = fold_locale[c1];
1470 reginput = locinput;
1474 if (ln && regrepeat(scan, ln) < ln)
1477 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
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 -- move forward. */
1495 reginput = locinput + ln;
1496 if (regrepeat(scan, 1)) {
1498 reginput = locinput + ln;
1505 n = regrepeat(scan, n);
1506 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1507 (!multiline || OP(next) == SEOL))
1508 ln = n; /* why back off? */
1512 /* If it could work, try it. */
1514 UCHARAT(reginput) == c1 ||
1515 UCHARAT(reginput) == c2)
1519 regstartp[paren] = reginput - 1;
1520 regendp[paren] = reginput;
1522 regendp[paren] = NULL;
1528 /* Couldn't or didn't -- back up. */
1530 reginput = locinput + n;
1534 /* If it could work, try it. */
1536 UCHARAT(reginput) == c1 ||
1537 UCHARAT(reginput) == c2)
1543 /* Couldn't or didn't -- back up. */
1545 reginput = locinput + n;
1552 if (locinput < regtill)
1553 sayNO; /* Cannot match: too short. */
1556 reginput = locinput; /* put where regtry can find it */
1557 sayYES; /* Success! */
1563 if (locinput < bostr + scan->flags)
1568 if (locinput < bostr + scan->flags)
1571 reginput = locinput - scan->flags;
1572 inner = NEXTOPER(NEXTOPER(scan));
1573 if (regmatch(inner) != n) {
1587 if (OP(scan) == SUSPEND) {
1588 locinput = reginput;
1589 nextchr = UCHARAT(locinput);
1594 next = scan + ARG(scan);
1599 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1600 (unsigned long)scan, OP(scan));
1601 FAIL("regexp memory corruption");
1607 * We get here only if there's trouble -- normally "case END" is
1608 * the terminating point.
1610 FAIL("corrupted regexp pointers");
1628 - regrepeat - repeatedly match something simple, report how many
1631 * [This routine now assumes that it will only match on things of length 1.
1632 * That was true before, but now we assume scan - reginput is the count,
1633 * rather than incrementing count on every character.]
1636 regrepeat(regnode *p, I32 max)
1639 register char *scan;
1640 register char *opnd;
1642 register char *loceol = regeol;
1645 if (max != REG_INFTY && max < loceol - scan)
1646 loceol = scan + max;
1647 opnd = (char *) OPERAND(p);
1650 while (scan < loceol && *scan != '\n')
1656 case EXACT: /* length of string is 1 */
1657 c = UCHARAT(++opnd);
1658 while (scan < loceol && UCHARAT(scan) == c)
1661 case EXACTF: /* length of string is 1 */
1662 c = UCHARAT(++opnd);
1663 while (scan < loceol &&
1664 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1667 case EXACTFL: /* length of string is 1 */
1668 reg_flags |= RF_tainted;
1669 c = UCHARAT(++opnd);
1670 while (scan < loceol &&
1671 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1675 while (scan < loceol && REGINCLASS(opnd, *scan))
1679 while (scan < loceol && isALNUM(*scan))
1683 reg_flags |= RF_tainted;
1684 while (scan < loceol && isALNUM_LC(*scan))
1688 while (scan < loceol && !isALNUM(*scan))
1692 reg_flags |= RF_tainted;
1693 while (scan < loceol && !isALNUM_LC(*scan))
1697 while (scan < loceol && isSPACE(*scan))
1701 reg_flags |= RF_tainted;
1702 while (scan < loceol && isSPACE_LC(*scan))
1706 while (scan < loceol && !isSPACE(*scan))
1710 reg_flags |= RF_tainted;
1711 while (scan < loceol && !isSPACE_LC(*scan))
1715 while (scan < loceol && isDIGIT(*scan))
1719 while (scan < loceol && !isDIGIT(*scan))
1722 default: /* Called on something of 0 width. */
1723 break; /* So match right here or not at all. */
1726 c = scan - reginput;
1731 SV *prop = sv_newmortal();
1734 PerlIO_printf(Perl_debug_log,
1735 "%*s %s can match %ld times out of %ld...\n",
1736 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1743 - regrepeat_hard - repeatedly match something, report total lenth and length
1745 * The repeater is supposed to have constant length.
1749 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1752 register char *scan;
1753 register char *start;
1754 register char *loceol = regeol;
1758 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1760 *lp = l = reginput - start;
1761 if (max != REG_INFTY && l*max < loceol - scan)
1762 loceol = scan + l*max;
1768 if (reginput < loceol)
1773 return (scan - start)/l;
1777 - regclass - determine if a character falls into a character class
1781 reginclass(register char *p, register I32 c)
1788 if (ANYOF_TEST(p, c))
1790 else if (flags & ANYOF_FOLD) {
1792 if (flags & ANYOF_LOCALE) {
1793 reg_flags |= RF_tainted;
1794 cf = fold_locale[c];
1798 if (ANYOF_TEST(p, cf))
1802 if (!match && (flags & ANYOF_ISA)) {
1803 reg_flags |= RF_tainted;
1805 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1806 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1807 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1808 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1814 return (flags & ANYOF_INVERT) ? !match : match;