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
40 # define Perl_reginitcolors my_reginitcolors
45 * pregcomp and pregexec -- regsub and regerror are not used in perl
47 * Copyright (c) 1986 by University of Toronto.
48 * Written by Henry Spencer. Not derived from licensed software.
50 * Permission is granted to anyone to use this software for any
51 * purpose on any computer system, and to redistribute it freely,
52 * subject to the following restrictions:
54 * 1. The author is not responsible for the consequences of use of
55 * this software, no matter how awful, even if they arise
58 * 2. The origin of this software must not be misrepresented, either
59 * by explicit claim or by omission.
61 * 3. Altered versions must be plainly marked as such, and must not
62 * be misrepresented as being the original software.
64 **** Alterations to Henry's code are...
66 **** Copyright (c) 1991-1999, Larry Wall
68 **** You may distribute under the terms of either the GNU General Public
69 **** License or the Artistic License, as specified in the README file.
71 * Beware that some of this code is subtly aware of the way operator
72 * precedence is structured in regular expressions. Serious changes in
73 * regular-expression syntax might require a total rethink.
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));
112 static void restore_pos _((void *arg));
115 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
116 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
118 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
119 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
122 static U8 * reghop _((U8 *pos, I32 off));
123 static U8 * reghopmaybe _((U8 *pos, I32 off));
125 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
126 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
127 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
128 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
129 #define HOPc(pos,off) ((char*)HOP(pos,off))
130 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
133 regcppush(I32 parenfloor)
136 int retval = PL_savestack_ix;
137 int i = (PL_regsize - parenfloor) * 4;
141 for (p = PL_regsize; p > parenfloor; p--) {
142 SSPUSHPTR(PL_regendp[p]);
143 SSPUSHPTR(PL_regstartp[p]);
144 SSPUSHPTR(PL_reg_start_tmp[p]);
147 SSPUSHINT(PL_regsize);
148 SSPUSHINT(*PL_reglastparen);
149 SSPUSHPTR(PL_reginput);
151 SSPUSHINT(SAVEt_REGCONTEXT);
155 /* These are needed since we do not localize EVAL nodes: */
156 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
157 " Setting an EVAL scope, savestack=%i\n", \
158 PL_savestack_ix)); lastcp = PL_savestack_ix
160 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
161 PerlIO_printf(Perl_debug_log, \
162 " Clearing an EVAL scope, savestack=%i..%i\n", \
163 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
173 assert(i == SAVEt_REGCONTEXT);
175 input = (char *) SSPOPPTR;
176 *PL_reglastparen = SSPOPINT;
177 PL_regsize = SSPOPINT;
178 for (i -= 3; i > 0; i -= 4) {
179 paren = (U32)SSPOPINT;
180 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
181 PL_regstartp[paren] = (char *) SSPOPPTR;
182 tmps = (char*)SSPOPPTR;
183 if (paren <= *PL_reglastparen)
184 PL_regendp[paren] = tmps;
186 PerlIO_printf(Perl_debug_log,
187 " restoring \\%d to %d(%d)..%d%s\n",
188 paren, PL_regstartp[paren] - PL_regbol,
189 PL_reg_start_tmp[paren] - PL_regbol,
190 PL_regendp[paren] - PL_regbol,
191 (paren > *PL_reglastparen ? "(no)" : ""));
195 if (*PL_reglastparen + 1 <= PL_regnpar) {
196 PerlIO_printf(Perl_debug_log,
197 " restoring \\%d..\\%d to undef\n",
198 *PL_reglastparen + 1, PL_regnpar);
201 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
202 if (paren > PL_regsize)
203 PL_regstartp[paren] = Nullch;
204 PL_regendp[paren] = Nullch;
213 I32 tmp = PL_savestack_ix;
215 PL_savestack_ix = ss;
217 PL_savestack_ix = tmp;
221 typedef struct re_cc_state
225 struct re_cc_state *prev;
230 #define regcpblow(cp) LEAVE_SCOPE(cp)
233 * pregexec and friends
237 - pregexec - match a regexp against a string
240 pregexec(register regexp *prog, char *stringarg, register char *strend,
241 char *strbeg, I32 minend, SV *screamer, U32 nosave)
242 /* strend: pointer to null at end of string */
243 /* strbeg: real beginning of string */
244 /* minend: end of match must be >=minend after stringarg. */
245 /* nosave: For optimizations. */
248 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
249 nosave ? 0 : REXEC_COPY_STR);
253 cache_re(regexp *prog)
256 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
258 PL_regprogram = prog->program;
260 PL_regnpar = prog->nparens;
261 PL_regdata = prog->data;
266 restore_pos(void *arg)
269 if (PL_reg_eval_set) {
270 PL_reg_magic->mg_len = PL_reg_oldpos;
272 PL_curpm = PL_reg_oldcurpm;
278 - regexec_flags - match a regexp against a string
281 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
282 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
283 /* strend: pointer to null at end of string */
284 /* strbeg: real beginning of string */
285 /* minend: end of match must be >=minend after stringarg. */
286 /* data: May be used for some additional optimizations. */
287 /* nosave: For optimizations. */
292 register char *startpos = stringarg;
294 I32 minlen; /* must match at least this many chars */
295 I32 dontbother = 0; /* how many characters not to try at end */
297 I32 start_shift = 0; /* Offset of the start to find
298 constant substr. */ /* CC */
299 I32 end_shift = 0; /* Same for the end. */ /* CC */
300 I32 scream_pos = -1; /* Internal iterator of scream. */
302 SV* oreplsv = GvSV(PL_replgv);
310 PL_regnarrate = PL_debug & 512;
314 if (prog == NULL || startpos == NULL) {
315 croak("NULL regexp parameter");
319 minlen = prog->minlen;
320 if (strend - startpos < minlen) goto phooey;
322 if (startpos == strbeg) /* is ^ valid at stringarg? */
325 PL_regprev = (U32)stringarg[-1];
326 if (!PL_multiline && PL_regprev == '\n')
327 PL_regprev = '\0'; /* force ^ to NOT match */
330 /* Check validity of program. */
331 if (UCHARAT(prog->program) != REG_MAGIC) {
332 croak("corrupted regexp program");
338 if (prog->reganch & ROPT_UTF8)
339 PL_reg_flags |= RF_utf8;
341 /* Mark beginning of line for ^ and lookbehind. */
342 PL_regbol = startpos;
346 /* Mark end of line for $ (and such) */
349 /* see how far we have to get to not match where we matched before */
350 PL_regtill = startpos+minend;
352 /* We start without call_cc context. */
355 /* If there is a "must appear" string, look for it. */
357 if (!(flags & REXEC_CHECKED)
358 && prog->check_substr != Nullsv &&
359 !(prog->reganch & ROPT_ANCH_GPOS) &&
360 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
361 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
364 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
365 /* Should be nonnegative! */
366 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
367 if (flags & REXEC_SCREAM) {
368 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
369 s = screaminstr(sv, prog->check_substr,
370 start_shift + (stringarg - strbeg),
371 end_shift, &scream_pos, 0);
377 s = fbm_instr((unsigned char*)s + start_shift,
378 (unsigned char*)strend - end_shift,
379 prog->check_substr, 0);
381 ++BmUSEFUL(prog->check_substr); /* hooray */
382 goto phooey; /* not present */
384 else if (s - stringarg > prog->check_offset_max &&
386 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
387 : (t = s - prog->check_offset_max) != 0
391 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
394 else if (!(prog->reganch & ROPT_NAUGHTY)
395 && --BmUSEFUL(prog->check_substr) < 0
396 && prog->check_substr == prog->float_substr) { /* boo */
397 SvREFCNT_dec(prog->check_substr);
398 prog->check_substr = Nullsv; /* disable */
399 prog->float_substr = Nullsv; /* clear */
406 DEBUG_r(if (!PL_colorset) reginitcolors());
407 DEBUG_r(PerlIO_printf(Perl_debug_log,
408 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
409 PL_colors[4],PL_colors[5],PL_colors[0],
412 (strlen(prog->precomp) > 60 ? "..." : ""),
414 (strend - startpos > 60 ? 60 : strend - startpos),
415 startpos, PL_colors[1],
416 (strend - startpos > 60 ? "..." : ""))
419 if (prog->reganch & ROPT_GPOS_SEEN) {
422 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
423 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
424 PL_reg_ganch = strbeg + mg->mg_len;
426 PL_reg_ganch = startpos;
429 /* Simplest case: anchored match need be tried only once. */
430 /* [unless only anchor is BOL and multiline is set] */
431 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
432 if (regtry(prog, startpos))
434 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
435 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
438 dontbother = minlen - 1;
439 strend = HOPc(strend, -dontbother);
440 /* for multiline we only have to try after newlines */
444 if (*s++ == '\n') { /* don't need PL_utf8skip here */
445 if (s < strend && regtry(prog, s))
451 } else if (prog->reganch & ROPT_ANCH_GPOS) {
452 if (regtry(prog, PL_reg_ganch))
457 /* Messy cases: unanchored match. */
458 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
459 /* we have /x+whatever/ */
460 /* it must be a one character string */
461 char ch = SvPVX(prog->anchored_substr)[0];
465 if (regtry(prog, s)) goto got_it;
467 while (s < strend && *s == ch)
476 if (regtry(prog, s)) goto got_it;
478 while (s < strend && *s == ch)
486 else if (prog->anchored_substr != Nullsv
487 || (prog->float_substr != Nullsv
488 && prog->float_max_offset < strend - s)) {
489 SV *must = prog->anchored_substr
490 ? prog->anchored_substr : prog->float_substr;
492 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
494 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
495 I32 delta = back_max - back_min;
496 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
497 char *last1; /* Last position checked before */
502 last1 = s - 1; /* bogus */
504 /* XXXX check_substr already used to find `s', can optimize if
505 check_substr==must. */
507 dontbother = end_shift;
508 strend = HOPc(strend, -dontbother);
509 while ( (s <= last) &&
510 ((flags & REXEC_SCREAM)
511 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
512 end_shift, &scream_pos, 0))
513 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
514 (unsigned char*)strend, must, 0))) ) {
515 if (HOPc(s, -back_max) > last1) {
516 last1 = HOPc(s, -back_min);
517 s = HOPc(s, -back_max);
520 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
522 last1 = HOPc(s, -back_min);
542 else if (c = prog->regstclass) {
543 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
547 dontbother = minlen - 1;
548 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
550 /* We know what class it must start with. */
553 cc = (char *) OPERAND(c);
555 if (REGINCLASSUTF8(c, (U8*)s)) {
556 if (tmp && regtry(prog, s))
567 cc = (char *) OPERAND(c);
569 if (REGINCLASS(cc, *s)) {
570 if (tmp && regtry(prog, s))
581 PL_reg_flags |= RF_tainted;
588 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
589 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
591 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
598 if ((minlen || tmp) && regtry(prog,s))
602 PL_reg_flags |= RF_tainted;
607 strend = reghop_c(strend, -1);
609 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
610 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
612 if (tmp == !(OP(c) == BOUND ?
613 swash_fetch(PL_utf8_alnum, (U8*)s) :
614 isALNUM_LC_utf8((U8*)s)))
622 if ((minlen || tmp) && regtry(prog,s))
626 PL_reg_flags |= RF_tainted;
633 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
634 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
636 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
638 else if (regtry(prog, s))
642 if ((minlen || !tmp) && regtry(prog,s))
646 PL_reg_flags |= RF_tainted;
651 strend = reghop_c(strend, -1);
653 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
654 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
656 if (tmp == !(OP(c) == NBOUND ?
657 swash_fetch(PL_utf8_alnum, (U8*)s) :
658 isALNUM_LC_utf8((U8*)s)))
660 else if (regtry(prog, s))
664 if ((minlen || !tmp) && regtry(prog,s))
670 if (tmp && regtry(prog, s))
682 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
683 if (tmp && regtry(prog, s))
694 PL_reg_flags |= RF_tainted;
696 if (isALNUM_LC(*s)) {
697 if (tmp && regtry(prog, s))
708 PL_reg_flags |= RF_tainted;
710 if (isALNUM_LC_utf8((U8*)s)) {
711 if (tmp && regtry(prog, s))
724 if (tmp && regtry(prog, s))
736 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
737 if (tmp && regtry(prog, s))
748 PL_reg_flags |= RF_tainted;
750 if (!isALNUM_LC(*s)) {
751 if (tmp && regtry(prog, s))
762 PL_reg_flags |= RF_tainted;
764 if (!isALNUM_LC_utf8((U8*)s)) {
765 if (tmp && regtry(prog, s))
778 if (tmp && regtry(prog, s))
790 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
791 if (tmp && regtry(prog, s))
802 PL_reg_flags |= RF_tainted;
804 if (isSPACE_LC(*s)) {
805 if (tmp && regtry(prog, s))
816 PL_reg_flags |= RF_tainted;
818 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
819 if (tmp && regtry(prog, s))
832 if (tmp && regtry(prog, s))
844 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
845 if (tmp && regtry(prog, s))
856 PL_reg_flags |= RF_tainted;
858 if (!isSPACE_LC(*s)) {
859 if (tmp && regtry(prog, s))
870 PL_reg_flags |= RF_tainted;
872 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
873 if (tmp && regtry(prog, s))
886 if (tmp && regtry(prog, s))
898 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
899 if (tmp && regtry(prog, s))
912 if (tmp && regtry(prog, s))
924 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
925 if (tmp && regtry(prog, s))
939 if (prog->float_substr != Nullsv) { /* Trim the end. */
941 I32 oldpos = scream_pos;
943 if (flags & REXEC_SCREAM) {
944 last = screaminstr(sv, prog->float_substr, s - strbeg,
945 end_shift, &scream_pos, 1); /* last one */
947 last = scream_olds; /* Only one occurence. */
952 char *little = SvPV(prog->float_substr, len);
954 last = rninstr(s, strend, little, little + len);
956 last = strend; /* matching `$' */
958 if (last == NULL) goto phooey; /* Should not happen! */
959 dontbother = strend - last + prog->float_min_offset;
961 if (minlen && (dontbother < minlen))
962 dontbother = minlen - 1;
963 strend -= dontbother; /* this one's always in bytes! */
964 /* We don't know much -- general case. */
978 } while (s++ < strend);
986 prog->subbeg = strbeg;
987 prog->subend = PL_regeol; /* strend may have been modified */
988 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
990 /* make sure $`, $&, $', and $digit will work later */
991 if (strbeg != prog->subbase) { /* second+ //g match. */
992 if (!(flags & REXEC_COPY_STR)) {
994 Safefree(prog->subbase);
995 prog->subbase = Nullch;
999 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1000 s = savepvn(strbeg, i);
1001 Safefree(prog->subbase);
1003 prog->subbeg = prog->subbase;
1004 prog->subend = prog->subbase + i;
1005 s = prog->subbase + (stringarg - strbeg);
1006 for (i = 0; i <= prog->nparens; i++) {
1007 if (prog->endp[i]) {
1008 prog->startp[i] = s + (prog->startp[i] - startpos);
1009 prog->endp[i] = s + (prog->endp[i] - startpos);
1014 if (PL_reg_eval_set) {
1015 /* Preserve the current value of $^R */
1016 if (oreplsv != GvSV(PL_replgv))
1017 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1018 restored, the value remains
1026 if (PL_reg_eval_set)
1032 - regtry - try match at specific point
1034 STATIC I32 /* 0 failure, 1 success */
1035 regtry(regexp *prog, char *startpos)
1043 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1046 PL_reg_eval_set = RS_init;
1048 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1049 PL_stack_sp - PL_stack_base);
1051 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1052 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1053 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1055 /* Apparently this is not needed, judging by wantarray. */
1056 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1057 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1060 /* Make $_ available to executed code. */
1061 if (PL_reg_sv != DEFSV) {
1062 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1067 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1068 && (mg = mg_find(PL_reg_sv, 'g')))) {
1069 /* prepare for quick setting of pos */
1070 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1071 mg = mg_find(PL_reg_sv, 'g');
1075 PL_reg_oldpos = mg->mg_len;
1076 SAVEDESTRUCTOR(restore_pos, 0);
1079 New(22,PL_reg_curpm, 1, PMOP);
1080 PL_reg_curpm->op_pmregexp = prog;
1081 PL_reg_oldcurpm = PL_curpm;
1082 PL_curpm = PL_reg_curpm;
1083 prog->subbeg = PL_bostr;
1084 prog->subend = PL_regeol; /* strend may have been modified */
1086 prog->startp[0] = startpos;
1087 PL_reginput = startpos;
1088 PL_regstartp = prog->startp;
1089 PL_regendp = prog->endp;
1090 PL_reglastparen = &prog->lastparen;
1091 prog->lastparen = 0;
1093 DEBUG_r(PL_reg_starttry = startpos);
1094 if (PL_reg_start_tmpl <= prog->nparens) {
1095 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1096 if(PL_reg_start_tmp)
1097 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1099 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1102 /* XXXX What this code is doing here?!!! There should be no need
1103 to do this again and again, PL_reglastparen should take care of
1107 if (prog->nparens) {
1108 for (i = prog->nparens; i >= 1; i--) {
1114 if (regmatch(prog->program + 1)) {
1115 prog->endp[0] = PL_reginput;
1123 - regmatch - main matching routine
1125 * Conceptually the strategy is simple: check to see whether the current
1126 * node matches, call self recursively to see whether the rest matches,
1127 * and then act accordingly. In practice we make some effort to avoid
1128 * recursion, in particular by going through "ordinary" nodes (that don't
1129 * need to know whether the rest of the match failed) by a loop instead of
1132 /* [lwall] I've hoisted the register declarations to the outer block in order to
1133 * maybe save a little bit of pushing and popping on the stack. It also takes
1134 * advantage of machines that use a register save mask on subroutine entry.
1136 STATIC I32 /* 0 failure, 1 success */
1137 regmatch(regnode *prog)
1140 register regnode *scan; /* Current node. */
1141 regnode *next; /* Next node. */
1142 regnode *inner; /* Next node in internal branch. */
1143 register I32 nextchr; /* renamed nextchr - nextchar colides with
1144 function of same name */
1145 register I32 n; /* no or next */
1146 register I32 ln; /* len or last */
1147 register char *s; /* operand or save */
1148 register char *locinput = PL_reginput;
1149 register I32 c1, c2, paren; /* case fold search, parenth */
1150 int minmod = 0, sw = 0, logical = 0;
1155 /* Note that nextchr is a byte even in UTF */
1156 nextchr = UCHARAT(locinput);
1158 while (scan != NULL) {
1159 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1161 # define sayYES goto yes
1162 # define sayNO goto no
1163 # define saySAME(x) if (x) goto yes; else goto no
1164 # define REPORT_CODE_OFF 24
1166 # define sayYES return 1
1167 # define sayNO return 0
1168 # define saySAME(x) return x
1171 SV *prop = sv_newmortal();
1172 int docolor = *PL_colors[0];
1173 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1174 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1175 /* The part of the string before starttry has one color
1176 (pref0_len chars), between starttry and current
1177 position another one (pref_len - pref0_len chars),
1178 after the current position the third one.
1179 We assume that pref0_len <= pref_len, otherwise we
1180 decrease pref0_len. */
1181 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1182 ? (5 + taill) - l : locinput - PL_bostr);
1183 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1185 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1186 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1187 ? (5 + taill) - pref_len : PL_regeol - locinput);
1190 if (pref0_len > pref_len)
1191 pref0_len = pref_len;
1192 regprop(prop, scan);
1193 PerlIO_printf(Perl_debug_log,
1194 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1195 locinput - PL_bostr,
1196 PL_colors[4], pref0_len,
1197 locinput - pref_len, PL_colors[5],
1198 PL_colors[2], pref_len - pref0_len,
1199 locinput - pref_len + pref0_len, PL_colors[3],
1200 (docolor ? "" : "> <"),
1201 PL_colors[0], l, locinput, PL_colors[1],
1202 15 - l - pref_len + 1,
1204 scan - PL_regprogram, PL_regindent*2, "",
1208 next = scan + NEXT_OFF(scan);
1214 if (locinput == PL_bostr
1215 ? PL_regprev == '\n'
1217 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1219 /* regtill = regbol; */
1224 if (locinput == PL_bostr
1225 ? PL_regprev == '\n'
1226 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1232 if (locinput == PL_regbol && PL_regprev == '\n')
1236 if (locinput == PL_reg_ganch)
1246 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1251 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1253 if (PL_regeol - locinput > 1)
1257 if (PL_regeol != locinput)
1261 if (nextchr & 0x80) {
1262 locinput += PL_utf8skip[nextchr];
1263 if (locinput > PL_regeol)
1265 nextchr = UCHARAT(locinput);
1268 if (!nextchr && locinput >= PL_regeol)
1270 nextchr = UCHARAT(++locinput);
1273 if (!nextchr && locinput >= PL_regeol)
1275 nextchr = UCHARAT(++locinput);
1278 if (nextchr & 0x80) {
1279 locinput += PL_utf8skip[nextchr];
1280 if (locinput > PL_regeol)
1282 nextchr = UCHARAT(locinput);
1285 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1287 nextchr = UCHARAT(++locinput);
1290 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1292 nextchr = UCHARAT(++locinput);
1295 s = (char *) OPERAND(scan);
1297 /* Inline the first character, for speed. */
1298 if (UCHARAT(s) != nextchr)
1300 if (PL_regeol - locinput < ln)
1302 if (ln > 1 && memNE(s, locinput, ln))
1305 nextchr = UCHARAT(locinput);
1308 PL_reg_flags |= RF_tainted;
1311 s = (char *) OPERAND(scan);
1317 c1 = OP(scan) == EXACTF;
1321 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1322 toLOWER_utf8((U8*)l) :
1323 toLOWER_LC_utf8((U8*)l)))
1331 nextchr = UCHARAT(locinput);
1335 /* Inline the first character, for speed. */
1336 if (UCHARAT(s) != nextchr &&
1337 UCHARAT(s) != ((OP(scan) == EXACTF)
1338 ? PL_fold : PL_fold_locale)[nextchr])
1340 if (PL_regeol - locinput < ln)
1342 if (ln > 1 && (OP(scan) == EXACTF
1343 ? ibcmp(s, locinput, ln)
1344 : ibcmp_locale(s, locinput, ln)))
1347 nextchr = UCHARAT(locinput);
1350 s = (char *) OPERAND(scan);
1351 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1353 if (locinput >= PL_regeol)
1355 locinput += PL_utf8skip[nextchr];
1356 nextchr = UCHARAT(locinput);
1359 s = (char *) OPERAND(scan);
1361 nextchr = UCHARAT(locinput);
1362 if (!REGINCLASS(s, nextchr))
1364 if (!nextchr && locinput >= PL_regeol)
1366 nextchr = UCHARAT(++locinput);
1369 PL_reg_flags |= RF_tainted;
1374 if (!(OP(scan) == ALNUM
1375 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1377 nextchr = UCHARAT(++locinput);
1380 PL_reg_flags |= RF_tainted;
1385 if (nextchr & 0x80) {
1386 if (!(OP(scan) == ALNUMUTF8
1387 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1388 : isALNUM_LC_utf8((U8*)locinput)))
1392 locinput += PL_utf8skip[nextchr];
1393 nextchr = UCHARAT(locinput);
1396 if (!(OP(scan) == ALNUMUTF8
1397 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1399 nextchr = UCHARAT(++locinput);
1402 PL_reg_flags |= RF_tainted;
1405 if (!nextchr && locinput >= PL_regeol)
1407 if (OP(scan) == NALNUM
1408 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1410 nextchr = UCHARAT(++locinput);
1413 PL_reg_flags |= RF_tainted;
1416 if (!nextchr && locinput >= PL_regeol)
1418 if (nextchr & 0x80) {
1419 if (OP(scan) == NALNUMUTF8
1420 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1421 : isALNUM_LC_utf8((U8*)locinput))
1425 locinput += PL_utf8skip[nextchr];
1426 nextchr = UCHARAT(locinput);
1429 if (OP(scan) == NALNUMUTF8
1430 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1432 nextchr = UCHARAT(++locinput);
1436 PL_reg_flags |= RF_tainted;
1440 /* was last char in word? */
1441 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1442 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1444 n = isALNUM(nextchr);
1447 ln = isALNUM_LC(ln);
1448 n = isALNUM_LC(nextchr);
1450 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1455 PL_reg_flags |= RF_tainted;
1459 /* was last char in word? */
1460 ln = (locinput != PL_regbol)
1461 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1462 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1463 ln = isALNUM_uni(ln);
1464 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1467 ln = isALNUM_LC_uni(ln);
1468 n = isALNUM_LC_utf8((U8*)locinput);
1470 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1474 PL_reg_flags |= RF_tainted;
1477 if (!nextchr && locinput >= PL_regeol)
1479 if (!(OP(scan) == SPACE
1480 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1482 nextchr = UCHARAT(++locinput);
1485 PL_reg_flags |= RF_tainted;
1488 if (!nextchr && locinput >= PL_regeol)
1490 if (nextchr & 0x80) {
1491 if (!(OP(scan) == SPACEUTF8
1492 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1493 : isSPACE_LC_utf8((U8*)locinput)))
1497 locinput += PL_utf8skip[nextchr];
1498 nextchr = UCHARAT(locinput);
1501 if (!(OP(scan) == SPACEUTF8
1502 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1504 nextchr = UCHARAT(++locinput);
1507 PL_reg_flags |= RF_tainted;
1512 if (OP(scan) == SPACE
1513 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1515 nextchr = UCHARAT(++locinput);
1518 PL_reg_flags |= RF_tainted;
1523 if (nextchr & 0x80) {
1524 if (OP(scan) == NSPACEUTF8
1525 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1526 : isSPACE_LC_utf8((U8*)locinput))
1530 locinput += PL_utf8skip[nextchr];
1531 nextchr = UCHARAT(locinput);
1534 if (OP(scan) == NSPACEUTF8
1535 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1537 nextchr = UCHARAT(++locinput);
1540 if (!isDIGIT(nextchr))
1542 nextchr = UCHARAT(++locinput);
1545 if (nextchr & 0x80) {
1546 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1548 locinput += PL_utf8skip[nextchr];
1549 nextchr = UCHARAT(locinput);
1552 if (!isDIGIT(nextchr))
1554 nextchr = UCHARAT(++locinput);
1557 if (!nextchr && locinput >= PL_regeol)
1559 if (isDIGIT(nextchr))
1561 nextchr = UCHARAT(++locinput);
1564 if (!nextchr && locinput >= PL_regeol)
1566 if (nextchr & 0x80) {
1567 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1569 locinput += PL_utf8skip[nextchr];
1570 nextchr = UCHARAT(locinput);
1573 if (isDIGIT(nextchr))
1575 nextchr = UCHARAT(++locinput);
1578 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1580 locinput += PL_utf8skip[nextchr];
1581 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1582 locinput += UTF8SKIP(locinput);
1583 if (locinput > PL_regeol)
1585 nextchr = UCHARAT(locinput);
1588 PL_reg_flags |= RF_tainted;
1592 n = ARG(scan); /* which paren pair */
1593 s = PL_regstartp[n];
1594 if (*PL_reglastparen < n || !s)
1595 sayNO; /* Do not match unless seen CLOSEn. */
1596 if (s == PL_regendp[n])
1599 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1601 char *e = PL_regendp[n];
1603 * Note that we can't do the "other character" lookup trick as
1604 * in the 8-bit case (no pun intended) because in Unicode we
1605 * have to map both upper and title case to lower case.
1607 if (OP(scan) == REFF) {
1611 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1621 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1628 nextchr = UCHARAT(locinput);
1632 /* Inline the first character, for speed. */
1633 if (UCHARAT(s) != nextchr &&
1635 (UCHARAT(s) != ((OP(scan) == REFF
1636 ? PL_fold : PL_fold_locale)[nextchr]))))
1638 ln = PL_regendp[n] - s;
1639 if (locinput + ln > PL_regeol)
1641 if (ln > 1 && (OP(scan) == REF
1642 ? memNE(s, locinput, ln)
1644 ? ibcmp(s, locinput, ln)
1645 : ibcmp_locale(s, locinput, ln))))
1648 nextchr = UCHARAT(locinput);
1659 OP_4tree *oop = PL_op;
1660 COP *ocurcop = PL_curcop;
1661 SV **ocurpad = PL_curpad;
1665 PL_op = (OP_4tree*)PL_regdata->data[n];
1666 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1667 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
1668 PL_reg_magic->mg_len = locinput - PL_bostr;
1669 PL_regendp[0] = locinput;
1671 CALLRUNOPS(); /* Scalar context. */
1677 PL_curpad = ocurpad;
1678 PL_curcop = ocurcop;
1680 if (logical == 2) { /* Postponed subexpression. */
1682 MAGIC *mg = Null(MAGIC*);
1685 CHECKPOINT cp, lastcp;
1687 if(SvROK(ret) || SvRMAGICAL(ret)) {
1688 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1691 mg = mg_find(sv, 'r');
1694 re = (regexp *)mg->mg_obj;
1695 (void)ReREFCNT_inc(re);
1699 char *t = SvPV(ret, len);
1701 char *oprecomp = PL_regprecomp;
1702 I32 osize = PL_regsize;
1703 I32 onpar = PL_regnpar;
1706 re = CALLREGCOMP(t, t + len, &pm);
1708 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1709 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1710 PL_regprecomp = oprecomp;
1715 PerlIO_printf(Perl_debug_log,
1716 "Entering embedded `%s%.60s%s%s'\n",
1720 (strlen(re->precomp) > 60 ? "..." : ""))
1723 state.prev = PL_reg_call_cc;
1724 state.cc = PL_regcc;
1725 state.re = PL_reg_re;
1731 cp = regcppush(0); /* Save *all* the positions. */
1734 state.ss = PL_savestack_ix;
1735 *PL_reglastparen = 0;
1736 PL_reg_call_cc = &state;
1737 PL_reginput = locinput;
1738 if (regmatch(re->program + 1)) {
1744 PerlIO_printf(Perl_debug_log,
1746 REPORT_CODE_OFF+PL_regindent*2, "")
1751 PL_reg_call_cc = state.prev;
1752 PL_regcc = state.cc;
1753 PL_reg_re = state.re;
1754 cache_re(PL_reg_re);
1761 sv_setsv(save_scalar(PL_replgv), ret);
1765 n = ARG(scan); /* which paren pair */
1766 PL_reg_start_tmp[n] = locinput;
1771 n = ARG(scan); /* which paren pair */
1772 PL_regstartp[n] = PL_reg_start_tmp[n];
1773 PL_regendp[n] = locinput;
1774 if (n > *PL_reglastparen)
1775 *PL_reglastparen = n;
1778 n = ARG(scan); /* which paren pair */
1779 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1783 next = NEXTOPER(NEXTOPER(scan));
1785 next = scan + ARG(scan);
1786 if (OP(next) == IFTHEN) /* Fake one. */
1787 next = NEXTOPER(NEXTOPER(next));
1791 logical = scan->flags;
1795 CHECKPOINT cp = PL_savestack_ix;
1797 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1799 cc.oldcc = PL_regcc;
1801 cc.parenfloor = *PL_reglastparen;
1803 cc.min = ARG1(scan);
1804 cc.max = ARG2(scan);
1805 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1809 PL_reginput = locinput;
1810 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1812 PL_regcc = cc.oldcc;
1818 * This is really hard to understand, because after we match
1819 * what we're trying to match, we must make sure the rest of
1820 * the RE is going to match for sure, and to do that we have
1821 * to go back UP the parse tree by recursing ever deeper. And
1822 * if it fails, we have to reset our parent's current state
1823 * that we can try again after backing off.
1826 CHECKPOINT cp, lastcp;
1827 CURCUR* cc = PL_regcc;
1828 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1830 n = cc->cur + 1; /* how many we know we matched */
1831 PL_reginput = locinput;
1834 PerlIO_printf(Perl_debug_log,
1835 "%*s %ld out of %ld..%ld cc=%lx\n",
1836 REPORT_CODE_OFF+PL_regindent*2, "",
1837 (long)n, (long)cc->min,
1838 (long)cc->max, (long)cc)
1841 /* If degenerate scan matches "", assume scan done. */
1843 if (locinput == cc->lastloc && n >= cc->min) {
1844 PL_regcc = cc->oldcc;
1847 PerlIO_printf(Perl_debug_log,
1848 "%*s empty match detected, try continuation...\n",
1849 REPORT_CODE_OFF+PL_regindent*2, "")
1851 if (regmatch(cc->next))
1854 PerlIO_printf(Perl_debug_log,
1856 REPORT_CODE_OFF+PL_regindent*2, "")
1863 /* First just match a string of min scans. */
1867 cc->lastloc = locinput;
1868 if (regmatch(cc->scan))
1871 cc->lastloc = lastloc;
1873 PerlIO_printf(Perl_debug_log,
1875 REPORT_CODE_OFF+PL_regindent*2, "")
1880 /* Prefer next over scan for minimal matching. */
1883 PL_regcc = cc->oldcc;
1885 cp = regcppush(cc->parenfloor);
1887 if (regmatch(cc->next)) {
1889 sayYES; /* All done. */
1896 if (n >= cc->max) { /* Maximum greed exceeded? */
1897 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1898 && !(PL_reg_flags & RF_warned)) {
1899 PL_reg_flags |= RF_warned;
1900 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1901 "Complex regular subexpression recursion",
1908 PerlIO_printf(Perl_debug_log,
1909 "%*s trying longer...\n",
1910 REPORT_CODE_OFF+PL_regindent*2, "")
1912 /* Try scanning more and see if it helps. */
1913 PL_reginput = locinput;
1915 cc->lastloc = locinput;
1916 cp = regcppush(cc->parenfloor);
1918 if (regmatch(cc->scan)) {
1923 PerlIO_printf(Perl_debug_log,
1925 REPORT_CODE_OFF+PL_regindent*2, "")
1930 cc->lastloc = lastloc;
1934 /* Prefer scan over next for maximal matching. */
1936 if (n < cc->max) { /* More greed allowed? */
1937 cp = regcppush(cc->parenfloor);
1939 cc->lastloc = locinput;
1941 if (regmatch(cc->scan)) {
1946 regcppop(); /* Restore some previous $<digit>s? */
1947 PL_reginput = locinput;
1949 PerlIO_printf(Perl_debug_log,
1950 "%*s failed, try continuation...\n",
1951 REPORT_CODE_OFF+PL_regindent*2, "")
1954 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1955 && !(PL_reg_flags & RF_warned)) {
1956 PL_reg_flags |= RF_warned;
1957 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1958 "Complex regular subexpression recursion",
1962 /* Failed deeper matches of scan, so see if this one works. */
1963 PL_regcc = cc->oldcc;
1965 if (regmatch(cc->next))
1968 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1969 REPORT_CODE_OFF+PL_regindent*2, "")
1974 cc->lastloc = lastloc;
1979 next = scan + ARG(scan);
1982 inner = NEXTOPER(NEXTOPER(scan));
1985 inner = NEXTOPER(scan);
1990 if (OP(next) != c1) /* No choice. */
1991 next = inner; /* Avoid recursion. */
1993 int lastparen = *PL_reglastparen;
1997 PL_reginput = locinput;
1998 if (regmatch(inner))
2001 for (n = *PL_reglastparen; n > lastparen; n--)
2003 *PL_reglastparen = n;
2006 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2010 inner = NEXTOPER(scan);
2011 if (c1 == BRANCHJ) {
2012 inner = NEXTOPER(inner);
2014 } while (scan != NULL && OP(scan) == c1);
2028 /* We suppose that the next guy does not need
2029 backtracking: in particular, it is of constant length,
2030 and has no parenths to influence future backrefs. */
2031 ln = ARG1(scan); /* min to match */
2032 n = ARG2(scan); /* max to match */
2033 paren = scan->flags;
2035 if (paren > PL_regsize)
2037 if (paren > *PL_reglastparen)
2038 *PL_reglastparen = paren;
2040 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2042 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2043 PL_reginput = locinput;
2046 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2048 if (ln && l == 0 && n >= ln
2049 /* In fact, this is tricky. If paren, then the
2050 fact that we did/didnot match may influence
2051 future execution. */
2052 && !(paren && ln == 0))
2054 locinput = PL_reginput;
2055 if (PL_regkind[(U8)OP(next)] == EXACT) {
2056 c1 = UCHARAT(OPERAND(next) + 1);
2057 if (OP(next) == EXACTF)
2059 else if (OP(next) == EXACTFL)
2060 c2 = PL_fold_locale[c1];
2067 /* This may be improved if l == 0. */
2068 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2069 /* If it could work, try it. */
2071 UCHARAT(PL_reginput) == c1 ||
2072 UCHARAT(PL_reginput) == c2)
2076 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2077 PL_regendp[paren] = PL_reginput;
2080 PL_regendp[paren] = NULL;
2086 /* Couldn't or didn't -- move forward. */
2087 PL_reginput = locinput;
2088 if (regrepeat_hard(scan, 1, &l)) {
2090 locinput = PL_reginput;
2097 n = regrepeat_hard(scan, n, &l);
2098 if (n != 0 && l == 0
2099 /* In fact, this is tricky. If paren, then the
2100 fact that we did/didnot match may influence
2101 future execution. */
2102 && !(paren && ln == 0))
2104 locinput = PL_reginput;
2106 PerlIO_printf(Perl_debug_log,
2107 "%*s matched %ld times, len=%ld...\n",
2108 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2111 if (PL_regkind[(U8)OP(next)] == EXACT) {
2112 c1 = UCHARAT(OPERAND(next) + 1);
2113 if (OP(next) == EXACTF)
2115 else if (OP(next) == EXACTFL)
2116 c2 = PL_fold_locale[c1];
2125 /* If it could work, try it. */
2127 UCHARAT(PL_reginput) == c1 ||
2128 UCHARAT(PL_reginput) == c2)
2131 PerlIO_printf(Perl_debug_log,
2132 "%*s trying tail with n=%ld...\n",
2133 REPORT_CODE_OFF+PL_regindent*2, "", n)
2137 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2138 PL_regendp[paren] = PL_reginput;
2141 PL_regendp[paren] = NULL;
2147 /* Couldn't or didn't -- back up. */
2149 locinput = HOPc(locinput, -l);
2150 PL_reginput = locinput;
2157 paren = scan->flags; /* Which paren to set */
2158 if (paren > PL_regsize)
2160 if (paren > *PL_reglastparen)
2161 *PL_reglastparen = paren;
2162 ln = ARG1(scan); /* min to match */
2163 n = ARG2(scan); /* max to match */
2164 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2168 ln = ARG1(scan); /* min to match */
2169 n = ARG2(scan); /* max to match */
2170 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2175 scan = NEXTOPER(scan);
2181 scan = NEXTOPER(scan);
2185 * Lookahead to avoid useless match attempts
2186 * when we know what character comes next.
2188 if (PL_regkind[(U8)OP(next)] == EXACT) {
2189 c1 = UCHARAT(OPERAND(next) + 1);
2190 if (OP(next) == EXACTF)
2192 else if (OP(next) == EXACTFL)
2193 c2 = PL_fold_locale[c1];
2199 PL_reginput = locinput;
2203 if (ln && regrepeat(scan, ln) < ln)
2205 locinput = PL_reginput;
2208 char *e = locinput + n - ln; /* Should not check after this */
2209 char *old = locinput;
2211 if (e >= PL_regeol || (n == REG_INFTY))
2214 /* Find place 'next' could work */
2216 while (locinput <= e && *locinput != c1)
2219 while (locinput <= e
2226 /* PL_reginput == old now */
2227 if (locinput != old) {
2228 ln = 1; /* Did some */
2229 if (regrepeat(scan, locinput - old) <
2233 /* PL_reginput == locinput now */
2236 PL_regstartp[paren] = HOPc(locinput, -1);
2237 PL_regendp[paren] = locinput;
2240 PL_regendp[paren] = NULL;
2244 PL_reginput = locinput; /* Could be reset... */
2246 /* Couldn't or didn't -- move forward. */
2251 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2252 /* If it could work, try it. */
2254 UCHARAT(PL_reginput) == c1 ||
2255 UCHARAT(PL_reginput) == c2)
2259 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2260 PL_regendp[paren] = PL_reginput;
2263 PL_regendp[paren] = NULL;
2269 /* Couldn't or didn't -- move forward. */
2270 PL_reginput = locinput;
2271 if (regrepeat(scan, 1)) {
2273 locinput = PL_reginput;
2281 n = regrepeat(scan, n);
2282 locinput = PL_reginput;
2283 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2284 (!PL_multiline || OP(next) == SEOL))
2285 ln = n; /* why back off? */
2289 /* If it could work, try it. */
2291 UCHARAT(PL_reginput) == c1 ||
2292 UCHARAT(PL_reginput) == c2)
2296 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2297 PL_regendp[paren] = PL_reginput;
2300 PL_regendp[paren] = NULL;
2306 /* Couldn't or didn't -- back up. */
2308 PL_reginput = locinput = HOPc(locinput, -1);
2313 /* If it could work, try it. */
2315 UCHARAT(PL_reginput) == c1 ||
2316 UCHARAT(PL_reginput) == c2)
2322 /* Couldn't or didn't -- back up. */
2324 PL_reginput = locinput = HOPc(locinput, -1);
2331 if (PL_reg_call_cc) {
2332 re_cc_state *cur_call_cc = PL_reg_call_cc;
2333 CURCUR *cctmp = PL_regcc;
2334 regexp *re = PL_reg_re;
2335 CHECKPOINT cp, lastcp;
2337 cp = regcppush(0); /* Save *all* the positions. */
2339 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2341 PL_reginput = locinput; /* Make position available to
2343 cache_re(PL_reg_call_cc->re);
2344 PL_regcc = PL_reg_call_cc->cc;
2345 PL_reg_call_cc = PL_reg_call_cc->prev;
2346 if (regmatch(cur_call_cc->node)) {
2347 PL_reg_call_cc = cur_call_cc;
2353 PL_reg_call_cc = cur_call_cc;
2359 PerlIO_printf(Perl_debug_log,
2360 "%*s continuation failed...\n",
2361 REPORT_CODE_OFF+PL_regindent*2, "")
2365 if (locinput < PL_regtill)
2366 sayNO; /* Cannot match: too short. */
2369 PL_reginput = locinput; /* put where regtry can find it */
2370 sayYES; /* Success! */
2373 PL_reginput = locinput;
2378 if (UTF) { /* XXXX This is absolutely
2379 broken, we read before
2381 s = HOPMAYBEc(locinput, -scan->flags);
2387 if (locinput < PL_bostr + scan->flags)
2389 PL_reginput = locinput - scan->flags;
2394 PL_reginput = locinput;
2399 if (UTF) { /* XXXX This is absolutely
2400 broken, we read before
2402 s = HOPMAYBEc(locinput, -scan->flags);
2403 if (!s || s < PL_bostr)
2408 if (locinput < PL_bostr + scan->flags)
2410 PL_reginput = locinput - scan->flags;
2415 PL_reginput = locinput;
2418 inner = NEXTOPER(NEXTOPER(scan));
2419 if (regmatch(inner) != n) {
2434 if (OP(scan) == SUSPEND) {
2435 locinput = PL_reginput;
2436 nextchr = UCHARAT(locinput);
2441 next = scan + ARG(scan);
2446 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2447 (unsigned long)scan, OP(scan));
2448 croak("regexp memory corruption");
2454 * We get here only if there's trouble -- normally "case END" is
2455 * the terminating point.
2457 croak("corrupted regexp pointers");
2475 - regrepeat - repeatedly match something simple, report how many
2478 * [This routine now assumes that it will only match on things of length 1.
2479 * That was true before, but now we assume scan - reginput is the count,
2480 * rather than incrementing count on every character. [Er, except utf8.]]
2483 regrepeat(regnode *p, I32 max)
2486 register char *scan;
2487 register char *opnd;
2489 register char *loceol = PL_regeol;
2490 register I32 hardcount = 0;
2493 if (max != REG_INFTY && max < loceol - scan)
2494 loceol = scan + max;
2495 opnd = (char *) OPERAND(p);
2498 while (scan < loceol && *scan != '\n')
2506 while (scan < loceol && *scan != '\n') {
2507 scan += UTF8SKIP(scan);
2513 while (scan < loceol) {
2514 scan += UTF8SKIP(scan);
2518 case EXACT: /* length of string is 1 */
2519 c = UCHARAT(++opnd);
2520 while (scan < loceol && UCHARAT(scan) == c)
2523 case EXACTF: /* length of string is 1 */
2524 c = UCHARAT(++opnd);
2525 while (scan < loceol &&
2526 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2529 case EXACTFL: /* length of string is 1 */
2530 PL_reg_flags |= RF_tainted;
2531 c = UCHARAT(++opnd);
2532 while (scan < loceol &&
2533 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2538 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2539 scan += UTF8SKIP(scan);
2544 while (scan < loceol && REGINCLASS(opnd, *scan))
2548 while (scan < loceol && isALNUM(*scan))
2553 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2554 scan += UTF8SKIP(scan);
2559 PL_reg_flags |= RF_tainted;
2560 while (scan < loceol && isALNUM_LC(*scan))
2564 PL_reg_flags |= RF_tainted;
2566 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2567 scan += UTF8SKIP(scan);
2573 while (scan < loceol && !isALNUM(*scan))
2578 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2579 scan += UTF8SKIP(scan);
2584 PL_reg_flags |= RF_tainted;
2585 while (scan < loceol && !isALNUM_LC(*scan))
2589 PL_reg_flags |= RF_tainted;
2591 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2592 scan += UTF8SKIP(scan);
2597 while (scan < loceol && isSPACE(*scan))
2602 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2603 scan += UTF8SKIP(scan);
2608 PL_reg_flags |= RF_tainted;
2609 while (scan < loceol && isSPACE_LC(*scan))
2613 PL_reg_flags |= RF_tainted;
2615 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2616 scan += UTF8SKIP(scan);
2621 while (scan < loceol && !isSPACE(*scan))
2626 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2627 scan += UTF8SKIP(scan);
2632 PL_reg_flags |= RF_tainted;
2633 while (scan < loceol && !isSPACE_LC(*scan))
2637 PL_reg_flags |= RF_tainted;
2639 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2640 scan += UTF8SKIP(scan);
2645 while (scan < loceol && isDIGIT(*scan))
2650 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2651 scan += UTF8SKIP(scan);
2657 while (scan < loceol && !isDIGIT(*scan))
2662 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2663 scan += UTF8SKIP(scan);
2667 default: /* Called on something of 0 width. */
2668 break; /* So match right here or not at all. */
2674 c = scan - PL_reginput;
2679 SV *prop = sv_newmortal();
2682 PerlIO_printf(Perl_debug_log,
2683 "%*s %s can match %ld times out of %ld...\n",
2684 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2691 - regrepeat_hard - repeatedly match something, report total lenth and length
2693 * The repeater is supposed to have constant length.
2697 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2700 register char *scan;
2701 register char *start;
2702 register char *loceol = PL_regeol;
2704 I32 count = 0, res = 1;
2709 start = PL_reginput;
2711 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2714 while (start < PL_reginput) {
2716 start += UTF8SKIP(start);
2727 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2729 *lp = l = PL_reginput - start;
2730 if (max != REG_INFTY && l*max < loceol - scan)
2731 loceol = scan + l*max;
2744 - reginclass - determine if a character falls into a character class
2748 reginclass(register char *p, register I32 c)
2755 if (ANYOF_TEST(p, c))
2757 else if (flags & ANYOF_FOLD) {
2759 if (flags & ANYOF_LOCALE) {
2760 PL_reg_flags |= RF_tainted;
2761 cf = PL_fold_locale[c];
2765 if (ANYOF_TEST(p, cf))
2769 if (!match && (flags & ANYOF_ISA)) {
2770 PL_reg_flags |= RF_tainted;
2772 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2773 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2774 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2775 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2781 return (flags & ANYOF_INVERT) ? !match : match;
2785 reginclassutf8(regnode *f, U8 *p)
2788 char flags = ARG1(f);
2790 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2792 if (swash_fetch(sv, p))
2794 else if (flags & ANYOF_FOLD) {
2797 if (flags & ANYOF_LOCALE) {
2798 PL_reg_flags |= RF_tainted;
2799 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2802 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2803 if (swash_fetch(sv, tmpbuf))
2807 if (!match && (flags & ANYOF_ISA)) {
2808 PL_reg_flags |= RF_tainted;
2810 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2811 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2812 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2813 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2819 return (flags & ANYOF_INVERT) ? !match : match;
2823 reghop(U8 *s, I32 off)
2827 while (off-- && s < (U8*)PL_regeol)
2832 if (s > (U8*)PL_bostr) {
2835 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2837 } /* XXX could check well-formedness here */
2845 reghopmaybe(U8* s, I32 off)
2849 while (off-- && s < (U8*)PL_regeol)
2856 if (s > (U8*)PL_bostr) {
2859 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2861 } /* XXX could check well-formedness here */