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 */
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 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregexec my_pregexec
40 # define Perl_reginitcolors my_reginitcolors
45 * pregcomp and pregexec -- regsub and regerror are not used in perl
47 * Copyright (c) 1986 by University of Toronto.
48 * Written by Henry Spencer. Not derived from licensed software.
50 * Permission is granted to anyone to use this software for any
51 * purpose on any computer system, and to redistribute it freely,
52 * subject to the following restrictions:
54 * 1. The author is not responsible for the consequences of use of
55 * this software, no matter how awful, even if they arise
58 * 2. The origin of this software must not be misrepresented, either
59 * by explicit claim or by omission.
61 * 3. Altered versions must be plainly marked as such, and must not
62 * be misrepresented as being the original software.
64 **** Alterations to Henry's code are...
66 **** Copyright (c) 1991-1999, Larry Wall
68 **** You may distribute under the terms of either the GNU General Public
69 **** License or the Artistic License, as specified in the README file.
71 * Beware that some of this code is subtly aware of the way operator
72 * precedence is structured in regular expressions. Serious changes in
73 * regular-expression syntax might require a total rethink.
76 #define PERL_IN_REGEXEC_C
81 #define RF_tainted 1 /* tainted information used? */
82 #define RF_warned 2 /* warned about big count? */
83 #define RF_evaled 4 /* Did an EVAL with setting? */
84 #define RF_utf8 8 /* String contains multibyte chars? */
86 #define UTF (PL_reg_flags & RF_utf8)
88 #define RS_init 1 /* eval environment created */
89 #define RS_set 2 /* replsv value is set */
99 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
100 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
102 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
103 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
105 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
106 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
107 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
108 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
109 #define HOPc(pos,off) ((char*)HOP(pos,off))
110 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
113 S_regcppush(pTHX_ I32 parenfloor)
116 int retval = PL_savestack_ix;
117 int i = (PL_regsize - parenfloor) * 4;
121 for (p = PL_regsize; p > parenfloor; p--) {
122 SSPUSHINT(PL_regendp[p]);
123 SSPUSHINT(PL_regstartp[p]);
124 SSPUSHPTR(PL_reg_start_tmp[p]);
127 SSPUSHINT(PL_regsize);
128 SSPUSHINT(*PL_reglastparen);
129 SSPUSHPTR(PL_reginput);
131 SSPUSHINT(SAVEt_REGCONTEXT);
135 /* These are needed since we do not localize EVAL nodes: */
136 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
137 " Setting an EVAL scope, savestack=%i\n", \
138 PL_savestack_ix)); lastcp = PL_savestack_ix
140 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
141 PerlIO_printf(Perl_debug_log, \
142 " Clearing an EVAL scope, savestack=%i..%i\n", \
143 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
153 assert(i == SAVEt_REGCONTEXT);
155 input = (char *) SSPOPPTR;
156 *PL_reglastparen = SSPOPINT;
157 PL_regsize = SSPOPINT;
158 for (i -= 3; i > 0; i -= 4) {
159 paren = (U32)SSPOPINT;
160 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
161 PL_regstartp[paren] = SSPOPINT;
163 if (paren <= *PL_reglastparen)
164 PL_regendp[paren] = tmps;
166 PerlIO_printf(Perl_debug_log,
167 " restoring \\%d to %d(%d)..%d%s\n",
168 paren, PL_regstartp[paren],
169 PL_reg_start_tmp[paren] - PL_bostr,
171 (paren > *PL_reglastparen ? "(no)" : ""));
175 if (*PL_reglastparen + 1 <= PL_regnpar) {
176 PerlIO_printf(Perl_debug_log,
177 " restoring \\%d..\\%d to undef\n",
178 *PL_reglastparen + 1, PL_regnpar);
181 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
182 if (paren > PL_regsize)
183 PL_regstartp[paren] = -1;
184 PL_regendp[paren] = -1;
190 S_regcp_set_to(pTHX_ I32 ss)
193 I32 tmp = PL_savestack_ix;
195 PL_savestack_ix = ss;
197 PL_savestack_ix = tmp;
201 typedef struct re_cc_state
205 struct re_cc_state *prev;
210 #define regcpblow(cp) LEAVE_SCOPE(cp)
213 * pregexec and friends
217 - pregexec - match a regexp against a string
220 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
221 char *strbeg, I32 minend, SV *screamer, U32 nosave)
222 /* strend: pointer to null at end of string */
223 /* strbeg: real beginning of string */
224 /* minend: end of match must be >=minend after stringarg. */
225 /* nosave: For optimizations. */
228 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
229 nosave ? 0 : REXEC_COPY_STR);
233 S_cache_re(pTHX_ regexp *prog)
236 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
238 PL_regprogram = prog->program;
240 PL_regnpar = prog->nparens;
241 PL_regdata = prog->data;
246 S_restore_pos(pTHX_ void *arg)
249 if (PL_reg_eval_set) {
250 if (PL_reg_oldsaved) {
251 PL_reg_re->subbeg = PL_reg_oldsaved;
252 PL_reg_re->sublen = PL_reg_oldsavedlen;
253 RX_MATCH_COPIED_on(PL_reg_re);
255 PL_reg_magic->mg_len = PL_reg_oldpos;
257 PL_curpm = PL_reg_oldcurpm;
263 - regexec_flags - match a regexp against a string
266 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
267 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
268 /* strend: pointer to null at end of string */
269 /* strbeg: real beginning of string */
270 /* minend: end of match must be >=minend after stringarg. */
271 /* data: May be used for some additional optimizations. */
272 /* nosave: For optimizations. */
277 register char *startpos = stringarg;
279 I32 minlen; /* must match at least this many chars */
280 I32 dontbother = 0; /* how many characters not to try at end */
282 I32 start_shift = 0; /* Offset of the start to find
283 constant substr. */ /* CC */
284 I32 end_shift = 0; /* Same for the end. */ /* CC */
285 I32 scream_pos = -1; /* Internal iterator of scream. */
287 SV* oreplsv = GvSV(PL_replgv);
295 PL_regnarrate = PL_debug & 512;
299 if (prog == NULL || startpos == NULL) {
300 Perl_croak(aTHX_ "NULL regexp parameter");
304 minlen = prog->minlen;
305 if (strend - startpos < minlen) goto phooey;
307 if (startpos == strbeg) /* is ^ valid at stringarg? */
310 PL_regprev = (U32)stringarg[-1];
311 if (!PL_multiline && PL_regprev == '\n')
312 PL_regprev = '\0'; /* force ^ to NOT match */
315 /* Check validity of program. */
316 if (UCHARAT(prog->program) != REG_MAGIC) {
317 Perl_croak(aTHX_ "corrupted regexp program");
323 if (prog->reganch & ROPT_UTF8)
324 PL_reg_flags |= RF_utf8;
326 /* Mark beginning of line for ^ and lookbehind. */
327 PL_regbol = startpos;
331 /* Mark end of line for $ (and such) */
334 /* see how far we have to get to not match where we matched before */
335 PL_regtill = startpos+minend;
337 /* We start without call_cc context. */
340 /* If there is a "must appear" string, look for it. */
342 if (!(flags & REXEC_CHECKED)
343 && prog->check_substr != Nullsv &&
344 !(prog->reganch & ROPT_ANCH_GPOS) &&
345 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
346 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
349 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
350 /* Should be nonnegative! */
351 end_shift = minlen - start_shift -
352 CHR_SVLEN(prog->check_substr) + (SvTAIL(prog->check_substr) != 0);
353 if (flags & REXEC_SCREAM) {
354 SV *c = prog->check_substr;
356 if (PL_screamfirst[BmRARE(c)] >= 0
357 || ( BmRARE(c) == '\n'
358 && (BmPREVIOUS(c) == SvCUR(c) - 1)
360 s = screaminstr(sv, prog->check_substr,
361 start_shift + (stringarg - strbeg),
362 end_shift, &scream_pos, 0);
368 s = fbm_instr((unsigned char*)s + start_shift,
369 (unsigned char*)strend - end_shift,
370 prog->check_substr, PL_multiline ? FBMrf_MULTILINE : 0);
372 ++BmUSEFUL(prog->check_substr); /* hooray */
373 goto phooey; /* not present */
375 else if (s - stringarg > prog->check_offset_max &&
377 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
378 : (t = s - prog->check_offset_max) != 0
382 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
385 else if (!(prog->reganch & ROPT_NAUGHTY)
386 && --BmUSEFUL(prog->check_substr) < 0
387 && prog->check_substr == prog->float_substr) { /* boo */
388 SvREFCNT_dec(prog->check_substr);
389 prog->check_substr = Nullsv; /* disable */
390 prog->float_substr = Nullsv; /* clear */
397 DEBUG_r(if (!PL_colorset) reginitcolors());
398 DEBUG_r(PerlIO_printf(Perl_debug_log,
399 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
400 PL_colors[4],PL_colors[5],PL_colors[0],
403 (strlen(prog->precomp) > 60 ? "..." : ""),
405 (strend - startpos > 60 ? 60 : strend - startpos),
406 startpos, PL_colors[1],
407 (strend - startpos > 60 ? "..." : ""))
410 if (prog->reganch & ROPT_GPOS_SEEN) {
413 if (!(flags & REXEC_IGNOREPOS) && sv && SvTYPE(sv) >= SVt_PVMG
414 && SvMAGIC(sv) && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
415 PL_reg_ganch = strbeg + mg->mg_len;
417 PL_reg_ganch = startpos;
420 /* Simplest case: anchored match need be tried only once. */
421 /* [unless only anchor is BOL and multiline is set] */
422 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
423 if (regtry(prog, startpos))
425 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
426 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
429 dontbother = minlen - 1;
430 strend = HOPc(strend, -dontbother);
431 /* for multiline we only have to try after newlines */
435 if (*s++ == '\n') { /* don't need PL_utf8skip here */
436 if (s < strend && regtry(prog, s))
442 } else if (prog->reganch & ROPT_ANCH_GPOS) {
443 if (regtry(prog, PL_reg_ganch))
448 /* Messy cases: unanchored match. */
449 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
450 /* we have /x+whatever/ */
451 /* it must be a one character string */
452 char ch = SvPVX(prog->anchored_substr)[0];
456 if (regtry(prog, s)) goto got_it;
458 while (s < strend && *s == ch)
467 if (regtry(prog, s)) goto got_it;
469 while (s < strend && *s == ch)
477 else if (prog->anchored_substr != Nullsv
478 || (prog->float_substr != Nullsv
479 && prog->float_max_offset < strend - s)) {
480 SV *must = prog->anchored_substr
481 ? prog->anchored_substr : prog->float_substr;
483 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
485 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
486 I32 delta = back_max - back_min;
487 char *last = HOPc(strend, /* Cannot start after this */
488 -(I32)(CHR_SVLEN(must)
489 - (SvTAIL(must) != 0) + back_min));
490 char *last1; /* Last position checked before */
495 last1 = s - 1; /* bogus */
497 /* XXXX check_substr already used to find `s', can optimize if
498 check_substr==must. */
500 dontbother = end_shift;
501 strend = HOPc(strend, -dontbother);
502 while ( (s <= last) &&
503 ((flags & REXEC_SCREAM)
504 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
505 end_shift, &scream_pos, 0))
506 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
507 (unsigned char*)strend, must,
508 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
509 if (HOPc(s, -back_max) > last1) {
510 last1 = HOPc(s, -back_min);
511 s = HOPc(s, -back_max);
514 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
516 last1 = HOPc(s, -back_min);
536 else if (c = prog->regstclass) {
537 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
541 dontbother = minlen - 1;
542 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
544 /* We know what class it must start with. */
547 cc = (char *) OPERAND(c);
549 if (REGINCLASSUTF8(c, (U8*)s)) {
550 if (tmp && regtry(prog, s))
561 cc = (char *) OPERAND(c);
563 if (REGINCLASS(cc, *s)) {
564 if (tmp && regtry(prog, s))
575 PL_reg_flags |= RF_tainted;
582 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
583 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
585 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
592 if ((minlen || tmp) && regtry(prog,s))
596 PL_reg_flags |= RF_tainted;
601 strend = reghop_c(strend, -1);
603 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
604 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
606 if (tmp == !(OP(c) == BOUND ?
607 swash_fetch(PL_utf8_alnum, (U8*)s) :
608 isALNUM_LC_utf8((U8*)s)))
616 if ((minlen || tmp) && regtry(prog,s))
620 PL_reg_flags |= RF_tainted;
627 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
628 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
630 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
632 else if (regtry(prog, s))
636 if ((minlen || !tmp) && regtry(prog,s))
640 PL_reg_flags |= RF_tainted;
645 strend = reghop_c(strend, -1);
647 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
648 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
650 if (tmp == !(OP(c) == NBOUND ?
651 swash_fetch(PL_utf8_alnum, (U8*)s) :
652 isALNUM_LC_utf8((U8*)s)))
654 else if (regtry(prog, s))
658 if ((minlen || !tmp) && regtry(prog,s))
664 if (tmp && regtry(prog, s))
676 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
677 if (tmp && regtry(prog, s))
688 PL_reg_flags |= RF_tainted;
690 if (isALNUM_LC(*s)) {
691 if (tmp && regtry(prog, s))
702 PL_reg_flags |= RF_tainted;
704 if (isALNUM_LC_utf8((U8*)s)) {
705 if (tmp && regtry(prog, s))
718 if (tmp && regtry(prog, s))
730 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
731 if (tmp && regtry(prog, s))
742 PL_reg_flags |= RF_tainted;
744 if (!isALNUM_LC(*s)) {
745 if (tmp && regtry(prog, s))
756 PL_reg_flags |= RF_tainted;
758 if (!isALNUM_LC_utf8((U8*)s)) {
759 if (tmp && regtry(prog, s))
772 if (tmp && regtry(prog, s))
784 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
785 if (tmp && regtry(prog, s))
796 PL_reg_flags |= RF_tainted;
798 if (isSPACE_LC(*s)) {
799 if (tmp && regtry(prog, s))
810 PL_reg_flags |= RF_tainted;
812 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
813 if (tmp && regtry(prog, s))
826 if (tmp && regtry(prog, s))
838 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
839 if (tmp && regtry(prog, s))
850 PL_reg_flags |= RF_tainted;
852 if (!isSPACE_LC(*s)) {
853 if (tmp && regtry(prog, s))
864 PL_reg_flags |= RF_tainted;
866 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
867 if (tmp && regtry(prog, s))
880 if (tmp && regtry(prog, s))
892 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
893 if (tmp && regtry(prog, s))
906 if (tmp && regtry(prog, s))
918 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
919 if (tmp && regtry(prog, s))
933 if (prog->float_substr != Nullsv) { /* Trim the end. */
935 I32 oldpos = scream_pos;
937 if (flags & REXEC_SCREAM) {
938 last = screaminstr(sv, prog->float_substr, s - strbeg,
939 end_shift, &scream_pos, 1); /* last one */
941 last = scream_olds; /* Only one occurence. */
945 char *little = SvPV(prog->float_substr, len);
947 if (SvTAIL(prog->float_substr)) {
948 if (memEQ(strend - len + 1, little, len - 1))
949 last = strend - len + 1;
950 else if (!PL_multiline)
951 last = memEQ(strend - len, little, len)
952 ? strend - len : Nullch;
958 last = rninstr(s, strend, little, little + len);
960 last = strend; /* matching `$' */
963 if (last == NULL) goto phooey; /* Should not happen! */
964 dontbother = strend - last + prog->float_min_offset;
966 if (minlen && (dontbother < minlen))
967 dontbother = minlen - 1;
968 strend -= dontbother; /* this one's always in bytes! */
969 /* We don't know much -- general case. */
983 } while (s++ < strend);
991 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
993 if (PL_reg_eval_set) {
994 /* Preserve the current value of $^R */
995 if (oreplsv != GvSV(PL_replgv))
996 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
997 restored, the value remains
1002 /* make sure $`, $&, $', and $digit will work later */
1003 if ( !(flags & REXEC_NOT_FIRST) ) {
1004 if (RX_MATCH_COPIED(prog)) {
1005 Safefree(prog->subbeg);
1006 RX_MATCH_COPIED_off(prog);
1008 if (flags & REXEC_COPY_STR) {
1009 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1011 s = savepvn(strbeg, i);
1014 RX_MATCH_COPIED_on(prog);
1017 prog->subbeg = strbeg;
1018 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1025 if (PL_reg_eval_set)
1031 - regtry - try match at specific point
1033 STATIC I32 /* 0 failure, 1 success */
1034 S_regtry(pTHX_ regexp *prog, char *startpos)
1042 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1045 PL_reg_eval_set = RS_init;
1047 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1048 PL_stack_sp - PL_stack_base);
1050 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1051 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1052 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1054 /* Apparently this is not needed, judging by wantarray. */
1055 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1056 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1059 /* Make $_ available to executed code. */
1060 if (PL_reg_sv != DEFSV) {
1061 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1066 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1067 && (mg = mg_find(PL_reg_sv, 'g')))) {
1068 /* prepare for quick setting of pos */
1069 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1070 mg = mg_find(PL_reg_sv, 'g');
1074 PL_reg_oldpos = mg->mg_len;
1075 SAVEDESTRUCTOR(S_restore_pos, 0);
1078 New(22,PL_reg_curpm, 1, PMOP);
1079 PL_reg_curpm->op_pmregexp = prog;
1080 PL_reg_oldcurpm = PL_curpm;
1081 PL_curpm = PL_reg_curpm;
1082 if (RX_MATCH_COPIED(prog)) {
1083 /* Here is a serious problem: we cannot rewrite subbeg,
1084 since it may be needed if this match fails. Thus
1085 $` inside (?{}) could fail... */
1086 PL_reg_oldsaved = prog->subbeg;
1087 PL_reg_oldsavedlen = prog->sublen;
1088 RX_MATCH_COPIED_off(prog);
1091 PL_reg_oldsaved = Nullch;
1092 prog->subbeg = PL_bostr;
1093 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1095 prog->startp[0] = startpos - PL_bostr;
1096 PL_reginput = startpos;
1097 PL_regstartp = prog->startp;
1098 PL_regendp = prog->endp;
1099 PL_reglastparen = &prog->lastparen;
1100 prog->lastparen = 0;
1102 DEBUG_r(PL_reg_starttry = startpos);
1103 if (PL_reg_start_tmpl <= prog->nparens) {
1104 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1105 if(PL_reg_start_tmp)
1106 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1108 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1111 /* XXXX What this code is doing here?!!! There should be no need
1112 to do this again and again, PL_reglastparen should take care of
1116 if (prog->nparens) {
1117 for (i = prog->nparens; i >= 1; i--) {
1123 if (regmatch(prog->program + 1)) {
1124 prog->endp[0] = PL_reginput - PL_bostr;
1132 - regmatch - main matching routine
1134 * Conceptually the strategy is simple: check to see whether the current
1135 * node matches, call self recursively to see whether the rest matches,
1136 * and then act accordingly. In practice we make some effort to avoid
1137 * recursion, in particular by going through "ordinary" nodes (that don't
1138 * need to know whether the rest of the match failed) by a loop instead of
1141 /* [lwall] I've hoisted the register declarations to the outer block in order to
1142 * maybe save a little bit of pushing and popping on the stack. It also takes
1143 * advantage of machines that use a register save mask on subroutine entry.
1145 STATIC I32 /* 0 failure, 1 success */
1146 S_regmatch(pTHX_ regnode *prog)
1149 register regnode *scan; /* Current node. */
1150 regnode *next; /* Next node. */
1151 regnode *inner; /* Next node in internal branch. */
1152 register I32 nextchr; /* renamed nextchr - nextchar colides with
1153 function of same name */
1154 register I32 n; /* no or next */
1155 register I32 ln; /* len or last */
1156 register char *s; /* operand or save */
1157 register char *locinput = PL_reginput;
1158 register I32 c1, c2, paren; /* case fold search, parenth */
1159 int minmod = 0, sw = 0, logical = 0;
1164 /* Note that nextchr is a byte even in UTF */
1165 nextchr = UCHARAT(locinput);
1167 while (scan != NULL) {
1168 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1170 # define sayYES goto yes
1171 # define sayNO goto no
1172 # define saySAME(x) if (x) goto yes; else goto no
1173 # define REPORT_CODE_OFF 24
1175 # define sayYES return 1
1176 # define sayNO return 0
1177 # define saySAME(x) return x
1180 SV *prop = sv_newmortal();
1181 int docolor = *PL_colors[0];
1182 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1183 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1184 /* The part of the string before starttry has one color
1185 (pref0_len chars), between starttry and current
1186 position another one (pref_len - pref0_len chars),
1187 after the current position the third one.
1188 We assume that pref0_len <= pref_len, otherwise we
1189 decrease pref0_len. */
1190 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1191 ? (5 + taill) - l : locinput - PL_bostr);
1192 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1194 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1195 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1196 ? (5 + taill) - pref_len : PL_regeol - locinput);
1199 if (pref0_len > pref_len)
1200 pref0_len = pref_len;
1201 regprop(prop, scan);
1202 PerlIO_printf(Perl_debug_log,
1203 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1204 locinput - PL_bostr,
1205 PL_colors[4], pref0_len,
1206 locinput - pref_len, PL_colors[5],
1207 PL_colors[2], pref_len - pref0_len,
1208 locinput - pref_len + pref0_len, PL_colors[3],
1209 (docolor ? "" : "> <"),
1210 PL_colors[0], l, locinput, PL_colors[1],
1211 15 - l - pref_len + 1,
1213 scan - PL_regprogram, PL_regindent*2, "",
1217 next = scan + NEXT_OFF(scan);
1223 if (locinput == PL_bostr
1224 ? PL_regprev == '\n'
1226 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1228 /* regtill = regbol; */
1233 if (locinput == PL_bostr
1234 ? PL_regprev == '\n'
1235 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1241 if (locinput == PL_regbol && PL_regprev == '\n')
1245 if (locinput == PL_reg_ganch)
1255 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1260 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1262 if (PL_regeol - locinput > 1)
1266 if (PL_regeol != locinput)
1270 if (nextchr & 0x80) {
1271 locinput += PL_utf8skip[nextchr];
1272 if (locinput > PL_regeol)
1274 nextchr = UCHARAT(locinput);
1277 if (!nextchr && locinput >= PL_regeol)
1279 nextchr = UCHARAT(++locinput);
1282 if (!nextchr && locinput >= PL_regeol)
1284 nextchr = UCHARAT(++locinput);
1287 if (nextchr & 0x80) {
1288 locinput += PL_utf8skip[nextchr];
1289 if (locinput > PL_regeol)
1291 nextchr = UCHARAT(locinput);
1294 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1296 nextchr = UCHARAT(++locinput);
1299 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1301 nextchr = UCHARAT(++locinput);
1304 s = (char *) OPERAND(scan);
1306 /* Inline the first character, for speed. */
1307 if (UCHARAT(s) != nextchr)
1309 if (PL_regeol - locinput < ln)
1311 if (ln > 1 && memNE(s, locinput, ln))
1314 nextchr = UCHARAT(locinput);
1317 PL_reg_flags |= RF_tainted;
1320 s = (char *) OPERAND(scan);
1326 c1 = OP(scan) == EXACTF;
1330 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1331 toLOWER_utf8((U8*)l) :
1332 toLOWER_LC_utf8((U8*)l)))
1340 nextchr = UCHARAT(locinput);
1344 /* Inline the first character, for speed. */
1345 if (UCHARAT(s) != nextchr &&
1346 UCHARAT(s) != ((OP(scan) == EXACTF)
1347 ? PL_fold : PL_fold_locale)[nextchr])
1349 if (PL_regeol - locinput < ln)
1351 if (ln > 1 && (OP(scan) == EXACTF
1352 ? ibcmp(s, locinput, ln)
1353 : ibcmp_locale(s, locinput, ln)))
1356 nextchr = UCHARAT(locinput);
1359 s = (char *) OPERAND(scan);
1360 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1362 if (locinput >= PL_regeol)
1364 locinput += PL_utf8skip[nextchr];
1365 nextchr = UCHARAT(locinput);
1368 s = (char *) OPERAND(scan);
1370 nextchr = UCHARAT(locinput);
1371 if (!REGINCLASS(s, nextchr))
1373 if (!nextchr && locinput >= PL_regeol)
1375 nextchr = UCHARAT(++locinput);
1378 PL_reg_flags |= RF_tainted;
1383 if (!(OP(scan) == ALNUM
1384 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1386 nextchr = UCHARAT(++locinput);
1389 PL_reg_flags |= RF_tainted;
1394 if (nextchr & 0x80) {
1395 if (!(OP(scan) == ALNUMUTF8
1396 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1397 : isALNUM_LC_utf8((U8*)locinput)))
1401 locinput += PL_utf8skip[nextchr];
1402 nextchr = UCHARAT(locinput);
1405 if (!(OP(scan) == ALNUMUTF8
1406 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1408 nextchr = UCHARAT(++locinput);
1411 PL_reg_flags |= RF_tainted;
1414 if (!nextchr && locinput >= PL_regeol)
1416 if (OP(scan) == NALNUM
1417 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1419 nextchr = UCHARAT(++locinput);
1422 PL_reg_flags |= RF_tainted;
1425 if (!nextchr && locinput >= PL_regeol)
1427 if (nextchr & 0x80) {
1428 if (OP(scan) == NALNUMUTF8
1429 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1430 : isALNUM_LC_utf8((U8*)locinput))
1434 locinput += PL_utf8skip[nextchr];
1435 nextchr = UCHARAT(locinput);
1438 if (OP(scan) == NALNUMUTF8
1439 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1441 nextchr = UCHARAT(++locinput);
1445 PL_reg_flags |= RF_tainted;
1449 /* was last char in word? */
1450 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1451 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1453 n = isALNUM(nextchr);
1456 ln = isALNUM_LC(ln);
1457 n = isALNUM_LC(nextchr);
1459 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1464 PL_reg_flags |= RF_tainted;
1468 /* was last char in word? */
1469 ln = (locinput != PL_regbol)
1470 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1471 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1472 ln = isALNUM_uni(ln);
1473 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1476 ln = isALNUM_LC_uni(ln);
1477 n = isALNUM_LC_utf8((U8*)locinput);
1479 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1483 PL_reg_flags |= RF_tainted;
1486 if (!nextchr && locinput >= PL_regeol)
1488 if (!(OP(scan) == SPACE
1489 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1491 nextchr = UCHARAT(++locinput);
1494 PL_reg_flags |= RF_tainted;
1497 if (!nextchr && locinput >= PL_regeol)
1499 if (nextchr & 0x80) {
1500 if (!(OP(scan) == SPACEUTF8
1501 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1502 : isSPACE_LC_utf8((U8*)locinput)))
1506 locinput += PL_utf8skip[nextchr];
1507 nextchr = UCHARAT(locinput);
1510 if (!(OP(scan) == SPACEUTF8
1511 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1513 nextchr = UCHARAT(++locinput);
1516 PL_reg_flags |= RF_tainted;
1521 if (OP(scan) == SPACE
1522 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1524 nextchr = UCHARAT(++locinput);
1527 PL_reg_flags |= RF_tainted;
1532 if (nextchr & 0x80) {
1533 if (OP(scan) == NSPACEUTF8
1534 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1535 : isSPACE_LC_utf8((U8*)locinput))
1539 locinput += PL_utf8skip[nextchr];
1540 nextchr = UCHARAT(locinput);
1543 if (OP(scan) == NSPACEUTF8
1544 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1546 nextchr = UCHARAT(++locinput);
1549 if (!isDIGIT(nextchr))
1551 nextchr = UCHARAT(++locinput);
1554 if (nextchr & 0x80) {
1555 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1557 locinput += PL_utf8skip[nextchr];
1558 nextchr = UCHARAT(locinput);
1561 if (!isDIGIT(nextchr))
1563 nextchr = UCHARAT(++locinput);
1566 if (!nextchr && locinput >= PL_regeol)
1568 if (isDIGIT(nextchr))
1570 nextchr = UCHARAT(++locinput);
1573 if (!nextchr && locinput >= PL_regeol)
1575 if (nextchr & 0x80) {
1576 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1578 locinput += PL_utf8skip[nextchr];
1579 nextchr = UCHARAT(locinput);
1582 if (isDIGIT(nextchr))
1584 nextchr = UCHARAT(++locinput);
1587 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1589 locinput += PL_utf8skip[nextchr];
1590 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1591 locinput += UTF8SKIP(locinput);
1592 if (locinput > PL_regeol)
1594 nextchr = UCHARAT(locinput);
1597 PL_reg_flags |= RF_tainted;
1601 n = ARG(scan); /* which paren pair */
1602 ln = PL_regstartp[n];
1603 if (*PL_reglastparen < n || ln == -1)
1604 sayNO; /* Do not match unless seen CLOSEn. */
1605 if (ln == PL_regendp[n])
1609 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1611 char *e = PL_bostr + PL_regendp[n];
1613 * Note that we can't do the "other character" lookup trick as
1614 * in the 8-bit case (no pun intended) because in Unicode we
1615 * have to map both upper and title case to lower case.
1617 if (OP(scan) == REFF) {
1621 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1631 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1638 nextchr = UCHARAT(locinput);
1642 /* Inline the first character, for speed. */
1643 if (UCHARAT(s) != nextchr &&
1645 (UCHARAT(s) != ((OP(scan) == REFF
1646 ? PL_fold : PL_fold_locale)[nextchr]))))
1648 ln = PL_regendp[n] - ln;
1649 if (locinput + ln > PL_regeol)
1651 if (ln > 1 && (OP(scan) == REF
1652 ? memNE(s, locinput, ln)
1654 ? ibcmp(s, locinput, ln)
1655 : ibcmp_locale(s, locinput, ln))))
1658 nextchr = UCHARAT(locinput);
1669 OP_4tree *oop = PL_op;
1670 COP *ocurcop = PL_curcop;
1671 SV **ocurpad = PL_curpad;
1675 PL_op = (OP_4tree*)PL_regdata->data[n];
1676 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1677 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
1678 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
1680 CALLRUNOPS(aTHX); /* Scalar context. */
1686 PL_curpad = ocurpad;
1687 PL_curcop = ocurcop;
1689 if (logical == 2) { /* Postponed subexpression. */
1691 MAGIC *mg = Null(MAGIC*);
1694 CHECKPOINT cp, lastcp;
1696 if(SvROK(ret) || SvRMAGICAL(ret)) {
1697 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1700 mg = mg_find(sv, 'r');
1703 re = (regexp *)mg->mg_obj;
1704 (void)ReREFCNT_inc(re);
1708 char *t = SvPV(ret, len);
1710 char *oprecomp = PL_regprecomp;
1711 I32 osize = PL_regsize;
1712 I32 onpar = PL_regnpar;
1715 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
1717 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1718 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1719 PL_regprecomp = oprecomp;
1724 PerlIO_printf(Perl_debug_log,
1725 "Entering embedded `%s%.60s%s%s'\n",
1729 (strlen(re->precomp) > 60 ? "..." : ""))
1732 state.prev = PL_reg_call_cc;
1733 state.cc = PL_regcc;
1734 state.re = PL_reg_re;
1740 cp = regcppush(0); /* Save *all* the positions. */
1743 state.ss = PL_savestack_ix;
1744 *PL_reglastparen = 0;
1745 PL_reg_call_cc = &state;
1746 PL_reginput = locinput;
1747 if (regmatch(re->program + 1)) {
1753 PerlIO_printf(Perl_debug_log,
1755 REPORT_CODE_OFF+PL_regindent*2, "")
1760 PL_reg_call_cc = state.prev;
1761 PL_regcc = state.cc;
1762 PL_reg_re = state.re;
1763 cache_re(PL_reg_re);
1770 sv_setsv(save_scalar(PL_replgv), ret);
1774 n = ARG(scan); /* which paren pair */
1775 PL_reg_start_tmp[n] = locinput;
1780 n = ARG(scan); /* which paren pair */
1781 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
1782 PL_regendp[n] = locinput - PL_bostr;
1783 if (n > *PL_reglastparen)
1784 *PL_reglastparen = n;
1787 n = ARG(scan); /* which paren pair */
1788 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
1792 next = NEXTOPER(NEXTOPER(scan));
1794 next = scan + ARG(scan);
1795 if (OP(next) == IFTHEN) /* Fake one. */
1796 next = NEXTOPER(NEXTOPER(next));
1800 logical = scan->flags;
1804 CHECKPOINT cp = PL_savestack_ix;
1806 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1808 cc.oldcc = PL_regcc;
1810 cc.parenfloor = *PL_reglastparen;
1812 cc.min = ARG1(scan);
1813 cc.max = ARG2(scan);
1814 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1818 PL_reginput = locinput;
1819 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1821 PL_regcc = cc.oldcc;
1827 * This is really hard to understand, because after we match
1828 * what we're trying to match, we must make sure the rest of
1829 * the RE is going to match for sure, and to do that we have
1830 * to go back UP the parse tree by recursing ever deeper. And
1831 * if it fails, we have to reset our parent's current state
1832 * that we can try again after backing off.
1835 CHECKPOINT cp, lastcp;
1836 CURCUR* cc = PL_regcc;
1837 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1839 n = cc->cur + 1; /* how many we know we matched */
1840 PL_reginput = locinput;
1843 PerlIO_printf(Perl_debug_log,
1844 "%*s %ld out of %ld..%ld cc=%lx\n",
1845 REPORT_CODE_OFF+PL_regindent*2, "",
1846 (long)n, (long)cc->min,
1847 (long)cc->max, (long)cc)
1850 /* If degenerate scan matches "", assume scan done. */
1852 if (locinput == cc->lastloc && n >= cc->min) {
1853 PL_regcc = cc->oldcc;
1856 PerlIO_printf(Perl_debug_log,
1857 "%*s empty match detected, try continuation...\n",
1858 REPORT_CODE_OFF+PL_regindent*2, "")
1860 if (regmatch(cc->next))
1863 PerlIO_printf(Perl_debug_log,
1865 REPORT_CODE_OFF+PL_regindent*2, "")
1872 /* First just match a string of min scans. */
1876 cc->lastloc = locinput;
1877 if (regmatch(cc->scan))
1880 cc->lastloc = lastloc;
1882 PerlIO_printf(Perl_debug_log,
1884 REPORT_CODE_OFF+PL_regindent*2, "")
1889 /* Prefer next over scan for minimal matching. */
1892 PL_regcc = cc->oldcc;
1894 cp = regcppush(cc->parenfloor);
1896 if (regmatch(cc->next)) {
1898 sayYES; /* All done. */
1905 if (n >= cc->max) { /* Maximum greed exceeded? */
1906 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1907 && !(PL_reg_flags & RF_warned)) {
1908 PL_reg_flags |= RF_warned;
1909 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
1910 "Complex regular subexpression recursion",
1917 PerlIO_printf(Perl_debug_log,
1918 "%*s trying longer...\n",
1919 REPORT_CODE_OFF+PL_regindent*2, "")
1921 /* Try scanning more and see if it helps. */
1922 PL_reginput = locinput;
1924 cc->lastloc = locinput;
1925 cp = regcppush(cc->parenfloor);
1927 if (regmatch(cc->scan)) {
1932 PerlIO_printf(Perl_debug_log,
1934 REPORT_CODE_OFF+PL_regindent*2, "")
1939 cc->lastloc = lastloc;
1943 /* Prefer scan over next for maximal matching. */
1945 if (n < cc->max) { /* More greed allowed? */
1946 cp = regcppush(cc->parenfloor);
1948 cc->lastloc = locinput;
1950 if (regmatch(cc->scan)) {
1955 regcppop(); /* Restore some previous $<digit>s? */
1956 PL_reginput = locinput;
1958 PerlIO_printf(Perl_debug_log,
1959 "%*s failed, try continuation...\n",
1960 REPORT_CODE_OFF+PL_regindent*2, "")
1963 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1964 && !(PL_reg_flags & RF_warned)) {
1965 PL_reg_flags |= RF_warned;
1966 Perl_warner(aTHX_ WARN_UNSAFE, "%s limit (%d) exceeded",
1967 "Complex regular subexpression recursion",
1971 /* Failed deeper matches of scan, so see if this one works. */
1972 PL_regcc = cc->oldcc;
1974 if (regmatch(cc->next))
1977 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1978 REPORT_CODE_OFF+PL_regindent*2, "")
1983 cc->lastloc = lastloc;
1988 next = scan + ARG(scan);
1991 inner = NEXTOPER(NEXTOPER(scan));
1994 inner = NEXTOPER(scan);
1999 if (OP(next) != c1) /* No choice. */
2000 next = inner; /* Avoid recursion. */
2002 int lastparen = *PL_reglastparen;
2006 PL_reginput = locinput;
2007 if (regmatch(inner))
2010 for (n = *PL_reglastparen; n > lastparen; n--)
2012 *PL_reglastparen = n;
2015 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2019 inner = NEXTOPER(scan);
2020 if (c1 == BRANCHJ) {
2021 inner = NEXTOPER(inner);
2023 } while (scan != NULL && OP(scan) == c1);
2037 /* We suppose that the next guy does not need
2038 backtracking: in particular, it is of constant length,
2039 and has no parenths to influence future backrefs. */
2040 ln = ARG1(scan); /* min to match */
2041 n = ARG2(scan); /* max to match */
2042 paren = scan->flags;
2044 if (paren > PL_regsize)
2046 if (paren > *PL_reglastparen)
2047 *PL_reglastparen = paren;
2049 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2051 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2052 PL_reginput = locinput;
2055 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2057 if (ln && l == 0 && n >= ln
2058 /* In fact, this is tricky. If paren, then the
2059 fact that we did/didnot match may influence
2060 future execution. */
2061 && !(paren && ln == 0))
2063 locinput = PL_reginput;
2064 if (PL_regkind[(U8)OP(next)] == EXACT) {
2065 c1 = UCHARAT(OPERAND(next) + 1);
2066 if (OP(next) == EXACTF)
2068 else if (OP(next) == EXACTFL)
2069 c2 = PL_fold_locale[c1];
2076 /* This may be improved if l == 0. */
2077 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2078 /* If it could work, try it. */
2080 UCHARAT(PL_reginput) == c1 ||
2081 UCHARAT(PL_reginput) == c2)
2085 PL_regstartp[paren] =
2086 HOPc(PL_reginput, -l) - PL_bostr;
2087 PL_regendp[paren] = PL_reginput - PL_bostr;
2090 PL_regendp[paren] = -1;
2096 /* Couldn't or didn't -- move forward. */
2097 PL_reginput = locinput;
2098 if (regrepeat_hard(scan, 1, &l)) {
2100 locinput = PL_reginput;
2107 n = regrepeat_hard(scan, n, &l);
2108 if (n != 0 && l == 0
2109 /* In fact, this is tricky. If paren, then the
2110 fact that we did/didnot match may influence
2111 future execution. */
2112 && !(paren && ln == 0))
2114 locinput = PL_reginput;
2116 PerlIO_printf(Perl_debug_log,
2117 "%*s matched %ld times, len=%ld...\n",
2118 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2121 if (PL_regkind[(U8)OP(next)] == EXACT) {
2122 c1 = UCHARAT(OPERAND(next) + 1);
2123 if (OP(next) == EXACTF)
2125 else if (OP(next) == EXACTFL)
2126 c2 = PL_fold_locale[c1];
2135 /* If it could work, try it. */
2137 UCHARAT(PL_reginput) == c1 ||
2138 UCHARAT(PL_reginput) == c2)
2141 PerlIO_printf(Perl_debug_log,
2142 "%*s trying tail with n=%ld...\n",
2143 REPORT_CODE_OFF+PL_regindent*2, "", n)
2147 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2148 PL_regendp[paren] = PL_reginput - PL_bostr;
2151 PL_regendp[paren] = -1;
2157 /* Couldn't or didn't -- back up. */
2159 locinput = HOPc(locinput, -l);
2160 PL_reginput = locinput;
2167 paren = scan->flags; /* Which paren to set */
2168 if (paren > PL_regsize)
2170 if (paren > *PL_reglastparen)
2171 *PL_reglastparen = paren;
2172 ln = ARG1(scan); /* min to match */
2173 n = ARG2(scan); /* max to match */
2174 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2178 ln = ARG1(scan); /* min to match */
2179 n = ARG2(scan); /* max to match */
2180 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2185 scan = NEXTOPER(scan);
2191 scan = NEXTOPER(scan);
2195 * Lookahead to avoid useless match attempts
2196 * when we know what character comes next.
2198 if (PL_regkind[(U8)OP(next)] == EXACT) {
2199 c1 = UCHARAT(OPERAND(next) + 1);
2200 if (OP(next) == EXACTF)
2202 else if (OP(next) == EXACTFL)
2203 c2 = PL_fold_locale[c1];
2209 PL_reginput = locinput;
2213 if (ln && regrepeat(scan, ln) < ln)
2215 locinput = PL_reginput;
2218 char *e = locinput + n - ln; /* Should not check after this */
2219 char *old = locinput;
2221 if (e >= PL_regeol || (n == REG_INFTY))
2224 /* Find place 'next' could work */
2226 while (locinput <= e && *locinput != c1)
2229 while (locinput <= e
2236 /* PL_reginput == old now */
2237 if (locinput != old) {
2238 ln = 1; /* Did some */
2239 if (regrepeat(scan, locinput - old) <
2243 /* PL_reginput == locinput now */
2246 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2247 PL_regendp[paren] = locinput - PL_bostr;
2250 PL_regendp[paren] = -1;
2254 PL_reginput = locinput; /* Could be reset... */
2256 /* Couldn't or didn't -- move forward. */
2261 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2262 /* If it could work, try it. */
2264 UCHARAT(PL_reginput) == c1 ||
2265 UCHARAT(PL_reginput) == c2)
2269 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2270 PL_regendp[paren] = PL_reginput - PL_bostr;
2273 PL_regendp[paren] = -1;
2279 /* Couldn't or didn't -- move forward. */
2280 PL_reginput = locinput;
2281 if (regrepeat(scan, 1)) {
2283 locinput = PL_reginput;
2291 n = regrepeat(scan, n);
2292 locinput = PL_reginput;
2293 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2294 (!PL_multiline || OP(next) == SEOL))
2295 ln = n; /* why back off? */
2299 /* If it could work, try it. */
2301 UCHARAT(PL_reginput) == c1 ||
2302 UCHARAT(PL_reginput) == c2)
2306 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2307 PL_regendp[paren] = PL_reginput - PL_bostr;
2310 PL_regendp[paren] = -1;
2316 /* Couldn't or didn't -- back up. */
2318 PL_reginput = locinput = HOPc(locinput, -1);
2323 /* If it could work, try it. */
2325 UCHARAT(PL_reginput) == c1 ||
2326 UCHARAT(PL_reginput) == c2)
2332 /* Couldn't or didn't -- back up. */
2334 PL_reginput = locinput = HOPc(locinput, -1);
2341 if (PL_reg_call_cc) {
2342 re_cc_state *cur_call_cc = PL_reg_call_cc;
2343 CURCUR *cctmp = PL_regcc;
2344 regexp *re = PL_reg_re;
2345 CHECKPOINT cp, lastcp;
2347 cp = regcppush(0); /* Save *all* the positions. */
2349 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2351 PL_reginput = locinput; /* Make position available to
2353 cache_re(PL_reg_call_cc->re);
2354 PL_regcc = PL_reg_call_cc->cc;
2355 PL_reg_call_cc = PL_reg_call_cc->prev;
2356 if (regmatch(cur_call_cc->node)) {
2357 PL_reg_call_cc = cur_call_cc;
2363 PL_reg_call_cc = cur_call_cc;
2369 PerlIO_printf(Perl_debug_log,
2370 "%*s continuation failed...\n",
2371 REPORT_CODE_OFF+PL_regindent*2, "")
2375 if (locinput < PL_regtill)
2376 sayNO; /* Cannot match: too short. */
2379 PL_reginput = locinput; /* put where regtry can find it */
2380 sayYES; /* Success! */
2383 PL_reginput = locinput;
2388 if (UTF) { /* XXXX This is absolutely
2389 broken, we read before
2391 s = HOPMAYBEc(locinput, -scan->flags);
2397 if (locinput < PL_bostr + scan->flags)
2399 PL_reginput = locinput - scan->flags;
2404 PL_reginput = locinput;
2409 if (UTF) { /* XXXX This is absolutely
2410 broken, we read before
2412 s = HOPMAYBEc(locinput, -scan->flags);
2413 if (!s || s < PL_bostr)
2418 if (locinput < PL_bostr + scan->flags)
2420 PL_reginput = locinput - scan->flags;
2425 PL_reginput = locinput;
2428 inner = NEXTOPER(NEXTOPER(scan));
2429 if (regmatch(inner) != n) {
2444 if (OP(scan) == SUSPEND) {
2445 locinput = PL_reginput;
2446 nextchr = UCHARAT(locinput);
2451 next = scan + ARG(scan);
2456 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2457 (unsigned long)scan, OP(scan));
2458 Perl_croak(aTHX_ "regexp memory corruption");
2464 * We get here only if there's trouble -- normally "case END" is
2465 * the terminating point.
2467 Perl_croak(aTHX_ "corrupted regexp pointers");
2485 - regrepeat - repeatedly match something simple, report how many
2488 * [This routine now assumes that it will only match on things of length 1.
2489 * That was true before, but now we assume scan - reginput is the count,
2490 * rather than incrementing count on every character. [Er, except utf8.]]
2493 S_regrepeat(pTHX_ regnode *p, I32 max)
2496 register char *scan;
2497 register char *opnd;
2499 register char *loceol = PL_regeol;
2500 register I32 hardcount = 0;
2503 if (max != REG_INFTY && max < loceol - scan)
2504 loceol = scan + max;
2505 opnd = (char *) OPERAND(p);
2508 while (scan < loceol && *scan != '\n')
2516 while (scan < loceol && *scan != '\n') {
2517 scan += UTF8SKIP(scan);
2523 while (scan < loceol) {
2524 scan += UTF8SKIP(scan);
2528 case EXACT: /* length of string is 1 */
2529 c = UCHARAT(++opnd);
2530 while (scan < loceol && UCHARAT(scan) == c)
2533 case EXACTF: /* length of string is 1 */
2534 c = UCHARAT(++opnd);
2535 while (scan < loceol &&
2536 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2539 case EXACTFL: /* length of string is 1 */
2540 PL_reg_flags |= RF_tainted;
2541 c = UCHARAT(++opnd);
2542 while (scan < loceol &&
2543 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2548 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2549 scan += UTF8SKIP(scan);
2554 while (scan < loceol && REGINCLASS(opnd, *scan))
2558 while (scan < loceol && isALNUM(*scan))
2563 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2564 scan += UTF8SKIP(scan);
2569 PL_reg_flags |= RF_tainted;
2570 while (scan < loceol && isALNUM_LC(*scan))
2574 PL_reg_flags |= RF_tainted;
2576 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2577 scan += UTF8SKIP(scan);
2583 while (scan < loceol && !isALNUM(*scan))
2588 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2589 scan += UTF8SKIP(scan);
2594 PL_reg_flags |= RF_tainted;
2595 while (scan < loceol && !isALNUM_LC(*scan))
2599 PL_reg_flags |= RF_tainted;
2601 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2602 scan += UTF8SKIP(scan);
2607 while (scan < loceol && isSPACE(*scan))
2612 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2613 scan += UTF8SKIP(scan);
2618 PL_reg_flags |= RF_tainted;
2619 while (scan < loceol && isSPACE_LC(*scan))
2623 PL_reg_flags |= RF_tainted;
2625 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2626 scan += UTF8SKIP(scan);
2631 while (scan < loceol && !isSPACE(*scan))
2636 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2637 scan += UTF8SKIP(scan);
2642 PL_reg_flags |= RF_tainted;
2643 while (scan < loceol && !isSPACE_LC(*scan))
2647 PL_reg_flags |= RF_tainted;
2649 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2650 scan += UTF8SKIP(scan);
2655 while (scan < loceol && isDIGIT(*scan))
2660 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2661 scan += UTF8SKIP(scan);
2667 while (scan < loceol && !isDIGIT(*scan))
2672 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2673 scan += UTF8SKIP(scan);
2677 default: /* Called on something of 0 width. */
2678 break; /* So match right here or not at all. */
2684 c = scan - PL_reginput;
2689 SV *prop = sv_newmortal();
2692 PerlIO_printf(Perl_debug_log,
2693 "%*s %s can match %ld times out of %ld...\n",
2694 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2701 - regrepeat_hard - repeatedly match something, report total lenth and length
2703 * The repeater is supposed to have constant length.
2707 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
2710 register char *scan;
2711 register char *start;
2712 register char *loceol = PL_regeol;
2714 I32 count = 0, res = 1;
2719 start = PL_reginput;
2721 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2724 while (start < PL_reginput) {
2726 start += UTF8SKIP(start);
2737 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2739 *lp = l = PL_reginput - start;
2740 if (max != REG_INFTY && l*max < loceol - scan)
2741 loceol = scan + l*max;
2754 - reginclass - determine if a character falls into a character class
2758 S_reginclass(pTHX_ register char *p, register I32 c)
2765 if (ANYOF_TEST(p, c))
2767 else if (flags & ANYOF_FOLD) {
2769 if (flags & ANYOF_LOCALE) {
2770 PL_reg_flags |= RF_tainted;
2771 cf = PL_fold_locale[c];
2775 if (ANYOF_TEST(p, cf))
2779 if (!match && (flags & ANYOF_ISA)) {
2780 PL_reg_flags |= RF_tainted;
2782 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2783 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2784 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2785 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2791 return (flags & ANYOF_INVERT) ? !match : match;
2795 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
2798 char flags = ARG1(f);
2800 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2802 if (swash_fetch(sv, p))
2804 else if (flags & ANYOF_FOLD) {
2807 if (flags & ANYOF_LOCALE) {
2808 PL_reg_flags |= RF_tainted;
2809 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2812 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2813 if (swash_fetch(sv, tmpbuf))
2817 if (!match && (flags & ANYOF_ISA)) {
2818 PL_reg_flags |= RF_tainted;
2820 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2821 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2822 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2823 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2829 return (flags & ANYOF_INVERT) ? !match : match;
2833 S_reghop(pTHX_ U8 *s, I32 off)
2837 while (off-- && s < (U8*)PL_regeol)
2842 if (s > (U8*)PL_bostr) {
2845 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2847 } /* XXX could check well-formedness here */
2855 S_reghopmaybe(pTHX_ U8* s, I32 off)
2859 while (off-- && s < (U8*)PL_regeol)
2866 if (s > (U8*)PL_bostr) {
2869 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2871 } /* XXX could check well-formedness here */