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-1998, 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.
79 #define RF_tainted 1 /* tainted information used? */
80 #define RF_warned 2 /* warned about big count? */
81 #define RF_evaled 4 /* Did an EVAL with setting? */
82 #define RF_utf8 8 /* String contains multibyte chars? */
84 #define UTF (PL_reg_flags & RF_utf8)
86 #define RS_init 1 /* eval environment created */
87 #define RS_set 2 /* replsv value is set */
94 typedef I32 CHECKPOINT;
100 static I32 regmatch _((regnode *prog));
101 static I32 regrepeat _((regnode *p, I32 max));
102 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
103 static I32 regtry _((regexp *prog, char *startpos));
105 static bool reginclass _((char *p, I32 c));
106 static bool reginclassutf8 _((regnode *f, U8* p));
107 static CHECKPOINT regcppush _((I32 parenfloor));
108 static char * regcppop _((void));
109 static char * regcp_set_to _((I32 ss));
110 static void cache_re _((regexp *prog));
113 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
114 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
116 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
117 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
120 static U8 * reghop _((U8 *pos, I32 off));
121 static U8 * reghopmaybe _((U8 *pos, I32 off));
123 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
124 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
125 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
126 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
127 #define HOPc(pos,off) ((char*)HOP(pos,off))
128 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
131 regcppush(I32 parenfloor)
134 int retval = PL_savestack_ix;
135 int i = (PL_regsize - parenfloor) * 4;
139 for (p = PL_regsize; p > parenfloor; p--) {
140 SSPUSHPTR(PL_regendp[p]);
141 SSPUSHPTR(PL_regstartp[p]);
142 SSPUSHPTR(PL_reg_start_tmp[p]);
145 SSPUSHINT(PL_regsize);
146 SSPUSHINT(*PL_reglastparen);
147 SSPUSHPTR(PL_reginput);
149 SSPUSHINT(SAVEt_REGCONTEXT);
153 /* These are needed since we do not localize EVAL nodes: */
154 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
155 " Setting an EVAL scope, savestack=%i\n", \
156 PL_savestack_ix)); lastcp = PL_savestack_ix
158 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
159 PerlIO_printf(Perl_debug_log, \
160 " Clearing an EVAL scope, savestack=%i..%i\n", \
161 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
171 assert(i == SAVEt_REGCONTEXT);
173 input = (char *) SSPOPPTR;
174 *PL_reglastparen = SSPOPINT;
175 PL_regsize = SSPOPINT;
176 for (i -= 3; i > 0; i -= 4) {
177 paren = (U32)SSPOPINT;
178 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
179 PL_regstartp[paren] = (char *) SSPOPPTR;
180 tmps = (char*)SSPOPPTR;
181 if (paren <= *PL_reglastparen)
182 PL_regendp[paren] = tmps;
184 PerlIO_printf(Perl_debug_log,
185 " restoring \\%d to %d(%d)..%d%s\n",
186 paren, PL_regstartp[paren] - PL_regbol,
187 PL_reg_start_tmp[paren] - PL_regbol,
188 PL_regendp[paren] - PL_regbol,
189 (paren > *PL_reglastparen ? "(no)" : ""));
193 if (*PL_reglastparen + 1 <= PL_regnpar) {
194 PerlIO_printf(Perl_debug_log,
195 " restoring \\%d..\\%d to undef\n",
196 *PL_reglastparen + 1, PL_regnpar);
199 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
200 if (paren > PL_regsize)
201 PL_regstartp[paren] = Nullch;
202 PL_regendp[paren] = Nullch;
211 I32 tmp = PL_savestack_ix;
213 PL_savestack_ix = ss;
215 PL_savestack_ix = tmp;
219 typedef struct re_cc_state
223 struct re_cc_state *prev;
228 #define regcpblow(cp) LEAVE_SCOPE(cp)
231 * pregexec and friends
235 - pregexec - match a regexp against a string
238 pregexec(register regexp *prog, char *stringarg, register char *strend,
239 char *strbeg, I32 minend, SV *screamer, U32 nosave)
240 /* strend: pointer to null at end of string */
241 /* strbeg: real beginning of string */
242 /* minend: end of match must be >=minend after stringarg. */
243 /* nosave: For optimizations. */
246 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
247 nosave ? 0 : REXEC_COPY_STR);
251 cache_re(regexp *prog)
254 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
256 PL_regprogram = prog->program;
258 PL_regnpar = prog->nparens;
259 PL_regdata = prog->data;
264 - regexec_flags - match a regexp against a string
267 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
268 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
269 /* strend: pointer to null at end of string */
270 /* strbeg: real beginning of string */
271 /* minend: end of match must be >=minend after stringarg. */
272 /* data: May be used for some additional optimizations. */
273 /* nosave: For optimizations. */
278 register char *startpos = stringarg;
280 I32 minlen; /* must match at least this many chars */
281 I32 dontbother = 0; /* how many characters not to try at end */
283 I32 start_shift = 0; /* Offset of the start to find
284 constant substr. */ /* CC */
285 I32 end_shift = 0; /* Same for the end. */ /* CC */
286 I32 scream_pos = -1; /* Internal iterator of scream. */
288 SV* oreplsv = GvSV(PL_replgv);
296 PL_regnarrate = PL_debug & 512;
300 if (prog == NULL || startpos == NULL) {
301 croak("NULL regexp parameter");
305 minlen = prog->minlen;
306 if (strend - startpos < minlen) goto phooey;
308 if (startpos == strbeg) /* is ^ valid at stringarg? */
311 PL_regprev = (U32)stringarg[-1];
312 if (!PL_multiline && PL_regprev == '\n')
313 PL_regprev = '\0'; /* force ^ to NOT match */
316 /* Check validity of program. */
317 if (UCHARAT(prog->program) != REG_MAGIC) {
318 FAIL("corrupted regexp program");
324 if (prog->reganch & ROPT_UTF8)
325 PL_reg_flags |= RF_utf8;
327 /* Mark beginning of line for ^ and lookbehind. */
328 PL_regbol = startpos;
331 /* Mark end of line for $ (and such) */
334 /* see how far we have to get to not match where we matched before */
335 PL_regtill = startpos+minend;
337 /* We start without call_cc context. */
340 /* If there is a "must appear" string, look for it. */
342 if (!(flags & REXEC_CHECKED)
343 && prog->check_substr != Nullsv &&
344 !(prog->reganch & ROPT_ANCH_GPOS) &&
345 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
346 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
349 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
350 /* Should be nonnegative! */
351 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
353 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
354 s = screaminstr(screamer, prog->check_substr,
355 start_shift + (stringarg - strbeg),
356 end_shift, &scream_pos, 0);
362 s = fbm_instr((unsigned char*)s + start_shift,
363 (unsigned char*)strend - end_shift,
364 prog->check_substr, 0);
366 ++BmUSEFUL(prog->check_substr); /* hooray */
367 goto phooey; /* not present */
369 else if (s - stringarg > prog->check_offset_max &&
371 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
372 : (t = s - prog->check_offset_max) != 0
376 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
379 else if (!(prog->reganch & ROPT_NAUGHTY)
380 && --BmUSEFUL(prog->check_substr) < 0
381 && prog->check_substr == prog->float_substr) { /* boo */
382 SvREFCNT_dec(prog->check_substr);
383 prog->check_substr = Nullsv; /* disable */
384 prog->float_substr = Nullsv; /* clear */
392 PerlIO_printf(Perl_debug_log,
393 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
394 PL_colors[4],PL_colors[5],PL_colors[0],
397 (strlen(prog->precomp) > 60 ? "..." : ""),
399 (strend - startpos > 60 ? 60 : strend - startpos),
400 startpos, PL_colors[1],
401 (strend - startpos > 60 ? "..." : ""))
404 /* Simplest case: anchored match need be tried only once. */
405 /* [unless only anchor is BOL and multiline is set] */
406 if (prog->reganch & ROPT_ANCH) {
407 if (regtry(prog, startpos))
409 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
410 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
411 || (prog->reganch & ROPT_ANCH_MBOL)))
414 dontbother = minlen - 1;
415 strend = HOPc(strend, -dontbother);
416 /* for multiline we only have to try after newlines */
420 if (*s++ == '\n') { /* don't need PL_utf8skip here */
421 if (s < strend && regtry(prog, s))
429 /* Messy cases: unanchored match. */
430 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
431 /* we have /x+whatever/ */
432 /* it must be a one character string */
433 char ch = SvPVX(prog->anchored_substr)[0];
437 if (regtry(prog, s)) goto got_it;
439 while (s < strend && *s == ch)
448 if (regtry(prog, s)) goto got_it;
450 while (s < strend && *s == ch)
458 else if (prog->anchored_substr != Nullsv
459 || (prog->float_substr != Nullsv
460 && prog->float_max_offset < strend - s)) {
461 SV *must = prog->anchored_substr
462 ? prog->anchored_substr : prog->float_substr;
464 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
466 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
467 I32 delta = back_max - back_min;
468 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
469 char *last1; /* Last position checked before */
474 last1 = s - 1; /* bogus */
476 /* XXXX check_substr already used to find `s', can optimize if
477 check_substr==must. */
479 dontbother = end_shift;
480 strend = HOPc(strend, -dontbother);
481 while ( (s <= last) &&
483 ? (s = screaminstr(screamer, must, HOPc(s, back_min) - strbeg,
484 end_shift, &scream_pos, 0))
485 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
486 (unsigned char*)strend, must, 0))) ) {
487 if (HOPc(s, -back_max) > last1) {
488 last1 = HOPc(s, -back_min);
489 s = HOPc(s, -back_max);
492 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
494 last1 = HOPc(s, -back_min);
514 else if (c = prog->regstclass) {
515 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
519 dontbother = minlen - 1;
520 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
522 /* We know what class it must start with. */
525 cc = (char *) OPERAND(c);
527 if (REGINCLASSUTF8(c, (U8*)s)) {
528 if (tmp && regtry(prog, s))
539 cc = (char *) OPERAND(c);
541 if (REGINCLASS(cc, *s)) {
542 if (tmp && regtry(prog, s))
553 PL_reg_flags |= RF_tainted;
560 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
561 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
563 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
570 if ((minlen || tmp) && regtry(prog,s))
574 PL_reg_flags |= RF_tainted;
579 strend = reghop_c(strend, -1);
581 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
582 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
584 if (tmp == !(OP(c) == BOUND ?
585 swash_fetch(PL_utf8_alnum, (U8*)s) :
586 isALNUM_LC_utf8((U8*)s)))
594 if ((minlen || tmp) && regtry(prog,s))
598 PL_reg_flags |= RF_tainted;
605 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
606 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
608 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
610 else if (regtry(prog, s))
614 if ((minlen || !tmp) && regtry(prog,s))
618 PL_reg_flags |= RF_tainted;
623 strend = reghop_c(strend, -1);
625 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
626 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
628 if (tmp == !(OP(c) == NBOUND ?
629 swash_fetch(PL_utf8_alnum, (U8*)s) :
630 isALNUM_LC_utf8((U8*)s)))
632 else if (regtry(prog, s))
636 if ((minlen || !tmp) && regtry(prog,s))
642 if (tmp && regtry(prog, s))
654 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
655 if (tmp && regtry(prog, s))
666 PL_reg_flags |= RF_tainted;
668 if (isALNUM_LC(*s)) {
669 if (tmp && regtry(prog, s))
680 PL_reg_flags |= RF_tainted;
682 if (isALNUM_LC_utf8((U8*)s)) {
683 if (tmp && regtry(prog, s))
696 if (tmp && regtry(prog, s))
708 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
709 if (tmp && regtry(prog, s))
720 PL_reg_flags |= RF_tainted;
722 if (!isALNUM_LC(*s)) {
723 if (tmp && regtry(prog, s))
734 PL_reg_flags |= RF_tainted;
736 if (!isALNUM_LC_utf8((U8*)s)) {
737 if (tmp && regtry(prog, s))
750 if (tmp && regtry(prog, s))
762 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
763 if (tmp && regtry(prog, s))
774 PL_reg_flags |= RF_tainted;
776 if (isSPACE_LC(*s)) {
777 if (tmp && regtry(prog, s))
788 PL_reg_flags |= RF_tainted;
790 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
791 if (tmp && regtry(prog, s))
804 if (tmp && regtry(prog, s))
816 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
817 if (tmp && regtry(prog, s))
828 PL_reg_flags |= RF_tainted;
830 if (!isSPACE_LC(*s)) {
831 if (tmp && regtry(prog, s))
842 PL_reg_flags |= RF_tainted;
844 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
845 if (tmp && regtry(prog, s))
858 if (tmp && regtry(prog, s))
870 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
871 if (tmp && regtry(prog, s))
884 if (tmp && regtry(prog, s))
896 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
897 if (tmp && regtry(prog, s))
911 if (prog->float_substr != Nullsv) { /* Trim the end. */
913 I32 oldpos = scream_pos;
916 last = screaminstr(screamer, prog->float_substr, s - strbeg,
917 end_shift, &scream_pos, 1); /* last one */
919 last = scream_olds; /* Only one occurence. */
924 char *little = SvPV(prog->float_substr, len);
926 last = rninstr(s, strend, little, little + len);
928 last = strend; /* matching `$' */
930 if (last == NULL) goto phooey; /* Should not happen! */
931 dontbother = strend - last + prog->float_min_offset;
933 if (minlen && (dontbother < minlen))
934 dontbother = minlen - 1;
935 strend -= dontbother; /* this one's always in bytes! */
936 /* We don't know much -- general case. */
950 } while (s++ < strend);
958 prog->subbeg = strbeg;
959 prog->subend = PL_regeol; /* strend may have been modified */
960 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
962 /* make sure $`, $&, $', and $digit will work later */
963 if (strbeg != prog->subbase) { /* second+ //g match. */
964 if (!(flags & REXEC_COPY_STR)) {
966 Safefree(prog->subbase);
967 prog->subbase = Nullch;
971 I32 i = PL_regeol - startpos + (stringarg - strbeg);
972 s = savepvn(strbeg, i);
973 Safefree(prog->subbase);
975 prog->subbeg = prog->subbase;
976 prog->subend = prog->subbase + i;
977 s = prog->subbase + (stringarg - strbeg);
978 for (i = 0; i <= prog->nparens; i++) {
980 prog->startp[i] = s + (prog->startp[i] - startpos);
981 prog->endp[i] = s + (prog->endp[i] - startpos);
986 /* Preserve the current value of $^R */
987 if (oreplsv != GvSV(PL_replgv)) {
988 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
989 restored, the value remains
999 - regtry - try match at specific point
1001 STATIC I32 /* 0 failure, 1 success */
1002 regtry(regexp *prog, char *startpos)
1010 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1011 PL_reg_eval_set = RS_init;
1013 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1014 PL_stack_sp - PL_stack_base);
1016 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1017 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1018 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1020 /* Apparently this is not needed, judging by wantarray. */
1021 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1022 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1024 PL_reginput = startpos;
1025 PL_regstartp = prog->startp;
1026 PL_regendp = prog->endp;
1027 PL_reglastparen = &prog->lastparen;
1028 prog->lastparen = 0;
1030 if (PL_reg_start_tmpl <= prog->nparens) {
1031 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1032 if(PL_reg_start_tmp)
1033 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1035 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1040 if (prog->nparens) {
1041 for (i = prog->nparens; i >= 0; i--) {
1047 if (regmatch(prog->program + 1)) {
1048 prog->startp[0] = startpos;
1049 prog->endp[0] = PL_reginput;
1057 - regmatch - main matching routine
1059 * Conceptually the strategy is simple: check to see whether the current
1060 * node matches, call self recursively to see whether the rest matches,
1061 * and then act accordingly. In practice we make some effort to avoid
1062 * recursion, in particular by going through "ordinary" nodes (that don't
1063 * need to know whether the rest of the match failed) by a loop instead of
1066 /* [lwall] I've hoisted the register declarations to the outer block in order to
1067 * maybe save a little bit of pushing and popping on the stack. It also takes
1068 * advantage of machines that use a register save mask on subroutine entry.
1070 STATIC I32 /* 0 failure, 1 success */
1071 regmatch(regnode *prog)
1074 register regnode *scan; /* Current node. */
1075 regnode *next; /* Next node. */
1076 regnode *inner; /* Next node in internal branch. */
1077 register I32 nextchr; /* renamed nextchr - nextchar colides with
1078 function of same name */
1079 register I32 n; /* no or next */
1080 register I32 ln; /* len or last */
1081 register char *s; /* operand or save */
1082 register char *locinput = PL_reginput;
1083 register I32 c1, c2, paren; /* case fold search, parenth */
1084 int minmod = 0, sw = 0, logical = 0;
1089 /* Note that nextchr is a byte even in UTF */
1090 nextchr = UCHARAT(locinput);
1092 while (scan != NULL) {
1093 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1095 # define sayYES goto yes
1096 # define sayNO goto no
1097 # define saySAME(x) if (x) goto yes; else goto no
1098 # define REPORT_CODE_OFF 24
1100 # define sayYES return 1
1101 # define sayNO return 0
1102 # define saySAME(x) return x
1105 SV *prop = sv_newmortal();
1106 int docolor = *PL_colors[0];
1107 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1108 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1109 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1110 ? (5 + taill) - l : locinput - PL_bostr);
1111 int pref0_len = pref_len - (locinput - PL_reginput);
1113 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1114 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1115 ? (5 + taill) - pref_len : PL_regeol - locinput);
1118 regprop(prop, scan);
1119 PerlIO_printf(Perl_debug_log,
1120 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1121 locinput - PL_bostr,
1122 PL_colors[4], pref0_len,
1123 locinput - pref_len, PL_colors[5],
1124 PL_colors[2], pref_len - pref0_len,
1125 locinput - pref_len + pref0_len, PL_colors[3],
1126 (docolor ? "" : "> <"),
1127 PL_colors[0], l, locinput, PL_colors[1],
1128 15 - l - pref_len + 1,
1130 scan - PL_regprogram, PL_regindent*2, "",
1134 next = scan + NEXT_OFF(scan);
1140 if (locinput == PL_bostr
1141 ? PL_regprev == '\n'
1143 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1145 /* regtill = regbol; */
1150 if (locinput == PL_bostr
1151 ? PL_regprev == '\n'
1152 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1158 if (locinput == PL_regbol && PL_regprev == '\n')
1162 if (locinput == PL_regbol)
1172 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1177 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1179 if (PL_regeol - locinput > 1)
1183 if (PL_regeol != locinput)
1187 if (nextchr & 0x80) {
1188 locinput += PL_utf8skip[nextchr];
1189 if (locinput > PL_regeol)
1191 nextchr = UCHARAT(locinput);
1194 if (!nextchr && locinput >= PL_regeol)
1196 nextchr = UCHARAT(++locinput);
1199 if (!nextchr && locinput >= PL_regeol)
1201 nextchr = UCHARAT(++locinput);
1204 if (nextchr & 0x80) {
1205 locinput += PL_utf8skip[nextchr];
1206 if (locinput > PL_regeol)
1208 nextchr = UCHARAT(locinput);
1211 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1213 nextchr = UCHARAT(++locinput);
1216 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1218 nextchr = UCHARAT(++locinput);
1221 s = (char *) OPERAND(scan);
1223 /* Inline the first character, for speed. */
1224 if (UCHARAT(s) != nextchr)
1226 if (PL_regeol - locinput < ln)
1228 if (ln > 1 && memNE(s, locinput, ln))
1231 nextchr = UCHARAT(locinput);
1234 PL_reg_flags |= RF_tainted;
1237 s = (char *) OPERAND(scan);
1243 c1 = OP(scan) == EXACTF;
1247 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1248 toLOWER_utf8((U8*)l) :
1249 toLOWER_LC_utf8((U8*)l)))
1257 nextchr = UCHARAT(locinput);
1261 /* Inline the first character, for speed. */
1262 if (UCHARAT(s) != nextchr &&
1263 UCHARAT(s) != ((OP(scan) == EXACTF)
1264 ? PL_fold : PL_fold_locale)[nextchr])
1266 if (PL_regeol - locinput < ln)
1268 if (ln > 1 && (OP(scan) == EXACTF
1269 ? ibcmp(s, locinput, ln)
1270 : ibcmp_locale(s, locinput, ln)))
1273 nextchr = UCHARAT(locinput);
1276 s = (char *) OPERAND(scan);
1277 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1279 if (locinput >= PL_regeol)
1281 locinput += PL_utf8skip[nextchr];
1282 nextchr = UCHARAT(locinput);
1285 s = (char *) OPERAND(scan);
1287 nextchr = UCHARAT(locinput);
1288 if (!REGINCLASS(s, nextchr))
1290 if (!nextchr && locinput >= PL_regeol)
1292 nextchr = UCHARAT(++locinput);
1295 PL_reg_flags |= RF_tainted;
1300 if (!(OP(scan) == ALNUM
1301 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1303 nextchr = UCHARAT(++locinput);
1306 PL_reg_flags |= RF_tainted;
1311 if (nextchr & 0x80) {
1312 if (!(OP(scan) == ALNUMUTF8
1313 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1314 : isALNUM_LC_utf8((U8*)locinput)))
1318 locinput += PL_utf8skip[nextchr];
1319 nextchr = UCHARAT(locinput);
1322 if (!(OP(scan) == ALNUMUTF8
1323 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1325 nextchr = UCHARAT(++locinput);
1328 PL_reg_flags |= RF_tainted;
1331 if (!nextchr && locinput >= PL_regeol)
1333 if (OP(scan) == NALNUM
1334 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1336 nextchr = UCHARAT(++locinput);
1339 PL_reg_flags |= RF_tainted;
1342 if (!nextchr && locinput >= PL_regeol)
1344 if (nextchr & 0x80) {
1345 if (OP(scan) == NALNUMUTF8
1346 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1347 : isALNUM_LC_utf8((U8*)locinput))
1351 locinput += PL_utf8skip[nextchr];
1352 nextchr = UCHARAT(locinput);
1355 if (OP(scan) == NALNUMUTF8
1356 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1358 nextchr = UCHARAT(++locinput);
1362 PL_reg_flags |= RF_tainted;
1366 /* was last char in word? */
1367 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1368 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1370 n = isALNUM(nextchr);
1373 ln = isALNUM_LC(ln);
1374 n = isALNUM_LC(nextchr);
1376 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1381 PL_reg_flags |= RF_tainted;
1385 /* was last char in word? */
1386 ln = (locinput != PL_regbol)
1387 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1388 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1389 ln = isALNUM_uni(ln);
1390 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1393 ln = isALNUM_LC_uni(ln);
1394 n = isALNUM_LC_utf8((U8*)locinput);
1396 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1400 PL_reg_flags |= RF_tainted;
1403 if (!nextchr && locinput >= PL_regeol)
1405 if (!(OP(scan) == SPACE
1406 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1408 nextchr = UCHARAT(++locinput);
1411 PL_reg_flags |= RF_tainted;
1414 if (!nextchr && locinput >= PL_regeol)
1416 if (nextchr & 0x80) {
1417 if (!(OP(scan) == SPACEUTF8
1418 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1419 : isSPACE_LC_utf8((U8*)locinput)))
1423 locinput += PL_utf8skip[nextchr];
1424 nextchr = UCHARAT(locinput);
1427 if (!(OP(scan) == SPACEUTF8
1428 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1430 nextchr = UCHARAT(++locinput);
1433 PL_reg_flags |= RF_tainted;
1438 if (OP(scan) == SPACE
1439 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1441 nextchr = UCHARAT(++locinput);
1444 PL_reg_flags |= RF_tainted;
1449 if (nextchr & 0x80) {
1450 if (OP(scan) == NSPACEUTF8
1451 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1452 : isSPACE_LC_utf8((U8*)locinput))
1456 locinput += PL_utf8skip[nextchr];
1457 nextchr = UCHARAT(locinput);
1460 if (OP(scan) == NSPACEUTF8
1461 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1463 nextchr = UCHARAT(++locinput);
1466 if (!isDIGIT(nextchr))
1468 nextchr = UCHARAT(++locinput);
1471 if (nextchr & 0x80) {
1472 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1474 locinput += PL_utf8skip[nextchr];
1475 nextchr = UCHARAT(locinput);
1478 if (!isDIGIT(nextchr))
1480 nextchr = UCHARAT(++locinput);
1483 if (!nextchr && locinput >= PL_regeol)
1485 if (isDIGIT(nextchr))
1487 nextchr = UCHARAT(++locinput);
1490 if (!nextchr && locinput >= PL_regeol)
1492 if (nextchr & 0x80) {
1493 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1495 locinput += PL_utf8skip[nextchr];
1496 nextchr = UCHARAT(locinput);
1499 if (isDIGIT(nextchr))
1501 nextchr = UCHARAT(++locinput);
1504 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1506 locinput += PL_utf8skip[nextchr];
1507 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1508 locinput += UTF8SKIP(locinput);
1509 if (locinput > PL_regeol)
1511 nextchr = UCHARAT(locinput);
1514 PL_reg_flags |= RF_tainted;
1518 n = ARG(scan); /* which paren pair */
1519 s = PL_regstartp[n];
1520 if (*PL_reglastparen < n || !s)
1521 sayNO; /* Do not match unless seen CLOSEn. */
1522 if (s == PL_regendp[n])
1525 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1527 char *e = PL_regendp[n];
1529 * Note that we can't do the "other character" lookup trick as
1530 * in the 8-bit case (no pun intended) because in Unicode we
1531 * have to map both upper and title case to lower case.
1533 if (OP(scan) == REFF) {
1537 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1547 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1554 nextchr = UCHARAT(locinput);
1558 /* Inline the first character, for speed. */
1559 if (UCHARAT(s) != nextchr &&
1561 (UCHARAT(s) != ((OP(scan) == REFF
1562 ? PL_fold : PL_fold_locale)[nextchr]))))
1564 ln = PL_regendp[n] - s;
1565 if (locinput + ln > PL_regeol)
1567 if (ln > 1 && (OP(scan) == REF
1568 ? memNE(s, locinput, ln)
1570 ? ibcmp(s, locinput, ln)
1571 : ibcmp_locale(s, locinput, ln))))
1574 nextchr = UCHARAT(locinput);
1585 OP_4tree *oop = PL_op;
1586 COP *ocurcop = PL_curcop;
1587 SV **ocurpad = PL_curpad;
1591 PL_op = (OP_4tree*)PL_regdata->data[n];
1592 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1593 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1595 CALLRUNOPS(); /* Scalar context. */
1601 PL_curpad = ocurpad;
1602 PL_curcop = ocurcop;
1604 if (logical == 2) { /* Postponed subexpression. */
1606 MAGIC *mg = Null(MAGIC*);
1609 CHECKPOINT cp, lastcp;
1611 if(SvROK(ret) || SvRMAGICAL(ret)) {
1612 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1615 mg = mg_find(sv, 'r');
1618 re = (regexp *)mg->mg_obj;
1619 (void)ReREFCNT_inc(re);
1623 char *t = SvPV(ret, len);
1625 char *oprecomp = PL_regprecomp;
1626 I32 osize = PL_regsize;
1627 I32 onpar = PL_regnpar;
1630 re = CALLREGCOMP(t, t + len, &pm);
1632 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1633 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1634 PL_regprecomp = oprecomp;
1639 PerlIO_printf(Perl_debug_log,
1640 "Entering embedded `%s%.60s%s%s'\n",
1644 (strlen(re->precomp) > 60 ? "..." : ""))
1647 state.prev = PL_reg_call_cc;
1648 state.cc = PL_regcc;
1649 state.re = PL_reg_re;
1655 cp = regcppush(0); /* Save *all* the positions. */
1658 state.ss = PL_savestack_ix;
1659 *PL_reglastparen = 0;
1660 PL_reg_call_cc = &state;
1661 PL_reginput = locinput;
1662 if (regmatch(re->program + 1)) {
1668 PerlIO_printf(Perl_debug_log,
1670 REPORT_CODE_OFF+PL_regindent*2, "")
1675 PL_reg_call_cc = state.prev;
1676 PL_regcc = state.cc;
1677 PL_reg_re = state.re;
1678 cache_re(PL_reg_re);
1685 sv_setsv(save_scalar(PL_replgv), ret);
1689 n = ARG(scan); /* which paren pair */
1690 PL_reg_start_tmp[n] = locinput;
1695 n = ARG(scan); /* which paren pair */
1696 PL_regstartp[n] = PL_reg_start_tmp[n];
1697 PL_regendp[n] = locinput;
1698 if (n > *PL_reglastparen)
1699 *PL_reglastparen = n;
1702 n = ARG(scan); /* which paren pair */
1703 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1707 next = NEXTOPER(NEXTOPER(scan));
1709 next = scan + ARG(scan);
1710 if (OP(next) == IFTHEN) /* Fake one. */
1711 next = NEXTOPER(NEXTOPER(next));
1715 logical = scan->flags;
1719 CHECKPOINT cp = PL_savestack_ix;
1721 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1723 cc.oldcc = PL_regcc;
1725 cc.parenfloor = *PL_reglastparen;
1727 cc.min = ARG1(scan);
1728 cc.max = ARG2(scan);
1729 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1733 PL_reginput = locinput;
1734 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1736 PL_regcc = cc.oldcc;
1742 * This is really hard to understand, because after we match
1743 * what we're trying to match, we must make sure the rest of
1744 * the RE is going to match for sure, and to do that we have
1745 * to go back UP the parse tree by recursing ever deeper. And
1746 * if it fails, we have to reset our parent's current state
1747 * that we can try again after backing off.
1750 CHECKPOINT cp, lastcp;
1751 CURCUR* cc = PL_regcc;
1752 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1754 n = cc->cur + 1; /* how many we know we matched */
1755 PL_reginput = locinput;
1758 PerlIO_printf(Perl_debug_log,
1759 "%*s %ld out of %ld..%ld cc=%lx\n",
1760 REPORT_CODE_OFF+PL_regindent*2, "",
1761 (long)n, (long)cc->min,
1762 (long)cc->max, (long)cc)
1765 /* If degenerate scan matches "", assume scan done. */
1767 if (locinput == cc->lastloc && n >= cc->min) {
1768 PL_regcc = cc->oldcc;
1771 PerlIO_printf(Perl_debug_log,
1772 "%*s empty match detected, try continuation...\n",
1773 REPORT_CODE_OFF+PL_regindent*2, "")
1775 if (regmatch(cc->next))
1778 PerlIO_printf(Perl_debug_log,
1780 REPORT_CODE_OFF+PL_regindent*2, "")
1787 /* First just match a string of min scans. */
1791 cc->lastloc = locinput;
1792 if (regmatch(cc->scan))
1795 cc->lastloc = lastloc;
1797 PerlIO_printf(Perl_debug_log,
1799 REPORT_CODE_OFF+PL_regindent*2, "")
1804 /* Prefer next over scan for minimal matching. */
1807 PL_regcc = cc->oldcc;
1809 cp = regcppush(cc->parenfloor);
1811 if (regmatch(cc->next)) {
1813 sayYES; /* All done. */
1820 if (n >= cc->max) { /* Maximum greed exceeded? */
1821 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1822 && !(PL_reg_flags & RF_warned)) {
1823 PL_reg_flags |= RF_warned;
1824 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1825 "Complex regular subexpression recursion",
1832 PerlIO_printf(Perl_debug_log,
1833 "%*s trying longer...\n",
1834 REPORT_CODE_OFF+PL_regindent*2, "")
1836 /* Try scanning more and see if it helps. */
1837 PL_reginput = locinput;
1839 cc->lastloc = locinput;
1840 cp = regcppush(cc->parenfloor);
1842 if (regmatch(cc->scan)) {
1847 PerlIO_printf(Perl_debug_log,
1849 REPORT_CODE_OFF+PL_regindent*2, "")
1854 cc->lastloc = lastloc;
1858 /* Prefer scan over next for maximal matching. */
1860 if (n < cc->max) { /* More greed allowed? */
1861 cp = regcppush(cc->parenfloor);
1863 cc->lastloc = locinput;
1865 if (regmatch(cc->scan)) {
1870 regcppop(); /* Restore some previous $<digit>s? */
1871 PL_reginput = locinput;
1873 PerlIO_printf(Perl_debug_log,
1874 "%*s failed, try continuation...\n",
1875 REPORT_CODE_OFF+PL_regindent*2, "")
1878 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1879 && !(PL_reg_flags & RF_warned)) {
1880 PL_reg_flags |= RF_warned;
1881 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1882 "Complex regular subexpression recursion",
1886 /* Failed deeper matches of scan, so see if this one works. */
1887 PL_regcc = cc->oldcc;
1889 if (regmatch(cc->next))
1892 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1893 REPORT_CODE_OFF+PL_regindent*2, "")
1898 cc->lastloc = lastloc;
1903 next = scan + ARG(scan);
1906 inner = NEXTOPER(NEXTOPER(scan));
1909 inner = NEXTOPER(scan);
1914 if (OP(next) != c1) /* No choice. */
1915 next = inner; /* Avoid recursion. */
1917 int lastparen = *PL_reglastparen;
1921 PL_reginput = locinput;
1922 if (regmatch(inner))
1925 for (n = *PL_reglastparen; n > lastparen; n--)
1927 *PL_reglastparen = n;
1930 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1934 inner = NEXTOPER(scan);
1935 if (c1 == BRANCHJ) {
1936 inner = NEXTOPER(inner);
1938 } while (scan != NULL && OP(scan) == c1);
1952 /* We suppose that the next guy does not need
1953 backtracking: in particular, it is of constant length,
1954 and has no parenths to influence future backrefs. */
1955 ln = ARG1(scan); /* min to match */
1956 n = ARG2(scan); /* max to match */
1957 paren = scan->flags;
1959 if (paren > PL_regsize)
1961 if (paren > *PL_reglastparen)
1962 *PL_reglastparen = paren;
1964 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1966 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1967 PL_reginput = locinput;
1970 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1972 if (ln && l == 0 && n >= ln
1973 /* In fact, this is tricky. If paren, then the
1974 fact that we did/didnot match may influence
1975 future execution. */
1976 && !(paren && ln == 0))
1978 locinput = PL_reginput;
1979 if (PL_regkind[(U8)OP(next)] == EXACT) {
1980 c1 = UCHARAT(OPERAND(next) + 1);
1981 if (OP(next) == EXACTF)
1983 else if (OP(next) == EXACTFL)
1984 c2 = PL_fold_locale[c1];
1991 /* This may be improved if l == 0. */
1992 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1993 /* If it could work, try it. */
1995 UCHARAT(PL_reginput) == c1 ||
1996 UCHARAT(PL_reginput) == c2)
2000 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2001 PL_regendp[paren] = PL_reginput;
2004 PL_regendp[paren] = NULL;
2010 /* Couldn't or didn't -- move forward. */
2011 PL_reginput = locinput;
2012 if (regrepeat_hard(scan, 1, &l)) {
2014 locinput = PL_reginput;
2021 n = regrepeat_hard(scan, n, &l);
2022 if (n != 0 && l == 0
2023 /* In fact, this is tricky. If paren, then the
2024 fact that we did/didnot match may influence
2025 future execution. */
2026 && !(paren && ln == 0))
2028 locinput = PL_reginput;
2030 PerlIO_printf(Perl_debug_log,
2031 "%*s matched %ld times, len=%ld...\n",
2032 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2035 if (PL_regkind[(U8)OP(next)] == EXACT) {
2036 c1 = UCHARAT(OPERAND(next) + 1);
2037 if (OP(next) == EXACTF)
2039 else if (OP(next) == EXACTFL)
2040 c2 = PL_fold_locale[c1];
2049 /* If it could work, try it. */
2051 UCHARAT(PL_reginput) == c1 ||
2052 UCHARAT(PL_reginput) == c2)
2055 PerlIO_printf(Perl_debug_log,
2056 "%*s trying tail with n=%ld...\n",
2057 REPORT_CODE_OFF+PL_regindent*2, "", n)
2061 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2062 PL_regendp[paren] = PL_reginput;
2065 PL_regendp[paren] = NULL;
2071 /* Couldn't or didn't -- back up. */
2073 locinput = HOPc(locinput, -l);
2074 PL_reginput = locinput;
2081 paren = scan->flags; /* Which paren to set */
2082 if (paren > PL_regsize)
2084 if (paren > *PL_reglastparen)
2085 *PL_reglastparen = paren;
2086 ln = ARG1(scan); /* min to match */
2087 n = ARG2(scan); /* max to match */
2088 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2092 ln = ARG1(scan); /* min to match */
2093 n = ARG2(scan); /* max to match */
2094 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2099 scan = NEXTOPER(scan);
2105 scan = NEXTOPER(scan);
2109 * Lookahead to avoid useless match attempts
2110 * when we know what character comes next.
2112 if (PL_regkind[(U8)OP(next)] == EXACT) {
2113 c1 = UCHARAT(OPERAND(next) + 1);
2114 if (OP(next) == EXACTF)
2116 else if (OP(next) == EXACTFL)
2117 c2 = PL_fold_locale[c1];
2123 PL_reginput = locinput;
2127 if (ln && regrepeat(scan, ln) < ln)
2129 locinput = PL_reginput;
2131 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2132 /* If it could work, try it. */
2134 UCHARAT(PL_reginput) == c1 ||
2135 UCHARAT(PL_reginput) == c2)
2139 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2140 PL_regendp[paren] = PL_reginput;
2143 PL_regendp[paren] = NULL;
2149 /* Couldn't or didn't -- move forward. */
2150 PL_reginput = locinput;
2151 if (regrepeat(scan, 1)) {
2153 locinput = PL_reginput;
2161 n = regrepeat(scan, n);
2162 locinput = PL_reginput;
2163 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2164 (!PL_multiline || OP(next) == SEOL))
2165 ln = n; /* why back off? */
2169 /* If it could work, try it. */
2171 UCHARAT(PL_reginput) == c1 ||
2172 UCHARAT(PL_reginput) == c2)
2176 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2177 PL_regendp[paren] = PL_reginput;
2180 PL_regendp[paren] = NULL;
2186 /* Couldn't or didn't -- back up. */
2188 PL_reginput = locinput = HOPc(locinput, -1);
2193 /* If it could work, try it. */
2195 UCHARAT(PL_reginput) == c1 ||
2196 UCHARAT(PL_reginput) == c2)
2202 /* Couldn't or didn't -- back up. */
2204 PL_reginput = locinput = HOPc(locinput, -1);
2211 if (PL_reg_call_cc) {
2212 re_cc_state *cur_call_cc = PL_reg_call_cc;
2213 CURCUR *cctmp = PL_regcc;
2214 regexp *re = PL_reg_re;
2215 CHECKPOINT cp, lastcp;
2217 cp = regcppush(0); /* Save *all* the positions. */
2219 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2221 PL_reginput = locinput; /* Make position available to
2223 cache_re(PL_reg_call_cc->re);
2224 PL_regcc = PL_reg_call_cc->cc;
2225 PL_reg_call_cc = PL_reg_call_cc->prev;
2226 if (regmatch(cur_call_cc->node)) {
2227 PL_reg_call_cc = cur_call_cc;
2233 PL_reg_call_cc = cur_call_cc;
2239 PerlIO_printf(Perl_debug_log,
2240 "%*s continuation failed...\n",
2241 REPORT_CODE_OFF+PL_regindent*2, "")
2245 if (locinput < PL_regtill)
2246 sayNO; /* Cannot match: too short. */
2249 PL_reginput = locinput; /* put where regtry can find it */
2250 sayYES; /* Success! */
2253 PL_reginput = locinput;
2258 s = HOPMAYBEc(locinput, -scan->flags);
2264 PL_reginput = locinput;
2269 s = HOPMAYBEc(locinput, -scan->flags);
2270 if (!s || s < PL_bostr)
2275 PL_reginput = locinput;
2278 inner = NEXTOPER(NEXTOPER(scan));
2279 if (regmatch(inner) != n) {
2294 if (OP(scan) == SUSPEND) {
2295 locinput = PL_reginput;
2296 nextchr = UCHARAT(locinput);
2301 next = scan + ARG(scan);
2306 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2307 (unsigned long)scan, OP(scan));
2308 FAIL("regexp memory corruption");
2314 * We get here only if there's trouble -- normally "case END" is
2315 * the terminating point.
2317 FAIL("corrupted regexp pointers");
2335 - regrepeat - repeatedly match something simple, report how many
2338 * [This routine now assumes that it will only match on things of length 1.
2339 * That was true before, but now we assume scan - reginput is the count,
2340 * rather than incrementing count on every character. [Er, except utf8.]]
2343 regrepeat(regnode *p, I32 max)
2346 register char *scan;
2347 register char *opnd;
2349 register char *loceol = PL_regeol;
2350 register I32 hardcount = 0;
2353 if (max != REG_INFTY && max < loceol - scan)
2354 loceol = scan + max;
2355 opnd = (char *) OPERAND(p);
2358 while (scan < loceol && *scan != '\n')
2366 while (scan < loceol && *scan != '\n') {
2367 scan += UTF8SKIP(scan);
2373 while (scan < loceol) {
2374 scan += UTF8SKIP(scan);
2378 case EXACT: /* length of string is 1 */
2379 c = UCHARAT(++opnd);
2380 while (scan < loceol && UCHARAT(scan) == c)
2383 case EXACTF: /* length of string is 1 */
2384 c = UCHARAT(++opnd);
2385 while (scan < loceol &&
2386 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2389 case EXACTFL: /* length of string is 1 */
2390 PL_reg_flags |= RF_tainted;
2391 c = UCHARAT(++opnd);
2392 while (scan < loceol &&
2393 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2398 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2399 scan += UTF8SKIP(scan);
2404 while (scan < loceol && REGINCLASS(opnd, *scan))
2408 while (scan < loceol && isALNUM(*scan))
2413 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2414 scan += UTF8SKIP(scan);
2419 PL_reg_flags |= RF_tainted;
2420 while (scan < loceol && isALNUM_LC(*scan))
2424 PL_reg_flags |= RF_tainted;
2426 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2427 scan += UTF8SKIP(scan);
2433 while (scan < loceol && !isALNUM(*scan))
2438 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2439 scan += UTF8SKIP(scan);
2444 PL_reg_flags |= RF_tainted;
2445 while (scan < loceol && !isALNUM_LC(*scan))
2449 PL_reg_flags |= RF_tainted;
2451 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2452 scan += UTF8SKIP(scan);
2457 while (scan < loceol && isSPACE(*scan))
2462 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2463 scan += UTF8SKIP(scan);
2468 PL_reg_flags |= RF_tainted;
2469 while (scan < loceol && isSPACE_LC(*scan))
2473 PL_reg_flags |= RF_tainted;
2475 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2476 scan += UTF8SKIP(scan);
2481 while (scan < loceol && !isSPACE(*scan))
2486 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2487 scan += UTF8SKIP(scan);
2492 PL_reg_flags |= RF_tainted;
2493 while (scan < loceol && !isSPACE_LC(*scan))
2497 PL_reg_flags |= RF_tainted;
2499 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2500 scan += UTF8SKIP(scan);
2505 while (scan < loceol && isDIGIT(*scan))
2510 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2511 scan += UTF8SKIP(scan);
2517 while (scan < loceol && !isDIGIT(*scan))
2522 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2523 scan += UTF8SKIP(scan);
2527 default: /* Called on something of 0 width. */
2528 break; /* So match right here or not at all. */
2534 c = scan - PL_reginput;
2539 SV *prop = sv_newmortal();
2542 PerlIO_printf(Perl_debug_log,
2543 "%*s %s can match %ld times out of %ld...\n",
2544 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2551 - regrepeat_hard - repeatedly match something, report total lenth and length
2553 * The repeater is supposed to have constant length.
2557 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2560 register char *scan;
2561 register char *start;
2562 register char *loceol = PL_regeol;
2564 I32 count = 0, res = 1;
2569 start = PL_reginput;
2571 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2574 while (start < PL_reginput) {
2576 start += UTF8SKIP(start);
2587 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2589 *lp = l = PL_reginput - start;
2590 if (max != REG_INFTY && l*max < loceol - scan)
2591 loceol = scan + l*max;
2604 - regclass - determine if a character falls into a character class
2608 reginclass(register char *p, register I32 c)
2615 if (ANYOF_TEST(p, c))
2617 else if (flags & ANYOF_FOLD) {
2619 if (flags & ANYOF_LOCALE) {
2620 PL_reg_flags |= RF_tainted;
2621 cf = PL_fold_locale[c];
2625 if (ANYOF_TEST(p, cf))
2629 if (!match && (flags & ANYOF_ISA)) {
2630 PL_reg_flags |= RF_tainted;
2632 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2633 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2634 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2635 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2641 return (flags & ANYOF_INVERT) ? !match : match;
2645 reginclassutf8(regnode *f, U8 *p)
2648 char flags = ARG1(f);
2650 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2652 if (swash_fetch(sv, p))
2654 else if (flags & ANYOF_FOLD) {
2657 if (flags & ANYOF_LOCALE) {
2658 PL_reg_flags |= RF_tainted;
2659 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2662 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2663 if (swash_fetch(sv, tmpbuf))
2667 if (!match && (flags & ANYOF_ISA)) {
2668 PL_reg_flags |= RF_tainted;
2670 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2671 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2672 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2673 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2679 return (flags & ANYOF_INVERT) ? !match : match;
2683 reghop(U8 *s, I32 off)
2687 while (off-- && s < (U8*)PL_regeol)
2692 if (s > (U8*)PL_bostr) {
2695 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2697 } /* XXX could check well-formedness here */
2705 reghopmaybe(U8* s, I32 off)
2709 while (off-- && s < (U8*)PL_regeol)
2716 if (s > (U8*)PL_bostr) {
2719 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2721 } /* XXX could check well-formedness here */