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.
24 * pregcomp and pregexec -- regsub and regerror are not used in perl
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
29 * Permission is granted to anyone to use this software for any
30 * purpose on any computer system, and to redistribute it freely,
31 * subject to the following restrictions:
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
43 **** Alterations to Henry's code are...
45 **** Copyright (c) 1991-1997, Larry Wall
47 **** You may distribute under the terms of either the GNU General Public
48 **** License or the Artistic License, as specified in the README file.
50 * Beware that some of this code is subtly aware of the way operator
51 * precedence is structured in regular expressions. Serious changes in
52 * regular-expression syntax might require a total rethink.
58 #define RF_tainted 1 /* tainted information used? */
59 #define RF_warned 2 /* warned about big count? */
60 #define RF_evaled 4 /* Did an EVAL? */
67 typedef I32 CHECKPOINT;
73 static I32 regmatch _((regnode *prog));
74 static I32 regrepeat _((regnode *p, I32 max));
75 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
76 static I32 regtry _((regexp *prog, char *startpos));
78 static bool reginclass _((char *p, I32 c));
79 static CHECKPOINT regcppush _((I32 parenfloor));
80 static char * regcppop _((void));
82 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
85 regcppush(I32 parenfloor)
88 int retval = savestack_ix;
89 int i = (regsize - parenfloor) * 4;
93 for (p = regsize; p > parenfloor; p--) {
94 SSPUSHPTR(regendp[p]);
95 SSPUSHPTR(regstartp[p]);
96 SSPUSHPTR(reg_start_tmp[p]);
100 SSPUSHINT(*reglastparen);
103 SSPUSHINT(SAVEt_REGCONTEXT);
107 /* These are needed since we do not localize EVAL nodes: */
108 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
109 # define REGCP_UNWIND DEBUG_r(lastcp != savestack_ix ? PerlIO_printf(Perl_debug_log," Clearing an EVAL scope, savestack=%i..%i\n", lastcp, savestack_ix) : 0); regcpblow(lastcp)
119 assert(i == SAVEt_REGCONTEXT);
121 input = (char *) SSPOPPTR;
122 *reglastparen = SSPOPINT;
124 for (i -= 3; i > 0; i -= 4) {
125 paren = (U32)SSPOPINT;
126 reg_start_tmp[paren] = (char *) SSPOPPTR;
127 regstartp[paren] = (char *) SSPOPPTR;
128 tmps = (char*)SSPOPPTR;
129 if (paren <= *reglastparen)
130 regendp[paren] = tmps;
132 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
133 paren, regstartp[paren] - regbol,
134 reg_start_tmp[paren] - regbol,
135 regendp[paren] - regbol,
136 (paren > *reglastparen ? "(no)" : ""));
140 if (*reglastparen + 1 <= regnpar) {
141 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
142 *reglastparen + 1, regnpar);
145 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
147 regstartp[paren] = Nullch;
148 regendp[paren] = Nullch;
153 #define regcpblow(cp) LEAVE_SCOPE(cp)
156 * pregexec and friends
160 - pregexec - match a regexp against a string
163 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
164 /* strend: pointer to null at end of string */
165 /* strbeg: real beginning of string */
166 /* minend: end of match must be >=minend after stringarg. */
167 /* nosave: For optimizations. */
170 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
171 nosave ? 0 : REXEC_COPY_STR);
175 - regexec_flags - match a regexp against a string
178 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
179 /* strend: pointer to null at end of string */
180 /* strbeg: real beginning of string */
181 /* minend: end of match must be >=minend after stringarg. */
182 /* data: May be used for some additional optimizations. */
183 /* nosave: For optimizations. */
187 register char *startpos = stringarg;
189 I32 minlen; /* must match at least this many chars */
190 I32 dontbother = 0; /* how many characters not to try at end */
192 I32 start_shift = 0; /* Offset of the start to find
194 I32 end_shift = 0; /* Same for the end. */
195 I32 scream_pos = -1; /* Internal iterator of scream. */
202 regprecomp = prog->precomp; /* Needed for error messages. */
204 regnarrate = debug & 512;
205 regprogram = prog->program;
209 if (prog == NULL || startpos == NULL) {
210 croak("NULL regexp parameter");
214 minlen = prog->minlen;
215 if (strend - startpos < minlen) goto phooey;
217 if (startpos == strbeg) /* is ^ valid at stringarg? */
220 regprev = stringarg[-1];
221 if (!multiline && regprev == '\n')
222 regprev = '\0'; /* force ^ to NOT match */
225 /* Check validity of program. */
226 if (UCHARAT(prog->program) != MAGIC) {
227 FAIL("corrupted regexp program");
230 regnpar = prog->nparens;
234 /* If there is a "must appear" string, look for it. */
236 if (!(flags & REXEC_CHECKED)
237 && prog->check_substr != Nullsv &&
238 !(prog->reganch & ROPT_ANCH_GPOS) &&
239 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
240 || (multiline && prog->check_substr == prog->anchored_substr)) )
242 start_shift = prog->check_offset_min;
243 /* Should be nonnegative! */
244 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
246 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
247 s = screaminstr(screamer, prog->check_substr,
248 start_shift + (stringarg - strbeg),
249 end_shift, &scream_pos, 0);
255 s = fbm_instr((unsigned char*)s + start_shift,
256 (unsigned char*)strend - end_shift,
259 ++BmUSEFUL(prog->check_substr); /* hooray */
260 goto phooey; /* not present */
261 } else if ((s - stringarg) > prog->check_offset_max) {
262 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
263 s -= prog->check_offset_max;
264 } else if (!prog->naughty
265 && --BmUSEFUL(prog->check_substr) < 0
266 && prog->check_substr == prog->float_substr) { /* boo */
267 SvREFCNT_dec(prog->check_substr);
268 prog->check_substr = Nullsv; /* disable */
269 prog->float_substr = Nullsv; /* clear */
274 /* Mark beginning of line for ^ and lookbehind. */
278 /* Mark end of line for $ (and such) */
281 /* see how far we have to get to not match where we matched before */
282 regtill = startpos+minend;
285 PerlIO_printf(Perl_debug_log,
286 "Matching `%.60s%s' against `%.*s%s'\n",
288 (strlen(prog->precomp) > 60 ? "..." : ""),
289 (strend - startpos > 60 ? 60 : strend - startpos),
291 (strend - startpos > 60 ? "..." : ""))
294 /* Simplest case: anchored match need be tried only once. */
295 /* [unless only anchor is BOL and multiline is set] */
296 if (prog->reganch & ROPT_ANCH) {
297 if (regtry(prog, startpos))
299 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
300 (multiline || (prog->reganch & ROPT_IMPLICIT)
301 || (prog->reganch & ROPT_ANCH_MBOL)))
304 dontbother = minlen - 1;
305 strend -= dontbother;
306 /* for multiline we only have to try after newlines */
311 if (s < strend && regtry(prog, s))
319 /* Messy cases: unanchored match. */
320 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
321 /* we have /x+whatever/ */
322 /* it must be a one character string */
323 char ch = SvPVX(prog->anchored_substr)[0];
326 if (regtry(prog, s)) goto got_it;
328 while (s < strend && *s == ch)
335 else if (prog->anchored_substr != Nullsv
336 || (prog->float_substr != Nullsv
337 && prog->float_max_offset < strend - s)) {
338 SV *must = prog->anchored_substr
339 ? prog->anchored_substr : prog->float_substr;
341 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
343 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
344 I32 delta = back_max - back_min;
345 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
346 char *last1 = s - 1; /* Last position checked before */
348 /* XXXX check_substr already used to find `s', can optimize if
349 check_substr==must. */
351 dontbother = end_shift;
352 strend -= dontbother;
353 while ( (s <= last) &&
355 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
356 end_shift, &scream_pos, 0))
357 : (s = fbm_instr((unsigned char*)s + back_min,
358 (unsigned char*)strend, must))) ) {
359 if (s - back_max > last1) {
360 last1 = s - back_min;
365 last1 = s - back_min;
375 } else if (c = prog->regstclass) {
376 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
380 dontbother = minlen - 1;
381 strend -= dontbother; /* don't bother with what can't match */
383 /* We know what class it must start with. */
386 Class = (char *) OPERAND(c);
388 if (REGINCLASS(Class, *s)) {
389 if (tmp && regtry(prog, s))
400 reg_flags |= RF_tainted;
404 dontbother++,strend--;
405 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
406 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
408 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
415 if ((minlen || tmp) && regtry(prog,s))
419 reg_flags |= RF_tainted;
423 dontbother++,strend--;
424 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
425 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
427 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
429 else if (regtry(prog, s))
433 if ((minlen || !tmp) && regtry(prog,s))
439 if (tmp && regtry(prog, s))
450 reg_flags |= RF_tainted;
452 if (isALNUM_LC(*s)) {
453 if (tmp && regtry(prog, s))
466 if (tmp && regtry(prog, s))
477 reg_flags |= RF_tainted;
479 if (!isALNUM_LC(*s)) {
480 if (tmp && regtry(prog, s))
493 if (tmp && regtry(prog, s))
504 reg_flags |= RF_tainted;
506 if (isSPACE_LC(*s)) {
507 if (tmp && regtry(prog, s))
520 if (tmp && regtry(prog, s))
531 reg_flags |= RF_tainted;
533 if (!isSPACE_LC(*s)) {
534 if (tmp && regtry(prog, s))
547 if (tmp && regtry(prog, s))
560 if (tmp && regtry(prog, s))
574 if (prog->float_substr != Nullsv) { /* Trim the end. */
576 I32 oldpos = scream_pos;
579 last = screaminstr(screamer, prog->float_substr, s - strbeg,
580 end_shift, &scream_pos, 1); /* last one */
582 last = scream_olds; /* Only one occurence. */
586 char *little = SvPV(prog->float_substr, len);
587 last = rninstr(s, strend, little, little + len);
589 if (last == NULL) goto phooey; /* Should not happen! */
590 dontbother = strend - last - 1;
592 if (minlen && (dontbother < minlen))
593 dontbother = minlen - 1;
594 strend -= dontbother;
595 /* We don't know much -- general case. */
599 } while (s++ < strend);
606 strend += dontbother; /* uncheat */
607 prog->subbeg = strbeg;
608 prog->subend = strend;
609 RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
611 /* make sure $`, $&, $', and $digit will work later */
612 if (strbeg != prog->subbase) { /* second+ //g match. */
613 if (!(flags & REXEC_COPY_STR)) {
615 Safefree(prog->subbase);
616 prog->subbase = Nullch;
620 I32 i = strend - startpos + (stringarg - strbeg);
621 s = savepvn(strbeg, i);
622 Safefree(prog->subbase);
624 prog->subbeg = prog->subbase;
625 prog->subend = prog->subbase + i;
626 s = prog->subbase + (stringarg - strbeg);
627 for (i = 0; i <= prog->nparens; i++) {
629 prog->startp[i] = s + (prog->startp[i] - startpos);
630 prog->endp[i] = s + (prog->endp[i] - startpos);
642 - regtry - try match at specific point
644 STATIC I32 /* 0 failure, 1 success */
645 regtry(regexp *prog, char *startpos)
654 regstartp = prog->startp;
655 regendp = prog->endp;
656 reglastparen = &prog->lastparen;
659 if (reg_start_tmpl <= prog->nparens) {
660 reg_start_tmpl = prog->nparens*3/2 + 3;
662 Renew(reg_start_tmp, reg_start_tmpl, char*);
664 New(22,reg_start_tmp, reg_start_tmpl, char*);
669 regdata = prog->data;
671 for (i = prog->nparens; i >= 0; i--) {
677 if (regmatch(prog->program + 1) && reginput >= regtill) {
678 prog->startp[0] = startpos;
679 prog->endp[0] = reginput;
687 - regmatch - main matching routine
689 * Conceptually the strategy is simple: check to see whether the current
690 * node matches, call self recursively to see whether the rest matches,
691 * and then act accordingly. In practice we make some effort to avoid
692 * recursion, in particular by going through "ordinary" nodes (that don't
693 * need to know whether the rest of the match failed) by a loop instead of
696 /* [lwall] I've hoisted the register declarations to the outer block in order to
697 * maybe save a little bit of pushing and popping on the stack. It also takes
698 * advantage of machines that use a register save mask on subroutine entry.
700 STATIC I32 /* 0 failure, 1 success */
701 regmatch(regnode *prog)
704 register regnode *scan; /* Current node. */
705 regnode *next; /* Next node. */
706 regnode *inner; /* Next node in internal branch. */
707 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
708 register I32 n; /* no or next */
709 register I32 ln; /* len or last */
710 register char *s; /* operand or save */
711 register char *locinput = reginput;
712 register I32 c1, c2, paren; /* case fold search, parenth */
713 int minmod = 0, sw = 0, logical = 0;
718 nextchr = UCHARAT(locinput);
720 while (scan != NULL) {
721 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
723 # define sayYES goto yes
724 # define sayNO goto no
725 # define saySAME(x) if (x) goto yes; else goto no
726 # define REPORT_CODE_OFF 24
728 # define sayYES return 1
729 # define sayNO return 0
730 # define saySAME(x) return x
733 SV *prop = sv_newmortal();
734 int docolor = *colors[0];
735 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
736 int l = (regeol - locinput > taill ? taill : regeol - locinput);
737 int pref_len = (locinput - bostr > (5 + taill) - l
738 ? (5 + taill) - l : locinput - bostr);
740 if (l + pref_len < (5 + taill) && l < regeol - locinput)
741 l = ( regeol - locinput > (5 + taill) - pref_len
742 ? (5 + taill) - pref_len : regeol - locinput);
744 PerlIO_printf(Perl_debug_log,
745 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
747 colors[2], pref_len, locinput - pref_len, colors[3],
748 (docolor ? "" : "> <"),
749 colors[0], l, locinput, colors[1],
750 15 - l - pref_len + 1,
752 regindent*2, "", scan - regprogram,
757 next = scan + NEXT_OFF(scan);
761 next = regnext(scan);
766 if (locinput == regbol
769 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
771 /* regtill = regbol; */
776 if (locinput == regbol
778 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
784 if (locinput == regbol && regprev == '\n')
788 if (locinput == regbol)
798 if ((nextchr || locinput < regeol) && nextchr != '\n')
803 if ((nextchr || locinput < regeol) && nextchr != '\n')
805 if (regeol - locinput > 1)
809 if (!nextchr && locinput >= regeol)
811 nextchr = UCHARAT(++locinput);
814 if (!nextchr && locinput >= regeol || nextchr == '\n')
816 nextchr = UCHARAT(++locinput);
819 s = (char *) OPERAND(scan);
821 /* Inline the first character, for speed. */
822 if (UCHARAT(s) != nextchr)
824 if (regeol - locinput < ln)
826 if (ln > 1 && memNE(s, locinput, ln))
829 nextchr = UCHARAT(locinput);
832 reg_flags |= RF_tainted;
835 s = (char *) OPERAND(scan);
837 /* Inline the first character, for speed. */
838 if (UCHARAT(s) != nextchr &&
839 UCHARAT(s) != ((OP(scan) == EXACTF)
840 ? fold : fold_locale)[nextchr])
842 if (regeol - locinput < ln)
844 if (ln > 1 && (OP(scan) == EXACTF
845 ? ibcmp(s, locinput, ln)
846 : ibcmp_locale(s, locinput, ln)))
849 nextchr = UCHARAT(locinput);
852 s = (char *) OPERAND(scan);
854 nextchr = UCHARAT(locinput);
855 if (!REGINCLASS(s, nextchr))
857 if (!nextchr && locinput >= regeol)
859 nextchr = UCHARAT(++locinput);
862 reg_flags |= RF_tainted;
867 if (!(OP(scan) == ALNUM
868 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
870 nextchr = UCHARAT(++locinput);
873 reg_flags |= RF_tainted;
876 if (!nextchr && locinput >= regeol)
878 if (OP(scan) == NALNUM
879 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
881 nextchr = UCHARAT(++locinput);
885 reg_flags |= RF_tainted;
889 /* was last char in word? */
890 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
891 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
893 n = isALNUM(nextchr);
897 n = isALNUM_LC(nextchr);
899 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
903 reg_flags |= RF_tainted;
906 if (!nextchr && locinput >= regeol)
908 if (!(OP(scan) == SPACE
909 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
911 nextchr = UCHARAT(++locinput);
914 reg_flags |= RF_tainted;
919 if (OP(scan) == SPACE
920 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
922 nextchr = UCHARAT(++locinput);
925 if (!isDIGIT(nextchr))
927 nextchr = UCHARAT(++locinput);
930 if (!nextchr && locinput >= regeol)
932 if (isDIGIT(nextchr))
934 nextchr = UCHARAT(++locinput);
937 reg_flags |= RF_tainted;
941 n = ARG(scan); /* which paren pair */
943 if (*reglastparen < n || !s)
944 sayNO; /* Do not match unless seen CLOSEn. */
947 /* Inline the first character, for speed. */
948 if (UCHARAT(s) != nextchr &&
950 (UCHARAT(s) != ((OP(scan) == REFF
951 ? fold : fold_locale)[nextchr]))))
954 if (locinput + ln > regeol)
956 if (ln > 1 && (OP(scan) == REF
957 ? memNE(s, locinput, ln)
959 ? ibcmp(s, locinput, ln)
960 : ibcmp_locale(s, locinput, ln))))
963 nextchr = UCHARAT(locinput);
975 COP *ocurcop = curcop;
976 SV **ocurpad = curpad;
980 op = (OP_4tree*)regdata->data[n];
981 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
982 curpad = AvARRAY((AV*)regdata->data[n + 1]);
984 /* Preserve whatever is on stack now, otherwise
985 OP_NEXTSTATE will overwrite it. */
986 SAVEINT(reg_eval_set); /* Protect against unwinding. */
989 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
991 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
992 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
993 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
995 /* Apparently this is not needed, judging by wantarray. */
996 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
997 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1000 CALLRUNOPS(); /* Scalar context. */
1015 n = ARG(scan); /* which paren pair */
1016 reg_start_tmp[n] = locinput;
1021 n = ARG(scan); /* which paren pair */
1022 regstartp[n] = reg_start_tmp[n];
1023 regendp[n] = locinput;
1024 if (n > *reglastparen)
1028 n = ARG(scan); /* which paren pair */
1029 sw = (*reglastparen >= n && regendp[n] != NULL);
1033 next = NEXTOPER(NEXTOPER(scan));
1035 next = scan + ARG(scan);
1036 if (OP(next) == IFTHEN) /* Fake one. */
1037 next = NEXTOPER(NEXTOPER(next));
1045 CHECKPOINT cp = savestack_ix;
1047 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1051 cc.parenfloor = *reglastparen;
1053 cc.min = ARG1(scan);
1054 cc.max = ARG2(scan);
1055 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1059 reginput = locinput;
1060 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1068 * This is really hard to understand, because after we match
1069 * what we're trying to match, we must make sure the rest of
1070 * the RE is going to match for sure, and to do that we have
1071 * to go back UP the parse tree by recursing ever deeper. And
1072 * if it fails, we have to reset our parent's current state
1073 * that we can try again after backing off.
1076 CHECKPOINT cp, lastcp;
1078 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1080 n = cc->cur + 1; /* how many we know we matched */
1081 reginput = locinput;
1084 PerlIO_printf(Perl_debug_log,
1085 "%*s %ld out of %ld..%ld cc=%lx\n",
1086 REPORT_CODE_OFF+regindent*2, "",
1087 (long)n, (long)cc->min,
1088 (long)cc->max, (long)cc)
1091 /* If degenerate scan matches "", assume scan done. */
1093 if (locinput == cc->lastloc && n >= cc->min) {
1097 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1099 if (regmatch(cc->next))
1102 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1109 /* First just match a string of min scans. */
1113 cc->lastloc = locinput;
1114 if (regmatch(cc->scan))
1117 cc->lastloc = lastloc;
1119 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1124 /* Prefer next over scan for minimal matching. */
1129 cp = regcppush(cc->parenfloor);
1131 if (regmatch(cc->next)) {
1133 sayYES; /* All done. */
1140 if (n >= cc->max) { /* Maximum greed exceeded? */
1141 if (dowarn && n >= REG_INFTY
1142 && !(reg_flags & RF_warned)) {
1143 reg_flags |= RF_warned;
1144 warn("count exceeded %d", REG_INFTY - 1);
1150 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1152 /* Try scanning more and see if it helps. */
1153 reginput = locinput;
1155 cc->lastloc = locinput;
1156 cp = regcppush(cc->parenfloor);
1158 if (regmatch(cc->scan)) {
1163 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1168 cc->lastloc = lastloc;
1172 /* Prefer scan over next for maximal matching. */
1174 if (n < cc->max) { /* More greed allowed? */
1175 cp = regcppush(cc->parenfloor);
1177 cc->lastloc = locinput;
1179 if (regmatch(cc->scan)) {
1184 regcppop(); /* Restore some previous $<digit>s? */
1185 reginput = locinput;
1187 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1190 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1191 reg_flags |= RF_warned;
1192 warn("count exceeded %d", REG_INFTY - 1);
1195 /* Failed deeper matches of scan, so see if this one works. */
1198 if (regmatch(cc->next))
1201 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1206 cc->lastloc = lastloc;
1211 next = scan + ARG(scan);
1214 inner = NEXTOPER(NEXTOPER(scan));
1217 inner = NEXTOPER(scan);
1222 if (OP(next) != c1) /* No choice. */
1223 next = inner; /* Avoid recursion. */
1225 int lastparen = *reglastparen;
1229 reginput = locinput;
1230 if (regmatch(inner))
1233 for (n = *reglastparen; n > lastparen; n--)
1239 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1244 next = regnext(next);
1246 inner = NEXTOPER(scan);
1247 if (c1 == BRANCHJ) {
1248 inner = NEXTOPER(inner);
1250 } while (scan != NULL && OP(scan) == c1);
1264 /* We suppose that the next guy does not need
1265 backtracking: in particular, it is of constant length,
1266 and has no parenths to influence future backrefs. */
1267 ln = ARG1(scan); /* min to match */
1268 n = ARG2(scan); /* max to match */
1269 #ifdef REGALIGN_STRUCT
1270 paren = scan->flags;
1272 if (paren > regsize)
1274 if (paren > *reglastparen)
1275 *reglastparen = paren;
1278 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1280 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1281 reginput = locinput;
1284 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1286 if (l == 0 && n >= ln
1287 /* In fact, this is tricky. If paren, then the
1288 fact that we did/didnot match may influence
1289 future execution. */
1290 && !(paren && ln == 0))
1292 locinput = reginput;
1293 if (regkind[(U8)OP(next)] == EXACT) {
1294 c1 = UCHARAT(OPERAND(next) + 1);
1295 if (OP(next) == EXACTF)
1297 else if (OP(next) == EXACTFL)
1298 c2 = fold_locale[c1];
1304 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1305 /* If it could work, try it. */
1307 UCHARAT(reginput) == c1 ||
1308 UCHARAT(reginput) == c2)
1312 regstartp[paren] = reginput - l;
1313 regendp[paren] = reginput;
1315 regendp[paren] = NULL;
1321 /* Couldn't or didn't -- move forward. */
1322 reginput = locinput;
1323 if (regrepeat_hard(scan, 1, &l)) {
1325 locinput = reginput;
1331 n = regrepeat_hard(scan, n, &l);
1332 if (n != 0 && l == 0
1333 /* In fact, this is tricky. If paren, then the
1334 fact that we did/didnot match may influence
1335 future execution. */
1336 && !(paren && ln == 0))
1338 locinput = reginput;
1340 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1343 if (regkind[(U8)OP(next)] == EXACT) {
1344 c1 = UCHARAT(OPERAND(next) + 1);
1345 if (OP(next) == EXACTF)
1347 else if (OP(next) == EXACTFL)
1348 c2 = fold_locale[c1];
1356 /* If it could work, try it. */
1358 UCHARAT(reginput) == c1 ||
1359 UCHARAT(reginput) == c2)
1362 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1366 regstartp[paren] = reginput - l;
1367 regendp[paren] = reginput;
1369 regendp[paren] = NULL;
1375 /* Couldn't or didn't -- back up. */
1378 reginput = locinput;
1385 paren = scan->flags; /* Which paren to set */
1386 if (paren > regsize)
1388 if (paren > *reglastparen)
1389 *reglastparen = paren;
1390 ln = ARG1(scan); /* min to match */
1391 n = ARG2(scan); /* max to match */
1392 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1396 ln = ARG1(scan); /* min to match */
1397 n = ARG2(scan); /* max to match */
1398 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1403 scan = NEXTOPER(scan);
1409 scan = NEXTOPER(scan);
1413 * Lookahead to avoid useless match attempts
1414 * when we know what character comes next.
1416 if (regkind[(U8)OP(next)] == EXACT) {
1417 c1 = UCHARAT(OPERAND(next) + 1);
1418 if (OP(next) == EXACTF)
1420 else if (OP(next) == EXACTFL)
1421 c2 = fold_locale[c1];
1427 reginput = locinput;
1431 if (ln && regrepeat(scan, ln) < ln)
1434 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1435 /* If it could work, try it. */
1437 UCHARAT(reginput) == c1 ||
1438 UCHARAT(reginput) == c2)
1442 regstartp[paren] = reginput - 1;
1443 regendp[paren] = reginput;
1445 regendp[paren] = NULL;
1451 /* Couldn't or didn't -- move forward. */
1452 reginput = locinput + ln;
1453 if (regrepeat(scan, 1)) {
1455 reginput = locinput + ln;
1462 n = regrepeat(scan, n);
1463 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1464 (!multiline || OP(next) == SEOL))
1465 ln = n; /* why back off? */
1469 /* If it could work, try it. */
1471 UCHARAT(reginput) == c1 ||
1472 UCHARAT(reginput) == c2)
1476 regstartp[paren] = reginput - 1;
1477 regendp[paren] = reginput;
1479 regendp[paren] = NULL;
1485 /* Couldn't or didn't -- back up. */
1487 reginput = locinput + n;
1491 /* If it could work, try it. */
1493 UCHARAT(reginput) == c1 ||
1494 UCHARAT(reginput) == c2)
1500 /* Couldn't or didn't -- back up. */
1502 reginput = locinput + n;
1510 reginput = locinput; /* put where regtry can find it */
1511 sayYES; /* Success! */
1517 if (locinput < bostr + scan->flags)
1522 if (locinput < bostr + scan->flags)
1525 reginput = locinput - scan->flags;
1526 inner = NEXTOPER(NEXTOPER(scan));
1527 if (regmatch(inner) != n) {
1541 if (OP(scan) == SUSPEND) {
1542 locinput = reginput;
1543 nextchr = UCHARAT(locinput);
1548 next = scan + ARG(scan);
1553 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1554 (unsigned long)scan, OP(scan));
1555 FAIL("regexp memory corruption");
1561 * We get here only if there's trouble -- normally "case END" is
1562 * the terminating point.
1564 FAIL("corrupted regexp pointers");
1582 - regrepeat - repeatedly match something simple, report how many
1585 * [This routine now assumes that it will only match on things of length 1.
1586 * That was true before, but now we assume scan - reginput is the count,
1587 * rather than incrementing count on every character.]
1590 regrepeat(regnode *p, I32 max)
1592 register char *scan;
1593 register char *opnd;
1595 register char *loceol = regeol;
1598 if (max != REG_INFTY && max < loceol - scan)
1599 loceol = scan + max;
1600 opnd = (char *) OPERAND(p);
1603 while (scan < loceol && *scan != '\n')
1609 case EXACT: /* length of string is 1 */
1610 c = UCHARAT(++opnd);
1611 while (scan < loceol && UCHARAT(scan) == c)
1614 case EXACTF: /* length of string is 1 */
1615 c = UCHARAT(++opnd);
1616 while (scan < loceol &&
1617 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1620 case EXACTFL: /* length of string is 1 */
1621 reg_flags |= RF_tainted;
1622 c = UCHARAT(++opnd);
1623 while (scan < loceol &&
1624 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1628 while (scan < loceol && REGINCLASS(opnd, *scan))
1632 while (scan < loceol && isALNUM(*scan))
1636 reg_flags |= RF_tainted;
1637 while (scan < loceol && isALNUM_LC(*scan))
1641 while (scan < loceol && !isALNUM(*scan))
1645 reg_flags |= RF_tainted;
1646 while (scan < loceol && !isALNUM_LC(*scan))
1650 while (scan < loceol && isSPACE(*scan))
1654 reg_flags |= RF_tainted;
1655 while (scan < loceol && isSPACE_LC(*scan))
1659 while (scan < loceol && !isSPACE(*scan))
1663 reg_flags |= RF_tainted;
1664 while (scan < loceol && !isSPACE_LC(*scan))
1668 while (scan < loceol && isDIGIT(*scan))
1672 while (scan < loceol && !isDIGIT(*scan))
1675 default: /* Called on something of 0 width. */
1676 break; /* So match right here or not at all. */
1679 c = scan - reginput;
1684 SV *prop = sv_newmortal();
1687 PerlIO_printf(Perl_debug_log,
1688 "%*s %s can match %ld times out of %ld...\n",
1689 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1696 - regrepeat_hard - repeatedly match something, report total lenth and length
1698 * The repeater is supposed to have constant length.
1702 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1704 register char *scan;
1705 register char *start;
1706 register char *loceol = regeol;
1710 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1712 *lp = l = reginput - start;
1713 if (max != REG_INFTY && l*max < loceol - scan)
1714 loceol = scan + l*max;
1720 if (reginput < loceol)
1725 return (scan - start)/l;
1729 - regclass - determine if a character falls into a character class
1733 reginclass(register char *p, register I32 c)
1739 if (ANYOF_TEST(p, c))
1741 else if (flags & ANYOF_FOLD) {
1743 if (flags & ANYOF_LOCALE) {
1744 reg_flags |= RF_tainted;
1745 cf = fold_locale[c];
1749 if (ANYOF_TEST(p, cf))
1753 if (!match && (flags & ANYOF_ISA)) {
1754 reg_flags |= RF_tainted;
1756 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1757 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1758 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1759 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1765 return (flags & ANYOF_INVERT) ? !match : match;