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-2002, 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 */
99 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
105 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
106 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
108 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
109 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
110 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
111 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
112 #define HOPc(pos,off) ((char*)HOP(pos,off))
113 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
115 #define HOPBACK(pos, off) ( \
116 (PL_reg_match_utf8) \
117 ? reghopmaybe((U8*)pos, -off) \
118 : (pos - off >= PL_bostr) \
122 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
124 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
125 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
126 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
127 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
128 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
129 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
131 #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
133 /* for use after a quantifier and before an EXACT-like node -- japhy */
134 #define JUMPABLE(rn) ( \
135 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
136 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
137 OP(rn) == PLUS || OP(rn) == MINMOD || \
138 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
141 #define HAS_TEXT(rn) ( \
142 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
146 Search for mandatory following text node; for lookahead, the text must
147 follow but for lookbehind (rn->flags != 0) we skip to the next step.
149 #define FIND_NEXT_IMPT(rn) STMT_START { \
150 while (JUMPABLE(rn)) \
151 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
152 rn = NEXTOPER(NEXTOPER(rn)); \
153 else if (OP(rn) == PLUS) \
155 else if (OP(rn) == IFMATCH) \
156 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
157 else rn += NEXT_OFF(rn); \
160 static void restore_pos(pTHX_ void *arg);
163 S_regcppush(pTHX_ I32 parenfloor)
165 int retval = PL_savestack_ix;
166 #define REGCP_PAREN_ELEMS 4
167 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
170 if (paren_elems_to_push < 0)
171 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
173 #define REGCP_OTHER_ELEMS 6
174 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
175 for (p = PL_regsize; p > parenfloor; p--) {
176 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
177 SSPUSHINT(PL_regendp[p]);
178 SSPUSHINT(PL_regstartp[p]);
179 SSPUSHPTR(PL_reg_start_tmp[p]);
182 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
183 SSPUSHINT(PL_regsize);
184 SSPUSHINT(*PL_reglastparen);
185 SSPUSHINT(*PL_reglastcloseparen);
186 SSPUSHPTR(PL_reginput);
187 #define REGCP_FRAME_ELEMS 2
188 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
189 * are needed for the regexp context stack bookkeeping. */
190 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
191 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
196 /* These are needed since we do not localize EVAL nodes: */
197 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
198 " Setting an EVAL scope, savestack=%"IVdf"\n", \
199 (IV)PL_savestack_ix)); cp = PL_savestack_ix
201 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
202 PerlIO_printf(Perl_debug_log, \
203 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
204 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
214 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
216 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
217 i = SSPOPINT; /* Parentheses elements to pop. */
218 input = (char *) SSPOPPTR;
219 *PL_reglastcloseparen = SSPOPINT;
220 *PL_reglastparen = SSPOPINT;
221 PL_regsize = SSPOPINT;
223 /* Now restore the parentheses context. */
224 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
225 i > 0; i -= REGCP_PAREN_ELEMS) {
226 paren = (U32)SSPOPINT;
227 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
228 PL_regstartp[paren] = SSPOPINT;
230 if (paren <= *PL_reglastparen)
231 PL_regendp[paren] = tmps;
233 PerlIO_printf(Perl_debug_log,
234 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
235 (UV)paren, (IV)PL_regstartp[paren],
236 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
237 (IV)PL_regendp[paren],
238 (paren > *PL_reglastparen ? "(no)" : ""));
242 if (*PL_reglastparen + 1 <= PL_regnpar) {
243 PerlIO_printf(Perl_debug_log,
244 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
245 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
249 /* It would seem that the similar code in regtry()
250 * already takes care of this, and in fact it is in
251 * a better location to since this code can #if 0-ed out
252 * but the code in regtry() is needed or otherwise tests
253 * requiring null fields (pat.t#187 and split.t#{13,14}
254 * (as of patchlevel 7877) will fail. Then again,
255 * this code seems to be necessary or otherwise
256 * building DynaLoader will fail:
257 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
259 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
260 if (paren > PL_regsize)
261 PL_regstartp[paren] = -1;
262 PL_regendp[paren] = -1;
269 S_regcp_set_to(pTHX_ I32 ss)
271 I32 tmp = PL_savestack_ix;
273 PL_savestack_ix = ss;
275 PL_savestack_ix = tmp;
279 typedef struct re_cc_state
283 struct re_cc_state *prev;
288 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
290 #define TRYPAREN(paren, n, input) { \
293 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
294 PL_regendp[paren] = input - PL_bostr; \
297 PL_regendp[paren] = -1; \
299 if (regmatch(next)) \
302 PL_regendp[paren] = -1; \
307 * pregexec and friends
311 - pregexec - match a regexp against a string
314 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
315 char *strbeg, I32 minend, SV *screamer, U32 nosave)
316 /* strend: pointer to null at end of string */
317 /* strbeg: real beginning of string */
318 /* minend: end of match must be >=minend after stringarg. */
319 /* nosave: For optimizations. */
322 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
323 nosave ? 0 : REXEC_COPY_STR);
327 S_cache_re(pTHX_ regexp *prog)
329 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
331 PL_regprogram = prog->program;
333 PL_regnpar = prog->nparens;
334 PL_regdata = prog->data;
339 * Need to implement the following flags for reg_anch:
341 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
343 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
344 * INTUIT_AUTORITATIVE_ML
345 * INTUIT_ONCE_NOML - Intuit can match in one location only.
348 * Another flag for this function: SECOND_TIME (so that float substrs
349 * with giant delta may be not rechecked).
352 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
354 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
355 Otherwise, only SvCUR(sv) is used to get strbeg. */
357 /* XXXX We assume that strpos is strbeg unless sv. */
359 /* XXXX Some places assume that there is a fixed substring.
360 An update may be needed if optimizer marks as "INTUITable"
361 RExen without fixed substrings. Similarly, it is assumed that
362 lengths of all the strings are no more than minlen, thus they
363 cannot come from lookahead.
364 (Or minlen should take into account lookahead.) */
366 /* A failure to find a constant substring means that there is no need to make
367 an expensive call to REx engine, thus we celebrate a failure. Similarly,
368 finding a substring too deep into the string means that less calls to
369 regtry() should be needed.
371 REx compiler's optimizer found 4 possible hints:
372 a) Anchored substring;
374 c) Whether we are anchored (beginning-of-line or \G);
375 d) First node (of those at offset 0) which may distingush positions;
376 We use a)b)d) and multiline-part of c), and try to find a position in the
377 string which does not contradict any of them.
380 /* Most of decisions we do here should have been done at compile time.
381 The nodes of the REx which we used for the search should have been
382 deleted from the finite automaton. */
385 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
386 char *strend, U32 flags, re_scream_pos_data *data)
388 register I32 start_shift = 0;
389 /* Should be nonnegative! */
390 register I32 end_shift = 0;
395 int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
397 register char *other_last = Nullch; /* other substr checked before this */
398 char *check_at = Nullch; /* check substr found at this pos */
400 char *i_strpos = strpos;
401 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
404 if (prog->reganch & ROPT_UTF8) {
405 DEBUG_r(PerlIO_printf(Perl_debug_log,
406 "UTF-8 regex...\n"));
407 PL_reg_flags |= RF_utf8;
411 char *s = PL_reg_match_utf8 ?
412 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
414 int len = PL_reg_match_utf8 ?
415 strlen(s) : strend - strpos;
418 if (PL_reg_match_utf8)
419 DEBUG_r(PerlIO_printf(Perl_debug_log,
420 "UTF-8 target...\n"));
421 PerlIO_printf(Perl_debug_log,
422 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
423 PL_colors[4],PL_colors[5],PL_colors[0],
426 (strlen(prog->precomp) > 60 ? "..." : ""),
428 (int)(len > 60 ? 60 : len),
430 (len > 60 ? "..." : "")
434 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
435 DEBUG_r(PerlIO_printf(Perl_debug_log,
436 "String too short... [re_intuit_start]\n"));
439 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
442 if (!prog->check_utf8 && prog->check_substr)
443 to_utf8_substr(prog);
444 check = prog->check_utf8;
446 if (!prog->check_substr && prog->check_utf8)
447 to_byte_substr(prog);
448 check = prog->check_substr;
450 if (check == &PL_sv_undef) {
451 DEBUG_r(PerlIO_printf(Perl_debug_log,
452 "Non-utf string cannot match utf check string\n"));
455 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
456 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
457 || ( (prog->reganch & ROPT_ANCH_BOL)
458 && !PL_multiline ) ); /* Check after \n? */
461 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
462 | ROPT_IMPLICIT)) /* not a real BOL */
463 /* SvCUR is not set on references: SvRV and SvPVX overlap */
465 && (strpos != strbeg)) {
466 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
469 if (prog->check_offset_min == prog->check_offset_max &&
470 !(prog->reganch & ROPT_CANY_SEEN)) {
471 /* Substring at constant offset from beg-of-str... */
474 s = HOP3c(strpos, prog->check_offset_min, strend);
476 slen = SvCUR(check); /* >= 1 */
478 if ( strend - s > slen || strend - s < slen - 1
479 || (strend - s == slen && strend[-1] != '\n')) {
480 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
483 /* Now should match s[0..slen-2] */
485 if (slen && (*SvPVX(check) != *s
487 && memNE(SvPVX(check), s, slen)))) {
489 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
493 else if (*SvPVX(check) != *s
494 || ((slen = SvCUR(check)) > 1
495 && memNE(SvPVX(check), s, slen)))
497 goto success_at_start;
500 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
502 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
503 end_shift = prog->minlen - start_shift -
504 CHR_SVLEN(check) + (SvTAIL(check) != 0);
506 I32 end = prog->check_offset_max + CHR_SVLEN(check)
507 - (SvTAIL(check) != 0);
508 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
510 if (end_shift < eshift)
514 else { /* Can match at random position */
517 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
518 /* Should be nonnegative! */
519 end_shift = prog->minlen - start_shift -
520 CHR_SVLEN(check) + (SvTAIL(check) != 0);
523 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
525 Perl_croak(aTHX_ "panic: end_shift");
529 /* Find a possible match in the region s..strend by looking for
530 the "check" substring in the region corrected by start/end_shift. */
531 if (flags & REXEC_SCREAM) {
532 I32 p = -1; /* Internal iterator of scream. */
533 I32 *pp = data ? data->scream_pos : &p;
535 if (PL_screamfirst[BmRARE(check)] >= 0
536 || ( BmRARE(check) == '\n'
537 && (BmPREVIOUS(check) == SvCUR(check) - 1)
539 s = screaminstr(sv, check,
540 start_shift + (s - strbeg), end_shift, pp, 0);
544 *data->scream_olds = s;
546 else if (prog->reganch & ROPT_CANY_SEEN)
547 s = fbm_instr((U8*)(s + start_shift),
548 (U8*)(strend - end_shift),
549 check, PL_multiline ? FBMrf_MULTILINE : 0);
551 s = fbm_instr(HOP3(s, start_shift, strend),
552 HOP3(strend, -end_shift, strbeg),
553 check, PL_multiline ? FBMrf_MULTILINE : 0);
555 /* Update the count-of-usability, remove useless subpatterns,
558 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
559 (s ? "Found" : "Did not find"),
560 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
562 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
564 PL_colors[1], (SvTAIL(check) ? "$" : ""),
565 (s ? " at offset " : "...\n") ) );
572 /* Finish the diagnostic message */
573 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
575 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
576 Start with the other substr.
577 XXXX no SCREAM optimization yet - and a very coarse implementation
578 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
579 *always* match. Probably should be marked during compile...
580 Probably it is right to do no SCREAM here...
583 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
584 /* Take into account the "other" substring. */
585 /* XXXX May be hopelessly wrong for UTF... */
588 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
591 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
595 t = s - prog->check_offset_max;
596 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
597 && (!(prog->reganch & ROPT_UTF8)
598 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
603 t = HOP3c(t, prog->anchored_offset, strend);
604 if (t < other_last) /* These positions already checked */
606 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
609 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
610 /* On end-of-str: see comment below. */
611 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
612 if (must == &PL_sv_undef) {
614 DEBUG_r(must = prog->anchored_utf8); /* for debug */
619 HOP3(HOP3(last1, prog->anchored_offset, strend)
620 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
622 PL_multiline ? FBMrf_MULTILINE : 0
624 DEBUG_r(PerlIO_printf(Perl_debug_log,
625 "%s anchored substr `%s%.*s%s'%s",
626 (s ? "Found" : "Contradicts"),
629 - (SvTAIL(must)!=0)),
631 PL_colors[1], (SvTAIL(must) ? "$" : "")));
633 if (last1 >= last2) {
634 DEBUG_r(PerlIO_printf(Perl_debug_log,
635 ", giving up...\n"));
638 DEBUG_r(PerlIO_printf(Perl_debug_log,
639 ", trying floating at offset %ld...\n",
640 (long)(HOP3c(s1, 1, strend) - i_strpos)));
641 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
642 s = HOP3c(last, 1, strend);
646 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
647 (long)(s - i_strpos)));
648 t = HOP3c(s, -prog->anchored_offset, strbeg);
649 other_last = HOP3c(s, 1, strend);
657 else { /* Take into account the floating substring. */
662 t = HOP3c(s, -start_shift, strbeg);
664 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
665 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
666 last = HOP3c(t, prog->float_max_offset, strend);
667 s = HOP3c(t, prog->float_min_offset, strend);
670 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
671 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
672 /* fbm_instr() takes into account exact value of end-of-str
673 if the check is SvTAIL(ed). Since false positives are OK,
674 and end-of-str is not later than strend we are OK. */
675 if (must == &PL_sv_undef) {
677 DEBUG_r(must = prog->float_utf8); /* for debug message */
680 s = fbm_instr((unsigned char*)s,
681 (unsigned char*)last + SvCUR(must)
683 must, PL_multiline ? FBMrf_MULTILINE : 0);
684 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
685 (s ? "Found" : "Contradicts"),
687 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
689 PL_colors[1], (SvTAIL(must) ? "$" : "")));
692 DEBUG_r(PerlIO_printf(Perl_debug_log,
693 ", giving up...\n"));
696 DEBUG_r(PerlIO_printf(Perl_debug_log,
697 ", trying anchored starting at offset %ld...\n",
698 (long)(s1 + 1 - i_strpos)));
700 s = HOP3c(t, 1, strend);
704 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
705 (long)(s - i_strpos)));
706 other_last = s; /* Fix this later. --Hugo */
715 t = s - prog->check_offset_max;
716 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
717 && (!(prog->reganch & ROPT_UTF8)
718 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
720 /* Fixed substring is found far enough so that the match
721 cannot start at strpos. */
723 if (ml_anch && t[-1] != '\n') {
724 /* Eventually fbm_*() should handle this, but often
725 anchored_offset is not 0, so this check will not be wasted. */
726 /* XXXX In the code below we prefer to look for "^" even in
727 presence of anchored substrings. And we search even
728 beyond the found float position. These pessimizations
729 are historical artefacts only. */
731 while (t < strend - prog->minlen) {
733 if (t < check_at - prog->check_offset_min) {
734 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
735 /* Since we moved from the found position,
736 we definitely contradict the found anchored
737 substr. Due to the above check we do not
738 contradict "check" substr.
739 Thus we can arrive here only if check substr
740 is float. Redo checking for "other"=="fixed".
743 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
744 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
745 goto do_other_anchored;
747 /* We don't contradict the found floating substring. */
748 /* XXXX Why not check for STCLASS? */
750 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
751 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
754 /* Position contradicts check-string */
755 /* XXXX probably better to look for check-string
756 than for "\n", so one should lower the limit for t? */
757 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
758 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
759 other_last = strpos = s = t + 1;
764 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
765 PL_colors[0],PL_colors[1]));
769 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
770 PL_colors[0],PL_colors[1]));
774 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
777 /* The found string does not prohibit matching at strpos,
778 - no optimization of calling REx engine can be performed,
779 unless it was an MBOL and we are not after MBOL,
780 or a future STCLASS check will fail this. */
782 /* Even in this situation we may use MBOL flag if strpos is offset
783 wrt the start of the string. */
784 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
785 && (strpos != strbeg) && strpos[-1] != '\n'
786 /* May be due to an implicit anchor of m{.*foo} */
787 && !(prog->reganch & ROPT_IMPLICIT))
792 DEBUG_r( if (ml_anch)
793 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
794 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
797 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
799 prog->check_utf8 /* Could be deleted already */
800 && --BmUSEFUL(prog->check_utf8) < 0
801 && (prog->check_utf8 == prog->float_utf8)
803 prog->check_substr /* Could be deleted already */
804 && --BmUSEFUL(prog->check_substr) < 0
805 && (prog->check_substr == prog->float_substr)
808 /* If flags & SOMETHING - do not do it many times on the same match */
809 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
810 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
811 if (do_utf8 ? prog->check_substr : prog->check_utf8)
812 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
813 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
814 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
815 check = Nullsv; /* abort */
817 /* XXXX This is a remnant of the old implementation. It
818 looks wasteful, since now INTUIT can use many
820 prog->reganch &= ~RE_USE_INTUIT;
827 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
828 if (prog->regstclass) {
829 /* minlen == 0 is possible if regstclass is \b or \B,
830 and the fixed substr is ''$.
831 Since minlen is already taken into account, s+1 is before strend;
832 accidentally, minlen >= 1 guaranties no false positives at s + 1
833 even for \b or \B. But (minlen? 1 : 0) below assumes that
834 regstclass does not come from lookahead... */
835 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
836 This leaves EXACTF only, which is dealt with in find_byclass(). */
837 U8* str = (U8*)STRING(prog->regstclass);
838 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
839 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
841 char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
842 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
843 : (prog->float_substr || prog->float_utf8
844 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
847 char *startpos = strbeg;
850 if (prog->reganch & ROPT_UTF8) {
851 PL_regdata = prog->data;
854 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
859 if (endpos == strend) {
860 DEBUG_r( PerlIO_printf(Perl_debug_log,
861 "Could not match STCLASS...\n") );
864 DEBUG_r( PerlIO_printf(Perl_debug_log,
865 "This position contradicts STCLASS...\n") );
866 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
868 /* Contradict one of substrings */
869 if (prog->anchored_substr || prog->anchored_utf8) {
870 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
871 DEBUG_r( what = "anchored" );
873 s = HOP3c(t, 1, strend);
874 if (s + start_shift + end_shift > strend) {
875 /* XXXX Should be taken into account earlier? */
876 DEBUG_r( PerlIO_printf(Perl_debug_log,
877 "Could not match STCLASS...\n") );
882 DEBUG_r( PerlIO_printf(Perl_debug_log,
883 "Looking for %s substr starting at offset %ld...\n",
884 what, (long)(s + start_shift - i_strpos)) );
887 /* Have both, check_string is floating */
888 if (t + start_shift >= check_at) /* Contradicts floating=check */
889 goto retry_floating_check;
890 /* Recheck anchored substring, but not floating... */
894 DEBUG_r( PerlIO_printf(Perl_debug_log,
895 "Looking for anchored substr starting at offset %ld...\n",
896 (long)(other_last - i_strpos)) );
897 goto do_other_anchored;
899 /* Another way we could have checked stclass at the
900 current position only: */
905 DEBUG_r( PerlIO_printf(Perl_debug_log,
906 "Looking for /%s^%s/m starting at offset %ld...\n",
907 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
910 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
912 /* Check is floating subtring. */
913 retry_floating_check:
914 t = check_at - start_shift;
915 DEBUG_r( what = "floating" );
916 goto hop_and_restart;
919 DEBUG_r(PerlIO_printf(Perl_debug_log,
920 "By STCLASS: moving %ld --> %ld\n",
921 (long)(t - i_strpos), (long)(s - i_strpos))
925 DEBUG_r(PerlIO_printf(Perl_debug_log,
926 "Does not contradict STCLASS...\n");
931 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
932 PL_colors[4], (check ? "Guessed" : "Giving up"),
933 PL_colors[5], (long)(s - i_strpos)) );
936 fail_finish: /* Substring not found */
937 if (prog->check_substr || prog->check_utf8) /* could be removed already */
938 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
940 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
941 PL_colors[4],PL_colors[5]));
945 /* We know what class REx starts with. Try to find this position... */
947 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
949 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
955 register I32 tmp = 1; /* Scratch variable? */
956 register bool do_utf8 = PL_reg_match_utf8;
958 /* We know what class it must start with. */
962 STRLEN skip = do_utf8 ? UTF8SKIP(s) : 1;
965 reginclass(c, (U8*)s, 0, do_utf8) :
966 REGINCLASS(c, (U8*)s) ||
967 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
968 /* The assignment of 2 is intentional:
969 * for the sharp s, the skip is 2. */
970 (skip = SHARP_S_SKIP)
972 if (tmp && (norun || regtry(prog, s)))
984 if (tmp && (norun || regtry(prog, s)))
996 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
997 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
999 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1000 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1002 c1 = utf8_to_uvchr(tmpbuf1, 0);
1003 c2 = utf8_to_uvchr(tmpbuf2, 0);
1014 c2 = PL_fold_locale[c1];
1016 e = do_utf8 ? s + ln : strend - ln;
1019 e = s; /* Due to minlen logic of intuit() */
1021 /* The idea in the EXACTF* cases is to first find the
1022 * first character of the EXACTF* node and then, if
1023 * necessary, case-insensitively compare the full
1024 * text of the node. The c1 and c2 are the first
1025 * characters (though in Unicode it gets a bit
1026 * more complicated because there are more cases
1027 * than just upper and lower: one needs to use
1028 * the so-called folding case for case-insensitive
1029 * matching (called "loose matching" in Unicode).
1030 * ibcmp_utf8() will do just that. */
1034 U8 tmpbuf [UTF8_MAXLEN+1];
1035 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1036 STRLEN len, foldlen;
1040 c = utf8_to_uvchr((U8*)s, &len);
1043 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1044 m, (char **)0, ln, UTF))
1045 && (norun || regtry(prog, s)) )
1048 uvchr_to_utf8(tmpbuf, c);
1049 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1051 && (f == c1 || f == c2)
1052 && (ln == foldlen ||
1053 !ibcmp_utf8((char *) foldbuf,
1054 (char **)0, foldlen, do_utf8,
1056 (char **)0, ln, UTF))
1057 && (norun || regtry(prog, s)) )
1065 c = utf8_to_uvchr((U8*)s, &len);
1067 /* Handle some of the three Greek sigmas cases.
1068 * Note that not all the possible combinations
1069 * are handled here: some of them are handled
1070 * by the standard folding rules, and some of
1071 * them (the character class or ANYOF cases)
1072 * are handled during compiletime in
1073 * regexec.c:S_regclass(). */
1074 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1075 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1076 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1078 if ( (c == c1 || c == c2)
1080 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1081 m, (char **)0, ln, UTF))
1082 && (norun || regtry(prog, s)) )
1085 uvchr_to_utf8(tmpbuf, c);
1086 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1088 && (f == c1 || f == c2)
1089 && (ln == foldlen ||
1090 !ibcmp_utf8((char *) foldbuf,
1091 (char **)0, foldlen, do_utf8,
1093 (char **)0, ln, UTF))
1094 && (norun || regtry(prog, s)) )
1105 && (ln == 1 || !(OP(c) == EXACTF
1107 : ibcmp_locale(s, m, ln)))
1108 && (norun || regtry(prog, s)) )
1114 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1115 && (ln == 1 || !(OP(c) == EXACTF
1117 : ibcmp_locale(s, m, ln)))
1118 && (norun || regtry(prog, s)) )
1125 PL_reg_flags |= RF_tainted;
1132 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1134 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1136 tmp = ((OP(c) == BOUND ?
1137 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1138 LOAD_UTF8_CHARCLASS(alnum,"a");
1139 while (s < strend) {
1140 if (tmp == !(OP(c) == BOUND ?
1141 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1142 isALNUM_LC_utf8((U8*)s)))
1145 if ((norun || regtry(prog, s)))
1152 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1153 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1154 while (s < strend) {
1156 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1158 if ((norun || regtry(prog, s)))
1164 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1168 PL_reg_flags |= RF_tainted;
1175 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1177 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1179 tmp = ((OP(c) == NBOUND ?
1180 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1181 LOAD_UTF8_CHARCLASS(alnum,"a");
1182 while (s < strend) {
1183 if (tmp == !(OP(c) == NBOUND ?
1184 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1185 isALNUM_LC_utf8((U8*)s)))
1187 else if ((norun || regtry(prog, s)))
1193 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1194 tmp = ((OP(c) == NBOUND ?
1195 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1196 while (s < strend) {
1198 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1200 else if ((norun || regtry(prog, s)))
1205 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1210 LOAD_UTF8_CHARCLASS(alnum,"a");
1211 while (s < strend) {
1212 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1213 if (tmp && (norun || regtry(prog, s)))
1224 while (s < strend) {
1226 if (tmp && (norun || regtry(prog, s)))
1238 PL_reg_flags |= RF_tainted;
1240 while (s < strend) {
1241 if (isALNUM_LC_utf8((U8*)s)) {
1242 if (tmp && (norun || regtry(prog, s)))
1253 while (s < strend) {
1254 if (isALNUM_LC(*s)) {
1255 if (tmp && (norun || regtry(prog, s)))
1268 LOAD_UTF8_CHARCLASS(alnum,"a");
1269 while (s < strend) {
1270 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1271 if (tmp && (norun || regtry(prog, s)))
1282 while (s < strend) {
1284 if (tmp && (norun || regtry(prog, s)))
1296 PL_reg_flags |= RF_tainted;
1298 while (s < strend) {
1299 if (!isALNUM_LC_utf8((U8*)s)) {
1300 if (tmp && (norun || regtry(prog, s)))
1311 while (s < strend) {
1312 if (!isALNUM_LC(*s)) {
1313 if (tmp && (norun || regtry(prog, s)))
1326 LOAD_UTF8_CHARCLASS(space," ");
1327 while (s < strend) {
1328 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1329 if (tmp && (norun || regtry(prog, s)))
1340 while (s < strend) {
1342 if (tmp && (norun || regtry(prog, s)))
1354 PL_reg_flags |= RF_tainted;
1356 while (s < strend) {
1357 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1358 if (tmp && (norun || regtry(prog, s)))
1369 while (s < strend) {
1370 if (isSPACE_LC(*s)) {
1371 if (tmp && (norun || regtry(prog, s)))
1384 LOAD_UTF8_CHARCLASS(space," ");
1385 while (s < strend) {
1386 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1387 if (tmp && (norun || regtry(prog, s)))
1398 while (s < strend) {
1400 if (tmp && (norun || regtry(prog, s)))
1412 PL_reg_flags |= RF_tainted;
1414 while (s < strend) {
1415 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1416 if (tmp && (norun || regtry(prog, s)))
1427 while (s < strend) {
1428 if (!isSPACE_LC(*s)) {
1429 if (tmp && (norun || regtry(prog, s)))
1442 LOAD_UTF8_CHARCLASS(digit,"0");
1443 while (s < strend) {
1444 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1445 if (tmp && (norun || regtry(prog, s)))
1456 while (s < strend) {
1458 if (tmp && (norun || regtry(prog, s)))
1470 PL_reg_flags |= RF_tainted;
1472 while (s < strend) {
1473 if (isDIGIT_LC_utf8((U8*)s)) {
1474 if (tmp && (norun || regtry(prog, s)))
1485 while (s < strend) {
1486 if (isDIGIT_LC(*s)) {
1487 if (tmp && (norun || regtry(prog, s)))
1500 LOAD_UTF8_CHARCLASS(digit,"0");
1501 while (s < strend) {
1502 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1503 if (tmp && (norun || regtry(prog, s)))
1514 while (s < strend) {
1516 if (tmp && (norun || regtry(prog, s)))
1528 PL_reg_flags |= RF_tainted;
1530 while (s < strend) {
1531 if (!isDIGIT_LC_utf8((U8*)s)) {
1532 if (tmp && (norun || regtry(prog, s)))
1543 while (s < strend) {
1544 if (!isDIGIT_LC(*s)) {
1545 if (tmp && (norun || regtry(prog, s)))
1557 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1566 - regexec_flags - match a regexp against a string
1569 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1570 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1571 /* strend: pointer to null at end of string */
1572 /* strbeg: real beginning of string */
1573 /* minend: end of match must be >=minend after stringarg. */
1574 /* data: May be used for some additional optimizations. */
1575 /* nosave: For optimizations. */
1578 register regnode *c;
1579 register char *startpos = stringarg;
1580 I32 minlen; /* must match at least this many chars */
1581 I32 dontbother = 0; /* how many characters not to try at end */
1582 /* I32 start_shift = 0; */ /* Offset of the start to find
1583 constant substr. */ /* CC */
1584 I32 end_shift = 0; /* Same for the end. */ /* CC */
1585 I32 scream_pos = -1; /* Internal iterator of scream. */
1587 SV* oreplsv = GvSV(PL_replgv);
1588 bool do_utf8 = DO_UTF8(sv);
1590 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1591 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1598 PL_regnarrate = DEBUG_r_TEST;
1601 /* Be paranoid... */
1602 if (prog == NULL || startpos == NULL) {
1603 Perl_croak(aTHX_ "NULL regexp parameter");
1607 minlen = prog->minlen;
1608 if (strend - startpos < minlen) {
1609 DEBUG_r(PerlIO_printf(Perl_debug_log,
1610 "String too short [regexec_flags]...\n"));
1614 /* Check validity of program. */
1615 if (UCHARAT(prog->program) != REG_MAGIC) {
1616 Perl_croak(aTHX_ "corrupted regexp program");
1620 PL_reg_eval_set = 0;
1623 if (prog->reganch & ROPT_UTF8)
1624 PL_reg_flags |= RF_utf8;
1626 /* Mark beginning of line for ^ and lookbehind. */
1627 PL_regbol = startpos;
1631 /* Mark end of line for $ (and such) */
1634 /* see how far we have to get to not match where we matched before */
1635 PL_regtill = startpos+minend;
1637 /* We start without call_cc context. */
1640 /* If there is a "must appear" string, look for it. */
1643 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1646 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1647 PL_reg_ganch = startpos;
1648 else if (sv && SvTYPE(sv) >= SVt_PVMG
1650 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1651 && mg->mg_len >= 0) {
1652 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1653 if (prog->reganch & ROPT_ANCH_GPOS) {
1654 if (s > PL_reg_ganch)
1659 else /* pos() not defined */
1660 PL_reg_ganch = strbeg;
1663 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1664 re_scream_pos_data d;
1666 d.scream_olds = &scream_olds;
1667 d.scream_pos = &scream_pos;
1668 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1670 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1671 goto phooey; /* not present */
1677 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1678 UNI_DISPLAY_REGEX) :
1680 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1681 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1682 UNI_DISPLAY_REGEX) : startpos;
1683 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1686 PerlIO_printf(Perl_debug_log,
1687 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1688 PL_colors[4],PL_colors[5],PL_colors[0],
1691 len0 > 60 ? "..." : "",
1693 (int)(len1 > 60 ? 60 : len1),
1695 (len1 > 60 ? "..." : "")
1699 /* Simplest case: anchored match need be tried only once. */
1700 /* [unless only anchor is BOL and multiline is set] */
1701 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1702 if (s == startpos && regtry(prog, startpos))
1704 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1705 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1710 dontbother = minlen - 1;
1711 end = HOP3c(strend, -dontbother, strbeg) - 1;
1712 /* for multiline we only have to try after newlines */
1713 if (prog->check_substr || prog->check_utf8) {
1717 if (regtry(prog, s))
1722 if (prog->reganch & RE_USE_INTUIT) {
1723 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1734 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1735 if (regtry(prog, s))
1742 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1743 if (regtry(prog, PL_reg_ganch))
1748 /* Messy cases: unanchored match. */
1749 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1750 /* we have /x+whatever/ */
1751 /* it must be a one character string (XXXX Except UTF?) */
1756 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1757 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1758 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1761 while (s < strend) {
1763 DEBUG_r( did_match = 1 );
1764 if (regtry(prog, s)) goto got_it;
1766 while (s < strend && *s == ch)
1773 while (s < strend) {
1775 DEBUG_r( did_match = 1 );
1776 if (regtry(prog, s)) goto got_it;
1778 while (s < strend && *s == ch)
1784 DEBUG_r(if (!did_match)
1785 PerlIO_printf(Perl_debug_log,
1786 "Did not find anchored character...\n")
1790 else if (prog->anchored_substr != Nullsv
1791 || prog->anchored_utf8 != Nullsv
1792 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1793 && prog->float_max_offset < strend - s)) {
1798 char *last1; /* Last position checked before */
1802 if (prog->anchored_substr || prog->anchored_utf8) {
1803 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1804 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1805 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1806 back_max = back_min = prog->anchored_offset;
1808 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1809 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1810 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1811 back_max = prog->float_max_offset;
1812 back_min = prog->float_min_offset;
1814 if (must == &PL_sv_undef)
1815 /* could not downgrade utf8 check substring, so must fail */
1818 last = HOP3c(strend, /* Cannot start after this */
1819 -(I32)(CHR_SVLEN(must)
1820 - (SvTAIL(must) != 0) + back_min), strbeg);
1823 last1 = HOPc(s, -1);
1825 last1 = s - 1; /* bogus */
1827 /* XXXX check_substr already used to find `s', can optimize if
1828 check_substr==must. */
1830 dontbother = end_shift;
1831 strend = HOPc(strend, -dontbother);
1832 while ( (s <= last) &&
1833 ((flags & REXEC_SCREAM)
1834 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1835 end_shift, &scream_pos, 0))
1836 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1837 (unsigned char*)strend, must,
1838 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1839 DEBUG_r( did_match = 1 );
1840 if (HOPc(s, -back_max) > last1) {
1841 last1 = HOPc(s, -back_min);
1842 s = HOPc(s, -back_max);
1845 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1847 last1 = HOPc(s, -back_min);
1851 while (s <= last1) {
1852 if (regtry(prog, s))
1858 while (s <= last1) {
1859 if (regtry(prog, s))
1865 DEBUG_r(if (!did_match)
1866 PerlIO_printf(Perl_debug_log,
1867 "Did not find %s substr `%s%.*s%s'%s...\n",
1868 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1869 ? "anchored" : "floating"),
1871 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1873 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1877 else if ((c = prog->regstclass)) {
1878 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1879 /* don't bother with what can't match */
1880 strend = HOPc(strend, -(minlen - 1));
1882 SV *prop = sv_newmortal();
1890 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1891 UNI_DISPLAY_REGEX) :
1893 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1895 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1896 len1 = UTF ? SvCUR(dsv1) : strend - s;
1897 PerlIO_printf(Perl_debug_log,
1898 "Matching stclass `%*.*s' against `%*.*s'\n",
1902 if (find_byclass(prog, c, s, strend, startpos, 0))
1904 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1908 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1913 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1914 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1915 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1917 if (flags & REXEC_SCREAM) {
1918 last = screaminstr(sv, float_real, s - strbeg,
1919 end_shift, &scream_pos, 1); /* last one */
1921 last = scream_olds; /* Only one occurrence. */
1925 char *little = SvPV(float_real, len);
1927 if (SvTAIL(float_real)) {
1928 if (memEQ(strend - len + 1, little, len - 1))
1929 last = strend - len + 1;
1930 else if (!PL_multiline)
1931 last = memEQ(strend - len, little, len)
1932 ? strend - len : Nullch;
1938 last = rninstr(s, strend, little, little + len);
1940 last = strend; /* matching `$' */
1944 DEBUG_r(PerlIO_printf(Perl_debug_log,
1945 "%sCan't trim the tail, match fails (should not happen)%s\n",
1946 PL_colors[4],PL_colors[5]));
1947 goto phooey; /* Should not happen! */
1949 dontbother = strend - last + prog->float_min_offset;
1951 if (minlen && (dontbother < minlen))
1952 dontbother = minlen - 1;
1953 strend -= dontbother; /* this one's always in bytes! */
1954 /* We don't know much -- general case. */
1957 if (regtry(prog, s))
1966 if (regtry(prog, s))
1968 } while (s++ < strend);
1976 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1978 if (PL_reg_eval_set) {
1979 /* Preserve the current value of $^R */
1980 if (oreplsv != GvSV(PL_replgv))
1981 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1982 restored, the value remains
1984 restore_pos(aTHX_ 0);
1987 /* make sure $`, $&, $', and $digit will work later */
1988 if ( !(flags & REXEC_NOT_FIRST) ) {
1989 if (RX_MATCH_COPIED(prog)) {
1990 Safefree(prog->subbeg);
1991 RX_MATCH_COPIED_off(prog);
1993 if (flags & REXEC_COPY_STR) {
1994 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1996 s = savepvn(strbeg, i);
1999 RX_MATCH_COPIED_on(prog);
2002 prog->subbeg = strbeg;
2003 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2010 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2011 PL_colors[4],PL_colors[5]));
2012 if (PL_reg_eval_set)
2013 restore_pos(aTHX_ 0);
2018 - regtry - try match at specific point
2020 STATIC I32 /* 0 failure, 1 success */
2021 S_regtry(pTHX_ regexp *prog, char *startpos)
2029 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2031 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2034 PL_reg_eval_set = RS_init;
2036 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2037 (IV)(PL_stack_sp - PL_stack_base));
2039 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2040 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2041 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2043 /* Apparently this is not needed, judging by wantarray. */
2044 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2045 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2048 /* Make $_ available to executed code. */
2049 if (PL_reg_sv != DEFSV) {
2050 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2055 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2056 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2057 /* prepare for quick setting of pos */
2058 sv_magic(PL_reg_sv, (SV*)0,
2059 PERL_MAGIC_regex_global, Nullch, 0);
2060 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2064 PL_reg_oldpos = mg->mg_len;
2065 SAVEDESTRUCTOR_X(restore_pos, 0);
2067 if (!PL_reg_curpm) {
2068 Newz(22,PL_reg_curpm, 1, PMOP);
2071 SV* repointer = newSViv(0);
2072 /* so we know which PL_regex_padav element is PL_reg_curpm */
2073 SvFLAGS(repointer) |= SVf_BREAK;
2074 av_push(PL_regex_padav,repointer);
2075 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2076 PL_regex_pad = AvARRAY(PL_regex_padav);
2080 PM_SETRE(PL_reg_curpm, prog);
2081 PL_reg_oldcurpm = PL_curpm;
2082 PL_curpm = PL_reg_curpm;
2083 if (RX_MATCH_COPIED(prog)) {
2084 /* Here is a serious problem: we cannot rewrite subbeg,
2085 since it may be needed if this match fails. Thus
2086 $` inside (?{}) could fail... */
2087 PL_reg_oldsaved = prog->subbeg;
2088 PL_reg_oldsavedlen = prog->sublen;
2089 RX_MATCH_COPIED_off(prog);
2092 PL_reg_oldsaved = Nullch;
2093 prog->subbeg = PL_bostr;
2094 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2096 prog->startp[0] = startpos - PL_bostr;
2097 PL_reginput = startpos;
2098 PL_regstartp = prog->startp;
2099 PL_regendp = prog->endp;
2100 PL_reglastparen = &prog->lastparen;
2101 PL_reglastcloseparen = &prog->lastcloseparen;
2102 prog->lastparen = 0;
2104 DEBUG_r(PL_reg_starttry = startpos);
2105 if (PL_reg_start_tmpl <= prog->nparens) {
2106 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2107 if(PL_reg_start_tmp)
2108 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2110 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2113 /* XXXX What this code is doing here?!!! There should be no need
2114 to do this again and again, PL_reglastparen should take care of
2117 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2118 * Actually, the code in regcppop() (which Ilya may be meaning by
2119 * PL_reglastparen), is not needed at all by the test suite
2120 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2121 * enough, for building DynaLoader, or otherwise this
2122 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2123 * will happen. Meanwhile, this code *is* needed for the
2124 * above-mentioned test suite tests to succeed. The common theme
2125 * on those tests seems to be returning null fields from matches.
2130 if (prog->nparens) {
2131 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2138 if (regmatch(prog->program + 1)) {
2139 prog->endp[0] = PL_reginput - PL_bostr;
2142 REGCP_UNWIND(lastcp);
2146 #define RE_UNWIND_BRANCH 1
2147 #define RE_UNWIND_BRANCHJ 2
2151 typedef struct { /* XX: makes sense to enlarge it... */
2155 } re_unwind_generic_t;
2168 } re_unwind_branch_t;
2170 typedef union re_unwind_t {
2172 re_unwind_generic_t generic;
2173 re_unwind_branch_t branch;
2176 #define sayYES goto yes
2177 #define sayNO goto no
2178 #define sayNO_ANYOF goto no_anyof
2179 #define sayYES_FINAL goto yes_final
2180 #define sayYES_LOUD goto yes_loud
2181 #define sayNO_FINAL goto no_final
2182 #define sayNO_SILENT goto do_no
2183 #define saySAME(x) if (x) goto yes; else goto no
2185 #define REPORT_CODE_OFF 24
2188 - regmatch - main matching routine
2190 * Conceptually the strategy is simple: check to see whether the current
2191 * node matches, call self recursively to see whether the rest matches,
2192 * and then act accordingly. In practice we make some effort to avoid
2193 * recursion, in particular by going through "ordinary" nodes (that don't
2194 * need to know whether the rest of the match failed) by a loop instead of
2197 /* [lwall] I've hoisted the register declarations to the outer block in order to
2198 * maybe save a little bit of pushing and popping on the stack. It also takes
2199 * advantage of machines that use a register save mask on subroutine entry.
2201 STATIC I32 /* 0 failure, 1 success */
2202 S_regmatch(pTHX_ regnode *prog)
2204 register regnode *scan; /* Current node. */
2205 regnode *next; /* Next node. */
2206 regnode *inner; /* Next node in internal branch. */
2207 register I32 nextchr; /* renamed nextchr - nextchar colides with
2208 function of same name */
2209 register I32 n; /* no or next */
2210 register I32 ln = 0; /* len or last */
2211 register char *s = Nullch; /* operand or save */
2212 register char *locinput = PL_reginput;
2213 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2214 int minmod = 0, sw = 0, logical = 0;
2217 I32 firstcp = PL_savestack_ix;
2219 register bool do_utf8 = PL_reg_match_utf8;
2221 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2222 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2223 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2230 /* Note that nextchr is a byte even in UTF */
2231 nextchr = UCHARAT(locinput);
2233 while (scan != NULL) {
2236 SV *prop = sv_newmortal();
2237 int docolor = *PL_colors[0];
2238 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2239 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2240 /* The part of the string before starttry has one color
2241 (pref0_len chars), between starttry and current
2242 position another one (pref_len - pref0_len chars),
2243 after the current position the third one.
2244 We assume that pref0_len <= pref_len, otherwise we
2245 decrease pref0_len. */
2246 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2247 ? (5 + taill) - l : locinput - PL_bostr;
2250 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2252 pref0_len = pref_len - (locinput - PL_reg_starttry);
2253 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2254 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2255 ? (5 + taill) - pref_len : PL_regeol - locinput);
2256 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2260 if (pref0_len > pref_len)
2261 pref0_len = pref_len;
2262 regprop(prop, scan);
2266 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2267 pref0_len, 60, UNI_DISPLAY_REGEX) :
2268 locinput - pref_len;
2269 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2270 char *s1 = do_utf8 ?
2271 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2272 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2273 locinput - pref_len + pref0_len;
2274 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2275 char *s2 = do_utf8 ?
2276 pv_uni_display(dsv2, (U8*)locinput,
2277 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2279 int len2 = do_utf8 ? strlen(s2) : l;
2280 PerlIO_printf(Perl_debug_log,
2281 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2282 (IV)(locinput - PL_bostr),
2289 (docolor ? "" : "> <"),
2293 15 - l - pref_len + 1,
2295 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2300 next = scan + NEXT_OFF(scan);
2306 if (locinput == PL_bostr || (PL_multiline &&
2307 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2309 /* regtill = regbol; */
2314 if (locinput == PL_bostr ||
2315 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2321 if (locinput == PL_bostr)
2325 if (locinput == PL_reg_ganch)
2335 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2340 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2342 if (PL_regeol - locinput > 1)
2346 if (PL_regeol != locinput)
2350 if (!nextchr && locinput >= PL_regeol)
2353 locinput += PL_utf8skip[nextchr];
2354 if (locinput > PL_regeol)
2356 nextchr = UCHARAT(locinput);
2359 nextchr = UCHARAT(++locinput);
2362 if (!nextchr && locinput >= PL_regeol)
2364 nextchr = UCHARAT(++locinput);
2367 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2370 locinput += PL_utf8skip[nextchr];
2371 if (locinput > PL_regeol)
2373 nextchr = UCHARAT(locinput);
2376 nextchr = UCHARAT(++locinput);
2381 if (do_utf8 != (UTF!=0)) {
2382 /* The target and the pattern have differing utf8ness. */
2388 /* The target is utf8, the pattern is not utf8. */
2392 if (NATIVE_TO_UNI(*(U8*)s) !=
2393 utf8_to_uvuni((U8*)l, &ulen))
2400 /* The target is not utf8, the pattern is utf8. */
2404 if (NATIVE_TO_UNI(*((U8*)l)) !=
2405 utf8_to_uvuni((U8*)s, &ulen))
2412 nextchr = UCHARAT(locinput);
2415 /* The target and the pattern have the same utf8ness. */
2416 /* Inline the first character, for speed. */
2417 if (UCHARAT(s) != nextchr)
2419 if (PL_regeol - locinput < ln)
2421 if (ln > 1 && memNE(s, locinput, ln))
2424 nextchr = UCHARAT(locinput);
2427 PL_reg_flags |= RF_tainted;
2433 if (do_utf8 || UTF) {
2434 /* Either target or the pattern are utf8. */
2436 char *e = PL_regeol;
2438 if (ibcmp_utf8(s, 0, ln, UTF,
2439 l, &e, 0, do_utf8)) {
2440 /* One more case for the sharp s:
2441 * pack("U0U*", 0xDF) =~ /ss/i,
2442 * the 0xC3 0x9F are the UTF-8
2443 * byte sequence for the U+00DF. */
2445 toLOWER(s[0]) == 's' &&
2447 toLOWER(s[1]) == 's' &&
2454 nextchr = UCHARAT(locinput);
2458 /* Neither the target and the pattern are utf8. */
2460 /* Inline the first character, for speed. */
2461 if (UCHARAT(s) != nextchr &&
2462 UCHARAT(s) != ((OP(scan) == EXACTF)
2463 ? PL_fold : PL_fold_locale)[nextchr])
2465 if (PL_regeol - locinput < ln)
2467 if (ln > 1 && (OP(scan) == EXACTF
2468 ? ibcmp(s, locinput, ln)
2469 : ibcmp_locale(s, locinput, ln)))
2472 nextchr = UCHARAT(locinput);
2476 STRLEN inclasslen = PL_regeol - locinput;
2478 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2480 if (locinput >= PL_regeol)
2482 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2483 nextchr = UCHARAT(locinput);
2488 nextchr = UCHARAT(locinput);
2489 if (!REGINCLASS(scan, (U8*)locinput))
2491 if (!nextchr && locinput >= PL_regeol)
2493 nextchr = UCHARAT(++locinput);
2497 /* If we might have the case of the German sharp s
2498 * in a casefolding Unicode character class. */
2500 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2501 locinput += SHARP_S_SKIP;
2502 nextchr = UCHARAT(locinput);
2508 PL_reg_flags |= RF_tainted;
2514 LOAD_UTF8_CHARCLASS(alnum,"a");
2515 if (!(OP(scan) == ALNUM
2516 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2517 : isALNUM_LC_utf8((U8*)locinput)))
2521 locinput += PL_utf8skip[nextchr];
2522 nextchr = UCHARAT(locinput);
2525 if (!(OP(scan) == ALNUM
2526 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2528 nextchr = UCHARAT(++locinput);
2531 PL_reg_flags |= RF_tainted;
2534 if (!nextchr && locinput >= PL_regeol)
2537 LOAD_UTF8_CHARCLASS(alnum,"a");
2538 if (OP(scan) == NALNUM
2539 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2540 : isALNUM_LC_utf8((U8*)locinput))
2544 locinput += PL_utf8skip[nextchr];
2545 nextchr = UCHARAT(locinput);
2548 if (OP(scan) == NALNUM
2549 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2551 nextchr = UCHARAT(++locinput);
2555 PL_reg_flags |= RF_tainted;
2559 /* was last char in word? */
2561 if (locinput == PL_bostr)
2564 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2566 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2568 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2569 ln = isALNUM_uni(ln);
2570 LOAD_UTF8_CHARCLASS(alnum,"a");
2571 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2574 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2575 n = isALNUM_LC_utf8((U8*)locinput);
2579 ln = (locinput != PL_bostr) ?
2580 UCHARAT(locinput - 1) : '\n';
2581 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2583 n = isALNUM(nextchr);
2586 ln = isALNUM_LC(ln);
2587 n = isALNUM_LC(nextchr);
2590 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2591 OP(scan) == BOUNDL))
2595 PL_reg_flags |= RF_tainted;
2601 if (UTF8_IS_CONTINUED(nextchr)) {
2602 LOAD_UTF8_CHARCLASS(space," ");
2603 if (!(OP(scan) == SPACE
2604 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2605 : isSPACE_LC_utf8((U8*)locinput)))
2609 locinput += PL_utf8skip[nextchr];
2610 nextchr = UCHARAT(locinput);
2613 if (!(OP(scan) == SPACE
2614 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2616 nextchr = UCHARAT(++locinput);
2619 if (!(OP(scan) == SPACE
2620 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2622 nextchr = UCHARAT(++locinput);
2626 PL_reg_flags |= RF_tainted;
2629 if (!nextchr && locinput >= PL_regeol)
2632 LOAD_UTF8_CHARCLASS(space," ");
2633 if (OP(scan) == NSPACE
2634 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2635 : isSPACE_LC_utf8((U8*)locinput))
2639 locinput += PL_utf8skip[nextchr];
2640 nextchr = UCHARAT(locinput);
2643 if (OP(scan) == NSPACE
2644 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2646 nextchr = UCHARAT(++locinput);
2649 PL_reg_flags |= RF_tainted;
2655 LOAD_UTF8_CHARCLASS(digit,"0");
2656 if (!(OP(scan) == DIGIT
2657 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2658 : isDIGIT_LC_utf8((U8*)locinput)))
2662 locinput += PL_utf8skip[nextchr];
2663 nextchr = UCHARAT(locinput);
2666 if (!(OP(scan) == DIGIT
2667 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2669 nextchr = UCHARAT(++locinput);
2672 PL_reg_flags |= RF_tainted;
2675 if (!nextchr && locinput >= PL_regeol)
2678 LOAD_UTF8_CHARCLASS(digit,"0");
2679 if (OP(scan) == NDIGIT
2680 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2681 : isDIGIT_LC_utf8((U8*)locinput))
2685 locinput += PL_utf8skip[nextchr];
2686 nextchr = UCHARAT(locinput);
2689 if (OP(scan) == NDIGIT
2690 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2692 nextchr = UCHARAT(++locinput);
2695 if (locinput >= PL_regeol)
2698 LOAD_UTF8_CHARCLASS(mark,"~");
2699 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2701 locinput += PL_utf8skip[nextchr];
2702 while (locinput < PL_regeol &&
2703 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2704 locinput += UTF8SKIP(locinput);
2705 if (locinput > PL_regeol)
2710 nextchr = UCHARAT(locinput);
2713 PL_reg_flags |= RF_tainted;
2717 n = ARG(scan); /* which paren pair */
2718 ln = PL_regstartp[n];
2719 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2720 if (*PL_reglastparen < n || ln == -1)
2721 sayNO; /* Do not match unless seen CLOSEn. */
2722 if (ln == PL_regendp[n])
2726 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2728 char *e = PL_bostr + PL_regendp[n];
2730 * Note that we can't do the "other character" lookup trick as
2731 * in the 8-bit case (no pun intended) because in Unicode we
2732 * have to map both upper and title case to lower case.
2734 if (OP(scan) == REFF) {
2735 STRLEN ulen1, ulen2;
2736 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2737 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2741 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2742 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2743 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2750 nextchr = UCHARAT(locinput);
2754 /* Inline the first character, for speed. */
2755 if (UCHARAT(s) != nextchr &&
2757 (UCHARAT(s) != ((OP(scan) == REFF
2758 ? PL_fold : PL_fold_locale)[nextchr]))))
2760 ln = PL_regendp[n] - ln;
2761 if (locinput + ln > PL_regeol)
2763 if (ln > 1 && (OP(scan) == REF
2764 ? memNE(s, locinput, ln)
2766 ? ibcmp(s, locinput, ln)
2767 : ibcmp_locale(s, locinput, ln))))
2770 nextchr = UCHARAT(locinput);
2781 OP_4tree *oop = PL_op;
2782 COP *ocurcop = PL_curcop;
2783 SV **ocurpad = PL_curpad;
2787 PL_op = (OP_4tree*)PL_regdata->data[n];
2788 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2789 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2790 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2794 CALLRUNOPS(aTHX); /* Scalar context. */
2797 ret = Nullsv; /* protect against empty (?{}) blocks. */
2805 PL_curpad = ocurpad;
2806 PL_curcop = ocurcop;
2808 if (logical == 2) { /* Postponed subexpression. */
2810 MAGIC *mg = Null(MAGIC*);
2812 CHECKPOINT cp, lastcp;
2814 if(SvROK(ret) || SvRMAGICAL(ret)) {
2815 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2818 mg = mg_find(sv, PERL_MAGIC_qr);
2821 re = (regexp *)mg->mg_obj;
2822 (void)ReREFCNT_inc(re);
2826 char *t = SvPV(ret, len);
2828 char *oprecomp = PL_regprecomp;
2829 I32 osize = PL_regsize;
2830 I32 onpar = PL_regnpar;
2833 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2835 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2836 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2838 PL_regprecomp = oprecomp;
2843 PerlIO_printf(Perl_debug_log,
2844 "Entering embedded `%s%.60s%s%s'\n",
2848 (strlen(re->precomp) > 60 ? "..." : ""))
2851 state.prev = PL_reg_call_cc;
2852 state.cc = PL_regcc;
2853 state.re = PL_reg_re;
2857 cp = regcppush(0); /* Save *all* the positions. */
2860 state.ss = PL_savestack_ix;
2861 *PL_reglastparen = 0;
2862 *PL_reglastcloseparen = 0;
2863 PL_reg_call_cc = &state;
2864 PL_reginput = locinput;
2866 /* XXXX This is too dramatic a measure... */
2869 if (regmatch(re->program + 1)) {
2870 /* Even though we succeeded, we need to restore
2871 global variables, since we may be wrapped inside
2872 SUSPEND, thus the match may be not finished yet. */
2874 /* XXXX Do this only if SUSPENDed? */
2875 PL_reg_call_cc = state.prev;
2876 PL_regcc = state.cc;
2877 PL_reg_re = state.re;
2878 cache_re(PL_reg_re);
2880 /* XXXX This is too dramatic a measure... */
2883 /* These are needed even if not SUSPEND. */
2889 REGCP_UNWIND(lastcp);
2891 PL_reg_call_cc = state.prev;
2892 PL_regcc = state.cc;
2893 PL_reg_re = state.re;
2894 cache_re(PL_reg_re);
2896 /* XXXX This is too dramatic a measure... */
2906 sv_setsv(save_scalar(PL_replgv), ret);
2910 n = ARG(scan); /* which paren pair */
2911 PL_reg_start_tmp[n] = locinput;
2916 n = ARG(scan); /* which paren pair */
2917 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2918 PL_regendp[n] = locinput - PL_bostr;
2919 if (n > *PL_reglastparen)
2920 *PL_reglastparen = n;
2921 *PL_reglastcloseparen = n;
2924 n = ARG(scan); /* which paren pair */
2925 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2928 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2930 next = NEXTOPER(NEXTOPER(scan));
2932 next = scan + ARG(scan);
2933 if (OP(next) == IFTHEN) /* Fake one. */
2934 next = NEXTOPER(NEXTOPER(next));
2938 logical = scan->flags;
2940 /*******************************************************************
2941 PL_regcc contains infoblock about the innermost (...)* loop, and
2942 a pointer to the next outer infoblock.
2944 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2946 1) After matching X, regnode for CURLYX is processed;
2948 2) This regnode creates infoblock on the stack, and calls
2949 regmatch() recursively with the starting point at WHILEM node;
2951 3) Each hit of WHILEM node tries to match A and Z (in the order
2952 depending on the current iteration, min/max of {min,max} and
2953 greediness). The information about where are nodes for "A"
2954 and "Z" is read from the infoblock, as is info on how many times "A"
2955 was already matched, and greediness.
2957 4) After A matches, the same WHILEM node is hit again.
2959 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2960 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2961 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2962 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2963 of the external loop.
2965 Currently present infoblocks form a tree with a stem formed by PL_curcc
2966 and whatever it mentions via ->next, and additional attached trees
2967 corresponding to temporarily unset infoblocks as in "5" above.
2969 In the following picture infoblocks for outer loop of
2970 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2971 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2972 infoblocks are drawn below the "reset" infoblock.
2974 In fact in the picture below we do not show failed matches for Z and T
2975 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2976 more obvious *why* one needs to *temporary* unset infoblocks.]
2978 Matched REx position InfoBlocks Comment
2982 Y A)*?Z)*?T x <- O <- I
2983 YA )*?Z)*?T x <- O <- I
2984 YA A)*?Z)*?T x <- O <- I
2985 YAA )*?Z)*?T x <- O <- I
2986 YAA Z)*?T x <- O # Temporary unset I
2989 YAAZ Y(A)*?Z)*?T x <- O
2992 YAAZY (A)*?Z)*?T x <- O
2995 YAAZY A)*?Z)*?T x <- O <- I
2998 YAAZYA )*?Z)*?T x <- O <- I
3001 YAAZYA Z)*?T x <- O # Temporary unset I
3007 YAAZYAZ T x # Temporary unset O
3014 *******************************************************************/
3017 CHECKPOINT cp = PL_savestack_ix;
3018 /* No need to save/restore up to this paren */
3019 I32 parenfloor = scan->flags;
3021 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3023 cc.oldcc = PL_regcc;
3025 /* XXXX Probably it is better to teach regpush to support
3026 parenfloor > PL_regsize... */
3027 if (parenfloor > *PL_reglastparen)
3028 parenfloor = *PL_reglastparen; /* Pessimization... */
3029 cc.parenfloor = parenfloor;
3031 cc.min = ARG1(scan);
3032 cc.max = ARG2(scan);
3033 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3037 PL_reginput = locinput;
3038 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3040 PL_regcc = cc.oldcc;
3046 * This is really hard to understand, because after we match
3047 * what we're trying to match, we must make sure the rest of
3048 * the REx is going to match for sure, and to do that we have
3049 * to go back UP the parse tree by recursing ever deeper. And
3050 * if it fails, we have to reset our parent's current state
3051 * that we can try again after backing off.
3054 CHECKPOINT cp, lastcp;
3055 CURCUR* cc = PL_regcc;
3056 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3058 n = cc->cur + 1; /* how many we know we matched */
3059 PL_reginput = locinput;
3062 PerlIO_printf(Perl_debug_log,
3063 "%*s %ld out of %ld..%ld cc=%lx\n",
3064 REPORT_CODE_OFF+PL_regindent*2, "",
3065 (long)n, (long)cc->min,
3066 (long)cc->max, (long)cc)
3069 /* If degenerate scan matches "", assume scan done. */
3071 if (locinput == cc->lastloc && n >= cc->min) {
3072 PL_regcc = cc->oldcc;
3076 PerlIO_printf(Perl_debug_log,
3077 "%*s empty match detected, try continuation...\n",
3078 REPORT_CODE_OFF+PL_regindent*2, "")
3080 if (regmatch(cc->next))
3088 /* First just match a string of min scans. */
3092 cc->lastloc = locinput;
3093 if (regmatch(cc->scan))
3096 cc->lastloc = lastloc;
3101 /* Check whether we already were at this position.
3102 Postpone detection until we know the match is not
3103 *that* much linear. */
3104 if (!PL_reg_maxiter) {
3105 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3106 PL_reg_leftiter = PL_reg_maxiter;
3108 if (PL_reg_leftiter-- == 0) {
3109 I32 size = (PL_reg_maxiter + 7)/8;
3110 if (PL_reg_poscache) {
3111 if (PL_reg_poscache_size < size) {
3112 Renew(PL_reg_poscache, size, char);
3113 PL_reg_poscache_size = size;
3115 Zero(PL_reg_poscache, size, char);
3118 PL_reg_poscache_size = size;
3119 Newz(29, PL_reg_poscache, size, char);
3122 PerlIO_printf(Perl_debug_log,
3123 "%sDetected a super-linear match, switching on caching%s...\n",
3124 PL_colors[4], PL_colors[5])
3127 if (PL_reg_leftiter < 0) {
3128 I32 o = locinput - PL_bostr, b;
3130 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3133 if (PL_reg_poscache[o] & (1<<b)) {
3135 PerlIO_printf(Perl_debug_log,
3136 "%*s already tried at this position...\n",
3137 REPORT_CODE_OFF+PL_regindent*2, "")
3141 PL_reg_poscache[o] |= (1<<b);
3145 /* Prefer next over scan for minimal matching. */
3148 PL_regcc = cc->oldcc;
3151 cp = regcppush(cc->parenfloor);
3153 if (regmatch(cc->next)) {
3155 sayYES; /* All done. */
3157 REGCP_UNWIND(lastcp);
3163 if (n >= cc->max) { /* Maximum greed exceeded? */
3164 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3165 && !(PL_reg_flags & RF_warned)) {
3166 PL_reg_flags |= RF_warned;
3167 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3168 "Complex regular subexpression recursion",
3175 PerlIO_printf(Perl_debug_log,
3176 "%*s trying longer...\n",
3177 REPORT_CODE_OFF+PL_regindent*2, "")
3179 /* Try scanning more and see if it helps. */
3180 PL_reginput = locinput;
3182 cc->lastloc = locinput;
3183 cp = regcppush(cc->parenfloor);
3185 if (regmatch(cc->scan)) {
3189 REGCP_UNWIND(lastcp);
3192 cc->lastloc = lastloc;
3196 /* Prefer scan over next for maximal matching. */
3198 if (n < cc->max) { /* More greed allowed? */
3199 cp = regcppush(cc->parenfloor);
3201 cc->lastloc = locinput;
3203 if (regmatch(cc->scan)) {
3207 REGCP_UNWIND(lastcp);
3208 regcppop(); /* Restore some previous $<digit>s? */
3209 PL_reginput = locinput;
3211 PerlIO_printf(Perl_debug_log,
3212 "%*s failed, try continuation...\n",
3213 REPORT_CODE_OFF+PL_regindent*2, "")
3216 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3217 && !(PL_reg_flags & RF_warned)) {
3218 PL_reg_flags |= RF_warned;
3219 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3220 "Complex regular subexpression recursion",
3224 /* Failed deeper matches of scan, so see if this one works. */
3225 PL_regcc = cc->oldcc;
3228 if (regmatch(cc->next))
3234 cc->lastloc = lastloc;
3239 next = scan + ARG(scan);
3242 inner = NEXTOPER(NEXTOPER(scan));
3245 inner = NEXTOPER(scan);
3249 if (OP(next) != c1) /* No choice. */
3250 next = inner; /* Avoid recursion. */
3252 I32 lastparen = *PL_reglastparen;
3254 re_unwind_branch_t *uw;
3256 /* Put unwinding data on stack */
3257 unwind1 = SSNEWt(1,re_unwind_branch_t);
3258 uw = SSPTRt(unwind1,re_unwind_branch_t);
3261 uw->type = ((c1 == BRANCH)
3263 : RE_UNWIND_BRANCHJ);
3264 uw->lastparen = lastparen;
3266 uw->locinput = locinput;
3267 uw->nextchr = nextchr;
3269 uw->regindent = ++PL_regindent;
3272 REGCP_SET(uw->lastcp);
3274 /* Now go into the first branch */
3287 /* We suppose that the next guy does not need
3288 backtracking: in particular, it is of constant length,
3289 and has no parenths to influence future backrefs. */
3290 ln = ARG1(scan); /* min to match */
3291 n = ARG2(scan); /* max to match */
3292 paren = scan->flags;
3294 if (paren > PL_regsize)
3296 if (paren > *PL_reglastparen)
3297 *PL_reglastparen = paren;
3299 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3301 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3302 PL_reginput = locinput;
3305 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3307 /* if we matched something zero-length we don't need to
3308 backtrack - capturing parens are already defined, so
3309 the caveat in the maximal case doesn't apply
3311 XXXX if ln == 0, we can redo this check first time
3312 through the following loop
3315 n = ln; /* don't backtrack */
3316 locinput = PL_reginput;
3317 if (HAS_TEXT(next) || JUMPABLE(next)) {
3318 regnode *text_node = next;
3320 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3322 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3324 if (PL_regkind[(U8)OP(text_node)] == REF) {
3326 n = ARG(text_node); /* which paren pair */
3327 ln = PL_regstartp[n];
3328 /* assume yes if we haven't seen CLOSEn */
3330 *PL_reglastparen < n ||
3337 c1 = *(PL_bostr + ln);
3339 else { c1 = (U8)*STRING(text_node); }
3340 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3342 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3343 c2 = PL_fold_locale[c1];
3352 /* This may be improved if l == 0. */
3353 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3354 /* If it could work, try it. */
3356 UCHARAT(PL_reginput) == c1 ||
3357 UCHARAT(PL_reginput) == c2)
3361 PL_regstartp[paren] =
3362 HOPc(PL_reginput, -l) - PL_bostr;
3363 PL_regendp[paren] = PL_reginput - PL_bostr;
3366 PL_regendp[paren] = -1;
3370 REGCP_UNWIND(lastcp);
3372 /* Couldn't or didn't -- move forward. */
3373 PL_reginput = locinput;
3374 if (regrepeat_hard(scan, 1, &l)) {
3376 locinput = PL_reginput;
3383 n = regrepeat_hard(scan, n, &l);
3384 /* if we matched something zero-length we don't need to
3385 backtrack, unless the minimum count is zero and we
3386 are capturing the result - in that case the capture
3387 being defined or not may affect later execution
3389 if (n != 0 && l == 0 && !(paren && ln == 0))
3390 ln = n; /* don't backtrack */
3391 locinput = PL_reginput;
3393 PerlIO_printf(Perl_debug_log,
3394 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3395 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
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) {
3408 n = ARG(text_node); /* which paren pair */
3409 ln = PL_regstartp[n];
3410 /* assume yes if we haven't seen CLOSEn */
3412 *PL_reglastparen < n ||
3419 c1 = *(PL_bostr + ln);
3421 else { c1 = (U8)*STRING(text_node); }
3423 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3425 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3426 c2 = PL_fold_locale[c1];
3437 /* If it could work, try it. */
3439 UCHARAT(PL_reginput) == c1 ||
3440 UCHARAT(PL_reginput) == c2)
3443 PerlIO_printf(Perl_debug_log,
3444 "%*s trying tail with n=%"IVdf"...\n",
3445 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3449 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3450 PL_regendp[paren] = PL_reginput - PL_bostr;
3453 PL_regendp[paren] = -1;
3457 REGCP_UNWIND(lastcp);
3459 /* Couldn't or didn't -- back up. */
3461 locinput = HOPc(locinput, -l);
3462 PL_reginput = locinput;
3469 paren = scan->flags; /* Which paren to set */
3470 if (paren > PL_regsize)
3472 if (paren > *PL_reglastparen)
3473 *PL_reglastparen = paren;
3474 ln = ARG1(scan); /* min to match */
3475 n = ARG2(scan); /* max to match */
3476 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3480 ln = ARG1(scan); /* min to match */
3481 n = ARG2(scan); /* max to match */
3482 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3487 scan = NEXTOPER(scan);
3493 scan = NEXTOPER(scan);
3497 * Lookahead to avoid useless match attempts
3498 * when we know what character comes next.
3502 * Used to only do .*x and .*?x, but now it allows
3503 * for )'s, ('s and (?{ ... })'s to be in the way
3504 * of the quantifier and the EXACT-like node. -- japhy
3507 if (HAS_TEXT(next) || JUMPABLE(next)) {
3509 regnode *text_node = next;
3511 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3513 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3515 if (PL_regkind[(U8)OP(text_node)] == REF) {
3517 n = ARG(text_node); /* which paren pair */
3518 ln = PL_regstartp[n];
3519 /* assume yes if we haven't seen CLOSEn */
3521 *PL_reglastparen < n ||
3526 goto assume_ok_easy;
3528 s = (U8*)PL_bostr + ln;
3530 else { s = (U8*)STRING(text_node); }
3534 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3536 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3537 c2 = PL_fold_locale[c1];
3540 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3541 STRLEN ulen1, ulen2;
3542 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3543 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3545 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3546 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3548 c1 = utf8_to_uvuni(tmpbuf1, 0);
3549 c2 = utf8_to_uvuni(tmpbuf2, 0);
3552 c2 = c1 = utf8_to_uvchr(s, NULL);
3560 PL_reginput = locinput;
3564 if (ln && regrepeat(scan, ln) < ln)
3566 locinput = PL_reginput;
3569 char *e; /* Should not check after this */
3570 char *old = locinput;
3572 if (n == REG_INFTY) {
3575 while (UTF8_IS_CONTINUATION(*(U8*)e))
3581 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3585 e = locinput + n - ln;
3591 /* Find place 'next' could work */
3594 while (locinput <= e &&
3595 UCHARAT(locinput) != c1)
3598 while (locinput <= e
3599 && UCHARAT(locinput) != c1
3600 && UCHARAT(locinput) != c2)
3603 count = locinput - old;
3610 utf8_to_uvchr((U8*)locinput, &len) != c1;
3615 for (count = 0; locinput <= e; count++) {
3616 UV c = utf8_to_uvchr((U8*)locinput, &len);
3617 if (c == c1 || c == c2)
3625 /* PL_reginput == old now */
3626 if (locinput != old) {
3627 ln = 1; /* Did some */
3628 if (regrepeat(scan, count) < count)
3631 /* PL_reginput == locinput now */
3632 TRYPAREN(paren, ln, locinput);
3633 PL_reginput = locinput; /* Could be reset... */
3634 REGCP_UNWIND(lastcp);
3635 /* Couldn't or didn't -- move forward. */
3638 locinput += UTF8SKIP(locinput);
3644 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3648 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3650 c = UCHARAT(PL_reginput);
3651 /* If it could work, try it. */
3652 if (c == c1 || c == c2)
3654 TRYPAREN(paren, n, PL_reginput);
3655 REGCP_UNWIND(lastcp);
3658 /* If it could work, try it. */
3659 else if (c1 == -1000)
3661 TRYPAREN(paren, n, PL_reginput);
3662 REGCP_UNWIND(lastcp);
3664 /* Couldn't or didn't -- move forward. */
3665 PL_reginput = locinput;
3666 if (regrepeat(scan, 1)) {
3668 locinput = PL_reginput;
3676 n = regrepeat(scan, n);
3677 locinput = PL_reginput;
3678 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3679 ((!PL_multiline && OP(next) != MEOL) ||
3680 OP(next) == SEOL || OP(next) == EOS))
3682 ln = n; /* why back off? */
3683 /* ...because $ and \Z can match before *and* after
3684 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3685 We should back off by one in this case. */
3686 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3695 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3697 c = UCHARAT(PL_reginput);
3699 /* If it could work, try it. */
3700 if (c1 == -1000 || c == c1 || c == c2)
3702 TRYPAREN(paren, n, PL_reginput);
3703 REGCP_UNWIND(lastcp);
3705 /* Couldn't or didn't -- back up. */
3707 PL_reginput = locinput = HOPc(locinput, -1);
3715 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3717 c = UCHARAT(PL_reginput);
3719 /* If it could work, try it. */
3720 if (c1 == -1000 || c == c1 || c == c2)
3722 TRYPAREN(paren, n, PL_reginput);
3723 REGCP_UNWIND(lastcp);
3725 /* Couldn't or didn't -- back up. */
3727 PL_reginput = locinput = HOPc(locinput, -1);
3734 if (PL_reg_call_cc) {
3735 re_cc_state *cur_call_cc = PL_reg_call_cc;
3736 CURCUR *cctmp = PL_regcc;
3737 regexp *re = PL_reg_re;
3738 CHECKPOINT cp, lastcp;
3740 cp = regcppush(0); /* Save *all* the positions. */
3742 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3744 PL_reginput = locinput; /* Make position available to
3746 cache_re(PL_reg_call_cc->re);
3747 PL_regcc = PL_reg_call_cc->cc;
3748 PL_reg_call_cc = PL_reg_call_cc->prev;
3749 if (regmatch(cur_call_cc->node)) {
3750 PL_reg_call_cc = cur_call_cc;
3754 REGCP_UNWIND(lastcp);
3756 PL_reg_call_cc = cur_call_cc;
3762 PerlIO_printf(Perl_debug_log,
3763 "%*s continuation failed...\n",
3764 REPORT_CODE_OFF+PL_regindent*2, "")
3768 if (locinput < PL_regtill) {
3769 DEBUG_r(PerlIO_printf(Perl_debug_log,
3770 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3772 (long)(locinput - PL_reg_starttry),
3773 (long)(PL_regtill - PL_reg_starttry),
3775 sayNO_FINAL; /* Cannot match: too short. */
3777 PL_reginput = locinput; /* put where regtry can find it */
3778 sayYES_FINAL; /* Success! */
3780 PL_reginput = locinput; /* put where regtry can find it */
3781 sayYES_LOUD; /* Success! */
3784 PL_reginput = locinput;
3789 s = HOPBACKc(locinput, scan->flags);
3795 PL_reginput = locinput;
3800 s = HOPBACKc(locinput, scan->flags);
3806 PL_reginput = locinput;
3809 inner = NEXTOPER(NEXTOPER(scan));
3810 if (regmatch(inner) != n) {
3825 if (OP(scan) == SUSPEND) {
3826 locinput = PL_reginput;
3827 nextchr = UCHARAT(locinput);
3832 next = scan + ARG(scan);
3837 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3838 PTR2UV(scan), OP(scan));
3839 Perl_croak(aTHX_ "regexp memory corruption");
3846 * We get here only if there's trouble -- normally "case END" is
3847 * the terminating point.
3849 Perl_croak(aTHX_ "corrupted regexp pointers");
3855 PerlIO_printf(Perl_debug_log,
3856 "%*s %scould match...%s\n",
3857 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3861 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3862 PL_colors[4],PL_colors[5]));
3868 #if 0 /* Breaks $^R */
3876 PerlIO_printf(Perl_debug_log,
3877 "%*s %sfailed...%s\n",
3878 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3884 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3887 case RE_UNWIND_BRANCH:
3888 case RE_UNWIND_BRANCHJ:
3890 re_unwind_branch_t *uwb = &(uw->branch);
3891 I32 lastparen = uwb->lastparen;
3893 REGCP_UNWIND(uwb->lastcp);
3894 for (n = *PL_reglastparen; n > lastparen; n--)
3896 *PL_reglastparen = n;
3897 scan = next = uwb->next;
3899 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3900 ? BRANCH : BRANCHJ) ) { /* Failure */
3907 /* Have more choice yet. Reuse the same uwb. */
3909 if ((n = (uwb->type == RE_UNWIND_BRANCH
3910 ? NEXT_OFF(next) : ARG(next))))
3913 next = NULL; /* XXXX Needn't unwinding in this case... */
3915 next = NEXTOPER(scan);
3916 if (uwb->type == RE_UNWIND_BRANCHJ)
3917 next = NEXTOPER(next);
3918 locinput = uwb->locinput;
3919 nextchr = uwb->nextchr;
3921 PL_regindent = uwb->regindent;
3928 Perl_croak(aTHX_ "regexp unwind memory corruption");
3939 - regrepeat - repeatedly match something simple, report how many
3942 * [This routine now assumes that it will only match on things of length 1.
3943 * That was true before, but now we assume scan - reginput is the count,
3944 * rather than incrementing count on every character. [Er, except utf8.]]
3947 S_regrepeat(pTHX_ regnode *p, I32 max)
3949 register char *scan;
3951 register char *loceol = PL_regeol;
3952 register I32 hardcount = 0;
3953 register bool do_utf8 = PL_reg_match_utf8;
3956 if (max != REG_INFTY && max < loceol - scan)
3957 loceol = scan + max;
3962 while (scan < loceol && hardcount < max && *scan != '\n') {
3963 scan += UTF8SKIP(scan);
3967 while (scan < loceol && *scan != '\n')
3974 while (scan < loceol && hardcount < max) {
3975 scan += UTF8SKIP(scan);
3985 case EXACT: /* length of string is 1 */
3987 while (scan < loceol && UCHARAT(scan) == c)
3990 case EXACTF: /* length of string is 1 */
3992 while (scan < loceol &&
3993 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3996 case EXACTFL: /* length of string is 1 */
3997 PL_reg_flags |= RF_tainted;
3999 while (scan < loceol &&
4000 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4006 while (hardcount < max && scan < loceol &&
4007 reginclass(p, (U8*)scan, 0, do_utf8)) {
4008 scan += UTF8SKIP(scan);
4012 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4019 LOAD_UTF8_CHARCLASS(alnum,"a");
4020 while (hardcount < max && scan < loceol &&
4021 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4022 scan += UTF8SKIP(scan);
4026 while (scan < loceol && isALNUM(*scan))
4031 PL_reg_flags |= RF_tainted;
4034 while (hardcount < max && scan < loceol &&
4035 isALNUM_LC_utf8((U8*)scan)) {
4036 scan += UTF8SKIP(scan);
4040 while (scan < loceol && isALNUM_LC(*scan))
4047 LOAD_UTF8_CHARCLASS(alnum,"a");
4048 while (hardcount < max && scan < loceol &&
4049 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4050 scan += UTF8SKIP(scan);
4054 while (scan < loceol && !isALNUM(*scan))
4059 PL_reg_flags |= RF_tainted;
4062 while (hardcount < max && scan < loceol &&
4063 !isALNUM_LC_utf8((U8*)scan)) {
4064 scan += UTF8SKIP(scan);
4068 while (scan < loceol && !isALNUM_LC(*scan))
4075 LOAD_UTF8_CHARCLASS(space," ");
4076 while (hardcount < max && scan < loceol &&
4078 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4079 scan += UTF8SKIP(scan);
4083 while (scan < loceol && isSPACE(*scan))
4088 PL_reg_flags |= RF_tainted;
4091 while (hardcount < max && scan < loceol &&
4092 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4093 scan += UTF8SKIP(scan);
4097 while (scan < loceol && isSPACE_LC(*scan))
4104 LOAD_UTF8_CHARCLASS(space," ");
4105 while (hardcount < max && scan < loceol &&
4107 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4108 scan += UTF8SKIP(scan);
4112 while (scan < loceol && !isSPACE(*scan))
4117 PL_reg_flags |= RF_tainted;
4120 while (hardcount < max && scan < loceol &&
4121 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4122 scan += UTF8SKIP(scan);
4126 while (scan < loceol && !isSPACE_LC(*scan))
4133 LOAD_UTF8_CHARCLASS(digit,"0");
4134 while (hardcount < max && scan < loceol &&
4135 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4136 scan += UTF8SKIP(scan);
4140 while (scan < loceol && isDIGIT(*scan))
4147 LOAD_UTF8_CHARCLASS(digit,"0");
4148 while (hardcount < max && scan < loceol &&
4149 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4150 scan += UTF8SKIP(scan);
4154 while (scan < loceol && !isDIGIT(*scan))
4158 default: /* Called on something of 0 width. */
4159 break; /* So match right here or not at all. */
4165 c = scan - PL_reginput;
4170 SV *prop = sv_newmortal();
4173 PerlIO_printf(Perl_debug_log,
4174 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4175 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4182 - regrepeat_hard - repeatedly match something, report total lenth and length
4184 * The repeater is supposed to have constant length.
4188 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4190 register char *scan = Nullch;
4191 register char *start;
4192 register char *loceol = PL_regeol;
4194 I32 count = 0, res = 1;
4199 start = PL_reginput;
4200 if (PL_reg_match_utf8) {
4201 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4204 while (start < PL_reginput) {
4206 start += UTF8SKIP(start);
4217 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4219 *lp = l = PL_reginput - start;
4220 if (max != REG_INFTY && l*max < loceol - scan)
4221 loceol = scan + l*max;
4234 - regclass_swash - prepare the utf8 swash
4238 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4244 if (PL_regdata && PL_regdata->count) {
4247 if (PL_regdata->what[n] == 's') {
4248 SV *rv = (SV*)PL_regdata->data[n];
4249 AV *av = (AV*)SvRV((SV*)rv);
4252 /* See the end of regcomp.c:S_reglass() for
4253 * documentation of these array elements. */
4255 si = *av_fetch(av, 0, FALSE);
4256 a = av_fetch(av, 1, FALSE);
4257 b = av_fetch(av, 2, FALSE);
4261 else if (si && doinit) {
4262 sw = swash_init("utf8", "", si, 1, 0);
4263 (void)av_store(av, 1, sw);
4279 - reginclass - determine if a character falls into a character class
4281 The n is the ANYOF regnode, the p is the target string, lenp
4282 is pointer to the maximum length of how far to go in the p
4283 (if the lenp is zero, UTF8SKIP(p) is used),
4284 do_utf8 tells whether the target string is in UTF-8.
4289 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4291 char flags = ANYOF_FLAGS(n);
4297 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4299 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4300 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4303 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4304 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4307 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4311 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4314 if (swash_fetch(sw, p, do_utf8))
4316 else if (flags & ANYOF_FOLD) {
4317 if (!match && lenp && av) {
4320 for (i = 0; i <= av_len(av); i++) {
4321 SV* sv = *av_fetch(av, i, FALSE);
4323 char *s = SvPV(sv, len);
4325 if (len <= plen && memEQ(s, (char*)p, len)) {
4333 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4336 to_utf8_fold(p, tmpbuf, &tmplen);
4337 if (swash_fetch(sw, tmpbuf, do_utf8))
4343 if (match && lenp && *lenp == 0)
4344 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4346 if (!match && c < 256) {
4347 if (ANYOF_BITMAP_TEST(n, c))
4349 else if (flags & ANYOF_FOLD) {
4352 if (flags & ANYOF_LOCALE) {
4353 PL_reg_flags |= RF_tainted;
4354 f = PL_fold_locale[c];
4358 if (f != c && ANYOF_BITMAP_TEST(n, f))
4362 if (!match && (flags & ANYOF_CLASS)) {
4363 PL_reg_flags |= RF_tainted;
4365 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4366 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4367 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4368 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4369 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4370 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4371 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4372 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4373 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4374 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4375 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4376 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4377 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4378 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4379 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4380 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4381 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4382 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4383 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4384 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4385 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4386 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4387 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4388 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4389 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4390 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4391 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4392 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4393 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4394 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4395 ) /* How's that for a conditional? */
4402 return (flags & ANYOF_INVERT) ? !match : match;
4406 S_reghop(pTHX_ U8 *s, I32 off)
4408 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4412 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4415 while (off-- && s < lim) {
4416 /* XXX could check well-formedness here */
4424 if (UTF8_IS_CONTINUED(*s)) {
4425 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4428 /* XXX could check well-formedness here */
4436 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4438 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4442 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4445 while (off-- && s < lim) {
4446 /* XXX could check well-formedness here */
4456 if (UTF8_IS_CONTINUED(*s)) {
4457 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4460 /* XXX could check well-formedness here */
4472 restore_pos(pTHX_ void *arg)
4474 if (PL_reg_eval_set) {
4475 if (PL_reg_oldsaved) {
4476 PL_reg_re->subbeg = PL_reg_oldsaved;
4477 PL_reg_re->sublen = PL_reg_oldsavedlen;
4478 RX_MATCH_COPIED_on(PL_reg_re);
4480 PL_reg_magic->mg_len = PL_reg_oldpos;
4481 PL_reg_eval_set = 0;
4482 PL_curpm = PL_reg_oldcurpm;
4487 S_to_utf8_substr(pTHX_ register regexp *prog)
4490 if (prog->float_substr && !prog->float_utf8) {
4491 prog->float_utf8 = sv = NEWSV(117, 0);
4492 SvSetMagicSV(sv, prog->float_substr);
4493 sv_utf8_upgrade(sv);
4494 if (SvTAIL(prog->float_substr))
4496 if (prog->float_substr == prog->check_substr)
4497 prog->check_utf8 = sv;
4499 if (prog->anchored_substr && !prog->anchored_utf8) {
4500 prog->anchored_utf8 = sv = NEWSV(118, 0);
4501 SvSetMagicSV(sv, prog->anchored_substr);
4502 sv_utf8_upgrade(sv);
4503 if (SvTAIL(prog->anchored_substr))
4505 if (prog->anchored_substr == prog->check_substr)
4506 prog->check_utf8 = sv;
4511 S_to_byte_substr(pTHX_ register regexp *prog)
4514 if (prog->float_utf8 && !prog->float_substr) {
4515 prog->float_substr = sv = NEWSV(117, 0);
4516 SvSetMagicSV(sv, prog->float_utf8);
4517 if (sv_utf8_downgrade(sv, TRUE)) {
4518 if (SvTAIL(prog->float_utf8))
4522 prog->float_substr = sv = &PL_sv_undef;
4524 if (prog->float_utf8 == prog->check_utf8)
4525 prog->check_substr = sv;
4527 if (prog->anchored_utf8 && !prog->anchored_substr) {
4528 prog->anchored_substr = sv = NEWSV(118, 0);
4529 SvSetMagicSV(sv, prog->anchored_utf8);
4530 if (sv_utf8_downgrade(sv, TRUE)) {
4531 if (SvTAIL(prog->anchored_utf8))
4535 prog->anchored_substr = sv = &PL_sv_undef;
4537 if (prog->anchored_utf8 == prog->check_utf8)
4538 prog->check_substr = sv;