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;
393 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
396 if (prog->reganch & ROPT_UTF8) {
397 DEBUG_r(PerlIO_printf(Perl_debug_log,
398 "UTF-8 regex...\n"));
399 PL_reg_flags |= RF_utf8;
403 char *s = PL_reg_match_utf8 ?
404 sv_uni_display(dsv, sv, 60, 0) : strpos;
405 int len = PL_reg_match_utf8 ?
406 strlen(s) : strend - strpos;
409 if (PL_reg_match_utf8)
410 DEBUG_r(PerlIO_printf(Perl_debug_log,
411 "UTF-8 target...\n"));
412 PerlIO_printf(Perl_debug_log,
413 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
414 PL_colors[4],PL_colors[5],PL_colors[0],
417 (strlen(prog->precomp) > 60 ? "..." : ""),
419 (int)(len > 60 ? 60 : len),
421 (len > 60 ? "..." : "")
425 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
426 DEBUG_r(PerlIO_printf(Perl_debug_log,
427 "String too short... [re_intuit_start]\n"));
430 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
432 check = prog->check_substr;
433 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
434 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
435 || ( (prog->reganch & ROPT_ANCH_BOL)
436 && !PL_multiline ) ); /* Check after \n? */
439 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
440 | ROPT_IMPLICIT)) /* not a real BOL */
441 /* SvCUR is not set on references: SvRV and SvPVX overlap */
443 && (strpos != strbeg)) {
444 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
447 if (prog->check_offset_min == prog->check_offset_max &&
448 !(prog->reganch & ROPT_CANY_SEEN)) {
449 /* Substring at constant offset from beg-of-str... */
452 s = HOP3c(strpos, prog->check_offset_min, strend);
454 slen = SvCUR(check); /* >= 1 */
456 if ( strend - s > slen || strend - s < slen - 1
457 || (strend - s == slen && strend[-1] != '\n')) {
458 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
461 /* Now should match s[0..slen-2] */
463 if (slen && (*SvPVX(check) != *s
465 && memNE(SvPVX(check), s, slen)))) {
467 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
471 else if (*SvPVX(check) != *s
472 || ((slen = SvCUR(check)) > 1
473 && memNE(SvPVX(check), s, slen)))
475 goto success_at_start;
478 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
480 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
481 end_shift = prog->minlen - start_shift -
482 CHR_SVLEN(check) + (SvTAIL(check) != 0);
484 I32 end = prog->check_offset_max + CHR_SVLEN(check)
485 - (SvTAIL(check) != 0);
486 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
488 if (end_shift < eshift)
492 else { /* Can match at random position */
495 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
496 /* Should be nonnegative! */
497 end_shift = prog->minlen - start_shift -
498 CHR_SVLEN(check) + (SvTAIL(check) != 0);
501 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
503 Perl_croak(aTHX_ "panic: end_shift");
507 /* Find a possible match in the region s..strend by looking for
508 the "check" substring in the region corrected by start/end_shift. */
509 if (flags & REXEC_SCREAM) {
510 I32 p = -1; /* Internal iterator of scream. */
511 I32 *pp = data ? data->scream_pos : &p;
513 if (PL_screamfirst[BmRARE(check)] >= 0
514 || ( BmRARE(check) == '\n'
515 && (BmPREVIOUS(check) == SvCUR(check) - 1)
517 s = screaminstr(sv, check,
518 start_shift + (s - strbeg), end_shift, pp, 0);
522 *data->scream_olds = s;
524 else if (prog->reganch & ROPT_CANY_SEEN)
525 s = fbm_instr((U8*)(s + start_shift),
526 (U8*)(strend - end_shift),
527 check, PL_multiline ? FBMrf_MULTILINE : 0);
529 s = fbm_instr(HOP3(s, start_shift, strend),
530 HOP3(strend, -end_shift, strbeg),
531 check, PL_multiline ? FBMrf_MULTILINE : 0);
533 /* Update the count-of-usability, remove useless subpatterns,
536 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
537 (s ? "Found" : "Did not find"),
538 ((check == prog->anchored_substr) ? "anchored" : "floating"),
540 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
542 PL_colors[1], (SvTAIL(check) ? "$" : ""),
543 (s ? " at offset " : "...\n") ) );
550 /* Finish the diagnostic message */
551 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
553 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
554 Start with the other substr.
555 XXXX no SCREAM optimization yet - and a very coarse implementation
556 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
557 *always* match. Probably should be marked during compile...
558 Probably it is right to do no SCREAM here...
561 if (prog->float_substr && prog->anchored_substr) {
562 /* Take into account the "other" substring. */
563 /* XXXX May be hopelessly wrong for UTF... */
566 if (check == prog->float_substr) {
569 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
572 t = s - prog->check_offset_max;
573 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
574 && (!(prog->reganch & ROPT_UTF8)
575 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
580 t = HOP3c(t, prog->anchored_offset, strend);
581 if (t < other_last) /* These positions already checked */
583 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
586 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
587 /* On end-of-str: see comment below. */
588 s = fbm_instr((unsigned char*)t,
589 HOP3(HOP3(last1, prog->anchored_offset, strend)
590 + SvCUR(prog->anchored_substr),
591 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
592 prog->anchored_substr,
593 PL_multiline ? FBMrf_MULTILINE : 0);
594 DEBUG_r(PerlIO_printf(Perl_debug_log,
595 "%s anchored substr `%s%.*s%s'%s",
596 (s ? "Found" : "Contradicts"),
598 (int)(SvCUR(prog->anchored_substr)
599 - (SvTAIL(prog->anchored_substr)!=0)),
600 SvPVX(prog->anchored_substr),
601 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
603 if (last1 >= last2) {
604 DEBUG_r(PerlIO_printf(Perl_debug_log,
605 ", giving up...\n"));
608 DEBUG_r(PerlIO_printf(Perl_debug_log,
609 ", trying floating at offset %ld...\n",
610 (long)(HOP3c(s1, 1, strend) - i_strpos)));
611 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
612 s = HOP3c(last, 1, strend);
616 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
617 (long)(s - i_strpos)));
618 t = HOP3c(s, -prog->anchored_offset, strbeg);
619 other_last = HOP3c(s, 1, strend);
627 else { /* Take into account the floating substring. */
631 t = HOP3c(s, -start_shift, strbeg);
633 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
634 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
635 last = HOP3c(t, prog->float_max_offset, strend);
636 s = HOP3c(t, prog->float_min_offset, strend);
639 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
640 /* fbm_instr() takes into account exact value of end-of-str
641 if the check is SvTAIL(ed). Since false positives are OK,
642 and end-of-str is not later than strend we are OK. */
643 s = fbm_instr((unsigned char*)s,
644 (unsigned char*)last + SvCUR(prog->float_substr)
645 - (SvTAIL(prog->float_substr)!=0),
646 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
647 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
648 (s ? "Found" : "Contradicts"),
650 (int)(SvCUR(prog->float_substr)
651 - (SvTAIL(prog->float_substr)!=0)),
652 SvPVX(prog->float_substr),
653 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
656 DEBUG_r(PerlIO_printf(Perl_debug_log,
657 ", giving up...\n"));
660 DEBUG_r(PerlIO_printf(Perl_debug_log,
661 ", trying anchored starting at offset %ld...\n",
662 (long)(s1 + 1 - i_strpos)));
664 s = HOP3c(t, 1, strend);
668 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
669 (long)(s - i_strpos)));
670 other_last = s; /* Fix this later. --Hugo */
679 t = s - prog->check_offset_max;
680 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
681 && (!(prog->reganch & ROPT_UTF8)
682 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
684 /* Fixed substring is found far enough so that the match
685 cannot start at strpos. */
687 if (ml_anch && t[-1] != '\n') {
688 /* Eventually fbm_*() should handle this, but often
689 anchored_offset is not 0, so this check will not be wasted. */
690 /* XXXX In the code below we prefer to look for "^" even in
691 presence of anchored substrings. And we search even
692 beyond the found float position. These pessimizations
693 are historical artefacts only. */
695 while (t < strend - prog->minlen) {
697 if (t < check_at - prog->check_offset_min) {
698 if (prog->anchored_substr) {
699 /* Since we moved from the found position,
700 we definitely contradict the found anchored
701 substr. Due to the above check we do not
702 contradict "check" substr.
703 Thus we can arrive here only if check substr
704 is float. Redo checking for "other"=="fixed".
707 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
708 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
709 goto do_other_anchored;
711 /* We don't contradict the found floating substring. */
712 /* XXXX Why not check for STCLASS? */
714 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
715 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
718 /* Position contradicts check-string */
719 /* XXXX probably better to look for check-string
720 than for "\n", so one should lower the limit for t? */
721 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
722 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
723 other_last = strpos = s = t + 1;
728 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
729 PL_colors[0],PL_colors[1]));
733 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
734 PL_colors[0],PL_colors[1]));
738 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
741 /* The found string does not prohibit matching at strpos,
742 - no optimization of calling REx engine can be performed,
743 unless it was an MBOL and we are not after MBOL,
744 or a future STCLASS check will fail this. */
746 /* Even in this situation we may use MBOL flag if strpos is offset
747 wrt the start of the string. */
748 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
749 && (strpos != strbeg) && strpos[-1] != '\n'
750 /* May be due to an implicit anchor of m{.*foo} */
751 && !(prog->reganch & ROPT_IMPLICIT))
756 DEBUG_r( if (ml_anch)
757 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
758 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
761 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
762 && prog->check_substr /* Could be deleted already */
763 && --BmUSEFUL(prog->check_substr) < 0
764 && prog->check_substr == prog->float_substr)
766 /* If flags & SOMETHING - do not do it many times on the same match */
767 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
768 SvREFCNT_dec(prog->check_substr);
769 prog->check_substr = Nullsv; /* disable */
770 prog->float_substr = Nullsv; /* clear */
771 check = Nullsv; /* abort */
773 /* XXXX This is a remnant of the old implementation. It
774 looks wasteful, since now INTUIT can use many
776 prog->reganch &= ~RE_USE_INTUIT;
783 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
784 if (prog->regstclass) {
785 /* minlen == 0 is possible if regstclass is \b or \B,
786 and the fixed substr is ''$.
787 Since minlen is already taken into account, s+1 is before strend;
788 accidentally, minlen >= 1 guaranties no false positives at s + 1
789 even for \b or \B. But (minlen? 1 : 0) below assumes that
790 regstclass does not come from lookahead... */
791 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
792 This leaves EXACTF only, which is dealt with in find_byclass(). */
793 U8* str = (U8*)STRING(prog->regstclass);
794 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
795 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
797 char *endpos = (prog->anchored_substr || ml_anch)
798 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
799 : (prog->float_substr
800 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
803 char *startpos = strbeg;
806 if (prog->reganch & ROPT_UTF8) {
807 PL_regdata = prog->data;
810 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
815 if (endpos == strend) {
816 DEBUG_r( PerlIO_printf(Perl_debug_log,
817 "Could not match STCLASS...\n") );
820 DEBUG_r( PerlIO_printf(Perl_debug_log,
821 "This position contradicts STCLASS...\n") );
822 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
824 /* Contradict one of substrings */
825 if (prog->anchored_substr) {
826 if (prog->anchored_substr == check) {
827 DEBUG_r( what = "anchored" );
829 s = HOP3c(t, 1, strend);
830 if (s + start_shift + end_shift > strend) {
831 /* XXXX Should be taken into account earlier? */
832 DEBUG_r( PerlIO_printf(Perl_debug_log,
833 "Could not match STCLASS...\n") );
838 DEBUG_r( PerlIO_printf(Perl_debug_log,
839 "Looking for %s substr starting at offset %ld...\n",
840 what, (long)(s + start_shift - i_strpos)) );
843 /* Have both, check_string is floating */
844 if (t + start_shift >= check_at) /* Contradicts floating=check */
845 goto retry_floating_check;
846 /* Recheck anchored substring, but not floating... */
850 DEBUG_r( PerlIO_printf(Perl_debug_log,
851 "Looking for anchored substr starting at offset %ld...\n",
852 (long)(other_last - i_strpos)) );
853 goto do_other_anchored;
855 /* Another way we could have checked stclass at the
856 current position only: */
861 DEBUG_r( PerlIO_printf(Perl_debug_log,
862 "Looking for /%s^%s/m starting at offset %ld...\n",
863 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
866 if (!prog->float_substr) /* Could have been deleted */
868 /* Check is floating subtring. */
869 retry_floating_check:
870 t = check_at - start_shift;
871 DEBUG_r( what = "floating" );
872 goto hop_and_restart;
875 DEBUG_r(PerlIO_printf(Perl_debug_log,
876 "By STCLASS: moving %ld --> %ld\n",
877 (long)(t - i_strpos), (long)(s - i_strpos))
881 DEBUG_r(PerlIO_printf(Perl_debug_log,
882 "Does not contradict STCLASS...\n");
887 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
888 PL_colors[4], (check ? "Guessed" : "Giving up"),
889 PL_colors[5], (long)(s - i_strpos)) );
892 fail_finish: /* Substring not found */
893 if (prog->check_substr) /* could be removed already */
894 BmUSEFUL(prog->check_substr) += 5; /* hooray */
896 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
897 PL_colors[4],PL_colors[5]));
901 /* We know what class REx starts with. Try to find this position... */
903 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
905 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
911 register I32 tmp = 1; /* Scratch variable? */
912 register bool do_utf8 = PL_reg_match_utf8;
914 /* We know what class it must start with. */
918 if (reginclass(c, (U8*)s, do_utf8)) {
919 if (tmp && (norun || regtry(prog, s)))
926 s += do_utf8 ? UTF8SKIP(s) : 1;
931 if (tmp && (norun || regtry(prog, s)))
943 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
944 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
946 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
947 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
949 c1 = utf8_to_uvuni(tmpbuf1, 0);
950 c2 = utf8_to_uvuni(tmpbuf2, 0);
961 c2 = PL_fold_locale[c1];
963 e = do_utf8 ? s + ln : strend - ln;
966 e = s; /* Due to minlen logic of intuit() */
968 /* The idea in the EXACTF* cases is to first find the
969 * first character of the EXACTF* node and then, if
970 * necessary, case-insensitively compare the full
971 * text of the node. The c1 and c2 are the first
972 * characters (though in Unicode it gets a bit
973 * more complicated because there are more cases
974 * than just upper and lower: one is really supposed
975 * to use the so-called folding case for case-insensitive
976 * matching (called "loose matching" in Unicode). */
980 U8 tmpbuf [UTF8_MAXLEN+1];
981 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
986 c = utf8_to_uvchr((U8*)s, &len);
989 ibcmp_utf8(s, (char **)0, 0, do_utf8,
990 m, (char **)0, ln, UTF))
991 && (norun || regtry(prog, s)) )
994 uvchr_to_utf8(tmpbuf, c);
995 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
997 && (f == c1 || f == c2)
999 !ibcmp_utf8((char *) foldbuf,
1000 (char **)0, foldlen, do_utf8,
1002 (char **)0, ln, UTF))
1003 && (norun || regtry(prog, s)) )
1011 c = utf8_to_uvchr((U8*)s, &len);
1013 /* Handle some of the three Greek sigmas cases.
1014 * Note that not all the possible combinations
1015 * are handled here: some of them are handled
1016 * handled by the standard folding rules, and
1017 * some of them (the character class or ANYOF
1018 * cases) are handled during compiletime in
1019 * regexec.c:S_regclass(). */
1020 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1021 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1022 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1024 if ( (c == c1 || c == c2)
1026 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1027 m, (char **)0, ln, UTF))
1028 && (norun || regtry(prog, s)) )
1031 uvchr_to_utf8(tmpbuf, c);
1032 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1034 && (f == c1 || f == c2)
1035 && (ln == foldlen ||
1036 !ibcmp_utf8((char *)foldbuf,
1037 (char **)0, foldlen, do_utf8,
1039 (char **)0, ln, UTF))
1040 && (norun || regtry(prog, s)) )
1051 && (ln == 1 || !(OP(c) == EXACTF
1053 : ibcmp_locale(s, m, ln)))
1054 && (norun || regtry(prog, s)) )
1060 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1061 && (ln == 1 || !(OP(c) == EXACTF
1063 : ibcmp_locale(s, m, ln)))
1064 && (norun || regtry(prog, s)) )
1071 PL_reg_flags |= RF_tainted;
1078 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1081 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1083 tmp = ((OP(c) == BOUND ?
1084 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1085 LOAD_UTF8_CHARCLASS(alnum,"a");
1086 while (s < strend) {
1087 if (tmp == !(OP(c) == BOUND ?
1088 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1089 isALNUM_LC_utf8((U8*)s)))
1092 if ((norun || regtry(prog, s)))
1099 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1100 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1101 while (s < strend) {
1103 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1105 if ((norun || regtry(prog, s)))
1111 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1115 PL_reg_flags |= RF_tainted;
1122 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1125 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1127 tmp = ((OP(c) == NBOUND ?
1128 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1129 LOAD_UTF8_CHARCLASS(alnum,"a");
1130 while (s < strend) {
1131 if (tmp == !(OP(c) == NBOUND ?
1132 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1133 isALNUM_LC_utf8((U8*)s)))
1135 else if ((norun || regtry(prog, s)))
1141 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1142 tmp = ((OP(c) == NBOUND ?
1143 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1144 while (s < strend) {
1146 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1148 else if ((norun || regtry(prog, s)))
1153 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1158 LOAD_UTF8_CHARCLASS(alnum,"a");
1159 while (s < strend) {
1160 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1161 if (tmp && (norun || regtry(prog, s)))
1172 while (s < strend) {
1174 if (tmp && (norun || regtry(prog, s)))
1186 PL_reg_flags |= RF_tainted;
1188 while (s < strend) {
1189 if (isALNUM_LC_utf8((U8*)s)) {
1190 if (tmp && (norun || regtry(prog, s)))
1201 while (s < strend) {
1202 if (isALNUM_LC(*s)) {
1203 if (tmp && (norun || regtry(prog, s)))
1216 LOAD_UTF8_CHARCLASS(alnum,"a");
1217 while (s < strend) {
1218 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1219 if (tmp && (norun || regtry(prog, s)))
1230 while (s < strend) {
1232 if (tmp && (norun || regtry(prog, s)))
1244 PL_reg_flags |= RF_tainted;
1246 while (s < strend) {
1247 if (!isALNUM_LC_utf8((U8*)s)) {
1248 if (tmp && (norun || regtry(prog, s)))
1259 while (s < strend) {
1260 if (!isALNUM_LC(*s)) {
1261 if (tmp && (norun || regtry(prog, s)))
1274 LOAD_UTF8_CHARCLASS(space," ");
1275 while (s < strend) {
1276 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1277 if (tmp && (norun || regtry(prog, s)))
1288 while (s < strend) {
1290 if (tmp && (norun || regtry(prog, s)))
1302 PL_reg_flags |= RF_tainted;
1304 while (s < strend) {
1305 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1306 if (tmp && (norun || regtry(prog, s)))
1317 while (s < strend) {
1318 if (isSPACE_LC(*s)) {
1319 if (tmp && (norun || regtry(prog, s)))
1332 LOAD_UTF8_CHARCLASS(space," ");
1333 while (s < strend) {
1334 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1335 if (tmp && (norun || regtry(prog, s)))
1346 while (s < strend) {
1348 if (tmp && (norun || regtry(prog, s)))
1360 PL_reg_flags |= RF_tainted;
1362 while (s < strend) {
1363 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1364 if (tmp && (norun || regtry(prog, s)))
1375 while (s < strend) {
1376 if (!isSPACE_LC(*s)) {
1377 if (tmp && (norun || regtry(prog, s)))
1390 LOAD_UTF8_CHARCLASS(digit,"0");
1391 while (s < strend) {
1392 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1393 if (tmp && (norun || regtry(prog, s)))
1404 while (s < strend) {
1406 if (tmp && (norun || regtry(prog, s)))
1418 PL_reg_flags |= RF_tainted;
1420 while (s < strend) {
1421 if (isDIGIT_LC_utf8((U8*)s)) {
1422 if (tmp && (norun || regtry(prog, s)))
1433 while (s < strend) {
1434 if (isDIGIT_LC(*s)) {
1435 if (tmp && (norun || regtry(prog, s)))
1448 LOAD_UTF8_CHARCLASS(digit,"0");
1449 while (s < strend) {
1450 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1451 if (tmp && (norun || regtry(prog, s)))
1462 while (s < strend) {
1464 if (tmp && (norun || regtry(prog, s)))
1476 PL_reg_flags |= RF_tainted;
1478 while (s < strend) {
1479 if (!isDIGIT_LC_utf8((U8*)s)) {
1480 if (tmp && (norun || regtry(prog, s)))
1491 while (s < strend) {
1492 if (!isDIGIT_LC(*s)) {
1493 if (tmp && (norun || regtry(prog, s)))
1505 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1514 - regexec_flags - match a regexp against a string
1517 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1518 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1519 /* strend: pointer to null at end of string */
1520 /* strbeg: real beginning of string */
1521 /* minend: end of match must be >=minend after stringarg. */
1522 /* data: May be used for some additional optimizations. */
1523 /* nosave: For optimizations. */
1526 register regnode *c;
1527 register char *startpos = stringarg;
1528 I32 minlen; /* must match at least this many chars */
1529 I32 dontbother = 0; /* how many characters not to try at end */
1530 /* I32 start_shift = 0; */ /* Offset of the start to find
1531 constant substr. */ /* CC */
1532 I32 end_shift = 0; /* Same for the end. */ /* CC */
1533 I32 scream_pos = -1; /* Internal iterator of scream. */
1535 SV* oreplsv = GvSV(PL_replgv);
1536 bool do_utf8 = DO_UTF8(sv);
1538 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
1545 PL_regnarrate = DEBUG_r_TEST;
1548 /* Be paranoid... */
1549 if (prog == NULL || startpos == NULL) {
1550 Perl_croak(aTHX_ "NULL regexp parameter");
1554 minlen = prog->minlen;
1555 if (strend - startpos < minlen) {
1556 DEBUG_r(PerlIO_printf(Perl_debug_log,
1557 "String too short [regexec_flags]...\n"));
1561 /* Check validity of program. */
1562 if (UCHARAT(prog->program) != REG_MAGIC) {
1563 Perl_croak(aTHX_ "corrupted regexp program");
1567 PL_reg_eval_set = 0;
1570 if (prog->reganch & ROPT_UTF8)
1571 PL_reg_flags |= RF_utf8;
1573 /* Mark beginning of line for ^ and lookbehind. */
1574 PL_regbol = startpos;
1578 /* Mark end of line for $ (and such) */
1581 /* see how far we have to get to not match where we matched before */
1582 PL_regtill = startpos+minend;
1584 /* We start without call_cc context. */
1587 /* If there is a "must appear" string, look for it. */
1590 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1593 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1594 PL_reg_ganch = startpos;
1595 else if (sv && SvTYPE(sv) >= SVt_PVMG
1597 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1598 && mg->mg_len >= 0) {
1599 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1600 if (prog->reganch & ROPT_ANCH_GPOS) {
1601 if (s > PL_reg_ganch)
1606 else /* pos() not defined */
1607 PL_reg_ganch = strbeg;
1610 if (do_utf8 == (UTF!=0) &&
1611 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1612 re_scream_pos_data d;
1614 d.scream_olds = &scream_olds;
1615 d.scream_pos = &scream_pos;
1616 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1618 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1619 goto phooey; /* not present */
1624 char *s = do_utf8 ? sv_uni_display(dsv, sv, 60, 0) : startpos;
1625 int len = do_utf8 ? strlen(s) : strend - startpos;
1628 PerlIO_printf(Perl_debug_log,
1629 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1630 PL_colors[4],PL_colors[5],PL_colors[0],
1633 (strlen(prog->precomp) > 60 ? "..." : ""),
1635 (int)(len > 60 ? 60 : len),
1637 (len > 60 ? "..." : "")
1641 /* Simplest case: anchored match need be tried only once. */
1642 /* [unless only anchor is BOL and multiline is set] */
1643 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1644 if (s == startpos && regtry(prog, startpos))
1646 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1647 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1652 dontbother = minlen - 1;
1653 end = HOP3c(strend, -dontbother, strbeg) - 1;
1654 /* for multiline we only have to try after newlines */
1655 if (prog->check_substr) {
1659 if (regtry(prog, s))
1664 if (prog->reganch & RE_USE_INTUIT) {
1665 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1676 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1677 if (regtry(prog, s))
1684 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1685 if (regtry(prog, PL_reg_ganch))
1690 /* Messy cases: unanchored match. */
1691 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1692 /* we have /x+whatever/ */
1693 /* it must be a one character string (XXXX Except UTF?) */
1694 char ch = SvPVX(prog->anchored_substr)[0];
1700 while (s < strend) {
1702 DEBUG_r( did_match = 1 );
1703 if (regtry(prog, s)) goto got_it;
1705 while (s < strend && *s == ch)
1712 while (s < strend) {
1714 DEBUG_r( did_match = 1 );
1715 if (regtry(prog, s)) goto got_it;
1717 while (s < strend && *s == ch)
1723 DEBUG_r(if (!did_match)
1724 PerlIO_printf(Perl_debug_log,
1725 "Did not find anchored character...\n")
1729 else if (do_utf8 == (UTF!=0) &&
1730 (prog->anchored_substr != Nullsv
1731 || (prog->float_substr != Nullsv
1732 && prog->float_max_offset < strend - s))) {
1733 SV *must = prog->anchored_substr
1734 ? prog->anchored_substr : prog->float_substr;
1736 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1738 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1739 char *last = HOP3c(strend, /* Cannot start after this */
1740 -(I32)(CHR_SVLEN(must)
1741 - (SvTAIL(must) != 0) + back_min), strbeg);
1742 char *last1; /* Last position checked before */
1748 last1 = HOPc(s, -1);
1750 last1 = s - 1; /* bogus */
1752 /* XXXX check_substr already used to find `s', can optimize if
1753 check_substr==must. */
1755 dontbother = end_shift;
1756 strend = HOPc(strend, -dontbother);
1757 while ( (s <= last) &&
1758 ((flags & REXEC_SCREAM)
1759 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1760 end_shift, &scream_pos, 0))
1761 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1762 (unsigned char*)strend, must,
1763 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1764 DEBUG_r( did_match = 1 );
1765 if (HOPc(s, -back_max) > last1) {
1766 last1 = HOPc(s, -back_min);
1767 s = HOPc(s, -back_max);
1770 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1772 last1 = HOPc(s, -back_min);
1776 while (s <= last1) {
1777 if (regtry(prog, s))
1783 while (s <= last1) {
1784 if (regtry(prog, s))
1790 DEBUG_r(if (!did_match)
1791 PerlIO_printf(Perl_debug_log,
1792 "Did not find %s substr `%s%.*s%s'%s...\n",
1793 ((must == prog->anchored_substr)
1794 ? "anchored" : "floating"),
1796 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1798 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1802 else if ((c = prog->regstclass)) {
1803 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1804 /* don't bother with what can't match */
1805 strend = HOPc(strend, -(minlen - 1));
1807 SV *prop = sv_newmortal();
1809 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), UTF ? sv_uni_display(dsv, sv, 60, 0) : s);
1811 if (find_byclass(prog, c, s, strend, startpos, 0))
1813 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1817 if (prog->float_substr != Nullsv) { /* Trim the end. */
1820 if (flags & REXEC_SCREAM) {
1821 last = screaminstr(sv, prog->float_substr, s - strbeg,
1822 end_shift, &scream_pos, 1); /* last one */
1824 last = scream_olds; /* Only one occurrence. */
1828 char *little = SvPV(prog->float_substr, len);
1830 if (SvTAIL(prog->float_substr)) {
1831 if (memEQ(strend - len + 1, little, len - 1))
1832 last = strend - len + 1;
1833 else if (!PL_multiline)
1834 last = memEQ(strend - len, little, len)
1835 ? strend - len : Nullch;
1841 last = rninstr(s, strend, little, little + len);
1843 last = strend; /* matching `$' */
1847 DEBUG_r(PerlIO_printf(Perl_debug_log,
1848 "%sCan't trim the tail, match fails (should not happen)%s\n",
1849 PL_colors[4],PL_colors[5]));
1850 goto phooey; /* Should not happen! */
1852 dontbother = strend - last + prog->float_min_offset;
1854 if (minlen && (dontbother < minlen))
1855 dontbother = minlen - 1;
1856 strend -= dontbother; /* this one's always in bytes! */
1857 /* We don't know much -- general case. */
1860 if (regtry(prog, s))
1869 if (regtry(prog, s))
1871 } while (s++ < strend);
1879 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1881 if (PL_reg_eval_set) {
1882 /* Preserve the current value of $^R */
1883 if (oreplsv != GvSV(PL_replgv))
1884 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1885 restored, the value remains
1887 restore_pos(aTHX_ 0);
1890 /* make sure $`, $&, $', and $digit will work later */
1891 if ( !(flags & REXEC_NOT_FIRST) ) {
1892 if (RX_MATCH_COPIED(prog)) {
1893 Safefree(prog->subbeg);
1894 RX_MATCH_COPIED_off(prog);
1896 if (flags & REXEC_COPY_STR) {
1897 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1899 s = savepvn(strbeg, i);
1902 RX_MATCH_COPIED_on(prog);
1905 prog->subbeg = strbeg;
1906 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1913 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1914 PL_colors[4],PL_colors[5]));
1915 if (PL_reg_eval_set)
1916 restore_pos(aTHX_ 0);
1921 - regtry - try match at specific point
1923 STATIC I32 /* 0 failure, 1 success */
1924 S_regtry(pTHX_ regexp *prog, char *startpos)
1932 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1934 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1937 PL_reg_eval_set = RS_init;
1939 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1940 (IV)(PL_stack_sp - PL_stack_base));
1942 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1943 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1944 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1946 /* Apparently this is not needed, judging by wantarray. */
1947 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1948 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1951 /* Make $_ available to executed code. */
1952 if (PL_reg_sv != DEFSV) {
1953 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1958 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1959 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1960 /* prepare for quick setting of pos */
1961 sv_magic(PL_reg_sv, (SV*)0,
1962 PERL_MAGIC_regex_global, Nullch, 0);
1963 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1967 PL_reg_oldpos = mg->mg_len;
1968 SAVEDESTRUCTOR_X(restore_pos, 0);
1970 if (!PL_reg_curpm) {
1971 Newz(22,PL_reg_curpm, 1, PMOP);
1974 SV* repointer = newSViv(0);
1975 /* so we know which PL_regex_padav element is PL_reg_curpm */
1976 SvFLAGS(repointer) |= SVf_BREAK;
1977 av_push(PL_regex_padav,repointer);
1978 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
1979 PL_regex_pad = AvARRAY(PL_regex_padav);
1983 PM_SETRE(PL_reg_curpm, prog);
1984 PL_reg_oldcurpm = PL_curpm;
1985 PL_curpm = PL_reg_curpm;
1986 if (RX_MATCH_COPIED(prog)) {
1987 /* Here is a serious problem: we cannot rewrite subbeg,
1988 since it may be needed if this match fails. Thus
1989 $` inside (?{}) could fail... */
1990 PL_reg_oldsaved = prog->subbeg;
1991 PL_reg_oldsavedlen = prog->sublen;
1992 RX_MATCH_COPIED_off(prog);
1995 PL_reg_oldsaved = Nullch;
1996 prog->subbeg = PL_bostr;
1997 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1999 prog->startp[0] = startpos - PL_bostr;
2000 PL_reginput = startpos;
2001 PL_regstartp = prog->startp;
2002 PL_regendp = prog->endp;
2003 PL_reglastparen = &prog->lastparen;
2004 PL_reglastcloseparen = &prog->lastcloseparen;
2005 prog->lastparen = 0;
2007 DEBUG_r(PL_reg_starttry = startpos);
2008 if (PL_reg_start_tmpl <= prog->nparens) {
2009 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2010 if(PL_reg_start_tmp)
2011 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2013 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2017 sv_setpvn(PERL_DEBUG_PAD(0), "", 0);
2018 sv_setpvn(PERL_DEBUG_PAD(1), "", 0);
2019 sv_setpvn(PERL_DEBUG_PAD(2), "", 0);
2022 /* XXXX What this code is doing here?!!! There should be no need
2023 to do this again and again, PL_reglastparen should take care of
2026 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2027 * Actually, the code in regcppop() (which Ilya may be meaning by
2028 * PL_reglastparen), is not needed at all by the test suite
2029 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2030 * enough, for building DynaLoader, or otherwise this
2031 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2032 * will happen. Meanwhile, this code *is* needed for the
2033 * above-mentioned test suite tests to succeed. The common theme
2034 * on those tests seems to be returning null fields from matches.
2039 if (prog->nparens) {
2040 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2047 if (regmatch(prog->program + 1)) {
2048 prog->endp[0] = PL_reginput - PL_bostr;
2051 REGCP_UNWIND(lastcp);
2055 #define RE_UNWIND_BRANCH 1
2056 #define RE_UNWIND_BRANCHJ 2
2060 typedef struct { /* XX: makes sense to enlarge it... */
2064 } re_unwind_generic_t;
2077 } re_unwind_branch_t;
2079 typedef union re_unwind_t {
2081 re_unwind_generic_t generic;
2082 re_unwind_branch_t branch;
2085 #define sayYES goto yes
2086 #define sayNO goto no
2087 #define sayYES_FINAL goto yes_final
2088 #define sayYES_LOUD goto yes_loud
2089 #define sayNO_FINAL goto no_final
2090 #define sayNO_SILENT goto do_no
2091 #define saySAME(x) if (x) goto yes; else goto no
2093 #define REPORT_CODE_OFF 24
2096 - regmatch - main matching routine
2098 * Conceptually the strategy is simple: check to see whether the current
2099 * node matches, call self recursively to see whether the rest matches,
2100 * and then act accordingly. In practice we make some effort to avoid
2101 * recursion, in particular by going through "ordinary" nodes (that don't
2102 * need to know whether the rest of the match failed) by a loop instead of
2105 /* [lwall] I've hoisted the register declarations to the outer block in order to
2106 * maybe save a little bit of pushing and popping on the stack. It also takes
2107 * advantage of machines that use a register save mask on subroutine entry.
2109 STATIC I32 /* 0 failure, 1 success */
2110 S_regmatch(pTHX_ regnode *prog)
2112 register regnode *scan; /* Current node. */
2113 regnode *next; /* Next node. */
2114 regnode *inner; /* Next node in internal branch. */
2115 register I32 nextchr; /* renamed nextchr - nextchar colides with
2116 function of same name */
2117 register I32 n; /* no or next */
2118 register I32 ln = 0; /* len or last */
2119 register char *s = Nullch; /* operand or save */
2120 register char *locinput = PL_reginput;
2121 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2122 int minmod = 0, sw = 0, logical = 0;
2125 I32 firstcp = PL_savestack_ix;
2127 register bool do_utf8 = PL_reg_match_utf8;
2129 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2130 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2131 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2138 /* Note that nextchr is a byte even in UTF */
2139 nextchr = UCHARAT(locinput);
2141 while (scan != NULL) {
2144 SV *prop = sv_newmortal();
2145 int docolor = *PL_colors[0];
2146 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2147 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2148 /* The part of the string before starttry has one color
2149 (pref0_len chars), between starttry and current
2150 position another one (pref_len - pref0_len chars),
2151 after the current position the third one.
2152 We assume that pref0_len <= pref_len, otherwise we
2153 decrease pref0_len. */
2154 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2155 ? (5 + taill) - l : locinput - PL_bostr;
2158 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2160 pref0_len = pref_len - (locinput - PL_reg_starttry);
2161 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2162 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2163 ? (5 + taill) - pref_len : PL_regeol - locinput);
2164 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2168 if (pref0_len > pref_len)
2169 pref0_len = pref_len;
2170 regprop(prop, scan);
2174 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2176 locinput - pref_len;
2177 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2178 char *s1 = do_utf8 ?
2179 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2180 pref_len - pref0_len, 60, 0) :
2181 locinput - pref_len + pref0_len;
2182 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2183 char *s2 = do_utf8 ?
2184 pv_uni_display(dsv2, (U8*)locinput,
2185 PL_regeol - locinput, 60, 0) :
2187 int len2 = do_utf8 ? strlen(s2) : l;
2188 PerlIO_printf(Perl_debug_log,
2189 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2190 (IV)(locinput - PL_bostr),
2197 (docolor ? "" : "> <"),
2201 15 - l - pref_len + 1,
2203 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2208 next = scan + NEXT_OFF(scan);
2214 if (locinput == PL_bostr || (PL_multiline &&
2215 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2217 /* regtill = regbol; */
2222 if (locinput == PL_bostr ||
2223 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2229 if (locinput == PL_bostr)
2233 if (locinput == PL_reg_ganch)
2243 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2248 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2250 if (PL_regeol - locinput > 1)
2254 if (PL_regeol != locinput)
2258 if (!nextchr && locinput >= PL_regeol)
2261 locinput += PL_utf8skip[nextchr];
2262 if (locinput > PL_regeol)
2264 nextchr = UCHARAT(locinput);
2267 nextchr = UCHARAT(++locinput);
2270 if (!nextchr && locinput >= PL_regeol)
2272 nextchr = UCHARAT(++locinput);
2275 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2278 locinput += PL_utf8skip[nextchr];
2279 if (locinput > PL_regeol)
2281 nextchr = UCHARAT(locinput);
2284 nextchr = UCHARAT(++locinput);
2289 if (do_utf8 != (UTF!=0)) {
2290 /* The target and the pattern have differing utf8ness. */
2296 /* The target is utf8, the pattern is not utf8. */
2300 if (NATIVE_TO_UNI(*(U8*)s) !=
2301 utf8_to_uvchr((U8*)l, &ulen))
2308 /* The target is not utf8, the pattern is utf8. */
2312 if (NATIVE_TO_UNI(*((U8*)l)) !=
2313 utf8_to_uvchr((U8*)s, &ulen))
2320 nextchr = UCHARAT(locinput);
2323 /* The target and the pattern have the same utf8ness. */
2324 /* Inline the first character, for speed. */
2325 if (UCHARAT(s) != nextchr)
2327 if (PL_regeol - locinput < ln)
2329 if (ln > 1 && memNE(s, locinput, ln))
2332 nextchr = UCHARAT(locinput);
2335 PL_reg_flags |= RF_tainted;
2341 if (do_utf8 || UTF) {
2342 /* Either target or the pattern are utf8. */
2344 char *e = PL_regeol;
2346 if (ibcmp_utf8(s, 0, ln, do_utf8,
2350 nextchr = UCHARAT(locinput);
2354 /* Neither the target and the pattern are utf8. */
2356 /* Inline the first character, for speed. */
2357 if (UCHARAT(s) != nextchr &&
2358 UCHARAT(s) != ((OP(scan) == EXACTF)
2359 ? PL_fold : PL_fold_locale)[nextchr])
2361 if (PL_regeol - locinput < ln)
2363 if (ln > 1 && (OP(scan) == EXACTF
2364 ? ibcmp(s, locinput, ln)
2365 : ibcmp_locale(s, locinput, ln)))
2368 nextchr = UCHARAT(locinput);
2372 if (!reginclass(scan, (U8*)locinput, do_utf8))
2374 if (locinput >= PL_regeol)
2376 locinput += PL_utf8skip[nextchr];
2377 nextchr = UCHARAT(locinput);
2381 nextchr = UCHARAT(locinput);
2382 if (!reginclass(scan, (U8*)locinput, do_utf8))
2384 if (!nextchr && locinput >= PL_regeol)
2386 nextchr = UCHARAT(++locinput);
2390 PL_reg_flags |= RF_tainted;
2396 LOAD_UTF8_CHARCLASS(alnum,"a");
2397 if (!(OP(scan) == ALNUM
2398 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2399 : isALNUM_LC_utf8((U8*)locinput)))
2403 locinput += PL_utf8skip[nextchr];
2404 nextchr = UCHARAT(locinput);
2407 if (!(OP(scan) == ALNUM
2408 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2410 nextchr = UCHARAT(++locinput);
2413 PL_reg_flags |= RF_tainted;
2416 if (!nextchr && locinput >= PL_regeol)
2419 LOAD_UTF8_CHARCLASS(alnum,"a");
2420 if (OP(scan) == NALNUM
2421 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2422 : isALNUM_LC_utf8((U8*)locinput))
2426 locinput += PL_utf8skip[nextchr];
2427 nextchr = UCHARAT(locinput);
2430 if (OP(scan) == NALNUM
2431 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2433 nextchr = UCHARAT(++locinput);
2437 PL_reg_flags |= RF_tainted;
2441 /* was last char in word? */
2443 if (locinput == PL_bostr)
2446 U8 *r = reghop((U8*)locinput, -1);
2448 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2450 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2451 ln = isALNUM_uni(ln);
2452 LOAD_UTF8_CHARCLASS(alnum,"a");
2453 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2456 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2457 n = isALNUM_LC_utf8((U8*)locinput);
2461 ln = (locinput != PL_bostr) ?
2462 UCHARAT(locinput - 1) : '\n';
2463 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2465 n = isALNUM(nextchr);
2468 ln = isALNUM_LC(ln);
2469 n = isALNUM_LC(nextchr);
2472 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2473 OP(scan) == BOUNDL))
2477 PL_reg_flags |= RF_tainted;
2483 if (UTF8_IS_CONTINUED(nextchr)) {
2484 LOAD_UTF8_CHARCLASS(space," ");
2485 if (!(OP(scan) == SPACE
2486 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2487 : isSPACE_LC_utf8((U8*)locinput)))
2491 locinput += PL_utf8skip[nextchr];
2492 nextchr = UCHARAT(locinput);
2495 if (!(OP(scan) == SPACE
2496 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2498 nextchr = UCHARAT(++locinput);
2501 if (!(OP(scan) == SPACE
2502 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2504 nextchr = UCHARAT(++locinput);
2508 PL_reg_flags |= RF_tainted;
2511 if (!nextchr && locinput >= PL_regeol)
2514 LOAD_UTF8_CHARCLASS(space," ");
2515 if (OP(scan) == NSPACE
2516 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2517 : isSPACE_LC_utf8((U8*)locinput))
2521 locinput += PL_utf8skip[nextchr];
2522 nextchr = UCHARAT(locinput);
2525 if (OP(scan) == NSPACE
2526 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2528 nextchr = UCHARAT(++locinput);
2531 PL_reg_flags |= RF_tainted;
2537 LOAD_UTF8_CHARCLASS(digit,"0");
2538 if (!(OP(scan) == DIGIT
2539 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2540 : isDIGIT_LC_utf8((U8*)locinput)))
2544 locinput += PL_utf8skip[nextchr];
2545 nextchr = UCHARAT(locinput);
2548 if (!(OP(scan) == DIGIT
2549 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2551 nextchr = UCHARAT(++locinput);
2554 PL_reg_flags |= RF_tainted;
2557 if (!nextchr && locinput >= PL_regeol)
2560 LOAD_UTF8_CHARCLASS(digit,"0");
2561 if (OP(scan) == NDIGIT
2562 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2563 : isDIGIT_LC_utf8((U8*)locinput))
2567 locinput += PL_utf8skip[nextchr];
2568 nextchr = UCHARAT(locinput);
2571 if (OP(scan) == NDIGIT
2572 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2574 nextchr = UCHARAT(++locinput);
2577 if (locinput >= PL_regeol)
2580 LOAD_UTF8_CHARCLASS(mark,"~");
2581 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2583 locinput += PL_utf8skip[nextchr];
2584 while (locinput < PL_regeol &&
2585 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2586 locinput += UTF8SKIP(locinput);
2587 if (locinput > PL_regeol)
2592 nextchr = UCHARAT(locinput);
2595 PL_reg_flags |= RF_tainted;
2599 n = ARG(scan); /* which paren pair */
2600 ln = PL_regstartp[n];
2601 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2602 if (*PL_reglastparen < n || ln == -1)
2603 sayNO; /* Do not match unless seen CLOSEn. */
2604 if (ln == PL_regendp[n])
2608 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2610 char *e = PL_bostr + PL_regendp[n];
2612 * Note that we can't do the "other character" lookup trick as
2613 * in the 8-bit case (no pun intended) because in Unicode we
2614 * have to map both upper and title case to lower case.
2616 if (OP(scan) == REFF) {
2617 STRLEN ulen1, ulen2;
2618 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2619 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2623 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2624 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2625 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2632 nextchr = UCHARAT(locinput);
2636 /* Inline the first character, for speed. */
2637 if (UCHARAT(s) != nextchr &&
2639 (UCHARAT(s) != ((OP(scan) == REFF
2640 ? PL_fold : PL_fold_locale)[nextchr]))))
2642 ln = PL_regendp[n] - ln;
2643 if (locinput + ln > PL_regeol)
2645 if (ln > 1 && (OP(scan) == REF
2646 ? memNE(s, locinput, ln)
2648 ? ibcmp(s, locinput, ln)
2649 : ibcmp_locale(s, locinput, ln))))
2652 nextchr = UCHARAT(locinput);
2663 OP_4tree *oop = PL_op;
2664 COP *ocurcop = PL_curcop;
2665 SV **ocurpad = PL_curpad;
2669 PL_op = (OP_4tree*)PL_regdata->data[n];
2670 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2671 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2672 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2676 CALLRUNOPS(aTHX); /* Scalar context. */
2679 ret = Nullsv; /* protect against empty (?{}) blocks. */
2687 PL_curpad = ocurpad;
2688 PL_curcop = ocurcop;
2690 if (logical == 2) { /* Postponed subexpression. */
2692 MAGIC *mg = Null(MAGIC*);
2694 CHECKPOINT cp, lastcp;
2696 if(SvROK(ret) || SvRMAGICAL(ret)) {
2697 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2700 mg = mg_find(sv, PERL_MAGIC_qr);
2703 re = (regexp *)mg->mg_obj;
2704 (void)ReREFCNT_inc(re);
2708 char *t = SvPV(ret, len);
2710 char *oprecomp = PL_regprecomp;
2711 I32 osize = PL_regsize;
2712 I32 onpar = PL_regnpar;
2715 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2717 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2718 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2720 PL_regprecomp = oprecomp;
2725 PerlIO_printf(Perl_debug_log,
2726 "Entering embedded `%s%.60s%s%s'\n",
2730 (strlen(re->precomp) > 60 ? "..." : ""))
2733 state.prev = PL_reg_call_cc;
2734 state.cc = PL_regcc;
2735 state.re = PL_reg_re;
2739 cp = regcppush(0); /* Save *all* the positions. */
2742 state.ss = PL_savestack_ix;
2743 *PL_reglastparen = 0;
2744 *PL_reglastcloseparen = 0;
2745 PL_reg_call_cc = &state;
2746 PL_reginput = locinput;
2748 /* XXXX This is too dramatic a measure... */
2751 if (regmatch(re->program + 1)) {
2752 /* Even though we succeeded, we need to restore
2753 global variables, since we may be wrapped inside
2754 SUSPEND, thus the match may be not finished yet. */
2756 /* XXXX Do this only if SUSPENDed? */
2757 PL_reg_call_cc = state.prev;
2758 PL_regcc = state.cc;
2759 PL_reg_re = state.re;
2760 cache_re(PL_reg_re);
2762 /* XXXX This is too dramatic a measure... */
2765 /* These are needed even if not SUSPEND. */
2771 REGCP_UNWIND(lastcp);
2773 PL_reg_call_cc = state.prev;
2774 PL_regcc = state.cc;
2775 PL_reg_re = state.re;
2776 cache_re(PL_reg_re);
2778 /* XXXX This is too dramatic a measure... */
2788 sv_setsv(save_scalar(PL_replgv), ret);
2792 n = ARG(scan); /* which paren pair */
2793 PL_reg_start_tmp[n] = locinput;
2798 n = ARG(scan); /* which paren pair */
2799 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2800 PL_regendp[n] = locinput - PL_bostr;
2801 if (n > *PL_reglastparen)
2802 *PL_reglastparen = n;
2803 *PL_reglastcloseparen = n;
2806 n = ARG(scan); /* which paren pair */
2807 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2810 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2812 next = NEXTOPER(NEXTOPER(scan));
2814 next = scan + ARG(scan);
2815 if (OP(next) == IFTHEN) /* Fake one. */
2816 next = NEXTOPER(NEXTOPER(next));
2820 logical = scan->flags;
2822 /*******************************************************************
2823 PL_regcc contains infoblock about the innermost (...)* loop, and
2824 a pointer to the next outer infoblock.
2826 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2828 1) After matching X, regnode for CURLYX is processed;
2830 2) This regnode creates infoblock on the stack, and calls
2831 regmatch() recursively with the starting point at WHILEM node;
2833 3) Each hit of WHILEM node tries to match A and Z (in the order
2834 depending on the current iteration, min/max of {min,max} and
2835 greediness). The information about where are nodes for "A"
2836 and "Z" is read from the infoblock, as is info on how many times "A"
2837 was already matched, and greediness.
2839 4) After A matches, the same WHILEM node is hit again.
2841 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2842 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2843 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2844 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2845 of the external loop.
2847 Currently present infoblocks form a tree with a stem formed by PL_curcc
2848 and whatever it mentions via ->next, and additional attached trees
2849 corresponding to temporarily unset infoblocks as in "5" above.
2851 In the following picture infoblocks for outer loop of
2852 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2853 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2854 infoblocks are drawn below the "reset" infoblock.
2856 In fact in the picture below we do not show failed matches for Z and T
2857 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2858 more obvious *why* one needs to *temporary* unset infoblocks.]
2860 Matched REx position InfoBlocks Comment
2864 Y A)*?Z)*?T x <- O <- I
2865 YA )*?Z)*?T x <- O <- I
2866 YA A)*?Z)*?T x <- O <- I
2867 YAA )*?Z)*?T x <- O <- I
2868 YAA Z)*?T x <- O # Temporary unset I
2871 YAAZ Y(A)*?Z)*?T x <- O
2874 YAAZY (A)*?Z)*?T x <- O
2877 YAAZY A)*?Z)*?T x <- O <- I
2880 YAAZYA )*?Z)*?T x <- O <- I
2883 YAAZYA Z)*?T x <- O # Temporary unset I
2889 YAAZYAZ T x # Temporary unset O
2896 *******************************************************************/
2899 CHECKPOINT cp = PL_savestack_ix;
2900 /* No need to save/restore up to this paren */
2901 I32 parenfloor = scan->flags;
2903 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2905 cc.oldcc = PL_regcc;
2907 /* XXXX Probably it is better to teach regpush to support
2908 parenfloor > PL_regsize... */
2909 if (parenfloor > *PL_reglastparen)
2910 parenfloor = *PL_reglastparen; /* Pessimization... */
2911 cc.parenfloor = parenfloor;
2913 cc.min = ARG1(scan);
2914 cc.max = ARG2(scan);
2915 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2919 PL_reginput = locinput;
2920 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2922 PL_regcc = cc.oldcc;
2928 * This is really hard to understand, because after we match
2929 * what we're trying to match, we must make sure the rest of
2930 * the REx is going to match for sure, and to do that we have
2931 * to go back UP the parse tree by recursing ever deeper. And
2932 * if it fails, we have to reset our parent's current state
2933 * that we can try again after backing off.
2936 CHECKPOINT cp, lastcp;
2937 CURCUR* cc = PL_regcc;
2938 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2940 n = cc->cur + 1; /* how many we know we matched */
2941 PL_reginput = locinput;
2944 PerlIO_printf(Perl_debug_log,
2945 "%*s %ld out of %ld..%ld cc=%lx\n",
2946 REPORT_CODE_OFF+PL_regindent*2, "",
2947 (long)n, (long)cc->min,
2948 (long)cc->max, (long)cc)
2951 /* If degenerate scan matches "", assume scan done. */
2953 if (locinput == cc->lastloc && n >= cc->min) {
2954 PL_regcc = cc->oldcc;
2958 PerlIO_printf(Perl_debug_log,
2959 "%*s empty match detected, try continuation...\n",
2960 REPORT_CODE_OFF+PL_regindent*2, "")
2962 if (regmatch(cc->next))
2970 /* First just match a string of min scans. */
2974 cc->lastloc = locinput;
2975 if (regmatch(cc->scan))
2978 cc->lastloc = lastloc;
2983 /* Check whether we already were at this position.
2984 Postpone detection until we know the match is not
2985 *that* much linear. */
2986 if (!PL_reg_maxiter) {
2987 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2988 PL_reg_leftiter = PL_reg_maxiter;
2990 if (PL_reg_leftiter-- == 0) {
2991 I32 size = (PL_reg_maxiter + 7)/8;
2992 if (PL_reg_poscache) {
2993 if (PL_reg_poscache_size < size) {
2994 Renew(PL_reg_poscache, size, char);
2995 PL_reg_poscache_size = size;
2997 Zero(PL_reg_poscache, size, char);
3000 PL_reg_poscache_size = size;
3001 Newz(29, PL_reg_poscache, size, char);
3004 PerlIO_printf(Perl_debug_log,
3005 "%sDetected a super-linear match, switching on caching%s...\n",
3006 PL_colors[4], PL_colors[5])
3009 if (PL_reg_leftiter < 0) {
3010 I32 o = locinput - PL_bostr, b;
3012 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3015 if (PL_reg_poscache[o] & (1<<b)) {
3017 PerlIO_printf(Perl_debug_log,
3018 "%*s already tried at this position...\n",
3019 REPORT_CODE_OFF+PL_regindent*2, "")
3023 PL_reg_poscache[o] |= (1<<b);
3027 /* Prefer next over scan for minimal matching. */
3030 PL_regcc = cc->oldcc;
3033 cp = regcppush(cc->parenfloor);
3035 if (regmatch(cc->next)) {
3037 sayYES; /* All done. */
3039 REGCP_UNWIND(lastcp);
3045 if (n >= cc->max) { /* Maximum greed exceeded? */
3046 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3047 && !(PL_reg_flags & RF_warned)) {
3048 PL_reg_flags |= RF_warned;
3049 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3050 "Complex regular subexpression recursion",
3057 PerlIO_printf(Perl_debug_log,
3058 "%*s trying longer...\n",
3059 REPORT_CODE_OFF+PL_regindent*2, "")
3061 /* Try scanning more and see if it helps. */
3062 PL_reginput = locinput;
3064 cc->lastloc = locinput;
3065 cp = regcppush(cc->parenfloor);
3067 if (regmatch(cc->scan)) {
3071 REGCP_UNWIND(lastcp);
3074 cc->lastloc = lastloc;
3078 /* Prefer scan over next for maximal matching. */
3080 if (n < cc->max) { /* More greed allowed? */
3081 cp = regcppush(cc->parenfloor);
3083 cc->lastloc = locinput;
3085 if (regmatch(cc->scan)) {
3089 REGCP_UNWIND(lastcp);
3090 regcppop(); /* Restore some previous $<digit>s? */
3091 PL_reginput = locinput;
3093 PerlIO_printf(Perl_debug_log,
3094 "%*s failed, try continuation...\n",
3095 REPORT_CODE_OFF+PL_regindent*2, "")
3098 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3099 && !(PL_reg_flags & RF_warned)) {
3100 PL_reg_flags |= RF_warned;
3101 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3102 "Complex regular subexpression recursion",
3106 /* Failed deeper matches of scan, so see if this one works. */
3107 PL_regcc = cc->oldcc;
3110 if (regmatch(cc->next))
3116 cc->lastloc = lastloc;
3121 next = scan + ARG(scan);
3124 inner = NEXTOPER(NEXTOPER(scan));
3127 inner = NEXTOPER(scan);
3131 if (OP(next) != c1) /* No choice. */
3132 next = inner; /* Avoid recursion. */
3134 I32 lastparen = *PL_reglastparen;
3136 re_unwind_branch_t *uw;
3138 /* Put unwinding data on stack */
3139 unwind1 = SSNEWt(1,re_unwind_branch_t);
3140 uw = SSPTRt(unwind1,re_unwind_branch_t);
3143 uw->type = ((c1 == BRANCH)
3145 : RE_UNWIND_BRANCHJ);
3146 uw->lastparen = lastparen;
3148 uw->locinput = locinput;
3149 uw->nextchr = nextchr;
3151 uw->regindent = ++PL_regindent;
3154 REGCP_SET(uw->lastcp);
3156 /* Now go into the first branch */
3169 /* We suppose that the next guy does not need
3170 backtracking: in particular, it is of constant length,
3171 and has no parenths to influence future backrefs. */
3172 ln = ARG1(scan); /* min to match */
3173 n = ARG2(scan); /* max to match */
3174 paren = scan->flags;
3176 if (paren > PL_regsize)
3178 if (paren > *PL_reglastparen)
3179 *PL_reglastparen = paren;
3181 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3183 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3184 PL_reginput = locinput;
3187 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3189 /* if we matched something zero-length we don't need to
3190 backtrack - capturing parens are already defined, so
3191 the caveat in the maximal case doesn't apply
3193 XXXX if ln == 0, we can redo this check first time
3194 through the following loop
3197 n = ln; /* don't backtrack */
3198 locinput = PL_reginput;
3199 if (HAS_TEXT(next) || JUMPABLE(next)) {
3200 regnode *text_node = next;
3202 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3204 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3206 if (PL_regkind[(U8)OP(text_node)] == REF) {
3208 n = ARG(text_node); /* which paren pair */
3209 ln = PL_regstartp[n];
3210 /* assume yes if we haven't seen CLOSEn */
3212 *PL_reglastparen < n ||
3219 c1 = *(PL_bostr + ln);
3221 else { c1 = (U8)*STRING(text_node); }
3222 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3224 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3225 c2 = PL_fold_locale[c1];
3234 /* This may be improved if l == 0. */
3235 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3236 /* If it could work, try it. */
3238 UCHARAT(PL_reginput) == c1 ||
3239 UCHARAT(PL_reginput) == c2)
3243 PL_regstartp[paren] =
3244 HOPc(PL_reginput, -l) - PL_bostr;
3245 PL_regendp[paren] = PL_reginput - PL_bostr;
3248 PL_regendp[paren] = -1;
3252 REGCP_UNWIND(lastcp);
3254 /* Couldn't or didn't -- move forward. */
3255 PL_reginput = locinput;
3256 if (regrepeat_hard(scan, 1, &l)) {
3258 locinput = PL_reginput;
3265 n = regrepeat_hard(scan, n, &l);
3266 /* if we matched something zero-length we don't need to
3267 backtrack, unless the minimum count is zero and we
3268 are capturing the result - in that case the capture
3269 being defined or not may affect later execution
3271 if (n != 0 && l == 0 && !(paren && ln == 0))
3272 ln = n; /* don't backtrack */
3273 locinput = PL_reginput;
3275 PerlIO_printf(Perl_debug_log,
3276 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3277 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3281 if (HAS_TEXT(next) || JUMPABLE(next)) {
3282 regnode *text_node = next;
3284 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3286 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3288 if (PL_regkind[(U8)OP(text_node)] == REF) {
3290 n = ARG(text_node); /* which paren pair */
3291 ln = PL_regstartp[n];
3292 /* assume yes if we haven't seen CLOSEn */
3294 *PL_reglastparen < n ||
3301 c1 = *(PL_bostr + ln);
3303 else { c1 = (U8)*STRING(text_node); }
3305 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3307 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3308 c2 = PL_fold_locale[c1];
3319 /* If it could work, try it. */
3321 UCHARAT(PL_reginput) == c1 ||
3322 UCHARAT(PL_reginput) == c2)
3325 PerlIO_printf(Perl_debug_log,
3326 "%*s trying tail with n=%"IVdf"...\n",
3327 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3331 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3332 PL_regendp[paren] = PL_reginput - PL_bostr;
3335 PL_regendp[paren] = -1;
3339 REGCP_UNWIND(lastcp);
3341 /* Couldn't or didn't -- back up. */
3343 locinput = HOPc(locinput, -l);
3344 PL_reginput = locinput;
3351 paren = scan->flags; /* Which paren to set */
3352 if (paren > PL_regsize)
3354 if (paren > *PL_reglastparen)
3355 *PL_reglastparen = paren;
3356 ln = ARG1(scan); /* min to match */
3357 n = ARG2(scan); /* max to match */
3358 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3362 ln = ARG1(scan); /* min to match */
3363 n = ARG2(scan); /* max to match */
3364 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3369 scan = NEXTOPER(scan);
3375 scan = NEXTOPER(scan);
3379 * Lookahead to avoid useless match attempts
3380 * when we know what character comes next.
3384 * Used to only do .*x and .*?x, but now it allows
3385 * for )'s, ('s and (?{ ... })'s to be in the way
3386 * of the quantifier and the EXACT-like node. -- japhy
3389 if (HAS_TEXT(next) || JUMPABLE(next)) {
3391 regnode *text_node = next;
3393 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3395 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3397 if (PL_regkind[(U8)OP(text_node)] == REF) {
3399 n = ARG(text_node); /* which paren pair */
3400 ln = PL_regstartp[n];
3401 /* assume yes if we haven't seen CLOSEn */
3403 *PL_reglastparen < n ||
3408 goto assume_ok_easy;
3410 s = (U8*)PL_bostr + ln;
3412 else { s = (U8*)STRING(text_node); }
3416 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3418 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3419 c2 = PL_fold_locale[c1];
3422 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3423 STRLEN ulen1, ulen2;
3424 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3425 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3427 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3428 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3430 c1 = utf8_to_uvuni(tmpbuf1, 0);
3431 c2 = utf8_to_uvuni(tmpbuf2, 0);
3434 c2 = c1 = utf8_to_uvchr(s, NULL);
3442 PL_reginput = locinput;
3446 if (ln && regrepeat(scan, ln) < ln)
3448 locinput = PL_reginput;
3451 char *e; /* Should not check after this */
3452 char *old = locinput;
3454 if (n == REG_INFTY) {
3457 while (UTF8_IS_CONTINUATION(*(U8*)e))
3463 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3467 e = locinput + n - ln;
3473 /* Find place 'next' could work */
3476 while (locinput <= e &&
3477 UCHARAT(locinput) != c1)
3480 while (locinput <= e
3481 && UCHARAT(locinput) != c1
3482 && UCHARAT(locinput) != c2)
3485 count = locinput - old;
3492 utf8_to_uvchr((U8*)locinput, &len) != c1;
3497 for (count = 0; locinput <= e; count++) {
3498 UV c = utf8_to_uvchr((U8*)locinput, &len);
3499 if (c == c1 || c == c2)
3507 /* PL_reginput == old now */
3508 if (locinput != old) {
3509 ln = 1; /* Did some */
3510 if (regrepeat(scan, count) < count)
3513 /* PL_reginput == locinput now */
3514 TRYPAREN(paren, ln, locinput);
3515 PL_reginput = locinput; /* Could be reset... */
3516 REGCP_UNWIND(lastcp);
3517 /* Couldn't or didn't -- move forward. */
3520 locinput += UTF8SKIP(locinput);
3526 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3530 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3532 c = UCHARAT(PL_reginput);
3533 /* If it could work, try it. */
3534 if (c == c1 || c == c2)
3536 TRYPAREN(paren, n, PL_reginput);
3537 REGCP_UNWIND(lastcp);
3540 /* If it could work, try it. */
3541 else if (c1 == -1000)
3543 TRYPAREN(paren, n, PL_reginput);
3544 REGCP_UNWIND(lastcp);
3546 /* Couldn't or didn't -- move forward. */
3547 PL_reginput = locinput;
3548 if (regrepeat(scan, 1)) {
3550 locinput = PL_reginput;
3558 n = regrepeat(scan, n);
3559 locinput = PL_reginput;
3560 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3561 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3562 ln = n; /* why back off? */
3563 /* ...because $ and \Z can match before *and* after
3564 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3565 We should back off by one in this case. */
3566 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3575 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3577 c = UCHARAT(PL_reginput);
3579 /* If it could work, try it. */
3580 if (c1 == -1000 || c == c1 || c == c2)
3582 TRYPAREN(paren, n, PL_reginput);
3583 REGCP_UNWIND(lastcp);
3585 /* Couldn't or didn't -- back up. */
3587 PL_reginput = locinput = HOPc(locinput, -1);
3595 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3597 c = UCHARAT(PL_reginput);
3599 /* If it could work, try it. */
3600 if (c1 == -1000 || c == c1 || c == c2)
3602 TRYPAREN(paren, n, PL_reginput);
3603 REGCP_UNWIND(lastcp);
3605 /* Couldn't or didn't -- back up. */
3607 PL_reginput = locinput = HOPc(locinput, -1);
3614 if (PL_reg_call_cc) {
3615 re_cc_state *cur_call_cc = PL_reg_call_cc;
3616 CURCUR *cctmp = PL_regcc;
3617 regexp *re = PL_reg_re;
3618 CHECKPOINT cp, lastcp;
3620 cp = regcppush(0); /* Save *all* the positions. */
3622 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3624 PL_reginput = locinput; /* Make position available to
3626 cache_re(PL_reg_call_cc->re);
3627 PL_regcc = PL_reg_call_cc->cc;
3628 PL_reg_call_cc = PL_reg_call_cc->prev;
3629 if (regmatch(cur_call_cc->node)) {
3630 PL_reg_call_cc = cur_call_cc;
3634 REGCP_UNWIND(lastcp);
3636 PL_reg_call_cc = cur_call_cc;
3642 PerlIO_printf(Perl_debug_log,
3643 "%*s continuation failed...\n",
3644 REPORT_CODE_OFF+PL_regindent*2, "")
3648 if (locinput < PL_regtill) {
3649 DEBUG_r(PerlIO_printf(Perl_debug_log,
3650 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3652 (long)(locinput - PL_reg_starttry),
3653 (long)(PL_regtill - PL_reg_starttry),
3655 sayNO_FINAL; /* Cannot match: too short. */
3657 PL_reginput = locinput; /* put where regtry can find it */
3658 sayYES_FINAL; /* Success! */
3660 PL_reginput = locinput; /* put where regtry can find it */
3661 sayYES_LOUD; /* Success! */
3664 PL_reginput = locinput;
3669 s = HOPBACKc(locinput, scan->flags);
3675 PL_reginput = locinput;
3680 s = HOPBACKc(locinput, scan->flags);
3686 PL_reginput = locinput;
3689 inner = NEXTOPER(NEXTOPER(scan));
3690 if (regmatch(inner) != n) {
3705 if (OP(scan) == SUSPEND) {
3706 locinput = PL_reginput;
3707 nextchr = UCHARAT(locinput);
3712 next = scan + ARG(scan);
3717 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3718 PTR2UV(scan), OP(scan));
3719 Perl_croak(aTHX_ "regexp memory corruption");
3726 * We get here only if there's trouble -- normally "case END" is
3727 * the terminating point.
3729 Perl_croak(aTHX_ "corrupted regexp pointers");
3735 PerlIO_printf(Perl_debug_log,
3736 "%*s %scould match...%s\n",
3737 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3741 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3742 PL_colors[4],PL_colors[5]));
3748 #if 0 /* Breaks $^R */
3756 PerlIO_printf(Perl_debug_log,
3757 "%*s %sfailed...%s\n",
3758 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3764 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3767 case RE_UNWIND_BRANCH:
3768 case RE_UNWIND_BRANCHJ:
3770 re_unwind_branch_t *uwb = &(uw->branch);
3771 I32 lastparen = uwb->lastparen;
3773 REGCP_UNWIND(uwb->lastcp);
3774 for (n = *PL_reglastparen; n > lastparen; n--)
3776 *PL_reglastparen = n;
3777 scan = next = uwb->next;
3779 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3780 ? BRANCH : BRANCHJ) ) { /* Failure */
3787 /* Have more choice yet. Reuse the same uwb. */
3789 if ((n = (uwb->type == RE_UNWIND_BRANCH
3790 ? NEXT_OFF(next) : ARG(next))))
3793 next = NULL; /* XXXX Needn't unwinding in this case... */
3795 next = NEXTOPER(scan);
3796 if (uwb->type == RE_UNWIND_BRANCHJ)
3797 next = NEXTOPER(next);
3798 locinput = uwb->locinput;
3799 nextchr = uwb->nextchr;
3801 PL_regindent = uwb->regindent;
3808 Perl_croak(aTHX_ "regexp unwind memory corruption");
3819 - regrepeat - repeatedly match something simple, report how many
3822 * [This routine now assumes that it will only match on things of length 1.
3823 * That was true before, but now we assume scan - reginput is the count,
3824 * rather than incrementing count on every character. [Er, except utf8.]]
3827 S_regrepeat(pTHX_ regnode *p, I32 max)
3829 register char *scan;
3831 register char *loceol = PL_regeol;
3832 register I32 hardcount = 0;
3833 register bool do_utf8 = PL_reg_match_utf8;
3836 if (max != REG_INFTY && max < loceol - scan)
3837 loceol = scan + max;
3842 while (scan < loceol && hardcount < max && *scan != '\n') {
3843 scan += UTF8SKIP(scan);
3847 while (scan < loceol && *scan != '\n')
3857 case EXACT: /* length of string is 1 */
3859 while (scan < loceol && UCHARAT(scan) == c)
3862 case EXACTF: /* length of string is 1 */
3864 while (scan < loceol &&
3865 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3868 case EXACTFL: /* length of string is 1 */
3869 PL_reg_flags |= RF_tainted;
3871 while (scan < loceol &&
3872 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3878 while (hardcount < max && scan < loceol &&
3879 reginclass(p, (U8*)scan, do_utf8)) {
3880 scan += UTF8SKIP(scan);
3884 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3891 LOAD_UTF8_CHARCLASS(alnum,"a");
3892 while (hardcount < max && scan < loceol &&
3893 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3894 scan += UTF8SKIP(scan);
3898 while (scan < loceol && isALNUM(*scan))
3903 PL_reg_flags |= RF_tainted;
3906 while (hardcount < max && scan < loceol &&
3907 isALNUM_LC_utf8((U8*)scan)) {
3908 scan += UTF8SKIP(scan);
3912 while (scan < loceol && isALNUM_LC(*scan))
3919 LOAD_UTF8_CHARCLASS(alnum,"a");
3920 while (hardcount < max && scan < loceol &&
3921 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3922 scan += UTF8SKIP(scan);
3926 while (scan < loceol && !isALNUM(*scan))
3931 PL_reg_flags |= RF_tainted;
3934 while (hardcount < max && scan < loceol &&
3935 !isALNUM_LC_utf8((U8*)scan)) {
3936 scan += UTF8SKIP(scan);
3940 while (scan < loceol && !isALNUM_LC(*scan))
3947 LOAD_UTF8_CHARCLASS(space," ");
3948 while (hardcount < max && scan < loceol &&
3950 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3951 scan += UTF8SKIP(scan);
3955 while (scan < loceol && isSPACE(*scan))
3960 PL_reg_flags |= RF_tainted;
3963 while (hardcount < max && scan < loceol &&
3964 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3965 scan += UTF8SKIP(scan);
3969 while (scan < loceol && isSPACE_LC(*scan))
3976 LOAD_UTF8_CHARCLASS(space," ");
3977 while (hardcount < max && scan < loceol &&
3979 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3980 scan += UTF8SKIP(scan);
3984 while (scan < loceol && !isSPACE(*scan))
3989 PL_reg_flags |= RF_tainted;
3992 while (hardcount < max && scan < loceol &&
3993 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3994 scan += UTF8SKIP(scan);
3998 while (scan < loceol && !isSPACE_LC(*scan))
4005 LOAD_UTF8_CHARCLASS(digit,"0");
4006 while (hardcount < max && scan < loceol &&
4007 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4008 scan += UTF8SKIP(scan);
4012 while (scan < loceol && isDIGIT(*scan))
4019 LOAD_UTF8_CHARCLASS(digit,"0");
4020 while (hardcount < max && scan < loceol &&
4021 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4022 scan += UTF8SKIP(scan);
4026 while (scan < loceol && !isDIGIT(*scan))
4030 default: /* Called on something of 0 width. */
4031 break; /* So match right here or not at all. */
4037 c = scan - PL_reginput;
4042 SV *prop = sv_newmortal();
4045 PerlIO_printf(Perl_debug_log,
4046 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4047 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4054 - regrepeat_hard - repeatedly match something, report total lenth and length
4056 * The repeater is supposed to have constant length.
4060 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4062 register char *scan = Nullch;
4063 register char *start;
4064 register char *loceol = PL_regeol;
4066 I32 count = 0, res = 1;
4071 start = PL_reginput;
4072 if (PL_reg_match_utf8) {
4073 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4076 while (start < PL_reginput) {
4078 start += UTF8SKIP(start);
4089 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4091 *lp = l = PL_reginput - start;
4092 if (max != REG_INFTY && l*max < loceol - scan)
4093 loceol = scan + l*max;
4106 - regclass_swash - prepare the utf8 swash
4110 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
4115 if (PL_regdata && PL_regdata->count) {
4118 if (PL_regdata->what[n] == 's') {
4119 SV *rv = (SV*)PL_regdata->data[n];
4120 AV *av = (AV*)SvRV((SV*)rv);
4123 si = *av_fetch(av, 0, FALSE);
4124 a = av_fetch(av, 1, FALSE);
4128 else if (si && doinit) {
4129 sw = swash_init("utf8", "", si, 1, 0);
4130 (void)av_store(av, 1, sw);
4142 - reginclass - determine if a character falls into a character class
4146 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
4148 char flags = ANYOF_FLAGS(n);
4153 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4155 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4156 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4157 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4160 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4163 SV *sw = regclass_swash(n, TRUE, 0);
4166 if (swash_fetch(sw, p, do_utf8))
4168 else if (flags & ANYOF_FOLD) {
4169 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
4172 to_utf8_fold(p, foldbuf, &foldlen);
4173 if (swash_fetch(sw, foldbuf, do_utf8))
4175 to_utf8_upper(p, foldbuf, &foldlen);
4176 if (swash_fetch(sw, foldbuf, do_utf8))
4182 if (!match && c < 256) {
4183 if (ANYOF_BITMAP_TEST(n, c))
4185 else if (flags & ANYOF_FOLD) {
4188 if (flags & ANYOF_LOCALE) {
4189 PL_reg_flags |= RF_tainted;
4190 f = PL_fold_locale[c];
4194 if (f != c && ANYOF_BITMAP_TEST(n, f))
4198 if (!match && (flags & ANYOF_CLASS)) {
4199 PL_reg_flags |= RF_tainted;
4201 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4202 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4203 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4204 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4205 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4206 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4207 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4208 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4209 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4210 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4211 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4212 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4213 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4214 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4215 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4216 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4217 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4218 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4219 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4220 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4221 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4222 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4223 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4224 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4225 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4226 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4227 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4228 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4229 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4230 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4231 ) /* How's that for a conditional? */
4238 return (flags & ANYOF_INVERT) ? !match : match;
4242 S_reghop(pTHX_ U8 *s, I32 off)
4244 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4248 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4251 while (off-- && s < lim) {
4252 /* XXX could check well-formedness here */
4260 if (UTF8_IS_CONTINUED(*s)) {
4261 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4264 /* XXX could check well-formedness here */
4272 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4274 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4278 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4281 while (off-- && s < lim) {
4282 /* XXX could check well-formedness here */
4292 if (UTF8_IS_CONTINUED(*s)) {
4293 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4296 /* XXX could check well-formedness here */
4308 restore_pos(pTHX_ void *arg)
4310 if (PL_reg_eval_set) {
4311 if (PL_reg_oldsaved) {
4312 PL_reg_re->subbeg = PL_reg_oldsaved;
4313 PL_reg_re->sublen = PL_reg_oldsavedlen;
4314 RX_MATCH_COPIED_on(PL_reg_re);
4316 PL_reg_magic->mg_len = PL_reg_oldpos;
4317 PL_reg_eval_set = 0;
4318 PL_curpm = PL_reg_oldcurpm;