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-2000, 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;
258 * Need to implement the following flags for reg_anch:
260 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
262 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
263 * INTUIT_AUTORITATIVE_ML
264 * INTUIT_ONCE_NOML - Intuit can match in one location only.
267 * Another flag for this function: SECOND_TIME (so that float substrs
268 * with giant delta may be not rechecked).
271 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
273 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
274 Otherwise, only SvCUR(sv) is used to get strbeg. */
276 /* XXXX We assume that strpos is strbeg unless sv. */
278 /* XXXX Some places assume that there is a fixed substring.
279 An update may be needed if optimizer marks as "INTUITable"
280 RExen without fixed substrings. Similarly, it is assumed that
281 lengths of all the strings are no more than minlen, thus they
282 cannot come from lookahead.
283 (Or minlen should take into account lookahead.) */
285 /* A failure to find a constant substring means that there is no need to make
286 an expensive call to REx engine, thus we celebrate a failure. Similarly,
287 finding a substring too deep into the string means that less calls to
288 regtry() should be needed.
290 REx compiler's optimizer found 4 possible hints:
291 a) Anchored substring;
293 c) Whether we are anchored (beginning-of-line or \G);
294 d) First node (of those at offset 0) which may distingush positions;
295 We use a)b)d) and multiline-part of c), and try to find a position in the
296 string which does not contradict any of them.
299 /* Most of decisions we do here should have been done at compile time.
300 The nodes of the REx which we used for the search should have been
301 deleted from the finite automaton. */
304 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
305 char *strend, U32 flags, re_scream_pos_data *data)
307 register I32 start_shift;
308 /* Should be nonnegative! */
309 register I32 end_shift;
315 register char *other_last = Nullch; /* other substr checked before this */
316 char *check_at; /* check substr found at this pos */
318 char *i_strpos = strpos;
321 DEBUG_r( if (!PL_colorset) reginitcolors() );
322 DEBUG_r(PerlIO_printf(Perl_debug_log,
323 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
324 PL_colors[4],PL_colors[5],PL_colors[0],
327 (strlen(prog->precomp) > 60 ? "..." : ""),
329 (int)(strend - strpos > 60 ? 60 : strend - strpos),
330 strpos, PL_colors[1],
331 (strend - strpos > 60 ? "..." : ""))
334 if (prog->minlen > strend - strpos) {
335 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
338 check = prog->check_substr;
339 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
340 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
341 || ( (prog->reganch & ROPT_ANCH_BOL)
342 && !PL_multiline ) ); /* Check after \n? */
344 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
345 /* Substring at constant offset from beg-of-str... */
348 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
349 && (sv && (strpos + SvCUR(sv) != strend)) ) {
350 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
353 PL_regeol = strend; /* Used in HOP() */
354 s = HOPc(strpos, prog->check_offset_min);
356 slen = SvCUR(check); /* >= 1 */
358 if ( strend - s > slen || strend - s < slen - 1
359 || (strend - s == slen && strend[-1] != '\n')) {
360 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
363 /* Now should match s[0..slen-2] */
365 if (slen && (*SvPVX(check) != *s
367 && memNE(SvPVX(check), s, slen)))) {
369 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
373 else if (*SvPVX(check) != *s
374 || ((slen = SvCUR(check)) > 1
375 && memNE(SvPVX(check), s, slen)))
377 goto success_at_start;
379 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
381 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
382 end_shift = prog->minlen - start_shift -
383 CHR_SVLEN(check) + (SvTAIL(check) != 0);
385 I32 end = prog->check_offset_max + CHR_SVLEN(check)
386 - (SvTAIL(check) != 0);
387 I32 eshift = strend - s - end;
389 if (end_shift < eshift)
393 else { /* Can match at random position */
396 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
397 /* Should be nonnegative! */
398 end_shift = prog->minlen - start_shift -
399 CHR_SVLEN(check) + (SvTAIL(check) != 0);
402 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
404 Perl_croak(aTHX_ "panic: end_shift");
408 /* Find a possible match in the region s..strend by looking for
409 the "check" substring in the region corrected by start/end_shift. */
410 if (flags & REXEC_SCREAM) {
411 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
412 I32 p = -1; /* Internal iterator of scream. */
413 I32 *pp = data ? data->scream_pos : &p;
415 if (PL_screamfirst[BmRARE(check)] >= 0
416 || ( BmRARE(check) == '\n'
417 && (BmPREVIOUS(check) == SvCUR(check) - 1)
419 s = screaminstr(sv, check,
420 start_shift + (s - strbeg), end_shift, pp, 0);
424 *data->scream_olds = s;
427 s = fbm_instr((unsigned char*)s + start_shift,
428 (unsigned char*)strend - end_shift,
429 check, PL_multiline ? FBMrf_MULTILINE : 0);
431 /* Update the count-of-usability, remove useless subpatterns,
434 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
435 (s ? "Found" : "Did not find"),
436 ((check == prog->anchored_substr) ? "anchored" : "floating"),
438 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
440 PL_colors[1], (SvTAIL(check) ? "$" : ""),
441 (s ? " at offset " : "...\n") ) );
448 /* Finish the diagnostic message */
449 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
451 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
452 Start with the other substr.
453 XXXX no SCREAM optimization yet - and a very coarse implementation
454 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
455 *always* match. Probably should be marked during compile...
456 Probably it is right to do no SCREAM here...
459 if (prog->float_substr && prog->anchored_substr) {
460 /* Take into account the "other" substring. */
461 /* XXXX May be hopelessly wrong for UTF... */
464 if (check == prog->float_substr) {
467 char *last = s - start_shift, *last1, *last2;
471 t = s - prog->check_offset_max;
472 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
473 && (!(prog->reganch & ROPT_UTF8)
474 || (PL_bostr = strpos, /* Used in regcopmaybe() */
475 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
480 t += prog->anchored_offset;
481 if (t < other_last) /* These positions already checked */
484 last2 = last1 = strend - prog->minlen;
487 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
488 /* On end-of-str: see comment below. */
489 s = fbm_instr((unsigned char*)t,
490 (unsigned char*)last1 + prog->anchored_offset
491 + SvCUR(prog->anchored_substr)
492 - (SvTAIL(prog->anchored_substr)!=0),
493 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
494 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
495 (s ? "Found" : "Contradicts"),
497 (int)(SvCUR(prog->anchored_substr)
498 - (SvTAIL(prog->anchored_substr)!=0)),
499 SvPVX(prog->anchored_substr),
500 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
502 if (last1 >= last2) {
503 DEBUG_r(PerlIO_printf(Perl_debug_log,
504 ", giving up...\n"));
507 DEBUG_r(PerlIO_printf(Perl_debug_log,
508 ", trying floating at offset %ld...\n",
509 (long)(s1 + 1 - i_strpos)));
510 PL_regeol = strend; /* Used in HOP() */
511 other_last = last1 + prog->anchored_offset + 1;
516 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
517 (long)(s - i_strpos)));
518 t = s - prog->anchored_offset;
527 else { /* Take into account the floating substring. */
532 last1 = last = strend - prog->minlen + prog->float_min_offset;
533 if (last - t > prog->float_max_offset)
534 last = t + prog->float_max_offset;
535 s = t + prog->float_min_offset;
538 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
539 /* fbm_instr() takes into account exact value of end-of-str
540 if the check is SvTAIL(ed). Since false positives are OK,
541 and end-of-str is not later than strend we are OK. */
542 s = fbm_instr((unsigned char*)s,
543 (unsigned char*)last + SvCUR(prog->float_substr)
544 - (SvTAIL(prog->float_substr)!=0),
545 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
546 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
547 (s ? "Found" : "Contradicts"),
549 (int)(SvCUR(prog->float_substr)
550 - (SvTAIL(prog->float_substr)!=0)),
551 SvPVX(prog->float_substr),
552 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
555 DEBUG_r(PerlIO_printf(Perl_debug_log,
556 ", giving up...\n"));
559 DEBUG_r(PerlIO_printf(Perl_debug_log,
560 ", trying anchored starting at offset %ld...\n",
561 (long)(s1 + 1 - i_strpos)));
562 other_last = last + 1;
563 PL_regeol = strend; /* Used in HOP() */
568 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
569 (long)(s - i_strpos)));
579 t = s - prog->check_offset_max;
581 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
582 && (!(prog->reganch & ROPT_UTF8)
583 || (PL_bostr = strpos, /* Used in regcopmaybe() */
584 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
587 /* Fixed substring is found far enough so that the match
588 cannot start at strpos. */
590 if (ml_anch && t[-1] != '\n') {
591 /* Eventually fbm_*() should handle this, but often
592 anchored_offset is not 0, so this check will not be wasted. */
593 /* XXXX In the code below we prefer to look for "^" even in
594 presence of anchored substrings. And we search even
595 beyond the found float position. These pessimizations
596 are historical artefacts only. */
598 while (t < strend - prog->minlen) {
600 if (t < s - prog->check_offset_min) {
601 if (prog->anchored_substr) {
602 /* We definitely contradict the found anchored
603 substr. Due to the above check we do not
604 contradict "check" substr.
605 Thus we can arrive here only if check substr
606 is float. Redo checking for "other"=="fixed".
609 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
610 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
611 goto do_other_anchored;
614 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
615 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
618 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting at offset %ld...\n",
619 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
625 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
626 PL_colors[0],PL_colors[1]));
631 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
635 /* The found string does not prohibit matching at beg-of-str
636 - no optimization of calling REx engine can be performed,
637 unless it was an MBOL and we are not after MBOL. */
639 /* Even in this situation we may use MBOL flag if strpos is offset
640 wrt the start of the string. */
642 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n'
643 /* May be due to an implicit anchor of m{.*foo} */
644 && !(prog->reganch & ROPT_IMPLICIT))
649 DEBUG_r( if (ml_anch)
650 PerlIO_printf(Perl_debug_log, "Does not contradict /%s^%s/m...\n",
651 PL_colors[0],PL_colors[1]);
654 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
655 && prog->check_substr /* Could be deleted already */
656 && --BmUSEFUL(prog->check_substr) < 0
657 && prog->check_substr == prog->float_substr)
659 /* If flags & SOMETHING - do not do it many times on the same match */
660 SvREFCNT_dec(prog->check_substr);
661 prog->check_substr = Nullsv; /* disable */
662 prog->float_substr = Nullsv; /* clear */
664 /* XXXX This is a remnant of the old implementation. It
665 looks wasteful, since now INTUIT can use many
667 prog->reganch &= ~RE_USE_INTUIT;
674 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
675 if (prog->regstclass) {
676 /* minlen == 0 is possible if regstclass is \b or \B,
677 and the fixed substr is ''$.
678 Since minlen is already taken into account, s+1 is before strend;
679 accidentally, minlen >= 1 guaranties no false positives at s + 1
680 even for \b or \B. But (minlen? 1 : 0) below assumes that
681 regstclass does not come from lookahead... */
682 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
683 This leaves EXACTF only, which is dealt with in find_byclass(). */
684 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
685 ? STR_LEN(prog->regstclass)
687 char *endpos = (prog->anchored_substr || ml_anch)
688 ? s + (prog->minlen? cl_l : 0)
689 : (prog->float_substr ? check_at - start_shift + cl_l
691 char *startpos = sv && SvPOK(sv) ? strend - SvCUR(sv) : s;
694 if (prog->reganch & ROPT_UTF8) {
695 PL_regdata = prog->data; /* Used by REGINCLASS UTF logic */
698 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
703 if (endpos == strend) {
704 DEBUG_r( PerlIO_printf(Perl_debug_log,
705 "Could not match STCLASS...\n") );
708 DEBUG_r( PerlIO_printf(Perl_debug_log,
709 "This position contradicts STCLASS...\n") );
710 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
712 /* Contradict one of substrings */
713 if (prog->anchored_substr) {
714 if (prog->anchored_substr == check) {
715 DEBUG_r( what = "anchored" );
717 PL_regeol = strend; /* Used in HOP() */
719 if (s + start_shift + end_shift > strend) {
720 /* XXXX Should be taken into account earlier? */
721 DEBUG_r( PerlIO_printf(Perl_debug_log,
722 "Could not match STCLASS...\n") );
725 DEBUG_r( PerlIO_printf(Perl_debug_log,
726 "Trying %s substr starting at offset %ld...\n",
727 what, (long)(s + start_shift - i_strpos)) );
730 /* Have both, check_string is floating */
731 if (t + start_shift >= check_at) /* Contradicts floating=check */
732 goto retry_floating_check;
733 /* Recheck anchored substring, but not floating... */
735 DEBUG_r( PerlIO_printf(Perl_debug_log,
736 "Trying anchored substr starting at offset %ld...\n",
737 (long)(other_last - i_strpos)) );
738 goto do_other_anchored;
740 /* Another way we could have checked stclass at the
741 current position only: */
744 DEBUG_r( PerlIO_printf(Perl_debug_log,
745 "Trying /^/m starting at offset %ld...\n",
746 (long)(t - i_strpos)) );
749 if (!prog->float_substr) /* Could have been deleted */
751 /* Check is floating subtring. */
752 retry_floating_check:
753 t = check_at - start_shift;
754 DEBUG_r( what = "floating" );
755 goto hop_and_restart;
758 PerlIO_printf(Perl_debug_log,
759 "By STCLASS: moving %ld --> %ld\n",
760 (long)(t - i_strpos), (long)(s - i_strpos));
762 PerlIO_printf(Perl_debug_log,
763 "Does not contradict STCLASS...\n") );
765 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sGuessed:%s match at offset %ld\n",
766 PL_colors[4], PL_colors[5], (long)(s - i_strpos)) );
769 fail_finish: /* Substring not found */
770 if (prog->check_substr) /* could be removed already */
771 BmUSEFUL(prog->check_substr) += 5; /* hooray */
773 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
774 PL_colors[4],PL_colors[5]));
778 /* We know what class REx starts with. Try to find this position... */
780 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
782 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
788 register I32 tmp = 1; /* Scratch variable? */
790 /* We know what class it must start with. */
794 if (REGINCLASSUTF8(c, (U8*)s)) {
795 if (tmp && (norun || regtry(prog, s)))
807 if (REGINCLASS(c, *(U8*)s)) {
808 if (tmp && (norun || regtry(prog, s)))
828 c2 = PL_fold_locale[c1];
833 e = s; /* Due to minlen logic of intuit() */
834 /* Here it is NOT UTF! */
838 && (ln == 1 || !(OP(c) == EXACTF
840 : ibcmp_locale(s, m, ln)))
841 && (norun || regtry(prog, s)) )
847 if ( (*(U8*)s == c1 || *(U8*)s == c2)
848 && (ln == 1 || !(OP(c) == EXACTF
850 : ibcmp_locale(s, m, ln)))
851 && (norun || regtry(prog, s)) )
858 PL_reg_flags |= RF_tainted;
861 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
862 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
864 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
866 if ((norun || regtry(prog, s)))
871 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
875 PL_reg_flags |= RF_tainted;
878 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
879 tmp = ((OP(c) == BOUNDUTF8 ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
881 if (tmp == !(OP(c) == BOUNDUTF8 ?
882 swash_fetch(PL_utf8_alnum, (U8*)s) :
883 isALNUM_LC_utf8((U8*)s)))
886 if ((norun || regtry(prog, s)))
891 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
895 PL_reg_flags |= RF_tainted;
898 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
899 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
901 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
903 else if ((norun || regtry(prog, s)))
907 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
911 PL_reg_flags |= RF_tainted;
914 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
915 tmp = ((OP(c) == NBOUNDUTF8 ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
917 if (tmp == !(OP(c) == NBOUNDUTF8 ?
918 swash_fetch(PL_utf8_alnum, (U8*)s) :
919 isALNUM_LC_utf8((U8*)s)))
921 else if ((norun || regtry(prog, s)))
925 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
931 if (tmp && (norun || regtry(prog, s)))
943 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
944 if (tmp && (norun || regtry(prog, s)))
955 PL_reg_flags |= RF_tainted;
957 if (isALNUM_LC(*s)) {
958 if (tmp && (norun || regtry(prog, s)))
969 PL_reg_flags |= RF_tainted;
971 if (isALNUM_LC_utf8((U8*)s)) {
972 if (tmp && (norun || regtry(prog, s)))
985 if (tmp && (norun || regtry(prog, s)))
997 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
998 if (tmp && (norun || regtry(prog, s)))
1009 PL_reg_flags |= RF_tainted;
1010 while (s < strend) {
1011 if (!isALNUM_LC(*s)) {
1012 if (tmp && (norun || regtry(prog, s)))
1023 PL_reg_flags |= RF_tainted;
1024 while (s < strend) {
1025 if (!isALNUM_LC_utf8((U8*)s)) {
1026 if (tmp && (norun || regtry(prog, s)))
1037 while (s < strend) {
1039 if (tmp && (norun || regtry(prog, s)))
1050 while (s < strend) {
1051 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1052 if (tmp && (norun || regtry(prog, s)))
1063 PL_reg_flags |= RF_tainted;
1064 while (s < strend) {
1065 if (isSPACE_LC(*s)) {
1066 if (tmp && (norun || regtry(prog, s)))
1077 PL_reg_flags |= RF_tainted;
1078 while (s < strend) {
1079 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1080 if (tmp && (norun || regtry(prog, s)))
1091 while (s < strend) {
1093 if (tmp && (norun || regtry(prog, s)))
1104 while (s < strend) {
1105 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1106 if (tmp && (norun || regtry(prog, s)))
1117 PL_reg_flags |= RF_tainted;
1118 while (s < strend) {
1119 if (!isSPACE_LC(*s)) {
1120 if (tmp && (norun || regtry(prog, s)))
1131 PL_reg_flags |= RF_tainted;
1132 while (s < strend) {
1133 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1134 if (tmp && (norun || regtry(prog, s)))
1145 while (s < strend) {
1147 if (tmp && (norun || regtry(prog, s)))
1158 while (s < strend) {
1159 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1160 if (tmp && (norun || regtry(prog, s)))
1171 PL_reg_flags |= RF_tainted;
1172 while (s < strend) {
1173 if (isDIGIT_LC(*s)) {
1174 if (tmp && (norun || regtry(prog, s)))
1185 PL_reg_flags |= RF_tainted;
1186 while (s < strend) {
1187 if (isDIGIT_LC_utf8((U8*)s)) {
1188 if (tmp && (norun || regtry(prog, s)))
1199 while (s < strend) {
1201 if (tmp && (norun || regtry(prog, s)))
1212 while (s < strend) {
1213 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1214 if (tmp && (norun || regtry(prog, s)))
1225 PL_reg_flags |= RF_tainted;
1226 while (s < strend) {
1227 if (!isDIGIT_LC(*s)) {
1228 if (tmp && (norun || regtry(prog, s)))
1239 PL_reg_flags |= RF_tainted;
1240 while (s < strend) {
1241 if (!isDIGIT_LC_utf8((U8*)s)) {
1242 if (tmp && (norun || regtry(prog, s)))
1253 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1262 - regexec_flags - match a regexp against a string
1265 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1266 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1267 /* strend: pointer to null at end of string */
1268 /* strbeg: real beginning of string */
1269 /* minend: end of match must be >=minend after stringarg. */
1270 /* data: May be used for some additional optimizations. */
1271 /* nosave: For optimizations. */
1275 register regnode *c;
1276 register char *startpos = stringarg;
1278 I32 minlen; /* must match at least this many chars */
1279 I32 dontbother = 0; /* how many characters not to try at end */
1280 I32 start_shift = 0; /* Offset of the start to find
1281 constant substr. */ /* CC */
1282 I32 end_shift = 0; /* Same for the end. */ /* CC */
1283 I32 scream_pos = -1; /* Internal iterator of scream. */
1285 SV* oreplsv = GvSV(PL_replgv);
1291 PL_regnarrate = PL_debug & 512;
1294 /* Be paranoid... */
1295 if (prog == NULL || startpos == NULL) {
1296 Perl_croak(aTHX_ "NULL regexp parameter");
1300 minlen = prog->minlen;
1301 if (strend - startpos < minlen) goto phooey;
1303 if (startpos == strbeg) /* is ^ valid at stringarg? */
1306 PL_regprev = (U32)stringarg[-1];
1307 if (!PL_multiline && PL_regprev == '\n')
1308 PL_regprev = '\0'; /* force ^ to NOT match */
1311 /* Check validity of program. */
1312 if (UCHARAT(prog->program) != REG_MAGIC) {
1313 Perl_croak(aTHX_ "corrupted regexp program");
1317 PL_reg_eval_set = 0;
1320 if (prog->reganch & ROPT_UTF8)
1321 PL_reg_flags |= RF_utf8;
1323 /* Mark beginning of line for ^ and lookbehind. */
1324 PL_regbol = startpos;
1328 /* Mark end of line for $ (and such) */
1331 /* see how far we have to get to not match where we matched before */
1332 PL_regtill = startpos+minend;
1334 /* We start without call_cc context. */
1337 /* If there is a "must appear" string, look for it. */
1340 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1343 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1344 PL_reg_ganch = startpos;
1345 else if (sv && SvTYPE(sv) >= SVt_PVMG
1347 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1348 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1349 if (prog->reganch & ROPT_ANCH_GPOS) {
1350 if (s > PL_reg_ganch)
1355 else /* pos() not defined */
1356 PL_reg_ganch = strbeg;
1359 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1360 re_scream_pos_data d;
1362 d.scream_olds = &scream_olds;
1363 d.scream_pos = &scream_pos;
1364 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1366 goto phooey; /* not present */
1369 DEBUG_r( if (!PL_colorset) reginitcolors() );
1370 DEBUG_r(PerlIO_printf(Perl_debug_log,
1371 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1372 PL_colors[4],PL_colors[5],PL_colors[0],
1375 (strlen(prog->precomp) > 60 ? "..." : ""),
1377 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1378 startpos, PL_colors[1],
1379 (strend - startpos > 60 ? "..." : ""))
1382 /* Simplest case: anchored match need be tried only once. */
1383 /* [unless only anchor is BOL and multiline is set] */
1384 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1385 if (s == startpos && regtry(prog, startpos))
1387 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1388 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1393 dontbother = minlen - 1;
1394 end = HOPc(strend, -dontbother) - 1;
1395 /* for multiline we only have to try after newlines */
1396 if (prog->check_substr) {
1400 if (regtry(prog, s))
1405 if (prog->reganch & RE_USE_INTUIT) {
1406 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1417 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1418 if (regtry(prog, s))
1425 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1426 if (regtry(prog, PL_reg_ganch))
1431 /* Messy cases: unanchored match. */
1432 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1433 /* we have /x+whatever/ */
1434 /* it must be a one character string (XXXX Except UTF?) */
1435 char ch = SvPVX(prog->anchored_substr)[0];
1437 while (s < strend) {
1439 if (regtry(prog, s)) goto got_it;
1441 while (s < strend && *s == ch)
1448 while (s < strend) {
1450 if (regtry(prog, s)) goto got_it;
1452 while (s < strend && *s == ch)
1460 else if (prog->anchored_substr != Nullsv
1461 || (prog->float_substr != Nullsv
1462 && prog->float_max_offset < strend - s)) {
1463 SV *must = prog->anchored_substr
1464 ? prog->anchored_substr : prog->float_substr;
1466 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1468 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1469 I32 delta = back_max - back_min;
1470 char *last = HOPc(strend, /* Cannot start after this */
1471 -(I32)(CHR_SVLEN(must)
1472 - (SvTAIL(must) != 0) + back_min));
1473 char *last1; /* Last position checked before */
1476 last1 = HOPc(s, -1);
1478 last1 = s - 1; /* bogus */
1480 /* XXXX check_substr already used to find `s', can optimize if
1481 check_substr==must. */
1483 dontbother = end_shift;
1484 strend = HOPc(strend, -dontbother);
1485 while ( (s <= last) &&
1486 ((flags & REXEC_SCREAM)
1487 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1488 end_shift, &scream_pos, 0))
1489 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1490 (unsigned char*)strend, must,
1491 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1492 if (HOPc(s, -back_max) > last1) {
1493 last1 = HOPc(s, -back_min);
1494 s = HOPc(s, -back_max);
1497 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1499 last1 = HOPc(s, -back_min);
1503 while (s <= last1) {
1504 if (regtry(prog, s))
1510 while (s <= last1) {
1511 if (regtry(prog, s))
1519 else if (c = prog->regstclass) {
1520 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1521 /* don't bother with what can't match */
1522 strend = HOPc(strend, -(minlen - 1));
1523 if (find_byclass(prog, c, s, strend, startpos, 0))
1528 if (prog->float_substr != Nullsv) { /* Trim the end. */
1530 I32 oldpos = scream_pos;
1532 if (flags & REXEC_SCREAM) {
1533 last = screaminstr(sv, prog->float_substr, s - strbeg,
1534 end_shift, &scream_pos, 1); /* last one */
1536 last = scream_olds; /* Only one occurence. */
1540 char *little = SvPV(prog->float_substr, len);
1542 if (SvTAIL(prog->float_substr)) {
1543 if (memEQ(strend - len + 1, little, len - 1))
1544 last = strend - len + 1;
1545 else if (!PL_multiline)
1546 last = memEQ(strend - len, little, len)
1547 ? strend - len : Nullch;
1553 last = rninstr(s, strend, little, little + len);
1555 last = strend; /* matching `$' */
1558 if (last == NULL) goto phooey; /* Should not happen! */
1559 dontbother = strend - last + prog->float_min_offset;
1561 if (minlen && (dontbother < minlen))
1562 dontbother = minlen - 1;
1563 strend -= dontbother; /* this one's always in bytes! */
1564 /* We don't know much -- general case. */
1567 if (regtry(prog, s))
1576 if (regtry(prog, s))
1578 } while (s++ < strend);
1586 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1588 if (PL_reg_eval_set) {
1589 /* Preserve the current value of $^R */
1590 if (oreplsv != GvSV(PL_replgv))
1591 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1592 restored, the value remains
1594 restore_pos(aTHXo_ 0);
1597 /* make sure $`, $&, $', and $digit will work later */
1598 if ( !(flags & REXEC_NOT_FIRST) ) {
1599 if (RX_MATCH_COPIED(prog)) {
1600 Safefree(prog->subbeg);
1601 RX_MATCH_COPIED_off(prog);
1603 if (flags & REXEC_COPY_STR) {
1604 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1606 s = savepvn(strbeg, i);
1609 RX_MATCH_COPIED_on(prog);
1612 prog->subbeg = strbeg;
1613 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1620 if (PL_reg_eval_set)
1621 restore_pos(aTHXo_ 0);
1626 - regtry - try match at specific point
1628 STATIC I32 /* 0 failure, 1 success */
1629 S_regtry(pTHX_ regexp *prog, char *startpos)
1637 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1640 PL_reg_eval_set = RS_init;
1642 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1643 (IV)(PL_stack_sp - PL_stack_base));
1645 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1646 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1647 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1649 /* Apparently this is not needed, judging by wantarray. */
1650 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1651 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1654 /* Make $_ available to executed code. */
1655 if (PL_reg_sv != DEFSV) {
1656 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1661 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1662 && (mg = mg_find(PL_reg_sv, 'g')))) {
1663 /* prepare for quick setting of pos */
1664 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1665 mg = mg_find(PL_reg_sv, 'g');
1669 PL_reg_oldpos = mg->mg_len;
1670 SAVEDESTRUCTOR_X(restore_pos, 0);
1673 Newz(22,PL_reg_curpm, 1, PMOP);
1674 PL_reg_curpm->op_pmregexp = prog;
1675 PL_reg_oldcurpm = PL_curpm;
1676 PL_curpm = PL_reg_curpm;
1677 if (RX_MATCH_COPIED(prog)) {
1678 /* Here is a serious problem: we cannot rewrite subbeg,
1679 since it may be needed if this match fails. Thus
1680 $` inside (?{}) could fail... */
1681 PL_reg_oldsaved = prog->subbeg;
1682 PL_reg_oldsavedlen = prog->sublen;
1683 RX_MATCH_COPIED_off(prog);
1686 PL_reg_oldsaved = Nullch;
1687 prog->subbeg = PL_bostr;
1688 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1690 prog->startp[0] = startpos - PL_bostr;
1691 PL_reginput = startpos;
1692 PL_regstartp = prog->startp;
1693 PL_regendp = prog->endp;
1694 PL_reglastparen = &prog->lastparen;
1695 prog->lastparen = 0;
1697 DEBUG_r(PL_reg_starttry = startpos);
1698 if (PL_reg_start_tmpl <= prog->nparens) {
1699 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1700 if(PL_reg_start_tmp)
1701 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1703 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1706 /* XXXX What this code is doing here?!!! There should be no need
1707 to do this again and again, PL_reglastparen should take care of
1711 if (prog->nparens) {
1712 for (i = prog->nparens; i >= 1; i--) {
1718 if (regmatch(prog->program + 1)) {
1719 prog->endp[0] = PL_reginput - PL_bostr;
1727 - regmatch - main matching routine
1729 * Conceptually the strategy is simple: check to see whether the current
1730 * node matches, call self recursively to see whether the rest matches,
1731 * and then act accordingly. In practice we make some effort to avoid
1732 * recursion, in particular by going through "ordinary" nodes (that don't
1733 * need to know whether the rest of the match failed) by a loop instead of
1736 /* [lwall] I've hoisted the register declarations to the outer block in order to
1737 * maybe save a little bit of pushing and popping on the stack. It also takes
1738 * advantage of machines that use a register save mask on subroutine entry.
1740 STATIC I32 /* 0 failure, 1 success */
1741 S_regmatch(pTHX_ regnode *prog)
1744 register regnode *scan; /* Current node. */
1745 regnode *next; /* Next node. */
1746 regnode *inner; /* Next node in internal branch. */
1747 register I32 nextchr; /* renamed nextchr - nextchar colides with
1748 function of same name */
1749 register I32 n; /* no or next */
1750 register I32 ln; /* len or last */
1751 register char *s; /* operand or save */
1752 register char *locinput = PL_reginput;
1753 register I32 c1, c2, paren; /* case fold search, parenth */
1754 int minmod = 0, sw = 0, logical = 0;
1759 /* Note that nextchr is a byte even in UTF */
1760 nextchr = UCHARAT(locinput);
1762 while (scan != NULL) {
1763 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1765 # define sayYES goto yes
1766 # define sayNO goto no
1767 # define sayYES_FINAL goto yes_final
1768 # define sayYES_LOUD goto yes_loud
1769 # define sayNO_FINAL goto no_final
1770 # define sayNO_SILENT goto do_no
1771 # define saySAME(x) if (x) goto yes; else goto no
1772 # define REPORT_CODE_OFF 24
1774 # define sayYES return 1
1775 # define sayNO return 0
1776 # define sayYES_FINAL return 1
1777 # define sayYES_LOUD return 1
1778 # define sayNO_FINAL return 0
1779 # define sayNO_SILENT return 0
1780 # define saySAME(x) return x
1783 SV *prop = sv_newmortal();
1784 int docolor = *PL_colors[0];
1785 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1786 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1787 /* The part of the string before starttry has one color
1788 (pref0_len chars), between starttry and current
1789 position another one (pref_len - pref0_len chars),
1790 after the current position the third one.
1791 We assume that pref0_len <= pref_len, otherwise we
1792 decrease pref0_len. */
1793 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1794 ? (5 + taill) - l : locinput - PL_bostr);
1795 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1797 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1798 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1799 ? (5 + taill) - pref_len : PL_regeol - locinput);
1802 if (pref0_len > pref_len)
1803 pref0_len = pref_len;
1804 regprop(prop, scan);
1805 PerlIO_printf(Perl_debug_log,
1806 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1807 (IV)(locinput - PL_bostr),
1808 PL_colors[4], pref0_len,
1809 locinput - pref_len, PL_colors[5],
1810 PL_colors[2], pref_len - pref0_len,
1811 locinput - pref_len + pref0_len, PL_colors[3],
1812 (docolor ? "" : "> <"),
1813 PL_colors[0], l, locinput, PL_colors[1],
1814 15 - l - pref_len + 1,
1816 (IV)(scan - PL_regprogram), PL_regindent*2, "",
1820 next = scan + NEXT_OFF(scan);
1826 if (locinput == PL_bostr
1827 ? PL_regprev == '\n'
1829 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1831 /* regtill = regbol; */
1836 if (locinput == PL_bostr
1837 ? PL_regprev == '\n'
1838 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1844 if (locinput == PL_regbol && PL_regprev == '\n')
1848 if (locinput == PL_reg_ganch)
1858 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1863 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1865 if (PL_regeol - locinput > 1)
1869 if (PL_regeol != locinput)
1873 if (nextchr & 0x80) {
1874 locinput += PL_utf8skip[nextchr];
1875 if (locinput > PL_regeol)
1877 nextchr = UCHARAT(locinput);
1880 if (!nextchr && locinput >= PL_regeol)
1882 nextchr = UCHARAT(++locinput);
1885 if (!nextchr && locinput >= PL_regeol)
1887 nextchr = UCHARAT(++locinput);
1890 if (nextchr & 0x80) {
1891 locinput += PL_utf8skip[nextchr];
1892 if (locinput > PL_regeol)
1894 nextchr = UCHARAT(locinput);
1897 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1899 nextchr = UCHARAT(++locinput);
1902 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1904 nextchr = UCHARAT(++locinput);
1909 /* Inline the first character, for speed. */
1910 if (UCHARAT(s) != nextchr)
1912 if (PL_regeol - locinput < ln)
1914 if (ln > 1 && memNE(s, locinput, ln))
1917 nextchr = UCHARAT(locinput);
1920 PL_reg_flags |= RF_tainted;
1929 c1 = OP(scan) == EXACTF;
1933 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1934 toLOWER_utf8((U8*)l) :
1935 toLOWER_LC_utf8((U8*)l)))
1943 nextchr = UCHARAT(locinput);
1947 /* Inline the first character, for speed. */
1948 if (UCHARAT(s) != nextchr &&
1949 UCHARAT(s) != ((OP(scan) == EXACTF)
1950 ? PL_fold : PL_fold_locale)[nextchr])
1952 if (PL_regeol - locinput < ln)
1954 if (ln > 1 && (OP(scan) == EXACTF
1955 ? ibcmp(s, locinput, ln)
1956 : ibcmp_locale(s, locinput, ln)))
1959 nextchr = UCHARAT(locinput);
1962 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1964 if (locinput >= PL_regeol)
1966 locinput += PL_utf8skip[nextchr];
1967 nextchr = UCHARAT(locinput);
1971 nextchr = UCHARAT(locinput);
1972 if (!REGINCLASS(scan, nextchr))
1974 if (!nextchr && locinput >= PL_regeol)
1976 nextchr = UCHARAT(++locinput);
1979 PL_reg_flags |= RF_tainted;
1984 if (!(OP(scan) == ALNUM
1985 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1987 nextchr = UCHARAT(++locinput);
1990 PL_reg_flags |= RF_tainted;
1995 if (nextchr & 0x80) {
1996 if (!(OP(scan) == ALNUMUTF8
1997 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1998 : isALNUM_LC_utf8((U8*)locinput)))
2002 locinput += PL_utf8skip[nextchr];
2003 nextchr = UCHARAT(locinput);
2006 if (!(OP(scan) == ALNUMUTF8
2007 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2009 nextchr = UCHARAT(++locinput);
2012 PL_reg_flags |= RF_tainted;
2015 if (!nextchr && locinput >= PL_regeol)
2017 if (OP(scan) == NALNUM
2018 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2020 nextchr = UCHARAT(++locinput);
2023 PL_reg_flags |= RF_tainted;
2026 if (!nextchr && locinput >= PL_regeol)
2028 if (nextchr & 0x80) {
2029 if (OP(scan) == NALNUMUTF8
2030 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2031 : isALNUM_LC_utf8((U8*)locinput))
2035 locinput += PL_utf8skip[nextchr];
2036 nextchr = UCHARAT(locinput);
2039 if (OP(scan) == NALNUMUTF8
2040 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2042 nextchr = UCHARAT(++locinput);
2046 PL_reg_flags |= RF_tainted;
2050 /* was last char in word? */
2051 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2052 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2054 n = isALNUM(nextchr);
2057 ln = isALNUM_LC(ln);
2058 n = isALNUM_LC(nextchr);
2060 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2065 PL_reg_flags |= RF_tainted;
2069 /* was last char in word? */
2070 ln = (locinput != PL_regbol)
2071 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2072 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2073 ln = isALNUM_uni(ln);
2074 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2077 ln = isALNUM_LC_uni(ln);
2078 n = isALNUM_LC_utf8((U8*)locinput);
2080 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2084 PL_reg_flags |= RF_tainted;
2089 if (!(OP(scan) == SPACE
2090 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2092 nextchr = UCHARAT(++locinput);
2095 PL_reg_flags |= RF_tainted;
2100 if (nextchr & 0x80) {
2101 if (!(OP(scan) == SPACEUTF8
2102 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2103 : isSPACE_LC_utf8((U8*)locinput)))
2107 locinput += PL_utf8skip[nextchr];
2108 nextchr = UCHARAT(locinput);
2111 if (!(OP(scan) == SPACEUTF8
2112 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2114 nextchr = UCHARAT(++locinput);
2117 PL_reg_flags |= RF_tainted;
2120 if (!nextchr && locinput >= PL_regeol)
2122 if (OP(scan) == NSPACE
2123 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2125 nextchr = UCHARAT(++locinput);
2128 PL_reg_flags |= RF_tainted;
2131 if (!nextchr && locinput >= PL_regeol)
2133 if (nextchr & 0x80) {
2134 if (OP(scan) == NSPACEUTF8
2135 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2136 : isSPACE_LC_utf8((U8*)locinput))
2140 locinput += PL_utf8skip[nextchr];
2141 nextchr = UCHARAT(locinput);
2144 if (OP(scan) == NSPACEUTF8
2145 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2147 nextchr = UCHARAT(++locinput);
2150 PL_reg_flags |= RF_tainted;
2155 if (!(OP(scan) == DIGIT
2156 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2158 nextchr = UCHARAT(++locinput);
2161 PL_reg_flags |= RF_tainted;
2166 if (nextchr & 0x80) {
2167 if (!(OP(scan) == DIGITUTF8
2168 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2169 : isDIGIT_LC_utf8((U8*)locinput)))
2173 locinput += PL_utf8skip[nextchr];
2174 nextchr = UCHARAT(locinput);
2177 if (!(OP(scan) == DIGITUTF8
2178 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2180 nextchr = UCHARAT(++locinput);
2183 PL_reg_flags |= RF_tainted;
2186 if (!nextchr && locinput >= PL_regeol)
2188 if (OP(scan) == NDIGIT
2189 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2191 nextchr = UCHARAT(++locinput);
2194 PL_reg_flags |= RF_tainted;
2197 if (!nextchr && locinput >= PL_regeol)
2199 if (nextchr & 0x80) {
2200 if (OP(scan) == NDIGITUTF8
2201 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2202 : isDIGIT_LC_utf8((U8*)locinput))
2206 locinput += PL_utf8skip[nextchr];
2207 nextchr = UCHARAT(locinput);
2210 if (OP(scan) == NDIGITUTF8
2211 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2213 nextchr = UCHARAT(++locinput);
2216 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2218 locinput += PL_utf8skip[nextchr];
2219 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2220 locinput += UTF8SKIP(locinput);
2221 if (locinput > PL_regeol)
2223 nextchr = UCHARAT(locinput);
2226 PL_reg_flags |= RF_tainted;
2230 n = ARG(scan); /* which paren pair */
2231 ln = PL_regstartp[n];
2232 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2233 if (*PL_reglastparen < n || ln == -1)
2234 sayNO; /* Do not match unless seen CLOSEn. */
2235 if (ln == PL_regendp[n])
2239 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2241 char *e = PL_bostr + PL_regendp[n];
2243 * Note that we can't do the "other character" lookup trick as
2244 * in the 8-bit case (no pun intended) because in Unicode we
2245 * have to map both upper and title case to lower case.
2247 if (OP(scan) == REFF) {
2251 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2261 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2268 nextchr = UCHARAT(locinput);
2272 /* Inline the first character, for speed. */
2273 if (UCHARAT(s) != nextchr &&
2275 (UCHARAT(s) != ((OP(scan) == REFF
2276 ? PL_fold : PL_fold_locale)[nextchr]))))
2278 ln = PL_regendp[n] - ln;
2279 if (locinput + ln > PL_regeol)
2281 if (ln > 1 && (OP(scan) == REF
2282 ? memNE(s, locinput, ln)
2284 ? ibcmp(s, locinput, ln)
2285 : ibcmp_locale(s, locinput, ln))))
2288 nextchr = UCHARAT(locinput);
2299 OP_4tree *oop = PL_op;
2300 COP *ocurcop = PL_curcop;
2301 SV **ocurpad = PL_curpad;
2305 PL_op = (OP_4tree*)PL_regdata->data[n];
2306 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2307 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2308 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2310 CALLRUNOPS(aTHX); /* Scalar context. */
2316 PL_curpad = ocurpad;
2317 PL_curcop = ocurcop;
2319 if (logical == 2) { /* Postponed subexpression. */
2321 MAGIC *mg = Null(MAGIC*);
2323 CHECKPOINT cp, lastcp;
2325 if(SvROK(ret) || SvRMAGICAL(ret)) {
2326 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2329 mg = mg_find(sv, 'r');
2332 re = (regexp *)mg->mg_obj;
2333 (void)ReREFCNT_inc(re);
2337 char *t = SvPV(ret, len);
2339 char *oprecomp = PL_regprecomp;
2340 I32 osize = PL_regsize;
2341 I32 onpar = PL_regnpar;
2344 pm.op_pmdynflags = (UTF||DO_UTF8(ret) ? PMdf_UTF8 : 0);
2345 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2347 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2348 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2349 PL_regprecomp = oprecomp;
2354 PerlIO_printf(Perl_debug_log,
2355 "Entering embedded `%s%.60s%s%s'\n",
2359 (strlen(re->precomp) > 60 ? "..." : ""))
2362 state.prev = PL_reg_call_cc;
2363 state.cc = PL_regcc;
2364 state.re = PL_reg_re;
2368 cp = regcppush(0); /* Save *all* the positions. */
2371 state.ss = PL_savestack_ix;
2372 *PL_reglastparen = 0;
2373 PL_reg_call_cc = &state;
2374 PL_reginput = locinput;
2376 /* XXXX This is too dramatic a measure... */
2379 if (regmatch(re->program + 1)) {
2380 /* Even though we succeeded, we need to restore
2381 global variables, since we may be wrapped inside
2382 SUSPEND, thus the match may be not finished yet. */
2384 /* XXXX Do this only if SUSPENDed? */
2385 PL_reg_call_cc = state.prev;
2386 PL_regcc = state.cc;
2387 PL_reg_re = state.re;
2388 cache_re(PL_reg_re);
2390 /* XXXX This is too dramatic a measure... */
2393 /* These are needed even if not SUSPEND. */
2401 PL_reg_call_cc = state.prev;
2402 PL_regcc = state.cc;
2403 PL_reg_re = state.re;
2404 cache_re(PL_reg_re);
2406 /* XXXX This is too dramatic a measure... */
2415 sv_setsv(save_scalar(PL_replgv), ret);
2419 n = ARG(scan); /* which paren pair */
2420 PL_reg_start_tmp[n] = locinput;
2425 n = ARG(scan); /* which paren pair */
2426 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2427 PL_regendp[n] = locinput - PL_bostr;
2428 if (n > *PL_reglastparen)
2429 *PL_reglastparen = n;
2432 n = ARG(scan); /* which paren pair */
2433 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2436 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2438 next = NEXTOPER(NEXTOPER(scan));
2440 next = scan + ARG(scan);
2441 if (OP(next) == IFTHEN) /* Fake one. */
2442 next = NEXTOPER(NEXTOPER(next));
2446 logical = scan->flags;
2448 /*******************************************************************
2449 PL_regcc contains infoblock about the innermost (...)* loop, and
2450 a pointer to the next outer infoblock.
2452 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2454 1) After matching X, regnode for CURLYX is processed;
2456 2) This regnode creates infoblock on the stack, and calls
2457 regmatch() recursively with the starting point at WHILEM node;
2459 3) Each hit of WHILEM node tries to match A and Z (in the order
2460 depending on the current iteration, min/max of {min,max} and
2461 greediness). The information about where are nodes for "A"
2462 and "Z" is read from the infoblock, as is info on how many times "A"
2463 was already matched, and greediness.
2465 4) After A matches, the same WHILEM node is hit again.
2467 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2468 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2469 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2470 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2471 of the external loop.
2473 Currently present infoblocks form a tree with a stem formed by PL_curcc
2474 and whatever it mentions via ->next, and additional attached trees
2475 corresponding to temporarily unset infoblocks as in "5" above.
2477 In the following picture infoblocks for outer loop of
2478 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2479 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2480 infoblocks are drawn below the "reset" infoblock.
2482 In fact in the picture below we do not show failed matches for Z and T
2483 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2484 more obvious *why* one needs to *temporary* unset infoblocks.]
2486 Matched REx position InfoBlocks Comment
2490 Y A)*?Z)*?T x <- O <- I
2491 YA )*?Z)*?T x <- O <- I
2492 YA A)*?Z)*?T x <- O <- I
2493 YAA )*?Z)*?T x <- O <- I
2494 YAA Z)*?T x <- O # Temporary unset I
2497 YAAZ Y(A)*?Z)*?T x <- O
2500 YAAZY (A)*?Z)*?T x <- O
2503 YAAZY A)*?Z)*?T x <- O <- I
2506 YAAZYA )*?Z)*?T x <- O <- I
2509 YAAZYA Z)*?T x <- O # Temporary unset I
2515 YAAZYAZ T x # Temporary unset O
2522 *******************************************************************/
2525 CHECKPOINT cp = PL_savestack_ix;
2527 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2529 cc.oldcc = PL_regcc;
2531 cc.parenfloor = *PL_reglastparen;
2533 cc.min = ARG1(scan);
2534 cc.max = ARG2(scan);
2535 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2539 PL_reginput = locinput;
2540 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2542 PL_regcc = cc.oldcc;
2548 * This is really hard to understand, because after we match
2549 * what we're trying to match, we must make sure the rest of
2550 * the REx is going to match for sure, and to do that we have
2551 * to go back UP the parse tree by recursing ever deeper. And
2552 * if it fails, we have to reset our parent's current state
2553 * that we can try again after backing off.
2556 CHECKPOINT cp, lastcp;
2557 CURCUR* cc = PL_regcc;
2558 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2560 n = cc->cur + 1; /* how many we know we matched */
2561 PL_reginput = locinput;
2564 PerlIO_printf(Perl_debug_log,
2565 "%*s %ld out of %ld..%ld cc=%lx\n",
2566 REPORT_CODE_OFF+PL_regindent*2, "",
2567 (long)n, (long)cc->min,
2568 (long)cc->max, (long)cc)
2571 /* If degenerate scan matches "", assume scan done. */
2573 if (locinput == cc->lastloc && n >= cc->min) {
2574 PL_regcc = cc->oldcc;
2578 PerlIO_printf(Perl_debug_log,
2579 "%*s empty match detected, try continuation...\n",
2580 REPORT_CODE_OFF+PL_regindent*2, "")
2582 if (regmatch(cc->next))
2590 /* First just match a string of min scans. */
2594 cc->lastloc = locinput;
2595 if (regmatch(cc->scan))
2598 cc->lastloc = lastloc;
2603 /* Check whether we already were at this position.
2604 Postpone detection until we know the match is not
2605 *that* much linear. */
2606 if (!PL_reg_maxiter) {
2607 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2608 PL_reg_leftiter = PL_reg_maxiter;
2610 if (PL_reg_leftiter-- == 0) {
2611 I32 size = (PL_reg_maxiter + 7)/8;
2612 if (PL_reg_poscache) {
2613 if (PL_reg_poscache_size < size) {
2614 Renew(PL_reg_poscache, size, char);
2615 PL_reg_poscache_size = size;
2617 Zero(PL_reg_poscache, size, char);
2620 PL_reg_poscache_size = size;
2621 Newz(29, PL_reg_poscache, size, char);
2624 PerlIO_printf(Perl_debug_log,
2625 "%sDetected a super-linear match, switching on caching%s...\n",
2626 PL_colors[4], PL_colors[5])
2629 if (PL_reg_leftiter < 0) {
2630 I32 o = locinput - PL_bostr, b;
2632 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2635 if (PL_reg_poscache[o] & (1<<b)) {
2637 PerlIO_printf(Perl_debug_log,
2638 "%*s already tried at this position...\n",
2639 REPORT_CODE_OFF+PL_regindent*2, "")
2643 PL_reg_poscache[o] |= (1<<b);
2647 /* Prefer next over scan for minimal matching. */
2650 PL_regcc = cc->oldcc;
2653 cp = regcppush(cc->parenfloor);
2655 if (regmatch(cc->next)) {
2657 sayYES; /* All done. */
2665 if (n >= cc->max) { /* Maximum greed exceeded? */
2666 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2667 && !(PL_reg_flags & RF_warned)) {
2668 PL_reg_flags |= RF_warned;
2669 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2670 "Complex regular subexpression recursion",
2677 PerlIO_printf(Perl_debug_log,
2678 "%*s trying longer...\n",
2679 REPORT_CODE_OFF+PL_regindent*2, "")
2681 /* Try scanning more and see if it helps. */
2682 PL_reginput = locinput;
2684 cc->lastloc = locinput;
2685 cp = regcppush(cc->parenfloor);
2687 if (regmatch(cc->scan)) {
2694 cc->lastloc = lastloc;
2698 /* Prefer scan over next for maximal matching. */
2700 if (n < cc->max) { /* More greed allowed? */
2701 cp = regcppush(cc->parenfloor);
2703 cc->lastloc = locinput;
2705 if (regmatch(cc->scan)) {
2710 regcppop(); /* Restore some previous $<digit>s? */
2711 PL_reginput = locinput;
2713 PerlIO_printf(Perl_debug_log,
2714 "%*s failed, try continuation...\n",
2715 REPORT_CODE_OFF+PL_regindent*2, "")
2718 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2719 && !(PL_reg_flags & RF_warned)) {
2720 PL_reg_flags |= RF_warned;
2721 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2722 "Complex regular subexpression recursion",
2726 /* Failed deeper matches of scan, so see if this one works. */
2727 PL_regcc = cc->oldcc;
2730 if (regmatch(cc->next))
2736 cc->lastloc = lastloc;
2741 next = scan + ARG(scan);
2744 inner = NEXTOPER(NEXTOPER(scan));
2747 inner = NEXTOPER(scan);
2752 if (OP(next) != c1) /* No choice. */
2753 next = inner; /* Avoid recursion. */
2755 int lastparen = *PL_reglastparen;
2759 PL_reginput = locinput;
2760 if (regmatch(inner))
2763 for (n = *PL_reglastparen; n > lastparen; n--)
2765 *PL_reglastparen = n;
2768 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2772 inner = NEXTOPER(scan);
2773 if (c1 == BRANCHJ) {
2774 inner = NEXTOPER(inner);
2776 } while (scan != NULL && OP(scan) == c1);
2790 /* We suppose that the next guy does not need
2791 backtracking: in particular, it is of constant length,
2792 and has no parenths to influence future backrefs. */
2793 ln = ARG1(scan); /* min to match */
2794 n = ARG2(scan); /* max to match */
2795 paren = scan->flags;
2797 if (paren > PL_regsize)
2799 if (paren > *PL_reglastparen)
2800 *PL_reglastparen = paren;
2802 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2804 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2805 PL_reginput = locinput;
2808 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2810 if (ln && l == 0 && n >= ln
2811 /* In fact, this is tricky. If paren, then the
2812 fact that we did/didnot match may influence
2813 future execution. */
2814 && !(paren && ln == 0))
2816 locinput = PL_reginput;
2817 if (PL_regkind[(U8)OP(next)] == EXACT) {
2818 c1 = (U8)*STRING(next);
2819 if (OP(next) == EXACTF)
2821 else if (OP(next) == EXACTFL)
2822 c2 = PL_fold_locale[c1];
2829 /* This may be improved if l == 0. */
2830 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2831 /* If it could work, try it. */
2833 UCHARAT(PL_reginput) == c1 ||
2834 UCHARAT(PL_reginput) == c2)
2838 PL_regstartp[paren] =
2839 HOPc(PL_reginput, -l) - PL_bostr;
2840 PL_regendp[paren] = PL_reginput - PL_bostr;
2843 PL_regendp[paren] = -1;
2849 /* Couldn't or didn't -- move forward. */
2850 PL_reginput = locinput;
2851 if (regrepeat_hard(scan, 1, &l)) {
2853 locinput = PL_reginput;
2860 n = regrepeat_hard(scan, n, &l);
2861 if (n != 0 && l == 0
2862 /* In fact, this is tricky. If paren, then the
2863 fact that we did/didnot match may influence
2864 future execution. */
2865 && !(paren && ln == 0))
2867 locinput = PL_reginput;
2869 PerlIO_printf(Perl_debug_log,
2870 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
2871 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
2875 if (PL_regkind[(U8)OP(next)] == EXACT) {
2876 c1 = (U8)*STRING(next);
2877 if (OP(next) == EXACTF)
2879 else if (OP(next) == EXACTFL)
2880 c2 = PL_fold_locale[c1];
2889 /* If it could work, try it. */
2891 UCHARAT(PL_reginput) == c1 ||
2892 UCHARAT(PL_reginput) == c2)
2895 PerlIO_printf(Perl_debug_log,
2896 "%*s trying tail with n=%"IVdf"...\n",
2897 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
2901 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2902 PL_regendp[paren] = PL_reginput - PL_bostr;
2905 PL_regendp[paren] = -1;
2911 /* Couldn't or didn't -- back up. */
2913 locinput = HOPc(locinput, -l);
2914 PL_reginput = locinput;
2921 paren = scan->flags; /* Which paren to set */
2922 if (paren > PL_regsize)
2924 if (paren > *PL_reglastparen)
2925 *PL_reglastparen = paren;
2926 ln = ARG1(scan); /* min to match */
2927 n = ARG2(scan); /* max to match */
2928 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2932 ln = ARG1(scan); /* min to match */
2933 n = ARG2(scan); /* max to match */
2934 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2939 scan = NEXTOPER(scan);
2945 scan = NEXTOPER(scan);
2949 * Lookahead to avoid useless match attempts
2950 * when we know what character comes next.
2952 if (PL_regkind[(U8)OP(next)] == EXACT) {
2953 c1 = (U8)*STRING(next);
2954 if (OP(next) == EXACTF)
2956 else if (OP(next) == EXACTFL)
2957 c2 = PL_fold_locale[c1];
2963 PL_reginput = locinput;
2967 if (ln && regrepeat(scan, ln) < ln)
2969 locinput = PL_reginput;
2972 char *e = locinput + n - ln; /* Should not check after this */
2973 char *old = locinput;
2975 if (e >= PL_regeol || (n == REG_INFTY))
2978 /* Find place 'next' could work */
2980 while (locinput <= e && *locinput != c1)
2983 while (locinput <= e
2990 /* PL_reginput == old now */
2991 if (locinput != old) {
2992 ln = 1; /* Did some */
2993 if (regrepeat(scan, locinput - old) <
2997 /* PL_reginput == locinput now */
3000 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
3001 PL_regendp[paren] = locinput - PL_bostr;
3004 PL_regendp[paren] = -1;
3008 PL_reginput = locinput; /* Could be reset... */
3010 /* Couldn't or didn't -- move forward. */
3015 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3016 /* If it could work, try it. */
3018 UCHARAT(PL_reginput) == c1 ||
3019 UCHARAT(PL_reginput) == c2)
3023 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3024 PL_regendp[paren] = PL_reginput - PL_bostr;
3027 PL_regendp[paren] = -1;
3033 /* Couldn't or didn't -- move forward. */
3034 PL_reginput = locinput;
3035 if (regrepeat(scan, 1)) {
3037 locinput = PL_reginput;
3045 n = regrepeat(scan, n);
3046 locinput = PL_reginput;
3047 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3048 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3049 ln = n; /* why back off? */
3050 /* ...because $ and \Z can match before *and* after
3051 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3052 We should back off by one in this case. */
3053 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3059 /* If it could work, try it. */
3061 UCHARAT(PL_reginput) == c1 ||
3062 UCHARAT(PL_reginput) == c2)
3066 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3067 PL_regendp[paren] = PL_reginput - PL_bostr;
3070 PL_regendp[paren] = -1;
3076 /* Couldn't or didn't -- back up. */
3078 PL_reginput = locinput = HOPc(locinput, -1);
3083 /* If it could work, try it. */
3085 UCHARAT(PL_reginput) == c1 ||
3086 UCHARAT(PL_reginput) == c2)
3092 /* Couldn't or didn't -- back up. */
3094 PL_reginput = locinput = HOPc(locinput, -1);
3101 if (PL_reg_call_cc) {
3102 re_cc_state *cur_call_cc = PL_reg_call_cc;
3103 CURCUR *cctmp = PL_regcc;
3104 regexp *re = PL_reg_re;
3105 CHECKPOINT cp, lastcp;
3107 cp = regcppush(0); /* Save *all* the positions. */
3109 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3111 PL_reginput = locinput; /* Make position available to
3113 cache_re(PL_reg_call_cc->re);
3114 PL_regcc = PL_reg_call_cc->cc;
3115 PL_reg_call_cc = PL_reg_call_cc->prev;
3116 if (regmatch(cur_call_cc->node)) {
3117 PL_reg_call_cc = cur_call_cc;
3123 PL_reg_call_cc = cur_call_cc;
3129 PerlIO_printf(Perl_debug_log,
3130 "%*s continuation failed...\n",
3131 REPORT_CODE_OFF+PL_regindent*2, "")
3135 if (locinput < PL_regtill) {
3136 DEBUG_r(PerlIO_printf(Perl_debug_log,
3137 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3139 (long)(locinput - PL_reg_starttry),
3140 (long)(PL_regtill - PL_reg_starttry),
3142 sayNO_FINAL; /* Cannot match: too short. */
3144 PL_reginput = locinput; /* put where regtry can find it */
3145 sayYES_FINAL; /* Success! */
3147 PL_reginput = locinput; /* put where regtry can find it */
3148 sayYES_LOUD; /* Success! */
3151 PL_reginput = locinput;
3156 if (UTF) { /* XXXX This is absolutely
3157 broken, we read before
3159 s = HOPMAYBEc(locinput, -scan->flags);
3165 if (locinput < PL_bostr + scan->flags)
3167 PL_reginput = locinput - scan->flags;
3172 PL_reginput = locinput;
3177 if (UTF) { /* XXXX This is absolutely
3178 broken, we read before
3180 s = HOPMAYBEc(locinput, -scan->flags);
3181 if (!s || s < PL_bostr)
3186 if (locinput < PL_bostr + scan->flags)
3188 PL_reginput = locinput - scan->flags;
3193 PL_reginput = locinput;
3196 inner = NEXTOPER(NEXTOPER(scan));
3197 if (regmatch(inner) != n) {
3212 if (OP(scan) == SUSPEND) {
3213 locinput = PL_reginput;
3214 nextchr = UCHARAT(locinput);
3219 next = scan + ARG(scan);
3224 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3225 PTR2UV(scan), OP(scan));
3226 Perl_croak(aTHX_ "regexp memory corruption");
3232 * We get here only if there's trouble -- normally "case END" is
3233 * the terminating point.
3235 Perl_croak(aTHX_ "corrupted regexp pointers");
3241 PerlIO_printf(Perl_debug_log,
3242 "%*s %scould match...%s\n",
3243 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3247 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3248 PL_colors[4],PL_colors[5]));
3257 PerlIO_printf(Perl_debug_log,
3258 "%*s %sfailed...%s\n",
3259 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3271 - regrepeat - repeatedly match something simple, report how many
3274 * [This routine now assumes that it will only match on things of length 1.
3275 * That was true before, but now we assume scan - reginput is the count,
3276 * rather than incrementing count on every character. [Er, except utf8.]]
3279 S_regrepeat(pTHX_ regnode *p, I32 max)
3282 register char *scan;
3284 register char *loceol = PL_regeol;
3285 register I32 hardcount = 0;
3288 if (max != REG_INFTY && max < loceol - scan)
3289 loceol = scan + max;
3292 while (scan < loceol && *scan != '\n')
3300 while (scan < loceol && *scan != '\n') {
3301 scan += UTF8SKIP(scan);
3307 while (scan < loceol) {
3308 scan += UTF8SKIP(scan);
3312 case EXACT: /* length of string is 1 */
3314 while (scan < loceol && UCHARAT(scan) == c)
3317 case EXACTF: /* length of string is 1 */
3319 while (scan < loceol &&
3320 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3323 case EXACTFL: /* length of string is 1 */
3324 PL_reg_flags |= RF_tainted;
3326 while (scan < loceol &&
3327 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3332 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3333 scan += UTF8SKIP(scan);
3338 while (scan < loceol && REGINCLASS(p, *scan))
3342 while (scan < loceol && isALNUM(*scan))
3347 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3348 scan += UTF8SKIP(scan);
3353 PL_reg_flags |= RF_tainted;
3354 while (scan < loceol && isALNUM_LC(*scan))
3358 PL_reg_flags |= RF_tainted;
3360 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3361 scan += UTF8SKIP(scan);
3367 while (scan < loceol && !isALNUM(*scan))
3372 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3373 scan += UTF8SKIP(scan);
3378 PL_reg_flags |= RF_tainted;
3379 while (scan < loceol && !isALNUM_LC(*scan))
3383 PL_reg_flags |= RF_tainted;
3385 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3386 scan += UTF8SKIP(scan);
3391 while (scan < loceol && isSPACE(*scan))
3396 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3397 scan += UTF8SKIP(scan);
3402 PL_reg_flags |= RF_tainted;
3403 while (scan < loceol && isSPACE_LC(*scan))
3407 PL_reg_flags |= RF_tainted;
3409 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3410 scan += UTF8SKIP(scan);
3415 while (scan < loceol && !isSPACE(*scan))
3420 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3421 scan += UTF8SKIP(scan);
3426 PL_reg_flags |= RF_tainted;
3427 while (scan < loceol && !isSPACE_LC(*scan))
3431 PL_reg_flags |= RF_tainted;
3433 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3434 scan += UTF8SKIP(scan);
3439 while (scan < loceol && isDIGIT(*scan))
3444 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3445 scan += UTF8SKIP(scan);
3451 while (scan < loceol && !isDIGIT(*scan))
3456 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3457 scan += UTF8SKIP(scan);
3461 default: /* Called on something of 0 width. */
3462 break; /* So match right here or not at all. */
3468 c = scan - PL_reginput;
3473 SV *prop = sv_newmortal();
3476 PerlIO_printf(Perl_debug_log,
3477 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3478 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3485 - regrepeat_hard - repeatedly match something, report total lenth and length
3487 * The repeater is supposed to have constant length.
3491 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3494 register char *scan;
3495 register char *start;
3496 register char *loceol = PL_regeol;
3498 I32 count = 0, res = 1;
3503 start = PL_reginput;
3505 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3508 while (start < PL_reginput) {
3510 start += UTF8SKIP(start);
3521 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3523 *lp = l = PL_reginput - start;
3524 if (max != REG_INFTY && l*max < loceol - scan)
3525 loceol = scan + l*max;
3538 - reginclass - determine if a character falls into a character class
3542 S_reginclass(pTHX_ register regnode *p, register I32 c)
3545 char flags = ANYOF_FLAGS(p);
3549 if (ANYOF_BITMAP_TEST(p, c))
3551 else if (flags & ANYOF_FOLD) {
3553 if (flags & ANYOF_LOCALE) {
3554 PL_reg_flags |= RF_tainted;
3555 cf = PL_fold_locale[c];
3559 if (ANYOF_BITMAP_TEST(p, cf))
3563 if (!match && (flags & ANYOF_CLASS)) {
3564 PL_reg_flags |= RF_tainted;
3566 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3567 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3568 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3569 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3570 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3571 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3572 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3573 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3574 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3575 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3576 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3577 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3578 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3579 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3580 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3581 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3582 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3583 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3584 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3585 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3586 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3587 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3588 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3589 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3590 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3591 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3592 ) /* How's that for a conditional? */
3598 return (flags & ANYOF_INVERT) ? !match : match;
3602 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3605 char flags = ARG1(f);
3607 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3609 if (swash_fetch(sv, p))
3611 else if (flags & ANYOF_FOLD) {
3613 U8 tmpbuf[UTF8_MAXLEN];
3614 if (flags & ANYOF_LOCALE) {
3615 PL_reg_flags |= RF_tainted;
3616 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3619 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3620 if (swash_fetch(sv, tmpbuf))
3624 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3626 return (flags & ANYOF_INVERT) ? !match : match;
3630 S_reghop(pTHX_ U8 *s, I32 off)
3634 while (off-- && s < (U8*)PL_regeol)
3639 if (s > (U8*)PL_bostr) {
3642 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3644 } /* XXX could check well-formedness here */
3652 S_reghopmaybe(pTHX_ U8* s, I32 off)
3656 while (off-- && s < (U8*)PL_regeol)
3663 if (s > (U8*)PL_bostr) {
3666 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3668 } /* XXX could check well-formedness here */
3684 restore_pos(pTHXo_ void *arg)
3687 if (PL_reg_eval_set) {
3688 if (PL_reg_oldsaved) {
3689 PL_reg_re->subbeg = PL_reg_oldsaved;
3690 PL_reg_re->sublen = PL_reg_oldsavedlen;
3691 RX_MATCH_COPIED_on(PL_reg_re);
3693 PL_reg_magic->mg_len = PL_reg_oldpos;
3694 PL_reg_eval_set = 0;
3695 PL_curpm = PL_reg_oldcurpm;