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))
110 # define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch(*av_fetch((AV*)SvRV((SV*)PL_regdata->data[ARG2(f)]),0,FALSE),p))
112 # define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
115 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
116 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
118 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
119 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
120 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
121 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
122 #define HOPc(pos,off) ((char*)HOP(pos,off))
123 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
125 static void restore_pos(pTHXo_ void *arg);
129 S_regcppush(pTHX_ I32 parenfloor)
132 int retval = PL_savestack_ix;
133 int i = (PL_regsize - parenfloor) * 4;
137 for (p = PL_regsize; p > parenfloor; p--) {
138 SSPUSHINT(PL_regendp[p]);
139 SSPUSHINT(PL_regstartp[p]);
140 SSPUSHPTR(PL_reg_start_tmp[p]);
143 SSPUSHINT(PL_regsize);
144 SSPUSHINT(*PL_reglastparen);
145 SSPUSHPTR(PL_reginput);
147 SSPUSHINT(SAVEt_REGCONTEXT);
151 /* These are needed since we do not localize EVAL nodes: */
152 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
153 " Setting an EVAL scope, savestack=%"IVdf"\n", \
154 (IV)PL_savestack_ix)); cp = PL_savestack_ix
156 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
157 PerlIO_printf(Perl_debug_log, \
158 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
159 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
169 assert(i == SAVEt_REGCONTEXT);
171 input = (char *) SSPOPPTR;
172 *PL_reglastparen = SSPOPINT;
173 PL_regsize = SSPOPINT;
174 for (i -= 3; i > 0; i -= 4) {
175 paren = (U32)SSPOPINT;
176 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
177 PL_regstartp[paren] = SSPOPINT;
179 if (paren <= *PL_reglastparen)
180 PL_regendp[paren] = tmps;
182 PerlIO_printf(Perl_debug_log,
183 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
184 (UV)paren, (IV)PL_regstartp[paren],
185 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
186 (IV)PL_regendp[paren],
187 (paren > *PL_reglastparen ? "(no)" : ""));
191 if (*PL_reglastparen + 1 <= PL_regnpar) {
192 PerlIO_printf(Perl_debug_log,
193 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
194 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
198 /* It would seem that the similar code in regtry()
199 * already takes care of this, and in fact it is in
200 * a better location to since this code can #if 0-ed out
201 * but the code in regtry() is needed or otherwise tests
202 * requiring null fields (pat.t#187 and split.t#{13,14}
203 * (as of patchlevel 7877) will fail. Then again,
204 * this code seems to be necessary or otherwise
205 * building DynaLoader will fail:
206 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
208 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
209 if (paren > PL_regsize)
210 PL_regstartp[paren] = -1;
211 PL_regendp[paren] = -1;
218 S_regcp_set_to(pTHX_ I32 ss)
221 I32 tmp = PL_savestack_ix;
223 PL_savestack_ix = ss;
225 PL_savestack_ix = tmp;
229 typedef struct re_cc_state
233 struct re_cc_state *prev;
238 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
240 #define TRYPAREN(paren, n, input) { \
243 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
244 PL_regendp[paren] = input - PL_bostr; \
247 PL_regendp[paren] = -1; \
249 if (regmatch(next)) \
252 PL_regendp[paren] = -1; \
257 * pregexec and friends
261 - pregexec - match a regexp against a string
264 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
265 char *strbeg, I32 minend, SV *screamer, U32 nosave)
266 /* strend: pointer to null at end of string */
267 /* strbeg: real beginning of string */
268 /* minend: end of match must be >=minend after stringarg. */
269 /* nosave: For optimizations. */
272 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
273 nosave ? 0 : REXEC_COPY_STR);
277 S_cache_re(pTHX_ regexp *prog)
280 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
282 PL_regprogram = prog->program;
284 PL_regnpar = prog->nparens;
285 PL_regdata = prog->data;
290 * Need to implement the following flags for reg_anch:
292 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
294 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
295 * INTUIT_AUTORITATIVE_ML
296 * INTUIT_ONCE_NOML - Intuit can match in one location only.
299 * Another flag for this function: SECOND_TIME (so that float substrs
300 * with giant delta may be not rechecked).
303 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
305 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
306 Otherwise, only SvCUR(sv) is used to get strbeg. */
308 /* XXXX We assume that strpos is strbeg unless sv. */
310 /* XXXX Some places assume that there is a fixed substring.
311 An update may be needed if optimizer marks as "INTUITable"
312 RExen without fixed substrings. Similarly, it is assumed that
313 lengths of all the strings are no more than minlen, thus they
314 cannot come from lookahead.
315 (Or minlen should take into account lookahead.) */
317 /* A failure to find a constant substring means that there is no need to make
318 an expensive call to REx engine, thus we celebrate a failure. Similarly,
319 finding a substring too deep into the string means that less calls to
320 regtry() should be needed.
322 REx compiler's optimizer found 4 possible hints:
323 a) Anchored substring;
325 c) Whether we are anchored (beginning-of-line or \G);
326 d) First node (of those at offset 0) which may distingush positions;
327 We use a)b)d) and multiline-part of c), and try to find a position in the
328 string which does not contradict any of them.
331 /* Most of decisions we do here should have been done at compile time.
332 The nodes of the REx which we used for the search should have been
333 deleted from the finite automaton. */
336 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
337 char *strend, U32 flags, re_scream_pos_data *data)
339 register I32 start_shift;
340 /* Should be nonnegative! */
341 register I32 end_shift;
348 register char *other_last = Nullch; /* other substr checked before this */
349 char *check_at; /* check substr found at this pos */
351 char *i_strpos = strpos;
354 DEBUG_r( if (!PL_colorset) reginitcolors() );
355 DEBUG_r(PerlIO_printf(Perl_debug_log,
356 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
357 PL_colors[4],PL_colors[5],PL_colors[0],
360 (strlen(prog->precomp) > 60 ? "..." : ""),
362 (int)(strend - strpos > 60 ? 60 : strend - strpos),
363 strpos, PL_colors[1],
364 (strend - strpos > 60 ? "..." : ""))
367 if (prog->minlen > strend - strpos) {
368 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
371 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
372 check = prog->check_substr;
373 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
374 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
375 || ( (prog->reganch & ROPT_ANCH_BOL)
376 && !PL_multiline ) ); /* Check after \n? */
379 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
380 /* SvCUR is not set on references: SvRV and SvPVX overlap */
382 && (strpos != strbeg)) {
383 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
386 if (prog->check_offset_min == prog->check_offset_max) {
387 /* Substring at constant offset from beg-of-str... */
390 PL_regeol = strend; /* Used in HOP() */
391 s = HOPc(strpos, prog->check_offset_min);
393 slen = SvCUR(check); /* >= 1 */
395 if ( strend - s > slen || strend - s < slen - 1
396 || (strend - s == slen && strend[-1] != '\n')) {
397 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
400 /* Now should match s[0..slen-2] */
402 if (slen && (*SvPVX(check) != *s
404 && memNE(SvPVX(check), s, slen)))) {
406 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
410 else if (*SvPVX(check) != *s
411 || ((slen = SvCUR(check)) > 1
412 && memNE(SvPVX(check), s, slen)))
414 goto success_at_start;
417 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
419 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
420 end_shift = prog->minlen - start_shift -
421 CHR_SVLEN(check) + (SvTAIL(check) != 0);
423 I32 end = prog->check_offset_max + CHR_SVLEN(check)
424 - (SvTAIL(check) != 0);
425 I32 eshift = strend - s - end;
427 if (end_shift < eshift)
431 else { /* Can match at random position */
434 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
435 /* Should be nonnegative! */
436 end_shift = prog->minlen - start_shift -
437 CHR_SVLEN(check) + (SvTAIL(check) != 0);
440 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
442 Perl_croak(aTHX_ "panic: end_shift");
446 /* Find a possible match in the region s..strend by looking for
447 the "check" substring in the region corrected by start/end_shift. */
448 if (flags & REXEC_SCREAM) {
449 I32 p = -1; /* Internal iterator of scream. */
450 I32 *pp = data ? data->scream_pos : &p;
452 if (PL_screamfirst[BmRARE(check)] >= 0
453 || ( BmRARE(check) == '\n'
454 && (BmPREVIOUS(check) == SvCUR(check) - 1)
456 s = screaminstr(sv, check,
457 start_shift + (s - strbeg), end_shift, pp, 0);
461 *data->scream_olds = s;
464 s = fbm_instr((unsigned char*)s + start_shift,
465 (unsigned char*)strend - end_shift,
466 check, PL_multiline ? FBMrf_MULTILINE : 0);
468 /* Update the count-of-usability, remove useless subpatterns,
471 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
472 (s ? "Found" : "Did not find"),
473 ((check == prog->anchored_substr) ? "anchored" : "floating"),
475 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
477 PL_colors[1], (SvTAIL(check) ? "$" : ""),
478 (s ? " at offset " : "...\n") ) );
485 /* Finish the diagnostic message */
486 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
488 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
489 Start with the other substr.
490 XXXX no SCREAM optimization yet - and a very coarse implementation
491 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
492 *always* match. Probably should be marked during compile...
493 Probably it is right to do no SCREAM here...
496 if (prog->float_substr && prog->anchored_substr) {
497 /* Take into account the "other" substring. */
498 /* XXXX May be hopelessly wrong for UTF... */
501 if (check == prog->float_substr) {
504 char *last = s - start_shift, *last1, *last2;
508 t = s - prog->check_offset_max;
509 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
510 && (!(prog->reganch & ROPT_UTF8)
511 || (PL_bostr = strpos, /* Used in regcopmaybe() */
512 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
517 t += prog->anchored_offset;
518 if (t < other_last) /* These positions already checked */
521 last2 = last1 = strend - prog->minlen;
524 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
525 /* On end-of-str: see comment below. */
526 s = fbm_instr((unsigned char*)t,
527 (unsigned char*)last1 + prog->anchored_offset
528 + SvCUR(prog->anchored_substr)
529 - (SvTAIL(prog->anchored_substr)!=0),
530 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
531 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
532 (s ? "Found" : "Contradicts"),
534 (int)(SvCUR(prog->anchored_substr)
535 - (SvTAIL(prog->anchored_substr)!=0)),
536 SvPVX(prog->anchored_substr),
537 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
539 if (last1 >= last2) {
540 DEBUG_r(PerlIO_printf(Perl_debug_log,
541 ", giving up...\n"));
544 DEBUG_r(PerlIO_printf(Perl_debug_log,
545 ", trying floating at offset %ld...\n",
546 (long)(s1 + 1 - i_strpos)));
547 PL_regeol = strend; /* Used in HOP() */
548 other_last = last1 + prog->anchored_offset + 1;
553 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
554 (long)(s - i_strpos)));
555 t = s - prog->anchored_offset;
564 else { /* Take into account the floating substring. */
569 last1 = last = strend - prog->minlen + prog->float_min_offset;
570 if (last - t > prog->float_max_offset)
571 last = t + prog->float_max_offset;
572 s = t + prog->float_min_offset;
575 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
576 /* fbm_instr() takes into account exact value of end-of-str
577 if the check is SvTAIL(ed). Since false positives are OK,
578 and end-of-str is not later than strend we are OK. */
579 s = fbm_instr((unsigned char*)s,
580 (unsigned char*)last + SvCUR(prog->float_substr)
581 - (SvTAIL(prog->float_substr)!=0),
582 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
583 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
584 (s ? "Found" : "Contradicts"),
586 (int)(SvCUR(prog->float_substr)
587 - (SvTAIL(prog->float_substr)!=0)),
588 SvPVX(prog->float_substr),
589 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
592 DEBUG_r(PerlIO_printf(Perl_debug_log,
593 ", giving up...\n"));
596 DEBUG_r(PerlIO_printf(Perl_debug_log,
597 ", trying anchored starting at offset %ld...\n",
598 (long)(s1 + 1 - i_strpos)));
599 other_last = last + 1;
600 PL_regeol = strend; /* Used in HOP() */
605 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
606 (long)(s - i_strpos)));
616 t = s - prog->check_offset_max;
618 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
619 && (!(prog->reganch & ROPT_UTF8)
620 || (PL_bostr = strpos, /* Used in regcopmaybe() */
621 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
624 /* Fixed substring is found far enough so that the match
625 cannot start at strpos. */
627 if (ml_anch && t[-1] != '\n') {
628 /* Eventually fbm_*() should handle this, but often
629 anchored_offset is not 0, so this check will not be wasted. */
630 /* XXXX In the code below we prefer to look for "^" even in
631 presence of anchored substrings. And we search even
632 beyond the found float position. These pessimizations
633 are historical artefacts only. */
635 while (t < strend - prog->minlen) {
637 if (t < check_at - prog->check_offset_min) {
638 if (prog->anchored_substr) {
639 /* Since we moved from the found position,
640 we definitely contradict the found anchored
641 substr. Due to the above check we do not
642 contradict "check" substr.
643 Thus we can arrive here only if check substr
644 is float. Redo checking for "other"=="fixed".
647 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
648 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
649 goto do_other_anchored;
651 /* We don't contradict the found floating substring. */
652 /* XXXX Why not check for STCLASS? */
654 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
655 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
658 /* Position contradicts check-string */
659 /* XXXX probably better to look for check-string
660 than for "\n", so one should lower the limit for t? */
661 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
662 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
663 other_last = strpos = s = t + 1;
668 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
669 PL_colors[0],PL_colors[1]));
673 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
674 PL_colors[0],PL_colors[1]));
678 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
682 /* The found string does not prohibit matching at strpos,
683 - no optimization of calling REx engine can be performed,
684 unless it was an MBOL and we are not after MBOL,
685 or a future STCLASS check will fail this. */
687 /* Even in this situation we may use MBOL flag if strpos is offset
688 wrt the start of the string. */
689 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
690 && (strpos != strbeg) && strpos[-1] != '\n'
691 /* May be due to an implicit anchor of m{.*foo} */
692 && !(prog->reganch & ROPT_IMPLICIT))
697 DEBUG_r( if (ml_anch)
698 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
699 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
702 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
703 && prog->check_substr /* Could be deleted already */
704 && --BmUSEFUL(prog->check_substr) < 0
705 && prog->check_substr == prog->float_substr)
707 /* If flags & SOMETHING - do not do it many times on the same match */
708 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
709 SvREFCNT_dec(prog->check_substr);
710 prog->check_substr = Nullsv; /* disable */
711 prog->float_substr = Nullsv; /* clear */
712 check = Nullsv; /* abort */
714 /* XXXX This is a remnant of the old implementation. It
715 looks wasteful, since now INTUIT can use many
717 prog->reganch &= ~RE_USE_INTUIT;
724 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
725 if (prog->regstclass) {
726 /* minlen == 0 is possible if regstclass is \b or \B,
727 and the fixed substr is ''$.
728 Since minlen is already taken into account, s+1 is before strend;
729 accidentally, minlen >= 1 guaranties no false positives at s + 1
730 even for \b or \B. But (minlen? 1 : 0) below assumes that
731 regstclass does not come from lookahead... */
732 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
733 This leaves EXACTF only, which is dealt with in find_byclass(). */
734 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
735 ? STR_LEN(prog->regstclass)
737 char *endpos = (prog->anchored_substr || ml_anch)
738 ? s + (prog->minlen? cl_l : 0)
739 : (prog->float_substr ? check_at - start_shift + cl_l
741 char *startpos = strbeg;
744 if (prog->reganch & ROPT_UTF8) {
745 PL_regdata = prog->data; /* Used by REGINCLASS UTF logic */
748 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
753 if (endpos == strend) {
754 DEBUG_r( PerlIO_printf(Perl_debug_log,
755 "Could not match STCLASS...\n") );
758 DEBUG_r( PerlIO_printf(Perl_debug_log,
759 "This position contradicts STCLASS...\n") );
760 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
762 /* Contradict one of substrings */
763 if (prog->anchored_substr) {
764 if (prog->anchored_substr == check) {
765 DEBUG_r( what = "anchored" );
767 PL_regeol = strend; /* Used in HOP() */
769 if (s + start_shift + end_shift > strend) {
770 /* XXXX Should be taken into account earlier? */
771 DEBUG_r( PerlIO_printf(Perl_debug_log,
772 "Could not match STCLASS...\n") );
777 DEBUG_r( PerlIO_printf(Perl_debug_log,
778 "Looking for %s substr starting at offset %ld...\n",
779 what, (long)(s + start_shift - i_strpos)) );
782 /* Have both, check_string is floating */
783 if (t + start_shift >= check_at) /* Contradicts floating=check */
784 goto retry_floating_check;
785 /* Recheck anchored substring, but not floating... */
789 DEBUG_r( PerlIO_printf(Perl_debug_log,
790 "Looking for anchored substr starting at offset %ld...\n",
791 (long)(other_last - i_strpos)) );
792 goto do_other_anchored;
794 /* Another way we could have checked stclass at the
795 current position only: */
800 DEBUG_r( PerlIO_printf(Perl_debug_log,
801 "Looking for /%s^%s/m starting at offset %ld...\n",
802 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
805 if (!prog->float_substr) /* Could have been deleted */
807 /* Check is floating subtring. */
808 retry_floating_check:
809 t = check_at - start_shift;
810 DEBUG_r( what = "floating" );
811 goto hop_and_restart;
814 PerlIO_printf(Perl_debug_log,
815 "By STCLASS: moving %ld --> %ld\n",
816 (long)(t - i_strpos), (long)(s - i_strpos));
818 PerlIO_printf(Perl_debug_log,
819 "Does not contradict STCLASS...\n") );
822 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
823 PL_colors[4], (check ? "Guessed" : "Giving up"),
824 PL_colors[5], (long)(s - i_strpos)) );
827 fail_finish: /* Substring not found */
828 if (prog->check_substr) /* could be removed already */
829 BmUSEFUL(prog->check_substr) += 5; /* hooray */
831 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
832 PL_colors[4],PL_colors[5]));
836 /* We know what class REx starts with. Try to find this position... */
838 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
840 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
846 register I32 tmp = 1; /* Scratch variable? */
848 /* We know what class it must start with. */
852 if (REGINCLASSUTF8(c, (U8*)s)) {
853 if (tmp && (norun || regtry(prog, s)))
865 if (REGINCLASS(c, *(U8*)s)) {
866 if (tmp && (norun || regtry(prog, s)))
886 c2 = PL_fold_locale[c1];
891 e = s; /* Due to minlen logic of intuit() */
892 /* Here it is NOT UTF! */
896 && (ln == 1 || !(OP(c) == EXACTF
898 : ibcmp_locale(s, m, ln)))
899 && (norun || regtry(prog, s)) )
905 if ( (*(U8*)s == c1 || *(U8*)s == c2)
906 && (ln == 1 || !(OP(c) == EXACTF
908 : ibcmp_locale(s, m, ln)))
909 && (norun || regtry(prog, s)) )
916 PL_reg_flags |= RF_tainted;
919 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
920 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
922 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
924 if ((norun || regtry(prog, s)))
929 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
933 PL_reg_flags |= RF_tainted;
939 U8 *r = reghop((U8*)s, -1);
941 tmp = (I32)utf8_to_uv(r, s - (char*)r, 0, 0);
943 tmp = ((OP(c) == BOUNDUTF8 ?
944 isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
946 if (tmp == !(OP(c) == BOUNDUTF8 ?
947 swash_fetch(PL_utf8_alnum, (U8*)s) :
948 isALNUM_LC_utf8((U8*)s)))
951 if ((norun || regtry(prog, s)))
956 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
960 PL_reg_flags |= RF_tainted;
963 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
964 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
966 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
968 else if ((norun || regtry(prog, s)))
972 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
976 PL_reg_flags |= RF_tainted;
982 U8 *r = reghop((U8*)s, -1);
984 tmp = (I32)utf8_to_uv(r, s - (char*)r, 0, 0);
986 tmp = ((OP(c) == NBOUNDUTF8 ?
987 isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
989 if (tmp == !(OP(c) == NBOUNDUTF8 ?
990 swash_fetch(PL_utf8_alnum, (U8*)s) :
991 isALNUM_LC_utf8((U8*)s)))
993 else if ((norun || regtry(prog, s)))
997 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1001 while (s < strend) {
1003 if (tmp && (norun || regtry(prog, s)))
1014 while (s < strend) {
1015 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1016 if (tmp && (norun || regtry(prog, s)))
1027 PL_reg_flags |= RF_tainted;
1028 while (s < strend) {
1029 if (isALNUM_LC(*s)) {
1030 if (tmp && (norun || regtry(prog, s)))
1041 PL_reg_flags |= RF_tainted;
1042 while (s < strend) {
1043 if (isALNUM_LC_utf8((U8*)s)) {
1044 if (tmp && (norun || regtry(prog, s)))
1055 while (s < strend) {
1057 if (tmp && (norun || regtry(prog, s)))
1068 while (s < strend) {
1069 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1070 if (tmp && (norun || regtry(prog, s)))
1081 PL_reg_flags |= RF_tainted;
1082 while (s < strend) {
1083 if (!isALNUM_LC(*s)) {
1084 if (tmp && (norun || regtry(prog, s)))
1095 PL_reg_flags |= RF_tainted;
1096 while (s < strend) {
1097 if (!isALNUM_LC_utf8((U8*)s)) {
1098 if (tmp && (norun || regtry(prog, s)))
1109 while (s < strend) {
1111 if (tmp && (norun || regtry(prog, s)))
1122 while (s < strend) {
1123 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1124 if (tmp && (norun || regtry(prog, s)))
1135 PL_reg_flags |= RF_tainted;
1136 while (s < strend) {
1137 if (isSPACE_LC(*s)) {
1138 if (tmp && (norun || regtry(prog, s)))
1149 PL_reg_flags |= RF_tainted;
1150 while (s < strend) {
1151 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1152 if (tmp && (norun || regtry(prog, s)))
1163 while (s < strend) {
1165 if (tmp && (norun || regtry(prog, s)))
1176 while (s < strend) {
1177 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1178 if (tmp && (norun || regtry(prog, s)))
1189 PL_reg_flags |= RF_tainted;
1190 while (s < strend) {
1191 if (!isSPACE_LC(*s)) {
1192 if (tmp && (norun || regtry(prog, s)))
1203 PL_reg_flags |= RF_tainted;
1204 while (s < strend) {
1205 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1206 if (tmp && (norun || regtry(prog, s)))
1217 while (s < strend) {
1219 if (tmp && (norun || regtry(prog, s)))
1230 while (s < strend) {
1231 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1232 if (tmp && (norun || regtry(prog, s)))
1243 PL_reg_flags |= RF_tainted;
1244 while (s < strend) {
1245 if (isDIGIT_LC(*s)) {
1246 if (tmp && (norun || regtry(prog, s)))
1257 PL_reg_flags |= RF_tainted;
1258 while (s < strend) {
1259 if (isDIGIT_LC_utf8((U8*)s)) {
1260 if (tmp && (norun || regtry(prog, s)))
1271 while (s < strend) {
1273 if (tmp && (norun || regtry(prog, s)))
1284 while (s < strend) {
1285 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1286 if (tmp && (norun || regtry(prog, s)))
1297 PL_reg_flags |= RF_tainted;
1298 while (s < strend) {
1299 if (!isDIGIT_LC(*s)) {
1300 if (tmp && (norun || regtry(prog, s)))
1311 PL_reg_flags |= RF_tainted;
1312 while (s < strend) {
1313 if (!isDIGIT_LC_utf8((U8*)s)) {
1314 if (tmp && (norun || regtry(prog, s)))
1325 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1334 - regexec_flags - match a regexp against a string
1337 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1338 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1339 /* strend: pointer to null at end of string */
1340 /* strbeg: real beginning of string */
1341 /* minend: end of match must be >=minend after stringarg. */
1342 /* data: May be used for some additional optimizations. */
1343 /* nosave: For optimizations. */
1347 register regnode *c;
1348 register char *startpos = stringarg;
1349 I32 minlen; /* must match at least this many chars */
1350 I32 dontbother = 0; /* how many characters not to try at end */
1351 /* I32 start_shift = 0; */ /* Offset of the start to find
1352 constant substr. */ /* CC */
1353 I32 end_shift = 0; /* Same for the end. */ /* CC */
1354 I32 scream_pos = -1; /* Internal iterator of scream. */
1356 SV* oreplsv = GvSV(PL_replgv);
1362 PL_regnarrate = PL_debug & 512;
1365 /* Be paranoid... */
1366 if (prog == NULL || startpos == NULL) {
1367 Perl_croak(aTHX_ "NULL regexp parameter");
1371 minlen = prog->minlen;
1372 if (strend - startpos < minlen) goto phooey;
1374 if (startpos == strbeg) /* is ^ valid at stringarg? */
1377 PL_regprev = (U32)stringarg[-1];
1378 if (!PL_multiline && PL_regprev == '\n')
1379 PL_regprev = '\0'; /* force ^ to NOT match */
1382 /* Check validity of program. */
1383 if (UCHARAT(prog->program) != REG_MAGIC) {
1384 Perl_croak(aTHX_ "corrupted regexp program");
1388 PL_reg_eval_set = 0;
1391 if (prog->reganch & ROPT_UTF8)
1392 PL_reg_flags |= RF_utf8;
1394 /* Mark beginning of line for ^ and lookbehind. */
1395 PL_regbol = startpos;
1399 /* Mark end of line for $ (and such) */
1402 /* see how far we have to get to not match where we matched before */
1403 PL_regtill = startpos+minend;
1405 /* We start without call_cc context. */
1408 /* If there is a "must appear" string, look for it. */
1411 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1414 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1415 PL_reg_ganch = startpos;
1416 else if (sv && SvTYPE(sv) >= SVt_PVMG
1418 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1419 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1420 if (prog->reganch & ROPT_ANCH_GPOS) {
1421 if (s > PL_reg_ganch)
1426 else /* pos() not defined */
1427 PL_reg_ganch = strbeg;
1430 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1431 re_scream_pos_data d;
1433 d.scream_olds = &scream_olds;
1434 d.scream_pos = &scream_pos;
1435 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1437 goto phooey; /* not present */
1440 DEBUG_r( if (!PL_colorset) reginitcolors() );
1441 DEBUG_r(PerlIO_printf(Perl_debug_log,
1442 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1443 PL_colors[4],PL_colors[5],PL_colors[0],
1446 (strlen(prog->precomp) > 60 ? "..." : ""),
1448 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1449 startpos, PL_colors[1],
1450 (strend - startpos > 60 ? "..." : ""))
1453 /* Simplest case: anchored match need be tried only once. */
1454 /* [unless only anchor is BOL and multiline is set] */
1455 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1456 if (s == startpos && regtry(prog, startpos))
1458 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1459 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1464 dontbother = minlen - 1;
1465 end = HOPc(strend, -dontbother) - 1;
1466 /* for multiline we only have to try after newlines */
1467 if (prog->check_substr) {
1471 if (regtry(prog, s))
1476 if (prog->reganch & RE_USE_INTUIT) {
1477 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1488 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1489 if (regtry(prog, s))
1496 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1497 if (regtry(prog, PL_reg_ganch))
1502 /* Messy cases: unanchored match. */
1503 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1504 /* we have /x+whatever/ */
1505 /* it must be a one character string (XXXX Except UTF?) */
1506 char ch = SvPVX(prog->anchored_substr)[0];
1512 while (s < strend) {
1514 DEBUG_r( did_match = 1 );
1515 if (regtry(prog, s)) goto got_it;
1517 while (s < strend && *s == ch)
1524 while (s < strend) {
1526 DEBUG_r( did_match = 1 );
1527 if (regtry(prog, s)) goto got_it;
1529 while (s < strend && *s == ch)
1535 DEBUG_r(did_match ||
1536 PerlIO_printf(Perl_debug_log,
1537 "Did not find anchored character...\n"));
1540 else if (prog->anchored_substr != Nullsv
1541 || (prog->float_substr != Nullsv
1542 && prog->float_max_offset < strend - s)) {
1543 SV *must = prog->anchored_substr
1544 ? prog->anchored_substr : prog->float_substr;
1546 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1548 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1549 char *last = HOPc(strend, /* Cannot start after this */
1550 -(I32)(CHR_SVLEN(must)
1551 - (SvTAIL(must) != 0) + back_min));
1552 char *last1; /* Last position checked before */
1558 last1 = HOPc(s, -1);
1560 last1 = s - 1; /* bogus */
1562 /* XXXX check_substr already used to find `s', can optimize if
1563 check_substr==must. */
1565 dontbother = end_shift;
1566 strend = HOPc(strend, -dontbother);
1567 while ( (s <= last) &&
1568 ((flags & REXEC_SCREAM)
1569 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1570 end_shift, &scream_pos, 0))
1571 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1572 (unsigned char*)strend, must,
1573 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1574 DEBUG_r( did_match = 1 );
1575 if (HOPc(s, -back_max) > last1) {
1576 last1 = HOPc(s, -back_min);
1577 s = HOPc(s, -back_max);
1580 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1582 last1 = HOPc(s, -back_min);
1586 while (s <= last1) {
1587 if (regtry(prog, s))
1593 while (s <= last1) {
1594 if (regtry(prog, s))
1600 DEBUG_r(did_match ||
1601 PerlIO_printf(Perl_debug_log, "Did not find %s substr `%s%.*s%s'%s...\n",
1602 ((must == prog->anchored_substr)
1603 ? "anchored" : "floating"),
1605 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1607 PL_colors[1], (SvTAIL(must) ? "$" : "")));
1610 else if ((c = prog->regstclass)) {
1611 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1612 /* don't bother with what can't match */
1613 strend = HOPc(strend, -(minlen - 1));
1614 if (find_byclass(prog, c, s, strend, startpos, 0))
1616 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1620 if (prog->float_substr != Nullsv) { /* Trim the end. */
1623 if (flags & REXEC_SCREAM) {
1624 last = screaminstr(sv, prog->float_substr, s - strbeg,
1625 end_shift, &scream_pos, 1); /* last one */
1627 last = scream_olds; /* Only one occurence. */
1631 char *little = SvPV(prog->float_substr, len);
1633 if (SvTAIL(prog->float_substr)) {
1634 if (memEQ(strend - len + 1, little, len - 1))
1635 last = strend - len + 1;
1636 else if (!PL_multiline)
1637 last = memEQ(strend - len, little, len)
1638 ? strend - len : Nullch;
1644 last = rninstr(s, strend, little, little + len);
1646 last = strend; /* matching `$' */
1650 DEBUG_r(PerlIO_printf(Perl_debug_log,
1651 "%sCan't trim the tail, match fails (should not happen)%s\n",
1652 PL_colors[4],PL_colors[5]));
1653 goto phooey; /* Should not happen! */
1655 dontbother = strend - last + prog->float_min_offset;
1657 if (minlen && (dontbother < minlen))
1658 dontbother = minlen - 1;
1659 strend -= dontbother; /* this one's always in bytes! */
1660 /* We don't know much -- general case. */
1663 if (regtry(prog, s))
1672 if (regtry(prog, s))
1674 } while (s++ < strend);
1682 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1684 if (PL_reg_eval_set) {
1685 /* Preserve the current value of $^R */
1686 if (oreplsv != GvSV(PL_replgv))
1687 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1688 restored, the value remains
1690 restore_pos(aTHXo_ 0);
1693 /* make sure $`, $&, $', and $digit will work later */
1694 if ( !(flags & REXEC_NOT_FIRST) ) {
1695 if (RX_MATCH_COPIED(prog)) {
1696 Safefree(prog->subbeg);
1697 RX_MATCH_COPIED_off(prog);
1699 if (flags & REXEC_COPY_STR) {
1700 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1702 s = savepvn(strbeg, i);
1705 RX_MATCH_COPIED_on(prog);
1708 prog->subbeg = strbeg;
1709 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1716 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1717 PL_colors[4],PL_colors[5]));
1718 if (PL_reg_eval_set)
1719 restore_pos(aTHXo_ 0);
1724 - regtry - try match at specific point
1726 STATIC I32 /* 0 failure, 1 success */
1727 S_regtry(pTHX_ regexp *prog, char *startpos)
1736 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1738 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1741 PL_reg_eval_set = RS_init;
1743 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1744 (IV)(PL_stack_sp - PL_stack_base));
1746 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1747 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1748 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1750 /* Apparently this is not needed, judging by wantarray. */
1751 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1752 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1755 /* Make $_ available to executed code. */
1756 if (PL_reg_sv != DEFSV) {
1757 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1762 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1763 && (mg = mg_find(PL_reg_sv, 'g')))) {
1764 /* prepare for quick setting of pos */
1765 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1766 mg = mg_find(PL_reg_sv, 'g');
1770 PL_reg_oldpos = mg->mg_len;
1771 SAVEDESTRUCTOR_X(restore_pos, 0);
1774 Newz(22,PL_reg_curpm, 1, PMOP);
1775 PL_reg_curpm->op_pmregexp = prog;
1776 PL_reg_oldcurpm = PL_curpm;
1777 PL_curpm = PL_reg_curpm;
1778 if (RX_MATCH_COPIED(prog)) {
1779 /* Here is a serious problem: we cannot rewrite subbeg,
1780 since it may be needed if this match fails. Thus
1781 $` inside (?{}) could fail... */
1782 PL_reg_oldsaved = prog->subbeg;
1783 PL_reg_oldsavedlen = prog->sublen;
1784 RX_MATCH_COPIED_off(prog);
1787 PL_reg_oldsaved = Nullch;
1788 prog->subbeg = PL_bostr;
1789 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1791 prog->startp[0] = startpos - PL_bostr;
1792 PL_reginput = startpos;
1793 PL_regstartp = prog->startp;
1794 PL_regendp = prog->endp;
1795 PL_reglastparen = &prog->lastparen;
1796 prog->lastparen = 0;
1798 DEBUG_r(PL_reg_starttry = startpos);
1799 if (PL_reg_start_tmpl <= prog->nparens) {
1800 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1801 if(PL_reg_start_tmp)
1802 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1804 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1807 /* XXXX What this code is doing here?!!! There should be no need
1808 to do this again and again, PL_reglastparen should take care of
1811 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1812 * Actually, the code in regcppop() (which Ilya may be meaning by
1813 * PL_reglastparen), is not needed at all by the test suite
1814 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1815 * enough, for building DynaLoader, or otherwise this
1816 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1817 * will happen. Meanwhile, this code *is* needed for the
1818 * above-mentioned test suite tests to succeed. The common theme
1819 * on those tests seems to be returning null fields from matches.
1824 if (prog->nparens) {
1825 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1832 if (regmatch(prog->program + 1)) {
1833 prog->endp[0] = PL_reginput - PL_bostr;
1836 REGCP_UNWIND(lastcp);
1840 #define RE_UNWIND_BRANCH 1
1841 #define RE_UNWIND_BRANCHJ 2
1845 typedef struct { /* XX: makes sense to enlarge it... */
1849 } re_unwind_generic_t;
1862 } re_unwind_branch_t;
1864 typedef union re_unwind_t {
1866 re_unwind_generic_t generic;
1867 re_unwind_branch_t branch;
1871 - regmatch - main matching routine
1873 * Conceptually the strategy is simple: check to see whether the current
1874 * node matches, call self recursively to see whether the rest matches,
1875 * and then act accordingly. In practice we make some effort to avoid
1876 * recursion, in particular by going through "ordinary" nodes (that don't
1877 * need to know whether the rest of the match failed) by a loop instead of
1880 /* [lwall] I've hoisted the register declarations to the outer block in order to
1881 * maybe save a little bit of pushing and popping on the stack. It also takes
1882 * advantage of machines that use a register save mask on subroutine entry.
1884 STATIC I32 /* 0 failure, 1 success */
1885 S_regmatch(pTHX_ regnode *prog)
1888 register regnode *scan; /* Current node. */
1889 regnode *next; /* Next node. */
1890 regnode *inner; /* Next node in internal branch. */
1891 register I32 nextchr; /* renamed nextchr - nextchar colides with
1892 function of same name */
1893 register I32 n; /* no or next */
1894 register I32 ln; /* len or last */
1895 register char *s; /* operand or save */
1896 register char *locinput = PL_reginput;
1897 register I32 c1, c2, paren; /* case fold search, parenth */
1898 int minmod = 0, sw = 0, logical = 0;
1900 I32 firstcp = PL_savestack_ix;
1906 /* Note that nextchr is a byte even in UTF */
1907 nextchr = UCHARAT(locinput);
1909 while (scan != NULL) {
1910 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1912 # define sayYES goto yes
1913 # define sayNO goto no
1914 # define sayYES_FINAL goto yes_final
1915 # define sayYES_LOUD goto yes_loud
1916 # define sayNO_FINAL goto no_final
1917 # define sayNO_SILENT goto do_no
1918 # define saySAME(x) if (x) goto yes; else goto no
1919 # define REPORT_CODE_OFF 24
1921 # define sayYES return 1
1922 # define sayNO return 0
1923 # define sayYES_FINAL return 1
1924 # define sayYES_LOUD return 1
1925 # define sayNO_FINAL return 0
1926 # define sayNO_SILENT return 0
1927 # define saySAME(x) return x
1930 SV *prop = sv_newmortal();
1931 int docolor = *PL_colors[0];
1932 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1933 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1934 /* The part of the string before starttry has one color
1935 (pref0_len chars), between starttry and current
1936 position another one (pref_len - pref0_len chars),
1937 after the current position the third one.
1938 We assume that pref0_len <= pref_len, otherwise we
1939 decrease pref0_len. */
1940 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1941 ? (5 + taill) - l : locinput - PL_bostr);
1942 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1944 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1945 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1946 ? (5 + taill) - pref_len : PL_regeol - locinput);
1949 if (pref0_len > pref_len)
1950 pref0_len = pref_len;
1951 regprop(prop, scan);
1952 PerlIO_printf(Perl_debug_log,
1953 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1954 (IV)(locinput - PL_bostr),
1955 PL_colors[4], pref0_len,
1956 locinput - pref_len, PL_colors[5],
1957 PL_colors[2], pref_len - pref0_len,
1958 locinput - pref_len + pref0_len, PL_colors[3],
1959 (docolor ? "" : "> <"),
1960 PL_colors[0], l, locinput, PL_colors[1],
1961 15 - l - pref_len + 1,
1963 (IV)(scan - PL_regprogram), PL_regindent*2, "",
1967 next = scan + NEXT_OFF(scan);
1973 if (locinput == PL_bostr
1974 ? PL_regprev == '\n'
1976 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1978 /* regtill = regbol; */
1983 if (locinput == PL_bostr
1984 ? PL_regprev == '\n'
1985 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1991 if (locinput == PL_bostr)
1995 if (locinput == PL_reg_ganch)
2005 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2010 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2012 if (PL_regeol - locinput > 1)
2016 if (PL_regeol != locinput)
2020 if (nextchr & 0x80) {
2021 locinput += PL_utf8skip[nextchr];
2022 if (locinput > PL_regeol)
2024 nextchr = UCHARAT(locinput);
2027 if (!nextchr && locinput >= PL_regeol)
2029 nextchr = UCHARAT(++locinput);
2032 if (!nextchr && locinput >= PL_regeol)
2034 nextchr = UCHARAT(++locinput);
2037 if (nextchr & 0x80) {
2038 locinput += PL_utf8skip[nextchr];
2039 if (locinput > PL_regeol)
2041 nextchr = UCHARAT(locinput);
2044 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2046 nextchr = UCHARAT(++locinput);
2049 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2051 nextchr = UCHARAT(++locinput);
2056 /* Inline the first character, for speed. */
2057 if (UCHARAT(s) != nextchr)
2059 if (PL_regeol - locinput < ln)
2061 if (ln > 1 && memNE(s, locinput, ln))
2064 nextchr = UCHARAT(locinput);
2067 PL_reg_flags |= RF_tainted;
2076 c1 = OP(scan) == EXACTF;
2080 if (utf8_to_uv((U8*)s, e - s, 0, 0) !=
2082 toLOWER_utf8((U8*)l) :
2083 toLOWER_LC_utf8((U8*)l)))
2091 nextchr = UCHARAT(locinput);
2095 /* Inline the first character, for speed. */
2096 if (UCHARAT(s) != nextchr &&
2097 UCHARAT(s) != ((OP(scan) == EXACTF)
2098 ? PL_fold : PL_fold_locale)[nextchr])
2100 if (PL_regeol - locinput < ln)
2102 if (ln > 1 && (OP(scan) == EXACTF
2103 ? ibcmp(s, locinput, ln)
2104 : ibcmp_locale(s, locinput, ln)))
2107 nextchr = UCHARAT(locinput);
2110 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2112 if (locinput >= PL_regeol)
2114 locinput += PL_utf8skip[nextchr];
2115 nextchr = UCHARAT(locinput);
2119 nextchr = UCHARAT(locinput);
2120 if (!REGINCLASS(scan, nextchr))
2122 if (!nextchr && locinput >= PL_regeol)
2124 nextchr = UCHARAT(++locinput);
2127 PL_reg_flags |= RF_tainted;
2132 if (!(OP(scan) == ALNUM
2133 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2135 nextchr = UCHARAT(++locinput);
2138 PL_reg_flags |= RF_tainted;
2143 if (nextchr & 0x80) {
2144 if (!(OP(scan) == ALNUMUTF8
2145 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2146 : isALNUM_LC_utf8((U8*)locinput)))
2150 locinput += PL_utf8skip[nextchr];
2151 nextchr = UCHARAT(locinput);
2154 if (!(OP(scan) == ALNUMUTF8
2155 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2157 nextchr = UCHARAT(++locinput);
2160 PL_reg_flags |= RF_tainted;
2163 if (!nextchr && locinput >= PL_regeol)
2165 if (OP(scan) == NALNUM
2166 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2168 nextchr = UCHARAT(++locinput);
2171 PL_reg_flags |= RF_tainted;
2174 if (!nextchr && locinput >= PL_regeol)
2176 if (nextchr & 0x80) {
2177 if (OP(scan) == NALNUMUTF8
2178 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2179 : isALNUM_LC_utf8((U8*)locinput))
2183 locinput += PL_utf8skip[nextchr];
2184 nextchr = UCHARAT(locinput);
2187 if (OP(scan) == NALNUMUTF8
2188 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2190 nextchr = UCHARAT(++locinput);
2194 PL_reg_flags |= RF_tainted;
2198 /* was last char in word? */
2199 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2200 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2202 n = isALNUM(nextchr);
2205 ln = isALNUM_LC(ln);
2206 n = isALNUM_LC(nextchr);
2208 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2213 PL_reg_flags |= RF_tainted;
2217 /* was last char in word? */
2218 if (locinput == PL_regbol)
2221 U8 *r = reghop((U8*)locinput, -1);
2223 ln = utf8_to_uv(r, s - (char*)r, 0, 0);
2225 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2226 ln = isALNUM_uni(ln);
2227 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2230 ln = isALNUM_LC_uni(ln);
2231 n = isALNUM_LC_utf8((U8*)locinput);
2233 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2237 PL_reg_flags |= RF_tainted;
2242 if (!(OP(scan) == SPACE
2243 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2245 nextchr = UCHARAT(++locinput);
2248 PL_reg_flags |= RF_tainted;
2253 if (nextchr & 0x80) {
2254 if (!(OP(scan) == SPACEUTF8
2255 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2256 : isSPACE_LC_utf8((U8*)locinput)))
2260 locinput += PL_utf8skip[nextchr];
2261 nextchr = UCHARAT(locinput);
2264 if (!(OP(scan) == SPACEUTF8
2265 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2267 nextchr = UCHARAT(++locinput);
2270 PL_reg_flags |= RF_tainted;
2273 if (!nextchr && locinput >= PL_regeol)
2275 if (OP(scan) == NSPACE
2276 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2278 nextchr = UCHARAT(++locinput);
2281 PL_reg_flags |= RF_tainted;
2284 if (!nextchr && locinput >= PL_regeol)
2286 if (nextchr & 0x80) {
2287 if (OP(scan) == NSPACEUTF8
2288 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2289 : isSPACE_LC_utf8((U8*)locinput))
2293 locinput += PL_utf8skip[nextchr];
2294 nextchr = UCHARAT(locinput);
2297 if (OP(scan) == NSPACEUTF8
2298 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2300 nextchr = UCHARAT(++locinput);
2303 PL_reg_flags |= RF_tainted;
2308 if (!(OP(scan) == DIGIT
2309 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2311 nextchr = UCHARAT(++locinput);
2314 PL_reg_flags |= RF_tainted;
2319 if (nextchr & 0x80) {
2320 if (!(OP(scan) == DIGITUTF8
2321 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2322 : isDIGIT_LC_utf8((U8*)locinput)))
2326 locinput += PL_utf8skip[nextchr];
2327 nextchr = UCHARAT(locinput);
2330 if (!(OP(scan) == DIGITUTF8
2331 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2333 nextchr = UCHARAT(++locinput);
2336 PL_reg_flags |= RF_tainted;
2339 if (!nextchr && locinput >= PL_regeol)
2341 if (OP(scan) == NDIGIT
2342 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2344 nextchr = UCHARAT(++locinput);
2347 PL_reg_flags |= RF_tainted;
2350 if (!nextchr && locinput >= PL_regeol)
2352 if (nextchr & 0x80) {
2353 if (OP(scan) == NDIGITUTF8
2354 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2355 : isDIGIT_LC_utf8((U8*)locinput))
2359 locinput += PL_utf8skip[nextchr];
2360 nextchr = UCHARAT(locinput);
2363 if (OP(scan) == NDIGITUTF8
2364 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2366 nextchr = UCHARAT(++locinput);
2369 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2371 locinput += PL_utf8skip[nextchr];
2372 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2373 locinput += UTF8SKIP(locinput);
2374 if (locinput > PL_regeol)
2376 nextchr = UCHARAT(locinput);
2379 PL_reg_flags |= RF_tainted;
2383 n = ARG(scan); /* which paren pair */
2384 ln = PL_regstartp[n];
2385 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2386 if (*PL_reglastparen < n || ln == -1)
2387 sayNO; /* Do not match unless seen CLOSEn. */
2388 if (ln == PL_regendp[n])
2392 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2394 char *e = PL_bostr + PL_regendp[n];
2396 * Note that we can't do the "other character" lookup trick as
2397 * in the 8-bit case (no pun intended) because in Unicode we
2398 * have to map both upper and title case to lower case.
2400 if (OP(scan) == REFF) {
2404 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2414 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2421 nextchr = UCHARAT(locinput);
2425 /* Inline the first character, for speed. */
2426 if (UCHARAT(s) != nextchr &&
2428 (UCHARAT(s) != ((OP(scan) == REFF
2429 ? PL_fold : PL_fold_locale)[nextchr]))))
2431 ln = PL_regendp[n] - ln;
2432 if (locinput + ln > PL_regeol)
2434 if (ln > 1 && (OP(scan) == REF
2435 ? memNE(s, locinput, ln)
2437 ? ibcmp(s, locinput, ln)
2438 : ibcmp_locale(s, locinput, ln))))
2441 nextchr = UCHARAT(locinput);
2452 OP_4tree *oop = PL_op;
2453 COP *ocurcop = PL_curcop;
2454 SV **ocurpad = PL_curpad;
2458 PL_op = (OP_4tree*)PL_regdata->data[n];
2459 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2460 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2461 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2463 CALLRUNOPS(aTHX); /* Scalar context. */
2469 PL_curpad = ocurpad;
2470 PL_curcop = ocurcop;
2472 if (logical == 2) { /* Postponed subexpression. */
2474 MAGIC *mg = Null(MAGIC*);
2476 CHECKPOINT cp, lastcp;
2478 if(SvROK(ret) || SvRMAGICAL(ret)) {
2479 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2482 mg = mg_find(sv, 'r');
2485 re = (regexp *)mg->mg_obj;
2486 (void)ReREFCNT_inc(re);
2490 char *t = SvPV(ret, len);
2492 char *oprecomp = PL_regprecomp;
2493 I32 osize = PL_regsize;
2494 I32 onpar = PL_regnpar;
2497 pm.op_pmdynflags = (UTF||DO_UTF8(ret) ? PMdf_UTF8 : 0);
2498 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2500 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2501 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2502 PL_regprecomp = oprecomp;
2507 PerlIO_printf(Perl_debug_log,
2508 "Entering embedded `%s%.60s%s%s'\n",
2512 (strlen(re->precomp) > 60 ? "..." : ""))
2515 state.prev = PL_reg_call_cc;
2516 state.cc = PL_regcc;
2517 state.re = PL_reg_re;
2521 cp = regcppush(0); /* Save *all* the positions. */
2524 state.ss = PL_savestack_ix;
2525 *PL_reglastparen = 0;
2526 PL_reg_call_cc = &state;
2527 PL_reginput = locinput;
2529 /* XXXX This is too dramatic a measure... */
2532 if (regmatch(re->program + 1)) {
2533 /* Even though we succeeded, we need to restore
2534 global variables, since we may be wrapped inside
2535 SUSPEND, thus the match may be not finished yet. */
2537 /* XXXX Do this only if SUSPENDed? */
2538 PL_reg_call_cc = state.prev;
2539 PL_regcc = state.cc;
2540 PL_reg_re = state.re;
2541 cache_re(PL_reg_re);
2543 /* XXXX This is too dramatic a measure... */
2546 /* These are needed even if not SUSPEND. */
2552 REGCP_UNWIND(lastcp);
2554 PL_reg_call_cc = state.prev;
2555 PL_regcc = state.cc;
2556 PL_reg_re = state.re;
2557 cache_re(PL_reg_re);
2559 /* XXXX This is too dramatic a measure... */
2568 sv_setsv(save_scalar(PL_replgv), ret);
2572 n = ARG(scan); /* which paren pair */
2573 PL_reg_start_tmp[n] = locinput;
2578 n = ARG(scan); /* which paren pair */
2579 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2580 PL_regendp[n] = locinput - PL_bostr;
2581 if (n > *PL_reglastparen)
2582 *PL_reglastparen = n;
2585 n = ARG(scan); /* which paren pair */
2586 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2589 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2591 next = NEXTOPER(NEXTOPER(scan));
2593 next = scan + ARG(scan);
2594 if (OP(next) == IFTHEN) /* Fake one. */
2595 next = NEXTOPER(NEXTOPER(next));
2599 logical = scan->flags;
2601 /*******************************************************************
2602 PL_regcc contains infoblock about the innermost (...)* loop, and
2603 a pointer to the next outer infoblock.
2605 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2607 1) After matching X, regnode for CURLYX is processed;
2609 2) This regnode creates infoblock on the stack, and calls
2610 regmatch() recursively with the starting point at WHILEM node;
2612 3) Each hit of WHILEM node tries to match A and Z (in the order
2613 depending on the current iteration, min/max of {min,max} and
2614 greediness). The information about where are nodes for "A"
2615 and "Z" is read from the infoblock, as is info on how many times "A"
2616 was already matched, and greediness.
2618 4) After A matches, the same WHILEM node is hit again.
2620 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2621 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2622 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2623 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2624 of the external loop.
2626 Currently present infoblocks form a tree with a stem formed by PL_curcc
2627 and whatever it mentions via ->next, and additional attached trees
2628 corresponding to temporarily unset infoblocks as in "5" above.
2630 In the following picture infoblocks for outer loop of
2631 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2632 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2633 infoblocks are drawn below the "reset" infoblock.
2635 In fact in the picture below we do not show failed matches for Z and T
2636 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2637 more obvious *why* one needs to *temporary* unset infoblocks.]
2639 Matched REx position InfoBlocks Comment
2643 Y A)*?Z)*?T x <- O <- I
2644 YA )*?Z)*?T x <- O <- I
2645 YA A)*?Z)*?T x <- O <- I
2646 YAA )*?Z)*?T x <- O <- I
2647 YAA Z)*?T x <- O # Temporary unset I
2650 YAAZ Y(A)*?Z)*?T x <- O
2653 YAAZY (A)*?Z)*?T x <- O
2656 YAAZY A)*?Z)*?T x <- O <- I
2659 YAAZYA )*?Z)*?T x <- O <- I
2662 YAAZYA Z)*?T x <- O # Temporary unset I
2668 YAAZYAZ T x # Temporary unset O
2675 *******************************************************************/
2678 CHECKPOINT cp = PL_savestack_ix;
2679 /* No need to save/restore up to this paren */
2680 I32 parenfloor = scan->flags;
2682 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2684 cc.oldcc = PL_regcc;
2686 /* XXXX Probably it is better to teach regpush to support
2687 parenfloor > PL_regsize... */
2688 if (parenfloor > *PL_reglastparen)
2689 parenfloor = *PL_reglastparen; /* Pessimization... */
2690 cc.parenfloor = parenfloor;
2692 cc.min = ARG1(scan);
2693 cc.max = ARG2(scan);
2694 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2698 PL_reginput = locinput;
2699 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2701 PL_regcc = cc.oldcc;
2707 * This is really hard to understand, because after we match
2708 * what we're trying to match, we must make sure the rest of
2709 * the REx is going to match for sure, and to do that we have
2710 * to go back UP the parse tree by recursing ever deeper. And
2711 * if it fails, we have to reset our parent's current state
2712 * that we can try again after backing off.
2715 CHECKPOINT cp, lastcp;
2716 CURCUR* cc = PL_regcc;
2717 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2719 n = cc->cur + 1; /* how many we know we matched */
2720 PL_reginput = locinput;
2723 PerlIO_printf(Perl_debug_log,
2724 "%*s %ld out of %ld..%ld cc=%lx\n",
2725 REPORT_CODE_OFF+PL_regindent*2, "",
2726 (long)n, (long)cc->min,
2727 (long)cc->max, (long)cc)
2730 /* If degenerate scan matches "", assume scan done. */
2732 if (locinput == cc->lastloc && n >= cc->min) {
2733 PL_regcc = cc->oldcc;
2737 PerlIO_printf(Perl_debug_log,
2738 "%*s empty match detected, try continuation...\n",
2739 REPORT_CODE_OFF+PL_regindent*2, "")
2741 if (regmatch(cc->next))
2749 /* First just match a string of min scans. */
2753 cc->lastloc = locinput;
2754 if (regmatch(cc->scan))
2757 cc->lastloc = lastloc;
2762 /* Check whether we already were at this position.
2763 Postpone detection until we know the match is not
2764 *that* much linear. */
2765 if (!PL_reg_maxiter) {
2766 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2767 PL_reg_leftiter = PL_reg_maxiter;
2769 if (PL_reg_leftiter-- == 0) {
2770 I32 size = (PL_reg_maxiter + 7)/8;
2771 if (PL_reg_poscache) {
2772 if (PL_reg_poscache_size < size) {
2773 Renew(PL_reg_poscache, size, char);
2774 PL_reg_poscache_size = size;
2776 Zero(PL_reg_poscache, size, char);
2779 PL_reg_poscache_size = size;
2780 Newz(29, PL_reg_poscache, size, char);
2783 PerlIO_printf(Perl_debug_log,
2784 "%sDetected a super-linear match, switching on caching%s...\n",
2785 PL_colors[4], PL_colors[5])
2788 if (PL_reg_leftiter < 0) {
2789 I32 o = locinput - PL_bostr, b;
2791 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2794 if (PL_reg_poscache[o] & (1<<b)) {
2796 PerlIO_printf(Perl_debug_log,
2797 "%*s already tried at this position...\n",
2798 REPORT_CODE_OFF+PL_regindent*2, "")
2802 PL_reg_poscache[o] |= (1<<b);
2806 /* Prefer next over scan for minimal matching. */
2809 PL_regcc = cc->oldcc;
2812 cp = regcppush(cc->parenfloor);
2814 if (regmatch(cc->next)) {
2816 sayYES; /* All done. */
2818 REGCP_UNWIND(lastcp);
2824 if (n >= cc->max) { /* Maximum greed exceeded? */
2825 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2826 && !(PL_reg_flags & RF_warned)) {
2827 PL_reg_flags |= RF_warned;
2828 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2829 "Complex regular subexpression recursion",
2836 PerlIO_printf(Perl_debug_log,
2837 "%*s trying longer...\n",
2838 REPORT_CODE_OFF+PL_regindent*2, "")
2840 /* Try scanning more and see if it helps. */
2841 PL_reginput = locinput;
2843 cc->lastloc = locinput;
2844 cp = regcppush(cc->parenfloor);
2846 if (regmatch(cc->scan)) {
2850 REGCP_UNWIND(lastcp);
2853 cc->lastloc = lastloc;
2857 /* Prefer scan over next for maximal matching. */
2859 if (n < cc->max) { /* More greed allowed? */
2860 cp = regcppush(cc->parenfloor);
2862 cc->lastloc = locinput;
2864 if (regmatch(cc->scan)) {
2868 REGCP_UNWIND(lastcp);
2869 regcppop(); /* Restore some previous $<digit>s? */
2870 PL_reginput = locinput;
2872 PerlIO_printf(Perl_debug_log,
2873 "%*s failed, try continuation...\n",
2874 REPORT_CODE_OFF+PL_regindent*2, "")
2877 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2878 && !(PL_reg_flags & RF_warned)) {
2879 PL_reg_flags |= RF_warned;
2880 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2881 "Complex regular subexpression recursion",
2885 /* Failed deeper matches of scan, so see if this one works. */
2886 PL_regcc = cc->oldcc;
2889 if (regmatch(cc->next))
2895 cc->lastloc = lastloc;
2900 next = scan + ARG(scan);
2903 inner = NEXTOPER(NEXTOPER(scan));
2906 inner = NEXTOPER(scan);
2911 if (OP(next) != c1) /* No choice. */
2912 next = inner; /* Avoid recursion. */
2914 I32 lastparen = *PL_reglastparen;
2916 re_unwind_branch_t *uw;
2918 /* Put unwinding data on stack */
2919 unwind1 = SSNEWt(1,re_unwind_branch_t);
2920 uw = SSPTRt(unwind1,re_unwind_branch_t);
2923 uw->type = ((c1 == BRANCH)
2925 : RE_UNWIND_BRANCHJ);
2926 uw->lastparen = lastparen;
2928 uw->locinput = locinput;
2929 uw->nextchr = nextchr;
2931 uw->regindent = ++PL_regindent;
2934 REGCP_SET(uw->lastcp);
2936 /* Now go into the first branch */
2949 /* We suppose that the next guy does not need
2950 backtracking: in particular, it is of constant length,
2951 and has no parenths to influence future backrefs. */
2952 ln = ARG1(scan); /* min to match */
2953 n = ARG2(scan); /* max to match */
2954 paren = scan->flags;
2956 if (paren > PL_regsize)
2958 if (paren > *PL_reglastparen)
2959 *PL_reglastparen = paren;
2961 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2963 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2964 PL_reginput = locinput;
2967 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2969 if (ln && l == 0 && n >= ln
2970 /* In fact, this is tricky. If paren, then the
2971 fact that we did/didnot match may influence
2972 future execution. */
2973 && !(paren && ln == 0))
2975 locinput = PL_reginput;
2976 if (PL_regkind[(U8)OP(next)] == EXACT) {
2977 c1 = (U8)*STRING(next);
2978 if (OP(next) == EXACTF)
2980 else if (OP(next) == EXACTFL)
2981 c2 = PL_fold_locale[c1];
2988 /* This may be improved if l == 0. */
2989 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2990 /* If it could work, try it. */
2992 UCHARAT(PL_reginput) == c1 ||
2993 UCHARAT(PL_reginput) == c2)
2997 PL_regstartp[paren] =
2998 HOPc(PL_reginput, -l) - PL_bostr;
2999 PL_regendp[paren] = PL_reginput - PL_bostr;
3002 PL_regendp[paren] = -1;
3006 REGCP_UNWIND(lastcp);
3008 /* Couldn't or didn't -- move forward. */
3009 PL_reginput = locinput;
3010 if (regrepeat_hard(scan, 1, &l)) {
3012 locinput = PL_reginput;
3019 n = regrepeat_hard(scan, n, &l);
3020 if (n != 0 && l == 0
3021 /* In fact, this is tricky. If paren, then the
3022 fact that we did/didnot match may influence
3023 future execution. */
3024 && !(paren && ln == 0))
3026 locinput = PL_reginput;
3028 PerlIO_printf(Perl_debug_log,
3029 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3030 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3034 if (PL_regkind[(U8)OP(next)] == EXACT) {
3035 c1 = (U8)*STRING(next);
3036 if (OP(next) == EXACTF)
3038 else if (OP(next) == EXACTFL)
3039 c2 = PL_fold_locale[c1];
3048 /* If it could work, try it. */
3050 UCHARAT(PL_reginput) == c1 ||
3051 UCHARAT(PL_reginput) == c2)
3054 PerlIO_printf(Perl_debug_log,
3055 "%*s trying tail with n=%"IVdf"...\n",
3056 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3060 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3061 PL_regendp[paren] = PL_reginput - PL_bostr;
3064 PL_regendp[paren] = -1;
3068 REGCP_UNWIND(lastcp);
3070 /* Couldn't or didn't -- back up. */
3072 locinput = HOPc(locinput, -l);
3073 PL_reginput = locinput;
3080 paren = scan->flags; /* Which paren to set */
3081 if (paren > PL_regsize)
3083 if (paren > *PL_reglastparen)
3084 *PL_reglastparen = paren;
3085 ln = ARG1(scan); /* min to match */
3086 n = ARG2(scan); /* max to match */
3087 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3091 ln = ARG1(scan); /* min to match */
3092 n = ARG2(scan); /* max to match */
3093 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3098 scan = NEXTOPER(scan);
3104 scan = NEXTOPER(scan);
3108 * Lookahead to avoid useless match attempts
3109 * when we know what character comes next.
3111 if (PL_regkind[(U8)OP(next)] == EXACT) {
3112 c1 = (U8)*STRING(next);
3113 if (OP(next) == EXACTF)
3115 else if (OP(next) == EXACTFL)
3116 c2 = PL_fold_locale[c1];
3122 PL_reginput = locinput;
3126 if (ln && regrepeat(scan, ln) < ln)
3128 locinput = PL_reginput;
3131 char *e = locinput + n - ln; /* Should not check after this */
3132 char *old = locinput;
3134 if (e >= PL_regeol || (n == REG_INFTY))
3137 /* Find place 'next' could work */
3139 while (locinput <= e && *locinput != c1)
3142 while (locinput <= e
3149 /* PL_reginput == old now */
3150 if (locinput != old) {
3151 ln = 1; /* Did some */
3152 if (regrepeat(scan, locinput - old) <
3156 /* PL_reginput == locinput now */
3157 TRYPAREN(paren, ln, locinput);
3158 PL_reginput = locinput; /* Could be reset... */
3159 REGCP_UNWIND(lastcp);
3160 /* Couldn't or didn't -- move forward. */
3165 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3166 /* If it could work, try it. */
3168 UCHARAT(PL_reginput) == c1 ||
3169 UCHARAT(PL_reginput) == c2)
3171 TRYPAREN(paren, n, PL_reginput);
3172 REGCP_UNWIND(lastcp);
3174 /* Couldn't or didn't -- move forward. */
3175 PL_reginput = locinput;
3176 if (regrepeat(scan, 1)) {
3178 locinput = PL_reginput;
3186 n = regrepeat(scan, n);
3187 locinput = PL_reginput;
3188 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3189 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3190 ln = n; /* why back off? */
3191 /* ...because $ and \Z can match before *and* after
3192 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3193 We should back off by one in this case. */
3194 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3200 /* If it could work, try it. */
3202 UCHARAT(PL_reginput) == c1 ||
3203 UCHARAT(PL_reginput) == c2)
3205 TRYPAREN(paren, n, PL_reginput);
3206 REGCP_UNWIND(lastcp);
3208 /* Couldn't or didn't -- back up. */
3210 PL_reginput = locinput = HOPc(locinput, -1);
3215 /* If it could work, try it. */
3217 UCHARAT(PL_reginput) == c1 ||
3218 UCHARAT(PL_reginput) == c2)
3220 TRYPAREN(paren, n, PL_reginput);
3221 REGCP_UNWIND(lastcp);
3223 /* Couldn't or didn't -- back up. */
3225 PL_reginput = locinput = HOPc(locinput, -1);
3232 if (PL_reg_call_cc) {
3233 re_cc_state *cur_call_cc = PL_reg_call_cc;
3234 CURCUR *cctmp = PL_regcc;
3235 regexp *re = PL_reg_re;
3236 CHECKPOINT cp, lastcp;
3238 cp = regcppush(0); /* Save *all* the positions. */
3240 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3242 PL_reginput = locinput; /* Make position available to
3244 cache_re(PL_reg_call_cc->re);
3245 PL_regcc = PL_reg_call_cc->cc;
3246 PL_reg_call_cc = PL_reg_call_cc->prev;
3247 if (regmatch(cur_call_cc->node)) {
3248 PL_reg_call_cc = cur_call_cc;
3252 REGCP_UNWIND(lastcp);
3254 PL_reg_call_cc = cur_call_cc;
3260 PerlIO_printf(Perl_debug_log,
3261 "%*s continuation failed...\n",
3262 REPORT_CODE_OFF+PL_regindent*2, "")
3266 if (locinput < PL_regtill) {
3267 DEBUG_r(PerlIO_printf(Perl_debug_log,
3268 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3270 (long)(locinput - PL_reg_starttry),
3271 (long)(PL_regtill - PL_reg_starttry),
3273 sayNO_FINAL; /* Cannot match: too short. */
3275 PL_reginput = locinput; /* put where regtry can find it */
3276 sayYES_FINAL; /* Success! */
3278 PL_reginput = locinput; /* put where regtry can find it */
3279 sayYES_LOUD; /* Success! */
3282 PL_reginput = locinput;
3287 if (UTF) { /* XXXX This is absolutely
3288 broken, we read before
3290 s = HOPMAYBEc(locinput, -scan->flags);
3296 if (locinput < PL_bostr + scan->flags)
3298 PL_reginput = locinput - scan->flags;
3303 PL_reginput = locinput;
3308 if (UTF) { /* XXXX This is absolutely
3309 broken, we read before
3311 s = HOPMAYBEc(locinput, -scan->flags);
3312 if (!s || s < PL_bostr)
3317 if (locinput < PL_bostr + scan->flags)
3319 PL_reginput = locinput - scan->flags;
3324 PL_reginput = locinput;
3327 inner = NEXTOPER(NEXTOPER(scan));
3328 if (regmatch(inner) != n) {
3343 if (OP(scan) == SUSPEND) {
3344 locinput = PL_reginput;
3345 nextchr = UCHARAT(locinput);
3350 next = scan + ARG(scan);
3355 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3356 PTR2UV(scan), OP(scan));
3357 Perl_croak(aTHX_ "regexp memory corruption");
3364 * We get here only if there's trouble -- normally "case END" is
3365 * the terminating point.
3367 Perl_croak(aTHX_ "corrupted regexp pointers");
3373 PerlIO_printf(Perl_debug_log,
3374 "%*s %scould match...%s\n",
3375 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3379 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3380 PL_colors[4],PL_colors[5]));
3386 #if 0 /* Breaks $^R */
3394 PerlIO_printf(Perl_debug_log,
3395 "%*s %sfailed...%s\n",
3396 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3402 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3405 case RE_UNWIND_BRANCH:
3406 case RE_UNWIND_BRANCHJ:
3408 re_unwind_branch_t *uwb = &(uw->branch);
3409 I32 lastparen = uwb->lastparen;
3411 REGCP_UNWIND(uwb->lastcp);
3412 for (n = *PL_reglastparen; n > lastparen; n--)
3414 *PL_reglastparen = n;
3415 scan = next = uwb->next;
3417 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3418 ? BRANCH : BRANCHJ) ) { /* Failure */
3425 /* Have more choice yet. Reuse the same uwb. */
3427 if ((n = (uwb->type == RE_UNWIND_BRANCH
3428 ? NEXT_OFF(next) : ARG(next))))
3431 next = NULL; /* XXXX Needn't unwinding in this case... */
3433 next = NEXTOPER(scan);
3434 if (uwb->type == RE_UNWIND_BRANCHJ)
3435 next = NEXTOPER(next);
3436 locinput = uwb->locinput;
3437 nextchr = uwb->nextchr;
3439 PL_regindent = uwb->regindent;
3446 Perl_croak(aTHX_ "regexp unwind memory corruption");
3457 - regrepeat - repeatedly match something simple, report how many
3460 * [This routine now assumes that it will only match on things of length 1.
3461 * That was true before, but now we assume scan - reginput is the count,
3462 * rather than incrementing count on every character. [Er, except utf8.]]
3465 S_regrepeat(pTHX_ regnode *p, I32 max)
3468 register char *scan;
3470 register char *loceol = PL_regeol;
3471 register I32 hardcount = 0;
3474 if (max != REG_INFTY && max < loceol - scan)
3475 loceol = scan + max;
3478 while (scan < loceol && *scan != '\n')
3486 while (scan < loceol && *scan != '\n') {
3487 scan += UTF8SKIP(scan);
3493 while (scan < loceol) {
3494 scan += UTF8SKIP(scan);
3498 case EXACT: /* length of string is 1 */
3500 while (scan < loceol && UCHARAT(scan) == c)
3503 case EXACTF: /* length of string is 1 */
3505 while (scan < loceol &&
3506 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3509 case EXACTFL: /* length of string is 1 */
3510 PL_reg_flags |= RF_tainted;
3512 while (scan < loceol &&
3513 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3518 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3519 scan += UTF8SKIP(scan);
3524 while (scan < loceol && REGINCLASS(p, *scan))
3528 while (scan < loceol && isALNUM(*scan))
3533 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3534 scan += UTF8SKIP(scan);
3539 PL_reg_flags |= RF_tainted;
3540 while (scan < loceol && isALNUM_LC(*scan))
3544 PL_reg_flags |= RF_tainted;
3546 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3547 scan += UTF8SKIP(scan);
3553 while (scan < loceol && !isALNUM(*scan))
3558 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3559 scan += UTF8SKIP(scan);
3564 PL_reg_flags |= RF_tainted;
3565 while (scan < loceol && !isALNUM_LC(*scan))
3569 PL_reg_flags |= RF_tainted;
3571 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3572 scan += UTF8SKIP(scan);
3577 while (scan < loceol && isSPACE(*scan))
3582 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3583 scan += UTF8SKIP(scan);
3588 PL_reg_flags |= RF_tainted;
3589 while (scan < loceol && isSPACE_LC(*scan))
3593 PL_reg_flags |= RF_tainted;
3595 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3596 scan += UTF8SKIP(scan);
3601 while (scan < loceol && !isSPACE(*scan))
3606 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3607 scan += UTF8SKIP(scan);
3612 PL_reg_flags |= RF_tainted;
3613 while (scan < loceol && !isSPACE_LC(*scan))
3617 PL_reg_flags |= RF_tainted;
3619 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3620 scan += UTF8SKIP(scan);
3625 while (scan < loceol && isDIGIT(*scan))
3630 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3631 scan += UTF8SKIP(scan);
3637 while (scan < loceol && !isDIGIT(*scan))
3642 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3643 scan += UTF8SKIP(scan);
3647 default: /* Called on something of 0 width. */
3648 break; /* So match right here or not at all. */
3654 c = scan - PL_reginput;
3659 SV *prop = sv_newmortal();
3662 PerlIO_printf(Perl_debug_log,
3663 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3664 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3671 - regrepeat_hard - repeatedly match something, report total lenth and length
3673 * The repeater is supposed to have constant length.
3677 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3680 register char *scan;
3681 register char *start;
3682 register char *loceol = PL_regeol;
3684 I32 count = 0, res = 1;
3689 start = PL_reginput;
3691 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3694 while (start < PL_reginput) {
3696 start += UTF8SKIP(start);
3707 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3709 *lp = l = PL_reginput - start;
3710 if (max != REG_INFTY && l*max < loceol - scan)
3711 loceol = scan + l*max;
3724 - reginclass - determine if a character falls into a character class
3728 S_reginclass(pTHX_ register regnode *p, register I32 c)
3731 char flags = ANYOF_FLAGS(p);
3735 if (ANYOF_BITMAP_TEST(p, c))
3737 else if (flags & ANYOF_FOLD) {
3739 if (flags & ANYOF_LOCALE) {
3740 PL_reg_flags |= RF_tainted;
3741 cf = PL_fold_locale[c];
3745 if (ANYOF_BITMAP_TEST(p, cf))
3749 if (!match && (flags & ANYOF_CLASS)) {
3750 PL_reg_flags |= RF_tainted;
3752 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3753 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3754 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3755 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3756 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3757 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3758 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3759 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3760 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3761 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3762 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3763 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3764 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3765 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3766 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3767 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3768 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3769 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3770 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3771 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3772 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3773 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3774 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3775 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3776 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3777 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
3778 (ANYOF_CLASS_TEST(p, ANYOF_PSXSPC) && isPSXSPC(c)) ||
3779 (ANYOF_CLASS_TEST(p, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
3780 (ANYOF_CLASS_TEST(p, ANYOF_BLANK) && isBLANK(c)) ||
3781 (ANYOF_CLASS_TEST(p, ANYOF_NBLANK) && !isBLANK(c))
3782 ) /* How's that for a conditional? */
3788 return (flags & ANYOF_INVERT) ? !match : match;
3792 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3795 char flags = ARG1(f);
3798 SV *rv = (SV*)PL_regdata->data[ARG2(f)];
3799 AV *av = (AV*)SvRV((SV*)rv);
3800 SV *sw = *av_fetch(av, 0, FALSE);
3801 SV *lv = *av_fetch(av, 1, FALSE);
3803 SV *sw = (SV*)PL_regdata->data[ARG2(f)];
3806 if (swash_fetch(sw, p))
3808 else if (flags & ANYOF_FOLD) {
3809 U8 tmpbuf[UTF8_MAXLEN+1];
3810 if (flags & ANYOF_LOCALE) {
3811 PL_reg_flags |= RF_tainted;
3812 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3815 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3816 if (swash_fetch(sw, tmpbuf))
3820 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3822 return (flags & ANYOF_INVERT) ? !match : match;
3826 S_reghop(pTHX_ U8 *s, I32 off)
3830 while (off-- && s < (U8*)PL_regeol)
3835 if (s > (U8*)PL_bostr) {
3838 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3840 } /* XXX could check well-formedness here */
3848 S_reghopmaybe(pTHX_ U8* s, I32 off)
3852 while (off-- && s < (U8*)PL_regeol)
3859 if (s > (U8*)PL_bostr) {
3862 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3864 } /* XXX could check well-formedness here */
3880 restore_pos(pTHXo_ void *arg)
3883 if (PL_reg_eval_set) {
3884 if (PL_reg_oldsaved) {
3885 PL_reg_re->subbeg = PL_reg_oldsaved;
3886 PL_reg_re->sublen = PL_reg_oldsavedlen;
3887 RX_MATCH_COPIED_on(PL_reg_re);
3889 PL_reg_magic->mg_len = PL_reg_oldpos;
3890 PL_reg_eval_set = 0;
3891 PL_curpm = PL_reg_oldcurpm;