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-2003, 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 = strbeg + (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;
856 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
861 if (endpos == strend) {
862 DEBUG_r( PerlIO_printf(Perl_debug_log,
863 "Could not match STCLASS...\n") );
866 DEBUG_r( PerlIO_printf(Perl_debug_log,
867 "This position contradicts STCLASS...\n") );
868 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
870 /* Contradict one of substrings */
871 if (prog->anchored_substr || prog->anchored_utf8) {
872 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
873 DEBUG_r( what = "anchored" );
875 s = HOP3c(t, 1, strend);
876 if (s + start_shift + end_shift > strend) {
877 /* XXXX Should be taken into account earlier? */
878 DEBUG_r( PerlIO_printf(Perl_debug_log,
879 "Could not match STCLASS...\n") );
884 DEBUG_r( PerlIO_printf(Perl_debug_log,
885 "Looking for %s substr starting at offset %ld...\n",
886 what, (long)(s + start_shift - i_strpos)) );
889 /* Have both, check_string is floating */
890 if (t + start_shift >= check_at) /* Contradicts floating=check */
891 goto retry_floating_check;
892 /* Recheck anchored substring, but not floating... */
896 DEBUG_r( PerlIO_printf(Perl_debug_log,
897 "Looking for anchored substr starting at offset %ld...\n",
898 (long)(other_last - i_strpos)) );
899 goto do_other_anchored;
901 /* Another way we could have checked stclass at the
902 current position only: */
907 DEBUG_r( PerlIO_printf(Perl_debug_log,
908 "Looking for /%s^%s/m starting at offset %ld...\n",
909 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
912 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
914 /* Check is floating subtring. */
915 retry_floating_check:
916 t = check_at - start_shift;
917 DEBUG_r( what = "floating" );
918 goto hop_and_restart;
921 DEBUG_r(PerlIO_printf(Perl_debug_log,
922 "By STCLASS: moving %ld --> %ld\n",
923 (long)(t - i_strpos), (long)(s - i_strpos))
927 DEBUG_r(PerlIO_printf(Perl_debug_log,
928 "Does not contradict STCLASS...\n");
933 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
934 PL_colors[4], (check ? "Guessed" : "Giving up"),
935 PL_colors[5], (long)(s - i_strpos)) );
938 fail_finish: /* Substring not found */
939 if (prog->check_substr || prog->check_utf8) /* could be removed already */
940 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
942 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
943 PL_colors[4],PL_colors[5]));
947 /* We know what class REx starts with. Try to find this position... */
949 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
951 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
957 register I32 tmp = 1; /* Scratch variable? */
958 register bool do_utf8 = PL_reg_match_utf8;
960 /* We know what class it must start with. */
965 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
966 !UTF8_IS_INVARIANT((U8)s[0]) ?
967 reginclass(c, (U8*)s, 0, do_utf8) :
968 REGINCLASS(c, (U8*)s)) {
969 if (tmp && (norun || regtry(prog, s)))
983 if (REGINCLASS(c, (U8*)s) ||
984 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
985 /* The assignment of 2 is intentional:
986 * for the folded sharp s, the skip is 2. */
987 (skip = SHARP_S_SKIP))) {
988 if (tmp && (norun || regtry(prog, s)))
1000 while (s < strend) {
1001 if (tmp && (norun || regtry(prog, s)))
1012 STRLEN ulen1, ulen2;
1013 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
1014 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1016 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1017 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1019 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC,
1020 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1021 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1022 0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1033 c2 = PL_fold_locale[c1];
1035 e = HOP3c(strend, -(I32)ln, s);
1038 e = s; /* Due to minlen logic of intuit() */
1040 /* The idea in the EXACTF* cases is to first find the
1041 * first character of the EXACTF* node and then, if
1042 * necessary, case-insensitively compare the full
1043 * text of the node. The c1 and c2 are the first
1044 * characters (though in Unicode it gets a bit
1045 * more complicated because there are more cases
1046 * than just upper and lower: one needs to use
1047 * the so-called folding case for case-insensitive
1048 * matching (called "loose matching" in Unicode).
1049 * ibcmp_utf8() will do just that. */
1053 U8 tmpbuf [UTF8_MAXLEN+1];
1054 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1055 STRLEN len, foldlen;
1059 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1061 0 : UTF8_ALLOW_ANY);
1064 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1065 m, (char **)0, ln, (bool)UTF))
1066 && (norun || regtry(prog, s)) )
1069 uvchr_to_utf8(tmpbuf, c);
1070 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1072 && (f == c1 || f == c2)
1073 && (ln == foldlen ||
1074 !ibcmp_utf8((char *) foldbuf,
1075 (char **)0, foldlen, do_utf8,
1077 (char **)0, ln, (bool)UTF))
1078 && (norun || regtry(prog, s)) )
1086 c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1088 0 : UTF8_ALLOW_ANY);
1090 /* Handle some of the three Greek sigmas cases.
1091 * Note that not all the possible combinations
1092 * are handled here: some of them are handled
1093 * by the standard folding rules, and some of
1094 * them (the character class or ANYOF cases)
1095 * are handled during compiletime in
1096 * regexec.c:S_regclass(). */
1097 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1098 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1099 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1101 if ( (c == c1 || c == c2)
1103 ibcmp_utf8(s, (char **)0, 0, do_utf8,
1104 m, (char **)0, ln, (bool)UTF))
1105 && (norun || regtry(prog, s)) )
1108 uvchr_to_utf8(tmpbuf, c);
1109 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1111 && (f == c1 || f == c2)
1112 && (ln == foldlen ||
1113 !ibcmp_utf8((char *) foldbuf,
1114 (char **)0, foldlen, do_utf8,
1116 (char **)0, ln, (bool)UTF))
1117 && (norun || regtry(prog, s)) )
1128 && (ln == 1 || !(OP(c) == EXACTF
1130 : ibcmp_locale(s, m, ln)))
1131 && (norun || regtry(prog, s)) )
1137 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1138 && (ln == 1 || !(OP(c) == EXACTF
1140 : ibcmp_locale(s, m, ln)))
1141 && (norun || regtry(prog, s)) )
1148 PL_reg_flags |= RF_tainted;
1155 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1157 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1159 tmp = ((OP(c) == BOUND ?
1160 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1161 LOAD_UTF8_CHARCLASS(alnum,"a");
1162 while (s < strend) {
1163 if (tmp == !(OP(c) == BOUND ?
1164 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1165 isALNUM_LC_utf8((U8*)s)))
1168 if ((norun || regtry(prog, s)))
1175 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1176 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1177 while (s < strend) {
1179 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1181 if ((norun || regtry(prog, s)))
1187 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1191 PL_reg_flags |= RF_tainted;
1198 U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1200 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1202 tmp = ((OP(c) == NBOUND ?
1203 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1204 LOAD_UTF8_CHARCLASS(alnum,"a");
1205 while (s < strend) {
1206 if (tmp == !(OP(c) == NBOUND ?
1207 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1208 isALNUM_LC_utf8((U8*)s)))
1210 else if ((norun || regtry(prog, s)))
1216 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1217 tmp = ((OP(c) == NBOUND ?
1218 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1219 while (s < strend) {
1221 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1223 else if ((norun || regtry(prog, s)))
1228 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1233 LOAD_UTF8_CHARCLASS(alnum,"a");
1234 while (s < strend) {
1235 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1236 if (tmp && (norun || regtry(prog, s)))
1247 while (s < strend) {
1249 if (tmp && (norun || regtry(prog, s)))
1261 PL_reg_flags |= RF_tainted;
1263 while (s < strend) {
1264 if (isALNUM_LC_utf8((U8*)s)) {
1265 if (tmp && (norun || regtry(prog, s)))
1276 while (s < strend) {
1277 if (isALNUM_LC(*s)) {
1278 if (tmp && (norun || regtry(prog, s)))
1291 LOAD_UTF8_CHARCLASS(alnum,"a");
1292 while (s < strend) {
1293 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1294 if (tmp && (norun || regtry(prog, s)))
1305 while (s < strend) {
1307 if (tmp && (norun || regtry(prog, s)))
1319 PL_reg_flags |= RF_tainted;
1321 while (s < strend) {
1322 if (!isALNUM_LC_utf8((U8*)s)) {
1323 if (tmp && (norun || regtry(prog, s)))
1334 while (s < strend) {
1335 if (!isALNUM_LC(*s)) {
1336 if (tmp && (norun || regtry(prog, s)))
1349 LOAD_UTF8_CHARCLASS(space," ");
1350 while (s < strend) {
1351 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1352 if (tmp && (norun || regtry(prog, s)))
1363 while (s < strend) {
1365 if (tmp && (norun || regtry(prog, s)))
1377 PL_reg_flags |= RF_tainted;
1379 while (s < strend) {
1380 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1381 if (tmp && (norun || regtry(prog, s)))
1392 while (s < strend) {
1393 if (isSPACE_LC(*s)) {
1394 if (tmp && (norun || regtry(prog, s)))
1407 LOAD_UTF8_CHARCLASS(space," ");
1408 while (s < strend) {
1409 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1410 if (tmp && (norun || regtry(prog, s)))
1421 while (s < strend) {
1423 if (tmp && (norun || regtry(prog, s)))
1435 PL_reg_flags |= RF_tainted;
1437 while (s < strend) {
1438 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1439 if (tmp && (norun || regtry(prog, s)))
1450 while (s < strend) {
1451 if (!isSPACE_LC(*s)) {
1452 if (tmp && (norun || regtry(prog, s)))
1465 LOAD_UTF8_CHARCLASS(digit,"0");
1466 while (s < strend) {
1467 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1468 if (tmp && (norun || regtry(prog, s)))
1479 while (s < strend) {
1481 if (tmp && (norun || regtry(prog, s)))
1493 PL_reg_flags |= RF_tainted;
1495 while (s < strend) {
1496 if (isDIGIT_LC_utf8((U8*)s)) {
1497 if (tmp && (norun || regtry(prog, s)))
1508 while (s < strend) {
1509 if (isDIGIT_LC(*s)) {
1510 if (tmp && (norun || regtry(prog, s)))
1523 LOAD_UTF8_CHARCLASS(digit,"0");
1524 while (s < strend) {
1525 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1526 if (tmp && (norun || regtry(prog, s)))
1537 while (s < strend) {
1539 if (tmp && (norun || regtry(prog, s)))
1551 PL_reg_flags |= RF_tainted;
1553 while (s < strend) {
1554 if (!isDIGIT_LC_utf8((U8*)s)) {
1555 if (tmp && (norun || regtry(prog, s)))
1566 while (s < strend) {
1567 if (!isDIGIT_LC(*s)) {
1568 if (tmp && (norun || regtry(prog, s)))
1580 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1589 - regexec_flags - match a regexp against a string
1592 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1593 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1594 /* strend: pointer to null at end of string */
1595 /* strbeg: real beginning of string */
1596 /* minend: end of match must be >=minend after stringarg. */
1597 /* data: May be used for some additional optimizations. */
1598 /* nosave: For optimizations. */
1601 register regnode *c;
1602 register char *startpos = stringarg;
1603 I32 minlen; /* must match at least this many chars */
1604 I32 dontbother = 0; /* how many characters not to try at end */
1605 /* I32 start_shift = 0; */ /* Offset of the start to find
1606 constant substr. */ /* CC */
1607 I32 end_shift = 0; /* Same for the end. */ /* CC */
1608 I32 scream_pos = -1; /* Internal iterator of scream. */
1610 SV* oreplsv = GvSV(PL_replgv);
1611 bool do_utf8 = DO_UTF8(sv);
1613 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1614 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1616 RX_MATCH_UTF8_set(prog,do_utf8);
1622 PL_regnarrate = DEBUG_r_TEST;
1625 /* Be paranoid... */
1626 if (prog == NULL || startpos == NULL) {
1627 Perl_croak(aTHX_ "NULL regexp parameter");
1631 minlen = prog->minlen;
1632 if (strend - startpos < minlen) {
1633 DEBUG_r(PerlIO_printf(Perl_debug_log,
1634 "String too short [regexec_flags]...\n"));
1638 /* Check validity of program. */
1639 if (UCHARAT(prog->program) != REG_MAGIC) {
1640 Perl_croak(aTHX_ "corrupted regexp program");
1644 PL_reg_eval_set = 0;
1647 if (prog->reganch & ROPT_UTF8)
1648 PL_reg_flags |= RF_utf8;
1650 /* Mark beginning of line for ^ and lookbehind. */
1651 PL_regbol = startpos;
1655 /* Mark end of line for $ (and such) */
1658 /* see how far we have to get to not match where we matched before */
1659 PL_regtill = startpos+minend;
1661 /* We start without call_cc context. */
1664 /* If there is a "must appear" string, look for it. */
1667 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1670 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1671 PL_reg_ganch = startpos;
1672 else if (sv && SvTYPE(sv) >= SVt_PVMG
1674 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1675 && mg->mg_len >= 0) {
1676 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1677 if (prog->reganch & ROPT_ANCH_GPOS) {
1678 if (s > PL_reg_ganch)
1683 else /* pos() not defined */
1684 PL_reg_ganch = strbeg;
1687 if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1688 re_scream_pos_data d;
1690 d.scream_olds = &scream_olds;
1691 d.scream_pos = &scream_pos;
1692 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1694 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1695 goto phooey; /* not present */
1701 pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1702 UNI_DISPLAY_REGEX) :
1704 int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1705 char *s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1706 UNI_DISPLAY_REGEX) : startpos;
1707 int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1710 PerlIO_printf(Perl_debug_log,
1711 "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1712 PL_colors[4],PL_colors[5],PL_colors[0],
1715 len0 > 60 ? "..." : "",
1717 (int)(len1 > 60 ? 60 : len1),
1719 (len1 > 60 ? "..." : "")
1723 /* Simplest case: anchored match need be tried only once. */
1724 /* [unless only anchor is BOL and multiline is set] */
1725 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1726 if (s == startpos && regtry(prog, startpos))
1728 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1729 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1734 dontbother = minlen - 1;
1735 end = HOP3c(strend, -dontbother, strbeg) - 1;
1736 /* for multiline we only have to try after newlines */
1737 if (prog->check_substr || prog->check_utf8) {
1741 if (regtry(prog, s))
1746 if (prog->reganch & RE_USE_INTUIT) {
1747 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1758 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1759 if (regtry(prog, s))
1766 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1767 if (regtry(prog, PL_reg_ganch))
1772 /* Messy cases: unanchored match. */
1773 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1774 /* we have /x+whatever/ */
1775 /* it must be a one character string (XXXX Except UTF?) */
1780 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1781 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1782 ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1785 while (s < strend) {
1787 DEBUG_r( did_match = 1 );
1788 if (regtry(prog, s)) goto got_it;
1790 while (s < strend && *s == ch)
1797 while (s < strend) {
1799 DEBUG_r( did_match = 1 );
1800 if (regtry(prog, s)) goto got_it;
1802 while (s < strend && *s == ch)
1808 DEBUG_r(if (!did_match)
1809 PerlIO_printf(Perl_debug_log,
1810 "Did not find anchored character...\n")
1814 else if (prog->anchored_substr != Nullsv
1815 || prog->anchored_utf8 != Nullsv
1816 || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1817 && prog->float_max_offset < strend - s)) {
1822 char *last1; /* Last position checked before */
1826 if (prog->anchored_substr || prog->anchored_utf8) {
1827 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1828 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1829 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1830 back_max = back_min = prog->anchored_offset;
1832 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1833 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1834 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1835 back_max = prog->float_max_offset;
1836 back_min = prog->float_min_offset;
1838 if (must == &PL_sv_undef)
1839 /* could not downgrade utf8 check substring, so must fail */
1842 last = HOP3c(strend, /* Cannot start after this */
1843 -(I32)(CHR_SVLEN(must)
1844 - (SvTAIL(must) != 0) + back_min), strbeg);
1847 last1 = HOPc(s, -1);
1849 last1 = s - 1; /* bogus */
1851 /* XXXX check_substr already used to find `s', can optimize if
1852 check_substr==must. */
1854 dontbother = end_shift;
1855 strend = HOPc(strend, -dontbother);
1856 while ( (s <= last) &&
1857 ((flags & REXEC_SCREAM)
1858 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1859 end_shift, &scream_pos, 0))
1860 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1861 (unsigned char*)strend, must,
1862 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1863 /* we may be pointing at the wrong string */
1864 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1865 s = strbeg + (s - SvPVX(sv));
1866 DEBUG_r( did_match = 1 );
1867 if (HOPc(s, -back_max) > last1) {
1868 last1 = HOPc(s, -back_min);
1869 s = HOPc(s, -back_max);
1872 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1874 last1 = HOPc(s, -back_min);
1878 while (s <= last1) {
1879 if (regtry(prog, s))
1885 while (s <= last1) {
1886 if (regtry(prog, s))
1892 DEBUG_r(if (!did_match)
1893 PerlIO_printf(Perl_debug_log,
1894 "Did not find %s substr `%s%.*s%s'%s...\n",
1895 ((must == prog->anchored_substr || must == prog->anchored_utf8)
1896 ? "anchored" : "floating"),
1898 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1900 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1904 else if ((c = prog->regstclass)) {
1906 I32 op = (U8)OP(prog->regstclass);
1907 /* don't bother with what can't match */
1908 if (PL_regkind[op] != EXACT && op != CANY)
1909 strend = HOPc(strend, -(minlen - 1));
1912 SV *prop = sv_newmortal();
1920 pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1921 UNI_DISPLAY_REGEX) :
1923 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1925 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1926 len1 = UTF ? SvCUR(dsv1) : strend - s;
1927 PerlIO_printf(Perl_debug_log,
1928 "Matching stclass `%*.*s' against `%*.*s'\n",
1932 if (find_byclass(prog, c, s, strend, startpos, 0))
1934 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1938 if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1943 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1944 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1945 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1947 if (flags & REXEC_SCREAM) {
1948 last = screaminstr(sv, float_real, s - strbeg,
1949 end_shift, &scream_pos, 1); /* last one */
1951 last = scream_olds; /* Only one occurrence. */
1952 /* we may be pointing at the wrong string */
1953 else if (RX_MATCH_COPIED(prog))
1954 s = strbeg + (s - SvPVX(sv));
1958 char *little = SvPV(float_real, len);
1960 if (SvTAIL(float_real)) {
1961 if (memEQ(strend - len + 1, little, len - 1))
1962 last = strend - len + 1;
1963 else if (!PL_multiline)
1964 last = memEQ(strend - len, little, len)
1965 ? strend - len : Nullch;
1971 last = rninstr(s, strend, little, little + len);
1973 last = strend; /* matching `$' */
1977 DEBUG_r(PerlIO_printf(Perl_debug_log,
1978 "%sCan't trim the tail, match fails (should not happen)%s\n",
1979 PL_colors[4],PL_colors[5]));
1980 goto phooey; /* Should not happen! */
1982 dontbother = strend - last + prog->float_min_offset;
1984 if (minlen && (dontbother < minlen))
1985 dontbother = minlen - 1;
1986 strend -= dontbother; /* this one's always in bytes! */
1987 /* We don't know much -- general case. */
1990 if (regtry(prog, s))
1999 if (regtry(prog, s))
2001 } while (s++ < strend);
2009 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2011 if (PL_reg_eval_set) {
2012 /* Preserve the current value of $^R */
2013 if (oreplsv != GvSV(PL_replgv))
2014 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2015 restored, the value remains
2017 restore_pos(aTHX_ 0);
2020 /* make sure $`, $&, $', and $digit will work later */
2021 if ( !(flags & REXEC_NOT_FIRST) ) {
2022 RX_MATCH_COPY_FREE(prog);
2023 if (flags & REXEC_COPY_STR) {
2024 I32 i = PL_regeol - startpos + (stringarg - strbeg);
2025 #ifdef PERL_COPY_ON_WRITE
2027 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2029 PerlIO_printf(Perl_debug_log,
2030 "Copy on write: regexp capture, type %d\n",
2033 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2034 prog->subbeg = SvPVX(prog->saved_copy);
2035 assert (SvPOKp(prog->saved_copy));
2039 RX_MATCH_COPIED_on(prog);
2040 s = savepvn(strbeg, i);
2046 prog->subbeg = strbeg;
2047 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2054 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2055 PL_colors[4],PL_colors[5]));
2056 if (PL_reg_eval_set)
2057 restore_pos(aTHX_ 0);
2062 - regtry - try match at specific point
2064 STATIC I32 /* 0 failure, 1 success */
2065 S_regtry(pTHX_ regexp *prog, char *startpos)
2073 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2075 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2078 PL_reg_eval_set = RS_init;
2080 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2081 (IV)(PL_stack_sp - PL_stack_base));
2083 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2084 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2085 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2087 /* Apparently this is not needed, judging by wantarray. */
2088 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2089 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2092 /* Make $_ available to executed code. */
2093 if (PL_reg_sv != DEFSV) {
2094 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2099 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2100 && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2101 /* prepare for quick setting of pos */
2102 sv_magic(PL_reg_sv, (SV*)0,
2103 PERL_MAGIC_regex_global, Nullch, 0);
2104 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2108 PL_reg_oldpos = mg->mg_len;
2109 SAVEDESTRUCTOR_X(restore_pos, 0);
2111 if (!PL_reg_curpm) {
2112 Newz(22,PL_reg_curpm, 1, PMOP);
2115 SV* repointer = newSViv(0);
2116 /* so we know which PL_regex_padav element is PL_reg_curpm */
2117 SvFLAGS(repointer) |= SVf_BREAK;
2118 av_push(PL_regex_padav,repointer);
2119 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2120 PL_regex_pad = AvARRAY(PL_regex_padav);
2124 PM_SETRE(PL_reg_curpm, prog);
2125 PL_reg_oldcurpm = PL_curpm;
2126 PL_curpm = PL_reg_curpm;
2127 if (RX_MATCH_COPIED(prog)) {
2128 /* Here is a serious problem: we cannot rewrite subbeg,
2129 since it may be needed if this match fails. Thus
2130 $` inside (?{}) could fail... */
2131 PL_reg_oldsaved = prog->subbeg;
2132 PL_reg_oldsavedlen = prog->sublen;
2133 #ifdef PERL_COPY_ON_WRITE
2134 PL_nrs = prog->saved_copy;
2136 RX_MATCH_COPIED_off(prog);
2139 PL_reg_oldsaved = Nullch;
2140 prog->subbeg = PL_bostr;
2141 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2143 prog->startp[0] = startpos - PL_bostr;
2144 PL_reginput = startpos;
2145 PL_regstartp = prog->startp;
2146 PL_regendp = prog->endp;
2147 PL_reglastparen = &prog->lastparen;
2148 PL_reglastcloseparen = &prog->lastcloseparen;
2149 prog->lastparen = 0;
2150 prog->lastcloseparen = 0;
2152 DEBUG_r(PL_reg_starttry = startpos);
2153 if (PL_reg_start_tmpl <= prog->nparens) {
2154 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2155 if(PL_reg_start_tmp)
2156 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2158 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2161 /* XXXX What this code is doing here?!!! There should be no need
2162 to do this again and again, PL_reglastparen should take care of
2165 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2166 * Actually, the code in regcppop() (which Ilya may be meaning by
2167 * PL_reglastparen), is not needed at all by the test suite
2168 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2169 * enough, for building DynaLoader, or otherwise this
2170 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2171 * will happen. Meanwhile, this code *is* needed for the
2172 * above-mentioned test suite tests to succeed. The common theme
2173 * on those tests seems to be returning null fields from matches.
2178 if (prog->nparens) {
2179 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2186 if (regmatch(prog->program + 1)) {
2187 prog->endp[0] = PL_reginput - PL_bostr;
2190 REGCP_UNWIND(lastcp);
2194 #define RE_UNWIND_BRANCH 1
2195 #define RE_UNWIND_BRANCHJ 2
2199 typedef struct { /* XX: makes sense to enlarge it... */
2203 } re_unwind_generic_t;
2216 } re_unwind_branch_t;
2218 typedef union re_unwind_t {
2220 re_unwind_generic_t generic;
2221 re_unwind_branch_t branch;
2224 #define sayYES goto yes
2225 #define sayNO goto no
2226 #define sayNO_ANYOF goto no_anyof
2227 #define sayYES_FINAL goto yes_final
2228 #define sayYES_LOUD goto yes_loud
2229 #define sayNO_FINAL goto no_final
2230 #define sayNO_SILENT goto do_no
2231 #define saySAME(x) if (x) goto yes; else goto no
2233 #define REPORT_CODE_OFF 24
2236 - regmatch - main matching routine
2238 * Conceptually the strategy is simple: check to see whether the current
2239 * node matches, call self recursively to see whether the rest matches,
2240 * and then act accordingly. In practice we make some effort to avoid
2241 * recursion, in particular by going through "ordinary" nodes (that don't
2242 * need to know whether the rest of the match failed) by a loop instead of
2245 /* [lwall] I've hoisted the register declarations to the outer block in order to
2246 * maybe save a little bit of pushing and popping on the stack. It also takes
2247 * advantage of machines that use a register save mask on subroutine entry.
2249 STATIC I32 /* 0 failure, 1 success */
2250 S_regmatch(pTHX_ regnode *prog)
2252 register regnode *scan; /* Current node. */
2253 regnode *next; /* Next node. */
2254 regnode *inner; /* Next node in internal branch. */
2255 register I32 nextchr; /* renamed nextchr - nextchar colides with
2256 function of same name */
2257 register I32 n; /* no or next */
2258 register I32 ln = 0; /* len or last */
2259 register char *s = Nullch; /* operand or save */
2260 register char *locinput = PL_reginput;
2261 register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2262 int minmod = 0, sw = 0, logical = 0;
2265 I32 firstcp = PL_savestack_ix;
2267 register bool do_utf8 = PL_reg_match_utf8;
2269 SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2270 SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2271 SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2278 /* Note that nextchr is a byte even in UTF */
2279 nextchr = UCHARAT(locinput);
2281 while (scan != NULL) {
2284 SV *prop = sv_newmortal();
2285 int docolor = *PL_colors[0];
2286 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2287 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2288 /* The part of the string before starttry has one color
2289 (pref0_len chars), between starttry and current
2290 position another one (pref_len - pref0_len chars),
2291 after the current position the third one.
2292 We assume that pref0_len <= pref_len, otherwise we
2293 decrease pref0_len. */
2294 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2295 ? (5 + taill) - l : locinput - PL_bostr;
2298 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2300 pref0_len = pref_len - (locinput - PL_reg_starttry);
2301 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2302 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2303 ? (5 + taill) - pref_len : PL_regeol - locinput);
2304 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2308 if (pref0_len > pref_len)
2309 pref0_len = pref_len;
2310 regprop(prop, scan);
2313 do_utf8 && OP(scan) != CANY ?
2314 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2315 pref0_len, 60, UNI_DISPLAY_REGEX) :
2316 locinput - pref_len;
2317 int len0 = do_utf8 ? strlen(s0) : pref0_len;
2318 char *s1 = do_utf8 && OP(scan) != CANY ?
2319 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2320 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2321 locinput - pref_len + pref0_len;
2322 int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2323 char *s2 = do_utf8 && OP(scan) != CANY ?
2324 pv_uni_display(dsv2, (U8*)locinput,
2325 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2327 int len2 = do_utf8 ? strlen(s2) : l;
2328 PerlIO_printf(Perl_debug_log,
2329 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2330 (IV)(locinput - PL_bostr),
2337 (docolor ? "" : "> <"),
2341 15 - l - pref_len + 1,
2343 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2348 next = scan + NEXT_OFF(scan);
2354 if (locinput == PL_bostr || (PL_multiline &&
2355 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2357 /* regtill = regbol; */
2362 if (locinput == PL_bostr ||
2363 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2369 if (locinput == PL_bostr)
2373 if (locinput == PL_reg_ganch)
2383 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2388 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2390 if (PL_regeol - locinput > 1)
2394 if (PL_regeol != locinput)
2398 if (!nextchr && locinput >= PL_regeol)
2401 locinput += PL_utf8skip[nextchr];
2402 if (locinput > PL_regeol)
2404 nextchr = UCHARAT(locinput);
2407 nextchr = UCHARAT(++locinput);
2410 if (!nextchr && locinput >= PL_regeol)
2412 nextchr = UCHARAT(++locinput);
2415 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2418 locinput += PL_utf8skip[nextchr];
2419 if (locinput > PL_regeol)
2421 nextchr = UCHARAT(locinput);
2424 nextchr = UCHARAT(++locinput);
2429 if (do_utf8 != UTF) {
2430 /* The target and the pattern have differing utf8ness. */
2436 /* The target is utf8, the pattern is not utf8. */
2440 if (NATIVE_TO_UNI(*(U8*)s) !=
2441 utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2443 0 : UTF8_ALLOW_ANY))
2450 /* The target is not utf8, the pattern is utf8. */
2454 if (NATIVE_TO_UNI(*((U8*)l)) !=
2455 utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2457 0 : UTF8_ALLOW_ANY))
2464 nextchr = UCHARAT(locinput);
2467 /* The target and the pattern have the same utf8ness. */
2468 /* Inline the first character, for speed. */
2469 if (UCHARAT(s) != nextchr)
2471 if (PL_regeol - locinput < ln)
2473 if (ln > 1 && memNE(s, locinput, ln))
2476 nextchr = UCHARAT(locinput);
2479 PL_reg_flags |= RF_tainted;
2485 if (do_utf8 || UTF) {
2486 /* Either target or the pattern are utf8. */
2488 char *e = PL_regeol;
2490 if (ibcmp_utf8(s, 0, ln, (bool)UTF,
2491 l, &e, 0, do_utf8)) {
2492 /* One more case for the sharp s:
2493 * pack("U0U*", 0xDF) =~ /ss/i,
2494 * the 0xC3 0x9F are the UTF-8
2495 * byte sequence for the U+00DF. */
2497 toLOWER(s[0]) == 's' &&
2499 toLOWER(s[1]) == 's' &&
2506 nextchr = UCHARAT(locinput);
2510 /* Neither the target and the pattern are utf8. */
2512 /* Inline the first character, for speed. */
2513 if (UCHARAT(s) != nextchr &&
2514 UCHARAT(s) != ((OP(scan) == EXACTF)
2515 ? PL_fold : PL_fold_locale)[nextchr])
2517 if (PL_regeol - locinput < ln)
2519 if (ln > 1 && (OP(scan) == EXACTF
2520 ? ibcmp(s, locinput, ln)
2521 : ibcmp_locale(s, locinput, ln)))
2524 nextchr = UCHARAT(locinput);
2528 STRLEN inclasslen = PL_regeol - locinput;
2530 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2532 if (locinput >= PL_regeol)
2534 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2535 nextchr = UCHARAT(locinput);
2540 nextchr = UCHARAT(locinput);
2541 if (!REGINCLASS(scan, (U8*)locinput))
2543 if (!nextchr && locinput >= PL_regeol)
2545 nextchr = UCHARAT(++locinput);
2549 /* If we might have the case of the German sharp s
2550 * in a casefolding Unicode character class. */
2552 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2553 locinput += SHARP_S_SKIP;
2554 nextchr = UCHARAT(locinput);
2560 PL_reg_flags |= RF_tainted;
2566 LOAD_UTF8_CHARCLASS(alnum,"a");
2567 if (!(OP(scan) == ALNUM
2568 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2569 : isALNUM_LC_utf8((U8*)locinput)))
2573 locinput += PL_utf8skip[nextchr];
2574 nextchr = UCHARAT(locinput);
2577 if (!(OP(scan) == ALNUM
2578 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2580 nextchr = UCHARAT(++locinput);
2583 PL_reg_flags |= RF_tainted;
2586 if (!nextchr && locinput >= PL_regeol)
2589 LOAD_UTF8_CHARCLASS(alnum,"a");
2590 if (OP(scan) == NALNUM
2591 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2592 : isALNUM_LC_utf8((U8*)locinput))
2596 locinput += PL_utf8skip[nextchr];
2597 nextchr = UCHARAT(locinput);
2600 if (OP(scan) == NALNUM
2601 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2603 nextchr = UCHARAT(++locinput);
2607 PL_reg_flags |= RF_tainted;
2611 /* was last char in word? */
2613 if (locinput == PL_bostr)
2616 U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2618 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2620 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2621 ln = isALNUM_uni(ln);
2622 LOAD_UTF8_CHARCLASS(alnum,"a");
2623 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2626 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2627 n = isALNUM_LC_utf8((U8*)locinput);
2631 ln = (locinput != PL_bostr) ?
2632 UCHARAT(locinput - 1) : '\n';
2633 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2635 n = isALNUM(nextchr);
2638 ln = isALNUM_LC(ln);
2639 n = isALNUM_LC(nextchr);
2642 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2643 OP(scan) == BOUNDL))
2647 PL_reg_flags |= RF_tainted;
2653 if (UTF8_IS_CONTINUED(nextchr)) {
2654 LOAD_UTF8_CHARCLASS(space," ");
2655 if (!(OP(scan) == SPACE
2656 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2657 : isSPACE_LC_utf8((U8*)locinput)))
2661 locinput += PL_utf8skip[nextchr];
2662 nextchr = UCHARAT(locinput);
2665 if (!(OP(scan) == SPACE
2666 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2668 nextchr = UCHARAT(++locinput);
2671 if (!(OP(scan) == SPACE
2672 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2674 nextchr = UCHARAT(++locinput);
2678 PL_reg_flags |= RF_tainted;
2681 if (!nextchr && locinput >= PL_regeol)
2684 LOAD_UTF8_CHARCLASS(space," ");
2685 if (OP(scan) == NSPACE
2686 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2687 : isSPACE_LC_utf8((U8*)locinput))
2691 locinput += PL_utf8skip[nextchr];
2692 nextchr = UCHARAT(locinput);
2695 if (OP(scan) == NSPACE
2696 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2698 nextchr = UCHARAT(++locinput);
2701 PL_reg_flags |= RF_tainted;
2707 LOAD_UTF8_CHARCLASS(digit,"0");
2708 if (!(OP(scan) == DIGIT
2709 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2710 : isDIGIT_LC_utf8((U8*)locinput)))
2714 locinput += PL_utf8skip[nextchr];
2715 nextchr = UCHARAT(locinput);
2718 if (!(OP(scan) == DIGIT
2719 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2721 nextchr = UCHARAT(++locinput);
2724 PL_reg_flags |= RF_tainted;
2727 if (!nextchr && locinput >= PL_regeol)
2730 LOAD_UTF8_CHARCLASS(digit,"0");
2731 if (OP(scan) == NDIGIT
2732 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2733 : isDIGIT_LC_utf8((U8*)locinput))
2737 locinput += PL_utf8skip[nextchr];
2738 nextchr = UCHARAT(locinput);
2741 if (OP(scan) == NDIGIT
2742 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2744 nextchr = UCHARAT(++locinput);
2747 if (locinput >= PL_regeol)
2750 LOAD_UTF8_CHARCLASS(mark,"~");
2751 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2753 locinput += PL_utf8skip[nextchr];
2754 while (locinput < PL_regeol &&
2755 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2756 locinput += UTF8SKIP(locinput);
2757 if (locinput > PL_regeol)
2762 nextchr = UCHARAT(locinput);
2765 PL_reg_flags |= RF_tainted;
2769 n = ARG(scan); /* which paren pair */
2770 ln = PL_regstartp[n];
2771 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2772 if ((I32)*PL_reglastparen < n || ln == -1)
2773 sayNO; /* Do not match unless seen CLOSEn. */
2774 if (ln == PL_regendp[n])
2778 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2780 char *e = PL_bostr + PL_regendp[n];
2782 * Note that we can't do the "other character" lookup trick as
2783 * in the 8-bit case (no pun intended) because in Unicode we
2784 * have to map both upper and title case to lower case.
2786 if (OP(scan) == REFF) {
2787 STRLEN ulen1, ulen2;
2788 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2789 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2793 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2794 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2795 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2802 nextchr = UCHARAT(locinput);
2806 /* Inline the first character, for speed. */
2807 if (UCHARAT(s) != nextchr &&
2809 (UCHARAT(s) != ((OP(scan) == REFF
2810 ? PL_fold : PL_fold_locale)[nextchr]))))
2812 ln = PL_regendp[n] - ln;
2813 if (locinput + ln > PL_regeol)
2815 if (ln > 1 && (OP(scan) == REF
2816 ? memNE(s, locinput, ln)
2818 ? ibcmp(s, locinput, ln)
2819 : ibcmp_locale(s, locinput, ln))))
2822 nextchr = UCHARAT(locinput);
2833 OP_4tree *oop = PL_op;
2834 COP *ocurcop = PL_curcop;
2839 PL_op = (OP_4tree*)PL_regdata->data[n];
2840 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2841 PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2842 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2846 CALLRUNOPS(aTHX); /* Scalar context. */
2849 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
2857 PAD_RESTORE_LOCAL(old_comppad);
2858 PL_curcop = ocurcop;
2860 if (logical == 2) { /* Postponed subexpression. */
2862 MAGIC *mg = Null(MAGIC*);
2864 CHECKPOINT cp, lastcp;
2868 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
2869 mg = mg_find(sv, PERL_MAGIC_qr);
2870 else if (SvSMAGICAL(ret)) {
2871 if (SvGMAGICAL(ret))
2872 sv_unmagic(ret, PERL_MAGIC_qr);
2874 mg = mg_find(ret, PERL_MAGIC_qr);
2878 re = (regexp *)mg->mg_obj;
2879 (void)ReREFCNT_inc(re);
2883 char *t = SvPV(ret, len);
2885 char *oprecomp = PL_regprecomp;
2886 I32 osize = PL_regsize;
2887 I32 onpar = PL_regnpar;
2890 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2891 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2893 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
2895 sv_magic(ret,(SV*)ReREFCNT_inc(re),
2897 PL_regprecomp = oprecomp;
2902 PerlIO_printf(Perl_debug_log,
2903 "Entering embedded `%s%.60s%s%s'\n",
2907 (strlen(re->precomp) > 60 ? "..." : ""))
2910 state.prev = PL_reg_call_cc;
2911 state.cc = PL_regcc;
2912 state.re = PL_reg_re;
2916 cp = regcppush(0); /* Save *all* the positions. */
2919 state.ss = PL_savestack_ix;
2920 *PL_reglastparen = 0;
2921 *PL_reglastcloseparen = 0;
2922 PL_reg_call_cc = &state;
2923 PL_reginput = locinput;
2924 toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2925 ((re->reganch & ROPT_UTF8) != 0);
2926 if (toggleutf) PL_reg_flags ^= RF_utf8;
2928 /* XXXX This is too dramatic a measure... */
2931 if (regmatch(re->program + 1)) {
2932 /* Even though we succeeded, we need to restore
2933 global variables, since we may be wrapped inside
2934 SUSPEND, thus the match may be not finished yet. */
2936 /* XXXX Do this only if SUSPENDed? */
2937 PL_reg_call_cc = state.prev;
2938 PL_regcc = state.cc;
2939 PL_reg_re = state.re;
2940 cache_re(PL_reg_re);
2941 if (toggleutf) PL_reg_flags ^= RF_utf8;
2943 /* XXXX This is too dramatic a measure... */
2946 /* These are needed even if not SUSPEND. */
2952 REGCP_UNWIND(lastcp);
2954 PL_reg_call_cc = state.prev;
2955 PL_regcc = state.cc;
2956 PL_reg_re = state.re;
2957 cache_re(PL_reg_re);
2958 if (toggleutf) PL_reg_flags ^= RF_utf8;
2960 /* XXXX This is too dramatic a measure... */
2970 sv_setsv(save_scalar(PL_replgv), ret);
2974 n = ARG(scan); /* which paren pair */
2975 PL_reg_start_tmp[n] = locinput;
2980 n = ARG(scan); /* which paren pair */
2981 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2982 PL_regendp[n] = locinput - PL_bostr;
2983 if (n > (I32)*PL_reglastparen)
2984 *PL_reglastparen = n;
2985 *PL_reglastcloseparen = n;
2988 n = ARG(scan); /* which paren pair */
2989 sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2992 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2994 next = NEXTOPER(NEXTOPER(scan));
2996 next = scan + ARG(scan);
2997 if (OP(next) == IFTHEN) /* Fake one. */
2998 next = NEXTOPER(NEXTOPER(next));
3002 logical = scan->flags;
3004 /*******************************************************************
3005 PL_regcc contains infoblock about the innermost (...)* loop, and
3006 a pointer to the next outer infoblock.
3008 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3010 1) After matching X, regnode for CURLYX is processed;
3012 2) This regnode creates infoblock on the stack, and calls
3013 regmatch() recursively with the starting point at WHILEM node;
3015 3) Each hit of WHILEM node tries to match A and Z (in the order
3016 depending on the current iteration, min/max of {min,max} and
3017 greediness). The information about where are nodes for "A"
3018 and "Z" is read from the infoblock, as is info on how many times "A"
3019 was already matched, and greediness.
3021 4) After A matches, the same WHILEM node is hit again.
3023 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3024 of the same pair. Thus when WHILEM tries to match Z, it temporarily
3025 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3026 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3027 of the external loop.
3029 Currently present infoblocks form a tree with a stem formed by PL_curcc
3030 and whatever it mentions via ->next, and additional attached trees
3031 corresponding to temporarily unset infoblocks as in "5" above.
3033 In the following picture infoblocks for outer loop of
3034 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3035 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3036 infoblocks are drawn below the "reset" infoblock.
3038 In fact in the picture below we do not show failed matches for Z and T
3039 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3040 more obvious *why* one needs to *temporary* unset infoblocks.]
3042 Matched REx position InfoBlocks Comment
3046 Y A)*?Z)*?T x <- O <- I
3047 YA )*?Z)*?T x <- O <- I
3048 YA A)*?Z)*?T x <- O <- I
3049 YAA )*?Z)*?T x <- O <- I
3050 YAA Z)*?T x <- O # Temporary unset I
3053 YAAZ Y(A)*?Z)*?T x <- O
3056 YAAZY (A)*?Z)*?T x <- O
3059 YAAZY A)*?Z)*?T x <- O <- I
3062 YAAZYA )*?Z)*?T x <- O <- I
3065 YAAZYA Z)*?T x <- O # Temporary unset I
3071 YAAZYAZ T x # Temporary unset O
3078 *******************************************************************/
3081 CHECKPOINT cp = PL_savestack_ix;
3082 /* No need to save/restore up to this paren */
3083 I32 parenfloor = scan->flags;
3085 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3087 cc.oldcc = PL_regcc;
3089 /* XXXX Probably it is better to teach regpush to support
3090 parenfloor > PL_regsize... */
3091 if (parenfloor > (I32)*PL_reglastparen)
3092 parenfloor = *PL_reglastparen; /* Pessimization... */
3093 cc.parenfloor = parenfloor;
3095 cc.min = ARG1(scan);
3096 cc.max = ARG2(scan);
3097 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3101 PL_reginput = locinput;
3102 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
3104 PL_regcc = cc.oldcc;
3110 * This is really hard to understand, because after we match
3111 * what we're trying to match, we must make sure the rest of
3112 * the REx is going to match for sure, and to do that we have
3113 * to go back UP the parse tree by recursing ever deeper. And
3114 * if it fails, we have to reset our parent's current state
3115 * that we can try again after backing off.
3118 CHECKPOINT cp, lastcp;
3119 CURCUR* cc = PL_regcc;
3120 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3122 n = cc->cur + 1; /* how many we know we matched */
3123 PL_reginput = locinput;
3126 PerlIO_printf(Perl_debug_log,
3127 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3128 REPORT_CODE_OFF+PL_regindent*2, "",
3129 (long)n, (long)cc->min,
3130 (long)cc->max, PTR2UV(cc))
3133 /* If degenerate scan matches "", assume scan done. */
3135 if (locinput == cc->lastloc && n >= cc->min) {
3136 PL_regcc = cc->oldcc;
3140 PerlIO_printf(Perl_debug_log,
3141 "%*s empty match detected, try continuation...\n",
3142 REPORT_CODE_OFF+PL_regindent*2, "")
3144 if (regmatch(cc->next))
3152 /* First just match a string of min scans. */
3156 cc->lastloc = locinput;
3157 if (regmatch(cc->scan))
3160 cc->lastloc = lastloc;
3165 /* Check whether we already were at this position.
3166 Postpone detection until we know the match is not
3167 *that* much linear. */
3168 if (!PL_reg_maxiter) {
3169 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3170 PL_reg_leftiter = PL_reg_maxiter;
3172 if (PL_reg_leftiter-- == 0) {
3173 I32 size = (PL_reg_maxiter + 7)/8;
3174 if (PL_reg_poscache) {
3175 if ((I32)PL_reg_poscache_size < size) {
3176 Renew(PL_reg_poscache, size, char);
3177 PL_reg_poscache_size = size;
3179 Zero(PL_reg_poscache, size, char);
3182 PL_reg_poscache_size = size;
3183 Newz(29, PL_reg_poscache, size, char);
3186 PerlIO_printf(Perl_debug_log,
3187 "%sDetected a super-linear match, switching on caching%s...\n",
3188 PL_colors[4], PL_colors[5])
3191 if (PL_reg_leftiter < 0) {
3192 I32 o = locinput - PL_bostr, b;
3194 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3197 if (PL_reg_poscache[o] & (1<<b)) {
3199 PerlIO_printf(Perl_debug_log,
3200 "%*s already tried at this position...\n",
3201 REPORT_CODE_OFF+PL_regindent*2, "")
3205 PL_reg_poscache[o] |= (1<<b);
3209 /* Prefer next over scan for minimal matching. */
3212 PL_regcc = cc->oldcc;
3215 cp = regcppush(cc->parenfloor);
3217 if (regmatch(cc->next)) {
3219 sayYES; /* All done. */
3221 REGCP_UNWIND(lastcp);
3227 if (n >= cc->max) { /* Maximum greed exceeded? */
3228 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3229 && !(PL_reg_flags & RF_warned)) {
3230 PL_reg_flags |= RF_warned;
3231 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3232 "Complex regular subexpression recursion",
3239 PerlIO_printf(Perl_debug_log,
3240 "%*s trying longer...\n",
3241 REPORT_CODE_OFF+PL_regindent*2, "")
3243 /* Try scanning more and see if it helps. */
3244 PL_reginput = locinput;
3246 cc->lastloc = locinput;
3247 cp = regcppush(cc->parenfloor);
3249 if (regmatch(cc->scan)) {
3253 REGCP_UNWIND(lastcp);
3256 cc->lastloc = lastloc;
3260 /* Prefer scan over next for maximal matching. */
3262 if (n < cc->max) { /* More greed allowed? */
3263 cp = regcppush(cc->parenfloor);
3265 cc->lastloc = locinput;
3267 if (regmatch(cc->scan)) {
3271 REGCP_UNWIND(lastcp);
3272 regcppop(); /* Restore some previous $<digit>s? */
3273 PL_reginput = locinput;
3275 PerlIO_printf(Perl_debug_log,
3276 "%*s failed, try continuation...\n",
3277 REPORT_CODE_OFF+PL_regindent*2, "")
3280 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3281 && !(PL_reg_flags & RF_warned)) {
3282 PL_reg_flags |= RF_warned;
3283 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3284 "Complex regular subexpression recursion",
3288 /* Failed deeper matches of scan, so see if this one works. */
3289 PL_regcc = cc->oldcc;
3292 if (regmatch(cc->next))
3298 cc->lastloc = lastloc;
3303 next = scan + ARG(scan);
3306 inner = NEXTOPER(NEXTOPER(scan));
3309 inner = NEXTOPER(scan);
3313 if (OP(next) != c1) /* No choice. */
3314 next = inner; /* Avoid recursion. */
3316 I32 lastparen = *PL_reglastparen;
3318 re_unwind_branch_t *uw;
3320 /* Put unwinding data on stack */
3321 unwind1 = SSNEWt(1,re_unwind_branch_t);
3322 uw = SSPTRt(unwind1,re_unwind_branch_t);
3325 uw->type = ((c1 == BRANCH)
3327 : RE_UNWIND_BRANCHJ);
3328 uw->lastparen = lastparen;
3330 uw->locinput = locinput;
3331 uw->nextchr = nextchr;
3333 uw->regindent = ++PL_regindent;
3336 REGCP_SET(uw->lastcp);
3338 /* Now go into the first branch */
3351 /* We suppose that the next guy does not need
3352 backtracking: in particular, it is of constant length,
3353 and has no parenths to influence future backrefs. */
3354 ln = ARG1(scan); /* min to match */
3355 n = ARG2(scan); /* max to match */
3356 paren = scan->flags;
3358 if (paren > PL_regsize)
3360 if (paren > (I32)*PL_reglastparen)
3361 *PL_reglastparen = paren;
3363 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3365 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3366 PL_reginput = locinput;
3369 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3371 /* if we matched something zero-length we don't need to
3372 backtrack - capturing parens are already defined, so
3373 the caveat in the maximal case doesn't apply
3375 XXXX if ln == 0, we can redo this check first time
3376 through the following loop
3379 n = ln; /* don't backtrack */
3380 locinput = PL_reginput;
3381 if (HAS_TEXT(next) || JUMPABLE(next)) {
3382 regnode *text_node = next;
3384 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3386 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3388 if (PL_regkind[(U8)OP(text_node)] == REF) {
3390 n = ARG(text_node); /* which paren pair */
3391 ln = PL_regstartp[n];
3392 /* assume yes if we haven't seen CLOSEn */
3394 (I32)*PL_reglastparen < n ||
3401 c1 = *(PL_bostr + ln);
3403 else { c1 = (U8)*STRING(text_node); }
3404 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3406 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3407 c2 = PL_fold_locale[c1];
3416 /* This may be improved if l == 0. */
3417 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3418 /* If it could work, try it. */
3420 UCHARAT(PL_reginput) == c1 ||
3421 UCHARAT(PL_reginput) == c2)
3425 PL_regstartp[paren] =
3426 HOPc(PL_reginput, -l) - PL_bostr;
3427 PL_regendp[paren] = PL_reginput - PL_bostr;
3430 PL_regendp[paren] = -1;
3434 REGCP_UNWIND(lastcp);
3436 /* Couldn't or didn't -- move forward. */
3437 PL_reginput = locinput;
3438 if (regrepeat_hard(scan, 1, &l)) {
3440 locinput = PL_reginput;
3447 n = regrepeat_hard(scan, n, &l);
3448 /* if we matched something zero-length we don't need to
3449 backtrack, unless the minimum count is zero and we
3450 are capturing the result - in that case the capture
3451 being defined or not may affect later execution
3453 if (n != 0 && l == 0 && !(paren && ln == 0))
3454 ln = n; /* don't backtrack */
3455 locinput = PL_reginput;
3457 PerlIO_printf(Perl_debug_log,
3458 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3459 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3463 if (HAS_TEXT(next) || JUMPABLE(next)) {
3464 regnode *text_node = next;
3466 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3468 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3470 if (PL_regkind[(U8)OP(text_node)] == REF) {
3472 n = ARG(text_node); /* which paren pair */
3473 ln = PL_regstartp[n];
3474 /* assume yes if we haven't seen CLOSEn */
3476 (I32)*PL_reglastparen < n ||
3483 c1 = *(PL_bostr + ln);
3485 else { c1 = (U8)*STRING(text_node); }
3487 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3489 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3490 c2 = PL_fold_locale[c1];
3501 /* If it could work, try it. */
3503 UCHARAT(PL_reginput) == c1 ||
3504 UCHARAT(PL_reginput) == c2)
3507 PerlIO_printf(Perl_debug_log,
3508 "%*s trying tail with n=%"IVdf"...\n",
3509 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3513 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3514 PL_regendp[paren] = PL_reginput - PL_bostr;
3517 PL_regendp[paren] = -1;
3521 REGCP_UNWIND(lastcp);
3523 /* Couldn't or didn't -- back up. */
3525 locinput = HOPc(locinput, -l);
3526 PL_reginput = locinput;
3533 paren = scan->flags; /* Which paren to set */
3534 if (paren > PL_regsize)
3536 if (paren > (I32)*PL_reglastparen)
3537 *PL_reglastparen = paren;
3538 ln = ARG1(scan); /* min to match */
3539 n = ARG2(scan); /* max to match */
3540 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3544 ln = ARG1(scan); /* min to match */
3545 n = ARG2(scan); /* max to match */
3546 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3551 scan = NEXTOPER(scan);
3557 scan = NEXTOPER(scan);
3561 * Lookahead to avoid useless match attempts
3562 * when we know what character comes next.
3566 * Used to only do .*x and .*?x, but now it allows
3567 * for )'s, ('s and (?{ ... })'s to be in the way
3568 * of the quantifier and the EXACT-like node. -- japhy
3571 if (HAS_TEXT(next) || JUMPABLE(next)) {
3573 regnode *text_node = next;
3575 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3577 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3579 if (PL_regkind[(U8)OP(text_node)] == REF) {
3581 n = ARG(text_node); /* which paren pair */
3582 ln = PL_regstartp[n];
3583 /* assume yes if we haven't seen CLOSEn */
3585 (I32)*PL_reglastparen < n ||
3590 goto assume_ok_easy;
3592 s = (U8*)PL_bostr + ln;
3594 else { s = (U8*)STRING(text_node); }
3598 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3600 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3601 c2 = PL_fold_locale[c1];
3604 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3605 STRLEN ulen1, ulen2;
3606 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3607 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3609 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3610 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3612 c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3614 0 : UTF8_ALLOW_ANY);
3615 c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3617 0 : UTF8_ALLOW_ANY);
3620 c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3622 0 : UTF8_ALLOW_ANY);
3630 PL_reginput = locinput;
3634 if (ln && regrepeat(scan, ln) < ln)
3636 locinput = PL_reginput;
3639 char *e; /* Should not check after this */
3640 char *old = locinput;
3643 if (n == REG_INFTY) {
3646 while (UTF8_IS_CONTINUATION(*(U8*)e))
3652 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3656 e = locinput + n - ln;
3661 /* Find place 'next' could work */
3664 while (locinput <= e &&
3665 UCHARAT(locinput) != c1)
3668 while (locinput <= e
3669 && UCHARAT(locinput) != c1
3670 && UCHARAT(locinput) != c2)
3673 count = locinput - old;
3678 /* count initialised to
3679 * utf8_distance(old, locinput) */
3680 while (locinput <= e &&
3681 utf8n_to_uvchr((U8*)locinput,
3684 0 : UTF8_ALLOW_ANY) != (UV)c1) {
3689 /* count initialised to
3690 * utf8_distance(old, locinput) */
3691 while (locinput <= e) {
3692 UV c = utf8n_to_uvchr((U8*)locinput,
3695 0 : UTF8_ALLOW_ANY);
3696 if (c == (UV)c1 || c == (UV)c2)
3705 /* PL_reginput == old now */
3706 if (locinput != old) {
3707 ln = 1; /* Did some */
3708 if (regrepeat(scan, count) < count)
3711 /* PL_reginput == locinput now */
3712 TRYPAREN(paren, ln, locinput);
3713 PL_reginput = locinput; /* Could be reset... */
3714 REGCP_UNWIND(lastcp);
3715 /* Couldn't or didn't -- move forward. */
3718 locinput += UTF8SKIP(locinput);
3725 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3729 c = utf8n_to_uvchr((U8*)PL_reginput,
3732 0 : UTF8_ALLOW_ANY);
3734 c = UCHARAT(PL_reginput);
3735 /* If it could work, try it. */
3736 if (c == (UV)c1 || c == (UV)c2)
3738 TRYPAREN(paren, ln, PL_reginput);
3739 REGCP_UNWIND(lastcp);
3742 /* If it could work, try it. */
3743 else if (c1 == -1000)
3745 TRYPAREN(paren, ln, PL_reginput);
3746 REGCP_UNWIND(lastcp);
3748 /* Couldn't or didn't -- move forward. */
3749 PL_reginput = locinput;
3750 if (regrepeat(scan, 1)) {
3752 locinput = PL_reginput;
3760 n = regrepeat(scan, n);
3761 locinput = PL_reginput;
3762 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3763 ((!PL_multiline && OP(next) != MEOL) ||
3764 OP(next) == SEOL || OP(next) == EOS))
3766 ln = n; /* why back off? */
3767 /* ...because $ and \Z can match before *and* after
3768 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3769 We should back off by one in this case. */
3770 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3779 c = utf8n_to_uvchr((U8*)PL_reginput,
3782 0 : UTF8_ALLOW_ANY);
3784 c = UCHARAT(PL_reginput);
3786 /* If it could work, try it. */
3787 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3789 TRYPAREN(paren, n, PL_reginput);
3790 REGCP_UNWIND(lastcp);
3792 /* Couldn't or didn't -- back up. */
3794 PL_reginput = locinput = HOPc(locinput, -1);
3802 c = utf8n_to_uvchr((U8*)PL_reginput,
3805 0 : UTF8_ALLOW_ANY);
3807 c = UCHARAT(PL_reginput);
3809 /* If it could work, try it. */
3810 if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3812 TRYPAREN(paren, n, PL_reginput);
3813 REGCP_UNWIND(lastcp);
3815 /* Couldn't or didn't -- back up. */
3817 PL_reginput = locinput = HOPc(locinput, -1);
3824 if (PL_reg_call_cc) {
3825 re_cc_state *cur_call_cc = PL_reg_call_cc;
3826 CURCUR *cctmp = PL_regcc;
3827 regexp *re = PL_reg_re;
3828 CHECKPOINT cp, lastcp;
3830 cp = regcppush(0); /* Save *all* the positions. */
3832 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3834 PL_reginput = locinput; /* Make position available to
3836 cache_re(PL_reg_call_cc->re);
3837 PL_regcc = PL_reg_call_cc->cc;
3838 PL_reg_call_cc = PL_reg_call_cc->prev;
3839 if (regmatch(cur_call_cc->node)) {
3840 PL_reg_call_cc = cur_call_cc;
3844 REGCP_UNWIND(lastcp);
3846 PL_reg_call_cc = cur_call_cc;
3852 PerlIO_printf(Perl_debug_log,
3853 "%*s continuation failed...\n",
3854 REPORT_CODE_OFF+PL_regindent*2, "")
3858 if (locinput < PL_regtill) {
3859 DEBUG_r(PerlIO_printf(Perl_debug_log,
3860 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3862 (long)(locinput - PL_reg_starttry),
3863 (long)(PL_regtill - PL_reg_starttry),
3865 sayNO_FINAL; /* Cannot match: too short. */
3867 PL_reginput = locinput; /* put where regtry can find it */
3868 sayYES_FINAL; /* Success! */
3870 PL_reginput = locinput; /* put where regtry can find it */
3871 sayYES_LOUD; /* Success! */
3874 PL_reginput = locinput;
3879 s = HOPBACKc(locinput, scan->flags);
3885 PL_reginput = locinput;
3890 s = HOPBACKc(locinput, scan->flags);
3896 PL_reginput = locinput;
3899 inner = NEXTOPER(NEXTOPER(scan));
3900 if (regmatch(inner) != n) {
3915 if (OP(scan) == SUSPEND) {
3916 locinput = PL_reginput;
3917 nextchr = UCHARAT(locinput);
3922 next = scan + ARG(scan);
3927 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3928 PTR2UV(scan), OP(scan));
3929 Perl_croak(aTHX_ "regexp memory corruption");
3936 * We get here only if there's trouble -- normally "case END" is
3937 * the terminating point.
3939 Perl_croak(aTHX_ "corrupted regexp pointers");
3945 PerlIO_printf(Perl_debug_log,
3946 "%*s %scould match...%s\n",
3947 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3951 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3952 PL_colors[4],PL_colors[5]));
3958 #if 0 /* Breaks $^R */
3966 PerlIO_printf(Perl_debug_log,
3967 "%*s %sfailed...%s\n",
3968 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3974 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3977 case RE_UNWIND_BRANCH:
3978 case RE_UNWIND_BRANCHJ:
3980 re_unwind_branch_t *uwb = &(uw->branch);
3981 I32 lastparen = uwb->lastparen;
3983 REGCP_UNWIND(uwb->lastcp);
3984 for (n = *PL_reglastparen; n > lastparen; n--)
3986 *PL_reglastparen = n;
3987 scan = next = uwb->next;
3989 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3990 ? BRANCH : BRANCHJ) ) { /* Failure */
3997 /* Have more choice yet. Reuse the same uwb. */
3999 if ((n = (uwb->type == RE_UNWIND_BRANCH
4000 ? NEXT_OFF(next) : ARG(next))))
4003 next = NULL; /* XXXX Needn't unwinding in this case... */
4005 next = NEXTOPER(scan);
4006 if (uwb->type == RE_UNWIND_BRANCHJ)
4007 next = NEXTOPER(next);
4008 locinput = uwb->locinput;
4009 nextchr = uwb->nextchr;
4011 PL_regindent = uwb->regindent;
4018 Perl_croak(aTHX_ "regexp unwind memory corruption");
4029 - regrepeat - repeatedly match something simple, report how many
4032 * [This routine now assumes that it will only match on things of length 1.
4033 * That was true before, but now we assume scan - reginput is the count,
4034 * rather than incrementing count on every character. [Er, except utf8.]]
4037 S_regrepeat(pTHX_ regnode *p, I32 max)
4039 register char *scan;
4041 register char *loceol = PL_regeol;
4042 register I32 hardcount = 0;
4043 register bool do_utf8 = PL_reg_match_utf8;
4046 if (max == REG_INFTY)
4048 else if (max < loceol - scan)
4049 loceol = scan + max;
4054 while (scan < loceol && hardcount < max && *scan != '\n') {
4055 scan += UTF8SKIP(scan);
4059 while (scan < loceol && *scan != '\n')
4066 while (scan < loceol && hardcount < max) {
4067 scan += UTF8SKIP(scan);
4077 case EXACT: /* length of string is 1 */
4079 while (scan < loceol && UCHARAT(scan) == c)
4082 case EXACTF: /* length of string is 1 */
4084 while (scan < loceol &&
4085 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4088 case EXACTFL: /* length of string is 1 */
4089 PL_reg_flags |= RF_tainted;
4091 while (scan < loceol &&
4092 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4098 while (hardcount < max && scan < loceol &&
4099 reginclass(p, (U8*)scan, 0, do_utf8)) {
4100 scan += UTF8SKIP(scan);
4104 while (scan < loceol && REGINCLASS(p, (U8*)scan))
4111 LOAD_UTF8_CHARCLASS(alnum,"a");
4112 while (hardcount < max && scan < loceol &&
4113 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4114 scan += UTF8SKIP(scan);
4118 while (scan < loceol && isALNUM(*scan))
4123 PL_reg_flags |= RF_tainted;
4126 while (hardcount < max && scan < loceol &&
4127 isALNUM_LC_utf8((U8*)scan)) {
4128 scan += UTF8SKIP(scan);
4132 while (scan < loceol && isALNUM_LC(*scan))
4139 LOAD_UTF8_CHARCLASS(alnum,"a");
4140 while (hardcount < max && scan < loceol &&
4141 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4142 scan += UTF8SKIP(scan);
4146 while (scan < loceol && !isALNUM(*scan))
4151 PL_reg_flags |= RF_tainted;
4154 while (hardcount < max && scan < loceol &&
4155 !isALNUM_LC_utf8((U8*)scan)) {
4156 scan += UTF8SKIP(scan);
4160 while (scan < loceol && !isALNUM_LC(*scan))
4167 LOAD_UTF8_CHARCLASS(space," ");
4168 while (hardcount < max && scan < loceol &&
4170 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4171 scan += UTF8SKIP(scan);
4175 while (scan < loceol && isSPACE(*scan))
4180 PL_reg_flags |= RF_tainted;
4183 while (hardcount < max && scan < loceol &&
4184 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4185 scan += UTF8SKIP(scan);
4189 while (scan < loceol && isSPACE_LC(*scan))
4196 LOAD_UTF8_CHARCLASS(space," ");
4197 while (hardcount < max && scan < loceol &&
4199 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4200 scan += UTF8SKIP(scan);
4204 while (scan < loceol && !isSPACE(*scan))
4209 PL_reg_flags |= RF_tainted;
4212 while (hardcount < max && scan < loceol &&
4213 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4214 scan += UTF8SKIP(scan);
4218 while (scan < loceol && !isSPACE_LC(*scan))
4225 LOAD_UTF8_CHARCLASS(digit,"0");
4226 while (hardcount < max && scan < loceol &&
4227 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4228 scan += UTF8SKIP(scan);
4232 while (scan < loceol && isDIGIT(*scan))
4239 LOAD_UTF8_CHARCLASS(digit,"0");
4240 while (hardcount < max && scan < loceol &&
4241 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4242 scan += UTF8SKIP(scan);
4246 while (scan < loceol && !isDIGIT(*scan))
4250 default: /* Called on something of 0 width. */
4251 break; /* So match right here or not at all. */
4257 c = scan - PL_reginput;
4262 SV *prop = sv_newmortal();
4265 PerlIO_printf(Perl_debug_log,
4266 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4267 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4274 - regrepeat_hard - repeatedly match something, report total lenth and length
4276 * The repeater is supposed to have constant length.
4280 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4282 register char *scan = Nullch;
4283 register char *start;
4284 register char *loceol = PL_regeol;
4286 I32 count = 0, res = 1;
4291 start = PL_reginput;
4292 if (PL_reg_match_utf8) {
4293 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4296 while (start < PL_reginput) {
4298 start += UTF8SKIP(start);
4309 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4311 *lp = l = PL_reginput - start;
4312 if (max != REG_INFTY && l*max < loceol - scan)
4313 loceol = scan + l*max;
4326 - regclass_swash - prepare the utf8 swash
4330 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4336 if (PL_regdata && PL_regdata->count) {
4339 if (PL_regdata->what[n] == 's') {
4340 SV *rv = (SV*)PL_regdata->data[n];
4341 AV *av = (AV*)SvRV((SV*)rv);
4342 SV **ary = AvARRAY(av);
4345 /* See the end of regcomp.c:S_reglass() for
4346 * documentation of these array elements. */
4349 a = SvTYPE(ary[1]) == SVt_RV ? &ary[1] : 0;
4350 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4354 else if (si && doinit) {
4355 sw = swash_init("utf8", "", si, 1, 0);
4356 (void)av_store(av, 1, sw);
4372 - reginclass - determine if a character falls into a character class
4374 The n is the ANYOF regnode, the p is the target string, lenp
4375 is pointer to the maximum length of how far to go in the p
4376 (if the lenp is zero, UTF8SKIP(p) is used),
4377 do_utf8 tells whether the target string is in UTF-8.
4382 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4384 char flags = ANYOF_FLAGS(n);
4390 if (do_utf8 && !UTF8_IS_INVARIANT(c))
4391 c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4392 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4394 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4395 if (do_utf8 || (flags & ANYOF_UNICODE)) {
4398 if (do_utf8 && !ANYOF_RUNTIME(n)) {
4399 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4402 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4406 SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4409 if (swash_fetch(sw, p, do_utf8))
4411 else if (flags & ANYOF_FOLD) {
4412 if (!match && lenp && av) {
4415 for (i = 0; i <= av_len(av); i++) {
4416 SV* sv = *av_fetch(av, i, FALSE);
4418 char *s = SvPV(sv, len);
4420 if (len <= plen && memEQ(s, (char*)p, len)) {
4428 U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4431 to_utf8_fold(p, tmpbuf, &tmplen);
4432 if (swash_fetch(sw, tmpbuf, do_utf8))
4438 if (match && lenp && *lenp == 0)
4439 *lenp = UNISKIP(NATIVE_TO_UNI(c));
4441 if (!match && c < 256) {
4442 if (ANYOF_BITMAP_TEST(n, c))
4444 else if (flags & ANYOF_FOLD) {
4447 if (flags & ANYOF_LOCALE) {
4448 PL_reg_flags |= RF_tainted;
4449 f = PL_fold_locale[c];
4453 if (f != c && ANYOF_BITMAP_TEST(n, f))
4457 if (!match && (flags & ANYOF_CLASS)) {
4458 PL_reg_flags |= RF_tainted;
4460 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4461 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4462 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4463 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4464 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4465 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4466 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4467 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4468 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4469 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4470 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4471 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4472 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4473 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4474 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4475 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4476 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4477 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4478 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4479 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4480 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4481 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4482 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4483 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4484 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4485 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4486 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4487 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4488 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4489 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4490 ) /* How's that for a conditional? */
4497 return (flags & ANYOF_INVERT) ? !match : match;
4501 S_reghop(pTHX_ U8 *s, I32 off)
4503 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4507 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4510 while (off-- && s < lim) {
4511 /* XXX could check well-formedness here */
4519 if (UTF8_IS_CONTINUED(*s)) {
4520 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4523 /* XXX could check well-formedness here */
4531 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4533 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4537 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4540 while (off-- && s < lim) {
4541 /* XXX could check well-formedness here */
4551 if (UTF8_IS_CONTINUED(*s)) {
4552 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4555 /* XXX could check well-formedness here */
4567 restore_pos(pTHX_ void *arg)
4569 if (PL_reg_eval_set) {
4570 if (PL_reg_oldsaved) {
4571 PL_reg_re->subbeg = PL_reg_oldsaved;
4572 PL_reg_re->sublen = PL_reg_oldsavedlen;
4573 #ifdef PERL_COPY_ON_WRITE
4574 PL_reg_re->saved_copy = PL_nrs;
4576 RX_MATCH_COPIED_on(PL_reg_re);
4578 PL_reg_magic->mg_len = PL_reg_oldpos;
4579 PL_reg_eval_set = 0;
4580 PL_curpm = PL_reg_oldcurpm;
4585 S_to_utf8_substr(pTHX_ register regexp *prog)
4588 if (prog->float_substr && !prog->float_utf8) {
4589 prog->float_utf8 = sv = NEWSV(117, 0);
4590 SvSetSV(sv, prog->float_substr);
4591 sv_utf8_upgrade(sv);
4592 if (SvTAIL(prog->float_substr))
4594 if (prog->float_substr == prog->check_substr)
4595 prog->check_utf8 = sv;
4597 if (prog->anchored_substr && !prog->anchored_utf8) {
4598 prog->anchored_utf8 = sv = NEWSV(118, 0);
4599 SvSetSV(sv, prog->anchored_substr);
4600 sv_utf8_upgrade(sv);
4601 if (SvTAIL(prog->anchored_substr))
4603 if (prog->anchored_substr == prog->check_substr)
4604 prog->check_utf8 = sv;
4609 S_to_byte_substr(pTHX_ register regexp *prog)
4612 if (prog->float_utf8 && !prog->float_substr) {
4613 prog->float_substr = sv = NEWSV(117, 0);
4614 SvSetSV(sv, prog->float_utf8);
4615 if (sv_utf8_downgrade(sv, TRUE)) {
4616 if (SvTAIL(prog->float_utf8))
4620 prog->float_substr = sv = &PL_sv_undef;
4622 if (prog->float_utf8 == prog->check_utf8)
4623 prog->check_substr = sv;
4625 if (prog->anchored_utf8 && !prog->anchored_substr) {
4626 prog->anchored_substr = sv = NEWSV(118, 0);
4627 SvSetSV(sv, prog->anchored_utf8);
4628 if (sv_utf8_downgrade(sv, TRUE)) {
4629 if (SvTAIL(prog->anchored_utf8))
4633 prog->anchored_substr = sv = &PL_sv_undef;
4635 if (prog->anchored_utf8 == prog->check_utf8)
4636 prog->check_substr = sv;