5 * "One Ring to rule them all, One Ring to find them..."
8 /* This file contains functions for executing a regular expression. See
9 * also regcomp.c which funnily enough, contains functions for compiling
10 * a regular expression.
12 * This file is also copied at build time to ext/re/re_exec.c, where
13 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
14 * This causes the main functions to be compiled under new names and with
15 * debugging support added, which makes "use re 'debug'" work.
19 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
20 * confused with the original package (see point 3 below). Thanks, Henry!
23 /* Additional note: this code is very heavily munged from Henry's version
24 * in places. In some spots I've traded clarity for efficiency, so don't
25 * blame Henry for some of the lack of readability.
28 /* The names of the functions have been changed from regcomp and
29 * regexec to pregcomp and pregexec in order to avoid conflicts
30 * with the POSIX routines of the same names.
33 #ifdef PERL_EXT_RE_BUILD
34 /* need to replace pregcomp et al, so enable that */
35 # ifndef PERL_IN_XSUB_RE
36 # define PERL_IN_XSUB_RE
38 /* need access to debugger hooks */
39 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
44 #ifdef PERL_IN_XSUB_RE
45 /* We *really* need to overwrite these symbols: */
46 # define Perl_regexec_flags my_regexec
47 # define Perl_regdump my_regdump
48 # define Perl_regprop my_regprop
49 # define Perl_re_intuit_start my_re_intuit_start
50 /* *These* symbols are masked to allow static link. */
51 # define Perl_pregexec my_pregexec
52 # define Perl_reginitcolors my_reginitcolors
53 # define Perl_regclass_swash my_regclass_swash
55 # define PERL_NO_GET_CONTEXT
60 * pregcomp and pregexec -- regsub and regerror are not used in perl
62 * Copyright (c) 1986 by University of Toronto.
63 * Written by Henry Spencer. Not derived from licensed software.
65 * Permission is granted to anyone to use this software for any
66 * purpose on any computer system, and to redistribute it freely,
67 * subject to the following restrictions:
69 * 1. The author is not responsible for the consequences of use of
70 * this software, no matter how awful, even if they arise
73 * 2. The origin of this software must not be misrepresented, either
74 * by explicit claim or by omission.
76 * 3. Altered versions must be plainly marked as such, and must not
77 * be misrepresented as being the original software.
79 **** Alterations to Henry's code are...
81 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
82 **** 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
84 **** You may distribute under the terms of either the GNU General Public
85 **** License or the Artistic License, as specified in the README file.
87 * Beware that some of this code is subtly aware of the way operator
88 * precedence is structured in regular expressions. Serious changes in
89 * regular-expression syntax might require a total rethink.
92 #define PERL_IN_REGEXEC_C
97 #define RF_tainted 1 /* tainted information used? */
98 #define RF_warned 2 /* warned about big count? */
99 #define RF_evaled 4 /* Did an EVAL with setting? */
100 #define RF_utf8 8 /* String contains multibyte chars? */
102 #define UTF ((PL_reg_flags & RF_utf8) != 0)
104 #define RS_init 1 /* eval environment created */
105 #define RS_set 2 /* replsv value is set */
108 #define STATIC static
111 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
117 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
118 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
120 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
121 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
122 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
123 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
124 #define HOPc(pos,off) ((char*)HOP(pos,off))
125 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
127 #define HOPBACK(pos, off) ( \
128 (PL_reg_match_utf8) \
129 ? reghopmaybe((U8*)pos, -off) \
130 : (pos - off >= PL_bostr) \
134 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
136 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
137 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
138 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
139 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
140 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
141 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
143 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) { ENTER; save_re_context(); (void)CAT2(is_utf8_, a)((const U8*)b); LEAVE; } } STMT_END
145 /* for use after a quantifier and before an EXACT-like node -- japhy */
146 #define JUMPABLE(rn) ( \
147 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
148 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
149 OP(rn) == PLUS || OP(rn) == MINMOD || \
150 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
153 #define HAS_TEXT(rn) ( \
154 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
158 Search for mandatory following text node; for lookahead, the text must
159 follow but for lookbehind (rn->flags != 0) we skip to the next step.
161 #define FIND_NEXT_IMPT(rn) STMT_START { \
162 while (JUMPABLE(rn)) \
163 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
164 rn = NEXTOPER(NEXTOPER(rn)); \
165 else if (OP(rn) == PLUS) \
167 else if (OP(rn) == IFMATCH) \
168 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
169 else rn += NEXT_OFF(rn); \
172 static void restore_pos(pTHX_ void *arg);
175 S_regcppush(pTHX_ I32 parenfloor)
177 const int retval = PL_savestack_ix;
178 #define REGCP_PAREN_ELEMS 4
179 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
182 if (paren_elems_to_push < 0)
183 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
185 #define REGCP_OTHER_ELEMS 6
186 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
187 for (p = PL_regsize; p > parenfloor; p--) {
188 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
189 SSPUSHINT(PL_regendp[p]);
190 SSPUSHINT(PL_regstartp[p]);
191 SSPUSHPTR(PL_reg_start_tmp[p]);
194 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
195 SSPUSHINT(PL_regsize);
196 SSPUSHINT(*PL_reglastparen);
197 SSPUSHINT(*PL_reglastcloseparen);
198 SSPUSHPTR(PL_reginput);
199 #define REGCP_FRAME_ELEMS 2
200 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
201 * are needed for the regexp context stack bookkeeping. */
202 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
203 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
208 /* These are needed since we do not localize EVAL nodes: */
209 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
210 " Setting an EVAL scope, savestack=%"IVdf"\n", \
211 (IV)PL_savestack_ix)); cp = PL_savestack_ix
213 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
214 PerlIO_printf(Perl_debug_log, \
215 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
216 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
225 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
227 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
228 i = SSPOPINT; /* Parentheses elements to pop. */
229 input = (char *) SSPOPPTR;
230 *PL_reglastcloseparen = SSPOPINT;
231 *PL_reglastparen = SSPOPINT;
232 PL_regsize = SSPOPINT;
234 /* Now restore the parentheses context. */
235 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
236 i > 0; i -= REGCP_PAREN_ELEMS) {
238 paren = (U32)SSPOPINT;
239 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
240 PL_regstartp[paren] = SSPOPINT;
242 if (paren <= *PL_reglastparen)
243 PL_regendp[paren] = tmps;
245 PerlIO_printf(Perl_debug_log,
246 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
247 (UV)paren, (IV)PL_regstartp[paren],
248 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
249 (IV)PL_regendp[paren],
250 (paren > *PL_reglastparen ? "(no)" : ""));
254 if ((I32)(*PL_reglastparen + 1) <= PL_regnpar) {
255 PerlIO_printf(Perl_debug_log,
256 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
257 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
261 /* It would seem that the similar code in regtry()
262 * already takes care of this, and in fact it is in
263 * a better location to since this code can #if 0-ed out
264 * but the code in regtry() is needed or otherwise tests
265 * requiring null fields (pat.t#187 and split.t#{13,14}
266 * (as of patchlevel 7877) will fail. Then again,
267 * this code seems to be necessary or otherwise
268 * building DynaLoader will fail:
269 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
271 for (paren = *PL_reglastparen + 1; (I32)paren <= PL_regnpar; paren++) {
272 if ((I32)paren > PL_regsize)
273 PL_regstartp[paren] = -1;
274 PL_regendp[paren] = -1;
281 S_regcp_set_to(pTHX_ I32 ss)
283 const I32 tmp = PL_savestack_ix;
285 PL_savestack_ix = ss;
287 PL_savestack_ix = tmp;
291 typedef struct re_cc_state
295 struct re_cc_state *prev;
300 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
302 #define TRYPAREN(paren, n, input) { \
305 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
306 PL_regendp[paren] = input - PL_bostr; \
309 PL_regendp[paren] = -1; \
311 if (regmatch(next)) \
314 PL_regendp[paren] = -1; \
319 * pregexec and friends
323 - pregexec - match a regexp against a string
326 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
327 char *strbeg, I32 minend, SV *screamer, U32 nosave)
328 /* strend: pointer to null at end of string */
329 /* strbeg: real beginning of string */
330 /* minend: end of match must be >=minend after stringarg. */
331 /* nosave: For optimizations. */
334 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
335 nosave ? 0 : REXEC_COPY_STR);
339 S_cache_re(pTHX_ regexp *prog)
341 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
343 PL_regprogram = prog->program;
345 PL_regnpar = prog->nparens;
346 PL_regdata = prog->data;
351 * Need to implement the following flags for reg_anch:
353 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
355 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
356 * INTUIT_AUTORITATIVE_ML
357 * INTUIT_ONCE_NOML - Intuit can match in one location only.
360 * Another flag for this function: SECOND_TIME (so that float substrs
361 * with giant delta may be not rechecked).
364 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
366 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
367 Otherwise, only SvCUR(sv) is used to get strbeg. */
369 /* XXXX We assume that strpos is strbeg unless sv. */
371 /* XXXX Some places assume that there is a fixed substring.
372 An update may be needed if optimizer marks as "INTUITable"
373 RExen without fixed substrings. Similarly, it is assumed that
374 lengths of all the strings are no more than minlen, thus they
375 cannot come from lookahead.
376 (Or minlen should take into account lookahead.) */
378 /* A failure to find a constant substring means that there is no need to make
379 an expensive call to REx engine, thus we celebrate a failure. Similarly,
380 finding a substring too deep into the string means that less calls to
381 regtry() should be needed.
383 REx compiler's optimizer found 4 possible hints:
384 a) Anchored substring;
386 c) Whether we are anchored (beginning-of-line or \G);
387 d) First node (of those at offset 0) which may distingush positions;
388 We use a)b)d) and multiline-part of c), and try to find a position in the
389 string which does not contradict any of them.
392 /* Most of decisions we do here should have been done at compile time.
393 The nodes of the REx which we used for the search should have been
394 deleted from the finite automaton. */
397 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
398 char *strend, U32 flags, re_scream_pos_data *data)
400 register I32 start_shift = 0;
401 /* Should be nonnegative! */
402 register I32 end_shift = 0;
407 const int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
409 register char *other_last = Nullch; /* other substr checked before this */
410 char *check_at = Nullch; /* check substr found at this pos */
412 char *i_strpos = strpos;
413 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
415 RX_MATCH_UTF8_set(prog,do_utf8);
417 if (prog->reganch & ROPT_UTF8) {
418 DEBUG_r(PerlIO_printf(Perl_debug_log,
419 "UTF-8 regex...\n"));
420 PL_reg_flags |= RF_utf8;
424 const char *s = PL_reg_match_utf8 ?
425 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
427 const int len = PL_reg_match_utf8 ?
428 strlen(s) : strend - strpos;
431 if (PL_reg_match_utf8)
432 DEBUG_r(PerlIO_printf(Perl_debug_log,
433 "UTF-8 target...\n"));
434 PerlIO_printf(Perl_debug_log,
435 "%sGuessing start of match, REx%s \"%s%.60s%s%s\" against \"%s%.*s%s%s\"...\n",
436 PL_colors[4],PL_colors[5],PL_colors[0],
439 (strlen(prog->precomp) > 60 ? "..." : ""),
441 (int)(len > 60 ? 60 : len),
443 (len > 60 ? "..." : "")
447 /* CHR_DIST() would be more correct here but it makes things slow. */
448 if (prog->minlen > strend - strpos) {
449 DEBUG_r(PerlIO_printf(Perl_debug_log,
450 "String too short... [re_intuit_start]\n"));
453 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
456 if (!prog->check_utf8 && prog->check_substr)
457 to_utf8_substr(prog);
458 check = prog->check_utf8;
460 if (!prog->check_substr && prog->check_utf8)
461 to_byte_substr(prog);
462 check = prog->check_substr;
464 if (check == &PL_sv_undef) {
465 DEBUG_r(PerlIO_printf(Perl_debug_log,
466 "Non-utf string cannot match utf check string\n"));
469 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
470 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
471 || ( (prog->reganch & ROPT_ANCH_BOL)
472 && !PL_multiline ) ); /* Check after \n? */
475 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
476 | ROPT_IMPLICIT)) /* not a real BOL */
477 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
479 && (strpos != strbeg)) {
480 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
483 if (prog->check_offset_min == prog->check_offset_max &&
484 !(prog->reganch & ROPT_CANY_SEEN)) {
485 /* Substring at constant offset from beg-of-str... */
488 s = HOP3c(strpos, prog->check_offset_min, strend);
490 slen = SvCUR(check); /* >= 1 */
492 if ( strend - s > slen || strend - s < slen - 1
493 || (strend - s == slen && strend[-1] != '\n')) {
494 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
497 /* Now should match s[0..slen-2] */
499 if (slen && (*SvPVX_const(check) != *s
501 && memNE(SvPVX_const(check), s, slen)))) {
503 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
507 else if (*SvPVX_const(check) != *s
508 || ((slen = SvCUR(check)) > 1
509 && memNE(SvPVX_const(check), s, slen)))
511 goto success_at_start;
514 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
516 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
517 end_shift = prog->minlen - start_shift -
518 CHR_SVLEN(check) + (SvTAIL(check) != 0);
520 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
521 - (SvTAIL(check) != 0);
522 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
524 if (end_shift < eshift)
528 else { /* Can match at random position */
531 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
532 /* Should be nonnegative! */
533 end_shift = prog->minlen - start_shift -
534 CHR_SVLEN(check) + (SvTAIL(check) != 0);
537 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
539 Perl_croak(aTHX_ "panic: end_shift");
543 /* Find a possible match in the region s..strend by looking for
544 the "check" substring in the region corrected by start/end_shift. */
545 if (flags & REXEC_SCREAM) {
546 I32 p = -1; /* Internal iterator of scream. */
547 I32 * const pp = data ? data->scream_pos : &p;
549 if (PL_screamfirst[BmRARE(check)] >= 0
550 || ( BmRARE(check) == '\n'
551 && (BmPREVIOUS(check) == SvCUR(check) - 1)
553 s = screaminstr(sv, check,
554 start_shift + (s - strbeg), end_shift, pp, 0);
557 /* we may be pointing at the wrong string */
558 if (s && RX_MATCH_COPIED(prog))
559 s = strbeg + (s - SvPVX_const(sv));
561 *data->scream_olds = s;
563 else if (prog->reganch & ROPT_CANY_SEEN)
564 s = fbm_instr((U8*)(s + start_shift),
565 (U8*)(strend - end_shift),
566 check, PL_multiline ? FBMrf_MULTILINE : 0);
568 s = fbm_instr(HOP3(s, start_shift, strend),
569 HOP3(strend, -end_shift, strbeg),
570 check, PL_multiline ? FBMrf_MULTILINE : 0);
572 /* Update the count-of-usability, remove useless subpatterns,
575 /* FIXME - DEBUG_EXECUTE_r if that is merged to maint. */
576 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr \"%s%.*s%s\"%s%s",
577 (s ? "Found" : "Did not find"),
578 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
580 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
582 PL_colors[1], (SvTAIL(check) ? "$" : ""),
583 (s ? " at offset " : "...\n") ) );
590 /* Finish the diagnostic message */
591 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
593 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
594 Start with the other substr.
595 XXXX no SCREAM optimization yet - and a very coarse implementation
596 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
597 *always* match. Probably should be marked during compile...
598 Probably it is right to do no SCREAM here...
601 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
602 /* Take into account the "other" substring. */
603 /* XXXX May be hopelessly wrong for UTF... */
606 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
609 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
613 t = s - prog->check_offset_max;
614 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
616 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
621 t = HOP3c(t, prog->anchored_offset, strend);
622 if (t < other_last) /* These positions already checked */
624 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
627 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
628 /* On end-of-str: see comment below. */
629 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
630 if (must == &PL_sv_undef) {
632 DEBUG_r(must = prog->anchored_utf8); /* for debug */
637 HOP3(HOP3(last1, prog->anchored_offset, strend)
638 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
640 PL_multiline ? FBMrf_MULTILINE : 0
642 DEBUG_r(PerlIO_printf(Perl_debug_log,
643 "%s anchored substr \"%s%.*s%s\"%s",
644 (s ? "Found" : "Contradicts"),
647 - (SvTAIL(must)!=0)),
649 PL_colors[1], (SvTAIL(must) ? "$" : "")));
651 if (last1 >= last2) {
652 DEBUG_r(PerlIO_printf(Perl_debug_log,
653 ", giving up...\n"));
656 DEBUG_r(PerlIO_printf(Perl_debug_log,
657 ", trying floating at offset %ld...\n",
658 (long)(HOP3c(s1, 1, strend) - i_strpos)));
659 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
660 s = HOP3c(last, 1, strend);
664 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
665 (long)(s - i_strpos)));
666 t = HOP3c(s, -prog->anchored_offset, strbeg);
667 other_last = HOP3c(s, 1, strend);
675 else { /* Take into account the floating substring. */
680 t = HOP3c(s, -start_shift, strbeg);
682 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
683 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
684 last = HOP3c(t, prog->float_max_offset, strend);
685 s = HOP3c(t, prog->float_min_offset, strend);
688 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
689 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
690 /* fbm_instr() takes into account exact value of end-of-str
691 if the check is SvTAIL(ed). Since false positives are OK,
692 and end-of-str is not later than strend we are OK. */
693 if (must == &PL_sv_undef) {
695 DEBUG_r(must = prog->float_utf8); /* for debug message */
698 s = fbm_instr((unsigned char*)s,
699 (unsigned char*)last + SvCUR(must)
701 must, PL_multiline ? FBMrf_MULTILINE : 0);
702 /* FIXME - DEBUG_EXECUTE_r if that is merged to maint */
703 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr \"%s%.*s%s\"%s",
704 (s ? "Found" : "Contradicts"),
706 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
708 PL_colors[1], (SvTAIL(must) ? "$" : "")));
711 DEBUG_r(PerlIO_printf(Perl_debug_log,
712 ", giving up...\n"));
715 DEBUG_r(PerlIO_printf(Perl_debug_log,
716 ", trying anchored starting at offset %ld...\n",
717 (long)(s1 + 1 - i_strpos)));
719 s = HOP3c(t, 1, strend);
723 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
724 (long)(s - i_strpos)));
725 other_last = s; /* Fix this later. --Hugo */
734 t = s - prog->check_offset_max;
735 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
737 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
739 /* Fixed substring is found far enough so that the match
740 cannot start at strpos. */
742 if (ml_anch && t[-1] != '\n') {
743 /* Eventually fbm_*() should handle this, but often
744 anchored_offset is not 0, so this check will not be wasted. */
745 /* XXXX In the code below we prefer to look for "^" even in
746 presence of anchored substrings. And we search even
747 beyond the found float position. These pessimizations
748 are historical artefacts only. */
750 while (t < strend - prog->minlen) {
752 if (t < check_at - prog->check_offset_min) {
753 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
754 /* Since we moved from the found position,
755 we definitely contradict the found anchored
756 substr. Due to the above check we do not
757 contradict "check" substr.
758 Thus we can arrive here only if check substr
759 is float. Redo checking for "other"=="fixed".
762 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
763 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
764 goto do_other_anchored;
766 /* We don't contradict the found floating substring. */
767 /* XXXX Why not check for STCLASS? */
769 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
770 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
773 /* Position contradicts check-string */
774 /* XXXX probably better to look for check-string
775 than for "\n", so one should lower the limit for t? */
776 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
777 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
778 other_last = strpos = s = t + 1;
783 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
784 PL_colors[0],PL_colors[1]));
788 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
789 PL_colors[0],PL_colors[1]));
793 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
796 /* The found string does not prohibit matching at strpos,
797 - no optimization of calling REx engine can be performed,
798 unless it was an MBOL and we are not after MBOL,
799 or a future STCLASS check will fail this. */
801 /* Even in this situation we may use MBOL flag if strpos is offset
802 wrt the start of the string. */
803 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
804 && (strpos != strbeg) && strpos[-1] != '\n'
805 /* May be due to an implicit anchor of m{.*foo} */
806 && !(prog->reganch & ROPT_IMPLICIT))
811 DEBUG_r( if (ml_anch)
812 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
813 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
816 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
818 prog->check_utf8 /* Could be deleted already */
819 && --BmUSEFUL(prog->check_utf8) < 0
820 && (prog->check_utf8 == prog->float_utf8)
822 prog->check_substr /* Could be deleted already */
823 && --BmUSEFUL(prog->check_substr) < 0
824 && (prog->check_substr == prog->float_substr)
827 /* If flags & SOMETHING - do not do it many times on the same match */
828 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
829 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
830 if (do_utf8 ? prog->check_substr : prog->check_utf8)
831 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
832 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
833 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
834 check = Nullsv; /* abort */
836 /* XXXX This is a remnant of the old implementation. It
837 looks wasteful, since now INTUIT can use many
839 prog->reganch &= ~RE_USE_INTUIT;
846 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
847 if (prog->regstclass) {
848 /* minlen == 0 is possible if regstclass is \b or \B,
849 and the fixed substr is ''$.
850 Since minlen is already taken into account, s+1 is before strend;
851 accidentally, minlen >= 1 guaranties no false positives at s + 1
852 even for \b or \B. But (minlen? 1 : 0) below assumes that
853 regstclass does not come from lookahead... */
854 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
855 This leaves EXACTF only, which is dealt with in find_byclass(). */
856 const U8* str = (U8*)STRING(prog->regstclass);
857 const int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
858 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
860 const char * const endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
861 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
862 : (prog->float_substr || prog->float_utf8
863 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
869 s = find_byclass(prog, prog->regstclass, s, endpos, 1);
872 const char *what = 0;
874 if (endpos == strend) {
875 DEBUG_r( PerlIO_printf(Perl_debug_log,
876 "Could not match STCLASS...\n") );
879 DEBUG_r( PerlIO_printf(Perl_debug_log,
880 "This position contradicts STCLASS...\n") );
881 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
883 /* Contradict one of substrings */
884 if (prog->anchored_substr || prog->anchored_utf8) {
885 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
886 DEBUG_r( what = "anchored" );
888 s = HOP3c(t, 1, strend);
889 if (s + start_shift + end_shift > strend) {
890 /* XXXX Should be taken into account earlier? */
891 DEBUG_r( PerlIO_printf(Perl_debug_log,
892 "Could not match STCLASS...\n") );
897 DEBUG_r( PerlIO_printf(Perl_debug_log,
898 "Looking for %s substr starting at offset %ld...\n",
899 what, (long)(s + start_shift - i_strpos)) );
902 /* Have both, check_string is floating */
903 if (t + start_shift >= check_at) /* Contradicts floating=check */
904 goto retry_floating_check;
905 /* Recheck anchored substring, but not floating... */
909 DEBUG_r( PerlIO_printf(Perl_debug_log,
910 "Looking for anchored substr starting at offset %ld...\n",
911 (long)(other_last - i_strpos)) );
912 goto do_other_anchored;
914 /* Another way we could have checked stclass at the
915 current position only: */
920 DEBUG_r( PerlIO_printf(Perl_debug_log,
921 "Looking for /%s^%s/m starting at offset %ld...\n",
922 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
925 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
927 /* Check is floating subtring. */
928 retry_floating_check:
929 t = check_at - start_shift;
930 DEBUG_r( what = "floating" );
931 goto hop_and_restart;
934 DEBUG_r(PerlIO_printf(Perl_debug_log,
935 "By STCLASS: moving %ld --> %ld\n",
936 (long)(t - i_strpos), (long)(s - i_strpos))
940 DEBUG_r(PerlIO_printf(Perl_debug_log,
941 "Does not contradict STCLASS...\n");
946 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
947 PL_colors[4], (check ? "Guessed" : "Giving up"),
948 PL_colors[5], (long)(s - i_strpos)) );
951 fail_finish: /* Substring not found */
952 if (prog->check_substr || prog->check_utf8) /* could be removed already */
953 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
955 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
956 PL_colors[4],PL_colors[5]));
960 /* We know what class REx starts with. Try to find this position... */
962 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, const char *strend, I32 norun)
964 const I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
968 register STRLEN uskip;
972 register I32 tmp = 1; /* Scratch variable? */
973 register const bool do_utf8 = PL_reg_match_utf8;
975 /* We know what class it must start with. */
979 while (s + (uskip = UTF8SKIP(s)) <= strend) {
980 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
981 !UTF8_IS_INVARIANT((U8)s[0]) ?
982 reginclass(c, (U8*)s, 0, do_utf8) :
983 REGINCLASS(c, (U8*)s)) {
984 if (tmp && (norun || regtry(prog, s)))
998 if (REGINCLASS(c, (U8*)s) ||
999 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1000 /* The assignment of 2 is intentional:
1001 * for the folded sharp s, the skip is 2. */
1002 (skip = SHARP_S_SKIP))) {
1003 if (tmp && (norun || regtry(prog, s)))
1015 while (s < strend) {
1016 if (tmp && (norun || regtry(prog, s)))
1025 ln = STR_LEN(c); /* length to match in octets/bytes */
1026 lnc = (I32) ln; /* length to match in characters */
1028 STRLEN ulen1, ulen2;
1030 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1031 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1033 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1034 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1036 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1037 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1038 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1039 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1041 while (sm < ((U8 *) m + ln)) {
1056 c2 = PL_fold_locale[c1];
1058 e = HOP3c(strend, -((I32)lnc), s);
1061 e = s; /* Due to minlen logic of intuit() */
1063 /* The idea in the EXACTF* cases is to first find the
1064 * first character of the EXACTF* node and then, if
1065 * necessary, case-insensitively compare the full
1066 * text of the node. The c1 and c2 are the first
1067 * characters (though in Unicode it gets a bit
1068 * more complicated because there are more cases
1069 * than just upper and lower: one needs to use
1070 * the so-called folding case for case-insensitive
1071 * matching (called "loose matching" in Unicode).
1072 * ibcmp_utf8() will do just that. */
1076 U8 tmpbuf [UTF8_MAXBYTES+1];
1077 STRLEN len, foldlen;
1080 /* Upper and lower of 1st char are equal -
1081 * probably not a "letter". */
1083 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1085 0 : UTF8_ALLOW_ANY);
1088 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1089 m, (char **)0, ln, (bool)UTF))
1090 && (norun || regtry(prog, s)) )
1093 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1094 uvchr_to_utf8(tmpbuf, c);
1095 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1097 && (f == c1 || f == c2)
1098 && (ln == foldlen ||
1099 !ibcmp_utf8((char *) foldbuf,
1100 (char **)0, foldlen, do_utf8,
1102 (char **)0, ln, (bool)UTF))
1103 && (norun || regtry(prog, s)) )
1111 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1113 0 : UTF8_ALLOW_ANY);
1115 /* Handle some of the three Greek sigmas cases.
1116 * Note that not all the possible combinations
1117 * are handled here: some of them are handled
1118 * by the standard folding rules, and some of
1119 * them (the character class or ANYOF cases)
1120 * are handled during compiletime in
1121 * regexec.c:S_regclass(). */
1122 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1123 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1124 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1126 if ( (c == c1 || c == c2)
1128 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1129 m, (char **)0, ln, (bool)UTF))
1130 && (norun || regtry(prog, s)) )
1133 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
1134 uvchr_to_utf8(tmpbuf, c);
1135 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1137 && (f == c1 || f == c2)
1138 && (ln == foldlen ||
1139 !ibcmp_utf8((char *) foldbuf,
1140 (char **)0, foldlen, do_utf8,
1142 (char **)0, ln, (bool)UTF))
1143 && (norun || regtry(prog, s)) )
1154 && (ln == 1 || !(OP(c) == EXACTF
1156 : ibcmp_locale(s, m, ln)))
1157 && (norun || regtry(prog, s)) )
1163 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1164 && (ln == 1 || !(OP(c) == EXACTF
1166 : ibcmp_locale(s, m, ln)))
1167 && (norun || regtry(prog, s)) )
1174 PL_reg_flags |= RF_tainted;
1181 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1183 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1185 tmp = ((OP(c) == BOUND ?
1186 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1187 LOAD_UTF8_CHARCLASS(alnum,"a");
1188 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1189 if (tmp == !(OP(c) == BOUND ?
1190 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1191 isALNUM_LC_utf8((U8*)s)))
1194 if ((norun || regtry(prog, s)))
1201 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1202 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1203 while (s < strend) {
1205 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1207 if ((norun || regtry(prog, s)))
1213 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1217 PL_reg_flags |= RF_tainted;
1224 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1226 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1228 tmp = ((OP(c) == NBOUND ?
1229 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1230 LOAD_UTF8_CHARCLASS(alnum,"a");
1231 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1232 if (tmp == !(OP(c) == NBOUND ?
1233 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1234 isALNUM_LC_utf8((U8*)s)))
1236 else if ((norun || regtry(prog, s)))
1242 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1243 tmp = ((OP(c) == NBOUND ?
1244 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1245 while (s < strend) {
1247 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1249 else if ((norun || regtry(prog, s)))
1254 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1259 LOAD_UTF8_CHARCLASS(alnum,"a");
1260 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1261 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1262 if (tmp && (norun || regtry(prog, s)))
1273 while (s < strend) {
1275 if (tmp && (norun || regtry(prog, s)))
1287 PL_reg_flags |= RF_tainted;
1289 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1290 if (isALNUM_LC_utf8((U8*)s)) {
1291 if (tmp && (norun || regtry(prog, s)))
1302 while (s < strend) {
1303 if (isALNUM_LC(*s)) {
1304 if (tmp && (norun || regtry(prog, s)))
1317 LOAD_UTF8_CHARCLASS(alnum,"a");
1318 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1319 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1320 if (tmp && (norun || regtry(prog, s)))
1331 while (s < strend) {
1333 if (tmp && (norun || regtry(prog, s)))
1345 PL_reg_flags |= RF_tainted;
1347 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1348 if (!isALNUM_LC_utf8((U8*)s)) {
1349 if (tmp && (norun || regtry(prog, s)))
1360 while (s < strend) {
1361 if (!isALNUM_LC(*s)) {
1362 if (tmp && (norun || regtry(prog, s)))
1375 LOAD_UTF8_CHARCLASS(space," ");
1376 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1377 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1378 if (tmp && (norun || regtry(prog, s)))
1389 while (s < strend) {
1391 if (tmp && (norun || regtry(prog, s)))
1403 PL_reg_flags |= RF_tainted;
1405 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1406 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1407 if (tmp && (norun || regtry(prog, s)))
1418 while (s < strend) {
1419 if (isSPACE_LC(*s)) {
1420 if (tmp && (norun || regtry(prog, s)))
1433 LOAD_UTF8_CHARCLASS(space," ");
1434 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1435 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1436 if (tmp && (norun || regtry(prog, s)))
1447 while (s < strend) {
1449 if (tmp && (norun || regtry(prog, s)))
1461 PL_reg_flags |= RF_tainted;
1463 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1464 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1465 if (tmp && (norun || regtry(prog, s)))
1476 while (s < strend) {
1477 if (!isSPACE_LC(*s)) {
1478 if (tmp && (norun || regtry(prog, s)))
1491 LOAD_UTF8_CHARCLASS(digit,"0");
1492 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1493 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1494 if (tmp && (norun || regtry(prog, s)))
1505 while (s < strend) {
1507 if (tmp && (norun || regtry(prog, s)))
1519 PL_reg_flags |= RF_tainted;
1521 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1522 if (isDIGIT_LC_utf8((U8*)s)) {
1523 if (tmp && (norun || regtry(prog, s)))
1534 while (s < strend) {
1535 if (isDIGIT_LC(*s)) {
1536 if (tmp && (norun || regtry(prog, s)))
1549 LOAD_UTF8_CHARCLASS(digit,"0");
1550 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1551 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1552 if (tmp && (norun || regtry(prog, s)))
1563 while (s < strend) {
1565 if (tmp && (norun || regtry(prog, s)))
1577 PL_reg_flags |= RF_tainted;
1579 while (s + (uskip = UTF8SKIP(s)) <= strend) {
1580 if (!isDIGIT_LC_utf8((U8*)s)) {
1581 if (tmp && (norun || regtry(prog, s)))
1592 while (s < strend) {
1593 if (!isDIGIT_LC(*s)) {
1594 if (tmp && (norun || regtry(prog, s)))
1606 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1615 - regexec_flags - match a regexp against a string
1618 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1619 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1620 /* strend: pointer to null at end of string */
1621 /* strbeg: real beginning of string */
1622 /* minend: end of match must be >=minend after stringarg. */
1623 /* data: May be used for some additional optimizations. */
1624 /* nosave: For optimizations. */
1627 register regnode *c;
1628 register char *startpos = stringarg;
1629 I32 minlen; /* must match at least this many chars */
1630 I32 dontbother = 0; /* how many characters not to try at end */
1631 I32 end_shift = 0; /* Same for the end. */ /* CC */
1632 I32 scream_pos = -1; /* Internal iterator of scream. */
1634 SV* oreplsv = GvSV(PL_replgv);
1635 const bool do_utf8 = DO_UTF8(sv);
1637 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1638 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1640 (void)data; /* Currently unused */
1641 RX_MATCH_UTF8_set(prog,do_utf8);
1647 PL_regnarrate = DEBUG_r_TEST;
1650 /* Be paranoid... */
1651 if (prog == NULL || startpos == NULL) {
1652 Perl_croak(aTHX_ "NULL regexp parameter");
1656 minlen = prog->minlen;
1657 if (strend - startpos < minlen) {
1658 DEBUG_r(PerlIO_printf(Perl_debug_log,
1659 "String too short [regexec_flags]...\n"));
1663 /* Check validity of program. */
1664 if (UCHARAT(prog->program) != REG_MAGIC) {
1665 Perl_croak(aTHX_ "corrupted regexp program");
1669 PL_reg_eval_set = 0;
1672 if (prog->reganch & ROPT_UTF8)
1673 PL_reg_flags |= RF_utf8;
1675 /* Mark beginning of line for ^ and lookbehind. */
1676 PL_regbol = startpos;
1680 /* Mark end of line for $ (and such) */
1683 /* see how far we have to get to not match where we matched before */
1684 PL_regtill = startpos+minend;
1686 /* We start without call_cc context. */
1689 /* If there is a "must appear" string, look for it. */
1692 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1695 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1696 PL_reg_ganch = startpos;
1697 else if (sv && SvTYPE(sv) >= SVt_PVMG
1699 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1700 && mg->mg_len >= 0) {
1701 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1702 if (prog->reganch & ROPT_ANCH_GPOS) {
1703 if (s > PL_reg_ganch)
1708 else /* pos() not defined */
1709 PL_reg_ganch = strbeg;
1712 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1713 re_scream_pos_data d;
1715 d.scream_olds = &scream_olds;
1716 d.scream_pos = &scream_pos;
1717 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1719 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1720 goto phooey; /* not present */
1725 const char * const s0 = UTF
1726 ? pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1729 const int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1730 const char * const s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1731 UNI_DISPLAY_REGEX) : startpos;
1732 const int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1735 PerlIO_printf(Perl_debug_log,
1736 "%sMatching REx%s \"%s%*.*s%s%s\" against \"%s%.*s%s%s\"\n",
1737 PL_colors[4],PL_colors[5],PL_colors[0],
1740 len0 > 60 ? "..." : "",
1742 (int)(len1 > 60 ? 60 : len1),
1744 (len1 > 60 ? "..." : "")
1748 /* Simplest case: anchored match need be tried only once. */
1749 /* [unless only anchor is BOL and multiline is set] */
1750 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1751 if (s == startpos && regtry(prog, startpos))
1753 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1754 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1759 dontbother = minlen - 1;
1760 end = HOP3c(strend, -dontbother, strbeg) - 1;
1761 /* for multiline we only have to try after newlines */
1762 if (prog->check_substr || prog->check_utf8) {
1766 if (regtry(prog, s))
1771 if (prog->reganch & RE_USE_INTUIT) {
1772 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1783 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1784 if (regtry(prog, s))
1791 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1792 if (regtry(prog, PL_reg_ganch))
1797 /* Messy cases: unanchored match. */
1798 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1799 /* we have /x+whatever/ */
1800 /* it must be a one character string (XXXX Except UTF?) */
1805 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1806 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1807 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1810 while (s < strend) {
1812 DEBUG_r( did_match = 1 );
1813 if (regtry(prog, s)) goto got_it;
1815 while (s < strend && *s == ch)
1822 while (s < strend) {
1824 DEBUG_r( did_match = 1 );
1825 if (regtry(prog, s)) goto got_it;
1827 while (s < strend && *s == ch)
1833 DEBUG_r(if (!did_match)
1834 PerlIO_printf(Perl_debug_log,
1835 "Did not find anchored character...\n")
1839 else if (prog->anchored_substr != Nullsv
1840 || prog->anchored_utf8 != Nullsv
1841 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1842 && prog->float_max_offset < strend - s)) {
1847 char *last1; /* Last position checked before */
1851 if (prog->anchored_substr || prog->anchored_utf8) {
1852 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1853 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1854 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1855 back_max = back_min = prog->anchored_offset;
1857 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1858 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1859 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1860 back_max = prog->float_max_offset;
1861 back_min = prog->float_min_offset;
1863 if (must == &PL_sv_undef)
1864 /* could not downgrade utf8 check substring, so must fail */
1867 last = HOP3c(strend, /* Cannot start after this */
1868 -(I32)(CHR_SVLEN(must)
1869 - (SvTAIL(must) != 0) + back_min), strbeg);
1872 last1 = HOPc(s, -1);
1874 last1 = s - 1; /* bogus */
1876 /* XXXX check_substr already used to find "s", can optimize if
1877 check_substr==must. */
1879 dontbother = end_shift;
1880 strend = HOPc(strend, -dontbother);
1881 while ( (s <= last) &&
1882 ((flags & REXEC_SCREAM)
1883 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1884 end_shift, &scream_pos, 0))
1885 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1886 (unsigned char*)strend, must,
1887 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1888 /* we may be pointing at the wrong string */
1889 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1890 s = strbeg + (s - SvPVX_const(sv));
1891 DEBUG_r( did_match = 1 );
1892 if (HOPc(s, -back_max) > last1) {
1893 last1 = HOPc(s, -back_min);
1894 s = HOPc(s, -back_max);
1897 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1899 last1 = HOPc(s, -back_min);
1903 while (s <= last1) {
1904 if (regtry(prog, s))
1910 while (s <= last1) {
1911 if (regtry(prog, s))
1917 DEBUG_r(if (!did_match)
1918 PerlIO_printf(Perl_debug_log,
1919 "Did not find %s substr \"%s%.*s%s\"%s...\n",
1920 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1921 ? "anchored" : "floating"),
1923 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1925 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1929 else if ((c = prog->regstclass)) {
1931 I32 op = (U8)OP(prog->regstclass);
1932 /* don't bother with what can't match */
1933 if (PL_regkind[op] != EXACT && op != CANY)
1934 strend = HOPc(strend, -(minlen - 1));
1937 SV *prop = sv_newmortal();
1945 pv_uni_display(dsv0, (U8*)SvPVX_const(prop), SvCUR(prop), 60,
1946 UNI_DISPLAY_REGEX) :
1948 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1950 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1951 len1 = UTF ? SvCUR(dsv1) : strend - s;
1952 PerlIO_printf(Perl_debug_log,
1953 "Matching stclass \"%*.*s\" against \"%*.*s\"\n",
1957 if (find_byclass(prog, c, s, strend, 0))
1959 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1963 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1968 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1969 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1970 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1972 if (flags & REXEC_SCREAM) {
1973 last = screaminstr(sv, float_real, s - strbeg,
1974 end_shift, &scream_pos, 1); /* last one */
1976 last = scream_olds; /* Only one occurrence. */
1977 /* we may be pointing at the wrong string */
1978 else if (RX_MATCH_COPIED(prog))
1979 s = strbeg + (s - SvPVX_const(sv));
1983 const char * const little = SvPV(float_real, len);
1985 if (SvTAIL(float_real)) {
1986 if (memEQ(strend - len + 1, little, len - 1))
1987 last = strend - len + 1;
1988 else if (!PL_multiline)
1989 last = memEQ(strend - len, little, len)
1990 ? strend - len : Nullch;
1996 last = rninstr(s, strend, little, little + len);
1998 last = strend; /* matching "$" */
2002 DEBUG_r(PerlIO_printf(Perl_debug_log,
2003 "%sCan't trim the tail, match fails (should not happen)%s\n",
2004 PL_colors[4],PL_colors[5]));
2005 goto phooey; /* Should not happen! */
2007 dontbother = strend - last + prog->float_min_offset;
2009 if (minlen && (dontbother < minlen))
2010 dontbother = minlen - 1;
2011 strend -= dontbother; /* this one's always in bytes! */
2012 /* We don't know much -- general case. */
2015 if (regtry(prog, s))
2024 if (regtry(prog, s))
2026 } while (s++ < strend);
2034 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2036 if (PL_reg_eval_set) {
2037 /* Preserve the current value of $^R */
2038 if (oreplsv != GvSV(PL_replgv))
2039 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2040 restored, the value remains
2042 restore_pos(aTHX_ 0);
2045 /* make sure $`, $&, $', and $digit will work later */
2046 if ( !(flags & REXEC_NOT_FIRST) ) {
2047 if (RX_MATCH_COPIED(prog)) {
2048 Safefree(prog->subbeg);
2049 RX_MATCH_COPIED_off(prog);
2051 if (flags & REXEC_COPY_STR) {
2052 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2054 s = savepvn(strbeg, i);
2057 RX_MATCH_COPIED_on(prog);
2060 prog->subbeg = strbeg;
2061 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2068 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2069 PL_colors[4],PL_colors[5]));
2070 if (PL_reg_eval_set)
2071 restore_pos(aTHX_ 0);
2076 - regtry - try match at specific point
2078 STATIC I32 /* 0 failure, 1 success */
2079 S_regtry(pTHX_ regexp *prog, char *startpos)
2087 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2089 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2092 PL_reg_eval_set = RS_init;
2094 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2095 (IV)(PL_stack_sp - PL_stack_base));
2097 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2098 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2099 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2101 /* Apparently this is not needed, judging by wantarray. */
2102 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2103 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2106 /* Make $_ available to executed code. */
2107 if (PL_reg_sv != DEFSV) {
2108 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2113 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2114 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2115 /* prepare for quick setting of pos */
2116 sv_magic(PL_reg_sv, (SV*)0,
2117 PERL_MAGIC_regex_global, Nullch, 0);
2118 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2122 PL_reg_oldpos = mg->mg_len;
2123 SAVEDESTRUCTOR_X(restore_pos, 0);
2125 if (!PL_reg_curpm) {
2126 Newz(22,PL_reg_curpm, 1, PMOP);
2129 SV* repointer = newSViv(0);
2130 /* so we know which PL_regex_padav element is PL_reg_curpm */
2131 SvFLAGS(repointer) |= SVf_BREAK;
2132 av_push(PL_regex_padav,repointer);
2133 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2134 PL_regex_pad = AvARRAY(PL_regex_padav);
2138 PM_SETRE(PL_reg_curpm, prog);
2139 PL_reg_oldcurpm = PL_curpm;
2140 PL_curpm = PL_reg_curpm;
2141 if (RX_MATCH_COPIED(prog)) {
2142 /* Here is a serious problem: we cannot rewrite subbeg,
2143 since it may be needed if this match fails. Thus
2144 $` inside (?{}) could fail... */
2145 PL_reg_oldsaved = prog->subbeg;
2146 PL_reg_oldsavedlen = prog->sublen;
2147 RX_MATCH_COPIED_off(prog);
2150 PL_reg_oldsaved = Nullch;
2151 prog->subbeg = PL_bostr;
2152 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2154 prog->startp[0] = startpos - PL_bostr;
2155 PL_reginput = startpos;
2156 PL_regstartp = prog->startp;
2157 PL_regendp = prog->endp;
2158 PL_reglastparen = &prog->lastparen;
2159 PL_reglastcloseparen = &prog->lastcloseparen;
2160 prog->lastparen = 0;
2161 prog->lastcloseparen = 0;
2163 DEBUG_r(PL_reg_starttry = startpos);
2164 if (PL_reg_start_tmpl <= prog->nparens) {
2165 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2166 if(PL_reg_start_tmp)
2167 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2169 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2172 /* XXXX What this code is doing here?!!! There should be no need
2173 to do this again and again, PL_reglastparen should take care of
2176 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2177 * Actually, the code in regcppop() (which Ilya may be meaning by
2178 * PL_reglastparen), is not needed at all by the test suite
2179 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2180 * enough, for building DynaLoader, or otherwise this
2181 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2182 * will happen. Meanwhile, this code *is* needed for the
2183 * above-mentioned test suite tests to succeed. The common theme
2184 * on those tests seems to be returning null fields from matches.
2189 if (prog->nparens) {
2190 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2197 if (regmatch(prog->program + 1)) {
2198 prog->endp[0] = PL_reginput - PL_bostr;
2201 REGCP_UNWIND(lastcp);
2205 #define RE_UNWIND_BRANCH 1
2206 #define RE_UNWIND_BRANCHJ 2
2210 typedef struct { /* XX: makes sense to enlarge it... */
2214 } re_unwind_generic_t;
2227 } re_unwind_branch_t;
2229 typedef union re_unwind_t {
2231 re_unwind_generic_t generic;
2232 re_unwind_branch_t branch;
2235 #define sayYES goto yes
2236 #define sayNO goto no
2237 #define sayNO_ANYOF goto no_anyof
2238 #define sayYES_FINAL goto yes_final
2239 #define sayYES_LOUD goto yes_loud
2240 #define sayNO_FINAL goto no_final
2241 #define sayNO_SILENT goto do_no
2242 #define saySAME(x) if (x) goto yes; else goto no
2244 #define POSCACHE_SUCCESS 0 /* caching success rather than failure */
2245 #define POSCACHE_SEEN 1 /* we know what we're caching */
2246 #define POSCACHE_START 2 /* the real cache: this bit maps to pos 0 */
2247 #define CACHEsayYES STMT_START { \
2248 if (cache_offset | cache_bit) { \
2249 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) \
2250 PL_reg_poscache[0] |= (1<<POSCACHE_SUCCESS) || (1<<POSCACHE_SEEN); \
2251 else if (!(PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2252 /* cache records failure, but this is success */ \
2254 PerlIO_printf(Perl_debug_log, \
2255 "%*s (remove success from failure cache)\n", \
2256 REPORT_CODE_OFF+PL_regindent*2, "") \
2258 PL_reg_poscache[cache_offset] &= ~(1<<cache_bit); \
2263 #define CACHEsayNO STMT_START { \
2264 if (cache_offset | cache_bit) { \
2265 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) \
2266 PL_reg_poscache[0] |= (1<<POSCACHE_SEEN); \
2267 else if ((PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2268 /* cache records success, but this is failure */ \
2270 PerlIO_printf(Perl_debug_log, \
2271 "%*s (remove failure from success cache)\n", \
2272 REPORT_CODE_OFF+PL_regindent*2, "") \
2274 PL_reg_poscache[cache_offset] &= ~(1<<cache_bit); \
2280 #define REPORT_CODE_OFF 24
2283 - regmatch - main matching routine
2285 * Conceptually the strategy is simple: check to see whether the current
2286 * node matches, call self recursively to see whether the rest matches,
2287 * and then act accordingly. In practice we make some effort to avoid
2288 * recursion, in particular by going through "ordinary" nodes (that don't
2289 * need to know whether the rest of the match failed) by a loop instead of
2292 /* [lwall] I've hoisted the register declarations to the outer block in order to
2293 * maybe save a little bit of pushing and popping on the stack. It also takes
2294 * advantage of machines that use a register save mask on subroutine entry.
2296 STATIC I32 /* 0 failure, 1 success */
2297 S_regmatch(pTHX_ regnode *prog)
2299 register regnode *scan; /* Current node. */
2300 regnode *next; /* Next node. */
2301 regnode *inner; /* Next node in internal branch. */
2302 register I32 nextchr; /* renamed nextchr - nextchar colides with
2303 function of same name */
2304 register I32 n; /* no or next */
2305 register I32 ln = 0; /* len or last */
2306 register char *s = Nullch; /* operand or save */
2307 register char *locinput = PL_reginput;
2308 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2309 int minmod = 0, sw = 0, logical = 0;
2312 I32 firstcp = PL_savestack_ix;
2314 const register bool do_utf8 = PL_reg_match_utf8;
2316 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2317 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2318 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2325 /* Note that nextchr is a byte even in UTF */
2326 nextchr = UCHARAT(locinput);
2328 while (scan != NULL) {
2331 SV *prop = sv_newmortal();
2332 const int docolor = *PL_colors[0];
2333 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2334 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2335 /* The part of the string before starttry has one color
2336 (pref0_len chars), between starttry and current
2337 position another one (pref_len - pref0_len chars),
2338 after the current position the third one.
2339 We assume that pref0_len <= pref_len, otherwise we
2340 decrease pref0_len. */
2341 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2342 ? (5 + taill) - l : locinput - PL_bostr;
2345 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2347 pref0_len = pref_len - (locinput - PL_reg_starttry);
2348 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2349 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2350 ? (5 + taill) - pref_len : PL_regeol - locinput);
2351 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2355 if (pref0_len > pref_len)
2356 pref0_len = pref_len;
2357 regprop(prop, scan);
2359 const char * const s0 =
2360 do_utf8 && OP(scan) != CANY ?
2361 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2362 pref0_len, 60, UNI_DISPLAY_REGEX) :
2363 locinput - pref_len;
2364 const int len0 = do_utf8 ? strlen(s0) : pref0_len;
2365 const char * const s1 = do_utf8 && OP(scan) != CANY ?
2366 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2367 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2368 locinput - pref_len + pref0_len;
2369 const int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2370 const char * const s2 = do_utf8 && OP(scan) != CANY ?
2371 pv_uni_display(dsv2, (U8*)locinput,
2372 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2374 const int len2 = do_utf8 ? strlen(s2) : l;
2375 PerlIO_printf(Perl_debug_log,
2376 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2377 (IV)(locinput - PL_bostr),
2384 (docolor ? "" : "> <"),
2388 15 - l - pref_len + 1,
2390 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2395 next = scan + NEXT_OFF(scan);
2401 if (locinput == PL_bostr || (PL_multiline &&
2402 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2404 /* regtill = regbol; */
2409 if (locinput == PL_bostr ||
2410 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2416 if (locinput == PL_bostr)
2420 if (locinput == PL_reg_ganch)
2430 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2435 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2437 if (PL_regeol - locinput > 1)
2441 if (PL_regeol != locinput)
2445 if (!nextchr && locinput >= PL_regeol)
2448 locinput += PL_utf8skip[nextchr];
2449 if (locinput > PL_regeol)
2451 nextchr = UCHARAT(locinput);
2454 nextchr = UCHARAT(++locinput);
2457 if (!nextchr && locinput >= PL_regeol)
2459 nextchr = UCHARAT(++locinput);
2462 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2465 locinput += PL_utf8skip[nextchr];
2466 if (locinput > PL_regeol)
2468 nextchr = UCHARAT(locinput);
2471 nextchr = UCHARAT(++locinput);
2477 if (do_utf8 != UTF) {
2478 /* The target and the pattern have differing utf8ness. */
2480 const char *e = s + ln;
2483 /* The target is utf8, the pattern is not utf8. */
2488 if (NATIVE_TO_UNI(*(U8*)s) !=
2489 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
2491 0 : UTF8_ALLOW_ANY))
2498 /* The target is not utf8, the pattern is utf8. */
2503 if (NATIVE_TO_UNI(*((U8*)l)) !=
2504 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
2506 0 : UTF8_ALLOW_ANY))
2513 nextchr = UCHARAT(locinput);
2516 /* The target and the pattern have the same utf8ness. */
2517 /* Inline the first character, for speed. */
2518 if (UCHARAT(s) != nextchr)
2520 if (PL_regeol - locinput < ln)
2522 if (ln > 1 && memNE(s, locinput, ln))
2525 nextchr = UCHARAT(locinput);
2528 PL_reg_flags |= RF_tainted;
2534 if (do_utf8 || UTF) {
2535 /* Either target or the pattern are utf8. */
2537 char *e = PL_regeol;
2539 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2540 l, &e, 0, do_utf8)) {
2541 /* One more case for the sharp s:
2542 * pack("U0U*", 0xDF) =~ /ss/i,
2543 * the 0xC3 0x9F are the UTF-8
2544 * byte sequence for the U+00DF. */
2546 toLOWER(s[0]) == 's' &&
2548 toLOWER(s[1]) == 's' &&
2555 nextchr = UCHARAT(locinput);
2559 /* Neither the target and the pattern are utf8. */
2561 /* Inline the first character, for speed. */
2562 if (UCHARAT(s) != nextchr &&
2563 UCHARAT(s) != ((OP(scan) == EXACTF)
2564 ? PL_fold : PL_fold_locale)[nextchr])
2566 if (PL_regeol - locinput < ln)
2568 if (ln > 1 && (OP(scan) == EXACTF
2569 ? ibcmp(s, locinput, ln)
2570 : ibcmp_locale(s, locinput, ln)))
2573 nextchr = UCHARAT(locinput);
2577 STRLEN inclasslen = PL_regeol - locinput;
2579 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2581 if (locinput >= PL_regeol)
2583 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2584 nextchr = UCHARAT(locinput);
2589 nextchr = UCHARAT(locinput);
2590 if (!REGINCLASS(scan, (U8*)locinput))
2592 if (!nextchr && locinput >= PL_regeol)
2594 nextchr = UCHARAT(++locinput);
2598 /* If we might have the case of the German sharp s
2599 * in a casefolding Unicode character class. */
2601 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2602 locinput += SHARP_S_SKIP;
2603 nextchr = UCHARAT(locinput);
2609 PL_reg_flags |= RF_tainted;
2615 LOAD_UTF8_CHARCLASS(alnum,"a");
2616 if (!(OP(scan) == ALNUM
2617 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2618 : isALNUM_LC_utf8((U8*)locinput)))
2622 locinput += PL_utf8skip[nextchr];
2623 nextchr = UCHARAT(locinput);
2626 if (!(OP(scan) == ALNUM
2627 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2629 nextchr = UCHARAT(++locinput);
2632 PL_reg_flags |= RF_tainted;
2635 if (!nextchr && locinput >= PL_regeol)
2638 LOAD_UTF8_CHARCLASS(alnum,"a");
2639 if (OP(scan) == NALNUM
2640 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2641 : isALNUM_LC_utf8((U8*)locinput))
2645 locinput += PL_utf8skip[nextchr];
2646 nextchr = UCHARAT(locinput);
2649 if (OP(scan) == NALNUM
2650 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2652 nextchr = UCHARAT(++locinput);
2656 PL_reg_flags |= RF_tainted;
2660 /* was last char in word? */
2662 if (locinput == PL_bostr)
2665 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2667 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2669 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2670 ln = isALNUM_uni(ln);
2671 LOAD_UTF8_CHARCLASS(alnum,"a");
2672 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2675 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2676 n = isALNUM_LC_utf8((U8*)locinput);
2680 ln = (locinput != PL_bostr) ?
2681 UCHARAT(locinput - 1) : '\n';
2682 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2684 n = isALNUM(nextchr);
2687 ln = isALNUM_LC(ln);
2688 n = isALNUM_LC(nextchr);
2691 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2692 OP(scan) == BOUNDL))
2696 PL_reg_flags |= RF_tainted;
2702 if (UTF8_IS_CONTINUED(nextchr)) {
2703 LOAD_UTF8_CHARCLASS(space," ");
2704 if (!(OP(scan) == SPACE
2705 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2706 : isSPACE_LC_utf8((U8*)locinput)))
2710 locinput += PL_utf8skip[nextchr];
2711 nextchr = UCHARAT(locinput);
2714 if (!(OP(scan) == SPACE
2715 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2717 nextchr = UCHARAT(++locinput);
2720 if (!(OP(scan) == SPACE
2721 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2723 nextchr = UCHARAT(++locinput);
2727 PL_reg_flags |= RF_tainted;
2730 if (!nextchr && locinput >= PL_regeol)
2733 LOAD_UTF8_CHARCLASS(space," ");
2734 if (OP(scan) == NSPACE
2735 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2736 : isSPACE_LC_utf8((U8*)locinput))
2740 locinput += PL_utf8skip[nextchr];
2741 nextchr = UCHARAT(locinput);
2744 if (OP(scan) == NSPACE
2745 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2747 nextchr = UCHARAT(++locinput);
2750 PL_reg_flags |= RF_tainted;
2756 LOAD_UTF8_CHARCLASS(digit,"0");
2757 if (!(OP(scan) == DIGIT
2758 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2759 : isDIGIT_LC_utf8((U8*)locinput)))
2763 locinput += PL_utf8skip[nextchr];
2764 nextchr = UCHARAT(locinput);
2767 if (!(OP(scan) == DIGIT
2768 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2770 nextchr = UCHARAT(++locinput);
2773 PL_reg_flags |= RF_tainted;
2776 if (!nextchr && locinput >= PL_regeol)
2779 LOAD_UTF8_CHARCLASS(digit,"0");
2780 if (OP(scan) == NDIGIT
2781 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2782 : isDIGIT_LC_utf8((U8*)locinput))
2786 locinput += PL_utf8skip[nextchr];
2787 nextchr = UCHARAT(locinput);
2790 if (OP(scan) == NDIGIT
2791 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2793 nextchr = UCHARAT(++locinput);
2796 if (locinput >= PL_regeol)
2799 LOAD_UTF8_CHARCLASS(mark,"~");
2800 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2802 locinput += PL_utf8skip[nextchr];
2803 while (locinput < PL_regeol &&
2804 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2805 locinput += UTF8SKIP(locinput);
2806 if (locinput > PL_regeol)
2811 nextchr = UCHARAT(locinput);
2814 PL_reg_flags |= RF_tainted;
2818 n = ARG(scan); /* which paren pair */
2819 ln = PL_regstartp[n];
2820 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2821 if ((I32)*PL_reglastparen < n || ln == -1)
2822 sayNO; /* Do not match unless seen CLOSEn. */
2823 if (ln == PL_regendp[n])
2827 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2829 const char *e = PL_bostr + PL_regendp[n];
2831 * Note that we can't do the "other character" lookup trick as
2832 * in the 8-bit case (no pun intended) because in Unicode we
2833 * have to map both upper and title case to lower case.
2835 if (OP(scan) == REFF) {
2837 STRLEN ulen1, ulen2;
2838 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
2839 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
2843 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2844 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2845 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2852 nextchr = UCHARAT(locinput);
2856 /* Inline the first character, for speed. */
2857 if (UCHARAT(s) != nextchr &&
2859 (UCHARAT(s) != ((OP(scan) == REFF
2860 ? PL_fold : PL_fold_locale)[nextchr]))))
2862 ln = PL_regendp[n] - ln;
2863 if (locinput + ln > PL_regeol)
2865 if (ln > 1 && (OP(scan) == REF
2866 ? memNE(s, locinput, ln)
2868 ? ibcmp(s, locinput, ln)
2869 : ibcmp_locale(s, locinput, ln))))
2872 nextchr = UCHARAT(locinput);
2883 OP_4tree *oop = PL_op;
2884 COP *ocurcop = PL_curcop;
2887 struct regexp *oreg = PL_reg_re;
2890 PL_op = (OP_4tree*)PL_regdata->data[n];
2891 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2892 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2893 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2897 CALLRUNOPS(aTHX); /* Scalar context. */
2900 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2908 PAD_RESTORE_LOCAL(old_comppad);
2909 PL_curcop = ocurcop;
2911 if (logical == 2) { /* Postponed subexpression. */
2913 MAGIC *mg = Null(MAGIC*);
2915 CHECKPOINT cp, lastcp;
2919 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
2920 mg = mg_find(sv, PERL_MAGIC_qr);
2921 else if (SvSMAGICAL(ret)) {
2922 if (SvGMAGICAL(ret))
2923 sv_unmagic(ret, PERL_MAGIC_qr);
2925 mg = mg_find(ret, PERL_MAGIC_qr);
2929 re = (regexp *)mg->mg_obj;
2930 (void)ReREFCNT_inc(re);
2934 char *t = SvPV(ret, len);
2936 char * const oprecomp = PL_regprecomp;
2937 const I32 osize = PL_regsize;
2938 const I32 onpar = PL_regnpar;
2941 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2942 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2944 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
2946 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2948 PL_regprecomp = oprecomp;
2953 PerlIO_printf(Perl_debug_log,
2954 "Entering embedded \"%s%.60s%s%s\"\n",
2958 (strlen(re->precomp) > 60 ? "..." : ""))
2961 state.prev = PL_reg_call_cc;
2962 state.cc = PL_regcc;
2963 state.re = PL_reg_re;
2967 cp = regcppush(0); /* Save *all* the positions. */
2970 state.ss = PL_savestack_ix;
2971 *PL_reglastparen = 0;
2972 *PL_reglastcloseparen = 0;
2973 PL_reg_call_cc = &state;
2974 PL_reginput = locinput;
2975 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2976 ((re->reganch & ROPT_UTF8) != 0);
2977 if (toggleutf) PL_reg_flags ^= RF_utf8;
2979 /* XXXX This is too dramatic a measure... */
2982 if (regmatch(re->program + 1)) {
2983 /* Even though we succeeded, we need to restore
2984 global variables, since we may be wrapped inside
2985 SUSPEND, thus the match may be not finished yet. */
2987 /* XXXX Do this only if SUSPENDed? */
2988 PL_reg_call_cc = state.prev;
2989 PL_regcc = state.cc;
2990 PL_reg_re = state.re;
2991 cache_re(PL_reg_re);
2992 if (toggleutf) PL_reg_flags ^= RF_utf8;
2994 /* XXXX This is too dramatic a measure... */
2997 /* These are needed even if not SUSPEND. */
3003 REGCP_UNWIND(lastcp);
3005 PL_reg_call_cc = state.prev;
3006 PL_regcc = state.cc;
3007 PL_reg_re = state.re;
3008 cache_re(PL_reg_re);
3009 if (toggleutf) PL_reg_flags ^= RF_utf8;
3011 /* XXXX This is too dramatic a measure... */
3021 sv_setsv(save_scalar(PL_replgv), ret);
3027 n = ARG(scan); /* which paren pair */
3028 PL_reg_start_tmp[n] = locinput;
3033 n = ARG(scan); /* which paren pair */
3034 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3035 PL_regendp[n] = locinput - PL_bostr;
3036 if (n > (I32)*PL_reglastparen)
3037 *PL_reglastparen = n;
3038 *PL_reglastcloseparen = n;
3041 n = ARG(scan); /* which paren pair */
3042 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
3045 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3047 next = NEXTOPER(NEXTOPER(scan));
3049 next = scan + ARG(scan);
3050 if (OP(next) == IFTHEN) /* Fake one. */
3051 next = NEXTOPER(NEXTOPER(next));
3055 logical = scan->flags;
3057 /*******************************************************************
3058 PL_regcc contains infoblock about the innermost (...)* loop, and
3059 a pointer to the next outer infoblock.
3061 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3063 1) After matching X, regnode for CURLYX is processed;
3065 2) This regnode creates infoblock on the stack, and calls
3066 regmatch() recursively with the starting point at WHILEM node;
3068 3) Each hit of WHILEM node tries to match A and Z (in the order
3069 depending on the current iteration, min/max of {min,max} and
3070 greediness). The information about where are nodes for "A"
3071 and "Z" is read from the infoblock, as is info on how many times "A"
3072 was already matched, and greediness.
3074 4) After A matches, the same WHILEM node is hit again.
3076 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3077 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3078 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3079 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3080 of the external loop.
3082 Currently present infoblocks form a tree with a stem formed by PL_curcc
3083 and whatever it mentions via ->next, and additional attached trees
3084 corresponding to temporarily unset infoblocks as in "5" above.
3086 In the following picture infoblocks for outer loop of
3087 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3088 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3089 infoblocks are drawn below the "reset" infoblock.
3091 In fact in the picture below we do not show failed matches for Z and T
3092 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3093 more obvious *why* one needs to *temporary* unset infoblocks.]
3095 Matched REx position InfoBlocks Comment
3099 Y A)*?Z)*?T x <- O <- I
3100 YA )*?Z)*?T x <- O <- I
3101 YA A)*?Z)*?T x <- O <- I
3102 YAA )*?Z)*?T x <- O <- I
3103 YAA Z)*?T x <- O # Temporary unset I
3106 YAAZ Y(A)*?Z)*?T x <- O
3109 YAAZY (A)*?Z)*?T x <- O
3112 YAAZY A)*?Z)*?T x <- O <- I
3115 YAAZYA )*?Z)*?T x <- O <- I
3118 YAAZYA Z)*?T x <- O # Temporary unset I
3124 YAAZYAZ T x # Temporary unset O
3131 *******************************************************************/
3134 CHECKPOINT cp = PL_savestack_ix;
3135 /* No need to save/restore up to this paren */
3136 I32 parenfloor = scan->flags;
3138 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3140 cc.oldcc = PL_regcc;
3142 /* XXXX Probably it is better to teach regpush to support
3143 parenfloor > PL_regsize... */
3144 if (parenfloor > (I32)*PL_reglastparen)
3145 parenfloor = *PL_reglastparen; /* Pessimization... */
3146 cc.parenfloor = parenfloor;
3148 cc.min = ARG1(scan);
3149 cc.max = ARG2(scan);
3150 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3154 PL_reginput = locinput;
3155 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3157 PL_regcc = cc.oldcc;
3163 * This is really hard to understand, because after we match
3164 * what we're trying to match, we must make sure the rest of
3165 * the REx is going to match for sure, and to do that we have
3166 * to go back UP the parse tree by recursing ever deeper. And
3167 * if it fails, we have to reset our parent's current state
3168 * that we can try again after backing off.
3171 CHECKPOINT cp, lastcp;
3172 CURCUR* cc = PL_regcc;
3173 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3174 I32 cache_offset = 0, cache_bit = 0;
3176 n = cc->cur + 1; /* how many we know we matched */
3177 PL_reginput = locinput;
3180 PerlIO_printf(Perl_debug_log,
3181 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3182 REPORT_CODE_OFF+PL_regindent*2, "",
3183 (long)n, (long)cc->min,
3184 (long)cc->max, PTR2UV(cc))
3187 /* If degenerate scan matches "", assume scan done. */
3189 if (locinput == cc->lastloc && n >= cc->min) {
3190 PL_regcc = cc->oldcc;
3194 PerlIO_printf(Perl_debug_log,
3195 "%*s empty match detected, try continuation...\n",
3196 REPORT_CODE_OFF+PL_regindent*2, "")
3198 if (regmatch(cc->next))
3206 /* First just match a string of min scans. */
3210 cc->lastloc = locinput;
3211 if (regmatch(cc->scan))
3214 cc->lastloc = lastloc;
3219 /* Check whether we already were at this position.
3220 Postpone detection until we know the match is not
3221 *that* much linear. */
3222 if (!PL_reg_maxiter) {
3223 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3224 PL_reg_leftiter = PL_reg_maxiter;
3226 if (PL_reg_leftiter-- == 0) {
3227 const I32 size = (PL_reg_maxiter + 7 + POSCACHE_START)/8;
3228 if (PL_reg_poscache) {
3229 if ((I32)PL_reg_poscache_size < size) {
3230 Renew(PL_reg_poscache, size, char);
3231 PL_reg_poscache_size = size;
3233 Zero(PL_reg_poscache, size, char);
3236 PL_reg_poscache_size = size;
3237 Newz(29, PL_reg_poscache, size, char);
3240 PerlIO_printf(Perl_debug_log,
3241 "%sDetected a super-linear match, switching on caching%s...\n",
3242 PL_colors[4], PL_colors[5])
3245 if (PL_reg_leftiter < 0) {
3246 cache_offset = locinput - PL_bostr;
3248 cache_offset = (scan->flags & 0xf) - 1 + POSCACHE_START
3249 + cache_offset * (scan->flags>>4);
3250 cache_bit = cache_offset % 8;
3252 if (PL_reg_poscache[cache_offset] & (1<<cache_bit)) {
3254 PerlIO_printf(Perl_debug_log,
3255 "%*s already tried at this position...\n",
3256 REPORT_CODE_OFF+PL_regindent*2, "")
3258 if (PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))
3259 /* cache records success */
3262 /* cache records failure */
3265 PL_reg_poscache[cache_offset] |= (1<<cache_bit);
3269 /* Prefer next over scan for minimal matching. */
3272 PL_regcc = cc->oldcc;
3275 cp = regcppush(cc->parenfloor);
3277 if (regmatch(cc->next)) {
3279 CACHEsayYES; /* All done. */
3281 REGCP_UNWIND(lastcp);
3287 if (n >= cc->max) { /* Maximum greed exceeded? */
3288 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3289 && !(PL_reg_flags & RF_warned)) {
3290 PL_reg_flags |= RF_warned;
3291 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3292 "Complex regular subexpression recursion",
3299 PerlIO_printf(Perl_debug_log,
3300 "%*s trying longer...\n",
3301 REPORT_CODE_OFF+PL_regindent*2, "")
3303 /* Try scanning more and see if it helps. */
3304 PL_reginput = locinput;
3306 cc->lastloc = locinput;
3307 cp = regcppush(cc->parenfloor);
3309 if (regmatch(cc->scan)) {
3313 REGCP_UNWIND(lastcp);
3316 cc->lastloc = lastloc;
3320 /* Prefer scan over next for maximal matching. */
3322 if (n < cc->max) { /* More greed allowed? */
3323 cp = regcppush(cc->parenfloor);
3325 cc->lastloc = locinput;
3327 if (regmatch(cc->scan)) {
3331 REGCP_UNWIND(lastcp);
3332 regcppop(); /* Restore some previous $<digit>s? */
3333 PL_reginput = locinput;
3335 PerlIO_printf(Perl_debug_log,
3336 "%*s failed, try continuation...\n",
3337 REPORT_CODE_OFF+PL_regindent*2, "")
3340 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3341 && !(PL_reg_flags & RF_warned)) {
3342 PL_reg_flags |= RF_warned;
3343 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3344 "Complex regular subexpression recursion",
3348 /* Failed deeper matches of scan, so see if this one works. */
3349 PL_regcc = cc->oldcc;
3352 if (regmatch(cc->next))
3358 cc->lastloc = lastloc;
3363 next = scan + ARG(scan);
3366 inner = NEXTOPER(NEXTOPER(scan));
3369 inner = NEXTOPER(scan);
3373 if (OP(next) != c1) /* No choice. */
3374 next = inner; /* Avoid recursion. */
3376 const I32 lastparen = *PL_reglastparen;
3378 re_unwind_branch_t *uw;
3380 /* Put unwinding data on stack */
3381 unwind1 = SSNEWt(1,re_unwind_branch_t);
3382 uw = SSPTRt(unwind1,re_unwind_branch_t);
3385 uw->type = ((c1 == BRANCH)
3387 : RE_UNWIND_BRANCHJ);
3388 uw->lastparen = lastparen;
3390 uw->locinput = locinput;
3391 uw->nextchr = nextchr;
3393 uw->regindent = ++PL_regindent;
3396 REGCP_SET(uw->lastcp);
3398 /* Now go into the first branch */
3411 /* We suppose that the next guy does not need
3412 backtracking: in particular, it is of constant non-zero length,
3413 and has no parenths to influence future backrefs. */
3414 ln = ARG1(scan); /* min to match */
3415 n = ARG2(scan); /* max to match */
3416 paren = scan->flags;
3418 if (paren > PL_regsize)
3420 if (paren > (I32)*PL_reglastparen)
3421 *PL_reglastparen = paren;
3423 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3425 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3426 PL_reginput = locinput;
3429 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3431 locinput = PL_reginput;
3432 if (HAS_TEXT(next) || JUMPABLE(next)) {
3433 regnode *text_node = next;
3435 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3437 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3439 if (PL_regkind[(U8)OP(text_node)] == REF) {
3443 else { c1 = (U8)*STRING(text_node); }
3444 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3446 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3447 c2 = PL_fold_locale[c1];
3456 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3457 /* If it could work, try it. */
3459 UCHARAT(PL_reginput) == c1 ||
3460 UCHARAT(PL_reginput) == c2)
3464 PL_regstartp[paren] =
3465 HOPc(PL_reginput, -l) - PL_bostr;
3466 PL_regendp[paren] = PL_reginput - PL_bostr;
3469 PL_regendp[paren] = -1;
3473 REGCP_UNWIND(lastcp);
3475 /* Couldn't or didn't -- move forward. */
3476 PL_reginput = locinput;
3477 if (regrepeat_hard(scan, 1, &l)) {
3479 locinput = PL_reginput;
3486 n = regrepeat_hard(scan, n, &l);
3487 locinput = PL_reginput;
3489 PerlIO_printf(Perl_debug_log,
3490 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3491 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3495 if (HAS_TEXT(next) || JUMPABLE(next)) {
3496 regnode *text_node = next;
3498 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3500 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3502 if (PL_regkind[(U8)OP(text_node)] == REF) {
3506 else { c1 = (U8)*STRING(text_node); }
3508 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3510 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3511 c2 = PL_fold_locale[c1];
3522 /* If it could work, try it. */
3524 UCHARAT(PL_reginput) == c1 ||
3525 UCHARAT(PL_reginput) == c2)
3528 PerlIO_printf(Perl_debug_log,
3529 "%*s trying tail with n=%"IVdf"...\n",
3530 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3534 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3535 PL_regendp[paren] = PL_reginput - PL_bostr;
3538 PL_regendp[paren] = -1;
3542 REGCP_UNWIND(lastcp);
3544 /* Couldn't or didn't -- back up. */
3546 locinput = HOPc(locinput, -l);
3547 PL_reginput = locinput;
3554 paren = scan->flags; /* Which paren to set */
3555 if (paren > PL_regsize)
3557 if (paren > (I32)*PL_reglastparen)
3558 *PL_reglastparen = paren;
3559 ln = ARG1(scan); /* min to match */
3560 n = ARG2(scan); /* max to match */
3561 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3565 ln = ARG1(scan); /* min to match */
3566 n = ARG2(scan); /* max to match */
3567 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3572 scan = NEXTOPER(scan);
3578 scan = NEXTOPER(scan);
3582 * Lookahead to avoid useless match attempts
3583 * when we know what character comes next.
3587 * Used to only do .*x and .*?x, but now it allows
3588 * for )'s, ('s and (?{ ... })'s to be in the way
3589 * of the quantifier and the EXACT-like node. -- japhy
3592 if (HAS_TEXT(next) || JUMPABLE(next)) {
3594 regnode *text_node = next;
3596 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3598 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3600 if (PL_regkind[(U8)OP(text_node)] == REF) {
3602 goto assume_ok_easy;
3604 else { s = (U8*)STRING(text_node); }
3608 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3610 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3611 c2 = PL_fold_locale[c1];
3614 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3615 STRLEN ulen1, ulen2;
3616 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3617 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3619 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3620 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3622 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
3624 0 : UTF8_ALLOW_ANY);
3625 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
3627 0 : UTF8_ALLOW_ANY);
3630 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
3632 0 : UTF8_ALLOW_ANY);
3640 PL_reginput = locinput;
3644 if (ln && regrepeat(scan, ln) < ln)
3646 locinput = PL_reginput;
3649 char *e; /* Should not check after this */
3650 char *old = locinput;
3653 if (n == REG_INFTY) {
3656 while (UTF8_IS_CONTINUATION(*(U8*)e))
3662 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3666 e = locinput + n - ln;
3671 /* Find place 'next' could work */
3674 while (locinput <= e &&
3675 UCHARAT(locinput) != c1)
3678 while (locinput <= e
3679 && UCHARAT(locinput) != c1
3680 && UCHARAT(locinput) != c2)
3683 count = locinput - old;
3688 /* count initialised to
3689 * utf8_distance(old, locinput) */
3690 while (locinput <= e &&
3691 utf8n_to_uvchr((U8*)locinput,
3692 UTF8_MAXBYTES, &len,
3694 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3700 /* count initialised to
3701 * utf8_distance(old, locinput) */
3702 while (locinput <= e) {
3703 UV c = utf8n_to_uvchr((U8*)locinput,
3704 UTF8_MAXBYTES, &len,
3706 0 : UTF8_ALLOW_ANY);
3707 if (c == (UV)c1 || c == (UV)c2)
3716 /* PL_reginput == old now */
3717 if (locinput != old) {
3718 ln = 1; /* Did some */
3719 if (regrepeat(scan, count) < count)
3722 /* PL_reginput == locinput now */
3723 TRYPAREN(paren, ln, locinput);
3724 PL_reginput = locinput; /* Could be reset... */
3725 REGCP_UNWIND(lastcp);
3726 /* Couldn't or didn't -- move forward. */
3729 locinput += UTF8SKIP(locinput);
3736 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3740 c = utf8n_to_uvchr((U8*)PL_reginput,
3743 0 : UTF8_ALLOW_ANY);
3745 c = UCHARAT(PL_reginput);
3746 /* If it could work, try it. */
3747 if (c == (UV)c1 || c == (UV)c2)
3749 TRYPAREN(paren, ln, PL_reginput);
3750 REGCP_UNWIND(lastcp);
3753 /* If it could work, try it. */
3754 else if (c1 == -1000)
3756 TRYPAREN(paren, ln, PL_reginput);
3757 REGCP_UNWIND(lastcp);
3759 /* Couldn't or didn't -- move forward. */
3760 PL_reginput = locinput;
3761 if (regrepeat(scan, 1)) {
3763 locinput = PL_reginput;
3771 n = regrepeat(scan, n);
3772 locinput = PL_reginput;
3773 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3774 ((!PL_multiline && OP(next) != MEOL) ||
3775 OP(next) == SEOL || OP(next) == EOS))
3777 ln = n; /* why back off? */
3778 /* ...because $ and \Z can match before *and* after
3779 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3780 We should back off by one in this case. */
3781 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3790 c = utf8n_to_uvchr((U8*)PL_reginput,
3793 0 : UTF8_ALLOW_ANY);
3795 c = UCHARAT(PL_reginput);
3797 /* If it could work, try it. */
3798 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3800 TRYPAREN(paren, n, PL_reginput);
3801 REGCP_UNWIND(lastcp);
3803 /* Couldn't or didn't -- back up. */
3805 PL_reginput = locinput = HOPc(locinput, -1);
3813 c = utf8n_to_uvchr((U8*)PL_reginput,
3816 0 : UTF8_ALLOW_ANY);
3818 c = UCHARAT(PL_reginput);
3820 /* If it could work, try it. */
3821 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3823 TRYPAREN(paren, n, PL_reginput);
3824 REGCP_UNWIND(lastcp);
3826 /* Couldn't or didn't -- back up. */
3828 PL_reginput = locinput = HOPc(locinput, -1);
3835 if (PL_reg_call_cc) {
3836 re_cc_state *cur_call_cc = PL_reg_call_cc;
3837 CURCUR *cctmp = PL_regcc;
3838 regexp *re = PL_reg_re;
3839 CHECKPOINT cp, lastcp;
3841 cp = regcppush(0); /* Save *all* the positions. */
3843 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3845 PL_reginput = locinput; /* Make position available to
3847 cache_re(PL_reg_call_cc->re);
3848 PL_regcc = PL_reg_call_cc->cc;
3849 PL_reg_call_cc = PL_reg_call_cc->prev;
3850 if (regmatch(cur_call_cc->node)) {
3851 PL_reg_call_cc = cur_call_cc;
3855 REGCP_UNWIND(lastcp);
3857 PL_reg_call_cc = cur_call_cc;
3863 PerlIO_printf(Perl_debug_log,
3864 "%*s continuation failed...\n",
3865 REPORT_CODE_OFF+PL_regindent*2, "")
3869 if (locinput < PL_regtill) {
3870 DEBUG_r(PerlIO_printf(Perl_debug_log,
3871 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3873 (long)(locinput - PL_reg_starttry),
3874 (long)(PL_regtill - PL_reg_starttry),
3876 sayNO_FINAL; /* Cannot match: too short. */
3878 PL_reginput = locinput; /* put where regtry can find it */
3879 sayYES_FINAL; /* Success! */
3881 PL_reginput = locinput; /* put where regtry can find it */
3882 sayYES_LOUD; /* Success! */
3885 PL_reginput = locinput;
3890 s = HOPBACKc(locinput, scan->flags);
3896 PL_reginput = locinput;
3901 s = HOPBACKc(locinput, scan->flags);
3907 PL_reginput = locinput;
3910 inner = NEXTOPER(NEXTOPER(scan));
3911 if (regmatch(inner) != n) {
3926 if (OP(scan) == SUSPEND) {
3927 locinput = PL_reginput;
3928 nextchr = UCHARAT(locinput);
3933 next = scan + ARG(scan);
3938 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3939 PTR2UV(scan), OP(scan));
3940 Perl_croak(aTHX_ "regexp memory corruption");
3947 * We get here only if there's trouble -- normally "case END" is
3948 * the terminating point.
3950 Perl_croak(aTHX_ "corrupted regexp pointers");
3956 PerlIO_printf(Perl_debug_log,
3957 "%*s %scould match...%s\n",
3958 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3962 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3963 PL_colors[4],PL_colors[5]));
3969 #if 0 /* Breaks $^R */
3977 PerlIO_printf(Perl_debug_log,
3978 "%*s %sfailed...%s\n",
3979 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3985 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3988 case RE_UNWIND_BRANCH:
3989 case RE_UNWIND_BRANCHJ:
3991 re_unwind_branch_t *uwb = &(uw->branch);
3992 const I32 lastparen = uwb->lastparen;
3994 REGCP_UNWIND(uwb->lastcp);
3995 for (n = *PL_reglastparen; n > lastparen; n--)
3997 *PL_reglastparen = n;
3998 scan = next = uwb->next;
4000 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
4001 ? BRANCH : BRANCHJ) ) { /* Failure */
4008 /* Have more choice yet. Reuse the same uwb. */
4010 if ((n = (uwb->type == RE_UNWIND_BRANCH
4011 ? NEXT_OFF(next) : ARG(next))))
4014 next = NULL; /* XXXX Needn't unwinding in this case... */
4016 next = NEXTOPER(scan);
4017 if (uwb->type == RE_UNWIND_BRANCHJ)
4018 next = NEXTOPER(next);
4019 locinput = uwb->locinput;
4020 nextchr = uwb->nextchr;
4022 PL_regindent = uwb->regindent;
4029 Perl_croak(aTHX_ "regexp unwind memory corruption");
4040 - regrepeat - repeatedly match something simple, report how many
4043 * [This routine now assumes that it will only match on things of length 1.
4044 * That was true before, but now we assume scan - reginput is the count,
4045 * rather than incrementing count on every character. [Er, except utf8.]]
4048 S_regrepeat(pTHX_ const regnode *p, I32 max)
4050 register char *scan;
4052 register char *loceol = PL_regeol;
4053 register I32 hardcount = 0;
4054 register bool do_utf8 = PL_reg_match_utf8;
4057 if (max == REG_INFTY)
4059 else if (max < loceol - scan)
4060 loceol = scan + max;
4065 while (scan < loceol && hardcount < max && *scan != '\n') {
4066 scan += UTF8SKIP(scan);
4070 while (scan < loceol && *scan != '\n')
4077 while (scan < loceol && hardcount < max) {
4078 scan += UTF8SKIP(scan);
4088 case EXACT: /* length of string is 1 */
4090 while (scan < loceol && UCHARAT(scan) == c)
4093 case EXACTF: /* length of string is 1 */
4095 while (scan < loceol &&
4096 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4099 case EXACTFL: /* length of string is 1 */
4100 PL_reg_flags |= RF_tainted;
4102 while (scan < loceol &&
4103 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4109 while (hardcount < max && scan < loceol &&
4110 reginclass(p, (U8*)scan, 0, do_utf8)) {
4111 scan += UTF8SKIP(scan);
4115 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4122 LOAD_UTF8_CHARCLASS(alnum,"a");
4123 while (hardcount < max && scan < loceol &&
4124 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4125 scan += UTF8SKIP(scan);
4129 while (scan < loceol && isALNUM(*scan))
4134 PL_reg_flags |= RF_tainted;
4137 while (hardcount < max && scan < loceol &&
4138 isALNUM_LC_utf8((U8*)scan)) {
4139 scan += UTF8SKIP(scan);
4143 while (scan < loceol && isALNUM_LC(*scan))
4150 LOAD_UTF8_CHARCLASS(alnum,"a");
4151 while (hardcount < max && scan < loceol &&
4152 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4153 scan += UTF8SKIP(scan);
4157 while (scan < loceol && !isALNUM(*scan))
4162 PL_reg_flags |= RF_tainted;
4165 while (hardcount < max && scan < loceol &&
4166 !isALNUM_LC_utf8((U8*)scan)) {
4167 scan += UTF8SKIP(scan);
4171 while (scan < loceol && !isALNUM_LC(*scan))
4178 LOAD_UTF8_CHARCLASS(space," ");
4179 while (hardcount < max && scan < loceol &&
4181 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4182 scan += UTF8SKIP(scan);
4186 while (scan < loceol && isSPACE(*scan))
4191 PL_reg_flags |= RF_tainted;
4194 while (hardcount < max && scan < loceol &&
4195 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4196 scan += UTF8SKIP(scan);
4200 while (scan < loceol && isSPACE_LC(*scan))
4207 LOAD_UTF8_CHARCLASS(space," ");
4208 while (hardcount < max && scan < loceol &&
4210 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4211 scan += UTF8SKIP(scan);
4215 while (scan < loceol && !isSPACE(*scan))
4220 PL_reg_flags |= RF_tainted;
4223 while (hardcount < max && scan < loceol &&
4224 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4225 scan += UTF8SKIP(scan);
4229 while (scan < loceol && !isSPACE_LC(*scan))
4236 LOAD_UTF8_CHARCLASS(digit,"0");
4237 while (hardcount < max && scan < loceol &&
4238 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4239 scan += UTF8SKIP(scan);
4243 while (scan < loceol && isDIGIT(*scan))
4250 LOAD_UTF8_CHARCLASS(digit,"0");
4251 while (hardcount < max && scan < loceol &&
4252 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4253 scan += UTF8SKIP(scan);
4257 while (scan < loceol && !isDIGIT(*scan))
4261 default: /* Called on something of 0 width. */
4262 break; /* So match right here or not at all. */
4268 c = scan - PL_reginput;
4273 SV *prop = sv_newmortal();
4276 PerlIO_printf(Perl_debug_log,
4277 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4278 REPORT_CODE_OFF+1, "", SvPVX_const(prop),(IV)c,(IV)max);
4285 - regrepeat_hard - repeatedly match something, report total lenth and length
4287 * The repeater is supposed to have constant non-zero length.
4291 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4293 register char *scan = Nullch;
4294 register char *start;
4295 register char *loceol = PL_regeol;
4297 I32 count = 0, res = 1;
4302 start = PL_reginput;
4303 if (PL_reg_match_utf8) {
4304 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4307 while (start < PL_reginput) {
4309 start += UTF8SKIP(start);
4320 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4322 *lp = l = PL_reginput - start;
4323 if (max != REG_INFTY && l*max < loceol - scan)
4324 loceol = scan + l*max;
4337 - regclass_swash - prepare the utf8 swash
4341 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4347 if (PL_regdata && PL_regdata->count) {
4348 const U32 n = ARG(node);
4350 if (PL_regdata->what[n] == 's') {
4351 SV *rv = (SV*)PL_regdata->data[n];
4352 AV *av = (AV*)SvRV((SV*)rv);
4353 SV **ary = AvARRAY(av);
4356 /* See the end of regcomp.c:S_reglass() for
4357 * documentation of these array elements. */
4360 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4361 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4365 else if (si && doinit) {
4366 sw = swash_init("utf8", "", si, 1, 0);
4367 (void)av_store(av, 1, sw);
4383 - reginclass - determine if a character falls into a character class
4385 The n is the ANYOF regnode, the p is the target string, lenp
4386 is pointer to the maximum length of how far to go in the p
4387 (if the lenp is zero, UTF8SKIP(p) is used),
4388 do_utf8 tells whether the target string is in UTF-8.
4393 S_reginclass(pTHX_ register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
4395 const char flags = ANYOF_FLAGS(n);
4401 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4402 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
4403 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4405 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4406 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4409 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4410 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4413 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4417 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4420 if (swash_fetch(sw, p, do_utf8))
4422 else if (flags & ANYOF_FOLD) {
4423 if (!match && lenp && av) {
4426 for (i = 0; i <= av_len(av); i++) {
4427 SV* sv = *av_fetch(av, i, FALSE);
4429 const char *s = SvPV(sv, len);
4431 if (len <= plen && memEQ(s, (char*)p, len)) {
4439 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4442 to_utf8_fold(p, tmpbuf, &tmplen);
4443 if (swash_fetch(sw, tmpbuf, do_utf8))
4449 if (match && lenp && *lenp == 0)
4450 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4452 if (!match && c < 256) {
4453 if (ANYOF_BITMAP_TEST(n, c))
4455 else if (flags & ANYOF_FOLD) {
4458 if (flags & ANYOF_LOCALE) {
4459 PL_reg_flags |= RF_tainted;
4460 f = PL_fold_locale[c];
4464 if (f != c && ANYOF_BITMAP_TEST(n, f))
4468 if (!match && (flags & ANYOF_CLASS)) {
4469 PL_reg_flags |= RF_tainted;
4471 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4472 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4473 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4474 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4475 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4476 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4477 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4478 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4479 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4480 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4481 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4482 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4483 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4484 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4485 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4486 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4487 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4488 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4489 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4490 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4491 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4492 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4493 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4494 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4495 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4496 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4497 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4498 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4499 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4500 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4501 ) /* How's that for a conditional? */
4508 return (flags & ANYOF_INVERT) ? !match : match;
4512 S_reghop(pTHX_ U8 *s, I32 off)
4514 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4518 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4521 while (off-- && s < lim) {
4522 /* XXX could check well-formedness here */
4530 if (UTF8_IS_CONTINUED(*s)) {
4531 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4534 /* XXX could check well-formedness here */
4542 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4544 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4548 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4551 while (off-- && s < lim) {
4552 /* XXX could check well-formedness here */
4562 if (UTF8_IS_CONTINUED(*s)) {
4563 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4566 /* XXX could check well-formedness here */
4578 restore_pos(pTHX_ void *arg)
4580 (void)arg; /* unused */
4581 if (PL_reg_eval_set) {
4582 if (PL_reg_oldsaved) {
4583 PL_reg_re->subbeg = PL_reg_oldsaved;
4584 PL_reg_re->sublen = PL_reg_oldsavedlen;
4585 RX_MATCH_COPIED_on(PL_reg_re);
4587 PL_reg_magic->mg_len = PL_reg_oldpos;
4588 PL_reg_eval_set = 0;
4589 PL_curpm = PL_reg_oldcurpm;
4594 S_to_utf8_substr(pTHX_ register regexp *prog)
4597 if (prog->float_substr && !prog->float_utf8) {
4598 prog->float_utf8 = sv = newSVsv(prog->float_substr);
4599 sv_utf8_upgrade(sv);
4600 if (SvTAIL(prog->float_substr))
4602 if (prog->float_substr == prog->check_substr)
4603 prog->check_utf8 = sv;
4605 if (prog->anchored_substr && !prog->anchored_utf8) {
4606 prog->anchored_utf8 = sv = newSVsv(prog->anchored_substr);
4607 sv_utf8_upgrade(sv);
4608 if (SvTAIL(prog->anchored_substr))
4610 if (prog->anchored_substr == prog->check_substr)
4611 prog->check_utf8 = sv;
4616 S_to_byte_substr(pTHX_ register regexp *prog)
4619 if (prog->float_utf8 && !prog->float_substr) {
4620 prog->float_substr = sv = newSVsv(prog->float_utf8);
4621 if (sv_utf8_downgrade(sv, TRUE)) {
4622 if (SvTAIL(prog->float_utf8))
4626 prog->float_substr = sv = &PL_sv_undef;
4628 if (prog->float_utf8 == prog->check_utf8)
4629 prog->check_substr = sv;
4631 if (prog->anchored_utf8 && !prog->anchored_substr) {
4632 prog->anchored_substr = sv = newSVsv(prog->anchored_utf8);
4633 if (sv_utf8_downgrade(sv, TRUE)) {
4634 if (SvTAIL(prog->anchored_utf8))
4638 prog->anchored_substr = sv = &PL_sv_undef;
4640 if (prog->anchored_utf8 == prog->check_utf8)
4641 prog->check_substr = sv;
4647 * c-indentation-style: bsd
4649 * indent-tabs-mode: t
4652 * ex: set ts=8 sts=4 sw=4 noet: