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));
110 static char * regcp_set_to _((I32 ss));
111 static void cache_re _((regexp *prog));
114 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
115 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
117 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
118 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
121 static U8 * reghop _((U8 *pos, I32 off));
122 static U8 * reghopmaybe _((U8 *pos, I32 off));
124 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
125 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
126 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
127 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
128 #define HOPc(pos,off) ((char*)HOP(pos,off))
129 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
132 regcppush(I32 parenfloor)
135 int retval = PL_savestack_ix;
136 int i = (PL_regsize - parenfloor) * 4;
140 for (p = PL_regsize; p > parenfloor; p--) {
141 SSPUSHPTR(PL_regendp[p]);
142 SSPUSHPTR(PL_regstartp[p]);
143 SSPUSHPTR(PL_reg_start_tmp[p]);
146 SSPUSHINT(PL_regsize);
147 SSPUSHINT(*PL_reglastparen);
148 SSPUSHPTR(PL_reginput);
150 SSPUSHINT(SAVEt_REGCONTEXT);
154 /* These are needed since we do not localize EVAL nodes: */
155 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
156 " Setting an EVAL scope, savestack=%i\n", \
157 PL_savestack_ix)); lastcp = PL_savestack_ix
159 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
160 PerlIO_printf(Perl_debug_log, \
161 " Clearing an EVAL scope, savestack=%i..%i\n", \
162 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
172 assert(i == SAVEt_REGCONTEXT);
174 input = (char *) SSPOPPTR;
175 *PL_reglastparen = SSPOPINT;
176 PL_regsize = SSPOPINT;
177 for (i -= 3; i > 0; i -= 4) {
178 paren = (U32)SSPOPINT;
179 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
180 PL_regstartp[paren] = (char *) SSPOPPTR;
181 tmps = (char*)SSPOPPTR;
182 if (paren <= *PL_reglastparen)
183 PL_regendp[paren] = tmps;
185 PerlIO_printf(Perl_debug_log,
186 " restoring \\%d to %d(%d)..%d%s\n",
187 paren, PL_regstartp[paren] - PL_regbol,
188 PL_reg_start_tmp[paren] - PL_regbol,
189 PL_regendp[paren] - PL_regbol,
190 (paren > *PL_reglastparen ? "(no)" : ""));
194 if (*PL_reglastparen + 1 <= PL_regnpar) {
195 PerlIO_printf(Perl_debug_log,
196 " restoring \\%d..\\%d to undef\n",
197 *PL_reglastparen + 1, PL_regnpar);
200 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
201 if (paren > PL_regsize)
202 PL_regstartp[paren] = Nullch;
203 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)
253 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
255 PL_regprogram = prog->program;
257 PL_regnpar = prog->nparens;
258 PL_regdata = prog->data;
263 - regexec_flags - match a regexp against a string
266 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
267 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
268 /* strend: pointer to null at end of string */
269 /* strbeg: real beginning of string */
270 /* minend: end of match must be >=minend after stringarg. */
271 /* data: May be used for some additional optimizations. */
272 /* nosave: For optimizations. */
277 register char *startpos = stringarg;
279 I32 minlen; /* must match at least this many chars */
280 I32 dontbother = 0; /* how many characters not to try at end */
282 I32 start_shift = 0; /* Offset of the start to find
283 constant substr. */ /* CC */
284 I32 end_shift = 0; /* Same for the end. */ /* CC */
285 I32 scream_pos = -1; /* Internal iterator of scream. */
287 SV* oreplsv = GvSV(PL_replgv);
295 PL_regnarrate = PL_debug & 512;
299 if (prog == NULL || startpos == NULL) {
300 croak("NULL regexp parameter");
304 minlen = prog->minlen;
305 if (strend - startpos < minlen) goto phooey;
307 if (startpos == strbeg) /* is ^ valid at stringarg? */
310 PL_regprev = (U32)stringarg[-1];
311 if (!PL_multiline && PL_regprev == '\n')
312 PL_regprev = '\0'; /* force ^ to NOT match */
315 /* Check validity of program. */
316 if (UCHARAT(prog->program) != MAGIC) {
317 FAIL("corrupted regexp program");
323 if (prog->reganch & ROPT_UTF8)
324 PL_reg_flags |= RF_utf8;
326 /* Mark beginning of line for ^ and lookbehind. */
327 PL_regbol = startpos;
330 /* Mark end of line for $ (and such) */
333 /* see how far we have to get to not match where we matched before */
334 PL_regtill = startpos+minend;
336 /* We start without call_cc context. */
339 /* If there is a "must appear" string, look for it. */
341 if (!(flags & REXEC_CHECKED)
342 && prog->check_substr != Nullsv &&
343 !(prog->reganch & ROPT_ANCH_GPOS) &&
344 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
345 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
348 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
349 /* Should be nonnegative! */
350 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
352 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
353 s = screaminstr(screamer, prog->check_substr,
354 start_shift + (stringarg - strbeg),
355 end_shift, &scream_pos, 0);
361 s = fbm_instr((unsigned char*)s + start_shift,
362 (unsigned char*)strend - end_shift,
363 prog->check_substr, 0);
365 ++BmUSEFUL(prog->check_substr); /* hooray */
366 goto phooey; /* not present */
368 else if (s - stringarg > prog->check_offset_max &&
370 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
371 : (t = s - prog->check_offset_max) != 0
375 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
378 else if (!(prog->reganch & ROPT_NAUGHTY)
379 && --BmUSEFUL(prog->check_substr) < 0
380 && prog->check_substr == prog->float_substr) { /* boo */
381 SvREFCNT_dec(prog->check_substr);
382 prog->check_substr = Nullsv; /* disable */
383 prog->float_substr = Nullsv; /* clear */
391 PerlIO_printf(Perl_debug_log,
392 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
393 PL_colors[4],PL_colors[5],PL_colors[0],
396 (strlen(prog->precomp) > 60 ? "..." : ""),
398 (strend - startpos > 60 ? 60 : strend - startpos),
399 startpos, PL_colors[1],
400 (strend - startpos > 60 ? "..." : ""))
403 /* Simplest case: anchored match need be tried only once. */
404 /* [unless only anchor is BOL and multiline is set] */
405 if (prog->reganch & ROPT_ANCH) {
406 if (regtry(prog, startpos))
408 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
409 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
410 || (prog->reganch & ROPT_ANCH_MBOL)))
413 dontbother = minlen - 1;
414 strend = HOPc(strend, -dontbother);
415 /* for multiline we only have to try after newlines */
419 if (*s++ == '\n') { /* don't need utf8skip here */
420 if (s < strend && regtry(prog, s))
428 /* Messy cases: unanchored match. */
429 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
430 /* we have /x+whatever/ */
431 /* it must be a one character string */
432 char ch = SvPVX(prog->anchored_substr)[0];
436 if (regtry(prog, s)) goto got_it;
438 while (s < strend && *s == ch)
447 if (regtry(prog, s)) goto got_it;
449 while (s < strend && *s == ch)
457 else if (prog->anchored_substr != Nullsv
458 || (prog->float_substr != Nullsv
459 && prog->float_max_offset < strend - s)) {
460 SV *must = prog->anchored_substr
461 ? prog->anchored_substr : prog->float_substr;
463 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
465 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
466 I32 delta = back_max - back_min;
467 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
468 char *last1; /* Last position checked before */
473 last1 = s - 1; /* bogus */
475 /* XXXX check_substr already used to find `s', can optimize if
476 check_substr==must. */
478 dontbother = end_shift;
479 strend = HOPc(strend, -dontbother);
480 while ( (s <= last) &&
482 ? (s = screaminstr(screamer, must, HOPc(s, back_min) - strbeg,
483 end_shift, &scream_pos, 0))
484 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
485 (unsigned char*)strend, must, 0))) ) {
486 if (HOPc(s, -back_max) > last1) {
487 last1 = HOPc(s, -back_min);
488 s = HOPc(s, -back_max);
491 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
493 last1 = HOPc(s, -back_min);
513 else if (c = prog->regstclass) {
514 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
518 dontbother = minlen - 1;
519 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
521 /* We know what class it must start with. */
524 cc = (char *) OPERAND(c);
526 if (REGINCLASSUTF8(c, (U8*)s)) {
527 if (tmp && regtry(prog, s))
538 cc = (char *) OPERAND(c);
540 if (REGINCLASS(cc, *s)) {
541 if (tmp && regtry(prog, s))
552 PL_reg_flags |= RF_tainted;
559 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
560 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
562 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
569 if ((minlen || tmp) && regtry(prog,s))
573 PL_reg_flags |= RF_tainted;
578 strend = reghop_c(strend, -1);
580 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
581 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
583 if (tmp == !(OP(c) == BOUND ?
584 swash_fetch(PL_utf8_alnum, (U8*)s) :
585 isALNUM_LC_utf8((U8*)s)))
593 if ((minlen || tmp) && regtry(prog,s))
597 PL_reg_flags |= RF_tainted;
604 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
605 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
607 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
609 else if (regtry(prog, s))
613 if ((minlen || !tmp) && regtry(prog,s))
617 PL_reg_flags |= RF_tainted;
622 strend = reghop_c(strend, -1);
624 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
625 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
627 if (tmp == !(OP(c) == NBOUND ?
628 swash_fetch(PL_utf8_alnum, (U8*)s) :
629 isALNUM_LC_utf8((U8*)s)))
631 else if (regtry(prog, s))
635 if ((minlen || !tmp) && regtry(prog,s))
641 if (tmp && regtry(prog, s))
653 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
654 if (tmp && regtry(prog, s))
665 PL_reg_flags |= RF_tainted;
667 if (isALNUM_LC(*s)) {
668 if (tmp && regtry(prog, s))
679 PL_reg_flags |= RF_tainted;
681 if (isALNUM_LC_utf8((U8*)s)) {
682 if (tmp && regtry(prog, s))
695 if (tmp && regtry(prog, s))
707 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
708 if (tmp && regtry(prog, s))
719 PL_reg_flags |= RF_tainted;
721 if (!isALNUM_LC(*s)) {
722 if (tmp && regtry(prog, s))
733 PL_reg_flags |= RF_tainted;
735 if (!isALNUM_LC_utf8((U8*)s)) {
736 if (tmp && regtry(prog, s))
749 if (tmp && regtry(prog, s))
761 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
762 if (tmp && regtry(prog, s))
773 PL_reg_flags |= RF_tainted;
775 if (isSPACE_LC(*s)) {
776 if (tmp && regtry(prog, s))
787 PL_reg_flags |= RF_tainted;
789 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
790 if (tmp && regtry(prog, s))
803 if (tmp && regtry(prog, s))
815 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
816 if (tmp && regtry(prog, s))
827 PL_reg_flags |= RF_tainted;
829 if (!isSPACE_LC(*s)) {
830 if (tmp && regtry(prog, s))
841 PL_reg_flags |= RF_tainted;
843 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
844 if (tmp && regtry(prog, s))
857 if (tmp && regtry(prog, s))
869 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
870 if (tmp && regtry(prog, s))
883 if (tmp && regtry(prog, s))
895 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
896 if (tmp && regtry(prog, s))
910 if (prog->float_substr != Nullsv) { /* Trim the end. */
912 I32 oldpos = scream_pos;
915 last = screaminstr(screamer, prog->float_substr, s - strbeg,
916 end_shift, &scream_pos, 1); /* last one */
918 last = scream_olds; /* Only one occurence. */
923 char *little = SvPV(prog->float_substr, len);
925 last = rninstr(s, strend, little, little + len);
927 last = strend; /* matching `$' */
929 if (last == NULL) goto phooey; /* Should not happen! */
930 dontbother = strend - last + prog->float_min_offset;
932 if (minlen && (dontbother < minlen))
933 dontbother = minlen - 1;
934 strend -= dontbother; /* this one's always in bytes! */
935 /* We don't know much -- general case. */
949 } while (s++ < strend);
957 prog->subbeg = strbeg;
958 prog->subend = PL_regeol; /* strend may have been modified */
959 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
961 /* make sure $`, $&, $', and $digit will work later */
962 if (strbeg != prog->subbase) { /* second+ //g match. */
963 if (!(flags & REXEC_COPY_STR)) {
965 Safefree(prog->subbase);
966 prog->subbase = Nullch;
970 I32 i = PL_regeol - startpos + (stringarg - strbeg);
971 s = savepvn(strbeg, i);
972 Safefree(prog->subbase);
974 prog->subbeg = prog->subbase;
975 prog->subend = prog->subbase + i;
976 s = prog->subbase + (stringarg - strbeg);
977 for (i = 0; i <= prog->nparens; i++) {
979 prog->startp[i] = s + (prog->startp[i] - startpos);
980 prog->endp[i] = s + (prog->endp[i] - startpos);
985 /* Preserve the current value of $^R */
986 if (oreplsv != GvSV(PL_replgv)) {
987 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
988 restored, the value remains
998 - regtry - try match at specific point
1000 STATIC I32 /* 0 failure, 1 success */
1001 regtry(regexp *prog, char *startpos)
1009 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1010 PL_reg_eval_set = RS_init;
1012 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1013 PL_stack_sp - PL_stack_base);
1015 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1016 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1017 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1019 /* Apparently this is not needed, judging by wantarray. */
1020 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1021 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1023 PL_reginput = startpos;
1024 PL_regstartp = prog->startp;
1025 PL_regendp = prog->endp;
1026 PL_reglastparen = &prog->lastparen;
1027 prog->lastparen = 0;
1029 if (PL_reg_start_tmpl <= prog->nparens) {
1030 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1031 if(PL_reg_start_tmp)
1032 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1034 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1039 if (prog->nparens) {
1040 for (i = prog->nparens; i >= 0; i--) {
1046 if (regmatch(prog->program + 1)) {
1047 prog->startp[0] = startpos;
1048 prog->endp[0] = PL_reginput;
1056 - regmatch - main matching routine
1058 * Conceptually the strategy is simple: check to see whether the current
1059 * node matches, call self recursively to see whether the rest matches,
1060 * and then act accordingly. In practice we make some effort to avoid
1061 * recursion, in particular by going through "ordinary" nodes (that don't
1062 * need to know whether the rest of the match failed) by a loop instead of
1065 /* [lwall] I've hoisted the register declarations to the outer block in order to
1066 * maybe save a little bit of pushing and popping on the stack. It also takes
1067 * advantage of machines that use a register save mask on subroutine entry.
1069 STATIC I32 /* 0 failure, 1 success */
1070 regmatch(regnode *prog)
1073 register regnode *scan; /* Current node. */
1074 regnode *next; /* Next node. */
1075 regnode *inner; /* Next node in internal branch. */
1076 register I32 nextchr; /* renamed nextchr - nextchar colides with
1077 function of same name */
1078 register I32 n; /* no or next */
1079 register I32 ln; /* len or last */
1080 register char *s; /* operand or save */
1081 register char *locinput = PL_reginput;
1082 register I32 c1, c2, paren; /* case fold search, parenth */
1083 int minmod = 0, sw = 0, logical = 0;
1088 /* Note that nextchr is a byte even in UTF */
1089 nextchr = UCHARAT(locinput);
1091 while (scan != NULL) {
1092 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1094 # define sayYES goto yes
1095 # define sayNO goto no
1096 # define saySAME(x) if (x) goto yes; else goto no
1097 # define REPORT_CODE_OFF 24
1099 # define sayYES return 1
1100 # define sayNO return 0
1101 # define saySAME(x) return x
1104 SV *prop = sv_newmortal();
1105 int docolor = *PL_colors[0];
1106 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1107 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1108 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1109 ? (5 + taill) - l : locinput - PL_bostr);
1110 int pref0_len = pref_len - (locinput - PL_reginput);
1112 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1113 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1114 ? (5 + taill) - pref_len : PL_regeol - locinput);
1117 regprop(prop, scan);
1118 PerlIO_printf(Perl_debug_log,
1119 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1120 locinput - PL_bostr,
1121 PL_colors[4], pref0_len,
1122 locinput - pref_len, PL_colors[5],
1123 PL_colors[2], pref_len - pref0_len,
1124 locinput - pref_len + pref0_len, PL_colors[3],
1125 (docolor ? "" : "> <"),
1126 PL_colors[0], l, locinput, PL_colors[1],
1127 15 - l - pref_len + 1,
1129 scan - PL_regprogram, PL_regindent*2, "",
1133 next = scan + NEXT_OFF(scan);
1139 if (locinput == PL_bostr
1140 ? PL_regprev == '\n'
1142 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1144 /* regtill = regbol; */
1149 if (locinput == PL_bostr
1150 ? PL_regprev == '\n'
1151 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1157 if (locinput == PL_regbol && PL_regprev == '\n')
1161 if (locinput == PL_regbol)
1171 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1176 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1178 if (PL_regeol - locinput > 1)
1182 if (PL_regeol != locinput)
1186 if (nextchr & 0x80) {
1187 locinput += utf8skip[nextchr];
1188 if (locinput > PL_regeol)
1190 nextchr = UCHARAT(locinput);
1193 if (!nextchr && locinput >= PL_regeol)
1195 nextchr = UCHARAT(++locinput);
1198 if (!nextchr && locinput >= PL_regeol)
1200 nextchr = UCHARAT(++locinput);
1203 if (nextchr & 0x80) {
1204 locinput += utf8skip[nextchr];
1205 if (locinput > PL_regeol)
1207 nextchr = UCHARAT(locinput);
1210 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1212 nextchr = UCHARAT(++locinput);
1215 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1217 nextchr = UCHARAT(++locinput);
1220 s = (char *) OPERAND(scan);
1222 /* Inline the first character, for speed. */
1223 if (UCHARAT(s) != nextchr)
1225 if (PL_regeol - locinput < ln)
1227 if (ln > 1 && memNE(s, locinput, ln))
1230 nextchr = UCHARAT(locinput);
1233 PL_reg_flags |= RF_tainted;
1236 s = (char *) OPERAND(scan);
1242 c1 = OP(scan) == EXACTF;
1246 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1247 toLOWER_utf8((U8*)l) :
1248 toLOWER_LC_utf8((U8*)l)))
1256 nextchr = UCHARAT(locinput);
1260 /* Inline the first character, for speed. */
1261 if (UCHARAT(s) != nextchr &&
1262 UCHARAT(s) != ((OP(scan) == EXACTF)
1263 ? fold : fold_locale)[nextchr])
1265 if (PL_regeol - locinput < ln)
1267 if (ln > 1 && (OP(scan) == EXACTF
1268 ? ibcmp(s, locinput, ln)
1269 : ibcmp_locale(s, locinput, ln)))
1272 nextchr = UCHARAT(locinput);
1275 s = (char *) OPERAND(scan);
1276 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1278 if (locinput >= PL_regeol)
1280 locinput += utf8skip[nextchr];
1281 nextchr = UCHARAT(locinput);
1284 s = (char *) OPERAND(scan);
1286 nextchr = UCHARAT(locinput);
1287 if (!REGINCLASS(s, nextchr))
1289 if (!nextchr && locinput >= PL_regeol)
1291 nextchr = UCHARAT(++locinput);
1294 PL_reg_flags |= RF_tainted;
1299 if (!(OP(scan) == ALNUM
1300 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1302 nextchr = UCHARAT(++locinput);
1305 PL_reg_flags |= RF_tainted;
1310 if (nextchr & 0x80) {
1311 if (!(OP(scan) == ALNUMUTF8
1312 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1313 : isALNUM_LC_utf8((U8*)locinput)))
1317 locinput += utf8skip[nextchr];
1318 nextchr = UCHARAT(locinput);
1321 if (!(OP(scan) == ALNUMUTF8
1322 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1324 nextchr = UCHARAT(++locinput);
1327 PL_reg_flags |= RF_tainted;
1330 if (!nextchr && locinput >= PL_regeol)
1332 if (OP(scan) == NALNUM
1333 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1335 nextchr = UCHARAT(++locinput);
1338 PL_reg_flags |= RF_tainted;
1341 if (!nextchr && locinput >= PL_regeol)
1343 if (nextchr & 0x80) {
1344 if (OP(scan) == NALNUMUTF8
1345 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1346 : isALNUM_LC_utf8((U8*)locinput))
1350 locinput += utf8skip[nextchr];
1351 nextchr = UCHARAT(locinput);
1354 if (OP(scan) == NALNUMUTF8
1355 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1357 nextchr = UCHARAT(++locinput);
1361 PL_reg_flags |= RF_tainted;
1365 /* was last char in word? */
1366 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1367 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1369 n = isALNUM(nextchr);
1372 ln = isALNUM_LC(ln);
1373 n = isALNUM_LC(nextchr);
1375 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1380 PL_reg_flags |= RF_tainted;
1384 /* was last char in word? */
1385 ln = (locinput != PL_regbol)
1386 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1387 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1388 ln = isALNUM_uni(ln);
1389 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1392 ln = isALNUM_LC_uni(ln);
1393 n = isALNUM_LC_utf8((U8*)locinput);
1395 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1399 PL_reg_flags |= RF_tainted;
1402 if (!nextchr && locinput >= PL_regeol)
1404 if (!(OP(scan) == SPACE
1405 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1407 nextchr = UCHARAT(++locinput);
1410 PL_reg_flags |= RF_tainted;
1413 if (!nextchr && locinput >= PL_regeol)
1415 if (nextchr & 0x80) {
1416 if (!(OP(scan) == SPACEUTF8
1417 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1418 : isSPACE_LC_utf8((U8*)locinput)))
1422 locinput += utf8skip[nextchr];
1423 nextchr = UCHARAT(locinput);
1426 if (!(OP(scan) == SPACEUTF8
1427 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1429 nextchr = UCHARAT(++locinput);
1432 PL_reg_flags |= RF_tainted;
1437 if (OP(scan) == SPACE
1438 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1440 nextchr = UCHARAT(++locinput);
1443 PL_reg_flags |= RF_tainted;
1448 if (nextchr & 0x80) {
1449 if (OP(scan) == NSPACEUTF8
1450 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1451 : isSPACE_LC_utf8((U8*)locinput))
1455 locinput += utf8skip[nextchr];
1456 nextchr = UCHARAT(locinput);
1459 if (OP(scan) == NSPACEUTF8
1460 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1462 nextchr = UCHARAT(++locinput);
1465 if (!isDIGIT(nextchr))
1467 nextchr = UCHARAT(++locinput);
1470 if (nextchr & 0x80) {
1471 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1473 locinput += utf8skip[nextchr];
1474 nextchr = UCHARAT(locinput);
1477 if (!isDIGIT(nextchr))
1479 nextchr = UCHARAT(++locinput);
1482 if (!nextchr && locinput >= PL_regeol)
1484 if (isDIGIT(nextchr))
1486 nextchr = UCHARAT(++locinput);
1489 if (!nextchr && locinput >= PL_regeol)
1491 if (nextchr & 0x80) {
1492 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1494 locinput += utf8skip[nextchr];
1495 nextchr = UCHARAT(locinput);
1498 if (isDIGIT(nextchr))
1500 nextchr = UCHARAT(++locinput);
1503 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1505 locinput += utf8skip[nextchr];
1506 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1507 locinput += UTF8SKIP(locinput);
1508 if (locinput > PL_regeol)
1510 nextchr = UCHARAT(locinput);
1513 PL_reg_flags |= RF_tainted;
1517 n = ARG(scan); /* which paren pair */
1518 s = PL_regstartp[n];
1519 if (*PL_reglastparen < n || !s)
1520 sayNO; /* Do not match unless seen CLOSEn. */
1521 if (s == PL_regendp[n])
1524 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1526 char *e = PL_regendp[n];
1528 * Note that we can't do the "other character" lookup trick as
1529 * in the 8-bit case (no pun intended) because in Unicode we
1530 * have to map both upper and title case to lower case.
1532 if (OP(scan) == REFF) {
1536 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1546 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1553 nextchr = UCHARAT(locinput);
1557 /* Inline the first character, for speed. */
1558 if (UCHARAT(s) != nextchr &&
1560 (UCHARAT(s) != ((OP(scan) == REFF
1561 ? fold : fold_locale)[nextchr]))))
1563 ln = PL_regendp[n] - s;
1564 if (locinput + ln > PL_regeol)
1566 if (ln > 1 && (OP(scan) == REF
1567 ? memNE(s, locinput, ln)
1569 ? ibcmp(s, locinput, ln)
1570 : ibcmp_locale(s, locinput, ln))))
1573 nextchr = UCHARAT(locinput);
1584 OP_4tree *oop = PL_op;
1585 COP *ocurcop = PL_curcop;
1586 SV **ocurpad = PL_curpad;
1590 PL_op = (OP_4tree*)PL_regdata->data[n];
1591 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1592 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1594 CALLRUNOPS(); /* Scalar context. */
1600 PL_curpad = ocurpad;
1601 PL_curcop = ocurcop;
1603 if (logical == 2) { /* Postponed subexpression. */
1605 my_magic mg = Null(my_magic);
1608 CHECKPOINT cp, lastcp;
1610 if(SvROK(ret) || SvRMAGICAL(ret)) {
1611 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1614 mg = mg_find(sv, 'r');
1617 re = (regexp *)mg->mg_obj;
1622 char *t = SvPV(ret, len);
1624 char *oprecomp = PL_regprecomp;
1625 I32 osize = PL_regsize;
1626 I32 onpar = PL_regnpar;
1629 re = CALLREGCOMP(t, t + len, &pm);
1631 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1632 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1633 PL_regprecomp = oprecomp;
1638 PerlIO_printf(Perl_debug_log,
1639 "Entering embedded `%s%.60s%s%s'\n",
1643 (strlen(re->precomp) > 60 ? "..." : ""))
1646 state.prev = PL_reg_call_cc;
1647 state.cc = PL_regcc;
1648 state.re = PL_reg_re;
1654 cp = regcppush(0); /* Save *all* the positions. */
1657 state.ss = PL_savestack_ix;
1658 *PL_reglastparen = 0;
1659 PL_reg_call_cc = &state;
1660 PL_reginput = locinput;
1661 if (regmatch(re->program + 1)) {
1667 PerlIO_printf(Perl_debug_log,
1669 REPORT_CODE_OFF+PL_regindent*2, "")
1674 PL_reg_call_cc = state.prev;
1675 PL_regcc = state.cc;
1676 PL_reg_re = state.re;
1677 cache_re(PL_reg_re);
1684 sv_setsv(save_scalar(PL_replgv), ret);
1688 n = ARG(scan); /* which paren pair */
1689 PL_reg_start_tmp[n] = locinput;
1694 n = ARG(scan); /* which paren pair */
1695 PL_regstartp[n] = PL_reg_start_tmp[n];
1696 PL_regendp[n] = locinput;
1697 if (n > *PL_reglastparen)
1698 *PL_reglastparen = n;
1701 n = ARG(scan); /* which paren pair */
1702 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1706 next = NEXTOPER(NEXTOPER(scan));
1708 next = scan + ARG(scan);
1709 if (OP(next) == IFTHEN) /* Fake one. */
1710 next = NEXTOPER(NEXTOPER(next));
1714 logical = scan->flags;
1718 CHECKPOINT cp = PL_savestack_ix;
1720 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1722 cc.oldcc = PL_regcc;
1724 cc.parenfloor = *PL_reglastparen;
1726 cc.min = ARG1(scan);
1727 cc.max = ARG2(scan);
1728 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1732 PL_reginput = locinput;
1733 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1735 PL_regcc = cc.oldcc;
1741 * This is really hard to understand, because after we match
1742 * what we're trying to match, we must make sure the rest of
1743 * the RE is going to match for sure, and to do that we have
1744 * to go back UP the parse tree by recursing ever deeper. And
1745 * if it fails, we have to reset our parent's current state
1746 * that we can try again after backing off.
1749 CHECKPOINT cp, lastcp;
1750 CURCUR* cc = PL_regcc;
1751 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1753 n = cc->cur + 1; /* how many we know we matched */
1754 PL_reginput = locinput;
1757 PerlIO_printf(Perl_debug_log,
1758 "%*s %ld out of %ld..%ld cc=%lx\n",
1759 REPORT_CODE_OFF+PL_regindent*2, "",
1760 (long)n, (long)cc->min,
1761 (long)cc->max, (long)cc)
1764 /* If degenerate scan matches "", assume scan done. */
1766 if (locinput == cc->lastloc && n >= cc->min) {
1767 PL_regcc = cc->oldcc;
1770 PerlIO_printf(Perl_debug_log,
1771 "%*s empty match detected, try continuation...\n",
1772 REPORT_CODE_OFF+PL_regindent*2, "")
1774 if (regmatch(cc->next))
1777 PerlIO_printf(Perl_debug_log,
1779 REPORT_CODE_OFF+PL_regindent*2, "")
1786 /* First just match a string of min scans. */
1790 cc->lastloc = locinput;
1791 if (regmatch(cc->scan))
1794 cc->lastloc = lastloc;
1796 PerlIO_printf(Perl_debug_log,
1798 REPORT_CODE_OFF+PL_regindent*2, "")
1803 /* Prefer next over scan for minimal matching. */
1806 PL_regcc = cc->oldcc;
1808 cp = regcppush(cc->parenfloor);
1810 if (regmatch(cc->next)) {
1812 sayYES; /* All done. */
1819 if (n >= cc->max) { /* Maximum greed exceeded? */
1820 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1821 && !(PL_reg_flags & RF_warned)) {
1822 PL_reg_flags |= RF_warned;
1823 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1824 "Complex regular subexpression recursion",
1831 PerlIO_printf(Perl_debug_log,
1832 "%*s trying longer...\n",
1833 REPORT_CODE_OFF+PL_regindent*2, "")
1835 /* Try scanning more and see if it helps. */
1836 PL_reginput = locinput;
1838 cc->lastloc = locinput;
1839 cp = regcppush(cc->parenfloor);
1841 if (regmatch(cc->scan)) {
1846 PerlIO_printf(Perl_debug_log,
1848 REPORT_CODE_OFF+PL_regindent*2, "")
1853 cc->lastloc = lastloc;
1857 /* Prefer scan over next for maximal matching. */
1859 if (n < cc->max) { /* More greed allowed? */
1860 cp = regcppush(cc->parenfloor);
1862 cc->lastloc = locinput;
1864 if (regmatch(cc->scan)) {
1869 regcppop(); /* Restore some previous $<digit>s? */
1870 PL_reginput = locinput;
1872 PerlIO_printf(Perl_debug_log,
1873 "%*s failed, try continuation...\n",
1874 REPORT_CODE_OFF+PL_regindent*2, "")
1877 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1878 && !(PL_reg_flags & RF_warned)) {
1879 PL_reg_flags |= RF_warned;
1880 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1881 "Complex regular subexpression recursion",
1885 /* Failed deeper matches of scan, so see if this one works. */
1886 PL_regcc = cc->oldcc;
1888 if (regmatch(cc->next))
1891 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1892 REPORT_CODE_OFF+PL_regindent*2, "")
1897 cc->lastloc = lastloc;
1902 next = scan + ARG(scan);
1905 inner = NEXTOPER(NEXTOPER(scan));
1908 inner = NEXTOPER(scan);
1913 if (OP(next) != c1) /* No choice. */
1914 next = inner; /* Avoid recursion. */
1916 int lastparen = *PL_reglastparen;
1920 PL_reginput = locinput;
1921 if (regmatch(inner))
1924 for (n = *PL_reglastparen; n > lastparen; n--)
1926 *PL_reglastparen = n;
1929 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1933 inner = NEXTOPER(scan);
1934 if (c1 == BRANCHJ) {
1935 inner = NEXTOPER(inner);
1937 } while (scan != NULL && OP(scan) == c1);
1951 /* We suppose that the next guy does not need
1952 backtracking: in particular, it is of constant length,
1953 and has no parenths to influence future backrefs. */
1954 ln = ARG1(scan); /* min to match */
1955 n = ARG2(scan); /* max to match */
1956 paren = scan->flags;
1958 if (paren > PL_regsize)
1960 if (paren > *PL_reglastparen)
1961 *PL_reglastparen = paren;
1963 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1965 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1966 PL_reginput = locinput;
1969 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1971 if (ln && l == 0 && n >= ln
1972 /* In fact, this is tricky. If paren, then the
1973 fact that we did/didnot match may influence
1974 future execution. */
1975 && !(paren && ln == 0))
1977 locinput = PL_reginput;
1978 if (regkind[(U8)OP(next)] == EXACT) {
1979 c1 = UCHARAT(OPERAND(next) + 1);
1980 if (OP(next) == EXACTF)
1982 else if (OP(next) == EXACTFL)
1983 c2 = fold_locale[c1];
1990 /* This may be improved if l == 0. */
1991 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1992 /* If it could work, try it. */
1994 UCHARAT(PL_reginput) == c1 ||
1995 UCHARAT(PL_reginput) == c2)
1999 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2000 PL_regendp[paren] = PL_reginput;
2003 PL_regendp[paren] = NULL;
2009 /* Couldn't or didn't -- move forward. */
2010 PL_reginput = locinput;
2011 if (regrepeat_hard(scan, 1, &l)) {
2013 locinput = PL_reginput;
2020 n = regrepeat_hard(scan, n, &l);
2021 if (n != 0 && l == 0
2022 /* In fact, this is tricky. If paren, then the
2023 fact that we did/didnot match may influence
2024 future execution. */
2025 && !(paren && ln == 0))
2027 locinput = PL_reginput;
2029 PerlIO_printf(Perl_debug_log,
2030 "%*s matched %ld times, len=%ld...\n",
2031 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2034 if (regkind[(U8)OP(next)] == EXACT) {
2035 c1 = UCHARAT(OPERAND(next) + 1);
2036 if (OP(next) == EXACTF)
2038 else if (OP(next) == EXACTFL)
2039 c2 = fold_locale[c1];
2048 /* If it could work, try it. */
2050 UCHARAT(PL_reginput) == c1 ||
2051 UCHARAT(PL_reginput) == c2)
2054 PerlIO_printf(Perl_debug_log,
2055 "%*s trying tail with n=%ld...\n",
2056 REPORT_CODE_OFF+PL_regindent*2, "", n)
2060 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2061 PL_regendp[paren] = PL_reginput;
2064 PL_regendp[paren] = NULL;
2070 /* Couldn't or didn't -- back up. */
2072 locinput = HOPc(locinput, -l);
2073 PL_reginput = locinput;
2080 paren = scan->flags; /* Which paren to set */
2081 if (paren > PL_regsize)
2083 if (paren > *PL_reglastparen)
2084 *PL_reglastparen = paren;
2085 ln = ARG1(scan); /* min to match */
2086 n = ARG2(scan); /* max to match */
2087 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2091 ln = ARG1(scan); /* min to match */
2092 n = ARG2(scan); /* max to match */
2093 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2098 scan = NEXTOPER(scan);
2104 scan = NEXTOPER(scan);
2108 * Lookahead to avoid useless match attempts
2109 * when we know what character comes next.
2111 if (regkind[(U8)OP(next)] == EXACT) {
2112 c1 = UCHARAT(OPERAND(next) + 1);
2113 if (OP(next) == EXACTF)
2115 else if (OP(next) == EXACTFL)
2116 c2 = fold_locale[c1];
2122 PL_reginput = locinput;
2126 if (ln && regrepeat(scan, ln) < ln)
2128 locinput = PL_reginput;
2130 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2131 /* If it could work, try it. */
2133 UCHARAT(PL_reginput) == c1 ||
2134 UCHARAT(PL_reginput) == c2)
2138 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2139 PL_regendp[paren] = PL_reginput;
2142 PL_regendp[paren] = NULL;
2148 /* Couldn't or didn't -- move forward. */
2149 PL_reginput = locinput;
2150 if (regrepeat(scan, 1)) {
2152 locinput = PL_reginput;
2160 n = regrepeat(scan, n);
2161 locinput = PL_reginput;
2162 if (ln < n && regkind[(U8)OP(next)] == EOL &&
2163 (!PL_multiline || OP(next) == SEOL))
2164 ln = n; /* why back off? */
2168 /* If it could work, try it. */
2170 UCHARAT(PL_reginput) == c1 ||
2171 UCHARAT(PL_reginput) == c2)
2175 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2176 PL_regendp[paren] = PL_reginput;
2179 PL_regendp[paren] = NULL;
2185 /* Couldn't or didn't -- back up. */
2187 PL_reginput = locinput = HOPc(locinput, -1);
2192 /* If it could work, try it. */
2194 UCHARAT(PL_reginput) == c1 ||
2195 UCHARAT(PL_reginput) == c2)
2201 /* Couldn't or didn't -- back up. */
2203 PL_reginput = locinput = HOPc(locinput, -1);
2210 if (PL_reg_call_cc) {
2211 re_cc_state *cur_call_cc = PL_reg_call_cc;
2212 CURCUR *cctmp = PL_regcc;
2213 regexp *re = PL_reg_re;
2214 CHECKPOINT cp, lastcp;
2216 cp = regcppush(0); /* Save *all* the positions. */
2218 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2220 PL_reginput = locinput; /* Make position available to
2222 cache_re(PL_reg_call_cc->re);
2223 PL_regcc = PL_reg_call_cc->cc;
2224 PL_reg_call_cc = PL_reg_call_cc->prev;
2225 if (regmatch(cur_call_cc->node)) {
2226 PL_reg_call_cc = cur_call_cc;
2232 PL_reg_call_cc = cur_call_cc;
2238 PerlIO_printf(Perl_debug_log,
2239 "%*s continuation failed...\n",
2240 REPORT_CODE_OFF+PL_regindent*2, "")
2244 if (locinput < PL_regtill)
2245 sayNO; /* Cannot match: too short. */
2248 PL_reginput = locinput; /* put where regtry can find it */
2249 sayYES; /* Success! */
2252 PL_reginput = locinput;
2257 s = HOPMAYBEc(locinput, -scan->flags);
2263 PL_reginput = locinput;
2268 s = HOPMAYBEc(locinput, -scan->flags);
2274 PL_reginput = locinput;
2277 inner = NEXTOPER(NEXTOPER(scan));
2278 if (regmatch(inner) != n) {
2293 if (OP(scan) == SUSPEND) {
2294 locinput = PL_reginput;
2295 nextchr = UCHARAT(locinput);
2300 next = scan + ARG(scan);
2305 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2306 (unsigned long)scan, OP(scan));
2307 FAIL("regexp memory corruption");
2313 * We get here only if there's trouble -- normally "case END" is
2314 * the terminating point.
2316 FAIL("corrupted regexp pointers");
2334 - regrepeat - repeatedly match something simple, report how many
2337 * [This routine now assumes that it will only match on things of length 1.
2338 * That was true before, but now we assume scan - reginput is the count,
2339 * rather than incrementing count on every character. [Er, except utf8.]]
2342 regrepeat(regnode *p, I32 max)
2345 register char *scan;
2346 register char *opnd;
2348 register char *loceol = PL_regeol;
2349 register I32 hardcount = 0;
2352 if (max != REG_INFTY && max < loceol - scan)
2353 loceol = scan + max;
2354 opnd = (char *) OPERAND(p);
2357 while (scan < loceol && *scan != '\n')
2365 while (scan < loceol && *scan != '\n') {
2366 scan += UTF8SKIP(scan);
2372 while (scan < loceol) {
2373 scan += UTF8SKIP(scan);
2377 case EXACT: /* length of string is 1 */
2378 c = UCHARAT(++opnd);
2379 while (scan < loceol && UCHARAT(scan) == c)
2382 case EXACTF: /* length of string is 1 */
2383 c = UCHARAT(++opnd);
2384 while (scan < loceol &&
2385 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
2388 case EXACTFL: /* length of string is 1 */
2389 PL_reg_flags |= RF_tainted;
2390 c = UCHARAT(++opnd);
2391 while (scan < loceol &&
2392 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
2397 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2398 scan += UTF8SKIP(scan);
2403 while (scan < loceol && REGINCLASS(opnd, *scan))
2407 while (scan < loceol && isALNUM(*scan))
2412 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2413 scan += UTF8SKIP(scan);
2418 PL_reg_flags |= RF_tainted;
2419 while (scan < loceol && isALNUM_LC(*scan))
2423 PL_reg_flags |= RF_tainted;
2425 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2426 scan += UTF8SKIP(scan);
2432 while (scan < loceol && !isALNUM(*scan))
2437 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2438 scan += UTF8SKIP(scan);
2443 PL_reg_flags |= RF_tainted;
2444 while (scan < loceol && !isALNUM_LC(*scan))
2448 PL_reg_flags |= RF_tainted;
2450 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2451 scan += UTF8SKIP(scan);
2456 while (scan < loceol && isSPACE(*scan))
2461 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2462 scan += UTF8SKIP(scan);
2467 PL_reg_flags |= RF_tainted;
2468 while (scan < loceol && isSPACE_LC(*scan))
2472 PL_reg_flags |= RF_tainted;
2474 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2475 scan += UTF8SKIP(scan);
2480 while (scan < loceol && !isSPACE(*scan))
2485 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2486 scan += UTF8SKIP(scan);
2491 PL_reg_flags |= RF_tainted;
2492 while (scan < loceol && !isSPACE_LC(*scan))
2496 PL_reg_flags |= RF_tainted;
2498 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2499 scan += UTF8SKIP(scan);
2504 while (scan < loceol && isDIGIT(*scan))
2509 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2510 scan += UTF8SKIP(scan);
2516 while (scan < loceol && !isDIGIT(*scan))
2521 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2522 scan += UTF8SKIP(scan);
2526 default: /* Called on something of 0 width. */
2527 break; /* So match right here or not at all. */
2533 c = scan - PL_reginput;
2538 SV *prop = sv_newmortal();
2541 PerlIO_printf(Perl_debug_log,
2542 "%*s %s can match %ld times out of %ld...\n",
2543 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2550 - regrepeat_hard - repeatedly match something, report total lenth and length
2552 * The repeater is supposed to have constant length.
2556 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2559 register char *scan;
2560 register char *start;
2561 register char *loceol = PL_regeol;
2563 I32 count = 0, res = 1;
2568 start = PL_reginput;
2570 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2573 while (start < PL_reginput) {
2575 start += UTF8SKIP(start);
2586 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2588 *lp = l = PL_reginput - start;
2589 if (max != REG_INFTY && l*max < loceol - scan)
2590 loceol = scan + l*max;
2603 - regclass - determine if a character falls into a character class
2607 reginclass(register char *p, register I32 c)
2614 if (ANYOF_TEST(p, c))
2616 else if (flags & ANYOF_FOLD) {
2618 if (flags & ANYOF_LOCALE) {
2619 PL_reg_flags |= RF_tainted;
2620 cf = fold_locale[c];
2624 if (ANYOF_TEST(p, cf))
2628 if (!match && (flags & ANYOF_ISA)) {
2629 PL_reg_flags |= RF_tainted;
2631 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2632 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2633 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2634 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2640 return (flags & ANYOF_INVERT) ? !match : match;
2644 reginclassutf8(regnode *f, U8 *p)
2647 char flags = ARG1(f);
2649 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2651 if (swash_fetch(sv, p))
2653 else if (flags & ANYOF_FOLD) {
2656 if (flags & ANYOF_LOCALE) {
2657 PL_reg_flags |= RF_tainted;
2658 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2661 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2662 if (swash_fetch(sv, tmpbuf))
2666 if (!match && (flags & ANYOF_ISA)) {
2667 PL_reg_flags |= RF_tainted;
2669 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2670 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2671 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2672 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2678 return (flags & ANYOF_INVERT) ? !match : match;
2682 reghop(U8 *s, I32 off)
2686 while (off-- && s < (U8*)PL_regeol)
2691 if (s > (U8*)PL_bostr) {
2694 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2696 } /* XXX could check well-formedness here */
2704 reghopmaybe(U8* s, I32 off)
2708 while (off-- && s < (U8*)PL_regeol)
2715 if (s > (U8*)PL_bostr) {
2718 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2720 } /* XXX could check well-formedness here */