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);
403 RX_MATCH_UTF8_set(prog,do_utf8);
405 if (prog->reganch & ROPT_UTF8) {
406 DEBUG_r(PerlIO_printf(Perl_debug_log,
407 "UTF-8 regex...\n"));
408 PL_reg_flags |= RF_utf8;
412 char *s = PL_reg_match_utf8 ?
413 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
415 int len = PL_reg_match_utf8 ?
416 strlen(s) : strend - strpos;
419 if (PL_reg_match_utf8)
420 DEBUG_r(PerlIO_printf(Perl_debug_log,
421 "UTF-8 target...\n"));
422 PerlIO_printf(Perl_debug_log,
423 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
424 PL_colors[4],PL_colors[5],PL_colors[0],
427 (strlen(prog->precomp) > 60 ? "..." : ""),
429 (int)(len > 60 ? 60 : len),
431 (len > 60 ? "..." : "")
435 /* CHR_DIST() would be more correct here but it makes things slow. */
436 if (prog->minlen > strend - strpos) {
437 DEBUG_r(PerlIO_printf(Perl_debug_log,
438 "String too short... [re_intuit_start]\n"));
441 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
444 if (!prog->check_utf8 && prog->check_substr)
445 to_utf8_substr(prog);
446 check = prog->check_utf8;
448 if (!prog->check_substr && prog->check_utf8)
449 to_byte_substr(prog);
450 check = prog->check_substr;
452 if (check == &PL_sv_undef) {
453 DEBUG_r(PerlIO_printf(Perl_debug_log,
454 "Non-utf string cannot match utf check string\n"));
457 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
458 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
459 || ( (prog->reganch & ROPT_ANCH_BOL)
460 && !PL_multiline ) ); /* Check after \n? */
463 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
464 | ROPT_IMPLICIT)) /* not a real BOL */
465 /* SvCUR is not set on references: SvRV and SvPVX overlap */
467 && (strpos != strbeg)) {
468 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
471 if (prog->check_offset_min == prog->check_offset_max &&
472 !(prog->reganch & ROPT_CANY_SEEN)) {
473 /* Substring at constant offset from beg-of-str... */
476 s = HOP3c(strpos, prog->check_offset_min, strend);
478 slen = SvCUR(check); /* >= 1 */
480 if ( strend - s > slen || strend - s < slen - 1
481 || (strend - s == slen && strend[-1] != '\n')) {
482 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
485 /* Now should match s[0..slen-2] */
487 if (slen && (*SvPVX(check) != *s
489 && memNE(SvPVX(check), s, slen)))) {
491 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
495 else if (*SvPVX(check) != *s
496 || ((slen = SvCUR(check)) > 1
497 && memNE(SvPVX(check), s, slen)))
499 goto success_at_start;
502 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
504 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
505 end_shift = prog->minlen - start_shift -
506 CHR_SVLEN(check) + (SvTAIL(check) != 0);
508 I32 end = prog->check_offset_max + CHR_SVLEN(check)
509 - (SvTAIL(check) != 0);
510 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
512 if (end_shift < eshift)
516 else { /* Can match at random position */
519 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
520 /* Should be nonnegative! */
521 end_shift = prog->minlen - start_shift -
522 CHR_SVLEN(check) + (SvTAIL(check) != 0);
525 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
527 Perl_croak(aTHX_ "panic: end_shift");
531 /* Find a possible match in the region s..strend by looking for
532 the "check" substring in the region corrected by start/end_shift. */
533 if (flags & REXEC_SCREAM) {
534 I32 p = -1; /* Internal iterator of scream. */
535 I32 *pp = data ? data->scream_pos : &p;
537 if (PL_screamfirst[BmRARE(check)] >= 0
538 || ( BmRARE(check) == '\n'
539 && (BmPREVIOUS(check) == SvCUR(check) - 1)
541 s = screaminstr(sv, check,
542 start_shift + (s - strbeg), end_shift, pp, 0);
545 /* we may be pointing at the wrong string */
546 if (s && RX_MATCH_COPIED(prog))
547 s = prog->subbeg + (s - SvPVX(sv));
549 *data->scream_olds = s;
551 else if (prog->reganch & ROPT_CANY_SEEN)
552 s = fbm_instr((U8*)(s + start_shift),
553 (U8*)(strend - end_shift),
554 check, PL_multiline ? FBMrf_MULTILINE : 0);
556 s = fbm_instr(HOP3(s, start_shift, strend),
557 HOP3(strend, -end_shift, strbeg),
558 check, PL_multiline ? FBMrf_MULTILINE : 0);
560 /* Update the count-of-usability, remove useless subpatterns,
563 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
564 (s ? "Found" : "Did not find"),
565 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
567 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
569 PL_colors[1], (SvTAIL(check) ? "$" : ""),
570 (s ? " at offset " : "...\n") ) );
577 /* Finish the diagnostic message */
578 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
580 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
581 Start with the other substr.
582 XXXX no SCREAM optimization yet - and a very coarse implementation
583 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
584 *always* match. Probably should be marked during compile...
585 Probably it is right to do no SCREAM here...
588 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
589 /* Take into account the "other" substring. */
590 /* XXXX May be hopelessly wrong for UTF... */
593 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
596 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
600 t = s - prog->check_offset_max;
601 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
603 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
608 t = HOP3c(t, prog->anchored_offset, strend);
609 if (t < other_last) /* These positions already checked */
611 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
614 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
615 /* On end-of-str: see comment below. */
616 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
617 if (must == &PL_sv_undef) {
619 DEBUG_r(must = prog->anchored_utf8); /* for debug */
624 HOP3(HOP3(last1, prog->anchored_offset, strend)
625 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
627 PL_multiline ? FBMrf_MULTILINE : 0
629 DEBUG_r(PerlIO_printf(Perl_debug_log,
630 "%s anchored substr `%s%.*s%s'%s",
631 (s ? "Found" : "Contradicts"),
634 - (SvTAIL(must)!=0)),
636 PL_colors[1], (SvTAIL(must) ? "$" : "")));
638 if (last1 >= last2) {
639 DEBUG_r(PerlIO_printf(Perl_debug_log,
640 ", giving up...\n"));
643 DEBUG_r(PerlIO_printf(Perl_debug_log,
644 ", trying floating at offset %ld...\n",
645 (long)(HOP3c(s1, 1, strend) - i_strpos)));
646 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
647 s = HOP3c(last, 1, strend);
651 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
652 (long)(s - i_strpos)));
653 t = HOP3c(s, -prog->anchored_offset, strbeg);
654 other_last = HOP3c(s, 1, strend);
662 else { /* Take into account the floating substring. */
667 t = HOP3c(s, -start_shift, strbeg);
669 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
670 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
671 last = HOP3c(t, prog->float_max_offset, strend);
672 s = HOP3c(t, prog->float_min_offset, strend);
675 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
676 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
677 /* fbm_instr() takes into account exact value of end-of-str
678 if the check is SvTAIL(ed). Since false positives are OK,
679 and end-of-str is not later than strend we are OK. */
680 if (must == &PL_sv_undef) {
682 DEBUG_r(must = prog->float_utf8); /* for debug message */
685 s = fbm_instr((unsigned char*)s,
686 (unsigned char*)last + SvCUR(must)
688 must, PL_multiline ? FBMrf_MULTILINE : 0);
689 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
690 (s ? "Found" : "Contradicts"),
692 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
694 PL_colors[1], (SvTAIL(must) ? "$" : "")));
697 DEBUG_r(PerlIO_printf(Perl_debug_log,
698 ", giving up...\n"));
701 DEBUG_r(PerlIO_printf(Perl_debug_log,
702 ", trying anchored starting at offset %ld...\n",
703 (long)(s1 + 1 - i_strpos)));
705 s = HOP3c(t, 1, strend);
709 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
710 (long)(s - i_strpos)));
711 other_last = s; /* Fix this later. --Hugo */
720 t = s - prog->check_offset_max;
721 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
723 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
725 /* Fixed substring is found far enough so that the match
726 cannot start at strpos. */
728 if (ml_anch && t[-1] != '\n') {
729 /* Eventually fbm_*() should handle this, but often
730 anchored_offset is not 0, so this check will not be wasted. */
731 /* XXXX In the code below we prefer to look for "^" even in
732 presence of anchored substrings. And we search even
733 beyond the found float position. These pessimizations
734 are historical artefacts only. */
736 while (t < strend - prog->minlen) {
738 if (t < check_at - prog->check_offset_min) {
739 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
740 /* Since we moved from the found position,
741 we definitely contradict the found anchored
742 substr. Due to the above check we do not
743 contradict "check" substr.
744 Thus we can arrive here only if check substr
745 is float. Redo checking for "other"=="fixed".
748 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
749 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
750 goto do_other_anchored;
752 /* We don't contradict the found floating substring. */
753 /* XXXX Why not check for STCLASS? */
755 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
756 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
759 /* Position contradicts check-string */
760 /* XXXX probably better to look for check-string
761 than for "\n", so one should lower the limit for t? */
762 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
763 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
764 other_last = strpos = s = t + 1;
769 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
770 PL_colors[0],PL_colors[1]));
774 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
775 PL_colors[0],PL_colors[1]));
779 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
782 /* The found string does not prohibit matching at strpos,
783 - no optimization of calling REx engine can be performed,
784 unless it was an MBOL and we are not after MBOL,
785 or a future STCLASS check will fail this. */
787 /* Even in this situation we may use MBOL flag if strpos is offset
788 wrt the start of the string. */
789 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
790 && (strpos != strbeg) && strpos[-1] != '\n'
791 /* May be due to an implicit anchor of m{.*foo} */
792 && !(prog->reganch & ROPT_IMPLICIT))
797 DEBUG_r( if (ml_anch)
798 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
799 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
802 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
804 prog->check_utf8 /* Could be deleted already */
805 && --BmUSEFUL(prog->check_utf8) < 0
806 && (prog->check_utf8 == prog->float_utf8)
808 prog->check_substr /* Could be deleted already */
809 && --BmUSEFUL(prog->check_substr) < 0
810 && (prog->check_substr == prog->float_substr)
813 /* If flags & SOMETHING - do not do it many times on the same match */
814 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
815 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
816 if (do_utf8 ? prog->check_substr : prog->check_utf8)
817 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
818 prog->check_substr = prog->check_utf8 = Nullsv; /* disable */
819 prog->float_substr = prog->float_utf8 = Nullsv; /* clear */
820 check = Nullsv; /* abort */
822 /* XXXX This is a remnant of the old implementation. It
823 looks wasteful, since now INTUIT can use many
825 prog->reganch &= ~RE_USE_INTUIT;
832 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
833 if (prog->regstclass) {
834 /* minlen == 0 is possible if regstclass is \b or \B,
835 and the fixed substr is ''$.
836 Since minlen is already taken into account, s+1 is before strend;
837 accidentally, minlen >= 1 guaranties no false positives at s + 1
838 even for \b or \B. But (minlen? 1 : 0) below assumes that
839 regstclass does not come from lookahead... */
840 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
841 This leaves EXACTF only, which is dealt with in find_byclass(). */
842 U8* str = (U8*)STRING(prog->regstclass);
843 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
844 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
846 char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
847 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
848 : (prog->float_substr || prog->float_utf8
849 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
852 char *startpos = strbeg;
855 if (prog->reganch & ROPT_UTF8) {
856 PL_regdata = prog->data;
860 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
865 if (endpos == strend) {
866 DEBUG_r( PerlIO_printf(Perl_debug_log,
867 "Could not match STCLASS...\n") );
870 DEBUG_r( PerlIO_printf(Perl_debug_log,
871 "This position contradicts STCLASS...\n") );
872 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
874 /* Contradict one of substrings */
875 if (prog->anchored_substr || prog->anchored_utf8) {
876 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
877 DEBUG_r( what = "anchored" );
879 s = HOP3c(t, 1, strend);
880 if (s + start_shift + end_shift > strend) {
881 /* XXXX Should be taken into account earlier? */
882 DEBUG_r( PerlIO_printf(Perl_debug_log,
883 "Could not match STCLASS...\n") );
888 DEBUG_r( PerlIO_printf(Perl_debug_log,
889 "Looking for %s substr starting at offset %ld...\n",
890 what, (long)(s + start_shift - i_strpos)) );
893 /* Have both, check_string is floating */
894 if (t + start_shift >= check_at) /* Contradicts floating=check */
895 goto retry_floating_check;
896 /* Recheck anchored substring, but not floating... */
900 DEBUG_r( PerlIO_printf(Perl_debug_log,
901 "Looking for anchored substr starting at offset %ld...\n",
902 (long)(other_last - i_strpos)) );
903 goto do_other_anchored;
905 /* Another way we could have checked stclass at the
906 current position only: */
911 DEBUG_r( PerlIO_printf(Perl_debug_log,
912 "Looking for /%s^%s/m starting at offset %ld...\n",
913 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
916 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
918 /* Check is floating subtring. */
919 retry_floating_check:
920 t = check_at - start_shift;
921 DEBUG_r( what = "floating" );
922 goto hop_and_restart;
925 DEBUG_r(PerlIO_printf(Perl_debug_log,
926 "By STCLASS: moving %ld --> %ld\n",
927 (long)(t - i_strpos), (long)(s - i_strpos))
931 DEBUG_r(PerlIO_printf(Perl_debug_log,
932 "Does not contradict STCLASS...\n");
937 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
938 PL_colors[4], (check ? "Guessed" : "Giving up"),
939 PL_colors[5], (long)(s - i_strpos)) );
942 fail_finish: /* Substring not found */
943 if (prog->check_substr || prog->check_utf8) /* could be removed already */
944 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
946 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
947 PL_colors[4],PL_colors[5]));
951 /* We know what class REx starts with. Try to find this position... */
953 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
955 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
961 register I32 tmp = 1; /* Scratch variable? */
962 register bool do_utf8 = PL_reg_match_utf8;
964 /* We know what class it must start with. */
969 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
970 !UTF8_IS_INVARIANT((U8)s[0]) ?
971 reginclass(c, (U8*)s, 0, do_utf8) :
972 REGINCLASS(c, (U8*)s)) {
973 if (tmp && (norun || regtry(prog, s)))
987 if (REGINCLASS(c, (U8*)s) ||
988 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
989 /* The assignment of 2 is intentional:
990 * for the folded sharp s, the skip is 2. */
991 (skip = SHARP_S_SKIP))) {
992 if (tmp && (norun || regtry(prog, s)))
1004 while (s < strend) {
1005 if (tmp && (norun || regtry(prog, s)))
1016 STRLEN ulen1, ulen2;
1017 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
1018 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1020 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1021 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1023 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1024 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1025 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1026 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1037 c2 = PL_fold_locale[c1];
1039 e = HOP3c(strend, -(I32)ln, s);
1042 e = s; /* Due to minlen logic of intuit() */
1044 /* The idea in the EXACTF* cases is to first find the
1045 * first character of the EXACTF* node and then, if
1046 * necessary, case-insensitively compare the full
1047 * text of the node. The c1 and c2 are the first
1048 * characters (though in Unicode it gets a bit
1049 * more complicated because there are more cases
1050 * than just upper and lower: one needs to use
1051 * the so-called folding case for case-insensitive
1052 * matching (called "loose matching" in Unicode).
1053 * ibcmp_utf8() will do just that. */
1057 U8 tmpbuf [UTF8_MAXLEN+1];
1058 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1059 STRLEN len, foldlen;
1063 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1065 0 : UTF8_ALLOW_ANY);
1068 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1069 m, (char **)0, ln, (bool)UTF))
1070 && (norun || regtry(prog, s)) )
1073 uvchr_to_utf8(tmpbuf, c);
1074 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1076 && (f == c1 || f == c2)
1077 && (ln == foldlen ||
1078 !ibcmp_utf8((char *) foldbuf,
1079 (char **)0, foldlen, do_utf8,
1081 (char **)0, ln, (bool)UTF))
1082 && (norun || regtry(prog, s)) )
1090 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1092 0 : UTF8_ALLOW_ANY);
1094 /* Handle some of the three Greek sigmas cases.
1095 * Note that not all the possible combinations
1096 * are handled here: some of them are handled
1097 * by the standard folding rules, and some of
1098 * them (the character class or ANYOF cases)
1099 * are handled during compiletime in
1100 * regexec.c:S_regclass(). */
1101 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1102 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1103 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1105 if ( (c == c1 || c == c2)
1107 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1108 m, (char **)0, ln, (bool)UTF))
1109 && (norun || regtry(prog, s)) )
1112 uvchr_to_utf8(tmpbuf, c);
1113 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1115 && (f == c1 || f == c2)
1116 && (ln == foldlen ||
1117 !ibcmp_utf8((char *) foldbuf,
1118 (char **)0, foldlen, do_utf8,
1120 (char **)0, ln, (bool)UTF))
1121 && (norun || regtry(prog, s)) )
1132 && (ln == 1 || !(OP(c) == EXACTF
1134 : ibcmp_locale(s, m, ln)))
1135 && (norun || regtry(prog, s)) )
1141 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1142 && (ln == 1 || !(OP(c) == EXACTF
1144 : ibcmp_locale(s, m, ln)))
1145 && (norun || regtry(prog, s)) )
1152 PL_reg_flags |= RF_tainted;
1159 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1161 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1163 tmp = ((OP(c) == BOUND ?
1164 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1165 LOAD_UTF8_CHARCLASS(alnum,"a");
1166 while (s < strend) {
1167 if (tmp == !(OP(c) == BOUND ?
1168 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1169 isALNUM_LC_utf8((U8*)s)))
1172 if ((norun || regtry(prog, s)))
1179 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1180 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1181 while (s < strend) {
1183 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1185 if ((norun || regtry(prog, s)))
1191 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1195 PL_reg_flags |= RF_tainted;
1202 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1204 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1206 tmp = ((OP(c) == NBOUND ?
1207 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1208 LOAD_UTF8_CHARCLASS(alnum,"a");
1209 while (s < strend) {
1210 if (tmp == !(OP(c) == NBOUND ?
1211 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1212 isALNUM_LC_utf8((U8*)s)))
1214 else if ((norun || regtry(prog, s)))
1220 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1221 tmp = ((OP(c) == NBOUND ?
1222 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1223 while (s < strend) {
1225 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1227 else if ((norun || regtry(prog, s)))
1232 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1237 LOAD_UTF8_CHARCLASS(alnum,"a");
1238 while (s < strend) {
1239 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1240 if (tmp && (norun || regtry(prog, s)))
1251 while (s < strend) {
1253 if (tmp && (norun || regtry(prog, s)))
1265 PL_reg_flags |= RF_tainted;
1267 while (s < strend) {
1268 if (isALNUM_LC_utf8((U8*)s)) {
1269 if (tmp && (norun || regtry(prog, s)))
1280 while (s < strend) {
1281 if (isALNUM_LC(*s)) {
1282 if (tmp && (norun || regtry(prog, s)))
1295 LOAD_UTF8_CHARCLASS(alnum,"a");
1296 while (s < strend) {
1297 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1298 if (tmp && (norun || regtry(prog, s)))
1309 while (s < strend) {
1311 if (tmp && (norun || regtry(prog, s)))
1323 PL_reg_flags |= RF_tainted;
1325 while (s < strend) {
1326 if (!isALNUM_LC_utf8((U8*)s)) {
1327 if (tmp && (norun || regtry(prog, s)))
1338 while (s < strend) {
1339 if (!isALNUM_LC(*s)) {
1340 if (tmp && (norun || regtry(prog, s)))
1353 LOAD_UTF8_CHARCLASS(space," ");
1354 while (s < strend) {
1355 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1356 if (tmp && (norun || regtry(prog, s)))
1367 while (s < strend) {
1369 if (tmp && (norun || regtry(prog, s)))
1381 PL_reg_flags |= RF_tainted;
1383 while (s < strend) {
1384 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1385 if (tmp && (norun || regtry(prog, s)))
1396 while (s < strend) {
1397 if (isSPACE_LC(*s)) {
1398 if (tmp && (norun || regtry(prog, s)))
1411 LOAD_UTF8_CHARCLASS(space," ");
1412 while (s < strend) {
1413 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1414 if (tmp && (norun || regtry(prog, s)))
1425 while (s < strend) {
1427 if (tmp && (norun || regtry(prog, s)))
1439 PL_reg_flags |= RF_tainted;
1441 while (s < strend) {
1442 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1443 if (tmp && (norun || regtry(prog, s)))
1454 while (s < strend) {
1455 if (!isSPACE_LC(*s)) {
1456 if (tmp && (norun || regtry(prog, s)))
1469 LOAD_UTF8_CHARCLASS(digit,"0");
1470 while (s < strend) {
1471 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1472 if (tmp && (norun || regtry(prog, s)))
1483 while (s < strend) {
1485 if (tmp && (norun || regtry(prog, s)))
1497 PL_reg_flags |= RF_tainted;
1499 while (s < strend) {
1500 if (isDIGIT_LC_utf8((U8*)s)) {
1501 if (tmp && (norun || regtry(prog, s)))
1512 while (s < strend) {
1513 if (isDIGIT_LC(*s)) {
1514 if (tmp && (norun || regtry(prog, s)))
1527 LOAD_UTF8_CHARCLASS(digit,"0");
1528 while (s < strend) {
1529 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1530 if (tmp && (norun || regtry(prog, s)))
1541 while (s < strend) {
1543 if (tmp && (norun || regtry(prog, s)))
1555 PL_reg_flags |= RF_tainted;
1557 while (s < strend) {
1558 if (!isDIGIT_LC_utf8((U8*)s)) {
1559 if (tmp && (norun || regtry(prog, s)))
1570 while (s < strend) {
1571 if (!isDIGIT_LC(*s)) {
1572 if (tmp && (norun || regtry(prog, s)))
1584 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1593 - regexec_flags - match a regexp against a string
1596 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1597 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1598 /* strend: pointer to null at end of string */
1599 /* strbeg: real beginning of string */
1600 /* minend: end of match must be >=minend after stringarg. */
1601 /* data: May be used for some additional optimizations. */
1602 /* nosave: For optimizations. */
1605 register regnode *c;
1606 register char *startpos = stringarg;
1607 I32 minlen; /* must match at least this many chars */
1608 I32 dontbother = 0; /* how many characters not to try at end */
1609 /* I32 start_shift = 0; */ /* Offset of the start to find
1610 constant substr. */ /* CC */
1611 I32 end_shift = 0; /* Same for the end. */ /* CC */
1612 I32 scream_pos = -1; /* Internal iterator of scream. */
1614 SV* oreplsv = GvSV(PL_replgv);
1615 bool do_utf8 = DO_UTF8(sv);
1617 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1618 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1620 RX_MATCH_UTF8_set(prog,do_utf8);
1626 PL_regnarrate = DEBUG_r_TEST;
1629 /* Be paranoid... */
1630 if (prog == NULL || startpos == NULL) {
1631 Perl_croak(aTHX_ "NULL regexp parameter");
1635 minlen = prog->minlen;
1636 if (strend - startpos < minlen) {
1637 DEBUG_r(PerlIO_printf(Perl_debug_log,
1638 "String too short [regexec_flags]...\n"));
1642 /* Check validity of program. */
1643 if (UCHARAT(prog->program) != REG_MAGIC) {
1644 Perl_croak(aTHX_ "corrupted regexp program");
1648 PL_reg_eval_set = 0;
1651 if (prog->reganch & ROPT_UTF8)
1652 PL_reg_flags |= RF_utf8;
1654 /* Mark beginning of line for ^ and lookbehind. */
1655 PL_regbol = startpos;
1659 /* Mark end of line for $ (and such) */
1662 /* see how far we have to get to not match where we matched before */
1663 PL_regtill = startpos+minend;
1665 /* We start without call_cc context. */
1668 /* If there is a "must appear" string, look for it. */
1671 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1674 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1675 PL_reg_ganch = startpos;
1676 else if (sv && SvTYPE(sv) >= SVt_PVMG
1678 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1679 && mg->mg_len >= 0) {
1680 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1681 if (prog->reganch & ROPT_ANCH_GPOS) {
1682 if (s > PL_reg_ganch)
1687 else /* pos() not defined */
1688 PL_reg_ganch = strbeg;
1691 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1692 re_scream_pos_data d;
1694 d.scream_olds = &scream_olds;
1695 d.scream_pos = &scream_pos;
1696 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1698 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1699 goto phooey; /* not present */
1705 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1706 UNI_DISPLAY_REGEX) :
1708 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1709 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1710 UNI_DISPLAY_REGEX) : startpos;
1711 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1714 PerlIO_printf(Perl_debug_log,
1715 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1716 PL_colors[4],PL_colors[5],PL_colors[0],
1719 len0 > 60 ? "..." : "",
1721 (int)(len1 > 60 ? 60 : len1),
1723 (len1 > 60 ? "..." : "")
1727 /* Simplest case: anchored match need be tried only once. */
1728 /* [unless only anchor is BOL and multiline is set] */
1729 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1730 if (s == startpos && regtry(prog, startpos))
1732 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1733 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1738 dontbother = minlen - 1;
1739 end = HOP3c(strend, -dontbother, strbeg) - 1;
1740 /* for multiline we only have to try after newlines */
1741 if (prog->check_substr || prog->check_utf8) {
1745 if (regtry(prog, s))
1750 if (prog->reganch & RE_USE_INTUIT) {
1751 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1762 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1763 if (regtry(prog, s))
1770 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1771 if (regtry(prog, PL_reg_ganch))
1776 /* Messy cases: unanchored match. */
1777 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1778 /* we have /x+whatever/ */
1779 /* it must be a one character string (XXXX Except UTF?) */
1784 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1785 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1786 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1789 while (s < strend) {
1791 DEBUG_r( did_match = 1 );
1792 if (regtry(prog, s)) goto got_it;
1794 while (s < strend && *s == ch)
1801 while (s < strend) {
1803 DEBUG_r( did_match = 1 );
1804 if (regtry(prog, s)) goto got_it;
1806 while (s < strend && *s == ch)
1812 DEBUG_r(if (!did_match)
1813 PerlIO_printf(Perl_debug_log,
1814 "Did not find anchored character...\n")
1818 else if (prog->anchored_substr != Nullsv
1819 || prog->anchored_utf8 != Nullsv
1820 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1821 && prog->float_max_offset < strend - s)) {
1826 char *last1; /* Last position checked before */
1830 if (prog->anchored_substr || prog->anchored_utf8) {
1831 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1832 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1833 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1834 back_max = back_min = prog->anchored_offset;
1836 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1837 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1838 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1839 back_max = prog->float_max_offset;
1840 back_min = prog->float_min_offset;
1842 if (must == &PL_sv_undef)
1843 /* could not downgrade utf8 check substring, so must fail */
1846 last = HOP3c(strend, /* Cannot start after this */
1847 -(I32)(CHR_SVLEN(must)
1848 - (SvTAIL(must) != 0) + back_min), strbeg);
1851 last1 = HOPc(s, -1);
1853 last1 = s - 1; /* bogus */
1855 /* XXXX check_substr already used to find `s', can optimize if
1856 check_substr==must. */
1858 dontbother = end_shift;
1859 strend = HOPc(strend, -dontbother);
1860 while ( (s <= last) &&
1861 ((flags & REXEC_SCREAM)
1862 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1863 end_shift, &scream_pos, 0))
1864 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1865 (unsigned char*)strend, must,
1866 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1867 /* we may be pointing at the wrong string */
1868 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1869 s = prog->subbeg + (s - SvPVX(sv));
1870 DEBUG_r( did_match = 1 );
1871 if (HOPc(s, -back_max) > last1) {
1872 last1 = HOPc(s, -back_min);
1873 s = HOPc(s, -back_max);
1876 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1878 last1 = HOPc(s, -back_min);
1882 while (s <= last1) {
1883 if (regtry(prog, s))
1889 while (s <= last1) {
1890 if (regtry(prog, s))
1896 DEBUG_r(if (!did_match)
1897 PerlIO_printf(Perl_debug_log,
1898 "Did not find %s substr `%s%.*s%s'%s...\n",
1899 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1900 ? "anchored" : "floating"),
1902 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1904 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1908 else if ((c = prog->regstclass)) {
1910 I32 op = (U8)OP(prog->regstclass);
1911 /* don't bother with what can't match */
1912 if (PL_regkind[op] != EXACT && op != CANY)
1913 strend = HOPc(strend, -(minlen - 1));
1916 SV *prop = sv_newmortal();
1924 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1925 UNI_DISPLAY_REGEX) :
1927 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1929 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1930 len1 = UTF ? SvCUR(dsv1) : strend - s;
1931 PerlIO_printf(Perl_debug_log,
1932 "Matching stclass `%*.*s' against `%*.*s'\n",
1936 if (find_byclass(prog, c, s, strend, startpos, 0))
1938 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1942 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1947 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1948 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1949 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1951 if (flags & REXEC_SCREAM) {
1952 last = screaminstr(sv, float_real, s - strbeg,
1953 end_shift, &scream_pos, 1); /* last one */
1955 last = scream_olds; /* Only one occurrence. */
1956 /* we may be pointing at the wrong string */
1957 else if (RX_MATCH_COPIED(prog))
1958 s = prog->subbeg + (s - SvPVX(sv));
1962 char *little = SvPV(float_real, len);
1964 if (SvTAIL(float_real)) {
1965 if (memEQ(strend - len + 1, little, len - 1))
1966 last = strend - len + 1;
1967 else if (!PL_multiline)
1968 last = memEQ(strend - len, little, len)
1969 ? strend - len : Nullch;
1975 last = rninstr(s, strend, little, little + len);
1977 last = strend; /* matching `$' */
1981 DEBUG_r(PerlIO_printf(Perl_debug_log,
1982 "%sCan't trim the tail, match fails (should not happen)%s\n",
1983 PL_colors[4],PL_colors[5]));
1984 goto phooey; /* Should not happen! */
1986 dontbother = strend - last + prog->float_min_offset;
1988 if (minlen && (dontbother < minlen))
1989 dontbother = minlen - 1;
1990 strend -= dontbother; /* this one's always in bytes! */
1991 /* We don't know much -- general case. */
1994 if (regtry(prog, s))
2003 if (regtry(prog, s))
2005 } while (s++ < strend);
2013 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2015 if (PL_reg_eval_set) {
2016 /* Preserve the current value of $^R */
2017 if (oreplsv != GvSV(PL_replgv))
2018 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2019 restored, the value remains
2021 restore_pos(aTHX_ 0);
2024 /* make sure $`, $&, $', and $digit will work later */
2025 if ( !(flags & REXEC_NOT_FIRST) ) {
2026 if (RX_MATCH_COPIED(prog)) {
2027 Safefree(prog->subbeg);
2028 RX_MATCH_COPIED_off(prog);
2030 if (flags & REXEC_COPY_STR) {
2031 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2033 s = savepvn(strbeg, i);
2036 RX_MATCH_COPIED_on(prog);
2039 prog->subbeg = strbeg;
2040 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2047 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2048 PL_colors[4],PL_colors[5]));
2049 if (PL_reg_eval_set)
2050 restore_pos(aTHX_ 0);
2055 - regtry - try match at specific point
2057 STATIC I32 /* 0 failure, 1 success */
2058 S_regtry(pTHX_ regexp *prog, char *startpos)
2066 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2068 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2071 PL_reg_eval_set = RS_init;
2073 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2074 (IV)(PL_stack_sp - PL_stack_base));
2076 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2077 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2078 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2080 /* Apparently this is not needed, judging by wantarray. */
2081 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2082 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2085 /* Make $_ available to executed code. */
2086 if (PL_reg_sv != DEFSV) {
2087 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2092 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2093 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2094 /* prepare for quick setting of pos */
2095 sv_magic(PL_reg_sv, (SV*)0,
2096 PERL_MAGIC_regex_global, Nullch, 0);
2097 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2101 PL_reg_oldpos = mg->mg_len;
2102 SAVEDESTRUCTOR_X(restore_pos, 0);
2104 if (!PL_reg_curpm) {
2105 Newz(22,PL_reg_curpm, 1, PMOP);
2108 SV* repointer = newSViv(0);
2109 /* so we know which PL_regex_padav element is PL_reg_curpm */
2110 SvFLAGS(repointer) |= SVf_BREAK;
2111 av_push(PL_regex_padav,repointer);
2112 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2113 PL_regex_pad = AvARRAY(PL_regex_padav);
2117 PM_SETRE(PL_reg_curpm, prog);
2118 PL_reg_oldcurpm = PL_curpm;
2119 PL_curpm = PL_reg_curpm;
2120 if (RX_MATCH_COPIED(prog)) {
2121 /* Here is a serious problem: we cannot rewrite subbeg,
2122 since it may be needed if this match fails. Thus
2123 $` inside (?{}) could fail... */
2124 PL_reg_oldsaved = prog->subbeg;
2125 PL_reg_oldsavedlen = prog->sublen;
2126 RX_MATCH_COPIED_off(prog);
2129 PL_reg_oldsaved = Nullch;
2130 prog->subbeg = PL_bostr;
2131 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2133 prog->startp[0] = startpos - PL_bostr;
2134 PL_reginput = startpos;
2135 PL_regstartp = prog->startp;
2136 PL_regendp = prog->endp;
2137 PL_reglastparen = &prog->lastparen;
2138 PL_reglastcloseparen = &prog->lastcloseparen;
2139 prog->lastparen = 0;
2140 prog->lastcloseparen = 0;
2142 DEBUG_r(PL_reg_starttry = startpos);
2143 if (PL_reg_start_tmpl <= prog->nparens) {
2144 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2145 if(PL_reg_start_tmp)
2146 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2148 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2151 /* XXXX What this code is doing here?!!! There should be no need
2152 to do this again and again, PL_reglastparen should take care of
2155 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2156 * Actually, the code in regcppop() (which Ilya may be meaning by
2157 * PL_reglastparen), is not needed at all by the test suite
2158 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2159 * enough, for building DynaLoader, or otherwise this
2160 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2161 * will happen. Meanwhile, this code *is* needed for the
2162 * above-mentioned test suite tests to succeed. The common theme
2163 * on those tests seems to be returning null fields from matches.
2168 if (prog->nparens) {
2169 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2176 if (regmatch(prog->program + 1)) {
2177 prog->endp[0] = PL_reginput - PL_bostr;
2180 REGCP_UNWIND(lastcp);
2184 #define RE_UNWIND_BRANCH 1
2185 #define RE_UNWIND_BRANCHJ 2
2189 typedef struct { /* XX: makes sense to enlarge it... */
2193 } re_unwind_generic_t;
2206 } re_unwind_branch_t;
2208 typedef union re_unwind_t {
2210 re_unwind_generic_t generic;
2211 re_unwind_branch_t branch;
2214 #define sayYES goto yes
2215 #define sayNO goto no
2216 #define sayNO_ANYOF goto no_anyof
2217 #define sayYES_FINAL goto yes_final
2218 #define sayYES_LOUD goto yes_loud
2219 #define sayNO_FINAL goto no_final
2220 #define sayNO_SILENT goto do_no
2221 #define saySAME(x) if (x) goto yes; else goto no
2223 #define REPORT_CODE_OFF 24
2226 - regmatch - main matching routine
2228 * Conceptually the strategy is simple: check to see whether the current
2229 * node matches, call self recursively to see whether the rest matches,
2230 * and then act accordingly. In practice we make some effort to avoid
2231 * recursion, in particular by going through "ordinary" nodes (that don't
2232 * need to know whether the rest of the match failed) by a loop instead of
2235 /* [lwall] I've hoisted the register declarations to the outer block in order to
2236 * maybe save a little bit of pushing and popping on the stack. It also takes
2237 * advantage of machines that use a register save mask on subroutine entry.
2239 STATIC I32 /* 0 failure, 1 success */
2240 S_regmatch(pTHX_ regnode *prog)
2242 register regnode *scan; /* Current node. */
2243 regnode *next; /* Next node. */
2244 regnode *inner; /* Next node in internal branch. */
2245 register I32 nextchr; /* renamed nextchr - nextchar colides with
2246 function of same name */
2247 register I32 n; /* no or next */
2248 register I32 ln = 0; /* len or last */
2249 register char *s = Nullch; /* operand or save */
2250 register char *locinput = PL_reginput;
2251 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2252 int minmod = 0, sw = 0, logical = 0;
2255 I32 firstcp = PL_savestack_ix;
2257 register bool do_utf8 = PL_reg_match_utf8;
2259 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2260 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2261 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2268 /* Note that nextchr is a byte even in UTF */
2269 nextchr = UCHARAT(locinput);
2271 while (scan != NULL) {
2274 SV *prop = sv_newmortal();
2275 int docolor = *PL_colors[0];
2276 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2277 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2278 /* The part of the string before starttry has one color
2279 (pref0_len chars), between starttry and current
2280 position another one (pref_len - pref0_len chars),
2281 after the current position the third one.
2282 We assume that pref0_len <= pref_len, otherwise we
2283 decrease pref0_len. */
2284 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2285 ? (5 + taill) - l : locinput - PL_bostr;
2288 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2290 pref0_len = pref_len - (locinput - PL_reg_starttry);
2291 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2292 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2293 ? (5 + taill) - pref_len : PL_regeol - locinput);
2294 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2298 if (pref0_len > pref_len)
2299 pref0_len = pref_len;
2300 regprop(prop, scan);
2303 do_utf8 && OP(scan) != CANY ?
2304 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2305 pref0_len, 60, UNI_DISPLAY_REGEX) :
2306 locinput - pref_len;
2307 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2308 char *s1 = do_utf8 && OP(scan) != CANY ?
2309 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2310 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2311 locinput - pref_len + pref0_len;
2312 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2313 char *s2 = do_utf8 && OP(scan) != CANY ?
2314 pv_uni_display(dsv2, (U8*)locinput,
2315 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2317 int len2 = do_utf8 ? strlen(s2) : l;
2318 PerlIO_printf(Perl_debug_log,
2319 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2320 (IV)(locinput - PL_bostr),
2327 (docolor ? "" : "> <"),
2331 15 - l - pref_len + 1,
2333 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2338 next = scan + NEXT_OFF(scan);
2344 if (locinput == PL_bostr || (PL_multiline &&
2345 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2347 /* regtill = regbol; */
2352 if (locinput == PL_bostr ||
2353 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2359 if (locinput == PL_bostr)
2363 if (locinput == PL_reg_ganch)
2373 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2378 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2380 if (PL_regeol - locinput > 1)
2384 if (PL_regeol != locinput)
2388 if (!nextchr && locinput >= PL_regeol)
2391 locinput += PL_utf8skip[nextchr];
2392 if (locinput > PL_regeol)
2394 nextchr = UCHARAT(locinput);
2397 nextchr = UCHARAT(++locinput);
2400 if (!nextchr && locinput >= PL_regeol)
2402 nextchr = UCHARAT(++locinput);
2405 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2408 locinput += PL_utf8skip[nextchr];
2409 if (locinput > PL_regeol)
2411 nextchr = UCHARAT(locinput);
2414 nextchr = UCHARAT(++locinput);
2419 if (do_utf8 != UTF) {
2420 /* The target and the pattern have differing utf8ness. */
2426 /* The target is utf8, the pattern is not utf8. */
2430 if (NATIVE_TO_UNI(*(U8*)s) !=
2431 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2433 0 : UTF8_ALLOW_ANY))
2440 /* The target is not utf8, the pattern is utf8. */
2444 if (NATIVE_TO_UNI(*((U8*)l)) !=
2445 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2447 0 : UTF8_ALLOW_ANY))
2454 nextchr = UCHARAT(locinput);
2457 /* The target and the pattern have the same utf8ness. */
2458 /* Inline the first character, for speed. */
2459 if (UCHARAT(s) != nextchr)
2461 if (PL_regeol - locinput < ln)
2463 if (ln > 1 && memNE(s, locinput, ln))
2466 nextchr = UCHARAT(locinput);
2469 PL_reg_flags |= RF_tainted;
2475 if (do_utf8 || UTF) {
2476 /* Either target or the pattern are utf8. */
2478 char *e = PL_regeol;
2480 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2481 l, &e, 0, do_utf8)) {
2482 /* One more case for the sharp s:
2483 * pack("U0U*", 0xDF) =~ /ss/i,
2484 * the 0xC3 0x9F are the UTF-8
2485 * byte sequence for the U+00DF. */
2487 toLOWER(s[0]) == 's' &&
2489 toLOWER(s[1]) == 's' &&
2496 nextchr = UCHARAT(locinput);
2500 /* Neither the target and the pattern are utf8. */
2502 /* Inline the first character, for speed. */
2503 if (UCHARAT(s) != nextchr &&
2504 UCHARAT(s) != ((OP(scan) == EXACTF)
2505 ? PL_fold : PL_fold_locale)[nextchr])
2507 if (PL_regeol - locinput < ln)
2509 if (ln > 1 && (OP(scan) == EXACTF
2510 ? ibcmp(s, locinput, ln)
2511 : ibcmp_locale(s, locinput, ln)))
2514 nextchr = UCHARAT(locinput);
2518 STRLEN inclasslen = PL_regeol - locinput;
2520 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2522 if (locinput >= PL_regeol)
2524 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2525 nextchr = UCHARAT(locinput);
2530 nextchr = UCHARAT(locinput);
2531 if (!REGINCLASS(scan, (U8*)locinput))
2533 if (!nextchr && locinput >= PL_regeol)
2535 nextchr = UCHARAT(++locinput);
2539 /* If we might have the case of the German sharp s
2540 * in a casefolding Unicode character class. */
2542 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2543 locinput += SHARP_S_SKIP;
2544 nextchr = UCHARAT(locinput);
2550 PL_reg_flags |= RF_tainted;
2556 LOAD_UTF8_CHARCLASS(alnum,"a");
2557 if (!(OP(scan) == ALNUM
2558 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2559 : isALNUM_LC_utf8((U8*)locinput)))
2563 locinput += PL_utf8skip[nextchr];
2564 nextchr = UCHARAT(locinput);
2567 if (!(OP(scan) == ALNUM
2568 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2570 nextchr = UCHARAT(++locinput);
2573 PL_reg_flags |= RF_tainted;
2576 if (!nextchr && locinput >= PL_regeol)
2579 LOAD_UTF8_CHARCLASS(alnum,"a");
2580 if (OP(scan) == NALNUM
2581 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2582 : isALNUM_LC_utf8((U8*)locinput))
2586 locinput += PL_utf8skip[nextchr];
2587 nextchr = UCHARAT(locinput);
2590 if (OP(scan) == NALNUM
2591 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2593 nextchr = UCHARAT(++locinput);
2597 PL_reg_flags |= RF_tainted;
2601 /* was last char in word? */
2603 if (locinput == PL_bostr)
2606 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2608 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2610 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2611 ln = isALNUM_uni(ln);
2612 LOAD_UTF8_CHARCLASS(alnum,"a");
2613 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2616 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2617 n = isALNUM_LC_utf8((U8*)locinput);
2621 ln = (locinput != PL_bostr) ?
2622 UCHARAT(locinput - 1) : '\n';
2623 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2625 n = isALNUM(nextchr);
2628 ln = isALNUM_LC(ln);
2629 n = isALNUM_LC(nextchr);
2632 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2633 OP(scan) == BOUNDL))
2637 PL_reg_flags |= RF_tainted;
2643 if (UTF8_IS_CONTINUED(nextchr)) {
2644 LOAD_UTF8_CHARCLASS(space," ");
2645 if (!(OP(scan) == SPACE
2646 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2647 : isSPACE_LC_utf8((U8*)locinput)))
2651 locinput += PL_utf8skip[nextchr];
2652 nextchr = UCHARAT(locinput);
2655 if (!(OP(scan) == SPACE
2656 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2658 nextchr = UCHARAT(++locinput);
2661 if (!(OP(scan) == SPACE
2662 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2664 nextchr = UCHARAT(++locinput);
2668 PL_reg_flags |= RF_tainted;
2671 if (!nextchr && locinput >= PL_regeol)
2674 LOAD_UTF8_CHARCLASS(space," ");
2675 if (OP(scan) == NSPACE
2676 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2677 : isSPACE_LC_utf8((U8*)locinput))
2681 locinput += PL_utf8skip[nextchr];
2682 nextchr = UCHARAT(locinput);
2685 if (OP(scan) == NSPACE
2686 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2688 nextchr = UCHARAT(++locinput);
2691 PL_reg_flags |= RF_tainted;
2697 LOAD_UTF8_CHARCLASS(digit,"0");
2698 if (!(OP(scan) == DIGIT
2699 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2700 : isDIGIT_LC_utf8((U8*)locinput)))
2704 locinput += PL_utf8skip[nextchr];
2705 nextchr = UCHARAT(locinput);
2708 if (!(OP(scan) == DIGIT
2709 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2711 nextchr = UCHARAT(++locinput);
2714 PL_reg_flags |= RF_tainted;
2717 if (!nextchr && locinput >= PL_regeol)
2720 LOAD_UTF8_CHARCLASS(digit,"0");
2721 if (OP(scan) == NDIGIT
2722 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2723 : isDIGIT_LC_utf8((U8*)locinput))
2727 locinput += PL_utf8skip[nextchr];
2728 nextchr = UCHARAT(locinput);
2731 if (OP(scan) == NDIGIT
2732 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2734 nextchr = UCHARAT(++locinput);
2737 if (locinput >= PL_regeol)
2740 LOAD_UTF8_CHARCLASS(mark,"~");
2741 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2743 locinput += PL_utf8skip[nextchr];
2744 while (locinput < PL_regeol &&
2745 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2746 locinput += UTF8SKIP(locinput);
2747 if (locinput > PL_regeol)
2752 nextchr = UCHARAT(locinput);
2755 PL_reg_flags |= RF_tainted;
2759 n = ARG(scan); /* which paren pair */
2760 ln = PL_regstartp[n];
2761 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2762 if ((I32)*PL_reglastparen < n || ln == -1)
2763 sayNO; /* Do not match unless seen CLOSEn. */
2764 if (ln == PL_regendp[n])
2768 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2770 char *e = PL_bostr + PL_regendp[n];
2772 * Note that we can't do the "other character" lookup trick as
2773 * in the 8-bit case (no pun intended) because in Unicode we
2774 * have to map both upper and title case to lower case.
2776 if (OP(scan) == REFF) {
2777 STRLEN ulen1, ulen2;
2778 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2779 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2783 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2784 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2785 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2792 nextchr = UCHARAT(locinput);
2796 /* Inline the first character, for speed. */
2797 if (UCHARAT(s) != nextchr &&
2799 (UCHARAT(s) != ((OP(scan) == REFF
2800 ? PL_fold : PL_fold_locale)[nextchr]))))
2802 ln = PL_regendp[n] - ln;
2803 if (locinput + ln > PL_regeol)
2805 if (ln > 1 && (OP(scan) == REF
2806 ? memNE(s, locinput, ln)
2808 ? ibcmp(s, locinput, ln)
2809 : ibcmp_locale(s, locinput, ln))))
2812 nextchr = UCHARAT(locinput);
2823 OP_4tree *oop = PL_op;
2824 COP *ocurcop = PL_curcop;
2829 PL_op = (OP_4tree*)PL_regdata->data[n];
2830 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2831 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2832 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2836 CALLRUNOPS(aTHX); /* Scalar context. */
2839 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2847 PAD_RESTORE_LOCAL(old_comppad);
2848 PL_curcop = ocurcop;
2850 if (logical == 2) { /* Postponed subexpression. */
2852 MAGIC *mg = Null(MAGIC*);
2854 CHECKPOINT cp, lastcp;
2857 if(SvROK(ret) || SvRMAGICAL(ret)) {
2858 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2861 mg = mg_find(sv, PERL_MAGIC_qr);
2864 re = (regexp *)mg->mg_obj;
2865 (void)ReREFCNT_inc(re);
2869 char *t = SvPV(ret, len);
2871 char *oprecomp = PL_regprecomp;
2872 I32 osize = PL_regsize;
2873 I32 onpar = PL_regnpar;
2876 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2877 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2879 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2880 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2882 PL_regprecomp = oprecomp;
2887 PerlIO_printf(Perl_debug_log,
2888 "Entering embedded `%s%.60s%s%s'\n",
2892 (strlen(re->precomp) > 60 ? "..." : ""))
2895 state.prev = PL_reg_call_cc;
2896 state.cc = PL_regcc;
2897 state.re = PL_reg_re;
2901 cp = regcppush(0); /* Save *all* the positions. */
2904 state.ss = PL_savestack_ix;
2905 *PL_reglastparen = 0;
2906 *PL_reglastcloseparen = 0;
2907 PL_reg_call_cc = &state;
2908 PL_reginput = locinput;
2909 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2910 ((re->reganch & ROPT_UTF8) != 0);
2911 if (toggleutf) PL_reg_flags ^= RF_utf8;
2913 /* XXXX This is too dramatic a measure... */
2916 if (regmatch(re->program + 1)) {
2917 /* Even though we succeeded, we need to restore
2918 global variables, since we may be wrapped inside
2919 SUSPEND, thus the match may be not finished yet. */
2921 /* XXXX Do this only if SUSPENDed? */
2922 PL_reg_call_cc = state.prev;
2923 PL_regcc = state.cc;
2924 PL_reg_re = state.re;
2925 cache_re(PL_reg_re);
2926 if (toggleutf) PL_reg_flags ^= RF_utf8;
2928 /* XXXX This is too dramatic a measure... */
2931 /* These are needed even if not SUSPEND. */
2937 REGCP_UNWIND(lastcp);
2939 PL_reg_call_cc = state.prev;
2940 PL_regcc = state.cc;
2941 PL_reg_re = state.re;
2942 cache_re(PL_reg_re);
2943 if (toggleutf) PL_reg_flags ^= RF_utf8;
2945 /* XXXX This is too dramatic a measure... */
2955 sv_setsv(save_scalar(PL_replgv), ret);
2959 n = ARG(scan); /* which paren pair */
2960 PL_reg_start_tmp[n] = locinput;
2965 n = ARG(scan); /* which paren pair */
2966 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2967 PL_regendp[n] = locinput - PL_bostr;
2968 if (n > (I32)*PL_reglastparen)
2969 *PL_reglastparen = n;
2970 *PL_reglastcloseparen = n;
2973 n = ARG(scan); /* which paren pair */
2974 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2977 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2979 next = NEXTOPER(NEXTOPER(scan));
2981 next = scan + ARG(scan);
2982 if (OP(next) == IFTHEN) /* Fake one. */
2983 next = NEXTOPER(NEXTOPER(next));
2987 logical = scan->flags;
2989 /*******************************************************************
2990 PL_regcc contains infoblock about the innermost (...)* loop, and
2991 a pointer to the next outer infoblock.
2993 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2995 1) After matching X, regnode for CURLYX is processed;
2997 2) This regnode creates infoblock on the stack, and calls
2998 regmatch() recursively with the starting point at WHILEM node;
3000 3) Each hit of WHILEM node tries to match A and Z (in the order
3001 depending on the current iteration, min/max of {min,max} and
3002 greediness). The information about where are nodes for "A"
3003 and "Z" is read from the infoblock, as is info on how many times "A"
3004 was already matched, and greediness.
3006 4) After A matches, the same WHILEM node is hit again.
3008 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3009 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3010 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3011 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3012 of the external loop.
3014 Currently present infoblocks form a tree with a stem formed by PL_curcc
3015 and whatever it mentions via ->next, and additional attached trees
3016 corresponding to temporarily unset infoblocks as in "5" above.
3018 In the following picture infoblocks for outer loop of
3019 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3020 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3021 infoblocks are drawn below the "reset" infoblock.
3023 In fact in the picture below we do not show failed matches for Z and T
3024 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3025 more obvious *why* one needs to *temporary* unset infoblocks.]
3027 Matched REx position InfoBlocks Comment
3031 Y A)*?Z)*?T x <- O <- I
3032 YA )*?Z)*?T x <- O <- I
3033 YA A)*?Z)*?T x <- O <- I
3034 YAA )*?Z)*?T x <- O <- I
3035 YAA Z)*?T x <- O # Temporary unset I
3038 YAAZ Y(A)*?Z)*?T x <- O
3041 YAAZY (A)*?Z)*?T x <- O
3044 YAAZY A)*?Z)*?T x <- O <- I
3047 YAAZYA )*?Z)*?T x <- O <- I
3050 YAAZYA Z)*?T x <- O # Temporary unset I
3056 YAAZYAZ T x # Temporary unset O
3063 *******************************************************************/
3066 CHECKPOINT cp = PL_savestack_ix;
3067 /* No need to save/restore up to this paren */
3068 I32 parenfloor = scan->flags;
3070 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3072 cc.oldcc = PL_regcc;
3074 /* XXXX Probably it is better to teach regpush to support
3075 parenfloor > PL_regsize... */
3076 if (parenfloor > (I32)*PL_reglastparen)
3077 parenfloor = *PL_reglastparen; /* Pessimization... */
3078 cc.parenfloor = parenfloor;
3080 cc.min = ARG1(scan);
3081 cc.max = ARG2(scan);
3082 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3086 PL_reginput = locinput;
3087 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3089 PL_regcc = cc.oldcc;
3095 * This is really hard to understand, because after we match
3096 * what we're trying to match, we must make sure the rest of
3097 * the REx is going to match for sure, and to do that we have
3098 * to go back UP the parse tree by recursing ever deeper. And
3099 * if it fails, we have to reset our parent's current state
3100 * that we can try again after backing off.
3103 CHECKPOINT cp, lastcp;
3104 CURCUR* cc = PL_regcc;
3105 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3107 n = cc->cur + 1; /* how many we know we matched */
3108 PL_reginput = locinput;
3111 PerlIO_printf(Perl_debug_log,
3112 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3113 REPORT_CODE_OFF+PL_regindent*2, "",
3114 (long)n, (long)cc->min,
3115 (long)cc->max, PTR2UV(cc))
3118 /* If degenerate scan matches "", assume scan done. */
3120 if (locinput == cc->lastloc && n >= cc->min) {
3121 PL_regcc = cc->oldcc;
3125 PerlIO_printf(Perl_debug_log,
3126 "%*s empty match detected, try continuation...\n",
3127 REPORT_CODE_OFF+PL_regindent*2, "")
3129 if (regmatch(cc->next))
3137 /* First just match a string of min scans. */
3141 cc->lastloc = locinput;
3142 if (regmatch(cc->scan))
3145 cc->lastloc = lastloc;
3150 /* Check whether we already were at this position.
3151 Postpone detection until we know the match is not
3152 *that* much linear. */
3153 if (!PL_reg_maxiter) {
3154 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3155 PL_reg_leftiter = PL_reg_maxiter;
3157 if (PL_reg_leftiter-- == 0) {
3158 I32 size = (PL_reg_maxiter + 7)/8;
3159 if (PL_reg_poscache) {
3160 if ((I32)PL_reg_poscache_size < size) {
3161 Renew(PL_reg_poscache, size, char);
3162 PL_reg_poscache_size = size;
3164 Zero(PL_reg_poscache, size, char);
3167 PL_reg_poscache_size = size;
3168 Newz(29, PL_reg_poscache, size, char);
3171 PerlIO_printf(Perl_debug_log,
3172 "%sDetected a super-linear match, switching on caching%s...\n",
3173 PL_colors[4], PL_colors[5])
3176 if (PL_reg_leftiter < 0) {
3177 I32 o = locinput - PL_bostr, b;
3179 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3182 if (PL_reg_poscache[o] & (1<<b)) {
3184 PerlIO_printf(Perl_debug_log,
3185 "%*s already tried at this position...\n",
3186 REPORT_CODE_OFF+PL_regindent*2, "")
3190 PL_reg_poscache[o] |= (1<<b);
3194 /* Prefer next over scan for minimal matching. */
3197 PL_regcc = cc->oldcc;
3200 cp = regcppush(cc->parenfloor);
3202 if (regmatch(cc->next)) {
3204 sayYES; /* All done. */
3206 REGCP_UNWIND(lastcp);
3212 if (n >= cc->max) { /* Maximum greed exceeded? */
3213 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3214 && !(PL_reg_flags & RF_warned)) {
3215 PL_reg_flags |= RF_warned;
3216 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3217 "Complex regular subexpression recursion",
3224 PerlIO_printf(Perl_debug_log,
3225 "%*s trying longer...\n",
3226 REPORT_CODE_OFF+PL_regindent*2, "")
3228 /* Try scanning more and see if it helps. */
3229 PL_reginput = locinput;
3231 cc->lastloc = locinput;
3232 cp = regcppush(cc->parenfloor);
3234 if (regmatch(cc->scan)) {
3238 REGCP_UNWIND(lastcp);
3241 cc->lastloc = lastloc;
3245 /* Prefer scan over next for maximal matching. */
3247 if (n < cc->max) { /* More greed allowed? */
3248 cp = regcppush(cc->parenfloor);
3250 cc->lastloc = locinput;
3252 if (regmatch(cc->scan)) {
3256 REGCP_UNWIND(lastcp);
3257 regcppop(); /* Restore some previous $<digit>s? */
3258 PL_reginput = locinput;
3260 PerlIO_printf(Perl_debug_log,
3261 "%*s failed, try continuation...\n",
3262 REPORT_CODE_OFF+PL_regindent*2, "")
3265 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3266 && !(PL_reg_flags & RF_warned)) {
3267 PL_reg_flags |= RF_warned;
3268 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3269 "Complex regular subexpression recursion",
3273 /* Failed deeper matches of scan, so see if this one works. */
3274 PL_regcc = cc->oldcc;
3277 if (regmatch(cc->next))
3283 cc->lastloc = lastloc;
3288 next = scan + ARG(scan);
3291 inner = NEXTOPER(NEXTOPER(scan));
3294 inner = NEXTOPER(scan);
3298 if (OP(next) != c1) /* No choice. */
3299 next = inner; /* Avoid recursion. */
3301 I32 lastparen = *PL_reglastparen;
3303 re_unwind_branch_t *uw;
3305 /* Put unwinding data on stack */
3306 unwind1 = SSNEWt(1,re_unwind_branch_t);
3307 uw = SSPTRt(unwind1,re_unwind_branch_t);
3310 uw->type = ((c1 == BRANCH)
3312 : RE_UNWIND_BRANCHJ);
3313 uw->lastparen = lastparen;
3315 uw->locinput = locinput;
3316 uw->nextchr = nextchr;
3318 uw->regindent = ++PL_regindent;
3321 REGCP_SET(uw->lastcp);
3323 /* Now go into the first branch */
3336 /* We suppose that the next guy does not need
3337 backtracking: in particular, it is of constant length,
3338 and has no parenths to influence future backrefs. */
3339 ln = ARG1(scan); /* min to match */
3340 n = ARG2(scan); /* max to match */
3341 paren = scan->flags;
3343 if (paren > PL_regsize)
3345 if (paren > (I32)*PL_reglastparen)
3346 *PL_reglastparen = paren;
3348 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3350 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3351 PL_reginput = locinput;
3354 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3356 /* if we matched something zero-length we don't need to
3357 backtrack - capturing parens are already defined, so
3358 the caveat in the maximal case doesn't apply
3360 XXXX if ln == 0, we can redo this check first time
3361 through the following loop
3364 n = ln; /* don't backtrack */
3365 locinput = PL_reginput;
3366 if (HAS_TEXT(next) || JUMPABLE(next)) {
3367 regnode *text_node = next;
3369 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3371 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3373 if (PL_regkind[(U8)OP(text_node)] == REF) {
3375 n = ARG(text_node); /* which paren pair */
3376 ln = PL_regstartp[n];
3377 /* assume yes if we haven't seen CLOSEn */
3379 (I32)*PL_reglastparen < n ||
3386 c1 = *(PL_bostr + ln);
3388 else { c1 = (U8)*STRING(text_node); }
3389 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3391 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3392 c2 = PL_fold_locale[c1];
3401 /* This may be improved if l == 0. */
3402 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3403 /* If it could work, try it. */
3405 UCHARAT(PL_reginput) == c1 ||
3406 UCHARAT(PL_reginput) == c2)
3410 PL_regstartp[paren] =
3411 HOPc(PL_reginput, -l) - PL_bostr;
3412 PL_regendp[paren] = PL_reginput - PL_bostr;
3415 PL_regendp[paren] = -1;
3419 REGCP_UNWIND(lastcp);
3421 /* Couldn't or didn't -- move forward. */
3422 PL_reginput = locinput;
3423 if (regrepeat_hard(scan, 1, &l)) {
3425 locinput = PL_reginput;
3432 n = regrepeat_hard(scan, n, &l);
3433 /* if we matched something zero-length we don't need to
3434 backtrack, unless the minimum count is zero and we
3435 are capturing the result - in that case the capture
3436 being defined or not may affect later execution
3438 if (n != 0 && l == 0 && !(paren && ln == 0))
3439 ln = n; /* don't backtrack */
3440 locinput = PL_reginput;
3442 PerlIO_printf(Perl_debug_log,
3443 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3444 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3448 if (HAS_TEXT(next) || JUMPABLE(next)) {
3449 regnode *text_node = next;
3451 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3453 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3455 if (PL_regkind[(U8)OP(text_node)] == REF) {
3457 n = ARG(text_node); /* which paren pair */
3458 ln = PL_regstartp[n];
3459 /* assume yes if we haven't seen CLOSEn */
3461 (I32)*PL_reglastparen < n ||
3468 c1 = *(PL_bostr + ln);
3470 else { c1 = (U8)*STRING(text_node); }
3472 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3474 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3475 c2 = PL_fold_locale[c1];
3486 /* If it could work, try it. */
3488 UCHARAT(PL_reginput) == c1 ||
3489 UCHARAT(PL_reginput) == c2)
3492 PerlIO_printf(Perl_debug_log,
3493 "%*s trying tail with n=%"IVdf"...\n",
3494 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3498 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3499 PL_regendp[paren] = PL_reginput - PL_bostr;
3502 PL_regendp[paren] = -1;
3506 REGCP_UNWIND(lastcp);
3508 /* Couldn't or didn't -- back up. */
3510 locinput = HOPc(locinput, -l);
3511 PL_reginput = locinput;
3518 paren = scan->flags; /* Which paren to set */
3519 if (paren > PL_regsize)
3521 if (paren > (I32)*PL_reglastparen)
3522 *PL_reglastparen = paren;
3523 ln = ARG1(scan); /* min to match */
3524 n = ARG2(scan); /* max to match */
3525 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3529 ln = ARG1(scan); /* min to match */
3530 n = ARG2(scan); /* max to match */
3531 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3536 scan = NEXTOPER(scan);
3542 scan = NEXTOPER(scan);
3546 * Lookahead to avoid useless match attempts
3547 * when we know what character comes next.
3551 * Used to only do .*x and .*?x, but now it allows
3552 * for )'s, ('s and (?{ ... })'s to be in the way
3553 * of the quantifier and the EXACT-like node. -- japhy
3556 if (HAS_TEXT(next) || JUMPABLE(next)) {
3558 regnode *text_node = next;
3560 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3562 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3564 if (PL_regkind[(U8)OP(text_node)] == REF) {
3566 n = ARG(text_node); /* which paren pair */
3567 ln = PL_regstartp[n];
3568 /* assume yes if we haven't seen CLOSEn */
3570 (I32)*PL_reglastparen < n ||
3575 goto assume_ok_easy;
3577 s = (U8*)PL_bostr + ln;
3579 else { s = (U8*)STRING(text_node); }
3583 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3585 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3586 c2 = PL_fold_locale[c1];
3589 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3590 STRLEN ulen1, ulen2;
3591 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3592 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3594 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3595 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3597 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3599 0 : UTF8_ALLOW_ANY);
3600 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3602 0 : UTF8_ALLOW_ANY);
3605 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3607 0 : UTF8_ALLOW_ANY);
3615 PL_reginput = locinput;
3619 if (ln && regrepeat(scan, ln) < ln)
3621 locinput = PL_reginput;
3624 char *e; /* Should not check after this */
3625 char *old = locinput;
3628 if (n == REG_INFTY) {
3631 while (UTF8_IS_CONTINUATION(*(U8*)e))
3637 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3641 e = locinput + n - ln;
3646 /* Find place 'next' could work */
3649 while (locinput <= e &&
3650 UCHARAT(locinput) != c1)
3653 while (locinput <= e
3654 && UCHARAT(locinput) != c1
3655 && UCHARAT(locinput) != c2)
3658 count = locinput - old;
3663 /* count initialised to
3664 * utf8_distance(old, locinput) */
3665 while (locinput <= e &&
3666 utf8n_to_uvchr((U8*)locinput,
3669 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3674 /* count initialised to
3675 * utf8_distance(old, locinput) */
3676 while (locinput <= e) {
3677 UV c = utf8n_to_uvchr((U8*)locinput,
3680 0 : UTF8_ALLOW_ANY);
3681 if (c == (UV)c1 || c == (UV)c2)
3690 /* PL_reginput == old now */
3691 if (locinput != old) {
3692 ln = 1; /* Did some */
3693 if (regrepeat(scan, count) < count)
3696 /* PL_reginput == locinput now */
3697 TRYPAREN(paren, ln, locinput);
3698 PL_reginput = locinput; /* Could be reset... */
3699 REGCP_UNWIND(lastcp);
3700 /* Couldn't or didn't -- move forward. */
3703 locinput += UTF8SKIP(locinput);
3710 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3714 c = utf8n_to_uvchr((U8*)PL_reginput,
3717 0 : UTF8_ALLOW_ANY);
3719 c = UCHARAT(PL_reginput);
3720 /* If it could work, try it. */
3721 if (c == (UV)c1 || c == (UV)c2)
3723 TRYPAREN(paren, ln, PL_reginput);
3724 REGCP_UNWIND(lastcp);
3727 /* If it could work, try it. */
3728 else if (c1 == -1000)
3730 TRYPAREN(paren, ln, PL_reginput);
3731 REGCP_UNWIND(lastcp);
3733 /* Couldn't or didn't -- move forward. */
3734 PL_reginput = locinput;
3735 if (regrepeat(scan, 1)) {
3737 locinput = PL_reginput;
3745 n = regrepeat(scan, n);
3746 locinput = PL_reginput;
3747 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3748 ((!PL_multiline && OP(next) != MEOL) ||
3749 OP(next) == SEOL || OP(next) == EOS))
3751 ln = n; /* why back off? */
3752 /* ...because $ and \Z can match before *and* after
3753 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3754 We should back off by one in this case. */
3755 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3764 c = utf8n_to_uvchr((U8*)PL_reginput,
3767 0 : UTF8_ALLOW_ANY);
3769 c = UCHARAT(PL_reginput);
3771 /* If it could work, try it. */
3772 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3774 TRYPAREN(paren, n, PL_reginput);
3775 REGCP_UNWIND(lastcp);
3777 /* Couldn't or didn't -- back up. */
3779 PL_reginput = locinput = HOPc(locinput, -1);
3787 c = utf8n_to_uvchr((U8*)PL_reginput,
3790 0 : UTF8_ALLOW_ANY);
3792 c = UCHARAT(PL_reginput);
3794 /* If it could work, try it. */
3795 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3797 TRYPAREN(paren, n, PL_reginput);
3798 REGCP_UNWIND(lastcp);
3800 /* Couldn't or didn't -- back up. */
3802 PL_reginput = locinput = HOPc(locinput, -1);
3809 if (PL_reg_call_cc) {
3810 re_cc_state *cur_call_cc = PL_reg_call_cc;
3811 CURCUR *cctmp = PL_regcc;
3812 regexp *re = PL_reg_re;
3813 CHECKPOINT cp, lastcp;
3815 cp = regcppush(0); /* Save *all* the positions. */
3817 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3819 PL_reginput = locinput; /* Make position available to
3821 cache_re(PL_reg_call_cc->re);
3822 PL_regcc = PL_reg_call_cc->cc;
3823 PL_reg_call_cc = PL_reg_call_cc->prev;
3824 if (regmatch(cur_call_cc->node)) {
3825 PL_reg_call_cc = cur_call_cc;
3829 REGCP_UNWIND(lastcp);
3831 PL_reg_call_cc = cur_call_cc;
3837 PerlIO_printf(Perl_debug_log,
3838 "%*s continuation failed...\n",
3839 REPORT_CODE_OFF+PL_regindent*2, "")
3843 if (locinput < PL_regtill) {
3844 DEBUG_r(PerlIO_printf(Perl_debug_log,
3845 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3847 (long)(locinput - PL_reg_starttry),
3848 (long)(PL_regtill - PL_reg_starttry),
3850 sayNO_FINAL; /* Cannot match: too short. */
3852 PL_reginput = locinput; /* put where regtry can find it */
3853 sayYES_FINAL; /* Success! */
3855 PL_reginput = locinput; /* put where regtry can find it */
3856 sayYES_LOUD; /* Success! */
3859 PL_reginput = locinput;
3864 s = HOPBACKc(locinput, scan->flags);
3870 PL_reginput = locinput;
3875 s = HOPBACKc(locinput, scan->flags);
3881 PL_reginput = locinput;
3884 inner = NEXTOPER(NEXTOPER(scan));
3885 if (regmatch(inner) != n) {
3900 if (OP(scan) == SUSPEND) {
3901 locinput = PL_reginput;
3902 nextchr = UCHARAT(locinput);
3907 next = scan + ARG(scan);
3912 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3913 PTR2UV(scan), OP(scan));
3914 Perl_croak(aTHX_ "regexp memory corruption");
3921 * We get here only if there's trouble -- normally "case END" is
3922 * the terminating point.
3924 Perl_croak(aTHX_ "corrupted regexp pointers");
3930 PerlIO_printf(Perl_debug_log,
3931 "%*s %scould match...%s\n",
3932 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3936 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3937 PL_colors[4],PL_colors[5]));
3943 #if 0 /* Breaks $^R */
3951 PerlIO_printf(Perl_debug_log,
3952 "%*s %sfailed...%s\n",
3953 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3959 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3962 case RE_UNWIND_BRANCH:
3963 case RE_UNWIND_BRANCHJ:
3965 re_unwind_branch_t *uwb = &(uw->branch);
3966 I32 lastparen = uwb->lastparen;
3968 REGCP_UNWIND(uwb->lastcp);
3969 for (n = *PL_reglastparen; n > lastparen; n--)
3971 *PL_reglastparen = n;
3972 scan = next = uwb->next;
3974 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3975 ? BRANCH : BRANCHJ) ) { /* Failure */
3982 /* Have more choice yet. Reuse the same uwb. */
3984 if ((n = (uwb->type == RE_UNWIND_BRANCH
3985 ? NEXT_OFF(next) : ARG(next))))
3988 next = NULL; /* XXXX Needn't unwinding in this case... */
3990 next = NEXTOPER(scan);
3991 if (uwb->type == RE_UNWIND_BRANCHJ)
3992 next = NEXTOPER(next);
3993 locinput = uwb->locinput;
3994 nextchr = uwb->nextchr;
3996 PL_regindent = uwb->regindent;
4003 Perl_croak(aTHX_ "regexp unwind memory corruption");
4014 - regrepeat - repeatedly match something simple, report how many
4017 * [This routine now assumes that it will only match on things of length 1.
4018 * That was true before, but now we assume scan - reginput is the count,
4019 * rather than incrementing count on every character. [Er, except utf8.]]
4022 S_regrepeat(pTHX_ regnode *p, I32 max)
4024 register char *scan;
4026 register char *loceol = PL_regeol;
4027 register I32 hardcount = 0;
4028 register bool do_utf8 = PL_reg_match_utf8;
4031 if (max == REG_INFTY)
4033 else if (max < loceol - scan)
4034 loceol = scan + max;
4039 while (scan < loceol && hardcount < max && *scan != '\n') {
4040 scan += UTF8SKIP(scan);
4044 while (scan < loceol && *scan != '\n')
4051 while (scan < loceol && hardcount < max) {
4052 scan += UTF8SKIP(scan);
4062 case EXACT: /* length of string is 1 */
4064 while (scan < loceol && UCHARAT(scan) == c)
4067 case EXACTF: /* length of string is 1 */
4069 while (scan < loceol &&
4070 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4073 case EXACTFL: /* length of string is 1 */
4074 PL_reg_flags |= RF_tainted;
4076 while (scan < loceol &&
4077 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4083 while (hardcount < max && scan < loceol &&
4084 reginclass(p, (U8*)scan, 0, do_utf8)) {
4085 scan += UTF8SKIP(scan);
4089 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4096 LOAD_UTF8_CHARCLASS(alnum,"a");
4097 while (hardcount < max && scan < loceol &&
4098 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4099 scan += UTF8SKIP(scan);
4103 while (scan < loceol && isALNUM(*scan))
4108 PL_reg_flags |= RF_tainted;
4111 while (hardcount < max && scan < loceol &&
4112 isALNUM_LC_utf8((U8*)scan)) {
4113 scan += UTF8SKIP(scan);
4117 while (scan < loceol && isALNUM_LC(*scan))
4124 LOAD_UTF8_CHARCLASS(alnum,"a");
4125 while (hardcount < max && scan < loceol &&
4126 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4127 scan += UTF8SKIP(scan);
4131 while (scan < loceol && !isALNUM(*scan))
4136 PL_reg_flags |= RF_tainted;
4139 while (hardcount < max && scan < loceol &&
4140 !isALNUM_LC_utf8((U8*)scan)) {
4141 scan += UTF8SKIP(scan);
4145 while (scan < loceol && !isALNUM_LC(*scan))
4152 LOAD_UTF8_CHARCLASS(space," ");
4153 while (hardcount < max && scan < loceol &&
4155 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4156 scan += UTF8SKIP(scan);
4160 while (scan < loceol && isSPACE(*scan))
4165 PL_reg_flags |= RF_tainted;
4168 while (hardcount < max && scan < loceol &&
4169 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4170 scan += UTF8SKIP(scan);
4174 while (scan < loceol && isSPACE_LC(*scan))
4181 LOAD_UTF8_CHARCLASS(space," ");
4182 while (hardcount < max && scan < loceol &&
4184 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4185 scan += UTF8SKIP(scan);
4189 while (scan < loceol && !isSPACE(*scan))
4194 PL_reg_flags |= RF_tainted;
4197 while (hardcount < max && scan < loceol &&
4198 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4199 scan += UTF8SKIP(scan);
4203 while (scan < loceol && !isSPACE_LC(*scan))
4210 LOAD_UTF8_CHARCLASS(digit,"0");
4211 while (hardcount < max && scan < loceol &&
4212 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4213 scan += UTF8SKIP(scan);
4217 while (scan < loceol && isDIGIT(*scan))
4224 LOAD_UTF8_CHARCLASS(digit,"0");
4225 while (hardcount < max && scan < loceol &&
4226 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4227 scan += UTF8SKIP(scan);
4231 while (scan < loceol && !isDIGIT(*scan))
4235 default: /* Called on something of 0 width. */
4236 break; /* So match right here or not at all. */
4242 c = scan - PL_reginput;
4247 SV *prop = sv_newmortal();
4250 PerlIO_printf(Perl_debug_log,
4251 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4252 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4259 - regrepeat_hard - repeatedly match something, report total lenth and length
4261 * The repeater is supposed to have constant length.
4265 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4267 register char *scan = Nullch;
4268 register char *start;
4269 register char *loceol = PL_regeol;
4271 I32 count = 0, res = 1;
4276 start = PL_reginput;
4277 if (PL_reg_match_utf8) {
4278 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4281 while (start < PL_reginput) {
4283 start += UTF8SKIP(start);
4294 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4296 *lp = l = PL_reginput - start;
4297 if (max != REG_INFTY && l*max < loceol - scan)
4298 loceol = scan + l*max;
4311 - regclass_swash - prepare the utf8 swash
4315 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4321 if (PL_regdata && PL_regdata->count) {
4324 if (PL_regdata->what[n] == 's') {
4325 SV *rv = (SV*)PL_regdata->data[n];
4326 AV *av = (AV*)SvRV((SV*)rv);
4327 SV **ary = AvARRAY(av);
4330 /* See the end of regcomp.c:S_reglass() for
4331 * documentation of these array elements. */
4334 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4335 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4339 else if (si && doinit) {
4340 sw = swash_init("utf8", "", si, 1, 0);
4341 (void)av_store(av, 1, sw);
4357 - reginclass - determine if a character falls into a character class
4359 The n is the ANYOF regnode, the p is the target string, lenp
4360 is pointer to the maximum length of how far to go in the p
4361 (if the lenp is zero, UTF8SKIP(p) is used),
4362 do_utf8 tells whether the target string is in UTF-8.
4367 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4369 char flags = ANYOF_FLAGS(n);
4375 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4376 c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4377 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4379 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4380 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4383 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4384 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4387 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4391 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4394 if (swash_fetch(sw, p, do_utf8))
4396 else if (flags & ANYOF_FOLD) {
4397 if (!match && lenp && av) {
4400 for (i = 0; i <= av_len(av); i++) {
4401 SV* sv = *av_fetch(av, i, FALSE);
4403 char *s = SvPV(sv, len);
4405 if (len <= plen && memEQ(s, (char*)p, len)) {
4413 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4416 to_utf8_fold(p, tmpbuf, &tmplen);
4417 if (swash_fetch(sw, tmpbuf, do_utf8))
4423 if (match && lenp && *lenp == 0)
4424 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4426 if (!match && c < 256) {
4427 if (ANYOF_BITMAP_TEST(n, c))
4429 else if (flags & ANYOF_FOLD) {
4432 if (flags & ANYOF_LOCALE) {
4433 PL_reg_flags |= RF_tainted;
4434 f = PL_fold_locale[c];
4438 if (f != c && ANYOF_BITMAP_TEST(n, f))
4442 if (!match && (flags & ANYOF_CLASS)) {
4443 PL_reg_flags |= RF_tainted;
4445 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4446 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4447 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4448 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4449 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4450 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4451 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4452 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4453 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4454 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4455 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4456 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4457 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4458 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4459 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4460 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4461 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4462 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4463 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4464 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4465 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4466 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4467 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4468 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4469 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4470 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4471 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4472 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4473 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4474 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4475 ) /* How's that for a conditional? */
4482 return (flags & ANYOF_INVERT) ? !match : match;
4486 S_reghop(pTHX_ U8 *s, I32 off)
4488 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4492 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4495 while (off-- && s < lim) {
4496 /* XXX could check well-formedness here */
4504 if (UTF8_IS_CONTINUED(*s)) {
4505 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4508 /* XXX could check well-formedness here */
4516 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4518 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4522 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4525 while (off-- && s < lim) {
4526 /* XXX could check well-formedness here */
4536 if (UTF8_IS_CONTINUED(*s)) {
4537 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4540 /* XXX could check well-formedness here */
4552 restore_pos(pTHX_ void *arg)
4554 if (PL_reg_eval_set) {
4555 if (PL_reg_oldsaved) {
4556 PL_reg_re->subbeg = PL_reg_oldsaved;
4557 PL_reg_re->sublen = PL_reg_oldsavedlen;
4558 RX_MATCH_COPIED_on(PL_reg_re);
4560 PL_reg_magic->mg_len = PL_reg_oldpos;
4561 PL_reg_eval_set = 0;
4562 PL_curpm = PL_reg_oldcurpm;
4567 S_to_utf8_substr(pTHX_ register regexp *prog)
4570 if (prog->float_substr && !prog->float_utf8) {
4571 prog->float_utf8 = sv = NEWSV(117, 0);
4572 SvSetSV(sv, prog->float_substr);
4573 sv_utf8_upgrade(sv);
4574 if (SvTAIL(prog->float_substr))
4576 if (prog->float_substr == prog->check_substr)
4577 prog->check_utf8 = sv;
4579 if (prog->anchored_substr && !prog->anchored_utf8) {
4580 prog->anchored_utf8 = sv = NEWSV(118, 0);
4581 SvSetSV(sv, prog->anchored_substr);
4582 sv_utf8_upgrade(sv);
4583 if (SvTAIL(prog->anchored_substr))
4585 if (prog->anchored_substr == prog->check_substr)
4586 prog->check_utf8 = sv;
4591 S_to_byte_substr(pTHX_ register regexp *prog)
4594 if (prog->float_utf8 && !prog->float_substr) {
4595 prog->float_substr = sv = NEWSV(117, 0);
4596 SvSetSV(sv, prog->float_utf8);
4597 if (sv_utf8_downgrade(sv, TRUE)) {
4598 if (SvTAIL(prog->float_utf8))
4602 prog->float_substr = sv = &PL_sv_undef;
4604 if (prog->float_utf8 == prog->check_utf8)
4605 prog->check_substr = sv;
4607 if (prog->anchored_utf8 && !prog->anchored_substr) {
4608 prog->anchored_substr = sv = NEWSV(118, 0);
4609 SvSetSV(sv, prog->anchored_utf8);
4610 if (sv_utf8_downgrade(sv, TRUE)) {
4611 if (SvTAIL(prog->anchored_utf8))
4615 prog->anchored_substr = sv = &PL_sv_undef;
4617 if (prog->anchored_utf8 == prog->check_utf8)
4618 prog->check_substr = sv;