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 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
390 PL_colors[4],PL_colors[5],PL_colors[0],
393 (strlen(prog->precomp) > 60 ? "..." : ""),
395 (strend - startpos > 60 ? 60 : strend - startpos),
396 startpos, PL_colors[1],
397 (strend - startpos > 60 ? "..." : ""))
400 /* Simplest case: anchored match need be tried only once. */
401 /* [unless only anchor is BOL and multiline is set] */
402 if (prog->reganch & ROPT_ANCH) {
403 if (regtry(prog, startpos))
405 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
406 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
407 || (prog->reganch & ROPT_ANCH_MBOL)))
410 dontbother = minlen - 1;
411 strend = HOPc(strend, -dontbother);
412 /* for multiline we only have to try after newlines */
416 if (*s++ == '\n') { /* don't need utf8skip here */
417 if (s < strend && regtry(prog, s))
425 /* Messy cases: unanchored match. */
426 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
427 /* we have /x+whatever/ */
428 /* it must be a one character string */
429 char ch = SvPVX(prog->anchored_substr)[0];
433 if (regtry(prog, s)) goto got_it;
435 while (s < strend && *s == ch)
444 if (regtry(prog, s)) goto got_it;
446 while (s < strend && *s == ch)
454 else if (prog->anchored_substr != Nullsv
455 || (prog->float_substr != Nullsv
456 && prog->float_max_offset < strend - s)) {
457 SV *must = prog->anchored_substr
458 ? prog->anchored_substr : prog->float_substr;
460 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
462 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
463 I32 delta = back_max - back_min;
464 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
465 char *last1; /* Last position checked before */
470 last1 = s - 1; /* bogus */
472 /* XXXX check_substr already used to find `s', can optimize if
473 check_substr==must. */
475 dontbother = end_shift;
476 strend = HOPc(strend, -dontbother);
477 while ( (s <= last) &&
479 ? (s = screaminstr(screamer, must, HOPc(s, back_min) - strbeg,
480 end_shift, &scream_pos, 0))
481 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
482 (unsigned char*)strend, must, 0))) ) {
483 if (HOPc(s, -back_max) > last1) {
484 last1 = HOPc(s, -back_min);
485 s = HOPc(s, -back_max);
488 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
490 last1 = HOPc(s, -back_min);
510 else if (c = prog->regstclass) {
511 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
515 dontbother = minlen - 1;
516 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
518 /* We know what class it must start with. */
521 cc = (char *) OPERAND(c);
523 if (REGINCLASSUTF8(c, (U8*)s)) {
524 if (tmp && regtry(prog, s))
535 cc = (char *) OPERAND(c);
537 if (REGINCLASS(cc, *s)) {
538 if (tmp && regtry(prog, s))
549 PL_reg_flags |= RF_tainted;
556 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
557 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
559 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
566 if ((minlen || tmp) && regtry(prog,s))
570 PL_reg_flags |= RF_tainted;
575 strend = reghop_c(strend, -1);
577 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
578 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
580 if (tmp == !(OP(c) == BOUND ?
581 swash_fetch(PL_utf8_alnum, (U8*)s) :
582 isALNUM_LC_utf8((U8*)s)))
590 if ((minlen || tmp) && regtry(prog,s))
594 PL_reg_flags |= RF_tainted;
601 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
602 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
604 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
606 else if (regtry(prog, s))
610 if ((minlen || !tmp) && regtry(prog,s))
614 PL_reg_flags |= RF_tainted;
619 strend = reghop_c(strend, -1);
621 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
622 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
624 if (tmp == !(OP(c) == NBOUND ?
625 swash_fetch(PL_utf8_alnum, (U8*)s) :
626 isALNUM_LC_utf8((U8*)s)))
628 else if (regtry(prog, s))
632 if ((minlen || !tmp) && regtry(prog,s))
638 if (tmp && regtry(prog, s))
650 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
651 if (tmp && regtry(prog, s))
662 PL_reg_flags |= RF_tainted;
664 if (isALNUM_LC(*s)) {
665 if (tmp && regtry(prog, s))
676 PL_reg_flags |= RF_tainted;
678 if (isALNUM_LC_utf8((U8*)s)) {
679 if (tmp && regtry(prog, s))
692 if (tmp && regtry(prog, s))
704 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
705 if (tmp && regtry(prog, s))
716 PL_reg_flags |= RF_tainted;
718 if (!isALNUM_LC(*s)) {
719 if (tmp && regtry(prog, s))
730 PL_reg_flags |= RF_tainted;
732 if (!isALNUM_LC_utf8((U8*)s)) {
733 if (tmp && regtry(prog, s))
746 if (tmp && regtry(prog, s))
758 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
759 if (tmp && regtry(prog, s))
770 PL_reg_flags |= RF_tainted;
772 if (isSPACE_LC(*s)) {
773 if (tmp && regtry(prog, s))
784 PL_reg_flags |= RF_tainted;
786 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
787 if (tmp && regtry(prog, s))
800 if (tmp && regtry(prog, s))
812 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
813 if (tmp && regtry(prog, s))
824 PL_reg_flags |= RF_tainted;
826 if (!isSPACE_LC(*s)) {
827 if (tmp && regtry(prog, s))
838 PL_reg_flags |= RF_tainted;
840 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
841 if (tmp && regtry(prog, s))
854 if (tmp && regtry(prog, s))
866 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
867 if (tmp && regtry(prog, s))
880 if (tmp && regtry(prog, s))
892 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
893 if (tmp && regtry(prog, s))
907 if (prog->float_substr != Nullsv) { /* Trim the end. */
909 I32 oldpos = scream_pos;
912 last = screaminstr(screamer, prog->float_substr, s - strbeg,
913 end_shift, &scream_pos, 1); /* last one */
915 last = scream_olds; /* Only one occurence. */
920 char *little = SvPV(prog->float_substr, len);
922 last = rninstr(s, strend, little, little + len);
924 last = strend; /* matching `$' */
926 if (last == NULL) goto phooey; /* Should not happen! */
927 dontbother = strend - last + prog->float_min_offset;
929 if (minlen && (dontbother < minlen))
930 dontbother = minlen - 1;
931 strend -= dontbother; /* this one's always in bytes! */
932 /* We don't know much -- general case. */
946 } while (s++ < strend);
954 prog->subbeg = strbeg;
955 prog->subend = PL_regeol; /* strend may have been modified */
956 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
958 /* make sure $`, $&, $', and $digit will work later */
959 if (strbeg != prog->subbase) { /* second+ //g match. */
960 if (!(flags & REXEC_COPY_STR)) {
962 Safefree(prog->subbase);
963 prog->subbase = Nullch;
967 I32 i = PL_regeol - startpos + (stringarg - strbeg);
968 s = savepvn(strbeg, i);
969 Safefree(prog->subbase);
971 prog->subbeg = prog->subbase;
972 prog->subend = prog->subbase + i;
973 s = prog->subbase + (stringarg - strbeg);
974 for (i = 0; i <= prog->nparens; i++) {
976 prog->startp[i] = s + (prog->startp[i] - startpos);
977 prog->endp[i] = s + (prog->endp[i] - startpos);
982 /* Preserve the current value of $^R */
983 if (oreplsv != GvSV(PL_replgv)) {
984 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
985 restored, the value remains
995 - regtry - try match at specific point
997 STATIC I32 /* 0 failure, 1 success */
998 regtry(regexp *prog, char *startpos)
1006 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1007 PL_reg_eval_set = RS_init;
1009 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1010 PL_stack_sp - PL_stack_base);
1012 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1013 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1014 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1016 /* Apparently this is not needed, judging by wantarray. */
1017 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1018 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1020 PL_reginput = startpos;
1021 PL_regstartp = prog->startp;
1022 PL_regendp = prog->endp;
1023 PL_reglastparen = &prog->lastparen;
1024 prog->lastparen = 0;
1026 if (PL_reg_start_tmpl <= prog->nparens) {
1027 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1028 if(PL_reg_start_tmp)
1029 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1031 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1036 if (prog->nparens) {
1037 for (i = prog->nparens; i >= 0; i--) {
1043 if (regmatch(prog->program + 1)) {
1044 prog->startp[0] = startpos;
1045 prog->endp[0] = PL_reginput;
1053 - regmatch - main matching routine
1055 * Conceptually the strategy is simple: check to see whether the current
1056 * node matches, call self recursively to see whether the rest matches,
1057 * and then act accordingly. In practice we make some effort to avoid
1058 * recursion, in particular by going through "ordinary" nodes (that don't
1059 * need to know whether the rest of the match failed) by a loop instead of
1062 /* [lwall] I've hoisted the register declarations to the outer block in order to
1063 * maybe save a little bit of pushing and popping on the stack. It also takes
1064 * advantage of machines that use a register save mask on subroutine entry.
1066 STATIC I32 /* 0 failure, 1 success */
1067 regmatch(regnode *prog)
1070 register regnode *scan; /* Current node. */
1071 regnode *next; /* Next node. */
1072 regnode *inner; /* Next node in internal branch. */
1073 register I32 nextchr; /* renamed nextchr - nextchar colides with
1074 function of same name */
1075 register I32 n; /* no or next */
1076 register I32 ln; /* len or last */
1077 register char *s; /* operand or save */
1078 register char *locinput = PL_reginput;
1079 register I32 c1, c2, paren; /* case fold search, parenth */
1080 int minmod = 0, sw = 0, logical = 0;
1085 /* Note that nextchr is a byte even in UTF */
1086 nextchr = UCHARAT(locinput);
1088 while (scan != NULL) {
1089 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1091 # define sayYES goto yes
1092 # define sayNO goto no
1093 # define saySAME(x) if (x) goto yes; else goto no
1094 # define REPORT_CODE_OFF 24
1096 # define sayYES return 1
1097 # define sayNO return 0
1098 # define saySAME(x) return x
1101 SV *prop = sv_newmortal();
1102 int docolor = *PL_colors[0];
1103 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1104 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1105 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1106 ? (5 + taill) - l : locinput - PL_bostr);
1107 int pref0_len = pref_len - (locinput - PL_reginput);
1109 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1110 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1111 ? (5 + taill) - pref_len : PL_regeol - locinput);
1114 regprop(prop, scan);
1115 PerlIO_printf(Perl_debug_log,
1116 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1117 locinput - PL_bostr,
1118 PL_colors[4], pref0_len,
1119 locinput - pref_len, PL_colors[5],
1120 PL_colors[2], pref_len - pref0_len,
1121 locinput - pref_len + pref0_len, PL_colors[3],
1122 (docolor ? "" : "> <"),
1123 PL_colors[0], l, locinput, PL_colors[1],
1124 15 - l - pref_len + 1,
1126 scan - PL_regprogram, PL_regindent*2, "",
1130 next = scan + NEXT_OFF(scan);
1136 if (locinput == PL_bostr
1137 ? PL_regprev == '\n'
1139 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1141 /* regtill = regbol; */
1146 if (locinput == PL_bostr
1147 ? PL_regprev == '\n'
1148 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1154 if (locinput == PL_regbol && PL_regprev == '\n')
1158 if (locinput == PL_regbol)
1168 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1173 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1175 if (PL_regeol - locinput > 1)
1179 if (PL_regeol != locinput)
1183 if (nextchr & 0x80) {
1184 locinput += utf8skip[nextchr];
1185 if (locinput > PL_regeol)
1187 nextchr = UCHARAT(locinput);
1190 if (!nextchr && locinput >= PL_regeol)
1192 nextchr = UCHARAT(++locinput);
1195 if (!nextchr && locinput >= PL_regeol)
1197 nextchr = UCHARAT(++locinput);
1200 if (nextchr & 0x80) {
1201 locinput += utf8skip[nextchr];
1202 if (locinput > PL_regeol)
1204 nextchr = UCHARAT(locinput);
1207 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1209 nextchr = UCHARAT(++locinput);
1212 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1214 nextchr = UCHARAT(++locinput);
1217 s = (char *) OPERAND(scan);
1219 /* Inline the first character, for speed. */
1220 if (UCHARAT(s) != nextchr)
1222 if (PL_regeol - locinput < ln)
1224 if (ln > 1 && memNE(s, locinput, ln))
1227 nextchr = UCHARAT(locinput);
1230 PL_reg_flags |= RF_tainted;
1233 s = (char *) OPERAND(scan);
1239 c1 = OP(scan) == EXACTF;
1243 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1244 toLOWER_utf8((U8*)l) :
1245 toLOWER_LC_utf8((U8*)l)))
1253 nextchr = UCHARAT(locinput);
1257 /* Inline the first character, for speed. */
1258 if (UCHARAT(s) != nextchr &&
1259 UCHARAT(s) != ((OP(scan) == EXACTF)
1260 ? fold : fold_locale)[nextchr])
1262 if (PL_regeol - locinput < ln)
1264 if (ln > 1 && (OP(scan) == EXACTF
1265 ? ibcmp(s, locinput, ln)
1266 : ibcmp_locale(s, locinput, ln)))
1269 nextchr = UCHARAT(locinput);
1272 s = (char *) OPERAND(scan);
1273 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1275 if (locinput >= PL_regeol)
1277 locinput += utf8skip[nextchr];
1278 nextchr = UCHARAT(locinput);
1281 s = (char *) OPERAND(scan);
1283 nextchr = UCHARAT(locinput);
1284 if (!REGINCLASS(s, nextchr))
1286 if (!nextchr && locinput >= PL_regeol)
1288 nextchr = UCHARAT(++locinput);
1291 PL_reg_flags |= RF_tainted;
1296 if (!(OP(scan) == ALNUM
1297 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1299 nextchr = UCHARAT(++locinput);
1302 PL_reg_flags |= RF_tainted;
1307 if (nextchr & 0x80) {
1308 if (!(OP(scan) == ALNUMUTF8
1309 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1310 : isALNUM_LC_utf8((U8*)locinput)))
1314 locinput += utf8skip[nextchr];
1315 nextchr = UCHARAT(locinput);
1318 if (!(OP(scan) == ALNUMUTF8
1319 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1321 nextchr = UCHARAT(++locinput);
1324 PL_reg_flags |= RF_tainted;
1327 if (!nextchr && locinput >= PL_regeol)
1329 if (OP(scan) == NALNUM
1330 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1332 nextchr = UCHARAT(++locinput);
1335 PL_reg_flags |= RF_tainted;
1338 if (!nextchr && locinput >= PL_regeol)
1340 if (nextchr & 0x80) {
1341 if (OP(scan) == NALNUMUTF8
1342 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1343 : isALNUM_LC_utf8((U8*)locinput))
1347 locinput += utf8skip[nextchr];
1348 nextchr = UCHARAT(locinput);
1351 if (OP(scan) == NALNUMUTF8
1352 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1354 nextchr = UCHARAT(++locinput);
1358 PL_reg_flags |= RF_tainted;
1362 /* was last char in word? */
1363 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1364 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1366 n = isALNUM(nextchr);
1369 ln = isALNUM_LC(ln);
1370 n = isALNUM_LC(nextchr);
1372 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1377 PL_reg_flags |= RF_tainted;
1381 /* was last char in word? */
1382 ln = (locinput != PL_regbol)
1383 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1384 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1385 ln = isALNUM_uni(ln);
1386 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1389 ln = isALNUM_LC_uni(ln);
1390 n = isALNUM_LC_utf8((U8*)locinput);
1392 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1396 PL_reg_flags |= RF_tainted;
1399 if (!nextchr && locinput >= PL_regeol)
1401 if (!(OP(scan) == SPACE
1402 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1404 nextchr = UCHARAT(++locinput);
1407 PL_reg_flags |= RF_tainted;
1410 if (!nextchr && locinput >= PL_regeol)
1412 if (nextchr & 0x80) {
1413 if (!(OP(scan) == SPACEUTF8
1414 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1415 : isSPACE_LC_utf8((U8*)locinput)))
1419 locinput += utf8skip[nextchr];
1420 nextchr = UCHARAT(locinput);
1423 if (!(OP(scan) == SPACEUTF8
1424 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1426 nextchr = UCHARAT(++locinput);
1429 PL_reg_flags |= RF_tainted;
1434 if (OP(scan) == SPACE
1435 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1437 nextchr = UCHARAT(++locinput);
1440 PL_reg_flags |= RF_tainted;
1445 if (nextchr & 0x80) {
1446 if (OP(scan) == NSPACEUTF8
1447 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1448 : isSPACE_LC_utf8((U8*)locinput))
1452 locinput += utf8skip[nextchr];
1453 nextchr = UCHARAT(locinput);
1456 if (OP(scan) == NSPACEUTF8
1457 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1459 nextchr = UCHARAT(++locinput);
1462 if (!isDIGIT(nextchr))
1464 nextchr = UCHARAT(++locinput);
1467 if (nextchr & 0x80) {
1468 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1470 locinput += utf8skip[nextchr];
1471 nextchr = UCHARAT(locinput);
1474 if (!isDIGIT(nextchr))
1476 nextchr = UCHARAT(++locinput);
1479 if (!nextchr && locinput >= PL_regeol)
1481 if (isDIGIT(nextchr))
1483 nextchr = UCHARAT(++locinput);
1486 if (!nextchr && locinput >= PL_regeol)
1488 if (nextchr & 0x80) {
1489 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1491 locinput += utf8skip[nextchr];
1492 nextchr = UCHARAT(locinput);
1495 if (isDIGIT(nextchr))
1497 nextchr = UCHARAT(++locinput);
1500 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1502 locinput += utf8skip[nextchr];
1503 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1504 locinput += UTF8SKIP(locinput);
1505 if (locinput > PL_regeol)
1507 nextchr = UCHARAT(locinput);
1510 PL_reg_flags |= RF_tainted;
1514 n = ARG(scan); /* which paren pair */
1515 s = PL_regstartp[n];
1516 if (*PL_reglastparen < n || !s)
1517 sayNO; /* Do not match unless seen CLOSEn. */
1518 if (s == PL_regendp[n])
1521 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1523 char *e = PL_regendp[n];
1525 * Note that we can't do the "other character" lookup trick as
1526 * in the 8-bit case (no pun intended) because in Unicode we
1527 * have to map both upper and title case to lower case.
1529 if (OP(scan) == REFF) {
1533 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1543 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1550 nextchr = UCHARAT(locinput);
1554 /* Inline the first character, for speed. */
1555 if (UCHARAT(s) != nextchr &&
1557 (UCHARAT(s) != ((OP(scan) == REFF
1558 ? fold : fold_locale)[nextchr]))))
1560 ln = PL_regendp[n] - s;
1561 if (locinput + ln > PL_regeol)
1563 if (ln > 1 && (OP(scan) == REF
1564 ? memNE(s, locinput, ln)
1566 ? ibcmp(s, locinput, ln)
1567 : ibcmp_locale(s, locinput, ln))))
1570 nextchr = UCHARAT(locinput);
1581 OP_4tree *oop = PL_op;
1582 COP *ocurcop = PL_curcop;
1583 SV **ocurpad = PL_curpad;
1587 PL_op = (OP_4tree*)PL_regdata->data[n];
1588 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1589 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1591 CALLRUNOPS(); /* Scalar context. */
1597 PL_curpad = ocurpad;
1598 PL_curcop = ocurcop;
1600 if (logical == 2) { /* Postponed subexpression. */
1602 my_magic mg = Null(my_magic);
1605 CHECKPOINT cp, lastcp;
1607 if(SvROK(ret) || SvRMAGICAL(ret)) {
1608 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1611 mg = mg_find(sv, 'r');
1614 re = (regexp *)mg->mg_obj;
1619 char *t = SvPV(ret, len);
1621 char *oprecomp = PL_regprecomp;
1622 I32 osize = PL_regsize;
1623 I32 onpar = PL_regnpar;
1626 re = CALLREGCOMP(t, t + len, &pm);
1628 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1629 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1630 PL_regprecomp = oprecomp;
1635 PerlIO_printf(Perl_debug_log,
1636 "Entering embedded `%s%.60s%s%s'\n",
1640 (strlen(re->precomp) > 60 ? "..." : ""))
1643 state.prev = PL_reg_call_cc;
1644 state.cc = PL_regcc;
1645 state.re = PL_reg_re;
1651 cp = regcppush(0); /* Save *all* the positions. */
1654 state.ss = PL_savestack_ix;
1655 *PL_reglastparen = 0;
1656 PL_reg_call_cc = &state;
1657 PL_reginput = locinput;
1658 if (regmatch(re->program + 1)) {
1664 PerlIO_printf(Perl_debug_log,
1666 REPORT_CODE_OFF+PL_regindent*2, "")
1671 PL_reg_call_cc = state.prev;
1672 PL_regcc = state.cc;
1673 PL_reg_re = state.re;
1680 sv_setsv(save_scalar(PL_replgv), ret);
1684 n = ARG(scan); /* which paren pair */
1685 PL_reg_start_tmp[n] = locinput;
1690 n = ARG(scan); /* which paren pair */
1691 PL_regstartp[n] = PL_reg_start_tmp[n];
1692 PL_regendp[n] = locinput;
1693 if (n > *PL_reglastparen)
1694 *PL_reglastparen = n;
1697 n = ARG(scan); /* which paren pair */
1698 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1702 next = NEXTOPER(NEXTOPER(scan));
1704 next = scan + ARG(scan);
1705 if (OP(next) == IFTHEN) /* Fake one. */
1706 next = NEXTOPER(NEXTOPER(next));
1710 logical = scan->flags;
1714 CHECKPOINT cp = PL_savestack_ix;
1716 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1718 cc.oldcc = PL_regcc;
1720 cc.parenfloor = *PL_reglastparen;
1722 cc.min = ARG1(scan);
1723 cc.max = ARG2(scan);
1724 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1728 PL_reginput = locinput;
1729 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1731 PL_regcc = cc.oldcc;
1737 * This is really hard to understand, because after we match
1738 * what we're trying to match, we must make sure the rest of
1739 * the RE is going to match for sure, and to do that we have
1740 * to go back UP the parse tree by recursing ever deeper. And
1741 * if it fails, we have to reset our parent's current state
1742 * that we can try again after backing off.
1745 CHECKPOINT cp, lastcp;
1746 CURCUR* cc = PL_regcc;
1747 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1749 n = cc->cur + 1; /* how many we know we matched */
1750 PL_reginput = locinput;
1753 PerlIO_printf(Perl_debug_log,
1754 "%*s %ld out of %ld..%ld cc=%lx\n",
1755 REPORT_CODE_OFF+PL_regindent*2, "",
1756 (long)n, (long)cc->min,
1757 (long)cc->max, (long)cc)
1760 /* If degenerate scan matches "", assume scan done. */
1762 if (locinput == cc->lastloc && n >= cc->min) {
1763 PL_regcc = cc->oldcc;
1766 PerlIO_printf(Perl_debug_log,
1767 "%*s empty match detected, try continuation...\n",
1768 REPORT_CODE_OFF+PL_regindent*2, "")
1770 if (regmatch(cc->next))
1773 PerlIO_printf(Perl_debug_log,
1775 REPORT_CODE_OFF+PL_regindent*2, "")
1782 /* First just match a string of min scans. */
1786 cc->lastloc = locinput;
1787 if (regmatch(cc->scan))
1790 cc->lastloc = lastloc;
1792 PerlIO_printf(Perl_debug_log,
1794 REPORT_CODE_OFF+PL_regindent*2, "")
1799 /* Prefer next over scan for minimal matching. */
1802 PL_regcc = cc->oldcc;
1804 cp = regcppush(cc->parenfloor);
1806 if (regmatch(cc->next)) {
1808 sayYES; /* All done. */
1815 if (n >= cc->max) { /* Maximum greed exceeded? */
1816 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1817 && !(PL_reg_flags & RF_warned)) {
1818 PL_reg_flags |= RF_warned;
1819 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1820 "Complex regular subexpression recursion",
1827 PerlIO_printf(Perl_debug_log,
1828 "%*s trying longer...\n",
1829 REPORT_CODE_OFF+PL_regindent*2, "")
1831 /* Try scanning more and see if it helps. */
1832 PL_reginput = locinput;
1834 cc->lastloc = locinput;
1835 cp = regcppush(cc->parenfloor);
1837 if (regmatch(cc->scan)) {
1842 PerlIO_printf(Perl_debug_log,
1844 REPORT_CODE_OFF+PL_regindent*2, "")
1849 cc->lastloc = lastloc;
1853 /* Prefer scan over next for maximal matching. */
1855 if (n < cc->max) { /* More greed allowed? */
1856 cp = regcppush(cc->parenfloor);
1858 cc->lastloc = locinput;
1860 if (regmatch(cc->scan)) {
1865 regcppop(); /* Restore some previous $<digit>s? */
1866 PL_reginput = locinput;
1868 PerlIO_printf(Perl_debug_log,
1869 "%*s failed, try continuation...\n",
1870 REPORT_CODE_OFF+PL_regindent*2, "")
1873 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1874 && !(PL_reg_flags & RF_warned)) {
1875 PL_reg_flags |= RF_warned;
1876 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1877 "Complex regular subexpression recursion",
1881 /* Failed deeper matches of scan, so see if this one works. */
1882 PL_regcc = cc->oldcc;
1884 if (regmatch(cc->next))
1887 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1888 REPORT_CODE_OFF+PL_regindent*2, "")
1893 cc->lastloc = lastloc;
1898 next = scan + ARG(scan);
1901 inner = NEXTOPER(NEXTOPER(scan));
1904 inner = NEXTOPER(scan);
1909 if (OP(next) != c1) /* No choice. */
1910 next = inner; /* Avoid recursion. */
1912 int lastparen = *PL_reglastparen;
1916 PL_reginput = locinput;
1917 if (regmatch(inner))
1920 for (n = *PL_reglastparen; n > lastparen; n--)
1922 *PL_reglastparen = n;
1925 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1929 inner = NEXTOPER(scan);
1930 if (c1 == BRANCHJ) {
1931 inner = NEXTOPER(inner);
1933 } while (scan != NULL && OP(scan) == c1);
1947 /* We suppose that the next guy does not need
1948 backtracking: in particular, it is of constant length,
1949 and has no parenths to influence future backrefs. */
1950 ln = ARG1(scan); /* min to match */
1951 n = ARG2(scan); /* max to match */
1952 paren = scan->flags;
1954 if (paren > PL_regsize)
1956 if (paren > *PL_reglastparen)
1957 *PL_reglastparen = paren;
1959 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1961 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1962 PL_reginput = locinput;
1965 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1967 if (ln && l == 0 && n >= ln
1968 /* In fact, this is tricky. If paren, then the
1969 fact that we did/didnot match may influence
1970 future execution. */
1971 && !(paren && ln == 0))
1973 locinput = PL_reginput;
1974 if (regkind[(U8)OP(next)] == EXACT) {
1975 c1 = UCHARAT(OPERAND(next) + 1);
1976 if (OP(next) == EXACTF)
1978 else if (OP(next) == EXACTFL)
1979 c2 = fold_locale[c1];
1986 /* This may be improved if l == 0. */
1987 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1988 /* If it could work, try it. */
1990 UCHARAT(PL_reginput) == c1 ||
1991 UCHARAT(PL_reginput) == c2)
1995 PL_regstartp[paren] = HOPc(PL_reginput, -l);
1996 PL_regendp[paren] = PL_reginput;
1999 PL_regendp[paren] = NULL;
2005 /* Couldn't or didn't -- move forward. */
2006 PL_reginput = locinput;
2007 if (regrepeat_hard(scan, 1, &l)) {
2009 locinput = PL_reginput;
2016 n = regrepeat_hard(scan, n, &l);
2017 if (n != 0 && l == 0
2018 /* In fact, this is tricky. If paren, then the
2019 fact that we did/didnot match may influence
2020 future execution. */
2021 && !(paren && ln == 0))
2023 locinput = PL_reginput;
2025 PerlIO_printf(Perl_debug_log,
2026 "%*s matched %ld times, len=%ld...\n",
2027 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2030 if (regkind[(U8)OP(next)] == EXACT) {
2031 c1 = UCHARAT(OPERAND(next) + 1);
2032 if (OP(next) == EXACTF)
2034 else if (OP(next) == EXACTFL)
2035 c2 = fold_locale[c1];
2044 /* If it could work, try it. */
2046 UCHARAT(PL_reginput) == c1 ||
2047 UCHARAT(PL_reginput) == c2)
2050 PerlIO_printf(Perl_debug_log,
2051 "%*s trying tail with n=%ld...\n",
2052 REPORT_CODE_OFF+PL_regindent*2, "", n)
2056 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2057 PL_regendp[paren] = PL_reginput;
2060 PL_regendp[paren] = NULL;
2066 /* Couldn't or didn't -- back up. */
2068 locinput = HOPc(locinput, -l);
2069 PL_reginput = locinput;
2076 paren = scan->flags; /* Which paren to set */
2077 if (paren > PL_regsize)
2079 if (paren > *PL_reglastparen)
2080 *PL_reglastparen = paren;
2081 ln = ARG1(scan); /* min to match */
2082 n = ARG2(scan); /* max to match */
2083 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2087 ln = ARG1(scan); /* min to match */
2088 n = ARG2(scan); /* max to match */
2089 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2094 scan = NEXTOPER(scan);
2100 scan = NEXTOPER(scan);
2104 * Lookahead to avoid useless match attempts
2105 * when we know what character comes next.
2107 if (regkind[(U8)OP(next)] == EXACT) {
2108 c1 = UCHARAT(OPERAND(next) + 1);
2109 if (OP(next) == EXACTF)
2111 else if (OP(next) == EXACTFL)
2112 c2 = fold_locale[c1];
2118 PL_reginput = locinput;
2122 if (ln && regrepeat(scan, ln) < ln)
2124 locinput = PL_reginput;
2126 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2127 /* If it could work, try it. */
2129 UCHARAT(PL_reginput) == c1 ||
2130 UCHARAT(PL_reginput) == c2)
2134 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2135 PL_regendp[paren] = PL_reginput;
2138 PL_regendp[paren] = NULL;
2144 /* Couldn't or didn't -- move forward. */
2145 PL_reginput = locinput;
2146 if (regrepeat(scan, 1)) {
2148 locinput = PL_reginput;
2156 n = regrepeat(scan, n);
2157 locinput = PL_reginput;
2158 if (ln < n && regkind[(U8)OP(next)] == EOL &&
2159 (!PL_multiline || OP(next) == SEOL))
2160 ln = n; /* why back off? */
2164 /* If it could work, try it. */
2166 UCHARAT(PL_reginput) == c1 ||
2167 UCHARAT(PL_reginput) == c2)
2171 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2172 PL_regendp[paren] = PL_reginput;
2175 PL_regendp[paren] = NULL;
2181 /* Couldn't or didn't -- back up. */
2183 PL_reginput = locinput = HOPc(locinput, -1);
2188 /* If it could work, try it. */
2190 UCHARAT(PL_reginput) == c1 ||
2191 UCHARAT(PL_reginput) == c2)
2197 /* Couldn't or didn't -- back up. */
2199 PL_reginput = locinput = HOPc(locinput, -1);
2206 if (PL_reg_call_cc) {
2207 re_cc_state *cur_call_cc = PL_reg_call_cc;
2208 CURCUR *cctmp = PL_regcc;
2209 regexp *re = PL_reg_re;
2210 CHECKPOINT cp, lastcp;
2212 cp = regcppush(0); /* Save *all* the positions. */
2214 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2216 PL_reginput = locinput; /* Make position available to
2218 cache_re(PL_reg_call_cc->re);
2219 PL_regcc = PL_reg_call_cc->cc;
2220 PL_reg_call_cc = PL_reg_call_cc->prev;
2221 if (regmatch(cur_call_cc->node)) {
2222 PL_reg_call_cc = cur_call_cc;
2228 PL_reg_call_cc = cur_call_cc;
2234 PerlIO_printf(Perl_debug_log,
2235 "%*s continuation failed...\n",
2236 REPORT_CODE_OFF+PL_regindent*2, "")
2240 if (locinput < PL_regtill)
2241 sayNO; /* Cannot match: too short. */
2244 PL_reginput = locinput; /* put where regtry can find it */
2245 sayYES; /* Success! */
2252 s = HOPMAYBEc(locinput, -scan->flags);
2258 PL_reginput = locinput;
2263 s = HOPMAYBEc(locinput, -scan->flags);
2269 PL_reginput = locinput;
2272 inner = NEXTOPER(NEXTOPER(scan));
2273 if (regmatch(inner) != n) {
2288 if (OP(scan) == SUSPEND) {
2289 locinput = PL_reginput;
2290 nextchr = UCHARAT(locinput);
2295 next = scan + ARG(scan);
2300 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2301 (unsigned long)scan, OP(scan));
2302 FAIL("regexp memory corruption");
2308 * We get here only if there's trouble -- normally "case END" is
2309 * the terminating point.
2311 FAIL("corrupted regexp pointers");
2329 - regrepeat - repeatedly match something simple, report how many
2332 * [This routine now assumes that it will only match on things of length 1.
2333 * That was true before, but now we assume scan - reginput is the count,
2334 * rather than incrementing count on every character. [Er, except utf8.]]
2337 regrepeat(regnode *p, I32 max)
2340 register char *scan;
2341 register char *opnd;
2343 register char *loceol = PL_regeol;
2344 register I32 hardcount = 0;
2347 if (max != REG_INFTY && max < loceol - scan)
2348 loceol = scan + max;
2349 opnd = (char *) OPERAND(p);
2352 while (scan < loceol && *scan != '\n')
2360 while (scan < loceol && *scan != '\n') {
2361 scan += UTF8SKIP(scan);
2367 while (scan < loceol) {
2368 scan += UTF8SKIP(scan);
2372 case EXACT: /* length of string is 1 */
2373 c = UCHARAT(++opnd);
2374 while (scan < loceol && UCHARAT(scan) == c)
2377 case EXACTF: /* length of string is 1 */
2378 c = UCHARAT(++opnd);
2379 while (scan < loceol &&
2380 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
2383 case EXACTFL: /* length of string is 1 */
2384 PL_reg_flags |= RF_tainted;
2385 c = UCHARAT(++opnd);
2386 while (scan < loceol &&
2387 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
2392 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2393 scan += UTF8SKIP(scan);
2398 while (scan < loceol && REGINCLASS(opnd, *scan))
2402 while (scan < loceol && isALNUM(*scan))
2407 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2408 scan += UTF8SKIP(scan);
2413 PL_reg_flags |= RF_tainted;
2414 while (scan < loceol && isALNUM_LC(*scan))
2418 PL_reg_flags |= RF_tainted;
2420 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2421 scan += UTF8SKIP(scan);
2427 while (scan < loceol && !isALNUM(*scan))
2432 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2433 scan += UTF8SKIP(scan);
2438 PL_reg_flags |= RF_tainted;
2439 while (scan < loceol && !isALNUM_LC(*scan))
2443 PL_reg_flags |= RF_tainted;
2445 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2446 scan += UTF8SKIP(scan);
2451 while (scan < loceol && isSPACE(*scan))
2456 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2457 scan += UTF8SKIP(scan);
2462 PL_reg_flags |= RF_tainted;
2463 while (scan < loceol && isSPACE_LC(*scan))
2467 PL_reg_flags |= RF_tainted;
2469 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2470 scan += UTF8SKIP(scan);
2475 while (scan < loceol && !isSPACE(*scan))
2480 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2481 scan += UTF8SKIP(scan);
2486 PL_reg_flags |= RF_tainted;
2487 while (scan < loceol && !isSPACE_LC(*scan))
2491 PL_reg_flags |= RF_tainted;
2493 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2494 scan += UTF8SKIP(scan);
2499 while (scan < loceol && isDIGIT(*scan))
2504 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2505 scan += UTF8SKIP(scan);
2511 while (scan < loceol && !isDIGIT(*scan))
2516 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2517 scan += UTF8SKIP(scan);
2521 default: /* Called on something of 0 width. */
2522 break; /* So match right here or not at all. */
2528 c = scan - PL_reginput;
2533 SV *prop = sv_newmortal();
2536 PerlIO_printf(Perl_debug_log,
2537 "%*s %s can match %ld times out of %ld...\n",
2538 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2545 - regrepeat_hard - repeatedly match something, report total lenth and length
2547 * The repeater is supposed to have constant length.
2551 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2554 register char *scan;
2555 register char *start;
2556 register char *loceol = PL_regeol;
2558 I32 count = 0, res = 1;
2563 start = PL_reginput;
2565 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2568 while (start < PL_reginput) {
2570 start += UTF8SKIP(start);
2581 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2583 *lp = l = PL_reginput - start;
2584 if (max != REG_INFTY && l*max < loceol - scan)
2585 loceol = scan + l*max;
2598 - regclass - determine if a character falls into a character class
2602 reginclass(register char *p, register I32 c)
2609 if (ANYOF_TEST(p, c))
2611 else if (flags & ANYOF_FOLD) {
2613 if (flags & ANYOF_LOCALE) {
2614 PL_reg_flags |= RF_tainted;
2615 cf = fold_locale[c];
2619 if (ANYOF_TEST(p, cf))
2623 if (!match && (flags & ANYOF_ISA)) {
2624 PL_reg_flags |= RF_tainted;
2626 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2627 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2628 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2629 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2635 return (flags & ANYOF_INVERT) ? !match : match;
2639 reginclassutf8(regnode *f, U8 *p)
2642 char flags = ARG1(f);
2644 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2646 if (swash_fetch(sv, p))
2648 else if (flags & ANYOF_FOLD) {
2651 if (flags & ANYOF_LOCALE) {
2652 PL_reg_flags |= RF_tainted;
2653 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2656 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2657 if (swash_fetch(sv, tmpbuf))
2661 if (!match && (flags & ANYOF_ISA)) {
2662 PL_reg_flags |= RF_tainted;
2664 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2665 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2666 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2667 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2673 return (flags & ANYOF_INVERT) ? !match : match;
2677 reghop(U8 *s, I32 off)
2681 while (off-- && s < (U8*)PL_regeol)
2686 if (s > (U8*)PL_bostr) {
2689 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2691 } /* XXX could check well-formedness here */
2699 reghopmaybe(U8* s, I32 off)
2703 while (off-- && s < (U8*)PL_regeol)
2710 if (s > (U8*)PL_bostr) {
2713 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2715 } /* XXX could check well-formedness here */