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
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-2001, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGEXEC_C
85 #define RF_tainted 1 /* tainted information used? */
86 #define RF_warned 2 /* warned about big count? */
87 #define RF_evaled 4 /* Did an EVAL with setting? */
88 #define RF_utf8 8 /* String contains multibyte chars? */
90 #define UTF (PL_reg_flags & RF_utf8)
92 #define RS_init 1 /* eval environment created */
93 #define RS_set 2 /* replsv value is set */
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? 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) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? 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))
113 #define HOPBACK(pos, off) ( \
114 (UTF && PL_reg_match_utf8) \
115 ? reghopmaybe((U8*)pos, -off) \
116 : (pos - off >= PL_bostr) \
120 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
122 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
123 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
124 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
125 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
126 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
127 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
129 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
131 /* for use after a quantifier and before an EXACT-like node -- japhy */
132 #define JUMPABLE(rn) ( \
133 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
134 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
135 OP(rn) == PLUS || OP(rn) == MINMOD || \
136 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
139 #define HAS_TEXT(rn) ( \
140 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
143 #define FIND_NEXT_IMPT(rn) STMT_START { \
144 while (JUMPABLE(rn)) \
145 if (OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
146 PL_regkind[(U8)OP(rn)] == CURLY) \
147 rn = NEXTOPER(NEXTOPER(rn)); \
148 else if (OP(rn) == PLUS) \
150 else rn += NEXT_OFF(rn); \
153 static void restore_pos(pTHX_ void *arg);
156 S_regcppush(pTHX_ I32 parenfloor)
158 int retval = PL_savestack_ix;
159 #define REGCP_PAREN_ELEMS 4
160 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
163 if (paren_elems_to_push < 0)
164 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
166 #define REGCP_OTHER_ELEMS 6
167 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
168 for (p = PL_regsize; p > parenfloor; p--) {
169 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
170 SSPUSHINT(PL_regendp[p]);
171 SSPUSHINT(PL_regstartp[p]);
172 SSPUSHPTR(PL_reg_start_tmp[p]);
175 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
176 SSPUSHINT(PL_regsize);
177 SSPUSHINT(*PL_reglastparen);
178 SSPUSHINT(*PL_reglastcloseparen);
179 SSPUSHPTR(PL_reginput);
180 #define REGCP_FRAME_ELEMS 2
181 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
182 * are needed for the regexp context stack bookkeeping. */
183 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
184 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
189 /* These are needed since we do not localize EVAL nodes: */
190 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
191 " Setting an EVAL scope, savestack=%"IVdf"\n", \
192 (IV)PL_savestack_ix)); cp = PL_savestack_ix
194 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
195 PerlIO_printf(Perl_debug_log, \
196 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
197 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
207 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
209 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
210 i = SSPOPINT; /* Parentheses elements to pop. */
211 input = (char *) SSPOPPTR;
212 *PL_reglastcloseparen = SSPOPINT;
213 *PL_reglastparen = SSPOPINT;
214 PL_regsize = SSPOPINT;
216 /* Now restore the parentheses context. */
217 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
218 i > 0; i -= REGCP_PAREN_ELEMS) {
219 paren = (U32)SSPOPINT;
220 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
221 PL_regstartp[paren] = SSPOPINT;
223 if (paren <= *PL_reglastparen)
224 PL_regendp[paren] = tmps;
226 PerlIO_printf(Perl_debug_log,
227 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
228 (UV)paren, (IV)PL_regstartp[paren],
229 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
230 (IV)PL_regendp[paren],
231 (paren > *PL_reglastparen ? "(no)" : ""));
235 if (*PL_reglastparen + 1 <= PL_regnpar) {
236 PerlIO_printf(Perl_debug_log,
237 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
238 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
242 /* It would seem that the similar code in regtry()
243 * already takes care of this, and in fact it is in
244 * a better location to since this code can #if 0-ed out
245 * but the code in regtry() is needed or otherwise tests
246 * requiring null fields (pat.t#187 and split.t#{13,14}
247 * (as of patchlevel 7877) will fail. Then again,
248 * this code seems to be necessary or otherwise
249 * building DynaLoader will fail:
250 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
252 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
253 if (paren > PL_regsize)
254 PL_regstartp[paren] = -1;
255 PL_regendp[paren] = -1;
262 S_regcp_set_to(pTHX_ I32 ss)
264 I32 tmp = PL_savestack_ix;
266 PL_savestack_ix = ss;
268 PL_savestack_ix = tmp;
272 typedef struct re_cc_state
276 struct re_cc_state *prev;
281 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
283 #define TRYPAREN(paren, n, input) { \
286 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
287 PL_regendp[paren] = input - PL_bostr; \
290 PL_regendp[paren] = -1; \
292 if (regmatch(next)) \
295 PL_regendp[paren] = -1; \
300 * pregexec and friends
304 - pregexec - match a regexp against a string
307 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
308 char *strbeg, I32 minend, SV *screamer, U32 nosave)
309 /* strend: pointer to null at end of string */
310 /* strbeg: real beginning of string */
311 /* minend: end of match must be >=minend after stringarg. */
312 /* nosave: For optimizations. */
315 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
316 nosave ? 0 : REXEC_COPY_STR);
320 S_cache_re(pTHX_ regexp *prog)
322 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
324 PL_regprogram = prog->program;
326 PL_regnpar = prog->nparens;
327 PL_regdata = prog->data;
332 * Need to implement the following flags for reg_anch:
334 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
336 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
337 * INTUIT_AUTORITATIVE_ML
338 * INTUIT_ONCE_NOML - Intuit can match in one location only.
341 * Another flag for this function: SECOND_TIME (so that float substrs
342 * with giant delta may be not rechecked).
345 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
347 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
348 Otherwise, only SvCUR(sv) is used to get strbeg. */
350 /* XXXX We assume that strpos is strbeg unless sv. */
352 /* XXXX Some places assume that there is a fixed substring.
353 An update may be needed if optimizer marks as "INTUITable"
354 RExen without fixed substrings. Similarly, it is assumed that
355 lengths of all the strings are no more than minlen, thus they
356 cannot come from lookahead.
357 (Or minlen should take into account lookahead.) */
359 /* A failure to find a constant substring means that there is no need to make
360 an expensive call to REx engine, thus we celebrate a failure. Similarly,
361 finding a substring too deep into the string means that less calls to
362 regtry() should be needed.
364 REx compiler's optimizer found 4 possible hints:
365 a) Anchored substring;
367 c) Whether we are anchored (beginning-of-line or \G);
368 d) First node (of those at offset 0) which may distingush positions;
369 We use a)b)d) and multiline-part of c), and try to find a position in the
370 string which does not contradict any of them.
373 /* Most of decisions we do here should have been done at compile time.
374 The nodes of the REx which we used for the search should have been
375 deleted from the finite automaton. */
378 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
379 char *strend, U32 flags, re_scream_pos_data *data)
381 register I32 start_shift = 0;
382 /* Should be nonnegative! */
383 register I32 end_shift = 0;
389 register char *other_last = Nullch; /* other substr checked before this */
390 char *check_at = Nullch; /* check substr found at this pos */
392 char *i_strpos = strpos;
395 dsv = newSVpvn("", 0);
399 char*s = UTF ? sv_uni_display(dsv, sv, 60, 0) : strpos;
400 int len = UTF ? strlen(s) : strend - strpos;
403 PerlIO_printf(Perl_debug_log,
404 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
405 PL_colors[4],PL_colors[5],PL_colors[0],
408 (strlen(prog->precomp) > 60 ? "..." : ""),
410 (int)(len > 60 ? 60 : len),
412 (len > 60 ? "..." : "")
416 if (prog->reganch & ROPT_UTF8)
417 PL_reg_flags |= RF_utf8;
419 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
420 DEBUG_r(PerlIO_printf(Perl_debug_log,
421 "String too short... [re_intuit_start]\n"));
424 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
426 check = prog->check_substr;
427 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
428 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
429 || ( (prog->reganch & ROPT_ANCH_BOL)
430 && !PL_multiline ) ); /* Check after \n? */
433 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
434 | ROPT_IMPLICIT)) /* not a real BOL */
435 /* SvCUR is not set on references: SvRV and SvPVX overlap */
437 && (strpos != strbeg)) {
438 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
441 if (prog->check_offset_min == prog->check_offset_max &&
442 !(prog->reganch & ROPT_CANY_SEEN)) {
443 /* Substring at constant offset from beg-of-str... */
446 s = HOP3c(strpos, prog->check_offset_min, strend);
448 slen = SvCUR(check); /* >= 1 */
450 if ( strend - s > slen || strend - s < slen - 1
451 || (strend - s == slen && strend[-1] != '\n')) {
452 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
455 /* Now should match s[0..slen-2] */
457 if (slen && (*SvPVX(check) != *s
459 && memNE(SvPVX(check), s, slen)))) {
461 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
465 else if (*SvPVX(check) != *s
466 || ((slen = SvCUR(check)) > 1
467 && memNE(SvPVX(check), s, slen)))
469 goto success_at_start;
472 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
474 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
475 end_shift = prog->minlen - start_shift -
476 CHR_SVLEN(check) + (SvTAIL(check) != 0);
478 I32 end = prog->check_offset_max + CHR_SVLEN(check)
479 - (SvTAIL(check) != 0);
480 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
482 if (end_shift < eshift)
486 else { /* Can match at random position */
489 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
490 /* Should be nonnegative! */
491 end_shift = prog->minlen - start_shift -
492 CHR_SVLEN(check) + (SvTAIL(check) != 0);
495 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
497 Perl_croak(aTHX_ "panic: end_shift");
501 /* Find a possible match in the region s..strend by looking for
502 the "check" substring in the region corrected by start/end_shift. */
503 if (flags & REXEC_SCREAM) {
504 I32 p = -1; /* Internal iterator of scream. */
505 I32 *pp = data ? data->scream_pos : &p;
507 if (PL_screamfirst[BmRARE(check)] >= 0
508 || ( BmRARE(check) == '\n'
509 && (BmPREVIOUS(check) == SvCUR(check) - 1)
511 s = screaminstr(sv, check,
512 start_shift + (s - strbeg), end_shift, pp, 0);
516 *data->scream_olds = s;
518 else if (prog->reganch & ROPT_CANY_SEEN)
519 s = fbm_instr((U8*)(s + start_shift),
520 (U8*)(strend - end_shift),
521 check, PL_multiline ? FBMrf_MULTILINE : 0);
523 s = fbm_instr(HOP3(s, start_shift, strend),
524 HOP3(strend, -end_shift, strbeg),
525 check, PL_multiline ? FBMrf_MULTILINE : 0);
527 /* Update the count-of-usability, remove useless subpatterns,
530 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
531 (s ? "Found" : "Did not find"),
532 ((check == prog->anchored_substr) ? "anchored" : "floating"),
534 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
536 PL_colors[1], (SvTAIL(check) ? "$" : ""),
537 (s ? " at offset " : "...\n") ) );
544 /* Finish the diagnostic message */
545 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
547 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
548 Start with the other substr.
549 XXXX no SCREAM optimization yet - and a very coarse implementation
550 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
551 *always* match. Probably should be marked during compile...
552 Probably it is right to do no SCREAM here...
555 if (prog->float_substr && prog->anchored_substr) {
556 /* Take into account the "other" substring. */
557 /* XXXX May be hopelessly wrong for UTF... */
560 if (check == prog->float_substr) {
563 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
566 t = s - prog->check_offset_max;
567 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
568 && (!(prog->reganch & ROPT_UTF8)
569 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
574 t = HOP3c(t, prog->anchored_offset, strend);
575 if (t < other_last) /* These positions already checked */
577 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
580 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
581 /* On end-of-str: see comment below. */
582 s = fbm_instr((unsigned char*)t,
583 HOP3(HOP3(last1, prog->anchored_offset, strend)
584 + SvCUR(prog->anchored_substr),
585 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
586 prog->anchored_substr,
587 PL_multiline ? FBMrf_MULTILINE : 0);
588 DEBUG_r(PerlIO_printf(Perl_debug_log,
589 "%s anchored substr `%s%.*s%s'%s",
590 (s ? "Found" : "Contradicts"),
592 (int)(SvCUR(prog->anchored_substr)
593 - (SvTAIL(prog->anchored_substr)!=0)),
594 SvPVX(prog->anchored_substr),
595 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
597 if (last1 >= last2) {
598 DEBUG_r(PerlIO_printf(Perl_debug_log,
599 ", giving up...\n"));
602 DEBUG_r(PerlIO_printf(Perl_debug_log,
603 ", trying floating at offset %ld...\n",
604 (long)(HOP3c(s1, 1, strend) - i_strpos)));
605 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
606 s = HOP3c(last, 1, strend);
610 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
611 (long)(s - i_strpos)));
612 t = HOP3c(s, -prog->anchored_offset, strbeg);
613 other_last = HOP3c(s, 1, strend);
621 else { /* Take into account the floating substring. */
625 t = HOP3c(s, -start_shift, strbeg);
627 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
628 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
629 last = HOP3c(t, prog->float_max_offset, strend);
630 s = HOP3c(t, prog->float_min_offset, strend);
633 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
634 /* fbm_instr() takes into account exact value of end-of-str
635 if the check is SvTAIL(ed). Since false positives are OK,
636 and end-of-str is not later than strend we are OK. */
637 s = fbm_instr((unsigned char*)s,
638 (unsigned char*)last + SvCUR(prog->float_substr)
639 - (SvTAIL(prog->float_substr)!=0),
640 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
641 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
642 (s ? "Found" : "Contradicts"),
644 (int)(SvCUR(prog->float_substr)
645 - (SvTAIL(prog->float_substr)!=0)),
646 SvPVX(prog->float_substr),
647 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
650 DEBUG_r(PerlIO_printf(Perl_debug_log,
651 ", giving up...\n"));
654 DEBUG_r(PerlIO_printf(Perl_debug_log,
655 ", trying anchored starting at offset %ld...\n",
656 (long)(s1 + 1 - i_strpos)));
658 s = HOP3c(t, 1, strend);
662 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
663 (long)(s - i_strpos)));
664 other_last = s; /* Fix this later. --Hugo */
673 t = s - prog->check_offset_max;
674 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
675 && (!(prog->reganch & ROPT_UTF8)
676 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
678 /* Fixed substring is found far enough so that the match
679 cannot start at strpos. */
681 if (ml_anch && t[-1] != '\n') {
682 /* Eventually fbm_*() should handle this, but often
683 anchored_offset is not 0, so this check will not be wasted. */
684 /* XXXX In the code below we prefer to look for "^" even in
685 presence of anchored substrings. And we search even
686 beyond the found float position. These pessimizations
687 are historical artefacts only. */
689 while (t < strend - prog->minlen) {
691 if (t < check_at - prog->check_offset_min) {
692 if (prog->anchored_substr) {
693 /* Since we moved from the found position,
694 we definitely contradict the found anchored
695 substr. Due to the above check we do not
696 contradict "check" substr.
697 Thus we can arrive here only if check substr
698 is float. Redo checking for "other"=="fixed".
701 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
702 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
703 goto do_other_anchored;
705 /* We don't contradict the found floating substring. */
706 /* XXXX Why not check for STCLASS? */
708 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
709 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
712 /* Position contradicts check-string */
713 /* XXXX probably better to look for check-string
714 than for "\n", so one should lower the limit for t? */
715 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
716 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
717 other_last = strpos = s = t + 1;
722 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
723 PL_colors[0],PL_colors[1]));
727 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
728 PL_colors[0],PL_colors[1]));
732 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
735 /* The found string does not prohibit matching at strpos,
736 - no optimization of calling REx engine can be performed,
737 unless it was an MBOL and we are not after MBOL,
738 or a future STCLASS check will fail this. */
740 /* Even in this situation we may use MBOL flag if strpos is offset
741 wrt the start of the string. */
742 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
743 && (strpos != strbeg) && strpos[-1] != '\n'
744 /* May be due to an implicit anchor of m{.*foo} */
745 && !(prog->reganch & ROPT_IMPLICIT))
750 DEBUG_r( if (ml_anch)
751 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
752 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
755 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
756 && prog->check_substr /* Could be deleted already */
757 && --BmUSEFUL(prog->check_substr) < 0
758 && prog->check_substr == prog->float_substr)
760 /* If flags & SOMETHING - do not do it many times on the same match */
761 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
762 SvREFCNT_dec(prog->check_substr);
763 prog->check_substr = Nullsv; /* disable */
764 prog->float_substr = Nullsv; /* clear */
765 check = Nullsv; /* abort */
767 /* XXXX This is a remnant of the old implementation. It
768 looks wasteful, since now INTUIT can use many
770 prog->reganch &= ~RE_USE_INTUIT;
777 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
778 if (prog->regstclass) {
779 /* minlen == 0 is possible if regstclass is \b or \B,
780 and the fixed substr is ''$.
781 Since minlen is already taken into account, s+1 is before strend;
782 accidentally, minlen >= 1 guaranties no false positives at s + 1
783 even for \b or \B. But (minlen? 1 : 0) below assumes that
784 regstclass does not come from lookahead... */
785 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
786 This leaves EXACTF only, which is dealt with in find_byclass(). */
787 U8* str = (U8*)STRING(prog->regstclass);
788 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
789 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
791 char *endpos = (prog->anchored_substr || ml_anch)
792 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
793 : (prog->float_substr
794 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
797 char *startpos = strbeg;
800 if (prog->reganch & ROPT_UTF8) {
801 PL_regdata = prog->data;
804 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
809 if (endpos == strend) {
810 DEBUG_r( PerlIO_printf(Perl_debug_log,
811 "Could not match STCLASS...\n") );
814 DEBUG_r( PerlIO_printf(Perl_debug_log,
815 "This position contradicts STCLASS...\n") );
816 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
818 /* Contradict one of substrings */
819 if (prog->anchored_substr) {
820 if (prog->anchored_substr == check) {
821 DEBUG_r( what = "anchored" );
823 s = HOP3c(t, 1, strend);
824 if (s + start_shift + end_shift > strend) {
825 /* XXXX Should be taken into account earlier? */
826 DEBUG_r( PerlIO_printf(Perl_debug_log,
827 "Could not match STCLASS...\n") );
832 DEBUG_r( PerlIO_printf(Perl_debug_log,
833 "Looking for %s substr starting at offset %ld...\n",
834 what, (long)(s + start_shift - i_strpos)) );
837 /* Have both, check_string is floating */
838 if (t + start_shift >= check_at) /* Contradicts floating=check */
839 goto retry_floating_check;
840 /* Recheck anchored substring, but not floating... */
844 DEBUG_r( PerlIO_printf(Perl_debug_log,
845 "Looking for anchored substr starting at offset %ld...\n",
846 (long)(other_last - i_strpos)) );
847 goto do_other_anchored;
849 /* Another way we could have checked stclass at the
850 current position only: */
855 DEBUG_r( PerlIO_printf(Perl_debug_log,
856 "Looking for /%s^%s/m starting at offset %ld...\n",
857 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
860 if (!prog->float_substr) /* Could have been deleted */
862 /* Check is floating subtring. */
863 retry_floating_check:
864 t = check_at - start_shift;
865 DEBUG_r( what = "floating" );
866 goto hop_and_restart;
869 DEBUG_r(PerlIO_printf(Perl_debug_log,
870 "By STCLASS: moving %ld --> %ld\n",
871 (long)(t - i_strpos), (long)(s - i_strpos))
875 DEBUG_r(PerlIO_printf(Perl_debug_log,
876 "Does not contradict STCLASS...\n");
881 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
882 PL_colors[4], (check ? "Guessed" : "Giving up"),
883 PL_colors[5], (long)(s - i_strpos)) );
886 fail_finish: /* Substring not found */
887 if (prog->check_substr) /* could be removed already */
888 BmUSEFUL(prog->check_substr) += 5; /* hooray */
890 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
891 PL_colors[4],PL_colors[5]));
895 /* We know what class REx starts with. Try to find this position... */
897 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
899 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
905 register I32 tmp = 1; /* Scratch variable? */
906 register bool do_utf8 = PL_reg_match_utf8;
908 /* We know what class it must start with. */
912 if (reginclass(c, (U8*)s, do_utf8)) {
913 if (tmp && (norun || regtry(prog, s)))
920 s += do_utf8 ? UTF8SKIP(s) : 1;
925 if (tmp && (norun || regtry(prog, s)))
937 U8 tmpbuf1[UTF8_MAXLEN*2+1];
938 U8 tmpbuf2[UTF8_MAXLEN*2+1];
940 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
941 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
943 c1 = utf8_to_uvuni(tmpbuf1, 0);
944 c2 = utf8_to_uvuni(tmpbuf2, 0);
955 c2 = PL_fold_locale[c1];
960 e = s; /* Due to minlen logic of intuit() */
966 if ( utf8_to_uvchr((U8*)s, &len) == c1
973 UV c = utf8_to_uvchr((U8*)s, &len);
974 if ( (c == c1 || c == c2) && regtry(prog, s) )
983 && (ln == 1 || !(OP(c) == EXACTF
985 : ibcmp_locale(s, m, ln)))
986 && (norun || regtry(prog, s)) )
992 if ( (*(U8*)s == c1 || *(U8*)s == c2)
993 && (ln == 1 || !(OP(c) == EXACTF
995 : ibcmp_locale(s, m, ln)))
996 && (norun || regtry(prog, s)) )
1003 PL_reg_flags |= RF_tainted;
1010 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1013 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1015 tmp = ((OP(c) == BOUND ?
1016 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1017 LOAD_UTF8_CHARCLASS(alnum,"a");
1018 while (s < strend) {
1019 if (tmp == !(OP(c) == BOUND ?
1020 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1021 isALNUM_LC_utf8((U8*)s)))
1024 if ((norun || regtry(prog, s)))
1031 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1032 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1033 while (s < strend) {
1035 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1037 if ((norun || regtry(prog, s)))
1043 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1047 PL_reg_flags |= RF_tainted;
1054 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1057 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1059 tmp = ((OP(c) == NBOUND ?
1060 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1061 LOAD_UTF8_CHARCLASS(alnum,"a");
1062 while (s < strend) {
1063 if (tmp == !(OP(c) == NBOUND ?
1064 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1065 isALNUM_LC_utf8((U8*)s)))
1067 else if ((norun || regtry(prog, s)))
1073 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1074 tmp = ((OP(c) == NBOUND ?
1075 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1076 while (s < strend) {
1078 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1080 else if ((norun || regtry(prog, s)))
1085 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1090 LOAD_UTF8_CHARCLASS(alnum,"a");
1091 while (s < strend) {
1092 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1093 if (tmp && (norun || regtry(prog, s)))
1104 while (s < strend) {
1106 if (tmp && (norun || regtry(prog, s)))
1118 PL_reg_flags |= RF_tainted;
1120 while (s < strend) {
1121 if (isALNUM_LC_utf8((U8*)s)) {
1122 if (tmp && (norun || regtry(prog, s)))
1133 while (s < strend) {
1134 if (isALNUM_LC(*s)) {
1135 if (tmp && (norun || regtry(prog, s)))
1148 LOAD_UTF8_CHARCLASS(alnum,"a");
1149 while (s < strend) {
1150 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1151 if (tmp && (norun || regtry(prog, s)))
1162 while (s < strend) {
1164 if (tmp && (norun || regtry(prog, s)))
1176 PL_reg_flags |= RF_tainted;
1178 while (s < strend) {
1179 if (!isALNUM_LC_utf8((U8*)s)) {
1180 if (tmp && (norun || regtry(prog, s)))
1191 while (s < strend) {
1192 if (!isALNUM_LC(*s)) {
1193 if (tmp && (norun || regtry(prog, s)))
1206 LOAD_UTF8_CHARCLASS(space," ");
1207 while (s < strend) {
1208 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1209 if (tmp && (norun || regtry(prog, s)))
1220 while (s < strend) {
1222 if (tmp && (norun || regtry(prog, s)))
1234 PL_reg_flags |= RF_tainted;
1236 while (s < strend) {
1237 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1238 if (tmp && (norun || regtry(prog, s)))
1249 while (s < strend) {
1250 if (isSPACE_LC(*s)) {
1251 if (tmp && (norun || regtry(prog, s)))
1264 LOAD_UTF8_CHARCLASS(space," ");
1265 while (s < strend) {
1266 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1267 if (tmp && (norun || regtry(prog, s)))
1278 while (s < strend) {
1280 if (tmp && (norun || regtry(prog, s)))
1292 PL_reg_flags |= RF_tainted;
1294 while (s < strend) {
1295 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1296 if (tmp && (norun || regtry(prog, s)))
1307 while (s < strend) {
1308 if (!isSPACE_LC(*s)) {
1309 if (tmp && (norun || regtry(prog, s)))
1322 LOAD_UTF8_CHARCLASS(digit,"0");
1323 while (s < strend) {
1324 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1325 if (tmp && (norun || regtry(prog, s)))
1336 while (s < strend) {
1338 if (tmp && (norun || regtry(prog, s)))
1350 PL_reg_flags |= RF_tainted;
1352 while (s < strend) {
1353 if (isDIGIT_LC_utf8((U8*)s)) {
1354 if (tmp && (norun || regtry(prog, s)))
1365 while (s < strend) {
1366 if (isDIGIT_LC(*s)) {
1367 if (tmp && (norun || regtry(prog, s)))
1380 LOAD_UTF8_CHARCLASS(digit,"0");
1381 while (s < strend) {
1382 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1383 if (tmp && (norun || regtry(prog, s)))
1394 while (s < strend) {
1396 if (tmp && (norun || regtry(prog, s)))
1408 PL_reg_flags |= RF_tainted;
1410 while (s < strend) {
1411 if (!isDIGIT_LC_utf8((U8*)s)) {
1412 if (tmp && (norun || regtry(prog, s)))
1423 while (s < strend) {
1424 if (!isDIGIT_LC(*s)) {
1425 if (tmp && (norun || regtry(prog, s)))
1437 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1446 - regexec_flags - match a regexp against a string
1449 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1450 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1451 /* strend: pointer to null at end of string */
1452 /* strbeg: real beginning of string */
1453 /* minend: end of match must be >=minend after stringarg. */
1454 /* data: May be used for some additional optimizations. */
1455 /* nosave: For optimizations. */
1458 register regnode *c;
1459 register char *startpos = stringarg;
1460 I32 minlen; /* must match at least this many chars */
1461 I32 dontbother = 0; /* how many characters not to try at end */
1462 /* I32 start_shift = 0; */ /* Offset of the start to find
1463 constant substr. */ /* CC */
1464 I32 end_shift = 0; /* Same for the end. */ /* CC */
1465 I32 scream_pos = -1; /* Internal iterator of scream. */
1467 SV* oreplsv = GvSV(PL_replgv);
1468 bool do_utf8 = DO_UTF8(sv);
1472 dsv = newSVpvn("", 0);
1479 PL_regnarrate = DEBUG_r_TEST;
1482 /* Be paranoid... */
1483 if (prog == NULL || startpos == NULL) {
1484 Perl_croak(aTHX_ "NULL regexp parameter");
1488 minlen = prog->minlen;
1489 if (strend - startpos < minlen) {
1490 DEBUG_r(PerlIO_printf(Perl_debug_log,
1491 "String too short [regexec_flags]...\n"));
1495 /* Check validity of program. */
1496 if (UCHARAT(prog->program) != REG_MAGIC) {
1497 Perl_croak(aTHX_ "corrupted regexp program");
1501 PL_reg_eval_set = 0;
1504 if (prog->reganch & ROPT_UTF8)
1505 PL_reg_flags |= RF_utf8;
1507 /* Mark beginning of line for ^ and lookbehind. */
1508 PL_regbol = startpos;
1512 /* Mark end of line for $ (and such) */
1515 /* see how far we have to get to not match where we matched before */
1516 PL_regtill = startpos+minend;
1518 /* We start without call_cc context. */
1521 /* If there is a "must appear" string, look for it. */
1524 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1527 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1528 PL_reg_ganch = startpos;
1529 else if (sv && SvTYPE(sv) >= SVt_PVMG
1531 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1532 && mg->mg_len >= 0) {
1533 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1534 if (prog->reganch & ROPT_ANCH_GPOS) {
1535 if (s > PL_reg_ganch)
1540 else /* pos() not defined */
1541 PL_reg_ganch = strbeg;
1544 if (do_utf8 == (UTF!=0) &&
1545 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1546 re_scream_pos_data d;
1548 d.scream_olds = &scream_olds;
1549 d.scream_pos = &scream_pos;
1550 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1552 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1553 goto phooey; /* not present */
1558 char *s = do_utf8 ? sv_uni_display(dsv, sv, 60, 0) : startpos;
1559 int len = do_utf8 ? strlen(s) : strend - startpos;
1562 PerlIO_printf(Perl_debug_log,
1563 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1564 PL_colors[4],PL_colors[5],PL_colors[0],
1567 (strlen(prog->precomp) > 60 ? "..." : ""),
1569 (int)(len > 60 ? 60 : len),
1571 (len > 60 ? "..." : "")
1575 /* Simplest case: anchored match need be tried only once. */
1576 /* [unless only anchor is BOL and multiline is set] */
1577 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1578 if (s == startpos && regtry(prog, startpos))
1580 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1581 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1586 dontbother = minlen - 1;
1587 end = HOP3c(strend, -dontbother, strbeg) - 1;
1588 /* for multiline we only have to try after newlines */
1589 if (prog->check_substr) {
1593 if (regtry(prog, s))
1598 if (prog->reganch & RE_USE_INTUIT) {
1599 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1610 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1611 if (regtry(prog, s))
1618 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1619 if (regtry(prog, PL_reg_ganch))
1624 /* Messy cases: unanchored match. */
1625 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1626 /* we have /x+whatever/ */
1627 /* it must be a one character string (XXXX Except UTF?) */
1628 char ch = SvPVX(prog->anchored_substr)[0];
1634 while (s < strend) {
1636 DEBUG_r( did_match = 1 );
1637 if (regtry(prog, s)) goto got_it;
1639 while (s < strend && *s == ch)
1646 while (s < strend) {
1648 DEBUG_r( did_match = 1 );
1649 if (regtry(prog, s)) goto got_it;
1651 while (s < strend && *s == ch)
1657 DEBUG_r(if (!did_match)
1658 PerlIO_printf(Perl_debug_log,
1659 "Did not find anchored character...\n")
1663 else if (do_utf8 == (UTF!=0) &&
1664 (prog->anchored_substr != Nullsv
1665 || (prog->float_substr != Nullsv
1666 && prog->float_max_offset < strend - s))) {
1667 SV *must = prog->anchored_substr
1668 ? prog->anchored_substr : prog->float_substr;
1670 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1672 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1673 char *last = HOP3c(strend, /* Cannot start after this */
1674 -(I32)(CHR_SVLEN(must)
1675 - (SvTAIL(must) != 0) + back_min), strbeg);
1676 char *last1; /* Last position checked before */
1682 last1 = HOPc(s, -1);
1684 last1 = s - 1; /* bogus */
1686 /* XXXX check_substr already used to find `s', can optimize if
1687 check_substr==must. */
1689 dontbother = end_shift;
1690 strend = HOPc(strend, -dontbother);
1691 while ( (s <= last) &&
1692 ((flags & REXEC_SCREAM)
1693 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1694 end_shift, &scream_pos, 0))
1695 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1696 (unsigned char*)strend, must,
1697 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1698 DEBUG_r( did_match = 1 );
1699 if (HOPc(s, -back_max) > last1) {
1700 last1 = HOPc(s, -back_min);
1701 s = HOPc(s, -back_max);
1704 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1706 last1 = HOPc(s, -back_min);
1710 while (s <= last1) {
1711 if (regtry(prog, s))
1717 while (s <= last1) {
1718 if (regtry(prog, s))
1724 DEBUG_r(if (!did_match)
1725 PerlIO_printf(Perl_debug_log,
1726 "Did not find %s substr `%s%.*s%s'%s...\n",
1727 ((must == prog->anchored_substr)
1728 ? "anchored" : "floating"),
1730 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1732 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1736 else if ((c = prog->regstclass)) {
1737 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1738 /* don't bother with what can't match */
1739 strend = HOPc(strend, -(minlen - 1));
1741 SV *prop = sv_newmortal();
1743 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), UTF ? sv_uni_display(dsv, sv, 60, 0) : s);
1745 if (find_byclass(prog, c, s, strend, startpos, 0))
1747 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1751 if (prog->float_substr != Nullsv) { /* Trim the end. */
1754 if (flags & REXEC_SCREAM) {
1755 last = screaminstr(sv, prog->float_substr, s - strbeg,
1756 end_shift, &scream_pos, 1); /* last one */
1758 last = scream_olds; /* Only one occurrence. */
1762 char *little = SvPV(prog->float_substr, len);
1764 if (SvTAIL(prog->float_substr)) {
1765 if (memEQ(strend - len + 1, little, len - 1))
1766 last = strend - len + 1;
1767 else if (!PL_multiline)
1768 last = memEQ(strend - len, little, len)
1769 ? strend - len : Nullch;
1775 last = rninstr(s, strend, little, little + len);
1777 last = strend; /* matching `$' */
1781 DEBUG_r(PerlIO_printf(Perl_debug_log,
1782 "%sCan't trim the tail, match fails (should not happen)%s\n",
1783 PL_colors[4],PL_colors[5]));
1784 goto phooey; /* Should not happen! */
1786 dontbother = strend - last + prog->float_min_offset;
1788 if (minlen && (dontbother < minlen))
1789 dontbother = minlen - 1;
1790 strend -= dontbother; /* this one's always in bytes! */
1791 /* We don't know much -- general case. */
1794 if (regtry(prog, s))
1803 if (regtry(prog, s))
1805 } while (s++ < strend);
1813 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1815 if (PL_reg_eval_set) {
1816 /* Preserve the current value of $^R */
1817 if (oreplsv != GvSV(PL_replgv))
1818 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1819 restored, the value remains
1821 restore_pos(aTHX_ 0);
1824 /* make sure $`, $&, $', and $digit will work later */
1825 if ( !(flags & REXEC_NOT_FIRST) ) {
1826 if (RX_MATCH_COPIED(prog)) {
1827 Safefree(prog->subbeg);
1828 RX_MATCH_COPIED_off(prog);
1830 if (flags & REXEC_COPY_STR) {
1831 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1833 s = savepvn(strbeg, i);
1836 RX_MATCH_COPIED_on(prog);
1839 prog->subbeg = strbeg;
1840 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1847 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1848 PL_colors[4],PL_colors[5]));
1849 if (PL_reg_eval_set)
1850 restore_pos(aTHX_ 0);
1855 - regtry - try match at specific point
1857 STATIC I32 /* 0 failure, 1 success */
1858 S_regtry(pTHX_ regexp *prog, char *startpos)
1866 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1868 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1871 PL_reg_eval_set = RS_init;
1873 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1874 (IV)(PL_stack_sp - PL_stack_base));
1876 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1877 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1878 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1880 /* Apparently this is not needed, judging by wantarray. */
1881 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1882 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1885 /* Make $_ available to executed code. */
1886 if (PL_reg_sv != DEFSV) {
1887 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1892 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1893 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1894 /* prepare for quick setting of pos */
1895 sv_magic(PL_reg_sv, (SV*)0,
1896 PERL_MAGIC_regex_global, Nullch, 0);
1897 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1901 PL_reg_oldpos = mg->mg_len;
1902 SAVEDESTRUCTOR_X(restore_pos, 0);
1904 if (!PL_reg_curpm) {
1905 Newz(22,PL_reg_curpm, 1, PMOP);
1908 SV* repointer = newSViv(0);
1909 /* so we know which PL_regex_padav element is PL_reg_curpm */
1910 SvFLAGS(repointer) |= SVf_BREAK;
1911 av_push(PL_regex_padav,repointer);
1912 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
1913 PL_regex_pad = AvARRAY(PL_regex_padav);
1917 PM_SETRE(PL_reg_curpm, prog);
1918 PL_reg_oldcurpm = PL_curpm;
1919 PL_curpm = PL_reg_curpm;
1920 if (RX_MATCH_COPIED(prog)) {
1921 /* Here is a serious problem: we cannot rewrite subbeg,
1922 since it may be needed if this match fails. Thus
1923 $` inside (?{}) could fail... */
1924 PL_reg_oldsaved = prog->subbeg;
1925 PL_reg_oldsavedlen = prog->sublen;
1926 RX_MATCH_COPIED_off(prog);
1929 PL_reg_oldsaved = Nullch;
1930 prog->subbeg = PL_bostr;
1931 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1933 prog->startp[0] = startpos - PL_bostr;
1934 PL_reginput = startpos;
1935 PL_regstartp = prog->startp;
1936 PL_regendp = prog->endp;
1937 PL_reglastparen = &prog->lastparen;
1938 PL_reglastcloseparen = &prog->lastcloseparen;
1939 prog->lastparen = 0;
1941 DEBUG_r(PL_reg_starttry = startpos);
1942 if (PL_reg_start_tmpl <= prog->nparens) {
1943 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1944 if(PL_reg_start_tmp)
1945 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1947 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1950 /* XXXX What this code is doing here?!!! There should be no need
1951 to do this again and again, PL_reglastparen should take care of
1954 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1955 * Actually, the code in regcppop() (which Ilya may be meaning by
1956 * PL_reglastparen), is not needed at all by the test suite
1957 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1958 * enough, for building DynaLoader, or otherwise this
1959 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1960 * will happen. Meanwhile, this code *is* needed for the
1961 * above-mentioned test suite tests to succeed. The common theme
1962 * on those tests seems to be returning null fields from matches.
1967 if (prog->nparens) {
1968 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1975 if (regmatch(prog->program + 1)) {
1976 prog->endp[0] = PL_reginput - PL_bostr;
1979 REGCP_UNWIND(lastcp);
1983 #define RE_UNWIND_BRANCH 1
1984 #define RE_UNWIND_BRANCHJ 2
1988 typedef struct { /* XX: makes sense to enlarge it... */
1992 } re_unwind_generic_t;
2005 } re_unwind_branch_t;
2007 typedef union re_unwind_t {
2009 re_unwind_generic_t generic;
2010 re_unwind_branch_t branch;
2013 #define sayYES goto yes
2014 #define sayNO goto no
2015 #define sayYES_FINAL goto yes_final
2016 #define sayYES_LOUD goto yes_loud
2017 #define sayNO_FINAL goto no_final
2018 #define sayNO_SILENT goto do_no
2019 #define saySAME(x) if (x) goto yes; else goto no
2021 #define REPORT_CODE_OFF 24
2024 - regmatch - main matching routine
2026 * Conceptually the strategy is simple: check to see whether the current
2027 * node matches, call self recursively to see whether the rest matches,
2028 * and then act accordingly. In practice we make some effort to avoid
2029 * recursion, in particular by going through "ordinary" nodes (that don't
2030 * need to know whether the rest of the match failed) by a loop instead of
2033 /* [lwall] I've hoisted the register declarations to the outer block in order to
2034 * maybe save a little bit of pushing and popping on the stack. It also takes
2035 * advantage of machines that use a register save mask on subroutine entry.
2037 STATIC I32 /* 0 failure, 1 success */
2038 S_regmatch(pTHX_ regnode *prog)
2040 register regnode *scan; /* Current node. */
2041 regnode *next; /* Next node. */
2042 regnode *inner; /* Next node in internal branch. */
2043 register I32 nextchr; /* renamed nextchr - nextchar colides with
2044 function of same name */
2045 register I32 n; /* no or next */
2046 register I32 ln = 0; /* len or last */
2047 register char *s = Nullch; /* operand or save */
2048 register char *locinput = PL_reginput;
2049 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2050 int minmod = 0, sw = 0, logical = 0;
2053 I32 firstcp = PL_savestack_ix;
2055 register bool do_utf8 = PL_reg_match_utf8;
2061 dsv0 = newSVpvn("", 0);
2063 dsv1 = newSVpvn("", 0);
2065 dsv2 = newSVpvn("", 0);
2072 /* Note that nextchr is a byte even in UTF */
2073 nextchr = UCHARAT(locinput);
2075 while (scan != NULL) {
2078 SV *prop = sv_newmortal();
2079 int docolor = *PL_colors[0];
2080 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2081 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2082 /* The part of the string before starttry has one color
2083 (pref0_len chars), between starttry and current
2084 position another one (pref_len - pref0_len chars),
2085 after the current position the third one.
2086 We assume that pref0_len <= pref_len, otherwise we
2087 decrease pref0_len. */
2088 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2089 ? (5 + taill) - l : locinput - PL_bostr;
2092 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2094 pref0_len = pref_len - (locinput - PL_reg_starttry);
2095 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2096 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2097 ? (5 + taill) - pref_len : PL_regeol - locinput);
2098 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2102 if (pref0_len > pref_len)
2103 pref0_len = pref_len;
2104 regprop(prop, scan);
2108 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2110 locinput - pref_len;
2111 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2112 char *s1 = do_utf8 ?
2113 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2114 pref_len - pref0_len, 60, 0) :
2115 locinput - pref_len + pref0_len;
2116 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2117 char *s2 = do_utf8 ?
2118 pv_uni_display(dsv2, (U8*)locinput,
2119 PL_regeol - locinput, 60, 0) :
2121 int len2 = do_utf8 ? strlen(s2) : l;
2122 PerlIO_printf(Perl_debug_log,
2123 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2124 (IV)(locinput - PL_bostr),
2131 (docolor ? "" : "> <"),
2135 15 - l - pref_len + 1,
2137 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2142 next = scan + NEXT_OFF(scan);
2148 if (locinput == PL_bostr || (PL_multiline &&
2149 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2151 /* regtill = regbol; */
2156 if (locinput == PL_bostr ||
2157 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2163 if (locinput == PL_bostr)
2167 if (locinput == PL_reg_ganch)
2177 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2182 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2184 if (PL_regeol - locinput > 1)
2188 if (PL_regeol != locinput)
2192 if (!nextchr && locinput >= PL_regeol)
2195 locinput += PL_utf8skip[nextchr];
2196 if (locinput > PL_regeol)
2198 nextchr = UCHARAT(locinput);
2201 nextchr = UCHARAT(++locinput);
2204 if (!nextchr && locinput >= PL_regeol)
2206 nextchr = UCHARAT(++locinput);
2209 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2212 locinput += PL_utf8skip[nextchr];
2213 if (locinput > PL_regeol)
2215 nextchr = UCHARAT(locinput);
2218 nextchr = UCHARAT(++locinput);
2223 if (do_utf8 != (UTF!=0)) {
2224 /* The target and the pattern have differing "utf8ness". */
2230 /* The target is utf8, the pattern is not utf8. */
2234 if (NATIVE_TO_UNI(*(U8*)s) !=
2235 utf8_to_uvchr((U8*)l, &len))
2242 /* The target is not utf8, the pattern is utf8. */
2246 if (NATIVE_TO_UNI(*((U8*)l)) !=
2247 utf8_to_uvchr((U8*)s, &len))
2254 nextchr = UCHARAT(locinput);
2257 /* The target and the pattern have the same "utf8ness". */
2258 /* Inline the first character, for speed. */
2259 if (UCHARAT(s) != nextchr)
2261 if (PL_regeol - locinput < ln)
2263 if (ln > 1 && memNE(s, locinput, ln))
2266 nextchr = UCHARAT(locinput);
2269 PL_reg_flags |= RF_tainted;
2279 U8 tmpbuf[UTF8_MAXLEN*2+1];
2284 toLOWER_utf8((U8*)l, tmpbuf, &ulen);
2285 if (memNE(s, (char*)tmpbuf, ulen))
2291 nextchr = UCHARAT(locinput);
2295 /* Inline the first character, for speed. */
2296 if (UCHARAT(s) != nextchr &&
2297 UCHARAT(s) != ((OP(scan) == EXACTF)
2298 ? PL_fold : PL_fold_locale)[nextchr])
2300 if (PL_regeol - locinput < ln)
2302 if (ln > 1 && (OP(scan) == EXACTF
2303 ? ibcmp(s, locinput, ln)
2304 : ibcmp_locale(s, locinput, ln)))
2307 nextchr = UCHARAT(locinput);
2311 if (!reginclass(scan, (U8*)locinput, do_utf8))
2313 if (locinput >= PL_regeol)
2315 locinput += PL_utf8skip[nextchr];
2316 nextchr = UCHARAT(locinput);
2320 nextchr = UCHARAT(locinput);
2321 if (!reginclass(scan, (U8*)locinput, do_utf8))
2323 if (!nextchr && locinput >= PL_regeol)
2325 nextchr = UCHARAT(++locinput);
2329 PL_reg_flags |= RF_tainted;
2335 LOAD_UTF8_CHARCLASS(alnum,"a");
2336 if (!(OP(scan) == ALNUM
2337 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2338 : isALNUM_LC_utf8((U8*)locinput)))
2342 locinput += PL_utf8skip[nextchr];
2343 nextchr = UCHARAT(locinput);
2346 if (!(OP(scan) == ALNUM
2347 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2349 nextchr = UCHARAT(++locinput);
2352 PL_reg_flags |= RF_tainted;
2355 if (!nextchr && locinput >= PL_regeol)
2358 LOAD_UTF8_CHARCLASS(alnum,"a");
2359 if (OP(scan) == NALNUM
2360 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2361 : isALNUM_LC_utf8((U8*)locinput))
2365 locinput += PL_utf8skip[nextchr];
2366 nextchr = UCHARAT(locinput);
2369 if (OP(scan) == NALNUM
2370 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2372 nextchr = UCHARAT(++locinput);
2376 PL_reg_flags |= RF_tainted;
2380 /* was last char in word? */
2382 if (locinput == PL_bostr)
2385 U8 *r = reghop((U8*)locinput, -1);
2387 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2389 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2390 ln = isALNUM_uni(ln);
2391 LOAD_UTF8_CHARCLASS(alnum,"a");
2392 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2395 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2396 n = isALNUM_LC_utf8((U8*)locinput);
2400 ln = (locinput != PL_bostr) ?
2401 UCHARAT(locinput - 1) : '\n';
2402 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2404 n = isALNUM(nextchr);
2407 ln = isALNUM_LC(ln);
2408 n = isALNUM_LC(nextchr);
2411 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2412 OP(scan) == BOUNDL))
2416 PL_reg_flags |= RF_tainted;
2422 if (UTF8_IS_CONTINUED(nextchr)) {
2423 LOAD_UTF8_CHARCLASS(space," ");
2424 if (!(OP(scan) == SPACE
2425 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2426 : isSPACE_LC_utf8((U8*)locinput)))
2430 locinput += PL_utf8skip[nextchr];
2431 nextchr = UCHARAT(locinput);
2434 if (!(OP(scan) == SPACE
2435 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2437 nextchr = UCHARAT(++locinput);
2440 if (!(OP(scan) == SPACE
2441 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2443 nextchr = UCHARAT(++locinput);
2447 PL_reg_flags |= RF_tainted;
2450 if (!nextchr && locinput >= PL_regeol)
2453 LOAD_UTF8_CHARCLASS(space," ");
2454 if (OP(scan) == NSPACE
2455 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2456 : isSPACE_LC_utf8((U8*)locinput))
2460 locinput += PL_utf8skip[nextchr];
2461 nextchr = UCHARAT(locinput);
2464 if (OP(scan) == NSPACE
2465 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2467 nextchr = UCHARAT(++locinput);
2470 PL_reg_flags |= RF_tainted;
2476 LOAD_UTF8_CHARCLASS(digit,"0");
2477 if (!(OP(scan) == DIGIT
2478 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2479 : isDIGIT_LC_utf8((U8*)locinput)))
2483 locinput += PL_utf8skip[nextchr];
2484 nextchr = UCHARAT(locinput);
2487 if (!(OP(scan) == DIGIT
2488 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2490 nextchr = UCHARAT(++locinput);
2493 PL_reg_flags |= RF_tainted;
2496 if (!nextchr && locinput >= PL_regeol)
2499 LOAD_UTF8_CHARCLASS(digit,"0");
2500 if (OP(scan) == NDIGIT
2501 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2502 : isDIGIT_LC_utf8((U8*)locinput))
2506 locinput += PL_utf8skip[nextchr];
2507 nextchr = UCHARAT(locinput);
2510 if (OP(scan) == NDIGIT
2511 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2513 nextchr = UCHARAT(++locinput);
2516 LOAD_UTF8_CHARCLASS(mark,"~");
2517 if (locinput >= PL_regeol ||
2518 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2520 locinput += PL_utf8skip[nextchr];
2521 while (locinput < PL_regeol &&
2522 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2523 locinput += UTF8SKIP(locinput);
2524 if (locinput > PL_regeol)
2526 nextchr = UCHARAT(locinput);
2529 PL_reg_flags |= RF_tainted;
2533 n = ARG(scan); /* which paren pair */
2534 ln = PL_regstartp[n];
2535 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2536 if (*PL_reglastparen < n || ln == -1)
2537 sayNO; /* Do not match unless seen CLOSEn. */
2538 if (ln == PL_regendp[n])
2542 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2544 char *e = PL_bostr + PL_regendp[n];
2546 * Note that we can't do the "other character" lookup trick as
2547 * in the 8-bit case (no pun intended) because in Unicode we
2548 * have to map both upper and title case to lower case.
2550 if (OP(scan) == REFF) {
2551 STRLEN ulen1, ulen2;
2552 U8 tmpbuf1[UTF8_MAXLEN*2+1];
2553 U8 tmpbuf2[UTF8_MAXLEN*2+1];
2557 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2558 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2559 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2566 nextchr = UCHARAT(locinput);
2570 /* Inline the first character, for speed. */
2571 if (UCHARAT(s) != nextchr &&
2573 (UCHARAT(s) != ((OP(scan) == REFF
2574 ? PL_fold : PL_fold_locale)[nextchr]))))
2576 ln = PL_regendp[n] - ln;
2577 if (locinput + ln > PL_regeol)
2579 if (ln > 1 && (OP(scan) == REF
2580 ? memNE(s, locinput, ln)
2582 ? ibcmp(s, locinput, ln)
2583 : ibcmp_locale(s, locinput, ln))))
2586 nextchr = UCHARAT(locinput);
2597 OP_4tree *oop = PL_op;
2598 COP *ocurcop = PL_curcop;
2599 SV **ocurpad = PL_curpad;
2603 PL_op = (OP_4tree*)PL_regdata->data[n];
2604 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2605 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2606 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2610 CALLRUNOPS(aTHX); /* Scalar context. */
2613 ret = Nullsv; /* protect against empty (?{}) blocks. */
2621 PL_curpad = ocurpad;
2622 PL_curcop = ocurcop;
2624 if (logical == 2) { /* Postponed subexpression. */
2626 MAGIC *mg = Null(MAGIC*);
2628 CHECKPOINT cp, lastcp;
2630 if(SvROK(ret) || SvRMAGICAL(ret)) {
2631 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2634 mg = mg_find(sv, PERL_MAGIC_qr);
2637 re = (regexp *)mg->mg_obj;
2638 (void)ReREFCNT_inc(re);
2642 char *t = SvPV(ret, len);
2644 char *oprecomp = PL_regprecomp;
2645 I32 osize = PL_regsize;
2646 I32 onpar = PL_regnpar;
2649 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2651 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2652 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2654 PL_regprecomp = oprecomp;
2659 PerlIO_printf(Perl_debug_log,
2660 "Entering embedded `%s%.60s%s%s'\n",
2664 (strlen(re->precomp) > 60 ? "..." : ""))
2667 state.prev = PL_reg_call_cc;
2668 state.cc = PL_regcc;
2669 state.re = PL_reg_re;
2673 cp = regcppush(0); /* Save *all* the positions. */
2676 state.ss = PL_savestack_ix;
2677 *PL_reglastparen = 0;
2678 *PL_reglastcloseparen = 0;
2679 PL_reg_call_cc = &state;
2680 PL_reginput = locinput;
2682 /* XXXX This is too dramatic a measure... */
2685 if (regmatch(re->program + 1)) {
2686 /* Even though we succeeded, we need to restore
2687 global variables, since we may be wrapped inside
2688 SUSPEND, thus the match may be not finished yet. */
2690 /* XXXX Do this only if SUSPENDed? */
2691 PL_reg_call_cc = state.prev;
2692 PL_regcc = state.cc;
2693 PL_reg_re = state.re;
2694 cache_re(PL_reg_re);
2696 /* XXXX This is too dramatic a measure... */
2699 /* These are needed even if not SUSPEND. */
2705 REGCP_UNWIND(lastcp);
2707 PL_reg_call_cc = state.prev;
2708 PL_regcc = state.cc;
2709 PL_reg_re = state.re;
2710 cache_re(PL_reg_re);
2712 /* XXXX This is too dramatic a measure... */
2722 sv_setsv(save_scalar(PL_replgv), ret);
2726 n = ARG(scan); /* which paren pair */
2727 PL_reg_start_tmp[n] = locinput;
2732 n = ARG(scan); /* which paren pair */
2733 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2734 PL_regendp[n] = locinput - PL_bostr;
2735 if (n > *PL_reglastparen)
2736 *PL_reglastparen = n;
2737 *PL_reglastcloseparen = n;
2740 n = ARG(scan); /* which paren pair */
2741 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2744 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2746 next = NEXTOPER(NEXTOPER(scan));
2748 next = scan + ARG(scan);
2749 if (OP(next) == IFTHEN) /* Fake one. */
2750 next = NEXTOPER(NEXTOPER(next));
2754 logical = scan->flags;
2756 /*******************************************************************
2757 PL_regcc contains infoblock about the innermost (...)* loop, and
2758 a pointer to the next outer infoblock.
2760 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2762 1) After matching X, regnode for CURLYX is processed;
2764 2) This regnode creates infoblock on the stack, and calls
2765 regmatch() recursively with the starting point at WHILEM node;
2767 3) Each hit of WHILEM node tries to match A and Z (in the order
2768 depending on the current iteration, min/max of {min,max} and
2769 greediness). The information about where are nodes for "A"
2770 and "Z" is read from the infoblock, as is info on how many times "A"
2771 was already matched, and greediness.
2773 4) After A matches, the same WHILEM node is hit again.
2775 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2776 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2777 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2778 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2779 of the external loop.
2781 Currently present infoblocks form a tree with a stem formed by PL_curcc
2782 and whatever it mentions via ->next, and additional attached trees
2783 corresponding to temporarily unset infoblocks as in "5" above.
2785 In the following picture infoblocks for outer loop of
2786 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2787 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2788 infoblocks are drawn below the "reset" infoblock.
2790 In fact in the picture below we do not show failed matches for Z and T
2791 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2792 more obvious *why* one needs to *temporary* unset infoblocks.]
2794 Matched REx position InfoBlocks Comment
2798 Y A)*?Z)*?T x <- O <- I
2799 YA )*?Z)*?T x <- O <- I
2800 YA A)*?Z)*?T x <- O <- I
2801 YAA )*?Z)*?T x <- O <- I
2802 YAA Z)*?T x <- O # Temporary unset I
2805 YAAZ Y(A)*?Z)*?T x <- O
2808 YAAZY (A)*?Z)*?T x <- O
2811 YAAZY A)*?Z)*?T x <- O <- I
2814 YAAZYA )*?Z)*?T x <- O <- I
2817 YAAZYA Z)*?T x <- O # Temporary unset I
2823 YAAZYAZ T x # Temporary unset O
2830 *******************************************************************/
2833 CHECKPOINT cp = PL_savestack_ix;
2834 /* No need to save/restore up to this paren */
2835 I32 parenfloor = scan->flags;
2837 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2839 cc.oldcc = PL_regcc;
2841 /* XXXX Probably it is better to teach regpush to support
2842 parenfloor > PL_regsize... */
2843 if (parenfloor > *PL_reglastparen)
2844 parenfloor = *PL_reglastparen; /* Pessimization... */
2845 cc.parenfloor = parenfloor;
2847 cc.min = ARG1(scan);
2848 cc.max = ARG2(scan);
2849 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2853 PL_reginput = locinput;
2854 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2856 PL_regcc = cc.oldcc;
2862 * This is really hard to understand, because after we match
2863 * what we're trying to match, we must make sure the rest of
2864 * the REx is going to match for sure, and to do that we have
2865 * to go back UP the parse tree by recursing ever deeper. And
2866 * if it fails, we have to reset our parent's current state
2867 * that we can try again after backing off.
2870 CHECKPOINT cp, lastcp;
2871 CURCUR* cc = PL_regcc;
2872 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2874 n = cc->cur + 1; /* how many we know we matched */
2875 PL_reginput = locinput;
2878 PerlIO_printf(Perl_debug_log,
2879 "%*s %ld out of %ld..%ld cc=%lx\n",
2880 REPORT_CODE_OFF+PL_regindent*2, "",
2881 (long)n, (long)cc->min,
2882 (long)cc->max, (long)cc)
2885 /* If degenerate scan matches "", assume scan done. */
2887 if (locinput == cc->lastloc && n >= cc->min) {
2888 PL_regcc = cc->oldcc;
2892 PerlIO_printf(Perl_debug_log,
2893 "%*s empty match detected, try continuation...\n",
2894 REPORT_CODE_OFF+PL_regindent*2, "")
2896 if (regmatch(cc->next))
2904 /* First just match a string of min scans. */
2908 cc->lastloc = locinput;
2909 if (regmatch(cc->scan))
2912 cc->lastloc = lastloc;
2917 /* Check whether we already were at this position.
2918 Postpone detection until we know the match is not
2919 *that* much linear. */
2920 if (!PL_reg_maxiter) {
2921 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2922 PL_reg_leftiter = PL_reg_maxiter;
2924 if (PL_reg_leftiter-- == 0) {
2925 I32 size = (PL_reg_maxiter + 7)/8;
2926 if (PL_reg_poscache) {
2927 if (PL_reg_poscache_size < size) {
2928 Renew(PL_reg_poscache, size, char);
2929 PL_reg_poscache_size = size;
2931 Zero(PL_reg_poscache, size, char);
2934 PL_reg_poscache_size = size;
2935 Newz(29, PL_reg_poscache, size, char);
2938 PerlIO_printf(Perl_debug_log,
2939 "%sDetected a super-linear match, switching on caching%s...\n",
2940 PL_colors[4], PL_colors[5])
2943 if (PL_reg_leftiter < 0) {
2944 I32 o = locinput - PL_bostr, b;
2946 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2949 if (PL_reg_poscache[o] & (1<<b)) {
2951 PerlIO_printf(Perl_debug_log,
2952 "%*s already tried at this position...\n",
2953 REPORT_CODE_OFF+PL_regindent*2, "")
2957 PL_reg_poscache[o] |= (1<<b);
2961 /* Prefer next over scan for minimal matching. */
2964 PL_regcc = cc->oldcc;
2967 cp = regcppush(cc->parenfloor);
2969 if (regmatch(cc->next)) {
2971 sayYES; /* All done. */
2973 REGCP_UNWIND(lastcp);
2979 if (n >= cc->max) { /* Maximum greed exceeded? */
2980 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2981 && !(PL_reg_flags & RF_warned)) {
2982 PL_reg_flags |= RF_warned;
2983 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2984 "Complex regular subexpression recursion",
2991 PerlIO_printf(Perl_debug_log,
2992 "%*s trying longer...\n",
2993 REPORT_CODE_OFF+PL_regindent*2, "")
2995 /* Try scanning more and see if it helps. */
2996 PL_reginput = locinput;
2998 cc->lastloc = locinput;
2999 cp = regcppush(cc->parenfloor);
3001 if (regmatch(cc->scan)) {
3005 REGCP_UNWIND(lastcp);
3008 cc->lastloc = lastloc;
3012 /* Prefer scan over next for maximal matching. */
3014 if (n < cc->max) { /* More greed allowed? */
3015 cp = regcppush(cc->parenfloor);
3017 cc->lastloc = locinput;
3019 if (regmatch(cc->scan)) {
3023 REGCP_UNWIND(lastcp);
3024 regcppop(); /* Restore some previous $<digit>s? */
3025 PL_reginput = locinput;
3027 PerlIO_printf(Perl_debug_log,
3028 "%*s failed, try continuation...\n",
3029 REPORT_CODE_OFF+PL_regindent*2, "")
3032 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3033 && !(PL_reg_flags & RF_warned)) {
3034 PL_reg_flags |= RF_warned;
3035 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3036 "Complex regular subexpression recursion",
3040 /* Failed deeper matches of scan, so see if this one works. */
3041 PL_regcc = cc->oldcc;
3044 if (regmatch(cc->next))
3050 cc->lastloc = lastloc;
3055 next = scan + ARG(scan);
3058 inner = NEXTOPER(NEXTOPER(scan));
3061 inner = NEXTOPER(scan);
3065 if (OP(next) != c1) /* No choice. */
3066 next = inner; /* Avoid recursion. */
3068 I32 lastparen = *PL_reglastparen;
3070 re_unwind_branch_t *uw;
3072 /* Put unwinding data on stack */
3073 unwind1 = SSNEWt(1,re_unwind_branch_t);
3074 uw = SSPTRt(unwind1,re_unwind_branch_t);
3077 uw->type = ((c1 == BRANCH)
3079 : RE_UNWIND_BRANCHJ);
3080 uw->lastparen = lastparen;
3082 uw->locinput = locinput;
3083 uw->nextchr = nextchr;
3085 uw->regindent = ++PL_regindent;
3088 REGCP_SET(uw->lastcp);
3090 /* Now go into the first branch */
3103 /* We suppose that the next guy does not need
3104 backtracking: in particular, it is of constant length,
3105 and has no parenths to influence future backrefs. */
3106 ln = ARG1(scan); /* min to match */
3107 n = ARG2(scan); /* max to match */
3108 paren = scan->flags;
3110 if (paren > PL_regsize)
3112 if (paren > *PL_reglastparen)
3113 *PL_reglastparen = paren;
3115 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3117 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3118 PL_reginput = locinput;
3121 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3123 /* if we matched something zero-length we don't need to
3124 backtrack - capturing parens are already defined, so
3125 the caveat in the maximal case doesn't apply
3127 XXXX if ln == 0, we can redo this check first time
3128 through the following loop
3131 n = ln; /* don't backtrack */
3132 locinput = PL_reginput;
3133 if (HAS_TEXT(next) || JUMPABLE(next)) {
3134 regnode *text_node = next;
3136 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3138 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3140 if (PL_regkind[(U8)OP(text_node)] == REF) {
3142 n = ARG(text_node); /* which paren pair */
3143 ln = PL_regstartp[n];
3144 /* assume yes if we haven't seen CLOSEn */
3146 *PL_reglastparen < n ||
3153 c1 = *(PL_bostr + ln);
3155 else { c1 = (U8)*STRING(text_node); }
3156 if (OP(next) == EXACTF)
3158 else if (OP(text_node) == EXACTFL)
3159 c2 = PL_fold_locale[c1];
3168 /* This may be improved if l == 0. */
3169 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3170 /* If it could work, try it. */
3172 UCHARAT(PL_reginput) == c1 ||
3173 UCHARAT(PL_reginput) == c2)
3177 PL_regstartp[paren] =
3178 HOPc(PL_reginput, -l) - PL_bostr;
3179 PL_regendp[paren] = PL_reginput - PL_bostr;
3182 PL_regendp[paren] = -1;
3186 REGCP_UNWIND(lastcp);
3188 /* Couldn't or didn't -- move forward. */
3189 PL_reginput = locinput;
3190 if (regrepeat_hard(scan, 1, &l)) {
3192 locinput = PL_reginput;
3199 n = regrepeat_hard(scan, n, &l);
3200 /* if we matched something zero-length we don't need to
3201 backtrack, unless the minimum count is zero and we
3202 are capturing the result - in that case the capture
3203 being defined or not may affect later execution
3205 if (n != 0 && l == 0 && !(paren && ln == 0))
3206 ln = n; /* don't backtrack */
3207 locinput = PL_reginput;
3209 PerlIO_printf(Perl_debug_log,
3210 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3211 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3215 if (HAS_TEXT(next) || JUMPABLE(next)) {
3216 regnode *text_node = next;
3218 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3220 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3222 if (PL_regkind[(U8)OP(text_node)] == REF) {
3224 n = ARG(text_node); /* which paren pair */
3225 ln = PL_regstartp[n];
3226 /* assume yes if we haven't seen CLOSEn */
3228 *PL_reglastparen < n ||
3235 c1 = *(PL_bostr + ln);
3237 else { c1 = (U8)*STRING(text_node); }
3239 if (OP(text_node) == EXACTF)
3241 else if (OP(text_node) == EXACTFL)
3242 c2 = PL_fold_locale[c1];
3253 /* If it could work, try it. */
3255 UCHARAT(PL_reginput) == c1 ||
3256 UCHARAT(PL_reginput) == c2)
3259 PerlIO_printf(Perl_debug_log,
3260 "%*s trying tail with n=%"IVdf"...\n",
3261 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3265 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3266 PL_regendp[paren] = PL_reginput - PL_bostr;
3269 PL_regendp[paren] = -1;
3273 REGCP_UNWIND(lastcp);
3275 /* Couldn't or didn't -- back up. */
3277 locinput = HOPc(locinput, -l);
3278 PL_reginput = locinput;
3285 paren = scan->flags; /* Which paren to set */
3286 if (paren > PL_regsize)
3288 if (paren > *PL_reglastparen)
3289 *PL_reglastparen = paren;
3290 ln = ARG1(scan); /* min to match */
3291 n = ARG2(scan); /* max to match */
3292 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3296 ln = ARG1(scan); /* min to match */
3297 n = ARG2(scan); /* max to match */
3298 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3303 scan = NEXTOPER(scan);
3309 scan = NEXTOPER(scan);
3313 * Lookahead to avoid useless match attempts
3314 * when we know what character comes next.
3318 * Used to only do .*x and .*?x, but now it allows
3319 * for )'s, ('s and (?{ ... })'s to be in the way
3320 * of the quantifier and the EXACT-like node. -- japhy
3323 if (HAS_TEXT(next) || JUMPABLE(next)) {
3325 regnode *text_node = next;
3327 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3329 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3331 if (PL_regkind[(U8)OP(text_node)] == REF) {
3333 n = ARG(text_node); /* which paren pair */
3334 ln = PL_regstartp[n];
3335 /* assume yes if we haven't seen CLOSEn */
3337 *PL_reglastparen < n ||
3342 goto assume_ok_easy;
3344 s = (U8*)PL_bostr + ln;
3346 else { s = (U8*)STRING(text_node); }
3350 if (OP(text_node) == EXACTF)
3352 else if (OP(text_node) == EXACTFL)
3353 c2 = PL_fold_locale[c1];
3356 if (OP(text_node) == EXACTF) {
3357 STRLEN ulen1, ulen2;
3358 U8 tmpbuf1[UTF8_MAXLEN*2+1];
3359 U8 tmpbuf2[UTF8_MAXLEN*2+1];
3361 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3362 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3364 c1 = utf8_to_uvuni(tmpbuf1, 0);
3365 c2 = utf8_to_uvuni(tmpbuf2, 0);
3368 c2 = c1 = utf8_to_uvchr(s, NULL);
3376 PL_reginput = locinput;
3380 if (ln && regrepeat(scan, ln) < ln)
3382 locinput = PL_reginput;
3385 char *e; /* Should not check after this */
3386 char *old = locinput;
3388 if (n == REG_INFTY) {
3391 while (UTF8_IS_CONTINUATION(*(U8*)e))
3397 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3401 e = locinput + n - ln;
3407 /* Find place 'next' could work */
3410 while (locinput <= e &&
3411 UCHARAT(locinput) != c1)
3414 while (locinput <= e
3415 && UCHARAT(locinput) != c1
3416 && UCHARAT(locinput) != c2)
3419 count = locinput - old;
3426 utf8_to_uvchr((U8*)locinput, &len) != c1;
3431 for (count = 0; locinput <= e; count++) {
3432 UV c = utf8_to_uvchr((U8*)locinput, &len);
3433 if (c == c1 || c == c2)
3441 /* PL_reginput == old now */
3442 if (locinput != old) {
3443 ln = 1; /* Did some */
3444 if (regrepeat(scan, count) < count)
3447 /* PL_reginput == locinput now */
3448 TRYPAREN(paren, ln, locinput);
3449 PL_reginput = locinput; /* Could be reset... */
3450 REGCP_UNWIND(lastcp);
3451 /* Couldn't or didn't -- move forward. */
3454 locinput += UTF8SKIP(locinput);
3460 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3464 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3466 c = UCHARAT(PL_reginput);
3467 /* If it could work, try it. */
3468 if (c == c1 || c == c2)
3470 TRYPAREN(paren, n, PL_reginput);
3471 REGCP_UNWIND(lastcp);
3474 /* If it could work, try it. */
3475 else if (c1 == -1000)
3477 TRYPAREN(paren, n, PL_reginput);
3478 REGCP_UNWIND(lastcp);
3480 /* Couldn't or didn't -- move forward. */
3481 PL_reginput = locinput;
3482 if (regrepeat(scan, 1)) {
3484 locinput = PL_reginput;
3492 n = regrepeat(scan, n);
3493 locinput = PL_reginput;
3494 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3495 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3496 ln = n; /* why back off? */
3497 /* ...because $ and \Z can match before *and* after
3498 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3499 We should back off by one in this case. */
3500 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3509 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3511 c = UCHARAT(PL_reginput);
3513 /* If it could work, try it. */
3514 if (c1 == -1000 || c == c1 || c == c2)
3516 TRYPAREN(paren, n, PL_reginput);
3517 REGCP_UNWIND(lastcp);
3519 /* Couldn't or didn't -- back up. */
3521 PL_reginput = locinput = HOPc(locinput, -1);
3529 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3531 c = UCHARAT(PL_reginput);
3533 /* If it could work, try it. */
3534 if (c1 == -1000 || c == c1 || c == c2)
3536 TRYPAREN(paren, n, PL_reginput);
3537 REGCP_UNWIND(lastcp);
3539 /* Couldn't or didn't -- back up. */
3541 PL_reginput = locinput = HOPc(locinput, -1);
3548 if (PL_reg_call_cc) {
3549 re_cc_state *cur_call_cc = PL_reg_call_cc;
3550 CURCUR *cctmp = PL_regcc;
3551 regexp *re = PL_reg_re;
3552 CHECKPOINT cp, lastcp;
3554 cp = regcppush(0); /* Save *all* the positions. */
3556 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3558 PL_reginput = locinput; /* Make position available to
3560 cache_re(PL_reg_call_cc->re);
3561 PL_regcc = PL_reg_call_cc->cc;
3562 PL_reg_call_cc = PL_reg_call_cc->prev;
3563 if (regmatch(cur_call_cc->node)) {
3564 PL_reg_call_cc = cur_call_cc;
3568 REGCP_UNWIND(lastcp);
3570 PL_reg_call_cc = cur_call_cc;
3576 PerlIO_printf(Perl_debug_log,
3577 "%*s continuation failed...\n",
3578 REPORT_CODE_OFF+PL_regindent*2, "")
3582 if (locinput < PL_regtill) {
3583 DEBUG_r(PerlIO_printf(Perl_debug_log,
3584 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3586 (long)(locinput - PL_reg_starttry),
3587 (long)(PL_regtill - PL_reg_starttry),
3589 sayNO_FINAL; /* Cannot match: too short. */
3591 PL_reginput = locinput; /* put where regtry can find it */
3592 sayYES_FINAL; /* Success! */
3594 PL_reginput = locinput; /* put where regtry can find it */
3595 sayYES_LOUD; /* Success! */
3598 PL_reginput = locinput;
3603 s = HOPBACKc(locinput, scan->flags);
3609 PL_reginput = locinput;
3614 s = HOPBACKc(locinput, scan->flags);
3620 PL_reginput = locinput;
3623 inner = NEXTOPER(NEXTOPER(scan));
3624 if (regmatch(inner) != n) {
3639 if (OP(scan) == SUSPEND) {
3640 locinput = PL_reginput;
3641 nextchr = UCHARAT(locinput);
3646 next = scan + ARG(scan);
3651 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3652 PTR2UV(scan), OP(scan));
3653 Perl_croak(aTHX_ "regexp memory corruption");
3660 * We get here only if there's trouble -- normally "case END" is
3661 * the terminating point.
3663 Perl_croak(aTHX_ "corrupted regexp pointers");
3669 PerlIO_printf(Perl_debug_log,
3670 "%*s %scould match...%s\n",
3671 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3675 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3676 PL_colors[4],PL_colors[5]));
3682 #if 0 /* Breaks $^R */
3690 PerlIO_printf(Perl_debug_log,
3691 "%*s %sfailed...%s\n",
3692 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3698 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3701 case RE_UNWIND_BRANCH:
3702 case RE_UNWIND_BRANCHJ:
3704 re_unwind_branch_t *uwb = &(uw->branch);
3705 I32 lastparen = uwb->lastparen;
3707 REGCP_UNWIND(uwb->lastcp);
3708 for (n = *PL_reglastparen; n > lastparen; n--)
3710 *PL_reglastparen = n;
3711 scan = next = uwb->next;
3713 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3714 ? BRANCH : BRANCHJ) ) { /* Failure */
3721 /* Have more choice yet. Reuse the same uwb. */
3723 if ((n = (uwb->type == RE_UNWIND_BRANCH
3724 ? NEXT_OFF(next) : ARG(next))))
3727 next = NULL; /* XXXX Needn't unwinding in this case... */
3729 next = NEXTOPER(scan);
3730 if (uwb->type == RE_UNWIND_BRANCHJ)
3731 next = NEXTOPER(next);
3732 locinput = uwb->locinput;
3733 nextchr = uwb->nextchr;
3735 PL_regindent = uwb->regindent;
3742 Perl_croak(aTHX_ "regexp unwind memory corruption");
3753 - regrepeat - repeatedly match something simple, report how many
3756 * [This routine now assumes that it will only match on things of length 1.
3757 * That was true before, but now we assume scan - reginput is the count,
3758 * rather than incrementing count on every character. [Er, except utf8.]]
3761 S_regrepeat(pTHX_ regnode *p, I32 max)
3763 register char *scan;
3765 register char *loceol = PL_regeol;
3766 register I32 hardcount = 0;
3767 register bool do_utf8 = PL_reg_match_utf8;
3770 if (max != REG_INFTY && max < loceol - scan)
3771 loceol = scan + max;
3776 while (scan < loceol && hardcount < max && *scan != '\n') {
3777 scan += UTF8SKIP(scan);
3781 while (scan < loceol && *scan != '\n')
3791 case EXACT: /* length of string is 1 */
3793 while (scan < loceol && UCHARAT(scan) == c)
3796 case EXACTF: /* length of string is 1 */
3798 while (scan < loceol &&
3799 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3802 case EXACTFL: /* length of string is 1 */
3803 PL_reg_flags |= RF_tainted;
3805 while (scan < loceol &&
3806 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3812 while (hardcount < max && scan < loceol &&
3813 reginclass(p, (U8*)scan, do_utf8)) {
3814 scan += UTF8SKIP(scan);
3818 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3825 LOAD_UTF8_CHARCLASS(alnum,"a");
3826 while (hardcount < max && scan < loceol &&
3827 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3828 scan += UTF8SKIP(scan);
3832 while (scan < loceol && isALNUM(*scan))
3837 PL_reg_flags |= RF_tainted;
3840 while (hardcount < max && scan < loceol &&
3841 isALNUM_LC_utf8((U8*)scan)) {
3842 scan += UTF8SKIP(scan);
3846 while (scan < loceol && isALNUM_LC(*scan))
3853 LOAD_UTF8_CHARCLASS(alnum,"a");
3854 while (hardcount < max && scan < loceol &&
3855 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3856 scan += UTF8SKIP(scan);
3860 while (scan < loceol && !isALNUM(*scan))
3865 PL_reg_flags |= RF_tainted;
3868 while (hardcount < max && scan < loceol &&
3869 !isALNUM_LC_utf8((U8*)scan)) {
3870 scan += UTF8SKIP(scan);
3874 while (scan < loceol && !isALNUM_LC(*scan))
3881 LOAD_UTF8_CHARCLASS(space," ");
3882 while (hardcount < max && scan < loceol &&
3884 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3885 scan += UTF8SKIP(scan);
3889 while (scan < loceol && isSPACE(*scan))
3894 PL_reg_flags |= RF_tainted;
3897 while (hardcount < max && scan < loceol &&
3898 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3899 scan += UTF8SKIP(scan);
3903 while (scan < loceol && isSPACE_LC(*scan))
3910 LOAD_UTF8_CHARCLASS(space," ");
3911 while (hardcount < max && scan < loceol &&
3913 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3914 scan += UTF8SKIP(scan);
3918 while (scan < loceol && !isSPACE(*scan))
3923 PL_reg_flags |= RF_tainted;
3926 while (hardcount < max && scan < loceol &&
3927 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3928 scan += UTF8SKIP(scan);
3932 while (scan < loceol && !isSPACE_LC(*scan))
3939 LOAD_UTF8_CHARCLASS(digit,"0");
3940 while (hardcount < max && scan < loceol &&
3941 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3942 scan += UTF8SKIP(scan);
3946 while (scan < loceol && isDIGIT(*scan))
3953 LOAD_UTF8_CHARCLASS(digit,"0");
3954 while (hardcount < max && scan < loceol &&
3955 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3956 scan += UTF8SKIP(scan);
3960 while (scan < loceol && !isDIGIT(*scan))
3964 default: /* Called on something of 0 width. */
3965 break; /* So match right here or not at all. */
3971 c = scan - PL_reginput;
3976 SV *prop = sv_newmortal();
3979 PerlIO_printf(Perl_debug_log,
3980 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3981 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3988 - regrepeat_hard - repeatedly match something, report total lenth and length
3990 * The repeater is supposed to have constant length.
3994 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3996 register char *scan = Nullch;
3997 register char *start;
3998 register char *loceol = PL_regeol;
4000 I32 count = 0, res = 1;
4005 start = PL_reginput;
4006 if (PL_reg_match_utf8) {
4007 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4010 while (start < PL_reginput) {
4012 start += UTF8SKIP(start);
4023 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4025 *lp = l = PL_reginput - start;
4026 if (max != REG_INFTY && l*max < loceol - scan)
4027 loceol = scan + l*max;
4040 - regclass_swash - prepare the utf8 swash
4044 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
4049 if (PL_regdata && PL_regdata->count) {
4052 if (PL_regdata->what[n] == 's') {
4053 SV *rv = (SV*)PL_regdata->data[n];
4054 AV *av = (AV*)SvRV((SV*)rv);
4057 si = *av_fetch(av, 0, FALSE);
4058 a = av_fetch(av, 1, FALSE);
4062 else if (si && doinit) {
4063 sw = swash_init("utf8", "", si, 1, 0);
4064 (void)av_store(av, 1, sw);
4076 - reginclass - determine if a character falls into a character class
4080 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
4082 char flags = ANYOF_FLAGS(n);
4087 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4089 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4090 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4091 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4094 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4097 SV *sw = regclass_swash(n, TRUE, 0);
4100 if (swash_fetch(sw, p, do_utf8))
4102 else if (flags & ANYOF_FOLD) {
4104 U8 tmpbuf[UTF8_MAXLEN*2+1];
4106 toLOWER_utf8(p, tmpbuf, &ulen);
4107 if (swash_fetch(sw, tmpbuf, do_utf8))
4113 if (!match && c < 256) {
4114 if (ANYOF_BITMAP_TEST(n, c))
4116 else if (flags & ANYOF_FOLD) {
4119 if (flags & ANYOF_LOCALE) {
4120 PL_reg_flags |= RF_tainted;
4121 f = PL_fold_locale[c];
4125 if (f != c && ANYOF_BITMAP_TEST(n, f))
4129 if (!match && (flags & ANYOF_CLASS)) {
4130 PL_reg_flags |= RF_tainted;
4132 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4133 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4134 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4135 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4136 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4137 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4138 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4139 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4140 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4141 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4142 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4143 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4144 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4145 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4146 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4147 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4148 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4149 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4150 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4151 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4152 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4153 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4154 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4155 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4156 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4157 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4158 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4159 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4160 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4161 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4162 ) /* How's that for a conditional? */
4169 return (flags & ANYOF_INVERT) ? !match : match;
4173 S_reghop(pTHX_ U8 *s, I32 off)
4175 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4179 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4182 while (off-- && s < lim) {
4183 /* XXX could check well-formedness here */
4191 if (UTF8_IS_CONTINUED(*s)) {
4192 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4195 /* XXX could check well-formedness here */
4203 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4205 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4209 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4212 while (off-- && s < lim) {
4213 /* XXX could check well-formedness here */
4223 if (UTF8_IS_CONTINUED(*s)) {
4224 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4227 /* XXX could check well-formedness here */
4239 restore_pos(pTHX_ void *arg)
4241 if (PL_reg_eval_set) {
4242 if (PL_reg_oldsaved) {
4243 PL_reg_re->subbeg = PL_reg_oldsaved;
4244 PL_reg_re->sublen = PL_reg_oldsavedlen;
4245 RX_MATCH_COPIED_on(PL_reg_re);
4247 PL_reg_magic->mg_len = PL_reg_oldpos;
4248 PL_reg_eval_set = 0;
4249 PL_curpm = PL_reg_oldcurpm;