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 \\%d to %d(%d)..%d%s\n",
180 paren, PL_regstartp[paren],
181 PL_reg_start_tmp[paren] - PL_bostr,
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. */
284 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
285 char *strend, U32 flags, re_scream_pos_data *data)
287 register I32 start_shift;
288 /* Should be nonnegative! */
289 register I32 end_shift;
295 register char *other_last = Nullch;
297 DEBUG_r( if (!PL_colorset) reginitcolors() );
298 DEBUG_r(PerlIO_printf(Perl_debug_log,
299 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
300 PL_colors[4],PL_colors[5],PL_colors[0],
303 (strlen(prog->precomp) > 60 ? "..." : ""),
305 (strend - strpos > 60 ? 60 : strend - strpos),
306 strpos, PL_colors[1],
307 (strend - strpos > 60 ? "..." : ""))
310 if (prog->minlen > strend - strpos) {
311 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
314 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
315 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
316 || ( (prog->reganch & ROPT_ANCH_BOL)
317 && !PL_multiline ) ); /* Check after \n? */
319 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
320 /* Substring at constant offset from beg-of-str... */
323 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
324 && (sv && (strpos + SvCUR(sv) != strend)) ) {
325 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
328 PL_regeol = strend; /* Used in HOP() */
329 s = HOPc(strpos, prog->check_offset_min);
330 if (SvTAIL(prog->check_substr)) {
331 slen = SvCUR(prog->check_substr); /* >= 1 */
333 if ( strend - s > slen || strend - s < slen - 1
334 || (strend - s == slen && strend[-1] != '\n')) {
335 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
338 /* Now should match s[0..slen-2] */
340 if (slen && (*SvPVX(prog->check_substr) != *s
342 && memNE(SvPVX(prog->check_substr), s, slen)))) {
344 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
348 else if (*SvPVX(prog->check_substr) != *s
349 || ((slen = SvCUR(prog->check_substr)) > 1
350 && memNE(SvPVX(prog->check_substr), s, slen)))
352 goto success_at_start;
354 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
356 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
357 /* Should be nonnegative! */
358 end_shift = prog->minlen - start_shift -
359 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
361 I32 end = prog->check_offset_max + CHR_SVLEN(prog->check_substr)
362 - (SvTAIL(prog->check_substr) != 0);
363 I32 eshift = strend - s - end;
365 if (end_shift < eshift)
369 else { /* Can match at random position */
372 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
373 /* Should be nonnegative! */
374 end_shift = prog->minlen - start_shift -
375 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
378 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
380 Perl_croak(aTHX_ "panic: end_shift");
383 check = prog->check_substr;
385 /* Find a possible match in the region s..strend by looking for
386 the "check" substring in the region corrected by start/end_shift. */
387 if (flags & REXEC_SCREAM) {
388 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
389 I32 p = -1; /* Internal iterator of scream. */
390 I32 *pp = data ? data->scream_pos : &p;
392 if (PL_screamfirst[BmRARE(check)] >= 0
393 || ( BmRARE(check) == '\n'
394 && (BmPREVIOUS(check) == SvCUR(check) - 1)
396 s = screaminstr(sv, check,
397 start_shift + (s - strbeg), end_shift, pp, 0);
401 *data->scream_olds = s;
404 s = fbm_instr((unsigned char*)s + start_shift,
405 (unsigned char*)strend - end_shift,
406 check, PL_multiline ? FBMrf_MULTILINE : 0);
408 /* Update the count-of-usability, remove useless subpatterns,
411 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
412 (s ? "Found" : "Did not find"),
413 ((check == prog->anchored_substr) ? "anchored" : "floating"),
415 SvCUR(check) - (SvTAIL(check)!=0), SvPVX(check),
416 PL_colors[1], (SvTAIL(check) ? "$" : ""),
417 (s ? " at offset " : "...\n") ) );
422 /* Finish the diagnostic message */
423 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - strpos)) );
425 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
426 Start with the other substr.
427 XXXX no SCREAM optimization yet - and a very coarse implementation
428 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
429 *always* match. Probably should be marked during compile...
430 Probably it is right to do no SCREAM here...
433 if (prog->float_substr && prog->anchored_substr) {
434 /* Take into account the anchored substring. */
435 /* XXXX May be hopelessly wrong for UTF... */
437 other_last = strpos - 1;
438 if (check == prog->float_substr) {
439 char *last = s - start_shift, *last1, *last2;
443 t = s - prog->check_offset_max;
444 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
445 && (!(prog->reganch & ROPT_UTF8)
446 || (PL_bostr = strpos, /* Used in regcopmaybe() */
447 (t = reghopmaybe_c(s, -(prog->check_offset_max)))
452 t += prog->anchored_offset;
456 last2 = last1 = strend - prog->minlen;
459 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
460 /* On end-of-str: see comment below. */
461 s = fbm_instr((unsigned char*)t,
462 (unsigned char*)last1 + prog->anchored_offset
463 + SvCUR(prog->anchored_substr)
464 - (SvTAIL(prog->anchored_substr)!=0),
465 prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
466 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
467 (s ? "Found" : "Contradicts"),
469 SvCUR(prog->anchored_substr)
470 - (SvTAIL(prog->anchored_substr)!=0),
471 SvPVX(prog->anchored_substr),
472 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
474 if (last1 >= last2) {
475 DEBUG_r(PerlIO_printf(Perl_debug_log,
476 ", giving up...\n"));
479 DEBUG_r(PerlIO_printf(Perl_debug_log,
480 ", trying floating at offset %ld...\n",
481 (long)(s1 + 1 - strpos)));
482 PL_regeol = strend; /* Used in HOP() */
483 other_last = last1 + prog->anchored_offset;
488 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
489 (long)(s - strpos)));
490 t = s - prog->anchored_offset;
498 else { /* Take into account the floating substring. */
503 last1 = last = strend - prog->minlen + prog->float_min_offset;
504 if (last - t > prog->float_max_offset)
505 last = t + prog->float_max_offset;
506 s = t + prog->float_min_offset;
509 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
510 /* fbm_instr() takes into account exact value of end-of-str
511 if the check is SvTAIL(ed). Since false positives are OK,
512 and end-of-str is not later than strend we are OK. */
513 s = fbm_instr((unsigned char*)s,
514 (unsigned char*)last + SvCUR(prog->float_substr)
515 - (SvTAIL(prog->float_substr)!=0),
516 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
517 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
518 (s ? "Found" : "Contradicts"),
520 SvCUR(prog->float_substr)
521 - (SvTAIL(prog->float_substr)!=0),
522 SvPVX(prog->float_substr),
523 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
526 DEBUG_r(PerlIO_printf(Perl_debug_log,
527 ", giving up...\n"));
530 DEBUG_r(PerlIO_printf(Perl_debug_log,
531 ", trying anchored starting at offset %ld...\n",
532 (long)(s1 + 1 - strpos)));
534 PL_regeol = strend; /* Used in HOP() */
539 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
540 (long)(s - strpos)));
550 t = s - prog->check_offset_max;
552 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
553 && (!(prog->reganch & ROPT_UTF8)
554 || (PL_bostr = strpos, /* Used in regcopmaybe() */
555 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
558 /* Fixed substring is found far enough so that the match
559 cannot start at strpos. */
561 if (ml_anch && t[-1] != '\n') {
562 find_anchor: /* Eventually fbm_*() should handle this */
563 while (t < strend - prog->minlen) {
565 if (t < s - prog->check_offset_min) {
567 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
568 PL_colors[0],PL_colors[1], (long)(s - strpos)));
571 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting at offset %ld...\n",
572 PL_colors[0],PL_colors[1], (long)(t + 1 - strpos)));
578 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
579 PL_colors[0],PL_colors[1]));
584 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
588 /* The found string does not prohibit matching at beg-of-str
589 - no optimization of calling REx engine can be performed,
590 unless it was an MBOL and we are not after MBOL. */
592 /* Even in this situation we may use MBOL flag if strpos is offset
593 wrt the start of the string. */
595 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
600 if (!(prog->reganch & ROPT_NAUGHTY)
601 && --BmUSEFUL(prog->check_substr) < 0
602 && prog->check_substr == prog->float_substr) { /* boo */
603 /* If flags & SOMETHING - do not do it many times on the same match */
604 SvREFCNT_dec(prog->check_substr);
605 prog->check_substr = Nullsv; /* disable */
606 prog->float_substr = Nullsv; /* clear */
608 prog->reganch &= ~RE_USE_INTUIT;
614 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sGuessed:%s match at offset %ld\n",
615 PL_colors[4], PL_colors[5], (long)(s - strpos)) );
618 fail_finish: /* Substring not found */
619 BmUSEFUL(prog->check_substr) += 5; /* hooray */
621 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
622 PL_colors[4],PL_colors[5]));
627 - regexec_flags - match a regexp against a string
630 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
631 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
632 /* strend: pointer to null at end of string */
633 /* strbeg: real beginning of string */
634 /* minend: end of match must be >=minend after stringarg. */
635 /* data: May be used for some additional optimizations. */
636 /* nosave: For optimizations. */
641 register char *startpos = stringarg;
643 I32 minlen; /* must match at least this many chars */
644 I32 dontbother = 0; /* how many characters not to try at end */
645 I32 start_shift = 0; /* Offset of the start to find
646 constant substr. */ /* CC */
647 I32 end_shift = 0; /* Same for the end. */ /* CC */
648 I32 scream_pos = -1; /* Internal iterator of scream. */
650 SV* oreplsv = GvSV(PL_replgv);
656 PL_regnarrate = PL_debug & 512;
660 if (prog == NULL || startpos == NULL) {
661 Perl_croak(aTHX_ "NULL regexp parameter");
665 minlen = prog->minlen;
666 if (strend - startpos < minlen) goto phooey;
668 if (startpos == strbeg) /* is ^ valid at stringarg? */
671 PL_regprev = (U32)stringarg[-1];
672 if (!PL_multiline && PL_regprev == '\n')
673 PL_regprev = '\0'; /* force ^ to NOT match */
676 /* Check validity of program. */
677 if (UCHARAT(prog->program) != REG_MAGIC) {
678 Perl_croak(aTHX_ "corrupted regexp program");
685 if (prog->reganch & ROPT_UTF8)
686 PL_reg_flags |= RF_utf8;
688 /* Mark beginning of line for ^ and lookbehind. */
689 PL_regbol = startpos;
693 /* Mark end of line for $ (and such) */
696 /* see how far we have to get to not match where we matched before */
697 PL_regtill = startpos+minend;
699 /* We start without call_cc context. */
702 /* If there is a "must appear" string, look for it. */
705 if (prog->reganch & ROPT_GPOS_SEEN) {
708 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
709 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
710 PL_reg_ganch = strbeg + mg->mg_len;
712 PL_reg_ganch = startpos;
713 if (prog->reganch & ROPT_ANCH_GPOS) {
714 if (s > PL_reg_ganch)
720 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
721 re_scream_pos_data d;
723 d.scream_olds = &scream_olds;
724 d.scream_pos = &scream_pos;
725 s = re_intuit_start(prog, sv, s, strend, flags, &d);
727 goto phooey; /* not present */
730 DEBUG_r( if (!PL_colorset) reginitcolors() );
731 DEBUG_r(PerlIO_printf(Perl_debug_log,
732 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
733 PL_colors[4],PL_colors[5],PL_colors[0],
736 (strlen(prog->precomp) > 60 ? "..." : ""),
738 (strend - startpos > 60 ? 60 : strend - startpos),
739 startpos, PL_colors[1],
740 (strend - startpos > 60 ? "..." : ""))
743 /* Simplest case: anchored match need be tried only once. */
744 /* [unless only anchor is BOL and multiline is set] */
745 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
746 if (s == startpos && regtry(prog, startpos))
748 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
749 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
754 dontbother = minlen - 1;
755 end = HOPc(strend, -dontbother) - 1;
756 /* for multiline we only have to try after newlines */
757 if (prog->check_substr) {
763 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
771 if (*s++ == '\n') { /* don't need PL_utf8skip here */
779 } else if (prog->reganch & ROPT_ANCH_GPOS) {
780 if (regtry(prog, PL_reg_ganch))
785 /* Messy cases: unanchored match. */
786 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
787 /* we have /x+whatever/ */
788 /* it must be a one character string (XXXX Except UTF?) */
789 char ch = SvPVX(prog->anchored_substr)[0];
793 if (regtry(prog, s)) goto got_it;
795 while (s < strend && *s == ch)
804 if (regtry(prog, s)) goto got_it;
806 while (s < strend && *s == ch)
814 else if (prog->anchored_substr != Nullsv
815 || (prog->float_substr != Nullsv
816 && prog->float_max_offset < strend - s)) {
817 SV *must = prog->anchored_substr
818 ? prog->anchored_substr : prog->float_substr;
820 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
822 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
823 I32 delta = back_max - back_min;
824 char *last = HOPc(strend, /* Cannot start after this */
825 -(I32)(CHR_SVLEN(must)
826 - (SvTAIL(must) != 0) + back_min));
827 char *last1; /* Last position checked before */
832 last1 = s - 1; /* bogus */
834 /* XXXX check_substr already used to find `s', can optimize if
835 check_substr==must. */
837 dontbother = end_shift;
838 strend = HOPc(strend, -dontbother);
839 while ( (s <= last) &&
840 ((flags & REXEC_SCREAM)
841 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
842 end_shift, &scream_pos, 0))
843 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
844 (unsigned char*)strend, must,
845 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
846 if (HOPc(s, -back_max) > last1) {
847 last1 = HOPc(s, -back_min);
848 s = HOPc(s, -back_max);
851 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
853 last1 = HOPc(s, -back_min);
873 else if (c = prog->regstclass) {
874 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
878 dontbother = minlen - 1;
879 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
881 /* We know what class it must start with. */
886 if (REGINCLASSUTF8(c, (U8*)s)) {
887 if (tmp && regtry(prog, s))
900 if (REGINCLASS(cc, *s)) {
901 if (tmp && regtry(prog, s))
912 PL_reg_flags |= RF_tainted;
919 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
920 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
922 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
929 if ((minlen || tmp) && regtry(prog,s))
933 PL_reg_flags |= RF_tainted;
938 strend = reghop_c(strend, -1);
940 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
941 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
943 if (tmp == !(OP(c) == BOUND ?
944 swash_fetch(PL_utf8_alnum, (U8*)s) :
945 isALNUM_LC_utf8((U8*)s)))
953 if ((minlen || tmp) && regtry(prog,s))
957 PL_reg_flags |= RF_tainted;
964 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
965 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
967 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
969 else if (regtry(prog, s))
973 if ((minlen || !tmp) && regtry(prog,s))
977 PL_reg_flags |= RF_tainted;
982 strend = reghop_c(strend, -1);
984 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
985 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
987 if (tmp == !(OP(c) == NBOUND ?
988 swash_fetch(PL_utf8_alnum, (U8*)s) :
989 isALNUM_LC_utf8((U8*)s)))
991 else if (regtry(prog, s))
995 if ((minlen || !tmp) && regtry(prog,s))
1001 if (tmp && regtry(prog, s))
1012 while (s < strend) {
1013 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1014 if (tmp && regtry(prog, s))
1025 PL_reg_flags |= RF_tainted;
1026 while (s < strend) {
1027 if (isALNUM_LC(*s)) {
1028 if (tmp && regtry(prog, s))
1039 PL_reg_flags |= RF_tainted;
1040 while (s < strend) {
1041 if (isALNUM_LC_utf8((U8*)s)) {
1042 if (tmp && regtry(prog, s))
1053 while (s < strend) {
1055 if (tmp && regtry(prog, s))
1066 while (s < strend) {
1067 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1068 if (tmp && regtry(prog, s))
1079 PL_reg_flags |= RF_tainted;
1080 while (s < strend) {
1081 if (!isALNUM_LC(*s)) {
1082 if (tmp && regtry(prog, s))
1093 PL_reg_flags |= RF_tainted;
1094 while (s < strend) {
1095 if (!isALNUM_LC_utf8((U8*)s)) {
1096 if (tmp && regtry(prog, s))
1107 while (s < strend) {
1109 if (tmp && regtry(prog, s))
1120 while (s < strend) {
1121 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1122 if (tmp && regtry(prog, s))
1133 PL_reg_flags |= RF_tainted;
1134 while (s < strend) {
1135 if (isSPACE_LC(*s)) {
1136 if (tmp && regtry(prog, s))
1147 PL_reg_flags |= RF_tainted;
1148 while (s < strend) {
1149 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1150 if (tmp && regtry(prog, s))
1161 while (s < strend) {
1163 if (tmp && regtry(prog, s))
1174 while (s < strend) {
1175 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1176 if (tmp && regtry(prog, s))
1187 PL_reg_flags |= RF_tainted;
1188 while (s < strend) {
1189 if (!isSPACE_LC(*s)) {
1190 if (tmp && regtry(prog, s))
1201 PL_reg_flags |= RF_tainted;
1202 while (s < strend) {
1203 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1204 if (tmp && regtry(prog, s))
1215 while (s < strend) {
1217 if (tmp && regtry(prog, s))
1228 while (s < strend) {
1229 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1230 if (tmp && regtry(prog, s))
1241 PL_reg_flags |= RF_tainted;
1242 while (s < strend) {
1243 if (isDIGIT_LC(*s)) {
1244 if (tmp && regtry(prog, s))
1255 PL_reg_flags |= RF_tainted;
1256 while (s < strend) {
1257 if (isDIGIT_LC_utf8((U8*)s)) {
1258 if (tmp && regtry(prog, s))
1269 while (s < strend) {
1271 if (tmp && regtry(prog, s))
1282 while (s < strend) {
1283 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1284 if (tmp && regtry(prog, s))
1295 PL_reg_flags |= RF_tainted;
1296 while (s < strend) {
1297 if (!isDIGIT_LC(*s)) {
1298 if (tmp && regtry(prog, s))
1309 PL_reg_flags |= RF_tainted;
1310 while (s < strend) {
1311 if (!isDIGIT_LC_utf8((U8*)s)) {
1312 if (tmp && regtry(prog, s))
1326 if (prog->float_substr != Nullsv) { /* Trim the end. */
1328 I32 oldpos = scream_pos;
1330 if (flags & REXEC_SCREAM) {
1331 last = screaminstr(sv, prog->float_substr, s - strbeg,
1332 end_shift, &scream_pos, 1); /* last one */
1334 last = scream_olds; /* Only one occurence. */
1338 char *little = SvPV(prog->float_substr, len);
1340 if (SvTAIL(prog->float_substr)) {
1341 if (memEQ(strend - len + 1, little, len - 1))
1342 last = strend - len + 1;
1343 else if (!PL_multiline)
1344 last = memEQ(strend - len, little, len)
1345 ? strend - len : Nullch;
1351 last = rninstr(s, strend, little, little + len);
1353 last = strend; /* matching `$' */
1356 if (last == NULL) goto phooey; /* Should not happen! */
1357 dontbother = strend - last + prog->float_min_offset;
1359 if (minlen && (dontbother < minlen))
1360 dontbother = minlen - 1;
1361 strend -= dontbother; /* this one's always in bytes! */
1362 /* We don't know much -- general case. */
1365 if (regtry(prog, s))
1374 if (regtry(prog, s))
1376 } while (s++ < strend);
1384 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1386 if (PL_reg_eval_set) {
1387 /* Preserve the current value of $^R */
1388 if (oreplsv != GvSV(PL_replgv))
1389 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1390 restored, the value remains
1392 restore_pos(aTHXo_ 0);
1395 /* make sure $`, $&, $', and $digit will work later */
1396 if ( !(flags & REXEC_NOT_FIRST) ) {
1397 if (RX_MATCH_COPIED(prog)) {
1398 Safefree(prog->subbeg);
1399 RX_MATCH_COPIED_off(prog);
1401 if (flags & REXEC_COPY_STR) {
1402 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1404 s = savepvn(strbeg, i);
1407 RX_MATCH_COPIED_on(prog);
1410 prog->subbeg = strbeg;
1411 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1418 if (PL_reg_eval_set)
1419 restore_pos(aTHXo_ 0);
1424 - regtry - try match at specific point
1426 STATIC I32 /* 0 failure, 1 success */
1427 S_regtry(pTHX_ regexp *prog, char *startpos)
1435 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1438 PL_reg_eval_set = RS_init;
1440 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1441 PL_stack_sp - PL_stack_base);
1443 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1444 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1445 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1447 /* Apparently this is not needed, judging by wantarray. */
1448 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1449 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1452 /* Make $_ available to executed code. */
1453 if (PL_reg_sv != DEFSV) {
1454 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1459 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1460 && (mg = mg_find(PL_reg_sv, 'g')))) {
1461 /* prepare for quick setting of pos */
1462 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1463 mg = mg_find(PL_reg_sv, 'g');
1467 PL_reg_oldpos = mg->mg_len;
1468 SAVEDESTRUCTOR(restore_pos, 0);
1471 New(22,PL_reg_curpm, 1, PMOP);
1472 PL_reg_curpm->op_pmregexp = prog;
1473 PL_reg_oldcurpm = PL_curpm;
1474 PL_curpm = PL_reg_curpm;
1475 if (RX_MATCH_COPIED(prog)) {
1476 /* Here is a serious problem: we cannot rewrite subbeg,
1477 since it may be needed if this match fails. Thus
1478 $` inside (?{}) could fail... */
1479 PL_reg_oldsaved = prog->subbeg;
1480 PL_reg_oldsavedlen = prog->sublen;
1481 RX_MATCH_COPIED_off(prog);
1484 PL_reg_oldsaved = Nullch;
1485 prog->subbeg = PL_bostr;
1486 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1488 prog->startp[0] = startpos - PL_bostr;
1489 PL_reginput = startpos;
1490 PL_regstartp = prog->startp;
1491 PL_regendp = prog->endp;
1492 PL_reglastparen = &prog->lastparen;
1493 prog->lastparen = 0;
1495 DEBUG_r(PL_reg_starttry = startpos);
1496 if (PL_reg_start_tmpl <= prog->nparens) {
1497 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1498 if(PL_reg_start_tmp)
1499 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1501 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1504 /* XXXX What this code is doing here?!!! There should be no need
1505 to do this again and again, PL_reglastparen should take care of
1509 if (prog->nparens) {
1510 for (i = prog->nparens; i >= 1; i--) {
1516 if (regmatch(prog->program + 1)) {
1517 prog->endp[0] = PL_reginput - PL_bostr;
1525 - regmatch - main matching routine
1527 * Conceptually the strategy is simple: check to see whether the current
1528 * node matches, call self recursively to see whether the rest matches,
1529 * and then act accordingly. In practice we make some effort to avoid
1530 * recursion, in particular by going through "ordinary" nodes (that don't
1531 * need to know whether the rest of the match failed) by a loop instead of
1534 /* [lwall] I've hoisted the register declarations to the outer block in order to
1535 * maybe save a little bit of pushing and popping on the stack. It also takes
1536 * advantage of machines that use a register save mask on subroutine entry.
1538 STATIC I32 /* 0 failure, 1 success */
1539 S_regmatch(pTHX_ regnode *prog)
1542 register regnode *scan; /* Current node. */
1543 regnode *next; /* Next node. */
1544 regnode *inner; /* Next node in internal branch. */
1545 register I32 nextchr; /* renamed nextchr - nextchar colides with
1546 function of same name */
1547 register I32 n; /* no or next */
1548 register I32 ln; /* len or last */
1549 register char *s; /* operand or save */
1550 register char *locinput = PL_reginput;
1551 register I32 c1, c2, paren; /* case fold search, parenth */
1552 int minmod = 0, sw = 0, logical = 0;
1557 /* Note that nextchr is a byte even in UTF */
1558 nextchr = UCHARAT(locinput);
1560 while (scan != NULL) {
1561 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1563 # define sayYES goto yes
1564 # define sayNO goto no
1565 # define saySAME(x) if (x) goto yes; else goto no
1566 # define REPORT_CODE_OFF 24
1568 # define sayYES return 1
1569 # define sayNO return 0
1570 # define saySAME(x) return x
1573 SV *prop = sv_newmortal();
1574 int docolor = *PL_colors[0];
1575 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1576 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1577 /* The part of the string before starttry has one color
1578 (pref0_len chars), between starttry and current
1579 position another one (pref_len - pref0_len chars),
1580 after the current position the third one.
1581 We assume that pref0_len <= pref_len, otherwise we
1582 decrease pref0_len. */
1583 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1584 ? (5 + taill) - l : locinput - PL_bostr);
1585 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1587 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1588 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1589 ? (5 + taill) - pref_len : PL_regeol - locinput);
1592 if (pref0_len > pref_len)
1593 pref0_len = pref_len;
1594 regprop(prop, scan);
1595 PerlIO_printf(Perl_debug_log,
1596 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1597 locinput - PL_bostr,
1598 PL_colors[4], pref0_len,
1599 locinput - pref_len, PL_colors[5],
1600 PL_colors[2], pref_len - pref0_len,
1601 locinput - pref_len + pref0_len, PL_colors[3],
1602 (docolor ? "" : "> <"),
1603 PL_colors[0], l, locinput, PL_colors[1],
1604 15 - l - pref_len + 1,
1606 scan - PL_regprogram, PL_regindent*2, "",
1610 next = scan + NEXT_OFF(scan);
1616 if (locinput == PL_bostr
1617 ? PL_regprev == '\n'
1619 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1621 /* regtill = regbol; */
1626 if (locinput == PL_bostr
1627 ? PL_regprev == '\n'
1628 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1634 if (locinput == PL_regbol && PL_regprev == '\n')
1638 if (locinput == PL_reg_ganch)
1648 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1653 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1655 if (PL_regeol - locinput > 1)
1659 if (PL_regeol != locinput)
1663 if (nextchr & 0x80) {
1664 locinput += PL_utf8skip[nextchr];
1665 if (locinput > PL_regeol)
1667 nextchr = UCHARAT(locinput);
1670 if (!nextchr && locinput >= PL_regeol)
1672 nextchr = UCHARAT(++locinput);
1675 if (!nextchr && locinput >= PL_regeol)
1677 nextchr = UCHARAT(++locinput);
1680 if (nextchr & 0x80) {
1681 locinput += PL_utf8skip[nextchr];
1682 if (locinput > PL_regeol)
1684 nextchr = UCHARAT(locinput);
1687 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1689 nextchr = UCHARAT(++locinput);
1692 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1694 nextchr = UCHARAT(++locinput);
1699 /* Inline the first character, for speed. */
1700 if (UCHARAT(s) != nextchr)
1702 if (PL_regeol - locinput < ln)
1704 if (ln > 1 && memNE(s, locinput, ln))
1707 nextchr = UCHARAT(locinput);
1710 PL_reg_flags |= RF_tainted;
1719 c1 = OP(scan) == EXACTF;
1723 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1724 toLOWER_utf8((U8*)l) :
1725 toLOWER_LC_utf8((U8*)l)))
1733 nextchr = UCHARAT(locinput);
1737 /* Inline the first character, for speed. */
1738 if (UCHARAT(s) != nextchr &&
1739 UCHARAT(s) != ((OP(scan) == EXACTF)
1740 ? PL_fold : PL_fold_locale)[nextchr])
1742 if (PL_regeol - locinput < ln)
1744 if (ln > 1 && (OP(scan) == EXACTF
1745 ? ibcmp(s, locinput, ln)
1746 : ibcmp_locale(s, locinput, ln)))
1749 nextchr = UCHARAT(locinput);
1753 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1755 if (locinput >= PL_regeol)
1757 locinput += PL_utf8skip[nextchr];
1758 nextchr = UCHARAT(locinput);
1763 nextchr = UCHARAT(locinput);
1764 if (!REGINCLASS(s, nextchr))
1766 if (!nextchr && locinput >= PL_regeol)
1768 nextchr = UCHARAT(++locinput);
1771 PL_reg_flags |= RF_tainted;
1776 if (!(OP(scan) == ALNUM
1777 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1779 nextchr = UCHARAT(++locinput);
1782 PL_reg_flags |= RF_tainted;
1787 if (nextchr & 0x80) {
1788 if (!(OP(scan) == ALNUMUTF8
1789 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1790 : isALNUM_LC_utf8((U8*)locinput)))
1794 locinput += PL_utf8skip[nextchr];
1795 nextchr = UCHARAT(locinput);
1798 if (!(OP(scan) == ALNUMUTF8
1799 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1801 nextchr = UCHARAT(++locinput);
1804 PL_reg_flags |= RF_tainted;
1807 if (!nextchr && locinput >= PL_regeol)
1809 if (OP(scan) == NALNUM
1810 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1812 nextchr = UCHARAT(++locinput);
1815 PL_reg_flags |= RF_tainted;
1818 if (!nextchr && locinput >= PL_regeol)
1820 if (nextchr & 0x80) {
1821 if (OP(scan) == NALNUMUTF8
1822 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1823 : isALNUM_LC_utf8((U8*)locinput))
1827 locinput += PL_utf8skip[nextchr];
1828 nextchr = UCHARAT(locinput);
1831 if (OP(scan) == NALNUMUTF8
1832 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1834 nextchr = UCHARAT(++locinput);
1838 PL_reg_flags |= RF_tainted;
1842 /* was last char in word? */
1843 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1844 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1846 n = isALNUM(nextchr);
1849 ln = isALNUM_LC(ln);
1850 n = isALNUM_LC(nextchr);
1852 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1857 PL_reg_flags |= RF_tainted;
1861 /* was last char in word? */
1862 ln = (locinput != PL_regbol)
1863 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1864 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1865 ln = isALNUM_uni(ln);
1866 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1869 ln = isALNUM_LC_uni(ln);
1870 n = isALNUM_LC_utf8((U8*)locinput);
1872 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1876 PL_reg_flags |= RF_tainted;
1879 if (!nextchr && locinput >= PL_regeol)
1881 if (!(OP(scan) == SPACE
1882 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1884 nextchr = UCHARAT(++locinput);
1887 PL_reg_flags |= RF_tainted;
1890 if (!nextchr && locinput >= PL_regeol)
1892 if (nextchr & 0x80) {
1893 if (!(OP(scan) == SPACEUTF8
1894 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1895 : isSPACE_LC_utf8((U8*)locinput)))
1899 locinput += PL_utf8skip[nextchr];
1900 nextchr = UCHARAT(locinput);
1903 if (!(OP(scan) == SPACEUTF8
1904 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1906 nextchr = UCHARAT(++locinput);
1909 PL_reg_flags |= RF_tainted;
1914 if (OP(scan) == SPACE
1915 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1917 nextchr = UCHARAT(++locinput);
1920 PL_reg_flags |= RF_tainted;
1925 if (nextchr & 0x80) {
1926 if (OP(scan) == NSPACEUTF8
1927 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1928 : isSPACE_LC_utf8((U8*)locinput))
1932 locinput += PL_utf8skip[nextchr];
1933 nextchr = UCHARAT(locinput);
1936 if (OP(scan) == NSPACEUTF8
1937 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1939 nextchr = UCHARAT(++locinput);
1942 PL_reg_flags |= RF_tainted;
1945 if (!nextchr && locinput >= PL_regeol)
1947 if (!(OP(scan) == DIGIT
1948 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
1950 nextchr = UCHARAT(++locinput);
1953 PL_reg_flags |= RF_tainted;
1958 if (nextchr & 0x80) {
1959 if (OP(scan) == NDIGITUTF8
1960 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
1961 : isDIGIT_LC_utf8((U8*)locinput))
1965 locinput += PL_utf8skip[nextchr];
1966 nextchr = UCHARAT(locinput);
1969 if (!isDIGIT(nextchr))
1971 nextchr = UCHARAT(++locinput);
1974 PL_reg_flags |= RF_tainted;
1979 if (OP(scan) == DIGIT
1980 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
1982 nextchr = UCHARAT(++locinput);
1985 PL_reg_flags |= RF_tainted;
1988 if (!nextchr && locinput >= PL_regeol)
1990 if (nextchr & 0x80) {
1991 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1993 locinput += PL_utf8skip[nextchr];
1994 nextchr = UCHARAT(locinput);
1997 if (isDIGIT(nextchr))
1999 nextchr = UCHARAT(++locinput);
2002 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2004 locinput += PL_utf8skip[nextchr];
2005 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2006 locinput += UTF8SKIP(locinput);
2007 if (locinput > PL_regeol)
2009 nextchr = UCHARAT(locinput);
2012 PL_reg_flags |= RF_tainted;
2016 n = ARG(scan); /* which paren pair */
2017 ln = PL_regstartp[n];
2018 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2019 if (*PL_reglastparen < n || ln == -1)
2020 sayNO; /* Do not match unless seen CLOSEn. */
2021 if (ln == PL_regendp[n])
2025 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2027 char *e = PL_bostr + PL_regendp[n];
2029 * Note that we can't do the "other character" lookup trick as
2030 * in the 8-bit case (no pun intended) because in Unicode we
2031 * have to map both upper and title case to lower case.
2033 if (OP(scan) == REFF) {
2037 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2047 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2054 nextchr = UCHARAT(locinput);
2058 /* Inline the first character, for speed. */
2059 if (UCHARAT(s) != nextchr &&
2061 (UCHARAT(s) != ((OP(scan) == REFF
2062 ? PL_fold : PL_fold_locale)[nextchr]))))
2064 ln = PL_regendp[n] - ln;
2065 if (locinput + ln > PL_regeol)
2067 if (ln > 1 && (OP(scan) == REF
2068 ? memNE(s, locinput, ln)
2070 ? ibcmp(s, locinput, ln)
2071 : ibcmp_locale(s, locinput, ln))))
2074 nextchr = UCHARAT(locinput);
2085 OP_4tree *oop = PL_op;
2086 COP *ocurcop = PL_curcop;
2087 SV **ocurpad = PL_curpad;
2091 PL_op = (OP_4tree*)PL_regdata->data[n];
2092 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
2093 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2094 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2096 CALLRUNOPS(aTHX); /* Scalar context. */
2102 PL_curpad = ocurpad;
2103 PL_curcop = ocurcop;
2105 if (logical == 2) { /* Postponed subexpression. */
2107 MAGIC *mg = Null(MAGIC*);
2109 CHECKPOINT cp, lastcp;
2111 if(SvROK(ret) || SvRMAGICAL(ret)) {
2112 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2115 mg = mg_find(sv, 'r');
2118 re = (regexp *)mg->mg_obj;
2119 (void)ReREFCNT_inc(re);
2123 char *t = SvPV(ret, len);
2125 char *oprecomp = PL_regprecomp;
2126 I32 osize = PL_regsize;
2127 I32 onpar = PL_regnpar;
2130 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2132 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2133 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2134 PL_regprecomp = oprecomp;
2139 PerlIO_printf(Perl_debug_log,
2140 "Entering embedded `%s%.60s%s%s'\n",
2144 (strlen(re->precomp) > 60 ? "..." : ""))
2147 state.prev = PL_reg_call_cc;
2148 state.cc = PL_regcc;
2149 state.re = PL_reg_re;
2153 cp = regcppush(0); /* Save *all* the positions. */
2156 state.ss = PL_savestack_ix;
2157 *PL_reglastparen = 0;
2158 PL_reg_call_cc = &state;
2159 PL_reginput = locinput;
2161 /* XXXX This is too dramatic a measure... */
2164 if (regmatch(re->program + 1)) {
2170 PerlIO_printf(Perl_debug_log,
2172 REPORT_CODE_OFF+PL_regindent*2, "")
2177 PL_reg_call_cc = state.prev;
2178 PL_regcc = state.cc;
2179 PL_reg_re = state.re;
2180 cache_re(PL_reg_re);
2182 /* XXXX This is too dramatic a measure... */
2191 sv_setsv(save_scalar(PL_replgv), ret);
2195 n = ARG(scan); /* which paren pair */
2196 PL_reg_start_tmp[n] = locinput;
2201 n = ARG(scan); /* which paren pair */
2202 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2203 PL_regendp[n] = locinput - PL_bostr;
2204 if (n > *PL_reglastparen)
2205 *PL_reglastparen = n;
2208 n = ARG(scan); /* which paren pair */
2209 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2212 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2214 next = NEXTOPER(NEXTOPER(scan));
2216 next = scan + ARG(scan);
2217 if (OP(next) == IFTHEN) /* Fake one. */
2218 next = NEXTOPER(NEXTOPER(next));
2222 logical = scan->flags;
2224 /*******************************************************************
2225 PL_regcc contains infoblock about the innermost (...)* loop, and
2226 a pointer to the next outer infoblock.
2228 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2230 1) After matching X, regnode for CURLYX is processed;
2232 2) This regnode creates infoblock on the stack, and calls
2233 regmatch() recursively with the starting point at WHILEM node;
2235 3) Each hit of WHILEM node tries to match A and Z (in the order
2236 depending on the current iteration, min/max of {min,max} and
2237 greediness). The information about where are nodes for "A"
2238 and "Z" is read from the infoblock, as is info on how many times "A"
2239 was already matched, and greediness.
2241 4) After A matches, the same WHILEM node is hit again.
2243 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2244 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2245 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2246 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2247 of the external loop.
2249 Currently present infoblocks form a tree with a stem formed by PL_curcc
2250 and whatever it mentions via ->next, and additional attached trees
2251 corresponding to temporarily unset infoblocks as in "5" above.
2253 In the following picture infoblocks for outer loop of
2254 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2255 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2256 infoblocks are drawn below the "reset" infoblock.
2258 In fact in the picture below we do not show failed matches for Z and T
2259 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2260 more obvious *why* one needs to *temporary* unset infoblocks.]
2262 Matched REx position InfoBlocks Comment
2266 Y A)*?Z)*?T x <- O <- I
2267 YA )*?Z)*?T x <- O <- I
2268 YA A)*?Z)*?T x <- O <- I
2269 YAA )*?Z)*?T x <- O <- I
2270 YAA Z)*?T x <- O # Temporary unset I
2273 YAAZ Y(A)*?Z)*?T x <- O
2276 YAAZY (A)*?Z)*?T x <- O
2279 YAAZY A)*?Z)*?T x <- O <- I
2282 YAAZYA )*?Z)*?T x <- O <- I
2285 YAAZYA Z)*?T x <- O # Temporary unset I
2291 YAAZYAZ T x # Temporary unset O
2298 *******************************************************************/
2301 CHECKPOINT cp = PL_savestack_ix;
2303 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2305 cc.oldcc = PL_regcc;
2307 cc.parenfloor = *PL_reglastparen;
2309 cc.min = ARG1(scan);
2310 cc.max = ARG2(scan);
2311 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2315 PL_reginput = locinput;
2316 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2318 PL_regcc = cc.oldcc;
2324 * This is really hard to understand, because after we match
2325 * what we're trying to match, we must make sure the rest of
2326 * the REx is going to match for sure, and to do that we have
2327 * to go back UP the parse tree by recursing ever deeper. And
2328 * if it fails, we have to reset our parent's current state
2329 * that we can try again after backing off.
2332 CHECKPOINT cp, lastcp;
2333 CURCUR* cc = PL_regcc;
2334 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2336 n = cc->cur + 1; /* how many we know we matched */
2337 PL_reginput = locinput;
2340 PerlIO_printf(Perl_debug_log,
2341 "%*s %ld out of %ld..%ld cc=%lx\n",
2342 REPORT_CODE_OFF+PL_regindent*2, "",
2343 (long)n, (long)cc->min,
2344 (long)cc->max, (long)cc)
2347 /* If degenerate scan matches "", assume scan done. */
2349 if (locinput == cc->lastloc && n >= cc->min) {
2350 PL_regcc = cc->oldcc;
2354 PerlIO_printf(Perl_debug_log,
2355 "%*s empty match detected, try continuation...\n",
2356 REPORT_CODE_OFF+PL_regindent*2, "")
2358 if (regmatch(cc->next))
2361 PerlIO_printf(Perl_debug_log,
2363 REPORT_CODE_OFF+PL_regindent*2, "")
2371 /* First just match a string of min scans. */
2375 cc->lastloc = locinput;
2376 if (regmatch(cc->scan))
2379 cc->lastloc = lastloc;
2381 PerlIO_printf(Perl_debug_log,
2383 REPORT_CODE_OFF+PL_regindent*2, "")
2389 /* Check whether we already were at this position.
2390 Postpone detection until we know the match is not
2391 *that* much linear. */
2392 if (!PL_reg_maxiter) {
2393 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2394 PL_reg_leftiter = PL_reg_maxiter;
2396 if (PL_reg_leftiter-- == 0) {
2397 I32 size = (PL_reg_maxiter + 7)/8;
2398 if (PL_reg_poscache) {
2399 if (PL_reg_poscache_size < size) {
2400 Renew(PL_reg_poscache, size, char);
2401 PL_reg_poscache_size = size;
2403 Zero(PL_reg_poscache, size, char);
2406 PL_reg_poscache_size = size;
2407 Newz(29, PL_reg_poscache, size, char);
2410 PerlIO_printf(Perl_debug_log,
2411 "%sDetected a super-linear match, switching on caching%s...\n",
2412 PL_colors[4], PL_colors[5])
2415 if (PL_reg_leftiter < 0) {
2416 I32 o = locinput - PL_bostr, b;
2418 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2421 if (PL_reg_poscache[o] & (1<<b)) {
2423 PerlIO_printf(Perl_debug_log,
2424 "%*s already tried at this position...\n",
2425 REPORT_CODE_OFF+PL_regindent*2, "")
2429 PL_reg_poscache[o] |= (1<<b);
2433 /* Prefer next over scan for minimal matching. */
2436 PL_regcc = cc->oldcc;
2439 cp = regcppush(cc->parenfloor);
2441 if (regmatch(cc->next)) {
2443 sayYES; /* All done. */
2451 if (n >= cc->max) { /* Maximum greed exceeded? */
2452 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2453 && !(PL_reg_flags & RF_warned)) {
2454 PL_reg_flags |= RF_warned;
2455 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2456 "Complex regular subexpression recursion",
2463 PerlIO_printf(Perl_debug_log,
2464 "%*s trying longer...\n",
2465 REPORT_CODE_OFF+PL_regindent*2, "")
2467 /* Try scanning more and see if it helps. */
2468 PL_reginput = locinput;
2470 cc->lastloc = locinput;
2471 cp = regcppush(cc->parenfloor);
2473 if (regmatch(cc->scan)) {
2478 PerlIO_printf(Perl_debug_log,
2480 REPORT_CODE_OFF+PL_regindent*2, "")
2485 cc->lastloc = lastloc;
2489 /* Prefer scan over next for maximal matching. */
2491 if (n < cc->max) { /* More greed allowed? */
2492 cp = regcppush(cc->parenfloor);
2494 cc->lastloc = locinput;
2496 if (regmatch(cc->scan)) {
2501 regcppop(); /* Restore some previous $<digit>s? */
2502 PL_reginput = locinput;
2504 PerlIO_printf(Perl_debug_log,
2505 "%*s failed, try continuation...\n",
2506 REPORT_CODE_OFF+PL_regindent*2, "")
2509 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2510 && !(PL_reg_flags & RF_warned)) {
2511 PL_reg_flags |= RF_warned;
2512 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2513 "Complex regular subexpression recursion",
2517 /* Failed deeper matches of scan, so see if this one works. */
2518 PL_regcc = cc->oldcc;
2521 if (regmatch(cc->next))
2524 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
2525 REPORT_CODE_OFF+PL_regindent*2, "")
2531 cc->lastloc = lastloc;
2536 next = scan + ARG(scan);
2539 inner = NEXTOPER(NEXTOPER(scan));
2542 inner = NEXTOPER(scan);
2547 if (OP(next) != c1) /* No choice. */
2548 next = inner; /* Avoid recursion. */
2550 int lastparen = *PL_reglastparen;
2554 PL_reginput = locinput;
2555 if (regmatch(inner))
2558 for (n = *PL_reglastparen; n > lastparen; n--)
2560 *PL_reglastparen = n;
2563 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2567 inner = NEXTOPER(scan);
2568 if (c1 == BRANCHJ) {
2569 inner = NEXTOPER(inner);
2571 } while (scan != NULL && OP(scan) == c1);
2585 /* We suppose that the next guy does not need
2586 backtracking: in particular, it is of constant length,
2587 and has no parenths to influence future backrefs. */
2588 ln = ARG1(scan); /* min to match */
2589 n = ARG2(scan); /* max to match */
2590 paren = scan->flags;
2592 if (paren > PL_regsize)
2594 if (paren > *PL_reglastparen)
2595 *PL_reglastparen = paren;
2597 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2599 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2600 PL_reginput = locinput;
2603 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2605 if (ln && l == 0 && n >= ln
2606 /* In fact, this is tricky. If paren, then the
2607 fact that we did/didnot match may influence
2608 future execution. */
2609 && !(paren && ln == 0))
2611 locinput = PL_reginput;
2612 if (PL_regkind[(U8)OP(next)] == EXACT) {
2613 c1 = (U8)*STRING(next);
2614 if (OP(next) == EXACTF)
2616 else if (OP(next) == EXACTFL)
2617 c2 = PL_fold_locale[c1];
2624 /* This may be improved if l == 0. */
2625 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2626 /* If it could work, try it. */
2628 UCHARAT(PL_reginput) == c1 ||
2629 UCHARAT(PL_reginput) == c2)
2633 PL_regstartp[paren] =
2634 HOPc(PL_reginput, -l) - PL_bostr;
2635 PL_regendp[paren] = PL_reginput - PL_bostr;
2638 PL_regendp[paren] = -1;
2644 /* Couldn't or didn't -- move forward. */
2645 PL_reginput = locinput;
2646 if (regrepeat_hard(scan, 1, &l)) {
2648 locinput = PL_reginput;
2655 n = regrepeat_hard(scan, n, &l);
2656 if (n != 0 && l == 0
2657 /* In fact, this is tricky. If paren, then the
2658 fact that we did/didnot match may influence
2659 future execution. */
2660 && !(paren && ln == 0))
2662 locinput = PL_reginput;
2664 PerlIO_printf(Perl_debug_log,
2665 "%*s matched %ld times, len=%ld...\n",
2666 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2669 if (PL_regkind[(U8)OP(next)] == EXACT) {
2670 c1 = (U8)*STRING(next);
2671 if (OP(next) == EXACTF)
2673 else if (OP(next) == EXACTFL)
2674 c2 = PL_fold_locale[c1];
2683 /* If it could work, try it. */
2685 UCHARAT(PL_reginput) == c1 ||
2686 UCHARAT(PL_reginput) == c2)
2689 PerlIO_printf(Perl_debug_log,
2690 "%*s trying tail with n=%ld...\n",
2691 REPORT_CODE_OFF+PL_regindent*2, "", n)
2695 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2696 PL_regendp[paren] = PL_reginput - PL_bostr;
2699 PL_regendp[paren] = -1;
2705 /* Couldn't or didn't -- back up. */
2707 locinput = HOPc(locinput, -l);
2708 PL_reginput = locinput;
2715 paren = scan->flags; /* Which paren to set */
2716 if (paren > PL_regsize)
2718 if (paren > *PL_reglastparen)
2719 *PL_reglastparen = paren;
2720 ln = ARG1(scan); /* min to match */
2721 n = ARG2(scan); /* max to match */
2722 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2726 ln = ARG1(scan); /* min to match */
2727 n = ARG2(scan); /* max to match */
2728 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2733 scan = NEXTOPER(scan);
2739 scan = NEXTOPER(scan);
2743 * Lookahead to avoid useless match attempts
2744 * when we know what character comes next.
2746 if (PL_regkind[(U8)OP(next)] == EXACT) {
2747 c1 = (U8)*STRING(next);
2748 if (OP(next) == EXACTF)
2750 else if (OP(next) == EXACTFL)
2751 c2 = PL_fold_locale[c1];
2757 PL_reginput = locinput;
2761 if (ln && regrepeat(scan, ln) < ln)
2763 locinput = PL_reginput;
2766 char *e = locinput + n - ln; /* Should not check after this */
2767 char *old = locinput;
2769 if (e >= PL_regeol || (n == REG_INFTY))
2772 /* Find place 'next' could work */
2774 while (locinput <= e && *locinput != c1)
2777 while (locinput <= e
2784 /* PL_reginput == old now */
2785 if (locinput != old) {
2786 ln = 1; /* Did some */
2787 if (regrepeat(scan, locinput - old) <
2791 /* PL_reginput == locinput now */
2794 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2795 PL_regendp[paren] = locinput - PL_bostr;
2798 PL_regendp[paren] = -1;
2802 PL_reginput = locinput; /* Could be reset... */
2804 /* Couldn't or didn't -- move forward. */
2809 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2810 /* If it could work, try it. */
2812 UCHARAT(PL_reginput) == c1 ||
2813 UCHARAT(PL_reginput) == c2)
2817 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2818 PL_regendp[paren] = PL_reginput - PL_bostr;
2821 PL_regendp[paren] = -1;
2827 /* Couldn't or didn't -- move forward. */
2828 PL_reginput = locinput;
2829 if (regrepeat(scan, 1)) {
2831 locinput = PL_reginput;
2839 n = regrepeat(scan, n);
2840 locinput = PL_reginput;
2841 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2842 (!PL_multiline || OP(next) == SEOL))
2843 ln = n; /* why back off? */
2847 /* If it could work, try it. */
2849 UCHARAT(PL_reginput) == c1 ||
2850 UCHARAT(PL_reginput) == c2)
2854 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2855 PL_regendp[paren] = PL_reginput - PL_bostr;
2858 PL_regendp[paren] = -1;
2864 /* Couldn't or didn't -- back up. */
2866 PL_reginput = locinput = HOPc(locinput, -1);
2871 /* If it could work, try it. */
2873 UCHARAT(PL_reginput) == c1 ||
2874 UCHARAT(PL_reginput) == c2)
2880 /* Couldn't or didn't -- back up. */
2882 PL_reginput = locinput = HOPc(locinput, -1);
2889 if (PL_reg_call_cc) {
2890 re_cc_state *cur_call_cc = PL_reg_call_cc;
2891 CURCUR *cctmp = PL_regcc;
2892 regexp *re = PL_reg_re;
2893 CHECKPOINT cp, lastcp;
2895 cp = regcppush(0); /* Save *all* the positions. */
2897 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2899 PL_reginput = locinput; /* Make position available to
2901 cache_re(PL_reg_call_cc->re);
2902 PL_regcc = PL_reg_call_cc->cc;
2903 PL_reg_call_cc = PL_reg_call_cc->prev;
2904 if (regmatch(cur_call_cc->node)) {
2905 PL_reg_call_cc = cur_call_cc;
2911 PL_reg_call_cc = cur_call_cc;
2917 PerlIO_printf(Perl_debug_log,
2918 "%*s continuation failed...\n",
2919 REPORT_CODE_OFF+PL_regindent*2, "")
2923 if (locinput < PL_regtill)
2924 sayNO; /* Cannot match: too short. */
2927 PL_reginput = locinput; /* put where regtry can find it */
2928 sayYES; /* Success! */
2931 PL_reginput = locinput;
2936 if (UTF) { /* XXXX This is absolutely
2937 broken, we read before
2939 s = HOPMAYBEc(locinput, -scan->flags);
2945 if (locinput < PL_bostr + scan->flags)
2947 PL_reginput = locinput - scan->flags;
2952 PL_reginput = locinput;
2957 if (UTF) { /* XXXX This is absolutely
2958 broken, we read before
2960 s = HOPMAYBEc(locinput, -scan->flags);
2961 if (!s || s < PL_bostr)
2966 if (locinput < PL_bostr + scan->flags)
2968 PL_reginput = locinput - scan->flags;
2973 PL_reginput = locinput;
2976 inner = NEXTOPER(NEXTOPER(scan));
2977 if (regmatch(inner) != n) {
2992 if (OP(scan) == SUSPEND) {
2993 locinput = PL_reginput;
2994 nextchr = UCHARAT(locinput);
2999 next = scan + ARG(scan);
3004 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
3005 (unsigned long)scan, OP(scan));
3006 Perl_croak(aTHX_ "regexp memory corruption");
3012 * We get here only if there's trouble -- normally "case END" is
3013 * the terminating point.
3015 Perl_croak(aTHX_ "corrupted regexp pointers");
3033 - regrepeat - repeatedly match something simple, report how many
3036 * [This routine now assumes that it will only match on things of length 1.
3037 * That was true before, but now we assume scan - reginput is the count,
3038 * rather than incrementing count on every character. [Er, except utf8.]]
3041 S_regrepeat(pTHX_ regnode *p, I32 max)
3044 register char *scan;
3045 register char *opnd;
3047 register char *loceol = PL_regeol;
3048 register I32 hardcount = 0;
3051 if (max != REG_INFTY && max < loceol - scan)
3052 loceol = scan + max;
3055 while (scan < loceol && *scan != '\n')
3063 while (scan < loceol && *scan != '\n') {
3064 scan += UTF8SKIP(scan);
3070 while (scan < loceol) {
3071 scan += UTF8SKIP(scan);
3075 case EXACT: /* length of string is 1 */
3077 while (scan < loceol && UCHARAT(scan) == c)
3080 case EXACTF: /* length of string is 1 */
3082 while (scan < loceol &&
3083 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3086 case EXACTFL: /* length of string is 1 */
3087 PL_reg_flags |= RF_tainted;
3089 while (scan < loceol &&
3090 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3095 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3096 scan += UTF8SKIP(scan);
3102 while (scan < loceol && REGINCLASS(opnd, *scan))
3106 while (scan < loceol && isALNUM(*scan))
3111 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3112 scan += UTF8SKIP(scan);
3117 PL_reg_flags |= RF_tainted;
3118 while (scan < loceol && isALNUM_LC(*scan))
3122 PL_reg_flags |= RF_tainted;
3124 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3125 scan += UTF8SKIP(scan);
3131 while (scan < loceol && !isALNUM(*scan))
3136 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3137 scan += UTF8SKIP(scan);
3142 PL_reg_flags |= RF_tainted;
3143 while (scan < loceol && !isALNUM_LC(*scan))
3147 PL_reg_flags |= RF_tainted;
3149 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3150 scan += UTF8SKIP(scan);
3155 while (scan < loceol && isSPACE(*scan))
3160 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3161 scan += UTF8SKIP(scan);
3166 PL_reg_flags |= RF_tainted;
3167 while (scan < loceol && isSPACE_LC(*scan))
3171 PL_reg_flags |= RF_tainted;
3173 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3174 scan += UTF8SKIP(scan);
3179 while (scan < loceol && !isSPACE(*scan))
3184 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3185 scan += UTF8SKIP(scan);
3190 PL_reg_flags |= RF_tainted;
3191 while (scan < loceol && !isSPACE_LC(*scan))
3195 PL_reg_flags |= RF_tainted;
3197 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3198 scan += UTF8SKIP(scan);
3203 while (scan < loceol && isDIGIT(*scan))
3208 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3209 scan += UTF8SKIP(scan);
3215 while (scan < loceol && !isDIGIT(*scan))
3220 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3221 scan += UTF8SKIP(scan);
3225 default: /* Called on something of 0 width. */
3226 break; /* So match right here or not at all. */
3232 c = scan - PL_reginput;
3237 SV *prop = sv_newmortal();
3240 PerlIO_printf(Perl_debug_log,
3241 "%*s %s can match %ld times out of %ld...\n",
3242 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
3249 - regrepeat_hard - repeatedly match something, report total lenth and length
3251 * The repeater is supposed to have constant length.
3255 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3258 register char *scan;
3259 register char *start;
3260 register char *loceol = PL_regeol;
3262 I32 count = 0, res = 1;
3267 start = PL_reginput;
3269 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3272 while (start < PL_reginput) {
3274 start += UTF8SKIP(start);
3285 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3287 *lp = l = PL_reginput - start;
3288 if (max != REG_INFTY && l*max < loceol - scan)
3289 loceol = scan + l*max;
3302 - reginclass - determine if a character falls into a character class
3306 S_reginclass(pTHX_ register char *p, register I32 c)
3309 char flags = ANYOF_FLAGS(p);
3313 if (ANYOF_BITMAP_TEST(p, c))
3315 else if (flags & ANYOF_FOLD) {
3317 if (flags & ANYOF_LOCALE) {
3318 PL_reg_flags |= RF_tainted;
3319 cf = PL_fold_locale[c];
3323 if (ANYOF_BITMAP_TEST(p, cf))
3327 if (!match && (flags & ANYOF_CLASS)) {
3328 PL_reg_flags |= RF_tainted;
3330 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3331 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3332 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3333 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3334 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3335 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3336 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3337 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3338 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3339 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3340 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3341 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3342 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3343 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3344 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3345 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3346 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3347 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3348 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3349 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3350 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3351 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3352 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3353 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3354 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3355 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3356 ) /* How's that for a conditional? */
3362 return (flags & ANYOF_INVERT) ? !match : match;
3366 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3369 char flags = ARG1(f);
3371 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3373 if (swash_fetch(sv, p))
3375 else if (flags & ANYOF_FOLD) {
3378 if (flags & ANYOF_LOCALE) {
3379 PL_reg_flags |= RF_tainted;
3380 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3383 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3384 if (swash_fetch(sv, tmpbuf))
3388 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3390 return (flags & ANYOF_INVERT) ? !match : match;
3394 S_reghop(pTHX_ U8 *s, I32 off)
3398 while (off-- && s < (U8*)PL_regeol)
3403 if (s > (U8*)PL_bostr) {
3406 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3408 } /* XXX could check well-formedness here */
3416 S_reghopmaybe(pTHX_ U8* s, I32 off)
3420 while (off-- && s < (U8*)PL_regeol)
3427 if (s > (U8*)PL_bostr) {
3430 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3432 } /* XXX could check well-formedness here */
3449 restore_pos(pTHXo_ void *arg)
3452 if (PL_reg_eval_set) {
3453 if (PL_reg_oldsaved) {
3454 PL_reg_re->subbeg = PL_reg_oldsaved;
3455 PL_reg_re->sublen = PL_reg_oldsavedlen;
3456 RX_MATCH_COPIED_on(PL_reg_re);
3458 PL_reg_magic->mg_len = PL_reg_oldpos;
3459 PL_reg_eval_set = 0;
3460 PL_curpm = PL_reg_oldcurpm;