5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregexec my_pregexec
44 * pregcomp and pregexec -- regsub and regerror are not used in perl
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
63 **** Alterations to Henry's code are...
65 **** Copyright (c) 1991-1998, Larry Wall
67 **** You may distribute under the terms of either the GNU General Public
68 **** License or the Artistic License, as specified in the README file.
70 * Beware that some of this code is subtly aware of the way operator
71 * precedence is structured in regular expressions. Serious changes in
72 * regular-expression syntax might require a total rethink.
79 #define RF_tainted 1 /* tainted information used? */
80 #define RF_warned 2 /* warned about big count? */
81 #define RF_evaled 4 /* Did an EVAL with setting? */
82 #define RF_utf8 8 /* String contains multibyte chars? */
84 #define UTF (PL_reg_flags & RF_utf8)
86 #define RS_init 1 /* eval environment created */
87 #define RS_set 2 /* replsv value is set */
94 typedef I32 CHECKPOINT;
100 static I32 regmatch _((regnode *prog));
101 static I32 regrepeat _((regnode *p, I32 max));
102 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
103 static I32 regtry _((regexp *prog, char *startpos));
105 static bool reginclass _((char *p, I32 c));
106 static bool reginclassutf8 _((regnode *f, U8* p));
107 static CHECKPOINT regcppush _((I32 parenfloor));
108 static char * regcppop _((void));
109 static char * regcp_set_to _((I32 ss));
110 static void cache_re _((regexp *prog));
111 static void restore_pos _((void *arg));
114 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
115 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
117 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
118 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
121 static U8 * reghop _((U8 *pos, I32 off));
122 static U8 * reghopmaybe _((U8 *pos, I32 off));
124 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
125 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
126 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
127 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
128 #define HOPc(pos,off) ((char*)HOP(pos,off))
129 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
132 regcppush(I32 parenfloor)
135 int retval = PL_savestack_ix;
136 int i = (PL_regsize - parenfloor) * 4;
140 for (p = PL_regsize; p > parenfloor; p--) {
141 SSPUSHPTR(PL_regendp[p]);
142 SSPUSHPTR(PL_regstartp[p]);
143 SSPUSHPTR(PL_reg_start_tmp[p]);
146 SSPUSHINT(PL_regsize);
147 SSPUSHINT(*PL_reglastparen);
148 SSPUSHPTR(PL_reginput);
150 SSPUSHINT(SAVEt_REGCONTEXT);
154 /* These are needed since we do not localize EVAL nodes: */
155 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
156 " Setting an EVAL scope, savestack=%i\n", \
157 PL_savestack_ix)); lastcp = PL_savestack_ix
159 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
160 PerlIO_printf(Perl_debug_log, \
161 " Clearing an EVAL scope, savestack=%i..%i\n", \
162 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
172 assert(i == SAVEt_REGCONTEXT);
174 input = (char *) SSPOPPTR;
175 *PL_reglastparen = SSPOPINT;
176 PL_regsize = SSPOPINT;
177 for (i -= 3; i > 0; i -= 4) {
178 paren = (U32)SSPOPINT;
179 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
180 PL_regstartp[paren] = (char *) SSPOPPTR;
181 tmps = (char*)SSPOPPTR;
182 if (paren <= *PL_reglastparen)
183 PL_regendp[paren] = tmps;
185 PerlIO_printf(Perl_debug_log,
186 " restoring \\%d to %d(%d)..%d%s\n",
187 paren, PL_regstartp[paren] - PL_regbol,
188 PL_reg_start_tmp[paren] - PL_regbol,
189 PL_regendp[paren] - PL_regbol,
190 (paren > *PL_reglastparen ? "(no)" : ""));
194 if (*PL_reglastparen + 1 <= PL_regnpar) {
195 PerlIO_printf(Perl_debug_log,
196 " restoring \\%d..\\%d to undef\n",
197 *PL_reglastparen + 1, PL_regnpar);
200 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
201 if (paren > PL_regsize)
202 PL_regstartp[paren] = Nullch;
203 PL_regendp[paren] = Nullch;
212 I32 tmp = PL_savestack_ix;
214 PL_savestack_ix = ss;
216 PL_savestack_ix = tmp;
220 typedef struct re_cc_state
224 struct re_cc_state *prev;
229 #define regcpblow(cp) LEAVE_SCOPE(cp)
232 * pregexec and friends
236 - pregexec - match a regexp against a string
239 pregexec(register regexp *prog, char *stringarg, register char *strend,
240 char *strbeg, I32 minend, SV *screamer, U32 nosave)
241 /* strend: pointer to null at end of string */
242 /* strbeg: real beginning of string */
243 /* minend: end of match must be >=minend after stringarg. */
244 /* nosave: For optimizations. */
247 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
248 nosave ? 0 : REXEC_COPY_STR);
252 cache_re(regexp *prog)
255 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
257 PL_regprogram = prog->program;
259 PL_regnpar = prog->nparens;
260 PL_regdata = prog->data;
265 restore_pos(void *arg)
267 if (PL_reg_eval_set) {
268 PL_reg_magic->mg_len = PL_reg_oldpos;
275 - regexec_flags - match a regexp against a string
278 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
279 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
280 /* strend: pointer to null at end of string */
281 /* strbeg: real beginning of string */
282 /* minend: end of match must be >=minend after stringarg. */
283 /* data: May be used for some additional optimizations. */
284 /* nosave: For optimizations. */
289 register char *startpos = stringarg;
291 I32 minlen; /* must match at least this many chars */
292 I32 dontbother = 0; /* how many characters not to try at end */
294 I32 start_shift = 0; /* Offset of the start to find
295 constant substr. */ /* CC */
296 I32 end_shift = 0; /* Same for the end. */ /* CC */
297 I32 scream_pos = -1; /* Internal iterator of scream. */
299 SV* oreplsv = GvSV(PL_replgv);
307 PL_regnarrate = PL_debug & 512;
311 if (prog == NULL || startpos == NULL) {
312 croak("NULL regexp parameter");
316 minlen = prog->minlen;
317 if (strend - startpos < minlen) goto phooey;
319 if (startpos == strbeg) /* is ^ valid at stringarg? */
322 PL_regprev = (U32)stringarg[-1];
323 if (!PL_multiline && PL_regprev == '\n')
324 PL_regprev = '\0'; /* force ^ to NOT match */
327 /* Check validity of program. */
328 if (UCHARAT(prog->program) != REG_MAGIC) {
329 FAIL("corrupted regexp program");
335 if (prog->reganch & ROPT_UTF8)
336 PL_reg_flags |= RF_utf8;
338 /* Mark beginning of line for ^ and lookbehind. */
339 PL_regbol = startpos;
343 /* Mark end of line for $ (and such) */
346 /* see how far we have to get to not match where we matched before */
347 PL_regtill = startpos+minend;
349 /* We start without call_cc context. */
352 /* If there is a "must appear" string, look for it. */
354 if (!(flags & REXEC_CHECKED)
355 && prog->check_substr != Nullsv &&
356 !(prog->reganch & ROPT_ANCH_GPOS) &&
357 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
358 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
361 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
362 /* Should be nonnegative! */
363 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
364 if (flags & REXEC_SCREAM) {
365 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
366 s = screaminstr(sv, prog->check_substr,
367 start_shift + (stringarg - strbeg),
368 end_shift, &scream_pos, 0);
374 s = fbm_instr((unsigned char*)s + start_shift,
375 (unsigned char*)strend - end_shift,
376 prog->check_substr, 0);
378 ++BmUSEFUL(prog->check_substr); /* hooray */
379 goto phooey; /* not present */
381 else if (s - stringarg > prog->check_offset_max &&
383 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
384 : (t = s - prog->check_offset_max) != 0
388 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
391 else if (!(prog->reganch & ROPT_NAUGHTY)
392 && --BmUSEFUL(prog->check_substr) < 0
393 && prog->check_substr == prog->float_substr) { /* boo */
394 SvREFCNT_dec(prog->check_substr);
395 prog->check_substr = Nullsv; /* disable */
396 prog->float_substr = Nullsv; /* clear */
404 PerlIO_printf(Perl_debug_log,
405 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
406 PL_colors[4],PL_colors[5],PL_colors[0],
409 (strlen(prog->precomp) > 60 ? "..." : ""),
411 (strend - startpos > 60 ? 60 : strend - startpos),
412 startpos, PL_colors[1],
413 (strend - startpos > 60 ? "..." : ""))
416 if (prog->reganch & ROPT_GPOS_SEEN) {
420 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)
421 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
423 PL_reg_ganch = startpos + pos;
426 /* Simplest case: anchored match need be tried only once. */
427 /* [unless only anchor is BOL and multiline is set] */
428 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
429 if (regtry(prog, startpos))
431 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
432 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
435 dontbother = minlen - 1;
436 strend = HOPc(strend, -dontbother);
437 /* for multiline we only have to try after newlines */
441 if (*s++ == '\n') { /* don't need PL_utf8skip here */
442 if (s < strend && regtry(prog, s))
448 } else if (prog->reganch & ROPT_ANCH_GPOS) {
449 if (regtry(prog, PL_reg_ganch))
454 /* Messy cases: unanchored match. */
455 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
456 /* we have /x+whatever/ */
457 /* it must be a one character string */
458 char ch = SvPVX(prog->anchored_substr)[0];
462 if (regtry(prog, s)) goto got_it;
464 while (s < strend && *s == ch)
473 if (regtry(prog, s)) goto got_it;
475 while (s < strend && *s == ch)
483 else if (prog->anchored_substr != Nullsv
484 || (prog->float_substr != Nullsv
485 && prog->float_max_offset < strend - s)) {
486 SV *must = prog->anchored_substr
487 ? prog->anchored_substr : prog->float_substr;
489 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
491 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
492 I32 delta = back_max - back_min;
493 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
494 char *last1; /* Last position checked before */
499 last1 = s - 1; /* bogus */
501 /* XXXX check_substr already used to find `s', can optimize if
502 check_substr==must. */
504 dontbother = end_shift;
505 strend = HOPc(strend, -dontbother);
506 while ( (s <= last) &&
507 ((flags & REXEC_SCREAM)
508 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
509 end_shift, &scream_pos, 0))
510 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
511 (unsigned char*)strend, must, 0))) ) {
512 if (HOPc(s, -back_max) > last1) {
513 last1 = HOPc(s, -back_min);
514 s = HOPc(s, -back_max);
517 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
519 last1 = HOPc(s, -back_min);
539 else if (c = prog->regstclass) {
540 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
544 dontbother = minlen - 1;
545 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
547 /* We know what class it must start with. */
550 cc = (char *) OPERAND(c);
552 if (REGINCLASSUTF8(c, (U8*)s)) {
553 if (tmp && regtry(prog, s))
564 cc = (char *) OPERAND(c);
566 if (REGINCLASS(cc, *s)) {
567 if (tmp && regtry(prog, s))
578 PL_reg_flags |= RF_tainted;
585 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
586 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
588 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
595 if ((minlen || tmp) && regtry(prog,s))
599 PL_reg_flags |= RF_tainted;
604 strend = reghop_c(strend, -1);
606 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
607 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
609 if (tmp == !(OP(c) == BOUND ?
610 swash_fetch(PL_utf8_alnum, (U8*)s) :
611 isALNUM_LC_utf8((U8*)s)))
619 if ((minlen || tmp) && regtry(prog,s))
623 PL_reg_flags |= RF_tainted;
630 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
631 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
633 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
635 else if (regtry(prog, s))
639 if ((minlen || !tmp) && regtry(prog,s))
643 PL_reg_flags |= RF_tainted;
648 strend = reghop_c(strend, -1);
650 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
651 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
653 if (tmp == !(OP(c) == NBOUND ?
654 swash_fetch(PL_utf8_alnum, (U8*)s) :
655 isALNUM_LC_utf8((U8*)s)))
657 else if (regtry(prog, s))
661 if ((minlen || !tmp) && regtry(prog,s))
667 if (tmp && regtry(prog, s))
679 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
680 if (tmp && regtry(prog, s))
691 PL_reg_flags |= RF_tainted;
693 if (isALNUM_LC(*s)) {
694 if (tmp && regtry(prog, s))
705 PL_reg_flags |= RF_tainted;
707 if (isALNUM_LC_utf8((U8*)s)) {
708 if (tmp && regtry(prog, s))
721 if (tmp && regtry(prog, s))
733 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
734 if (tmp && regtry(prog, s))
745 PL_reg_flags |= RF_tainted;
747 if (!isALNUM_LC(*s)) {
748 if (tmp && regtry(prog, s))
759 PL_reg_flags |= RF_tainted;
761 if (!isALNUM_LC_utf8((U8*)s)) {
762 if (tmp && regtry(prog, s))
775 if (tmp && regtry(prog, s))
787 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
788 if (tmp && regtry(prog, s))
799 PL_reg_flags |= RF_tainted;
801 if (isSPACE_LC(*s)) {
802 if (tmp && regtry(prog, s))
813 PL_reg_flags |= RF_tainted;
815 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
816 if (tmp && regtry(prog, s))
829 if (tmp && regtry(prog, s))
841 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
842 if (tmp && regtry(prog, s))
853 PL_reg_flags |= RF_tainted;
855 if (!isSPACE_LC(*s)) {
856 if (tmp && regtry(prog, s))
867 PL_reg_flags |= RF_tainted;
869 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
870 if (tmp && regtry(prog, s))
883 if (tmp && regtry(prog, s))
895 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
896 if (tmp && regtry(prog, s))
909 if (tmp && regtry(prog, s))
921 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
922 if (tmp && regtry(prog, s))
936 if (prog->float_substr != Nullsv) { /* Trim the end. */
938 I32 oldpos = scream_pos;
940 if (flags & REXEC_SCREAM) {
941 last = screaminstr(sv, prog->float_substr, s - strbeg,
942 end_shift, &scream_pos, 1); /* last one */
944 last = scream_olds; /* Only one occurence. */
949 char *little = SvPV(prog->float_substr, len);
951 last = rninstr(s, strend, little, little + len);
953 last = strend; /* matching `$' */
955 if (last == NULL) goto phooey; /* Should not happen! */
956 dontbother = strend - last + prog->float_min_offset;
958 if (minlen && (dontbother < minlen))
959 dontbother = minlen - 1;
960 strend -= dontbother; /* this one's always in bytes! */
961 /* We don't know much -- general case. */
975 } while (s++ < strend);
983 prog->subbeg = strbeg;
984 prog->subend = PL_regeol; /* strend may have been modified */
985 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
987 /* make sure $`, $&, $', and $digit will work later */
988 if (strbeg != prog->subbase) { /* second+ //g match. */
989 if (!(flags & REXEC_COPY_STR)) {
991 Safefree(prog->subbase);
992 prog->subbase = Nullch;
996 I32 i = PL_regeol - startpos + (stringarg - strbeg);
997 s = savepvn(strbeg, i);
998 Safefree(prog->subbase);
1000 prog->subbeg = prog->subbase;
1001 prog->subend = prog->subbase + i;
1002 s = prog->subbase + (stringarg - strbeg);
1003 for (i = 0; i <= prog->nparens; i++) {
1004 if (prog->endp[i]) {
1005 prog->startp[i] = s + (prog->startp[i] - startpos);
1006 prog->endp[i] = s + (prog->endp[i] - startpos);
1011 /* Preserve the current value of $^R */
1012 if (oreplsv != GvSV(PL_replgv)) {
1013 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1014 restored, the value remains
1017 if (PL_reg_eval_set)
1022 if (PL_reg_eval_set)
1028 - regtry - try match at specific point
1030 STATIC I32 /* 0 failure, 1 success */
1031 regtry(regexp *prog, char *startpos)
1039 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1042 PL_reg_eval_set = RS_init;
1044 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1045 PL_stack_sp - PL_stack_base);
1047 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1048 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1049 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1051 /* Apparently this is not needed, judging by wantarray. */
1052 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1053 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1056 /* Make $_ available to executed code. */
1057 if (PL_reg_sv != GvSV(PL_defgv)) {
1058 SAVESPTR(GvSV(PL_defgv));
1059 GvSV(PL_defgv) = PL_reg_sv;
1062 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1063 && (mg = mg_find(PL_reg_sv, 'g')))) {
1064 /* prepare for quick setting of pos */
1065 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1066 mg = mg_find(PL_reg_sv, 'g');
1070 PL_reg_oldpos = mg->mg_len;
1071 SAVEDESTRUCTOR(restore_pos, 0);
1074 PL_reginput = startpos;
1075 PL_regstartp = prog->startp;
1076 PL_regendp = prog->endp;
1077 PL_reglastparen = &prog->lastparen;
1078 prog->lastparen = 0;
1080 if (PL_reg_start_tmpl <= prog->nparens) {
1081 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1082 if(PL_reg_start_tmp)
1083 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1085 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1090 if (prog->nparens) {
1091 for (i = prog->nparens; i >= 0; i--) {
1097 if (regmatch(prog->program + 1)) {
1098 prog->startp[0] = startpos;
1099 prog->endp[0] = PL_reginput;
1107 - regmatch - main matching routine
1109 * Conceptually the strategy is simple: check to see whether the current
1110 * node matches, call self recursively to see whether the rest matches,
1111 * and then act accordingly. In practice we make some effort to avoid
1112 * recursion, in particular by going through "ordinary" nodes (that don't
1113 * need to know whether the rest of the match failed) by a loop instead of
1116 /* [lwall] I've hoisted the register declarations to the outer block in order to
1117 * maybe save a little bit of pushing and popping on the stack. It also takes
1118 * advantage of machines that use a register save mask on subroutine entry.
1120 STATIC I32 /* 0 failure, 1 success */
1121 regmatch(regnode *prog)
1124 register regnode *scan; /* Current node. */
1125 regnode *next; /* Next node. */
1126 regnode *inner; /* Next node in internal branch. */
1127 register I32 nextchr; /* renamed nextchr - nextchar colides with
1128 function of same name */
1129 register I32 n; /* no or next */
1130 register I32 ln; /* len or last */
1131 register char *s; /* operand or save */
1132 register char *locinput = PL_reginput;
1133 register I32 c1, c2, paren; /* case fold search, parenth */
1134 int minmod = 0, sw = 0, logical = 0;
1139 /* Note that nextchr is a byte even in UTF */
1140 nextchr = UCHARAT(locinput);
1142 while (scan != NULL) {
1143 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1145 # define sayYES goto yes
1146 # define sayNO goto no
1147 # define saySAME(x) if (x) goto yes; else goto no
1148 # define REPORT_CODE_OFF 24
1150 # define sayYES return 1
1151 # define sayNO return 0
1152 # define saySAME(x) return x
1155 SV *prop = sv_newmortal();
1156 int docolor = *PL_colors[0];
1157 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1158 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1159 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1160 ? (5 + taill) - l : locinput - PL_bostr);
1161 int pref0_len = pref_len - (locinput - PL_reginput);
1163 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1164 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1165 ? (5 + taill) - pref_len : PL_regeol - locinput);
1168 regprop(prop, scan);
1169 PerlIO_printf(Perl_debug_log,
1170 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1171 locinput - PL_bostr,
1172 PL_colors[4], pref0_len,
1173 locinput - pref_len, PL_colors[5],
1174 PL_colors[2], pref_len - pref0_len,
1175 locinput - pref_len + pref0_len, PL_colors[3],
1176 (docolor ? "" : "> <"),
1177 PL_colors[0], l, locinput, PL_colors[1],
1178 15 - l - pref_len + 1,
1180 scan - PL_regprogram, PL_regindent*2, "",
1184 next = scan + NEXT_OFF(scan);
1190 if (locinput == PL_bostr
1191 ? PL_regprev == '\n'
1193 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1195 /* regtill = regbol; */
1200 if (locinput == PL_bostr
1201 ? PL_regprev == '\n'
1202 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1208 if (locinput == PL_regbol && PL_regprev == '\n')
1212 if (locinput == PL_reg_ganch)
1222 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1227 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1229 if (PL_regeol - locinput > 1)
1233 if (PL_regeol != locinput)
1237 if (nextchr & 0x80) {
1238 locinput += PL_utf8skip[nextchr];
1239 if (locinput > PL_regeol)
1241 nextchr = UCHARAT(locinput);
1244 if (!nextchr && locinput >= PL_regeol)
1246 nextchr = UCHARAT(++locinput);
1249 if (!nextchr && locinput >= PL_regeol)
1251 nextchr = UCHARAT(++locinput);
1254 if (nextchr & 0x80) {
1255 locinput += PL_utf8skip[nextchr];
1256 if (locinput > PL_regeol)
1258 nextchr = UCHARAT(locinput);
1261 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1263 nextchr = UCHARAT(++locinput);
1266 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1268 nextchr = UCHARAT(++locinput);
1271 s = (char *) OPERAND(scan);
1273 /* Inline the first character, for speed. */
1274 if (UCHARAT(s) != nextchr)
1276 if (PL_regeol - locinput < ln)
1278 if (ln > 1 && memNE(s, locinput, ln))
1281 nextchr = UCHARAT(locinput);
1284 PL_reg_flags |= RF_tainted;
1287 s = (char *) OPERAND(scan);
1293 c1 = OP(scan) == EXACTF;
1297 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1298 toLOWER_utf8((U8*)l) :
1299 toLOWER_LC_utf8((U8*)l)))
1307 nextchr = UCHARAT(locinput);
1311 /* Inline the first character, for speed. */
1312 if (UCHARAT(s) != nextchr &&
1313 UCHARAT(s) != ((OP(scan) == EXACTF)
1314 ? PL_fold : PL_fold_locale)[nextchr])
1316 if (PL_regeol - locinput < ln)
1318 if (ln > 1 && (OP(scan) == EXACTF
1319 ? ibcmp(s, locinput, ln)
1320 : ibcmp_locale(s, locinput, ln)))
1323 nextchr = UCHARAT(locinput);
1326 s = (char *) OPERAND(scan);
1327 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1329 if (locinput >= PL_regeol)
1331 locinput += PL_utf8skip[nextchr];
1332 nextchr = UCHARAT(locinput);
1335 s = (char *) OPERAND(scan);
1337 nextchr = UCHARAT(locinput);
1338 if (!REGINCLASS(s, nextchr))
1340 if (!nextchr && locinput >= PL_regeol)
1342 nextchr = UCHARAT(++locinput);
1345 PL_reg_flags |= RF_tainted;
1350 if (!(OP(scan) == ALNUM
1351 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1353 nextchr = UCHARAT(++locinput);
1356 PL_reg_flags |= RF_tainted;
1361 if (nextchr & 0x80) {
1362 if (!(OP(scan) == ALNUMUTF8
1363 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1364 : isALNUM_LC_utf8((U8*)locinput)))
1368 locinput += PL_utf8skip[nextchr];
1369 nextchr = UCHARAT(locinput);
1372 if (!(OP(scan) == ALNUMUTF8
1373 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1375 nextchr = UCHARAT(++locinput);
1378 PL_reg_flags |= RF_tainted;
1381 if (!nextchr && locinput >= PL_regeol)
1383 if (OP(scan) == NALNUM
1384 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1386 nextchr = UCHARAT(++locinput);
1389 PL_reg_flags |= RF_tainted;
1392 if (!nextchr && locinput >= PL_regeol)
1394 if (nextchr & 0x80) {
1395 if (OP(scan) == NALNUMUTF8
1396 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1397 : isALNUM_LC_utf8((U8*)locinput))
1401 locinput += PL_utf8skip[nextchr];
1402 nextchr = UCHARAT(locinput);
1405 if (OP(scan) == NALNUMUTF8
1406 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1408 nextchr = UCHARAT(++locinput);
1412 PL_reg_flags |= RF_tainted;
1416 /* was last char in word? */
1417 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1418 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1420 n = isALNUM(nextchr);
1423 ln = isALNUM_LC(ln);
1424 n = isALNUM_LC(nextchr);
1426 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1431 PL_reg_flags |= RF_tainted;
1435 /* was last char in word? */
1436 ln = (locinput != PL_regbol)
1437 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1438 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1439 ln = isALNUM_uni(ln);
1440 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1443 ln = isALNUM_LC_uni(ln);
1444 n = isALNUM_LC_utf8((U8*)locinput);
1446 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1450 PL_reg_flags |= RF_tainted;
1453 if (!nextchr && locinput >= PL_regeol)
1455 if (!(OP(scan) == SPACE
1456 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1458 nextchr = UCHARAT(++locinput);
1461 PL_reg_flags |= RF_tainted;
1464 if (!nextchr && locinput >= PL_regeol)
1466 if (nextchr & 0x80) {
1467 if (!(OP(scan) == SPACEUTF8
1468 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1469 : isSPACE_LC_utf8((U8*)locinput)))
1473 locinput += PL_utf8skip[nextchr];
1474 nextchr = UCHARAT(locinput);
1477 if (!(OP(scan) == SPACEUTF8
1478 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1480 nextchr = UCHARAT(++locinput);
1483 PL_reg_flags |= RF_tainted;
1488 if (OP(scan) == SPACE
1489 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1491 nextchr = UCHARAT(++locinput);
1494 PL_reg_flags |= RF_tainted;
1499 if (nextchr & 0x80) {
1500 if (OP(scan) == NSPACEUTF8
1501 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1502 : isSPACE_LC_utf8((U8*)locinput))
1506 locinput += PL_utf8skip[nextchr];
1507 nextchr = UCHARAT(locinput);
1510 if (OP(scan) == NSPACEUTF8
1511 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1513 nextchr = UCHARAT(++locinput);
1516 if (!isDIGIT(nextchr))
1518 nextchr = UCHARAT(++locinput);
1521 if (nextchr & 0x80) {
1522 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1524 locinput += PL_utf8skip[nextchr];
1525 nextchr = UCHARAT(locinput);
1528 if (!isDIGIT(nextchr))
1530 nextchr = UCHARAT(++locinput);
1533 if (!nextchr && locinput >= PL_regeol)
1535 if (isDIGIT(nextchr))
1537 nextchr = UCHARAT(++locinput);
1540 if (!nextchr && locinput >= PL_regeol)
1542 if (nextchr & 0x80) {
1543 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1545 locinput += PL_utf8skip[nextchr];
1546 nextchr = UCHARAT(locinput);
1549 if (isDIGIT(nextchr))
1551 nextchr = UCHARAT(++locinput);
1554 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1556 locinput += PL_utf8skip[nextchr];
1557 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1558 locinput += UTF8SKIP(locinput);
1559 if (locinput > PL_regeol)
1561 nextchr = UCHARAT(locinput);
1564 PL_reg_flags |= RF_tainted;
1568 n = ARG(scan); /* which paren pair */
1569 s = PL_regstartp[n];
1570 if (*PL_reglastparen < n || !s)
1571 sayNO; /* Do not match unless seen CLOSEn. */
1572 if (s == PL_regendp[n])
1575 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1577 char *e = PL_regendp[n];
1579 * Note that we can't do the "other character" lookup trick as
1580 * in the 8-bit case (no pun intended) because in Unicode we
1581 * have to map both upper and title case to lower case.
1583 if (OP(scan) == REFF) {
1587 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1597 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1604 nextchr = UCHARAT(locinput);
1608 /* Inline the first character, for speed. */
1609 if (UCHARAT(s) != nextchr &&
1611 (UCHARAT(s) != ((OP(scan) == REFF
1612 ? PL_fold : PL_fold_locale)[nextchr]))))
1614 ln = PL_regendp[n] - s;
1615 if (locinput + ln > PL_regeol)
1617 if (ln > 1 && (OP(scan) == REF
1618 ? memNE(s, locinput, ln)
1620 ? ibcmp(s, locinput, ln)
1621 : ibcmp_locale(s, locinput, ln))))
1624 nextchr = UCHARAT(locinput);
1635 OP_4tree *oop = PL_op;
1636 COP *ocurcop = PL_curcop;
1637 SV **ocurpad = PL_curpad;
1641 PL_op = (OP_4tree*)PL_regdata->data[n];
1642 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1643 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1644 PL_reg_magic->mg_len = locinput - PL_bostr;
1646 CALLRUNOPS(); /* Scalar context. */
1652 PL_curpad = ocurpad;
1653 PL_curcop = ocurcop;
1655 if (logical == 2) { /* Postponed subexpression. */
1657 MAGIC *mg = Null(MAGIC*);
1660 CHECKPOINT cp, lastcp;
1662 if(SvROK(ret) || SvRMAGICAL(ret)) {
1663 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1666 mg = mg_find(sv, 'r');
1669 re = (regexp *)mg->mg_obj;
1670 (void)ReREFCNT_inc(re);
1674 char *t = SvPV(ret, len);
1676 char *oprecomp = PL_regprecomp;
1677 I32 osize = PL_regsize;
1678 I32 onpar = PL_regnpar;
1681 re = CALLREGCOMP(t, t + len, &pm);
1683 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1684 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1685 PL_regprecomp = oprecomp;
1690 PerlIO_printf(Perl_debug_log,
1691 "Entering embedded `%s%.60s%s%s'\n",
1695 (strlen(re->precomp) > 60 ? "..." : ""))
1698 state.prev = PL_reg_call_cc;
1699 state.cc = PL_regcc;
1700 state.re = PL_reg_re;
1706 cp = regcppush(0); /* Save *all* the positions. */
1709 state.ss = PL_savestack_ix;
1710 *PL_reglastparen = 0;
1711 PL_reg_call_cc = &state;
1712 PL_reginput = locinput;
1713 if (regmatch(re->program + 1)) {
1719 PerlIO_printf(Perl_debug_log,
1721 REPORT_CODE_OFF+PL_regindent*2, "")
1726 PL_reg_call_cc = state.prev;
1727 PL_regcc = state.cc;
1728 PL_reg_re = state.re;
1729 cache_re(PL_reg_re);
1736 sv_setsv(save_scalar(PL_replgv), ret);
1740 n = ARG(scan); /* which paren pair */
1741 PL_reg_start_tmp[n] = locinput;
1746 n = ARG(scan); /* which paren pair */
1747 PL_regstartp[n] = PL_reg_start_tmp[n];
1748 PL_regendp[n] = locinput;
1749 if (n > *PL_reglastparen)
1750 *PL_reglastparen = n;
1753 n = ARG(scan); /* which paren pair */
1754 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1758 next = NEXTOPER(NEXTOPER(scan));
1760 next = scan + ARG(scan);
1761 if (OP(next) == IFTHEN) /* Fake one. */
1762 next = NEXTOPER(NEXTOPER(next));
1766 logical = scan->flags;
1770 CHECKPOINT cp = PL_savestack_ix;
1772 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1774 cc.oldcc = PL_regcc;
1776 cc.parenfloor = *PL_reglastparen;
1778 cc.min = ARG1(scan);
1779 cc.max = ARG2(scan);
1780 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1784 PL_reginput = locinput;
1785 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1787 PL_regcc = cc.oldcc;
1793 * This is really hard to understand, because after we match
1794 * what we're trying to match, we must make sure the rest of
1795 * the RE is going to match for sure, and to do that we have
1796 * to go back UP the parse tree by recursing ever deeper. And
1797 * if it fails, we have to reset our parent's current state
1798 * that we can try again after backing off.
1801 CHECKPOINT cp, lastcp;
1802 CURCUR* cc = PL_regcc;
1803 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1805 n = cc->cur + 1; /* how many we know we matched */
1806 PL_reginput = locinput;
1809 PerlIO_printf(Perl_debug_log,
1810 "%*s %ld out of %ld..%ld cc=%lx\n",
1811 REPORT_CODE_OFF+PL_regindent*2, "",
1812 (long)n, (long)cc->min,
1813 (long)cc->max, (long)cc)
1816 /* If degenerate scan matches "", assume scan done. */
1818 if (locinput == cc->lastloc && n >= cc->min) {
1819 PL_regcc = cc->oldcc;
1822 PerlIO_printf(Perl_debug_log,
1823 "%*s empty match detected, try continuation...\n",
1824 REPORT_CODE_OFF+PL_regindent*2, "")
1826 if (regmatch(cc->next))
1829 PerlIO_printf(Perl_debug_log,
1831 REPORT_CODE_OFF+PL_regindent*2, "")
1838 /* First just match a string of min scans. */
1842 cc->lastloc = locinput;
1843 if (regmatch(cc->scan))
1846 cc->lastloc = lastloc;
1848 PerlIO_printf(Perl_debug_log,
1850 REPORT_CODE_OFF+PL_regindent*2, "")
1855 /* Prefer next over scan for minimal matching. */
1858 PL_regcc = cc->oldcc;
1860 cp = regcppush(cc->parenfloor);
1862 if (regmatch(cc->next)) {
1864 sayYES; /* All done. */
1871 if (n >= cc->max) { /* Maximum greed exceeded? */
1872 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1873 && !(PL_reg_flags & RF_warned)) {
1874 PL_reg_flags |= RF_warned;
1875 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1876 "Complex regular subexpression recursion",
1883 PerlIO_printf(Perl_debug_log,
1884 "%*s trying longer...\n",
1885 REPORT_CODE_OFF+PL_regindent*2, "")
1887 /* Try scanning more and see if it helps. */
1888 PL_reginput = locinput;
1890 cc->lastloc = locinput;
1891 cp = regcppush(cc->parenfloor);
1893 if (regmatch(cc->scan)) {
1898 PerlIO_printf(Perl_debug_log,
1900 REPORT_CODE_OFF+PL_regindent*2, "")
1905 cc->lastloc = lastloc;
1909 /* Prefer scan over next for maximal matching. */
1911 if (n < cc->max) { /* More greed allowed? */
1912 cp = regcppush(cc->parenfloor);
1914 cc->lastloc = locinput;
1916 if (regmatch(cc->scan)) {
1921 regcppop(); /* Restore some previous $<digit>s? */
1922 PL_reginput = locinput;
1924 PerlIO_printf(Perl_debug_log,
1925 "%*s failed, try continuation...\n",
1926 REPORT_CODE_OFF+PL_regindent*2, "")
1929 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1930 && !(PL_reg_flags & RF_warned)) {
1931 PL_reg_flags |= RF_warned;
1932 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1933 "Complex regular subexpression recursion",
1937 /* Failed deeper matches of scan, so see if this one works. */
1938 PL_regcc = cc->oldcc;
1940 if (regmatch(cc->next))
1943 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1944 REPORT_CODE_OFF+PL_regindent*2, "")
1949 cc->lastloc = lastloc;
1954 next = scan + ARG(scan);
1957 inner = NEXTOPER(NEXTOPER(scan));
1960 inner = NEXTOPER(scan);
1965 if (OP(next) != c1) /* No choice. */
1966 next = inner; /* Avoid recursion. */
1968 int lastparen = *PL_reglastparen;
1972 PL_reginput = locinput;
1973 if (regmatch(inner))
1976 for (n = *PL_reglastparen; n > lastparen; n--)
1978 *PL_reglastparen = n;
1981 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1985 inner = NEXTOPER(scan);
1986 if (c1 == BRANCHJ) {
1987 inner = NEXTOPER(inner);
1989 } while (scan != NULL && OP(scan) == c1);
2003 /* We suppose that the next guy does not need
2004 backtracking: in particular, it is of constant length,
2005 and has no parenths to influence future backrefs. */
2006 ln = ARG1(scan); /* min to match */
2007 n = ARG2(scan); /* max to match */
2008 paren = scan->flags;
2010 if (paren > PL_regsize)
2012 if (paren > *PL_reglastparen)
2013 *PL_reglastparen = paren;
2015 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2017 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2018 PL_reginput = locinput;
2021 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2023 if (ln && l == 0 && n >= ln
2024 /* In fact, this is tricky. If paren, then the
2025 fact that we did/didnot match may influence
2026 future execution. */
2027 && !(paren && ln == 0))
2029 locinput = PL_reginput;
2030 if (PL_regkind[(U8)OP(next)] == EXACT) {
2031 c1 = UCHARAT(OPERAND(next) + 1);
2032 if (OP(next) == EXACTF)
2034 else if (OP(next) == EXACTFL)
2035 c2 = PL_fold_locale[c1];
2042 /* This may be improved if l == 0. */
2043 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2044 /* If it could work, try it. */
2046 UCHARAT(PL_reginput) == c1 ||
2047 UCHARAT(PL_reginput) == c2)
2051 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2052 PL_regendp[paren] = PL_reginput;
2055 PL_regendp[paren] = NULL;
2061 /* Couldn't or didn't -- move forward. */
2062 PL_reginput = locinput;
2063 if (regrepeat_hard(scan, 1, &l)) {
2065 locinput = PL_reginput;
2072 n = regrepeat_hard(scan, n, &l);
2073 if (n != 0 && l == 0
2074 /* In fact, this is tricky. If paren, then the
2075 fact that we did/didnot match may influence
2076 future execution. */
2077 && !(paren && ln == 0))
2079 locinput = PL_reginput;
2081 PerlIO_printf(Perl_debug_log,
2082 "%*s matched %ld times, len=%ld...\n",
2083 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2086 if (PL_regkind[(U8)OP(next)] == EXACT) {
2087 c1 = UCHARAT(OPERAND(next) + 1);
2088 if (OP(next) == EXACTF)
2090 else if (OP(next) == EXACTFL)
2091 c2 = PL_fold_locale[c1];
2100 /* If it could work, try it. */
2102 UCHARAT(PL_reginput) == c1 ||
2103 UCHARAT(PL_reginput) == c2)
2106 PerlIO_printf(Perl_debug_log,
2107 "%*s trying tail with n=%ld...\n",
2108 REPORT_CODE_OFF+PL_regindent*2, "", n)
2112 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2113 PL_regendp[paren] = PL_reginput;
2116 PL_regendp[paren] = NULL;
2122 /* Couldn't or didn't -- back up. */
2124 locinput = HOPc(locinput, -l);
2125 PL_reginput = locinput;
2132 paren = scan->flags; /* Which paren to set */
2133 if (paren > PL_regsize)
2135 if (paren > *PL_reglastparen)
2136 *PL_reglastparen = paren;
2137 ln = ARG1(scan); /* min to match */
2138 n = ARG2(scan); /* max to match */
2139 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2143 ln = ARG1(scan); /* min to match */
2144 n = ARG2(scan); /* max to match */
2145 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2150 scan = NEXTOPER(scan);
2156 scan = NEXTOPER(scan);
2160 * Lookahead to avoid useless match attempts
2161 * when we know what character comes next.
2163 if (PL_regkind[(U8)OP(next)] == EXACT) {
2164 c1 = UCHARAT(OPERAND(next) + 1);
2165 if (OP(next) == EXACTF)
2167 else if (OP(next) == EXACTFL)
2168 c2 = PL_fold_locale[c1];
2174 PL_reginput = locinput;
2178 if (ln && regrepeat(scan, ln) < ln)
2180 locinput = PL_reginput;
2182 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2183 /* If it could work, try it. */
2185 UCHARAT(PL_reginput) == c1 ||
2186 UCHARAT(PL_reginput) == c2)
2190 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2191 PL_regendp[paren] = PL_reginput;
2194 PL_regendp[paren] = NULL;
2200 /* Couldn't or didn't -- move forward. */
2201 PL_reginput = locinput;
2202 if (regrepeat(scan, 1)) {
2204 locinput = PL_reginput;
2212 n = regrepeat(scan, n);
2213 locinput = PL_reginput;
2214 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2215 (!PL_multiline || OP(next) == SEOL))
2216 ln = n; /* why back off? */
2220 /* If it could work, try it. */
2222 UCHARAT(PL_reginput) == c1 ||
2223 UCHARAT(PL_reginput) == c2)
2227 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2228 PL_regendp[paren] = PL_reginput;
2231 PL_regendp[paren] = NULL;
2237 /* Couldn't or didn't -- back up. */
2239 PL_reginput = locinput = HOPc(locinput, -1);
2244 /* If it could work, try it. */
2246 UCHARAT(PL_reginput) == c1 ||
2247 UCHARAT(PL_reginput) == c2)
2253 /* Couldn't or didn't -- back up. */
2255 PL_reginput = locinput = HOPc(locinput, -1);
2262 if (PL_reg_call_cc) {
2263 re_cc_state *cur_call_cc = PL_reg_call_cc;
2264 CURCUR *cctmp = PL_regcc;
2265 regexp *re = PL_reg_re;
2266 CHECKPOINT cp, lastcp;
2268 cp = regcppush(0); /* Save *all* the positions. */
2270 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2272 PL_reginput = locinput; /* Make position available to
2274 cache_re(PL_reg_call_cc->re);
2275 PL_regcc = PL_reg_call_cc->cc;
2276 PL_reg_call_cc = PL_reg_call_cc->prev;
2277 if (regmatch(cur_call_cc->node)) {
2278 PL_reg_call_cc = cur_call_cc;
2284 PL_reg_call_cc = cur_call_cc;
2290 PerlIO_printf(Perl_debug_log,
2291 "%*s continuation failed...\n",
2292 REPORT_CODE_OFF+PL_regindent*2, "")
2296 if (locinput < PL_regtill)
2297 sayNO; /* Cannot match: too short. */
2300 PL_reginput = locinput; /* put where regtry can find it */
2301 sayYES; /* Success! */
2304 PL_reginput = locinput;
2309 s = HOPMAYBEc(locinput, -scan->flags);
2315 PL_reginput = locinput;
2320 s = HOPMAYBEc(locinput, -scan->flags);
2321 if (!s || s < PL_bostr)
2326 PL_reginput = locinput;
2329 inner = NEXTOPER(NEXTOPER(scan));
2330 if (regmatch(inner) != n) {
2345 if (OP(scan) == SUSPEND) {
2346 locinput = PL_reginput;
2347 nextchr = UCHARAT(locinput);
2352 next = scan + ARG(scan);
2357 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2358 (unsigned long)scan, OP(scan));
2359 FAIL("regexp memory corruption");
2365 * We get here only if there's trouble -- normally "case END" is
2366 * the terminating point.
2368 FAIL("corrupted regexp pointers");
2386 - regrepeat - repeatedly match something simple, report how many
2389 * [This routine now assumes that it will only match on things of length 1.
2390 * That was true before, but now we assume scan - reginput is the count,
2391 * rather than incrementing count on every character. [Er, except utf8.]]
2394 regrepeat(regnode *p, I32 max)
2397 register char *scan;
2398 register char *opnd;
2400 register char *loceol = PL_regeol;
2401 register I32 hardcount = 0;
2404 if (max != REG_INFTY && max < loceol - scan)
2405 loceol = scan + max;
2406 opnd = (char *) OPERAND(p);
2409 while (scan < loceol && *scan != '\n')
2417 while (scan < loceol && *scan != '\n') {
2418 scan += UTF8SKIP(scan);
2424 while (scan < loceol) {
2425 scan += UTF8SKIP(scan);
2429 case EXACT: /* length of string is 1 */
2430 c = UCHARAT(++opnd);
2431 while (scan < loceol && UCHARAT(scan) == c)
2434 case EXACTF: /* length of string is 1 */
2435 c = UCHARAT(++opnd);
2436 while (scan < loceol &&
2437 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2440 case EXACTFL: /* length of string is 1 */
2441 PL_reg_flags |= RF_tainted;
2442 c = UCHARAT(++opnd);
2443 while (scan < loceol &&
2444 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2449 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2450 scan += UTF8SKIP(scan);
2455 while (scan < loceol && REGINCLASS(opnd, *scan))
2459 while (scan < loceol && isALNUM(*scan))
2464 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2465 scan += UTF8SKIP(scan);
2470 PL_reg_flags |= RF_tainted;
2471 while (scan < loceol && isALNUM_LC(*scan))
2475 PL_reg_flags |= RF_tainted;
2477 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2478 scan += UTF8SKIP(scan);
2484 while (scan < loceol && !isALNUM(*scan))
2489 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2490 scan += UTF8SKIP(scan);
2495 PL_reg_flags |= RF_tainted;
2496 while (scan < loceol && !isALNUM_LC(*scan))
2500 PL_reg_flags |= RF_tainted;
2502 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2503 scan += UTF8SKIP(scan);
2508 while (scan < loceol && isSPACE(*scan))
2513 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2514 scan += UTF8SKIP(scan);
2519 PL_reg_flags |= RF_tainted;
2520 while (scan < loceol && isSPACE_LC(*scan))
2524 PL_reg_flags |= RF_tainted;
2526 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2527 scan += UTF8SKIP(scan);
2532 while (scan < loceol && !isSPACE(*scan))
2537 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2538 scan += UTF8SKIP(scan);
2543 PL_reg_flags |= RF_tainted;
2544 while (scan < loceol && !isSPACE_LC(*scan))
2548 PL_reg_flags |= RF_tainted;
2550 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2551 scan += UTF8SKIP(scan);
2556 while (scan < loceol && isDIGIT(*scan))
2561 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2562 scan += UTF8SKIP(scan);
2568 while (scan < loceol && !isDIGIT(*scan))
2573 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2574 scan += UTF8SKIP(scan);
2578 default: /* Called on something of 0 width. */
2579 break; /* So match right here or not at all. */
2585 c = scan - PL_reginput;
2590 SV *prop = sv_newmortal();
2593 PerlIO_printf(Perl_debug_log,
2594 "%*s %s can match %ld times out of %ld...\n",
2595 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2602 - regrepeat_hard - repeatedly match something, report total lenth and length
2604 * The repeater is supposed to have constant length.
2608 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2611 register char *scan;
2612 register char *start;
2613 register char *loceol = PL_regeol;
2615 I32 count = 0, res = 1;
2620 start = PL_reginput;
2622 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2625 while (start < PL_reginput) {
2627 start += UTF8SKIP(start);
2638 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2640 *lp = l = PL_reginput - start;
2641 if (max != REG_INFTY && l*max < loceol - scan)
2642 loceol = scan + l*max;
2655 - regclass - determine if a character falls into a character class
2659 reginclass(register char *p, register I32 c)
2666 if (ANYOF_TEST(p, c))
2668 else if (flags & ANYOF_FOLD) {
2670 if (flags & ANYOF_LOCALE) {
2671 PL_reg_flags |= RF_tainted;
2672 cf = PL_fold_locale[c];
2676 if (ANYOF_TEST(p, cf))
2680 if (!match && (flags & ANYOF_ISA)) {
2681 PL_reg_flags |= RF_tainted;
2683 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2684 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2685 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2686 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2692 return (flags & ANYOF_INVERT) ? !match : match;
2696 reginclassutf8(regnode *f, U8 *p)
2699 char flags = ARG1(f);
2701 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2703 if (swash_fetch(sv, p))
2705 else if (flags & ANYOF_FOLD) {
2708 if (flags & ANYOF_LOCALE) {
2709 PL_reg_flags |= RF_tainted;
2710 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2713 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2714 if (swash_fetch(sv, tmpbuf))
2718 if (!match && (flags & ANYOF_ISA)) {
2719 PL_reg_flags |= RF_tainted;
2721 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2722 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2723 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2724 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2730 return (flags & ANYOF_INVERT) ? !match : match;
2734 reghop(U8 *s, I32 off)
2738 while (off-- && s < (U8*)PL_regeol)
2743 if (s > (U8*)PL_bostr) {
2746 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2748 } /* XXX could check well-formedness here */
2756 reghopmaybe(U8* s, I32 off)
2760 while (off-- && s < (U8*)PL_regeol)
2767 if (s > (U8*)PL_bostr) {
2770 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2772 } /* XXX could check well-formedness here */