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.
76 typedef MAGIC *my_magic;
80 #define RF_tainted 1 /* tainted information used? */
81 #define RF_warned 2 /* warned about big count? */
82 #define RF_evaled 4 /* Did an EVAL with setting? */
83 #define RF_utf8 8 /* String contains multibyte chars? */
85 #define UTF (PL_reg_flags & RF_utf8)
87 #define RS_init 1 /* eval environment created */
88 #define RS_set 2 /* replsv value is set */
95 typedef I32 CHECKPOINT;
101 static I32 regmatch _((regnode *prog));
102 static I32 regrepeat _((regnode *p, I32 max));
103 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
104 static I32 regtry _((regexp *prog, char *startpos));
106 static bool reginclass _((char *p, I32 c));
107 static bool reginclassutf8 _((regnode *f, U8* p));
108 static CHECKPOINT regcppush _((I32 parenfloor));
109 static char * regcppop _((void));
112 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
113 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
115 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
116 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
119 static U8 * reghop _((U8 *pos, I32 off));
120 static U8 * reghopmaybe _((U8 *pos, I32 off));
122 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
123 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
124 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
125 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
126 #define HOPc(pos,off) ((char*)HOP(pos,off))
127 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
130 regcppush(I32 parenfloor)
133 int retval = PL_savestack_ix;
134 int i = (PL_regsize - parenfloor) * 4;
138 for (p = PL_regsize; p > parenfloor; p--) {
139 SSPUSHPTR(PL_regendp[p]);
140 SSPUSHPTR(PL_regstartp[p]);
141 SSPUSHPTR(PL_reg_start_tmp[p]);
144 SSPUSHINT(PL_regsize);
145 SSPUSHINT(*PL_reglastparen);
146 SSPUSHPTR(PL_reginput);
148 SSPUSHINT(SAVEt_REGCONTEXT);
152 /* These are needed since we do not localize EVAL nodes: */
153 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
154 " Setting an EVAL scope, savestack=%i\n", \
155 PL_savestack_ix)); lastcp = PL_savestack_ix
157 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
158 PerlIO_printf(Perl_debug_log, \
159 " Clearing an EVAL scope, savestack=%i..%i\n", \
160 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
170 assert(i == SAVEt_REGCONTEXT);
172 input = (char *) SSPOPPTR;
173 *PL_reglastparen = SSPOPINT;
174 PL_regsize = SSPOPINT;
175 for (i -= 3; i > 0; i -= 4) {
176 paren = (U32)SSPOPINT;
177 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
178 PL_regstartp[paren] = (char *) SSPOPPTR;
179 tmps = (char*)SSPOPPTR;
180 if (paren <= *PL_reglastparen)
181 PL_regendp[paren] = tmps;
183 PerlIO_printf(Perl_debug_log,
184 " restoring \\%d to %d(%d)..%d%s\n",
185 paren, PL_regstartp[paren] - PL_regbol,
186 PL_reg_start_tmp[paren] - PL_regbol,
187 PL_regendp[paren] - PL_regbol,
188 (paren > *PL_reglastparen ? "(no)" : ""));
192 if (*PL_reglastparen + 1 <= PL_regnpar) {
193 PerlIO_printf(Perl_debug_log,
194 " restoring \\%d..\\%d to undef\n",
195 *PL_reglastparen + 1, PL_regnpar);
198 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
199 if (paren > PL_regsize)
200 PL_regstartp[paren] = Nullch;
201 PL_regendp[paren] = Nullch;
209 I32 tmp = PL_savestack_ix;
211 PL_savestack_ix = ss;
213 PL_savestack_ix = tmp;
216 typedef struct re_cc_state
220 struct re_cc_state *prev;
225 #define regcpblow(cp) LEAVE_SCOPE(cp)
228 * pregexec and friends
232 - pregexec - match a regexp against a string
235 pregexec(register regexp *prog, char *stringarg, register char *strend,
236 char *strbeg, I32 minend, SV *screamer, U32 nosave)
237 /* strend: pointer to null at end of string */
238 /* strbeg: real beginning of string */
239 /* minend: end of match must be >=minend after stringarg. */
240 /* nosave: For optimizations. */
243 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
244 nosave ? 0 : REXEC_COPY_STR);
248 cache_re(regexp *prog)
250 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
252 PL_regprogram = prog->program;
254 PL_regnpar = prog->nparens;
255 PL_regdata = prog->data;
260 - regexec_flags - match a regexp against a string
263 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
264 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
265 /* strend: pointer to null at end of string */
266 /* strbeg: real beginning of string */
267 /* minend: end of match must be >=minend after stringarg. */
268 /* data: May be used for some additional optimizations. */
269 /* nosave: For optimizations. */
274 register char *startpos = stringarg;
276 I32 minlen; /* must match at least this many chars */
277 I32 dontbother = 0; /* how many characters not to try at end */
279 I32 start_shift = 0; /* Offset of the start to find
280 constant substr. */ /* CC */
281 I32 end_shift = 0; /* Same for the end. */ /* CC */
282 I32 scream_pos = -1; /* Internal iterator of scream. */
284 SV* oreplsv = GvSV(PL_replgv);
292 PL_regnarrate = PL_debug & 512;
296 if (prog == NULL || startpos == NULL) {
297 croak("NULL regexp parameter");
301 minlen = prog->minlen;
302 if (strend - startpos < minlen) goto phooey;
304 if (startpos == strbeg) /* is ^ valid at stringarg? */
307 PL_regprev = (U32)stringarg[-1];
308 if (!PL_multiline && PL_regprev == '\n')
309 PL_regprev = '\0'; /* force ^ to NOT match */
312 /* Check validity of program. */
313 if (UCHARAT(prog->program) != MAGIC) {
314 FAIL("corrupted regexp program");
320 if (prog->reganch & ROPT_UTF8)
321 PL_reg_flags |= RF_utf8;
323 /* Mark beginning of line for ^ and lookbehind. */
324 PL_regbol = startpos;
327 /* Mark end of line for $ (and such) */
330 /* see how far we have to get to not match where we matched before */
331 PL_regtill = startpos+minend;
333 /* We start without call_cc context. */
336 /* If there is a "must appear" string, look for it. */
338 if (!(flags & REXEC_CHECKED)
339 && prog->check_substr != Nullsv &&
340 !(prog->reganch & ROPT_ANCH_GPOS) &&
341 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
342 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
345 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
346 /* Should be nonnegative! */
347 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
349 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
350 s = screaminstr(screamer, prog->check_substr,
351 start_shift + (stringarg - strbeg),
352 end_shift, &scream_pos, 0);
358 s = fbm_instr((unsigned char*)s + start_shift,
359 (unsigned char*)strend - end_shift,
360 prog->check_substr, 0);
362 ++BmUSEFUL(prog->check_substr); /* hooray */
363 goto phooey; /* not present */
365 else if (s - stringarg > prog->check_offset_max &&
367 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
368 : (t = s - prog->check_offset_max) != 0
372 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
375 else if (!(prog->reganch & ROPT_NAUGHTY)
376 && --BmUSEFUL(prog->check_substr) < 0
377 && prog->check_substr == prog->float_substr) { /* boo */
378 SvREFCNT_dec(prog->check_substr);
379 prog->check_substr = Nullsv; /* disable */
380 prog->float_substr = Nullsv; /* clear */
388 PerlIO_printf(Perl_debug_log,
389 "Matching `%.60s%s' against `%.*s%s'\n",
391 (strlen(prog->precomp) > 60 ? "..." : ""),
392 (strend - startpos > 60 ? 60 : strend - startpos),
394 (strend - startpos > 60 ? "..." : ""))
397 /* Simplest case: anchored match need be tried only once. */
398 /* [unless only anchor is BOL and multiline is set] */
399 if (prog->reganch & ROPT_ANCH) {
400 if (regtry(prog, startpos))
402 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
403 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
404 || (prog->reganch & ROPT_ANCH_MBOL)))
407 dontbother = minlen - 1;
408 strend = HOPc(strend, -dontbother);
409 /* for multiline we only have to try after newlines */
413 if (*s++ == '\n') { /* don't need utf8skip here */
414 if (s < strend && regtry(prog, s))
422 /* Messy cases: unanchored match. */
423 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
424 /* we have /x+whatever/ */
425 /* it must be a one character string */
426 char ch = SvPVX(prog->anchored_substr)[0];
430 if (regtry(prog, s)) goto got_it;
432 while (s < strend && *s == ch)
441 if (regtry(prog, s)) goto got_it;
443 while (s < strend && *s == ch)
451 else if (prog->anchored_substr != Nullsv
452 || (prog->float_substr != Nullsv
453 && prog->float_max_offset < strend - s)) {
454 SV *must = prog->anchored_substr
455 ? prog->anchored_substr : prog->float_substr;
457 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
459 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
460 I32 delta = back_max - back_min;
461 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
462 char *last1; /* Last position checked before */
467 last1 = s - 1; /* bogus */
469 /* XXXX check_substr already used to find `s', can optimize if
470 check_substr==must. */
472 dontbother = end_shift;
473 strend = HOPc(strend, -dontbother);
474 while ( (s <= last) &&
476 ? (s = screaminstr(screamer, must, HOPc(s, back_min) - strbeg,
477 end_shift, &scream_pos, 0))
478 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
479 (unsigned char*)strend, must, 0))) ) {
480 if (HOPc(s, -back_max) > last1) {
481 last1 = HOPc(s, -back_min);
482 s = HOPc(s, -back_max);
485 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
487 last1 = HOPc(s, -back_min);
507 else if (c = prog->regstclass) {
508 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
512 dontbother = minlen - 1;
513 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
515 /* We know what class it must start with. */
518 cc = (char *) OPERAND(c);
520 if (REGINCLASSUTF8(c, (U8*)s)) {
521 if (tmp && regtry(prog, s))
532 cc = (char *) OPERAND(c);
534 if (REGINCLASS(cc, *s)) {
535 if (tmp && regtry(prog, s))
546 PL_reg_flags |= RF_tainted;
553 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
554 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
556 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
563 if ((minlen || tmp) && regtry(prog,s))
567 PL_reg_flags |= RF_tainted;
572 strend = reghop_c(strend, -1);
574 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
575 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
577 if (tmp == !(OP(c) == BOUND ?
578 swash_fetch(PL_utf8_alnum, (U8*)s) :
579 isALNUM_LC_utf8((U8*)s)))
587 if ((minlen || tmp) && regtry(prog,s))
591 PL_reg_flags |= RF_tainted;
598 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
599 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
601 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
603 else if (regtry(prog, s))
607 if ((minlen || !tmp) && regtry(prog,s))
611 PL_reg_flags |= RF_tainted;
616 strend = reghop_c(strend, -1);
618 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
619 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
621 if (tmp == !(OP(c) == NBOUND ?
622 swash_fetch(PL_utf8_alnum, (U8*)s) :
623 isALNUM_LC_utf8((U8*)s)))
625 else if (regtry(prog, s))
629 if ((minlen || !tmp) && regtry(prog,s))
635 if (tmp && regtry(prog, s))
647 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
648 if (tmp && regtry(prog, s))
659 PL_reg_flags |= RF_tainted;
661 if (isALNUM_LC(*s)) {
662 if (tmp && regtry(prog, s))
673 PL_reg_flags |= RF_tainted;
675 if (isALNUM_LC_utf8((U8*)s)) {
676 if (tmp && regtry(prog, s))
689 if (tmp && regtry(prog, s))
701 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
702 if (tmp && regtry(prog, s))
713 PL_reg_flags |= RF_tainted;
715 if (!isALNUM_LC(*s)) {
716 if (tmp && regtry(prog, s))
727 PL_reg_flags |= RF_tainted;
729 if (!isALNUM_LC_utf8((U8*)s)) {
730 if (tmp && regtry(prog, s))
743 if (tmp && regtry(prog, s))
755 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
756 if (tmp && regtry(prog, s))
767 PL_reg_flags |= RF_tainted;
769 if (isSPACE_LC(*s)) {
770 if (tmp && regtry(prog, s))
781 PL_reg_flags |= RF_tainted;
783 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
784 if (tmp && regtry(prog, s))
797 if (tmp && regtry(prog, s))
809 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
810 if (tmp && regtry(prog, s))
821 PL_reg_flags |= RF_tainted;
823 if (!isSPACE_LC(*s)) {
824 if (tmp && regtry(prog, s))
835 PL_reg_flags |= RF_tainted;
837 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
838 if (tmp && regtry(prog, s))
851 if (tmp && regtry(prog, s))
863 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
864 if (tmp && regtry(prog, s))
877 if (tmp && regtry(prog, s))
889 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
890 if (tmp && regtry(prog, s))
904 if (prog->float_substr != Nullsv) { /* Trim the end. */
906 I32 oldpos = scream_pos;
909 last = screaminstr(screamer, prog->float_substr, s - strbeg,
910 end_shift, &scream_pos, 1); /* last one */
912 last = scream_olds; /* Only one occurence. */
917 char *little = SvPV(prog->float_substr, len);
919 last = rninstr(s, strend, little, little + len);
921 last = strend; /* matching `$' */
923 if (last == NULL) goto phooey; /* Should not happen! */
924 dontbother = strend - last + prog->float_min_offset;
926 if (minlen && (dontbother < minlen))
927 dontbother = minlen - 1;
928 strend -= dontbother; /* this one's always in bytes! */
929 /* We don't know much -- general case. */
943 } while (s++ < strend);
951 prog->subbeg = strbeg;
952 prog->subend = PL_regeol; /* strend may have been modified */
953 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
955 /* make sure $`, $&, $', and $digit will work later */
956 if (strbeg != prog->subbase) { /* second+ //g match. */
957 if (!(flags & REXEC_COPY_STR)) {
959 Safefree(prog->subbase);
960 prog->subbase = Nullch;
964 I32 i = PL_regeol - startpos + (stringarg - strbeg);
965 s = savepvn(strbeg, i);
966 Safefree(prog->subbase);
968 prog->subbeg = prog->subbase;
969 prog->subend = prog->subbase + i;
970 s = prog->subbase + (stringarg - strbeg);
971 for (i = 0; i <= prog->nparens; i++) {
973 prog->startp[i] = s + (prog->startp[i] - startpos);
974 prog->endp[i] = s + (prog->endp[i] - startpos);
979 /* Preserve the current value of $^R */
980 if (oreplsv != GvSV(PL_replgv)) {
981 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
982 restored, the value remains
992 - regtry - try match at specific point
994 STATIC I32 /* 0 failure, 1 success */
995 regtry(regexp *prog, char *startpos)
1003 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1004 PL_reg_eval_set = RS_init;
1006 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1007 PL_stack_sp - PL_stack_base);
1009 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1010 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1011 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1013 /* Apparently this is not needed, judging by wantarray. */
1014 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1015 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1017 PL_reginput = startpos;
1018 PL_regstartp = prog->startp;
1019 PL_regendp = prog->endp;
1020 PL_reglastparen = &prog->lastparen;
1021 prog->lastparen = 0;
1023 if (PL_reg_start_tmpl <= prog->nparens) {
1024 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1025 if(PL_reg_start_tmp)
1026 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1028 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1033 if (prog->nparens) {
1034 for (i = prog->nparens; i >= 0; i--) {
1040 if (regmatch(prog->program + 1)) {
1041 prog->startp[0] = startpos;
1042 prog->endp[0] = PL_reginput;
1050 - regmatch - main matching routine
1052 * Conceptually the strategy is simple: check to see whether the current
1053 * node matches, call self recursively to see whether the rest matches,
1054 * and then act accordingly. In practice we make some effort to avoid
1055 * recursion, in particular by going through "ordinary" nodes (that don't
1056 * need to know whether the rest of the match failed) by a loop instead of
1059 /* [lwall] I've hoisted the register declarations to the outer block in order to
1060 * maybe save a little bit of pushing and popping on the stack. It also takes
1061 * advantage of machines that use a register save mask on subroutine entry.
1063 STATIC I32 /* 0 failure, 1 success */
1064 regmatch(regnode *prog)
1067 register regnode *scan; /* Current node. */
1068 regnode *next; /* Next node. */
1069 regnode *inner; /* Next node in internal branch. */
1070 register I32 nextchr; /* renamed nextchr - nextchar colides with
1071 function of same name */
1072 register I32 n; /* no or next */
1073 register I32 ln; /* len or last */
1074 register char *s; /* operand or save */
1075 register char *locinput = PL_reginput;
1076 register I32 c1, c2, paren; /* case fold search, parenth */
1077 int minmod = 0, sw = 0, logical = 0;
1082 /* Note that nextchr is a byte even in UTF */
1083 nextchr = UCHARAT(locinput);
1085 while (scan != NULL) {
1086 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1088 # define sayYES goto yes
1089 # define sayNO goto no
1090 # define saySAME(x) if (x) goto yes; else goto no
1091 # define REPORT_CODE_OFF 24
1093 # define sayYES return 1
1094 # define sayNO return 0
1095 # define saySAME(x) return x
1098 SV *prop = sv_newmortal();
1099 int docolor = *PL_colors[0];
1100 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1101 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1102 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1103 ? (5 + taill) - l : locinput - PL_bostr);
1105 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1106 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1107 ? (5 + taill) - pref_len : PL_regeol - locinput);
1108 regprop(prop, scan);
1109 PerlIO_printf(Perl_debug_log,
1110 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1111 locinput - PL_bostr,
1112 PL_colors[2], pref_len, locinput - pref_len, PL_colors[3],
1113 (docolor ? "" : "> <"),
1114 PL_colors[0], l, locinput, PL_colors[1],
1115 15 - l - pref_len + 1,
1117 scan - PL_regprogram, PL_regindent*2, "",
1121 next = scan + NEXT_OFF(scan);
1127 if (locinput == PL_bostr
1128 ? PL_regprev == '\n'
1130 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1132 /* regtill = regbol; */
1137 if (locinput == PL_bostr
1138 ? PL_regprev == '\n'
1139 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1145 if (locinput == PL_regbol && PL_regprev == '\n')
1149 if (locinput == PL_regbol)
1159 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1164 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1166 if (PL_regeol - locinput > 1)
1170 if (PL_regeol != locinput)
1174 if (nextchr & 0x80) {
1175 locinput += utf8skip[nextchr];
1176 if (locinput > PL_regeol)
1178 nextchr = UCHARAT(locinput);
1181 if (!nextchr && locinput >= PL_regeol)
1183 nextchr = UCHARAT(++locinput);
1186 if (!nextchr && locinput >= PL_regeol)
1188 nextchr = UCHARAT(++locinput);
1191 if (nextchr & 0x80) {
1192 locinput += utf8skip[nextchr];
1193 if (locinput > PL_regeol)
1195 nextchr = UCHARAT(locinput);
1198 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1200 nextchr = UCHARAT(++locinput);
1203 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1205 nextchr = UCHARAT(++locinput);
1208 s = (char *) OPERAND(scan);
1210 /* Inline the first character, for speed. */
1211 if (UCHARAT(s) != nextchr)
1213 if (PL_regeol - locinput < ln)
1215 if (ln > 1 && memNE(s, locinput, ln))
1218 nextchr = UCHARAT(locinput);
1221 PL_reg_flags |= RF_tainted;
1224 s = (char *) OPERAND(scan);
1230 c1 = OP(scan) == EXACTF;
1234 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1235 toLOWER_utf8((U8*)l) :
1236 toLOWER_LC_utf8((U8*)l)))
1244 nextchr = UCHARAT(locinput);
1248 /* Inline the first character, for speed. */
1249 if (UCHARAT(s) != nextchr &&
1250 UCHARAT(s) != ((OP(scan) == EXACTF)
1251 ? fold : fold_locale)[nextchr])
1253 if (PL_regeol - locinput < ln)
1255 if (ln > 1 && (OP(scan) == EXACTF
1256 ? ibcmp(s, locinput, ln)
1257 : ibcmp_locale(s, locinput, ln)))
1260 nextchr = UCHARAT(locinput);
1263 s = (char *) OPERAND(scan);
1264 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1266 if (locinput >= PL_regeol)
1268 locinput += utf8skip[nextchr];
1269 nextchr = UCHARAT(locinput);
1272 s = (char *) OPERAND(scan);
1274 nextchr = UCHARAT(locinput);
1275 if (!REGINCLASS(s, nextchr))
1277 if (!nextchr && locinput >= PL_regeol)
1279 nextchr = UCHARAT(++locinput);
1282 PL_reg_flags |= RF_tainted;
1287 if (!(OP(scan) == ALNUM
1288 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1290 nextchr = UCHARAT(++locinput);
1293 PL_reg_flags |= RF_tainted;
1298 if (nextchr & 0x80) {
1299 if (!(OP(scan) == ALNUMUTF8
1300 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1301 : isALNUM_LC_utf8((U8*)locinput)))
1305 locinput += utf8skip[nextchr];
1306 nextchr = UCHARAT(locinput);
1309 if (!(OP(scan) == ALNUMUTF8
1310 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1312 nextchr = UCHARAT(++locinput);
1315 PL_reg_flags |= RF_tainted;
1318 if (!nextchr && locinput >= PL_regeol)
1320 if (OP(scan) == NALNUM
1321 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1323 nextchr = UCHARAT(++locinput);
1326 PL_reg_flags |= RF_tainted;
1329 if (!nextchr && locinput >= PL_regeol)
1331 if (nextchr & 0x80) {
1332 if (OP(scan) == NALNUMUTF8
1333 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1334 : isALNUM_LC_utf8((U8*)locinput))
1338 locinput += utf8skip[nextchr];
1339 nextchr = UCHARAT(locinput);
1342 if (OP(scan) == NALNUMUTF8
1343 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1345 nextchr = UCHARAT(++locinput);
1349 PL_reg_flags |= RF_tainted;
1353 /* was last char in word? */
1354 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1355 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1357 n = isALNUM(nextchr);
1360 ln = isALNUM_LC(ln);
1361 n = isALNUM_LC(nextchr);
1363 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1368 PL_reg_flags |= RF_tainted;
1372 /* was last char in word? */
1373 ln = (locinput != PL_regbol)
1374 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1375 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1376 ln = isALNUM_uni(ln);
1377 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1380 ln = isALNUM_LC_uni(ln);
1381 n = isALNUM_LC_utf8((U8*)locinput);
1383 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1387 PL_reg_flags |= RF_tainted;
1390 if (!nextchr && locinput >= PL_regeol)
1392 if (!(OP(scan) == SPACE
1393 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1395 nextchr = UCHARAT(++locinput);
1398 PL_reg_flags |= RF_tainted;
1401 if (!nextchr && locinput >= PL_regeol)
1403 if (nextchr & 0x80) {
1404 if (!(OP(scan) == SPACEUTF8
1405 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1406 : isSPACE_LC_utf8((U8*)locinput)))
1410 locinput += utf8skip[nextchr];
1411 nextchr = UCHARAT(locinput);
1414 if (!(OP(scan) == SPACEUTF8
1415 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1417 nextchr = UCHARAT(++locinput);
1420 PL_reg_flags |= RF_tainted;
1425 if (OP(scan) == SPACE
1426 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1428 nextchr = UCHARAT(++locinput);
1431 PL_reg_flags |= RF_tainted;
1436 if (nextchr & 0x80) {
1437 if (OP(scan) == NSPACEUTF8
1438 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1439 : isSPACE_LC_utf8((U8*)locinput))
1443 locinput += utf8skip[nextchr];
1444 nextchr = UCHARAT(locinput);
1447 if (OP(scan) == NSPACEUTF8
1448 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1450 nextchr = UCHARAT(++locinput);
1453 if (!isDIGIT(nextchr))
1455 nextchr = UCHARAT(++locinput);
1458 if (nextchr & 0x80) {
1459 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1461 locinput += utf8skip[nextchr];
1462 nextchr = UCHARAT(locinput);
1465 if (!isDIGIT(nextchr))
1467 nextchr = UCHARAT(++locinput);
1470 if (!nextchr && locinput >= PL_regeol)
1472 if (isDIGIT(nextchr))
1474 nextchr = UCHARAT(++locinput);
1477 if (!nextchr && locinput >= PL_regeol)
1479 if (nextchr & 0x80) {
1480 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1482 locinput += utf8skip[nextchr];
1483 nextchr = UCHARAT(locinput);
1486 if (isDIGIT(nextchr))
1488 nextchr = UCHARAT(++locinput);
1491 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1493 locinput += utf8skip[nextchr];
1494 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1495 locinput += UTF8SKIP(locinput);
1496 if (locinput > PL_regeol)
1498 nextchr = UCHARAT(locinput);
1501 PL_reg_flags |= RF_tainted;
1505 n = ARG(scan); /* which paren pair */
1506 s = PL_regstartp[n];
1507 if (*PL_reglastparen < n || !s)
1508 sayNO; /* Do not match unless seen CLOSEn. */
1509 if (s == PL_regendp[n])
1512 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1514 char *e = PL_regendp[n];
1516 * Note that we can't do the "other character" lookup trick as
1517 * in the 8-bit case (no pun intended) because in Unicode we
1518 * have to map both upper and title case to lower case.
1520 if (OP(scan) == REFF) {
1524 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1534 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1541 nextchr = UCHARAT(locinput);
1545 /* Inline the first character, for speed. */
1546 if (UCHARAT(s) != nextchr &&
1548 (UCHARAT(s) != ((OP(scan) == REFF
1549 ? fold : fold_locale)[nextchr]))))
1551 ln = PL_regendp[n] - s;
1552 if (locinput + ln > PL_regeol)
1554 if (ln > 1 && (OP(scan) == REF
1555 ? memNE(s, locinput, ln)
1557 ? ibcmp(s, locinput, ln)
1558 : ibcmp_locale(s, locinput, ln))))
1561 nextchr = UCHARAT(locinput);
1572 OP_4tree *oop = PL_op;
1573 COP *ocurcop = PL_curcop;
1574 SV **ocurpad = PL_curpad;
1578 PL_op = (OP_4tree*)PL_regdata->data[n];
1579 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1580 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1582 CALLRUNOPS(); /* Scalar context. */
1588 PL_curpad = ocurpad;
1589 PL_curcop = ocurcop;
1591 if (logical == 2) { /* Postponed subexpression. */
1593 my_magic mg = Null(my_magic);
1596 CHECKPOINT cp, lastcp;
1598 if(SvROK(ret) || SvRMAGICAL(ret)) {
1599 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1602 mg = mg_find(sv, 'r');
1605 re = (regexp *)mg->mg_obj;
1610 char *t = SvPV(ret, len);
1612 char *oprecomp = PL_regprecomp;
1613 I32 osize = PL_regsize;
1614 I32 onpar = PL_regnpar;
1617 re = CALLREGCOMP(t, t + len, &pm);
1619 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1620 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1621 PL_regprecomp = oprecomp;
1626 PerlIO_printf(Perl_debug_log,
1627 "Entering embedded `%s%.60s%s%s'\n",
1631 (strlen(re->precomp) > 60 ? "..." : ""))
1634 state.prev = PL_reg_call_cc;
1635 state.cc = PL_regcc;
1636 state.re = PL_reg_re;
1642 cp = regcppush(0); /* Save *all* the positions. */
1645 state.ss = PL_savestack_ix;
1646 *PL_reglastparen = 0;
1647 PL_reg_call_cc = &state;
1648 PL_reginput = locinput;
1649 if (regmatch(re->program + 1)) {
1655 PerlIO_printf(Perl_debug_log,
1657 REPORT_CODE_OFF+PL_regindent*2, "")
1662 PL_reg_call_cc = state.prev;
1663 PL_regcc = state.cc;
1664 PL_reg_re = state.re;
1671 sv_setsv(save_scalar(PL_replgv), ret);
1675 n = ARG(scan); /* which paren pair */
1676 PL_reg_start_tmp[n] = locinput;
1681 n = ARG(scan); /* which paren pair */
1682 PL_regstartp[n] = PL_reg_start_tmp[n];
1683 PL_regendp[n] = locinput;
1684 if (n > *PL_reglastparen)
1685 *PL_reglastparen = n;
1688 n = ARG(scan); /* which paren pair */
1689 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1693 next = NEXTOPER(NEXTOPER(scan));
1695 next = scan + ARG(scan);
1696 if (OP(next) == IFTHEN) /* Fake one. */
1697 next = NEXTOPER(NEXTOPER(next));
1701 logical = scan->flags;
1705 CHECKPOINT cp = PL_savestack_ix;
1707 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1709 cc.oldcc = PL_regcc;
1711 cc.parenfloor = *PL_reglastparen;
1713 cc.min = ARG1(scan);
1714 cc.max = ARG2(scan);
1715 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1719 PL_reginput = locinput;
1720 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1722 PL_regcc = cc.oldcc;
1728 * This is really hard to understand, because after we match
1729 * what we're trying to match, we must make sure the rest of
1730 * the RE is going to match for sure, and to do that we have
1731 * to go back UP the parse tree by recursing ever deeper. And
1732 * if it fails, we have to reset our parent's current state
1733 * that we can try again after backing off.
1736 CHECKPOINT cp, lastcp;
1737 CURCUR* cc = PL_regcc;
1738 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1740 n = cc->cur + 1; /* how many we know we matched */
1741 PL_reginput = locinput;
1744 PerlIO_printf(Perl_debug_log,
1745 "%*s %ld out of %ld..%ld cc=%lx\n",
1746 REPORT_CODE_OFF+PL_regindent*2, "",
1747 (long)n, (long)cc->min,
1748 (long)cc->max, (long)cc)
1751 /* If degenerate scan matches "", assume scan done. */
1753 if (locinput == cc->lastloc && n >= cc->min) {
1754 PL_regcc = cc->oldcc;
1757 PerlIO_printf(Perl_debug_log,
1758 "%*s empty match detected, try continuation...\n",
1759 REPORT_CODE_OFF+PL_regindent*2, "")
1761 if (regmatch(cc->next))
1764 PerlIO_printf(Perl_debug_log,
1766 REPORT_CODE_OFF+PL_regindent*2, "")
1773 /* First just match a string of min scans. */
1777 cc->lastloc = locinput;
1778 if (regmatch(cc->scan))
1781 cc->lastloc = lastloc;
1783 PerlIO_printf(Perl_debug_log,
1785 REPORT_CODE_OFF+PL_regindent*2, "")
1790 /* Prefer next over scan for minimal matching. */
1793 PL_regcc = cc->oldcc;
1795 cp = regcppush(cc->parenfloor);
1797 if (regmatch(cc->next)) {
1799 sayYES; /* All done. */
1806 if (n >= cc->max) { /* Maximum greed exceeded? */
1807 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1808 && !(PL_reg_flags & RF_warned)) {
1809 PL_reg_flags |= RF_warned;
1810 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1811 "Complex regular subexpression recursion",
1818 PerlIO_printf(Perl_debug_log,
1819 "%*s trying longer...\n",
1820 REPORT_CODE_OFF+PL_regindent*2, "")
1822 /* Try scanning more and see if it helps. */
1823 PL_reginput = locinput;
1825 cc->lastloc = locinput;
1826 cp = regcppush(cc->parenfloor);
1828 if (regmatch(cc->scan)) {
1833 PerlIO_printf(Perl_debug_log,
1835 REPORT_CODE_OFF+PL_regindent*2, "")
1840 cc->lastloc = lastloc;
1844 /* Prefer scan over next for maximal matching. */
1846 if (n < cc->max) { /* More greed allowed? */
1847 cp = regcppush(cc->parenfloor);
1849 cc->lastloc = locinput;
1851 if (regmatch(cc->scan)) {
1856 regcppop(); /* Restore some previous $<digit>s? */
1857 PL_reginput = locinput;
1859 PerlIO_printf(Perl_debug_log,
1860 "%*s failed, try continuation...\n",
1861 REPORT_CODE_OFF+PL_regindent*2, "")
1864 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1865 && !(PL_reg_flags & RF_warned)) {
1866 PL_reg_flags |= RF_warned;
1867 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1868 "Complex regular subexpression recursion",
1872 /* Failed deeper matches of scan, so see if this one works. */
1873 PL_regcc = cc->oldcc;
1875 if (regmatch(cc->next))
1878 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1879 REPORT_CODE_OFF+PL_regindent*2, "")
1884 cc->lastloc = lastloc;
1889 next = scan + ARG(scan);
1892 inner = NEXTOPER(NEXTOPER(scan));
1895 inner = NEXTOPER(scan);
1900 if (OP(next) != c1) /* No choice. */
1901 next = inner; /* Avoid recursion. */
1903 int lastparen = *PL_reglastparen;
1907 PL_reginput = locinput;
1908 if (regmatch(inner))
1911 for (n = *PL_reglastparen; n > lastparen; n--)
1913 *PL_reglastparen = n;
1916 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1920 inner = NEXTOPER(scan);
1921 if (c1 == BRANCHJ) {
1922 inner = NEXTOPER(inner);
1924 } while (scan != NULL && OP(scan) == c1);
1938 /* We suppose that the next guy does not need
1939 backtracking: in particular, it is of constant length,
1940 and has no parenths to influence future backrefs. */
1941 ln = ARG1(scan); /* min to match */
1942 n = ARG2(scan); /* max to match */
1943 paren = scan->flags;
1945 if (paren > PL_regsize)
1947 if (paren > *PL_reglastparen)
1948 *PL_reglastparen = paren;
1950 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1952 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1953 PL_reginput = locinput;
1956 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1958 if (ln && l == 0 && n >= ln
1959 /* In fact, this is tricky. If paren, then the
1960 fact that we did/didnot match may influence
1961 future execution. */
1962 && !(paren && ln == 0))
1964 locinput = PL_reginput;
1965 if (regkind[(U8)OP(next)] == EXACT) {
1966 c1 = UCHARAT(OPERAND(next) + 1);
1967 if (OP(next) == EXACTF)
1969 else if (OP(next) == EXACTFL)
1970 c2 = fold_locale[c1];
1977 /* This may be improved if l == 0. */
1978 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1979 /* If it could work, try it. */
1981 UCHARAT(PL_reginput) == c1 ||
1982 UCHARAT(PL_reginput) == c2)
1986 PL_regstartp[paren] = HOPc(PL_reginput, -l);
1987 PL_regendp[paren] = PL_reginput;
1990 PL_regendp[paren] = NULL;
1996 /* Couldn't or didn't -- move forward. */
1997 PL_reginput = locinput;
1998 if (regrepeat_hard(scan, 1, &l)) {
2000 locinput = PL_reginput;
2007 n = regrepeat_hard(scan, n, &l);
2008 if (n != 0 && l == 0
2009 /* In fact, this is tricky. If paren, then the
2010 fact that we did/didnot match may influence
2011 future execution. */
2012 && !(paren && ln == 0))
2014 locinput = PL_reginput;
2016 PerlIO_printf(Perl_debug_log,
2017 "%*s matched %ld times, len=%ld...\n",
2018 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2021 if (regkind[(U8)OP(next)] == EXACT) {
2022 c1 = UCHARAT(OPERAND(next) + 1);
2023 if (OP(next) == EXACTF)
2025 else if (OP(next) == EXACTFL)
2026 c2 = fold_locale[c1];
2035 /* If it could work, try it. */
2037 UCHARAT(PL_reginput) == c1 ||
2038 UCHARAT(PL_reginput) == c2)
2041 PerlIO_printf(Perl_debug_log,
2042 "%*s trying tail with n=%ld...\n",
2043 REPORT_CODE_OFF+PL_regindent*2, "", n)
2047 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2048 PL_regendp[paren] = PL_reginput;
2051 PL_regendp[paren] = NULL;
2057 /* Couldn't or didn't -- back up. */
2059 locinput = HOPc(locinput, -l);
2060 PL_reginput = locinput;
2067 paren = scan->flags; /* Which paren to set */
2068 if (paren > PL_regsize)
2070 if (paren > *PL_reglastparen)
2071 *PL_reglastparen = paren;
2072 ln = ARG1(scan); /* min to match */
2073 n = ARG2(scan); /* max to match */
2074 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2078 ln = ARG1(scan); /* min to match */
2079 n = ARG2(scan); /* max to match */
2080 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2085 scan = NEXTOPER(scan);
2091 scan = NEXTOPER(scan);
2095 * Lookahead to avoid useless match attempts
2096 * when we know what character comes next.
2098 if (regkind[(U8)OP(next)] == EXACT) {
2099 c1 = UCHARAT(OPERAND(next) + 1);
2100 if (OP(next) == EXACTF)
2102 else if (OP(next) == EXACTFL)
2103 c2 = fold_locale[c1];
2109 PL_reginput = locinput;
2113 if (ln && regrepeat(scan, ln) < ln)
2115 locinput = PL_reginput;
2117 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2118 /* If it could work, try it. */
2120 UCHARAT(PL_reginput) == c1 ||
2121 UCHARAT(PL_reginput) == c2)
2125 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2126 PL_regendp[paren] = PL_reginput;
2129 PL_regendp[paren] = NULL;
2135 /* Couldn't or didn't -- move forward. */
2136 PL_reginput = locinput;
2137 if (regrepeat(scan, 1)) {
2139 locinput = PL_reginput;
2147 n = regrepeat(scan, n);
2148 locinput = PL_reginput;
2149 if (ln < n && regkind[(U8)OP(next)] == EOL &&
2150 (!PL_multiline || OP(next) == SEOL))
2151 ln = n; /* why back off? */
2155 /* If it could work, try it. */
2157 UCHARAT(PL_reginput) == c1 ||
2158 UCHARAT(PL_reginput) == c2)
2162 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2163 PL_regendp[paren] = PL_reginput;
2166 PL_regendp[paren] = NULL;
2172 /* Couldn't or didn't -- back up. */
2174 PL_reginput = locinput = HOPc(locinput, -1);
2179 /* If it could work, try it. */
2181 UCHARAT(PL_reginput) == c1 ||
2182 UCHARAT(PL_reginput) == c2)
2188 /* Couldn't or didn't -- back up. */
2190 PL_reginput = locinput = HOPc(locinput, -1);
2197 if (PL_reg_call_cc) {
2198 re_cc_state *cur_call_cc = PL_reg_call_cc;
2199 CURCUR *cctmp = PL_regcc;
2200 regexp *re = PL_reg_re;
2201 CHECKPOINT cp, lastcp;
2203 cp = regcppush(0); /* Save *all* the positions. */
2205 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2207 PL_reginput = locinput; /* Make position available to
2209 cache_re(PL_reg_call_cc->re);
2210 PL_regcc = PL_reg_call_cc->cc;
2211 PL_reg_call_cc = PL_reg_call_cc->prev;
2212 if (regmatch(cur_call_cc->node)) {
2213 PL_reg_call_cc = cur_call_cc;
2219 PL_reg_call_cc = cur_call_cc;
2225 PerlIO_printf(Perl_debug_log,
2226 "%*s continuation failed...\n",
2227 REPORT_CODE_OFF+PL_regindent*2, "")
2231 if (locinput < PL_regtill)
2232 sayNO; /* Cannot match: too short. */
2235 PL_reginput = locinput; /* put where regtry can find it */
2236 sayYES; /* Success! */
2243 s = HOPMAYBEc(locinput, -scan->flags);
2249 PL_reginput = locinput;
2254 s = HOPMAYBEc(locinput, -scan->flags);
2260 PL_reginput = locinput;
2263 inner = NEXTOPER(NEXTOPER(scan));
2264 if (regmatch(inner) != n) {
2279 if (OP(scan) == SUSPEND) {
2280 locinput = PL_reginput;
2281 nextchr = UCHARAT(locinput);
2286 next = scan + ARG(scan);
2291 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2292 (unsigned long)scan, OP(scan));
2293 FAIL("regexp memory corruption");
2299 * We get here only if there's trouble -- normally "case END" is
2300 * the terminating point.
2302 FAIL("corrupted regexp pointers");
2320 - regrepeat - repeatedly match something simple, report how many
2323 * [This routine now assumes that it will only match on things of length 1.
2324 * That was true before, but now we assume scan - reginput is the count,
2325 * rather than incrementing count on every character. [Er, except utf8.]]
2328 regrepeat(regnode *p, I32 max)
2331 register char *scan;
2332 register char *opnd;
2334 register char *loceol = PL_regeol;
2335 register I32 hardcount = 0;
2338 if (max != REG_INFTY && max < loceol - scan)
2339 loceol = scan + max;
2340 opnd = (char *) OPERAND(p);
2343 while (scan < loceol && *scan != '\n')
2351 while (scan < loceol && *scan != '\n') {
2352 scan += UTF8SKIP(scan);
2358 while (scan < loceol) {
2359 scan += UTF8SKIP(scan);
2363 case EXACT: /* length of string is 1 */
2364 c = UCHARAT(++opnd);
2365 while (scan < loceol && UCHARAT(scan) == c)
2368 case EXACTF: /* length of string is 1 */
2369 c = UCHARAT(++opnd);
2370 while (scan < loceol &&
2371 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
2374 case EXACTFL: /* length of string is 1 */
2375 PL_reg_flags |= RF_tainted;
2376 c = UCHARAT(++opnd);
2377 while (scan < loceol &&
2378 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
2383 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2384 scan += UTF8SKIP(scan);
2389 while (scan < loceol && REGINCLASS(opnd, *scan))
2393 while (scan < loceol && isALNUM(*scan))
2398 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2399 scan += UTF8SKIP(scan);
2404 PL_reg_flags |= RF_tainted;
2405 while (scan < loceol && isALNUM_LC(*scan))
2409 PL_reg_flags |= RF_tainted;
2411 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2412 scan += UTF8SKIP(scan);
2418 while (scan < loceol && !isALNUM(*scan))
2423 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2424 scan += UTF8SKIP(scan);
2429 PL_reg_flags |= RF_tainted;
2430 while (scan < loceol && !isALNUM_LC(*scan))
2434 PL_reg_flags |= RF_tainted;
2436 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2437 scan += UTF8SKIP(scan);
2442 while (scan < loceol && isSPACE(*scan))
2447 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2448 scan += UTF8SKIP(scan);
2453 PL_reg_flags |= RF_tainted;
2454 while (scan < loceol && isSPACE_LC(*scan))
2458 PL_reg_flags |= RF_tainted;
2460 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2461 scan += UTF8SKIP(scan);
2466 while (scan < loceol && !isSPACE(*scan))
2471 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2472 scan += UTF8SKIP(scan);
2477 PL_reg_flags |= RF_tainted;
2478 while (scan < loceol && !isSPACE_LC(*scan))
2482 PL_reg_flags |= RF_tainted;
2484 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2485 scan += UTF8SKIP(scan);
2490 while (scan < loceol && isDIGIT(*scan))
2495 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2496 scan += UTF8SKIP(scan);
2502 while (scan < loceol && !isDIGIT(*scan))
2507 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2508 scan += UTF8SKIP(scan);
2512 default: /* Called on something of 0 width. */
2513 break; /* So match right here or not at all. */
2519 c = scan - PL_reginput;
2524 SV *prop = sv_newmortal();
2527 PerlIO_printf(Perl_debug_log,
2528 "%*s %s can match %ld times out of %ld...\n",
2529 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2536 - regrepeat_hard - repeatedly match something, report total lenth and length
2538 * The repeater is supposed to have constant length.
2542 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2545 register char *scan;
2546 register char *start;
2547 register char *loceol = PL_regeol;
2554 start = PL_reginput;
2556 while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) {
2559 while (start < PL_reginput) {
2561 start += UTF8SKIP(start);
2572 while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) {
2574 *lp = l = PL_reginput - start;
2575 if (max != REG_INFTY && l*max < loceol - scan)
2576 loceol = scan + l*max;
2582 if (PL_reginput < loceol)
2589 - regclass - determine if a character falls into a character class
2593 reginclass(register char *p, register I32 c)
2600 if (ANYOF_TEST(p, c))
2602 else if (flags & ANYOF_FOLD) {
2604 if (flags & ANYOF_LOCALE) {
2605 PL_reg_flags |= RF_tainted;
2606 cf = fold_locale[c];
2610 if (ANYOF_TEST(p, cf))
2614 if (!match && (flags & ANYOF_ISA)) {
2615 PL_reg_flags |= RF_tainted;
2617 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2618 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2619 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2620 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2626 return (flags & ANYOF_INVERT) ? !match : match;
2630 reginclassutf8(regnode *f, U8 *p)
2633 char flags = ARG1(f);
2635 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2637 if (swash_fetch(sv, p))
2639 else if (flags & ANYOF_FOLD) {
2642 if (flags & ANYOF_LOCALE) {
2643 PL_reg_flags |= RF_tainted;
2644 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2647 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2648 if (swash_fetch(sv, tmpbuf))
2652 if (!match && (flags & ANYOF_ISA)) {
2653 PL_reg_flags |= RF_tainted;
2655 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2656 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2657 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2658 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2664 return (flags & ANYOF_INVERT) ? !match : match;
2668 reghop(U8 *s, I32 off)
2672 while (off-- && s < (U8*)PL_regeol)
2677 if (s > (U8*)PL_bostr) {
2680 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2682 } /* XXX could check well-formedness here */
2690 reghopmaybe(U8* s, I32 off)
2694 while (off-- && s < (U8*)PL_regeol)
2701 if (s > (U8*)PL_bostr) {
2704 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2706 } /* XXX could check well-formedness here */