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
46 * pregcomp and pregexec -- regsub and regerror are not used in perl
48 * Copyright (c) 1986 by University of Toronto.
49 * Written by Henry Spencer. Not derived from licensed software.
51 * Permission is granted to anyone to use this software for any
52 * purpose on any computer system, and to redistribute it freely,
53 * subject to the following restrictions:
55 * 1. The author is not responsible for the consequences of use of
56 * this software, no matter how awful, even if they arise
59 * 2. The origin of this software must not be misrepresented, either
60 * by explicit claim or by omission.
62 * 3. Altered versions must be plainly marked as such, and must not
63 * be misrepresented as being the original software.
65 **** Alterations to Henry's code are...
67 **** Copyright (c) 1991-1999, Larry Wall
69 **** You may distribute under the terms of either the GNU General Public
70 **** License or the Artistic License, as specified in the README file.
72 * Beware that some of this code is subtly aware of the way operator
73 * precedence is structured in regular expressions. Serious changes in
74 * regular-expression syntax might require a total rethink.
77 #define PERL_IN_REGEXEC_C
82 #define RF_tainted 1 /* tainted information used? */
83 #define RF_warned 2 /* warned about big count? */
84 #define RF_evaled 4 /* Did an EVAL with setting? */
85 #define RF_utf8 8 /* String contains multibyte chars? */
87 #define UTF (PL_reg_flags & RF_utf8)
89 #define RS_init 1 /* eval environment created */
90 #define RS_set 2 /* replsv value is set */
100 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c) : ANYOF_BITMAP_TEST(p,c))
101 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
103 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
106 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
107 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
108 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
110 #define HOPc(pos,off) ((char*)HOP(pos,off))
111 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
114 S_regcppush(pTHX_ I32 parenfloor)
117 int retval = PL_savestack_ix;
118 int i = (PL_regsize - parenfloor) * 4;
122 for (p = PL_regsize; p > parenfloor; p--) {
123 SSPUSHINT(PL_regendp[p]);
124 SSPUSHINT(PL_regstartp[p]);
125 SSPUSHPTR(PL_reg_start_tmp[p]);
128 SSPUSHINT(PL_regsize);
129 SSPUSHINT(*PL_reglastparen);
130 SSPUSHPTR(PL_reginput);
132 SSPUSHINT(SAVEt_REGCONTEXT);
136 /* These are needed since we do not localize EVAL nodes: */
137 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
138 " Setting an EVAL scope, savestack=%i\n", \
139 PL_savestack_ix)); lastcp = PL_savestack_ix
141 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
142 PerlIO_printf(Perl_debug_log, \
143 " Clearing an EVAL scope, savestack=%i..%i\n", \
144 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
154 assert(i == SAVEt_REGCONTEXT);
156 input = (char *) SSPOPPTR;
157 *PL_reglastparen = SSPOPINT;
158 PL_regsize = SSPOPINT;
159 for (i -= 3; i > 0; i -= 4) {
160 paren = (U32)SSPOPINT;
161 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
162 PL_regstartp[paren] = SSPOPINT;
164 if (paren <= *PL_reglastparen)
165 PL_regendp[paren] = tmps;
167 PerlIO_printf(Perl_debug_log,
168 " restoring \\%d to %d(%d)..%d%s\n",
169 paren, PL_regstartp[paren],
170 PL_reg_start_tmp[paren] - PL_bostr,
172 (paren > *PL_reglastparen ? "(no)" : ""));
176 if (*PL_reglastparen + 1 <= PL_regnpar) {
177 PerlIO_printf(Perl_debug_log,
178 " restoring \\%d..\\%d to undef\n",
179 *PL_reglastparen + 1, PL_regnpar);
182 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
183 if (paren > PL_regsize)
184 PL_regstartp[paren] = -1;
185 PL_regendp[paren] = -1;
191 S_regcp_set_to(pTHX_ I32 ss)
194 I32 tmp = PL_savestack_ix;
196 PL_savestack_ix = ss;
198 PL_savestack_ix = tmp;
202 typedef struct re_cc_state
206 struct re_cc_state *prev;
211 #define regcpblow(cp) LEAVE_SCOPE(cp)
214 * pregexec and friends
218 - pregexec - match a regexp against a string
221 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
222 char *strbeg, I32 minend, SV *screamer, U32 nosave)
223 /* strend: pointer to null at end of string */
224 /* strbeg: real beginning of string */
225 /* minend: end of match must be >=minend after stringarg. */
226 /* nosave: For optimizations. */
229 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
230 nosave ? 0 : REXEC_COPY_STR);
234 S_cache_re(pTHX_ regexp *prog)
237 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
239 PL_regprogram = prog->program;
241 PL_regnpar = prog->nparens;
242 PL_regdata = prog->data;
247 S_restore_pos(pTHX_ void *arg)
250 if (PL_reg_eval_set) {
251 if (PL_reg_oldsaved) {
252 PL_reg_re->subbeg = PL_reg_oldsaved;
253 PL_reg_re->sublen = PL_reg_oldsavedlen;
254 RX_MATCH_COPIED_on(PL_reg_re);
256 PL_reg_magic->mg_len = PL_reg_oldpos;
258 PL_curpm = PL_reg_oldcurpm;
263 * Need to implement the following flags for reg_anch:
265 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
267 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
268 * INTUIT_AUTORITATIVE_ML
269 * INTUIT_ONCE_NOML - Intuit can match in one location only.
272 * Another flag for this function: SECOND_TIME (so that float substrs
273 * with giant delta may be not rechecked).
276 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
278 /* If SCREAM, then sv should be compatible with strpos and strend.
279 Otherwise, only SvCUR(sv) is used to get strbeg. */
281 /* XXXX We assume that strpos is strbeg unless sv. */
284 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
285 char *strend, U32 flags, re_scream_pos_data *data)
288 /* Should be nonnegative! */
294 DEBUG_r( if (!PL_colorset) reginitcolors() );
295 DEBUG_r(PerlIO_printf(Perl_debug_log,
296 "%sGuessing start of match:%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
297 PL_colors[4],PL_colors[5],PL_colors[0],
300 (strlen(prog->precomp) > 60 ? "..." : ""),
302 (strend - strpos > 60 ? 60 : strend - strpos),
303 strpos, PL_colors[1],
304 (strend - strpos > 60 ? "..." : ""))
307 if (prog->minlen > strend - strpos)
310 /* XXXX Move further down? */
311 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
312 /* Should be nonnegative! */
313 end_shift = prog->minlen - start_shift -
314 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
316 if (prog->reganch & ROPT_ANCH) {
317 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
318 || ( (prog->reganch & ROPT_ANCH_BOL)
319 && !PL_multiline ) );
321 if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
325 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
326 && (sv && (strpos + SvCUR(sv) != strend)) )
329 s = (char*)HOP((U8*)strpos, prog->check_offset_min);
330 if (SvTAIL(prog->check_substr)) {
331 slen = SvCUR(prog->check_substr); /* >= 1 */
333 if ( strend - s > slen || strend - s < slen - 1 ) {
337 if ( strend - s == slen && strend[-1] != '\n') {
341 /* Now should match s[0..slen-2] */
343 if (slen && (*SvPVX(prog->check_substr) != *s
345 && memNE(SvPVX(prog->check_substr), s, slen))))
348 else if (*SvPVX(prog->check_substr) != *s
349 || ((slen = SvCUR(prog->check_substr)) > 1
350 && memNE(SvPVX(prog->check_substr), s, slen)))
357 if (!ml_anch && (s + prog->check_offset_max < strend - prog->minlen))
358 end_shift += strend - s - prog->minlen - prog->check_offset_max;
366 if (flags & REXEC_SCREAM) {
367 SV *c = prog->check_substr;
368 char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
369 I32 p = -1; /* Internal iterator of scream. */
370 I32 *pp = data ? data->scream_pos : &p;
372 if (PL_screamfirst[BmRARE(c)] >= 0
373 || ( BmRARE(c) == '\n'
374 && (BmPREVIOUS(c) == SvCUR(c) - 1)
376 s = screaminstr(sv, prog->check_substr,
377 start_shift + (strpos - strbeg), end_shift, pp, 0);
381 *data->scream_olds = s;
384 s = fbm_instr((unsigned char*)s + start_shift,
385 (unsigned char*)strend - end_shift,
386 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
388 /* Update the count-of-usability, remove useless subpatterns,
392 ++BmUSEFUL(prog->check_substr); /* hooray */
393 goto fail; /* not present */
395 else if (s - strpos > prog->check_offset_max &&
396 ((prog->reganch & ROPT_UTF8)
397 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
399 : (t = s - prog->check_offset_max) != 0) ) {
400 if (ml_anch && t[-1] != '\n') {
402 while (t < strend - end_shift - prog->minlen) {
404 if (t < s - prog->check_offset_min) {
418 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
422 && (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n') {
426 if (!(prog->reganch & ROPT_NAUGHTY)
427 && --BmUSEFUL(prog->check_substr) < 0
428 && prog->check_substr == prog->float_substr) { /* boo */
429 /* If flags & SOMETHING - do not do it many times on the same match */
430 SvREFCNT_dec(prog->check_substr);
431 prog->check_substr = Nullsv; /* disable */
432 prog->float_substr = Nullsv; /* clear */
434 prog->reganch &= ~RE_USE_INTUIT;
440 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sFound%s at offset %ld\n",
441 PL_colors[4],PL_colors[5], (long)(s - strpos)) );
444 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sNot found...%s\n",
445 PL_colors[4],PL_colors[5]));
450 - regexec_flags - match a regexp against a string
453 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
454 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
455 /* strend: pointer to null at end of string */
456 /* strbeg: real beginning of string */
457 /* minend: end of match must be >=minend after stringarg. */
458 /* data: May be used for some additional optimizations. */
459 /* nosave: For optimizations. */
464 register char *startpos = stringarg;
466 I32 minlen; /* must match at least this many chars */
467 I32 dontbother = 0; /* how many characters not to try at end */
469 I32 start_shift = 0; /* Offset of the start to find
470 constant substr. */ /* CC */
471 I32 end_shift = 0; /* Same for the end. */ /* CC */
472 I32 scream_pos = -1; /* Internal iterator of scream. */
474 SV* oreplsv = GvSV(PL_replgv);
482 PL_regnarrate = PL_debug & 512;
486 if (prog == NULL || startpos == NULL) {
487 Perl_croak(aTHX_ "NULL regexp parameter");
491 minlen = prog->minlen;
492 if (strend - startpos < minlen) goto phooey;
494 if (startpos == strbeg) /* is ^ valid at stringarg? */
497 PL_regprev = (U32)stringarg[-1];
498 if (!PL_multiline && PL_regprev == '\n')
499 PL_regprev = '\0'; /* force ^ to NOT match */
502 /* Check validity of program. */
503 if (UCHARAT(prog->program) != REG_MAGIC) {
504 Perl_croak(aTHX_ "corrupted regexp program");
510 if (prog->reganch & ROPT_UTF8)
511 PL_reg_flags |= RF_utf8;
513 /* Mark beginning of line for ^ and lookbehind. */
514 PL_regbol = startpos;
518 /* Mark end of line for $ (and such) */
521 /* see how far we have to get to not match where we matched before */
522 PL_regtill = startpos+minend;
524 /* We start without call_cc context. */
527 /* If there is a "must appear" string, look for it. */
530 if (prog->reganch & ROPT_GPOS_SEEN) {
533 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
534 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
535 PL_reg_ganch = strbeg + mg->mg_len;
537 PL_reg_ganch = startpos;
538 if (prog->reganch & ROPT_ANCH_GPOS) {
539 if (s > PL_reg_ganch)
545 if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
546 re_scream_pos_data d;
548 d.scream_olds = &scream_olds;
549 d.scream_pos = &scream_pos;
550 s = re_intuit_start(prog, sv, s, strend, flags, &d);
552 goto phooey; /* not present */
555 DEBUG_r( if (!PL_colorset) reginitcolors() );
556 DEBUG_r(PerlIO_printf(Perl_debug_log,
557 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
558 PL_colors[4],PL_colors[5],PL_colors[0],
561 (strlen(prog->precomp) > 60 ? "..." : ""),
563 (strend - startpos > 60 ? 60 : strend - startpos),
564 startpos, PL_colors[1],
565 (strend - startpos > 60 ? "..." : ""))
568 /* Simplest case: anchored match need be tried only once. */
569 /* [unless only anchor is BOL and multiline is set] */
570 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
571 if (s == startpos && regtry(prog, startpos))
573 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
574 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
579 dontbother = minlen - 1;
580 end = HOPc(strend, -dontbother) - 1;
581 /* for multiline we only have to try after newlines */
582 if (prog->check_substr) {
588 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
596 if (*s++ == '\n') { /* don't need PL_utf8skip here */
604 } else if (prog->reganch & ROPT_ANCH_GPOS) {
605 if (regtry(prog, PL_reg_ganch))
610 /* Messy cases: unanchored match. */
611 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
612 /* we have /x+whatever/ */
613 /* it must be a one character string (XXXX Except UTF?) */
614 char ch = SvPVX(prog->anchored_substr)[0];
618 if (regtry(prog, s)) goto got_it;
620 while (s < strend && *s == ch)
629 if (regtry(prog, s)) goto got_it;
631 while (s < strend && *s == ch)
639 else if (prog->anchored_substr != Nullsv
640 || (prog->float_substr != Nullsv
641 && prog->float_max_offset < strend - s)) {
642 SV *must = prog->anchored_substr
643 ? prog->anchored_substr : prog->float_substr;
645 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
647 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
648 I32 delta = back_max - back_min;
649 char *last = HOPc(strend, /* Cannot start after this */
650 -(I32)(CHR_SVLEN(must)
651 - (SvTAIL(must) != 0) + back_min));
652 char *last1; /* Last position checked before */
657 last1 = s - 1; /* bogus */
659 /* XXXX check_substr already used to find `s', can optimize if
660 check_substr==must. */
662 dontbother = end_shift;
663 strend = HOPc(strend, -dontbother);
664 while ( (s <= last) &&
665 ((flags & REXEC_SCREAM)
666 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
667 end_shift, &scream_pos, 0))
668 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
669 (unsigned char*)strend, must,
670 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
671 if (HOPc(s, -back_max) > last1) {
672 last1 = HOPc(s, -back_min);
673 s = HOPc(s, -back_max);
676 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
678 last1 = HOPc(s, -back_min);
698 else if (c = prog->regstclass) {
699 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
703 dontbother = minlen - 1;
704 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
706 /* We know what class it must start with. */
709 cc = (char *) OPERAND(c);
711 if (REGINCLASSUTF8(c, (U8*)s)) {
712 if (tmp && regtry(prog, s))
723 cc = (char *) OPERAND(c);
725 if (REGINCLASS(cc, *s)) {
726 if (tmp && regtry(prog, s))
737 PL_reg_flags |= RF_tainted;
744 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
745 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
747 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
754 if ((minlen || tmp) && regtry(prog,s))
758 PL_reg_flags |= RF_tainted;
763 strend = reghop_c(strend, -1);
765 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
766 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
768 if (tmp == !(OP(c) == BOUND ?
769 swash_fetch(PL_utf8_alnum, (U8*)s) :
770 isALNUM_LC_utf8((U8*)s)))
778 if ((minlen || tmp) && regtry(prog,s))
782 PL_reg_flags |= RF_tainted;
789 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
790 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
792 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
794 else if (regtry(prog, s))
798 if ((minlen || !tmp) && regtry(prog,s))
802 PL_reg_flags |= RF_tainted;
807 strend = reghop_c(strend, -1);
809 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
810 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
812 if (tmp == !(OP(c) == NBOUND ?
813 swash_fetch(PL_utf8_alnum, (U8*)s) :
814 isALNUM_LC_utf8((U8*)s)))
816 else if (regtry(prog, s))
820 if ((minlen || !tmp) && regtry(prog,s))
826 if (tmp && regtry(prog, s))
838 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
839 if (tmp && regtry(prog, s))
850 PL_reg_flags |= RF_tainted;
852 if (isALNUM_LC(*s)) {
853 if (tmp && regtry(prog, s))
864 PL_reg_flags |= RF_tainted;
866 if (isALNUM_LC_utf8((U8*)s)) {
867 if (tmp && regtry(prog, s))
880 if (tmp && regtry(prog, s))
892 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
893 if (tmp && regtry(prog, s))
904 PL_reg_flags |= RF_tainted;
906 if (!isALNUM_LC(*s)) {
907 if (tmp && regtry(prog, s))
918 PL_reg_flags |= RF_tainted;
920 if (!isALNUM_LC_utf8((U8*)s)) {
921 if (tmp && regtry(prog, s))
934 if (tmp && regtry(prog, s))
946 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
947 if (tmp && regtry(prog, s))
958 PL_reg_flags |= RF_tainted;
960 if (isSPACE_LC(*s)) {
961 if (tmp && regtry(prog, s))
972 PL_reg_flags |= RF_tainted;
974 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
975 if (tmp && regtry(prog, s))
988 if (tmp && regtry(prog, s))
1000 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1001 if (tmp && regtry(prog, s))
1012 PL_reg_flags |= RF_tainted;
1013 while (s < strend) {
1014 if (!isSPACE_LC(*s)) {
1015 if (tmp && regtry(prog, s))
1026 PL_reg_flags |= RF_tainted;
1027 while (s < strend) {
1028 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1029 if (tmp && regtry(prog, s))
1040 while (s < strend) {
1042 if (tmp && regtry(prog, s))
1053 while (s < strend) {
1054 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1055 if (tmp && regtry(prog, s))
1066 PL_reg_flags |= RF_tainted;
1067 while (s < strend) {
1068 if (isDIGIT_LC(*s)) {
1069 if (tmp && regtry(prog, s))
1080 PL_reg_flags |= RF_tainted;
1081 while (s < strend) {
1082 if (isDIGIT_LC_utf8((U8*)s)) {
1083 if (tmp && regtry(prog, s))
1094 while (s < strend) {
1096 if (tmp && regtry(prog, s))
1107 while (s < strend) {
1108 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1109 if (tmp && regtry(prog, s))
1120 PL_reg_flags |= RF_tainted;
1121 while (s < strend) {
1122 if (!isDIGIT_LC(*s)) {
1123 if (tmp && regtry(prog, s))
1134 PL_reg_flags |= RF_tainted;
1135 while (s < strend) {
1136 if (!isDIGIT_LC_utf8((U8*)s)) {
1137 if (tmp && regtry(prog, s))
1148 while (s < strend) {
1150 if (tmp && regtry(prog, s))
1161 while (s < strend) {
1162 if (swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1163 if (tmp && regtry(prog, s))
1174 PL_reg_flags |= RF_tainted;
1175 while (s < strend) {
1176 if (isALNUMC_LC(*s)) {
1177 if (tmp && regtry(prog, s))
1188 PL_reg_flags |= RF_tainted;
1189 while (s < strend) {
1190 if (isALNUMC_LC_utf8((U8*)s)) {
1191 if (tmp && regtry(prog, s))
1202 while (s < strend) {
1203 if (!isALNUMC(*s)) {
1204 if (tmp && regtry(prog, s))
1215 while (s < strend) {
1216 if (!swash_fetch(PL_utf8_alnumc, (U8*)s)) {
1217 if (tmp && regtry(prog, s))
1228 PL_reg_flags |= RF_tainted;
1229 while (s < strend) {
1230 if (!isALNUMC_LC(*s)) {
1231 if (tmp && regtry(prog, s))
1242 PL_reg_flags |= RF_tainted;
1243 while (s < strend) {
1244 if (!isALNUMC_LC_utf8((U8*)s)) {
1245 if (tmp && regtry(prog, s))
1256 while (s < strend) {
1257 if (isASCII(*(U8*)s)) {
1258 if (tmp && regtry(prog, s))
1269 while (s < strend) {
1270 if (!isASCII(*(U8*)s)) {
1271 if (tmp && regtry(prog, s))
1282 while (s < strend) {
1284 if (tmp && regtry(prog, s))
1295 while (s < strend) {
1296 if (swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1297 if (tmp && regtry(prog, s))
1308 PL_reg_flags |= RF_tainted;
1309 while (s < strend) {
1310 if (isCNTRL_LC(*s)) {
1311 if (tmp && regtry(prog, s))
1322 PL_reg_flags |= RF_tainted;
1323 while (s < strend) {
1324 if (*s == ' ' || isCNTRL_LC_utf8((U8*)s)) {
1325 if (tmp && regtry(prog, s))
1336 while (s < strend) {
1338 if (tmp && regtry(prog, s))
1349 while (s < strend) {
1350 if (!swash_fetch(PL_utf8_cntrl,(U8*)s)) {
1351 if (tmp && regtry(prog, s))
1362 PL_reg_flags |= RF_tainted;
1363 while (s < strend) {
1364 if (!isCNTRL_LC(*s)) {
1365 if (tmp && regtry(prog, s))
1376 PL_reg_flags |= RF_tainted;
1377 while (s < strend) {
1378 if (!isCNTRL_LC_utf8((U8*)s)) {
1379 if (tmp && regtry(prog, s))
1390 while (s < strend) {
1392 if (tmp && regtry(prog, s))
1403 while (s < strend) {
1404 if (swash_fetch(PL_utf8_graph,(U8*)s)) {
1405 if (tmp && regtry(prog, s))
1416 PL_reg_flags |= RF_tainted;
1417 while (s < strend) {
1418 if (isGRAPH_LC(*s)) {
1419 if (tmp && regtry(prog, s))
1430 PL_reg_flags |= RF_tainted;
1431 while (s < strend) {
1432 if (*s == ' ' || isGRAPH_LC_utf8((U8*)s)) {
1433 if (tmp && regtry(prog, s))
1444 while (s < strend) {
1446 if (tmp && regtry(prog, s))
1457 while (s < strend) {
1458 if (!swash_fetch(PL_utf8_graph,(U8*)s)) {
1459 if (tmp && regtry(prog, s))
1470 PL_reg_flags |= RF_tainted;
1471 while (s < strend) {
1472 if (!isGRAPH_LC(*s)) {
1473 if (tmp && regtry(prog, s))
1484 PL_reg_flags |= RF_tainted;
1485 while (s < strend) {
1486 if (!isGRAPH_LC_utf8((U8*)s)) {
1487 if (tmp && regtry(prog, s))
1498 while (s < strend) {
1500 if (tmp && regtry(prog, s))
1511 while (s < strend) {
1512 if (swash_fetch(PL_utf8_lower,(U8*)s)) {
1513 if (tmp && regtry(prog, s))
1524 PL_reg_flags |= RF_tainted;
1525 while (s < strend) {
1526 if (isLOWER_LC(*s)) {
1527 if (tmp && regtry(prog, s))
1538 PL_reg_flags |= RF_tainted;
1539 while (s < strend) {
1540 if (*s == ' ' || isLOWER_LC_utf8((U8*)s)) {
1541 if (tmp && regtry(prog, s))
1552 while (s < strend) {
1554 if (tmp && regtry(prog, s))
1565 while (s < strend) {
1566 if (!swash_fetch(PL_utf8_lower,(U8*)s)) {
1567 if (tmp && regtry(prog, s))
1578 PL_reg_flags |= RF_tainted;
1579 while (s < strend) {
1580 if (!isLOWER_LC(*s)) {
1581 if (tmp && regtry(prog, s))
1592 PL_reg_flags |= RF_tainted;
1593 while (s < strend) {
1594 if (!isLOWER_LC_utf8((U8*)s)) {
1595 if (tmp && regtry(prog, s))
1606 while (s < strend) {
1608 if (tmp && regtry(prog, s))
1619 while (s < strend) {
1620 if (swash_fetch(PL_utf8_print,(U8*)s)) {
1621 if (tmp && regtry(prog, s))
1632 PL_reg_flags |= RF_tainted;
1633 while (s < strend) {
1634 if (isPRINT_LC(*s)) {
1635 if (tmp && regtry(prog, s))
1646 PL_reg_flags |= RF_tainted;
1647 while (s < strend) {
1648 if (*s == ' ' || isPRINT_LC_utf8((U8*)s)) {
1649 if (tmp && regtry(prog, s))
1660 while (s < strend) {
1662 if (tmp && regtry(prog, s))
1673 while (s < strend) {
1674 if (!swash_fetch(PL_utf8_print,(U8*)s)) {
1675 if (tmp && regtry(prog, s))
1686 PL_reg_flags |= RF_tainted;
1687 while (s < strend) {
1688 if (!isPRINT_LC(*s)) {
1689 if (tmp && regtry(prog, s))
1700 PL_reg_flags |= RF_tainted;
1701 while (s < strend) {
1702 if (!isPRINT_LC_utf8((U8*)s)) {
1703 if (tmp && regtry(prog, s))
1714 while (s < strend) {
1716 if (tmp && regtry(prog, s))
1727 while (s < strend) {
1728 if (swash_fetch(PL_utf8_punct,(U8*)s)) {
1729 if (tmp && regtry(prog, s))
1740 PL_reg_flags |= RF_tainted;
1741 while (s < strend) {
1742 if (isPUNCT_LC(*s)) {
1743 if (tmp && regtry(prog, s))
1754 PL_reg_flags |= RF_tainted;
1755 while (s < strend) {
1756 if (*s == ' ' || isPUNCT_LC_utf8((U8*)s)) {
1757 if (tmp && regtry(prog, s))
1768 while (s < strend) {
1770 if (tmp && regtry(prog, s))
1781 while (s < strend) {
1782 if (!swash_fetch(PL_utf8_punct,(U8*)s)) {
1783 if (tmp && regtry(prog, s))
1794 PL_reg_flags |= RF_tainted;
1795 while (s < strend) {
1796 if (!isPUNCT_LC(*s)) {
1797 if (tmp && regtry(prog, s))
1808 PL_reg_flags |= RF_tainted;
1809 while (s < strend) {
1810 if (!isPUNCT_LC_utf8((U8*)s)) {
1811 if (tmp && regtry(prog, s))
1822 while (s < strend) {
1824 if (tmp && regtry(prog, s))
1835 while (s < strend) {
1836 if (swash_fetch(PL_utf8_upper,(U8*)s)) {
1837 if (tmp && regtry(prog, s))
1848 PL_reg_flags |= RF_tainted;
1849 while (s < strend) {
1850 if (isUPPER_LC(*s)) {
1851 if (tmp && regtry(prog, s))
1862 PL_reg_flags |= RF_tainted;
1863 while (s < strend) {
1864 if (*s == ' ' || isUPPER_LC_utf8((U8*)s)) {
1865 if (tmp && regtry(prog, s))
1876 while (s < strend) {
1878 if (tmp && regtry(prog, s))
1889 while (s < strend) {
1890 if (!swash_fetch(PL_utf8_upper,(U8*)s)) {
1891 if (tmp && regtry(prog, s))
1902 PL_reg_flags |= RF_tainted;
1903 while (s < strend) {
1904 if (!isUPPER_LC(*s)) {
1905 if (tmp && regtry(prog, s))
1916 PL_reg_flags |= RF_tainted;
1917 while (s < strend) {
1918 if (!isUPPER_LC_utf8((U8*)s)) {
1919 if (tmp && regtry(prog, s))
1930 while (s < strend) {
1932 if (tmp && regtry(prog, s))
1943 while (s < strend) {
1944 if (!isXDIGIT(*s)) {
1945 if (tmp && regtry(prog, s))
1959 if (prog->float_substr != Nullsv) { /* Trim the end. */
1961 I32 oldpos = scream_pos;
1963 if (flags & REXEC_SCREAM) {
1964 last = screaminstr(sv, prog->float_substr, s - strbeg,
1965 end_shift, &scream_pos, 1); /* last one */
1967 last = scream_olds; /* Only one occurence. */
1971 char *little = SvPV(prog->float_substr, len);
1973 if (SvTAIL(prog->float_substr)) {
1974 if (memEQ(strend - len + 1, little, len - 1))
1975 last = strend - len + 1;
1976 else if (!PL_multiline)
1977 last = memEQ(strend - len, little, len)
1978 ? strend - len : Nullch;
1984 last = rninstr(s, strend, little, little + len);
1986 last = strend; /* matching `$' */
1989 if (last == NULL) goto phooey; /* Should not happen! */
1990 dontbother = strend - last + prog->float_min_offset;
1992 if (minlen && (dontbother < minlen))
1993 dontbother = minlen - 1;
1994 strend -= dontbother; /* this one's always in bytes! */
1995 /* We don't know much -- general case. */
1998 if (regtry(prog, s))
2007 if (regtry(prog, s))
2009 } while (s++ < strend);
2017 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2019 if (PL_reg_eval_set) {
2020 /* Preserve the current value of $^R */
2021 if (oreplsv != GvSV(PL_replgv))
2022 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2023 restored, the value remains
2028 /* make sure $`, $&, $', and $digit will work later */
2029 if ( !(flags & REXEC_NOT_FIRST) ) {
2030 if (RX_MATCH_COPIED(prog)) {
2031 Safefree(prog->subbeg);
2032 RX_MATCH_COPIED_off(prog);
2034 if (flags & REXEC_COPY_STR) {
2035 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2037 s = savepvn(strbeg, i);
2040 RX_MATCH_COPIED_on(prog);
2043 prog->subbeg = strbeg;
2044 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2051 if (PL_reg_eval_set)
2057 - regtry - try match at specific point
2059 STATIC I32 /* 0 failure, 1 success */
2060 S_regtry(pTHX_ regexp *prog, char *startpos)
2068 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2071 PL_reg_eval_set = RS_init;
2073 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
2074 PL_stack_sp - PL_stack_base);
2076 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
2077 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2078 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2080 /* Apparently this is not needed, judging by wantarray. */
2081 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
2082 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2085 /* Make $_ available to executed code. */
2086 if (PL_reg_sv != DEFSV) {
2087 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
2092 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2093 && (mg = mg_find(PL_reg_sv, 'g')))) {
2094 /* prepare for quick setting of pos */
2095 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
2096 mg = mg_find(PL_reg_sv, 'g');
2100 PL_reg_oldpos = mg->mg_len;
2101 SAVEDESTRUCTOR(S_restore_pos, 0);
2104 New(22,PL_reg_curpm, 1, PMOP);
2105 PL_reg_curpm->op_pmregexp = prog;
2106 PL_reg_oldcurpm = PL_curpm;
2107 PL_curpm = PL_reg_curpm;
2108 if (RX_MATCH_COPIED(prog)) {
2109 /* Here is a serious problem: we cannot rewrite subbeg,
2110 since it may be needed if this match fails. Thus
2111 $` inside (?{}) could fail... */
2112 PL_reg_oldsaved = prog->subbeg;
2113 PL_reg_oldsavedlen = prog->sublen;
2114 RX_MATCH_COPIED_off(prog);
2117 PL_reg_oldsaved = Nullch;
2118 prog->subbeg = PL_bostr;
2119 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2121 prog->startp[0] = startpos - PL_bostr;
2122 PL_reginput = startpos;
2123 PL_regstartp = prog->startp;
2124 PL_regendp = prog->endp;
2125 PL_reglastparen = &prog->lastparen;
2126 prog->lastparen = 0;
2128 DEBUG_r(PL_reg_starttry = startpos);
2129 if (PL_reg_start_tmpl <= prog->nparens) {
2130 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2131 if(PL_reg_start_tmp)
2132 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2134 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2137 /* XXXX What this code is doing here?!!! There should be no need
2138 to do this again and again, PL_reglastparen should take care of
2142 if (prog->nparens) {
2143 for (i = prog->nparens; i >= 1; i--) {
2149 if (regmatch(prog->program + 1)) {
2150 prog->endp[0] = PL_reginput - PL_bostr;
2158 - regmatch - main matching routine
2160 * Conceptually the strategy is simple: check to see whether the current
2161 * node matches, call self recursively to see whether the rest matches,
2162 * and then act accordingly. In practice we make some effort to avoid
2163 * recursion, in particular by going through "ordinary" nodes (that don't
2164 * need to know whether the rest of the match failed) by a loop instead of
2167 /* [lwall] I've hoisted the register declarations to the outer block in order to
2168 * maybe save a little bit of pushing and popping on the stack. It also takes
2169 * advantage of machines that use a register save mask on subroutine entry.
2171 STATIC I32 /* 0 failure, 1 success */
2172 S_regmatch(pTHX_ regnode *prog)
2175 register regnode *scan; /* Current node. */
2176 regnode *next; /* Next node. */
2177 regnode *inner; /* Next node in internal branch. */
2178 register I32 nextchr; /* renamed nextchr - nextchar colides with
2179 function of same name */
2180 register I32 n; /* no or next */
2181 register I32 ln; /* len or last */
2182 register char *s; /* operand or save */
2183 register char *locinput = PL_reginput;
2184 register I32 c1, c2, paren; /* case fold search, parenth */
2185 int minmod = 0, sw = 0, logical = 0;
2190 /* Note that nextchr is a byte even in UTF */
2191 nextchr = UCHARAT(locinput);
2193 while (scan != NULL) {
2194 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
2196 # define sayYES goto yes
2197 # define sayNO goto no
2198 # define saySAME(x) if (x) goto yes; else goto no
2199 # define REPORT_CODE_OFF 24
2201 # define sayYES return 1
2202 # define sayNO return 0
2203 # define saySAME(x) return x
2206 SV *prop = sv_newmortal();
2207 int docolor = *PL_colors[0];
2208 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2209 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
2210 /* The part of the string before starttry has one color
2211 (pref0_len chars), between starttry and current
2212 position another one (pref_len - pref0_len chars),
2213 after the current position the third one.
2214 We assume that pref0_len <= pref_len, otherwise we
2215 decrease pref0_len. */
2216 int pref_len = (locinput - PL_bostr > (5 + taill) - l
2217 ? (5 + taill) - l : locinput - PL_bostr);
2218 int pref0_len = pref_len - (locinput - PL_reg_starttry);
2220 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2221 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2222 ? (5 + taill) - pref_len : PL_regeol - locinput);
2225 if (pref0_len > pref_len)
2226 pref0_len = pref_len;
2227 regprop(prop, scan);
2228 PerlIO_printf(Perl_debug_log,
2229 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
2230 locinput - PL_bostr,
2231 PL_colors[4], pref0_len,
2232 locinput - pref_len, PL_colors[5],
2233 PL_colors[2], pref_len - pref0_len,
2234 locinput - pref_len + pref0_len, PL_colors[3],
2235 (docolor ? "" : "> <"),
2236 PL_colors[0], l, locinput, PL_colors[1],
2237 15 - l - pref_len + 1,
2239 scan - PL_regprogram, PL_regindent*2, "",
2243 next = scan + NEXT_OFF(scan);
2249 if (locinput == PL_bostr
2250 ? PL_regprev == '\n'
2252 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2254 /* regtill = regbol; */
2259 if (locinput == PL_bostr
2260 ? PL_regprev == '\n'
2261 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2267 if (locinput == PL_regbol && PL_regprev == '\n')
2271 if (locinput == PL_reg_ganch)
2281 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2286 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2288 if (PL_regeol - locinput > 1)
2292 if (PL_regeol != locinput)
2296 if (nextchr & 0x80) {
2297 locinput += PL_utf8skip[nextchr];
2298 if (locinput > PL_regeol)
2300 nextchr = UCHARAT(locinput);
2303 if (!nextchr && locinput >= PL_regeol)
2305 nextchr = UCHARAT(++locinput);
2308 if (!nextchr && locinput >= PL_regeol)
2310 nextchr = UCHARAT(++locinput);
2313 if (nextchr & 0x80) {
2314 locinput += PL_utf8skip[nextchr];
2315 if (locinput > PL_regeol)
2317 nextchr = UCHARAT(locinput);
2320 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2322 nextchr = UCHARAT(++locinput);
2325 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
2327 nextchr = UCHARAT(++locinput);
2330 s = (char *) OPERAND(scan);
2332 /* Inline the first character, for speed. */
2333 if (UCHARAT(s) != nextchr)
2335 if (PL_regeol - locinput < ln)
2337 if (ln > 1 && memNE(s, locinput, ln))
2340 nextchr = UCHARAT(locinput);
2343 PL_reg_flags |= RF_tainted;
2346 s = (char *) OPERAND(scan);
2352 c1 = OP(scan) == EXACTF;
2356 if (utf8_to_uv((U8*)s, 0) != (c1 ?
2357 toLOWER_utf8((U8*)l) :
2358 toLOWER_LC_utf8((U8*)l)))
2366 nextchr = UCHARAT(locinput);
2370 /* Inline the first character, for speed. */
2371 if (UCHARAT(s) != nextchr &&
2372 UCHARAT(s) != ((OP(scan) == EXACTF)
2373 ? PL_fold : PL_fold_locale)[nextchr])
2375 if (PL_regeol - locinput < ln)
2377 if (ln > 1 && (OP(scan) == EXACTF
2378 ? ibcmp(s, locinput, ln)
2379 : ibcmp_locale(s, locinput, ln)))
2382 nextchr = UCHARAT(locinput);
2385 s = (char *) OPERAND(scan);
2386 if (!REGINCLASSUTF8(scan, (U8*)locinput))
2388 if (locinput >= PL_regeol)
2390 locinput += PL_utf8skip[nextchr];
2391 nextchr = UCHARAT(locinput);
2394 s = (char *) OPERAND(scan);
2396 nextchr = UCHARAT(locinput);
2397 if (!REGINCLASS(s, nextchr))
2399 if (!nextchr && locinput >= PL_regeol)
2401 nextchr = UCHARAT(++locinput);
2404 PL_reg_flags |= RF_tainted;
2409 if (!(OP(scan) == ALNUM
2410 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2412 nextchr = UCHARAT(++locinput);
2415 PL_reg_flags |= RF_tainted;
2420 if (nextchr & 0x80) {
2421 if (!(OP(scan) == ALNUMUTF8
2422 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2423 : isALNUM_LC_utf8((U8*)locinput)))
2427 locinput += PL_utf8skip[nextchr];
2428 nextchr = UCHARAT(locinput);
2431 if (!(OP(scan) == ALNUMUTF8
2432 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2434 nextchr = UCHARAT(++locinput);
2437 PL_reg_flags |= RF_tainted;
2440 if (!nextchr && locinput >= PL_regeol)
2442 if (OP(scan) == NALNUM
2443 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2445 nextchr = UCHARAT(++locinput);
2448 PL_reg_flags |= RF_tainted;
2451 if (!nextchr && locinput >= PL_regeol)
2453 if (nextchr & 0x80) {
2454 if (OP(scan) == NALNUMUTF8
2455 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2456 : isALNUM_LC_utf8((U8*)locinput))
2460 locinput += PL_utf8skip[nextchr];
2461 nextchr = UCHARAT(locinput);
2464 if (OP(scan) == NALNUMUTF8
2465 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2467 nextchr = UCHARAT(++locinput);
2471 PL_reg_flags |= RF_tainted;
2475 /* was last char in word? */
2476 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2477 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2479 n = isALNUM(nextchr);
2482 ln = isALNUM_LC(ln);
2483 n = isALNUM_LC(nextchr);
2485 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2490 PL_reg_flags |= RF_tainted;
2494 /* was last char in word? */
2495 ln = (locinput != PL_regbol)
2496 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
2497 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2498 ln = isALNUM_uni(ln);
2499 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2502 ln = isALNUM_LC_uni(ln);
2503 n = isALNUM_LC_utf8((U8*)locinput);
2505 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2509 PL_reg_flags |= RF_tainted;
2512 if (!nextchr && locinput >= PL_regeol)
2514 if (!(OP(scan) == SPACE
2515 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2517 nextchr = UCHARAT(++locinput);
2520 PL_reg_flags |= RF_tainted;
2523 if (!nextchr && locinput >= PL_regeol)
2525 if (nextchr & 0x80) {
2526 if (!(OP(scan) == SPACEUTF8
2527 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2528 : isSPACE_LC_utf8((U8*)locinput)))
2532 locinput += PL_utf8skip[nextchr];
2533 nextchr = UCHARAT(locinput);
2536 if (!(OP(scan) == SPACEUTF8
2537 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2539 nextchr = UCHARAT(++locinput);
2542 PL_reg_flags |= RF_tainted;
2547 if (OP(scan) == SPACE
2548 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2550 nextchr = UCHARAT(++locinput);
2553 PL_reg_flags |= RF_tainted;
2558 if (nextchr & 0x80) {
2559 if (OP(scan) == NSPACEUTF8
2560 ? swash_fetch(PL_utf8_space,(U8*)locinput)
2561 : isSPACE_LC_utf8((U8*)locinput))
2565 locinput += PL_utf8skip[nextchr];
2566 nextchr = UCHARAT(locinput);
2569 if (OP(scan) == NSPACEUTF8
2570 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2572 nextchr = UCHARAT(++locinput);
2575 PL_reg_flags |= RF_tainted;
2578 if (!nextchr && locinput >= PL_regeol)
2580 if (!(OP(scan) == DIGIT
2581 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2583 nextchr = UCHARAT(++locinput);
2586 PL_reg_flags |= RF_tainted;
2591 if (nextchr & 0x80) {
2592 if (OP(scan) == NDIGITUTF8
2593 ? swash_fetch(PL_utf8_digit,(U8*)locinput)
2594 : isDIGIT_LC_utf8((U8*)locinput))
2598 locinput += PL_utf8skip[nextchr];
2599 nextchr = UCHARAT(locinput);
2602 if (!isDIGIT(nextchr))
2604 nextchr = UCHARAT(++locinput);
2607 PL_reg_flags |= RF_tainted;
2612 if (OP(scan) == DIGIT
2613 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2615 nextchr = UCHARAT(++locinput);
2618 PL_reg_flags |= RF_tainted;
2621 if (!nextchr && locinput >= PL_regeol)
2623 if (nextchr & 0x80) {
2624 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
2626 locinput += PL_utf8skip[nextchr];
2627 nextchr = UCHARAT(locinput);
2630 if (isDIGIT(nextchr))
2632 nextchr = UCHARAT(++locinput);
2635 PL_reg_flags |= RF_tainted;
2640 if (!(OP(scan) == ALNUMC
2641 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2643 nextchr = UCHARAT(++locinput);
2646 PL_reg_flags |= RF_tainted;
2651 if (nextchr & 0x80) {
2652 if (!(OP(scan) == ALNUMCUTF8
2653 ? swash_fetch(PL_utf8_alnumc, (U8*)locinput)
2654 : isALNUMC_LC_utf8((U8*)locinput)))
2658 locinput += PL_utf8skip[nextchr];
2659 nextchr = UCHARAT(locinput);
2662 if (!(OP(scan) == ALNUMCUTF8
2663 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr)))
2665 nextchr = UCHARAT(++locinput);
2668 PL_reg_flags |= RF_tainted;
2673 if (OP(scan) == ALNUMC
2674 ? isALNUMC(nextchr) : isALNUMC_LC(nextchr))
2676 nextchr = UCHARAT(++locinput);
2679 PL_reg_flags |= RF_tainted;
2682 if (!nextchr && locinput >= PL_regeol)
2684 if (nextchr & 0x80) {
2685 if (swash_fetch(PL_utf8_alnumc,(U8*)locinput))
2687 locinput += PL_utf8skip[nextchr];
2688 nextchr = UCHARAT(locinput);
2691 if (isALNUMC(nextchr))
2693 nextchr = UCHARAT(++locinput);
2696 PL_reg_flags |= RF_tainted;
2701 if (!(OP(scan) == ALPHA
2702 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2704 nextchr = UCHARAT(++locinput);
2707 PL_reg_flags |= RF_tainted;
2712 if (nextchr & 0x80) {
2713 if (!(OP(scan) == ALPHAUTF8
2714 ? swash_fetch(PL_utf8_alpha, (U8*)locinput)
2715 : isALPHA_LC_utf8((U8*)locinput)))
2719 locinput += PL_utf8skip[nextchr];
2720 nextchr = UCHARAT(locinput);
2723 if (!(OP(scan) == ALPHAUTF8
2724 ? isALPHA(nextchr) : isALPHA_LC(nextchr)))
2726 nextchr = UCHARAT(++locinput);
2729 PL_reg_flags |= RF_tainted;
2734 if (OP(scan) == ALPHA
2735 ? isALPHA(nextchr) : isALPHA_LC(nextchr))
2737 nextchr = UCHARAT(++locinput);
2740 PL_reg_flags |= RF_tainted;
2743 if (!nextchr && locinput >= PL_regeol)
2745 if (nextchr & 0x80) {
2746 if (swash_fetch(PL_utf8_alpha,(U8*)locinput))
2748 locinput += PL_utf8skip[nextchr];
2749 nextchr = UCHARAT(locinput);
2752 if (isALPHA(nextchr))
2754 nextchr = UCHARAT(++locinput);
2757 if (!nextchr && locinput >= PL_regeol)
2759 if (!isASCII(nextchr))
2761 nextchr = UCHARAT(++locinput);
2764 if (!nextchr && locinput >= PL_regeol)
2766 if (isASCII(nextchr))
2768 nextchr = UCHARAT(++locinput);
2771 PL_reg_flags |= RF_tainted;
2776 if (!(OP(scan) == CNTRL
2777 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2779 nextchr = UCHARAT(++locinput);
2782 PL_reg_flags |= RF_tainted;
2787 if (nextchr & 0x80) {
2788 if (!(OP(scan) == CNTRLUTF8
2789 ? swash_fetch(PL_utf8_cntrl, (U8*)locinput)
2790 : isCNTRL_LC_utf8((U8*)locinput)))
2794 locinput += PL_utf8skip[nextchr];
2795 nextchr = UCHARAT(locinput);
2798 if (!(OP(scan) == CNTRLUTF8
2799 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr)))
2801 nextchr = UCHARAT(++locinput);
2804 PL_reg_flags |= RF_tainted;
2809 if (OP(scan) == CNTRL
2810 ? isCNTRL(nextchr) : isCNTRL_LC(nextchr))
2812 nextchr = UCHARAT(++locinput);
2815 PL_reg_flags |= RF_tainted;
2818 if (!nextchr && locinput >= PL_regeol)
2820 if (nextchr & 0x80) {
2821 if (swash_fetch(PL_utf8_cntrl,(U8*)locinput))
2823 locinput += PL_utf8skip[nextchr];
2824 nextchr = UCHARAT(locinput);
2827 if (isCNTRL(nextchr))
2829 nextchr = UCHARAT(++locinput);
2832 PL_reg_flags |= RF_tainted;
2837 if (!(OP(scan) == GRAPH
2838 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2840 nextchr = UCHARAT(++locinput);
2843 PL_reg_flags |= RF_tainted;
2848 if (nextchr & 0x80) {
2849 if (!(OP(scan) == GRAPHUTF8
2850 ? swash_fetch(PL_utf8_graph, (U8*)locinput)
2851 : isGRAPH_LC_utf8((U8*)locinput)))
2855 locinput += PL_utf8skip[nextchr];
2856 nextchr = UCHARAT(locinput);
2859 if (!(OP(scan) == GRAPHUTF8
2860 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr)))
2862 nextchr = UCHARAT(++locinput);
2865 PL_reg_flags |= RF_tainted;
2870 if (OP(scan) == GRAPH
2871 ? isGRAPH(nextchr) : isGRAPH_LC(nextchr))
2873 nextchr = UCHARAT(++locinput);
2876 PL_reg_flags |= RF_tainted;
2879 if (!nextchr && locinput >= PL_regeol)
2881 if (nextchr & 0x80) {
2882 if (swash_fetch(PL_utf8_graph,(U8*)locinput))
2884 locinput += PL_utf8skip[nextchr];
2885 nextchr = UCHARAT(locinput);
2888 if (isGRAPH(nextchr))
2890 nextchr = UCHARAT(++locinput);
2893 PL_reg_flags |= RF_tainted;
2898 if (!(OP(scan) == LOWER
2899 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2901 nextchr = UCHARAT(++locinput);
2904 PL_reg_flags |= RF_tainted;
2909 if (nextchr & 0x80) {
2910 if (!(OP(scan) == LOWERUTF8
2911 ? swash_fetch(PL_utf8_lower, (U8*)locinput)
2912 : isLOWER_LC_utf8((U8*)locinput)))
2916 locinput += PL_utf8skip[nextchr];
2917 nextchr = UCHARAT(locinput);
2920 if (!(OP(scan) == LOWERUTF8
2921 ? isLOWER(nextchr) : isLOWER_LC(nextchr)))
2923 nextchr = UCHARAT(++locinput);
2926 PL_reg_flags |= RF_tainted;
2931 if (OP(scan) == LOWER
2932 ? isLOWER(nextchr) : isLOWER_LC(nextchr))
2934 nextchr = UCHARAT(++locinput);
2937 PL_reg_flags |= RF_tainted;
2940 if (!nextchr && locinput >= PL_regeol)
2942 if (nextchr & 0x80) {
2943 if (swash_fetch(PL_utf8_lower,(U8*)locinput))
2945 locinput += PL_utf8skip[nextchr];
2946 nextchr = UCHARAT(locinput);
2949 if (isLOWER(nextchr))
2951 nextchr = UCHARAT(++locinput);
2954 PL_reg_flags |= RF_tainted;
2959 if (!(OP(scan) == PRINT
2960 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2962 nextchr = UCHARAT(++locinput);
2965 PL_reg_flags |= RF_tainted;
2970 if (nextchr & 0x80) {
2971 if (!(OP(scan) == PRINTUTF8
2972 ? swash_fetch(PL_utf8_print, (U8*)locinput)
2973 : isPRINT_LC_utf8((U8*)locinput)))
2977 locinput += PL_utf8skip[nextchr];
2978 nextchr = UCHARAT(locinput);
2981 if (!(OP(scan) == PRINTUTF8
2982 ? isPRINT(nextchr) : isPRINT_LC(nextchr)))
2984 nextchr = UCHARAT(++locinput);
2987 PL_reg_flags |= RF_tainted;
2992 if (OP(scan) == PRINT
2993 ? isPRINT(nextchr) : isPRINT_LC(nextchr))
2995 nextchr = UCHARAT(++locinput);
2998 PL_reg_flags |= RF_tainted;
3001 if (!nextchr && locinput >= PL_regeol)
3003 if (nextchr & 0x80) {
3004 if (swash_fetch(PL_utf8_print,(U8*)locinput))
3006 locinput += PL_utf8skip[nextchr];
3007 nextchr = UCHARAT(locinput);
3010 if (isPRINT(nextchr))
3012 nextchr = UCHARAT(++locinput);
3015 PL_reg_flags |= RF_tainted;
3020 if (!(OP(scan) == PUNCT
3021 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3023 nextchr = UCHARAT(++locinput);
3026 PL_reg_flags |= RF_tainted;
3031 if (nextchr & 0x80) {
3032 if (!(OP(scan) == PUNCTUTF8
3033 ? swash_fetch(PL_utf8_punct, (U8*)locinput)
3034 : isPUNCT_LC_utf8((U8*)locinput)))
3038 locinput += PL_utf8skip[nextchr];
3039 nextchr = UCHARAT(locinput);
3042 if (!(OP(scan) == PUNCTUTF8
3043 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr)))
3045 nextchr = UCHARAT(++locinput);
3048 PL_reg_flags |= RF_tainted;
3053 if (OP(scan) == PUNCT
3054 ? isPUNCT(nextchr) : isPUNCT_LC(nextchr))
3056 nextchr = UCHARAT(++locinput);
3059 PL_reg_flags |= RF_tainted;
3062 if (!nextchr && locinput >= PL_regeol)
3064 if (nextchr & 0x80) {
3065 if (swash_fetch(PL_utf8_punct,(U8*)locinput))
3067 locinput += PL_utf8skip[nextchr];
3068 nextchr = UCHARAT(locinput);
3071 if (isPUNCT(nextchr))
3073 nextchr = UCHARAT(++locinput);
3076 PL_reg_flags |= RF_tainted;
3081 if (!(OP(scan) == UPPER
3082 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3084 nextchr = UCHARAT(++locinput);
3087 PL_reg_flags |= RF_tainted;
3092 if (nextchr & 0x80) {
3093 if (!(OP(scan) == UPPERUTF8
3094 ? swash_fetch(PL_utf8_upper, (U8*)locinput)
3095 : isUPPER_LC_utf8((U8*)locinput)))
3099 locinput += PL_utf8skip[nextchr];
3100 nextchr = UCHARAT(locinput);
3103 if (!(OP(scan) == UPPERUTF8
3104 ? isUPPER(nextchr) : isUPPER_LC(nextchr)))
3106 nextchr = UCHARAT(++locinput);
3109 PL_reg_flags |= RF_tainted;
3114 if (OP(scan) == UPPER
3115 ? isUPPER(nextchr) : isUPPER_LC(nextchr))
3117 nextchr = UCHARAT(++locinput);
3120 PL_reg_flags |= RF_tainted;
3123 if (!nextchr && locinput >= PL_regeol)
3125 if (nextchr & 0x80) {
3126 if (swash_fetch(PL_utf8_upper,(U8*)locinput))
3128 locinput += PL_utf8skip[nextchr];
3129 nextchr = UCHARAT(locinput);
3132 if (isUPPER(nextchr))
3134 nextchr = UCHARAT(++locinput);
3137 if (!nextchr && locinput >= PL_regeol)
3139 if (!isXDIGIT(nextchr))
3141 nextchr = UCHARAT(++locinput);
3144 if (!nextchr && locinput >= PL_regeol)
3146 if (isXDIGIT(nextchr))
3148 nextchr = UCHARAT(++locinput);
3151 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
3153 locinput += PL_utf8skip[nextchr];
3154 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
3155 locinput += UTF8SKIP(locinput);
3156 if (locinput > PL_regeol)
3158 nextchr = UCHARAT(locinput);
3161 PL_reg_flags |= RF_tainted;
3165 n = ARG(scan); /* which paren pair */
3166 ln = PL_regstartp[n];
3167 if (*PL_reglastparen < n || ln == -1)
3168 sayNO; /* Do not match unless seen CLOSEn. */
3169 if (ln == PL_regendp[n])
3173 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
3175 char *e = PL_bostr + PL_regendp[n];
3177 * Note that we can't do the "other character" lookup trick as
3178 * in the 8-bit case (no pun intended) because in Unicode we
3179 * have to map both upper and title case to lower case.
3181 if (OP(scan) == REFF) {
3185 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
3195 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
3202 nextchr = UCHARAT(locinput);
3206 /* Inline the first character, for speed. */
3207 if (UCHARAT(s) != nextchr &&
3209 (UCHARAT(s) != ((OP(scan) == REFF
3210 ? PL_fold : PL_fold_locale)[nextchr]))))
3212 ln = PL_regendp[n] - ln;
3213 if (locinput + ln > PL_regeol)
3215 if (ln > 1 && (OP(scan) == REF
3216 ? memNE(s, locinput, ln)
3218 ? ibcmp(s, locinput, ln)
3219 : ibcmp_locale(s, locinput, ln))))
3222 nextchr = UCHARAT(locinput);
3233 OP_4tree *oop = PL_op;
3234 COP *ocurcop = PL_curcop;
3235 SV **ocurpad = PL_curpad;
3239 PL_op = (OP_4tree*)PL_regdata->data[n];
3240 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
3241 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
3242 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3244 CALLRUNOPS(aTHX); /* Scalar context. */
3250 PL_curpad = ocurpad;
3251 PL_curcop = ocurcop;
3253 if (logical == 2) { /* Postponed subexpression. */
3255 MAGIC *mg = Null(MAGIC*);
3258 CHECKPOINT cp, lastcp;
3260 if(SvROK(ret) || SvRMAGICAL(ret)) {
3261 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
3264 mg = mg_find(sv, 'r');
3267 re = (regexp *)mg->mg_obj;
3268 (void)ReREFCNT_inc(re);
3272 char *t = SvPV(ret, len);
3274 char *oprecomp = PL_regprecomp;
3275 I32 osize = PL_regsize;
3276 I32 onpar = PL_regnpar;
3279 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
3281 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
3282 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
3283 PL_regprecomp = oprecomp;
3288 PerlIO_printf(Perl_debug_log,
3289 "Entering embedded `%s%.60s%s%s'\n",
3293 (strlen(re->precomp) > 60 ? "..." : ""))
3296 state.prev = PL_reg_call_cc;
3297 state.cc = PL_regcc;
3298 state.re = PL_reg_re;
3304 cp = regcppush(0); /* Save *all* the positions. */
3307 state.ss = PL_savestack_ix;
3308 *PL_reglastparen = 0;
3309 PL_reg_call_cc = &state;
3310 PL_reginput = locinput;
3311 if (regmatch(re->program + 1)) {
3317 PerlIO_printf(Perl_debug_log,
3319 REPORT_CODE_OFF+PL_regindent*2, "")
3324 PL_reg_call_cc = state.prev;
3325 PL_regcc = state.cc;
3326 PL_reg_re = state.re;
3327 cache_re(PL_reg_re);
3334 sv_setsv(save_scalar(PL_replgv), ret);
3338 n = ARG(scan); /* which paren pair */
3339 PL_reg_start_tmp[n] = locinput;
3344 n = ARG(scan); /* which paren pair */
3345 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3346 PL_regendp[n] = locinput - PL_bostr;
3347 if (n > *PL_reglastparen)
3348 *PL_reglastparen = n;
3351 n = ARG(scan); /* which paren pair */
3352 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
3356 next = NEXTOPER(NEXTOPER(scan));
3358 next = scan + ARG(scan);
3359 if (OP(next) == IFTHEN) /* Fake one. */
3360 next = NEXTOPER(NEXTOPER(next));
3364 logical = scan->flags;
3368 CHECKPOINT cp = PL_savestack_ix;
3370 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3372 cc.oldcc = PL_regcc;
3374 cc.parenfloor = *PL_reglastparen;
3376 cc.min = ARG1(scan);
3377 cc.max = ARG2(scan);
3378 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3382 PL_reginput = locinput;
3383 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3385 PL_regcc = cc.oldcc;
3391 * This is really hard to understand, because after we match
3392 * what we're trying to match, we must make sure the rest of
3393 * the RE is going to match for sure, and to do that we have
3394 * to go back UP the parse tree by recursing ever deeper. And
3395 * if it fails, we have to reset our parent's current state
3396 * that we can try again after backing off.
3399 CHECKPOINT cp, lastcp;
3400 CURCUR* cc = PL_regcc;
3401 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3403 n = cc->cur + 1; /* how many we know we matched */
3404 PL_reginput = locinput;
3407 PerlIO_printf(Perl_debug_log,
3408 "%*s %ld out of %ld..%ld cc=%lx\n",
3409 REPORT_CODE_OFF+PL_regindent*2, "",
3410 (long)n, (long)cc->min,
3411 (long)cc->max, (long)cc)
3414 /* If degenerate scan matches "", assume scan done. */
3416 if (locinput == cc->lastloc && n >= cc->min) {
3417 PL_regcc = cc->oldcc;
3420 PerlIO_printf(Perl_debug_log,
3421 "%*s empty match detected, try continuation...\n",
3422 REPORT_CODE_OFF+PL_regindent*2, "")
3424 if (regmatch(cc->next))
3427 PerlIO_printf(Perl_debug_log,
3429 REPORT_CODE_OFF+PL_regindent*2, "")
3436 /* First just match a string of min scans. */
3440 cc->lastloc = locinput;
3441 if (regmatch(cc->scan))
3444 cc->lastloc = lastloc;
3446 PerlIO_printf(Perl_debug_log,
3448 REPORT_CODE_OFF+PL_regindent*2, "")
3453 /* Prefer next over scan for minimal matching. */
3456 PL_regcc = cc->oldcc;
3458 cp = regcppush(cc->parenfloor);
3460 if (regmatch(cc->next)) {
3462 sayYES; /* All done. */
3469 if (n >= cc->max) { /* Maximum greed exceeded? */
3470 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3471 && !(PL_reg_flags & RF_warned)) {
3472 PL_reg_flags |= RF_warned;
3473 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3474 "Complex regular subexpression recursion",
3481 PerlIO_printf(Perl_debug_log,
3482 "%*s trying longer...\n",
3483 REPORT_CODE_OFF+PL_regindent*2, "")
3485 /* Try scanning more and see if it helps. */
3486 PL_reginput = locinput;
3488 cc->lastloc = locinput;
3489 cp = regcppush(cc->parenfloor);
3491 if (regmatch(cc->scan)) {
3496 PerlIO_printf(Perl_debug_log,
3498 REPORT_CODE_OFF+PL_regindent*2, "")
3503 cc->lastloc = lastloc;
3507 /* Prefer scan over next for maximal matching. */
3509 if (n < cc->max) { /* More greed allowed? */
3510 cp = regcppush(cc->parenfloor);
3512 cc->lastloc = locinput;
3514 if (regmatch(cc->scan)) {
3519 regcppop(); /* Restore some previous $<digit>s? */
3520 PL_reginput = locinput;
3522 PerlIO_printf(Perl_debug_log,
3523 "%*s failed, try continuation...\n",
3524 REPORT_CODE_OFF+PL_regindent*2, "")
3527 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3528 && !(PL_reg_flags & RF_warned)) {
3529 PL_reg_flags |= RF_warned;
3530 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
3531 "Complex regular subexpression recursion",
3535 /* Failed deeper matches of scan, so see if this one works. */
3536 PL_regcc = cc->oldcc;
3538 if (regmatch(cc->next))
3541 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
3542 REPORT_CODE_OFF+PL_regindent*2, "")
3547 cc->lastloc = lastloc;
3552 next = scan + ARG(scan);
3555 inner = NEXTOPER(NEXTOPER(scan));
3558 inner = NEXTOPER(scan);
3563 if (OP(next) != c1) /* No choice. */
3564 next = inner; /* Avoid recursion. */
3566 int lastparen = *PL_reglastparen;
3570 PL_reginput = locinput;
3571 if (regmatch(inner))
3574 for (n = *PL_reglastparen; n > lastparen; n--)
3576 *PL_reglastparen = n;
3579 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
3583 inner = NEXTOPER(scan);
3584 if (c1 == BRANCHJ) {
3585 inner = NEXTOPER(inner);
3587 } while (scan != NULL && OP(scan) == c1);
3601 /* We suppose that the next guy does not need
3602 backtracking: in particular, it is of constant length,
3603 and has no parenths to influence future backrefs. */
3604 ln = ARG1(scan); /* min to match */
3605 n = ARG2(scan); /* max to match */
3606 paren = scan->flags;
3608 if (paren > PL_regsize)
3610 if (paren > *PL_reglastparen)
3611 *PL_reglastparen = paren;
3613 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3615 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3616 PL_reginput = locinput;
3619 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3621 if (ln && l == 0 && n >= ln
3622 /* In fact, this is tricky. If paren, then the
3623 fact that we did/didnot match may influence
3624 future execution. */
3625 && !(paren && ln == 0))
3627 locinput = PL_reginput;
3628 if (PL_regkind[(U8)OP(next)] == EXACT) {
3629 c1 = UCHARAT(OPERAND(next) + 1);
3630 if (OP(next) == EXACTF)
3632 else if (OP(next) == EXACTFL)
3633 c2 = PL_fold_locale[c1];
3640 /* This may be improved if l == 0. */
3641 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3642 /* If it could work, try it. */
3644 UCHARAT(PL_reginput) == c1 ||
3645 UCHARAT(PL_reginput) == c2)
3649 PL_regstartp[paren] =
3650 HOPc(PL_reginput, -l) - PL_bostr;
3651 PL_regendp[paren] = PL_reginput - PL_bostr;
3654 PL_regendp[paren] = -1;
3660 /* Couldn't or didn't -- move forward. */
3661 PL_reginput = locinput;
3662 if (regrepeat_hard(scan, 1, &l)) {
3664 locinput = PL_reginput;
3671 n = regrepeat_hard(scan, n, &l);
3672 if (n != 0 && l == 0
3673 /* In fact, this is tricky. If paren, then the
3674 fact that we did/didnot match may influence
3675 future execution. */
3676 && !(paren && ln == 0))
3678 locinput = PL_reginput;
3680 PerlIO_printf(Perl_debug_log,
3681 "%*s matched %ld times, len=%ld...\n",
3682 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
3685 if (PL_regkind[(U8)OP(next)] == EXACT) {
3686 c1 = UCHARAT(OPERAND(next) + 1);
3687 if (OP(next) == EXACTF)
3689 else if (OP(next) == EXACTFL)
3690 c2 = PL_fold_locale[c1];
3699 /* If it could work, try it. */
3701 UCHARAT(PL_reginput) == c1 ||
3702 UCHARAT(PL_reginput) == c2)
3705 PerlIO_printf(Perl_debug_log,
3706 "%*s trying tail with n=%ld...\n",
3707 REPORT_CODE_OFF+PL_regindent*2, "", n)
3711 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3712 PL_regendp[paren] = PL_reginput - PL_bostr;
3715 PL_regendp[paren] = -1;
3721 /* Couldn't or didn't -- back up. */
3723 locinput = HOPc(locinput, -l);
3724 PL_reginput = locinput;
3731 paren = scan->flags; /* Which paren to set */
3732 if (paren > PL_regsize)
3734 if (paren > *PL_reglastparen)
3735 *PL_reglastparen = paren;
3736 ln = ARG1(scan); /* min to match */
3737 n = ARG2(scan); /* max to match */
3738 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3742 ln = ARG1(scan); /* min to match */
3743 n = ARG2(scan); /* max to match */
3744 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3749 scan = NEXTOPER(scan);
3755 scan = NEXTOPER(scan);
3759 * Lookahead to avoid useless match attempts
3760 * when we know what character comes next.
3762 if (PL_regkind[(U8)OP(next)] == EXACT) {
3763 c1 = UCHARAT(OPERAND(next) + 1);
3764 if (OP(next) == EXACTF)
3766 else if (OP(next) == EXACTFL)
3767 c2 = PL_fold_locale[c1];
3773 PL_reginput = locinput;
3777 if (ln && regrepeat(scan, ln) < ln)
3779 locinput = PL_reginput;
3782 char *e = locinput + n - ln; /* Should not check after this */
3783 char *old = locinput;
3785 if (e >= PL_regeol || (n == REG_INFTY))
3788 /* Find place 'next' could work */
3790 while (locinput <= e && *locinput != c1)
3793 while (locinput <= e
3800 /* PL_reginput == old now */
3801 if (locinput != old) {
3802 ln = 1; /* Did some */
3803 if (regrepeat(scan, locinput - old) <
3807 /* PL_reginput == locinput now */
3810 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
3811 PL_regendp[paren] = locinput - PL_bostr;
3814 PL_regendp[paren] = -1;
3818 PL_reginput = locinput; /* Could be reset... */
3820 /* Couldn't or didn't -- move forward. */
3825 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3826 /* If it could work, try it. */
3828 UCHARAT(PL_reginput) == c1 ||
3829 UCHARAT(PL_reginput) == c2)
3833 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3834 PL_regendp[paren] = PL_reginput - PL_bostr;
3837 PL_regendp[paren] = -1;
3843 /* Couldn't or didn't -- move forward. */
3844 PL_reginput = locinput;
3845 if (regrepeat(scan, 1)) {
3847 locinput = PL_reginput;
3855 n = regrepeat(scan, n);
3856 locinput = PL_reginput;
3857 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3858 (!PL_multiline || OP(next) == SEOL))
3859 ln = n; /* why back off? */
3863 /* If it could work, try it. */
3865 UCHARAT(PL_reginput) == c1 ||
3866 UCHARAT(PL_reginput) == c2)
3870 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
3871 PL_regendp[paren] = PL_reginput - PL_bostr;
3874 PL_regendp[paren] = -1;
3880 /* Couldn't or didn't -- back up. */
3882 PL_reginput = locinput = HOPc(locinput, -1);
3887 /* If it could work, try it. */
3889 UCHARAT(PL_reginput) == c1 ||
3890 UCHARAT(PL_reginput) == c2)
3896 /* Couldn't or didn't -- back up. */
3898 PL_reginput = locinput = HOPc(locinput, -1);
3905 if (PL_reg_call_cc) {
3906 re_cc_state *cur_call_cc = PL_reg_call_cc;
3907 CURCUR *cctmp = PL_regcc;
3908 regexp *re = PL_reg_re;
3909 CHECKPOINT cp, lastcp;
3911 cp = regcppush(0); /* Save *all* the positions. */
3913 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3915 PL_reginput = locinput; /* Make position available to
3917 cache_re(PL_reg_call_cc->re);
3918 PL_regcc = PL_reg_call_cc->cc;
3919 PL_reg_call_cc = PL_reg_call_cc->prev;
3920 if (regmatch(cur_call_cc->node)) {
3921 PL_reg_call_cc = cur_call_cc;
3927 PL_reg_call_cc = cur_call_cc;
3933 PerlIO_printf(Perl_debug_log,
3934 "%*s continuation failed...\n",
3935 REPORT_CODE_OFF+PL_regindent*2, "")
3939 if (locinput < PL_regtill)
3940 sayNO; /* Cannot match: too short. */
3943 PL_reginput = locinput; /* put where regtry can find it */
3944 sayYES; /* Success! */
3947 PL_reginput = locinput;
3952 if (UTF) { /* XXXX This is absolutely
3953 broken, we read before
3955 s = HOPMAYBEc(locinput, -scan->flags);
3961 if (locinput < PL_bostr + scan->flags)
3963 PL_reginput = locinput - scan->flags;
3968 PL_reginput = locinput;
3973 if (UTF) { /* XXXX This is absolutely
3974 broken, we read before
3976 s = HOPMAYBEc(locinput, -scan->flags);
3977 if (!s || s < PL_bostr)
3982 if (locinput < PL_bostr + scan->flags)
3984 PL_reginput = locinput - scan->flags;
3989 PL_reginput = locinput;
3992 inner = NEXTOPER(NEXTOPER(scan));
3993 if (regmatch(inner) != n) {
4008 if (OP(scan) == SUSPEND) {
4009 locinput = PL_reginput;
4010 nextchr = UCHARAT(locinput);
4015 next = scan + ARG(scan);
4020 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
4021 (unsigned long)scan, OP(scan));
4022 Perl_croak(aTHX_ "regexp memory corruption");
4028 * We get here only if there's trouble -- normally "case END" is
4029 * the terminating point.
4031 Perl_croak(aTHX_ "corrupted regexp pointers");
4049 - regrepeat - repeatedly match something simple, report how many
4052 * [This routine now assumes that it will only match on things of length 1.
4053 * That was true before, but now we assume scan - reginput is the count,
4054 * rather than incrementing count on every character. [Er, except utf8.]]
4057 S_regrepeat(pTHX_ regnode *p, I32 max)
4060 register char *scan;
4061 register char *opnd;
4063 register char *loceol = PL_regeol;
4064 register I32 hardcount = 0;
4067 if (max != REG_INFTY && max < loceol - scan)
4068 loceol = scan + max;
4069 opnd = (char *) OPERAND(p);
4072 while (scan < loceol && *scan != '\n')
4080 while (scan < loceol && *scan != '\n') {
4081 scan += UTF8SKIP(scan);
4087 while (scan < loceol) {
4088 scan += UTF8SKIP(scan);
4092 case EXACT: /* length of string is 1 */
4093 c = UCHARAT(++opnd);
4094 while (scan < loceol && UCHARAT(scan) == c)
4097 case EXACTF: /* length of string is 1 */
4098 c = UCHARAT(++opnd);
4099 while (scan < loceol &&
4100 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4103 case EXACTFL: /* length of string is 1 */
4104 PL_reg_flags |= RF_tainted;
4105 c = UCHARAT(++opnd);
4106 while (scan < loceol &&
4107 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4112 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
4113 scan += UTF8SKIP(scan);
4118 while (scan < loceol && REGINCLASS(opnd, *scan))
4122 while (scan < loceol && isALNUM(*scan))
4127 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4128 scan += UTF8SKIP(scan);
4133 PL_reg_flags |= RF_tainted;
4134 while (scan < loceol && isALNUM_LC(*scan))
4138 PL_reg_flags |= RF_tainted;
4140 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
4141 scan += UTF8SKIP(scan);
4147 while (scan < loceol && !isALNUM(*scan))
4152 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
4153 scan += UTF8SKIP(scan);
4158 PL_reg_flags |= RF_tainted;
4159 while (scan < loceol && !isALNUM_LC(*scan))
4163 PL_reg_flags |= RF_tainted;
4165 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
4166 scan += UTF8SKIP(scan);
4171 while (scan < loceol && isSPACE(*scan))
4176 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4177 scan += UTF8SKIP(scan);
4182 PL_reg_flags |= RF_tainted;
4183 while (scan < loceol && isSPACE_LC(*scan))
4187 PL_reg_flags |= RF_tainted;
4189 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4190 scan += UTF8SKIP(scan);
4195 while (scan < loceol && !isSPACE(*scan))
4200 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
4201 scan += UTF8SKIP(scan);
4206 PL_reg_flags |= RF_tainted;
4207 while (scan < loceol && !isSPACE_LC(*scan))
4211 PL_reg_flags |= RF_tainted;
4213 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4214 scan += UTF8SKIP(scan);
4219 while (scan < loceol && isDIGIT(*scan))
4224 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
4225 scan += UTF8SKIP(scan);
4231 while (scan < loceol && !isDIGIT(*scan))
4236 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
4237 scan += UTF8SKIP(scan);
4241 default: /* Called on something of 0 width. */
4242 break; /* So match right here or not at all. */
4248 c = scan - PL_reginput;
4253 SV *prop = sv_newmortal();
4256 PerlIO_printf(Perl_debug_log,
4257 "%*s %s can match %ld times out of %ld...\n",
4258 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
4265 - regrepeat_hard - repeatedly match something, report total lenth and length
4267 * The repeater is supposed to have constant length.
4271 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4274 register char *scan;
4275 register char *start;
4276 register char *loceol = PL_regeol;
4278 I32 count = 0, res = 1;
4283 start = PL_reginput;
4285 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4288 while (start < PL_reginput) {
4290 start += UTF8SKIP(start);
4301 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4303 *lp = l = PL_reginput - start;
4304 if (max != REG_INFTY && l*max < loceol - scan)
4305 loceol = scan + l*max;
4318 - reginclass - determine if a character falls into a character class
4322 S_reginclass(pTHX_ register char *p, register I32 c)
4325 char flags = ANYOF_FLAGS(p);
4329 if (ANYOF_BITMAP_TEST(p, c))
4331 else if (flags & ANYOF_FOLD) {
4333 if (flags & ANYOF_LOCALE) {
4334 PL_reg_flags |= RF_tainted;
4335 cf = PL_fold_locale[c];
4339 if (ANYOF_BITMAP_TEST(p, cf))
4343 if (!match && (flags & ANYOF_CLASS)) {
4344 PL_reg_flags |= RF_tainted;
4346 (ANYOF_CLASS_TEST(p, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4347 (ANYOF_CLASS_TEST(p, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4348 (ANYOF_CLASS_TEST(p, ANYOF_SPACE) && isSPACE_LC(c)) ||
4349 (ANYOF_CLASS_TEST(p, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4350 (ANYOF_CLASS_TEST(p, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4351 (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4352 (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4353 (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4354 (ANYOF_CLASS_TEST(p, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4355 (ANYOF_CLASS_TEST(p, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4356 (ANYOF_CLASS_TEST(p, ANYOF_ASCII) && isASCII(c)) ||
4357 (ANYOF_CLASS_TEST(p, ANYOF_NASCII) && !isASCII(c)) ||
4358 (ANYOF_CLASS_TEST(p, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4359 (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4360 (ANYOF_CLASS_TEST(p, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4361 (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4362 (ANYOF_CLASS_TEST(p, ANYOF_LOWER) && isLOWER_LC(c)) ||
4363 (ANYOF_CLASS_TEST(p, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4364 (ANYOF_CLASS_TEST(p, ANYOF_PRINT) && isPRINT_LC(c)) ||
4365 (ANYOF_CLASS_TEST(p, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4366 (ANYOF_CLASS_TEST(p, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4367 (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4368 (ANYOF_CLASS_TEST(p, ANYOF_UPPER) && isUPPER_LC(c)) ||
4369 (ANYOF_CLASS_TEST(p, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4370 (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4371 (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))
4372 ) /* How's that for a conditional? */
4378 return (flags & ANYOF_INVERT) ? !match : match;
4382 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
4385 char flags = ARG1(f);
4387 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
4389 if (swash_fetch(sv, p))
4391 else if (flags & ANYOF_FOLD) {
4394 if (flags & ANYOF_LOCALE) {
4395 PL_reg_flags |= RF_tainted;
4396 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
4399 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
4400 if (swash_fetch(sv, tmpbuf))
4404 /* UTF8 combined with ANYOF_CLASS is ill-defined. */
4406 return (flags & ANYOF_INVERT) ? !match : match;
4410 S_reghop(pTHX_ U8 *s, I32 off)
4414 while (off-- && s < (U8*)PL_regeol)
4419 if (s > (U8*)PL_bostr) {
4422 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4424 } /* XXX could check well-formedness here */
4432 S_reghopmaybe(pTHX_ U8* s, I32 off)
4436 while (off-- && s < (U8*)PL_regeol)
4443 if (s > (U8*)PL_bostr) {
4446 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
4448 } /* XXX could check well-formedness here */