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 \
137 #define NEAR_EXACT(rn) (PL_regkind[(U8)OP(rn)] == EXACT || JUMPABLE(rn))
139 #define NEXT_IMPT(rn) STMT_START { \
140 while (JUMPABLE(rn)) \
141 if (OP(rn) == SUSPEND || OP(rn) == IFMATCH) \
142 rn = NEXTOPER(NEXTOPER(rn)); \
143 else rn += NEXT_OFF(rn); \
146 static void restore_pos(pTHX_ void *arg);
149 S_regcppush(pTHX_ I32 parenfloor)
151 int retval = PL_savestack_ix;
152 #define REGCP_PAREN_ELEMS 4
153 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
156 if (paren_elems_to_push < 0)
157 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
159 #define REGCP_OTHER_ELEMS 6
160 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
161 for (p = PL_regsize; p > parenfloor; p--) {
162 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
163 SSPUSHINT(PL_regendp[p]);
164 SSPUSHINT(PL_regstartp[p]);
165 SSPUSHPTR(PL_reg_start_tmp[p]);
168 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
169 SSPUSHINT(PL_regsize);
170 SSPUSHINT(*PL_reglastparen);
171 SSPUSHINT(*PL_reglastcloseparen);
172 SSPUSHPTR(PL_reginput);
173 #define REGCP_FRAME_ELEMS 2
174 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
175 * are needed for the regexp context stack bookkeeping. */
176 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
177 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
182 /* These are needed since we do not localize EVAL nodes: */
183 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
184 " Setting an EVAL scope, savestack=%"IVdf"\n", \
185 (IV)PL_savestack_ix)); cp = PL_savestack_ix
187 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
188 PerlIO_printf(Perl_debug_log, \
189 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
190 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
200 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
202 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
203 i = SSPOPINT; /* Parentheses elements to pop. */
204 input = (char *) SSPOPPTR;
205 *PL_reglastcloseparen = SSPOPINT;
206 *PL_reglastparen = SSPOPINT;
207 PL_regsize = SSPOPINT;
209 /* Now restore the parentheses context. */
210 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
211 i > 0; i -= REGCP_PAREN_ELEMS) {
212 paren = (U32)SSPOPINT;
213 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
214 PL_regstartp[paren] = SSPOPINT;
216 if (paren <= *PL_reglastparen)
217 PL_regendp[paren] = tmps;
219 PerlIO_printf(Perl_debug_log,
220 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
221 (UV)paren, (IV)PL_regstartp[paren],
222 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
223 (IV)PL_regendp[paren],
224 (paren > *PL_reglastparen ? "(no)" : ""));
228 if (*PL_reglastparen + 1 <= PL_regnpar) {
229 PerlIO_printf(Perl_debug_log,
230 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
231 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
235 /* It would seem that the similar code in regtry()
236 * already takes care of this, and in fact it is in
237 * a better location to since this code can #if 0-ed out
238 * but the code in regtry() is needed or otherwise tests
239 * requiring null fields (pat.t#187 and split.t#{13,14}
240 * (as of patchlevel 7877) will fail. Then again,
241 * this code seems to be necessary or otherwise
242 * building DynaLoader will fail:
243 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
245 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
246 if (paren > PL_regsize)
247 PL_regstartp[paren] = -1;
248 PL_regendp[paren] = -1;
255 S_regcp_set_to(pTHX_ I32 ss)
257 I32 tmp = PL_savestack_ix;
259 PL_savestack_ix = ss;
261 PL_savestack_ix = tmp;
265 typedef struct re_cc_state
269 struct re_cc_state *prev;
274 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
276 #define TRYPAREN(paren, n, input) { \
279 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
280 PL_regendp[paren] = input - PL_bostr; \
283 PL_regendp[paren] = -1; \
285 if (regmatch(next)) \
288 PL_regendp[paren] = -1; \
293 * pregexec and friends
297 - pregexec - match a regexp against a string
300 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
301 char *strbeg, I32 minend, SV *screamer, U32 nosave)
302 /* strend: pointer to null at end of string */
303 /* strbeg: real beginning of string */
304 /* minend: end of match must be >=minend after stringarg. */
305 /* nosave: For optimizations. */
308 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
309 nosave ? 0 : REXEC_COPY_STR);
313 S_cache_re(pTHX_ regexp *prog)
315 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
317 PL_regprogram = prog->program;
319 PL_regnpar = prog->nparens;
320 PL_regdata = prog->data;
325 * Need to implement the following flags for reg_anch:
327 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
329 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
330 * INTUIT_AUTORITATIVE_ML
331 * INTUIT_ONCE_NOML - Intuit can match in one location only.
334 * Another flag for this function: SECOND_TIME (so that float substrs
335 * with giant delta may be not rechecked).
338 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
340 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
341 Otherwise, only SvCUR(sv) is used to get strbeg. */
343 /* XXXX We assume that strpos is strbeg unless sv. */
345 /* XXXX Some places assume that there is a fixed substring.
346 An update may be needed if optimizer marks as "INTUITable"
347 RExen without fixed substrings. Similarly, it is assumed that
348 lengths of all the strings are no more than minlen, thus they
349 cannot come from lookahead.
350 (Or minlen should take into account lookahead.) */
352 /* A failure to find a constant substring means that there is no need to make
353 an expensive call to REx engine, thus we celebrate a failure. Similarly,
354 finding a substring too deep into the string means that less calls to
355 regtry() should be needed.
357 REx compiler's optimizer found 4 possible hints:
358 a) Anchored substring;
360 c) Whether we are anchored (beginning-of-line or \G);
361 d) First node (of those at offset 0) which may distingush positions;
362 We use a)b)d) and multiline-part of c), and try to find a position in the
363 string which does not contradict any of them.
366 /* Most of decisions we do here should have been done at compile time.
367 The nodes of the REx which we used for the search should have been
368 deleted from the finite automaton. */
371 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
372 char *strend, U32 flags, re_scream_pos_data *data)
374 register I32 start_shift = 0;
375 /* Should be nonnegative! */
376 register I32 end_shift = 0;
382 register char *other_last = Nullch; /* other substr checked before this */
383 char *check_at = Nullch; /* check substr found at this pos */
385 char *i_strpos = strpos;
388 DEBUG_r( if (!PL_colorset) reginitcolors() );
389 DEBUG_r(PerlIO_printf(Perl_debug_log,
390 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
391 PL_colors[4],PL_colors[5],PL_colors[0],
394 (strlen(prog->precomp) > 60 ? "..." : ""),
396 (int)(strend - strpos > 60 ? 60 : strend - strpos),
397 strpos, PL_colors[1],
398 (strend - strpos > 60 ? "..." : ""))
401 if (prog->reganch & ROPT_UTF8)
402 PL_reg_flags |= RF_utf8;
404 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
405 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
408 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
410 check = prog->check_substr;
411 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
412 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
413 || ( (prog->reganch & ROPT_ANCH_BOL)
414 && !PL_multiline ) ); /* Check after \n? */
417 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
418 | ROPT_IMPLICIT)) /* not a real BOL */
419 /* SvCUR is not set on references: SvRV and SvPVX overlap */
421 && (strpos != strbeg)) {
422 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
425 if (prog->check_offset_min == prog->check_offset_max &&
426 !(prog->reganch & ROPT_CANY_SEEN)) {
427 /* Substring at constant offset from beg-of-str... */
430 s = HOP3c(strpos, prog->check_offset_min, strend);
432 slen = SvCUR(check); /* >= 1 */
434 if ( strend - s > slen || strend - s < slen - 1
435 || (strend - s == slen && strend[-1] != '\n')) {
436 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
439 /* Now should match s[0..slen-2] */
441 if (slen && (*SvPVX(check) != *s
443 && memNE(SvPVX(check), s, slen)))) {
445 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
449 else if (*SvPVX(check) != *s
450 || ((slen = SvCUR(check)) > 1
451 && memNE(SvPVX(check), s, slen)))
453 goto success_at_start;
456 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
458 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
459 end_shift = prog->minlen - start_shift -
460 CHR_SVLEN(check) + (SvTAIL(check) != 0);
462 I32 end = prog->check_offset_max + CHR_SVLEN(check)
463 - (SvTAIL(check) != 0);
464 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
466 if (end_shift < eshift)
470 else { /* Can match at random position */
473 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
474 /* Should be nonnegative! */
475 end_shift = prog->minlen - start_shift -
476 CHR_SVLEN(check) + (SvTAIL(check) != 0);
479 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
481 Perl_croak(aTHX_ "panic: end_shift");
485 /* Find a possible match in the region s..strend by looking for
486 the "check" substring in the region corrected by start/end_shift. */
487 if (flags & REXEC_SCREAM) {
488 I32 p = -1; /* Internal iterator of scream. */
489 I32 *pp = data ? data->scream_pos : &p;
491 if (PL_screamfirst[BmRARE(check)] >= 0
492 || ( BmRARE(check) == '\n'
493 && (BmPREVIOUS(check) == SvCUR(check) - 1)
495 s = screaminstr(sv, check,
496 start_shift + (s - strbeg), end_shift, pp, 0);
500 *data->scream_olds = s;
502 else if (prog->reganch & ROPT_CANY_SEEN)
503 s = fbm_instr((U8*)(s + start_shift),
504 (U8*)(strend - end_shift),
505 check, PL_multiline ? FBMrf_MULTILINE : 0);
507 s = fbm_instr(HOP3(s, start_shift, strend),
508 HOP3(strend, -end_shift, strbeg),
509 check, PL_multiline ? FBMrf_MULTILINE : 0);
511 /* Update the count-of-usability, remove useless subpatterns,
514 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
515 (s ? "Found" : "Did not find"),
516 ((check == prog->anchored_substr) ? "anchored" : "floating"),
518 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
520 PL_colors[1], (SvTAIL(check) ? "$" : ""),
521 (s ? " at offset " : "...\n") ) );
528 /* Finish the diagnostic message */
529 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
531 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
532 Start with the other substr.
533 XXXX no SCREAM optimization yet - and a very coarse implementation
534 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
535 *always* match. Probably should be marked during compile...
536 Probably it is right to do no SCREAM here...
539 if (prog->float_substr && prog->anchored_substr) {
540 /* Take into account the "other" substring. */
541 /* XXXX May be hopelessly wrong for UTF... */
544 if (check == prog->float_substr) {
547 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
550 t = s - prog->check_offset_max;
551 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
552 && (!(prog->reganch & ROPT_UTF8)
553 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
558 t = HOP3c(t, prog->anchored_offset, strend);
559 if (t < other_last) /* These positions already checked */
561 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
564 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
565 /* On end-of-str: see comment below. */
566 s = fbm_instr((unsigned char*)t,
567 HOP3(HOP3(last1, prog->anchored_offset, strend)
568 + SvCUR(prog->anchored_substr),
569 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
570 prog->anchored_substr,
571 PL_multiline ? FBMrf_MULTILINE : 0);
572 DEBUG_r(PerlIO_printf(Perl_debug_log,
573 "%s anchored substr `%s%.*s%s'%s",
574 (s ? "Found" : "Contradicts"),
576 (int)(SvCUR(prog->anchored_substr)
577 - (SvTAIL(prog->anchored_substr)!=0)),
578 SvPVX(prog->anchored_substr),
579 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
581 if (last1 >= last2) {
582 DEBUG_r(PerlIO_printf(Perl_debug_log,
583 ", giving up...\n"));
586 DEBUG_r(PerlIO_printf(Perl_debug_log,
587 ", trying floating at offset %ld...\n",
588 (long)(HOP3c(s1, 1, strend) - i_strpos)));
589 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
590 s = HOP3c(last, 1, strend);
594 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
595 (long)(s - i_strpos)));
596 t = HOP3c(s, -prog->anchored_offset, strbeg);
597 other_last = HOP3c(s, 1, strend);
605 else { /* Take into account the floating substring. */
609 t = HOP3c(s, -start_shift, strbeg);
611 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
612 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
613 last = HOP3c(t, prog->float_max_offset, strend);
614 s = HOP3c(t, prog->float_min_offset, strend);
617 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
618 /* fbm_instr() takes into account exact value of end-of-str
619 if the check is SvTAIL(ed). Since false positives are OK,
620 and end-of-str is not later than strend we are OK. */
621 s = fbm_instr((unsigned char*)s,
622 (unsigned char*)last + SvCUR(prog->float_substr)
623 - (SvTAIL(prog->float_substr)!=0),
624 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
625 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
626 (s ? "Found" : "Contradicts"),
628 (int)(SvCUR(prog->float_substr)
629 - (SvTAIL(prog->float_substr)!=0)),
630 SvPVX(prog->float_substr),
631 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
634 DEBUG_r(PerlIO_printf(Perl_debug_log,
635 ", giving up...\n"));
638 DEBUG_r(PerlIO_printf(Perl_debug_log,
639 ", trying anchored starting at offset %ld...\n",
640 (long)(s1 + 1 - i_strpos)));
642 s = HOP3c(t, 1, strend);
646 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
647 (long)(s - i_strpos)));
648 other_last = s; /* Fix this later. --Hugo */
657 t = s - prog->check_offset_max;
658 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
659 && (!(prog->reganch & ROPT_UTF8)
660 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
662 /* Fixed substring is found far enough so that the match
663 cannot start at strpos. */
665 if (ml_anch && t[-1] != '\n') {
666 /* Eventually fbm_*() should handle this, but often
667 anchored_offset is not 0, so this check will not be wasted. */
668 /* XXXX In the code below we prefer to look for "^" even in
669 presence of anchored substrings. And we search even
670 beyond the found float position. These pessimizations
671 are historical artefacts only. */
673 while (t < strend - prog->minlen) {
675 if (t < check_at - prog->check_offset_min) {
676 if (prog->anchored_substr) {
677 /* Since we moved from the found position,
678 we definitely contradict the found anchored
679 substr. Due to the above check we do not
680 contradict "check" substr.
681 Thus we can arrive here only if check substr
682 is float. Redo checking for "other"=="fixed".
685 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
686 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
687 goto do_other_anchored;
689 /* We don't contradict the found floating substring. */
690 /* XXXX Why not check for STCLASS? */
692 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
693 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
696 /* Position contradicts check-string */
697 /* XXXX probably better to look for check-string
698 than for "\n", so one should lower the limit for t? */
699 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
700 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
701 other_last = strpos = s = t + 1;
706 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
707 PL_colors[0],PL_colors[1]));
711 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
712 PL_colors[0],PL_colors[1]));
716 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
719 /* The found string does not prohibit matching at strpos,
720 - no optimization of calling REx engine can be performed,
721 unless it was an MBOL and we are not after MBOL,
722 or a future STCLASS check will fail this. */
724 /* Even in this situation we may use MBOL flag if strpos is offset
725 wrt the start of the string. */
726 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
727 && (strpos != strbeg) && strpos[-1] != '\n'
728 /* May be due to an implicit anchor of m{.*foo} */
729 && !(prog->reganch & ROPT_IMPLICIT))
734 DEBUG_r( if (ml_anch)
735 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
736 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
739 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
740 && prog->check_substr /* Could be deleted already */
741 && --BmUSEFUL(prog->check_substr) < 0
742 && prog->check_substr == prog->float_substr)
744 /* If flags & SOMETHING - do not do it many times on the same match */
745 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
746 SvREFCNT_dec(prog->check_substr);
747 prog->check_substr = Nullsv; /* disable */
748 prog->float_substr = Nullsv; /* clear */
749 check = Nullsv; /* abort */
751 /* XXXX This is a remnant of the old implementation. It
752 looks wasteful, since now INTUIT can use many
754 prog->reganch &= ~RE_USE_INTUIT;
761 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
762 if (prog->regstclass) {
763 /* minlen == 0 is possible if regstclass is \b or \B,
764 and the fixed substr is ''$.
765 Since minlen is already taken into account, s+1 is before strend;
766 accidentally, minlen >= 1 guaranties no false positives at s + 1
767 even for \b or \B. But (minlen? 1 : 0) below assumes that
768 regstclass does not come from lookahead... */
769 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
770 This leaves EXACTF only, which is dealt with in find_byclass(). */
771 U8* str = (U8*)STRING(prog->regstclass);
772 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
773 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
775 char *endpos = (prog->anchored_substr || ml_anch)
776 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
777 : (prog->float_substr
778 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
781 char *startpos = strbeg;
784 if (prog->reganch & ROPT_UTF8) {
785 PL_regdata = prog->data;
788 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
793 if (endpos == strend) {
794 DEBUG_r( PerlIO_printf(Perl_debug_log,
795 "Could not match STCLASS...\n") );
798 DEBUG_r( PerlIO_printf(Perl_debug_log,
799 "This position contradicts STCLASS...\n") );
800 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
802 /* Contradict one of substrings */
803 if (prog->anchored_substr) {
804 if (prog->anchored_substr == check) {
805 DEBUG_r( what = "anchored" );
807 s = HOP3c(t, 1, strend);
808 if (s + start_shift + end_shift > strend) {
809 /* XXXX Should be taken into account earlier? */
810 DEBUG_r( PerlIO_printf(Perl_debug_log,
811 "Could not match STCLASS...\n") );
816 DEBUG_r( PerlIO_printf(Perl_debug_log,
817 "Looking for %s substr starting at offset %ld...\n",
818 what, (long)(s + start_shift - i_strpos)) );
821 /* Have both, check_string is floating */
822 if (t + start_shift >= check_at) /* Contradicts floating=check */
823 goto retry_floating_check;
824 /* Recheck anchored substring, but not floating... */
828 DEBUG_r( PerlIO_printf(Perl_debug_log,
829 "Looking for anchored substr starting at offset %ld...\n",
830 (long)(other_last - i_strpos)) );
831 goto do_other_anchored;
833 /* Another way we could have checked stclass at the
834 current position only: */
839 DEBUG_r( PerlIO_printf(Perl_debug_log,
840 "Looking for /%s^%s/m starting at offset %ld...\n",
841 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
844 if (!prog->float_substr) /* Could have been deleted */
846 /* Check is floating subtring. */
847 retry_floating_check:
848 t = check_at - start_shift;
849 DEBUG_r( what = "floating" );
850 goto hop_and_restart;
853 DEBUG_r(PerlIO_printf(Perl_debug_log,
854 "By STCLASS: moving %ld --> %ld\n",
855 (long)(t - i_strpos), (long)(s - i_strpos))
859 DEBUG_r(PerlIO_printf(Perl_debug_log,
860 "Does not contradict STCLASS...\n");
865 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
866 PL_colors[4], (check ? "Guessed" : "Giving up"),
867 PL_colors[5], (long)(s - i_strpos)) );
870 fail_finish: /* Substring not found */
871 if (prog->check_substr) /* could be removed already */
872 BmUSEFUL(prog->check_substr) += 5; /* hooray */
874 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
875 PL_colors[4],PL_colors[5]));
879 /* We know what class REx starts with. Try to find this position... */
881 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
883 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
889 register I32 tmp = 1; /* Scratch variable? */
890 register bool do_utf8 = PL_reg_match_utf8;
892 /* We know what class it must start with. */
896 if (reginclass(c, (U8*)s, do_utf8)) {
897 if (tmp && (norun || regtry(prog, s)))
904 s += do_utf8 ? UTF8SKIP(s) : 1;
909 if (tmp && (norun || regtry(prog, s)))
920 c1 = to_utf8_lower((U8*)m);
921 c2 = to_utf8_upper((U8*)m);
932 c2 = PL_fold_locale[c1];
937 e = s; /* Due to minlen logic of intuit() */
943 if ( utf8_to_uvchr((U8*)s, &len) == c1
950 UV c = utf8_to_uvchr((U8*)s, &len);
951 if ( (c == c1 || c == c2) && regtry(prog, s) )
960 && (ln == 1 || !(OP(c) == EXACTF
962 : ibcmp_locale(s, m, ln)))
963 && (norun || regtry(prog, s)) )
969 if ( (*(U8*)s == c1 || *(U8*)s == c2)
970 && (ln == 1 || !(OP(c) == EXACTF
972 : ibcmp_locale(s, m, ln)))
973 && (norun || regtry(prog, s)) )
980 PL_reg_flags |= RF_tainted;
987 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
990 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
992 tmp = ((OP(c) == BOUND ?
993 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
994 LOAD_UTF8_CHARCLASS(alnum,"a");
996 if (tmp == !(OP(c) == BOUND ?
997 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
998 isALNUM_LC_utf8((U8*)s)))
1001 if ((norun || regtry(prog, s)))
1008 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1009 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1010 while (s < strend) {
1012 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1014 if ((norun || regtry(prog, s)))
1020 if ((!prog->minlen && tmp) && (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) == NBOUND ?
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) == NBOUND ?
1041 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1042 isALNUM_LC_utf8((U8*)s)))
1044 else if ((norun || regtry(prog, s)))
1050 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1051 tmp = ((OP(c) == NBOUND ?
1052 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1053 while (s < strend) {
1055 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1057 else if ((norun || regtry(prog, s)))
1062 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1067 LOAD_UTF8_CHARCLASS(alnum,"a");
1068 while (s < strend) {
1069 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1070 if (tmp && (norun || regtry(prog, s)))
1081 while (s < strend) {
1083 if (tmp && (norun || regtry(prog, s)))
1095 PL_reg_flags |= RF_tainted;
1097 while (s < strend) {
1098 if (isALNUM_LC_utf8((U8*)s)) {
1099 if (tmp && (norun || regtry(prog, s)))
1110 while (s < strend) {
1111 if (isALNUM_LC(*s)) {
1112 if (tmp && (norun || regtry(prog, s)))
1125 LOAD_UTF8_CHARCLASS(alnum,"a");
1126 while (s < strend) {
1127 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1128 if (tmp && (norun || regtry(prog, s)))
1139 while (s < strend) {
1141 if (tmp && (norun || regtry(prog, s)))
1153 PL_reg_flags |= RF_tainted;
1155 while (s < strend) {
1156 if (!isALNUM_LC_utf8((U8*)s)) {
1157 if (tmp && (norun || regtry(prog, s)))
1168 while (s < strend) {
1169 if (!isALNUM_LC(*s)) {
1170 if (tmp && (norun || regtry(prog, s)))
1183 LOAD_UTF8_CHARCLASS(space," ");
1184 while (s < strend) {
1185 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1186 if (tmp && (norun || regtry(prog, s)))
1197 while (s < strend) {
1199 if (tmp && (norun || regtry(prog, s)))
1211 PL_reg_flags |= RF_tainted;
1213 while (s < strend) {
1214 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1215 if (tmp && (norun || regtry(prog, s)))
1226 while (s < strend) {
1227 if (isSPACE_LC(*s)) {
1228 if (tmp && (norun || regtry(prog, s)))
1241 LOAD_UTF8_CHARCLASS(space," ");
1242 while (s < strend) {
1243 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1244 if (tmp && (norun || regtry(prog, s)))
1255 while (s < strend) {
1257 if (tmp && (norun || regtry(prog, s)))
1269 PL_reg_flags |= RF_tainted;
1271 while (s < strend) {
1272 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1273 if (tmp && (norun || regtry(prog, s)))
1284 while (s < strend) {
1285 if (!isSPACE_LC(*s)) {
1286 if (tmp && (norun || regtry(prog, s)))
1299 LOAD_UTF8_CHARCLASS(digit,"0");
1300 while (s < strend) {
1301 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1302 if (tmp && (norun || regtry(prog, s)))
1313 while (s < strend) {
1315 if (tmp && (norun || regtry(prog, s)))
1327 PL_reg_flags |= RF_tainted;
1329 while (s < strend) {
1330 if (isDIGIT_LC_utf8((U8*)s)) {
1331 if (tmp && (norun || regtry(prog, s)))
1342 while (s < strend) {
1343 if (isDIGIT_LC(*s)) {
1344 if (tmp && (norun || regtry(prog, s)))
1357 LOAD_UTF8_CHARCLASS(digit,"0");
1358 while (s < strend) {
1359 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1360 if (tmp && (norun || regtry(prog, s)))
1371 while (s < strend) {
1373 if (tmp && (norun || regtry(prog, s)))
1385 PL_reg_flags |= RF_tainted;
1387 while (s < strend) {
1388 if (!isDIGIT_LC_utf8((U8*)s)) {
1389 if (tmp && (norun || regtry(prog, s)))
1400 while (s < strend) {
1401 if (!isDIGIT_LC(*s)) {
1402 if (tmp && (norun || regtry(prog, s)))
1414 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1423 - regexec_flags - match a regexp against a string
1426 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1427 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1428 /* strend: pointer to null at end of string */
1429 /* strbeg: real beginning of string */
1430 /* minend: end of match must be >=minend after stringarg. */
1431 /* data: May be used for some additional optimizations. */
1432 /* nosave: For optimizations. */
1435 register regnode *c;
1436 register char *startpos = stringarg;
1437 I32 minlen; /* must match at least this many chars */
1438 I32 dontbother = 0; /* how many characters not to try at end */
1439 /* I32 start_shift = 0; */ /* Offset of the start to find
1440 constant substr. */ /* CC */
1441 I32 end_shift = 0; /* Same for the end. */ /* CC */
1442 I32 scream_pos = -1; /* Internal iterator of scream. */
1444 SV* oreplsv = GvSV(PL_replgv);
1445 bool do_utf8 = DO_UTF8(sv);
1451 PL_regnarrate = DEBUG_r_TEST;
1454 /* Be paranoid... */
1455 if (prog == NULL || startpos == NULL) {
1456 Perl_croak(aTHX_ "NULL regexp parameter");
1460 minlen = prog->minlen;
1461 if (do_utf8 && !(prog->reganch & ROPT_CANY_SEEN)) {
1462 if (utf8_distance((U8*)strend, (U8*)startpos) < minlen) goto phooey;
1465 if (strend - startpos < minlen) goto phooey;
1468 /* Check validity of program. */
1469 if (UCHARAT(prog->program) != REG_MAGIC) {
1470 Perl_croak(aTHX_ "corrupted regexp program");
1474 PL_reg_eval_set = 0;
1477 if (prog->reganch & ROPT_UTF8)
1478 PL_reg_flags |= RF_utf8;
1480 /* Mark beginning of line for ^ and lookbehind. */
1481 PL_regbol = startpos;
1485 /* Mark end of line for $ (and such) */
1488 /* see how far we have to get to not match where we matched before */
1489 PL_regtill = startpos+minend;
1491 /* We start without call_cc context. */
1494 /* If there is a "must appear" string, look for it. */
1497 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1500 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1501 PL_reg_ganch = startpos;
1502 else if (sv && SvTYPE(sv) >= SVt_PVMG
1504 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1505 && mg->mg_len >= 0) {
1506 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1507 if (prog->reganch & ROPT_ANCH_GPOS) {
1508 if (s > PL_reg_ganch)
1513 else /* pos() not defined */
1514 PL_reg_ganch = strbeg;
1517 if (do_utf8 == (UTF!=0) &&
1518 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1519 re_scream_pos_data d;
1521 d.scream_olds = &scream_olds;
1522 d.scream_pos = &scream_pos;
1523 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1525 goto phooey; /* not present */
1528 DEBUG_r( if (!PL_colorset) reginitcolors() );
1529 DEBUG_r(PerlIO_printf(Perl_debug_log,
1530 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1531 PL_colors[4],PL_colors[5],PL_colors[0],
1534 (strlen(prog->precomp) > 60 ? "..." : ""),
1536 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1537 startpos, PL_colors[1],
1538 (strend - startpos > 60 ? "..." : ""))
1541 /* Simplest case: anchored match need be tried only once. */
1542 /* [unless only anchor is BOL and multiline is set] */
1543 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1544 if (s == startpos && regtry(prog, startpos))
1546 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1547 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1552 dontbother = minlen - 1;
1553 end = HOP3c(strend, -dontbother, strbeg) - 1;
1554 /* for multiline we only have to try after newlines */
1555 if (prog->check_substr) {
1559 if (regtry(prog, s))
1564 if (prog->reganch & RE_USE_INTUIT) {
1565 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1576 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1577 if (regtry(prog, s))
1584 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1585 if (regtry(prog, PL_reg_ganch))
1590 /* Messy cases: unanchored match. */
1591 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1592 /* we have /x+whatever/ */
1593 /* it must be a one character string (XXXX Except UTF?) */
1594 char ch = SvPVX(prog->anchored_substr)[0];
1600 while (s < strend) {
1602 DEBUG_r( did_match = 1 );
1603 if (regtry(prog, s)) goto got_it;
1605 while (s < strend && *s == ch)
1612 while (s < strend) {
1614 DEBUG_r( did_match = 1 );
1615 if (regtry(prog, s)) goto got_it;
1617 while (s < strend && *s == ch)
1623 DEBUG_r(if (!did_match)
1624 PerlIO_printf(Perl_debug_log,
1625 "Did not find anchored character...\n")
1629 else if (do_utf8 == (UTF!=0) &&
1630 (prog->anchored_substr != Nullsv
1631 || (prog->float_substr != Nullsv
1632 && prog->float_max_offset < strend - s))) {
1633 SV *must = prog->anchored_substr
1634 ? prog->anchored_substr : prog->float_substr;
1636 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1638 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1639 char *last = HOP3c(strend, /* Cannot start after this */
1640 -(I32)(CHR_SVLEN(must)
1641 - (SvTAIL(must) != 0) + back_min), strbeg);
1642 char *last1; /* Last position checked before */
1648 last1 = HOPc(s, -1);
1650 last1 = s - 1; /* bogus */
1652 /* XXXX check_substr already used to find `s', can optimize if
1653 check_substr==must. */
1655 dontbother = end_shift;
1656 strend = HOPc(strend, -dontbother);
1657 while ( (s <= last) &&
1658 ((flags & REXEC_SCREAM)
1659 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1660 end_shift, &scream_pos, 0))
1661 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1662 (unsigned char*)strend, must,
1663 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1664 DEBUG_r( did_match = 1 );
1665 if (HOPc(s, -back_max) > last1) {
1666 last1 = HOPc(s, -back_min);
1667 s = HOPc(s, -back_max);
1670 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1672 last1 = HOPc(s, -back_min);
1676 while (s <= last1) {
1677 if (regtry(prog, s))
1683 while (s <= last1) {
1684 if (regtry(prog, s))
1690 DEBUG_r(if (!did_match)
1691 PerlIO_printf(Perl_debug_log,
1692 "Did not find %s substr `%s%.*s%s'%s...\n",
1693 ((must == prog->anchored_substr)
1694 ? "anchored" : "floating"),
1696 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1698 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1702 else if ((c = prog->regstclass)) {
1703 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1704 /* don't bother with what can't match */
1705 strend = HOPc(strend, -(minlen - 1));
1707 SV *prop = sv_newmortal();
1709 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), s);
1711 if (find_byclass(prog, c, s, strend, startpos, 0))
1713 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1717 if (prog->float_substr != Nullsv) { /* Trim the end. */
1720 if (flags & REXEC_SCREAM) {
1721 last = screaminstr(sv, prog->float_substr, s - strbeg,
1722 end_shift, &scream_pos, 1); /* last one */
1724 last = scream_olds; /* Only one occurrence. */
1728 char *little = SvPV(prog->float_substr, len);
1730 if (SvTAIL(prog->float_substr)) {
1731 if (memEQ(strend - len + 1, little, len - 1))
1732 last = strend - len + 1;
1733 else if (!PL_multiline)
1734 last = memEQ(strend - len, little, len)
1735 ? strend - len : Nullch;
1741 last = rninstr(s, strend, little, little + len);
1743 last = strend; /* matching `$' */
1747 DEBUG_r(PerlIO_printf(Perl_debug_log,
1748 "%sCan't trim the tail, match fails (should not happen)%s\n",
1749 PL_colors[4],PL_colors[5]));
1750 goto phooey; /* Should not happen! */
1752 dontbother = strend - last + prog->float_min_offset;
1754 if (minlen && (dontbother < minlen))
1755 dontbother = minlen - 1;
1756 strend -= dontbother; /* this one's always in bytes! */
1757 /* We don't know much -- general case. */
1760 if (regtry(prog, s))
1769 if (regtry(prog, s))
1771 } while (s++ < strend);
1779 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1781 if (PL_reg_eval_set) {
1782 /* Preserve the current value of $^R */
1783 if (oreplsv != GvSV(PL_replgv))
1784 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1785 restored, the value remains
1787 restore_pos(aTHX_ 0);
1790 /* make sure $`, $&, $', and $digit will work later */
1791 if ( !(flags & REXEC_NOT_FIRST) ) {
1792 if (RX_MATCH_COPIED(prog)) {
1793 Safefree(prog->subbeg);
1794 RX_MATCH_COPIED_off(prog);
1796 if (flags & REXEC_COPY_STR) {
1797 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1799 s = savepvn(strbeg, i);
1802 RX_MATCH_COPIED_on(prog);
1805 prog->subbeg = strbeg;
1806 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1813 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1814 PL_colors[4],PL_colors[5]));
1815 if (PL_reg_eval_set)
1816 restore_pos(aTHX_ 0);
1821 - regtry - try match at specific point
1823 STATIC I32 /* 0 failure, 1 success */
1824 S_regtry(pTHX_ regexp *prog, char *startpos)
1832 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1834 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1837 PL_reg_eval_set = RS_init;
1839 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1840 (IV)(PL_stack_sp - PL_stack_base));
1842 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1843 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1844 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1846 /* Apparently this is not needed, judging by wantarray. */
1847 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1848 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1851 /* Make $_ available to executed code. */
1852 if (PL_reg_sv != DEFSV) {
1853 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1858 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1859 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1860 /* prepare for quick setting of pos */
1861 sv_magic(PL_reg_sv, (SV*)0,
1862 PERL_MAGIC_regex_global, Nullch, 0);
1863 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1867 PL_reg_oldpos = mg->mg_len;
1868 SAVEDESTRUCTOR_X(restore_pos, 0);
1870 if (!PL_reg_curpm) {
1871 Newz(22,PL_reg_curpm, 1, PMOP);
1874 SV* repointer = newSViv(0);
1875 /* so we know which PL_regex_padav element is PL_reg_curpm */
1876 SvFLAGS(repointer) |= SVf_BREAK;
1877 av_push(PL_regex_padav,repointer);
1878 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
1879 PL_regex_pad = AvARRAY(PL_regex_padav);
1883 PM_SETRE(PL_reg_curpm, prog);
1884 PL_reg_oldcurpm = PL_curpm;
1885 PL_curpm = PL_reg_curpm;
1886 if (RX_MATCH_COPIED(prog)) {
1887 /* Here is a serious problem: we cannot rewrite subbeg,
1888 since it may be needed if this match fails. Thus
1889 $` inside (?{}) could fail... */
1890 PL_reg_oldsaved = prog->subbeg;
1891 PL_reg_oldsavedlen = prog->sublen;
1892 RX_MATCH_COPIED_off(prog);
1895 PL_reg_oldsaved = Nullch;
1896 prog->subbeg = PL_bostr;
1897 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1899 prog->startp[0] = startpos - PL_bostr;
1900 PL_reginput = startpos;
1901 PL_regstartp = prog->startp;
1902 PL_regendp = prog->endp;
1903 PL_reglastparen = &prog->lastparen;
1904 PL_reglastcloseparen = &prog->lastcloseparen;
1905 prog->lastparen = 0;
1907 DEBUG_r(PL_reg_starttry = startpos);
1908 if (PL_reg_start_tmpl <= prog->nparens) {
1909 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1910 if(PL_reg_start_tmp)
1911 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1913 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1916 /* XXXX What this code is doing here?!!! There should be no need
1917 to do this again and again, PL_reglastparen should take care of
1920 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1921 * Actually, the code in regcppop() (which Ilya may be meaning by
1922 * PL_reglastparen), is not needed at all by the test suite
1923 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1924 * enough, for building DynaLoader, or otherwise this
1925 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1926 * will happen. Meanwhile, this code *is* needed for the
1927 * above-mentioned test suite tests to succeed. The common theme
1928 * on those tests seems to be returning null fields from matches.
1933 if (prog->nparens) {
1934 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1941 if (regmatch(prog->program + 1)) {
1942 prog->endp[0] = PL_reginput - PL_bostr;
1945 REGCP_UNWIND(lastcp);
1949 #define RE_UNWIND_BRANCH 1
1950 #define RE_UNWIND_BRANCHJ 2
1954 typedef struct { /* XX: makes sense to enlarge it... */
1958 } re_unwind_generic_t;
1971 } re_unwind_branch_t;
1973 typedef union re_unwind_t {
1975 re_unwind_generic_t generic;
1976 re_unwind_branch_t branch;
1979 #define sayYES goto yes
1980 #define sayNO goto no
1981 #define sayYES_FINAL goto yes_final
1982 #define sayYES_LOUD goto yes_loud
1983 #define sayNO_FINAL goto no_final
1984 #define sayNO_SILENT goto do_no
1985 #define saySAME(x) if (x) goto yes; else goto no
1987 #define REPORT_CODE_OFF 24
1990 - regmatch - main matching routine
1992 * Conceptually the strategy is simple: check to see whether the current
1993 * node matches, call self recursively to see whether the rest matches,
1994 * and then act accordingly. In practice we make some effort to avoid
1995 * recursion, in particular by going through "ordinary" nodes (that don't
1996 * need to know whether the rest of the match failed) by a loop instead of
1999 /* [lwall] I've hoisted the register declarations to the outer block in order to
2000 * maybe save a little bit of pushing and popping on the stack. It also takes
2001 * advantage of machines that use a register save mask on subroutine entry.
2003 STATIC I32 /* 0 failure, 1 success */
2004 S_regmatch(pTHX_ regnode *prog)
2006 register regnode *scan; /* Current node. */
2007 regnode *next; /* Next node. */
2008 regnode *inner; /* Next node in internal branch. */
2009 register I32 nextchr; /* renamed nextchr - nextchar colides with
2010 function of same name */
2011 register I32 n; /* no or next */
2012 register I32 ln = 0; /* len or last */
2013 register char *s = Nullch; /* operand or save */
2014 register char *locinput = PL_reginput;
2015 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2016 int minmod = 0, sw = 0, logical = 0;
2019 I32 firstcp = PL_savestack_ix;
2021 register bool do_utf8 = PL_reg_match_utf8;
2027 /* Note that nextchr is a byte even in UTF */
2028 nextchr = UCHARAT(locinput);
2030 while (scan != NULL) {
2033 SV *prop = sv_newmortal();
2034 int docolor = *PL_colors[0];
2035 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2036 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2037 /* The part of the string before starttry has one color
2038 (pref0_len chars), between starttry and current
2039 position another one (pref_len - pref0_len chars),
2040 after the current position the third one.
2041 We assume that pref0_len <= pref_len, otherwise we
2042 decrease pref0_len. */
2043 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2044 ? (5 + taill) - l : locinput - PL_bostr;
2047 while (UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2049 pref0_len = pref_len - (locinput - PL_reg_starttry);
2050 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2051 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2052 ? (5 + taill) - pref_len : PL_regeol - locinput);
2053 while (UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2057 if (pref0_len > pref_len)
2058 pref0_len = pref_len;
2059 regprop(prop, scan);
2060 PerlIO_printf(Perl_debug_log,
2061 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2062 (IV)(locinput - PL_bostr),
2063 PL_colors[4], pref0_len,
2064 locinput - pref_len, PL_colors[5],
2065 PL_colors[2], pref_len - pref0_len,
2066 locinput - pref_len + pref0_len, PL_colors[3],
2067 (docolor ? "" : "> <"),
2068 PL_colors[0], l, locinput, PL_colors[1],
2069 15 - l - pref_len + 1,
2071 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2075 next = scan + NEXT_OFF(scan);
2081 if (locinput == PL_bostr || (PL_multiline &&
2082 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2084 /* regtill = regbol; */
2089 if (locinput == PL_bostr ||
2090 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2096 if (locinput == PL_bostr)
2100 if (locinput == PL_reg_ganch)
2110 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2115 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2117 if (PL_regeol - locinput > 1)
2121 if (PL_regeol != locinput)
2125 if (!nextchr && locinput >= PL_regeol)
2128 locinput += PL_utf8skip[nextchr];
2129 if (locinput > PL_regeol)
2131 nextchr = UCHARAT(locinput);
2134 nextchr = UCHARAT(++locinput);
2137 if (!nextchr && locinput >= PL_regeol)
2139 nextchr = UCHARAT(++locinput);
2142 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2145 locinput += PL_utf8skip[nextchr];
2146 if (locinput > PL_regeol)
2148 nextchr = UCHARAT(locinput);
2151 nextchr = UCHARAT(++locinput);
2156 if (do_utf8 != (UTF!=0)) {
2164 if (*((U8*)s) != utf8_to_uvchr((U8*)l, &len))
2173 if (*((U8*)l) != utf8_to_uvchr((U8*)s, &len))
2179 nextchr = UCHARAT(locinput);
2182 /* Inline the first character, for speed. */
2183 if (UCHARAT(s) != nextchr)
2185 if (PL_regeol - locinput < ln)
2187 if (ln > 1 && memNE(s, locinput, ln))
2190 nextchr = UCHARAT(locinput);
2193 PL_reg_flags |= RF_tainted;
2203 c1 = OP(scan) == EXACTF;
2205 if (l >= PL_regeol) {
2208 if ((UTF ? utf8n_to_uvchr((U8*)s, e - s, 0, 0) : *((U8*)s)) !=
2209 (c1 ? toLOWER_utf8((U8*)l) : toLOWER_LC_utf8((U8*)l)))
2211 s += UTF ? UTF8SKIP(s) : 1;
2215 nextchr = UCHARAT(locinput);
2219 /* Inline the first character, for speed. */
2220 if (UCHARAT(s) != nextchr &&
2221 UCHARAT(s) != ((OP(scan) == EXACTF)
2222 ? PL_fold : PL_fold_locale)[nextchr])
2224 if (PL_regeol - locinput < ln)
2226 if (ln > 1 && (OP(scan) == EXACTF
2227 ? ibcmp(s, locinput, ln)
2228 : ibcmp_locale(s, locinput, ln)))
2231 nextchr = UCHARAT(locinput);
2235 if (!reginclass(scan, (U8*)locinput, do_utf8))
2237 if (locinput >= PL_regeol)
2239 locinput += PL_utf8skip[nextchr];
2240 nextchr = UCHARAT(locinput);
2244 nextchr = UCHARAT(locinput);
2245 if (!reginclass(scan, (U8*)locinput, do_utf8))
2247 if (!nextchr && locinput >= PL_regeol)
2249 nextchr = UCHARAT(++locinput);
2253 PL_reg_flags |= RF_tainted;
2259 LOAD_UTF8_CHARCLASS(alnum,"a");
2260 if (!(OP(scan) == ALNUM
2261 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2262 : isALNUM_LC_utf8((U8*)locinput)))
2266 locinput += PL_utf8skip[nextchr];
2267 nextchr = UCHARAT(locinput);
2270 if (!(OP(scan) == ALNUM
2271 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2273 nextchr = UCHARAT(++locinput);
2276 PL_reg_flags |= RF_tainted;
2279 if (!nextchr && locinput >= PL_regeol)
2282 LOAD_UTF8_CHARCLASS(alnum,"a");
2283 if (OP(scan) == NALNUM
2284 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2285 : isALNUM_LC_utf8((U8*)locinput))
2289 locinput += PL_utf8skip[nextchr];
2290 nextchr = UCHARAT(locinput);
2293 if (OP(scan) == NALNUM
2294 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2296 nextchr = UCHARAT(++locinput);
2300 PL_reg_flags |= RF_tainted;
2304 /* was last char in word? */
2306 if (locinput == PL_bostr)
2309 U8 *r = reghop((U8*)locinput, -1);
2311 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2313 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2314 ln = isALNUM_uni(ln);
2315 LOAD_UTF8_CHARCLASS(alnum,"a");
2316 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2319 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2320 n = isALNUM_LC_utf8((U8*)locinput);
2324 ln = (locinput != PL_bostr) ?
2325 UCHARAT(locinput - 1) : '\n';
2326 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2328 n = isALNUM(nextchr);
2331 ln = isALNUM_LC(ln);
2332 n = isALNUM_LC(nextchr);
2335 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2336 OP(scan) == BOUNDL))
2340 PL_reg_flags |= RF_tainted;
2346 if (UTF8_IS_CONTINUED(nextchr)) {
2347 LOAD_UTF8_CHARCLASS(space," ");
2348 if (!(OP(scan) == SPACE
2349 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2350 : isSPACE_LC_utf8((U8*)locinput)))
2354 locinput += PL_utf8skip[nextchr];
2355 nextchr = UCHARAT(locinput);
2358 if (!(OP(scan) == SPACE
2359 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2361 nextchr = UCHARAT(++locinput);
2364 if (!(OP(scan) == SPACE
2365 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2367 nextchr = UCHARAT(++locinput);
2371 PL_reg_flags |= RF_tainted;
2374 if (!nextchr && locinput >= PL_regeol)
2377 LOAD_UTF8_CHARCLASS(space," ");
2378 if (OP(scan) == NSPACE
2379 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2380 : isSPACE_LC_utf8((U8*)locinput))
2384 locinput += PL_utf8skip[nextchr];
2385 nextchr = UCHARAT(locinput);
2388 if (OP(scan) == NSPACE
2389 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2391 nextchr = UCHARAT(++locinput);
2394 PL_reg_flags |= RF_tainted;
2400 LOAD_UTF8_CHARCLASS(digit,"0");
2401 if (!(OP(scan) == DIGIT
2402 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2403 : isDIGIT_LC_utf8((U8*)locinput)))
2407 locinput += PL_utf8skip[nextchr];
2408 nextchr = UCHARAT(locinput);
2411 if (!(OP(scan) == DIGIT
2412 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2414 nextchr = UCHARAT(++locinput);
2417 PL_reg_flags |= RF_tainted;
2420 if (!nextchr && locinput >= PL_regeol)
2423 LOAD_UTF8_CHARCLASS(digit,"0");
2424 if (OP(scan) == NDIGIT
2425 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2426 : isDIGIT_LC_utf8((U8*)locinput))
2430 locinput += PL_utf8skip[nextchr];
2431 nextchr = UCHARAT(locinput);
2434 if (OP(scan) == NDIGIT
2435 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2437 nextchr = UCHARAT(++locinput);
2440 LOAD_UTF8_CHARCLASS(mark,"~");
2441 if (locinput >= PL_regeol ||
2442 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2444 locinput += PL_utf8skip[nextchr];
2445 while (locinput < PL_regeol &&
2446 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2447 locinput += UTF8SKIP(locinput);
2448 if (locinput > PL_regeol)
2450 nextchr = UCHARAT(locinput);
2453 PL_reg_flags |= RF_tainted;
2457 n = ARG(scan); /* which paren pair */
2458 ln = PL_regstartp[n];
2459 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2460 if (*PL_reglastparen < n || ln == -1)
2461 sayNO; /* Do not match unless seen CLOSEn. */
2462 if (ln == PL_regendp[n])
2466 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2468 char *e = PL_bostr + PL_regendp[n];
2470 * Note that we can't do the "other character" lookup trick as
2471 * in the 8-bit case (no pun intended) because in Unicode we
2472 * have to map both upper and title case to lower case.
2474 if (OP(scan) == REFF) {
2478 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2488 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2495 nextchr = UCHARAT(locinput);
2499 /* Inline the first character, for speed. */
2500 if (UCHARAT(s) != nextchr &&
2502 (UCHARAT(s) != ((OP(scan) == REFF
2503 ? PL_fold : PL_fold_locale)[nextchr]))))
2505 ln = PL_regendp[n] - ln;
2506 if (locinput + ln > PL_regeol)
2508 if (ln > 1 && (OP(scan) == REF
2509 ? memNE(s, locinput, ln)
2511 ? ibcmp(s, locinput, ln)
2512 : ibcmp_locale(s, locinput, ln))))
2515 nextchr = UCHARAT(locinput);
2526 OP_4tree *oop = PL_op;
2527 COP *ocurcop = PL_curcop;
2528 SV **ocurpad = PL_curpad;
2532 PL_op = (OP_4tree*)PL_regdata->data[n];
2533 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2534 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2535 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2537 CALLRUNOPS(aTHX); /* Scalar context. */
2543 PL_curpad = ocurpad;
2544 PL_curcop = ocurcop;
2546 if (logical == 2) { /* Postponed subexpression. */
2548 MAGIC *mg = Null(MAGIC*);
2550 CHECKPOINT cp, lastcp;
2552 if(SvROK(ret) || SvRMAGICAL(ret)) {
2553 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2556 mg = mg_find(sv, PERL_MAGIC_qr);
2559 re = (regexp *)mg->mg_obj;
2560 (void)ReREFCNT_inc(re);
2564 char *t = SvPV(ret, len);
2566 char *oprecomp = PL_regprecomp;
2567 I32 osize = PL_regsize;
2568 I32 onpar = PL_regnpar;
2571 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2573 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2574 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2576 PL_regprecomp = oprecomp;
2581 PerlIO_printf(Perl_debug_log,
2582 "Entering embedded `%s%.60s%s%s'\n",
2586 (strlen(re->precomp) > 60 ? "..." : ""))
2589 state.prev = PL_reg_call_cc;
2590 state.cc = PL_regcc;
2591 state.re = PL_reg_re;
2595 cp = regcppush(0); /* Save *all* the positions. */
2598 state.ss = PL_savestack_ix;
2599 *PL_reglastparen = 0;
2600 *PL_reglastcloseparen = 0;
2601 PL_reg_call_cc = &state;
2602 PL_reginput = locinput;
2604 /* XXXX This is too dramatic a measure... */
2607 if (regmatch(re->program + 1)) {
2608 /* Even though we succeeded, we need to restore
2609 global variables, since we may be wrapped inside
2610 SUSPEND, thus the match may be not finished yet. */
2612 /* XXXX Do this only if SUSPENDed? */
2613 PL_reg_call_cc = state.prev;
2614 PL_regcc = state.cc;
2615 PL_reg_re = state.re;
2616 cache_re(PL_reg_re);
2618 /* XXXX This is too dramatic a measure... */
2621 /* These are needed even if not SUSPEND. */
2627 REGCP_UNWIND(lastcp);
2629 PL_reg_call_cc = state.prev;
2630 PL_regcc = state.cc;
2631 PL_reg_re = state.re;
2632 cache_re(PL_reg_re);
2634 /* XXXX This is too dramatic a measure... */
2644 sv_setsv(save_scalar(PL_replgv), ret);
2648 n = ARG(scan); /* which paren pair */
2649 PL_reg_start_tmp[n] = locinput;
2654 n = ARG(scan); /* which paren pair */
2655 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2656 PL_regendp[n] = locinput - PL_bostr;
2657 if (n > *PL_reglastparen)
2658 *PL_reglastparen = n;
2659 *PL_reglastcloseparen = n;
2662 n = ARG(scan); /* which paren pair */
2663 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2666 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2668 next = NEXTOPER(NEXTOPER(scan));
2670 next = scan + ARG(scan);
2671 if (OP(next) == IFTHEN) /* Fake one. */
2672 next = NEXTOPER(NEXTOPER(next));
2676 logical = scan->flags;
2678 /*******************************************************************
2679 PL_regcc contains infoblock about the innermost (...)* loop, and
2680 a pointer to the next outer infoblock.
2682 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2684 1) After matching X, regnode for CURLYX is processed;
2686 2) This regnode creates infoblock on the stack, and calls
2687 regmatch() recursively with the starting point at WHILEM node;
2689 3) Each hit of WHILEM node tries to match A and Z (in the order
2690 depending on the current iteration, min/max of {min,max} and
2691 greediness). The information about where are nodes for "A"
2692 and "Z" is read from the infoblock, as is info on how many times "A"
2693 was already matched, and greediness.
2695 4) After A matches, the same WHILEM node is hit again.
2697 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2698 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2699 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2700 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2701 of the external loop.
2703 Currently present infoblocks form a tree with a stem formed by PL_curcc
2704 and whatever it mentions via ->next, and additional attached trees
2705 corresponding to temporarily unset infoblocks as in "5" above.
2707 In the following picture infoblocks for outer loop of
2708 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2709 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2710 infoblocks are drawn below the "reset" infoblock.
2712 In fact in the picture below we do not show failed matches for Z and T
2713 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2714 more obvious *why* one needs to *temporary* unset infoblocks.]
2716 Matched REx position InfoBlocks Comment
2720 Y A)*?Z)*?T x <- O <- I
2721 YA )*?Z)*?T x <- O <- I
2722 YA A)*?Z)*?T x <- O <- I
2723 YAA )*?Z)*?T x <- O <- I
2724 YAA Z)*?T x <- O # Temporary unset I
2727 YAAZ Y(A)*?Z)*?T x <- O
2730 YAAZY (A)*?Z)*?T x <- O
2733 YAAZY A)*?Z)*?T x <- O <- I
2736 YAAZYA )*?Z)*?T x <- O <- I
2739 YAAZYA Z)*?T x <- O # Temporary unset I
2745 YAAZYAZ T x # Temporary unset O
2752 *******************************************************************/
2755 CHECKPOINT cp = PL_savestack_ix;
2756 /* No need to save/restore up to this paren */
2757 I32 parenfloor = scan->flags;
2759 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2761 cc.oldcc = PL_regcc;
2763 /* XXXX Probably it is better to teach regpush to support
2764 parenfloor > PL_regsize... */
2765 if (parenfloor > *PL_reglastparen)
2766 parenfloor = *PL_reglastparen; /* Pessimization... */
2767 cc.parenfloor = parenfloor;
2769 cc.min = ARG1(scan);
2770 cc.max = ARG2(scan);
2771 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2775 PL_reginput = locinput;
2776 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2778 PL_regcc = cc.oldcc;
2784 * This is really hard to understand, because after we match
2785 * what we're trying to match, we must make sure the rest of
2786 * the REx is going to match for sure, and to do that we have
2787 * to go back UP the parse tree by recursing ever deeper. And
2788 * if it fails, we have to reset our parent's current state
2789 * that we can try again after backing off.
2792 CHECKPOINT cp, lastcp;
2793 CURCUR* cc = PL_regcc;
2794 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2796 n = cc->cur + 1; /* how many we know we matched */
2797 PL_reginput = locinput;
2800 PerlIO_printf(Perl_debug_log,
2801 "%*s %ld out of %ld..%ld cc=%lx\n",
2802 REPORT_CODE_OFF+PL_regindent*2, "",
2803 (long)n, (long)cc->min,
2804 (long)cc->max, (long)cc)
2807 /* If degenerate scan matches "", assume scan done. */
2809 if (locinput == cc->lastloc && n >= cc->min) {
2810 PL_regcc = cc->oldcc;
2814 PerlIO_printf(Perl_debug_log,
2815 "%*s empty match detected, try continuation...\n",
2816 REPORT_CODE_OFF+PL_regindent*2, "")
2818 if (regmatch(cc->next))
2826 /* First just match a string of min scans. */
2830 cc->lastloc = locinput;
2831 if (regmatch(cc->scan))
2834 cc->lastloc = lastloc;
2839 /* Check whether we already were at this position.
2840 Postpone detection until we know the match is not
2841 *that* much linear. */
2842 if (!PL_reg_maxiter) {
2843 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2844 PL_reg_leftiter = PL_reg_maxiter;
2846 if (PL_reg_leftiter-- == 0) {
2847 I32 size = (PL_reg_maxiter + 7)/8;
2848 if (PL_reg_poscache) {
2849 if (PL_reg_poscache_size < size) {
2850 Renew(PL_reg_poscache, size, char);
2851 PL_reg_poscache_size = size;
2853 Zero(PL_reg_poscache, size, char);
2856 PL_reg_poscache_size = size;
2857 Newz(29, PL_reg_poscache, size, char);
2860 PerlIO_printf(Perl_debug_log,
2861 "%sDetected a super-linear match, switching on caching%s...\n",
2862 PL_colors[4], PL_colors[5])
2865 if (PL_reg_leftiter < 0) {
2866 I32 o = locinput - PL_bostr, b;
2868 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2871 if (PL_reg_poscache[o] & (1<<b)) {
2873 PerlIO_printf(Perl_debug_log,
2874 "%*s already tried at this position...\n",
2875 REPORT_CODE_OFF+PL_regindent*2, "")
2879 PL_reg_poscache[o] |= (1<<b);
2883 /* Prefer next over scan for minimal matching. */
2886 PL_regcc = cc->oldcc;
2889 cp = regcppush(cc->parenfloor);
2891 if (regmatch(cc->next)) {
2893 sayYES; /* All done. */
2895 REGCP_UNWIND(lastcp);
2901 if (n >= cc->max) { /* Maximum greed exceeded? */
2902 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2903 && !(PL_reg_flags & RF_warned)) {
2904 PL_reg_flags |= RF_warned;
2905 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2906 "Complex regular subexpression recursion",
2913 PerlIO_printf(Perl_debug_log,
2914 "%*s trying longer...\n",
2915 REPORT_CODE_OFF+PL_regindent*2, "")
2917 /* Try scanning more and see if it helps. */
2918 PL_reginput = locinput;
2920 cc->lastloc = locinput;
2921 cp = regcppush(cc->parenfloor);
2923 if (regmatch(cc->scan)) {
2927 REGCP_UNWIND(lastcp);
2930 cc->lastloc = lastloc;
2934 /* Prefer scan over next for maximal matching. */
2936 if (n < cc->max) { /* More greed allowed? */
2937 cp = regcppush(cc->parenfloor);
2939 cc->lastloc = locinput;
2941 if (regmatch(cc->scan)) {
2945 REGCP_UNWIND(lastcp);
2946 regcppop(); /* Restore some previous $<digit>s? */
2947 PL_reginput = locinput;
2949 PerlIO_printf(Perl_debug_log,
2950 "%*s failed, try continuation...\n",
2951 REPORT_CODE_OFF+PL_regindent*2, "")
2954 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2955 && !(PL_reg_flags & RF_warned)) {
2956 PL_reg_flags |= RF_warned;
2957 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2958 "Complex regular subexpression recursion",
2962 /* Failed deeper matches of scan, so see if this one works. */
2963 PL_regcc = cc->oldcc;
2966 if (regmatch(cc->next))
2972 cc->lastloc = lastloc;
2977 next = scan + ARG(scan);
2980 inner = NEXTOPER(NEXTOPER(scan));
2983 inner = NEXTOPER(scan);
2987 if (OP(next) != c1) /* No choice. */
2988 next = inner; /* Avoid recursion. */
2990 I32 lastparen = *PL_reglastparen;
2992 re_unwind_branch_t *uw;
2994 /* Put unwinding data on stack */
2995 unwind1 = SSNEWt(1,re_unwind_branch_t);
2996 uw = SSPTRt(unwind1,re_unwind_branch_t);
2999 uw->type = ((c1 == BRANCH)
3001 : RE_UNWIND_BRANCHJ);
3002 uw->lastparen = lastparen;
3004 uw->locinput = locinput;
3005 uw->nextchr = nextchr;
3007 uw->regindent = ++PL_regindent;
3010 REGCP_SET(uw->lastcp);
3012 /* Now go into the first branch */
3025 /* We suppose that the next guy does not need
3026 backtracking: in particular, it is of constant length,
3027 and has no parenths to influence future backrefs. */
3028 ln = ARG1(scan); /* min to match */
3029 n = ARG2(scan); /* max to match */
3030 paren = scan->flags;
3032 if (paren > PL_regsize)
3034 if (paren > *PL_reglastparen)
3035 *PL_reglastparen = paren;
3037 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3039 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3040 PL_reginput = locinput;
3043 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3045 /* if we matched something zero-length we don't need to
3046 backtrack - capturing parens are already defined, so
3047 the caveat in the maximal case doesn't apply
3049 XXXX if ln == 0, we can redo this check first time
3050 through the following loop
3053 n = ln; /* don't backtrack */
3054 locinput = PL_reginput;
3055 if (NEAR_EXACT(next)) {
3056 regnode *text_node = next;
3058 if (PL_regkind[(U8)OP(next)] != EXACT)
3059 NEXT_IMPT(text_node);
3061 if (PL_regkind[(U8)OP(text_node)] != EXACT) {
3065 c1 = (U8)*STRING(text_node);
3066 if (OP(next) == EXACTF)
3068 else if (OP(text_node) == EXACTFL)
3069 c2 = PL_fold_locale[c1];
3077 /* This may be improved if l == 0. */
3078 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3079 /* If it could work, try it. */
3081 UCHARAT(PL_reginput) == c1 ||
3082 UCHARAT(PL_reginput) == c2)
3086 PL_regstartp[paren] =
3087 HOPc(PL_reginput, -l) - PL_bostr;
3088 PL_regendp[paren] = PL_reginput - PL_bostr;
3091 PL_regendp[paren] = -1;
3095 REGCP_UNWIND(lastcp);
3097 /* Couldn't or didn't -- move forward. */
3098 PL_reginput = locinput;
3099 if (regrepeat_hard(scan, 1, &l)) {
3101 locinput = PL_reginput;
3108 n = regrepeat_hard(scan, n, &l);
3109 /* if we matched something zero-length we don't need to
3110 backtrack, unless the minimum count is zero and we
3111 are capturing the result - in that case the capture
3112 being defined or not may affect later execution
3114 if (n != 0 && l == 0 && !(paren && ln == 0))
3115 ln = n; /* don't backtrack */
3116 locinput = PL_reginput;
3118 PerlIO_printf(Perl_debug_log,
3119 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3120 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3124 if (NEAR_EXACT(next)) {
3125 regnode *text_node = next;
3127 if (PL_regkind[(U8)OP(next)] != EXACT)
3128 NEXT_IMPT(text_node);
3130 if (PL_regkind[(U8)OP(text_node)] != EXACT) {
3134 c1 = (U8)*STRING(text_node);
3135 if (OP(text_node) == EXACTF)
3137 else if (OP(text_node) == EXACTFL)
3138 c2 = PL_fold_locale[c1];
3148 /* If it could work, try it. */
3150 UCHARAT(PL_reginput) == c1 ||
3151 UCHARAT(PL_reginput) == c2)
3154 PerlIO_printf(Perl_debug_log,
3155 "%*s trying tail with n=%"IVdf"...\n",
3156 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3160 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3161 PL_regendp[paren] = PL_reginput - PL_bostr;
3164 PL_regendp[paren] = -1;
3168 REGCP_UNWIND(lastcp);
3170 /* Couldn't or didn't -- back up. */
3172 locinput = HOPc(locinput, -l);
3173 PL_reginput = locinput;
3180 paren = scan->flags; /* Which paren to set */
3181 if (paren > PL_regsize)
3183 if (paren > *PL_reglastparen)
3184 *PL_reglastparen = paren;
3185 ln = ARG1(scan); /* min to match */
3186 n = ARG2(scan); /* max to match */
3187 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3191 ln = ARG1(scan); /* min to match */
3192 n = ARG2(scan); /* max to match */
3193 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3198 scan = NEXTOPER(scan);
3204 scan = NEXTOPER(scan);
3208 * Lookahead to avoid useless match attempts
3209 * when we know what character comes next.
3213 * Used to only do .*x and .*?x, but now it allows
3214 * for )'s, ('s and (?{ ... })'s to be in the way
3215 * of the quantifier and the EXACT-like node. -- japhy
3218 if (NEAR_EXACT(next)) {
3220 regnode *text_node = next;
3222 if (PL_regkind[(U8)OP(next)] != EXACT)
3223 NEXT_IMPT(text_node);
3225 if (PL_regkind[(U8)OP(text_node)] != EXACT) {
3229 s = (U8*)STRING(text_node);
3233 if (OP(text_node) == EXACTF)
3235 else if (OP(text_node) == EXACTFL)
3236 c2 = PL_fold_locale[c1];
3239 if (OP(text_node) == EXACTF) {
3240 c1 = to_utf8_lower(s);
3241 c2 = to_utf8_upper(s);
3244 c2 = c1 = utf8_to_uvchr(s, NULL);
3251 PL_reginput = locinput;
3255 if (ln && regrepeat(scan, ln) < ln)
3257 locinput = PL_reginput;
3260 char *e; /* Should not check after this */
3261 char *old = locinput;
3263 if (n == REG_INFTY) {
3266 while (UTF8_IS_CONTINUATION(*(U8*)e))
3272 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3276 e = locinput + n - ln;
3282 /* Find place 'next' could work */
3285 while (locinput <= e &&
3286 UCHARAT(locinput) != c1)
3289 while (locinput <= e
3290 && UCHARAT(locinput) != c1
3291 && UCHARAT(locinput) != c2)
3294 count = locinput - old;
3301 utf8_to_uvchr((U8*)locinput, &len) != c1;
3306 for (count = 0; locinput <= e; count++) {
3307 UV c = utf8_to_uvchr((U8*)locinput, &len);
3308 if (c == c1 || c == c2)
3316 /* PL_reginput == old now */
3317 if (locinput != old) {
3318 ln = 1; /* Did some */
3319 if (regrepeat(scan, count) < count)
3322 /* PL_reginput == locinput now */
3323 TRYPAREN(paren, ln, locinput);
3324 PL_reginput = locinput; /* Could be reset... */
3325 REGCP_UNWIND(lastcp);
3326 /* Couldn't or didn't -- move forward. */
3329 locinput += UTF8SKIP(locinput);
3335 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3339 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3341 c = UCHARAT(PL_reginput);
3342 /* If it could work, try it. */
3343 if (c == c1 || c == c2)
3345 TRYPAREN(paren, n, PL_reginput);
3346 REGCP_UNWIND(lastcp);
3349 /* If it could work, try it. */
3350 else if (c1 == -1000)
3352 TRYPAREN(paren, n, PL_reginput);
3353 REGCP_UNWIND(lastcp);
3355 /* Couldn't or didn't -- move forward. */
3356 PL_reginput = locinput;
3357 if (regrepeat(scan, 1)) {
3359 locinput = PL_reginput;
3367 n = regrepeat(scan, n);
3368 locinput = PL_reginput;
3369 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3370 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3371 ln = n; /* why back off? */
3372 /* ...because $ and \Z can match before *and* after
3373 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3374 We should back off by one in this case. */
3375 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3384 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3386 c = UCHARAT(PL_reginput);
3388 /* If it could work, try it. */
3389 if (c1 == -1000 || c == c1 || c == c2)
3391 TRYPAREN(paren, n, PL_reginput);
3392 REGCP_UNWIND(lastcp);
3394 /* Couldn't or didn't -- back up. */
3396 PL_reginput = locinput = HOPc(locinput, -1);
3404 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3406 c = UCHARAT(PL_reginput);
3408 /* If it could work, try it. */
3409 if (c1 == -1000 || c == c1 || c == c2)
3411 TRYPAREN(paren, n, PL_reginput);
3412 REGCP_UNWIND(lastcp);
3414 /* Couldn't or didn't -- back up. */
3416 PL_reginput = locinput = HOPc(locinput, -1);
3423 if (PL_reg_call_cc) {
3424 re_cc_state *cur_call_cc = PL_reg_call_cc;
3425 CURCUR *cctmp = PL_regcc;
3426 regexp *re = PL_reg_re;
3427 CHECKPOINT cp, lastcp;
3429 cp = regcppush(0); /* Save *all* the positions. */
3431 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3433 PL_reginput = locinput; /* Make position available to
3435 cache_re(PL_reg_call_cc->re);
3436 PL_regcc = PL_reg_call_cc->cc;
3437 PL_reg_call_cc = PL_reg_call_cc->prev;
3438 if (regmatch(cur_call_cc->node)) {
3439 PL_reg_call_cc = cur_call_cc;
3443 REGCP_UNWIND(lastcp);
3445 PL_reg_call_cc = cur_call_cc;
3451 PerlIO_printf(Perl_debug_log,
3452 "%*s continuation failed...\n",
3453 REPORT_CODE_OFF+PL_regindent*2, "")
3457 if (locinput < PL_regtill) {
3458 DEBUG_r(PerlIO_printf(Perl_debug_log,
3459 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3461 (long)(locinput - PL_reg_starttry),
3462 (long)(PL_regtill - PL_reg_starttry),
3464 sayNO_FINAL; /* Cannot match: too short. */
3466 PL_reginput = locinput; /* put where regtry can find it */
3467 sayYES_FINAL; /* Success! */
3469 PL_reginput = locinput; /* put where regtry can find it */
3470 sayYES_LOUD; /* Success! */
3473 PL_reginput = locinput;
3478 s = HOPBACKc(locinput, scan->flags);
3484 PL_reginput = locinput;
3489 s = HOPBACKc(locinput, scan->flags);
3495 PL_reginput = locinput;
3498 inner = NEXTOPER(NEXTOPER(scan));
3499 if (regmatch(inner) != n) {
3514 if (OP(scan) == SUSPEND) {
3515 locinput = PL_reginput;
3516 nextchr = UCHARAT(locinput);
3521 next = scan + ARG(scan);
3526 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3527 PTR2UV(scan), OP(scan));
3528 Perl_croak(aTHX_ "regexp memory corruption");
3535 * We get here only if there's trouble -- normally "case END" is
3536 * the terminating point.
3538 Perl_croak(aTHX_ "corrupted regexp pointers");
3544 PerlIO_printf(Perl_debug_log,
3545 "%*s %scould match...%s\n",
3546 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3550 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3551 PL_colors[4],PL_colors[5]));
3557 #if 0 /* Breaks $^R */
3565 PerlIO_printf(Perl_debug_log,
3566 "%*s %sfailed...%s\n",
3567 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3573 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3576 case RE_UNWIND_BRANCH:
3577 case RE_UNWIND_BRANCHJ:
3579 re_unwind_branch_t *uwb = &(uw->branch);
3580 I32 lastparen = uwb->lastparen;
3582 REGCP_UNWIND(uwb->lastcp);
3583 for (n = *PL_reglastparen; n > lastparen; n--)
3585 *PL_reglastparen = n;
3586 scan = next = uwb->next;
3588 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3589 ? BRANCH : BRANCHJ) ) { /* Failure */
3596 /* Have more choice yet. Reuse the same uwb. */
3598 if ((n = (uwb->type == RE_UNWIND_BRANCH
3599 ? NEXT_OFF(next) : ARG(next))))
3602 next = NULL; /* XXXX Needn't unwinding in this case... */
3604 next = NEXTOPER(scan);
3605 if (uwb->type == RE_UNWIND_BRANCHJ)
3606 next = NEXTOPER(next);
3607 locinput = uwb->locinput;
3608 nextchr = uwb->nextchr;
3610 PL_regindent = uwb->regindent;
3617 Perl_croak(aTHX_ "regexp unwind memory corruption");
3628 - regrepeat - repeatedly match something simple, report how many
3631 * [This routine now assumes that it will only match on things of length 1.
3632 * That was true before, but now we assume scan - reginput is the count,
3633 * rather than incrementing count on every character. [Er, except utf8.]]
3636 S_regrepeat(pTHX_ regnode *p, I32 max)
3638 register char *scan;
3640 register char *loceol = PL_regeol;
3641 register I32 hardcount = 0;
3642 register bool do_utf8 = PL_reg_match_utf8;
3645 if (max != REG_INFTY && max < loceol - scan)
3646 loceol = scan + max;
3651 while (scan < loceol && hardcount < max && *scan != '\n') {
3652 scan += UTF8SKIP(scan);
3656 while (scan < loceol && *scan != '\n')
3666 case EXACT: /* length of string is 1 */
3668 while (scan < loceol && UCHARAT(scan) == c)
3671 case EXACTF: /* length of string is 1 */
3673 while (scan < loceol &&
3674 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3677 case EXACTFL: /* length of string is 1 */
3678 PL_reg_flags |= RF_tainted;
3680 while (scan < loceol &&
3681 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3687 while (hardcount < max && scan < loceol &&
3688 reginclass(p, (U8*)scan, do_utf8)) {
3689 scan += UTF8SKIP(scan);
3693 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3700 LOAD_UTF8_CHARCLASS(alnum,"a");
3701 while (hardcount < max && scan < loceol &&
3702 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3703 scan += UTF8SKIP(scan);
3707 while (scan < loceol && isALNUM(*scan))
3712 PL_reg_flags |= RF_tainted;
3715 while (hardcount < max && scan < loceol &&
3716 isALNUM_LC_utf8((U8*)scan)) {
3717 scan += UTF8SKIP(scan);
3721 while (scan < loceol && isALNUM_LC(*scan))
3728 LOAD_UTF8_CHARCLASS(alnum,"a");
3729 while (hardcount < max && scan < loceol &&
3730 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3731 scan += UTF8SKIP(scan);
3735 while (scan < loceol && !isALNUM(*scan))
3740 PL_reg_flags |= RF_tainted;
3743 while (hardcount < max && scan < loceol &&
3744 !isALNUM_LC_utf8((U8*)scan)) {
3745 scan += UTF8SKIP(scan);
3749 while (scan < loceol && !isALNUM_LC(*scan))
3756 LOAD_UTF8_CHARCLASS(space," ");
3757 while (hardcount < max && scan < loceol &&
3759 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3760 scan += UTF8SKIP(scan);
3764 while (scan < loceol && isSPACE(*scan))
3769 PL_reg_flags |= RF_tainted;
3772 while (hardcount < max && scan < loceol &&
3773 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3774 scan += UTF8SKIP(scan);
3778 while (scan < loceol && isSPACE_LC(*scan))
3785 LOAD_UTF8_CHARCLASS(space," ");
3786 while (hardcount < max && scan < loceol &&
3788 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3789 scan += UTF8SKIP(scan);
3793 while (scan < loceol && !isSPACE(*scan))
3798 PL_reg_flags |= RF_tainted;
3801 while (hardcount < max && scan < loceol &&
3802 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3803 scan += UTF8SKIP(scan);
3807 while (scan < loceol && !isSPACE_LC(*scan))
3814 LOAD_UTF8_CHARCLASS(digit,"0");
3815 while (hardcount < max && scan < loceol &&
3816 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3817 scan += UTF8SKIP(scan);
3821 while (scan < loceol && isDIGIT(*scan))
3828 LOAD_UTF8_CHARCLASS(digit,"0");
3829 while (hardcount < max && scan < loceol &&
3830 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3831 scan += UTF8SKIP(scan);
3835 while (scan < loceol && !isDIGIT(*scan))
3839 default: /* Called on something of 0 width. */
3840 break; /* So match right here or not at all. */
3846 c = scan - PL_reginput;
3851 SV *prop = sv_newmortal();
3854 PerlIO_printf(Perl_debug_log,
3855 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3856 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3863 - regrepeat_hard - repeatedly match something, report total lenth and length
3865 * The repeater is supposed to have constant length.
3869 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3871 register char *scan = Nullch;
3872 register char *start;
3873 register char *loceol = PL_regeol;
3875 I32 count = 0, res = 1;
3880 start = PL_reginput;
3881 if (PL_reg_match_utf8) {
3882 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3885 while (start < PL_reginput) {
3887 start += UTF8SKIP(start);
3898 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3900 *lp = l = PL_reginput - start;
3901 if (max != REG_INFTY && l*max < loceol - scan)
3902 loceol = scan + l*max;
3915 - regclass_swash - prepare the utf8 swash
3919 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
3924 if (PL_regdata && PL_regdata->count) {
3927 if (PL_regdata->what[n] == 's') {
3928 SV *rv = (SV*)PL_regdata->data[n];
3929 AV *av = (AV*)SvRV((SV*)rv);
3932 si = *av_fetch(av, 0, FALSE);
3933 a = av_fetch(av, 1, FALSE);
3937 else if (si && doinit) {
3938 sw = swash_init("utf8", "", si, 1, 0);
3939 (void)av_store(av, 1, sw);
3951 - reginclass - determine if a character falls into a character class
3955 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
3957 char flags = ANYOF_FLAGS(n);
3962 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
3964 if (do_utf8 || (flags & ANYOF_UNICODE)) {
3965 if (do_utf8 && !ANYOF_RUNTIME(n)) {
3966 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
3969 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
3972 SV *sw = regclass_swash(n, TRUE, 0);
3975 if (swash_fetch(sw, p, do_utf8))
3977 else if (flags & ANYOF_FOLD) {
3978 U8 tmpbuf[UTF8_MAXLEN+1];
3980 if (flags & ANYOF_LOCALE) {
3981 PL_reg_flags |= RF_tainted;
3982 uvchr_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3985 uvchr_to_utf8(tmpbuf, toLOWER_utf8(p));
3986 if (swash_fetch(sw, tmpbuf, do_utf8))
3992 if (!match && c < 256) {
3993 if (ANYOF_BITMAP_TEST(n, c))
3995 else if (flags & ANYOF_FOLD) {
3998 if (flags & ANYOF_LOCALE) {
3999 PL_reg_flags |= RF_tainted;
4000 f = PL_fold_locale[c];
4004 if (f != c && ANYOF_BITMAP_TEST(n, f))
4008 if (!match && (flags & ANYOF_CLASS)) {
4009 PL_reg_flags |= RF_tainted;
4011 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4012 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4013 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4014 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4015 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4016 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4017 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4018 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4019 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4020 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4021 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4022 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4023 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4024 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4025 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4026 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4027 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4028 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4029 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4030 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4031 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4032 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4033 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4034 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4035 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4036 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4037 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4038 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4039 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4040 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4041 ) /* How's that for a conditional? */
4048 return (flags & ANYOF_INVERT) ? !match : match;
4052 S_reghop(pTHX_ U8 *s, I32 off)
4054 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4058 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4061 while (off-- && s < lim) {
4062 /* XXX could check well-formedness here */
4070 if (UTF8_IS_CONTINUED(*s)) {
4071 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4074 /* XXX could check well-formedness here */
4082 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4084 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4088 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4091 while (off-- && s < lim) {
4092 /* XXX could check well-formedness here */
4102 if (UTF8_IS_CONTINUED(*s)) {
4103 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4106 /* XXX could check well-formedness here */
4118 restore_pos(pTHX_ void *arg)
4120 if (PL_reg_eval_set) {
4121 if (PL_reg_oldsaved) {
4122 PL_reg_re->subbeg = PL_reg_oldsaved;
4123 PL_reg_re->sublen = PL_reg_oldsavedlen;
4124 RX_MATCH_COPIED_on(PL_reg_re);
4126 PL_reg_magic->mg_len = PL_reg_oldpos;
4127 PL_reg_eval_set = 0;
4128 PL_curpm = PL_reg_oldcurpm;