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 */
646 I32 start_shift = 0; /* Offset of the start to find
647 constant substr. */ /* CC */
648 I32 end_shift = 0; /* Same for the end. */ /* CC */
649 I32 scream_pos = -1; /* Internal iterator of scream. */
651 SV* oreplsv = GvSV(PL_replgv);
659 PL_regnarrate = PL_debug & 512;
663 if (prog == NULL || startpos == NULL) {
664 Perl_croak(aTHX_ "NULL regexp parameter");
668 minlen = prog->minlen;
669 if (strend - startpos < minlen) goto phooey;
671 if (startpos == strbeg) /* is ^ valid at stringarg? */
674 PL_regprev = (U32)stringarg[-1];
675 if (!PL_multiline && PL_regprev == '\n')
676 PL_regprev = '\0'; /* force ^ to NOT match */
679 /* Check validity of program. */
680 if (UCHARAT(prog->program) != REG_MAGIC) {
681 Perl_croak(aTHX_ "corrupted regexp program");
688 if (prog->reganch & ROPT_UTF8)
689 PL_reg_flags |= RF_utf8;
691 /* Mark beginning of line for ^ and lookbehind. */
692 PL_regbol = startpos;
696 /* Mark end of line for $ (and such) */
699 /* see how far we have to get to not match where we matched before */
700 PL_regtill = startpos+minend;
702 /* We start without call_cc context. */
705 /* If there is a "must appear" string, look for it. */
708 if (prog->reganch & ROPT_GPOS_SEEN) {
711 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
712 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
713 PL_reg_ganch = strbeg + mg->mg_len;
715 PL_reg_ganch = startpos;
716 if (prog->reganch & ROPT_ANCH_GPOS) {
717 if (s > PL_reg_ganch)
723 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
724 re_scream_pos_data d;
726 d.scream_olds = &scream_olds;
727 d.scream_pos = &scream_pos;
728 s = re_intuit_start(prog, sv, s, strend, flags, &d);
730 goto phooey; /* not present */
733 DEBUG_r( if (!PL_colorset) reginitcolors() );
734 DEBUG_r(PerlIO_printf(Perl_debug_log,
735 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
736 PL_colors[4],PL_colors[5],PL_colors[0],
739 (strlen(prog->precomp) > 60 ? "..." : ""),
741 (strend - startpos > 60 ? 60 : strend - startpos),
742 startpos, PL_colors[1],
743 (strend - startpos > 60 ? "..." : ""))
746 /* Simplest case: anchored match need be tried only once. */
747 /* [unless only anchor is BOL and multiline is set] */
748 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
749 if (s == startpos && regtry(prog, startpos))
751 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
752 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
757 dontbother = minlen - 1;
758 end = HOPc(strend, -dontbother) - 1;
759 /* for multiline we only have to try after newlines */
760 if (prog->check_substr) {
766 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
774 if (*s++ == '\n') { /* don't need PL_utf8skip here */
782 } else if (prog->reganch & ROPT_ANCH_GPOS) {
783 if (regtry(prog, PL_reg_ganch))
788 /* Messy cases: unanchored match. */
789 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
790 /* we have /x+whatever/ */
791 /* it must be a one character string (XXXX Except UTF?) */
792 char ch = SvPVX(prog->anchored_substr)[0];
796 if (regtry(prog, s)) goto got_it;
798 while (s < strend && *s == ch)
807 if (regtry(prog, s)) goto got_it;
809 while (s < strend && *s == ch)
817 else if (prog->anchored_substr != Nullsv
818 || (prog->float_substr != Nullsv
819 && prog->float_max_offset < strend - s)) {
820 SV *must = prog->anchored_substr
821 ? prog->anchored_substr : prog->float_substr;
823 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
825 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
826 I32 delta = back_max - back_min;
827 char *last = HOPc(strend, /* Cannot start after this */
828 -(I32)(CHR_SVLEN(must)
829 - (SvTAIL(must) != 0) + back_min));
830 char *last1; /* Last position checked before */
835 last1 = s - 1; /* bogus */
837 /* XXXX check_substr already used to find `s', can optimize if
838 check_substr==must. */
840 dontbother = end_shift;
841 strend = HOPc(strend, -dontbother);
842 while ( (s <= last) &&
843 ((flags & REXEC_SCREAM)
844 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
845 end_shift, &scream_pos, 0))
846 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
847 (unsigned char*)strend, must,
848 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
849 if (HOPc(s, -back_max) > last1) {
850 last1 = HOPc(s, -back_min);
851 s = HOPc(s, -back_max);
854 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
856 last1 = HOPc(s, -back_min);
876 else if (c = prog->regstclass) {
877 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
881 dontbother = minlen - 1;
882 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
884 /* We know what class it must start with. */
889 if (REGINCLASSUTF8(c, (U8*)s)) {
890 if (tmp && regtry(prog, s))
903 if (REGINCLASS(cc, *s)) {
904 if (tmp && regtry(prog, s))
915 PL_reg_flags |= RF_tainted;
922 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
923 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
925 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
932 if ((minlen || tmp) && regtry(prog,s))
936 PL_reg_flags |= RF_tainted;
941 strend = reghop_c(strend, -1);
943 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
944 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
946 if (tmp == !(OP(c) == BOUND ?
947 swash_fetch(PL_utf8_alnum, (U8*)s) :
948 isALNUM_LC_utf8((U8*)s)))
956 if ((minlen || tmp) && regtry(prog,s))
960 PL_reg_flags |= RF_tainted;
967 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
968 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
970 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
972 else if (regtry(prog, s))
976 if ((minlen || !tmp) && regtry(prog,s))
980 PL_reg_flags |= RF_tainted;
985 strend = reghop_c(strend, -1);
987 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
988 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
990 if (tmp == !(OP(c) == NBOUND ?
991 swash_fetch(PL_utf8_alnum, (U8*)s) :
992 isALNUM_LC_utf8((U8*)s)))
994 else if (regtry(prog, s))
998 if ((minlen || !tmp) && regtry(prog,s))
1002 while (s < strend) {
1004 if (tmp && regtry(prog, s))
1015 while (s < strend) {
1016 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1017 if (tmp && regtry(prog, s))
1028 PL_reg_flags |= RF_tainted;
1029 while (s < strend) {
1030 if (isALNUM_LC(*s)) {
1031 if (tmp && regtry(prog, s))
1042 PL_reg_flags |= RF_tainted;
1043 while (s < strend) {
1044 if (isALNUM_LC_utf8((U8*)s)) {
1045 if (tmp && regtry(prog, s))
1056 while (s < strend) {
1058 if (tmp && regtry(prog, s))
1069 while (s < strend) {
1070 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1071 if (tmp && regtry(prog, s))
1082 PL_reg_flags |= RF_tainted;
1083 while (s < strend) {
1084 if (!isALNUM_LC(*s)) {
1085 if (tmp && regtry(prog, s))
1096 PL_reg_flags |= RF_tainted;
1097 while (s < strend) {
1098 if (!isALNUM_LC_utf8((U8*)s)) {
1099 if (tmp && regtry(prog, s))
1110 while (s < strend) {
1112 if (tmp && regtry(prog, s))
1123 while (s < strend) {
1124 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1125 if (tmp && regtry(prog, s))
1136 PL_reg_flags |= RF_tainted;
1137 while (s < strend) {
1138 if (isSPACE_LC(*s)) {
1139 if (tmp && regtry(prog, s))
1150 PL_reg_flags |= RF_tainted;
1151 while (s < strend) {
1152 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1153 if (tmp && regtry(prog, s))
1164 while (s < strend) {
1166 if (tmp && regtry(prog, s))
1177 while (s < strend) {
1178 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1179 if (tmp && regtry(prog, s))
1190 PL_reg_flags |= RF_tainted;
1191 while (s < strend) {
1192 if (!isSPACE_LC(*s)) {
1193 if (tmp && regtry(prog, s))
1204 PL_reg_flags |= RF_tainted;
1205 while (s < strend) {
1206 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1207 if (tmp && regtry(prog, s))
1218 while (s < strend) {
1220 if (tmp && regtry(prog, s))
1231 while (s < strend) {
1232 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1233 if (tmp && regtry(prog, s))
1244 PL_reg_flags |= RF_tainted;
1245 while (s < strend) {
1246 if (isDIGIT_LC(*s)) {
1247 if (tmp && regtry(prog, s))
1258 PL_reg_flags |= RF_tainted;
1259 while (s < strend) {
1260 if (isDIGIT_LC_utf8((U8*)s)) {
1261 if (tmp && regtry(prog, s))
1272 while (s < strend) {
1274 if (tmp && regtry(prog, s))
1285 while (s < strend) {
1286 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1287 if (tmp && regtry(prog, s))
1298 PL_reg_flags |= RF_tainted;
1299 while (s < strend) {
1300 if (!isDIGIT_LC(*s)) {
1301 if (tmp && regtry(prog, s))
1312 PL_reg_flags |= RF_tainted;
1313 while (s < strend) {
1314 if (!isDIGIT_LC_utf8((U8*)s)) {
1315 if (tmp && regtry(prog, s))
1329 if (prog->float_substr != Nullsv) { /* Trim the end. */
1331 I32 oldpos = scream_pos;
1333 if (flags & REXEC_SCREAM) {
1334 last = screaminstr(sv, prog->float_substr, s - strbeg,
1335 end_shift, &scream_pos, 1); /* last one */
1337 last = scream_olds; /* Only one occurence. */
1341 char *little = SvPV(prog->float_substr, len);
1343 if (SvTAIL(prog->float_substr)) {
1344 if (memEQ(strend - len + 1, little, len - 1))
1345 last = strend - len + 1;
1346 else if (!PL_multiline)
1347 last = memEQ(strend - len, little, len)
1348 ? strend - len : Nullch;
1354 last = rninstr(s, strend, little, little + len);
1356 last = strend; /* matching `$' */
1359 if (last == NULL) goto phooey; /* Should not happen! */
1360 dontbother = strend - last + prog->float_min_offset;
1362 if (minlen && (dontbother < minlen))
1363 dontbother = minlen - 1;
1364 strend -= dontbother; /* this one's always in bytes! */
1365 /* We don't know much -- general case. */
1368 if (regtry(prog, s))
1377 if (regtry(prog, s))
1379 } while (s++ < strend);
1387 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1389 if (PL_reg_eval_set) {
1390 /* Preserve the current value of $^R */
1391 if (oreplsv != GvSV(PL_replgv))
1392 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1393 restored, the value remains
1395 restore_pos(aTHXo_ 0);
1398 /* make sure $`, $&, $', and $digit will work later */
1399 if ( !(flags & REXEC_NOT_FIRST) ) {
1400 if (RX_MATCH_COPIED(prog)) {
1401 Safefree(prog->subbeg);
1402 RX_MATCH_COPIED_off(prog);
1404 if (flags & REXEC_COPY_STR) {
1405 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1407 s = savepvn(strbeg, i);
1410 RX_MATCH_COPIED_on(prog);
1413 prog->subbeg = strbeg;
1414 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1421 if (PL_reg_eval_set)
1422 restore_pos(aTHXo_ 0);
1427 - regtry - try match at specific point
1429 STATIC I32 /* 0 failure, 1 success */
1430 S_regtry(pTHX_ regexp *prog, char *startpos)
1438 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1441 PL_reg_eval_set = RS_init;
1443 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1444 PL_stack_sp - PL_stack_base);
1446 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1447 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1448 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1450 /* Apparently this is not needed, judging by wantarray. */
1451 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1452 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1455 /* Make $_ available to executed code. */
1456 if (PL_reg_sv != DEFSV) {
1457 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1462 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1463 && (mg = mg_find(PL_reg_sv, 'g')))) {
1464 /* prepare for quick setting of pos */
1465 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1466 mg = mg_find(PL_reg_sv, 'g');
1470 PL_reg_oldpos = mg->mg_len;
1471 SAVEDESTRUCTOR(restore_pos, 0);
1474 New(22,PL_reg_curpm, 1, PMOP);
1475 PL_reg_curpm->op_pmregexp = prog;
1476 PL_reg_oldcurpm = PL_curpm;
1477 PL_curpm = PL_reg_curpm;
1478 if (RX_MATCH_COPIED(prog)) {
1479 /* Here is a serious problem: we cannot rewrite subbeg,
1480 since it may be needed if this match fails. Thus
1481 $` inside (?{}) could fail... */
1482 PL_reg_oldsaved = prog->subbeg;
1483 PL_reg_oldsavedlen = prog->sublen;
1484 RX_MATCH_COPIED_off(prog);
1487 PL_reg_oldsaved = Nullch;
1488 prog->subbeg = PL_bostr;
1489 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1491 prog->startp[0] = startpos - PL_bostr;
1492 PL_reginput = startpos;
1493 PL_regstartp = prog->startp;
1494 PL_regendp = prog->endp;
1495 PL_reglastparen = &prog->lastparen;
1496 prog->lastparen = 0;
1498 DEBUG_r(PL_reg_starttry = startpos);
1499 if (PL_reg_start_tmpl <= prog->nparens) {
1500 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1501 if(PL_reg_start_tmp)
1502 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1504 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1507 /* XXXX What this code is doing here?!!! There should be no need
1508 to do this again and again, PL_reglastparen should take care of
1512 if (prog->nparens) {
1513 for (i = prog->nparens; i >= 1; i--) {
1519 if (regmatch(prog->program + 1)) {
1520 prog->endp[0] = PL_reginput - PL_bostr;
1528 - regmatch - main matching routine
1530 * Conceptually the strategy is simple: check to see whether the current
1531 * node matches, call self recursively to see whether the rest matches,
1532 * and then act accordingly. In practice we make some effort to avoid
1533 * recursion, in particular by going through "ordinary" nodes (that don't
1534 * need to know whether the rest of the match failed) by a loop instead of
1537 /* [lwall] I've hoisted the register declarations to the outer block in order to
1538 * maybe save a little bit of pushing and popping on the stack. It also takes
1539 * advantage of machines that use a register save mask on subroutine entry.
1541 STATIC I32 /* 0 failure, 1 success */
1542 S_regmatch(pTHX_ regnode *prog)
1545 register regnode *scan; /* Current node. */
1546 regnode *next; /* Next node. */
1547 regnode *inner; /* Next node in internal branch. */
1548 register I32 nextchr; /* renamed nextchr - nextchar colides with
1549 function of same name */
1550 register I32 n; /* no or next */
1551 register I32 ln; /* len or last */
1552 register char *s; /* operand or save */
1553 register char *locinput = PL_reginput;
1554 register I32 c1, c2, paren; /* case fold search, parenth */
1555 int minmod = 0, sw = 0, logical = 0;
1560 /* Note that nextchr is a byte even in UTF */
1561 nextchr = UCHARAT(locinput);
1563 while (scan != NULL) {
1564 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1566 # define sayYES goto yes
1567 # define sayNO goto no
1568 # define saySAME(x) if (x) goto yes; else goto no
1569 # define REPORT_CODE_OFF 24
1571 # define sayYES return 1
1572 # define sayNO return 0
1573 # define saySAME(x) return x
1576 SV *prop = sv_newmortal();
1577 int docolor = *PL_colors[0];
1578 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1579 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1580 /* The part of the string before starttry has one color
1581 (pref0_len chars), between starttry and current
1582 position another one (pref_len - pref0_len chars),
1583 after the current position the third one.
1584 We assume that pref0_len <= pref_len, otherwise we
1585 decrease pref0_len. */
1586 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1587 ? (5 + taill) - l : locinput - PL_bostr);
1588 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1590 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1591 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1592 ? (5 + taill) - pref_len : PL_regeol - locinput);
1595 if (pref0_len > pref_len)
1596 pref0_len = pref_len;
1597 regprop(prop, scan);
1598 PerlIO_printf(Perl_debug_log,
1599 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1600 locinput - PL_bostr,
1601 PL_colors[4], pref0_len,
1602 locinput - pref_len, PL_colors[5],
1603 PL_colors[2], pref_len - pref0_len,
1604 locinput - pref_len + pref0_len, PL_colors[3],
1605 (docolor ? "" : "> <"),
1606 PL_colors[0], l, locinput, PL_colors[1],
1607 15 - l - pref_len + 1,
1609 scan - PL_regprogram, PL_regindent*2, "",
1613 next = scan + NEXT_OFF(scan);
1619 if (locinput == PL_bostr
1620 ? PL_regprev == '\n'
1622 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1624 /* regtill = regbol; */
1629 if (locinput == PL_bostr
1630 ? PL_regprev == '\n'
1631 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1637 if (locinput == PL_regbol && PL_regprev == '\n')
1641 if (locinput == PL_reg_ganch)
1651 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1656 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1658 if (PL_regeol - locinput > 1)
1662 if (PL_regeol != locinput)
1666 if (nextchr & 0x80) {
1667 locinput += PL_utf8skip[nextchr];
1668 if (locinput > PL_regeol)
1670 nextchr = UCHARAT(locinput);
1673 if (!nextchr && locinput >= PL_regeol)
1675 nextchr = UCHARAT(++locinput);
1678 if (!nextchr && locinput >= PL_regeol)
1680 nextchr = UCHARAT(++locinput);
1683 if (nextchr & 0x80) {
1684 locinput += PL_utf8skip[nextchr];
1685 if (locinput > PL_regeol)
1687 nextchr = UCHARAT(locinput);
1690 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1692 nextchr = UCHARAT(++locinput);
1695 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1697 nextchr = UCHARAT(++locinput);
1702 /* Inline the first character, for speed. */
1703 if (UCHARAT(s) != nextchr)
1705 if (PL_regeol - locinput < ln)
1707 if (ln > 1 && memNE(s, locinput, ln))
1710 nextchr = UCHARAT(locinput);
1713 PL_reg_flags |= RF_tainted;
1722 c1 = OP(scan) == EXACTF;
1726 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1727 toLOWER_utf8((U8*)l) :
1728 toLOWER_LC_utf8((U8*)l)))
1736 nextchr = UCHARAT(locinput);
1740 /* Inline the first character, for speed. */
1741 if (UCHARAT(s) != nextchr &&
1742 UCHARAT(s) != ((OP(scan) == EXACTF)
1743 ? PL_fold : PL_fold_locale)[nextchr])
1745 if (PL_regeol - locinput < ln)
1747 if (ln > 1 && (OP(scan) == EXACTF
1748 ? ibcmp(s, locinput, ln)
1749 : ibcmp_locale(s, locinput, ln)))
1752 nextchr = UCHARAT(locinput);
1756 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1758 if (locinput >= PL_regeol)
1760 locinput += PL_utf8skip[nextchr];
1761 nextchr = UCHARAT(locinput);
1766 nextchr = UCHARAT(locinput);
1767 if (!REGINCLASS(s, nextchr))
1769 if (!nextchr && locinput >= PL_regeol)
1771 nextchr = UCHARAT(++locinput);
1774 PL_reg_flags |= RF_tainted;
1779 if (!(OP(scan) == ALNUM
1780 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1782 nextchr = UCHARAT(++locinput);
1785 PL_reg_flags |= RF_tainted;
1790 if (nextchr & 0x80) {
1791 if (!(OP(scan) == ALNUMUTF8
1792 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1793 : isALNUM_LC_utf8((U8*)locinput)))
1797 locinput += PL_utf8skip[nextchr];
1798 nextchr = UCHARAT(locinput);
1801 if (!(OP(scan) == ALNUMUTF8
1802 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1804 nextchr = UCHARAT(++locinput);
1807 PL_reg_flags |= RF_tainted;
1810 if (!nextchr && locinput >= PL_regeol)
1812 if (OP(scan) == NALNUM
1813 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1815 nextchr = UCHARAT(++locinput);
1818 PL_reg_flags |= RF_tainted;
1821 if (!nextchr && locinput >= PL_regeol)
1823 if (nextchr & 0x80) {
1824 if (OP(scan) == NALNUMUTF8
1825 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1826 : isALNUM_LC_utf8((U8*)locinput))
1830 locinput += PL_utf8skip[nextchr];
1831 nextchr = UCHARAT(locinput);
1834 if (OP(scan) == NALNUMUTF8
1835 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1837 nextchr = UCHARAT(++locinput);
1841 PL_reg_flags |= RF_tainted;
1845 /* was last char in word? */
1846 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1847 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1849 n = isALNUM(nextchr);
1852 ln = isALNUM_LC(ln);
1853 n = isALNUM_LC(nextchr);
1855 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1860 PL_reg_flags |= RF_tainted;
1864 /* was last char in word? */
1865 ln = (locinput != PL_regbol)
1866 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1867 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1868 ln = isALNUM_uni(ln);
1869 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1872 ln = isALNUM_LC_uni(ln);
1873 n = isALNUM_LC_utf8((U8*)locinput);
1875 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1879 PL_reg_flags |= RF_tainted;
1882 if (!nextchr && locinput >= PL_regeol)
1884 if (!(OP(scan) == SPACE
1885 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1887 nextchr = UCHARAT(++locinput);
1890 PL_reg_flags |= RF_tainted;
1893 if (!nextchr && locinput >= PL_regeol)
1895 if (nextchr & 0x80) {
1896 if (!(OP(scan) == SPACEUTF8
1897 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1898 : isSPACE_LC_utf8((U8*)locinput)))
1902 locinput += PL_utf8skip[nextchr];
1903 nextchr = UCHARAT(locinput);
1906 if (!(OP(scan) == SPACEUTF8
1907 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1909 nextchr = UCHARAT(++locinput);
1912 PL_reg_flags |= RF_tainted;
1917 if (OP(scan) == SPACE
1918 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1920 nextchr = UCHARAT(++locinput);
1923 PL_reg_flags |= RF_tainted;
1928 if (nextchr & 0x80) {
1929 if (OP(scan) == NSPACEUTF8
1930 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1931 : isSPACE_LC_utf8((U8*)locinput))
1935 locinput += PL_utf8skip[nextchr];
1936 nextchr = UCHARAT(locinput);
1939 if (OP(scan) == NSPACEUTF8
1940 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1942 nextchr = UCHARAT(++locinput);
1945 PL_reg_flags |= RF_tainted;
1948 if (!nextchr && locinput >= PL_regeol)
1950 if (!(OP(scan) == DIGIT
1951 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
1953 nextchr = UCHARAT(++locinput);
1956 PL_reg_flags |= RF_tainted;
1961 if (nextchr & 0x80) {
1962 if (OP(scan) == NDIGITUTF8
1963 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
1964 : isDIGIT_LC_utf8((U8*)locinput))
1968 locinput += PL_utf8skip[nextchr];
1969 nextchr = UCHARAT(locinput);
1972 if (!isDIGIT(nextchr))
1974 nextchr = UCHARAT(++locinput);
1977 PL_reg_flags |= RF_tainted;
1982 if (OP(scan) == DIGIT
1983 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
1985 nextchr = UCHARAT(++locinput);
1988 PL_reg_flags |= RF_tainted;
1991 if (!nextchr && locinput >= PL_regeol)
1993 if (nextchr & 0x80) {
1994 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1996 locinput += PL_utf8skip[nextchr];
1997 nextchr = UCHARAT(locinput);
2000 if (isDIGIT(nextchr))
2002 nextchr = UCHARAT(++locinput);
2005 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2007 locinput += PL_utf8skip[nextchr];
2008 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2009 locinput += UTF8SKIP(locinput);
2010 if (locinput > PL_regeol)
2012 nextchr = UCHARAT(locinput);
2015 PL_reg_flags |= RF_tainted;
2019 n = ARG(scan); /* which paren pair */
2020 ln = PL_regstartp[n];
2021 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2022 if (*PL_reglastparen < n || ln == -1)
2023 sayNO; /* Do not match unless seen CLOSEn. */
2024 if (ln == PL_regendp[n])
2028 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
2030 char *e = PL_bostr + PL_regendp[n];
2032 * Note that we can't do the "other character" lookup trick as
2033 * in the 8-bit case (no pun intended) because in Unicode we
2034 * have to map both upper and title case to lower case.
2036 if (OP(scan) == REFF) {
2040 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2050 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2057 nextchr = UCHARAT(locinput);
2061 /* Inline the first character, for speed. */
2062 if (UCHARAT(s) != nextchr &&
2064 (UCHARAT(s) != ((OP(scan) == REFF
2065 ? PL_fold : PL_fold_locale)[nextchr]))))
2067 ln = PL_regendp[n] - ln;
2068 if (locinput + ln > PL_regeol)
2070 if (ln > 1 && (OP(scan) == REF
2071 ? memNE(s, locinput, ln)
2073 ? ibcmp(s, locinput, ln)
2074 : ibcmp_locale(s, locinput, ln))))
2077 nextchr = UCHARAT(locinput);
2088 OP_4tree *oop = PL_op;
2089 COP *ocurcop = PL_curcop;
2090 SV **ocurpad = PL_curpad;
2094 PL_op = (OP_4tree*)PL_regdata->data[n];
2095 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
2096 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2097 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2099 CALLRUNOPS(aTHX); /* Scalar context. */
2105 PL_curpad = ocurpad;
2106 PL_curcop = ocurcop;
2108 if (logical == 2) { /* Postponed subexpression. */
2110 MAGIC *mg = Null(MAGIC*);
2113 CHECKPOINT cp, lastcp;
2115 if(SvROK(ret) || SvRMAGICAL(ret)) {
2116 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2119 mg = mg_find(sv, 'r');
2122 re = (regexp *)mg->mg_obj;
2123 (void)ReREFCNT_inc(re);
2127 char *t = SvPV(ret, len);
2129 char *oprecomp = PL_regprecomp;
2130 I32 osize = PL_regsize;
2131 I32 onpar = PL_regnpar;
2134 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2136 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2137 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2138 PL_regprecomp = oprecomp;
2143 PerlIO_printf(Perl_debug_log,
2144 "Entering embedded `%s%.60s%s%s'\n",
2148 (strlen(re->precomp) > 60 ? "..." : ""))
2151 state.prev = PL_reg_call_cc;
2152 state.cc = PL_regcc;
2153 state.re = PL_reg_re;
2159 cp = regcppush(0); /* Save *all* the positions. */
2162 state.ss = PL_savestack_ix;
2163 *PL_reglastparen = 0;
2164 PL_reg_call_cc = &state;
2165 PL_reginput = locinput;
2167 /* XXXX This is too dramatic a measure... */
2170 if (regmatch(re->program + 1)) {
2176 PerlIO_printf(Perl_debug_log,
2178 REPORT_CODE_OFF+PL_regindent*2, "")
2183 PL_reg_call_cc = state.prev;
2184 PL_regcc = state.cc;
2185 PL_reg_re = state.re;
2186 cache_re(PL_reg_re);
2188 /* XXXX This is too dramatic a measure... */
2197 sv_setsv(save_scalar(PL_replgv), ret);
2201 n = ARG(scan); /* which paren pair */
2202 PL_reg_start_tmp[n] = locinput;
2207 n = ARG(scan); /* which paren pair */
2208 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2209 PL_regendp[n] = locinput - PL_bostr;
2210 if (n > *PL_reglastparen)
2211 *PL_reglastparen = n;
2214 n = ARG(scan); /* which paren pair */
2215 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2218 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2220 next = NEXTOPER(NEXTOPER(scan));
2222 next = scan + ARG(scan);
2223 if (OP(next) == IFTHEN) /* Fake one. */
2224 next = NEXTOPER(NEXTOPER(next));
2228 logical = scan->flags;
2232 CHECKPOINT cp = PL_savestack_ix;
2234 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2236 cc.oldcc = PL_regcc;
2238 cc.parenfloor = *PL_reglastparen;
2240 cc.min = ARG1(scan);
2241 cc.max = ARG2(scan);
2242 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2246 PL_reginput = locinput;
2247 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2249 PL_regcc = cc.oldcc;
2255 * This is really hard to understand, because after we match
2256 * what we're trying to match, we must make sure the rest of
2257 * the REx is going to match for sure, and to do that we have
2258 * to go back UP the parse tree by recursing ever deeper. And
2259 * if it fails, we have to reset our parent's current state
2260 * that we can try again after backing off.
2263 CHECKPOINT cp, lastcp;
2264 CURCUR* cc = PL_regcc;
2265 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2267 n = cc->cur + 1; /* how many we know we matched */
2268 PL_reginput = locinput;
2271 PerlIO_printf(Perl_debug_log,
2272 "%*s %ld out of %ld..%ld cc=%lx\n",
2273 REPORT_CODE_OFF+PL_regindent*2, "",
2274 (long)n, (long)cc->min,
2275 (long)cc->max, (long)cc)
2278 /* If degenerate scan matches "", assume scan done. */
2280 if (locinput == cc->lastloc && n >= cc->min) {
2281 PL_regcc = cc->oldcc;
2284 PerlIO_printf(Perl_debug_log,
2285 "%*s empty match detected, try continuation...\n",
2286 REPORT_CODE_OFF+PL_regindent*2, "")
2288 if (regmatch(cc->next))
2291 PerlIO_printf(Perl_debug_log,
2293 REPORT_CODE_OFF+PL_regindent*2, "")
2300 /* First just match a string of min scans. */
2304 cc->lastloc = locinput;
2305 if (regmatch(cc->scan))
2308 cc->lastloc = lastloc;
2310 PerlIO_printf(Perl_debug_log,
2312 REPORT_CODE_OFF+PL_regindent*2, "")
2318 /* Check whether we already were at this position.
2319 Postpone detection until we know the match is not
2320 *that* much linear. */
2321 if (!PL_reg_maxiter) {
2322 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2323 PL_reg_leftiter = PL_reg_maxiter;
2325 if (PL_reg_leftiter-- == 0) {
2326 I32 size = (PL_reg_maxiter + 7)/8;
2327 if (PL_reg_poscache) {
2328 if (PL_reg_poscache_size < size) {
2329 Renew(PL_reg_poscache, size, char);
2330 PL_reg_poscache_size = size;
2332 Zero(PL_reg_poscache, size, char);
2335 PL_reg_poscache_size = size;
2336 Newz(29, PL_reg_poscache, size, char);
2339 PerlIO_printf(Perl_debug_log,
2340 "%sDetected a super-linear match, switching on caching%s...\n",
2341 PL_colors[4], PL_colors[5])
2344 if (PL_reg_leftiter < 0) {
2345 I32 o = locinput - PL_bostr, b;
2347 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2350 if (PL_reg_poscache[o] & (1<<b)) {
2352 PerlIO_printf(Perl_debug_log,
2353 "%*s already tried at this position...\n",
2354 REPORT_CODE_OFF+PL_regindent*2, "")
2358 PL_reg_poscache[o] |= (1<<b);
2362 /* Prefer next over scan for minimal matching. */
2365 PL_regcc = cc->oldcc;
2367 cp = regcppush(cc->parenfloor);
2369 if (regmatch(cc->next)) {
2371 sayYES; /* All done. */
2378 if (n >= cc->max) { /* Maximum greed exceeded? */
2379 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2380 && !(PL_reg_flags & RF_warned)) {
2381 PL_reg_flags |= RF_warned;
2382 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2383 "Complex regular subexpression recursion",
2390 PerlIO_printf(Perl_debug_log,
2391 "%*s trying longer...\n",
2392 REPORT_CODE_OFF+PL_regindent*2, "")
2394 /* Try scanning more and see if it helps. */
2395 PL_reginput = locinput;
2397 cc->lastloc = locinput;
2398 cp = regcppush(cc->parenfloor);
2400 if (regmatch(cc->scan)) {
2405 PerlIO_printf(Perl_debug_log,
2407 REPORT_CODE_OFF+PL_regindent*2, "")
2412 cc->lastloc = lastloc;
2416 /* Prefer scan over next for maximal matching. */
2418 if (n < cc->max) { /* More greed allowed? */
2419 cp = regcppush(cc->parenfloor);
2421 cc->lastloc = locinput;
2423 if (regmatch(cc->scan)) {
2428 regcppop(); /* Restore some previous $<digit>s? */
2429 PL_reginput = locinput;
2431 PerlIO_printf(Perl_debug_log,
2432 "%*s failed, try continuation...\n",
2433 REPORT_CODE_OFF+PL_regindent*2, "")
2436 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
2437 && !(PL_reg_flags & RF_warned)) {
2438 PL_reg_flags |= RF_warned;
2439 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
2440 "Complex regular subexpression recursion",
2444 /* Failed deeper matches of scan, so see if this one works. */
2445 PL_regcc = cc->oldcc;
2447 if (regmatch(cc->next))
2450 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
2451 REPORT_CODE_OFF+PL_regindent*2, "")
2456 cc->lastloc = lastloc;
2461 next = scan + ARG(scan);
2464 inner = NEXTOPER(NEXTOPER(scan));
2467 inner = NEXTOPER(scan);
2472 if (OP(next) != c1) /* No choice. */
2473 next = inner; /* Avoid recursion. */
2475 int lastparen = *PL_reglastparen;
2479 PL_reginput = locinput;
2480 if (regmatch(inner))
2483 for (n = *PL_reglastparen; n > lastparen; n--)
2485 *PL_reglastparen = n;
2488 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2492 inner = NEXTOPER(scan);
2493 if (c1 == BRANCHJ) {
2494 inner = NEXTOPER(inner);
2496 } while (scan != NULL && OP(scan) == c1);
2510 /* We suppose that the next guy does not need
2511 backtracking: in particular, it is of constant length,
2512 and has no parenths to influence future backrefs. */
2513 ln = ARG1(scan); /* min to match */
2514 n = ARG2(scan); /* max to match */
2515 paren = scan->flags;
2517 if (paren > PL_regsize)
2519 if (paren > *PL_reglastparen)
2520 *PL_reglastparen = paren;
2522 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2524 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2525 PL_reginput = locinput;
2528 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2530 if (ln && l == 0 && n >= ln
2531 /* In fact, this is tricky. If paren, then the
2532 fact that we did/didnot match may influence
2533 future execution. */
2534 && !(paren && ln == 0))
2536 locinput = PL_reginput;
2537 if (PL_regkind[(U8)OP(next)] == EXACT) {
2538 c1 = (U8)*STRING(next);
2539 if (OP(next) == EXACTF)
2541 else if (OP(next) == EXACTFL)
2542 c2 = PL_fold_locale[c1];
2549 /* This may be improved if l == 0. */
2550 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2551 /* If it could work, try it. */
2553 UCHARAT(PL_reginput) == c1 ||
2554 UCHARAT(PL_reginput) == c2)
2558 PL_regstartp[paren] =
2559 HOPc(PL_reginput, -l) - PL_bostr;
2560 PL_regendp[paren] = PL_reginput - PL_bostr;
2563 PL_regendp[paren] = -1;
2569 /* Couldn't or didn't -- move forward. */
2570 PL_reginput = locinput;
2571 if (regrepeat_hard(scan, 1, &l)) {
2573 locinput = PL_reginput;
2580 n = regrepeat_hard(scan, n, &l);
2581 if (n != 0 && l == 0
2582 /* In fact, this is tricky. If paren, then the
2583 fact that we did/didnot match may influence
2584 future execution. */
2585 && !(paren && ln == 0))
2587 locinput = PL_reginput;
2589 PerlIO_printf(Perl_debug_log,
2590 "%*s matched %ld times, len=%ld...\n",
2591 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2594 if (PL_regkind[(U8)OP(next)] == EXACT) {
2595 c1 = (U8)*STRING(next);
2596 if (OP(next) == EXACTF)
2598 else if (OP(next) == EXACTFL)
2599 c2 = PL_fold_locale[c1];
2608 /* If it could work, try it. */
2610 UCHARAT(PL_reginput) == c1 ||
2611 UCHARAT(PL_reginput) == c2)
2614 PerlIO_printf(Perl_debug_log,
2615 "%*s trying tail with n=%ld...\n",
2616 REPORT_CODE_OFF+PL_regindent*2, "", n)
2620 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2621 PL_regendp[paren] = PL_reginput - PL_bostr;
2624 PL_regendp[paren] = -1;
2630 /* Couldn't or didn't -- back up. */
2632 locinput = HOPc(locinput, -l);
2633 PL_reginput = locinput;
2640 paren = scan->flags; /* Which paren to set */
2641 if (paren > PL_regsize)
2643 if (paren > *PL_reglastparen)
2644 *PL_reglastparen = paren;
2645 ln = ARG1(scan); /* min to match */
2646 n = ARG2(scan); /* max to match */
2647 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2651 ln = ARG1(scan); /* min to match */
2652 n = ARG2(scan); /* max to match */
2653 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2658 scan = NEXTOPER(scan);
2664 scan = NEXTOPER(scan);
2668 * Lookahead to avoid useless match attempts
2669 * when we know what character comes next.
2671 if (PL_regkind[(U8)OP(next)] == EXACT) {
2672 c1 = (U8)*STRING(next);
2673 if (OP(next) == EXACTF)
2675 else if (OP(next) == EXACTFL)
2676 c2 = PL_fold_locale[c1];
2682 PL_reginput = locinput;
2686 if (ln && regrepeat(scan, ln) < ln)
2688 locinput = PL_reginput;
2691 char *e = locinput + n - ln; /* Should not check after this */
2692 char *old = locinput;
2694 if (e >= PL_regeol || (n == REG_INFTY))
2697 /* Find place 'next' could work */
2699 while (locinput <= e && *locinput != c1)
2702 while (locinput <= e
2709 /* PL_reginput == old now */
2710 if (locinput != old) {
2711 ln = 1; /* Did some */
2712 if (regrepeat(scan, locinput - old) <
2716 /* PL_reginput == locinput now */
2719 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2720 PL_regendp[paren] = locinput - PL_bostr;
2723 PL_regendp[paren] = -1;
2727 PL_reginput = locinput; /* Could be reset... */
2729 /* Couldn't or didn't -- move forward. */
2734 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2735 /* If it could work, try it. */
2737 UCHARAT(PL_reginput) == c1 ||
2738 UCHARAT(PL_reginput) == c2)
2742 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2743 PL_regendp[paren] = PL_reginput - PL_bostr;
2746 PL_regendp[paren] = -1;
2752 /* Couldn't or didn't -- move forward. */
2753 PL_reginput = locinput;
2754 if (regrepeat(scan, 1)) {
2756 locinput = PL_reginput;
2764 n = regrepeat(scan, n);
2765 locinput = PL_reginput;
2766 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2767 (!PL_multiline || OP(next) == SEOL))
2768 ln = n; /* why back off? */
2772 /* If it could work, try it. */
2774 UCHARAT(PL_reginput) == c1 ||
2775 UCHARAT(PL_reginput) == c2)
2779 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2780 PL_regendp[paren] = PL_reginput - PL_bostr;
2783 PL_regendp[paren] = -1;
2789 /* Couldn't or didn't -- back up. */
2791 PL_reginput = locinput = HOPc(locinput, -1);
2796 /* If it could work, try it. */
2798 UCHARAT(PL_reginput) == c1 ||
2799 UCHARAT(PL_reginput) == c2)
2805 /* Couldn't or didn't -- back up. */
2807 PL_reginput = locinput = HOPc(locinput, -1);
2814 if (PL_reg_call_cc) {
2815 re_cc_state *cur_call_cc = PL_reg_call_cc;
2816 CURCUR *cctmp = PL_regcc;
2817 regexp *re = PL_reg_re;
2818 CHECKPOINT cp, lastcp;
2820 cp = regcppush(0); /* Save *all* the positions. */
2822 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2824 PL_reginput = locinput; /* Make position available to
2826 cache_re(PL_reg_call_cc->re);
2827 PL_regcc = PL_reg_call_cc->cc;
2828 PL_reg_call_cc = PL_reg_call_cc->prev;
2829 if (regmatch(cur_call_cc->node)) {
2830 PL_reg_call_cc = cur_call_cc;
2836 PL_reg_call_cc = cur_call_cc;
2842 PerlIO_printf(Perl_debug_log,
2843 "%*s continuation failed...\n",
2844 REPORT_CODE_OFF+PL_regindent*2, "")
2848 if (locinput < PL_regtill)
2849 sayNO; /* Cannot match: too short. */
2852 PL_reginput = locinput; /* put where regtry can find it */
2853 sayYES; /* Success! */
2856 PL_reginput = locinput;
2861 if (UTF) { /* XXXX This is absolutely
2862 broken, we read before
2864 s = HOPMAYBEc(locinput, -scan->flags);
2870 if (locinput < PL_bostr + scan->flags)
2872 PL_reginput = locinput - scan->flags;
2877 PL_reginput = locinput;
2882 if (UTF) { /* XXXX This is absolutely
2883 broken, we read before
2885 s = HOPMAYBEc(locinput, -scan->flags);
2886 if (!s || s < PL_bostr)
2891 if (locinput < PL_bostr + scan->flags)
2893 PL_reginput = locinput - scan->flags;
2898 PL_reginput = locinput;
2901 inner = NEXTOPER(NEXTOPER(scan));
2902 if (regmatch(inner) != n) {
2917 if (OP(scan) == SUSPEND) {
2918 locinput = PL_reginput;
2919 nextchr = UCHARAT(locinput);
2924 next = scan + ARG(scan);
2929 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2930 (unsigned long)scan, OP(scan));
2931 Perl_croak(aTHX_ "regexp memory corruption");
2937 * We get here only if there's trouble -- normally "case END" is
2938 * the terminating point.
2940 Perl_croak(aTHX_ "corrupted regexp pointers");
2958 - regrepeat - repeatedly match something simple, report how many
2961 * [This routine now assumes that it will only match on things of length 1.
2962 * That was true before, but now we assume scan - reginput is the count,
2963 * rather than incrementing count on every character. [Er, except utf8.]]
2966 S_regrepeat(pTHX_ regnode *p, I32 max)
2969 register char *scan;
2970 register char *opnd;
2972 register char *loceol = PL_regeol;
2973 register I32 hardcount = 0;
2976 if (max != REG_INFTY && max < loceol - scan)
2977 loceol = scan + max;
2980 while (scan < loceol && *scan != '\n')
2988 while (scan < loceol && *scan != '\n') {
2989 scan += UTF8SKIP(scan);
2995 while (scan < loceol) {
2996 scan += UTF8SKIP(scan);
3000 case EXACT: /* length of string is 1 */
3002 while (scan < loceol && UCHARAT(scan) == c)
3005 case EXACTF: /* length of string is 1 */
3007 while (scan < loceol &&
3008 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3011 case EXACTFL: /* length of string is 1 */
3012 PL_reg_flags |= RF_tainted;
3014 while (scan < loceol &&
3015 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3020 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3021 scan += UTF8SKIP(scan);
3027 while (scan < loceol && REGINCLASS(opnd, *scan))
3031 while (scan < loceol && isALNUM(*scan))
3036 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3037 scan += UTF8SKIP(scan);
3042 PL_reg_flags |= RF_tainted;
3043 while (scan < loceol && isALNUM_LC(*scan))
3047 PL_reg_flags |= RF_tainted;
3049 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3050 scan += UTF8SKIP(scan);
3056 while (scan < loceol && !isALNUM(*scan))
3061 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3062 scan += UTF8SKIP(scan);
3067 PL_reg_flags |= RF_tainted;
3068 while (scan < loceol && !isALNUM_LC(*scan))
3072 PL_reg_flags |= RF_tainted;
3074 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3075 scan += UTF8SKIP(scan);
3080 while (scan < loceol && isSPACE(*scan))
3085 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3086 scan += UTF8SKIP(scan);
3091 PL_reg_flags |= RF_tainted;
3092 while (scan < loceol && isSPACE_LC(*scan))
3096 PL_reg_flags |= RF_tainted;
3098 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3099 scan += UTF8SKIP(scan);
3104 while (scan < loceol && !isSPACE(*scan))
3109 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3110 scan += UTF8SKIP(scan);
3115 PL_reg_flags |= RF_tainted;
3116 while (scan < loceol && !isSPACE_LC(*scan))
3120 PL_reg_flags |= RF_tainted;
3122 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3123 scan += UTF8SKIP(scan);
3128 while (scan < loceol && isDIGIT(*scan))
3133 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3134 scan += UTF8SKIP(scan);
3140 while (scan < loceol && !isDIGIT(*scan))
3145 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3146 scan += UTF8SKIP(scan);
3150 default: /* Called on something of 0 width. */
3151 break; /* So match right here or not at all. */
3157 c = scan - PL_reginput;
3162 SV *prop = sv_newmortal();
3165 PerlIO_printf(Perl_debug_log,
3166 "%*s %s can match %ld times out of %ld...\n",
3167 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
3174 - regrepeat_hard - repeatedly match something, report total lenth and length
3176 * The repeater is supposed to have constant length.
3180 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3183 register char *scan;
3184 register char *start;
3185 register char *loceol = PL_regeol;
3187 I32 count = 0, res = 1;
3192 start = PL_reginput;
3194 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3197 while (start < PL_reginput) {
3199 start += UTF8SKIP(start);
3210 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3212 *lp = l = PL_reginput - start;
3213 if (max != REG_INFTY && l*max < loceol - scan)
3214 loceol = scan + l*max;
3227 - reginclass - determine if a character falls into a character class
3231 S_reginclass(pTHX_ register char *p, register I32 c)
3234 char flags = ANYOF_FLAGS(p);
3238 if (ANYOF_BITMAP_TEST(p, c))
3240 else if (flags & ANYOF_FOLD) {
3242 if (flags & ANYOF_LOCALE) {
3243 PL_reg_flags |= RF_tainted;
3244 cf = PL_fold_locale[c];
3248 if (ANYOF_BITMAP_TEST(p, cf))
3252 if (!match && (flags & ANYOF_CLASS)) {
3253 PL_reg_flags |= RF_tainted;
3255 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3256 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3257 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
3258 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3259 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3260 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3261 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3262 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3263 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3264 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3265 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
3266 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
3267 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3268 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3269 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3270 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3271 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
3272 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3273 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
3274 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3275 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3276 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3277 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
3278 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3279 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3280 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
3281 ) /* How's that for a conditional? */
3287 return (flags & ANYOF_INVERT) ? !match : match;
3291 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3294 char flags = ARG1(f);
3296 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
3298 if (swash_fetch(sv, p))
3300 else if (flags & ANYOF_FOLD) {
3303 if (flags & ANYOF_LOCALE) {
3304 PL_reg_flags |= RF_tainted;
3305 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3308 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3309 if (swash_fetch(sv, tmpbuf))
3313 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3315 return (flags & ANYOF_INVERT) ? !match : match;
3319 S_reghop(pTHX_ U8 *s, I32 off)
3323 while (off-- && s < (U8*)PL_regeol)
3328 if (s > (U8*)PL_bostr) {
3331 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3333 } /* XXX could check well-formedness here */
3341 S_reghopmaybe(pTHX_ U8* s, I32 off)
3345 while (off-- && s < (U8*)PL_regeol)
3352 if (s > (U8*)PL_bostr) {
3355 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3357 } /* XXX could check well-formedness here */
3374 restore_pos(pTHXo_ void *arg)
3377 if (PL_reg_eval_set) {
3378 if (PL_reg_oldsaved) {
3379 PL_reg_re->subbeg = PL_reg_oldsaved;
3380 PL_reg_re->sublen = PL_reg_oldsavedlen;
3381 RX_MATCH_COPIED_on(PL_reg_re);
3383 PL_reg_magic->mg_len = PL_reg_oldpos;
3384 PL_reg_eval_set = 0;
3385 PL_curpm = PL_reg_oldcurpm;