5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregexec my_pregexec
40 # define Perl_reginitcolors my_reginitcolors
45 * pregcomp and pregexec -- regsub and regerror are not used in perl
47 * Copyright (c) 1986 by University of Toronto.
48 * Written by Henry Spencer. Not derived from licensed software.
50 * Permission is granted to anyone to use this software for any
51 * purpose on any computer system, and to redistribute it freely,
52 * subject to the following restrictions:
54 * 1. The author is not responsible for the consequences of use of
55 * this software, no matter how awful, even if they arise
58 * 2. The origin of this software must not be misrepresented, either
59 * by explicit claim or by omission.
61 * 3. Altered versions must be plainly marked as such, and must not
62 * be misrepresented as being the original software.
64 **** Alterations to Henry's code are...
66 **** Copyright (c) 1991-1999, Larry Wall
68 **** You may distribute under the terms of either the GNU General Public
69 **** License or the Artistic License, as specified in the README file.
71 * Beware that some of this code is subtly aware of the way operator
72 * precedence is structured in regular expressions. Serious changes in
73 * regular-expression syntax might require a total rethink.
80 #define RF_tainted 1 /* tainted information used? */
81 #define RF_warned 2 /* warned about big count? */
82 #define RF_evaled 4 /* Did an EVAL with setting? */
83 #define RF_utf8 8 /* String contains multibyte chars? */
85 #define UTF (PL_reg_flags & RF_utf8)
87 #define RS_init 1 /* eval environment created */
88 #define RS_set 2 /* replsv value is set */
95 typedef I32 CHECKPOINT;
101 static I32 regmatch (regnode *prog);
102 static I32 regrepeat (regnode *p, I32 max);
103 static I32 regrepeat_hard (regnode *p, I32 max, I32 *lp);
104 static I32 regtry (regexp *prog, char *startpos);
106 static bool reginclass (char *p, I32 c);
107 static bool reginclassutf8 (regnode *f, U8* p);
108 static CHECKPOINT regcppush (I32 parenfloor);
109 static char * regcppop (void);
110 static char * regcp_set_to (I32 ss);
111 static void cache_re (regexp *prog);
112 static void restore_pos (void *arg);
115 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
116 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
118 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
119 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
122 static U8 * reghop (U8 *pos, I32 off);
123 static U8 * reghopmaybe (U8 *pos, I32 off);
125 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
126 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
127 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
128 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
129 #define HOPc(pos,off) ((char*)HOP(pos,off))
130 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
133 regcppush(I32 parenfloor)
136 int retval = PL_savestack_ix;
137 int i = (PL_regsize - parenfloor) * 4;
141 for (p = PL_regsize; p > parenfloor; p--) {
142 SSPUSHINT(PL_regendp[p]);
143 SSPUSHINT(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] = SSPOPINT;
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],
189 PL_reg_start_tmp[paren] - PL_bostr,
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] = -1;
204 PL_regendp[paren] = -1;
213 I32 tmp = PL_savestack_ix;
215 PL_savestack_ix = ss;
217 PL_savestack_ix = tmp;
221 typedef struct re_cc_state
225 struct re_cc_state *prev;
230 #define regcpblow(cp) LEAVE_SCOPE(cp)
233 * pregexec and friends
237 - pregexec - match a regexp against a string
240 pregexec(register regexp *prog, char *stringarg, register char *strend,
241 char *strbeg, I32 minend, SV *screamer, U32 nosave)
242 /* strend: pointer to null at end of string */
243 /* strbeg: real beginning of string */
244 /* minend: end of match must be >=minend after stringarg. */
245 /* nosave: For optimizations. */
248 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
249 nosave ? 0 : REXEC_COPY_STR);
253 cache_re(regexp *prog)
256 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
258 PL_regprogram = prog->program;
260 PL_regnpar = prog->nparens;
261 PL_regdata = prog->data;
266 restore_pos(void *arg)
269 if (PL_reg_eval_set) {
270 if (PL_reg_oldsaved) {
271 PL_reg_re->subbeg = PL_reg_oldsaved;
272 PL_reg_re->sublen = PL_reg_oldsavedlen;
273 RX_MATCH_COPIED_on(PL_reg_re);
275 PL_reg_magic->mg_len = PL_reg_oldpos;
277 PL_curpm = PL_reg_oldcurpm;
283 - regexec_flags - match a regexp against a string
286 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
287 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
288 /* strend: pointer to null at end of string */
289 /* strbeg: real beginning of string */
290 /* minend: end of match must be >=minend after stringarg. */
291 /* data: May be used for some additional optimizations. */
292 /* nosave: For optimizations. */
297 register char *startpos = stringarg;
299 I32 minlen; /* must match at least this many chars */
300 I32 dontbother = 0; /* how many characters not to try at end */
302 I32 start_shift = 0; /* Offset of the start to find
303 constant substr. */ /* CC */
304 I32 end_shift = 0; /* Same for the end. */ /* CC */
305 I32 scream_pos = -1; /* Internal iterator of scream. */
307 SV* oreplsv = GvSV(PL_replgv);
315 PL_regnarrate = PL_debug & 512;
319 if (prog == NULL || startpos == NULL) {
320 croak("NULL regexp parameter");
324 minlen = prog->minlen;
325 if (strend - startpos < minlen) goto phooey;
327 if (startpos == strbeg) /* is ^ valid at stringarg? */
330 PL_regprev = (U32)stringarg[-1];
331 if (!PL_multiline && PL_regprev == '\n')
332 PL_regprev = '\0'; /* force ^ to NOT match */
335 /* Check validity of program. */
336 if (UCHARAT(prog->program) != REG_MAGIC) {
337 croak("corrupted regexp program");
343 if (prog->reganch & ROPT_UTF8)
344 PL_reg_flags |= RF_utf8;
346 /* Mark beginning of line for ^ and lookbehind. */
347 PL_regbol = startpos;
351 /* Mark end of line for $ (and such) */
354 /* see how far we have to get to not match where we matched before */
355 PL_regtill = startpos+minend;
357 /* We start without call_cc context. */
360 /* If there is a "must appear" string, look for it. */
362 if (!(flags & REXEC_CHECKED)
363 && prog->check_substr != Nullsv &&
364 !(prog->reganch & ROPT_ANCH_GPOS) &&
365 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
366 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
369 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
370 /* Should be nonnegative! */
371 end_shift = minlen - start_shift -
372 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
373 if (flags & REXEC_SCREAM) {
374 SV *c = prog->check_substr;
376 if (PL_screamfirst[BmRARE(c)] >= 0
377 || ( BmRARE(c) == '\n'
378 && (BmPREVIOUS(c) == SvCUR(c) - 1)
380 s = screaminstr(sv, prog->check_substr,
381 start_shift + (stringarg - strbeg),
382 end_shift, &scream_pos, 0);
388 s = fbm_instr((unsigned char*)s + start_shift,
389 (unsigned char*)strend - end_shift,
390 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
392 ++BmUSEFUL(prog->check_substr); /* hooray */
393 goto phooey; /* not present */
395 else if (s - stringarg > prog->check_offset_max &&
397 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
398 : (t = s - prog->check_offset_max) != 0
402 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
405 else if (!(prog->reganch & ROPT_NAUGHTY)
406 && --BmUSEFUL(prog->check_substr) < 0
407 && prog->check_substr == prog->float_substr) { /* boo */
408 SvREFCNT_dec(prog->check_substr);
409 prog->check_substr = Nullsv; /* disable */
410 prog->float_substr = Nullsv; /* clear */
417 DEBUG_r(if (!PL_colorset) reginitcolors());
418 DEBUG_r(PerlIO_printf(Perl_debug_log,
419 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
420 PL_colors[4],PL_colors[5],PL_colors[0],
423 (strlen(prog->precomp) > 60 ? "..." : ""),
425 (strend - startpos > 60 ? 60 : strend - startpos),
426 startpos, PL_colors[1],
427 (strend - startpos > 60 ? "..." : ""))
430 if (prog->reganch & ROPT_GPOS_SEEN) {
433 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
434 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
435 PL_reg_ganch = strbeg + mg->mg_len;
437 PL_reg_ganch = startpos;
440 /* Simplest case: anchored match need be tried only once. */
441 /* [unless only anchor is BOL and multiline is set] */
442 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
443 if (regtry(prog, startpos))
445 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
446 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
449 dontbother = minlen - 1;
450 strend = HOPc(strend, -dontbother);
451 /* for multiline we only have to try after newlines */
455 if (*s++ == '\n') { /* don't need PL_utf8skip here */
456 if (s < strend && regtry(prog, s))
462 } else if (prog->reganch & ROPT_ANCH_GPOS) {
463 if (regtry(prog, PL_reg_ganch))
468 /* Messy cases: unanchored match. */
469 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
470 /* we have /x+whatever/ */
471 /* it must be a one character string */
472 char ch = SvPVX(prog->anchored_substr)[0];
476 if (regtry(prog, s)) goto got_it;
478 while (s < strend && *s == ch)
487 if (regtry(prog, s)) goto got_it;
489 while (s < strend && *s == ch)
497 else if (prog->anchored_substr != Nullsv
498 || (prog->float_substr != Nullsv
499 && prog->float_max_offset < strend - s)) {
500 SV *must = prog->anchored_substr
501 ? prog->anchored_substr : prog->float_substr;
503 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
505 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
506 I32 delta = back_max - back_min;
507 char *last = HOPc(strend, /* Cannot start after this */
508 -(CHR_SVLEN(must) - (SvTAIL(must) != 0) + back_min));
509 char *last1; /* Last position checked before */
514 last1 = s - 1; /* bogus */
516 /* XXXX check_substr already used to find `s', can optimize if
517 check_substr==must. */
519 dontbother = end_shift;
520 strend = HOPc(strend, -dontbother);
521 while ( (s <= last) &&
522 ((flags & REXEC_SCREAM)
523 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
524 end_shift, &scream_pos, 0))
525 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
526 (unsigned char*)strend, must,
527 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
528 if (HOPc(s, -back_max) > last1) {
529 last1 = HOPc(s, -back_min);
530 s = HOPc(s, -back_max);
533 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
535 last1 = HOPc(s, -back_min);
555 else if (c = prog->regstclass) {
556 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
560 dontbother = minlen - 1;
561 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
563 /* We know what class it must start with. */
566 cc = (char *) OPERAND(c);
568 if (REGINCLASSUTF8(c, (U8*)s)) {
569 if (tmp && regtry(prog, s))
580 cc = (char *) OPERAND(c);
582 if (REGINCLASS(cc, *s)) {
583 if (tmp && regtry(prog, s))
594 PL_reg_flags |= RF_tainted;
601 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
602 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
604 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
611 if ((minlen || tmp) && regtry(prog,s))
615 PL_reg_flags |= RF_tainted;
620 strend = reghop_c(strend, -1);
622 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
623 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
625 if (tmp == !(OP(c) == BOUND ?
626 swash_fetch(PL_utf8_alnum, (U8*)s) :
627 isALNUM_LC_utf8((U8*)s)))
635 if ((minlen || tmp) && regtry(prog,s))
639 PL_reg_flags |= RF_tainted;
646 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
647 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
649 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
651 else if (regtry(prog, s))
655 if ((minlen || !tmp) && regtry(prog,s))
659 PL_reg_flags |= RF_tainted;
664 strend = reghop_c(strend, -1);
666 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
667 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
669 if (tmp == !(OP(c) == NBOUND ?
670 swash_fetch(PL_utf8_alnum, (U8*)s) :
671 isALNUM_LC_utf8((U8*)s)))
673 else if (regtry(prog, s))
677 if ((minlen || !tmp) && regtry(prog,s))
683 if (tmp && regtry(prog, s))
695 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
696 if (tmp && regtry(prog, s))
707 PL_reg_flags |= RF_tainted;
709 if (isALNUM_LC(*s)) {
710 if (tmp && regtry(prog, s))
721 PL_reg_flags |= RF_tainted;
723 if (isALNUM_LC_utf8((U8*)s)) {
724 if (tmp && regtry(prog, s))
737 if (tmp && regtry(prog, s))
749 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
750 if (tmp && regtry(prog, s))
761 PL_reg_flags |= RF_tainted;
763 if (!isALNUM_LC(*s)) {
764 if (tmp && regtry(prog, s))
775 PL_reg_flags |= RF_tainted;
777 if (!isALNUM_LC_utf8((U8*)s)) {
778 if (tmp && regtry(prog, s))
791 if (tmp && regtry(prog, s))
803 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
804 if (tmp && regtry(prog, s))
815 PL_reg_flags |= RF_tainted;
817 if (isSPACE_LC(*s)) {
818 if (tmp && regtry(prog, s))
829 PL_reg_flags |= RF_tainted;
831 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
832 if (tmp && regtry(prog, s))
845 if (tmp && regtry(prog, s))
857 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
858 if (tmp && regtry(prog, s))
869 PL_reg_flags |= RF_tainted;
871 if (!isSPACE_LC(*s)) {
872 if (tmp && regtry(prog, s))
883 PL_reg_flags |= RF_tainted;
885 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
886 if (tmp && regtry(prog, s))
899 if (tmp && regtry(prog, s))
911 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
912 if (tmp && regtry(prog, s))
925 if (tmp && regtry(prog, s))
937 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
938 if (tmp && regtry(prog, s))
952 if (prog->float_substr != Nullsv) { /* Trim the end. */
954 I32 oldpos = scream_pos;
956 if (flags & REXEC_SCREAM) {
957 last = screaminstr(sv, prog->float_substr, s - strbeg,
958 end_shift, &scream_pos, 1); /* last one */
960 last = scream_olds; /* Only one occurence. */
964 char *little = SvPV(prog->float_substr, len);
966 if (SvTAIL(prog->float_substr)) {
967 if (memEQ(strend - len + 1, little, len - 1))
968 last = strend - len + 1;
969 else if (!PL_multiline)
970 last = memEQ(strend - len, little, len)
971 ? strend - len : Nullch;
977 last = rninstr(s, strend, little, little + len);
979 last = strend; /* matching `$' */
982 if (last == NULL) goto phooey; /* Should not happen! */
983 dontbother = strend - last + prog->float_min_offset;
985 if (minlen && (dontbother < minlen))
986 dontbother = minlen - 1;
987 strend -= dontbother; /* this one's always in bytes! */
988 /* We don't know much -- general case. */
1000 if (regtry(prog, s))
1002 } while (s++ < strend);
1010 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1012 if (PL_reg_eval_set) {
1013 /* Preserve the current value of $^R */
1014 if (oreplsv != GvSV(PL_replgv))
1015 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1016 restored, the value remains
1021 /* make sure $`, $&, $', and $digit will work later */
1022 if ( !(flags & REXEC_NOT_FIRST) ) {
1023 if (RX_MATCH_COPIED(prog)) {
1024 Safefree(prog->subbeg);
1025 RX_MATCH_COPIED_off(prog);
1027 if (flags & REXEC_COPY_STR) {
1028 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1030 s = savepvn(strbeg, i);
1033 RX_MATCH_COPIED_on(prog);
1036 prog->subbeg = strbeg;
1037 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1044 if (PL_reg_eval_set)
1050 - regtry - try match at specific point
1052 STATIC I32 /* 0 failure, 1 success */
1053 regtry(regexp *prog, char *startpos)
1061 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1064 PL_reg_eval_set = RS_init;
1066 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1067 PL_stack_sp - PL_stack_base);
1069 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1070 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1071 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1073 /* Apparently this is not needed, judging by wantarray. */
1074 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1075 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1078 /* Make $_ available to executed code. */
1079 if (PL_reg_sv != DEFSV) {
1080 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1085 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1086 && (mg = mg_find(PL_reg_sv, 'g')))) {
1087 /* prepare for quick setting of pos */
1088 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1089 mg = mg_find(PL_reg_sv, 'g');
1093 PL_reg_oldpos = mg->mg_len;
1094 SAVEDESTRUCTOR(restore_pos, 0);
1097 New(22,PL_reg_curpm, 1, PMOP);
1098 PL_reg_curpm->op_pmregexp = prog;
1099 PL_reg_oldcurpm = PL_curpm;
1100 PL_curpm = PL_reg_curpm;
1101 if (RX_MATCH_COPIED(prog)) {
1102 /* Here is a serious problem: we cannot rewrite subbeg,
1103 since it may be needed if this match fails. Thus
1104 $` inside (?{}) could fail... */
1105 PL_reg_oldsaved = prog->subbeg;
1106 PL_reg_oldsavedlen = prog->sublen;
1107 RX_MATCH_COPIED_off(prog);
1110 PL_reg_oldsaved = Nullch;
1111 prog->subbeg = PL_bostr;
1112 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1114 prog->startp[0] = startpos - PL_bostr;
1115 PL_reginput = startpos;
1116 PL_regstartp = prog->startp;
1117 PL_regendp = prog->endp;
1118 PL_reglastparen = &prog->lastparen;
1119 prog->lastparen = 0;
1121 DEBUG_r(PL_reg_starttry = startpos);
1122 if (PL_reg_start_tmpl <= prog->nparens) {
1123 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1124 if(PL_reg_start_tmp)
1125 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1127 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1130 /* XXXX What this code is doing here?!!! There should be no need
1131 to do this again and again, PL_reglastparen should take care of
1135 if (prog->nparens) {
1136 for (i = prog->nparens; i >= 1; i--) {
1142 if (regmatch(prog->program + 1)) {
1143 prog->endp[0] = PL_reginput - PL_bostr;
1151 - regmatch - main matching routine
1153 * Conceptually the strategy is simple: check to see whether the current
1154 * node matches, call self recursively to see whether the rest matches,
1155 * and then act accordingly. In practice we make some effort to avoid
1156 * recursion, in particular by going through "ordinary" nodes (that don't
1157 * need to know whether the rest of the match failed) by a loop instead of
1160 /* [lwall] I've hoisted the register declarations to the outer block in order to
1161 * maybe save a little bit of pushing and popping on the stack. It also takes
1162 * advantage of machines that use a register save mask on subroutine entry.
1164 STATIC I32 /* 0 failure, 1 success */
1165 regmatch(regnode *prog)
1168 register regnode *scan; /* Current node. */
1169 regnode *next; /* Next node. */
1170 regnode *inner; /* Next node in internal branch. */
1171 register I32 nextchr; /* renamed nextchr - nextchar colides with
1172 function of same name */
1173 register I32 n; /* no or next */
1174 register I32 ln; /* len or last */
1175 register char *s; /* operand or save */
1176 register char *locinput = PL_reginput;
1177 register I32 c1, c2, paren; /* case fold search, parenth */
1178 int minmod = 0, sw = 0, logical = 0;
1183 /* Note that nextchr is a byte even in UTF */
1184 nextchr = UCHARAT(locinput);
1186 while (scan != NULL) {
1187 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1189 # define sayYES goto yes
1190 # define sayNO goto no
1191 # define saySAME(x) if (x) goto yes; else goto no
1192 # define REPORT_CODE_OFF 24
1194 # define sayYES return 1
1195 # define sayNO return 0
1196 # define saySAME(x) return x
1199 SV *prop = sv_newmortal();
1200 int docolor = *PL_colors[0];
1201 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1202 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1203 /* The part of the string before starttry has one color
1204 (pref0_len chars), between starttry and current
1205 position another one (pref_len - pref0_len chars),
1206 after the current position the third one.
1207 We assume that pref0_len <= pref_len, otherwise we
1208 decrease pref0_len. */
1209 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1210 ? (5 + taill) - l : locinput - PL_bostr);
1211 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1213 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1214 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1215 ? (5 + taill) - pref_len : PL_regeol - locinput);
1218 if (pref0_len > pref_len)
1219 pref0_len = pref_len;
1220 regprop(prop, scan);
1221 PerlIO_printf(Perl_debug_log,
1222 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1223 locinput - PL_bostr,
1224 PL_colors[4], pref0_len,
1225 locinput - pref_len, PL_colors[5],
1226 PL_colors[2], pref_len - pref0_len,
1227 locinput - pref_len + pref0_len, PL_colors[3],
1228 (docolor ? "" : "> <"),
1229 PL_colors[0], l, locinput, PL_colors[1],
1230 15 - l - pref_len + 1,
1232 scan - PL_regprogram, PL_regindent*2, "",
1236 next = scan + NEXT_OFF(scan);
1242 if (locinput == PL_bostr
1243 ? PL_regprev == '\n'
1245 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1247 /* regtill = regbol; */
1252 if (locinput == PL_bostr
1253 ? PL_regprev == '\n'
1254 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1260 if (locinput == PL_regbol && PL_regprev == '\n')
1264 if (locinput == PL_reg_ganch)
1274 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1279 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1281 if (PL_regeol - locinput > 1)
1285 if (PL_regeol != locinput)
1289 if (nextchr & 0x80) {
1290 locinput += PL_utf8skip[nextchr];
1291 if (locinput > PL_regeol)
1293 nextchr = UCHARAT(locinput);
1296 if (!nextchr && locinput >= PL_regeol)
1298 nextchr = UCHARAT(++locinput);
1301 if (!nextchr && locinput >= PL_regeol)
1303 nextchr = UCHARAT(++locinput);
1306 if (nextchr & 0x80) {
1307 locinput += PL_utf8skip[nextchr];
1308 if (locinput > PL_regeol)
1310 nextchr = UCHARAT(locinput);
1313 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1315 nextchr = UCHARAT(++locinput);
1318 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1320 nextchr = UCHARAT(++locinput);
1323 s = (char *) OPERAND(scan);
1325 /* Inline the first character, for speed. */
1326 if (UCHARAT(s) != nextchr)
1328 if (PL_regeol - locinput < ln)
1330 if (ln > 1 && memNE(s, locinput, ln))
1333 nextchr = UCHARAT(locinput);
1336 PL_reg_flags |= RF_tainted;
1339 s = (char *) OPERAND(scan);
1345 c1 = OP(scan) == EXACTF;
1349 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1350 toLOWER_utf8((U8*)l) :
1351 toLOWER_LC_utf8((U8*)l)))
1359 nextchr = UCHARAT(locinput);
1363 /* Inline the first character, for speed. */
1364 if (UCHARAT(s) != nextchr &&
1365 UCHARAT(s) != ((OP(scan) == EXACTF)
1366 ? PL_fold : PL_fold_locale)[nextchr])
1368 if (PL_regeol - locinput < ln)
1370 if (ln > 1 && (OP(scan) == EXACTF
1371 ? ibcmp(s, locinput, ln)
1372 : ibcmp_locale(s, locinput, ln)))
1375 nextchr = UCHARAT(locinput);
1378 s = (char *) OPERAND(scan);
1379 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1381 if (locinput >= PL_regeol)
1383 locinput += PL_utf8skip[nextchr];
1384 nextchr = UCHARAT(locinput);
1387 s = (char *) OPERAND(scan);
1389 nextchr = UCHARAT(locinput);
1390 if (!REGINCLASS(s, nextchr))
1392 if (!nextchr && locinput >= PL_regeol)
1394 nextchr = UCHARAT(++locinput);
1397 PL_reg_flags |= RF_tainted;
1402 if (!(OP(scan) == ALNUM
1403 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1405 nextchr = UCHARAT(++locinput);
1408 PL_reg_flags |= RF_tainted;
1413 if (nextchr & 0x80) {
1414 if (!(OP(scan) == ALNUMUTF8
1415 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1416 : isALNUM_LC_utf8((U8*)locinput)))
1420 locinput += PL_utf8skip[nextchr];
1421 nextchr = UCHARAT(locinput);
1424 if (!(OP(scan) == ALNUMUTF8
1425 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1427 nextchr = UCHARAT(++locinput);
1430 PL_reg_flags |= RF_tainted;
1433 if (!nextchr && locinput >= PL_regeol)
1435 if (OP(scan) == NALNUM
1436 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1438 nextchr = UCHARAT(++locinput);
1441 PL_reg_flags |= RF_tainted;
1444 if (!nextchr && locinput >= PL_regeol)
1446 if (nextchr & 0x80) {
1447 if (OP(scan) == NALNUMUTF8
1448 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1449 : isALNUM_LC_utf8((U8*)locinput))
1453 locinput += PL_utf8skip[nextchr];
1454 nextchr = UCHARAT(locinput);
1457 if (OP(scan) == NALNUMUTF8
1458 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1460 nextchr = UCHARAT(++locinput);
1464 PL_reg_flags |= RF_tainted;
1468 /* was last char in word? */
1469 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1470 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1472 n = isALNUM(nextchr);
1475 ln = isALNUM_LC(ln);
1476 n = isALNUM_LC(nextchr);
1478 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1483 PL_reg_flags |= RF_tainted;
1487 /* was last char in word? */
1488 ln = (locinput != PL_regbol)
1489 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1490 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1491 ln = isALNUM_uni(ln);
1492 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1495 ln = isALNUM_LC_uni(ln);
1496 n = isALNUM_LC_utf8((U8*)locinput);
1498 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1502 PL_reg_flags |= RF_tainted;
1505 if (!nextchr && locinput >= PL_regeol)
1507 if (!(OP(scan) == SPACE
1508 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1510 nextchr = UCHARAT(++locinput);
1513 PL_reg_flags |= RF_tainted;
1516 if (!nextchr && locinput >= PL_regeol)
1518 if (nextchr & 0x80) {
1519 if (!(OP(scan) == SPACEUTF8
1520 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1521 : isSPACE_LC_utf8((U8*)locinput)))
1525 locinput += PL_utf8skip[nextchr];
1526 nextchr = UCHARAT(locinput);
1529 if (!(OP(scan) == SPACEUTF8
1530 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1532 nextchr = UCHARAT(++locinput);
1535 PL_reg_flags |= RF_tainted;
1540 if (OP(scan) == SPACE
1541 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1543 nextchr = UCHARAT(++locinput);
1546 PL_reg_flags |= RF_tainted;
1551 if (nextchr & 0x80) {
1552 if (OP(scan) == NSPACEUTF8
1553 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1554 : isSPACE_LC_utf8((U8*)locinput))
1558 locinput += PL_utf8skip[nextchr];
1559 nextchr = UCHARAT(locinput);
1562 if (OP(scan) == NSPACEUTF8
1563 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1565 nextchr = UCHARAT(++locinput);
1568 if (!isDIGIT(nextchr))
1570 nextchr = UCHARAT(++locinput);
1573 if (nextchr & 0x80) {
1574 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1576 locinput += PL_utf8skip[nextchr];
1577 nextchr = UCHARAT(locinput);
1580 if (!isDIGIT(nextchr))
1582 nextchr = UCHARAT(++locinput);
1585 if (!nextchr && locinput >= PL_regeol)
1587 if (isDIGIT(nextchr))
1589 nextchr = UCHARAT(++locinput);
1592 if (!nextchr && locinput >= PL_regeol)
1594 if (nextchr & 0x80) {
1595 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1597 locinput += PL_utf8skip[nextchr];
1598 nextchr = UCHARAT(locinput);
1601 if (isDIGIT(nextchr))
1603 nextchr = UCHARAT(++locinput);
1606 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1608 locinput += PL_utf8skip[nextchr];
1609 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1610 locinput += UTF8SKIP(locinput);
1611 if (locinput > PL_regeol)
1613 nextchr = UCHARAT(locinput);
1616 PL_reg_flags |= RF_tainted;
1620 n = ARG(scan); /* which paren pair */
1621 ln = PL_regstartp[n];
1622 if (*PL_reglastparen < n || ln == -1)
1623 sayNO; /* Do not match unless seen CLOSEn. */
1624 if (ln == PL_regendp[n])
1628 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1630 char *e = PL_bostr + PL_regendp[n];
1632 * Note that we can't do the "other character" lookup trick as
1633 * in the 8-bit case (no pun intended) because in Unicode we
1634 * have to map both upper and title case to lower case.
1636 if (OP(scan) == REFF) {
1640 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1650 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1657 nextchr = UCHARAT(locinput);
1661 /* Inline the first character, for speed. */
1662 if (UCHARAT(s) != nextchr &&
1664 (UCHARAT(s) != ((OP(scan) == REFF
1665 ? PL_fold : PL_fold_locale)[nextchr]))))
1667 ln = PL_regendp[n] - ln;
1668 if (locinput + ln > PL_regeol)
1670 if (ln > 1 && (OP(scan) == REF
1671 ? memNE(s, locinput, ln)
1673 ? ibcmp(s, locinput, ln)
1674 : ibcmp_locale(s, locinput, ln))))
1677 nextchr = UCHARAT(locinput);
1688 OP_4tree *oop = PL_op;
1689 COP *ocurcop = PL_curcop;
1690 SV **ocurpad = PL_curpad;
1694 PL_op = (OP_4tree*)PL_regdata->data[n];
1695 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1696 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
1697 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
1699 CALLRUNOPS(); /* Scalar context. */
1705 PL_curpad = ocurpad;
1706 PL_curcop = ocurcop;
1708 if (logical == 2) { /* Postponed subexpression. */
1710 MAGIC *mg = Null(MAGIC*);
1713 CHECKPOINT cp, lastcp;
1715 if(SvROK(ret) || SvRMAGICAL(ret)) {
1716 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1719 mg = mg_find(sv, 'r');
1722 re = (regexp *)mg->mg_obj;
1723 (void)ReREFCNT_inc(re);
1727 char *t = SvPV(ret, len);
1729 char *oprecomp = PL_regprecomp;
1730 I32 osize = PL_regsize;
1731 I32 onpar = PL_regnpar;
1734 re = CALLREGCOMP(t, t + len, &pm);
1736 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1737 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1738 PL_regprecomp = oprecomp;
1743 PerlIO_printf(Perl_debug_log,
1744 "Entering embedded `%s%.60s%s%s'\n",
1748 (strlen(re->precomp) > 60 ? "..." : ""))
1751 state.prev = PL_reg_call_cc;
1752 state.cc = PL_regcc;
1753 state.re = PL_reg_re;
1759 cp = regcppush(0); /* Save *all* the positions. */
1762 state.ss = PL_savestack_ix;
1763 *PL_reglastparen = 0;
1764 PL_reg_call_cc = &state;
1765 PL_reginput = locinput;
1766 if (regmatch(re->program + 1)) {
1772 PerlIO_printf(Perl_debug_log,
1774 REPORT_CODE_OFF+PL_regindent*2, "")
1779 PL_reg_call_cc = state.prev;
1780 PL_regcc = state.cc;
1781 PL_reg_re = state.re;
1782 cache_re(PL_reg_re);
1789 sv_setsv(save_scalar(PL_replgv), ret);
1793 n = ARG(scan); /* which paren pair */
1794 PL_reg_start_tmp[n] = locinput;
1799 n = ARG(scan); /* which paren pair */
1800 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
1801 PL_regendp[n] = locinput - PL_bostr;
1802 if (n > *PL_reglastparen)
1803 *PL_reglastparen = n;
1806 n = ARG(scan); /* which paren pair */
1807 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
1811 next = NEXTOPER(NEXTOPER(scan));
1813 next = scan + ARG(scan);
1814 if (OP(next) == IFTHEN) /* Fake one. */
1815 next = NEXTOPER(NEXTOPER(next));
1819 logical = scan->flags;
1823 CHECKPOINT cp = PL_savestack_ix;
1825 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1827 cc.oldcc = PL_regcc;
1829 cc.parenfloor = *PL_reglastparen;
1831 cc.min = ARG1(scan);
1832 cc.max = ARG2(scan);
1833 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1837 PL_reginput = locinput;
1838 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1840 PL_regcc = cc.oldcc;
1846 * This is really hard to understand, because after we match
1847 * what we're trying to match, we must make sure the rest of
1848 * the RE is going to match for sure, and to do that we have
1849 * to go back UP the parse tree by recursing ever deeper. And
1850 * if it fails, we have to reset our parent's current state
1851 * that we can try again after backing off.
1854 CHECKPOINT cp, lastcp;
1855 CURCUR* cc = PL_regcc;
1856 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1858 n = cc->cur + 1; /* how many we know we matched */
1859 PL_reginput = locinput;
1862 PerlIO_printf(Perl_debug_log,
1863 "%*s %ld out of %ld..%ld cc=%lx\n",
1864 REPORT_CODE_OFF+PL_regindent*2, "",
1865 (long)n, (long)cc->min,
1866 (long)cc->max, (long)cc)
1869 /* If degenerate scan matches "", assume scan done. */
1871 if (locinput == cc->lastloc && n >= cc->min) {
1872 PL_regcc = cc->oldcc;
1875 PerlIO_printf(Perl_debug_log,
1876 "%*s empty match detected, try continuation...\n",
1877 REPORT_CODE_OFF+PL_regindent*2, "")
1879 if (regmatch(cc->next))
1882 PerlIO_printf(Perl_debug_log,
1884 REPORT_CODE_OFF+PL_regindent*2, "")
1891 /* First just match a string of min scans. */
1895 cc->lastloc = locinput;
1896 if (regmatch(cc->scan))
1899 cc->lastloc = lastloc;
1901 PerlIO_printf(Perl_debug_log,
1903 REPORT_CODE_OFF+PL_regindent*2, "")
1908 /* Prefer next over scan for minimal matching. */
1911 PL_regcc = cc->oldcc;
1913 cp = regcppush(cc->parenfloor);
1915 if (regmatch(cc->next)) {
1917 sayYES; /* All done. */
1924 if (n >= cc->max) { /* Maximum greed exceeded? */
1925 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1926 && !(PL_reg_flags & RF_warned)) {
1927 PL_reg_flags |= RF_warned;
1928 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1929 "Complex regular subexpression recursion",
1936 PerlIO_printf(Perl_debug_log,
1937 "%*s trying longer...\n",
1938 REPORT_CODE_OFF+PL_regindent*2, "")
1940 /* Try scanning more and see if it helps. */
1941 PL_reginput = locinput;
1943 cc->lastloc = locinput;
1944 cp = regcppush(cc->parenfloor);
1946 if (regmatch(cc->scan)) {
1951 PerlIO_printf(Perl_debug_log,
1953 REPORT_CODE_OFF+PL_regindent*2, "")
1958 cc->lastloc = lastloc;
1962 /* Prefer scan over next for maximal matching. */
1964 if (n < cc->max) { /* More greed allowed? */
1965 cp = regcppush(cc->parenfloor);
1967 cc->lastloc = locinput;
1969 if (regmatch(cc->scan)) {
1974 regcppop(); /* Restore some previous $<digit>s? */
1975 PL_reginput = locinput;
1977 PerlIO_printf(Perl_debug_log,
1978 "%*s failed, try continuation...\n",
1979 REPORT_CODE_OFF+PL_regindent*2, "")
1982 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1983 && !(PL_reg_flags & RF_warned)) {
1984 PL_reg_flags |= RF_warned;
1985 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1986 "Complex regular subexpression recursion",
1990 /* Failed deeper matches of scan, so see if this one works. */
1991 PL_regcc = cc->oldcc;
1993 if (regmatch(cc->next))
1996 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1997 REPORT_CODE_OFF+PL_regindent*2, "")
2002 cc->lastloc = lastloc;
2007 next = scan + ARG(scan);
2010 inner = NEXTOPER(NEXTOPER(scan));
2013 inner = NEXTOPER(scan);
2018 if (OP(next) != c1) /* No choice. */
2019 next = inner; /* Avoid recursion. */
2021 int lastparen = *PL_reglastparen;
2025 PL_reginput = locinput;
2026 if (regmatch(inner))
2029 for (n = *PL_reglastparen; n > lastparen; n--)
2031 *PL_reglastparen = n;
2034 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2038 inner = NEXTOPER(scan);
2039 if (c1 == BRANCHJ) {
2040 inner = NEXTOPER(inner);
2042 } while (scan != NULL && OP(scan) == c1);
2056 /* We suppose that the next guy does not need
2057 backtracking: in particular, it is of constant length,
2058 and has no parenths to influence future backrefs. */
2059 ln = ARG1(scan); /* min to match */
2060 n = ARG2(scan); /* max to match */
2061 paren = scan->flags;
2063 if (paren > PL_regsize)
2065 if (paren > *PL_reglastparen)
2066 *PL_reglastparen = paren;
2068 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2070 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2071 PL_reginput = locinput;
2074 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2076 if (ln && l == 0 && n >= ln
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;
2083 if (PL_regkind[(U8)OP(next)] == EXACT) {
2084 c1 = UCHARAT(OPERAND(next) + 1);
2085 if (OP(next) == EXACTF)
2087 else if (OP(next) == EXACTFL)
2088 c2 = PL_fold_locale[c1];
2095 /* This may be improved if l == 0. */
2096 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2097 /* If it could work, try it. */
2099 UCHARAT(PL_reginput) == c1 ||
2100 UCHARAT(PL_reginput) == c2)
2104 PL_regstartp[paren] =
2105 HOPc(PL_reginput, -l) - PL_bostr;
2106 PL_regendp[paren] = PL_reginput - PL_bostr;
2109 PL_regendp[paren] = -1;
2115 /* Couldn't or didn't -- move forward. */
2116 PL_reginput = locinput;
2117 if (regrepeat_hard(scan, 1, &l)) {
2119 locinput = PL_reginput;
2126 n = regrepeat_hard(scan, n, &l);
2127 if (n != 0 && l == 0
2128 /* In fact, this is tricky. If paren, then the
2129 fact that we did/didnot match may influence
2130 future execution. */
2131 && !(paren && ln == 0))
2133 locinput = PL_reginput;
2135 PerlIO_printf(Perl_debug_log,
2136 "%*s matched %ld times, len=%ld...\n",
2137 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2140 if (PL_regkind[(U8)OP(next)] == EXACT) {
2141 c1 = UCHARAT(OPERAND(next) + 1);
2142 if (OP(next) == EXACTF)
2144 else if (OP(next) == EXACTFL)
2145 c2 = PL_fold_locale[c1];
2154 /* If it could work, try it. */
2156 UCHARAT(PL_reginput) == c1 ||
2157 UCHARAT(PL_reginput) == c2)
2160 PerlIO_printf(Perl_debug_log,
2161 "%*s trying tail with n=%ld...\n",
2162 REPORT_CODE_OFF+PL_regindent*2, "", n)
2166 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2167 PL_regendp[paren] = PL_reginput - PL_bostr;
2170 PL_regendp[paren] = -1;
2176 /* Couldn't or didn't -- back up. */
2178 locinput = HOPc(locinput, -l);
2179 PL_reginput = locinput;
2186 paren = scan->flags; /* Which paren to set */
2187 if (paren > PL_regsize)
2189 if (paren > *PL_reglastparen)
2190 *PL_reglastparen = paren;
2191 ln = ARG1(scan); /* min to match */
2192 n = ARG2(scan); /* max to match */
2193 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2197 ln = ARG1(scan); /* min to match */
2198 n = ARG2(scan); /* max to match */
2199 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2204 scan = NEXTOPER(scan);
2210 scan = NEXTOPER(scan);
2214 * Lookahead to avoid useless match attempts
2215 * when we know what character comes next.
2217 if (PL_regkind[(U8)OP(next)] == EXACT) {
2218 c1 = UCHARAT(OPERAND(next) + 1);
2219 if (OP(next) == EXACTF)
2221 else if (OP(next) == EXACTFL)
2222 c2 = PL_fold_locale[c1];
2228 PL_reginput = locinput;
2232 if (ln && regrepeat(scan, ln) < ln)
2234 locinput = PL_reginput;
2237 char *e = locinput + n - ln; /* Should not check after this */
2238 char *old = locinput;
2240 if (e >= PL_regeol || (n == REG_INFTY))
2243 /* Find place 'next' could work */
2245 while (locinput <= e && *locinput != c1)
2248 while (locinput <= e
2255 /* PL_reginput == old now */
2256 if (locinput != old) {
2257 ln = 1; /* Did some */
2258 if (regrepeat(scan, locinput - old) <
2262 /* PL_reginput == locinput now */
2265 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2266 PL_regendp[paren] = locinput - PL_bostr;
2269 PL_regendp[paren] = -1;
2273 PL_reginput = locinput; /* Could be reset... */
2275 /* Couldn't or didn't -- move forward. */
2280 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2281 /* If it could work, try it. */
2283 UCHARAT(PL_reginput) == c1 ||
2284 UCHARAT(PL_reginput) == c2)
2288 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2289 PL_regendp[paren] = PL_reginput - PL_bostr;
2292 PL_regendp[paren] = -1;
2298 /* Couldn't or didn't -- move forward. */
2299 PL_reginput = locinput;
2300 if (regrepeat(scan, 1)) {
2302 locinput = PL_reginput;
2310 n = regrepeat(scan, n);
2311 locinput = PL_reginput;
2312 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2313 (!PL_multiline || OP(next) == SEOL))
2314 ln = n; /* why back off? */
2318 /* If it could work, try it. */
2320 UCHARAT(PL_reginput) == c1 ||
2321 UCHARAT(PL_reginput) == c2)
2325 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2326 PL_regendp[paren] = PL_reginput - PL_bostr;
2329 PL_regendp[paren] = -1;
2335 /* Couldn't or didn't -- back up. */
2337 PL_reginput = locinput = HOPc(locinput, -1);
2342 /* If it could work, try it. */
2344 UCHARAT(PL_reginput) == c1 ||
2345 UCHARAT(PL_reginput) == c2)
2351 /* Couldn't or didn't -- back up. */
2353 PL_reginput = locinput = HOPc(locinput, -1);
2360 if (PL_reg_call_cc) {
2361 re_cc_state *cur_call_cc = PL_reg_call_cc;
2362 CURCUR *cctmp = PL_regcc;
2363 regexp *re = PL_reg_re;
2364 CHECKPOINT cp, lastcp;
2366 cp = regcppush(0); /* Save *all* the positions. */
2368 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2370 PL_reginput = locinput; /* Make position available to
2372 cache_re(PL_reg_call_cc->re);
2373 PL_regcc = PL_reg_call_cc->cc;
2374 PL_reg_call_cc = PL_reg_call_cc->prev;
2375 if (regmatch(cur_call_cc->node)) {
2376 PL_reg_call_cc = cur_call_cc;
2382 PL_reg_call_cc = cur_call_cc;
2388 PerlIO_printf(Perl_debug_log,
2389 "%*s continuation failed...\n",
2390 REPORT_CODE_OFF+PL_regindent*2, "")
2394 if (locinput < PL_regtill)
2395 sayNO; /* Cannot match: too short. */
2398 PL_reginput = locinput; /* put where regtry can find it */
2399 sayYES; /* Success! */
2402 PL_reginput = locinput;
2407 if (UTF) { /* XXXX This is absolutely
2408 broken, we read before
2410 s = HOPMAYBEc(locinput, -scan->flags);
2416 if (locinput < PL_bostr + scan->flags)
2418 PL_reginput = locinput - scan->flags;
2423 PL_reginput = locinput;
2428 if (UTF) { /* XXXX This is absolutely
2429 broken, we read before
2431 s = HOPMAYBEc(locinput, -scan->flags);
2432 if (!s || s < PL_bostr)
2437 if (locinput < PL_bostr + scan->flags)
2439 PL_reginput = locinput - scan->flags;
2444 PL_reginput = locinput;
2447 inner = NEXTOPER(NEXTOPER(scan));
2448 if (regmatch(inner) != n) {
2463 if (OP(scan) == SUSPEND) {
2464 locinput = PL_reginput;
2465 nextchr = UCHARAT(locinput);
2470 next = scan + ARG(scan);
2475 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2476 (unsigned long)scan, OP(scan));
2477 croak("regexp memory corruption");
2483 * We get here only if there's trouble -- normally "case END" is
2484 * the terminating point.
2486 croak("corrupted regexp pointers");
2504 - regrepeat - repeatedly match something simple, report how many
2507 * [This routine now assumes that it will only match on things of length 1.
2508 * That was true before, but now we assume scan - reginput is the count,
2509 * rather than incrementing count on every character. [Er, except utf8.]]
2512 regrepeat(regnode *p, I32 max)
2515 register char *scan;
2516 register char *opnd;
2518 register char *loceol = PL_regeol;
2519 register I32 hardcount = 0;
2522 if (max != REG_INFTY && max < loceol - scan)
2523 loceol = scan + max;
2524 opnd = (char *) OPERAND(p);
2527 while (scan < loceol && *scan != '\n')
2535 while (scan < loceol && *scan != '\n') {
2536 scan += UTF8SKIP(scan);
2542 while (scan < loceol) {
2543 scan += UTF8SKIP(scan);
2547 case EXACT: /* length of string is 1 */
2548 c = UCHARAT(++opnd);
2549 while (scan < loceol && UCHARAT(scan) == c)
2552 case EXACTF: /* length of string is 1 */
2553 c = UCHARAT(++opnd);
2554 while (scan < loceol &&
2555 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2558 case EXACTFL: /* length of string is 1 */
2559 PL_reg_flags |= RF_tainted;
2560 c = UCHARAT(++opnd);
2561 while (scan < loceol &&
2562 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2567 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2568 scan += UTF8SKIP(scan);
2573 while (scan < loceol && REGINCLASS(opnd, *scan))
2577 while (scan < loceol && isALNUM(*scan))
2582 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2583 scan += UTF8SKIP(scan);
2588 PL_reg_flags |= RF_tainted;
2589 while (scan < loceol && isALNUM_LC(*scan))
2593 PL_reg_flags |= RF_tainted;
2595 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2596 scan += UTF8SKIP(scan);
2602 while (scan < loceol && !isALNUM(*scan))
2607 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2608 scan += UTF8SKIP(scan);
2613 PL_reg_flags |= RF_tainted;
2614 while (scan < loceol && !isALNUM_LC(*scan))
2618 PL_reg_flags |= RF_tainted;
2620 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2621 scan += UTF8SKIP(scan);
2626 while (scan < loceol && isSPACE(*scan))
2631 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2632 scan += UTF8SKIP(scan);
2637 PL_reg_flags |= RF_tainted;
2638 while (scan < loceol && isSPACE_LC(*scan))
2642 PL_reg_flags |= RF_tainted;
2644 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2645 scan += UTF8SKIP(scan);
2650 while (scan < loceol && !isSPACE(*scan))
2655 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2656 scan += UTF8SKIP(scan);
2661 PL_reg_flags |= RF_tainted;
2662 while (scan < loceol && !isSPACE_LC(*scan))
2666 PL_reg_flags |= RF_tainted;
2668 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2669 scan += UTF8SKIP(scan);
2674 while (scan < loceol && isDIGIT(*scan))
2679 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2680 scan += UTF8SKIP(scan);
2686 while (scan < loceol && !isDIGIT(*scan))
2691 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2692 scan += UTF8SKIP(scan);
2696 default: /* Called on something of 0 width. */
2697 break; /* So match right here or not at all. */
2703 c = scan - PL_reginput;
2708 SV *prop = sv_newmortal();
2711 PerlIO_printf(Perl_debug_log,
2712 "%*s %s can match %ld times out of %ld...\n",
2713 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2720 - regrepeat_hard - repeatedly match something, report total lenth and length
2722 * The repeater is supposed to have constant length.
2726 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2729 register char *scan;
2730 register char *start;
2731 register char *loceol = PL_regeol;
2733 I32 count = 0, res = 1;
2738 start = PL_reginput;
2740 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2743 while (start < PL_reginput) {
2745 start += UTF8SKIP(start);
2756 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2758 *lp = l = PL_reginput - start;
2759 if (max != REG_INFTY && l*max < loceol - scan)
2760 loceol = scan + l*max;
2773 - reginclass - determine if a character falls into a character class
2777 reginclass(register char *p, register I32 c)
2784 if (ANYOF_TEST(p, c))
2786 else if (flags & ANYOF_FOLD) {
2788 if (flags & ANYOF_LOCALE) {
2789 PL_reg_flags |= RF_tainted;
2790 cf = PL_fold_locale[c];
2794 if (ANYOF_TEST(p, cf))
2798 if (!match && (flags & ANYOF_ISA)) {
2799 PL_reg_flags |= RF_tainted;
2801 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2802 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2803 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2804 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2810 return (flags & ANYOF_INVERT) ? !match : match;
2814 reginclassutf8(regnode *f, U8 *p)
2817 char flags = ARG1(f);
2819 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2821 if (swash_fetch(sv, p))
2823 else if (flags & ANYOF_FOLD) {
2826 if (flags & ANYOF_LOCALE) {
2827 PL_reg_flags |= RF_tainted;
2828 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2831 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2832 if (swash_fetch(sv, tmpbuf))
2836 if (!match && (flags & ANYOF_ISA)) {
2837 PL_reg_flags |= RF_tainted;
2839 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2840 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2841 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2842 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2848 return (flags & ANYOF_INVERT) ? !match : match;
2852 reghop(U8 *s, I32 off)
2856 while (off-- && s < (U8*)PL_regeol)
2861 if (s > (U8*)PL_bostr) {
2864 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2866 } /* XXX could check well-formedness here */
2874 reghopmaybe(U8* s, I32 off)
2878 while (off-- && s < (U8*)PL_regeol)
2885 if (s > (U8*)PL_bostr) {
2888 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2890 } /* XXX could check well-formedness here */