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
665 other heuristics too. */
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]));
751 - regexec_flags - match a regexp against a string
754 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
755 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
756 /* strend: pointer to null at end of string */
757 /* strbeg: real beginning of string */
758 /* minend: end of match must be >=minend after stringarg. */
759 /* data: May be used for some additional optimizations. */
760 /* nosave: For optimizations. */
765 register char *startpos = stringarg;
767 I32 minlen; /* must match at least this many chars */
768 I32 dontbother = 0; /* how many characters not to try at end */
769 I32 start_shift = 0; /* Offset of the start to find
770 constant substr. */ /* CC */
771 I32 end_shift = 0; /* Same for the end. */ /* CC */
772 I32 scream_pos = -1; /* Internal iterator of scream. */
774 SV* oreplsv = GvSV(PL_replgv);
780 PL_regnarrate = PL_debug & 512;
784 if (prog == NULL || startpos == NULL) {
785 Perl_croak(aTHX_ "NULL regexp parameter");
789 minlen = prog->minlen;
790 if (strend - startpos < minlen) goto phooey;
792 if (startpos == strbeg) /* is ^ valid at stringarg? */
795 PL_regprev = (U32)stringarg[-1];
796 if (!PL_multiline && PL_regprev == '\n')
797 PL_regprev = '\0'; /* force ^ to NOT match */
800 /* Check validity of program. */
801 if (UCHARAT(prog->program) != REG_MAGIC) {
802 Perl_croak(aTHX_ "corrupted regexp program");
809 if (prog->reganch & ROPT_UTF8)
810 PL_reg_flags |= RF_utf8;
812 /* Mark beginning of line for ^ and lookbehind. */
813 PL_regbol = startpos;
817 /* Mark end of line for $ (and such) */
820 /* see how far we have to get to not match where we matched before */
821 PL_regtill = startpos+minend;
823 /* We start without call_cc context. */
826 /* If there is a "must appear" string, look for it. */
829 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
832 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
833 PL_reg_ganch = startpos;
834 else if (sv && SvTYPE(sv) >= SVt_PVMG
836 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
837 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
838 if (prog->reganch & ROPT_ANCH_GPOS) {
839 if (s > PL_reg_ganch)
844 else /* pos() not defined */
845 PL_reg_ganch = strbeg;
848 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
849 re_scream_pos_data d;
851 d.scream_olds = &scream_olds;
852 d.scream_pos = &scream_pos;
853 s = re_intuit_start(prog, sv, s, strend, flags, &d);
855 goto phooey; /* not present */
858 DEBUG_r( if (!PL_colorset) reginitcolors() );
859 DEBUG_r(PerlIO_printf(Perl_debug_log,
860 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
861 PL_colors[4],PL_colors[5],PL_colors[0],
864 (strlen(prog->precomp) > 60 ? "..." : ""),
866 (int)(strend - startpos > 60 ? 60 : strend - startpos),
867 startpos, PL_colors[1],
868 (strend - startpos > 60 ? "..." : ""))
871 /* Simplest case: anchored match need be tried only once. */
872 /* [unless only anchor is BOL and multiline is set] */
873 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
874 if (s == startpos && regtry(prog, startpos))
876 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
877 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
882 dontbother = minlen - 1;
883 end = HOPc(strend, -dontbother) - 1;
884 /* for multiline we only have to try after newlines */
885 if (prog->check_substr) {
894 if (prog->reganch & RE_USE_INTUIT) {
895 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
906 if (*s++ == '\n') { /* don't need PL_utf8skip here */
914 } else if (prog->reganch & ROPT_ANCH_GPOS) {
915 if (regtry(prog, PL_reg_ganch))
920 /* Messy cases: unanchored match. */
921 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
922 /* we have /x+whatever/ */
923 /* it must be a one character string (XXXX Except UTF?) */
924 char ch = SvPVX(prog->anchored_substr)[0];
928 if (regtry(prog, s)) goto got_it;
930 while (s < strend && *s == ch)
939 if (regtry(prog, s)) goto got_it;
941 while (s < strend && *s == ch)
949 else if (prog->anchored_substr != Nullsv
950 || (prog->float_substr != Nullsv
951 && prog->float_max_offset < strend - s)) {
952 SV *must = prog->anchored_substr
953 ? prog->anchored_substr : prog->float_substr;
955 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
957 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
958 I32 delta = back_max - back_min;
959 char *last = HOPc(strend, /* Cannot start after this */
960 -(I32)(CHR_SVLEN(must)
961 - (SvTAIL(must) != 0) + back_min));
962 char *last1; /* Last position checked before */
967 last1 = s - 1; /* bogus */
969 /* XXXX check_substr already used to find `s', can optimize if
970 check_substr==must. */
972 dontbother = end_shift;
973 strend = HOPc(strend, -dontbother);
974 while ( (s <= last) &&
975 ((flags & REXEC_SCREAM)
976 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
977 end_shift, &scream_pos, 0))
978 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
979 (unsigned char*)strend, must,
980 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
981 if (HOPc(s, -back_max) > last1) {
982 last1 = HOPc(s, -back_min);
983 s = HOPc(s, -back_max);
986 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
988 last1 = HOPc(s, -back_min);
1000 if (regtry(prog, s))
1008 else if (c = prog->regstclass) {
1009 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
1017 dontbother = minlen - 1;
1018 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
1020 /* We know what class it must start with. */
1023 while (s < strend) {
1024 if (REGINCLASSUTF8(c, (U8*)s)) {
1025 if (tmp && regtry(prog, s))
1036 while (s < strend) {
1037 if (REGINCLASS(c, *s)) {
1038 if (tmp && regtry(prog, s))
1058 c2 = PL_fold_locale[c1];
1062 /* Here it is NOT UTF! */
1066 && (ln == 1 || (OP(c) == EXACTF
1068 : ibcmp_locale(s, m, ln)))
1069 && (norun || regtry(prog, s)) )
1075 if ( (*s == c1 || *s == c2)
1076 && (ln == 1 || (OP(c) == EXACTF
1078 : ibcmp_locale(s, m, ln)))
1079 && (norun || regtry(prog, s)) )
1086 PL_reg_flags |= RF_tainted;
1089 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
1090 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1091 while (s < strend) {
1092 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1094 if ((norun || regtry(prog, s)))
1099 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1103 PL_reg_flags |= RF_tainted;
1106 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
1107 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
1108 while (s < strend) {
1109 if (tmp == !(OP(c) == BOUND ?
1110 swash_fetch(PL_utf8_alnum, (U8*)s) :
1111 isALNUM_LC_utf8((U8*)s)))
1114 if ((norun || regtry(prog, s)))
1119 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1123 PL_reg_flags |= RF_tainted;
1126 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
1127 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1128 while (s < strend) {
1129 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1131 else if ((norun || regtry(prog, s)))
1135 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1139 PL_reg_flags |= RF_tainted;
1143 strend = reghop_c(strend, -1);
1144 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
1145 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
1146 while (s < strend) {
1147 if (tmp == !(OP(c) == NBOUND ?
1148 swash_fetch(PL_utf8_alnum, (U8*)s) :
1149 isALNUM_LC_utf8((U8*)s)))
1151 else if ((norun || regtry(prog, s)))
1155 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1159 while (s < strend) {
1161 if (tmp && (norun || regtry(prog, s)))
1172 while (s < strend) {
1173 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1174 if (tmp && (norun || regtry(prog, s)))
1185 PL_reg_flags |= RF_tainted;
1186 while (s < strend) {
1187 if (isALNUM_LC(*s)) {
1188 if (tmp && (norun || regtry(prog, s)))
1199 PL_reg_flags |= RF_tainted;
1200 while (s < strend) {
1201 if (isALNUM_LC_utf8((U8*)s)) {
1202 if (tmp && (norun || regtry(prog, s)))
1213 while (s < strend) {
1215 if (tmp && (norun || regtry(prog, s)))
1226 while (s < strend) {
1227 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1228 if (tmp && (norun || regtry(prog, s)))
1239 PL_reg_flags |= RF_tainted;
1240 while (s < strend) {
1241 if (!isALNUM_LC(*s)) {
1242 if (tmp && (norun || regtry(prog, s)))
1253 PL_reg_flags |= RF_tainted;
1254 while (s < strend) {
1255 if (!isALNUM_LC_utf8((U8*)s)) {
1256 if (tmp && (norun || regtry(prog, s)))
1267 while (s < strend) {
1269 if (tmp && (norun || regtry(prog, s)))
1280 while (s < strend) {
1281 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1282 if (tmp && (norun || regtry(prog, s)))
1293 PL_reg_flags |= RF_tainted;
1294 while (s < strend) {
1295 if (isSPACE_LC(*s)) {
1296 if (tmp && (norun || regtry(prog, s)))
1307 PL_reg_flags |= RF_tainted;
1308 while (s < strend) {
1309 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1310 if (tmp && (norun || regtry(prog, s)))
1321 while (s < strend) {
1323 if (tmp && (norun || regtry(prog, s)))
1334 while (s < strend) {
1335 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1336 if (tmp && (norun || regtry(prog, s)))
1347 PL_reg_flags |= RF_tainted;
1348 while (s < strend) {
1349 if (!isSPACE_LC(*s)) {
1350 if (tmp && (norun || regtry(prog, s)))
1361 PL_reg_flags |= RF_tainted;
1362 while (s < strend) {
1363 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1364 if (tmp && (norun || regtry(prog, s)))
1375 while (s < strend) {
1377 if (tmp && (norun || regtry(prog, s)))
1388 while (s < strend) {
1389 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1390 if (tmp && (norun || regtry(prog, s)))
1401 PL_reg_flags |= RF_tainted;
1402 while (s < strend) {
1403 if (isDIGIT_LC(*s)) {
1404 if (tmp && (norun || regtry(prog, s)))
1415 PL_reg_flags |= RF_tainted;
1416 while (s < strend) {
1417 if (isDIGIT_LC_utf8((U8*)s)) {
1418 if (tmp && (norun || regtry(prog, s)))
1429 while (s < strend) {
1431 if (tmp && (norun || regtry(prog, s)))
1442 while (s < strend) {
1443 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1444 if (tmp && (norun || regtry(prog, s)))
1455 PL_reg_flags |= RF_tainted;
1456 while (s < strend) {
1457 if (!isDIGIT_LC(*s)) {
1458 if (tmp && (norun || regtry(prog, s)))
1469 PL_reg_flags |= RF_tainted;
1470 while (s < strend) {
1471 if (!isDIGIT_LC_utf8((U8*)s)) {
1472 if (tmp && (norun || regtry(prog, s)))
1483 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1492 - regexec_flags - match a regexp against a string
1495 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1496 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1497 /* strend: pointer to null at end of string */
1498 /* strbeg: real beginning of string */
1499 /* minend: end of match must be >=minend after stringarg. */
1500 /* data: May be used for some additional optimizations. */
1501 /* nosave: For optimizations. */
1505 register regnode *c;
1506 register char *startpos = stringarg;
1508 I32 minlen; /* must match at least this many chars */
1509 I32 dontbother = 0; /* how many characters not to try at end */
1510 I32 start_shift = 0; /* Offset of the start to find
1511 constant substr. */ /* CC */
1512 I32 end_shift = 0; /* Same for the end. */ /* CC */
1513 I32 scream_pos = -1; /* Internal iterator of scream. */
1515 SV* oreplsv = GvSV(PL_replgv);
1521 PL_regnarrate = PL_debug & 512;
1524 /* Be paranoid... */
1525 if (prog == NULL || startpos == NULL) {
1526 Perl_croak(aTHX_ "NULL regexp parameter");
1530 minlen = prog->minlen;
1531 if (strend - startpos < minlen) goto phooey;
1533 if (startpos == strbeg) /* is ^ valid at stringarg? */
1536 PL_regprev = (U32)stringarg[-1];
1537 if (!PL_multiline && PL_regprev == '\n')
1538 PL_regprev = '\0'; /* force ^ to NOT match */
1541 /* Check validity of program. */
1542 if (UCHARAT(prog->program) != REG_MAGIC) {
1543 Perl_croak(aTHX_ "corrupted regexp program");
1547 PL_reg_eval_set = 0;
1550 if (prog->reganch & ROPT_UTF8)
1551 PL_reg_flags |= RF_utf8;
1553 /* Mark beginning of line for ^ and lookbehind. */
1554 PL_regbol = startpos;
1558 /* Mark end of line for $ (and such) */
1561 /* see how far we have to get to not match where we matched before */
1562 PL_regtill = startpos+minend;
1564 /* We start without call_cc context. */
1567 /* If there is a "must appear" string, look for it. */
1570 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1573 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1574 PL_reg_ganch = startpos;
1575 else if (sv && SvTYPE(sv) >= SVt_PVMG
1577 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1578 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1579 if (prog->reganch & ROPT_ANCH_GPOS) {
1580 if (s > PL_reg_ganch)
1585 else /* pos() not defined */
1586 PL_reg_ganch = strbeg;
1589 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1590 re_scream_pos_data d;
1592 d.scream_olds = &scream_olds;
1593 d.scream_pos = &scream_pos;
1594 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1596 goto phooey; /* not present */
1599 DEBUG_r( if (!PL_colorset) reginitcolors() );
1600 DEBUG_r(PerlIO_printf(Perl_debug_log,
1601 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1602 PL_colors[4],PL_colors[5],PL_colors[0],
1605 (strlen(prog->precomp) > 60 ? "..." : ""),
1607 (strend - startpos > 60 ? 60 : strend - startpos),
1608 startpos, PL_colors[1],
1609 (strend - startpos > 60 ? "..." : ""))
1612 /* Simplest case: anchored match need be tried only once. */
1613 /* [unless only anchor is BOL and multiline is set] */
1614 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1615 if (s == startpos && regtry(prog, startpos))
1617 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1618 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1623 dontbother = minlen - 1;
1624 end = HOPc(strend, -dontbother) - 1;
1625 /* for multiline we only have to try after newlines */
1626 if (prog->check_substr) {
1630 if (regtry(prog, s))
1635 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1643 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1644 if (regtry(prog, s))
1651 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1652 if (regtry(prog, PL_reg_ganch))
1657 /* Messy cases: unanchored match. */
1658 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1659 /* we have /x+whatever/ */
1660 /* it must be a one character string (XXXX Except UTF?) */
1661 char ch = SvPVX(prog->anchored_substr)[0];
1663 while (s < strend) {
1665 if (regtry(prog, s)) goto got_it;
1667 while (s < strend && *s == ch)
1674 while (s < strend) {
1676 if (regtry(prog, s)) goto got_it;
1678 while (s < strend && *s == ch)
1686 else if (prog->anchored_substr != Nullsv
1687 || (prog->float_substr != Nullsv
1688 && prog->float_max_offset < strend - s)) {
1689 SV *must = prog->anchored_substr
1690 ? prog->anchored_substr : prog->float_substr;
1692 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1694 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1695 I32 delta = back_max - back_min;
1696 char *last = HOPc(strend, /* Cannot start after this */
1697 -(I32)(CHR_SVLEN(must)
1698 - (SvTAIL(must) != 0) + back_min));
1699 char *last1; /* Last position checked before */
1702 last1 = HOPc(s, -1);
1704 last1 = s - 1; /* bogus */
1706 /* XXXX check_substr already used to find `s', can optimize if
1707 check_substr==must. */
1709 dontbother = end_shift;
1710 strend = HOPc(strend, -dontbother);
1711 while ( (s <= last) &&
1712 ((flags & REXEC_SCREAM)
1713 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1714 end_shift, &scream_pos, 0))
1715 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1716 (unsigned char*)strend, must,
1717 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1718 if (HOPc(s, -back_max) > last1) {
1719 last1 = HOPc(s, -back_min);
1720 s = HOPc(s, -back_max);
1723 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1725 last1 = HOPc(s, -back_min);
1729 while (s <= last1) {
1730 if (regtry(prog, s))
1736 while (s <= last1) {
1737 if (regtry(prog, s))
1745 else if (c = prog->regstclass) {
1746 if (minlen) /* don't bother with what can't match */
1747 strend = HOPc(strend, -(minlen - 1));
1748 if (find_byclass(prog, c, s, strend, startpos, 0))
1753 if (prog->float_substr != Nullsv) { /* Trim the end. */
1755 I32 oldpos = scream_pos;
1757 if (flags & REXEC_SCREAM) {
1758 last = screaminstr(sv, prog->float_substr, s - strbeg,
1759 end_shift, &scream_pos, 1); /* last one */
1761 last = scream_olds; /* Only one occurence. */
1765 char *little = SvPV(prog->float_substr, len);
1767 if (SvTAIL(prog->float_substr)) {
1768 if (memEQ(strend - len + 1, little, len - 1))
1769 last = strend - len + 1;
1770 else if (!PL_multiline)
1771 last = memEQ(strend - len, little, len)
1772 ? strend - len : Nullch;
1778 last = rninstr(s, strend, little, little + len);
1780 last = strend; /* matching `$' */
1783 if (last == NULL) goto phooey; /* Should not happen! */
1784 dontbother = strend - last + prog->float_min_offset;
1786 if (minlen && (dontbother < minlen))
1787 dontbother = minlen - 1;
1788 strend -= dontbother; /* this one's always in bytes! */
1789 /* We don't know much -- general case. */
1792 if (regtry(prog, s))
1801 if (regtry(prog, s))
1803 } while (s++ < strend);
1811 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1813 if (PL_reg_eval_set) {
1814 /* Preserve the current value of $^R */
1815 if (oreplsv != GvSV(PL_replgv))
1816 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1817 restored, the value remains
1819 restore_pos(aTHXo_ 0);
1822 /* make sure $`, $&, $', and $digit will work later */
1823 if ( !(flags & REXEC_NOT_FIRST) ) {
1824 if (RX_MATCH_COPIED(prog)) {
1825 Safefree(prog->subbeg);
1826 RX_MATCH_COPIED_off(prog);
1828 if (flags & REXEC_COPY_STR) {
1829 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1831 s = savepvn(strbeg, i);
1834 RX_MATCH_COPIED_on(prog);
1837 prog->subbeg = strbeg;
1838 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1845 if (PL_reg_eval_set)
1846 restore_pos(aTHXo_ 0);
1851 - regtry - try match at specific point
1853 STATIC I32 /* 0 failure, 1 success */
1854 S_regtry(pTHX_ regexp *prog, char *startpos)
1862 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1865 PL_reg_eval_set = RS_init;
1867 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1868 (IV)(PL_stack_sp - PL_stack_base));
1870 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1871 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1872 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1874 /* Apparently this is not needed, judging by wantarray. */
1875 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1876 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1879 /* Make $_ available to executed code. */
1880 if (PL_reg_sv != DEFSV) {
1881 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1886 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1887 && (mg = mg_find(PL_reg_sv, 'g')))) {
1888 /* prepare for quick setting of pos */
1889 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1890 mg = mg_find(PL_reg_sv, 'g');
1894 PL_reg_oldpos = mg->mg_len;
1895 SAVEDESTRUCTOR_X(restore_pos, 0);
1898 New(22,PL_reg_curpm, 1, PMOP);
1899 PL_reg_curpm->op_pmregexp = prog;
1900 PL_reg_oldcurpm = PL_curpm;
1901 PL_curpm = PL_reg_curpm;
1902 if (RX_MATCH_COPIED(prog)) {
1903 /* Here is a serious problem: we cannot rewrite subbeg,
1904 since it may be needed if this match fails. Thus
1905 $` inside (?{}) could fail... */
1906 PL_reg_oldsaved = prog->subbeg;
1907 PL_reg_oldsavedlen = prog->sublen;
1908 RX_MATCH_COPIED_off(prog);
1911 PL_reg_oldsaved = Nullch;
1912 prog->subbeg = PL_bostr;
1913 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1915 prog->startp[0] = startpos - PL_bostr;
1916 PL_reginput = startpos;
1917 PL_regstartp = prog->startp;
1918 PL_regendp = prog->endp;
1919 PL_reglastparen = &prog->lastparen;
1920 prog->lastparen = 0;
1922 DEBUG_r(PL_reg_starttry = startpos);
1923 if (PL_reg_start_tmpl <= prog->nparens) {
1924 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1925 if(PL_reg_start_tmp)
1926 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1928 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1931 /* XXXX What this code is doing here?!!! There should be no need
1932 to do this again and again, PL_reglastparen should take care of
1936 if (prog->nparens) {
1937 for (i = prog->nparens; i >= 1; i--) {
1943 if (regmatch(prog->program + 1)) {
1944 prog->endp[0] = PL_reginput - PL_bostr;
1952 - regmatch - main matching routine
1954 * Conceptually the strategy is simple: check to see whether the current
1955 * node matches, call self recursively to see whether the rest matches,
1956 * and then act accordingly. In practice we make some effort to avoid
1957 * recursion, in particular by going through "ordinary" nodes (that don't
1958 * need to know whether the rest of the match failed) by a loop instead of
1961 /* [lwall] I've hoisted the register declarations to the outer block in order to
1962 * maybe save a little bit of pushing and popping on the stack. It also takes
1963 * advantage of machines that use a register save mask on subroutine entry.
1965 STATIC I32 /* 0 failure, 1 success */
1966 S_regmatch(pTHX_ regnode *prog)
1969 register regnode *scan; /* Current node. */
1970 regnode *next; /* Next node. */
1971 regnode *inner; /* Next node in internal branch. */
1972 register I32 nextchr; /* renamed nextchr - nextchar colides with
1973 function of same name */
1974 register I32 n; /* no or next */
1975 register I32 ln; /* len or last */
1976 register char *s; /* operand or save */
1977 register char *locinput = PL_reginput;
1978 register I32 c1, c2, paren; /* case fold search, parenth */
1979 int minmod = 0, sw = 0, logical = 0;
1984 /* Note that nextchr is a byte even in UTF */
1985 nextchr = UCHARAT(locinput);
1987 while (scan != NULL) {
1988 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1990 # define sayYES goto yes
1991 # define sayNO goto no
1992 # define sayYES_FINAL goto yes_final
1993 # define sayYES_LOUD goto yes_loud
1994 # define sayNO_FINAL goto no_final
1995 # define sayNO_SILENT goto do_no
1996 # define saySAME(x) if (x) goto yes; else goto no
1997 # define REPORT_CODE_OFF 24
1999 # define sayYES return 1
2000 # define sayNO return 0
2001 # define sayYES_FINAL return 1
2002 # define sayYES_LOUD return 1
2003 # define sayNO_FINAL return 0
2004 # define sayNO_SILENT return 0
2005 # define saySAME(x) return x
2008 SV *prop = sv_newmortal();
2009 int docolor = *PL_colors[0];
2010 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2011 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
2012 /* The part of the string before starttry has one color
2013 (pref0_len chars), between starttry and current
2014 position another one (pref_len - pref0_len chars),
2015 after the current position the third one.
2016 We assume that pref0_len <= pref_len, otherwise we
2017 decrease pref0_len. */
2018 int pref_len = (locinput - PL_bostr > (5 + taill) - l
2019 ? (5 + taill) - l : locinput - PL_bostr);
2020 int pref0_len = pref_len - (locinput - PL_reg_starttry);
2022 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2023 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2024 ? (5 + taill) - pref_len : PL_regeol - locinput);
2027 if (pref0_len > pref_len)
2028 pref0_len = pref_len;
2029 regprop(prop, scan);
2030 PerlIO_printf(Perl_debug_log,
2031 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2032 (IV)(locinput - PL_bostr),
2033 PL_colors[4], pref0_len,
2034 locinput - pref_len, PL_colors[5],
2035 PL_colors[2], pref_len - pref0_len,
2036 locinput - pref_len + pref0_len, PL_colors[3],
2037 (docolor ? "" : "> <"),
2038 PL_colors[0], l, locinput, PL_colors[1],
2039 15 - l - pref_len + 1,
2041 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2045 next = scan + NEXT_OFF(scan);
2051 if (locinput == PL_bostr
2052 ? PL_regprev == '\n'
2054 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2056 /* regtill = regbol; */
2061 if (locinput == PL_bostr
2062 ? PL_regprev == '\n'
2063 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2069 if (locinput == PL_regbol && PL_regprev == '\n')
2073 if (locinput == PL_reg_ganch)
2083 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2088 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2090 if (PL_regeol - locinput > 1)
2094 if (PL_regeol != locinput)
2098 if (nextchr & 0x80) {
2099 locinput += PL_utf8skip[nextchr];
2100 if (locinput > PL_regeol)
2102 nextchr = UCHARAT(locinput);
2105 if (!nextchr && locinput >= PL_regeol)
2107 nextchr = UCHARAT(++locinput);
2110 if (!nextchr && locinput >= PL_regeol)
2112 nextchr = UCHARAT(++locinput);
2115 if (nextchr & 0x80) {
2116 locinput += PL_utf8skip[nextchr];
2117 if (locinput > PL_regeol)
2119 nextchr = UCHARAT(locinput);
2122 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2124 nextchr = UCHARAT(++locinput);
2127 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2129 nextchr = UCHARAT(++locinput);
2134 /* Inline the first character, for speed. */
2135 if (UCHARAT(s) != nextchr)
2137 if (PL_regeol - locinput < ln)
2139 if (ln > 1 && memNE(s, locinput, ln))
2142 nextchr = UCHARAT(locinput);
2145 PL_reg_flags |= RF_tainted;
2154 c1 = OP(scan) == EXACTF;
2158 if (utf8_to_uv((U8*)s, 0) != (c1 ?
2159 toLOWER_utf8((U8*)l) :
2160 toLOWER_LC_utf8((U8*)l)))
2168 nextchr = UCHARAT(locinput);
2172 /* Inline the first character, for speed. */
2173 if (UCHARAT(s) != nextchr &&
2174 UCHARAT(s) != ((OP(scan) == EXACTF)
2175 ? PL_fold : PL_fold_locale)[nextchr])
2177 if (PL_regeol - locinput < ln)
2179 if (ln > 1 && (OP(scan) == EXACTF
2180 ? ibcmp(s, locinput, ln)
2181 : ibcmp_locale(s, locinput, ln)))
2184 nextchr = UCHARAT(locinput);
2187 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2189 if (locinput >= PL_regeol)
2191 locinput += PL_utf8skip[nextchr];
2192 nextchr = UCHARAT(locinput);
2196 nextchr = UCHARAT(locinput);
2197 if (!REGINCLASS(scan, nextchr))
2199 if (!nextchr && locinput >= PL_regeol)
2201 nextchr = UCHARAT(++locinput);
2204 PL_reg_flags |= RF_tainted;
2209 if (!(OP(scan) == ALNUM
2210 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2212 nextchr = UCHARAT(++locinput);
2215 PL_reg_flags |= RF_tainted;
2220 if (nextchr & 0x80) {
2221 if (!(OP(scan) == ALNUMUTF8
2222 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2223 : isALNUM_LC_utf8((U8*)locinput)))
2227 locinput += PL_utf8skip[nextchr];
2228 nextchr = UCHARAT(locinput);
2231 if (!(OP(scan) == ALNUMUTF8
2232 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2234 nextchr = UCHARAT(++locinput);
2237 PL_reg_flags |= RF_tainted;
2240 if (!nextchr && locinput >= PL_regeol)
2242 if (OP(scan) == NALNUM
2243 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2245 nextchr = UCHARAT(++locinput);
2248 PL_reg_flags |= RF_tainted;
2251 if (!nextchr && locinput >= PL_regeol)
2253 if (nextchr & 0x80) {
2254 if (OP(scan) == NALNUMUTF8
2255 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2256 : isALNUM_LC_utf8((U8*)locinput))
2260 locinput += PL_utf8skip[nextchr];
2261 nextchr = UCHARAT(locinput);
2264 if (OP(scan) == NALNUMUTF8
2265 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2267 nextchr = UCHARAT(++locinput);
2271 PL_reg_flags |= RF_tainted;
2275 /* was last char in word? */
2276 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2277 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2279 n = isALNUM(nextchr);
2282 ln = isALNUM_LC(ln);
2283 n = isALNUM_LC(nextchr);
2285 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2290 PL_reg_flags |= RF_tainted;
2294 /* was last char in word? */
2295 ln = (locinput != PL_regbol)
2296 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2297 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2298 ln = isALNUM_uni(ln);
2299 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2302 ln = isALNUM_LC_uni(ln);
2303 n = isALNUM_LC_utf8((U8*)locinput);
2305 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2309 PL_reg_flags |= RF_tainted;
2312 if (!nextchr && locinput >= PL_regeol)
2314 if (!(OP(scan) == SPACE
2315 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2317 nextchr = UCHARAT(++locinput);
2320 PL_reg_flags |= RF_tainted;
2323 if (!nextchr && locinput >= PL_regeol)
2325 if (nextchr & 0x80) {
2326 if (!(OP(scan) == SPACEUTF8
2327 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2328 : isSPACE_LC_utf8((U8*)locinput)))
2332 locinput += PL_utf8skip[nextchr];
2333 nextchr = UCHARAT(locinput);
2336 if (!(OP(scan) == SPACEUTF8
2337 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2339 nextchr = UCHARAT(++locinput);
2342 PL_reg_flags |= RF_tainted;
2347 if (OP(scan) == SPACE
2348 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2350 nextchr = UCHARAT(++locinput);
2353 PL_reg_flags |= RF_tainted;
2358 if (nextchr & 0x80) {
2359 if (OP(scan) == NSPACEUTF8
2360 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2361 : isSPACE_LC_utf8((U8*)locinput))
2365 locinput += PL_utf8skip[nextchr];
2366 nextchr = UCHARAT(locinput);
2369 if (OP(scan) == NSPACEUTF8
2370 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2372 nextchr = UCHARAT(++locinput);
2375 PL_reg_flags |= RF_tainted;
2378 if (!nextchr && locinput >= PL_regeol)
2380 if (!(OP(scan) == DIGIT
2381 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2383 nextchr = UCHARAT(++locinput);
2386 PL_reg_flags |= RF_tainted;
2391 if (nextchr & 0x80) {
2392 if (OP(scan) == NDIGITUTF8
2393 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2394 : isDIGIT_LC_utf8((U8*)locinput))
2398 locinput += PL_utf8skip[nextchr];
2399 nextchr = UCHARAT(locinput);
2402 if (!isDIGIT(nextchr))
2404 nextchr = UCHARAT(++locinput);
2407 PL_reg_flags |= RF_tainted;
2412 if (OP(scan) == DIGIT
2413 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2415 nextchr = UCHARAT(++locinput);
2418 PL_reg_flags |= RF_tainted;
2421 if (!nextchr && locinput >= PL_regeol)
2423 if (nextchr & 0x80) {
2424 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2426 locinput += PL_utf8skip[nextchr];
2427 nextchr = UCHARAT(locinput);
2430 if (isDIGIT(nextchr))
2432 nextchr = UCHARAT(++locinput);
2435 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2437 locinput += PL_utf8skip[nextchr];
2438 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2439 locinput += UTF8SKIP(locinput);
2440 if (locinput > PL_regeol)
2442 nextchr = UCHARAT(locinput);
2445 PL_reg_flags |= RF_tainted;
2449 n = ARG(scan); /* which paren pair */
2450 ln = PL_regstartp[n];
2451 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2452 if (*PL_reglastparen < n || ln == -1)
2453 sayNO; /* Do not match unless seen CLOSEn. */
2454 if (ln == PL_regendp[n])
2458 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2460 char *e = PL_bostr + PL_regendp[n];
2462 * Note that we can't do the "other character" lookup trick as
2463 * in the 8-bit case (no pun intended) because in Unicode we
2464 * have to map both upper and title case to lower case.
2466 if (OP(scan) == REFF) {
2470 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2480 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2487 nextchr = UCHARAT(locinput);
2491 /* Inline the first character, for speed. */
2492 if (UCHARAT(s) != nextchr &&
2494 (UCHARAT(s) != ((OP(scan) == REFF
2495 ? PL_fold : PL_fold_locale)[nextchr]))))
2497 ln = PL_regendp[n] - ln;
2498 if (locinput + ln > PL_regeol)
2500 if (ln > 1 && (OP(scan) == REF
2501 ? memNE(s, locinput, ln)
2503 ? ibcmp(s, locinput, ln)
2504 : ibcmp_locale(s, locinput, ln))))
2507 nextchr = UCHARAT(locinput);
2518 OP_4tree *oop = PL_op;
2519 COP *ocurcop = PL_curcop;
2520 SV **ocurpad = PL_curpad;
2524 PL_op = (OP_4tree*)PL_regdata->data[n];
2525 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2526 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2527 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2529 CALLRUNOPS(aTHX); /* Scalar context. */
2535 PL_curpad = ocurpad;
2536 PL_curcop = ocurcop;
2538 if (logical == 2) { /* Postponed subexpression. */
2540 MAGIC *mg = Null(MAGIC*);
2542 CHECKPOINT cp, lastcp;
2544 if(SvROK(ret) || SvRMAGICAL(ret)) {
2545 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2548 mg = mg_find(sv, 'r');
2551 re = (regexp *)mg->mg_obj;
2552 (void)ReREFCNT_inc(re);
2556 char *t = SvPV(ret, len);
2558 char *oprecomp = PL_regprecomp;
2559 I32 osize = PL_regsize;
2560 I32 onpar = PL_regnpar;
2563 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2565 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2566 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2567 PL_regprecomp = oprecomp;
2572 PerlIO_printf(Perl_debug_log,
2573 "Entering embedded `%s%.60s%s%s'\n",
2577 (strlen(re->precomp) > 60 ? "..." : ""))
2580 state.prev = PL_reg_call_cc;
2581 state.cc = PL_regcc;
2582 state.re = PL_reg_re;
2586 cp = regcppush(0); /* Save *all* the positions. */
2589 state.ss = PL_savestack_ix;
2590 *PL_reglastparen = 0;
2591 PL_reg_call_cc = &state;
2592 PL_reginput = locinput;
2594 /* XXXX This is too dramatic a measure... */
2597 if (regmatch(re->program + 1)) {
2598 /* Even though we succeeded, we need to restore
2599 global variables, since we may be wrapped inside
2600 SUSPEND, thus the match may be not finished yet. */
2602 /* XXXX Do this only if SUSPENDed? */
2603 PL_reg_call_cc = state.prev;
2604 PL_regcc = state.cc;
2605 PL_reg_re = state.re;
2606 cache_re(PL_reg_re);
2608 /* XXXX This is too dramatic a measure... */
2611 /* These are needed even if not SUSPEND. */
2619 PL_reg_call_cc = state.prev;
2620 PL_regcc = state.cc;
2621 PL_reg_re = state.re;
2622 cache_re(PL_reg_re);
2624 /* XXXX This is too dramatic a measure... */
2633 sv_setsv(save_scalar(PL_replgv), ret);
2637 n = ARG(scan); /* which paren pair */
2638 PL_reg_start_tmp[n] = locinput;
2643 n = ARG(scan); /* which paren pair */
2644 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2645 PL_regendp[n] = locinput - PL_bostr;
2646 if (n > *PL_reglastparen)
2647 *PL_reglastparen = n;
2650 n = ARG(scan); /* which paren pair */
2651 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2654 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2656 next = NEXTOPER(NEXTOPER(scan));
2658 next = scan + ARG(scan);
2659 if (OP(next) == IFTHEN) /* Fake one. */
2660 next = NEXTOPER(NEXTOPER(next));
2664 logical = scan->flags;
2666 /*******************************************************************
2667 PL_regcc contains infoblock about the innermost (...)* loop, and
2668 a pointer to the next outer infoblock.
2670 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2672 1) After matching X, regnode for CURLYX is processed;
2674 2) This regnode creates infoblock on the stack, and calls
2675 regmatch() recursively with the starting point at WHILEM node;
2677 3) Each hit of WHILEM node tries to match A and Z (in the order
2678 depending on the current iteration, min/max of {min,max} and
2679 greediness). The information about where are nodes for "A"
2680 and "Z" is read from the infoblock, as is info on how many times "A"
2681 was already matched, and greediness.
2683 4) After A matches, the same WHILEM node is hit again.
2685 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2686 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2687 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2688 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2689 of the external loop.
2691 Currently present infoblocks form a tree with a stem formed by PL_curcc
2692 and whatever it mentions via ->next, and additional attached trees
2693 corresponding to temporarily unset infoblocks as in "5" above.
2695 In the following picture infoblocks for outer loop of
2696 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2697 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2698 infoblocks are drawn below the "reset" infoblock.
2700 In fact in the picture below we do not show failed matches for Z and T
2701 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2702 more obvious *why* one needs to *temporary* unset infoblocks.]
2704 Matched REx position InfoBlocks Comment
2708 Y A)*?Z)*?T x <- O <- I
2709 YA )*?Z)*?T x <- O <- I
2710 YA A)*?Z)*?T x <- O <- I
2711 YAA )*?Z)*?T x <- O <- I
2712 YAA Z)*?T x <- O # Temporary unset I
2715 YAAZ Y(A)*?Z)*?T x <- O
2718 YAAZY (A)*?Z)*?T x <- O
2721 YAAZY A)*?Z)*?T x <- O <- I
2724 YAAZYA )*?Z)*?T x <- O <- I
2727 YAAZYA Z)*?T x <- O # Temporary unset I
2733 YAAZYAZ T x # Temporary unset O
2740 *******************************************************************/
2743 CHECKPOINT cp = PL_savestack_ix;
2745 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2747 cc.oldcc = PL_regcc;
2749 cc.parenfloor = *PL_reglastparen;
2751 cc.min = ARG1(scan);
2752 cc.max = ARG2(scan);
2753 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2757 PL_reginput = locinput;
2758 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2760 PL_regcc = cc.oldcc;
2766 * This is really hard to understand, because after we match
2767 * what we're trying to match, we must make sure the rest of
2768 * the REx is going to match for sure, and to do that we have
2769 * to go back UP the parse tree by recursing ever deeper. And
2770 * if it fails, we have to reset our parent's current state
2771 * that we can try again after backing off.
2774 CHECKPOINT cp, lastcp;
2775 CURCUR* cc = PL_regcc;
2776 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2778 n = cc->cur + 1; /* how many we know we matched */
2779 PL_reginput = locinput;
2782 PerlIO_printf(Perl_debug_log,
2783 "%*s %ld out of %ld..%ld cc=%lx\n",
2784 REPORT_CODE_OFF+PL_regindent*2, "",
2785 (long)n, (long)cc->min,
2786 (long)cc->max, (long)cc)
2789 /* If degenerate scan matches "", assume scan done. */
2791 if (locinput == cc->lastloc && n >= cc->min) {
2792 PL_regcc = cc->oldcc;
2796 PerlIO_printf(Perl_debug_log,
2797 "%*s empty match detected, try continuation...\n",
2798 REPORT_CODE_OFF+PL_regindent*2, "")
2800 if (regmatch(cc->next))
2808 /* First just match a string of min scans. */
2812 cc->lastloc = locinput;
2813 if (regmatch(cc->scan))
2816 cc->lastloc = lastloc;
2821 /* Check whether we already were at this position.
2822 Postpone detection until we know the match is not
2823 *that* much linear. */
2824 if (!PL_reg_maxiter) {
2825 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2826 PL_reg_leftiter = PL_reg_maxiter;
2828 if (PL_reg_leftiter-- == 0) {
2829 I32 size = (PL_reg_maxiter + 7)/8;
2830 if (PL_reg_poscache) {
2831 if (PL_reg_poscache_size < size) {
2832 Renew(PL_reg_poscache, size, char);
2833 PL_reg_poscache_size = size;
2835 Zero(PL_reg_poscache, size, char);
2838 PL_reg_poscache_size = size;
2839 Newz(29, PL_reg_poscache, size, char);
2842 PerlIO_printf(Perl_debug_log,
2843 "%sDetected a super-linear match, switching on caching%s...\n",
2844 PL_colors[4], PL_colors[5])
2847 if (PL_reg_leftiter < 0) {
2848 I32 o = locinput - PL_bostr, b;
2850 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2853 if (PL_reg_poscache[o] & (1<<b)) {
2855 PerlIO_printf(Perl_debug_log,
2856 "%*s already tried at this position...\n",
2857 REPORT_CODE_OFF+PL_regindent*2, "")
2861 PL_reg_poscache[o] |= (1<<b);
2865 /* Prefer next over scan for minimal matching. */
2868 PL_regcc = cc->oldcc;
2871 cp = regcppush(cc->parenfloor);
2873 if (regmatch(cc->next)) {
2875 sayYES; /* All done. */
2883 if (n >= cc->max) { /* Maximum greed exceeded? */
2884 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2885 && !(PL_reg_flags & RF_warned)) {
2886 PL_reg_flags |= RF_warned;
2887 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2888 "Complex regular subexpression recursion",
2895 PerlIO_printf(Perl_debug_log,
2896 "%*s trying longer...\n",
2897 REPORT_CODE_OFF+PL_regindent*2, "")
2899 /* Try scanning more and see if it helps. */
2900 PL_reginput = locinput;
2902 cc->lastloc = locinput;
2903 cp = regcppush(cc->parenfloor);
2905 if (regmatch(cc->scan)) {
2912 cc->lastloc = lastloc;
2916 /* Prefer scan over next for maximal matching. */
2918 if (n < cc->max) { /* More greed allowed? */
2919 cp = regcppush(cc->parenfloor);
2921 cc->lastloc = locinput;
2923 if (regmatch(cc->scan)) {
2928 regcppop(); /* Restore some previous $<digit>s? */
2929 PL_reginput = locinput;
2931 PerlIO_printf(Perl_debug_log,
2932 "%*s failed, try continuation...\n",
2933 REPORT_CODE_OFF+PL_regindent*2, "")
2936 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2937 && !(PL_reg_flags & RF_warned)) {
2938 PL_reg_flags |= RF_warned;
2939 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2940 "Complex regular subexpression recursion",
2944 /* Failed deeper matches of scan, so see if this one works. */
2945 PL_regcc = cc->oldcc;
2948 if (regmatch(cc->next))
2954 cc->lastloc = lastloc;
2959 next = scan + ARG(scan);
2962 inner = NEXTOPER(NEXTOPER(scan));
2965 inner = NEXTOPER(scan);
2970 if (OP(next) != c1) /* No choice. */
2971 next = inner; /* Avoid recursion. */
2973 int lastparen = *PL_reglastparen;
2977 PL_reginput = locinput;
2978 if (regmatch(inner))
2981 for (n = *PL_reglastparen; n > lastparen; n--)
2983 *PL_reglastparen = n;
2986 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2990 inner = NEXTOPER(scan);
2991 if (c1 == BRANCHJ) {
2992 inner = NEXTOPER(inner);
2994 } while (scan != NULL && OP(scan) == c1);
3008 /* We suppose that the next guy does not need
3009 backtracking: in particular, it is of constant length,
3010 and has no parenths to influence future backrefs. */
3011 ln = ARG1(scan); /* min to match */
3012 n = ARG2(scan); /* max to match */
3013 paren = scan->flags;
3015 if (paren > PL_regsize)
3017 if (paren > *PL_reglastparen)
3018 *PL_reglastparen = paren;
3020 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3022 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3023 PL_reginput = locinput;
3026 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3028 if (ln && l == 0 && n >= ln
3029 /* In fact, this is tricky. If paren, then the
3030 fact that we did/didnot match may influence
3031 future execution. */
3032 && !(paren && ln == 0))
3034 locinput = PL_reginput;
3035 if (PL_regkind[(U8)OP(next)] == EXACT) {
3036 c1 = (U8)*STRING(next);
3037 if (OP(next) == EXACTF)
3039 else if (OP(next) == EXACTFL)
3040 c2 = PL_fold_locale[c1];
3047 /* This may be improved if l == 0. */
3048 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3049 /* If it could work, try it. */
3051 UCHARAT(PL_reginput) == c1 ||
3052 UCHARAT(PL_reginput) == c2)
3056 PL_regstartp[paren] =
3057 HOPc(PL_reginput, -l) - PL_bostr;
3058 PL_regendp[paren] = PL_reginput - PL_bostr;
3061 PL_regendp[paren] = -1;
3067 /* Couldn't or didn't -- move forward. */
3068 PL_reginput = locinput;
3069 if (regrepeat_hard(scan, 1, &l)) {
3071 locinput = PL_reginput;
3078 n = regrepeat_hard(scan, n, &l);
3079 if (n != 0 && l == 0
3080 /* In fact, this is tricky. If paren, then the
3081 fact that we did/didnot match may influence
3082 future execution. */
3083 && !(paren && ln == 0))
3085 locinput = PL_reginput;
3087 PerlIO_printf(Perl_debug_log,
3088 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3089 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3093 if (PL_regkind[(U8)OP(next)] == EXACT) {
3094 c1 = (U8)*STRING(next);
3095 if (OP(next) == EXACTF)
3097 else if (OP(next) == EXACTFL)
3098 c2 = PL_fold_locale[c1];
3107 /* If it could work, try it. */
3109 UCHARAT(PL_reginput) == c1 ||
3110 UCHARAT(PL_reginput) == c2)
3113 PerlIO_printf(Perl_debug_log,
3114 "%*s trying tail with n=%"IVdf"...\n",
3115 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3119 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3120 PL_regendp[paren] = PL_reginput - PL_bostr;
3123 PL_regendp[paren] = -1;
3129 /* Couldn't or didn't -- back up. */
3131 locinput = HOPc(locinput, -l);
3132 PL_reginput = locinput;
3139 paren = scan->flags; /* Which paren to set */
3140 if (paren > PL_regsize)
3142 if (paren > *PL_reglastparen)
3143 *PL_reglastparen = paren;
3144 ln = ARG1(scan); /* min to match */
3145 n = ARG2(scan); /* max to match */
3146 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3150 ln = ARG1(scan); /* min to match */
3151 n = ARG2(scan); /* max to match */
3152 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3157 scan = NEXTOPER(scan);
3163 scan = NEXTOPER(scan);
3167 * Lookahead to avoid useless match attempts
3168 * when we know what character comes next.
3170 if (PL_regkind[(U8)OP(next)] == EXACT) {
3171 c1 = (U8)*STRING(next);
3172 if (OP(next) == EXACTF)
3174 else if (OP(next) == EXACTFL)
3175 c2 = PL_fold_locale[c1];
3181 PL_reginput = locinput;
3185 if (ln && regrepeat(scan, ln) < ln)
3187 locinput = PL_reginput;
3190 char *e = locinput + n - ln; /* Should not check after this */
3191 char *old = locinput;
3193 if (e >= PL_regeol || (n == REG_INFTY))
3196 /* Find place 'next' could work */
3198 while (locinput <= e && *locinput != c1)
3201 while (locinput <= e
3208 /* PL_reginput == old now */
3209 if (locinput != old) {
3210 ln = 1; /* Did some */
3211 if (regrepeat(scan, locinput - old) <
3215 /* PL_reginput == locinput now */
3218 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
3219 PL_regendp[paren] = locinput - PL_bostr;
3222 PL_regendp[paren] = -1;
3226 PL_reginput = locinput; /* Could be reset... */
3228 /* Couldn't or didn't -- move forward. */
3233 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3234 /* If it could work, try it. */
3236 UCHARAT(PL_reginput) == c1 ||
3237 UCHARAT(PL_reginput) == c2)
3241 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3242 PL_regendp[paren] = PL_reginput - PL_bostr;
3245 PL_regendp[paren] = -1;
3251 /* Couldn't or didn't -- move forward. */
3252 PL_reginput = locinput;
3253 if (regrepeat(scan, 1)) {
3255 locinput = PL_reginput;
3263 n = regrepeat(scan, n);
3264 locinput = PL_reginput;
3265 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3266 (!PL_multiline || OP(next) == SEOL))
3267 ln = n; /* why back off? */
3271 /* If it could work, try it. */
3273 UCHARAT(PL_reginput) == c1 ||
3274 UCHARAT(PL_reginput) == c2)
3278 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3279 PL_regendp[paren] = PL_reginput - PL_bostr;
3282 PL_regendp[paren] = -1;
3288 /* Couldn't or didn't -- back up. */
3290 PL_reginput = locinput = HOPc(locinput, -1);
3295 /* If it could work, try it. */
3297 UCHARAT(PL_reginput) == c1 ||
3298 UCHARAT(PL_reginput) == c2)
3304 /* Couldn't or didn't -- back up. */
3306 PL_reginput = locinput = HOPc(locinput, -1);
3313 if (PL_reg_call_cc) {
3314 re_cc_state *cur_call_cc = PL_reg_call_cc;
3315 CURCUR *cctmp = PL_regcc;
3316 regexp *re = PL_reg_re;
3317 CHECKPOINT cp, lastcp;
3319 cp = regcppush(0); /* Save *all* the positions. */
3321 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3323 PL_reginput = locinput; /* Make position available to
3325 cache_re(PL_reg_call_cc->re);
3326 PL_regcc = PL_reg_call_cc->cc;
3327 PL_reg_call_cc = PL_reg_call_cc->prev;
3328 if (regmatch(cur_call_cc->node)) {
3329 PL_reg_call_cc = cur_call_cc;
3335 PL_reg_call_cc = cur_call_cc;
3341 PerlIO_printf(Perl_debug_log,
3342 "%*s continuation failed...\n",
3343 REPORT_CODE_OFF+PL_regindent*2, "")
3347 if (locinput < PL_regtill) {
3348 DEBUG_r(PerlIO_printf(Perl_debug_log,
3349 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3351 (long)(locinput - PL_reg_starttry),
3352 (long)(PL_regtill - PL_reg_starttry),
3354 sayNO_FINAL; /* Cannot match: too short. */
3356 PL_reginput = locinput; /* put where regtry can find it */
3357 sayYES_FINAL; /* Success! */
3359 PL_reginput = locinput; /* put where regtry can find it */
3360 sayYES_LOUD; /* Success! */
3363 PL_reginput = locinput;
3368 if (UTF) { /* XXXX This is absolutely
3369 broken, we read before
3371 s = HOPMAYBEc(locinput, -scan->flags);
3377 if (locinput < PL_bostr + scan->flags)
3379 PL_reginput = locinput - scan->flags;
3384 PL_reginput = locinput;
3389 if (UTF) { /* XXXX This is absolutely
3390 broken, we read before
3392 s = HOPMAYBEc(locinput, -scan->flags);
3393 if (!s || s < PL_bostr)
3398 if (locinput < PL_bostr + scan->flags)
3400 PL_reginput = locinput - scan->flags;
3405 PL_reginput = locinput;
3408 inner = NEXTOPER(NEXTOPER(scan));
3409 if (regmatch(inner) != n) {
3424 if (OP(scan) == SUSPEND) {
3425 locinput = PL_reginput;
3426 nextchr = UCHARAT(locinput);
3431 next = scan + ARG(scan);
3436 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3437 PTR2UV(scan), OP(scan));
3438 Perl_croak(aTHX_ "regexp memory corruption");
3444 * We get here only if there's trouble -- normally "case END" is
3445 * the terminating point.
3447 Perl_croak(aTHX_ "corrupted regexp pointers");
3453 PerlIO_printf(Perl_debug_log,
3454 "%*s %scould match...%s\n",
3455 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3459 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3460 PL_colors[4],PL_colors[5]));
3469 PerlIO_printf(Perl_debug_log,
3470 "%*s %sfailed...%s\n",
3471 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3483 - regrepeat - repeatedly match something simple, report how many
3486 * [This routine now assumes that it will only match on things of length 1.
3487 * That was true before, but now we assume scan - reginput is the count,
3488 * rather than incrementing count on every character. [Er, except utf8.]]
3491 S_regrepeat(pTHX_ regnode *p, I32 max)
3494 register char *scan;
3496 register char *loceol = PL_regeol;
3497 register I32 hardcount = 0;
3500 if (max != REG_INFTY && max < loceol - scan)
3501 loceol = scan + max;
3504 while (scan < loceol && *scan != '\n')
3512 while (scan < loceol && *scan != '\n') {
3513 scan += UTF8SKIP(scan);
3519 while (scan < loceol) {
3520 scan += UTF8SKIP(scan);
3524 case EXACT: /* length of string is 1 */
3526 while (scan < loceol && UCHARAT(scan) == c)
3529 case EXACTF: /* length of string is 1 */
3531 while (scan < loceol &&
3532 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3535 case EXACTFL: /* length of string is 1 */
3536 PL_reg_flags |= RF_tainted;
3538 while (scan < loceol &&
3539 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3544 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3545 scan += UTF8SKIP(scan);
3550 while (scan < loceol && REGINCLASS(p, *scan))
3554 while (scan < loceol && isALNUM(*scan))
3559 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3560 scan += UTF8SKIP(scan);
3565 PL_reg_flags |= RF_tainted;
3566 while (scan < loceol && isALNUM_LC(*scan))
3570 PL_reg_flags |= RF_tainted;
3572 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3573 scan += UTF8SKIP(scan);
3579 while (scan < loceol && !isALNUM(*scan))
3584 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3585 scan += UTF8SKIP(scan);
3590 PL_reg_flags |= RF_tainted;
3591 while (scan < loceol && !isALNUM_LC(*scan))
3595 PL_reg_flags |= RF_tainted;
3597 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3598 scan += UTF8SKIP(scan);
3603 while (scan < loceol && isSPACE(*scan))
3608 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3609 scan += UTF8SKIP(scan);
3614 PL_reg_flags |= RF_tainted;
3615 while (scan < loceol && isSPACE_LC(*scan))
3619 PL_reg_flags |= RF_tainted;
3621 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3622 scan += UTF8SKIP(scan);
3627 while (scan < loceol && !isSPACE(*scan))
3632 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3633 scan += UTF8SKIP(scan);
3638 PL_reg_flags |= RF_tainted;
3639 while (scan < loceol && !isSPACE_LC(*scan))
3643 PL_reg_flags |= RF_tainted;
3645 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3646 scan += UTF8SKIP(scan);
3651 while (scan < loceol && isDIGIT(*scan))
3656 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3657 scan += UTF8SKIP(scan);
3663 while (scan < loceol && !isDIGIT(*scan))
3668 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3669 scan += UTF8SKIP(scan);
3673 default: /* Called on something of 0 width. */
3674 break; /* So match right here or not at all. */
3680 c = scan - PL_reginput;
3685 SV *prop = sv_newmortal();
3688 PerlIO_printf(Perl_debug_log,
3689 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3690 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3697 - regrepeat_hard - repeatedly match something, report total lenth and length
3699 * The repeater is supposed to have constant length.
3703 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3706 register char *scan;
3707 register char *start;
3708 register char *loceol = PL_regeol;
3710 I32 count = 0, res = 1;
3715 start = PL_reginput;
3717 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3720 while (start < PL_reginput) {
3722 start += UTF8SKIP(start);
3733 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3735 *lp = l = PL_reginput - start;
3736 if (max != REG_INFTY && l*max < loceol - scan)
3737 loceol = scan + l*max;
3750 - reginclass - determine if a character falls into a character class
3754 S_reginclass(pTHX_ register regnode *p, register I32 c)
3757 char flags = ANYOF_FLAGS(p);
3761 if (ANYOF_BITMAP_TEST(p, c))
3763 else if (flags & ANYOF_FOLD) {
3765 if (flags & ANYOF_LOCALE) {
3766 PL_reg_flags |= RF_tainted;
3767 cf = PL_fold_locale[c];
3771 if (ANYOF_BITMAP_TEST(p, cf))
3775 if (!match && (flags & ANYOF_CLASS)) {
3776 PL_reg_flags |= RF_tainted;
3778 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3779 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3780 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3781 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3782 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3783 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3784 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3785 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3786 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3787 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3788 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3789 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3790 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3791 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3792 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3793 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3794 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3795 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3796 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3797 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3798 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3799 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3800 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3801 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3802 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3803 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3804 ) /* How's that for a conditional? */
3810 return (flags & ANYOF_INVERT) ? !match : match;
3814 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3817 char flags = ARG1(f);
3819 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3821 if (swash_fetch(sv, p))
3823 else if (flags & ANYOF_FOLD) {
3826 if (flags & ANYOF_LOCALE) {
3827 PL_reg_flags |= RF_tainted;
3828 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3831 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3832 if (swash_fetch(sv, tmpbuf))
3836 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3838 return (flags & ANYOF_INVERT) ? !match : match;
3842 S_reghop(pTHX_ U8 *s, I32 off)
3846 while (off-- && s < (U8*)PL_regeol)
3851 if (s > (U8*)PL_bostr) {
3854 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3856 } /* XXX could check well-formedness here */
3864 S_reghopmaybe(pTHX_ U8* s, I32 off)
3868 while (off-- && s < (U8*)PL_regeol)
3875 if (s > (U8*)PL_bostr) {
3878 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3880 } /* XXX could check well-formedness here */
3896 restore_pos(pTHXo_ void *arg)
3899 if (PL_reg_eval_set) {
3900 if (PL_reg_oldsaved) {
3901 PL_reg_re->subbeg = PL_reg_oldsaved;
3902 PL_reg_re->sublen = PL_reg_oldsavedlen;
3903 RX_MATCH_COPIED_on(PL_reg_re);
3905 PL_reg_magic->mg_len = PL_reg_oldpos;
3906 PL_reg_eval_set = 0;
3907 PL_curpm = PL_reg_oldcurpm;