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? */
91 #define UTF ((PL_reg_flags & RF_utf8) != 0)
93 #define RS_init 1 /* eval environment created */
94 #define RS_set 2 /* replsv value is set */
100 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
106 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
107 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
109 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
110 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
111 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
112 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
113 #define HOPc(pos,off) ((char*)HOP(pos,off))
114 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
116 #define HOPBACK(pos, off) ( \
117 (PL_reg_match_utf8) \
118 ? reghopmaybe((U8*)pos, -off) \
119 : (pos - off >= PL_bostr) \
123 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
125 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
126 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
127 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
128 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
129 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
130 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
132 #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
134 /* for use after a quantifier and before an EXACT-like node -- japhy */
135 #define JUMPABLE(rn) ( \
136 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
137 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
138 OP(rn) == PLUS || OP(rn) == MINMOD || \
139 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
142 #define HAS_TEXT(rn) ( \
143 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
147 Search for mandatory following text node; for lookahead, the text must
148 follow but for lookbehind (rn->flags != 0) we skip to the next step.
150 #define FIND_NEXT_IMPT(rn) STMT_START { \
151 while (JUMPABLE(rn)) \
152 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
153 rn = NEXTOPER(NEXTOPER(rn)); \
154 else if (OP(rn) == PLUS) \
156 else if (OP(rn) == IFMATCH) \
157 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
158 else rn += NEXT_OFF(rn); \
161 static void restore_pos(pTHX_ void *arg);
164 S_regcppush(pTHX_ I32 parenfloor)
166 int retval = PL_savestack_ix;
167 #define REGCP_PAREN_ELEMS 4
168 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
171 if (paren_elems_to_push < 0)
172 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
174 #define REGCP_OTHER_ELEMS 6
175 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
176 for (p = PL_regsize; p > parenfloor; p--) {
177 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
178 SSPUSHINT(PL_regendp[p]);
179 SSPUSHINT(PL_regstartp[p]);
180 SSPUSHPTR(PL_reg_start_tmp[p]);
183 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
184 SSPUSHINT(PL_regsize);
185 SSPUSHINT(*PL_reglastparen);
186 SSPUSHINT(*PL_reglastcloseparen);
187 SSPUSHPTR(PL_reginput);
188 #define REGCP_FRAME_ELEMS 2
189 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
190 * are needed for the regexp context stack bookkeeping. */
191 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
192 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
197 /* These are needed since we do not localize EVAL nodes: */
198 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
199 " Setting an EVAL scope, savestack=%"IVdf"\n", \
200 (IV)PL_savestack_ix)); cp = PL_savestack_ix
202 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
203 PerlIO_printf(Perl_debug_log, \
204 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
205 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
215 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
217 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
218 i = SSPOPINT; /* Parentheses elements to pop. */
219 input = (char *) SSPOPPTR;
220 *PL_reglastcloseparen = SSPOPINT;
221 *PL_reglastparen = SSPOPINT;
222 PL_regsize = SSPOPINT;
224 /* Now restore the parentheses context. */
225 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
226 i > 0; i -= REGCP_PAREN_ELEMS) {
227 paren = (U32)SSPOPINT;
228 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
229 PL_regstartp[paren] = SSPOPINT;
231 if (paren <= *PL_reglastparen)
232 PL_regendp[paren] = tmps;
234 PerlIO_printf(Perl_debug_log,
235 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
236 (UV)paren, (IV)PL_regstartp[paren],
237 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
238 (IV)PL_regendp[paren],
239 (paren > *PL_reglastparen ? "(no)" : ""));
243 if ((I32)(*PL_reglastparen + 1) <= PL_regnpar) {
244 PerlIO_printf(Perl_debug_log,
245 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
246 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
250 /* It would seem that the similar code in regtry()
251 * already takes care of this, and in fact it is in
252 * a better location to since this code can #if 0-ed out
253 * but the code in regtry() is needed or otherwise tests
254 * requiring null fields (pat.t#187 and split.t#{13,14}
255 * (as of patchlevel 7877) will fail. Then again,
256 * this code seems to be necessary or otherwise
257 * building DynaLoader will fail:
258 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
260 for (paren = *PL_reglastparen + 1; (I32)paren <= PL_regnpar; paren++) {
261 if ((I32)paren > PL_regsize)
262 PL_regstartp[paren] = -1;
263 PL_regendp[paren] = -1;
270 S_regcp_set_to(pTHX_ I32 ss)
272 I32 tmp = PL_savestack_ix;
274 PL_savestack_ix = ss;
276 PL_savestack_ix = tmp;
280 typedef struct re_cc_state
284 struct re_cc_state *prev;
289 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
291 #define TRYPAREN(paren, n, input) { \
294 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
295 PL_regendp[paren] = input - PL_bostr; \
298 PL_regendp[paren] = -1; \
300 if (regmatch(next)) \
303 PL_regendp[paren] = -1; \
308 * pregexec and friends
312 - pregexec - match a regexp against a string
315 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
316 char *strbeg, I32 minend, SV *screamer, U32 nosave)
317 /* strend: pointer to null at end of string */
318 /* strbeg: real beginning of string */
319 /* minend: end of match must be >=minend after stringarg. */
320 /* nosave: For optimizations. */
323 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
324 nosave ? 0 : REXEC_COPY_STR);
328 S_cache_re(pTHX_ regexp *prog)
330 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
332 PL_regprogram = prog->program;
334 PL_regnpar = prog->nparens;
335 PL_regdata = prog->data;
340 * Need to implement the following flags for reg_anch:
342 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
344 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
345 * INTUIT_AUTORITATIVE_ML
346 * INTUIT_ONCE_NOML - Intuit can match in one location only.
349 * Another flag for this function: SECOND_TIME (so that float substrs
350 * with giant delta may be not rechecked).
353 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
355 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
356 Otherwise, only SvCUR(sv) is used to get strbeg. */
358 /* XXXX We assume that strpos is strbeg unless sv. */
360 /* XXXX Some places assume that there is a fixed substring.
361 An update may be needed if optimizer marks as "INTUITable"
362 RExen without fixed substrings. Similarly, it is assumed that
363 lengths of all the strings are no more than minlen, thus they
364 cannot come from lookahead.
365 (Or minlen should take into account lookahead.) */
367 /* A failure to find a constant substring means that there is no need to make
368 an expensive call to REx engine, thus we celebrate a failure. Similarly,
369 finding a substring too deep into the string means that less calls to
370 regtry() should be needed.
372 REx compiler's optimizer found 4 possible hints:
373 a) Anchored substring;
375 c) Whether we are anchored (beginning-of-line or \G);
376 d) First node (of those at offset 0) which may distingush positions;
377 We use a)b)d) and multiline-part of c), and try to find a position in the
378 string which does not contradict any of them.
381 /* Most of decisions we do here should have been done at compile time.
382 The nodes of the REx which we used for the search should have been
383 deleted from the finite automaton. */
386 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
387 char *strend, U32 flags, re_scream_pos_data *data)
389 register I32 start_shift = 0;
390 /* Should be nonnegative! */
391 register I32 end_shift = 0;
396 int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
398 register char *other_last = Nullch; /* other substr checked before this */
399 char *check_at = Nullch; /* check substr found at this pos */
401 char *i_strpos = strpos;
402 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
404 RX_MATCH_UTF8_set(prog,do_utf8);
406 if (prog->reganch & ROPT_UTF8) {
407 DEBUG_r(PerlIO_printf(Perl_debug_log,
408 "UTF-8 regex...\n"));
409 PL_reg_flags |= RF_utf8;
413 char *s = PL_reg_match_utf8 ?
414 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
416 int len = PL_reg_match_utf8 ?
417 strlen(s) : strend - strpos;
420 if (PL_reg_match_utf8)
421 DEBUG_r(PerlIO_printf(Perl_debug_log,
422 "UTF-8 target...\n"));
423 PerlIO_printf(Perl_debug_log,
424 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
425 PL_colors[4],PL_colors[5],PL_colors[0],
428 (strlen(prog->precomp) > 60 ? "..." : ""),
430 (int)(len > 60 ? 60 : len),
432 (len > 60 ? "..." : "")
436 /* CHR_DIST() would be more correct here but it makes things slow. */
437 if (prog->minlen > strend - strpos) {
438 DEBUG_r(PerlIO_printf(Perl_debug_log,
439 "String too short... [re_intuit_start]\n"));
442 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
445 if (!prog->check_utf8 && prog->check_substr)
446 to_utf8_substr(prog);
447 check = prog->check_utf8;
449 if (!prog->check_substr && prog->check_utf8)
450 to_byte_substr(prog);
451 check = prog->check_substr;
453 if (check == &PL_sv_undef) {
454 DEBUG_r(PerlIO_printf(Perl_debug_log,
455 "Non-utf string cannot match utf check string\n"));
458 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
459 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
460 || ( (prog->reganch & ROPT_ANCH_BOL)
461 && !PL_multiline ) ); /* Check after \n? */
464 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
465 | ROPT_IMPLICIT)) /* not a real BOL */
466 /* SvCUR is not set on references: SvRV and SvPVX overlap */
468 && (strpos != strbeg)) {
469 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
472 if (prog->check_offset_min == prog->check_offset_max &&
473 !(prog->reganch & ROPT_CANY_SEEN)) {
474 /* Substring at constant offset from beg-of-str... */
477 s = HOP3c(strpos, prog->check_offset_min, strend);
479 slen = SvCUR(check); /* >= 1 */
481 if ( strend - s > slen || strend - s < slen - 1
482 || (strend - s == slen && strend[-1] != '\n')) {
483 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
486 /* Now should match s[0..slen-2] */
488 if (slen && (*SvPVX(check) != *s
490 && memNE(SvPVX(check), s, slen)))) {
492 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
496 else if (*SvPVX(check) != *s
497 || ((slen = SvCUR(check)) > 1
498 && memNE(SvPVX(check), s, slen)))
500 goto success_at_start;
503 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
505 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
506 end_shift = prog->minlen - start_shift -
507 CHR_SVLEN(check) + (SvTAIL(check) != 0);
509 I32 end = prog->check_offset_max + CHR_SVLEN(check)
510 - (SvTAIL(check) != 0);
511 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
513 if (end_shift < eshift)
517 else { /* Can match at random position */
520 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
521 /* Should be nonnegative! */
522 end_shift = prog->minlen - start_shift -
523 CHR_SVLEN(check) + (SvTAIL(check) != 0);
526 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
528 Perl_croak(aTHX_ "panic: end_shift");
532 /* Find a possible match in the region s..strend by looking for
533 the "check" substring in the region corrected by start/end_shift. */
534 if (flags & REXEC_SCREAM) {
535 I32 p = -1; /* Internal iterator of scream. */
536 I32 *pp = data ? data->scream_pos : &p;
538 if (PL_screamfirst[BmRARE(check)] >= 0
539 || ( BmRARE(check) == '\n'
540 && (BmPREVIOUS(check) == SvCUR(check) - 1)
542 s = screaminstr(sv, check,
543 start_shift + (s - strbeg), end_shift, pp, 0);
546 /* we may be pointing at the wrong string */
547 if (s && RX_MATCH_COPIED(prog))
548 s = strbeg + (s - SvPVX(sv));
550 *data->scream_olds = s;
552 else if (prog->reganch & ROPT_CANY_SEEN)
553 s = fbm_instr((U8*)(s + start_shift),
554 (U8*)(strend - end_shift),
555 check, PL_multiline ? FBMrf_MULTILINE : 0);
557 s = fbm_instr(HOP3(s, start_shift, strend),
558 HOP3(strend, -end_shift, strbeg),
559 check, PL_multiline ? FBMrf_MULTILINE : 0);
561 /* Update the count-of-usability, remove useless subpatterns,
564 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
565 (s ? "Found" : "Did not find"),
566 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
568 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
570 PL_colors[1], (SvTAIL(check) ? "$" : ""),
571 (s ? " at offset " : "...\n") ) );
578 /* Finish the diagnostic message */
579 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
581 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
582 Start with the other substr.
583 XXXX no SCREAM optimization yet - and a very coarse implementation
584 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
585 *always* match. Probably should be marked during compile...
586 Probably it is right to do no SCREAM here...
589 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
590 /* Take into account the "other" substring. */
591 /* XXXX May be hopelessly wrong for UTF... */
594 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
597 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
601 t = s - prog->check_offset_max;
602 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
604 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
609 t = HOP3c(t, prog->anchored_offset, strend);
610 if (t < other_last) /* These positions already checked */
612 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
615 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
616 /* On end-of-str: see comment below. */
617 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
618 if (must == &PL_sv_undef) {
620 DEBUG_r(must = prog->anchored_utf8); /* for debug */
625 HOP3(HOP3(last1, prog->anchored_offset, strend)
626 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
628 PL_multiline ? FBMrf_MULTILINE : 0
630 DEBUG_r(PerlIO_printf(Perl_debug_log,
631 "%s anchored substr `%s%.*s%s'%s",
632 (s ? "Found" : "Contradicts"),
635 - (SvTAIL(must)!=0)),
637 PL_colors[1], (SvTAIL(must) ? "$" : "")));
639 if (last1 >= last2) {
640 DEBUG_r(PerlIO_printf(Perl_debug_log,
641 ", giving up...\n"));
644 DEBUG_r(PerlIO_printf(Perl_debug_log,
645 ", trying floating at offset %ld...\n",
646 (long)(HOP3c(s1, 1, strend) - i_strpos)));
647 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
648 s = HOP3c(last, 1, strend);
652 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
653 (long)(s - i_strpos)));
654 t = HOP3c(s, -prog->anchored_offset, strbeg);
655 other_last = HOP3c(s, 1, strend);
663 else { /* Take into account the floating substring. */
668 t = HOP3c(s, -start_shift, strbeg);
670 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
671 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
672 last = HOP3c(t, prog->float_max_offset, strend);
673 s = HOP3c(t, prog->float_min_offset, strend);
676 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
677 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
678 /* fbm_instr() takes into account exact value of end-of-str
679 if the check is SvTAIL(ed). Since false positives are OK,
680 and end-of-str is not later than strend we are OK. */
681 if (must == &PL_sv_undef) {
683 DEBUG_r(must = prog->float_utf8); /* for debug message */
686 s = fbm_instr((unsigned char*)s,
687 (unsigned char*)last + SvCUR(must)
689 must, PL_multiline ? FBMrf_MULTILINE : 0);
690 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
691 (s ? "Found" : "Contradicts"),
693 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
695 PL_colors[1], (SvTAIL(must) ? "$" : "")));
698 DEBUG_r(PerlIO_printf(Perl_debug_log,
699 ", giving up...\n"));
702 DEBUG_r(PerlIO_printf(Perl_debug_log,
703 ", trying anchored starting at offset %ld...\n",
704 (long)(s1 + 1 - i_strpos)));
706 s = HOP3c(t, 1, strend);
710 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
711 (long)(s - i_strpos)));
712 other_last = s; /* Fix this later. --Hugo */
721 t = s - prog->check_offset_max;
722 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
724 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
726 /* Fixed substring is found far enough so that the match
727 cannot start at strpos. */
729 if (ml_anch && t[-1] != '\n') {
730 /* Eventually fbm_*() should handle this, but often
731 anchored_offset is not 0, so this check will not be wasted. */
732 /* XXXX In the code below we prefer to look for "^" even in
733 presence of anchored substrings. And we search even
734 beyond the found float position. These pessimizations
735 are historical artefacts only. */
737 while (t < strend - prog->minlen) {
739 if (t < check_at - prog->check_offset_min) {
740 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
741 /* Since we moved from the found position,
742 we definitely contradict the found anchored
743 substr. Due to the above check we do not
744 contradict "check" substr.
745 Thus we can arrive here only if check substr
746 is float. Redo checking for "other"=="fixed".
749 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
750 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
751 goto do_other_anchored;
753 /* We don't contradict the found floating substring. */
754 /* XXXX Why not check for STCLASS? */
756 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
757 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
760 /* Position contradicts check-string */
761 /* XXXX probably better to look for check-string
762 than for "\n", so one should lower the limit for t? */
763 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
764 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
765 other_last = strpos = s = t + 1;
770 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
771 PL_colors[0],PL_colors[1]));
775 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
776 PL_colors[0],PL_colors[1]));
780 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
783 /* The found string does not prohibit matching at strpos,
784 - no optimization of calling REx engine can be performed,
785 unless it was an MBOL and we are not after MBOL,
786 or a future STCLASS check will fail this. */
788 /* Even in this situation we may use MBOL flag if strpos is offset
789 wrt the start of the string. */
790 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
791 && (strpos != strbeg) && strpos[-1] != '\n'
792 /* May be due to an implicit anchor of m{.*foo} */
793 && !(prog->reganch & ROPT_IMPLICIT))
798 DEBUG_r( if (ml_anch)
799 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
800 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
803 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
805 prog->check_utf8 /* Could be deleted already */
806 && --BmUSEFUL(prog->check_utf8) < 0
807 && (prog->check_utf8 == prog->float_utf8)
809 prog->check_substr /* Could be deleted already */
810 && --BmUSEFUL(prog->check_substr) < 0
811 && (prog->check_substr == prog->float_substr)
814 /* If flags & SOMETHING - do not do it many times on the same match */
815 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
816 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
817 if (do_utf8 ? prog->check_substr : prog->check_utf8)
818 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
819 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
820 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
821 check = Nullsv; /* abort */
823 /* XXXX This is a remnant of the old implementation. It
824 looks wasteful, since now INTUIT can use many
826 prog->reganch &= ~RE_USE_INTUIT;
833 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
834 if (prog->regstclass) {
835 /* minlen == 0 is possible if regstclass is \b or \B,
836 and the fixed substr is ''$.
837 Since minlen is already taken into account, s+1 is before strend;
838 accidentally, minlen >= 1 guaranties no false positives at s + 1
839 even for \b or \B. But (minlen? 1 : 0) below assumes that
840 regstclass does not come from lookahead... */
841 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
842 This leaves EXACTF only, which is dealt with in find_byclass(). */
843 U8* str = (U8*)STRING(prog->regstclass);
844 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
845 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
847 char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
848 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
849 : (prog->float_substr || prog->float_utf8
850 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
853 char *startpos = strbeg;
857 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
862 if (endpos == strend) {
863 DEBUG_r( PerlIO_printf(Perl_debug_log,
864 "Could not match STCLASS...\n") );
867 DEBUG_r( PerlIO_printf(Perl_debug_log,
868 "This position contradicts STCLASS...\n") );
869 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
871 /* Contradict one of substrings */
872 if (prog->anchored_substr || prog->anchored_utf8) {
873 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
874 DEBUG_r( what = "anchored" );
876 s = HOP3c(t, 1, strend);
877 if (s + start_shift + end_shift > strend) {
878 /* XXXX Should be taken into account earlier? */
879 DEBUG_r( PerlIO_printf(Perl_debug_log,
880 "Could not match STCLASS...\n") );
885 DEBUG_r( PerlIO_printf(Perl_debug_log,
886 "Looking for %s substr starting at offset %ld...\n",
887 what, (long)(s + start_shift - i_strpos)) );
890 /* Have both, check_string is floating */
891 if (t + start_shift >= check_at) /* Contradicts floating=check */
892 goto retry_floating_check;
893 /* Recheck anchored substring, but not floating... */
897 DEBUG_r( PerlIO_printf(Perl_debug_log,
898 "Looking for anchored substr starting at offset %ld...\n",
899 (long)(other_last - i_strpos)) );
900 goto do_other_anchored;
902 /* Another way we could have checked stclass at the
903 current position only: */
908 DEBUG_r( PerlIO_printf(Perl_debug_log,
909 "Looking for /%s^%s/m starting at offset %ld...\n",
910 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
913 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
915 /* Check is floating subtring. */
916 retry_floating_check:
917 t = check_at - start_shift;
918 DEBUG_r( what = "floating" );
919 goto hop_and_restart;
922 DEBUG_r(PerlIO_printf(Perl_debug_log,
923 "By STCLASS: moving %ld --> %ld\n",
924 (long)(t - i_strpos), (long)(s - i_strpos))
928 DEBUG_r(PerlIO_printf(Perl_debug_log,
929 "Does not contradict STCLASS...\n");
934 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
935 PL_colors[4], (check ? "Guessed" : "Giving up"),
936 PL_colors[5], (long)(s - i_strpos)) );
939 fail_finish: /* Substring not found */
940 if (prog->check_substr || prog->check_utf8) /* could be removed already */
941 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
943 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
944 PL_colors[4],PL_colors[5]));
948 /* We know what class REx starts with. Try to find this position... */
950 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
952 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
958 register I32 tmp = 1; /* Scratch variable? */
959 register bool do_utf8 = PL_reg_match_utf8;
961 /* We know what class it must start with. */
966 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
967 !UTF8_IS_INVARIANT((U8)s[0]) ?
968 reginclass(c, (U8*)s, 0, do_utf8) :
969 REGINCLASS(c, (U8*)s)) {
970 if (tmp && (norun || regtry(prog, s)))
984 if (REGINCLASS(c, (U8*)s) ||
985 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
986 /* The assignment of 2 is intentional:
987 * for the folded sharp s, the skip is 2. */
988 (skip = SHARP_S_SKIP))) {
989 if (tmp && (norun || regtry(prog, s)))
1001 while (s < strend) {
1002 if (tmp && (norun || regtry(prog, s)))
1013 STRLEN ulen1, ulen2;
1014 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
1015 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1017 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1018 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1020 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1021 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1022 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1023 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1034 c2 = PL_fold_locale[c1];
1036 e = HOP3c(strend, -(I32)ln, s);
1039 e = s; /* Due to minlen logic of intuit() */
1041 /* The idea in the EXACTF* cases is to first find the
1042 * first character of the EXACTF* node and then, if
1043 * necessary, case-insensitively compare the full
1044 * text of the node. The c1 and c2 are the first
1045 * characters (though in Unicode it gets a bit
1046 * more complicated because there are more cases
1047 * than just upper and lower: one needs to use
1048 * the so-called folding case for case-insensitive
1049 * matching (called "loose matching" in Unicode).
1050 * ibcmp_utf8() will do just that. */
1054 U8 tmpbuf [UTF8_MAXLEN+1];
1055 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1056 STRLEN len, foldlen;
1060 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1062 0 : UTF8_ALLOW_ANY);
1065 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1066 m, (char **)0, ln, (bool)UTF))
1067 && (norun || regtry(prog, s)) )
1070 uvchr_to_utf8(tmpbuf, c);
1071 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1073 && (f == c1 || f == c2)
1074 && (ln == foldlen ||
1075 !ibcmp_utf8((char *) foldbuf,
1076 (char **)0, foldlen, do_utf8,
1078 (char **)0, ln, (bool)UTF))
1079 && (norun || regtry(prog, s)) )
1087 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1089 0 : UTF8_ALLOW_ANY);
1091 /* Handle some of the three Greek sigmas cases.
1092 * Note that not all the possible combinations
1093 * are handled here: some of them are handled
1094 * by the standard folding rules, and some of
1095 * them (the character class or ANYOF cases)
1096 * are handled during compiletime in
1097 * regexec.c:S_regclass(). */
1098 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1099 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1100 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1102 if ( (c == c1 || c == c2)
1104 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1105 m, (char **)0, ln, (bool)UTF))
1106 && (norun || regtry(prog, s)) )
1109 uvchr_to_utf8(tmpbuf, c);
1110 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1112 && (f == c1 || f == c2)
1113 && (ln == foldlen ||
1114 !ibcmp_utf8((char *) foldbuf,
1115 (char **)0, foldlen, do_utf8,
1117 (char **)0, ln, (bool)UTF))
1118 && (norun || regtry(prog, s)) )
1129 && (ln == 1 || !(OP(c) == EXACTF
1131 : ibcmp_locale(s, m, ln)))
1132 && (norun || regtry(prog, s)) )
1138 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1139 && (ln == 1 || !(OP(c) == EXACTF
1141 : ibcmp_locale(s, m, ln)))
1142 && (norun || regtry(prog, s)) )
1149 PL_reg_flags |= RF_tainted;
1156 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1158 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1160 tmp = ((OP(c) == BOUND ?
1161 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1162 LOAD_UTF8_CHARCLASS(alnum,"a");
1163 while (s < strend) {
1164 if (tmp == !(OP(c) == BOUND ?
1165 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1166 isALNUM_LC_utf8((U8*)s)))
1169 if ((norun || regtry(prog, s)))
1176 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1177 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1178 while (s < strend) {
1180 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1182 if ((norun || regtry(prog, s)))
1188 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1192 PL_reg_flags |= RF_tainted;
1199 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1201 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1203 tmp = ((OP(c) == NBOUND ?
1204 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1205 LOAD_UTF8_CHARCLASS(alnum,"a");
1206 while (s < strend) {
1207 if (tmp == !(OP(c) == NBOUND ?
1208 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1209 isALNUM_LC_utf8((U8*)s)))
1211 else if ((norun || regtry(prog, s)))
1217 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1218 tmp = ((OP(c) == NBOUND ?
1219 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1220 while (s < strend) {
1222 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1224 else if ((norun || regtry(prog, s)))
1229 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1234 LOAD_UTF8_CHARCLASS(alnum,"a");
1235 while (s < strend) {
1236 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1237 if (tmp && (norun || regtry(prog, s)))
1248 while (s < strend) {
1250 if (tmp && (norun || regtry(prog, s)))
1262 PL_reg_flags |= RF_tainted;
1264 while (s < strend) {
1265 if (isALNUM_LC_utf8((U8*)s)) {
1266 if (tmp && (norun || regtry(prog, s)))
1277 while (s < strend) {
1278 if (isALNUM_LC(*s)) {
1279 if (tmp && (norun || regtry(prog, s)))
1292 LOAD_UTF8_CHARCLASS(alnum,"a");
1293 while (s < strend) {
1294 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1295 if (tmp && (norun || regtry(prog, s)))
1306 while (s < strend) {
1308 if (tmp && (norun || regtry(prog, s)))
1320 PL_reg_flags |= RF_tainted;
1322 while (s < strend) {
1323 if (!isALNUM_LC_utf8((U8*)s)) {
1324 if (tmp && (norun || regtry(prog, s)))
1335 while (s < strend) {
1336 if (!isALNUM_LC(*s)) {
1337 if (tmp && (norun || regtry(prog, s)))
1350 LOAD_UTF8_CHARCLASS(space," ");
1351 while (s < strend) {
1352 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1353 if (tmp && (norun || regtry(prog, s)))
1364 while (s < strend) {
1366 if (tmp && (norun || regtry(prog, s)))
1378 PL_reg_flags |= RF_tainted;
1380 while (s < strend) {
1381 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1382 if (tmp && (norun || regtry(prog, s)))
1393 while (s < strend) {
1394 if (isSPACE_LC(*s)) {
1395 if (tmp && (norun || regtry(prog, s)))
1408 LOAD_UTF8_CHARCLASS(space," ");
1409 while (s < strend) {
1410 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1411 if (tmp && (norun || regtry(prog, s)))
1422 while (s < strend) {
1424 if (tmp && (norun || regtry(prog, s)))
1436 PL_reg_flags |= RF_tainted;
1438 while (s < strend) {
1439 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1440 if (tmp && (norun || regtry(prog, s)))
1451 while (s < strend) {
1452 if (!isSPACE_LC(*s)) {
1453 if (tmp && (norun || regtry(prog, s)))
1466 LOAD_UTF8_CHARCLASS(digit,"0");
1467 while (s < strend) {
1468 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1469 if (tmp && (norun || regtry(prog, s)))
1480 while (s < strend) {
1482 if (tmp && (norun || regtry(prog, s)))
1494 PL_reg_flags |= RF_tainted;
1496 while (s < strend) {
1497 if (isDIGIT_LC_utf8((U8*)s)) {
1498 if (tmp && (norun || regtry(prog, s)))
1509 while (s < strend) {
1510 if (isDIGIT_LC(*s)) {
1511 if (tmp && (norun || regtry(prog, s)))
1524 LOAD_UTF8_CHARCLASS(digit,"0");
1525 while (s < strend) {
1526 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1527 if (tmp && (norun || regtry(prog, s)))
1538 while (s < strend) {
1540 if (tmp && (norun || regtry(prog, s)))
1552 PL_reg_flags |= RF_tainted;
1554 while (s < strend) {
1555 if (!isDIGIT_LC_utf8((U8*)s)) {
1556 if (tmp && (norun || regtry(prog, s)))
1567 while (s < strend) {
1568 if (!isDIGIT_LC(*s)) {
1569 if (tmp && (norun || regtry(prog, s)))
1581 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1590 - regexec_flags - match a regexp against a string
1593 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1594 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1595 /* strend: pointer to null at end of string */
1596 /* strbeg: real beginning of string */
1597 /* minend: end of match must be >=minend after stringarg. */
1598 /* data: May be used for some additional optimizations. */
1599 /* nosave: For optimizations. */
1602 register regnode *c;
1603 register char *startpos = stringarg;
1604 I32 minlen; /* must match at least this many chars */
1605 I32 dontbother = 0; /* how many characters not to try at end */
1606 /* I32 start_shift = 0; */ /* Offset of the start to find
1607 constant substr. */ /* CC */
1608 I32 end_shift = 0; /* Same for the end. */ /* CC */
1609 I32 scream_pos = -1; /* Internal iterator of scream. */
1611 SV* oreplsv = GvSV(PL_replgv);
1612 bool do_utf8 = DO_UTF8(sv);
1614 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1615 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1617 RX_MATCH_UTF8_set(prog,do_utf8);
1623 PL_regnarrate = DEBUG_r_TEST;
1626 /* Be paranoid... */
1627 if (prog == NULL || startpos == NULL) {
1628 Perl_croak(aTHX_ "NULL regexp parameter");
1632 minlen = prog->minlen;
1633 if (strend - startpos < minlen) {
1634 DEBUG_r(PerlIO_printf(Perl_debug_log,
1635 "String too short [regexec_flags]...\n"));
1639 /* Check validity of program. */
1640 if (UCHARAT(prog->program) != REG_MAGIC) {
1641 Perl_croak(aTHX_ "corrupted regexp program");
1645 PL_reg_eval_set = 0;
1648 if (prog->reganch & ROPT_UTF8)
1649 PL_reg_flags |= RF_utf8;
1651 /* Mark beginning of line for ^ and lookbehind. */
1652 PL_regbol = startpos;
1656 /* Mark end of line for $ (and such) */
1659 /* see how far we have to get to not match where we matched before */
1660 PL_regtill = startpos+minend;
1662 /* We start without call_cc context. */
1665 /* If there is a "must appear" string, look for it. */
1668 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1671 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1672 PL_reg_ganch = startpos;
1673 else if (sv && SvTYPE(sv) >= SVt_PVMG
1675 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1676 && mg->mg_len >= 0) {
1677 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1678 if (prog->reganch & ROPT_ANCH_GPOS) {
1679 if (s > PL_reg_ganch)
1684 else /* pos() not defined */
1685 PL_reg_ganch = strbeg;
1688 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1689 re_scream_pos_data d;
1691 d.scream_olds = &scream_olds;
1692 d.scream_pos = &scream_pos;
1693 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1695 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1696 goto phooey; /* not present */
1702 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1703 UNI_DISPLAY_REGEX) :
1705 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1706 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1707 UNI_DISPLAY_REGEX) : startpos;
1708 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1711 PerlIO_printf(Perl_debug_log,
1712 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1713 PL_colors[4],PL_colors[5],PL_colors[0],
1716 len0 > 60 ? "..." : "",
1718 (int)(len1 > 60 ? 60 : len1),
1720 (len1 > 60 ? "..." : "")
1724 /* Simplest case: anchored match need be tried only once. */
1725 /* [unless only anchor is BOL and multiline is set] */
1726 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1727 if (s == startpos && regtry(prog, startpos))
1729 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1730 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1735 dontbother = minlen - 1;
1736 end = HOP3c(strend, -dontbother, strbeg) - 1;
1737 /* for multiline we only have to try after newlines */
1738 if (prog->check_substr || prog->check_utf8) {
1742 if (regtry(prog, s))
1747 if (prog->reganch & RE_USE_INTUIT) {
1748 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1759 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1760 if (regtry(prog, s))
1767 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1768 if (regtry(prog, PL_reg_ganch))
1773 /* Messy cases: unanchored match. */
1774 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1775 /* we have /x+whatever/ */
1776 /* it must be a one character string (XXXX Except UTF?) */
1781 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1782 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1783 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1786 while (s < strend) {
1788 DEBUG_r( did_match = 1 );
1789 if (regtry(prog, s)) goto got_it;
1791 while (s < strend && *s == ch)
1798 while (s < strend) {
1800 DEBUG_r( did_match = 1 );
1801 if (regtry(prog, s)) goto got_it;
1803 while (s < strend && *s == ch)
1809 DEBUG_r(if (!did_match)
1810 PerlIO_printf(Perl_debug_log,
1811 "Did not find anchored character...\n")
1815 else if (prog->anchored_substr != Nullsv
1816 || prog->anchored_utf8 != Nullsv
1817 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1818 && prog->float_max_offset < strend - s)) {
1823 char *last1; /* Last position checked before */
1827 if (prog->anchored_substr || prog->anchored_utf8) {
1828 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1829 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1830 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1831 back_max = back_min = prog->anchored_offset;
1833 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1834 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1835 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1836 back_max = prog->float_max_offset;
1837 back_min = prog->float_min_offset;
1839 if (must == &PL_sv_undef)
1840 /* could not downgrade utf8 check substring, so must fail */
1843 last = HOP3c(strend, /* Cannot start after this */
1844 -(I32)(CHR_SVLEN(must)
1845 - (SvTAIL(must) != 0) + back_min), strbeg);
1848 last1 = HOPc(s, -1);
1850 last1 = s - 1; /* bogus */
1852 /* XXXX check_substr already used to find `s', can optimize if
1853 check_substr==must. */
1855 dontbother = end_shift;
1856 strend = HOPc(strend, -dontbother);
1857 while ( (s <= last) &&
1858 ((flags & REXEC_SCREAM)
1859 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1860 end_shift, &scream_pos, 0))
1861 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1862 (unsigned char*)strend, must,
1863 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1864 /* we may be pointing at the wrong string */
1865 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1866 s = strbeg + (s - SvPVX(sv));
1867 DEBUG_r( did_match = 1 );
1868 if (HOPc(s, -back_max) > last1) {
1869 last1 = HOPc(s, -back_min);
1870 s = HOPc(s, -back_max);
1873 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1875 last1 = HOPc(s, -back_min);
1879 while (s <= last1) {
1880 if (regtry(prog, s))
1886 while (s <= last1) {
1887 if (regtry(prog, s))
1893 DEBUG_r(if (!did_match)
1894 PerlIO_printf(Perl_debug_log,
1895 "Did not find %s substr `%s%.*s%s'%s...\n",
1896 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1897 ? "anchored" : "floating"),
1899 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1901 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1905 else if ((c = prog->regstclass)) {
1907 I32 op = (U8)OP(prog->regstclass);
1908 /* don't bother with what can't match */
1909 if (PL_regkind[op] != EXACT && op != CANY)
1910 strend = HOPc(strend, -(minlen - 1));
1913 SV *prop = sv_newmortal();
1921 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1922 UNI_DISPLAY_REGEX) :
1924 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1926 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1927 len1 = UTF ? SvCUR(dsv1) : strend - s;
1928 PerlIO_printf(Perl_debug_log,
1929 "Matching stclass `%*.*s' against `%*.*s'\n",
1933 if (find_byclass(prog, c, s, strend, startpos, 0))
1935 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1939 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1944 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1945 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1946 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1948 if (flags & REXEC_SCREAM) {
1949 last = screaminstr(sv, float_real, s - strbeg,
1950 end_shift, &scream_pos, 1); /* last one */
1952 last = scream_olds; /* Only one occurrence. */
1953 /* we may be pointing at the wrong string */
1954 else if (RX_MATCH_COPIED(prog))
1955 s = strbeg + (s - SvPVX(sv));
1959 char *little = SvPV(float_real, len);
1961 if (SvTAIL(float_real)) {
1962 if (memEQ(strend - len + 1, little, len - 1))
1963 last = strend - len + 1;
1964 else if (!PL_multiline)
1965 last = memEQ(strend - len, little, len)
1966 ? strend - len : Nullch;
1972 last = rninstr(s, strend, little, little + len);
1974 last = strend; /* matching `$' */
1978 DEBUG_r(PerlIO_printf(Perl_debug_log,
1979 "%sCan't trim the tail, match fails (should not happen)%s\n",
1980 PL_colors[4],PL_colors[5]));
1981 goto phooey; /* Should not happen! */
1983 dontbother = strend - last + prog->float_min_offset;
1985 if (minlen && (dontbother < minlen))
1986 dontbother = minlen - 1;
1987 strend -= dontbother; /* this one's always in bytes! */
1988 /* We don't know much -- general case. */
1991 if (regtry(prog, s))
2000 if (regtry(prog, s))
2002 } while (s++ < strend);
2010 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2012 if (PL_reg_eval_set) {
2013 /* Preserve the current value of $^R */
2014 if (oreplsv != GvSV(PL_replgv))
2015 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2016 restored, the value remains
2018 restore_pos(aTHX_ 0);
2021 /* make sure $`, $&, $', and $digit will work later */
2022 if ( !(flags & REXEC_NOT_FIRST) ) {
2023 RX_MATCH_COPY_FREE(prog);
2024 if (flags & REXEC_COPY_STR) {
2025 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2026 #ifdef PERL_COPY_ON_WRITE
2028 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2030 PerlIO_printf(Perl_debug_log,
2031 "Copy on write: regexp capture, type %d\n",
2034 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2035 prog->subbeg = SvPVX(prog->saved_copy);
2036 assert (SvPOKp(prog->saved_copy));
2040 RX_MATCH_COPIED_on(prog);
2041 s = savepvn(strbeg, i);
2047 prog->subbeg = strbeg;
2048 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2055 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2056 PL_colors[4],PL_colors[5]));
2057 if (PL_reg_eval_set)
2058 restore_pos(aTHX_ 0);
2063 - regtry - try match at specific point
2065 STATIC I32 /* 0 failure, 1 success */
2066 S_regtry(pTHX_ regexp *prog, char *startpos)
2074 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2076 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2079 PL_reg_eval_set = RS_init;
2081 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2082 (IV)(PL_stack_sp - PL_stack_base));
2084 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2085 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2086 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2088 /* Apparently this is not needed, judging by wantarray. */
2089 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2090 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2093 /* Make $_ available to executed code. */
2094 if (PL_reg_sv != DEFSV) {
2095 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2100 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2101 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2102 /* prepare for quick setting of pos */
2103 sv_magic(PL_reg_sv, (SV*)0,
2104 PERL_MAGIC_regex_global, Nullch, 0);
2105 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2109 PL_reg_oldpos = mg->mg_len;
2110 SAVEDESTRUCTOR_X(restore_pos, 0);
2112 if (!PL_reg_curpm) {
2113 Newz(22,PL_reg_curpm, 1, PMOP);
2116 SV* repointer = newSViv(0);
2117 /* so we know which PL_regex_padav element is PL_reg_curpm */
2118 SvFLAGS(repointer) |= SVf_BREAK;
2119 av_push(PL_regex_padav,repointer);
2120 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2121 PL_regex_pad = AvARRAY(PL_regex_padav);
2125 PM_SETRE(PL_reg_curpm, prog);
2126 PL_reg_oldcurpm = PL_curpm;
2127 PL_curpm = PL_reg_curpm;
2128 if (RX_MATCH_COPIED(prog)) {
2129 /* Here is a serious problem: we cannot rewrite subbeg,
2130 since it may be needed if this match fails. Thus
2131 $` inside (?{}) could fail... */
2132 PL_reg_oldsaved = prog->subbeg;
2133 PL_reg_oldsavedlen = prog->sublen;
2134 #ifdef PERL_COPY_ON_WRITE
2135 PL_nrs = prog->saved_copy;
2137 RX_MATCH_COPIED_off(prog);
2140 PL_reg_oldsaved = Nullch;
2141 prog->subbeg = PL_bostr;
2142 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2144 prog->startp[0] = startpos - PL_bostr;
2145 PL_reginput = startpos;
2146 PL_regstartp = prog->startp;
2147 PL_regendp = prog->endp;
2148 PL_reglastparen = &prog->lastparen;
2149 PL_reglastcloseparen = &prog->lastcloseparen;
2150 prog->lastparen = 0;
2151 prog->lastcloseparen = 0;
2153 DEBUG_r(PL_reg_starttry = startpos);
2154 if (PL_reg_start_tmpl <= prog->nparens) {
2155 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2156 if(PL_reg_start_tmp)
2157 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2159 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2162 /* XXXX What this code is doing here?!!! There should be no need
2163 to do this again and again, PL_reglastparen should take care of
2166 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2167 * Actually, the code in regcppop() (which Ilya may be meaning by
2168 * PL_reglastparen), is not needed at all by the test suite
2169 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2170 * enough, for building DynaLoader, or otherwise this
2171 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2172 * will happen. Meanwhile, this code *is* needed for the
2173 * above-mentioned test suite tests to succeed. The common theme
2174 * on those tests seems to be returning null fields from matches.
2179 if (prog->nparens) {
2180 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2187 if (regmatch(prog->program + 1)) {
2188 prog->endp[0] = PL_reginput - PL_bostr;
2191 REGCP_UNWIND(lastcp);
2195 #define RE_UNWIND_BRANCH 1
2196 #define RE_UNWIND_BRANCHJ 2
2200 typedef struct { /* XX: makes sense to enlarge it... */
2204 } re_unwind_generic_t;
2217 } re_unwind_branch_t;
2219 typedef union re_unwind_t {
2221 re_unwind_generic_t generic;
2222 re_unwind_branch_t branch;
2225 #define sayYES goto yes
2226 #define sayNO goto no
2227 #define sayNO_ANYOF goto no_anyof
2228 #define sayYES_FINAL goto yes_final
2229 #define sayYES_LOUD goto yes_loud
2230 #define sayNO_FINAL goto no_final
2231 #define sayNO_SILENT goto do_no
2232 #define saySAME(x) if (x) goto yes; else goto no
2234 #define REPORT_CODE_OFF 24
2237 - regmatch - main matching routine
2239 * Conceptually the strategy is simple: check to see whether the current
2240 * node matches, call self recursively to see whether the rest matches,
2241 * and then act accordingly. In practice we make some effort to avoid
2242 * recursion, in particular by going through "ordinary" nodes (that don't
2243 * need to know whether the rest of the match failed) by a loop instead of
2246 /* [lwall] I've hoisted the register declarations to the outer block in order to
2247 * maybe save a little bit of pushing and popping on the stack. It also takes
2248 * advantage of machines that use a register save mask on subroutine entry.
2250 STATIC I32 /* 0 failure, 1 success */
2251 S_regmatch(pTHX_ regnode *prog)
2253 register regnode *scan; /* Current node. */
2254 regnode *next; /* Next node. */
2255 regnode *inner; /* Next node in internal branch. */
2256 register I32 nextchr; /* renamed nextchr - nextchar colides with
2257 function of same name */
2258 register I32 n; /* no or next */
2259 register I32 ln = 0; /* len or last */
2260 register char *s = Nullch; /* operand or save */
2261 register char *locinput = PL_reginput;
2262 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2263 int minmod = 0, sw = 0, logical = 0;
2266 I32 firstcp = PL_savestack_ix;
2268 register bool do_utf8 = PL_reg_match_utf8;
2270 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2271 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2272 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2279 /* Note that nextchr is a byte even in UTF */
2280 nextchr = UCHARAT(locinput);
2282 while (scan != NULL) {
2285 SV *prop = sv_newmortal();
2286 int docolor = *PL_colors[0];
2287 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2288 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2289 /* The part of the string before starttry has one color
2290 (pref0_len chars), between starttry and current
2291 position another one (pref_len - pref0_len chars),
2292 after the current position the third one.
2293 We assume that pref0_len <= pref_len, otherwise we
2294 decrease pref0_len. */
2295 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2296 ? (5 + taill) - l : locinput - PL_bostr;
2299 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2301 pref0_len = pref_len - (locinput - PL_reg_starttry);
2302 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2303 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2304 ? (5 + taill) - pref_len : PL_regeol - locinput);
2305 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2309 if (pref0_len > pref_len)
2310 pref0_len = pref_len;
2311 regprop(prop, scan);
2314 do_utf8 && OP(scan) != CANY ?
2315 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2316 pref0_len, 60, UNI_DISPLAY_REGEX) :
2317 locinput - pref_len;
2318 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2319 char *s1 = do_utf8 && OP(scan) != CANY ?
2320 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2321 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2322 locinput - pref_len + pref0_len;
2323 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2324 char *s2 = do_utf8 && OP(scan) != CANY ?
2325 pv_uni_display(dsv2, (U8*)locinput,
2326 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2328 int len2 = do_utf8 ? strlen(s2) : l;
2329 PerlIO_printf(Perl_debug_log,
2330 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2331 (IV)(locinput - PL_bostr),
2338 (docolor ? "" : "> <"),
2342 15 - l - pref_len + 1,
2344 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2349 next = scan + NEXT_OFF(scan);
2355 if (locinput == PL_bostr || (PL_multiline &&
2356 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2358 /* regtill = regbol; */
2363 if (locinput == PL_bostr ||
2364 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2370 if (locinput == PL_bostr)
2374 if (locinput == PL_reg_ganch)
2384 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2389 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2391 if (PL_regeol - locinput > 1)
2395 if (PL_regeol != locinput)
2399 if (!nextchr && locinput >= PL_regeol)
2402 locinput += PL_utf8skip[nextchr];
2403 if (locinput > PL_regeol)
2405 nextchr = UCHARAT(locinput);
2408 nextchr = UCHARAT(++locinput);
2411 if (!nextchr && locinput >= PL_regeol)
2413 nextchr = UCHARAT(++locinput);
2416 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2419 locinput += PL_utf8skip[nextchr];
2420 if (locinput > PL_regeol)
2422 nextchr = UCHARAT(locinput);
2425 nextchr = UCHARAT(++locinput);
2430 if (do_utf8 != UTF) {
2431 /* The target and the pattern have differing utf8ness. */
2437 /* The target is utf8, the pattern is not utf8. */
2441 if (NATIVE_TO_UNI(*(U8*)s) !=
2442 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2444 0 : UTF8_ALLOW_ANY))
2451 /* The target is not utf8, the pattern is utf8. */
2455 if (NATIVE_TO_UNI(*((U8*)l)) !=
2456 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2458 0 : UTF8_ALLOW_ANY))
2465 nextchr = UCHARAT(locinput);
2468 /* The target and the pattern have the same utf8ness. */
2469 /* Inline the first character, for speed. */
2470 if (UCHARAT(s) != nextchr)
2472 if (PL_regeol - locinput < ln)
2474 if (ln > 1 && memNE(s, locinput, ln))
2477 nextchr = UCHARAT(locinput);
2480 PL_reg_flags |= RF_tainted;
2486 if (do_utf8 || UTF) {
2487 /* Either target or the pattern are utf8. */
2489 char *e = PL_regeol;
2491 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2492 l, &e, 0, do_utf8)) {
2493 /* One more case for the sharp s:
2494 * pack("U0U*", 0xDF) =~ /ss/i,
2495 * the 0xC3 0x9F are the UTF-8
2496 * byte sequence for the U+00DF. */
2498 toLOWER(s[0]) == 's' &&
2500 toLOWER(s[1]) == 's' &&
2507 nextchr = UCHARAT(locinput);
2511 /* Neither the target and the pattern are utf8. */
2513 /* Inline the first character, for speed. */
2514 if (UCHARAT(s) != nextchr &&
2515 UCHARAT(s) != ((OP(scan) == EXACTF)
2516 ? PL_fold : PL_fold_locale)[nextchr])
2518 if (PL_regeol - locinput < ln)
2520 if (ln > 1 && (OP(scan) == EXACTF
2521 ? ibcmp(s, locinput, ln)
2522 : ibcmp_locale(s, locinput, ln)))
2525 nextchr = UCHARAT(locinput);
2529 STRLEN inclasslen = PL_regeol - locinput;
2531 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2533 if (locinput >= PL_regeol)
2535 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2536 nextchr = UCHARAT(locinput);
2541 nextchr = UCHARAT(locinput);
2542 if (!REGINCLASS(scan, (U8*)locinput))
2544 if (!nextchr && locinput >= PL_regeol)
2546 nextchr = UCHARAT(++locinput);
2550 /* If we might have the case of the German sharp s
2551 * in a casefolding Unicode character class. */
2553 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2554 locinput += SHARP_S_SKIP;
2555 nextchr = UCHARAT(locinput);
2561 PL_reg_flags |= RF_tainted;
2567 LOAD_UTF8_CHARCLASS(alnum,"a");
2568 if (!(OP(scan) == ALNUM
2569 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2570 : isALNUM_LC_utf8((U8*)locinput)))
2574 locinput += PL_utf8skip[nextchr];
2575 nextchr = UCHARAT(locinput);
2578 if (!(OP(scan) == ALNUM
2579 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2581 nextchr = UCHARAT(++locinput);
2584 PL_reg_flags |= RF_tainted;
2587 if (!nextchr && locinput >= PL_regeol)
2590 LOAD_UTF8_CHARCLASS(alnum,"a");
2591 if (OP(scan) == NALNUM
2592 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2593 : isALNUM_LC_utf8((U8*)locinput))
2597 locinput += PL_utf8skip[nextchr];
2598 nextchr = UCHARAT(locinput);
2601 if (OP(scan) == NALNUM
2602 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2604 nextchr = UCHARAT(++locinput);
2608 PL_reg_flags |= RF_tainted;
2612 /* was last char in word? */
2614 if (locinput == PL_bostr)
2617 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2619 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2621 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2622 ln = isALNUM_uni(ln);
2623 LOAD_UTF8_CHARCLASS(alnum,"a");
2624 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2627 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2628 n = isALNUM_LC_utf8((U8*)locinput);
2632 ln = (locinput != PL_bostr) ?
2633 UCHARAT(locinput - 1) : '\n';
2634 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2636 n = isALNUM(nextchr);
2639 ln = isALNUM_LC(ln);
2640 n = isALNUM_LC(nextchr);
2643 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2644 OP(scan) == BOUNDL))
2648 PL_reg_flags |= RF_tainted;
2654 if (UTF8_IS_CONTINUED(nextchr)) {
2655 LOAD_UTF8_CHARCLASS(space," ");
2656 if (!(OP(scan) == SPACE
2657 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2658 : isSPACE_LC_utf8((U8*)locinput)))
2662 locinput += PL_utf8skip[nextchr];
2663 nextchr = UCHARAT(locinput);
2666 if (!(OP(scan) == SPACE
2667 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2669 nextchr = UCHARAT(++locinput);
2672 if (!(OP(scan) == SPACE
2673 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2675 nextchr = UCHARAT(++locinput);
2679 PL_reg_flags |= RF_tainted;
2682 if (!nextchr && locinput >= PL_regeol)
2685 LOAD_UTF8_CHARCLASS(space," ");
2686 if (OP(scan) == NSPACE
2687 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2688 : isSPACE_LC_utf8((U8*)locinput))
2692 locinput += PL_utf8skip[nextchr];
2693 nextchr = UCHARAT(locinput);
2696 if (OP(scan) == NSPACE
2697 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2699 nextchr = UCHARAT(++locinput);
2702 PL_reg_flags |= RF_tainted;
2708 LOAD_UTF8_CHARCLASS(digit,"0");
2709 if (!(OP(scan) == DIGIT
2710 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2711 : isDIGIT_LC_utf8((U8*)locinput)))
2715 locinput += PL_utf8skip[nextchr];
2716 nextchr = UCHARAT(locinput);
2719 if (!(OP(scan) == DIGIT
2720 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2722 nextchr = UCHARAT(++locinput);
2725 PL_reg_flags |= RF_tainted;
2728 if (!nextchr && locinput >= PL_regeol)
2731 LOAD_UTF8_CHARCLASS(digit,"0");
2732 if (OP(scan) == NDIGIT
2733 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2734 : isDIGIT_LC_utf8((U8*)locinput))
2738 locinput += PL_utf8skip[nextchr];
2739 nextchr = UCHARAT(locinput);
2742 if (OP(scan) == NDIGIT
2743 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2745 nextchr = UCHARAT(++locinput);
2748 if (locinput >= PL_regeol)
2751 LOAD_UTF8_CHARCLASS(mark,"~");
2752 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2754 locinput += PL_utf8skip[nextchr];
2755 while (locinput < PL_regeol &&
2756 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2757 locinput += UTF8SKIP(locinput);
2758 if (locinput > PL_regeol)
2763 nextchr = UCHARAT(locinput);
2766 PL_reg_flags |= RF_tainted;
2770 n = ARG(scan); /* which paren pair */
2771 ln = PL_regstartp[n];
2772 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2773 if ((I32)*PL_reglastparen < n || ln == -1)
2774 sayNO; /* Do not match unless seen CLOSEn. */
2775 if (ln == PL_regendp[n])
2779 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2781 char *e = PL_bostr + PL_regendp[n];
2783 * Note that we can't do the "other character" lookup trick as
2784 * in the 8-bit case (no pun intended) because in Unicode we
2785 * have to map both upper and title case to lower case.
2787 if (OP(scan) == REFF) {
2788 STRLEN ulen1, ulen2;
2789 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2790 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2794 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2795 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2796 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2803 nextchr = UCHARAT(locinput);
2807 /* Inline the first character, for speed. */
2808 if (UCHARAT(s) != nextchr &&
2810 (UCHARAT(s) != ((OP(scan) == REFF
2811 ? PL_fold : PL_fold_locale)[nextchr]))))
2813 ln = PL_regendp[n] - ln;
2814 if (locinput + ln > PL_regeol)
2816 if (ln > 1 && (OP(scan) == REF
2817 ? memNE(s, locinput, ln)
2819 ? ibcmp(s, locinput, ln)
2820 : ibcmp_locale(s, locinput, ln))))
2823 nextchr = UCHARAT(locinput);
2834 OP_4tree *oop = PL_op;
2835 COP *ocurcop = PL_curcop;
2838 struct regexp *oreg = PL_reg_re;
2841 PL_op = (OP_4tree*)PL_regdata->data[n];
2842 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2843 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2844 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2848 CALLRUNOPS(aTHX); /* Scalar context. */
2851 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2859 PAD_RESTORE_LOCAL(old_comppad);
2860 PL_curcop = ocurcop;
2862 if (logical == 2) { /* Postponed subexpression. */
2864 MAGIC *mg = Null(MAGIC*);
2866 CHECKPOINT cp, lastcp;
2870 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
2871 mg = mg_find(sv, PERL_MAGIC_qr);
2872 else if (SvSMAGICAL(ret)) {
2873 if (SvGMAGICAL(ret))
2874 sv_unmagic(ret, PERL_MAGIC_qr);
2876 mg = mg_find(ret, PERL_MAGIC_qr);
2880 re = (regexp *)mg->mg_obj;
2881 (void)ReREFCNT_inc(re);
2885 char *t = SvPV(ret, len);
2887 char *oprecomp = PL_regprecomp;
2888 I32 osize = PL_regsize;
2889 I32 onpar = PL_regnpar;
2892 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2893 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2895 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
2897 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2899 PL_regprecomp = oprecomp;
2904 PerlIO_printf(Perl_debug_log,
2905 "Entering embedded `%s%.60s%s%s'\n",
2909 (strlen(re->precomp) > 60 ? "..." : ""))
2912 state.prev = PL_reg_call_cc;
2913 state.cc = PL_regcc;
2914 state.re = PL_reg_re;
2918 cp = regcppush(0); /* Save *all* the positions. */
2921 state.ss = PL_savestack_ix;
2922 *PL_reglastparen = 0;
2923 *PL_reglastcloseparen = 0;
2924 PL_reg_call_cc = &state;
2925 PL_reginput = locinput;
2926 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2927 ((re->reganch & ROPT_UTF8) != 0);
2928 if (toggleutf) PL_reg_flags ^= RF_utf8;
2930 /* XXXX This is too dramatic a measure... */
2933 if (regmatch(re->program + 1)) {
2934 /* Even though we succeeded, we need to restore
2935 global variables, since we may be wrapped inside
2936 SUSPEND, thus the match may be not finished yet. */
2938 /* XXXX Do this only if SUSPENDed? */
2939 PL_reg_call_cc = state.prev;
2940 PL_regcc = state.cc;
2941 PL_reg_re = state.re;
2942 cache_re(PL_reg_re);
2943 if (toggleutf) PL_reg_flags ^= RF_utf8;
2945 /* XXXX This is too dramatic a measure... */
2948 /* These are needed even if not SUSPEND. */
2954 REGCP_UNWIND(lastcp);
2956 PL_reg_call_cc = state.prev;
2957 PL_regcc = state.cc;
2958 PL_reg_re = state.re;
2959 cache_re(PL_reg_re);
2960 if (toggleutf) PL_reg_flags ^= RF_utf8;
2962 /* XXXX This is too dramatic a measure... */
2972 sv_setsv(save_scalar(PL_replgv), ret);
2978 n = ARG(scan); /* which paren pair */
2979 PL_reg_start_tmp[n] = locinput;
2984 n = ARG(scan); /* which paren pair */
2985 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2986 PL_regendp[n] = locinput - PL_bostr;
2987 if (n > (I32)*PL_reglastparen)
2988 *PL_reglastparen = n;
2989 *PL_reglastcloseparen = n;
2992 n = ARG(scan); /* which paren pair */
2993 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2996 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2998 next = NEXTOPER(NEXTOPER(scan));
3000 next = scan + ARG(scan);
3001 if (OP(next) == IFTHEN) /* Fake one. */
3002 next = NEXTOPER(NEXTOPER(next));
3006 logical = scan->flags;
3008 /*******************************************************************
3009 PL_regcc contains infoblock about the innermost (...)* loop, and
3010 a pointer to the next outer infoblock.
3012 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3014 1) After matching X, regnode for CURLYX is processed;
3016 2) This regnode creates infoblock on the stack, and calls
3017 regmatch() recursively with the starting point at WHILEM node;
3019 3) Each hit of WHILEM node tries to match A and Z (in the order
3020 depending on the current iteration, min/max of {min,max} and
3021 greediness). The information about where are nodes for "A"
3022 and "Z" is read from the infoblock, as is info on how many times "A"
3023 was already matched, and greediness.
3025 4) After A matches, the same WHILEM node is hit again.
3027 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3028 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3029 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3030 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3031 of the external loop.
3033 Currently present infoblocks form a tree with a stem formed by PL_curcc
3034 and whatever it mentions via ->next, and additional attached trees
3035 corresponding to temporarily unset infoblocks as in "5" above.
3037 In the following picture infoblocks for outer loop of
3038 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3039 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3040 infoblocks are drawn below the "reset" infoblock.
3042 In fact in the picture below we do not show failed matches for Z and T
3043 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3044 more obvious *why* one needs to *temporary* unset infoblocks.]
3046 Matched REx position InfoBlocks Comment
3050 Y A)*?Z)*?T x <- O <- I
3051 YA )*?Z)*?T x <- O <- I
3052 YA A)*?Z)*?T x <- O <- I
3053 YAA )*?Z)*?T x <- O <- I
3054 YAA Z)*?T x <- O # Temporary unset I
3057 YAAZ Y(A)*?Z)*?T x <- O
3060 YAAZY (A)*?Z)*?T x <- O
3063 YAAZY A)*?Z)*?T x <- O <- I
3066 YAAZYA )*?Z)*?T x <- O <- I
3069 YAAZYA Z)*?T x <- O # Temporary unset I
3075 YAAZYAZ T x # Temporary unset O
3082 *******************************************************************/
3085 CHECKPOINT cp = PL_savestack_ix;
3086 /* No need to save/restore up to this paren */
3087 I32 parenfloor = scan->flags;
3089 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3091 cc.oldcc = PL_regcc;
3093 /* XXXX Probably it is better to teach regpush to support
3094 parenfloor > PL_regsize... */
3095 if (parenfloor > (I32)*PL_reglastparen)
3096 parenfloor = *PL_reglastparen; /* Pessimization... */
3097 cc.parenfloor = parenfloor;
3099 cc.min = ARG1(scan);
3100 cc.max = ARG2(scan);
3101 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3105 PL_reginput = locinput;
3106 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3108 PL_regcc = cc.oldcc;
3114 * This is really hard to understand, because after we match
3115 * what we're trying to match, we must make sure the rest of
3116 * the REx is going to match for sure, and to do that we have
3117 * to go back UP the parse tree by recursing ever deeper. And
3118 * if it fails, we have to reset our parent's current state
3119 * that we can try again after backing off.
3122 CHECKPOINT cp, lastcp;
3123 CURCUR* cc = PL_regcc;
3124 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3126 n = cc->cur + 1; /* how many we know we matched */
3127 PL_reginput = locinput;
3130 PerlIO_printf(Perl_debug_log,
3131 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3132 REPORT_CODE_OFF+PL_regindent*2, "",
3133 (long)n, (long)cc->min,
3134 (long)cc->max, PTR2UV(cc))
3137 /* If degenerate scan matches "", assume scan done. */
3139 if (locinput == cc->lastloc && n >= cc->min) {
3140 PL_regcc = cc->oldcc;
3144 PerlIO_printf(Perl_debug_log,
3145 "%*s empty match detected, try continuation...\n",
3146 REPORT_CODE_OFF+PL_regindent*2, "")
3148 if (regmatch(cc->next))
3156 /* First just match a string of min scans. */
3160 cc->lastloc = locinput;
3161 if (regmatch(cc->scan))
3164 cc->lastloc = lastloc;
3169 /* Check whether we already were at this position.
3170 Postpone detection until we know the match is not
3171 *that* much linear. */
3172 if (!PL_reg_maxiter) {
3173 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3174 PL_reg_leftiter = PL_reg_maxiter;
3176 if (PL_reg_leftiter-- == 0) {
3177 I32 size = (PL_reg_maxiter + 7)/8;
3178 if (PL_reg_poscache) {
3179 if ((I32)PL_reg_poscache_size < size) {
3180 Renew(PL_reg_poscache, size, char);
3181 PL_reg_poscache_size = size;
3183 Zero(PL_reg_poscache, size, char);
3186 PL_reg_poscache_size = size;
3187 Newz(29, PL_reg_poscache, size, char);
3190 PerlIO_printf(Perl_debug_log,
3191 "%sDetected a super-linear match, switching on caching%s...\n",
3192 PL_colors[4], PL_colors[5])
3195 if (PL_reg_leftiter < 0) {
3196 I32 o = locinput - PL_bostr, b;
3198 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3201 if (PL_reg_poscache[o] & (1<<b)) {
3203 PerlIO_printf(Perl_debug_log,
3204 "%*s already tried at this position...\n",
3205 REPORT_CODE_OFF+PL_regindent*2, "")
3209 PL_reg_poscache[o] |= (1<<b);
3213 /* Prefer next over scan for minimal matching. */
3216 PL_regcc = cc->oldcc;
3219 cp = regcppush(cc->parenfloor);
3221 if (regmatch(cc->next)) {
3223 sayYES; /* All done. */
3225 REGCP_UNWIND(lastcp);
3231 if (n >= cc->max) { /* Maximum greed exceeded? */
3232 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3233 && !(PL_reg_flags & RF_warned)) {
3234 PL_reg_flags |= RF_warned;
3235 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3236 "Complex regular subexpression recursion",
3243 PerlIO_printf(Perl_debug_log,
3244 "%*s trying longer...\n",
3245 REPORT_CODE_OFF+PL_regindent*2, "")
3247 /* Try scanning more and see if it helps. */
3248 PL_reginput = locinput;
3250 cc->lastloc = locinput;
3251 cp = regcppush(cc->parenfloor);
3253 if (regmatch(cc->scan)) {
3257 REGCP_UNWIND(lastcp);
3260 cc->lastloc = lastloc;
3264 /* Prefer scan over next for maximal matching. */
3266 if (n < cc->max) { /* More greed allowed? */
3267 cp = regcppush(cc->parenfloor);
3269 cc->lastloc = locinput;
3271 if (regmatch(cc->scan)) {
3275 REGCP_UNWIND(lastcp);
3276 regcppop(); /* Restore some previous $<digit>s? */
3277 PL_reginput = locinput;
3279 PerlIO_printf(Perl_debug_log,
3280 "%*s failed, try continuation...\n",
3281 REPORT_CODE_OFF+PL_regindent*2, "")
3284 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3285 && !(PL_reg_flags & RF_warned)) {
3286 PL_reg_flags |= RF_warned;
3287 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3288 "Complex regular subexpression recursion",
3292 /* Failed deeper matches of scan, so see if this one works. */
3293 PL_regcc = cc->oldcc;
3296 if (regmatch(cc->next))
3302 cc->lastloc = lastloc;
3307 next = scan + ARG(scan);
3310 inner = NEXTOPER(NEXTOPER(scan));
3313 inner = NEXTOPER(scan);
3317 if (OP(next) != c1) /* No choice. */
3318 next = inner; /* Avoid recursion. */
3320 I32 lastparen = *PL_reglastparen;
3322 re_unwind_branch_t *uw;
3324 /* Put unwinding data on stack */
3325 unwind1 = SSNEWt(1,re_unwind_branch_t);
3326 uw = SSPTRt(unwind1,re_unwind_branch_t);
3329 uw->type = ((c1 == BRANCH)
3331 : RE_UNWIND_BRANCHJ);
3332 uw->lastparen = lastparen;
3334 uw->locinput = locinput;
3335 uw->nextchr = nextchr;
3337 uw->regindent = ++PL_regindent;
3340 REGCP_SET(uw->lastcp);
3342 /* Now go into the first branch */
3355 /* We suppose that the next guy does not need
3356 backtracking: in particular, it is of constant length,
3357 and has no parenths to influence future backrefs. */
3358 ln = ARG1(scan); /* min to match */
3359 n = ARG2(scan); /* max to match */
3360 paren = scan->flags;
3362 if (paren > PL_regsize)
3364 if (paren > (I32)*PL_reglastparen)
3365 *PL_reglastparen = paren;
3367 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3369 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3370 PL_reginput = locinput;
3373 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3375 /* if we matched something zero-length we don't need to
3376 backtrack - capturing parens are already defined, so
3377 the caveat in the maximal case doesn't apply
3379 XXXX if ln == 0, we can redo this check first time
3380 through the following loop
3383 n = ln; /* don't backtrack */
3384 locinput = PL_reginput;
3385 if (HAS_TEXT(next) || JUMPABLE(next)) {
3386 regnode *text_node = next;
3388 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3390 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3392 if (PL_regkind[(U8)OP(text_node)] == REF) {
3394 n = ARG(text_node); /* which paren pair */
3395 ln = PL_regstartp[n];
3396 /* assume yes if we haven't seen CLOSEn */
3398 (I32)*PL_reglastparen < n ||
3405 c1 = *(PL_bostr + ln);
3407 else { c1 = (U8)*STRING(text_node); }
3408 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3410 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3411 c2 = PL_fold_locale[c1];
3420 /* This may be improved if l == 0. */
3421 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3422 /* If it could work, try it. */
3424 UCHARAT(PL_reginput) == c1 ||
3425 UCHARAT(PL_reginput) == c2)
3429 PL_regstartp[paren] =
3430 HOPc(PL_reginput, -l) - PL_bostr;
3431 PL_regendp[paren] = PL_reginput - PL_bostr;
3434 PL_regendp[paren] = -1;
3438 REGCP_UNWIND(lastcp);
3440 /* Couldn't or didn't -- move forward. */
3441 PL_reginput = locinput;
3442 if (regrepeat_hard(scan, 1, &l)) {
3444 locinput = PL_reginput;
3451 n = regrepeat_hard(scan, n, &l);
3452 /* if we matched something zero-length we don't need to
3453 backtrack, unless the minimum count is zero and we
3454 are capturing the result - in that case the capture
3455 being defined or not may affect later execution
3457 if (n != 0 && l == 0 && !(paren && ln == 0))
3458 ln = n; /* don't backtrack */
3459 locinput = PL_reginput;
3461 PerlIO_printf(Perl_debug_log,
3462 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3463 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3467 if (HAS_TEXT(next) || JUMPABLE(next)) {
3468 regnode *text_node = next;
3470 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3472 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3474 if (PL_regkind[(U8)OP(text_node)] == REF) {
3476 n = ARG(text_node); /* which paren pair */
3477 ln = PL_regstartp[n];
3478 /* assume yes if we haven't seen CLOSEn */
3480 (I32)*PL_reglastparen < n ||
3487 c1 = *(PL_bostr + ln);
3489 else { c1 = (U8)*STRING(text_node); }
3491 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3493 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3494 c2 = PL_fold_locale[c1];
3505 /* If it could work, try it. */
3507 UCHARAT(PL_reginput) == c1 ||
3508 UCHARAT(PL_reginput) == c2)
3511 PerlIO_printf(Perl_debug_log,
3512 "%*s trying tail with n=%"IVdf"...\n",
3513 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3517 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3518 PL_regendp[paren] = PL_reginput - PL_bostr;
3521 PL_regendp[paren] = -1;
3525 REGCP_UNWIND(lastcp);
3527 /* Couldn't or didn't -- back up. */
3529 locinput = HOPc(locinput, -l);
3530 PL_reginput = locinput;
3537 paren = scan->flags; /* Which paren to set */
3538 if (paren > PL_regsize)
3540 if (paren > (I32)*PL_reglastparen)
3541 *PL_reglastparen = paren;
3542 ln = ARG1(scan); /* min to match */
3543 n = ARG2(scan); /* max to match */
3544 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3548 ln = ARG1(scan); /* min to match */
3549 n = ARG2(scan); /* max to match */
3550 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3555 scan = NEXTOPER(scan);
3561 scan = NEXTOPER(scan);
3565 * Lookahead to avoid useless match attempts
3566 * when we know what character comes next.
3570 * Used to only do .*x and .*?x, but now it allows
3571 * for )'s, ('s and (?{ ... })'s to be in the way
3572 * of the quantifier and the EXACT-like node. -- japhy
3575 if (HAS_TEXT(next) || JUMPABLE(next)) {
3577 regnode *text_node = next;
3579 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3581 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3583 if (PL_regkind[(U8)OP(text_node)] == REF) {
3585 n = ARG(text_node); /* which paren pair */
3586 ln = PL_regstartp[n];
3587 /* assume yes if we haven't seen CLOSEn */
3589 (I32)*PL_reglastparen < n ||
3594 goto assume_ok_easy;
3596 s = (U8*)PL_bostr + ln;
3598 else { s = (U8*)STRING(text_node); }
3602 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3604 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3605 c2 = PL_fold_locale[c1];
3608 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3609 STRLEN ulen1, ulen2;
3610 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3611 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3613 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3614 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3616 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3618 0 : UTF8_ALLOW_ANY);
3619 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3621 0 : UTF8_ALLOW_ANY);
3624 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3626 0 : UTF8_ALLOW_ANY);
3634 PL_reginput = locinput;
3638 if (ln && regrepeat(scan, ln) < ln)
3640 locinput = PL_reginput;
3643 char *e; /* Should not check after this */
3644 char *old = locinput;
3647 if (n == REG_INFTY) {
3650 while (UTF8_IS_CONTINUATION(*(U8*)e))
3656 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3660 e = locinput + n - ln;
3665 /* Find place 'next' could work */
3668 while (locinput <= e &&
3669 UCHARAT(locinput) != c1)
3672 while (locinput <= e
3673 && UCHARAT(locinput) != c1
3674 && UCHARAT(locinput) != c2)
3677 count = locinput - old;
3682 /* count initialised to
3683 * utf8_distance(old, locinput) */
3684 while (locinput <= e &&
3685 utf8n_to_uvchr((U8*)locinput,
3688 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3693 /* count initialised to
3694 * utf8_distance(old, locinput) */
3695 while (locinput <= e) {
3696 UV c = utf8n_to_uvchr((U8*)locinput,
3699 0 : UTF8_ALLOW_ANY);
3700 if (c == (UV)c1 || c == (UV)c2)
3709 /* PL_reginput == old now */
3710 if (locinput != old) {
3711 ln = 1; /* Did some */
3712 if (regrepeat(scan, count) < count)
3715 /* PL_reginput == locinput now */
3716 TRYPAREN(paren, ln, locinput);
3717 PL_reginput = locinput; /* Could be reset... */
3718 REGCP_UNWIND(lastcp);
3719 /* Couldn't or didn't -- move forward. */
3722 locinput += UTF8SKIP(locinput);
3729 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3733 c = utf8n_to_uvchr((U8*)PL_reginput,
3736 0 : UTF8_ALLOW_ANY);
3738 c = UCHARAT(PL_reginput);
3739 /* If it could work, try it. */
3740 if (c == (UV)c1 || c == (UV)c2)
3742 TRYPAREN(paren, ln, PL_reginput);
3743 REGCP_UNWIND(lastcp);
3746 /* If it could work, try it. */
3747 else if (c1 == -1000)
3749 TRYPAREN(paren, ln, PL_reginput);
3750 REGCP_UNWIND(lastcp);
3752 /* Couldn't or didn't -- move forward. */
3753 PL_reginput = locinput;
3754 if (regrepeat(scan, 1)) {
3756 locinput = PL_reginput;
3764 n = regrepeat(scan, n);
3765 locinput = PL_reginput;
3766 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3767 ((!PL_multiline && OP(next) != MEOL) ||
3768 OP(next) == SEOL || OP(next) == EOS))
3770 ln = n; /* why back off? */
3771 /* ...because $ and \Z can match before *and* after
3772 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3773 We should back off by one in this case. */
3774 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3783 c = utf8n_to_uvchr((U8*)PL_reginput,
3786 0 : UTF8_ALLOW_ANY);
3788 c = UCHARAT(PL_reginput);
3790 /* If it could work, try it. */
3791 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3793 TRYPAREN(paren, n, PL_reginput);
3794 REGCP_UNWIND(lastcp);
3796 /* Couldn't or didn't -- back up. */
3798 PL_reginput = locinput = HOPc(locinput, -1);
3806 c = utf8n_to_uvchr((U8*)PL_reginput,
3809 0 : UTF8_ALLOW_ANY);
3811 c = UCHARAT(PL_reginput);
3813 /* If it could work, try it. */
3814 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3816 TRYPAREN(paren, n, PL_reginput);
3817 REGCP_UNWIND(lastcp);
3819 /* Couldn't or didn't -- back up. */
3821 PL_reginput = locinput = HOPc(locinput, -1);
3828 if (PL_reg_call_cc) {
3829 re_cc_state *cur_call_cc = PL_reg_call_cc;
3830 CURCUR *cctmp = PL_regcc;
3831 regexp *re = PL_reg_re;
3832 CHECKPOINT cp, lastcp;
3834 cp = regcppush(0); /* Save *all* the positions. */
3836 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3838 PL_reginput = locinput; /* Make position available to
3840 cache_re(PL_reg_call_cc->re);
3841 PL_regcc = PL_reg_call_cc->cc;
3842 PL_reg_call_cc = PL_reg_call_cc->prev;
3843 if (regmatch(cur_call_cc->node)) {
3844 PL_reg_call_cc = cur_call_cc;
3848 REGCP_UNWIND(lastcp);
3850 PL_reg_call_cc = cur_call_cc;
3856 PerlIO_printf(Perl_debug_log,
3857 "%*s continuation failed...\n",
3858 REPORT_CODE_OFF+PL_regindent*2, "")
3862 if (locinput < PL_regtill) {
3863 DEBUG_r(PerlIO_printf(Perl_debug_log,
3864 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3866 (long)(locinput - PL_reg_starttry),
3867 (long)(PL_regtill - PL_reg_starttry),
3869 sayNO_FINAL; /* Cannot match: too short. */
3871 PL_reginput = locinput; /* put where regtry can find it */
3872 sayYES_FINAL; /* Success! */
3874 PL_reginput = locinput; /* put where regtry can find it */
3875 sayYES_LOUD; /* Success! */
3878 PL_reginput = locinput;
3883 s = HOPBACKc(locinput, scan->flags);
3889 PL_reginput = locinput;
3894 s = HOPBACKc(locinput, scan->flags);
3900 PL_reginput = locinput;
3903 inner = NEXTOPER(NEXTOPER(scan));
3904 if (regmatch(inner) != n) {
3919 if (OP(scan) == SUSPEND) {
3920 locinput = PL_reginput;
3921 nextchr = UCHARAT(locinput);
3926 next = scan + ARG(scan);
3931 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3932 PTR2UV(scan), OP(scan));
3933 Perl_croak(aTHX_ "regexp memory corruption");
3940 * We get here only if there's trouble -- normally "case END" is
3941 * the terminating point.
3943 Perl_croak(aTHX_ "corrupted regexp pointers");
3949 PerlIO_printf(Perl_debug_log,
3950 "%*s %scould match...%s\n",
3951 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3955 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3956 PL_colors[4],PL_colors[5]));
3962 #if 0 /* Breaks $^R */
3970 PerlIO_printf(Perl_debug_log,
3971 "%*s %sfailed...%s\n",
3972 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3978 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3981 case RE_UNWIND_BRANCH:
3982 case RE_UNWIND_BRANCHJ:
3984 re_unwind_branch_t *uwb = &(uw->branch);
3985 I32 lastparen = uwb->lastparen;
3987 REGCP_UNWIND(uwb->lastcp);
3988 for (n = *PL_reglastparen; n > lastparen; n--)
3990 *PL_reglastparen = n;
3991 scan = next = uwb->next;
3993 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3994 ? BRANCH : BRANCHJ) ) { /* Failure */
4001 /* Have more choice yet. Reuse the same uwb. */
4003 if ((n = (uwb->type == RE_UNWIND_BRANCH
4004 ? NEXT_OFF(next) : ARG(next))))
4007 next = NULL; /* XXXX Needn't unwinding in this case... */
4009 next = NEXTOPER(scan);
4010 if (uwb->type == RE_UNWIND_BRANCHJ)
4011 next = NEXTOPER(next);
4012 locinput = uwb->locinput;
4013 nextchr = uwb->nextchr;
4015 PL_regindent = uwb->regindent;
4022 Perl_croak(aTHX_ "regexp unwind memory corruption");
4033 - regrepeat - repeatedly match something simple, report how many
4036 * [This routine now assumes that it will only match on things of length 1.
4037 * That was true before, but now we assume scan - reginput is the count,
4038 * rather than incrementing count on every character. [Er, except utf8.]]
4041 S_regrepeat(pTHX_ regnode *p, I32 max)
4043 register char *scan;
4045 register char *loceol = PL_regeol;
4046 register I32 hardcount = 0;
4047 register bool do_utf8 = PL_reg_match_utf8;
4050 if (max == REG_INFTY)
4052 else if (max < loceol - scan)
4053 loceol = scan + max;
4058 while (scan < loceol && hardcount < max && *scan != '\n') {
4059 scan += UTF8SKIP(scan);
4063 while (scan < loceol && *scan != '\n')
4070 while (scan < loceol && hardcount < max) {
4071 scan += UTF8SKIP(scan);
4081 case EXACT: /* length of string is 1 */
4083 while (scan < loceol && UCHARAT(scan) == c)
4086 case EXACTF: /* length of string is 1 */
4088 while (scan < loceol &&
4089 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4092 case EXACTFL: /* length of string is 1 */
4093 PL_reg_flags |= RF_tainted;
4095 while (scan < loceol &&
4096 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4102 while (hardcount < max && scan < loceol &&
4103 reginclass(p, (U8*)scan, 0, do_utf8)) {
4104 scan += UTF8SKIP(scan);
4108 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4115 LOAD_UTF8_CHARCLASS(alnum,"a");
4116 while (hardcount < max && scan < loceol &&
4117 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4118 scan += UTF8SKIP(scan);
4122 while (scan < loceol && isALNUM(*scan))
4127 PL_reg_flags |= RF_tainted;
4130 while (hardcount < max && scan < loceol &&
4131 isALNUM_LC_utf8((U8*)scan)) {
4132 scan += UTF8SKIP(scan);
4136 while (scan < loceol && isALNUM_LC(*scan))
4143 LOAD_UTF8_CHARCLASS(alnum,"a");
4144 while (hardcount < max && scan < loceol &&
4145 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4146 scan += UTF8SKIP(scan);
4150 while (scan < loceol && !isALNUM(*scan))
4155 PL_reg_flags |= RF_tainted;
4158 while (hardcount < max && scan < loceol &&
4159 !isALNUM_LC_utf8((U8*)scan)) {
4160 scan += UTF8SKIP(scan);
4164 while (scan < loceol && !isALNUM_LC(*scan))
4171 LOAD_UTF8_CHARCLASS(space," ");
4172 while (hardcount < max && scan < loceol &&
4174 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4175 scan += UTF8SKIP(scan);
4179 while (scan < loceol && isSPACE(*scan))
4184 PL_reg_flags |= RF_tainted;
4187 while (hardcount < max && scan < loceol &&
4188 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4189 scan += UTF8SKIP(scan);
4193 while (scan < loceol && isSPACE_LC(*scan))
4200 LOAD_UTF8_CHARCLASS(space," ");
4201 while (hardcount < max && scan < loceol &&
4203 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4204 scan += UTF8SKIP(scan);
4208 while (scan < loceol && !isSPACE(*scan))
4213 PL_reg_flags |= RF_tainted;
4216 while (hardcount < max && scan < loceol &&
4217 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4218 scan += UTF8SKIP(scan);
4222 while (scan < loceol && !isSPACE_LC(*scan))
4229 LOAD_UTF8_CHARCLASS(digit,"0");
4230 while (hardcount < max && scan < loceol &&
4231 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4232 scan += UTF8SKIP(scan);
4236 while (scan < loceol && isDIGIT(*scan))
4243 LOAD_UTF8_CHARCLASS(digit,"0");
4244 while (hardcount < max && scan < loceol &&
4245 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4246 scan += UTF8SKIP(scan);
4250 while (scan < loceol && !isDIGIT(*scan))
4254 default: /* Called on something of 0 width. */
4255 break; /* So match right here or not at all. */
4261 c = scan - PL_reginput;
4266 SV *prop = sv_newmortal();
4269 PerlIO_printf(Perl_debug_log,
4270 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4271 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4278 - regrepeat_hard - repeatedly match something, report total lenth and length
4280 * The repeater is supposed to have constant length.
4284 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4286 register char *scan = Nullch;
4287 register char *start;
4288 register char *loceol = PL_regeol;
4290 I32 count = 0, res = 1;
4295 start = PL_reginput;
4296 if (PL_reg_match_utf8) {
4297 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4300 while (start < PL_reginput) {
4302 start += UTF8SKIP(start);
4313 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4315 *lp = l = PL_reginput - start;
4316 if (max != REG_INFTY && l*max < loceol - scan)
4317 loceol = scan + l*max;
4330 - regclass_swash - prepare the utf8 swash
4334 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4340 if (PL_regdata && PL_regdata->count) {
4343 if (PL_regdata->what[n] == 's') {
4344 SV *rv = (SV*)PL_regdata->data[n];
4345 AV *av = (AV*)SvRV((SV*)rv);
4346 SV **ary = AvARRAY(av);
4349 /* See the end of regcomp.c:S_reglass() for
4350 * documentation of these array elements. */
4353 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4354 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4358 else if (si && doinit) {
4359 sw = swash_init("utf8", "", si, 1, 0);
4360 (void)av_store(av, 1, sw);
4376 - reginclass - determine if a character falls into a character class
4378 The n is the ANYOF regnode, the p is the target string, lenp
4379 is pointer to the maximum length of how far to go in the p
4380 (if the lenp is zero, UTF8SKIP(p) is used),
4381 do_utf8 tells whether the target string is in UTF-8.
4386 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4388 char flags = ANYOF_FLAGS(n);
4394 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4395 c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4396 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4398 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4399 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4402 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4403 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4406 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4410 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4413 if (swash_fetch(sw, p, do_utf8))
4415 else if (flags & ANYOF_FOLD) {
4416 if (!match && lenp && av) {
4419 for (i = 0; i <= av_len(av); i++) {
4420 SV* sv = *av_fetch(av, i, FALSE);
4422 char *s = SvPV(sv, len);
4424 if (len <= plen && memEQ(s, (char*)p, len)) {
4432 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4435 to_utf8_fold(p, tmpbuf, &tmplen);
4436 if (swash_fetch(sw, tmpbuf, do_utf8))
4442 if (match && lenp && *lenp == 0)
4443 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4445 if (!match && c < 256) {
4446 if (ANYOF_BITMAP_TEST(n, c))
4448 else if (flags & ANYOF_FOLD) {
4451 if (flags & ANYOF_LOCALE) {
4452 PL_reg_flags |= RF_tainted;
4453 f = PL_fold_locale[c];
4457 if (f != c && ANYOF_BITMAP_TEST(n, f))
4461 if (!match && (flags & ANYOF_CLASS)) {
4462 PL_reg_flags |= RF_tainted;
4464 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4465 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4466 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4467 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4468 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4469 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4470 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4471 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4472 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4473 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4474 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4475 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4476 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4477 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4478 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4479 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4480 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4481 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4482 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4483 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4484 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4485 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4486 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4487 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4488 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4489 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4490 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4491 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4492 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4493 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4494 ) /* How's that for a conditional? */
4501 return (flags & ANYOF_INVERT) ? !match : match;
4505 S_reghop(pTHX_ U8 *s, I32 off)
4507 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4511 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4514 while (off-- && s < lim) {
4515 /* XXX could check well-formedness here */
4523 if (UTF8_IS_CONTINUED(*s)) {
4524 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4527 /* XXX could check well-formedness here */
4535 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4537 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4541 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4544 while (off-- && s < lim) {
4545 /* XXX could check well-formedness here */
4555 if (UTF8_IS_CONTINUED(*s)) {
4556 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4559 /* XXX could check well-formedness here */
4571 restore_pos(pTHX_ void *arg)
4573 if (PL_reg_eval_set) {
4574 if (PL_reg_oldsaved) {
4575 PL_reg_re->subbeg = PL_reg_oldsaved;
4576 PL_reg_re->sublen = PL_reg_oldsavedlen;
4577 #ifdef PERL_COPY_ON_WRITE
4578 PL_reg_re->saved_copy = PL_nrs;
4580 RX_MATCH_COPIED_on(PL_reg_re);
4582 PL_reg_magic->mg_len = PL_reg_oldpos;
4583 PL_reg_eval_set = 0;
4584 PL_curpm = PL_reg_oldcurpm;
4589 S_to_utf8_substr(pTHX_ register regexp *prog)
4592 if (prog->float_substr && !prog->float_utf8) {
4593 prog->float_utf8 = sv = NEWSV(117, 0);
4594 SvSetSV(sv, prog->float_substr);
4595 sv_utf8_upgrade(sv);
4596 if (SvTAIL(prog->float_substr))
4598 if (prog->float_substr == prog->check_substr)
4599 prog->check_utf8 = sv;
4601 if (prog->anchored_substr && !prog->anchored_utf8) {
4602 prog->anchored_utf8 = sv = NEWSV(118, 0);
4603 SvSetSV(sv, prog->anchored_substr);
4604 sv_utf8_upgrade(sv);
4605 if (SvTAIL(prog->anchored_substr))
4607 if (prog->anchored_substr == prog->check_substr)
4608 prog->check_utf8 = sv;
4613 S_to_byte_substr(pTHX_ register regexp *prog)
4616 if (prog->float_utf8 && !prog->float_substr) {
4617 prog->float_substr = sv = NEWSV(117, 0);
4618 SvSetSV(sv, prog->float_utf8);
4619 if (sv_utf8_downgrade(sv, TRUE)) {
4620 if (SvTAIL(prog->float_utf8))
4624 prog->float_substr = sv = &PL_sv_undef;
4626 if (prog->float_utf8 == prog->check_utf8)
4627 prog->check_substr = sv;
4629 if (prog->anchored_utf8 && !prog->anchored_substr) {
4630 prog->anchored_substr = sv = NEWSV(118, 0);
4631 SvSetSV(sv, prog->anchored_utf8);
4632 if (sv_utf8_downgrade(sv, TRUE)) {
4633 if (SvTAIL(prog->anchored_utf8))
4637 prog->anchored_substr = sv = &PL_sv_undef;
4639 if (prog->anchored_utf8 == prog->check_utf8)
4640 prog->check_substr = sv;