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 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 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 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 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 croak("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 croak("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 -(CHR_SVLEN(must) - (SvTAIL(must) != 0) + back_min));
489 char *last1; /* Last position checked before */
494 last1 = s - 1; /* bogus */
496 /* XXXX check_substr already used to find `s', can optimize if
497 check_substr==must. */
499 dontbother = end_shift;
500 strend = HOPc(strend, -dontbother);
501 while ( (s <= last) &&
502 ((flags & REXEC_SCREAM)
503 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
504 end_shift, &scream_pos, 0))
505 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
506 (unsigned char*)strend, must,
507 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
508 if (HOPc(s, -back_max) > last1) {
509 last1 = HOPc(s, -back_min);
510 s = HOPc(s, -back_max);
513 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
515 last1 = HOPc(s, -back_min);
535 else if (c = prog->regstclass) {
536 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
540 dontbother = minlen - 1;
541 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
543 /* We know what class it must start with. */
546 cc = (char *) OPERAND(c);
548 if (REGINCLASSUTF8(c, (U8*)s)) {
549 if (tmp && regtry(prog, s))
560 cc = (char *) OPERAND(c);
562 if (REGINCLASS(cc, *s)) {
563 if (tmp && regtry(prog, s))
574 PL_reg_flags |= RF_tainted;
581 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
582 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
584 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
591 if ((minlen || tmp) && regtry(prog,s))
595 PL_reg_flags |= RF_tainted;
600 strend = reghop_c(strend, -1);
602 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
603 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
605 if (tmp == !(OP(c) == BOUND ?
606 swash_fetch(PL_utf8_alnum, (U8*)s) :
607 isALNUM_LC_utf8((U8*)s)))
615 if ((minlen || tmp) && regtry(prog,s))
619 PL_reg_flags |= RF_tainted;
626 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
627 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
629 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
631 else if (regtry(prog, s))
635 if ((minlen || !tmp) && regtry(prog,s))
639 PL_reg_flags |= RF_tainted;
644 strend = reghop_c(strend, -1);
646 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
647 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
649 if (tmp == !(OP(c) == NBOUND ?
650 swash_fetch(PL_utf8_alnum, (U8*)s) :
651 isALNUM_LC_utf8((U8*)s)))
653 else if (regtry(prog, s))
657 if ((minlen || !tmp) && regtry(prog,s))
663 if (tmp && regtry(prog, s))
675 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
676 if (tmp && regtry(prog, s))
687 PL_reg_flags |= RF_tainted;
689 if (isALNUM_LC(*s)) {
690 if (tmp && regtry(prog, s))
701 PL_reg_flags |= RF_tainted;
703 if (isALNUM_LC_utf8((U8*)s)) {
704 if (tmp && regtry(prog, s))
717 if (tmp && regtry(prog, s))
729 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
730 if (tmp && regtry(prog, s))
741 PL_reg_flags |= RF_tainted;
743 if (!isALNUM_LC(*s)) {
744 if (tmp && regtry(prog, s))
755 PL_reg_flags |= RF_tainted;
757 if (!isALNUM_LC_utf8((U8*)s)) {
758 if (tmp && regtry(prog, s))
771 if (tmp && regtry(prog, s))
783 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
784 if (tmp && regtry(prog, s))
795 PL_reg_flags |= RF_tainted;
797 if (isSPACE_LC(*s)) {
798 if (tmp && regtry(prog, s))
809 PL_reg_flags |= RF_tainted;
811 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
812 if (tmp && regtry(prog, s))
825 if (tmp && regtry(prog, s))
837 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
838 if (tmp && regtry(prog, s))
849 PL_reg_flags |= RF_tainted;
851 if (!isSPACE_LC(*s)) {
852 if (tmp && regtry(prog, s))
863 PL_reg_flags |= RF_tainted;
865 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
866 if (tmp && regtry(prog, s))
879 if (tmp && regtry(prog, s))
891 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
892 if (tmp && regtry(prog, s))
905 if (tmp && regtry(prog, s))
917 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
918 if (tmp && regtry(prog, s))
932 if (prog->float_substr != Nullsv) { /* Trim the end. */
934 I32 oldpos = scream_pos;
936 if (flags & REXEC_SCREAM) {
937 last = screaminstr(sv, prog->float_substr, s - strbeg,
938 end_shift, &scream_pos, 1); /* last one */
940 last = scream_olds; /* Only one occurence. */
944 char *little = SvPV(prog->float_substr, len);
946 if (SvTAIL(prog->float_substr)) {
947 if (memEQ(strend - len + 1, little, len - 1))
948 last = strend - len + 1;
949 else if (!PL_multiline)
950 last = memEQ(strend - len, little, len)
951 ? strend - len : Nullch;
957 last = rninstr(s, strend, little, little + len);
959 last = strend; /* matching `$' */
962 if (last == NULL) goto phooey; /* Should not happen! */
963 dontbother = strend - last + prog->float_min_offset;
965 if (minlen && (dontbother < minlen))
966 dontbother = minlen - 1;
967 strend -= dontbother; /* this one's always in bytes! */
968 /* We don't know much -- general case. */
982 } while (s++ < strend);
990 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
992 if (PL_reg_eval_set) {
993 /* Preserve the current value of $^R */
994 if (oreplsv != GvSV(PL_replgv))
995 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
996 restored, the value remains
1001 /* make sure $`, $&, $', and $digit will work later */
1002 if ( !(flags & REXEC_NOT_FIRST) ) {
1003 if (RX_MATCH_COPIED(prog)) {
1004 Safefree(prog->subbeg);
1005 RX_MATCH_COPIED_off(prog);
1007 if (flags & REXEC_COPY_STR) {
1008 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1010 s = savepvn(strbeg, i);
1013 RX_MATCH_COPIED_on(prog);
1016 prog->subbeg = strbeg;
1017 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1024 if (PL_reg_eval_set)
1030 - regtry - try match at specific point
1032 STATIC I32 /* 0 failure, 1 success */
1033 regtry(pTHX_ regexp *prog, char *startpos)
1041 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1044 PL_reg_eval_set = RS_init;
1046 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
1047 PL_stack_sp - PL_stack_base);
1049 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1050 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1051 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1053 /* Apparently this is not needed, judging by wantarray. */
1054 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1055 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1058 /* Make $_ available to executed code. */
1059 if (PL_reg_sv != DEFSV) {
1060 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1065 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1066 && (mg = mg_find(PL_reg_sv, 'g')))) {
1067 /* prepare for quick setting of pos */
1068 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1069 mg = mg_find(PL_reg_sv, 'g');
1073 PL_reg_oldpos = mg->mg_len;
1074 SAVEDESTRUCTOR(restore_pos, 0);
1077 New(22,PL_reg_curpm, 1, PMOP);
1078 PL_reg_curpm->op_pmregexp = prog;
1079 PL_reg_oldcurpm = PL_curpm;
1080 PL_curpm = PL_reg_curpm;
1081 if (RX_MATCH_COPIED(prog)) {
1082 /* Here is a serious problem: we cannot rewrite subbeg,
1083 since it may be needed if this match fails. Thus
1084 $` inside (?{}) could fail... */
1085 PL_reg_oldsaved = prog->subbeg;
1086 PL_reg_oldsavedlen = prog->sublen;
1087 RX_MATCH_COPIED_off(prog);
1090 PL_reg_oldsaved = Nullch;
1091 prog->subbeg = PL_bostr;
1092 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1094 prog->startp[0] = startpos - PL_bostr;
1095 PL_reginput = startpos;
1096 PL_regstartp = prog->startp;
1097 PL_regendp = prog->endp;
1098 PL_reglastparen = &prog->lastparen;
1099 prog->lastparen = 0;
1101 DEBUG_r(PL_reg_starttry = startpos);
1102 if (PL_reg_start_tmpl <= prog->nparens) {
1103 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1104 if(PL_reg_start_tmp)
1105 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1107 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1110 /* XXXX What this code is doing here?!!! There should be no need
1111 to do this again and again, PL_reglastparen should take care of
1115 if (prog->nparens) {
1116 for (i = prog->nparens; i >= 1; i--) {
1122 if (regmatch(prog->program + 1)) {
1123 prog->endp[0] = PL_reginput - PL_bostr;
1131 - regmatch - main matching routine
1133 * Conceptually the strategy is simple: check to see whether the current
1134 * node matches, call self recursively to see whether the rest matches,
1135 * and then act accordingly. In practice we make some effort to avoid
1136 * recursion, in particular by going through "ordinary" nodes (that don't
1137 * need to know whether the rest of the match failed) by a loop instead of
1140 /* [lwall] I've hoisted the register declarations to the outer block in order to
1141 * maybe save a little bit of pushing and popping on the stack. It also takes
1142 * advantage of machines that use a register save mask on subroutine entry.
1144 STATIC I32 /* 0 failure, 1 success */
1145 regmatch(pTHX_ regnode *prog)
1148 register regnode *scan; /* Current node. */
1149 regnode *next; /* Next node. */
1150 regnode *inner; /* Next node in internal branch. */
1151 register I32 nextchr; /* renamed nextchr - nextchar colides with
1152 function of same name */
1153 register I32 n; /* no or next */
1154 register I32 ln; /* len or last */
1155 register char *s; /* operand or save */
1156 register char *locinput = PL_reginput;
1157 register I32 c1, c2, paren; /* case fold search, parenth */
1158 int minmod = 0, sw = 0, logical = 0;
1163 /* Note that nextchr is a byte even in UTF */
1164 nextchr = UCHARAT(locinput);
1166 while (scan != NULL) {
1167 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1169 # define sayYES goto yes
1170 # define sayNO goto no
1171 # define saySAME(x) if (x) goto yes; else goto no
1172 # define REPORT_CODE_OFF 24
1174 # define sayYES return 1
1175 # define sayNO return 0
1176 # define saySAME(x) return x
1179 SV *prop = sv_newmortal();
1180 int docolor = *PL_colors[0];
1181 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1182 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1183 /* The part of the string before starttry has one color
1184 (pref0_len chars), between starttry and current
1185 position another one (pref_len - pref0_len chars),
1186 after the current position the third one.
1187 We assume that pref0_len <= pref_len, otherwise we
1188 decrease pref0_len. */
1189 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1190 ? (5 + taill) - l : locinput - PL_bostr);
1191 int pref0_len = pref_len - (locinput - PL_reg_starttry);
1193 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1194 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1195 ? (5 + taill) - pref_len : PL_regeol - locinput);
1198 if (pref0_len > pref_len)
1199 pref0_len = pref_len;
1200 regprop(prop, scan);
1201 PerlIO_printf(Perl_debug_log,
1202 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1203 locinput - PL_bostr,
1204 PL_colors[4], pref0_len,
1205 locinput - pref_len, PL_colors[5],
1206 PL_colors[2], pref_len - pref0_len,
1207 locinput - pref_len + pref0_len, PL_colors[3],
1208 (docolor ? "" : "> <"),
1209 PL_colors[0], l, locinput, PL_colors[1],
1210 15 - l - pref_len + 1,
1212 scan - PL_regprogram, PL_regindent*2, "",
1216 next = scan + NEXT_OFF(scan);
1222 if (locinput == PL_bostr
1223 ? PL_regprev == '\n'
1225 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1227 /* regtill = regbol; */
1232 if (locinput == PL_bostr
1233 ? PL_regprev == '\n'
1234 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1240 if (locinput == PL_regbol && PL_regprev == '\n')
1244 if (locinput == PL_reg_ganch)
1254 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1259 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1261 if (PL_regeol - locinput > 1)
1265 if (PL_regeol != locinput)
1269 if (nextchr & 0x80) {
1270 locinput += PL_utf8skip[nextchr];
1271 if (locinput > PL_regeol)
1273 nextchr = UCHARAT(locinput);
1276 if (!nextchr && locinput >= PL_regeol)
1278 nextchr = UCHARAT(++locinput);
1281 if (!nextchr && locinput >= PL_regeol)
1283 nextchr = UCHARAT(++locinput);
1286 if (nextchr & 0x80) {
1287 locinput += PL_utf8skip[nextchr];
1288 if (locinput > PL_regeol)
1290 nextchr = UCHARAT(locinput);
1293 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1295 nextchr = UCHARAT(++locinput);
1298 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1300 nextchr = UCHARAT(++locinput);
1303 s = (char *) OPERAND(scan);
1305 /* Inline the first character, for speed. */
1306 if (UCHARAT(s) != nextchr)
1308 if (PL_regeol - locinput < ln)
1310 if (ln > 1 && memNE(s, locinput, ln))
1313 nextchr = UCHARAT(locinput);
1316 PL_reg_flags |= RF_tainted;
1319 s = (char *) OPERAND(scan);
1325 c1 = OP(scan) == EXACTF;
1329 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1330 toLOWER_utf8((U8*)l) :
1331 toLOWER_LC_utf8((U8*)l)))
1339 nextchr = UCHARAT(locinput);
1343 /* Inline the first character, for speed. */
1344 if (UCHARAT(s) != nextchr &&
1345 UCHARAT(s) != ((OP(scan) == EXACTF)
1346 ? PL_fold : PL_fold_locale)[nextchr])
1348 if (PL_regeol - locinput < ln)
1350 if (ln > 1 && (OP(scan) == EXACTF
1351 ? ibcmp(s, locinput, ln)
1352 : ibcmp_locale(s, locinput, ln)))
1355 nextchr = UCHARAT(locinput);
1358 s = (char *) OPERAND(scan);
1359 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1361 if (locinput >= PL_regeol)
1363 locinput += PL_utf8skip[nextchr];
1364 nextchr = UCHARAT(locinput);
1367 s = (char *) OPERAND(scan);
1369 nextchr = UCHARAT(locinput);
1370 if (!REGINCLASS(s, nextchr))
1372 if (!nextchr && locinput >= PL_regeol)
1374 nextchr = UCHARAT(++locinput);
1377 PL_reg_flags |= RF_tainted;
1382 if (!(OP(scan) == ALNUM
1383 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1385 nextchr = UCHARAT(++locinput);
1388 PL_reg_flags |= RF_tainted;
1393 if (nextchr & 0x80) {
1394 if (!(OP(scan) == ALNUMUTF8
1395 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1396 : isALNUM_LC_utf8((U8*)locinput)))
1400 locinput += PL_utf8skip[nextchr];
1401 nextchr = UCHARAT(locinput);
1404 if (!(OP(scan) == ALNUMUTF8
1405 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1407 nextchr = UCHARAT(++locinput);
1410 PL_reg_flags |= RF_tainted;
1413 if (!nextchr && locinput >= PL_regeol)
1415 if (OP(scan) == NALNUM
1416 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1418 nextchr = UCHARAT(++locinput);
1421 PL_reg_flags |= RF_tainted;
1424 if (!nextchr && locinput >= PL_regeol)
1426 if (nextchr & 0x80) {
1427 if (OP(scan) == NALNUMUTF8
1428 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1429 : isALNUM_LC_utf8((U8*)locinput))
1433 locinput += PL_utf8skip[nextchr];
1434 nextchr = UCHARAT(locinput);
1437 if (OP(scan) == NALNUMUTF8
1438 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1440 nextchr = UCHARAT(++locinput);
1444 PL_reg_flags |= RF_tainted;
1448 /* was last char in word? */
1449 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1450 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1452 n = isALNUM(nextchr);
1455 ln = isALNUM_LC(ln);
1456 n = isALNUM_LC(nextchr);
1458 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1463 PL_reg_flags |= RF_tainted;
1467 /* was last char in word? */
1468 ln = (locinput != PL_regbol)
1469 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1470 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1471 ln = isALNUM_uni(ln);
1472 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1475 ln = isALNUM_LC_uni(ln);
1476 n = isALNUM_LC_utf8((U8*)locinput);
1478 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1482 PL_reg_flags |= RF_tainted;
1485 if (!nextchr && locinput >= PL_regeol)
1487 if (!(OP(scan) == SPACE
1488 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1490 nextchr = UCHARAT(++locinput);
1493 PL_reg_flags |= RF_tainted;
1496 if (!nextchr && locinput >= PL_regeol)
1498 if (nextchr & 0x80) {
1499 if (!(OP(scan) == SPACEUTF8
1500 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1501 : isSPACE_LC_utf8((U8*)locinput)))
1505 locinput += PL_utf8skip[nextchr];
1506 nextchr = UCHARAT(locinput);
1509 if (!(OP(scan) == SPACEUTF8
1510 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1512 nextchr = UCHARAT(++locinput);
1515 PL_reg_flags |= RF_tainted;
1520 if (OP(scan) == SPACE
1521 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1523 nextchr = UCHARAT(++locinput);
1526 PL_reg_flags |= RF_tainted;
1531 if (nextchr & 0x80) {
1532 if (OP(scan) == NSPACEUTF8
1533 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1534 : isSPACE_LC_utf8((U8*)locinput))
1538 locinput += PL_utf8skip[nextchr];
1539 nextchr = UCHARAT(locinput);
1542 if (OP(scan) == NSPACEUTF8
1543 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1545 nextchr = UCHARAT(++locinput);
1548 if (!isDIGIT(nextchr))
1550 nextchr = UCHARAT(++locinput);
1553 if (nextchr & 0x80) {
1554 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1556 locinput += PL_utf8skip[nextchr];
1557 nextchr = UCHARAT(locinput);
1560 if (!isDIGIT(nextchr))
1562 nextchr = UCHARAT(++locinput);
1565 if (!nextchr && locinput >= PL_regeol)
1567 if (isDIGIT(nextchr))
1569 nextchr = UCHARAT(++locinput);
1572 if (!nextchr && locinput >= PL_regeol)
1574 if (nextchr & 0x80) {
1575 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1577 locinput += PL_utf8skip[nextchr];
1578 nextchr = UCHARAT(locinput);
1581 if (isDIGIT(nextchr))
1583 nextchr = UCHARAT(++locinput);
1586 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1588 locinput += PL_utf8skip[nextchr];
1589 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1590 locinput += UTF8SKIP(locinput);
1591 if (locinput > PL_regeol)
1593 nextchr = UCHARAT(locinput);
1596 PL_reg_flags |= RF_tainted;
1600 n = ARG(scan); /* which paren pair */
1601 ln = PL_regstartp[n];
1602 if (*PL_reglastparen < n || ln == -1)
1603 sayNO; /* Do not match unless seen CLOSEn. */
1604 if (ln == PL_regendp[n])
1608 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1610 char *e = PL_bostr + PL_regendp[n];
1612 * Note that we can't do the "other character" lookup trick as
1613 * in the 8-bit case (no pun intended) because in Unicode we
1614 * have to map both upper and title case to lower case.
1616 if (OP(scan) == REFF) {
1620 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1630 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1637 nextchr = UCHARAT(locinput);
1641 /* Inline the first character, for speed. */
1642 if (UCHARAT(s) != nextchr &&
1644 (UCHARAT(s) != ((OP(scan) == REFF
1645 ? PL_fold : PL_fold_locale)[nextchr]))))
1647 ln = PL_regendp[n] - ln;
1648 if (locinput + ln > PL_regeol)
1650 if (ln > 1 && (OP(scan) == REF
1651 ? memNE(s, locinput, ln)
1653 ? ibcmp(s, locinput, ln)
1654 : ibcmp_locale(s, locinput, ln))))
1657 nextchr = UCHARAT(locinput);
1668 OP_4tree *oop = PL_op;
1669 COP *ocurcop = PL_curcop;
1670 SV **ocurpad = PL_curpad;
1674 PL_op = (OP_4tree*)PL_regdata->data[n];
1675 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1676 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
1677 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
1679 CALLRUNOPS(); /* Scalar context. */
1685 PL_curpad = ocurpad;
1686 PL_curcop = ocurcop;
1688 if (logical == 2) { /* Postponed subexpression. */
1690 MAGIC *mg = Null(MAGIC*);
1693 CHECKPOINT cp, lastcp;
1695 if(SvROK(ret) || SvRMAGICAL(ret)) {
1696 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1699 mg = mg_find(sv, 'r');
1702 re = (regexp *)mg->mg_obj;
1703 (void)ReREFCNT_inc(re);
1707 char *t = SvPV(ret, len);
1709 char *oprecomp = PL_regprecomp;
1710 I32 osize = PL_regsize;
1711 I32 onpar = PL_regnpar;
1714 re = CALLREGCOMP(t, t + len, &pm);
1716 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1717 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1718 PL_regprecomp = oprecomp;
1723 PerlIO_printf(Perl_debug_log,
1724 "Entering embedded `%s%.60s%s%s'\n",
1728 (strlen(re->precomp) > 60 ? "..." : ""))
1731 state.prev = PL_reg_call_cc;
1732 state.cc = PL_regcc;
1733 state.re = PL_reg_re;
1739 cp = regcppush(0); /* Save *all* the positions. */
1742 state.ss = PL_savestack_ix;
1743 *PL_reglastparen = 0;
1744 PL_reg_call_cc = &state;
1745 PL_reginput = locinput;
1746 if (regmatch(re->program + 1)) {
1752 PerlIO_printf(Perl_debug_log,
1754 REPORT_CODE_OFF+PL_regindent*2, "")
1759 PL_reg_call_cc = state.prev;
1760 PL_regcc = state.cc;
1761 PL_reg_re = state.re;
1762 cache_re(PL_reg_re);
1769 sv_setsv(save_scalar(PL_replgv), ret);
1773 n = ARG(scan); /* which paren pair */
1774 PL_reg_start_tmp[n] = locinput;
1779 n = ARG(scan); /* which paren pair */
1780 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
1781 PL_regendp[n] = locinput - PL_bostr;
1782 if (n > *PL_reglastparen)
1783 *PL_reglastparen = n;
1786 n = ARG(scan); /* which paren pair */
1787 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
1791 next = NEXTOPER(NEXTOPER(scan));
1793 next = scan + ARG(scan);
1794 if (OP(next) == IFTHEN) /* Fake one. */
1795 next = NEXTOPER(NEXTOPER(next));
1799 logical = scan->flags;
1803 CHECKPOINT cp = PL_savestack_ix;
1805 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1807 cc.oldcc = PL_regcc;
1809 cc.parenfloor = *PL_reglastparen;
1811 cc.min = ARG1(scan);
1812 cc.max = ARG2(scan);
1813 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1817 PL_reginput = locinput;
1818 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1820 PL_regcc = cc.oldcc;
1826 * This is really hard to understand, because after we match
1827 * what we're trying to match, we must make sure the rest of
1828 * the RE is going to match for sure, and to do that we have
1829 * to go back UP the parse tree by recursing ever deeper. And
1830 * if it fails, we have to reset our parent's current state
1831 * that we can try again after backing off.
1834 CHECKPOINT cp, lastcp;
1835 CURCUR* cc = PL_regcc;
1836 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1838 n = cc->cur + 1; /* how many we know we matched */
1839 PL_reginput = locinput;
1842 PerlIO_printf(Perl_debug_log,
1843 "%*s %ld out of %ld..%ld cc=%lx\n",
1844 REPORT_CODE_OFF+PL_regindent*2, "",
1845 (long)n, (long)cc->min,
1846 (long)cc->max, (long)cc)
1849 /* If degenerate scan matches "", assume scan done. */
1851 if (locinput == cc->lastloc && n >= cc->min) {
1852 PL_regcc = cc->oldcc;
1855 PerlIO_printf(Perl_debug_log,
1856 "%*s empty match detected, try continuation...\n",
1857 REPORT_CODE_OFF+PL_regindent*2, "")
1859 if (regmatch(cc->next))
1862 PerlIO_printf(Perl_debug_log,
1864 REPORT_CODE_OFF+PL_regindent*2, "")
1871 /* First just match a string of min scans. */
1875 cc->lastloc = locinput;
1876 if (regmatch(cc->scan))
1879 cc->lastloc = lastloc;
1881 PerlIO_printf(Perl_debug_log,
1883 REPORT_CODE_OFF+PL_regindent*2, "")
1888 /* Prefer next over scan for minimal matching. */
1891 PL_regcc = cc->oldcc;
1893 cp = regcppush(cc->parenfloor);
1895 if (regmatch(cc->next)) {
1897 sayYES; /* All done. */
1904 if (n >= cc->max) { /* Maximum greed exceeded? */
1905 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1906 && !(PL_reg_flags & RF_warned)) {
1907 PL_reg_flags |= RF_warned;
1908 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1909 "Complex regular subexpression recursion",
1916 PerlIO_printf(Perl_debug_log,
1917 "%*s trying longer...\n",
1918 REPORT_CODE_OFF+PL_regindent*2, "")
1920 /* Try scanning more and see if it helps. */
1921 PL_reginput = locinput;
1923 cc->lastloc = locinput;
1924 cp = regcppush(cc->parenfloor);
1926 if (regmatch(cc->scan)) {
1931 PerlIO_printf(Perl_debug_log,
1933 REPORT_CODE_OFF+PL_regindent*2, "")
1938 cc->lastloc = lastloc;
1942 /* Prefer scan over next for maximal matching. */
1944 if (n < cc->max) { /* More greed allowed? */
1945 cp = regcppush(cc->parenfloor);
1947 cc->lastloc = locinput;
1949 if (regmatch(cc->scan)) {
1954 regcppop(); /* Restore some previous $<digit>s? */
1955 PL_reginput = locinput;
1957 PerlIO_printf(Perl_debug_log,
1958 "%*s failed, try continuation...\n",
1959 REPORT_CODE_OFF+PL_regindent*2, "")
1962 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1963 && !(PL_reg_flags & RF_warned)) {
1964 PL_reg_flags |= RF_warned;
1965 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1966 "Complex regular subexpression recursion",
1970 /* Failed deeper matches of scan, so see if this one works. */
1971 PL_regcc = cc->oldcc;
1973 if (regmatch(cc->next))
1976 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1977 REPORT_CODE_OFF+PL_regindent*2, "")
1982 cc->lastloc = lastloc;
1987 next = scan + ARG(scan);
1990 inner = NEXTOPER(NEXTOPER(scan));
1993 inner = NEXTOPER(scan);
1998 if (OP(next) != c1) /* No choice. */
1999 next = inner; /* Avoid recursion. */
2001 int lastparen = *PL_reglastparen;
2005 PL_reginput = locinput;
2006 if (regmatch(inner))
2009 for (n = *PL_reglastparen; n > lastparen; n--)
2011 *PL_reglastparen = n;
2014 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
2018 inner = NEXTOPER(scan);
2019 if (c1 == BRANCHJ) {
2020 inner = NEXTOPER(inner);
2022 } while (scan != NULL && OP(scan) == c1);
2036 /* We suppose that the next guy does not need
2037 backtracking: in particular, it is of constant length,
2038 and has no parenths to influence future backrefs. */
2039 ln = ARG1(scan); /* min to match */
2040 n = ARG2(scan); /* max to match */
2041 paren = scan->flags;
2043 if (paren > PL_regsize)
2045 if (paren > *PL_reglastparen)
2046 *PL_reglastparen = paren;
2048 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2050 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2051 PL_reginput = locinput;
2054 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2056 if (ln && l == 0 && n >= ln
2057 /* In fact, this is tricky. If paren, then the
2058 fact that we did/didnot match may influence
2059 future execution. */
2060 && !(paren && ln == 0))
2062 locinput = PL_reginput;
2063 if (PL_regkind[(U8)OP(next)] == EXACT) {
2064 c1 = UCHARAT(OPERAND(next) + 1);
2065 if (OP(next) == EXACTF)
2067 else if (OP(next) == EXACTFL)
2068 c2 = PL_fold_locale[c1];
2075 /* This may be improved if l == 0. */
2076 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2077 /* If it could work, try it. */
2079 UCHARAT(PL_reginput) == c1 ||
2080 UCHARAT(PL_reginput) == c2)
2084 PL_regstartp[paren] =
2085 HOPc(PL_reginput, -l) - PL_bostr;
2086 PL_regendp[paren] = PL_reginput - PL_bostr;
2089 PL_regendp[paren] = -1;
2095 /* Couldn't or didn't -- move forward. */
2096 PL_reginput = locinput;
2097 if (regrepeat_hard(scan, 1, &l)) {
2099 locinput = PL_reginput;
2106 n = regrepeat_hard(scan, n, &l);
2107 if (n != 0 && l == 0
2108 /* In fact, this is tricky. If paren, then the
2109 fact that we did/didnot match may influence
2110 future execution. */
2111 && !(paren && ln == 0))
2113 locinput = PL_reginput;
2115 PerlIO_printf(Perl_debug_log,
2116 "%*s matched %ld times, len=%ld...\n",
2117 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2120 if (PL_regkind[(U8)OP(next)] == EXACT) {
2121 c1 = UCHARAT(OPERAND(next) + 1);
2122 if (OP(next) == EXACTF)
2124 else if (OP(next) == EXACTFL)
2125 c2 = PL_fold_locale[c1];
2134 /* If it could work, try it. */
2136 UCHARAT(PL_reginput) == c1 ||
2137 UCHARAT(PL_reginput) == c2)
2140 PerlIO_printf(Perl_debug_log,
2141 "%*s trying tail with n=%ld...\n",
2142 REPORT_CODE_OFF+PL_regindent*2, "", n)
2146 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
2147 PL_regendp[paren] = PL_reginput - PL_bostr;
2150 PL_regendp[paren] = -1;
2156 /* Couldn't or didn't -- back up. */
2158 locinput = HOPc(locinput, -l);
2159 PL_reginput = locinput;
2166 paren = scan->flags; /* Which paren to set */
2167 if (paren > PL_regsize)
2169 if (paren > *PL_reglastparen)
2170 *PL_reglastparen = paren;
2171 ln = ARG1(scan); /* min to match */
2172 n = ARG2(scan); /* max to match */
2173 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2177 ln = ARG1(scan); /* min to match */
2178 n = ARG2(scan); /* max to match */
2179 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2184 scan = NEXTOPER(scan);
2190 scan = NEXTOPER(scan);
2194 * Lookahead to avoid useless match attempts
2195 * when we know what character comes next.
2197 if (PL_regkind[(U8)OP(next)] == EXACT) {
2198 c1 = UCHARAT(OPERAND(next) + 1);
2199 if (OP(next) == EXACTF)
2201 else if (OP(next) == EXACTFL)
2202 c2 = PL_fold_locale[c1];
2208 PL_reginput = locinput;
2212 if (ln && regrepeat(scan, ln) < ln)
2214 locinput = PL_reginput;
2217 char *e = locinput + n - ln; /* Should not check after this */
2218 char *old = locinput;
2220 if (e >= PL_regeol || (n == REG_INFTY))
2223 /* Find place 'next' could work */
2225 while (locinput <= e && *locinput != c1)
2228 while (locinput <= e
2235 /* PL_reginput == old now */
2236 if (locinput != old) {
2237 ln = 1; /* Did some */
2238 if (regrepeat(scan, locinput - old) <
2242 /* PL_reginput == locinput now */
2245 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr;
2246 PL_regendp[paren] = locinput - PL_bostr;
2249 PL_regendp[paren] = -1;
2253 PL_reginput = locinput; /* Could be reset... */
2255 /* Couldn't or didn't -- move forward. */
2260 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2261 /* If it could work, try it. */
2263 UCHARAT(PL_reginput) == c1 ||
2264 UCHARAT(PL_reginput) == c2)
2268 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2269 PL_regendp[paren] = PL_reginput - PL_bostr;
2272 PL_regendp[paren] = -1;
2278 /* Couldn't or didn't -- move forward. */
2279 PL_reginput = locinput;
2280 if (regrepeat(scan, 1)) {
2282 locinput = PL_reginput;
2290 n = regrepeat(scan, n);
2291 locinput = PL_reginput;
2292 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2293 (!PL_multiline || OP(next) == SEOL))
2294 ln = n; /* why back off? */
2298 /* If it could work, try it. */
2300 UCHARAT(PL_reginput) == c1 ||
2301 UCHARAT(PL_reginput) == c2)
2305 PL_regstartp[paren] = HOPc(PL_reginput, -1) - PL_bostr;
2306 PL_regendp[paren] = PL_reginput - PL_bostr;
2309 PL_regendp[paren] = -1;
2315 /* Couldn't or didn't -- back up. */
2317 PL_reginput = locinput = HOPc(locinput, -1);
2322 /* If it could work, try it. */
2324 UCHARAT(PL_reginput) == c1 ||
2325 UCHARAT(PL_reginput) == c2)
2331 /* Couldn't or didn't -- back up. */
2333 PL_reginput = locinput = HOPc(locinput, -1);
2340 if (PL_reg_call_cc) {
2341 re_cc_state *cur_call_cc = PL_reg_call_cc;
2342 CURCUR *cctmp = PL_regcc;
2343 regexp *re = PL_reg_re;
2344 CHECKPOINT cp, lastcp;
2346 cp = regcppush(0); /* Save *all* the positions. */
2348 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2350 PL_reginput = locinput; /* Make position available to
2352 cache_re(PL_reg_call_cc->re);
2353 PL_regcc = PL_reg_call_cc->cc;
2354 PL_reg_call_cc = PL_reg_call_cc->prev;
2355 if (regmatch(cur_call_cc->node)) {
2356 PL_reg_call_cc = cur_call_cc;
2362 PL_reg_call_cc = cur_call_cc;
2368 PerlIO_printf(Perl_debug_log,
2369 "%*s continuation failed...\n",
2370 REPORT_CODE_OFF+PL_regindent*2, "")
2374 if (locinput < PL_regtill)
2375 sayNO; /* Cannot match: too short. */
2378 PL_reginput = locinput; /* put where regtry can find it */
2379 sayYES; /* Success! */
2382 PL_reginput = locinput;
2387 if (UTF) { /* XXXX This is absolutely
2388 broken, we read before
2390 s = HOPMAYBEc(locinput, -scan->flags);
2396 if (locinput < PL_bostr + scan->flags)
2398 PL_reginput = locinput - scan->flags;
2403 PL_reginput = locinput;
2408 if (UTF) { /* XXXX This is absolutely
2409 broken, we read before
2411 s = HOPMAYBEc(locinput, -scan->flags);
2412 if (!s || s < PL_bostr)
2417 if (locinput < PL_bostr + scan->flags)
2419 PL_reginput = locinput - scan->flags;
2424 PL_reginput = locinput;
2427 inner = NEXTOPER(NEXTOPER(scan));
2428 if (regmatch(inner) != n) {
2443 if (OP(scan) == SUSPEND) {
2444 locinput = PL_reginput;
2445 nextchr = UCHARAT(locinput);
2450 next = scan + ARG(scan);
2455 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2456 (unsigned long)scan, OP(scan));
2457 croak("regexp memory corruption");
2463 * We get here only if there's trouble -- normally "case END" is
2464 * the terminating point.
2466 croak("corrupted regexp pointers");
2484 - regrepeat - repeatedly match something simple, report how many
2487 * [This routine now assumes that it will only match on things of length 1.
2488 * That was true before, but now we assume scan - reginput is the count,
2489 * rather than incrementing count on every character. [Er, except utf8.]]
2492 regrepeat(pTHX_ regnode *p, I32 max)
2495 register char *scan;
2496 register char *opnd;
2498 register char *loceol = PL_regeol;
2499 register I32 hardcount = 0;
2502 if (max != REG_INFTY && max < loceol - scan)
2503 loceol = scan + max;
2504 opnd = (char *) OPERAND(p);
2507 while (scan < loceol && *scan != '\n')
2515 while (scan < loceol && *scan != '\n') {
2516 scan += UTF8SKIP(scan);
2522 while (scan < loceol) {
2523 scan += UTF8SKIP(scan);
2527 case EXACT: /* length of string is 1 */
2528 c = UCHARAT(++opnd);
2529 while (scan < loceol && UCHARAT(scan) == c)
2532 case EXACTF: /* length of string is 1 */
2533 c = UCHARAT(++opnd);
2534 while (scan < loceol &&
2535 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2538 case EXACTFL: /* length of string is 1 */
2539 PL_reg_flags |= RF_tainted;
2540 c = UCHARAT(++opnd);
2541 while (scan < loceol &&
2542 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2547 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2548 scan += UTF8SKIP(scan);
2553 while (scan < loceol && REGINCLASS(opnd, *scan))
2557 while (scan < loceol && isALNUM(*scan))
2562 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2563 scan += UTF8SKIP(scan);
2568 PL_reg_flags |= RF_tainted;
2569 while (scan < loceol && isALNUM_LC(*scan))
2573 PL_reg_flags |= RF_tainted;
2575 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2576 scan += UTF8SKIP(scan);
2582 while (scan < loceol && !isALNUM(*scan))
2587 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2588 scan += UTF8SKIP(scan);
2593 PL_reg_flags |= RF_tainted;
2594 while (scan < loceol && !isALNUM_LC(*scan))
2598 PL_reg_flags |= RF_tainted;
2600 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2601 scan += UTF8SKIP(scan);
2606 while (scan < loceol && isSPACE(*scan))
2611 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2612 scan += UTF8SKIP(scan);
2617 PL_reg_flags |= RF_tainted;
2618 while (scan < loceol && isSPACE_LC(*scan))
2622 PL_reg_flags |= RF_tainted;
2624 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2625 scan += UTF8SKIP(scan);
2630 while (scan < loceol && !isSPACE(*scan))
2635 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2636 scan += UTF8SKIP(scan);
2641 PL_reg_flags |= RF_tainted;
2642 while (scan < loceol && !isSPACE_LC(*scan))
2646 PL_reg_flags |= RF_tainted;
2648 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2649 scan += UTF8SKIP(scan);
2654 while (scan < loceol && isDIGIT(*scan))
2659 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2660 scan += UTF8SKIP(scan);
2666 while (scan < loceol && !isDIGIT(*scan))
2671 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2672 scan += UTF8SKIP(scan);
2676 default: /* Called on something of 0 width. */
2677 break; /* So match right here or not at all. */
2683 c = scan - PL_reginput;
2688 SV *prop = sv_newmortal();
2691 PerlIO_printf(Perl_debug_log,
2692 "%*s %s can match %ld times out of %ld...\n",
2693 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2700 - regrepeat_hard - repeatedly match something, report total lenth and length
2702 * The repeater is supposed to have constant length.
2706 regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
2709 register char *scan;
2710 register char *start;
2711 register char *loceol = PL_regeol;
2713 I32 count = 0, res = 1;
2718 start = PL_reginput;
2720 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2723 while (start < PL_reginput) {
2725 start += UTF8SKIP(start);
2736 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2738 *lp = l = PL_reginput - start;
2739 if (max != REG_INFTY && l*max < loceol - scan)
2740 loceol = scan + l*max;
2753 - reginclass - determine if a character falls into a character class
2757 reginclass(pTHX_ register char *p, register I32 c)
2764 if (ANYOF_TEST(p, c))
2766 else if (flags & ANYOF_FOLD) {
2768 if (flags & ANYOF_LOCALE) {
2769 PL_reg_flags |= RF_tainted;
2770 cf = PL_fold_locale[c];
2774 if (ANYOF_TEST(p, cf))
2778 if (!match && (flags & ANYOF_ISA)) {
2779 PL_reg_flags |= RF_tainted;
2781 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2782 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2783 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2784 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2790 return (flags & ANYOF_INVERT) ? !match : match;
2794 reginclassutf8(pTHX_ regnode *f, U8 *p)
2797 char flags = ARG1(f);
2799 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2801 if (swash_fetch(sv, p))
2803 else if (flags & ANYOF_FOLD) {
2806 if (flags & ANYOF_LOCALE) {
2807 PL_reg_flags |= RF_tainted;
2808 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2811 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2812 if (swash_fetch(sv, tmpbuf))
2816 if (!match && (flags & ANYOF_ISA)) {
2817 PL_reg_flags |= RF_tainted;
2819 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2820 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2821 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2822 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2828 return (flags & ANYOF_INVERT) ? !match : match;
2832 reghop(pTHX_ U8 *s, I32 off)
2836 while (off-- && s < (U8*)PL_regeol)
2841 if (s > (U8*)PL_bostr) {
2844 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2846 } /* XXX could check well-formedness here */
2854 reghopmaybe(pTHX_ U8* s, I32 off)
2858 while (off-- && s < (U8*)PL_regeol)
2865 if (s > (U8*)PL_bostr) {
2868 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2870 } /* XXX could check well-formedness here */