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(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
149 " Setting an EVAL scope, savestack=%"IVdf"\n", \
150 (IV)PL_savestack_ix)); cp = PL_savestack_ix
152 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
153 PerlIO_printf(Perl_debug_log, \
154 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
155 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
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);
194 /* It would seem that the similar code in regtry()
195 * already takes care of this, and in fact it is in
196 * a better location to since this code can #if 0-ed out
197 * but the code in regtry() is needed or otherwise tests
198 * requiring null fields (pat.t#187 and split.t#{13,14}
199 * (as of patchlevel 7877) will fail. Then again,
200 * this code seems to be necessary or otherwise
201 * building DynaLoader will fail:
202 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
204 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
205 if (paren > PL_regsize)
206 PL_regstartp[paren] = -1;
207 PL_regendp[paren] = -1;
214 S_regcp_set_to(pTHX_ I32 ss)
217 I32 tmp = PL_savestack_ix;
219 PL_savestack_ix = ss;
221 PL_savestack_ix = tmp;
225 typedef struct re_cc_state
229 struct re_cc_state *prev;
234 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
236 #define TRYPAREN(paren, n, input) { \
239 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
240 PL_regendp[paren] = input - PL_bostr; \
243 PL_regendp[paren] = -1; \
245 if (regmatch(next)) \
248 PL_regendp[paren] = -1; \
253 * pregexec and friends
257 - pregexec - match a regexp against a string
260 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
261 char *strbeg, I32 minend, SV *screamer, U32 nosave)
262 /* strend: pointer to null at end of string */
263 /* strbeg: real beginning of string */
264 /* minend: end of match must be >=minend after stringarg. */
265 /* nosave: For optimizations. */
268 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
269 nosave ? 0 : REXEC_COPY_STR);
273 S_cache_re(pTHX_ regexp *prog)
276 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
278 PL_regprogram = prog->program;
280 PL_regnpar = prog->nparens;
281 PL_regdata = prog->data;
286 * Need to implement the following flags for reg_anch:
288 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
290 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
291 * INTUIT_AUTORITATIVE_ML
292 * INTUIT_ONCE_NOML - Intuit can match in one location only.
295 * Another flag for this function: SECOND_TIME (so that float substrs
296 * with giant delta may be not rechecked).
299 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
301 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
302 Otherwise, only SvCUR(sv) is used to get strbeg. */
304 /* XXXX We assume that strpos is strbeg unless sv. */
306 /* XXXX Some places assume that there is a fixed substring.
307 An update may be needed if optimizer marks as "INTUITable"
308 RExen without fixed substrings. Similarly, it is assumed that
309 lengths of all the strings are no more than minlen, thus they
310 cannot come from lookahead.
311 (Or minlen should take into account lookahead.) */
313 /* A failure to find a constant substring means that there is no need to make
314 an expensive call to REx engine, thus we celebrate a failure. Similarly,
315 finding a substring too deep into the string means that less calls to
316 regtry() should be needed.
318 REx compiler's optimizer found 4 possible hints:
319 a) Anchored substring;
321 c) Whether we are anchored (beginning-of-line or \G);
322 d) First node (of those at offset 0) which may distingush positions;
323 We use a)b)d) and multiline-part of c), and try to find a position in the
324 string which does not contradict any of them.
327 /* Most of decisions we do here should have been done at compile time.
328 The nodes of the REx which we used for the search should have been
329 deleted from the finite automaton. */
332 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
333 char *strend, U32 flags, re_scream_pos_data *data)
335 register I32 start_shift;
336 /* Should be nonnegative! */
337 register I32 end_shift;
344 register char *other_last = Nullch; /* other substr checked before this */
345 char *check_at; /* check substr found at this pos */
347 char *i_strpos = strpos;
350 DEBUG_r( if (!PL_colorset) reginitcolors() );
351 DEBUG_r(PerlIO_printf(Perl_debug_log,
352 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
353 PL_colors[4],PL_colors[5],PL_colors[0],
356 (strlen(prog->precomp) > 60 ? "..." : ""),
358 (int)(strend - strpos > 60 ? 60 : strend - strpos),
359 strpos, PL_colors[1],
360 (strend - strpos > 60 ? "..." : ""))
363 if (prog->minlen > strend - strpos) {
364 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
367 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
368 check = prog->check_substr;
369 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
370 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
371 || ( (prog->reganch & ROPT_ANCH_BOL)
372 && !PL_multiline ) ); /* Check after \n? */
375 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
376 /* SvCUR is not set on references: SvRV and SvPVX overlap */
378 && (strpos != strbeg)) {
379 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
382 if (prog->check_offset_min == prog->check_offset_max) {
383 /* Substring at constant offset from beg-of-str... */
386 PL_regeol = strend; /* Used in HOP() */
387 s = HOPc(strpos, prog->check_offset_min);
389 slen = SvCUR(check); /* >= 1 */
391 if ( strend - s > slen || strend - s < slen - 1
392 || (strend - s == slen && strend[-1] != '\n')) {
393 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
396 /* Now should match s[0..slen-2] */
398 if (slen && (*SvPVX(check) != *s
400 && memNE(SvPVX(check), s, slen)))) {
402 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
406 else if (*SvPVX(check) != *s
407 || ((slen = SvCUR(check)) > 1
408 && memNE(SvPVX(check), s, slen)))
410 goto success_at_start;
413 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
415 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
416 end_shift = prog->minlen - start_shift -
417 CHR_SVLEN(check) + (SvTAIL(check) != 0);
419 I32 end = prog->check_offset_max + CHR_SVLEN(check)
420 - (SvTAIL(check) != 0);
421 I32 eshift = strend - s - end;
423 if (end_shift < eshift)
427 else { /* Can match at random position */
430 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
431 /* Should be nonnegative! */
432 end_shift = prog->minlen - start_shift -
433 CHR_SVLEN(check) + (SvTAIL(check) != 0);
436 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
438 Perl_croak(aTHX_ "panic: end_shift");
442 /* Find a possible match in the region s..strend by looking for
443 the "check" substring in the region corrected by start/end_shift. */
444 if (flags & REXEC_SCREAM) {
445 I32 p = -1; /* Internal iterator of scream. */
446 I32 *pp = data ? data->scream_pos : &p;
448 if (PL_screamfirst[BmRARE(check)] >= 0
449 || ( BmRARE(check) == '\n'
450 && (BmPREVIOUS(check) == SvCUR(check) - 1)
452 s = screaminstr(sv, check,
453 start_shift + (s - strbeg), end_shift, pp, 0);
457 *data->scream_olds = s;
460 s = fbm_instr((unsigned char*)s + start_shift,
461 (unsigned char*)strend - end_shift,
462 check, PL_multiline ? FBMrf_MULTILINE : 0);
464 /* Update the count-of-usability, remove useless subpatterns,
467 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
468 (s ? "Found" : "Did not find"),
469 ((check == prog->anchored_substr) ? "anchored" : "floating"),
471 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
473 PL_colors[1], (SvTAIL(check) ? "$" : ""),
474 (s ? " at offset " : "...\n") ) );
481 /* Finish the diagnostic message */
482 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
484 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
485 Start with the other substr.
486 XXXX no SCREAM optimization yet - and a very coarse implementation
487 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
488 *always* match. Probably should be marked during compile...
489 Probably it is right to do no SCREAM here...
492 if (prog->float_substr && prog->anchored_substr) {
493 /* Take into account the "other" substring. */
494 /* XXXX May be hopelessly wrong for UTF... */
497 if (check == prog->float_substr) {
500 char *last = s - start_shift, *last1, *last2;
504 t = s - prog->check_offset_max;
505 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
506 && (!(prog->reganch & ROPT_UTF8)
507 || (PL_bostr = strpos, /* Used in regcopmaybe() */
508 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
513 t += prog->anchored_offset;
514 if (t < other_last) /* These positions already checked */
517 last2 = last1 = strend - prog->minlen;
520 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
521 /* On end-of-str: see comment below. */
522 s = fbm_instr((unsigned char*)t,
523 (unsigned char*)last1 + prog->anchored_offset
524 + SvCUR(prog->anchored_substr)
525 - (SvTAIL(prog->anchored_substr)!=0),
526 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
527 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
528 (s ? "Found" : "Contradicts"),
530 (int)(SvCUR(prog->anchored_substr)
531 - (SvTAIL(prog->anchored_substr)!=0)),
532 SvPVX(prog->anchored_substr),
533 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
535 if (last1 >= last2) {
536 DEBUG_r(PerlIO_printf(Perl_debug_log,
537 ", giving up...\n"));
540 DEBUG_r(PerlIO_printf(Perl_debug_log,
541 ", trying floating at offset %ld...\n",
542 (long)(s1 + 1 - i_strpos)));
543 PL_regeol = strend; /* Used in HOP() */
544 other_last = last1 + prog->anchored_offset + 1;
549 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
550 (long)(s - i_strpos)));
551 t = s - prog->anchored_offset;
560 else { /* Take into account the floating substring. */
565 last1 = last = strend - prog->minlen + prog->float_min_offset;
566 if (last - t > prog->float_max_offset)
567 last = t + prog->float_max_offset;
568 s = t + prog->float_min_offset;
571 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
572 /* fbm_instr() takes into account exact value of end-of-str
573 if the check is SvTAIL(ed). Since false positives are OK,
574 and end-of-str is not later than strend we are OK. */
575 s = fbm_instr((unsigned char*)s,
576 (unsigned char*)last + SvCUR(prog->float_substr)
577 - (SvTAIL(prog->float_substr)!=0),
578 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
579 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
580 (s ? "Found" : "Contradicts"),
582 (int)(SvCUR(prog->float_substr)
583 - (SvTAIL(prog->float_substr)!=0)),
584 SvPVX(prog->float_substr),
585 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
588 DEBUG_r(PerlIO_printf(Perl_debug_log,
589 ", giving up...\n"));
592 DEBUG_r(PerlIO_printf(Perl_debug_log,
593 ", trying anchored starting at offset %ld...\n",
594 (long)(s1 + 1 - i_strpos)));
595 other_last = last + 1;
596 PL_regeol = strend; /* Used in HOP() */
601 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
602 (long)(s - i_strpos)));
612 t = s - prog->check_offset_max;
614 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
615 && (!(prog->reganch & ROPT_UTF8)
616 || (PL_bostr = strpos, /* Used in regcopmaybe() */
617 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
620 /* Fixed substring is found far enough so that the match
621 cannot start at strpos. */
623 if (ml_anch && t[-1] != '\n') {
624 /* Eventually fbm_*() should handle this, but often
625 anchored_offset is not 0, so this check will not be wasted. */
626 /* XXXX In the code below we prefer to look for "^" even in
627 presence of anchored substrings. And we search even
628 beyond the found float position. These pessimizations
629 are historical artefacts only. */
631 while (t < strend - prog->minlen) {
633 if (t < check_at - prog->check_offset_min) {
634 if (prog->anchored_substr) {
635 /* Since we moved from the found position,
636 we definitely contradict the found anchored
637 substr. Due to the above check we do not
638 contradict "check" substr.
639 Thus we can arrive here only if check substr
640 is float. Redo checking for "other"=="fixed".
643 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
644 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
645 goto do_other_anchored;
647 /* We don't contradict the found floating substring. */
648 /* XXXX Why not check for STCLASS? */
650 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
651 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
654 /* Position contradicts check-string */
655 /* XXXX probably better to look for check-string
656 than for "\n", so one should lower the limit for t? */
657 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
658 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
659 other_last = strpos = s = t + 1;
664 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
665 PL_colors[0],PL_colors[1]));
669 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
670 PL_colors[0],PL_colors[1]));
674 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
678 /* The found string does not prohibit matching at strpos,
679 - no optimization of calling REx engine can be performed,
680 unless it was an MBOL and we are not after MBOL,
681 or a future STCLASS check will fail this. */
683 /* Even in this situation we may use MBOL flag if strpos is offset
684 wrt the start of the string. */
685 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
686 && (strpos != strbeg) && strpos[-1] != '\n'
687 /* May be due to an implicit anchor of m{.*foo} */
688 && !(prog->reganch & ROPT_IMPLICIT))
693 DEBUG_r( if (ml_anch)
694 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
695 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
698 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
699 && prog->check_substr /* Could be deleted already */
700 && --BmUSEFUL(prog->check_substr) < 0
701 && prog->check_substr == prog->float_substr)
703 /* If flags & SOMETHING - do not do it many times on the same match */
704 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
705 SvREFCNT_dec(prog->check_substr);
706 prog->check_substr = Nullsv; /* disable */
707 prog->float_substr = Nullsv; /* clear */
708 check = Nullsv; /* abort */
710 /* XXXX This is a remnant of the old implementation. It
711 looks wasteful, since now INTUIT can use many
713 prog->reganch &= ~RE_USE_INTUIT;
720 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
721 if (prog->regstclass) {
722 /* minlen == 0 is possible if regstclass is \b or \B,
723 and the fixed substr is ''$.
724 Since minlen is already taken into account, s+1 is before strend;
725 accidentally, minlen >= 1 guaranties no false positives at s + 1
726 even for \b or \B. But (minlen? 1 : 0) below assumes that
727 regstclass does not come from lookahead... */
728 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
729 This leaves EXACTF only, which is dealt with in find_byclass(). */
730 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
731 ? STR_LEN(prog->regstclass)
733 char *endpos = (prog->anchored_substr || ml_anch)
734 ? s + (prog->minlen? cl_l : 0)
735 : (prog->float_substr ? check_at - start_shift + cl_l
737 char *startpos = strbeg;
740 if (prog->reganch & ROPT_UTF8) {
741 PL_regdata = prog->data; /* Used by REGINCLASS UTF logic */
744 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
749 if (endpos == strend) {
750 DEBUG_r( PerlIO_printf(Perl_debug_log,
751 "Could not match STCLASS...\n") );
754 DEBUG_r( PerlIO_printf(Perl_debug_log,
755 "This position contradicts STCLASS...\n") );
756 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
758 /* Contradict one of substrings */
759 if (prog->anchored_substr) {
760 if (prog->anchored_substr == check) {
761 DEBUG_r( what = "anchored" );
763 PL_regeol = strend; /* Used in HOP() */
765 if (s + start_shift + end_shift > strend) {
766 /* XXXX Should be taken into account earlier? */
767 DEBUG_r( PerlIO_printf(Perl_debug_log,
768 "Could not match STCLASS...\n") );
773 DEBUG_r( PerlIO_printf(Perl_debug_log,
774 "Looking for %s substr starting at offset %ld...\n",
775 what, (long)(s + start_shift - i_strpos)) );
778 /* Have both, check_string is floating */
779 if (t + start_shift >= check_at) /* Contradicts floating=check */
780 goto retry_floating_check;
781 /* Recheck anchored substring, but not floating... */
785 DEBUG_r( PerlIO_printf(Perl_debug_log,
786 "Looking for anchored substr starting at offset %ld...\n",
787 (long)(other_last - i_strpos)) );
788 goto do_other_anchored;
790 /* Another way we could have checked stclass at the
791 current position only: */
796 DEBUG_r( PerlIO_printf(Perl_debug_log,
797 "Looking for /%s^%s/m starting at offset %ld...\n",
798 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
801 if (!prog->float_substr) /* Could have been deleted */
803 /* Check is floating subtring. */
804 retry_floating_check:
805 t = check_at - start_shift;
806 DEBUG_r( what = "floating" );
807 goto hop_and_restart;
810 PerlIO_printf(Perl_debug_log,
811 "By STCLASS: moving %ld --> %ld\n",
812 (long)(t - i_strpos), (long)(s - i_strpos));
814 PerlIO_printf(Perl_debug_log,
815 "Does not contradict STCLASS...\n") );
818 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
819 PL_colors[4], (check ? "Guessed" : "Giving up"),
820 PL_colors[5], (long)(s - i_strpos)) );
823 fail_finish: /* Substring not found */
824 if (prog->check_substr) /* could be removed already */
825 BmUSEFUL(prog->check_substr) += 5; /* hooray */
827 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
828 PL_colors[4],PL_colors[5]));
832 /* We know what class REx starts with. Try to find this position... */
834 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
836 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
842 register I32 tmp = 1; /* Scratch variable? */
844 /* We know what class it must start with. */
848 if (REGINCLASSUTF8(c, (U8*)s)) {
849 if (tmp && (norun || regtry(prog, s)))
861 if (REGINCLASS(c, *(U8*)s)) {
862 if (tmp && (norun || regtry(prog, s)))
882 c2 = PL_fold_locale[c1];
887 e = s; /* Due to minlen logic of intuit() */
888 /* Here it is NOT UTF! */
892 && (ln == 1 || !(OP(c) == EXACTF
894 : ibcmp_locale(s, m, ln)))
895 && (norun || regtry(prog, s)) )
901 if ( (*(U8*)s == c1 || *(U8*)s == c2)
902 && (ln == 1 || !(OP(c) == EXACTF
904 : ibcmp_locale(s, m, ln)))
905 && (norun || regtry(prog, s)) )
912 PL_reg_flags |= RF_tainted;
915 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
916 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
918 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
920 if ((norun || regtry(prog, s)))
925 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
929 PL_reg_flags |= RF_tainted;
935 U8 *r = reghop((U8*)s, -1);
937 tmp = (I32)utf8_to_uv(r, s - (char*)r, 0, 0);
939 tmp = ((OP(c) == BOUNDUTF8 ?
940 isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
942 if (tmp == !(OP(c) == BOUNDUTF8 ?
943 swash_fetch(PL_utf8_alnum, (U8*)s) :
944 isALNUM_LC_utf8((U8*)s)))
947 if ((norun || regtry(prog, s)))
952 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
956 PL_reg_flags |= RF_tainted;
959 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
960 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
962 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
964 else if ((norun || regtry(prog, s)))
968 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
972 PL_reg_flags |= RF_tainted;
978 U8 *r = reghop((U8*)s, -1);
980 tmp = (I32)utf8_to_uv(r, s - (char*)r, 0, 0);
982 tmp = ((OP(c) == NBOUNDUTF8 ?
983 isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
985 if (tmp == !(OP(c) == NBOUNDUTF8 ?
986 swash_fetch(PL_utf8_alnum, (U8*)s) :
987 isALNUM_LC_utf8((U8*)s)))
989 else if ((norun || regtry(prog, s)))
993 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
999 if (tmp && (norun || regtry(prog, s)))
1010 while (s < strend) {
1011 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1012 if (tmp && (norun || regtry(prog, s)))
1023 PL_reg_flags |= RF_tainted;
1024 while (s < strend) {
1025 if (isALNUM_LC(*s)) {
1026 if (tmp && (norun || regtry(prog, s)))
1037 PL_reg_flags |= RF_tainted;
1038 while (s < strend) {
1039 if (isALNUM_LC_utf8((U8*)s)) {
1040 if (tmp && (norun || regtry(prog, s)))
1051 while (s < strend) {
1053 if (tmp && (norun || regtry(prog, s)))
1064 while (s < strend) {
1065 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1066 if (tmp && (norun || regtry(prog, s)))
1077 PL_reg_flags |= RF_tainted;
1078 while (s < strend) {
1079 if (!isALNUM_LC(*s)) {
1080 if (tmp && (norun || regtry(prog, s)))
1091 PL_reg_flags |= RF_tainted;
1092 while (s < strend) {
1093 if (!isALNUM_LC_utf8((U8*)s)) {
1094 if (tmp && (norun || regtry(prog, s)))
1105 while (s < strend) {
1107 if (tmp && (norun || regtry(prog, s)))
1118 while (s < strend) {
1119 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1120 if (tmp && (norun || regtry(prog, s)))
1131 PL_reg_flags |= RF_tainted;
1132 while (s < strend) {
1133 if (isSPACE_LC(*s)) {
1134 if (tmp && (norun || regtry(prog, s)))
1145 PL_reg_flags |= RF_tainted;
1146 while (s < strend) {
1147 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1148 if (tmp && (norun || regtry(prog, s)))
1159 while (s < strend) {
1161 if (tmp && (norun || regtry(prog, s)))
1172 while (s < strend) {
1173 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1174 if (tmp && (norun || regtry(prog, s)))
1185 PL_reg_flags |= RF_tainted;
1186 while (s < strend) {
1187 if (!isSPACE_LC(*s)) {
1188 if (tmp && (norun || regtry(prog, s)))
1199 PL_reg_flags |= RF_tainted;
1200 while (s < strend) {
1201 if (!(*s == ' ' || isSPACE_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_digit,(U8*)s)) {
1228 if (tmp && (norun || regtry(prog, s)))
1239 PL_reg_flags |= RF_tainted;
1240 while (s < strend) {
1241 if (isDIGIT_LC(*s)) {
1242 if (tmp && (norun || regtry(prog, s)))
1253 PL_reg_flags |= RF_tainted;
1254 while (s < strend) {
1255 if (isDIGIT_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 (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1282 if (tmp && (norun || regtry(prog, s)))
1293 PL_reg_flags |= RF_tainted;
1294 while (s < strend) {
1295 if (!isDIGIT_LC(*s)) {
1296 if (tmp && (norun || regtry(prog, s)))
1307 PL_reg_flags |= RF_tainted;
1308 while (s < strend) {
1309 if (!isDIGIT_LC_utf8((U8*)s)) {
1310 if (tmp && (norun || regtry(prog, s)))
1321 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1330 - regexec_flags - match a regexp against a string
1333 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1334 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1335 /* strend: pointer to null at end of string */
1336 /* strbeg: real beginning of string */
1337 /* minend: end of match must be >=minend after stringarg. */
1338 /* data: May be used for some additional optimizations. */
1339 /* nosave: For optimizations. */
1343 register regnode *c;
1344 register char *startpos = stringarg;
1345 I32 minlen; /* must match at least this many chars */
1346 I32 dontbother = 0; /* how many characters not to try at end */
1347 /* I32 start_shift = 0; */ /* Offset of the start to find
1348 constant substr. */ /* CC */
1349 I32 end_shift = 0; /* Same for the end. */ /* CC */
1350 I32 scream_pos = -1; /* Internal iterator of scream. */
1352 SV* oreplsv = GvSV(PL_replgv);
1358 PL_regnarrate = PL_debug & 512;
1361 /* Be paranoid... */
1362 if (prog == NULL || startpos == NULL) {
1363 Perl_croak(aTHX_ "NULL regexp parameter");
1367 minlen = prog->minlen;
1368 if (strend - startpos < minlen) goto phooey;
1370 if (startpos == strbeg) /* is ^ valid at stringarg? */
1373 PL_regprev = (U32)stringarg[-1];
1374 if (!PL_multiline && PL_regprev == '\n')
1375 PL_regprev = '\0'; /* force ^ to NOT match */
1378 /* Check validity of program. */
1379 if (UCHARAT(prog->program) != REG_MAGIC) {
1380 Perl_croak(aTHX_ "corrupted regexp program");
1384 PL_reg_eval_set = 0;
1387 if (prog->reganch & ROPT_UTF8)
1388 PL_reg_flags |= RF_utf8;
1390 /* Mark beginning of line for ^ and lookbehind. */
1391 PL_regbol = startpos;
1395 /* Mark end of line for $ (and such) */
1398 /* see how far we have to get to not match where we matched before */
1399 PL_regtill = startpos+minend;
1401 /* We start without call_cc context. */
1404 /* If there is a "must appear" string, look for it. */
1407 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1410 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1411 PL_reg_ganch = startpos;
1412 else if (sv && SvTYPE(sv) >= SVt_PVMG
1414 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1415 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1416 if (prog->reganch & ROPT_ANCH_GPOS) {
1417 if (s > PL_reg_ganch)
1422 else /* pos() not defined */
1423 PL_reg_ganch = strbeg;
1426 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1427 re_scream_pos_data d;
1429 d.scream_olds = &scream_olds;
1430 d.scream_pos = &scream_pos;
1431 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1433 goto phooey; /* not present */
1436 DEBUG_r( if (!PL_colorset) reginitcolors() );
1437 DEBUG_r(PerlIO_printf(Perl_debug_log,
1438 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1439 PL_colors[4],PL_colors[5],PL_colors[0],
1442 (strlen(prog->precomp) > 60 ? "..." : ""),
1444 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1445 startpos, PL_colors[1],
1446 (strend - startpos > 60 ? "..." : ""))
1449 /* Simplest case: anchored match need be tried only once. */
1450 /* [unless only anchor is BOL and multiline is set] */
1451 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1452 if (s == startpos && regtry(prog, startpos))
1454 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1455 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1460 dontbother = minlen - 1;
1461 end = HOPc(strend, -dontbother) - 1;
1462 /* for multiline we only have to try after newlines */
1463 if (prog->check_substr) {
1467 if (regtry(prog, s))
1472 if (prog->reganch & RE_USE_INTUIT) {
1473 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1484 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1485 if (regtry(prog, s))
1492 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1493 if (regtry(prog, PL_reg_ganch))
1498 /* Messy cases: unanchored match. */
1499 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1500 /* we have /x+whatever/ */
1501 /* it must be a one character string (XXXX Except UTF?) */
1502 char ch = SvPVX(prog->anchored_substr)[0];
1508 while (s < strend) {
1510 DEBUG_r( did_match = 1 );
1511 if (regtry(prog, s)) goto got_it;
1513 while (s < strend && *s == ch)
1520 while (s < strend) {
1522 DEBUG_r( did_match = 1 );
1523 if (regtry(prog, s)) goto got_it;
1525 while (s < strend && *s == ch)
1531 DEBUG_r(did_match ||
1532 PerlIO_printf(Perl_debug_log,
1533 "Did not find anchored character...\n"));
1536 else if (prog->anchored_substr != Nullsv
1537 || (prog->float_substr != Nullsv
1538 && prog->float_max_offset < strend - s)) {
1539 SV *must = prog->anchored_substr
1540 ? prog->anchored_substr : prog->float_substr;
1542 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1544 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1545 char *last = HOPc(strend, /* Cannot start after this */
1546 -(I32)(CHR_SVLEN(must)
1547 - (SvTAIL(must) != 0) + back_min));
1548 char *last1; /* Last position checked before */
1554 last1 = HOPc(s, -1);
1556 last1 = s - 1; /* bogus */
1558 /* XXXX check_substr already used to find `s', can optimize if
1559 check_substr==must. */
1561 dontbother = end_shift;
1562 strend = HOPc(strend, -dontbother);
1563 while ( (s <= last) &&
1564 ((flags & REXEC_SCREAM)
1565 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1566 end_shift, &scream_pos, 0))
1567 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1568 (unsigned char*)strend, must,
1569 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1570 DEBUG_r( did_match = 1 );
1571 if (HOPc(s, -back_max) > last1) {
1572 last1 = HOPc(s, -back_min);
1573 s = HOPc(s, -back_max);
1576 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1578 last1 = HOPc(s, -back_min);
1582 while (s <= last1) {
1583 if (regtry(prog, s))
1589 while (s <= last1) {
1590 if (regtry(prog, s))
1596 DEBUG_r(did_match ||
1597 PerlIO_printf(Perl_debug_log, "Did not find %s substr `%s%.*s%s'%s...\n",
1598 ((must == prog->anchored_substr)
1599 ? "anchored" : "floating"),
1601 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1603 PL_colors[1], (SvTAIL(must) ? "$" : "")));
1606 else if ((c = prog->regstclass)) {
1607 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1608 /* don't bother with what can't match */
1609 strend = HOPc(strend, -(minlen - 1));
1610 if (find_byclass(prog, c, s, strend, startpos, 0))
1612 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1616 if (prog->float_substr != Nullsv) { /* Trim the end. */
1619 if (flags & REXEC_SCREAM) {
1620 last = screaminstr(sv, prog->float_substr, s - strbeg,
1621 end_shift, &scream_pos, 1); /* last one */
1623 last = scream_olds; /* Only one occurence. */
1627 char *little = SvPV(prog->float_substr, len);
1629 if (SvTAIL(prog->float_substr)) {
1630 if (memEQ(strend - len + 1, little, len - 1))
1631 last = strend - len + 1;
1632 else if (!PL_multiline)
1633 last = memEQ(strend - len, little, len)
1634 ? strend - len : Nullch;
1640 last = rninstr(s, strend, little, little + len);
1642 last = strend; /* matching `$' */
1646 DEBUG_r(PerlIO_printf(Perl_debug_log,
1647 "%sCan't trim the tail, match fails (should not happen)%s\n",
1648 PL_colors[4],PL_colors[5]));
1649 goto phooey; /* Should not happen! */
1651 dontbother = strend - last + prog->float_min_offset;
1653 if (minlen && (dontbother < minlen))
1654 dontbother = minlen - 1;
1655 strend -= dontbother; /* this one's always in bytes! */
1656 /* We don't know much -- general case. */
1659 if (regtry(prog, s))
1668 if (regtry(prog, s))
1670 } while (s++ < strend);
1678 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1680 if (PL_reg_eval_set) {
1681 /* Preserve the current value of $^R */
1682 if (oreplsv != GvSV(PL_replgv))
1683 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1684 restored, the value remains
1686 restore_pos(aTHXo_ 0);
1689 /* make sure $`, $&, $', and $digit will work later */
1690 if ( !(flags & REXEC_NOT_FIRST) ) {
1691 if (RX_MATCH_COPIED(prog)) {
1692 Safefree(prog->subbeg);
1693 RX_MATCH_COPIED_off(prog);
1695 if (flags & REXEC_COPY_STR) {
1696 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1698 s = savepvn(strbeg, i);
1701 RX_MATCH_COPIED_on(prog);
1704 prog->subbeg = strbeg;
1705 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1712 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1713 PL_colors[4],PL_colors[5]));
1714 if (PL_reg_eval_set)
1715 restore_pos(aTHXo_ 0);
1720 - regtry - try match at specific point
1722 STATIC I32 /* 0 failure, 1 success */
1723 S_regtry(pTHX_ regexp *prog, char *startpos)
1732 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1734 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1737 PL_reg_eval_set = RS_init;
1739 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1740 (IV)(PL_stack_sp - PL_stack_base));
1742 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1743 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1744 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1746 /* Apparently this is not needed, judging by wantarray. */
1747 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1748 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1751 /* Make $_ available to executed code. */
1752 if (PL_reg_sv != DEFSV) {
1753 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1758 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1759 && (mg = mg_find(PL_reg_sv, 'g')))) {
1760 /* prepare for quick setting of pos */
1761 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1762 mg = mg_find(PL_reg_sv, 'g');
1766 PL_reg_oldpos = mg->mg_len;
1767 SAVEDESTRUCTOR_X(restore_pos, 0);
1770 Newz(22,PL_reg_curpm, 1, PMOP);
1771 PL_reg_curpm->op_pmregexp = prog;
1772 PL_reg_oldcurpm = PL_curpm;
1773 PL_curpm = PL_reg_curpm;
1774 if (RX_MATCH_COPIED(prog)) {
1775 /* Here is a serious problem: we cannot rewrite subbeg,
1776 since it may be needed if this match fails. Thus
1777 $` inside (?{}) could fail... */
1778 PL_reg_oldsaved = prog->subbeg;
1779 PL_reg_oldsavedlen = prog->sublen;
1780 RX_MATCH_COPIED_off(prog);
1783 PL_reg_oldsaved = Nullch;
1784 prog->subbeg = PL_bostr;
1785 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1787 prog->startp[0] = startpos - PL_bostr;
1788 PL_reginput = startpos;
1789 PL_regstartp = prog->startp;
1790 PL_regendp = prog->endp;
1791 PL_reglastparen = &prog->lastparen;
1792 prog->lastparen = 0;
1794 DEBUG_r(PL_reg_starttry = startpos);
1795 if (PL_reg_start_tmpl <= prog->nparens) {
1796 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1797 if(PL_reg_start_tmp)
1798 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1800 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1803 /* XXXX What this code is doing here?!!! There should be no need
1804 to do this again and again, PL_reglastparen should take care of
1807 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1808 * Actually, the code in regcppop() (which Ilya may be meaning by
1809 * PL_reglastparen), is not needed at all by the test suite
1810 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1811 * enough, for building DynaLoader, or otherwise this
1812 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1813 * will happen. Meanwhile, this code *is* needed for the
1814 * above-mentioned test suite tests to succeed. The common theme
1815 * on those tests seems to be returning null fields from matches.
1820 if (prog->nparens) {
1821 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1828 if (regmatch(prog->program + 1)) {
1829 prog->endp[0] = PL_reginput - PL_bostr;
1832 REGCP_UNWIND(lastcp);
1836 #define RE_UNWIND_BRANCH 1
1837 #define RE_UNWIND_BRANCHJ 2
1841 typedef struct { /* XX: makes sense to enlarge it... */
1845 } re_unwind_generic_t;
1858 } re_unwind_branch_t;
1860 typedef union re_unwind_t {
1862 re_unwind_generic_t generic;
1863 re_unwind_branch_t branch;
1867 - regmatch - main matching routine
1869 * Conceptually the strategy is simple: check to see whether the current
1870 * node matches, call self recursively to see whether the rest matches,
1871 * and then act accordingly. In practice we make some effort to avoid
1872 * recursion, in particular by going through "ordinary" nodes (that don't
1873 * need to know whether the rest of the match failed) by a loop instead of
1876 /* [lwall] I've hoisted the register declarations to the outer block in order to
1877 * maybe save a little bit of pushing and popping on the stack. It also takes
1878 * advantage of machines that use a register save mask on subroutine entry.
1880 STATIC I32 /* 0 failure, 1 success */
1881 S_regmatch(pTHX_ regnode *prog)
1884 register regnode *scan; /* Current node. */
1885 regnode *next; /* Next node. */
1886 regnode *inner; /* Next node in internal branch. */
1887 register I32 nextchr; /* renamed nextchr - nextchar colides with
1888 function of same name */
1889 register I32 n; /* no or next */
1890 register I32 ln; /* len or last */
1891 register char *s; /* operand or save */
1892 register char *locinput = PL_reginput;
1893 register I32 c1, c2, paren; /* case fold search, parenth */
1894 int minmod = 0, sw = 0, logical = 0;
1896 I32 firstcp = PL_savestack_ix;
1902 /* Note that nextchr is a byte even in UTF */
1903 nextchr = UCHARAT(locinput);
1905 while (scan != NULL) {
1906 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1908 # define sayYES goto yes
1909 # define sayNO goto no
1910 # define sayYES_FINAL goto yes_final
1911 # define sayYES_LOUD goto yes_loud
1912 # define sayNO_FINAL goto no_final
1913 # define sayNO_SILENT goto do_no
1914 # define saySAME(x) if (x) goto yes; else goto no
1915 # define REPORT_CODE_OFF 24
1917 # define sayYES return 1
1918 # define sayNO return 0
1919 # define sayYES_FINAL return 1
1920 # define sayYES_LOUD return 1
1921 # define sayNO_FINAL return 0
1922 # define sayNO_SILENT return 0
1923 # define saySAME(x) return x
1926 SV *prop = sv_newmortal();
1927 int docolor = *PL_colors[0];
1928 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1929 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1930 /* The part of the string before starttry has one color
1931 (pref0_len chars), between starttry and current
1932 position another one (pref_len - pref0_len chars),
1933 after the current position the third one.
1934 We assume that pref0_len <= pref_len, otherwise we
1935 decrease pref0_len. */
1936 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1937 ? (5 + taill) - l : locinput - PL_bostr);
1938 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1940 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1941 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1942 ? (5 + taill) - pref_len : PL_regeol - locinput);
1945 if (pref0_len > pref_len)
1946 pref0_len = pref_len;
1947 regprop(prop, scan);
1948 PerlIO_printf(Perl_debug_log,
1949 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1950 (IV)(locinput - PL_bostr),
1951 PL_colors[4], pref0_len,
1952 locinput - pref_len, PL_colors[5],
1953 PL_colors[2], pref_len - pref0_len,
1954 locinput - pref_len + pref0_len, PL_colors[3],
1955 (docolor ? "" : "> <"),
1956 PL_colors[0], l, locinput, PL_colors[1],
1957 15 - l - pref_len + 1,
1959 (IV)(scan - PL_regprogram), PL_regindent*2, "",
1963 next = scan + NEXT_OFF(scan);
1969 if (locinput == PL_bostr
1970 ? PL_regprev == '\n'
1972 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1974 /* regtill = regbol; */
1979 if (locinput == PL_bostr
1980 ? PL_regprev == '\n'
1981 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1987 if (locinput == PL_bostr)
1991 if (locinput == PL_reg_ganch)
2001 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2006 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2008 if (PL_regeol - locinput > 1)
2012 if (PL_regeol != locinput)
2016 if (nextchr & 0x80) {
2017 locinput += PL_utf8skip[nextchr];
2018 if (locinput > PL_regeol)
2020 nextchr = UCHARAT(locinput);
2023 if (!nextchr && locinput >= PL_regeol)
2025 nextchr = UCHARAT(++locinput);
2028 if (!nextchr && locinput >= PL_regeol)
2030 nextchr = UCHARAT(++locinput);
2033 if (nextchr & 0x80) {
2034 locinput += PL_utf8skip[nextchr];
2035 if (locinput > PL_regeol)
2037 nextchr = UCHARAT(locinput);
2040 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2042 nextchr = UCHARAT(++locinput);
2045 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2047 nextchr = UCHARAT(++locinput);
2052 /* Inline the first character, for speed. */
2053 if (UCHARAT(s) != nextchr)
2055 if (PL_regeol - locinput < ln)
2057 if (ln > 1 && memNE(s, locinput, ln))
2060 nextchr = UCHARAT(locinput);
2063 PL_reg_flags |= RF_tainted;
2072 c1 = OP(scan) == EXACTF;
2076 if (utf8_to_uv((U8*)s, e - s, 0, 0) !=
2078 toLOWER_utf8((U8*)l) :
2079 toLOWER_LC_utf8((U8*)l)))
2087 nextchr = UCHARAT(locinput);
2091 /* Inline the first character, for speed. */
2092 if (UCHARAT(s) != nextchr &&
2093 UCHARAT(s) != ((OP(scan) == EXACTF)
2094 ? PL_fold : PL_fold_locale)[nextchr])
2096 if (PL_regeol - locinput < ln)
2098 if (ln > 1 && (OP(scan) == EXACTF
2099 ? ibcmp(s, locinput, ln)
2100 : ibcmp_locale(s, locinput, ln)))
2103 nextchr = UCHARAT(locinput);
2106 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2108 if (locinput >= PL_regeol)
2110 locinput += PL_utf8skip[nextchr];
2111 nextchr = UCHARAT(locinput);
2115 nextchr = UCHARAT(locinput);
2116 if (!REGINCLASS(scan, nextchr))
2118 if (!nextchr && locinput >= PL_regeol)
2120 nextchr = UCHARAT(++locinput);
2123 PL_reg_flags |= RF_tainted;
2128 if (!(OP(scan) == ALNUM
2129 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2131 nextchr = UCHARAT(++locinput);
2134 PL_reg_flags |= RF_tainted;
2139 if (nextchr & 0x80) {
2140 if (!(OP(scan) == ALNUMUTF8
2141 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2142 : isALNUM_LC_utf8((U8*)locinput)))
2146 locinput += PL_utf8skip[nextchr];
2147 nextchr = UCHARAT(locinput);
2150 if (!(OP(scan) == ALNUMUTF8
2151 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2153 nextchr = UCHARAT(++locinput);
2156 PL_reg_flags |= RF_tainted;
2159 if (!nextchr && locinput >= PL_regeol)
2161 if (OP(scan) == NALNUM
2162 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2164 nextchr = UCHARAT(++locinput);
2167 PL_reg_flags |= RF_tainted;
2170 if (!nextchr && locinput >= PL_regeol)
2172 if (nextchr & 0x80) {
2173 if (OP(scan) == NALNUMUTF8
2174 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2175 : isALNUM_LC_utf8((U8*)locinput))
2179 locinput += PL_utf8skip[nextchr];
2180 nextchr = UCHARAT(locinput);
2183 if (OP(scan) == NALNUMUTF8
2184 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2186 nextchr = UCHARAT(++locinput);
2190 PL_reg_flags |= RF_tainted;
2194 /* was last char in word? */
2195 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2196 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2198 n = isALNUM(nextchr);
2201 ln = isALNUM_LC(ln);
2202 n = isALNUM_LC(nextchr);
2204 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2209 PL_reg_flags |= RF_tainted;
2213 /* was last char in word? */
2214 if (locinput == PL_regbol)
2217 U8 *r = reghop((U8*)locinput, -1);
2219 ln = utf8_to_uv(r, s - (char*)r, 0, 0);
2221 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2222 ln = isALNUM_uni(ln);
2223 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2226 ln = isALNUM_LC_uni(ln);
2227 n = isALNUM_LC_utf8((U8*)locinput);
2229 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2233 PL_reg_flags |= RF_tainted;
2238 if (!(OP(scan) == SPACE
2239 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2241 nextchr = UCHARAT(++locinput);
2244 PL_reg_flags |= RF_tainted;
2249 if (nextchr & 0x80) {
2250 if (!(OP(scan) == SPACEUTF8
2251 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2252 : isSPACE_LC_utf8((U8*)locinput)))
2256 locinput += PL_utf8skip[nextchr];
2257 nextchr = UCHARAT(locinput);
2260 if (!(OP(scan) == SPACEUTF8
2261 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2263 nextchr = UCHARAT(++locinput);
2266 PL_reg_flags |= RF_tainted;
2269 if (!nextchr && locinput >= PL_regeol)
2271 if (OP(scan) == NSPACE
2272 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2274 nextchr = UCHARAT(++locinput);
2277 PL_reg_flags |= RF_tainted;
2280 if (!nextchr && locinput >= PL_regeol)
2282 if (nextchr & 0x80) {
2283 if (OP(scan) == NSPACEUTF8
2284 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2285 : isSPACE_LC_utf8((U8*)locinput))
2289 locinput += PL_utf8skip[nextchr];
2290 nextchr = UCHARAT(locinput);
2293 if (OP(scan) == NSPACEUTF8
2294 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2296 nextchr = UCHARAT(++locinput);
2299 PL_reg_flags |= RF_tainted;
2304 if (!(OP(scan) == DIGIT
2305 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2307 nextchr = UCHARAT(++locinput);
2310 PL_reg_flags |= RF_tainted;
2315 if (nextchr & 0x80) {
2316 if (!(OP(scan) == DIGITUTF8
2317 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2318 : isDIGIT_LC_utf8((U8*)locinput)))
2322 locinput += PL_utf8skip[nextchr];
2323 nextchr = UCHARAT(locinput);
2326 if (!(OP(scan) == DIGITUTF8
2327 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2329 nextchr = UCHARAT(++locinput);
2332 PL_reg_flags |= RF_tainted;
2335 if (!nextchr && locinput >= PL_regeol)
2337 if (OP(scan) == NDIGIT
2338 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2340 nextchr = UCHARAT(++locinput);
2343 PL_reg_flags |= RF_tainted;
2346 if (!nextchr && locinput >= PL_regeol)
2348 if (nextchr & 0x80) {
2349 if (OP(scan) == NDIGITUTF8
2350 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2351 : isDIGIT_LC_utf8((U8*)locinput))
2355 locinput += PL_utf8skip[nextchr];
2356 nextchr = UCHARAT(locinput);
2359 if (OP(scan) == NDIGITUTF8
2360 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2362 nextchr = UCHARAT(++locinput);
2365 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2367 locinput += PL_utf8skip[nextchr];
2368 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2369 locinput += UTF8SKIP(locinput);
2370 if (locinput > PL_regeol)
2372 nextchr = UCHARAT(locinput);
2375 PL_reg_flags |= RF_tainted;
2379 n = ARG(scan); /* which paren pair */
2380 ln = PL_regstartp[n];
2381 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2382 if (*PL_reglastparen < n || ln == -1)
2383 sayNO; /* Do not match unless seen CLOSEn. */
2384 if (ln == PL_regendp[n])
2388 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2390 char *e = PL_bostr + PL_regendp[n];
2392 * Note that we can't do the "other character" lookup trick as
2393 * in the 8-bit case (no pun intended) because in Unicode we
2394 * have to map both upper and title case to lower case.
2396 if (OP(scan) == REFF) {
2400 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2410 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2417 nextchr = UCHARAT(locinput);
2421 /* Inline the first character, for speed. */
2422 if (UCHARAT(s) != nextchr &&
2424 (UCHARAT(s) != ((OP(scan) == REFF
2425 ? PL_fold : PL_fold_locale)[nextchr]))))
2427 ln = PL_regendp[n] - ln;
2428 if (locinput + ln > PL_regeol)
2430 if (ln > 1 && (OP(scan) == REF
2431 ? memNE(s, locinput, ln)
2433 ? ibcmp(s, locinput, ln)
2434 : ibcmp_locale(s, locinput, ln))))
2437 nextchr = UCHARAT(locinput);
2448 OP_4tree *oop = PL_op;
2449 COP *ocurcop = PL_curcop;
2450 SV **ocurpad = PL_curpad;
2454 PL_op = (OP_4tree*)PL_regdata->data[n];
2455 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2456 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2457 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2459 CALLRUNOPS(aTHX); /* Scalar context. */
2465 PL_curpad = ocurpad;
2466 PL_curcop = ocurcop;
2468 if (logical == 2) { /* Postponed subexpression. */
2470 MAGIC *mg = Null(MAGIC*);
2472 CHECKPOINT cp, lastcp;
2474 if(SvROK(ret) || SvRMAGICAL(ret)) {
2475 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2478 mg = mg_find(sv, 'r');
2481 re = (regexp *)mg->mg_obj;
2482 (void)ReREFCNT_inc(re);
2486 char *t = SvPV(ret, len);
2488 char *oprecomp = PL_regprecomp;
2489 I32 osize = PL_regsize;
2490 I32 onpar = PL_regnpar;
2493 pm.op_pmdynflags = (UTF||DO_UTF8(ret) ? PMdf_UTF8 : 0);
2494 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2496 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2497 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2498 PL_regprecomp = oprecomp;
2503 PerlIO_printf(Perl_debug_log,
2504 "Entering embedded `%s%.60s%s%s'\n",
2508 (strlen(re->precomp) > 60 ? "..." : ""))
2511 state.prev = PL_reg_call_cc;
2512 state.cc = PL_regcc;
2513 state.re = PL_reg_re;
2517 cp = regcppush(0); /* Save *all* the positions. */
2520 state.ss = PL_savestack_ix;
2521 *PL_reglastparen = 0;
2522 PL_reg_call_cc = &state;
2523 PL_reginput = locinput;
2525 /* XXXX This is too dramatic a measure... */
2528 if (regmatch(re->program + 1)) {
2529 /* Even though we succeeded, we need to restore
2530 global variables, since we may be wrapped inside
2531 SUSPEND, thus the match may be not finished yet. */
2533 /* XXXX Do this only if SUSPENDed? */
2534 PL_reg_call_cc = state.prev;
2535 PL_regcc = state.cc;
2536 PL_reg_re = state.re;
2537 cache_re(PL_reg_re);
2539 /* XXXX This is too dramatic a measure... */
2542 /* These are needed even if not SUSPEND. */
2548 REGCP_UNWIND(lastcp);
2550 PL_reg_call_cc = state.prev;
2551 PL_regcc = state.cc;
2552 PL_reg_re = state.re;
2553 cache_re(PL_reg_re);
2555 /* XXXX This is too dramatic a measure... */
2564 sv_setsv(save_scalar(PL_replgv), ret);
2568 n = ARG(scan); /* which paren pair */
2569 PL_reg_start_tmp[n] = locinput;
2574 n = ARG(scan); /* which paren pair */
2575 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2576 PL_regendp[n] = locinput - PL_bostr;
2577 if (n > *PL_reglastparen)
2578 *PL_reglastparen = n;
2581 n = ARG(scan); /* which paren pair */
2582 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2585 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2587 next = NEXTOPER(NEXTOPER(scan));
2589 next = scan + ARG(scan);
2590 if (OP(next) == IFTHEN) /* Fake one. */
2591 next = NEXTOPER(NEXTOPER(next));
2595 logical = scan->flags;
2597 /*******************************************************************
2598 PL_regcc contains infoblock about the innermost (...)* loop, and
2599 a pointer to the next outer infoblock.
2601 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2603 1) After matching X, regnode for CURLYX is processed;
2605 2) This regnode creates infoblock on the stack, and calls
2606 regmatch() recursively with the starting point at WHILEM node;
2608 3) Each hit of WHILEM node tries to match A and Z (in the order
2609 depending on the current iteration, min/max of {min,max} and
2610 greediness). The information about where are nodes for "A"
2611 and "Z" is read from the infoblock, as is info on how many times "A"
2612 was already matched, and greediness.
2614 4) After A matches, the same WHILEM node is hit again.
2616 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2617 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2618 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2619 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2620 of the external loop.
2622 Currently present infoblocks form a tree with a stem formed by PL_curcc
2623 and whatever it mentions via ->next, and additional attached trees
2624 corresponding to temporarily unset infoblocks as in "5" above.
2626 In the following picture infoblocks for outer loop of
2627 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2628 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2629 infoblocks are drawn below the "reset" infoblock.
2631 In fact in the picture below we do not show failed matches for Z and T
2632 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2633 more obvious *why* one needs to *temporary* unset infoblocks.]
2635 Matched REx position InfoBlocks Comment
2639 Y A)*?Z)*?T x <- O <- I
2640 YA )*?Z)*?T x <- O <- I
2641 YA A)*?Z)*?T x <- O <- I
2642 YAA )*?Z)*?T x <- O <- I
2643 YAA Z)*?T x <- O # Temporary unset I
2646 YAAZ Y(A)*?Z)*?T x <- O
2649 YAAZY (A)*?Z)*?T x <- O
2652 YAAZY A)*?Z)*?T x <- O <- I
2655 YAAZYA )*?Z)*?T x <- O <- I
2658 YAAZYA Z)*?T x <- O # Temporary unset I
2664 YAAZYAZ T x # Temporary unset O
2671 *******************************************************************/
2674 CHECKPOINT cp = PL_savestack_ix;
2675 /* No need to save/restore up to this paren */
2676 I32 parenfloor = scan->flags;
2678 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2680 cc.oldcc = PL_regcc;
2682 /* XXXX Probably it is better to teach regpush to support
2683 parenfloor > PL_regsize... */
2684 if (parenfloor > *PL_reglastparen)
2685 parenfloor = *PL_reglastparen; /* Pessimization... */
2686 cc.parenfloor = parenfloor;
2688 cc.min = ARG1(scan);
2689 cc.max = ARG2(scan);
2690 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2694 PL_reginput = locinput;
2695 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2697 PL_regcc = cc.oldcc;
2703 * This is really hard to understand, because after we match
2704 * what we're trying to match, we must make sure the rest of
2705 * the REx is going to match for sure, and to do that we have
2706 * to go back UP the parse tree by recursing ever deeper. And
2707 * if it fails, we have to reset our parent's current state
2708 * that we can try again after backing off.
2711 CHECKPOINT cp, lastcp;
2712 CURCUR* cc = PL_regcc;
2713 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2715 n = cc->cur + 1; /* how many we know we matched */
2716 PL_reginput = locinput;
2719 PerlIO_printf(Perl_debug_log,
2720 "%*s %ld out of %ld..%ld cc=%lx\n",
2721 REPORT_CODE_OFF+PL_regindent*2, "",
2722 (long)n, (long)cc->min,
2723 (long)cc->max, (long)cc)
2726 /* If degenerate scan matches "", assume scan done. */
2728 if (locinput == cc->lastloc && n >= cc->min) {
2729 PL_regcc = cc->oldcc;
2733 PerlIO_printf(Perl_debug_log,
2734 "%*s empty match detected, try continuation...\n",
2735 REPORT_CODE_OFF+PL_regindent*2, "")
2737 if (regmatch(cc->next))
2745 /* First just match a string of min scans. */
2749 cc->lastloc = locinput;
2750 if (regmatch(cc->scan))
2753 cc->lastloc = lastloc;
2758 /* Check whether we already were at this position.
2759 Postpone detection until we know the match is not
2760 *that* much linear. */
2761 if (!PL_reg_maxiter) {
2762 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2763 PL_reg_leftiter = PL_reg_maxiter;
2765 if (PL_reg_leftiter-- == 0) {
2766 I32 size = (PL_reg_maxiter + 7)/8;
2767 if (PL_reg_poscache) {
2768 if (PL_reg_poscache_size < size) {
2769 Renew(PL_reg_poscache, size, char);
2770 PL_reg_poscache_size = size;
2772 Zero(PL_reg_poscache, size, char);
2775 PL_reg_poscache_size = size;
2776 Newz(29, PL_reg_poscache, size, char);
2779 PerlIO_printf(Perl_debug_log,
2780 "%sDetected a super-linear match, switching on caching%s...\n",
2781 PL_colors[4], PL_colors[5])
2784 if (PL_reg_leftiter < 0) {
2785 I32 o = locinput - PL_bostr, b;
2787 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2790 if (PL_reg_poscache[o] & (1<<b)) {
2792 PerlIO_printf(Perl_debug_log,
2793 "%*s already tried at this position...\n",
2794 REPORT_CODE_OFF+PL_regindent*2, "")
2798 PL_reg_poscache[o] |= (1<<b);
2802 /* Prefer next over scan for minimal matching. */
2805 PL_regcc = cc->oldcc;
2808 cp = regcppush(cc->parenfloor);
2810 if (regmatch(cc->next)) {
2812 sayYES; /* All done. */
2814 REGCP_UNWIND(lastcp);
2820 if (n >= cc->max) { /* Maximum greed exceeded? */
2821 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2822 && !(PL_reg_flags & RF_warned)) {
2823 PL_reg_flags |= RF_warned;
2824 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2825 "Complex regular subexpression recursion",
2832 PerlIO_printf(Perl_debug_log,
2833 "%*s trying longer...\n",
2834 REPORT_CODE_OFF+PL_regindent*2, "")
2836 /* Try scanning more and see if it helps. */
2837 PL_reginput = locinput;
2839 cc->lastloc = locinput;
2840 cp = regcppush(cc->parenfloor);
2842 if (regmatch(cc->scan)) {
2846 REGCP_UNWIND(lastcp);
2849 cc->lastloc = lastloc;
2853 /* Prefer scan over next for maximal matching. */
2855 if (n < cc->max) { /* More greed allowed? */
2856 cp = regcppush(cc->parenfloor);
2858 cc->lastloc = locinput;
2860 if (regmatch(cc->scan)) {
2864 REGCP_UNWIND(lastcp);
2865 regcppop(); /* Restore some previous $<digit>s? */
2866 PL_reginput = locinput;
2868 PerlIO_printf(Perl_debug_log,
2869 "%*s failed, try continuation...\n",
2870 REPORT_CODE_OFF+PL_regindent*2, "")
2873 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2874 && !(PL_reg_flags & RF_warned)) {
2875 PL_reg_flags |= RF_warned;
2876 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2877 "Complex regular subexpression recursion",
2881 /* Failed deeper matches of scan, so see if this one works. */
2882 PL_regcc = cc->oldcc;
2885 if (regmatch(cc->next))
2891 cc->lastloc = lastloc;
2896 next = scan + ARG(scan);
2899 inner = NEXTOPER(NEXTOPER(scan));
2902 inner = NEXTOPER(scan);
2907 if (OP(next) != c1) /* No choice. */
2908 next = inner; /* Avoid recursion. */
2910 I32 lastparen = *PL_reglastparen;
2912 re_unwind_branch_t *uw;
2914 /* Put unwinding data on stack */
2915 unwind1 = SSNEWt(1,re_unwind_branch_t);
2916 uw = SSPTRt(unwind1,re_unwind_branch_t);
2919 uw->type = ((c1 == BRANCH)
2921 : RE_UNWIND_BRANCHJ);
2922 uw->lastparen = lastparen;
2924 uw->locinput = locinput;
2925 uw->nextchr = nextchr;
2927 uw->regindent = ++PL_regindent;
2930 REGCP_SET(uw->lastcp);
2932 /* Now go into the first branch */
2945 /* We suppose that the next guy does not need
2946 backtracking: in particular, it is of constant length,
2947 and has no parenths to influence future backrefs. */
2948 ln = ARG1(scan); /* min to match */
2949 n = ARG2(scan); /* max to match */
2950 paren = scan->flags;
2952 if (paren > PL_regsize)
2954 if (paren > *PL_reglastparen)
2955 *PL_reglastparen = paren;
2957 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2959 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2960 PL_reginput = locinput;
2963 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2965 if (ln && l == 0 && n >= ln
2966 /* In fact, this is tricky. If paren, then the
2967 fact that we did/didnot match may influence
2968 future execution. */
2969 && !(paren && ln == 0))
2971 locinput = PL_reginput;
2972 if (PL_regkind[(U8)OP(next)] == EXACT) {
2973 c1 = (U8)*STRING(next);
2974 if (OP(next) == EXACTF)
2976 else if (OP(next) == EXACTFL)
2977 c2 = PL_fold_locale[c1];
2984 /* This may be improved if l == 0. */
2985 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2986 /* If it could work, try it. */
2988 UCHARAT(PL_reginput) == c1 ||
2989 UCHARAT(PL_reginput) == c2)
2993 PL_regstartp[paren] =
2994 HOPc(PL_reginput, -l) - PL_bostr;
2995 PL_regendp[paren] = PL_reginput - PL_bostr;
2998 PL_regendp[paren] = -1;
3002 REGCP_UNWIND(lastcp);
3004 /* Couldn't or didn't -- move forward. */
3005 PL_reginput = locinput;
3006 if (regrepeat_hard(scan, 1, &l)) {
3008 locinput = PL_reginput;
3015 n = regrepeat_hard(scan, n, &l);
3016 if (n != 0 && l == 0
3017 /* In fact, this is tricky. If paren, then the
3018 fact that we did/didnot match may influence
3019 future execution. */
3020 && !(paren && ln == 0))
3022 locinput = PL_reginput;
3024 PerlIO_printf(Perl_debug_log,
3025 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3026 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3030 if (PL_regkind[(U8)OP(next)] == EXACT) {
3031 c1 = (U8)*STRING(next);
3032 if (OP(next) == EXACTF)
3034 else if (OP(next) == EXACTFL)
3035 c2 = PL_fold_locale[c1];
3044 /* If it could work, try it. */
3046 UCHARAT(PL_reginput) == c1 ||
3047 UCHARAT(PL_reginput) == c2)
3050 PerlIO_printf(Perl_debug_log,
3051 "%*s trying tail with n=%"IVdf"...\n",
3052 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3056 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3057 PL_regendp[paren] = PL_reginput - PL_bostr;
3060 PL_regendp[paren] = -1;
3064 REGCP_UNWIND(lastcp);
3066 /* Couldn't or didn't -- back up. */
3068 locinput = HOPc(locinput, -l);
3069 PL_reginput = locinput;
3076 paren = scan->flags; /* Which paren to set */
3077 if (paren > PL_regsize)
3079 if (paren > *PL_reglastparen)
3080 *PL_reglastparen = paren;
3081 ln = ARG1(scan); /* min to match */
3082 n = ARG2(scan); /* max to match */
3083 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3087 ln = ARG1(scan); /* min to match */
3088 n = ARG2(scan); /* max to match */
3089 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3094 scan = NEXTOPER(scan);
3100 scan = NEXTOPER(scan);
3104 * Lookahead to avoid useless match attempts
3105 * when we know what character comes next.
3107 if (PL_regkind[(U8)OP(next)] == EXACT) {
3108 c1 = (U8)*STRING(next);
3109 if (OP(next) == EXACTF)
3111 else if (OP(next) == EXACTFL)
3112 c2 = PL_fold_locale[c1];
3118 PL_reginput = locinput;
3122 if (ln && regrepeat(scan, ln) < ln)
3124 locinput = PL_reginput;
3127 char *e = locinput + n - ln; /* Should not check after this */
3128 char *old = locinput;
3130 if (e >= PL_regeol || (n == REG_INFTY))
3133 /* Find place 'next' could work */
3135 while (locinput <= e && *locinput != c1)
3138 while (locinput <= e
3145 /* PL_reginput == old now */
3146 if (locinput != old) {
3147 ln = 1; /* Did some */
3148 if (regrepeat(scan, locinput - old) <
3152 /* PL_reginput == locinput now */
3153 TRYPAREN(paren, ln, locinput);
3154 PL_reginput = locinput; /* Could be reset... */
3155 REGCP_UNWIND(lastcp);
3156 /* Couldn't or didn't -- move forward. */
3161 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3162 /* If it could work, try it. */
3164 UCHARAT(PL_reginput) == c1 ||
3165 UCHARAT(PL_reginput) == c2)
3167 TRYPAREN(paren, n, PL_reginput);
3168 REGCP_UNWIND(lastcp);
3170 /* Couldn't or didn't -- move forward. */
3171 PL_reginput = locinput;
3172 if (regrepeat(scan, 1)) {
3174 locinput = PL_reginput;
3182 n = regrepeat(scan, n);
3183 locinput = PL_reginput;
3184 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3185 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3186 ln = n; /* why back off? */
3187 /* ...because $ and \Z can match before *and* after
3188 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3189 We should back off by one in this case. */
3190 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3196 /* If it could work, try it. */
3198 UCHARAT(PL_reginput) == c1 ||
3199 UCHARAT(PL_reginput) == c2)
3201 TRYPAREN(paren, n, PL_reginput);
3202 REGCP_UNWIND(lastcp);
3204 /* Couldn't or didn't -- back up. */
3206 PL_reginput = locinput = HOPc(locinput, -1);
3211 /* If it could work, try it. */
3213 UCHARAT(PL_reginput) == c1 ||
3214 UCHARAT(PL_reginput) == c2)
3216 TRYPAREN(paren, n, PL_reginput);
3217 REGCP_UNWIND(lastcp);
3219 /* Couldn't or didn't -- back up. */
3221 PL_reginput = locinput = HOPc(locinput, -1);
3228 if (PL_reg_call_cc) {
3229 re_cc_state *cur_call_cc = PL_reg_call_cc;
3230 CURCUR *cctmp = PL_regcc;
3231 regexp *re = PL_reg_re;
3232 CHECKPOINT cp, lastcp;
3234 cp = regcppush(0); /* Save *all* the positions. */
3236 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3238 PL_reginput = locinput; /* Make position available to
3240 cache_re(PL_reg_call_cc->re);
3241 PL_regcc = PL_reg_call_cc->cc;
3242 PL_reg_call_cc = PL_reg_call_cc->prev;
3243 if (regmatch(cur_call_cc->node)) {
3244 PL_reg_call_cc = cur_call_cc;
3248 REGCP_UNWIND(lastcp);
3250 PL_reg_call_cc = cur_call_cc;
3256 PerlIO_printf(Perl_debug_log,
3257 "%*s continuation failed...\n",
3258 REPORT_CODE_OFF+PL_regindent*2, "")
3262 if (locinput < PL_regtill) {
3263 DEBUG_r(PerlIO_printf(Perl_debug_log,
3264 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3266 (long)(locinput - PL_reg_starttry),
3267 (long)(PL_regtill - PL_reg_starttry),
3269 sayNO_FINAL; /* Cannot match: too short. */
3271 PL_reginput = locinput; /* put where regtry can find it */
3272 sayYES_FINAL; /* Success! */
3274 PL_reginput = locinput; /* put where regtry can find it */
3275 sayYES_LOUD; /* Success! */
3278 PL_reginput = locinput;
3283 if (UTF) { /* XXXX This is absolutely
3284 broken, we read before
3286 s = HOPMAYBEc(locinput, -scan->flags);
3292 if (locinput < PL_bostr + scan->flags)
3294 PL_reginput = locinput - scan->flags;
3299 PL_reginput = locinput;
3304 if (UTF) { /* XXXX This is absolutely
3305 broken, we read before
3307 s = HOPMAYBEc(locinput, -scan->flags);
3308 if (!s || s < PL_bostr)
3313 if (locinput < PL_bostr + scan->flags)
3315 PL_reginput = locinput - scan->flags;
3320 PL_reginput = locinput;
3323 inner = NEXTOPER(NEXTOPER(scan));
3324 if (regmatch(inner) != n) {
3339 if (OP(scan) == SUSPEND) {
3340 locinput = PL_reginput;
3341 nextchr = UCHARAT(locinput);
3346 next = scan + ARG(scan);
3351 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3352 PTR2UV(scan), OP(scan));
3353 Perl_croak(aTHX_ "regexp memory corruption");
3360 * We get here only if there's trouble -- normally "case END" is
3361 * the terminating point.
3363 Perl_croak(aTHX_ "corrupted regexp pointers");
3369 PerlIO_printf(Perl_debug_log,
3370 "%*s %scould match...%s\n",
3371 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3375 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3376 PL_colors[4],PL_colors[5]));
3382 #if 0 /* Breaks $^R */
3390 PerlIO_printf(Perl_debug_log,
3391 "%*s %sfailed...%s\n",
3392 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3398 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3401 case RE_UNWIND_BRANCH:
3402 case RE_UNWIND_BRANCHJ:
3404 re_unwind_branch_t *uwb = &(uw->branch);
3405 I32 lastparen = uwb->lastparen;
3407 REGCP_UNWIND(uwb->lastcp);
3408 for (n = *PL_reglastparen; n > lastparen; n--)
3410 *PL_reglastparen = n;
3411 scan = next = uwb->next;
3413 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3414 ? BRANCH : BRANCHJ) ) { /* Failure */
3421 /* Have more choice yet. Reuse the same uwb. */
3423 if ((n = (uwb->type == RE_UNWIND_BRANCH
3424 ? NEXT_OFF(next) : ARG(next))))
3427 next = NULL; /* XXXX Needn't unwinding in this case... */
3429 next = NEXTOPER(scan);
3430 if (uwb->type == RE_UNWIND_BRANCHJ)
3431 next = NEXTOPER(next);
3432 locinput = uwb->locinput;
3433 nextchr = uwb->nextchr;
3435 PL_regindent = uwb->regindent;
3442 Perl_croak(aTHX_ "regexp unwind memory corruption");
3453 - regrepeat - repeatedly match something simple, report how many
3456 * [This routine now assumes that it will only match on things of length 1.
3457 * That was true before, but now we assume scan - reginput is the count,
3458 * rather than incrementing count on every character. [Er, except utf8.]]
3461 S_regrepeat(pTHX_ regnode *p, I32 max)
3464 register char *scan;
3466 register char *loceol = PL_regeol;
3467 register I32 hardcount = 0;
3470 if (max != REG_INFTY && max < loceol - scan)
3471 loceol = scan + max;
3474 while (scan < loceol && *scan != '\n')
3482 while (scan < loceol && *scan != '\n') {
3483 scan += UTF8SKIP(scan);
3489 while (scan < loceol) {
3490 scan += UTF8SKIP(scan);
3494 case EXACT: /* length of string is 1 */
3496 while (scan < loceol && UCHARAT(scan) == c)
3499 case EXACTF: /* length of string is 1 */
3501 while (scan < loceol &&
3502 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3505 case EXACTFL: /* length of string is 1 */
3506 PL_reg_flags |= RF_tainted;
3508 while (scan < loceol &&
3509 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3514 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3515 scan += UTF8SKIP(scan);
3520 while (scan < loceol && REGINCLASS(p, *scan))
3524 while (scan < loceol && isALNUM(*scan))
3529 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3530 scan += UTF8SKIP(scan);
3535 PL_reg_flags |= RF_tainted;
3536 while (scan < loceol && isALNUM_LC(*scan))
3540 PL_reg_flags |= RF_tainted;
3542 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3543 scan += UTF8SKIP(scan);
3549 while (scan < loceol && !isALNUM(*scan))
3554 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3555 scan += UTF8SKIP(scan);
3560 PL_reg_flags |= RF_tainted;
3561 while (scan < loceol && !isALNUM_LC(*scan))
3565 PL_reg_flags |= RF_tainted;
3567 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3568 scan += UTF8SKIP(scan);
3573 while (scan < loceol && isSPACE(*scan))
3578 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3579 scan += UTF8SKIP(scan);
3584 PL_reg_flags |= RF_tainted;
3585 while (scan < loceol && isSPACE_LC(*scan))
3589 PL_reg_flags |= RF_tainted;
3591 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3592 scan += UTF8SKIP(scan);
3597 while (scan < loceol && !isSPACE(*scan))
3602 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3603 scan += UTF8SKIP(scan);
3608 PL_reg_flags |= RF_tainted;
3609 while (scan < loceol && !isSPACE_LC(*scan))
3613 PL_reg_flags |= RF_tainted;
3615 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3616 scan += UTF8SKIP(scan);
3621 while (scan < loceol && isDIGIT(*scan))
3626 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3627 scan += UTF8SKIP(scan);
3633 while (scan < loceol && !isDIGIT(*scan))
3638 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3639 scan += UTF8SKIP(scan);
3643 default: /* Called on something of 0 width. */
3644 break; /* So match right here or not at all. */
3650 c = scan - PL_reginput;
3655 SV *prop = sv_newmortal();
3658 PerlIO_printf(Perl_debug_log,
3659 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3660 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3667 - regrepeat_hard - repeatedly match something, report total lenth and length
3669 * The repeater is supposed to have constant length.
3673 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3676 register char *scan;
3677 register char *start;
3678 register char *loceol = PL_regeol;
3680 I32 count = 0, res = 1;
3685 start = PL_reginput;
3687 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3690 while (start < PL_reginput) {
3692 start += UTF8SKIP(start);
3703 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3705 *lp = l = PL_reginput - start;
3706 if (max != REG_INFTY && l*max < loceol - scan)
3707 loceol = scan + l*max;
3720 - reginclass - determine if a character falls into a character class
3724 S_reginclass(pTHX_ register regnode *p, register I32 c)
3727 char flags = ANYOF_FLAGS(p);
3731 if (ANYOF_BITMAP_TEST(p, c))
3733 else if (flags & ANYOF_FOLD) {
3735 if (flags & ANYOF_LOCALE) {
3736 PL_reg_flags |= RF_tainted;
3737 cf = PL_fold_locale[c];
3741 if (ANYOF_BITMAP_TEST(p, cf))
3745 if (!match && (flags & ANYOF_CLASS)) {
3746 PL_reg_flags |= RF_tainted;
3748 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3749 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3750 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3751 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3752 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3753 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3754 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3755 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3756 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3757 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3758 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3759 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3760 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3761 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3762 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3763 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3764 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3765 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3766 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3767 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3768 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3769 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3770 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3771 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3772 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3773 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
3774 (ANYOF_CLASS_TEST(p, ANYOF_PSXSPC) && isPSXSPC(c)) ||
3775 (ANYOF_CLASS_TEST(p, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
3776 (ANYOF_CLASS_TEST(p, ANYOF_BLANK) && isBLANK(c)) ||
3777 (ANYOF_CLASS_TEST(p, ANYOF_NBLANK) && !isBLANK(c))
3778 ) /* How's that for a conditional? */
3784 return (flags & ANYOF_INVERT) ? !match : match;
3788 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3791 char flags = ARG1(f);
3793 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3795 if (swash_fetch(sv, p))
3797 else if (flags & ANYOF_FOLD) {
3798 U8 tmpbuf[UTF8_MAXLEN];
3799 if (flags & ANYOF_LOCALE) {
3800 PL_reg_flags |= RF_tainted;
3801 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3804 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3805 if (swash_fetch(sv, tmpbuf))
3809 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3811 return (flags & ANYOF_INVERT) ? !match : match;
3815 S_reghop(pTHX_ U8 *s, I32 off)
3819 while (off-- && s < (U8*)PL_regeol)
3824 if (s > (U8*)PL_bostr) {
3827 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3829 } /* XXX could check well-formedness here */
3837 S_reghopmaybe(pTHX_ U8* s, I32 off)
3841 while (off-- && s < (U8*)PL_regeol)
3848 if (s > (U8*)PL_bostr) {
3851 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3853 } /* XXX could check well-formedness here */
3869 restore_pos(pTHXo_ void *arg)
3872 if (PL_reg_eval_set) {
3873 if (PL_reg_oldsaved) {
3874 PL_reg_re->subbeg = PL_reg_oldsaved;
3875 PL_reg_re->sublen = PL_reg_oldsavedlen;
3876 RX_MATCH_COPIED_on(PL_reg_re);
3878 PL_reg_magic->mg_len = PL_reg_oldpos;
3879 PL_reg_eval_set = 0;
3880 PL_curpm = PL_reg_oldcurpm;