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
83 #ifdef PERL_IN_XSUB_RE
84 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
91 #define RF_tainted 1 /* tainted information used? */
92 #define RF_warned 2 /* warned about big count? */
93 #define RF_evaled 4 /* Did an EVAL with setting? */
94 #define RF_utf8 8 /* String contains multibyte chars? */
96 #define UTF (PL_reg_flags & RF_utf8)
98 #define RS_init 1 /* eval environment created */
99 #define RS_set 2 /* replsv value is set */
102 #define STATIC static
109 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
110 #define CHR_DIST(a,b) (DO_UTF8(PL_reg_sv) ? utf8_distance(a,b) : a - b)
112 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
113 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
114 #define HOP(pos,off) (DO_UTF8(PL_reg_sv) ? reghop((U8*)pos, off) : (U8*)(pos + off))
115 #define HOPMAYBE(pos,off) (DO_UTF8(PL_reg_sv) ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
116 #define HOPc(pos,off) ((char*)HOP(pos,off))
117 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
119 #define HOPBACK(pos, off) ( \
120 (UTF && DO_UTF8(PL_reg_sv)) \
121 ? reghopmaybe((U8*)pos, -off) \
122 : (pos - off >= PL_bostr) \
126 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
128 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
129 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
130 #define HOP3(pos,off,lim) (DO_UTF8(PL_reg_sv) ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
131 #define HOPMAYBE3(pos,off,lim) (DO_UTF8(PL_reg_sv) ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
132 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
133 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
135 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
137 static void restore_pos(pTHXo_ void *arg);
140 S_regcppush(pTHX_ I32 parenfloor)
142 int retval = PL_savestack_ix;
143 #define REGCP_PAREN_ELEMS 4
144 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
147 if (paren_elems_to_push < 0)
148 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
150 #define REGCP_OTHER_ELEMS 6
151 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
152 for (p = PL_regsize; p > parenfloor; p--) {
153 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
154 SSPUSHINT(PL_regendp[p]);
155 SSPUSHINT(PL_regstartp[p]);
156 SSPUSHPTR(PL_reg_start_tmp[p]);
159 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
160 SSPUSHINT(PL_regsize);
161 SSPUSHINT(*PL_reglastparen);
162 SSPUSHINT(*PL_reglastcloseparen);
163 SSPUSHPTR(PL_reginput);
164 #define REGCP_FRAME_ELEMS 2
165 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
166 * are needed for the regexp context stack bookkeeping. */
167 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
168 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
173 /* These are needed since we do not localize EVAL nodes: */
174 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
175 " Setting an EVAL scope, savestack=%"IVdf"\n", \
176 (IV)PL_savestack_ix)); cp = PL_savestack_ix
178 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
179 PerlIO_printf(Perl_debug_log, \
180 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
181 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
191 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
193 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
194 i = SSPOPINT; /* Parentheses elements to pop. */
195 input = (char *) SSPOPPTR;
196 *PL_reglastcloseparen = SSPOPINT;
197 *PL_reglastparen = SSPOPINT;
198 PL_regsize = SSPOPINT;
200 /* Now restore the parentheses context. */
201 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
202 i > 0; i -= REGCP_PAREN_ELEMS) {
203 paren = (U32)SSPOPINT;
204 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
205 PL_regstartp[paren] = SSPOPINT;
207 if (paren <= *PL_reglastparen)
208 PL_regendp[paren] = tmps;
210 PerlIO_printf(Perl_debug_log,
211 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
212 (UV)paren, (IV)PL_regstartp[paren],
213 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
214 (IV)PL_regendp[paren],
215 (paren > *PL_reglastparen ? "(no)" : ""));
219 if (*PL_reglastparen + 1 <= PL_regnpar) {
220 PerlIO_printf(Perl_debug_log,
221 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
222 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
226 /* It would seem that the similar code in regtry()
227 * already takes care of this, and in fact it is in
228 * a better location to since this code can #if 0-ed out
229 * but the code in regtry() is needed or otherwise tests
230 * requiring null fields (pat.t#187 and split.t#{13,14}
231 * (as of patchlevel 7877) will fail. Then again,
232 * this code seems to be necessary or otherwise
233 * building DynaLoader will fail:
234 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
236 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
237 if (paren > PL_regsize)
238 PL_regstartp[paren] = -1;
239 PL_regendp[paren] = -1;
246 S_regcp_set_to(pTHX_ I32 ss)
248 I32 tmp = PL_savestack_ix;
250 PL_savestack_ix = ss;
252 PL_savestack_ix = tmp;
256 typedef struct re_cc_state
260 struct re_cc_state *prev;
265 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
267 #define TRYPAREN(paren, n, input) { \
270 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
271 PL_regendp[paren] = input - PL_bostr; \
274 PL_regendp[paren] = -1; \
276 if (regmatch(next)) \
279 PL_regendp[paren] = -1; \
284 * pregexec and friends
288 - pregexec - match a regexp against a string
291 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
292 char *strbeg, I32 minend, SV *screamer, U32 nosave)
293 /* strend: pointer to null at end of string */
294 /* strbeg: real beginning of string */
295 /* minend: end of match must be >=minend after stringarg. */
296 /* nosave: For optimizations. */
299 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
300 nosave ? 0 : REXEC_COPY_STR);
304 S_cache_re(pTHX_ regexp *prog)
306 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
308 PL_regprogram = prog->program;
310 PL_regnpar = prog->nparens;
311 PL_regdata = prog->data;
316 * Need to implement the following flags for reg_anch:
318 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
320 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
321 * INTUIT_AUTORITATIVE_ML
322 * INTUIT_ONCE_NOML - Intuit can match in one location only.
325 * Another flag for this function: SECOND_TIME (so that float substrs
326 * with giant delta may be not rechecked).
329 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
331 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
332 Otherwise, only SvCUR(sv) is used to get strbeg. */
334 /* XXXX We assume that strpos is strbeg unless sv. */
336 /* XXXX Some places assume that there is a fixed substring.
337 An update may be needed if optimizer marks as "INTUITable"
338 RExen without fixed substrings. Similarly, it is assumed that
339 lengths of all the strings are no more than minlen, thus they
340 cannot come from lookahead.
341 (Or minlen should take into account lookahead.) */
343 /* A failure to find a constant substring means that there is no need to make
344 an expensive call to REx engine, thus we celebrate a failure. Similarly,
345 finding a substring too deep into the string means that less calls to
346 regtry() should be needed.
348 REx compiler's optimizer found 4 possible hints:
349 a) Anchored substring;
351 c) Whether we are anchored (beginning-of-line or \G);
352 d) First node (of those at offset 0) which may distingush positions;
353 We use a)b)d) and multiline-part of c), and try to find a position in the
354 string which does not contradict any of them.
357 /* Most of decisions we do here should have been done at compile time.
358 The nodes of the REx which we used for the search should have been
359 deleted from the finite automaton. */
362 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
363 char *strend, U32 flags, re_scream_pos_data *data)
365 register I32 start_shift = 0;
366 /* Should be nonnegative! */
367 register I32 end_shift = 0;
373 register char *other_last = Nullch; /* other substr checked before this */
374 char *check_at = Nullch; /* check substr found at this pos */
376 char *i_strpos = strpos;
379 DEBUG_r( if (!PL_colorset) reginitcolors() );
380 DEBUG_r(PerlIO_printf(Perl_debug_log,
381 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
382 PL_colors[4],PL_colors[5],PL_colors[0],
385 (strlen(prog->precomp) > 60 ? "..." : ""),
387 (int)(strend - strpos > 60 ? 60 : strend - strpos),
388 strpos, PL_colors[1],
389 (strend - strpos > 60 ? "..." : ""))
392 if (prog->reganch & ROPT_UTF8)
393 PL_reg_flags |= RF_utf8;
395 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
396 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
399 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
401 check = prog->check_substr;
402 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
403 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
404 || ( (prog->reganch & ROPT_ANCH_BOL)
405 && !PL_multiline ) ); /* Check after \n? */
408 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
409 | ROPT_IMPLICIT)) /* not a real BOL */
410 /* SvCUR is not set on references: SvRV and SvPVX overlap */
412 && (strpos != strbeg)) {
413 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
416 if (prog->check_offset_min == prog->check_offset_max &&
417 !(prog->reganch & ROPT_CANY_SEEN)) {
418 /* Substring at constant offset from beg-of-str... */
421 s = HOP3c(strpos, prog->check_offset_min, strend);
423 slen = SvCUR(check); /* >= 1 */
425 if ( strend - s > slen || strend - s < slen - 1
426 || (strend - s == slen && strend[-1] != '\n')) {
427 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
430 /* Now should match s[0..slen-2] */
432 if (slen && (*SvPVX(check) != *s
434 && memNE(SvPVX(check), s, slen)))) {
436 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
440 else if (*SvPVX(check) != *s
441 || ((slen = SvCUR(check)) > 1
442 && memNE(SvPVX(check), s, slen)))
444 goto success_at_start;
447 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
449 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
450 end_shift = prog->minlen - start_shift -
451 CHR_SVLEN(check) + (SvTAIL(check) != 0);
453 I32 end = prog->check_offset_max + CHR_SVLEN(check)
454 - (SvTAIL(check) != 0);
455 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
457 if (end_shift < eshift)
461 else { /* Can match at random position */
464 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
465 /* Should be nonnegative! */
466 end_shift = prog->minlen - start_shift -
467 CHR_SVLEN(check) + (SvTAIL(check) != 0);
470 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
472 Perl_croak(aTHX_ "panic: end_shift");
476 /* Find a possible match in the region s..strend by looking for
477 the "check" substring in the region corrected by start/end_shift. */
478 if (flags & REXEC_SCREAM) {
479 I32 p = -1; /* Internal iterator of scream. */
480 I32 *pp = data ? data->scream_pos : &p;
482 if (PL_screamfirst[BmRARE(check)] >= 0
483 || ( BmRARE(check) == '\n'
484 && (BmPREVIOUS(check) == SvCUR(check) - 1)
486 s = screaminstr(sv, check,
487 start_shift + (s - strbeg), end_shift, pp, 0);
491 *data->scream_olds = s;
493 else if (prog->reganch & ROPT_CANY_SEEN)
494 s = fbm_instr((U8*)(s + start_shift),
495 (U8*)(strend - end_shift),
496 check, PL_multiline ? FBMrf_MULTILINE : 0);
498 s = fbm_instr(HOP3(s, start_shift, strend),
499 HOP3(strend, -end_shift, strbeg),
500 check, PL_multiline ? FBMrf_MULTILINE : 0);
502 /* Update the count-of-usability, remove useless subpatterns,
505 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
506 (s ? "Found" : "Did not find"),
507 ((check == prog->anchored_substr) ? "anchored" : "floating"),
509 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
511 PL_colors[1], (SvTAIL(check) ? "$" : ""),
512 (s ? " at offset " : "...\n") ) );
519 /* Finish the diagnostic message */
520 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
522 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
523 Start with the other substr.
524 XXXX no SCREAM optimization yet - and a very coarse implementation
525 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
526 *always* match. Probably should be marked during compile...
527 Probably it is right to do no SCREAM here...
530 if (prog->float_substr && prog->anchored_substr) {
531 /* Take into account the "other" substring. */
532 /* XXXX May be hopelessly wrong for UTF... */
535 if (check == prog->float_substr) {
538 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
541 t = s - prog->check_offset_max;
542 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
543 && (!(prog->reganch & ROPT_UTF8)
544 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
549 t = HOP3c(t, prog->anchored_offset, strend);
550 if (t < other_last) /* These positions already checked */
552 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
555 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
556 /* On end-of-str: see comment below. */
557 s = fbm_instr((unsigned char*)t,
558 HOP3(HOP3(last1, prog->anchored_offset, strend)
559 + SvCUR(prog->anchored_substr),
560 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
561 prog->anchored_substr,
562 PL_multiline ? FBMrf_MULTILINE : 0);
563 DEBUG_r(PerlIO_printf(Perl_debug_log,
564 "%s anchored substr `%s%.*s%s'%s",
565 (s ? "Found" : "Contradicts"),
567 (int)(SvCUR(prog->anchored_substr)
568 - (SvTAIL(prog->anchored_substr)!=0)),
569 SvPVX(prog->anchored_substr),
570 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
572 if (last1 >= last2) {
573 DEBUG_r(PerlIO_printf(Perl_debug_log,
574 ", giving up...\n"));
577 DEBUG_r(PerlIO_printf(Perl_debug_log,
578 ", trying floating at offset %ld...\n",
579 (long)(HOP3c(s1, 1, strend) - i_strpos)));
580 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
581 s = HOP3c(last, 1, strend);
585 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
586 (long)(s - i_strpos)));
587 t = HOP3c(s, -prog->anchored_offset, strbeg);
588 other_last = HOP3c(s, 1, strend);
596 else { /* Take into account the floating substring. */
600 t = HOP3c(s, -start_shift, strbeg);
602 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
603 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
604 last = HOP3c(t, prog->float_max_offset, strend);
605 s = HOP3c(t, prog->float_min_offset, strend);
608 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
609 /* fbm_instr() takes into account exact value of end-of-str
610 if the check is SvTAIL(ed). Since false positives are OK,
611 and end-of-str is not later than strend we are OK. */
612 s = fbm_instr((unsigned char*)s,
613 (unsigned char*)last + SvCUR(prog->float_substr)
614 - (SvTAIL(prog->float_substr)!=0),
615 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
616 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
617 (s ? "Found" : "Contradicts"),
619 (int)(SvCUR(prog->float_substr)
620 - (SvTAIL(prog->float_substr)!=0)),
621 SvPVX(prog->float_substr),
622 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
625 DEBUG_r(PerlIO_printf(Perl_debug_log,
626 ", giving up...\n"));
629 DEBUG_r(PerlIO_printf(Perl_debug_log,
630 ", trying anchored starting at offset %ld...\n",
631 (long)(s1 + 1 - i_strpos)));
633 s = HOP3c(t, 1, strend);
637 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
638 (long)(s - i_strpos)));
639 other_last = s; /* Fix this later. --Hugo */
648 t = s - prog->check_offset_max;
649 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
650 && (!(prog->reganch & ROPT_UTF8)
651 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
653 /* Fixed substring is found far enough so that the match
654 cannot start at strpos. */
656 if (ml_anch && t[-1] != '\n') {
657 /* Eventually fbm_*() should handle this, but often
658 anchored_offset is not 0, so this check will not be wasted. */
659 /* XXXX In the code below we prefer to look for "^" even in
660 presence of anchored substrings. And we search even
661 beyond the found float position. These pessimizations
662 are historical artefacts only. */
664 while (t < strend - prog->minlen) {
666 if (t < check_at - prog->check_offset_min) {
667 if (prog->anchored_substr) {
668 /* Since we moved from the found position,
669 we definitely contradict the found anchored
670 substr. Due to the above check we do not
671 contradict "check" substr.
672 Thus we can arrive here only if check substr
673 is float. Redo checking for "other"=="fixed".
676 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
677 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
678 goto do_other_anchored;
680 /* We don't contradict the found floating substring. */
681 /* XXXX Why not check for STCLASS? */
683 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
684 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
687 /* Position contradicts check-string */
688 /* XXXX probably better to look for check-string
689 than for "\n", so one should lower the limit for t? */
690 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
691 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
692 other_last = strpos = s = t + 1;
697 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
698 PL_colors[0],PL_colors[1]));
702 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
703 PL_colors[0],PL_colors[1]));
707 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
710 /* The found string does not prohibit matching at strpos,
711 - no optimization of calling REx engine can be performed,
712 unless it was an MBOL and we are not after MBOL,
713 or a future STCLASS check will fail this. */
715 /* Even in this situation we may use MBOL flag if strpos is offset
716 wrt the start of the string. */
717 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
718 && (strpos != strbeg) && strpos[-1] != '\n'
719 /* May be due to an implicit anchor of m{.*foo} */
720 && !(prog->reganch & ROPT_IMPLICIT))
725 DEBUG_r( if (ml_anch)
726 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
727 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
730 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
731 && prog->check_substr /* Could be deleted already */
732 && --BmUSEFUL(prog->check_substr) < 0
733 && prog->check_substr == prog->float_substr)
735 /* If flags & SOMETHING - do not do it many times on the same match */
736 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
737 SvREFCNT_dec(prog->check_substr);
738 prog->check_substr = Nullsv; /* disable */
739 prog->float_substr = Nullsv; /* clear */
740 check = Nullsv; /* abort */
742 /* XXXX This is a remnant of the old implementation. It
743 looks wasteful, since now INTUIT can use many
745 prog->reganch &= ~RE_USE_INTUIT;
752 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
753 if (prog->regstclass) {
754 /* minlen == 0 is possible if regstclass is \b or \B,
755 and the fixed substr is ''$.
756 Since minlen is already taken into account, s+1 is before strend;
757 accidentally, minlen >= 1 guaranties no false positives at s + 1
758 even for \b or \B. But (minlen? 1 : 0) below assumes that
759 regstclass does not come from lookahead... */
760 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
761 This leaves EXACTF only, which is dealt with in find_byclass(). */
762 U8* str = (U8*)STRING(prog->regstclass);
763 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
764 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
766 char *endpos = (prog->anchored_substr || ml_anch)
767 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
768 : (prog->float_substr
769 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
772 char *startpos = strbeg;
775 if (prog->reganch & ROPT_UTF8) {
776 PL_regdata = prog->data;
779 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
784 if (endpos == strend) {
785 DEBUG_r( PerlIO_printf(Perl_debug_log,
786 "Could not match STCLASS...\n") );
789 DEBUG_r( PerlIO_printf(Perl_debug_log,
790 "This position contradicts STCLASS...\n") );
791 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
793 /* Contradict one of substrings */
794 if (prog->anchored_substr) {
795 if (prog->anchored_substr == check) {
796 DEBUG_r( what = "anchored" );
798 s = HOP3c(t, 1, strend);
799 if (s + start_shift + end_shift > strend) {
800 /* XXXX Should be taken into account earlier? */
801 DEBUG_r( PerlIO_printf(Perl_debug_log,
802 "Could not match STCLASS...\n") );
807 DEBUG_r( PerlIO_printf(Perl_debug_log,
808 "Looking for %s substr starting at offset %ld...\n",
809 what, (long)(s + start_shift - i_strpos)) );
812 /* Have both, check_string is floating */
813 if (t + start_shift >= check_at) /* Contradicts floating=check */
814 goto retry_floating_check;
815 /* Recheck anchored substring, but not floating... */
819 DEBUG_r( PerlIO_printf(Perl_debug_log,
820 "Looking for anchored substr starting at offset %ld...\n",
821 (long)(other_last - i_strpos)) );
822 goto do_other_anchored;
824 /* Another way we could have checked stclass at the
825 current position only: */
830 DEBUG_r( PerlIO_printf(Perl_debug_log,
831 "Looking for /%s^%s/m starting at offset %ld...\n",
832 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
835 if (!prog->float_substr) /* Could have been deleted */
837 /* Check is floating subtring. */
838 retry_floating_check:
839 t = check_at - start_shift;
840 DEBUG_r( what = "floating" );
841 goto hop_and_restart;
844 DEBUG_r(PerlIO_printf(Perl_debug_log,
845 "By STCLASS: moving %ld --> %ld\n",
846 (long)(t - i_strpos), (long)(s - i_strpos))
850 DEBUG_r(PerlIO_printf(Perl_debug_log,
851 "Does not contradict STCLASS...\n");
856 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
857 PL_colors[4], (check ? "Guessed" : "Giving up"),
858 PL_colors[5], (long)(s - i_strpos)) );
861 fail_finish: /* Substring not found */
862 if (prog->check_substr) /* could be removed already */
863 BmUSEFUL(prog->check_substr) += 5; /* hooray */
865 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
866 PL_colors[4],PL_colors[5]));
870 /* We know what class REx starts with. Try to find this position... */
872 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
874 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
880 register I32 tmp = 1; /* Scratch variable? */
881 register bool do_utf8 = DO_UTF8(PL_reg_sv);
883 /* We know what class it must start with. */
887 if (reginclass(c, (U8*)s, do_utf8)) {
888 if (tmp && (norun || regtry(prog, s)))
895 s += do_utf8 ? UTF8SKIP(s) : 1;
900 if (tmp && (norun || regtry(prog, s)))
911 c1 = to_utf8_lower((U8*)m);
912 c2 = to_utf8_upper((U8*)m);
923 c2 = PL_fold_locale[c1];
928 e = s; /* Due to minlen logic of intuit() */
934 if ( utf8_to_uvchr((U8*)s, &len) == c1
941 UV c = utf8_to_uvchr((U8*)s, &len);
942 if ( (c == c1 || c == c2) && regtry(prog, s) )
951 && (ln == 1 || !(OP(c) == EXACTF
953 : ibcmp_locale(s, m, ln)))
954 && (norun || regtry(prog, s)) )
960 if ( (*(U8*)s == c1 || *(U8*)s == c2)
961 && (ln == 1 || !(OP(c) == EXACTF
963 : ibcmp_locale(s, m, ln)))
964 && (norun || regtry(prog, s)) )
971 PL_reg_flags |= RF_tainted;
978 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
981 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
983 tmp = ((OP(c) == BOUND ?
984 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
985 LOAD_UTF8_CHARCLASS(alnum,"a");
987 if (tmp == !(OP(c) == BOUND ?
988 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
989 isALNUM_LC_utf8((U8*)s)))
992 if ((norun || regtry(prog, s)))
999 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1000 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1001 while (s < strend) {
1003 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1005 if ((norun || regtry(prog, s)))
1011 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1015 PL_reg_flags |= RF_tainted;
1022 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1025 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1027 tmp = ((OP(c) == NBOUND ?
1028 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1029 LOAD_UTF8_CHARCLASS(alnum,"a");
1030 while (s < strend) {
1031 if (tmp == !(OP(c) == NBOUND ?
1032 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1033 isALNUM_LC_utf8((U8*)s)))
1035 else if ((norun || regtry(prog, s)))
1041 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1042 tmp = ((OP(c) == NBOUND ?
1043 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1044 while (s < strend) {
1046 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1048 else if ((norun || regtry(prog, s)))
1053 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1058 LOAD_UTF8_CHARCLASS(alnum,"a");
1059 while (s < strend) {
1060 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1061 if (tmp && (norun || regtry(prog, s)))
1072 while (s < strend) {
1074 if (tmp && (norun || regtry(prog, s)))
1086 PL_reg_flags |= RF_tainted;
1088 while (s < strend) {
1089 if (isALNUM_LC_utf8((U8*)s)) {
1090 if (tmp && (norun || regtry(prog, s)))
1101 while (s < strend) {
1102 if (isALNUM_LC(*s)) {
1103 if (tmp && (norun || regtry(prog, s)))
1116 LOAD_UTF8_CHARCLASS(alnum,"a");
1117 while (s < strend) {
1118 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1119 if (tmp && (norun || regtry(prog, s)))
1130 while (s < strend) {
1132 if (tmp && (norun || regtry(prog, s)))
1144 PL_reg_flags |= RF_tainted;
1146 while (s < strend) {
1147 if (!isALNUM_LC_utf8((U8*)s)) {
1148 if (tmp && (norun || regtry(prog, s)))
1159 while (s < strend) {
1160 if (!isALNUM_LC(*s)) {
1161 if (tmp && (norun || regtry(prog, s)))
1174 LOAD_UTF8_CHARCLASS(space," ");
1175 while (s < strend) {
1176 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1177 if (tmp && (norun || regtry(prog, s)))
1188 while (s < strend) {
1190 if (tmp && (norun || regtry(prog, s)))
1202 PL_reg_flags |= RF_tainted;
1204 while (s < strend) {
1205 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1206 if (tmp && (norun || regtry(prog, s)))
1217 while (s < strend) {
1218 if (isSPACE_LC(*s)) {
1219 if (tmp && (norun || regtry(prog, s)))
1232 LOAD_UTF8_CHARCLASS(space," ");
1233 while (s < strend) {
1234 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1235 if (tmp && (norun || regtry(prog, s)))
1246 while (s < strend) {
1248 if (tmp && (norun || regtry(prog, s)))
1260 PL_reg_flags |= RF_tainted;
1262 while (s < strend) {
1263 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1264 if (tmp && (norun || regtry(prog, s)))
1275 while (s < strend) {
1276 if (!isSPACE_LC(*s)) {
1277 if (tmp && (norun || regtry(prog, s)))
1290 LOAD_UTF8_CHARCLASS(digit,"0");
1291 while (s < strend) {
1292 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1293 if (tmp && (norun || regtry(prog, s)))
1304 while (s < strend) {
1306 if (tmp && (norun || regtry(prog, s)))
1318 PL_reg_flags |= RF_tainted;
1320 while (s < strend) {
1321 if (isDIGIT_LC_utf8((U8*)s)) {
1322 if (tmp && (norun || regtry(prog, s)))
1333 while (s < strend) {
1334 if (isDIGIT_LC(*s)) {
1335 if (tmp && (norun || regtry(prog, s)))
1348 LOAD_UTF8_CHARCLASS(digit,"0");
1349 while (s < strend) {
1350 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1351 if (tmp && (norun || regtry(prog, s)))
1362 while (s < strend) {
1364 if (tmp && (norun || regtry(prog, s)))
1376 PL_reg_flags |= RF_tainted;
1378 while (s < strend) {
1379 if (!isDIGIT_LC_utf8((U8*)s)) {
1380 if (tmp && (norun || regtry(prog, s)))
1391 while (s < strend) {
1392 if (!isDIGIT_LC(*s)) {
1393 if (tmp && (norun || regtry(prog, s)))
1405 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1414 - regexec_flags - match a regexp against a string
1417 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1418 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1419 /* strend: pointer to null at end of string */
1420 /* strbeg: real beginning of string */
1421 /* minend: end of match must be >=minend after stringarg. */
1422 /* data: May be used for some additional optimizations. */
1423 /* nosave: For optimizations. */
1426 register regnode *c;
1427 register char *startpos = stringarg;
1428 I32 minlen; /* must match at least this many chars */
1429 I32 dontbother = 0; /* how many characters not to try at end */
1430 /* I32 start_shift = 0; */ /* Offset of the start to find
1431 constant substr. */ /* CC */
1432 I32 end_shift = 0; /* Same for the end. */ /* CC */
1433 I32 scream_pos = -1; /* Internal iterator of scream. */
1435 SV* oreplsv = GvSV(PL_replgv);
1436 bool do_utf8 = DO_UTF8(sv);
1442 PL_regnarrate = DEBUG_r_TEST;
1445 /* Be paranoid... */
1446 if (prog == NULL || startpos == NULL) {
1447 Perl_croak(aTHX_ "NULL regexp parameter");
1451 minlen = prog->minlen;
1452 if (do_utf8 && !(prog->reganch & ROPT_CANY_SEEN)) {
1453 if (utf8_distance((U8*)strend, (U8*)startpos) < minlen) goto phooey;
1456 if (strend - startpos < minlen) goto phooey;
1459 /* Check validity of program. */
1460 if (UCHARAT(prog->program) != REG_MAGIC) {
1461 Perl_croak(aTHX_ "corrupted regexp program");
1465 PL_reg_eval_set = 0;
1468 if (prog->reganch & ROPT_UTF8)
1469 PL_reg_flags |= RF_utf8;
1471 /* Mark beginning of line for ^ and lookbehind. */
1472 PL_regbol = startpos;
1476 /* Mark end of line for $ (and such) */
1479 /* see how far we have to get to not match where we matched before */
1480 PL_regtill = startpos+minend;
1482 /* We start without call_cc context. */
1485 /* If there is a "must appear" string, look for it. */
1488 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1491 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1492 PL_reg_ganch = startpos;
1493 else if (sv && SvTYPE(sv) >= SVt_PVMG
1495 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1496 && mg->mg_len >= 0) {
1497 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1498 if (prog->reganch & ROPT_ANCH_GPOS) {
1499 if (s > PL_reg_ganch)
1504 else /* pos() not defined */
1505 PL_reg_ganch = strbeg;
1508 if (do_utf8 == (UTF!=0) &&
1509 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1510 re_scream_pos_data d;
1512 d.scream_olds = &scream_olds;
1513 d.scream_pos = &scream_pos;
1514 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1516 goto phooey; /* not present */
1519 DEBUG_r( if (!PL_colorset) reginitcolors() );
1520 DEBUG_r(PerlIO_printf(Perl_debug_log,
1521 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1522 PL_colors[4],PL_colors[5],PL_colors[0],
1525 (strlen(prog->precomp) > 60 ? "..." : ""),
1527 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1528 startpos, PL_colors[1],
1529 (strend - startpos > 60 ? "..." : ""))
1532 /* Simplest case: anchored match need be tried only once. */
1533 /* [unless only anchor is BOL and multiline is set] */
1534 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1535 if (s == startpos && regtry(prog, startpos))
1537 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1538 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1543 dontbother = minlen - 1;
1544 end = HOP3c(strend, -dontbother, strbeg) - 1;
1545 /* for multiline we only have to try after newlines */
1546 if (prog->check_substr) {
1550 if (regtry(prog, s))
1555 if (prog->reganch & RE_USE_INTUIT) {
1556 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1567 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1568 if (regtry(prog, s))
1575 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1576 if (regtry(prog, PL_reg_ganch))
1581 /* Messy cases: unanchored match. */
1582 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1583 /* we have /x+whatever/ */
1584 /* it must be a one character string (XXXX Except UTF?) */
1585 char ch = SvPVX(prog->anchored_substr)[0];
1591 while (s < strend) {
1593 DEBUG_r( did_match = 1 );
1594 if (regtry(prog, s)) goto got_it;
1596 while (s < strend && *s == ch)
1603 while (s < strend) {
1605 DEBUG_r( did_match = 1 );
1606 if (regtry(prog, s)) goto got_it;
1608 while (s < strend && *s == ch)
1614 DEBUG_r(if (!did_match)
1615 PerlIO_printf(Perl_debug_log,
1616 "Did not find anchored character...\n")
1620 else if (do_utf8 == (UTF!=0) &&
1621 (prog->anchored_substr != Nullsv
1622 || (prog->float_substr != Nullsv
1623 && prog->float_max_offset < strend - s))) {
1624 SV *must = prog->anchored_substr
1625 ? prog->anchored_substr : prog->float_substr;
1627 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1629 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1630 char *last = HOP3c(strend, /* Cannot start after this */
1631 -(I32)(CHR_SVLEN(must)
1632 - (SvTAIL(must) != 0) + back_min), strbeg);
1633 char *last1; /* Last position checked before */
1639 last1 = HOPc(s, -1);
1641 last1 = s - 1; /* bogus */
1643 /* XXXX check_substr already used to find `s', can optimize if
1644 check_substr==must. */
1646 dontbother = end_shift;
1647 strend = HOPc(strend, -dontbother);
1648 while ( (s <= last) &&
1649 ((flags & REXEC_SCREAM)
1650 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1651 end_shift, &scream_pos, 0))
1652 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1653 (unsigned char*)strend, must,
1654 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1655 DEBUG_r( did_match = 1 );
1656 if (HOPc(s, -back_max) > last1) {
1657 last1 = HOPc(s, -back_min);
1658 s = HOPc(s, -back_max);
1661 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1663 last1 = HOPc(s, -back_min);
1667 while (s <= last1) {
1668 if (regtry(prog, s))
1674 while (s <= last1) {
1675 if (regtry(prog, s))
1681 DEBUG_r(if (!did_match)
1682 PerlIO_printf(Perl_debug_log,
1683 "Did not find %s substr `%s%.*s%s'%s...\n",
1684 ((must == prog->anchored_substr)
1685 ? "anchored" : "floating"),
1687 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1689 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1693 else if ((c = prog->regstclass)) {
1694 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1695 /* don't bother with what can't match */
1696 strend = HOPc(strend, -(minlen - 1));
1698 SV *prop = sv_newmortal();
1700 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), s);
1702 if (find_byclass(prog, c, s, strend, startpos, 0))
1704 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1708 if (prog->float_substr != Nullsv) { /* Trim the end. */
1711 if (flags & REXEC_SCREAM) {
1712 last = screaminstr(sv, prog->float_substr, s - strbeg,
1713 end_shift, &scream_pos, 1); /* last one */
1715 last = scream_olds; /* Only one occurrence. */
1719 char *little = SvPV(prog->float_substr, len);
1721 if (SvTAIL(prog->float_substr)) {
1722 if (memEQ(strend - len + 1, little, len - 1))
1723 last = strend - len + 1;
1724 else if (!PL_multiline)
1725 last = memEQ(strend - len, little, len)
1726 ? strend - len : Nullch;
1732 last = rninstr(s, strend, little, little + len);
1734 last = strend; /* matching `$' */
1738 DEBUG_r(PerlIO_printf(Perl_debug_log,
1739 "%sCan't trim the tail, match fails (should not happen)%s\n",
1740 PL_colors[4],PL_colors[5]));
1741 goto phooey; /* Should not happen! */
1743 dontbother = strend - last + prog->float_min_offset;
1745 if (minlen && (dontbother < minlen))
1746 dontbother = minlen - 1;
1747 strend -= dontbother; /* this one's always in bytes! */
1748 /* We don't know much -- general case. */
1751 if (regtry(prog, s))
1760 if (regtry(prog, s))
1762 } while (s++ < strend);
1770 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1772 if (PL_reg_eval_set) {
1773 /* Preserve the current value of $^R */
1774 if (oreplsv != GvSV(PL_replgv))
1775 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1776 restored, the value remains
1778 restore_pos(aTHXo_ 0);
1781 /* make sure $`, $&, $', and $digit will work later */
1782 if ( !(flags & REXEC_NOT_FIRST) ) {
1783 if (RX_MATCH_COPIED(prog)) {
1784 Safefree(prog->subbeg);
1785 RX_MATCH_COPIED_off(prog);
1787 if (flags & REXEC_COPY_STR) {
1788 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1790 s = savepvn(strbeg, i);
1793 RX_MATCH_COPIED_on(prog);
1796 prog->subbeg = strbeg;
1797 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1804 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1805 PL_colors[4],PL_colors[5]));
1806 if (PL_reg_eval_set)
1807 restore_pos(aTHXo_ 0);
1812 - regtry - try match at specific point
1814 STATIC I32 /* 0 failure, 1 success */
1815 S_regtry(pTHX_ regexp *prog, char *startpos)
1823 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1825 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1828 PL_reg_eval_set = RS_init;
1830 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1831 (IV)(PL_stack_sp - PL_stack_base));
1833 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1834 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1835 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1837 /* Apparently this is not needed, judging by wantarray. */
1838 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1839 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1842 /* Make $_ available to executed code. */
1843 if (PL_reg_sv != DEFSV) {
1844 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1849 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1850 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1851 /* prepare for quick setting of pos */
1852 sv_magic(PL_reg_sv, (SV*)0,
1853 PERL_MAGIC_regex_global, Nullch, 0);
1854 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1858 PL_reg_oldpos = mg->mg_len;
1859 SAVEDESTRUCTOR_X(restore_pos, 0);
1861 if (!PL_reg_curpm) {
1862 Newz(22,PL_reg_curpm, 1, PMOP);
1865 SV* repointer = newSViv(0);
1866 /* so we know which PL_regex_padav element is PL_reg_curpm */
1867 SvFLAGS(repointer) |= SVf_BREAK;
1868 av_push(PL_regex_padav,repointer);
1869 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
1870 PL_regex_pad = AvARRAY(PL_regex_padav);
1874 PM_SETRE(PL_reg_curpm, prog);
1875 PL_reg_oldcurpm = PL_curpm;
1876 PL_curpm = PL_reg_curpm;
1877 if (RX_MATCH_COPIED(prog)) {
1878 /* Here is a serious problem: we cannot rewrite subbeg,
1879 since it may be needed if this match fails. Thus
1880 $` inside (?{}) could fail... */
1881 PL_reg_oldsaved = prog->subbeg;
1882 PL_reg_oldsavedlen = prog->sublen;
1883 RX_MATCH_COPIED_off(prog);
1886 PL_reg_oldsaved = Nullch;
1887 prog->subbeg = PL_bostr;
1888 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1890 prog->startp[0] = startpos - PL_bostr;
1891 PL_reginput = startpos;
1892 PL_regstartp = prog->startp;
1893 PL_regendp = prog->endp;
1894 PL_reglastparen = &prog->lastparen;
1895 PL_reglastcloseparen = &prog->lastcloseparen;
1896 prog->lastparen = 0;
1898 DEBUG_r(PL_reg_starttry = startpos);
1899 if (PL_reg_start_tmpl <= prog->nparens) {
1900 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1901 if(PL_reg_start_tmp)
1902 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1904 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1907 /* XXXX What this code is doing here?!!! There should be no need
1908 to do this again and again, PL_reglastparen should take care of
1911 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1912 * Actually, the code in regcppop() (which Ilya may be meaning by
1913 * PL_reglastparen), is not needed at all by the test suite
1914 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1915 * enough, for building DynaLoader, or otherwise this
1916 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1917 * will happen. Meanwhile, this code *is* needed for the
1918 * above-mentioned test suite tests to succeed. The common theme
1919 * on those tests seems to be returning null fields from matches.
1924 if (prog->nparens) {
1925 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1932 if (regmatch(prog->program + 1)) {
1933 prog->endp[0] = PL_reginput - PL_bostr;
1936 REGCP_UNWIND(lastcp);
1940 #define RE_UNWIND_BRANCH 1
1941 #define RE_UNWIND_BRANCHJ 2
1945 typedef struct { /* XX: makes sense to enlarge it... */
1949 } re_unwind_generic_t;
1962 } re_unwind_branch_t;
1964 typedef union re_unwind_t {
1966 re_unwind_generic_t generic;
1967 re_unwind_branch_t branch;
1970 #define sayYES goto yes
1971 #define sayNO goto no
1972 #define sayYES_FINAL goto yes_final
1973 #define sayYES_LOUD goto yes_loud
1974 #define sayNO_FINAL goto no_final
1975 #define sayNO_SILENT goto do_no
1976 #define saySAME(x) if (x) goto yes; else goto no
1978 #define REPORT_CODE_OFF 24
1981 - regmatch - main matching routine
1983 * Conceptually the strategy is simple: check to see whether the current
1984 * node matches, call self recursively to see whether the rest matches,
1985 * and then act accordingly. In practice we make some effort to avoid
1986 * recursion, in particular by going through "ordinary" nodes (that don't
1987 * need to know whether the rest of the match failed) by a loop instead of
1990 /* [lwall] I've hoisted the register declarations to the outer block in order to
1991 * maybe save a little bit of pushing and popping on the stack. It also takes
1992 * advantage of machines that use a register save mask on subroutine entry.
1994 STATIC I32 /* 0 failure, 1 success */
1995 S_regmatch(pTHX_ regnode *prog)
1997 register regnode *scan; /* Current node. */
1998 regnode *next; /* Next node. */
1999 regnode *inner; /* Next node in internal branch. */
2000 register I32 nextchr; /* renamed nextchr - nextchar colides with
2001 function of same name */
2002 register I32 n; /* no or next */
2003 register I32 ln = 0; /* len or last */
2004 register char *s = Nullch; /* operand or save */
2005 register char *locinput = PL_reginput;
2006 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2007 int minmod = 0, sw = 0, logical = 0;
2010 I32 firstcp = PL_savestack_ix;
2012 register bool do_utf8 = DO_UTF8(PL_reg_sv);
2018 /* Note that nextchr is a byte even in UTF */
2019 nextchr = UCHARAT(locinput);
2021 while (scan != NULL) {
2024 SV *prop = sv_newmortal();
2025 int docolor = *PL_colors[0];
2026 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2027 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2028 /* The part of the string before starttry has one color
2029 (pref0_len chars), between starttry and current
2030 position another one (pref_len - pref0_len chars),
2031 after the current position the third one.
2032 We assume that pref0_len <= pref_len, otherwise we
2033 decrease pref0_len. */
2034 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2035 ? (5 + taill) - l : locinput - PL_bostr;
2038 while (UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2040 pref0_len = pref_len - (locinput - PL_reg_starttry);
2041 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2042 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2043 ? (5 + taill) - pref_len : PL_regeol - locinput);
2044 while (UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2048 if (pref0_len > pref_len)
2049 pref0_len = pref_len;
2050 regprop(prop, scan);
2051 PerlIO_printf(Perl_debug_log,
2052 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2053 (IV)(locinput - PL_bostr),
2054 PL_colors[4], pref0_len,
2055 locinput - pref_len, PL_colors[5],
2056 PL_colors[2], pref_len - pref0_len,
2057 locinput - pref_len + pref0_len, PL_colors[3],
2058 (docolor ? "" : "> <"),
2059 PL_colors[0], l, locinput, PL_colors[1],
2060 15 - l - pref_len + 1,
2062 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2066 next = scan + NEXT_OFF(scan);
2072 if (locinput == PL_bostr || (PL_multiline &&
2073 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2075 /* regtill = regbol; */
2080 if (locinput == PL_bostr ||
2081 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2087 if (locinput == PL_bostr)
2091 if (locinput == PL_reg_ganch)
2101 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2106 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2108 if (PL_regeol - locinput > 1)
2112 if (PL_regeol != locinput)
2116 if (!nextchr && locinput >= PL_regeol)
2119 locinput += PL_utf8skip[nextchr];
2120 if (locinput > PL_regeol)
2122 nextchr = UCHARAT(locinput);
2125 nextchr = UCHARAT(++locinput);
2128 if (!nextchr && locinput >= PL_regeol)
2130 nextchr = UCHARAT(++locinput);
2133 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2136 locinput += PL_utf8skip[nextchr];
2137 if (locinput > PL_regeol)
2139 nextchr = UCHARAT(locinput);
2142 nextchr = UCHARAT(++locinput);
2147 if (do_utf8 != (UTF!=0)) {
2155 if (*((U8*)s) != utf8_to_uvchr((U8*)l, &len))
2164 if (*((U8*)l) != utf8_to_uvchr((U8*)s, &len))
2170 nextchr = UCHARAT(locinput);
2173 /* Inline the first character, for speed. */
2174 if (UCHARAT(s) != nextchr)
2176 if (PL_regeol - locinput < ln)
2178 if (ln > 1 && memNE(s, locinput, ln))
2181 nextchr = UCHARAT(locinput);
2184 PL_reg_flags |= RF_tainted;
2194 c1 = OP(scan) == EXACTF;
2196 if (l >= PL_regeol) {
2199 if ((UTF ? utf8n_to_uvchr((U8*)s, e - s, 0, 0) : *((U8*)s)) !=
2200 (c1 ? toLOWER_utf8((U8*)l) : toLOWER_LC_utf8((U8*)l)))
2202 s += UTF ? UTF8SKIP(s) : 1;
2206 nextchr = UCHARAT(locinput);
2210 /* Inline the first character, for speed. */
2211 if (UCHARAT(s) != nextchr &&
2212 UCHARAT(s) != ((OP(scan) == EXACTF)
2213 ? PL_fold : PL_fold_locale)[nextchr])
2215 if (PL_regeol - locinput < ln)
2217 if (ln > 1 && (OP(scan) == EXACTF
2218 ? ibcmp(s, locinput, ln)
2219 : ibcmp_locale(s, locinput, ln)))
2222 nextchr = UCHARAT(locinput);
2226 if (!reginclass(scan, (U8*)locinput, do_utf8))
2228 if (locinput >= PL_regeol)
2230 locinput += PL_utf8skip[nextchr];
2231 nextchr = UCHARAT(locinput);
2235 nextchr = UCHARAT(locinput);
2236 if (!reginclass(scan, (U8*)locinput, do_utf8))
2238 if (!nextchr && locinput >= PL_regeol)
2240 nextchr = UCHARAT(++locinput);
2244 PL_reg_flags |= RF_tainted;
2250 LOAD_UTF8_CHARCLASS(alnum,"a");
2251 if (!(OP(scan) == ALNUM
2252 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2253 : isALNUM_LC_utf8((U8*)locinput)))
2257 locinput += PL_utf8skip[nextchr];
2258 nextchr = UCHARAT(locinput);
2261 if (!(OP(scan) == ALNUM
2262 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2264 nextchr = UCHARAT(++locinput);
2267 PL_reg_flags |= RF_tainted;
2270 if (!nextchr && locinput >= PL_regeol)
2273 LOAD_UTF8_CHARCLASS(alnum,"a");
2274 if (OP(scan) == NALNUM
2275 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2276 : isALNUM_LC_utf8((U8*)locinput))
2280 locinput += PL_utf8skip[nextchr];
2281 nextchr = UCHARAT(locinput);
2284 if (OP(scan) == NALNUM
2285 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2287 nextchr = UCHARAT(++locinput);
2291 PL_reg_flags |= RF_tainted;
2295 /* was last char in word? */
2297 if (locinput == PL_bostr)
2300 U8 *r = reghop((U8*)locinput, -1);
2302 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2304 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2305 ln = isALNUM_uni(ln);
2306 LOAD_UTF8_CHARCLASS(alnum,"a");
2307 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2310 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2311 n = isALNUM_LC_utf8((U8*)locinput);
2315 ln = (locinput != PL_bostr) ?
2316 UCHARAT(locinput - 1) : '\n';
2317 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2319 n = isALNUM(nextchr);
2322 ln = isALNUM_LC(ln);
2323 n = isALNUM_LC(nextchr);
2326 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2327 OP(scan) == BOUNDL))
2331 PL_reg_flags |= RF_tainted;
2337 if (UTF8_IS_CONTINUED(nextchr)) {
2338 LOAD_UTF8_CHARCLASS(space," ");
2339 if (!(OP(scan) == SPACE
2340 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2341 : isSPACE_LC_utf8((U8*)locinput)))
2345 locinput += PL_utf8skip[nextchr];
2346 nextchr = UCHARAT(locinput);
2349 if (!(OP(scan) == SPACE
2350 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2352 nextchr = UCHARAT(++locinput);
2355 if (!(OP(scan) == SPACE
2356 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2358 nextchr = UCHARAT(++locinput);
2362 PL_reg_flags |= RF_tainted;
2365 if (!nextchr && locinput >= PL_regeol)
2368 LOAD_UTF8_CHARCLASS(space," ");
2369 if (OP(scan) == NSPACE
2370 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2371 : isSPACE_LC_utf8((U8*)locinput))
2375 locinput += PL_utf8skip[nextchr];
2376 nextchr = UCHARAT(locinput);
2379 if (OP(scan) == NSPACE
2380 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2382 nextchr = UCHARAT(++locinput);
2385 PL_reg_flags |= RF_tainted;
2391 LOAD_UTF8_CHARCLASS(digit,"0");
2392 if (!(OP(scan) == DIGIT
2393 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2394 : isDIGIT_LC_utf8((U8*)locinput)))
2398 locinput += PL_utf8skip[nextchr];
2399 nextchr = UCHARAT(locinput);
2402 if (!(OP(scan) == DIGIT
2403 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2405 nextchr = UCHARAT(++locinput);
2408 PL_reg_flags |= RF_tainted;
2411 if (!nextchr && locinput >= PL_regeol)
2414 LOAD_UTF8_CHARCLASS(digit,"0");
2415 if (OP(scan) == NDIGIT
2416 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2417 : isDIGIT_LC_utf8((U8*)locinput))
2421 locinput += PL_utf8skip[nextchr];
2422 nextchr = UCHARAT(locinput);
2425 if (OP(scan) == NDIGIT
2426 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2428 nextchr = UCHARAT(++locinput);
2431 LOAD_UTF8_CHARCLASS(mark,"~");
2432 if (locinput >= PL_regeol ||
2433 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2435 locinput += PL_utf8skip[nextchr];
2436 while (locinput < PL_regeol &&
2437 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2438 locinput += UTF8SKIP(locinput);
2439 if (locinput > PL_regeol)
2441 nextchr = UCHARAT(locinput);
2444 PL_reg_flags |= RF_tainted;
2448 n = ARG(scan); /* which paren pair */
2449 ln = PL_regstartp[n];
2450 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2451 if (*PL_reglastparen < n || ln == -1)
2452 sayNO; /* Do not match unless seen CLOSEn. */
2453 if (ln == PL_regendp[n])
2457 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2459 char *e = PL_bostr + PL_regendp[n];
2461 * Note that we can't do the "other character" lookup trick as
2462 * in the 8-bit case (no pun intended) because in Unicode we
2463 * have to map both upper and title case to lower case.
2465 if (OP(scan) == REFF) {
2469 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2479 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2486 nextchr = UCHARAT(locinput);
2490 /* Inline the first character, for speed. */
2491 if (UCHARAT(s) != nextchr &&
2493 (UCHARAT(s) != ((OP(scan) == REFF
2494 ? PL_fold : PL_fold_locale)[nextchr]))))
2496 ln = PL_regendp[n] - ln;
2497 if (locinput + ln > PL_regeol)
2499 if (ln > 1 && (OP(scan) == REF
2500 ? memNE(s, locinput, ln)
2502 ? ibcmp(s, locinput, ln)
2503 : ibcmp_locale(s, locinput, ln))))
2506 nextchr = UCHARAT(locinput);
2517 OP_4tree *oop = PL_op;
2518 COP *ocurcop = PL_curcop;
2519 SV **ocurpad = PL_curpad;
2523 PL_op = (OP_4tree*)PL_regdata->data[n];
2524 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2525 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2526 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2528 CALLRUNOPS(aTHX); /* Scalar context. */
2534 PL_curpad = ocurpad;
2535 PL_curcop = ocurcop;
2537 if (logical == 2) { /* Postponed subexpression. */
2539 MAGIC *mg = Null(MAGIC*);
2541 CHECKPOINT cp, lastcp;
2543 if(SvROK(ret) || SvRMAGICAL(ret)) {
2544 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2547 mg = mg_find(sv, PERL_MAGIC_qr);
2550 re = (regexp *)mg->mg_obj;
2551 (void)ReREFCNT_inc(re);
2555 char *t = SvPV(ret, len);
2557 char *oprecomp = PL_regprecomp;
2558 I32 osize = PL_regsize;
2559 I32 onpar = PL_regnpar;
2562 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2564 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2565 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2567 PL_regprecomp = oprecomp;
2572 PerlIO_printf(Perl_debug_log,
2573 "Entering embedded `%s%.60s%s%s'\n",
2577 (strlen(re->precomp) > 60 ? "..." : ""))
2580 state.prev = PL_reg_call_cc;
2581 state.cc = PL_regcc;
2582 state.re = PL_reg_re;
2586 cp = regcppush(0); /* Save *all* the positions. */
2589 state.ss = PL_savestack_ix;
2590 *PL_reglastparen = 0;
2591 *PL_reglastcloseparen = 0;
2592 PL_reg_call_cc = &state;
2593 PL_reginput = locinput;
2595 /* XXXX This is too dramatic a measure... */
2598 if (regmatch(re->program + 1)) {
2599 /* Even though we succeeded, we need to restore
2600 global variables, since we may be wrapped inside
2601 SUSPEND, thus the match may be not finished yet. */
2603 /* XXXX Do this only if SUSPENDed? */
2604 PL_reg_call_cc = state.prev;
2605 PL_regcc = state.cc;
2606 PL_reg_re = state.re;
2607 cache_re(PL_reg_re);
2609 /* XXXX This is too dramatic a measure... */
2612 /* These are needed even if not SUSPEND. */
2618 REGCP_UNWIND(lastcp);
2620 PL_reg_call_cc = state.prev;
2621 PL_regcc = state.cc;
2622 PL_reg_re = state.re;
2623 cache_re(PL_reg_re);
2625 /* XXXX This is too dramatic a measure... */
2634 sv_setsv(save_scalar(PL_replgv), ret);
2638 n = ARG(scan); /* which paren pair */
2639 PL_reg_start_tmp[n] = locinput;
2644 n = ARG(scan); /* which paren pair */
2645 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2646 PL_regendp[n] = locinput - PL_bostr;
2647 if (n > *PL_reglastparen)
2648 *PL_reglastparen = n;
2649 *PL_reglastcloseparen = n;
2652 n = ARG(scan); /* which paren pair */
2653 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2656 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2658 next = NEXTOPER(NEXTOPER(scan));
2660 next = scan + ARG(scan);
2661 if (OP(next) == IFTHEN) /* Fake one. */
2662 next = NEXTOPER(NEXTOPER(next));
2666 logical = scan->flags;
2668 /*******************************************************************
2669 PL_regcc contains infoblock about the innermost (...)* loop, and
2670 a pointer to the next outer infoblock.
2672 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2674 1) After matching X, regnode for CURLYX is processed;
2676 2) This regnode creates infoblock on the stack, and calls
2677 regmatch() recursively with the starting point at WHILEM node;
2679 3) Each hit of WHILEM node tries to match A and Z (in the order
2680 depending on the current iteration, min/max of {min,max} and
2681 greediness). The information about where are nodes for "A"
2682 and "Z" is read from the infoblock, as is info on how many times "A"
2683 was already matched, and greediness.
2685 4) After A matches, the same WHILEM node is hit again.
2687 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2688 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2689 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2690 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2691 of the external loop.
2693 Currently present infoblocks form a tree with a stem formed by PL_curcc
2694 and whatever it mentions via ->next, and additional attached trees
2695 corresponding to temporarily unset infoblocks as in "5" above.
2697 In the following picture infoblocks for outer loop of
2698 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2699 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2700 infoblocks are drawn below the "reset" infoblock.
2702 In fact in the picture below we do not show failed matches for Z and T
2703 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2704 more obvious *why* one needs to *temporary* unset infoblocks.]
2706 Matched REx position InfoBlocks Comment
2710 Y A)*?Z)*?T x <- O <- I
2711 YA )*?Z)*?T x <- O <- I
2712 YA A)*?Z)*?T x <- O <- I
2713 YAA )*?Z)*?T x <- O <- I
2714 YAA Z)*?T x <- O # Temporary unset I
2717 YAAZ Y(A)*?Z)*?T x <- O
2720 YAAZY (A)*?Z)*?T x <- O
2723 YAAZY A)*?Z)*?T x <- O <- I
2726 YAAZYA )*?Z)*?T x <- O <- I
2729 YAAZYA Z)*?T x <- O # Temporary unset I
2735 YAAZYAZ T x # Temporary unset O
2742 *******************************************************************/
2745 CHECKPOINT cp = PL_savestack_ix;
2746 /* No need to save/restore up to this paren */
2747 I32 parenfloor = scan->flags;
2749 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2751 cc.oldcc = PL_regcc;
2753 /* XXXX Probably it is better to teach regpush to support
2754 parenfloor > PL_regsize... */
2755 if (parenfloor > *PL_reglastparen)
2756 parenfloor = *PL_reglastparen; /* Pessimization... */
2757 cc.parenfloor = parenfloor;
2759 cc.min = ARG1(scan);
2760 cc.max = ARG2(scan);
2761 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2765 PL_reginput = locinput;
2766 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2768 PL_regcc = cc.oldcc;
2774 * This is really hard to understand, because after we match
2775 * what we're trying to match, we must make sure the rest of
2776 * the REx is going to match for sure, and to do that we have
2777 * to go back UP the parse tree by recursing ever deeper. And
2778 * if it fails, we have to reset our parent's current state
2779 * that we can try again after backing off.
2782 CHECKPOINT cp, lastcp;
2783 CURCUR* cc = PL_regcc;
2784 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2786 n = cc->cur + 1; /* how many we know we matched */
2787 PL_reginput = locinput;
2790 PerlIO_printf(Perl_debug_log,
2791 "%*s %ld out of %ld..%ld cc=%lx\n",
2792 REPORT_CODE_OFF+PL_regindent*2, "",
2793 (long)n, (long)cc->min,
2794 (long)cc->max, (long)cc)
2797 /* If degenerate scan matches "", assume scan done. */
2799 if (locinput == cc->lastloc && n >= cc->min) {
2800 PL_regcc = cc->oldcc;
2804 PerlIO_printf(Perl_debug_log,
2805 "%*s empty match detected, try continuation...\n",
2806 REPORT_CODE_OFF+PL_regindent*2, "")
2808 if (regmatch(cc->next))
2816 /* First just match a string of min scans. */
2820 cc->lastloc = locinput;
2821 if (regmatch(cc->scan))
2824 cc->lastloc = lastloc;
2829 /* Check whether we already were at this position.
2830 Postpone detection until we know the match is not
2831 *that* much linear. */
2832 if (!PL_reg_maxiter) {
2833 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2834 PL_reg_leftiter = PL_reg_maxiter;
2836 if (PL_reg_leftiter-- == 0) {
2837 I32 size = (PL_reg_maxiter + 7)/8;
2838 if (PL_reg_poscache) {
2839 if (PL_reg_poscache_size < size) {
2840 Renew(PL_reg_poscache, size, char);
2841 PL_reg_poscache_size = size;
2843 Zero(PL_reg_poscache, size, char);
2846 PL_reg_poscache_size = size;
2847 Newz(29, PL_reg_poscache, size, char);
2850 PerlIO_printf(Perl_debug_log,
2851 "%sDetected a super-linear match, switching on caching%s...\n",
2852 PL_colors[4], PL_colors[5])
2855 if (PL_reg_leftiter < 0) {
2856 I32 o = locinput - PL_bostr, b;
2858 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2861 if (PL_reg_poscache[o] & (1<<b)) {
2863 PerlIO_printf(Perl_debug_log,
2864 "%*s already tried at this position...\n",
2865 REPORT_CODE_OFF+PL_regindent*2, "")
2869 PL_reg_poscache[o] |= (1<<b);
2873 /* Prefer next over scan for minimal matching. */
2876 PL_regcc = cc->oldcc;
2879 cp = regcppush(cc->parenfloor);
2881 if (regmatch(cc->next)) {
2883 sayYES; /* All done. */
2885 REGCP_UNWIND(lastcp);
2891 if (n >= cc->max) { /* Maximum greed exceeded? */
2892 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2893 && !(PL_reg_flags & RF_warned)) {
2894 PL_reg_flags |= RF_warned;
2895 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2896 "Complex regular subexpression recursion",
2903 PerlIO_printf(Perl_debug_log,
2904 "%*s trying longer...\n",
2905 REPORT_CODE_OFF+PL_regindent*2, "")
2907 /* Try scanning more and see if it helps. */
2908 PL_reginput = locinput;
2910 cc->lastloc = locinput;
2911 cp = regcppush(cc->parenfloor);
2913 if (regmatch(cc->scan)) {
2917 REGCP_UNWIND(lastcp);
2920 cc->lastloc = lastloc;
2924 /* Prefer scan over next for maximal matching. */
2926 if (n < cc->max) { /* More greed allowed? */
2927 cp = regcppush(cc->parenfloor);
2929 cc->lastloc = locinput;
2931 if (regmatch(cc->scan)) {
2935 REGCP_UNWIND(lastcp);
2936 regcppop(); /* Restore some previous $<digit>s? */
2937 PL_reginput = locinput;
2939 PerlIO_printf(Perl_debug_log,
2940 "%*s failed, try continuation...\n",
2941 REPORT_CODE_OFF+PL_regindent*2, "")
2944 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2945 && !(PL_reg_flags & RF_warned)) {
2946 PL_reg_flags |= RF_warned;
2947 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2948 "Complex regular subexpression recursion",
2952 /* Failed deeper matches of scan, so see if this one works. */
2953 PL_regcc = cc->oldcc;
2956 if (regmatch(cc->next))
2962 cc->lastloc = lastloc;
2967 next = scan + ARG(scan);
2970 inner = NEXTOPER(NEXTOPER(scan));
2973 inner = NEXTOPER(scan);
2977 if (OP(next) != c1) /* No choice. */
2978 next = inner; /* Avoid recursion. */
2980 I32 lastparen = *PL_reglastparen;
2982 re_unwind_branch_t *uw;
2984 /* Put unwinding data on stack */
2985 unwind1 = SSNEWt(1,re_unwind_branch_t);
2986 uw = SSPTRt(unwind1,re_unwind_branch_t);
2989 uw->type = ((c1 == BRANCH)
2991 : RE_UNWIND_BRANCHJ);
2992 uw->lastparen = lastparen;
2994 uw->locinput = locinput;
2995 uw->nextchr = nextchr;
2997 uw->regindent = ++PL_regindent;
3000 REGCP_SET(uw->lastcp);
3002 /* Now go into the first branch */
3015 /* We suppose that the next guy does not need
3016 backtracking: in particular, it is of constant length,
3017 and has no parenths to influence future backrefs. */
3018 ln = ARG1(scan); /* min to match */
3019 n = ARG2(scan); /* max to match */
3020 paren = scan->flags;
3022 if (paren > PL_regsize)
3024 if (paren > *PL_reglastparen)
3025 *PL_reglastparen = paren;
3027 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3029 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3030 PL_reginput = locinput;
3033 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3035 if (ln && l == 0 && n >= ln
3036 /* In fact, this is tricky. If paren, then the
3037 fact that we did/didnot match may influence
3038 future execution. */
3039 && !(paren && ln == 0))
3041 locinput = PL_reginput;
3042 if (PL_regkind[(U8)OP(next)] == EXACT) {
3043 c1 = (U8)*STRING(next);
3044 if (OP(next) == EXACTF)
3046 else if (OP(next) == EXACTFL)
3047 c2 = PL_fold_locale[c1];
3054 /* This may be improved if l == 0. */
3055 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3056 /* If it could work, try it. */
3058 UCHARAT(PL_reginput) == c1 ||
3059 UCHARAT(PL_reginput) == c2)
3063 PL_regstartp[paren] =
3064 HOPc(PL_reginput, -l) - PL_bostr;
3065 PL_regendp[paren] = PL_reginput - PL_bostr;
3068 PL_regendp[paren] = -1;
3072 REGCP_UNWIND(lastcp);
3074 /* Couldn't or didn't -- move forward. */
3075 PL_reginput = locinput;
3076 if (regrepeat_hard(scan, 1, &l)) {
3078 locinput = PL_reginput;
3085 n = regrepeat_hard(scan, n, &l);
3086 if (n != 0 && l == 0
3087 /* In fact, this is tricky. If paren, then the
3088 fact that we did/didnot match may influence
3089 future execution. */
3090 && !(paren && ln == 0))
3092 locinput = PL_reginput;
3094 PerlIO_printf(Perl_debug_log,
3095 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3096 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3100 if (PL_regkind[(U8)OP(next)] == EXACT) {
3101 c1 = (U8)*STRING(next);
3102 if (OP(next) == EXACTF)
3104 else if (OP(next) == EXACTFL)
3105 c2 = PL_fold_locale[c1];
3114 /* If it could work, try it. */
3116 UCHARAT(PL_reginput) == c1 ||
3117 UCHARAT(PL_reginput) == c2)
3120 PerlIO_printf(Perl_debug_log,
3121 "%*s trying tail with n=%"IVdf"...\n",
3122 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3126 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3127 PL_regendp[paren] = PL_reginput - PL_bostr;
3130 PL_regendp[paren] = -1;
3134 REGCP_UNWIND(lastcp);
3136 /* Couldn't or didn't -- back up. */
3138 locinput = HOPc(locinput, -l);
3139 PL_reginput = locinput;
3146 paren = scan->flags; /* Which paren to set */
3147 if (paren > PL_regsize)
3149 if (paren > *PL_reglastparen)
3150 *PL_reglastparen = paren;
3151 ln = ARG1(scan); /* min to match */
3152 n = ARG2(scan); /* max to match */
3153 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3157 ln = ARG1(scan); /* min to match */
3158 n = ARG2(scan); /* max to match */
3159 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3164 scan = NEXTOPER(scan);
3170 scan = NEXTOPER(scan);
3174 * Lookahead to avoid useless match attempts
3175 * when we know what character comes next.
3177 if (PL_regkind[(U8)OP(next)] == EXACT) {
3178 U8 *s = (U8*)STRING(next);
3181 if (OP(next) == EXACTF)
3183 else if (OP(next) == EXACTFL)
3184 c2 = PL_fold_locale[c1];
3187 if (OP(next) == EXACTF) {
3188 c1 = to_utf8_lower(s);
3189 c2 = to_utf8_upper(s);
3192 c2 = c1 = utf8_to_uvchr(s, NULL);
3198 PL_reginput = locinput;
3202 if (ln && regrepeat(scan, ln) < ln)
3204 locinput = PL_reginput;
3207 char *e; /* Should not check after this */
3208 char *old = locinput;
3210 if (n == REG_INFTY) {
3213 while (UTF8_IS_CONTINUATION(*(U8*)e))
3219 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3223 e = locinput + n - ln;
3229 /* Find place 'next' could work */
3232 while (locinput <= e && *locinput != c1)
3235 while (locinput <= e
3240 count = locinput - old;
3247 utf8_to_uvchr((U8*)locinput, &len) != c1;
3252 for (count = 0; locinput <= e; count++) {
3253 UV c = utf8_to_uvchr((U8*)locinput, &len);
3254 if (c == c1 || c == c2)
3262 /* PL_reginput == old now */
3263 if (locinput != old) {
3264 ln = 1; /* Did some */
3265 if (regrepeat(scan, count) < count)
3268 /* PL_reginput == locinput now */
3269 TRYPAREN(paren, ln, locinput);
3270 PL_reginput = locinput; /* Could be reset... */
3271 REGCP_UNWIND(lastcp);
3272 /* Couldn't or didn't -- move forward. */
3275 locinput += UTF8SKIP(locinput);
3281 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3285 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3287 c = UCHARAT(PL_reginput);
3288 /* If it could work, try it. */
3289 if (c == c1 || c == c2)
3291 TRYPAREN(paren, n, PL_reginput);
3292 REGCP_UNWIND(lastcp);
3295 /* If it could work, try it. */
3296 else if (c1 == -1000)
3298 TRYPAREN(paren, n, PL_reginput);
3299 REGCP_UNWIND(lastcp);
3301 /* Couldn't or didn't -- move forward. */
3302 PL_reginput = locinput;
3303 if (regrepeat(scan, 1)) {
3305 locinput = PL_reginput;
3313 n = regrepeat(scan, n);
3314 locinput = PL_reginput;
3315 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3316 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3317 ln = n; /* why back off? */
3318 /* ...because $ and \Z can match before *and* after
3319 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3320 We should back off by one in this case. */
3321 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3330 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3332 c = UCHARAT(PL_reginput);
3334 /* If it could work, try it. */
3335 if (c1 == -1000 || c == c1 || c == c2)
3337 TRYPAREN(paren, n, PL_reginput);
3338 REGCP_UNWIND(lastcp);
3340 /* Couldn't or didn't -- back up. */
3342 PL_reginput = locinput = HOPc(locinput, -1);
3350 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3352 c = UCHARAT(PL_reginput);
3354 /* If it could work, try it. */
3355 if (c1 == -1000 || c == c1 || c == c2)
3357 TRYPAREN(paren, n, PL_reginput);
3358 REGCP_UNWIND(lastcp);
3360 /* Couldn't or didn't -- back up. */
3362 PL_reginput = locinput = HOPc(locinput, -1);
3369 if (PL_reg_call_cc) {
3370 re_cc_state *cur_call_cc = PL_reg_call_cc;
3371 CURCUR *cctmp = PL_regcc;
3372 regexp *re = PL_reg_re;
3373 CHECKPOINT cp, lastcp;
3375 cp = regcppush(0); /* Save *all* the positions. */
3377 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3379 PL_reginput = locinput; /* Make position available to
3381 cache_re(PL_reg_call_cc->re);
3382 PL_regcc = PL_reg_call_cc->cc;
3383 PL_reg_call_cc = PL_reg_call_cc->prev;
3384 if (regmatch(cur_call_cc->node)) {
3385 PL_reg_call_cc = cur_call_cc;
3389 REGCP_UNWIND(lastcp);
3391 PL_reg_call_cc = cur_call_cc;
3397 PerlIO_printf(Perl_debug_log,
3398 "%*s continuation failed...\n",
3399 REPORT_CODE_OFF+PL_regindent*2, "")
3403 if (locinput < PL_regtill) {
3404 DEBUG_r(PerlIO_printf(Perl_debug_log,
3405 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3407 (long)(locinput - PL_reg_starttry),
3408 (long)(PL_regtill - PL_reg_starttry),
3410 sayNO_FINAL; /* Cannot match: too short. */
3412 PL_reginput = locinput; /* put where regtry can find it */
3413 sayYES_FINAL; /* Success! */
3415 PL_reginput = locinput; /* put where regtry can find it */
3416 sayYES_LOUD; /* Success! */
3419 PL_reginput = locinput;
3424 s = HOPBACKc(locinput, scan->flags);
3430 PL_reginput = locinput;
3435 s = HOPBACKc(locinput, scan->flags);
3441 PL_reginput = locinput;
3444 inner = NEXTOPER(NEXTOPER(scan));
3445 if (regmatch(inner) != n) {
3460 if (OP(scan) == SUSPEND) {
3461 locinput = PL_reginput;
3462 nextchr = UCHARAT(locinput);
3467 next = scan + ARG(scan);
3472 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3473 PTR2UV(scan), OP(scan));
3474 Perl_croak(aTHX_ "regexp memory corruption");
3481 * We get here only if there's trouble -- normally "case END" is
3482 * the terminating point.
3484 Perl_croak(aTHX_ "corrupted regexp pointers");
3490 PerlIO_printf(Perl_debug_log,
3491 "%*s %scould match...%s\n",
3492 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3496 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3497 PL_colors[4],PL_colors[5]));
3503 #if 0 /* Breaks $^R */
3511 PerlIO_printf(Perl_debug_log,
3512 "%*s %sfailed...%s\n",
3513 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3519 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3522 case RE_UNWIND_BRANCH:
3523 case RE_UNWIND_BRANCHJ:
3525 re_unwind_branch_t *uwb = &(uw->branch);
3526 I32 lastparen = uwb->lastparen;
3528 REGCP_UNWIND(uwb->lastcp);
3529 for (n = *PL_reglastparen; n > lastparen; n--)
3531 *PL_reglastparen = n;
3532 scan = next = uwb->next;
3534 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3535 ? BRANCH : BRANCHJ) ) { /* Failure */
3542 /* Have more choice yet. Reuse the same uwb. */
3544 if ((n = (uwb->type == RE_UNWIND_BRANCH
3545 ? NEXT_OFF(next) : ARG(next))))
3548 next = NULL; /* XXXX Needn't unwinding in this case... */
3550 next = NEXTOPER(scan);
3551 if (uwb->type == RE_UNWIND_BRANCHJ)
3552 next = NEXTOPER(next);
3553 locinput = uwb->locinput;
3554 nextchr = uwb->nextchr;
3556 PL_regindent = uwb->regindent;
3563 Perl_croak(aTHX_ "regexp unwind memory corruption");
3574 - regrepeat - repeatedly match something simple, report how many
3577 * [This routine now assumes that it will only match on things of length 1.
3578 * That was true before, but now we assume scan - reginput is the count,
3579 * rather than incrementing count on every character. [Er, except utf8.]]
3582 S_regrepeat(pTHX_ regnode *p, I32 max)
3584 register char *scan;
3586 register char *loceol = PL_regeol;
3587 register I32 hardcount = 0;
3588 register bool do_utf8 = DO_UTF8(PL_reg_sv);
3591 if (max != REG_INFTY && max < loceol - scan)
3592 loceol = scan + max;
3597 while (scan < loceol && hardcount < max && *scan != '\n') {
3598 scan += UTF8SKIP(scan);
3602 while (scan < loceol && *scan != '\n')
3612 case EXACT: /* length of string is 1 */
3614 while (scan < loceol && UCHARAT(scan) == c)
3617 case EXACTF: /* length of string is 1 */
3619 while (scan < loceol &&
3620 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3623 case EXACTFL: /* length of string is 1 */
3624 PL_reg_flags |= RF_tainted;
3626 while (scan < loceol &&
3627 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3633 while (hardcount < max && scan < loceol &&
3634 reginclass(p, (U8*)scan, do_utf8)) {
3635 scan += UTF8SKIP(scan);
3639 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3646 LOAD_UTF8_CHARCLASS(alnum,"a");
3647 while (hardcount < max && scan < loceol &&
3648 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3649 scan += UTF8SKIP(scan);
3653 while (scan < loceol && isALNUM(*scan))
3658 PL_reg_flags |= RF_tainted;
3661 while (hardcount < max && scan < loceol &&
3662 isALNUM_LC_utf8((U8*)scan)) {
3663 scan += UTF8SKIP(scan);
3667 while (scan < loceol && isALNUM_LC(*scan))
3674 LOAD_UTF8_CHARCLASS(alnum,"a");
3675 while (hardcount < max && scan < loceol &&
3676 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3677 scan += UTF8SKIP(scan);
3681 while (scan < loceol && !isALNUM(*scan))
3686 PL_reg_flags |= RF_tainted;
3689 while (hardcount < max && scan < loceol &&
3690 !isALNUM_LC_utf8((U8*)scan)) {
3691 scan += UTF8SKIP(scan);
3695 while (scan < loceol && !isALNUM_LC(*scan))
3702 LOAD_UTF8_CHARCLASS(space," ");
3703 while (hardcount < max && scan < loceol &&
3705 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3706 scan += UTF8SKIP(scan);
3710 while (scan < loceol && isSPACE(*scan))
3715 PL_reg_flags |= RF_tainted;
3718 while (hardcount < max && scan < loceol &&
3719 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3720 scan += UTF8SKIP(scan);
3724 while (scan < loceol && isSPACE_LC(*scan))
3731 LOAD_UTF8_CHARCLASS(space," ");
3732 while (hardcount < max && scan < loceol &&
3734 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3735 scan += UTF8SKIP(scan);
3739 while (scan < loceol && !isSPACE(*scan))
3744 PL_reg_flags |= RF_tainted;
3747 while (hardcount < max && scan < loceol &&
3748 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3749 scan += UTF8SKIP(scan);
3753 while (scan < loceol && !isSPACE_LC(*scan))
3760 LOAD_UTF8_CHARCLASS(digit,"0");
3761 while (hardcount < max && scan < loceol &&
3762 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3763 scan += UTF8SKIP(scan);
3767 while (scan < loceol && isDIGIT(*scan))
3774 LOAD_UTF8_CHARCLASS(digit,"0");
3775 while (hardcount < max && scan < loceol &&
3776 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3777 scan += UTF8SKIP(scan);
3781 while (scan < loceol && !isDIGIT(*scan))
3785 default: /* Called on something of 0 width. */
3786 break; /* So match right here or not at all. */
3792 c = scan - PL_reginput;
3797 SV *prop = sv_newmortal();
3800 PerlIO_printf(Perl_debug_log,
3801 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3802 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3809 - regrepeat_hard - repeatedly match something, report total lenth and length
3811 * The repeater is supposed to have constant length.
3815 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3817 register char *scan = Nullch;
3818 register char *start;
3819 register char *loceol = PL_regeol;
3821 I32 count = 0, res = 1;
3826 start = PL_reginput;
3827 if (DO_UTF8(PL_reg_sv)) {
3828 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3831 while (start < PL_reginput) {
3833 start += UTF8SKIP(start);
3844 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3846 *lp = l = PL_reginput - start;
3847 if (max != REG_INFTY && l*max < loceol - scan)
3848 loceol = scan + l*max;
3861 - regclass_swash - prepare the utf8 swash
3865 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
3870 if (PL_regdata && PL_regdata->count) {
3873 if (PL_regdata->what[n] == 's') {
3874 SV *rv = (SV*)PL_regdata->data[n];
3875 AV *av = (AV*)SvRV((SV*)rv);
3878 si = *av_fetch(av, 0, FALSE);
3879 a = av_fetch(av, 1, FALSE);
3883 else if (si && doinit) {
3884 sw = swash_init("utf8", "", si, 1, 0);
3885 (void)av_store(av, 1, sw);
3897 - reginclass - determine if a character falls into a character class
3901 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
3903 char flags = ANYOF_FLAGS(n);
3908 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
3910 if (do_utf8 || (flags & ANYOF_UNICODE)) {
3911 if (do_utf8 && !ANYOF_RUNTIME(n)) {
3912 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
3915 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
3918 SV *sw = regclass_swash(n, TRUE, 0);
3921 if (swash_fetch(sw, p, do_utf8))
3923 else if (flags & ANYOF_FOLD) {
3924 U8 tmpbuf[UTF8_MAXLEN+1];
3926 if (flags & ANYOF_LOCALE) {
3927 PL_reg_flags |= RF_tainted;
3928 uvchr_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3931 uvchr_to_utf8(tmpbuf, toLOWER_utf8(p));
3932 if (swash_fetch(sw, tmpbuf, do_utf8))
3938 if (!match && c < 256) {
3939 if (ANYOF_BITMAP_TEST(n, c))
3941 else if (flags & ANYOF_FOLD) {
3944 if (flags & ANYOF_LOCALE) {
3945 PL_reg_flags |= RF_tainted;
3946 f = PL_fold_locale[c];
3950 if (f != c && ANYOF_BITMAP_TEST(n, f))
3954 if (!match && (flags & ANYOF_CLASS)) {
3955 PL_reg_flags |= RF_tainted;
3957 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3958 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3959 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
3960 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3961 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3962 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3963 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3964 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3965 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3966 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3967 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
3968 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
3969 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3970 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3971 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3972 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3973 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
3974 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3975 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
3976 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3977 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3978 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3979 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
3980 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3981 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3982 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
3983 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
3984 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
3985 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
3986 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
3987 ) /* How's that for a conditional? */
3994 return (flags & ANYOF_INVERT) ? !match : match;
3998 S_reghop(pTHX_ U8 *s, I32 off)
4000 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4004 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4007 while (off-- && s < lim) {
4008 /* XXX could check well-formedness here */
4016 if (UTF8_IS_CONTINUED(*s)) {
4017 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4020 /* XXX could check well-formedness here */
4028 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4030 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4034 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4037 while (off-- && s < lim) {
4038 /* XXX could check well-formedness here */
4048 if (UTF8_IS_CONTINUED(*s)) {
4049 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4052 /* XXX could check well-formedness here */
4068 restore_pos(pTHXo_ void *arg)
4070 if (PL_reg_eval_set) {
4071 if (PL_reg_oldsaved) {
4072 PL_reg_re->subbeg = PL_reg_oldsaved;
4073 PL_reg_re->sublen = PL_reg_oldsavedlen;
4074 RX_MATCH_COPIED_on(PL_reg_re);
4076 PL_reg_magic->mg_len = PL_reg_oldpos;
4077 PL_reg_eval_set = 0;
4078 PL_curpm = PL_reg_oldcurpm;