5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
71 **** 2000, 2001, 2002, 2003, by Larry Wall and others
73 **** You may distribute under the terms of either the GNU General Public
74 **** License or the Artistic License, as specified in the README file.
76 * Beware that some of this code is subtly aware of the way operator
77 * precedence is structured in regular expressions. Serious changes in
78 * regular-expression syntax might require a total rethink.
81 #define PERL_IN_REGEXEC_C
86 #define RF_tainted 1 /* tainted information used? */
87 #define RF_warned 2 /* warned about big count? */
88 #define RF_evaled 4 /* Did an EVAL with setting? */
89 #define RF_utf8 8 /* String contains multibyte chars? */
90 #define RF_false 16 /* odd number of nested negatives */
92 #define UTF ((PL_reg_flags & RF_utf8) != 0)
94 #define RS_init 1 /* eval environment created */
95 #define RS_set 2 /* replsv value is set */
101 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
107 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
108 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
110 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
111 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
112 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
113 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
114 #define HOPc(pos,off) ((char*)HOP(pos,off))
115 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
117 #define HOPBACK(pos, off) ( \
118 (PL_reg_match_utf8) \
119 ? reghopmaybe((U8*)pos, -off) \
120 : (pos - off >= PL_bostr) \
124 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
126 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
127 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
128 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
129 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
130 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
131 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
133 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) { ENTER; save_re_context(); (void)CAT2(is_utf8_, a)((U8*)b); LEAVE; } } STMT_END
135 /* for use after a quantifier and before an EXACT-like node -- japhy */
136 #define JUMPABLE(rn) ( \
137 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
138 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
139 OP(rn) == PLUS || OP(rn) == MINMOD || \
140 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
143 #define HAS_TEXT(rn) ( \
144 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
148 Search for mandatory following text node; for lookahead, the text must
149 follow but for lookbehind (rn->flags != 0) we skip to the next step.
151 #define FIND_NEXT_IMPT(rn) STMT_START { \
152 while (JUMPABLE(rn)) \
153 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
154 rn = NEXTOPER(NEXTOPER(rn)); \
155 else if (OP(rn) == PLUS) \
157 else if (OP(rn) == IFMATCH) \
158 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
159 else rn += NEXT_OFF(rn); \
162 static void restore_pos(pTHX_ void *arg);
165 S_regcppush(pTHX_ I32 parenfloor)
167 int retval = PL_savestack_ix;
168 #define REGCP_PAREN_ELEMS 4
169 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
172 if (paren_elems_to_push < 0)
173 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
175 #define REGCP_OTHER_ELEMS 6
176 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
177 for (p = PL_regsize; p > parenfloor; p--) {
178 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
179 SSPUSHINT(PL_regendp[p]);
180 SSPUSHINT(PL_regstartp[p]);
181 SSPUSHPTR(PL_reg_start_tmp[p]);
184 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
185 SSPUSHINT(PL_regsize);
186 SSPUSHINT(*PL_reglastparen);
187 SSPUSHINT(*PL_reglastcloseparen);
188 SSPUSHPTR(PL_reginput);
189 #define REGCP_FRAME_ELEMS 2
190 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
191 * are needed for the regexp context stack bookkeeping. */
192 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
193 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
198 /* These are needed since we do not localize EVAL nodes: */
199 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
200 " Setting an EVAL scope, savestack=%"IVdf"\n", \
201 (IV)PL_savestack_ix)); cp = PL_savestack_ix
203 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
204 PerlIO_printf(Perl_debug_log, \
205 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
206 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
216 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
218 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
219 i = SSPOPINT; /* Parentheses elements to pop. */
220 input = (char *) SSPOPPTR;
221 *PL_reglastcloseparen = SSPOPINT;
222 *PL_reglastparen = SSPOPINT;
223 PL_regsize = SSPOPINT;
225 /* Now restore the parentheses context. */
226 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
227 i > 0; i -= REGCP_PAREN_ELEMS) {
228 paren = (U32)SSPOPINT;
229 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
230 PL_regstartp[paren] = SSPOPINT;
232 if (paren <= *PL_reglastparen)
233 PL_regendp[paren] = tmps;
235 PerlIO_printf(Perl_debug_log,
236 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
237 (UV)paren, (IV)PL_regstartp[paren],
238 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
239 (IV)PL_regendp[paren],
240 (paren > *PL_reglastparen ? "(no)" : ""));
244 if ((I32)(*PL_reglastparen + 1) <= PL_regnpar) {
245 PerlIO_printf(Perl_debug_log,
246 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
247 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
251 /* It would seem that the similar code in regtry()
252 * already takes care of this, and in fact it is in
253 * a better location to since this code can #if 0-ed out
254 * but the code in regtry() is needed or otherwise tests
255 * requiring null fields (pat.t#187 and split.t#{13,14}
256 * (as of patchlevel 7877) will fail. Then again,
257 * this code seems to be necessary or otherwise
258 * building DynaLoader will fail:
259 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
261 for (paren = *PL_reglastparen + 1; (I32)paren <= PL_regnpar; paren++) {
262 if ((I32)paren > PL_regsize)
263 PL_regstartp[paren] = -1;
264 PL_regendp[paren] = -1;
271 S_regcp_set_to(pTHX_ I32 ss)
273 I32 tmp = PL_savestack_ix;
275 PL_savestack_ix = ss;
277 PL_savestack_ix = tmp;
281 typedef struct re_cc_state
285 struct re_cc_state *prev;
290 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
292 #define TRYPAREN(paren, n, input) { \
295 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
296 PL_regendp[paren] = input - PL_bostr; \
299 PL_regendp[paren] = -1; \
301 if (regmatch(next)) \
304 PL_regendp[paren] = -1; \
309 * pregexec and friends
313 - pregexec - match a regexp against a string
316 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
317 char *strbeg, I32 minend, SV *screamer, U32 nosave)
318 /* strend: pointer to null at end of string */
319 /* strbeg: real beginning of string */
320 /* minend: end of match must be >=minend after stringarg. */
321 /* nosave: For optimizations. */
324 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
325 nosave ? 0 : REXEC_COPY_STR);
329 S_cache_re(pTHX_ regexp *prog)
331 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
333 PL_regprogram = prog->program;
335 PL_regnpar = prog->nparens;
336 PL_regdata = prog->data;
341 * Need to implement the following flags for reg_anch:
343 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
345 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
346 * INTUIT_AUTORITATIVE_ML
347 * INTUIT_ONCE_NOML - Intuit can match in one location only.
350 * Another flag for this function: SECOND_TIME (so that float substrs
351 * with giant delta may be not rechecked).
354 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
356 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
357 Otherwise, only SvCUR(sv) is used to get strbeg. */
359 /* XXXX We assume that strpos is strbeg unless sv. */
361 /* XXXX Some places assume that there is a fixed substring.
362 An update may be needed if optimizer marks as "INTUITable"
363 RExen without fixed substrings. Similarly, it is assumed that
364 lengths of all the strings are no more than minlen, thus they
365 cannot come from lookahead.
366 (Or minlen should take into account lookahead.) */
368 /* A failure to find a constant substring means that there is no need to make
369 an expensive call to REx engine, thus we celebrate a failure. Similarly,
370 finding a substring too deep into the string means that less calls to
371 regtry() should be needed.
373 REx compiler's optimizer found 4 possible hints:
374 a) Anchored substring;
376 c) Whether we are anchored (beginning-of-line or \G);
377 d) First node (of those at offset 0) which may distingush positions;
378 We use a)b)d) and multiline-part of c), and try to find a position in the
379 string which does not contradict any of them.
382 /* Most of decisions we do here should have been done at compile time.
383 The nodes of the REx which we used for the search should have been
384 deleted from the finite automaton. */
387 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
388 char *strend, U32 flags, re_scream_pos_data *data)
390 register I32 start_shift = 0;
391 /* Should be nonnegative! */
392 register I32 end_shift = 0;
397 int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
399 register char *other_last = Nullch; /* other substr checked before this */
400 char *check_at = Nullch; /* check substr found at this pos */
402 char *i_strpos = strpos;
403 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
405 RX_MATCH_UTF8_set(prog,do_utf8);
407 if (prog->reganch & ROPT_UTF8) {
408 DEBUG_r(PerlIO_printf(Perl_debug_log,
409 "UTF-8 regex...\n"));
410 PL_reg_flags |= RF_utf8;
414 char *s = PL_reg_match_utf8 ?
415 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
417 int len = PL_reg_match_utf8 ?
418 strlen(s) : strend - strpos;
421 if (PL_reg_match_utf8)
422 DEBUG_r(PerlIO_printf(Perl_debug_log,
423 "UTF-8 target...\n"));
424 PerlIO_printf(Perl_debug_log,
425 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
426 PL_colors[4],PL_colors[5],PL_colors[0],
429 (strlen(prog->precomp) > 60 ? "..." : ""),
431 (int)(len > 60 ? 60 : len),
433 (len > 60 ? "..." : "")
437 /* CHR_DIST() would be more correct here but it makes things slow. */
438 if (prog->minlen > strend - strpos) {
439 DEBUG_r(PerlIO_printf(Perl_debug_log,
440 "String too short... [re_intuit_start]\n"));
443 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
446 if (!prog->check_utf8 && prog->check_substr)
447 to_utf8_substr(prog);
448 check = prog->check_utf8;
450 if (!prog->check_substr && prog->check_utf8)
451 to_byte_substr(prog);
452 check = prog->check_substr;
454 if (check == &PL_sv_undef) {
455 DEBUG_r(PerlIO_printf(Perl_debug_log,
456 "Non-utf string cannot match utf check string\n"));
459 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
460 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
461 || ( (prog->reganch & ROPT_ANCH_BOL)
462 && !PL_multiline ) ); /* Check after \n? */
465 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
466 | ROPT_IMPLICIT)) /* not a real BOL */
467 /* SvCUR is not set on references: SvRV and SvPVX overlap */
469 && (strpos != strbeg)) {
470 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
473 if (prog->check_offset_min == prog->check_offset_max &&
474 !(prog->reganch & ROPT_CANY_SEEN)) {
475 /* Substring at constant offset from beg-of-str... */
478 s = HOP3c(strpos, prog->check_offset_min, strend);
480 slen = SvCUR(check); /* >= 1 */
482 if ( strend - s > slen || strend - s < slen - 1
483 || (strend - s == slen && strend[-1] != '\n')) {
484 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
487 /* Now should match s[0..slen-2] */
489 if (slen && (*SvPVX(check) != *s
491 && memNE(SvPVX(check), s, slen)))) {
493 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
497 else if (*SvPVX(check) != *s
498 || ((slen = SvCUR(check)) > 1
499 && memNE(SvPVX(check), s, slen)))
501 goto success_at_start;
504 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
506 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
507 end_shift = prog->minlen - start_shift -
508 CHR_SVLEN(check) + (SvTAIL(check) != 0);
510 I32 end = prog->check_offset_max + CHR_SVLEN(check)
511 - (SvTAIL(check) != 0);
512 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
514 if (end_shift < eshift)
518 else { /* Can match at random position */
521 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
522 /* Should be nonnegative! */
523 end_shift = prog->minlen - start_shift -
524 CHR_SVLEN(check) + (SvTAIL(check) != 0);
527 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
529 Perl_croak(aTHX_ "panic: end_shift");
533 /* Find a possible match in the region s..strend by looking for
534 the "check" substring in the region corrected by start/end_shift. */
535 if (flags & REXEC_SCREAM) {
536 I32 p = -1; /* Internal iterator of scream. */
537 I32 *pp = data ? data->scream_pos : &p;
539 if (PL_screamfirst[BmRARE(check)] >= 0
540 || ( BmRARE(check) == '\n'
541 && (BmPREVIOUS(check) == SvCUR(check) - 1)
543 s = screaminstr(sv, check,
544 start_shift + (s - strbeg), end_shift, pp, 0);
547 /* we may be pointing at the wrong string */
548 if (s && RX_MATCH_COPIED(prog))
549 s = strbeg + (s - SvPVX(sv));
551 *data->scream_olds = s;
553 else if (prog->reganch & ROPT_CANY_SEEN)
554 s = fbm_instr((U8*)(s + start_shift),
555 (U8*)(strend - end_shift),
556 check, PL_multiline ? FBMrf_MULTILINE : 0);
558 s = fbm_instr(HOP3(s, start_shift, strend),
559 HOP3(strend, -end_shift, strbeg),
560 check, PL_multiline ? FBMrf_MULTILINE : 0);
562 /* Update the count-of-usability, remove useless subpatterns,
565 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
566 (s ? "Found" : "Did not find"),
567 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
569 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
571 PL_colors[1], (SvTAIL(check) ? "$" : ""),
572 (s ? " at offset " : "...\n") ) );
579 /* Finish the diagnostic message */
580 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
582 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
583 Start with the other substr.
584 XXXX no SCREAM optimization yet - and a very coarse implementation
585 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
586 *always* match. Probably should be marked during compile...
587 Probably it is right to do no SCREAM here...
590 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
591 /* Take into account the "other" substring. */
592 /* XXXX May be hopelessly wrong for UTF... */
595 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
598 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
602 t = s - prog->check_offset_max;
603 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
605 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
610 t = HOP3c(t, prog->anchored_offset, strend);
611 if (t < other_last) /* These positions already checked */
613 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
616 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
617 /* On end-of-str: see comment below. */
618 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
619 if (must == &PL_sv_undef) {
621 DEBUG_r(must = prog->anchored_utf8); /* for debug */
626 HOP3(HOP3(last1, prog->anchored_offset, strend)
627 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
629 PL_multiline ? FBMrf_MULTILINE : 0
631 DEBUG_r(PerlIO_printf(Perl_debug_log,
632 "%s anchored substr `%s%.*s%s'%s",
633 (s ? "Found" : "Contradicts"),
636 - (SvTAIL(must)!=0)),
638 PL_colors[1], (SvTAIL(must) ? "$" : "")));
640 if (last1 >= last2) {
641 DEBUG_r(PerlIO_printf(Perl_debug_log,
642 ", giving up...\n"));
645 DEBUG_r(PerlIO_printf(Perl_debug_log,
646 ", trying floating at offset %ld...\n",
647 (long)(HOP3c(s1, 1, strend) - i_strpos)));
648 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
649 s = HOP3c(last, 1, strend);
653 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
654 (long)(s - i_strpos)));
655 t = HOP3c(s, -prog->anchored_offset, strbeg);
656 other_last = HOP3c(s, 1, strend);
664 else { /* Take into account the floating substring. */
669 t = HOP3c(s, -start_shift, strbeg);
671 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
672 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
673 last = HOP3c(t, prog->float_max_offset, strend);
674 s = HOP3c(t, prog->float_min_offset, strend);
677 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
678 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
679 /* fbm_instr() takes into account exact value of end-of-str
680 if the check is SvTAIL(ed). Since false positives are OK,
681 and end-of-str is not later than strend we are OK. */
682 if (must == &PL_sv_undef) {
684 DEBUG_r(must = prog->float_utf8); /* for debug message */
687 s = fbm_instr((unsigned char*)s,
688 (unsigned char*)last + SvCUR(must)
690 must, PL_multiline ? FBMrf_MULTILINE : 0);
691 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
692 (s ? "Found" : "Contradicts"),
694 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
696 PL_colors[1], (SvTAIL(must) ? "$" : "")));
699 DEBUG_r(PerlIO_printf(Perl_debug_log,
700 ", giving up...\n"));
703 DEBUG_r(PerlIO_printf(Perl_debug_log,
704 ", trying anchored starting at offset %ld...\n",
705 (long)(s1 + 1 - i_strpos)));
707 s = HOP3c(t, 1, strend);
711 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
712 (long)(s - i_strpos)));
713 other_last = s; /* Fix this later. --Hugo */
722 t = s - prog->check_offset_max;
723 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
725 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
727 /* Fixed substring is found far enough so that the match
728 cannot start at strpos. */
730 if (ml_anch && t[-1] != '\n') {
731 /* Eventually fbm_*() should handle this, but often
732 anchored_offset is not 0, so this check will not be wasted. */
733 /* XXXX In the code below we prefer to look for "^" even in
734 presence of anchored substrings. And we search even
735 beyond the found float position. These pessimizations
736 are historical artefacts only. */
738 while (t < strend - prog->minlen) {
740 if (t < check_at - prog->check_offset_min) {
741 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
742 /* Since we moved from the found position,
743 we definitely contradict the found anchored
744 substr. Due to the above check we do not
745 contradict "check" substr.
746 Thus we can arrive here only if check substr
747 is float. Redo checking for "other"=="fixed".
750 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
751 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
752 goto do_other_anchored;
754 /* We don't contradict the found floating substring. */
755 /* XXXX Why not check for STCLASS? */
757 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
758 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
761 /* Position contradicts check-string */
762 /* XXXX probably better to look for check-string
763 than for "\n", so one should lower the limit for t? */
764 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
765 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
766 other_last = strpos = s = t + 1;
771 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
772 PL_colors[0],PL_colors[1]));
776 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
777 PL_colors[0],PL_colors[1]));
781 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
784 /* The found string does not prohibit matching at strpos,
785 - no optimization of calling REx engine can be performed,
786 unless it was an MBOL and we are not after MBOL,
787 or a future STCLASS check will fail this. */
789 /* Even in this situation we may use MBOL flag if strpos is offset
790 wrt the start of the string. */
791 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
792 && (strpos != strbeg) && strpos[-1] != '\n'
793 /* May be due to an implicit anchor of m{.*foo} */
794 && !(prog->reganch & ROPT_IMPLICIT))
799 DEBUG_r( if (ml_anch)
800 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
801 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
804 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
806 prog->check_utf8 /* Could be deleted already */
807 && --BmUSEFUL(prog->check_utf8) < 0
808 && (prog->check_utf8 == prog->float_utf8)
810 prog->check_substr /* Could be deleted already */
811 && --BmUSEFUL(prog->check_substr) < 0
812 && (prog->check_substr == prog->float_substr)
815 /* If flags & SOMETHING - do not do it many times on the same match */
816 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
817 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
818 if (do_utf8 ? prog->check_substr : prog->check_utf8)
819 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
820 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
821 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
822 check = Nullsv; /* abort */
824 /* XXXX This is a remnant of the old implementation. It
825 looks wasteful, since now INTUIT can use many
827 prog->reganch &= ~RE_USE_INTUIT;
834 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
835 if (prog->regstclass) {
836 /* minlen == 0 is possible if regstclass is \b or \B,
837 and the fixed substr is ''$.
838 Since minlen is already taken into account, s+1 is before strend;
839 accidentally, minlen >= 1 guaranties no false positives at s + 1
840 even for \b or \B. But (minlen? 1 : 0) below assumes that
841 regstclass does not come from lookahead... */
842 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
843 This leaves EXACTF only, which is dealt with in find_byclass(). */
844 U8* str = (U8*)STRING(prog->regstclass);
845 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
846 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
848 char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
849 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
850 : (prog->float_substr || prog->float_utf8
851 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
854 char *startpos = strbeg;
858 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
863 if (endpos == strend) {
864 DEBUG_r( PerlIO_printf(Perl_debug_log,
865 "Could not match STCLASS...\n") );
868 DEBUG_r( PerlIO_printf(Perl_debug_log,
869 "This position contradicts STCLASS...\n") );
870 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
872 /* Contradict one of substrings */
873 if (prog->anchored_substr || prog->anchored_utf8) {
874 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
875 DEBUG_r( what = "anchored" );
877 s = HOP3c(t, 1, strend);
878 if (s + start_shift + end_shift > strend) {
879 /* XXXX Should be taken into account earlier? */
880 DEBUG_r( PerlIO_printf(Perl_debug_log,
881 "Could not match STCLASS...\n") );
886 DEBUG_r( PerlIO_printf(Perl_debug_log,
887 "Looking for %s substr starting at offset %ld...\n",
888 what, (long)(s + start_shift - i_strpos)) );
891 /* Have both, check_string is floating */
892 if (t + start_shift >= check_at) /* Contradicts floating=check */
893 goto retry_floating_check;
894 /* Recheck anchored substring, but not floating... */
898 DEBUG_r( PerlIO_printf(Perl_debug_log,
899 "Looking for anchored substr starting at offset %ld...\n",
900 (long)(other_last - i_strpos)) );
901 goto do_other_anchored;
903 /* Another way we could have checked stclass at the
904 current position only: */
909 DEBUG_r( PerlIO_printf(Perl_debug_log,
910 "Looking for /%s^%s/m starting at offset %ld...\n",
911 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
914 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
916 /* Check is floating subtring. */
917 retry_floating_check:
918 t = check_at - start_shift;
919 DEBUG_r( what = "floating" );
920 goto hop_and_restart;
923 DEBUG_r(PerlIO_printf(Perl_debug_log,
924 "By STCLASS: moving %ld --> %ld\n",
925 (long)(t - i_strpos), (long)(s - i_strpos))
929 DEBUG_r(PerlIO_printf(Perl_debug_log,
930 "Does not contradict STCLASS...\n");
935 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
936 PL_colors[4], (check ? "Guessed" : "Giving up"),
937 PL_colors[5], (long)(s - i_strpos)) );
940 fail_finish: /* Substring not found */
941 if (prog->check_substr || prog->check_utf8) /* could be removed already */
942 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
944 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
945 PL_colors[4],PL_colors[5]));
949 /* We know what class REx starts with. Try to find this position... */
951 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
953 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
959 register I32 tmp = 1; /* Scratch variable? */
960 register bool do_utf8 = PL_reg_match_utf8;
962 /* We know what class it must start with. */
967 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
968 !UTF8_IS_INVARIANT((U8)s[0]) ?
969 reginclass(c, (U8*)s, 0, do_utf8) :
970 REGINCLASS(c, (U8*)s)) {
971 if (tmp && (norun || regtry(prog, s)))
985 if (REGINCLASS(c, (U8*)s) ||
986 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
987 /* The assignment of 2 is intentional:
988 * for the folded sharp s, the skip is 2. */
989 (skip = SHARP_S_SKIP))) {
990 if (tmp && (norun || regtry(prog, s)))
1002 while (s < strend) {
1003 if (tmp && (norun || regtry(prog, s)))
1014 STRLEN ulen1, ulen2;
1015 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
1016 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1018 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1019 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1021 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1022 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1023 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1024 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1035 c2 = PL_fold_locale[c1];
1037 e = HOP3c(strend, -(I32)ln, s);
1040 e = s; /* Due to minlen logic of intuit() */
1042 /* The idea in the EXACTF* cases is to first find the
1043 * first character of the EXACTF* node and then, if
1044 * necessary, case-insensitively compare the full
1045 * text of the node. The c1 and c2 are the first
1046 * characters (though in Unicode it gets a bit
1047 * more complicated because there are more cases
1048 * than just upper and lower: one needs to use
1049 * the so-called folding case for case-insensitive
1050 * matching (called "loose matching" in Unicode).
1051 * ibcmp_utf8() will do just that. */
1055 U8 tmpbuf [UTF8_MAXLEN+1];
1056 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1057 STRLEN len, foldlen;
1061 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1063 0 : UTF8_ALLOW_ANY);
1066 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1067 m, (char **)0, ln, (bool)UTF))
1068 && (norun || regtry(prog, s)) )
1071 uvchr_to_utf8(tmpbuf, c);
1072 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1074 && (f == c1 || f == c2)
1075 && (ln == foldlen ||
1076 !ibcmp_utf8((char *) foldbuf,
1077 (char **)0, foldlen, do_utf8,
1079 (char **)0, ln, (bool)UTF))
1080 && (norun || regtry(prog, s)) )
1088 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1090 0 : UTF8_ALLOW_ANY);
1092 /* Handle some of the three Greek sigmas cases.
1093 * Note that not all the possible combinations
1094 * are handled here: some of them are handled
1095 * by the standard folding rules, and some of
1096 * them (the character class or ANYOF cases)
1097 * are handled during compiletime in
1098 * regexec.c:S_regclass(). */
1099 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1100 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1101 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1103 if ( (c == c1 || c == c2)
1105 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1106 m, (char **)0, ln, (bool)UTF))
1107 && (norun || regtry(prog, s)) )
1110 uvchr_to_utf8(tmpbuf, c);
1111 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1113 && (f == c1 || f == c2)
1114 && (ln == foldlen ||
1115 !ibcmp_utf8((char *) foldbuf,
1116 (char **)0, foldlen, do_utf8,
1118 (char **)0, ln, (bool)UTF))
1119 && (norun || regtry(prog, s)) )
1130 && (ln == 1 || !(OP(c) == EXACTF
1132 : ibcmp_locale(s, m, ln)))
1133 && (norun || regtry(prog, s)) )
1139 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1140 && (ln == 1 || !(OP(c) == EXACTF
1142 : ibcmp_locale(s, m, ln)))
1143 && (norun || regtry(prog, s)) )
1150 PL_reg_flags |= RF_tainted;
1157 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1159 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1161 tmp = ((OP(c) == BOUND ?
1162 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1163 LOAD_UTF8_CHARCLASS(alnum,"a");
1164 while (s < strend) {
1165 if (tmp == !(OP(c) == BOUND ?
1166 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1167 isALNUM_LC_utf8((U8*)s)))
1170 if ((norun || regtry(prog, s)))
1177 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1178 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1179 while (s < strend) {
1181 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1183 if ((norun || regtry(prog, s)))
1189 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1193 PL_reg_flags |= RF_tainted;
1200 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1202 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1204 tmp = ((OP(c) == NBOUND ?
1205 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1206 LOAD_UTF8_CHARCLASS(alnum,"a");
1207 while (s < strend) {
1208 if (tmp == !(OP(c) == NBOUND ?
1209 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1210 isALNUM_LC_utf8((U8*)s)))
1212 else if ((norun || regtry(prog, s)))
1218 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1219 tmp = ((OP(c) == NBOUND ?
1220 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1221 while (s < strend) {
1223 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1225 else if ((norun || regtry(prog, s)))
1230 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1235 LOAD_UTF8_CHARCLASS(alnum,"a");
1236 while (s < strend) {
1237 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1238 if (tmp && (norun || regtry(prog, s)))
1249 while (s < strend) {
1251 if (tmp && (norun || regtry(prog, s)))
1263 PL_reg_flags |= RF_tainted;
1265 while (s < strend) {
1266 if (isALNUM_LC_utf8((U8*)s)) {
1267 if (tmp && (norun || regtry(prog, s)))
1278 while (s < strend) {
1279 if (isALNUM_LC(*s)) {
1280 if (tmp && (norun || regtry(prog, s)))
1293 LOAD_UTF8_CHARCLASS(alnum,"a");
1294 while (s < strend) {
1295 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1296 if (tmp && (norun || regtry(prog, s)))
1307 while (s < strend) {
1309 if (tmp && (norun || regtry(prog, s)))
1321 PL_reg_flags |= RF_tainted;
1323 while (s < strend) {
1324 if (!isALNUM_LC_utf8((U8*)s)) {
1325 if (tmp && (norun || regtry(prog, s)))
1336 while (s < strend) {
1337 if (!isALNUM_LC(*s)) {
1338 if (tmp && (norun || regtry(prog, s)))
1351 LOAD_UTF8_CHARCLASS(space," ");
1352 while (s < strend) {
1353 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1354 if (tmp && (norun || regtry(prog, s)))
1365 while (s < strend) {
1367 if (tmp && (norun || regtry(prog, s)))
1379 PL_reg_flags |= RF_tainted;
1381 while (s < strend) {
1382 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1383 if (tmp && (norun || regtry(prog, s)))
1394 while (s < strend) {
1395 if (isSPACE_LC(*s)) {
1396 if (tmp && (norun || regtry(prog, s)))
1409 LOAD_UTF8_CHARCLASS(space," ");
1410 while (s < strend) {
1411 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1412 if (tmp && (norun || regtry(prog, s)))
1423 while (s < strend) {
1425 if (tmp && (norun || regtry(prog, s)))
1437 PL_reg_flags |= RF_tainted;
1439 while (s < strend) {
1440 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1441 if (tmp && (norun || regtry(prog, s)))
1452 while (s < strend) {
1453 if (!isSPACE_LC(*s)) {
1454 if (tmp && (norun || regtry(prog, s)))
1467 LOAD_UTF8_CHARCLASS(digit,"0");
1468 while (s < strend) {
1469 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1470 if (tmp && (norun || regtry(prog, s)))
1481 while (s < strend) {
1483 if (tmp && (norun || regtry(prog, s)))
1495 PL_reg_flags |= RF_tainted;
1497 while (s < strend) {
1498 if (isDIGIT_LC_utf8((U8*)s)) {
1499 if (tmp && (norun || regtry(prog, s)))
1510 while (s < strend) {
1511 if (isDIGIT_LC(*s)) {
1512 if (tmp && (norun || regtry(prog, s)))
1525 LOAD_UTF8_CHARCLASS(digit,"0");
1526 while (s < strend) {
1527 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1528 if (tmp && (norun || regtry(prog, s)))
1539 while (s < strend) {
1541 if (tmp && (norun || regtry(prog, s)))
1553 PL_reg_flags |= RF_tainted;
1555 while (s < strend) {
1556 if (!isDIGIT_LC_utf8((U8*)s)) {
1557 if (tmp && (norun || regtry(prog, s)))
1568 while (s < strend) {
1569 if (!isDIGIT_LC(*s)) {
1570 if (tmp && (norun || regtry(prog, s)))
1582 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1591 - regexec_flags - match a regexp against a string
1594 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1595 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1596 /* strend: pointer to null at end of string */
1597 /* strbeg: real beginning of string */
1598 /* minend: end of match must be >=minend after stringarg. */
1599 /* data: May be used for some additional optimizations. */
1600 /* nosave: For optimizations. */
1603 register regnode *c;
1604 register char *startpos = stringarg;
1605 I32 minlen; /* must match at least this many chars */
1606 I32 dontbother = 0; /* how many characters not to try at end */
1607 /* I32 start_shift = 0; */ /* Offset of the start to find
1608 constant substr. */ /* CC */
1609 I32 end_shift = 0; /* Same for the end. */ /* CC */
1610 I32 scream_pos = -1; /* Internal iterator of scream. */
1612 SV* oreplsv = GvSV(PL_replgv);
1613 bool do_utf8 = DO_UTF8(sv);
1615 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1616 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1618 RX_MATCH_UTF8_set(prog,do_utf8);
1624 PL_regnarrate = DEBUG_r_TEST;
1627 /* Be paranoid... */
1628 if (prog == NULL || startpos == NULL) {
1629 Perl_croak(aTHX_ "NULL regexp parameter");
1633 minlen = prog->minlen;
1634 if (strend - startpos < minlen) {
1635 DEBUG_r(PerlIO_printf(Perl_debug_log,
1636 "String too short [regexec_flags]...\n"));
1640 /* Check validity of program. */
1641 if (UCHARAT(prog->program) != REG_MAGIC) {
1642 Perl_croak(aTHX_ "corrupted regexp program");
1646 PL_reg_eval_set = 0;
1649 if (prog->reganch & ROPT_UTF8)
1650 PL_reg_flags |= RF_utf8;
1652 /* Mark beginning of line for ^ and lookbehind. */
1653 PL_regbol = startpos;
1657 /* Mark end of line for $ (and such) */
1660 /* see how far we have to get to not match where we matched before */
1661 PL_regtill = startpos+minend;
1663 /* We start without call_cc context. */
1666 /* If there is a "must appear" string, look for it. */
1669 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1672 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1673 PL_reg_ganch = startpos;
1674 else if (sv && SvTYPE(sv) >= SVt_PVMG
1676 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1677 && mg->mg_len >= 0) {
1678 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1679 if (prog->reganch & ROPT_ANCH_GPOS) {
1680 if (s > PL_reg_ganch)
1685 else /* pos() not defined */
1686 PL_reg_ganch = strbeg;
1689 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1690 re_scream_pos_data d;
1692 d.scream_olds = &scream_olds;
1693 d.scream_pos = &scream_pos;
1694 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1696 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1697 goto phooey; /* not present */
1703 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1704 UNI_DISPLAY_REGEX) :
1706 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1707 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1708 UNI_DISPLAY_REGEX) : startpos;
1709 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1712 PerlIO_printf(Perl_debug_log,
1713 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1714 PL_colors[4],PL_colors[5],PL_colors[0],
1717 len0 > 60 ? "..." : "",
1719 (int)(len1 > 60 ? 60 : len1),
1721 (len1 > 60 ? "..." : "")
1725 /* Simplest case: anchored match need be tried only once. */
1726 /* [unless only anchor is BOL and multiline is set] */
1727 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1728 if (s == startpos && regtry(prog, startpos))
1730 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1731 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1736 dontbother = minlen - 1;
1737 end = HOP3c(strend, -dontbother, strbeg) - 1;
1738 /* for multiline we only have to try after newlines */
1739 if (prog->check_substr || prog->check_utf8) {
1743 if (regtry(prog, s))
1748 if (prog->reganch & RE_USE_INTUIT) {
1749 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1760 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1761 if (regtry(prog, s))
1768 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1769 if (regtry(prog, PL_reg_ganch))
1774 /* Messy cases: unanchored match. */
1775 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1776 /* we have /x+whatever/ */
1777 /* it must be a one character string (XXXX Except UTF?) */
1782 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1783 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1784 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1787 while (s < strend) {
1789 DEBUG_r( did_match = 1 );
1790 if (regtry(prog, s)) goto got_it;
1792 while (s < strend && *s == ch)
1799 while (s < strend) {
1801 DEBUG_r( did_match = 1 );
1802 if (regtry(prog, s)) goto got_it;
1804 while (s < strend && *s == ch)
1810 DEBUG_r(if (!did_match)
1811 PerlIO_printf(Perl_debug_log,
1812 "Did not find anchored character...\n")
1816 else if (prog->anchored_substr != Nullsv
1817 || prog->anchored_utf8 != Nullsv
1818 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1819 && prog->float_max_offset < strend - s)) {
1824 char *last1; /* Last position checked before */
1828 if (prog->anchored_substr || prog->anchored_utf8) {
1829 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1830 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1831 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1832 back_max = back_min = prog->anchored_offset;
1834 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1835 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1836 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1837 back_max = prog->float_max_offset;
1838 back_min = prog->float_min_offset;
1840 if (must == &PL_sv_undef)
1841 /* could not downgrade utf8 check substring, so must fail */
1844 last = HOP3c(strend, /* Cannot start after this */
1845 -(I32)(CHR_SVLEN(must)
1846 - (SvTAIL(must) != 0) + back_min), strbeg);
1849 last1 = HOPc(s, -1);
1851 last1 = s - 1; /* bogus */
1853 /* XXXX check_substr already used to find `s', can optimize if
1854 check_substr==must. */
1856 dontbother = end_shift;
1857 strend = HOPc(strend, -dontbother);
1858 while ( (s <= last) &&
1859 ((flags & REXEC_SCREAM)
1860 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1861 end_shift, &scream_pos, 0))
1862 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1863 (unsigned char*)strend, must,
1864 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1865 /* we may be pointing at the wrong string */
1866 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1867 s = strbeg + (s - SvPVX(sv));
1868 DEBUG_r( did_match = 1 );
1869 if (HOPc(s, -back_max) > last1) {
1870 last1 = HOPc(s, -back_min);
1871 s = HOPc(s, -back_max);
1874 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1876 last1 = HOPc(s, -back_min);
1880 while (s <= last1) {
1881 if (regtry(prog, s))
1887 while (s <= last1) {
1888 if (regtry(prog, s))
1894 DEBUG_r(if (!did_match)
1895 PerlIO_printf(Perl_debug_log,
1896 "Did not find %s substr `%s%.*s%s'%s...\n",
1897 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1898 ? "anchored" : "floating"),
1900 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1902 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1906 else if ((c = prog->regstclass)) {
1908 I32 op = (U8)OP(prog->regstclass);
1909 /* don't bother with what can't match */
1910 if (PL_regkind[op] != EXACT && op != CANY)
1911 strend = HOPc(strend, -(minlen - 1));
1914 SV *prop = sv_newmortal();
1922 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1923 UNI_DISPLAY_REGEX) :
1925 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1927 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1928 len1 = UTF ? SvCUR(dsv1) : strend - s;
1929 PerlIO_printf(Perl_debug_log,
1930 "Matching stclass `%*.*s' against `%*.*s'\n",
1934 if (find_byclass(prog, c, s, strend, startpos, 0))
1936 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1940 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1945 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1946 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1947 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1949 if (flags & REXEC_SCREAM) {
1950 last = screaminstr(sv, float_real, s - strbeg,
1951 end_shift, &scream_pos, 1); /* last one */
1953 last = scream_olds; /* Only one occurrence. */
1954 /* we may be pointing at the wrong string */
1955 else if (RX_MATCH_COPIED(prog))
1956 s = strbeg + (s - SvPVX(sv));
1960 char *little = SvPV(float_real, len);
1962 if (SvTAIL(float_real)) {
1963 if (memEQ(strend - len + 1, little, len - 1))
1964 last = strend - len + 1;
1965 else if (!PL_multiline)
1966 last = memEQ(strend - len, little, len)
1967 ? strend - len : Nullch;
1973 last = rninstr(s, strend, little, little + len);
1975 last = strend; /* matching `$' */
1979 DEBUG_r(PerlIO_printf(Perl_debug_log,
1980 "%sCan't trim the tail, match fails (should not happen)%s\n",
1981 PL_colors[4],PL_colors[5]));
1982 goto phooey; /* Should not happen! */
1984 dontbother = strend - last + prog->float_min_offset;
1986 if (minlen && (dontbother < minlen))
1987 dontbother = minlen - 1;
1988 strend -= dontbother; /* this one's always in bytes! */
1989 /* We don't know much -- general case. */
1992 if (regtry(prog, s))
2001 if (regtry(prog, s))
2003 } while (s++ < strend);
2011 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2013 if (PL_reg_eval_set) {
2014 /* Preserve the current value of $^R */
2015 if (oreplsv != GvSV(PL_replgv))
2016 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2017 restored, the value remains
2019 restore_pos(aTHX_ 0);
2022 /* make sure $`, $&, $', and $digit will work later */
2023 if ( !(flags & REXEC_NOT_FIRST) ) {
2024 if (RX_MATCH_COPIED(prog)) {
2025 Safefree(prog->subbeg);
2026 RX_MATCH_COPIED_off(prog);
2028 if (flags & REXEC_COPY_STR) {
2029 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2031 s = savepvn(strbeg, i);
2034 RX_MATCH_COPIED_on(prog);
2037 prog->subbeg = strbeg;
2038 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2045 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2046 PL_colors[4],PL_colors[5]));
2047 if (PL_reg_eval_set)
2048 restore_pos(aTHX_ 0);
2053 - regtry - try match at specific point
2055 STATIC I32 /* 0 failure, 1 success */
2056 S_regtry(pTHX_ regexp *prog, char *startpos)
2064 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2066 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2069 PL_reg_eval_set = RS_init;
2071 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2072 (IV)(PL_stack_sp - PL_stack_base));
2074 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2075 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2076 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2078 /* Apparently this is not needed, judging by wantarray. */
2079 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2080 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2083 /* Make $_ available to executed code. */
2084 if (PL_reg_sv != DEFSV) {
2085 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2090 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2091 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2092 /* prepare for quick setting of pos */
2093 sv_magic(PL_reg_sv, (SV*)0,
2094 PERL_MAGIC_regex_global, Nullch, 0);
2095 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2099 PL_reg_oldpos = mg->mg_len;
2100 SAVEDESTRUCTOR_X(restore_pos, 0);
2102 if (!PL_reg_curpm) {
2103 Newz(22,PL_reg_curpm, 1, PMOP);
2106 SV* repointer = newSViv(0);
2107 /* so we know which PL_regex_padav element is PL_reg_curpm */
2108 SvFLAGS(repointer) |= SVf_BREAK;
2109 av_push(PL_regex_padav,repointer);
2110 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2111 PL_regex_pad = AvARRAY(PL_regex_padav);
2115 PM_SETRE(PL_reg_curpm, prog);
2116 PL_reg_oldcurpm = PL_curpm;
2117 PL_curpm = PL_reg_curpm;
2118 if (RX_MATCH_COPIED(prog)) {
2119 /* Here is a serious problem: we cannot rewrite subbeg,
2120 since it may be needed if this match fails. Thus
2121 $` inside (?{}) could fail... */
2122 PL_reg_oldsaved = prog->subbeg;
2123 PL_reg_oldsavedlen = prog->sublen;
2124 RX_MATCH_COPIED_off(prog);
2127 PL_reg_oldsaved = Nullch;
2128 prog->subbeg = PL_bostr;
2129 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2131 prog->startp[0] = startpos - PL_bostr;
2132 PL_reginput = startpos;
2133 PL_regstartp = prog->startp;
2134 PL_regendp = prog->endp;
2135 PL_reglastparen = &prog->lastparen;
2136 PL_reglastcloseparen = &prog->lastcloseparen;
2137 prog->lastparen = 0;
2138 prog->lastcloseparen = 0;
2140 DEBUG_r(PL_reg_starttry = startpos);
2141 if (PL_reg_start_tmpl <= prog->nparens) {
2142 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2143 if(PL_reg_start_tmp)
2144 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2146 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2149 /* XXXX What this code is doing here?!!! There should be no need
2150 to do this again and again, PL_reglastparen should take care of
2153 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2154 * Actually, the code in regcppop() (which Ilya may be meaning by
2155 * PL_reglastparen), is not needed at all by the test suite
2156 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2157 * enough, for building DynaLoader, or otherwise this
2158 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2159 * will happen. Meanwhile, this code *is* needed for the
2160 * above-mentioned test suite tests to succeed. The common theme
2161 * on those tests seems to be returning null fields from matches.
2166 if (prog->nparens) {
2167 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2174 if (regmatch(prog->program + 1)) {
2175 prog->endp[0] = PL_reginput - PL_bostr;
2178 REGCP_UNWIND(lastcp);
2182 #define RE_UNWIND_BRANCH 1
2183 #define RE_UNWIND_BRANCHJ 2
2187 typedef struct { /* XX: makes sense to enlarge it... */
2191 } re_unwind_generic_t;
2204 } re_unwind_branch_t;
2206 typedef union re_unwind_t {
2208 re_unwind_generic_t generic;
2209 re_unwind_branch_t branch;
2212 #define sayYES goto yes
2213 #define sayNO goto no
2214 #define sayNO_ANYOF goto no_anyof
2215 #define sayYES_FINAL goto yes_final
2216 #define sayYES_LOUD goto yes_loud
2217 #define sayNO_FINAL goto no_final
2218 #define sayNO_SILENT goto do_no
2219 #define saySAME(x) if (x) goto yes; else goto no
2221 #define REPORT_CODE_OFF 24
2224 - regmatch - main matching routine
2226 * Conceptually the strategy is simple: check to see whether the current
2227 * node matches, call self recursively to see whether the rest matches,
2228 * and then act accordingly. In practice we make some effort to avoid
2229 * recursion, in particular by going through "ordinary" nodes (that don't
2230 * need to know whether the rest of the match failed) by a loop instead of
2233 /* [lwall] I've hoisted the register declarations to the outer block in order to
2234 * maybe save a little bit of pushing and popping on the stack. It also takes
2235 * advantage of machines that use a register save mask on subroutine entry.
2237 STATIC I32 /* 0 failure, 1 success */
2238 S_regmatch(pTHX_ regnode *prog)
2240 register regnode *scan; /* Current node. */
2241 regnode *next; /* Next node. */
2242 regnode *inner; /* Next node in internal branch. */
2243 register I32 nextchr; /* renamed nextchr - nextchar colides with
2244 function of same name */
2245 register I32 n; /* no or next */
2246 register I32 ln = 0; /* len or last */
2247 register char *s = Nullch; /* operand or save */
2248 register char *locinput = PL_reginput;
2249 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2250 int minmod = 0, sw = 0, logical = 0;
2253 I32 firstcp = PL_savestack_ix;
2255 register bool do_utf8 = PL_reg_match_utf8;
2257 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2258 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2259 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2266 /* Note that nextchr is a byte even in UTF */
2267 nextchr = UCHARAT(locinput);
2269 while (scan != NULL) {
2272 SV *prop = sv_newmortal();
2273 int docolor = *PL_colors[0];
2274 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2275 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2276 /* The part of the string before starttry has one color
2277 (pref0_len chars), between starttry and current
2278 position another one (pref_len - pref0_len chars),
2279 after the current position the third one.
2280 We assume that pref0_len <= pref_len, otherwise we
2281 decrease pref0_len. */
2282 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2283 ? (5 + taill) - l : locinput - PL_bostr;
2286 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2288 pref0_len = pref_len - (locinput - PL_reg_starttry);
2289 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2290 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2291 ? (5 + taill) - pref_len : PL_regeol - locinput);
2292 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2296 if (pref0_len > pref_len)
2297 pref0_len = pref_len;
2298 regprop(prop, scan);
2301 do_utf8 && OP(scan) != CANY ?
2302 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2303 pref0_len, 60, UNI_DISPLAY_REGEX) :
2304 locinput - pref_len;
2305 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2306 char *s1 = do_utf8 && OP(scan) != CANY ?
2307 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2308 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2309 locinput - pref_len + pref0_len;
2310 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2311 char *s2 = do_utf8 && OP(scan) != CANY ?
2312 pv_uni_display(dsv2, (U8*)locinput,
2313 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2315 int len2 = do_utf8 ? strlen(s2) : l;
2316 PerlIO_printf(Perl_debug_log,
2317 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2318 (IV)(locinput - PL_bostr),
2325 (docolor ? "" : "> <"),
2329 15 - l - pref_len + 1,
2331 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2336 next = scan + NEXT_OFF(scan);
2342 if (locinput == PL_bostr || (PL_multiline &&
2343 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2345 /* regtill = regbol; */
2350 if (locinput == PL_bostr ||
2351 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2357 if (locinput == PL_bostr)
2361 if (locinput == PL_reg_ganch)
2371 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2376 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2378 if (PL_regeol - locinput > 1)
2382 if (PL_regeol != locinput)
2386 if (!nextchr && locinput >= PL_regeol)
2389 locinput += PL_utf8skip[nextchr];
2390 if (locinput > PL_regeol)
2392 nextchr = UCHARAT(locinput);
2395 nextchr = UCHARAT(++locinput);
2398 if (!nextchr && locinput >= PL_regeol)
2400 nextchr = UCHARAT(++locinput);
2403 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2406 locinput += PL_utf8skip[nextchr];
2407 if (locinput > PL_regeol)
2409 nextchr = UCHARAT(locinput);
2412 nextchr = UCHARAT(++locinput);
2417 if (do_utf8 != UTF) {
2418 /* The target and the pattern have differing utf8ness. */
2424 /* The target is utf8, the pattern is not utf8. */
2428 if (NATIVE_TO_UNI(*(U8*)s) !=
2429 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2431 0 : UTF8_ALLOW_ANY))
2438 /* The target is not utf8, the pattern is utf8. */
2442 if (NATIVE_TO_UNI(*((U8*)l)) !=
2443 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2445 0 : UTF8_ALLOW_ANY))
2452 nextchr = UCHARAT(locinput);
2455 /* The target and the pattern have the same utf8ness. */
2456 /* Inline the first character, for speed. */
2457 if (UCHARAT(s) != nextchr)
2459 if (PL_regeol - locinput < ln)
2461 if (ln > 1 && memNE(s, locinput, ln))
2464 nextchr = UCHARAT(locinput);
2467 PL_reg_flags |= RF_tainted;
2473 if (do_utf8 || UTF) {
2474 /* Either target or the pattern are utf8. */
2476 char *e = PL_regeol;
2478 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2479 l, &e, 0, do_utf8)) {
2480 /* One more case for the sharp s:
2481 * pack("U0U*", 0xDF) =~ /ss/i,
2482 * the 0xC3 0x9F are the UTF-8
2483 * byte sequence for the U+00DF. */
2485 toLOWER(s[0]) == 's' &&
2487 toLOWER(s[1]) == 's' &&
2494 nextchr = UCHARAT(locinput);
2498 /* Neither the target and the pattern are utf8. */
2500 /* Inline the first character, for speed. */
2501 if (UCHARAT(s) != nextchr &&
2502 UCHARAT(s) != ((OP(scan) == EXACTF)
2503 ? PL_fold : PL_fold_locale)[nextchr])
2505 if (PL_regeol - locinput < ln)
2507 if (ln > 1 && (OP(scan) == EXACTF
2508 ? ibcmp(s, locinput, ln)
2509 : ibcmp_locale(s, locinput, ln)))
2512 nextchr = UCHARAT(locinput);
2516 STRLEN inclasslen = PL_regeol - locinput;
2518 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2520 if (locinput >= PL_regeol)
2522 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2523 nextchr = UCHARAT(locinput);
2528 nextchr = UCHARAT(locinput);
2529 if (!REGINCLASS(scan, (U8*)locinput))
2531 if (!nextchr && locinput >= PL_regeol)
2533 nextchr = UCHARAT(++locinput);
2537 /* If we might have the case of the German sharp s
2538 * in a casefolding Unicode character class. */
2540 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2541 locinput += SHARP_S_SKIP;
2542 nextchr = UCHARAT(locinput);
2548 PL_reg_flags |= RF_tainted;
2554 LOAD_UTF8_CHARCLASS(alnum,"a");
2555 if (!(OP(scan) == ALNUM
2556 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2557 : isALNUM_LC_utf8((U8*)locinput)))
2561 locinput += PL_utf8skip[nextchr];
2562 nextchr = UCHARAT(locinput);
2565 if (!(OP(scan) == ALNUM
2566 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2568 nextchr = UCHARAT(++locinput);
2571 PL_reg_flags |= RF_tainted;
2574 if (!nextchr && locinput >= PL_regeol)
2577 LOAD_UTF8_CHARCLASS(alnum,"a");
2578 if (OP(scan) == NALNUM
2579 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2580 : isALNUM_LC_utf8((U8*)locinput))
2584 locinput += PL_utf8skip[nextchr];
2585 nextchr = UCHARAT(locinput);
2588 if (OP(scan) == NALNUM
2589 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2591 nextchr = UCHARAT(++locinput);
2595 PL_reg_flags |= RF_tainted;
2599 /* was last char in word? */
2601 if (locinput == PL_bostr)
2604 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2606 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2608 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2609 ln = isALNUM_uni(ln);
2610 LOAD_UTF8_CHARCLASS(alnum,"a");
2611 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2614 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2615 n = isALNUM_LC_utf8((U8*)locinput);
2619 ln = (locinput != PL_bostr) ?
2620 UCHARAT(locinput - 1) : '\n';
2621 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2623 n = isALNUM(nextchr);
2626 ln = isALNUM_LC(ln);
2627 n = isALNUM_LC(nextchr);
2630 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2631 OP(scan) == BOUNDL))
2635 PL_reg_flags |= RF_tainted;
2641 if (UTF8_IS_CONTINUED(nextchr)) {
2642 LOAD_UTF8_CHARCLASS(space," ");
2643 if (!(OP(scan) == SPACE
2644 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2645 : isSPACE_LC_utf8((U8*)locinput)))
2649 locinput += PL_utf8skip[nextchr];
2650 nextchr = UCHARAT(locinput);
2653 if (!(OP(scan) == SPACE
2654 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2656 nextchr = UCHARAT(++locinput);
2659 if (!(OP(scan) == SPACE
2660 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2662 nextchr = UCHARAT(++locinput);
2666 PL_reg_flags |= RF_tainted;
2669 if (!nextchr && locinput >= PL_regeol)
2672 LOAD_UTF8_CHARCLASS(space," ");
2673 if (OP(scan) == NSPACE
2674 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2675 : isSPACE_LC_utf8((U8*)locinput))
2679 locinput += PL_utf8skip[nextchr];
2680 nextchr = UCHARAT(locinput);
2683 if (OP(scan) == NSPACE
2684 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2686 nextchr = UCHARAT(++locinput);
2689 PL_reg_flags |= RF_tainted;
2695 LOAD_UTF8_CHARCLASS(digit,"0");
2696 if (!(OP(scan) == DIGIT
2697 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2698 : isDIGIT_LC_utf8((U8*)locinput)))
2702 locinput += PL_utf8skip[nextchr];
2703 nextchr = UCHARAT(locinput);
2706 if (!(OP(scan) == DIGIT
2707 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2709 nextchr = UCHARAT(++locinput);
2712 PL_reg_flags |= RF_tainted;
2715 if (!nextchr && locinput >= PL_regeol)
2718 LOAD_UTF8_CHARCLASS(digit,"0");
2719 if (OP(scan) == NDIGIT
2720 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2721 : isDIGIT_LC_utf8((U8*)locinput))
2725 locinput += PL_utf8skip[nextchr];
2726 nextchr = UCHARAT(locinput);
2729 if (OP(scan) == NDIGIT
2730 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2732 nextchr = UCHARAT(++locinput);
2735 if (locinput >= PL_regeol)
2738 LOAD_UTF8_CHARCLASS(mark,"~");
2739 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2741 locinput += PL_utf8skip[nextchr];
2742 while (locinput < PL_regeol &&
2743 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2744 locinput += UTF8SKIP(locinput);
2745 if (locinput > PL_regeol)
2750 nextchr = UCHARAT(locinput);
2753 PL_reg_flags |= RF_tainted;
2757 n = ARG(scan); /* which paren pair */
2758 ln = PL_regstartp[n];
2759 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2760 if ((I32)*PL_reglastparen < n || ln == -1)
2761 sayNO; /* Do not match unless seen CLOSEn. */
2762 if (ln == PL_regendp[n])
2766 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2768 char *e = PL_bostr + PL_regendp[n];
2770 * Note that we can't do the "other character" lookup trick as
2771 * in the 8-bit case (no pun intended) because in Unicode we
2772 * have to map both upper and title case to lower case.
2774 if (OP(scan) == REFF) {
2775 STRLEN ulen1, ulen2;
2776 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2777 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2781 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2782 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2783 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2790 nextchr = UCHARAT(locinput);
2794 /* Inline the first character, for speed. */
2795 if (UCHARAT(s) != nextchr &&
2797 (UCHARAT(s) != ((OP(scan) == REFF
2798 ? PL_fold : PL_fold_locale)[nextchr]))))
2800 ln = PL_regendp[n] - ln;
2801 if (locinput + ln > PL_regeol)
2803 if (ln > 1 && (OP(scan) == REF
2804 ? memNE(s, locinput, ln)
2806 ? ibcmp(s, locinput, ln)
2807 : ibcmp_locale(s, locinput, ln))))
2810 nextchr = UCHARAT(locinput);
2821 OP_4tree *oop = PL_op;
2822 COP *ocurcop = PL_curcop;
2825 struct regexp *oreg = PL_reg_re;
2828 PL_op = (OP_4tree*)PL_regdata->data[n];
2829 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2830 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2831 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2835 CALLRUNOPS(aTHX); /* Scalar context. */
2838 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2846 PAD_RESTORE_LOCAL(old_comppad);
2847 PL_curcop = ocurcop;
2849 if (logical == 2) { /* Postponed subexpression. */
2851 MAGIC *mg = Null(MAGIC*);
2853 CHECKPOINT cp, lastcp;
2857 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
2858 mg = mg_find(sv, PERL_MAGIC_qr);
2859 else if (SvSMAGICAL(ret)) {
2860 if (SvGMAGICAL(ret))
2861 sv_unmagic(ret, PERL_MAGIC_qr);
2863 mg = mg_find(ret, PERL_MAGIC_qr);
2867 re = (regexp *)mg->mg_obj;
2868 (void)ReREFCNT_inc(re);
2872 char *t = SvPV(ret, len);
2874 char *oprecomp = PL_regprecomp;
2875 I32 osize = PL_regsize;
2876 I32 onpar = PL_regnpar;
2879 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2880 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2882 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
2884 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2886 PL_regprecomp = oprecomp;
2891 PerlIO_printf(Perl_debug_log,
2892 "Entering embedded `%s%.60s%s%s'\n",
2896 (strlen(re->precomp) > 60 ? "..." : ""))
2899 state.prev = PL_reg_call_cc;
2900 state.cc = PL_regcc;
2901 state.re = PL_reg_re;
2905 cp = regcppush(0); /* Save *all* the positions. */
2908 state.ss = PL_savestack_ix;
2909 *PL_reglastparen = 0;
2910 *PL_reglastcloseparen = 0;
2911 PL_reg_call_cc = &state;
2912 PL_reginput = locinput;
2913 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2914 ((re->reganch & ROPT_UTF8) != 0);
2915 if (toggleutf) PL_reg_flags ^= RF_utf8;
2917 /* XXXX This is too dramatic a measure... */
2920 if (regmatch(re->program + 1)) {
2921 /* Even though we succeeded, we need to restore
2922 global variables, since we may be wrapped inside
2923 SUSPEND, thus the match may be not finished yet. */
2925 /* XXXX Do this only if SUSPENDed? */
2926 PL_reg_call_cc = state.prev;
2927 PL_regcc = state.cc;
2928 PL_reg_re = state.re;
2929 cache_re(PL_reg_re);
2930 if (toggleutf) PL_reg_flags ^= RF_utf8;
2932 /* XXXX This is too dramatic a measure... */
2935 /* These are needed even if not SUSPEND. */
2941 REGCP_UNWIND(lastcp);
2943 PL_reg_call_cc = state.prev;
2944 PL_regcc = state.cc;
2945 PL_reg_re = state.re;
2946 cache_re(PL_reg_re);
2947 if (toggleutf) PL_reg_flags ^= RF_utf8;
2949 /* XXXX This is too dramatic a measure... */
2959 sv_setsv(save_scalar(PL_replgv), ret);
2965 n = ARG(scan); /* which paren pair */
2966 PL_reg_start_tmp[n] = locinput;
2971 n = ARG(scan); /* which paren pair */
2972 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2973 PL_regendp[n] = locinput - PL_bostr;
2974 if (n > (I32)*PL_reglastparen)
2975 *PL_reglastparen = n;
2976 *PL_reglastcloseparen = n;
2979 n = ARG(scan); /* which paren pair */
2980 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2983 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2985 next = NEXTOPER(NEXTOPER(scan));
2987 next = scan + ARG(scan);
2988 if (OP(next) == IFTHEN) /* Fake one. */
2989 next = NEXTOPER(NEXTOPER(next));
2993 logical = scan->flags;
2995 /*******************************************************************
2996 PL_regcc contains infoblock about the innermost (...)* loop, and
2997 a pointer to the next outer infoblock.
2999 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3001 1) After matching X, regnode for CURLYX is processed;
3003 2) This regnode creates infoblock on the stack, and calls
3004 regmatch() recursively with the starting point at WHILEM node;
3006 3) Each hit of WHILEM node tries to match A and Z (in the order
3007 depending on the current iteration, min/max of {min,max} and
3008 greediness). The information about where are nodes for "A"
3009 and "Z" is read from the infoblock, as is info on how many times "A"
3010 was already matched, and greediness.
3012 4) After A matches, the same WHILEM node is hit again.
3014 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3015 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3016 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3017 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3018 of the external loop.
3020 Currently present infoblocks form a tree with a stem formed by PL_curcc
3021 and whatever it mentions via ->next, and additional attached trees
3022 corresponding to temporarily unset infoblocks as in "5" above.
3024 In the following picture infoblocks for outer loop of
3025 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3026 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3027 infoblocks are drawn below the "reset" infoblock.
3029 In fact in the picture below we do not show failed matches for Z and T
3030 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3031 more obvious *why* one needs to *temporary* unset infoblocks.]
3033 Matched REx position InfoBlocks Comment
3037 Y A)*?Z)*?T x <- O <- I
3038 YA )*?Z)*?T x <- O <- I
3039 YA A)*?Z)*?T x <- O <- I
3040 YAA )*?Z)*?T x <- O <- I
3041 YAA Z)*?T x <- O # Temporary unset I
3044 YAAZ Y(A)*?Z)*?T x <- O
3047 YAAZY (A)*?Z)*?T x <- O
3050 YAAZY A)*?Z)*?T x <- O <- I
3053 YAAZYA )*?Z)*?T x <- O <- I
3056 YAAZYA Z)*?T x <- O # Temporary unset I
3062 YAAZYAZ T x # Temporary unset O
3069 *******************************************************************/
3072 CHECKPOINT cp = PL_savestack_ix;
3073 /* No need to save/restore up to this paren */
3074 I32 parenfloor = scan->flags;
3076 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3078 cc.oldcc = PL_regcc;
3080 /* XXXX Probably it is better to teach regpush to support
3081 parenfloor > PL_regsize... */
3082 if (parenfloor > (I32)*PL_reglastparen)
3083 parenfloor = *PL_reglastparen; /* Pessimization... */
3084 cc.parenfloor = parenfloor;
3086 cc.min = ARG1(scan);
3087 cc.max = ARG2(scan);
3088 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3092 PL_reginput = locinput;
3093 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3095 PL_regcc = cc.oldcc;
3101 * This is really hard to understand, because after we match
3102 * what we're trying to match, we must make sure the rest of
3103 * the REx is going to match for sure, and to do that we have
3104 * to go back UP the parse tree by recursing ever deeper. And
3105 * if it fails, we have to reset our parent's current state
3106 * that we can try again after backing off.
3109 CHECKPOINT cp, lastcp;
3110 CURCUR* cc = PL_regcc;
3111 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3113 n = cc->cur + 1; /* how many we know we matched */
3114 PL_reginput = locinput;
3117 PerlIO_printf(Perl_debug_log,
3118 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3119 REPORT_CODE_OFF+PL_regindent*2, "",
3120 (long)n, (long)cc->min,
3121 (long)cc->max, PTR2UV(cc))
3124 /* If degenerate scan matches "", assume scan done. */
3126 if (locinput == cc->lastloc && n >= cc->min) {
3127 PL_regcc = cc->oldcc;
3131 PerlIO_printf(Perl_debug_log,
3132 "%*s empty match detected, try continuation...\n",
3133 REPORT_CODE_OFF+PL_regindent*2, "")
3135 if (regmatch(cc->next))
3143 /* First just match a string of min scans. */
3147 cc->lastloc = locinput;
3148 if (regmatch(cc->scan))
3151 cc->lastloc = lastloc;
3156 /* Check whether we already were at this position.
3157 Postpone detection until we know the match is not
3158 *that* much linear. */
3159 if (!PL_reg_maxiter) {
3160 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3161 PL_reg_leftiter = PL_reg_maxiter;
3163 if (PL_reg_leftiter-- == 0) {
3164 I32 size = (PL_reg_maxiter + 7)/8;
3165 if (PL_reg_poscache) {
3166 if ((I32)PL_reg_poscache_size < size) {
3167 Renew(PL_reg_poscache, size, char);
3168 PL_reg_poscache_size = size;
3170 Zero(PL_reg_poscache, size, char);
3173 PL_reg_poscache_size = size;
3174 Newz(29, PL_reg_poscache, size, char);
3177 PerlIO_printf(Perl_debug_log,
3178 "%sDetected a super-linear match, switching on caching%s...\n",
3179 PL_colors[4], PL_colors[5])
3182 if (PL_reg_leftiter < 0) {
3183 I32 o = locinput - PL_bostr, b;
3185 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3188 if (PL_reg_poscache[o] & (1<<b)) {
3190 PerlIO_printf(Perl_debug_log,
3191 "%*s already tried at this position...\n",
3192 REPORT_CODE_OFF+PL_regindent*2, "")
3194 if (PL_reg_flags & RF_false)
3199 PL_reg_poscache[o] |= (1<<b);
3203 /* Prefer next over scan for minimal matching. */
3206 PL_regcc = cc->oldcc;
3209 cp = regcppush(cc->parenfloor);
3211 if (regmatch(cc->next)) {
3213 sayYES; /* All done. */
3215 REGCP_UNWIND(lastcp);
3221 if (n >= cc->max) { /* Maximum greed exceeded? */
3222 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3223 && !(PL_reg_flags & RF_warned)) {
3224 PL_reg_flags |= RF_warned;
3225 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3226 "Complex regular subexpression recursion",
3233 PerlIO_printf(Perl_debug_log,
3234 "%*s trying longer...\n",
3235 REPORT_CODE_OFF+PL_regindent*2, "")
3237 /* Try scanning more and see if it helps. */
3238 PL_reginput = locinput;
3240 cc->lastloc = locinput;
3241 cp = regcppush(cc->parenfloor);
3243 if (regmatch(cc->scan)) {
3247 REGCP_UNWIND(lastcp);
3250 cc->lastloc = lastloc;
3254 /* Prefer scan over next for maximal matching. */
3256 if (n < cc->max) { /* More greed allowed? */
3257 cp = regcppush(cc->parenfloor);
3259 cc->lastloc = locinput;
3261 if (regmatch(cc->scan)) {
3265 REGCP_UNWIND(lastcp);
3266 regcppop(); /* Restore some previous $<digit>s? */
3267 PL_reginput = locinput;
3269 PerlIO_printf(Perl_debug_log,
3270 "%*s failed, try continuation...\n",
3271 REPORT_CODE_OFF+PL_regindent*2, "")
3274 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3275 && !(PL_reg_flags & RF_warned)) {
3276 PL_reg_flags |= RF_warned;
3277 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3278 "Complex regular subexpression recursion",
3282 /* Failed deeper matches of scan, so see if this one works. */
3283 PL_regcc = cc->oldcc;
3286 if (regmatch(cc->next))
3292 cc->lastloc = lastloc;
3297 next = scan + ARG(scan);
3300 inner = NEXTOPER(NEXTOPER(scan));
3303 inner = NEXTOPER(scan);
3307 if (OP(next) != c1) /* No choice. */
3308 next = inner; /* Avoid recursion. */
3310 I32 lastparen = *PL_reglastparen;
3312 re_unwind_branch_t *uw;
3314 /* Put unwinding data on stack */
3315 unwind1 = SSNEWt(1,re_unwind_branch_t);
3316 uw = SSPTRt(unwind1,re_unwind_branch_t);
3319 uw->type = ((c1 == BRANCH)
3321 : RE_UNWIND_BRANCHJ);
3322 uw->lastparen = lastparen;
3324 uw->locinput = locinput;
3325 uw->nextchr = nextchr;
3327 uw->regindent = ++PL_regindent;
3330 REGCP_SET(uw->lastcp);
3332 /* Now go into the first branch */
3345 /* We suppose that the next guy does not need
3346 backtracking: in particular, it is of constant length,
3347 and has no parenths to influence future backrefs. */
3348 ln = ARG1(scan); /* min to match */
3349 n = ARG2(scan); /* max to match */
3350 paren = scan->flags;
3352 if (paren > PL_regsize)
3354 if (paren > (I32)*PL_reglastparen)
3355 *PL_reglastparen = paren;
3357 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3359 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3360 PL_reginput = locinput;
3363 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3365 /* if we matched something zero-length we don't need to
3366 backtrack - capturing parens are already defined, so
3367 the caveat in the maximal case doesn't apply
3369 XXXX if ln == 0, we can redo this check first time
3370 through the following loop
3373 n = ln; /* don't backtrack */
3374 locinput = PL_reginput;
3375 if (HAS_TEXT(next) || JUMPABLE(next)) {
3376 regnode *text_node = next;
3378 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3380 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3382 if (PL_regkind[(U8)OP(text_node)] == REF) {
3386 else { c1 = (U8)*STRING(text_node); }
3387 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3389 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3390 c2 = PL_fold_locale[c1];
3399 /* This may be improved if l == 0. */
3400 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3401 /* If it could work, try it. */
3403 UCHARAT(PL_reginput) == c1 ||
3404 UCHARAT(PL_reginput) == c2)
3408 PL_regstartp[paren] =
3409 HOPc(PL_reginput, -l) - PL_bostr;
3410 PL_regendp[paren] = PL_reginput - PL_bostr;
3413 PL_regendp[paren] = -1;
3417 REGCP_UNWIND(lastcp);
3419 /* Couldn't or didn't -- move forward. */
3420 PL_reginput = locinput;
3421 if (regrepeat_hard(scan, 1, &l)) {
3423 locinput = PL_reginput;
3430 n = regrepeat_hard(scan, n, &l);
3431 /* if we matched something zero-length we don't need to
3432 backtrack, unless the minimum count is zero and we
3433 are capturing the result - in that case the capture
3434 being defined or not may affect later execution
3436 if (n != 0 && l == 0 && !(paren && ln == 0))
3437 ln = n; /* don't backtrack */
3438 locinput = PL_reginput;
3440 PerlIO_printf(Perl_debug_log,
3441 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3442 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3446 if (HAS_TEXT(next) || JUMPABLE(next)) {
3447 regnode *text_node = next;
3449 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3451 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3453 if (PL_regkind[(U8)OP(text_node)] == REF) {
3457 else { c1 = (U8)*STRING(text_node); }
3459 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3461 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3462 c2 = PL_fold_locale[c1];
3473 /* If it could work, try it. */
3475 UCHARAT(PL_reginput) == c1 ||
3476 UCHARAT(PL_reginput) == c2)
3479 PerlIO_printf(Perl_debug_log,
3480 "%*s trying tail with n=%"IVdf"...\n",
3481 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3485 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3486 PL_regendp[paren] = PL_reginput - PL_bostr;
3489 PL_regendp[paren] = -1;
3493 REGCP_UNWIND(lastcp);
3495 /* Couldn't or didn't -- back up. */
3497 locinput = HOPc(locinput, -l);
3498 PL_reginput = locinput;
3505 paren = scan->flags; /* Which paren to set */
3506 if (paren > PL_regsize)
3508 if (paren > (I32)*PL_reglastparen)
3509 *PL_reglastparen = paren;
3510 ln = ARG1(scan); /* min to match */
3511 n = ARG2(scan); /* max to match */
3512 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3516 ln = ARG1(scan); /* min to match */
3517 n = ARG2(scan); /* max to match */
3518 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3523 scan = NEXTOPER(scan);
3529 scan = NEXTOPER(scan);
3533 * Lookahead to avoid useless match attempts
3534 * when we know what character comes next.
3538 * Used to only do .*x and .*?x, but now it allows
3539 * for )'s, ('s and (?{ ... })'s to be in the way
3540 * of the quantifier and the EXACT-like node. -- japhy
3543 if (HAS_TEXT(next) || JUMPABLE(next)) {
3545 regnode *text_node = next;
3547 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3549 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3551 if (PL_regkind[(U8)OP(text_node)] == REF) {
3553 goto assume_ok_easy;
3555 else { s = (U8*)STRING(text_node); }
3559 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3561 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3562 c2 = PL_fold_locale[c1];
3565 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3566 STRLEN ulen1, ulen2;
3567 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3568 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3570 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3571 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3573 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3575 0 : UTF8_ALLOW_ANY);
3576 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3578 0 : UTF8_ALLOW_ANY);
3581 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3583 0 : UTF8_ALLOW_ANY);
3591 PL_reginput = locinput;
3595 if (ln && regrepeat(scan, ln) < ln)
3597 locinput = PL_reginput;
3600 char *e; /* Should not check after this */
3601 char *old = locinput;
3604 if (n == REG_INFTY) {
3607 while (UTF8_IS_CONTINUATION(*(U8*)e))
3613 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3617 e = locinput + n - ln;
3622 /* Find place 'next' could work */
3625 while (locinput <= e &&
3626 UCHARAT(locinput) != c1)
3629 while (locinput <= e
3630 && UCHARAT(locinput) != c1
3631 && UCHARAT(locinput) != c2)
3634 count = locinput - old;
3639 /* count initialised to
3640 * utf8_distance(old, locinput) */
3641 while (locinput <= e &&
3642 utf8n_to_uvchr((U8*)locinput,
3645 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3650 /* count initialised to
3651 * utf8_distance(old, locinput) */
3652 while (locinput <= e) {
3653 UV c = utf8n_to_uvchr((U8*)locinput,
3656 0 : UTF8_ALLOW_ANY);
3657 if (c == (UV)c1 || c == (UV)c2)
3666 /* PL_reginput == old now */
3667 if (locinput != old) {
3668 ln = 1; /* Did some */
3669 if (regrepeat(scan, count) < count)
3672 /* PL_reginput == locinput now */
3673 TRYPAREN(paren, ln, locinput);
3674 PL_reginput = locinput; /* Could be reset... */
3675 REGCP_UNWIND(lastcp);
3676 /* Couldn't or didn't -- move forward. */
3679 locinput += UTF8SKIP(locinput);
3686 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3690 c = utf8n_to_uvchr((U8*)PL_reginput,
3693 0 : UTF8_ALLOW_ANY);
3695 c = UCHARAT(PL_reginput);
3696 /* If it could work, try it. */
3697 if (c == (UV)c1 || c == (UV)c2)
3699 TRYPAREN(paren, ln, PL_reginput);
3700 REGCP_UNWIND(lastcp);
3703 /* If it could work, try it. */
3704 else if (c1 == -1000)
3706 TRYPAREN(paren, ln, PL_reginput);
3707 REGCP_UNWIND(lastcp);
3709 /* Couldn't or didn't -- move forward. */
3710 PL_reginput = locinput;
3711 if (regrepeat(scan, 1)) {
3713 locinput = PL_reginput;
3721 n = regrepeat(scan, n);
3722 locinput = PL_reginput;
3723 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3724 ((!PL_multiline && OP(next) != MEOL) ||
3725 OP(next) == SEOL || OP(next) == EOS))
3727 ln = n; /* why back off? */
3728 /* ...because $ and \Z can match before *and* after
3729 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3730 We should back off by one in this case. */
3731 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3740 c = utf8n_to_uvchr((U8*)PL_reginput,
3743 0 : UTF8_ALLOW_ANY);
3745 c = UCHARAT(PL_reginput);
3747 /* If it could work, try it. */
3748 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3750 TRYPAREN(paren, n, PL_reginput);
3751 REGCP_UNWIND(lastcp);
3753 /* Couldn't or didn't -- back up. */
3755 PL_reginput = locinput = HOPc(locinput, -1);
3763 c = utf8n_to_uvchr((U8*)PL_reginput,
3766 0 : UTF8_ALLOW_ANY);
3768 c = UCHARAT(PL_reginput);
3770 /* If it could work, try it. */
3771 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3773 TRYPAREN(paren, n, PL_reginput);
3774 REGCP_UNWIND(lastcp);
3776 /* Couldn't or didn't -- back up. */
3778 PL_reginput = locinput = HOPc(locinput, -1);
3785 if (PL_reg_call_cc) {
3786 re_cc_state *cur_call_cc = PL_reg_call_cc;
3787 CURCUR *cctmp = PL_regcc;
3788 regexp *re = PL_reg_re;
3789 CHECKPOINT cp, lastcp;
3791 cp = regcppush(0); /* Save *all* the positions. */
3793 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3795 PL_reginput = locinput; /* Make position available to
3797 cache_re(PL_reg_call_cc->re);
3798 PL_regcc = PL_reg_call_cc->cc;
3799 PL_reg_call_cc = PL_reg_call_cc->prev;
3800 if (regmatch(cur_call_cc->node)) {
3801 PL_reg_call_cc = cur_call_cc;
3805 REGCP_UNWIND(lastcp);
3807 PL_reg_call_cc = cur_call_cc;
3813 PerlIO_printf(Perl_debug_log,
3814 "%*s continuation failed...\n",
3815 REPORT_CODE_OFF+PL_regindent*2, "")
3819 if (locinput < PL_regtill) {
3820 DEBUG_r(PerlIO_printf(Perl_debug_log,
3821 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3823 (long)(locinput - PL_reg_starttry),
3824 (long)(PL_regtill - PL_reg_starttry),
3826 sayNO_FINAL; /* Cannot match: too short. */
3828 PL_reginput = locinput; /* put where regtry can find it */
3829 sayYES_FINAL; /* Success! */
3831 PL_reginput = locinput; /* put where regtry can find it */
3832 sayYES_LOUD; /* Success! */
3835 PL_reginput = locinput;
3840 s = HOPBACKc(locinput, scan->flags);
3846 PL_reginput = locinput;
3847 PL_reg_flags ^= RF_false;
3852 s = HOPBACKc(locinput, scan->flags);
3858 PL_reginput = locinput;
3861 inner = NEXTOPER(NEXTOPER(scan));
3862 if (regmatch(inner) != n) {
3864 PL_reg_flags ^= RF_false;
3875 PL_reg_flags ^= RF_false;
3881 if (OP(scan) == SUSPEND) {
3882 locinput = PL_reginput;
3883 nextchr = UCHARAT(locinput);
3888 next = scan + ARG(scan);
3893 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3894 PTR2UV(scan), OP(scan));
3895 Perl_croak(aTHX_ "regexp memory corruption");
3902 * We get here only if there's trouble -- normally "case END" is
3903 * the terminating point.
3905 Perl_croak(aTHX_ "corrupted regexp pointers");
3911 PerlIO_printf(Perl_debug_log,
3912 "%*s %scould match...%s\n",
3913 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3917 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3918 PL_colors[4],PL_colors[5]));
3924 #if 0 /* Breaks $^R */
3932 PerlIO_printf(Perl_debug_log,
3933 "%*s %sfailed...%s\n",
3934 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3940 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3943 case RE_UNWIND_BRANCH:
3944 case RE_UNWIND_BRANCHJ:
3946 re_unwind_branch_t *uwb = &(uw->branch);
3947 I32 lastparen = uwb->lastparen;
3949 REGCP_UNWIND(uwb->lastcp);
3950 for (n = *PL_reglastparen; n > lastparen; n--)
3952 *PL_reglastparen = n;
3953 scan = next = uwb->next;
3955 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3956 ? BRANCH : BRANCHJ) ) { /* Failure */
3963 /* Have more choice yet. Reuse the same uwb. */
3965 if ((n = (uwb->type == RE_UNWIND_BRANCH
3966 ? NEXT_OFF(next) : ARG(next))))
3969 next = NULL; /* XXXX Needn't unwinding in this case... */
3971 next = NEXTOPER(scan);
3972 if (uwb->type == RE_UNWIND_BRANCHJ)
3973 next = NEXTOPER(next);
3974 locinput = uwb->locinput;
3975 nextchr = uwb->nextchr;
3977 PL_regindent = uwb->regindent;
3984 Perl_croak(aTHX_ "regexp unwind memory corruption");
3995 - regrepeat - repeatedly match something simple, report how many
3998 * [This routine now assumes that it will only match on things of length 1.
3999 * That was true before, but now we assume scan - reginput is the count,
4000 * rather than incrementing count on every character. [Er, except utf8.]]
4003 S_regrepeat(pTHX_ regnode *p, I32 max)
4005 register char *scan;
4007 register char *loceol = PL_regeol;
4008 register I32 hardcount = 0;
4009 register bool do_utf8 = PL_reg_match_utf8;
4012 if (max == REG_INFTY)
4014 else if (max < loceol - scan)
4015 loceol = scan + max;
4020 while (scan < loceol && hardcount < max && *scan != '\n') {
4021 scan += UTF8SKIP(scan);
4025 while (scan < loceol && *scan != '\n')
4032 while (scan < loceol && hardcount < max) {
4033 scan += UTF8SKIP(scan);
4043 case EXACT: /* length of string is 1 */
4045 while (scan < loceol && UCHARAT(scan) == c)
4048 case EXACTF: /* length of string is 1 */
4050 while (scan < loceol &&
4051 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4054 case EXACTFL: /* length of string is 1 */
4055 PL_reg_flags |= RF_tainted;
4057 while (scan < loceol &&
4058 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4064 while (hardcount < max && scan < loceol &&
4065 reginclass(p, (U8*)scan, 0, do_utf8)) {
4066 scan += UTF8SKIP(scan);
4070 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4077 LOAD_UTF8_CHARCLASS(alnum,"a");
4078 while (hardcount < max && scan < loceol &&
4079 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4080 scan += UTF8SKIP(scan);
4084 while (scan < loceol && isALNUM(*scan))
4089 PL_reg_flags |= RF_tainted;
4092 while (hardcount < max && scan < loceol &&
4093 isALNUM_LC_utf8((U8*)scan)) {
4094 scan += UTF8SKIP(scan);
4098 while (scan < loceol && isALNUM_LC(*scan))
4105 LOAD_UTF8_CHARCLASS(alnum,"a");
4106 while (hardcount < max && scan < loceol &&
4107 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4108 scan += UTF8SKIP(scan);
4112 while (scan < loceol && !isALNUM(*scan))
4117 PL_reg_flags |= RF_tainted;
4120 while (hardcount < max && scan < loceol &&
4121 !isALNUM_LC_utf8((U8*)scan)) {
4122 scan += UTF8SKIP(scan);
4126 while (scan < loceol && !isALNUM_LC(*scan))
4133 LOAD_UTF8_CHARCLASS(space," ");
4134 while (hardcount < max && scan < loceol &&
4136 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4137 scan += UTF8SKIP(scan);
4141 while (scan < loceol && isSPACE(*scan))
4146 PL_reg_flags |= RF_tainted;
4149 while (hardcount < max && scan < loceol &&
4150 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4151 scan += UTF8SKIP(scan);
4155 while (scan < loceol && isSPACE_LC(*scan))
4162 LOAD_UTF8_CHARCLASS(space," ");
4163 while (hardcount < max && scan < loceol &&
4165 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4166 scan += UTF8SKIP(scan);
4170 while (scan < loceol && !isSPACE(*scan))
4175 PL_reg_flags |= RF_tainted;
4178 while (hardcount < max && scan < loceol &&
4179 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4180 scan += UTF8SKIP(scan);
4184 while (scan < loceol && !isSPACE_LC(*scan))
4191 LOAD_UTF8_CHARCLASS(digit,"0");
4192 while (hardcount < max && scan < loceol &&
4193 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4194 scan += UTF8SKIP(scan);
4198 while (scan < loceol && isDIGIT(*scan))
4205 LOAD_UTF8_CHARCLASS(digit,"0");
4206 while (hardcount < max && scan < loceol &&
4207 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4208 scan += UTF8SKIP(scan);
4212 while (scan < loceol && !isDIGIT(*scan))
4216 default: /* Called on something of 0 width. */
4217 break; /* So match right here or not at all. */
4223 c = scan - PL_reginput;
4228 SV *prop = sv_newmortal();
4231 PerlIO_printf(Perl_debug_log,
4232 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4233 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4240 - regrepeat_hard - repeatedly match something, report total lenth and length
4242 * The repeater is supposed to have constant length.
4246 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4248 register char *scan = Nullch;
4249 register char *start;
4250 register char *loceol = PL_regeol;
4252 I32 count = 0, res = 1;
4257 start = PL_reginput;
4258 if (PL_reg_match_utf8) {
4259 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4262 while (start < PL_reginput) {
4264 start += UTF8SKIP(start);
4275 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4277 *lp = l = PL_reginput - start;
4278 if (max != REG_INFTY && l*max < loceol - scan)
4279 loceol = scan + l*max;
4292 - regclass_swash - prepare the utf8 swash
4296 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4302 if (PL_regdata && PL_regdata->count) {
4305 if (PL_regdata->what[n] == 's') {
4306 SV *rv = (SV*)PL_regdata->data[n];
4307 AV *av = (AV*)SvRV((SV*)rv);
4308 SV **ary = AvARRAY(av);
4311 /* See the end of regcomp.c:S_reglass() for
4312 * documentation of these array elements. */
4315 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4316 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4320 else if (si && doinit) {
4321 sw = swash_init("utf8", "", si, 1, 0);
4322 (void)av_store(av, 1, sw);
4338 - reginclass - determine if a character falls into a character class
4340 The n is the ANYOF regnode, the p is the target string, lenp
4341 is pointer to the maximum length of how far to go in the p
4342 (if the lenp is zero, UTF8SKIP(p) is used),
4343 do_utf8 tells whether the target string is in UTF-8.
4348 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4350 char flags = ANYOF_FLAGS(n);
4356 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4357 c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4358 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4360 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4361 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4364 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4365 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4368 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4372 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4375 if (swash_fetch(sw, p, do_utf8))
4377 else if (flags & ANYOF_FOLD) {
4378 if (!match && lenp && av) {
4381 for (i = 0; i <= av_len(av); i++) {
4382 SV* sv = *av_fetch(av, i, FALSE);
4384 char *s = SvPV(sv, len);
4386 if (len <= plen && memEQ(s, (char*)p, len)) {
4394 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4397 to_utf8_fold(p, tmpbuf, &tmplen);
4398 if (swash_fetch(sw, tmpbuf, do_utf8))
4404 if (match && lenp && *lenp == 0)
4405 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4407 if (!match && c < 256) {
4408 if (ANYOF_BITMAP_TEST(n, c))
4410 else if (flags & ANYOF_FOLD) {
4413 if (flags & ANYOF_LOCALE) {
4414 PL_reg_flags |= RF_tainted;
4415 f = PL_fold_locale[c];
4419 if (f != c && ANYOF_BITMAP_TEST(n, f))
4423 if (!match && (flags & ANYOF_CLASS)) {
4424 PL_reg_flags |= RF_tainted;
4426 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4427 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4428 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4429 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4430 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4431 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4432 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4433 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4434 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4435 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4436 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4437 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4438 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4439 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4440 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4441 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4442 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4443 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4444 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4445 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4446 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4447 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4448 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4449 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4450 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4451 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4452 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4453 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4454 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4455 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4456 ) /* How's that for a conditional? */
4463 return (flags & ANYOF_INVERT) ? !match : match;
4467 S_reghop(pTHX_ U8 *s, I32 off)
4469 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4473 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4476 while (off-- && s < lim) {
4477 /* XXX could check well-formedness here */
4485 if (UTF8_IS_CONTINUED(*s)) {
4486 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4489 /* XXX could check well-formedness here */
4497 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4499 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4503 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4506 while (off-- && s < lim) {
4507 /* XXX could check well-formedness here */
4517 if (UTF8_IS_CONTINUED(*s)) {
4518 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4521 /* XXX could check well-formedness here */
4533 restore_pos(pTHX_ void *arg)
4535 if (PL_reg_eval_set) {
4536 if (PL_reg_oldsaved) {
4537 PL_reg_re->subbeg = PL_reg_oldsaved;
4538 PL_reg_re->sublen = PL_reg_oldsavedlen;
4539 RX_MATCH_COPIED_on(PL_reg_re);
4541 PL_reg_magic->mg_len = PL_reg_oldpos;
4542 PL_reg_eval_set = 0;
4543 PL_curpm = PL_reg_oldcurpm;
4548 S_to_utf8_substr(pTHX_ register regexp *prog)
4551 if (prog->float_substr && !prog->float_utf8) {
4552 prog->float_utf8 = sv = NEWSV(117, 0);
4553 SvSetSV(sv, prog->float_substr);
4554 sv_utf8_upgrade(sv);
4555 if (SvTAIL(prog->float_substr))
4557 if (prog->float_substr == prog->check_substr)
4558 prog->check_utf8 = sv;
4560 if (prog->anchored_substr && !prog->anchored_utf8) {
4561 prog->anchored_utf8 = sv = NEWSV(118, 0);
4562 SvSetSV(sv, prog->anchored_substr);
4563 sv_utf8_upgrade(sv);
4564 if (SvTAIL(prog->anchored_substr))
4566 if (prog->anchored_substr == prog->check_substr)
4567 prog->check_utf8 = sv;
4572 S_to_byte_substr(pTHX_ register regexp *prog)
4575 if (prog->float_utf8 && !prog->float_substr) {
4576 prog->float_substr = sv = NEWSV(117, 0);
4577 SvSetSV(sv, prog->float_utf8);
4578 if (sv_utf8_downgrade(sv, TRUE)) {
4579 if (SvTAIL(prog->float_utf8))
4583 prog->float_substr = sv = &PL_sv_undef;
4585 if (prog->float_utf8 == prog->check_utf8)
4586 prog->check_substr = sv;
4588 if (prog->anchored_utf8 && !prog->anchored_substr) {
4589 prog->anchored_substr = sv = NEWSV(118, 0);
4590 SvSetSV(sv, prog->anchored_utf8);
4591 if (sv_utf8_downgrade(sv, TRUE)) {
4592 if (SvTAIL(prog->anchored_utf8))
4596 prog->anchored_substr = sv = &PL_sv_undef;
4598 if (prog->anchored_utf8 == prog->check_utf8)
4599 prog->check_substr = sv;