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.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregexec my_pregexec
44 * pregcomp and pregexec -- regsub and regerror are not used in perl
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
63 **** Alterations to Henry's code are...
65 **** Copyright (c) 1991-1997, Larry Wall
67 **** You may distribute under the terms of either the GNU General Public
68 **** License or the Artistic License, as specified in the README file.
70 * Beware that some of this code is subtly aware of the way operator
71 * precedence is structured in regular expressions. Serious changes in
72 * regular-expression syntax might require a total rethink.
78 #define RF_tainted 1 /* tainted information used? */
79 #define RF_warned 2 /* warned about big count? */
80 #define RF_evaled 4 /* Did an EVAL with setting? */
82 #define RS_init 1 /* eval environment created */
83 #define RS_set 2 /* replsv value is set */
90 typedef I32 CHECKPOINT;
96 static I32 regmatch _((regnode *prog));
97 static I32 regrepeat _((regnode *p, I32 max));
98 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
99 static I32 regtry _((regexp *prog, char *startpos));
101 static bool reginclass _((char *p, I32 c));
102 static CHECKPOINT regcppush _((I32 parenfloor));
103 static char * regcppop _((void));
105 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
108 regcppush(I32 parenfloor)
111 int retval = PL_savestack_ix;
112 int i = (PL_regsize - parenfloor) * 4;
116 for (p = PL_regsize; p > parenfloor; p--) {
117 SSPUSHPTR(PL_regendp[p]);
118 SSPUSHPTR(PL_regstartp[p]);
119 SSPUSHPTR(PL_reg_start_tmp[p]);
122 SSPUSHINT(PL_regsize);
123 SSPUSHINT(*PL_reglastparen);
124 SSPUSHPTR(PL_reginput);
126 SSPUSHINT(SAVEt_REGCONTEXT);
130 /* These are needed since we do not localize EVAL nodes: */
131 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
132 " Setting an EVAL scope, savestack=%i\n", \
133 PL_savestack_ix)); lastcp = PL_savestack_ix
135 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
136 PerlIO_printf(Perl_debug_log, \
137 " Clearing an EVAL scope, savestack=%i..%i\n", \
138 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
148 assert(i == SAVEt_REGCONTEXT);
150 input = (char *) SSPOPPTR;
151 *PL_reglastparen = SSPOPINT;
152 PL_regsize = SSPOPINT;
153 for (i -= 3; i > 0; i -= 4) {
154 paren = (U32)SSPOPINT;
155 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
156 PL_regstartp[paren] = (char *) SSPOPPTR;
157 tmps = (char*)SSPOPPTR;
158 if (paren <= *PL_reglastparen)
159 PL_regendp[paren] = tmps;
161 PerlIO_printf(Perl_debug_log,
162 " restoring \\%d to %d(%d)..%d%s\n",
163 paren, PL_regstartp[paren] - PL_regbol,
164 PL_reg_start_tmp[paren] - PL_regbol,
165 PL_regendp[paren] - PL_regbol,
166 (paren > *PL_reglastparen ? "(no)" : ""));
170 if (*PL_reglastparen + 1 <= PL_regnpar) {
171 PerlIO_printf(Perl_debug_log,
172 " restoring \\%d..\\%d to undef\n",
173 *PL_reglastparen + 1, PL_regnpar);
176 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
177 if (paren > PL_regsize)
178 PL_regstartp[paren] = Nullch;
179 PL_regendp[paren] = Nullch;
184 #define regcpblow(cp) LEAVE_SCOPE(cp)
187 * pregexec and friends
191 - pregexec - match a regexp against a string
194 pregexec(register regexp *prog, char *stringarg, register char *strend,
195 char *strbeg, I32 minend, SV *screamer, U32 nosave)
196 /* strend: pointer to null at end of string */
197 /* strbeg: real beginning of string */
198 /* minend: end of match must be >=minend after stringarg. */
199 /* nosave: For optimizations. */
202 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
203 nosave ? 0 : REXEC_COPY_STR);
207 - regexec_flags - match a regexp against a string
210 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
211 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
212 /* strend: pointer to null at end of string */
213 /* strbeg: real beginning of string */
214 /* minend: end of match must be >=minend after stringarg. */
215 /* data: May be used for some additional optimizations. */
216 /* nosave: For optimizations. */
221 register char *startpos = stringarg;
223 I32 minlen; /* must match at least this many chars */
224 I32 dontbother = 0; /* how many characters not to try at end */
226 I32 start_shift = 0; /* Offset of the start to find
228 I32 end_shift = 0; /* Same for the end. */
229 I32 scream_pos = -1; /* Internal iterator of scream. */
231 SV* oreplsv = GvSV(PL_replgv);
237 PL_regprecomp = prog->precomp; /* Needed for error messages. */
239 PL_regnarrate = PL_debug & 512;
240 PL_regprogram = prog->program;
244 if (prog == NULL || startpos == NULL) {
245 croak("NULL regexp parameter");
249 minlen = prog->minlen;
250 if (strend - startpos < minlen) goto phooey;
252 if (startpos == strbeg) /* is ^ valid at stringarg? */
255 PL_regprev = stringarg[-1];
256 if (!PL_multiline && PL_regprev == '\n')
257 PL_regprev = '\0'; /* force ^ to NOT match */
260 /* Check validity of program. */
261 if (UCHARAT(prog->program) != MAGIC) {
262 FAIL("corrupted regexp program");
265 PL_regnpar = prog->nparens;
269 /* If there is a "must appear" string, look for it. */
271 if (!(flags & REXEC_CHECKED)
272 && prog->check_substr != Nullsv &&
273 !(prog->reganch & ROPT_ANCH_GPOS) &&
274 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
275 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
277 start_shift = prog->check_offset_min;
278 /* Should be nonnegative! */
279 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
281 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
282 s = screaminstr(screamer, prog->check_substr,
283 start_shift + (stringarg - strbeg),
284 end_shift, &scream_pos, 0);
290 s = fbm_instr((unsigned char*)s + start_shift,
291 (unsigned char*)strend - end_shift,
292 prog->check_substr, 0);
294 ++BmUSEFUL(prog->check_substr); /* hooray */
295 goto phooey; /* not present */
296 } else if ((s - stringarg) > prog->check_offset_max) {
297 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
298 s -= prog->check_offset_max;
299 } else if (!prog->naughty
300 && --BmUSEFUL(prog->check_substr) < 0
301 && prog->check_substr == prog->float_substr) { /* boo */
302 SvREFCNT_dec(prog->check_substr);
303 prog->check_substr = Nullsv; /* disable */
304 prog->float_substr = Nullsv; /* clear */
309 /* Mark beginning of line for ^ and lookbehind. */
310 PL_regbol = startpos;
313 /* Mark end of line for $ (and such) */
316 /* see how far we have to get to not match where we matched before */
317 PL_regtill = startpos+minend;
320 PerlIO_printf(Perl_debug_log,
321 "Matching `%.60s%s' against `%.*s%s'\n",
323 (strlen(prog->precomp) > 60 ? "..." : ""),
324 (strend - startpos > 60 ? 60 : strend - startpos),
326 (strend - startpos > 60 ? "..." : ""))
329 /* Simplest case: anchored match need be tried only once. */
330 /* [unless only anchor is BOL and multiline is set] */
331 if (prog->reganch & ROPT_ANCH) {
332 if (regtry(prog, startpos))
334 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
335 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
336 || (prog->reganch & ROPT_ANCH_MBOL)))
339 dontbother = minlen - 1;
340 strend -= dontbother;
341 /* for multiline we only have to try after newlines */
346 if (s < strend && regtry(prog, s))
354 /* Messy cases: unanchored match. */
355 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
356 /* we have /x+whatever/ */
357 /* it must be a one character string */
358 char ch = SvPVX(prog->anchored_substr)[0];
361 if (regtry(prog, s)) goto got_it;
363 while (s < strend && *s == ch)
370 else if (prog->anchored_substr != Nullsv
371 || (prog->float_substr != Nullsv
372 && prog->float_max_offset < strend - s)) {
373 SV *must = prog->anchored_substr
374 ? prog->anchored_substr : prog->float_substr;
376 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
378 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
379 I32 delta = back_max - back_min;
380 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
381 char *last1 = s - 1; /* Last position checked before */
383 /* XXXX check_substr already used to find `s', can optimize if
384 check_substr==must. */
386 dontbother = end_shift;
387 strend -= dontbother;
388 while ( (s <= last) &&
390 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
391 end_shift, &scream_pos, 0))
392 : (s = fbm_instr((unsigned char*)s + back_min,
393 (unsigned char*)strend, must, 0))) ) {
394 if (s - back_max > last1) {
395 last1 = s - back_min;
400 last1 = s - back_min;
410 } else if (c = prog->regstclass) {
411 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
415 dontbother = minlen - 1;
416 strend -= dontbother; /* don't bother with what can't match */
418 /* We know what class it must start with. */
421 Class = (char *) OPERAND(c);
423 if (REGINCLASS(Class, *s)) {
424 if (tmp && regtry(prog, s))
435 PL_reg_flags |= RF_tainted;
439 dontbother++,strend--;
440 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
441 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
443 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
450 if ((minlen || tmp) && regtry(prog,s))
454 PL_reg_flags |= RF_tainted;
458 dontbother++,strend--;
459 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
460 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
462 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
464 else if (regtry(prog, s))
468 if ((minlen || !tmp) && regtry(prog,s))
474 if (tmp && regtry(prog, s))
485 PL_reg_flags |= RF_tainted;
487 if (isALNUM_LC(*s)) {
488 if (tmp && regtry(prog, s))
501 if (tmp && regtry(prog, s))
512 PL_reg_flags |= RF_tainted;
514 if (!isALNUM_LC(*s)) {
515 if (tmp && regtry(prog, s))
528 if (tmp && regtry(prog, s))
539 PL_reg_flags |= RF_tainted;
541 if (isSPACE_LC(*s)) {
542 if (tmp && regtry(prog, s))
555 if (tmp && regtry(prog, s))
566 PL_reg_flags |= RF_tainted;
568 if (!isSPACE_LC(*s)) {
569 if (tmp && regtry(prog, s))
582 if (tmp && regtry(prog, s))
595 if (tmp && regtry(prog, s))
609 if (prog->float_substr != Nullsv) { /* Trim the end. */
611 I32 oldpos = scream_pos;
614 last = screaminstr(screamer, prog->float_substr, s - strbeg,
615 end_shift, &scream_pos, 1); /* last one */
617 last = scream_olds; /* Only one occurence. */
621 char *little = SvPV(prog->float_substr, len);
623 last = rninstr(s, strend, little, little + len);
625 last = strend; /* matching `$' */
627 if (last == NULL) goto phooey; /* Should not happen! */
628 dontbother = strend - last + prog->float_min_offset;
630 if (minlen && (dontbother < minlen))
631 dontbother = minlen - 1;
632 strend -= dontbother;
633 /* We don't know much -- general case. */
637 } while (s++ < strend);
644 prog->subbeg = strbeg;
645 prog->subend = PL_regeol; /* strend may have been modified */
646 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
648 /* make sure $`, $&, $', and $digit will work later */
649 if (strbeg != prog->subbase) { /* second+ //g match. */
650 if (!(flags & REXEC_COPY_STR)) {
652 Safefree(prog->subbase);
653 prog->subbase = Nullch;
657 I32 i = PL_regeol - startpos + (stringarg - strbeg);
658 s = savepvn(strbeg, i);
659 Safefree(prog->subbase);
661 prog->subbeg = prog->subbase;
662 prog->subend = prog->subbase + i;
663 s = prog->subbase + (stringarg - strbeg);
664 for (i = 0; i <= prog->nparens; i++) {
666 prog->startp[i] = s + (prog->startp[i] - startpos);
667 prog->endp[i] = s + (prog->endp[i] - startpos);
672 /* Preserve the current value of $^R */
673 if (oreplsv != GvSV(PL_replgv)) {
674 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
675 restored, the value remains
685 - regtry - try match at specific point
687 STATIC I32 /* 0 failure, 1 success */
688 regtry(regexp *prog, char *startpos)
696 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
697 PL_reg_eval_set = RS_init;
699 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
700 PL_stack_sp - PL_stack_base);
702 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
703 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
704 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
706 /* Apparently this is not needed, judging by wantarray. */
707 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
708 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
710 PL_reginput = startpos;
711 PL_regstartp = prog->startp;
712 PL_regendp = prog->endp;
713 PL_reglastparen = &prog->lastparen;
716 if (PL_reg_start_tmpl <= prog->nparens) {
717 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
719 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
721 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
726 PL_regdata = prog->data;
728 for (i = prog->nparens; i >= 0; i--) {
734 if (regmatch(prog->program + 1)) {
735 prog->startp[0] = startpos;
736 prog->endp[0] = PL_reginput;
744 - regmatch - main matching routine
746 * Conceptually the strategy is simple: check to see whether the current
747 * node matches, call self recursively to see whether the rest matches,
748 * and then act accordingly. In practice we make some effort to avoid
749 * recursion, in particular by going through "ordinary" nodes (that don't
750 * need to know whether the rest of the match failed) by a loop instead of
753 /* [lwall] I've hoisted the register declarations to the outer block in order to
754 * maybe save a little bit of pushing and popping on the stack. It also takes
755 * advantage of machines that use a register save mask on subroutine entry.
757 STATIC I32 /* 0 failure, 1 success */
758 regmatch(regnode *prog)
761 register regnode *scan; /* Current node. */
762 regnode *next; /* Next node. */
763 regnode *inner; /* Next node in internal branch. */
764 register I32 nextchr; /* renamed nextchr - nextchar colides with
765 function of same name */
766 register I32 n; /* no or next */
767 register I32 ln; /* len or last */
768 register char *s; /* operand or save */
769 register char *locinput = PL_reginput;
770 register I32 c1, c2, paren; /* case fold search, parenth */
771 int minmod = 0, sw = 0, logical = 0;
776 nextchr = UCHARAT(locinput);
778 while (scan != NULL) {
779 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
781 # define sayYES goto yes
782 # define sayNO goto no
783 # define saySAME(x) if (x) goto yes; else goto no
784 # define REPORT_CODE_OFF 24
786 # define sayYES return 1
787 # define sayNO return 0
788 # define saySAME(x) return x
791 SV *prop = sv_newmortal();
792 int docolor = *PL_colors[0];
793 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
794 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
795 int pref_len = (locinput - PL_bostr > (5 + taill) - l
796 ? (5 + taill) - l : locinput - PL_bostr);
798 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
799 l = ( PL_regeol - locinput > (5 + taill) - pref_len
800 ? (5 + taill) - pref_len : PL_regeol - locinput);
802 PerlIO_printf(Perl_debug_log,
803 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
805 PL_colors[2], pref_len, locinput - pref_len, PL_colors[3],
806 (docolor ? "" : "> <"),
807 PL_colors[0], l, locinput, PL_colors[1],
808 15 - l - pref_len + 1,
810 scan - PL_regprogram, PL_regindent*2, "",
814 next = scan + NEXT_OFF(scan);
820 if (locinput == PL_bostr
823 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
825 /* PL_regtill = PL_regbol; */
830 if (locinput == PL_bostr
832 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
838 if (locinput == PL_regbol && PL_regprev == '\n')
842 if (locinput == PL_regbol)
852 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
857 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
859 if (PL_regeol - locinput > 1)
863 if (PL_regeol != locinput)
867 if (!nextchr && locinput >= PL_regeol)
869 nextchr = UCHARAT(++locinput);
872 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
874 nextchr = UCHARAT(++locinput);
877 s = (char *) OPERAND(scan);
879 /* Inline the first character, for speed. */
880 if (UCHARAT(s) != nextchr)
882 if (PL_regeol - locinput < ln)
884 if (ln > 1 && memNE(s, locinput, ln))
887 nextchr = UCHARAT(locinput);
890 PL_reg_flags |= RF_tainted;
893 s = (char *) OPERAND(scan);
895 /* Inline the first character, for speed. */
896 if (UCHARAT(s) != nextchr &&
897 UCHARAT(s) != ((OP(scan) == EXACTF)
898 ? fold : fold_locale)[nextchr])
900 if (PL_regeol - locinput < ln)
902 if (ln > 1 && (OP(scan) == EXACTF
903 ? ibcmp(s, locinput, ln)
904 : ibcmp_locale(s, locinput, ln)))
907 nextchr = UCHARAT(locinput);
910 s = (char *) OPERAND(scan);
912 nextchr = UCHARAT(locinput);
913 if (!REGINCLASS(s, nextchr))
915 if (!nextchr && locinput >= PL_regeol)
917 nextchr = UCHARAT(++locinput);
920 PL_reg_flags |= RF_tainted;
925 if (!(OP(scan) == ALNUM
926 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
928 nextchr = UCHARAT(++locinput);
931 PL_reg_flags |= RF_tainted;
934 if (!nextchr && locinput >= PL_regeol)
936 if (OP(scan) == NALNUM
937 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
939 nextchr = UCHARAT(++locinput);
943 PL_reg_flags |= RF_tainted;
947 /* was last char in word? */
948 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
949 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
951 n = isALNUM(nextchr);
955 n = isALNUM_LC(nextchr);
957 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
961 PL_reg_flags |= RF_tainted;
964 if (!nextchr && locinput >= PL_regeol)
966 if (!(OP(scan) == SPACE
967 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
969 nextchr = UCHARAT(++locinput);
972 PL_reg_flags |= RF_tainted;
977 if (OP(scan) == SPACE
978 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
980 nextchr = UCHARAT(++locinput);
983 if (!isDIGIT(nextchr))
985 nextchr = UCHARAT(++locinput);
988 if (!nextchr && locinput >= PL_regeol)
990 if (isDIGIT(nextchr))
992 nextchr = UCHARAT(++locinput);
995 PL_reg_flags |= RF_tainted;
999 n = ARG(scan); /* which paren pair */
1000 s = PL_regstartp[n];
1001 if (*PL_reglastparen < n || !s)
1002 sayNO; /* Do not match unless seen CLOSEn. */
1003 if (s == PL_regendp[n])
1005 /* Inline the first character, for speed. */
1006 if (UCHARAT(s) != nextchr &&
1008 (UCHARAT(s) != ((OP(scan) == REFF
1009 ? fold : fold_locale)[nextchr]))))
1011 ln = PL_regendp[n] - s;
1012 if (locinput + ln > PL_regeol)
1014 if (ln > 1 && (OP(scan) == REF
1015 ? memNE(s, locinput, ln)
1017 ? ibcmp(s, locinput, ln)
1018 : ibcmp_locale(s, locinput, ln))))
1021 nextchr = UCHARAT(locinput);
1032 OP_4tree *oop = PL_op;
1033 COP *ocurcop = PL_curcop;
1034 SV **ocurpad = PL_curpad;
1038 PL_op = (OP_4tree*)PL_regdata->data[n];
1039 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1040 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1042 CALLRUNOPS(); /* Scalar context. */
1051 sv_setsv(save_scalar(PL_replgv), ret);
1053 PL_curpad = ocurpad;
1054 PL_curcop = ocurcop;
1058 n = ARG(scan); /* which paren pair */
1059 PL_reg_start_tmp[n] = locinput;
1064 n = ARG(scan); /* which paren pair */
1065 PL_regstartp[n] = PL_reg_start_tmp[n];
1066 PL_regendp[n] = locinput;
1067 if (n > *PL_reglastparen)
1068 *PL_reglastparen = n;
1071 n = ARG(scan); /* which paren pair */
1072 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1076 next = NEXTOPER(NEXTOPER(scan));
1078 next = scan + ARG(scan);
1079 if (OP(next) == IFTHEN) /* Fake one. */
1080 next = NEXTOPER(NEXTOPER(next));
1088 CHECKPOINT cp = PL_savestack_ix;
1090 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1092 cc.oldcc = PL_regcc;
1094 cc.parenfloor = *PL_reglastparen;
1096 cc.min = ARG1(scan);
1097 cc.max = ARG2(scan);
1098 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1102 PL_reginput = locinput;
1103 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1105 PL_regcc = cc.oldcc;
1111 * This is really hard to understand, because after we match
1112 * what we're trying to match, we must make sure the rest of
1113 * the RE is going to match for sure, and to do that we have
1114 * to go back UP the parse tree by recursing ever deeper. And
1115 * if it fails, we have to reset our parent's current state
1116 * that we can try again after backing off.
1119 CHECKPOINT cp, lastcp;
1120 CURCUR* cc = PL_regcc;
1121 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1123 n = cc->cur + 1; /* how many we know we matched */
1124 PL_reginput = locinput;
1127 PerlIO_printf(Perl_debug_log,
1128 "%*s %ld out of %ld..%ld cc=%lx\n",
1129 REPORT_CODE_OFF+PL_regindent*2, "",
1130 (long)n, (long)cc->min,
1131 (long)cc->max, (long)cc)
1134 /* If degenerate scan matches "", assume scan done. */
1136 if (locinput == cc->lastloc && n >= cc->min) {
1137 PL_regcc = cc->oldcc;
1140 PerlIO_printf(Perl_debug_log,
1141 "%*s empty match detected, try continuation...\n",
1142 REPORT_CODE_OFF+PL_regindent*2, "")
1144 if (regmatch(cc->next))
1147 PerlIO_printf(Perl_debug_log,
1149 REPORT_CODE_OFF+PL_regindent*2, "")
1156 /* First just match a string of min scans. */
1160 cc->lastloc = locinput;
1161 if (regmatch(cc->scan))
1164 cc->lastloc = lastloc;
1166 PerlIO_printf(Perl_debug_log,
1168 REPORT_CODE_OFF+PL_regindent*2, "")
1173 /* Prefer next over scan for minimal matching. */
1176 PL_regcc = cc->oldcc;
1178 cp = regcppush(cc->parenfloor);
1180 if (regmatch(cc->next)) {
1182 sayYES; /* All done. */
1189 if (n >= cc->max) { /* Maximum greed exceeded? */
1190 if (PL_dowarn && n >= REG_INFTY
1191 && !(PL_reg_flags & RF_warned)) {
1192 PL_reg_flags |= RF_warned;
1193 warn("%s limit (%d) exceeded",
1194 "Complex regular subexpression recursion",
1201 PerlIO_printf(Perl_debug_log,
1202 "%*s trying longer...\n",
1203 REPORT_CODE_OFF+PL_regindent*2, "")
1205 /* Try scanning more and see if it helps. */
1206 PL_reginput = locinput;
1208 cc->lastloc = locinput;
1209 cp = regcppush(cc->parenfloor);
1211 if (regmatch(cc->scan)) {
1216 PerlIO_printf(Perl_debug_log,
1218 REPORT_CODE_OFF+PL_regindent*2, "")
1223 cc->lastloc = lastloc;
1227 /* Prefer scan over next for maximal matching. */
1229 if (n < cc->max) { /* More greed allowed? */
1230 cp = regcppush(cc->parenfloor);
1232 cc->lastloc = locinput;
1234 if (regmatch(cc->scan)) {
1239 regcppop(); /* Restore some previous $<digit>s? */
1240 PL_reginput = locinput;
1242 PerlIO_printf(Perl_debug_log,
1243 "%*s failed, try continuation...\n",
1244 REPORT_CODE_OFF+PL_regindent*2, "")
1247 if (PL_dowarn && n >= REG_INFTY && !(PL_reg_flags & RF_warned)) {
1248 PL_reg_flags |= RF_warned;
1249 warn("count exceeded %d", REG_INFTY - 1);
1252 /* Failed deeper matches of scan, so see if this one works. */
1253 PL_regcc = cc->oldcc;
1255 if (regmatch(cc->next))
1258 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1259 REPORT_CODE_OFF+PL_regindent*2, "")
1264 cc->lastloc = lastloc;
1269 next = scan + ARG(scan);
1272 inner = NEXTOPER(NEXTOPER(scan));
1275 inner = NEXTOPER(scan);
1280 if (OP(next) != c1) /* No choice. */
1281 next = inner; /* Avoid recursion. */
1283 int lastparen = *PL_reglastparen;
1287 PL_reginput = locinput;
1288 if (regmatch(inner))
1291 for (n = *PL_reglastparen; n > lastparen; n--)
1293 *PL_reglastparen = n;
1296 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1300 inner = NEXTOPER(scan);
1301 if (c1 == BRANCHJ) {
1302 inner = NEXTOPER(inner);
1304 } while (scan != NULL && OP(scan) == c1);
1318 /* We suppose that the next guy does not need
1319 backtracking: in particular, it is of constant length,
1320 and has no parenths to influence future backrefs. */
1321 ln = ARG1(scan); /* min to match */
1322 n = ARG2(scan); /* max to match */
1323 paren = scan->flags;
1325 if (paren > PL_regsize)
1327 if (paren > *PL_reglastparen)
1328 *PL_reglastparen = paren;
1330 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1332 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1333 PL_reginput = locinput;
1336 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1338 if (ln && l == 0 && n >= ln
1339 /* In fact, this is tricky. If paren, then the
1340 fact that we did/didnot match may influence
1341 future execution. */
1342 && !(paren && ln == 0))
1344 locinput = PL_reginput;
1345 if (regkind[(U8)OP(next)] == EXACT) {
1346 c1 = UCHARAT(OPERAND(next) + 1);
1347 if (OP(next) == EXACTF)
1349 else if (OP(next) == EXACTFL)
1350 c2 = fold_locale[c1];
1356 /* This may be improved if l == 0. */
1357 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1358 /* If it could work, try it. */
1360 UCHARAT(PL_reginput) == c1 ||
1361 UCHARAT(PL_reginput) == c2)
1365 PL_regstartp[paren] = PL_reginput - l;
1366 PL_regendp[paren] = PL_reginput;
1368 PL_regendp[paren] = NULL;
1374 /* Couldn't or didn't -- move forward. */
1375 PL_reginput = locinput;
1376 if (regrepeat_hard(scan, 1, &l)) {
1378 locinput = PL_reginput;
1384 n = regrepeat_hard(scan, n, &l);
1385 if (n != 0 && l == 0
1386 /* In fact, this is tricky. If paren, then the
1387 fact that we did/didnot match may influence
1388 future execution. */
1389 && !(paren && ln == 0))
1391 locinput = PL_reginput;
1393 PerlIO_printf(Perl_debug_log,
1394 "%*s matched %ld times, len=%ld...\n",
1395 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
1398 if (regkind[(U8)OP(next)] == EXACT) {
1399 c1 = UCHARAT(OPERAND(next) + 1);
1400 if (OP(next) == EXACTF)
1402 else if (OP(next) == EXACTFL)
1403 c2 = fold_locale[c1];
1411 /* If it could work, try it. */
1413 UCHARAT(PL_reginput) == c1 ||
1414 UCHARAT(PL_reginput) == c2)
1417 PerlIO_printf(Perl_debug_log,
1418 "%*s trying tail with n=%ld...\n",
1419 REPORT_CODE_OFF+PL_regindent*2, "", n)
1423 PL_regstartp[paren] = PL_reginput - l;
1424 PL_regendp[paren] = PL_reginput;
1426 PL_regendp[paren] = NULL;
1432 /* Couldn't or didn't -- back up. */
1435 PL_reginput = locinput;
1442 paren = scan->flags; /* Which paren to set */
1443 if (paren > PL_regsize)
1445 if (paren > *PL_reglastparen)
1446 *PL_reglastparen = paren;
1447 ln = ARG1(scan); /* min to match */
1448 n = ARG2(scan); /* max to match */
1449 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1453 ln = ARG1(scan); /* min to match */
1454 n = ARG2(scan); /* max to match */
1455 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1460 scan = NEXTOPER(scan);
1466 scan = NEXTOPER(scan);
1470 * Lookahead to avoid useless match attempts
1471 * when we know what character comes next.
1473 if (regkind[(U8)OP(next)] == EXACT) {
1474 c1 = UCHARAT(OPERAND(next) + 1);
1475 if (OP(next) == EXACTF)
1477 else if (OP(next) == EXACTFL)
1478 c2 = fold_locale[c1];
1484 PL_reginput = locinput;
1488 if (ln && regrepeat(scan, ln) < ln)
1491 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1492 /* If it could work, try it. */
1494 UCHARAT(PL_reginput) == c1 ||
1495 UCHARAT(PL_reginput) == c2)
1499 PL_regstartp[paren] = PL_reginput - 1;
1500 PL_regendp[paren] = PL_reginput;
1502 PL_regendp[paren] = NULL;
1508 /* Couldn't or didn't -- move forward. */
1509 PL_reginput = locinput + ln;
1510 if (regrepeat(scan, 1)) {
1512 PL_reginput = locinput + ln;
1519 n = regrepeat(scan, n);
1520 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1521 (!PL_multiline || OP(next) == SEOL))
1522 ln = n; /* why back off? */
1526 /* If it could work, try it. */
1528 UCHARAT(PL_reginput) == c1 ||
1529 UCHARAT(PL_reginput) == c2)
1533 PL_regstartp[paren] = PL_reginput - 1;
1534 PL_regendp[paren] = PL_reginput;
1536 PL_regendp[paren] = NULL;
1542 /* Couldn't or didn't -- back up. */
1544 PL_reginput = locinput + n;
1548 /* If it could work, try it. */
1550 UCHARAT(PL_reginput) == c1 ||
1551 UCHARAT(PL_reginput) == c2)
1557 /* Couldn't or didn't -- back up. */
1559 PL_reginput = locinput + n;
1566 if (locinput < PL_regtill)
1567 sayNO; /* Cannot match: too short. */
1570 PL_reginput = locinput; /* put where regtry can find it */
1571 sayYES; /* Success! */
1577 if (locinput < PL_bostr + scan->flags)
1582 if (locinput < PL_bostr + scan->flags)
1585 PL_reginput = locinput - scan->flags;
1586 inner = NEXTOPER(NEXTOPER(scan));
1587 if (regmatch(inner) != n) {
1601 if (OP(scan) == SUSPEND) {
1602 locinput = PL_reginput;
1603 nextchr = UCHARAT(locinput);
1608 next = scan + ARG(scan);
1613 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1614 (unsigned long)scan, OP(scan));
1615 FAIL("regexp memory corruption");
1621 * We get here only if there's trouble -- normally "case END" is
1622 * the terminating point.
1624 FAIL("corrupted regexp pointers");
1642 - regrepeat - repeatedly match something simple, report how many
1645 * [This routine now assumes that it will only match on things of length 1.
1646 * That was true before, but now we assume scan - reginput is the count,
1647 * rather than incrementing count on every character.]
1650 regrepeat(regnode *p, I32 max)
1653 register char *scan;
1654 register char *opnd;
1656 register char *loceol = PL_regeol;
1659 if (max != REG_INFTY && max < loceol - scan)
1660 loceol = scan + max;
1661 opnd = (char *) OPERAND(p);
1664 while (scan < loceol && *scan != '\n')
1670 case EXACT: /* length of string is 1 */
1671 c = UCHARAT(++opnd);
1672 while (scan < loceol && UCHARAT(scan) == c)
1675 case EXACTF: /* length of string is 1 */
1676 c = UCHARAT(++opnd);
1677 while (scan < loceol &&
1678 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1681 case EXACTFL: /* length of string is 1 */
1682 PL_reg_flags |= RF_tainted;
1683 c = UCHARAT(++opnd);
1684 while (scan < loceol &&
1685 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1689 while (scan < loceol && REGINCLASS(opnd, *scan))
1693 while (scan < loceol && isALNUM(*scan))
1697 PL_reg_flags |= RF_tainted;
1698 while (scan < loceol && isALNUM_LC(*scan))
1702 while (scan < loceol && !isALNUM(*scan))
1706 PL_reg_flags |= RF_tainted;
1707 while (scan < loceol && !isALNUM_LC(*scan))
1711 while (scan < loceol && isSPACE(*scan))
1715 PL_reg_flags |= RF_tainted;
1716 while (scan < loceol && isSPACE_LC(*scan))
1720 while (scan < loceol && !isSPACE(*scan))
1724 PL_reg_flags |= RF_tainted;
1725 while (scan < loceol && !isSPACE_LC(*scan))
1729 while (scan < loceol && isDIGIT(*scan))
1733 while (scan < loceol && !isDIGIT(*scan))
1736 default: /* Called on something of 0 width. */
1737 break; /* So match right here or not at all. */
1740 c = scan - PL_reginput;
1745 SV *prop = sv_newmortal();
1748 PerlIO_printf(Perl_debug_log,
1749 "%*s %s can match %ld times out of %ld...\n",
1750 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1757 - regrepeat_hard - repeatedly match something, report total lenth and length
1759 * The repeater is supposed to have constant length.
1763 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1766 register char *scan;
1767 register char *start;
1768 register char *loceol = PL_regeol;
1771 start = PL_reginput;
1772 while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) {
1774 *lp = l = PL_reginput - start;
1775 if (max != REG_INFTY && l*max < loceol - scan)
1776 loceol = scan + l*max;
1782 if (PL_reginput < loceol)
1787 return (scan - start)/l;
1791 - regclass - determine if a character falls into a character class
1795 reginclass(register char *p, register I32 c)
1802 if (ANYOF_TEST(p, c))
1804 else if (flags & ANYOF_FOLD) {
1806 if (flags & ANYOF_LOCALE) {
1807 PL_reg_flags |= RF_tainted;
1808 cf = fold_locale[c];
1812 if (ANYOF_TEST(p, cf))
1816 if (!match && (flags & ANYOF_ISA)) {
1817 PL_reg_flags |= RF_tainted;
1819 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1820 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1821 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1822 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1828 return (flags & ANYOF_INVERT) ? !match : match;