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 */
598 && (!(prog->reganch & ROPT_UTF8)
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 */
718 && (!(prog->reganch & ROPT_UTF8)
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. */
963 STRLEN skip = do_utf8 ? UTF8SKIP(s) : 1;
966 reginclass(c, (U8*)s, 0, do_utf8) :
967 REGINCLASS(c, (U8*)s) ||
968 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
969 /* The assignment of 2 is intentional:
970 * for the sharp s, the skip is 2. */
971 (skip = SHARP_S_SKIP)
973 if (tmp && (norun || regtry(prog, s)))
985 if (tmp && (norun || regtry(prog, s)))
997 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
998 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1000 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1001 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1003 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1004 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1005 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1006 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1017 c2 = PL_fold_locale[c1];
1019 e = HOP3c(strend, -(I32)ln, s);
1022 e = s; /* Due to minlen logic of intuit() */
1024 /* The idea in the EXACTF* cases is to first find the
1025 * first character of the EXACTF* node and then, if
1026 * necessary, case-insensitively compare the full
1027 * text of the node. The c1 and c2 are the first
1028 * characters (though in Unicode it gets a bit
1029 * more complicated because there are more cases
1030 * than just upper and lower: one needs to use
1031 * the so-called folding case for case-insensitive
1032 * matching (called "loose matching" in Unicode).
1033 * ibcmp_utf8() will do just that. */
1037 U8 tmpbuf [UTF8_MAXLEN+1];
1038 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1039 STRLEN len, foldlen;
1043 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1045 0 : UTF8_ALLOW_ANY);
1048 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1049 m, (char **)0, ln, (bool)UTF))
1050 && (norun || regtry(prog, s)) )
1053 uvchr_to_utf8(tmpbuf, c);
1054 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1056 && (f == c1 || f == c2)
1057 && (ln == foldlen ||
1058 !ibcmp_utf8((char *) foldbuf,
1059 (char **)0, foldlen, do_utf8,
1061 (char **)0, ln, (bool)UTF))
1062 && (norun || regtry(prog, s)) )
1070 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1072 0 : UTF8_ALLOW_ANY);
1074 /* Handle some of the three Greek sigmas cases.
1075 * Note that not all the possible combinations
1076 * are handled here: some of them are handled
1077 * by the standard folding rules, and some of
1078 * them (the character class or ANYOF cases)
1079 * are handled during compiletime in
1080 * regexec.c:S_regclass(). */
1081 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1082 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1083 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1085 if ( (c == c1 || c == c2)
1087 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1088 m, (char **)0, ln, (bool)UTF))
1089 && (norun || regtry(prog, s)) )
1092 uvchr_to_utf8(tmpbuf, c);
1093 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1095 && (f == c1 || f == c2)
1096 && (ln == foldlen ||
1097 !ibcmp_utf8((char *) foldbuf,
1098 (char **)0, foldlen, do_utf8,
1100 (char **)0, ln, (bool)UTF))
1101 && (norun || regtry(prog, s)) )
1112 && (ln == 1 || !(OP(c) == EXACTF
1114 : ibcmp_locale(s, m, ln)))
1115 && (norun || regtry(prog, s)) )
1121 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1122 && (ln == 1 || !(OP(c) == EXACTF
1124 : ibcmp_locale(s, m, ln)))
1125 && (norun || regtry(prog, s)) )
1132 PL_reg_flags |= RF_tainted;
1139 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1141 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1143 tmp = ((OP(c) == BOUND ?
1144 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1145 LOAD_UTF8_CHARCLASS(alnum,"a");
1146 while (s < strend) {
1147 if (tmp == !(OP(c) == BOUND ?
1148 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1149 isALNUM_LC_utf8((U8*)s)))
1152 if ((norun || regtry(prog, s)))
1159 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1160 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1161 while (s < strend) {
1163 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1165 if ((norun || regtry(prog, s)))
1171 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1175 PL_reg_flags |= RF_tainted;
1182 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1184 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1186 tmp = ((OP(c) == NBOUND ?
1187 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1188 LOAD_UTF8_CHARCLASS(alnum,"a");
1189 while (s < strend) {
1190 if (tmp == !(OP(c) == NBOUND ?
1191 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1192 isALNUM_LC_utf8((U8*)s)))
1194 else if ((norun || regtry(prog, s)))
1200 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1201 tmp = ((OP(c) == NBOUND ?
1202 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1203 while (s < strend) {
1205 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1207 else if ((norun || regtry(prog, s)))
1212 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1217 LOAD_UTF8_CHARCLASS(alnum,"a");
1218 while (s < strend) {
1219 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1220 if (tmp && (norun || regtry(prog, s)))
1231 while (s < strend) {
1233 if (tmp && (norun || regtry(prog, s)))
1245 PL_reg_flags |= RF_tainted;
1247 while (s < strend) {
1248 if (isALNUM_LC_utf8((U8*)s)) {
1249 if (tmp && (norun || regtry(prog, s)))
1260 while (s < strend) {
1261 if (isALNUM_LC(*s)) {
1262 if (tmp && (norun || regtry(prog, s)))
1275 LOAD_UTF8_CHARCLASS(alnum,"a");
1276 while (s < strend) {
1277 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1278 if (tmp && (norun || regtry(prog, s)))
1289 while (s < strend) {
1291 if (tmp && (norun || regtry(prog, s)))
1303 PL_reg_flags |= RF_tainted;
1305 while (s < strend) {
1306 if (!isALNUM_LC_utf8((U8*)s)) {
1307 if (tmp && (norun || regtry(prog, s)))
1318 while (s < strend) {
1319 if (!isALNUM_LC(*s)) {
1320 if (tmp && (norun || regtry(prog, s)))
1333 LOAD_UTF8_CHARCLASS(space," ");
1334 while (s < strend) {
1335 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1336 if (tmp && (norun || regtry(prog, s)))
1347 while (s < strend) {
1349 if (tmp && (norun || regtry(prog, s)))
1361 PL_reg_flags |= RF_tainted;
1363 while (s < strend) {
1364 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1365 if (tmp && (norun || regtry(prog, s)))
1376 while (s < strend) {
1377 if (isSPACE_LC(*s)) {
1378 if (tmp && (norun || regtry(prog, s)))
1391 LOAD_UTF8_CHARCLASS(space," ");
1392 while (s < strend) {
1393 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1394 if (tmp && (norun || regtry(prog, s)))
1405 while (s < strend) {
1407 if (tmp && (norun || regtry(prog, s)))
1419 PL_reg_flags |= RF_tainted;
1421 while (s < strend) {
1422 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1423 if (tmp && (norun || regtry(prog, s)))
1434 while (s < strend) {
1435 if (!isSPACE_LC(*s)) {
1436 if (tmp && (norun || regtry(prog, s)))
1449 LOAD_UTF8_CHARCLASS(digit,"0");
1450 while (s < strend) {
1451 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1452 if (tmp && (norun || regtry(prog, s)))
1463 while (s < strend) {
1465 if (tmp && (norun || regtry(prog, s)))
1477 PL_reg_flags |= RF_tainted;
1479 while (s < strend) {
1480 if (isDIGIT_LC_utf8((U8*)s)) {
1481 if (tmp && (norun || regtry(prog, s)))
1492 while (s < strend) {
1493 if (isDIGIT_LC(*s)) {
1494 if (tmp && (norun || regtry(prog, s)))
1507 LOAD_UTF8_CHARCLASS(digit,"0");
1508 while (s < strend) {
1509 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1510 if (tmp && (norun || regtry(prog, s)))
1521 while (s < strend) {
1523 if (tmp && (norun || regtry(prog, s)))
1535 PL_reg_flags |= RF_tainted;
1537 while (s < strend) {
1538 if (!isDIGIT_LC_utf8((U8*)s)) {
1539 if (tmp && (norun || regtry(prog, s)))
1550 while (s < strend) {
1551 if (!isDIGIT_LC(*s)) {
1552 if (tmp && (norun || regtry(prog, s)))
1564 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1573 - regexec_flags - match a regexp against a string
1576 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1577 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1578 /* strend: pointer to null at end of string */
1579 /* strbeg: real beginning of string */
1580 /* minend: end of match must be >=minend after stringarg. */
1581 /* data: May be used for some additional optimizations. */
1582 /* nosave: For optimizations. */
1585 register regnode *c;
1586 register char *startpos = stringarg;
1587 I32 minlen; /* must match at least this many chars */
1588 I32 dontbother = 0; /* how many characters not to try at end */
1589 /* I32 start_shift = 0; */ /* Offset of the start to find
1590 constant substr. */ /* CC */
1591 I32 end_shift = 0; /* Same for the end. */ /* CC */
1592 I32 scream_pos = -1; /* Internal iterator of scream. */
1594 SV* oreplsv = GvSV(PL_replgv);
1595 bool do_utf8 = DO_UTF8(sv);
1597 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1598 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1605 PL_regnarrate = DEBUG_r_TEST;
1608 /* Be paranoid... */
1609 if (prog == NULL || startpos == NULL) {
1610 Perl_croak(aTHX_ "NULL regexp parameter");
1614 minlen = prog->minlen;
1615 if (strend - startpos < minlen) {
1616 DEBUG_r(PerlIO_printf(Perl_debug_log,
1617 "String too short [regexec_flags]...\n"));
1621 /* Check validity of program. */
1622 if (UCHARAT(prog->program) != REG_MAGIC) {
1623 Perl_croak(aTHX_ "corrupted regexp program");
1627 PL_reg_eval_set = 0;
1630 if (prog->reganch & ROPT_UTF8)
1631 PL_reg_flags |= RF_utf8;
1633 /* Mark beginning of line for ^ and lookbehind. */
1634 PL_regbol = startpos;
1638 /* Mark end of line for $ (and such) */
1641 /* see how far we have to get to not match where we matched before */
1642 PL_regtill = startpos+minend;
1644 /* We start without call_cc context. */
1647 /* If there is a "must appear" string, look for it. */
1650 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1653 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1654 PL_reg_ganch = startpos;
1655 else if (sv && SvTYPE(sv) >= SVt_PVMG
1657 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1658 && mg->mg_len >= 0) {
1659 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1660 if (prog->reganch & ROPT_ANCH_GPOS) {
1661 if (s > PL_reg_ganch)
1666 else /* pos() not defined */
1667 PL_reg_ganch = strbeg;
1670 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1671 re_scream_pos_data d;
1673 d.scream_olds = &scream_olds;
1674 d.scream_pos = &scream_pos;
1675 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1677 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1678 goto phooey; /* not present */
1684 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1685 UNI_DISPLAY_REGEX) :
1687 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1688 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1689 UNI_DISPLAY_REGEX) : startpos;
1690 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1693 PerlIO_printf(Perl_debug_log,
1694 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1695 PL_colors[4],PL_colors[5],PL_colors[0],
1698 len0 > 60 ? "..." : "",
1700 (int)(len1 > 60 ? 60 : len1),
1702 (len1 > 60 ? "..." : "")
1706 /* Simplest case: anchored match need be tried only once. */
1707 /* [unless only anchor is BOL and multiline is set] */
1708 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1709 if (s == startpos && regtry(prog, startpos))
1711 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1712 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1717 dontbother = minlen - 1;
1718 end = HOP3c(strend, -dontbother, strbeg) - 1;
1719 /* for multiline we only have to try after newlines */
1720 if (prog->check_substr || prog->check_utf8) {
1724 if (regtry(prog, s))
1729 if (prog->reganch & RE_USE_INTUIT) {
1730 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1741 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1742 if (regtry(prog, s))
1749 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1750 if (regtry(prog, PL_reg_ganch))
1755 /* Messy cases: unanchored match. */
1756 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1757 /* we have /x+whatever/ */
1758 /* it must be a one character string (XXXX Except UTF?) */
1763 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1764 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1765 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1768 while (s < strend) {
1770 DEBUG_r( did_match = 1 );
1771 if (regtry(prog, s)) goto got_it;
1773 while (s < strend && *s == ch)
1780 while (s < strend) {
1782 DEBUG_r( did_match = 1 );
1783 if (regtry(prog, s)) goto got_it;
1785 while (s < strend && *s == ch)
1791 DEBUG_r(if (!did_match)
1792 PerlIO_printf(Perl_debug_log,
1793 "Did not find anchored character...\n")
1797 else if (prog->anchored_substr != Nullsv
1798 || prog->anchored_utf8 != Nullsv
1799 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1800 && prog->float_max_offset < strend - s)) {
1805 char *last1; /* Last position checked before */
1809 if (prog->anchored_substr || prog->anchored_utf8) {
1810 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1811 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1812 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1813 back_max = back_min = prog->anchored_offset;
1815 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1816 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1817 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1818 back_max = prog->float_max_offset;
1819 back_min = prog->float_min_offset;
1821 if (must == &PL_sv_undef)
1822 /* could not downgrade utf8 check substring, so must fail */
1825 last = HOP3c(strend, /* Cannot start after this */
1826 -(I32)(CHR_SVLEN(must)
1827 - (SvTAIL(must) != 0) + back_min), strbeg);
1830 last1 = HOPc(s, -1);
1832 last1 = s - 1; /* bogus */
1834 /* XXXX check_substr already used to find `s', can optimize if
1835 check_substr==must. */
1837 dontbother = end_shift;
1838 strend = HOPc(strend, -dontbother);
1839 while ( (s <= last) &&
1840 ((flags & REXEC_SCREAM)
1841 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1842 end_shift, &scream_pos, 0))
1843 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1844 (unsigned char*)strend, must,
1845 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1846 DEBUG_r( did_match = 1 );
1847 if (HOPc(s, -back_max) > last1) {
1848 last1 = HOPc(s, -back_min);
1849 s = HOPc(s, -back_max);
1852 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1854 last1 = HOPc(s, -back_min);
1858 while (s <= last1) {
1859 if (regtry(prog, s))
1865 while (s <= last1) {
1866 if (regtry(prog, s))
1872 DEBUG_r(if (!did_match)
1873 PerlIO_printf(Perl_debug_log,
1874 "Did not find %s substr `%s%.*s%s'%s...\n",
1875 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1876 ? "anchored" : "floating"),
1878 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1880 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1884 else if ((c = prog->regstclass)) {
1885 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1886 /* don't bother with what can't match */
1887 strend = HOPc(strend, -(minlen - 1));
1889 SV *prop = sv_newmortal();
1897 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1898 UNI_DISPLAY_REGEX) :
1900 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1902 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1903 len1 = UTF ? SvCUR(dsv1) : strend - s;
1904 PerlIO_printf(Perl_debug_log,
1905 "Matching stclass `%*.*s' against `%*.*s'\n",
1909 if (find_byclass(prog, c, s, strend, startpos, 0))
1911 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1915 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1920 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1921 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1922 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1924 if (flags & REXEC_SCREAM) {
1925 last = screaminstr(sv, float_real, s - strbeg,
1926 end_shift, &scream_pos, 1); /* last one */
1928 last = scream_olds; /* Only one occurrence. */
1932 char *little = SvPV(float_real, len);
1934 if (SvTAIL(float_real)) {
1935 if (memEQ(strend - len + 1, little, len - 1))
1936 last = strend - len + 1;
1937 else if (!PL_multiline)
1938 last = memEQ(strend - len, little, len)
1939 ? strend - len : Nullch;
1945 last = rninstr(s, strend, little, little + len);
1947 last = strend; /* matching `$' */
1951 DEBUG_r(PerlIO_printf(Perl_debug_log,
1952 "%sCan't trim the tail, match fails (should not happen)%s\n",
1953 PL_colors[4],PL_colors[5]));
1954 goto phooey; /* Should not happen! */
1956 dontbother = strend - last + prog->float_min_offset;
1958 if (minlen && (dontbother < minlen))
1959 dontbother = minlen - 1;
1960 strend -= dontbother; /* this one's always in bytes! */
1961 /* We don't know much -- general case. */
1964 if (regtry(prog, s))
1973 if (regtry(prog, s))
1975 } while (s++ < strend);
1983 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1985 if (PL_reg_eval_set) {
1986 /* Preserve the current value of $^R */
1987 if (oreplsv != GvSV(PL_replgv))
1988 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1989 restored, the value remains
1991 restore_pos(aTHX_ 0);
1994 /* make sure $`, $&, $', and $digit will work later */
1995 if ( !(flags & REXEC_NOT_FIRST) ) {
1996 if (RX_MATCH_COPIED(prog)) {
1997 Safefree(prog->subbeg);
1998 RX_MATCH_COPIED_off(prog);
2000 if (flags & REXEC_COPY_STR) {
2001 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2003 s = savepvn(strbeg, i);
2006 RX_MATCH_COPIED_on(prog);
2009 prog->subbeg = strbeg;
2010 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2017 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2018 PL_colors[4],PL_colors[5]));
2019 if (PL_reg_eval_set)
2020 restore_pos(aTHX_ 0);
2025 - regtry - try match at specific point
2027 STATIC I32 /* 0 failure, 1 success */
2028 S_regtry(pTHX_ regexp *prog, char *startpos)
2036 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2038 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2041 PL_reg_eval_set = RS_init;
2043 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2044 (IV)(PL_stack_sp - PL_stack_base));
2046 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2047 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2048 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2050 /* Apparently this is not needed, judging by wantarray. */
2051 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2052 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2055 /* Make $_ available to executed code. */
2056 if (PL_reg_sv != DEFSV) {
2057 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2062 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2063 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2064 /* prepare for quick setting of pos */
2065 sv_magic(PL_reg_sv, (SV*)0,
2066 PERL_MAGIC_regex_global, Nullch, 0);
2067 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2071 PL_reg_oldpos = mg->mg_len;
2072 SAVEDESTRUCTOR_X(restore_pos, 0);
2074 if (!PL_reg_curpm) {
2075 Newz(22,PL_reg_curpm, 1, PMOP);
2078 SV* repointer = newSViv(0);
2079 /* so we know which PL_regex_padav element is PL_reg_curpm */
2080 SvFLAGS(repointer) |= SVf_BREAK;
2081 av_push(PL_regex_padav,repointer);
2082 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2083 PL_regex_pad = AvARRAY(PL_regex_padav);
2087 PM_SETRE(PL_reg_curpm, prog);
2088 PL_reg_oldcurpm = PL_curpm;
2089 PL_curpm = PL_reg_curpm;
2090 if (RX_MATCH_COPIED(prog)) {
2091 /* Here is a serious problem: we cannot rewrite subbeg,
2092 since it may be needed if this match fails. Thus
2093 $` inside (?{}) could fail... */
2094 PL_reg_oldsaved = prog->subbeg;
2095 PL_reg_oldsavedlen = prog->sublen;
2096 RX_MATCH_COPIED_off(prog);
2099 PL_reg_oldsaved = Nullch;
2100 prog->subbeg = PL_bostr;
2101 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2103 prog->startp[0] = startpos - PL_bostr;
2104 PL_reginput = startpos;
2105 PL_regstartp = prog->startp;
2106 PL_regendp = prog->endp;
2107 PL_reglastparen = &prog->lastparen;
2108 PL_reglastcloseparen = &prog->lastcloseparen;
2109 prog->lastparen = 0;
2111 DEBUG_r(PL_reg_starttry = startpos);
2112 if (PL_reg_start_tmpl <= prog->nparens) {
2113 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2114 if(PL_reg_start_tmp)
2115 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2117 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2120 /* XXXX What this code is doing here?!!! There should be no need
2121 to do this again and again, PL_reglastparen should take care of
2124 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2125 * Actually, the code in regcppop() (which Ilya may be meaning by
2126 * PL_reglastparen), is not needed at all by the test suite
2127 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2128 * enough, for building DynaLoader, or otherwise this
2129 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2130 * will happen. Meanwhile, this code *is* needed for the
2131 * above-mentioned test suite tests to succeed. The common theme
2132 * on those tests seems to be returning null fields from matches.
2137 if (prog->nparens) {
2138 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2145 if (regmatch(prog->program + 1)) {
2146 prog->endp[0] = PL_reginput - PL_bostr;
2149 REGCP_UNWIND(lastcp);
2153 #define RE_UNWIND_BRANCH 1
2154 #define RE_UNWIND_BRANCHJ 2
2158 typedef struct { /* XX: makes sense to enlarge it... */
2162 } re_unwind_generic_t;
2175 } re_unwind_branch_t;
2177 typedef union re_unwind_t {
2179 re_unwind_generic_t generic;
2180 re_unwind_branch_t branch;
2183 #define sayYES goto yes
2184 #define sayNO goto no
2185 #define sayNO_ANYOF goto no_anyof
2186 #define sayYES_FINAL goto yes_final
2187 #define sayYES_LOUD goto yes_loud
2188 #define sayNO_FINAL goto no_final
2189 #define sayNO_SILENT goto do_no
2190 #define saySAME(x) if (x) goto yes; else goto no
2192 #define REPORT_CODE_OFF 24
2195 - regmatch - main matching routine
2197 * Conceptually the strategy is simple: check to see whether the current
2198 * node matches, call self recursively to see whether the rest matches,
2199 * and then act accordingly. In practice we make some effort to avoid
2200 * recursion, in particular by going through "ordinary" nodes (that don't
2201 * need to know whether the rest of the match failed) by a loop instead of
2204 /* [lwall] I've hoisted the register declarations to the outer block in order to
2205 * maybe save a little bit of pushing and popping on the stack. It also takes
2206 * advantage of machines that use a register save mask on subroutine entry.
2208 STATIC I32 /* 0 failure, 1 success */
2209 S_regmatch(pTHX_ regnode *prog)
2211 register regnode *scan; /* Current node. */
2212 regnode *next; /* Next node. */
2213 regnode *inner; /* Next node in internal branch. */
2214 register I32 nextchr; /* renamed nextchr - nextchar colides with
2215 function of same name */
2216 register I32 n; /* no or next */
2217 register I32 ln = 0; /* len or last */
2218 register char *s = Nullch; /* operand or save */
2219 register char *locinput = PL_reginput;
2220 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2221 int minmod = 0, sw = 0, logical = 0;
2224 I32 firstcp = PL_savestack_ix;
2226 register bool do_utf8 = PL_reg_match_utf8;
2228 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2229 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2230 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2237 /* Note that nextchr is a byte even in UTF */
2238 nextchr = UCHARAT(locinput);
2240 while (scan != NULL) {
2243 SV *prop = sv_newmortal();
2244 int docolor = *PL_colors[0];
2245 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2246 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2247 /* The part of the string before starttry has one color
2248 (pref0_len chars), between starttry and current
2249 position another one (pref_len - pref0_len chars),
2250 after the current position the third one.
2251 We assume that pref0_len <= pref_len, otherwise we
2252 decrease pref0_len. */
2253 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2254 ? (5 + taill) - l : locinput - PL_bostr;
2257 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2259 pref0_len = pref_len - (locinput - PL_reg_starttry);
2260 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2261 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2262 ? (5 + taill) - pref_len : PL_regeol - locinput);
2263 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2267 if (pref0_len > pref_len)
2268 pref0_len = pref_len;
2269 regprop(prop, scan);
2273 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2274 pref0_len, 60, UNI_DISPLAY_REGEX) :
2275 locinput - pref_len;
2276 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2277 char *s1 = do_utf8 ?
2278 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2279 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2280 locinput - pref_len + pref0_len;
2281 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2282 char *s2 = do_utf8 ?
2283 pv_uni_display(dsv2, (U8*)locinput,
2284 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2286 int len2 = do_utf8 ? strlen(s2) : l;
2287 PerlIO_printf(Perl_debug_log,
2288 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2289 (IV)(locinput - PL_bostr),
2296 (docolor ? "" : "> <"),
2300 15 - l - pref_len + 1,
2302 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2307 next = scan + NEXT_OFF(scan);
2313 if (locinput == PL_bostr || (PL_multiline &&
2314 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2316 /* regtill = regbol; */
2321 if (locinput == PL_bostr ||
2322 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2328 if (locinput == PL_bostr)
2332 if (locinput == PL_reg_ganch)
2342 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2347 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2349 if (PL_regeol - locinput > 1)
2353 if (PL_regeol != locinput)
2357 if (!nextchr && locinput >= PL_regeol)
2360 locinput += PL_utf8skip[nextchr];
2361 if (locinput > PL_regeol)
2363 nextchr = UCHARAT(locinput);
2366 nextchr = UCHARAT(++locinput);
2369 if (!nextchr && locinput >= PL_regeol)
2371 nextchr = UCHARAT(++locinput);
2374 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2377 locinput += PL_utf8skip[nextchr];
2378 if (locinput > PL_regeol)
2380 nextchr = UCHARAT(locinput);
2383 nextchr = UCHARAT(++locinput);
2388 if (do_utf8 != UTF) {
2389 /* The target and the pattern have differing utf8ness. */
2395 /* The target is utf8, the pattern is not utf8. */
2399 if (NATIVE_TO_UNI(*(U8*)s) !=
2400 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2402 0 : UTF8_ALLOW_ANY))
2409 /* The target is not utf8, the pattern is utf8. */
2413 if (NATIVE_TO_UNI(*((U8*)l)) !=
2414 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2416 0 : UTF8_ALLOW_ANY))
2423 nextchr = UCHARAT(locinput);
2426 /* The target and the pattern have the same utf8ness. */
2427 /* Inline the first character, for speed. */
2428 if (UCHARAT(s) != nextchr)
2430 if (PL_regeol - locinput < ln)
2432 if (ln > 1 && memNE(s, locinput, ln))
2435 nextchr = UCHARAT(locinput);
2438 PL_reg_flags |= RF_tainted;
2444 if (do_utf8 || UTF) {
2445 /* Either target or the pattern are utf8. */
2447 char *e = PL_regeol;
2449 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2450 l, &e, 0, do_utf8)) {
2451 /* One more case for the sharp s:
2452 * pack("U0U*", 0xDF) =~ /ss/i,
2453 * the 0xC3 0x9F are the UTF-8
2454 * byte sequence for the U+00DF. */
2456 toLOWER(s[0]) == 's' &&
2458 toLOWER(s[1]) == 's' &&
2465 nextchr = UCHARAT(locinput);
2469 /* Neither the target and the pattern are utf8. */
2471 /* Inline the first character, for speed. */
2472 if (UCHARAT(s) != nextchr &&
2473 UCHARAT(s) != ((OP(scan) == EXACTF)
2474 ? PL_fold : PL_fold_locale)[nextchr])
2476 if (PL_regeol - locinput < ln)
2478 if (ln > 1 && (OP(scan) == EXACTF
2479 ? ibcmp(s, locinput, ln)
2480 : ibcmp_locale(s, locinput, ln)))
2483 nextchr = UCHARAT(locinput);
2487 STRLEN inclasslen = PL_regeol - locinput;
2489 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2491 if (locinput >= PL_regeol)
2493 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2494 nextchr = UCHARAT(locinput);
2499 nextchr = UCHARAT(locinput);
2500 if (!REGINCLASS(scan, (U8*)locinput))
2502 if (!nextchr && locinput >= PL_regeol)
2504 nextchr = UCHARAT(++locinput);
2508 /* If we might have the case of the German sharp s
2509 * in a casefolding Unicode character class. */
2511 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2512 locinput += SHARP_S_SKIP;
2513 nextchr = UCHARAT(locinput);
2519 PL_reg_flags |= RF_tainted;
2525 LOAD_UTF8_CHARCLASS(alnum,"a");
2526 if (!(OP(scan) == ALNUM
2527 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2528 : isALNUM_LC_utf8((U8*)locinput)))
2532 locinput += PL_utf8skip[nextchr];
2533 nextchr = UCHARAT(locinput);
2536 if (!(OP(scan) == ALNUM
2537 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2539 nextchr = UCHARAT(++locinput);
2542 PL_reg_flags |= RF_tainted;
2545 if (!nextchr && locinput >= PL_regeol)
2548 LOAD_UTF8_CHARCLASS(alnum,"a");
2549 if (OP(scan) == NALNUM
2550 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2551 : isALNUM_LC_utf8((U8*)locinput))
2555 locinput += PL_utf8skip[nextchr];
2556 nextchr = UCHARAT(locinput);
2559 if (OP(scan) == NALNUM
2560 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2562 nextchr = UCHARAT(++locinput);
2566 PL_reg_flags |= RF_tainted;
2570 /* was last char in word? */
2572 if (locinput == PL_bostr)
2575 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2577 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2579 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2580 ln = isALNUM_uni(ln);
2581 LOAD_UTF8_CHARCLASS(alnum,"a");
2582 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2585 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2586 n = isALNUM_LC_utf8((U8*)locinput);
2590 ln = (locinput != PL_bostr) ?
2591 UCHARAT(locinput - 1) : '\n';
2592 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2594 n = isALNUM(nextchr);
2597 ln = isALNUM_LC(ln);
2598 n = isALNUM_LC(nextchr);
2601 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2602 OP(scan) == BOUNDL))
2606 PL_reg_flags |= RF_tainted;
2612 if (UTF8_IS_CONTINUED(nextchr)) {
2613 LOAD_UTF8_CHARCLASS(space," ");
2614 if (!(OP(scan) == SPACE
2615 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2616 : isSPACE_LC_utf8((U8*)locinput)))
2620 locinput += PL_utf8skip[nextchr];
2621 nextchr = UCHARAT(locinput);
2624 if (!(OP(scan) == SPACE
2625 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2627 nextchr = UCHARAT(++locinput);
2630 if (!(OP(scan) == SPACE
2631 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2633 nextchr = UCHARAT(++locinput);
2637 PL_reg_flags |= RF_tainted;
2640 if (!nextchr && locinput >= PL_regeol)
2643 LOAD_UTF8_CHARCLASS(space," ");
2644 if (OP(scan) == NSPACE
2645 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2646 : isSPACE_LC_utf8((U8*)locinput))
2650 locinput += PL_utf8skip[nextchr];
2651 nextchr = UCHARAT(locinput);
2654 if (OP(scan) == NSPACE
2655 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2657 nextchr = UCHARAT(++locinput);
2660 PL_reg_flags |= RF_tainted;
2666 LOAD_UTF8_CHARCLASS(digit,"0");
2667 if (!(OP(scan) == DIGIT
2668 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2669 : isDIGIT_LC_utf8((U8*)locinput)))
2673 locinput += PL_utf8skip[nextchr];
2674 nextchr = UCHARAT(locinput);
2677 if (!(OP(scan) == DIGIT
2678 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2680 nextchr = UCHARAT(++locinput);
2683 PL_reg_flags |= RF_tainted;
2686 if (!nextchr && locinput >= PL_regeol)
2689 LOAD_UTF8_CHARCLASS(digit,"0");
2690 if (OP(scan) == NDIGIT
2691 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2692 : isDIGIT_LC_utf8((U8*)locinput))
2696 locinput += PL_utf8skip[nextchr];
2697 nextchr = UCHARAT(locinput);
2700 if (OP(scan) == NDIGIT
2701 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2703 nextchr = UCHARAT(++locinput);
2706 if (locinput >= PL_regeol)
2709 LOAD_UTF8_CHARCLASS(mark,"~");
2710 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2712 locinput += PL_utf8skip[nextchr];
2713 while (locinput < PL_regeol &&
2714 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2715 locinput += UTF8SKIP(locinput);
2716 if (locinput > PL_regeol)
2721 nextchr = UCHARAT(locinput);
2724 PL_reg_flags |= RF_tainted;
2728 n = ARG(scan); /* which paren pair */
2729 ln = PL_regstartp[n];
2730 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2731 if ((I32)*PL_reglastparen < n || ln == -1)
2732 sayNO; /* Do not match unless seen CLOSEn. */
2733 if (ln == PL_regendp[n])
2737 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2739 char *e = PL_bostr + PL_regendp[n];
2741 * Note that we can't do the "other character" lookup trick as
2742 * in the 8-bit case (no pun intended) because in Unicode we
2743 * have to map both upper and title case to lower case.
2745 if (OP(scan) == REFF) {
2746 STRLEN ulen1, ulen2;
2747 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2748 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2752 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2753 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2754 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2761 nextchr = UCHARAT(locinput);
2765 /* Inline the first character, for speed. */
2766 if (UCHARAT(s) != nextchr &&
2768 (UCHARAT(s) != ((OP(scan) == REFF
2769 ? PL_fold : PL_fold_locale)[nextchr]))))
2771 ln = PL_regendp[n] - ln;
2772 if (locinput + ln > PL_regeol)
2774 if (ln > 1 && (OP(scan) == REF
2775 ? memNE(s, locinput, ln)
2777 ? ibcmp(s, locinput, ln)
2778 : ibcmp_locale(s, locinput, ln))))
2781 nextchr = UCHARAT(locinput);
2792 OP_4tree *oop = PL_op;
2793 COP *ocurcop = PL_curcop;
2794 SV **ocurpad = PL_curpad;
2798 PL_op = (OP_4tree*)PL_regdata->data[n];
2799 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2800 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2801 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2805 CALLRUNOPS(aTHX); /* Scalar context. */
2808 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2816 PL_curpad = ocurpad;
2817 PL_curcop = ocurcop;
2819 if (logical == 2) { /* Postponed subexpression. */
2821 MAGIC *mg = Null(MAGIC*);
2823 CHECKPOINT cp, lastcp;
2826 if(SvROK(ret) || SvRMAGICAL(ret)) {
2827 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2830 mg = mg_find(sv, PERL_MAGIC_qr);
2833 re = (regexp *)mg->mg_obj;
2834 (void)ReREFCNT_inc(re);
2838 char *t = SvPV(ret, len);
2840 char *oprecomp = PL_regprecomp;
2841 I32 osize = PL_regsize;
2842 I32 onpar = PL_regnpar;
2845 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2846 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2848 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2849 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2851 PL_regprecomp = oprecomp;
2856 PerlIO_printf(Perl_debug_log,
2857 "Entering embedded `%s%.60s%s%s'\n",
2861 (strlen(re->precomp) > 60 ? "..." : ""))
2864 state.prev = PL_reg_call_cc;
2865 state.cc = PL_regcc;
2866 state.re = PL_reg_re;
2870 cp = regcppush(0); /* Save *all* the positions. */
2873 state.ss = PL_savestack_ix;
2874 *PL_reglastparen = 0;
2875 *PL_reglastcloseparen = 0;
2876 PL_reg_call_cc = &state;
2877 PL_reginput = locinput;
2878 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2879 ((re->reganch & ROPT_UTF8) != 0);
2880 if (toggleutf) PL_reg_flags ^= RF_utf8;
2882 /* XXXX This is too dramatic a measure... */
2885 if (regmatch(re->program + 1)) {
2886 /* Even though we succeeded, we need to restore
2887 global variables, since we may be wrapped inside
2888 SUSPEND, thus the match may be not finished yet. */
2890 /* XXXX Do this only if SUSPENDed? */
2891 PL_reg_call_cc = state.prev;
2892 PL_regcc = state.cc;
2893 PL_reg_re = state.re;
2894 cache_re(PL_reg_re);
2895 if (toggleutf) PL_reg_flags ^= RF_utf8;
2897 /* XXXX This is too dramatic a measure... */
2900 /* These are needed even if not SUSPEND. */
2906 REGCP_UNWIND(lastcp);
2908 PL_reg_call_cc = state.prev;
2909 PL_regcc = state.cc;
2910 PL_reg_re = state.re;
2911 cache_re(PL_reg_re);
2912 if (toggleutf) PL_reg_flags ^= RF_utf8;
2914 /* XXXX This is too dramatic a measure... */
2924 sv_setsv(save_scalar(PL_replgv), ret);
2928 n = ARG(scan); /* which paren pair */
2929 PL_reg_start_tmp[n] = locinput;
2934 n = ARG(scan); /* which paren pair */
2935 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2936 PL_regendp[n] = locinput - PL_bostr;
2937 if (n > (I32)*PL_reglastparen)
2938 *PL_reglastparen = n;
2939 *PL_reglastcloseparen = n;
2942 n = ARG(scan); /* which paren pair */
2943 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2946 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2948 next = NEXTOPER(NEXTOPER(scan));
2950 next = scan + ARG(scan);
2951 if (OP(next) == IFTHEN) /* Fake one. */
2952 next = NEXTOPER(NEXTOPER(next));
2956 logical = scan->flags;
2958 /*******************************************************************
2959 PL_regcc contains infoblock about the innermost (...)* loop, and
2960 a pointer to the next outer infoblock.
2962 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2964 1) After matching X, regnode for CURLYX is processed;
2966 2) This regnode creates infoblock on the stack, and calls
2967 regmatch() recursively with the starting point at WHILEM node;
2969 3) Each hit of WHILEM node tries to match A and Z (in the order
2970 depending on the current iteration, min/max of {min,max} and
2971 greediness). The information about where are nodes for "A"
2972 and "Z" is read from the infoblock, as is info on how many times "A"
2973 was already matched, and greediness.
2975 4) After A matches, the same WHILEM node is hit again.
2977 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2978 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2979 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2980 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2981 of the external loop.
2983 Currently present infoblocks form a tree with a stem formed by PL_curcc
2984 and whatever it mentions via ->next, and additional attached trees
2985 corresponding to temporarily unset infoblocks as in "5" above.
2987 In the following picture infoblocks for outer loop of
2988 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2989 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2990 infoblocks are drawn below the "reset" infoblock.
2992 In fact in the picture below we do not show failed matches for Z and T
2993 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2994 more obvious *why* one needs to *temporary* unset infoblocks.]
2996 Matched REx position InfoBlocks Comment
3000 Y A)*?Z)*?T x <- O <- I
3001 YA )*?Z)*?T x <- O <- I
3002 YA A)*?Z)*?T x <- O <- I
3003 YAA )*?Z)*?T x <- O <- I
3004 YAA Z)*?T x <- O # Temporary unset I
3007 YAAZ Y(A)*?Z)*?T x <- O
3010 YAAZY (A)*?Z)*?T x <- O
3013 YAAZY A)*?Z)*?T x <- O <- I
3016 YAAZYA )*?Z)*?T x <- O <- I
3019 YAAZYA Z)*?T x <- O # Temporary unset I
3025 YAAZYAZ T x # Temporary unset O
3032 *******************************************************************/
3035 CHECKPOINT cp = PL_savestack_ix;
3036 /* No need to save/restore up to this paren */
3037 I32 parenfloor = scan->flags;
3039 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3041 cc.oldcc = PL_regcc;
3043 /* XXXX Probably it is better to teach regpush to support
3044 parenfloor > PL_regsize... */
3045 if (parenfloor > (I32)*PL_reglastparen)
3046 parenfloor = *PL_reglastparen; /* Pessimization... */
3047 cc.parenfloor = parenfloor;
3049 cc.min = ARG1(scan);
3050 cc.max = ARG2(scan);
3051 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3055 PL_reginput = locinput;
3056 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3058 PL_regcc = cc.oldcc;
3064 * This is really hard to understand, because after we match
3065 * what we're trying to match, we must make sure the rest of
3066 * the REx is going to match for sure, and to do that we have
3067 * to go back UP the parse tree by recursing ever deeper. And
3068 * if it fails, we have to reset our parent's current state
3069 * that we can try again after backing off.
3072 CHECKPOINT cp, lastcp;
3073 CURCUR* cc = PL_regcc;
3074 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3076 n = cc->cur + 1; /* how many we know we matched */
3077 PL_reginput = locinput;
3080 PerlIO_printf(Perl_debug_log,
3081 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3082 REPORT_CODE_OFF+PL_regindent*2, "",
3083 (long)n, (long)cc->min,
3084 (long)cc->max, PTR2UV(cc))
3087 /* If degenerate scan matches "", assume scan done. */
3089 if (locinput == cc->lastloc && n >= cc->min) {
3090 PL_regcc = cc->oldcc;
3094 PerlIO_printf(Perl_debug_log,
3095 "%*s empty match detected, try continuation...\n",
3096 REPORT_CODE_OFF+PL_regindent*2, "")
3098 if (regmatch(cc->next))
3106 /* First just match a string of min scans. */
3110 cc->lastloc = locinput;
3111 if (regmatch(cc->scan))
3114 cc->lastloc = lastloc;
3119 /* Check whether we already were at this position.
3120 Postpone detection until we know the match is not
3121 *that* much linear. */
3122 if (!PL_reg_maxiter) {
3123 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3124 PL_reg_leftiter = PL_reg_maxiter;
3126 if (PL_reg_leftiter-- == 0) {
3127 I32 size = (PL_reg_maxiter + 7)/8;
3128 if (PL_reg_poscache) {
3129 if ((I32)PL_reg_poscache_size < size) {
3130 Renew(PL_reg_poscache, size, char);
3131 PL_reg_poscache_size = size;
3133 Zero(PL_reg_poscache, size, char);
3136 PL_reg_poscache_size = size;
3137 Newz(29, PL_reg_poscache, size, char);
3140 PerlIO_printf(Perl_debug_log,
3141 "%sDetected a super-linear match, switching on caching%s...\n",
3142 PL_colors[4], PL_colors[5])
3145 if (PL_reg_leftiter < 0) {
3146 I32 o = locinput - PL_bostr, b;
3148 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3151 if (PL_reg_poscache[o] & (1<<b)) {
3153 PerlIO_printf(Perl_debug_log,
3154 "%*s already tried at this position...\n",
3155 REPORT_CODE_OFF+PL_regindent*2, "")
3159 PL_reg_poscache[o] |= (1<<b);
3163 /* Prefer next over scan for minimal matching. */
3166 PL_regcc = cc->oldcc;
3169 cp = regcppush(cc->parenfloor);
3171 if (regmatch(cc->next)) {
3173 sayYES; /* All done. */
3175 REGCP_UNWIND(lastcp);
3181 if (n >= cc->max) { /* Maximum greed exceeded? */
3182 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3183 && !(PL_reg_flags & RF_warned)) {
3184 PL_reg_flags |= RF_warned;
3185 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3186 "Complex regular subexpression recursion",
3193 PerlIO_printf(Perl_debug_log,
3194 "%*s trying longer...\n",
3195 REPORT_CODE_OFF+PL_regindent*2, "")
3197 /* Try scanning more and see if it helps. */
3198 PL_reginput = locinput;
3200 cc->lastloc = locinput;
3201 cp = regcppush(cc->parenfloor);
3203 if (regmatch(cc->scan)) {
3207 REGCP_UNWIND(lastcp);
3210 cc->lastloc = lastloc;
3214 /* Prefer scan over next for maximal matching. */
3216 if (n < cc->max) { /* More greed allowed? */
3217 cp = regcppush(cc->parenfloor);
3219 cc->lastloc = locinput;
3221 if (regmatch(cc->scan)) {
3225 REGCP_UNWIND(lastcp);
3226 regcppop(); /* Restore some previous $<digit>s? */
3227 PL_reginput = locinput;
3229 PerlIO_printf(Perl_debug_log,
3230 "%*s failed, try continuation...\n",
3231 REPORT_CODE_OFF+PL_regindent*2, "")
3234 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3235 && !(PL_reg_flags & RF_warned)) {
3236 PL_reg_flags |= RF_warned;
3237 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3238 "Complex regular subexpression recursion",
3242 /* Failed deeper matches of scan, so see if this one works. */
3243 PL_regcc = cc->oldcc;
3246 if (regmatch(cc->next))
3252 cc->lastloc = lastloc;
3257 next = scan + ARG(scan);
3260 inner = NEXTOPER(NEXTOPER(scan));
3263 inner = NEXTOPER(scan);
3267 if (OP(next) != c1) /* No choice. */
3268 next = inner; /* Avoid recursion. */
3270 I32 lastparen = *PL_reglastparen;
3272 re_unwind_branch_t *uw;
3274 /* Put unwinding data on stack */
3275 unwind1 = SSNEWt(1,re_unwind_branch_t);
3276 uw = SSPTRt(unwind1,re_unwind_branch_t);
3279 uw->type = ((c1 == BRANCH)
3281 : RE_UNWIND_BRANCHJ);
3282 uw->lastparen = lastparen;
3284 uw->locinput = locinput;
3285 uw->nextchr = nextchr;
3287 uw->regindent = ++PL_regindent;
3290 REGCP_SET(uw->lastcp);
3292 /* Now go into the first branch */
3305 /* We suppose that the next guy does not need
3306 backtracking: in particular, it is of constant length,
3307 and has no parenths to influence future backrefs. */
3308 ln = ARG1(scan); /* min to match */
3309 n = ARG2(scan); /* max to match */
3310 paren = scan->flags;
3312 if (paren > PL_regsize)
3314 if (paren > (I32)*PL_reglastparen)
3315 *PL_reglastparen = paren;
3317 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3319 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3320 PL_reginput = locinput;
3323 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3325 /* if we matched something zero-length we don't need to
3326 backtrack - capturing parens are already defined, so
3327 the caveat in the maximal case doesn't apply
3329 XXXX if ln == 0, we can redo this check first time
3330 through the following loop
3333 n = ln; /* don't backtrack */
3334 locinput = PL_reginput;
3335 if (HAS_TEXT(next) || JUMPABLE(next)) {
3336 regnode *text_node = next;
3338 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3340 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3342 if (PL_regkind[(U8)OP(text_node)] == REF) {
3344 n = ARG(text_node); /* which paren pair */
3345 ln = PL_regstartp[n];
3346 /* assume yes if we haven't seen CLOSEn */
3348 (I32)*PL_reglastparen < n ||
3355 c1 = *(PL_bostr + ln);
3357 else { c1 = (U8)*STRING(text_node); }
3358 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3360 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3361 c2 = PL_fold_locale[c1];
3370 /* This may be improved if l == 0. */
3371 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3372 /* If it could work, try it. */
3374 UCHARAT(PL_reginput) == c1 ||
3375 UCHARAT(PL_reginput) == c2)
3379 PL_regstartp[paren] =
3380 HOPc(PL_reginput, -l) - PL_bostr;
3381 PL_regendp[paren] = PL_reginput - PL_bostr;
3384 PL_regendp[paren] = -1;
3388 REGCP_UNWIND(lastcp);
3390 /* Couldn't or didn't -- move forward. */
3391 PL_reginput = locinput;
3392 if (regrepeat_hard(scan, 1, &l)) {
3394 locinput = PL_reginput;
3401 n = regrepeat_hard(scan, n, &l);
3402 /* if we matched something zero-length we don't need to
3403 backtrack, unless the minimum count is zero and we
3404 are capturing the result - in that case the capture
3405 being defined or not may affect later execution
3407 if (n != 0 && l == 0 && !(paren && ln == 0))
3408 ln = n; /* don't backtrack */
3409 locinput = PL_reginput;
3411 PerlIO_printf(Perl_debug_log,
3412 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3413 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3417 if (HAS_TEXT(next) || JUMPABLE(next)) {
3418 regnode *text_node = next;
3420 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3422 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3424 if (PL_regkind[(U8)OP(text_node)] == REF) {
3426 n = ARG(text_node); /* which paren pair */
3427 ln = PL_regstartp[n];
3428 /* assume yes if we haven't seen CLOSEn */
3430 (I32)*PL_reglastparen < n ||
3437 c1 = *(PL_bostr + ln);
3439 else { c1 = (U8)*STRING(text_node); }
3441 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3443 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3444 c2 = PL_fold_locale[c1];
3455 /* If it could work, try it. */
3457 UCHARAT(PL_reginput) == c1 ||
3458 UCHARAT(PL_reginput) == c2)
3461 PerlIO_printf(Perl_debug_log,
3462 "%*s trying tail with n=%"IVdf"...\n",
3463 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3467 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3468 PL_regendp[paren] = PL_reginput - PL_bostr;
3471 PL_regendp[paren] = -1;
3475 REGCP_UNWIND(lastcp);
3477 /* Couldn't or didn't -- back up. */
3479 locinput = HOPc(locinput, -l);
3480 PL_reginput = locinput;
3487 paren = scan->flags; /* Which paren to set */
3488 if (paren > PL_regsize)
3490 if (paren > (I32)*PL_reglastparen)
3491 *PL_reglastparen = paren;
3492 ln = ARG1(scan); /* min to match */
3493 n = ARG2(scan); /* max to match */
3494 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3498 ln = ARG1(scan); /* min to match */
3499 n = ARG2(scan); /* max to match */
3500 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3505 scan = NEXTOPER(scan);
3511 scan = NEXTOPER(scan);
3515 * Lookahead to avoid useless match attempts
3516 * when we know what character comes next.
3520 * Used to only do .*x and .*?x, but now it allows
3521 * for )'s, ('s and (?{ ... })'s to be in the way
3522 * of the quantifier and the EXACT-like node. -- japhy
3525 if (HAS_TEXT(next) || JUMPABLE(next)) {
3527 regnode *text_node = next;
3529 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3531 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3533 if (PL_regkind[(U8)OP(text_node)] == REF) {
3535 n = ARG(text_node); /* which paren pair */
3536 ln = PL_regstartp[n];
3537 /* assume yes if we haven't seen CLOSEn */
3539 (I32)*PL_reglastparen < n ||
3544 goto assume_ok_easy;
3546 s = (U8*)PL_bostr + ln;
3548 else { s = (U8*)STRING(text_node); }
3552 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3554 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3555 c2 = PL_fold_locale[c1];
3558 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3559 STRLEN ulen1, ulen2;
3560 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3561 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3563 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3564 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3566 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3568 0 : UTF8_ALLOW_ANY);
3569 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3571 0 : UTF8_ALLOW_ANY);
3574 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3576 0 : UTF8_ALLOW_ANY);
3584 PL_reginput = locinput;
3588 if (ln && regrepeat(scan, ln) < ln)
3590 locinput = PL_reginput;
3593 char *e; /* Should not check after this */
3594 char *old = locinput;
3597 if (n == REG_INFTY) {
3600 while (UTF8_IS_CONTINUATION(*(U8*)e))
3606 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3610 e = locinput + n - ln;
3615 /* Find place 'next' could work */
3618 while (locinput <= e &&
3619 UCHARAT(locinput) != c1)
3622 while (locinput <= e
3623 && UCHARAT(locinput) != c1
3624 && UCHARAT(locinput) != c2)
3627 count = locinput - old;
3632 /* count initialised to
3633 * utf8_distance(old, locinput) */
3634 while (locinput <= e &&
3635 utf8n_to_uvchr((U8*)locinput,
3638 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3643 /* count initialised to
3644 * utf8_distance(old, locinput) */
3645 while (locinput <= e) {
3646 UV c = utf8n_to_uvchr((U8*)locinput,
3649 0 : UTF8_ALLOW_ANY);
3650 if (c == (UV)c1 || c == (UV)c2)
3659 /* PL_reginput == old now */
3660 if (locinput != old) {
3661 ln = 1; /* Did some */
3662 if (regrepeat(scan, count) < count)
3665 /* PL_reginput == locinput now */
3666 TRYPAREN(paren, ln, locinput);
3667 PL_reginput = locinput; /* Could be reset... */
3668 REGCP_UNWIND(lastcp);
3669 /* Couldn't or didn't -- move forward. */
3672 locinput += UTF8SKIP(locinput);
3679 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3683 c = utf8n_to_uvchr((U8*)PL_reginput,
3686 0 : UTF8_ALLOW_ANY);
3688 c = UCHARAT(PL_reginput);
3689 /* If it could work, try it. */
3690 if (c == (UV)c1 || c == (UV)c2)
3692 TRYPAREN(paren, ln, PL_reginput);
3693 REGCP_UNWIND(lastcp);
3696 /* If it could work, try it. */
3697 else if (c1 == -1000)
3699 TRYPAREN(paren, ln, PL_reginput);
3700 REGCP_UNWIND(lastcp);
3702 /* Couldn't or didn't -- move forward. */
3703 PL_reginput = locinput;
3704 if (regrepeat(scan, 1)) {
3706 locinput = PL_reginput;
3714 n = regrepeat(scan, n);
3715 locinput = PL_reginput;
3716 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3717 ((!PL_multiline && OP(next) != MEOL) ||
3718 OP(next) == SEOL || OP(next) == EOS))
3720 ln = n; /* why back off? */
3721 /* ...because $ and \Z can match before *and* after
3722 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3723 We should back off by one in this case. */
3724 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3733 c = utf8n_to_uvchr((U8*)PL_reginput,
3736 0 : UTF8_ALLOW_ANY);
3738 c = UCHARAT(PL_reginput);
3740 /* If it could work, try it. */
3741 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3743 TRYPAREN(paren, n, PL_reginput);
3744 REGCP_UNWIND(lastcp);
3746 /* Couldn't or didn't -- back up. */
3748 PL_reginput = locinput = HOPc(locinput, -1);
3756 c = utf8n_to_uvchr((U8*)PL_reginput,
3759 0 : UTF8_ALLOW_ANY);
3761 c = UCHARAT(PL_reginput);
3763 /* If it could work, try it. */
3764 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3766 TRYPAREN(paren, n, PL_reginput);
3767 REGCP_UNWIND(lastcp);
3769 /* Couldn't or didn't -- back up. */
3771 PL_reginput = locinput = HOPc(locinput, -1);
3778 if (PL_reg_call_cc) {
3779 re_cc_state *cur_call_cc = PL_reg_call_cc;
3780 CURCUR *cctmp = PL_regcc;
3781 regexp *re = PL_reg_re;
3782 CHECKPOINT cp, lastcp;
3784 cp = regcppush(0); /* Save *all* the positions. */
3786 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3788 PL_reginput = locinput; /* Make position available to
3790 cache_re(PL_reg_call_cc->re);
3791 PL_regcc = PL_reg_call_cc->cc;
3792 PL_reg_call_cc = PL_reg_call_cc->prev;
3793 if (regmatch(cur_call_cc->node)) {
3794 PL_reg_call_cc = cur_call_cc;
3798 REGCP_UNWIND(lastcp);
3800 PL_reg_call_cc = cur_call_cc;
3806 PerlIO_printf(Perl_debug_log,
3807 "%*s continuation failed...\n",
3808 REPORT_CODE_OFF+PL_regindent*2, "")
3812 if (locinput < PL_regtill) {
3813 DEBUG_r(PerlIO_printf(Perl_debug_log,
3814 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3816 (long)(locinput - PL_reg_starttry),
3817 (long)(PL_regtill - PL_reg_starttry),
3819 sayNO_FINAL; /* Cannot match: too short. */
3821 PL_reginput = locinput; /* put where regtry can find it */
3822 sayYES_FINAL; /* Success! */
3824 PL_reginput = locinput; /* put where regtry can find it */
3825 sayYES_LOUD; /* Success! */
3828 PL_reginput = locinput;
3833 s = HOPBACKc(locinput, scan->flags);
3839 PL_reginput = locinput;
3844 s = HOPBACKc(locinput, scan->flags);
3850 PL_reginput = locinput;
3853 inner = NEXTOPER(NEXTOPER(scan));
3854 if (regmatch(inner) != n) {
3869 if (OP(scan) == SUSPEND) {
3870 locinput = PL_reginput;
3871 nextchr = UCHARAT(locinput);
3876 next = scan + ARG(scan);
3881 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3882 PTR2UV(scan), OP(scan));
3883 Perl_croak(aTHX_ "regexp memory corruption");
3890 * We get here only if there's trouble -- normally "case END" is
3891 * the terminating point.
3893 Perl_croak(aTHX_ "corrupted regexp pointers");
3899 PerlIO_printf(Perl_debug_log,
3900 "%*s %scould match...%s\n",
3901 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3905 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3906 PL_colors[4],PL_colors[5]));
3912 #if 0 /* Breaks $^R */
3920 PerlIO_printf(Perl_debug_log,
3921 "%*s %sfailed...%s\n",
3922 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3928 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3931 case RE_UNWIND_BRANCH:
3932 case RE_UNWIND_BRANCHJ:
3934 re_unwind_branch_t *uwb = &(uw->branch);
3935 I32 lastparen = uwb->lastparen;
3937 REGCP_UNWIND(uwb->lastcp);
3938 for (n = *PL_reglastparen; n > lastparen; n--)
3940 *PL_reglastparen = n;
3941 scan = next = uwb->next;
3943 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3944 ? BRANCH : BRANCHJ) ) { /* Failure */
3951 /* Have more choice yet. Reuse the same uwb. */
3953 if ((n = (uwb->type == RE_UNWIND_BRANCH
3954 ? NEXT_OFF(next) : ARG(next))))
3957 next = NULL; /* XXXX Needn't unwinding in this case... */
3959 next = NEXTOPER(scan);
3960 if (uwb->type == RE_UNWIND_BRANCHJ)
3961 next = NEXTOPER(next);
3962 locinput = uwb->locinput;
3963 nextchr = uwb->nextchr;
3965 PL_regindent = uwb->regindent;
3972 Perl_croak(aTHX_ "regexp unwind memory corruption");
3983 - regrepeat - repeatedly match something simple, report how many
3986 * [This routine now assumes that it will only match on things of length 1.
3987 * That was true before, but now we assume scan - reginput is the count,
3988 * rather than incrementing count on every character. [Er, except utf8.]]
3991 S_regrepeat(pTHX_ regnode *p, I32 max)
3993 register char *scan;
3995 register char *loceol = PL_regeol;
3996 register I32 hardcount = 0;
3997 register bool do_utf8 = PL_reg_match_utf8;
4000 if (max == REG_INFTY)
4002 else if (max < loceol - scan)
4003 loceol = scan + max;
4008 while (scan < loceol && hardcount < max && *scan != '\n') {
4009 scan += UTF8SKIP(scan);
4013 while (scan < loceol && *scan != '\n')
4020 while (scan < loceol && hardcount < max) {
4021 scan += UTF8SKIP(scan);
4031 case EXACT: /* length of string is 1 */
4033 while (scan < loceol && UCHARAT(scan) == c)
4036 case EXACTF: /* length of string is 1 */
4038 while (scan < loceol &&
4039 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4042 case EXACTFL: /* length of string is 1 */
4043 PL_reg_flags |= RF_tainted;
4045 while (scan < loceol &&
4046 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4052 while (hardcount < max && scan < loceol &&
4053 reginclass(p, (U8*)scan, 0, do_utf8)) {
4054 scan += UTF8SKIP(scan);
4058 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4065 LOAD_UTF8_CHARCLASS(alnum,"a");
4066 while (hardcount < max && scan < loceol &&
4067 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4068 scan += UTF8SKIP(scan);
4072 while (scan < loceol && isALNUM(*scan))
4077 PL_reg_flags |= RF_tainted;
4080 while (hardcount < max && scan < loceol &&
4081 isALNUM_LC_utf8((U8*)scan)) {
4082 scan += UTF8SKIP(scan);
4086 while (scan < loceol && isALNUM_LC(*scan))
4093 LOAD_UTF8_CHARCLASS(alnum,"a");
4094 while (hardcount < max && scan < loceol &&
4095 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4096 scan += UTF8SKIP(scan);
4100 while (scan < loceol && !isALNUM(*scan))
4105 PL_reg_flags |= RF_tainted;
4108 while (hardcount < max && scan < loceol &&
4109 !isALNUM_LC_utf8((U8*)scan)) {
4110 scan += UTF8SKIP(scan);
4114 while (scan < loceol && !isALNUM_LC(*scan))
4121 LOAD_UTF8_CHARCLASS(space," ");
4122 while (hardcount < max && scan < loceol &&
4124 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4125 scan += UTF8SKIP(scan);
4129 while (scan < loceol && isSPACE(*scan))
4134 PL_reg_flags |= RF_tainted;
4137 while (hardcount < max && scan < loceol &&
4138 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4139 scan += UTF8SKIP(scan);
4143 while (scan < loceol && isSPACE_LC(*scan))
4150 LOAD_UTF8_CHARCLASS(space," ");
4151 while (hardcount < max && scan < loceol &&
4153 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4154 scan += UTF8SKIP(scan);
4158 while (scan < loceol && !isSPACE(*scan))
4163 PL_reg_flags |= RF_tainted;
4166 while (hardcount < max && scan < loceol &&
4167 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4168 scan += UTF8SKIP(scan);
4172 while (scan < loceol && !isSPACE_LC(*scan))
4179 LOAD_UTF8_CHARCLASS(digit,"0");
4180 while (hardcount < max && scan < loceol &&
4181 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4182 scan += UTF8SKIP(scan);
4186 while (scan < loceol && isDIGIT(*scan))
4193 LOAD_UTF8_CHARCLASS(digit,"0");
4194 while (hardcount < max && scan < loceol &&
4195 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4196 scan += UTF8SKIP(scan);
4200 while (scan < loceol && !isDIGIT(*scan))
4204 default: /* Called on something of 0 width. */
4205 break; /* So match right here or not at all. */
4211 c = scan - PL_reginput;
4216 SV *prop = sv_newmortal();
4219 PerlIO_printf(Perl_debug_log,
4220 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4221 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4228 - regrepeat_hard - repeatedly match something, report total lenth and length
4230 * The repeater is supposed to have constant length.
4234 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4236 register char *scan = Nullch;
4237 register char *start;
4238 register char *loceol = PL_regeol;
4240 I32 count = 0, res = 1;
4245 start = PL_reginput;
4246 if (PL_reg_match_utf8) {
4247 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4250 while (start < PL_reginput) {
4252 start += UTF8SKIP(start);
4263 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4265 *lp = l = PL_reginput - start;
4266 if (max != REG_INFTY && l*max < loceol - scan)
4267 loceol = scan + l*max;
4280 - regclass_swash - prepare the utf8 swash
4284 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4290 if (PL_regdata && PL_regdata->count) {
4293 if (PL_regdata->what[n] == 's') {
4294 SV *rv = (SV*)PL_regdata->data[n];
4295 AV *av = (AV*)SvRV((SV*)rv);
4296 SV **ary = AvARRAY(av);
4299 /* See the end of regcomp.c:S_reglass() for
4300 * documentation of these array elements. */
4303 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4304 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4308 else if (si && doinit) {
4309 sw = swash_init("utf8", "", si, 1, 0);
4310 (void)av_store(av, 1, sw);
4326 - reginclass - determine if a character falls into a character class
4328 The n is the ANYOF regnode, the p is the target string, lenp
4329 is pointer to the maximum length of how far to go in the p
4330 (if the lenp is zero, UTF8SKIP(p) is used),
4331 do_utf8 tells whether the target string is in UTF-8.
4336 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4338 char flags = ANYOF_FLAGS(n);
4344 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4345 c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4346 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4348 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4349 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4352 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4353 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4356 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4360 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4363 if (swash_fetch(sw, p, do_utf8))
4365 else if (flags & ANYOF_FOLD) {
4366 if (!match && lenp && av) {
4369 for (i = 0; i <= av_len(av); i++) {
4370 SV* sv = *av_fetch(av, i, FALSE);
4372 char *s = SvPV(sv, len);
4374 if (len <= plen && memEQ(s, (char*)p, len)) {
4382 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4385 to_utf8_fold(p, tmpbuf, &tmplen);
4386 if (swash_fetch(sw, tmpbuf, do_utf8))
4392 if (match && lenp && *lenp == 0)
4393 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4395 if (!match && c < 256) {
4396 if (ANYOF_BITMAP_TEST(n, c))
4398 else if (flags & ANYOF_FOLD) {
4401 if (flags & ANYOF_LOCALE) {
4402 PL_reg_flags |= RF_tainted;
4403 f = PL_fold_locale[c];
4407 if (f != c && ANYOF_BITMAP_TEST(n, f))
4411 if (!match && (flags & ANYOF_CLASS)) {
4412 PL_reg_flags |= RF_tainted;
4414 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4415 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4416 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4417 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4418 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4419 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4420 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4421 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4422 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4423 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4424 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4425 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4426 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4427 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4428 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4429 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4430 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4431 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4432 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4433 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4434 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4435 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4436 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4437 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4438 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4439 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4440 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4441 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4442 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4443 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4444 ) /* How's that for a conditional? */
4451 return (flags & ANYOF_INVERT) ? !match : match;
4455 S_reghop(pTHX_ U8 *s, I32 off)
4457 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4461 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4464 while (off-- && s < lim) {
4465 /* XXX could check well-formedness here */
4473 if (UTF8_IS_CONTINUED(*s)) {
4474 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4477 /* XXX could check well-formedness here */
4485 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4487 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4491 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4494 while (off-- && s < lim) {
4495 /* XXX could check well-formedness here */
4505 if (UTF8_IS_CONTINUED(*s)) {
4506 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4509 /* XXX could check well-formedness here */
4521 restore_pos(pTHX_ void *arg)
4523 if (PL_reg_eval_set) {
4524 if (PL_reg_oldsaved) {
4525 PL_reg_re->subbeg = PL_reg_oldsaved;
4526 PL_reg_re->sublen = PL_reg_oldsavedlen;
4527 RX_MATCH_COPIED_on(PL_reg_re);
4529 PL_reg_magic->mg_len = PL_reg_oldpos;
4530 PL_reg_eval_set = 0;
4531 PL_curpm = PL_reg_oldcurpm;
4536 S_to_utf8_substr(pTHX_ register regexp *prog)
4539 if (prog->float_substr && !prog->float_utf8) {
4540 prog->float_utf8 = sv = NEWSV(117, 0);
4541 SvSetSV(sv, prog->float_substr);
4542 sv_utf8_upgrade(sv);
4543 if (SvTAIL(prog->float_substr))
4545 if (prog->float_substr == prog->check_substr)
4546 prog->check_utf8 = sv;
4548 if (prog->anchored_substr && !prog->anchored_utf8) {
4549 prog->anchored_utf8 = sv = NEWSV(118, 0);
4550 SvSetSV(sv, prog->anchored_substr);
4551 sv_utf8_upgrade(sv);
4552 if (SvTAIL(prog->anchored_substr))
4554 if (prog->anchored_substr == prog->check_substr)
4555 prog->check_utf8 = sv;
4560 S_to_byte_substr(pTHX_ register regexp *prog)
4563 if (prog->float_utf8 && !prog->float_substr) {
4564 prog->float_substr = sv = NEWSV(117, 0);
4565 SvSetSV(sv, prog->float_utf8);
4566 if (sv_utf8_downgrade(sv, TRUE)) {
4567 if (SvTAIL(prog->float_utf8))
4571 prog->float_substr = sv = &PL_sv_undef;
4573 if (prog->float_utf8 == prog->check_utf8)
4574 prog->check_substr = sv;
4576 if (prog->anchored_utf8 && !prog->anchored_substr) {
4577 prog->anchored_substr = sv = NEWSV(118, 0);
4578 SvSetSV(sv, prog->anchored_utf8);
4579 if (sv_utf8_downgrade(sv, TRUE)) {
4580 if (SvTAIL(prog->anchored_utf8))
4584 prog->anchored_substr = sv = &PL_sv_undef;
4586 if (prog->anchored_utf8 == prog->check_utf8)
4587 prog->check_substr = sv;