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) != 0)
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) (do_utf8 ? 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 ((I32)(*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; (I32)paren <= PL_regnpar; paren++) {
260 if ((I32)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;
395 int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
397 register char *other_last = Nullch; /* other substr checked before this */
398 char *check_at = Nullch; /* check substr found at this pos */
400 char *i_strpos = strpos;
401 SV *dsv = PERL_DEBUG_PAD_ZERO(0);
404 if (prog->reganch & ROPT_UTF8) {
405 DEBUG_r(PerlIO_printf(Perl_debug_log,
406 "UTF-8 regex...\n"));
407 PL_reg_flags |= RF_utf8;
411 char *s = PL_reg_match_utf8 ?
412 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
414 int len = PL_reg_match_utf8 ?
415 strlen(s) : strend - strpos;
418 if (PL_reg_match_utf8)
419 DEBUG_r(PerlIO_printf(Perl_debug_log,
420 "UTF-8 target...\n"));
421 PerlIO_printf(Perl_debug_log,
422 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
423 PL_colors[4],PL_colors[5],PL_colors[0],
426 (strlen(prog->precomp) > 60 ? "..." : ""),
428 (int)(len > 60 ? 60 : len),
430 (len > 60 ? "..." : "")
434 /* CHR_DIST() would be more correct here but it makes things slow. */
435 if (prog->minlen > strend - strpos) {
436 DEBUG_r(PerlIO_printf(Perl_debug_log,
437 "String too short... [re_intuit_start]\n"));
440 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
443 if (!prog->check_utf8 && prog->check_substr)
444 to_utf8_substr(prog);
445 check = prog->check_utf8;
447 if (!prog->check_substr && prog->check_utf8)
448 to_byte_substr(prog);
449 check = prog->check_substr;
451 if (check == &PL_sv_undef) {
452 DEBUG_r(PerlIO_printf(Perl_debug_log,
453 "Non-utf string cannot match utf check string\n"));
456 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
457 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
458 || ( (prog->reganch & ROPT_ANCH_BOL)
459 && !PL_multiline ) ); /* Check after \n? */
462 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
463 | ROPT_IMPLICIT)) /* not a real BOL */
464 /* SvCUR is not set on references: SvRV and SvPVX overlap */
466 && (strpos != strbeg)) {
467 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
470 if (prog->check_offset_min == prog->check_offset_max &&
471 !(prog->reganch & ROPT_CANY_SEEN)) {
472 /* Substring at constant offset from beg-of-str... */
475 s = HOP3c(strpos, prog->check_offset_min, strend);
477 slen = SvCUR(check); /* >= 1 */
479 if ( strend - s > slen || strend - s < slen - 1
480 || (strend - s == slen && strend[-1] != '\n')) {
481 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
484 /* Now should match s[0..slen-2] */
486 if (slen && (*SvPVX(check) != *s
488 && memNE(SvPVX(check), s, slen)))) {
490 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
494 else if (*SvPVX(check) != *s
495 || ((slen = SvCUR(check)) > 1
496 && memNE(SvPVX(check), s, slen)))
498 goto success_at_start;
501 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
503 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
504 end_shift = prog->minlen - start_shift -
505 CHR_SVLEN(check) + (SvTAIL(check) != 0);
507 I32 end = prog->check_offset_max + CHR_SVLEN(check)
508 - (SvTAIL(check) != 0);
509 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
511 if (end_shift < eshift)
515 else { /* Can match at random position */
518 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
519 /* Should be nonnegative! */
520 end_shift = prog->minlen - start_shift -
521 CHR_SVLEN(check) + (SvTAIL(check) != 0);
524 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
526 Perl_croak(aTHX_ "panic: end_shift");
530 /* Find a possible match in the region s..strend by looking for
531 the "check" substring in the region corrected by start/end_shift. */
532 if (flags & REXEC_SCREAM) {
533 I32 p = -1; /* Internal iterator of scream. */
534 I32 *pp = data ? data->scream_pos : &p;
536 if (PL_screamfirst[BmRARE(check)] >= 0
537 || ( BmRARE(check) == '\n'
538 && (BmPREVIOUS(check) == SvCUR(check) - 1)
540 s = screaminstr(sv, check,
541 start_shift + (s - strbeg), end_shift, pp, 0);
545 *data->scream_olds = s;
547 else if (prog->reganch & ROPT_CANY_SEEN)
548 s = fbm_instr((U8*)(s + start_shift),
549 (U8*)(strend - end_shift),
550 check, PL_multiline ? FBMrf_MULTILINE : 0);
552 s = fbm_instr(HOP3(s, start_shift, strend),
553 HOP3(strend, -end_shift, strbeg),
554 check, PL_multiline ? FBMrf_MULTILINE : 0);
556 /* Update the count-of-usability, remove useless subpatterns,
559 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
560 (s ? "Found" : "Did not find"),
561 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
563 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
565 PL_colors[1], (SvTAIL(check) ? "$" : ""),
566 (s ? " at offset " : "...\n") ) );
573 /* Finish the diagnostic message */
574 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
576 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
577 Start with the other substr.
578 XXXX no SCREAM optimization yet - and a very coarse implementation
579 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
580 *always* match. Probably should be marked during compile...
581 Probably it is right to do no SCREAM here...
584 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
585 /* Take into account the "other" substring. */
586 /* XXXX May be hopelessly wrong for UTF... */
589 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
592 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
596 t = s - prog->check_offset_max;
597 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
599 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
604 t = HOP3c(t, prog->anchored_offset, strend);
605 if (t < other_last) /* These positions already checked */
607 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
610 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
611 /* On end-of-str: see comment below. */
612 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
613 if (must == &PL_sv_undef) {
615 DEBUG_r(must = prog->anchored_utf8); /* for debug */
620 HOP3(HOP3(last1, prog->anchored_offset, strend)
621 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
623 PL_multiline ? FBMrf_MULTILINE : 0
625 DEBUG_r(PerlIO_printf(Perl_debug_log,
626 "%s anchored substr `%s%.*s%s'%s",
627 (s ? "Found" : "Contradicts"),
630 - (SvTAIL(must)!=0)),
632 PL_colors[1], (SvTAIL(must) ? "$" : "")));
634 if (last1 >= last2) {
635 DEBUG_r(PerlIO_printf(Perl_debug_log,
636 ", giving up...\n"));
639 DEBUG_r(PerlIO_printf(Perl_debug_log,
640 ", trying floating at offset %ld...\n",
641 (long)(HOP3c(s1, 1, strend) - i_strpos)));
642 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
643 s = HOP3c(last, 1, strend);
647 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
648 (long)(s - i_strpos)));
649 t = HOP3c(s, -prog->anchored_offset, strbeg);
650 other_last = HOP3c(s, 1, strend);
658 else { /* Take into account the floating substring. */
663 t = HOP3c(s, -start_shift, strbeg);
665 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
666 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
667 last = HOP3c(t, prog->float_max_offset, strend);
668 s = HOP3c(t, prog->float_min_offset, strend);
671 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
672 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
673 /* fbm_instr() takes into account exact value of end-of-str
674 if the check is SvTAIL(ed). Since false positives are OK,
675 and end-of-str is not later than strend we are OK. */
676 if (must == &PL_sv_undef) {
678 DEBUG_r(must = prog->float_utf8); /* for debug message */
681 s = fbm_instr((unsigned char*)s,
682 (unsigned char*)last + SvCUR(must)
684 must, PL_multiline ? FBMrf_MULTILINE : 0);
685 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
686 (s ? "Found" : "Contradicts"),
688 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
690 PL_colors[1], (SvTAIL(must) ? "$" : "")));
693 DEBUG_r(PerlIO_printf(Perl_debug_log,
694 ", giving up...\n"));
697 DEBUG_r(PerlIO_printf(Perl_debug_log,
698 ", trying anchored starting at offset %ld...\n",
699 (long)(s1 + 1 - i_strpos)));
701 s = HOP3c(t, 1, strend);
705 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
706 (long)(s - i_strpos)));
707 other_last = s; /* Fix this later. --Hugo */
716 t = s - prog->check_offset_max;
717 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
719 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
721 /* Fixed substring is found far enough so that the match
722 cannot start at strpos. */
724 if (ml_anch && t[-1] != '\n') {
725 /* Eventually fbm_*() should handle this, but often
726 anchored_offset is not 0, so this check will not be wasted. */
727 /* XXXX In the code below we prefer to look for "^" even in
728 presence of anchored substrings. And we search even
729 beyond the found float position. These pessimizations
730 are historical artefacts only. */
732 while (t < strend - prog->minlen) {
734 if (t < check_at - prog->check_offset_min) {
735 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
736 /* Since we moved from the found position,
737 we definitely contradict the found anchored
738 substr. Due to the above check we do not
739 contradict "check" substr.
740 Thus we can arrive here only if check substr
741 is float. Redo checking for "other"=="fixed".
744 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
745 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
746 goto do_other_anchored;
748 /* We don't contradict the found floating substring. */
749 /* XXXX Why not check for STCLASS? */
751 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
752 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
755 /* Position contradicts check-string */
756 /* XXXX probably better to look for check-string
757 than for "\n", so one should lower the limit for t? */
758 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
759 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
760 other_last = strpos = s = t + 1;
765 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
766 PL_colors[0],PL_colors[1]));
770 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
771 PL_colors[0],PL_colors[1]));
775 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
778 /* The found string does not prohibit matching at strpos,
779 - no optimization of calling REx engine can be performed,
780 unless it was an MBOL and we are not after MBOL,
781 or a future STCLASS check will fail this. */
783 /* Even in this situation we may use MBOL flag if strpos is offset
784 wrt the start of the string. */
785 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
786 && (strpos != strbeg) && strpos[-1] != '\n'
787 /* May be due to an implicit anchor of m{.*foo} */
788 && !(prog->reganch & ROPT_IMPLICIT))
793 DEBUG_r( if (ml_anch)
794 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
795 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
798 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
800 prog->check_utf8 /* Could be deleted already */
801 && --BmUSEFUL(prog->check_utf8) < 0
802 && (prog->check_utf8 == prog->float_utf8)
804 prog->check_substr /* Could be deleted already */
805 && --BmUSEFUL(prog->check_substr) < 0
806 && (prog->check_substr == prog->float_substr)
809 /* If flags & SOMETHING - do not do it many times on the same match */
810 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
811 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
812 if (do_utf8 ? prog->check_substr : prog->check_utf8)
813 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
814 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
815 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
816 check = Nullsv; /* abort */
818 /* XXXX This is a remnant of the old implementation. It
819 looks wasteful, since now INTUIT can use many
821 prog->reganch &= ~RE_USE_INTUIT;
828 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
829 if (prog->regstclass) {
830 /* minlen == 0 is possible if regstclass is \b or \B,
831 and the fixed substr is ''$.
832 Since minlen is already taken into account, s+1 is before strend;
833 accidentally, minlen >= 1 guaranties no false positives at s + 1
834 even for \b or \B. But (minlen? 1 : 0) below assumes that
835 regstclass does not come from lookahead... */
836 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
837 This leaves EXACTF only, which is dealt with in find_byclass(). */
838 U8* str = (U8*)STRING(prog->regstclass);
839 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
840 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
842 char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
843 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
844 : (prog->float_substr || prog->float_utf8
845 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
848 char *startpos = strbeg;
851 if (prog->reganch & ROPT_UTF8) {
852 PL_regdata = prog->data;
855 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
860 if (endpos == strend) {
861 DEBUG_r( PerlIO_printf(Perl_debug_log,
862 "Could not match STCLASS...\n") );
865 DEBUG_r( PerlIO_printf(Perl_debug_log,
866 "This position contradicts STCLASS...\n") );
867 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
869 /* Contradict one of substrings */
870 if (prog->anchored_substr || prog->anchored_utf8) {
871 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
872 DEBUG_r( what = "anchored" );
874 s = HOP3c(t, 1, strend);
875 if (s + start_shift + end_shift > strend) {
876 /* XXXX Should be taken into account earlier? */
877 DEBUG_r( PerlIO_printf(Perl_debug_log,
878 "Could not match STCLASS...\n") );
883 DEBUG_r( PerlIO_printf(Perl_debug_log,
884 "Looking for %s substr starting at offset %ld...\n",
885 what, (long)(s + start_shift - i_strpos)) );
888 /* Have both, check_string is floating */
889 if (t + start_shift >= check_at) /* Contradicts floating=check */
890 goto retry_floating_check;
891 /* Recheck anchored substring, but not floating... */
895 DEBUG_r( PerlIO_printf(Perl_debug_log,
896 "Looking for anchored substr starting at offset %ld...\n",
897 (long)(other_last - i_strpos)) );
898 goto do_other_anchored;
900 /* Another way we could have checked stclass at the
901 current position only: */
906 DEBUG_r( PerlIO_printf(Perl_debug_log,
907 "Looking for /%s^%s/m starting at offset %ld...\n",
908 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
911 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
913 /* Check is floating subtring. */
914 retry_floating_check:
915 t = check_at - start_shift;
916 DEBUG_r( what = "floating" );
917 goto hop_and_restart;
920 DEBUG_r(PerlIO_printf(Perl_debug_log,
921 "By STCLASS: moving %ld --> %ld\n",
922 (long)(t - i_strpos), (long)(s - i_strpos))
926 DEBUG_r(PerlIO_printf(Perl_debug_log,
927 "Does not contradict STCLASS...\n");
932 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
933 PL_colors[4], (check ? "Guessed" : "Giving up"),
934 PL_colors[5], (long)(s - i_strpos)) );
937 fail_finish: /* Substring not found */
938 if (prog->check_substr || prog->check_utf8) /* could be removed already */
939 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
941 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
942 PL_colors[4],PL_colors[5]));
946 /* We know what class REx starts with. Try to find this position... */
948 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
950 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
956 register I32 tmp = 1; /* Scratch variable? */
957 register bool do_utf8 = PL_reg_match_utf8;
959 /* We know what class it must start with. */
964 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
965 !UTF8_IS_INVARIANT((U8)s[0]) ?
966 reginclass(c, (U8*)s, 0, do_utf8) :
967 REGINCLASS(c, (U8*)s)) {
968 if (tmp && (norun || regtry(prog, s)))
982 if (REGINCLASS(c, (U8*)s) ||
983 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
984 /* The assignment of 2 is intentional:
985 * for the folded sharp s, the skip is 2. */
986 (skip = SHARP_S_SKIP))) {
987 if (tmp && (norun || regtry(prog, s)))
1000 if (tmp && (norun || regtry(prog, s)))
1011 STRLEN ulen1, ulen2;
1012 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
1013 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1015 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1016 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1018 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1019 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1020 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1021 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1032 c2 = PL_fold_locale[c1];
1034 e = HOP3c(strend, -(I32)ln, s);
1037 e = s; /* Due to minlen logic of intuit() */
1039 /* The idea in the EXACTF* cases is to first find the
1040 * first character of the EXACTF* node and then, if
1041 * necessary, case-insensitively compare the full
1042 * text of the node. The c1 and c2 are the first
1043 * characters (though in Unicode it gets a bit
1044 * more complicated because there are more cases
1045 * than just upper and lower: one needs to use
1046 * the so-called folding case for case-insensitive
1047 * matching (called "loose matching" in Unicode).
1048 * ibcmp_utf8() will do just that. */
1052 U8 tmpbuf [UTF8_MAXLEN+1];
1053 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1054 STRLEN len, foldlen;
1058 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1060 0 : UTF8_ALLOW_ANY);
1063 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1064 m, (char **)0, ln, (bool)UTF))
1065 && (norun || regtry(prog, s)) )
1068 uvchr_to_utf8(tmpbuf, c);
1069 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1071 && (f == c1 || f == c2)
1072 && (ln == foldlen ||
1073 !ibcmp_utf8((char *) foldbuf,
1074 (char **)0, foldlen, do_utf8,
1076 (char **)0, ln, (bool)UTF))
1077 && (norun || regtry(prog, s)) )
1085 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1087 0 : UTF8_ALLOW_ANY);
1089 /* Handle some of the three Greek sigmas cases.
1090 * Note that not all the possible combinations
1091 * are handled here: some of them are handled
1092 * by the standard folding rules, and some of
1093 * them (the character class or ANYOF cases)
1094 * are handled during compiletime in
1095 * regexec.c:S_regclass(). */
1096 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1097 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1098 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1100 if ( (c == c1 || c == c2)
1102 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1103 m, (char **)0, ln, (bool)UTF))
1104 && (norun || regtry(prog, s)) )
1107 uvchr_to_utf8(tmpbuf, c);
1108 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1110 && (f == c1 || f == c2)
1111 && (ln == foldlen ||
1112 !ibcmp_utf8((char *) foldbuf,
1113 (char **)0, foldlen, do_utf8,
1115 (char **)0, ln, (bool)UTF))
1116 && (norun || regtry(prog, s)) )
1127 && (ln == 1 || !(OP(c) == EXACTF
1129 : ibcmp_locale(s, m, ln)))
1130 && (norun || regtry(prog, s)) )
1136 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1137 && (ln == 1 || !(OP(c) == EXACTF
1139 : ibcmp_locale(s, m, ln)))
1140 && (norun || regtry(prog, s)) )
1147 PL_reg_flags |= RF_tainted;
1154 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1156 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1158 tmp = ((OP(c) == BOUND ?
1159 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1160 LOAD_UTF8_CHARCLASS(alnum,"a");
1161 while (s < strend) {
1162 if (tmp == !(OP(c) == BOUND ?
1163 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1164 isALNUM_LC_utf8((U8*)s)))
1167 if ((norun || regtry(prog, s)))
1174 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1175 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1176 while (s < strend) {
1178 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1180 if ((norun || regtry(prog, s)))
1186 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1190 PL_reg_flags |= RF_tainted;
1197 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1199 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1201 tmp = ((OP(c) == NBOUND ?
1202 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1203 LOAD_UTF8_CHARCLASS(alnum,"a");
1204 while (s < strend) {
1205 if (tmp == !(OP(c) == NBOUND ?
1206 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1207 isALNUM_LC_utf8((U8*)s)))
1209 else if ((norun || regtry(prog, s)))
1215 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1216 tmp = ((OP(c) == NBOUND ?
1217 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1218 while (s < strend) {
1220 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1222 else if ((norun || regtry(prog, s)))
1227 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1232 LOAD_UTF8_CHARCLASS(alnum,"a");
1233 while (s < strend) {
1234 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1235 if (tmp && (norun || regtry(prog, s)))
1246 while (s < strend) {
1248 if (tmp && (norun || regtry(prog, s)))
1260 PL_reg_flags |= RF_tainted;
1262 while (s < strend) {
1263 if (isALNUM_LC_utf8((U8*)s)) {
1264 if (tmp && (norun || regtry(prog, s)))
1275 while (s < strend) {
1276 if (isALNUM_LC(*s)) {
1277 if (tmp && (norun || regtry(prog, s)))
1290 LOAD_UTF8_CHARCLASS(alnum,"a");
1291 while (s < strend) {
1292 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1293 if (tmp && (norun || regtry(prog, s)))
1304 while (s < strend) {
1306 if (tmp && (norun || regtry(prog, s)))
1318 PL_reg_flags |= RF_tainted;
1320 while (s < strend) {
1321 if (!isALNUM_LC_utf8((U8*)s)) {
1322 if (tmp && (norun || regtry(prog, s)))
1333 while (s < strend) {
1334 if (!isALNUM_LC(*s)) {
1335 if (tmp && (norun || regtry(prog, s)))
1348 LOAD_UTF8_CHARCLASS(space," ");
1349 while (s < strend) {
1350 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1351 if (tmp && (norun || regtry(prog, s)))
1362 while (s < strend) {
1364 if (tmp && (norun || regtry(prog, s)))
1376 PL_reg_flags |= RF_tainted;
1378 while (s < strend) {
1379 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1380 if (tmp && (norun || regtry(prog, s)))
1391 while (s < strend) {
1392 if (isSPACE_LC(*s)) {
1393 if (tmp && (norun || regtry(prog, s)))
1406 LOAD_UTF8_CHARCLASS(space," ");
1407 while (s < strend) {
1408 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1409 if (tmp && (norun || regtry(prog, s)))
1420 while (s < strend) {
1422 if (tmp && (norun || regtry(prog, s)))
1434 PL_reg_flags |= RF_tainted;
1436 while (s < strend) {
1437 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1438 if (tmp && (norun || regtry(prog, s)))
1449 while (s < strend) {
1450 if (!isSPACE_LC(*s)) {
1451 if (tmp && (norun || regtry(prog, s)))
1464 LOAD_UTF8_CHARCLASS(digit,"0");
1465 while (s < strend) {
1466 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1467 if (tmp && (norun || regtry(prog, s)))
1478 while (s < strend) {
1480 if (tmp && (norun || regtry(prog, s)))
1492 PL_reg_flags |= RF_tainted;
1494 while (s < strend) {
1495 if (isDIGIT_LC_utf8((U8*)s)) {
1496 if (tmp && (norun || regtry(prog, s)))
1507 while (s < strend) {
1508 if (isDIGIT_LC(*s)) {
1509 if (tmp && (norun || regtry(prog, s)))
1522 LOAD_UTF8_CHARCLASS(digit,"0");
1523 while (s < strend) {
1524 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1525 if (tmp && (norun || regtry(prog, s)))
1536 while (s < strend) {
1538 if (tmp && (norun || regtry(prog, s)))
1550 PL_reg_flags |= RF_tainted;
1552 while (s < strend) {
1553 if (!isDIGIT_LC_utf8((U8*)s)) {
1554 if (tmp && (norun || regtry(prog, s)))
1565 while (s < strend) {
1566 if (!isDIGIT_LC(*s)) {
1567 if (tmp && (norun || regtry(prog, s)))
1579 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1588 - regexec_flags - match a regexp against a string
1591 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1592 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1593 /* strend: pointer to null at end of string */
1594 /* strbeg: real beginning of string */
1595 /* minend: end of match must be >=minend after stringarg. */
1596 /* data: May be used for some additional optimizations. */
1597 /* nosave: For optimizations. */
1600 register regnode *c;
1601 register char *startpos = stringarg;
1602 I32 minlen; /* must match at least this many chars */
1603 I32 dontbother = 0; /* how many characters not to try at end */
1604 /* I32 start_shift = 0; */ /* Offset of the start to find
1605 constant substr. */ /* CC */
1606 I32 end_shift = 0; /* Same for the end. */ /* CC */
1607 I32 scream_pos = -1; /* Internal iterator of scream. */
1609 SV* oreplsv = GvSV(PL_replgv);
1610 bool do_utf8 = DO_UTF8(sv);
1612 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1613 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1620 PL_regnarrate = DEBUG_r_TEST;
1623 /* Be paranoid... */
1624 if (prog == NULL || startpos == NULL) {
1625 Perl_croak(aTHX_ "NULL regexp parameter");
1629 minlen = prog->minlen;
1630 if (strend - startpos < minlen) {
1631 DEBUG_r(PerlIO_printf(Perl_debug_log,
1632 "String too short [regexec_flags]...\n"));
1636 /* Check validity of program. */
1637 if (UCHARAT(prog->program) != REG_MAGIC) {
1638 Perl_croak(aTHX_ "corrupted regexp program");
1642 PL_reg_eval_set = 0;
1645 if (prog->reganch & ROPT_UTF8)
1646 PL_reg_flags |= RF_utf8;
1648 /* Mark beginning of line for ^ and lookbehind. */
1649 PL_regbol = startpos;
1653 /* Mark end of line for $ (and such) */
1656 /* see how far we have to get to not match where we matched before */
1657 PL_regtill = startpos+minend;
1659 /* We start without call_cc context. */
1662 /* If there is a "must appear" string, look for it. */
1665 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1668 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1669 PL_reg_ganch = startpos;
1670 else if (sv && SvTYPE(sv) >= SVt_PVMG
1672 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1673 && mg->mg_len >= 0) {
1674 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1675 if (prog->reganch & ROPT_ANCH_GPOS) {
1676 if (s > PL_reg_ganch)
1681 else /* pos() not defined */
1682 PL_reg_ganch = strbeg;
1685 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1686 re_scream_pos_data d;
1688 d.scream_olds = &scream_olds;
1689 d.scream_pos = &scream_pos;
1690 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1692 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1693 goto phooey; /* not present */
1699 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1700 UNI_DISPLAY_REGEX) :
1702 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1703 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1704 UNI_DISPLAY_REGEX) : startpos;
1705 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1708 PerlIO_printf(Perl_debug_log,
1709 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1710 PL_colors[4],PL_colors[5],PL_colors[0],
1713 len0 > 60 ? "..." : "",
1715 (int)(len1 > 60 ? 60 : len1),
1717 (len1 > 60 ? "..." : "")
1721 /* Simplest case: anchored match need be tried only once. */
1722 /* [unless only anchor is BOL and multiline is set] */
1723 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1724 if (s == startpos && regtry(prog, startpos))
1726 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1727 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1732 dontbother = minlen - 1;
1733 end = HOP3c(strend, -dontbother, strbeg) - 1;
1734 /* for multiline we only have to try after newlines */
1735 if (prog->check_substr || prog->check_utf8) {
1739 if (regtry(prog, s))
1744 if (prog->reganch & RE_USE_INTUIT) {
1745 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1756 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1757 if (regtry(prog, s))
1764 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1765 if (regtry(prog, PL_reg_ganch))
1770 /* Messy cases: unanchored match. */
1771 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1772 /* we have /x+whatever/ */
1773 /* it must be a one character string (XXXX Except UTF?) */
1778 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1779 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1780 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1783 while (s < strend) {
1785 DEBUG_r( did_match = 1 );
1786 if (regtry(prog, s)) goto got_it;
1788 while (s < strend && *s == ch)
1795 while (s < strend) {
1797 DEBUG_r( did_match = 1 );
1798 if (regtry(prog, s)) goto got_it;
1800 while (s < strend && *s == ch)
1806 DEBUG_r(if (!did_match)
1807 PerlIO_printf(Perl_debug_log,
1808 "Did not find anchored character...\n")
1812 else if (prog->anchored_substr != Nullsv
1813 || prog->anchored_utf8 != Nullsv
1814 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1815 && prog->float_max_offset < strend - s)) {
1820 char *last1; /* Last position checked before */
1824 if (prog->anchored_substr || prog->anchored_utf8) {
1825 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1826 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1827 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1828 back_max = back_min = prog->anchored_offset;
1830 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1831 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1832 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1833 back_max = prog->float_max_offset;
1834 back_min = prog->float_min_offset;
1836 if (must == &PL_sv_undef)
1837 /* could not downgrade utf8 check substring, so must fail */
1840 last = HOP3c(strend, /* Cannot start after this */
1841 -(I32)(CHR_SVLEN(must)
1842 - (SvTAIL(must) != 0) + back_min), strbeg);
1845 last1 = HOPc(s, -1);
1847 last1 = s - 1; /* bogus */
1849 /* XXXX check_substr already used to find `s', can optimize if
1850 check_substr==must. */
1852 dontbother = end_shift;
1853 strend = HOPc(strend, -dontbother);
1854 while ( (s <= last) &&
1855 ((flags & REXEC_SCREAM)
1856 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1857 end_shift, &scream_pos, 0))
1858 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1859 (unsigned char*)strend, must,
1860 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1861 DEBUG_r( did_match = 1 );
1862 if (HOPc(s, -back_max) > last1) {
1863 last1 = HOPc(s, -back_min);
1864 s = HOPc(s, -back_max);
1867 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1869 last1 = HOPc(s, -back_min);
1873 while (s <= last1) {
1874 if (regtry(prog, s))
1880 while (s <= last1) {
1881 if (regtry(prog, s))
1887 DEBUG_r(if (!did_match)
1888 PerlIO_printf(Perl_debug_log,
1889 "Did not find %s substr `%s%.*s%s'%s...\n",
1890 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1891 ? "anchored" : "floating"),
1893 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1895 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1899 else if ((c = prog->regstclass)) {
1901 I32 op = (U8)OP(prog->regstclass);
1902 /* don't bother with what can't match */
1903 if (PL_regkind[op] != EXACT && op != CANY)
1904 strend = HOPc(strend, -(minlen - 1));
1907 SV *prop = sv_newmortal();
1915 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1916 UNI_DISPLAY_REGEX) :
1918 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1920 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1921 len1 = UTF ? SvCUR(dsv1) : strend - s;
1922 PerlIO_printf(Perl_debug_log,
1923 "Matching stclass `%*.*s' against `%*.*s'\n",
1927 if (find_byclass(prog, c, s, strend, startpos, 0))
1929 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1933 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1938 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1939 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1940 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1942 if (flags & REXEC_SCREAM) {
1943 last = screaminstr(sv, float_real, s - strbeg,
1944 end_shift, &scream_pos, 1); /* last one */
1946 last = scream_olds; /* Only one occurrence. */
1950 char *little = SvPV(float_real, len);
1952 if (SvTAIL(float_real)) {
1953 if (memEQ(strend - len + 1, little, len - 1))
1954 last = strend - len + 1;
1955 else if (!PL_multiline)
1956 last = memEQ(strend - len, little, len)
1957 ? strend - len : Nullch;
1963 last = rninstr(s, strend, little, little + len);
1965 last = strend; /* matching `$' */
1969 DEBUG_r(PerlIO_printf(Perl_debug_log,
1970 "%sCan't trim the tail, match fails (should not happen)%s\n",
1971 PL_colors[4],PL_colors[5]));
1972 goto phooey; /* Should not happen! */
1974 dontbother = strend - last + prog->float_min_offset;
1976 if (minlen && (dontbother < minlen))
1977 dontbother = minlen - 1;
1978 strend -= dontbother; /* this one's always in bytes! */
1979 /* We don't know much -- general case. */
1982 if (regtry(prog, s))
1991 if (regtry(prog, s))
1993 } while (s++ < strend);
2001 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2003 if (PL_reg_eval_set) {
2004 /* Preserve the current value of $^R */
2005 if (oreplsv != GvSV(PL_replgv))
2006 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2007 restored, the value remains
2009 restore_pos(aTHX_ 0);
2012 /* make sure $`, $&, $', and $digit will work later */
2013 if ( !(flags & REXEC_NOT_FIRST) ) {
2014 if (RX_MATCH_COPIED(prog)) {
2015 Safefree(prog->subbeg);
2016 RX_MATCH_COPIED_off(prog);
2018 if (flags & REXEC_COPY_STR) {
2019 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2021 s = savepvn(strbeg, i);
2024 RX_MATCH_COPIED_on(prog);
2027 prog->subbeg = strbeg;
2028 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2035 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2036 PL_colors[4],PL_colors[5]));
2037 if (PL_reg_eval_set)
2038 restore_pos(aTHX_ 0);
2043 - regtry - try match at specific point
2045 STATIC I32 /* 0 failure, 1 success */
2046 S_regtry(pTHX_ regexp *prog, char *startpos)
2054 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2056 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2059 PL_reg_eval_set = RS_init;
2061 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2062 (IV)(PL_stack_sp - PL_stack_base));
2064 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2065 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2066 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2068 /* Apparently this is not needed, judging by wantarray. */
2069 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2070 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2073 /* Make $_ available to executed code. */
2074 if (PL_reg_sv != DEFSV) {
2075 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2080 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2081 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2082 /* prepare for quick setting of pos */
2083 sv_magic(PL_reg_sv, (SV*)0,
2084 PERL_MAGIC_regex_global, Nullch, 0);
2085 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2089 PL_reg_oldpos = mg->mg_len;
2090 SAVEDESTRUCTOR_X(restore_pos, 0);
2092 if (!PL_reg_curpm) {
2093 Newz(22,PL_reg_curpm, 1, PMOP);
2096 SV* repointer = newSViv(0);
2097 /* so we know which PL_regex_padav element is PL_reg_curpm */
2098 SvFLAGS(repointer) |= SVf_BREAK;
2099 av_push(PL_regex_padav,repointer);
2100 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2101 PL_regex_pad = AvARRAY(PL_regex_padav);
2105 PM_SETRE(PL_reg_curpm, prog);
2106 PL_reg_oldcurpm = PL_curpm;
2107 PL_curpm = PL_reg_curpm;
2108 if (RX_MATCH_COPIED(prog)) {
2109 /* Here is a serious problem: we cannot rewrite subbeg,
2110 since it may be needed if this match fails. Thus
2111 $` inside (?{}) could fail... */
2112 PL_reg_oldsaved = prog->subbeg;
2113 PL_reg_oldsavedlen = prog->sublen;
2114 RX_MATCH_COPIED_off(prog);
2117 PL_reg_oldsaved = Nullch;
2118 prog->subbeg = PL_bostr;
2119 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2121 prog->startp[0] = startpos - PL_bostr;
2122 PL_reginput = startpos;
2123 PL_regstartp = prog->startp;
2124 PL_regendp = prog->endp;
2125 PL_reglastparen = &prog->lastparen;
2126 PL_reglastcloseparen = &prog->lastcloseparen;
2127 prog->lastparen = 0;
2128 prog->lastcloseparen = 0;
2130 DEBUG_r(PL_reg_starttry = startpos);
2131 if (PL_reg_start_tmpl <= prog->nparens) {
2132 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2133 if(PL_reg_start_tmp)
2134 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2136 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2139 /* XXXX What this code is doing here?!!! There should be no need
2140 to do this again and again, PL_reglastparen should take care of
2143 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2144 * Actually, the code in regcppop() (which Ilya may be meaning by
2145 * PL_reglastparen), is not needed at all by the test suite
2146 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2147 * enough, for building DynaLoader, or otherwise this
2148 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2149 * will happen. Meanwhile, this code *is* needed for the
2150 * above-mentioned test suite tests to succeed. The common theme
2151 * on those tests seems to be returning null fields from matches.
2156 if (prog->nparens) {
2157 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2164 if (regmatch(prog->program + 1)) {
2165 prog->endp[0] = PL_reginput - PL_bostr;
2168 REGCP_UNWIND(lastcp);
2172 #define RE_UNWIND_BRANCH 1
2173 #define RE_UNWIND_BRANCHJ 2
2177 typedef struct { /* XX: makes sense to enlarge it... */
2181 } re_unwind_generic_t;
2194 } re_unwind_branch_t;
2196 typedef union re_unwind_t {
2198 re_unwind_generic_t generic;
2199 re_unwind_branch_t branch;
2202 #define sayYES goto yes
2203 #define sayNO goto no
2204 #define sayNO_ANYOF goto no_anyof
2205 #define sayYES_FINAL goto yes_final
2206 #define sayYES_LOUD goto yes_loud
2207 #define sayNO_FINAL goto no_final
2208 #define sayNO_SILENT goto do_no
2209 #define saySAME(x) if (x) goto yes; else goto no
2211 #define REPORT_CODE_OFF 24
2214 - regmatch - main matching routine
2216 * Conceptually the strategy is simple: check to see whether the current
2217 * node matches, call self recursively to see whether the rest matches,
2218 * and then act accordingly. In practice we make some effort to avoid
2219 * recursion, in particular by going through "ordinary" nodes (that don't
2220 * need to know whether the rest of the match failed) by a loop instead of
2223 /* [lwall] I've hoisted the register declarations to the outer block in order to
2224 * maybe save a little bit of pushing and popping on the stack. It also takes
2225 * advantage of machines that use a register save mask on subroutine entry.
2227 STATIC I32 /* 0 failure, 1 success */
2228 S_regmatch(pTHX_ regnode *prog)
2230 register regnode *scan; /* Current node. */
2231 regnode *next; /* Next node. */
2232 regnode *inner; /* Next node in internal branch. */
2233 register I32 nextchr; /* renamed nextchr - nextchar colides with
2234 function of same name */
2235 register I32 n; /* no or next */
2236 register I32 ln = 0; /* len or last */
2237 register char *s = Nullch; /* operand or save */
2238 register char *locinput = PL_reginput;
2239 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2240 int minmod = 0, sw = 0, logical = 0;
2243 I32 firstcp = PL_savestack_ix;
2245 register bool do_utf8 = PL_reg_match_utf8;
2247 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2248 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2249 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2256 /* Note that nextchr is a byte even in UTF */
2257 nextchr = UCHARAT(locinput);
2259 while (scan != NULL) {
2262 SV *prop = sv_newmortal();
2263 int docolor = *PL_colors[0];
2264 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2265 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2266 /* The part of the string before starttry has one color
2267 (pref0_len chars), between starttry and current
2268 position another one (pref_len - pref0_len chars),
2269 after the current position the third one.
2270 We assume that pref0_len <= pref_len, otherwise we
2271 decrease pref0_len. */
2272 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2273 ? (5 + taill) - l : locinput - PL_bostr;
2276 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2278 pref0_len = pref_len - (locinput - PL_reg_starttry);
2279 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2280 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2281 ? (5 + taill) - pref_len : PL_regeol - locinput);
2282 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2286 if (pref0_len > pref_len)
2287 pref0_len = pref_len;
2288 regprop(prop, scan);
2291 do_utf8 && OP(scan) != CANY ?
2292 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2293 pref0_len, 60, UNI_DISPLAY_REGEX) :
2294 locinput - pref_len;
2295 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2296 char *s1 = do_utf8 && OP(scan) != CANY ?
2297 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2298 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2299 locinput - pref_len + pref0_len;
2300 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2301 char *s2 = do_utf8 && OP(scan) != CANY ?
2302 pv_uni_display(dsv2, (U8*)locinput,
2303 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2305 int len2 = do_utf8 ? strlen(s2) : l;
2306 PerlIO_printf(Perl_debug_log,
2307 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2308 (IV)(locinput - PL_bostr),
2315 (docolor ? "" : "> <"),
2319 15 - l - pref_len + 1,
2321 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2326 next = scan + NEXT_OFF(scan);
2332 if (locinput == PL_bostr || (PL_multiline &&
2333 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2335 /* regtill = regbol; */
2340 if (locinput == PL_bostr ||
2341 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2347 if (locinput == PL_bostr)
2351 if (locinput == PL_reg_ganch)
2361 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2366 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2368 if (PL_regeol - locinput > 1)
2372 if (PL_regeol != locinput)
2376 if (!nextchr && locinput >= PL_regeol)
2379 locinput += PL_utf8skip[nextchr];
2380 if (locinput > PL_regeol)
2382 nextchr = UCHARAT(locinput);
2385 nextchr = UCHARAT(++locinput);
2388 if (!nextchr && locinput >= PL_regeol)
2390 nextchr = UCHARAT(++locinput);
2393 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2396 locinput += PL_utf8skip[nextchr];
2397 if (locinput > PL_regeol)
2399 nextchr = UCHARAT(locinput);
2402 nextchr = UCHARAT(++locinput);
2407 if (do_utf8 != UTF) {
2408 /* The target and the pattern have differing utf8ness. */
2414 /* The target is utf8, the pattern is not utf8. */
2418 if (NATIVE_TO_UNI(*(U8*)s) !=
2419 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2421 0 : UTF8_ALLOW_ANY))
2428 /* The target is not utf8, the pattern is utf8. */
2432 if (NATIVE_TO_UNI(*((U8*)l)) !=
2433 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2435 0 : UTF8_ALLOW_ANY))
2442 nextchr = UCHARAT(locinput);
2445 /* The target and the pattern have the same utf8ness. */
2446 /* Inline the first character, for speed. */
2447 if (UCHARAT(s) != nextchr)
2449 if (PL_regeol - locinput < ln)
2451 if (ln > 1 && memNE(s, locinput, ln))
2454 nextchr = UCHARAT(locinput);
2457 PL_reg_flags |= RF_tainted;
2463 if (do_utf8 || UTF) {
2464 /* Either target or the pattern are utf8. */
2466 char *e = PL_regeol;
2468 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2469 l, &e, 0, do_utf8)) {
2470 /* One more case for the sharp s:
2471 * pack("U0U*", 0xDF) =~ /ss/i,
2472 * the 0xC3 0x9F are the UTF-8
2473 * byte sequence for the U+00DF. */
2475 toLOWER(s[0]) == 's' &&
2477 toLOWER(s[1]) == 's' &&
2484 nextchr = UCHARAT(locinput);
2488 /* Neither the target and the pattern are utf8. */
2490 /* Inline the first character, for speed. */
2491 if (UCHARAT(s) != nextchr &&
2492 UCHARAT(s) != ((OP(scan) == EXACTF)
2493 ? PL_fold : PL_fold_locale)[nextchr])
2495 if (PL_regeol - locinput < ln)
2497 if (ln > 1 && (OP(scan) == EXACTF
2498 ? ibcmp(s, locinput, ln)
2499 : ibcmp_locale(s, locinput, ln)))
2502 nextchr = UCHARAT(locinput);
2506 STRLEN inclasslen = PL_regeol - locinput;
2508 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2510 if (locinput >= PL_regeol)
2512 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2513 nextchr = UCHARAT(locinput);
2518 nextchr = UCHARAT(locinput);
2519 if (!REGINCLASS(scan, (U8*)locinput))
2521 if (!nextchr && locinput >= PL_regeol)
2523 nextchr = UCHARAT(++locinput);
2527 /* If we might have the case of the German sharp s
2528 * in a casefolding Unicode character class. */
2530 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2531 locinput += SHARP_S_SKIP;
2532 nextchr = UCHARAT(locinput);
2538 PL_reg_flags |= RF_tainted;
2544 LOAD_UTF8_CHARCLASS(alnum,"a");
2545 if (!(OP(scan) == ALNUM
2546 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2547 : isALNUM_LC_utf8((U8*)locinput)))
2551 locinput += PL_utf8skip[nextchr];
2552 nextchr = UCHARAT(locinput);
2555 if (!(OP(scan) == ALNUM
2556 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2558 nextchr = UCHARAT(++locinput);
2561 PL_reg_flags |= RF_tainted;
2564 if (!nextchr && locinput >= PL_regeol)
2567 LOAD_UTF8_CHARCLASS(alnum,"a");
2568 if (OP(scan) == NALNUM
2569 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2570 : isALNUM_LC_utf8((U8*)locinput))
2574 locinput += PL_utf8skip[nextchr];
2575 nextchr = UCHARAT(locinput);
2578 if (OP(scan) == NALNUM
2579 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2581 nextchr = UCHARAT(++locinput);
2585 PL_reg_flags |= RF_tainted;
2589 /* was last char in word? */
2591 if (locinput == PL_bostr)
2594 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2596 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2598 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2599 ln = isALNUM_uni(ln);
2600 LOAD_UTF8_CHARCLASS(alnum,"a");
2601 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2604 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2605 n = isALNUM_LC_utf8((U8*)locinput);
2609 ln = (locinput != PL_bostr) ?
2610 UCHARAT(locinput - 1) : '\n';
2611 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2613 n = isALNUM(nextchr);
2616 ln = isALNUM_LC(ln);
2617 n = isALNUM_LC(nextchr);
2620 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2621 OP(scan) == BOUNDL))
2625 PL_reg_flags |= RF_tainted;
2631 if (UTF8_IS_CONTINUED(nextchr)) {
2632 LOAD_UTF8_CHARCLASS(space," ");
2633 if (!(OP(scan) == SPACE
2634 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2635 : isSPACE_LC_utf8((U8*)locinput)))
2639 locinput += PL_utf8skip[nextchr];
2640 nextchr = UCHARAT(locinput);
2643 if (!(OP(scan) == SPACE
2644 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2646 nextchr = UCHARAT(++locinput);
2649 if (!(OP(scan) == SPACE
2650 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2652 nextchr = UCHARAT(++locinput);
2656 PL_reg_flags |= RF_tainted;
2659 if (!nextchr && locinput >= PL_regeol)
2662 LOAD_UTF8_CHARCLASS(space," ");
2663 if (OP(scan) == NSPACE
2664 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2665 : isSPACE_LC_utf8((U8*)locinput))
2669 locinput += PL_utf8skip[nextchr];
2670 nextchr = UCHARAT(locinput);
2673 if (OP(scan) == NSPACE
2674 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2676 nextchr = UCHARAT(++locinput);
2679 PL_reg_flags |= RF_tainted;
2685 LOAD_UTF8_CHARCLASS(digit,"0");
2686 if (!(OP(scan) == DIGIT
2687 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2688 : isDIGIT_LC_utf8((U8*)locinput)))
2692 locinput += PL_utf8skip[nextchr];
2693 nextchr = UCHARAT(locinput);
2696 if (!(OP(scan) == DIGIT
2697 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2699 nextchr = UCHARAT(++locinput);
2702 PL_reg_flags |= RF_tainted;
2705 if (!nextchr && locinput >= PL_regeol)
2708 LOAD_UTF8_CHARCLASS(digit,"0");
2709 if (OP(scan) == NDIGIT
2710 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2711 : isDIGIT_LC_utf8((U8*)locinput))
2715 locinput += PL_utf8skip[nextchr];
2716 nextchr = UCHARAT(locinput);
2719 if (OP(scan) == NDIGIT
2720 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2722 nextchr = UCHARAT(++locinput);
2725 if (locinput >= PL_regeol)
2728 LOAD_UTF8_CHARCLASS(mark,"~");
2729 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2731 locinput += PL_utf8skip[nextchr];
2732 while (locinput < PL_regeol &&
2733 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2734 locinput += UTF8SKIP(locinput);
2735 if (locinput > PL_regeol)
2740 nextchr = UCHARAT(locinput);
2743 PL_reg_flags |= RF_tainted;
2747 n = ARG(scan); /* which paren pair */
2748 ln = PL_regstartp[n];
2749 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2750 if ((I32)*PL_reglastparen < n || ln == -1)
2751 sayNO; /* Do not match unless seen CLOSEn. */
2752 if (ln == PL_regendp[n])
2756 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2758 char *e = PL_bostr + PL_regendp[n];
2760 * Note that we can't do the "other character" lookup trick as
2761 * in the 8-bit case (no pun intended) because in Unicode we
2762 * have to map both upper and title case to lower case.
2764 if (OP(scan) == REFF) {
2765 STRLEN ulen1, ulen2;
2766 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2767 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2771 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2772 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2773 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2780 nextchr = UCHARAT(locinput);
2784 /* Inline the first character, for speed. */
2785 if (UCHARAT(s) != nextchr &&
2787 (UCHARAT(s) != ((OP(scan) == REFF
2788 ? PL_fold : PL_fold_locale)[nextchr]))))
2790 ln = PL_regendp[n] - ln;
2791 if (locinput + ln > PL_regeol)
2793 if (ln > 1 && (OP(scan) == REF
2794 ? memNE(s, locinput, ln)
2796 ? ibcmp(s, locinput, ln)
2797 : ibcmp_locale(s, locinput, ln))))
2800 nextchr = UCHARAT(locinput);
2811 OP_4tree *oop = PL_op;
2812 COP *ocurcop = PL_curcop;
2817 PL_op = (OP_4tree*)PL_regdata->data[n];
2818 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2819 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2820 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2824 CALLRUNOPS(aTHX); /* Scalar context. */
2827 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2835 PAD_RESTORE_LOCAL(old_comppad);
2836 PL_curcop = ocurcop;
2838 if (logical == 2) { /* Postponed subexpression. */
2840 MAGIC *mg = Null(MAGIC*);
2842 CHECKPOINT cp, lastcp;
2845 if(SvROK(ret) || SvRMAGICAL(ret)) {
2846 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2849 mg = mg_find(sv, PERL_MAGIC_qr);
2852 re = (regexp *)mg->mg_obj;
2853 (void)ReREFCNT_inc(re);
2857 char *t = SvPV(ret, len);
2859 char *oprecomp = PL_regprecomp;
2860 I32 osize = PL_regsize;
2861 I32 onpar = PL_regnpar;
2864 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2865 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2867 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2868 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2870 PL_regprecomp = oprecomp;
2875 PerlIO_printf(Perl_debug_log,
2876 "Entering embedded `%s%.60s%s%s'\n",
2880 (strlen(re->precomp) > 60 ? "..." : ""))
2883 state.prev = PL_reg_call_cc;
2884 state.cc = PL_regcc;
2885 state.re = PL_reg_re;
2889 cp = regcppush(0); /* Save *all* the positions. */
2892 state.ss = PL_savestack_ix;
2893 *PL_reglastparen = 0;
2894 *PL_reglastcloseparen = 0;
2895 PL_reg_call_cc = &state;
2896 PL_reginput = locinput;
2897 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2898 ((re->reganch & ROPT_UTF8) != 0);
2899 if (toggleutf) PL_reg_flags ^= RF_utf8;
2901 /* XXXX This is too dramatic a measure... */
2904 if (regmatch(re->program + 1)) {
2905 /* Even though we succeeded, we need to restore
2906 global variables, since we may be wrapped inside
2907 SUSPEND, thus the match may be not finished yet. */
2909 /* XXXX Do this only if SUSPENDed? */
2910 PL_reg_call_cc = state.prev;
2911 PL_regcc = state.cc;
2912 PL_reg_re = state.re;
2913 cache_re(PL_reg_re);
2914 if (toggleutf) PL_reg_flags ^= RF_utf8;
2916 /* XXXX This is too dramatic a measure... */
2919 /* These are needed even if not SUSPEND. */
2925 REGCP_UNWIND(lastcp);
2927 PL_reg_call_cc = state.prev;
2928 PL_regcc = state.cc;
2929 PL_reg_re = state.re;
2930 cache_re(PL_reg_re);
2931 if (toggleutf) PL_reg_flags ^= RF_utf8;
2933 /* XXXX This is too dramatic a measure... */
2943 sv_setsv(save_scalar(PL_replgv), ret);
2947 n = ARG(scan); /* which paren pair */
2948 PL_reg_start_tmp[n] = locinput;
2953 n = ARG(scan); /* which paren pair */
2954 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2955 PL_regendp[n] = locinput - PL_bostr;
2956 if (n > (I32)*PL_reglastparen)
2957 *PL_reglastparen = n;
2958 *PL_reglastcloseparen = n;
2961 n = ARG(scan); /* which paren pair */
2962 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2965 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2967 next = NEXTOPER(NEXTOPER(scan));
2969 next = scan + ARG(scan);
2970 if (OP(next) == IFTHEN) /* Fake one. */
2971 next = NEXTOPER(NEXTOPER(next));
2975 logical = scan->flags;
2977 /*******************************************************************
2978 PL_regcc contains infoblock about the innermost (...)* loop, and
2979 a pointer to the next outer infoblock.
2981 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2983 1) After matching X, regnode for CURLYX is processed;
2985 2) This regnode creates infoblock on the stack, and calls
2986 regmatch() recursively with the starting point at WHILEM node;
2988 3) Each hit of WHILEM node tries to match A and Z (in the order
2989 depending on the current iteration, min/max of {min,max} and
2990 greediness). The information about where are nodes for "A"
2991 and "Z" is read from the infoblock, as is info on how many times "A"
2992 was already matched, and greediness.
2994 4) After A matches, the same WHILEM node is hit again.
2996 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2997 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2998 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2999 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3000 of the external loop.
3002 Currently present infoblocks form a tree with a stem formed by PL_curcc
3003 and whatever it mentions via ->next, and additional attached trees
3004 corresponding to temporarily unset infoblocks as in "5" above.
3006 In the following picture infoblocks for outer loop of
3007 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3008 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3009 infoblocks are drawn below the "reset" infoblock.
3011 In fact in the picture below we do not show failed matches for Z and T
3012 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3013 more obvious *why* one needs to *temporary* unset infoblocks.]
3015 Matched REx position InfoBlocks Comment
3019 Y A)*?Z)*?T x <- O <- I
3020 YA )*?Z)*?T x <- O <- I
3021 YA A)*?Z)*?T x <- O <- I
3022 YAA )*?Z)*?T x <- O <- I
3023 YAA Z)*?T x <- O # Temporary unset I
3026 YAAZ Y(A)*?Z)*?T x <- O
3029 YAAZY (A)*?Z)*?T x <- O
3032 YAAZY A)*?Z)*?T x <- O <- I
3035 YAAZYA )*?Z)*?T x <- O <- I
3038 YAAZYA Z)*?T x <- O # Temporary unset I
3044 YAAZYAZ T x # Temporary unset O
3051 *******************************************************************/
3054 CHECKPOINT cp = PL_savestack_ix;
3055 /* No need to save/restore up to this paren */
3056 I32 parenfloor = scan->flags;
3058 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3060 cc.oldcc = PL_regcc;
3062 /* XXXX Probably it is better to teach regpush to support
3063 parenfloor > PL_regsize... */
3064 if (parenfloor > (I32)*PL_reglastparen)
3065 parenfloor = *PL_reglastparen; /* Pessimization... */
3066 cc.parenfloor = parenfloor;
3068 cc.min = ARG1(scan);
3069 cc.max = ARG2(scan);
3070 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3074 PL_reginput = locinput;
3075 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3077 PL_regcc = cc.oldcc;
3083 * This is really hard to understand, because after we match
3084 * what we're trying to match, we must make sure the rest of
3085 * the REx is going to match for sure, and to do that we have
3086 * to go back UP the parse tree by recursing ever deeper. And
3087 * if it fails, we have to reset our parent's current state
3088 * that we can try again after backing off.
3091 CHECKPOINT cp, lastcp;
3092 CURCUR* cc = PL_regcc;
3093 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3095 n = cc->cur + 1; /* how many we know we matched */
3096 PL_reginput = locinput;
3099 PerlIO_printf(Perl_debug_log,
3100 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3101 REPORT_CODE_OFF+PL_regindent*2, "",
3102 (long)n, (long)cc->min,
3103 (long)cc->max, PTR2UV(cc))
3106 /* If degenerate scan matches "", assume scan done. */
3108 if (locinput == cc->lastloc && n >= cc->min) {
3109 PL_regcc = cc->oldcc;
3113 PerlIO_printf(Perl_debug_log,
3114 "%*s empty match detected, try continuation...\n",
3115 REPORT_CODE_OFF+PL_regindent*2, "")
3117 if (regmatch(cc->next))
3125 /* First just match a string of min scans. */
3129 cc->lastloc = locinput;
3130 if (regmatch(cc->scan))
3133 cc->lastloc = lastloc;
3138 /* Check whether we already were at this position.
3139 Postpone detection until we know the match is not
3140 *that* much linear. */
3141 if (!PL_reg_maxiter) {
3142 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3143 PL_reg_leftiter = PL_reg_maxiter;
3145 if (PL_reg_leftiter-- == 0) {
3146 I32 size = (PL_reg_maxiter + 7)/8;
3147 if (PL_reg_poscache) {
3148 if ((I32)PL_reg_poscache_size < size) {
3149 Renew(PL_reg_poscache, size, char);
3150 PL_reg_poscache_size = size;
3152 Zero(PL_reg_poscache, size, char);
3155 PL_reg_poscache_size = size;
3156 Newz(29, PL_reg_poscache, size, char);
3159 PerlIO_printf(Perl_debug_log,
3160 "%sDetected a super-linear match, switching on caching%s...\n",
3161 PL_colors[4], PL_colors[5])
3164 if (PL_reg_leftiter < 0) {
3165 I32 o = locinput - PL_bostr, b;
3167 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3170 if (PL_reg_poscache[o] & (1<<b)) {
3172 PerlIO_printf(Perl_debug_log,
3173 "%*s already tried at this position...\n",
3174 REPORT_CODE_OFF+PL_regindent*2, "")
3178 PL_reg_poscache[o] |= (1<<b);
3182 /* Prefer next over scan for minimal matching. */
3185 PL_regcc = cc->oldcc;
3188 cp = regcppush(cc->parenfloor);
3190 if (regmatch(cc->next)) {
3192 sayYES; /* All done. */
3194 REGCP_UNWIND(lastcp);
3200 if (n >= cc->max) { /* Maximum greed exceeded? */
3201 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3202 && !(PL_reg_flags & RF_warned)) {
3203 PL_reg_flags |= RF_warned;
3204 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3205 "Complex regular subexpression recursion",
3212 PerlIO_printf(Perl_debug_log,
3213 "%*s trying longer...\n",
3214 REPORT_CODE_OFF+PL_regindent*2, "")
3216 /* Try scanning more and see if it helps. */
3217 PL_reginput = locinput;
3219 cc->lastloc = locinput;
3220 cp = regcppush(cc->parenfloor);
3222 if (regmatch(cc->scan)) {
3226 REGCP_UNWIND(lastcp);
3229 cc->lastloc = lastloc;
3233 /* Prefer scan over next for maximal matching. */
3235 if (n < cc->max) { /* More greed allowed? */
3236 cp = regcppush(cc->parenfloor);
3238 cc->lastloc = locinput;
3240 if (regmatch(cc->scan)) {
3244 REGCP_UNWIND(lastcp);
3245 regcppop(); /* Restore some previous $<digit>s? */
3246 PL_reginput = locinput;
3248 PerlIO_printf(Perl_debug_log,
3249 "%*s failed, try continuation...\n",
3250 REPORT_CODE_OFF+PL_regindent*2, "")
3253 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3254 && !(PL_reg_flags & RF_warned)) {
3255 PL_reg_flags |= RF_warned;
3256 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3257 "Complex regular subexpression recursion",
3261 /* Failed deeper matches of scan, so see if this one works. */
3262 PL_regcc = cc->oldcc;
3265 if (regmatch(cc->next))
3271 cc->lastloc = lastloc;
3276 next = scan + ARG(scan);
3279 inner = NEXTOPER(NEXTOPER(scan));
3282 inner = NEXTOPER(scan);
3286 if (OP(next) != c1) /* No choice. */
3287 next = inner; /* Avoid recursion. */
3289 I32 lastparen = *PL_reglastparen;
3291 re_unwind_branch_t *uw;
3293 /* Put unwinding data on stack */
3294 unwind1 = SSNEWt(1,re_unwind_branch_t);
3295 uw = SSPTRt(unwind1,re_unwind_branch_t);
3298 uw->type = ((c1 == BRANCH)
3300 : RE_UNWIND_BRANCHJ);
3301 uw->lastparen = lastparen;
3303 uw->locinput = locinput;
3304 uw->nextchr = nextchr;
3306 uw->regindent = ++PL_regindent;
3309 REGCP_SET(uw->lastcp);
3311 /* Now go into the first branch */
3324 /* We suppose that the next guy does not need
3325 backtracking: in particular, it is of constant length,
3326 and has no parenths to influence future backrefs. */
3327 ln = ARG1(scan); /* min to match */
3328 n = ARG2(scan); /* max to match */
3329 paren = scan->flags;
3331 if (paren > PL_regsize)
3333 if (paren > (I32)*PL_reglastparen)
3334 *PL_reglastparen = paren;
3336 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3338 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3339 PL_reginput = locinput;
3342 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3344 /* if we matched something zero-length we don't need to
3345 backtrack - capturing parens are already defined, so
3346 the caveat in the maximal case doesn't apply
3348 XXXX if ln == 0, we can redo this check first time
3349 through the following loop
3352 n = ln; /* don't backtrack */
3353 locinput = PL_reginput;
3354 if (HAS_TEXT(next) || JUMPABLE(next)) {
3355 regnode *text_node = next;
3357 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3359 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3361 if (PL_regkind[(U8)OP(text_node)] == REF) {
3363 n = ARG(text_node); /* which paren pair */
3364 ln = PL_regstartp[n];
3365 /* assume yes if we haven't seen CLOSEn */
3367 (I32)*PL_reglastparen < n ||
3374 c1 = *(PL_bostr + ln);
3376 else { c1 = (U8)*STRING(text_node); }
3377 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3379 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3380 c2 = PL_fold_locale[c1];
3389 /* This may be improved if l == 0. */
3390 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3391 /* If it could work, try it. */
3393 UCHARAT(PL_reginput) == c1 ||
3394 UCHARAT(PL_reginput) == c2)
3398 PL_regstartp[paren] =
3399 HOPc(PL_reginput, -l) - PL_bostr;
3400 PL_regendp[paren] = PL_reginput - PL_bostr;
3403 PL_regendp[paren] = -1;
3407 REGCP_UNWIND(lastcp);
3409 /* Couldn't or didn't -- move forward. */
3410 PL_reginput = locinput;
3411 if (regrepeat_hard(scan, 1, &l)) {
3413 locinput = PL_reginput;
3420 n = regrepeat_hard(scan, n, &l);
3421 /* if we matched something zero-length we don't need to
3422 backtrack, unless the minimum count is zero and we
3423 are capturing the result - in that case the capture
3424 being defined or not may affect later execution
3426 if (n != 0 && l == 0 && !(paren && ln == 0))
3427 ln = n; /* don't backtrack */
3428 locinput = PL_reginput;
3430 PerlIO_printf(Perl_debug_log,
3431 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3432 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3436 if (HAS_TEXT(next) || JUMPABLE(next)) {
3437 regnode *text_node = next;
3439 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3441 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3443 if (PL_regkind[(U8)OP(text_node)] == REF) {
3445 n = ARG(text_node); /* which paren pair */
3446 ln = PL_regstartp[n];
3447 /* assume yes if we haven't seen CLOSEn */
3449 (I32)*PL_reglastparen < n ||
3456 c1 = *(PL_bostr + ln);
3458 else { c1 = (U8)*STRING(text_node); }
3460 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3462 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3463 c2 = PL_fold_locale[c1];
3474 /* If it could work, try it. */
3476 UCHARAT(PL_reginput) == c1 ||
3477 UCHARAT(PL_reginput) == c2)
3480 PerlIO_printf(Perl_debug_log,
3481 "%*s trying tail with n=%"IVdf"...\n",
3482 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3486 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3487 PL_regendp[paren] = PL_reginput - PL_bostr;
3490 PL_regendp[paren] = -1;
3494 REGCP_UNWIND(lastcp);
3496 /* Couldn't or didn't -- back up. */
3498 locinput = HOPc(locinput, -l);
3499 PL_reginput = locinput;
3506 paren = scan->flags; /* Which paren to set */
3507 if (paren > PL_regsize)
3509 if (paren > (I32)*PL_reglastparen)
3510 *PL_reglastparen = paren;
3511 ln = ARG1(scan); /* min to match */
3512 n = ARG2(scan); /* max to match */
3513 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3517 ln = ARG1(scan); /* min to match */
3518 n = ARG2(scan); /* max to match */
3519 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3524 scan = NEXTOPER(scan);
3530 scan = NEXTOPER(scan);
3534 * Lookahead to avoid useless match attempts
3535 * when we know what character comes next.
3539 * Used to only do .*x and .*?x, but now it allows
3540 * for )'s, ('s and (?{ ... })'s to be in the way
3541 * of the quantifier and the EXACT-like node. -- japhy
3544 if (HAS_TEXT(next) || JUMPABLE(next)) {
3546 regnode *text_node = next;
3548 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3550 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3552 if (PL_regkind[(U8)OP(text_node)] == REF) {
3554 n = ARG(text_node); /* which paren pair */
3555 ln = PL_regstartp[n];
3556 /* assume yes if we haven't seen CLOSEn */
3558 (I32)*PL_reglastparen < n ||
3563 goto assume_ok_easy;
3565 s = (U8*)PL_bostr + ln;
3567 else { s = (U8*)STRING(text_node); }
3571 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3573 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3574 c2 = PL_fold_locale[c1];
3577 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3578 STRLEN ulen1, ulen2;
3579 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3580 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3582 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3583 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3585 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3587 0 : UTF8_ALLOW_ANY);
3588 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3590 0 : UTF8_ALLOW_ANY);
3593 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3595 0 : UTF8_ALLOW_ANY);
3603 PL_reginput = locinput;
3607 if (ln && regrepeat(scan, ln) < ln)
3609 locinput = PL_reginput;
3612 char *e; /* Should not check after this */
3613 char *old = locinput;
3616 if (n == REG_INFTY) {
3619 while (UTF8_IS_CONTINUATION(*(U8*)e))
3625 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3629 e = locinput + n - ln;
3634 /* Find place 'next' could work */
3637 while (locinput <= e &&
3638 UCHARAT(locinput) != c1)
3641 while (locinput <= e
3642 && UCHARAT(locinput) != c1
3643 && UCHARAT(locinput) != c2)
3646 count = locinput - old;
3651 /* count initialised to
3652 * utf8_distance(old, locinput) */
3653 while (locinput <= e &&
3654 utf8n_to_uvchr((U8*)locinput,
3657 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3662 /* count initialised to
3663 * utf8_distance(old, locinput) */
3664 while (locinput <= e) {
3665 UV c = utf8n_to_uvchr((U8*)locinput,
3668 0 : UTF8_ALLOW_ANY);
3669 if (c == (UV)c1 || c == (UV)c2)
3678 /* PL_reginput == old now */
3679 if (locinput != old) {
3680 ln = 1; /* Did some */
3681 if (regrepeat(scan, count) < count)
3684 /* PL_reginput == locinput now */
3685 TRYPAREN(paren, ln, locinput);
3686 PL_reginput = locinput; /* Could be reset... */
3687 REGCP_UNWIND(lastcp);
3688 /* Couldn't or didn't -- move forward. */
3691 locinput += UTF8SKIP(locinput);
3698 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3702 c = utf8n_to_uvchr((U8*)PL_reginput,
3705 0 : UTF8_ALLOW_ANY);
3707 c = UCHARAT(PL_reginput);
3708 /* If it could work, try it. */
3709 if (c == (UV)c1 || c == (UV)c2)
3711 TRYPAREN(paren, ln, PL_reginput);
3712 REGCP_UNWIND(lastcp);
3715 /* If it could work, try it. */
3716 else if (c1 == -1000)
3718 TRYPAREN(paren, ln, PL_reginput);
3719 REGCP_UNWIND(lastcp);
3721 /* Couldn't or didn't -- move forward. */
3722 PL_reginput = locinput;
3723 if (regrepeat(scan, 1)) {
3725 locinput = PL_reginput;
3733 n = regrepeat(scan, n);
3734 locinput = PL_reginput;
3735 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3736 ((!PL_multiline && OP(next) != MEOL) ||
3737 OP(next) == SEOL || OP(next) == EOS))
3739 ln = n; /* why back off? */
3740 /* ...because $ and \Z can match before *and* after
3741 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3742 We should back off by one in this case. */
3743 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3752 c = utf8n_to_uvchr((U8*)PL_reginput,
3755 0 : UTF8_ALLOW_ANY);
3757 c = UCHARAT(PL_reginput);
3759 /* If it could work, try it. */
3760 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3762 TRYPAREN(paren, n, PL_reginput);
3763 REGCP_UNWIND(lastcp);
3765 /* Couldn't or didn't -- back up. */
3767 PL_reginput = locinput = HOPc(locinput, -1);
3775 c = utf8n_to_uvchr((U8*)PL_reginput,
3778 0 : UTF8_ALLOW_ANY);
3780 c = UCHARAT(PL_reginput);
3782 /* If it could work, try it. */
3783 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3785 TRYPAREN(paren, n, PL_reginput);
3786 REGCP_UNWIND(lastcp);
3788 /* Couldn't or didn't -- back up. */
3790 PL_reginput = locinput = HOPc(locinput, -1);
3797 if (PL_reg_call_cc) {
3798 re_cc_state *cur_call_cc = PL_reg_call_cc;
3799 CURCUR *cctmp = PL_regcc;
3800 regexp *re = PL_reg_re;
3801 CHECKPOINT cp, lastcp;
3803 cp = regcppush(0); /* Save *all* the positions. */
3805 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3807 PL_reginput = locinput; /* Make position available to
3809 cache_re(PL_reg_call_cc->re);
3810 PL_regcc = PL_reg_call_cc->cc;
3811 PL_reg_call_cc = PL_reg_call_cc->prev;
3812 if (regmatch(cur_call_cc->node)) {
3813 PL_reg_call_cc = cur_call_cc;
3817 REGCP_UNWIND(lastcp);
3819 PL_reg_call_cc = cur_call_cc;
3825 PerlIO_printf(Perl_debug_log,
3826 "%*s continuation failed...\n",
3827 REPORT_CODE_OFF+PL_regindent*2, "")
3831 if (locinput < PL_regtill) {
3832 DEBUG_r(PerlIO_printf(Perl_debug_log,
3833 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3835 (long)(locinput - PL_reg_starttry),
3836 (long)(PL_regtill - PL_reg_starttry),
3838 sayNO_FINAL; /* Cannot match: too short. */
3840 PL_reginput = locinput; /* put where regtry can find it */
3841 sayYES_FINAL; /* Success! */
3843 PL_reginput = locinput; /* put where regtry can find it */
3844 sayYES_LOUD; /* Success! */
3847 PL_reginput = locinput;
3852 s = HOPBACKc(locinput, scan->flags);
3858 PL_reginput = locinput;
3863 s = HOPBACKc(locinput, scan->flags);
3869 PL_reginput = locinput;
3872 inner = NEXTOPER(NEXTOPER(scan));
3873 if (regmatch(inner) != n) {
3888 if (OP(scan) == SUSPEND) {
3889 locinput = PL_reginput;
3890 nextchr = UCHARAT(locinput);
3895 next = scan + ARG(scan);
3900 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3901 PTR2UV(scan), OP(scan));
3902 Perl_croak(aTHX_ "regexp memory corruption");
3909 * We get here only if there's trouble -- normally "case END" is
3910 * the terminating point.
3912 Perl_croak(aTHX_ "corrupted regexp pointers");
3918 PerlIO_printf(Perl_debug_log,
3919 "%*s %scould match...%s\n",
3920 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3924 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3925 PL_colors[4],PL_colors[5]));
3931 #if 0 /* Breaks $^R */
3939 PerlIO_printf(Perl_debug_log,
3940 "%*s %sfailed...%s\n",
3941 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3947 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3950 case RE_UNWIND_BRANCH:
3951 case RE_UNWIND_BRANCHJ:
3953 re_unwind_branch_t *uwb = &(uw->branch);
3954 I32 lastparen = uwb->lastparen;
3956 REGCP_UNWIND(uwb->lastcp);
3957 for (n = *PL_reglastparen; n > lastparen; n--)
3959 *PL_reglastparen = n;
3960 scan = next = uwb->next;
3962 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3963 ? BRANCH : BRANCHJ) ) { /* Failure */
3970 /* Have more choice yet. Reuse the same uwb. */
3972 if ((n = (uwb->type == RE_UNWIND_BRANCH
3973 ? NEXT_OFF(next) : ARG(next))))
3976 next = NULL; /* XXXX Needn't unwinding in this case... */
3978 next = NEXTOPER(scan);
3979 if (uwb->type == RE_UNWIND_BRANCHJ)
3980 next = NEXTOPER(next);
3981 locinput = uwb->locinput;
3982 nextchr = uwb->nextchr;
3984 PL_regindent = uwb->regindent;
3991 Perl_croak(aTHX_ "regexp unwind memory corruption");
4002 - regrepeat - repeatedly match something simple, report how many
4005 * [This routine now assumes that it will only match on things of length 1.
4006 * That was true before, but now we assume scan - reginput is the count,
4007 * rather than incrementing count on every character. [Er, except utf8.]]
4010 S_regrepeat(pTHX_ regnode *p, I32 max)
4012 register char *scan;
4014 register char *loceol = PL_regeol;
4015 register I32 hardcount = 0;
4016 register bool do_utf8 = PL_reg_match_utf8;
4019 if (max == REG_INFTY)
4021 else if (max < loceol - scan)
4022 loceol = scan + max;
4027 while (scan < loceol && hardcount < max && *scan != '\n') {
4028 scan += UTF8SKIP(scan);
4032 while (scan < loceol && *scan != '\n')
4039 while (scan < loceol && hardcount < max) {
4040 scan += UTF8SKIP(scan);
4050 case EXACT: /* length of string is 1 */
4052 while (scan < loceol && UCHARAT(scan) == c)
4055 case EXACTF: /* length of string is 1 */
4057 while (scan < loceol &&
4058 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4061 case EXACTFL: /* length of string is 1 */
4062 PL_reg_flags |= RF_tainted;
4064 while (scan < loceol &&
4065 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4071 while (hardcount < max && scan < loceol) {
4073 if (ANYOF_FLAGS(p) & ANYOF_UNICODE) {
4074 if (reginclass(p, (U8*)scan, 0, do_utf8))
4080 if (UTF8_IS_INVARIANT(c)) {
4081 if (ANYOF_BITMAP_TEST(p, c))
4085 if (reginclass(p, (U8*)scan, 0, do_utf8))
4091 scan += UTF8SKIP(scan);
4095 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4102 LOAD_UTF8_CHARCLASS(alnum,"a");
4103 while (hardcount < max && scan < loceol &&
4104 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4105 scan += UTF8SKIP(scan);
4109 while (scan < loceol && isALNUM(*scan))
4114 PL_reg_flags |= RF_tainted;
4117 while (hardcount < max && scan < loceol &&
4118 isALNUM_LC_utf8((U8*)scan)) {
4119 scan += UTF8SKIP(scan);
4123 while (scan < loceol && isALNUM_LC(*scan))
4130 LOAD_UTF8_CHARCLASS(alnum,"a");
4131 while (hardcount < max && scan < loceol &&
4132 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4133 scan += UTF8SKIP(scan);
4137 while (scan < loceol && !isALNUM(*scan))
4142 PL_reg_flags |= RF_tainted;
4145 while (hardcount < max && scan < loceol &&
4146 !isALNUM_LC_utf8((U8*)scan)) {
4147 scan += UTF8SKIP(scan);
4151 while (scan < loceol && !isALNUM_LC(*scan))
4158 LOAD_UTF8_CHARCLASS(space," ");
4159 while (hardcount < max && scan < loceol &&
4161 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4162 scan += UTF8SKIP(scan);
4166 while (scan < loceol && isSPACE(*scan))
4171 PL_reg_flags |= RF_tainted;
4174 while (hardcount < max && scan < loceol &&
4175 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4176 scan += UTF8SKIP(scan);
4180 while (scan < loceol && isSPACE_LC(*scan))
4187 LOAD_UTF8_CHARCLASS(space," ");
4188 while (hardcount < max && scan < loceol &&
4190 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4191 scan += UTF8SKIP(scan);
4195 while (scan < loceol && !isSPACE(*scan))
4200 PL_reg_flags |= RF_tainted;
4203 while (hardcount < max && scan < loceol &&
4204 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4205 scan += UTF8SKIP(scan);
4209 while (scan < loceol && !isSPACE_LC(*scan))
4216 LOAD_UTF8_CHARCLASS(digit,"0");
4217 while (hardcount < max && scan < loceol &&
4218 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4219 scan += UTF8SKIP(scan);
4223 while (scan < loceol && isDIGIT(*scan))
4230 LOAD_UTF8_CHARCLASS(digit,"0");
4231 while (hardcount < max && scan < loceol &&
4232 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4233 scan += UTF8SKIP(scan);
4237 while (scan < loceol && !isDIGIT(*scan))
4241 default: /* Called on something of 0 width. */
4242 break; /* So match right here or not at all. */
4248 c = scan - PL_reginput;
4253 SV *prop = sv_newmortal();
4256 PerlIO_printf(Perl_debug_log,
4257 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4258 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4265 - regrepeat_hard - repeatedly match something, report total lenth and length
4267 * The repeater is supposed to have constant length.
4271 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4273 register char *scan = Nullch;
4274 register char *start;
4275 register char *loceol = PL_regeol;
4277 I32 count = 0, res = 1;
4282 start = PL_reginput;
4283 if (PL_reg_match_utf8) {
4284 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4287 while (start < PL_reginput) {
4289 start += UTF8SKIP(start);
4300 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4302 *lp = l = PL_reginput - start;
4303 if (max != REG_INFTY && l*max < loceol - scan)
4304 loceol = scan + l*max;
4317 - regclass_swash - prepare the utf8 swash
4321 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4327 if (PL_regdata && PL_regdata->count) {
4330 if (PL_regdata->what[n] == 's') {
4331 SV *rv = (SV*)PL_regdata->data[n];
4332 AV *av = (AV*)SvRV((SV*)rv);
4333 SV **ary = AvARRAY(av);
4336 /* See the end of regcomp.c:S_reglass() for
4337 * documentation of these array elements. */
4340 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4341 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4345 else if (si && doinit) {
4346 sw = swash_init("utf8", "", si, 1, 0);
4347 (void)av_store(av, 1, sw);
4363 - reginclass - determine if a character falls into a character class
4365 The n is the ANYOF regnode, the p is the target string, lenp
4366 is pointer to the maximum length of how far to go in the p
4367 (if the lenp is zero, UTF8SKIP(p) is used),
4368 do_utf8 tells whether the target string is in UTF-8.
4373 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4375 char flags = ANYOF_FLAGS(n);
4381 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4382 c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4383 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4385 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4386 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4389 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4390 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4393 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4397 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4400 if (swash_fetch(sw, p, do_utf8))
4402 else if (flags & ANYOF_FOLD) {
4403 if (!match && lenp && av) {
4406 for (i = 0; i <= av_len(av); i++) {
4407 SV* sv = *av_fetch(av, i, FALSE);
4409 char *s = SvPV(sv, len);
4411 if (len <= plen && memEQ(s, (char*)p, len)) {
4419 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4422 to_utf8_fold(p, tmpbuf, &tmplen);
4423 if (swash_fetch(sw, tmpbuf, do_utf8))
4429 if (match && lenp && *lenp == 0)
4430 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4432 if (!match && c < 256) {
4433 if (ANYOF_BITMAP_TEST(n, c))
4435 else if (flags & ANYOF_FOLD) {
4438 if (flags & ANYOF_LOCALE) {
4439 PL_reg_flags |= RF_tainted;
4440 f = PL_fold_locale[c];
4444 if (f != c && ANYOF_BITMAP_TEST(n, f))
4448 if (!match && (flags & ANYOF_CLASS)) {
4449 PL_reg_flags |= RF_tainted;
4451 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4452 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4453 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4454 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4455 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4456 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4457 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4458 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4459 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4460 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4461 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4462 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4463 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4464 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4465 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4466 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4467 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4468 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4469 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4470 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4471 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4472 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4473 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4474 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4475 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4476 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4477 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4478 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4479 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4480 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4481 ) /* How's that for a conditional? */
4488 return (flags & ANYOF_INVERT) ? !match : match;
4492 S_reghop(pTHX_ U8 *s, I32 off)
4494 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4498 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4501 while (off-- && s < lim) {
4502 /* XXX could check well-formedness here */
4510 if (UTF8_IS_CONTINUED(*s)) {
4511 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4514 /* XXX could check well-formedness here */
4522 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4524 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4528 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4531 while (off-- && s < lim) {
4532 /* XXX could check well-formedness here */
4542 if (UTF8_IS_CONTINUED(*s)) {
4543 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4546 /* XXX could check well-formedness here */
4558 restore_pos(pTHX_ void *arg)
4560 if (PL_reg_eval_set) {
4561 if (PL_reg_oldsaved) {
4562 PL_reg_re->subbeg = PL_reg_oldsaved;
4563 PL_reg_re->sublen = PL_reg_oldsavedlen;
4564 RX_MATCH_COPIED_on(PL_reg_re);
4566 PL_reg_magic->mg_len = PL_reg_oldpos;
4567 PL_reg_eval_set = 0;
4568 PL_curpm = PL_reg_oldcurpm;
4573 S_to_utf8_substr(pTHX_ register regexp *prog)
4576 if (prog->float_substr && !prog->float_utf8) {
4577 prog->float_utf8 = sv = NEWSV(117, 0);
4578 SvSetSV(sv, prog->float_substr);
4579 sv_utf8_upgrade(sv);
4580 if (SvTAIL(prog->float_substr))
4582 if (prog->float_substr == prog->check_substr)
4583 prog->check_utf8 = sv;
4585 if (prog->anchored_substr && !prog->anchored_utf8) {
4586 prog->anchored_utf8 = sv = NEWSV(118, 0);
4587 SvSetSV(sv, prog->anchored_substr);
4588 sv_utf8_upgrade(sv);
4589 if (SvTAIL(prog->anchored_substr))
4591 if (prog->anchored_substr == prog->check_substr)
4592 prog->check_utf8 = sv;
4597 S_to_byte_substr(pTHX_ register regexp *prog)
4600 if (prog->float_utf8 && !prog->float_substr) {
4601 prog->float_substr = sv = NEWSV(117, 0);
4602 SvSetSV(sv, prog->float_utf8);
4603 if (sv_utf8_downgrade(sv, TRUE)) {
4604 if (SvTAIL(prog->float_utf8))
4608 prog->float_substr = sv = &PL_sv_undef;
4610 if (prog->float_utf8 == prog->check_utf8)
4611 prog->check_substr = sv;
4613 if (prog->anchored_utf8 && !prog->anchored_substr) {
4614 prog->anchored_substr = sv = NEWSV(118, 0);
4615 SvSetSV(sv, prog->anchored_utf8);
4616 if (sv_utf8_downgrade(sv, TRUE)) {
4617 if (SvTAIL(prog->anchored_utf8))
4621 prog->anchored_substr = sv = &PL_sv_undef;
4623 if (prog->anchored_utf8 == prog->check_utf8)
4624 prog->check_substr = sv;