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;
362 end_shift = 0; /* can happen when strend == strpos */
363 if (flags & REXEC_SCREAM) {
364 SV *c = prog->check_substr;
365 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
366 I32 p = -1; /* Internal iterator of scream. */
367 I32 *pp = data ? data->scream_pos : &p;
369 if (PL_screamfirst[BmRARE(c)] >= 0
370 || ( BmRARE(c) == '\n'
371 && (BmPREVIOUS(c) == SvCUR(c) - 1)
373 s = screaminstr(sv, prog->check_substr,
374 start_shift + (strpos - strbeg), end_shift, pp, 0);
378 *data->scream_olds = s;
381 s = fbm_instr((unsigned char*)s + start_shift,
382 (unsigned char*)strend - end_shift,
383 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
385 /* Update the count-of-usability, remove useless subpatterns,
389 ++BmUSEFUL(prog->check_substr); /* hooray */
390 goto fail; /* not present */
392 else if (s - strpos > prog->check_offset_max &&
393 ((prog->reganch & ROPT_UTF8)
394 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
396 : (t = s - prog->check_offset_max) != 0) ) {
397 if (ml_anch && t[-1] != '\n') {
399 while (t < strend - end_shift - prog->minlen) {
401 if (t < s - prog->check_offset_min) {
415 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
419 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
423 if (!(prog->reganch & ROPT_NAUGHTY)
424 && --BmUSEFUL(prog->check_substr) < 0
425 && prog->check_substr == prog->float_substr) { /* boo */
426 /* If flags & SOMETHING - do not do it many times on the same match */
427 SvREFCNT_dec(prog->check_substr);
428 prog->check_substr = Nullsv; /* disable */
429 prog->float_substr = Nullsv; /* clear */
431 prog->reganch &= ~RE_USE_INTUIT;
437 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sFound%s at offset %ld\n",
438 PL_colors[4],PL_colors[5], (long)(s - strpos)) );
441 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sNot found...%s\n",
442 PL_colors[4],PL_colors[5]));
447 - regexec_flags - match a regexp against a string
450 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
451 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
452 /* strend: pointer to null at end of string */
453 /* strbeg: real beginning of string */
454 /* minend: end of match must be >=minend after stringarg. */
455 /* data: May be used for some additional optimizations. */
456 /* nosave: For optimizations. */
461 register char *startpos = stringarg;
463 I32 minlen; /* must match at least this many chars */
464 I32 dontbother = 0; /* how many characters not to try at end */
466 I32 start_shift = 0; /* Offset of the start to find
467 constant substr. */ /* CC */
468 I32 end_shift = 0; /* Same for the end. */ /* CC */
469 I32 scream_pos = -1; /* Internal iterator of scream. */
471 SV* oreplsv = GvSV(PL_replgv);
479 PL_regnarrate = PL_debug & 512;
483 if (prog == NULL || startpos == NULL) {
484 Perl_croak(aTHX_ "NULL regexp parameter");
488 minlen = prog->minlen;
489 if (strend - startpos < minlen) goto phooey;
491 if (startpos == strbeg) /* is ^ valid at stringarg? */
494 PL_regprev = (U32)stringarg[-1];
495 if (!PL_multiline && PL_regprev == '\n')
496 PL_regprev = '\0'; /* force ^ to NOT match */
499 /* Check validity of program. */
500 if (UCHARAT(prog->program) != REG_MAGIC) {
501 Perl_croak(aTHX_ "corrupted regexp program");
507 if (prog->reganch & ROPT_UTF8)
508 PL_reg_flags |= RF_utf8;
510 /* Mark beginning of line for ^ and lookbehind. */
511 PL_regbol = startpos;
515 /* Mark end of line for $ (and such) */
518 /* see how far we have to get to not match where we matched before */
519 PL_regtill = startpos+minend;
521 /* We start without call_cc context. */
524 /* If there is a "must appear" string, look for it. */
527 if (prog->reganch & ROPT_GPOS_SEEN) {
530 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
531 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
532 PL_reg_ganch = strbeg + mg->mg_len;
534 PL_reg_ganch = startpos;
535 if (prog->reganch & ROPT_ANCH_GPOS) {
536 if (s > PL_reg_ganch)
542 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
543 re_scream_pos_data d;
545 d.scream_olds = &scream_olds;
546 d.scream_pos = &scream_pos;
547 s = re_intuit_start(prog, sv, s, strend, flags, &d);
549 goto phooey; /* not present */
552 DEBUG_r( if (!PL_colorset) reginitcolors() );
553 DEBUG_r(PerlIO_printf(Perl_debug_log,
554 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
555 PL_colors[4],PL_colors[5],PL_colors[0],
558 (strlen(prog->precomp) > 60 ? "..." : ""),
560 (strend - startpos > 60 ? 60 : strend - startpos),
561 startpos, PL_colors[1],
562 (strend - startpos > 60 ? "..." : ""))
565 /* Simplest case: anchored match need be tried only once. */
566 /* [unless only anchor is BOL and multiline is set] */
567 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
568 if (s == startpos && regtry(prog, startpos))
570 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
571 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
576 dontbother = minlen - 1;
577 end = HOPc(strend, -dontbother) - 1;
578 /* for multiline we only have to try after newlines */
579 if (prog->check_substr) {
585 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
593 if (*s++ == '\n') { /* don't need PL_utf8skip here */
601 } else if (prog->reganch & ROPT_ANCH_GPOS) {
602 if (regtry(prog, PL_reg_ganch))
607 /* Messy cases: unanchored match. */
608 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
609 /* we have /x+whatever/ */
610 /* it must be a one character string (XXXX Except UTF?) */
611 char ch = SvPVX(prog->anchored_substr)[0];
615 if (regtry(prog, s)) goto got_it;
617 while (s < strend && *s == ch)
626 if (regtry(prog, s)) goto got_it;
628 while (s < strend && *s == ch)
636 else if (prog->anchored_substr != Nullsv
637 || (prog->float_substr != Nullsv
638 && prog->float_max_offset < strend - s)) {
639 SV *must = prog->anchored_substr
640 ? prog->anchored_substr : prog->float_substr;
642 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
644 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
645 I32 delta = back_max - back_min;
646 char *last = HOPc(strend, /* Cannot start after this */
647 -(I32)(CHR_SVLEN(must)
648 - (SvTAIL(must) != 0) + back_min));
649 char *last1; /* Last position checked before */
654 last1 = s - 1; /* bogus */
656 /* XXXX check_substr already used to find `s', can optimize if
657 check_substr==must. */
659 dontbother = end_shift;
660 strend = HOPc(strend, -dontbother);
661 while ( (s <= last) &&
662 ((flags & REXEC_SCREAM)
663 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
664 end_shift, &scream_pos, 0))
665 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
666 (unsigned char*)strend, must,
667 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
668 if (HOPc(s, -back_max) > last1) {
669 last1 = HOPc(s, -back_min);
670 s = HOPc(s, -back_max);
673 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
675 last1 = HOPc(s, -back_min);
695 else if (c = prog->regstclass) {
696 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
700 dontbother = minlen - 1;
701 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
703 /* We know what class it must start with. */
706 cc = (char *) OPERAND(c);
708 if (REGINCLASSUTF8(c, (U8*)s)) {
709 if (tmp && regtry(prog, s))
720 cc = (char *) OPERAND(c);
722 if (REGINCLASS(cc, *s)) {
723 if (tmp && regtry(prog, s))
734 PL_reg_flags |= RF_tainted;
741 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
742 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
744 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
751 if ((minlen || tmp) && regtry(prog,s))
755 PL_reg_flags |= RF_tainted;
760 strend = reghop_c(strend, -1);
762 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
763 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
765 if (tmp == !(OP(c) == BOUND ?
766 swash_fetch(PL_utf8_alnum, (U8*)s) :
767 isALNUM_LC_utf8((U8*)s)))
775 if ((minlen || tmp) && regtry(prog,s))
779 PL_reg_flags |= RF_tainted;
786 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
787 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
789 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
791 else if (regtry(prog, s))
795 if ((minlen || !tmp) && regtry(prog,s))
799 PL_reg_flags |= RF_tainted;
804 strend = reghop_c(strend, -1);
806 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
807 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
809 if (tmp == !(OP(c) == NBOUND ?
810 swash_fetch(PL_utf8_alnum, (U8*)s) :
811 isALNUM_LC_utf8((U8*)s)))
813 else if (regtry(prog, s))
817 if ((minlen || !tmp) && regtry(prog,s))
823 if (tmp && regtry(prog, s))
835 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
836 if (tmp && regtry(prog, s))
847 PL_reg_flags |= RF_tainted;
849 if (isALNUM_LC(*s)) {
850 if (tmp && regtry(prog, s))
861 PL_reg_flags |= RF_tainted;
863 if (isALNUM_LC_utf8((U8*)s)) {
864 if (tmp && regtry(prog, s))
877 if (tmp && regtry(prog, s))
889 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
890 if (tmp && regtry(prog, s))
901 PL_reg_flags |= RF_tainted;
903 if (!isALNUM_LC(*s)) {
904 if (tmp && regtry(prog, s))
915 PL_reg_flags |= RF_tainted;
917 if (!isALNUM_LC_utf8((U8*)s)) {
918 if (tmp && regtry(prog, s))
931 if (tmp && regtry(prog, s))
943 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
944 if (tmp && regtry(prog, s))
955 PL_reg_flags |= RF_tainted;
957 if (isSPACE_LC(*s)) {
958 if (tmp && regtry(prog, s))
969 PL_reg_flags |= RF_tainted;
971 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
972 if (tmp && regtry(prog, s))
985 if (tmp && regtry(prog, s))
997 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
998 if (tmp && regtry(prog, s))
1009 PL_reg_flags |= RF_tainted;
1010 while (s < strend) {
1011 if (!isSPACE_LC(*s)) {
1012 if (tmp && regtry(prog, s))
1023 PL_reg_flags |= RF_tainted;
1024 while (s < strend) {
1025 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1026 if (tmp && regtry(prog, s))
1037 while (s < strend) {
1039 if (tmp && regtry(prog, s))
1050 while (s < strend) {
1051 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1052 if (tmp && regtry(prog, s))
1063 PL_reg_flags |= RF_tainted;
1064 while (s < strend) {
1065 if (isDIGIT_LC(*s)) {
1066 if (tmp && regtry(prog, s))
1077 PL_reg_flags |= RF_tainted;
1078 while (s < strend) {
1079 if (isDIGIT_LC_utf8((U8*)s)) {
1080 if (tmp && regtry(prog, s))
1091 while (s < strend) {
1093 if (tmp && regtry(prog, s))
1104 while (s < strend) {
1105 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1106 if (tmp && regtry(prog, s))
1117 PL_reg_flags |= RF_tainted;
1118 while (s < strend) {
1119 if (!isDIGIT_LC(*s)) {
1120 if (tmp && regtry(prog, s))
1131 PL_reg_flags |= RF_tainted;
1132 while (s < strend) {
1133 if (!isDIGIT_LC_utf8((U8*)s)) {
1134 if (tmp && regtry(prog, s))
1145 while (s < strend) {
1147 if (tmp && regtry(prog, s))
1158 while (s < strend) {
1159 if (swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1160 if (tmp && regtry(prog, s))
1171 PL_reg_flags |= RF_tainted;
1172 while (s < strend) {
1173 if (isALNUMC_LC(*s)) {
1174 if (tmp && regtry(prog, s))
1185 PL_reg_flags |= RF_tainted;
1186 while (s < strend) {
1187 if (isALNUMC_LC_utf8((U8*)s)) {
1188 if (tmp && regtry(prog, s))
1199 while (s < strend) {
1200 if (!isALNUMC(*s)) {
1201 if (tmp && regtry(prog, s))
1212 while (s < strend) {
1213 if (!swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1214 if (tmp && regtry(prog, s))
1225 PL_reg_flags |= RF_tainted;
1226 while (s < strend) {
1227 if (!isALNUMC_LC(*s)) {
1228 if (tmp && regtry(prog, s))
1239 PL_reg_flags |= RF_tainted;
1240 while (s < strend) {
1241 if (!isALNUMC_LC_utf8((U8*)s)) {
1242 if (tmp && regtry(prog, s))
1253 while (s < strend) {
1254 if (isASCII(*(U8*)s)) {
1255 if (tmp && regtry(prog, s))
1266 while (s < strend) {
1267 if (!isASCII(*(U8*)s)) {
1268 if (tmp && regtry(prog, s))
1279 while (s < strend) {
1281 if (tmp && regtry(prog, s))
1292 while (s < strend) {
1293 if (swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1294 if (tmp && regtry(prog, s))
1305 PL_reg_flags |= RF_tainted;
1306 while (s < strend) {
1307 if (isCNTRL_LC(*s)) {
1308 if (tmp && regtry(prog, s))
1319 PL_reg_flags |= RF_tainted;
1320 while (s < strend) {
1321 if (*s == ' ' || isCNTRL_LC_utf8((U8*)s)) {
1322 if (tmp && regtry(prog, s))
1333 while (s < strend) {
1335 if (tmp && regtry(prog, s))
1346 while (s < strend) {
1347 if (!swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1348 if (tmp && regtry(prog, s))
1359 PL_reg_flags |= RF_tainted;
1360 while (s < strend) {
1361 if (!isCNTRL_LC(*s)) {
1362 if (tmp && regtry(prog, s))
1373 PL_reg_flags |= RF_tainted;
1374 while (s < strend) {
1375 if (!isCNTRL_LC_utf8((U8*)s)) {
1376 if (tmp && regtry(prog, s))
1387 while (s < strend) {
1389 if (tmp && regtry(prog, s))
1400 while (s < strend) {
1401 if (swash_fetch(PL_utf8_graph,(U8*)s)) {
1402 if (tmp && regtry(prog, s))
1413 PL_reg_flags |= RF_tainted;
1414 while (s < strend) {
1415 if (isGRAPH_LC(*s)) {
1416 if (tmp && regtry(prog, s))
1427 PL_reg_flags |= RF_tainted;
1428 while (s < strend) {
1429 if (*s == ' ' || isGRAPH_LC_utf8((U8*)s)) {
1430 if (tmp && regtry(prog, s))
1441 while (s < strend) {
1443 if (tmp && regtry(prog, s))
1454 while (s < strend) {
1455 if (!swash_fetch(PL_utf8_graph,(U8*)s)) {
1456 if (tmp && regtry(prog, s))
1467 PL_reg_flags |= RF_tainted;
1468 while (s < strend) {
1469 if (!isGRAPH_LC(*s)) {
1470 if (tmp && regtry(prog, s))
1481 PL_reg_flags |= RF_tainted;
1482 while (s < strend) {
1483 if (!isGRAPH_LC_utf8((U8*)s)) {
1484 if (tmp && regtry(prog, s))
1495 while (s < strend) {
1497 if (tmp && regtry(prog, s))
1508 while (s < strend) {
1509 if (swash_fetch(PL_utf8_lower,(U8*)s)) {
1510 if (tmp && regtry(prog, s))
1521 PL_reg_flags |= RF_tainted;
1522 while (s < strend) {
1523 if (isLOWER_LC(*s)) {
1524 if (tmp && regtry(prog, s))
1535 PL_reg_flags |= RF_tainted;
1536 while (s < strend) {
1537 if (*s == ' ' || isLOWER_LC_utf8((U8*)s)) {
1538 if (tmp && regtry(prog, s))
1549 while (s < strend) {
1551 if (tmp && regtry(prog, s))
1562 while (s < strend) {
1563 if (!swash_fetch(PL_utf8_lower,(U8*)s)) {
1564 if (tmp && regtry(prog, s))
1575 PL_reg_flags |= RF_tainted;
1576 while (s < strend) {
1577 if (!isLOWER_LC(*s)) {
1578 if (tmp && regtry(prog, s))
1589 PL_reg_flags |= RF_tainted;
1590 while (s < strend) {
1591 if (!isLOWER_LC_utf8((U8*)s)) {
1592 if (tmp && regtry(prog, s))
1603 while (s < strend) {
1605 if (tmp && regtry(prog, s))
1616 while (s < strend) {
1617 if (swash_fetch(PL_utf8_print,(U8*)s)) {
1618 if (tmp && regtry(prog, s))
1629 PL_reg_flags |= RF_tainted;
1630 while (s < strend) {
1631 if (isPRINT_LC(*s)) {
1632 if (tmp && regtry(prog, s))
1643 PL_reg_flags |= RF_tainted;
1644 while (s < strend) {
1645 if (*s == ' ' || isPRINT_LC_utf8((U8*)s)) {
1646 if (tmp && regtry(prog, s))
1657 while (s < strend) {
1659 if (tmp && regtry(prog, s))
1670 while (s < strend) {
1671 if (!swash_fetch(PL_utf8_print,(U8*)s)) {
1672 if (tmp && regtry(prog, s))
1683 PL_reg_flags |= RF_tainted;
1684 while (s < strend) {
1685 if (!isPRINT_LC(*s)) {
1686 if (tmp && regtry(prog, s))
1697 PL_reg_flags |= RF_tainted;
1698 while (s < strend) {
1699 if (!isPRINT_LC_utf8((U8*)s)) {
1700 if (tmp && regtry(prog, s))
1711 while (s < strend) {
1713 if (tmp && regtry(prog, s))
1724 while (s < strend) {
1725 if (swash_fetch(PL_utf8_punct,(U8*)s)) {
1726 if (tmp && regtry(prog, s))
1737 PL_reg_flags |= RF_tainted;
1738 while (s < strend) {
1739 if (isPUNCT_LC(*s)) {
1740 if (tmp && regtry(prog, s))
1751 PL_reg_flags |= RF_tainted;
1752 while (s < strend) {
1753 if (*s == ' ' || isPUNCT_LC_utf8((U8*)s)) {
1754 if (tmp && regtry(prog, s))
1765 while (s < strend) {
1767 if (tmp && regtry(prog, s))
1778 while (s < strend) {
1779 if (!swash_fetch(PL_utf8_punct,(U8*)s)) {
1780 if (tmp && regtry(prog, s))
1791 PL_reg_flags |= RF_tainted;
1792 while (s < strend) {
1793 if (!isPUNCT_LC(*s)) {
1794 if (tmp && regtry(prog, s))
1805 PL_reg_flags |= RF_tainted;
1806 while (s < strend) {
1807 if (!isPUNCT_LC_utf8((U8*)s)) {
1808 if (tmp && regtry(prog, s))
1819 while (s < strend) {
1821 if (tmp && regtry(prog, s))
1832 while (s < strend) {
1833 if (swash_fetch(PL_utf8_upper,(U8*)s)) {
1834 if (tmp && regtry(prog, s))
1845 PL_reg_flags |= RF_tainted;
1846 while (s < strend) {
1847 if (isUPPER_LC(*s)) {
1848 if (tmp && regtry(prog, s))
1859 PL_reg_flags |= RF_tainted;
1860 while (s < strend) {
1861 if (*s == ' ' || isUPPER_LC_utf8((U8*)s)) {
1862 if (tmp && regtry(prog, s))
1873 while (s < strend) {
1875 if (tmp && regtry(prog, s))
1886 while (s < strend) {
1887 if (!swash_fetch(PL_utf8_upper,(U8*)s)) {
1888 if (tmp && regtry(prog, s))
1899 PL_reg_flags |= RF_tainted;
1900 while (s < strend) {
1901 if (!isUPPER_LC(*s)) {
1902 if (tmp && regtry(prog, s))
1913 PL_reg_flags |= RF_tainted;
1914 while (s < strend) {
1915 if (!isUPPER_LC_utf8((U8*)s)) {
1916 if (tmp && regtry(prog, s))
1927 while (s < strend) {
1929 if (tmp && regtry(prog, s))
1940 while (s < strend) {
1941 if (!isXDIGIT(*s)) {
1942 if (tmp && regtry(prog, s))
1956 if (prog->float_substr != Nullsv) { /* Trim the end. */
1958 I32 oldpos = scream_pos;
1960 if (flags & REXEC_SCREAM) {
1961 last = screaminstr(sv, prog->float_substr, s - strbeg,
1962 end_shift, &scream_pos, 1); /* last one */
1964 last = scream_olds; /* Only one occurence. */
1968 char *little = SvPV(prog->float_substr, len);
1970 if (SvTAIL(prog->float_substr)) {
1971 if (memEQ(strend - len + 1, little, len - 1))
1972 last = strend - len + 1;
1973 else if (!PL_multiline)
1974 last = memEQ(strend - len, little, len)
1975 ? strend - len : Nullch;
1981 last = rninstr(s, strend, little, little + len);
1983 last = strend; /* matching `$' */
1986 if (last == NULL) goto phooey; /* Should not happen! */
1987 dontbother = strend - last + prog->float_min_offset;
1989 if (minlen && (dontbother < minlen))
1990 dontbother = minlen - 1;
1991 strend -= dontbother; /* this one's always in bytes! */
1992 /* We don't know much -- general case. */
1995 if (regtry(prog, s))
2004 if (regtry(prog, s))
2006 } while (s++ < strend);
2014 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2016 if (PL_reg_eval_set) {
2017 /* Preserve the current value of $^R */
2018 if (oreplsv != GvSV(PL_replgv))
2019 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2020 restored, the value remains
2022 restore_pos(aTHXo_ 0);
2025 /* make sure $`, $&, $', and $digit will work later */
2026 if ( !(flags & REXEC_NOT_FIRST) ) {
2027 if (RX_MATCH_COPIED(prog)) {
2028 Safefree(prog->subbeg);
2029 RX_MATCH_COPIED_off(prog);
2031 if (flags & REXEC_COPY_STR) {
2032 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2034 s = savepvn(strbeg, i);
2037 RX_MATCH_COPIED_on(prog);
2040 prog->subbeg = strbeg;
2041 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2048 if (PL_reg_eval_set)
2049 restore_pos(aTHXo_ 0);
2054 - regtry - try match at specific point
2056 STATIC I32 /* 0 failure, 1 success */
2057 S_regtry(pTHX_ regexp *prog, char *startpos)
2065 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2068 PL_reg_eval_set = RS_init;
2070 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
2071 PL_stack_sp - PL_stack_base);
2073 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
2074 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2075 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2077 /* Apparently this is not needed, judging by wantarray. */
2078 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
2079 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2082 /* Make $_ available to executed code. */
2083 if (PL_reg_sv != DEFSV) {
2084 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
2089 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2090 && (mg = mg_find(PL_reg_sv, 'g')))) {
2091 /* prepare for quick setting of pos */
2092 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
2093 mg = mg_find(PL_reg_sv, 'g');
2097 PL_reg_oldpos = mg->mg_len;
2098 SAVEDESTRUCTOR(restore_pos, 0);
2101 New(22,PL_reg_curpm, 1, PMOP);
2102 PL_reg_curpm->op_pmregexp = prog;
2103 PL_reg_oldcurpm = PL_curpm;
2104 PL_curpm = PL_reg_curpm;
2105 if (RX_MATCH_COPIED(prog)) {
2106 /* Here is a serious problem: we cannot rewrite subbeg,
2107 since it may be needed if this match fails. Thus
2108 $` inside (?{}) could fail... */
2109 PL_reg_oldsaved = prog->subbeg;
2110 PL_reg_oldsavedlen = prog->sublen;
2111 RX_MATCH_COPIED_off(prog);
2114 PL_reg_oldsaved = Nullch;
2115 prog->subbeg = PL_bostr;
2116 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2118 prog->startp[0] = startpos - PL_bostr;
2119 PL_reginput = startpos;
2120 PL_regstartp = prog->startp;
2121 PL_regendp = prog->endp;
2122 PL_reglastparen = &prog->lastparen;
2123 prog->lastparen = 0;
2125 DEBUG_r(PL_reg_starttry = startpos);
2126 if (PL_reg_start_tmpl <= prog->nparens) {
2127 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2128 if(PL_reg_start_tmp)
2129 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2131 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2134 /* XXXX What this code is doing here?!!! There should be no need
2135 to do this again and again, PL_reglastparen should take care of
2139 if (prog->nparens) {
2140 for (i = prog->nparens; i >= 1; i--) {
2146 if (regmatch(prog->program + 1)) {
2147 prog->endp[0] = PL_reginput - PL_bostr;
2155 - regmatch - main matching routine
2157 * Conceptually the strategy is simple: check to see whether the current
2158 * node matches, call self recursively to see whether the rest matches,
2159 * and then act accordingly. In practice we make some effort to avoid
2160 * recursion, in particular by going through "ordinary" nodes (that don't
2161 * need to know whether the rest of the match failed) by a loop instead of
2164 /* [lwall] I've hoisted the register declarations to the outer block in order to
2165 * maybe save a little bit of pushing and popping on the stack. It also takes
2166 * advantage of machines that use a register save mask on subroutine entry.
2168 STATIC I32 /* 0 failure, 1 success */
2169 S_regmatch(pTHX_ regnode *prog)
2172 register regnode *scan; /* Current node. */
2173 regnode *next; /* Next node. */
2174 regnode *inner; /* Next node in internal branch. */
2175 register I32 nextchr; /* renamed nextchr - nextchar colides with
2176 function of same name */
2177 register I32 n; /* no or next */
2178 register I32 ln; /* len or last */
2179 register char *s; /* operand or save */
2180 register char *locinput = PL_reginput;
2181 register I32 c1, c2, paren; /* case fold search, parenth */
2182 int minmod = 0, sw = 0, logical = 0;
2187 /* Note that nextchr is a byte even in UTF */
2188 nextchr = UCHARAT(locinput);
2190 while (scan != NULL) {
2191 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
2193 # define sayYES goto yes
2194 # define sayNO goto no
2195 # define saySAME(x) if (x) goto yes; else goto no
2196 # define REPORT_CODE_OFF 24
2198 # define sayYES return 1
2199 # define sayNO return 0
2200 # define saySAME(x) return x
2203 SV *prop = sv_newmortal();
2204 int docolor = *PL_colors[0];
2205 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2206 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
2207 /* The part of the string before starttry has one color
2208 (pref0_len chars), between starttry and current
2209 position another one (pref_len - pref0_len chars),
2210 after the current position the third one.
2211 We assume that pref0_len <= pref_len, otherwise we
2212 decrease pref0_len. */
2213 int pref_len = (locinput - PL_bostr > (5 + taill) - l
2214 ? (5 + taill) - l : locinput - PL_bostr);
2215 int pref0_len = pref_len - (locinput - PL_reg_starttry);
2217 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2218 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2219 ? (5 + taill) - pref_len : PL_regeol - locinput);
2222 if (pref0_len > pref_len)
2223 pref0_len = pref_len;
2224 regprop(prop, scan);
2225 PerlIO_printf(Perl_debug_log,
2226 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
2227 locinput - PL_bostr,
2228 PL_colors[4], pref0_len,
2229 locinput - pref_len, PL_colors[5],
2230 PL_colors[2], pref_len - pref0_len,
2231 locinput - pref_len + pref0_len, PL_colors[3],
2232 (docolor ? "" : "> <"),
2233 PL_colors[0], l, locinput, PL_colors[1],
2234 15 - l - pref_len + 1,
2236 scan - PL_regprogram, PL_regindent*2, "",
2240 next = scan + NEXT_OFF(scan);
2246 if (locinput == PL_bostr
2247 ? PL_regprev == '\n'
2249 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2251 /* regtill = regbol; */
2256 if (locinput == PL_bostr
2257 ? PL_regprev == '\n'
2258 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2264 if (locinput == PL_regbol && PL_regprev == '\n')
2268 if (locinput == PL_reg_ganch)
2278 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2283 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2285 if (PL_regeol - locinput > 1)
2289 if (PL_regeol != locinput)
2293 if (nextchr & 0x80) {
2294 locinput += PL_utf8skip[nextchr];
2295 if (locinput > PL_regeol)
2297 nextchr = UCHARAT(locinput);
2300 if (!nextchr && locinput >= PL_regeol)
2302 nextchr = UCHARAT(++locinput);
2305 if (!nextchr && locinput >= PL_regeol)
2307 nextchr = UCHARAT(++locinput);
2310 if (nextchr & 0x80) {
2311 locinput += PL_utf8skip[nextchr];
2312 if (locinput > PL_regeol)
2314 nextchr = UCHARAT(locinput);
2317 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2319 nextchr = UCHARAT(++locinput);
2322 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2324 nextchr = UCHARAT(++locinput);
2327 s = (char *) OPERAND(scan);
2329 /* Inline the first character, for speed. */
2330 if (UCHARAT(s) != nextchr)
2332 if (PL_regeol - locinput < ln)
2334 if (ln > 1 && memNE(s, locinput, ln))
2337 nextchr = UCHARAT(locinput);
2340 PL_reg_flags |= RF_tainted;
2343 s = (char *) OPERAND(scan);
2349 c1 = OP(scan) == EXACTF;
2353 if (utf8_to_uv((U8*)s, 0) != (c1 ?
2354 toLOWER_utf8((U8*)l) :
2355 toLOWER_LC_utf8((U8*)l)))
2363 nextchr = UCHARAT(locinput);
2367 /* Inline the first character, for speed. */
2368 if (UCHARAT(s) != nextchr &&
2369 UCHARAT(s) != ((OP(scan) == EXACTF)
2370 ? PL_fold : PL_fold_locale)[nextchr])
2372 if (PL_regeol - locinput < ln)
2374 if (ln > 1 && (OP(scan) == EXACTF
2375 ? ibcmp(s, locinput, ln)
2376 : ibcmp_locale(s, locinput, ln)))
2379 nextchr = UCHARAT(locinput);
2382 s = (char *) OPERAND(scan);
2383 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2385 if (locinput >= PL_regeol)
2387 locinput += PL_utf8skip[nextchr];
2388 nextchr = UCHARAT(locinput);
2391 s = (char *) OPERAND(scan);
2393 nextchr = UCHARAT(locinput);
2394 if (!REGINCLASS(s, nextchr))
2396 if (!nextchr && locinput >= PL_regeol)
2398 nextchr = UCHARAT(++locinput);
2401 PL_reg_flags |= RF_tainted;
2406 if (!(OP(scan) == ALNUM
2407 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2409 nextchr = UCHARAT(++locinput);
2412 PL_reg_flags |= RF_tainted;
2417 if (nextchr & 0x80) {
2418 if (!(OP(scan) == ALNUMUTF8
2419 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2420 : isALNUM_LC_utf8((U8*)locinput)))
2424 locinput += PL_utf8skip[nextchr];
2425 nextchr = UCHARAT(locinput);
2428 if (!(OP(scan) == ALNUMUTF8
2429 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2431 nextchr = UCHARAT(++locinput);
2434 PL_reg_flags |= RF_tainted;
2437 if (!nextchr && locinput >= PL_regeol)
2439 if (OP(scan) == NALNUM
2440 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2442 nextchr = UCHARAT(++locinput);
2445 PL_reg_flags |= RF_tainted;
2448 if (!nextchr && locinput >= PL_regeol)
2450 if (nextchr & 0x80) {
2451 if (OP(scan) == NALNUMUTF8
2452 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2453 : isALNUM_LC_utf8((U8*)locinput))
2457 locinput += PL_utf8skip[nextchr];
2458 nextchr = UCHARAT(locinput);
2461 if (OP(scan) == NALNUMUTF8
2462 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2464 nextchr = UCHARAT(++locinput);
2468 PL_reg_flags |= RF_tainted;
2472 /* was last char in word? */
2473 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2474 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2476 n = isALNUM(nextchr);
2479 ln = isALNUM_LC(ln);
2480 n = isALNUM_LC(nextchr);
2482 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2487 PL_reg_flags |= RF_tainted;
2491 /* was last char in word? */
2492 ln = (locinput != PL_regbol)
2493 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2494 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2495 ln = isALNUM_uni(ln);
2496 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2499 ln = isALNUM_LC_uni(ln);
2500 n = isALNUM_LC_utf8((U8*)locinput);
2502 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2506 PL_reg_flags |= RF_tainted;
2509 if (!nextchr && locinput >= PL_regeol)
2511 if (!(OP(scan) == SPACE
2512 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2514 nextchr = UCHARAT(++locinput);
2517 PL_reg_flags |= RF_tainted;
2520 if (!nextchr && locinput >= PL_regeol)
2522 if (nextchr & 0x80) {
2523 if (!(OP(scan) == SPACEUTF8
2524 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2525 : isSPACE_LC_utf8((U8*)locinput)))
2529 locinput += PL_utf8skip[nextchr];
2530 nextchr = UCHARAT(locinput);
2533 if (!(OP(scan) == SPACEUTF8
2534 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2536 nextchr = UCHARAT(++locinput);
2539 PL_reg_flags |= RF_tainted;
2544 if (OP(scan) == SPACE
2545 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2547 nextchr = UCHARAT(++locinput);
2550 PL_reg_flags |= RF_tainted;
2555 if (nextchr & 0x80) {
2556 if (OP(scan) == NSPACEUTF8
2557 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2558 : isSPACE_LC_utf8((U8*)locinput))
2562 locinput += PL_utf8skip[nextchr];
2563 nextchr = UCHARAT(locinput);
2566 if (OP(scan) == NSPACEUTF8
2567 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2569 nextchr = UCHARAT(++locinput);
2572 PL_reg_flags |= RF_tainted;
2575 if (!nextchr && locinput >= PL_regeol)
2577 if (!(OP(scan) == DIGIT
2578 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2580 nextchr = UCHARAT(++locinput);
2583 PL_reg_flags |= RF_tainted;
2588 if (nextchr & 0x80) {
2589 if (OP(scan) == NDIGITUTF8
2590 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2591 : isDIGIT_LC_utf8((U8*)locinput))
2595 locinput += PL_utf8skip[nextchr];
2596 nextchr = UCHARAT(locinput);
2599 if (!isDIGIT(nextchr))
2601 nextchr = UCHARAT(++locinput);
2604 PL_reg_flags |= RF_tainted;
2609 if (OP(scan) == DIGIT
2610 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2612 nextchr = UCHARAT(++locinput);
2615 PL_reg_flags |= RF_tainted;
2618 if (!nextchr && locinput >= PL_regeol)
2620 if (nextchr & 0x80) {
2621 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2623 locinput += PL_utf8skip[nextchr];
2624 nextchr = UCHARAT(locinput);
2627 if (isDIGIT(nextchr))
2629 nextchr = UCHARAT(++locinput);
2632 PL_reg_flags |= RF_tainted;
2637 if (!(OP(scan) == ALNUMC
2638 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2640 nextchr = UCHARAT(++locinput);
2643 PL_reg_flags |= RF_tainted;
2648 if (nextchr & 0x80) {
2649 if (!(OP(scan) == ALNUMCUTF8
2650 ? swash_fetch(PL_utf8_alnumc, (U8*)locinput)
2651 : isALNUMC_LC_utf8((U8*)locinput)))
2655 locinput += PL_utf8skip[nextchr];
2656 nextchr = UCHARAT(locinput);
2659 if (!(OP(scan) == ALNUMCUTF8
2660 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2662 nextchr = UCHARAT(++locinput);
2665 PL_reg_flags |= RF_tainted;
2670 if (OP(scan) == ALNUMC
2671 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr))
2673 nextchr = UCHARAT(++locinput);
2676 PL_reg_flags |= RF_tainted;
2679 if (!nextchr && locinput >= PL_regeol)
2681 if (nextchr & 0x80) {
2682 if (swash_fetch(PL_utf8_alnumc,(U8*)locinput))
2684 locinput += PL_utf8skip[nextchr];
2685 nextchr = UCHARAT(locinput);
2688 if (isALNUMC(nextchr))
2690 nextchr = UCHARAT(++locinput);
2693 PL_reg_flags |= RF_tainted;
2698 if (!(OP(scan) == ALPHA
2699 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2701 nextchr = UCHARAT(++locinput);
2704 PL_reg_flags |= RF_tainted;
2709 if (nextchr & 0x80) {
2710 if (!(OP(scan) == ALPHAUTF8
2711 ? swash_fetch(PL_utf8_alpha, (U8*)locinput)
2712 : isALPHA_LC_utf8((U8*)locinput)))
2716 locinput += PL_utf8skip[nextchr];
2717 nextchr = UCHARAT(locinput);
2720 if (!(OP(scan) == ALPHAUTF8
2721 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2723 nextchr = UCHARAT(++locinput);
2726 PL_reg_flags |= RF_tainted;
2731 if (OP(scan) == ALPHA
2732 ? isALPHA(nextchr) : isALPHA_LC(nextchr))
2734 nextchr = UCHARAT(++locinput);
2737 PL_reg_flags |= RF_tainted;
2740 if (!nextchr && locinput >= PL_regeol)
2742 if (nextchr & 0x80) {
2743 if (swash_fetch(PL_utf8_alpha,(U8*)locinput))
2745 locinput += PL_utf8skip[nextchr];
2746 nextchr = UCHARAT(locinput);
2749 if (isALPHA(nextchr))
2751 nextchr = UCHARAT(++locinput);
2754 if (!nextchr && locinput >= PL_regeol)
2756 if (!isASCII(nextchr))
2758 nextchr = UCHARAT(++locinput);
2761 if (!nextchr && locinput >= PL_regeol)
2763 if (isASCII(nextchr))
2765 nextchr = UCHARAT(++locinput);
2768 PL_reg_flags |= RF_tainted;
2773 if (!(OP(scan) == CNTRL
2774 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2776 nextchr = UCHARAT(++locinput);
2779 PL_reg_flags |= RF_tainted;
2784 if (nextchr & 0x80) {
2785 if (!(OP(scan) == CNTRLUTF8
2786 ? swash_fetch(PL_utf8_cntrl, (U8*)locinput)
2787 : isCNTRL_LC_utf8((U8*)locinput)))
2791 locinput += PL_utf8skip[nextchr];
2792 nextchr = UCHARAT(locinput);
2795 if (!(OP(scan) == CNTRLUTF8
2796 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2798 nextchr = UCHARAT(++locinput);
2801 PL_reg_flags |= RF_tainted;
2806 if (OP(scan) == CNTRL
2807 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr))
2809 nextchr = UCHARAT(++locinput);
2812 PL_reg_flags |= RF_tainted;
2815 if (!nextchr && locinput >= PL_regeol)
2817 if (nextchr & 0x80) {
2818 if (swash_fetch(PL_utf8_cntrl,(U8*)locinput))
2820 locinput += PL_utf8skip[nextchr];
2821 nextchr = UCHARAT(locinput);
2824 if (isCNTRL(nextchr))
2826 nextchr = UCHARAT(++locinput);
2829 PL_reg_flags |= RF_tainted;
2834 if (!(OP(scan) == GRAPH
2835 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2837 nextchr = UCHARAT(++locinput);
2840 PL_reg_flags |= RF_tainted;
2845 if (nextchr & 0x80) {
2846 if (!(OP(scan) == GRAPHUTF8
2847 ? swash_fetch(PL_utf8_graph, (U8*)locinput)
2848 : isGRAPH_LC_utf8((U8*)locinput)))
2852 locinput += PL_utf8skip[nextchr];
2853 nextchr = UCHARAT(locinput);
2856 if (!(OP(scan) == GRAPHUTF8
2857 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2859 nextchr = UCHARAT(++locinput);
2862 PL_reg_flags |= RF_tainted;
2867 if (OP(scan) == GRAPH
2868 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr))
2870 nextchr = UCHARAT(++locinput);
2873 PL_reg_flags |= RF_tainted;
2876 if (!nextchr && locinput >= PL_regeol)
2878 if (nextchr & 0x80) {
2879 if (swash_fetch(PL_utf8_graph,(U8*)locinput))
2881 locinput += PL_utf8skip[nextchr];
2882 nextchr = UCHARAT(locinput);
2885 if (isGRAPH(nextchr))
2887 nextchr = UCHARAT(++locinput);
2890 PL_reg_flags |= RF_tainted;
2895 if (!(OP(scan) == LOWER
2896 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2898 nextchr = UCHARAT(++locinput);
2901 PL_reg_flags |= RF_tainted;
2906 if (nextchr & 0x80) {
2907 if (!(OP(scan) == LOWERUTF8
2908 ? swash_fetch(PL_utf8_lower, (U8*)locinput)
2909 : isLOWER_LC_utf8((U8*)locinput)))
2913 locinput += PL_utf8skip[nextchr];
2914 nextchr = UCHARAT(locinput);
2917 if (!(OP(scan) == LOWERUTF8
2918 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2920 nextchr = UCHARAT(++locinput);
2923 PL_reg_flags |= RF_tainted;
2928 if (OP(scan) == LOWER
2929 ? isLOWER(nextchr) : isLOWER_LC(nextchr))
2931 nextchr = UCHARAT(++locinput);
2934 PL_reg_flags |= RF_tainted;
2937 if (!nextchr && locinput >= PL_regeol)
2939 if (nextchr & 0x80) {
2940 if (swash_fetch(PL_utf8_lower,(U8*)locinput))
2942 locinput += PL_utf8skip[nextchr];
2943 nextchr = UCHARAT(locinput);
2946 if (isLOWER(nextchr))
2948 nextchr = UCHARAT(++locinput);
2951 PL_reg_flags |= RF_tainted;
2956 if (!(OP(scan) == PRINT
2957 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2959 nextchr = UCHARAT(++locinput);
2962 PL_reg_flags |= RF_tainted;
2967 if (nextchr & 0x80) {
2968 if (!(OP(scan) == PRINTUTF8
2969 ? swash_fetch(PL_utf8_print, (U8*)locinput)
2970 : isPRINT_LC_utf8((U8*)locinput)))
2974 locinput += PL_utf8skip[nextchr];
2975 nextchr = UCHARAT(locinput);
2978 if (!(OP(scan) == PRINTUTF8
2979 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2981 nextchr = UCHARAT(++locinput);
2984 PL_reg_flags |= RF_tainted;
2989 if (OP(scan) == PRINT
2990 ? isPRINT(nextchr) : isPRINT_LC(nextchr))
2992 nextchr = UCHARAT(++locinput);
2995 PL_reg_flags |= RF_tainted;
2998 if (!nextchr && locinput >= PL_regeol)
3000 if (nextchr & 0x80) {
3001 if (swash_fetch(PL_utf8_print,(U8*)locinput))
3003 locinput += PL_utf8skip[nextchr];
3004 nextchr = UCHARAT(locinput);
3007 if (isPRINT(nextchr))
3009 nextchr = UCHARAT(++locinput);
3012 PL_reg_flags |= RF_tainted;
3017 if (!(OP(scan) == PUNCT
3018 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3020 nextchr = UCHARAT(++locinput);
3023 PL_reg_flags |= RF_tainted;
3028 if (nextchr & 0x80) {
3029 if (!(OP(scan) == PUNCTUTF8
3030 ? swash_fetch(PL_utf8_punct, (U8*)locinput)
3031 : isPUNCT_LC_utf8((U8*)locinput)))
3035 locinput += PL_utf8skip[nextchr];
3036 nextchr = UCHARAT(locinput);
3039 if (!(OP(scan) == PUNCTUTF8
3040 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3042 nextchr = UCHARAT(++locinput);
3045 PL_reg_flags |= RF_tainted;
3050 if (OP(scan) == PUNCT
3051 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr))
3053 nextchr = UCHARAT(++locinput);
3056 PL_reg_flags |= RF_tainted;
3059 if (!nextchr && locinput >= PL_regeol)
3061 if (nextchr & 0x80) {
3062 if (swash_fetch(PL_utf8_punct,(U8*)locinput))
3064 locinput += PL_utf8skip[nextchr];
3065 nextchr = UCHARAT(locinput);
3068 if (isPUNCT(nextchr))
3070 nextchr = UCHARAT(++locinput);
3073 PL_reg_flags |= RF_tainted;
3078 if (!(OP(scan) == UPPER
3079 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3081 nextchr = UCHARAT(++locinput);
3084 PL_reg_flags |= RF_tainted;
3089 if (nextchr & 0x80) {
3090 if (!(OP(scan) == UPPERUTF8
3091 ? swash_fetch(PL_utf8_upper, (U8*)locinput)
3092 : isUPPER_LC_utf8((U8*)locinput)))
3096 locinput += PL_utf8skip[nextchr];
3097 nextchr = UCHARAT(locinput);
3100 if (!(OP(scan) == UPPERUTF8
3101 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3103 nextchr = UCHARAT(++locinput);
3106 PL_reg_flags |= RF_tainted;
3111 if (OP(scan) == UPPER
3112 ? isUPPER(nextchr) : isUPPER_LC(nextchr))
3114 nextchr = UCHARAT(++locinput);
3117 PL_reg_flags |= RF_tainted;
3120 if (!nextchr && locinput >= PL_regeol)
3122 if (nextchr & 0x80) {
3123 if (swash_fetch(PL_utf8_upper,(U8*)locinput))
3125 locinput += PL_utf8skip[nextchr];
3126 nextchr = UCHARAT(locinput);
3129 if (isUPPER(nextchr))
3131 nextchr = UCHARAT(++locinput);
3134 if (!nextchr && locinput >= PL_regeol)
3136 if (!isXDIGIT(nextchr))
3138 nextchr = UCHARAT(++locinput);
3141 if (!nextchr && locinput >= PL_regeol)
3143 if (isXDIGIT(nextchr))
3145 nextchr = UCHARAT(++locinput);
3148 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
3150 locinput += PL_utf8skip[nextchr];
3151 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
3152 locinput += UTF8SKIP(locinput);
3153 if (locinput > PL_regeol)
3155 nextchr = UCHARAT(locinput);
3158 PL_reg_flags |= RF_tainted;
3162 n = ARG(scan); /* which paren pair */
3163 ln = PL_regstartp[n];
3164 if (*PL_reglastparen < n || ln == -1)
3165 sayNO; /* Do not match unless seen CLOSEn. */
3166 if (ln == PL_regendp[n])
3170 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
3172 char *e = PL_bostr + PL_regendp[n];
3174 * Note that we can't do the "other character" lookup trick as
3175 * in the 8-bit case (no pun intended) because in Unicode we
3176 * have to map both upper and title case to lower case.
3178 if (OP(scan) == REFF) {
3182 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
3192 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
3199 nextchr = UCHARAT(locinput);
3203 /* Inline the first character, for speed. */
3204 if (UCHARAT(s) != nextchr &&
3206 (UCHARAT(s) != ((OP(scan) == REFF
3207 ? PL_fold : PL_fold_locale)[nextchr]))))
3209 ln = PL_regendp[n] - ln;
3210 if (locinput + ln > PL_regeol)
3212 if (ln > 1 && (OP(scan) == REF
3213 ? memNE(s, locinput, ln)
3215 ? ibcmp(s, locinput, ln)
3216 : ibcmp_locale(s, locinput, ln))))
3219 nextchr = UCHARAT(locinput);
3230 OP_4tree *oop = PL_op;
3231 COP *ocurcop = PL_curcop;
3232 SV **ocurpad = PL_curpad;
3236 PL_op = (OP_4tree*)PL_regdata->data[n];
3237 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
3238 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
3239 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3241 CALLRUNOPS(aTHX); /* Scalar context. */
3247 PL_curpad = ocurpad;
3248 PL_curcop = ocurcop;
3250 if (logical == 2) { /* Postponed subexpression. */
3252 MAGIC *mg = Null(MAGIC*);
3255 CHECKPOINT cp, lastcp;
3257 if(SvROK(ret) || SvRMAGICAL(ret)) {
3258 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
3261 mg = mg_find(sv, 'r');
3264 re = (regexp *)mg->mg_obj;
3265 (void)ReREFCNT_inc(re);
3269 char *t = SvPV(ret, len);
3271 char *oprecomp = PL_regprecomp;
3272 I32 osize = PL_regsize;
3273 I32 onpar = PL_regnpar;
3276 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
3278 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
3279 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
3280 PL_regprecomp = oprecomp;
3285 PerlIO_printf(Perl_debug_log,
3286 "Entering embedded `%s%.60s%s%s'\n",
3290 (strlen(re->precomp) > 60 ? "..." : ""))
3293 state.prev = PL_reg_call_cc;
3294 state.cc = PL_regcc;
3295 state.re = PL_reg_re;
3301 cp = regcppush(0); /* Save *all* the positions. */
3304 state.ss = PL_savestack_ix;
3305 *PL_reglastparen = 0;
3306 PL_reg_call_cc = &state;
3307 PL_reginput = locinput;
3308 if (regmatch(re->program + 1)) {
3314 PerlIO_printf(Perl_debug_log,
3316 REPORT_CODE_OFF+PL_regindent*2, "")
3321 PL_reg_call_cc = state.prev;
3322 PL_regcc = state.cc;
3323 PL_reg_re = state.re;
3324 cache_re(PL_reg_re);
3331 sv_setsv(save_scalar(PL_replgv), ret);
3335 n = ARG(scan); /* which paren pair */
3336 PL_reg_start_tmp[n] = locinput;
3341 n = ARG(scan); /* which paren pair */
3342 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3343 PL_regendp[n] = locinput - PL_bostr;
3344 if (n > *PL_reglastparen)
3345 *PL_reglastparen = n;
3348 n = ARG(scan); /* which paren pair */
3349 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
3353 next = NEXTOPER(NEXTOPER(scan));
3355 next = scan + ARG(scan);
3356 if (OP(next) == IFTHEN) /* Fake one. */
3357 next = NEXTOPER(NEXTOPER(next));
3361 logical = scan->flags;
3365 CHECKPOINT cp = PL_savestack_ix;
3367 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3369 cc.oldcc = PL_regcc;
3371 cc.parenfloor = *PL_reglastparen;
3373 cc.min = ARG1(scan);
3374 cc.max = ARG2(scan);
3375 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3379 PL_reginput = locinput;
3380 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3382 PL_regcc = cc.oldcc;
3388 * This is really hard to understand, because after we match
3389 * what we're trying to match, we must make sure the rest of
3390 * the RE is going to match for sure, and to do that we have
3391 * to go back UP the parse tree by recursing ever deeper. And
3392 * if it fails, we have to reset our parent's current state
3393 * that we can try again after backing off.
3396 CHECKPOINT cp, lastcp;
3397 CURCUR* cc = PL_regcc;
3398 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3400 n = cc->cur + 1; /* how many we know we matched */
3401 PL_reginput = locinput;
3404 PerlIO_printf(Perl_debug_log,
3405 "%*s %ld out of %ld..%ld cc=%lx\n",
3406 REPORT_CODE_OFF+PL_regindent*2, "",
3407 (long)n, (long)cc->min,
3408 (long)cc->max, (long)cc)
3411 /* If degenerate scan matches "", assume scan done. */
3413 if (locinput == cc->lastloc && n >= cc->min) {
3414 PL_regcc = cc->oldcc;
3417 PerlIO_printf(Perl_debug_log,
3418 "%*s empty match detected, try continuation...\n",
3419 REPORT_CODE_OFF+PL_regindent*2, "")
3421 if (regmatch(cc->next))
3424 PerlIO_printf(Perl_debug_log,
3426 REPORT_CODE_OFF+PL_regindent*2, "")
3433 /* First just match a string of min scans. */
3437 cc->lastloc = locinput;
3438 if (regmatch(cc->scan))
3441 cc->lastloc = lastloc;
3443 PerlIO_printf(Perl_debug_log,
3445 REPORT_CODE_OFF+PL_regindent*2, "")
3450 /* Prefer next over scan for minimal matching. */
3453 PL_regcc = cc->oldcc;
3455 cp = regcppush(cc->parenfloor);
3457 if (regmatch(cc->next)) {
3459 sayYES; /* All done. */
3466 if (n >= cc->max) { /* Maximum greed exceeded? */
3467 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3468 && !(PL_reg_flags & RF_warned)) {
3469 PL_reg_flags |= RF_warned;
3470 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3471 "Complex regular subexpression recursion",
3478 PerlIO_printf(Perl_debug_log,
3479 "%*s trying longer...\n",
3480 REPORT_CODE_OFF+PL_regindent*2, "")
3482 /* Try scanning more and see if it helps. */
3483 PL_reginput = locinput;
3485 cc->lastloc = locinput;
3486 cp = regcppush(cc->parenfloor);
3488 if (regmatch(cc->scan)) {
3493 PerlIO_printf(Perl_debug_log,
3495 REPORT_CODE_OFF+PL_regindent*2, "")
3500 cc->lastloc = lastloc;
3504 /* Prefer scan over next for maximal matching. */
3506 if (n < cc->max) { /* More greed allowed? */
3507 cp = regcppush(cc->parenfloor);
3509 cc->lastloc = locinput;
3511 if (regmatch(cc->scan)) {
3516 regcppop(); /* Restore some previous $<digit>s? */
3517 PL_reginput = locinput;
3519 PerlIO_printf(Perl_debug_log,
3520 "%*s failed, try continuation...\n",
3521 REPORT_CODE_OFF+PL_regindent*2, "")
3524 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3525 && !(PL_reg_flags & RF_warned)) {
3526 PL_reg_flags |= RF_warned;
3527 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3528 "Complex regular subexpression recursion",
3532 /* Failed deeper matches of scan, so see if this one works. */
3533 PL_regcc = cc->oldcc;
3535 if (regmatch(cc->next))
3538 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
3539 REPORT_CODE_OFF+PL_regindent*2, "")
3544 cc->lastloc = lastloc;
3549 next = scan + ARG(scan);
3552 inner = NEXTOPER(NEXTOPER(scan));
3555 inner = NEXTOPER(scan);
3560 if (OP(next) != c1) /* No choice. */
3561 next = inner; /* Avoid recursion. */
3563 int lastparen = *PL_reglastparen;
3567 PL_reginput = locinput;
3568 if (regmatch(inner))
3571 for (n = *PL_reglastparen; n > lastparen; n--)
3573 *PL_reglastparen = n;
3576 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
3580 inner = NEXTOPER(scan);
3581 if (c1 == BRANCHJ) {
3582 inner = NEXTOPER(inner);
3584 } while (scan != NULL && OP(scan) == c1);
3598 /* We suppose that the next guy does not need
3599 backtracking: in particular, it is of constant length,
3600 and has no parenths to influence future backrefs. */
3601 ln = ARG1(scan); /* min to match */
3602 n = ARG2(scan); /* max to match */
3603 paren = scan->flags;
3605 if (paren > PL_regsize)
3607 if (paren > *PL_reglastparen)
3608 *PL_reglastparen = paren;
3610 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3612 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3613 PL_reginput = locinput;
3616 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3618 if (ln && l == 0 && n >= ln
3619 /* In fact, this is tricky. If paren, then the
3620 fact that we did/didnot match may influence
3621 future execution. */
3622 && !(paren && ln == 0))
3624 locinput = PL_reginput;
3625 if (PL_regkind[(U8)OP(next)] == EXACT) {
3626 c1 = UCHARAT(OPERAND(next) + 1);
3627 if (OP(next) == EXACTF)
3629 else if (OP(next) == EXACTFL)
3630 c2 = PL_fold_locale[c1];
3637 /* This may be improved if l == 0. */
3638 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3639 /* If it could work, try it. */
3641 UCHARAT(PL_reginput) == c1 ||
3642 UCHARAT(PL_reginput) == c2)
3646 PL_regstartp[paren] =
3647 HOPc(PL_reginput, -l) - PL_bostr;
3648 PL_regendp[paren] = PL_reginput - PL_bostr;
3651 PL_regendp[paren] = -1;
3657 /* Couldn't or didn't -- move forward. */
3658 PL_reginput = locinput;
3659 if (regrepeat_hard(scan, 1, &l)) {
3661 locinput = PL_reginput;
3668 n = regrepeat_hard(scan, n, &l);
3669 if (n != 0 && l == 0
3670 /* In fact, this is tricky. If paren, then the
3671 fact that we did/didnot match may influence
3672 future execution. */
3673 && !(paren && ln == 0))
3675 locinput = PL_reginput;
3677 PerlIO_printf(Perl_debug_log,
3678 "%*s matched %ld times, len=%ld...\n",
3679 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
3682 if (PL_regkind[(U8)OP(next)] == EXACT) {
3683 c1 = UCHARAT(OPERAND(next) + 1);
3684 if (OP(next) == EXACTF)
3686 else if (OP(next) == EXACTFL)
3687 c2 = PL_fold_locale[c1];
3696 /* If it could work, try it. */
3698 UCHARAT(PL_reginput) == c1 ||
3699 UCHARAT(PL_reginput) == c2)
3702 PerlIO_printf(Perl_debug_log,
3703 "%*s trying tail with n=%ld...\n",
3704 REPORT_CODE_OFF+PL_regindent*2, "", n)
3708 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3709 PL_regendp[paren] = PL_reginput - PL_bostr;
3712 PL_regendp[paren] = -1;
3718 /* Couldn't or didn't -- back up. */
3720 locinput = HOPc(locinput, -l);
3721 PL_reginput = locinput;
3728 paren = scan->flags; /* Which paren to set */
3729 if (paren > PL_regsize)
3731 if (paren > *PL_reglastparen)
3732 *PL_reglastparen = paren;
3733 ln = ARG1(scan); /* min to match */
3734 n = ARG2(scan); /* max to match */
3735 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3739 ln = ARG1(scan); /* min to match */
3740 n = ARG2(scan); /* max to match */
3741 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3746 scan = NEXTOPER(scan);
3752 scan = NEXTOPER(scan);
3756 * Lookahead to avoid useless match attempts
3757 * when we know what character comes next.
3759 if (PL_regkind[(U8)OP(next)] == EXACT) {
3760 c1 = UCHARAT(OPERAND(next) + 1);
3761 if (OP(next) == EXACTF)
3763 else if (OP(next) == EXACTFL)
3764 c2 = PL_fold_locale[c1];
3770 PL_reginput = locinput;
3774 if (ln && regrepeat(scan, ln) < ln)
3776 locinput = PL_reginput;
3779 char *e = locinput + n - ln; /* Should not check after this */
3780 char *old = locinput;
3782 if (e >= PL_regeol || (n == REG_INFTY))
3785 /* Find place 'next' could work */
3787 while (locinput <= e && *locinput != c1)
3790 while (locinput <= e
3797 /* PL_reginput == old now */
3798 if (locinput != old) {
3799 ln = 1; /* Did some */
3800 if (regrepeat(scan, locinput - old) <
3804 /* PL_reginput == locinput now */
3807 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
3808 PL_regendp[paren] = locinput - PL_bostr;
3811 PL_regendp[paren] = -1;
3815 PL_reginput = locinput; /* Could be reset... */
3817 /* Couldn't or didn't -- move forward. */
3822 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3823 /* If it could work, try it. */
3825 UCHARAT(PL_reginput) == c1 ||
3826 UCHARAT(PL_reginput) == c2)
3830 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3831 PL_regendp[paren] = PL_reginput - PL_bostr;
3834 PL_regendp[paren] = -1;
3840 /* Couldn't or didn't -- move forward. */
3841 PL_reginput = locinput;
3842 if (regrepeat(scan, 1)) {
3844 locinput = PL_reginput;
3852 n = regrepeat(scan, n);
3853 locinput = PL_reginput;
3854 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3855 (!PL_multiline || OP(next) == SEOL))
3856 ln = n; /* why back off? */
3860 /* If it could work, try it. */
3862 UCHARAT(PL_reginput) == c1 ||
3863 UCHARAT(PL_reginput) == c2)
3867 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3868 PL_regendp[paren] = PL_reginput - PL_bostr;
3871 PL_regendp[paren] = -1;
3877 /* Couldn't or didn't -- back up. */
3879 PL_reginput = locinput = HOPc(locinput, -1);
3884 /* If it could work, try it. */
3886 UCHARAT(PL_reginput) == c1 ||
3887 UCHARAT(PL_reginput) == c2)
3893 /* Couldn't or didn't -- back up. */
3895 PL_reginput = locinput = HOPc(locinput, -1);
3902 if (PL_reg_call_cc) {
3903 re_cc_state *cur_call_cc = PL_reg_call_cc;
3904 CURCUR *cctmp = PL_regcc;
3905 regexp *re = PL_reg_re;
3906 CHECKPOINT cp, lastcp;
3908 cp = regcppush(0); /* Save *all* the positions. */
3910 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3912 PL_reginput = locinput; /* Make position available to
3914 cache_re(PL_reg_call_cc->re);
3915 PL_regcc = PL_reg_call_cc->cc;
3916 PL_reg_call_cc = PL_reg_call_cc->prev;
3917 if (regmatch(cur_call_cc->node)) {
3918 PL_reg_call_cc = cur_call_cc;
3924 PL_reg_call_cc = cur_call_cc;
3930 PerlIO_printf(Perl_debug_log,
3931 "%*s continuation failed...\n",
3932 REPORT_CODE_OFF+PL_regindent*2, "")
3936 if (locinput < PL_regtill)
3937 sayNO; /* Cannot match: too short. */
3940 PL_reginput = locinput; /* put where regtry can find it */
3941 sayYES; /* Success! */
3944 PL_reginput = locinput;
3949 if (UTF) { /* XXXX This is absolutely
3950 broken, we read before
3952 s = HOPMAYBEc(locinput, -scan->flags);
3958 if (locinput < PL_bostr + scan->flags)
3960 PL_reginput = locinput - scan->flags;
3965 PL_reginput = locinput;
3970 if (UTF) { /* XXXX This is absolutely
3971 broken, we read before
3973 s = HOPMAYBEc(locinput, -scan->flags);
3974 if (!s || s < PL_bostr)
3979 if (locinput < PL_bostr + scan->flags)
3981 PL_reginput = locinput - scan->flags;
3986 PL_reginput = locinput;
3989 inner = NEXTOPER(NEXTOPER(scan));
3990 if (regmatch(inner) != n) {
4005 if (OP(scan) == SUSPEND) {
4006 locinput = PL_reginput;
4007 nextchr = UCHARAT(locinput);
4012 next = scan + ARG(scan);
4017 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
4018 (unsigned long)scan, OP(scan));
4019 Perl_croak(aTHX_ "regexp memory corruption");
4025 * We get here only if there's trouble -- normally "case END" is
4026 * the terminating point.
4028 Perl_croak(aTHX_ "corrupted regexp pointers");
4046 - regrepeat - repeatedly match something simple, report how many
4049 * [This routine now assumes that it will only match on things of length 1.
4050 * That was true before, but now we assume scan - reginput is the count,
4051 * rather than incrementing count on every character. [Er, except utf8.]]
4054 S_regrepeat(pTHX_ regnode *p, I32 max)
4057 register char *scan;
4058 register char *opnd;
4060 register char *loceol = PL_regeol;
4061 register I32 hardcount = 0;
4064 if (max != REG_INFTY && max < loceol - scan)
4065 loceol = scan + max;
4066 opnd = (char *) OPERAND(p);
4069 while (scan < loceol && *scan != '\n')
4077 while (scan < loceol && *scan != '\n') {
4078 scan += UTF8SKIP(scan);
4084 while (scan < loceol) {
4085 scan += UTF8SKIP(scan);
4089 case EXACT: /* length of string is 1 */
4090 c = UCHARAT(++opnd);
4091 while (scan < loceol && UCHARAT(scan) == c)
4094 case EXACTF: /* length of string is 1 */
4095 c = UCHARAT(++opnd);
4096 while (scan < loceol &&
4097 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4100 case EXACTFL: /* length of string is 1 */
4101 PL_reg_flags |= RF_tainted;
4102 c = UCHARAT(++opnd);
4103 while (scan < loceol &&
4104 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4109 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
4110 scan += UTF8SKIP(scan);
4115 while (scan < loceol && REGINCLASS(opnd, *scan))
4119 while (scan < loceol && isALNUM(*scan))
4124 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4125 scan += UTF8SKIP(scan);
4130 PL_reg_flags |= RF_tainted;
4131 while (scan < loceol && isALNUM_LC(*scan))
4135 PL_reg_flags |= RF_tainted;
4137 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
4138 scan += UTF8SKIP(scan);
4144 while (scan < loceol && !isALNUM(*scan))
4149 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4150 scan += UTF8SKIP(scan);
4155 PL_reg_flags |= RF_tainted;
4156 while (scan < loceol && !isALNUM_LC(*scan))
4160 PL_reg_flags |= RF_tainted;
4162 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
4163 scan += UTF8SKIP(scan);
4168 while (scan < loceol && isSPACE(*scan))
4173 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4174 scan += UTF8SKIP(scan);
4179 PL_reg_flags |= RF_tainted;
4180 while (scan < loceol && isSPACE_LC(*scan))
4184 PL_reg_flags |= RF_tainted;
4186 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4187 scan += UTF8SKIP(scan);
4192 while (scan < loceol && !isSPACE(*scan))
4197 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4198 scan += UTF8SKIP(scan);
4203 PL_reg_flags |= RF_tainted;
4204 while (scan < loceol && !isSPACE_LC(*scan))
4208 PL_reg_flags |= RF_tainted;
4210 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4211 scan += UTF8SKIP(scan);
4216 while (scan < loceol && isDIGIT(*scan))
4221 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
4222 scan += UTF8SKIP(scan);
4228 while (scan < loceol && !isDIGIT(*scan))
4233 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
4234 scan += UTF8SKIP(scan);
4238 default: /* Called on something of 0 width. */
4239 break; /* So match right here or not at all. */
4245 c = scan - PL_reginput;
4250 SV *prop = sv_newmortal();
4253 PerlIO_printf(Perl_debug_log,
4254 "%*s %s can match %ld times out of %ld...\n",
4255 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
4262 - regrepeat_hard - repeatedly match something, report total lenth and length
4264 * The repeater is supposed to have constant length.
4268 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4271 register char *scan;
4272 register char *start;
4273 register char *loceol = PL_regeol;
4275 I32 count = 0, res = 1;
4280 start = PL_reginput;
4282 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4285 while (start < PL_reginput) {
4287 start += UTF8SKIP(start);
4298 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4300 *lp = l = PL_reginput - start;
4301 if (max != REG_INFTY && l*max < loceol - scan)
4302 loceol = scan + l*max;
4315 - reginclass - determine if a character falls into a character class
4319 S_reginclass(pTHX_ register char *p, register I32 c)
4322 char flags = ANYOF_FLAGS(p);
4326 if (ANYOF_BITMAP_TEST(p, c))
4328 else if (flags & ANYOF_FOLD) {
4330 if (flags & ANYOF_LOCALE) {
4331 PL_reg_flags |= RF_tainted;
4332 cf = PL_fold_locale[c];
4336 if (ANYOF_BITMAP_TEST(p, cf))
4340 if (!match && (flags & ANYOF_CLASS)) {
4341 PL_reg_flags |= RF_tainted;
4343 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4344 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4345 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
4346 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4347 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4348 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4349 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4350 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4351 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4352 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4353 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
4354 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
4355 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4356 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4357 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4358 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4359 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
4360 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4361 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
4362 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4363 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4364 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4365 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
4366 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4367 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4368 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
4369 ) /* How's that for a conditional? */
4375 return (flags & ANYOF_INVERT) ? !match : match;
4379 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
4382 char flags = ARG1(f);
4384 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
4386 if (swash_fetch(sv, p))
4388 else if (flags & ANYOF_FOLD) {
4391 if (flags & ANYOF_LOCALE) {
4392 PL_reg_flags |= RF_tainted;
4393 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
4396 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
4397 if (swash_fetch(sv, tmpbuf))
4401 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
4403 return (flags & ANYOF_INVERT) ? !match : match;
4407 S_reghop(pTHX_ U8 *s, I32 off)
4411 while (off-- && s < (U8*)PL_regeol)
4416 if (s > (U8*)PL_bostr) {
4419 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4421 } /* XXX could check well-formedness here */
4429 S_reghopmaybe(pTHX_ U8* s, I32 off)
4433 while (off-- && s < (U8*)PL_regeol)
4440 if (s > (U8*)PL_bostr) {
4443 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4445 } /* XXX could check well-formedness here */
4462 restore_pos(pTHXo_ void *arg)
4465 if (PL_reg_eval_set) {
4466 if (PL_reg_oldsaved) {
4467 PL_reg_re->subbeg = PL_reg_oldsaved;
4468 PL_reg_re->sublen = PL_reg_oldsavedlen;
4469 RX_MATCH_COPIED_on(PL_reg_re);
4471 PL_reg_magic->mg_len = PL_reg_oldpos;
4472 PL_reg_eval_set = 0;
4473 PL_curpm = PL_reg_oldcurpm;