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 PL_regeol = strend; /* Used in HOP() */
325 s = (char*)HOP((U8*)strpos, prog->check_offset_min);
326 if (SvTAIL(prog->check_substr)) {
327 slen = SvCUR(prog->check_substr); /* >= 1 */
329 if ( strend - s > slen || strend - s < slen - 1 ) {
333 if ( strend - s == slen && strend[-1] != '\n') {
337 /* Now should match s[0..slen-2] */
339 if (slen && (*SvPVX(prog->check_substr) != *s
341 && memNE(SvPVX(prog->check_substr), s, slen))))
344 else if (*SvPVX(prog->check_substr) != *s
345 || ((slen = SvCUR(prog->check_substr)) > 1
346 && memNE(SvPVX(prog->check_substr), s, slen)))
353 if (!ml_anch && (s + prog->check_offset_max < strend - prog->minlen))
354 end_shift += strend - s - prog->minlen - prog->check_offset_max;
363 end_shift = 0; /* can happen when strend == strpos */
364 if (flags & REXEC_SCREAM) {
365 SV *c = prog->check_substr;
366 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
367 I32 p = -1; /* Internal iterator of scream. */
368 I32 *pp = data ? data->scream_pos : &p;
370 if (PL_screamfirst[BmRARE(c)] >= 0
371 || ( BmRARE(c) == '\n'
372 && (BmPREVIOUS(c) == SvCUR(c) - 1)
374 s = screaminstr(sv, prog->check_substr,
375 start_shift + (strpos - strbeg), end_shift, pp, 0);
379 *data->scream_olds = s;
382 s = fbm_instr((unsigned char*)s + start_shift,
383 (unsigned char*)strend - end_shift,
384 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
386 /* Update the count-of-usability, remove useless subpatterns,
390 ++BmUSEFUL(prog->check_substr); /* hooray */
391 goto fail; /* not present */
393 else if (s - strpos > prog->check_offset_max &&
394 ((prog->reganch & ROPT_UTF8)
395 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
397 : (t = s - prog->check_offset_max) != 0) ) {
398 if (ml_anch && t[-1] != '\n') {
400 while (t < strend - end_shift - prog->minlen) {
402 if (t < s - prog->check_offset_min) {
416 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
420 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
424 if (!(prog->reganch & ROPT_NAUGHTY)
425 && --BmUSEFUL(prog->check_substr) < 0
426 && prog->check_substr == prog->float_substr) { /* boo */
427 /* If flags & SOMETHING - do not do it many times on the same match */
428 SvREFCNT_dec(prog->check_substr);
429 prog->check_substr = Nullsv; /* disable */
430 prog->float_substr = Nullsv; /* clear */
432 prog->reganch &= ~RE_USE_INTUIT;
438 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sFound%s at offset %ld\n",
439 PL_colors[4],PL_colors[5], (long)(s - strpos)) );
442 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sNot found...%s\n",
443 PL_colors[4],PL_colors[5]));
448 - regexec_flags - match a regexp against a string
451 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
452 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
453 /* strend: pointer to null at end of string */
454 /* strbeg: real beginning of string */
455 /* minend: end of match must be >=minend after stringarg. */
456 /* data: May be used for some additional optimizations. */
457 /* nosave: For optimizations. */
462 register char *startpos = stringarg;
464 I32 minlen; /* must match at least this many chars */
465 I32 dontbother = 0; /* how many characters not to try at end */
467 I32 start_shift = 0; /* Offset of the start to find
468 constant substr. */ /* CC */
469 I32 end_shift = 0; /* Same for the end. */ /* CC */
470 I32 scream_pos = -1; /* Internal iterator of scream. */
472 SV* oreplsv = GvSV(PL_replgv);
480 PL_regnarrate = PL_debug & 512;
484 if (prog == NULL || startpos == NULL) {
485 Perl_croak(aTHX_ "NULL regexp parameter");
489 minlen = prog->minlen;
490 if (strend - startpos < minlen) goto phooey;
492 if (startpos == strbeg) /* is ^ valid at stringarg? */
495 PL_regprev = (U32)stringarg[-1];
496 if (!PL_multiline && PL_regprev == '\n')
497 PL_regprev = '\0'; /* force ^ to NOT match */
500 /* Check validity of program. */
501 if (UCHARAT(prog->program) != REG_MAGIC) {
502 Perl_croak(aTHX_ "corrupted regexp program");
508 if (prog->reganch & ROPT_UTF8)
509 PL_reg_flags |= RF_utf8;
511 /* Mark beginning of line for ^ and lookbehind. */
512 PL_regbol = startpos;
516 /* Mark end of line for $ (and such) */
519 /* see how far we have to get to not match where we matched before */
520 PL_regtill = startpos+minend;
522 /* We start without call_cc context. */
525 /* If there is a "must appear" string, look for it. */
528 if (prog->reganch & ROPT_GPOS_SEEN) {
531 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
532 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
533 PL_reg_ganch = strbeg + mg->mg_len;
535 PL_reg_ganch = startpos;
536 if (prog->reganch & ROPT_ANCH_GPOS) {
537 if (s > PL_reg_ganch)
543 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
544 re_scream_pos_data d;
546 d.scream_olds = &scream_olds;
547 d.scream_pos = &scream_pos;
548 s = re_intuit_start(prog, sv, s, strend, flags, &d);
550 goto phooey; /* not present */
553 DEBUG_r( if (!PL_colorset) reginitcolors() );
554 DEBUG_r(PerlIO_printf(Perl_debug_log,
555 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
556 PL_colors[4],PL_colors[5],PL_colors[0],
559 (strlen(prog->precomp) > 60 ? "..." : ""),
561 (strend - startpos > 60 ? 60 : strend - startpos),
562 startpos, PL_colors[1],
563 (strend - startpos > 60 ? "..." : ""))
566 /* Simplest case: anchored match need be tried only once. */
567 /* [unless only anchor is BOL and multiline is set] */
568 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
569 if (s == startpos && regtry(prog, startpos))
571 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
572 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
577 dontbother = minlen - 1;
578 end = HOPc(strend, -dontbother) - 1;
579 /* for multiline we only have to try after newlines */
580 if (prog->check_substr) {
586 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
594 if (*s++ == '\n') { /* don't need PL_utf8skip here */
602 } else if (prog->reganch & ROPT_ANCH_GPOS) {
603 if (regtry(prog, PL_reg_ganch))
608 /* Messy cases: unanchored match. */
609 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
610 /* we have /x+whatever/ */
611 /* it must be a one character string (XXXX Except UTF?) */
612 char ch = SvPVX(prog->anchored_substr)[0];
616 if (regtry(prog, s)) goto got_it;
618 while (s < strend && *s == ch)
627 if (regtry(prog, s)) goto got_it;
629 while (s < strend && *s == ch)
637 else if (prog->anchored_substr != Nullsv
638 || (prog->float_substr != Nullsv
639 && prog->float_max_offset < strend - s)) {
640 SV *must = prog->anchored_substr
641 ? prog->anchored_substr : prog->float_substr;
643 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
645 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
646 I32 delta = back_max - back_min;
647 char *last = HOPc(strend, /* Cannot start after this */
648 -(I32)(CHR_SVLEN(must)
649 - (SvTAIL(must) != 0) + back_min));
650 char *last1; /* Last position checked before */
655 last1 = s - 1; /* bogus */
657 /* XXXX check_substr already used to find `s', can optimize if
658 check_substr==must. */
660 dontbother = end_shift;
661 strend = HOPc(strend, -dontbother);
662 while ( (s <= last) &&
663 ((flags & REXEC_SCREAM)
664 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
665 end_shift, &scream_pos, 0))
666 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
667 (unsigned char*)strend, must,
668 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
669 if (HOPc(s, -back_max) > last1) {
670 last1 = HOPc(s, -back_min);
671 s = HOPc(s, -back_max);
674 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
676 last1 = HOPc(s, -back_min);
696 else if (c = prog->regstclass) {
697 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
701 dontbother = minlen - 1;
702 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
704 /* We know what class it must start with. */
707 cc = (char *) OPERAND(c);
709 if (REGINCLASSUTF8(c, (U8*)s)) {
710 if (tmp && regtry(prog, s))
721 cc = (char *) OPERAND(c);
723 if (REGINCLASS(cc, *s)) {
724 if (tmp && regtry(prog, s))
735 PL_reg_flags |= RF_tainted;
742 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
743 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
745 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
752 if ((minlen || tmp) && regtry(prog,s))
756 PL_reg_flags |= RF_tainted;
761 strend = reghop_c(strend, -1);
763 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
764 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
766 if (tmp == !(OP(c) == BOUND ?
767 swash_fetch(PL_utf8_alnum, (U8*)s) :
768 isALNUM_LC_utf8((U8*)s)))
776 if ((minlen || tmp) && regtry(prog,s))
780 PL_reg_flags |= RF_tainted;
787 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
788 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
790 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
792 else if (regtry(prog, s))
796 if ((minlen || !tmp) && regtry(prog,s))
800 PL_reg_flags |= RF_tainted;
805 strend = reghop_c(strend, -1);
807 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
808 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
810 if (tmp == !(OP(c) == NBOUND ?
811 swash_fetch(PL_utf8_alnum, (U8*)s) :
812 isALNUM_LC_utf8((U8*)s)))
814 else if (regtry(prog, s))
818 if ((minlen || !tmp) && regtry(prog,s))
824 if (tmp && regtry(prog, s))
836 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
837 if (tmp && regtry(prog, s))
848 PL_reg_flags |= RF_tainted;
850 if (isALNUM_LC(*s)) {
851 if (tmp && regtry(prog, s))
862 PL_reg_flags |= RF_tainted;
864 if (isALNUM_LC_utf8((U8*)s)) {
865 if (tmp && regtry(prog, s))
878 if (tmp && regtry(prog, s))
890 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
891 if (tmp && regtry(prog, s))
902 PL_reg_flags |= RF_tainted;
904 if (!isALNUM_LC(*s)) {
905 if (tmp && regtry(prog, s))
916 PL_reg_flags |= RF_tainted;
918 if (!isALNUM_LC_utf8((U8*)s)) {
919 if (tmp && regtry(prog, s))
932 if (tmp && regtry(prog, s))
944 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
945 if (tmp && regtry(prog, s))
956 PL_reg_flags |= RF_tainted;
958 if (isSPACE_LC(*s)) {
959 if (tmp && regtry(prog, s))
970 PL_reg_flags |= RF_tainted;
972 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
973 if (tmp && regtry(prog, s))
986 if (tmp && regtry(prog, s))
998 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
999 if (tmp && regtry(prog, s))
1010 PL_reg_flags |= RF_tainted;
1011 while (s < strend) {
1012 if (!isSPACE_LC(*s)) {
1013 if (tmp && regtry(prog, s))
1024 PL_reg_flags |= RF_tainted;
1025 while (s < strend) {
1026 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1027 if (tmp && regtry(prog, s))
1038 while (s < strend) {
1040 if (tmp && regtry(prog, s))
1051 while (s < strend) {
1052 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1053 if (tmp && regtry(prog, s))
1064 PL_reg_flags |= RF_tainted;
1065 while (s < strend) {
1066 if (isDIGIT_LC(*s)) {
1067 if (tmp && regtry(prog, s))
1078 PL_reg_flags |= RF_tainted;
1079 while (s < strend) {
1080 if (isDIGIT_LC_utf8((U8*)s)) {
1081 if (tmp && regtry(prog, s))
1092 while (s < strend) {
1094 if (tmp && regtry(prog, s))
1105 while (s < strend) {
1106 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1107 if (tmp && regtry(prog, s))
1118 PL_reg_flags |= RF_tainted;
1119 while (s < strend) {
1120 if (!isDIGIT_LC(*s)) {
1121 if (tmp && regtry(prog, s))
1132 PL_reg_flags |= RF_tainted;
1133 while (s < strend) {
1134 if (!isDIGIT_LC_utf8((U8*)s)) {
1135 if (tmp && regtry(prog, s))
1146 while (s < strend) {
1148 if (tmp && regtry(prog, s))
1159 while (s < strend) {
1160 if (swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1161 if (tmp && regtry(prog, s))
1172 PL_reg_flags |= RF_tainted;
1173 while (s < strend) {
1174 if (isALNUMC_LC(*s)) {
1175 if (tmp && regtry(prog, s))
1186 PL_reg_flags |= RF_tainted;
1187 while (s < strend) {
1188 if (isALNUMC_LC_utf8((U8*)s)) {
1189 if (tmp && regtry(prog, s))
1200 while (s < strend) {
1201 if (!isALNUMC(*s)) {
1202 if (tmp && regtry(prog, s))
1213 while (s < strend) {
1214 if (!swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1215 if (tmp && regtry(prog, s))
1226 PL_reg_flags |= RF_tainted;
1227 while (s < strend) {
1228 if (!isALNUMC_LC(*s)) {
1229 if (tmp && regtry(prog, s))
1240 PL_reg_flags |= RF_tainted;
1241 while (s < strend) {
1242 if (!isALNUMC_LC_utf8((U8*)s)) {
1243 if (tmp && regtry(prog, s))
1254 while (s < strend) {
1255 if (isASCII(*(U8*)s)) {
1256 if (tmp && regtry(prog, s))
1267 while (s < strend) {
1268 if (!isASCII(*(U8*)s)) {
1269 if (tmp && regtry(prog, s))
1280 while (s < strend) {
1282 if (tmp && regtry(prog, s))
1293 while (s < strend) {
1294 if (swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1295 if (tmp && regtry(prog, s))
1306 PL_reg_flags |= RF_tainted;
1307 while (s < strend) {
1308 if (isCNTRL_LC(*s)) {
1309 if (tmp && regtry(prog, s))
1320 PL_reg_flags |= RF_tainted;
1321 while (s < strend) {
1322 if (*s == ' ' || isCNTRL_LC_utf8((U8*)s)) {
1323 if (tmp && regtry(prog, s))
1334 while (s < strend) {
1336 if (tmp && regtry(prog, s))
1347 while (s < strend) {
1348 if (!swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1349 if (tmp && regtry(prog, s))
1360 PL_reg_flags |= RF_tainted;
1361 while (s < strend) {
1362 if (!isCNTRL_LC(*s)) {
1363 if (tmp && regtry(prog, s))
1374 PL_reg_flags |= RF_tainted;
1375 while (s < strend) {
1376 if (!isCNTRL_LC_utf8((U8*)s)) {
1377 if (tmp && regtry(prog, s))
1388 while (s < strend) {
1390 if (tmp && regtry(prog, s))
1401 while (s < strend) {
1402 if (swash_fetch(PL_utf8_graph,(U8*)s)) {
1403 if (tmp && regtry(prog, s))
1414 PL_reg_flags |= RF_tainted;
1415 while (s < strend) {
1416 if (isGRAPH_LC(*s)) {
1417 if (tmp && regtry(prog, s))
1428 PL_reg_flags |= RF_tainted;
1429 while (s < strend) {
1430 if (*s == ' ' || isGRAPH_LC_utf8((U8*)s)) {
1431 if (tmp && regtry(prog, s))
1442 while (s < strend) {
1444 if (tmp && regtry(prog, s))
1455 while (s < strend) {
1456 if (!swash_fetch(PL_utf8_graph,(U8*)s)) {
1457 if (tmp && regtry(prog, s))
1468 PL_reg_flags |= RF_tainted;
1469 while (s < strend) {
1470 if (!isGRAPH_LC(*s)) {
1471 if (tmp && regtry(prog, s))
1482 PL_reg_flags |= RF_tainted;
1483 while (s < strend) {
1484 if (!isGRAPH_LC_utf8((U8*)s)) {
1485 if (tmp && regtry(prog, s))
1496 while (s < strend) {
1498 if (tmp && regtry(prog, s))
1509 while (s < strend) {
1510 if (swash_fetch(PL_utf8_lower,(U8*)s)) {
1511 if (tmp && regtry(prog, s))
1522 PL_reg_flags |= RF_tainted;
1523 while (s < strend) {
1524 if (isLOWER_LC(*s)) {
1525 if (tmp && regtry(prog, s))
1536 PL_reg_flags |= RF_tainted;
1537 while (s < strend) {
1538 if (*s == ' ' || isLOWER_LC_utf8((U8*)s)) {
1539 if (tmp && regtry(prog, s))
1550 while (s < strend) {
1552 if (tmp && regtry(prog, s))
1563 while (s < strend) {
1564 if (!swash_fetch(PL_utf8_lower,(U8*)s)) {
1565 if (tmp && regtry(prog, s))
1576 PL_reg_flags |= RF_tainted;
1577 while (s < strend) {
1578 if (!isLOWER_LC(*s)) {
1579 if (tmp && regtry(prog, s))
1590 PL_reg_flags |= RF_tainted;
1591 while (s < strend) {
1592 if (!isLOWER_LC_utf8((U8*)s)) {
1593 if (tmp && regtry(prog, s))
1604 while (s < strend) {
1606 if (tmp && regtry(prog, s))
1617 while (s < strend) {
1618 if (swash_fetch(PL_utf8_print,(U8*)s)) {
1619 if (tmp && regtry(prog, s))
1630 PL_reg_flags |= RF_tainted;
1631 while (s < strend) {
1632 if (isPRINT_LC(*s)) {
1633 if (tmp && regtry(prog, s))
1644 PL_reg_flags |= RF_tainted;
1645 while (s < strend) {
1646 if (*s == ' ' || isPRINT_LC_utf8((U8*)s)) {
1647 if (tmp && regtry(prog, s))
1658 while (s < strend) {
1660 if (tmp && regtry(prog, s))
1671 while (s < strend) {
1672 if (!swash_fetch(PL_utf8_print,(U8*)s)) {
1673 if (tmp && regtry(prog, s))
1684 PL_reg_flags |= RF_tainted;
1685 while (s < strend) {
1686 if (!isPRINT_LC(*s)) {
1687 if (tmp && regtry(prog, s))
1698 PL_reg_flags |= RF_tainted;
1699 while (s < strend) {
1700 if (!isPRINT_LC_utf8((U8*)s)) {
1701 if (tmp && regtry(prog, s))
1712 while (s < strend) {
1714 if (tmp && regtry(prog, s))
1725 while (s < strend) {
1726 if (swash_fetch(PL_utf8_punct,(U8*)s)) {
1727 if (tmp && regtry(prog, s))
1738 PL_reg_flags |= RF_tainted;
1739 while (s < strend) {
1740 if (isPUNCT_LC(*s)) {
1741 if (tmp && regtry(prog, s))
1752 PL_reg_flags |= RF_tainted;
1753 while (s < strend) {
1754 if (*s == ' ' || isPUNCT_LC_utf8((U8*)s)) {
1755 if (tmp && regtry(prog, s))
1766 while (s < strend) {
1768 if (tmp && regtry(prog, s))
1779 while (s < strend) {
1780 if (!swash_fetch(PL_utf8_punct,(U8*)s)) {
1781 if (tmp && regtry(prog, s))
1792 PL_reg_flags |= RF_tainted;
1793 while (s < strend) {
1794 if (!isPUNCT_LC(*s)) {
1795 if (tmp && regtry(prog, s))
1806 PL_reg_flags |= RF_tainted;
1807 while (s < strend) {
1808 if (!isPUNCT_LC_utf8((U8*)s)) {
1809 if (tmp && regtry(prog, s))
1820 while (s < strend) {
1822 if (tmp && regtry(prog, s))
1833 while (s < strend) {
1834 if (swash_fetch(PL_utf8_upper,(U8*)s)) {
1835 if (tmp && regtry(prog, s))
1846 PL_reg_flags |= RF_tainted;
1847 while (s < strend) {
1848 if (isUPPER_LC(*s)) {
1849 if (tmp && regtry(prog, s))
1860 PL_reg_flags |= RF_tainted;
1861 while (s < strend) {
1862 if (*s == ' ' || isUPPER_LC_utf8((U8*)s)) {
1863 if (tmp && regtry(prog, s))
1874 while (s < strend) {
1876 if (tmp && regtry(prog, s))
1887 while (s < strend) {
1888 if (!swash_fetch(PL_utf8_upper,(U8*)s)) {
1889 if (tmp && regtry(prog, s))
1900 PL_reg_flags |= RF_tainted;
1901 while (s < strend) {
1902 if (!isUPPER_LC(*s)) {
1903 if (tmp && regtry(prog, s))
1914 PL_reg_flags |= RF_tainted;
1915 while (s < strend) {
1916 if (!isUPPER_LC_utf8((U8*)s)) {
1917 if (tmp && regtry(prog, s))
1928 while (s < strend) {
1930 if (tmp && regtry(prog, s))
1941 while (s < strend) {
1942 if (!isXDIGIT(*s)) {
1943 if (tmp && regtry(prog, s))
1957 if (prog->float_substr != Nullsv) { /* Trim the end. */
1959 I32 oldpos = scream_pos;
1961 if (flags & REXEC_SCREAM) {
1962 last = screaminstr(sv, prog->float_substr, s - strbeg,
1963 end_shift, &scream_pos, 1); /* last one */
1965 last = scream_olds; /* Only one occurence. */
1969 char *little = SvPV(prog->float_substr, len);
1971 if (SvTAIL(prog->float_substr)) {
1972 if (memEQ(strend - len + 1, little, len - 1))
1973 last = strend - len + 1;
1974 else if (!PL_multiline)
1975 last = memEQ(strend - len, little, len)
1976 ? strend - len : Nullch;
1982 last = rninstr(s, strend, little, little + len);
1984 last = strend; /* matching `$' */
1987 if (last == NULL) goto phooey; /* Should not happen! */
1988 dontbother = strend - last + prog->float_min_offset;
1990 if (minlen && (dontbother < minlen))
1991 dontbother = minlen - 1;
1992 strend -= dontbother; /* this one's always in bytes! */
1993 /* We don't know much -- general case. */
1996 if (regtry(prog, s))
2005 if (regtry(prog, s))
2007 } while (s++ < strend);
2015 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2017 if (PL_reg_eval_set) {
2018 /* Preserve the current value of $^R */
2019 if (oreplsv != GvSV(PL_replgv))
2020 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2021 restored, the value remains
2023 restore_pos(aTHXo_ 0);
2026 /* make sure $`, $&, $', and $digit will work later */
2027 if ( !(flags & REXEC_NOT_FIRST) ) {
2028 if (RX_MATCH_COPIED(prog)) {
2029 Safefree(prog->subbeg);
2030 RX_MATCH_COPIED_off(prog);
2032 if (flags & REXEC_COPY_STR) {
2033 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2035 s = savepvn(strbeg, i);
2038 RX_MATCH_COPIED_on(prog);
2041 prog->subbeg = strbeg;
2042 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2049 if (PL_reg_eval_set)
2050 restore_pos(aTHXo_ 0);
2055 - regtry - try match at specific point
2057 STATIC I32 /* 0 failure, 1 success */
2058 S_regtry(pTHX_ regexp *prog, char *startpos)
2066 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2069 PL_reg_eval_set = RS_init;
2071 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
2072 PL_stack_sp - PL_stack_base);
2074 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
2075 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2076 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2078 /* Apparently this is not needed, judging by wantarray. */
2079 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
2080 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2083 /* Make $_ available to executed code. */
2084 if (PL_reg_sv != DEFSV) {
2085 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
2090 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2091 && (mg = mg_find(PL_reg_sv, 'g')))) {
2092 /* prepare for quick setting of pos */
2093 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
2094 mg = mg_find(PL_reg_sv, 'g');
2098 PL_reg_oldpos = mg->mg_len;
2099 SAVEDESTRUCTOR(restore_pos, 0);
2102 New(22,PL_reg_curpm, 1, PMOP);
2103 PL_reg_curpm->op_pmregexp = prog;
2104 PL_reg_oldcurpm = PL_curpm;
2105 PL_curpm = PL_reg_curpm;
2106 if (RX_MATCH_COPIED(prog)) {
2107 /* Here is a serious problem: we cannot rewrite subbeg,
2108 since it may be needed if this match fails. Thus
2109 $` inside (?{}) could fail... */
2110 PL_reg_oldsaved = prog->subbeg;
2111 PL_reg_oldsavedlen = prog->sublen;
2112 RX_MATCH_COPIED_off(prog);
2115 PL_reg_oldsaved = Nullch;
2116 prog->subbeg = PL_bostr;
2117 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2119 prog->startp[0] = startpos - PL_bostr;
2120 PL_reginput = startpos;
2121 PL_regstartp = prog->startp;
2122 PL_regendp = prog->endp;
2123 PL_reglastparen = &prog->lastparen;
2124 prog->lastparen = 0;
2126 DEBUG_r(PL_reg_starttry = startpos);
2127 if (PL_reg_start_tmpl <= prog->nparens) {
2128 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2129 if(PL_reg_start_tmp)
2130 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2132 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2135 /* XXXX What this code is doing here?!!! There should be no need
2136 to do this again and again, PL_reglastparen should take care of
2140 if (prog->nparens) {
2141 for (i = prog->nparens; i >= 1; i--) {
2147 if (regmatch(prog->program + 1)) {
2148 prog->endp[0] = PL_reginput - PL_bostr;
2156 - regmatch - main matching routine
2158 * Conceptually the strategy is simple: check to see whether the current
2159 * node matches, call self recursively to see whether the rest matches,
2160 * and then act accordingly. In practice we make some effort to avoid
2161 * recursion, in particular by going through "ordinary" nodes (that don't
2162 * need to know whether the rest of the match failed) by a loop instead of
2165 /* [lwall] I've hoisted the register declarations to the outer block in order to
2166 * maybe save a little bit of pushing and popping on the stack. It also takes
2167 * advantage of machines that use a register save mask on subroutine entry.
2169 STATIC I32 /* 0 failure, 1 success */
2170 S_regmatch(pTHX_ regnode *prog)
2173 register regnode *scan; /* Current node. */
2174 regnode *next; /* Next node. */
2175 regnode *inner; /* Next node in internal branch. */
2176 register I32 nextchr; /* renamed nextchr - nextchar colides with
2177 function of same name */
2178 register I32 n; /* no or next */
2179 register I32 ln; /* len or last */
2180 register char *s; /* operand or save */
2181 register char *locinput = PL_reginput;
2182 register I32 c1, c2, paren; /* case fold search, parenth */
2183 int minmod = 0, sw = 0, logical = 0;
2188 /* Note that nextchr is a byte even in UTF */
2189 nextchr = UCHARAT(locinput);
2191 while (scan != NULL) {
2192 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
2194 # define sayYES goto yes
2195 # define sayNO goto no
2196 # define saySAME(x) if (x) goto yes; else goto no
2197 # define REPORT_CODE_OFF 24
2199 # define sayYES return 1
2200 # define sayNO return 0
2201 # define saySAME(x) return x
2204 SV *prop = sv_newmortal();
2205 int docolor = *PL_colors[0];
2206 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2207 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
2208 /* The part of the string before starttry has one color
2209 (pref0_len chars), between starttry and current
2210 position another one (pref_len - pref0_len chars),
2211 after the current position the third one.
2212 We assume that pref0_len <= pref_len, otherwise we
2213 decrease pref0_len. */
2214 int pref_len = (locinput - PL_bostr > (5 + taill) - l
2215 ? (5 + taill) - l : locinput - PL_bostr);
2216 int pref0_len = pref_len - (locinput - PL_reg_starttry);
2218 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2219 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2220 ? (5 + taill) - pref_len : PL_regeol - locinput);
2223 if (pref0_len > pref_len)
2224 pref0_len = pref_len;
2225 regprop(prop, scan);
2226 PerlIO_printf(Perl_debug_log,
2227 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
2228 locinput - PL_bostr,
2229 PL_colors[4], pref0_len,
2230 locinput - pref_len, PL_colors[5],
2231 PL_colors[2], pref_len - pref0_len,
2232 locinput - pref_len + pref0_len, PL_colors[3],
2233 (docolor ? "" : "> <"),
2234 PL_colors[0], l, locinput, PL_colors[1],
2235 15 - l - pref_len + 1,
2237 scan - PL_regprogram, PL_regindent*2, "",
2241 next = scan + NEXT_OFF(scan);
2247 if (locinput == PL_bostr
2248 ? PL_regprev == '\n'
2250 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2252 /* regtill = regbol; */
2257 if (locinput == PL_bostr
2258 ? PL_regprev == '\n'
2259 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2265 if (locinput == PL_regbol && PL_regprev == '\n')
2269 if (locinput == PL_reg_ganch)
2279 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2284 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2286 if (PL_regeol - locinput > 1)
2290 if (PL_regeol != locinput)
2294 if (nextchr & 0x80) {
2295 locinput += PL_utf8skip[nextchr];
2296 if (locinput > PL_regeol)
2298 nextchr = UCHARAT(locinput);
2301 if (!nextchr && locinput >= PL_regeol)
2303 nextchr = UCHARAT(++locinput);
2306 if (!nextchr && locinput >= PL_regeol)
2308 nextchr = UCHARAT(++locinput);
2311 if (nextchr & 0x80) {
2312 locinput += PL_utf8skip[nextchr];
2313 if (locinput > PL_regeol)
2315 nextchr = UCHARAT(locinput);
2318 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2320 nextchr = UCHARAT(++locinput);
2323 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2325 nextchr = UCHARAT(++locinput);
2328 s = (char *) OPERAND(scan);
2330 /* Inline the first character, for speed. */
2331 if (UCHARAT(s) != nextchr)
2333 if (PL_regeol - locinput < ln)
2335 if (ln > 1 && memNE(s, locinput, ln))
2338 nextchr = UCHARAT(locinput);
2341 PL_reg_flags |= RF_tainted;
2344 s = (char *) OPERAND(scan);
2350 c1 = OP(scan) == EXACTF;
2354 if (utf8_to_uv((U8*)s, 0) != (c1 ?
2355 toLOWER_utf8((U8*)l) :
2356 toLOWER_LC_utf8((U8*)l)))
2364 nextchr = UCHARAT(locinput);
2368 /* Inline the first character, for speed. */
2369 if (UCHARAT(s) != nextchr &&
2370 UCHARAT(s) != ((OP(scan) == EXACTF)
2371 ? PL_fold : PL_fold_locale)[nextchr])
2373 if (PL_regeol - locinput < ln)
2375 if (ln > 1 && (OP(scan) == EXACTF
2376 ? ibcmp(s, locinput, ln)
2377 : ibcmp_locale(s, locinput, ln)))
2380 nextchr = UCHARAT(locinput);
2383 s = (char *) OPERAND(scan);
2384 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2386 if (locinput >= PL_regeol)
2388 locinput += PL_utf8skip[nextchr];
2389 nextchr = UCHARAT(locinput);
2392 s = (char *) OPERAND(scan);
2394 nextchr = UCHARAT(locinput);
2395 if (!REGINCLASS(s, nextchr))
2397 if (!nextchr && locinput >= PL_regeol)
2399 nextchr = UCHARAT(++locinput);
2402 PL_reg_flags |= RF_tainted;
2407 if (!(OP(scan) == ALNUM
2408 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2410 nextchr = UCHARAT(++locinput);
2413 PL_reg_flags |= RF_tainted;
2418 if (nextchr & 0x80) {
2419 if (!(OP(scan) == ALNUMUTF8
2420 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2421 : isALNUM_LC_utf8((U8*)locinput)))
2425 locinput += PL_utf8skip[nextchr];
2426 nextchr = UCHARAT(locinput);
2429 if (!(OP(scan) == ALNUMUTF8
2430 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2432 nextchr = UCHARAT(++locinput);
2435 PL_reg_flags |= RF_tainted;
2438 if (!nextchr && locinput >= PL_regeol)
2440 if (OP(scan) == NALNUM
2441 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2443 nextchr = UCHARAT(++locinput);
2446 PL_reg_flags |= RF_tainted;
2449 if (!nextchr && locinput >= PL_regeol)
2451 if (nextchr & 0x80) {
2452 if (OP(scan) == NALNUMUTF8
2453 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2454 : isALNUM_LC_utf8((U8*)locinput))
2458 locinput += PL_utf8skip[nextchr];
2459 nextchr = UCHARAT(locinput);
2462 if (OP(scan) == NALNUMUTF8
2463 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2465 nextchr = UCHARAT(++locinput);
2469 PL_reg_flags |= RF_tainted;
2473 /* was last char in word? */
2474 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2475 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2477 n = isALNUM(nextchr);
2480 ln = isALNUM_LC(ln);
2481 n = isALNUM_LC(nextchr);
2483 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2488 PL_reg_flags |= RF_tainted;
2492 /* was last char in word? */
2493 ln = (locinput != PL_regbol)
2494 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2495 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2496 ln = isALNUM_uni(ln);
2497 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2500 ln = isALNUM_LC_uni(ln);
2501 n = isALNUM_LC_utf8((U8*)locinput);
2503 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2507 PL_reg_flags |= RF_tainted;
2510 if (!nextchr && locinput >= PL_regeol)
2512 if (!(OP(scan) == SPACE
2513 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2515 nextchr = UCHARAT(++locinput);
2518 PL_reg_flags |= RF_tainted;
2521 if (!nextchr && locinput >= PL_regeol)
2523 if (nextchr & 0x80) {
2524 if (!(OP(scan) == SPACEUTF8
2525 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2526 : isSPACE_LC_utf8((U8*)locinput)))
2530 locinput += PL_utf8skip[nextchr];
2531 nextchr = UCHARAT(locinput);
2534 if (!(OP(scan) == SPACEUTF8
2535 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2537 nextchr = UCHARAT(++locinput);
2540 PL_reg_flags |= RF_tainted;
2545 if (OP(scan) == SPACE
2546 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2548 nextchr = UCHARAT(++locinput);
2551 PL_reg_flags |= RF_tainted;
2556 if (nextchr & 0x80) {
2557 if (OP(scan) == NSPACEUTF8
2558 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2559 : isSPACE_LC_utf8((U8*)locinput))
2563 locinput += PL_utf8skip[nextchr];
2564 nextchr = UCHARAT(locinput);
2567 if (OP(scan) == NSPACEUTF8
2568 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2570 nextchr = UCHARAT(++locinput);
2573 PL_reg_flags |= RF_tainted;
2576 if (!nextchr && locinput >= PL_regeol)
2578 if (!(OP(scan) == DIGIT
2579 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2581 nextchr = UCHARAT(++locinput);
2584 PL_reg_flags |= RF_tainted;
2589 if (nextchr & 0x80) {
2590 if (OP(scan) == NDIGITUTF8
2591 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2592 : isDIGIT_LC_utf8((U8*)locinput))
2596 locinput += PL_utf8skip[nextchr];
2597 nextchr = UCHARAT(locinput);
2600 if (!isDIGIT(nextchr))
2602 nextchr = UCHARAT(++locinput);
2605 PL_reg_flags |= RF_tainted;
2610 if (OP(scan) == DIGIT
2611 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2613 nextchr = UCHARAT(++locinput);
2616 PL_reg_flags |= RF_tainted;
2619 if (!nextchr && locinput >= PL_regeol)
2621 if (nextchr & 0x80) {
2622 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2624 locinput += PL_utf8skip[nextchr];
2625 nextchr = UCHARAT(locinput);
2628 if (isDIGIT(nextchr))
2630 nextchr = UCHARAT(++locinput);
2633 PL_reg_flags |= RF_tainted;
2638 if (!(OP(scan) == ALNUMC
2639 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2641 nextchr = UCHARAT(++locinput);
2644 PL_reg_flags |= RF_tainted;
2649 if (nextchr & 0x80) {
2650 if (!(OP(scan) == ALNUMCUTF8
2651 ? swash_fetch(PL_utf8_alnumc, (U8*)locinput)
2652 : isALNUMC_LC_utf8((U8*)locinput)))
2656 locinput += PL_utf8skip[nextchr];
2657 nextchr = UCHARAT(locinput);
2660 if (!(OP(scan) == ALNUMCUTF8
2661 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2663 nextchr = UCHARAT(++locinput);
2666 PL_reg_flags |= RF_tainted;
2671 if (OP(scan) == ALNUMC
2672 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr))
2674 nextchr = UCHARAT(++locinput);
2677 PL_reg_flags |= RF_tainted;
2680 if (!nextchr && locinput >= PL_regeol)
2682 if (nextchr & 0x80) {
2683 if (swash_fetch(PL_utf8_alnumc,(U8*)locinput))
2685 locinput += PL_utf8skip[nextchr];
2686 nextchr = UCHARAT(locinput);
2689 if (isALNUMC(nextchr))
2691 nextchr = UCHARAT(++locinput);
2694 PL_reg_flags |= RF_tainted;
2699 if (!(OP(scan) == ALPHA
2700 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2702 nextchr = UCHARAT(++locinput);
2705 PL_reg_flags |= RF_tainted;
2710 if (nextchr & 0x80) {
2711 if (!(OP(scan) == ALPHAUTF8
2712 ? swash_fetch(PL_utf8_alpha, (U8*)locinput)
2713 : isALPHA_LC_utf8((U8*)locinput)))
2717 locinput += PL_utf8skip[nextchr];
2718 nextchr = UCHARAT(locinput);
2721 if (!(OP(scan) == ALPHAUTF8
2722 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2724 nextchr = UCHARAT(++locinput);
2727 PL_reg_flags |= RF_tainted;
2732 if (OP(scan) == ALPHA
2733 ? isALPHA(nextchr) : isALPHA_LC(nextchr))
2735 nextchr = UCHARAT(++locinput);
2738 PL_reg_flags |= RF_tainted;
2741 if (!nextchr && locinput >= PL_regeol)
2743 if (nextchr & 0x80) {
2744 if (swash_fetch(PL_utf8_alpha,(U8*)locinput))
2746 locinput += PL_utf8skip[nextchr];
2747 nextchr = UCHARAT(locinput);
2750 if (isALPHA(nextchr))
2752 nextchr = UCHARAT(++locinput);
2755 if (!nextchr && locinput >= PL_regeol)
2757 if (!isASCII(nextchr))
2759 nextchr = UCHARAT(++locinput);
2762 if (!nextchr && locinput >= PL_regeol)
2764 if (isASCII(nextchr))
2766 nextchr = UCHARAT(++locinput);
2769 PL_reg_flags |= RF_tainted;
2774 if (!(OP(scan) == CNTRL
2775 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2777 nextchr = UCHARAT(++locinput);
2780 PL_reg_flags |= RF_tainted;
2785 if (nextchr & 0x80) {
2786 if (!(OP(scan) == CNTRLUTF8
2787 ? swash_fetch(PL_utf8_cntrl, (U8*)locinput)
2788 : isCNTRL_LC_utf8((U8*)locinput)))
2792 locinput += PL_utf8skip[nextchr];
2793 nextchr = UCHARAT(locinput);
2796 if (!(OP(scan) == CNTRLUTF8
2797 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2799 nextchr = UCHARAT(++locinput);
2802 PL_reg_flags |= RF_tainted;
2807 if (OP(scan) == CNTRL
2808 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr))
2810 nextchr = UCHARAT(++locinput);
2813 PL_reg_flags |= RF_tainted;
2816 if (!nextchr && locinput >= PL_regeol)
2818 if (nextchr & 0x80) {
2819 if (swash_fetch(PL_utf8_cntrl,(U8*)locinput))
2821 locinput += PL_utf8skip[nextchr];
2822 nextchr = UCHARAT(locinput);
2825 if (isCNTRL(nextchr))
2827 nextchr = UCHARAT(++locinput);
2830 PL_reg_flags |= RF_tainted;
2835 if (!(OP(scan) == GRAPH
2836 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2838 nextchr = UCHARAT(++locinput);
2841 PL_reg_flags |= RF_tainted;
2846 if (nextchr & 0x80) {
2847 if (!(OP(scan) == GRAPHUTF8
2848 ? swash_fetch(PL_utf8_graph, (U8*)locinput)
2849 : isGRAPH_LC_utf8((U8*)locinput)))
2853 locinput += PL_utf8skip[nextchr];
2854 nextchr = UCHARAT(locinput);
2857 if (!(OP(scan) == GRAPHUTF8
2858 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2860 nextchr = UCHARAT(++locinput);
2863 PL_reg_flags |= RF_tainted;
2868 if (OP(scan) == GRAPH
2869 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr))
2871 nextchr = UCHARAT(++locinput);
2874 PL_reg_flags |= RF_tainted;
2877 if (!nextchr && locinput >= PL_regeol)
2879 if (nextchr & 0x80) {
2880 if (swash_fetch(PL_utf8_graph,(U8*)locinput))
2882 locinput += PL_utf8skip[nextchr];
2883 nextchr = UCHARAT(locinput);
2886 if (isGRAPH(nextchr))
2888 nextchr = UCHARAT(++locinput);
2891 PL_reg_flags |= RF_tainted;
2896 if (!(OP(scan) == LOWER
2897 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2899 nextchr = UCHARAT(++locinput);
2902 PL_reg_flags |= RF_tainted;
2907 if (nextchr & 0x80) {
2908 if (!(OP(scan) == LOWERUTF8
2909 ? swash_fetch(PL_utf8_lower, (U8*)locinput)
2910 : isLOWER_LC_utf8((U8*)locinput)))
2914 locinput += PL_utf8skip[nextchr];
2915 nextchr = UCHARAT(locinput);
2918 if (!(OP(scan) == LOWERUTF8
2919 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2921 nextchr = UCHARAT(++locinput);
2924 PL_reg_flags |= RF_tainted;
2929 if (OP(scan) == LOWER
2930 ? isLOWER(nextchr) : isLOWER_LC(nextchr))
2932 nextchr = UCHARAT(++locinput);
2935 PL_reg_flags |= RF_tainted;
2938 if (!nextchr && locinput >= PL_regeol)
2940 if (nextchr & 0x80) {
2941 if (swash_fetch(PL_utf8_lower,(U8*)locinput))
2943 locinput += PL_utf8skip[nextchr];
2944 nextchr = UCHARAT(locinput);
2947 if (isLOWER(nextchr))
2949 nextchr = UCHARAT(++locinput);
2952 PL_reg_flags |= RF_tainted;
2957 if (!(OP(scan) == PRINT
2958 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2960 nextchr = UCHARAT(++locinput);
2963 PL_reg_flags |= RF_tainted;
2968 if (nextchr & 0x80) {
2969 if (!(OP(scan) == PRINTUTF8
2970 ? swash_fetch(PL_utf8_print, (U8*)locinput)
2971 : isPRINT_LC_utf8((U8*)locinput)))
2975 locinput += PL_utf8skip[nextchr];
2976 nextchr = UCHARAT(locinput);
2979 if (!(OP(scan) == PRINTUTF8
2980 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2982 nextchr = UCHARAT(++locinput);
2985 PL_reg_flags |= RF_tainted;
2990 if (OP(scan) == PRINT
2991 ? isPRINT(nextchr) : isPRINT_LC(nextchr))
2993 nextchr = UCHARAT(++locinput);
2996 PL_reg_flags |= RF_tainted;
2999 if (!nextchr && locinput >= PL_regeol)
3001 if (nextchr & 0x80) {
3002 if (swash_fetch(PL_utf8_print,(U8*)locinput))
3004 locinput += PL_utf8skip[nextchr];
3005 nextchr = UCHARAT(locinput);
3008 if (isPRINT(nextchr))
3010 nextchr = UCHARAT(++locinput);
3013 PL_reg_flags |= RF_tainted;
3018 if (!(OP(scan) == PUNCT
3019 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3021 nextchr = UCHARAT(++locinput);
3024 PL_reg_flags |= RF_tainted;
3029 if (nextchr & 0x80) {
3030 if (!(OP(scan) == PUNCTUTF8
3031 ? swash_fetch(PL_utf8_punct, (U8*)locinput)
3032 : isPUNCT_LC_utf8((U8*)locinput)))
3036 locinput += PL_utf8skip[nextchr];
3037 nextchr = UCHARAT(locinput);
3040 if (!(OP(scan) == PUNCTUTF8
3041 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3043 nextchr = UCHARAT(++locinput);
3046 PL_reg_flags |= RF_tainted;
3051 if (OP(scan) == PUNCT
3052 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr))
3054 nextchr = UCHARAT(++locinput);
3057 PL_reg_flags |= RF_tainted;
3060 if (!nextchr && locinput >= PL_regeol)
3062 if (nextchr & 0x80) {
3063 if (swash_fetch(PL_utf8_punct,(U8*)locinput))
3065 locinput += PL_utf8skip[nextchr];
3066 nextchr = UCHARAT(locinput);
3069 if (isPUNCT(nextchr))
3071 nextchr = UCHARAT(++locinput);
3074 PL_reg_flags |= RF_tainted;
3079 if (!(OP(scan) == UPPER
3080 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3082 nextchr = UCHARAT(++locinput);
3085 PL_reg_flags |= RF_tainted;
3090 if (nextchr & 0x80) {
3091 if (!(OP(scan) == UPPERUTF8
3092 ? swash_fetch(PL_utf8_upper, (U8*)locinput)
3093 : isUPPER_LC_utf8((U8*)locinput)))
3097 locinput += PL_utf8skip[nextchr];
3098 nextchr = UCHARAT(locinput);
3101 if (!(OP(scan) == UPPERUTF8
3102 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3104 nextchr = UCHARAT(++locinput);
3107 PL_reg_flags |= RF_tainted;
3112 if (OP(scan) == UPPER
3113 ? isUPPER(nextchr) : isUPPER_LC(nextchr))
3115 nextchr = UCHARAT(++locinput);
3118 PL_reg_flags |= RF_tainted;
3121 if (!nextchr && locinput >= PL_regeol)
3123 if (nextchr & 0x80) {
3124 if (swash_fetch(PL_utf8_upper,(U8*)locinput))
3126 locinput += PL_utf8skip[nextchr];
3127 nextchr = UCHARAT(locinput);
3130 if (isUPPER(nextchr))
3132 nextchr = UCHARAT(++locinput);
3135 if (!nextchr && locinput >= PL_regeol)
3137 if (!isXDIGIT(nextchr))
3139 nextchr = UCHARAT(++locinput);
3142 if (!nextchr && locinput >= PL_regeol)
3144 if (isXDIGIT(nextchr))
3146 nextchr = UCHARAT(++locinput);
3149 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
3151 locinput += PL_utf8skip[nextchr];
3152 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
3153 locinput += UTF8SKIP(locinput);
3154 if (locinput > PL_regeol)
3156 nextchr = UCHARAT(locinput);
3159 PL_reg_flags |= RF_tainted;
3163 n = ARG(scan); /* which paren pair */
3164 ln = PL_regstartp[n];
3165 if (*PL_reglastparen < n || ln == -1)
3166 sayNO; /* Do not match unless seen CLOSEn. */
3167 if (ln == PL_regendp[n])
3171 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
3173 char *e = PL_bostr + PL_regendp[n];
3175 * Note that we can't do the "other character" lookup trick as
3176 * in the 8-bit case (no pun intended) because in Unicode we
3177 * have to map both upper and title case to lower case.
3179 if (OP(scan) == REFF) {
3183 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
3193 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
3200 nextchr = UCHARAT(locinput);
3204 /* Inline the first character, for speed. */
3205 if (UCHARAT(s) != nextchr &&
3207 (UCHARAT(s) != ((OP(scan) == REFF
3208 ? PL_fold : PL_fold_locale)[nextchr]))))
3210 ln = PL_regendp[n] - ln;
3211 if (locinput + ln > PL_regeol)
3213 if (ln > 1 && (OP(scan) == REF
3214 ? memNE(s, locinput, ln)
3216 ? ibcmp(s, locinput, ln)
3217 : ibcmp_locale(s, locinput, ln))))
3220 nextchr = UCHARAT(locinput);
3231 OP_4tree *oop = PL_op;
3232 COP *ocurcop = PL_curcop;
3233 SV **ocurpad = PL_curpad;
3237 PL_op = (OP_4tree*)PL_regdata->data[n];
3238 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
3239 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
3240 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3242 CALLRUNOPS(aTHX); /* Scalar context. */
3248 PL_curpad = ocurpad;
3249 PL_curcop = ocurcop;
3251 if (logical == 2) { /* Postponed subexpression. */
3253 MAGIC *mg = Null(MAGIC*);
3256 CHECKPOINT cp, lastcp;
3258 if(SvROK(ret) || SvRMAGICAL(ret)) {
3259 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
3262 mg = mg_find(sv, 'r');
3265 re = (regexp *)mg->mg_obj;
3266 (void)ReREFCNT_inc(re);
3270 char *t = SvPV(ret, len);
3272 char *oprecomp = PL_regprecomp;
3273 I32 osize = PL_regsize;
3274 I32 onpar = PL_regnpar;
3277 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
3279 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
3280 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
3281 PL_regprecomp = oprecomp;
3286 PerlIO_printf(Perl_debug_log,
3287 "Entering embedded `%s%.60s%s%s'\n",
3291 (strlen(re->precomp) > 60 ? "..." : ""))
3294 state.prev = PL_reg_call_cc;
3295 state.cc = PL_regcc;
3296 state.re = PL_reg_re;
3302 cp = regcppush(0); /* Save *all* the positions. */
3305 state.ss = PL_savestack_ix;
3306 *PL_reglastparen = 0;
3307 PL_reg_call_cc = &state;
3308 PL_reginput = locinput;
3309 if (regmatch(re->program + 1)) {
3315 PerlIO_printf(Perl_debug_log,
3317 REPORT_CODE_OFF+PL_regindent*2, "")
3322 PL_reg_call_cc = state.prev;
3323 PL_regcc = state.cc;
3324 PL_reg_re = state.re;
3325 cache_re(PL_reg_re);
3332 sv_setsv(save_scalar(PL_replgv), ret);
3336 n = ARG(scan); /* which paren pair */
3337 PL_reg_start_tmp[n] = locinput;
3342 n = ARG(scan); /* which paren pair */
3343 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3344 PL_regendp[n] = locinput - PL_bostr;
3345 if (n > *PL_reglastparen)
3346 *PL_reglastparen = n;
3349 n = ARG(scan); /* which paren pair */
3350 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
3354 next = NEXTOPER(NEXTOPER(scan));
3356 next = scan + ARG(scan);
3357 if (OP(next) == IFTHEN) /* Fake one. */
3358 next = NEXTOPER(NEXTOPER(next));
3362 logical = scan->flags;
3366 CHECKPOINT cp = PL_savestack_ix;
3368 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3370 cc.oldcc = PL_regcc;
3372 cc.parenfloor = *PL_reglastparen;
3374 cc.min = ARG1(scan);
3375 cc.max = ARG2(scan);
3376 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3380 PL_reginput = locinput;
3381 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3383 PL_regcc = cc.oldcc;
3389 * This is really hard to understand, because after we match
3390 * what we're trying to match, we must make sure the rest of
3391 * the RE is going to match for sure, and to do that we have
3392 * to go back UP the parse tree by recursing ever deeper. And
3393 * if it fails, we have to reset our parent's current state
3394 * that we can try again after backing off.
3397 CHECKPOINT cp, lastcp;
3398 CURCUR* cc = PL_regcc;
3399 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3401 n = cc->cur + 1; /* how many we know we matched */
3402 PL_reginput = locinput;
3405 PerlIO_printf(Perl_debug_log,
3406 "%*s %ld out of %ld..%ld cc=%lx\n",
3407 REPORT_CODE_OFF+PL_regindent*2, "",
3408 (long)n, (long)cc->min,
3409 (long)cc->max, (long)cc)
3412 /* If degenerate scan matches "", assume scan done. */
3414 if (locinput == cc->lastloc && n >= cc->min) {
3415 PL_regcc = cc->oldcc;
3418 PerlIO_printf(Perl_debug_log,
3419 "%*s empty match detected, try continuation...\n",
3420 REPORT_CODE_OFF+PL_regindent*2, "")
3422 if (regmatch(cc->next))
3425 PerlIO_printf(Perl_debug_log,
3427 REPORT_CODE_OFF+PL_regindent*2, "")
3434 /* First just match a string of min scans. */
3438 cc->lastloc = locinput;
3439 if (regmatch(cc->scan))
3442 cc->lastloc = lastloc;
3444 PerlIO_printf(Perl_debug_log,
3446 REPORT_CODE_OFF+PL_regindent*2, "")
3451 /* Prefer next over scan for minimal matching. */
3454 PL_regcc = cc->oldcc;
3456 cp = regcppush(cc->parenfloor);
3458 if (regmatch(cc->next)) {
3460 sayYES; /* All done. */
3467 if (n >= cc->max) { /* Maximum greed exceeded? */
3468 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3469 && !(PL_reg_flags & RF_warned)) {
3470 PL_reg_flags |= RF_warned;
3471 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3472 "Complex regular subexpression recursion",
3479 PerlIO_printf(Perl_debug_log,
3480 "%*s trying longer...\n",
3481 REPORT_CODE_OFF+PL_regindent*2, "")
3483 /* Try scanning more and see if it helps. */
3484 PL_reginput = locinput;
3486 cc->lastloc = locinput;
3487 cp = regcppush(cc->parenfloor);
3489 if (regmatch(cc->scan)) {
3494 PerlIO_printf(Perl_debug_log,
3496 REPORT_CODE_OFF+PL_regindent*2, "")
3501 cc->lastloc = lastloc;
3505 /* Prefer scan over next for maximal matching. */
3507 if (n < cc->max) { /* More greed allowed? */
3508 cp = regcppush(cc->parenfloor);
3510 cc->lastloc = locinput;
3512 if (regmatch(cc->scan)) {
3517 regcppop(); /* Restore some previous $<digit>s? */
3518 PL_reginput = locinput;
3520 PerlIO_printf(Perl_debug_log,
3521 "%*s failed, try continuation...\n",
3522 REPORT_CODE_OFF+PL_regindent*2, "")
3525 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3526 && !(PL_reg_flags & RF_warned)) {
3527 PL_reg_flags |= RF_warned;
3528 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3529 "Complex regular subexpression recursion",
3533 /* Failed deeper matches of scan, so see if this one works. */
3534 PL_regcc = cc->oldcc;
3536 if (regmatch(cc->next))
3539 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
3540 REPORT_CODE_OFF+PL_regindent*2, "")
3545 cc->lastloc = lastloc;
3550 next = scan + ARG(scan);
3553 inner = NEXTOPER(NEXTOPER(scan));
3556 inner = NEXTOPER(scan);
3561 if (OP(next) != c1) /* No choice. */
3562 next = inner; /* Avoid recursion. */
3564 int lastparen = *PL_reglastparen;
3568 PL_reginput = locinput;
3569 if (regmatch(inner))
3572 for (n = *PL_reglastparen; n > lastparen; n--)
3574 *PL_reglastparen = n;
3577 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
3581 inner = NEXTOPER(scan);
3582 if (c1 == BRANCHJ) {
3583 inner = NEXTOPER(inner);
3585 } while (scan != NULL && OP(scan) == c1);
3599 /* We suppose that the next guy does not need
3600 backtracking: in particular, it is of constant length,
3601 and has no parenths to influence future backrefs. */
3602 ln = ARG1(scan); /* min to match */
3603 n = ARG2(scan); /* max to match */
3604 paren = scan->flags;
3606 if (paren > PL_regsize)
3608 if (paren > *PL_reglastparen)
3609 *PL_reglastparen = paren;
3611 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3613 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3614 PL_reginput = locinput;
3617 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3619 if (ln && l == 0 && n >= ln
3620 /* In fact, this is tricky. If paren, then the
3621 fact that we did/didnot match may influence
3622 future execution. */
3623 && !(paren && ln == 0))
3625 locinput = PL_reginput;
3626 if (PL_regkind[(U8)OP(next)] == EXACT) {
3627 c1 = UCHARAT(OPERAND(next) + 1);
3628 if (OP(next) == EXACTF)
3630 else if (OP(next) == EXACTFL)
3631 c2 = PL_fold_locale[c1];
3638 /* This may be improved if l == 0. */
3639 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3640 /* If it could work, try it. */
3642 UCHARAT(PL_reginput) == c1 ||
3643 UCHARAT(PL_reginput) == c2)
3647 PL_regstartp[paren] =
3648 HOPc(PL_reginput, -l) - PL_bostr;
3649 PL_regendp[paren] = PL_reginput - PL_bostr;
3652 PL_regendp[paren] = -1;
3658 /* Couldn't or didn't -- move forward. */
3659 PL_reginput = locinput;
3660 if (regrepeat_hard(scan, 1, &l)) {
3662 locinput = PL_reginput;
3669 n = regrepeat_hard(scan, n, &l);
3670 if (n != 0 && l == 0
3671 /* In fact, this is tricky. If paren, then the
3672 fact that we did/didnot match may influence
3673 future execution. */
3674 && !(paren && ln == 0))
3676 locinput = PL_reginput;
3678 PerlIO_printf(Perl_debug_log,
3679 "%*s matched %ld times, len=%ld...\n",
3680 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
3683 if (PL_regkind[(U8)OP(next)] == EXACT) {
3684 c1 = UCHARAT(OPERAND(next) + 1);
3685 if (OP(next) == EXACTF)
3687 else if (OP(next) == EXACTFL)
3688 c2 = PL_fold_locale[c1];
3697 /* If it could work, try it. */
3699 UCHARAT(PL_reginput) == c1 ||
3700 UCHARAT(PL_reginput) == c2)
3703 PerlIO_printf(Perl_debug_log,
3704 "%*s trying tail with n=%ld...\n",
3705 REPORT_CODE_OFF+PL_regindent*2, "", n)
3709 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3710 PL_regendp[paren] = PL_reginput - PL_bostr;
3713 PL_regendp[paren] = -1;
3719 /* Couldn't or didn't -- back up. */
3721 locinput = HOPc(locinput, -l);
3722 PL_reginput = locinput;
3729 paren = scan->flags; /* Which paren to set */
3730 if (paren > PL_regsize)
3732 if (paren > *PL_reglastparen)
3733 *PL_reglastparen = paren;
3734 ln = ARG1(scan); /* min to match */
3735 n = ARG2(scan); /* max to match */
3736 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3740 ln = ARG1(scan); /* min to match */
3741 n = ARG2(scan); /* max to match */
3742 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3747 scan = NEXTOPER(scan);
3753 scan = NEXTOPER(scan);
3757 * Lookahead to avoid useless match attempts
3758 * when we know what character comes next.
3760 if (PL_regkind[(U8)OP(next)] == EXACT) {
3761 c1 = UCHARAT(OPERAND(next) + 1);
3762 if (OP(next) == EXACTF)
3764 else if (OP(next) == EXACTFL)
3765 c2 = PL_fold_locale[c1];
3771 PL_reginput = locinput;
3775 if (ln && regrepeat(scan, ln) < ln)
3777 locinput = PL_reginput;
3780 char *e = locinput + n - ln; /* Should not check after this */
3781 char *old = locinput;
3783 if (e >= PL_regeol || (n == REG_INFTY))
3786 /* Find place 'next' could work */
3788 while (locinput <= e && *locinput != c1)
3791 while (locinput <= e
3798 /* PL_reginput == old now */
3799 if (locinput != old) {
3800 ln = 1; /* Did some */
3801 if (regrepeat(scan, locinput - old) <
3805 /* PL_reginput == locinput now */
3808 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
3809 PL_regendp[paren] = locinput - PL_bostr;
3812 PL_regendp[paren] = -1;
3816 PL_reginput = locinput; /* Could be reset... */
3818 /* Couldn't or didn't -- move forward. */
3823 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3824 /* If it could work, try it. */
3826 UCHARAT(PL_reginput) == c1 ||
3827 UCHARAT(PL_reginput) == c2)
3831 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3832 PL_regendp[paren] = PL_reginput - PL_bostr;
3835 PL_regendp[paren] = -1;
3841 /* Couldn't or didn't -- move forward. */
3842 PL_reginput = locinput;
3843 if (regrepeat(scan, 1)) {
3845 locinput = PL_reginput;
3853 n = regrepeat(scan, n);
3854 locinput = PL_reginput;
3855 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3856 (!PL_multiline || OP(next) == SEOL))
3857 ln = n; /* why back off? */
3861 /* If it could work, try it. */
3863 UCHARAT(PL_reginput) == c1 ||
3864 UCHARAT(PL_reginput) == c2)
3868 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3869 PL_regendp[paren] = PL_reginput - PL_bostr;
3872 PL_regendp[paren] = -1;
3878 /* Couldn't or didn't -- back up. */
3880 PL_reginput = locinput = HOPc(locinput, -1);
3885 /* If it could work, try it. */
3887 UCHARAT(PL_reginput) == c1 ||
3888 UCHARAT(PL_reginput) == c2)
3894 /* Couldn't or didn't -- back up. */
3896 PL_reginput = locinput = HOPc(locinput, -1);
3903 if (PL_reg_call_cc) {
3904 re_cc_state *cur_call_cc = PL_reg_call_cc;
3905 CURCUR *cctmp = PL_regcc;
3906 regexp *re = PL_reg_re;
3907 CHECKPOINT cp, lastcp;
3909 cp = regcppush(0); /* Save *all* the positions. */
3911 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3913 PL_reginput = locinput; /* Make position available to
3915 cache_re(PL_reg_call_cc->re);
3916 PL_regcc = PL_reg_call_cc->cc;
3917 PL_reg_call_cc = PL_reg_call_cc->prev;
3918 if (regmatch(cur_call_cc->node)) {
3919 PL_reg_call_cc = cur_call_cc;
3925 PL_reg_call_cc = cur_call_cc;
3931 PerlIO_printf(Perl_debug_log,
3932 "%*s continuation failed...\n",
3933 REPORT_CODE_OFF+PL_regindent*2, "")
3937 if (locinput < PL_regtill)
3938 sayNO; /* Cannot match: too short. */
3941 PL_reginput = locinput; /* put where regtry can find it */
3942 sayYES; /* Success! */
3945 PL_reginput = locinput;
3950 if (UTF) { /* XXXX This is absolutely
3951 broken, we read before
3953 s = HOPMAYBEc(locinput, -scan->flags);
3959 if (locinput < PL_bostr + scan->flags)
3961 PL_reginput = locinput - scan->flags;
3966 PL_reginput = locinput;
3971 if (UTF) { /* XXXX This is absolutely
3972 broken, we read before
3974 s = HOPMAYBEc(locinput, -scan->flags);
3975 if (!s || s < PL_bostr)
3980 if (locinput < PL_bostr + scan->flags)
3982 PL_reginput = locinput - scan->flags;
3987 PL_reginput = locinput;
3990 inner = NEXTOPER(NEXTOPER(scan));
3991 if (regmatch(inner) != n) {
4006 if (OP(scan) == SUSPEND) {
4007 locinput = PL_reginput;
4008 nextchr = UCHARAT(locinput);
4013 next = scan + ARG(scan);
4018 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
4019 (unsigned long)scan, OP(scan));
4020 Perl_croak(aTHX_ "regexp memory corruption");
4026 * We get here only if there's trouble -- normally "case END" is
4027 * the terminating point.
4029 Perl_croak(aTHX_ "corrupted regexp pointers");
4047 - regrepeat - repeatedly match something simple, report how many
4050 * [This routine now assumes that it will only match on things of length 1.
4051 * That was true before, but now we assume scan - reginput is the count,
4052 * rather than incrementing count on every character. [Er, except utf8.]]
4055 S_regrepeat(pTHX_ regnode *p, I32 max)
4058 register char *scan;
4059 register char *opnd;
4061 register char *loceol = PL_regeol;
4062 register I32 hardcount = 0;
4065 if (max != REG_INFTY && max < loceol - scan)
4066 loceol = scan + max;
4067 opnd = (char *) OPERAND(p);
4070 while (scan < loceol && *scan != '\n')
4078 while (scan < loceol && *scan != '\n') {
4079 scan += UTF8SKIP(scan);
4085 while (scan < loceol) {
4086 scan += UTF8SKIP(scan);
4090 case EXACT: /* length of string is 1 */
4091 c = UCHARAT(++opnd);
4092 while (scan < loceol && UCHARAT(scan) == c)
4095 case EXACTF: /* length of string is 1 */
4096 c = UCHARAT(++opnd);
4097 while (scan < loceol &&
4098 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4101 case EXACTFL: /* length of string is 1 */
4102 PL_reg_flags |= RF_tainted;
4103 c = UCHARAT(++opnd);
4104 while (scan < loceol &&
4105 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4110 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
4111 scan += UTF8SKIP(scan);
4116 while (scan < loceol && REGINCLASS(opnd, *scan))
4120 while (scan < loceol && isALNUM(*scan))
4125 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4126 scan += UTF8SKIP(scan);
4131 PL_reg_flags |= RF_tainted;
4132 while (scan < loceol && isALNUM_LC(*scan))
4136 PL_reg_flags |= RF_tainted;
4138 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
4139 scan += UTF8SKIP(scan);
4145 while (scan < loceol && !isALNUM(*scan))
4150 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4151 scan += UTF8SKIP(scan);
4156 PL_reg_flags |= RF_tainted;
4157 while (scan < loceol && !isALNUM_LC(*scan))
4161 PL_reg_flags |= RF_tainted;
4163 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
4164 scan += UTF8SKIP(scan);
4169 while (scan < loceol && isSPACE(*scan))
4174 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4175 scan += UTF8SKIP(scan);
4180 PL_reg_flags |= RF_tainted;
4181 while (scan < loceol && isSPACE_LC(*scan))
4185 PL_reg_flags |= RF_tainted;
4187 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4188 scan += UTF8SKIP(scan);
4193 while (scan < loceol && !isSPACE(*scan))
4198 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4199 scan += UTF8SKIP(scan);
4204 PL_reg_flags |= RF_tainted;
4205 while (scan < loceol && !isSPACE_LC(*scan))
4209 PL_reg_flags |= RF_tainted;
4211 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4212 scan += UTF8SKIP(scan);
4217 while (scan < loceol && isDIGIT(*scan))
4222 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
4223 scan += UTF8SKIP(scan);
4229 while (scan < loceol && !isDIGIT(*scan))
4234 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
4235 scan += UTF8SKIP(scan);
4239 default: /* Called on something of 0 width. */
4240 break; /* So match right here or not at all. */
4246 c = scan - PL_reginput;
4251 SV *prop = sv_newmortal();
4254 PerlIO_printf(Perl_debug_log,
4255 "%*s %s can match %ld times out of %ld...\n",
4256 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
4263 - regrepeat_hard - repeatedly match something, report total lenth and length
4265 * The repeater is supposed to have constant length.
4269 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4272 register char *scan;
4273 register char *start;
4274 register char *loceol = PL_regeol;
4276 I32 count = 0, res = 1;
4281 start = PL_reginput;
4283 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4286 while (start < PL_reginput) {
4288 start += UTF8SKIP(start);
4299 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4301 *lp = l = PL_reginput - start;
4302 if (max != REG_INFTY && l*max < loceol - scan)
4303 loceol = scan + l*max;
4316 - reginclass - determine if a character falls into a character class
4320 S_reginclass(pTHX_ register char *p, register I32 c)
4323 char flags = ANYOF_FLAGS(p);
4327 if (ANYOF_BITMAP_TEST(p, c))
4329 else if (flags & ANYOF_FOLD) {
4331 if (flags & ANYOF_LOCALE) {
4332 PL_reg_flags |= RF_tainted;
4333 cf = PL_fold_locale[c];
4337 if (ANYOF_BITMAP_TEST(p, cf))
4341 if (!match && (flags & ANYOF_CLASS)) {
4342 PL_reg_flags |= RF_tainted;
4344 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4345 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4346 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
4347 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4348 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4349 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4350 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4351 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4352 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4353 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4354 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
4355 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
4356 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4357 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4358 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4359 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4360 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
4361 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4362 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
4363 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4364 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4365 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4366 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
4367 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4368 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4369 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
4370 ) /* How's that for a conditional? */
4376 return (flags & ANYOF_INVERT) ? !match : match;
4380 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
4383 char flags = ARG1(f);
4385 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
4387 if (swash_fetch(sv, p))
4389 else if (flags & ANYOF_FOLD) {
4392 if (flags & ANYOF_LOCALE) {
4393 PL_reg_flags |= RF_tainted;
4394 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
4397 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
4398 if (swash_fetch(sv, tmpbuf))
4402 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
4404 return (flags & ANYOF_INVERT) ? !match : match;
4408 S_reghop(pTHX_ U8 *s, I32 off)
4412 while (off-- && s < (U8*)PL_regeol)
4417 if (s > (U8*)PL_bostr) {
4420 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4422 } /* XXX could check well-formedness here */
4430 S_reghopmaybe(pTHX_ U8* s, I32 off)
4434 while (off-- && s < (U8*)PL_regeol)
4441 if (s > (U8*)PL_bostr) {
4444 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4446 } /* XXX could check well-formedness here */
4463 restore_pos(pTHXo_ void *arg)
4466 if (PL_reg_eval_set) {
4467 if (PL_reg_oldsaved) {
4468 PL_reg_re->subbeg = PL_reg_oldsaved;
4469 PL_reg_re->sublen = PL_reg_oldsavedlen;
4470 RX_MATCH_COPIED_on(PL_reg_re);
4472 PL_reg_magic->mg_len = PL_reg_oldpos;
4473 PL_reg_eval_set = 0;
4474 PL_curpm = PL_reg_oldcurpm;