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-1998, 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)
268 if (PL_reg_eval_set) {
269 PL_reg_magic->mg_len = PL_reg_oldpos;
276 - regexec_flags - match a regexp against a string
279 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
280 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
281 /* strend: pointer to null at end of string */
282 /* strbeg: real beginning of string */
283 /* minend: end of match must be >=minend after stringarg. */
284 /* data: May be used for some additional optimizations. */
285 /* nosave: For optimizations. */
290 register char *startpos = stringarg;
292 I32 minlen; /* must match at least this many chars */
293 I32 dontbother = 0; /* how many characters not to try at end */
295 I32 start_shift = 0; /* Offset of the start to find
296 constant substr. */ /* CC */
297 I32 end_shift = 0; /* Same for the end. */ /* CC */
298 I32 scream_pos = -1; /* Internal iterator of scream. */
300 SV* oreplsv = GvSV(PL_replgv);
308 PL_regnarrate = PL_debug & 512;
312 if (prog == NULL || startpos == NULL) {
313 croak("NULL regexp parameter");
317 minlen = prog->minlen;
318 if (strend - startpos < minlen) goto phooey;
320 if (startpos == strbeg) /* is ^ valid at stringarg? */
323 PL_regprev = (U32)stringarg[-1];
324 if (!PL_multiline && PL_regprev == '\n')
325 PL_regprev = '\0'; /* force ^ to NOT match */
328 /* Check validity of program. */
329 if (UCHARAT(prog->program) != REG_MAGIC) {
330 FAIL("corrupted regexp program");
336 if (prog->reganch & ROPT_UTF8)
337 PL_reg_flags |= RF_utf8;
339 /* Mark beginning of line for ^ and lookbehind. */
340 PL_regbol = startpos;
344 /* Mark end of line for $ (and such) */
347 /* see how far we have to get to not match where we matched before */
348 PL_regtill = startpos+minend;
350 /* We start without call_cc context. */
353 /* If there is a "must appear" string, look for it. */
355 if (!(flags & REXEC_CHECKED)
356 && prog->check_substr != Nullsv &&
357 !(prog->reganch & ROPT_ANCH_GPOS) &&
358 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
359 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
362 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
363 /* Should be nonnegative! */
364 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
365 if (flags & REXEC_SCREAM) {
366 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
367 s = screaminstr(sv, prog->check_substr,
368 start_shift + (stringarg - strbeg),
369 end_shift, &scream_pos, 0);
375 s = fbm_instr((unsigned char*)s + start_shift,
376 (unsigned char*)strend - end_shift,
377 prog->check_substr, 0);
379 ++BmUSEFUL(prog->check_substr); /* hooray */
380 goto phooey; /* not present */
382 else if (s - stringarg > prog->check_offset_max &&
384 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
385 : (t = s - prog->check_offset_max) != 0
389 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
392 else if (!(prog->reganch & ROPT_NAUGHTY)
393 && --BmUSEFUL(prog->check_substr) < 0
394 && prog->check_substr == prog->float_substr) { /* boo */
395 SvREFCNT_dec(prog->check_substr);
396 prog->check_substr = Nullsv; /* disable */
397 prog->float_substr = Nullsv; /* clear */
407 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) {
423 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)
424 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
426 PL_reg_ganch = startpos + pos;
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 /* Preserve the current value of $^R */
1015 if (oreplsv != GvSV(PL_replgv)) {
1016 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1017 restored, the value remains
1020 if (PL_reg_eval_set)
1025 if (PL_reg_eval_set)
1031 - regtry - try match at specific point
1033 STATIC I32 /* 0 failure, 1 success */
1034 regtry(regexp *prog, char *startpos)
1042 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1045 PL_reg_eval_set = RS_init;
1047 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1048 PL_stack_sp - PL_stack_base);
1050 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1051 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1052 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1054 /* Apparently this is not needed, judging by wantarray. */
1055 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1056 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1059 /* Make $_ available to executed code. */
1060 if (PL_reg_sv != GvSV(PL_defgv)) {
1061 SAVESPTR(GvSV(PL_defgv));
1062 GvSV(PL_defgv) = PL_reg_sv;
1065 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1066 && (mg = mg_find(PL_reg_sv, 'g')))) {
1067 /* prepare for quick setting of pos */
1068 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1069 mg = mg_find(PL_reg_sv, 'g');
1073 PL_reg_oldpos = mg->mg_len;
1074 SAVEDESTRUCTOR(restore_pos, 0);
1077 PL_reginput = startpos;
1078 PL_regstartp = prog->startp;
1079 PL_regendp = prog->endp;
1080 PL_reglastparen = &prog->lastparen;
1081 prog->lastparen = 0;
1083 if (PL_reg_start_tmpl <= prog->nparens) {
1084 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1085 if(PL_reg_start_tmp)
1086 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1088 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1093 if (prog->nparens) {
1094 for (i = prog->nparens; i >= 0; i--) {
1100 if (regmatch(prog->program + 1)) {
1101 prog->startp[0] = startpos;
1102 prog->endp[0] = PL_reginput;
1110 - regmatch - main matching routine
1112 * Conceptually the strategy is simple: check to see whether the current
1113 * node matches, call self recursively to see whether the rest matches,
1114 * and then act accordingly. In practice we make some effort to avoid
1115 * recursion, in particular by going through "ordinary" nodes (that don't
1116 * need to know whether the rest of the match failed) by a loop instead of
1119 /* [lwall] I've hoisted the register declarations to the outer block in order to
1120 * maybe save a little bit of pushing and popping on the stack. It also takes
1121 * advantage of machines that use a register save mask on subroutine entry.
1123 STATIC I32 /* 0 failure, 1 success */
1124 regmatch(regnode *prog)
1127 register regnode *scan; /* Current node. */
1128 regnode *next; /* Next node. */
1129 regnode *inner; /* Next node in internal branch. */
1130 register I32 nextchr; /* renamed nextchr - nextchar colides with
1131 function of same name */
1132 register I32 n; /* no or next */
1133 register I32 ln; /* len or last */
1134 register char *s; /* operand or save */
1135 register char *locinput = PL_reginput;
1136 register I32 c1, c2, paren; /* case fold search, parenth */
1137 int minmod = 0, sw = 0, logical = 0;
1142 /* Note that nextchr is a byte even in UTF */
1143 nextchr = UCHARAT(locinput);
1145 while (scan != NULL) {
1146 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1148 # define sayYES goto yes
1149 # define sayNO goto no
1150 # define saySAME(x) if (x) goto yes; else goto no
1151 # define REPORT_CODE_OFF 24
1153 # define sayYES return 1
1154 # define sayNO return 0
1155 # define saySAME(x) return x
1158 SV *prop = sv_newmortal();
1159 int docolor = *PL_colors[0];
1160 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1161 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1162 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1163 ? (5 + taill) - l : locinput - PL_bostr);
1164 int pref0_len = pref_len - (locinput - PL_reginput);
1166 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1167 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1168 ? (5 + taill) - pref_len : PL_regeol - locinput);
1171 regprop(prop, scan);
1172 PerlIO_printf(Perl_debug_log,
1173 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1174 locinput - PL_bostr,
1175 PL_colors[4], pref0_len,
1176 locinput - pref_len, PL_colors[5],
1177 PL_colors[2], pref_len - pref0_len,
1178 locinput - pref_len + pref0_len, PL_colors[3],
1179 (docolor ? "" : "> <"),
1180 PL_colors[0], l, locinput, PL_colors[1],
1181 15 - l - pref_len + 1,
1183 scan - PL_regprogram, PL_regindent*2, "",
1187 next = scan + NEXT_OFF(scan);
1193 if (locinput == PL_bostr
1194 ? PL_regprev == '\n'
1196 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1198 /* regtill = regbol; */
1203 if (locinput == PL_bostr
1204 ? PL_regprev == '\n'
1205 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1211 if (locinput == PL_regbol && PL_regprev == '\n')
1215 if (locinput == PL_reg_ganch)
1225 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1230 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1232 if (PL_regeol - locinput > 1)
1236 if (PL_regeol != locinput)
1240 if (nextchr & 0x80) {
1241 locinput += PL_utf8skip[nextchr];
1242 if (locinput > PL_regeol)
1244 nextchr = UCHARAT(locinput);
1247 if (!nextchr && locinput >= PL_regeol)
1249 nextchr = UCHARAT(++locinput);
1252 if (!nextchr && locinput >= PL_regeol)
1254 nextchr = UCHARAT(++locinput);
1257 if (nextchr & 0x80) {
1258 locinput += PL_utf8skip[nextchr];
1259 if (locinput > PL_regeol)
1261 nextchr = UCHARAT(locinput);
1264 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1266 nextchr = UCHARAT(++locinput);
1269 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1271 nextchr = UCHARAT(++locinput);
1274 s = (char *) OPERAND(scan);
1276 /* Inline the first character, for speed. */
1277 if (UCHARAT(s) != nextchr)
1279 if (PL_regeol - locinput < ln)
1281 if (ln > 1 && memNE(s, locinput, ln))
1284 nextchr = UCHARAT(locinput);
1287 PL_reg_flags |= RF_tainted;
1290 s = (char *) OPERAND(scan);
1296 c1 = OP(scan) == EXACTF;
1300 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1301 toLOWER_utf8((U8*)l) :
1302 toLOWER_LC_utf8((U8*)l)))
1310 nextchr = UCHARAT(locinput);
1314 /* Inline the first character, for speed. */
1315 if (UCHARAT(s) != nextchr &&
1316 UCHARAT(s) != ((OP(scan) == EXACTF)
1317 ? PL_fold : PL_fold_locale)[nextchr])
1319 if (PL_regeol - locinput < ln)
1321 if (ln > 1 && (OP(scan) == EXACTF
1322 ? ibcmp(s, locinput, ln)
1323 : ibcmp_locale(s, locinput, ln)))
1326 nextchr = UCHARAT(locinput);
1329 s = (char *) OPERAND(scan);
1330 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1332 if (locinput >= PL_regeol)
1334 locinput += PL_utf8skip[nextchr];
1335 nextchr = UCHARAT(locinput);
1338 s = (char *) OPERAND(scan);
1340 nextchr = UCHARAT(locinput);
1341 if (!REGINCLASS(s, nextchr))
1343 if (!nextchr && locinput >= PL_regeol)
1345 nextchr = UCHARAT(++locinput);
1348 PL_reg_flags |= RF_tainted;
1353 if (!(OP(scan) == ALNUM
1354 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1356 nextchr = UCHARAT(++locinput);
1359 PL_reg_flags |= RF_tainted;
1364 if (nextchr & 0x80) {
1365 if (!(OP(scan) == ALNUMUTF8
1366 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1367 : isALNUM_LC_utf8((U8*)locinput)))
1371 locinput += PL_utf8skip[nextchr];
1372 nextchr = UCHARAT(locinput);
1375 if (!(OP(scan) == ALNUMUTF8
1376 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1378 nextchr = UCHARAT(++locinput);
1381 PL_reg_flags |= RF_tainted;
1384 if (!nextchr && locinput >= PL_regeol)
1386 if (OP(scan) == NALNUM
1387 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1389 nextchr = UCHARAT(++locinput);
1392 PL_reg_flags |= RF_tainted;
1395 if (!nextchr && locinput >= PL_regeol)
1397 if (nextchr & 0x80) {
1398 if (OP(scan) == NALNUMUTF8
1399 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1400 : isALNUM_LC_utf8((U8*)locinput))
1404 locinput += PL_utf8skip[nextchr];
1405 nextchr = UCHARAT(locinput);
1408 if (OP(scan) == NALNUMUTF8
1409 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1411 nextchr = UCHARAT(++locinput);
1415 PL_reg_flags |= RF_tainted;
1419 /* was last char in word? */
1420 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1421 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1423 n = isALNUM(nextchr);
1426 ln = isALNUM_LC(ln);
1427 n = isALNUM_LC(nextchr);
1429 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1434 PL_reg_flags |= RF_tainted;
1438 /* was last char in word? */
1439 ln = (locinput != PL_regbol)
1440 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1441 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1442 ln = isALNUM_uni(ln);
1443 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1446 ln = isALNUM_LC_uni(ln);
1447 n = isALNUM_LC_utf8((U8*)locinput);
1449 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1453 PL_reg_flags |= RF_tainted;
1456 if (!nextchr && locinput >= PL_regeol)
1458 if (!(OP(scan) == SPACE
1459 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1461 nextchr = UCHARAT(++locinput);
1464 PL_reg_flags |= RF_tainted;
1467 if (!nextchr && locinput >= PL_regeol)
1469 if (nextchr & 0x80) {
1470 if (!(OP(scan) == SPACEUTF8
1471 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1472 : isSPACE_LC_utf8((U8*)locinput)))
1476 locinput += PL_utf8skip[nextchr];
1477 nextchr = UCHARAT(locinput);
1480 if (!(OP(scan) == SPACEUTF8
1481 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1483 nextchr = UCHARAT(++locinput);
1486 PL_reg_flags |= RF_tainted;
1491 if (OP(scan) == SPACE
1492 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1494 nextchr = UCHARAT(++locinput);
1497 PL_reg_flags |= RF_tainted;
1502 if (nextchr & 0x80) {
1503 if (OP(scan) == NSPACEUTF8
1504 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1505 : isSPACE_LC_utf8((U8*)locinput))
1509 locinput += PL_utf8skip[nextchr];
1510 nextchr = UCHARAT(locinput);
1513 if (OP(scan) == NSPACEUTF8
1514 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1516 nextchr = UCHARAT(++locinput);
1519 if (!isDIGIT(nextchr))
1521 nextchr = UCHARAT(++locinput);
1524 if (nextchr & 0x80) {
1525 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1527 locinput += PL_utf8skip[nextchr];
1528 nextchr = UCHARAT(locinput);
1531 if (!isDIGIT(nextchr))
1533 nextchr = UCHARAT(++locinput);
1536 if (!nextchr && locinput >= PL_regeol)
1538 if (isDIGIT(nextchr))
1540 nextchr = UCHARAT(++locinput);
1543 if (!nextchr && locinput >= PL_regeol)
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 (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1559 locinput += PL_utf8skip[nextchr];
1560 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1561 locinput += UTF8SKIP(locinput);
1562 if (locinput > PL_regeol)
1564 nextchr = UCHARAT(locinput);
1567 PL_reg_flags |= RF_tainted;
1571 n = ARG(scan); /* which paren pair */
1572 s = PL_regstartp[n];
1573 if (*PL_reglastparen < n || !s)
1574 sayNO; /* Do not match unless seen CLOSEn. */
1575 if (s == PL_regendp[n])
1578 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1580 char *e = PL_regendp[n];
1582 * Note that we can't do the "other character" lookup trick as
1583 * in the 8-bit case (no pun intended) because in Unicode we
1584 * have to map both upper and title case to lower case.
1586 if (OP(scan) == REFF) {
1590 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1600 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1607 nextchr = UCHARAT(locinput);
1611 /* Inline the first character, for speed. */
1612 if (UCHARAT(s) != nextchr &&
1614 (UCHARAT(s) != ((OP(scan) == REFF
1615 ? PL_fold : PL_fold_locale)[nextchr]))))
1617 ln = PL_regendp[n] - s;
1618 if (locinput + ln > PL_regeol)
1620 if (ln > 1 && (OP(scan) == REF
1621 ? memNE(s, locinput, ln)
1623 ? ibcmp(s, locinput, ln)
1624 : ibcmp_locale(s, locinput, ln))))
1627 nextchr = UCHARAT(locinput);
1638 OP_4tree *oop = PL_op;
1639 COP *ocurcop = PL_curcop;
1640 SV **ocurpad = PL_curpad;
1644 PL_op = (OP_4tree*)PL_regdata->data[n];
1645 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1646 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1647 PL_reg_magic->mg_len = locinput - PL_bostr;
1649 CALLRUNOPS(); /* Scalar context. */
1655 PL_curpad = ocurpad;
1656 PL_curcop = ocurcop;
1658 if (logical == 2) { /* Postponed subexpression. */
1660 MAGIC *mg = Null(MAGIC*);
1663 CHECKPOINT cp, lastcp;
1665 if(SvROK(ret) || SvRMAGICAL(ret)) {
1666 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1669 mg = mg_find(sv, 'r');
1672 re = (regexp *)mg->mg_obj;
1673 (void)ReREFCNT_inc(re);
1677 char *t = SvPV(ret, len);
1679 char *oprecomp = PL_regprecomp;
1680 I32 osize = PL_regsize;
1681 I32 onpar = PL_regnpar;
1684 re = CALLREGCOMP(t, t + len, &pm);
1686 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1687 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1688 PL_regprecomp = oprecomp;
1693 PerlIO_printf(Perl_debug_log,
1694 "Entering embedded `%s%.60s%s%s'\n",
1698 (strlen(re->precomp) > 60 ? "..." : ""))
1701 state.prev = PL_reg_call_cc;
1702 state.cc = PL_regcc;
1703 state.re = PL_reg_re;
1709 cp = regcppush(0); /* Save *all* the positions. */
1712 state.ss = PL_savestack_ix;
1713 *PL_reglastparen = 0;
1714 PL_reg_call_cc = &state;
1715 PL_reginput = locinput;
1716 if (regmatch(re->program + 1)) {
1722 PerlIO_printf(Perl_debug_log,
1724 REPORT_CODE_OFF+PL_regindent*2, "")
1729 PL_reg_call_cc = state.prev;
1730 PL_regcc = state.cc;
1731 PL_reg_re = state.re;
1732 cache_re(PL_reg_re);
1739 sv_setsv(save_scalar(PL_replgv), ret);
1743 n = ARG(scan); /* which paren pair */
1744 PL_reg_start_tmp[n] = locinput;
1749 n = ARG(scan); /* which paren pair */
1750 PL_regstartp[n] = PL_reg_start_tmp[n];
1751 PL_regendp[n] = locinput;
1752 if (n > *PL_reglastparen)
1753 *PL_reglastparen = n;
1756 n = ARG(scan); /* which paren pair */
1757 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1761 next = NEXTOPER(NEXTOPER(scan));
1763 next = scan + ARG(scan);
1764 if (OP(next) == IFTHEN) /* Fake one. */
1765 next = NEXTOPER(NEXTOPER(next));
1769 logical = scan->flags;
1773 CHECKPOINT cp = PL_savestack_ix;
1775 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1777 cc.oldcc = PL_regcc;
1779 cc.parenfloor = *PL_reglastparen;
1781 cc.min = ARG1(scan);
1782 cc.max = ARG2(scan);
1783 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1787 PL_reginput = locinput;
1788 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1790 PL_regcc = cc.oldcc;
1796 * This is really hard to understand, because after we match
1797 * what we're trying to match, we must make sure the rest of
1798 * the RE is going to match for sure, and to do that we have
1799 * to go back UP the parse tree by recursing ever deeper. And
1800 * if it fails, we have to reset our parent's current state
1801 * that we can try again after backing off.
1804 CHECKPOINT cp, lastcp;
1805 CURCUR* cc = PL_regcc;
1806 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1808 n = cc->cur + 1; /* how many we know we matched */
1809 PL_reginput = locinput;
1812 PerlIO_printf(Perl_debug_log,
1813 "%*s %ld out of %ld..%ld cc=%lx\n",
1814 REPORT_CODE_OFF+PL_regindent*2, "",
1815 (long)n, (long)cc->min,
1816 (long)cc->max, (long)cc)
1819 /* If degenerate scan matches "", assume scan done. */
1821 if (locinput == cc->lastloc && n >= cc->min) {
1822 PL_regcc = cc->oldcc;
1825 PerlIO_printf(Perl_debug_log,
1826 "%*s empty match detected, try continuation...\n",
1827 REPORT_CODE_OFF+PL_regindent*2, "")
1829 if (regmatch(cc->next))
1832 PerlIO_printf(Perl_debug_log,
1834 REPORT_CODE_OFF+PL_regindent*2, "")
1841 /* First just match a string of min scans. */
1845 cc->lastloc = locinput;
1846 if (regmatch(cc->scan))
1849 cc->lastloc = lastloc;
1851 PerlIO_printf(Perl_debug_log,
1853 REPORT_CODE_OFF+PL_regindent*2, "")
1858 /* Prefer next over scan for minimal matching. */
1861 PL_regcc = cc->oldcc;
1863 cp = regcppush(cc->parenfloor);
1865 if (regmatch(cc->next)) {
1867 sayYES; /* All done. */
1874 if (n >= cc->max) { /* Maximum greed exceeded? */
1875 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1876 && !(PL_reg_flags & RF_warned)) {
1877 PL_reg_flags |= RF_warned;
1878 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1879 "Complex regular subexpression recursion",
1886 PerlIO_printf(Perl_debug_log,
1887 "%*s trying longer...\n",
1888 REPORT_CODE_OFF+PL_regindent*2, "")
1890 /* Try scanning more and see if it helps. */
1891 PL_reginput = locinput;
1893 cc->lastloc = locinput;
1894 cp = regcppush(cc->parenfloor);
1896 if (regmatch(cc->scan)) {
1901 PerlIO_printf(Perl_debug_log,
1903 REPORT_CODE_OFF+PL_regindent*2, "")
1908 cc->lastloc = lastloc;
1912 /* Prefer scan over next for maximal matching. */
1914 if (n < cc->max) { /* More greed allowed? */
1915 cp = regcppush(cc->parenfloor);
1917 cc->lastloc = locinput;
1919 if (regmatch(cc->scan)) {
1924 regcppop(); /* Restore some previous $<digit>s? */
1925 PL_reginput = locinput;
1927 PerlIO_printf(Perl_debug_log,
1928 "%*s failed, try continuation...\n",
1929 REPORT_CODE_OFF+PL_regindent*2, "")
1932 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1933 && !(PL_reg_flags & RF_warned)) {
1934 PL_reg_flags |= RF_warned;
1935 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1936 "Complex regular subexpression recursion",
1940 /* Failed deeper matches of scan, so see if this one works. */
1941 PL_regcc = cc->oldcc;
1943 if (regmatch(cc->next))
1946 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1947 REPORT_CODE_OFF+PL_regindent*2, "")
1952 cc->lastloc = lastloc;
1957 next = scan + ARG(scan);
1960 inner = NEXTOPER(NEXTOPER(scan));
1963 inner = NEXTOPER(scan);
1968 if (OP(next) != c1) /* No choice. */
1969 next = inner; /* Avoid recursion. */
1971 int lastparen = *PL_reglastparen;
1975 PL_reginput = locinput;
1976 if (regmatch(inner))
1979 for (n = *PL_reglastparen; n > lastparen; n--)
1981 *PL_reglastparen = n;
1984 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1988 inner = NEXTOPER(scan);
1989 if (c1 == BRANCHJ) {
1990 inner = NEXTOPER(inner);
1992 } while (scan != NULL && OP(scan) == c1);
2006 /* We suppose that the next guy does not need
2007 backtracking: in particular, it is of constant length,
2008 and has no parenths to influence future backrefs. */
2009 ln = ARG1(scan); /* min to match */
2010 n = ARG2(scan); /* max to match */
2011 paren = scan->flags;
2013 if (paren > PL_regsize)
2015 if (paren > *PL_reglastparen)
2016 *PL_reglastparen = paren;
2018 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2020 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2021 PL_reginput = locinput;
2024 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2026 if (ln && l == 0 && n >= ln
2027 /* In fact, this is tricky. If paren, then the
2028 fact that we did/didnot match may influence
2029 future execution. */
2030 && !(paren && ln == 0))
2032 locinput = PL_reginput;
2033 if (PL_regkind[(U8)OP(next)] == EXACT) {
2034 c1 = UCHARAT(OPERAND(next) + 1);
2035 if (OP(next) == EXACTF)
2037 else if (OP(next) == EXACTFL)
2038 c2 = PL_fold_locale[c1];
2045 /* This may be improved if l == 0. */
2046 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2047 /* If it could work, try it. */
2049 UCHARAT(PL_reginput) == c1 ||
2050 UCHARAT(PL_reginput) == c2)
2054 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2055 PL_regendp[paren] = PL_reginput;
2058 PL_regendp[paren] = NULL;
2064 /* Couldn't or didn't -- move forward. */
2065 PL_reginput = locinput;
2066 if (regrepeat_hard(scan, 1, &l)) {
2068 locinput = PL_reginput;
2075 n = regrepeat_hard(scan, n, &l);
2076 if (n != 0 && l == 0
2077 /* In fact, this is tricky. If paren, then the
2078 fact that we did/didnot match may influence
2079 future execution. */
2080 && !(paren && ln == 0))
2082 locinput = PL_reginput;
2084 PerlIO_printf(Perl_debug_log,
2085 "%*s matched %ld times, len=%ld...\n",
2086 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2089 if (PL_regkind[(U8)OP(next)] == EXACT) {
2090 c1 = UCHARAT(OPERAND(next) + 1);
2091 if (OP(next) == EXACTF)
2093 else if (OP(next) == EXACTFL)
2094 c2 = PL_fold_locale[c1];
2103 /* If it could work, try it. */
2105 UCHARAT(PL_reginput) == c1 ||
2106 UCHARAT(PL_reginput) == c2)
2109 PerlIO_printf(Perl_debug_log,
2110 "%*s trying tail with n=%ld...\n",
2111 REPORT_CODE_OFF+PL_regindent*2, "", n)
2115 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2116 PL_regendp[paren] = PL_reginput;
2119 PL_regendp[paren] = NULL;
2125 /* Couldn't or didn't -- back up. */
2127 locinput = HOPc(locinput, -l);
2128 PL_reginput = locinput;
2135 paren = scan->flags; /* Which paren to set */
2136 if (paren > PL_regsize)
2138 if (paren > *PL_reglastparen)
2139 *PL_reglastparen = paren;
2140 ln = ARG1(scan); /* min to match */
2141 n = ARG2(scan); /* max to match */
2142 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2146 ln = ARG1(scan); /* min to match */
2147 n = ARG2(scan); /* max to match */
2148 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2153 scan = NEXTOPER(scan);
2159 scan = NEXTOPER(scan);
2163 * Lookahead to avoid useless match attempts
2164 * when we know what character comes next.
2166 if (PL_regkind[(U8)OP(next)] == EXACT) {
2167 c1 = UCHARAT(OPERAND(next) + 1);
2168 if (OP(next) == EXACTF)
2170 else if (OP(next) == EXACTFL)
2171 c2 = PL_fold_locale[c1];
2177 PL_reginput = locinput;
2181 if (ln && regrepeat(scan, ln) < ln)
2183 locinput = PL_reginput;
2185 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2186 /* If it could work, try it. */
2188 UCHARAT(PL_reginput) == c1 ||
2189 UCHARAT(PL_reginput) == c2)
2193 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2194 PL_regendp[paren] = PL_reginput;
2197 PL_regendp[paren] = NULL;
2203 /* Couldn't or didn't -- move forward. */
2204 PL_reginput = locinput;
2205 if (regrepeat(scan, 1)) {
2207 locinput = PL_reginput;
2215 n = regrepeat(scan, n);
2216 locinput = PL_reginput;
2217 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2218 (!PL_multiline || OP(next) == SEOL))
2219 ln = n; /* why back off? */
2223 /* If it could work, try it. */
2225 UCHARAT(PL_reginput) == c1 ||
2226 UCHARAT(PL_reginput) == c2)
2230 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2231 PL_regendp[paren] = PL_reginput;
2234 PL_regendp[paren] = NULL;
2240 /* Couldn't or didn't -- back up. */
2242 PL_reginput = locinput = HOPc(locinput, -1);
2247 /* If it could work, try it. */
2249 UCHARAT(PL_reginput) == c1 ||
2250 UCHARAT(PL_reginput) == c2)
2256 /* Couldn't or didn't -- back up. */
2258 PL_reginput = locinput = HOPc(locinput, -1);
2265 if (PL_reg_call_cc) {
2266 re_cc_state *cur_call_cc = PL_reg_call_cc;
2267 CURCUR *cctmp = PL_regcc;
2268 regexp *re = PL_reg_re;
2269 CHECKPOINT cp, lastcp;
2271 cp = regcppush(0); /* Save *all* the positions. */
2273 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2275 PL_reginput = locinput; /* Make position available to
2277 cache_re(PL_reg_call_cc->re);
2278 PL_regcc = PL_reg_call_cc->cc;
2279 PL_reg_call_cc = PL_reg_call_cc->prev;
2280 if (regmatch(cur_call_cc->node)) {
2281 PL_reg_call_cc = cur_call_cc;
2287 PL_reg_call_cc = cur_call_cc;
2293 PerlIO_printf(Perl_debug_log,
2294 "%*s continuation failed...\n",
2295 REPORT_CODE_OFF+PL_regindent*2, "")
2299 if (locinput < PL_regtill)
2300 sayNO; /* Cannot match: too short. */
2303 PL_reginput = locinput; /* put where regtry can find it */
2304 sayYES; /* Success! */
2307 PL_reginput = locinput;
2312 s = HOPMAYBEc(locinput, -scan->flags);
2318 PL_reginput = locinput;
2323 s = HOPMAYBEc(locinput, -scan->flags);
2324 if (!s || s < PL_bostr)
2329 PL_reginput = locinput;
2332 inner = NEXTOPER(NEXTOPER(scan));
2333 if (regmatch(inner) != n) {
2348 if (OP(scan) == SUSPEND) {
2349 locinput = PL_reginput;
2350 nextchr = UCHARAT(locinput);
2355 next = scan + ARG(scan);
2360 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2361 (unsigned long)scan, OP(scan));
2362 FAIL("regexp memory corruption");
2368 * We get here only if there's trouble -- normally "case END" is
2369 * the terminating point.
2371 FAIL("corrupted regexp pointers");
2389 - regrepeat - repeatedly match something simple, report how many
2392 * [This routine now assumes that it will only match on things of length 1.
2393 * That was true before, but now we assume scan - reginput is the count,
2394 * rather than incrementing count on every character. [Er, except utf8.]]
2397 regrepeat(regnode *p, I32 max)
2400 register char *scan;
2401 register char *opnd;
2403 register char *loceol = PL_regeol;
2404 register I32 hardcount = 0;
2407 if (max != REG_INFTY && max < loceol - scan)
2408 loceol = scan + max;
2409 opnd = (char *) OPERAND(p);
2412 while (scan < loceol && *scan != '\n')
2420 while (scan < loceol && *scan != '\n') {
2421 scan += UTF8SKIP(scan);
2427 while (scan < loceol) {
2428 scan += UTF8SKIP(scan);
2432 case EXACT: /* length of string is 1 */
2433 c = UCHARAT(++opnd);
2434 while (scan < loceol && UCHARAT(scan) == c)
2437 case EXACTF: /* length of string is 1 */
2438 c = UCHARAT(++opnd);
2439 while (scan < loceol &&
2440 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2443 case EXACTFL: /* length of string is 1 */
2444 PL_reg_flags |= RF_tainted;
2445 c = UCHARAT(++opnd);
2446 while (scan < loceol &&
2447 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2452 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2453 scan += UTF8SKIP(scan);
2458 while (scan < loceol && REGINCLASS(opnd, *scan))
2462 while (scan < loceol && isALNUM(*scan))
2467 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2468 scan += UTF8SKIP(scan);
2473 PL_reg_flags |= RF_tainted;
2474 while (scan < loceol && isALNUM_LC(*scan))
2478 PL_reg_flags |= RF_tainted;
2480 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2481 scan += UTF8SKIP(scan);
2487 while (scan < loceol && !isALNUM(*scan))
2492 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2493 scan += UTF8SKIP(scan);
2498 PL_reg_flags |= RF_tainted;
2499 while (scan < loceol && !isALNUM_LC(*scan))
2503 PL_reg_flags |= RF_tainted;
2505 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2506 scan += UTF8SKIP(scan);
2511 while (scan < loceol && isSPACE(*scan))
2516 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2517 scan += UTF8SKIP(scan);
2522 PL_reg_flags |= RF_tainted;
2523 while (scan < loceol && isSPACE_LC(*scan))
2527 PL_reg_flags |= RF_tainted;
2529 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2530 scan += UTF8SKIP(scan);
2535 while (scan < loceol && !isSPACE(*scan))
2540 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2541 scan += UTF8SKIP(scan);
2546 PL_reg_flags |= RF_tainted;
2547 while (scan < loceol && !isSPACE_LC(*scan))
2551 PL_reg_flags |= RF_tainted;
2553 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2554 scan += UTF8SKIP(scan);
2559 while (scan < loceol && isDIGIT(*scan))
2564 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2565 scan += UTF8SKIP(scan);
2571 while (scan < loceol && !isDIGIT(*scan))
2576 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2577 scan += UTF8SKIP(scan);
2581 default: /* Called on something of 0 width. */
2582 break; /* So match right here or not at all. */
2588 c = scan - PL_reginput;
2593 SV *prop = sv_newmortal();
2596 PerlIO_printf(Perl_debug_log,
2597 "%*s %s can match %ld times out of %ld...\n",
2598 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2605 - regrepeat_hard - repeatedly match something, report total lenth and length
2607 * The repeater is supposed to have constant length.
2611 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2614 register char *scan;
2615 register char *start;
2616 register char *loceol = PL_regeol;
2618 I32 count = 0, res = 1;
2623 start = PL_reginput;
2625 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2628 while (start < PL_reginput) {
2630 start += UTF8SKIP(start);
2641 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2643 *lp = l = PL_reginput - start;
2644 if (max != REG_INFTY && l*max < loceol - scan)
2645 loceol = scan + l*max;
2658 - regclass - determine if a character falls into a character class
2662 reginclass(register char *p, register I32 c)
2669 if (ANYOF_TEST(p, c))
2671 else if (flags & ANYOF_FOLD) {
2673 if (flags & ANYOF_LOCALE) {
2674 PL_reg_flags |= RF_tainted;
2675 cf = PL_fold_locale[c];
2679 if (ANYOF_TEST(p, cf))
2683 if (!match && (flags & ANYOF_ISA)) {
2684 PL_reg_flags |= RF_tainted;
2686 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2687 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2688 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2689 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2695 return (flags & ANYOF_INVERT) ? !match : match;
2699 reginclassutf8(regnode *f, U8 *p)
2702 char flags = ARG1(f);
2704 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2706 if (swash_fetch(sv, p))
2708 else if (flags & ANYOF_FOLD) {
2711 if (flags & ANYOF_LOCALE) {
2712 PL_reg_flags |= RF_tainted;
2713 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2716 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2717 if (swash_fetch(sv, tmpbuf))
2721 if (!match && (flags & ANYOF_ISA)) {
2722 PL_reg_flags |= RF_tainted;
2724 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2725 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2726 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2727 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2733 return (flags & ANYOF_INVERT) ? !match : match;
2737 reghop(U8 *s, I32 off)
2741 while (off-- && s < (U8*)PL_regeol)
2746 if (s > (U8*)PL_bostr) {
2749 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2751 } /* XXX could check well-formedness here */
2759 reghopmaybe(U8* s, I32 off)
2763 while (off-- && s < (U8*)PL_regeol)
2770 if (s > (U8*)PL_bostr) {
2773 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2775 } /* XXX could check well-formedness here */