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;
1674 cache_re(PL_reg_re);
1681 sv_setsv(save_scalar(PL_replgv), ret);
1685 n = ARG(scan); /* which paren pair */
1686 PL_reg_start_tmp[n] = locinput;
1691 n = ARG(scan); /* which paren pair */
1692 PL_regstartp[n] = PL_reg_start_tmp[n];
1693 PL_regendp[n] = locinput;
1694 if (n > *PL_reglastparen)
1695 *PL_reglastparen = n;
1698 n = ARG(scan); /* which paren pair */
1699 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1703 next = NEXTOPER(NEXTOPER(scan));
1705 next = scan + ARG(scan);
1706 if (OP(next) == IFTHEN) /* Fake one. */
1707 next = NEXTOPER(NEXTOPER(next));
1711 logical = scan->flags;
1715 CHECKPOINT cp = PL_savestack_ix;
1717 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1719 cc.oldcc = PL_regcc;
1721 cc.parenfloor = *PL_reglastparen;
1723 cc.min = ARG1(scan);
1724 cc.max = ARG2(scan);
1725 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1729 PL_reginput = locinput;
1730 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1732 PL_regcc = cc.oldcc;
1738 * This is really hard to understand, because after we match
1739 * what we're trying to match, we must make sure the rest of
1740 * the RE is going to match for sure, and to do that we have
1741 * to go back UP the parse tree by recursing ever deeper. And
1742 * if it fails, we have to reset our parent's current state
1743 * that we can try again after backing off.
1746 CHECKPOINT cp, lastcp;
1747 CURCUR* cc = PL_regcc;
1748 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1750 n = cc->cur + 1; /* how many we know we matched */
1751 PL_reginput = locinput;
1754 PerlIO_printf(Perl_debug_log,
1755 "%*s %ld out of %ld..%ld cc=%lx\n",
1756 REPORT_CODE_OFF+PL_regindent*2, "",
1757 (long)n, (long)cc->min,
1758 (long)cc->max, (long)cc)
1761 /* If degenerate scan matches "", assume scan done. */
1763 if (locinput == cc->lastloc && n >= cc->min) {
1764 PL_regcc = cc->oldcc;
1767 PerlIO_printf(Perl_debug_log,
1768 "%*s empty match detected, try continuation...\n",
1769 REPORT_CODE_OFF+PL_regindent*2, "")
1771 if (regmatch(cc->next))
1774 PerlIO_printf(Perl_debug_log,
1776 REPORT_CODE_OFF+PL_regindent*2, "")
1783 /* First just match a string of min scans. */
1787 cc->lastloc = locinput;
1788 if (regmatch(cc->scan))
1791 cc->lastloc = lastloc;
1793 PerlIO_printf(Perl_debug_log,
1795 REPORT_CODE_OFF+PL_regindent*2, "")
1800 /* Prefer next over scan for minimal matching. */
1803 PL_regcc = cc->oldcc;
1805 cp = regcppush(cc->parenfloor);
1807 if (regmatch(cc->next)) {
1809 sayYES; /* All done. */
1816 if (n >= cc->max) { /* Maximum greed exceeded? */
1817 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1818 && !(PL_reg_flags & RF_warned)) {
1819 PL_reg_flags |= RF_warned;
1820 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1821 "Complex regular subexpression recursion",
1828 PerlIO_printf(Perl_debug_log,
1829 "%*s trying longer...\n",
1830 REPORT_CODE_OFF+PL_regindent*2, "")
1832 /* Try scanning more and see if it helps. */
1833 PL_reginput = locinput;
1835 cc->lastloc = locinput;
1836 cp = regcppush(cc->parenfloor);
1838 if (regmatch(cc->scan)) {
1843 PerlIO_printf(Perl_debug_log,
1845 REPORT_CODE_OFF+PL_regindent*2, "")
1850 cc->lastloc = lastloc;
1854 /* Prefer scan over next for maximal matching. */
1856 if (n < cc->max) { /* More greed allowed? */
1857 cp = regcppush(cc->parenfloor);
1859 cc->lastloc = locinput;
1861 if (regmatch(cc->scan)) {
1866 regcppop(); /* Restore some previous $<digit>s? */
1867 PL_reginput = locinput;
1869 PerlIO_printf(Perl_debug_log,
1870 "%*s failed, try continuation...\n",
1871 REPORT_CODE_OFF+PL_regindent*2, "")
1874 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1875 && !(PL_reg_flags & RF_warned)) {
1876 PL_reg_flags |= RF_warned;
1877 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1878 "Complex regular subexpression recursion",
1882 /* Failed deeper matches of scan, so see if this one works. */
1883 PL_regcc = cc->oldcc;
1885 if (regmatch(cc->next))
1888 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1889 REPORT_CODE_OFF+PL_regindent*2, "")
1894 cc->lastloc = lastloc;
1899 next = scan + ARG(scan);
1902 inner = NEXTOPER(NEXTOPER(scan));
1905 inner = NEXTOPER(scan);
1910 if (OP(next) != c1) /* No choice. */
1911 next = inner; /* Avoid recursion. */
1913 int lastparen = *PL_reglastparen;
1917 PL_reginput = locinput;
1918 if (regmatch(inner))
1921 for (n = *PL_reglastparen; n > lastparen; n--)
1923 *PL_reglastparen = n;
1926 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1930 inner = NEXTOPER(scan);
1931 if (c1 == BRANCHJ) {
1932 inner = NEXTOPER(inner);
1934 } while (scan != NULL && OP(scan) == c1);
1948 /* We suppose that the next guy does not need
1949 backtracking: in particular, it is of constant length,
1950 and has no parenths to influence future backrefs. */
1951 ln = ARG1(scan); /* min to match */
1952 n = ARG2(scan); /* max to match */
1953 paren = scan->flags;
1955 if (paren > PL_regsize)
1957 if (paren > *PL_reglastparen)
1958 *PL_reglastparen = paren;
1960 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1962 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1963 PL_reginput = locinput;
1966 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1968 if (ln && l == 0 && n >= ln
1969 /* In fact, this is tricky. If paren, then the
1970 fact that we did/didnot match may influence
1971 future execution. */
1972 && !(paren && ln == 0))
1974 locinput = PL_reginput;
1975 if (regkind[(U8)OP(next)] == EXACT) {
1976 c1 = UCHARAT(OPERAND(next) + 1);
1977 if (OP(next) == EXACTF)
1979 else if (OP(next) == EXACTFL)
1980 c2 = fold_locale[c1];
1987 /* This may be improved if l == 0. */
1988 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1989 /* If it could work, try it. */
1991 UCHARAT(PL_reginput) == c1 ||
1992 UCHARAT(PL_reginput) == c2)
1996 PL_regstartp[paren] = HOPc(PL_reginput, -l);
1997 PL_regendp[paren] = PL_reginput;
2000 PL_regendp[paren] = NULL;
2006 /* Couldn't or didn't -- move forward. */
2007 PL_reginput = locinput;
2008 if (regrepeat_hard(scan, 1, &l)) {
2010 locinput = PL_reginput;
2017 n = regrepeat_hard(scan, n, &l);
2018 if (n != 0 && l == 0
2019 /* In fact, this is tricky. If paren, then the
2020 fact that we did/didnot match may influence
2021 future execution. */
2022 && !(paren && ln == 0))
2024 locinput = PL_reginput;
2026 PerlIO_printf(Perl_debug_log,
2027 "%*s matched %ld times, len=%ld...\n",
2028 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2031 if (regkind[(U8)OP(next)] == EXACT) {
2032 c1 = UCHARAT(OPERAND(next) + 1);
2033 if (OP(next) == EXACTF)
2035 else if (OP(next) == EXACTFL)
2036 c2 = fold_locale[c1];
2045 /* If it could work, try it. */
2047 UCHARAT(PL_reginput) == c1 ||
2048 UCHARAT(PL_reginput) == c2)
2051 PerlIO_printf(Perl_debug_log,
2052 "%*s trying tail with n=%ld...\n",
2053 REPORT_CODE_OFF+PL_regindent*2, "", n)
2057 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2058 PL_regendp[paren] = PL_reginput;
2061 PL_regendp[paren] = NULL;
2067 /* Couldn't or didn't -- back up. */
2069 locinput = HOPc(locinput, -l);
2070 PL_reginput = locinput;
2077 paren = scan->flags; /* Which paren to set */
2078 if (paren > PL_regsize)
2080 if (paren > *PL_reglastparen)
2081 *PL_reglastparen = paren;
2082 ln = ARG1(scan); /* min to match */
2083 n = ARG2(scan); /* max to match */
2084 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2088 ln = ARG1(scan); /* min to match */
2089 n = ARG2(scan); /* max to match */
2090 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2095 scan = NEXTOPER(scan);
2101 scan = NEXTOPER(scan);
2105 * Lookahead to avoid useless match attempts
2106 * when we know what character comes next.
2108 if (regkind[(U8)OP(next)] == EXACT) {
2109 c1 = UCHARAT(OPERAND(next) + 1);
2110 if (OP(next) == EXACTF)
2112 else if (OP(next) == EXACTFL)
2113 c2 = fold_locale[c1];
2119 PL_reginput = locinput;
2123 if (ln && regrepeat(scan, ln) < ln)
2125 locinput = PL_reginput;
2127 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2128 /* If it could work, try it. */
2130 UCHARAT(PL_reginput) == c1 ||
2131 UCHARAT(PL_reginput) == c2)
2135 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2136 PL_regendp[paren] = PL_reginput;
2139 PL_regendp[paren] = NULL;
2145 /* Couldn't or didn't -- move forward. */
2146 PL_reginput = locinput;
2147 if (regrepeat(scan, 1)) {
2149 locinput = PL_reginput;
2157 n = regrepeat(scan, n);
2158 locinput = PL_reginput;
2159 if (ln < n && regkind[(U8)OP(next)] == EOL &&
2160 (!PL_multiline || OP(next) == SEOL))
2161 ln = n; /* why back off? */
2165 /* If it could work, try it. */
2167 UCHARAT(PL_reginput) == c1 ||
2168 UCHARAT(PL_reginput) == c2)
2172 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2173 PL_regendp[paren] = PL_reginput;
2176 PL_regendp[paren] = NULL;
2182 /* Couldn't or didn't -- back up. */
2184 PL_reginput = locinput = HOPc(locinput, -1);
2189 /* If it could work, try it. */
2191 UCHARAT(PL_reginput) == c1 ||
2192 UCHARAT(PL_reginput) == c2)
2198 /* Couldn't or didn't -- back up. */
2200 PL_reginput = locinput = HOPc(locinput, -1);
2207 if (PL_reg_call_cc) {
2208 re_cc_state *cur_call_cc = PL_reg_call_cc;
2209 CURCUR *cctmp = PL_regcc;
2210 regexp *re = PL_reg_re;
2211 CHECKPOINT cp, lastcp;
2213 cp = regcppush(0); /* Save *all* the positions. */
2215 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2217 PL_reginput = locinput; /* Make position available to
2219 cache_re(PL_reg_call_cc->re);
2220 PL_regcc = PL_reg_call_cc->cc;
2221 PL_reg_call_cc = PL_reg_call_cc->prev;
2222 if (regmatch(cur_call_cc->node)) {
2223 PL_reg_call_cc = cur_call_cc;
2229 PL_reg_call_cc = cur_call_cc;
2235 PerlIO_printf(Perl_debug_log,
2236 "%*s continuation failed...\n",
2237 REPORT_CODE_OFF+PL_regindent*2, "")
2241 if (locinput < PL_regtill)
2242 sayNO; /* Cannot match: too short. */
2245 PL_reginput = locinput; /* put where regtry can find it */
2246 sayYES; /* Success! */
2249 PL_reginput = locinput;
2254 s = HOPMAYBEc(locinput, -scan->flags);
2260 PL_reginput = locinput;
2265 s = HOPMAYBEc(locinput, -scan->flags);
2271 PL_reginput = locinput;
2274 inner = NEXTOPER(NEXTOPER(scan));
2275 if (regmatch(inner) != n) {
2290 if (OP(scan) == SUSPEND) {
2291 locinput = PL_reginput;
2292 nextchr = UCHARAT(locinput);
2297 next = scan + ARG(scan);
2302 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2303 (unsigned long)scan, OP(scan));
2304 FAIL("regexp memory corruption");
2310 * We get here only if there's trouble -- normally "case END" is
2311 * the terminating point.
2313 FAIL("corrupted regexp pointers");
2331 - regrepeat - repeatedly match something simple, report how many
2334 * [This routine now assumes that it will only match on things of length 1.
2335 * That was true before, but now we assume scan - reginput is the count,
2336 * rather than incrementing count on every character. [Er, except utf8.]]
2339 regrepeat(regnode *p, I32 max)
2342 register char *scan;
2343 register char *opnd;
2345 register char *loceol = PL_regeol;
2346 register I32 hardcount = 0;
2349 if (max != REG_INFTY && max < loceol - scan)
2350 loceol = scan + max;
2351 opnd = (char *) OPERAND(p);
2354 while (scan < loceol && *scan != '\n')
2362 while (scan < loceol && *scan != '\n') {
2363 scan += UTF8SKIP(scan);
2369 while (scan < loceol) {
2370 scan += UTF8SKIP(scan);
2374 case EXACT: /* length of string is 1 */
2375 c = UCHARAT(++opnd);
2376 while (scan < loceol && UCHARAT(scan) == c)
2379 case EXACTF: /* length of string is 1 */
2380 c = UCHARAT(++opnd);
2381 while (scan < loceol &&
2382 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
2385 case EXACTFL: /* length of string is 1 */
2386 PL_reg_flags |= RF_tainted;
2387 c = UCHARAT(++opnd);
2388 while (scan < loceol &&
2389 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
2394 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2395 scan += UTF8SKIP(scan);
2400 while (scan < loceol && REGINCLASS(opnd, *scan))
2404 while (scan < loceol && isALNUM(*scan))
2409 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2410 scan += UTF8SKIP(scan);
2415 PL_reg_flags |= RF_tainted;
2416 while (scan < loceol && isALNUM_LC(*scan))
2420 PL_reg_flags |= RF_tainted;
2422 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2423 scan += UTF8SKIP(scan);
2429 while (scan < loceol && !isALNUM(*scan))
2434 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2435 scan += UTF8SKIP(scan);
2440 PL_reg_flags |= RF_tainted;
2441 while (scan < loceol && !isALNUM_LC(*scan))
2445 PL_reg_flags |= RF_tainted;
2447 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2448 scan += UTF8SKIP(scan);
2453 while (scan < loceol && isSPACE(*scan))
2458 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2459 scan += UTF8SKIP(scan);
2464 PL_reg_flags |= RF_tainted;
2465 while (scan < loceol && isSPACE_LC(*scan))
2469 PL_reg_flags |= RF_tainted;
2471 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2472 scan += UTF8SKIP(scan);
2477 while (scan < loceol && !isSPACE(*scan))
2482 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2483 scan += UTF8SKIP(scan);
2488 PL_reg_flags |= RF_tainted;
2489 while (scan < loceol && !isSPACE_LC(*scan))
2493 PL_reg_flags |= RF_tainted;
2495 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2496 scan += UTF8SKIP(scan);
2501 while (scan < loceol && isDIGIT(*scan))
2506 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2507 scan += UTF8SKIP(scan);
2513 while (scan < loceol && !isDIGIT(*scan))
2518 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2519 scan += UTF8SKIP(scan);
2523 default: /* Called on something of 0 width. */
2524 break; /* So match right here or not at all. */
2530 c = scan - PL_reginput;
2535 SV *prop = sv_newmortal();
2538 PerlIO_printf(Perl_debug_log,
2539 "%*s %s can match %ld times out of %ld...\n",
2540 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2547 - regrepeat_hard - repeatedly match something, report total lenth and length
2549 * The repeater is supposed to have constant length.
2553 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2556 register char *scan;
2557 register char *start;
2558 register char *loceol = PL_regeol;
2560 I32 count = 0, res = 1;
2565 start = PL_reginput;
2567 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2570 while (start < PL_reginput) {
2572 start += UTF8SKIP(start);
2583 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2585 *lp = l = PL_reginput - start;
2586 if (max != REG_INFTY && l*max < loceol - scan)
2587 loceol = scan + l*max;
2600 - regclass - determine if a character falls into a character class
2604 reginclass(register char *p, register I32 c)
2611 if (ANYOF_TEST(p, c))
2613 else if (flags & ANYOF_FOLD) {
2615 if (flags & ANYOF_LOCALE) {
2616 PL_reg_flags |= RF_tainted;
2617 cf = fold_locale[c];
2621 if (ANYOF_TEST(p, cf))
2625 if (!match && (flags & ANYOF_ISA)) {
2626 PL_reg_flags |= RF_tainted;
2628 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2629 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2630 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2631 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2637 return (flags & ANYOF_INVERT) ? !match : match;
2641 reginclassutf8(regnode *f, U8 *p)
2644 char flags = ARG1(f);
2646 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2648 if (swash_fetch(sv, p))
2650 else if (flags & ANYOF_FOLD) {
2653 if (flags & ANYOF_LOCALE) {
2654 PL_reg_flags |= RF_tainted;
2655 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2658 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2659 if (swash_fetch(sv, tmpbuf))
2663 if (!match && (flags & ANYOF_ISA)) {
2664 PL_reg_flags |= RF_tainted;
2666 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2667 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2668 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2669 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2675 return (flags & ANYOF_INVERT) ? !match : match;
2679 reghop(U8 *s, I32 off)
2683 while (off-- && s < (U8*)PL_regeol)
2688 if (s > (U8*)PL_bostr) {
2691 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2693 } /* XXX could check well-formedness here */
2701 reghopmaybe(U8* s, I32 off)
2705 while (off-- && s < (U8*)PL_regeol)
2712 if (s > (U8*)PL_bostr) {
2715 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2717 } /* XXX could check well-formedness here */