5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregexec my_pregexec
44 * pregcomp and pregexec -- regsub and regerror are not used in perl
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
63 **** Alterations to Henry's code are...
65 **** Copyright (c) 1991-1998, Larry Wall
67 **** You may distribute under the terms of either the GNU General Public
68 **** License or the Artistic License, as specified in the README file.
70 * Beware that some of this code is subtly aware of the way operator
71 * precedence is structured in regular expressions. Serious changes in
72 * regular-expression syntax might require a total rethink.
79 #define RF_tainted 1 /* tainted information used? */
80 #define RF_warned 2 /* warned about big count? */
81 #define RF_evaled 4 /* Did an EVAL with setting? */
82 #define RF_utf8 8 /* String contains multibyte chars? */
84 #define UTF (PL_reg_flags & RF_utf8)
86 #define RS_init 1 /* eval environment created */
87 #define RS_set 2 /* replsv value is set */
94 typedef I32 CHECKPOINT;
100 static I32 regmatch _((regnode *prog));
101 static I32 regrepeat _((regnode *p, I32 max));
102 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
103 static I32 regtry _((regexp *prog, char *startpos));
105 static bool reginclass _((char *p, I32 c));
106 static bool reginclassutf8 _((regnode *f, U8* p));
107 static CHECKPOINT regcppush _((I32 parenfloor));
108 static char * regcppop _((void));
109 static char * regcp_set_to _((I32 ss));
110 static void cache_re _((regexp *prog));
113 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
114 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
116 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
117 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
120 static U8 * reghop _((U8 *pos, I32 off));
121 static U8 * reghopmaybe _((U8 *pos, I32 off));
123 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
124 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
125 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
126 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
127 #define HOPc(pos,off) ((char*)HOP(pos,off))
128 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
131 regcppush(I32 parenfloor)
134 int retval = PL_savestack_ix;
135 int i = (PL_regsize - parenfloor) * 4;
139 for (p = PL_regsize; p > parenfloor; p--) {
140 SSPUSHPTR(PL_regendp[p]);
141 SSPUSHPTR(PL_regstartp[p]);
142 SSPUSHPTR(PL_reg_start_tmp[p]);
145 SSPUSHINT(PL_regsize);
146 SSPUSHINT(*PL_reglastparen);
147 SSPUSHPTR(PL_reginput);
149 SSPUSHINT(SAVEt_REGCONTEXT);
153 /* These are needed since we do not localize EVAL nodes: */
154 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
155 " Setting an EVAL scope, savestack=%i\n", \
156 PL_savestack_ix)); lastcp = PL_savestack_ix
158 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
159 PerlIO_printf(Perl_debug_log, \
160 " Clearing an EVAL scope, savestack=%i..%i\n", \
161 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
171 assert(i == SAVEt_REGCONTEXT);
173 input = (char *) SSPOPPTR;
174 *PL_reglastparen = SSPOPINT;
175 PL_regsize = SSPOPINT;
176 for (i -= 3; i > 0; i -= 4) {
177 paren = (U32)SSPOPINT;
178 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
179 PL_regstartp[paren] = (char *) SSPOPPTR;
180 tmps = (char*)SSPOPPTR;
181 if (paren <= *PL_reglastparen)
182 PL_regendp[paren] = tmps;
184 PerlIO_printf(Perl_debug_log,
185 " restoring \\%d to %d(%d)..%d%s\n",
186 paren, PL_regstartp[paren] - PL_regbol,
187 PL_reg_start_tmp[paren] - PL_regbol,
188 PL_regendp[paren] - PL_regbol,
189 (paren > *PL_reglastparen ? "(no)" : ""));
193 if (*PL_reglastparen + 1 <= PL_regnpar) {
194 PerlIO_printf(Perl_debug_log,
195 " restoring \\%d..\\%d to undef\n",
196 *PL_reglastparen + 1, PL_regnpar);
199 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
200 if (paren > PL_regsize)
201 PL_regstartp[paren] = Nullch;
202 PL_regendp[paren] = Nullch;
211 I32 tmp = PL_savestack_ix;
213 PL_savestack_ix = ss;
215 PL_savestack_ix = tmp;
219 typedef struct re_cc_state
223 struct re_cc_state *prev;
228 #define regcpblow(cp) LEAVE_SCOPE(cp)
231 * pregexec and friends
235 - pregexec - match a regexp against a string
238 pregexec(register regexp *prog, char *stringarg, register char *strend,
239 char *strbeg, I32 minend, SV *screamer, U32 nosave)
240 /* strend: pointer to null at end of string */
241 /* strbeg: real beginning of string */
242 /* minend: end of match must be >=minend after stringarg. */
243 /* nosave: For optimizations. */
246 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
247 nosave ? 0 : REXEC_COPY_STR);
251 cache_re(regexp *prog)
254 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
256 PL_regprogram = prog->program;
258 PL_regnpar = prog->nparens;
259 PL_regdata = prog->data;
264 - regexec_flags - match a regexp against a string
267 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
268 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
269 /* strend: pointer to null at end of string */
270 /* strbeg: real beginning of string */
271 /* minend: end of match must be >=minend after stringarg. */
272 /* data: May be used for some additional optimizations. */
273 /* nosave: For optimizations. */
278 register char *startpos = stringarg;
280 I32 minlen; /* must match at least this many chars */
281 I32 dontbother = 0; /* how many characters not to try at end */
283 I32 start_shift = 0; /* Offset of the start to find
284 constant substr. */ /* CC */
285 I32 end_shift = 0; /* Same for the end. */ /* CC */
286 I32 scream_pos = -1; /* Internal iterator of scream. */
288 SV* oreplsv = GvSV(PL_replgv);
296 PL_regnarrate = PL_debug & 512;
300 if (prog == NULL || startpos == NULL) {
301 croak("NULL regexp parameter");
305 minlen = prog->minlen;
306 if (strend - startpos < minlen) goto phooey;
308 if (startpos == strbeg) /* is ^ valid at stringarg? */
311 PL_regprev = (U32)stringarg[-1];
312 if (!PL_multiline && PL_regprev == '\n')
313 PL_regprev = '\0'; /* force ^ to NOT match */
316 /* Check validity of program. */
317 if (UCHARAT(prog->program) != REG_MAGIC) {
318 FAIL("corrupted regexp program");
324 if (prog->reganch & ROPT_UTF8)
325 PL_reg_flags |= RF_utf8;
327 /* Mark beginning of line for ^ and lookbehind. */
328 PL_regbol = startpos;
331 /* Mark end of line for $ (and such) */
334 /* see how far we have to get to not match where we matched before */
335 PL_regtill = startpos+minend;
337 /* We start without call_cc context. */
340 /* If there is a "must appear" string, look for it. */
342 if (!(flags & REXEC_CHECKED)
343 && prog->check_substr != Nullsv &&
344 !(prog->reganch & ROPT_ANCH_GPOS) &&
345 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
346 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
349 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
350 /* Should be nonnegative! */
351 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
352 if (flags & REXEC_SCREAM) {
353 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
354 s = screaminstr(sv, prog->check_substr,
355 start_shift + (stringarg - strbeg),
356 end_shift, &scream_pos, 0);
362 s = fbm_instr((unsigned char*)s + start_shift,
363 (unsigned char*)strend - end_shift,
364 prog->check_substr, 0);
366 ++BmUSEFUL(prog->check_substr); /* hooray */
367 goto phooey; /* not present */
369 else if (s - stringarg > prog->check_offset_max &&
371 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
372 : (t = s - prog->check_offset_max) != 0
376 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
379 else if (!(prog->reganch & ROPT_NAUGHTY)
380 && --BmUSEFUL(prog->check_substr) < 0
381 && prog->check_substr == prog->float_substr) { /* boo */
382 SvREFCNT_dec(prog->check_substr);
383 prog->check_substr = Nullsv; /* disable */
384 prog->float_substr = Nullsv; /* clear */
392 PerlIO_printf(Perl_debug_log,
393 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
394 PL_colors[4],PL_colors[5],PL_colors[0],
397 (strlen(prog->precomp) > 60 ? "..." : ""),
399 (strend - startpos > 60 ? 60 : strend - startpos),
400 startpos, PL_colors[1],
401 (strend - startpos > 60 ? "..." : ""))
404 if (prog->reganch & ROPT_GPOS_SEEN) {
408 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)
409 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
411 PL_reg_ganch = startpos + pos;
414 /* Simplest case: anchored match need be tried only once. */
415 /* [unless only anchor is BOL and multiline is set] */
416 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
417 if (regtry(prog, startpos))
419 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
420 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
423 dontbother = minlen - 1;
424 strend = HOPc(strend, -dontbother);
425 /* for multiline we only have to try after newlines */
429 if (*s++ == '\n') { /* don't need PL_utf8skip here */
430 if (s < strend && regtry(prog, s))
436 } else if (prog->reganch & ROPT_ANCH_GPOS) {
437 if (regtry(prog, PL_reg_ganch))
442 /* Messy cases: unanchored match. */
443 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
444 /* we have /x+whatever/ */
445 /* it must be a one character string */
446 char ch = SvPVX(prog->anchored_substr)[0];
450 if (regtry(prog, s)) goto got_it;
452 while (s < strend && *s == ch)
461 if (regtry(prog, s)) goto got_it;
463 while (s < strend && *s == ch)
471 else if (prog->anchored_substr != Nullsv
472 || (prog->float_substr != Nullsv
473 && prog->float_max_offset < strend - s)) {
474 SV *must = prog->anchored_substr
475 ? prog->anchored_substr : prog->float_substr;
477 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
479 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
480 I32 delta = back_max - back_min;
481 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
482 char *last1; /* Last position checked before */
487 last1 = s - 1; /* bogus */
489 /* XXXX check_substr already used to find `s', can optimize if
490 check_substr==must. */
492 dontbother = end_shift;
493 strend = HOPc(strend, -dontbother);
494 while ( (s <= last) &&
495 ((flags & REXEC_SCREAM)
496 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
497 end_shift, &scream_pos, 0))
498 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
499 (unsigned char*)strend, must, 0))) ) {
500 if (HOPc(s, -back_max) > last1) {
501 last1 = HOPc(s, -back_min);
502 s = HOPc(s, -back_max);
505 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
507 last1 = HOPc(s, -back_min);
527 else if (c = prog->regstclass) {
528 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
532 dontbother = minlen - 1;
533 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
535 /* We know what class it must start with. */
538 cc = (char *) OPERAND(c);
540 if (REGINCLASSUTF8(c, (U8*)s)) {
541 if (tmp && regtry(prog, s))
552 cc = (char *) OPERAND(c);
554 if (REGINCLASS(cc, *s)) {
555 if (tmp && regtry(prog, s))
566 PL_reg_flags |= RF_tainted;
573 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
574 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
576 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
583 if ((minlen || tmp) && regtry(prog,s))
587 PL_reg_flags |= RF_tainted;
592 strend = reghop_c(strend, -1);
594 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
595 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
597 if (tmp == !(OP(c) == BOUND ?
598 swash_fetch(PL_utf8_alnum, (U8*)s) :
599 isALNUM_LC_utf8((U8*)s)))
607 if ((minlen || tmp) && regtry(prog,s))
611 PL_reg_flags |= RF_tainted;
618 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
619 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
621 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
623 else if (regtry(prog, s))
627 if ((minlen || !tmp) && regtry(prog,s))
631 PL_reg_flags |= RF_tainted;
636 strend = reghop_c(strend, -1);
638 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
639 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
641 if (tmp == !(OP(c) == NBOUND ?
642 swash_fetch(PL_utf8_alnum, (U8*)s) :
643 isALNUM_LC_utf8((U8*)s)))
645 else if (regtry(prog, s))
649 if ((minlen || !tmp) && regtry(prog,s))
655 if (tmp && regtry(prog, s))
667 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
668 if (tmp && regtry(prog, s))
679 PL_reg_flags |= RF_tainted;
681 if (isALNUM_LC(*s)) {
682 if (tmp && regtry(prog, s))
693 PL_reg_flags |= RF_tainted;
695 if (isALNUM_LC_utf8((U8*)s)) {
696 if (tmp && regtry(prog, s))
709 if (tmp && regtry(prog, s))
721 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
722 if (tmp && regtry(prog, s))
733 PL_reg_flags |= RF_tainted;
735 if (!isALNUM_LC(*s)) {
736 if (tmp && regtry(prog, s))
747 PL_reg_flags |= RF_tainted;
749 if (!isALNUM_LC_utf8((U8*)s)) {
750 if (tmp && regtry(prog, s))
763 if (tmp && regtry(prog, s))
775 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
776 if (tmp && regtry(prog, s))
787 PL_reg_flags |= RF_tainted;
789 if (isSPACE_LC(*s)) {
790 if (tmp && regtry(prog, s))
801 PL_reg_flags |= RF_tainted;
803 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
804 if (tmp && regtry(prog, s))
817 if (tmp && regtry(prog, s))
829 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
830 if (tmp && regtry(prog, s))
841 PL_reg_flags |= RF_tainted;
843 if (!isSPACE_LC(*s)) {
844 if (tmp && regtry(prog, s))
855 PL_reg_flags |= RF_tainted;
857 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
858 if (tmp && regtry(prog, s))
871 if (tmp && regtry(prog, s))
883 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
884 if (tmp && regtry(prog, s))
897 if (tmp && regtry(prog, s))
909 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
910 if (tmp && regtry(prog, s))
924 if (prog->float_substr != Nullsv) { /* Trim the end. */
926 I32 oldpos = scream_pos;
928 if (flags & REXEC_SCREAM) {
929 last = screaminstr(sv, prog->float_substr, s - strbeg,
930 end_shift, &scream_pos, 1); /* last one */
932 last = scream_olds; /* Only one occurence. */
937 char *little = SvPV(prog->float_substr, len);
939 last = rninstr(s, strend, little, little + len);
941 last = strend; /* matching `$' */
943 if (last == NULL) goto phooey; /* Should not happen! */
944 dontbother = strend - last + prog->float_min_offset;
946 if (minlen && (dontbother < minlen))
947 dontbother = minlen - 1;
948 strend -= dontbother; /* this one's always in bytes! */
949 /* We don't know much -- general case. */
963 } while (s++ < strend);
971 prog->subbeg = strbeg;
972 prog->subend = PL_regeol; /* strend may have been modified */
973 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
975 /* make sure $`, $&, $', and $digit will work later */
976 if (strbeg != prog->subbase) { /* second+ //g match. */
977 if (!(flags & REXEC_COPY_STR)) {
979 Safefree(prog->subbase);
980 prog->subbase = Nullch;
984 I32 i = PL_regeol - startpos + (stringarg - strbeg);
985 s = savepvn(strbeg, i);
986 Safefree(prog->subbase);
988 prog->subbeg = prog->subbase;
989 prog->subend = prog->subbase + i;
990 s = prog->subbase + (stringarg - strbeg);
991 for (i = 0; i <= prog->nparens; i++) {
993 prog->startp[i] = s + (prog->startp[i] - startpos);
994 prog->endp[i] = s + (prog->endp[i] - startpos);
999 /* Preserve the current value of $^R */
1000 if (oreplsv != GvSV(PL_replgv)) {
1001 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1002 restored, the value remains
1012 - regtry - try match at specific point
1014 STATIC I32 /* 0 failure, 1 success */
1015 regtry(regexp *prog, char *startpos)
1023 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1024 PL_reg_eval_set = RS_init;
1026 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1027 PL_stack_sp - PL_stack_base);
1029 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1030 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1031 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1033 /* Apparently this is not needed, judging by wantarray. */
1034 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1035 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1037 PL_reginput = startpos;
1038 PL_regstartp = prog->startp;
1039 PL_regendp = prog->endp;
1040 PL_reglastparen = &prog->lastparen;
1041 prog->lastparen = 0;
1043 if (PL_reg_start_tmpl <= prog->nparens) {
1044 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1045 if(PL_reg_start_tmp)
1046 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1048 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1053 if (prog->nparens) {
1054 for (i = prog->nparens; i >= 0; i--) {
1060 if (regmatch(prog->program + 1)) {
1061 prog->startp[0] = startpos;
1062 prog->endp[0] = PL_reginput;
1070 - regmatch - main matching routine
1072 * Conceptually the strategy is simple: check to see whether the current
1073 * node matches, call self recursively to see whether the rest matches,
1074 * and then act accordingly. In practice we make some effort to avoid
1075 * recursion, in particular by going through "ordinary" nodes (that don't
1076 * need to know whether the rest of the match failed) by a loop instead of
1079 /* [lwall] I've hoisted the register declarations to the outer block in order to
1080 * maybe save a little bit of pushing and popping on the stack. It also takes
1081 * advantage of machines that use a register save mask on subroutine entry.
1083 STATIC I32 /* 0 failure, 1 success */
1084 regmatch(regnode *prog)
1087 register regnode *scan; /* Current node. */
1088 regnode *next; /* Next node. */
1089 regnode *inner; /* Next node in internal branch. */
1090 register I32 nextchr; /* renamed nextchr - nextchar colides with
1091 function of same name */
1092 register I32 n; /* no or next */
1093 register I32 ln; /* len or last */
1094 register char *s; /* operand or save */
1095 register char *locinput = PL_reginput;
1096 register I32 c1, c2, paren; /* case fold search, parenth */
1097 int minmod = 0, sw = 0, logical = 0;
1102 /* Note that nextchr is a byte even in UTF */
1103 nextchr = UCHARAT(locinput);
1105 while (scan != NULL) {
1106 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1108 # define sayYES goto yes
1109 # define sayNO goto no
1110 # define saySAME(x) if (x) goto yes; else goto no
1111 # define REPORT_CODE_OFF 24
1113 # define sayYES return 1
1114 # define sayNO return 0
1115 # define saySAME(x) return x
1118 SV *prop = sv_newmortal();
1119 int docolor = *PL_colors[0];
1120 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1121 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1122 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1123 ? (5 + taill) - l : locinput - PL_bostr);
1124 int pref0_len = pref_len - (locinput - PL_reginput);
1126 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1127 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1128 ? (5 + taill) - pref_len : PL_regeol - locinput);
1131 regprop(prop, scan);
1132 PerlIO_printf(Perl_debug_log,
1133 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1134 locinput - PL_bostr,
1135 PL_colors[4], pref0_len,
1136 locinput - pref_len, PL_colors[5],
1137 PL_colors[2], pref_len - pref0_len,
1138 locinput - pref_len + pref0_len, PL_colors[3],
1139 (docolor ? "" : "> <"),
1140 PL_colors[0], l, locinput, PL_colors[1],
1141 15 - l - pref_len + 1,
1143 scan - PL_regprogram, PL_regindent*2, "",
1147 next = scan + NEXT_OFF(scan);
1153 if (locinput == PL_bostr
1154 ? PL_regprev == '\n'
1156 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1158 /* regtill = regbol; */
1163 if (locinput == PL_bostr
1164 ? PL_regprev == '\n'
1165 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1171 if (locinput == PL_regbol && PL_regprev == '\n')
1175 if (locinput == PL_reg_ganch)
1185 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1190 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1192 if (PL_regeol - locinput > 1)
1196 if (PL_regeol != locinput)
1200 if (nextchr & 0x80) {
1201 locinput += PL_utf8skip[nextchr];
1202 if (locinput > PL_regeol)
1204 nextchr = UCHARAT(locinput);
1207 if (!nextchr && locinput >= PL_regeol)
1209 nextchr = UCHARAT(++locinput);
1212 if (!nextchr && locinput >= PL_regeol)
1214 nextchr = UCHARAT(++locinput);
1217 if (nextchr & 0x80) {
1218 locinput += PL_utf8skip[nextchr];
1219 if (locinput > PL_regeol)
1221 nextchr = UCHARAT(locinput);
1224 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1226 nextchr = UCHARAT(++locinput);
1229 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1231 nextchr = UCHARAT(++locinput);
1234 s = (char *) OPERAND(scan);
1236 /* Inline the first character, for speed. */
1237 if (UCHARAT(s) != nextchr)
1239 if (PL_regeol - locinput < ln)
1241 if (ln > 1 && memNE(s, locinput, ln))
1244 nextchr = UCHARAT(locinput);
1247 PL_reg_flags |= RF_tainted;
1250 s = (char *) OPERAND(scan);
1256 c1 = OP(scan) == EXACTF;
1260 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1261 toLOWER_utf8((U8*)l) :
1262 toLOWER_LC_utf8((U8*)l)))
1270 nextchr = UCHARAT(locinput);
1274 /* Inline the first character, for speed. */
1275 if (UCHARAT(s) != nextchr &&
1276 UCHARAT(s) != ((OP(scan) == EXACTF)
1277 ? PL_fold : PL_fold_locale)[nextchr])
1279 if (PL_regeol - locinput < ln)
1281 if (ln > 1 && (OP(scan) == EXACTF
1282 ? ibcmp(s, locinput, ln)
1283 : ibcmp_locale(s, locinput, ln)))
1286 nextchr = UCHARAT(locinput);
1289 s = (char *) OPERAND(scan);
1290 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1292 if (locinput >= PL_regeol)
1294 locinput += PL_utf8skip[nextchr];
1295 nextchr = UCHARAT(locinput);
1298 s = (char *) OPERAND(scan);
1300 nextchr = UCHARAT(locinput);
1301 if (!REGINCLASS(s, nextchr))
1303 if (!nextchr && locinput >= PL_regeol)
1305 nextchr = UCHARAT(++locinput);
1308 PL_reg_flags |= RF_tainted;
1313 if (!(OP(scan) == ALNUM
1314 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1316 nextchr = UCHARAT(++locinput);
1319 PL_reg_flags |= RF_tainted;
1324 if (nextchr & 0x80) {
1325 if (!(OP(scan) == ALNUMUTF8
1326 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1327 : isALNUM_LC_utf8((U8*)locinput)))
1331 locinput += PL_utf8skip[nextchr];
1332 nextchr = UCHARAT(locinput);
1335 if (!(OP(scan) == ALNUMUTF8
1336 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1338 nextchr = UCHARAT(++locinput);
1341 PL_reg_flags |= RF_tainted;
1344 if (!nextchr && locinput >= PL_regeol)
1346 if (OP(scan) == NALNUM
1347 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1349 nextchr = UCHARAT(++locinput);
1352 PL_reg_flags |= RF_tainted;
1355 if (!nextchr && locinput >= PL_regeol)
1357 if (nextchr & 0x80) {
1358 if (OP(scan) == NALNUMUTF8
1359 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1360 : isALNUM_LC_utf8((U8*)locinput))
1364 locinput += PL_utf8skip[nextchr];
1365 nextchr = UCHARAT(locinput);
1368 if (OP(scan) == NALNUMUTF8
1369 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1371 nextchr = UCHARAT(++locinput);
1375 PL_reg_flags |= RF_tainted;
1379 /* was last char in word? */
1380 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1381 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1383 n = isALNUM(nextchr);
1386 ln = isALNUM_LC(ln);
1387 n = isALNUM_LC(nextchr);
1389 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1394 PL_reg_flags |= RF_tainted;
1398 /* was last char in word? */
1399 ln = (locinput != PL_regbol)
1400 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1401 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1402 ln = isALNUM_uni(ln);
1403 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1406 ln = isALNUM_LC_uni(ln);
1407 n = isALNUM_LC_utf8((U8*)locinput);
1409 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1413 PL_reg_flags |= RF_tainted;
1416 if (!nextchr && locinput >= PL_regeol)
1418 if (!(OP(scan) == SPACE
1419 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1421 nextchr = UCHARAT(++locinput);
1424 PL_reg_flags |= RF_tainted;
1427 if (!nextchr && locinput >= PL_regeol)
1429 if (nextchr & 0x80) {
1430 if (!(OP(scan) == SPACEUTF8
1431 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1432 : isSPACE_LC_utf8((U8*)locinput)))
1436 locinput += PL_utf8skip[nextchr];
1437 nextchr = UCHARAT(locinput);
1440 if (!(OP(scan) == SPACEUTF8
1441 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1443 nextchr = UCHARAT(++locinput);
1446 PL_reg_flags |= RF_tainted;
1451 if (OP(scan) == SPACE
1452 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1454 nextchr = UCHARAT(++locinput);
1457 PL_reg_flags |= RF_tainted;
1462 if (nextchr & 0x80) {
1463 if (OP(scan) == NSPACEUTF8
1464 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1465 : isSPACE_LC_utf8((U8*)locinput))
1469 locinput += PL_utf8skip[nextchr];
1470 nextchr = UCHARAT(locinput);
1473 if (OP(scan) == NSPACEUTF8
1474 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1476 nextchr = UCHARAT(++locinput);
1479 if (!isDIGIT(nextchr))
1481 nextchr = UCHARAT(++locinput);
1484 if (nextchr & 0x80) {
1485 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1487 locinput += PL_utf8skip[nextchr];
1488 nextchr = UCHARAT(locinput);
1491 if (!isDIGIT(nextchr))
1493 nextchr = UCHARAT(++locinput);
1496 if (!nextchr && locinput >= PL_regeol)
1498 if (isDIGIT(nextchr))
1500 nextchr = UCHARAT(++locinput);
1503 if (!nextchr && locinput >= PL_regeol)
1505 if (nextchr & 0x80) {
1506 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1508 locinput += PL_utf8skip[nextchr];
1509 nextchr = UCHARAT(locinput);
1512 if (isDIGIT(nextchr))
1514 nextchr = UCHARAT(++locinput);
1517 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1519 locinput += PL_utf8skip[nextchr];
1520 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1521 locinput += UTF8SKIP(locinput);
1522 if (locinput > PL_regeol)
1524 nextchr = UCHARAT(locinput);
1527 PL_reg_flags |= RF_tainted;
1531 n = ARG(scan); /* which paren pair */
1532 s = PL_regstartp[n];
1533 if (*PL_reglastparen < n || !s)
1534 sayNO; /* Do not match unless seen CLOSEn. */
1535 if (s == PL_regendp[n])
1538 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1540 char *e = PL_regendp[n];
1542 * Note that we can't do the "other character" lookup trick as
1543 * in the 8-bit case (no pun intended) because in Unicode we
1544 * have to map both upper and title case to lower case.
1546 if (OP(scan) == REFF) {
1550 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1560 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1567 nextchr = UCHARAT(locinput);
1571 /* Inline the first character, for speed. */
1572 if (UCHARAT(s) != nextchr &&
1574 (UCHARAT(s) != ((OP(scan) == REFF
1575 ? PL_fold : PL_fold_locale)[nextchr]))))
1577 ln = PL_regendp[n] - s;
1578 if (locinput + ln > PL_regeol)
1580 if (ln > 1 && (OP(scan) == REF
1581 ? memNE(s, locinput, ln)
1583 ? ibcmp(s, locinput, ln)
1584 : ibcmp_locale(s, locinput, ln))))
1587 nextchr = UCHARAT(locinput);
1598 OP_4tree *oop = PL_op;
1599 COP *ocurcop = PL_curcop;
1600 SV **ocurpad = PL_curpad;
1604 PL_op = (OP_4tree*)PL_regdata->data[n];
1605 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1606 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1608 CALLRUNOPS(); /* Scalar context. */
1614 PL_curpad = ocurpad;
1615 PL_curcop = ocurcop;
1617 if (logical == 2) { /* Postponed subexpression. */
1619 MAGIC *mg = Null(MAGIC*);
1622 CHECKPOINT cp, lastcp;
1624 if(SvROK(ret) || SvRMAGICAL(ret)) {
1625 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1628 mg = mg_find(sv, 'r');
1631 re = (regexp *)mg->mg_obj;
1632 (void)ReREFCNT_inc(re);
1636 char *t = SvPV(ret, len);
1638 char *oprecomp = PL_regprecomp;
1639 I32 osize = PL_regsize;
1640 I32 onpar = PL_regnpar;
1643 re = CALLREGCOMP(t, t + len, &pm);
1645 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1646 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1647 PL_regprecomp = oprecomp;
1652 PerlIO_printf(Perl_debug_log,
1653 "Entering embedded `%s%.60s%s%s'\n",
1657 (strlen(re->precomp) > 60 ? "..." : ""))
1660 state.prev = PL_reg_call_cc;
1661 state.cc = PL_regcc;
1662 state.re = PL_reg_re;
1668 cp = regcppush(0); /* Save *all* the positions. */
1671 state.ss = PL_savestack_ix;
1672 *PL_reglastparen = 0;
1673 PL_reg_call_cc = &state;
1674 PL_reginput = locinput;
1675 if (regmatch(re->program + 1)) {
1681 PerlIO_printf(Perl_debug_log,
1683 REPORT_CODE_OFF+PL_regindent*2, "")
1688 PL_reg_call_cc = state.prev;
1689 PL_regcc = state.cc;
1690 PL_reg_re = state.re;
1691 cache_re(PL_reg_re);
1698 sv_setsv(save_scalar(PL_replgv), ret);
1702 n = ARG(scan); /* which paren pair */
1703 PL_reg_start_tmp[n] = locinput;
1708 n = ARG(scan); /* which paren pair */
1709 PL_regstartp[n] = PL_reg_start_tmp[n];
1710 PL_regendp[n] = locinput;
1711 if (n > *PL_reglastparen)
1712 *PL_reglastparen = n;
1715 n = ARG(scan); /* which paren pair */
1716 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1720 next = NEXTOPER(NEXTOPER(scan));
1722 next = scan + ARG(scan);
1723 if (OP(next) == IFTHEN) /* Fake one. */
1724 next = NEXTOPER(NEXTOPER(next));
1728 logical = scan->flags;
1732 CHECKPOINT cp = PL_savestack_ix;
1734 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1736 cc.oldcc = PL_regcc;
1738 cc.parenfloor = *PL_reglastparen;
1740 cc.min = ARG1(scan);
1741 cc.max = ARG2(scan);
1742 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1746 PL_reginput = locinput;
1747 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1749 PL_regcc = cc.oldcc;
1755 * This is really hard to understand, because after we match
1756 * what we're trying to match, we must make sure the rest of
1757 * the RE is going to match for sure, and to do that we have
1758 * to go back UP the parse tree by recursing ever deeper. And
1759 * if it fails, we have to reset our parent's current state
1760 * that we can try again after backing off.
1763 CHECKPOINT cp, lastcp;
1764 CURCUR* cc = PL_regcc;
1765 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1767 n = cc->cur + 1; /* how many we know we matched */
1768 PL_reginput = locinput;
1771 PerlIO_printf(Perl_debug_log,
1772 "%*s %ld out of %ld..%ld cc=%lx\n",
1773 REPORT_CODE_OFF+PL_regindent*2, "",
1774 (long)n, (long)cc->min,
1775 (long)cc->max, (long)cc)
1778 /* If degenerate scan matches "", assume scan done. */
1780 if (locinput == cc->lastloc && n >= cc->min) {
1781 PL_regcc = cc->oldcc;
1784 PerlIO_printf(Perl_debug_log,
1785 "%*s empty match detected, try continuation...\n",
1786 REPORT_CODE_OFF+PL_regindent*2, "")
1788 if (regmatch(cc->next))
1791 PerlIO_printf(Perl_debug_log,
1793 REPORT_CODE_OFF+PL_regindent*2, "")
1800 /* First just match a string of min scans. */
1804 cc->lastloc = locinput;
1805 if (regmatch(cc->scan))
1808 cc->lastloc = lastloc;
1810 PerlIO_printf(Perl_debug_log,
1812 REPORT_CODE_OFF+PL_regindent*2, "")
1817 /* Prefer next over scan for minimal matching. */
1820 PL_regcc = cc->oldcc;
1822 cp = regcppush(cc->parenfloor);
1824 if (regmatch(cc->next)) {
1826 sayYES; /* All done. */
1833 if (n >= cc->max) { /* Maximum greed exceeded? */
1834 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1835 && !(PL_reg_flags & RF_warned)) {
1836 PL_reg_flags |= RF_warned;
1837 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1838 "Complex regular subexpression recursion",
1845 PerlIO_printf(Perl_debug_log,
1846 "%*s trying longer...\n",
1847 REPORT_CODE_OFF+PL_regindent*2, "")
1849 /* Try scanning more and see if it helps. */
1850 PL_reginput = locinput;
1852 cc->lastloc = locinput;
1853 cp = regcppush(cc->parenfloor);
1855 if (regmatch(cc->scan)) {
1860 PerlIO_printf(Perl_debug_log,
1862 REPORT_CODE_OFF+PL_regindent*2, "")
1867 cc->lastloc = lastloc;
1871 /* Prefer scan over next for maximal matching. */
1873 if (n < cc->max) { /* More greed allowed? */
1874 cp = regcppush(cc->parenfloor);
1876 cc->lastloc = locinput;
1878 if (regmatch(cc->scan)) {
1883 regcppop(); /* Restore some previous $<digit>s? */
1884 PL_reginput = locinput;
1886 PerlIO_printf(Perl_debug_log,
1887 "%*s failed, try continuation...\n",
1888 REPORT_CODE_OFF+PL_regindent*2, "")
1891 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1892 && !(PL_reg_flags & RF_warned)) {
1893 PL_reg_flags |= RF_warned;
1894 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1895 "Complex regular subexpression recursion",
1899 /* Failed deeper matches of scan, so see if this one works. */
1900 PL_regcc = cc->oldcc;
1902 if (regmatch(cc->next))
1905 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1906 REPORT_CODE_OFF+PL_regindent*2, "")
1911 cc->lastloc = lastloc;
1916 next = scan + ARG(scan);
1919 inner = NEXTOPER(NEXTOPER(scan));
1922 inner = NEXTOPER(scan);
1927 if (OP(next) != c1) /* No choice. */
1928 next = inner; /* Avoid recursion. */
1930 int lastparen = *PL_reglastparen;
1934 PL_reginput = locinput;
1935 if (regmatch(inner))
1938 for (n = *PL_reglastparen; n > lastparen; n--)
1940 *PL_reglastparen = n;
1943 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1947 inner = NEXTOPER(scan);
1948 if (c1 == BRANCHJ) {
1949 inner = NEXTOPER(inner);
1951 } while (scan != NULL && OP(scan) == c1);
1965 /* We suppose that the next guy does not need
1966 backtracking: in particular, it is of constant length,
1967 and has no parenths to influence future backrefs. */
1968 ln = ARG1(scan); /* min to match */
1969 n = ARG2(scan); /* max to match */
1970 paren = scan->flags;
1972 if (paren > PL_regsize)
1974 if (paren > *PL_reglastparen)
1975 *PL_reglastparen = paren;
1977 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1979 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1980 PL_reginput = locinput;
1983 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1985 if (ln && l == 0 && n >= ln
1986 /* In fact, this is tricky. If paren, then the
1987 fact that we did/didnot match may influence
1988 future execution. */
1989 && !(paren && ln == 0))
1991 locinput = PL_reginput;
1992 if (PL_regkind[(U8)OP(next)] == EXACT) {
1993 c1 = UCHARAT(OPERAND(next) + 1);
1994 if (OP(next) == EXACTF)
1996 else if (OP(next) == EXACTFL)
1997 c2 = PL_fold_locale[c1];
2004 /* This may be improved if l == 0. */
2005 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2006 /* If it could work, try it. */
2008 UCHARAT(PL_reginput) == c1 ||
2009 UCHARAT(PL_reginput) == c2)
2013 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2014 PL_regendp[paren] = PL_reginput;
2017 PL_regendp[paren] = NULL;
2023 /* Couldn't or didn't -- move forward. */
2024 PL_reginput = locinput;
2025 if (regrepeat_hard(scan, 1, &l)) {
2027 locinput = PL_reginput;
2034 n = regrepeat_hard(scan, n, &l);
2035 if (n != 0 && l == 0
2036 /* In fact, this is tricky. If paren, then the
2037 fact that we did/didnot match may influence
2038 future execution. */
2039 && !(paren && ln == 0))
2041 locinput = PL_reginput;
2043 PerlIO_printf(Perl_debug_log,
2044 "%*s matched %ld times, len=%ld...\n",
2045 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2048 if (PL_regkind[(U8)OP(next)] == EXACT) {
2049 c1 = UCHARAT(OPERAND(next) + 1);
2050 if (OP(next) == EXACTF)
2052 else if (OP(next) == EXACTFL)
2053 c2 = PL_fold_locale[c1];
2062 /* If it could work, try it. */
2064 UCHARAT(PL_reginput) == c1 ||
2065 UCHARAT(PL_reginput) == c2)
2068 PerlIO_printf(Perl_debug_log,
2069 "%*s trying tail with n=%ld...\n",
2070 REPORT_CODE_OFF+PL_regindent*2, "", n)
2074 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2075 PL_regendp[paren] = PL_reginput;
2078 PL_regendp[paren] = NULL;
2084 /* Couldn't or didn't -- back up. */
2086 locinput = HOPc(locinput, -l);
2087 PL_reginput = locinput;
2094 paren = scan->flags; /* Which paren to set */
2095 if (paren > PL_regsize)
2097 if (paren > *PL_reglastparen)
2098 *PL_reglastparen = paren;
2099 ln = ARG1(scan); /* min to match */
2100 n = ARG2(scan); /* max to match */
2101 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2105 ln = ARG1(scan); /* min to match */
2106 n = ARG2(scan); /* max to match */
2107 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2112 scan = NEXTOPER(scan);
2118 scan = NEXTOPER(scan);
2122 * Lookahead to avoid useless match attempts
2123 * when we know what character comes next.
2125 if (PL_regkind[(U8)OP(next)] == EXACT) {
2126 c1 = UCHARAT(OPERAND(next) + 1);
2127 if (OP(next) == EXACTF)
2129 else if (OP(next) == EXACTFL)
2130 c2 = PL_fold_locale[c1];
2136 PL_reginput = locinput;
2140 if (ln && regrepeat(scan, ln) < ln)
2142 locinput = PL_reginput;
2144 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2145 /* If it could work, try it. */
2147 UCHARAT(PL_reginput) == c1 ||
2148 UCHARAT(PL_reginput) == c2)
2152 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2153 PL_regendp[paren] = PL_reginput;
2156 PL_regendp[paren] = NULL;
2162 /* Couldn't or didn't -- move forward. */
2163 PL_reginput = locinput;
2164 if (regrepeat(scan, 1)) {
2166 locinput = PL_reginput;
2174 n = regrepeat(scan, n);
2175 locinput = PL_reginput;
2176 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2177 (!PL_multiline || OP(next) == SEOL))
2178 ln = n; /* why back off? */
2182 /* If it could work, try it. */
2184 UCHARAT(PL_reginput) == c1 ||
2185 UCHARAT(PL_reginput) == c2)
2189 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2190 PL_regendp[paren] = PL_reginput;
2193 PL_regendp[paren] = NULL;
2199 /* Couldn't or didn't -- back up. */
2201 PL_reginput = locinput = HOPc(locinput, -1);
2206 /* If it could work, try it. */
2208 UCHARAT(PL_reginput) == c1 ||
2209 UCHARAT(PL_reginput) == c2)
2215 /* Couldn't or didn't -- back up. */
2217 PL_reginput = locinput = HOPc(locinput, -1);
2224 if (PL_reg_call_cc) {
2225 re_cc_state *cur_call_cc = PL_reg_call_cc;
2226 CURCUR *cctmp = PL_regcc;
2227 regexp *re = PL_reg_re;
2228 CHECKPOINT cp, lastcp;
2230 cp = regcppush(0); /* Save *all* the positions. */
2232 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2234 PL_reginput = locinput; /* Make position available to
2236 cache_re(PL_reg_call_cc->re);
2237 PL_regcc = PL_reg_call_cc->cc;
2238 PL_reg_call_cc = PL_reg_call_cc->prev;
2239 if (regmatch(cur_call_cc->node)) {
2240 PL_reg_call_cc = cur_call_cc;
2246 PL_reg_call_cc = cur_call_cc;
2252 PerlIO_printf(Perl_debug_log,
2253 "%*s continuation failed...\n",
2254 REPORT_CODE_OFF+PL_regindent*2, "")
2258 if (locinput < PL_regtill)
2259 sayNO; /* Cannot match: too short. */
2262 PL_reginput = locinput; /* put where regtry can find it */
2263 sayYES; /* Success! */
2266 PL_reginput = locinput;
2271 s = HOPMAYBEc(locinput, -scan->flags);
2277 PL_reginput = locinput;
2282 s = HOPMAYBEc(locinput, -scan->flags);
2283 if (!s || s < PL_bostr)
2288 PL_reginput = locinput;
2291 inner = NEXTOPER(NEXTOPER(scan));
2292 if (regmatch(inner) != n) {
2307 if (OP(scan) == SUSPEND) {
2308 locinput = PL_reginput;
2309 nextchr = UCHARAT(locinput);
2314 next = scan + ARG(scan);
2319 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2320 (unsigned long)scan, OP(scan));
2321 FAIL("regexp memory corruption");
2327 * We get here only if there's trouble -- normally "case END" is
2328 * the terminating point.
2330 FAIL("corrupted regexp pointers");
2348 - regrepeat - repeatedly match something simple, report how many
2351 * [This routine now assumes that it will only match on things of length 1.
2352 * That was true before, but now we assume scan - reginput is the count,
2353 * rather than incrementing count on every character. [Er, except utf8.]]
2356 regrepeat(regnode *p, I32 max)
2359 register char *scan;
2360 register char *opnd;
2362 register char *loceol = PL_regeol;
2363 register I32 hardcount = 0;
2366 if (max != REG_INFTY && max < loceol - scan)
2367 loceol = scan + max;
2368 opnd = (char *) OPERAND(p);
2371 while (scan < loceol && *scan != '\n')
2379 while (scan < loceol && *scan != '\n') {
2380 scan += UTF8SKIP(scan);
2386 while (scan < loceol) {
2387 scan += UTF8SKIP(scan);
2391 case EXACT: /* length of string is 1 */
2392 c = UCHARAT(++opnd);
2393 while (scan < loceol && UCHARAT(scan) == c)
2396 case EXACTF: /* length of string is 1 */
2397 c = UCHARAT(++opnd);
2398 while (scan < loceol &&
2399 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2402 case EXACTFL: /* length of string is 1 */
2403 PL_reg_flags |= RF_tainted;
2404 c = UCHARAT(++opnd);
2405 while (scan < loceol &&
2406 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2411 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2412 scan += UTF8SKIP(scan);
2417 while (scan < loceol && REGINCLASS(opnd, *scan))
2421 while (scan < loceol && isALNUM(*scan))
2426 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2427 scan += UTF8SKIP(scan);
2432 PL_reg_flags |= RF_tainted;
2433 while (scan < loceol && isALNUM_LC(*scan))
2437 PL_reg_flags |= RF_tainted;
2439 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2440 scan += UTF8SKIP(scan);
2446 while (scan < loceol && !isALNUM(*scan))
2451 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2452 scan += UTF8SKIP(scan);
2457 PL_reg_flags |= RF_tainted;
2458 while (scan < loceol && !isALNUM_LC(*scan))
2462 PL_reg_flags |= RF_tainted;
2464 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2465 scan += UTF8SKIP(scan);
2470 while (scan < loceol && isSPACE(*scan))
2475 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2476 scan += UTF8SKIP(scan);
2481 PL_reg_flags |= RF_tainted;
2482 while (scan < loceol && isSPACE_LC(*scan))
2486 PL_reg_flags |= RF_tainted;
2488 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2489 scan += UTF8SKIP(scan);
2494 while (scan < loceol && !isSPACE(*scan))
2499 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2500 scan += UTF8SKIP(scan);
2505 PL_reg_flags |= RF_tainted;
2506 while (scan < loceol && !isSPACE_LC(*scan))
2510 PL_reg_flags |= RF_tainted;
2512 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2513 scan += UTF8SKIP(scan);
2518 while (scan < loceol && isDIGIT(*scan))
2523 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2524 scan += UTF8SKIP(scan);
2530 while (scan < loceol && !isDIGIT(*scan))
2535 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2536 scan += UTF8SKIP(scan);
2540 default: /* Called on something of 0 width. */
2541 break; /* So match right here or not at all. */
2547 c = scan - PL_reginput;
2552 SV *prop = sv_newmortal();
2555 PerlIO_printf(Perl_debug_log,
2556 "%*s %s can match %ld times out of %ld...\n",
2557 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2564 - regrepeat_hard - repeatedly match something, report total lenth and length
2566 * The repeater is supposed to have constant length.
2570 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2573 register char *scan;
2574 register char *start;
2575 register char *loceol = PL_regeol;
2577 I32 count = 0, res = 1;
2582 start = PL_reginput;
2584 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2587 while (start < PL_reginput) {
2589 start += UTF8SKIP(start);
2600 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2602 *lp = l = PL_reginput - start;
2603 if (max != REG_INFTY && l*max < loceol - scan)
2604 loceol = scan + l*max;
2617 - regclass - determine if a character falls into a character class
2621 reginclass(register char *p, register I32 c)
2628 if (ANYOF_TEST(p, c))
2630 else if (flags & ANYOF_FOLD) {
2632 if (flags & ANYOF_LOCALE) {
2633 PL_reg_flags |= RF_tainted;
2634 cf = PL_fold_locale[c];
2638 if (ANYOF_TEST(p, cf))
2642 if (!match && (flags & ANYOF_ISA)) {
2643 PL_reg_flags |= RF_tainted;
2645 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2646 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2647 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2648 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2654 return (flags & ANYOF_INVERT) ? !match : match;
2658 reginclassutf8(regnode *f, U8 *p)
2661 char flags = ARG1(f);
2663 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2665 if (swash_fetch(sv, p))
2667 else if (flags & ANYOF_FOLD) {
2670 if (flags & ANYOF_LOCALE) {
2671 PL_reg_flags |= RF_tainted;
2672 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2675 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2676 if (swash_fetch(sv, tmpbuf))
2680 if (!match && (flags & ANYOF_ISA)) {
2681 PL_reg_flags |= RF_tainted;
2683 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2684 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2685 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2686 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2692 return (flags & ANYOF_INVERT) ? !match : match;
2696 reghop(U8 *s, I32 off)
2700 while (off-- && s < (U8*)PL_regeol)
2705 if (s > (U8*)PL_bostr) {
2708 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2710 } /* XXX could check well-formedness here */
2718 reghopmaybe(U8* s, I32 off)
2722 while (off-- && s < (U8*)PL_regeol)
2729 if (s > (U8*)PL_bostr) {
2732 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2734 } /* XXX could check well-formedness here */