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
43 # define PERL_NO_GET_CONTEXT
48 * pregcomp and pregexec -- regsub and regerror are not used in perl
50 * Copyright (c) 1986 by University of Toronto.
51 * Written by Henry Spencer. Not derived from licensed software.
53 * Permission is granted to anyone to use this software for any
54 * purpose on any computer system, and to redistribute it freely,
55 * subject to the following restrictions:
57 * 1. The author is not responsible for the consequences of use of
58 * this software, no matter how awful, even if they arise
61 * 2. The origin of this software must not be misrepresented, either
62 * by explicit claim or by omission.
64 * 3. Altered versions must be plainly marked as such, and must not
65 * be misrepresented as being the original software.
67 **** Alterations to Henry's code are...
69 **** Copyright (c) 1991-1999, Larry Wall
71 **** You may distribute under the terms of either the GNU General Public
72 **** License or the Artistic License, as specified in the README file.
74 * Beware that some of this code is subtly aware of the way operator
75 * precedence is structured in regular expressions. Serious changes in
76 * regular-expression syntax might require a total rethink.
79 #define PERL_IN_REGEXEC_C
82 #ifdef PERL_IN_XSUB_RE
83 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
90 #define RF_tainted 1 /* tainted information used? */
91 #define RF_warned 2 /* warned about big count? */
92 #define RF_evaled 4 /* Did an EVAL with setting? */
93 #define RF_utf8 8 /* String contains multibyte chars? */
95 #define UTF (PL_reg_flags & RF_utf8)
97 #define RS_init 1 /* eval environment created */
98 #define RS_set 2 /* replsv value is set */
101 #define STATIC static
108 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c) : ANYOF_BITMAP_TEST(p,c))
109 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
111 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
112 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
114 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
115 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
116 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
117 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
118 #define HOPc(pos,off) ((char*)HOP(pos,off))
119 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
121 static void restore_pos(pTHXo_ void *arg);
125 S_regcppush(pTHX_ I32 parenfloor)
128 int retval = PL_savestack_ix;
129 int i = (PL_regsize - parenfloor) * 4;
133 for (p = PL_regsize; p > parenfloor; p--) {
134 SSPUSHINT(PL_regendp[p]);
135 SSPUSHINT(PL_regstartp[p]);
136 SSPUSHPTR(PL_reg_start_tmp[p]);
139 SSPUSHINT(PL_regsize);
140 SSPUSHINT(*PL_reglastparen);
141 SSPUSHPTR(PL_reginput);
143 SSPUSHINT(SAVEt_REGCONTEXT);
147 /* These are needed since we do not localize EVAL nodes: */
148 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
149 " Setting an EVAL scope, savestack=%"IVdf"\n", \
150 (IV)PL_savestack_ix)); lastcp = PL_savestack_ix
152 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
153 PerlIO_printf(Perl_debug_log, \
154 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
155 (IV)lastcp, (IV)PL_savestack_ix) : 0); regcpblow(lastcp)
165 assert(i == SAVEt_REGCONTEXT);
167 input = (char *) SSPOPPTR;
168 *PL_reglastparen = SSPOPINT;
169 PL_regsize = SSPOPINT;
170 for (i -= 3; i > 0; i -= 4) {
171 paren = (U32)SSPOPINT;
172 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
173 PL_regstartp[paren] = SSPOPINT;
175 if (paren <= *PL_reglastparen)
176 PL_regendp[paren] = tmps;
178 PerlIO_printf(Perl_debug_log,
179 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
180 (UV)paren, (IV)PL_regstartp[paren],
181 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
182 (IV)PL_regendp[paren],
183 (paren > *PL_reglastparen ? "(no)" : ""));
187 if (*PL_reglastparen + 1 <= PL_regnpar) {
188 PerlIO_printf(Perl_debug_log,
189 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
190 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
193 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
194 if (paren > PL_regsize)
195 PL_regstartp[paren] = -1;
196 PL_regendp[paren] = -1;
202 S_regcp_set_to(pTHX_ I32 ss)
205 I32 tmp = PL_savestack_ix;
207 PL_savestack_ix = ss;
209 PL_savestack_ix = tmp;
213 typedef struct re_cc_state
217 struct re_cc_state *prev;
222 #define regcpblow(cp) LEAVE_SCOPE(cp)
225 * pregexec and friends
229 - pregexec - match a regexp against a string
232 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
233 char *strbeg, I32 minend, SV *screamer, U32 nosave)
234 /* strend: pointer to null at end of string */
235 /* strbeg: real beginning of string */
236 /* minend: end of match must be >=minend after stringarg. */
237 /* nosave: For optimizations. */
240 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
241 nosave ? 0 : REXEC_COPY_STR);
245 S_cache_re(pTHX_ regexp *prog)
248 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
250 PL_regprogram = prog->program;
252 PL_regnpar = prog->nparens;
253 PL_regdata = prog->data;
257 static char *find_byclass(regexp * prog, regnode *c, char *s, char *strend,
258 char *startpos, I32 norun);
261 * Need to implement the following flags for reg_anch:
263 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
265 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
266 * INTUIT_AUTORITATIVE_ML
267 * INTUIT_ONCE_NOML - Intuit can match in one location only.
270 * Another flag for this function: SECOND_TIME (so that float substrs
271 * with giant delta may be not rechecked).
274 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
276 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
277 Otherwise, only SvCUR(sv) is used to get strbeg. */
279 /* XXXX We assume that strpos is strbeg unless sv. */
281 /* XXXX Some places assume that there is a fixed substring.
282 An update may be needed if optimizer marks as "INTUITable"
283 RExen without fixed substrings. Similarly, it is assumed that
284 lengths of all the strings are no more than minlen, thus they
285 cannot come from lookahead.
286 (Or minlen should take into account lookahead.) */
288 /* A failure to find a constant substring means that there is no need to make
289 an expensive call to REx engine, thus we celebrate a failure. Similarly,
290 finding a substring too deep into the string means that less calls to
291 regtry() should be needed.
293 REx compiler's optimizer found 4 possible hints:
294 a) Anchored substring;
296 c) Whether we are anchored (beginning-of-line or \G);
297 d) First node (of those at offset 0) which may distingush positions;
298 We use a)b)d) and multiline-part of c), and try to find a position in the
299 string which does not contradict any of them.
302 /* Most of decisions we do here should have been done at compile time.
303 The nodes of the REx which we used for the search should have been
304 deleted from the finite automaton. */
307 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
308 char *strend, U32 flags, re_scream_pos_data *data)
310 register I32 start_shift;
311 /* Should be nonnegative! */
312 register I32 end_shift;
318 register char *other_last = Nullch; /* other substr checked before this */
319 char *check_at; /* check substr found at this pos */
321 char *i_strpos = strpos;
324 DEBUG_r( if (!PL_colorset) reginitcolors() );
325 DEBUG_r(PerlIO_printf(Perl_debug_log,
326 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
327 PL_colors[4],PL_colors[5],PL_colors[0],
330 (strlen(prog->precomp) > 60 ? "..." : ""),
332 (int)(strend - strpos > 60 ? 60 : strend - strpos),
333 strpos, PL_colors[1],
334 (strend - strpos > 60 ? "..." : ""))
337 if (prog->minlen > strend - strpos) {
338 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
341 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
342 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
343 || ( (prog->reganch & ROPT_ANCH_BOL)
344 && !PL_multiline ) ); /* Check after \n? */
346 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
347 /* Substring at constant offset from beg-of-str... */
350 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
351 && (sv && (strpos + SvCUR(sv) != strend)) ) {
352 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
355 PL_regeol = strend; /* Used in HOP() */
356 s = HOPc(strpos, prog->check_offset_min);
357 if (SvTAIL(prog->check_substr)) {
358 slen = SvCUR(prog->check_substr); /* >= 1 */
360 if ( strend - s > slen || strend - s < slen - 1
361 || (strend - s == slen && strend[-1] != '\n')) {
362 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
365 /* Now should match s[0..slen-2] */
367 if (slen && (*SvPVX(prog->check_substr) != *s
369 && memNE(SvPVX(prog->check_substr), s, slen)))) {
371 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
375 else if (*SvPVX(prog->check_substr) != *s
376 || ((slen = SvCUR(prog->check_substr)) > 1
377 && memNE(SvPVX(prog->check_substr), s, slen)))
379 goto success_at_start;
381 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
383 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
384 /* Should be nonnegative! */
385 end_shift = prog->minlen - start_shift -
386 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
388 I32 end = prog->check_offset_max + CHR_SVLEN(prog->check_substr)
389 - (SvTAIL(prog->check_substr) != 0);
390 I32 eshift = strend - s - end;
392 if (end_shift < eshift)
396 else { /* Can match at random position */
399 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
400 /* Should be nonnegative! */
401 end_shift = prog->minlen - start_shift -
402 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
405 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
407 Perl_croak(aTHX_ "panic: end_shift");
410 check = prog->check_substr;
412 /* Find a possible match in the region s..strend by looking for
413 the "check" substring in the region corrected by start/end_shift. */
414 if (flags & REXEC_SCREAM) {
415 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
416 I32 p = -1; /* Internal iterator of scream. */
417 I32 *pp = data ? data->scream_pos : &p;
419 if (PL_screamfirst[BmRARE(check)] >= 0
420 || ( BmRARE(check) == '\n'
421 && (BmPREVIOUS(check) == SvCUR(check) - 1)
423 s = screaminstr(sv, check,
424 start_shift + (s - strbeg), end_shift, pp, 0);
428 *data->scream_olds = s;
431 s = fbm_instr((unsigned char*)s + start_shift,
432 (unsigned char*)strend - end_shift,
433 check, PL_multiline ? FBMrf_MULTILINE : 0);
435 /* Update the count-of-usability, remove useless subpatterns,
438 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
439 (s ? "Found" : "Did not find"),
440 ((check == prog->anchored_substr) ? "anchored" : "floating"),
442 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
444 PL_colors[1], (SvTAIL(check) ? "$" : ""),
445 (s ? " at offset " : "...\n") ) );
452 /* Finish the diagnostic message */
453 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
455 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
456 Start with the other substr.
457 XXXX no SCREAM optimization yet - and a very coarse implementation
458 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
459 *always* match. Probably should be marked during compile...
460 Probably it is right to do no SCREAM here...
463 if (prog->float_substr && prog->anchored_substr) {
464 /* Take into account the "other" substring. */
465 /* XXXX May be hopelessly wrong for UTF... */
468 if (check == prog->float_substr) {
471 char *last = s - start_shift, *last1, *last2;
475 t = s - prog->check_offset_max;
476 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
477 && (!(prog->reganch & ROPT_UTF8)
478 || (PL_bostr = strpos, /* Used in regcopmaybe() */
479 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
484 t += prog->anchored_offset;
485 if (t < other_last) /* These positions already checked */
488 last2 = last1 = strend - prog->minlen;
491 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
492 /* On end-of-str: see comment below. */
493 s = fbm_instr((unsigned char*)t,
494 (unsigned char*)last1 + prog->anchored_offset
495 + SvCUR(prog->anchored_substr)
496 - (SvTAIL(prog->anchored_substr)!=0),
497 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
498 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
499 (s ? "Found" : "Contradicts"),
501 (int)(SvCUR(prog->anchored_substr)
502 - (SvTAIL(prog->anchored_substr)!=0)),
503 SvPVX(prog->anchored_substr),
504 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
506 if (last1 >= last2) {
507 DEBUG_r(PerlIO_printf(Perl_debug_log,
508 ", giving up...\n"));
511 DEBUG_r(PerlIO_printf(Perl_debug_log,
512 ", trying floating at offset %ld...\n",
513 (long)(s1 + 1 - i_strpos)));
514 PL_regeol = strend; /* Used in HOP() */
515 other_last = last1 + prog->anchored_offset + 1;
520 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
521 (long)(s - i_strpos)));
522 t = s - prog->anchored_offset;
531 else { /* Take into account the floating substring. */
536 last1 = last = strend - prog->minlen + prog->float_min_offset;
537 if (last - t > prog->float_max_offset)
538 last = t + prog->float_max_offset;
539 s = t + prog->float_min_offset;
542 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
543 /* fbm_instr() takes into account exact value of end-of-str
544 if the check is SvTAIL(ed). Since false positives are OK,
545 and end-of-str is not later than strend we are OK. */
546 s = fbm_instr((unsigned char*)s,
547 (unsigned char*)last + SvCUR(prog->float_substr)
548 - (SvTAIL(prog->float_substr)!=0),
549 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
550 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
551 (s ? "Found" : "Contradicts"),
553 (int)(SvCUR(prog->float_substr)
554 - (SvTAIL(prog->float_substr)!=0)),
555 SvPVX(prog->float_substr),
556 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
559 DEBUG_r(PerlIO_printf(Perl_debug_log,
560 ", giving up...\n"));
563 DEBUG_r(PerlIO_printf(Perl_debug_log,
564 ", trying anchored starting at offset %ld...\n",
565 (long)(s1 + 1 - i_strpos)));
566 other_last = last + 1;
567 PL_regeol = strend; /* Used in HOP() */
572 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
573 (long)(s - i_strpos)));
583 t = s - prog->check_offset_max;
585 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
586 && (!(prog->reganch & ROPT_UTF8)
587 || (PL_bostr = strpos, /* Used in regcopmaybe() */
588 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
591 /* Fixed substring is found far enough so that the match
592 cannot start at strpos. */
594 if (ml_anch && t[-1] != '\n') {
595 /* Eventually fbm_*() should handle this, but often
596 anchored_offset is not 0, so this check will not be wasted. */
597 /* XXXX In the code below we prefer to look for "^" even in
598 presence of anchored substrings. And we search even
599 beyond the found float position. These pessimizations
600 are historical artefacts only. */
602 while (t < strend - prog->minlen) {
604 if (t < s - prog->check_offset_min) {
605 if (prog->anchored_substr) {
606 /* We definitely contradict the found anchored
607 substr. Due to the above check we do not
608 contradict "check" substr.
609 Thus we can arrive here only if check substr
610 is float. Redo checking for "other"=="fixed".
613 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
614 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
615 goto do_other_anchored;
618 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
619 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
622 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting at offset %ld...\n",
623 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
629 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
630 PL_colors[0],PL_colors[1]));
635 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
639 /* The found string does not prohibit matching at beg-of-str
640 - no optimization of calling REx engine can be performed,
641 unless it was an MBOL and we are not after MBOL. */
643 /* Even in this situation we may use MBOL flag if strpos is offset
644 wrt the start of the string. */
646 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
650 DEBUG_r( if (ml_anch)
651 PerlIO_printf(Perl_debug_log, "Does not contradict /%s^%s/m...\n",
652 PL_colors[0],PL_colors[1]);
655 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
656 && --BmUSEFUL(prog->check_substr) < 0
657 && prog->check_substr == prog->float_substr) { /* boo */
658 /* If flags & SOMETHING - do not do it many times on the same match */
659 SvREFCNT_dec(prog->check_substr);
660 prog->check_substr = Nullsv; /* disable */
661 prog->float_substr = Nullsv; /* clear */
663 /* XXXX This is a remnant of the old implementation. It
664 looks wasteful, since now INTUIT can use many
666 prog->reganch &= ~RE_USE_INTUIT;
673 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
674 if (prog->regstclass) {
675 /* minlen == 0 is possible if regstclass is \b or \B,
676 and the fixed substr is ''$.
677 Since minlen is already taken into account, s+1 is before strend;
678 accidentally, minlen >= 1 guaranties no false positives at s + 1
679 even for \b or \B. But (minlen? 1 : 0) below assumes that
680 regstclass does not come from lookahead... */
681 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
682 This leaves EXACTF only, which is dealt with in find_byclass(). */
683 char *endpos = (prog->anchored_substr || ml_anch)
684 ? s + (prog->minlen? 1 : 0)
685 : (prog->float_substr ? check_at - start_shift + 1
687 char *startpos = sv ? strend - SvCUR(sv) : s;
690 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
695 if (endpos == strend) {
696 DEBUG_r( PerlIO_printf(Perl_debug_log,
697 "Could not match STCLASS...\n") );
700 /* Contradict one of substrings */
701 if (prog->anchored_substr) {
702 DEBUG_r( PerlIO_printf(Perl_debug_log,
703 "This position contradicts STCLASS...\n") );
704 if (prog->anchored_substr == check) {
705 DEBUG_r( what = "anchored" );
707 PL_regeol = strend; /* Used in HOP() */
709 DEBUG_r( PerlIO_printf(Perl_debug_log,
710 "trying %s substr starting at offset %ld...\n",
711 what, (long)(s + start_shift - i_strpos)) );
714 /* Have both, check is floating */
715 if (t + start_shift >= check_at) /* Contradicts floating=check */
716 goto retry_floating_check;
717 /* Recheck anchored substring, but not floating... */
719 DEBUG_r( PerlIO_printf(Perl_debug_log,
720 "trying anchored substr starting at offset %ld...\n",
721 (long)(other_last - i_strpos)) );
722 goto do_other_anchored;
724 /* Check is floating subtring. */
725 retry_floating_check:
726 t = check_at - start_shift;
727 DEBUG_r( what = "floating" );
728 goto hop_and_restart;
731 PerlIO_printf(Perl_debug_log,
732 "By STCLASS: moving %ld --> %ld\n",
733 (long)(t - i_strpos), (long)(s - i_strpos));
735 PerlIO_printf(Perl_debug_log,
736 "Does not contradict STCLASS...\n") );
738 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sGuessed:%s match at offset %ld\n",
739 PL_colors[4], PL_colors[5], (long)(s - i_strpos)) );
742 fail_finish: /* Substring not found */
743 BmUSEFUL(prog->check_substr) += 5; /* hooray */
745 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
746 PL_colors[4],PL_colors[5]));
750 /* We know what class REx starts with. Try to find this position... */
752 find_byclass(regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
754 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
760 register I32 tmp = 1; /* Scratch variable? */
762 /* We know what class it must start with. */
766 if (REGINCLASSUTF8(c, (U8*)s)) {
767 if (tmp && (norun || regtry(prog, s)))
779 if (REGINCLASS(c, *s)) {
780 if (tmp && (norun || regtry(prog, s)))
800 c2 = PL_fold_locale[c1];
805 e = s; /* Due to minlen logic of intuit() */
806 /* Here it is NOT UTF! */
810 && (ln == 1 || (OP(c) == EXACTF
812 : ibcmp_locale(s, m, ln)))
813 && (norun || regtry(prog, s)) )
819 if ( (*s == c1 || *s == c2)
820 && (ln == 1 || (OP(c) == EXACTF
822 : ibcmp_locale(s, m, ln)))
823 && (norun || regtry(prog, s)) )
830 PL_reg_flags |= RF_tainted;
833 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
834 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
836 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
838 if ((norun || regtry(prog, s)))
843 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
847 PL_reg_flags |= RF_tainted;
850 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
851 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
853 if (tmp == !(OP(c) == BOUND ?
854 swash_fetch(PL_utf8_alnum, (U8*)s) :
855 isALNUM_LC_utf8((U8*)s)))
858 if ((norun || regtry(prog, s)))
863 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
867 PL_reg_flags |= RF_tainted;
870 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
871 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
873 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
875 else if ((norun || regtry(prog, s)))
879 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
883 PL_reg_flags |= RF_tainted;
887 strend = reghop_c(strend, -1);
888 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
889 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
891 if (tmp == !(OP(c) == NBOUND ?
892 swash_fetch(PL_utf8_alnum, (U8*)s) :
893 isALNUM_LC_utf8((U8*)s)))
895 else if ((norun || regtry(prog, s)))
899 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
905 if (tmp && (norun || regtry(prog, s)))
917 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
918 if (tmp && (norun || regtry(prog, s)))
929 PL_reg_flags |= RF_tainted;
931 if (isALNUM_LC(*s)) {
932 if (tmp && (norun || regtry(prog, s)))
943 PL_reg_flags |= RF_tainted;
945 if (isALNUM_LC_utf8((U8*)s)) {
946 if (tmp && (norun || regtry(prog, s)))
959 if (tmp && (norun || regtry(prog, s)))
971 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
972 if (tmp && (norun || regtry(prog, s)))
983 PL_reg_flags |= RF_tainted;
985 if (!isALNUM_LC(*s)) {
986 if (tmp && (norun || regtry(prog, s)))
997 PL_reg_flags |= RF_tainted;
999 if (!isALNUM_LC_utf8((U8*)s)) {
1000 if (tmp && (norun || regtry(prog, s)))
1011 while (s < strend) {
1013 if (tmp && (norun || regtry(prog, s)))
1024 while (s < strend) {
1025 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1026 if (tmp && (norun || regtry(prog, s)))
1037 PL_reg_flags |= RF_tainted;
1038 while (s < strend) {
1039 if (isSPACE_LC(*s)) {
1040 if (tmp && (norun || regtry(prog, s)))
1051 PL_reg_flags |= RF_tainted;
1052 while (s < strend) {
1053 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1054 if (tmp && (norun || regtry(prog, s)))
1065 while (s < strend) {
1067 if (tmp && (norun || regtry(prog, s)))
1078 while (s < strend) {
1079 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1080 if (tmp && (norun || regtry(prog, s)))
1091 PL_reg_flags |= RF_tainted;
1092 while (s < strend) {
1093 if (!isSPACE_LC(*s)) {
1094 if (tmp && (norun || regtry(prog, s)))
1105 PL_reg_flags |= RF_tainted;
1106 while (s < strend) {
1107 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1108 if (tmp && (norun || regtry(prog, s)))
1119 while (s < strend) {
1121 if (tmp && (norun || regtry(prog, s)))
1132 while (s < strend) {
1133 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1134 if (tmp && (norun || regtry(prog, s)))
1145 PL_reg_flags |= RF_tainted;
1146 while (s < strend) {
1147 if (isDIGIT_LC(*s)) {
1148 if (tmp && (norun || regtry(prog, s)))
1159 PL_reg_flags |= RF_tainted;
1160 while (s < strend) {
1161 if (isDIGIT_LC_utf8((U8*)s)) {
1162 if (tmp && (norun || regtry(prog, s)))
1173 while (s < strend) {
1175 if (tmp && (norun || regtry(prog, s)))
1186 while (s < strend) {
1187 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1188 if (tmp && (norun || regtry(prog, s)))
1199 PL_reg_flags |= RF_tainted;
1200 while (s < strend) {
1201 if (!isDIGIT_LC(*s)) {
1202 if (tmp && (norun || regtry(prog, s)))
1213 PL_reg_flags |= RF_tainted;
1214 while (s < strend) {
1215 if (!isDIGIT_LC_utf8((U8*)s)) {
1216 if (tmp && (norun || regtry(prog, s)))
1229 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1239 - regexec_flags - match a regexp against a string
1242 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1243 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1244 /* strend: pointer to null at end of string */
1245 /* strbeg: real beginning of string */
1246 /* minend: end of match must be >=minend after stringarg. */
1247 /* data: May be used for some additional optimizations. */
1248 /* nosave: For optimizations. */
1252 register regnode *c;
1253 register char *startpos = stringarg;
1255 I32 minlen; /* must match at least this many chars */
1256 I32 dontbother = 0; /* how many characters not to try at end */
1257 I32 start_shift = 0; /* Offset of the start to find
1258 constant substr. */ /* CC */
1259 I32 end_shift = 0; /* Same for the end. */ /* CC */
1260 I32 scream_pos = -1; /* Internal iterator of scream. */
1262 SV* oreplsv = GvSV(PL_replgv);
1268 PL_regnarrate = PL_debug & 512;
1271 /* Be paranoid... */
1272 if (prog == NULL || startpos == NULL) {
1273 Perl_croak(aTHX_ "NULL regexp parameter");
1277 minlen = prog->minlen;
1278 if (strend - startpos < minlen) goto phooey;
1280 if (startpos == strbeg) /* is ^ valid at stringarg? */
1283 PL_regprev = (U32)stringarg[-1];
1284 if (!PL_multiline && PL_regprev == '\n')
1285 PL_regprev = '\0'; /* force ^ to NOT match */
1288 /* Check validity of program. */
1289 if (UCHARAT(prog->program) != REG_MAGIC) {
1290 Perl_croak(aTHX_ "corrupted regexp program");
1294 PL_reg_eval_set = 0;
1297 if (prog->reganch & ROPT_UTF8)
1298 PL_reg_flags |= RF_utf8;
1300 /* Mark beginning of line for ^ and lookbehind. */
1301 PL_regbol = startpos;
1305 /* Mark end of line for $ (and such) */
1308 /* see how far we have to get to not match where we matched before */
1309 PL_regtill = startpos+minend;
1311 /* We start without call_cc context. */
1314 /* If there is a "must appear" string, look for it. */
1317 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1320 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1321 PL_reg_ganch = startpos;
1322 else if (sv && SvTYPE(sv) >= SVt_PVMG
1324 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1325 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1326 if (prog->reganch & ROPT_ANCH_GPOS) {
1327 if (s > PL_reg_ganch)
1332 else /* pos() not defined */
1333 PL_reg_ganch = strbeg;
1336 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1337 re_scream_pos_data d;
1339 d.scream_olds = &scream_olds;
1340 d.scream_pos = &scream_pos;
1341 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1343 goto phooey; /* not present */
1346 DEBUG_r( if (!PL_colorset) reginitcolors() );
1347 DEBUG_r(PerlIO_printf(Perl_debug_log,
1348 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1349 PL_colors[4],PL_colors[5],PL_colors[0],
1352 (strlen(prog->precomp) > 60 ? "..." : ""),
1354 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1355 startpos, PL_colors[1],
1356 (strend - startpos > 60 ? "..." : ""))
1359 /* Simplest case: anchored match need be tried only once. */
1360 /* [unless only anchor is BOL and multiline is set] */
1361 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1362 if (s == startpos && regtry(prog, startpos))
1364 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1365 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1370 dontbother = minlen - 1;
1371 end = HOPc(strend, -dontbother) - 1;
1372 /* for multiline we only have to try after newlines */
1373 if (prog->check_substr) {
1377 if (regtry(prog, s))
1382 if (prog->reganch & RE_USE_INTUIT) {
1383 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1394 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1395 if (regtry(prog, s))
1402 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1403 if (regtry(prog, PL_reg_ganch))
1408 /* Messy cases: unanchored match. */
1409 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1410 /* we have /x+whatever/ */
1411 /* it must be a one character string (XXXX Except UTF?) */
1412 char ch = SvPVX(prog->anchored_substr)[0];
1414 while (s < strend) {
1416 if (regtry(prog, s)) goto got_it;
1418 while (s < strend && *s == ch)
1425 while (s < strend) {
1427 if (regtry(prog, s)) goto got_it;
1429 while (s < strend && *s == ch)
1437 else if (prog->anchored_substr != Nullsv
1438 || (prog->float_substr != Nullsv
1439 && prog->float_max_offset < strend - s)) {
1440 SV *must = prog->anchored_substr
1441 ? prog->anchored_substr : prog->float_substr;
1443 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1445 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1446 I32 delta = back_max - back_min;
1447 char *last = HOPc(strend, /* Cannot start after this */
1448 -(I32)(CHR_SVLEN(must)
1449 - (SvTAIL(must) != 0) + back_min));
1450 char *last1; /* Last position checked before */
1453 last1 = HOPc(s, -1);
1455 last1 = s - 1; /* bogus */
1457 /* XXXX check_substr already used to find `s', can optimize if
1458 check_substr==must. */
1460 dontbother = end_shift;
1461 strend = HOPc(strend, -dontbother);
1462 while ( (s <= last) &&
1463 ((flags & REXEC_SCREAM)
1464 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1465 end_shift, &scream_pos, 0))
1466 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1467 (unsigned char*)strend, must,
1468 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1469 if (HOPc(s, -back_max) > last1) {
1470 last1 = HOPc(s, -back_min);
1471 s = HOPc(s, -back_max);
1474 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1476 last1 = HOPc(s, -back_min);
1480 while (s <= last1) {
1481 if (regtry(prog, s))
1487 while (s <= last1) {
1488 if (regtry(prog, s))
1496 else if (c = prog->regstclass) {
1497 if (minlen) /* don't bother with what can't match */
1498 strend = HOPc(strend, -(minlen - 1));
1499 if (find_byclass(prog, c, s, strend, startpos, 0))
1504 if (prog->float_substr != Nullsv) { /* Trim the end. */
1506 I32 oldpos = scream_pos;
1508 if (flags & REXEC_SCREAM) {
1509 last = screaminstr(sv, prog->float_substr, s - strbeg,
1510 end_shift, &scream_pos, 1); /* last one */
1512 last = scream_olds; /* Only one occurence. */
1516 char *little = SvPV(prog->float_substr, len);
1518 if (SvTAIL(prog->float_substr)) {
1519 if (memEQ(strend - len + 1, little, len - 1))
1520 last = strend - len + 1;
1521 else if (!PL_multiline)
1522 last = memEQ(strend - len, little, len)
1523 ? strend - len : Nullch;
1529 last = rninstr(s, strend, little, little + len);
1531 last = strend; /* matching `$' */
1534 if (last == NULL) goto phooey; /* Should not happen! */
1535 dontbother = strend - last + prog->float_min_offset;
1537 if (minlen && (dontbother < minlen))
1538 dontbother = minlen - 1;
1539 strend -= dontbother; /* this one's always in bytes! */
1540 /* We don't know much -- general case. */
1543 if (regtry(prog, s))
1552 if (regtry(prog, s))
1554 } while (s++ < strend);
1562 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1564 if (PL_reg_eval_set) {
1565 /* Preserve the current value of $^R */
1566 if (oreplsv != GvSV(PL_replgv))
1567 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1568 restored, the value remains
1570 restore_pos(aTHXo_ 0);
1573 /* make sure $`, $&, $', and $digit will work later */
1574 if ( !(flags & REXEC_NOT_FIRST) ) {
1575 if (RX_MATCH_COPIED(prog)) {
1576 Safefree(prog->subbeg);
1577 RX_MATCH_COPIED_off(prog);
1579 if (flags & REXEC_COPY_STR) {
1580 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1582 s = savepvn(strbeg, i);
1585 RX_MATCH_COPIED_on(prog);
1588 prog->subbeg = strbeg;
1589 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1596 if (PL_reg_eval_set)
1597 restore_pos(aTHXo_ 0);
1602 - regtry - try match at specific point
1604 STATIC I32 /* 0 failure, 1 success */
1605 S_regtry(pTHX_ regexp *prog, char *startpos)
1613 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1616 PL_reg_eval_set = RS_init;
1618 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1619 (IV)(PL_stack_sp - PL_stack_base));
1621 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1622 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1623 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1625 /* Apparently this is not needed, judging by wantarray. */
1626 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1627 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1630 /* Make $_ available to executed code. */
1631 if (PL_reg_sv != DEFSV) {
1632 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1637 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1638 && (mg = mg_find(PL_reg_sv, 'g')))) {
1639 /* prepare for quick setting of pos */
1640 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1641 mg = mg_find(PL_reg_sv, 'g');
1645 PL_reg_oldpos = mg->mg_len;
1646 SAVEDESTRUCTOR_X(restore_pos, 0);
1649 New(22,PL_reg_curpm, 1, PMOP);
1650 PL_reg_curpm->op_pmregexp = prog;
1651 PL_reg_oldcurpm = PL_curpm;
1652 PL_curpm = PL_reg_curpm;
1653 if (RX_MATCH_COPIED(prog)) {
1654 /* Here is a serious problem: we cannot rewrite subbeg,
1655 since it may be needed if this match fails. Thus
1656 $` inside (?{}) could fail... */
1657 PL_reg_oldsaved = prog->subbeg;
1658 PL_reg_oldsavedlen = prog->sublen;
1659 RX_MATCH_COPIED_off(prog);
1662 PL_reg_oldsaved = Nullch;
1663 prog->subbeg = PL_bostr;
1664 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1666 prog->startp[0] = startpos - PL_bostr;
1667 PL_reginput = startpos;
1668 PL_regstartp = prog->startp;
1669 PL_regendp = prog->endp;
1670 PL_reglastparen = &prog->lastparen;
1671 prog->lastparen = 0;
1673 DEBUG_r(PL_reg_starttry = startpos);
1674 if (PL_reg_start_tmpl <= prog->nparens) {
1675 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1676 if(PL_reg_start_tmp)
1677 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1679 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1682 /* XXXX What this code is doing here?!!! There should be no need
1683 to do this again and again, PL_reglastparen should take care of
1687 if (prog->nparens) {
1688 for (i = prog->nparens; i >= 1; i--) {
1694 if (regmatch(prog->program + 1)) {
1695 prog->endp[0] = PL_reginput - PL_bostr;
1703 - regmatch - main matching routine
1705 * Conceptually the strategy is simple: check to see whether the current
1706 * node matches, call self recursively to see whether the rest matches,
1707 * and then act accordingly. In practice we make some effort to avoid
1708 * recursion, in particular by going through "ordinary" nodes (that don't
1709 * need to know whether the rest of the match failed) by a loop instead of
1712 /* [lwall] I've hoisted the register declarations to the outer block in order to
1713 * maybe save a little bit of pushing and popping on the stack. It also takes
1714 * advantage of machines that use a register save mask on subroutine entry.
1716 STATIC I32 /* 0 failure, 1 success */
1717 S_regmatch(pTHX_ regnode *prog)
1720 register regnode *scan; /* Current node. */
1721 regnode *next; /* Next node. */
1722 regnode *inner; /* Next node in internal branch. */
1723 register I32 nextchr; /* renamed nextchr - nextchar colides with
1724 function of same name */
1725 register I32 n; /* no or next */
1726 register I32 ln; /* len or last */
1727 register char *s; /* operand or save */
1728 register char *locinput = PL_reginput;
1729 register I32 c1, c2, paren; /* case fold search, parenth */
1730 int minmod = 0, sw = 0, logical = 0;
1735 /* Note that nextchr is a byte even in UTF */
1736 nextchr = UCHARAT(locinput);
1738 while (scan != NULL) {
1739 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1741 # define sayYES goto yes
1742 # define sayNO goto no
1743 # define sayYES_FINAL goto yes_final
1744 # define sayYES_LOUD goto yes_loud
1745 # define sayNO_FINAL goto no_final
1746 # define sayNO_SILENT goto do_no
1747 # define saySAME(x) if (x) goto yes; else goto no
1748 # define REPORT_CODE_OFF 24
1750 # define sayYES return 1
1751 # define sayNO return 0
1752 # define sayYES_FINAL return 1
1753 # define sayYES_LOUD return 1
1754 # define sayNO_FINAL return 0
1755 # define sayNO_SILENT return 0
1756 # define saySAME(x) return x
1759 SV *prop = sv_newmortal();
1760 int docolor = *PL_colors[0];
1761 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1762 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1763 /* The part of the string before starttry has one color
1764 (pref0_len chars), between starttry and current
1765 position another one (pref_len - pref0_len chars),
1766 after the current position the third one.
1767 We assume that pref0_len <= pref_len, otherwise we
1768 decrease pref0_len. */
1769 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1770 ? (5 + taill) - l : locinput - PL_bostr);
1771 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1773 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1774 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1775 ? (5 + taill) - pref_len : PL_regeol - locinput);
1778 if (pref0_len > pref_len)
1779 pref0_len = pref_len;
1780 regprop(prop, scan);
1781 PerlIO_printf(Perl_debug_log,
1782 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1783 (IV)(locinput - PL_bostr),
1784 PL_colors[4], pref0_len,
1785 locinput - pref_len, PL_colors[5],
1786 PL_colors[2], pref_len - pref0_len,
1787 locinput - pref_len + pref0_len, PL_colors[3],
1788 (docolor ? "" : "> <"),
1789 PL_colors[0], l, locinput, PL_colors[1],
1790 15 - l - pref_len + 1,
1792 (IV)(scan - PL_regprogram), PL_regindent*2, "",
1796 next = scan + NEXT_OFF(scan);
1802 if (locinput == PL_bostr
1803 ? PL_regprev == '\n'
1805 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1807 /* regtill = regbol; */
1812 if (locinput == PL_bostr
1813 ? PL_regprev == '\n'
1814 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1820 if (locinput == PL_regbol && PL_regprev == '\n')
1824 if (locinput == PL_reg_ganch)
1834 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1839 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1841 if (PL_regeol - locinput > 1)
1845 if (PL_regeol != locinput)
1849 if (nextchr & 0x80) {
1850 locinput += PL_utf8skip[nextchr];
1851 if (locinput > PL_regeol)
1853 nextchr = UCHARAT(locinput);
1856 if (!nextchr && locinput >= PL_regeol)
1858 nextchr = UCHARAT(++locinput);
1861 if (!nextchr && locinput >= PL_regeol)
1863 nextchr = UCHARAT(++locinput);
1866 if (nextchr & 0x80) {
1867 locinput += PL_utf8skip[nextchr];
1868 if (locinput > PL_regeol)
1870 nextchr = UCHARAT(locinput);
1873 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1875 nextchr = UCHARAT(++locinput);
1878 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1880 nextchr = UCHARAT(++locinput);
1885 /* Inline the first character, for speed. */
1886 if (UCHARAT(s) != nextchr)
1888 if (PL_regeol - locinput < ln)
1890 if (ln > 1 && memNE(s, locinput, ln))
1893 nextchr = UCHARAT(locinput);
1896 PL_reg_flags |= RF_tainted;
1905 c1 = OP(scan) == EXACTF;
1909 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1910 toLOWER_utf8((U8*)l) :
1911 toLOWER_LC_utf8((U8*)l)))
1919 nextchr = UCHARAT(locinput);
1923 /* Inline the first character, for speed. */
1924 if (UCHARAT(s) != nextchr &&
1925 UCHARAT(s) != ((OP(scan) == EXACTF)
1926 ? PL_fold : PL_fold_locale)[nextchr])
1928 if (PL_regeol - locinput < ln)
1930 if (ln > 1 && (OP(scan) == EXACTF
1931 ? ibcmp(s, locinput, ln)
1932 : ibcmp_locale(s, locinput, ln)))
1935 nextchr = UCHARAT(locinput);
1938 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1940 if (locinput >= PL_regeol)
1942 locinput += PL_utf8skip[nextchr];
1943 nextchr = UCHARAT(locinput);
1947 nextchr = UCHARAT(locinput);
1948 if (!REGINCLASS(scan, nextchr))
1950 if (!nextchr && locinput >= PL_regeol)
1952 nextchr = UCHARAT(++locinput);
1955 PL_reg_flags |= RF_tainted;
1960 if (!(OP(scan) == ALNUM
1961 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1963 nextchr = UCHARAT(++locinput);
1966 PL_reg_flags |= RF_tainted;
1971 if (nextchr & 0x80) {
1972 if (!(OP(scan) == ALNUMUTF8
1973 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1974 : isALNUM_LC_utf8((U8*)locinput)))
1978 locinput += PL_utf8skip[nextchr];
1979 nextchr = UCHARAT(locinput);
1982 if (!(OP(scan) == ALNUMUTF8
1983 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1985 nextchr = UCHARAT(++locinput);
1988 PL_reg_flags |= RF_tainted;
1991 if (!nextchr && locinput >= PL_regeol)
1993 if (OP(scan) == NALNUM
1994 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1996 nextchr = UCHARAT(++locinput);
1999 PL_reg_flags |= RF_tainted;
2002 if (!nextchr && locinput >= PL_regeol)
2004 if (nextchr & 0x80) {
2005 if (OP(scan) == NALNUMUTF8
2006 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2007 : isALNUM_LC_utf8((U8*)locinput))
2011 locinput += PL_utf8skip[nextchr];
2012 nextchr = UCHARAT(locinput);
2015 if (OP(scan) == NALNUMUTF8
2016 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2018 nextchr = UCHARAT(++locinput);
2022 PL_reg_flags |= RF_tainted;
2026 /* was last char in word? */
2027 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2028 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2030 n = isALNUM(nextchr);
2033 ln = isALNUM_LC(ln);
2034 n = isALNUM_LC(nextchr);
2036 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2041 PL_reg_flags |= RF_tainted;
2045 /* was last char in word? */
2046 ln = (locinput != PL_regbol)
2047 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2048 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2049 ln = isALNUM_uni(ln);
2050 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2053 ln = isALNUM_LC_uni(ln);
2054 n = isALNUM_LC_utf8((U8*)locinput);
2056 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2060 PL_reg_flags |= RF_tainted;
2063 if (!nextchr && locinput >= PL_regeol)
2065 if (!(OP(scan) == SPACE
2066 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2068 nextchr = UCHARAT(++locinput);
2071 PL_reg_flags |= RF_tainted;
2074 if (!nextchr && locinput >= PL_regeol)
2076 if (nextchr & 0x80) {
2077 if (!(OP(scan) == SPACEUTF8
2078 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2079 : isSPACE_LC_utf8((U8*)locinput)))
2083 locinput += PL_utf8skip[nextchr];
2084 nextchr = UCHARAT(locinput);
2087 if (!(OP(scan) == SPACEUTF8
2088 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2090 nextchr = UCHARAT(++locinput);
2093 PL_reg_flags |= RF_tainted;
2098 if (OP(scan) == SPACE
2099 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2101 nextchr = UCHARAT(++locinput);
2104 PL_reg_flags |= RF_tainted;
2109 if (nextchr & 0x80) {
2110 if (OP(scan) == NSPACEUTF8
2111 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2112 : isSPACE_LC_utf8((U8*)locinput))
2116 locinput += PL_utf8skip[nextchr];
2117 nextchr = UCHARAT(locinput);
2120 if (OP(scan) == NSPACEUTF8
2121 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2123 nextchr = UCHARAT(++locinput);
2126 PL_reg_flags |= RF_tainted;
2129 if (!nextchr && locinput >= PL_regeol)
2131 if (!(OP(scan) == DIGIT
2132 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2134 nextchr = UCHARAT(++locinput);
2137 PL_reg_flags |= RF_tainted;
2142 if (nextchr & 0x80) {
2143 if (OP(scan) == NDIGITUTF8
2144 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2145 : isDIGIT_LC_utf8((U8*)locinput))
2149 locinput += PL_utf8skip[nextchr];
2150 nextchr = UCHARAT(locinput);
2153 if (!isDIGIT(nextchr))
2155 nextchr = UCHARAT(++locinput);
2158 PL_reg_flags |= RF_tainted;
2163 if (OP(scan) == DIGIT
2164 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2166 nextchr = UCHARAT(++locinput);
2169 PL_reg_flags |= RF_tainted;
2172 if (!nextchr && locinput >= PL_regeol)
2174 if (nextchr & 0x80) {
2175 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2177 locinput += PL_utf8skip[nextchr];
2178 nextchr = UCHARAT(locinput);
2181 if (isDIGIT(nextchr))
2183 nextchr = UCHARAT(++locinput);
2186 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2188 locinput += PL_utf8skip[nextchr];
2189 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2190 locinput += UTF8SKIP(locinput);
2191 if (locinput > PL_regeol)
2193 nextchr = UCHARAT(locinput);
2196 PL_reg_flags |= RF_tainted;
2200 n = ARG(scan); /* which paren pair */
2201 ln = PL_regstartp[n];
2202 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2203 if (*PL_reglastparen < n || ln == -1)
2204 sayNO; /* Do not match unless seen CLOSEn. */
2205 if (ln == PL_regendp[n])
2209 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2211 char *e = PL_bostr + PL_regendp[n];
2213 * Note that we can't do the "other character" lookup trick as
2214 * in the 8-bit case (no pun intended) because in Unicode we
2215 * have to map both upper and title case to lower case.
2217 if (OP(scan) == REFF) {
2221 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2231 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2238 nextchr = UCHARAT(locinput);
2242 /* Inline the first character, for speed. */
2243 if (UCHARAT(s) != nextchr &&
2245 (UCHARAT(s) != ((OP(scan) == REFF
2246 ? PL_fold : PL_fold_locale)[nextchr]))))
2248 ln = PL_regendp[n] - ln;
2249 if (locinput + ln > PL_regeol)
2251 if (ln > 1 && (OP(scan) == REF
2252 ? memNE(s, locinput, ln)
2254 ? ibcmp(s, locinput, ln)
2255 : ibcmp_locale(s, locinput, ln))))
2258 nextchr = UCHARAT(locinput);
2269 OP_4tree *oop = PL_op;
2270 COP *ocurcop = PL_curcop;
2271 SV **ocurpad = PL_curpad;
2275 PL_op = (OP_4tree*)PL_regdata->data[n];
2276 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2277 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2278 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2280 CALLRUNOPS(aTHX); /* Scalar context. */
2286 PL_curpad = ocurpad;
2287 PL_curcop = ocurcop;
2289 if (logical == 2) { /* Postponed subexpression. */
2291 MAGIC *mg = Null(MAGIC*);
2293 CHECKPOINT cp, lastcp;
2295 if(SvROK(ret) || SvRMAGICAL(ret)) {
2296 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2299 mg = mg_find(sv, 'r');
2302 re = (regexp *)mg->mg_obj;
2303 (void)ReREFCNT_inc(re);
2307 char *t = SvPV(ret, len);
2309 char *oprecomp = PL_regprecomp;
2310 I32 osize = PL_regsize;
2311 I32 onpar = PL_regnpar;
2314 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2316 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2317 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2318 PL_regprecomp = oprecomp;
2323 PerlIO_printf(Perl_debug_log,
2324 "Entering embedded `%s%.60s%s%s'\n",
2328 (strlen(re->precomp) > 60 ? "..." : ""))
2331 state.prev = PL_reg_call_cc;
2332 state.cc = PL_regcc;
2333 state.re = PL_reg_re;
2337 cp = regcppush(0); /* Save *all* the positions. */
2340 state.ss = PL_savestack_ix;
2341 *PL_reglastparen = 0;
2342 PL_reg_call_cc = &state;
2343 PL_reginput = locinput;
2345 /* XXXX This is too dramatic a measure... */
2348 if (regmatch(re->program + 1)) {
2349 /* Even though we succeeded, we need to restore
2350 global variables, since we may be wrapped inside
2351 SUSPEND, thus the match may be not finished yet. */
2353 /* XXXX Do this only if SUSPENDed? */
2354 PL_reg_call_cc = state.prev;
2355 PL_regcc = state.cc;
2356 PL_reg_re = state.re;
2357 cache_re(PL_reg_re);
2359 /* XXXX This is too dramatic a measure... */
2362 /* These are needed even if not SUSPEND. */
2370 PL_reg_call_cc = state.prev;
2371 PL_regcc = state.cc;
2372 PL_reg_re = state.re;
2373 cache_re(PL_reg_re);
2375 /* XXXX This is too dramatic a measure... */
2384 sv_setsv(save_scalar(PL_replgv), ret);
2388 n = ARG(scan); /* which paren pair */
2389 PL_reg_start_tmp[n] = locinput;
2394 n = ARG(scan); /* which paren pair */
2395 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2396 PL_regendp[n] = locinput - PL_bostr;
2397 if (n > *PL_reglastparen)
2398 *PL_reglastparen = n;
2401 n = ARG(scan); /* which paren pair */
2402 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2405 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2407 next = NEXTOPER(NEXTOPER(scan));
2409 next = scan + ARG(scan);
2410 if (OP(next) == IFTHEN) /* Fake one. */
2411 next = NEXTOPER(NEXTOPER(next));
2415 logical = scan->flags;
2417 /*******************************************************************
2418 PL_regcc contains infoblock about the innermost (...)* loop, and
2419 a pointer to the next outer infoblock.
2421 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2423 1) After matching X, regnode for CURLYX is processed;
2425 2) This regnode creates infoblock on the stack, and calls
2426 regmatch() recursively with the starting point at WHILEM node;
2428 3) Each hit of WHILEM node tries to match A and Z (in the order
2429 depending on the current iteration, min/max of {min,max} and
2430 greediness). The information about where are nodes for "A"
2431 and "Z" is read from the infoblock, as is info on how many times "A"
2432 was already matched, and greediness.
2434 4) After A matches, the same WHILEM node is hit again.
2436 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2437 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2438 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2439 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2440 of the external loop.
2442 Currently present infoblocks form a tree with a stem formed by PL_curcc
2443 and whatever it mentions via ->next, and additional attached trees
2444 corresponding to temporarily unset infoblocks as in "5" above.
2446 In the following picture infoblocks for outer loop of
2447 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2448 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2449 infoblocks are drawn below the "reset" infoblock.
2451 In fact in the picture below we do not show failed matches for Z and T
2452 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2453 more obvious *why* one needs to *temporary* unset infoblocks.]
2455 Matched REx position InfoBlocks Comment
2459 Y A)*?Z)*?T x <- O <- I
2460 YA )*?Z)*?T x <- O <- I
2461 YA A)*?Z)*?T x <- O <- I
2462 YAA )*?Z)*?T x <- O <- I
2463 YAA Z)*?T x <- O # Temporary unset I
2466 YAAZ Y(A)*?Z)*?T x <- O
2469 YAAZY (A)*?Z)*?T x <- O
2472 YAAZY A)*?Z)*?T x <- O <- I
2475 YAAZYA )*?Z)*?T x <- O <- I
2478 YAAZYA Z)*?T x <- O # Temporary unset I
2484 YAAZYAZ T x # Temporary unset O
2491 *******************************************************************/
2494 CHECKPOINT cp = PL_savestack_ix;
2496 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2498 cc.oldcc = PL_regcc;
2500 cc.parenfloor = *PL_reglastparen;
2502 cc.min = ARG1(scan);
2503 cc.max = ARG2(scan);
2504 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2508 PL_reginput = locinput;
2509 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2511 PL_regcc = cc.oldcc;
2517 * This is really hard to understand, because after we match
2518 * what we're trying to match, we must make sure the rest of
2519 * the REx is going to match for sure, and to do that we have
2520 * to go back UP the parse tree by recursing ever deeper. And
2521 * if it fails, we have to reset our parent's current state
2522 * that we can try again after backing off.
2525 CHECKPOINT cp, lastcp;
2526 CURCUR* cc = PL_regcc;
2527 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2529 n = cc->cur + 1; /* how many we know we matched */
2530 PL_reginput = locinput;
2533 PerlIO_printf(Perl_debug_log,
2534 "%*s %ld out of %ld..%ld cc=%lx\n",
2535 REPORT_CODE_OFF+PL_regindent*2, "",
2536 (long)n, (long)cc->min,
2537 (long)cc->max, (long)cc)
2540 /* If degenerate scan matches "", assume scan done. */
2542 if (locinput == cc->lastloc && n >= cc->min) {
2543 PL_regcc = cc->oldcc;
2547 PerlIO_printf(Perl_debug_log,
2548 "%*s empty match detected, try continuation...\n",
2549 REPORT_CODE_OFF+PL_regindent*2, "")
2551 if (regmatch(cc->next))
2559 /* First just match a string of min scans. */
2563 cc->lastloc = locinput;
2564 if (regmatch(cc->scan))
2567 cc->lastloc = lastloc;
2572 /* Check whether we already were at this position.
2573 Postpone detection until we know the match is not
2574 *that* much linear. */
2575 if (!PL_reg_maxiter) {
2576 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2577 PL_reg_leftiter = PL_reg_maxiter;
2579 if (PL_reg_leftiter-- == 0) {
2580 I32 size = (PL_reg_maxiter + 7)/8;
2581 if (PL_reg_poscache) {
2582 if (PL_reg_poscache_size < size) {
2583 Renew(PL_reg_poscache, size, char);
2584 PL_reg_poscache_size = size;
2586 Zero(PL_reg_poscache, size, char);
2589 PL_reg_poscache_size = size;
2590 Newz(29, PL_reg_poscache, size, char);
2593 PerlIO_printf(Perl_debug_log,
2594 "%sDetected a super-linear match, switching on caching%s...\n",
2595 PL_colors[4], PL_colors[5])
2598 if (PL_reg_leftiter < 0) {
2599 I32 o = locinput - PL_bostr, b;
2601 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2604 if (PL_reg_poscache[o] & (1<<b)) {
2606 PerlIO_printf(Perl_debug_log,
2607 "%*s already tried at this position...\n",
2608 REPORT_CODE_OFF+PL_regindent*2, "")
2612 PL_reg_poscache[o] |= (1<<b);
2616 /* Prefer next over scan for minimal matching. */
2619 PL_regcc = cc->oldcc;
2622 cp = regcppush(cc->parenfloor);
2624 if (regmatch(cc->next)) {
2626 sayYES; /* All done. */
2634 if (n >= cc->max) { /* Maximum greed exceeded? */
2635 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2636 && !(PL_reg_flags & RF_warned)) {
2637 PL_reg_flags |= RF_warned;
2638 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2639 "Complex regular subexpression recursion",
2646 PerlIO_printf(Perl_debug_log,
2647 "%*s trying longer...\n",
2648 REPORT_CODE_OFF+PL_regindent*2, "")
2650 /* Try scanning more and see if it helps. */
2651 PL_reginput = locinput;
2653 cc->lastloc = locinput;
2654 cp = regcppush(cc->parenfloor);
2656 if (regmatch(cc->scan)) {
2663 cc->lastloc = lastloc;
2667 /* Prefer scan over next for maximal matching. */
2669 if (n < cc->max) { /* More greed allowed? */
2670 cp = regcppush(cc->parenfloor);
2672 cc->lastloc = locinput;
2674 if (regmatch(cc->scan)) {
2679 regcppop(); /* Restore some previous $<digit>s? */
2680 PL_reginput = locinput;
2682 PerlIO_printf(Perl_debug_log,
2683 "%*s failed, try continuation...\n",
2684 REPORT_CODE_OFF+PL_regindent*2, "")
2687 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2688 && !(PL_reg_flags & RF_warned)) {
2689 PL_reg_flags |= RF_warned;
2690 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2691 "Complex regular subexpression recursion",
2695 /* Failed deeper matches of scan, so see if this one works. */
2696 PL_regcc = cc->oldcc;
2699 if (regmatch(cc->next))
2705 cc->lastloc = lastloc;
2710 next = scan + ARG(scan);
2713 inner = NEXTOPER(NEXTOPER(scan));
2716 inner = NEXTOPER(scan);
2721 if (OP(next) != c1) /* No choice. */
2722 next = inner; /* Avoid recursion. */
2724 int lastparen = *PL_reglastparen;
2728 PL_reginput = locinput;
2729 if (regmatch(inner))
2732 for (n = *PL_reglastparen; n > lastparen; n--)
2734 *PL_reglastparen = n;
2737 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2741 inner = NEXTOPER(scan);
2742 if (c1 == BRANCHJ) {
2743 inner = NEXTOPER(inner);
2745 } while (scan != NULL && OP(scan) == c1);
2759 /* We suppose that the next guy does not need
2760 backtracking: in particular, it is of constant length,
2761 and has no parenths to influence future backrefs. */
2762 ln = ARG1(scan); /* min to match */
2763 n = ARG2(scan); /* max to match */
2764 paren = scan->flags;
2766 if (paren > PL_regsize)
2768 if (paren > *PL_reglastparen)
2769 *PL_reglastparen = paren;
2771 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2773 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2774 PL_reginput = locinput;
2777 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2779 if (ln && l == 0 && n >= ln
2780 /* In fact, this is tricky. If paren, then the
2781 fact that we did/didnot match may influence
2782 future execution. */
2783 && !(paren && ln == 0))
2785 locinput = PL_reginput;
2786 if (PL_regkind[(U8)OP(next)] == EXACT) {
2787 c1 = (U8)*STRING(next);
2788 if (OP(next) == EXACTF)
2790 else if (OP(next) == EXACTFL)
2791 c2 = PL_fold_locale[c1];
2798 /* This may be improved if l == 0. */
2799 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2800 /* If it could work, try it. */
2802 UCHARAT(PL_reginput) == c1 ||
2803 UCHARAT(PL_reginput) == c2)
2807 PL_regstartp[paren] =
2808 HOPc(PL_reginput, -l) - PL_bostr;
2809 PL_regendp[paren] = PL_reginput - PL_bostr;
2812 PL_regendp[paren] = -1;
2818 /* Couldn't or didn't -- move forward. */
2819 PL_reginput = locinput;
2820 if (regrepeat_hard(scan, 1, &l)) {
2822 locinput = PL_reginput;
2829 n = regrepeat_hard(scan, n, &l);
2830 if (n != 0 && l == 0
2831 /* In fact, this is tricky. If paren, then the
2832 fact that we did/didnot match may influence
2833 future execution. */
2834 && !(paren && ln == 0))
2836 locinput = PL_reginput;
2838 PerlIO_printf(Perl_debug_log,
2839 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
2840 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
2844 if (PL_regkind[(U8)OP(next)] == EXACT) {
2845 c1 = (U8)*STRING(next);
2846 if (OP(next) == EXACTF)
2848 else if (OP(next) == EXACTFL)
2849 c2 = PL_fold_locale[c1];
2858 /* If it could work, try it. */
2860 UCHARAT(PL_reginput) == c1 ||
2861 UCHARAT(PL_reginput) == c2)
2864 PerlIO_printf(Perl_debug_log,
2865 "%*s trying tail with n=%"IVdf"...\n",
2866 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
2870 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2871 PL_regendp[paren] = PL_reginput - PL_bostr;
2874 PL_regendp[paren] = -1;
2880 /* Couldn't or didn't -- back up. */
2882 locinput = HOPc(locinput, -l);
2883 PL_reginput = locinput;
2890 paren = scan->flags; /* Which paren to set */
2891 if (paren > PL_regsize)
2893 if (paren > *PL_reglastparen)
2894 *PL_reglastparen = paren;
2895 ln = ARG1(scan); /* min to match */
2896 n = ARG2(scan); /* max to match */
2897 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2901 ln = ARG1(scan); /* min to match */
2902 n = ARG2(scan); /* max to match */
2903 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2908 scan = NEXTOPER(scan);
2914 scan = NEXTOPER(scan);
2918 * Lookahead to avoid useless match attempts
2919 * when we know what character comes next.
2921 if (PL_regkind[(U8)OP(next)] == EXACT) {
2922 c1 = (U8)*STRING(next);
2923 if (OP(next) == EXACTF)
2925 else if (OP(next) == EXACTFL)
2926 c2 = PL_fold_locale[c1];
2932 PL_reginput = locinput;
2936 if (ln && regrepeat(scan, ln) < ln)
2938 locinput = PL_reginput;
2941 char *e = locinput + n - ln; /* Should not check after this */
2942 char *old = locinput;
2944 if (e >= PL_regeol || (n == REG_INFTY))
2947 /* Find place 'next' could work */
2949 while (locinput <= e && *locinput != c1)
2952 while (locinput <= e
2959 /* PL_reginput == old now */
2960 if (locinput != old) {
2961 ln = 1; /* Did some */
2962 if (regrepeat(scan, locinput - old) <
2966 /* PL_reginput == locinput now */
2969 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2970 PL_regendp[paren] = locinput - PL_bostr;
2973 PL_regendp[paren] = -1;
2977 PL_reginput = locinput; /* Could be reset... */
2979 /* Couldn't or didn't -- move forward. */
2984 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2985 /* If it could work, try it. */
2987 UCHARAT(PL_reginput) == c1 ||
2988 UCHARAT(PL_reginput) == c2)
2992 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2993 PL_regendp[paren] = PL_reginput - PL_bostr;
2996 PL_regendp[paren] = -1;
3002 /* Couldn't or didn't -- move forward. */
3003 PL_reginput = locinput;
3004 if (regrepeat(scan, 1)) {
3006 locinput = PL_reginput;
3014 n = regrepeat(scan, n);
3015 locinput = PL_reginput;
3016 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3017 (!PL_multiline || OP(next) == SEOL))
3018 ln = n; /* why back off? */
3022 /* If it could work, try it. */
3024 UCHARAT(PL_reginput) == c1 ||
3025 UCHARAT(PL_reginput) == c2)
3029 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3030 PL_regendp[paren] = PL_reginput - PL_bostr;
3033 PL_regendp[paren] = -1;
3039 /* Couldn't or didn't -- back up. */
3041 PL_reginput = locinput = HOPc(locinput, -1);
3046 /* If it could work, try it. */
3048 UCHARAT(PL_reginput) == c1 ||
3049 UCHARAT(PL_reginput) == c2)
3055 /* Couldn't or didn't -- back up. */
3057 PL_reginput = locinput = HOPc(locinput, -1);
3064 if (PL_reg_call_cc) {
3065 re_cc_state *cur_call_cc = PL_reg_call_cc;
3066 CURCUR *cctmp = PL_regcc;
3067 regexp *re = PL_reg_re;
3068 CHECKPOINT cp, lastcp;
3070 cp = regcppush(0); /* Save *all* the positions. */
3072 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3074 PL_reginput = locinput; /* Make position available to
3076 cache_re(PL_reg_call_cc->re);
3077 PL_regcc = PL_reg_call_cc->cc;
3078 PL_reg_call_cc = PL_reg_call_cc->prev;
3079 if (regmatch(cur_call_cc->node)) {
3080 PL_reg_call_cc = cur_call_cc;
3086 PL_reg_call_cc = cur_call_cc;
3092 PerlIO_printf(Perl_debug_log,
3093 "%*s continuation failed...\n",
3094 REPORT_CODE_OFF+PL_regindent*2, "")
3098 if (locinput < PL_regtill) {
3099 DEBUG_r(PerlIO_printf(Perl_debug_log,
3100 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3102 (long)(locinput - PL_reg_starttry),
3103 (long)(PL_regtill - PL_reg_starttry),
3105 sayNO_FINAL; /* Cannot match: too short. */
3107 PL_reginput = locinput; /* put where regtry can find it */
3108 sayYES_FINAL; /* Success! */
3110 PL_reginput = locinput; /* put where regtry can find it */
3111 sayYES_LOUD; /* Success! */
3114 PL_reginput = locinput;
3119 if (UTF) { /* XXXX This is absolutely
3120 broken, we read before
3122 s = HOPMAYBEc(locinput, -scan->flags);
3128 if (locinput < PL_bostr + scan->flags)
3130 PL_reginput = locinput - scan->flags;
3135 PL_reginput = locinput;
3140 if (UTF) { /* XXXX This is absolutely
3141 broken, we read before
3143 s = HOPMAYBEc(locinput, -scan->flags);
3144 if (!s || s < PL_bostr)
3149 if (locinput < PL_bostr + scan->flags)
3151 PL_reginput = locinput - scan->flags;
3156 PL_reginput = locinput;
3159 inner = NEXTOPER(NEXTOPER(scan));
3160 if (regmatch(inner) != n) {
3175 if (OP(scan) == SUSPEND) {
3176 locinput = PL_reginput;
3177 nextchr = UCHARAT(locinput);
3182 next = scan + ARG(scan);
3187 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3188 PTR2UV(scan), OP(scan));
3189 Perl_croak(aTHX_ "regexp memory corruption");
3195 * We get here only if there's trouble -- normally "case END" is
3196 * the terminating point.
3198 Perl_croak(aTHX_ "corrupted regexp pointers");
3204 PerlIO_printf(Perl_debug_log,
3205 "%*s %scould match...%s\n",
3206 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3210 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3211 PL_colors[4],PL_colors[5]));
3220 PerlIO_printf(Perl_debug_log,
3221 "%*s %sfailed...%s\n",
3222 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3234 - regrepeat - repeatedly match something simple, report how many
3237 * [This routine now assumes that it will only match on things of length 1.
3238 * That was true before, but now we assume scan - reginput is the count,
3239 * rather than incrementing count on every character. [Er, except utf8.]]
3242 S_regrepeat(pTHX_ regnode *p, I32 max)
3245 register char *scan;
3247 register char *loceol = PL_regeol;
3248 register I32 hardcount = 0;
3251 if (max != REG_INFTY && max < loceol - scan)
3252 loceol = scan + max;
3255 while (scan < loceol && *scan != '\n')
3263 while (scan < loceol && *scan != '\n') {
3264 scan += UTF8SKIP(scan);
3270 while (scan < loceol) {
3271 scan += UTF8SKIP(scan);
3275 case EXACT: /* length of string is 1 */
3277 while (scan < loceol && UCHARAT(scan) == c)
3280 case EXACTF: /* length of string is 1 */
3282 while (scan < loceol &&
3283 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3286 case EXACTFL: /* length of string is 1 */
3287 PL_reg_flags |= RF_tainted;
3289 while (scan < loceol &&
3290 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3295 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3296 scan += UTF8SKIP(scan);
3301 while (scan < loceol && REGINCLASS(p, *scan))
3305 while (scan < loceol && isALNUM(*scan))
3310 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3311 scan += UTF8SKIP(scan);
3316 PL_reg_flags |= RF_tainted;
3317 while (scan < loceol && isALNUM_LC(*scan))
3321 PL_reg_flags |= RF_tainted;
3323 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3324 scan += UTF8SKIP(scan);
3330 while (scan < loceol && !isALNUM(*scan))
3335 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3336 scan += UTF8SKIP(scan);
3341 PL_reg_flags |= RF_tainted;
3342 while (scan < loceol && !isALNUM_LC(*scan))
3346 PL_reg_flags |= RF_tainted;
3348 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3349 scan += UTF8SKIP(scan);
3354 while (scan < loceol && isSPACE(*scan))
3359 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3360 scan += UTF8SKIP(scan);
3365 PL_reg_flags |= RF_tainted;
3366 while (scan < loceol && isSPACE_LC(*scan))
3370 PL_reg_flags |= RF_tainted;
3372 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3373 scan += UTF8SKIP(scan);
3378 while (scan < loceol && !isSPACE(*scan))
3383 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3384 scan += UTF8SKIP(scan);
3389 PL_reg_flags |= RF_tainted;
3390 while (scan < loceol && !isSPACE_LC(*scan))
3394 PL_reg_flags |= RF_tainted;
3396 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3397 scan += UTF8SKIP(scan);
3402 while (scan < loceol && isDIGIT(*scan))
3407 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3408 scan += UTF8SKIP(scan);
3414 while (scan < loceol && !isDIGIT(*scan))
3419 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3420 scan += UTF8SKIP(scan);
3424 default: /* Called on something of 0 width. */
3425 break; /* So match right here or not at all. */
3431 c = scan - PL_reginput;
3436 SV *prop = sv_newmortal();
3439 PerlIO_printf(Perl_debug_log,
3440 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3441 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3448 - regrepeat_hard - repeatedly match something, report total lenth and length
3450 * The repeater is supposed to have constant length.
3454 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3457 register char *scan;
3458 register char *start;
3459 register char *loceol = PL_regeol;
3461 I32 count = 0, res = 1;
3466 start = PL_reginput;
3468 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3471 while (start < PL_reginput) {
3473 start += UTF8SKIP(start);
3484 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3486 *lp = l = PL_reginput - start;
3487 if (max != REG_INFTY && l*max < loceol - scan)
3488 loceol = scan + l*max;
3501 - reginclass - determine if a character falls into a character class
3505 S_reginclass(pTHX_ register regnode *p, register I32 c)
3508 char flags = ANYOF_FLAGS(p);
3512 if (ANYOF_BITMAP_TEST(p, c))
3514 else if (flags & ANYOF_FOLD) {
3516 if (flags & ANYOF_LOCALE) {
3517 PL_reg_flags |= RF_tainted;
3518 cf = PL_fold_locale[c];
3522 if (ANYOF_BITMAP_TEST(p, cf))
3526 if (!match && (flags & ANYOF_CLASS)) {
3527 PL_reg_flags |= RF_tainted;
3529 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3530 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3531 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3532 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3533 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3534 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3535 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3536 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3537 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3538 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3539 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3540 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3541 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3542 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3543 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3544 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3545 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3546 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3547 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3548 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3549 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3550 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3551 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3552 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3553 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3554 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3555 ) /* How's that for a conditional? */
3561 return (flags & ANYOF_INVERT) ? !match : match;
3565 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3568 char flags = ARG1(f);
3570 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3572 if (swash_fetch(sv, p))
3574 else if (flags & ANYOF_FOLD) {
3577 if (flags & ANYOF_LOCALE) {
3578 PL_reg_flags |= RF_tainted;
3579 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3582 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3583 if (swash_fetch(sv, tmpbuf))
3587 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3589 return (flags & ANYOF_INVERT) ? !match : match;
3593 S_reghop(pTHX_ U8 *s, I32 off)
3597 while (off-- && s < (U8*)PL_regeol)
3602 if (s > (U8*)PL_bostr) {
3605 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3607 } /* XXX could check well-formedness here */
3615 S_reghopmaybe(pTHX_ U8* s, I32 off)
3619 while (off-- && s < (U8*)PL_regeol)
3626 if (s > (U8*)PL_bostr) {
3629 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3631 } /* XXX could check well-formedness here */
3647 restore_pos(pTHXo_ void *arg)
3650 if (PL_reg_eval_set) {
3651 if (PL_reg_oldsaved) {
3652 PL_reg_re->subbeg = PL_reg_oldsaved;
3653 PL_reg_re->sublen = PL_reg_oldsavedlen;
3654 RX_MATCH_COPIED_on(PL_reg_re);
3656 PL_reg_magic->mg_len = PL_reg_oldpos;
3657 PL_reg_eval_set = 0;
3658 PL_curpm = PL_reg_oldcurpm;