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 5
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 SSPUSHPTR(PL_reginput);
163 #define REGCP_FRAME_ELEMS 2
164 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
165 * are needed for the regexp context stack bookkeeping. */
166 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
167 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
172 /* These are needed since we do not localize EVAL nodes: */
173 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
174 " Setting an EVAL scope, savestack=%"IVdf"\n", \
175 (IV)PL_savestack_ix)); cp = PL_savestack_ix
177 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
178 PerlIO_printf(Perl_debug_log, \
179 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
180 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
190 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
192 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
193 i = SSPOPINT; /* Parentheses elements to pop. */
194 input = (char *) SSPOPPTR;
195 *PL_reglastparen = SSPOPINT;
196 PL_regsize = SSPOPINT;
198 /* Now restore the parentheses context. */
199 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
200 i > 0; i -= REGCP_PAREN_ELEMS) {
201 paren = (U32)SSPOPINT;
202 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
203 PL_regstartp[paren] = SSPOPINT;
205 if (paren <= *PL_reglastparen)
206 PL_regendp[paren] = tmps;
208 PerlIO_printf(Perl_debug_log,
209 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
210 (UV)paren, (IV)PL_regstartp[paren],
211 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
212 (IV)PL_regendp[paren],
213 (paren > *PL_reglastparen ? "(no)" : ""));
217 if (*PL_reglastparen + 1 <= PL_regnpar) {
218 PerlIO_printf(Perl_debug_log,
219 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
220 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
224 /* It would seem that the similar code in regtry()
225 * already takes care of this, and in fact it is in
226 * a better location to since this code can #if 0-ed out
227 * but the code in regtry() is needed or otherwise tests
228 * requiring null fields (pat.t#187 and split.t#{13,14}
229 * (as of patchlevel 7877) will fail. Then again,
230 * this code seems to be necessary or otherwise
231 * building DynaLoader will fail:
232 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
234 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
235 if (paren > PL_regsize)
236 PL_regstartp[paren] = -1;
237 PL_regendp[paren] = -1;
244 S_regcp_set_to(pTHX_ I32 ss)
246 I32 tmp = PL_savestack_ix;
248 PL_savestack_ix = ss;
250 PL_savestack_ix = tmp;
254 typedef struct re_cc_state
258 struct re_cc_state *prev;
263 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
265 #define TRYPAREN(paren, n, input) { \
268 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
269 PL_regendp[paren] = input - PL_bostr; \
272 PL_regendp[paren] = -1; \
274 if (regmatch(next)) \
277 PL_regendp[paren] = -1; \
282 * pregexec and friends
286 - pregexec - match a regexp against a string
289 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
290 char *strbeg, I32 minend, SV *screamer, U32 nosave)
291 /* strend: pointer to null at end of string */
292 /* strbeg: real beginning of string */
293 /* minend: end of match must be >=minend after stringarg. */
294 /* nosave: For optimizations. */
297 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
298 nosave ? 0 : REXEC_COPY_STR);
302 S_cache_re(pTHX_ regexp *prog)
304 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
306 PL_regprogram = prog->program;
308 PL_regnpar = prog->nparens;
309 PL_regdata = prog->data;
314 * Need to implement the following flags for reg_anch:
316 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
318 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
319 * INTUIT_AUTORITATIVE_ML
320 * INTUIT_ONCE_NOML - Intuit can match in one location only.
323 * Another flag for this function: SECOND_TIME (so that float substrs
324 * with giant delta may be not rechecked).
327 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
329 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
330 Otherwise, only SvCUR(sv) is used to get strbeg. */
332 /* XXXX We assume that strpos is strbeg unless sv. */
334 /* XXXX Some places assume that there is a fixed substring.
335 An update may be needed if optimizer marks as "INTUITable"
336 RExen without fixed substrings. Similarly, it is assumed that
337 lengths of all the strings are no more than minlen, thus they
338 cannot come from lookahead.
339 (Or minlen should take into account lookahead.) */
341 /* A failure to find a constant substring means that there is no need to make
342 an expensive call to REx engine, thus we celebrate a failure. Similarly,
343 finding a substring too deep into the string means that less calls to
344 regtry() should be needed.
346 REx compiler's optimizer found 4 possible hints:
347 a) Anchored substring;
349 c) Whether we are anchored (beginning-of-line or \G);
350 d) First node (of those at offset 0) which may distingush positions;
351 We use a)b)d) and multiline-part of c), and try to find a position in the
352 string which does not contradict any of them.
355 /* Most of decisions we do here should have been done at compile time.
356 The nodes of the REx which we used for the search should have been
357 deleted from the finite automaton. */
360 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
361 char *strend, U32 flags, re_scream_pos_data *data)
363 register I32 start_shift;
364 /* Should be nonnegative! */
365 register I32 end_shift;
372 register char *other_last = Nullch; /* other substr checked before this */
373 char *check_at; /* check substr found at this pos */
375 char *i_strpos = strpos;
378 DEBUG_r( if (!PL_colorset) reginitcolors() );
379 DEBUG_r(PerlIO_printf(Perl_debug_log,
380 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
381 PL_colors[4],PL_colors[5],PL_colors[0],
384 (strlen(prog->precomp) > 60 ? "..." : ""),
386 (int)(strend - strpos > 60 ? 60 : strend - strpos),
387 strpos, PL_colors[1],
388 (strend - strpos > 60 ? "..." : ""))
391 if (prog->reganch & ROPT_UTF8)
392 PL_reg_flags |= RF_utf8;
394 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
395 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
398 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
400 check = prog->check_substr;
401 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
402 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
403 || ( (prog->reganch & ROPT_ANCH_BOL)
404 && !PL_multiline ) ); /* Check after \n? */
407 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
408 | ROPT_IMPLICIT)) /* not a real BOL */
409 /* SvCUR is not set on references: SvRV and SvPVX overlap */
411 && (strpos != strbeg)) {
412 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
415 if (prog->check_offset_min == prog->check_offset_max &&
416 !(prog->reganch & ROPT_SANY_SEEN)) {
417 /* Substring at constant offset from beg-of-str... */
420 s = HOP3c(strpos, prog->check_offset_min, strend);
422 slen = SvCUR(check); /* >= 1 */
424 if ( strend - s > slen || strend - s < slen - 1
425 || (strend - s == slen && strend[-1] != '\n')) {
426 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
429 /* Now should match s[0..slen-2] */
431 if (slen && (*SvPVX(check) != *s
433 && memNE(SvPVX(check), s, slen)))) {
435 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
439 else if (*SvPVX(check) != *s
440 || ((slen = SvCUR(check)) > 1
441 && memNE(SvPVX(check), s, slen)))
443 goto success_at_start;
446 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
448 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
449 end_shift = prog->minlen - start_shift -
450 CHR_SVLEN(check) + (SvTAIL(check) != 0);
452 I32 end = prog->check_offset_max + CHR_SVLEN(check)
453 - (SvTAIL(check) != 0);
454 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
456 if (end_shift < eshift)
460 else { /* Can match at random position */
463 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
464 /* Should be nonnegative! */
465 end_shift = prog->minlen - start_shift -
466 CHR_SVLEN(check) + (SvTAIL(check) != 0);
469 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
471 Perl_croak(aTHX_ "panic: end_shift");
475 /* Find a possible match in the region s..strend by looking for
476 the "check" substring in the region corrected by start/end_shift. */
477 if (flags & REXEC_SCREAM) {
478 I32 p = -1; /* Internal iterator of scream. */
479 I32 *pp = data ? data->scream_pos : &p;
481 if (PL_screamfirst[BmRARE(check)] >= 0
482 || ( BmRARE(check) == '\n'
483 && (BmPREVIOUS(check) == SvCUR(check) - 1)
485 s = screaminstr(sv, check,
486 start_shift + (s - strbeg), end_shift, pp, 0);
490 *data->scream_olds = s;
492 else if (prog->reganch & ROPT_SANY_SEEN)
493 s = fbm_instr((U8*)(s + start_shift),
494 (U8*)(strend - end_shift),
495 check, PL_multiline ? FBMrf_MULTILINE : 0);
497 s = fbm_instr(HOP3(s, start_shift, strend),
498 HOP3(strend, -end_shift, strbeg),
499 check, PL_multiline ? FBMrf_MULTILINE : 0);
501 /* Update the count-of-usability, remove useless subpatterns,
504 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
505 (s ? "Found" : "Did not find"),
506 ((check == prog->anchored_substr) ? "anchored" : "floating"),
508 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
510 PL_colors[1], (SvTAIL(check) ? "$" : ""),
511 (s ? " at offset " : "...\n") ) );
518 /* Finish the diagnostic message */
519 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
521 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
522 Start with the other substr.
523 XXXX no SCREAM optimization yet - and a very coarse implementation
524 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
525 *always* match. Probably should be marked during compile...
526 Probably it is right to do no SCREAM here...
529 if (prog->float_substr && prog->anchored_substr) {
530 /* Take into account the "other" substring. */
531 /* XXXX May be hopelessly wrong for UTF... */
534 if (check == prog->float_substr) {
537 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
540 t = s - prog->check_offset_max;
541 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
542 && (!(prog->reganch & ROPT_UTF8)
543 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
548 t = HOP3c(t, prog->anchored_offset, strend);
549 if (t < other_last) /* These positions already checked */
551 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
554 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
555 /* On end-of-str: see comment below. */
556 s = fbm_instr((unsigned char*)t,
557 HOP3(HOP3(last1, prog->anchored_offset, strend)
558 + SvCUR(prog->anchored_substr),
559 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
560 prog->anchored_substr,
561 PL_multiline ? FBMrf_MULTILINE : 0);
562 DEBUG_r(PerlIO_printf(Perl_debug_log,
563 "%s anchored substr `%s%.*s%s'%s",
564 (s ? "Found" : "Contradicts"),
566 (int)(SvCUR(prog->anchored_substr)
567 - (SvTAIL(prog->anchored_substr)!=0)),
568 SvPVX(prog->anchored_substr),
569 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
571 if (last1 >= last2) {
572 DEBUG_r(PerlIO_printf(Perl_debug_log,
573 ", giving up...\n"));
576 DEBUG_r(PerlIO_printf(Perl_debug_log,
577 ", trying floating at offset %ld...\n",
578 (long)(HOP3c(s1, 1, strend) - i_strpos)));
579 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
580 s = HOP3c(last, 1, strend);
584 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
585 (long)(s - i_strpos)));
586 t = HOP3c(s, -prog->anchored_offset, strbeg);
587 other_last = HOP3c(s, 1, strend);
595 else { /* Take into account the floating substring. */
599 t = HOP3c(s, -start_shift, strbeg);
601 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
602 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
603 last = HOP3c(t, prog->float_max_offset, strend);
604 s = HOP3c(t, prog->float_min_offset, strend);
607 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
608 /* fbm_instr() takes into account exact value of end-of-str
609 if the check is SvTAIL(ed). Since false positives are OK,
610 and end-of-str is not later than strend we are OK. */
611 s = fbm_instr((unsigned char*)s,
612 (unsigned char*)last + SvCUR(prog->float_substr)
613 - (SvTAIL(prog->float_substr)!=0),
614 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
615 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
616 (s ? "Found" : "Contradicts"),
618 (int)(SvCUR(prog->float_substr)
619 - (SvTAIL(prog->float_substr)!=0)),
620 SvPVX(prog->float_substr),
621 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
624 DEBUG_r(PerlIO_printf(Perl_debug_log,
625 ", giving up...\n"));
628 DEBUG_r(PerlIO_printf(Perl_debug_log,
629 ", trying anchored starting at offset %ld...\n",
630 (long)(s1 + 1 - i_strpos)));
632 s = HOP3c(t, 1, strend);
636 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
637 (long)(s - i_strpos)));
638 other_last = s; /* Fix this later. --Hugo */
647 t = s - prog->check_offset_max;
648 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
649 && (!(prog->reganch & ROPT_UTF8)
650 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
652 /* Fixed substring is found far enough so that the match
653 cannot start at strpos. */
655 if (ml_anch && t[-1] != '\n') {
656 /* Eventually fbm_*() should handle this, but often
657 anchored_offset is not 0, so this check will not be wasted. */
658 /* XXXX In the code below we prefer to look for "^" even in
659 presence of anchored substrings. And we search even
660 beyond the found float position. These pessimizations
661 are historical artefacts only. */
663 while (t < strend - prog->minlen) {
665 if (t < check_at - prog->check_offset_min) {
666 if (prog->anchored_substr) {
667 /* Since we moved from the found position,
668 we definitely contradict the found anchored
669 substr. Due to the above check we do not
670 contradict "check" substr.
671 Thus we can arrive here only if check substr
672 is float. Redo checking for "other"=="fixed".
675 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
676 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
677 goto do_other_anchored;
679 /* We don't contradict the found floating substring. */
680 /* XXXX Why not check for STCLASS? */
682 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
683 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
686 /* Position contradicts check-string */
687 /* XXXX probably better to look for check-string
688 than for "\n", so one should lower the limit for t? */
689 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
690 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
691 other_last = strpos = s = t + 1;
696 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
697 PL_colors[0],PL_colors[1]));
701 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
702 PL_colors[0],PL_colors[1]));
706 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
709 /* The found string does not prohibit matching at strpos,
710 - no optimization of calling REx engine can be performed,
711 unless it was an MBOL and we are not after MBOL,
712 or a future STCLASS check will fail this. */
714 /* Even in this situation we may use MBOL flag if strpos is offset
715 wrt the start of the string. */
716 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
717 && (strpos != strbeg) && strpos[-1] != '\n'
718 /* May be due to an implicit anchor of m{.*foo} */
719 && !(prog->reganch & ROPT_IMPLICIT))
724 DEBUG_r( if (ml_anch)
725 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
726 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
729 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
730 && prog->check_substr /* Could be deleted already */
731 && --BmUSEFUL(prog->check_substr) < 0
732 && prog->check_substr == prog->float_substr)
734 /* If flags & SOMETHING - do not do it many times on the same match */
735 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
736 SvREFCNT_dec(prog->check_substr);
737 prog->check_substr = Nullsv; /* disable */
738 prog->float_substr = Nullsv; /* clear */
739 check = Nullsv; /* abort */
741 /* XXXX This is a remnant of the old implementation. It
742 looks wasteful, since now INTUIT can use many
744 prog->reganch &= ~RE_USE_INTUIT;
751 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
752 if (prog->regstclass) {
753 /* minlen == 0 is possible if regstclass is \b or \B,
754 and the fixed substr is ''$.
755 Since minlen is already taken into account, s+1 is before strend;
756 accidentally, minlen >= 1 guaranties no false positives at s + 1
757 even for \b or \B. But (minlen? 1 : 0) below assumes that
758 regstclass does not come from lookahead... */
759 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
760 This leaves EXACTF only, which is dealt with in find_byclass(). */
761 U8* str = (U8*)STRING(prog->regstclass);
762 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
763 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
765 char *endpos = (prog->anchored_substr || ml_anch)
766 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
767 : (prog->float_substr
768 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
771 char *startpos = strbeg;
774 if (prog->reganch & ROPT_UTF8) {
775 PL_regdata = prog->data;
778 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
783 if (endpos == strend) {
784 DEBUG_r( PerlIO_printf(Perl_debug_log,
785 "Could not match STCLASS...\n") );
788 DEBUG_r( PerlIO_printf(Perl_debug_log,
789 "This position contradicts STCLASS...\n") );
790 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
792 /* Contradict one of substrings */
793 if (prog->anchored_substr) {
794 if (prog->anchored_substr == check) {
795 DEBUG_r( what = "anchored" );
797 s = HOP3c(t, 1, strend);
798 if (s + start_shift + end_shift > strend) {
799 /* XXXX Should be taken into account earlier? */
800 DEBUG_r( PerlIO_printf(Perl_debug_log,
801 "Could not match STCLASS...\n") );
806 DEBUG_r( PerlIO_printf(Perl_debug_log,
807 "Looking for %s substr starting at offset %ld...\n",
808 what, (long)(s + start_shift - i_strpos)) );
811 /* Have both, check_string is floating */
812 if (t + start_shift >= check_at) /* Contradicts floating=check */
813 goto retry_floating_check;
814 /* Recheck anchored substring, but not floating... */
818 DEBUG_r( PerlIO_printf(Perl_debug_log,
819 "Looking for anchored substr starting at offset %ld...\n",
820 (long)(other_last - i_strpos)) );
821 goto do_other_anchored;
823 /* Another way we could have checked stclass at the
824 current position only: */
829 DEBUG_r( PerlIO_printf(Perl_debug_log,
830 "Looking for /%s^%s/m starting at offset %ld...\n",
831 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
834 if (!prog->float_substr) /* Could have been deleted */
836 /* Check is floating subtring. */
837 retry_floating_check:
838 t = check_at - start_shift;
839 DEBUG_r( what = "floating" );
840 goto hop_and_restart;
843 PerlIO_printf(Perl_debug_log,
844 "By STCLASS: moving %ld --> %ld\n",
845 (long)(t - i_strpos), (long)(s - i_strpos));
847 PerlIO_printf(Perl_debug_log,
848 "Does not contradict STCLASS...\n") );
851 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
852 PL_colors[4], (check ? "Guessed" : "Giving up"),
853 PL_colors[5], (long)(s - i_strpos)) );
856 fail_finish: /* Substring not found */
857 if (prog->check_substr) /* could be removed already */
858 BmUSEFUL(prog->check_substr) += 5; /* hooray */
860 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
861 PL_colors[4],PL_colors[5]));
865 /* We know what class REx starts with. Try to find this position... */
867 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
869 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
875 register I32 tmp = 1; /* Scratch variable? */
876 register bool do_utf8 = DO_UTF8(PL_reg_sv);
878 /* We know what class it must start with. */
882 if (reginclass(c, (U8*)s, do_utf8)) {
883 if (tmp && (norun || regtry(prog, s)))
890 s += do_utf8 ? UTF8SKIP(s) : 1;
897 c1 = to_utf8_lower((U8*)m);
898 c2 = to_utf8_upper((U8*)m);
909 c2 = PL_fold_locale[c1];
914 e = s; /* Due to minlen logic of intuit() */
920 if ( utf8_to_uvchr((U8*)s, &len) == c1
927 UV c = utf8_to_uvchr((U8*)s, &len);
928 if ( (c == c1 || c == c2) && regtry(prog, s) )
937 && (ln == 1 || !(OP(c) == EXACTF
939 : ibcmp_locale(s, m, ln)))
940 && (norun || regtry(prog, s)) )
946 if ( (*(U8*)s == c1 || *(U8*)s == c2)
947 && (ln == 1 || !(OP(c) == EXACTF
949 : ibcmp_locale(s, m, ln)))
950 && (norun || regtry(prog, s)) )
957 PL_reg_flags |= RF_tainted;
964 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
966 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
968 tmp = ((OP(c) == BOUND ?
969 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
970 LOAD_UTF8_CHARCLASS(alnum,"a");
972 if (tmp == !(OP(c) == BOUND ?
973 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
974 isALNUM_LC_utf8((U8*)s)))
977 if ((norun || regtry(prog, s)))
984 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
985 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
988 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
990 if ((norun || regtry(prog, s)))
996 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1000 PL_reg_flags |= RF_tainted;
1007 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1009 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1011 tmp = ((OP(c) == NBOUND ?
1012 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1013 LOAD_UTF8_CHARCLASS(alnum,"a");
1014 while (s < strend) {
1015 if (tmp == !(OP(c) == NBOUND ?
1016 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1017 isALNUM_LC_utf8((U8*)s)))
1019 else if ((norun || regtry(prog, s)))
1025 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1026 tmp = ((OP(c) == NBOUND ?
1027 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1028 while (s < strend) {
1030 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1032 else if ((norun || regtry(prog, s)))
1037 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1042 LOAD_UTF8_CHARCLASS(alnum,"a");
1043 while (s < strend) {
1044 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1045 if (tmp && (norun || regtry(prog, s)))
1056 while (s < strend) {
1058 if (tmp && (norun || regtry(prog, s)))
1070 PL_reg_flags |= RF_tainted;
1072 while (s < strend) {
1073 if (isALNUM_LC_utf8((U8*)s)) {
1074 if (tmp && (norun || regtry(prog, s)))
1085 while (s < strend) {
1086 if (isALNUM_LC(*s)) {
1087 if (tmp && (norun || regtry(prog, s)))
1100 LOAD_UTF8_CHARCLASS(alnum,"a");
1101 while (s < strend) {
1102 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1103 if (tmp && (norun || regtry(prog, s)))
1114 while (s < strend) {
1116 if (tmp && (norun || regtry(prog, s)))
1128 PL_reg_flags |= RF_tainted;
1130 while (s < strend) {
1131 if (!isALNUM_LC_utf8((U8*)s)) {
1132 if (tmp && (norun || regtry(prog, s)))
1143 while (s < strend) {
1144 if (!isALNUM_LC(*s)) {
1145 if (tmp && (norun || regtry(prog, s)))
1158 LOAD_UTF8_CHARCLASS(space," ");
1159 while (s < strend) {
1160 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1161 if (tmp && (norun || regtry(prog, s)))
1172 while (s < strend) {
1174 if (tmp && (norun || regtry(prog, s)))
1186 PL_reg_flags |= RF_tainted;
1188 while (s < strend) {
1189 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1190 if (tmp && (norun || regtry(prog, s)))
1201 while (s < strend) {
1202 if (isSPACE_LC(*s)) {
1203 if (tmp && (norun || regtry(prog, s)))
1216 LOAD_UTF8_CHARCLASS(space," ");
1217 while (s < strend) {
1218 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1219 if (tmp && (norun || regtry(prog, s)))
1230 while (s < strend) {
1232 if (tmp && (norun || regtry(prog, s)))
1244 PL_reg_flags |= RF_tainted;
1246 while (s < strend) {
1247 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1248 if (tmp && (norun || regtry(prog, s)))
1259 while (s < strend) {
1260 if (!isSPACE_LC(*s)) {
1261 if (tmp && (norun || regtry(prog, s)))
1274 LOAD_UTF8_CHARCLASS(digit,"0");
1275 while (s < strend) {
1276 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1277 if (tmp && (norun || regtry(prog, s)))
1288 while (s < strend) {
1290 if (tmp && (norun || regtry(prog, s)))
1302 PL_reg_flags |= RF_tainted;
1304 while (s < strend) {
1305 if (isDIGIT_LC_utf8((U8*)s)) {
1306 if (tmp && (norun || regtry(prog, s)))
1317 while (s < strend) {
1318 if (isDIGIT_LC(*s)) {
1319 if (tmp && (norun || regtry(prog, s)))
1332 LOAD_UTF8_CHARCLASS(digit,"0");
1333 while (s < strend) {
1334 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1335 if (tmp && (norun || regtry(prog, s)))
1346 while (s < strend) {
1348 if (tmp && (norun || regtry(prog, s)))
1360 PL_reg_flags |= RF_tainted;
1362 while (s < strend) {
1363 if (!isDIGIT_LC_utf8((U8*)s)) {
1364 if (tmp && (norun || regtry(prog, s)))
1375 while (s < strend) {
1376 if (!isDIGIT_LC(*s)) {
1377 if (tmp && (norun || regtry(prog, s)))
1389 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1398 - regexec_flags - match a regexp against a string
1401 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1402 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1403 /* strend: pointer to null at end of string */
1404 /* strbeg: real beginning of string */
1405 /* minend: end of match must be >=minend after stringarg. */
1406 /* data: May be used for some additional optimizations. */
1407 /* nosave: For optimizations. */
1410 register regnode *c;
1411 register char *startpos = stringarg;
1412 I32 minlen; /* must match at least this many chars */
1413 I32 dontbother = 0; /* how many characters not to try at end */
1414 /* I32 start_shift = 0; */ /* Offset of the start to find
1415 constant substr. */ /* CC */
1416 I32 end_shift = 0; /* Same for the end. */ /* CC */
1417 I32 scream_pos = -1; /* Internal iterator of scream. */
1419 SV* oreplsv = GvSV(PL_replgv);
1420 bool do_utf8 = DO_UTF8(sv);
1426 PL_regnarrate = DEBUG_r_TEST;
1429 /* Be paranoid... */
1430 if (prog == NULL || startpos == NULL) {
1431 Perl_croak(aTHX_ "NULL regexp parameter");
1435 minlen = prog->minlen;
1437 if (!(prog->reganch & ROPT_SANY_SEEN))
1438 if (utf8_distance((U8*)strend, (U8*)startpos) < minlen) goto phooey;
1441 if (strend - startpos < minlen) goto phooey;
1444 /* Check validity of program. */
1445 if (UCHARAT(prog->program) != REG_MAGIC) {
1446 Perl_croak(aTHX_ "corrupted regexp program");
1450 PL_reg_eval_set = 0;
1453 if (prog->reganch & ROPT_UTF8)
1454 PL_reg_flags |= RF_utf8;
1456 /* Mark beginning of line for ^ and lookbehind. */
1457 PL_regbol = startpos;
1461 /* Mark end of line for $ (and such) */
1464 /* see how far we have to get to not match where we matched before */
1465 PL_regtill = startpos+minend;
1467 /* We start without call_cc context. */
1470 /* If there is a "must appear" string, look for it. */
1473 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1476 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1477 PL_reg_ganch = startpos;
1478 else if (sv && SvTYPE(sv) >= SVt_PVMG
1480 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1481 && mg->mg_len >= 0) {
1482 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1483 if (prog->reganch & ROPT_ANCH_GPOS) {
1484 if (s > PL_reg_ganch)
1489 else /* pos() not defined */
1490 PL_reg_ganch = strbeg;
1493 if (do_utf8 == (UTF!=0) &&
1494 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1495 re_scream_pos_data d;
1497 d.scream_olds = &scream_olds;
1498 d.scream_pos = &scream_pos;
1499 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1501 goto phooey; /* not present */
1504 DEBUG_r( if (!PL_colorset) reginitcolors() );
1505 DEBUG_r(PerlIO_printf(Perl_debug_log,
1506 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1507 PL_colors[4],PL_colors[5],PL_colors[0],
1510 (strlen(prog->precomp) > 60 ? "..." : ""),
1512 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1513 startpos, PL_colors[1],
1514 (strend - startpos > 60 ? "..." : ""))
1517 /* Simplest case: anchored match need be tried only once. */
1518 /* [unless only anchor is BOL and multiline is set] */
1519 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1520 if (s == startpos && regtry(prog, startpos))
1522 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1523 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1528 dontbother = minlen - 1;
1529 end = HOP3c(strend, -dontbother, strbeg) - 1;
1530 /* for multiline we only have to try after newlines */
1531 if (prog->check_substr) {
1535 if (regtry(prog, s))
1540 if (prog->reganch & RE_USE_INTUIT) {
1541 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1552 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1553 if (regtry(prog, s))
1560 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1561 if (regtry(prog, PL_reg_ganch))
1566 /* Messy cases: unanchored match. */
1567 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1568 /* we have /x+whatever/ */
1569 /* it must be a one character string (XXXX Except UTF?) */
1570 char ch = SvPVX(prog->anchored_substr)[0];
1576 while (s < strend) {
1578 DEBUG_r( did_match = 1 );
1579 if (regtry(prog, s)) goto got_it;
1581 while (s < strend && *s == ch)
1588 while (s < strend) {
1590 DEBUG_r( did_match = 1 );
1591 if (regtry(prog, s)) goto got_it;
1593 while (s < strend && *s == ch)
1599 DEBUG_r(did_match ||
1600 PerlIO_printf(Perl_debug_log,
1601 "Did not find anchored character...\n"));
1604 else if (do_utf8 == (UTF!=0) &&
1605 (prog->anchored_substr != Nullsv
1606 || (prog->float_substr != Nullsv
1607 && prog->float_max_offset < strend - s))) {
1608 SV *must = prog->anchored_substr
1609 ? prog->anchored_substr : prog->float_substr;
1611 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1613 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1614 char *last = HOP3c(strend, /* Cannot start after this */
1615 -(I32)(CHR_SVLEN(must)
1616 - (SvTAIL(must) != 0) + back_min), strbeg);
1617 char *last1; /* Last position checked before */
1623 last1 = HOPc(s, -1);
1625 last1 = s - 1; /* bogus */
1627 /* XXXX check_substr already used to find `s', can optimize if
1628 check_substr==must. */
1630 dontbother = end_shift;
1631 strend = HOPc(strend, -dontbother);
1632 while ( (s <= last) &&
1633 ((flags & REXEC_SCREAM)
1634 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1635 end_shift, &scream_pos, 0))
1636 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1637 (unsigned char*)strend, must,
1638 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1639 DEBUG_r( did_match = 1 );
1640 if (HOPc(s, -back_max) > last1) {
1641 last1 = HOPc(s, -back_min);
1642 s = HOPc(s, -back_max);
1645 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1647 last1 = HOPc(s, -back_min);
1651 while (s <= last1) {
1652 if (regtry(prog, s))
1658 while (s <= last1) {
1659 if (regtry(prog, s))
1665 DEBUG_r(did_match ||
1666 PerlIO_printf(Perl_debug_log, "Did not find %s substr `%s%.*s%s'%s...\n",
1667 ((must == prog->anchored_substr)
1668 ? "anchored" : "floating"),
1670 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1672 PL_colors[1], (SvTAIL(must) ? "$" : "")));
1675 else if ((c = prog->regstclass)) {
1676 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1677 /* don't bother with what can't match */
1678 strend = HOPc(strend, -(minlen - 1));
1680 SV *prop = sv_newmortal();
1682 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), s);
1684 if (find_byclass(prog, c, s, strend, startpos, 0))
1686 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1690 if (prog->float_substr != Nullsv) { /* Trim the end. */
1693 if (flags & REXEC_SCREAM) {
1694 last = screaminstr(sv, prog->float_substr, s - strbeg,
1695 end_shift, &scream_pos, 1); /* last one */
1697 last = scream_olds; /* Only one occurrence. */
1701 char *little = SvPV(prog->float_substr, len);
1703 if (SvTAIL(prog->float_substr)) {
1704 if (memEQ(strend - len + 1, little, len - 1))
1705 last = strend - len + 1;
1706 else if (!PL_multiline)
1707 last = memEQ(strend - len, little, len)
1708 ? strend - len : Nullch;
1714 last = rninstr(s, strend, little, little + len);
1716 last = strend; /* matching `$' */
1720 DEBUG_r(PerlIO_printf(Perl_debug_log,
1721 "%sCan't trim the tail, match fails (should not happen)%s\n",
1722 PL_colors[4],PL_colors[5]));
1723 goto phooey; /* Should not happen! */
1725 dontbother = strend - last + prog->float_min_offset;
1727 if (minlen && (dontbother < minlen))
1728 dontbother = minlen - 1;
1729 strend -= dontbother; /* this one's always in bytes! */
1730 /* We don't know much -- general case. */
1733 if (regtry(prog, s))
1742 if (regtry(prog, s))
1744 } while (s++ < strend);
1752 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1754 if (PL_reg_eval_set) {
1755 /* Preserve the current value of $^R */
1756 if (oreplsv != GvSV(PL_replgv))
1757 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1758 restored, the value remains
1760 restore_pos(aTHXo_ 0);
1763 /* make sure $`, $&, $', and $digit will work later */
1764 if ( !(flags & REXEC_NOT_FIRST) ) {
1765 if (RX_MATCH_COPIED(prog)) {
1766 Safefree(prog->subbeg);
1767 RX_MATCH_COPIED_off(prog);
1769 if (flags & REXEC_COPY_STR) {
1770 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1772 s = savepvn(strbeg, i);
1775 RX_MATCH_COPIED_on(prog);
1778 prog->subbeg = strbeg;
1779 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1786 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1787 PL_colors[4],PL_colors[5]));
1788 if (PL_reg_eval_set)
1789 restore_pos(aTHXo_ 0);
1794 - regtry - try match at specific point
1796 STATIC I32 /* 0 failure, 1 success */
1797 S_regtry(pTHX_ regexp *prog, char *startpos)
1805 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1807 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1810 PL_reg_eval_set = RS_init;
1812 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1813 (IV)(PL_stack_sp - PL_stack_base));
1815 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1816 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1817 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1819 /* Apparently this is not needed, judging by wantarray. */
1820 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1821 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1824 /* Make $_ available to executed code. */
1825 if (PL_reg_sv != DEFSV) {
1826 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1831 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1832 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
1833 /* prepare for quick setting of pos */
1834 sv_magic(PL_reg_sv, (SV*)0,
1835 PERL_MAGIC_regex_global, Nullch, 0);
1836 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
1840 PL_reg_oldpos = mg->mg_len;
1841 SAVEDESTRUCTOR_X(restore_pos, 0);
1844 Newz(22,PL_reg_curpm, 1, PMOP);
1845 PL_reg_curpm->op_pmregexp = prog;
1846 PL_reg_oldcurpm = PL_curpm;
1847 PL_curpm = PL_reg_curpm;
1848 if (RX_MATCH_COPIED(prog)) {
1849 /* Here is a serious problem: we cannot rewrite subbeg,
1850 since it may be needed if this match fails. Thus
1851 $` inside (?{}) could fail... */
1852 PL_reg_oldsaved = prog->subbeg;
1853 PL_reg_oldsavedlen = prog->sublen;
1854 RX_MATCH_COPIED_off(prog);
1857 PL_reg_oldsaved = Nullch;
1858 prog->subbeg = PL_bostr;
1859 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1861 prog->startp[0] = startpos - PL_bostr;
1862 PL_reginput = startpos;
1863 PL_regstartp = prog->startp;
1864 PL_regendp = prog->endp;
1865 PL_reglastparen = &prog->lastparen;
1866 prog->lastparen = 0;
1868 DEBUG_r(PL_reg_starttry = startpos);
1869 if (PL_reg_start_tmpl <= prog->nparens) {
1870 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1871 if(PL_reg_start_tmp)
1872 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1874 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1877 /* XXXX What this code is doing here?!!! There should be no need
1878 to do this again and again, PL_reglastparen should take care of
1881 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1882 * Actually, the code in regcppop() (which Ilya may be meaning by
1883 * PL_reglastparen), is not needed at all by the test suite
1884 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1885 * enough, for building DynaLoader, or otherwise this
1886 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1887 * will happen. Meanwhile, this code *is* needed for the
1888 * above-mentioned test suite tests to succeed. The common theme
1889 * on those tests seems to be returning null fields from matches.
1894 if (prog->nparens) {
1895 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1902 if (regmatch(prog->program + 1)) {
1903 prog->endp[0] = PL_reginput - PL_bostr;
1906 REGCP_UNWIND(lastcp);
1910 #define RE_UNWIND_BRANCH 1
1911 #define RE_UNWIND_BRANCHJ 2
1915 typedef struct { /* XX: makes sense to enlarge it... */
1919 } re_unwind_generic_t;
1932 } re_unwind_branch_t;
1934 typedef union re_unwind_t {
1936 re_unwind_generic_t generic;
1937 re_unwind_branch_t branch;
1941 - regmatch - main matching routine
1943 * Conceptually the strategy is simple: check to see whether the current
1944 * node matches, call self recursively to see whether the rest matches,
1945 * and then act accordingly. In practice we make some effort to avoid
1946 * recursion, in particular by going through "ordinary" nodes (that don't
1947 * need to know whether the rest of the match failed) by a loop instead of
1950 /* [lwall] I've hoisted the register declarations to the outer block in order to
1951 * maybe save a little bit of pushing and popping on the stack. It also takes
1952 * advantage of machines that use a register save mask on subroutine entry.
1954 STATIC I32 /* 0 failure, 1 success */
1955 S_regmatch(pTHX_ regnode *prog)
1957 register regnode *scan; /* Current node. */
1958 regnode *next; /* Next node. */
1959 regnode *inner; /* Next node in internal branch. */
1960 register I32 nextchr; /* renamed nextchr - nextchar colides with
1961 function of same name */
1962 register I32 n; /* no or next */
1963 register I32 ln; /* len or last */
1964 register char *s; /* operand or save */
1965 register char *locinput = PL_reginput;
1966 register I32 c1, c2, paren; /* case fold search, parenth */
1967 int minmod = 0, sw = 0, logical = 0;
1969 I32 firstcp = PL_savestack_ix;
1970 register bool do_utf8 = DO_UTF8(PL_reg_sv);
1976 /* Note that nextchr is a byte even in UTF */
1977 nextchr = UCHARAT(locinput);
1979 while (scan != NULL) {
1980 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1982 # define sayYES goto yes
1983 # define sayNO goto no
1984 # define sayYES_FINAL goto yes_final
1985 # define sayYES_LOUD goto yes_loud
1986 # define sayNO_FINAL goto no_final
1987 # define sayNO_SILENT goto do_no
1988 # define saySAME(x) if (x) goto yes; else goto no
1989 # define REPORT_CODE_OFF 24
1991 # define sayYES return 1
1992 # define sayNO return 0
1993 # define sayYES_FINAL return 1
1994 # define sayYES_LOUD return 1
1995 # define sayNO_FINAL return 0
1996 # define sayNO_SILENT return 0
1997 # define saySAME(x) return x
2000 SV *prop = sv_newmortal();
2001 int docolor = *PL_colors[0];
2002 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2003 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2004 /* The part of the string before starttry has one color
2005 (pref0_len chars), between starttry and current
2006 position another one (pref_len - pref0_len chars),
2007 after the current position the third one.
2008 We assume that pref0_len <= pref_len, otherwise we
2009 decrease pref0_len. */
2010 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2011 ? (5 + taill) - l : locinput - PL_bostr;
2014 while (UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2016 pref0_len = pref_len - (locinput - PL_reg_starttry);
2017 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2018 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2019 ? (5 + taill) - pref_len : PL_regeol - locinput);
2020 while (UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2024 if (pref0_len > pref_len)
2025 pref0_len = pref_len;
2026 regprop(prop, scan);
2027 PerlIO_printf(Perl_debug_log,
2028 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2029 (IV)(locinput - PL_bostr),
2030 PL_colors[4], pref0_len,
2031 locinput - pref_len, PL_colors[5],
2032 PL_colors[2], pref_len - pref0_len,
2033 locinput - pref_len + pref0_len, PL_colors[3],
2034 (docolor ? "" : "> <"),
2035 PL_colors[0], l, locinput, PL_colors[1],
2036 15 - l - pref_len + 1,
2038 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2042 next = scan + NEXT_OFF(scan);
2048 if (locinput == PL_bostr || (PL_multiline &&
2049 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2051 /* regtill = regbol; */
2056 if (locinput == PL_bostr ||
2057 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2063 if (locinput == PL_bostr)
2067 if (locinput == PL_reg_ganch)
2077 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2082 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2084 if (PL_regeol - locinput > 1)
2088 if (PL_regeol != locinput)
2092 if (!nextchr && locinput >= PL_regeol)
2094 nextchr = UCHARAT(++locinput);
2097 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2100 locinput += PL_utf8skip[nextchr];
2101 if (locinput > PL_regeol)
2103 nextchr = UCHARAT(locinput);
2106 nextchr = UCHARAT(++locinput);
2111 if (do_utf8 != (UTF!=0)) {
2119 if (*((U8*)s) != utf8_to_uvchr((U8*)l, &len))
2128 if (*((U8*)l) != utf8_to_uvchr((U8*)s, &len))
2134 nextchr = UCHARAT(locinput);
2137 /* Inline the first character, for speed. */
2138 if (UCHARAT(s) != nextchr)
2140 if (PL_regeol - locinput < ln)
2142 if (ln > 1 && memNE(s, locinput, ln))
2145 nextchr = UCHARAT(locinput);
2148 PL_reg_flags |= RF_tainted;
2158 c1 = OP(scan) == EXACTF;
2160 if (l >= PL_regeol) {
2163 if ((UTF ? utf8n_to_uvchr((U8*)s, e - s, 0, 0) : *((U8*)s)) !=
2164 (c1 ? toLOWER_utf8((U8*)l) : toLOWER_LC_utf8((U8*)l)))
2166 s += UTF ? UTF8SKIP(s) : 1;
2170 nextchr = UCHARAT(locinput);
2174 /* Inline the first character, for speed. */
2175 if (UCHARAT(s) != nextchr &&
2176 UCHARAT(s) != ((OP(scan) == EXACTF)
2177 ? PL_fold : PL_fold_locale)[nextchr])
2179 if (PL_regeol - locinput < ln)
2181 if (ln > 1 && (OP(scan) == EXACTF
2182 ? ibcmp(s, locinput, ln)
2183 : ibcmp_locale(s, locinput, ln)))
2186 nextchr = UCHARAT(locinput);
2190 if (!reginclass(scan, (U8*)locinput, do_utf8))
2192 if (locinput >= PL_regeol)
2194 locinput += PL_utf8skip[nextchr];
2195 nextchr = UCHARAT(locinput);
2199 nextchr = UCHARAT(locinput);
2200 if (!reginclass(scan, (U8*)locinput, do_utf8))
2202 if (!nextchr && locinput >= PL_regeol)
2204 nextchr = UCHARAT(++locinput);
2208 PL_reg_flags |= RF_tainted;
2214 LOAD_UTF8_CHARCLASS(alnum,"a");
2215 if (!(OP(scan) == ALNUM
2216 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2217 : isALNUM_LC_utf8((U8*)locinput)))
2221 locinput += PL_utf8skip[nextchr];
2222 nextchr = UCHARAT(locinput);
2225 if (!(OP(scan) == ALNUM
2226 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2228 nextchr = UCHARAT(++locinput);
2231 PL_reg_flags |= RF_tainted;
2234 if (!nextchr && locinput >= PL_regeol)
2237 LOAD_UTF8_CHARCLASS(alnum,"a");
2238 if (OP(scan) == NALNUM
2239 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2240 : isALNUM_LC_utf8((U8*)locinput))
2244 locinput += PL_utf8skip[nextchr];
2245 nextchr = UCHARAT(locinput);
2248 if (OP(scan) == NALNUM
2249 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2251 nextchr = UCHARAT(++locinput);
2255 PL_reg_flags |= RF_tainted;
2259 /* was last char in word? */
2261 if (locinput == PL_bostr)
2264 U8 *r = reghop((U8*)locinput, -1);
2266 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2268 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2269 ln = isALNUM_uni(ln);
2270 LOAD_UTF8_CHARCLASS(alnum,"a");
2271 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2274 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2275 n = isALNUM_LC_utf8((U8*)locinput);
2279 ln = (locinput != PL_bostr) ?
2280 UCHARAT(locinput - 1) : '\n';
2281 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2283 n = isALNUM(nextchr);
2286 ln = isALNUM_LC(ln);
2287 n = isALNUM_LC(nextchr);
2290 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2291 OP(scan) == BOUNDL))
2295 PL_reg_flags |= RF_tainted;
2301 if (UTF8_IS_CONTINUED(nextchr)) {
2302 LOAD_UTF8_CHARCLASS(space," ");
2303 if (!(OP(scan) == SPACE
2304 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2305 : isSPACE_LC_utf8((U8*)locinput)))
2309 locinput += PL_utf8skip[nextchr];
2310 nextchr = UCHARAT(locinput);
2313 if (!(OP(scan) == SPACE
2314 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2316 nextchr = UCHARAT(++locinput);
2319 if (!(OP(scan) == SPACE
2320 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2322 nextchr = UCHARAT(++locinput);
2326 PL_reg_flags |= RF_tainted;
2329 if (!nextchr && locinput >= PL_regeol)
2332 LOAD_UTF8_CHARCLASS(space," ");
2333 if (OP(scan) == NSPACE
2334 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2335 : isSPACE_LC_utf8((U8*)locinput))
2339 locinput += PL_utf8skip[nextchr];
2340 nextchr = UCHARAT(locinput);
2343 if (OP(scan) == NSPACE
2344 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2346 nextchr = UCHARAT(++locinput);
2349 PL_reg_flags |= RF_tainted;
2355 LOAD_UTF8_CHARCLASS(digit,"0");
2356 if (!(OP(scan) == DIGIT
2357 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2358 : isDIGIT_LC_utf8((U8*)locinput)))
2362 locinput += PL_utf8skip[nextchr];
2363 nextchr = UCHARAT(locinput);
2366 if (!(OP(scan) == DIGIT
2367 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2369 nextchr = UCHARAT(++locinput);
2372 PL_reg_flags |= RF_tainted;
2375 if (!nextchr && locinput >= PL_regeol)
2378 LOAD_UTF8_CHARCLASS(digit,"0");
2379 if (OP(scan) == NDIGIT
2380 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2381 : isDIGIT_LC_utf8((U8*)locinput))
2385 locinput += PL_utf8skip[nextchr];
2386 nextchr = UCHARAT(locinput);
2389 if (OP(scan) == NDIGIT
2390 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2392 nextchr = UCHARAT(++locinput);
2395 LOAD_UTF8_CHARCLASS(mark,"~");
2396 if (locinput >= PL_regeol ||
2397 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2399 locinput += PL_utf8skip[nextchr];
2400 while (locinput < PL_regeol &&
2401 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2402 locinput += UTF8SKIP(locinput);
2403 if (locinput > PL_regeol)
2405 nextchr = UCHARAT(locinput);
2408 PL_reg_flags |= RF_tainted;
2412 n = ARG(scan); /* which paren pair */
2413 ln = PL_regstartp[n];
2414 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2415 if (*PL_reglastparen < n || ln == -1)
2416 sayNO; /* Do not match unless seen CLOSEn. */
2417 if (ln == PL_regendp[n])
2421 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2423 char *e = PL_bostr + PL_regendp[n];
2425 * Note that we can't do the "other character" lookup trick as
2426 * in the 8-bit case (no pun intended) because in Unicode we
2427 * have to map both upper and title case to lower case.
2429 if (OP(scan) == REFF) {
2433 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2443 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2450 nextchr = UCHARAT(locinput);
2454 /* Inline the first character, for speed. */
2455 if (UCHARAT(s) != nextchr &&
2457 (UCHARAT(s) != ((OP(scan) == REFF
2458 ? PL_fold : PL_fold_locale)[nextchr]))))
2460 ln = PL_regendp[n] - ln;
2461 if (locinput + ln > PL_regeol)
2463 if (ln > 1 && (OP(scan) == REF
2464 ? memNE(s, locinput, ln)
2466 ? ibcmp(s, locinput, ln)
2467 : ibcmp_locale(s, locinput, ln))))
2470 nextchr = UCHARAT(locinput);
2481 OP_4tree *oop = PL_op;
2482 COP *ocurcop = PL_curcop;
2483 SV **ocurpad = PL_curpad;
2487 PL_op = (OP_4tree*)PL_regdata->data[n];
2488 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2489 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2490 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2492 CALLRUNOPS(aTHX); /* Scalar context. */
2498 PL_curpad = ocurpad;
2499 PL_curcop = ocurcop;
2501 if (logical == 2) { /* Postponed subexpression. */
2503 MAGIC *mg = Null(MAGIC*);
2505 CHECKPOINT cp, lastcp;
2507 if(SvROK(ret) || SvRMAGICAL(ret)) {
2508 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2511 mg = mg_find(sv, PERL_MAGIC_qr);
2514 re = (regexp *)mg->mg_obj;
2515 (void)ReREFCNT_inc(re);
2519 char *t = SvPV(ret, len);
2521 char *oprecomp = PL_regprecomp;
2522 I32 osize = PL_regsize;
2523 I32 onpar = PL_regnpar;
2526 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2528 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2529 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2531 PL_regprecomp = oprecomp;
2536 PerlIO_printf(Perl_debug_log,
2537 "Entering embedded `%s%.60s%s%s'\n",
2541 (strlen(re->precomp) > 60 ? "..." : ""))
2544 state.prev = PL_reg_call_cc;
2545 state.cc = PL_regcc;
2546 state.re = PL_reg_re;
2550 cp = regcppush(0); /* Save *all* the positions. */
2553 state.ss = PL_savestack_ix;
2554 *PL_reglastparen = 0;
2555 PL_reg_call_cc = &state;
2556 PL_reginput = locinput;
2558 /* XXXX This is too dramatic a measure... */
2561 if (regmatch(re->program + 1)) {
2562 /* Even though we succeeded, we need to restore
2563 global variables, since we may be wrapped inside
2564 SUSPEND, thus the match may be not finished yet. */
2566 /* XXXX Do this only if SUSPENDed? */
2567 PL_reg_call_cc = state.prev;
2568 PL_regcc = state.cc;
2569 PL_reg_re = state.re;
2570 cache_re(PL_reg_re);
2572 /* XXXX This is too dramatic a measure... */
2575 /* These are needed even if not SUSPEND. */
2581 REGCP_UNWIND(lastcp);
2583 PL_reg_call_cc = state.prev;
2584 PL_regcc = state.cc;
2585 PL_reg_re = state.re;
2586 cache_re(PL_reg_re);
2588 /* XXXX This is too dramatic a measure... */
2597 sv_setsv(save_scalar(PL_replgv), ret);
2601 n = ARG(scan); /* which paren pair */
2602 PL_reg_start_tmp[n] = locinput;
2607 n = ARG(scan); /* which paren pair */
2608 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2609 PL_regendp[n] = locinput - PL_bostr;
2610 if (n > *PL_reglastparen)
2611 *PL_reglastparen = n;
2614 n = ARG(scan); /* which paren pair */
2615 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2618 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2620 next = NEXTOPER(NEXTOPER(scan));
2622 next = scan + ARG(scan);
2623 if (OP(next) == IFTHEN) /* Fake one. */
2624 next = NEXTOPER(NEXTOPER(next));
2628 logical = scan->flags;
2630 /*******************************************************************
2631 PL_regcc contains infoblock about the innermost (...)* loop, and
2632 a pointer to the next outer infoblock.
2634 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2636 1) After matching X, regnode for CURLYX is processed;
2638 2) This regnode creates infoblock on the stack, and calls
2639 regmatch() recursively with the starting point at WHILEM node;
2641 3) Each hit of WHILEM node tries to match A and Z (in the order
2642 depending on the current iteration, min/max of {min,max} and
2643 greediness). The information about where are nodes for "A"
2644 and "Z" is read from the infoblock, as is info on how many times "A"
2645 was already matched, and greediness.
2647 4) After A matches, the same WHILEM node is hit again.
2649 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2650 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2651 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2652 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2653 of the external loop.
2655 Currently present infoblocks form a tree with a stem formed by PL_curcc
2656 and whatever it mentions via ->next, and additional attached trees
2657 corresponding to temporarily unset infoblocks as in "5" above.
2659 In the following picture infoblocks for outer loop of
2660 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2661 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2662 infoblocks are drawn below the "reset" infoblock.
2664 In fact in the picture below we do not show failed matches for Z and T
2665 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2666 more obvious *why* one needs to *temporary* unset infoblocks.]
2668 Matched REx position InfoBlocks Comment
2672 Y A)*?Z)*?T x <- O <- I
2673 YA )*?Z)*?T x <- O <- I
2674 YA A)*?Z)*?T x <- O <- I
2675 YAA )*?Z)*?T x <- O <- I
2676 YAA Z)*?T x <- O # Temporary unset I
2679 YAAZ Y(A)*?Z)*?T x <- O
2682 YAAZY (A)*?Z)*?T x <- O
2685 YAAZY A)*?Z)*?T x <- O <- I
2688 YAAZYA )*?Z)*?T x <- O <- I
2691 YAAZYA Z)*?T x <- O # Temporary unset I
2697 YAAZYAZ T x # Temporary unset O
2704 *******************************************************************/
2707 CHECKPOINT cp = PL_savestack_ix;
2708 /* No need to save/restore up to this paren */
2709 I32 parenfloor = scan->flags;
2711 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2713 cc.oldcc = PL_regcc;
2715 /* XXXX Probably it is better to teach regpush to support
2716 parenfloor > PL_regsize... */
2717 if (parenfloor > *PL_reglastparen)
2718 parenfloor = *PL_reglastparen; /* Pessimization... */
2719 cc.parenfloor = parenfloor;
2721 cc.min = ARG1(scan);
2722 cc.max = ARG2(scan);
2723 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2727 PL_reginput = locinput;
2728 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2730 PL_regcc = cc.oldcc;
2736 * This is really hard to understand, because after we match
2737 * what we're trying to match, we must make sure the rest of
2738 * the REx is going to match for sure, and to do that we have
2739 * to go back UP the parse tree by recursing ever deeper. And
2740 * if it fails, we have to reset our parent's current state
2741 * that we can try again after backing off.
2744 CHECKPOINT cp, lastcp;
2745 CURCUR* cc = PL_regcc;
2746 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2748 n = cc->cur + 1; /* how many we know we matched */
2749 PL_reginput = locinput;
2752 PerlIO_printf(Perl_debug_log,
2753 "%*s %ld out of %ld..%ld cc=%lx\n",
2754 REPORT_CODE_OFF+PL_regindent*2, "",
2755 (long)n, (long)cc->min,
2756 (long)cc->max, (long)cc)
2759 /* If degenerate scan matches "", assume scan done. */
2761 if (locinput == cc->lastloc && n >= cc->min) {
2762 PL_regcc = cc->oldcc;
2766 PerlIO_printf(Perl_debug_log,
2767 "%*s empty match detected, try continuation...\n",
2768 REPORT_CODE_OFF+PL_regindent*2, "")
2770 if (regmatch(cc->next))
2778 /* First just match a string of min scans. */
2782 cc->lastloc = locinput;
2783 if (regmatch(cc->scan))
2786 cc->lastloc = lastloc;
2791 /* Check whether we already were at this position.
2792 Postpone detection until we know the match is not
2793 *that* much linear. */
2794 if (!PL_reg_maxiter) {
2795 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2796 PL_reg_leftiter = PL_reg_maxiter;
2798 if (PL_reg_leftiter-- == 0) {
2799 I32 size = (PL_reg_maxiter + 7)/8;
2800 if (PL_reg_poscache) {
2801 if (PL_reg_poscache_size < size) {
2802 Renew(PL_reg_poscache, size, char);
2803 PL_reg_poscache_size = size;
2805 Zero(PL_reg_poscache, size, char);
2808 PL_reg_poscache_size = size;
2809 Newz(29, PL_reg_poscache, size, char);
2812 PerlIO_printf(Perl_debug_log,
2813 "%sDetected a super-linear match, switching on caching%s...\n",
2814 PL_colors[4], PL_colors[5])
2817 if (PL_reg_leftiter < 0) {
2818 I32 o = locinput - PL_bostr, b;
2820 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2823 if (PL_reg_poscache[o] & (1<<b)) {
2825 PerlIO_printf(Perl_debug_log,
2826 "%*s already tried at this position...\n",
2827 REPORT_CODE_OFF+PL_regindent*2, "")
2831 PL_reg_poscache[o] |= (1<<b);
2835 /* Prefer next over scan for minimal matching. */
2838 PL_regcc = cc->oldcc;
2841 cp = regcppush(cc->parenfloor);
2843 if (regmatch(cc->next)) {
2845 sayYES; /* All done. */
2847 REGCP_UNWIND(lastcp);
2853 if (n >= cc->max) { /* Maximum greed exceeded? */
2854 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2855 && !(PL_reg_flags & RF_warned)) {
2856 PL_reg_flags |= RF_warned;
2857 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2858 "Complex regular subexpression recursion",
2865 PerlIO_printf(Perl_debug_log,
2866 "%*s trying longer...\n",
2867 REPORT_CODE_OFF+PL_regindent*2, "")
2869 /* Try scanning more and see if it helps. */
2870 PL_reginput = locinput;
2872 cc->lastloc = locinput;
2873 cp = regcppush(cc->parenfloor);
2875 if (regmatch(cc->scan)) {
2879 REGCP_UNWIND(lastcp);
2882 cc->lastloc = lastloc;
2886 /* Prefer scan over next for maximal matching. */
2888 if (n < cc->max) { /* More greed allowed? */
2889 cp = regcppush(cc->parenfloor);
2891 cc->lastloc = locinput;
2893 if (regmatch(cc->scan)) {
2897 REGCP_UNWIND(lastcp);
2898 regcppop(); /* Restore some previous $<digit>s? */
2899 PL_reginput = locinput;
2901 PerlIO_printf(Perl_debug_log,
2902 "%*s failed, try continuation...\n",
2903 REPORT_CODE_OFF+PL_regindent*2, "")
2906 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2907 && !(PL_reg_flags & RF_warned)) {
2908 PL_reg_flags |= RF_warned;
2909 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2910 "Complex regular subexpression recursion",
2914 /* Failed deeper matches of scan, so see if this one works. */
2915 PL_regcc = cc->oldcc;
2918 if (regmatch(cc->next))
2924 cc->lastloc = lastloc;
2929 next = scan + ARG(scan);
2932 inner = NEXTOPER(NEXTOPER(scan));
2935 inner = NEXTOPER(scan);
2940 if (OP(next) != c1) /* No choice. */
2941 next = inner; /* Avoid recursion. */
2943 I32 lastparen = *PL_reglastparen;
2945 re_unwind_branch_t *uw;
2947 /* Put unwinding data on stack */
2948 unwind1 = SSNEWt(1,re_unwind_branch_t);
2949 uw = SSPTRt(unwind1,re_unwind_branch_t);
2952 uw->type = ((c1 == BRANCH)
2954 : RE_UNWIND_BRANCHJ);
2955 uw->lastparen = lastparen;
2957 uw->locinput = locinput;
2958 uw->nextchr = nextchr;
2960 uw->regindent = ++PL_regindent;
2963 REGCP_SET(uw->lastcp);
2965 /* Now go into the first branch */
2978 /* We suppose that the next guy does not need
2979 backtracking: in particular, it is of constant length,
2980 and has no parenths to influence future backrefs. */
2981 ln = ARG1(scan); /* min to match */
2982 n = ARG2(scan); /* max to match */
2983 paren = scan->flags;
2985 if (paren > PL_regsize)
2987 if (paren > *PL_reglastparen)
2988 *PL_reglastparen = paren;
2990 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2992 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2993 PL_reginput = locinput;
2996 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2998 if (ln && l == 0 && n >= ln
2999 /* In fact, this is tricky. If paren, then the
3000 fact that we did/didnot match may influence
3001 future execution. */
3002 && !(paren && ln == 0))
3004 locinput = PL_reginput;
3005 if (PL_regkind[(U8)OP(next)] == EXACT) {
3006 c1 = (U8)*STRING(next);
3007 if (OP(next) == EXACTF)
3009 else if (OP(next) == EXACTFL)
3010 c2 = PL_fold_locale[c1];
3017 /* This may be improved if l == 0. */
3018 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3019 /* If it could work, try it. */
3021 UCHARAT(PL_reginput) == c1 ||
3022 UCHARAT(PL_reginput) == c2)
3026 PL_regstartp[paren] =
3027 HOPc(PL_reginput, -l) - PL_bostr;
3028 PL_regendp[paren] = PL_reginput - PL_bostr;
3031 PL_regendp[paren] = -1;
3035 REGCP_UNWIND(lastcp);
3037 /* Couldn't or didn't -- move forward. */
3038 PL_reginput = locinput;
3039 if (regrepeat_hard(scan, 1, &l)) {
3041 locinput = PL_reginput;
3048 n = regrepeat_hard(scan, n, &l);
3049 if (n != 0 && l == 0
3050 /* In fact, this is tricky. If paren, then the
3051 fact that we did/didnot match may influence
3052 future execution. */
3053 && !(paren && ln == 0))
3055 locinput = PL_reginput;
3057 PerlIO_printf(Perl_debug_log,
3058 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3059 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3063 if (PL_regkind[(U8)OP(next)] == EXACT) {
3064 c1 = (U8)*STRING(next);
3065 if (OP(next) == EXACTF)
3067 else if (OP(next) == EXACTFL)
3068 c2 = PL_fold_locale[c1];
3077 /* If it could work, try it. */
3079 UCHARAT(PL_reginput) == c1 ||
3080 UCHARAT(PL_reginput) == c2)
3083 PerlIO_printf(Perl_debug_log,
3084 "%*s trying tail with n=%"IVdf"...\n",
3085 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3089 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3090 PL_regendp[paren] = PL_reginput - PL_bostr;
3093 PL_regendp[paren] = -1;
3097 REGCP_UNWIND(lastcp);
3099 /* Couldn't or didn't -- back up. */
3101 locinput = HOPc(locinput, -l);
3102 PL_reginput = locinput;
3109 paren = scan->flags; /* Which paren to set */
3110 if (paren > PL_regsize)
3112 if (paren > *PL_reglastparen)
3113 *PL_reglastparen = paren;
3114 ln = ARG1(scan); /* min to match */
3115 n = ARG2(scan); /* max to match */
3116 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3120 ln = ARG1(scan); /* min to match */
3121 n = ARG2(scan); /* max to match */
3122 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3127 scan = NEXTOPER(scan);
3133 scan = NEXTOPER(scan);
3137 * Lookahead to avoid useless match attempts
3138 * when we know what character comes next.
3140 if (PL_regkind[(U8)OP(next)] == EXACT) {
3141 U8 *s = (U8*)STRING(next);
3144 if (OP(next) == EXACTF)
3146 else if (OP(next) == EXACTFL)
3147 c2 = PL_fold_locale[c1];
3150 if (OP(next) == EXACTF) {
3151 c1 = to_utf8_lower(s);
3152 c2 = to_utf8_upper(s);
3155 c2 = c1 = utf8_to_uvchr(s, NULL);
3161 PL_reginput = locinput;
3165 if (ln && regrepeat(scan, ln) < ln)
3167 locinput = PL_reginput;
3170 char *e; /* Should not check after this */
3171 char *old = locinput;
3173 if (n == REG_INFTY) {
3176 while (UTF8_IS_CONTINUATION(*(U8*)e))
3182 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3186 e = locinput + n - ln;
3192 /* Find place 'next' could work */
3195 while (locinput <= e && *locinput != c1)
3198 while (locinput <= e
3203 count = locinput - old;
3210 utf8_to_uvchr((U8*)locinput, &len) != c1;
3215 for (count = 0; locinput <= e; count++) {
3216 UV c = utf8_to_uvchr((U8*)locinput, &len);
3217 if (c == c1 || c == c2)
3225 /* PL_reginput == old now */
3226 if (locinput != old) {
3227 ln = 1; /* Did some */
3228 if (regrepeat(scan, count) < count)
3231 /* PL_reginput == locinput now */
3232 TRYPAREN(paren, ln, locinput);
3233 PL_reginput = locinput; /* Could be reset... */
3234 REGCP_UNWIND(lastcp);
3235 /* Couldn't or didn't -- move forward. */
3238 locinput += UTF8SKIP(locinput);
3244 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3248 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3250 c = UCHARAT(PL_reginput);
3251 /* If it could work, try it. */
3252 if (c == c1 || c == c2)
3254 TRYPAREN(paren, n, PL_reginput);
3255 REGCP_UNWIND(lastcp);
3258 /* If it could work, try it. */
3259 else if (c1 == -1000)
3261 TRYPAREN(paren, n, PL_reginput);
3262 REGCP_UNWIND(lastcp);
3264 /* Couldn't or didn't -- move forward. */
3265 PL_reginput = locinput;
3266 if (regrepeat(scan, 1)) {
3268 locinput = PL_reginput;
3276 n = regrepeat(scan, n);
3277 locinput = PL_reginput;
3278 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3279 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3280 ln = n; /* why back off? */
3281 /* ...because $ and \Z can match before *and* after
3282 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3283 We should back off by one in this case. */
3284 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3293 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3295 c = UCHARAT(PL_reginput);
3297 /* If it could work, try it. */
3298 if (c1 == -1000 || c == c1 || c == c2)
3300 TRYPAREN(paren, n, PL_reginput);
3301 REGCP_UNWIND(lastcp);
3303 /* Couldn't or didn't -- back up. */
3305 PL_reginput = locinput = HOPc(locinput, -1);
3313 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3315 c = UCHARAT(PL_reginput);
3317 /* If it could work, try it. */
3318 if (c1 == -1000 || c == c1 || c == c2)
3320 TRYPAREN(paren, n, PL_reginput);
3321 REGCP_UNWIND(lastcp);
3323 /* Couldn't or didn't -- back up. */
3325 PL_reginput = locinput = HOPc(locinput, -1);
3332 if (PL_reg_call_cc) {
3333 re_cc_state *cur_call_cc = PL_reg_call_cc;
3334 CURCUR *cctmp = PL_regcc;
3335 regexp *re = PL_reg_re;
3336 CHECKPOINT cp, lastcp;
3338 cp = regcppush(0); /* Save *all* the positions. */
3340 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3342 PL_reginput = locinput; /* Make position available to
3344 cache_re(PL_reg_call_cc->re);
3345 PL_regcc = PL_reg_call_cc->cc;
3346 PL_reg_call_cc = PL_reg_call_cc->prev;
3347 if (regmatch(cur_call_cc->node)) {
3348 PL_reg_call_cc = cur_call_cc;
3352 REGCP_UNWIND(lastcp);
3354 PL_reg_call_cc = cur_call_cc;
3360 PerlIO_printf(Perl_debug_log,
3361 "%*s continuation failed...\n",
3362 REPORT_CODE_OFF+PL_regindent*2, "")
3366 if (locinput < PL_regtill) {
3367 DEBUG_r(PerlIO_printf(Perl_debug_log,
3368 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3370 (long)(locinput - PL_reg_starttry),
3371 (long)(PL_regtill - PL_reg_starttry),
3373 sayNO_FINAL; /* Cannot match: too short. */
3375 PL_reginput = locinput; /* put where regtry can find it */
3376 sayYES_FINAL; /* Success! */
3378 PL_reginput = locinput; /* put where regtry can find it */
3379 sayYES_LOUD; /* Success! */
3382 PL_reginput = locinput;
3387 s = HOPBACKc(locinput, scan->flags);
3393 PL_reginput = locinput;
3398 s = HOPBACKc(locinput, scan->flags);
3404 PL_reginput = locinput;
3407 inner = NEXTOPER(NEXTOPER(scan));
3408 if (regmatch(inner) != n) {
3423 if (OP(scan) == SUSPEND) {
3424 locinput = PL_reginput;
3425 nextchr = UCHARAT(locinput);
3430 next = scan + ARG(scan);
3435 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3436 PTR2UV(scan), OP(scan));
3437 Perl_croak(aTHX_ "regexp memory corruption");
3444 * We get here only if there's trouble -- normally "case END" is
3445 * the terminating point.
3447 Perl_croak(aTHX_ "corrupted regexp pointers");
3453 PerlIO_printf(Perl_debug_log,
3454 "%*s %scould match...%s\n",
3455 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3459 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3460 PL_colors[4],PL_colors[5]));
3466 #if 0 /* Breaks $^R */
3474 PerlIO_printf(Perl_debug_log,
3475 "%*s %sfailed...%s\n",
3476 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3482 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3485 case RE_UNWIND_BRANCH:
3486 case RE_UNWIND_BRANCHJ:
3488 re_unwind_branch_t *uwb = &(uw->branch);
3489 I32 lastparen = uwb->lastparen;
3491 REGCP_UNWIND(uwb->lastcp);
3492 for (n = *PL_reglastparen; n > lastparen; n--)
3494 *PL_reglastparen = n;
3495 scan = next = uwb->next;
3497 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3498 ? BRANCH : BRANCHJ) ) { /* Failure */
3505 /* Have more choice yet. Reuse the same uwb. */
3507 if ((n = (uwb->type == RE_UNWIND_BRANCH
3508 ? NEXT_OFF(next) : ARG(next))))
3511 next = NULL; /* XXXX Needn't unwinding in this case... */
3513 next = NEXTOPER(scan);
3514 if (uwb->type == RE_UNWIND_BRANCHJ)
3515 next = NEXTOPER(next);
3516 locinput = uwb->locinput;
3517 nextchr = uwb->nextchr;
3519 PL_regindent = uwb->regindent;
3526 Perl_croak(aTHX_ "regexp unwind memory corruption");
3537 - regrepeat - repeatedly match something simple, report how many
3540 * [This routine now assumes that it will only match on things of length 1.
3541 * That was true before, but now we assume scan - reginput is the count,
3542 * rather than incrementing count on every character. [Er, except utf8.]]
3545 S_regrepeat(pTHX_ regnode *p, I32 max)
3547 register char *scan;
3549 register char *loceol = PL_regeol;
3550 register I32 hardcount = 0;
3551 register bool do_utf8 = DO_UTF8(PL_reg_sv);
3554 if (max != REG_INFTY && max < loceol - scan)
3555 loceol = scan + max;
3560 while (scan < loceol && hardcount < max && *scan != '\n') {
3561 scan += UTF8SKIP(scan);
3565 while (scan < loceol && *scan != '\n')
3572 case EXACT: /* length of string is 1 */
3574 while (scan < loceol && UCHARAT(scan) == c)
3577 case EXACTF: /* length of string is 1 */
3579 while (scan < loceol &&
3580 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3583 case EXACTFL: /* length of string is 1 */
3584 PL_reg_flags |= RF_tainted;
3586 while (scan < loceol &&
3587 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3593 while (hardcount < max && scan < loceol &&
3594 reginclass(p, (U8*)scan, do_utf8)) {
3595 scan += UTF8SKIP(scan);
3599 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3606 LOAD_UTF8_CHARCLASS(alnum,"a");
3607 while (hardcount < max && scan < loceol &&
3608 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3609 scan += UTF8SKIP(scan);
3613 while (scan < loceol && isALNUM(*scan))
3618 PL_reg_flags |= RF_tainted;
3621 while (hardcount < max && scan < loceol &&
3622 isALNUM_LC_utf8((U8*)scan)) {
3623 scan += UTF8SKIP(scan);
3627 while (scan < loceol && isALNUM_LC(*scan))
3634 LOAD_UTF8_CHARCLASS(alnum,"a");
3635 while (hardcount < max && scan < loceol &&
3636 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3637 scan += UTF8SKIP(scan);
3641 while (scan < loceol && !isALNUM(*scan))
3646 PL_reg_flags |= RF_tainted;
3649 while (hardcount < max && scan < loceol &&
3650 !isALNUM_LC_utf8((U8*)scan)) {
3651 scan += UTF8SKIP(scan);
3655 while (scan < loceol && !isALNUM_LC(*scan))
3662 LOAD_UTF8_CHARCLASS(space," ");
3663 while (hardcount < max && scan < loceol &&
3665 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3666 scan += UTF8SKIP(scan);
3670 while (scan < loceol && isSPACE(*scan))
3675 PL_reg_flags |= RF_tainted;
3678 while (hardcount < max && scan < loceol &&
3679 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3680 scan += UTF8SKIP(scan);
3684 while (scan < loceol && isSPACE_LC(*scan))
3691 LOAD_UTF8_CHARCLASS(space," ");
3692 while (hardcount < max && scan < loceol &&
3694 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
3695 scan += UTF8SKIP(scan);
3699 while (scan < loceol && !isSPACE(*scan))
3704 PL_reg_flags |= RF_tainted;
3707 while (hardcount < max && scan < loceol &&
3708 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3709 scan += UTF8SKIP(scan);
3713 while (scan < loceol && !isSPACE_LC(*scan))
3720 LOAD_UTF8_CHARCLASS(digit,"0");
3721 while (hardcount < max && scan < loceol &&
3722 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3723 scan += UTF8SKIP(scan);
3727 while (scan < loceol && isDIGIT(*scan))
3734 LOAD_UTF8_CHARCLASS(digit,"0");
3735 while (hardcount < max && scan < loceol &&
3736 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
3737 scan += UTF8SKIP(scan);
3741 while (scan < loceol && !isDIGIT(*scan))
3745 default: /* Called on something of 0 width. */
3746 break; /* So match right here or not at all. */
3752 c = scan - PL_reginput;
3757 SV *prop = sv_newmortal();
3760 PerlIO_printf(Perl_debug_log,
3761 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3762 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3769 - regrepeat_hard - repeatedly match something, report total lenth and length
3771 * The repeater is supposed to have constant length.
3775 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3777 register char *scan;
3778 register char *start;
3779 register char *loceol = PL_regeol;
3781 I32 count = 0, res = 1;
3786 start = PL_reginput;
3787 if (DO_UTF8(PL_reg_sv)) {
3788 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3791 while (start < PL_reginput) {
3793 start += UTF8SKIP(start);
3804 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3806 *lp = l = PL_reginput - start;
3807 if (max != REG_INFTY && l*max < loceol - scan)
3808 loceol = scan + l*max;
3821 - regclass_swash - prepare the utf8 swash
3825 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
3830 if (PL_regdata && PL_regdata->count) {
3833 if (PL_regdata->what[n] == 's') {
3834 SV *rv = (SV*)PL_regdata->data[n];
3835 AV *av = (AV*)SvRV((SV*)rv);
3838 si = *av_fetch(av, 0, FALSE);
3839 a = av_fetch(av, 1, FALSE);
3843 else if (si && doinit) {
3844 sw = swash_init("utf8", "", si, 1, 0);
3845 (void)av_store(av, 1, sw);
3857 - reginclass - determine if a character falls into a character class
3861 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
3863 char flags = ANYOF_FLAGS(n);
3868 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
3870 if (do_utf8 || (flags & ANYOF_UNICODE)) {
3871 if (do_utf8 && !ANYOF_RUNTIME(n)) {
3872 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
3875 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
3878 SV *sw = regclass_swash(n, TRUE, 0);
3881 if (swash_fetch(sw, p, do_utf8))
3883 else if (flags & ANYOF_FOLD) {
3884 U8 tmpbuf[UTF8_MAXLEN+1];
3886 if (flags & ANYOF_LOCALE) {
3887 PL_reg_flags |= RF_tainted;
3888 uvchr_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3891 uvchr_to_utf8(tmpbuf, toLOWER_utf8(p));
3892 if (swash_fetch(sw, tmpbuf, do_utf8))
3898 if (!match && c < 256) {
3899 if (ANYOF_BITMAP_TEST(n, c))
3901 else if (flags & ANYOF_FOLD) {
3904 if (flags & ANYOF_LOCALE) {
3905 PL_reg_flags |= RF_tainted;
3906 f = PL_fold_locale[c];
3910 if (f != c && ANYOF_BITMAP_TEST(n, f))
3914 if (!match && (flags & ANYOF_CLASS)) {
3915 PL_reg_flags |= RF_tainted;
3917 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3918 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3919 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
3920 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3921 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3922 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3923 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3924 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3925 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3926 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3927 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
3928 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
3929 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3930 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3931 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3932 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3933 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
3934 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3935 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
3936 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3937 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3938 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3939 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
3940 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3941 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3942 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
3943 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
3944 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
3945 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
3946 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
3947 ) /* How's that for a conditional? */
3954 return (flags & ANYOF_INVERT) ? !match : match;
3958 S_reghop(pTHX_ U8 *s, I32 off)
3960 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
3964 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
3967 while (off-- && s < lim) {
3968 /* XXX could check well-formedness here */
3976 if (UTF8_IS_CONTINUED(*s)) {
3977 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
3980 /* XXX could check well-formedness here */
3988 S_reghopmaybe(pTHX_ U8 *s, I32 off)
3990 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
3994 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
3997 while (off-- && s < lim) {
3998 /* XXX could check well-formedness here */
4008 if (UTF8_IS_CONTINUED(*s)) {
4009 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4012 /* XXX could check well-formedness here */
4028 restore_pos(pTHXo_ void *arg)
4030 if (PL_reg_eval_set) {
4031 if (PL_reg_oldsaved) {
4032 PL_reg_re->subbeg = PL_reg_oldsaved;
4033 PL_reg_re->sublen = PL_reg_oldsavedlen;
4034 RX_MATCH_COPIED_on(PL_reg_re);
4036 PL_reg_magic->mg_len = PL_reg_oldpos;
4037 PL_reg_eval_set = 0;
4038 PL_curpm = PL_reg_oldcurpm;