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 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1003 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1004 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1005 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1016 c2 = PL_fold_locale[c1];
1018 e = HOP3c(strend, -(I32)ln, s);
1021 e = s; /* Due to minlen logic of intuit() */
1023 /* The idea in the EXACTF* cases is to first find the
1024 * first character of the EXACTF* node and then, if
1025 * necessary, case-insensitively compare the full
1026 * text of the node. The c1 and c2 are the first
1027 * characters (though in Unicode it gets a bit
1028 * more complicated because there are more cases
1029 * than just upper and lower: one needs to use
1030 * the so-called folding case for case-insensitive
1031 * matching (called "loose matching" in Unicode).
1032 * ibcmp_utf8() will do just that. */
1036 U8 tmpbuf [UTF8_MAXLEN+1];
1037 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1038 STRLEN len, foldlen;
1042 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1044 0 : UTF8_ALLOW_ANY);
1047 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1048 m, (char **)0, ln, UTF))
1049 && (norun || regtry(prog, s)) )
1052 uvchr_to_utf8(tmpbuf, c);
1053 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1055 && (f == c1 || f == c2)
1056 && (ln == foldlen ||
1057 !ibcmp_utf8((char *) foldbuf,
1058 (char **)0, foldlen, do_utf8,
1060 (char **)0, ln, UTF))
1061 && (norun || regtry(prog, s)) )
1069 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1071 0 : UTF8_ALLOW_ANY);
1073 /* Handle some of the three Greek sigmas cases.
1074 * Note that not all the possible combinations
1075 * are handled here: some of them are handled
1076 * by the standard folding rules, and some of
1077 * them (the character class or ANYOF cases)
1078 * are handled during compiletime in
1079 * regexec.c:S_regclass(). */
1080 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1081 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1082 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1084 if ( (c == c1 || c == c2)
1086 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1087 m, (char **)0, ln, UTF))
1088 && (norun || regtry(prog, s)) )
1091 uvchr_to_utf8(tmpbuf, c);
1092 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1094 && (f == c1 || f == c2)
1095 && (ln == foldlen ||
1096 !ibcmp_utf8((char *) foldbuf,
1097 (char **)0, foldlen, do_utf8,
1099 (char **)0, ln, UTF))
1100 && (norun || regtry(prog, s)) )
1111 && (ln == 1 || !(OP(c) == EXACTF
1113 : ibcmp_locale(s, m, ln)))
1114 && (norun || regtry(prog, s)) )
1120 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1121 && (ln == 1 || !(OP(c) == EXACTF
1123 : ibcmp_locale(s, m, ln)))
1124 && (norun || regtry(prog, s)) )
1131 PL_reg_flags |= RF_tainted;
1138 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1140 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1142 tmp = ((OP(c) == BOUND ?
1143 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1144 LOAD_UTF8_CHARCLASS(alnum,"a");
1145 while (s < strend) {
1146 if (tmp == !(OP(c) == BOUND ?
1147 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1148 isALNUM_LC_utf8((U8*)s)))
1151 if ((norun || regtry(prog, s)))
1158 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1159 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1160 while (s < strend) {
1162 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1164 if ((norun || regtry(prog, s)))
1170 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1174 PL_reg_flags |= RF_tainted;
1181 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1183 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1185 tmp = ((OP(c) == NBOUND ?
1186 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1187 LOAD_UTF8_CHARCLASS(alnum,"a");
1188 while (s < strend) {
1189 if (tmp == !(OP(c) == NBOUND ?
1190 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1191 isALNUM_LC_utf8((U8*)s)))
1193 else if ((norun || regtry(prog, s)))
1199 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1200 tmp = ((OP(c) == NBOUND ?
1201 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1202 while (s < strend) {
1204 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1206 else if ((norun || regtry(prog, s)))
1211 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1216 LOAD_UTF8_CHARCLASS(alnum,"a");
1217 while (s < strend) {
1218 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1219 if (tmp && (norun || regtry(prog, s)))
1230 while (s < strend) {
1232 if (tmp && (norun || regtry(prog, s)))
1244 PL_reg_flags |= RF_tainted;
1246 while (s < strend) {
1247 if (isALNUM_LC_utf8((U8*)s)) {
1248 if (tmp && (norun || regtry(prog, s)))
1259 while (s < strend) {
1260 if (isALNUM_LC(*s)) {
1261 if (tmp && (norun || regtry(prog, s)))
1274 LOAD_UTF8_CHARCLASS(alnum,"a");
1275 while (s < strend) {
1276 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1277 if (tmp && (norun || regtry(prog, s)))
1288 while (s < strend) {
1290 if (tmp && (norun || regtry(prog, s)))
1302 PL_reg_flags |= RF_tainted;
1304 while (s < strend) {
1305 if (!isALNUM_LC_utf8((U8*)s)) {
1306 if (tmp && (norun || regtry(prog, s)))
1317 while (s < strend) {
1318 if (!isALNUM_LC(*s)) {
1319 if (tmp && (norun || regtry(prog, s)))
1332 LOAD_UTF8_CHARCLASS(space," ");
1333 while (s < strend) {
1334 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1335 if (tmp && (norun || regtry(prog, s)))
1346 while (s < strend) {
1348 if (tmp && (norun || regtry(prog, s)))
1360 PL_reg_flags |= RF_tainted;
1362 while (s < strend) {
1363 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1364 if (tmp && (norun || regtry(prog, s)))
1375 while (s < strend) {
1376 if (isSPACE_LC(*s)) {
1377 if (tmp && (norun || regtry(prog, s)))
1390 LOAD_UTF8_CHARCLASS(space," ");
1391 while (s < strend) {
1392 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1393 if (tmp && (norun || regtry(prog, s)))
1404 while (s < strend) {
1406 if (tmp && (norun || regtry(prog, s)))
1418 PL_reg_flags |= RF_tainted;
1420 while (s < strend) {
1421 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1422 if (tmp && (norun || regtry(prog, s)))
1433 while (s < strend) {
1434 if (!isSPACE_LC(*s)) {
1435 if (tmp && (norun || regtry(prog, s)))
1448 LOAD_UTF8_CHARCLASS(digit,"0");
1449 while (s < strend) {
1450 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1451 if (tmp && (norun || regtry(prog, s)))
1462 while (s < strend) {
1464 if (tmp && (norun || regtry(prog, s)))
1476 PL_reg_flags |= RF_tainted;
1478 while (s < strend) {
1479 if (isDIGIT_LC_utf8((U8*)s)) {
1480 if (tmp && (norun || regtry(prog, s)))
1491 while (s < strend) {
1492 if (isDIGIT_LC(*s)) {
1493 if (tmp && (norun || regtry(prog, s)))
1506 LOAD_UTF8_CHARCLASS(digit,"0");
1507 while (s < strend) {
1508 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1509 if (tmp && (norun || regtry(prog, s)))
1520 while (s < strend) {
1522 if (tmp && (norun || regtry(prog, s)))
1534 PL_reg_flags |= RF_tainted;
1536 while (s < strend) {
1537 if (!isDIGIT_LC_utf8((U8*)s)) {
1538 if (tmp && (norun || regtry(prog, s)))
1549 while (s < strend) {
1550 if (!isDIGIT_LC(*s)) {
1551 if (tmp && (norun || regtry(prog, s)))
1563 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1572 - regexec_flags - match a regexp against a string
1575 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1576 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1577 /* strend: pointer to null at end of string */
1578 /* strbeg: real beginning of string */
1579 /* minend: end of match must be >=minend after stringarg. */
1580 /* data: May be used for some additional optimizations. */
1581 /* nosave: For optimizations. */
1584 register regnode *c;
1585 register char *startpos = stringarg;
1586 I32 minlen; /* must match at least this many chars */
1587 I32 dontbother = 0; /* how many characters not to try at end */
1588 /* I32 start_shift = 0; */ /* Offset of the start to find
1589 constant substr. */ /* CC */
1590 I32 end_shift = 0; /* Same for the end. */ /* CC */
1591 I32 scream_pos = -1; /* Internal iterator of scream. */
1593 SV* oreplsv = GvSV(PL_replgv);
1594 bool do_utf8 = DO_UTF8(sv);
1596 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1597 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1604 PL_regnarrate = DEBUG_r_TEST;
1607 /* Be paranoid... */
1608 if (prog == NULL || startpos == NULL) {
1609 Perl_croak(aTHX_ "NULL regexp parameter");
1613 minlen = prog->minlen;
1614 if (strend - startpos < minlen) {
1615 DEBUG_r(PerlIO_printf(Perl_debug_log,
1616 "String too short [regexec_flags]...\n"));
1620 /* Check validity of program. */
1621 if (UCHARAT(prog->program) != REG_MAGIC) {
1622 Perl_croak(aTHX_ "corrupted regexp program");
1626 PL_reg_eval_set = 0;
1629 if (prog->reganch & ROPT_UTF8)
1630 PL_reg_flags |= RF_utf8;
1632 /* Mark beginning of line for ^ and lookbehind. */
1633 PL_regbol = startpos;
1637 /* Mark end of line for $ (and such) */
1640 /* see how far we have to get to not match where we matched before */
1641 PL_regtill = startpos+minend;
1643 /* We start without call_cc context. */
1646 /* If there is a "must appear" string, look for it. */
1649 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1652 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1653 PL_reg_ganch = startpos;
1654 else if (sv && SvTYPE(sv) >= SVt_PVMG
1656 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1657 && mg->mg_len >= 0) {
1658 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1659 if (prog->reganch & ROPT_ANCH_GPOS) {
1660 if (s > PL_reg_ganch)
1665 else /* pos() not defined */
1666 PL_reg_ganch = strbeg;
1669 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1670 re_scream_pos_data d;
1672 d.scream_olds = &scream_olds;
1673 d.scream_pos = &scream_pos;
1674 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1676 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1677 goto phooey; /* not present */
1683 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1684 UNI_DISPLAY_REGEX) :
1686 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1687 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1688 UNI_DISPLAY_REGEX) : startpos;
1689 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1692 PerlIO_printf(Perl_debug_log,
1693 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1694 PL_colors[4],PL_colors[5],PL_colors[0],
1697 len0 > 60 ? "..." : "",
1699 (int)(len1 > 60 ? 60 : len1),
1701 (len1 > 60 ? "..." : "")
1705 /* Simplest case: anchored match need be tried only once. */
1706 /* [unless only anchor is BOL and multiline is set] */
1707 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1708 if (s == startpos && regtry(prog, startpos))
1710 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1711 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1716 dontbother = minlen - 1;
1717 end = HOP3c(strend, -dontbother, strbeg) - 1;
1718 /* for multiline we only have to try after newlines */
1719 if (prog->check_substr || prog->check_utf8) {
1723 if (regtry(prog, s))
1728 if (prog->reganch & RE_USE_INTUIT) {
1729 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1740 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1741 if (regtry(prog, s))
1748 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1749 if (regtry(prog, PL_reg_ganch))
1754 /* Messy cases: unanchored match. */
1755 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1756 /* we have /x+whatever/ */
1757 /* it must be a one character string (XXXX Except UTF?) */
1762 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1763 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1764 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1767 while (s < strend) {
1769 DEBUG_r( did_match = 1 );
1770 if (regtry(prog, s)) goto got_it;
1772 while (s < strend && *s == ch)
1779 while (s < strend) {
1781 DEBUG_r( did_match = 1 );
1782 if (regtry(prog, s)) goto got_it;
1784 while (s < strend && *s == ch)
1790 DEBUG_r(if (!did_match)
1791 PerlIO_printf(Perl_debug_log,
1792 "Did not find anchored character...\n")
1796 else if (prog->anchored_substr != Nullsv
1797 || prog->anchored_utf8 != Nullsv
1798 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1799 && prog->float_max_offset < strend - s)) {
1804 char *last1; /* Last position checked before */
1808 if (prog->anchored_substr || prog->anchored_utf8) {
1809 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1810 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1811 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1812 back_max = back_min = prog->anchored_offset;
1814 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1815 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1816 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1817 back_max = prog->float_max_offset;
1818 back_min = prog->float_min_offset;
1820 if (must == &PL_sv_undef)
1821 /* could not downgrade utf8 check substring, so must fail */
1824 last = HOP3c(strend, /* Cannot start after this */
1825 -(I32)(CHR_SVLEN(must)
1826 - (SvTAIL(must) != 0) + back_min), strbeg);
1829 last1 = HOPc(s, -1);
1831 last1 = s - 1; /* bogus */
1833 /* XXXX check_substr already used to find `s', can optimize if
1834 check_substr==must. */
1836 dontbother = end_shift;
1837 strend = HOPc(strend, -dontbother);
1838 while ( (s <= last) &&
1839 ((flags & REXEC_SCREAM)
1840 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1841 end_shift, &scream_pos, 0))
1842 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1843 (unsigned char*)strend, must,
1844 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1845 DEBUG_r( did_match = 1 );
1846 if (HOPc(s, -back_max) > last1) {
1847 last1 = HOPc(s, -back_min);
1848 s = HOPc(s, -back_max);
1851 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1853 last1 = HOPc(s, -back_min);
1857 while (s <= last1) {
1858 if (regtry(prog, s))
1864 while (s <= last1) {
1865 if (regtry(prog, s))
1871 DEBUG_r(if (!did_match)
1872 PerlIO_printf(Perl_debug_log,
1873 "Did not find %s substr `%s%.*s%s'%s...\n",
1874 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1875 ? "anchored" : "floating"),
1877 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1879 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1883 else if ((c = prog->regstclass)) {
1884 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1885 /* don't bother with what can't match */
1886 strend = HOPc(strend, -(minlen - 1));
1888 SV *prop = sv_newmortal();
1896 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1897 UNI_DISPLAY_REGEX) :
1899 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1901 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1902 len1 = UTF ? SvCUR(dsv1) : strend - s;
1903 PerlIO_printf(Perl_debug_log,
1904 "Matching stclass `%*.*s' against `%*.*s'\n",
1908 if (find_byclass(prog, c, s, strend, startpos, 0))
1910 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1914 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1919 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1920 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1921 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1923 if (flags & REXEC_SCREAM) {
1924 last = screaminstr(sv, float_real, s - strbeg,
1925 end_shift, &scream_pos, 1); /* last one */
1927 last = scream_olds; /* Only one occurrence. */
1931 char *little = SvPV(float_real, len);
1933 if (SvTAIL(float_real)) {
1934 if (memEQ(strend - len + 1, little, len - 1))
1935 last = strend - len + 1;
1936 else if (!PL_multiline)
1937 last = memEQ(strend - len, little, len)
1938 ? strend - len : Nullch;
1944 last = rninstr(s, strend, little, little + len);
1946 last = strend; /* matching `$' */
1950 DEBUG_r(PerlIO_printf(Perl_debug_log,
1951 "%sCan't trim the tail, match fails (should not happen)%s\n",
1952 PL_colors[4],PL_colors[5]));
1953 goto phooey; /* Should not happen! */
1955 dontbother = strend - last + prog->float_min_offset;
1957 if (minlen && (dontbother < minlen))
1958 dontbother = minlen - 1;
1959 strend -= dontbother; /* this one's always in bytes! */
1960 /* We don't know much -- general case. */
1963 if (regtry(prog, s))
1972 if (regtry(prog, s))
1974 } while (s++ < strend);
1982 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1984 if (PL_reg_eval_set) {
1985 /* Preserve the current value of $^R */
1986 if (oreplsv != GvSV(PL_replgv))
1987 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1988 restored, the value remains
1990 restore_pos(aTHX_ 0);
1993 /* make sure $`, $&, $', and $digit will work later */
1994 if ( !(flags & REXEC_NOT_FIRST) ) {
1995 if (RX_MATCH_COPIED(prog)) {
1996 Safefree(prog->subbeg);
1997 RX_MATCH_COPIED_off(prog);
1999 if (flags & REXEC_COPY_STR) {
2000 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2002 s = savepvn(strbeg, i);
2005 RX_MATCH_COPIED_on(prog);
2008 prog->subbeg = strbeg;
2009 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2016 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2017 PL_colors[4],PL_colors[5]));
2018 if (PL_reg_eval_set)
2019 restore_pos(aTHX_ 0);
2024 - regtry - try match at specific point
2026 STATIC I32 /* 0 failure, 1 success */
2027 S_regtry(pTHX_ regexp *prog, char *startpos)
2035 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2037 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2040 PL_reg_eval_set = RS_init;
2042 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2043 (IV)(PL_stack_sp - PL_stack_base));
2045 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2046 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2047 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2049 /* Apparently this is not needed, judging by wantarray. */
2050 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2051 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2054 /* Make $_ available to executed code. */
2055 if (PL_reg_sv != DEFSV) {
2056 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2061 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2062 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2063 /* prepare for quick setting of pos */
2064 sv_magic(PL_reg_sv, (SV*)0,
2065 PERL_MAGIC_regex_global, Nullch, 0);
2066 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2070 PL_reg_oldpos = mg->mg_len;
2071 SAVEDESTRUCTOR_X(restore_pos, 0);
2073 if (!PL_reg_curpm) {
2074 Newz(22,PL_reg_curpm, 1, PMOP);
2077 SV* repointer = newSViv(0);
2078 /* so we know which PL_regex_padav element is PL_reg_curpm */
2079 SvFLAGS(repointer) |= SVf_BREAK;
2080 av_push(PL_regex_padav,repointer);
2081 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2082 PL_regex_pad = AvARRAY(PL_regex_padav);
2086 PM_SETRE(PL_reg_curpm, prog);
2087 PL_reg_oldcurpm = PL_curpm;
2088 PL_curpm = PL_reg_curpm;
2089 if (RX_MATCH_COPIED(prog)) {
2090 /* Here is a serious problem: we cannot rewrite subbeg,
2091 since it may be needed if this match fails. Thus
2092 $` inside (?{}) could fail... */
2093 PL_reg_oldsaved = prog->subbeg;
2094 PL_reg_oldsavedlen = prog->sublen;
2095 RX_MATCH_COPIED_off(prog);
2098 PL_reg_oldsaved = Nullch;
2099 prog->subbeg = PL_bostr;
2100 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2102 prog->startp[0] = startpos - PL_bostr;
2103 PL_reginput = startpos;
2104 PL_regstartp = prog->startp;
2105 PL_regendp = prog->endp;
2106 PL_reglastparen = &prog->lastparen;
2107 PL_reglastcloseparen = &prog->lastcloseparen;
2108 prog->lastparen = 0;
2110 DEBUG_r(PL_reg_starttry = startpos);
2111 if (PL_reg_start_tmpl <= prog->nparens) {
2112 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2113 if(PL_reg_start_tmp)
2114 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2116 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2119 /* XXXX What this code is doing here?!!! There should be no need
2120 to do this again and again, PL_reglastparen should take care of
2123 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2124 * Actually, the code in regcppop() (which Ilya may be meaning by
2125 * PL_reglastparen), is not needed at all by the test suite
2126 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2127 * enough, for building DynaLoader, or otherwise this
2128 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2129 * will happen. Meanwhile, this code *is* needed for the
2130 * above-mentioned test suite tests to succeed. The common theme
2131 * on those tests seems to be returning null fields from matches.
2136 if (prog->nparens) {
2137 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2144 if (regmatch(prog->program + 1)) {
2145 prog->endp[0] = PL_reginput - PL_bostr;
2148 REGCP_UNWIND(lastcp);
2152 #define RE_UNWIND_BRANCH 1
2153 #define RE_UNWIND_BRANCHJ 2
2157 typedef struct { /* XX: makes sense to enlarge it... */
2161 } re_unwind_generic_t;
2174 } re_unwind_branch_t;
2176 typedef union re_unwind_t {
2178 re_unwind_generic_t generic;
2179 re_unwind_branch_t branch;
2182 #define sayYES goto yes
2183 #define sayNO goto no
2184 #define sayNO_ANYOF goto no_anyof
2185 #define sayYES_FINAL goto yes_final
2186 #define sayYES_LOUD goto yes_loud
2187 #define sayNO_FINAL goto no_final
2188 #define sayNO_SILENT goto do_no
2189 #define saySAME(x) if (x) goto yes; else goto no
2191 #define REPORT_CODE_OFF 24
2194 - regmatch - main matching routine
2196 * Conceptually the strategy is simple: check to see whether the current
2197 * node matches, call self recursively to see whether the rest matches,
2198 * and then act accordingly. In practice we make some effort to avoid
2199 * recursion, in particular by going through "ordinary" nodes (that don't
2200 * need to know whether the rest of the match failed) by a loop instead of
2203 /* [lwall] I've hoisted the register declarations to the outer block in order to
2204 * maybe save a little bit of pushing and popping on the stack. It also takes
2205 * advantage of machines that use a register save mask on subroutine entry.
2207 STATIC I32 /* 0 failure, 1 success */
2208 S_regmatch(pTHX_ regnode *prog)
2210 register regnode *scan; /* Current node. */
2211 regnode *next; /* Next node. */
2212 regnode *inner; /* Next node in internal branch. */
2213 register I32 nextchr; /* renamed nextchr - nextchar colides with
2214 function of same name */
2215 register I32 n; /* no or next */
2216 register I32 ln = 0; /* len or last */
2217 register char *s = Nullch; /* operand or save */
2218 register char *locinput = PL_reginput;
2219 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2220 int minmod = 0, sw = 0, logical = 0;
2223 I32 firstcp = PL_savestack_ix;
2225 register bool do_utf8 = PL_reg_match_utf8;
2227 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2228 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2229 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2236 /* Note that nextchr is a byte even in UTF */
2237 nextchr = UCHARAT(locinput);
2239 while (scan != NULL) {
2242 SV *prop = sv_newmortal();
2243 int docolor = *PL_colors[0];
2244 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2245 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2246 /* The part of the string before starttry has one color
2247 (pref0_len chars), between starttry and current
2248 position another one (pref_len - pref0_len chars),
2249 after the current position the third one.
2250 We assume that pref0_len <= pref_len, otherwise we
2251 decrease pref0_len. */
2252 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2253 ? (5 + taill) - l : locinput - PL_bostr;
2256 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2258 pref0_len = pref_len - (locinput - PL_reg_starttry);
2259 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2260 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2261 ? (5 + taill) - pref_len : PL_regeol - locinput);
2262 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2266 if (pref0_len > pref_len)
2267 pref0_len = pref_len;
2268 regprop(prop, scan);
2272 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2273 pref0_len, 60, UNI_DISPLAY_REGEX) :
2274 locinput - pref_len;
2275 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2276 char *s1 = do_utf8 ?
2277 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2278 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2279 locinput - pref_len + pref0_len;
2280 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2281 char *s2 = do_utf8 ?
2282 pv_uni_display(dsv2, (U8*)locinput,
2283 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2285 int len2 = do_utf8 ? strlen(s2) : l;
2286 PerlIO_printf(Perl_debug_log,
2287 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2288 (IV)(locinput - PL_bostr),
2295 (docolor ? "" : "> <"),
2299 15 - l - pref_len + 1,
2301 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2306 next = scan + NEXT_OFF(scan);
2312 if (locinput == PL_bostr || (PL_multiline &&
2313 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2315 /* regtill = regbol; */
2320 if (locinput == PL_bostr ||
2321 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2327 if (locinput == PL_bostr)
2331 if (locinput == PL_reg_ganch)
2341 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2346 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2348 if (PL_regeol - locinput > 1)
2352 if (PL_regeol != locinput)
2356 if (!nextchr && locinput >= PL_regeol)
2359 locinput += PL_utf8skip[nextchr];
2360 if (locinput > PL_regeol)
2362 nextchr = UCHARAT(locinput);
2365 nextchr = UCHARAT(++locinput);
2368 if (!nextchr && locinput >= PL_regeol)
2370 nextchr = UCHARAT(++locinput);
2373 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2376 locinput += PL_utf8skip[nextchr];
2377 if (locinput > PL_regeol)
2379 nextchr = UCHARAT(locinput);
2382 nextchr = UCHARAT(++locinput);
2387 if (do_utf8 != (UTF!=0)) {
2388 /* The target and the pattern have differing utf8ness. */
2394 /* The target is utf8, the pattern is not utf8. */
2398 if (NATIVE_TO_UNI(*(U8*)s) !=
2399 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2401 0 : UTF8_ALLOW_ANY))
2408 /* The target is not utf8, the pattern is utf8. */
2412 if (NATIVE_TO_UNI(*((U8*)l)) !=
2413 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2415 0 : UTF8_ALLOW_ANY))
2422 nextchr = UCHARAT(locinput);
2425 /* The target and the pattern have the same utf8ness. */
2426 /* Inline the first character, for speed. */
2427 if (UCHARAT(s) != nextchr)
2429 if (PL_regeol - locinput < ln)
2431 if (ln > 1 && memNE(s, locinput, ln))
2434 nextchr = UCHARAT(locinput);
2437 PL_reg_flags |= RF_tainted;
2443 if (do_utf8 || UTF) {
2444 /* Either target or the pattern are utf8. */
2446 char *e = PL_regeol;
2448 if (ibcmp_utf8(s, 0, ln, UTF,
2449 l, &e, 0, do_utf8)) {
2450 /* One more case for the sharp s:
2451 * pack("U0U*", 0xDF) =~ /ss/i,
2452 * the 0xC3 0x9F are the UTF-8
2453 * byte sequence for the U+00DF. */
2455 toLOWER(s[0]) == 's' &&
2457 toLOWER(s[1]) == 's' &&
2464 nextchr = UCHARAT(locinput);
2468 /* Neither the target and the pattern are utf8. */
2470 /* Inline the first character, for speed. */
2471 if (UCHARAT(s) != nextchr &&
2472 UCHARAT(s) != ((OP(scan) == EXACTF)
2473 ? PL_fold : PL_fold_locale)[nextchr])
2475 if (PL_regeol - locinput < ln)
2477 if (ln > 1 && (OP(scan) == EXACTF
2478 ? ibcmp(s, locinput, ln)
2479 : ibcmp_locale(s, locinput, ln)))
2482 nextchr = UCHARAT(locinput);
2486 STRLEN inclasslen = PL_regeol - locinput;
2488 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2490 if (locinput >= PL_regeol)
2492 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2493 nextchr = UCHARAT(locinput);
2498 nextchr = UCHARAT(locinput);
2499 if (!REGINCLASS(scan, (U8*)locinput))
2501 if (!nextchr && locinput >= PL_regeol)
2503 nextchr = UCHARAT(++locinput);
2507 /* If we might have the case of the German sharp s
2508 * in a casefolding Unicode character class. */
2510 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2511 locinput += SHARP_S_SKIP;
2512 nextchr = UCHARAT(locinput);
2518 PL_reg_flags |= RF_tainted;
2524 LOAD_UTF8_CHARCLASS(alnum,"a");
2525 if (!(OP(scan) == ALNUM
2526 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2527 : isALNUM_LC_utf8((U8*)locinput)))
2531 locinput += PL_utf8skip[nextchr];
2532 nextchr = UCHARAT(locinput);
2535 if (!(OP(scan) == ALNUM
2536 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2538 nextchr = UCHARAT(++locinput);
2541 PL_reg_flags |= RF_tainted;
2544 if (!nextchr && locinput >= PL_regeol)
2547 LOAD_UTF8_CHARCLASS(alnum,"a");
2548 if (OP(scan) == NALNUM
2549 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2550 : isALNUM_LC_utf8((U8*)locinput))
2554 locinput += PL_utf8skip[nextchr];
2555 nextchr = UCHARAT(locinput);
2558 if (OP(scan) == NALNUM
2559 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2561 nextchr = UCHARAT(++locinput);
2565 PL_reg_flags |= RF_tainted;
2569 /* was last char in word? */
2571 if (locinput == PL_bostr)
2574 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2576 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2578 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2579 ln = isALNUM_uni(ln);
2580 LOAD_UTF8_CHARCLASS(alnum,"a");
2581 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2584 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2585 n = isALNUM_LC_utf8((U8*)locinput);
2589 ln = (locinput != PL_bostr) ?
2590 UCHARAT(locinput - 1) : '\n';
2591 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2593 n = isALNUM(nextchr);
2596 ln = isALNUM_LC(ln);
2597 n = isALNUM_LC(nextchr);
2600 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2601 OP(scan) == BOUNDL))
2605 PL_reg_flags |= RF_tainted;
2611 if (UTF8_IS_CONTINUED(nextchr)) {
2612 LOAD_UTF8_CHARCLASS(space," ");
2613 if (!(OP(scan) == SPACE
2614 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2615 : isSPACE_LC_utf8((U8*)locinput)))
2619 locinput += PL_utf8skip[nextchr];
2620 nextchr = UCHARAT(locinput);
2623 if (!(OP(scan) == SPACE
2624 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2626 nextchr = UCHARAT(++locinput);
2629 if (!(OP(scan) == SPACE
2630 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2632 nextchr = UCHARAT(++locinput);
2636 PL_reg_flags |= RF_tainted;
2639 if (!nextchr && locinput >= PL_regeol)
2642 LOAD_UTF8_CHARCLASS(space," ");
2643 if (OP(scan) == NSPACE
2644 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2645 : isSPACE_LC_utf8((U8*)locinput))
2649 locinput += PL_utf8skip[nextchr];
2650 nextchr = UCHARAT(locinput);
2653 if (OP(scan) == NSPACE
2654 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2656 nextchr = UCHARAT(++locinput);
2659 PL_reg_flags |= RF_tainted;
2665 LOAD_UTF8_CHARCLASS(digit,"0");
2666 if (!(OP(scan) == DIGIT
2667 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2668 : isDIGIT_LC_utf8((U8*)locinput)))
2672 locinput += PL_utf8skip[nextchr];
2673 nextchr = UCHARAT(locinput);
2676 if (!(OP(scan) == DIGIT
2677 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2679 nextchr = UCHARAT(++locinput);
2682 PL_reg_flags |= RF_tainted;
2685 if (!nextchr && locinput >= PL_regeol)
2688 LOAD_UTF8_CHARCLASS(digit,"0");
2689 if (OP(scan) == NDIGIT
2690 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2691 : isDIGIT_LC_utf8((U8*)locinput))
2695 locinput += PL_utf8skip[nextchr];
2696 nextchr = UCHARAT(locinput);
2699 if (OP(scan) == NDIGIT
2700 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2702 nextchr = UCHARAT(++locinput);
2705 if (locinput >= PL_regeol)
2708 LOAD_UTF8_CHARCLASS(mark,"~");
2709 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2711 locinput += PL_utf8skip[nextchr];
2712 while (locinput < PL_regeol &&
2713 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2714 locinput += UTF8SKIP(locinput);
2715 if (locinput > PL_regeol)
2720 nextchr = UCHARAT(locinput);
2723 PL_reg_flags |= RF_tainted;
2727 n = ARG(scan); /* which paren pair */
2728 ln = PL_regstartp[n];
2729 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2730 if (*PL_reglastparen < n || ln == -1)
2731 sayNO; /* Do not match unless seen CLOSEn. */
2732 if (ln == PL_regendp[n])
2736 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2738 char *e = PL_bostr + PL_regendp[n];
2740 * Note that we can't do the "other character" lookup trick as
2741 * in the 8-bit case (no pun intended) because in Unicode we
2742 * have to map both upper and title case to lower case.
2744 if (OP(scan) == REFF) {
2745 STRLEN ulen1, ulen2;
2746 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2747 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2751 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2752 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2753 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2760 nextchr = UCHARAT(locinput);
2764 /* Inline the first character, for speed. */
2765 if (UCHARAT(s) != nextchr &&
2767 (UCHARAT(s) != ((OP(scan) == REFF
2768 ? PL_fold : PL_fold_locale)[nextchr]))))
2770 ln = PL_regendp[n] - ln;
2771 if (locinput + ln > PL_regeol)
2773 if (ln > 1 && (OP(scan) == REF
2774 ? memNE(s, locinput, ln)
2776 ? ibcmp(s, locinput, ln)
2777 : ibcmp_locale(s, locinput, ln))))
2780 nextchr = UCHARAT(locinput);
2791 OP_4tree *oop = PL_op;
2792 COP *ocurcop = PL_curcop;
2793 SV **ocurpad = PL_curpad;
2797 PL_op = (OP_4tree*)PL_regdata->data[n];
2798 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2799 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2800 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2804 CALLRUNOPS(aTHX); /* Scalar context. */
2807 ret = Nullsv; /* protect against empty (?{}) blocks. */
2815 PL_curpad = ocurpad;
2816 PL_curcop = ocurcop;
2818 if (logical == 2) { /* Postponed subexpression. */
2820 MAGIC *mg = Null(MAGIC*);
2822 CHECKPOINT cp, lastcp;
2824 if(SvROK(ret) || SvRMAGICAL(ret)) {
2825 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2828 mg = mg_find(sv, PERL_MAGIC_qr);
2831 re = (regexp *)mg->mg_obj;
2832 (void)ReREFCNT_inc(re);
2836 char *t = SvPV(ret, len);
2838 char *oprecomp = PL_regprecomp;
2839 I32 osize = PL_regsize;
2840 I32 onpar = PL_regnpar;
2843 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2845 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2846 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2848 PL_regprecomp = oprecomp;
2853 PerlIO_printf(Perl_debug_log,
2854 "Entering embedded `%s%.60s%s%s'\n",
2858 (strlen(re->precomp) > 60 ? "..." : ""))
2861 state.prev = PL_reg_call_cc;
2862 state.cc = PL_regcc;
2863 state.re = PL_reg_re;
2867 cp = regcppush(0); /* Save *all* the positions. */
2870 state.ss = PL_savestack_ix;
2871 *PL_reglastparen = 0;
2872 *PL_reglastcloseparen = 0;
2873 PL_reg_call_cc = &state;
2874 PL_reginput = locinput;
2876 /* XXXX This is too dramatic a measure... */
2879 if (regmatch(re->program + 1)) {
2880 /* Even though we succeeded, we need to restore
2881 global variables, since we may be wrapped inside
2882 SUSPEND, thus the match may be not finished yet. */
2884 /* XXXX Do this only if SUSPENDed? */
2885 PL_reg_call_cc = state.prev;
2886 PL_regcc = state.cc;
2887 PL_reg_re = state.re;
2888 cache_re(PL_reg_re);
2890 /* XXXX This is too dramatic a measure... */
2893 /* These are needed even if not SUSPEND. */
2899 REGCP_UNWIND(lastcp);
2901 PL_reg_call_cc = state.prev;
2902 PL_regcc = state.cc;
2903 PL_reg_re = state.re;
2904 cache_re(PL_reg_re);
2906 /* XXXX This is too dramatic a measure... */
2916 sv_setsv(save_scalar(PL_replgv), ret);
2920 n = ARG(scan); /* which paren pair */
2921 PL_reg_start_tmp[n] = locinput;
2926 n = ARG(scan); /* which paren pair */
2927 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2928 PL_regendp[n] = locinput - PL_bostr;
2929 if (n > *PL_reglastparen)
2930 *PL_reglastparen = n;
2931 *PL_reglastcloseparen = n;
2934 n = ARG(scan); /* which paren pair */
2935 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2938 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2940 next = NEXTOPER(NEXTOPER(scan));
2942 next = scan + ARG(scan);
2943 if (OP(next) == IFTHEN) /* Fake one. */
2944 next = NEXTOPER(NEXTOPER(next));
2948 logical = scan->flags;
2950 /*******************************************************************
2951 PL_regcc contains infoblock about the innermost (...)* loop, and
2952 a pointer to the next outer infoblock.
2954 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2956 1) After matching X, regnode for CURLYX is processed;
2958 2) This regnode creates infoblock on the stack, and calls
2959 regmatch() recursively with the starting point at WHILEM node;
2961 3) Each hit of WHILEM node tries to match A and Z (in the order
2962 depending on the current iteration, min/max of {min,max} and
2963 greediness). The information about where are nodes for "A"
2964 and "Z" is read from the infoblock, as is info on how many times "A"
2965 was already matched, and greediness.
2967 4) After A matches, the same WHILEM node is hit again.
2969 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2970 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2971 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2972 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2973 of the external loop.
2975 Currently present infoblocks form a tree with a stem formed by PL_curcc
2976 and whatever it mentions via ->next, and additional attached trees
2977 corresponding to temporarily unset infoblocks as in "5" above.
2979 In the following picture infoblocks for outer loop of
2980 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2981 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2982 infoblocks are drawn below the "reset" infoblock.
2984 In fact in the picture below we do not show failed matches for Z and T
2985 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2986 more obvious *why* one needs to *temporary* unset infoblocks.]
2988 Matched REx position InfoBlocks Comment
2992 Y A)*?Z)*?T x <- O <- I
2993 YA )*?Z)*?T x <- O <- I
2994 YA A)*?Z)*?T x <- O <- I
2995 YAA )*?Z)*?T x <- O <- I
2996 YAA Z)*?T x <- O # Temporary unset I
2999 YAAZ Y(A)*?Z)*?T x <- O
3002 YAAZY (A)*?Z)*?T x <- O
3005 YAAZY A)*?Z)*?T x <- O <- I
3008 YAAZYA )*?Z)*?T x <- O <- I
3011 YAAZYA Z)*?T x <- O # Temporary unset I
3017 YAAZYAZ T x # Temporary unset O
3024 *******************************************************************/
3027 CHECKPOINT cp = PL_savestack_ix;
3028 /* No need to save/restore up to this paren */
3029 I32 parenfloor = scan->flags;
3031 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3033 cc.oldcc = PL_regcc;
3035 /* XXXX Probably it is better to teach regpush to support
3036 parenfloor > PL_regsize... */
3037 if (parenfloor > *PL_reglastparen)
3038 parenfloor = *PL_reglastparen; /* Pessimization... */
3039 cc.parenfloor = parenfloor;
3041 cc.min = ARG1(scan);
3042 cc.max = ARG2(scan);
3043 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3047 PL_reginput = locinput;
3048 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3050 PL_regcc = cc.oldcc;
3056 * This is really hard to understand, because after we match
3057 * what we're trying to match, we must make sure the rest of
3058 * the REx is going to match for sure, and to do that we have
3059 * to go back UP the parse tree by recursing ever deeper. And
3060 * if it fails, we have to reset our parent's current state
3061 * that we can try again after backing off.
3064 CHECKPOINT cp, lastcp;
3065 CURCUR* cc = PL_regcc;
3066 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3068 n = cc->cur + 1; /* how many we know we matched */
3069 PL_reginput = locinput;
3072 PerlIO_printf(Perl_debug_log,
3073 "%*s %ld out of %ld..%ld cc=%lx\n",
3074 REPORT_CODE_OFF+PL_regindent*2, "",
3075 (long)n, (long)cc->min,
3076 (long)cc->max, (long)cc)
3079 /* If degenerate scan matches "", assume scan done. */
3081 if (locinput == cc->lastloc && n >= cc->min) {
3082 PL_regcc = cc->oldcc;
3086 PerlIO_printf(Perl_debug_log,
3087 "%*s empty match detected, try continuation...\n",
3088 REPORT_CODE_OFF+PL_regindent*2, "")
3090 if (regmatch(cc->next))
3098 /* First just match a string of min scans. */
3102 cc->lastloc = locinput;
3103 if (regmatch(cc->scan))
3106 cc->lastloc = lastloc;
3111 /* Check whether we already were at this position.
3112 Postpone detection until we know the match is not
3113 *that* much linear. */
3114 if (!PL_reg_maxiter) {
3115 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3116 PL_reg_leftiter = PL_reg_maxiter;
3118 if (PL_reg_leftiter-- == 0) {
3119 I32 size = (PL_reg_maxiter + 7)/8;
3120 if (PL_reg_poscache) {
3121 if (PL_reg_poscache_size < size) {
3122 Renew(PL_reg_poscache, size, char);
3123 PL_reg_poscache_size = size;
3125 Zero(PL_reg_poscache, size, char);
3128 PL_reg_poscache_size = size;
3129 Newz(29, PL_reg_poscache, size, char);
3132 PerlIO_printf(Perl_debug_log,
3133 "%sDetected a super-linear match, switching on caching%s...\n",
3134 PL_colors[4], PL_colors[5])
3137 if (PL_reg_leftiter < 0) {
3138 I32 o = locinput - PL_bostr, b;
3140 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3143 if (PL_reg_poscache[o] & (1<<b)) {
3145 PerlIO_printf(Perl_debug_log,
3146 "%*s already tried at this position...\n",
3147 REPORT_CODE_OFF+PL_regindent*2, "")
3151 PL_reg_poscache[o] |= (1<<b);
3155 /* Prefer next over scan for minimal matching. */
3158 PL_regcc = cc->oldcc;
3161 cp = regcppush(cc->parenfloor);
3163 if (regmatch(cc->next)) {
3165 sayYES; /* All done. */
3167 REGCP_UNWIND(lastcp);
3173 if (n >= cc->max) { /* Maximum greed exceeded? */
3174 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3175 && !(PL_reg_flags & RF_warned)) {
3176 PL_reg_flags |= RF_warned;
3177 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3178 "Complex regular subexpression recursion",
3185 PerlIO_printf(Perl_debug_log,
3186 "%*s trying longer...\n",
3187 REPORT_CODE_OFF+PL_regindent*2, "")
3189 /* Try scanning more and see if it helps. */
3190 PL_reginput = locinput;
3192 cc->lastloc = locinput;
3193 cp = regcppush(cc->parenfloor);
3195 if (regmatch(cc->scan)) {
3199 REGCP_UNWIND(lastcp);
3202 cc->lastloc = lastloc;
3206 /* Prefer scan over next for maximal matching. */
3208 if (n < cc->max) { /* More greed allowed? */
3209 cp = regcppush(cc->parenfloor);
3211 cc->lastloc = locinput;
3213 if (regmatch(cc->scan)) {
3217 REGCP_UNWIND(lastcp);
3218 regcppop(); /* Restore some previous $<digit>s? */
3219 PL_reginput = locinput;
3221 PerlIO_printf(Perl_debug_log,
3222 "%*s failed, try continuation...\n",
3223 REPORT_CODE_OFF+PL_regindent*2, "")
3226 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3227 && !(PL_reg_flags & RF_warned)) {
3228 PL_reg_flags |= RF_warned;
3229 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3230 "Complex regular subexpression recursion",
3234 /* Failed deeper matches of scan, so see if this one works. */
3235 PL_regcc = cc->oldcc;
3238 if (regmatch(cc->next))
3244 cc->lastloc = lastloc;
3249 next = scan + ARG(scan);
3252 inner = NEXTOPER(NEXTOPER(scan));
3255 inner = NEXTOPER(scan);
3259 if (OP(next) != c1) /* No choice. */
3260 next = inner; /* Avoid recursion. */
3262 I32 lastparen = *PL_reglastparen;
3264 re_unwind_branch_t *uw;
3266 /* Put unwinding data on stack */
3267 unwind1 = SSNEWt(1,re_unwind_branch_t);
3268 uw = SSPTRt(unwind1,re_unwind_branch_t);
3271 uw->type = ((c1 == BRANCH)
3273 : RE_UNWIND_BRANCHJ);
3274 uw->lastparen = lastparen;
3276 uw->locinput = locinput;
3277 uw->nextchr = nextchr;
3279 uw->regindent = ++PL_regindent;
3282 REGCP_SET(uw->lastcp);
3284 /* Now go into the first branch */
3297 /* We suppose that the next guy does not need
3298 backtracking: in particular, it is of constant length,
3299 and has no parenths to influence future backrefs. */
3300 ln = ARG1(scan); /* min to match */
3301 n = ARG2(scan); /* max to match */
3302 paren = scan->flags;
3304 if (paren > PL_regsize)
3306 if (paren > *PL_reglastparen)
3307 *PL_reglastparen = paren;
3309 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3311 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3312 PL_reginput = locinput;
3315 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3317 /* if we matched something zero-length we don't need to
3318 backtrack - capturing parens are already defined, so
3319 the caveat in the maximal case doesn't apply
3321 XXXX if ln == 0, we can redo this check first time
3322 through the following loop
3325 n = ln; /* don't backtrack */
3326 locinput = PL_reginput;
3327 if (HAS_TEXT(next) || JUMPABLE(next)) {
3328 regnode *text_node = next;
3330 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3332 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3334 if (PL_regkind[(U8)OP(text_node)] == REF) {
3336 n = ARG(text_node); /* which paren pair */
3337 ln = PL_regstartp[n];
3338 /* assume yes if we haven't seen CLOSEn */
3340 *PL_reglastparen < n ||
3347 c1 = *(PL_bostr + ln);
3349 else { c1 = (U8)*STRING(text_node); }
3350 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3352 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3353 c2 = PL_fold_locale[c1];
3362 /* This may be improved if l == 0. */
3363 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3364 /* If it could work, try it. */
3366 UCHARAT(PL_reginput) == c1 ||
3367 UCHARAT(PL_reginput) == c2)
3371 PL_regstartp[paren] =
3372 HOPc(PL_reginput, -l) - PL_bostr;
3373 PL_regendp[paren] = PL_reginput - PL_bostr;
3376 PL_regendp[paren] = -1;
3380 REGCP_UNWIND(lastcp);
3382 /* Couldn't or didn't -- move forward. */
3383 PL_reginput = locinput;
3384 if (regrepeat_hard(scan, 1, &l)) {
3386 locinput = PL_reginput;
3393 n = regrepeat_hard(scan, n, &l);
3394 /* if we matched something zero-length we don't need to
3395 backtrack, unless the minimum count is zero and we
3396 are capturing the result - in that case the capture
3397 being defined or not may affect later execution
3399 if (n != 0 && l == 0 && !(paren && ln == 0))
3400 ln = n; /* don't backtrack */
3401 locinput = PL_reginput;
3403 PerlIO_printf(Perl_debug_log,
3404 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3405 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3409 if (HAS_TEXT(next) || JUMPABLE(next)) {
3410 regnode *text_node = next;
3412 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3414 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3416 if (PL_regkind[(U8)OP(text_node)] == REF) {
3418 n = ARG(text_node); /* which paren pair */
3419 ln = PL_regstartp[n];
3420 /* assume yes if we haven't seen CLOSEn */
3422 *PL_reglastparen < n ||
3429 c1 = *(PL_bostr + ln);
3431 else { c1 = (U8)*STRING(text_node); }
3433 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3435 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3436 c2 = PL_fold_locale[c1];
3447 /* If it could work, try it. */
3449 UCHARAT(PL_reginput) == c1 ||
3450 UCHARAT(PL_reginput) == c2)
3453 PerlIO_printf(Perl_debug_log,
3454 "%*s trying tail with n=%"IVdf"...\n",
3455 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3459 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3460 PL_regendp[paren] = PL_reginput - PL_bostr;
3463 PL_regendp[paren] = -1;
3467 REGCP_UNWIND(lastcp);
3469 /* Couldn't or didn't -- back up. */
3471 locinput = HOPc(locinput, -l);
3472 PL_reginput = locinput;
3479 paren = scan->flags; /* Which paren to set */
3480 if (paren > PL_regsize)
3482 if (paren > *PL_reglastparen)
3483 *PL_reglastparen = paren;
3484 ln = ARG1(scan); /* min to match */
3485 n = ARG2(scan); /* max to match */
3486 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3490 ln = ARG1(scan); /* min to match */
3491 n = ARG2(scan); /* max to match */
3492 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3497 scan = NEXTOPER(scan);
3503 scan = NEXTOPER(scan);
3507 * Lookahead to avoid useless match attempts
3508 * when we know what character comes next.
3512 * Used to only do .*x and .*?x, but now it allows
3513 * for )'s, ('s and (?{ ... })'s to be in the way
3514 * of the quantifier and the EXACT-like node. -- japhy
3517 if (HAS_TEXT(next) || JUMPABLE(next)) {
3519 regnode *text_node = next;
3521 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3523 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3525 if (PL_regkind[(U8)OP(text_node)] == REF) {
3527 n = ARG(text_node); /* which paren pair */
3528 ln = PL_regstartp[n];
3529 /* assume yes if we haven't seen CLOSEn */
3531 *PL_reglastparen < n ||
3536 goto assume_ok_easy;
3538 s = (U8*)PL_bostr + ln;
3540 else { s = (U8*)STRING(text_node); }
3544 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3546 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3547 c2 = PL_fold_locale[c1];
3550 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3551 STRLEN ulen1, ulen2;
3552 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3553 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3555 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3556 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3558 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3560 0 : UTF8_ALLOW_ANY);
3561 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3563 0 : UTF8_ALLOW_ANY);
3566 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3568 0 : UTF8_ALLOW_ANY);
3576 PL_reginput = locinput;
3580 if (ln && regrepeat(scan, ln) < ln)
3582 locinput = PL_reginput;
3585 char *e; /* Should not check after this */
3586 char *old = locinput;
3589 if (n == REG_INFTY) {
3592 while (UTF8_IS_CONTINUATION(*(U8*)e))
3598 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3602 e = locinput + n - ln;
3607 /* Find place 'next' could work */
3610 while (locinput <= e &&
3611 UCHARAT(locinput) != c1)
3614 while (locinput <= e
3615 && UCHARAT(locinput) != c1
3616 && UCHARAT(locinput) != c2)
3619 count = locinput - old;
3624 /* count initialised to
3625 * utf8_distance(old, locinput) */
3626 while (locinput <= e &&
3627 utf8n_to_uvchr((U8*)locinput,
3630 0 : UTF8_ALLOW_ANY) != c1) {
3635 /* count initialised to
3636 * utf8_distance(old, locinput) */
3637 while (locinput <= e) {
3638 UV c = utf8n_to_uvchr((U8*)locinput,
3641 0 : UTF8_ALLOW_ANY);
3642 if (c == c1 || c == c2)
3651 /* PL_reginput == old now */
3652 if (locinput != old) {
3653 ln = 1; /* Did some */
3654 if (regrepeat(scan, count) < count)
3657 /* PL_reginput == locinput now */
3658 TRYPAREN(paren, ln, locinput);
3659 PL_reginput = locinput; /* Could be reset... */
3660 REGCP_UNWIND(lastcp);
3661 /* Couldn't or didn't -- move forward. */
3664 locinput += UTF8SKIP(locinput);
3671 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3675 c = utf8n_to_uvchr((U8*)PL_reginput,
3678 0 : UTF8_ALLOW_ANY);
3680 c = UCHARAT(PL_reginput);
3681 /* If it could work, try it. */
3682 if (c == c1 || c == c2)
3684 TRYPAREN(paren, n, PL_reginput);
3685 REGCP_UNWIND(lastcp);
3688 /* If it could work, try it. */
3689 else if (c1 == -1000)
3691 TRYPAREN(paren, n, PL_reginput);
3692 REGCP_UNWIND(lastcp);
3694 /* Couldn't or didn't -- move forward. */
3695 PL_reginput = locinput;
3696 if (regrepeat(scan, 1)) {
3698 locinput = PL_reginput;
3706 n = regrepeat(scan, n);
3707 locinput = PL_reginput;
3708 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3709 ((!PL_multiline && OP(next) != MEOL) ||
3710 OP(next) == SEOL || OP(next) == EOS))
3712 ln = n; /* why back off? */
3713 /* ...because $ and \Z can match before *and* after
3714 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3715 We should back off by one in this case. */
3716 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3725 c = utf8n_to_uvchr((U8*)PL_reginput,
3728 0 : UTF8_ALLOW_ANY);
3730 c = UCHARAT(PL_reginput);
3732 /* If it could work, try it. */
3733 if (c1 == -1000 || c == c1 || c == c2)
3735 TRYPAREN(paren, n, PL_reginput);
3736 REGCP_UNWIND(lastcp);
3738 /* Couldn't or didn't -- back up. */
3740 PL_reginput = locinput = HOPc(locinput, -1);
3748 c = utf8n_to_uvchr((U8*)PL_reginput,
3751 0 : UTF8_ALLOW_ANY);
3753 c = UCHARAT(PL_reginput);
3755 /* If it could work, try it. */
3756 if (c1 == -1000 || c == c1 || c == c2)
3758 TRYPAREN(paren, n, PL_reginput);
3759 REGCP_UNWIND(lastcp);
3761 /* Couldn't or didn't -- back up. */
3763 PL_reginput = locinput = HOPc(locinput, -1);
3770 if (PL_reg_call_cc) {
3771 re_cc_state *cur_call_cc = PL_reg_call_cc;
3772 CURCUR *cctmp = PL_regcc;
3773 regexp *re = PL_reg_re;
3774 CHECKPOINT cp, lastcp;
3776 cp = regcppush(0); /* Save *all* the positions. */
3778 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3780 PL_reginput = locinput; /* Make position available to
3782 cache_re(PL_reg_call_cc->re);
3783 PL_regcc = PL_reg_call_cc->cc;
3784 PL_reg_call_cc = PL_reg_call_cc->prev;
3785 if (regmatch(cur_call_cc->node)) {
3786 PL_reg_call_cc = cur_call_cc;
3790 REGCP_UNWIND(lastcp);
3792 PL_reg_call_cc = cur_call_cc;
3798 PerlIO_printf(Perl_debug_log,
3799 "%*s continuation failed...\n",
3800 REPORT_CODE_OFF+PL_regindent*2, "")
3804 if (locinput < PL_regtill) {
3805 DEBUG_r(PerlIO_printf(Perl_debug_log,
3806 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3808 (long)(locinput - PL_reg_starttry),
3809 (long)(PL_regtill - PL_reg_starttry),
3811 sayNO_FINAL; /* Cannot match: too short. */
3813 PL_reginput = locinput; /* put where regtry can find it */
3814 sayYES_FINAL; /* Success! */
3816 PL_reginput = locinput; /* put where regtry can find it */
3817 sayYES_LOUD; /* Success! */
3820 PL_reginput = locinput;
3825 s = HOPBACKc(locinput, scan->flags);
3831 PL_reginput = locinput;
3836 s = HOPBACKc(locinput, scan->flags);
3842 PL_reginput = locinput;
3845 inner = NEXTOPER(NEXTOPER(scan));
3846 if (regmatch(inner) != n) {
3861 if (OP(scan) == SUSPEND) {
3862 locinput = PL_reginput;
3863 nextchr = UCHARAT(locinput);
3868 next = scan + ARG(scan);
3873 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3874 PTR2UV(scan), OP(scan));
3875 Perl_croak(aTHX_ "regexp memory corruption");
3882 * We get here only if there's trouble -- normally "case END" is
3883 * the terminating point.
3885 Perl_croak(aTHX_ "corrupted regexp pointers");
3891 PerlIO_printf(Perl_debug_log,
3892 "%*s %scould match...%s\n",
3893 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3897 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3898 PL_colors[4],PL_colors[5]));
3904 #if 0 /* Breaks $^R */
3912 PerlIO_printf(Perl_debug_log,
3913 "%*s %sfailed...%s\n",
3914 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3920 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3923 case RE_UNWIND_BRANCH:
3924 case RE_UNWIND_BRANCHJ:
3926 re_unwind_branch_t *uwb = &(uw->branch);
3927 I32 lastparen = uwb->lastparen;
3929 REGCP_UNWIND(uwb->lastcp);
3930 for (n = *PL_reglastparen; n > lastparen; n--)
3932 *PL_reglastparen = n;
3933 scan = next = uwb->next;
3935 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3936 ? BRANCH : BRANCHJ) ) { /* Failure */
3943 /* Have more choice yet. Reuse the same uwb. */
3945 if ((n = (uwb->type == RE_UNWIND_BRANCH
3946 ? NEXT_OFF(next) : ARG(next))))
3949 next = NULL; /* XXXX Needn't unwinding in this case... */
3951 next = NEXTOPER(scan);
3952 if (uwb->type == RE_UNWIND_BRANCHJ)
3953 next = NEXTOPER(next);
3954 locinput = uwb->locinput;
3955 nextchr = uwb->nextchr;
3957 PL_regindent = uwb->regindent;
3964 Perl_croak(aTHX_ "regexp unwind memory corruption");
3975 - regrepeat - repeatedly match something simple, report how many
3978 * [This routine now assumes that it will only match on things of length 1.
3979 * That was true before, but now we assume scan - reginput is the count,
3980 * rather than incrementing count on every character. [Er, except utf8.]]
3983 S_regrepeat(pTHX_ regnode *p, I32 max)
3985 register char *scan;
3987 register char *loceol = PL_regeol;
3988 register I32 hardcount = 0;
3989 register bool do_utf8 = PL_reg_match_utf8;
3992 if (max != REG_INFTY && max < loceol - scan)
3993 loceol = scan + max;
3998 while (scan < loceol && hardcount < max && *scan != '\n') {
3999 scan += UTF8SKIP(scan);
4003 while (scan < loceol && *scan != '\n')
4010 while (scan < loceol && hardcount < max) {
4011 scan += UTF8SKIP(scan);
4021 case EXACT: /* length of string is 1 */
4023 while (scan < loceol && UCHARAT(scan) == c)
4026 case EXACTF: /* length of string is 1 */
4028 while (scan < loceol &&
4029 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4032 case EXACTFL: /* length of string is 1 */
4033 PL_reg_flags |= RF_tainted;
4035 while (scan < loceol &&
4036 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4042 while (hardcount < max && scan < loceol &&
4043 reginclass(p, (U8*)scan, 0, do_utf8)) {
4044 scan += UTF8SKIP(scan);
4048 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4055 LOAD_UTF8_CHARCLASS(alnum,"a");
4056 while (hardcount < max && scan < loceol &&
4057 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4058 scan += UTF8SKIP(scan);
4062 while (scan < loceol && isALNUM(*scan))
4067 PL_reg_flags |= RF_tainted;
4070 while (hardcount < max && scan < loceol &&
4071 isALNUM_LC_utf8((U8*)scan)) {
4072 scan += UTF8SKIP(scan);
4076 while (scan < loceol && isALNUM_LC(*scan))
4083 LOAD_UTF8_CHARCLASS(alnum,"a");
4084 while (hardcount < max && scan < loceol &&
4085 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4086 scan += UTF8SKIP(scan);
4090 while (scan < loceol && !isALNUM(*scan))
4095 PL_reg_flags |= RF_tainted;
4098 while (hardcount < max && scan < loceol &&
4099 !isALNUM_LC_utf8((U8*)scan)) {
4100 scan += UTF8SKIP(scan);
4104 while (scan < loceol && !isALNUM_LC(*scan))
4111 LOAD_UTF8_CHARCLASS(space," ");
4112 while (hardcount < max && scan < loceol &&
4114 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4115 scan += UTF8SKIP(scan);
4119 while (scan < loceol && isSPACE(*scan))
4124 PL_reg_flags |= RF_tainted;
4127 while (hardcount < max && scan < loceol &&
4128 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4129 scan += UTF8SKIP(scan);
4133 while (scan < loceol && isSPACE_LC(*scan))
4140 LOAD_UTF8_CHARCLASS(space," ");
4141 while (hardcount < max && scan < loceol &&
4143 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4144 scan += UTF8SKIP(scan);
4148 while (scan < loceol && !isSPACE(*scan))
4153 PL_reg_flags |= RF_tainted;
4156 while (hardcount < max && scan < loceol &&
4157 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4158 scan += UTF8SKIP(scan);
4162 while (scan < loceol && !isSPACE_LC(*scan))
4169 LOAD_UTF8_CHARCLASS(digit,"0");
4170 while (hardcount < max && scan < loceol &&
4171 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4172 scan += UTF8SKIP(scan);
4176 while (scan < loceol && isDIGIT(*scan))
4183 LOAD_UTF8_CHARCLASS(digit,"0");
4184 while (hardcount < max && scan < loceol &&
4185 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4186 scan += UTF8SKIP(scan);
4190 while (scan < loceol && !isDIGIT(*scan))
4194 default: /* Called on something of 0 width. */
4195 break; /* So match right here or not at all. */
4201 c = scan - PL_reginput;
4206 SV *prop = sv_newmortal();
4209 PerlIO_printf(Perl_debug_log,
4210 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4211 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4218 - regrepeat_hard - repeatedly match something, report total lenth and length
4220 * The repeater is supposed to have constant length.
4224 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4226 register char *scan = Nullch;
4227 register char *start;
4228 register char *loceol = PL_regeol;
4230 I32 count = 0, res = 1;
4235 start = PL_reginput;
4236 if (PL_reg_match_utf8) {
4237 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4240 while (start < PL_reginput) {
4242 start += UTF8SKIP(start);
4253 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4255 *lp = l = PL_reginput - start;
4256 if (max != REG_INFTY && l*max < loceol - scan)
4257 loceol = scan + l*max;
4270 - regclass_swash - prepare the utf8 swash
4274 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4280 if (PL_regdata && PL_regdata->count) {
4283 if (PL_regdata->what[n] == 's') {
4284 SV *rv = (SV*)PL_regdata->data[n];
4285 AV *av = (AV*)SvRV((SV*)rv);
4288 /* See the end of regcomp.c:S_reglass() for
4289 * documentation of these array elements. */
4291 si = *av_fetch(av, 0, FALSE);
4292 a = av_fetch(av, 1, FALSE);
4293 b = av_fetch(av, 2, FALSE);
4297 else if (si && doinit) {
4298 sw = swash_init("utf8", "", si, 1, 0);
4299 (void)av_store(av, 1, sw);
4315 - reginclass - determine if a character falls into a character class
4317 The n is the ANYOF regnode, the p is the target string, lenp
4318 is pointer to the maximum length of how far to go in the p
4319 (if the lenp is zero, UTF8SKIP(p) is used),
4320 do_utf8 tells whether the target string is in UTF-8.
4325 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4327 char flags = ANYOF_FLAGS(n);
4333 c = do_utf8 ? utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4334 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY) : *p;
4336 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4337 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4340 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4341 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4344 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4348 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4351 if (swash_fetch(sw, p, do_utf8))
4353 else if (flags & ANYOF_FOLD) {
4354 if (!match && lenp && av) {
4357 for (i = 0; i <= av_len(av); i++) {
4358 SV* sv = *av_fetch(av, i, FALSE);
4360 char *s = SvPV(sv, len);
4362 if (len <= plen && memEQ(s, (char*)p, len)) {
4370 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4373 to_utf8_fold(p, tmpbuf, &tmplen);
4374 if (swash_fetch(sw, tmpbuf, do_utf8))
4380 if (match && lenp && *lenp == 0)
4381 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4383 if (!match && c < 256) {
4384 if (ANYOF_BITMAP_TEST(n, c))
4386 else if (flags & ANYOF_FOLD) {
4389 if (flags & ANYOF_LOCALE) {
4390 PL_reg_flags |= RF_tainted;
4391 f = PL_fold_locale[c];
4395 if (f != c && ANYOF_BITMAP_TEST(n, f))
4399 if (!match && (flags & ANYOF_CLASS)) {
4400 PL_reg_flags |= RF_tainted;
4402 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4403 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4404 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4405 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4406 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4407 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4408 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4409 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4410 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4411 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4412 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4413 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4414 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4415 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4416 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4417 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4418 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4419 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4420 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4421 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4422 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4423 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4424 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4425 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4426 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4427 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4428 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4429 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4430 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4431 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4432 ) /* How's that for a conditional? */
4439 return (flags & ANYOF_INVERT) ? !match : match;
4443 S_reghop(pTHX_ U8 *s, I32 off)
4445 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4449 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4452 while (off-- && s < lim) {
4453 /* XXX could check well-formedness here */
4461 if (UTF8_IS_CONTINUED(*s)) {
4462 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4465 /* XXX could check well-formedness here */
4473 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4475 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4479 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4482 while (off-- && s < lim) {
4483 /* XXX could check well-formedness here */
4493 if (UTF8_IS_CONTINUED(*s)) {
4494 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4497 /* XXX could check well-formedness here */
4509 restore_pos(pTHX_ void *arg)
4511 if (PL_reg_eval_set) {
4512 if (PL_reg_oldsaved) {
4513 PL_reg_re->subbeg = PL_reg_oldsaved;
4514 PL_reg_re->sublen = PL_reg_oldsavedlen;
4515 RX_MATCH_COPIED_on(PL_reg_re);
4517 PL_reg_magic->mg_len = PL_reg_oldpos;
4518 PL_reg_eval_set = 0;
4519 PL_curpm = PL_reg_oldcurpm;
4524 S_to_utf8_substr(pTHX_ register regexp *prog)
4527 if (prog->float_substr && !prog->float_utf8) {
4528 prog->float_utf8 = sv = NEWSV(117, 0);
4529 SvSetMagicSV(sv, prog->float_substr);
4530 sv_utf8_upgrade(sv);
4531 if (SvTAIL(prog->float_substr))
4533 if (prog->float_substr == prog->check_substr)
4534 prog->check_utf8 = sv;
4536 if (prog->anchored_substr && !prog->anchored_utf8) {
4537 prog->anchored_utf8 = sv = NEWSV(118, 0);
4538 SvSetMagicSV(sv, prog->anchored_substr);
4539 sv_utf8_upgrade(sv);
4540 if (SvTAIL(prog->anchored_substr))
4542 if (prog->anchored_substr == prog->check_substr)
4543 prog->check_utf8 = sv;
4548 S_to_byte_substr(pTHX_ register regexp *prog)
4551 if (prog->float_utf8 && !prog->float_substr) {
4552 prog->float_substr = sv = NEWSV(117, 0);
4553 SvSetMagicSV(sv, prog->float_utf8);
4554 if (sv_utf8_downgrade(sv, TRUE)) {
4555 if (SvTAIL(prog->float_utf8))
4559 prog->float_substr = sv = &PL_sv_undef;
4561 if (prog->float_utf8 == prog->check_utf8)
4562 prog->check_substr = sv;
4564 if (prog->anchored_utf8 && !prog->anchored_substr) {
4565 prog->anchored_substr = sv = NEWSV(118, 0);
4566 SvSetMagicSV(sv, prog->anchored_utf8);
4567 if (sv_utf8_downgrade(sv, TRUE)) {
4568 if (SvTAIL(prog->anchored_utf8))
4572 prog->anchored_substr = sv = &PL_sv_undef;
4574 if (prog->anchored_utf8 == prog->check_utf8)
4575 prog->check_substr = sv;