5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-2002, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGEXEC_C
85 #define RF_tainted 1 /* tainted information used? */
86 #define RF_warned 2 /* warned about big count? */
87 #define RF_evaled 4 /* Did an EVAL with setting? */
88 #define RF_utf8 8 /* String contains multibyte chars? */
90 #define UTF (PL_reg_flags & RF_utf8)
92 #define RS_init 1 /* eval environment created */
93 #define RS_set 2 /* replsv value is set */
99 #define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
105 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
106 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
108 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
109 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
110 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
111 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
112 #define HOPc(pos,off) ((char*)HOP(pos,off))
113 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
115 #define HOPBACK(pos, off) ( \
116 (PL_reg_match_utf8) \
117 ? reghopmaybe((U8*)pos, -off) \
118 : (pos - off >= PL_bostr) \
122 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
124 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
125 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
126 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
127 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
128 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
129 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
131 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) { ENTER; save_re_context(); (void)CAT2(is_utf8_, a)((U8*)b); LEAVE; } } STMT_END
133 /* for use after a quantifier and before an EXACT-like node -- japhy */
134 #define JUMPABLE(rn) ( \
135 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
136 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
137 OP(rn) == PLUS || OP(rn) == MINMOD || \
138 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
141 #define HAS_TEXT(rn) ( \
142 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
146 Search for mandatory following text node; for lookahead, the text must
147 follow but for lookbehind (rn->flags != 0) we skip to the next step.
149 #define FIND_NEXT_IMPT(rn) STMT_START { \
150 while (JUMPABLE(rn)) \
151 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
152 rn = NEXTOPER(NEXTOPER(rn)); \
153 else if (OP(rn) == PLUS) \
155 else if (OP(rn) == IFMATCH) \
156 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
157 else rn += NEXT_OFF(rn); \
160 static void restore_pos(pTHX_ void *arg);
163 S_regcppush(pTHX_ I32 parenfloor)
165 int retval = PL_savestack_ix;
166 #define REGCP_PAREN_ELEMS 4
167 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
170 if (paren_elems_to_push < 0)
171 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
173 #define REGCP_OTHER_ELEMS 6
174 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
175 for (p = PL_regsize; p > parenfloor; p--) {
176 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
177 SSPUSHINT(PL_regendp[p]);
178 SSPUSHINT(PL_regstartp[p]);
179 SSPUSHPTR(PL_reg_start_tmp[p]);
182 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
183 SSPUSHINT(PL_regsize);
184 SSPUSHINT(*PL_reglastparen);
185 SSPUSHINT(*PL_reglastcloseparen);
186 SSPUSHPTR(PL_reginput);
187 #define REGCP_FRAME_ELEMS 2
188 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
189 * are needed for the regexp context stack bookkeeping. */
190 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
191 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
196 /* These are needed since we do not localize EVAL nodes: */
197 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
198 " Setting an EVAL scope, savestack=%"IVdf"\n", \
199 (IV)PL_savestack_ix)); cp = PL_savestack_ix
201 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
202 PerlIO_printf(Perl_debug_log, \
203 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
204 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
214 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
216 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
217 i = SSPOPINT; /* Parentheses elements to pop. */
218 input = (char *) SSPOPPTR;
219 *PL_reglastcloseparen = SSPOPINT;
220 *PL_reglastparen = SSPOPINT;
221 PL_regsize = SSPOPINT;
223 /* Now restore the parentheses context. */
224 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
225 i > 0; i -= REGCP_PAREN_ELEMS) {
226 paren = (U32)SSPOPINT;
227 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
228 PL_regstartp[paren] = SSPOPINT;
230 if (paren <= *PL_reglastparen)
231 PL_regendp[paren] = tmps;
233 PerlIO_printf(Perl_debug_log,
234 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
235 (UV)paren, (IV)PL_regstartp[paren],
236 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
237 (IV)PL_regendp[paren],
238 (paren > *PL_reglastparen ? "(no)" : ""));
242 if (*PL_reglastparen + 1 <= PL_regnpar) {
243 PerlIO_printf(Perl_debug_log,
244 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
245 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
249 /* It would seem that the similar code in regtry()
250 * already takes care of this, and in fact it is in
251 * a better location to since this code can #if 0-ed out
252 * but the code in regtry() is needed or otherwise tests
253 * requiring null fields (pat.t#187 and split.t#{13,14}
254 * (as of patchlevel 7877) will fail. Then again,
255 * this code seems to be necessary or otherwise
256 * building DynaLoader will fail:
257 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
259 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
260 if (paren > PL_regsize)
261 PL_regstartp[paren] = -1;
262 PL_regendp[paren] = -1;
269 S_regcp_set_to(pTHX_ I32 ss)
271 I32 tmp = PL_savestack_ix;
273 PL_savestack_ix = ss;
275 PL_savestack_ix = tmp;
279 typedef struct re_cc_state
283 struct re_cc_state *prev;
288 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
290 #define TRYPAREN(paren, n, input) { \
293 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
294 PL_regendp[paren] = input - PL_bostr; \
297 PL_regendp[paren] = -1; \
299 if (regmatch(next)) \
302 PL_regendp[paren] = -1; \
307 * pregexec and friends
311 - pregexec - match a regexp against a string
314 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
315 char *strbeg, I32 minend, SV *screamer, U32 nosave)
316 /* strend: pointer to null at end of string */
317 /* strbeg: real beginning of string */
318 /* minend: end of match must be >=minend after stringarg. */
319 /* nosave: For optimizations. */
322 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
323 nosave ? 0 : REXEC_COPY_STR);
327 S_cache_re(pTHX_ regexp *prog)
329 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
331 PL_regprogram = prog->program;
333 PL_regnpar = prog->nparens;
334 PL_regdata = prog->data;
339 * Need to implement the following flags for reg_anch:
341 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
343 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
344 * INTUIT_AUTORITATIVE_ML
345 * INTUIT_ONCE_NOML - Intuit can match in one location only.
348 * Another flag for this function: SECOND_TIME (so that float substrs
349 * with giant delta may be not rechecked).
352 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
354 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
355 Otherwise, only SvCUR(sv) is used to get strbeg. */
357 /* XXXX We assume that strpos is strbeg unless sv. */
359 /* XXXX Some places assume that there is a fixed substring.
360 An update may be needed if optimizer marks as "INTUITable"
361 RExen without fixed substrings. Similarly, it is assumed that
362 lengths of all the strings are no more than minlen, thus they
363 cannot come from lookahead.
364 (Or minlen should take into account lookahead.) */
366 /* A failure to find a constant substring means that there is no need to make
367 an expensive call to REx engine, thus we celebrate a failure. Similarly,
368 finding a substring too deep into the string means that less calls to
369 regtry() should be needed.
371 REx compiler's optimizer found 4 possible hints:
372 a) Anchored substring;
374 c) Whether we are anchored (beginning-of-line or \G);
375 d) First node (of those at offset 0) which may distingush positions;
376 We use a)b)d) and multiline-part of c), and try to find a position in the
377 string which does not contradict any of them.
380 /* Most of decisions we do here should have been done at compile time.
381 The nodes of the REx which we used for the search should have been
382 deleted from the finite automaton. */
385 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
386 char *strend, U32 flags, re_scream_pos_data *data)
388 register I32 start_shift = 0;
389 /* Should be nonnegative! */
390 register I32 end_shift = 0;
396 register char *other_last = Nullch; /* other substr checked before this */
397 char *check_at = Nullch; /* check substr found at this pos */
399 char *i_strpos = strpos;
400 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
403 if (prog->reganch & ROPT_UTF8) {
404 DEBUG_r(PerlIO_printf(Perl_debug_log,
405 "UTF-8 regex...\n"));
406 PL_reg_flags |= RF_utf8;
410 char *s = PL_reg_match_utf8 ?
411 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
413 int len = PL_reg_match_utf8 ?
414 strlen(s) : strend - strpos;
417 if (PL_reg_match_utf8)
418 DEBUG_r(PerlIO_printf(Perl_debug_log,
419 "UTF-8 target...\n"));
420 PerlIO_printf(Perl_debug_log,
421 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
422 PL_colors[4],PL_colors[5],PL_colors[0],
425 (strlen(prog->precomp) > 60 ? "..." : ""),
427 (int)(len > 60 ? 60 : len),
429 (len > 60 ? "..." : "")
433 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
434 DEBUG_r(PerlIO_printf(Perl_debug_log,
435 "String too short... [re_intuit_start]\n"));
438 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
440 check = prog->check_substr;
441 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
442 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
443 || ( (prog->reganch & ROPT_ANCH_BOL)
444 && !PL_multiline ) ); /* Check after \n? */
447 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
448 | ROPT_IMPLICIT)) /* not a real BOL */
449 /* SvCUR is not set on references: SvRV and SvPVX overlap */
451 && (strpos != strbeg)) {
452 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
455 if (prog->check_offset_min == prog->check_offset_max &&
456 !(prog->reganch & ROPT_CANY_SEEN)) {
457 /* Substring at constant offset from beg-of-str... */
460 s = HOP3c(strpos, prog->check_offset_min, strend);
462 slen = SvCUR(check); /* >= 1 */
464 if ( strend - s > slen || strend - s < slen - 1
465 || (strend - s == slen && strend[-1] != '\n')) {
466 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
469 /* Now should match s[0..slen-2] */
471 if (slen && (*SvPVX(check) != *s
473 && memNE(SvPVX(check), s, slen)))) {
475 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
479 else if (*SvPVX(check) != *s
480 || ((slen = SvCUR(check)) > 1
481 && memNE(SvPVX(check), s, slen)))
483 goto success_at_start;
486 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
488 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
489 end_shift = prog->minlen - start_shift -
490 CHR_SVLEN(check) + (SvTAIL(check) != 0);
492 I32 end = prog->check_offset_max + CHR_SVLEN(check)
493 - (SvTAIL(check) != 0);
494 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
496 if (end_shift < eshift)
500 else { /* Can match at random position */
503 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
504 /* Should be nonnegative! */
505 end_shift = prog->minlen - start_shift -
506 CHR_SVLEN(check) + (SvTAIL(check) != 0);
509 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
511 Perl_croak(aTHX_ "panic: end_shift");
515 /* Find a possible match in the region s..strend by looking for
516 the "check" substring in the region corrected by start/end_shift. */
517 if (flags & REXEC_SCREAM) {
518 I32 p = -1; /* Internal iterator of scream. */
519 I32 *pp = data ? data->scream_pos : &p;
521 if (PL_screamfirst[BmRARE(check)] >= 0
522 || ( BmRARE(check) == '\n'
523 && (BmPREVIOUS(check) == SvCUR(check) - 1)
525 s = screaminstr(sv, check,
526 start_shift + (s - strbeg), end_shift, pp, 0);
530 *data->scream_olds = s;
532 else if (prog->reganch & ROPT_CANY_SEEN)
533 s = fbm_instr((U8*)(s + start_shift),
534 (U8*)(strend - end_shift),
535 check, PL_multiline ? FBMrf_MULTILINE : 0);
537 s = fbm_instr(HOP3(s, start_shift, strend),
538 HOP3(strend, -end_shift, strbeg),
539 check, PL_multiline ? FBMrf_MULTILINE : 0);
541 /* Update the count-of-usability, remove useless subpatterns,
544 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
545 (s ? "Found" : "Did not find"),
546 ((check == prog->anchored_substr) ? "anchored" : "floating"),
548 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
550 PL_colors[1], (SvTAIL(check) ? "$" : ""),
551 (s ? " at offset " : "...\n") ) );
558 /* Finish the diagnostic message */
559 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
561 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
562 Start with the other substr.
563 XXXX no SCREAM optimization yet - and a very coarse implementation
564 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
565 *always* match. Probably should be marked during compile...
566 Probably it is right to do no SCREAM here...
569 if (prog->float_substr && prog->anchored_substr) {
570 /* Take into account the "other" substring. */
571 /* XXXX May be hopelessly wrong for UTF... */
574 if (check == prog->float_substr) {
577 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
580 t = s - prog->check_offset_max;
581 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
582 && (!(prog->reganch & ROPT_UTF8)
583 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
588 t = HOP3c(t, prog->anchored_offset, strend);
589 if (t < other_last) /* These positions already checked */
591 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
594 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
595 /* On end-of-str: see comment below. */
596 s = fbm_instr((unsigned char*)t,
597 HOP3(HOP3(last1, prog->anchored_offset, strend)
598 + SvCUR(prog->anchored_substr),
599 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
600 prog->anchored_substr,
601 PL_multiline ? FBMrf_MULTILINE : 0);
602 DEBUG_r(PerlIO_printf(Perl_debug_log,
603 "%s anchored substr `%s%.*s%s'%s",
604 (s ? "Found" : "Contradicts"),
606 (int)(SvCUR(prog->anchored_substr)
607 - (SvTAIL(prog->anchored_substr)!=0)),
608 SvPVX(prog->anchored_substr),
609 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
611 if (last1 >= last2) {
612 DEBUG_r(PerlIO_printf(Perl_debug_log,
613 ", giving up...\n"));
616 DEBUG_r(PerlIO_printf(Perl_debug_log,
617 ", trying floating at offset %ld...\n",
618 (long)(HOP3c(s1, 1, strend) - i_strpos)));
619 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
620 s = HOP3c(last, 1, strend);
624 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
625 (long)(s - i_strpos)));
626 t = HOP3c(s, -prog->anchored_offset, strbeg);
627 other_last = HOP3c(s, 1, strend);
635 else { /* Take into account the floating substring. */
639 t = HOP3c(s, -start_shift, strbeg);
641 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
642 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
643 last = HOP3c(t, prog->float_max_offset, strend);
644 s = HOP3c(t, prog->float_min_offset, strend);
647 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
648 /* fbm_instr() takes into account exact value of end-of-str
649 if the check is SvTAIL(ed). Since false positives are OK,
650 and end-of-str is not later than strend we are OK. */
651 s = fbm_instr((unsigned char*)s,
652 (unsigned char*)last + SvCUR(prog->float_substr)
653 - (SvTAIL(prog->float_substr)!=0),
654 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
655 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
656 (s ? "Found" : "Contradicts"),
658 (int)(SvCUR(prog->float_substr)
659 - (SvTAIL(prog->float_substr)!=0)),
660 SvPVX(prog->float_substr),
661 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
664 DEBUG_r(PerlIO_printf(Perl_debug_log,
665 ", giving up...\n"));
668 DEBUG_r(PerlIO_printf(Perl_debug_log,
669 ", trying anchored starting at offset %ld...\n",
670 (long)(s1 + 1 - i_strpos)));
672 s = HOP3c(t, 1, strend);
676 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
677 (long)(s - i_strpos)));
678 other_last = s; /* Fix this later. --Hugo */
687 t = s - prog->check_offset_max;
688 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
689 && (!(prog->reganch & ROPT_UTF8)
690 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
692 /* Fixed substring is found far enough so that the match
693 cannot start at strpos. */
695 if (ml_anch && t[-1] != '\n') {
696 /* Eventually fbm_*() should handle this, but often
697 anchored_offset is not 0, so this check will not be wasted. */
698 /* XXXX In the code below we prefer to look for "^" even in
699 presence of anchored substrings. And we search even
700 beyond the found float position. These pessimizations
701 are historical artefacts only. */
703 while (t < strend - prog->minlen) {
705 if (t < check_at - prog->check_offset_min) {
706 if (prog->anchored_substr) {
707 /* Since we moved from the found position,
708 we definitely contradict the found anchored
709 substr. Due to the above check we do not
710 contradict "check" substr.
711 Thus we can arrive here only if check substr
712 is float. Redo checking for "other"=="fixed".
715 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
716 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
717 goto do_other_anchored;
719 /* We don't contradict the found floating substring. */
720 /* XXXX Why not check for STCLASS? */
722 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
723 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
726 /* Position contradicts check-string */
727 /* XXXX probably better to look for check-string
728 than for "\n", so one should lower the limit for t? */
729 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
730 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
731 other_last = strpos = s = t + 1;
736 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
737 PL_colors[0],PL_colors[1]));
741 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
742 PL_colors[0],PL_colors[1]));
746 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
749 /* The found string does not prohibit matching at strpos,
750 - no optimization of calling REx engine can be performed,
751 unless it was an MBOL and we are not after MBOL,
752 or a future STCLASS check will fail this. */
754 /* Even in this situation we may use MBOL flag if strpos is offset
755 wrt the start of the string. */
756 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
757 && (strpos != strbeg) && strpos[-1] != '\n'
758 /* May be due to an implicit anchor of m{.*foo} */
759 && !(prog->reganch & ROPT_IMPLICIT))
764 DEBUG_r( if (ml_anch)
765 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
766 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
769 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
770 && prog->check_substr /* Could be deleted already */
771 && --BmUSEFUL(prog->check_substr) < 0
772 && prog->check_substr == prog->float_substr)
774 /* If flags & SOMETHING - do not do it many times on the same match */
775 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
776 SvREFCNT_dec(prog->check_substr);
777 prog->check_substr = Nullsv; /* disable */
778 prog->float_substr = Nullsv; /* clear */
779 check = Nullsv; /* abort */
781 /* XXXX This is a remnant of the old implementation. It
782 looks wasteful, since now INTUIT can use many
784 prog->reganch &= ~RE_USE_INTUIT;
791 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
792 if (prog->regstclass) {
793 /* minlen == 0 is possible if regstclass is \b or \B,
794 and the fixed substr is ''$.
795 Since minlen is already taken into account, s+1 is before strend;
796 accidentally, minlen >= 1 guaranties no false positives at s + 1
797 even for \b or \B. But (minlen? 1 : 0) below assumes that
798 regstclass does not come from lookahead... */
799 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
800 This leaves EXACTF only, which is dealt with in find_byclass(). */
801 U8* str = (U8*)STRING(prog->regstclass);
802 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
803 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
805 char *endpos = (prog->anchored_substr || ml_anch)
806 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
807 : (prog->float_substr
808 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
811 char *startpos = strbeg;
814 if (prog->reganch & ROPT_UTF8) {
815 PL_regdata = prog->data;
818 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
823 if (endpos == strend) {
824 DEBUG_r( PerlIO_printf(Perl_debug_log,
825 "Could not match STCLASS...\n") );
828 DEBUG_r( PerlIO_printf(Perl_debug_log,
829 "This position contradicts STCLASS...\n") );
830 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
832 /* Contradict one of substrings */
833 if (prog->anchored_substr) {
834 if (prog->anchored_substr == check) {
835 DEBUG_r( what = "anchored" );
837 s = HOP3c(t, 1, strend);
838 if (s + start_shift + end_shift > strend) {
839 /* XXXX Should be taken into account earlier? */
840 DEBUG_r( PerlIO_printf(Perl_debug_log,
841 "Could not match STCLASS...\n") );
846 DEBUG_r( PerlIO_printf(Perl_debug_log,
847 "Looking for %s substr starting at offset %ld...\n",
848 what, (long)(s + start_shift - i_strpos)) );
851 /* Have both, check_string is floating */
852 if (t + start_shift >= check_at) /* Contradicts floating=check */
853 goto retry_floating_check;
854 /* Recheck anchored substring, but not floating... */
858 DEBUG_r( PerlIO_printf(Perl_debug_log,
859 "Looking for anchored substr starting at offset %ld...\n",
860 (long)(other_last - i_strpos)) );
861 goto do_other_anchored;
863 /* Another way we could have checked stclass at the
864 current position only: */
869 DEBUG_r( PerlIO_printf(Perl_debug_log,
870 "Looking for /%s^%s/m starting at offset %ld...\n",
871 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
874 if (!prog->float_substr) /* Could have been deleted */
876 /* Check is floating subtring. */
877 retry_floating_check:
878 t = check_at - start_shift;
879 DEBUG_r( what = "floating" );
880 goto hop_and_restart;
883 DEBUG_r(PerlIO_printf(Perl_debug_log,
884 "By STCLASS: moving %ld --> %ld\n",
885 (long)(t - i_strpos), (long)(s - i_strpos))
889 DEBUG_r(PerlIO_printf(Perl_debug_log,
890 "Does not contradict STCLASS...\n");
895 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
896 PL_colors[4], (check ? "Guessed" : "Giving up"),
897 PL_colors[5], (long)(s - i_strpos)) );
900 fail_finish: /* Substring not found */
901 if (prog->check_substr) /* could be removed already */
902 BmUSEFUL(prog->check_substr) += 5; /* hooray */
904 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
905 PL_colors[4],PL_colors[5]));
909 /* We know what class REx starts with. Try to find this position... */
911 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
913 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
919 register I32 tmp = 1; /* Scratch variable? */
920 register bool do_utf8 = PL_reg_match_utf8;
922 /* We know what class it must start with. */
926 STRLEN skip = do_utf8 ? UTF8SKIP(s) : 1;
929 reginclass(c, (U8*)s, 0, do_utf8) :
930 REGINCLASS(c, (U8*)s) ||
931 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
932 /* The assignment of 2 is intentional:
933 * for the sharp s, the skip is 2. */
934 (skip = SHARP_S_SKIP)
936 if (tmp && (norun || regtry(prog, s)))
948 if (tmp && (norun || regtry(prog, s)))
960 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
961 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
963 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
964 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
966 c1 = utf8_to_uvchr(tmpbuf1, 0);
967 c2 = utf8_to_uvchr(tmpbuf2, 0);
978 c2 = PL_fold_locale[c1];
980 e = do_utf8 ? s + ln : strend - ln;
983 e = s; /* Due to minlen logic of intuit() */
985 /* The idea in the EXACTF* cases is to first find the
986 * first character of the EXACTF* node and then, if
987 * necessary, case-insensitively compare the full
988 * text of the node. The c1 and c2 are the first
989 * characters (though in Unicode it gets a bit
990 * more complicated because there are more cases
991 * than just upper and lower: one needs to use
992 * the so-called folding case for case-insensitive
993 * matching (called "loose matching" in Unicode).
994 * ibcmp_utf8() will do just that. */
998 U8 tmpbuf [UTF8_MAXLEN+1];
999 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1000 STRLEN len, foldlen;
1004 c = utf8_to_uvchr((U8*)s, &len);
1007 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1008 m, (char **)0, ln, UTF))
1009 && (norun || regtry(prog, s)) )
1012 uvchr_to_utf8(tmpbuf, c);
1013 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1015 && (f == c1 || f == c2)
1016 && (ln == foldlen ||
1017 !ibcmp_utf8((char *) foldbuf,
1018 (char **)0, foldlen, do_utf8,
1020 (char **)0, ln, UTF))
1021 && (norun || regtry(prog, s)) )
1029 c = utf8_to_uvchr((U8*)s, &len);
1031 /* Handle some of the three Greek sigmas cases.
1032 * Note that not all the possible combinations
1033 * are handled here: some of them are handled
1034 * by the standard folding rules, and some of
1035 * them (the character class or ANYOF cases)
1036 * are handled during compiletime in
1037 * regexec.c:S_regclass(). */
1038 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1039 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1040 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1042 if ( (c == c1 || c == c2)
1044 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1045 m, (char **)0, ln, UTF))
1046 && (norun || regtry(prog, s)) )
1049 uvchr_to_utf8(tmpbuf, c);
1050 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1052 && (f == c1 || f == c2)
1053 && (ln == foldlen ||
1054 !ibcmp_utf8((char *) foldbuf,
1055 (char **)0, foldlen, do_utf8,
1057 (char **)0, ln, UTF))
1058 && (norun || regtry(prog, s)) )
1069 && (ln == 1 || !(OP(c) == EXACTF
1071 : ibcmp_locale(s, m, ln)))
1072 && (norun || regtry(prog, s)) )
1078 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1079 && (ln == 1 || !(OP(c) == EXACTF
1081 : ibcmp_locale(s, m, ln)))
1082 && (norun || regtry(prog, s)) )
1089 PL_reg_flags |= RF_tainted;
1096 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1099 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1101 tmp = ((OP(c) == BOUND ?
1102 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1103 LOAD_UTF8_CHARCLASS(alnum,"a");
1104 while (s < strend) {
1105 if (tmp == !(OP(c) == BOUND ?
1106 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1107 isALNUM_LC_utf8((U8*)s)))
1110 if ((norun || regtry(prog, s)))
1117 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1118 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1119 while (s < strend) {
1121 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1123 if ((norun || regtry(prog, s)))
1129 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1133 PL_reg_flags |= RF_tainted;
1140 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
1143 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
1145 tmp = ((OP(c) == NBOUND ?
1146 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1147 LOAD_UTF8_CHARCLASS(alnum,"a");
1148 while (s < strend) {
1149 if (tmp == !(OP(c) == NBOUND ?
1150 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1151 isALNUM_LC_utf8((U8*)s)))
1153 else if ((norun || regtry(prog, s)))
1159 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1160 tmp = ((OP(c) == NBOUND ?
1161 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1162 while (s < strend) {
1164 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1166 else if ((norun || regtry(prog, s)))
1171 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1176 LOAD_UTF8_CHARCLASS(alnum,"a");
1177 while (s < strend) {
1178 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1179 if (tmp && (norun || regtry(prog, s)))
1190 while (s < strend) {
1192 if (tmp && (norun || regtry(prog, s)))
1204 PL_reg_flags |= RF_tainted;
1206 while (s < strend) {
1207 if (isALNUM_LC_utf8((U8*)s)) {
1208 if (tmp && (norun || regtry(prog, s)))
1219 while (s < strend) {
1220 if (isALNUM_LC(*s)) {
1221 if (tmp && (norun || regtry(prog, s)))
1234 LOAD_UTF8_CHARCLASS(alnum,"a");
1235 while (s < strend) {
1236 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1237 if (tmp && (norun || regtry(prog, s)))
1248 while (s < strend) {
1250 if (tmp && (norun || regtry(prog, s)))
1262 PL_reg_flags |= RF_tainted;
1264 while (s < strend) {
1265 if (!isALNUM_LC_utf8((U8*)s)) {
1266 if (tmp && (norun || regtry(prog, s)))
1277 while (s < strend) {
1278 if (!isALNUM_LC(*s)) {
1279 if (tmp && (norun || regtry(prog, s)))
1292 LOAD_UTF8_CHARCLASS(space," ");
1293 while (s < strend) {
1294 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1295 if (tmp && (norun || regtry(prog, s)))
1306 while (s < strend) {
1308 if (tmp && (norun || regtry(prog, s)))
1320 PL_reg_flags |= RF_tainted;
1322 while (s < strend) {
1323 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1324 if (tmp && (norun || regtry(prog, s)))
1335 while (s < strend) {
1336 if (isSPACE_LC(*s)) {
1337 if (tmp && (norun || regtry(prog, s)))
1350 LOAD_UTF8_CHARCLASS(space," ");
1351 while (s < strend) {
1352 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1353 if (tmp && (norun || regtry(prog, s)))
1364 while (s < strend) {
1366 if (tmp && (norun || regtry(prog, s)))
1378 PL_reg_flags |= RF_tainted;
1380 while (s < strend) {
1381 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1382 if (tmp && (norun || regtry(prog, s)))
1393 while (s < strend) {
1394 if (!isSPACE_LC(*s)) {
1395 if (tmp && (norun || regtry(prog, s)))
1408 LOAD_UTF8_CHARCLASS(digit,"0");
1409 while (s < strend) {
1410 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1411 if (tmp && (norun || regtry(prog, s)))
1422 while (s < strend) {
1424 if (tmp && (norun || regtry(prog, s)))
1436 PL_reg_flags |= RF_tainted;
1438 while (s < strend) {
1439 if (isDIGIT_LC_utf8((U8*)s)) {
1440 if (tmp && (norun || regtry(prog, s)))
1451 while (s < strend) {
1452 if (isDIGIT_LC(*s)) {
1453 if (tmp && (norun || regtry(prog, s)))
1466 LOAD_UTF8_CHARCLASS(digit,"0");
1467 while (s < strend) {
1468 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1469 if (tmp && (norun || regtry(prog, s)))
1480 while (s < strend) {
1482 if (tmp && (norun || regtry(prog, s)))
1494 PL_reg_flags |= RF_tainted;
1496 while (s < strend) {
1497 if (!isDIGIT_LC_utf8((U8*)s)) {
1498 if (tmp && (norun || regtry(prog, s)))
1509 while (s < strend) {
1510 if (!isDIGIT_LC(*s)) {
1511 if (tmp && (norun || regtry(prog, s)))
1523 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1532 - regexec_flags - match a regexp against a string
1535 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1536 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1537 /* strend: pointer to null at end of string */
1538 /* strbeg: real beginning of string */
1539 /* minend: end of match must be >=minend after stringarg. */
1540 /* data: May be used for some additional optimizations. */
1541 /* nosave: For optimizations. */
1544 register regnode *c;
1545 register char *startpos = stringarg;
1546 I32 minlen; /* must match at least this many chars */
1547 I32 dontbother = 0; /* how many characters not to try at end */
1548 /* I32 start_shift = 0; */ /* Offset of the start to find
1549 constant substr. */ /* CC */
1550 I32 end_shift = 0; /* Same for the end. */ /* CC */
1551 I32 scream_pos = -1; /* Internal iterator of scream. */
1553 SV* oreplsv = GvSV(PL_replgv);
1554 bool do_utf8 = DO_UTF8(sv);
1556 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1557 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1564 PL_regnarrate = DEBUG_r_TEST;
1567 /* Be paranoid... */
1568 if (prog == NULL || startpos == NULL) {
1569 Perl_croak(aTHX_ "NULL regexp parameter");
1573 minlen = prog->minlen;
1574 if (strend - startpos < minlen) {
1575 DEBUG_r(PerlIO_printf(Perl_debug_log,
1576 "String too short [regexec_flags]...\n"));
1580 /* Check validity of program. */
1581 if (UCHARAT(prog->program) != REG_MAGIC) {
1582 Perl_croak(aTHX_ "corrupted regexp program");
1586 PL_reg_eval_set = 0;
1589 if (prog->reganch & ROPT_UTF8)
1590 PL_reg_flags |= RF_utf8;
1592 /* Mark beginning of line for ^ and lookbehind. */
1593 PL_regbol = startpos;
1597 /* Mark end of line for $ (and such) */
1600 /* see how far we have to get to not match where we matched before */
1601 PL_regtill = startpos+minend;
1603 /* We start without call_cc context. */
1606 /* If there is a "must appear" string, look for it. */
1609 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1612 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1613 PL_reg_ganch = startpos;
1614 else if (sv && SvTYPE(sv) >= SVt_PVMG
1616 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1617 && mg->mg_len >= 0) {
1618 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1619 if (prog->reganch & ROPT_ANCH_GPOS) {
1620 if (s > PL_reg_ganch)
1625 else /* pos() not defined */
1626 PL_reg_ganch = strbeg;
1629 if (do_utf8 == (UTF!=0) &&
1630 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1631 re_scream_pos_data d;
1633 d.scream_olds = &scream_olds;
1634 d.scream_pos = &scream_pos;
1635 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1637 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1638 goto phooey; /* not present */
1644 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1645 UNI_DISPLAY_REGEX) :
1647 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1648 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1649 UNI_DISPLAY_REGEX) : startpos;
1650 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1653 PerlIO_printf(Perl_debug_log,
1654 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1655 PL_colors[4],PL_colors[5],PL_colors[0],
1658 len0 > 60 ? "..." : "",
1660 (int)(len1 > 60 ? 60 : len1),
1662 (len1 > 60 ? "..." : "")
1666 /* Simplest case: anchored match need be tried only once. */
1667 /* [unless only anchor is BOL and multiline is set] */
1668 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1669 if (s == startpos && regtry(prog, startpos))
1671 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1672 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1677 dontbother = minlen - 1;
1678 end = HOP3c(strend, -dontbother, strbeg) - 1;
1679 /* for multiline we only have to try after newlines */
1680 if (prog->check_substr) {
1684 if (regtry(prog, s))
1689 if (prog->reganch & RE_USE_INTUIT) {
1690 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1701 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1702 if (regtry(prog, s))
1709 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1710 if (regtry(prog, PL_reg_ganch))
1715 /* Messy cases: unanchored match. */
1716 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1717 /* we have /x+whatever/ */
1718 /* it must be a one character string (XXXX Except UTF?) */
1719 char ch = SvPVX(prog->anchored_substr)[0];
1725 while (s < strend) {
1727 DEBUG_r( did_match = 1 );
1728 if (regtry(prog, s)) goto got_it;
1730 while (s < strend && *s == ch)
1737 while (s < strend) {
1739 DEBUG_r( did_match = 1 );
1740 if (regtry(prog, s)) goto got_it;
1742 while (s < strend && *s == ch)
1748 DEBUG_r(if (!did_match)
1749 PerlIO_printf(Perl_debug_log,
1750 "Did not find anchored character...\n")
1754 else if (do_utf8 == (UTF!=0) &&
1755 (prog->anchored_substr != Nullsv
1756 || (prog->float_substr != Nullsv
1757 && prog->float_max_offset < strend - s))) {
1758 SV *must = prog->anchored_substr
1759 ? prog->anchored_substr : prog->float_substr;
1761 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1763 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1764 char *last = HOP3c(strend, /* Cannot start after this */
1765 -(I32)(CHR_SVLEN(must)
1766 - (SvTAIL(must) != 0) + back_min), strbeg);
1767 char *last1; /* Last position checked before */
1773 last1 = HOPc(s, -1);
1775 last1 = s - 1; /* bogus */
1777 /* XXXX check_substr already used to find `s', can optimize if
1778 check_substr==must. */
1780 dontbother = end_shift;
1781 strend = HOPc(strend, -dontbother);
1782 while ( (s <= last) &&
1783 ((flags & REXEC_SCREAM)
1784 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1785 end_shift, &scream_pos, 0))
1786 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1787 (unsigned char*)strend, must,
1788 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1789 DEBUG_r( did_match = 1 );
1790 if (HOPc(s, -back_max) > last1) {
1791 last1 = HOPc(s, -back_min);
1792 s = HOPc(s, -back_max);
1795 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1797 last1 = HOPc(s, -back_min);
1801 while (s <= last1) {
1802 if (regtry(prog, s))
1808 while (s <= last1) {
1809 if (regtry(prog, s))
1815 DEBUG_r(if (!did_match)
1816 PerlIO_printf(Perl_debug_log,
1817 "Did not find %s substr `%s%.*s%s'%s...\n",
1818 ((must == prog->anchored_substr)
1819 ? "anchored" : "floating"),
1821 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1823 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1827 else if ((c = prog->regstclass)) {
1828 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1829 /* don't bother with what can't match */
1830 strend = HOPc(strend, -(minlen - 1));
1832 SV *prop = sv_newmortal();
1840 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1841 UNI_DISPLAY_REGEX) :
1843 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1845 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1846 len1 = UTF ? SvCUR(dsv1) : strend - s;
1847 PerlIO_printf(Perl_debug_log,
1848 "Matching stclass `%*.*s' against `%*.*s'\n",
1852 if (find_byclass(prog, c, s, strend, startpos, 0))
1854 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1858 if (prog->float_substr != Nullsv) { /* Trim the end. */
1861 if (flags & REXEC_SCREAM) {
1862 last = screaminstr(sv, prog->float_substr, s - strbeg,
1863 end_shift, &scream_pos, 1); /* last one */
1865 last = scream_olds; /* Only one occurrence. */
1869 char *little = SvPV(prog->float_substr, len);
1871 if (SvTAIL(prog->float_substr)) {
1872 if (memEQ(strend - len + 1, little, len - 1))
1873 last = strend - len + 1;
1874 else if (!PL_multiline)
1875 last = memEQ(strend - len, little, len)
1876 ? strend - len : Nullch;
1882 last = rninstr(s, strend, little, little + len);
1884 last = strend; /* matching `$' */
1888 DEBUG_r(PerlIO_printf(Perl_debug_log,
1889 "%sCan't trim the tail, match fails (should not happen)%s\n",
1890 PL_colors[4],PL_colors[5]));
1891 goto phooey; /* Should not happen! */
1893 dontbother = strend - last + prog->float_min_offset;
1895 if (minlen && (dontbother < minlen))
1896 dontbother = minlen - 1;
1897 strend -= dontbother; /* this one's always in bytes! */
1898 /* We don't know much -- general case. */
1901 if (regtry(prog, s))
1910 if (regtry(prog, s))
1912 } while (s++ < strend);
1920 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1922 if (PL_reg_eval_set) {
1923 /* Preserve the current value of $^R */
1924 if (oreplsv != GvSV(PL_replgv))
1925 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1926 restored, the value remains
1928 restore_pos(aTHX_ 0);
1931 /* make sure $`, $&, $', and $digit will work later */
1932 if ( !(flags & REXEC_NOT_FIRST) ) {
1933 if (RX_MATCH_COPIED(prog)) {
1934 Safefree(prog->subbeg);
1935 RX_MATCH_COPIED_off(prog);
1937 if (flags & REXEC_COPY_STR) {
1938 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1940 s = savepvn(strbeg, i);
1943 RX_MATCH_COPIED_on(prog);
1946 prog->subbeg = strbeg;
1947 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1954 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1955 PL_colors[4],PL_colors[5]));
1956 if (PL_reg_eval_set)
1957 restore_pos(aTHX_ 0);
1962 - regtry - try match at specific point
1964 STATIC I32 /* 0 failure, 1 success */
1965 S_regtry(pTHX_ regexp *prog, char *startpos)
1973 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1975 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1978 PL_reg_eval_set = RS_init;
1980 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1981 (IV)(PL_stack_sp - PL_stack_base));
1983 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1984 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1985 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1987 /* Apparently this is not needed, judging by wantarray. */
1988 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1989 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1992 /* Make $_ available to executed code. */
1993 if (PL_reg_sv != DEFSV) {
1994 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
1999 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2000 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2001 /* prepare for quick setting of pos */
2002 sv_magic(PL_reg_sv, (SV*)0,
2003 PERL_MAGIC_regex_global, Nullch, 0);
2004 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2008 PL_reg_oldpos = mg->mg_len;
2009 SAVEDESTRUCTOR_X(restore_pos, 0);
2011 if (!PL_reg_curpm) {
2012 Newz(22,PL_reg_curpm, 1, PMOP);
2015 SV* repointer = newSViv(0);
2016 /* so we know which PL_regex_padav element is PL_reg_curpm */
2017 SvFLAGS(repointer) |= SVf_BREAK;
2018 av_push(PL_regex_padav,repointer);
2019 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2020 PL_regex_pad = AvARRAY(PL_regex_padav);
2024 PM_SETRE(PL_reg_curpm, prog);
2025 PL_reg_oldcurpm = PL_curpm;
2026 PL_curpm = PL_reg_curpm;
2027 if (RX_MATCH_COPIED(prog)) {
2028 /* Here is a serious problem: we cannot rewrite subbeg,
2029 since it may be needed if this match fails. Thus
2030 $` inside (?{}) could fail... */
2031 PL_reg_oldsaved = prog->subbeg;
2032 PL_reg_oldsavedlen = prog->sublen;
2033 RX_MATCH_COPIED_off(prog);
2036 PL_reg_oldsaved = Nullch;
2037 prog->subbeg = PL_bostr;
2038 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2040 prog->startp[0] = startpos - PL_bostr;
2041 PL_reginput = startpos;
2042 PL_regstartp = prog->startp;
2043 PL_regendp = prog->endp;
2044 PL_reglastparen = &prog->lastparen;
2045 PL_reglastcloseparen = &prog->lastcloseparen;
2046 prog->lastparen = 0;
2048 DEBUG_r(PL_reg_starttry = startpos);
2049 if (PL_reg_start_tmpl <= prog->nparens) {
2050 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2051 if(PL_reg_start_tmp)
2052 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2054 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2057 /* XXXX What this code is doing here?!!! There should be no need
2058 to do this again and again, PL_reglastparen should take care of
2061 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2062 * Actually, the code in regcppop() (which Ilya may be meaning by
2063 * PL_reglastparen), is not needed at all by the test suite
2064 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2065 * enough, for building DynaLoader, or otherwise this
2066 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2067 * will happen. Meanwhile, this code *is* needed for the
2068 * above-mentioned test suite tests to succeed. The common theme
2069 * on those tests seems to be returning null fields from matches.
2074 if (prog->nparens) {
2075 for (i = prog->nparens; i > *PL_reglastparen; i--) {
2082 if (regmatch(prog->program + 1)) {
2083 prog->endp[0] = PL_reginput - PL_bostr;
2086 REGCP_UNWIND(lastcp);
2090 #define RE_UNWIND_BRANCH 1
2091 #define RE_UNWIND_BRANCHJ 2
2095 typedef struct { /* XX: makes sense to enlarge it... */
2099 } re_unwind_generic_t;
2112 } re_unwind_branch_t;
2114 typedef union re_unwind_t {
2116 re_unwind_generic_t generic;
2117 re_unwind_branch_t branch;
2120 #define sayYES goto yes
2121 #define sayNO goto no
2122 #define sayNO_ANYOF goto no_anyof
2123 #define sayYES_FINAL goto yes_final
2124 #define sayYES_LOUD goto yes_loud
2125 #define sayNO_FINAL goto no_final
2126 #define sayNO_SILENT goto do_no
2127 #define saySAME(x) if (x) goto yes; else goto no
2129 #define REPORT_CODE_OFF 24
2132 - regmatch - main matching routine
2134 * Conceptually the strategy is simple: check to see whether the current
2135 * node matches, call self recursively to see whether the rest matches,
2136 * and then act accordingly. In practice we make some effort to avoid
2137 * recursion, in particular by going through "ordinary" nodes (that don't
2138 * need to know whether the rest of the match failed) by a loop instead of
2141 /* [lwall] I've hoisted the register declarations to the outer block in order to
2142 * maybe save a little bit of pushing and popping on the stack. It also takes
2143 * advantage of machines that use a register save mask on subroutine entry.
2145 STATIC I32 /* 0 failure, 1 success */
2146 S_regmatch(pTHX_ regnode *prog)
2148 register regnode *scan; /* Current node. */
2149 regnode *next; /* Next node. */
2150 regnode *inner; /* Next node in internal branch. */
2151 register I32 nextchr; /* renamed nextchr - nextchar colides with
2152 function of same name */
2153 register I32 n; /* no or next */
2154 register I32 ln = 0; /* len or last */
2155 register char *s = Nullch; /* operand or save */
2156 register char *locinput = PL_reginput;
2157 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2158 int minmod = 0, sw = 0, logical = 0;
2161 I32 firstcp = PL_savestack_ix;
2163 register bool do_utf8 = PL_reg_match_utf8;
2165 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2166 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2167 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2174 /* Note that nextchr is a byte even in UTF */
2175 nextchr = UCHARAT(locinput);
2177 while (scan != NULL) {
2180 SV *prop = sv_newmortal();
2181 int docolor = *PL_colors[0];
2182 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2183 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2184 /* The part of the string before starttry has one color
2185 (pref0_len chars), between starttry and current
2186 position another one (pref_len - pref0_len chars),
2187 after the current position the third one.
2188 We assume that pref0_len <= pref_len, otherwise we
2189 decrease pref0_len. */
2190 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2191 ? (5 + taill) - l : locinput - PL_bostr;
2194 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2196 pref0_len = pref_len - (locinput - PL_reg_starttry);
2197 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2198 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2199 ? (5 + taill) - pref_len : PL_regeol - locinput);
2200 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2204 if (pref0_len > pref_len)
2205 pref0_len = pref_len;
2206 regprop(prop, scan);
2210 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2211 pref0_len, 60, UNI_DISPLAY_REGEX) :
2212 locinput - pref_len;
2213 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2214 char *s1 = do_utf8 ?
2215 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2216 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2217 locinput - pref_len + pref0_len;
2218 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2219 char *s2 = do_utf8 ?
2220 pv_uni_display(dsv2, (U8*)locinput,
2221 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2223 int len2 = do_utf8 ? strlen(s2) : l;
2224 PerlIO_printf(Perl_debug_log,
2225 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2226 (IV)(locinput - PL_bostr),
2233 (docolor ? "" : "> <"),
2237 15 - l - pref_len + 1,
2239 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2244 next = scan + NEXT_OFF(scan);
2250 if (locinput == PL_bostr || (PL_multiline &&
2251 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2253 /* regtill = regbol; */
2258 if (locinput == PL_bostr ||
2259 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2265 if (locinput == PL_bostr)
2269 if (locinput == PL_reg_ganch)
2279 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2284 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2286 if (PL_regeol - locinput > 1)
2290 if (PL_regeol != locinput)
2294 if (!nextchr && locinput >= PL_regeol)
2297 locinput += PL_utf8skip[nextchr];
2298 if (locinput > PL_regeol)
2300 nextchr = UCHARAT(locinput);
2303 nextchr = UCHARAT(++locinput);
2306 if (!nextchr && locinput >= PL_regeol)
2308 nextchr = UCHARAT(++locinput);
2311 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2314 locinput += PL_utf8skip[nextchr];
2315 if (locinput > PL_regeol)
2317 nextchr = UCHARAT(locinput);
2320 nextchr = UCHARAT(++locinput);
2325 if (do_utf8 != (UTF!=0)) {
2326 /* The target and the pattern have differing utf8ness. */
2332 /* The target is utf8, the pattern is not utf8. */
2336 if (NATIVE_TO_UNI(*(U8*)s) !=
2337 utf8_to_uvuni((U8*)l, &ulen))
2344 /* The target is not utf8, the pattern is utf8. */
2348 if (NATIVE_TO_UNI(*((U8*)l)) !=
2349 utf8_to_uvuni((U8*)s, &ulen))
2356 nextchr = UCHARAT(locinput);
2359 /* The target and the pattern have the same utf8ness. */
2360 /* Inline the first character, for speed. */
2361 if (UCHARAT(s) != nextchr)
2363 if (PL_regeol - locinput < ln)
2365 if (ln > 1 && memNE(s, locinput, ln))
2368 nextchr = UCHARAT(locinput);
2371 PL_reg_flags |= RF_tainted;
2377 if (do_utf8 || UTF) {
2378 /* Either target or the pattern are utf8. */
2380 char *e = PL_regeol;
2382 if (ibcmp_utf8(s, 0, ln, UTF,
2383 l, &e, 0, do_utf8)) {
2384 /* One more case for the sharp s:
2385 * pack("U0U*", 0xDF) =~ /ss/i,
2386 * the 0xC3 0x9F are the UTF-8
2387 * byte sequence for the U+00DF. */
2389 toLOWER(s[0]) == 's' &&
2391 toLOWER(s[1]) == 's' &&
2398 nextchr = UCHARAT(locinput);
2402 /* Neither the target and the pattern are utf8. */
2404 /* Inline the first character, for speed. */
2405 if (UCHARAT(s) != nextchr &&
2406 UCHARAT(s) != ((OP(scan) == EXACTF)
2407 ? PL_fold : PL_fold_locale)[nextchr])
2409 if (PL_regeol - locinput < ln)
2411 if (ln > 1 && (OP(scan) == EXACTF
2412 ? ibcmp(s, locinput, ln)
2413 : ibcmp_locale(s, locinput, ln)))
2416 nextchr = UCHARAT(locinput);
2420 STRLEN inclasslen = PL_regeol - locinput;
2422 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2424 if (locinput >= PL_regeol)
2426 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2427 nextchr = UCHARAT(locinput);
2432 nextchr = UCHARAT(locinput);
2433 if (!REGINCLASS(scan, (U8*)locinput))
2435 if (!nextchr && locinput >= PL_regeol)
2437 nextchr = UCHARAT(++locinput);
2441 /* If we might have the case of the German sharp s
2442 * in a casefolding Unicode character class. */
2444 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2445 locinput += SHARP_S_SKIP;
2446 nextchr = UCHARAT(locinput);
2452 PL_reg_flags |= RF_tainted;
2458 LOAD_UTF8_CHARCLASS(alnum,"a");
2459 if (!(OP(scan) == ALNUM
2460 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2461 : isALNUM_LC_utf8((U8*)locinput)))
2465 locinput += PL_utf8skip[nextchr];
2466 nextchr = UCHARAT(locinput);
2469 if (!(OP(scan) == ALNUM
2470 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2472 nextchr = UCHARAT(++locinput);
2475 PL_reg_flags |= RF_tainted;
2478 if (!nextchr && locinput >= PL_regeol)
2481 LOAD_UTF8_CHARCLASS(alnum,"a");
2482 if (OP(scan) == NALNUM
2483 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2484 : isALNUM_LC_utf8((U8*)locinput))
2488 locinput += PL_utf8skip[nextchr];
2489 nextchr = UCHARAT(locinput);
2492 if (OP(scan) == NALNUM
2493 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2495 nextchr = UCHARAT(++locinput);
2499 PL_reg_flags |= RF_tainted;
2503 /* was last char in word? */
2505 if (locinput == PL_bostr)
2508 U8 *r = reghop((U8*)locinput, -1);
2510 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2512 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2513 ln = isALNUM_uni(ln);
2514 LOAD_UTF8_CHARCLASS(alnum,"a");
2515 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2518 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2519 n = isALNUM_LC_utf8((U8*)locinput);
2523 ln = (locinput != PL_bostr) ?
2524 UCHARAT(locinput - 1) : '\n';
2525 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2527 n = isALNUM(nextchr);
2530 ln = isALNUM_LC(ln);
2531 n = isALNUM_LC(nextchr);
2534 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2535 OP(scan) == BOUNDL))
2539 PL_reg_flags |= RF_tainted;
2545 if (UTF8_IS_CONTINUED(nextchr)) {
2546 LOAD_UTF8_CHARCLASS(space," ");
2547 if (!(OP(scan) == SPACE
2548 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2549 : isSPACE_LC_utf8((U8*)locinput)))
2553 locinput += PL_utf8skip[nextchr];
2554 nextchr = UCHARAT(locinput);
2557 if (!(OP(scan) == SPACE
2558 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2560 nextchr = UCHARAT(++locinput);
2563 if (!(OP(scan) == SPACE
2564 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2566 nextchr = UCHARAT(++locinput);
2570 PL_reg_flags |= RF_tainted;
2573 if (!nextchr && locinput >= PL_regeol)
2576 LOAD_UTF8_CHARCLASS(space," ");
2577 if (OP(scan) == NSPACE
2578 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2579 : isSPACE_LC_utf8((U8*)locinput))
2583 locinput += PL_utf8skip[nextchr];
2584 nextchr = UCHARAT(locinput);
2587 if (OP(scan) == NSPACE
2588 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2590 nextchr = UCHARAT(++locinput);
2593 PL_reg_flags |= RF_tainted;
2599 LOAD_UTF8_CHARCLASS(digit,"0");
2600 if (!(OP(scan) == DIGIT
2601 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2602 : isDIGIT_LC_utf8((U8*)locinput)))
2606 locinput += PL_utf8skip[nextchr];
2607 nextchr = UCHARAT(locinput);
2610 if (!(OP(scan) == DIGIT
2611 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2613 nextchr = UCHARAT(++locinput);
2616 PL_reg_flags |= RF_tainted;
2619 if (!nextchr && locinput >= PL_regeol)
2622 LOAD_UTF8_CHARCLASS(digit,"0");
2623 if (OP(scan) == NDIGIT
2624 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2625 : isDIGIT_LC_utf8((U8*)locinput))
2629 locinput += PL_utf8skip[nextchr];
2630 nextchr = UCHARAT(locinput);
2633 if (OP(scan) == NDIGIT
2634 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2636 nextchr = UCHARAT(++locinput);
2639 if (locinput >= PL_regeol)
2642 LOAD_UTF8_CHARCLASS(mark,"~");
2643 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2645 locinput += PL_utf8skip[nextchr];
2646 while (locinput < PL_regeol &&
2647 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2648 locinput += UTF8SKIP(locinput);
2649 if (locinput > PL_regeol)
2654 nextchr = UCHARAT(locinput);
2657 PL_reg_flags |= RF_tainted;
2661 n = ARG(scan); /* which paren pair */
2662 ln = PL_regstartp[n];
2663 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2664 if (*PL_reglastparen < n || ln == -1)
2665 sayNO; /* Do not match unless seen CLOSEn. */
2666 if (ln == PL_regendp[n])
2670 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2672 char *e = PL_bostr + PL_regendp[n];
2674 * Note that we can't do the "other character" lookup trick as
2675 * in the 8-bit case (no pun intended) because in Unicode we
2676 * have to map both upper and title case to lower case.
2678 if (OP(scan) == REFF) {
2679 STRLEN ulen1, ulen2;
2680 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2681 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2685 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2686 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2687 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2694 nextchr = UCHARAT(locinput);
2698 /* Inline the first character, for speed. */
2699 if (UCHARAT(s) != nextchr &&
2701 (UCHARAT(s) != ((OP(scan) == REFF
2702 ? PL_fold : PL_fold_locale)[nextchr]))))
2704 ln = PL_regendp[n] - ln;
2705 if (locinput + ln > PL_regeol)
2707 if (ln > 1 && (OP(scan) == REF
2708 ? memNE(s, locinput, ln)
2710 ? ibcmp(s, locinput, ln)
2711 : ibcmp_locale(s, locinput, ln))))
2714 nextchr = UCHARAT(locinput);
2725 OP_4tree *oop = PL_op;
2726 COP *ocurcop = PL_curcop;
2727 SV **ocurpad = PL_curpad;
2731 PL_op = (OP_4tree*)PL_regdata->data[n];
2732 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2733 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2734 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2738 CALLRUNOPS(aTHX); /* Scalar context. */
2741 ret = Nullsv; /* protect against empty (?{}) blocks. */
2749 PL_curpad = ocurpad;
2750 PL_curcop = ocurcop;
2752 if (logical == 2) { /* Postponed subexpression. */
2754 MAGIC *mg = Null(MAGIC*);
2756 CHECKPOINT cp, lastcp;
2758 if(SvROK(ret) || SvRMAGICAL(ret)) {
2759 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2762 mg = mg_find(sv, PERL_MAGIC_qr);
2765 re = (regexp *)mg->mg_obj;
2766 (void)ReREFCNT_inc(re);
2770 char *t = SvPV(ret, len);
2772 char *oprecomp = PL_regprecomp;
2773 I32 osize = PL_regsize;
2774 I32 onpar = PL_regnpar;
2777 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2779 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2780 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2782 PL_regprecomp = oprecomp;
2787 PerlIO_printf(Perl_debug_log,
2788 "Entering embedded `%s%.60s%s%s'\n",
2792 (strlen(re->precomp) > 60 ? "..." : ""))
2795 state.prev = PL_reg_call_cc;
2796 state.cc = PL_regcc;
2797 state.re = PL_reg_re;
2801 cp = regcppush(0); /* Save *all* the positions. */
2804 state.ss = PL_savestack_ix;
2805 *PL_reglastparen = 0;
2806 *PL_reglastcloseparen = 0;
2807 PL_reg_call_cc = &state;
2808 PL_reginput = locinput;
2810 /* XXXX This is too dramatic a measure... */
2813 if (regmatch(re->program + 1)) {
2814 /* Even though we succeeded, we need to restore
2815 global variables, since we may be wrapped inside
2816 SUSPEND, thus the match may be not finished yet. */
2818 /* XXXX Do this only if SUSPENDed? */
2819 PL_reg_call_cc = state.prev;
2820 PL_regcc = state.cc;
2821 PL_reg_re = state.re;
2822 cache_re(PL_reg_re);
2824 /* XXXX This is too dramatic a measure... */
2827 /* These are needed even if not SUSPEND. */
2833 REGCP_UNWIND(lastcp);
2835 PL_reg_call_cc = state.prev;
2836 PL_regcc = state.cc;
2837 PL_reg_re = state.re;
2838 cache_re(PL_reg_re);
2840 /* XXXX This is too dramatic a measure... */
2850 sv_setsv(save_scalar(PL_replgv), ret);
2854 n = ARG(scan); /* which paren pair */
2855 PL_reg_start_tmp[n] = locinput;
2860 n = ARG(scan); /* which paren pair */
2861 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2862 PL_regendp[n] = locinput - PL_bostr;
2863 if (n > *PL_reglastparen)
2864 *PL_reglastparen = n;
2865 *PL_reglastcloseparen = n;
2868 n = ARG(scan); /* which paren pair */
2869 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2872 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2874 next = NEXTOPER(NEXTOPER(scan));
2876 next = scan + ARG(scan);
2877 if (OP(next) == IFTHEN) /* Fake one. */
2878 next = NEXTOPER(NEXTOPER(next));
2882 logical = scan->flags;
2884 /*******************************************************************
2885 PL_regcc contains infoblock about the innermost (...)* loop, and
2886 a pointer to the next outer infoblock.
2888 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2890 1) After matching X, regnode for CURLYX is processed;
2892 2) This regnode creates infoblock on the stack, and calls
2893 regmatch() recursively with the starting point at WHILEM node;
2895 3) Each hit of WHILEM node tries to match A and Z (in the order
2896 depending on the current iteration, min/max of {min,max} and
2897 greediness). The information about where are nodes for "A"
2898 and "Z" is read from the infoblock, as is info on how many times "A"
2899 was already matched, and greediness.
2901 4) After A matches, the same WHILEM node is hit again.
2903 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2904 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2905 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2906 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2907 of the external loop.
2909 Currently present infoblocks form a tree with a stem formed by PL_curcc
2910 and whatever it mentions via ->next, and additional attached trees
2911 corresponding to temporarily unset infoblocks as in "5" above.
2913 In the following picture infoblocks for outer loop of
2914 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2915 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2916 infoblocks are drawn below the "reset" infoblock.
2918 In fact in the picture below we do not show failed matches for Z and T
2919 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2920 more obvious *why* one needs to *temporary* unset infoblocks.]
2922 Matched REx position InfoBlocks Comment
2926 Y A)*?Z)*?T x <- O <- I
2927 YA )*?Z)*?T x <- O <- I
2928 YA A)*?Z)*?T x <- O <- I
2929 YAA )*?Z)*?T x <- O <- I
2930 YAA Z)*?T x <- O # Temporary unset I
2933 YAAZ Y(A)*?Z)*?T x <- O
2936 YAAZY (A)*?Z)*?T x <- O
2939 YAAZY A)*?Z)*?T x <- O <- I
2942 YAAZYA )*?Z)*?T x <- O <- I
2945 YAAZYA Z)*?T x <- O # Temporary unset I
2951 YAAZYAZ T x # Temporary unset O
2958 *******************************************************************/
2961 CHECKPOINT cp = PL_savestack_ix;
2962 /* No need to save/restore up to this paren */
2963 I32 parenfloor = scan->flags;
2965 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2967 cc.oldcc = PL_regcc;
2969 /* XXXX Probably it is better to teach regpush to support
2970 parenfloor > PL_regsize... */
2971 if (parenfloor > *PL_reglastparen)
2972 parenfloor = *PL_reglastparen; /* Pessimization... */
2973 cc.parenfloor = parenfloor;
2975 cc.min = ARG1(scan);
2976 cc.max = ARG2(scan);
2977 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2981 PL_reginput = locinput;
2982 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2984 PL_regcc = cc.oldcc;
2990 * This is really hard to understand, because after we match
2991 * what we're trying to match, we must make sure the rest of
2992 * the REx is going to match for sure, and to do that we have
2993 * to go back UP the parse tree by recursing ever deeper. And
2994 * if it fails, we have to reset our parent's current state
2995 * that we can try again after backing off.
2998 CHECKPOINT cp, lastcp;
2999 CURCUR* cc = PL_regcc;
3000 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3002 n = cc->cur + 1; /* how many we know we matched */
3003 PL_reginput = locinput;
3006 PerlIO_printf(Perl_debug_log,
3007 "%*s %ld out of %ld..%ld cc=%lx\n",
3008 REPORT_CODE_OFF+PL_regindent*2, "",
3009 (long)n, (long)cc->min,
3010 (long)cc->max, (long)cc)
3013 /* If degenerate scan matches "", assume scan done. */
3015 if (locinput == cc->lastloc && n >= cc->min) {
3016 PL_regcc = cc->oldcc;
3020 PerlIO_printf(Perl_debug_log,
3021 "%*s empty match detected, try continuation...\n",
3022 REPORT_CODE_OFF+PL_regindent*2, "")
3024 if (regmatch(cc->next))
3032 /* First just match a string of min scans. */
3036 cc->lastloc = locinput;
3037 if (regmatch(cc->scan))
3040 cc->lastloc = lastloc;
3045 /* Check whether we already were at this position.
3046 Postpone detection until we know the match is not
3047 *that* much linear. */
3048 if (!PL_reg_maxiter) {
3049 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3050 PL_reg_leftiter = PL_reg_maxiter;
3052 if (PL_reg_leftiter-- == 0) {
3053 I32 size = (PL_reg_maxiter + 7)/8;
3054 if (PL_reg_poscache) {
3055 if (PL_reg_poscache_size < size) {
3056 Renew(PL_reg_poscache, size, char);
3057 PL_reg_poscache_size = size;
3059 Zero(PL_reg_poscache, size, char);
3062 PL_reg_poscache_size = size;
3063 Newz(29, PL_reg_poscache, size, char);
3066 PerlIO_printf(Perl_debug_log,
3067 "%sDetected a super-linear match, switching on caching%s...\n",
3068 PL_colors[4], PL_colors[5])
3071 if (PL_reg_leftiter < 0) {
3072 I32 o = locinput - PL_bostr, b;
3074 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3077 if (PL_reg_poscache[o] & (1<<b)) {
3079 PerlIO_printf(Perl_debug_log,
3080 "%*s already tried at this position...\n",
3081 REPORT_CODE_OFF+PL_regindent*2, "")
3085 PL_reg_poscache[o] |= (1<<b);
3089 /* Prefer next over scan for minimal matching. */
3092 PL_regcc = cc->oldcc;
3095 cp = regcppush(cc->parenfloor);
3097 if (regmatch(cc->next)) {
3099 sayYES; /* All done. */
3101 REGCP_UNWIND(lastcp);
3107 if (n >= cc->max) { /* Maximum greed exceeded? */
3108 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3109 && !(PL_reg_flags & RF_warned)) {
3110 PL_reg_flags |= RF_warned;
3111 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3112 "Complex regular subexpression recursion",
3119 PerlIO_printf(Perl_debug_log,
3120 "%*s trying longer...\n",
3121 REPORT_CODE_OFF+PL_regindent*2, "")
3123 /* Try scanning more and see if it helps. */
3124 PL_reginput = locinput;
3126 cc->lastloc = locinput;
3127 cp = regcppush(cc->parenfloor);
3129 if (regmatch(cc->scan)) {
3133 REGCP_UNWIND(lastcp);
3136 cc->lastloc = lastloc;
3140 /* Prefer scan over next for maximal matching. */
3142 if (n < cc->max) { /* More greed allowed? */
3143 cp = regcppush(cc->parenfloor);
3145 cc->lastloc = locinput;
3147 if (regmatch(cc->scan)) {
3151 REGCP_UNWIND(lastcp);
3152 regcppop(); /* Restore some previous $<digit>s? */
3153 PL_reginput = locinput;
3155 PerlIO_printf(Perl_debug_log,
3156 "%*s failed, try continuation...\n",
3157 REPORT_CODE_OFF+PL_regindent*2, "")
3160 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3161 && !(PL_reg_flags & RF_warned)) {
3162 PL_reg_flags |= RF_warned;
3163 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3164 "Complex regular subexpression recursion",
3168 /* Failed deeper matches of scan, so see if this one works. */
3169 PL_regcc = cc->oldcc;
3172 if (regmatch(cc->next))
3178 cc->lastloc = lastloc;
3183 next = scan + ARG(scan);
3186 inner = NEXTOPER(NEXTOPER(scan));
3189 inner = NEXTOPER(scan);
3193 if (OP(next) != c1) /* No choice. */
3194 next = inner; /* Avoid recursion. */
3196 I32 lastparen = *PL_reglastparen;
3198 re_unwind_branch_t *uw;
3200 /* Put unwinding data on stack */
3201 unwind1 = SSNEWt(1,re_unwind_branch_t);
3202 uw = SSPTRt(unwind1,re_unwind_branch_t);
3205 uw->type = ((c1 == BRANCH)
3207 : RE_UNWIND_BRANCHJ);
3208 uw->lastparen = lastparen;
3210 uw->locinput = locinput;
3211 uw->nextchr = nextchr;
3213 uw->regindent = ++PL_regindent;
3216 REGCP_SET(uw->lastcp);
3218 /* Now go into the first branch */
3231 /* We suppose that the next guy does not need
3232 backtracking: in particular, it is of constant length,
3233 and has no parenths to influence future backrefs. */
3234 ln = ARG1(scan); /* min to match */
3235 n = ARG2(scan); /* max to match */
3236 paren = scan->flags;
3238 if (paren > PL_regsize)
3240 if (paren > *PL_reglastparen)
3241 *PL_reglastparen = paren;
3243 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3245 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3246 PL_reginput = locinput;
3249 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3251 /* if we matched something zero-length we don't need to
3252 backtrack - capturing parens are already defined, so
3253 the caveat in the maximal case doesn't apply
3255 XXXX if ln == 0, we can redo this check first time
3256 through the following loop
3259 n = ln; /* don't backtrack */
3260 locinput = PL_reginput;
3261 if (HAS_TEXT(next) || JUMPABLE(next)) {
3262 regnode *text_node = next;
3264 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3266 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3268 if (PL_regkind[(U8)OP(text_node)] == REF) {
3270 n = ARG(text_node); /* which paren pair */
3271 ln = PL_regstartp[n];
3272 /* assume yes if we haven't seen CLOSEn */
3274 *PL_reglastparen < n ||
3281 c1 = *(PL_bostr + ln);
3283 else { c1 = (U8)*STRING(text_node); }
3284 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3286 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3287 c2 = PL_fold_locale[c1];
3296 /* This may be improved if l == 0. */
3297 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3298 /* If it could work, try it. */
3300 UCHARAT(PL_reginput) == c1 ||
3301 UCHARAT(PL_reginput) == c2)
3305 PL_regstartp[paren] =
3306 HOPc(PL_reginput, -l) - PL_bostr;
3307 PL_regendp[paren] = PL_reginput - PL_bostr;
3310 PL_regendp[paren] = -1;
3314 REGCP_UNWIND(lastcp);
3316 /* Couldn't or didn't -- move forward. */
3317 PL_reginput = locinput;
3318 if (regrepeat_hard(scan, 1, &l)) {
3320 locinput = PL_reginput;
3327 n = regrepeat_hard(scan, n, &l);
3328 /* if we matched something zero-length we don't need to
3329 backtrack, unless the minimum count is zero and we
3330 are capturing the result - in that case the capture
3331 being defined or not may affect later execution
3333 if (n != 0 && l == 0 && !(paren && ln == 0))
3334 ln = n; /* don't backtrack */
3335 locinput = PL_reginput;
3337 PerlIO_printf(Perl_debug_log,
3338 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3339 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3343 if (HAS_TEXT(next) || JUMPABLE(next)) {
3344 regnode *text_node = next;
3346 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3348 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3350 if (PL_regkind[(U8)OP(text_node)] == REF) {
3352 n = ARG(text_node); /* which paren pair */
3353 ln = PL_regstartp[n];
3354 /* assume yes if we haven't seen CLOSEn */
3356 *PL_reglastparen < n ||
3363 c1 = *(PL_bostr + ln);
3365 else { c1 = (U8)*STRING(text_node); }
3367 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3369 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3370 c2 = PL_fold_locale[c1];
3381 /* If it could work, try it. */
3383 UCHARAT(PL_reginput) == c1 ||
3384 UCHARAT(PL_reginput) == c2)
3387 PerlIO_printf(Perl_debug_log,
3388 "%*s trying tail with n=%"IVdf"...\n",
3389 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3393 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3394 PL_regendp[paren] = PL_reginput - PL_bostr;
3397 PL_regendp[paren] = -1;
3401 REGCP_UNWIND(lastcp);
3403 /* Couldn't or didn't -- back up. */
3405 locinput = HOPc(locinput, -l);
3406 PL_reginput = locinput;
3413 paren = scan->flags; /* Which paren to set */
3414 if (paren > PL_regsize)
3416 if (paren > *PL_reglastparen)
3417 *PL_reglastparen = paren;
3418 ln = ARG1(scan); /* min to match */
3419 n = ARG2(scan); /* max to match */
3420 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3424 ln = ARG1(scan); /* min to match */
3425 n = ARG2(scan); /* max to match */
3426 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3431 scan = NEXTOPER(scan);
3437 scan = NEXTOPER(scan);
3441 * Lookahead to avoid useless match attempts
3442 * when we know what character comes next.
3446 * Used to only do .*x and .*?x, but now it allows
3447 * for )'s, ('s and (?{ ... })'s to be in the way
3448 * of the quantifier and the EXACT-like node. -- japhy
3451 if (HAS_TEXT(next) || JUMPABLE(next)) {
3453 regnode *text_node = next;
3455 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3457 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3459 if (PL_regkind[(U8)OP(text_node)] == REF) {
3461 n = ARG(text_node); /* which paren pair */
3462 ln = PL_regstartp[n];
3463 /* assume yes if we haven't seen CLOSEn */
3465 *PL_reglastparen < n ||
3470 goto assume_ok_easy;
3472 s = (U8*)PL_bostr + ln;
3474 else { s = (U8*)STRING(text_node); }
3478 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3480 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3481 c2 = PL_fold_locale[c1];
3484 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3485 STRLEN ulen1, ulen2;
3486 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3487 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3489 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3490 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3492 c1 = utf8_to_uvuni(tmpbuf1, 0);
3493 c2 = utf8_to_uvuni(tmpbuf2, 0);
3496 c2 = c1 = utf8_to_uvchr(s, NULL);
3504 PL_reginput = locinput;
3508 if (ln && regrepeat(scan, ln) < ln)
3510 locinput = PL_reginput;
3513 char *e; /* Should not check after this */
3514 char *old = locinput;
3516 if (n == REG_INFTY) {
3519 while (UTF8_IS_CONTINUATION(*(U8*)e))
3525 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3529 e = locinput + n - ln;
3535 /* Find place 'next' could work */
3538 while (locinput <= e &&
3539 UCHARAT(locinput) != c1)
3542 while (locinput <= e
3543 && UCHARAT(locinput) != c1
3544 && UCHARAT(locinput) != c2)
3547 count = locinput - old;
3554 utf8_to_uvchr((U8*)locinput, &len) != c1;
3559 for (count = 0; locinput <= e; count++) {
3560 UV c = utf8_to_uvchr((U8*)locinput, &len);
3561 if (c == c1 || c == c2)
3569 /* PL_reginput == old now */
3570 if (locinput != old) {
3571 ln = 1; /* Did some */
3572 if (regrepeat(scan, count) < count)
3575 /* PL_reginput == locinput now */
3576 TRYPAREN(paren, ln, locinput);
3577 PL_reginput = locinput; /* Could be reset... */
3578 REGCP_UNWIND(lastcp);
3579 /* Couldn't or didn't -- move forward. */
3582 locinput += UTF8SKIP(locinput);
3588 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3592 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3594 c = UCHARAT(PL_reginput);
3595 /* If it could work, try it. */
3596 if (c == c1 || c == c2)
3598 TRYPAREN(paren, n, PL_reginput);
3599 REGCP_UNWIND(lastcp);
3602 /* If it could work, try it. */
3603 else if (c1 == -1000)
3605 TRYPAREN(paren, n, PL_reginput);
3606 REGCP_UNWIND(lastcp);
3608 /* Couldn't or didn't -- move forward. */
3609 PL_reginput = locinput;
3610 if (regrepeat(scan, 1)) {
3612 locinput = PL_reginput;
3620 n = regrepeat(scan, n);
3621 locinput = PL_reginput;
3622 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3623 ((!PL_multiline && OP(next) != MEOL) ||
3624 OP(next) == SEOL || OP(next) == EOS))
3626 ln = n; /* why back off? */
3627 /* ...because $ and \Z can match before *and* after
3628 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3629 We should back off by one in this case. */
3630 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3639 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3641 c = UCHARAT(PL_reginput);
3643 /* If it could work, try it. */
3644 if (c1 == -1000 || c == c1 || c == c2)
3646 TRYPAREN(paren, n, PL_reginput);
3647 REGCP_UNWIND(lastcp);
3649 /* Couldn't or didn't -- back up. */
3651 PL_reginput = locinput = HOPc(locinput, -1);
3659 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3661 c = UCHARAT(PL_reginput);
3663 /* If it could work, try it. */
3664 if (c1 == -1000 || c == c1 || c == c2)
3666 TRYPAREN(paren, n, PL_reginput);
3667 REGCP_UNWIND(lastcp);
3669 /* Couldn't or didn't -- back up. */
3671 PL_reginput = locinput = HOPc(locinput, -1);
3678 if (PL_reg_call_cc) {
3679 re_cc_state *cur_call_cc = PL_reg_call_cc;
3680 CURCUR *cctmp = PL_regcc;
3681 regexp *re = PL_reg_re;
3682 CHECKPOINT cp, lastcp;
3684 cp = regcppush(0); /* Save *all* the positions. */
3686 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3688 PL_reginput = locinput; /* Make position available to
3690 cache_re(PL_reg_call_cc->re);
3691 PL_regcc = PL_reg_call_cc->cc;
3692 PL_reg_call_cc = PL_reg_call_cc->prev;
3693 if (regmatch(cur_call_cc->node)) {
3694 PL_reg_call_cc = cur_call_cc;
3698 REGCP_UNWIND(lastcp);
3700 PL_reg_call_cc = cur_call_cc;
3706 PerlIO_printf(Perl_debug_log,
3707 "%*s continuation failed...\n",
3708 REPORT_CODE_OFF+PL_regindent*2, "")
3712 if (locinput < PL_regtill) {
3713 DEBUG_r(PerlIO_printf(Perl_debug_log,
3714 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3716 (long)(locinput - PL_reg_starttry),
3717 (long)(PL_regtill - PL_reg_starttry),
3719 sayNO_FINAL; /* Cannot match: too short. */
3721 PL_reginput = locinput; /* put where regtry can find it */
3722 sayYES_FINAL; /* Success! */
3724 PL_reginput = locinput; /* put where regtry can find it */
3725 sayYES_LOUD; /* Success! */
3728 PL_reginput = locinput;
3733 s = HOPBACKc(locinput, scan->flags);
3739 PL_reginput = locinput;
3744 s = HOPBACKc(locinput, scan->flags);
3750 PL_reginput = locinput;
3753 inner = NEXTOPER(NEXTOPER(scan));
3754 if (regmatch(inner) != n) {
3769 if (OP(scan) == SUSPEND) {
3770 locinput = PL_reginput;
3771 nextchr = UCHARAT(locinput);
3776 next = scan + ARG(scan);
3781 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3782 PTR2UV(scan), OP(scan));
3783 Perl_croak(aTHX_ "regexp memory corruption");
3790 * We get here only if there's trouble -- normally "case END" is
3791 * the terminating point.
3793 Perl_croak(aTHX_ "corrupted regexp pointers");
3799 PerlIO_printf(Perl_debug_log,
3800 "%*s %scould match...%s\n",
3801 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3805 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3806 PL_colors[4],PL_colors[5]));
3812 #if 0 /* Breaks $^R */
3820 PerlIO_printf(Perl_debug_log,
3821 "%*s %sfailed...%s\n",
3822 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3828 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3831 case RE_UNWIND_BRANCH:
3832 case RE_UNWIND_BRANCHJ:
3834 re_unwind_branch_t *uwb = &(uw->branch);
3835 I32 lastparen = uwb->lastparen;
3837 REGCP_UNWIND(uwb->lastcp);
3838 for (n = *PL_reglastparen; n > lastparen; n--)
3840 *PL_reglastparen = n;
3841 scan = next = uwb->next;
3843 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3844 ? BRANCH : BRANCHJ) ) { /* Failure */
3851 /* Have more choice yet. Reuse the same uwb. */
3853 if ((n = (uwb->type == RE_UNWIND_BRANCH
3854 ? NEXT_OFF(next) : ARG(next))))
3857 next = NULL; /* XXXX Needn't unwinding in this case... */
3859 next = NEXTOPER(scan);
3860 if (uwb->type == RE_UNWIND_BRANCHJ)
3861 next = NEXTOPER(next);
3862 locinput = uwb->locinput;
3863 nextchr = uwb->nextchr;
3865 PL_regindent = uwb->regindent;
3872 Perl_croak(aTHX_ "regexp unwind memory corruption");
3883 - regrepeat - repeatedly match something simple, report how many
3886 * [This routine now assumes that it will only match on things of length 1.
3887 * That was true before, but now we assume scan - reginput is the count,
3888 * rather than incrementing count on every character. [Er, except utf8.]]
3891 S_regrepeat(pTHX_ regnode *p, I32 max)
3893 register char *scan;
3895 register char *loceol = PL_regeol;
3896 register I32 hardcount = 0;
3897 register bool do_utf8 = PL_reg_match_utf8;
3900 if (max != REG_INFTY && max < loceol - scan)
3901 loceol = scan + max;
3906 while (scan < loceol && hardcount < max && *scan != '\n') {
3907 scan += UTF8SKIP(scan);
3911 while (scan < loceol && *scan != '\n')
3918 while (scan < loceol && hardcount < max) {
3919 scan += UTF8SKIP(scan);
3929 case EXACT: /* length of string is 1 */
3931 while (scan < loceol && UCHARAT(scan) == c)
3934 case EXACTF: /* length of string is 1 */
3936 while (scan < loceol &&
3937 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3940 case EXACTFL: /* length of string is 1 */
3941 PL_reg_flags |= RF_tainted;
3943 while (scan < loceol &&
3944 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3950 while (hardcount < max && scan < loceol &&
3951 reginclass(p, (U8*)scan, 0, do_utf8)) {
3952 scan += UTF8SKIP(scan);
3956 while (scan < loceol && REGINCLASS(p, (U8*)scan))
3963 LOAD_UTF8_CHARCLASS(alnum,"a");
3964 while (hardcount < max && scan < loceol &&
3965 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3966 scan += UTF8SKIP(scan);
3970 while (scan < loceol && isALNUM(*scan))
3975 PL_reg_flags |= RF_tainted;
3978 while (hardcount < max && scan < loceol &&
3979 isALNUM_LC_utf8((U8*)scan)) {
3980 scan += UTF8SKIP(scan);
3984 while (scan < loceol && isALNUM_LC(*scan))
3991 LOAD_UTF8_CHARCLASS(alnum,"a");
3992 while (hardcount < max && scan < loceol &&
3993 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
3994 scan += UTF8SKIP(scan);
3998 while (scan < loceol && !isALNUM(*scan))
4003 PL_reg_flags |= RF_tainted;
4006 while (hardcount < max && scan < loceol &&
4007 !isALNUM_LC_utf8((U8*)scan)) {
4008 scan += UTF8SKIP(scan);
4012 while (scan < loceol && !isALNUM_LC(*scan))
4019 LOAD_UTF8_CHARCLASS(space," ");
4020 while (hardcount < max && scan < loceol &&
4022 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4023 scan += UTF8SKIP(scan);
4027 while (scan < loceol && isSPACE(*scan))
4032 PL_reg_flags |= RF_tainted;
4035 while (hardcount < max && scan < loceol &&
4036 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4037 scan += UTF8SKIP(scan);
4041 while (scan < loceol && isSPACE_LC(*scan))
4048 LOAD_UTF8_CHARCLASS(space," ");
4049 while (hardcount < max && scan < loceol &&
4051 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4052 scan += UTF8SKIP(scan);
4056 while (scan < loceol && !isSPACE(*scan))
4061 PL_reg_flags |= RF_tainted;
4064 while (hardcount < max && scan < loceol &&
4065 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4066 scan += UTF8SKIP(scan);
4070 while (scan < loceol && !isSPACE_LC(*scan))
4077 LOAD_UTF8_CHARCLASS(digit,"0");
4078 while (hardcount < max && scan < loceol &&
4079 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4080 scan += UTF8SKIP(scan);
4084 while (scan < loceol && isDIGIT(*scan))
4091 LOAD_UTF8_CHARCLASS(digit,"0");
4092 while (hardcount < max && scan < loceol &&
4093 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4094 scan += UTF8SKIP(scan);
4098 while (scan < loceol && !isDIGIT(*scan))
4102 default: /* Called on something of 0 width. */
4103 break; /* So match right here or not at all. */
4109 c = scan - PL_reginput;
4114 SV *prop = sv_newmortal();
4117 PerlIO_printf(Perl_debug_log,
4118 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4119 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4126 - regrepeat_hard - repeatedly match something, report total lenth and length
4128 * The repeater is supposed to have constant length.
4132 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4134 register char *scan = Nullch;
4135 register char *start;
4136 register char *loceol = PL_regeol;
4138 I32 count = 0, res = 1;
4143 start = PL_reginput;
4144 if (PL_reg_match_utf8) {
4145 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4148 while (start < PL_reginput) {
4150 start += UTF8SKIP(start);
4161 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4163 *lp = l = PL_reginput - start;
4164 if (max != REG_INFTY && l*max < loceol - scan)
4165 loceol = scan + l*max;
4178 - regclass_swash - prepare the utf8 swash
4182 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4188 if (PL_regdata && PL_regdata->count) {
4191 if (PL_regdata->what[n] == 's') {
4192 SV *rv = (SV*)PL_regdata->data[n];
4193 AV *av = (AV*)SvRV((SV*)rv);
4196 /* See the end of regcomp.c:S_reglass() for
4197 * documentation of these array elements. */
4199 si = *av_fetch(av, 0, FALSE);
4200 a = av_fetch(av, 1, FALSE);
4201 b = av_fetch(av, 2, FALSE);
4205 else if (si && doinit) {
4206 sw = swash_init("utf8", "", si, 1, 0);
4207 (void)av_store(av, 1, sw);
4223 - reginclass - determine if a character falls into a character class
4225 The n is the ANYOF regnode, the p is the target string, lenp
4226 is pointer to the maximum length of how far to go in the p
4227 (if the lenp is zero, UTF8SKIP(p) is used),
4228 do_utf8 tells whether the target string is in UTF-8.
4233 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4235 char flags = ANYOF_FLAGS(n);
4241 c = do_utf8 ? utf8_to_uvchr(p, &len) : *p;
4243 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4244 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4247 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4248 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4251 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4255 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4258 if (swash_fetch(sw, p, do_utf8))
4260 else if (flags & ANYOF_FOLD) {
4261 if (!match && lenp && av) {
4264 for (i = 0; i <= av_len(av); i++) {
4265 SV* sv = *av_fetch(av, i, FALSE);
4267 char *s = SvPV(sv, len);
4269 if (len <= plen && memEQ(s, (char*)p, len)) {
4277 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4280 to_utf8_fold(p, tmpbuf, &tmplen);
4281 if (swash_fetch(sw, tmpbuf, do_utf8))
4287 if (match && lenp && *lenp == 0)
4288 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4290 if (!match && c < 256) {
4291 if (ANYOF_BITMAP_TEST(n, c))
4293 else if (flags & ANYOF_FOLD) {
4296 if (flags & ANYOF_LOCALE) {
4297 PL_reg_flags |= RF_tainted;
4298 f = PL_fold_locale[c];
4302 if (f != c && ANYOF_BITMAP_TEST(n, f))
4306 if (!match && (flags & ANYOF_CLASS)) {
4307 PL_reg_flags |= RF_tainted;
4309 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4310 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4311 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4312 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4313 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4314 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4315 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4316 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4317 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4318 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4319 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4320 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4321 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4322 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4323 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4324 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4325 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4326 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4327 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4328 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4329 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4330 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4331 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4332 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4333 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4334 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4335 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4336 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4337 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4338 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4339 ) /* How's that for a conditional? */
4346 return (flags & ANYOF_INVERT) ? !match : match;
4350 S_reghop(pTHX_ U8 *s, I32 off)
4352 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4356 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4359 while (off-- && s < lim) {
4360 /* XXX could check well-formedness here */
4368 if (UTF8_IS_CONTINUED(*s)) {
4369 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4372 /* XXX could check well-formedness here */
4380 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4382 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4386 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4389 while (off-- && s < lim) {
4390 /* XXX could check well-formedness here */
4400 if (UTF8_IS_CONTINUED(*s)) {
4401 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4404 /* XXX could check well-formedness here */
4416 restore_pos(pTHX_ void *arg)
4418 if (PL_reg_eval_set) {
4419 if (PL_reg_oldsaved) {
4420 PL_reg_re->subbeg = PL_reg_oldsaved;
4421 PL_reg_re->sublen = PL_reg_oldsavedlen;
4422 RX_MATCH_COPIED_on(PL_reg_re);
4424 PL_reg_magic->mg_len = PL_reg_oldpos;
4425 PL_reg_eval_set = 0;
4426 PL_curpm = PL_reg_oldcurpm;