5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
43 # define PERL_NO_GET_CONTEXT
48 * pregcomp and pregexec -- regsub and regerror are not used in perl
50 * Copyright (c) 1986 by University of Toronto.
51 * Written by Henry Spencer. Not derived from licensed software.
53 * Permission is granted to anyone to use this software for any
54 * purpose on any computer system, and to redistribute it freely,
55 * subject to the following restrictions:
57 * 1. The author is not responsible for the consequences of use of
58 * this software, no matter how awful, even if they arise
61 * 2. The origin of this software must not be misrepresented, either
62 * by explicit claim or by omission.
64 * 3. Altered versions must be plainly marked as such, and must not
65 * be misrepresented as being the original software.
67 **** Alterations to Henry's code are...
69 **** Copyright (c) 1991-1999, Larry Wall
71 **** You may distribute under the terms of either the GNU General Public
72 **** License or the Artistic License, as specified in the README file.
74 * Beware that some of this code is subtly aware of the way operator
75 * precedence is structured in regular expressions. Serious changes in
76 * regular-expression syntax might require a total rethink.
79 #define PERL_IN_REGEXEC_C
82 #ifdef PERL_IN_XSUB_RE
83 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
90 #define RF_tainted 1 /* tainted information used? */
91 #define RF_warned 2 /* warned about big count? */
92 #define RF_evaled 4 /* Did an EVAL with setting? */
93 #define RF_utf8 8 /* String contains multibyte chars? */
95 #define UTF (PL_reg_flags & RF_utf8)
97 #define RS_init 1 /* eval environment created */
98 #define RS_set 2 /* replsv value is set */
101 #define STATIC static
108 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c) : ANYOF_BITMAP_TEST(p,c))
109 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
111 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
112 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
114 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
115 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
116 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
117 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
118 #define HOPc(pos,off) ((char*)HOP(pos,off))
119 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
121 static void restore_pos(pTHXo_ void *arg);
125 S_regcppush(pTHX_ I32 parenfloor)
128 int retval = PL_savestack_ix;
129 int i = (PL_regsize - parenfloor) * 4;
133 for (p = PL_regsize; p > parenfloor; p--) {
134 SSPUSHINT(PL_regendp[p]);
135 SSPUSHINT(PL_regstartp[p]);
136 SSPUSHPTR(PL_reg_start_tmp[p]);
139 SSPUSHINT(PL_regsize);
140 SSPUSHINT(*PL_reglastparen);
141 SSPUSHPTR(PL_reginput);
143 SSPUSHINT(SAVEt_REGCONTEXT);
147 /* These are needed since we do not localize EVAL nodes: */
148 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
149 " Setting an EVAL scope, savestack=%i\n", \
150 PL_savestack_ix)); lastcp = PL_savestack_ix
152 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
153 PerlIO_printf(Perl_debug_log, \
154 " Clearing an EVAL scope, savestack=%i..%i\n", \
155 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
165 assert(i == SAVEt_REGCONTEXT);
167 input = (char *) SSPOPPTR;
168 *PL_reglastparen = SSPOPINT;
169 PL_regsize = SSPOPINT;
170 for (i -= 3; i > 0; i -= 4) {
171 paren = (U32)SSPOPINT;
172 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
173 PL_regstartp[paren] = SSPOPINT;
175 if (paren <= *PL_reglastparen)
176 PL_regendp[paren] = tmps;
178 PerlIO_printf(Perl_debug_log,
179 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
180 (UV)paren, (IV)PL_regstartp[paren],
181 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
182 (IV)PL_regendp[paren],
183 (paren > *PL_reglastparen ? "(no)" : ""));
187 if (*PL_reglastparen + 1 <= PL_regnpar) {
188 PerlIO_printf(Perl_debug_log,
189 " restoring \\%d..\\%d to undef\n",
190 *PL_reglastparen + 1, PL_regnpar);
193 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
194 if (paren > PL_regsize)
195 PL_regstartp[paren] = -1;
196 PL_regendp[paren] = -1;
202 S_regcp_set_to(pTHX_ I32 ss)
205 I32 tmp = PL_savestack_ix;
207 PL_savestack_ix = ss;
209 PL_savestack_ix = tmp;
213 typedef struct re_cc_state
217 struct re_cc_state *prev;
222 #define regcpblow(cp) LEAVE_SCOPE(cp)
225 * pregexec and friends
229 - pregexec - match a regexp against a string
232 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
233 char *strbeg, I32 minend, SV *screamer, U32 nosave)
234 /* strend: pointer to null at end of string */
235 /* strbeg: real beginning of string */
236 /* minend: end of match must be >=minend after stringarg. */
237 /* nosave: For optimizations. */
240 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
241 nosave ? 0 : REXEC_COPY_STR);
245 S_cache_re(pTHX_ regexp *prog)
248 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
250 PL_regprogram = prog->program;
252 PL_regnpar = prog->nparens;
253 PL_regdata = prog->data;
258 * Need to implement the following flags for reg_anch:
260 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
262 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
263 * INTUIT_AUTORITATIVE_ML
264 * INTUIT_ONCE_NOML - Intuit can match in one location only.
267 * Another flag for this function: SECOND_TIME (so that float substrs
268 * with giant delta may be not rechecked).
271 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
273 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
274 Otherwise, only SvCUR(sv) is used to get strbeg. */
276 /* XXXX We assume that strpos is strbeg unless sv. */
278 /* A failure to find a constant substring means that there is no need to make
279 an expensive call to REx engine, thus we celebrate a failure. Similarly,
280 finding a substring too deep into the string means that less calls to
281 regtry() should be needed.
283 REx compiler's optimizer found 4 possible hints:
284 a) Anchored substring;
286 c) Whether we are anchored (beginning-of-line or \G);
287 d) First node (of those at offset 0) which may distingush positions;
288 We use 'a', 'b', multiline-part of 'c', and try to find a position in the
289 string which does not contradict any of them.
293 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
294 char *strend, U32 flags, re_scream_pos_data *data)
296 register I32 start_shift;
297 /* Should be nonnegative! */
298 register I32 end_shift;
304 register char *other_last = Nullch;
306 char *i_strpos = strpos;
309 DEBUG_r( if (!PL_colorset) reginitcolors() );
310 DEBUG_r(PerlIO_printf(Perl_debug_log,
311 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
312 PL_colors[4],PL_colors[5],PL_colors[0],
315 (strlen(prog->precomp) > 60 ? "..." : ""),
317 (int)(strend - strpos > 60 ? 60 : strend - strpos),
318 strpos, PL_colors[1],
319 (strend - strpos > 60 ? "..." : ""))
322 if (prog->minlen > strend - strpos) {
323 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
326 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
327 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
328 || ( (prog->reganch & ROPT_ANCH_BOL)
329 && !PL_multiline ) ); /* Check after \n? */
331 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
332 /* Substring at constant offset from beg-of-str... */
335 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
336 && (sv && (strpos + SvCUR(sv) != strend)) ) {
337 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
340 PL_regeol = strend; /* Used in HOP() */
341 s = HOPc(strpos, prog->check_offset_min);
342 if (SvTAIL(prog->check_substr)) {
343 slen = SvCUR(prog->check_substr); /* >= 1 */
345 if ( strend - s > slen || strend - s < slen - 1
346 || (strend - s == slen && strend[-1] != '\n')) {
347 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
350 /* Now should match s[0..slen-2] */
352 if (slen && (*SvPVX(prog->check_substr) != *s
354 && memNE(SvPVX(prog->check_substr), s, slen)))) {
356 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
360 else if (*SvPVX(prog->check_substr) != *s
361 || ((slen = SvCUR(prog->check_substr)) > 1
362 && memNE(SvPVX(prog->check_substr), s, slen)))
364 goto success_at_start;
366 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
368 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
369 /* Should be nonnegative! */
370 end_shift = prog->minlen - start_shift -
371 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
373 I32 end = prog->check_offset_max + CHR_SVLEN(prog->check_substr)
374 - (SvTAIL(prog->check_substr) != 0);
375 I32 eshift = strend - s - end;
377 if (end_shift < eshift)
381 else { /* Can match at random position */
384 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
385 /* Should be nonnegative! */
386 end_shift = prog->minlen - start_shift -
387 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
390 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
392 Perl_croak(aTHX_ "panic: end_shift");
395 check = prog->check_substr;
397 /* Find a possible match in the region s..strend by looking for
398 the "check" substring in the region corrected by start/end_shift. */
399 if (flags & REXEC_SCREAM) {
400 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
401 I32 p = -1; /* Internal iterator of scream. */
402 I32 *pp = data ? data->scream_pos : &p;
404 if (PL_screamfirst[BmRARE(check)] >= 0
405 || ( BmRARE(check) == '\n'
406 && (BmPREVIOUS(check) == SvCUR(check) - 1)
408 s = screaminstr(sv, check,
409 start_shift + (s - strbeg), end_shift, pp, 0);
413 *data->scream_olds = s;
416 s = fbm_instr((unsigned char*)s + start_shift,
417 (unsigned char*)strend - end_shift,
418 check, PL_multiline ? FBMrf_MULTILINE : 0);
420 /* Update the count-of-usability, remove useless subpatterns,
423 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
424 (s ? "Found" : "Did not find"),
425 ((check == prog->anchored_substr) ? "anchored" : "floating"),
427 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
429 PL_colors[1], (SvTAIL(check) ? "$" : ""),
430 (s ? " at offset " : "...\n") ) );
435 /* Finish the diagnostic message */
436 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
438 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
439 Start with the other substr.
440 XXXX no SCREAM optimization yet - and a very coarse implementation
441 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
442 *always* match. Probably should be marked during compile...
443 Probably it is right to do no SCREAM here...
446 if (prog->float_substr && prog->anchored_substr) {
447 /* Take into account the "other" substring. */
448 /* XXXX May be hopelessly wrong for UTF... */
450 other_last = strpos - 1;
451 if (check == prog->float_substr) {
454 char *last = s - start_shift, *last1, *last2;
458 t = s - prog->check_offset_max;
459 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
460 && (!(prog->reganch & ROPT_UTF8)
461 || (PL_bostr = strpos, /* Used in regcopmaybe() */
462 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
467 t += prog->anchored_offset;
471 last2 = last1 = strend - prog->minlen;
474 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
475 /* On end-of-str: see comment below. */
476 s = fbm_instr((unsigned char*)t,
477 (unsigned char*)last1 + prog->anchored_offset
478 + SvCUR(prog->anchored_substr)
479 - (SvTAIL(prog->anchored_substr)!=0),
480 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
481 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
482 (s ? "Found" : "Contradicts"),
484 (int)(SvCUR(prog->anchored_substr)
485 - (SvTAIL(prog->anchored_substr)!=0)),
486 SvPVX(prog->anchored_substr),
487 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
489 if (last1 >= last2) {
490 DEBUG_r(PerlIO_printf(Perl_debug_log,
491 ", giving up...\n"));
494 DEBUG_r(PerlIO_printf(Perl_debug_log,
495 ", trying floating at offset %ld...\n",
496 (long)(s1 + 1 - i_strpos)));
497 PL_regeol = strend; /* Used in HOP() */
498 other_last = last1 + prog->anchored_offset;
503 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
504 (long)(s - i_strpos)));
505 t = s - prog->anchored_offset;
514 else { /* Take into account the floating substring. */
519 last1 = last = strend - prog->minlen + prog->float_min_offset;
520 if (last - t > prog->float_max_offset)
521 last = t + prog->float_max_offset;
522 s = t + prog->float_min_offset;
525 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
526 /* fbm_instr() takes into account exact value of end-of-str
527 if the check is SvTAIL(ed). Since false positives are OK,
528 and end-of-str is not later than strend we are OK. */
529 s = fbm_instr((unsigned char*)s,
530 (unsigned char*)last + SvCUR(prog->float_substr)
531 - (SvTAIL(prog->float_substr)!=0),
532 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
533 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
534 (s ? "Found" : "Contradicts"),
536 (int)(SvCUR(prog->float_substr)
537 - (SvTAIL(prog->float_substr)!=0)),
538 SvPVX(prog->float_substr),
539 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
542 DEBUG_r(PerlIO_printf(Perl_debug_log,
543 ", giving up...\n"));
546 DEBUG_r(PerlIO_printf(Perl_debug_log,
547 ", trying anchored starting at offset %ld...\n",
548 (long)(s1 + 1 - i_strpos)));
550 PL_regeol = strend; /* Used in HOP() */
555 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
556 (long)(s - i_strpos)));
566 t = s - prog->check_offset_max;
568 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
569 && (!(prog->reganch & ROPT_UTF8)
570 || (PL_bostr = strpos, /* Used in regcopmaybe() */
571 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
574 /* Fixed substring is found far enough so that the match
575 cannot start at strpos. */
577 if (ml_anch && t[-1] != '\n') {
578 /* Eventually fbm_*() should handle this, but often
579 anchored_offset is not 0, so this check will not be wasted. */
580 /* XXXX In the code below we prefer to look for "^" even in
581 presence of anchored substrings. And we search even
582 beyond the found float position. These pessimizations
583 are historical artefacts only. */
585 while (t < strend - prog->minlen) {
587 if (t < s - prog->check_offset_min) {
588 if (prog->anchored_substr) {
589 /* We definitely contradict the found anchored
590 substr. Due to the above check we do not
591 contradict "check" substr.
592 Thus we can arrive here only if check substr
593 is float. Redo checking for "other"=="fixed".
596 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
597 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
598 goto do_other_anchored;
601 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
602 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
605 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting at offset %ld...\n",
606 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
612 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
613 PL_colors[0],PL_colors[1]));
618 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
622 /* The found string does not prohibit matching at beg-of-str
623 - no optimization of calling REx engine can be performed,
624 unless it was an MBOL and we are not after MBOL. */
626 /* Even in this situation we may use MBOL flag if strpos is offset
627 wrt the start of the string. */
629 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
633 DEBUG_r( if (ml_anch)
634 PerlIO_printf(Perl_debug_log, "Does not contradict /%s^%s/m...\n",
635 PL_colors[0],PL_colors[1]);
638 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
639 && --BmUSEFUL(prog->check_substr) < 0
640 && prog->check_substr == prog->float_substr) { /* boo */
641 /* If flags & SOMETHING - do not do it many times on the same match */
642 SvREFCNT_dec(prog->check_substr);
643 prog->check_substr = Nullsv; /* disable */
644 prog->float_substr = Nullsv; /* clear */
646 prog->reganch &= ~RE_USE_INTUIT;
652 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sGuessed:%s match at offset %ld\n",
653 PL_colors[4], PL_colors[5], (long)(s - i_strpos)) );
656 fail_finish: /* Substring not found */
657 BmUSEFUL(prog->check_substr) += 5; /* hooray */
659 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
660 PL_colors[4],PL_colors[5]));
665 - regexec_flags - match a regexp against a string
668 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
669 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
670 /* strend: pointer to null at end of string */
671 /* strbeg: real beginning of string */
672 /* minend: end of match must be >=minend after stringarg. */
673 /* data: May be used for some additional optimizations. */
674 /* nosave: For optimizations. */
679 register char *startpos = stringarg;
681 I32 minlen; /* must match at least this many chars */
682 I32 dontbother = 0; /* how many characters not to try at end */
683 I32 start_shift = 0; /* Offset of the start to find
684 constant substr. */ /* CC */
685 I32 end_shift = 0; /* Same for the end. */ /* CC */
686 I32 scream_pos = -1; /* Internal iterator of scream. */
688 SV* oreplsv = GvSV(PL_replgv);
694 PL_regnarrate = PL_debug & 512;
698 if (prog == NULL || startpos == NULL) {
699 Perl_croak(aTHX_ "NULL regexp parameter");
703 minlen = prog->minlen;
704 if (strend - startpos < minlen) goto phooey;
706 if (startpos == strbeg) /* is ^ valid at stringarg? */
709 PL_regprev = (U32)stringarg[-1];
710 if (!PL_multiline && PL_regprev == '\n')
711 PL_regprev = '\0'; /* force ^ to NOT match */
714 /* Check validity of program. */
715 if (UCHARAT(prog->program) != REG_MAGIC) {
716 Perl_croak(aTHX_ "corrupted regexp program");
723 if (prog->reganch & ROPT_UTF8)
724 PL_reg_flags |= RF_utf8;
726 /* Mark beginning of line for ^ and lookbehind. */
727 PL_regbol = startpos;
731 /* Mark end of line for $ (and such) */
734 /* see how far we have to get to not match where we matched before */
735 PL_regtill = startpos+minend;
737 /* We start without call_cc context. */
740 /* If there is a "must appear" string, look for it. */
743 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
746 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
747 PL_reg_ganch = startpos;
748 else if (sv && SvTYPE(sv) >= SVt_PVMG
750 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
751 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
752 if (prog->reganch & ROPT_ANCH_GPOS) {
753 if (s > PL_reg_ganch)
758 else /* pos() not defined */
759 PL_reg_ganch = strbeg;
762 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
763 re_scream_pos_data d;
765 d.scream_olds = &scream_olds;
766 d.scream_pos = &scream_pos;
767 s = re_intuit_start(prog, sv, s, strend, flags, &d);
769 goto phooey; /* not present */
772 DEBUG_r( if (!PL_colorset) reginitcolors() );
773 DEBUG_r(PerlIO_printf(Perl_debug_log,
774 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
775 PL_colors[4],PL_colors[5],PL_colors[0],
778 (strlen(prog->precomp) > 60 ? "..." : ""),
780 (int)(strend - startpos > 60 ? 60 : strend - startpos),
781 startpos, PL_colors[1],
782 (strend - startpos > 60 ? "..." : ""))
785 /* Simplest case: anchored match need be tried only once. */
786 /* [unless only anchor is BOL and multiline is set] */
787 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
788 if (s == startpos && regtry(prog, startpos))
790 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
791 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
796 dontbother = minlen - 1;
797 end = HOPc(strend, -dontbother) - 1;
798 /* for multiline we only have to try after newlines */
799 if (prog->check_substr) {
808 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
816 if (*s++ == '\n') { /* don't need PL_utf8skip here */
824 } else if (prog->reganch & ROPT_ANCH_GPOS) {
825 if (regtry(prog, PL_reg_ganch))
830 /* Messy cases: unanchored match. */
831 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
832 /* we have /x+whatever/ */
833 /* it must be a one character string (XXXX Except UTF?) */
834 char ch = SvPVX(prog->anchored_substr)[0];
838 if (regtry(prog, s)) goto got_it;
840 while (s < strend && *s == ch)
849 if (regtry(prog, s)) goto got_it;
851 while (s < strend && *s == ch)
859 else if (prog->anchored_substr != Nullsv
860 || (prog->float_substr != Nullsv
861 && prog->float_max_offset < strend - s)) {
862 SV *must = prog->anchored_substr
863 ? prog->anchored_substr : prog->float_substr;
865 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
867 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
868 I32 delta = back_max - back_min;
869 char *last = HOPc(strend, /* Cannot start after this */
870 -(I32)(CHR_SVLEN(must)
871 - (SvTAIL(must) != 0) + back_min));
872 char *last1; /* Last position checked before */
877 last1 = s - 1; /* bogus */
879 /* XXXX check_substr already used to find `s', can optimize if
880 check_substr==must. */
882 dontbother = end_shift;
883 strend = HOPc(strend, -dontbother);
884 while ( (s <= last) &&
885 ((flags & REXEC_SCREAM)
886 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
887 end_shift, &scream_pos, 0))
888 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
889 (unsigned char*)strend, must,
890 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
891 if (HOPc(s, -back_max) > last1) {
892 last1 = HOPc(s, -back_min);
893 s = HOPc(s, -back_max);
896 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
898 last1 = HOPc(s, -back_min);
918 else if (c = prog->regstclass) {
919 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
928 dontbother = minlen - 1;
929 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
931 /* We know what class it must start with. */
936 if (REGINCLASSUTF8(c, (U8*)s)) {
937 if (tmp && regtry(prog, s))
950 if (REGINCLASS(cc, *s)) {
951 if (tmp && regtry(prog, s))
971 c2 = PL_fold_locale[c1];
975 /* Here it is NOT UTF! */
979 && (ln == 1 || (OP(c) == EXACTF
981 : ibcmp_locale(s, m, ln)))
988 if ( (*s == c1 || *s == c2)
989 && (ln == 1 || (OP(c) == EXACTF
991 : ibcmp_locale(s, m, ln)))
999 PL_reg_flags |= RF_tainted;
1006 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
1007 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1008 while (s < strend) {
1009 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1011 if (regtry(prog, s))
1016 if ((minlen || tmp) && regtry(prog,s))
1020 PL_reg_flags |= RF_tainted;
1025 strend = reghop_c(strend, -1);
1027 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
1028 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
1029 while (s < strend) {
1030 if (tmp == !(OP(c) == BOUND ?
1031 swash_fetch(PL_utf8_alnum, (U8*)s) :
1032 isALNUM_LC_utf8((U8*)s)))
1035 if (regtry(prog, s))
1040 if ((minlen || tmp) && regtry(prog,s))
1044 PL_reg_flags |= RF_tainted;
1051 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
1052 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1053 while (s < strend) {
1054 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1056 else if (regtry(prog, s))
1060 if ((minlen || !tmp) && regtry(prog,s))
1064 PL_reg_flags |= RF_tainted;
1069 strend = reghop_c(strend, -1);
1071 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
1072 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
1073 while (s < strend) {
1074 if (tmp == !(OP(c) == NBOUND ?
1075 swash_fetch(PL_utf8_alnum, (U8*)s) :
1076 isALNUM_LC_utf8((U8*)s)))
1078 else if (regtry(prog, s))
1082 if ((minlen || !tmp) && regtry(prog,s))
1086 while (s < strend) {
1088 if (tmp && regtry(prog, s))
1099 while (s < strend) {
1100 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1101 if (tmp && regtry(prog, s))
1112 PL_reg_flags |= RF_tainted;
1113 while (s < strend) {
1114 if (isALNUM_LC(*s)) {
1115 if (tmp && regtry(prog, s))
1126 PL_reg_flags |= RF_tainted;
1127 while (s < strend) {
1128 if (isALNUM_LC_utf8((U8*)s)) {
1129 if (tmp && regtry(prog, s))
1140 while (s < strend) {
1142 if (tmp && regtry(prog, s))
1153 while (s < strend) {
1154 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1155 if (tmp && regtry(prog, s))
1166 PL_reg_flags |= RF_tainted;
1167 while (s < strend) {
1168 if (!isALNUM_LC(*s)) {
1169 if (tmp && regtry(prog, s))
1180 PL_reg_flags |= RF_tainted;
1181 while (s < strend) {
1182 if (!isALNUM_LC_utf8((U8*)s)) {
1183 if (tmp && regtry(prog, s))
1194 while (s < strend) {
1196 if (tmp && regtry(prog, s))
1207 while (s < strend) {
1208 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1209 if (tmp && regtry(prog, s))
1220 PL_reg_flags |= RF_tainted;
1221 while (s < strend) {
1222 if (isSPACE_LC(*s)) {
1223 if (tmp && regtry(prog, s))
1234 PL_reg_flags |= RF_tainted;
1235 while (s < strend) {
1236 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1237 if (tmp && regtry(prog, s))
1248 while (s < strend) {
1250 if (tmp && regtry(prog, s))
1261 while (s < strend) {
1262 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1263 if (tmp && regtry(prog, s))
1274 PL_reg_flags |= RF_tainted;
1275 while (s < strend) {
1276 if (!isSPACE_LC(*s)) {
1277 if (tmp && regtry(prog, s))
1288 PL_reg_flags |= RF_tainted;
1289 while (s < strend) {
1290 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1291 if (tmp && regtry(prog, s))
1302 while (s < strend) {
1304 if (tmp && regtry(prog, s))
1315 while (s < strend) {
1316 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1317 if (tmp && regtry(prog, s))
1328 PL_reg_flags |= RF_tainted;
1329 while (s < strend) {
1330 if (isDIGIT_LC(*s)) {
1331 if (tmp && regtry(prog, s))
1342 PL_reg_flags |= RF_tainted;
1343 while (s < strend) {
1344 if (isDIGIT_LC_utf8((U8*)s)) {
1345 if (tmp && regtry(prog, s))
1356 while (s < strend) {
1358 if (tmp && regtry(prog, s))
1369 while (s < strend) {
1370 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1371 if (tmp && regtry(prog, s))
1382 PL_reg_flags |= RF_tainted;
1383 while (s < strend) {
1384 if (!isDIGIT_LC(*s)) {
1385 if (tmp && regtry(prog, s))
1396 PL_reg_flags |= RF_tainted;
1397 while (s < strend) {
1398 if (!isDIGIT_LC_utf8((U8*)s)) {
1399 if (tmp && regtry(prog, s))
1410 croak("panic: unknown regstclass %d", (int)OP(c));
1416 if (prog->float_substr != Nullsv) { /* Trim the end. */
1418 I32 oldpos = scream_pos;
1420 if (flags & REXEC_SCREAM) {
1421 last = screaminstr(sv, prog->float_substr, s - strbeg,
1422 end_shift, &scream_pos, 1); /* last one */
1424 last = scream_olds; /* Only one occurence. */
1428 char *little = SvPV(prog->float_substr, len);
1430 if (SvTAIL(prog->float_substr)) {
1431 if (memEQ(strend - len + 1, little, len - 1))
1432 last = strend - len + 1;
1433 else if (!PL_multiline)
1434 last = memEQ(strend - len, little, len)
1435 ? strend - len : Nullch;
1441 last = rninstr(s, strend, little, little + len);
1443 last = strend; /* matching `$' */
1446 if (last == NULL) goto phooey; /* Should not happen! */
1447 dontbother = strend - last + prog->float_min_offset;
1449 if (minlen && (dontbother < minlen))
1450 dontbother = minlen - 1;
1451 strend -= dontbother; /* this one's always in bytes! */
1452 /* We don't know much -- general case. */
1455 if (regtry(prog, s))
1464 if (regtry(prog, s))
1466 } while (s++ < strend);
1474 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1476 if (PL_reg_eval_set) {
1477 /* Preserve the current value of $^R */
1478 if (oreplsv != GvSV(PL_replgv))
1479 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1480 restored, the value remains
1482 restore_pos(aTHXo_ 0);
1485 /* make sure $`, $&, $', and $digit will work later */
1486 if ( !(flags & REXEC_NOT_FIRST) ) {
1487 if (RX_MATCH_COPIED(prog)) {
1488 Safefree(prog->subbeg);
1489 RX_MATCH_COPIED_off(prog);
1491 if (flags & REXEC_COPY_STR) {
1492 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1494 s = savepvn(strbeg, i);
1497 RX_MATCH_COPIED_on(prog);
1500 prog->subbeg = strbeg;
1501 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1508 if (PL_reg_eval_set)
1509 restore_pos(aTHXo_ 0);
1514 - regtry - try match at specific point
1516 STATIC I32 /* 0 failure, 1 success */
1517 S_regtry(pTHX_ regexp *prog, char *startpos)
1525 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1528 PL_reg_eval_set = RS_init;
1530 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1531 (IV)(PL_stack_sp - PL_stack_base));
1533 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1534 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1535 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1537 /* Apparently this is not needed, judging by wantarray. */
1538 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1539 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1542 /* Make $_ available to executed code. */
1543 if (PL_reg_sv != DEFSV) {
1544 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1549 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1550 && (mg = mg_find(PL_reg_sv, 'g')))) {
1551 /* prepare for quick setting of pos */
1552 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1553 mg = mg_find(PL_reg_sv, 'g');
1557 PL_reg_oldpos = mg->mg_len;
1558 SAVEDESTRUCTOR_X(restore_pos, 0);
1561 New(22,PL_reg_curpm, 1, PMOP);
1562 PL_reg_curpm->op_pmregexp = prog;
1563 PL_reg_oldcurpm = PL_curpm;
1564 PL_curpm = PL_reg_curpm;
1565 if (RX_MATCH_COPIED(prog)) {
1566 /* Here is a serious problem: we cannot rewrite subbeg,
1567 since it may be needed if this match fails. Thus
1568 $` inside (?{}) could fail... */
1569 PL_reg_oldsaved = prog->subbeg;
1570 PL_reg_oldsavedlen = prog->sublen;
1571 RX_MATCH_COPIED_off(prog);
1574 PL_reg_oldsaved = Nullch;
1575 prog->subbeg = PL_bostr;
1576 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1578 prog->startp[0] = startpos - PL_bostr;
1579 PL_reginput = startpos;
1580 PL_regstartp = prog->startp;
1581 PL_regendp = prog->endp;
1582 PL_reglastparen = &prog->lastparen;
1583 prog->lastparen = 0;
1585 DEBUG_r(PL_reg_starttry = startpos);
1586 if (PL_reg_start_tmpl <= prog->nparens) {
1587 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1588 if(PL_reg_start_tmp)
1589 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1591 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1594 /* XXXX What this code is doing here?!!! There should be no need
1595 to do this again and again, PL_reglastparen should take care of
1599 if (prog->nparens) {
1600 for (i = prog->nparens; i >= 1; i--) {
1606 if (regmatch(prog->program + 1)) {
1607 prog->endp[0] = PL_reginput - PL_bostr;
1615 - regmatch - main matching routine
1617 * Conceptually the strategy is simple: check to see whether the current
1618 * node matches, call self recursively to see whether the rest matches,
1619 * and then act accordingly. In practice we make some effort to avoid
1620 * recursion, in particular by going through "ordinary" nodes (that don't
1621 * need to know whether the rest of the match failed) by a loop instead of
1624 /* [lwall] I've hoisted the register declarations to the outer block in order to
1625 * maybe save a little bit of pushing and popping on the stack. It also takes
1626 * advantage of machines that use a register save mask on subroutine entry.
1628 STATIC I32 /* 0 failure, 1 success */
1629 S_regmatch(pTHX_ regnode *prog)
1632 register regnode *scan; /* Current node. */
1633 regnode *next; /* Next node. */
1634 regnode *inner; /* Next node in internal branch. */
1635 register I32 nextchr; /* renamed nextchr - nextchar colides with
1636 function of same name */
1637 register I32 n; /* no or next */
1638 register I32 ln; /* len or last */
1639 register char *s; /* operand or save */
1640 register char *locinput = PL_reginput;
1641 register I32 c1, c2, paren; /* case fold search, parenth */
1642 int minmod = 0, sw = 0, logical = 0;
1647 /* Note that nextchr is a byte even in UTF */
1648 nextchr = UCHARAT(locinput);
1650 while (scan != NULL) {
1651 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1653 # define sayYES goto yes
1654 # define sayNO goto no
1655 # define sayYES_FINAL goto yes_final
1656 # define sayYES_LOUD goto yes_loud
1657 # define sayNO_FINAL goto no_final
1658 # define sayNO_SILENT goto do_no
1659 # define saySAME(x) if (x) goto yes; else goto no
1660 # define REPORT_CODE_OFF 24
1662 # define sayYES return 1
1663 # define sayNO return 0
1664 # define sayYES_FINAL return 1
1665 # define sayYES_LOUD return 1
1666 # define sayNO_FINAL return 0
1667 # define sayNO_SILENT return 0
1668 # define saySAME(x) return x
1671 SV *prop = sv_newmortal();
1672 int docolor = *PL_colors[0];
1673 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1674 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1675 /* The part of the string before starttry has one color
1676 (pref0_len chars), between starttry and current
1677 position another one (pref_len - pref0_len chars),
1678 after the current position the third one.
1679 We assume that pref0_len <= pref_len, otherwise we
1680 decrease pref0_len. */
1681 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1682 ? (5 + taill) - l : locinput - PL_bostr);
1683 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1685 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1686 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1687 ? (5 + taill) - pref_len : PL_regeol - locinput);
1690 if (pref0_len > pref_len)
1691 pref0_len = pref_len;
1692 regprop(prop, scan);
1693 PerlIO_printf(Perl_debug_log,
1694 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1695 (IV)(locinput - PL_bostr),
1696 PL_colors[4], pref0_len,
1697 locinput - pref_len, PL_colors[5],
1698 PL_colors[2], pref_len - pref0_len,
1699 locinput - pref_len + pref0_len, PL_colors[3],
1700 (docolor ? "" : "> <"),
1701 PL_colors[0], l, locinput, PL_colors[1],
1702 15 - l - pref_len + 1,
1704 (IV)(scan - PL_regprogram), PL_regindent*2, "",
1708 next = scan + NEXT_OFF(scan);
1714 if (locinput == PL_bostr
1715 ? PL_regprev == '\n'
1717 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1719 /* regtill = regbol; */
1724 if (locinput == PL_bostr
1725 ? PL_regprev == '\n'
1726 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1732 if (locinput == PL_regbol && PL_regprev == '\n')
1736 if (locinput == PL_reg_ganch)
1746 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1751 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1753 if (PL_regeol - locinput > 1)
1757 if (PL_regeol != locinput)
1761 if (nextchr & 0x80) {
1762 locinput += PL_utf8skip[nextchr];
1763 if (locinput > PL_regeol)
1765 nextchr = UCHARAT(locinput);
1768 if (!nextchr && locinput >= PL_regeol)
1770 nextchr = UCHARAT(++locinput);
1773 if (!nextchr && locinput >= PL_regeol)
1775 nextchr = UCHARAT(++locinput);
1778 if (nextchr & 0x80) {
1779 locinput += PL_utf8skip[nextchr];
1780 if (locinput > PL_regeol)
1782 nextchr = UCHARAT(locinput);
1785 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1787 nextchr = UCHARAT(++locinput);
1790 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1792 nextchr = UCHARAT(++locinput);
1797 /* Inline the first character, for speed. */
1798 if (UCHARAT(s) != nextchr)
1800 if (PL_regeol - locinput < ln)
1802 if (ln > 1 && memNE(s, locinput, ln))
1805 nextchr = UCHARAT(locinput);
1808 PL_reg_flags |= RF_tainted;
1817 c1 = OP(scan) == EXACTF;
1821 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1822 toLOWER_utf8((U8*)l) :
1823 toLOWER_LC_utf8((U8*)l)))
1831 nextchr = UCHARAT(locinput);
1835 /* Inline the first character, for speed. */
1836 if (UCHARAT(s) != nextchr &&
1837 UCHARAT(s) != ((OP(scan) == EXACTF)
1838 ? PL_fold : PL_fold_locale)[nextchr])
1840 if (PL_regeol - locinput < ln)
1842 if (ln > 1 && (OP(scan) == EXACTF
1843 ? ibcmp(s, locinput, ln)
1844 : ibcmp_locale(s, locinput, ln)))
1847 nextchr = UCHARAT(locinput);
1851 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1853 if (locinput >= PL_regeol)
1855 locinput += PL_utf8skip[nextchr];
1856 nextchr = UCHARAT(locinput);
1861 nextchr = UCHARAT(locinput);
1862 if (!REGINCLASS(s, nextchr))
1864 if (!nextchr && locinput >= PL_regeol)
1866 nextchr = UCHARAT(++locinput);
1869 PL_reg_flags |= RF_tainted;
1874 if (!(OP(scan) == ALNUM
1875 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1877 nextchr = UCHARAT(++locinput);
1880 PL_reg_flags |= RF_tainted;
1885 if (nextchr & 0x80) {
1886 if (!(OP(scan) == ALNUMUTF8
1887 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1888 : isALNUM_LC_utf8((U8*)locinput)))
1892 locinput += PL_utf8skip[nextchr];
1893 nextchr = UCHARAT(locinput);
1896 if (!(OP(scan) == ALNUMUTF8
1897 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1899 nextchr = UCHARAT(++locinput);
1902 PL_reg_flags |= RF_tainted;
1905 if (!nextchr && locinput >= PL_regeol)
1907 if (OP(scan) == NALNUM
1908 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1910 nextchr = UCHARAT(++locinput);
1913 PL_reg_flags |= RF_tainted;
1916 if (!nextchr && locinput >= PL_regeol)
1918 if (nextchr & 0x80) {
1919 if (OP(scan) == NALNUMUTF8
1920 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1921 : isALNUM_LC_utf8((U8*)locinput))
1925 locinput += PL_utf8skip[nextchr];
1926 nextchr = UCHARAT(locinput);
1929 if (OP(scan) == NALNUMUTF8
1930 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1932 nextchr = UCHARAT(++locinput);
1936 PL_reg_flags |= RF_tainted;
1940 /* was last char in word? */
1941 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1942 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1944 n = isALNUM(nextchr);
1947 ln = isALNUM_LC(ln);
1948 n = isALNUM_LC(nextchr);
1950 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1955 PL_reg_flags |= RF_tainted;
1959 /* was last char in word? */
1960 ln = (locinput != PL_regbol)
1961 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1962 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1963 ln = isALNUM_uni(ln);
1964 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1967 ln = isALNUM_LC_uni(ln);
1968 n = isALNUM_LC_utf8((U8*)locinput);
1970 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1974 PL_reg_flags |= RF_tainted;
1977 if (!nextchr && locinput >= PL_regeol)
1979 if (!(OP(scan) == SPACE
1980 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1982 nextchr = UCHARAT(++locinput);
1985 PL_reg_flags |= RF_tainted;
1988 if (!nextchr && locinput >= PL_regeol)
1990 if (nextchr & 0x80) {
1991 if (!(OP(scan) == SPACEUTF8
1992 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1993 : isSPACE_LC_utf8((U8*)locinput)))
1997 locinput += PL_utf8skip[nextchr];
1998 nextchr = UCHARAT(locinput);
2001 if (!(OP(scan) == SPACEUTF8
2002 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2004 nextchr = UCHARAT(++locinput);
2007 PL_reg_flags |= RF_tainted;
2012 if (OP(scan) == SPACE
2013 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2015 nextchr = UCHARAT(++locinput);
2018 PL_reg_flags |= RF_tainted;
2023 if (nextchr & 0x80) {
2024 if (OP(scan) == NSPACEUTF8
2025 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2026 : isSPACE_LC_utf8((U8*)locinput))
2030 locinput += PL_utf8skip[nextchr];
2031 nextchr = UCHARAT(locinput);
2034 if (OP(scan) == NSPACEUTF8
2035 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2037 nextchr = UCHARAT(++locinput);
2040 PL_reg_flags |= RF_tainted;
2043 if (!nextchr && locinput >= PL_regeol)
2045 if (!(OP(scan) == DIGIT
2046 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2048 nextchr = UCHARAT(++locinput);
2051 PL_reg_flags |= RF_tainted;
2056 if (nextchr & 0x80) {
2057 if (OP(scan) == NDIGITUTF8
2058 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2059 : isDIGIT_LC_utf8((U8*)locinput))
2063 locinput += PL_utf8skip[nextchr];
2064 nextchr = UCHARAT(locinput);
2067 if (!isDIGIT(nextchr))
2069 nextchr = UCHARAT(++locinput);
2072 PL_reg_flags |= RF_tainted;
2077 if (OP(scan) == DIGIT
2078 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2080 nextchr = UCHARAT(++locinput);
2083 PL_reg_flags |= RF_tainted;
2086 if (!nextchr && locinput >= PL_regeol)
2088 if (nextchr & 0x80) {
2089 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2091 locinput += PL_utf8skip[nextchr];
2092 nextchr = UCHARAT(locinput);
2095 if (isDIGIT(nextchr))
2097 nextchr = UCHARAT(++locinput);
2100 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2102 locinput += PL_utf8skip[nextchr];
2103 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2104 locinput += UTF8SKIP(locinput);
2105 if (locinput > PL_regeol)
2107 nextchr = UCHARAT(locinput);
2110 PL_reg_flags |= RF_tainted;
2114 n = ARG(scan); /* which paren pair */
2115 ln = PL_regstartp[n];
2116 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2117 if (*PL_reglastparen < n || ln == -1)
2118 sayNO; /* Do not match unless seen CLOSEn. */
2119 if (ln == PL_regendp[n])
2123 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2125 char *e = PL_bostr + PL_regendp[n];
2127 * Note that we can't do the "other character" lookup trick as
2128 * in the 8-bit case (no pun intended) because in Unicode we
2129 * have to map both upper and title case to lower case.
2131 if (OP(scan) == REFF) {
2135 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2145 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2152 nextchr = UCHARAT(locinput);
2156 /* Inline the first character, for speed. */
2157 if (UCHARAT(s) != nextchr &&
2159 (UCHARAT(s) != ((OP(scan) == REFF
2160 ? PL_fold : PL_fold_locale)[nextchr]))))
2162 ln = PL_regendp[n] - ln;
2163 if (locinput + ln > PL_regeol)
2165 if (ln > 1 && (OP(scan) == REF
2166 ? memNE(s, locinput, ln)
2168 ? ibcmp(s, locinput, ln)
2169 : ibcmp_locale(s, locinput, ln))))
2172 nextchr = UCHARAT(locinput);
2183 OP_4tree *oop = PL_op;
2184 COP *ocurcop = PL_curcop;
2185 SV **ocurpad = PL_curpad;
2189 PL_op = (OP_4tree*)PL_regdata->data[n];
2190 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", (UV)PL_op) );
2191 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2192 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2194 CALLRUNOPS(aTHX); /* Scalar context. */
2200 PL_curpad = ocurpad;
2201 PL_curcop = ocurcop;
2203 if (logical == 2) { /* Postponed subexpression. */
2205 MAGIC *mg = Null(MAGIC*);
2207 CHECKPOINT cp, lastcp;
2209 if(SvROK(ret) || SvRMAGICAL(ret)) {
2210 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2213 mg = mg_find(sv, 'r');
2216 re = (regexp *)mg->mg_obj;
2217 (void)ReREFCNT_inc(re);
2221 char *t = SvPV(ret, len);
2223 char *oprecomp = PL_regprecomp;
2224 I32 osize = PL_regsize;
2225 I32 onpar = PL_regnpar;
2228 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2230 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2231 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2232 PL_regprecomp = oprecomp;
2237 PerlIO_printf(Perl_debug_log,
2238 "Entering embedded `%s%.60s%s%s'\n",
2242 (strlen(re->precomp) > 60 ? "..." : ""))
2245 state.prev = PL_reg_call_cc;
2246 state.cc = PL_regcc;
2247 state.re = PL_reg_re;
2251 cp = regcppush(0); /* Save *all* the positions. */
2254 state.ss = PL_savestack_ix;
2255 *PL_reglastparen = 0;
2256 PL_reg_call_cc = &state;
2257 PL_reginput = locinput;
2259 /* XXXX This is too dramatic a measure... */
2262 if (regmatch(re->program + 1)) {
2263 /* Even though we succeeded, we need to restore
2264 global variables, since we may be wrapped inside
2265 SUSPEND, thus the match may be not finished yet. */
2267 /* XXXX Do this only if SUSPENDed? */
2268 PL_reg_call_cc = state.prev;
2269 PL_regcc = state.cc;
2270 PL_reg_re = state.re;
2271 cache_re(PL_reg_re);
2273 /* XXXX This is too dramatic a measure... */
2276 /* These are needed even if not SUSPEND. */
2284 PL_reg_call_cc = state.prev;
2285 PL_regcc = state.cc;
2286 PL_reg_re = state.re;
2287 cache_re(PL_reg_re);
2289 /* XXXX This is too dramatic a measure... */
2298 sv_setsv(save_scalar(PL_replgv), ret);
2302 n = ARG(scan); /* which paren pair */
2303 PL_reg_start_tmp[n] = locinput;
2308 n = ARG(scan); /* which paren pair */
2309 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2310 PL_regendp[n] = locinput - PL_bostr;
2311 if (n > *PL_reglastparen)
2312 *PL_reglastparen = n;
2315 n = ARG(scan); /* which paren pair */
2316 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2319 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2321 next = NEXTOPER(NEXTOPER(scan));
2323 next = scan + ARG(scan);
2324 if (OP(next) == IFTHEN) /* Fake one. */
2325 next = NEXTOPER(NEXTOPER(next));
2329 logical = scan->flags;
2331 /*******************************************************************
2332 PL_regcc contains infoblock about the innermost (...)* loop, and
2333 a pointer to the next outer infoblock.
2335 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2337 1) After matching X, regnode for CURLYX is processed;
2339 2) This regnode creates infoblock on the stack, and calls
2340 regmatch() recursively with the starting point at WHILEM node;
2342 3) Each hit of WHILEM node tries to match A and Z (in the order
2343 depending on the current iteration, min/max of {min,max} and
2344 greediness). The information about where are nodes for "A"
2345 and "Z" is read from the infoblock, as is info on how many times "A"
2346 was already matched, and greediness.
2348 4) After A matches, the same WHILEM node is hit again.
2350 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2351 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2352 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2353 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2354 of the external loop.
2356 Currently present infoblocks form a tree with a stem formed by PL_curcc
2357 and whatever it mentions via ->next, and additional attached trees
2358 corresponding to temporarily unset infoblocks as in "5" above.
2360 In the following picture infoblocks for outer loop of
2361 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2362 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2363 infoblocks are drawn below the "reset" infoblock.
2365 In fact in the picture below we do not show failed matches for Z and T
2366 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2367 more obvious *why* one needs to *temporary* unset infoblocks.]
2369 Matched REx position InfoBlocks Comment
2373 Y A)*?Z)*?T x <- O <- I
2374 YA )*?Z)*?T x <- O <- I
2375 YA A)*?Z)*?T x <- O <- I
2376 YAA )*?Z)*?T x <- O <- I
2377 YAA Z)*?T x <- O # Temporary unset I
2380 YAAZ Y(A)*?Z)*?T x <- O
2383 YAAZY (A)*?Z)*?T x <- O
2386 YAAZY A)*?Z)*?T x <- O <- I
2389 YAAZYA )*?Z)*?T x <- O <- I
2392 YAAZYA Z)*?T x <- O # Temporary unset I
2398 YAAZYAZ T x # Temporary unset O
2405 *******************************************************************/
2408 CHECKPOINT cp = PL_savestack_ix;
2410 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2412 cc.oldcc = PL_regcc;
2414 cc.parenfloor = *PL_reglastparen;
2416 cc.min = ARG1(scan);
2417 cc.max = ARG2(scan);
2418 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2422 PL_reginput = locinput;
2423 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2425 PL_regcc = cc.oldcc;
2431 * This is really hard to understand, because after we match
2432 * what we're trying to match, we must make sure the rest of
2433 * the REx is going to match for sure, and to do that we have
2434 * to go back UP the parse tree by recursing ever deeper. And
2435 * if it fails, we have to reset our parent's current state
2436 * that we can try again after backing off.
2439 CHECKPOINT cp, lastcp;
2440 CURCUR* cc = PL_regcc;
2441 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2443 n = cc->cur + 1; /* how many we know we matched */
2444 PL_reginput = locinput;
2447 PerlIO_printf(Perl_debug_log,
2448 "%*s %ld out of %ld..%ld cc=%lx\n",
2449 REPORT_CODE_OFF+PL_regindent*2, "",
2450 (long)n, (long)cc->min,
2451 (long)cc->max, (long)cc)
2454 /* If degenerate scan matches "", assume scan done. */
2456 if (locinput == cc->lastloc && n >= cc->min) {
2457 PL_regcc = cc->oldcc;
2461 PerlIO_printf(Perl_debug_log,
2462 "%*s empty match detected, try continuation...\n",
2463 REPORT_CODE_OFF+PL_regindent*2, "")
2465 if (regmatch(cc->next))
2473 /* First just match a string of min scans. */
2477 cc->lastloc = locinput;
2478 if (regmatch(cc->scan))
2481 cc->lastloc = lastloc;
2486 /* Check whether we already were at this position.
2487 Postpone detection until we know the match is not
2488 *that* much linear. */
2489 if (!PL_reg_maxiter) {
2490 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2491 PL_reg_leftiter = PL_reg_maxiter;
2493 if (PL_reg_leftiter-- == 0) {
2494 I32 size = (PL_reg_maxiter + 7)/8;
2495 if (PL_reg_poscache) {
2496 if (PL_reg_poscache_size < size) {
2497 Renew(PL_reg_poscache, size, char);
2498 PL_reg_poscache_size = size;
2500 Zero(PL_reg_poscache, size, char);
2503 PL_reg_poscache_size = size;
2504 Newz(29, PL_reg_poscache, size, char);
2507 PerlIO_printf(Perl_debug_log,
2508 "%sDetected a super-linear match, switching on caching%s...\n",
2509 PL_colors[4], PL_colors[5])
2512 if (PL_reg_leftiter < 0) {
2513 I32 o = locinput - PL_bostr, b;
2515 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2518 if (PL_reg_poscache[o] & (1<<b)) {
2520 PerlIO_printf(Perl_debug_log,
2521 "%*s already tried at this position...\n",
2522 REPORT_CODE_OFF+PL_regindent*2, "")
2526 PL_reg_poscache[o] |= (1<<b);
2530 /* Prefer next over scan for minimal matching. */
2533 PL_regcc = cc->oldcc;
2536 cp = regcppush(cc->parenfloor);
2538 if (regmatch(cc->next)) {
2540 sayYES; /* All done. */
2548 if (n >= cc->max) { /* Maximum greed exceeded? */
2549 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2550 && !(PL_reg_flags & RF_warned)) {
2551 PL_reg_flags |= RF_warned;
2552 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2553 "Complex regular subexpression recursion",
2560 PerlIO_printf(Perl_debug_log,
2561 "%*s trying longer...\n",
2562 REPORT_CODE_OFF+PL_regindent*2, "")
2564 /* Try scanning more and see if it helps. */
2565 PL_reginput = locinput;
2567 cc->lastloc = locinput;
2568 cp = regcppush(cc->parenfloor);
2570 if (regmatch(cc->scan)) {
2577 cc->lastloc = lastloc;
2581 /* Prefer scan over next for maximal matching. */
2583 if (n < cc->max) { /* More greed allowed? */
2584 cp = regcppush(cc->parenfloor);
2586 cc->lastloc = locinput;
2588 if (regmatch(cc->scan)) {
2593 regcppop(); /* Restore some previous $<digit>s? */
2594 PL_reginput = locinput;
2596 PerlIO_printf(Perl_debug_log,
2597 "%*s failed, try continuation...\n",
2598 REPORT_CODE_OFF+PL_regindent*2, "")
2601 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2602 && !(PL_reg_flags & RF_warned)) {
2603 PL_reg_flags |= RF_warned;
2604 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2605 "Complex regular subexpression recursion",
2609 /* Failed deeper matches of scan, so see if this one works. */
2610 PL_regcc = cc->oldcc;
2613 if (regmatch(cc->next))
2619 cc->lastloc = lastloc;
2624 next = scan + ARG(scan);
2627 inner = NEXTOPER(NEXTOPER(scan));
2630 inner = NEXTOPER(scan);
2635 if (OP(next) != c1) /* No choice. */
2636 next = inner; /* Avoid recursion. */
2638 int lastparen = *PL_reglastparen;
2642 PL_reginput = locinput;
2643 if (regmatch(inner))
2646 for (n = *PL_reglastparen; n > lastparen; n--)
2648 *PL_reglastparen = n;
2651 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2655 inner = NEXTOPER(scan);
2656 if (c1 == BRANCHJ) {
2657 inner = NEXTOPER(inner);
2659 } while (scan != NULL && OP(scan) == c1);
2673 /* We suppose that the next guy does not need
2674 backtracking: in particular, it is of constant length,
2675 and has no parenths to influence future backrefs. */
2676 ln = ARG1(scan); /* min to match */
2677 n = ARG2(scan); /* max to match */
2678 paren = scan->flags;
2680 if (paren > PL_regsize)
2682 if (paren > *PL_reglastparen)
2683 *PL_reglastparen = paren;
2685 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2687 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2688 PL_reginput = locinput;
2691 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2693 if (ln && l == 0 && n >= ln
2694 /* In fact, this is tricky. If paren, then the
2695 fact that we did/didnot match may influence
2696 future execution. */
2697 && !(paren && ln == 0))
2699 locinput = PL_reginput;
2700 if (PL_regkind[(U8)OP(next)] == EXACT) {
2701 c1 = (U8)*STRING(next);
2702 if (OP(next) == EXACTF)
2704 else if (OP(next) == EXACTFL)
2705 c2 = PL_fold_locale[c1];
2712 /* This may be improved if l == 0. */
2713 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2714 /* If it could work, try it. */
2716 UCHARAT(PL_reginput) == c1 ||
2717 UCHARAT(PL_reginput) == c2)
2721 PL_regstartp[paren] =
2722 HOPc(PL_reginput, -l) - PL_bostr;
2723 PL_regendp[paren] = PL_reginput - PL_bostr;
2726 PL_regendp[paren] = -1;
2732 /* Couldn't or didn't -- move forward. */
2733 PL_reginput = locinput;
2734 if (regrepeat_hard(scan, 1, &l)) {
2736 locinput = PL_reginput;
2743 n = regrepeat_hard(scan, n, &l);
2744 if (n != 0 && l == 0
2745 /* In fact, this is tricky. If paren, then the
2746 fact that we did/didnot match may influence
2747 future execution. */
2748 && !(paren && ln == 0))
2750 locinput = PL_reginput;
2752 PerlIO_printf(Perl_debug_log,
2753 "%*s matched %d times, len=%"IVdf"...\n",
2754 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
2758 if (PL_regkind[(U8)OP(next)] == EXACT) {
2759 c1 = (U8)*STRING(next);
2760 if (OP(next) == EXACTF)
2762 else if (OP(next) == EXACTFL)
2763 c2 = PL_fold_locale[c1];
2772 /* If it could work, try it. */
2774 UCHARAT(PL_reginput) == c1 ||
2775 UCHARAT(PL_reginput) == c2)
2778 PerlIO_printf(Perl_debug_log,
2779 "%*s trying tail with n=%"IVdf"...\n",
2780 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
2784 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2785 PL_regendp[paren] = PL_reginput - PL_bostr;
2788 PL_regendp[paren] = -1;
2794 /* Couldn't or didn't -- back up. */
2796 locinput = HOPc(locinput, -l);
2797 PL_reginput = locinput;
2804 paren = scan->flags; /* Which paren to set */
2805 if (paren > PL_regsize)
2807 if (paren > *PL_reglastparen)
2808 *PL_reglastparen = paren;
2809 ln = ARG1(scan); /* min to match */
2810 n = ARG2(scan); /* max to match */
2811 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2815 ln = ARG1(scan); /* min to match */
2816 n = ARG2(scan); /* max to match */
2817 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2822 scan = NEXTOPER(scan);
2828 scan = NEXTOPER(scan);
2832 * Lookahead to avoid useless match attempts
2833 * when we know what character comes next.
2835 if (PL_regkind[(U8)OP(next)] == EXACT) {
2836 c1 = (U8)*STRING(next);
2837 if (OP(next) == EXACTF)
2839 else if (OP(next) == EXACTFL)
2840 c2 = PL_fold_locale[c1];
2846 PL_reginput = locinput;
2850 if (ln && regrepeat(scan, ln) < ln)
2852 locinput = PL_reginput;
2855 char *e = locinput + n - ln; /* Should not check after this */
2856 char *old = locinput;
2858 if (e >= PL_regeol || (n == REG_INFTY))
2861 /* Find place 'next' could work */
2863 while (locinput <= e && *locinput != c1)
2866 while (locinput <= e
2873 /* PL_reginput == old now */
2874 if (locinput != old) {
2875 ln = 1; /* Did some */
2876 if (regrepeat(scan, locinput - old) <
2880 /* PL_reginput == locinput now */
2883 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2884 PL_regendp[paren] = locinput - PL_bostr;
2887 PL_regendp[paren] = -1;
2891 PL_reginput = locinput; /* Could be reset... */
2893 /* Couldn't or didn't -- move forward. */
2898 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2899 /* If it could work, try it. */
2901 UCHARAT(PL_reginput) == c1 ||
2902 UCHARAT(PL_reginput) == c2)
2906 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2907 PL_regendp[paren] = PL_reginput - PL_bostr;
2910 PL_regendp[paren] = -1;
2916 /* Couldn't or didn't -- move forward. */
2917 PL_reginput = locinput;
2918 if (regrepeat(scan, 1)) {
2920 locinput = PL_reginput;
2928 n = regrepeat(scan, n);
2929 locinput = PL_reginput;
2930 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2931 (!PL_multiline || OP(next) == SEOL))
2932 ln = n; /* why back off? */
2936 /* If it could work, try it. */
2938 UCHARAT(PL_reginput) == c1 ||
2939 UCHARAT(PL_reginput) == c2)
2943 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2944 PL_regendp[paren] = PL_reginput - PL_bostr;
2947 PL_regendp[paren] = -1;
2953 /* Couldn't or didn't -- back up. */
2955 PL_reginput = locinput = HOPc(locinput, -1);
2960 /* If it could work, try it. */
2962 UCHARAT(PL_reginput) == c1 ||
2963 UCHARAT(PL_reginput) == c2)
2969 /* Couldn't or didn't -- back up. */
2971 PL_reginput = locinput = HOPc(locinput, -1);
2978 if (PL_reg_call_cc) {
2979 re_cc_state *cur_call_cc = PL_reg_call_cc;
2980 CURCUR *cctmp = PL_regcc;
2981 regexp *re = PL_reg_re;
2982 CHECKPOINT cp, lastcp;
2984 cp = regcppush(0); /* Save *all* the positions. */
2986 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2988 PL_reginput = locinput; /* Make position available to
2990 cache_re(PL_reg_call_cc->re);
2991 PL_regcc = PL_reg_call_cc->cc;
2992 PL_reg_call_cc = PL_reg_call_cc->prev;
2993 if (regmatch(cur_call_cc->node)) {
2994 PL_reg_call_cc = cur_call_cc;
3000 PL_reg_call_cc = cur_call_cc;
3006 PerlIO_printf(Perl_debug_log,
3007 "%*s continuation failed...\n",
3008 REPORT_CODE_OFF+PL_regindent*2, "")
3012 if (locinput < PL_regtill) {
3013 DEBUG_r(PerlIO_printf(Perl_debug_log,
3014 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3016 (long)(locinput - PL_reg_starttry),
3017 (long)(PL_regtill - PL_reg_starttry),
3019 sayNO_FINAL; /* Cannot match: too short. */
3021 PL_reginput = locinput; /* put where regtry can find it */
3022 sayYES_FINAL; /* Success! */
3024 PL_reginput = locinput; /* put where regtry can find it */
3025 sayYES_LOUD; /* Success! */
3028 PL_reginput = locinput;
3033 if (UTF) { /* XXXX This is absolutely
3034 broken, we read before
3036 s = HOPMAYBEc(locinput, -scan->flags);
3042 if (locinput < PL_bostr + scan->flags)
3044 PL_reginput = locinput - scan->flags;
3049 PL_reginput = locinput;
3054 if (UTF) { /* XXXX This is absolutely
3055 broken, we read before
3057 s = HOPMAYBEc(locinput, -scan->flags);
3058 if (!s || s < PL_bostr)
3063 if (locinput < PL_bostr + scan->flags)
3065 PL_reginput = locinput - scan->flags;
3070 PL_reginput = locinput;
3073 inner = NEXTOPER(NEXTOPER(scan));
3074 if (regmatch(inner) != n) {
3089 if (OP(scan) == SUSPEND) {
3090 locinput = PL_reginput;
3091 nextchr = UCHARAT(locinput);
3096 next = scan + ARG(scan);
3101 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3102 (UV)scan, OP(scan));
3103 Perl_croak(aTHX_ "regexp memory corruption");
3109 * We get here only if there's trouble -- normally "case END" is
3110 * the terminating point.
3112 Perl_croak(aTHX_ "corrupted regexp pointers");
3118 PerlIO_printf(Perl_debug_log,
3119 "%*s %scould match...%s\n",
3120 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3124 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3125 PL_colors[4],PL_colors[5]));
3134 PerlIO_printf(Perl_debug_log,
3135 "%*s %sfailed...%s\n",
3136 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3148 - regrepeat - repeatedly match something simple, report how many
3151 * [This routine now assumes that it will only match on things of length 1.
3152 * That was true before, but now we assume scan - reginput is the count,
3153 * rather than incrementing count on every character. [Er, except utf8.]]
3156 S_regrepeat(pTHX_ regnode *p, I32 max)
3159 register char *scan;
3160 register char *opnd;
3162 register char *loceol = PL_regeol;
3163 register I32 hardcount = 0;
3166 if (max != REG_INFTY && max < loceol - scan)
3167 loceol = scan + max;
3170 while (scan < loceol && *scan != '\n')
3178 while (scan < loceol && *scan != '\n') {
3179 scan += UTF8SKIP(scan);
3185 while (scan < loceol) {
3186 scan += UTF8SKIP(scan);
3190 case EXACT: /* length of string is 1 */
3192 while (scan < loceol && UCHARAT(scan) == c)
3195 case EXACTF: /* length of string is 1 */
3197 while (scan < loceol &&
3198 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3201 case EXACTFL: /* length of string is 1 */
3202 PL_reg_flags |= RF_tainted;
3204 while (scan < loceol &&
3205 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3210 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3211 scan += UTF8SKIP(scan);
3217 while (scan < loceol && REGINCLASS(opnd, *scan))
3221 while (scan < loceol && isALNUM(*scan))
3226 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3227 scan += UTF8SKIP(scan);
3232 PL_reg_flags |= RF_tainted;
3233 while (scan < loceol && isALNUM_LC(*scan))
3237 PL_reg_flags |= RF_tainted;
3239 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3240 scan += UTF8SKIP(scan);
3246 while (scan < loceol && !isALNUM(*scan))
3251 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3252 scan += UTF8SKIP(scan);
3257 PL_reg_flags |= RF_tainted;
3258 while (scan < loceol && !isALNUM_LC(*scan))
3262 PL_reg_flags |= RF_tainted;
3264 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3265 scan += UTF8SKIP(scan);
3270 while (scan < loceol && isSPACE(*scan))
3275 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3276 scan += UTF8SKIP(scan);
3281 PL_reg_flags |= RF_tainted;
3282 while (scan < loceol && isSPACE_LC(*scan))
3286 PL_reg_flags |= RF_tainted;
3288 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3289 scan += UTF8SKIP(scan);
3294 while (scan < loceol && !isSPACE(*scan))
3299 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3300 scan += UTF8SKIP(scan);
3305 PL_reg_flags |= RF_tainted;
3306 while (scan < loceol && !isSPACE_LC(*scan))
3310 PL_reg_flags |= RF_tainted;
3312 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3313 scan += UTF8SKIP(scan);
3318 while (scan < loceol && isDIGIT(*scan))
3323 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3324 scan += UTF8SKIP(scan);
3330 while (scan < loceol && !isDIGIT(*scan))
3335 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3336 scan += UTF8SKIP(scan);
3340 default: /* Called on something of 0 width. */
3341 break; /* So match right here or not at all. */
3347 c = scan - PL_reginput;
3352 SV *prop = sv_newmortal();
3355 PerlIO_printf(Perl_debug_log,
3356 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3357 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3364 - regrepeat_hard - repeatedly match something, report total lenth and length
3366 * The repeater is supposed to have constant length.
3370 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3373 register char *scan;
3374 register char *start;
3375 register char *loceol = PL_regeol;
3377 I32 count = 0, res = 1;
3382 start = PL_reginput;
3384 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3387 while (start < PL_reginput) {
3389 start += UTF8SKIP(start);
3400 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3402 *lp = l = PL_reginput - start;
3403 if (max != REG_INFTY && l*max < loceol - scan)
3404 loceol = scan + l*max;
3417 - reginclass - determine if a character falls into a character class
3421 S_reginclass(pTHX_ register char *p, register I32 c)
3424 char flags = ANYOF_FLAGS(p);
3428 if (ANYOF_BITMAP_TEST(p, c))
3430 else if (flags & ANYOF_FOLD) {
3432 if (flags & ANYOF_LOCALE) {
3433 PL_reg_flags |= RF_tainted;
3434 cf = PL_fold_locale[c];
3438 if (ANYOF_BITMAP_TEST(p, cf))
3442 if (!match && (flags & ANYOF_CLASS)) {
3443 PL_reg_flags |= RF_tainted;
3445 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3446 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3447 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3448 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3449 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3450 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3451 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3452 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3453 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3454 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3455 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3456 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3457 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3458 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3459 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3460 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3461 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3462 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3463 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3464 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3465 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3466 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3467 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3468 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3469 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3470 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3471 ) /* How's that for a conditional? */
3477 return (flags & ANYOF_INVERT) ? !match : match;
3481 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3484 char flags = ARG1(f);
3486 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3488 if (swash_fetch(sv, p))
3490 else if (flags & ANYOF_FOLD) {
3493 if (flags & ANYOF_LOCALE) {
3494 PL_reg_flags |= RF_tainted;
3495 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3498 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3499 if (swash_fetch(sv, tmpbuf))
3503 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3505 return (flags & ANYOF_INVERT) ? !match : match;
3509 S_reghop(pTHX_ U8 *s, I32 off)
3513 while (off-- && s < (U8*)PL_regeol)
3518 if (s > (U8*)PL_bostr) {
3521 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3523 } /* XXX could check well-formedness here */
3531 S_reghopmaybe(pTHX_ U8* s, I32 off)
3535 while (off-- && s < (U8*)PL_regeol)
3542 if (s > (U8*)PL_bostr) {
3545 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3547 } /* XXX could check well-formedness here */
3564 restore_pos(pTHXo_ void *arg)
3567 if (PL_reg_eval_set) {
3568 if (PL_reg_oldsaved) {
3569 PL_reg_re->subbeg = PL_reg_oldsaved;
3570 PL_reg_re->sublen = PL_reg_oldsavedlen;
3571 RX_MATCH_COPIED_on(PL_reg_re);
3573 PL_reg_magic->mg_len = PL_reg_oldpos;
3574 PL_reg_eval_set = 0;
3575 PL_curpm = PL_reg_oldcurpm;