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 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. */
279 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
280 char *strend, U32 flags, re_scream_pos_data *data)
283 /* Should be nonnegative! */
289 DEBUG_r( if (!PL_colorset) reginitcolors() );
290 DEBUG_r(PerlIO_printf(Perl_debug_log,
291 "%sGuessing start of match:%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
292 PL_colors[4],PL_colors[5],PL_colors[0],
295 (strlen(prog->precomp) > 60 ? "..." : ""),
297 (strend - strpos > 60 ? 60 : strend - strpos),
298 strpos, PL_colors[1],
299 (strend - strpos > 60 ? "..." : ""))
302 if (prog->minlen > strend - strpos)
305 /* XXXX Move further down? */
306 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
307 /* Should be nonnegative! */
308 end_shift = prog->minlen - start_shift -
309 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
311 if (prog->reganch & ROPT_ANCH) {
312 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
313 || ( (prog->reganch & ROPT_ANCH_BOL)
314 && !PL_multiline ) );
316 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
320 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
321 && (sv && (strpos + SvCUR(sv) != strend)) )
324 s = (char*)HOP((U8*)strpos, prog->check_offset_min);
325 if (SvTAIL(prog->check_substr)) {
326 slen = SvCUR(prog->check_substr); /* >= 1 */
328 if ( strend - s > slen || strend - s < slen - 1 ) {
332 if ( strend - s == slen && strend[-1] != '\n') {
336 /* Now should match s[0..slen-2] */
338 if (slen && (*SvPVX(prog->check_substr) != *s
340 && memNE(SvPVX(prog->check_substr), s, slen))))
343 else if (*SvPVX(prog->check_substr) != *s
344 || ((slen = SvCUR(prog->check_substr)) > 1
345 && memNE(SvPVX(prog->check_substr), s, slen)))
352 if (!ml_anch && (s + prog->check_offset_max < strend - prog->minlen))
353 end_shift += strend - s - prog->minlen - prog->check_offset_max;
361 if (flags & REXEC_SCREAM) {
362 SV *c = prog->check_substr;
363 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
364 I32 p = -1; /* Internal iterator of scream. */
365 I32 *pp = data ? data->scream_pos : &p;
367 if (PL_screamfirst[BmRARE(c)] >= 0
368 || ( BmRARE(c) == '\n'
369 && (BmPREVIOUS(c) == SvCUR(c) - 1)
371 s = screaminstr(sv, prog->check_substr,
372 start_shift + (strpos - strbeg), end_shift, pp, 0);
376 *data->scream_olds = s;
379 s = fbm_instr((unsigned char*)s + start_shift,
380 (unsigned char*)strend - end_shift,
381 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
383 /* Update the count-of-usability, remove useless subpatterns,
387 ++BmUSEFUL(prog->check_substr); /* hooray */
388 goto fail; /* not present */
390 else if (s - strpos > prog->check_offset_max &&
391 ((prog->reganch & ROPT_UTF8)
392 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
394 : (t = s - prog->check_offset_max) != 0) ) {
395 if (ml_anch && t[-1] != '\n') {
397 while (t < strend - end_shift - prog->minlen) {
399 if (t < s - prog->check_offset_min) {
413 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
417 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
421 if (!(prog->reganch & ROPT_NAUGHTY)
422 && --BmUSEFUL(prog->check_substr) < 0
423 && prog->check_substr == prog->float_substr) { /* boo */
424 /* If flags & SOMETHING - do not do it many times on the same match */
425 SvREFCNT_dec(prog->check_substr);
426 prog->check_substr = Nullsv; /* disable */
427 prog->float_substr = Nullsv; /* clear */
429 prog->reganch &= ~RE_USE_INTUIT;
435 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sFound%s at offset %ld\n",
436 PL_colors[4],PL_colors[5], (long)(s - strpos)) );
439 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sNot found...%s\n",
440 PL_colors[4],PL_colors[5]));
445 - regexec_flags - match a regexp against a string
448 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
449 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
450 /* strend: pointer to null at end of string */
451 /* strbeg: real beginning of string */
452 /* minend: end of match must be >=minend after stringarg. */
453 /* data: May be used for some additional optimizations. */
454 /* nosave: For optimizations. */
459 register char *startpos = stringarg;
461 I32 minlen; /* must match at least this many chars */
462 I32 dontbother = 0; /* how many characters not to try at end */
464 I32 start_shift = 0; /* Offset of the start to find
465 constant substr. */ /* CC */
466 I32 end_shift = 0; /* Same for the end. */ /* CC */
467 I32 scream_pos = -1; /* Internal iterator of scream. */
469 SV* oreplsv = GvSV(PL_replgv);
477 PL_regnarrate = PL_debug & 512;
481 if (prog == NULL || startpos == NULL) {
482 Perl_croak(aTHX_ "NULL regexp parameter");
486 minlen = prog->minlen;
487 if (strend - startpos < minlen) goto phooey;
489 if (startpos == strbeg) /* is ^ valid at stringarg? */
492 PL_regprev = (U32)stringarg[-1];
493 if (!PL_multiline && PL_regprev == '\n')
494 PL_regprev = '\0'; /* force ^ to NOT match */
497 /* Check validity of program. */
498 if (UCHARAT(prog->program) != REG_MAGIC) {
499 Perl_croak(aTHX_ "corrupted regexp program");
505 if (prog->reganch & ROPT_UTF8)
506 PL_reg_flags |= RF_utf8;
508 /* Mark beginning of line for ^ and lookbehind. */
509 PL_regbol = startpos;
513 /* Mark end of line for $ (and such) */
516 /* see how far we have to get to not match where we matched before */
517 PL_regtill = startpos+minend;
519 /* We start without call_cc context. */
522 /* If there is a "must appear" string, look for it. */
525 if (prog->reganch & ROPT_GPOS_SEEN) {
528 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
529 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
530 PL_reg_ganch = strbeg + mg->mg_len;
532 PL_reg_ganch = startpos;
533 if (prog->reganch & ROPT_ANCH_GPOS) {
534 if (s > PL_reg_ganch)
540 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
541 re_scream_pos_data d;
543 d.scream_olds = &scream_olds;
544 d.scream_pos = &scream_pos;
545 s = re_intuit_start(prog, sv, s, strend, flags, &d);
547 goto phooey; /* not present */
550 DEBUG_r( if (!PL_colorset) reginitcolors() );
551 DEBUG_r(PerlIO_printf(Perl_debug_log,
552 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
553 PL_colors[4],PL_colors[5],PL_colors[0],
556 (strlen(prog->precomp) > 60 ? "..." : ""),
558 (strend - startpos > 60 ? 60 : strend - startpos),
559 startpos, PL_colors[1],
560 (strend - startpos > 60 ? "..." : ""))
563 /* Simplest case: anchored match need be tried only once. */
564 /* [unless only anchor is BOL and multiline is set] */
565 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
566 if (s == startpos && regtry(prog, startpos))
568 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
569 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
574 dontbother = minlen - 1;
575 end = HOPc(strend, -dontbother) - 1;
576 /* for multiline we only have to try after newlines */
577 if (prog->check_substr) {
583 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
591 if (*s++ == '\n') { /* don't need PL_utf8skip here */
599 } else if (prog->reganch & ROPT_ANCH_GPOS) {
600 if (regtry(prog, PL_reg_ganch))
605 /* Messy cases: unanchored match. */
606 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
607 /* we have /x+whatever/ */
608 /* it must be a one character string (XXXX Except UTF?) */
609 char ch = SvPVX(prog->anchored_substr)[0];
613 if (regtry(prog, s)) goto got_it;
615 while (s < strend && *s == ch)
624 if (regtry(prog, s)) goto got_it;
626 while (s < strend && *s == ch)
634 else if (prog->anchored_substr != Nullsv
635 || (prog->float_substr != Nullsv
636 && prog->float_max_offset < strend - s)) {
637 SV *must = prog->anchored_substr
638 ? prog->anchored_substr : prog->float_substr;
640 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
642 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
643 I32 delta = back_max - back_min;
644 char *last = HOPc(strend, /* Cannot start after this */
645 -(I32)(CHR_SVLEN(must)
646 - (SvTAIL(must) != 0) + back_min));
647 char *last1; /* Last position checked before */
652 last1 = s - 1; /* bogus */
654 /* XXXX check_substr already used to find `s', can optimize if
655 check_substr==must. */
657 dontbother = end_shift;
658 strend = HOPc(strend, -dontbother);
659 while ( (s <= last) &&
660 ((flags & REXEC_SCREAM)
661 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
662 end_shift, &scream_pos, 0))
663 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
664 (unsigned char*)strend, must,
665 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
666 if (HOPc(s, -back_max) > last1) {
667 last1 = HOPc(s, -back_min);
668 s = HOPc(s, -back_max);
671 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
673 last1 = HOPc(s, -back_min);
693 else if (c = prog->regstclass) {
694 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
698 dontbother = minlen - 1;
699 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
701 /* We know what class it must start with. */
704 cc = (char *) OPERAND(c);
706 if (REGINCLASSUTF8(c, (U8*)s)) {
707 if (tmp && regtry(prog, s))
718 cc = (char *) OPERAND(c);
720 if (REGINCLASS(cc, *s)) {
721 if (tmp && regtry(prog, s))
732 PL_reg_flags |= RF_tainted;
739 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
740 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
742 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
749 if ((minlen || tmp) && regtry(prog,s))
753 PL_reg_flags |= RF_tainted;
758 strend = reghop_c(strend, -1);
760 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
761 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
763 if (tmp == !(OP(c) == BOUND ?
764 swash_fetch(PL_utf8_alnum, (U8*)s) :
765 isALNUM_LC_utf8((U8*)s)))
773 if ((minlen || tmp) && regtry(prog,s))
777 PL_reg_flags |= RF_tainted;
784 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
785 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
787 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
789 else if (regtry(prog, s))
793 if ((minlen || !tmp) && regtry(prog,s))
797 PL_reg_flags |= RF_tainted;
802 strend = reghop_c(strend, -1);
804 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
805 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
807 if (tmp == !(OP(c) == NBOUND ?
808 swash_fetch(PL_utf8_alnum, (U8*)s) :
809 isALNUM_LC_utf8((U8*)s)))
811 else if (regtry(prog, s))
815 if ((minlen || !tmp) && regtry(prog,s))
821 if (tmp && regtry(prog, s))
833 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
834 if (tmp && regtry(prog, s))
845 PL_reg_flags |= RF_tainted;
847 if (isALNUM_LC(*s)) {
848 if (tmp && regtry(prog, s))
859 PL_reg_flags |= RF_tainted;
861 if (isALNUM_LC_utf8((U8*)s)) {
862 if (tmp && regtry(prog, s))
875 if (tmp && regtry(prog, s))
887 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
888 if (tmp && regtry(prog, s))
899 PL_reg_flags |= RF_tainted;
901 if (!isALNUM_LC(*s)) {
902 if (tmp && regtry(prog, s))
913 PL_reg_flags |= RF_tainted;
915 if (!isALNUM_LC_utf8((U8*)s)) {
916 if (tmp && regtry(prog, s))
929 if (tmp && regtry(prog, s))
941 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
942 if (tmp && regtry(prog, s))
953 PL_reg_flags |= RF_tainted;
955 if (isSPACE_LC(*s)) {
956 if (tmp && regtry(prog, s))
967 PL_reg_flags |= RF_tainted;
969 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
970 if (tmp && regtry(prog, s))
983 if (tmp && regtry(prog, s))
995 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
996 if (tmp && regtry(prog, s))
1007 PL_reg_flags |= RF_tainted;
1008 while (s < strend) {
1009 if (!isSPACE_LC(*s)) {
1010 if (tmp && regtry(prog, s))
1021 PL_reg_flags |= RF_tainted;
1022 while (s < strend) {
1023 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1024 if (tmp && regtry(prog, s))
1035 while (s < strend) {
1037 if (tmp && regtry(prog, s))
1048 while (s < strend) {
1049 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1050 if (tmp && regtry(prog, s))
1061 PL_reg_flags |= RF_tainted;
1062 while (s < strend) {
1063 if (isDIGIT_LC(*s)) {
1064 if (tmp && regtry(prog, s))
1075 PL_reg_flags |= RF_tainted;
1076 while (s < strend) {
1077 if (isDIGIT_LC_utf8((U8*)s)) {
1078 if (tmp && regtry(prog, s))
1089 while (s < strend) {
1091 if (tmp && regtry(prog, s))
1102 while (s < strend) {
1103 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1104 if (tmp && regtry(prog, s))
1115 PL_reg_flags |= RF_tainted;
1116 while (s < strend) {
1117 if (!isDIGIT_LC(*s)) {
1118 if (tmp && regtry(prog, s))
1129 PL_reg_flags |= RF_tainted;
1130 while (s < strend) {
1131 if (!isDIGIT_LC_utf8((U8*)s)) {
1132 if (tmp && regtry(prog, s))
1143 while (s < strend) {
1145 if (tmp && regtry(prog, s))
1156 while (s < strend) {
1157 if (swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1158 if (tmp && regtry(prog, s))
1169 PL_reg_flags |= RF_tainted;
1170 while (s < strend) {
1171 if (isALNUMC_LC(*s)) {
1172 if (tmp && regtry(prog, s))
1183 PL_reg_flags |= RF_tainted;
1184 while (s < strend) {
1185 if (isALNUMC_LC_utf8((U8*)s)) {
1186 if (tmp && regtry(prog, s))
1197 while (s < strend) {
1198 if (!isALNUMC(*s)) {
1199 if (tmp && regtry(prog, s))
1210 while (s < strend) {
1211 if (!swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1212 if (tmp && regtry(prog, s))
1223 PL_reg_flags |= RF_tainted;
1224 while (s < strend) {
1225 if (!isALNUMC_LC(*s)) {
1226 if (tmp && regtry(prog, s))
1237 PL_reg_flags |= RF_tainted;
1238 while (s < strend) {
1239 if (!isALNUMC_LC_utf8((U8*)s)) {
1240 if (tmp && regtry(prog, s))
1251 while (s < strend) {
1252 if (isASCII(*(U8*)s)) {
1253 if (tmp && regtry(prog, s))
1264 while (s < strend) {
1265 if (!isASCII(*(U8*)s)) {
1266 if (tmp && regtry(prog, s))
1277 while (s < strend) {
1279 if (tmp && regtry(prog, s))
1290 while (s < strend) {
1291 if (swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1292 if (tmp && regtry(prog, s))
1303 PL_reg_flags |= RF_tainted;
1304 while (s < strend) {
1305 if (isCNTRL_LC(*s)) {
1306 if (tmp && regtry(prog, s))
1317 PL_reg_flags |= RF_tainted;
1318 while (s < strend) {
1319 if (*s == ' ' || isCNTRL_LC_utf8((U8*)s)) {
1320 if (tmp && regtry(prog, s))
1331 while (s < strend) {
1333 if (tmp && regtry(prog, s))
1344 while (s < strend) {
1345 if (!swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1346 if (tmp && regtry(prog, s))
1357 PL_reg_flags |= RF_tainted;
1358 while (s < strend) {
1359 if (!isCNTRL_LC(*s)) {
1360 if (tmp && regtry(prog, s))
1371 PL_reg_flags |= RF_tainted;
1372 while (s < strend) {
1373 if (!isCNTRL_LC_utf8((U8*)s)) {
1374 if (tmp && regtry(prog, s))
1385 while (s < strend) {
1387 if (tmp && regtry(prog, s))
1398 while (s < strend) {
1399 if (swash_fetch(PL_utf8_graph,(U8*)s)) {
1400 if (tmp && regtry(prog, s))
1411 PL_reg_flags |= RF_tainted;
1412 while (s < strend) {
1413 if (isGRAPH_LC(*s)) {
1414 if (tmp && regtry(prog, s))
1425 PL_reg_flags |= RF_tainted;
1426 while (s < strend) {
1427 if (*s == ' ' || isGRAPH_LC_utf8((U8*)s)) {
1428 if (tmp && regtry(prog, s))
1439 while (s < strend) {
1441 if (tmp && regtry(prog, s))
1452 while (s < strend) {
1453 if (!swash_fetch(PL_utf8_graph,(U8*)s)) {
1454 if (tmp && regtry(prog, s))
1465 PL_reg_flags |= RF_tainted;
1466 while (s < strend) {
1467 if (!isGRAPH_LC(*s)) {
1468 if (tmp && regtry(prog, s))
1479 PL_reg_flags |= RF_tainted;
1480 while (s < strend) {
1481 if (!isGRAPH_LC_utf8((U8*)s)) {
1482 if (tmp && regtry(prog, s))
1493 while (s < strend) {
1495 if (tmp && regtry(prog, s))
1506 while (s < strend) {
1507 if (swash_fetch(PL_utf8_lower,(U8*)s)) {
1508 if (tmp && regtry(prog, s))
1519 PL_reg_flags |= RF_tainted;
1520 while (s < strend) {
1521 if (isLOWER_LC(*s)) {
1522 if (tmp && regtry(prog, s))
1533 PL_reg_flags |= RF_tainted;
1534 while (s < strend) {
1535 if (*s == ' ' || isLOWER_LC_utf8((U8*)s)) {
1536 if (tmp && regtry(prog, s))
1547 while (s < strend) {
1549 if (tmp && regtry(prog, s))
1560 while (s < strend) {
1561 if (!swash_fetch(PL_utf8_lower,(U8*)s)) {
1562 if (tmp && regtry(prog, s))
1573 PL_reg_flags |= RF_tainted;
1574 while (s < strend) {
1575 if (!isLOWER_LC(*s)) {
1576 if (tmp && regtry(prog, s))
1587 PL_reg_flags |= RF_tainted;
1588 while (s < strend) {
1589 if (!isLOWER_LC_utf8((U8*)s)) {
1590 if (tmp && regtry(prog, s))
1601 while (s < strend) {
1603 if (tmp && regtry(prog, s))
1614 while (s < strend) {
1615 if (swash_fetch(PL_utf8_print,(U8*)s)) {
1616 if (tmp && regtry(prog, s))
1627 PL_reg_flags |= RF_tainted;
1628 while (s < strend) {
1629 if (isPRINT_LC(*s)) {
1630 if (tmp && regtry(prog, s))
1641 PL_reg_flags |= RF_tainted;
1642 while (s < strend) {
1643 if (*s == ' ' || isPRINT_LC_utf8((U8*)s)) {
1644 if (tmp && regtry(prog, s))
1655 while (s < strend) {
1657 if (tmp && regtry(prog, s))
1668 while (s < strend) {
1669 if (!swash_fetch(PL_utf8_print,(U8*)s)) {
1670 if (tmp && regtry(prog, s))
1681 PL_reg_flags |= RF_tainted;
1682 while (s < strend) {
1683 if (!isPRINT_LC(*s)) {
1684 if (tmp && regtry(prog, s))
1695 PL_reg_flags |= RF_tainted;
1696 while (s < strend) {
1697 if (!isPRINT_LC_utf8((U8*)s)) {
1698 if (tmp && regtry(prog, s))
1709 while (s < strend) {
1711 if (tmp && regtry(prog, s))
1722 while (s < strend) {
1723 if (swash_fetch(PL_utf8_punct,(U8*)s)) {
1724 if (tmp && regtry(prog, s))
1735 PL_reg_flags |= RF_tainted;
1736 while (s < strend) {
1737 if (isPUNCT_LC(*s)) {
1738 if (tmp && regtry(prog, s))
1749 PL_reg_flags |= RF_tainted;
1750 while (s < strend) {
1751 if (*s == ' ' || isPUNCT_LC_utf8((U8*)s)) {
1752 if (tmp && regtry(prog, s))
1763 while (s < strend) {
1765 if (tmp && regtry(prog, s))
1776 while (s < strend) {
1777 if (!swash_fetch(PL_utf8_punct,(U8*)s)) {
1778 if (tmp && regtry(prog, s))
1789 PL_reg_flags |= RF_tainted;
1790 while (s < strend) {
1791 if (!isPUNCT_LC(*s)) {
1792 if (tmp && regtry(prog, s))
1803 PL_reg_flags |= RF_tainted;
1804 while (s < strend) {
1805 if (!isPUNCT_LC_utf8((U8*)s)) {
1806 if (tmp && regtry(prog, s))
1817 while (s < strend) {
1819 if (tmp && regtry(prog, s))
1830 while (s < strend) {
1831 if (swash_fetch(PL_utf8_upper,(U8*)s)) {
1832 if (tmp && regtry(prog, s))
1843 PL_reg_flags |= RF_tainted;
1844 while (s < strend) {
1845 if (isUPPER_LC(*s)) {
1846 if (tmp && regtry(prog, s))
1857 PL_reg_flags |= RF_tainted;
1858 while (s < strend) {
1859 if (*s == ' ' || isUPPER_LC_utf8((U8*)s)) {
1860 if (tmp && regtry(prog, s))
1871 while (s < strend) {
1873 if (tmp && regtry(prog, s))
1884 while (s < strend) {
1885 if (!swash_fetch(PL_utf8_upper,(U8*)s)) {
1886 if (tmp && regtry(prog, s))
1897 PL_reg_flags |= RF_tainted;
1898 while (s < strend) {
1899 if (!isUPPER_LC(*s)) {
1900 if (tmp && regtry(prog, s))
1911 PL_reg_flags |= RF_tainted;
1912 while (s < strend) {
1913 if (!isUPPER_LC_utf8((U8*)s)) {
1914 if (tmp && regtry(prog, s))
1925 while (s < strend) {
1927 if (tmp && regtry(prog, s))
1938 while (s < strend) {
1939 if (!isXDIGIT(*s)) {
1940 if (tmp && regtry(prog, s))
1954 if (prog->float_substr != Nullsv) { /* Trim the end. */
1956 I32 oldpos = scream_pos;
1958 if (flags & REXEC_SCREAM) {
1959 last = screaminstr(sv, prog->float_substr, s - strbeg,
1960 end_shift, &scream_pos, 1); /* last one */
1962 last = scream_olds; /* Only one occurence. */
1966 char *little = SvPV(prog->float_substr, len);
1968 if (SvTAIL(prog->float_substr)) {
1969 if (memEQ(strend - len + 1, little, len - 1))
1970 last = strend - len + 1;
1971 else if (!PL_multiline)
1972 last = memEQ(strend - len, little, len)
1973 ? strend - len : Nullch;
1979 last = rninstr(s, strend, little, little + len);
1981 last = strend; /* matching `$' */
1984 if (last == NULL) goto phooey; /* Should not happen! */
1985 dontbother = strend - last + prog->float_min_offset;
1987 if (minlen && (dontbother < minlen))
1988 dontbother = minlen - 1;
1989 strend -= dontbother; /* this one's always in bytes! */
1990 /* We don't know much -- general case. */
1993 if (regtry(prog, s))
2002 if (regtry(prog, s))
2004 } while (s++ < strend);
2012 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2014 if (PL_reg_eval_set) {
2015 /* Preserve the current value of $^R */
2016 if (oreplsv != GvSV(PL_replgv))
2017 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2018 restored, the value remains
2020 restore_pos(aTHXo_ 0);
2023 /* make sure $`, $&, $', and $digit will work later */
2024 if ( !(flags & REXEC_NOT_FIRST) ) {
2025 if (RX_MATCH_COPIED(prog)) {
2026 Safefree(prog->subbeg);
2027 RX_MATCH_COPIED_off(prog);
2029 if (flags & REXEC_COPY_STR) {
2030 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2032 s = savepvn(strbeg, i);
2035 RX_MATCH_COPIED_on(prog);
2038 prog->subbeg = strbeg;
2039 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2046 if (PL_reg_eval_set)
2047 restore_pos(aTHXo_ 0);
2052 - regtry - try match at specific point
2054 STATIC I32 /* 0 failure, 1 success */
2055 S_regtry(pTHX_ regexp *prog, char *startpos)
2063 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2066 PL_reg_eval_set = RS_init;
2068 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
2069 PL_stack_sp - PL_stack_base);
2071 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
2072 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2073 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2075 /* Apparently this is not needed, judging by wantarray. */
2076 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
2077 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2080 /* Make $_ available to executed code. */
2081 if (PL_reg_sv != DEFSV) {
2082 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
2087 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2088 && (mg = mg_find(PL_reg_sv, 'g')))) {
2089 /* prepare for quick setting of pos */
2090 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
2091 mg = mg_find(PL_reg_sv, 'g');
2095 PL_reg_oldpos = mg->mg_len;
2096 SAVEDESTRUCTOR(restore_pos, 0);
2099 New(22,PL_reg_curpm, 1, PMOP);
2100 PL_reg_curpm->op_pmregexp = prog;
2101 PL_reg_oldcurpm = PL_curpm;
2102 PL_curpm = PL_reg_curpm;
2103 if (RX_MATCH_COPIED(prog)) {
2104 /* Here is a serious problem: we cannot rewrite subbeg,
2105 since it may be needed if this match fails. Thus
2106 $` inside (?{}) could fail... */
2107 PL_reg_oldsaved = prog->subbeg;
2108 PL_reg_oldsavedlen = prog->sublen;
2109 RX_MATCH_COPIED_off(prog);
2112 PL_reg_oldsaved = Nullch;
2113 prog->subbeg = PL_bostr;
2114 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2116 prog->startp[0] = startpos - PL_bostr;
2117 PL_reginput = startpos;
2118 PL_regstartp = prog->startp;
2119 PL_regendp = prog->endp;
2120 PL_reglastparen = &prog->lastparen;
2121 prog->lastparen = 0;
2123 DEBUG_r(PL_reg_starttry = startpos);
2124 if (PL_reg_start_tmpl <= prog->nparens) {
2125 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2126 if(PL_reg_start_tmp)
2127 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2129 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2132 /* XXXX What this code is doing here?!!! There should be no need
2133 to do this again and again, PL_reglastparen should take care of
2137 if (prog->nparens) {
2138 for (i = prog->nparens; i >= 1; i--) {
2144 if (regmatch(prog->program + 1)) {
2145 prog->endp[0] = PL_reginput - PL_bostr;
2153 - regmatch - main matching routine
2155 * Conceptually the strategy is simple: check to see whether the current
2156 * node matches, call self recursively to see whether the rest matches,
2157 * and then act accordingly. In practice we make some effort to avoid
2158 * recursion, in particular by going through "ordinary" nodes (that don't
2159 * need to know whether the rest of the match failed) by a loop instead of
2162 /* [lwall] I've hoisted the register declarations to the outer block in order to
2163 * maybe save a little bit of pushing and popping on the stack. It also takes
2164 * advantage of machines that use a register save mask on subroutine entry.
2166 STATIC I32 /* 0 failure, 1 success */
2167 S_regmatch(pTHX_ regnode *prog)
2170 register regnode *scan; /* Current node. */
2171 regnode *next; /* Next node. */
2172 regnode *inner; /* Next node in internal branch. */
2173 register I32 nextchr; /* renamed nextchr - nextchar colides with
2174 function of same name */
2175 register I32 n; /* no or next */
2176 register I32 ln; /* len or last */
2177 register char *s; /* operand or save */
2178 register char *locinput = PL_reginput;
2179 register I32 c1, c2, paren; /* case fold search, parenth */
2180 int minmod = 0, sw = 0, logical = 0;
2185 /* Note that nextchr is a byte even in UTF */
2186 nextchr = UCHARAT(locinput);
2188 while (scan != NULL) {
2189 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
2191 # define sayYES goto yes
2192 # define sayNO goto no
2193 # define saySAME(x) if (x) goto yes; else goto no
2194 # define REPORT_CODE_OFF 24
2196 # define sayYES return 1
2197 # define sayNO return 0
2198 # define saySAME(x) return x
2201 SV *prop = sv_newmortal();
2202 int docolor = *PL_colors[0];
2203 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2204 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
2205 /* The part of the string before starttry has one color
2206 (pref0_len chars), between starttry and current
2207 position another one (pref_len - pref0_len chars),
2208 after the current position the third one.
2209 We assume that pref0_len <= pref_len, otherwise we
2210 decrease pref0_len. */
2211 int pref_len = (locinput - PL_bostr > (5 + taill) - l
2212 ? (5 + taill) - l : locinput - PL_bostr);
2213 int pref0_len = pref_len - (locinput - PL_reg_starttry);
2215 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2216 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2217 ? (5 + taill) - pref_len : PL_regeol - locinput);
2220 if (pref0_len > pref_len)
2221 pref0_len = pref_len;
2222 regprop(prop, scan);
2223 PerlIO_printf(Perl_debug_log,
2224 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
2225 locinput - PL_bostr,
2226 PL_colors[4], pref0_len,
2227 locinput - pref_len, PL_colors[5],
2228 PL_colors[2], pref_len - pref0_len,
2229 locinput - pref_len + pref0_len, PL_colors[3],
2230 (docolor ? "" : "> <"),
2231 PL_colors[0], l, locinput, PL_colors[1],
2232 15 - l - pref_len + 1,
2234 scan - PL_regprogram, PL_regindent*2, "",
2238 next = scan + NEXT_OFF(scan);
2244 if (locinput == PL_bostr
2245 ? PL_regprev == '\n'
2247 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2249 /* regtill = regbol; */
2254 if (locinput == PL_bostr
2255 ? PL_regprev == '\n'
2256 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2262 if (locinput == PL_regbol && PL_regprev == '\n')
2266 if (locinput == PL_reg_ganch)
2276 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2281 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2283 if (PL_regeol - locinput > 1)
2287 if (PL_regeol != locinput)
2291 if (nextchr & 0x80) {
2292 locinput += PL_utf8skip[nextchr];
2293 if (locinput > PL_regeol)
2295 nextchr = UCHARAT(locinput);
2298 if (!nextchr && locinput >= PL_regeol)
2300 nextchr = UCHARAT(++locinput);
2303 if (!nextchr && locinput >= PL_regeol)
2305 nextchr = UCHARAT(++locinput);
2308 if (nextchr & 0x80) {
2309 locinput += PL_utf8skip[nextchr];
2310 if (locinput > PL_regeol)
2312 nextchr = UCHARAT(locinput);
2315 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2317 nextchr = UCHARAT(++locinput);
2320 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2322 nextchr = UCHARAT(++locinput);
2325 s = (char *) OPERAND(scan);
2327 /* Inline the first character, for speed. */
2328 if (UCHARAT(s) != nextchr)
2330 if (PL_regeol - locinput < ln)
2332 if (ln > 1 && memNE(s, locinput, ln))
2335 nextchr = UCHARAT(locinput);
2338 PL_reg_flags |= RF_tainted;
2341 s = (char *) OPERAND(scan);
2347 c1 = OP(scan) == EXACTF;
2351 if (utf8_to_uv((U8*)s, 0) != (c1 ?
2352 toLOWER_utf8((U8*)l) :
2353 toLOWER_LC_utf8((U8*)l)))
2361 nextchr = UCHARAT(locinput);
2365 /* Inline the first character, for speed. */
2366 if (UCHARAT(s) != nextchr &&
2367 UCHARAT(s) != ((OP(scan) == EXACTF)
2368 ? PL_fold : PL_fold_locale)[nextchr])
2370 if (PL_regeol - locinput < ln)
2372 if (ln > 1 && (OP(scan) == EXACTF
2373 ? ibcmp(s, locinput, ln)
2374 : ibcmp_locale(s, locinput, ln)))
2377 nextchr = UCHARAT(locinput);
2380 s = (char *) OPERAND(scan);
2381 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2383 if (locinput >= PL_regeol)
2385 locinput += PL_utf8skip[nextchr];
2386 nextchr = UCHARAT(locinput);
2389 s = (char *) OPERAND(scan);
2391 nextchr = UCHARAT(locinput);
2392 if (!REGINCLASS(s, nextchr))
2394 if (!nextchr && locinput >= PL_regeol)
2396 nextchr = UCHARAT(++locinput);
2399 PL_reg_flags |= RF_tainted;
2404 if (!(OP(scan) == ALNUM
2405 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2407 nextchr = UCHARAT(++locinput);
2410 PL_reg_flags |= RF_tainted;
2415 if (nextchr & 0x80) {
2416 if (!(OP(scan) == ALNUMUTF8
2417 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2418 : isALNUM_LC_utf8((U8*)locinput)))
2422 locinput += PL_utf8skip[nextchr];
2423 nextchr = UCHARAT(locinput);
2426 if (!(OP(scan) == ALNUMUTF8
2427 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2429 nextchr = UCHARAT(++locinput);
2432 PL_reg_flags |= RF_tainted;
2435 if (!nextchr && locinput >= PL_regeol)
2437 if (OP(scan) == NALNUM
2438 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2440 nextchr = UCHARAT(++locinput);
2443 PL_reg_flags |= RF_tainted;
2446 if (!nextchr && locinput >= PL_regeol)
2448 if (nextchr & 0x80) {
2449 if (OP(scan) == NALNUMUTF8
2450 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2451 : isALNUM_LC_utf8((U8*)locinput))
2455 locinput += PL_utf8skip[nextchr];
2456 nextchr = UCHARAT(locinput);
2459 if (OP(scan) == NALNUMUTF8
2460 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2462 nextchr = UCHARAT(++locinput);
2466 PL_reg_flags |= RF_tainted;
2470 /* was last char in word? */
2471 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2472 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2474 n = isALNUM(nextchr);
2477 ln = isALNUM_LC(ln);
2478 n = isALNUM_LC(nextchr);
2480 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2485 PL_reg_flags |= RF_tainted;
2489 /* was last char in word? */
2490 ln = (locinput != PL_regbol)
2491 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2492 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2493 ln = isALNUM_uni(ln);
2494 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2497 ln = isALNUM_LC_uni(ln);
2498 n = isALNUM_LC_utf8((U8*)locinput);
2500 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2504 PL_reg_flags |= RF_tainted;
2507 if (!nextchr && locinput >= PL_regeol)
2509 if (!(OP(scan) == SPACE
2510 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2512 nextchr = UCHARAT(++locinput);
2515 PL_reg_flags |= RF_tainted;
2518 if (!nextchr && locinput >= PL_regeol)
2520 if (nextchr & 0x80) {
2521 if (!(OP(scan) == SPACEUTF8
2522 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2523 : isSPACE_LC_utf8((U8*)locinput)))
2527 locinput += PL_utf8skip[nextchr];
2528 nextchr = UCHARAT(locinput);
2531 if (!(OP(scan) == SPACEUTF8
2532 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2534 nextchr = UCHARAT(++locinput);
2537 PL_reg_flags |= RF_tainted;
2542 if (OP(scan) == SPACE
2543 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2545 nextchr = UCHARAT(++locinput);
2548 PL_reg_flags |= RF_tainted;
2553 if (nextchr & 0x80) {
2554 if (OP(scan) == NSPACEUTF8
2555 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2556 : isSPACE_LC_utf8((U8*)locinput))
2560 locinput += PL_utf8skip[nextchr];
2561 nextchr = UCHARAT(locinput);
2564 if (OP(scan) == NSPACEUTF8
2565 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2567 nextchr = UCHARAT(++locinput);
2570 PL_reg_flags |= RF_tainted;
2573 if (!nextchr && locinput >= PL_regeol)
2575 if (!(OP(scan) == DIGIT
2576 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2578 nextchr = UCHARAT(++locinput);
2581 PL_reg_flags |= RF_tainted;
2586 if (nextchr & 0x80) {
2587 if (OP(scan) == NDIGITUTF8
2588 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2589 : isDIGIT_LC_utf8((U8*)locinput))
2593 locinput += PL_utf8skip[nextchr];
2594 nextchr = UCHARAT(locinput);
2597 if (!isDIGIT(nextchr))
2599 nextchr = UCHARAT(++locinput);
2602 PL_reg_flags |= RF_tainted;
2607 if (OP(scan) == DIGIT
2608 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2610 nextchr = UCHARAT(++locinput);
2613 PL_reg_flags |= RF_tainted;
2616 if (!nextchr && locinput >= PL_regeol)
2618 if (nextchr & 0x80) {
2619 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2621 locinput += PL_utf8skip[nextchr];
2622 nextchr = UCHARAT(locinput);
2625 if (isDIGIT(nextchr))
2627 nextchr = UCHARAT(++locinput);
2630 PL_reg_flags |= RF_tainted;
2635 if (!(OP(scan) == ALNUMC
2636 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2638 nextchr = UCHARAT(++locinput);
2641 PL_reg_flags |= RF_tainted;
2646 if (nextchr & 0x80) {
2647 if (!(OP(scan) == ALNUMCUTF8
2648 ? swash_fetch(PL_utf8_alnumc, (U8*)locinput)
2649 : isALNUMC_LC_utf8((U8*)locinput)))
2653 locinput += PL_utf8skip[nextchr];
2654 nextchr = UCHARAT(locinput);
2657 if (!(OP(scan) == ALNUMCUTF8
2658 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2660 nextchr = UCHARAT(++locinput);
2663 PL_reg_flags |= RF_tainted;
2668 if (OP(scan) == ALNUMC
2669 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr))
2671 nextchr = UCHARAT(++locinput);
2674 PL_reg_flags |= RF_tainted;
2677 if (!nextchr && locinput >= PL_regeol)
2679 if (nextchr & 0x80) {
2680 if (swash_fetch(PL_utf8_alnumc,(U8*)locinput))
2682 locinput += PL_utf8skip[nextchr];
2683 nextchr = UCHARAT(locinput);
2686 if (isALNUMC(nextchr))
2688 nextchr = UCHARAT(++locinput);
2691 PL_reg_flags |= RF_tainted;
2696 if (!(OP(scan) == ALPHA
2697 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2699 nextchr = UCHARAT(++locinput);
2702 PL_reg_flags |= RF_tainted;
2707 if (nextchr & 0x80) {
2708 if (!(OP(scan) == ALPHAUTF8
2709 ? swash_fetch(PL_utf8_alpha, (U8*)locinput)
2710 : isALPHA_LC_utf8((U8*)locinput)))
2714 locinput += PL_utf8skip[nextchr];
2715 nextchr = UCHARAT(locinput);
2718 if (!(OP(scan) == ALPHAUTF8
2719 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2721 nextchr = UCHARAT(++locinput);
2724 PL_reg_flags |= RF_tainted;
2729 if (OP(scan) == ALPHA
2730 ? isALPHA(nextchr) : isALPHA_LC(nextchr))
2732 nextchr = UCHARAT(++locinput);
2735 PL_reg_flags |= RF_tainted;
2738 if (!nextchr && locinput >= PL_regeol)
2740 if (nextchr & 0x80) {
2741 if (swash_fetch(PL_utf8_alpha,(U8*)locinput))
2743 locinput += PL_utf8skip[nextchr];
2744 nextchr = UCHARAT(locinput);
2747 if (isALPHA(nextchr))
2749 nextchr = UCHARAT(++locinput);
2752 if (!nextchr && locinput >= PL_regeol)
2754 if (!isASCII(nextchr))
2756 nextchr = UCHARAT(++locinput);
2759 if (!nextchr && locinput >= PL_regeol)
2761 if (isASCII(nextchr))
2763 nextchr = UCHARAT(++locinput);
2766 PL_reg_flags |= RF_tainted;
2771 if (!(OP(scan) == CNTRL
2772 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2774 nextchr = UCHARAT(++locinput);
2777 PL_reg_flags |= RF_tainted;
2782 if (nextchr & 0x80) {
2783 if (!(OP(scan) == CNTRLUTF8
2784 ? swash_fetch(PL_utf8_cntrl, (U8*)locinput)
2785 : isCNTRL_LC_utf8((U8*)locinput)))
2789 locinput += PL_utf8skip[nextchr];
2790 nextchr = UCHARAT(locinput);
2793 if (!(OP(scan) == CNTRLUTF8
2794 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2796 nextchr = UCHARAT(++locinput);
2799 PL_reg_flags |= RF_tainted;
2804 if (OP(scan) == CNTRL
2805 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr))
2807 nextchr = UCHARAT(++locinput);
2810 PL_reg_flags |= RF_tainted;
2813 if (!nextchr && locinput >= PL_regeol)
2815 if (nextchr & 0x80) {
2816 if (swash_fetch(PL_utf8_cntrl,(U8*)locinput))
2818 locinput += PL_utf8skip[nextchr];
2819 nextchr = UCHARAT(locinput);
2822 if (isCNTRL(nextchr))
2824 nextchr = UCHARAT(++locinput);
2827 PL_reg_flags |= RF_tainted;
2832 if (!(OP(scan) == GRAPH
2833 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2835 nextchr = UCHARAT(++locinput);
2838 PL_reg_flags |= RF_tainted;
2843 if (nextchr & 0x80) {
2844 if (!(OP(scan) == GRAPHUTF8
2845 ? swash_fetch(PL_utf8_graph, (U8*)locinput)
2846 : isGRAPH_LC_utf8((U8*)locinput)))
2850 locinput += PL_utf8skip[nextchr];
2851 nextchr = UCHARAT(locinput);
2854 if (!(OP(scan) == GRAPHUTF8
2855 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2857 nextchr = UCHARAT(++locinput);
2860 PL_reg_flags |= RF_tainted;
2865 if (OP(scan) == GRAPH
2866 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr))
2868 nextchr = UCHARAT(++locinput);
2871 PL_reg_flags |= RF_tainted;
2874 if (!nextchr && locinput >= PL_regeol)
2876 if (nextchr & 0x80) {
2877 if (swash_fetch(PL_utf8_graph,(U8*)locinput))
2879 locinput += PL_utf8skip[nextchr];
2880 nextchr = UCHARAT(locinput);
2883 if (isGRAPH(nextchr))
2885 nextchr = UCHARAT(++locinput);
2888 PL_reg_flags |= RF_tainted;
2893 if (!(OP(scan) == LOWER
2894 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2896 nextchr = UCHARAT(++locinput);
2899 PL_reg_flags |= RF_tainted;
2904 if (nextchr & 0x80) {
2905 if (!(OP(scan) == LOWERUTF8
2906 ? swash_fetch(PL_utf8_lower, (U8*)locinput)
2907 : isLOWER_LC_utf8((U8*)locinput)))
2911 locinput += PL_utf8skip[nextchr];
2912 nextchr = UCHARAT(locinput);
2915 if (!(OP(scan) == LOWERUTF8
2916 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2918 nextchr = UCHARAT(++locinput);
2921 PL_reg_flags |= RF_tainted;
2926 if (OP(scan) == LOWER
2927 ? isLOWER(nextchr) : isLOWER_LC(nextchr))
2929 nextchr = UCHARAT(++locinput);
2932 PL_reg_flags |= RF_tainted;
2935 if (!nextchr && locinput >= PL_regeol)
2937 if (nextchr & 0x80) {
2938 if (swash_fetch(PL_utf8_lower,(U8*)locinput))
2940 locinput += PL_utf8skip[nextchr];
2941 nextchr = UCHARAT(locinput);
2944 if (isLOWER(nextchr))
2946 nextchr = UCHARAT(++locinput);
2949 PL_reg_flags |= RF_tainted;
2954 if (!(OP(scan) == PRINT
2955 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2957 nextchr = UCHARAT(++locinput);
2960 PL_reg_flags |= RF_tainted;
2965 if (nextchr & 0x80) {
2966 if (!(OP(scan) == PRINTUTF8
2967 ? swash_fetch(PL_utf8_print, (U8*)locinput)
2968 : isPRINT_LC_utf8((U8*)locinput)))
2972 locinput += PL_utf8skip[nextchr];
2973 nextchr = UCHARAT(locinput);
2976 if (!(OP(scan) == PRINTUTF8
2977 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2979 nextchr = UCHARAT(++locinput);
2982 PL_reg_flags |= RF_tainted;
2987 if (OP(scan) == PRINT
2988 ? isPRINT(nextchr) : isPRINT_LC(nextchr))
2990 nextchr = UCHARAT(++locinput);
2993 PL_reg_flags |= RF_tainted;
2996 if (!nextchr && locinput >= PL_regeol)
2998 if (nextchr & 0x80) {
2999 if (swash_fetch(PL_utf8_print,(U8*)locinput))
3001 locinput += PL_utf8skip[nextchr];
3002 nextchr = UCHARAT(locinput);
3005 if (isPRINT(nextchr))
3007 nextchr = UCHARAT(++locinput);
3010 PL_reg_flags |= RF_tainted;
3015 if (!(OP(scan) == PUNCT
3016 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3018 nextchr = UCHARAT(++locinput);
3021 PL_reg_flags |= RF_tainted;
3026 if (nextchr & 0x80) {
3027 if (!(OP(scan) == PUNCTUTF8
3028 ? swash_fetch(PL_utf8_punct, (U8*)locinput)
3029 : isPUNCT_LC_utf8((U8*)locinput)))
3033 locinput += PL_utf8skip[nextchr];
3034 nextchr = UCHARAT(locinput);
3037 if (!(OP(scan) == PUNCTUTF8
3038 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3040 nextchr = UCHARAT(++locinput);
3043 PL_reg_flags |= RF_tainted;
3048 if (OP(scan) == PUNCT
3049 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr))
3051 nextchr = UCHARAT(++locinput);
3054 PL_reg_flags |= RF_tainted;
3057 if (!nextchr && locinput >= PL_regeol)
3059 if (nextchr & 0x80) {
3060 if (swash_fetch(PL_utf8_punct,(U8*)locinput))
3062 locinput += PL_utf8skip[nextchr];
3063 nextchr = UCHARAT(locinput);
3066 if (isPUNCT(nextchr))
3068 nextchr = UCHARAT(++locinput);
3071 PL_reg_flags |= RF_tainted;
3076 if (!(OP(scan) == UPPER
3077 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3079 nextchr = UCHARAT(++locinput);
3082 PL_reg_flags |= RF_tainted;
3087 if (nextchr & 0x80) {
3088 if (!(OP(scan) == UPPERUTF8
3089 ? swash_fetch(PL_utf8_upper, (U8*)locinput)
3090 : isUPPER_LC_utf8((U8*)locinput)))
3094 locinput += PL_utf8skip[nextchr];
3095 nextchr = UCHARAT(locinput);
3098 if (!(OP(scan) == UPPERUTF8
3099 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3101 nextchr = UCHARAT(++locinput);
3104 PL_reg_flags |= RF_tainted;
3109 if (OP(scan) == UPPER
3110 ? isUPPER(nextchr) : isUPPER_LC(nextchr))
3112 nextchr = UCHARAT(++locinput);
3115 PL_reg_flags |= RF_tainted;
3118 if (!nextchr && locinput >= PL_regeol)
3120 if (nextchr & 0x80) {
3121 if (swash_fetch(PL_utf8_upper,(U8*)locinput))
3123 locinput += PL_utf8skip[nextchr];
3124 nextchr = UCHARAT(locinput);
3127 if (isUPPER(nextchr))
3129 nextchr = UCHARAT(++locinput);
3132 if (!nextchr && locinput >= PL_regeol)
3134 if (!isXDIGIT(nextchr))
3136 nextchr = UCHARAT(++locinput);
3139 if (!nextchr && locinput >= PL_regeol)
3141 if (isXDIGIT(nextchr))
3143 nextchr = UCHARAT(++locinput);
3146 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
3148 locinput += PL_utf8skip[nextchr];
3149 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
3150 locinput += UTF8SKIP(locinput);
3151 if (locinput > PL_regeol)
3153 nextchr = UCHARAT(locinput);
3156 PL_reg_flags |= RF_tainted;
3160 n = ARG(scan); /* which paren pair */
3161 ln = PL_regstartp[n];
3162 if (*PL_reglastparen < n || ln == -1)
3163 sayNO; /* Do not match unless seen CLOSEn. */
3164 if (ln == PL_regendp[n])
3168 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
3170 char *e = PL_bostr + PL_regendp[n];
3172 * Note that we can't do the "other character" lookup trick as
3173 * in the 8-bit case (no pun intended) because in Unicode we
3174 * have to map both upper and title case to lower case.
3176 if (OP(scan) == REFF) {
3180 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
3190 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
3197 nextchr = UCHARAT(locinput);
3201 /* Inline the first character, for speed. */
3202 if (UCHARAT(s) != nextchr &&
3204 (UCHARAT(s) != ((OP(scan) == REFF
3205 ? PL_fold : PL_fold_locale)[nextchr]))))
3207 ln = PL_regendp[n] - ln;
3208 if (locinput + ln > PL_regeol)
3210 if (ln > 1 && (OP(scan) == REF
3211 ? memNE(s, locinput, ln)
3213 ? ibcmp(s, locinput, ln)
3214 : ibcmp_locale(s, locinput, ln))))
3217 nextchr = UCHARAT(locinput);
3228 OP_4tree *oop = PL_op;
3229 COP *ocurcop = PL_curcop;
3230 SV **ocurpad = PL_curpad;
3234 PL_op = (OP_4tree*)PL_regdata->data[n];
3235 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
3236 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
3237 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3239 CALLRUNOPS(aTHX); /* Scalar context. */
3245 PL_curpad = ocurpad;
3246 PL_curcop = ocurcop;
3248 if (logical == 2) { /* Postponed subexpression. */
3250 MAGIC *mg = Null(MAGIC*);
3253 CHECKPOINT cp, lastcp;
3255 if(SvROK(ret) || SvRMAGICAL(ret)) {
3256 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
3259 mg = mg_find(sv, 'r');
3262 re = (regexp *)mg->mg_obj;
3263 (void)ReREFCNT_inc(re);
3267 char *t = SvPV(ret, len);
3269 char *oprecomp = PL_regprecomp;
3270 I32 osize = PL_regsize;
3271 I32 onpar = PL_regnpar;
3274 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
3276 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
3277 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
3278 PL_regprecomp = oprecomp;
3283 PerlIO_printf(Perl_debug_log,
3284 "Entering embedded `%s%.60s%s%s'\n",
3288 (strlen(re->precomp) > 60 ? "..." : ""))
3291 state.prev = PL_reg_call_cc;
3292 state.cc = PL_regcc;
3293 state.re = PL_reg_re;
3299 cp = regcppush(0); /* Save *all* the positions. */
3302 state.ss = PL_savestack_ix;
3303 *PL_reglastparen = 0;
3304 PL_reg_call_cc = &state;
3305 PL_reginput = locinput;
3306 if (regmatch(re->program + 1)) {
3312 PerlIO_printf(Perl_debug_log,
3314 REPORT_CODE_OFF+PL_regindent*2, "")
3319 PL_reg_call_cc = state.prev;
3320 PL_regcc = state.cc;
3321 PL_reg_re = state.re;
3322 cache_re(PL_reg_re);
3329 sv_setsv(save_scalar(PL_replgv), ret);
3333 n = ARG(scan); /* which paren pair */
3334 PL_reg_start_tmp[n] = locinput;
3339 n = ARG(scan); /* which paren pair */
3340 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3341 PL_regendp[n] = locinput - PL_bostr;
3342 if (n > *PL_reglastparen)
3343 *PL_reglastparen = n;
3346 n = ARG(scan); /* which paren pair */
3347 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
3351 next = NEXTOPER(NEXTOPER(scan));
3353 next = scan + ARG(scan);
3354 if (OP(next) == IFTHEN) /* Fake one. */
3355 next = NEXTOPER(NEXTOPER(next));
3359 logical = scan->flags;
3363 CHECKPOINT cp = PL_savestack_ix;
3365 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3367 cc.oldcc = PL_regcc;
3369 cc.parenfloor = *PL_reglastparen;
3371 cc.min = ARG1(scan);
3372 cc.max = ARG2(scan);
3373 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3377 PL_reginput = locinput;
3378 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3380 PL_regcc = cc.oldcc;
3386 * This is really hard to understand, because after we match
3387 * what we're trying to match, we must make sure the rest of
3388 * the RE is going to match for sure, and to do that we have
3389 * to go back UP the parse tree by recursing ever deeper. And
3390 * if it fails, we have to reset our parent's current state
3391 * that we can try again after backing off.
3394 CHECKPOINT cp, lastcp;
3395 CURCUR* cc = PL_regcc;
3396 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3398 n = cc->cur + 1; /* how many we know we matched */
3399 PL_reginput = locinput;
3402 PerlIO_printf(Perl_debug_log,
3403 "%*s %ld out of %ld..%ld cc=%lx\n",
3404 REPORT_CODE_OFF+PL_regindent*2, "",
3405 (long)n, (long)cc->min,
3406 (long)cc->max, (long)cc)
3409 /* If degenerate scan matches "", assume scan done. */
3411 if (locinput == cc->lastloc && n >= cc->min) {
3412 PL_regcc = cc->oldcc;
3415 PerlIO_printf(Perl_debug_log,
3416 "%*s empty match detected, try continuation...\n",
3417 REPORT_CODE_OFF+PL_regindent*2, "")
3419 if (regmatch(cc->next))
3422 PerlIO_printf(Perl_debug_log,
3424 REPORT_CODE_OFF+PL_regindent*2, "")
3431 /* First just match a string of min scans. */
3435 cc->lastloc = locinput;
3436 if (regmatch(cc->scan))
3439 cc->lastloc = lastloc;
3441 PerlIO_printf(Perl_debug_log,
3443 REPORT_CODE_OFF+PL_regindent*2, "")
3448 /* Prefer next over scan for minimal matching. */
3451 PL_regcc = cc->oldcc;
3453 cp = regcppush(cc->parenfloor);
3455 if (regmatch(cc->next)) {
3457 sayYES; /* All done. */
3464 if (n >= cc->max) { /* Maximum greed exceeded? */
3465 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3466 && !(PL_reg_flags & RF_warned)) {
3467 PL_reg_flags |= RF_warned;
3468 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3469 "Complex regular subexpression recursion",
3476 PerlIO_printf(Perl_debug_log,
3477 "%*s trying longer...\n",
3478 REPORT_CODE_OFF+PL_regindent*2, "")
3480 /* Try scanning more and see if it helps. */
3481 PL_reginput = locinput;
3483 cc->lastloc = locinput;
3484 cp = regcppush(cc->parenfloor);
3486 if (regmatch(cc->scan)) {
3491 PerlIO_printf(Perl_debug_log,
3493 REPORT_CODE_OFF+PL_regindent*2, "")
3498 cc->lastloc = lastloc;
3502 /* Prefer scan over next for maximal matching. */
3504 if (n < cc->max) { /* More greed allowed? */
3505 cp = regcppush(cc->parenfloor);
3507 cc->lastloc = locinput;
3509 if (regmatch(cc->scan)) {
3514 regcppop(); /* Restore some previous $<digit>s? */
3515 PL_reginput = locinput;
3517 PerlIO_printf(Perl_debug_log,
3518 "%*s failed, try continuation...\n",
3519 REPORT_CODE_OFF+PL_regindent*2, "")
3522 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3523 && !(PL_reg_flags & RF_warned)) {
3524 PL_reg_flags |= RF_warned;
3525 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3526 "Complex regular subexpression recursion",
3530 /* Failed deeper matches of scan, so see if this one works. */
3531 PL_regcc = cc->oldcc;
3533 if (regmatch(cc->next))
3536 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
3537 REPORT_CODE_OFF+PL_regindent*2, "")
3542 cc->lastloc = lastloc;
3547 next = scan + ARG(scan);
3550 inner = NEXTOPER(NEXTOPER(scan));
3553 inner = NEXTOPER(scan);
3558 if (OP(next) != c1) /* No choice. */
3559 next = inner; /* Avoid recursion. */
3561 int lastparen = *PL_reglastparen;
3565 PL_reginput = locinput;
3566 if (regmatch(inner))
3569 for (n = *PL_reglastparen; n > lastparen; n--)
3571 *PL_reglastparen = n;
3574 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
3578 inner = NEXTOPER(scan);
3579 if (c1 == BRANCHJ) {
3580 inner = NEXTOPER(inner);
3582 } while (scan != NULL && OP(scan) == c1);
3596 /* We suppose that the next guy does not need
3597 backtracking: in particular, it is of constant length,
3598 and has no parenths to influence future backrefs. */
3599 ln = ARG1(scan); /* min to match */
3600 n = ARG2(scan); /* max to match */
3601 paren = scan->flags;
3603 if (paren > PL_regsize)
3605 if (paren > *PL_reglastparen)
3606 *PL_reglastparen = paren;
3608 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3610 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3611 PL_reginput = locinput;
3614 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3616 if (ln && l == 0 && n >= ln
3617 /* In fact, this is tricky. If paren, then the
3618 fact that we did/didnot match may influence
3619 future execution. */
3620 && !(paren && ln == 0))
3622 locinput = PL_reginput;
3623 if (PL_regkind[(U8)OP(next)] == EXACT) {
3624 c1 = UCHARAT(OPERAND(next) + 1);
3625 if (OP(next) == EXACTF)
3627 else if (OP(next) == EXACTFL)
3628 c2 = PL_fold_locale[c1];
3635 /* This may be improved if l == 0. */
3636 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3637 /* If it could work, try it. */
3639 UCHARAT(PL_reginput) == c1 ||
3640 UCHARAT(PL_reginput) == c2)
3644 PL_regstartp[paren] =
3645 HOPc(PL_reginput, -l) - PL_bostr;
3646 PL_regendp[paren] = PL_reginput - PL_bostr;
3649 PL_regendp[paren] = -1;
3655 /* Couldn't or didn't -- move forward. */
3656 PL_reginput = locinput;
3657 if (regrepeat_hard(scan, 1, &l)) {
3659 locinput = PL_reginput;
3666 n = regrepeat_hard(scan, n, &l);
3667 if (n != 0 && l == 0
3668 /* In fact, this is tricky. If paren, then the
3669 fact that we did/didnot match may influence
3670 future execution. */
3671 && !(paren && ln == 0))
3673 locinput = PL_reginput;
3675 PerlIO_printf(Perl_debug_log,
3676 "%*s matched %ld times, len=%ld...\n",
3677 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
3680 if (PL_regkind[(U8)OP(next)] == EXACT) {
3681 c1 = UCHARAT(OPERAND(next) + 1);
3682 if (OP(next) == EXACTF)
3684 else if (OP(next) == EXACTFL)
3685 c2 = PL_fold_locale[c1];
3694 /* If it could work, try it. */
3696 UCHARAT(PL_reginput) == c1 ||
3697 UCHARAT(PL_reginput) == c2)
3700 PerlIO_printf(Perl_debug_log,
3701 "%*s trying tail with n=%ld...\n",
3702 REPORT_CODE_OFF+PL_regindent*2, "", n)
3706 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3707 PL_regendp[paren] = PL_reginput - PL_bostr;
3710 PL_regendp[paren] = -1;
3716 /* Couldn't or didn't -- back up. */
3718 locinput = HOPc(locinput, -l);
3719 PL_reginput = locinput;
3726 paren = scan->flags; /* Which paren to set */
3727 if (paren > PL_regsize)
3729 if (paren > *PL_reglastparen)
3730 *PL_reglastparen = paren;
3731 ln = ARG1(scan); /* min to match */
3732 n = ARG2(scan); /* max to match */
3733 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3737 ln = ARG1(scan); /* min to match */
3738 n = ARG2(scan); /* max to match */
3739 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3744 scan = NEXTOPER(scan);
3750 scan = NEXTOPER(scan);
3754 * Lookahead to avoid useless match attempts
3755 * when we know what character comes next.
3757 if (PL_regkind[(U8)OP(next)] == EXACT) {
3758 c1 = UCHARAT(OPERAND(next) + 1);
3759 if (OP(next) == EXACTF)
3761 else if (OP(next) == EXACTFL)
3762 c2 = PL_fold_locale[c1];
3768 PL_reginput = locinput;
3772 if (ln && regrepeat(scan, ln) < ln)
3774 locinput = PL_reginput;
3777 char *e = locinput + n - ln; /* Should not check after this */
3778 char *old = locinput;
3780 if (e >= PL_regeol || (n == REG_INFTY))
3783 /* Find place 'next' could work */
3785 while (locinput <= e && *locinput != c1)
3788 while (locinput <= e
3795 /* PL_reginput == old now */
3796 if (locinput != old) {
3797 ln = 1; /* Did some */
3798 if (regrepeat(scan, locinput - old) <
3802 /* PL_reginput == locinput now */
3805 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
3806 PL_regendp[paren] = locinput - PL_bostr;
3809 PL_regendp[paren] = -1;
3813 PL_reginput = locinput; /* Could be reset... */
3815 /* Couldn't or didn't -- move forward. */
3820 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3821 /* If it could work, try it. */
3823 UCHARAT(PL_reginput) == c1 ||
3824 UCHARAT(PL_reginput) == c2)
3828 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3829 PL_regendp[paren] = PL_reginput - PL_bostr;
3832 PL_regendp[paren] = -1;
3838 /* Couldn't or didn't -- move forward. */
3839 PL_reginput = locinput;
3840 if (regrepeat(scan, 1)) {
3842 locinput = PL_reginput;
3850 n = regrepeat(scan, n);
3851 locinput = PL_reginput;
3852 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3853 (!PL_multiline || OP(next) == SEOL))
3854 ln = n; /* why back off? */
3858 /* If it could work, try it. */
3860 UCHARAT(PL_reginput) == c1 ||
3861 UCHARAT(PL_reginput) == c2)
3865 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3866 PL_regendp[paren] = PL_reginput - PL_bostr;
3869 PL_regendp[paren] = -1;
3875 /* Couldn't or didn't -- back up. */
3877 PL_reginput = locinput = HOPc(locinput, -1);
3882 /* If it could work, try it. */
3884 UCHARAT(PL_reginput) == c1 ||
3885 UCHARAT(PL_reginput) == c2)
3891 /* Couldn't or didn't -- back up. */
3893 PL_reginput = locinput = HOPc(locinput, -1);
3900 if (PL_reg_call_cc) {
3901 re_cc_state *cur_call_cc = PL_reg_call_cc;
3902 CURCUR *cctmp = PL_regcc;
3903 regexp *re = PL_reg_re;
3904 CHECKPOINT cp, lastcp;
3906 cp = regcppush(0); /* Save *all* the positions. */
3908 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3910 PL_reginput = locinput; /* Make position available to
3912 cache_re(PL_reg_call_cc->re);
3913 PL_regcc = PL_reg_call_cc->cc;
3914 PL_reg_call_cc = PL_reg_call_cc->prev;
3915 if (regmatch(cur_call_cc->node)) {
3916 PL_reg_call_cc = cur_call_cc;
3922 PL_reg_call_cc = cur_call_cc;
3928 PerlIO_printf(Perl_debug_log,
3929 "%*s continuation failed...\n",
3930 REPORT_CODE_OFF+PL_regindent*2, "")
3934 if (locinput < PL_regtill)
3935 sayNO; /* Cannot match: too short. */
3938 PL_reginput = locinput; /* put where regtry can find it */
3939 sayYES; /* Success! */
3942 PL_reginput = locinput;
3947 if (UTF) { /* XXXX This is absolutely
3948 broken, we read before
3950 s = HOPMAYBEc(locinput, -scan->flags);
3956 if (locinput < PL_bostr + scan->flags)
3958 PL_reginput = locinput - scan->flags;
3963 PL_reginput = locinput;
3968 if (UTF) { /* XXXX This is absolutely
3969 broken, we read before
3971 s = HOPMAYBEc(locinput, -scan->flags);
3972 if (!s || s < PL_bostr)
3977 if (locinput < PL_bostr + scan->flags)
3979 PL_reginput = locinput - scan->flags;
3984 PL_reginput = locinput;
3987 inner = NEXTOPER(NEXTOPER(scan));
3988 if (regmatch(inner) != n) {
4003 if (OP(scan) == SUSPEND) {
4004 locinput = PL_reginput;
4005 nextchr = UCHARAT(locinput);
4010 next = scan + ARG(scan);
4015 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
4016 (unsigned long)scan, OP(scan));
4017 Perl_croak(aTHX_ "regexp memory corruption");
4023 * We get here only if there's trouble -- normally "case END" is
4024 * the terminating point.
4026 Perl_croak(aTHX_ "corrupted regexp pointers");
4044 - regrepeat - repeatedly match something simple, report how many
4047 * [This routine now assumes that it will only match on things of length 1.
4048 * That was true before, but now we assume scan - reginput is the count,
4049 * rather than incrementing count on every character. [Er, except utf8.]]
4052 S_regrepeat(pTHX_ regnode *p, I32 max)
4055 register char *scan;
4056 register char *opnd;
4058 register char *loceol = PL_regeol;
4059 register I32 hardcount = 0;
4062 if (max != REG_INFTY && max < loceol - scan)
4063 loceol = scan + max;
4064 opnd = (char *) OPERAND(p);
4067 while (scan < loceol && *scan != '\n')
4075 while (scan < loceol && *scan != '\n') {
4076 scan += UTF8SKIP(scan);
4082 while (scan < loceol) {
4083 scan += UTF8SKIP(scan);
4087 case EXACT: /* length of string is 1 */
4088 c = UCHARAT(++opnd);
4089 while (scan < loceol && UCHARAT(scan) == c)
4092 case EXACTF: /* length of string is 1 */
4093 c = UCHARAT(++opnd);
4094 while (scan < loceol &&
4095 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4098 case EXACTFL: /* length of string is 1 */
4099 PL_reg_flags |= RF_tainted;
4100 c = UCHARAT(++opnd);
4101 while (scan < loceol &&
4102 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4107 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
4108 scan += UTF8SKIP(scan);
4113 while (scan < loceol && REGINCLASS(opnd, *scan))
4117 while (scan < loceol && isALNUM(*scan))
4122 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4123 scan += UTF8SKIP(scan);
4128 PL_reg_flags |= RF_tainted;
4129 while (scan < loceol && isALNUM_LC(*scan))
4133 PL_reg_flags |= RF_tainted;
4135 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
4136 scan += UTF8SKIP(scan);
4142 while (scan < loceol && !isALNUM(*scan))
4147 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4148 scan += UTF8SKIP(scan);
4153 PL_reg_flags |= RF_tainted;
4154 while (scan < loceol && !isALNUM_LC(*scan))
4158 PL_reg_flags |= RF_tainted;
4160 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
4161 scan += UTF8SKIP(scan);
4166 while (scan < loceol && isSPACE(*scan))
4171 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4172 scan += UTF8SKIP(scan);
4177 PL_reg_flags |= RF_tainted;
4178 while (scan < loceol && isSPACE_LC(*scan))
4182 PL_reg_flags |= RF_tainted;
4184 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4185 scan += UTF8SKIP(scan);
4190 while (scan < loceol && !isSPACE(*scan))
4195 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4196 scan += UTF8SKIP(scan);
4201 PL_reg_flags |= RF_tainted;
4202 while (scan < loceol && !isSPACE_LC(*scan))
4206 PL_reg_flags |= RF_tainted;
4208 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4209 scan += UTF8SKIP(scan);
4214 while (scan < loceol && isDIGIT(*scan))
4219 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
4220 scan += UTF8SKIP(scan);
4226 while (scan < loceol && !isDIGIT(*scan))
4231 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
4232 scan += UTF8SKIP(scan);
4236 default: /* Called on something of 0 width. */
4237 break; /* So match right here or not at all. */
4243 c = scan - PL_reginput;
4248 SV *prop = sv_newmortal();
4251 PerlIO_printf(Perl_debug_log,
4252 "%*s %s can match %ld times out of %ld...\n",
4253 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
4260 - regrepeat_hard - repeatedly match something, report total lenth and length
4262 * The repeater is supposed to have constant length.
4266 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4269 register char *scan;
4270 register char *start;
4271 register char *loceol = PL_regeol;
4273 I32 count = 0, res = 1;
4278 start = PL_reginput;
4280 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4283 while (start < PL_reginput) {
4285 start += UTF8SKIP(start);
4296 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4298 *lp = l = PL_reginput - start;
4299 if (max != REG_INFTY && l*max < loceol - scan)
4300 loceol = scan + l*max;
4313 - reginclass - determine if a character falls into a character class
4317 S_reginclass(pTHX_ register char *p, register I32 c)
4320 char flags = ANYOF_FLAGS(p);
4324 if (ANYOF_BITMAP_TEST(p, c))
4326 else if (flags & ANYOF_FOLD) {
4328 if (flags & ANYOF_LOCALE) {
4329 PL_reg_flags |= RF_tainted;
4330 cf = PL_fold_locale[c];
4334 if (ANYOF_BITMAP_TEST(p, cf))
4338 if (!match && (flags & ANYOF_CLASS)) {
4339 PL_reg_flags |= RF_tainted;
4341 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4342 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4343 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
4344 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4345 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4346 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4347 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4348 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4349 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4350 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4351 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
4352 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
4353 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4354 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4355 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4356 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4357 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
4358 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4359 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
4360 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4361 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4362 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4363 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
4364 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4365 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4366 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
4367 ) /* How's that for a conditional? */
4373 return (flags & ANYOF_INVERT) ? !match : match;
4377 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
4380 char flags = ARG1(f);
4382 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
4384 if (swash_fetch(sv, p))
4386 else if (flags & ANYOF_FOLD) {
4389 if (flags & ANYOF_LOCALE) {
4390 PL_reg_flags |= RF_tainted;
4391 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
4394 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
4395 if (swash_fetch(sv, tmpbuf))
4399 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
4401 return (flags & ANYOF_INVERT) ? !match : match;
4405 S_reghop(pTHX_ U8 *s, I32 off)
4409 while (off-- && s < (U8*)PL_regeol)
4414 if (s > (U8*)PL_bostr) {
4417 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4419 } /* XXX could check well-formedness here */
4427 S_reghopmaybe(pTHX_ U8* s, I32 off)
4431 while (off-- && s < (U8*)PL_regeol)
4438 if (s > (U8*)PL_bostr) {
4441 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4443 } /* XXX could check well-formedness here */
4460 restore_pos(pTHXo_ void *arg)
4463 if (PL_reg_eval_set) {
4464 if (PL_reg_oldsaved) {
4465 PL_reg_re->subbeg = PL_reg_oldsaved;
4466 PL_reg_re->sublen = PL_reg_oldsavedlen;
4467 RX_MATCH_COPIED_on(PL_reg_re);
4469 PL_reg_magic->mg_len = PL_reg_oldpos;
4470 PL_reg_eval_set = 0;
4471 PL_curpm = PL_reg_oldcurpm;