5 * "One Ring to rule them all, One Ring to find them..."
8 /* This file contains functions for executing a regular expression. See
9 * also regcomp.c which funnily enough, contains functions for compiling
10 * a regular expression.
12 * This file is also copied at build time to ext/re/re_exec.c, where
13 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
14 * This causes the main functions to be compiled under new names and with
15 * debugging support added, which makes "use re 'debug'" work.
19 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
20 * confused with the original package (see point 3 below). Thanks, Henry!
23 /* Additional note: this code is very heavily munged from Henry's version
24 * in places. In some spots I've traded clarity for efficiency, so don't
25 * blame Henry for some of the lack of readability.
28 /* The names of the functions have been changed from regcomp and
29 * regexec to pregcomp and pregexec in order to avoid conflicts
30 * with the POSIX routines of the same names.
33 #ifdef PERL_EXT_RE_BUILD
34 /* need to replace pregcomp et al, so enable that */
35 # ifndef PERL_IN_XSUB_RE
36 # define PERL_IN_XSUB_RE
38 /* need access to debugger hooks */
39 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
44 #ifdef PERL_IN_XSUB_RE
45 /* We *really* need to overwrite these symbols: */
46 # define Perl_regexec_flags my_regexec
47 # define Perl_regdump my_regdump
48 # define Perl_regprop my_regprop
49 # define Perl_re_intuit_start my_re_intuit_start
50 /* *These* symbols are masked to allow static link. */
51 # define Perl_pregexec my_pregexec
52 # define Perl_reginitcolors my_reginitcolors
53 # define Perl_regclass_swash my_regclass_swash
55 # define PERL_NO_GET_CONTEXT
60 * pregcomp and pregexec -- regsub and regerror are not used in perl
62 * Copyright (c) 1986 by University of Toronto.
63 * Written by Henry Spencer. Not derived from licensed software.
65 * Permission is granted to anyone to use this software for any
66 * purpose on any computer system, and to redistribute it freely,
67 * subject to the following restrictions:
69 * 1. The author is not responsible for the consequences of use of
70 * this software, no matter how awful, even if they arise
73 * 2. The origin of this software must not be misrepresented, either
74 * by explicit claim or by omission.
76 * 3. Altered versions must be plainly marked as such, and must not
77 * be misrepresented as being the original software.
79 **** Alterations to Henry's code are...
81 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
82 **** 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
84 **** You may distribute under the terms of either the GNU General Public
85 **** License or the Artistic License, as specified in the README file.
87 * Beware that some of this code is subtly aware of the way operator
88 * precedence is structured in regular expressions. Serious changes in
89 * regular-expression syntax might require a total rethink.
92 #define PERL_IN_REGEXEC_C
97 #define RF_tainted 1 /* tainted information used? */
98 #define RF_warned 2 /* warned about big count? */
99 #define RF_evaled 4 /* Did an EVAL with setting? */
100 #define RF_utf8 8 /* String contains multibyte chars? */
101 #define RF_false 16 /* odd number of nested negatives */
103 #define UTF ((PL_reg_flags & RF_utf8) != 0)
105 #define RS_init 1 /* eval environment created */
106 #define RS_set 2 /* replsv value is set */
109 #define STATIC static
112 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
118 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
119 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
121 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
122 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
123 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
124 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
125 #define HOPc(pos,off) ((char*)HOP(pos,off))
126 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
128 #define HOPBACK(pos, off) ( \
129 (PL_reg_match_utf8) \
130 ? reghopmaybe((U8*)pos, -off) \
131 : (pos - off >= PL_bostr) \
135 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
137 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
138 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
139 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
140 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
141 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
142 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
144 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) { ENTER; save_re_context(); (void)CAT2(is_utf8_, a)((U8*)b); LEAVE; } } STMT_END
146 /* for use after a quantifier and before an EXACT-like node -- japhy */
147 #define JUMPABLE(rn) ( \
148 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
149 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
150 OP(rn) == PLUS || OP(rn) == MINMOD || \
151 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
154 #define HAS_TEXT(rn) ( \
155 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
159 Search for mandatory following text node; for lookahead, the text must
160 follow but for lookbehind (rn->flags != 0) we skip to the next step.
162 #define FIND_NEXT_IMPT(rn) STMT_START { \
163 while (JUMPABLE(rn)) \
164 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
165 rn = NEXTOPER(NEXTOPER(rn)); \
166 else if (OP(rn) == PLUS) \
168 else if (OP(rn) == IFMATCH) \
169 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
170 else rn += NEXT_OFF(rn); \
173 static void restore_pos(pTHX_ void *arg);
176 S_regcppush(pTHX_ I32 parenfloor)
178 int retval = PL_savestack_ix;
179 #define REGCP_PAREN_ELEMS 4
180 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
183 if (paren_elems_to_push < 0)
184 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
186 #define REGCP_OTHER_ELEMS 6
187 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
188 for (p = PL_regsize; p > parenfloor; p--) {
189 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
190 SSPUSHINT(PL_regendp[p]);
191 SSPUSHINT(PL_regstartp[p]);
192 SSPUSHPTR(PL_reg_start_tmp[p]);
195 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
196 SSPUSHINT(PL_regsize);
197 SSPUSHINT(*PL_reglastparen);
198 SSPUSHINT(*PL_reglastcloseparen);
199 SSPUSHPTR(PL_reginput);
200 #define REGCP_FRAME_ELEMS 2
201 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
202 * are needed for the regexp context stack bookkeeping. */
203 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
204 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
209 /* These are needed since we do not localize EVAL nodes: */
210 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
211 " Setting an EVAL scope, savestack=%"IVdf"\n", \
212 (IV)PL_savestack_ix)); cp = PL_savestack_ix
214 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
215 PerlIO_printf(Perl_debug_log, \
216 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
217 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
227 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
229 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
230 i = SSPOPINT; /* Parentheses elements to pop. */
231 input = (char *) SSPOPPTR;
232 *PL_reglastcloseparen = SSPOPINT;
233 *PL_reglastparen = SSPOPINT;
234 PL_regsize = SSPOPINT;
236 /* Now restore the parentheses context. */
237 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
238 i > 0; i -= REGCP_PAREN_ELEMS) {
239 paren = (U32)SSPOPINT;
240 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
241 PL_regstartp[paren] = SSPOPINT;
243 if (paren <= *PL_reglastparen)
244 PL_regendp[paren] = tmps;
246 PerlIO_printf(Perl_debug_log,
247 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
248 (UV)paren, (IV)PL_regstartp[paren],
249 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
250 (IV)PL_regendp[paren],
251 (paren > *PL_reglastparen ? "(no)" : ""));
255 if ((I32)(*PL_reglastparen + 1) <= PL_regnpar) {
256 PerlIO_printf(Perl_debug_log,
257 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
258 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
262 /* It would seem that the similar code in regtry()
263 * already takes care of this, and in fact it is in
264 * a better location to since this code can #if 0-ed out
265 * but the code in regtry() is needed or otherwise tests
266 * requiring null fields (pat.t#187 and split.t#{13,14}
267 * (as of patchlevel 7877) will fail. Then again,
268 * this code seems to be necessary or otherwise
269 * building DynaLoader will fail:
270 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
272 for (paren = *PL_reglastparen + 1; (I32)paren <= PL_regnpar; paren++) {
273 if ((I32)paren > PL_regsize)
274 PL_regstartp[paren] = -1;
275 PL_regendp[paren] = -1;
282 S_regcp_set_to(pTHX_ I32 ss)
284 I32 tmp = PL_savestack_ix;
286 PL_savestack_ix = ss;
288 PL_savestack_ix = tmp;
292 typedef struct re_cc_state
296 struct re_cc_state *prev;
301 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
303 #define TRYPAREN(paren, n, input) { \
306 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
307 PL_regendp[paren] = input - PL_bostr; \
310 PL_regendp[paren] = -1; \
312 if (regmatch(next)) \
315 PL_regendp[paren] = -1; \
320 * pregexec and friends
324 - pregexec - match a regexp against a string
327 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
328 char *strbeg, I32 minend, SV *screamer, U32 nosave)
329 /* strend: pointer to null at end of string */
330 /* strbeg: real beginning of string */
331 /* minend: end of match must be >=minend after stringarg. */
332 /* nosave: For optimizations. */
335 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
336 nosave ? 0 : REXEC_COPY_STR);
340 S_cache_re(pTHX_ regexp *prog)
342 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
344 PL_regprogram = prog->program;
346 PL_regnpar = prog->nparens;
347 PL_regdata = prog->data;
352 * Need to implement the following flags for reg_anch:
354 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
356 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
357 * INTUIT_AUTORITATIVE_ML
358 * INTUIT_ONCE_NOML - Intuit can match in one location only.
361 * Another flag for this function: SECOND_TIME (so that float substrs
362 * with giant delta may be not rechecked).
365 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
367 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
368 Otherwise, only SvCUR(sv) is used to get strbeg. */
370 /* XXXX We assume that strpos is strbeg unless sv. */
372 /* XXXX Some places assume that there is a fixed substring.
373 An update may be needed if optimizer marks as "INTUITable"
374 RExen without fixed substrings. Similarly, it is assumed that
375 lengths of all the strings are no more than minlen, thus they
376 cannot come from lookahead.
377 (Or minlen should take into account lookahead.) */
379 /* A failure to find a constant substring means that there is no need to make
380 an expensive call to REx engine, thus we celebrate a failure. Similarly,
381 finding a substring too deep into the string means that less calls to
382 regtry() should be needed.
384 REx compiler's optimizer found 4 possible hints:
385 a) Anchored substring;
387 c) Whether we are anchored (beginning-of-line or \G);
388 d) First node (of those at offset 0) which may distingush positions;
389 We use a)b)d) and multiline-part of c), and try to find a position in the
390 string which does not contradict any of them.
393 /* Most of decisions we do here should have been done at compile time.
394 The nodes of the REx which we used for the search should have been
395 deleted from the finite automaton. */
398 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
399 char *strend, U32 flags, re_scream_pos_data *data)
401 register I32 start_shift = 0;
402 /* Should be nonnegative! */
403 register I32 end_shift = 0;
408 int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
410 register char *other_last = Nullch; /* other substr checked before this */
411 char *check_at = Nullch; /* check substr found at this pos */
412 I32 multiline = prog->reganch & PMf_MULTILINE;
414 char *i_strpos = strpos;
415 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
417 RX_MATCH_UTF8_set(prog,do_utf8);
419 if (prog->reganch & ROPT_UTF8) {
420 DEBUG_r(PerlIO_printf(Perl_debug_log,
421 "UTF-8 regex...\n"));
422 PL_reg_flags |= RF_utf8;
426 char *s = PL_reg_match_utf8 ?
427 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
429 int len = PL_reg_match_utf8 ?
430 strlen(s) : strend - strpos;
433 if (PL_reg_match_utf8)
434 DEBUG_r(PerlIO_printf(Perl_debug_log,
435 "UTF-8 target...\n"));
436 PerlIO_printf(Perl_debug_log,
437 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
438 PL_colors[4],PL_colors[5],PL_colors[0],
441 (strlen(prog->precomp) > 60 ? "..." : ""),
443 (int)(len > 60 ? 60 : len),
445 (len > 60 ? "..." : "")
449 /* CHR_DIST() would be more correct here but it makes things slow. */
450 if (prog->minlen > strend - strpos) {
451 DEBUG_r(PerlIO_printf(Perl_debug_log,
452 "String too short... [re_intuit_start]\n"));
455 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
458 if (!prog->check_utf8 && prog->check_substr)
459 to_utf8_substr(prog);
460 check = prog->check_utf8;
462 if (!prog->check_substr && prog->check_utf8)
463 to_byte_substr(prog);
464 check = prog->check_substr;
466 if (check == &PL_sv_undef) {
467 DEBUG_r(PerlIO_printf(Perl_debug_log,
468 "Non-utf string cannot match utf check string\n"));
471 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
472 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
473 || ( (prog->reganch & ROPT_ANCH_BOL)
474 && !multiline ) ); /* Check after \n? */
477 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
478 | ROPT_IMPLICIT)) /* not a real BOL */
479 /* SvCUR is not set on references: SvRV and SvPVX overlap */
481 && (strpos != strbeg)) {
482 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
485 if (prog->check_offset_min == prog->check_offset_max &&
486 !(prog->reganch & ROPT_CANY_SEEN)) {
487 /* Substring at constant offset from beg-of-str... */
490 s = HOP3c(strpos, prog->check_offset_min, strend);
492 slen = SvCUR(check); /* >= 1 */
494 if ( strend - s > slen || strend - s < slen - 1
495 || (strend - s == slen && strend[-1] != '\n')) {
496 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
499 /* Now should match s[0..slen-2] */
501 if (slen && (*SvPVX(check) != *s
503 && memNE(SvPVX(check), s, slen)))) {
505 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
509 else if (*SvPVX(check) != *s
510 || ((slen = SvCUR(check)) > 1
511 && memNE(SvPVX(check), s, slen)))
513 goto success_at_start;
516 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
518 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
519 end_shift = prog->minlen - start_shift -
520 CHR_SVLEN(check) + (SvTAIL(check) != 0);
522 I32 end = prog->check_offset_max + CHR_SVLEN(check)
523 - (SvTAIL(check) != 0);
524 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
526 if (end_shift < eshift)
530 else { /* Can match at random position */
533 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
534 /* Should be nonnegative! */
535 end_shift = prog->minlen - start_shift -
536 CHR_SVLEN(check) + (SvTAIL(check) != 0);
539 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
541 Perl_croak(aTHX_ "panic: end_shift");
545 /* Find a possible match in the region s..strend by looking for
546 the "check" substring in the region corrected by start/end_shift. */
547 if (flags & REXEC_SCREAM) {
548 I32 p = -1; /* Internal iterator of scream. */
549 I32 *pp = data ? data->scream_pos : &p;
551 if (PL_screamfirst[BmRARE(check)] >= 0
552 || ( BmRARE(check) == '\n'
553 && (BmPREVIOUS(check) == SvCUR(check) - 1)
555 s = screaminstr(sv, check,
556 start_shift + (s - strbeg), end_shift, pp, 0);
559 /* we may be pointing at the wrong string */
560 if (s && RX_MATCH_COPIED(prog))
561 s = strbeg + (s - SvPVX(sv));
563 *data->scream_olds = s;
565 else if (prog->reganch & ROPT_CANY_SEEN)
566 s = fbm_instr((U8*)(s + start_shift),
567 (U8*)(strend - end_shift),
568 check, multiline ? FBMrf_MULTILINE : 0);
570 s = fbm_instr(HOP3(s, start_shift, strend),
571 HOP3(strend, -end_shift, strbeg),
572 check, multiline ? FBMrf_MULTILINE : 0);
574 /* Update the count-of-usability, remove useless subpatterns,
577 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
578 (s ? "Found" : "Did not find"),
579 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
581 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
583 PL_colors[1], (SvTAIL(check) ? "$" : ""),
584 (s ? " at offset " : "...\n") ) );
591 /* Finish the diagnostic message */
592 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
594 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
595 Start with the other substr.
596 XXXX no SCREAM optimization yet - and a very coarse implementation
597 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
598 *always* match. Probably should be marked during compile...
599 Probably it is right to do no SCREAM here...
602 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
603 /* Take into account the "other" substring. */
604 /* XXXX May be hopelessly wrong for UTF... */
607 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
610 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
614 t = s - prog->check_offset_max;
615 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
617 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
622 t = HOP3c(t, prog->anchored_offset, strend);
623 if (t < other_last) /* These positions already checked */
625 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
628 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
629 /* On end-of-str: see comment below. */
630 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
631 if (must == &PL_sv_undef) {
633 DEBUG_r(must = prog->anchored_utf8); /* for debug */
638 HOP3(HOP3(last1, prog->anchored_offset, strend)
639 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
641 multiline ? FBMrf_MULTILINE : 0
643 DEBUG_r(PerlIO_printf(Perl_debug_log,
644 "%s anchored substr `%s%.*s%s'%s",
645 (s ? "Found" : "Contradicts"),
648 - (SvTAIL(must)!=0)),
650 PL_colors[1], (SvTAIL(must) ? "$" : "")));
652 if (last1 >= last2) {
653 DEBUG_r(PerlIO_printf(Perl_debug_log,
654 ", giving up...\n"));
657 DEBUG_r(PerlIO_printf(Perl_debug_log,
658 ", trying floating at offset %ld...\n",
659 (long)(HOP3c(s1, 1, strend) - i_strpos)));
660 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
661 s = HOP3c(last, 1, strend);
665 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
666 (long)(s - i_strpos)));
667 t = HOP3c(s, -prog->anchored_offset, strbeg);
668 other_last = HOP3c(s, 1, strend);
676 else { /* Take into account the floating substring. */
681 t = HOP3c(s, -start_shift, strbeg);
683 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
684 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
685 last = HOP3c(t, prog->float_max_offset, strend);
686 s = HOP3c(t, prog->float_min_offset, strend);
689 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
690 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
691 /* fbm_instr() takes into account exact value of end-of-str
692 if the check is SvTAIL(ed). Since false positives are OK,
693 and end-of-str is not later than strend we are OK. */
694 if (must == &PL_sv_undef) {
696 DEBUG_r(must = prog->float_utf8); /* for debug message */
699 s = fbm_instr((unsigned char*)s,
700 (unsigned char*)last + SvCUR(must)
702 must, multiline ? FBMrf_MULTILINE : 0);
703 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
704 (s ? "Found" : "Contradicts"),
706 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
708 PL_colors[1], (SvTAIL(must) ? "$" : "")));
711 DEBUG_r(PerlIO_printf(Perl_debug_log,
712 ", giving up...\n"));
715 DEBUG_r(PerlIO_printf(Perl_debug_log,
716 ", trying anchored starting at offset %ld...\n",
717 (long)(s1 + 1 - i_strpos)));
719 s = HOP3c(t, 1, strend);
723 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
724 (long)(s - i_strpos)));
725 other_last = s; /* Fix this later. --Hugo */
734 t = s - prog->check_offset_max;
735 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
737 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
739 /* Fixed substring is found far enough so that the match
740 cannot start at strpos. */
742 if (ml_anch && t[-1] != '\n') {
743 /* Eventually fbm_*() should handle this, but often
744 anchored_offset is not 0, so this check will not be wasted. */
745 /* XXXX In the code below we prefer to look for "^" even in
746 presence of anchored substrings. And we search even
747 beyond the found float position. These pessimizations
748 are historical artefacts only. */
750 while (t < strend - prog->minlen) {
752 if (t < check_at - prog->check_offset_min) {
753 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
754 /* Since we moved from the found position,
755 we definitely contradict the found anchored
756 substr. Due to the above check we do not
757 contradict "check" substr.
758 Thus we can arrive here only if check substr
759 is float. Redo checking for "other"=="fixed".
762 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
763 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
764 goto do_other_anchored;
766 /* We don't contradict the found floating substring. */
767 /* XXXX Why not check for STCLASS? */
769 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
770 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
773 /* Position contradicts check-string */
774 /* XXXX probably better to look for check-string
775 than for "\n", so one should lower the limit for t? */
776 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
777 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
778 other_last = strpos = s = t + 1;
783 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
784 PL_colors[0],PL_colors[1]));
788 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
789 PL_colors[0],PL_colors[1]));
793 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
796 /* The found string does not prohibit matching at strpos,
797 - no optimization of calling REx engine can be performed,
798 unless it was an MBOL and we are not after MBOL,
799 or a future STCLASS check will fail this. */
801 /* Even in this situation we may use MBOL flag if strpos is offset
802 wrt the start of the string. */
803 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
804 && (strpos != strbeg) && strpos[-1] != '\n'
805 /* May be due to an implicit anchor of m{.*foo} */
806 && !(prog->reganch & ROPT_IMPLICIT))
811 DEBUG_r( if (ml_anch)
812 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
813 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
816 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
818 prog->check_utf8 /* Could be deleted already */
819 && --BmUSEFUL(prog->check_utf8) < 0
820 && (prog->check_utf8 == prog->float_utf8)
822 prog->check_substr /* Could be deleted already */
823 && --BmUSEFUL(prog->check_substr) < 0
824 && (prog->check_substr == prog->float_substr)
827 /* If flags & SOMETHING - do not do it many times on the same match */
828 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
829 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
830 if (do_utf8 ? prog->check_substr : prog->check_utf8)
831 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
832 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
833 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
834 check = Nullsv; /* abort */
836 /* XXXX This is a remnant of the old implementation. It
837 looks wasteful, since now INTUIT can use many
839 prog->reganch &= ~RE_USE_INTUIT;
846 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
847 if (prog->regstclass) {
848 /* minlen == 0 is possible if regstclass is \b or \B,
849 and the fixed substr is ''$.
850 Since minlen is already taken into account, s+1 is before strend;
851 accidentally, minlen >= 1 guaranties no false positives at s + 1
852 even for \b or \B. But (minlen? 1 : 0) below assumes that
853 regstclass does not come from lookahead... */
854 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
855 This leaves EXACTF only, which is dealt with in find_byclass(). */
856 U8* str = (U8*)STRING(prog->regstclass);
857 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
858 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
860 char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
861 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
862 : (prog->float_substr || prog->float_utf8
863 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
866 char *startpos = strbeg;
870 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
875 if (endpos == strend) {
876 DEBUG_r( PerlIO_printf(Perl_debug_log,
877 "Could not match STCLASS...\n") );
880 DEBUG_r( PerlIO_printf(Perl_debug_log,
881 "This position contradicts STCLASS...\n") );
882 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
884 /* Contradict one of substrings */
885 if (prog->anchored_substr || prog->anchored_utf8) {
886 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
887 DEBUG_r( what = "anchored" );
889 s = HOP3c(t, 1, strend);
890 if (s + start_shift + end_shift > strend) {
891 /* XXXX Should be taken into account earlier? */
892 DEBUG_r( PerlIO_printf(Perl_debug_log,
893 "Could not match STCLASS...\n") );
898 DEBUG_r( PerlIO_printf(Perl_debug_log,
899 "Looking for %s substr starting at offset %ld...\n",
900 what, (long)(s + start_shift - i_strpos)) );
903 /* Have both, check_string is floating */
904 if (t + start_shift >= check_at) /* Contradicts floating=check */
905 goto retry_floating_check;
906 /* Recheck anchored substring, but not floating... */
910 DEBUG_r( PerlIO_printf(Perl_debug_log,
911 "Looking for anchored substr starting at offset %ld...\n",
912 (long)(other_last - i_strpos)) );
913 goto do_other_anchored;
915 /* Another way we could have checked stclass at the
916 current position only: */
921 DEBUG_r( PerlIO_printf(Perl_debug_log,
922 "Looking for /%s^%s/m starting at offset %ld...\n",
923 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
926 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
928 /* Check is floating subtring. */
929 retry_floating_check:
930 t = check_at - start_shift;
931 DEBUG_r( what = "floating" );
932 goto hop_and_restart;
935 DEBUG_r(PerlIO_printf(Perl_debug_log,
936 "By STCLASS: moving %ld --> %ld\n",
937 (long)(t - i_strpos), (long)(s - i_strpos))
941 DEBUG_r(PerlIO_printf(Perl_debug_log,
942 "Does not contradict STCLASS...\n");
947 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
948 PL_colors[4], (check ? "Guessed" : "Giving up"),
949 PL_colors[5], (long)(s - i_strpos)) );
952 fail_finish: /* Substring not found */
953 if (prog->check_substr || prog->check_utf8) /* could be removed already */
954 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
956 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
957 PL_colors[4],PL_colors[5]));
961 /* We know what class REx starts with. Try to find this position... */
963 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
965 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
969 register STRLEN uskip;
973 register I32 tmp = 1; /* Scratch variable? */
974 register bool do_utf8 = PL_reg_match_utf8;
976 /* We know what class it must start with. */
980 while (s + (uskip = UTF8SKIP(s)) <= strend) {
981 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
982 !UTF8_IS_INVARIANT((U8)s[0]) ?
983 reginclass(c, (U8*)s, 0, do_utf8) :
984 REGINCLASS(c, (U8*)s)) {
985 if (tmp && (norun || regtry(prog, s)))
999 if (REGINCLASS(c, (U8*)s) ||
1000 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1001 /* The assignment of 2 is intentional:
1002 * for the folded sharp s, the skip is 2. */
1003 (skip = SHARP_S_SKIP))) {
1004 if (tmp && (norun || regtry(prog, s)))
1016 while (s < strend) {
1017 if (tmp && (norun || regtry(prog, s)))
1026 ln = STR_LEN(c); /* length to match in octets/bytes */
1027 lnc = (I32) ln; /* length to match in characters */
1029 STRLEN ulen1, ulen2;
1031 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1032 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1034 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1035 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1037 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1038 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1039 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1040 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1042 while (sm < ((U8 *) m + ln)) {
1057 c2 = PL_fold_locale[c1];
1059 e = HOP3c(strend, -((I32)lnc), s);
1062 e = s; /* Due to minlen logic of intuit() */
1064 /* The idea in the EXACTF* cases is to first find the
1065 * first character of the EXACTF* node and then, if
1066 * necessary, case-insensitively compare the full
1067 * text of the node. The c1 and c2 are the first
1068 * characters (though in Unicode it gets a bit
1069 * more complicated because there are more cases
1070 * than just upper and lower: one needs to use
1071 * the so-called folding case for case-insensitive
1072 * matching (called "loose matching" in Unicode).
1073 * ibcmp_utf8() will do just that. */
1077 U8 tmpbuf [UTF8_MAXBYTES+1];
1078 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1079 STRLEN len, foldlen;
1082 /* Upper and lower of 1st char are equal -
1083 * probably not a "letter". */
1085 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1087 0 : UTF8_ALLOW_ANY);
1090 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1091 m, (char **)0, ln, (bool)UTF))
1092 && (norun || regtry(prog, s)) )
1095 uvchr_to_utf8(tmpbuf, c);
1096 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1098 && (f == c1 || f == c2)
1099 && (ln == foldlen ||
1100 !ibcmp_utf8((char *) foldbuf,
1101 (char **)0, foldlen, do_utf8,
1103 (char **)0, ln, (bool)UTF))
1104 && (norun || regtry(prog, s)) )
1112 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1114 0 : UTF8_ALLOW_ANY);
1116 /* Handle some of the three Greek sigmas cases.
1117 * Note that not all the possible combinations
1118 * are handled here: some of them are handled
1119 * by the standard folding rules, and some of
1120 * them (the character class or ANYOF cases)
1121 * are handled during compiletime in
1122 * regexec.c:S_regclass(). */
1123 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1124 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1125 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1127 if ( (c == c1 || c == c2)
1129 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1130 m, (char **)0, ln, (bool)UTF))
1131 && (norun || regtry(prog, s)) )
1134 uvchr_to_utf8(tmpbuf, c);
1135 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1137 && (f == c1 || f == c2)
1138 && (ln == foldlen ||
1139 !ibcmp_utf8((char *) foldbuf,
1140 (char **)0, foldlen, do_utf8,
1142 (char **)0, ln, (bool)UTF))
1143 && (norun || regtry(prog, s)) )
1154 && (ln == 1 || !(OP(c) == EXACTF
1156 : ibcmp_locale(s, m, ln)))
1157 && (norun || regtry(prog, s)) )
1163 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1164 && (ln == 1 || !(OP(c) == EXACTF
1166 : ibcmp_locale(s, m, ln)))
1167 && (norun || regtry(prog, s)) )
1174 PL_reg_flags |= RF_tainted;
1181 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1183 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1185 tmp = ((OP(c) == BOUND ?
1186 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1187 LOAD_UTF8_CHARCLASS(alnum,"a");
1188 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1189 if (tmp == !(OP(c) == BOUND ?
1190 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1191 isALNUM_LC_utf8((U8*)s)))
1194 if ((norun || regtry(prog, s)))
1201 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1202 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1203 while (s < strend) {
1205 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1207 if ((norun || regtry(prog, s)))
1213 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1217 PL_reg_flags |= RF_tainted;
1224 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1226 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1228 tmp = ((OP(c) == NBOUND ?
1229 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1230 LOAD_UTF8_CHARCLASS(alnum,"a");
1231 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1232 if (tmp == !(OP(c) == NBOUND ?
1233 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1234 isALNUM_LC_utf8((U8*)s)))
1236 else if ((norun || regtry(prog, s)))
1242 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1243 tmp = ((OP(c) == NBOUND ?
1244 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1245 while (s < strend) {
1247 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1249 else if ((norun || regtry(prog, s)))
1254 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1259 LOAD_UTF8_CHARCLASS(alnum,"a");
1260 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1261 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1262 if (tmp && (norun || regtry(prog, s)))
1273 while (s < strend) {
1275 if (tmp && (norun || regtry(prog, s)))
1287 PL_reg_flags |= RF_tainted;
1289 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1290 if (isALNUM_LC_utf8((U8*)s)) {
1291 if (tmp && (norun || regtry(prog, s)))
1302 while (s < strend) {
1303 if (isALNUM_LC(*s)) {
1304 if (tmp && (norun || regtry(prog, s)))
1317 LOAD_UTF8_CHARCLASS(alnum,"a");
1318 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1319 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1320 if (tmp && (norun || regtry(prog, s)))
1331 while (s < strend) {
1333 if (tmp && (norun || regtry(prog, s)))
1345 PL_reg_flags |= RF_tainted;
1347 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1348 if (!isALNUM_LC_utf8((U8*)s)) {
1349 if (tmp && (norun || regtry(prog, s)))
1360 while (s < strend) {
1361 if (!isALNUM_LC(*s)) {
1362 if (tmp && (norun || regtry(prog, s)))
1375 LOAD_UTF8_CHARCLASS(space," ");
1376 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1377 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1378 if (tmp && (norun || regtry(prog, s)))
1389 while (s < strend) {
1391 if (tmp && (norun || regtry(prog, s)))
1403 PL_reg_flags |= RF_tainted;
1405 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1406 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1407 if (tmp && (norun || regtry(prog, s)))
1418 while (s < strend) {
1419 if (isSPACE_LC(*s)) {
1420 if (tmp && (norun || regtry(prog, s)))
1433 LOAD_UTF8_CHARCLASS(space," ");
1434 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1435 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1436 if (tmp && (norun || regtry(prog, s)))
1447 while (s < strend) {
1449 if (tmp && (norun || regtry(prog, s)))
1461 PL_reg_flags |= RF_tainted;
1463 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1464 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1465 if (tmp && (norun || regtry(prog, s)))
1476 while (s < strend) {
1477 if (!isSPACE_LC(*s)) {
1478 if (tmp && (norun || regtry(prog, s)))
1491 LOAD_UTF8_CHARCLASS(digit,"0");
1492 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1493 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1494 if (tmp && (norun || regtry(prog, s)))
1505 while (s < strend) {
1507 if (tmp && (norun || regtry(prog, s)))
1519 PL_reg_flags |= RF_tainted;
1521 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1522 if (isDIGIT_LC_utf8((U8*)s)) {
1523 if (tmp && (norun || regtry(prog, s)))
1534 while (s < strend) {
1535 if (isDIGIT_LC(*s)) {
1536 if (tmp && (norun || regtry(prog, s)))
1549 LOAD_UTF8_CHARCLASS(digit,"0");
1550 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1551 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1552 if (tmp && (norun || regtry(prog, s)))
1563 while (s < strend) {
1565 if (tmp && (norun || regtry(prog, s)))
1577 PL_reg_flags |= RF_tainted;
1579 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1580 if (!isDIGIT_LC_utf8((U8*)s)) {
1581 if (tmp && (norun || regtry(prog, s)))
1592 while (s < strend) {
1593 if (!isDIGIT_LC(*s)) {
1594 if (tmp && (norun || regtry(prog, s)))
1606 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1615 - regexec_flags - match a regexp against a string
1618 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1619 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1620 /* strend: pointer to null at end of string */
1621 /* strbeg: real beginning of string */
1622 /* minend: end of match must be >=minend after stringarg. */
1623 /* data: May be used for some additional optimizations. */
1624 /* nosave: For optimizations. */
1627 register regnode *c;
1628 register char *startpos = stringarg;
1629 I32 minlen; /* must match at least this many chars */
1630 I32 dontbother = 0; /* how many characters not to try at end */
1631 /* I32 start_shift = 0; */ /* Offset of the start to find
1632 constant substr. */ /* CC */
1633 I32 end_shift = 0; /* Same for the end. */ /* CC */
1634 I32 scream_pos = -1; /* Internal iterator of scream. */
1636 SV* oreplsv = GvSV(PL_replgv);
1637 bool do_utf8 = DO_UTF8(sv);
1638 I32 multiline = prog->reganch & PMf_MULTILINE;
1640 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1641 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1643 RX_MATCH_UTF8_set(prog,do_utf8);
1649 PL_regnarrate = DEBUG_r_TEST;
1652 /* Be paranoid... */
1653 if (prog == NULL || startpos == NULL) {
1654 Perl_croak(aTHX_ "NULL regexp parameter");
1658 minlen = prog->minlen;
1659 if (strend - startpos < minlen) {
1660 DEBUG_r(PerlIO_printf(Perl_debug_log,
1661 "String too short [regexec_flags]...\n"));
1665 /* Check validity of program. */
1666 if (UCHARAT(prog->program) != REG_MAGIC) {
1667 Perl_croak(aTHX_ "corrupted regexp program");
1671 PL_reg_eval_set = 0;
1674 if (prog->reganch & ROPT_UTF8)
1675 PL_reg_flags |= RF_utf8;
1677 /* Mark beginning of line for ^ and lookbehind. */
1678 PL_regbol = startpos;
1682 /* Mark end of line for $ (and such) */
1685 /* see how far we have to get to not match where we matched before */
1686 PL_regtill = startpos+minend;
1688 /* We start without call_cc context. */
1691 /* If there is a "must appear" string, look for it. */
1694 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1697 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1698 PL_reg_ganch = startpos;
1699 else if (sv && SvTYPE(sv) >= SVt_PVMG
1701 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1702 && mg->mg_len >= 0) {
1703 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1704 if (prog->reganch & ROPT_ANCH_GPOS) {
1705 if (s > PL_reg_ganch)
1710 else /* pos() not defined */
1711 PL_reg_ganch = strbeg;
1714 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1715 re_scream_pos_data d;
1717 d.scream_olds = &scream_olds;
1718 d.scream_pos = &scream_pos;
1719 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1721 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1722 goto phooey; /* not present */
1728 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1729 UNI_DISPLAY_REGEX) :
1731 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1732 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1733 UNI_DISPLAY_REGEX) : startpos;
1734 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1737 PerlIO_printf(Perl_debug_log,
1738 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1739 PL_colors[4],PL_colors[5],PL_colors[0],
1742 len0 > 60 ? "..." : "",
1744 (int)(len1 > 60 ? 60 : len1),
1746 (len1 > 60 ? "..." : "")
1750 /* Simplest case: anchored match need be tried only once. */
1751 /* [unless only anchor is BOL and multiline is set] */
1752 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1753 if (s == startpos && regtry(prog, startpos))
1755 else if (multiline || (prog->reganch & ROPT_IMPLICIT)
1756 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1761 dontbother = minlen - 1;
1762 end = HOP3c(strend, -dontbother, strbeg) - 1;
1763 /* for multiline we only have to try after newlines */
1764 if (prog->check_substr || prog->check_utf8) {
1768 if (regtry(prog, s))
1773 if (prog->reganch & RE_USE_INTUIT) {
1774 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1785 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1786 if (regtry(prog, s))
1793 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1794 if (regtry(prog, PL_reg_ganch))
1799 /* Messy cases: unanchored match. */
1800 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1801 /* we have /x+whatever/ */
1802 /* it must be a one character string (XXXX Except UTF?) */
1807 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1808 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1809 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1812 while (s < strend) {
1814 DEBUG_r( did_match = 1 );
1815 if (regtry(prog, s)) goto got_it;
1817 while (s < strend && *s == ch)
1824 while (s < strend) {
1826 DEBUG_r( did_match = 1 );
1827 if (regtry(prog, s)) goto got_it;
1829 while (s < strend && *s == ch)
1835 DEBUG_r(if (!did_match)
1836 PerlIO_printf(Perl_debug_log,
1837 "Did not find anchored character...\n")
1841 else if (prog->anchored_substr != Nullsv
1842 || prog->anchored_utf8 != Nullsv
1843 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1844 && prog->float_max_offset < strend - s)) {
1849 char *last1; /* Last position checked before */
1853 if (prog->anchored_substr || prog->anchored_utf8) {
1854 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1855 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1856 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1857 back_max = back_min = prog->anchored_offset;
1859 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1860 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1861 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1862 back_max = prog->float_max_offset;
1863 back_min = prog->float_min_offset;
1865 if (must == &PL_sv_undef)
1866 /* could not downgrade utf8 check substring, so must fail */
1869 last = HOP3c(strend, /* Cannot start after this */
1870 -(I32)(CHR_SVLEN(must)
1871 - (SvTAIL(must) != 0) + back_min), strbeg);
1874 last1 = HOPc(s, -1);
1876 last1 = s - 1; /* bogus */
1878 /* XXXX check_substr already used to find `s', can optimize if
1879 check_substr==must. */
1881 dontbother = end_shift;
1882 strend = HOPc(strend, -dontbother);
1883 while ( (s <= last) &&
1884 ((flags & REXEC_SCREAM)
1885 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1886 end_shift, &scream_pos, 0))
1887 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1888 (unsigned char*)strend, must,
1889 multiline ? FBMrf_MULTILINE : 0))) ) {
1890 /* we may be pointing at the wrong string */
1891 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1892 s = strbeg + (s - SvPVX(sv));
1893 DEBUG_r( did_match = 1 );
1894 if (HOPc(s, -back_max) > last1) {
1895 last1 = HOPc(s, -back_min);
1896 s = HOPc(s, -back_max);
1899 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1901 last1 = HOPc(s, -back_min);
1905 while (s <= last1) {
1906 if (regtry(prog, s))
1912 while (s <= last1) {
1913 if (regtry(prog, s))
1919 DEBUG_r(if (!did_match)
1920 PerlIO_printf(Perl_debug_log,
1921 "Did not find %s substr `%s%.*s%s'%s...\n",
1922 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1923 ? "anchored" : "floating"),
1925 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1927 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1931 else if ((c = prog->regstclass)) {
1933 I32 op = (U8)OP(prog->regstclass);
1934 /* don't bother with what can't match */
1935 if (PL_regkind[op] != EXACT && op != CANY)
1936 strend = HOPc(strend, -(minlen - 1));
1939 SV *prop = sv_newmortal();
1947 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1948 UNI_DISPLAY_REGEX) :
1950 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1952 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1953 len1 = UTF ? SvCUR(dsv1) : strend - s;
1954 PerlIO_printf(Perl_debug_log,
1955 "Matching stclass `%*.*s' against `%*.*s'\n",
1959 if (find_byclass(prog, c, s, strend, startpos, 0))
1961 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1965 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1970 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1971 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1972 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1974 if (flags & REXEC_SCREAM) {
1975 last = screaminstr(sv, float_real, s - strbeg,
1976 end_shift, &scream_pos, 1); /* last one */
1978 last = scream_olds; /* Only one occurrence. */
1979 /* we may be pointing at the wrong string */
1980 else if (RX_MATCH_COPIED(prog))
1981 s = strbeg + (s - SvPVX(sv));
1985 char *little = SvPV(float_real, len);
1987 if (SvTAIL(float_real)) {
1988 if (memEQ(strend - len + 1, little, len - 1))
1989 last = strend - len + 1;
1990 else if (!multiline)
1991 last = memEQ(strend - len, little, len)
1992 ? strend - len : Nullch;
1998 last = rninstr(s, strend, little, little + len);
2000 last = strend; /* matching `$' */
2004 DEBUG_r(PerlIO_printf(Perl_debug_log,
2005 "%sCan't trim the tail, match fails (should not happen)%s\n",
2006 PL_colors[4],PL_colors[5]));
2007 goto phooey; /* Should not happen! */
2009 dontbother = strend - last + prog->float_min_offset;
2011 if (minlen && (dontbother < minlen))
2012 dontbother = minlen - 1;
2013 strend -= dontbother; /* this one's always in bytes! */
2014 /* We don't know much -- general case. */
2017 if (regtry(prog, s))
2026 if (regtry(prog, s))
2028 } while (s++ < strend);
2036 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2038 if (PL_reg_eval_set) {
2039 /* Preserve the current value of $^R */
2040 if (oreplsv != GvSV(PL_replgv))
2041 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2042 restored, the value remains
2044 restore_pos(aTHX_ 0);
2047 /* make sure $`, $&, $', and $digit will work later */
2048 if ( !(flags & REXEC_NOT_FIRST) ) {
2049 RX_MATCH_COPY_FREE(prog);
2050 if (flags & REXEC_COPY_STR) {
2051 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2052 #ifdef PERL_COPY_ON_WRITE
2054 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2056 PerlIO_printf(Perl_debug_log,
2057 "Copy on write: regexp capture, type %d\n",
2060 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2061 prog->subbeg = SvPVX(prog->saved_copy);
2062 assert (SvPOKp(prog->saved_copy));
2066 RX_MATCH_COPIED_on(prog);
2067 s = savepvn(strbeg, i);
2073 prog->subbeg = strbeg;
2074 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2081 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2082 PL_colors[4],PL_colors[5]));
2083 if (PL_reg_eval_set)
2084 restore_pos(aTHX_ 0);
2089 - regtry - try match at specific point
2091 STATIC I32 /* 0 failure, 1 success */
2092 S_regtry(pTHX_ regexp *prog, char *startpos)
2100 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2102 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2105 PL_reg_eval_set = RS_init;
2107 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2108 (IV)(PL_stack_sp - PL_stack_base));
2110 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2111 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2112 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2114 /* Apparently this is not needed, judging by wantarray. */
2115 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2116 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2119 /* Make $_ available to executed code. */
2120 if (PL_reg_sv != DEFSV) {
2125 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2126 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2127 /* prepare for quick setting of pos */
2128 sv_magic(PL_reg_sv, (SV*)0,
2129 PERL_MAGIC_regex_global, Nullch, 0);
2130 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2134 PL_reg_oldpos = mg->mg_len;
2135 SAVEDESTRUCTOR_X(restore_pos, 0);
2137 if (!PL_reg_curpm) {
2138 Newz(22,PL_reg_curpm, 1, PMOP);
2141 SV* repointer = newSViv(0);
2142 /* so we know which PL_regex_padav element is PL_reg_curpm */
2143 SvFLAGS(repointer) |= SVf_BREAK;
2144 av_push(PL_regex_padav,repointer);
2145 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2146 PL_regex_pad = AvARRAY(PL_regex_padav);
2150 PM_SETRE(PL_reg_curpm, prog);
2151 PL_reg_oldcurpm = PL_curpm;
2152 PL_curpm = PL_reg_curpm;
2153 if (RX_MATCH_COPIED(prog)) {
2154 /* Here is a serious problem: we cannot rewrite subbeg,
2155 since it may be needed if this match fails. Thus
2156 $` inside (?{}) could fail... */
2157 PL_reg_oldsaved = prog->subbeg;
2158 PL_reg_oldsavedlen = prog->sublen;
2159 #ifdef PERL_COPY_ON_WRITE
2160 PL_nrs = prog->saved_copy;
2162 RX_MATCH_COPIED_off(prog);
2165 PL_reg_oldsaved = Nullch;
2166 prog->subbeg = PL_bostr;
2167 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2169 prog->startp[0] = startpos - PL_bostr;
2170 PL_reginput = startpos;
2171 PL_regstartp = prog->startp;
2172 PL_regendp = prog->endp;
2173 PL_reglastparen = &prog->lastparen;
2174 PL_reglastcloseparen = &prog->lastcloseparen;
2175 prog->lastparen = 0;
2176 prog->lastcloseparen = 0;
2178 DEBUG_r(PL_reg_starttry = startpos);
2179 if (PL_reg_start_tmpl <= prog->nparens) {
2180 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2181 if(PL_reg_start_tmp)
2182 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2184 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2187 /* XXXX What this code is doing here?!!! There should be no need
2188 to do this again and again, PL_reglastparen should take care of
2191 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2192 * Actually, the code in regcppop() (which Ilya may be meaning by
2193 * PL_reglastparen), is not needed at all by the test suite
2194 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2195 * enough, for building DynaLoader, or otherwise this
2196 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2197 * will happen. Meanwhile, this code *is* needed for the
2198 * above-mentioned test suite tests to succeed. The common theme
2199 * on those tests seems to be returning null fields from matches.
2204 if (prog->nparens) {
2205 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2212 if (regmatch(prog->program + 1)) {
2213 prog->endp[0] = PL_reginput - PL_bostr;
2216 REGCP_UNWIND(lastcp);
2220 #define RE_UNWIND_BRANCH 1
2221 #define RE_UNWIND_BRANCHJ 2
2225 typedef struct { /* XX: makes sense to enlarge it... */
2229 } re_unwind_generic_t;
2242 } re_unwind_branch_t;
2244 typedef union re_unwind_t {
2246 re_unwind_generic_t generic;
2247 re_unwind_branch_t branch;
2250 #define sayYES goto yes
2251 #define sayNO goto no
2252 #define sayNO_ANYOF goto no_anyof
2253 #define sayYES_FINAL goto yes_final
2254 #define sayYES_LOUD goto yes_loud
2255 #define sayNO_FINAL goto no_final
2256 #define sayNO_SILENT goto do_no
2257 #define saySAME(x) if (x) goto yes; else goto no
2259 #define REPORT_CODE_OFF 24
2262 - regmatch - main matching routine
2264 * Conceptually the strategy is simple: check to see whether the current
2265 * node matches, call self recursively to see whether the rest matches,
2266 * and then act accordingly. In practice we make some effort to avoid
2267 * recursion, in particular by going through "ordinary" nodes (that don't
2268 * need to know whether the rest of the match failed) by a loop instead of
2271 /* [lwall] I've hoisted the register declarations to the outer block in order to
2272 * maybe save a little bit of pushing and popping on the stack. It also takes
2273 * advantage of machines that use a register save mask on subroutine entry.
2275 STATIC I32 /* 0 failure, 1 success */
2276 S_regmatch(pTHX_ regnode *prog)
2278 register regnode *scan; /* Current node. */
2279 regnode *next; /* Next node. */
2280 regnode *inner; /* Next node in internal branch. */
2281 register I32 nextchr; /* renamed nextchr - nextchar colides with
2282 function of same name */
2283 register I32 n; /* no or next */
2284 register I32 ln = 0; /* len or last */
2285 register char *s = Nullch; /* operand or save */
2286 register char *locinput = PL_reginput;
2287 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2288 int minmod = 0, sw = 0, logical = 0;
2291 I32 firstcp = PL_savestack_ix;
2293 register bool do_utf8 = PL_reg_match_utf8;
2295 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2296 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2297 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2304 /* Note that nextchr is a byte even in UTF */
2305 nextchr = UCHARAT(locinput);
2307 while (scan != NULL) {
2310 SV *prop = sv_newmortal();
2311 int docolor = *PL_colors[0];
2312 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2313 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2314 /* The part of the string before starttry has one color
2315 (pref0_len chars), between starttry and current
2316 position another one (pref_len - pref0_len chars),
2317 after the current position the third one.
2318 We assume that pref0_len <= pref_len, otherwise we
2319 decrease pref0_len. */
2320 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2321 ? (5 + taill) - l : locinput - PL_bostr;
2324 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2326 pref0_len = pref_len - (locinput - PL_reg_starttry);
2327 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2328 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2329 ? (5 + taill) - pref_len : PL_regeol - locinput);
2330 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2334 if (pref0_len > pref_len)
2335 pref0_len = pref_len;
2336 regprop(prop, scan);
2339 do_utf8 && OP(scan) != CANY ?
2340 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2341 pref0_len, 60, UNI_DISPLAY_REGEX) :
2342 locinput - pref_len;
2343 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2344 char *s1 = do_utf8 && OP(scan) != CANY ?
2345 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2346 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2347 locinput - pref_len + pref0_len;
2348 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2349 char *s2 = do_utf8 && OP(scan) != CANY ?
2350 pv_uni_display(dsv2, (U8*)locinput,
2351 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2353 int len2 = do_utf8 ? strlen(s2) : l;
2354 PerlIO_printf(Perl_debug_log,
2355 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2356 (IV)(locinput - PL_bostr),
2363 (docolor ? "" : "> <"),
2367 15 - l - pref_len + 1,
2369 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2374 next = scan + NEXT_OFF(scan);
2380 if (locinput == PL_bostr)
2382 /* regtill = regbol; */
2387 if (locinput == PL_bostr ||
2388 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2394 if (locinput == PL_bostr)
2398 if (locinput == PL_reg_ganch)
2404 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2409 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2411 if (PL_regeol - locinput > 1)
2415 if (PL_regeol != locinput)
2419 if (!nextchr && locinput >= PL_regeol)
2422 locinput += PL_utf8skip[nextchr];
2423 if (locinput > PL_regeol)
2425 nextchr = UCHARAT(locinput);
2428 nextchr = UCHARAT(++locinput);
2431 if (!nextchr && locinput >= PL_regeol)
2433 nextchr = UCHARAT(++locinput);
2436 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2439 locinput += PL_utf8skip[nextchr];
2440 if (locinput > PL_regeol)
2442 nextchr = UCHARAT(locinput);
2445 nextchr = UCHARAT(++locinput);
2450 if (do_utf8 != UTF) {
2451 /* The target and the pattern have differing utf8ness. */
2457 /* The target is utf8, the pattern is not utf8. */
2461 if (NATIVE_TO_UNI(*(U8*)s) !=
2462 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
2464 0 : UTF8_ALLOW_ANY))
2471 /* The target is not utf8, the pattern is utf8. */
2475 if (NATIVE_TO_UNI(*((U8*)l)) !=
2476 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
2478 0 : UTF8_ALLOW_ANY))
2485 nextchr = UCHARAT(locinput);
2488 /* The target and the pattern have the same utf8ness. */
2489 /* Inline the first character, for speed. */
2490 if (UCHARAT(s) != nextchr)
2492 if (PL_regeol - locinput < ln)
2494 if (ln > 1 && memNE(s, locinput, ln))
2497 nextchr = UCHARAT(locinput);
2500 PL_reg_flags |= RF_tainted;
2506 if (do_utf8 || UTF) {
2507 /* Either target or the pattern are utf8. */
2509 char *e = PL_regeol;
2511 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2512 l, &e, 0, do_utf8)) {
2513 /* One more case for the sharp s:
2514 * pack("U0U*", 0xDF) =~ /ss/i,
2515 * the 0xC3 0x9F are the UTF-8
2516 * byte sequence for the U+00DF. */
2518 toLOWER(s[0]) == 's' &&
2520 toLOWER(s[1]) == 's' &&
2527 nextchr = UCHARAT(locinput);
2531 /* Neither the target and the pattern are utf8. */
2533 /* Inline the first character, for speed. */
2534 if (UCHARAT(s) != nextchr &&
2535 UCHARAT(s) != ((OP(scan) == EXACTF)
2536 ? PL_fold : PL_fold_locale)[nextchr])
2538 if (PL_regeol - locinput < ln)
2540 if (ln > 1 && (OP(scan) == EXACTF
2541 ? ibcmp(s, locinput, ln)
2542 : ibcmp_locale(s, locinput, ln)))
2545 nextchr = UCHARAT(locinput);
2549 STRLEN inclasslen = PL_regeol - locinput;
2551 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2553 if (locinput >= PL_regeol)
2555 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2556 nextchr = UCHARAT(locinput);
2561 nextchr = UCHARAT(locinput);
2562 if (!REGINCLASS(scan, (U8*)locinput))
2564 if (!nextchr && locinput >= PL_regeol)
2566 nextchr = UCHARAT(++locinput);
2570 /* If we might have the case of the German sharp s
2571 * in a casefolding Unicode character class. */
2573 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2574 locinput += SHARP_S_SKIP;
2575 nextchr = UCHARAT(locinput);
2581 PL_reg_flags |= RF_tainted;
2587 LOAD_UTF8_CHARCLASS(alnum,"a");
2588 if (!(OP(scan) == ALNUM
2589 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2590 : isALNUM_LC_utf8((U8*)locinput)))
2594 locinput += PL_utf8skip[nextchr];
2595 nextchr = UCHARAT(locinput);
2598 if (!(OP(scan) == ALNUM
2599 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2601 nextchr = UCHARAT(++locinput);
2604 PL_reg_flags |= RF_tainted;
2607 if (!nextchr && locinput >= PL_regeol)
2610 LOAD_UTF8_CHARCLASS(alnum,"a");
2611 if (OP(scan) == NALNUM
2612 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2613 : isALNUM_LC_utf8((U8*)locinput))
2617 locinput += PL_utf8skip[nextchr];
2618 nextchr = UCHARAT(locinput);
2621 if (OP(scan) == NALNUM
2622 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2624 nextchr = UCHARAT(++locinput);
2628 PL_reg_flags |= RF_tainted;
2632 /* was last char in word? */
2634 if (locinput == PL_bostr)
2637 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2639 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2641 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2642 ln = isALNUM_uni(ln);
2643 LOAD_UTF8_CHARCLASS(alnum,"a");
2644 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2647 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2648 n = isALNUM_LC_utf8((U8*)locinput);
2652 ln = (locinput != PL_bostr) ?
2653 UCHARAT(locinput - 1) : '\n';
2654 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2656 n = isALNUM(nextchr);
2659 ln = isALNUM_LC(ln);
2660 n = isALNUM_LC(nextchr);
2663 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2664 OP(scan) == BOUNDL))
2668 PL_reg_flags |= RF_tainted;
2674 if (UTF8_IS_CONTINUED(nextchr)) {
2675 LOAD_UTF8_CHARCLASS(space," ");
2676 if (!(OP(scan) == SPACE
2677 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2678 : isSPACE_LC_utf8((U8*)locinput)))
2682 locinput += PL_utf8skip[nextchr];
2683 nextchr = UCHARAT(locinput);
2686 if (!(OP(scan) == SPACE
2687 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2689 nextchr = UCHARAT(++locinput);
2692 if (!(OP(scan) == SPACE
2693 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2695 nextchr = UCHARAT(++locinput);
2699 PL_reg_flags |= RF_tainted;
2702 if (!nextchr && locinput >= PL_regeol)
2705 LOAD_UTF8_CHARCLASS(space," ");
2706 if (OP(scan) == NSPACE
2707 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2708 : isSPACE_LC_utf8((U8*)locinput))
2712 locinput += PL_utf8skip[nextchr];
2713 nextchr = UCHARAT(locinput);
2716 if (OP(scan) == NSPACE
2717 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2719 nextchr = UCHARAT(++locinput);
2722 PL_reg_flags |= RF_tainted;
2728 LOAD_UTF8_CHARCLASS(digit,"0");
2729 if (!(OP(scan) == DIGIT
2730 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2731 : isDIGIT_LC_utf8((U8*)locinput)))
2735 locinput += PL_utf8skip[nextchr];
2736 nextchr = UCHARAT(locinput);
2739 if (!(OP(scan) == DIGIT
2740 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2742 nextchr = UCHARAT(++locinput);
2745 PL_reg_flags |= RF_tainted;
2748 if (!nextchr && locinput >= PL_regeol)
2751 LOAD_UTF8_CHARCLASS(digit,"0");
2752 if (OP(scan) == NDIGIT
2753 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2754 : isDIGIT_LC_utf8((U8*)locinput))
2758 locinput += PL_utf8skip[nextchr];
2759 nextchr = UCHARAT(locinput);
2762 if (OP(scan) == NDIGIT
2763 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2765 nextchr = UCHARAT(++locinput);
2768 if (locinput >= PL_regeol)
2771 LOAD_UTF8_CHARCLASS(mark,"~");
2772 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2774 locinput += PL_utf8skip[nextchr];
2775 while (locinput < PL_regeol &&
2776 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2777 locinput += UTF8SKIP(locinput);
2778 if (locinput > PL_regeol)
2783 nextchr = UCHARAT(locinput);
2786 PL_reg_flags |= RF_tainted;
2790 n = ARG(scan); /* which paren pair */
2791 ln = PL_regstartp[n];
2792 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2793 if ((I32)*PL_reglastparen < n || ln == -1)
2794 sayNO; /* Do not match unless seen CLOSEn. */
2795 if (ln == PL_regendp[n])
2799 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2801 char *e = PL_bostr + PL_regendp[n];
2803 * Note that we can't do the "other character" lookup trick as
2804 * in the 8-bit case (no pun intended) because in Unicode we
2805 * have to map both upper and title case to lower case.
2807 if (OP(scan) == REFF) {
2808 STRLEN ulen1, ulen2;
2809 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
2810 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
2814 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2815 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2816 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2823 nextchr = UCHARAT(locinput);
2827 /* Inline the first character, for speed. */
2828 if (UCHARAT(s) != nextchr &&
2830 (UCHARAT(s) != ((OP(scan) == REFF
2831 ? PL_fold : PL_fold_locale)[nextchr]))))
2833 ln = PL_regendp[n] - ln;
2834 if (locinput + ln > PL_regeol)
2836 if (ln > 1 && (OP(scan) == REF
2837 ? memNE(s, locinput, ln)
2839 ? ibcmp(s, locinput, ln)
2840 : ibcmp_locale(s, locinput, ln))))
2843 nextchr = UCHARAT(locinput);
2854 OP_4tree *oop = PL_op;
2855 COP *ocurcop = PL_curcop;
2858 struct regexp *oreg = PL_reg_re;
2861 PL_op = (OP_4tree*)PL_regdata->data[n];
2862 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2863 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2864 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2868 CALLRUNOPS(aTHX); /* Scalar context. */
2871 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2879 PAD_RESTORE_LOCAL(old_comppad);
2880 PL_curcop = ocurcop;
2882 if (logical == 2) { /* Postponed subexpression. */
2884 MAGIC *mg = Null(MAGIC*);
2886 CHECKPOINT cp, lastcp;
2890 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
2891 mg = mg_find(sv, PERL_MAGIC_qr);
2892 else if (SvSMAGICAL(ret)) {
2893 if (SvGMAGICAL(ret))
2894 sv_unmagic(ret, PERL_MAGIC_qr);
2896 mg = mg_find(ret, PERL_MAGIC_qr);
2900 re = (regexp *)mg->mg_obj;
2901 (void)ReREFCNT_inc(re);
2905 char *t = SvPV(ret, len);
2907 char *oprecomp = PL_regprecomp;
2908 I32 osize = PL_regsize;
2909 I32 onpar = PL_regnpar;
2912 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2913 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2915 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
2917 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2919 PL_regprecomp = oprecomp;
2924 PerlIO_printf(Perl_debug_log,
2925 "Entering embedded `%s%.60s%s%s'\n",
2929 (strlen(re->precomp) > 60 ? "..." : ""))
2932 state.prev = PL_reg_call_cc;
2933 state.cc = PL_regcc;
2934 state.re = PL_reg_re;
2938 cp = regcppush(0); /* Save *all* the positions. */
2941 state.ss = PL_savestack_ix;
2942 *PL_reglastparen = 0;
2943 *PL_reglastcloseparen = 0;
2944 PL_reg_call_cc = &state;
2945 PL_reginput = locinput;
2946 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2947 ((re->reganch & ROPT_UTF8) != 0);
2948 if (toggleutf) PL_reg_flags ^= RF_utf8;
2950 /* XXXX This is too dramatic a measure... */
2953 if (regmatch(re->program + 1)) {
2954 /* Even though we succeeded, we need to restore
2955 global variables, since we may be wrapped inside
2956 SUSPEND, thus the match may be not finished yet. */
2958 /* XXXX Do this only if SUSPENDed? */
2959 PL_reg_call_cc = state.prev;
2960 PL_regcc = state.cc;
2961 PL_reg_re = state.re;
2962 cache_re(PL_reg_re);
2963 if (toggleutf) PL_reg_flags ^= RF_utf8;
2965 /* XXXX This is too dramatic a measure... */
2968 /* These are needed even if not SUSPEND. */
2974 REGCP_UNWIND(lastcp);
2976 PL_reg_call_cc = state.prev;
2977 PL_regcc = state.cc;
2978 PL_reg_re = state.re;
2979 cache_re(PL_reg_re);
2980 if (toggleutf) PL_reg_flags ^= RF_utf8;
2982 /* XXXX This is too dramatic a measure... */
2992 sv_setsv(save_scalar(PL_replgv), ret);
2998 n = ARG(scan); /* which paren pair */
2999 PL_reg_start_tmp[n] = locinput;
3004 n = ARG(scan); /* which paren pair */
3005 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3006 PL_regendp[n] = locinput - PL_bostr;
3007 if (n > (I32)*PL_reglastparen)
3008 *PL_reglastparen = n;
3009 *PL_reglastcloseparen = n;
3012 n = ARG(scan); /* which paren pair */
3013 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
3016 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3018 next = NEXTOPER(NEXTOPER(scan));
3020 next = scan + ARG(scan);
3021 if (OP(next) == IFTHEN) /* Fake one. */
3022 next = NEXTOPER(NEXTOPER(next));
3026 logical = scan->flags;
3028 /*******************************************************************
3029 PL_regcc contains infoblock about the innermost (...)* loop, and
3030 a pointer to the next outer infoblock.
3032 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3034 1) After matching X, regnode for CURLYX is processed;
3036 2) This regnode creates infoblock on the stack, and calls
3037 regmatch() recursively with the starting point at WHILEM node;
3039 3) Each hit of WHILEM node tries to match A and Z (in the order
3040 depending on the current iteration, min/max of {min,max} and
3041 greediness). The information about where are nodes for "A"
3042 and "Z" is read from the infoblock, as is info on how many times "A"
3043 was already matched, and greediness.
3045 4) After A matches, the same WHILEM node is hit again.
3047 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3048 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3049 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3050 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3051 of the external loop.
3053 Currently present infoblocks form a tree with a stem formed by PL_curcc
3054 and whatever it mentions via ->next, and additional attached trees
3055 corresponding to temporarily unset infoblocks as in "5" above.
3057 In the following picture infoblocks for outer loop of
3058 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3059 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3060 infoblocks are drawn below the "reset" infoblock.
3062 In fact in the picture below we do not show failed matches for Z and T
3063 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3064 more obvious *why* one needs to *temporary* unset infoblocks.]
3066 Matched REx position InfoBlocks Comment
3070 Y A)*?Z)*?T x <- O <- I
3071 YA )*?Z)*?T x <- O <- I
3072 YA A)*?Z)*?T x <- O <- I
3073 YAA )*?Z)*?T x <- O <- I
3074 YAA Z)*?T x <- O # Temporary unset I
3077 YAAZ Y(A)*?Z)*?T x <- O
3080 YAAZY (A)*?Z)*?T x <- O
3083 YAAZY A)*?Z)*?T x <- O <- I
3086 YAAZYA )*?Z)*?T x <- O <- I
3089 YAAZYA Z)*?T x <- O # Temporary unset I
3095 YAAZYAZ T x # Temporary unset O
3102 *******************************************************************/
3105 CHECKPOINT cp = PL_savestack_ix;
3106 /* No need to save/restore up to this paren */
3107 I32 parenfloor = scan->flags;
3109 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3111 cc.oldcc = PL_regcc;
3113 /* XXXX Probably it is better to teach regpush to support
3114 parenfloor > PL_regsize... */
3115 if (parenfloor > (I32)*PL_reglastparen)
3116 parenfloor = *PL_reglastparen; /* Pessimization... */
3117 cc.parenfloor = parenfloor;
3119 cc.min = ARG1(scan);
3120 cc.max = ARG2(scan);
3121 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3125 PL_reginput = locinput;
3126 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3128 PL_regcc = cc.oldcc;
3134 * This is really hard to understand, because after we match
3135 * what we're trying to match, we must make sure the rest of
3136 * the REx is going to match for sure, and to do that we have
3137 * to go back UP the parse tree by recursing ever deeper. And
3138 * if it fails, we have to reset our parent's current state
3139 * that we can try again after backing off.
3142 CHECKPOINT cp, lastcp;
3143 CURCUR* cc = PL_regcc;
3144 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3146 n = cc->cur + 1; /* how many we know we matched */
3147 PL_reginput = locinput;
3150 PerlIO_printf(Perl_debug_log,
3151 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3152 REPORT_CODE_OFF+PL_regindent*2, "",
3153 (long)n, (long)cc->min,
3154 (long)cc->max, PTR2UV(cc))
3157 /* If degenerate scan matches "", assume scan done. */
3159 if (locinput == cc->lastloc && n >= cc->min) {
3160 PL_regcc = cc->oldcc;
3164 PerlIO_printf(Perl_debug_log,
3165 "%*s empty match detected, try continuation...\n",
3166 REPORT_CODE_OFF+PL_regindent*2, "")
3168 if (regmatch(cc->next))
3176 /* First just match a string of min scans. */
3180 cc->lastloc = locinput;
3181 if (regmatch(cc->scan))
3184 cc->lastloc = lastloc;
3189 /* Check whether we already were at this position.
3190 Postpone detection until we know the match is not
3191 *that* much linear. */
3192 if (!PL_reg_maxiter) {
3193 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3194 PL_reg_leftiter = PL_reg_maxiter;
3196 if (PL_reg_leftiter-- == 0) {
3197 I32 size = (PL_reg_maxiter + 7)/8;
3198 if (PL_reg_poscache) {
3199 if ((I32)PL_reg_poscache_size < size) {
3200 Renew(PL_reg_poscache, size, char);
3201 PL_reg_poscache_size = size;
3203 Zero(PL_reg_poscache, size, char);
3206 PL_reg_poscache_size = size;
3207 Newz(29, PL_reg_poscache, size, char);
3210 PerlIO_printf(Perl_debug_log,
3211 "%sDetected a super-linear match, switching on caching%s...\n",
3212 PL_colors[4], PL_colors[5])
3215 if (PL_reg_leftiter < 0) {
3216 I32 o = locinput - PL_bostr, b;
3218 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3221 if (PL_reg_poscache[o] & (1<<b)) {
3223 PerlIO_printf(Perl_debug_log,
3224 "%*s already tried at this position...\n",
3225 REPORT_CODE_OFF+PL_regindent*2, "")
3227 if (PL_reg_flags & RF_false)
3232 PL_reg_poscache[o] |= (1<<b);
3236 /* Prefer next over scan for minimal matching. */
3239 PL_regcc = cc->oldcc;
3242 cp = regcppush(cc->parenfloor);
3244 if (regmatch(cc->next)) {
3246 sayYES; /* All done. */
3248 REGCP_UNWIND(lastcp);
3254 if (n >= cc->max) { /* Maximum greed exceeded? */
3255 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3256 && !(PL_reg_flags & RF_warned)) {
3257 PL_reg_flags |= RF_warned;
3258 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3259 "Complex regular subexpression recursion",
3266 PerlIO_printf(Perl_debug_log,
3267 "%*s trying longer...\n",
3268 REPORT_CODE_OFF+PL_regindent*2, "")
3270 /* Try scanning more and see if it helps. */
3271 PL_reginput = locinput;
3273 cc->lastloc = locinput;
3274 cp = regcppush(cc->parenfloor);
3276 if (regmatch(cc->scan)) {
3280 REGCP_UNWIND(lastcp);
3283 cc->lastloc = lastloc;
3287 /* Prefer scan over next for maximal matching. */
3289 if (n < cc->max) { /* More greed allowed? */
3290 cp = regcppush(cc->parenfloor);
3292 cc->lastloc = locinput;
3294 if (regmatch(cc->scan)) {
3298 REGCP_UNWIND(lastcp);
3299 regcppop(); /* Restore some previous $<digit>s? */
3300 PL_reginput = locinput;
3302 PerlIO_printf(Perl_debug_log,
3303 "%*s failed, try continuation...\n",
3304 REPORT_CODE_OFF+PL_regindent*2, "")
3307 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3308 && !(PL_reg_flags & RF_warned)) {
3309 PL_reg_flags |= RF_warned;
3310 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3311 "Complex regular subexpression recursion",
3315 /* Failed deeper matches of scan, so see if this one works. */
3316 PL_regcc = cc->oldcc;
3319 if (regmatch(cc->next))
3325 cc->lastloc = lastloc;
3330 next = scan + ARG(scan);
3333 inner = NEXTOPER(NEXTOPER(scan));
3336 inner = NEXTOPER(scan);
3340 if (OP(next) != c1) /* No choice. */
3341 next = inner; /* Avoid recursion. */
3343 I32 lastparen = *PL_reglastparen;
3345 re_unwind_branch_t *uw;
3347 /* Put unwinding data on stack */
3348 unwind1 = SSNEWt(1,re_unwind_branch_t);
3349 uw = SSPTRt(unwind1,re_unwind_branch_t);
3352 uw->type = ((c1 == BRANCH)
3354 : RE_UNWIND_BRANCHJ);
3355 uw->lastparen = lastparen;
3357 uw->locinput = locinput;
3358 uw->nextchr = nextchr;
3360 uw->regindent = ++PL_regindent;
3363 REGCP_SET(uw->lastcp);
3365 /* Now go into the first branch */
3378 /* We suppose that the next guy does not need
3379 backtracking: in particular, it is of constant non-zero length,
3380 and has no parenths to influence future backrefs. */
3381 ln = ARG1(scan); /* min to match */
3382 n = ARG2(scan); /* max to match */
3383 paren = scan->flags;
3385 if (paren > PL_regsize)
3387 if (paren > (I32)*PL_reglastparen)
3388 *PL_reglastparen = paren;
3390 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3392 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3393 PL_reginput = locinput;
3396 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3398 locinput = PL_reginput;
3399 if (HAS_TEXT(next) || JUMPABLE(next)) {
3400 regnode *text_node = next;
3402 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3404 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3406 if (PL_regkind[(U8)OP(text_node)] == REF) {
3410 else { c1 = (U8)*STRING(text_node); }
3411 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3413 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3414 c2 = PL_fold_locale[c1];
3423 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3424 /* If it could work, try it. */
3426 UCHARAT(PL_reginput) == c1 ||
3427 UCHARAT(PL_reginput) == c2)
3431 PL_regstartp[paren] =
3432 HOPc(PL_reginput, -l) - PL_bostr;
3433 PL_regendp[paren] = PL_reginput - PL_bostr;
3436 PL_regendp[paren] = -1;
3440 REGCP_UNWIND(lastcp);
3442 /* Couldn't or didn't -- move forward. */
3443 PL_reginput = locinput;
3444 if (regrepeat_hard(scan, 1, &l)) {
3446 locinput = PL_reginput;
3453 n = regrepeat_hard(scan, n, &l);
3454 locinput = PL_reginput;
3456 PerlIO_printf(Perl_debug_log,
3457 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3458 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3462 if (HAS_TEXT(next) || JUMPABLE(next)) {
3463 regnode *text_node = next;
3465 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3467 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3469 if (PL_regkind[(U8)OP(text_node)] == REF) {
3473 else { c1 = (U8)*STRING(text_node); }
3475 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3477 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3478 c2 = PL_fold_locale[c1];
3489 /* If it could work, try it. */
3491 UCHARAT(PL_reginput) == c1 ||
3492 UCHARAT(PL_reginput) == c2)
3495 PerlIO_printf(Perl_debug_log,
3496 "%*s trying tail with n=%"IVdf"...\n",
3497 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3501 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3502 PL_regendp[paren] = PL_reginput - PL_bostr;
3505 PL_regendp[paren] = -1;
3509 REGCP_UNWIND(lastcp);
3511 /* Couldn't or didn't -- back up. */
3513 locinput = HOPc(locinput, -l);
3514 PL_reginput = locinput;
3521 paren = scan->flags; /* Which paren to set */
3522 if (paren > PL_regsize)
3524 if (paren > (I32)*PL_reglastparen)
3525 *PL_reglastparen = paren;
3526 ln = ARG1(scan); /* min to match */
3527 n = ARG2(scan); /* max to match */
3528 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3532 ln = ARG1(scan); /* min to match */
3533 n = ARG2(scan); /* max to match */
3534 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3539 scan = NEXTOPER(scan);
3545 scan = NEXTOPER(scan);
3549 * Lookahead to avoid useless match attempts
3550 * when we know what character comes next.
3554 * Used to only do .*x and .*?x, but now it allows
3555 * for )'s, ('s and (?{ ... })'s to be in the way
3556 * of the quantifier and the EXACT-like node. -- japhy
3559 if (HAS_TEXT(next) || JUMPABLE(next)) {
3561 regnode *text_node = next;
3563 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3565 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3567 if (PL_regkind[(U8)OP(text_node)] == REF) {
3569 goto assume_ok_easy;
3571 else { s = (U8*)STRING(text_node); }
3575 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3577 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3578 c2 = PL_fold_locale[c1];
3581 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3582 STRLEN ulen1, ulen2;
3583 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3584 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3586 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3587 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3589 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
3591 0 : UTF8_ALLOW_ANY);
3592 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
3594 0 : UTF8_ALLOW_ANY);
3597 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
3599 0 : UTF8_ALLOW_ANY);
3607 PL_reginput = locinput;
3611 if (ln && regrepeat(scan, ln) < ln)
3613 locinput = PL_reginput;
3616 char *e; /* Should not check after this */
3617 char *old = locinput;
3620 if (n == REG_INFTY) {
3623 while (UTF8_IS_CONTINUATION(*(U8*)e))
3629 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3633 e = locinput + n - ln;
3638 /* Find place 'next' could work */
3641 while (locinput <= e &&
3642 UCHARAT(locinput) != c1)
3645 while (locinput <= e
3646 && UCHARAT(locinput) != c1
3647 && UCHARAT(locinput) != c2)
3650 count = locinput - old;
3655 /* count initialised to
3656 * utf8_distance(old, locinput) */
3657 while (locinput <= e &&
3658 utf8n_to_uvchr((U8*)locinput,
3659 UTF8_MAXBYTES, &len,
3661 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3666 /* count initialised to
3667 * utf8_distance(old, locinput) */
3668 while (locinput <= e) {
3669 UV c = utf8n_to_uvchr((U8*)locinput,
3670 UTF8_MAXBYTES, &len,
3672 0 : UTF8_ALLOW_ANY);
3673 if (c == (UV)c1 || c == (UV)c2)
3682 /* PL_reginput == old now */
3683 if (locinput != old) {
3684 ln = 1; /* Did some */
3685 if (regrepeat(scan, count) < count)
3688 /* PL_reginput == locinput now */
3689 TRYPAREN(paren, ln, locinput);
3690 PL_reginput = locinput; /* Could be reset... */
3691 REGCP_UNWIND(lastcp);
3692 /* Couldn't or didn't -- move forward. */
3695 locinput += UTF8SKIP(locinput);
3702 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3706 c = utf8n_to_uvchr((U8*)PL_reginput,
3709 0 : UTF8_ALLOW_ANY);
3711 c = UCHARAT(PL_reginput);
3712 /* If it could work, try it. */
3713 if (c == (UV)c1 || c == (UV)c2)
3715 TRYPAREN(paren, ln, PL_reginput);
3716 REGCP_UNWIND(lastcp);
3719 /* If it could work, try it. */
3720 else if (c1 == -1000)
3722 TRYPAREN(paren, ln, PL_reginput);
3723 REGCP_UNWIND(lastcp);
3725 /* Couldn't or didn't -- move forward. */
3726 PL_reginput = locinput;
3727 if (regrepeat(scan, 1)) {
3729 locinput = PL_reginput;
3737 n = regrepeat(scan, n);
3738 locinput = PL_reginput;
3739 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3740 (OP(next) != MEOL ||
3741 OP(next) == SEOL || OP(next) == EOS))
3743 ln = n; /* why back off? */
3744 /* ...because $ and \Z can match before *and* after
3745 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3746 We should back off by one in this case. */
3747 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3756 c = utf8n_to_uvchr((U8*)PL_reginput,
3759 0 : UTF8_ALLOW_ANY);
3761 c = UCHARAT(PL_reginput);
3763 /* If it could work, try it. */
3764 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3766 TRYPAREN(paren, n, PL_reginput);
3767 REGCP_UNWIND(lastcp);
3769 /* Couldn't or didn't -- back up. */
3771 PL_reginput = locinput = HOPc(locinput, -1);
3779 c = utf8n_to_uvchr((U8*)PL_reginput,
3782 0 : UTF8_ALLOW_ANY);
3784 c = UCHARAT(PL_reginput);
3786 /* If it could work, try it. */
3787 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3789 TRYPAREN(paren, n, PL_reginput);
3790 REGCP_UNWIND(lastcp);
3792 /* Couldn't or didn't -- back up. */
3794 PL_reginput = locinput = HOPc(locinput, -1);
3801 if (PL_reg_call_cc) {
3802 re_cc_state *cur_call_cc = PL_reg_call_cc;
3803 CURCUR *cctmp = PL_regcc;
3804 regexp *re = PL_reg_re;
3805 CHECKPOINT cp, lastcp;
3807 cp = regcppush(0); /* Save *all* the positions. */
3809 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3811 PL_reginput = locinput; /* Make position available to
3813 cache_re(PL_reg_call_cc->re);
3814 PL_regcc = PL_reg_call_cc->cc;
3815 PL_reg_call_cc = PL_reg_call_cc->prev;
3816 if (regmatch(cur_call_cc->node)) {
3817 PL_reg_call_cc = cur_call_cc;
3821 REGCP_UNWIND(lastcp);
3823 PL_reg_call_cc = cur_call_cc;
3829 PerlIO_printf(Perl_debug_log,
3830 "%*s continuation failed...\n",
3831 REPORT_CODE_OFF+PL_regindent*2, "")
3835 if (locinput < PL_regtill) {
3836 DEBUG_r(PerlIO_printf(Perl_debug_log,
3837 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3839 (long)(locinput - PL_reg_starttry),
3840 (long)(PL_regtill - PL_reg_starttry),
3842 sayNO_FINAL; /* Cannot match: too short. */
3844 PL_reginput = locinput; /* put where regtry can find it */
3845 sayYES_FINAL; /* Success! */
3847 PL_reginput = locinput; /* put where regtry can find it */
3848 sayYES_LOUD; /* Success! */
3851 PL_reginput = locinput;