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
44 * pregcomp and pregexec -- regsub and regerror are not used in perl
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
63 **** Alterations to Henry's code are...
65 **** Copyright (c) 1991-1998, Larry Wall
67 **** You may distribute under the terms of either the GNU General Public
68 **** License or the Artistic License, as specified in the README file.
70 * Beware that some of this code is subtly aware of the way operator
71 * precedence is structured in regular expressions. Serious changes in
72 * regular-expression syntax might require a total rethink.
78 #define RF_tainted 1 /* tainted information used? */
79 #define RF_warned 2 /* warned about big count? */
80 #define RF_evaled 4 /* Did an EVAL with setting? */
81 #define RF_utf8 8 /* String contains multibyte chars? */
83 #define UTF (PL_reg_flags & RF_utf8)
85 #define RS_init 1 /* eval environment created */
86 #define RS_set 2 /* replsv value is set */
93 typedef I32 CHECKPOINT;
99 static I32 regmatch _((regnode *prog));
100 static I32 regrepeat _((regnode *p, I32 max));
101 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
102 static I32 regtry _((regexp *prog, char *startpos));
104 static bool reginclass _((char *p, I32 c));
105 static bool reginclassutf8 _((regnode *f, U8* p));
106 static CHECKPOINT regcppush _((I32 parenfloor));
107 static char * regcppop _((void));
110 #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
111 #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
113 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
114 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
116 static char * reghop _((unsigned char *pos, I32 off));
117 static char * reghopmaybe _((unsigned char *pos, I32 off));
118 #define HOP(pos,off) (UTF ? reghop(pos, off) : (pos + off))
119 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe(pos, off) : (pos + off))
122 regcppush(I32 parenfloor)
125 int retval = PL_savestack_ix;
126 int i = (PL_regsize - parenfloor) * 4;
130 for (p = PL_regsize; p > parenfloor; p--) {
131 SSPUSHPTR(PL_regendp[p]);
132 SSPUSHPTR(PL_regstartp[p]);
133 SSPUSHPTR(PL_reg_start_tmp[p]);
136 SSPUSHINT(PL_regsize);
137 SSPUSHINT(*PL_reglastparen);
138 SSPUSHPTR(PL_reginput);
140 SSPUSHINT(SAVEt_REGCONTEXT);
144 /* These are needed since we do not localize EVAL nodes: */
145 # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
146 " Setting an EVAL scope, savestack=%i\n", \
147 PL_savestack_ix)); lastcp = PL_savestack_ix
149 # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
150 PerlIO_printf(Perl_debug_log, \
151 " Clearing an EVAL scope, savestack=%i..%i\n", \
152 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
162 assert(i == SAVEt_REGCONTEXT);
164 input = (char *) SSPOPPTR;
165 *PL_reglastparen = SSPOPINT;
166 PL_regsize = SSPOPINT;
167 for (i -= 3; i > 0; i -= 4) {
168 paren = (U32)SSPOPINT;
169 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
170 PL_regstartp[paren] = (char *) SSPOPPTR;
171 tmps = (char*)SSPOPPTR;
172 if (paren <= *PL_reglastparen)
173 PL_regendp[paren] = tmps;
175 PerlIO_printf(Perl_debug_log,
176 " restoring \\%d to %d(%d)..%d%s\n",
177 paren, PL_regstartp[paren] - PL_regbol,
178 PL_reg_start_tmp[paren] - PL_regbol,
179 PL_regendp[paren] - PL_regbol,
180 (paren > *PL_reglastparen ? "(no)" : ""));
184 if (*PL_reglastparen + 1 <= PL_regnpar) {
185 PerlIO_printf(Perl_debug_log,
186 " restoring \\%d..\\%d to undef\n",
187 *PL_reglastparen + 1, PL_regnpar);
190 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
191 if (paren > PL_regsize)
192 PL_regstartp[paren] = Nullch;
193 PL_regendp[paren] = Nullch;
198 #define regcpblow(cp) LEAVE_SCOPE(cp)
201 * pregexec and friends
205 - pregexec - match a regexp against a string
208 pregexec(register regexp *prog, char *stringarg, register char *strend,
209 char *strbeg, I32 minend, SV *screamer, U32 nosave)
210 /* strend: pointer to null at end of string */
211 /* strbeg: real beginning of string */
212 /* minend: end of match must be >=minend after stringarg. */
213 /* nosave: For optimizations. */
216 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
217 nosave ? 0 : REXEC_COPY_STR);
221 - regexec_flags - match a regexp against a string
224 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
225 char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
226 /* strend: pointer to null at end of string */
227 /* strbeg: real beginning of string */
228 /* minend: end of match must be >=minend after stringarg. */
229 /* data: May be used for some additional optimizations. */
230 /* nosave: For optimizations. */
235 register char *startpos = stringarg;
237 I32 minlen; /* must match at least this many chars */
238 I32 dontbother = 0; /* how many characters not to try at end */
240 I32 start_shift = 0; /* Offset of the start to find
241 constant substr. */ /* CC */
242 I32 end_shift = 0; /* Same for the end. */ /* CC */
243 I32 scream_pos = -1; /* Internal iterator of scream. */
245 SV* oreplsv = GvSV(PL_replgv);
251 PL_regprecomp = prog->precomp; /* Needed for error messages. */
253 PL_regnarrate = PL_debug & 512;
254 PL_regprogram = prog->program;
258 if (prog == NULL || startpos == NULL) {
259 croak("NULL regexp parameter");
263 minlen = prog->minlen;
264 if (strend - startpos < minlen) goto phooey;
266 if (startpos == strbeg) /* is ^ valid at stringarg? */
269 PL_regprev = (U32)stringarg[-1];
270 if (!PL_multiline && PL_regprev == '\n')
271 PL_regprev = '\0'; /* force ^ to NOT match */
274 /* Check validity of program. */
275 if (UCHARAT(prog->program) != MAGIC) {
276 FAIL("corrupted regexp program");
279 PL_regnpar = prog->nparens;
283 if (prog->reganch & ROPT_UTF8)
284 PL_reg_flags |= RF_utf8;
286 /* Mark beginning of line for ^ and lookbehind. */
287 PL_regbol = startpos;
290 /* Mark end of line for $ (and such) */
293 /* see how far we have to get to not match where we matched before */
294 PL_regtill = startpos+minend;
296 /* If there is a "must appear" string, look for it. */
298 if (!(flags & REXEC_CHECKED)
299 && prog->check_substr != Nullsv &&
300 !(prog->reganch & ROPT_ANCH_GPOS) &&
301 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
302 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
305 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
306 /* Should be nonnegative! */
307 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
309 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
310 s = screaminstr(screamer, prog->check_substr,
311 start_shift + (stringarg - strbeg),
312 end_shift, &scream_pos, 0);
318 s = fbm_instr((unsigned char*)s + start_shift,
319 (unsigned char*)strend - end_shift,
320 prog->check_substr, 0);
322 ++BmUSEFUL(prog->check_substr); /* hooray */
323 goto phooey; /* not present */
325 else if (s - stringarg > prog->check_offset_max &&
327 ? ((t = reghopmaybe(s, -(prog->check_offset_max))) && t >= stringarg)
328 : (t = s - prog->check_offset_max) != 0
332 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
335 else if (!(prog->reganch & ROPT_NAUGHTY)
336 && --BmUSEFUL(prog->check_substr) < 0
337 && prog->check_substr == prog->float_substr) { /* boo */
338 SvREFCNT_dec(prog->check_substr);
339 prog->check_substr = Nullsv; /* disable */
340 prog->float_substr = Nullsv; /* clear */
348 PerlIO_printf(Perl_debug_log,
349 "Matching `%.60s%s' against `%.*s%s'\n",
351 (strlen(prog->precomp) > 60 ? "..." : ""),
352 (strend - startpos > 60 ? 60 : strend - startpos),
354 (strend - startpos > 60 ? "..." : ""))
357 PL_regdata = prog->data;
359 /* Simplest case: anchored match need be tried only once. */
360 /* [unless only anchor is BOL and multiline is set] */
361 if (prog->reganch & ROPT_ANCH) {
362 if (regtry(prog, startpos))
364 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
365 (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
366 || (prog->reganch & ROPT_ANCH_MBOL)))
369 dontbother = minlen - 1;
370 strend = HOP(strend, -dontbother);
371 /* for multiline we only have to try after newlines */
375 if (*s++ == '\n') { /* don't need utf8skip here */
376 if (s < strend && regtry(prog, s))
384 /* Messy cases: unanchored match. */
385 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
386 /* we have /x+whatever/ */
387 /* it must be a one character string */
388 char ch = SvPVX(prog->anchored_substr)[0];
392 if (regtry(prog, s)) goto got_it;
394 while (s < strend && *s == ch)
403 if (regtry(prog, s)) goto got_it;
405 while (s < strend && *s == ch)
413 else if (prog->anchored_substr != Nullsv
414 || (prog->float_substr != Nullsv
415 && prog->float_max_offset < strend - s)) {
416 SV *must = prog->anchored_substr
417 ? prog->anchored_substr : prog->float_substr;
419 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
421 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
422 I32 delta = back_max - back_min;
423 char *last = HOP(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
424 char *last1; /* Last position checked before */
429 last1 = s - 1; /* bogus */
431 /* XXXX check_substr already used to find `s', can optimize if
432 check_substr==must. */
434 dontbother = end_shift;
435 strend = HOP(strend, -dontbother);
436 while ( (s <= last) &&
438 ? (s = screaminstr(screamer, must, HOP(s, back_min) - strbeg,
439 end_shift, &scream_pos, 0))
440 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
441 (unsigned char*)strend, must, 0))) ) {
442 if (HOP(s, -back_max) > last1) {
443 last1 = HOP(s, -back_min);
444 s = HOP(s, -back_max);
447 char *t = (last1 >= PL_bostr) ? HOP(last1, 1) : last + 1;
449 last1 = HOP(s, -back_min);
469 else if (c = prog->regstclass) {
470 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
474 dontbother = minlen - 1;
475 strend = HOP(strend, -dontbother); /* don't bother with what can't match */
477 /* We know what class it must start with. */
480 cc = (char *) OPERAND(c);
482 if (REGINCLASSUTF8(c, (U8*)s)) {
483 if (tmp && regtry(prog, s))
494 cc = (char *) OPERAND(c);
496 if (REGINCLASS(cc, *s)) {
497 if (tmp && regtry(prog, s))
508 PL_reg_flags |= RF_tainted;
515 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
516 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
518 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
525 if ((minlen || tmp) && regtry(prog,s))
529 PL_reg_flags |= RF_tainted;
534 strend = reghop(strend, -1);
536 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop(s, -1), 0) : PL_regprev;
537 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
539 if (tmp == !(OP(c) == BOUND ? swash_fetch(PL_utf8_alnum, s) : isALNUM_LC_utf8(s))) {
546 if ((minlen || tmp) && regtry(prog,s))
550 PL_reg_flags |= RF_tainted;
557 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
558 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
560 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
562 else if (regtry(prog, s))
566 if ((minlen || !tmp) && regtry(prog,s))
570 PL_reg_flags |= RF_tainted;
575 strend = reghop(strend, -1);
577 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop(s, -1), 0) : PL_regprev;
578 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
580 if (tmp == !(OP(c) == NBOUND ? swash_fetch(PL_utf8_alnum, s) : isALNUM_LC_utf8(s)))
582 else if (regtry(prog, s))
586 if ((minlen || !tmp) && regtry(prog,s))
592 if (tmp && regtry(prog, s))
604 if (swash_fetch(PL_utf8_alnum, s)) {
605 if (tmp && regtry(prog, s))
616 PL_reg_flags |= RF_tainted;
618 if (isALNUM_LC(*s)) {
619 if (tmp && regtry(prog, s))
630 PL_reg_flags |= RF_tainted;
632 if (isALNUM_LC_utf8(s)) {
633 if (tmp && regtry(prog, s))
646 if (tmp && regtry(prog, s))
658 if (!swash_fetch(PL_utf8_alnum, s)) {
659 if (tmp && regtry(prog, s))
670 PL_reg_flags |= RF_tainted;
672 if (!isALNUM_LC(*s)) {
673 if (tmp && regtry(prog, s))
684 PL_reg_flags |= RF_tainted;
686 if (!isALNUM_LC_utf8(s)) {
687 if (tmp && regtry(prog, s))
700 if (tmp && regtry(prog, s))
712 if (*s == ' ' || swash_fetch(PL_utf8_space,s)) {
713 if (tmp && regtry(prog, s))
724 PL_reg_flags |= RF_tainted;
726 if (isSPACE_LC(*s)) {
727 if (tmp && regtry(prog, s))
738 PL_reg_flags |= RF_tainted;
740 if (*s == ' ' || isSPACE_LC_utf8(s)) {
741 if (tmp && regtry(prog, s))
754 if (tmp && regtry(prog, s))
766 if (!(*s == ' ' || swash_fetch(PL_utf8_space,s))) {
767 if (tmp && regtry(prog, s))
778 PL_reg_flags |= RF_tainted;
780 if (!isSPACE_LC(*s)) {
781 if (tmp && regtry(prog, s))
792 PL_reg_flags |= RF_tainted;
794 if (!(*s == ' ' || isSPACE_LC_utf8(s))) {
795 if (tmp && regtry(prog, s))
808 if (tmp && regtry(prog, s))
820 if (swash_fetch(PL_utf8_digit,s)) {
821 if (tmp && regtry(prog, s))
834 if (tmp && regtry(prog, s))
846 if (!swash_fetch(PL_utf8_digit,s)) {
847 if (tmp && regtry(prog, s))
861 if (prog->float_substr != Nullsv) { /* Trim the end. */
863 I32 oldpos = scream_pos;
866 last = screaminstr(screamer, prog->float_substr, s - strbeg,
867 end_shift, &scream_pos, 1); /* last one */
869 last = scream_olds; /* Only one occurence. */
874 char *little = SvPV(prog->float_substr, len);
875 last = rninstr(s, strend, little, little + len);
877 if (last == NULL) goto phooey; /* Should not happen! */
878 dontbother = strend - last - 1;
880 if (minlen && (dontbother < minlen))
881 dontbother = minlen - 1;
882 strend -= dontbother; /* this one's always in bytes! */
883 /* We don't know much -- general case. */
886 if (regtry(prog, s)) {
887 strend += dontbother; /* this one's always in bytes! */
900 } while (s++ < strend);
908 strend = HOP(strend, dontbother); /* uncheat */
909 prog->subbeg = strbeg;
910 prog->subend = strend;
911 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
913 /* make sure $`, $&, $', and $digit will work later */
914 if (strbeg != prog->subbase) { /* second+ //g match. */
915 if (!(flags & REXEC_COPY_STR)) {
917 Safefree(prog->subbase);
918 prog->subbase = Nullch;
922 I32 i = strend - startpos + (stringarg - strbeg);
923 s = savepvn(strbeg, i);
924 Safefree(prog->subbase);
926 prog->subbeg = prog->subbase;
927 prog->subend = prog->subbase + i;
928 s = prog->subbase + (stringarg - strbeg);
929 for (i = 0; i <= prog->nparens; i++) {
931 prog->startp[i] = s + (prog->startp[i] - startpos);
932 prog->endp[i] = s + (prog->endp[i] - startpos);
937 /* Preserve the current value of $^R */
938 if (oreplsv != GvSV(PL_replgv)) {
939 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
940 restored, the value remains
950 - regtry - try match at specific point
952 STATIC I32 /* 0 failure, 1 success */
953 regtry(regexp *prog, char *startpos)
961 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
962 PL_reg_eval_set = RS_init;
964 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
965 PL_stack_sp - PL_stack_base);
967 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
968 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
969 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
971 /* Apparently this is not needed, judging by wantarray. */
972 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
973 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
975 PL_reginput = startpos;
976 PL_regstartp = prog->startp;
977 PL_regendp = prog->endp;
978 PL_reglastparen = &prog->lastparen;
981 if (PL_reg_start_tmpl <= prog->nparens) {
982 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
984 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
986 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
992 for (i = prog->nparens; i >= 0; i--) {
998 if (regmatch(prog->program + 1)) {
999 prog->startp[0] = startpos;
1000 prog->endp[0] = PL_reginput;
1008 - regmatch - main matching routine
1010 * Conceptually the strategy is simple: check to see whether the current
1011 * node matches, call self recursively to see whether the rest matches,
1012 * and then act accordingly. In practice we make some effort to avoid
1013 * recursion, in particular by going through "ordinary" nodes (that don't
1014 * need to know whether the rest of the match failed) by a loop instead of
1017 /* [lwall] I've hoisted the register declarations to the outer block in order to
1018 * maybe save a little bit of pushing and popping on the stack. It also takes
1019 * advantage of machines that use a register save mask on subroutine entry.
1021 STATIC I32 /* 0 failure, 1 success */
1022 regmatch(regnode *prog)
1025 register regnode *scan; /* Current node. */
1026 regnode *next; /* Next node. */
1027 regnode *inner; /* Next node in internal branch. */
1028 register I32 nextchr; /* renamed nextchr - nextchar colides with
1029 function of same name */
1030 register I32 n; /* no or next */
1031 register I32 ln; /* len or last */
1032 register char *s; /* operand or save */
1033 register char *locinput = PL_reginput;
1034 register I32 c1, c2, paren; /* case fold search, parenth */
1035 int minmod = 0, sw = 0, logical = 0;
1040 /* Note that nextchr is a byte even in UTF */
1041 nextchr = UCHARAT(locinput);
1043 while (scan != NULL) {
1044 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1046 # define sayYES goto yes
1047 # define sayNO goto no
1048 # define saySAME(x) if (x) goto yes; else goto no
1049 # define REPORT_CODE_OFF 24
1051 # define sayYES return 1
1052 # define sayNO return 0
1053 # define saySAME(x) return x
1056 SV *prop = sv_newmortal();
1057 int docolor = *PL_colors[0];
1058 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1059 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1060 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1061 ? (5 + taill) - l : locinput - PL_bostr);
1063 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1064 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1065 ? (5 + taill) - pref_len : PL_regeol - locinput);
1066 regprop(prop, scan);
1067 PerlIO_printf(Perl_debug_log,
1068 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1069 locinput - PL_bostr,
1070 PL_colors[2], pref_len, locinput - pref_len, PL_colors[3],
1071 (docolor ? "" : "> <"),
1072 PL_colors[0], l, locinput, PL_colors[1],
1073 15 - l - pref_len + 1,
1075 scan - PL_regprogram, PL_regindent*2, "",
1079 next = scan + NEXT_OFF(scan);
1085 if (locinput == PL_bostr
1086 ? PL_regprev == '\n'
1088 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1090 /* regtill = regbol; */
1095 if (locinput == PL_bostr
1096 ? PL_regprev == '\n'
1097 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1103 if (locinput == PL_regbol && PL_regprev == '\n')
1107 if (locinput == PL_regbol)
1117 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1122 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1124 if (PL_regeol - locinput > 1)
1128 if (PL_regeol != locinput)
1132 if (nextchr & 0x80) {
1133 locinput += utf8skip[nextchr];
1134 if (locinput > PL_regeol)
1136 nextchr = UCHARAT(locinput);
1139 if (!nextchr && locinput >= PL_regeol)
1141 nextchr = UCHARAT(++locinput);
1144 if (!nextchr && locinput >= PL_regeol)
1146 nextchr = UCHARAT(++locinput);
1149 if (nextchr & 0x80) {
1150 locinput += utf8skip[nextchr];
1151 if (locinput > PL_regeol)
1153 nextchr = UCHARAT(locinput);
1156 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1158 nextchr = UCHARAT(++locinput);
1161 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1163 nextchr = UCHARAT(++locinput);
1166 s = (char *) OPERAND(scan);
1168 /* Inline the first character, for speed. */
1169 if (UCHARAT(s) != nextchr)
1171 if (PL_regeol - locinput < ln)
1173 if (ln > 1 && memNE(s, locinput, ln))
1176 nextchr = UCHARAT(locinput);
1179 PL_reg_flags |= RF_tainted;
1182 s = (char *) OPERAND(scan);
1188 c1 = OP(scan) == EXACTF;
1192 if (utf8_to_uv(s, 0) != (c1 ? toLOWER_utf8(l) : toLOWER_LC_utf8(l)))
1198 nextchr = UCHARAT(locinput);
1202 /* Inline the first character, for speed. */
1203 if (UCHARAT(s) != nextchr &&
1204 UCHARAT(s) != ((OP(scan) == EXACTF)
1205 ? fold : fold_locale)[nextchr])
1207 if (PL_regeol - locinput < ln)
1209 if (ln > 1 && (OP(scan) == EXACTF
1210 ? ibcmp(s, locinput, ln)
1211 : ibcmp_locale(s, locinput, ln)))
1214 nextchr = UCHARAT(locinput);
1217 s = (char *) OPERAND(scan);
1218 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1220 if (locinput >= PL_regeol)
1222 locinput += utf8skip[nextchr];
1223 nextchr = UCHARAT(locinput);
1226 s = (char *) OPERAND(scan);
1228 nextchr = UCHARAT(locinput);
1229 if (!REGINCLASS(s, nextchr))
1231 if (!nextchr && locinput >= PL_regeol)
1233 nextchr = UCHARAT(++locinput);
1236 PL_reg_flags |= RF_tainted;
1241 if (!(OP(scan) == ALNUM
1242 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1244 nextchr = UCHARAT(++locinput);
1247 PL_reg_flags |= RF_tainted;
1252 if (nextchr & 0x80) {
1253 if (!(OP(scan) == ALNUMUTF8
1254 ? swash_fetch(PL_utf8_alnum, locinput) : isALNUM_LC_utf8(locinput)))
1256 locinput += utf8skip[nextchr];
1257 nextchr = UCHARAT(locinput);
1260 if (!(OP(scan) == ALNUMUTF8
1261 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1263 nextchr = UCHARAT(++locinput);
1266 PL_reg_flags |= RF_tainted;
1269 if (!nextchr && locinput >= PL_regeol)
1271 if (OP(scan) == NALNUM
1272 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1274 nextchr = UCHARAT(++locinput);
1277 PL_reg_flags |= RF_tainted;
1280 if (!nextchr && locinput >= PL_regeol)
1282 if (nextchr & 0x80) {
1283 if (OP(scan) == NALNUMUTF8
1284 ? swash_fetch(PL_utf8_alnum, locinput) : isALNUM_LC_utf8(locinput))
1286 locinput += utf8skip[nextchr];
1287 nextchr = UCHARAT(locinput);
1290 if (OP(scan) == NALNUMUTF8
1291 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1293 nextchr = UCHARAT(++locinput);
1297 PL_reg_flags |= RF_tainted;
1301 /* was last char in word? */
1302 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1303 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1305 n = isALNUM(nextchr);
1308 ln = isALNUM_LC(ln);
1309 n = isALNUM_LC(nextchr);
1311 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1316 PL_reg_flags |= RF_tainted;
1320 /* was last char in word? */
1321 ln = (locinput != PL_regbol) ? utf8_to_uv(reghop(locinput, -1), 0) : PL_regprev;
1322 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1323 ln = isALNUM_uni(ln);
1324 n = swash_fetch(PL_utf8_alnum, locinput);
1327 ln = isALNUM_LC_uni(ln);
1328 n = isALNUM_LC_utf8(locinput);
1330 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1334 PL_reg_flags |= RF_tainted;
1337 if (!nextchr && locinput >= PL_regeol)
1339 if (!(OP(scan) == SPACE
1340 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1342 nextchr = UCHARAT(++locinput);
1345 PL_reg_flags |= RF_tainted;
1348 if (!nextchr && locinput >= PL_regeol)
1350 if (nextchr & 0x80) {
1351 if (!(OP(scan) == SPACEUTF8
1352 ? swash_fetch(PL_utf8_space,locinput) : isSPACE_LC_utf8(locinput)))
1354 locinput += utf8skip[nextchr];
1355 nextchr = UCHARAT(locinput);
1358 if (!(OP(scan) == SPACEUTF8
1359 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1361 nextchr = UCHARAT(++locinput);
1364 PL_reg_flags |= RF_tainted;
1369 if (OP(scan) == SPACE
1370 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1372 nextchr = UCHARAT(++locinput);
1375 PL_reg_flags |= RF_tainted;
1380 if (nextchr & 0x80) {
1381 if (OP(scan) == NSPACEUTF8
1382 ? swash_fetch(PL_utf8_space,locinput) : isSPACE_LC_utf8(locinput))
1384 locinput += utf8skip[nextchr];
1385 nextchr = UCHARAT(locinput);
1388 if (OP(scan) == NSPACEUTF8
1389 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1391 nextchr = UCHARAT(++locinput);
1394 if (!isDIGIT(nextchr))
1396 nextchr = UCHARAT(++locinput);
1399 if (nextchr & 0x80) {
1400 if (!(swash_fetch(PL_utf8_digit,locinput)))
1402 locinput += utf8skip[nextchr];
1403 nextchr = UCHARAT(locinput);
1406 if (!isDIGIT(nextchr))
1408 nextchr = UCHARAT(++locinput);
1411 if (!nextchr && locinput >= PL_regeol)
1413 if (isDIGIT(nextchr))
1415 nextchr = UCHARAT(++locinput);
1418 if (!nextchr && locinput >= PL_regeol)
1420 if (nextchr & 0x80) {
1421 if (swash_fetch(PL_utf8_digit,locinput))
1423 locinput += utf8skip[nextchr];
1424 nextchr = UCHARAT(locinput);
1427 if (isDIGIT(nextchr))
1429 nextchr = UCHARAT(++locinput);
1432 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark, locinput))
1434 locinput += utf8skip[nextchr];
1435 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark, locinput))
1436 locinput += UTF8SKIP(locinput);
1437 if (locinput > PL_regeol)
1439 nextchr = UCHARAT(locinput);
1442 PL_reg_flags |= RF_tainted;
1446 n = ARG(scan); /* which paren pair */
1447 s = PL_regstartp[n];
1448 if (*PL_reglastparen < n || !s)
1449 sayNO; /* Do not match unless seen CLOSEn. */
1450 if (s == PL_regendp[n])
1453 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1455 char *e = PL_regendp[n];
1457 * Note that we can't do the "other character" lookup trick as
1458 * in the 8-bit case (no pun intended) because in Unicode we
1459 * have to map both upper and title case to lower case.
1461 if (OP(scan) == REFF) {
1465 if (toLOWER_utf8(s) != toLOWER_utf8(l))
1475 if (toLOWER_LC_utf8(s) != toLOWER_LC_utf8(l))
1482 nextchr = UCHARAT(locinput);
1486 /* Inline the first character, for speed. */
1487 if (UCHARAT(s) != nextchr &&
1489 (UCHARAT(s) != ((OP(scan) == REFF
1490 ? fold : fold_locale)[nextchr]))))
1492 ln = PL_regendp[n] - s;
1493 if (locinput + ln > PL_regeol)
1495 if (ln > 1 && (OP(scan) == REF
1496 ? memNE(s, locinput, ln)
1498 ? ibcmp(s, locinput, ln)
1499 : ibcmp_locale(s, locinput, ln))))
1502 nextchr = UCHARAT(locinput);
1513 OP_4tree *oop = PL_op;
1514 COP *ocurcop = PL_curcop;
1515 SV **ocurpad = PL_curpad;
1519 PL_op = (OP_4tree*)PL_regdata->data[n];
1520 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
1521 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1523 CALLRUNOPS(); /* Scalar context. */
1533 sv_setsv(save_scalar(PL_replgv), ret);
1535 PL_curpad = ocurpad;
1536 PL_curcop = ocurcop;
1540 n = ARG(scan); /* which paren pair */
1541 PL_reg_start_tmp[n] = locinput;
1546 n = ARG(scan); /* which paren pair */
1547 PL_regstartp[n] = PL_reg_start_tmp[n];
1548 PL_regendp[n] = locinput;
1549 if (n > *PL_reglastparen)
1550 *PL_reglastparen = n;
1553 n = ARG(scan); /* which paren pair */
1554 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1558 next = NEXTOPER(NEXTOPER(scan));
1560 next = scan + ARG(scan);
1561 if (OP(next) == IFTHEN) /* Fake one. */
1562 next = NEXTOPER(NEXTOPER(next));
1570 CHECKPOINT cp = PL_savestack_ix;
1572 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1574 cc.oldcc = PL_regcc;
1576 cc.parenfloor = *PL_reglastparen;
1578 cc.min = ARG1(scan);
1579 cc.max = ARG2(scan);
1580 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1584 PL_reginput = locinput;
1585 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1587 PL_regcc = cc.oldcc;
1593 * This is really hard to understand, because after we match
1594 * what we're trying to match, we must make sure the rest of
1595 * the RE is going to match for sure, and to do that we have
1596 * to go back UP the parse tree by recursing ever deeper. And
1597 * if it fails, we have to reset our parent's current state
1598 * that we can try again after backing off.
1601 CHECKPOINT cp, lastcp;
1602 CURCUR* cc = PL_regcc;
1603 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1605 n = cc->cur + 1; /* how many we know we matched */
1606 PL_reginput = locinput;
1609 PerlIO_printf(Perl_debug_log,
1610 "%*s %ld out of %ld..%ld cc=%lx\n",
1611 REPORT_CODE_OFF+PL_regindent*2, "",
1612 (long)n, (long)cc->min,
1613 (long)cc->max, (long)cc)
1616 /* If degenerate scan matches "", assume scan done. */
1618 if (locinput == cc->lastloc && n >= cc->min) {
1619 PL_regcc = cc->oldcc;
1622 PerlIO_printf(Perl_debug_log,
1623 "%*s empty match detected, try continuation...\n",
1624 REPORT_CODE_OFF+PL_regindent*2, "")
1626 if (regmatch(cc->next))
1629 PerlIO_printf(Perl_debug_log,
1631 REPORT_CODE_OFF+PL_regindent*2, "")
1638 /* First just match a string of min scans. */
1642 cc->lastloc = locinput;
1643 if (regmatch(cc->scan))
1646 cc->lastloc = lastloc;
1648 PerlIO_printf(Perl_debug_log,
1650 REPORT_CODE_OFF+PL_regindent*2, "")
1655 /* Prefer next over scan for minimal matching. */
1658 PL_regcc = cc->oldcc;
1660 cp = regcppush(cc->parenfloor);
1662 if (regmatch(cc->next)) {
1664 sayYES; /* All done. */
1671 if (n >= cc->max) { /* Maximum greed exceeded? */
1672 if (PL_dowarn && n >= REG_INFTY
1673 && !(PL_reg_flags & RF_warned)) {
1674 PL_reg_flags |= RF_warned;
1675 warn("%s limit (%d) exceeded",
1676 "Complex regular subexpression recursion",
1683 PerlIO_printf(Perl_debug_log,
1684 "%*s trying longer...\n",
1685 REPORT_CODE_OFF+PL_regindent*2, "")
1687 /* Try scanning more and see if it helps. */
1688 PL_reginput = locinput;
1690 cc->lastloc = locinput;
1691 cp = regcppush(cc->parenfloor);
1693 if (regmatch(cc->scan)) {
1698 PerlIO_printf(Perl_debug_log,
1700 REPORT_CODE_OFF+PL_regindent*2, "")
1705 cc->lastloc = lastloc;
1709 /* Prefer scan over next for maximal matching. */
1711 if (n < cc->max) { /* More greed allowed? */
1712 cp = regcppush(cc->parenfloor);
1714 cc->lastloc = locinput;
1716 if (regmatch(cc->scan)) {
1721 regcppop(); /* Restore some previous $<digit>s? */
1722 PL_reginput = locinput;
1724 PerlIO_printf(Perl_debug_log,
1725 "%*s failed, try continuation...\n",
1726 REPORT_CODE_OFF+PL_regindent*2, "")
1729 if (PL_dowarn && n >= REG_INFTY && !(PL_reg_flags & RF_warned)) {
1730 PL_reg_flags |= RF_warned;
1731 warn("count exceeded %d", REG_INFTY - 1);
1734 /* Failed deeper matches of scan, so see if this one works. */
1735 PL_regcc = cc->oldcc;
1737 if (regmatch(cc->next))
1740 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
1741 REPORT_CODE_OFF+PL_regindent*2, "")
1746 cc->lastloc = lastloc;
1751 next = scan + ARG(scan);
1754 inner = NEXTOPER(NEXTOPER(scan));
1757 inner = NEXTOPER(scan);
1762 if (OP(next) != c1) /* No choice. */
1763 next = inner; /* Avoid recursion. */
1765 int lastparen = *PL_reglastparen;
1769 PL_reginput = locinput;
1770 if (regmatch(inner))
1773 for (n = *PL_reglastparen; n > lastparen; n--)
1775 *PL_reglastparen = n;
1778 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1782 inner = NEXTOPER(scan);
1783 if (c1 == BRANCHJ) {
1784 inner = NEXTOPER(inner);
1786 } while (scan != NULL && OP(scan) == c1);
1800 /* We suppose that the next guy does not need
1801 backtracking: in particular, it is of constant length,
1802 and has no parenths to influence future backrefs. */
1803 ln = ARG1(scan); /* min to match */
1804 n = ARG2(scan); /* max to match */
1805 paren = scan->flags;
1807 if (paren > PL_regsize)
1809 if (paren > *PL_reglastparen)
1810 *PL_reglastparen = paren;
1812 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1814 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1815 PL_reginput = locinput;
1818 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1820 if (ln && l == 0 && n >= ln
1821 /* In fact, this is tricky. If paren, then the
1822 fact that we did/didnot match may influence
1823 future execution. */
1824 && !(paren && ln == 0))
1826 locinput = PL_reginput;
1827 if (regkind[(U8)OP(next)] == EXACT) {
1828 c1 = UCHARAT(OPERAND(next) + 1);
1829 if (OP(next) == EXACTF)
1831 else if (OP(next) == EXACTFL)
1832 c2 = fold_locale[c1];
1839 /* This may be improved if l == 0. */
1840 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1841 /* If it could work, try it. */
1843 UCHARAT(PL_reginput) == c1 ||
1844 UCHARAT(PL_reginput) == c2)
1848 PL_regstartp[paren] = HOP(PL_reginput, -l);
1849 PL_regendp[paren] = PL_reginput;
1852 PL_regendp[paren] = NULL;
1858 /* Couldn't or didn't -- move forward. */
1859 PL_reginput = locinput;
1860 if (regrepeat_hard(scan, 1, &l)) {
1862 locinput = PL_reginput;
1869 n = regrepeat_hard(scan, n, &l);
1870 if (n != 0 && l == 0
1871 /* In fact, this is tricky. If paren, then the
1872 fact that we did/didnot match may influence
1873 future execution. */
1874 && !(paren && ln == 0))
1876 locinput = PL_reginput;
1878 PerlIO_printf(Perl_debug_log,
1879 "%*s matched %ld times, len=%ld...\n",
1880 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
1883 if (regkind[(U8)OP(next)] == EXACT) {
1884 c1 = UCHARAT(OPERAND(next) + 1);
1885 if (OP(next) == EXACTF)
1887 else if (OP(next) == EXACTFL)
1888 c2 = fold_locale[c1];
1897 /* If it could work, try it. */
1899 UCHARAT(PL_reginput) == c1 ||
1900 UCHARAT(PL_reginput) == c2)
1903 PerlIO_printf(Perl_debug_log,
1904 "%*s trying tail with n=%ld...\n",
1905 REPORT_CODE_OFF+PL_regindent*2, "", n)
1909 PL_regstartp[paren] = HOP(PL_reginput, -l);
1910 PL_regendp[paren] = PL_reginput;
1913 PL_regendp[paren] = NULL;
1919 /* Couldn't or didn't -- back up. */
1921 locinput = HOP(locinput, -l);
1922 PL_reginput = locinput;
1929 paren = scan->flags; /* Which paren to set */
1930 if (paren > PL_regsize)
1932 if (paren > *PL_reglastparen)
1933 *PL_reglastparen = paren;
1934 ln = ARG1(scan); /* min to match */
1935 n = ARG2(scan); /* max to match */
1936 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
1940 ln = ARG1(scan); /* min to match */
1941 n = ARG2(scan); /* max to match */
1942 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1947 scan = NEXTOPER(scan);
1953 scan = NEXTOPER(scan);
1957 * Lookahead to avoid useless match attempts
1958 * when we know what character comes next.
1960 if (regkind[(U8)OP(next)] == EXACT) {
1961 c1 = UCHARAT(OPERAND(next) + 1);
1962 if (OP(next) == EXACTF)
1964 else if (OP(next) == EXACTFL)
1965 c2 = fold_locale[c1];
1971 PL_reginput = locinput;
1975 if (ln && regrepeat(scan, ln) < ln)
1977 locinput = PL_reginput;
1979 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1980 /* If it could work, try it. */
1982 UCHARAT(PL_reginput) == c1 ||
1983 UCHARAT(PL_reginput) == c2)
1987 PL_regstartp[paren] = HOP(PL_reginput, -1);
1988 PL_regendp[paren] = PL_reginput;
1991 PL_regendp[paren] = NULL;
1997 /* Couldn't or didn't -- move forward. */
1998 PL_reginput = locinput;
1999 if (regrepeat(scan, 1)) {
2001 locinput = PL_reginput;
2009 n = regrepeat(scan, n);
2010 locinput = PL_reginput;
2011 if (ln < n && regkind[(U8)OP(next)] == EOL &&
2012 (!PL_multiline || OP(next) == SEOL))
2013 ln = n; /* why back off? */
2017 /* If it could work, try it. */
2019 UCHARAT(PL_reginput) == c1 ||
2020 UCHARAT(PL_reginput) == c2)
2024 PL_regstartp[paren] = HOP(PL_reginput, -1);
2025 PL_regendp[paren] = PL_reginput;
2028 PL_regendp[paren] = NULL;
2034 /* Couldn't or didn't -- back up. */
2036 PL_reginput = locinput = HOP(locinput, -1);
2041 /* If it could work, try it. */
2043 UCHARAT(PL_reginput) == c1 ||
2044 UCHARAT(PL_reginput) == c2)
2050 /* Couldn't or didn't -- back up. */
2052 PL_reginput = locinput = HOP(locinput, -1);
2059 if (locinput < PL_regtill)
2060 sayNO; /* Cannot match: too short. */
2063 PL_reginput = locinput; /* put where regtry can find it */
2064 sayYES; /* Success! */
2071 s = HOPMAYBE(locinput, -scan->flags);
2077 PL_reginput = locinput;
2082 s = HOPMAYBE(locinput, -scan->flags);
2088 PL_reginput = locinput;
2091 inner = NEXTOPER(NEXTOPER(scan));
2092 if (regmatch(inner) != n) {
2107 if (OP(scan) == SUSPEND) {
2108 locinput = PL_reginput;
2109 nextchr = UCHARAT(locinput);
2114 next = scan + ARG(scan);
2119 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2120 (unsigned long)scan, OP(scan));
2121 FAIL("regexp memory corruption");
2127 * We get here only if there's trouble -- normally "case END" is
2128 * the terminating point.
2130 FAIL("corrupted regexp pointers");
2148 - regrepeat - repeatedly match something simple, report how many
2151 * [This routine now assumes that it will only match on things of length 1.
2152 * That was true before, but now we assume scan - reginput is the count,
2153 * rather than incrementing count on every character. [Er, except utf8.]]
2156 regrepeat(regnode *p, I32 max)
2159 register char *scan;
2160 register char *opnd;
2162 register char *loceol = PL_regeol;
2163 register I32 hardcount = 0;
2166 if (max != REG_INFTY && max < loceol - scan)
2167 loceol = scan + max;
2168 opnd = (char *) OPERAND(p);
2171 while (scan < loceol && *scan != '\n')
2179 while (scan < loceol && *scan != '\n') {
2180 scan += UTF8SKIP(scan);
2186 while (scan < loceol) {
2187 scan += UTF8SKIP(scan);
2191 case EXACT: /* length of string is 1 */
2192 c = UCHARAT(++opnd);
2193 while (scan < loceol && UCHARAT(scan) == c)
2196 case EXACTF: /* length of string is 1 */
2197 c = UCHARAT(++opnd);
2198 while (scan < loceol &&
2199 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
2202 case EXACTFL: /* length of string is 1 */
2203 PL_reg_flags |= RF_tainted;
2204 c = UCHARAT(++opnd);
2205 while (scan < loceol &&
2206 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
2211 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2212 scan += UTF8SKIP(scan);
2217 while (scan < loceol && REGINCLASS(opnd, *scan))
2221 while (scan < loceol && isALNUM(*scan))
2226 while (scan < loceol && swash_fetch(PL_utf8_alnum, scan)) {
2227 scan += UTF8SKIP(scan);
2232 PL_reg_flags |= RF_tainted;
2233 while (scan < loceol && isALNUM_LC(*scan))
2237 PL_reg_flags |= RF_tainted;
2239 while (scan < loceol && isALNUM_LC_utf8(scan)) {
2240 scan += UTF8SKIP(scan);
2246 while (scan < loceol && !isALNUM(*scan))
2251 while (scan < loceol && !swash_fetch(PL_utf8_alnum, scan)) {
2252 scan += UTF8SKIP(scan);
2257 PL_reg_flags |= RF_tainted;
2258 while (scan < loceol && !isALNUM_LC(*scan))
2262 PL_reg_flags |= RF_tainted;
2264 while (scan < loceol && !isALNUM_LC_utf8(scan)) {
2265 scan += UTF8SKIP(scan);
2270 while (scan < loceol && isSPACE(*scan))
2275 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,scan))) {
2276 scan += UTF8SKIP(scan);
2281 PL_reg_flags |= RF_tainted;
2282 while (scan < loceol && isSPACE_LC(*scan))
2286 PL_reg_flags |= RF_tainted;
2288 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8(scan))) {
2289 scan += UTF8SKIP(scan);
2294 while (scan < loceol && !isSPACE(*scan))
2299 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,scan))) {
2300 scan += UTF8SKIP(scan);
2305 PL_reg_flags |= RF_tainted;
2306 while (scan < loceol && !isSPACE_LC(*scan))
2310 PL_reg_flags |= RF_tainted;
2312 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8(scan))) {
2313 scan += UTF8SKIP(scan);
2318 while (scan < loceol && isDIGIT(*scan))
2323 while (scan < loceol && swash_fetch(PL_utf8_digit,scan)) {
2324 scan += UTF8SKIP(scan);
2330 while (scan < loceol && !isDIGIT(*scan))
2335 while (scan < loceol && !swash_fetch(PL_utf8_digit,scan)) {
2336 scan += UTF8SKIP(scan);
2340 default: /* Called on something of 0 width. */
2341 break; /* So match right here or not at all. */
2347 c = scan - PL_reginput;
2352 SV *prop = sv_newmortal();
2355 PerlIO_printf(Perl_debug_log,
2356 "%*s %s can match %ld times out of %ld...\n",
2357 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2364 - regrepeat_hard - repeatedly match something, report total lenth and length
2366 * The repeater is supposed to have constant length.
2370 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2373 register char *scan;
2374 register char *start;
2375 register char *loceol = PL_regeol;
2382 start = PL_reginput;
2384 while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) {
2387 while (start < PL_reginput) {
2389 start += UTF8SKIP(start);
2400 while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) {
2402 *lp = l = PL_reginput - start;
2403 if (max != REG_INFTY && l*max < loceol - scan)
2404 loceol = scan + l*max;
2410 if (PL_reginput < loceol)
2417 - regclass - determine if a character falls into a character class
2421 reginclass(register char *p, register I32 c)
2428 if (ANYOF_TEST(p, c))
2430 else if (flags & ANYOF_FOLD) {
2432 if (flags & ANYOF_LOCALE) {
2433 PL_reg_flags |= RF_tainted;
2434 cf = fold_locale[c];
2438 if (ANYOF_TEST(p, cf))
2442 if (!match && (flags & ANYOF_ISA)) {
2443 PL_reg_flags |= RF_tainted;
2445 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2446 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2447 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2448 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2454 return (flags & ANYOF_INVERT) ? !match : match;
2458 reginclassutf8(regnode *f, U8 *p)
2461 char flags = ARG1(f);
2463 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2465 if (swash_fetch(sv, p))
2467 else if (flags & ANYOF_FOLD) {
2470 if (flags & ANYOF_LOCALE) {
2471 PL_reg_flags |= RF_tainted;
2472 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2475 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2476 if (swash_fetch(sv, tmpbuf))
2480 if (!match && (flags & ANYOF_ISA)) {
2481 PL_reg_flags |= RF_tainted;
2483 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2484 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2485 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2486 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2492 return (flags & ANYOF_INVERT) ? !match : match;
2496 reghop(unsigned char *s, I32 off)
2500 while (off-- && s < (U8*)PL_regeol)
2505 if (s > (U8*)PL_bostr) {
2508 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2510 } /* XXX could check well-formedness here */
2518 reghopmaybe(unsigned char *s, I32 off)
2522 while (off-- && s < (U8*)PL_regeol)
2529 if (s > (U8*)PL_bostr) {
2532 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2534 } /* XXX could check well-formedness here */