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;
212 I32 tmp = PL_savestack_ix;
214 PL_savestack_ix = ss;
216 PL_savestack_ix = tmp;
220 typedef struct re_cc_state
224 struct re_cc_state *prev;
229 #define regcpblow(cp) LEAVE_SCOPE(cp)
232 * pregexec and friends
236 - pregexec - match a regexp against a string
239 pregexec(register regexp *prog, char *stringarg, register char *strend,
240 char *strbeg, I32 minend, SV *screamer, U32 nosave)
241 /* strend: pointer to null at end of string */
242 /* strbeg: real beginning of string */
243 /* minend: end of match must be >=minend after stringarg. */
244 /* nosave: For optimizations. */
247 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
248 nosave ? 0 : REXEC_COPY_STR);
252 cache_re(regexp *prog)
255 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
257 PL_regprogram = prog->program;
259 PL_regnpar = prog->nparens;
260 PL_regdata = prog->data;
265 - regexec_flags - match a regexp against a string
268 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
269 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
270 /* strend: pointer to null at end of string */
271 /* strbeg: real beginning of string */
272 /* minend: end of match must be >=minend after stringarg. */
273 /* data: May be used for some additional optimizations. */
274 /* nosave: For optimizations. */
279 register char *startpos = stringarg;
281 I32 minlen; /* must match at least this many chars */
282 I32 dontbother = 0; /* how many characters not to try at end */
284 I32 start_shift = 0; /* Offset of the start to find
285 constant substr. */ /* CC */
286 I32 end_shift = 0; /* Same for the end. */ /* CC */
287 I32 scream_pos = -1; /* Internal iterator of scream. */
289 SV* oreplsv = GvSV(PL_replgv);
297 PL_regnarrate = PL_debug & 512;
301 if (prog == NULL || startpos == NULL) {
302 croak("NULL regexp parameter");
306 minlen = prog->minlen;
307 if (strend - startpos < minlen) goto phooey;
309 if (startpos == strbeg) /* is ^ valid at stringarg? */
312 PL_regprev = (U32)stringarg[-1];
313 if (!PL_multiline && PL_regprev == '\n')
314 PL_regprev = '\0'; /* force ^ to NOT match */
317 /* Check validity of program. */
318 if (UCHARAT(prog->program) != MAGIC) {
319 FAIL("corrupted regexp program");
325 if (prog->reganch & ROPT_UTF8)
326 PL_reg_flags |= RF_utf8;
328 /* Mark beginning of line for ^ and lookbehind. */
329 PL_regbol = startpos;
332 /* Mark end of line for $ (and such) */
335 /* see how far we have to get to not match where we matched before */
336 PL_regtill = startpos+minend;
338 /* We start without call_cc context. */
341 /* If there is a "must appear" string, look for it. */
343 if (!(flags & REXEC_CHECKED)
344 && prog->check_substr != Nullsv &&
345 !(prog->reganch & ROPT_ANCH_GPOS) &&
346 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
347 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
350 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
351 /* Should be nonnegative! */
352 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
354 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
355 s = screaminstr(screamer, prog->check_substr,
356 start_shift + (stringarg - strbeg),
357 end_shift, &scream_pos, 0);
363 s = fbm_instr((unsigned char*)s + start_shift,
364 (unsigned char*)strend - end_shift,
365 prog->check_substr, 0);
367 ++BmUSEFUL(prog->check_substr); /* hooray */
368 goto phooey; /* not present */
370 else if (s - stringarg > prog->check_offset_max &&
372 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
373 : (t = s - prog->check_offset_max) != 0
377 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
380 else if (!(prog->reganch & ROPT_NAUGHTY)
381 && --BmUSEFUL(prog->check_substr) < 0
382 && prog->check_substr == prog->float_substr) { /* boo */
383 SvREFCNT_dec(prog->check_substr);
384 prog->check_substr = Nullsv; /* disable */
385 prog->float_substr = Nullsv; /* clear */
393 PerlIO_printf(Perl_debug_log,
394 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
395 PL_colors[4],PL_colors[5],PL_colors[0],
398 (strlen(prog->precomp) > 60 ? "..." : ""),
400 (strend - startpos > 60 ? 60 : strend - startpos),
401 startpos, PL_colors[1],
402 (strend - startpos > 60 ? "..." : ""))
405 /* Simplest case: anchored match need be tried only once. */
406 /* [unless only anchor is BOL and multiline is set] */
407 if (prog->reganch & ROPT_ANCH) {
408 if (regtry(prog, startpos))
410 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
411 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
412 || (prog->reganch & ROPT_ANCH_MBOL)))
415 dontbother = minlen - 1;
416 strend = HOPc(strend, -dontbother);
417 /* for multiline we only have to try after newlines */
421 if (*s++ == '\n') { /* don't need utf8skip here */
422 if (s < strend && regtry(prog, s))
430 /* Messy cases: unanchored match. */
431 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
432 /* we have /x+whatever/ */
433 /* it must be a one character string */
434 char ch = SvPVX(prog->anchored_substr)[0];
438 if (regtry(prog, s)) goto got_it;
440 while (s < strend && *s == ch)
449 if (regtry(prog, s)) goto got_it;
451 while (s < strend && *s == ch)
459 else if (prog->anchored_substr != Nullsv
460 || (prog->float_substr != Nullsv
461 && prog->float_max_offset < strend - s)) {
462 SV *must = prog->anchored_substr
463 ? prog->anchored_substr : prog->float_substr;
465 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
467 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
468 I32 delta = back_max - back_min;
469 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
470 char *last1; /* Last position checked before */
475 last1 = s - 1; /* bogus */
477 /* XXXX check_substr already used to find `s', can optimize if
478 check_substr==must. */
480 dontbother = end_shift;
481 strend = HOPc(strend, -dontbother);
482 while ( (s <= last) &&
484 ? (s = screaminstr(screamer, must, HOPc(s, back_min) - strbeg,
485 end_shift, &scream_pos, 0))
486 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
487 (unsigned char*)strend, must, 0))) ) {
488 if (HOPc(s, -back_max) > last1) {
489 last1 = HOPc(s, -back_min);
490 s = HOPc(s, -back_max);
493 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
495 last1 = HOPc(s, -back_min);
515 else if (c = prog->regstclass) {
516 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
520 dontbother = minlen - 1;
521 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
523 /* We know what class it must start with. */
526 cc = (char *) OPERAND(c);
528 if (REGINCLASSUTF8(c, (U8*)s)) {
529 if (tmp && regtry(prog, s))
540 cc = (char *) OPERAND(c);
542 if (REGINCLASS(cc, *s)) {
543 if (tmp && regtry(prog, s))
554 PL_reg_flags |= RF_tainted;
561 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
562 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
564 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
571 if ((minlen || tmp) && regtry(prog,s))
575 PL_reg_flags |= RF_tainted;
580 strend = reghop_c(strend, -1);
582 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
583 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
585 if (tmp == !(OP(c) == BOUND ?
586 swash_fetch(PL_utf8_alnum, (U8*)s) :
587 isALNUM_LC_utf8((U8*)s)))
595 if ((minlen || tmp) && regtry(prog,s))
599 PL_reg_flags |= RF_tainted;
606 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
607 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
609 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
611 else if (regtry(prog, s))
615 if ((minlen || !tmp) && regtry(prog,s))
619 PL_reg_flags |= RF_tainted;
624 strend = reghop_c(strend, -1);
626 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
627 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
629 if (tmp == !(OP(c) == NBOUND ?
630 swash_fetch(PL_utf8_alnum, (U8*)s) :
631 isALNUM_LC_utf8((U8*)s)))
633 else if (regtry(prog, s))
637 if ((minlen || !tmp) && regtry(prog,s))
643 if (tmp && regtry(prog, s))
655 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
656 if (tmp && regtry(prog, s))
667 PL_reg_flags |= RF_tainted;
669 if (isALNUM_LC(*s)) {
670 if (tmp && regtry(prog, s))
681 PL_reg_flags |= RF_tainted;
683 if (isALNUM_LC_utf8((U8*)s)) {
684 if (tmp && regtry(prog, s))
697 if (tmp && regtry(prog, s))
709 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
710 if (tmp && regtry(prog, s))
721 PL_reg_flags |= RF_tainted;
723 if (!isALNUM_LC(*s)) {
724 if (tmp && regtry(prog, s))
735 PL_reg_flags |= RF_tainted;
737 if (!isALNUM_LC_utf8((U8*)s)) {
738 if (tmp && regtry(prog, s))
751 if (tmp && regtry(prog, s))
763 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
764 if (tmp && regtry(prog, s))
775 PL_reg_flags |= RF_tainted;
777 if (isSPACE_LC(*s)) {
778 if (tmp && regtry(prog, s))
789 PL_reg_flags |= RF_tainted;
791 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
792 if (tmp && regtry(prog, s))
805 if (tmp && regtry(prog, s))
817 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
818 if (tmp && regtry(prog, s))
829 PL_reg_flags |= RF_tainted;
831 if (!isSPACE_LC(*s)) {
832 if (tmp && regtry(prog, s))
843 PL_reg_flags |= RF_tainted;
845 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
846 if (tmp && regtry(prog, s))
859 if (tmp && regtry(prog, s))
871 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
872 if (tmp && regtry(prog, s))
885 if (tmp && regtry(prog, s))
897 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
898 if (tmp && regtry(prog, s))
912 if (prog->float_substr != Nullsv) { /* Trim the end. */
914 I32 oldpos = scream_pos;
917 last = screaminstr(screamer, prog->float_substr, s - strbeg,
918 end_shift, &scream_pos, 1); /* last one */
920 last = scream_olds; /* Only one occurence. */
925 char *little = SvPV(prog->float_substr, len);
927 last = rninstr(s, strend, little, little + len);
929 last = strend; /* matching `$' */
931 if (last == NULL) goto phooey; /* Should not happen! */
932 dontbother = strend - last + prog->float_min_offset;
934 if (minlen && (dontbother < minlen))
935 dontbother = minlen - 1;
936 strend -= dontbother; /* this one's always in bytes! */
937 /* We don't know much -- general case. */
951 } while (s++ < strend);
959 prog->subbeg = strbeg;
960 prog->subend = PL_regeol; /* strend may have been modified */
961 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
963 /* make sure $`, $&, $', and $digit will work later */
964 if (strbeg != prog->subbase) { /* second+ //g match. */
965 if (!(flags & REXEC_COPY_STR)) {
967 Safefree(prog->subbase);
968 prog->subbase = Nullch;
972 I32 i = PL_regeol - startpos + (stringarg - strbeg);
973 s = savepvn(strbeg, i);
974 Safefree(prog->subbase);
976 prog->subbeg = prog->subbase;
977 prog->subend = prog->subbase + i;
978 s = prog->subbase + (stringarg - strbeg);
979 for (i = 0; i <= prog->nparens; i++) {
981 prog->startp[i] = s + (prog->startp[i] - startpos);
982 prog->endp[i] = s + (prog->endp[i] - startpos);
987 /* Preserve the current value of $^R */
988 if (oreplsv != GvSV(PL_replgv)) {
989 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
990 restored, the value remains
1000 - regtry - try match at specific point
1002 STATIC I32 /* 0 failure, 1 success */
1003 regtry(regexp *prog, char *startpos)
1011 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1012 PL_reg_eval_set = RS_init;
1014 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1015 PL_stack_sp - PL_stack_base);
1017 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1018 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1019 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1021 /* Apparently this is not needed, judging by wantarray. */
1022 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1023 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1025 PL_reginput = startpos;
1026 PL_regstartp = prog->startp;
1027 PL_regendp = prog->endp;
1028 PL_reglastparen = &prog->lastparen;
1029 prog->lastparen = 0;
1031 if (PL_reg_start_tmpl <= prog->nparens) {
1032 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1033 if(PL_reg_start_tmp)
1034 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1036 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1041 if (prog->nparens) {
1042 for (i = prog->nparens; i >= 0; i--) {
1048 if (regmatch(prog->program + 1)) {
1049 prog->startp[0] = startpos;
1050 prog->endp[0] = PL_reginput;
1058 - regmatch - main matching routine
1060 * Conceptually the strategy is simple: check to see whether the current
1061 * node matches, call self recursively to see whether the rest matches,
1062 * and then act accordingly. In practice we make some effort to avoid
1063 * recursion, in particular by going through "ordinary" nodes (that don't
1064 * need to know whether the rest of the match failed) by a loop instead of
1067 /* [lwall] I've hoisted the register declarations to the outer block in order to
1068 * maybe save a little bit of pushing and popping on the stack. It also takes
1069 * advantage of machines that use a register save mask on subroutine entry.
1071 STATIC I32 /* 0 failure, 1 success */
1072 regmatch(regnode *prog)
1075 register regnode *scan; /* Current node. */
1076 regnode *next; /* Next node. */
1077 regnode *inner; /* Next node in internal branch. */
1078 register I32 nextchr; /* renamed nextchr - nextchar colides with
1079 function of same name */
1080 register I32 n; /* no or next */
1081 register I32 ln; /* len or last */
1082 register char *s; /* operand or save */
1083 register char *locinput = PL_reginput;
1084 register I32 c1, c2, paren; /* case fold search, parenth */
1085 int minmod = 0, sw = 0, logical = 0;
1090 /* Note that nextchr is a byte even in UTF */
1091 nextchr = UCHARAT(locinput);
1093 while (scan != NULL) {
1094 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1096 # define sayYES goto yes
1097 # define sayNO goto no
1098 # define saySAME(x) if (x) goto yes; else goto no
1099 # define REPORT_CODE_OFF 24
1101 # define sayYES return 1
1102 # define sayNO return 0
1103 # define saySAME(x) return x
1106 SV *prop = sv_newmortal();
1107 int docolor = *PL_colors[0];
1108 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1109 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1110 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1111 ? (5 + taill) - l : locinput - PL_bostr);
1112 int pref0_len = pref_len - (locinput - PL_reginput);
1114 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1115 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1116 ? (5 + taill) - pref_len : PL_regeol - locinput);
1119 regprop(prop, scan);
1120 PerlIO_printf(Perl_debug_log,
1121 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1122 locinput - PL_bostr,
1123 PL_colors[4], pref0_len,
1124 locinput - pref_len, PL_colors[5],
1125 PL_colors[2], pref_len - pref0_len,
1126 locinput - pref_len + pref0_len, PL_colors[3],
1127 (docolor ? "" : "> <"),
1128 PL_colors[0], l, locinput, PL_colors[1],
1129 15 - l - pref_len + 1,
1131 scan - PL_regprogram, PL_regindent*2, "",
1135 next = scan + NEXT_OFF(scan);
1141 if (locinput == PL_bostr
1142 ? PL_regprev == '\n'
1144 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1146 /* regtill = regbol; */
1151 if (locinput == PL_bostr
1152 ? PL_regprev == '\n'
1153 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1159 if (locinput == PL_regbol && PL_regprev == '\n')
1163 if (locinput == PL_regbol)
1173 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1178 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1180 if (PL_regeol - locinput > 1)
1184 if (PL_regeol != locinput)
1188 if (nextchr & 0x80) {
1189 locinput += utf8skip[nextchr];
1190 if (locinput > PL_regeol)
1192 nextchr = UCHARAT(locinput);
1195 if (!nextchr && locinput >= PL_regeol)
1197 nextchr = UCHARAT(++locinput);
1200 if (!nextchr && locinput >= PL_regeol)
1202 nextchr = UCHARAT(++locinput);
1205 if (nextchr & 0x80) {
1206 locinput += utf8skip[nextchr];
1207 if (locinput > PL_regeol)
1209 nextchr = UCHARAT(locinput);
1212 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1214 nextchr = UCHARAT(++locinput);
1217 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1219 nextchr = UCHARAT(++locinput);
1222 s = (char *) OPERAND(scan);
1224 /* Inline the first character, for speed. */
1225 if (UCHARAT(s) != nextchr)
1227 if (PL_regeol - locinput < ln)
1229 if (ln > 1 && memNE(s, locinput, ln))
1232 nextchr = UCHARAT(locinput);
1235 PL_reg_flags |= RF_tainted;
1238 s = (char *) OPERAND(scan);
1244 c1 = OP(scan) == EXACTF;
1248 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1249 toLOWER_utf8((U8*)l) :
1250 toLOWER_LC_utf8((U8*)l)))
1258 nextchr = UCHARAT(locinput);
1262 /* Inline the first character, for speed. */
1263 if (UCHARAT(s) != nextchr &&
1264 UCHARAT(s) != ((OP(scan) == EXACTF)
1265 ? fold : fold_locale)[nextchr])
1267 if (PL_regeol - locinput < ln)
1269 if (ln > 1 && (OP(scan) == EXACTF
1270 ? ibcmp(s, locinput, ln)
1271 : ibcmp_locale(s, locinput, ln)))
1274 nextchr = UCHARAT(locinput);
1277 s = (char *) OPERAND(scan);
1278 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1280 if (locinput >= PL_regeol)
1282 locinput += utf8skip[nextchr];
1283 nextchr = UCHARAT(locinput);
1286 s = (char *) OPERAND(scan);
1288 nextchr = UCHARAT(locinput);
1289 if (!REGINCLASS(s, nextchr))
1291 if (!nextchr && locinput >= PL_regeol)
1293 nextchr = UCHARAT(++locinput);
1296 PL_reg_flags |= RF_tainted;
1301 if (!(OP(scan) == ALNUM
1302 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1304 nextchr = UCHARAT(++locinput);
1307 PL_reg_flags |= RF_tainted;
1312 if (nextchr & 0x80) {
1313 if (!(OP(scan) == ALNUMUTF8
1314 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1315 : isALNUM_LC_utf8((U8*)locinput)))
1319 locinput += utf8skip[nextchr];
1320 nextchr = UCHARAT(locinput);
1323 if (!(OP(scan) == ALNUMUTF8
1324 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1326 nextchr = UCHARAT(++locinput);
1329 PL_reg_flags |= RF_tainted;
1332 if (!nextchr && locinput >= PL_regeol)
1334 if (OP(scan) == NALNUM
1335 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1337 nextchr = UCHARAT(++locinput);
1340 PL_reg_flags |= RF_tainted;
1343 if (!nextchr && locinput >= PL_regeol)
1345 if (nextchr & 0x80) {
1346 if (OP(scan) == NALNUMUTF8
1347 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1348 : isALNUM_LC_utf8((U8*)locinput))
1352 locinput += utf8skip[nextchr];
1353 nextchr = UCHARAT(locinput);
1356 if (OP(scan) == NALNUMUTF8
1357 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1359 nextchr = UCHARAT(++locinput);
1363 PL_reg_flags |= RF_tainted;
1367 /* was last char in word? */
1368 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1369 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1371 n = isALNUM(nextchr);
1374 ln = isALNUM_LC(ln);
1375 n = isALNUM_LC(nextchr);
1377 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1382 PL_reg_flags |= RF_tainted;
1386 /* was last char in word? */
1387 ln = (locinput != PL_regbol)
1388 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1389 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1390 ln = isALNUM_uni(ln);
1391 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1394 ln = isALNUM_LC_uni(ln);
1395 n = isALNUM_LC_utf8((U8*)locinput);
1397 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1401 PL_reg_flags |= RF_tainted;
1404 if (!nextchr && locinput >= PL_regeol)
1406 if (!(OP(scan) == SPACE
1407 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1409 nextchr = UCHARAT(++locinput);
1412 PL_reg_flags |= RF_tainted;
1415 if (!nextchr && locinput >= PL_regeol)
1417 if (nextchr & 0x80) {
1418 if (!(OP(scan) == SPACEUTF8
1419 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1420 : isSPACE_LC_utf8((U8*)locinput)))
1424 locinput += utf8skip[nextchr];
1425 nextchr = UCHARAT(locinput);
1428 if (!(OP(scan) == SPACEUTF8
1429 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1431 nextchr = UCHARAT(++locinput);
1434 PL_reg_flags |= RF_tainted;
1439 if (OP(scan) == SPACE
1440 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1442 nextchr = UCHARAT(++locinput);
1445 PL_reg_flags |= RF_tainted;
1450 if (nextchr & 0x80) {
1451 if (OP(scan) == NSPACEUTF8
1452 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1453 : isSPACE_LC_utf8((U8*)locinput))
1457 locinput += utf8skip[nextchr];
1458 nextchr = UCHARAT(locinput);
1461 if (OP(scan) == NSPACEUTF8
1462 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1464 nextchr = UCHARAT(++locinput);
1467 if (!isDIGIT(nextchr))
1469 nextchr = UCHARAT(++locinput);
1472 if (nextchr & 0x80) {
1473 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1475 locinput += utf8skip[nextchr];
1476 nextchr = UCHARAT(locinput);
1479 if (!isDIGIT(nextchr))
1481 nextchr = UCHARAT(++locinput);
1484 if (!nextchr && locinput >= PL_regeol)
1486 if (isDIGIT(nextchr))
1488 nextchr = UCHARAT(++locinput);
1491 if (!nextchr && locinput >= PL_regeol)
1493 if (nextchr & 0x80) {
1494 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1496 locinput += utf8skip[nextchr];
1497 nextchr = UCHARAT(locinput);
1500 if (isDIGIT(nextchr))
1502 nextchr = UCHARAT(++locinput);
1505 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1507 locinput += utf8skip[nextchr];
1508 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1509 locinput += UTF8SKIP(locinput);
1510 if (locinput > PL_regeol)
1512 nextchr = UCHARAT(locinput);
1515 PL_reg_flags |= RF_tainted;
1519 n = ARG(scan); /* which paren pair */
1520 s = PL_regstartp[n];
1521 if (*PL_reglastparen < n || !s)
1522 sayNO; /* Do not match unless seen CLOSEn. */
1523 if (s == PL_regendp[n])
1526 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1528 char *e = PL_regendp[n];
1530 * Note that we can't do the "other character" lookup trick as
1531 * in the 8-bit case (no pun intended) because in Unicode we
1532 * have to map both upper and title case to lower case.
1534 if (OP(scan) == REFF) {
1538 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1548 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1555 nextchr = UCHARAT(locinput);
1559 /* Inline the first character, for speed. */
1560 if (UCHARAT(s) != nextchr &&
1562 (UCHARAT(s) != ((OP(scan) == REFF
1563 ? fold : fold_locale)[nextchr]))))
1565 ln = PL_regendp[n] - s;
1566 if (locinput + ln > PL_regeol)
1568 if (ln > 1 && (OP(scan) == REF
1569 ? memNE(s, locinput, ln)
1571 ? ibcmp(s, locinput, ln)
1572 : ibcmp_locale(s, locinput, ln))))
1575 nextchr = UCHARAT(locinput);
1586 OP_4tree *oop = PL_op;
1587 COP *ocurcop = PL_curcop;
1588 SV **ocurpad = PL_curpad;
1592 PL_op = (OP_4tree*)PL_regdata->data[n];
1593 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1594 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1596 CALLRUNOPS(); /* Scalar context. */
1602 PL_curpad = ocurpad;
1603 PL_curcop = ocurcop;
1605 if (logical == 2) { /* Postponed subexpression. */
1607 my_magic mg = Null(my_magic);
1610 CHECKPOINT cp, lastcp;
1612 if(SvROK(ret) || SvRMAGICAL(ret)) {
1613 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1616 mg = mg_find(sv, 'r');
1619 re = (regexp *)mg->mg_obj;
1624 char *t = SvPV(ret, len);
1626 char *oprecomp = PL_regprecomp;
1627 I32 osize = PL_regsize;
1628 I32 onpar = PL_regnpar;
1631 re = CALLREGCOMP(t, t + len, &pm);
1633 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1634 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1635 PL_regprecomp = oprecomp;
1640 PerlIO_printf(Perl_debug_log,
1641 "Entering embedded `%s%.60s%s%s'\n",
1645 (strlen(re->precomp) > 60 ? "..." : ""))
1648 state.prev = PL_reg_call_cc;
1649 state.cc = PL_regcc;
1650 state.re = PL_reg_re;
1656 cp = regcppush(0); /* Save *all* the positions. */
1659 state.ss = PL_savestack_ix;
1660 *PL_reglastparen = 0;
1661 PL_reg_call_cc = &state;
1662 PL_reginput = locinput;
1663 if (regmatch(re->program + 1)) {
1669 PerlIO_printf(Perl_debug_log,
1671 REPORT_CODE_OFF+PL_regindent*2, "")
1676 PL_reg_call_cc = state.prev;
1677 PL_regcc = state.cc;
1678 PL_reg_re = state.re;
1679 cache_re(PL_reg_re);
1686 sv_setsv(save_scalar(PL_replgv), ret);
1690 n = ARG(scan); /* which paren pair */
1691 PL_reg_start_tmp[n] = locinput;
1696 n = ARG(scan); /* which paren pair */
1697 PL_regstartp[n] = PL_reg_start_tmp[n];
1698 PL_regendp[n] = locinput;
1699 if (n > *PL_reglastparen)
1700 *PL_reglastparen = n;
1703 n = ARG(scan); /* which paren pair */
1704 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1708 next = NEXTOPER(NEXTOPER(scan));
1710 next = scan + ARG(scan);
1711 if (OP(next) == IFTHEN) /* Fake one. */
1712 next = NEXTOPER(NEXTOPER(next));
1716 logical = scan->flags;
1720 CHECKPOINT cp = PL_savestack_ix;
1722 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1724 cc.oldcc = PL_regcc;
1726 cc.parenfloor = *PL_reglastparen;
1728 cc.min = ARG1(scan);
1729 cc.max = ARG2(scan);
1730 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1734 PL_reginput = locinput;
1735 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1737 PL_regcc = cc.oldcc;
1743 * This is really hard to understand, because after we match
1744 * what we're trying to match, we must make sure the rest of
1745 * the RE is going to match for sure, and to do that we have
1746 * to go back UP the parse tree by recursing ever deeper. And
1747 * if it fails, we have to reset our parent's current state
1748 * that we can try again after backing off.
1751 CHECKPOINT cp, lastcp;
1752 CURCUR* cc = PL_regcc;
1753 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1755 n = cc->cur + 1; /* how many we know we matched */
1756 PL_reginput = locinput;
1759 PerlIO_printf(Perl_debug_log,
1760 "%*s %ld out of %ld..%ld cc=%lx\n",
1761 REPORT_CODE_OFF+PL_regindent*2, "",
1762 (long)n, (long)cc->min,
1763 (long)cc->max, (long)cc)
1766 /* If degenerate scan matches "", assume scan done. */
1768 if (locinput == cc->lastloc && n >= cc->min) {
1769 PL_regcc = cc->oldcc;
1772 PerlIO_printf(Perl_debug_log,
1773 "%*s empty match detected, try continuation...\n",
1774 REPORT_CODE_OFF+PL_regindent*2, "")
1776 if (regmatch(cc->next))
1779 PerlIO_printf(Perl_debug_log,
1781 REPORT_CODE_OFF+PL_regindent*2, "")
1788 /* First just match a string of min scans. */
1792 cc->lastloc = locinput;
1793 if (regmatch(cc->scan))
1796 cc->lastloc = lastloc;
1798 PerlIO_printf(Perl_debug_log,
1800 REPORT_CODE_OFF+PL_regindent*2, "")
1805 /* Prefer next over scan for minimal matching. */
1808 PL_regcc = cc->oldcc;
1810 cp = regcppush(cc->parenfloor);
1812 if (regmatch(cc->next)) {
1814 sayYES; /* All done. */
1821 if (n >= cc->max) { /* Maximum greed exceeded? */
1822 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1823 && !(PL_reg_flags & RF_warned)) {
1824 PL_reg_flags |= RF_warned;
1825 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1826 "Complex regular subexpression recursion",
1833 PerlIO_printf(Perl_debug_log,
1834 "%*s trying longer...\n",
1835 REPORT_CODE_OFF+PL_regindent*2, "")
1837 /* Try scanning more and see if it helps. */
1838 PL_reginput = locinput;
1840 cc->lastloc = locinput;
1841 cp = regcppush(cc->parenfloor);
1843 if (regmatch(cc->scan)) {
1848 PerlIO_printf(Perl_debug_log,
1850 REPORT_CODE_OFF+PL_regindent*2, "")
1855 cc->lastloc = lastloc;
1859 /* Prefer scan over next for maximal matching. */
1861 if (n < cc->max) { /* More greed allowed? */
1862 cp = regcppush(cc->parenfloor);
1864 cc->lastloc = locinput;
1866 if (regmatch(cc->scan)) {
1871 regcppop(); /* Restore some previous $<digit>s? */
1872 PL_reginput = locinput;
1874 PerlIO_printf(Perl_debug_log,
1875 "%*s failed, try continuation...\n",
1876 REPORT_CODE_OFF+PL_regindent*2, "")
1879 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1880 && !(PL_reg_flags & RF_warned)) {
1881 PL_reg_flags |= RF_warned;
1882 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1883 "Complex regular subexpression recursion",
1887 /* Failed deeper matches of scan, so see if this one works. */
1888 PL_regcc = cc->oldcc;
1890 if (regmatch(cc->next))
1893 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1894 REPORT_CODE_OFF+PL_regindent*2, "")
1899 cc->lastloc = lastloc;
1904 next = scan + ARG(scan);
1907 inner = NEXTOPER(NEXTOPER(scan));
1910 inner = NEXTOPER(scan);
1915 if (OP(next) != c1) /* No choice. */
1916 next = inner; /* Avoid recursion. */
1918 int lastparen = *PL_reglastparen;
1922 PL_reginput = locinput;
1923 if (regmatch(inner))
1926 for (n = *PL_reglastparen; n > lastparen; n--)
1928 *PL_reglastparen = n;
1931 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1935 inner = NEXTOPER(scan);
1936 if (c1 == BRANCHJ) {
1937 inner = NEXTOPER(inner);
1939 } while (scan != NULL && OP(scan) == c1);
1953 /* We suppose that the next guy does not need
1954 backtracking: in particular, it is of constant length,
1955 and has no parenths to influence future backrefs. */
1956 ln = ARG1(scan); /* min to match */
1957 n = ARG2(scan); /* max to match */
1958 paren = scan->flags;
1960 if (paren > PL_regsize)
1962 if (paren > *PL_reglastparen)
1963 *PL_reglastparen = paren;
1965 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1967 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1968 PL_reginput = locinput;
1971 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1973 if (ln && l == 0 && n >= ln
1974 /* In fact, this is tricky. If paren, then the
1975 fact that we did/didnot match may influence
1976 future execution. */
1977 && !(paren && ln == 0))
1979 locinput = PL_reginput;
1980 if (regkind[(U8)OP(next)] == EXACT) {
1981 c1 = UCHARAT(OPERAND(next) + 1);
1982 if (OP(next) == EXACTF)
1984 else if (OP(next) == EXACTFL)
1985 c2 = fold_locale[c1];
1992 /* This may be improved if l == 0. */
1993 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1994 /* If it could work, try it. */
1996 UCHARAT(PL_reginput) == c1 ||
1997 UCHARAT(PL_reginput) == c2)
2001 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2002 PL_regendp[paren] = PL_reginput;
2005 PL_regendp[paren] = NULL;
2011 /* Couldn't or didn't -- move forward. */
2012 PL_reginput = locinput;
2013 if (regrepeat_hard(scan, 1, &l)) {
2015 locinput = PL_reginput;
2022 n = regrepeat_hard(scan, n, &l);
2023 if (n != 0 && l == 0
2024 /* In fact, this is tricky. If paren, then the
2025 fact that we did/didnot match may influence
2026 future execution. */
2027 && !(paren && ln == 0))
2029 locinput = PL_reginput;
2031 PerlIO_printf(Perl_debug_log,
2032 "%*s matched %ld times, len=%ld...\n",
2033 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2036 if (regkind[(U8)OP(next)] == EXACT) {
2037 c1 = UCHARAT(OPERAND(next) + 1);
2038 if (OP(next) == EXACTF)
2040 else if (OP(next) == EXACTFL)
2041 c2 = fold_locale[c1];
2050 /* If it could work, try it. */
2052 UCHARAT(PL_reginput) == c1 ||
2053 UCHARAT(PL_reginput) == c2)
2056 PerlIO_printf(Perl_debug_log,
2057 "%*s trying tail with n=%ld...\n",
2058 REPORT_CODE_OFF+PL_regindent*2, "", n)
2062 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2063 PL_regendp[paren] = PL_reginput;
2066 PL_regendp[paren] = NULL;
2072 /* Couldn't or didn't -- back up. */
2074 locinput = HOPc(locinput, -l);
2075 PL_reginput = locinput;
2082 paren = scan->flags; /* Which paren to set */
2083 if (paren > PL_regsize)
2085 if (paren > *PL_reglastparen)
2086 *PL_reglastparen = paren;
2087 ln = ARG1(scan); /* min to match */
2088 n = ARG2(scan); /* max to match */
2089 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2093 ln = ARG1(scan); /* min to match */
2094 n = ARG2(scan); /* max to match */
2095 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2100 scan = NEXTOPER(scan);
2106 scan = NEXTOPER(scan);
2110 * Lookahead to avoid useless match attempts
2111 * when we know what character comes next.
2113 if (regkind[(U8)OP(next)] == EXACT) {
2114 c1 = UCHARAT(OPERAND(next) + 1);
2115 if (OP(next) == EXACTF)
2117 else if (OP(next) == EXACTFL)
2118 c2 = fold_locale[c1];
2124 PL_reginput = locinput;
2128 if (ln && regrepeat(scan, ln) < ln)
2130 locinput = PL_reginput;
2132 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2133 /* If it could work, try it. */
2135 UCHARAT(PL_reginput) == c1 ||
2136 UCHARAT(PL_reginput) == c2)
2140 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2141 PL_regendp[paren] = PL_reginput;
2144 PL_regendp[paren] = NULL;
2150 /* Couldn't or didn't -- move forward. */
2151 PL_reginput = locinput;
2152 if (regrepeat(scan, 1)) {
2154 locinput = PL_reginput;
2162 n = regrepeat(scan, n);
2163 locinput = PL_reginput;
2164 if (ln < n && regkind[(U8)OP(next)] == EOL &&
2165 (!PL_multiline || OP(next) == SEOL))
2166 ln = n; /* why back off? */
2170 /* If it could work, try it. */
2172 UCHARAT(PL_reginput) == c1 ||
2173 UCHARAT(PL_reginput) == c2)
2177 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2178 PL_regendp[paren] = PL_reginput;
2181 PL_regendp[paren] = NULL;
2187 /* Couldn't or didn't -- back up. */
2189 PL_reginput = locinput = HOPc(locinput, -1);
2194 /* If it could work, try it. */
2196 UCHARAT(PL_reginput) == c1 ||
2197 UCHARAT(PL_reginput) == c2)
2203 /* Couldn't or didn't -- back up. */
2205 PL_reginput = locinput = HOPc(locinput, -1);
2212 if (PL_reg_call_cc) {
2213 re_cc_state *cur_call_cc = PL_reg_call_cc;
2214 CURCUR *cctmp = PL_regcc;
2215 regexp *re = PL_reg_re;
2216 CHECKPOINT cp, lastcp;
2218 cp = regcppush(0); /* Save *all* the positions. */
2220 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2222 PL_reginput = locinput; /* Make position available to
2224 cache_re(PL_reg_call_cc->re);
2225 PL_regcc = PL_reg_call_cc->cc;
2226 PL_reg_call_cc = PL_reg_call_cc->prev;
2227 if (regmatch(cur_call_cc->node)) {
2228 PL_reg_call_cc = cur_call_cc;
2234 PL_reg_call_cc = cur_call_cc;
2240 PerlIO_printf(Perl_debug_log,
2241 "%*s continuation failed...\n",
2242 REPORT_CODE_OFF+PL_regindent*2, "")
2246 if (locinput < PL_regtill)
2247 sayNO; /* Cannot match: too short. */
2250 PL_reginput = locinput; /* put where regtry can find it */
2251 sayYES; /* Success! */
2254 PL_reginput = locinput;
2259 s = HOPMAYBEc(locinput, -scan->flags);
2265 PL_reginput = locinput;
2270 s = HOPMAYBEc(locinput, -scan->flags);
2276 PL_reginput = locinput;
2279 inner = NEXTOPER(NEXTOPER(scan));
2280 if (regmatch(inner) != n) {
2295 if (OP(scan) == SUSPEND) {
2296 locinput = PL_reginput;
2297 nextchr = UCHARAT(locinput);
2302 next = scan + ARG(scan);
2307 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2308 (unsigned long)scan, OP(scan));
2309 FAIL("regexp memory corruption");
2315 * We get here only if there's trouble -- normally "case END" is
2316 * the terminating point.
2318 FAIL("corrupted regexp pointers");
2336 - regrepeat - repeatedly match something simple, report how many
2339 * [This routine now assumes that it will only match on things of length 1.
2340 * That was true before, but now we assume scan - reginput is the count,
2341 * rather than incrementing count on every character. [Er, except utf8.]]
2344 regrepeat(regnode *p, I32 max)
2347 register char *scan;
2348 register char *opnd;
2350 register char *loceol = PL_regeol;
2351 register I32 hardcount = 0;
2354 if (max != REG_INFTY && max < loceol - scan)
2355 loceol = scan + max;
2356 opnd = (char *) OPERAND(p);
2359 while (scan < loceol && *scan != '\n')
2367 while (scan < loceol && *scan != '\n') {
2368 scan += UTF8SKIP(scan);
2374 while (scan < loceol) {
2375 scan += UTF8SKIP(scan);
2379 case EXACT: /* length of string is 1 */
2380 c = UCHARAT(++opnd);
2381 while (scan < loceol && UCHARAT(scan) == c)
2384 case EXACTF: /* length of string is 1 */
2385 c = UCHARAT(++opnd);
2386 while (scan < loceol &&
2387 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
2390 case EXACTFL: /* length of string is 1 */
2391 PL_reg_flags |= RF_tainted;
2392 c = UCHARAT(++opnd);
2393 while (scan < loceol &&
2394 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
2399 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2400 scan += UTF8SKIP(scan);
2405 while (scan < loceol && REGINCLASS(opnd, *scan))
2409 while (scan < loceol && isALNUM(*scan))
2414 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2415 scan += UTF8SKIP(scan);
2420 PL_reg_flags |= RF_tainted;
2421 while (scan < loceol && isALNUM_LC(*scan))
2425 PL_reg_flags |= RF_tainted;
2427 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2428 scan += UTF8SKIP(scan);
2434 while (scan < loceol && !isALNUM(*scan))
2439 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2440 scan += UTF8SKIP(scan);
2445 PL_reg_flags |= RF_tainted;
2446 while (scan < loceol && !isALNUM_LC(*scan))
2450 PL_reg_flags |= RF_tainted;
2452 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2453 scan += UTF8SKIP(scan);
2458 while (scan < loceol && isSPACE(*scan))
2463 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2464 scan += UTF8SKIP(scan);
2469 PL_reg_flags |= RF_tainted;
2470 while (scan < loceol && isSPACE_LC(*scan))
2474 PL_reg_flags |= RF_tainted;
2476 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2477 scan += UTF8SKIP(scan);
2482 while (scan < loceol && !isSPACE(*scan))
2487 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2488 scan += UTF8SKIP(scan);
2493 PL_reg_flags |= RF_tainted;
2494 while (scan < loceol && !isSPACE_LC(*scan))
2498 PL_reg_flags |= RF_tainted;
2500 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2501 scan += UTF8SKIP(scan);
2506 while (scan < loceol && isDIGIT(*scan))
2511 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2512 scan += UTF8SKIP(scan);
2518 while (scan < loceol && !isDIGIT(*scan))
2523 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2524 scan += UTF8SKIP(scan);
2528 default: /* Called on something of 0 width. */
2529 break; /* So match right here or not at all. */
2535 c = scan - PL_reginput;
2540 SV *prop = sv_newmortal();
2543 PerlIO_printf(Perl_debug_log,
2544 "%*s %s can match %ld times out of %ld...\n",
2545 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2552 - regrepeat_hard - repeatedly match something, report total lenth and length
2554 * The repeater is supposed to have constant length.
2558 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2561 register char *scan;
2562 register char *start;
2563 register char *loceol = PL_regeol;
2565 I32 count = 0, res = 1;
2570 start = PL_reginput;
2572 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2575 while (start < PL_reginput) {
2577 start += UTF8SKIP(start);
2588 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2590 *lp = l = PL_reginput - start;
2591 if (max != REG_INFTY && l*max < loceol - scan)
2592 loceol = scan + l*max;
2605 - regclass - determine if a character falls into a character class
2609 reginclass(register char *p, register I32 c)
2616 if (ANYOF_TEST(p, c))
2618 else if (flags & ANYOF_FOLD) {
2620 if (flags & ANYOF_LOCALE) {
2621 PL_reg_flags |= RF_tainted;
2622 cf = fold_locale[c];
2626 if (ANYOF_TEST(p, cf))
2630 if (!match && (flags & ANYOF_ISA)) {
2631 PL_reg_flags |= RF_tainted;
2633 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2634 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2635 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2636 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2642 return (flags & ANYOF_INVERT) ? !match : match;
2646 reginclassutf8(regnode *f, U8 *p)
2649 char flags = ARG1(f);
2651 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2653 if (swash_fetch(sv, p))
2655 else if (flags & ANYOF_FOLD) {
2658 if (flags & ANYOF_LOCALE) {
2659 PL_reg_flags |= RF_tainted;
2660 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2663 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2664 if (swash_fetch(sv, tmpbuf))
2668 if (!match && (flags & ANYOF_ISA)) {
2669 PL_reg_flags |= RF_tainted;
2671 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2672 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2673 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2674 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2680 return (flags & ANYOF_INVERT) ? !match : match;
2684 reghop(U8 *s, I32 off)
2688 while (off-- && s < (U8*)PL_regeol)
2693 if (s > (U8*)PL_bostr) {
2696 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2698 } /* XXX could check well-formedness here */
2706 reghopmaybe(U8* s, I32 off)
2710 while (off-- && s < (U8*)PL_regeol)
2717 if (s > (U8*)PL_bostr) {
2720 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2722 } /* XXX could check well-formedness here */