5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-2001, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGEXEC_C
85 #define RF_tainted 1 /* tainted information used? */
86 #define RF_warned 2 /* warned about big count? */
87 #define RF_evaled 4 /* Did an EVAL with setting? */
88 #define RF_utf8 8 /* String contains multibyte chars? */
90 #define UTF (PL_reg_flags & RF_utf8)
92 #define RS_init 1 /* eval environment created */
93 #define RS_set 2 /* replsv value is set */
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
107 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
108 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
110 #define HOPc(pos,off) ((char*)HOP(pos,off))
111 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
113 #define HOPBACK(pos, off) ( \
114 (UTF && PL_reg_match_utf8) \
115 ? reghopmaybe((U8*)pos, -off) \
116 : (pos - off >= PL_bostr) \
120 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
122 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
123 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
124 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
125 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
126 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
127 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
129 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
131 /* for use after a quantifier and before an EXACT-like node -- japhy */
132 #define JUMPABLE(rn) ( \
133 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
134 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
135 OP(rn) == PLUS || OP(rn) == MINMOD || \
136 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
139 #define HAS_TEXT(rn) ( \
140 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
143 #define FIND_NEXT_IMPT(rn) STMT_START { \
144 while (JUMPABLE(rn)) \
145 if (OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
146 PL_regkind[(U8)OP(rn)] == CURLY) \
147 rn = NEXTOPER(NEXTOPER(rn)); \
148 else if (OP(rn) == PLUS) \
150 else rn += NEXT_OFF(rn); \
153 static void restore_pos(pTHX_ void *arg);
156 S_regcppush(pTHX_ I32 parenfloor)
158 int retval = PL_savestack_ix;
159 #define REGCP_PAREN_ELEMS 4
160 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
163 if (paren_elems_to_push < 0)
164 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
166 #define REGCP_OTHER_ELEMS 6
167 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
168 for (p = PL_regsize; p > parenfloor; p--) {
169 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
170 SSPUSHINT(PL_regendp[p]);
171 SSPUSHINT(PL_regstartp[p]);
172 SSPUSHPTR(PL_reg_start_tmp[p]);
175 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
176 SSPUSHINT(PL_regsize);
177 SSPUSHINT(*PL_reglastparen);
178 SSPUSHINT(*PL_reglastcloseparen);
179 SSPUSHPTR(PL_reginput);
180 #define REGCP_FRAME_ELEMS 2
181 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
182 * are needed for the regexp context stack bookkeeping. */
183 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
184 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
189 /* These are needed since we do not localize EVAL nodes: */
190 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
191 " Setting an EVAL scope, savestack=%"IVdf"\n", \
192 (IV)PL_savestack_ix)); cp = PL_savestack_ix
194 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
195 PerlIO_printf(Perl_debug_log, \
196 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
197 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
207 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
209 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
210 i = SSPOPINT; /* Parentheses elements to pop. */
211 input = (char *) SSPOPPTR;
212 *PL_reglastcloseparen = SSPOPINT;
213 *PL_reglastparen = SSPOPINT;
214 PL_regsize = SSPOPINT;
216 /* Now restore the parentheses context. */
217 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
218 i > 0; i -= REGCP_PAREN_ELEMS) {
219 paren = (U32)SSPOPINT;
220 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
221 PL_regstartp[paren] = SSPOPINT;
223 if (paren <= *PL_reglastparen)
224 PL_regendp[paren] = tmps;
226 PerlIO_printf(Perl_debug_log,
227 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
228 (UV)paren, (IV)PL_regstartp[paren],
229 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
230 (IV)PL_regendp[paren],
231 (paren > *PL_reglastparen ? "(no)" : ""));
235 if (*PL_reglastparen + 1 <= PL_regnpar) {
236 PerlIO_printf(Perl_debug_log,
237 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
238 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
242 /* It would seem that the similar code in regtry()
243 * already takes care of this, and in fact it is in
244 * a better location to since this code can #if 0-ed out
245 * but the code in regtry() is needed or otherwise tests
246 * requiring null fields (pat.t#187 and split.t#{13,14}
247 * (as of patchlevel 7877) will fail. Then again,
248 * this code seems to be necessary or otherwise
249 * building DynaLoader will fail:
250 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
252 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
253 if (paren > PL_regsize)
254 PL_regstartp[paren] = -1;
255 PL_regendp[paren] = -1;
262 S_regcp_set_to(pTHX_ I32 ss)
264 I32 tmp = PL_savestack_ix;
266 PL_savestack_ix = ss;
268 PL_savestack_ix = tmp;
272 typedef struct re_cc_state
276 struct re_cc_state *prev;
281 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
283 #define TRYPAREN(paren, n, input) { \
286 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
287 PL_regendp[paren] = input - PL_bostr; \
290 PL_regendp[paren] = -1; \
292 if (regmatch(next)) \
295 PL_regendp[paren] = -1; \
300 * pregexec and friends
304 - pregexec - match a regexp against a string
307 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
308 char *strbeg, I32 minend, SV *screamer, U32 nosave)
309 /* strend: pointer to null at end of string */
310 /* strbeg: real beginning of string */
311 /* minend: end of match must be >=minend after stringarg. */
312 /* nosave: For optimizations. */
315 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
316 nosave ? 0 : REXEC_COPY_STR);
320 S_cache_re(pTHX_ regexp *prog)
322 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
324 PL_regprogram = prog->program;
326 PL_regnpar = prog->nparens;
327 PL_regdata = prog->data;
332 * Need to implement the following flags for reg_anch:
334 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
336 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
337 * INTUIT_AUTORITATIVE_ML
338 * INTUIT_ONCE_NOML - Intuit can match in one location only.
341 * Another flag for this function: SECOND_TIME (so that float substrs
342 * with giant delta may be not rechecked).
345 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
347 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
348 Otherwise, only SvCUR(sv) is used to get strbeg. */
350 /* XXXX We assume that strpos is strbeg unless sv. */
352 /* XXXX Some places assume that there is a fixed substring.
353 An update may be needed if optimizer marks as "INTUITable"
354 RExen without fixed substrings. Similarly, it is assumed that
355 lengths of all the strings are no more than minlen, thus they
356 cannot come from lookahead.
357 (Or minlen should take into account lookahead.) */
359 /* A failure to find a constant substring means that there is no need to make
360 an expensive call to REx engine, thus we celebrate a failure. Similarly,
361 finding a substring too deep into the string means that less calls to
362 regtry() should be needed.
364 REx compiler's optimizer found 4 possible hints:
365 a) Anchored substring;
367 c) Whether we are anchored (beginning-of-line or \G);
368 d) First node (of those at offset 0) which may distingush positions;
369 We use a)b)d) and multiline-part of c), and try to find a position in the
370 string which does not contradict any of them.
373 /* Most of decisions we do here should have been done at compile time.
374 The nodes of the REx which we used for the search should have been
375 deleted from the finite automaton. */
378 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
379 char *strend, U32 flags, re_scream_pos_data *data)
381 register I32 start_shift = 0;
382 /* Should be nonnegative! */
383 register I32 end_shift = 0;
389 register char *other_last = Nullch; /* other substr checked before this */
390 char *check_at = Nullch; /* check substr found at this pos */
392 char *i_strpos = strpos;
393 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
396 if (prog->reganch & ROPT_UTF8) {
397 DEBUG_r(PerlIO_printf(Perl_debug_log,
398 "UTF-8 regex...\n"));
399 PL_reg_flags |= RF_utf8;
403 char *s = PL_reg_match_utf8 ?
404 sv_uni_display(dsv, sv, 60, 0) : strpos;
405 int len = PL_reg_match_utf8 ?
406 strlen(s) : strend - strpos;
409 if (PL_reg_match_utf8)
410 DEBUG_r(PerlIO_printf(Perl_debug_log,
411 "UTF-8 target...\n"));
412 PerlIO_printf(Perl_debug_log,
413 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
414 PL_colors[4],PL_colors[5],PL_colors[0],
417 (strlen(prog->precomp) > 60 ? "..." : ""),
419 (int)(len > 60 ? 60 : len),
421 (len > 60 ? "..." : "")
425 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
426 DEBUG_r(PerlIO_printf(Perl_debug_log,
427 "String too short... [re_intuit_start]\n"));
430 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
432 check = prog->check_substr;
433 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
434 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
435 || ( (prog->reganch & ROPT_ANCH_BOL)
436 && !PL_multiline ) ); /* Check after \n? */
439 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
440 | ROPT_IMPLICIT)) /* not a real BOL */
441 /* SvCUR is not set on references: SvRV and SvPVX overlap */
443 && (strpos != strbeg)) {
444 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
447 if (prog->check_offset_min == prog->check_offset_max &&
448 !(prog->reganch & ROPT_CANY_SEEN)) {
449 /* Substring at constant offset from beg-of-str... */
452 s = HOP3c(strpos, prog->check_offset_min, strend);
454 slen = SvCUR(check); /* >= 1 */
456 if ( strend - s > slen || strend - s < slen - 1
457 || (strend - s == slen && strend[-1] != '\n')) {
458 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
461 /* Now should match s[0..slen-2] */
463 if (slen && (*SvPVX(check) != *s
465 && memNE(SvPVX(check), s, slen)))) {
467 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
471 else if (*SvPVX(check) != *s
472 || ((slen = SvCUR(check)) > 1
473 && memNE(SvPVX(check), s, slen)))
475 goto success_at_start;
478 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
480 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
481 end_shift = prog->minlen - start_shift -
482 CHR_SVLEN(check) + (SvTAIL(check) != 0);
484 I32 end = prog->check_offset_max + CHR_SVLEN(check)
485 - (SvTAIL(check) != 0);
486 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
488 if (end_shift < eshift)
492 else { /* Can match at random position */
495 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
496 /* Should be nonnegative! */
497 end_shift = prog->minlen - start_shift -
498 CHR_SVLEN(check) + (SvTAIL(check) != 0);
501 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
503 Perl_croak(aTHX_ "panic: end_shift");
507 /* Find a possible match in the region s..strend by looking for
508 the "check" substring in the region corrected by start/end_shift. */
509 if (flags & REXEC_SCREAM) {
510 I32 p = -1; /* Internal iterator of scream. */
511 I32 *pp = data ? data->scream_pos : &p;
513 if (PL_screamfirst[BmRARE(check)] >= 0
514 || ( BmRARE(check) == '\n'
515 && (BmPREVIOUS(check) == SvCUR(check) - 1)
517 s = screaminstr(sv, check,
518 start_shift + (s - strbeg), end_shift, pp, 0);
522 *data->scream_olds = s;
524 else if (prog->reganch & ROPT_CANY_SEEN)
525 s = fbm_instr((U8*)(s + start_shift),
526 (U8*)(strend - end_shift),
527 check, PL_multiline ? FBMrf_MULTILINE : 0);
529 s = fbm_instr(HOP3(s, start_shift, strend),
530 HOP3(strend, -end_shift, strbeg),
531 check, PL_multiline ? FBMrf_MULTILINE : 0);
533 /* Update the count-of-usability, remove useless subpatterns,
536 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
537 (s ? "Found" : "Did not find"),
538 ((check == prog->anchored_substr) ? "anchored" : "floating"),
540 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
542 PL_colors[1], (SvTAIL(check) ? "$" : ""),
543 (s ? " at offset " : "...\n") ) );
550 /* Finish the diagnostic message */
551 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
553 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
554 Start with the other substr.
555 XXXX no SCREAM optimization yet - and a very coarse implementation
556 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
557 *always* match. Probably should be marked during compile...
558 Probably it is right to do no SCREAM here...
561 if (prog->float_substr && prog->anchored_substr) {
562 /* Take into account the "other" substring. */
563 /* XXXX May be hopelessly wrong for UTF... */
566 if (check == prog->float_substr) {
569 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
572 t = s - prog->check_offset_max;
573 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
574 && (!(prog->reganch & ROPT_UTF8)
575 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
580 t = HOP3c(t, prog->anchored_offset, strend);
581 if (t < other_last) /* These positions already checked */
583 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
586 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
587 /* On end-of-str: see comment below. */
588 s = fbm_instr((unsigned char*)t,
589 HOP3(HOP3(last1, prog->anchored_offset, strend)
590 + SvCUR(prog->anchored_substr),
591 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
592 prog->anchored_substr,
593 PL_multiline ? FBMrf_MULTILINE : 0);
594 DEBUG_r(PerlIO_printf(Perl_debug_log,
595 "%s anchored substr `%s%.*s%s'%s",
596 (s ? "Found" : "Contradicts"),
598 (int)(SvCUR(prog->anchored_substr)
599 - (SvTAIL(prog->anchored_substr)!=0)),
600 SvPVX(prog->anchored_substr),
601 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
603 if (last1 >= last2) {
604 DEBUG_r(PerlIO_printf(Perl_debug_log,
605 ", giving up...\n"));
608 DEBUG_r(PerlIO_printf(Perl_debug_log,
609 ", trying floating at offset %ld...\n",
610 (long)(HOP3c(s1, 1, strend) - i_strpos)));
611 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
612 s = HOP3c(last, 1, strend);
616 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
617 (long)(s - i_strpos)));
618 t = HOP3c(s, -prog->anchored_offset, strbeg);
619 other_last = HOP3c(s, 1, strend);
627 else { /* Take into account the floating substring. */
631 t = HOP3c(s, -start_shift, strbeg);
633 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
634 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
635 last = HOP3c(t, prog->float_max_offset, strend);
636 s = HOP3c(t, prog->float_min_offset, strend);
639 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
640 /* fbm_instr() takes into account exact value of end-of-str
641 if the check is SvTAIL(ed). Since false positives are OK,
642 and end-of-str is not later than strend we are OK. */
643 s = fbm_instr((unsigned char*)s,
644 (unsigned char*)last + SvCUR(prog->float_substr)
645 - (SvTAIL(prog->float_substr)!=0),
646 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
647 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
648 (s ? "Found" : "Contradicts"),
650 (int)(SvCUR(prog->float_substr)
651 - (SvTAIL(prog->float_substr)!=0)),
652 SvPVX(prog->float_substr),
653 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
656 DEBUG_r(PerlIO_printf(Perl_debug_log,
657 ", giving up...\n"));
660 DEBUG_r(PerlIO_printf(Perl_debug_log,
661 ", trying anchored starting at offset %ld...\n",
662 (long)(s1 + 1 - i_strpos)));
664 s = HOP3c(t, 1, strend);
668 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
669 (long)(s - i_strpos)));
670 other_last = s; /* Fix this later. --Hugo */
679 t = s - prog->check_offset_max;
680 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
681 && (!(prog->reganch & ROPT_UTF8)
682 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
684 /* Fixed substring is found far enough so that the match
685 cannot start at strpos. */
687 if (ml_anch && t[-1] != '\n') {
688 /* Eventually fbm_*() should handle this, but often
689 anchored_offset is not 0, so this check will not be wasted. */
690 /* XXXX In the code below we prefer to look for "^" even in
691 presence of anchored substrings. And we search even
692 beyond the found float position. These pessimizations
693 are historical artefacts only. */
695 while (t < strend - prog->minlen) {
697 if (t < check_at - prog->check_offset_min) {
698 if (prog->anchored_substr) {
699 /* Since we moved from the found position,
700 we definitely contradict the found anchored
701 substr. Due to the above check we do not
702 contradict "check" substr.
703 Thus we can arrive here only if check substr
704 is float. Redo checking for "other"=="fixed".
707 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
708 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
709 goto do_other_anchored;
711 /* We don't contradict the found floating substring. */
712 /* XXXX Why not check for STCLASS? */
714 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
715 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
718 /* Position contradicts check-string */
719 /* XXXX probably better to look for check-string
720 than for "\n", so one should lower the limit for t? */
721 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
722 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
723 other_last = strpos = s = t + 1;
728 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
729 PL_colors[0],PL_colors[1]));
733 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
734 PL_colors[0],PL_colors[1]));
738 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
741 /* The found string does not prohibit matching at strpos,
742 - no optimization of calling REx engine can be performed,
743 unless it was an MBOL and we are not after MBOL,
744 or a future STCLASS check will fail this. */
746 /* Even in this situation we may use MBOL flag if strpos is offset
747 wrt the start of the string. */
748 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
749 && (strpos != strbeg) && strpos[-1] != '\n'
750 /* May be due to an implicit anchor of m{.*foo} */
751 && !(prog->reganch & ROPT_IMPLICIT))
756 DEBUG_r( if (ml_anch)
757 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
758 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
761 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
762 && prog->check_substr /* Could be deleted already */
763 && --BmUSEFUL(prog->check_substr) < 0
764 && prog->check_substr == prog->float_substr)
766 /* If flags & SOMETHING - do not do it many times on the same match */
767 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
768 SvREFCNT_dec(prog->check_substr);
769 prog->check_substr = Nullsv; /* disable */
770 prog->float_substr = Nullsv; /* clear */
771 check = Nullsv; /* abort */
773 /* XXXX This is a remnant of the old implementation. It
774 looks wasteful, since now INTUIT can use many
776 prog->reganch &= ~RE_USE_INTUIT;
783 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
784 if (prog->regstclass) {
785 /* minlen == 0 is possible if regstclass is \b or \B,
786 and the fixed substr is ''$.
787 Since minlen is already taken into account, s+1 is before strend;
788 accidentally, minlen >= 1 guaranties no false positives at s + 1
789 even for \b or \B. But (minlen? 1 : 0) below assumes that
790 regstclass does not come from lookahead... */
791 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
792 This leaves EXACTF only, which is dealt with in find_byclass(). */
793 U8* str = (U8*)STRING(prog->regstclass);
794 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
795 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
797 char *endpos = (prog->anchored_substr || ml_anch)
798 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
799 : (prog->float_substr
800 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
803 char *startpos = strbeg;
806 if (prog->reganch & ROPT_UTF8) {
807 PL_regdata = prog->data;
810 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
815 if (endpos == strend) {
816 DEBUG_r( PerlIO_printf(Perl_debug_log,
817 "Could not match STCLASS...\n") );
820 DEBUG_r( PerlIO_printf(Perl_debug_log,
821 "This position contradicts STCLASS...\n") );
822 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
824 /* Contradict one of substrings */
825 if (prog->anchored_substr) {
826 if (prog->anchored_substr == check) {
827 DEBUG_r( what = "anchored" );
829 s = HOP3c(t, 1, strend);
830 if (s + start_shift + end_shift > strend) {
831 /* XXXX Should be taken into account earlier? */
832 DEBUG_r( PerlIO_printf(Perl_debug_log,
833 "Could not match STCLASS...\n") );
838 DEBUG_r( PerlIO_printf(Perl_debug_log,
839 "Looking for %s substr starting at offset %ld...\n",
840 what, (long)(s + start_shift - i_strpos)) );
843 /* Have both, check_string is floating */
844 if (t + start_shift >= check_at) /* Contradicts floating=check */
845 goto retry_floating_check;
846 /* Recheck anchored substring, but not floating... */
850 DEBUG_r( PerlIO_printf(Perl_debug_log,
851 "Looking for anchored substr starting at offset %ld...\n",
852 (long)(other_last - i_strpos)) );
853 goto do_other_anchored;
855 /* Another way we could have checked stclass at the
856 current position only: */
861 DEBUG_r( PerlIO_printf(Perl_debug_log,
862 "Looking for /%s^%s/m starting at offset %ld...\n",
863 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
866 if (!prog->float_substr) /* Could have been deleted */
868 /* Check is floating subtring. */
869 retry_floating_check:
870 t = check_at - start_shift;
871 DEBUG_r( what = "floating" );
872 goto hop_and_restart;
875 DEBUG_r(PerlIO_printf(Perl_debug_log,
876 "By STCLASS: moving %ld --> %ld\n",
877 (long)(t - i_strpos), (long)(s - i_strpos))
881 DEBUG_r(PerlIO_printf(Perl_debug_log,
882 "Does not contradict STCLASS...\n");
887 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
888 PL_colors[4], (check ? "Guessed" : "Giving up"),
889 PL_colors[5], (long)(s - i_strpos)) );
892 fail_finish: /* Substring not found */
893 if (prog->check_substr) /* could be removed already */
894 BmUSEFUL(prog->check_substr) += 5; /* hooray */
896 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
897 PL_colors[4],PL_colors[5]));
901 /* We know what class REx starts with. Try to find this position... */
903 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
905 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
911 register I32 tmp = 1; /* Scratch variable? */
912 register bool do_utf8 = PL_reg_match_utf8;
914 /* We know what class it must start with. */
918 if (reginclass(c, (U8*)s, do_utf8)) {
919 if (tmp && (norun || regtry(prog, s)))
926 s += do_utf8 ? UTF8SKIP(s) : 1;
931 if (tmp && (norun || regtry(prog, s)))
943 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
944 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
946 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
947 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
949 c1 = utf8_to_uvuni(tmpbuf1, 0);
950 c2 = utf8_to_uvuni(tmpbuf2, 0);
961 c2 = PL_fold_locale[c1];
966 e = s; /* Due to minlen logic of intuit() */
970 /* The ibcmp_utf8() uses to_uni_fold() which is more
971 * correct folding for Unicode than using lowercase.
972 * However, it doesn't work quite fully since the folding
973 * is a one-to-many mapping and the regex optimizer is
974 * unaware of this, so it may throw out good matches.
975 * Fortunately, not getting this right is allowed
976 * for Unicode Regular Expression Support level 1,
977 * only one-to-one matching is required. --jhi */
980 if ( utf8_to_uvchr((U8*)s, &len) == c1
982 ibcmp_utf8(s, do_utf8, strend - s,
984 && (norun || regtry(prog, s)) )
990 UV c = utf8_to_uvchr((U8*)s, &len);
991 if ( (c == c1 || c == c2)
993 ibcmp_utf8(s, do_utf8, strend - s,
995 && (norun || regtry(prog, s)) )
1004 && (ln == 1 || !(OP(c) == EXACTF
1006 : ibcmp_locale(s, m, ln)))
1007 && (norun || regtry(prog, s)) )
1013 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1014 && (ln == 1 || !(OP(c) == EXACTF
1016 : ibcmp_locale(s, m, ln)))
1017 && (norun || regtry(prog, s)) )
1024 PL_reg_flags |= RF_tainted;
1031 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1034 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1036 tmp = ((OP(c) == BOUND ?
1037 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1038 LOAD_UTF8_CHARCLASS(alnum,"a");
1039 while (s < strend) {
1040 if (tmp == !(OP(c) == BOUND ?
1041 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1042 isALNUM_LC_utf8((U8*)s)))
1045 if ((norun || regtry(prog, s)))
1052 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1053 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1054 while (s < strend) {
1056 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1058 if ((norun || regtry(prog, s)))
1064 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1068 PL_reg_flags |= RF_tainted;
1075 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1078 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1080 tmp = ((OP(c) == NBOUND ?
1081 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1082 LOAD_UTF8_CHARCLASS(alnum,"a");
1083 while (s < strend) {
1084 if (tmp == !(OP(c) == NBOUND ?
1085 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1086 isALNUM_LC_utf8((U8*)s)))
1088 else if ((norun || regtry(prog, s)))
1094 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1095 tmp = ((OP(c) == NBOUND ?
1096 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1097 while (s < strend) {
1099 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1101 else if ((norun || regtry(prog, s)))
1106 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1111 LOAD_UTF8_CHARCLASS(alnum,"a");
1112 while (s < strend) {
1113 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1114 if (tmp && (norun || regtry(prog, s)))
1125 while (s < strend) {
1127 if (tmp && (norun || regtry(prog, s)))
1139 PL_reg_flags |= RF_tainted;
1141 while (s < strend) {
1142 if (isALNUM_LC_utf8((U8*)s)) {
1143 if (tmp && (norun || regtry(prog, s)))
1154 while (s < strend) {
1155 if (isALNUM_LC(*s)) {
1156 if (tmp && (norun || regtry(prog, s)))
1169 LOAD_UTF8_CHARCLASS(alnum,"a");
1170 while (s < strend) {
1171 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1172 if (tmp && (norun || regtry(prog, s)))
1183 while (s < strend) {
1185 if (tmp && (norun || regtry(prog, s)))
1197 PL_reg_flags |= RF_tainted;
1199 while (s < strend) {
1200 if (!isALNUM_LC_utf8((U8*)s)) {
1201 if (tmp && (norun || regtry(prog, s)))
1212 while (s < strend) {
1213 if (!isALNUM_LC(*s)) {
1214 if (tmp && (norun || regtry(prog, s)))
1227 LOAD_UTF8_CHARCLASS(space," ");
1228 while (s < strend) {
1229 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1230 if (tmp && (norun || regtry(prog, s)))
1241 while (s < strend) {
1243 if (tmp && (norun || regtry(prog, s)))
1255 PL_reg_flags |= RF_tainted;
1257 while (s < strend) {
1258 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1259 if (tmp && (norun || regtry(prog, s)))
1270 while (s < strend) {
1271 if (isSPACE_LC(*s)) {
1272 if (tmp && (norun || regtry(prog, s)))
1285 LOAD_UTF8_CHARCLASS(space," ");
1286 while (s < strend) {
1287 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1288 if (tmp && (norun || regtry(prog, s)))
1299 while (s < strend) {
1301 if (tmp && (norun || regtry(prog, s)))
1313 PL_reg_flags |= RF_tainted;
1315 while (s < strend) {
1316 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1317 if (tmp && (norun || regtry(prog, s)))
1328 while (s < strend) {
1329 if (!isSPACE_LC(*s)) {
1330 if (tmp && (norun || regtry(prog, s)))
1343 LOAD_UTF8_CHARCLASS(digit,"0");
1344 while (s < strend) {
1345 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1346 if (tmp && (norun || regtry(prog, s)))
1357 while (s < strend) {
1359 if (tmp && (norun || regtry(prog, s)))
1371 PL_reg_flags |= RF_tainted;
1373 while (s < strend) {
1374 if (isDIGIT_LC_utf8((U8*)s)) {
1375 if (tmp && (norun || regtry(prog, s)))
1386 while (s < strend) {
1387 if (isDIGIT_LC(*s)) {
1388 if (tmp && (norun || regtry(prog, s)))
1401 LOAD_UTF8_CHARCLASS(digit,"0");
1402 while (s < strend) {
1403 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1404 if (tmp && (norun || regtry(prog, s)))
1415 while (s < strend) {
1417 if (tmp && (norun || regtry(prog, s)))
1429 PL_reg_flags |= RF_tainted;
1431 while (s < strend) {
1432 if (!isDIGIT_LC_utf8((U8*)s)) {
1433 if (tmp && (norun || regtry(prog, s)))
1444 while (s < strend) {
1445 if (!isDIGIT_LC(*s)) {
1446 if (tmp && (norun || regtry(prog, s)))
1458 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1467 - regexec_flags - match a regexp against a string
1470 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1471 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1472 /* strend: pointer to null at end of string */
1473 /* strbeg: real beginning of string */
1474 /* minend: end of match must be >=minend after stringarg. */
1475 /* data: May be used for some additional optimizations. */
1476 /* nosave: For optimizations. */
1479 register regnode *c;
1480 register char *startpos = stringarg;
1481 I32 minlen; /* must match at least this many chars */
1482 I32 dontbother = 0; /* how many characters not to try at end */
1483 /* I32 start_shift = 0; */ /* Offset of the start to find
1484 constant substr. */ /* CC */
1485 I32 end_shift = 0; /* Same for the end. */ /* CC */
1486 I32 scream_pos = -1; /* Internal iterator of scream. */
1488 SV* oreplsv = GvSV(PL_replgv);
1489 bool do_utf8 = DO_UTF8(sv);
1491 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
1498 PL_regnarrate = DEBUG_r_TEST;
1501 /* Be paranoid... */
1502 if (prog == NULL || startpos == NULL) {
1503 Perl_croak(aTHX_ "NULL regexp parameter");
1507 minlen = prog->minlen;
1508 if (strend - startpos < minlen) {
1509 DEBUG_r(PerlIO_printf(Perl_debug_log,
1510 "String too short [regexec_flags]...\n"));
1514 /* Check validity of program. */
1515 if (UCHARAT(prog->program) != REG_MAGIC) {
1516 Perl_croak(aTHX_ "corrupted regexp program");
1520 PL_reg_eval_set = 0;
1523 if (prog->reganch & ROPT_UTF8)
1524 PL_reg_flags |= RF_utf8;
1526 /* Mark beginning of line for ^ and lookbehind. */
1527 PL_regbol = startpos;
1531 /* Mark end of line for $ (and such) */
1534 /* see how far we have to get to not match where we matched before */
1535 PL_regtill = startpos+minend;
1537 /* We start without call_cc context. */
1540 /* If there is a "must appear" string, look for it. */
1543 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1546 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1547 PL_reg_ganch = startpos;
1548 else if (sv && SvTYPE(sv) >= SVt_PVMG
1550 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1551 && mg->mg_len >= 0) {
1552 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1553 if (prog->reganch & ROPT_ANCH_GPOS) {
1554 if (s > PL_reg_ganch)
1559 else /* pos() not defined */
1560 PL_reg_ganch = strbeg;
1563 if (do_utf8 == (UTF!=0) &&
1564 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1565 re_scream_pos_data d;
1567 d.scream_olds = &scream_olds;
1568 d.scream_pos = &scream_pos;
1569 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1571 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1572 goto phooey; /* not present */
1577 char *s = do_utf8 ? sv_uni_display(dsv, sv, 60, 0) : startpos;
1578 int len = do_utf8 ? strlen(s) : strend - startpos;
1581 PerlIO_printf(Perl_debug_log,
1582 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1583 PL_colors[4],PL_colors[5],PL_colors[0],
1586 (strlen(prog->precomp) > 60 ? "..." : ""),
1588 (int)(len > 60 ? 60 : len),
1590 (len > 60 ? "..." : "")
1594 /* Simplest case: anchored match need be tried only once. */
1595 /* [unless only anchor is BOL and multiline is set] */
1596 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1597 if (s == startpos && regtry(prog, startpos))
1599 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1600 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1605 dontbother = minlen - 1;
1606 end = HOP3c(strend, -dontbother, strbeg) - 1;
1607 /* for multiline we only have to try after newlines */
1608 if (prog->check_substr) {
1612 if (regtry(prog, s))
1617 if (prog->reganch & RE_USE_INTUIT) {
1618 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1629 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1630 if (regtry(prog, s))
1637 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1638 if (regtry(prog, PL_reg_ganch))
1643 /* Messy cases: unanchored match. */
1644 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1645 /* we have /x+whatever/ */
1646 /* it must be a one character string (XXXX Except UTF?) */
1647 char ch = SvPVX(prog->anchored_substr)[0];
1653 while (s < strend) {
1655 DEBUG_r( did_match = 1 );
1656 if (regtry(prog, s)) goto got_it;
1658 while (s < strend && *s == ch)
1665 while (s < strend) {
1667 DEBUG_r( did_match = 1 );
1668 if (regtry(prog, s)) goto got_it;
1670 while (s < strend && *s == ch)
1676 DEBUG_r(if (!did_match)
1677 PerlIO_printf(Perl_debug_log,
1678 "Did not find anchored character...\n")
1682 else if (do_utf8 == (UTF!=0) &&
1683 (prog->anchored_substr != Nullsv
1684 || (prog->float_substr != Nullsv
1685 && prog->float_max_offset < strend - s))) {
1686 SV *must = prog->anchored_substr
1687 ? prog->anchored_substr : prog->float_substr;
1689 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1691 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1692 char *last = HOP3c(strend, /* Cannot start after this */
1693 -(I32)(CHR_SVLEN(must)
1694 - (SvTAIL(must) != 0) + back_min), strbeg);
1695 char *last1; /* Last position checked before */
1701 last1 = HOPc(s, -1);
1703 last1 = s - 1; /* bogus */
1705 /* XXXX check_substr already used to find `s', can optimize if
1706 check_substr==must. */
1708 dontbother = end_shift;
1709 strend = HOPc(strend, -dontbother);
1710 while ( (s <= last) &&
1711 ((flags & REXEC_SCREAM)
1712 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1713 end_shift, &scream_pos, 0))
1714 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1715 (unsigned char*)strend, must,
1716 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1717 DEBUG_r( did_match = 1 );
1718 if (HOPc(s, -back_max) > last1) {
1719 last1 = HOPc(s, -back_min);
1720 s = HOPc(s, -back_max);
1723 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1725 last1 = HOPc(s, -back_min);
1729 while (s <= last1) {
1730 if (regtry(prog, s))
1736 while (s <= last1) {
1737 if (regtry(prog, s))
1743 DEBUG_r(if (!did_match)
1744 PerlIO_printf(Perl_debug_log,
1745 "Did not find %s substr `%s%.*s%s'%s...\n",
1746 ((must == prog->anchored_substr)
1747 ? "anchored" : "floating"),
1749 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1751 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1755 else if ((c = prog->regstclass)) {
1756 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1757 /* don't bother with what can't match */
1758 strend = HOPc(strend, -(minlen - 1));
1760 SV *prop = sv_newmortal();
1762 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), UTF ? sv_uni_display(dsv, sv, 60, 0) : s);
1764 if (find_byclass(prog, c, s, strend, startpos, 0))
1766 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1770 if (prog->float_substr != Nullsv) { /* Trim the end. */
1773 if (flags & REXEC_SCREAM) {
1774 last = screaminstr(sv, prog->float_substr, s - strbeg,
1775 end_shift, &scream_pos, 1); /* last one */
1777 last = scream_olds; /* Only one occurrence. */
1781 char *little = SvPV(prog->float_substr, len);
1783 if (SvTAIL(prog->float_substr)) {
1784 if (memEQ(strend - len + 1, little, len - 1))
1785 last = strend - len + 1;
1786 else if (!PL_multiline)
1787 last = memEQ(strend - len, little, len)
1788 ? strend - len : Nullch;
1794 last = rninstr(s, strend, little, little + len);
1796 last = strend; /* matching `$' */
1800 DEBUG_r(PerlIO_printf(Perl_debug_log,
1801 "%sCan't trim the tail, match fails (should not happen)%s\n",
1802 PL_colors[4],PL_colors[5]));
1803 goto phooey; /* Should not happen! */
1805 dontbother = strend - last + prog->float_min_offset;
1807 if (minlen && (dontbother < minlen))
1808 dontbother = minlen - 1;
1809 strend -= dontbother; /* this one's always in bytes! */
1810 /* We don't know much -- general case. */
1813 if (regtry(prog, s))
1822 if (regtry(prog, s))
1824 } while (s++ < strend);
1832 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1834 if (PL_reg_eval_set) {
1835 /* Preserve the current value of $^R */
1836 if (oreplsv != GvSV(PL_replgv))
1837 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1838 restored, the value remains
1840 restore_pos(aTHX_ 0);
1843 /* make sure $`, $&, $', and $digit will work later */
1844 if ( !(flags & REXEC_NOT_FIRST) ) {
1845 if (RX_MATCH_COPIED(prog)) {
1846 Safefree(prog->subbeg);
1847 RX_MATCH_COPIED_off(prog);
1849 if (flags & REXEC_COPY_STR) {
1850 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1852 s = savepvn(strbeg, i);
1855 RX_MATCH_COPIED_on(prog);
1858 prog->subbeg = strbeg;
1859 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1866 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1867 PL_colors[4],PL_colors[5]));
1868 if (PL_reg_eval_set)
1869 restore_pos(aTHX_ 0);
1874 - regtry - try match at specific point
1876 STATIC I32 /* 0 failure, 1 success */
1877 S_regtry(pTHX_ regexp *prog, char *startpos)
1885 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1887 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1890 PL_reg_eval_set = RS_init;
1892 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1893 (IV)(PL_stack_sp - PL_stack_base));
1895 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1896 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1897 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1899 /* Apparently this is not needed, judging by wantarray. */
1900 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1901 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1904 /* Make $_ available to executed code. */
1905 if (PL_reg_sv != DEFSV) {
1906 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1911 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1912 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1913 /* prepare for quick setting of pos */
1914 sv_magic(PL_reg_sv, (SV*)0,
1915 PERL_MAGIC_regex_global, Nullch, 0);
1916 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1920 PL_reg_oldpos = mg->mg_len;
1921 SAVEDESTRUCTOR_X(restore_pos, 0);
1923 if (!PL_reg_curpm) {
1924 Newz(22,PL_reg_curpm, 1, PMOP);
1927 SV* repointer = newSViv(0);
1928 /* so we know which PL_regex_padav element is PL_reg_curpm */
1929 SvFLAGS(repointer) |= SVf_BREAK;
1930 av_push(PL_regex_padav,repointer);
1931 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
1932 PL_regex_pad = AvARRAY(PL_regex_padav);
1936 PM_SETRE(PL_reg_curpm, prog);
1937 PL_reg_oldcurpm = PL_curpm;
1938 PL_curpm = PL_reg_curpm;
1939 if (RX_MATCH_COPIED(prog)) {
1940 /* Here is a serious problem: we cannot rewrite subbeg,
1941 since it may be needed if this match fails. Thus
1942 $` inside (?{}) could fail... */
1943 PL_reg_oldsaved = prog->subbeg;
1944 PL_reg_oldsavedlen = prog->sublen;
1945 RX_MATCH_COPIED_off(prog);
1948 PL_reg_oldsaved = Nullch;
1949 prog->subbeg = PL_bostr;
1950 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1952 prog->startp[0] = startpos - PL_bostr;
1953 PL_reginput = startpos;
1954 PL_regstartp = prog->startp;
1955 PL_regendp = prog->endp;
1956 PL_reglastparen = &prog->lastparen;
1957 PL_reglastcloseparen = &prog->lastcloseparen;
1958 prog->lastparen = 0;
1960 DEBUG_r(PL_reg_starttry = startpos);
1961 if (PL_reg_start_tmpl <= prog->nparens) {
1962 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1963 if(PL_reg_start_tmp)
1964 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1966 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1970 sv_setpvn(PERL_DEBUG_PAD(0), "", 0);
1971 sv_setpvn(PERL_DEBUG_PAD(1), "", 0);
1972 sv_setpvn(PERL_DEBUG_PAD(2), "", 0);
1975 /* XXXX What this code is doing here?!!! There should be no need
1976 to do this again and again, PL_reglastparen should take care of
1979 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1980 * Actually, the code in regcppop() (which Ilya may be meaning by
1981 * PL_reglastparen), is not needed at all by the test suite
1982 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1983 * enough, for building DynaLoader, or otherwise this
1984 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1985 * will happen. Meanwhile, this code *is* needed for the
1986 * above-mentioned test suite tests to succeed. The common theme
1987 * on those tests seems to be returning null fields from matches.
1992 if (prog->nparens) {
1993 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2000 if (regmatch(prog->program + 1)) {
2001 prog->endp[0] = PL_reginput - PL_bostr;
2004 REGCP_UNWIND(lastcp);
2008 #define RE_UNWIND_BRANCH 1
2009 #define RE_UNWIND_BRANCHJ 2
2013 typedef struct { /* XX: makes sense to enlarge it... */
2017 } re_unwind_generic_t;
2030 } re_unwind_branch_t;
2032 typedef union re_unwind_t {
2034 re_unwind_generic_t generic;
2035 re_unwind_branch_t branch;
2038 #define sayYES goto yes
2039 #define sayNO goto no
2040 #define sayYES_FINAL goto yes_final
2041 #define sayYES_LOUD goto yes_loud
2042 #define sayNO_FINAL goto no_final
2043 #define sayNO_SILENT goto do_no
2044 #define saySAME(x) if (x) goto yes; else goto no
2046 #define REPORT_CODE_OFF 24
2049 - regmatch - main matching routine
2051 * Conceptually the strategy is simple: check to see whether the current
2052 * node matches, call self recursively to see whether the rest matches,
2053 * and then act accordingly. In practice we make some effort to avoid
2054 * recursion, in particular by going through "ordinary" nodes (that don't
2055 * need to know whether the rest of the match failed) by a loop instead of
2058 /* [lwall] I've hoisted the register declarations to the outer block in order to
2059 * maybe save a little bit of pushing and popping on the stack. It also takes
2060 * advantage of machines that use a register save mask on subroutine entry.
2062 STATIC I32 /* 0 failure, 1 success */
2063 S_regmatch(pTHX_ regnode *prog)
2065 register regnode *scan; /* Current node. */
2066 regnode *next; /* Next node. */
2067 regnode *inner; /* Next node in internal branch. */
2068 register I32 nextchr; /* renamed nextchr - nextchar colides with
2069 function of same name */
2070 register I32 n; /* no or next */
2071 register I32 ln = 0; /* len or last */
2072 register char *s = Nullch; /* operand or save */
2073 register char *locinput = PL_reginput;
2074 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2075 int minmod = 0, sw = 0, logical = 0;
2078 I32 firstcp = PL_savestack_ix;
2080 register bool do_utf8 = PL_reg_match_utf8;
2082 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2083 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2084 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2091 /* Note that nextchr is a byte even in UTF */
2092 nextchr = UCHARAT(locinput);
2094 while (scan != NULL) {
2097 SV *prop = sv_newmortal();
2098 int docolor = *PL_colors[0];
2099 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2100 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2101 /* The part of the string before starttry has one color
2102 (pref0_len chars), between starttry and current
2103 position another one (pref_len - pref0_len chars),
2104 after the current position the third one.
2105 We assume that pref0_len <= pref_len, otherwise we
2106 decrease pref0_len. */
2107 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2108 ? (5 + taill) - l : locinput - PL_bostr;
2111 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2113 pref0_len = pref_len - (locinput - PL_reg_starttry);
2114 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2115 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2116 ? (5 + taill) - pref_len : PL_regeol - locinput);
2117 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2121 if (pref0_len > pref_len)
2122 pref0_len = pref_len;
2123 regprop(prop, scan);
2127 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2129 locinput - pref_len;
2130 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2131 char *s1 = do_utf8 ?
2132 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2133 pref_len - pref0_len, 60, 0) :
2134 locinput - pref_len + pref0_len;
2135 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2136 char *s2 = do_utf8 ?
2137 pv_uni_display(dsv2, (U8*)locinput,
2138 PL_regeol - locinput, 60, 0) :
2140 int len2 = do_utf8 ? strlen(s2) : l;
2141 PerlIO_printf(Perl_debug_log,
2142 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2143 (IV)(locinput - PL_bostr),
2150 (docolor ? "" : "> <"),
2154 15 - l - pref_len + 1,
2156 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2161 next = scan + NEXT_OFF(scan);
2167 if (locinput == PL_bostr || (PL_multiline &&
2168 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2170 /* regtill = regbol; */
2175 if (locinput == PL_bostr ||
2176 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2182 if (locinput == PL_bostr)
2186 if (locinput == PL_reg_ganch)
2196 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2201 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2203 if (PL_regeol - locinput > 1)
2207 if (PL_regeol != locinput)
2211 if (!nextchr && locinput >= PL_regeol)
2214 locinput += PL_utf8skip[nextchr];
2215 if (locinput > PL_regeol)
2217 nextchr = UCHARAT(locinput);
2220 nextchr = UCHARAT(++locinput);
2223 if (!nextchr && locinput >= PL_regeol)
2225 nextchr = UCHARAT(++locinput);
2228 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2231 locinput += PL_utf8skip[nextchr];
2232 if (locinput > PL_regeol)
2234 nextchr = UCHARAT(locinput);
2237 nextchr = UCHARAT(++locinput);
2242 if (do_utf8 != (UTF!=0)) {
2243 /* The target and the pattern have differing utf8ness. */
2249 /* The target is utf8, the pattern is not utf8. */
2253 if (NATIVE_TO_UNI(*(U8*)s) !=
2254 utf8_to_uvchr((U8*)l, &ulen))
2261 /* The target is not utf8, the pattern is utf8. */
2265 if (NATIVE_TO_UNI(*((U8*)l)) !=
2266 utf8_to_uvchr((U8*)s, &ulen))
2273 nextchr = UCHARAT(locinput);
2276 /* The target and the pattern have the same utf8ness. */
2277 /* Inline the first character, for speed. */
2278 if (UCHARAT(s) != nextchr)
2280 if (PL_regeol - locinput < ln)
2282 if (ln > 1 && memNE(s, locinput, ln))
2285 nextchr = UCHARAT(locinput);
2288 PL_reg_flags |= RF_tainted;
2297 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
2299 if (do_utf8 != (UTF!=0)) {
2300 /* The target and the pattern have differing utf8ness. */
2301 STRLEN ulen1, ulen2;
2305 /* The target is utf8, the pattern is not utf8. */
2310 cs = to_uni_fold(NATIVE_TO_UNI(*(U8*)s),
2312 cl = utf8_to_uvchr((U8*)l, &ulen2);
2315 cl = to_uni_fold(cl, (U8*)l, &ulen2);
2316 if (ulen1 != ulen2 || cs != cl)
2324 /* The target is not utf8, the pattern is utf8. */
2329 cs = utf8_to_uvchr((U8*)s, &ulen1);
2331 cl = to_uni_fold(NATIVE_TO_UNI(*(U8*)l),
2335 cs = to_uni_fold(cs, (U8*)s, &ulen1);
2336 if (ulen1 != ulen2 || cs != cl)
2344 nextchr = UCHARAT(locinput);
2348 if (do_utf8 && UTF) {
2349 /* Both the target and the pattern are utf8. */
2355 if (UTF8SKIP(s) != UTF8SKIP(l) ||
2356 memNE(s, (char*)l, UTF8SKIP(s))) {
2357 to_utf8_fold((U8*)l, tmpbuf, &ulen);
2358 if (UTF8SKIP(s) != ulen ||
2359 memNE(s, (char*)tmpbuf, ulen))
2366 nextchr = UCHARAT(locinput);
2371 /* Neither the target and the pattern are utf8. */
2373 /* Inline the first character, for speed. */
2374 if (UCHARAT(s) != nextchr &&
2375 UCHARAT(s) != ((OP(scan) == EXACTF)
2376 ? PL_fold : PL_fold_locale)[nextchr])
2378 if (PL_regeol - locinput < ln)
2380 if (ln > 1 && (OP(scan) == EXACTF
2381 ? ibcmp(s, locinput, ln)
2382 : ibcmp_locale(s, locinput, ln)))
2385 nextchr = UCHARAT(locinput);
2389 if (!reginclass(scan, (U8*)locinput, do_utf8))
2391 if (locinput >= PL_regeol)
2393 locinput += PL_utf8skip[nextchr];
2394 nextchr = UCHARAT(locinput);
2398 nextchr = UCHARAT(locinput);
2399 if (!reginclass(scan, (U8*)locinput, do_utf8))
2401 if (!nextchr && locinput >= PL_regeol)
2403 nextchr = UCHARAT(++locinput);
2407 PL_reg_flags |= RF_tainted;
2413 LOAD_UTF8_CHARCLASS(alnum,"a");
2414 if (!(OP(scan) == ALNUM
2415 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2416 : isALNUM_LC_utf8((U8*)locinput)))
2420 locinput += PL_utf8skip[nextchr];
2421 nextchr = UCHARAT(locinput);
2424 if (!(OP(scan) == ALNUM
2425 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2427 nextchr = UCHARAT(++locinput);
2430 PL_reg_flags |= RF_tainted;
2433 if (!nextchr && locinput >= PL_regeol)
2436 LOAD_UTF8_CHARCLASS(alnum,"a");
2437 if (OP(scan) == NALNUM
2438 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2439 : isALNUM_LC_utf8((U8*)locinput))
2443 locinput += PL_utf8skip[nextchr];
2444 nextchr = UCHARAT(locinput);
2447 if (OP(scan) == NALNUM
2448 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2450 nextchr = UCHARAT(++locinput);
2454 PL_reg_flags |= RF_tainted;
2458 /* was last char in word? */
2460 if (locinput == PL_bostr)
2463 U8 *r = reghop((U8*)locinput, -1);
2465 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2467 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2468 ln = isALNUM_uni(ln);
2469 LOAD_UTF8_CHARCLASS(alnum,"a");
2470 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2473 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2474 n = isALNUM_LC_utf8((U8*)locinput);
2478 ln = (locinput != PL_bostr) ?
2479 UCHARAT(locinput - 1) : '\n';
2480 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2482 n = isALNUM(nextchr);
2485 ln = isALNUM_LC(ln);
2486 n = isALNUM_LC(nextchr);
2489 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2490 OP(scan) == BOUNDL))
2494 PL_reg_flags |= RF_tainted;
2500 if (UTF8_IS_CONTINUED(nextchr)) {
2501 LOAD_UTF8_CHARCLASS(space," ");
2502 if (!(OP(scan) == SPACE
2503 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2504 : isSPACE_LC_utf8((U8*)locinput)))
2508 locinput += PL_utf8skip[nextchr];
2509 nextchr = UCHARAT(locinput);
2512 if (!(OP(scan) == SPACE
2513 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2515 nextchr = UCHARAT(++locinput);
2518 if (!(OP(scan) == SPACE
2519 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2521 nextchr = UCHARAT(++locinput);
2525 PL_reg_flags |= RF_tainted;
2528 if (!nextchr && locinput >= PL_regeol)
2531 LOAD_UTF8_CHARCLASS(space," ");
2532 if (OP(scan) == NSPACE
2533 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2534 : isSPACE_LC_utf8((U8*)locinput))
2538 locinput += PL_utf8skip[nextchr];
2539 nextchr = UCHARAT(locinput);
2542 if (OP(scan) == NSPACE
2543 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2545 nextchr = UCHARAT(++locinput);
2548 PL_reg_flags |= RF_tainted;
2554 LOAD_UTF8_CHARCLASS(digit,"0");
2555 if (!(OP(scan) == DIGIT
2556 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2557 : isDIGIT_LC_utf8((U8*)locinput)))
2561 locinput += PL_utf8skip[nextchr];
2562 nextchr = UCHARAT(locinput);
2565 if (!(OP(scan) == DIGIT
2566 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2568 nextchr = UCHARAT(++locinput);
2571 PL_reg_flags |= RF_tainted;
2574 if (!nextchr && locinput >= PL_regeol)
2577 LOAD_UTF8_CHARCLASS(digit,"0");
2578 if (OP(scan) == NDIGIT
2579 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2580 : isDIGIT_LC_utf8((U8*)locinput))
2584 locinput += PL_utf8skip[nextchr];
2585 nextchr = UCHARAT(locinput);
2588 if (OP(scan) == NDIGIT
2589 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2591 nextchr = UCHARAT(++locinput);
2594 LOAD_UTF8_CHARCLASS(mark,"~");
2595 if (locinput >= PL_regeol ||
2596 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2598 locinput += PL_utf8skip[nextchr];
2599 while (locinput < PL_regeol &&
2600 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2601 locinput += UTF8SKIP(locinput);
2602 if (locinput > PL_regeol)
2604 nextchr = UCHARAT(locinput);
2607 PL_reg_flags |= RF_tainted;
2611 n = ARG(scan); /* which paren pair */
2612 ln = PL_regstartp[n];
2613 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2614 if (*PL_reglastparen < n || ln == -1)
2615 sayNO; /* Do not match unless seen CLOSEn. */
2616 if (ln == PL_regendp[n])
2620 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2622 char *e = PL_bostr + PL_regendp[n];
2624 * Note that we can't do the "other character" lookup trick as
2625 * in the 8-bit case (no pun intended) because in Unicode we
2626 * have to map both upper and title case to lower case.
2628 if (OP(scan) == REFF) {
2629 STRLEN ulen1, ulen2;
2630 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2631 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2635 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2636 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2637 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2644 nextchr = UCHARAT(locinput);
2648 /* Inline the first character, for speed. */
2649 if (UCHARAT(s) != nextchr &&
2651 (UCHARAT(s) != ((OP(scan) == REFF
2652 ? PL_fold : PL_fold_locale)[nextchr]))))
2654 ln = PL_regendp[n] - ln;
2655 if (locinput + ln > PL_regeol)
2657 if (ln > 1 && (OP(scan) == REF
2658 ? memNE(s, locinput, ln)
2660 ? ibcmp(s, locinput, ln)
2661 : ibcmp_locale(s, locinput, ln))))
2664 nextchr = UCHARAT(locinput);
2675 OP_4tree *oop = PL_op;
2676 COP *ocurcop = PL_curcop;
2677 SV **ocurpad = PL_curpad;
2681 PL_op = (OP_4tree*)PL_regdata->data[n];
2682 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2683 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2684 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2688 CALLRUNOPS(aTHX); /* Scalar context. */
2691 ret = Nullsv; /* protect against empty (?{}) blocks. */
2699 PL_curpad = ocurpad;
2700 PL_curcop = ocurcop;
2702 if (logical == 2) { /* Postponed subexpression. */
2704 MAGIC *mg = Null(MAGIC*);
2706 CHECKPOINT cp, lastcp;
2708 if(SvROK(ret) || SvRMAGICAL(ret)) {
2709 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2712 mg = mg_find(sv, PERL_MAGIC_qr);
2715 re = (regexp *)mg->mg_obj;
2716 (void)ReREFCNT_inc(re);
2720 char *t = SvPV(ret, len);
2722 char *oprecomp = PL_regprecomp;
2723 I32 osize = PL_regsize;
2724 I32 onpar = PL_regnpar;
2727 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2729 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2730 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2732 PL_regprecomp = oprecomp;
2737 PerlIO_printf(Perl_debug_log,
2738 "Entering embedded `%s%.60s%s%s'\n",
2742 (strlen(re->precomp) > 60 ? "..." : ""))
2745 state.prev = PL_reg_call_cc;
2746 state.cc = PL_regcc;
2747 state.re = PL_reg_re;
2751 cp = regcppush(0); /* Save *all* the positions. */
2754 state.ss = PL_savestack_ix;
2755 *PL_reglastparen = 0;
2756 *PL_reglastcloseparen = 0;
2757 PL_reg_call_cc = &state;
2758 PL_reginput = locinput;
2760 /* XXXX This is too dramatic a measure... */
2763 if (regmatch(re->program + 1)) {
2764 /* Even though we succeeded, we need to restore
2765 global variables, since we may be wrapped inside
2766 SUSPEND, thus the match may be not finished yet. */
2768 /* XXXX Do this only if SUSPENDed? */
2769 PL_reg_call_cc = state.prev;
2770 PL_regcc = state.cc;
2771 PL_reg_re = state.re;
2772 cache_re(PL_reg_re);
2774 /* XXXX This is too dramatic a measure... */
2777 /* These are needed even if not SUSPEND. */
2783 REGCP_UNWIND(lastcp);
2785 PL_reg_call_cc = state.prev;
2786 PL_regcc = state.cc;
2787 PL_reg_re = state.re;
2788 cache_re(PL_reg_re);
2790 /* XXXX This is too dramatic a measure... */
2800 sv_setsv(save_scalar(PL_replgv), ret);
2804 n = ARG(scan); /* which paren pair */
2805 PL_reg_start_tmp[n] = locinput;
2810 n = ARG(scan); /* which paren pair */
2811 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2812 PL_regendp[n] = locinput - PL_bostr;
2813 if (n > *PL_reglastparen)
2814 *PL_reglastparen = n;
2815 *PL_reglastcloseparen = n;
2818 n = ARG(scan); /* which paren pair */
2819 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2822 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2824 next = NEXTOPER(NEXTOPER(scan));
2826 next = scan + ARG(scan);
2827 if (OP(next) == IFTHEN) /* Fake one. */
2828 next = NEXTOPER(NEXTOPER(next));
2832 logical = scan->flags;
2834 /*******************************************************************
2835 PL_regcc contains infoblock about the innermost (...)* loop, and
2836 a pointer to the next outer infoblock.
2838 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2840 1) After matching X, regnode for CURLYX is processed;
2842 2) This regnode creates infoblock on the stack, and calls
2843 regmatch() recursively with the starting point at WHILEM node;
2845 3) Each hit of WHILEM node tries to match A and Z (in the order
2846 depending on the current iteration, min/max of {min,max} and
2847 greediness). The information about where are nodes for "A"
2848 and "Z" is read from the infoblock, as is info on how many times "A"
2849 was already matched, and greediness.
2851 4) After A matches, the same WHILEM node is hit again.
2853 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2854 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2855 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2856 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2857 of the external loop.
2859 Currently present infoblocks form a tree with a stem formed by PL_curcc
2860 and whatever it mentions via ->next, and additional attached trees
2861 corresponding to temporarily unset infoblocks as in "5" above.
2863 In the following picture infoblocks for outer loop of
2864 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2865 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2866 infoblocks are drawn below the "reset" infoblock.
2868 In fact in the picture below we do not show failed matches for Z and T
2869 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2870 more obvious *why* one needs to *temporary* unset infoblocks.]
2872 Matched REx position InfoBlocks Comment
2876 Y A)*?Z)*?T x <- O <- I
2877 YA )*?Z)*?T x <- O <- I
2878 YA A)*?Z)*?T x <- O <- I
2879 YAA )*?Z)*?T x <- O <- I
2880 YAA Z)*?T x <- O # Temporary unset I
2883 YAAZ Y(A)*?Z)*?T x <- O
2886 YAAZY (A)*?Z)*?T x <- O
2889 YAAZY A)*?Z)*?T x <- O <- I
2892 YAAZYA )*?Z)*?T x <- O <- I
2895 YAAZYA Z)*?T x <- O # Temporary unset I
2901 YAAZYAZ T x # Temporary unset O
2908 *******************************************************************/
2911 CHECKPOINT cp = PL_savestack_ix;
2912 /* No need to save/restore up to this paren */
2913 I32 parenfloor = scan->flags;
2915 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2917 cc.oldcc = PL_regcc;
2919 /* XXXX Probably it is better to teach regpush to support
2920 parenfloor > PL_regsize... */
2921 if (parenfloor > *PL_reglastparen)
2922 parenfloor = *PL_reglastparen; /* Pessimization... */
2923 cc.parenfloor = parenfloor;
2925 cc.min = ARG1(scan);
2926 cc.max = ARG2(scan);
2927 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2931 PL_reginput = locinput;
2932 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2934 PL_regcc = cc.oldcc;
2940 * This is really hard to understand, because after we match
2941 * what we're trying to match, we must make sure the rest of
2942 * the REx is going to match for sure, and to do that we have
2943 * to go back UP the parse tree by recursing ever deeper. And
2944 * if it fails, we have to reset our parent's current state
2945 * that we can try again after backing off.
2948 CHECKPOINT cp, lastcp;
2949 CURCUR* cc = PL_regcc;
2950 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2952 n = cc->cur + 1; /* how many we know we matched */
2953 PL_reginput = locinput;
2956 PerlIO_printf(Perl_debug_log,
2957 "%*s %ld out of %ld..%ld cc=%lx\n",
2958 REPORT_CODE_OFF+PL_regindent*2, "",
2959 (long)n, (long)cc->min,
2960 (long)cc->max, (long)cc)
2963 /* If degenerate scan matches "", assume scan done. */
2965 if (locinput == cc->lastloc && n >= cc->min) {
2966 PL_regcc = cc->oldcc;
2970 PerlIO_printf(Perl_debug_log,
2971 "%*s empty match detected, try continuation...\n",
2972 REPORT_CODE_OFF+PL_regindent*2, "")
2974 if (regmatch(cc->next))
2982 /* First just match a string of min scans. */
2986 cc->lastloc = locinput;
2987 if (regmatch(cc->scan))
2990 cc->lastloc = lastloc;
2995 /* Check whether we already were at this position.
2996 Postpone detection until we know the match is not
2997 *that* much linear. */
2998 if (!PL_reg_maxiter) {
2999 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3000 PL_reg_leftiter = PL_reg_maxiter;
3002 if (PL_reg_leftiter-- == 0) {
3003 I32 size = (PL_reg_maxiter + 7)/8;
3004 if (PL_reg_poscache) {
3005 if (PL_reg_poscache_size < size) {
3006 Renew(PL_reg_poscache, size, char);
3007 PL_reg_poscache_size = size;
3009 Zero(PL_reg_poscache, size, char);
3012 PL_reg_poscache_size = size;
3013 Newz(29, PL_reg_poscache, size, char);
3016 PerlIO_printf(Perl_debug_log,
3017 "%sDetected a super-linear match, switching on caching%s...\n",
3018 PL_colors[4], PL_colors[5])
3021 if (PL_reg_leftiter < 0) {
3022 I32 o = locinput - PL_bostr, b;
3024 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3027 if (PL_reg_poscache[o] & (1<<b)) {
3029 PerlIO_printf(Perl_debug_log,
3030 "%*s already tried at this position...\n",
3031 REPORT_CODE_OFF+PL_regindent*2, "")
3035 PL_reg_poscache[o] |= (1<<b);
3039 /* Prefer next over scan for minimal matching. */
3042 PL_regcc = cc->oldcc;
3045 cp = regcppush(cc->parenfloor);
3047 if (regmatch(cc->next)) {
3049 sayYES; /* All done. */
3051 REGCP_UNWIND(lastcp);
3057 if (n >= cc->max) { /* Maximum greed exceeded? */
3058 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3059 && !(PL_reg_flags & RF_warned)) {
3060 PL_reg_flags |= RF_warned;
3061 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3062 "Complex regular subexpression recursion",
3069 PerlIO_printf(Perl_debug_log,
3070 "%*s trying longer...\n",
3071 REPORT_CODE_OFF+PL_regindent*2, "")
3073 /* Try scanning more and see if it helps. */
3074 PL_reginput = locinput;
3076 cc->lastloc = locinput;
3077 cp = regcppush(cc->parenfloor);
3079 if (regmatch(cc->scan)) {
3083 REGCP_UNWIND(lastcp);
3086 cc->lastloc = lastloc;
3090 /* Prefer scan over next for maximal matching. */
3092 if (n < cc->max) { /* More greed allowed? */
3093 cp = regcppush(cc->parenfloor);
3095 cc->lastloc = locinput;
3097 if (regmatch(cc->scan)) {
3101 REGCP_UNWIND(lastcp);
3102 regcppop(); /* Restore some previous $<digit>s? */
3103 PL_reginput = locinput;
3105 PerlIO_printf(Perl_debug_log,
3106 "%*s failed, try continuation...\n",
3107 REPORT_CODE_OFF+PL_regindent*2, "")
3110 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3111 && !(PL_reg_flags & RF_warned)) {
3112 PL_reg_flags |= RF_warned;
3113 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
3114 "Complex regular subexpression recursion",
3118 /* Failed deeper matches of scan, so see if this one works. */
3119 PL_regcc = cc->oldcc;
3122 if (regmatch(cc->next))
3128 cc->lastloc = lastloc;
3133 next = scan + ARG(scan);
3136 inner = NEXTOPER(NEXTOPER(scan));
3139 inner = NEXTOPER(scan);
3143 if (OP(next) != c1) /* No choice. */
3144 next = inner; /* Avoid recursion. */
3146 I32 lastparen = *PL_reglastparen;
3148 re_unwind_branch_t *uw;
3150 /* Put unwinding data on stack */
3151 unwind1 = SSNEWt(1,re_unwind_branch_t);
3152 uw = SSPTRt(unwind1,re_unwind_branch_t);
3155 uw->type = ((c1 == BRANCH)
3157 : RE_UNWIND_BRANCHJ);
3158 uw->lastparen = lastparen;
3160 uw->locinput = locinput;
3161 uw->nextchr = nextchr;
3163 uw->regindent = ++PL_regindent;
3166 REGCP_SET(uw->lastcp);
3168 /* Now go into the first branch */
3181 /* We suppose that the next guy does not need
3182 backtracking: in particular, it is of constant length,
3183 and has no parenths to influence future backrefs. */
3184 ln = ARG1(scan); /* min to match */
3185 n = ARG2(scan); /* max to match */
3186 paren = scan->flags;
3188 if (paren > PL_regsize)
3190 if (paren > *PL_reglastparen)
3191 *PL_reglastparen = paren;
3193 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3195 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3196 PL_reginput = locinput;
3199 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3201 /* if we matched something zero-length we don't need to
3202 backtrack - capturing parens are already defined, so
3203 the caveat in the maximal case doesn't apply
3205 XXXX if ln == 0, we can redo this check first time
3206 through the following loop
3209 n = ln; /* don't backtrack */
3210 locinput = PL_reginput;
3211 if (HAS_TEXT(next) || JUMPABLE(next)) {
3212 regnode *text_node = next;
3214 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3216 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3218 if (PL_regkind[(U8)OP(text_node)] == REF) {
3220 n = ARG(text_node); /* which paren pair */
3221 ln = PL_regstartp[n];
3222 /* assume yes if we haven't seen CLOSEn */
3224 *PL_reglastparen < n ||
3231 c1 = *(PL_bostr + ln);
3233 else { c1 = (U8)*STRING(text_node); }
3234 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3236 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3237 c2 = PL_fold_locale[c1];
3246 /* This may be improved if l == 0. */
3247 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3248 /* If it could work, try it. */
3250 UCHARAT(PL_reginput) == c1 ||
3251 UCHARAT(PL_reginput) == c2)
3255 PL_regstartp[paren] =
3256 HOPc(PL_reginput, -l) - PL_bostr;
3257 PL_regendp[paren] = PL_reginput - PL_bostr;
3260 PL_regendp[paren] = -1;
3264 REGCP_UNWIND(lastcp);
3266 /* Couldn't or didn't -- move forward. */
3267 PL_reginput = locinput;
3268 if (regrepeat_hard(scan, 1, &l)) {
3270 locinput = PL_reginput;
3277 n = regrepeat_hard(scan, n, &l);
3278 /* if we matched something zero-length we don't need to
3279 backtrack, unless the minimum count is zero and we
3280 are capturing the result - in that case the capture
3281 being defined or not may affect later execution
3283 if (n != 0 && l == 0 && !(paren && ln == 0))
3284 ln = n; /* don't backtrack */
3285 locinput = PL_reginput;
3287 PerlIO_printf(Perl_debug_log,
3288 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3289 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3293 if (HAS_TEXT(next) || JUMPABLE(next)) {
3294 regnode *text_node = next;
3296 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3298 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3300 if (PL_regkind[(U8)OP(text_node)] == REF) {
3302 n = ARG(text_node); /* which paren pair */
3303 ln = PL_regstartp[n];
3304 /* assume yes if we haven't seen CLOSEn */
3306 *PL_reglastparen < n ||
3313 c1 = *(PL_bostr + ln);
3315 else { c1 = (U8)*STRING(text_node); }
3317 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3319 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3320 c2 = PL_fold_locale[c1];
3331 /* If it could work, try it. */
3333 UCHARAT(PL_reginput) == c1 ||
3334 UCHARAT(PL_reginput) == c2)
3337 PerlIO_printf(Perl_debug_log,
3338 "%*s trying tail with n=%"IVdf"...\n",
3339 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3343 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3344 PL_regendp[paren] = PL_reginput - PL_bostr;
3347 PL_regendp[paren] = -1;
3351 REGCP_UNWIND(lastcp);
3353 /* Couldn't or didn't -- back up. */
3355 locinput = HOPc(locinput, -l);
3356 PL_reginput = locinput;
3363 paren = scan->flags; /* Which paren to set */
3364 if (paren > PL_regsize)
3366 if (paren > *PL_reglastparen)
3367 *PL_reglastparen = paren;
3368 ln = ARG1(scan); /* min to match */
3369 n = ARG2(scan); /* max to match */
3370 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3374 ln = ARG1(scan); /* min to match */
3375 n = ARG2(scan); /* max to match */
3376 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3381 scan = NEXTOPER(scan);
3387 scan = NEXTOPER(scan);
3391 * Lookahead to avoid useless match attempts
3392 * when we know what character comes next.
3396 * Used to only do .*x and .*?x, but now it allows
3397 * for )'s, ('s and (?{ ... })'s to be in the way
3398 * of the quantifier and the EXACT-like node. -- japhy
3401 if (HAS_TEXT(next) || JUMPABLE(next)) {
3403 regnode *text_node = next;
3405 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3407 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3409 if (PL_regkind[(U8)OP(text_node)] == REF) {
3411 n = ARG(text_node); /* which paren pair */
3412 ln = PL_regstartp[n];
3413 /* assume yes if we haven't seen CLOSEn */
3415 *PL_reglastparen < n ||
3420 goto assume_ok_easy;
3422 s = (U8*)PL_bostr + ln;
3424 else { s = (U8*)STRING(text_node); }
3428 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3430 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3431 c2 = PL_fold_locale[c1];
3434 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3435 STRLEN ulen1, ulen2;
3436 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3437 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3439 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3440 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3442 c1 = utf8_to_uvuni(tmpbuf1, 0);
3443 c2 = utf8_to_uvuni(tmpbuf2, 0);
3446 c2 = c1 = utf8_to_uvchr(s, NULL);
3454 PL_reginput = locinput;
3458 if (ln && regrepeat(scan, ln) < ln)
3460 locinput = PL_reginput;
3463 char *e; /* Should not check after this */
3464 char *old = locinput;
3466 if (n == REG_INFTY) {
3469 while (UTF8_IS_CONTINUATION(*(U8*)e))
3475 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3479 e = locinput + n - ln;
3485 /* Find place 'next' could work */
3488 while (locinput <= e &&
3489 UCHARAT(locinput) != c1)
3492 while (locinput <= e
3493 && UCHARAT(locinput) != c1
3494 && UCHARAT(locinput) != c2)
3497 count = locinput - old;
3504 utf8_to_uvchr((U8*)locinput, &len) != c1;
3509 for (count = 0; locinput <= e; count++) {
3510 UV c = utf8_to_uvchr((U8*)locinput, &len);
3511 if (c == c1 || c == c2)
3519 /* PL_reginput == old now */
3520 if (locinput != old) {
3521 ln = 1; /* Did some */
3522 if (regrepeat(scan, count) < count)
3525 /* PL_reginput == locinput now */
3526 TRYPAREN(paren, ln, locinput);
3527 PL_reginput = locinput; /* Could be reset... */
3528 REGCP_UNWIND(lastcp);
3529 /* Couldn't or didn't -- move forward. */
3532 locinput += UTF8SKIP(locinput);
3538 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3542 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3544 c = UCHARAT(PL_reginput);
3545 /* If it could work, try it. */
3546 if (c == c1 || c == c2)
3548 TRYPAREN(paren, n, PL_reginput);
3549 REGCP_UNWIND(lastcp);
3552 /* If it could work, try it. */
3553 else if (c1 == -1000)
3555 TRYPAREN(paren, n, PL_reginput);
3556 REGCP_UNWIND(lastcp);
3558 /* Couldn't or didn't -- move forward. */
3559 PL_reginput = locinput;
3560 if (regrepeat(scan, 1)) {
3562 locinput = PL_reginput;
3570 n = regrepeat(scan, n);
3571 locinput = PL_reginput;
3572 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3573 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3574 ln = n; /* why back off? */
3575 /* ...because $ and \Z can match before *and* after
3576 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3577 We should back off by one in this case. */
3578 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3587 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3589 c = UCHARAT(PL_reginput);
3591 /* If it could work, try it. */
3592 if (c1 == -1000 || c == c1 || c == c2)
3594 TRYPAREN(paren, n, PL_reginput);
3595 REGCP_UNWIND(lastcp);
3597 /* Couldn't or didn't -- back up. */
3599 PL_reginput = locinput = HOPc(locinput, -1);
3607 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3609 c = UCHARAT(PL_reginput);
3611 /* If it could work, try it. */
3612 if (c1 == -1000 || c == c1 || c == c2)
3614 TRYPAREN(paren, n, PL_reginput);
3615 REGCP_UNWIND(lastcp);
3617 /* Couldn't or didn't -- back up. */
3619 PL_reginput = locinput = HOPc(locinput, -1);
3626 if (PL_reg_call_cc) {
3627 re_cc_state *cur_call_cc = PL_reg_call_cc;
3628 CURCUR *cctmp = PL_regcc;
3629 regexp *re = PL_reg_re;
3630 CHECKPOINT cp, lastcp;
3632 cp = regcppush(0); /* Save *all* the positions. */
3634 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3636 PL_reginput = locinput; /* Make position available to
3638 cache_re(PL_reg_call_cc->re);
3639 PL_regcc = PL_reg_call_cc->cc;
3640 PL_reg_call_cc = PL_reg_call_cc->prev;
3641 if (regmatch(cur_call_cc->node)) {
3642 PL_reg_call_cc = cur_call_cc;
3646 REGCP_UNWIND(lastcp);
3648 PL_reg_call_cc = cur_call_cc;
3654 PerlIO_printf(Perl_debug_log,
3655 "%*s continuation failed...\n",
3656 REPORT_CODE_OFF+PL_regindent*2, "")
3660 if (locinput < PL_regtill) {
3661 DEBUG_r(PerlIO_printf(Perl_debug_log,
3662 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3664 (long)(locinput - PL_reg_starttry),
3665 (long)(PL_regtill - PL_reg_starttry),
3667 sayNO_FINAL; /* Cannot match: too short. */
3669 PL_reginput = locinput; /* put where regtry can find it */
3670 sayYES_FINAL; /* Success! */
3672 PL_reginput = locinput; /* put where regtry can find it */
3673 sayYES_LOUD; /* Success! */
3676 PL_reginput = locinput;
3681 s = HOPBACKc(locinput, scan->flags);
3687 PL_reginput = locinput;
3692 s = HOPBACKc(locinput, scan->flags);
3698 PL_reginput = locinput;
3701 inner = NEXTOPER(NEXTOPER(scan));
3702 if (regmatch(inner) != n) {
3717 if (OP(scan) == SUSPEND) {
3718 locinput = PL_reginput;
3719 nextchr = UCHARAT(locinput);
3724 next = scan + ARG(scan);
3729 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3730 PTR2UV(scan), OP(scan));
3731 Perl_croak(aTHX_ "regexp memory corruption");
3738 * We get here only if there's trouble -- normally "case END" is
3739 * the terminating point.
3741 Perl_croak(aTHX_ "corrupted regexp pointers");
3747 PerlIO_printf(Perl_debug_log,
3748 "%*s %scould match...%s\n",
3749 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3753 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3754 PL_colors[4],PL_colors[5]));
3760 #if 0 /* Breaks $^R */
3768 PerlIO_printf(Perl_debug_log,
3769 "%*s %sfailed...%s\n",
3770 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3776 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3779 case RE_UNWIND_BRANCH:
3780 case RE_UNWIND_BRANCHJ:
3782 re_unwind_branch_t *uwb = &(uw->branch);
3783 I32 lastparen = uwb->lastparen;
3785 REGCP_UNWIND(uwb->lastcp);
3786 for (n = *PL_reglastparen; n > lastparen; n--)
3788 *PL_reglastparen = n;
3789 scan = next = uwb->next;
3791 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3792 ? BRANCH : BRANCHJ) ) { /* Failure */
3799 /* Have more choice yet. Reuse the same uwb. */
3801 if ((n = (uwb->type == RE_UNWIND_BRANCH
3802 ? NEXT_OFF(next) : ARG(next))))
3805 next = NULL; /* XXXX Needn't unwinding in this case... */
3807 next = NEXTOPER(scan);
3808 if (uwb->type == RE_UNWIND_BRANCHJ)
3809 next = NEXTOPER(next);
3810 locinput = uwb->locinput;
3811 nextchr = uwb->nextchr;
3813 PL_regindent = uwb->regindent;
3820 Perl_croak(aTHX_ "regexp unwind memory corruption");
3831 - regrepeat - repeatedly match something simple, report how many
3834 * [This routine now assumes that it will only match on things of length 1.
3835 * That was true before, but now we assume scan - reginput is the count,
3836 * rather than incrementing count on every character. [Er, except utf8.]]
3839 S_regrepeat(pTHX_ regnode *p, I32 max)
3841 register char *scan;
3843 register char *loceol = PL_regeol;
3844 register I32 hardcount = 0;
3845 register bool do_utf8 = PL_reg_match_utf8;
3848 if (max != REG_INFTY && max < loceol - scan)
3849 loceol = scan + max;
3854 while (scan < loceol && hardcount < max && *scan != '\n') {
3855 scan += UTF8SKIP(scan);
3859 while (scan < loceol && *scan != '\n')
3869 case EXACT: /* length of string is 1 */
3871 while (scan < loceol && UCHARAT(scan) == c)
3874 case EXACTF: /* length of string is 1 */
3876 while (scan < loceol &&
3877 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3880 case EXACTFL: /* length of string is 1 */
3881 PL_reg_flags |= RF_tainted;
3883 while (scan < loceol &&
3884 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3890 while (hardcount < max && scan < loceol &&
3891 reginclass(p, (U8*)scan, do_utf8)) {
3892 scan += UTF8SKIP(scan);
3896 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3903 LOAD_UTF8_CHARCLASS(alnum,"a");
3904 while (hardcount < max && scan < loceol &&
3905 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3906 scan += UTF8SKIP(scan);
3910 while (scan < loceol && isALNUM(*scan))
3915 PL_reg_flags |= RF_tainted;
3918 while (hardcount < max && scan < loceol &&
3919 isALNUM_LC_utf8((U8*)scan)) {
3920 scan += UTF8SKIP(scan);
3924 while (scan < loceol && isALNUM_LC(*scan))
3931 LOAD_UTF8_CHARCLASS(alnum,"a");
3932 while (hardcount < max && scan < loceol &&
3933 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3934 scan += UTF8SKIP(scan);
3938 while (scan < loceol && !isALNUM(*scan))
3943 PL_reg_flags |= RF_tainted;
3946 while (hardcount < max && scan < loceol &&
3947 !isALNUM_LC_utf8((U8*)scan)) {
3948 scan += UTF8SKIP(scan);
3952 while (scan < loceol && !isALNUM_LC(*scan))
3959 LOAD_UTF8_CHARCLASS(space," ");
3960 while (hardcount < max && scan < loceol &&
3962 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3963 scan += UTF8SKIP(scan);
3967 while (scan < loceol && isSPACE(*scan))
3972 PL_reg_flags |= RF_tainted;
3975 while (hardcount < max && scan < loceol &&
3976 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3977 scan += UTF8SKIP(scan);
3981 while (scan < loceol && isSPACE_LC(*scan))
3988 LOAD_UTF8_CHARCLASS(space," ");
3989 while (hardcount < max && scan < loceol &&
3991 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3992 scan += UTF8SKIP(scan);
3996 while (scan < loceol && !isSPACE(*scan))
4001 PL_reg_flags |= RF_tainted;
4004 while (hardcount < max && scan < loceol &&
4005 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4006 scan += UTF8SKIP(scan);
4010 while (scan < loceol && !isSPACE_LC(*scan))
4017 LOAD_UTF8_CHARCLASS(digit,"0");
4018 while (hardcount < max && scan < loceol &&
4019 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4020 scan += UTF8SKIP(scan);
4024 while (scan < loceol && isDIGIT(*scan))
4031 LOAD_UTF8_CHARCLASS(digit,"0");
4032 while (hardcount < max && scan < loceol &&
4033 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4034 scan += UTF8SKIP(scan);
4038 while (scan < loceol && !isDIGIT(*scan))
4042 default: /* Called on something of 0 width. */
4043 break; /* So match right here or not at all. */
4049 c = scan - PL_reginput;
4054 SV *prop = sv_newmortal();
4057 PerlIO_printf(Perl_debug_log,
4058 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4059 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4066 - regrepeat_hard - repeatedly match something, report total lenth and length
4068 * The repeater is supposed to have constant length.
4072 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4074 register char *scan = Nullch;
4075 register char *start;
4076 register char *loceol = PL_regeol;
4078 I32 count = 0, res = 1;
4083 start = PL_reginput;
4084 if (PL_reg_match_utf8) {
4085 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4088 while (start < PL_reginput) {
4090 start += UTF8SKIP(start);
4101 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4103 *lp = l = PL_reginput - start;
4104 if (max != REG_INFTY && l*max < loceol - scan)
4105 loceol = scan + l*max;
4118 - regclass_swash - prepare the utf8 swash
4122 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
4127 if (PL_regdata && PL_regdata->count) {
4130 if (PL_regdata->what[n] == 's') {
4131 SV *rv = (SV*)PL_regdata->data[n];
4132 AV *av = (AV*)SvRV((SV*)rv);
4135 si = *av_fetch(av, 0, FALSE);
4136 a = av_fetch(av, 1, FALSE);
4140 else if (si && doinit) {
4141 sw = swash_init("utf8", "", si, 1, 0);
4142 (void)av_store(av, 1, sw);
4154 - reginclass - determine if a character falls into a character class
4158 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
4160 char flags = ANYOF_FLAGS(n);
4165 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4167 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4168 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4169 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4172 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4175 SV *sw = regclass_swash(n, TRUE, 0);
4178 if (swash_fetch(sw, p, do_utf8))
4180 else if (flags & ANYOF_FOLD) {
4182 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4184 to_utf8_fold(p, tmpbuf, &ulen);
4185 if (swash_fetch(sw, tmpbuf, do_utf8))
4187 to_utf8_upper(p, tmpbuf, &ulen);
4188 if (swash_fetch(sw, tmpbuf, do_utf8))
4194 if (!match && c < 256) {
4195 if (ANYOF_BITMAP_TEST(n, c))
4197 else if (flags & ANYOF_FOLD) {
4200 if (flags & ANYOF_LOCALE) {
4201 PL_reg_flags |= RF_tainted;
4202 f = PL_fold_locale[c];
4206 if (f != c && ANYOF_BITMAP_TEST(n, f))
4210 if (!match && (flags & ANYOF_CLASS)) {
4211 PL_reg_flags |= RF_tainted;
4213 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4214 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4215 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4216 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4217 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4218 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4219 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4220 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4221 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4222 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4223 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4224 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4225 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4226 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4227 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4228 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4229 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4230 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4231 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4232 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4233 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4234 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4235 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4236 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4237 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4238 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4239 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4240 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4241 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4242 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4243 ) /* How's that for a conditional? */
4250 return (flags & ANYOF_INVERT) ? !match : match;
4254 S_reghop(pTHX_ U8 *s, I32 off)
4256 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4260 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4263 while (off-- && s < lim) {
4264 /* XXX could check well-formedness here */
4272 if (UTF8_IS_CONTINUED(*s)) {
4273 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4276 /* XXX could check well-formedness here */
4284 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4286 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4290 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4293 while (off-- && s < lim) {
4294 /* XXX could check well-formedness here */
4304 if (UTF8_IS_CONTINUED(*s)) {
4305 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4308 /* XXX could check well-formedness here */
4320 restore_pos(pTHX_ void *arg)
4322 if (PL_reg_eval_set) {
4323 if (PL_reg_oldsaved) {
4324 PL_reg_re->subbeg = PL_reg_oldsaved;
4325 PL_reg_re->sublen = PL_reg_oldsavedlen;
4326 RX_MATCH_COPIED_on(PL_reg_re);
4328 PL_reg_magic->mg_len = PL_reg_oldpos;
4329 PL_reg_eval_set = 0;
4330 PL_curpm = PL_reg_oldcurpm;