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 */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
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 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
46 * pregcomp and pregexec -- regsub and regerror are not used in perl
48 * Copyright (c) 1986 by University of Toronto.
49 * Written by Henry Spencer. Not derived from licensed software.
51 * Permission is granted to anyone to use this software for any
52 * purpose on any computer system, and to redistribute it freely,
53 * subject to the following restrictions:
55 * 1. The author is not responsible for the consequences of use of
56 * this software, no matter how awful, even if they arise
59 * 2. The origin of this software must not be misrepresented, either
60 * by explicit claim or by omission.
62 * 3. Altered versions must be plainly marked as such, and must not
63 * be misrepresented as being the original software.
65 **** Alterations to Henry's code are...
67 **** Copyright (c) 1991-1999, Larry Wall
69 **** You may distribute under the terms of either the GNU General Public
70 **** License or the Artistic License, as specified in the README file.
72 * Beware that some of this code is subtly aware of the way operator
73 * precedence is structured in regular expressions. Serious changes in
74 * regular-expression syntax might require a total rethink.
77 #define PERL_IN_REGEXEC_C
82 #define RF_tainted 1 /* tainted information used? */
83 #define RF_warned 2 /* warned about big count? */
84 #define RF_evaled 4 /* Did an EVAL with setting? */
85 #define RF_utf8 8 /* String contains multibyte chars? */
87 #define UTF (PL_reg_flags & RF_utf8)
89 #define RS_init 1 /* eval environment created */
90 #define RS_set 2 /* replsv value is set */
100 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
101 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
106 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
107 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
108 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
110 #define HOPc(pos,off) ((char*)HOP(pos,off))
111 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
114 S_regcppush(pTHX_ I32 parenfloor)
117 int retval = PL_savestack_ix;
118 int i = (PL_regsize - parenfloor) * 4;
122 for (p = PL_regsize; p > parenfloor; p--) {
123 SSPUSHINT(PL_regendp[p]);
124 SSPUSHINT(PL_regstartp[p]);
125 SSPUSHPTR(PL_reg_start_tmp[p]);
128 SSPUSHINT(PL_regsize);
129 SSPUSHINT(*PL_reglastparen);
130 SSPUSHPTR(PL_reginput);
132 SSPUSHINT(SAVEt_REGCONTEXT);
136 /* These are needed since we do not localize EVAL nodes: */
137 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
138 " Setting an EVAL scope, savestack=%i\n", \
139 PL_savestack_ix)); lastcp = PL_savestack_ix
141 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
142 PerlIO_printf(Perl_debug_log, \
143 " Clearing an EVAL scope, savestack=%i..%i\n", \
144 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
154 assert(i == SAVEt_REGCONTEXT);
156 input = (char *) SSPOPPTR;
157 *PL_reglastparen = SSPOPINT;
158 PL_regsize = SSPOPINT;
159 for (i -= 3; i > 0; i -= 4) {
160 paren = (U32)SSPOPINT;
161 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
162 PL_regstartp[paren] = SSPOPINT;
164 if (paren <= *PL_reglastparen)
165 PL_regendp[paren] = tmps;
167 PerlIO_printf(Perl_debug_log,
168 " restoring \\%d to %d(%d)..%d%s\n",
169 paren, PL_regstartp[paren],
170 PL_reg_start_tmp[paren] - PL_bostr,
172 (paren > *PL_reglastparen ? "(no)" : ""));
176 if (*PL_reglastparen + 1 <= PL_regnpar) {
177 PerlIO_printf(Perl_debug_log,
178 " restoring \\%d..\\%d to undef\n",
179 *PL_reglastparen + 1, PL_regnpar);
182 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
183 if (paren > PL_regsize)
184 PL_regstartp[paren] = -1;
185 PL_regendp[paren] = -1;
191 S_regcp_set_to(pTHX_ I32 ss)
194 I32 tmp = PL_savestack_ix;
196 PL_savestack_ix = ss;
198 PL_savestack_ix = tmp;
202 typedef struct re_cc_state
206 struct re_cc_state *prev;
211 #define regcpblow(cp) LEAVE_SCOPE(cp)
214 * pregexec and friends
218 - pregexec - match a regexp against a string
221 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
222 char *strbeg, I32 minend, SV *screamer, U32 nosave)
223 /* strend: pointer to null at end of string */
224 /* strbeg: real beginning of string */
225 /* minend: end of match must be >=minend after stringarg. */
226 /* nosave: For optimizations. */
229 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
230 nosave ? 0 : REXEC_COPY_STR);
234 S_cache_re(pTHX_ regexp *prog)
237 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
239 PL_regprogram = prog->program;
241 PL_regnpar = prog->nparens;
242 PL_regdata = prog->data;
247 S_restore_pos(pTHX_ void *arg)
250 if (PL_reg_eval_set) {
251 if (PL_reg_oldsaved) {
252 PL_reg_re->subbeg = PL_reg_oldsaved;
253 PL_reg_re->sublen = PL_reg_oldsavedlen;
254 RX_MATCH_COPIED_on(PL_reg_re);
256 PL_reg_magic->mg_len = PL_reg_oldpos;
258 PL_curpm = PL_reg_oldcurpm;
263 * Need to implement the following flags for reg_anch:
265 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
267 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
268 * INTUIT_AUTORITATIVE_ML
269 * INTUIT_ONCE_NOML - Intuit can match in one location only.
272 * Another flag for this function: SECOND_TIME (so that float substrs
273 * with giant delta may be not rechecked).
276 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
278 /* If SCREAM, then sv should be compatible with strpos and strend.
279 Otherwise, only SvCUR(sv) is used to get strbeg. */
281 /* XXXX We assume that strpos is strbeg unless sv. */
284 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
285 char *strend, U32 flags, re_scream_pos_data *data)
288 /* Should be nonnegative! */
294 DEBUG_r( if (!PL_colorset) reginitcolors() );
295 DEBUG_r(PerlIO_printf(Perl_debug_log,
296 "%sGuessing start of match:%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
297 PL_colors[4],PL_colors[5],PL_colors[0],
300 (strlen(prog->precomp) > 60 ? "..." : ""),
302 (strend - strpos > 60 ? 60 : strend - strpos),
303 strpos, PL_colors[1],
304 (strend - strpos > 60 ? "..." : ""))
307 if (prog->minlen > strend - strpos)
310 /* XXXX Move further down? */
311 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
312 /* Should be nonnegative! */
313 end_shift = prog->minlen - start_shift -
314 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
316 if (prog->reganch & ROPT_ANCH) {
317 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
318 || ( (prog->reganch & ROPT_ANCH_BOL)
319 && !PL_multiline ) );
321 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
325 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
326 && (sv && (strpos + SvCUR(sv) != strend)) )
329 s = (char*)HOP((U8*)strpos, prog->check_offset_min);
330 if (SvTAIL(prog->check_substr)) {
331 slen = SvCUR(prog->check_substr); /* >= 1 */
333 if ( strend - s > slen || strend - s < slen - 1 ) {
337 if ( strend - s == slen && strend[-1] != '\n') {
341 /* Now should match s[0..slen-2] */
343 if (slen && (*SvPVX(prog->check_substr) != *s
345 && memNE(SvPVX(prog->check_substr), s, slen))))
348 else if (*SvPVX(prog->check_substr) != *s
349 || ((slen = SvCUR(prog->check_substr)) > 1
350 && memNE(SvPVX(prog->check_substr), s, slen)))
357 if (!ml_anch && (s + prog->check_offset_max < strend - prog->minlen))
358 end_shift += strend - s - prog->minlen - prog->check_offset_max;
366 if (flags & REXEC_SCREAM) {
367 SV *c = prog->check_substr;
368 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
369 I32 p = -1; /* Internal iterator of scream. */
370 I32 *pp = data ? data->scream_pos : &p;
372 if (PL_screamfirst[BmRARE(c)] >= 0
373 || ( BmRARE(c) == '\n'
374 && (BmPREVIOUS(c) == SvCUR(c) - 1)
376 s = screaminstr(sv, prog->check_substr,
377 start_shift + (strpos - strbeg), end_shift, pp, 0);
381 *data->scream_olds = s;
384 s = fbm_instr((unsigned char*)s + start_shift,
385 (unsigned char*)strend - end_shift,
386 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
388 /* Update the count-of-usability, remove useless subpatterns,
392 ++BmUSEFUL(prog->check_substr); /* hooray */
393 goto fail; /* not present */
395 else if (s - strpos > prog->check_offset_max &&
396 ((prog->reganch & ROPT_UTF8)
397 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
399 : (t = s - prog->check_offset_max) != 0) ) {
400 if (ml_anch && t[-1] != '\n') {
402 while (t < strend - end_shift - prog->minlen) {
404 if (t < s - prog->check_offset_min) {
418 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
422 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
426 if (!(prog->reganch & ROPT_NAUGHTY)
427 && --BmUSEFUL(prog->check_substr) < 0
428 && prog->check_substr == prog->float_substr) { /* boo */
429 /* If flags & SOMETHING - do not do it many times on the same match */
430 SvREFCNT_dec(prog->check_substr);
431 prog->check_substr = Nullsv; /* disable */
432 prog->float_substr = Nullsv; /* clear */
434 prog->reganch &= ~RE_USE_INTUIT;
440 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sFound%s at offset %ld\n",
441 PL_colors[4],PL_colors[5], (long)(s - strpos)) );
444 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sNot found...%s\n",
445 PL_colors[4],PL_colors[5]));
450 - regexec_flags - match a regexp against a string
453 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
454 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
455 /* strend: pointer to null at end of string */
456 /* strbeg: real beginning of string */
457 /* minend: end of match must be >=minend after stringarg. */
458 /* data: May be used for some additional optimizations. */
459 /* nosave: For optimizations. */
464 register char *startpos = stringarg;
466 I32 minlen; /* must match at least this many chars */
467 I32 dontbother = 0; /* how many characters not to try at end */
469 I32 start_shift = 0; /* Offset of the start to find
470 constant substr. */ /* CC */
471 I32 end_shift = 0; /* Same for the end. */ /* CC */
472 I32 scream_pos = -1; /* Internal iterator of scream. */
474 SV* oreplsv = GvSV(PL_replgv);
482 PL_regnarrate = PL_debug & 512;
486 if (prog == NULL || startpos == NULL) {
487 Perl_croak(aTHX_ "NULL regexp parameter");
491 minlen = prog->minlen;
492 if (strend - startpos < minlen) goto phooey;
494 if (startpos == strbeg) /* is ^ valid at stringarg? */
497 PL_regprev = (U32)stringarg[-1];
498 if (!PL_multiline && PL_regprev == '\n')
499 PL_regprev = '\0'; /* force ^ to NOT match */
502 /* Check validity of program. */
503 if (UCHARAT(prog->program) != REG_MAGIC) {
504 Perl_croak(aTHX_ "corrupted regexp program");
510 if (prog->reganch & ROPT_UTF8)
511 PL_reg_flags |= RF_utf8;
513 /* Mark beginning of line for ^ and lookbehind. */
514 PL_regbol = startpos;
518 /* Mark end of line for $ (and such) */
521 /* see how far we have to get to not match where we matched before */
522 PL_regtill = startpos+minend;
524 /* We start without call_cc context. */
527 /* If there is a "must appear" string, look for it. */
530 if (prog->reganch & ROPT_GPOS_SEEN) {
533 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
534 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
535 PL_reg_ganch = strbeg + mg->mg_len;
537 PL_reg_ganch = startpos;
538 if (prog->reganch & ROPT_ANCH_GPOS) {
539 if (s > PL_reg_ganch)
545 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
546 re_scream_pos_data d;
548 d.scream_olds = &scream_olds;
549 d.scream_pos = &scream_pos;
550 s = re_intuit_start(prog, sv, s, strend, flags, &d);
552 goto phooey; /* not present */
555 DEBUG_r( if (!PL_colorset) reginitcolors() );
556 DEBUG_r(PerlIO_printf(Perl_debug_log,
557 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
558 PL_colors[4],PL_colors[5],PL_colors[0],
561 (strlen(prog->precomp) > 60 ? "..." : ""),
563 (strend - startpos > 60 ? 60 : strend - startpos),
564 startpos, PL_colors[1],
565 (strend - startpos > 60 ? "..." : ""))
568 /* Simplest case: anchored match need be tried only once. */
569 /* [unless only anchor is BOL and multiline is set] */
570 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
571 if (s == startpos && regtry(prog, startpos))
573 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
574 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
579 dontbother = minlen - 1;
580 end = HOPc(strend, -dontbother) - 1;
581 /* for multiline we only have to try after newlines */
582 if (prog->check_substr) {
588 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
596 if (*s++ == '\n') { /* don't need PL_utf8skip here */
604 } else if (prog->reganch & ROPT_ANCH_GPOS) {
605 if (regtry(prog, PL_reg_ganch))
610 /* Messy cases: unanchored match. */
611 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
612 /* we have /x+whatever/ */
613 /* it must be a one character string (XXXX Except UTF?) */
614 char ch = SvPVX(prog->anchored_substr)[0];
618 if (regtry(prog, s)) goto got_it;
620 while (s < strend && *s == ch)
629 if (regtry(prog, s)) goto got_it;
631 while (s < strend && *s == ch)
639 else if (prog->anchored_substr != Nullsv
640 || (prog->float_substr != Nullsv
641 && prog->float_max_offset < strend - s)) {
642 SV *must = prog->anchored_substr
643 ? prog->anchored_substr : prog->float_substr;
645 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
647 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
648 I32 delta = back_max - back_min;
649 char *last = HOPc(strend, /* Cannot start after this */
650 -(I32)(CHR_SVLEN(must)
651 - (SvTAIL(must) != 0) + back_min));
652 char *last1; /* Last position checked before */
657 last1 = s - 1; /* bogus */
659 /* XXXX check_substr already used to find `s', can optimize if
660 check_substr==must. */
662 dontbother = end_shift;
663 strend = HOPc(strend, -dontbother);
664 while ( (s <= last) &&
665 ((flags & REXEC_SCREAM)
666 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
667 end_shift, &scream_pos, 0))
668 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
669 (unsigned char*)strend, must,
670 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
671 if (HOPc(s, -back_max) > last1) {
672 last1 = HOPc(s, -back_min);
673 s = HOPc(s, -back_max);
676 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
678 last1 = HOPc(s, -back_min);
698 else if (c = prog->regstclass) {
699 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
703 dontbother = minlen - 1;
704 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
706 /* We know what class it must start with. */
709 cc = (char *) OPERAND(c);
711 if (REGINCLASSUTF8(c, (U8*)s)) {
712 if (tmp && regtry(prog, s))
723 cc = (char *) OPERAND(c);
725 if (REGINCLASS(cc, *s)) {
726 if (tmp && regtry(prog, s))
737 PL_reg_flags |= RF_tainted;
744 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
745 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
747 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
754 if ((minlen || tmp) && regtry(prog,s))
758 PL_reg_flags |= RF_tainted;
763 strend = reghop_c(strend, -1);
765 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
766 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
768 if (tmp == !(OP(c) == BOUND ?
769 swash_fetch(PL_utf8_alnum, (U8*)s) :
770 isALNUM_LC_utf8((U8*)s)))
778 if ((minlen || tmp) && regtry(prog,s))
782 PL_reg_flags |= RF_tainted;
789 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
790 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
792 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
794 else if (regtry(prog, s))
798 if ((minlen || !tmp) && regtry(prog,s))
802 PL_reg_flags |= RF_tainted;
807 strend = reghop_c(strend, -1);
809 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
810 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
812 if (tmp == !(OP(c) == NBOUND ?
813 swash_fetch(PL_utf8_alnum, (U8*)s) :
814 isALNUM_LC_utf8((U8*)s)))
816 else if (regtry(prog, s))
820 if ((minlen || !tmp) && regtry(prog,s))
826 if (tmp && regtry(prog, s))
838 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
839 if (tmp && regtry(prog, s))
850 PL_reg_flags |= RF_tainted;
852 if (isALNUM_LC(*s)) {
853 if (tmp && regtry(prog, s))
864 PL_reg_flags |= RF_tainted;
866 if (isALNUM_LC_utf8((U8*)s)) {
867 if (tmp && regtry(prog, s))
880 if (tmp && regtry(prog, s))
892 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
893 if (tmp && regtry(prog, s))
904 PL_reg_flags |= RF_tainted;
906 if (!isALNUM_LC(*s)) {
907 if (tmp && regtry(prog, s))
918 PL_reg_flags |= RF_tainted;
920 if (!isALNUM_LC_utf8((U8*)s)) {
921 if (tmp && regtry(prog, s))
934 if (tmp && regtry(prog, s))
946 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
947 if (tmp && regtry(prog, s))
958 PL_reg_flags |= RF_tainted;
960 if (isSPACE_LC(*s)) {
961 if (tmp && regtry(prog, s))
972 PL_reg_flags |= RF_tainted;
974 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
975 if (tmp && regtry(prog, s))
988 if (tmp && regtry(prog, s))
1000 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1001 if (tmp && regtry(prog, s))
1012 PL_reg_flags |= RF_tainted;
1013 while (s < strend) {
1014 if (!isSPACE_LC(*s)) {
1015 if (tmp && regtry(prog, s))
1026 PL_reg_flags |= RF_tainted;
1027 while (s < strend) {
1028 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1029 if (tmp && regtry(prog, s))
1040 while (s < strend) {
1042 if (tmp && regtry(prog, s))
1053 while (s < strend) {
1054 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1055 if (tmp && regtry(prog, s))
1066 while (s < strend) {
1068 if (tmp && regtry(prog, s))
1079 while (s < strend) {
1080 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1081 if (tmp && regtry(prog, s))
1095 if (prog->float_substr != Nullsv) { /* Trim the end. */
1097 I32 oldpos = scream_pos;
1099 if (flags & REXEC_SCREAM) {
1100 last = screaminstr(sv, prog->float_substr, s - strbeg,
1101 end_shift, &scream_pos, 1); /* last one */
1103 last = scream_olds; /* Only one occurence. */
1107 char *little = SvPV(prog->float_substr, len);
1109 if (SvTAIL(prog->float_substr)) {
1110 if (memEQ(strend - len + 1, little, len - 1))
1111 last = strend - len + 1;
1112 else if (!PL_multiline)
1113 last = memEQ(strend - len, little, len)
1114 ? strend - len : Nullch;
1120 last = rninstr(s, strend, little, little + len);
1122 last = strend; /* matching `$' */
1125 if (last == NULL) goto phooey; /* Should not happen! */
1126 dontbother = strend - last + prog->float_min_offset;
1128 if (minlen && (dontbother < minlen))
1129 dontbother = minlen - 1;
1130 strend -= dontbother; /* this one's always in bytes! */
1131 /* We don't know much -- general case. */
1134 if (regtry(prog, s))
1143 if (regtry(prog, s))
1145 } while (s++ < strend);
1153 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1155 if (PL_reg_eval_set) {
1156 /* Preserve the current value of $^R */
1157 if (oreplsv != GvSV(PL_replgv))
1158 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1159 restored, the value remains
1164 /* make sure $`, $&, $', and $digit will work later */
1165 if ( !(flags & REXEC_NOT_FIRST) ) {
1166 if (RX_MATCH_COPIED(prog)) {
1167 Safefree(prog->subbeg);
1168 RX_MATCH_COPIED_off(prog);
1170 if (flags & REXEC_COPY_STR) {
1171 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1173 s = savepvn(strbeg, i);
1176 RX_MATCH_COPIED_on(prog);
1179 prog->subbeg = strbeg;
1180 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1187 if (PL_reg_eval_set)
1193 - regtry - try match at specific point
1195 STATIC I32 /* 0 failure, 1 success */
1196 S_regtry(pTHX_ regexp *prog, char *startpos)
1204 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1207 PL_reg_eval_set = RS_init;
1209 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1210 PL_stack_sp - PL_stack_base);
1212 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1213 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1214 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1216 /* Apparently this is not needed, judging by wantarray. */
1217 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1218 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1221 /* Make $_ available to executed code. */
1222 if (PL_reg_sv != DEFSV) {
1223 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1228 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1229 && (mg = mg_find(PL_reg_sv, 'g')))) {
1230 /* prepare for quick setting of pos */
1231 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1232 mg = mg_find(PL_reg_sv, 'g');
1236 PL_reg_oldpos = mg->mg_len;
1237 SAVEDESTRUCTOR(S_restore_pos, 0);
1240 New(22,PL_reg_curpm, 1, PMOP);
1241 PL_reg_curpm->op_pmregexp = prog;
1242 PL_reg_oldcurpm = PL_curpm;
1243 PL_curpm = PL_reg_curpm;
1244 if (RX_MATCH_COPIED(prog)) {
1245 /* Here is a serious problem: we cannot rewrite subbeg,
1246 since it may be needed if this match fails. Thus
1247 $` inside (?{}) could fail... */
1248 PL_reg_oldsaved = prog->subbeg;
1249 PL_reg_oldsavedlen = prog->sublen;
1250 RX_MATCH_COPIED_off(prog);
1253 PL_reg_oldsaved = Nullch;
1254 prog->subbeg = PL_bostr;
1255 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1257 prog->startp[0] = startpos - PL_bostr;
1258 PL_reginput = startpos;
1259 PL_regstartp = prog->startp;
1260 PL_regendp = prog->endp;
1261 PL_reglastparen = &prog->lastparen;
1262 prog->lastparen = 0;
1264 DEBUG_r(PL_reg_starttry = startpos);
1265 if (PL_reg_start_tmpl <= prog->nparens) {
1266 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1267 if(PL_reg_start_tmp)
1268 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1270 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1273 /* XXXX What this code is doing here?!!! There should be no need
1274 to do this again and again, PL_reglastparen should take care of
1278 if (prog->nparens) {
1279 for (i = prog->nparens; i >= 1; i--) {
1285 if (regmatch(prog->program + 1)) {
1286 prog->endp[0] = PL_reginput - PL_bostr;
1294 - regmatch - main matching routine
1296 * Conceptually the strategy is simple: check to see whether the current
1297 * node matches, call self recursively to see whether the rest matches,
1298 * and then act accordingly. In practice we make some effort to avoid
1299 * recursion, in particular by going through "ordinary" nodes (that don't
1300 * need to know whether the rest of the match failed) by a loop instead of
1303 /* [lwall] I've hoisted the register declarations to the outer block in order to
1304 * maybe save a little bit of pushing and popping on the stack. It also takes
1305 * advantage of machines that use a register save mask on subroutine entry.
1307 STATIC I32 /* 0 failure, 1 success */
1308 S_regmatch(pTHX_ regnode *prog)
1311 register regnode *scan; /* Current node. */
1312 regnode *next; /* Next node. */
1313 regnode *inner; /* Next node in internal branch. */
1314 register I32 nextchr; /* renamed nextchr - nextchar colides with
1315 function of same name */
1316 register I32 n; /* no or next */
1317 register I32 ln; /* len or last */
1318 register char *s; /* operand or save */
1319 register char *locinput = PL_reginput;
1320 register I32 c1, c2, paren; /* case fold search, parenth */
1321 int minmod = 0, sw = 0, logical = 0;
1326 /* Note that nextchr is a byte even in UTF */
1327 nextchr = UCHARAT(locinput);
1329 while (scan != NULL) {
1330 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1332 # define sayYES goto yes
1333 # define sayNO goto no
1334 # define saySAME(x) if (x) goto yes; else goto no
1335 # define REPORT_CODE_OFF 24
1337 # define sayYES return 1
1338 # define sayNO return 0
1339 # define saySAME(x) return x
1342 SV *prop = sv_newmortal();
1343 int docolor = *PL_colors[0];
1344 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1345 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1346 /* The part of the string before starttry has one color
1347 (pref0_len chars), between starttry and current
1348 position another one (pref_len - pref0_len chars),
1349 after the current position the third one.
1350 We assume that pref0_len <= pref_len, otherwise we
1351 decrease pref0_len. */
1352 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1353 ? (5 + taill) - l : locinput - PL_bostr);
1354 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1356 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1357 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1358 ? (5 + taill) - pref_len : PL_regeol - locinput);
1361 if (pref0_len > pref_len)
1362 pref0_len = pref_len;
1363 regprop(prop, scan);
1364 PerlIO_printf(Perl_debug_log,
1365 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1366 locinput - PL_bostr,
1367 PL_colors[4], pref0_len,
1368 locinput - pref_len, PL_colors[5],
1369 PL_colors[2], pref_len - pref0_len,
1370 locinput - pref_len + pref0_len, PL_colors[3],
1371 (docolor ? "" : "> <"),
1372 PL_colors[0], l, locinput, PL_colors[1],
1373 15 - l - pref_len + 1,
1375 scan - PL_regprogram, PL_regindent*2, "",
1379 next = scan + NEXT_OFF(scan);
1385 if (locinput == PL_bostr
1386 ? PL_regprev == '\n'
1388 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1390 /* regtill = regbol; */
1395 if (locinput == PL_bostr
1396 ? PL_regprev == '\n'
1397 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1403 if (locinput == PL_regbol && PL_regprev == '\n')
1407 if (locinput == PL_reg_ganch)
1417 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1422 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1424 if (PL_regeol - locinput > 1)
1428 if (PL_regeol != locinput)
1432 if (nextchr & 0x80) {
1433 locinput += PL_utf8skip[nextchr];
1434 if (locinput > PL_regeol)
1436 nextchr = UCHARAT(locinput);
1439 if (!nextchr && locinput >= PL_regeol)
1441 nextchr = UCHARAT(++locinput);
1444 if (!nextchr && locinput >= PL_regeol)
1446 nextchr = UCHARAT(++locinput);
1449 if (nextchr & 0x80) {
1450 locinput += PL_utf8skip[nextchr];
1451 if (locinput > PL_regeol)
1453 nextchr = UCHARAT(locinput);
1456 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1458 nextchr = UCHARAT(++locinput);
1461 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1463 nextchr = UCHARAT(++locinput);
1466 s = (char *) OPERAND(scan);
1468 /* Inline the first character, for speed. */
1469 if (UCHARAT(s) != nextchr)
1471 if (PL_regeol - locinput < ln)
1473 if (ln > 1 && memNE(s, locinput, ln))
1476 nextchr = UCHARAT(locinput);
1479 PL_reg_flags |= RF_tainted;
1482 s = (char *) OPERAND(scan);
1488 c1 = OP(scan) == EXACTF;
1492 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1493 toLOWER_utf8((U8*)l) :
1494 toLOWER_LC_utf8((U8*)l)))
1502 nextchr = UCHARAT(locinput);
1506 /* Inline the first character, for speed. */
1507 if (UCHARAT(s) != nextchr &&
1508 UCHARAT(s) != ((OP(scan) == EXACTF)
1509 ? PL_fold : PL_fold_locale)[nextchr])
1511 if (PL_regeol - locinput < ln)
1513 if (ln > 1 && (OP(scan) == EXACTF
1514 ? ibcmp(s, locinput, ln)
1515 : ibcmp_locale(s, locinput, ln)))
1518 nextchr = UCHARAT(locinput);
1521 s = (char *) OPERAND(scan);
1522 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1524 if (locinput >= PL_regeol)
1526 locinput += PL_utf8skip[nextchr];
1527 nextchr = UCHARAT(locinput);
1530 s = (char *) OPERAND(scan);
1532 nextchr = UCHARAT(locinput);
1533 if (!REGINCLASS(s, nextchr))
1535 if (!nextchr && locinput >= PL_regeol)
1537 nextchr = UCHARAT(++locinput);
1540 PL_reg_flags |= RF_tainted;
1545 if (!(OP(scan) == ALNUM
1546 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1548 nextchr = UCHARAT(++locinput);
1551 PL_reg_flags |= RF_tainted;
1556 if (nextchr & 0x80) {
1557 if (!(OP(scan) == ALNUMUTF8
1558 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1559 : isALNUM_LC_utf8((U8*)locinput)))
1563 locinput += PL_utf8skip[nextchr];
1564 nextchr = UCHARAT(locinput);
1567 if (!(OP(scan) == ALNUMUTF8
1568 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1570 nextchr = UCHARAT(++locinput);
1573 PL_reg_flags |= RF_tainted;
1576 if (!nextchr && locinput >= PL_regeol)
1578 if (OP(scan) == NALNUM
1579 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1581 nextchr = UCHARAT(++locinput);
1584 PL_reg_flags |= RF_tainted;
1587 if (!nextchr && locinput >= PL_regeol)
1589 if (nextchr & 0x80) {
1590 if (OP(scan) == NALNUMUTF8
1591 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1592 : isALNUM_LC_utf8((U8*)locinput))
1596 locinput += PL_utf8skip[nextchr];
1597 nextchr = UCHARAT(locinput);
1600 if (OP(scan) == NALNUMUTF8
1601 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1603 nextchr = UCHARAT(++locinput);
1607 PL_reg_flags |= RF_tainted;
1611 /* was last char in word? */
1612 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1613 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1615 n = isALNUM(nextchr);
1618 ln = isALNUM_LC(ln);
1619 n = isALNUM_LC(nextchr);
1621 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1626 PL_reg_flags |= RF_tainted;
1630 /* was last char in word? */
1631 ln = (locinput != PL_regbol)
1632 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1633 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1634 ln = isALNUM_uni(ln);
1635 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1638 ln = isALNUM_LC_uni(ln);
1639 n = isALNUM_LC_utf8((U8*)locinput);
1641 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1645 PL_reg_flags |= RF_tainted;
1648 if (!nextchr && locinput >= PL_regeol)
1650 if (!(OP(scan) == SPACE
1651 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1653 nextchr = UCHARAT(++locinput);
1656 PL_reg_flags |= RF_tainted;
1659 if (!nextchr && locinput >= PL_regeol)
1661 if (nextchr & 0x80) {
1662 if (!(OP(scan) == SPACEUTF8
1663 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1664 : isSPACE_LC_utf8((U8*)locinput)))
1668 locinput += PL_utf8skip[nextchr];
1669 nextchr = UCHARAT(locinput);
1672 if (!(OP(scan) == SPACEUTF8
1673 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1675 nextchr = UCHARAT(++locinput);
1678 PL_reg_flags |= RF_tainted;
1683 if (OP(scan) == SPACE
1684 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1686 nextchr = UCHARAT(++locinput);
1689 PL_reg_flags |= RF_tainted;
1694 if (nextchr & 0x80) {
1695 if (OP(scan) == NSPACEUTF8
1696 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1697 : isSPACE_LC_utf8((U8*)locinput))
1701 locinput += PL_utf8skip[nextchr];
1702 nextchr = UCHARAT(locinput);
1705 if (OP(scan) == NSPACEUTF8
1706 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1708 nextchr = UCHARAT(++locinput);
1711 if (!isDIGIT(nextchr))
1713 nextchr = UCHARAT(++locinput);
1716 if (nextchr & 0x80) {
1717 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1719 locinput += PL_utf8skip[nextchr];
1720 nextchr = UCHARAT(locinput);
1723 if (!isDIGIT(nextchr))
1725 nextchr = UCHARAT(++locinput);
1728 if (!nextchr && locinput >= PL_regeol)
1730 if (isDIGIT(nextchr))
1732 nextchr = UCHARAT(++locinput);
1735 if (!nextchr && locinput >= PL_regeol)
1737 if (nextchr & 0x80) {
1738 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1740 locinput += PL_utf8skip[nextchr];
1741 nextchr = UCHARAT(locinput);
1744 if (isDIGIT(nextchr))
1746 nextchr = UCHARAT(++locinput);
1749 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1751 locinput += PL_utf8skip[nextchr];
1752 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1753 locinput += UTF8SKIP(locinput);
1754 if (locinput > PL_regeol)
1756 nextchr = UCHARAT(locinput);
1759 PL_reg_flags |= RF_tainted;
1763 n = ARG(scan); /* which paren pair */
1764 ln = PL_regstartp[n];
1765 if (*PL_reglastparen < n || ln == -1)
1766 sayNO; /* Do not match unless seen CLOSEn. */
1767 if (ln == PL_regendp[n])
1771 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1773 char *e = PL_bostr + PL_regendp[n];
1775 * Note that we can't do the "other character" lookup trick as
1776 * in the 8-bit case (no pun intended) because in Unicode we
1777 * have to map both upper and title case to lower case.
1779 if (OP(scan) == REFF) {
1783 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1793 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1800 nextchr = UCHARAT(locinput);
1804 /* Inline the first character, for speed. */
1805 if (UCHARAT(s) != nextchr &&
1807 (UCHARAT(s) != ((OP(scan) == REFF
1808 ? PL_fold : PL_fold_locale)[nextchr]))))
1810 ln = PL_regendp[n] - ln;
1811 if (locinput + ln > PL_regeol)
1813 if (ln > 1 && (OP(scan) == REF
1814 ? memNE(s, locinput, ln)
1816 ? ibcmp(s, locinput, ln)
1817 : ibcmp_locale(s, locinput, ln))))
1820 nextchr = UCHARAT(locinput);
1831 OP_4tree *oop = PL_op;
1832 COP *ocurcop = PL_curcop;
1833 SV **ocurpad = PL_curpad;
1837 PL_op = (OP_4tree*)PL_regdata->data[n];
1838 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1839 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
1840 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
1842 CALLRUNOPS(aTHX); /* Scalar context. */
1848 PL_curpad = ocurpad;
1849 PL_curcop = ocurcop;
1851 if (logical == 2) { /* Postponed subexpression. */
1853 MAGIC *mg = Null(MAGIC*);
1856 CHECKPOINT cp, lastcp;
1858 if(SvROK(ret) || SvRMAGICAL(ret)) {
1859 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1862 mg = mg_find(sv, 'r');
1865 re = (regexp *)mg->mg_obj;
1866 (void)ReREFCNT_inc(re);
1870 char *t = SvPV(ret, len);
1872 char *oprecomp = PL_regprecomp;
1873 I32 osize = PL_regsize;
1874 I32 onpar = PL_regnpar;
1877 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
1879 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1880 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1881 PL_regprecomp = oprecomp;
1886 PerlIO_printf(Perl_debug_log,
1887 "Entering embedded `%s%.60s%s%s'\n",
1891 (strlen(re->precomp) > 60 ? "..." : ""))
1894 state.prev = PL_reg_call_cc;
1895 state.cc = PL_regcc;
1896 state.re = PL_reg_re;
1902 cp = regcppush(0); /* Save *all* the positions. */
1905 state.ss = PL_savestack_ix;
1906 *PL_reglastparen = 0;
1907 PL_reg_call_cc = &state;
1908 PL_reginput = locinput;
1909 if (regmatch(re->program + 1)) {
1915 PerlIO_printf(Perl_debug_log,
1917 REPORT_CODE_OFF+PL_regindent*2, "")
1922 PL_reg_call_cc = state.prev;
1923 PL_regcc = state.cc;
1924 PL_reg_re = state.re;
1925 cache_re(PL_reg_re);
1932 sv_setsv(save_scalar(PL_replgv), ret);
1936 n = ARG(scan); /* which paren pair */
1937 PL_reg_start_tmp[n] = locinput;
1942 n = ARG(scan); /* which paren pair */
1943 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
1944 PL_regendp[n] = locinput - PL_bostr;
1945 if (n > *PL_reglastparen)
1946 *PL_reglastparen = n;
1949 n = ARG(scan); /* which paren pair */
1950 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
1954 next = NEXTOPER(NEXTOPER(scan));
1956 next = scan + ARG(scan);
1957 if (OP(next) == IFTHEN) /* Fake one. */
1958 next = NEXTOPER(NEXTOPER(next));
1962 logical = scan->flags;
1966 CHECKPOINT cp = PL_savestack_ix;
1968 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1970 cc.oldcc = PL_regcc;
1972 cc.parenfloor = *PL_reglastparen;
1974 cc.min = ARG1(scan);
1975 cc.max = ARG2(scan);
1976 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1980 PL_reginput = locinput;
1981 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1983 PL_regcc = cc.oldcc;
1989 * This is really hard to understand, because after we match
1990 * what we're trying to match, we must make sure the rest of
1991 * the RE is going to match for sure, and to do that we have
1992 * to go back UP the parse tree by recursing ever deeper. And
1993 * if it fails, we have to reset our parent's current state
1994 * that we can try again after backing off.
1997 CHECKPOINT cp, lastcp;
1998 CURCUR* cc = PL_regcc;
1999 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2001 n = cc->cur + 1; /* how many we know we matched */
2002 PL_reginput = locinput;
2005 PerlIO_printf(Perl_debug_log,
2006 "%*s %ld out of %ld..%ld cc=%lx\n",
2007 REPORT_CODE_OFF+PL_regindent*2, "",
2008 (long)n, (long)cc->min,
2009 (long)cc->max, (long)cc)
2012 /* If degenerate scan matches "", assume scan done. */
2014 if (locinput == cc->lastloc && n >= cc->min) {
2015 PL_regcc = cc->oldcc;
2018 PerlIO_printf(Perl_debug_log,
2019 "%*s empty match detected, try continuation...\n",
2020 REPORT_CODE_OFF+PL_regindent*2, "")
2022 if (regmatch(cc->next))
2025 PerlIO_printf(Perl_debug_log,
2027 REPORT_CODE_OFF+PL_regindent*2, "")
2034 /* First just match a string of min scans. */
2038 cc->lastloc = locinput;
2039 if (regmatch(cc->scan))
2042 cc->lastloc = lastloc;
2044 PerlIO_printf(Perl_debug_log,
2046 REPORT_CODE_OFF+PL_regindent*2, "")
2051 /* Prefer next over scan for minimal matching. */
2054 PL_regcc = cc->oldcc;
2056 cp = regcppush(cc->parenfloor);
2058 if (regmatch(cc->next)) {
2060 sayYES; /* All done. */
2067 if (n >= cc->max) { /* Maximum greed exceeded? */
2068 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2069 && !(PL_reg_flags & RF_warned)) {
2070 PL_reg_flags |= RF_warned;
2071 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2072 "Complex regular subexpression recursion",
2079 PerlIO_printf(Perl_debug_log,
2080 "%*s trying longer...\n",
2081 REPORT_CODE_OFF+PL_regindent*2, "")
2083 /* Try scanning more and see if it helps. */
2084 PL_reginput = locinput;
2086 cc->lastloc = locinput;
2087 cp = regcppush(cc->parenfloor);
2089 if (regmatch(cc->scan)) {
2094 PerlIO_printf(Perl_debug_log,
2096 REPORT_CODE_OFF+PL_regindent*2, "")
2101 cc->lastloc = lastloc;
2105 /* Prefer scan over next for maximal matching. */
2107 if (n < cc->max) { /* More greed allowed? */
2108 cp = regcppush(cc->parenfloor);
2110 cc->lastloc = locinput;
2112 if (regmatch(cc->scan)) {
2117 regcppop(); /* Restore some previous $<digit>s? */
2118 PL_reginput = locinput;
2120 PerlIO_printf(Perl_debug_log,
2121 "%*s failed, try continuation...\n",
2122 REPORT_CODE_OFF+PL_regindent*2, "")
2125 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2126 && !(PL_reg_flags & RF_warned)) {
2127 PL_reg_flags |= RF_warned;
2128 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2129 "Complex regular subexpression recursion",
2133 /* Failed deeper matches of scan, so see if this one works. */
2134 PL_regcc = cc->oldcc;
2136 if (regmatch(cc->next))
2139 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
2140 REPORT_CODE_OFF+PL_regindent*2, "")
2145 cc->lastloc = lastloc;
2150 next = scan + ARG(scan);
2153 inner = NEXTOPER(NEXTOPER(scan));
2156 inner = NEXTOPER(scan);
2161 if (OP(next) != c1) /* No choice. */
2162 next = inner; /* Avoid recursion. */
2164 int lastparen = *PL_reglastparen;
2168 PL_reginput = locinput;
2169 if (regmatch(inner))
2172 for (n = *PL_reglastparen; n > lastparen; n--)
2174 *PL_reglastparen = n;
2177 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2181 inner = NEXTOPER(scan);
2182 if (c1 == BRANCHJ) {
2183 inner = NEXTOPER(inner);
2185 } while (scan != NULL && OP(scan) == c1);
2199 /* We suppose that the next guy does not need
2200 backtracking: in particular, it is of constant length,
2201 and has no parenths to influence future backrefs. */
2202 ln = ARG1(scan); /* min to match */
2203 n = ARG2(scan); /* max to match */
2204 paren = scan->flags;
2206 if (paren > PL_regsize)
2208 if (paren > *PL_reglastparen)
2209 *PL_reglastparen = paren;
2211 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2213 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2214 PL_reginput = locinput;
2217 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2219 if (ln && l == 0 && n >= ln
2220 /* In fact, this is tricky. If paren, then the
2221 fact that we did/didnot match may influence
2222 future execution. */
2223 && !(paren && ln == 0))
2225 locinput = PL_reginput;
2226 if (PL_regkind[(U8)OP(next)] == EXACT) {
2227 c1 = UCHARAT(OPERAND(next) + 1);
2228 if (OP(next) == EXACTF)
2230 else if (OP(next) == EXACTFL)
2231 c2 = PL_fold_locale[c1];
2238 /* This may be improved if l == 0. */
2239 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2240 /* If it could work, try it. */
2242 UCHARAT(PL_reginput) == c1 ||
2243 UCHARAT(PL_reginput) == c2)
2247 PL_regstartp[paren] =
2248 HOPc(PL_reginput, -l) - PL_bostr;
2249 PL_regendp[paren] = PL_reginput - PL_bostr;
2252 PL_regendp[paren] = -1;
2258 /* Couldn't or didn't -- move forward. */
2259 PL_reginput = locinput;
2260 if (regrepeat_hard(scan, 1, &l)) {
2262 locinput = PL_reginput;
2269 n = regrepeat_hard(scan, n, &l);
2270 if (n != 0 && l == 0
2271 /* In fact, this is tricky. If paren, then the
2272 fact that we did/didnot match may influence
2273 future execution. */
2274 && !(paren && ln == 0))
2276 locinput = PL_reginput;
2278 PerlIO_printf(Perl_debug_log,
2279 "%*s matched %ld times, len=%ld...\n",
2280 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2283 if (PL_regkind[(U8)OP(next)] == EXACT) {
2284 c1 = UCHARAT(OPERAND(next) + 1);
2285 if (OP(next) == EXACTF)
2287 else if (OP(next) == EXACTFL)
2288 c2 = PL_fold_locale[c1];
2297 /* If it could work, try it. */
2299 UCHARAT(PL_reginput) == c1 ||
2300 UCHARAT(PL_reginput) == c2)
2303 PerlIO_printf(Perl_debug_log,
2304 "%*s trying tail with n=%ld...\n",
2305 REPORT_CODE_OFF+PL_regindent*2, "", n)
2309 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2310 PL_regendp[paren] = PL_reginput - PL_bostr;
2313 PL_regendp[paren] = -1;
2319 /* Couldn't or didn't -- back up. */
2321 locinput = HOPc(locinput, -l);
2322 PL_reginput = locinput;
2329 paren = scan->flags; /* Which paren to set */
2330 if (paren > PL_regsize)
2332 if (paren > *PL_reglastparen)
2333 *PL_reglastparen = paren;
2334 ln = ARG1(scan); /* min to match */
2335 n = ARG2(scan); /* max to match */
2336 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2340 ln = ARG1(scan); /* min to match */
2341 n = ARG2(scan); /* max to match */
2342 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2347 scan = NEXTOPER(scan);
2353 scan = NEXTOPER(scan);
2357 * Lookahead to avoid useless match attempts
2358 * when we know what character comes next.
2360 if (PL_regkind[(U8)OP(next)] == EXACT) {
2361 c1 = UCHARAT(OPERAND(next) + 1);
2362 if (OP(next) == EXACTF)
2364 else if (OP(next) == EXACTFL)
2365 c2 = PL_fold_locale[c1];
2371 PL_reginput = locinput;
2375 if (ln && regrepeat(scan, ln) < ln)
2377 locinput = PL_reginput;
2380 char *e = locinput + n - ln; /* Should not check after this */
2381 char *old = locinput;
2383 if (e >= PL_regeol || (n == REG_INFTY))
2386 /* Find place 'next' could work */
2388 while (locinput <= e && *locinput != c1)
2391 while (locinput <= e
2398 /* PL_reginput == old now */
2399 if (locinput != old) {
2400 ln = 1; /* Did some */
2401 if (regrepeat(scan, locinput - old) <
2405 /* PL_reginput == locinput now */
2408 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2409 PL_regendp[paren] = locinput - PL_bostr;
2412 PL_regendp[paren] = -1;
2416 PL_reginput = locinput; /* Could be reset... */
2418 /* Couldn't or didn't -- move forward. */
2423 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2424 /* If it could work, try it. */
2426 UCHARAT(PL_reginput) == c1 ||
2427 UCHARAT(PL_reginput) == c2)
2431 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2432 PL_regendp[paren] = PL_reginput - PL_bostr;
2435 PL_regendp[paren] = -1;
2441 /* Couldn't or didn't -- move forward. */
2442 PL_reginput = locinput;
2443 if (regrepeat(scan, 1)) {
2445 locinput = PL_reginput;
2453 n = regrepeat(scan, n);
2454 locinput = PL_reginput;
2455 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2456 (!PL_multiline || OP(next) == SEOL))
2457 ln = n; /* why back off? */
2461 /* If it could work, try it. */
2463 UCHARAT(PL_reginput) == c1 ||
2464 UCHARAT(PL_reginput) == c2)
2468 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2469 PL_regendp[paren] = PL_reginput - PL_bostr;
2472 PL_regendp[paren] = -1;
2478 /* Couldn't or didn't -- back up. */
2480 PL_reginput = locinput = HOPc(locinput, -1);
2485 /* If it could work, try it. */
2487 UCHARAT(PL_reginput) == c1 ||
2488 UCHARAT(PL_reginput) == c2)
2494 /* Couldn't or didn't -- back up. */
2496 PL_reginput = locinput = HOPc(locinput, -1);
2503 if (PL_reg_call_cc) {
2504 re_cc_state *cur_call_cc = PL_reg_call_cc;
2505 CURCUR *cctmp = PL_regcc;
2506 regexp *re = PL_reg_re;
2507 CHECKPOINT cp, lastcp;
2509 cp = regcppush(0); /* Save *all* the positions. */
2511 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2513 PL_reginput = locinput; /* Make position available to
2515 cache_re(PL_reg_call_cc->re);
2516 PL_regcc = PL_reg_call_cc->cc;
2517 PL_reg_call_cc = PL_reg_call_cc->prev;
2518 if (regmatch(cur_call_cc->node)) {
2519 PL_reg_call_cc = cur_call_cc;
2525 PL_reg_call_cc = cur_call_cc;
2531 PerlIO_printf(Perl_debug_log,
2532 "%*s continuation failed...\n",
2533 REPORT_CODE_OFF+PL_regindent*2, "")
2537 if (locinput < PL_regtill)
2538 sayNO; /* Cannot match: too short. */
2541 PL_reginput = locinput; /* put where regtry can find it */
2542 sayYES; /* Success! */
2545 PL_reginput = locinput;
2550 if (UTF) { /* XXXX This is absolutely
2551 broken, we read before
2553 s = HOPMAYBEc(locinput, -scan->flags);
2559 if (locinput < PL_bostr + scan->flags)
2561 PL_reginput = locinput - scan->flags;
2566 PL_reginput = locinput;
2571 if (UTF) { /* XXXX This is absolutely
2572 broken, we read before
2574 s = HOPMAYBEc(locinput, -scan->flags);
2575 if (!s || s < PL_bostr)
2580 if (locinput < PL_bostr + scan->flags)
2582 PL_reginput = locinput - scan->flags;
2587 PL_reginput = locinput;
2590 inner = NEXTOPER(NEXTOPER(scan));
2591 if (regmatch(inner) != n) {
2606 if (OP(scan) == SUSPEND) {
2607 locinput = PL_reginput;
2608 nextchr = UCHARAT(locinput);
2613 next = scan + ARG(scan);
2618 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2619 (unsigned long)scan, OP(scan));
2620 Perl_croak(aTHX_ "regexp memory corruption");
2626 * We get here only if there's trouble -- normally "case END" is
2627 * the terminating point.
2629 Perl_croak(aTHX_ "corrupted regexp pointers");
2647 - regrepeat - repeatedly match something simple, report how many
2650 * [This routine now assumes that it will only match on things of length 1.
2651 * That was true before, but now we assume scan - reginput is the count,
2652 * rather than incrementing count on every character. [Er, except utf8.]]
2655 S_regrepeat(pTHX_ regnode *p, I32 max)
2658 register char *scan;
2659 register char *opnd;
2661 register char *loceol = PL_regeol;
2662 register I32 hardcount = 0;
2665 if (max != REG_INFTY && max < loceol - scan)
2666 loceol = scan + max;
2667 opnd = (char *) OPERAND(p);
2670 while (scan < loceol && *scan != '\n')
2678 while (scan < loceol && *scan != '\n') {
2679 scan += UTF8SKIP(scan);
2685 while (scan < loceol) {
2686 scan += UTF8SKIP(scan);
2690 case EXACT: /* length of string is 1 */
2691 c = UCHARAT(++opnd);
2692 while (scan < loceol && UCHARAT(scan) == c)
2695 case EXACTF: /* length of string is 1 */
2696 c = UCHARAT(++opnd);
2697 while (scan < loceol &&
2698 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2701 case EXACTFL: /* length of string is 1 */
2702 PL_reg_flags |= RF_tainted;
2703 c = UCHARAT(++opnd);
2704 while (scan < loceol &&
2705 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2710 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2711 scan += UTF8SKIP(scan);
2716 while (scan < loceol && REGINCLASS(opnd, *scan))
2720 while (scan < loceol && isALNUM(*scan))
2725 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2726 scan += UTF8SKIP(scan);
2731 PL_reg_flags |= RF_tainted;
2732 while (scan < loceol && isALNUM_LC(*scan))
2736 PL_reg_flags |= RF_tainted;
2738 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2739 scan += UTF8SKIP(scan);
2745 while (scan < loceol && !isALNUM(*scan))
2750 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2751 scan += UTF8SKIP(scan);
2756 PL_reg_flags |= RF_tainted;
2757 while (scan < loceol && !isALNUM_LC(*scan))
2761 PL_reg_flags |= RF_tainted;
2763 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2764 scan += UTF8SKIP(scan);
2769 while (scan < loceol && isSPACE(*scan))
2774 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2775 scan += UTF8SKIP(scan);
2780 PL_reg_flags |= RF_tainted;
2781 while (scan < loceol && isSPACE_LC(*scan))
2785 PL_reg_flags |= RF_tainted;
2787 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2788 scan += UTF8SKIP(scan);
2793 while (scan < loceol && !isSPACE(*scan))
2798 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2799 scan += UTF8SKIP(scan);
2804 PL_reg_flags |= RF_tainted;
2805 while (scan < loceol && !isSPACE_LC(*scan))
2809 PL_reg_flags |= RF_tainted;
2811 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2812 scan += UTF8SKIP(scan);
2817 while (scan < loceol && isDIGIT(*scan))
2822 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2823 scan += UTF8SKIP(scan);
2829 while (scan < loceol && !isDIGIT(*scan))
2834 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2835 scan += UTF8SKIP(scan);
2839 default: /* Called on something of 0 width. */
2840 break; /* So match right here or not at all. */
2846 c = scan - PL_reginput;
2851 SV *prop = sv_newmortal();
2854 PerlIO_printf(Perl_debug_log,
2855 "%*s %s can match %ld times out of %ld...\n",
2856 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2863 - regrepeat_hard - repeatedly match something, report total lenth and length
2865 * The repeater is supposed to have constant length.
2869 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
2872 register char *scan;
2873 register char *start;
2874 register char *loceol = PL_regeol;
2876 I32 count = 0, res = 1;
2881 start = PL_reginput;
2883 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2886 while (start < PL_reginput) {
2888 start += UTF8SKIP(start);
2899 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2901 *lp = l = PL_reginput - start;
2902 if (max != REG_INFTY && l*max < loceol - scan)
2903 loceol = scan + l*max;
2916 - reginclass - determine if a character falls into a character class
2920 S_reginclass(pTHX_ register char *p, register I32 c)
2927 if (ANYOF_TEST(p, c))
2929 else if (flags & ANYOF_FOLD) {
2931 if (flags & ANYOF_LOCALE) {
2932 PL_reg_flags |= RF_tainted;
2933 cf = PL_fold_locale[c];
2937 if (ANYOF_TEST(p, cf))
2941 if (!match && (flags & ANYOF_ISA)) {
2942 PL_reg_flags |= RF_tainted;
2944 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2945 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2946 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2947 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2953 return (flags & ANYOF_INVERT) ? !match : match;
2957 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
2960 char flags = ARG1(f);
2962 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2964 if (swash_fetch(sv, p))
2966 else if (flags & ANYOF_FOLD) {
2969 if (flags & ANYOF_LOCALE) {
2970 PL_reg_flags |= RF_tainted;
2971 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2974 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2975 if (swash_fetch(sv, tmpbuf))
2979 if (!match && (flags & ANYOF_ISA)) {
2980 PL_reg_flags |= RF_tainted;
2982 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2983 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2984 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2985 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2991 return (flags & ANYOF_INVERT) ? !match : match;
2995 S_reghop(pTHX_ U8 *s, I32 off)
2999 while (off-- && s < (U8*)PL_regeol)
3004 if (s > (U8*)PL_bostr) {
3007 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3009 } /* XXX could check well-formedness here */
3017 S_reghopmaybe(pTHX_ U8* s, I32 off)
3021 while (off-- && s < (U8*)PL_regeol)
3028 if (s > (U8*)PL_bostr) {
3031 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3033 } /* XXX could check well-formedness here */