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 */
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
107 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
108 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
110 #define HOPc(pos,off) ((char*)HOP(pos,off))
111 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
113 #define HOPBACK(pos, off) ( \
114 (PL_reg_match_utf8) \
115 ? reghopmaybe((U8*)pos, -off) \
116 : (pos - off >= PL_bostr) \
120 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
122 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
123 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
124 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
125 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
126 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
127 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
129 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) { ENTER; save_re_context(); (void)CAT2(is_utf8_, a)((U8*)b); LEAVE; } } STMT_END
131 /* for use after a quantifier and before an EXACT-like node -- japhy */
132 #define JUMPABLE(rn) ( \
133 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
134 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
135 OP(rn) == PLUS || OP(rn) == MINMOD || \
136 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
139 #define HAS_TEXT(rn) ( \
140 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
144 Search for mandatory following text node; for lookahead, the text must
145 follow but for lookbehind (rn->flags != 0) we skip to the next step.
147 #define FIND_NEXT_IMPT(rn) STMT_START { \
148 while (JUMPABLE(rn)) \
149 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
150 rn = NEXTOPER(NEXTOPER(rn)); \
151 else if (OP(rn) == PLUS) \
153 else if (OP(rn) == IFMATCH) \
154 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
155 else rn += NEXT_OFF(rn); \
158 static void restore_pos(pTHX_ void *arg);
161 S_regcppush(pTHX_ I32 parenfloor)
163 int retval = PL_savestack_ix;
164 #define REGCP_PAREN_ELEMS 4
165 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
168 if (paren_elems_to_push < 0)
169 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
171 #define REGCP_OTHER_ELEMS 6
172 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
173 for (p = PL_regsize; p > parenfloor; p--) {
174 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
175 SSPUSHINT(PL_regendp[p]);
176 SSPUSHINT(PL_regstartp[p]);
177 SSPUSHPTR(PL_reg_start_tmp[p]);
180 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
181 SSPUSHINT(PL_regsize);
182 SSPUSHINT(*PL_reglastparen);
183 SSPUSHINT(*PL_reglastcloseparen);
184 SSPUSHPTR(PL_reginput);
185 #define REGCP_FRAME_ELEMS 2
186 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
187 * are needed for the regexp context stack bookkeeping. */
188 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
189 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
194 /* These are needed since we do not localize EVAL nodes: */
195 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
196 " Setting an EVAL scope, savestack=%"IVdf"\n", \
197 (IV)PL_savestack_ix)); cp = PL_savestack_ix
199 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
200 PerlIO_printf(Perl_debug_log, \
201 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
202 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
212 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
214 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
215 i = SSPOPINT; /* Parentheses elements to pop. */
216 input = (char *) SSPOPPTR;
217 *PL_reglastcloseparen = SSPOPINT;
218 *PL_reglastparen = SSPOPINT;
219 PL_regsize = SSPOPINT;
221 /* Now restore the parentheses context. */
222 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
223 i > 0; i -= REGCP_PAREN_ELEMS) {
224 paren = (U32)SSPOPINT;
225 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
226 PL_regstartp[paren] = SSPOPINT;
228 if (paren <= *PL_reglastparen)
229 PL_regendp[paren] = tmps;
231 PerlIO_printf(Perl_debug_log,
232 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
233 (UV)paren, (IV)PL_regstartp[paren],
234 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
235 (IV)PL_regendp[paren],
236 (paren > *PL_reglastparen ? "(no)" : ""));
240 if (*PL_reglastparen + 1 <= PL_regnpar) {
241 PerlIO_printf(Perl_debug_log,
242 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
243 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
247 /* It would seem that the similar code in regtry()
248 * already takes care of this, and in fact it is in
249 * a better location to since this code can #if 0-ed out
250 * but the code in regtry() is needed or otherwise tests
251 * requiring null fields (pat.t#187 and split.t#{13,14}
252 * (as of patchlevel 7877) will fail. Then again,
253 * this code seems to be necessary or otherwise
254 * building DynaLoader will fail:
255 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
257 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
258 if (paren > PL_regsize)
259 PL_regstartp[paren] = -1;
260 PL_regendp[paren] = -1;
267 S_regcp_set_to(pTHX_ I32 ss)
269 I32 tmp = PL_savestack_ix;
271 PL_savestack_ix = ss;
273 PL_savestack_ix = tmp;
277 typedef struct re_cc_state
281 struct re_cc_state *prev;
286 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
288 #define TRYPAREN(paren, n, input) { \
291 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
292 PL_regendp[paren] = input - PL_bostr; \
295 PL_regendp[paren] = -1; \
297 if (regmatch(next)) \
300 PL_regendp[paren] = -1; \
305 * pregexec and friends
309 - pregexec - match a regexp against a string
312 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
313 char *strbeg, I32 minend, SV *screamer, U32 nosave)
314 /* strend: pointer to null at end of string */
315 /* strbeg: real beginning of string */
316 /* minend: end of match must be >=minend after stringarg. */
317 /* nosave: For optimizations. */
320 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
321 nosave ? 0 : REXEC_COPY_STR);
325 S_cache_re(pTHX_ regexp *prog)
327 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
329 PL_regprogram = prog->program;
331 PL_regnpar = prog->nparens;
332 PL_regdata = prog->data;
337 * Need to implement the following flags for reg_anch:
339 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
341 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
342 * INTUIT_AUTORITATIVE_ML
343 * INTUIT_ONCE_NOML - Intuit can match in one location only.
346 * Another flag for this function: SECOND_TIME (so that float substrs
347 * with giant delta may be not rechecked).
350 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
352 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
353 Otherwise, only SvCUR(sv) is used to get strbeg. */
355 /* XXXX We assume that strpos is strbeg unless sv. */
357 /* XXXX Some places assume that there is a fixed substring.
358 An update may be needed if optimizer marks as "INTUITable"
359 RExen without fixed substrings. Similarly, it is assumed that
360 lengths of all the strings are no more than minlen, thus they
361 cannot come from lookahead.
362 (Or minlen should take into account lookahead.) */
364 /* A failure to find a constant substring means that there is no need to make
365 an expensive call to REx engine, thus we celebrate a failure. Similarly,
366 finding a substring too deep into the string means that less calls to
367 regtry() should be needed.
369 REx compiler's optimizer found 4 possible hints:
370 a) Anchored substring;
372 c) Whether we are anchored (beginning-of-line or \G);
373 d) First node (of those at offset 0) which may distingush positions;
374 We use a)b)d) and multiline-part of c), and try to find a position in the
375 string which does not contradict any of them.
378 /* Most of decisions we do here should have been done at compile time.
379 The nodes of the REx which we used for the search should have been
380 deleted from the finite automaton. */
383 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
384 char *strend, U32 flags, re_scream_pos_data *data)
386 register I32 start_shift = 0;
387 /* Should be nonnegative! */
388 register I32 end_shift = 0;
394 register char *other_last = Nullch; /* other substr checked before this */
395 char *check_at = Nullch; /* check substr found at this pos */
397 char *i_strpos = strpos;
398 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
401 if (prog->reganch & ROPT_UTF8) {
402 DEBUG_r(PerlIO_printf(Perl_debug_log,
403 "UTF-8 regex...\n"));
404 PL_reg_flags |= RF_utf8;
408 char *s = PL_reg_match_utf8 ?
409 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
411 int len = PL_reg_match_utf8 ?
412 strlen(s) : strend - strpos;
415 if (PL_reg_match_utf8)
416 DEBUG_r(PerlIO_printf(Perl_debug_log,
417 "UTF-8 target...\n"));
418 PerlIO_printf(Perl_debug_log,
419 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
420 PL_colors[4],PL_colors[5],PL_colors[0],
423 (strlen(prog->precomp) > 60 ? "..." : ""),
425 (int)(len > 60 ? 60 : len),
427 (len > 60 ? "..." : "")
431 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
432 DEBUG_r(PerlIO_printf(Perl_debug_log,
433 "String too short... [re_intuit_start]\n"));
436 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
438 check = prog->check_substr;
439 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
440 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
441 || ( (prog->reganch & ROPT_ANCH_BOL)
442 && !PL_multiline ) ); /* Check after \n? */
445 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
446 | ROPT_IMPLICIT)) /* not a real BOL */
447 /* SvCUR is not set on references: SvRV and SvPVX overlap */
449 && (strpos != strbeg)) {
450 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
453 if (prog->check_offset_min == prog->check_offset_max &&
454 !(prog->reganch & ROPT_CANY_SEEN)) {
455 /* Substring at constant offset from beg-of-str... */
458 s = HOP3c(strpos, prog->check_offset_min, strend);
460 slen = SvCUR(check); /* >= 1 */
462 if ( strend - s > slen || strend - s < slen - 1
463 || (strend - s == slen && strend[-1] != '\n')) {
464 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
467 /* Now should match s[0..slen-2] */
469 if (slen && (*SvPVX(check) != *s
471 && memNE(SvPVX(check), s, slen)))) {
473 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
477 else if (*SvPVX(check) != *s
478 || ((slen = SvCUR(check)) > 1
479 && memNE(SvPVX(check), s, slen)))
481 goto success_at_start;
484 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
486 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
487 end_shift = prog->minlen - start_shift -
488 CHR_SVLEN(check) + (SvTAIL(check) != 0);
490 I32 end = prog->check_offset_max + CHR_SVLEN(check)
491 - (SvTAIL(check) != 0);
492 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
494 if (end_shift < eshift)
498 else { /* Can match at random position */
501 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
502 /* Should be nonnegative! */
503 end_shift = prog->minlen - start_shift -
504 CHR_SVLEN(check) + (SvTAIL(check) != 0);
507 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
509 Perl_croak(aTHX_ "panic: end_shift");
513 /* Find a possible match in the region s..strend by looking for
514 the "check" substring in the region corrected by start/end_shift. */
515 if (flags & REXEC_SCREAM) {
516 I32 p = -1; /* Internal iterator of scream. */
517 I32 *pp = data ? data->scream_pos : &p;
519 if (PL_screamfirst[BmRARE(check)] >= 0
520 || ( BmRARE(check) == '\n'
521 && (BmPREVIOUS(check) == SvCUR(check) - 1)
523 s = screaminstr(sv, check,
524 start_shift + (s - strbeg), end_shift, pp, 0);
528 *data->scream_olds = s;
530 else if (prog->reganch & ROPT_CANY_SEEN)
531 s = fbm_instr((U8*)(s + start_shift),
532 (U8*)(strend - end_shift),
533 check, PL_multiline ? FBMrf_MULTILINE : 0);
535 s = fbm_instr(HOP3(s, start_shift, strend),
536 HOP3(strend, -end_shift, strbeg),
537 check, PL_multiline ? FBMrf_MULTILINE : 0);
539 /* Update the count-of-usability, remove useless subpatterns,
542 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
543 (s ? "Found" : "Did not find"),
544 ((check == prog->anchored_substr) ? "anchored" : "floating"),
546 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
548 PL_colors[1], (SvTAIL(check) ? "$" : ""),
549 (s ? " at offset " : "...\n") ) );
556 /* Finish the diagnostic message */
557 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
559 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
560 Start with the other substr.
561 XXXX no SCREAM optimization yet - and a very coarse implementation
562 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
563 *always* match. Probably should be marked during compile...
564 Probably it is right to do no SCREAM here...
567 if (prog->float_substr && prog->anchored_substr) {
568 /* Take into account the "other" substring. */
569 /* XXXX May be hopelessly wrong for UTF... */
572 if (check == prog->float_substr) {
575 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
578 t = s - prog->check_offset_max;
579 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
580 && (!(prog->reganch & ROPT_UTF8)
581 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
586 t = HOP3c(t, prog->anchored_offset, strend);
587 if (t < other_last) /* These positions already checked */
589 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
592 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
593 /* On end-of-str: see comment below. */
594 s = fbm_instr((unsigned char*)t,
595 HOP3(HOP3(last1, prog->anchored_offset, strend)
596 + SvCUR(prog->anchored_substr),
597 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
598 prog->anchored_substr,
599 PL_multiline ? FBMrf_MULTILINE : 0);
600 DEBUG_r(PerlIO_printf(Perl_debug_log,
601 "%s anchored substr `%s%.*s%s'%s",
602 (s ? "Found" : "Contradicts"),
604 (int)(SvCUR(prog->anchored_substr)
605 - (SvTAIL(prog->anchored_substr)!=0)),
606 SvPVX(prog->anchored_substr),
607 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
609 if (last1 >= last2) {
610 DEBUG_r(PerlIO_printf(Perl_debug_log,
611 ", giving up...\n"));
614 DEBUG_r(PerlIO_printf(Perl_debug_log,
615 ", trying floating at offset %ld...\n",
616 (long)(HOP3c(s1, 1, strend) - i_strpos)));
617 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
618 s = HOP3c(last, 1, strend);
622 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
623 (long)(s - i_strpos)));
624 t = HOP3c(s, -prog->anchored_offset, strbeg);
625 other_last = HOP3c(s, 1, strend);
633 else { /* Take into account the floating substring. */
637 t = HOP3c(s, -start_shift, strbeg);
639 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
640 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
641 last = HOP3c(t, prog->float_max_offset, strend);
642 s = HOP3c(t, prog->float_min_offset, strend);
645 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
646 /* fbm_instr() takes into account exact value of end-of-str
647 if the check is SvTAIL(ed). Since false positives are OK,
648 and end-of-str is not later than strend we are OK. */
649 s = fbm_instr((unsigned char*)s,
650 (unsigned char*)last + SvCUR(prog->float_substr)
651 - (SvTAIL(prog->float_substr)!=0),
652 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
653 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
654 (s ? "Found" : "Contradicts"),
656 (int)(SvCUR(prog->float_substr)
657 - (SvTAIL(prog->float_substr)!=0)),
658 SvPVX(prog->float_substr),
659 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
662 DEBUG_r(PerlIO_printf(Perl_debug_log,
663 ", giving up...\n"));
666 DEBUG_r(PerlIO_printf(Perl_debug_log,
667 ", trying anchored starting at offset %ld...\n",
668 (long)(s1 + 1 - i_strpos)));
670 s = HOP3c(t, 1, strend);
674 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
675 (long)(s - i_strpos)));
676 other_last = s; /* Fix this later. --Hugo */
685 t = s - prog->check_offset_max;
686 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
687 && (!(prog->reganch & ROPT_UTF8)
688 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
690 /* Fixed substring is found far enough so that the match
691 cannot start at strpos. */
693 if (ml_anch && t[-1] != '\n') {
694 /* Eventually fbm_*() should handle this, but often
695 anchored_offset is not 0, so this check will not be wasted. */
696 /* XXXX In the code below we prefer to look for "^" even in
697 presence of anchored substrings. And we search even
698 beyond the found float position. These pessimizations
699 are historical artefacts only. */
701 while (t < strend - prog->minlen) {
703 if (t < check_at - prog->check_offset_min) {
704 if (prog->anchored_substr) {
705 /* Since we moved from the found position,
706 we definitely contradict the found anchored
707 substr. Due to the above check we do not
708 contradict "check" substr.
709 Thus we can arrive here only if check substr
710 is float. Redo checking for "other"=="fixed".
713 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
714 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
715 goto do_other_anchored;
717 /* We don't contradict the found floating substring. */
718 /* XXXX Why not check for STCLASS? */
720 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
721 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
724 /* Position contradicts check-string */
725 /* XXXX probably better to look for check-string
726 than for "\n", so one should lower the limit for t? */
727 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
728 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
729 other_last = strpos = s = t + 1;
734 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
735 PL_colors[0],PL_colors[1]));
739 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
740 PL_colors[0],PL_colors[1]));
744 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
747 /* The found string does not prohibit matching at strpos,
748 - no optimization of calling REx engine can be performed,
749 unless it was an MBOL and we are not after MBOL,
750 or a future STCLASS check will fail this. */
752 /* Even in this situation we may use MBOL flag if strpos is offset
753 wrt the start of the string. */
754 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
755 && (strpos != strbeg) && strpos[-1] != '\n'
756 /* May be due to an implicit anchor of m{.*foo} */
757 && !(prog->reganch & ROPT_IMPLICIT))
762 DEBUG_r( if (ml_anch)
763 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
764 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
767 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
768 && prog->check_substr /* Could be deleted already */
769 && --BmUSEFUL(prog->check_substr) < 0
770 && prog->check_substr == prog->float_substr)
772 /* If flags & SOMETHING - do not do it many times on the same match */
773 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
774 SvREFCNT_dec(prog->check_substr);
775 prog->check_substr = Nullsv; /* disable */
776 prog->float_substr = Nullsv; /* clear */
777 check = Nullsv; /* abort */
779 /* XXXX This is a remnant of the old implementation. It
780 looks wasteful, since now INTUIT can use many
782 prog->reganch &= ~RE_USE_INTUIT;
789 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
790 if (prog->regstclass) {
791 /* minlen == 0 is possible if regstclass is \b or \B,
792 and the fixed substr is ''$.
793 Since minlen is already taken into account, s+1 is before strend;
794 accidentally, minlen >= 1 guaranties no false positives at s + 1
795 even for \b or \B. But (minlen? 1 : 0) below assumes that
796 regstclass does not come from lookahead... */
797 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
798 This leaves EXACTF only, which is dealt with in find_byclass(). */
799 U8* str = (U8*)STRING(prog->regstclass);
800 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
801 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
803 char *endpos = (prog->anchored_substr || ml_anch)
804 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
805 : (prog->float_substr
806 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
809 char *startpos = strbeg;
812 if (prog->reganch & ROPT_UTF8) {
813 PL_regdata = prog->data;
816 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
821 if (endpos == strend) {
822 DEBUG_r( PerlIO_printf(Perl_debug_log,
823 "Could not match STCLASS...\n") );
826 DEBUG_r( PerlIO_printf(Perl_debug_log,
827 "This position contradicts STCLASS...\n") );
828 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
830 /* Contradict one of substrings */
831 if (prog->anchored_substr) {
832 if (prog->anchored_substr == check) {
833 DEBUG_r( what = "anchored" );
835 s = HOP3c(t, 1, strend);
836 if (s + start_shift + end_shift > strend) {
837 /* XXXX Should be taken into account earlier? */
838 DEBUG_r( PerlIO_printf(Perl_debug_log,
839 "Could not match STCLASS...\n") );
844 DEBUG_r( PerlIO_printf(Perl_debug_log,
845 "Looking for %s substr starting at offset %ld...\n",
846 what, (long)(s + start_shift - i_strpos)) );
849 /* Have both, check_string is floating */
850 if (t + start_shift >= check_at) /* Contradicts floating=check */
851 goto retry_floating_check;
852 /* Recheck anchored substring, but not floating... */
856 DEBUG_r( PerlIO_printf(Perl_debug_log,
857 "Looking for anchored substr starting at offset %ld...\n",
858 (long)(other_last - i_strpos)) );
859 goto do_other_anchored;
861 /* Another way we could have checked stclass at the
862 current position only: */
867 DEBUG_r( PerlIO_printf(Perl_debug_log,
868 "Looking for /%s^%s/m starting at offset %ld...\n",
869 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
872 if (!prog->float_substr) /* Could have been deleted */
874 /* Check is floating subtring. */
875 retry_floating_check:
876 t = check_at - start_shift;
877 DEBUG_r( what = "floating" );
878 goto hop_and_restart;
881 DEBUG_r(PerlIO_printf(Perl_debug_log,
882 "By STCLASS: moving %ld --> %ld\n",
883 (long)(t - i_strpos), (long)(s - i_strpos))
887 DEBUG_r(PerlIO_printf(Perl_debug_log,
888 "Does not contradict STCLASS...\n");
893 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
894 PL_colors[4], (check ? "Guessed" : "Giving up"),
895 PL_colors[5], (long)(s - i_strpos)) );
898 fail_finish: /* Substring not found */
899 if (prog->check_substr) /* could be removed already */
900 BmUSEFUL(prog->check_substr) += 5; /* hooray */
902 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
903 PL_colors[4],PL_colors[5]));
907 /* We know what class REx starts with. Try to find this position... */
909 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
911 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
917 register I32 tmp = 1; /* Scratch variable? */
918 register bool do_utf8 = PL_reg_match_utf8;
920 /* We know what class it must start with. */
924 STRLEN skip = do_utf8 ? UTF8SKIP(s) : 1;
926 if (reginclass(c, (U8*)s, do_utf8) ||
927 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
928 /* The assignment of 2 is intentional:
929 * for the sharp s, the skip is 2. */
930 (skip = SHARP_S_SKIP)
932 if (tmp && (norun || regtry(prog, s)))
944 if (tmp && (norun || regtry(prog, s)))
956 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
957 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
959 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
960 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
962 c1 = utf8_to_uvchr(tmpbuf1, 0);
963 c2 = utf8_to_uvchr(tmpbuf2, 0);
974 c2 = PL_fold_locale[c1];
976 e = do_utf8 ? s + ln : strend - ln;
979 e = s; /* Due to minlen logic of intuit() */
981 /* The idea in the EXACTF* cases is to first find the
982 * first character of the EXACTF* node and then, if
983 * necessary, case-insensitively compare the full
984 * text of the node. The c1 and c2 are the first
985 * characters (though in Unicode it gets a bit
986 * more complicated because there are more cases
987 * than just upper and lower: one needs to use
988 * the so-called folding case for case-insensitive
989 * matching (called "loose matching" in Unicode).
990 * ibcmp_utf8() will do just that. */
994 U8 tmpbuf [UTF8_MAXLEN+1];
995 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1000 c = utf8_to_uvchr((U8*)s, &len);
1003 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1004 m, (char **)0, ln, UTF))
1005 && (norun || regtry(prog, s)) )
1008 uvchr_to_utf8(tmpbuf, c);
1009 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1011 && (f == c1 || f == c2)
1012 && (ln == foldlen ||
1013 !ibcmp_utf8((char *) foldbuf,
1014 (char **)0, foldlen, do_utf8,
1016 (char **)0, ln, UTF))
1017 && (norun || regtry(prog, s)) )
1025 c = utf8_to_uvchr((U8*)s, &len);
1027 /* Handle some of the three Greek sigmas cases.
1028 * Note that not all the possible combinations
1029 * are handled here: some of them are handled
1030 * by the standard folding rules, and some of
1031 * them (the character class or ANYOF cases)
1032 * are handled during compiletime in
1033 * regexec.c:S_regclass(). */
1034 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1035 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1036 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1038 if ( (c == c1 || c == c2)
1040 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1041 m, (char **)0, ln, UTF))
1042 && (norun || regtry(prog, s)) )
1045 uvchr_to_utf8(tmpbuf, c);
1046 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1048 && (f == c1 || f == c2)
1049 && (ln == foldlen ||
1050 !ibcmp_utf8((char *) foldbuf,
1051 (char **)0, foldlen, do_utf8,
1053 (char **)0, ln, UTF))
1054 && (norun || regtry(prog, s)) )
1065 && (ln == 1 || !(OP(c) == EXACTF
1067 : ibcmp_locale(s, m, ln)))
1068 && (norun || regtry(prog, s)) )
1074 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1075 && (ln == 1 || !(OP(c) == EXACTF
1077 : ibcmp_locale(s, m, ln)))
1078 && (norun || regtry(prog, s)) )
1085 PL_reg_flags |= RF_tainted;
1092 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1095 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1097 tmp = ((OP(c) == BOUND ?
1098 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1099 LOAD_UTF8_CHARCLASS(alnum,"a");
1100 while (s < strend) {
1101 if (tmp == !(OP(c) == BOUND ?
1102 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1103 isALNUM_LC_utf8((U8*)s)))
1106 if ((norun || regtry(prog, s)))
1113 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1114 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1115 while (s < strend) {
1117 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1119 if ((norun || regtry(prog, s)))
1125 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1129 PL_reg_flags |= RF_tainted;
1136 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1139 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1141 tmp = ((OP(c) == NBOUND ?
1142 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1143 LOAD_UTF8_CHARCLASS(alnum,"a");
1144 while (s < strend) {
1145 if (tmp == !(OP(c) == NBOUND ?
1146 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1147 isALNUM_LC_utf8((U8*)s)))
1149 else if ((norun || regtry(prog, s)))
1155 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1156 tmp = ((OP(c) == NBOUND ?
1157 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1158 while (s < strend) {
1160 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1162 else if ((norun || regtry(prog, s)))
1167 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1172 LOAD_UTF8_CHARCLASS(alnum,"a");
1173 while (s < strend) {
1174 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1175 if (tmp && (norun || regtry(prog, s)))
1186 while (s < strend) {
1188 if (tmp && (norun || regtry(prog, s)))
1200 PL_reg_flags |= RF_tainted;
1202 while (s < strend) {
1203 if (isALNUM_LC_utf8((U8*)s)) {
1204 if (tmp && (norun || regtry(prog, s)))
1215 while (s < strend) {
1216 if (isALNUM_LC(*s)) {
1217 if (tmp && (norun || regtry(prog, s)))
1230 LOAD_UTF8_CHARCLASS(alnum,"a");
1231 while (s < strend) {
1232 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1233 if (tmp && (norun || regtry(prog, s)))
1244 while (s < strend) {
1246 if (tmp && (norun || regtry(prog, s)))
1258 PL_reg_flags |= RF_tainted;
1260 while (s < strend) {
1261 if (!isALNUM_LC_utf8((U8*)s)) {
1262 if (tmp && (norun || regtry(prog, s)))
1273 while (s < strend) {
1274 if (!isALNUM_LC(*s)) {
1275 if (tmp && (norun || regtry(prog, s)))
1288 LOAD_UTF8_CHARCLASS(space," ");
1289 while (s < strend) {
1290 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1291 if (tmp && (norun || regtry(prog, s)))
1302 while (s < strend) {
1304 if (tmp && (norun || regtry(prog, s)))
1316 PL_reg_flags |= RF_tainted;
1318 while (s < strend) {
1319 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1320 if (tmp && (norun || regtry(prog, s)))
1331 while (s < strend) {
1332 if (isSPACE_LC(*s)) {
1333 if (tmp && (norun || regtry(prog, s)))
1346 LOAD_UTF8_CHARCLASS(space," ");
1347 while (s < strend) {
1348 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1349 if (tmp && (norun || regtry(prog, s)))
1360 while (s < strend) {
1362 if (tmp && (norun || regtry(prog, s)))
1374 PL_reg_flags |= RF_tainted;
1376 while (s < strend) {
1377 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1378 if (tmp && (norun || regtry(prog, s)))
1389 while (s < strend) {
1390 if (!isSPACE_LC(*s)) {
1391 if (tmp && (norun || regtry(prog, s)))
1404 LOAD_UTF8_CHARCLASS(digit,"0");
1405 while (s < strend) {
1406 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1407 if (tmp && (norun || regtry(prog, s)))
1418 while (s < strend) {
1420 if (tmp && (norun || regtry(prog, s)))
1432 PL_reg_flags |= RF_tainted;
1434 while (s < strend) {
1435 if (isDIGIT_LC_utf8((U8*)s)) {
1436 if (tmp && (norun || regtry(prog, s)))
1447 while (s < strend) {
1448 if (isDIGIT_LC(*s)) {
1449 if (tmp && (norun || regtry(prog, s)))
1462 LOAD_UTF8_CHARCLASS(digit,"0");
1463 while (s < strend) {
1464 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1465 if (tmp && (norun || regtry(prog, s)))
1476 while (s < strend) {
1478 if (tmp && (norun || regtry(prog, s)))
1490 PL_reg_flags |= RF_tainted;
1492 while (s < strend) {
1493 if (!isDIGIT_LC_utf8((U8*)s)) {
1494 if (tmp && (norun || regtry(prog, s)))
1505 while (s < strend) {
1506 if (!isDIGIT_LC(*s)) {
1507 if (tmp && (norun || regtry(prog, s)))
1519 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1528 - regexec_flags - match a regexp against a string
1531 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1532 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1533 /* strend: pointer to null at end of string */
1534 /* strbeg: real beginning of string */
1535 /* minend: end of match must be >=minend after stringarg. */
1536 /* data: May be used for some additional optimizations. */
1537 /* nosave: For optimizations. */
1540 register regnode *c;
1541 register char *startpos = stringarg;
1542 I32 minlen; /* must match at least this many chars */
1543 I32 dontbother = 0; /* how many characters not to try at end */
1544 /* I32 start_shift = 0; */ /* Offset of the start to find
1545 constant substr. */ /* CC */
1546 I32 end_shift = 0; /* Same for the end. */ /* CC */
1547 I32 scream_pos = -1; /* Internal iterator of scream. */
1549 SV* oreplsv = GvSV(PL_replgv);
1550 bool do_utf8 = DO_UTF8(sv);
1552 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1553 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1560 PL_regnarrate = DEBUG_r_TEST;
1563 /* Be paranoid... */
1564 if (prog == NULL || startpos == NULL) {
1565 Perl_croak(aTHX_ "NULL regexp parameter");
1569 minlen = prog->minlen;
1570 if (strend - startpos < minlen) {
1571 DEBUG_r(PerlIO_printf(Perl_debug_log,
1572 "String too short [regexec_flags]...\n"));
1576 /* Check validity of program. */
1577 if (UCHARAT(prog->program) != REG_MAGIC) {
1578 Perl_croak(aTHX_ "corrupted regexp program");
1582 PL_reg_eval_set = 0;
1585 if (prog->reganch & ROPT_UTF8)
1586 PL_reg_flags |= RF_utf8;
1588 /* Mark beginning of line for ^ and lookbehind. */
1589 PL_regbol = startpos;
1593 /* Mark end of line for $ (and such) */
1596 /* see how far we have to get to not match where we matched before */
1597 PL_regtill = startpos+minend;
1599 /* We start without call_cc context. */
1602 /* If there is a "must appear" string, look for it. */
1605 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1608 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1609 PL_reg_ganch = startpos;
1610 else if (sv && SvTYPE(sv) >= SVt_PVMG
1612 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1613 && mg->mg_len >= 0) {
1614 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1615 if (prog->reganch & ROPT_ANCH_GPOS) {
1616 if (s > PL_reg_ganch)
1621 else /* pos() not defined */
1622 PL_reg_ganch = strbeg;
1625 if (do_utf8 == (UTF!=0) &&
1626 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1627 re_scream_pos_data d;
1629 d.scream_olds = &scream_olds;
1630 d.scream_pos = &scream_pos;
1631 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1633 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1634 goto phooey; /* not present */
1640 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1641 UNI_DISPLAY_REGEX) :
1643 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1644 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1645 UNI_DISPLAY_REGEX) : startpos;
1646 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1649 PerlIO_printf(Perl_debug_log,
1650 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1651 PL_colors[4],PL_colors[5],PL_colors[0],
1654 len0 > 60 ? "..." : "",
1656 (int)(len1 > 60 ? 60 : len1),
1658 (len1 > 60 ? "..." : "")
1662 /* Simplest case: anchored match need be tried only once. */
1663 /* [unless only anchor is BOL and multiline is set] */
1664 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1665 if (s == startpos && regtry(prog, startpos))
1667 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1668 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1673 dontbother = minlen - 1;
1674 end = HOP3c(strend, -dontbother, strbeg) - 1;
1675 /* for multiline we only have to try after newlines */
1676 if (prog->check_substr) {
1680 if (regtry(prog, s))
1685 if (prog->reganch & RE_USE_INTUIT) {
1686 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1697 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1698 if (regtry(prog, s))
1705 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1706 if (regtry(prog, PL_reg_ganch))
1711 /* Messy cases: unanchored match. */
1712 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1713 /* we have /x+whatever/ */
1714 /* it must be a one character string (XXXX Except UTF?) */
1715 char ch = SvPVX(prog->anchored_substr)[0];
1721 while (s < strend) {
1723 DEBUG_r( did_match = 1 );
1724 if (regtry(prog, s)) goto got_it;
1726 while (s < strend && *s == ch)
1733 while (s < strend) {
1735 DEBUG_r( did_match = 1 );
1736 if (regtry(prog, s)) goto got_it;
1738 while (s < strend && *s == ch)
1744 DEBUG_r(if (!did_match)
1745 PerlIO_printf(Perl_debug_log,
1746 "Did not find anchored character...\n")
1750 else if (do_utf8 == (UTF!=0) &&
1751 (prog->anchored_substr != Nullsv
1752 || (prog->float_substr != Nullsv
1753 && prog->float_max_offset < strend - s))) {
1754 SV *must = prog->anchored_substr
1755 ? prog->anchored_substr : prog->float_substr;
1757 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1759 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1760 char *last = HOP3c(strend, /* Cannot start after this */
1761 -(I32)(CHR_SVLEN(must)
1762 - (SvTAIL(must) != 0) + back_min), strbeg);
1763 char *last1; /* Last position checked before */
1769 last1 = HOPc(s, -1);
1771 last1 = s - 1; /* bogus */
1773 /* XXXX check_substr already used to find `s', can optimize if
1774 check_substr==must. */
1776 dontbother = end_shift;
1777 strend = HOPc(strend, -dontbother);
1778 while ( (s <= last) &&
1779 ((flags & REXEC_SCREAM)
1780 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1781 end_shift, &scream_pos, 0))
1782 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1783 (unsigned char*)strend, must,
1784 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1785 DEBUG_r( did_match = 1 );
1786 if (HOPc(s, -back_max) > last1) {
1787 last1 = HOPc(s, -back_min);
1788 s = HOPc(s, -back_max);
1791 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1793 last1 = HOPc(s, -back_min);
1797 while (s <= last1) {
1798 if (regtry(prog, s))
1804 while (s <= last1) {
1805 if (regtry(prog, s))
1811 DEBUG_r(if (!did_match)
1812 PerlIO_printf(Perl_debug_log,
1813 "Did not find %s substr `%s%.*s%s'%s...\n",
1814 ((must == prog->anchored_substr)
1815 ? "anchored" : "floating"),
1817 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1819 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1823 else if ((c = prog->regstclass)) {
1824 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1825 /* don't bother with what can't match */
1826 strend = HOPc(strend, -(minlen - 1));
1828 SV *prop = sv_newmortal();
1836 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1837 UNI_DISPLAY_REGEX) :
1839 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1841 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1842 len1 = UTF ? SvCUR(dsv1) : strend - s;
1843 PerlIO_printf(Perl_debug_log,
1844 "Matching stclass `%*.*s' against `%*.*s'\n",
1848 if (find_byclass(prog, c, s, strend, startpos, 0))
1850 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1854 if (prog->float_substr != Nullsv) { /* Trim the end. */
1857 if (flags & REXEC_SCREAM) {
1858 last = screaminstr(sv, prog->float_substr, s - strbeg,
1859 end_shift, &scream_pos, 1); /* last one */
1861 last = scream_olds; /* Only one occurrence. */
1865 char *little = SvPV(prog->float_substr, len);
1867 if (SvTAIL(prog->float_substr)) {
1868 if (memEQ(strend - len + 1, little, len - 1))
1869 last = strend - len + 1;
1870 else if (!PL_multiline)
1871 last = memEQ(strend - len, little, len)
1872 ? strend - len : Nullch;
1878 last = rninstr(s, strend, little, little + len);
1880 last = strend; /* matching `$' */
1884 DEBUG_r(PerlIO_printf(Perl_debug_log,
1885 "%sCan't trim the tail, match fails (should not happen)%s\n",
1886 PL_colors[4],PL_colors[5]));
1887 goto phooey; /* Should not happen! */
1889 dontbother = strend - last + prog->float_min_offset;
1891 if (minlen && (dontbother < minlen))
1892 dontbother = minlen - 1;
1893 strend -= dontbother; /* this one's always in bytes! */
1894 /* We don't know much -- general case. */
1897 if (regtry(prog, s))
1906 if (regtry(prog, s))
1908 } while (s++ < strend);
1916 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1918 if (PL_reg_eval_set) {
1919 /* Preserve the current value of $^R */
1920 if (oreplsv != GvSV(PL_replgv))
1921 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1922 restored, the value remains
1924 restore_pos(aTHX_ 0);
1927 /* make sure $`, $&, $', and $digit will work later */
1928 if ( !(flags & REXEC_NOT_FIRST) ) {
1929 if (RX_MATCH_COPIED(prog)) {
1930 Safefree(prog->subbeg);
1931 RX_MATCH_COPIED_off(prog);
1933 if (flags & REXEC_COPY_STR) {
1934 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1936 s = savepvn(strbeg, i);
1939 RX_MATCH_COPIED_on(prog);
1942 prog->subbeg = strbeg;
1943 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1950 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1951 PL_colors[4],PL_colors[5]));
1952 if (PL_reg_eval_set)
1953 restore_pos(aTHX_ 0);
1958 - regtry - try match at specific point
1960 STATIC I32 /* 0 failure, 1 success */
1961 S_regtry(pTHX_ regexp *prog, char *startpos)
1969 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1971 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1974 PL_reg_eval_set = RS_init;
1976 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1977 (IV)(PL_stack_sp - PL_stack_base));
1979 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1980 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1981 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1983 /* Apparently this is not needed, judging by wantarray. */
1984 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1985 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1988 /* Make $_ available to executed code. */
1989 if (PL_reg_sv != DEFSV) {
1990 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1995 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1996 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1997 /* prepare for quick setting of pos */
1998 sv_magic(PL_reg_sv, (SV*)0,
1999 PERL_MAGIC_regex_global, Nullch, 0);
2000 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2004 PL_reg_oldpos = mg->mg_len;
2005 SAVEDESTRUCTOR_X(restore_pos, 0);
2007 if (!PL_reg_curpm) {
2008 Newz(22,PL_reg_curpm, 1, PMOP);
2011 SV* repointer = newSViv(0);
2012 /* so we know which PL_regex_padav element is PL_reg_curpm */
2013 SvFLAGS(repointer) |= SVf_BREAK;
2014 av_push(PL_regex_padav,repointer);
2015 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2016 PL_regex_pad = AvARRAY(PL_regex_padav);
2020 PM_SETRE(PL_reg_curpm, prog);
2021 PL_reg_oldcurpm = PL_curpm;
2022 PL_curpm = PL_reg_curpm;
2023 if (RX_MATCH_COPIED(prog)) {
2024 /* Here is a serious problem: we cannot rewrite subbeg,
2025 since it may be needed if this match fails. Thus
2026 $` inside (?{}) could fail... */
2027 PL_reg_oldsaved = prog->subbeg;
2028 PL_reg_oldsavedlen = prog->sublen;
2029 RX_MATCH_COPIED_off(prog);
2032 PL_reg_oldsaved = Nullch;
2033 prog->subbeg = PL_bostr;
2034 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2036 prog->startp[0] = startpos - PL_bostr;
2037 PL_reginput = startpos;
2038 PL_regstartp = prog->startp;
2039 PL_regendp = prog->endp;
2040 PL_reglastparen = &prog->lastparen;
2041 PL_reglastcloseparen = &prog->lastcloseparen;
2042 prog->lastparen = 0;
2044 DEBUG_r(PL_reg_starttry = startpos);
2045 if (PL_reg_start_tmpl <= prog->nparens) {
2046 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2047 if(PL_reg_start_tmp)
2048 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2050 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2054 sv_setpvn(PERL_DEBUG_PAD(0), "", 0);
2055 sv_setpvn(PERL_DEBUG_PAD(1), "", 0);
2056 sv_setpvn(PERL_DEBUG_PAD(2), "", 0);
2059 /* XXXX What this code is doing here?!!! There should be no need
2060 to do this again and again, PL_reglastparen should take care of
2063 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2064 * Actually, the code in regcppop() (which Ilya may be meaning by
2065 * PL_reglastparen), is not needed at all by the test suite
2066 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2067 * enough, for building DynaLoader, or otherwise this
2068 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2069 * will happen. Meanwhile, this code *is* needed for the
2070 * above-mentioned test suite tests to succeed. The common theme
2071 * on those tests seems to be returning null fields from matches.
2076 if (prog->nparens) {
2077 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2084 if (regmatch(prog->program + 1)) {
2085 prog->endp[0] = PL_reginput - PL_bostr;
2088 REGCP_UNWIND(lastcp);
2092 #define RE_UNWIND_BRANCH 1
2093 #define RE_UNWIND_BRANCHJ 2
2097 typedef struct { /* XX: makes sense to enlarge it... */
2101 } re_unwind_generic_t;
2114 } re_unwind_branch_t;
2116 typedef union re_unwind_t {
2118 re_unwind_generic_t generic;
2119 re_unwind_branch_t branch;
2122 #define sayYES goto yes
2123 #define sayNO goto no
2124 #define sayNO_ANYOF goto no_anyof
2125 #define sayYES_FINAL goto yes_final
2126 #define sayYES_LOUD goto yes_loud
2127 #define sayNO_FINAL goto no_final
2128 #define sayNO_SILENT goto do_no
2129 #define saySAME(x) if (x) goto yes; else goto no
2131 #define REPORT_CODE_OFF 24
2134 - regmatch - main matching routine
2136 * Conceptually the strategy is simple: check to see whether the current
2137 * node matches, call self recursively to see whether the rest matches,
2138 * and then act accordingly. In practice we make some effort to avoid
2139 * recursion, in particular by going through "ordinary" nodes (that don't
2140 * need to know whether the rest of the match failed) by a loop instead of
2143 /* [lwall] I've hoisted the register declarations to the outer block in order to
2144 * maybe save a little bit of pushing and popping on the stack. It also takes
2145 * advantage of machines that use a register save mask on subroutine entry.
2147 STATIC I32 /* 0 failure, 1 success */
2148 S_regmatch(pTHX_ regnode *prog)
2150 register regnode *scan; /* Current node. */
2151 regnode *next; /* Next node. */
2152 regnode *inner; /* Next node in internal branch. */
2153 register I32 nextchr; /* renamed nextchr - nextchar colides with
2154 function of same name */
2155 register I32 n; /* no or next */
2156 register I32 ln = 0; /* len or last */
2157 register char *s = Nullch; /* operand or save */
2158 register char *locinput = PL_reginput;
2159 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2160 int minmod = 0, sw = 0, logical = 0;
2163 I32 firstcp = PL_savestack_ix;
2165 register bool do_utf8 = PL_reg_match_utf8;
2167 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2168 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2169 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2176 /* Note that nextchr is a byte even in UTF */
2177 nextchr = UCHARAT(locinput);
2179 while (scan != NULL) {
2182 SV *prop = sv_newmortal();
2183 int docolor = *PL_colors[0];
2184 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2185 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2186 /* The part of the string before starttry has one color
2187 (pref0_len chars), between starttry and current
2188 position another one (pref_len - pref0_len chars),
2189 after the current position the third one.
2190 We assume that pref0_len <= pref_len, otherwise we
2191 decrease pref0_len. */
2192 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2193 ? (5 + taill) - l : locinput - PL_bostr;
2196 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2198 pref0_len = pref_len - (locinput - PL_reg_starttry);
2199 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2200 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2201 ? (5 + taill) - pref_len : PL_regeol - locinput);
2202 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2206 if (pref0_len > pref_len)
2207 pref0_len = pref_len;
2208 regprop(prop, scan);
2212 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2213 pref0_len, 60, UNI_DISPLAY_REGEX) :
2214 locinput - pref_len;
2215 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2216 char *s1 = do_utf8 ?
2217 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2218 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2219 locinput - pref_len + pref0_len;
2220 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2221 char *s2 = do_utf8 ?
2222 pv_uni_display(dsv2, (U8*)locinput,
2223 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2225 int len2 = do_utf8 ? strlen(s2) : l;
2226 PerlIO_printf(Perl_debug_log,
2227 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2228 (IV)(locinput - PL_bostr),
2235 (docolor ? "" : "> <"),
2239 15 - l - pref_len + 1,
2241 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2246 next = scan + NEXT_OFF(scan);
2252 if (locinput == PL_bostr || (PL_multiline &&
2253 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2255 /* regtill = regbol; */
2260 if (locinput == PL_bostr ||
2261 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2267 if (locinput == PL_bostr)
2271 if (locinput == PL_reg_ganch)
2281 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2286 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2288 if (PL_regeol - locinput > 1)
2292 if (PL_regeol != locinput)
2296 if (!nextchr && locinput >= PL_regeol)
2299 locinput += PL_utf8skip[nextchr];
2300 if (locinput > PL_regeol)
2302 nextchr = UCHARAT(locinput);
2305 nextchr = UCHARAT(++locinput);
2308 if (!nextchr && locinput >= PL_regeol)
2310 nextchr = UCHARAT(++locinput);
2313 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2316 locinput += PL_utf8skip[nextchr];
2317 if (locinput > PL_regeol)
2319 nextchr = UCHARAT(locinput);
2322 nextchr = UCHARAT(++locinput);
2327 if (do_utf8 != (UTF!=0)) {
2328 /* The target and the pattern have differing utf8ness. */
2334 /* The target is utf8, the pattern is not utf8. */
2338 if (NATIVE_TO_UNI(*(U8*)s) !=
2339 utf8_to_uvuni((U8*)l, &ulen))
2346 /* The target is not utf8, the pattern is utf8. */
2350 if (NATIVE_TO_UNI(*((U8*)l)) !=
2351 utf8_to_uvuni((U8*)s, &ulen))
2358 nextchr = UCHARAT(locinput);
2361 /* The target and the pattern have the same utf8ness. */
2362 /* Inline the first character, for speed. */
2363 if (UCHARAT(s) != nextchr)
2365 if (PL_regeol - locinput < ln)
2367 if (ln > 1 && memNE(s, locinput, ln))
2370 nextchr = UCHARAT(locinput);
2373 PL_reg_flags |= RF_tainted;
2379 if (do_utf8 || UTF) {
2380 /* Either target or the pattern are utf8. */
2382 char *e = PL_regeol;
2384 if (ibcmp_utf8(s, 0, ln, UTF,
2385 l, &e, 0, do_utf8)) {
2386 /* One more case for the sharp s:
2387 * pack("U0U*", 0xDF) =~ /ss/i,
2388 * the 0xC3 0x9F are the UTF-8
2389 * byte sequence for the U+00DF. */
2391 toLOWER(s[0]) == 's' &&
2393 toLOWER(s[1]) == 's' &&
2400 nextchr = UCHARAT(locinput);
2404 /* Neither the target and the pattern are utf8. */
2406 /* Inline the first character, for speed. */
2407 if (UCHARAT(s) != nextchr &&
2408 UCHARAT(s) != ((OP(scan) == EXACTF)
2409 ? PL_fold : PL_fold_locale)[nextchr])
2411 if (PL_regeol - locinput < ln)
2413 if (ln > 1 && (OP(scan) == EXACTF
2414 ? ibcmp(s, locinput, ln)
2415 : ibcmp_locale(s, locinput, ln)))
2418 nextchr = UCHARAT(locinput);
2422 STRLEN inclasslen = PL_regeol - locinput;
2424 if (!reginclasslen(scan, (U8*)locinput, &inclasslen, do_utf8))
2426 if (locinput >= PL_regeol)
2428 locinput += inclasslen;
2429 nextchr = UCHARAT(locinput);
2434 nextchr = UCHARAT(locinput);
2435 if (!reginclass(scan, (U8*)locinput, do_utf8))
2437 if (!nextchr && locinput >= PL_regeol)
2439 nextchr = UCHARAT(++locinput);
2443 /* If we might have the case of the German sharp s
2444 * in a casefolding Unicode character class. */
2446 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2447 locinput += SHARP_S_SKIP;
2448 nextchr = UCHARAT(locinput);
2454 PL_reg_flags |= RF_tainted;
2460 LOAD_UTF8_CHARCLASS(alnum,"a");
2461 if (!(OP(scan) == ALNUM
2462 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2463 : isALNUM_LC_utf8((U8*)locinput)))
2467 locinput += PL_utf8skip[nextchr];
2468 nextchr = UCHARAT(locinput);
2471 if (!(OP(scan) == ALNUM
2472 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2474 nextchr = UCHARAT(++locinput);
2477 PL_reg_flags |= RF_tainted;
2480 if (!nextchr && locinput >= PL_regeol)
2483 LOAD_UTF8_CHARCLASS(alnum,"a");
2484 if (OP(scan) == NALNUM
2485 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2486 : isALNUM_LC_utf8((U8*)locinput))
2490 locinput += PL_utf8skip[nextchr];
2491 nextchr = UCHARAT(locinput);
2494 if (OP(scan) == NALNUM
2495 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2497 nextchr = UCHARAT(++locinput);
2501 PL_reg_flags |= RF_tainted;
2505 /* was last char in word? */
2507 if (locinput == PL_bostr)
2510 U8 *r = reghop((U8*)locinput, -1);
2512 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2514 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2515 ln = isALNUM_uni(ln);
2516 LOAD_UTF8_CHARCLASS(alnum,"a");
2517 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2520 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2521 n = isALNUM_LC_utf8((U8*)locinput);
2525 ln = (locinput != PL_bostr) ?
2526 UCHARAT(locinput - 1) : '\n';
2527 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2529 n = isALNUM(nextchr);
2532 ln = isALNUM_LC(ln);
2533 n = isALNUM_LC(nextchr);
2536 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2537 OP(scan) == BOUNDL))
2541 PL_reg_flags |= RF_tainted;
2547 if (UTF8_IS_CONTINUED(nextchr)) {
2548 LOAD_UTF8_CHARCLASS(space," ");
2549 if (!(OP(scan) == SPACE
2550 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2551 : isSPACE_LC_utf8((U8*)locinput)))
2555 locinput += PL_utf8skip[nextchr];
2556 nextchr = UCHARAT(locinput);
2559 if (!(OP(scan) == SPACE
2560 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2562 nextchr = UCHARAT(++locinput);
2565 if (!(OP(scan) == SPACE
2566 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2568 nextchr = UCHARAT(++locinput);
2572 PL_reg_flags |= RF_tainted;
2575 if (!nextchr && locinput >= PL_regeol)
2578 LOAD_UTF8_CHARCLASS(space," ");
2579 if (OP(scan) == NSPACE
2580 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2581 : isSPACE_LC_utf8((U8*)locinput))
2585 locinput += PL_utf8skip[nextchr];
2586 nextchr = UCHARAT(locinput);
2589 if (OP(scan) == NSPACE
2590 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2592 nextchr = UCHARAT(++locinput);
2595 PL_reg_flags |= RF_tainted;
2601 LOAD_UTF8_CHARCLASS(digit,"0");
2602 if (!(OP(scan) == DIGIT
2603 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2604 : isDIGIT_LC_utf8((U8*)locinput)))
2608 locinput += PL_utf8skip[nextchr];
2609 nextchr = UCHARAT(locinput);
2612 if (!(OP(scan) == DIGIT
2613 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2615 nextchr = UCHARAT(++locinput);
2618 PL_reg_flags |= RF_tainted;
2621 if (!nextchr && locinput >= PL_regeol)
2624 LOAD_UTF8_CHARCLASS(digit,"0");
2625 if (OP(scan) == NDIGIT
2626 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2627 : isDIGIT_LC_utf8((U8*)locinput))
2631 locinput += PL_utf8skip[nextchr];
2632 nextchr = UCHARAT(locinput);
2635 if (OP(scan) == NDIGIT
2636 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2638 nextchr = UCHARAT(++locinput);
2641 if (locinput >= PL_regeol)
2644 LOAD_UTF8_CHARCLASS(mark,"~");
2645 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2647 locinput += PL_utf8skip[nextchr];
2648 while (locinput < PL_regeol &&
2649 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2650 locinput += UTF8SKIP(locinput);
2651 if (locinput > PL_regeol)
2656 nextchr = UCHARAT(locinput);
2659 PL_reg_flags |= RF_tainted;
2663 n = ARG(scan); /* which paren pair */
2664 ln = PL_regstartp[n];
2665 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2666 if (*PL_reglastparen < n || ln == -1)
2667 sayNO; /* Do not match unless seen CLOSEn. */
2668 if (ln == PL_regendp[n])
2672 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2674 char *e = PL_bostr + PL_regendp[n];
2676 * Note that we can't do the "other character" lookup trick as
2677 * in the 8-bit case (no pun intended) because in Unicode we
2678 * have to map both upper and title case to lower case.
2680 if (OP(scan) == REFF) {
2681 STRLEN ulen1, ulen2;
2682 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2683 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2687 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2688 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2689 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2696 nextchr = UCHARAT(locinput);
2700 /* Inline the first character, for speed. */
2701 if (UCHARAT(s) != nextchr &&
2703 (UCHARAT(s) != ((OP(scan) == REFF
2704 ? PL_fold : PL_fold_locale)[nextchr]))))
2706 ln = PL_regendp[n] - ln;
2707 if (locinput + ln > PL_regeol)
2709 if (ln > 1 && (OP(scan) == REF
2710 ? memNE(s, locinput, ln)
2712 ? ibcmp(s, locinput, ln)
2713 : ibcmp_locale(s, locinput, ln))))
2716 nextchr = UCHARAT(locinput);
2727 OP_4tree *oop = PL_op;
2728 COP *ocurcop = PL_curcop;
2729 SV **ocurpad = PL_curpad;
2733 PL_op = (OP_4tree*)PL_regdata->data[n];
2734 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2735 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2736 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2740 CALLRUNOPS(aTHX); /* Scalar context. */
2743 ret = Nullsv; /* protect against empty (?{}) blocks. */
2751 PL_curpad = ocurpad;
2752 PL_curcop = ocurcop;
2754 if (logical == 2) { /* Postponed subexpression. */
2756 MAGIC *mg = Null(MAGIC*);
2758 CHECKPOINT cp, lastcp;
2760 if(SvROK(ret) || SvRMAGICAL(ret)) {
2761 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2764 mg = mg_find(sv, PERL_MAGIC_qr);
2767 re = (regexp *)mg->mg_obj;
2768 (void)ReREFCNT_inc(re);
2772 char *t = SvPV(ret, len);
2774 char *oprecomp = PL_regprecomp;
2775 I32 osize = PL_regsize;
2776 I32 onpar = PL_regnpar;
2779 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2781 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2782 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2784 PL_regprecomp = oprecomp;
2789 PerlIO_printf(Perl_debug_log,
2790 "Entering embedded `%s%.60s%s%s'\n",
2794 (strlen(re->precomp) > 60 ? "..." : ""))
2797 state.prev = PL_reg_call_cc;
2798 state.cc = PL_regcc;
2799 state.re = PL_reg_re;
2803 cp = regcppush(0); /* Save *all* the positions. */
2806 state.ss = PL_savestack_ix;
2807 *PL_reglastparen = 0;
2808 *PL_reglastcloseparen = 0;
2809 PL_reg_call_cc = &state;
2810 PL_reginput = locinput;
2812 /* XXXX This is too dramatic a measure... */
2815 if (regmatch(re->program + 1)) {
2816 /* Even though we succeeded, we need to restore
2817 global variables, since we may be wrapped inside
2818 SUSPEND, thus the match may be not finished yet. */
2820 /* XXXX Do this only if SUSPENDed? */
2821 PL_reg_call_cc = state.prev;
2822 PL_regcc = state.cc;
2823 PL_reg_re = state.re;
2824 cache_re(PL_reg_re);
2826 /* XXXX This is too dramatic a measure... */
2829 /* These are needed even if not SUSPEND. */
2835 REGCP_UNWIND(lastcp);
2837 PL_reg_call_cc = state.prev;
2838 PL_regcc = state.cc;
2839 PL_reg_re = state.re;
2840 cache_re(PL_reg_re);
2842 /* XXXX This is too dramatic a measure... */
2852 sv_setsv(save_scalar(PL_replgv), ret);
2856 n = ARG(scan); /* which paren pair */
2857 PL_reg_start_tmp[n] = locinput;
2862 n = ARG(scan); /* which paren pair */
2863 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2864 PL_regendp[n] = locinput - PL_bostr;
2865 if (n > *PL_reglastparen)
2866 *PL_reglastparen = n;
2867 *PL_reglastcloseparen = n;
2870 n = ARG(scan); /* which paren pair */
2871 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2874 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2876 next = NEXTOPER(NEXTOPER(scan));
2878 next = scan + ARG(scan);
2879 if (OP(next) == IFTHEN) /* Fake one. */
2880 next = NEXTOPER(NEXTOPER(next));
2884 logical = scan->flags;
2886 /*******************************************************************
2887 PL_regcc contains infoblock about the innermost (...)* loop, and
2888 a pointer to the next outer infoblock.
2890 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2892 1) After matching X, regnode for CURLYX is processed;
2894 2) This regnode creates infoblock on the stack, and calls
2895 regmatch() recursively with the starting point at WHILEM node;
2897 3) Each hit of WHILEM node tries to match A and Z (in the order
2898 depending on the current iteration, min/max of {min,max} and
2899 greediness). The information about where are nodes for "A"
2900 and "Z" is read from the infoblock, as is info on how many times "A"
2901 was already matched, and greediness.
2903 4) After A matches, the same WHILEM node is hit again.
2905 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2906 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2907 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2908 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2909 of the external loop.
2911 Currently present infoblocks form a tree with a stem formed by PL_curcc
2912 and whatever it mentions via ->next, and additional attached trees
2913 corresponding to temporarily unset infoblocks as in "5" above.
2915 In the following picture infoblocks for outer loop of
2916 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2917 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2918 infoblocks are drawn below the "reset" infoblock.
2920 In fact in the picture below we do not show failed matches for Z and T
2921 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2922 more obvious *why* one needs to *temporary* unset infoblocks.]
2924 Matched REx position InfoBlocks Comment
2928 Y A)*?Z)*?T x <- O <- I
2929 YA )*?Z)*?T x <- O <- I
2930 YA A)*?Z)*?T x <- O <- I
2931 YAA )*?Z)*?T x <- O <- I
2932 YAA Z)*?T x <- O # Temporary unset I
2935 YAAZ Y(A)*?Z)*?T x <- O
2938 YAAZY (A)*?Z)*?T x <- O
2941 YAAZY A)*?Z)*?T x <- O <- I
2944 YAAZYA )*?Z)*?T x <- O <- I
2947 YAAZYA Z)*?T x <- O # Temporary unset I
2953 YAAZYAZ T x # Temporary unset O
2960 *******************************************************************/
2963 CHECKPOINT cp = PL_savestack_ix;
2964 /* No need to save/restore up to this paren */
2965 I32 parenfloor = scan->flags;
2967 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2969 cc.oldcc = PL_regcc;
2971 /* XXXX Probably it is better to teach regpush to support
2972 parenfloor > PL_regsize... */
2973 if (parenfloor > *PL_reglastparen)
2974 parenfloor = *PL_reglastparen; /* Pessimization... */
2975 cc.parenfloor = parenfloor;
2977 cc.min = ARG1(scan);
2978 cc.max = ARG2(scan);
2979 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2983 PL_reginput = locinput;
2984 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2986 PL_regcc = cc.oldcc;
2992 * This is really hard to understand, because after we match
2993 * what we're trying to match, we must make sure the rest of
2994 * the REx is going to match for sure, and to do that we have
2995 * to go back UP the parse tree by recursing ever deeper. And
2996 * if it fails, we have to reset our parent's current state
2997 * that we can try again after backing off.
3000 CHECKPOINT cp, lastcp;
3001 CURCUR* cc = PL_regcc;
3002 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3004 n = cc->cur + 1; /* how many we know we matched */
3005 PL_reginput = locinput;
3008 PerlIO_printf(Perl_debug_log,
3009 "%*s %ld out of %ld..%ld cc=%lx\n",
3010 REPORT_CODE_OFF+PL_regindent*2, "",
3011 (long)n, (long)cc->min,
3012 (long)cc->max, (long)cc)
3015 /* If degenerate scan matches "", assume scan done. */
3017 if (locinput == cc->lastloc && n >= cc->min) {
3018 PL_regcc = cc->oldcc;
3022 PerlIO_printf(Perl_debug_log,
3023 "%*s empty match detected, try continuation...\n",
3024 REPORT_CODE_OFF+PL_regindent*2, "")
3026 if (regmatch(cc->next))
3034 /* First just match a string of min scans. */
3038 cc->lastloc = locinput;
3039 if (regmatch(cc->scan))
3042 cc->lastloc = lastloc;
3047 /* Check whether we already were at this position.
3048 Postpone detection until we know the match is not
3049 *that* much linear. */
3050 if (!PL_reg_maxiter) {
3051 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3052 PL_reg_leftiter = PL_reg_maxiter;
3054 if (PL_reg_leftiter-- == 0) {
3055 I32 size = (PL_reg_maxiter + 7)/8;
3056 if (PL_reg_poscache) {
3057 if (PL_reg_poscache_size < size) {
3058 Renew(PL_reg_poscache, size, char);
3059 PL_reg_poscache_size = size;
3061 Zero(PL_reg_poscache, size, char);
3064 PL_reg_poscache_size = size;
3065 Newz(29, PL_reg_poscache, size, char);
3068 PerlIO_printf(Perl_debug_log,
3069 "%sDetected a super-linear match, switching on caching%s...\n",
3070 PL_colors[4], PL_colors[5])
3073 if (PL_reg_leftiter < 0) {
3074 I32 o = locinput - PL_bostr, b;
3076 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3079 if (PL_reg_poscache[o] & (1<<b)) {
3081 PerlIO_printf(Perl_debug_log,
3082 "%*s already tried at this position...\n",
3083 REPORT_CODE_OFF+PL_regindent*2, "")
3087 PL_reg_poscache[o] |= (1<<b);
3091 /* Prefer next over scan for minimal matching. */
3094 PL_regcc = cc->oldcc;
3097 cp = regcppush(cc->parenfloor);
3099 if (regmatch(cc->next)) {
3101 sayYES; /* All done. */
3103 REGCP_UNWIND(lastcp);
3109 if (n >= cc->max) { /* Maximum greed exceeded? */
3110 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3111 && !(PL_reg_flags & RF_warned)) {
3112 PL_reg_flags |= RF_warned;
3113 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3114 "Complex regular subexpression recursion",
3121 PerlIO_printf(Perl_debug_log,
3122 "%*s trying longer...\n",
3123 REPORT_CODE_OFF+PL_regindent*2, "")
3125 /* Try scanning more and see if it helps. */
3126 PL_reginput = locinput;
3128 cc->lastloc = locinput;
3129 cp = regcppush(cc->parenfloor);
3131 if (regmatch(cc->scan)) {
3135 REGCP_UNWIND(lastcp);
3138 cc->lastloc = lastloc;
3142 /* Prefer scan over next for maximal matching. */
3144 if (n < cc->max) { /* More greed allowed? */
3145 cp = regcppush(cc->parenfloor);
3147 cc->lastloc = locinput;
3149 if (regmatch(cc->scan)) {
3153 REGCP_UNWIND(lastcp);
3154 regcppop(); /* Restore some previous $<digit>s? */
3155 PL_reginput = locinput;
3157 PerlIO_printf(Perl_debug_log,
3158 "%*s failed, try continuation...\n",
3159 REPORT_CODE_OFF+PL_regindent*2, "")
3162 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3163 && !(PL_reg_flags & RF_warned)) {
3164 PL_reg_flags |= RF_warned;
3165 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3166 "Complex regular subexpression recursion",
3170 /* Failed deeper matches of scan, so see if this one works. */
3171 PL_regcc = cc->oldcc;
3174 if (regmatch(cc->next))
3180 cc->lastloc = lastloc;
3185 next = scan + ARG(scan);
3188 inner = NEXTOPER(NEXTOPER(scan));
3191 inner = NEXTOPER(scan);
3195 if (OP(next) != c1) /* No choice. */
3196 next = inner; /* Avoid recursion. */
3198 I32 lastparen = *PL_reglastparen;
3200 re_unwind_branch_t *uw;
3202 /* Put unwinding data on stack */
3203 unwind1 = SSNEWt(1,re_unwind_branch_t);
3204 uw = SSPTRt(unwind1,re_unwind_branch_t);
3207 uw->type = ((c1 == BRANCH)
3209 : RE_UNWIND_BRANCHJ);
3210 uw->lastparen = lastparen;
3212 uw->locinput = locinput;
3213 uw->nextchr = nextchr;
3215 uw->regindent = ++PL_regindent;
3218 REGCP_SET(uw->lastcp);
3220 /* Now go into the first branch */
3233 /* We suppose that the next guy does not need
3234 backtracking: in particular, it is of constant length,
3235 and has no parenths to influence future backrefs. */
3236 ln = ARG1(scan); /* min to match */
3237 n = ARG2(scan); /* max to match */
3238 paren = scan->flags;
3240 if (paren > PL_regsize)
3242 if (paren > *PL_reglastparen)
3243 *PL_reglastparen = paren;
3245 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3247 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3248 PL_reginput = locinput;
3251 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3253 /* if we matched something zero-length we don't need to
3254 backtrack - capturing parens are already defined, so
3255 the caveat in the maximal case doesn't apply
3257 XXXX if ln == 0, we can redo this check first time
3258 through the following loop
3261 n = ln; /* don't backtrack */
3262 locinput = PL_reginput;
3263 if (HAS_TEXT(next) || JUMPABLE(next)) {
3264 regnode *text_node = next;
3266 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3268 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3270 if (PL_regkind[(U8)OP(text_node)] == REF) {
3272 n = ARG(text_node); /* which paren pair */
3273 ln = PL_regstartp[n];
3274 /* assume yes if we haven't seen CLOSEn */
3276 *PL_reglastparen < n ||
3283 c1 = *(PL_bostr + ln);
3285 else { c1 = (U8)*STRING(text_node); }
3286 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3288 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3289 c2 = PL_fold_locale[c1];
3298 /* This may be improved if l == 0. */
3299 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3300 /* If it could work, try it. */
3302 UCHARAT(PL_reginput) == c1 ||
3303 UCHARAT(PL_reginput) == c2)
3307 PL_regstartp[paren] =
3308 HOPc(PL_reginput, -l) - PL_bostr;
3309 PL_regendp[paren] = PL_reginput - PL_bostr;
3312 PL_regendp[paren] = -1;
3316 REGCP_UNWIND(lastcp);
3318 /* Couldn't or didn't -- move forward. */
3319 PL_reginput = locinput;
3320 if (regrepeat_hard(scan, 1, &l)) {
3322 locinput = PL_reginput;
3329 n = regrepeat_hard(scan, n, &l);
3330 /* if we matched something zero-length we don't need to
3331 backtrack, unless the minimum count is zero and we
3332 are capturing the result - in that case the capture
3333 being defined or not may affect later execution
3335 if (n != 0 && l == 0 && !(paren && ln == 0))
3336 ln = n; /* don't backtrack */
3337 locinput = PL_reginput;
3339 PerlIO_printf(Perl_debug_log,
3340 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3341 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3345 if (HAS_TEXT(next) || JUMPABLE(next)) {
3346 regnode *text_node = next;
3348 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3350 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3352 if (PL_regkind[(U8)OP(text_node)] == REF) {
3354 n = ARG(text_node); /* which paren pair */
3355 ln = PL_regstartp[n];
3356 /* assume yes if we haven't seen CLOSEn */
3358 *PL_reglastparen < n ||
3365 c1 = *(PL_bostr + ln);
3367 else { c1 = (U8)*STRING(text_node); }
3369 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3371 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3372 c2 = PL_fold_locale[c1];
3383 /* If it could work, try it. */
3385 UCHARAT(PL_reginput) == c1 ||
3386 UCHARAT(PL_reginput) == c2)
3389 PerlIO_printf(Perl_debug_log,
3390 "%*s trying tail with n=%"IVdf"...\n",
3391 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3395 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3396 PL_regendp[paren] = PL_reginput - PL_bostr;
3399 PL_regendp[paren] = -1;
3403 REGCP_UNWIND(lastcp);
3405 /* Couldn't or didn't -- back up. */
3407 locinput = HOPc(locinput, -l);
3408 PL_reginput = locinput;
3415 paren = scan->flags; /* Which paren to set */
3416 if (paren > PL_regsize)
3418 if (paren > *PL_reglastparen)
3419 *PL_reglastparen = paren;
3420 ln = ARG1(scan); /* min to match */
3421 n = ARG2(scan); /* max to match */
3422 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3426 ln = ARG1(scan); /* min to match */
3427 n = ARG2(scan); /* max to match */
3428 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3433 scan = NEXTOPER(scan);
3439 scan = NEXTOPER(scan);
3443 * Lookahead to avoid useless match attempts
3444 * when we know what character comes next.
3448 * Used to only do .*x and .*?x, but now it allows
3449 * for )'s, ('s and (?{ ... })'s to be in the way
3450 * of the quantifier and the EXACT-like node. -- japhy
3453 if (HAS_TEXT(next) || JUMPABLE(next)) {
3455 regnode *text_node = next;
3457 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3459 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3461 if (PL_regkind[(U8)OP(text_node)] == REF) {
3463 n = ARG(text_node); /* which paren pair */
3464 ln = PL_regstartp[n];
3465 /* assume yes if we haven't seen CLOSEn */
3467 *PL_reglastparen < n ||
3472 goto assume_ok_easy;
3474 s = (U8*)PL_bostr + ln;
3476 else { s = (U8*)STRING(text_node); }
3480 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3482 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3483 c2 = PL_fold_locale[c1];
3486 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3487 STRLEN ulen1, ulen2;
3488 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3489 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3491 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3492 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3494 c1 = utf8_to_uvuni(tmpbuf1, 0);
3495 c2 = utf8_to_uvuni(tmpbuf2, 0);
3498 c2 = c1 = utf8_to_uvchr(s, NULL);
3506 PL_reginput = locinput;
3510 if (ln && regrepeat(scan, ln) < ln)
3512 locinput = PL_reginput;
3515 char *e; /* Should not check after this */
3516 char *old = locinput;
3518 if (n == REG_INFTY) {
3521 while (UTF8_IS_CONTINUATION(*(U8*)e))
3527 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3531 e = locinput + n - ln;
3537 /* Find place 'next' could work */
3540 while (locinput <= e &&
3541 UCHARAT(locinput) != c1)
3544 while (locinput <= e
3545 && UCHARAT(locinput) != c1
3546 && UCHARAT(locinput) != c2)
3549 count = locinput - old;
3556 utf8_to_uvchr((U8*)locinput, &len) != c1;
3561 for (count = 0; locinput <= e; count++) {
3562 UV c = utf8_to_uvchr((U8*)locinput, &len);
3563 if (c == c1 || c == c2)
3571 /* PL_reginput == old now */
3572 if (locinput != old) {
3573 ln = 1; /* Did some */
3574 if (regrepeat(scan, count) < count)
3577 /* PL_reginput == locinput now */
3578 TRYPAREN(paren, ln, locinput);
3579 PL_reginput = locinput; /* Could be reset... */
3580 REGCP_UNWIND(lastcp);
3581 /* Couldn't or didn't -- move forward. */
3584 locinput += UTF8SKIP(locinput);
3590 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3594 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3596 c = UCHARAT(PL_reginput);
3597 /* If it could work, try it. */
3598 if (c == c1 || c == c2)
3600 TRYPAREN(paren, n, PL_reginput);
3601 REGCP_UNWIND(lastcp);
3604 /* If it could work, try it. */
3605 else if (c1 == -1000)
3607 TRYPAREN(paren, n, PL_reginput);
3608 REGCP_UNWIND(lastcp);
3610 /* Couldn't or didn't -- move forward. */
3611 PL_reginput = locinput;
3612 if (regrepeat(scan, 1)) {
3614 locinput = PL_reginput;
3622 n = regrepeat(scan, n);
3623 locinput = PL_reginput;
3624 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3625 ((!PL_multiline && OP(next) != MEOL) ||
3626 OP(next) == SEOL || OP(next) == EOS))
3628 ln = n; /* why back off? */
3629 /* ...because $ and \Z can match before *and* after
3630 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3631 We should back off by one in this case. */
3632 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3641 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3643 c = UCHARAT(PL_reginput);
3645 /* If it could work, try it. */
3646 if (c1 == -1000 || c == c1 || c == c2)
3648 TRYPAREN(paren, n, PL_reginput);
3649 REGCP_UNWIND(lastcp);
3651 /* Couldn't or didn't -- back up. */
3653 PL_reginput = locinput = HOPc(locinput, -1);
3661 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3663 c = UCHARAT(PL_reginput);
3665 /* If it could work, try it. */
3666 if (c1 == -1000 || c == c1 || c == c2)
3668 TRYPAREN(paren, n, PL_reginput);
3669 REGCP_UNWIND(lastcp);
3671 /* Couldn't or didn't -- back up. */
3673 PL_reginput = locinput = HOPc(locinput, -1);
3680 if (PL_reg_call_cc) {
3681 re_cc_state *cur_call_cc = PL_reg_call_cc;
3682 CURCUR *cctmp = PL_regcc;
3683 regexp *re = PL_reg_re;
3684 CHECKPOINT cp, lastcp;
3686 cp = regcppush(0); /* Save *all* the positions. */
3688 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3690 PL_reginput = locinput; /* Make position available to
3692 cache_re(PL_reg_call_cc->re);
3693 PL_regcc = PL_reg_call_cc->cc;
3694 PL_reg_call_cc = PL_reg_call_cc->prev;
3695 if (regmatch(cur_call_cc->node)) {
3696 PL_reg_call_cc = cur_call_cc;
3700 REGCP_UNWIND(lastcp);
3702 PL_reg_call_cc = cur_call_cc;
3708 PerlIO_printf(Perl_debug_log,
3709 "%*s continuation failed...\n",
3710 REPORT_CODE_OFF+PL_regindent*2, "")
3714 if (locinput < PL_regtill) {
3715 DEBUG_r(PerlIO_printf(Perl_debug_log,
3716 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3718 (long)(locinput - PL_reg_starttry),
3719 (long)(PL_regtill - PL_reg_starttry),
3721 sayNO_FINAL; /* Cannot match: too short. */
3723 PL_reginput = locinput; /* put where regtry can find it */
3724 sayYES_FINAL; /* Success! */
3726 PL_reginput = locinput; /* put where regtry can find it */
3727 sayYES_LOUD; /* Success! */
3730 PL_reginput = locinput;
3735 s = HOPBACKc(locinput, scan->flags);
3741 PL_reginput = locinput;
3746 s = HOPBACKc(locinput, scan->flags);
3752 PL_reginput = locinput;
3755 inner = NEXTOPER(NEXTOPER(scan));
3756 if (regmatch(inner) != n) {
3771 if (OP(scan) == SUSPEND) {
3772 locinput = PL_reginput;
3773 nextchr = UCHARAT(locinput);
3778 next = scan + ARG(scan);
3783 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3784 PTR2UV(scan), OP(scan));
3785 Perl_croak(aTHX_ "regexp memory corruption");
3792 * We get here only if there's trouble -- normally "case END" is
3793 * the terminating point.
3795 Perl_croak(aTHX_ "corrupted regexp pointers");
3801 PerlIO_printf(Perl_debug_log,
3802 "%*s %scould match...%s\n",
3803 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3807 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3808 PL_colors[4],PL_colors[5]));
3814 #if 0 /* Breaks $^R */
3822 PerlIO_printf(Perl_debug_log,
3823 "%*s %sfailed...%s\n",
3824 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3830 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3833 case RE_UNWIND_BRANCH:
3834 case RE_UNWIND_BRANCHJ:
3836 re_unwind_branch_t *uwb = &(uw->branch);
3837 I32 lastparen = uwb->lastparen;
3839 REGCP_UNWIND(uwb->lastcp);
3840 for (n = *PL_reglastparen; n > lastparen; n--)
3842 *PL_reglastparen = n;
3843 scan = next = uwb->next;
3845 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3846 ? BRANCH : BRANCHJ) ) { /* Failure */
3853 /* Have more choice yet. Reuse the same uwb. */
3855 if ((n = (uwb->type == RE_UNWIND_BRANCH
3856 ? NEXT_OFF(next) : ARG(next))))
3859 next = NULL; /* XXXX Needn't unwinding in this case... */
3861 next = NEXTOPER(scan);
3862 if (uwb->type == RE_UNWIND_BRANCHJ)
3863 next = NEXTOPER(next);
3864 locinput = uwb->locinput;
3865 nextchr = uwb->nextchr;
3867 PL_regindent = uwb->regindent;
3874 Perl_croak(aTHX_ "regexp unwind memory corruption");
3885 - regrepeat - repeatedly match something simple, report how many
3888 * [This routine now assumes that it will only match on things of length 1.
3889 * That was true before, but now we assume scan - reginput is the count,
3890 * rather than incrementing count on every character. [Er, except utf8.]]
3893 S_regrepeat(pTHX_ regnode *p, I32 max)
3895 register char *scan;
3897 register char *loceol = PL_regeol;
3898 register I32 hardcount = 0;
3899 register bool do_utf8 = PL_reg_match_utf8;
3902 if (max != REG_INFTY && max < loceol - scan)
3903 loceol = scan + max;
3908 while (scan < loceol && hardcount < max && *scan != '\n') {
3909 scan += UTF8SKIP(scan);
3913 while (scan < loceol && *scan != '\n')
3920 while (scan < loceol && hardcount < max) {
3921 scan += UTF8SKIP(scan);
3931 case EXACT: /* length of string is 1 */
3933 while (scan < loceol && UCHARAT(scan) == c)
3936 case EXACTF: /* length of string is 1 */
3938 while (scan < loceol &&
3939 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3942 case EXACTFL: /* length of string is 1 */
3943 PL_reg_flags |= RF_tainted;
3945 while (scan < loceol &&
3946 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3952 while (hardcount < max && scan < loceol &&
3953 reginclass(p, (U8*)scan, do_utf8)) {
3954 scan += UTF8SKIP(scan);
3958 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3965 LOAD_UTF8_CHARCLASS(alnum,"a");
3966 while (hardcount < max && scan < loceol &&
3967 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3968 scan += UTF8SKIP(scan);
3972 while (scan < loceol && isALNUM(*scan))
3977 PL_reg_flags |= RF_tainted;
3980 while (hardcount < max && scan < loceol &&
3981 isALNUM_LC_utf8((U8*)scan)) {
3982 scan += UTF8SKIP(scan);
3986 while (scan < loceol && isALNUM_LC(*scan))
3993 LOAD_UTF8_CHARCLASS(alnum,"a");
3994 while (hardcount < max && scan < loceol &&
3995 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3996 scan += UTF8SKIP(scan);
4000 while (scan < loceol && !isALNUM(*scan))
4005 PL_reg_flags |= RF_tainted;
4008 while (hardcount < max && scan < loceol &&
4009 !isALNUM_LC_utf8((U8*)scan)) {
4010 scan += UTF8SKIP(scan);
4014 while (scan < loceol && !isALNUM_LC(*scan))
4021 LOAD_UTF8_CHARCLASS(space," ");
4022 while (hardcount < max && scan < loceol &&
4024 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4025 scan += UTF8SKIP(scan);
4029 while (scan < loceol && isSPACE(*scan))
4034 PL_reg_flags |= RF_tainted;
4037 while (hardcount < max && scan < loceol &&
4038 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4039 scan += UTF8SKIP(scan);
4043 while (scan < loceol && isSPACE_LC(*scan))
4050 LOAD_UTF8_CHARCLASS(space," ");
4051 while (hardcount < max && scan < loceol &&
4053 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4054 scan += UTF8SKIP(scan);
4058 while (scan < loceol && !isSPACE(*scan))
4063 PL_reg_flags |= RF_tainted;
4066 while (hardcount < max && scan < loceol &&
4067 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4068 scan += UTF8SKIP(scan);
4072 while (scan < loceol && !isSPACE_LC(*scan))
4079 LOAD_UTF8_CHARCLASS(digit,"0");
4080 while (hardcount < max && scan < loceol &&
4081 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4082 scan += UTF8SKIP(scan);
4086 while (scan < loceol && isDIGIT(*scan))
4093 LOAD_UTF8_CHARCLASS(digit,"0");
4094 while (hardcount < max && scan < loceol &&
4095 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4096 scan += UTF8SKIP(scan);
4100 while (scan < loceol && !isDIGIT(*scan))
4104 default: /* Called on something of 0 width. */
4105 break; /* So match right here or not at all. */
4111 c = scan - PL_reginput;
4116 SV *prop = sv_newmortal();
4119 PerlIO_printf(Perl_debug_log,
4120 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4121 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4128 - regrepeat_hard - repeatedly match something, report total lenth and length
4130 * The repeater is supposed to have constant length.
4134 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4136 register char *scan = Nullch;
4137 register char *start;
4138 register char *loceol = PL_regeol;
4140 I32 count = 0, res = 1;
4145 start = PL_reginput;
4146 if (PL_reg_match_utf8) {
4147 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4150 while (start < PL_reginput) {
4152 start += UTF8SKIP(start);
4163 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4165 *lp = l = PL_reginput - start;
4166 if (max != REG_INFTY && l*max < loceol - scan)
4167 loceol = scan + l*max;
4180 - regclass_swash - prepare the utf8 swash
4184 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4190 if (PL_regdata && PL_regdata->count) {
4193 if (PL_regdata->what[n] == 's') {
4194 SV *rv = (SV*)PL_regdata->data[n];
4195 AV *av = (AV*)SvRV((SV*)rv);
4198 /* See the end of regcomp.c:S_reglass() for
4199 * documentation of these array elements. */
4201 si = *av_fetch(av, 0, FALSE);
4202 a = av_fetch(av, 1, FALSE);
4203 b = av_fetch(av, 2, FALSE);
4207 else if (si && doinit) {
4208 sw = swash_init("utf8", "", si, 1, 0);
4209 (void)av_store(av, 1, sw);
4225 - reginclasslen - determine if a character falls into a character class
4227 The n is the ANYOF regnode, the p is the target string, lenp
4228 is pointer to the maximum length of how far to go in the p
4229 (if the lenp is zero, UTF8SKIP(p) is used),
4230 do_utf8 tells whether the target string is in UTF-8.
4235 S_reginclasslen(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4237 char flags = ANYOF_FLAGS(n);
4243 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4245 plen = lenp ? *lenp : UNISKIP(c);
4246 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4249 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4250 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4253 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4257 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4260 if (swash_fetch(sw, p, do_utf8))
4262 else if (flags & ANYOF_FOLD) {
4263 if (!match && lenp && av) {
4266 for (i = 0; i <= av_len(av); i++) {
4267 SV* sv = *av_fetch(av, i, FALSE);
4269 char *s = SvPV(sv, len);
4271 if (len <= plen && memEQ(s, (char*)p, len)) {
4279 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4282 to_utf8_fold(p, tmpbuf, &tmplen);
4283 if (swash_fetch(sw, tmpbuf, do_utf8))
4289 if (match && lenp && *lenp == 0)
4292 if (!match && c < 256) {
4293 if (ANYOF_BITMAP_TEST(n, c))
4295 else if (flags & ANYOF_FOLD) {
4298 if (flags & ANYOF_LOCALE) {
4299 PL_reg_flags |= RF_tainted;
4300 f = PL_fold_locale[c];
4304 if (f != c && ANYOF_BITMAP_TEST(n, f))
4308 if (!match && (flags & ANYOF_CLASS)) {
4309 PL_reg_flags |= RF_tainted;
4311 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4312 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4313 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4314 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4315 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4316 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4317 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4318 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4319 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4320 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4321 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4322 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4323 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4324 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4325 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4326 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4327 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4328 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4329 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4330 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4331 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4332 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4333 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4334 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4335 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4336 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4337 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4338 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4339 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4340 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4341 ) /* How's that for a conditional? */
4348 return (flags & ANYOF_INVERT) ? !match : match;
4352 - reginclass - determine if a character falls into a character class
4354 The n is the ANYOF regnode, the p is the target string, do_utf8 tells
4355 whether the target string is in UTF-8.
4360 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
4362 return S_reginclasslen(aTHX_ n, p, 0, do_utf8);
4366 S_reghop(pTHX_ U8 *s, I32 off)
4368 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4372 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4375 while (off-- && s < lim) {
4376 /* XXX could check well-formedness here */
4384 if (UTF8_IS_CONTINUED(*s)) {
4385 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4388 /* XXX could check well-formedness here */
4396 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4398 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4402 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4405 while (off-- && s < lim) {
4406 /* XXX could check well-formedness here */
4416 if (UTF8_IS_CONTINUED(*s)) {
4417 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4420 /* XXX could check well-formedness here */
4432 restore_pos(pTHX_ void *arg)
4434 if (PL_reg_eval_set) {
4435 if (PL_reg_oldsaved) {
4436 PL_reg_re->subbeg = PL_reg_oldsaved;
4437 PL_reg_re->sublen = PL_reg_oldsavedlen;
4438 RX_MATCH_COPIED_on(PL_reg_re);
4440 PL_reg_magic->mg_len = PL_reg_oldpos;
4441 PL_reg_eval_set = 0;
4442 PL_curpm = PL_reg_oldcurpm;