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 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
322 PL_colors[4],PL_colors[5],PL_colors[0],
325 (strlen(prog->precomp) > 60 ? "..." : ""),
327 (strend - startpos > 60 ? 60 : strend - startpos),
328 startpos, PL_colors[1],
329 (strend - startpos > 60 ? "..." : ""))
332 /* Simplest case: anchored match need be tried only once. */
333 /* [unless only anchor is BOL and multiline is set] */
334 if (prog->reganch & ROPT_ANCH) {
335 if (regtry(prog, startpos))
337 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
338 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
339 || (prog->reganch & ROPT_ANCH_MBOL)))
342 dontbother = minlen - 1;
343 strend -= dontbother;
344 /* for multiline we only have to try after newlines */
349 if (s < strend && regtry(prog, s))
357 /* Messy cases: unanchored match. */
358 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
359 /* we have /x+whatever/ */
360 /* it must be a one character string */
361 char ch = SvPVX(prog->anchored_substr)[0];
364 if (regtry(prog, s)) goto got_it;
366 while (s < strend && *s == ch)
373 else if (prog->anchored_substr != Nullsv
374 || (prog->float_substr != Nullsv
375 && prog->float_max_offset < strend - s)) {
376 SV *must = prog->anchored_substr
377 ? prog->anchored_substr : prog->float_substr;
379 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
381 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
382 I32 delta = back_max - back_min;
383 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
384 char *last1 = s - 1; /* Last position checked before */
386 /* XXXX check_substr already used to find `s', can optimize if
387 check_substr==must. */
389 dontbother = end_shift;
390 strend -= dontbother;
391 while ( (s <= last) &&
393 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
394 end_shift, &scream_pos, 0))
395 : (s = fbm_instr((unsigned char*)s + back_min,
396 (unsigned char*)strend, must, 0))) ) {
397 if (s - back_max > last1) {
398 last1 = s - back_min;
403 last1 = s - back_min;
413 } else if (c = prog->regstclass) {
414 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
418 dontbother = minlen - 1;
419 strend -= dontbother; /* don't bother with what can't match */
421 /* We know what class it must start with. */
424 Class = (char *) OPERAND(c);
426 if (REGINCLASS(Class, *s)) {
427 if (tmp && regtry(prog, s))
438 PL_reg_flags |= RF_tainted;
442 dontbother++,strend--;
443 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
444 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
446 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
453 if ((minlen || tmp) && regtry(prog,s))
457 PL_reg_flags |= RF_tainted;
461 dontbother++,strend--;
462 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
463 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
465 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
467 else if (regtry(prog, s))
471 if ((minlen || !tmp) && regtry(prog,s))
477 if (tmp && regtry(prog, s))
488 PL_reg_flags |= RF_tainted;
490 if (isALNUM_LC(*s)) {
491 if (tmp && regtry(prog, s))
504 if (tmp && regtry(prog, s))
515 PL_reg_flags |= RF_tainted;
517 if (!isALNUM_LC(*s)) {
518 if (tmp && regtry(prog, s))
531 if (tmp && regtry(prog, s))
542 PL_reg_flags |= RF_tainted;
544 if (isSPACE_LC(*s)) {
545 if (tmp && regtry(prog, s))
558 if (tmp && regtry(prog, s))
569 PL_reg_flags |= RF_tainted;
571 if (!isSPACE_LC(*s)) {
572 if (tmp && regtry(prog, s))
585 if (tmp && regtry(prog, s))
598 if (tmp && regtry(prog, s))
612 if (prog->float_substr != Nullsv) { /* Trim the end. */
614 I32 oldpos = scream_pos;
617 last = screaminstr(screamer, prog->float_substr, s - strbeg,
618 end_shift, &scream_pos, 1); /* last one */
620 last = scream_olds; /* Only one occurence. */
624 char *little = SvPV(prog->float_substr, len);
626 last = rninstr(s, strend, little, little + len);
628 last = strend; /* matching `$' */
630 if (last == NULL) goto phooey; /* Should not happen! */
631 dontbother = strend - last + prog->float_min_offset;
633 if (minlen && (dontbother < minlen))
634 dontbother = minlen - 1;
635 strend -= dontbother;
636 /* We don't know much -- general case. */
640 } while (s++ < strend);
647 prog->subbeg = strbeg;
648 prog->subend = PL_regeol; /* strend may have been modified */
649 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
651 /* make sure $`, $&, $', and $digit will work later */
652 if (strbeg != prog->subbase) { /* second+ //g match. */
653 if (!(flags & REXEC_COPY_STR)) {
655 Safefree(prog->subbase);
656 prog->subbase = Nullch;
660 I32 i = PL_regeol - startpos + (stringarg - strbeg);
661 s = savepvn(strbeg, i);
662 Safefree(prog->subbase);
664 prog->subbeg = prog->subbase;
665 prog->subend = prog->subbase + i;
666 s = prog->subbase + (stringarg - strbeg);
667 for (i = 0; i <= prog->nparens; i++) {
669 prog->startp[i] = s + (prog->startp[i] - startpos);
670 prog->endp[i] = s + (prog->endp[i] - startpos);
675 /* Preserve the current value of $^R */
676 if (oreplsv != GvSV(PL_replgv)) {
677 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
678 restored, the value remains
688 - regtry - try match at specific point
690 STATIC I32 /* 0 failure, 1 success */
691 regtry(regexp *prog, char *startpos)
699 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
700 PL_reg_eval_set = RS_init;
702 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
703 PL_stack_sp - PL_stack_base);
705 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
706 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
707 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
709 /* Apparently this is not needed, judging by wantarray. */
710 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
711 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
713 PL_reginput = startpos;
714 PL_regstartp = prog->startp;
715 PL_regendp = prog->endp;
716 PL_reglastparen = &prog->lastparen;
719 if (PL_reg_start_tmpl <= prog->nparens) {
720 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
722 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
724 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
729 PL_regdata = prog->data;
731 for (i = prog->nparens; i >= 0; i--) {
737 if (regmatch(prog->program + 1)) {
738 prog->startp[0] = startpos;
739 prog->endp[0] = PL_reginput;
747 - regmatch - main matching routine
749 * Conceptually the strategy is simple: check to see whether the current
750 * node matches, call self recursively to see whether the rest matches,
751 * and then act accordingly. In practice we make some effort to avoid
752 * recursion, in particular by going through "ordinary" nodes (that don't
753 * need to know whether the rest of the match failed) by a loop instead of
756 /* [lwall] I've hoisted the register declarations to the outer block in order to
757 * maybe save a little bit of pushing and popping on the stack. It also takes
758 * advantage of machines that use a register save mask on subroutine entry.
760 STATIC I32 /* 0 failure, 1 success */
761 regmatch(regnode *prog)
764 register regnode *scan; /* Current node. */
765 regnode *next; /* Next node. */
766 regnode *inner; /* Next node in internal branch. */
767 register I32 nextchr; /* renamed nextchr - nextchar colides with
768 function of same name */
769 register I32 n; /* no or next */
770 register I32 ln; /* len or last */
771 register char *s; /* operand or save */
772 register char *locinput = PL_reginput;
773 register I32 c1, c2, paren; /* case fold search, parenth */
774 int minmod = 0, sw = 0, logical = 0;
779 nextchr = UCHARAT(locinput);
781 while (scan != NULL) {
782 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
784 # define sayYES goto yes
785 # define sayNO goto no
786 # define saySAME(x) if (x) goto yes; else goto no
787 # define REPORT_CODE_OFF 24
789 # define sayYES return 1
790 # define sayNO return 0
791 # define saySAME(x) return x
794 SV *prop = sv_newmortal();
795 int docolor = *PL_colors[0];
796 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
797 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
798 int pref_len = (locinput - PL_bostr > (5 + taill) - l
799 ? (5 + taill) - l : locinput - PL_bostr);
800 int pref0_len = pref_len - (locinput - PL_reginput);
802 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
803 l = ( PL_regeol - locinput > (5 + taill) - pref_len
804 ? (5 + taill) - pref_len : PL_regeol - locinput);
808 PerlIO_printf(Perl_debug_log,
809 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
811 PL_colors[4], pref0_len,
812 locinput - pref_len, PL_colors[5],
813 PL_colors[2], pref_len - pref0_len,
814 locinput - pref_len + pref0_len, PL_colors[3],
815 (docolor ? "" : "> <"),
816 PL_colors[0], l, locinput, PL_colors[1],
817 15 - l - pref_len + 1,
819 scan - PL_regprogram, PL_regindent*2, "",
823 next = scan + NEXT_OFF(scan);
829 if (locinput == PL_bostr
832 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
834 /* PL_regtill = PL_regbol; */
839 if (locinput == PL_bostr
841 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
847 if (locinput == PL_regbol && PL_regprev == '\n')
851 if (locinput == PL_regbol)
861 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
866 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
868 if (PL_regeol - locinput > 1)
872 if (PL_regeol != locinput)
876 if (!nextchr && locinput >= PL_regeol)
878 nextchr = UCHARAT(++locinput);
881 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
883 nextchr = UCHARAT(++locinput);
886 s = (char *) OPERAND(scan);
888 /* Inline the first character, for speed. */
889 if (UCHARAT(s) != nextchr)
891 if (PL_regeol - locinput < ln)
893 if (ln > 1 && memNE(s, locinput, ln))
896 nextchr = UCHARAT(locinput);
899 PL_reg_flags |= RF_tainted;
902 s = (char *) OPERAND(scan);
904 /* Inline the first character, for speed. */
905 if (UCHARAT(s) != nextchr &&
906 UCHARAT(s) != ((OP(scan) == EXACTF)
907 ? fold : fold_locale)[nextchr])
909 if (PL_regeol - locinput < ln)
911 if (ln > 1 && (OP(scan) == EXACTF
912 ? ibcmp(s, locinput, ln)
913 : ibcmp_locale(s, locinput, ln)))
916 nextchr = UCHARAT(locinput);
919 s = (char *) OPERAND(scan);
921 nextchr = UCHARAT(locinput);
922 if (!REGINCLASS(s, nextchr))
924 if (!nextchr && locinput >= PL_regeol)
926 nextchr = UCHARAT(++locinput);
929 PL_reg_flags |= RF_tainted;
934 if (!(OP(scan) == ALNUM
935 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
937 nextchr = UCHARAT(++locinput);
940 PL_reg_flags |= RF_tainted;
943 if (!nextchr && locinput >= PL_regeol)
945 if (OP(scan) == NALNUM
946 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
948 nextchr = UCHARAT(++locinput);
952 PL_reg_flags |= RF_tainted;
956 /* was last char in word? */
957 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
958 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
960 n = isALNUM(nextchr);
964 n = isALNUM_LC(nextchr);
966 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
970 PL_reg_flags |= RF_tainted;
973 if (!nextchr && locinput >= PL_regeol)
975 if (!(OP(scan) == SPACE
976 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
978 nextchr = UCHARAT(++locinput);
981 PL_reg_flags |= RF_tainted;
986 if (OP(scan) == SPACE
987 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
989 nextchr = UCHARAT(++locinput);
992 if (!isDIGIT(nextchr))
994 nextchr = UCHARAT(++locinput);
997 if (!nextchr && locinput >= PL_regeol)
999 if (isDIGIT(nextchr))
1001 nextchr = UCHARAT(++locinput);
1004 PL_reg_flags |= RF_tainted;
1008 n = ARG(scan); /* which paren pair */
1009 s = PL_regstartp[n];
1010 if (*PL_reglastparen < n || !s)
1011 sayNO; /* Do not match unless seen CLOSEn. */
1012 if (s == PL_regendp[n])
1014 /* Inline the first character, for speed. */
1015 if (UCHARAT(s) != nextchr &&
1017 (UCHARAT(s) != ((OP(scan) == REFF
1018 ? fold : fold_locale)[nextchr]))))
1020 ln = PL_regendp[n] - s;
1021 if (locinput + ln > PL_regeol)
1023 if (ln > 1 && (OP(scan) == REF
1024 ? memNE(s, locinput, ln)
1026 ? ibcmp(s, locinput, ln)
1027 : ibcmp_locale(s, locinput, ln))))
1030 nextchr = UCHARAT(locinput);
1041 OP_4tree *oop = PL_op;
1042 COP *ocurcop = PL_curcop;
1043 SV **ocurpad = PL_curpad;
1047 PL_op = (OP_4tree*)PL_regdata->data[n];
1048 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1049 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1051 CALLRUNOPS(); /* Scalar context. */
1060 sv_setsv(save_scalar(PL_replgv), ret);
1062 PL_curpad = ocurpad;
1063 PL_curcop = ocurcop;
1067 n = ARG(scan); /* which paren pair */
1068 PL_reg_start_tmp[n] = locinput;
1073 n = ARG(scan); /* which paren pair */
1074 PL_regstartp[n] = PL_reg_start_tmp[n];
1075 PL_regendp[n] = locinput;
1076 if (n > *PL_reglastparen)
1077 *PL_reglastparen = n;
1080 n = ARG(scan); /* which paren pair */
1081 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1085 next = NEXTOPER(NEXTOPER(scan));
1087 next = scan + ARG(scan);
1088 if (OP(next) == IFTHEN) /* Fake one. */
1089 next = NEXTOPER(NEXTOPER(next));
1097 CHECKPOINT cp = PL_savestack_ix;
1099 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1101 cc.oldcc = PL_regcc;
1103 cc.parenfloor = *PL_reglastparen;
1105 cc.min = ARG1(scan);
1106 cc.max = ARG2(scan);
1107 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1111 PL_reginput = locinput;
1112 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1114 PL_regcc = cc.oldcc;
1120 * This is really hard to understand, because after we match
1121 * what we're trying to match, we must make sure the rest of
1122 * the RE is going to match for sure, and to do that we have
1123 * to go back UP the parse tree by recursing ever deeper. And
1124 * if it fails, we have to reset our parent's current state
1125 * that we can try again after backing off.
1128 CHECKPOINT cp, lastcp;
1129 CURCUR* cc = PL_regcc;
1130 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1132 n = cc->cur + 1; /* how many we know we matched */
1133 PL_reginput = locinput;
1136 PerlIO_printf(Perl_debug_log,
1137 "%*s %ld out of %ld..%ld cc=%lx\n",
1138 REPORT_CODE_OFF+PL_regindent*2, "",
1139 (long)n, (long)cc->min,
1140 (long)cc->max, (long)cc)
1143 /* If degenerate scan matches "", assume scan done. */
1145 if (locinput == cc->lastloc && n >= cc->min) {
1146 PL_regcc = cc->oldcc;
1149 PerlIO_printf(Perl_debug_log,
1150 "%*s empty match detected, try continuation...\n",
1151 REPORT_CODE_OFF+PL_regindent*2, "")
1153 if (regmatch(cc->next))
1156 PerlIO_printf(Perl_debug_log,
1158 REPORT_CODE_OFF+PL_regindent*2, "")
1165 /* First just match a string of min scans. */
1169 cc->lastloc = locinput;
1170 if (regmatch(cc->scan))
1173 cc->lastloc = lastloc;
1175 PerlIO_printf(Perl_debug_log,
1177 REPORT_CODE_OFF+PL_regindent*2, "")
1182 /* Prefer next over scan for minimal matching. */
1185 PL_regcc = cc->oldcc;
1187 cp = regcppush(cc->parenfloor);
1189 if (regmatch(cc->next)) {
1191 sayYES; /* All done. */
1198 if (n >= cc->max) { /* Maximum greed exceeded? */
1199 if (PL_dowarn && n >= REG_INFTY
1200 && !(PL_reg_flags & RF_warned)) {
1201 PL_reg_flags |= RF_warned;
1202 warn("%s limit (%d) exceeded",
1203 "Complex regular subexpression recursion",
1210 PerlIO_printf(Perl_debug_log,
1211 "%*s trying longer...\n",
1212 REPORT_CODE_OFF+PL_regindent*2, "")
1214 /* Try scanning more and see if it helps. */
1215 PL_reginput = locinput;
1217 cc->lastloc = locinput;
1218 cp = regcppush(cc->parenfloor);
1220 if (regmatch(cc->scan)) {
1225 PerlIO_printf(Perl_debug_log,
1227 REPORT_CODE_OFF+PL_regindent*2, "")
1232 cc->lastloc = lastloc;
1236 /* Prefer scan over next for maximal matching. */
1238 if (n < cc->max) { /* More greed allowed? */
1239 cp = regcppush(cc->parenfloor);
1241 cc->lastloc = locinput;
1243 if (regmatch(cc->scan)) {
1248 regcppop(); /* Restore some previous $<digit>s? */
1249 PL_reginput = locinput;
1251 PerlIO_printf(Perl_debug_log,
1252 "%*s failed, try continuation...\n",
1253 REPORT_CODE_OFF+PL_regindent*2, "")
1256 if (PL_dowarn && n >= REG_INFTY && !(PL_reg_flags & RF_warned)) {
1257 PL_reg_flags |= RF_warned;
1258 warn("%s limit (%d) exceeded",
1259 "Complex regular subexpression recursion",
1263 /* Failed deeper matches of scan, so see if this one works. */
1264 PL_regcc = cc->oldcc;
1266 if (regmatch(cc->next))
1269 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1270 REPORT_CODE_OFF+PL_regindent*2, "")
1275 cc->lastloc = lastloc;
1280 next = scan + ARG(scan);
1283 inner = NEXTOPER(NEXTOPER(scan));
1286 inner = NEXTOPER(scan);
1291 if (OP(next) != c1) /* No choice. */
1292 next = inner; /* Avoid recursion. */
1294 int lastparen = *PL_reglastparen;
1298 PL_reginput = locinput;
1299 if (regmatch(inner))
1302 for (n = *PL_reglastparen; n > lastparen; n--)
1304 *PL_reglastparen = n;
1307 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1311 inner = NEXTOPER(scan);
1312 if (c1 == BRANCHJ) {
1313 inner = NEXTOPER(inner);
1315 } while (scan != NULL && OP(scan) == c1);
1329 /* We suppose that the next guy does not need
1330 backtracking: in particular, it is of constant length,
1331 and has no parenths to influence future backrefs. */
1332 ln = ARG1(scan); /* min to match */
1333 n = ARG2(scan); /* max to match */
1334 paren = scan->flags;
1336 if (paren > PL_regsize)
1338 if (paren > *PL_reglastparen)
1339 *PL_reglastparen = paren;
1341 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1343 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1344 PL_reginput = locinput;
1347 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1349 if (ln && l == 0 && n >= ln
1350 /* In fact, this is tricky. If paren, then the
1351 fact that we did/didnot match may influence
1352 future execution. */
1353 && !(paren && ln == 0))
1355 locinput = PL_reginput;
1356 if (regkind[(U8)OP(next)] == EXACT) {
1357 c1 = UCHARAT(OPERAND(next) + 1);
1358 if (OP(next) == EXACTF)
1360 else if (OP(next) == EXACTFL)
1361 c2 = fold_locale[c1];
1367 /* This may be improved if l == 0. */
1368 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1369 /* If it could work, try it. */
1371 UCHARAT(PL_reginput) == c1 ||
1372 UCHARAT(PL_reginput) == c2)
1376 PL_regstartp[paren] = PL_reginput - l;
1377 PL_regendp[paren] = PL_reginput;
1379 PL_regendp[paren] = NULL;
1385 /* Couldn't or didn't -- move forward. */
1386 PL_reginput = locinput;
1387 if (regrepeat_hard(scan, 1, &l)) {
1389 locinput = PL_reginput;
1395 n = regrepeat_hard(scan, n, &l);
1396 if (n != 0 && l == 0
1397 /* In fact, this is tricky. If paren, then the
1398 fact that we did/didnot match may influence
1399 future execution. */
1400 && !(paren && ln == 0))
1402 locinput = PL_reginput;
1404 PerlIO_printf(Perl_debug_log,
1405 "%*s matched %ld times, len=%ld...\n",
1406 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
1409 if (regkind[(U8)OP(next)] == EXACT) {
1410 c1 = UCHARAT(OPERAND(next) + 1);
1411 if (OP(next) == EXACTF)
1413 else if (OP(next) == EXACTFL)
1414 c2 = fold_locale[c1];
1422 /* If it could work, try it. */
1424 UCHARAT(PL_reginput) == c1 ||
1425 UCHARAT(PL_reginput) == c2)
1428 PerlIO_printf(Perl_debug_log,
1429 "%*s trying tail with n=%ld...\n",
1430 REPORT_CODE_OFF+PL_regindent*2, "", n)
1434 PL_regstartp[paren] = PL_reginput - l;
1435 PL_regendp[paren] = PL_reginput;
1437 PL_regendp[paren] = NULL;
1443 /* Couldn't or didn't -- back up. */
1446 PL_reginput = locinput;
1453 paren = scan->flags; /* Which paren to set */
1454 if (paren > PL_regsize)
1456 if (paren > *PL_reglastparen)
1457 *PL_reglastparen = paren;
1458 ln = ARG1(scan); /* min to match */
1459 n = ARG2(scan); /* max to match */
1460 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1464 ln = ARG1(scan); /* min to match */
1465 n = ARG2(scan); /* max to match */
1466 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1471 scan = NEXTOPER(scan);
1477 scan = NEXTOPER(scan);
1481 * Lookahead to avoid useless match attempts
1482 * when we know what character comes next.
1484 if (regkind[(U8)OP(next)] == EXACT) {
1485 c1 = UCHARAT(OPERAND(next) + 1);
1486 if (OP(next) == EXACTF)
1488 else if (OP(next) == EXACTFL)
1489 c2 = fold_locale[c1];
1495 PL_reginput = locinput;
1499 if (ln && regrepeat(scan, ln) < ln)
1502 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1503 /* If it could work, try it. */
1505 UCHARAT(PL_reginput) == c1 ||
1506 UCHARAT(PL_reginput) == c2)
1510 PL_regstartp[paren] = PL_reginput - 1;
1511 PL_regendp[paren] = PL_reginput;
1513 PL_regendp[paren] = NULL;
1519 /* Couldn't or didn't -- move forward. */
1520 PL_reginput = locinput + ln;
1521 if (regrepeat(scan, 1)) {
1523 PL_reginput = locinput + ln;
1530 n = regrepeat(scan, n);
1531 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1532 (!PL_multiline || OP(next) == SEOL))
1533 ln = n; /* why back off? */
1537 /* If it could work, try it. */
1539 UCHARAT(PL_reginput) == c1 ||
1540 UCHARAT(PL_reginput) == c2)
1544 PL_regstartp[paren] = PL_reginput - 1;
1545 PL_regendp[paren] = PL_reginput;
1547 PL_regendp[paren] = NULL;
1553 /* Couldn't or didn't -- back up. */
1555 PL_reginput = locinput + n;
1559 /* If it could work, try it. */
1561 UCHARAT(PL_reginput) == c1 ||
1562 UCHARAT(PL_reginput) == c2)
1568 /* Couldn't or didn't -- back up. */
1570 PL_reginput = locinput + n;
1577 if (locinput < PL_regtill)
1578 sayNO; /* Cannot match: too short. */
1581 PL_reginput = locinput; /* put where regtry can find it */
1582 sayYES; /* Success! */
1588 if (locinput < PL_bostr + scan->flags)
1593 if (locinput < PL_bostr + scan->flags)
1596 PL_reginput = locinput - scan->flags;
1597 inner = NEXTOPER(NEXTOPER(scan));
1598 if (regmatch(inner) != n) {
1612 if (OP(scan) == SUSPEND) {
1613 locinput = PL_reginput;
1614 nextchr = UCHARAT(locinput);
1619 next = scan + ARG(scan);
1624 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1625 (unsigned long)scan, OP(scan));
1626 FAIL("regexp memory corruption");
1632 * We get here only if there's trouble -- normally "case END" is
1633 * the terminating point.
1635 FAIL("corrupted regexp pointers");
1653 - regrepeat - repeatedly match something simple, report how many
1656 * [This routine now assumes that it will only match on things of length 1.
1657 * That was true before, but now we assume scan - reginput is the count,
1658 * rather than incrementing count on every character.]
1661 regrepeat(regnode *p, I32 max)
1664 register char *scan;
1665 register char *opnd;
1667 register char *loceol = PL_regeol;
1670 if (max != REG_INFTY && max < loceol - scan)
1671 loceol = scan + max;
1672 opnd = (char *) OPERAND(p);
1675 while (scan < loceol && *scan != '\n')
1681 case EXACT: /* length of string is 1 */
1682 c = UCHARAT(++opnd);
1683 while (scan < loceol && UCHARAT(scan) == c)
1686 case EXACTF: /* length of string is 1 */
1687 c = UCHARAT(++opnd);
1688 while (scan < loceol &&
1689 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1692 case EXACTFL: /* length of string is 1 */
1693 PL_reg_flags |= RF_tainted;
1694 c = UCHARAT(++opnd);
1695 while (scan < loceol &&
1696 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1700 while (scan < loceol && REGINCLASS(opnd, *scan))
1704 while (scan < loceol && isALNUM(*scan))
1708 PL_reg_flags |= RF_tainted;
1709 while (scan < loceol && isALNUM_LC(*scan))
1713 while (scan < loceol && !isALNUM(*scan))
1717 PL_reg_flags |= RF_tainted;
1718 while (scan < loceol && !isALNUM_LC(*scan))
1722 while (scan < loceol && isSPACE(*scan))
1726 PL_reg_flags |= RF_tainted;
1727 while (scan < loceol && isSPACE_LC(*scan))
1731 while (scan < loceol && !isSPACE(*scan))
1735 PL_reg_flags |= RF_tainted;
1736 while (scan < loceol && !isSPACE_LC(*scan))
1740 while (scan < loceol && isDIGIT(*scan))
1744 while (scan < loceol && !isDIGIT(*scan))
1747 default: /* Called on something of 0 width. */
1748 break; /* So match right here or not at all. */
1751 c = scan - PL_reginput;
1756 SV *prop = sv_newmortal();
1759 PerlIO_printf(Perl_debug_log,
1760 "%*s %s can match %ld times out of %ld...\n",
1761 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1768 - regrepeat_hard - repeatedly match something, report total lenth and length
1770 * The repeater is supposed to have constant length.
1774 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1777 register char *scan;
1778 register char *start;
1779 register char *loceol = PL_regeol;
1782 start = PL_reginput;
1783 while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) {
1785 *lp = l = PL_reginput - start;
1786 if (max != REG_INFTY && l*max < loceol - scan)
1787 loceol = scan + l*max;
1793 if (PL_reginput < loceol)
1798 return (scan - start)/l;
1802 - regclass - determine if a character falls into a character class
1806 reginclass(register char *p, register I32 c)
1813 if (ANYOF_TEST(p, c))
1815 else if (flags & ANYOF_FOLD) {
1817 if (flags & ANYOF_LOCALE) {
1818 PL_reg_flags |= RF_tainted;
1819 cf = fold_locale[c];
1823 if (ANYOF_TEST(p, cf))
1827 if (!match && (flags & ANYOF_ISA)) {
1828 PL_reg_flags |= RF_tainted;
1830 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1831 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1832 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1833 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1839 return (flags & ANYOF_INVERT) ? !match : match;