5 * One Ring to rule them all, One Ring to find them
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
12 /* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
14 * a regular expression.
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
26 /* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
31 /* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
36 #ifdef PERL_EXT_RE_BUILD
41 * pregcomp and pregexec -- regsub and regerror are not used in perl
43 * Copyright (c) 1986 by University of Toronto.
44 * Written by Henry Spencer. Not derived from licensed software.
46 * Permission is granted to anyone to use this software for any
47 * purpose on any computer system, and to redistribute it freely,
48 * subject to the following restrictions:
50 * 1. The author is not responsible for the consequences of use of
51 * this software, no matter how awful, even if they arise
54 * 2. The origin of this software must not be misrepresented, either
55 * by explicit claim or by omission.
57 * 3. Altered versions must be plainly marked as such, and must not
58 * be misrepresented as being the original software.
60 **** Alterations to Henry's code are...
62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
66 **** You may distribute under the terms of either the GNU General Public
67 **** License or the Artistic License, as specified in the README file.
69 * Beware that some of this code is subtly aware of the way operator
70 * precedence is structured in regular expressions. Serious changes in
71 * regular-expression syntax might require a total rethink.
74 #define PERL_IN_REGEXEC_C
77 #ifdef PERL_IN_XSUB_RE
83 #include "invlist_inline.h"
84 #include "unicode_constants.h"
86 static const char b_utf8_locale_required[] =
87 "Use of \\b{} or \\B{} for non-UTF-8 locale is wrong."
88 " Assuming a UTF-8 locale";
90 #define CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_BOUND \
92 if (! IN_UTF8_CTYPE_LOCALE) { \
93 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), \
94 b_utf8_locale_required); \
98 static const char sets_utf8_locale_required[] =
99 "Use of (?[ ]) for non-UTF-8 locale is wrong. Assuming a UTF-8 locale";
101 #define CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_SETS(n) \
103 if (! IN_UTF8_CTYPE_LOCALE && ANYOFL_UTF8_LOCALE_REQD(FLAGS(n))) { \
104 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), \
105 sets_utf8_locale_required); \
110 /* At least one required character in the target string is expressible only in
112 static const char non_utf8_target_but_utf8_required[]
113 = "Can't match, because target string needs to be in UTF-8\n";
116 #define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
117 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%s", non_utf8_target_but_utf8_required));\
122 #define STATIC static
129 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
131 #define HOPc(pos,off) \
132 (char *)(reginfo->is_utf8_target \
133 ? reghop3((U8*)pos, off, \
134 (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
137 /* like HOPMAYBE3 but backwards. lim must be +ve. Returns NULL on overshoot */
138 #define HOPBACK3(pos, off, lim) \
139 (reginfo->is_utf8_target \
140 ? reghopmaybe3((U8*)pos, (SSize_t)0-off, (U8*)(lim)) \
141 : (pos - off >= lim) \
145 #define HOPBACKc(pos, off) ((char*)HOPBACK3(pos, off, reginfo->strbeg))
147 #define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
148 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
150 /* lim must be +ve. Returns NULL on overshoot */
151 #define HOPMAYBE3(pos,off,lim) \
152 (reginfo->is_utf8_target \
153 ? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \
154 : ((U8*)pos + off <= lim) \
158 /* like HOP3, but limits the result to <= lim even for the non-utf8 case.
159 * off must be >=0; args should be vars rather than expressions */
160 #define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
161 ? reghop3((U8*)(pos), off, (U8*)(lim)) \
162 : (U8*)((pos + off) > lim ? lim : (pos + off)))
163 #define HOP3clim(pos,off,lim) ((char*)HOP3lim(pos,off,lim))
165 #define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
166 ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
168 #define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
170 #define PLACEHOLDER /* Something for the preprocessor to grab onto */
171 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
173 /* for use after a quantifier and before an EXACT-like node -- japhy */
174 /* it would be nice to rework regcomp.sym to generate this stuff. sigh
176 * NOTE that *nothing* that affects backtracking should be in here, specifically
177 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
178 * node that is in between two EXACT like nodes when ascertaining what the required
179 * "follow" character is. This should probably be moved to regex compile time
180 * although it may be done at run time beause of the REF possibility - more
181 * investigation required. -- demerphq
183 #define JUMPABLE(rn) ( \
185 (OP(rn) == CLOSE && \
186 !EVAL_CLOSE_PAREN_IS(cur_eval,ARG(rn)) ) || \
188 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
189 OP(rn) == PLUS || OP(rn) == MINMOD || \
191 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
193 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
195 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
198 Search for mandatory following text node; for lookahead, the text must
199 follow but for lookbehind (rn->flags != 0) we skip to the next step.
201 #define FIND_NEXT_IMPT(rn) STMT_START { \
202 while (JUMPABLE(rn)) { \
203 const OPCODE type = OP(rn); \
204 if (type == SUSPEND || PL_regkind[type] == CURLY) \
205 rn = NEXTOPER(NEXTOPER(rn)); \
206 else if (type == PLUS) \
208 else if (type == IFMATCH) \
209 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
210 else rn += NEXT_OFF(rn); \
214 #define SLAB_FIRST(s) (&(s)->states[0])
215 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
217 static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
218 static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
219 static regmatch_state * S_push_slab(pTHX);
221 #define REGCP_PAREN_ELEMS 3
222 #define REGCP_OTHER_ELEMS 3
223 #define REGCP_FRAME_ELEMS 1
224 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
225 * are needed for the regexp context stack bookkeeping. */
228 S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen _pDEPTH)
230 const int retval = PL_savestack_ix;
231 const int paren_elems_to_push =
232 (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS;
233 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
234 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
236 DECLARE_AND_GET_RE_DEBUG_FLAGS;
238 PERL_ARGS_ASSERT_REGCPPUSH;
240 if (paren_elems_to_push < 0)
241 Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %u",
242 (int)paren_elems_to_push, (int)maxopenparen,
243 (int)parenfloor, (unsigned)REGCP_PAREN_ELEMS);
245 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
246 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %" UVuf
247 " out of range (%lu-%ld)",
249 (unsigned long)maxopenparen,
252 SSGROW(total_elems + REGCP_FRAME_ELEMS);
255 if ((int)maxopenparen > (int)parenfloor)
256 Perl_re_exec_indentf( aTHX_
257 "rex=0x%" UVxf " offs=0x%" UVxf ": saving capture indices:\n",
263 for (p = parenfloor+1; p <= (I32)maxopenparen; p++) {
264 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
265 SSPUSHIV(rex->offs[p].end);
266 SSPUSHIV(rex->offs[p].start);
267 SSPUSHINT(rex->offs[p].start_tmp);
268 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
269 " \\%" UVuf ": %" IVdf "(%" IVdf ")..%" IVdf "\n",
272 (IV)rex->offs[p].start,
273 (IV)rex->offs[p].start_tmp,
277 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
278 SSPUSHINT(maxopenparen);
279 SSPUSHINT(rex->lastparen);
280 SSPUSHINT(rex->lastcloseparen);
281 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
286 /* These are needed since we do not localize EVAL nodes: */
287 #define REGCP_SET(cp) \
289 Perl_re_exec_indentf( aTHX_ \
290 "Setting an EVAL scope, savestack=%" IVdf ",\n", \
291 depth, (IV)PL_savestack_ix \
296 #define REGCP_UNWIND(cp) \
298 if (cp != PL_savestack_ix) \
299 Perl_re_exec_indentf( aTHX_ \
300 "Clearing an EVAL scope, savestack=%" \
301 IVdf "..%" IVdf "\n", \
302 depth, (IV)(cp), (IV)PL_savestack_ix \
307 /* set the start and end positions of capture ix */
308 #define CLOSE_CAPTURE(ix, s, e) \
309 rex->offs[ix].start = s; \
310 rex->offs[ix].end = e; \
311 if (ix > rex->lastparen) \
312 rex->lastparen = ix; \
313 rex->lastcloseparen = ix; \
314 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_ \
315 "CLOSE: rex=0x%" UVxf " offs=0x%" UVxf ": \\%" UVuf ": set %" IVdf "..%" IVdf " max: %" UVuf "\n", \
320 (IV)rex->offs[ix].start, \
321 (IV)rex->offs[ix].end, \
325 #define UNWIND_PAREN(lp, lcp) \
326 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_ \
327 "UNWIND_PAREN: rex=0x%" UVxf " offs=0x%" UVxf ": invalidate (%" UVuf "..%" UVuf "] set lcp: %" UVuf "\n", \
332 (UV)(rex->lastparen), \
335 for (n = rex->lastparen; n > lp; n--) \
336 rex->offs[n].end = -1; \
337 rex->lastparen = n; \
338 rex->lastcloseparen = lcp;
342 S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p _pDEPTH)
346 DECLARE_AND_GET_RE_DEBUG_FLAGS;
348 PERL_ARGS_ASSERT_REGCPPOP;
350 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
352 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
353 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
354 rex->lastcloseparen = SSPOPINT;
355 rex->lastparen = SSPOPINT;
356 *maxopenparen_p = SSPOPINT;
358 i -= REGCP_OTHER_ELEMS;
359 /* Now restore the parentheses context. */
361 if (i || rex->lastparen + 1 <= rex->nparens)
362 Perl_re_exec_indentf( aTHX_
363 "rex=0x%" UVxf " offs=0x%" UVxf ": restoring capture indices to:\n",
369 paren = *maxopenparen_p;
370 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
372 rex->offs[paren].start_tmp = SSPOPINT;
373 rex->offs[paren].start = SSPOPIV;
375 if (paren <= rex->lastparen)
376 rex->offs[paren].end = tmps;
377 DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
378 " \\%" UVuf ": %" IVdf "(%" IVdf ")..%" IVdf "%s\n",
381 (IV)rex->offs[paren].start,
382 (IV)rex->offs[paren].start_tmp,
383 (IV)rex->offs[paren].end,
384 (paren > rex->lastparen ? "(skipped)" : ""));
389 /* It would seem that the similar code in regtry()
390 * already takes care of this, and in fact it is in
391 * a better location to since this code can #if 0-ed out
392 * but the code in regtry() is needed or otherwise tests
393 * requiring null fields (pat.t#187 and split.t#{13,14}
394 * (as of patchlevel 7877) will fail. Then again,
395 * this code seems to be necessary or otherwise
396 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
397 * --jhi updated by dapm */
398 for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
399 if (i > *maxopenparen_p)
400 rex->offs[i].start = -1;
401 rex->offs[i].end = -1;
402 DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
403 " \\%" UVuf ": %s ..-1 undeffing\n",
406 (i > *maxopenparen_p) ? "-1" : " "
412 /* restore the parens and associated vars at savestack position ix,
413 * but without popping the stack */
416 S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p _pDEPTH)
418 I32 tmpix = PL_savestack_ix;
419 PERL_ARGS_ASSERT_REGCP_RESTORE;
421 PL_savestack_ix = ix;
422 regcppop(rex, maxopenparen_p);
423 PL_savestack_ix = tmpix;
426 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
429 S_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
431 /* Returns a boolean as to whether or not 'character' is a member of the
432 * Posix character class given by 'classnum' that should be equivalent to a
433 * value in the typedef 'char_class_number_'.
435 * Ideally this could be replaced by a just an array of function pointers
436 * to the C library functions that implement the macros this calls.
437 * However, to compile, the precise function signatures are required, and
438 * these may vary from platform to platform. To avoid having to figure
439 * out what those all are on each platform, I (khw) am using this method,
440 * which adds an extra layer of function call overhead (unless the C
441 * optimizer strips it away). But we don't particularly care about
442 * performance with locales anyway. */
444 if (IN_UTF8_CTYPE_LOCALE) {
445 return cBOOL(generic_isCC_(character, classnum));
448 switch ((char_class_number_) classnum) {
449 case CC_ENUM_ALPHANUMERIC_: return isU8_ALPHANUMERIC_LC(character);
450 case CC_ENUM_ALPHA_: return isU8_ALPHA_LC(character);
451 case CC_ENUM_ASCII_: return isU8_ASCII_LC(character);
452 case CC_ENUM_BLANK_: return isU8_BLANK_LC(character);
453 case CC_ENUM_CASED_: return isU8_CASED_LC(character);
454 case CC_ENUM_CNTRL_: return isU8_CNTRL_LC(character);
455 case CC_ENUM_DIGIT_: return isU8_DIGIT_LC(character);
456 case CC_ENUM_GRAPH_: return isU8_GRAPH_LC(character);
457 case CC_ENUM_LOWER_: return isU8_LOWER_LC(character);
458 case CC_ENUM_PRINT_: return isU8_PRINT_LC(character);
459 case CC_ENUM_PUNCT_: return isU8_PUNCT_LC(character);
460 case CC_ENUM_SPACE_: return isU8_SPACE_LC(character);
461 case CC_ENUM_UPPER_: return isU8_UPPER_LC(character);
462 case CC_ENUM_WORDCHAR_: return isU8_WORDCHAR_LC(character);
463 case CC_ENUM_XDIGIT_: return isU8_XDIGIT_LC(character);
464 default: /* VERTSPACE should never occur in locales */
469 "panic: isFOO_lc() has an unexpected character class '%d'",
472 NOT_REACHED; /* NOTREACHED */
476 PERL_STATIC_INLINE I32
477 S_foldEQ_latin1_s2_folded(pTHX_ const char *s1, const char *s2, I32 len)
479 /* Compare non-UTF-8 using Unicode (Latin1) semantics. s2 must already be
480 * folded. Works on all folds representable without UTF-8, except for
481 * LATIN_SMALL_LETTER_SHARP_S, and does not check for this. Nor does it
482 * check that the strings each have at least 'len' characters.
484 * There is almost an identical API function where s2 need not be folded:
485 * Perl_foldEQ_latin1() */
487 const U8 *a = (const U8 *)s1;
488 const U8 *b = (const U8 *)s2;
490 PERL_ARGS_ASSERT_FOLDEQ_LATIN1_S2_FOLDED;
495 assert(! isUPPER_L1(*b));
496 if (toLOWER_L1(*a) != *b) {
505 S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character, const U8* e)
507 /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
508 * 'character' is a member of the Posix character class given by 'classnum'
509 * that should be equivalent to a value in the typedef
510 * 'char_class_number_'.
512 * This just calls isFOO_lc on the code point for the character if it is in
513 * the range 0-255. Outside that range, all characters use Unicode
514 * rules, ignoring any locale. So use the Unicode function if this class
515 * requires an inversion list, and use the Unicode macro otherwise. */
518 PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
520 if (UTF8_IS_INVARIANT(*character)) {
521 return isFOO_lc(classnum, *character);
523 else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
524 return isFOO_lc(classnum,
525 EIGHT_BIT_UTF8_TO_NATIVE(*character, *(character + 1)));
528 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(character, e);
530 switch ((char_class_number_) classnum) {
531 case CC_ENUM_SPACE_: return is_XPERLSPACE_high(character);
532 case CC_ENUM_BLANK_: return is_HORIZWS_high(character);
533 case CC_ENUM_XDIGIT_: return is_XDIGIT_high(character);
534 case CC_ENUM_VERTSPACE_: return is_VERTWS_high(character);
536 return _invlist_contains_cp(PL_XPosix_ptrs[classnum],
537 utf8_to_uvchr_buf(character, e, NULL));
540 return FALSE; /* Things like CNTRL are always below 256 */
544 S_find_span_end(U8 * s, const U8 * send, const U8 span_byte)
546 /* Returns the position of the first byte in the sequence between 's' and
547 * 'send-1' inclusive that isn't 'span_byte'; returns 'send' if none found.
550 PERL_ARGS_ASSERT_FIND_SPAN_END;
554 if ((STRLEN) (send - s) >= PERL_WORDSIZE
555 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
556 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
558 PERL_UINTMAX_T span_word;
560 /* Process per-byte until reach word boundary. XXX This loop could be
561 * eliminated if we knew that this platform had fast unaligned reads */
562 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
563 if (*s != span_byte) {
569 /* Create a word filled with the bytes we are spanning */
570 span_word = PERL_COUNT_MULTIPLIER * span_byte;
572 /* Process per-word as long as we have at least a full word left */
575 /* Keep going if the whole word is composed of 'span_byte's */
576 if ((* (PERL_UINTMAX_T *) s) == span_word) {
581 /* Here, at least one byte in the word isn't 'span_byte'. */
589 /* This xor leaves 1 bits only in those non-matching bytes */
590 span_word ^= * (PERL_UINTMAX_T *) s;
592 /* Make sure the upper bit of each non-matching byte is set. This
593 * makes each such byte look like an ASCII platform variant byte */
594 span_word |= span_word << 1;
595 span_word |= span_word << 2;
596 span_word |= span_word << 4;
598 /* That reduces the problem to what this function solves */
599 return s + variant_byte_number(span_word);
603 } while (s + PERL_WORDSIZE <= send);
606 /* Process the straggler bytes beyond the final word boundary */
608 if (*s != span_byte) {
618 S_find_next_masked(U8 * s, const U8 * send, const U8 byte, const U8 mask)
620 /* Returns the position of the first byte in the sequence between 's'
621 * and 'send-1' inclusive that when ANDed with 'mask' yields 'byte';
622 * returns 'send' if none found. It uses word-level operations instead of
623 * byte to speed up the process */
625 PERL_ARGS_ASSERT_FIND_NEXT_MASKED;
628 assert((byte & mask) == byte);
632 if ((STRLEN) (send - s) >= PERL_WORDSIZE
633 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
634 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
636 PERL_UINTMAX_T word, mask_word;
638 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
639 if (((*s) & mask) == byte) {
645 word = PERL_COUNT_MULTIPLIER * byte;
646 mask_word = PERL_COUNT_MULTIPLIER * mask;
649 PERL_UINTMAX_T masked = (* (PERL_UINTMAX_T *) s) & mask_word;
651 /* If 'masked' contains bytes with the bit pattern of 'byte' within
652 * it, xoring with 'word' will leave each of the 8 bits in such
653 * bytes be 0, and no byte containing any other bit pattern will be
657 /* This causes the most significant bit to be set to 1 for any
658 * bytes in the word that aren't completely 0 */
659 masked |= masked << 1;
660 masked |= masked << 2;
661 masked |= masked << 4;
663 /* The msbits are the same as what marks a byte as variant, so we
664 * can use this mask. If all msbits are 1, the word doesn't
666 if ((masked & PERL_VARIANTS_WORD_MASK) == PERL_VARIANTS_WORD_MASK) {
671 /* Here, the msbit of bytes in the word that aren't 'byte' are 1,
672 * and any that are, are 0. Complement and re-AND to swap that */
674 masked &= PERL_VARIANTS_WORD_MASK;
676 /* This reduces the problem to that solved by this function */
677 s += variant_byte_number(masked);
680 } while (s + PERL_WORDSIZE <= send);
686 if (((*s) & mask) == byte) {
696 S_find_span_end_mask(U8 * s, const U8 * send, const U8 span_byte, const U8 mask)
698 /* Returns the position of the first byte in the sequence between 's' and
699 * 'send-1' inclusive that when ANDed with 'mask' isn't 'span_byte'.
700 * 'span_byte' should have been ANDed with 'mask' in the call of this
701 * function. Returns 'send' if none found. Works like find_span_end(),
702 * except for the AND */
704 PERL_ARGS_ASSERT_FIND_SPAN_END_MASK;
707 assert((span_byte & mask) == span_byte);
709 if ((STRLEN) (send - s) >= PERL_WORDSIZE
710 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
711 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
713 PERL_UINTMAX_T span_word, mask_word;
715 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
716 if (((*s) & mask) != span_byte) {
722 span_word = PERL_COUNT_MULTIPLIER * span_byte;
723 mask_word = PERL_COUNT_MULTIPLIER * mask;
726 PERL_UINTMAX_T masked = (* (PERL_UINTMAX_T *) s) & mask_word;
728 if (masked == span_word) {
740 masked |= masked << 1;
741 masked |= masked << 2;
742 masked |= masked << 4;
743 return s + variant_byte_number(masked);
747 } while (s + PERL_WORDSIZE <= send);
751 if (((*s) & mask) != span_byte) {
761 * pregexec and friends
764 #ifndef PERL_IN_XSUB_RE
766 - pregexec - match a regexp against a string
769 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
770 char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
771 /* stringarg: the point in the string at which to begin matching */
772 /* strend: pointer to null at end of string */
773 /* strbeg: real beginning of string */
774 /* minend: end of match must be >= minend bytes after stringarg. */
775 /* screamer: SV being matched: only used for utf8 flag, pos() etc; string
776 * itself is accessed via the pointers above */
777 /* nosave: For optimizations. */
779 PERL_ARGS_ASSERT_PREGEXEC;
782 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
783 nosave ? 0 : REXEC_COPY_STR);
789 /* re_intuit_start():
791 * Based on some optimiser hints, try to find the earliest position in the
792 * string where the regex could match.
794 * rx: the regex to match against
795 * sv: the SV being matched: only used for utf8 flag; the string
796 * itself is accessed via the pointers below. Note that on
797 * something like an overloaded SV, SvPOK(sv) may be false
798 * and the string pointers may point to something unrelated to
800 * strbeg: real beginning of string
801 * strpos: the point in the string at which to begin matching
802 * strend: pointer to the byte following the last char of the string
803 * flags currently unused; set to 0
804 * data: currently unused; set to NULL
806 * The basic idea of re_intuit_start() is to use some known information
807 * about the pattern, namely:
809 * a) the longest known anchored substring (i.e. one that's at a
810 * constant offset from the beginning of the pattern; but not
811 * necessarily at a fixed offset from the beginning of the
813 * b) the longest floating substring (i.e. one that's not at a constant
814 * offset from the beginning of the pattern);
815 * c) Whether the pattern is anchored to the string; either
816 * an absolute anchor: /^../, or anchored to \n: /^.../m,
817 * or anchored to pos(): /\G/;
818 * d) A start class: a real or synthetic character class which
819 * represents which characters are legal at the start of the pattern;
821 * to either quickly reject the match, or to find the earliest position
822 * within the string at which the pattern might match, thus avoiding
823 * running the full NFA engine at those earlier locations, only to
824 * eventually fail and retry further along.
826 * Returns NULL if the pattern can't match, or returns the address within
827 * the string which is the earliest place the match could occur.
829 * The longest of the anchored and floating substrings is called 'check'
830 * and is checked first. The other is called 'other' and is checked
831 * second. The 'other' substring may not be present. For example,
833 * /(abc|xyz)ABC\d{0,3}DEFG/
837 * check substr (float) = "DEFG", offset 6..9 chars
838 * other substr (anchored) = "ABC", offset 3..3 chars
841 * Be aware that during the course of this function, sometimes 'anchored'
842 * refers to a substring being anchored relative to the start of the
843 * pattern, and sometimes to the pattern itself being anchored relative to
844 * the string. For example:
846 * /\dabc/: "abc" is anchored to the pattern;
847 * /^\dabc/: "abc" is anchored to the pattern and the string;
848 * /\d+abc/: "abc" is anchored to neither the pattern nor the string;
849 * /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
850 * but the pattern is anchored to the string.
854 Perl_re_intuit_start(pTHX_
857 const char * const strbeg,
861 re_scream_pos_data *data)
863 struct regexp *const prog = ReANY(rx);
864 SSize_t start_shift = prog->check_offset_min;
865 /* Should be nonnegative! */
866 SSize_t end_shift = 0;
867 /* current lowest pos in string where the regex can start matching */
868 char *rx_origin = strpos;
870 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
871 U8 other_ix = 1 - prog->substrs->check_ix;
873 char *other_last = strpos;/* latest pos 'other' substr already checked to */
874 char *check_at = NULL; /* check substr found at this pos */
875 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
876 RXi_GET_DECL(prog,progi);
877 regmatch_info reginfo_buf; /* create some info to pass to find_byclass */
878 regmatch_info *const reginfo = ®info_buf;
879 DECLARE_AND_GET_RE_DEBUG_FLAGS;
881 PERL_ARGS_ASSERT_RE_INTUIT_START;
882 PERL_UNUSED_ARG(flags);
883 PERL_UNUSED_ARG(data);
885 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
886 "Intuit: trying to determine minimum start position...\n"));
888 /* for now, assume that all substr offsets are positive. If at some point
889 * in the future someone wants to do clever things with lookbehind and
890 * -ve offsets, they'll need to fix up any code in this function
891 * which uses these offsets. See the thread beginning
892 * <20140113145929.GF27210@iabyn.com>
894 assert(prog->substrs->data[0].min_offset >= 0);
895 assert(prog->substrs->data[0].max_offset >= 0);
896 assert(prog->substrs->data[1].min_offset >= 0);
897 assert(prog->substrs->data[1].max_offset >= 0);
898 assert(prog->substrs->data[2].min_offset >= 0);
899 assert(prog->substrs->data[2].max_offset >= 0);
901 /* for now, assume that if both present, that the floating substring
902 * doesn't start before the anchored substring.
903 * If you break this assumption (e.g. doing better optimisations
904 * with lookahead/behind), then you'll need to audit the code in this
905 * function carefully first
908 ! ( (prog->anchored_utf8 || prog->anchored_substr)
909 && (prog->float_utf8 || prog->float_substr))
910 || (prog->float_min_offset >= prog->anchored_offset));
912 /* byte rather than char calculation for efficiency. It fails
913 * to quickly reject some cases that can't match, but will reject
914 * them later after doing full char arithmetic */
915 if (prog->minlen > strend - strpos) {
916 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
917 " String too short...\n"));
921 RXp_MATCH_UTF8_set(prog, utf8_target);
922 reginfo->is_utf8_target = cBOOL(utf8_target);
923 reginfo->info_aux = NULL;
924 reginfo->strbeg = strbeg;
925 reginfo->strend = strend;
926 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
928 /* not actually used within intuit, but zero for safety anyway */
929 reginfo->poscache_maxiter = 0;
932 if ((!prog->anchored_utf8 && prog->anchored_substr)
933 || (!prog->float_utf8 && prog->float_substr))
934 to_utf8_substr(prog);
935 check = prog->check_utf8;
937 if (!prog->check_substr && prog->check_utf8) {
938 if (! to_byte_substr(prog)) {
939 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
942 check = prog->check_substr;
945 /* dump the various substring data */
946 DEBUG_OPTIMISE_MORE_r({
948 for (i=0; i<=2; i++) {
949 SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr
950 : prog->substrs->data[i].substr);
954 Perl_re_printf( aTHX_
955 " substrs[%d]: min=%" IVdf " max=%" IVdf " end shift=%" IVdf
956 " useful=%" IVdf " utf8=%d [%s]\n",
958 (IV)prog->substrs->data[i].min_offset,
959 (IV)prog->substrs->data[i].max_offset,
960 (IV)prog->substrs->data[i].end_shift,
967 if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */
969 /* ml_anch: check after \n?
971 * A note about PREGf_IMPLICIT: on an un-anchored pattern beginning
972 * with /.*.../, these flags will have been added by the
974 * /.*abc/, /.*abc/m: PREGf_IMPLICIT | PREGf_ANCH_MBOL
975 * /.*abc/s: PREGf_IMPLICIT | PREGf_ANCH_SBOL
977 ml_anch = (prog->intflags & PREGf_ANCH_MBOL)
978 && !(prog->intflags & PREGf_IMPLICIT);
980 if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) {
981 /* we are only allowed to match at BOS or \G */
983 /* trivially reject if there's a BOS anchor and we're not at BOS.
985 * Note that we don't try to do a similar quick reject for
986 * \G, since generally the caller will have calculated strpos
987 * based on pos() and gofs, so the string is already correctly
988 * anchored by definition; and handling the exceptions would
989 * be too fiddly (e.g. REXEC_IGNOREPOS).
991 if ( strpos != strbeg
992 && (prog->intflags & PREGf_ANCH_SBOL))
994 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
995 " Not at start...\n"));
999 /* in the presence of an anchor, the anchored (relative to the
1000 * start of the regex) substr must also be anchored relative
1001 * to strpos. So quickly reject if substr isn't found there.
1002 * This works for \G too, because the caller will already have
1003 * subtracted gofs from pos, and gofs is the offset from the
1004 * \G to the start of the regex. For example, in /.abc\Gdef/,
1005 * where substr="abcdef", pos()=3, gofs=4, offset_min=1:
1006 * caller will have set strpos=pos()-4; we look for the substr
1007 * at position pos()-4+1, which lines up with the "a" */
1009 if (prog->check_offset_min == prog->check_offset_max) {
1010 /* Substring at constant offset from beg-of-str... */
1011 SSize_t slen = SvCUR(check);
1012 char *s = HOP3c(strpos, prog->check_offset_min, strend);
1014 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1015 " Looking for check substr at fixed offset %" IVdf "...\n",
1016 (IV)prog->check_offset_min));
1018 if (SvTAIL(check)) {
1019 /* In this case, the regex is anchored at the end too.
1020 * Unless it's a multiline match, the lengths must match
1021 * exactly, give or take a \n. NB: slen >= 1 since
1022 * the last char of check is \n */
1024 && ( strend - s > slen
1025 || strend - s < slen - 1
1026 || (strend - s == slen && strend[-1] != '\n')))
1028 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1029 " String too long...\n"));
1032 /* Now should match s[0..slen-2] */
1035 if (slen && (strend - s < slen
1036 || *SvPVX_const(check) != *s
1037 || (slen > 1 && (memNE(SvPVX_const(check), s, slen)))))
1039 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1040 " String not equal...\n"));
1045 goto success_at_start;
1050 end_shift = prog->check_end_shift;
1052 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
1054 Perl_croak(aTHX_ "panic: end_shift: %" IVdf " pattern:\n%s\n ",
1055 (IV)end_shift, RX_PRECOMP(rx));
1060 /* This is the (re)entry point of the main loop in this function.
1061 * The goal of this loop is to:
1062 * 1) find the "check" substring in the region rx_origin..strend
1063 * (adjusted by start_shift / end_shift). If not found, reject
1065 * 2) If it exists, look for the "other" substr too if defined; for
1066 * example, if the check substr maps to the anchored substr, then
1067 * check the floating substr, and vice-versa. If not found, go
1068 * back to (1) with rx_origin suitably incremented.
1069 * 3) If we find an rx_origin position that doesn't contradict
1070 * either of the substrings, then check the possible additional
1071 * constraints on rx_origin of /^.../m or a known start class.
1072 * If these fail, then depending on which constraints fail, jump
1073 * back to here, or to various other re-entry points further along
1074 * that skip some of the first steps.
1075 * 4) If we pass all those tests, update the BmUSEFUL() count on the
1076 * substring. If the start position was determined to be at the
1077 * beginning of the string - so, not rejected, but not optimised,
1078 * since we have to run regmatch from position 0 - decrement the
1079 * BmUSEFUL() count. Otherwise increment it.
1083 /* first, look for the 'check' substring */
1089 DEBUG_OPTIMISE_MORE_r({
1090 Perl_re_printf( aTHX_
1091 " At restart: rx_origin=%" IVdf " Check offset min: %" IVdf
1092 " Start shift: %" IVdf " End shift %" IVdf
1093 " Real end Shift: %" IVdf "\n",
1094 (IV)(rx_origin - strbeg),
1095 (IV)prog->check_offset_min,
1098 (IV)prog->check_end_shift);
1101 end_point = HOPBACK3(strend, end_shift, rx_origin);
1104 start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
1109 /* If the regex is absolutely anchored to either the start of the
1110 * string (SBOL) or to pos() (ANCH_GPOS), then
1111 * check_offset_max represents an upper bound on the string where
1112 * the substr could start. For the ANCH_GPOS case, we assume that
1113 * the caller of intuit will have already set strpos to
1114 * pos()-gofs, so in this case strpos + offset_max will still be
1115 * an upper bound on the substr.
1118 && prog->intflags & PREGf_ANCH
1119 && prog->check_offset_max != SSize_t_MAX)
1121 SSize_t check_len = SvCUR(check) - cBOOL(SvTAIL(check));
1122 const char * const anchor =
1123 (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg);
1124 SSize_t targ_len = (char*)end_point - anchor;
1126 if (check_len > targ_len) {
1127 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1128 "Target string too short to match required substring...\n"));
1132 /* do a bytes rather than chars comparison. It's conservative;
1133 * so it skips doing the HOP if the result can't possibly end
1134 * up earlier than the old value of end_point.
1136 assert(anchor + check_len <= (char *)end_point);
1137 if (prog->check_offset_max + check_len < targ_len) {
1138 end_point = HOP3lim((U8*)anchor,
1139 prog->check_offset_max,
1140 end_point - check_len
1143 if (end_point < start_point)
1148 check_at = fbm_instr( start_point, end_point,
1149 check, multiline ? FBMrf_MULTILINE : 0);
1151 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1152 " doing 'check' fbm scan, [%" IVdf "..%" IVdf "] gave %" IVdf "\n",
1153 (IV)((char*)start_point - strbeg),
1154 (IV)((char*)end_point - strbeg),
1155 (IV)(check_at ? check_at - strbeg : -1)
1158 /* Update the count-of-usability, remove useless subpatterns,
1162 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1163 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
1164 Perl_re_printf( aTHX_ " %s %s substr %s%s%s",
1165 (check_at ? "Found" : "Did not find"),
1166 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
1167 ? "anchored" : "floating"),
1170 (check_at ? " at offset " : "...\n") );
1175 /* set rx_origin to the minimum position where the regex could start
1176 * matching, given the constraint of the just-matched check substring.
1177 * But don't set it lower than previously.
1180 if (check_at - rx_origin > prog->check_offset_max)
1181 rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin);
1182 /* Finish the diagnostic message */
1183 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1184 "%ld (rx_origin now %" IVdf ")...\n",
1185 (long)(check_at - strbeg),
1186 (IV)(rx_origin - strbeg)
1191 /* now look for the 'other' substring if defined */
1193 if (prog->substrs->data[other_ix].utf8_substr
1194 || prog->substrs->data[other_ix].substr)
1196 /* Take into account the "other" substring. */
1200 struct reg_substr_datum *other;
1203 other = &prog->substrs->data[other_ix];
1204 if (!utf8_target && !other->substr) {
1205 if (!to_byte_substr(prog)) {
1206 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
1210 /* if "other" is anchored:
1211 * we've previously found a floating substr starting at check_at.
1212 * This means that the regex origin must lie somewhere
1213 * between min (rx_origin): HOP3(check_at, -check_offset_max)
1214 * and max: HOP3(check_at, -check_offset_min)
1215 * (except that min will be >= strpos)
1216 * So the fixed substr must lie somewhere between
1217 * HOP3(min, anchored_offset)
1218 * HOP3(max, anchored_offset) + SvCUR(substr)
1221 /* if "other" is floating
1222 * Calculate last1, the absolute latest point where the
1223 * floating substr could start in the string, ignoring any
1224 * constraints from the earlier fixed match. It is calculated
1227 * strend - prog->minlen (in chars) is the absolute latest
1228 * position within the string where the origin of the regex
1229 * could appear. The latest start point for the floating
1230 * substr is float_min_offset(*) on from the start of the
1231 * regex. last1 simply combines thee two offsets.
1233 * (*) You might think the latest start point should be
1234 * float_max_offset from the regex origin, and technically
1235 * you'd be correct. However, consider
1237 * Here, float min, max are 3,5 and minlen is 7.
1238 * This can match either
1242 * In the first case, the regex matches minlen chars; in the
1243 * second, minlen+1, in the third, minlen+2.
1244 * In the first case, the floating offset is 3 (which equals
1245 * float_min), in the second, 4, and in the third, 5 (which
1246 * equals float_max). In all cases, the floating string bcd
1247 * can never start more than 4 chars from the end of the
1248 * string, which equals minlen - float_min. As the substring
1249 * starts to match more than float_min from the start of the
1250 * regex, it makes the regex match more than minlen chars,
1251 * and the two cancel each other out. So we can always use
1252 * float_min - minlen, rather than float_max - minlen for the
1253 * latest position in the string.
1255 * Note that -minlen + float_min_offset is equivalent (AFAIKT)
1256 * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift
1259 assert(prog->minlen >= other->min_offset);
1260 last1 = HOP3c(strend,
1261 other->min_offset - prog->minlen, strbeg);
1263 if (other_ix) {/* i.e. if (other-is-float) */
1264 /* last is the latest point where the floating substr could
1265 * start, *given* any constraints from the earlier fixed
1266 * match. This constraint is that the floating string starts
1267 * <= float_max_offset chars from the regex origin (rx_origin).
1268 * If this value is less than last1, use it instead.
1270 assert(rx_origin <= last1);
1272 /* this condition handles the offset==infinity case, and
1273 * is a short-cut otherwise. Although it's comparing a
1274 * byte offset to a char length, it does so in a safe way,
1275 * since 1 char always occupies 1 or more bytes,
1276 * so if a string range is (last1 - rx_origin) bytes,
1277 * it will be less than or equal to (last1 - rx_origin)
1278 * chars; meaning it errs towards doing the accurate HOP3
1279 * rather than just using last1 as a short-cut */
1280 (last1 - rx_origin) < other->max_offset
1282 : (char*)HOP3lim(rx_origin, other->max_offset, last1);
1285 assert(strpos + start_shift <= check_at);
1286 last = HOP4c(check_at, other->min_offset - start_shift,
1290 s = HOP3c(rx_origin, other->min_offset, strend);
1291 if (s < other_last) /* These positions already checked */
1294 must = utf8_target ? other->utf8_substr : other->substr;
1295 assert(SvPOK(must));
1298 char *to = last + SvCUR(must) - (SvTAIL(must)!=0);
1304 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1305 " skipping 'other' fbm scan: %" IVdf " > %" IVdf "\n",
1306 (IV)(from - strbeg),
1312 (unsigned char*)from,
1315 multiline ? FBMrf_MULTILINE : 0
1317 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1318 " doing 'other' fbm scan, [%" IVdf "..%" IVdf "] gave %" IVdf "\n",
1319 (IV)(from - strbeg),
1321 (IV)(s ? s - strbeg : -1)
1327 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1328 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
1329 Perl_re_printf( aTHX_ " %s %s substr %s%s",
1330 s ? "Found" : "Contradicts",
1331 other_ix ? "floating" : "anchored",
1332 quoted, RE_SV_TAIL(must));
1337 /* last1 is latest possible substr location. If we didn't
1338 * find it before there, we never will */
1339 if (last >= last1) {
1340 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1341 "; giving up...\n"));
1345 /* try to find the check substr again at a later
1346 * position. Maybe next time we'll find the "other" substr
1348 other_last = HOP3c(last, 1, strend) /* highest failure */;
1350 other_ix /* i.e. if other-is-float */
1351 ? HOP3c(rx_origin, 1, strend)
1352 : HOP4c(last, 1 - other->min_offset, strbeg, strend);
1353 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1354 "; about to retry %s at offset %ld (rx_origin now %" IVdf ")...\n",
1355 (other_ix ? "floating" : "anchored"),
1356 (long)(HOP3c(check_at, 1, strend) - strbeg),
1357 (IV)(rx_origin - strbeg)
1362 if (other_ix) { /* if (other-is-float) */
1363 /* other_last is set to s, not s+1, since its possible for
1364 * a floating substr to fail first time, then succeed
1365 * second time at the same floating position; e.g.:
1366 * "-AB--AABZ" =~ /\wAB\d*Z/
1367 * The first time round, anchored and float match at
1368 * "-(AB)--AAB(Z)" then fail on the initial \w character
1369 * class. Second time round, they match at "-AB--A(AB)(Z)".
1374 rx_origin = HOP3c(s, -other->min_offset, strbeg);
1375 other_last = HOP3c(s, 1, strend);
1377 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1378 " at offset %ld (rx_origin now %" IVdf ")...\n",
1380 (IV)(rx_origin - strbeg)
1386 DEBUG_OPTIMISE_MORE_r(
1387 Perl_re_printf( aTHX_
1388 " Check-only match: offset min:%" IVdf " max:%" IVdf
1389 " check_at:%" IVdf " rx_origin:%" IVdf " rx_origin-check_at:%" IVdf
1390 " strend:%" IVdf "\n",
1391 (IV)prog->check_offset_min,
1392 (IV)prog->check_offset_max,
1393 (IV)(check_at-strbeg),
1394 (IV)(rx_origin-strbeg),
1395 (IV)(rx_origin-check_at),
1401 postprocess_substr_matches:
1403 /* handle the extra constraint of /^.../m if present */
1405 if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') {
1408 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1409 " looking for /^/m anchor"));
1411 /* we have failed the constraint of a \n before rx_origin.
1412 * Find the next \n, if any, even if it's beyond the current
1413 * anchored and/or floating substrings. Whether we should be
1414 * scanning ahead for the next \n or the next substr is debatable.
1415 * On the one hand you'd expect rare substrings to appear less
1416 * often than \n's. On the other hand, searching for \n means
1417 * we're effectively flipping between check_substr and "\n" on each
1418 * iteration as the current "rarest" candidate string, which
1419 * means for example that we'll quickly reject the whole string if
1420 * hasn't got a \n, rather than trying every substr position
1424 s = HOP3c(strend, - prog->minlen, strpos);
1425 if (s <= rx_origin ||
1426 ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin)))
1428 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1429 " Did not find /%s^%s/m...\n",
1430 PL_colors[0], PL_colors[1]));
1434 /* earliest possible origin is 1 char after the \n.
1435 * (since *rx_origin == '\n', it's safe to ++ here rather than
1436 * HOP(rx_origin, 1)) */
1439 if (prog->substrs->check_ix == 0 /* check is anchored */
1440 || rx_origin >= HOP3c(check_at, - prog->check_offset_min, strpos))
1442 /* Position contradicts check-string; either because
1443 * check was anchored (and thus has no wiggle room),
1444 * or check was float and rx_origin is above the float range */
1445 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1446 " Found /%s^%s/m, about to restart lookup for check-string with rx_origin %ld...\n",
1447 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
1451 /* if we get here, the check substr must have been float,
1452 * is in range, and we may or may not have had an anchored
1453 * "other" substr which still contradicts */
1454 assert(prog->substrs->check_ix); /* check is float */
1456 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
1457 /* whoops, the anchored "other" substr exists, so we still
1458 * contradict. On the other hand, the float "check" substr
1459 * didn't contradict, so just retry the anchored "other"
1461 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1462 " Found /%s^%s/m, rescanning for anchored from offset %" IVdf " (rx_origin now %" IVdf ")...\n",
1463 PL_colors[0], PL_colors[1],
1464 (IV)(rx_origin - strbeg + prog->anchored_offset),
1465 (IV)(rx_origin - strbeg)
1467 goto do_other_substr;
1470 /* success: we don't contradict the found floating substring
1471 * (and there's no anchored substr). */
1472 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1473 " Found /%s^%s/m with rx_origin %ld...\n",
1474 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
1477 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1478 " (multiline anchor test skipped)\n"));
1484 /* if we have a starting character class, then test that extra constraint.
1485 * (trie stclasses are too expensive to use here, we are better off to
1486 * leave it to regmatch itself) */
1488 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1489 const U8* const str = (U8*)STRING(progi->regstclass);
1491 /* XXX this value could be pre-computed */
1492 const SSize_t cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1493 ? (reginfo->is_utf8_pat
1494 ? (SSize_t)utf8_distance(str + STR_LEN(progi->regstclass), str)
1495 : (SSize_t)STR_LEN(progi->regstclass))
1499 /* latest pos that a matching float substr constrains rx start to */
1500 char *rx_max_float = NULL;
1502 /* if the current rx_origin is anchored, either by satisfying an
1503 * anchored substring constraint, or a /^.../m constraint, then we
1504 * can reject the current origin if the start class isn't found
1505 * at the current position. If we have a float-only match, then
1506 * rx_origin is constrained to a range; so look for the start class
1507 * in that range. if neither, then look for the start class in the
1508 * whole rest of the string */
1510 /* XXX DAPM it's not clear what the minlen test is for, and why
1511 * it's not used in the floating case. Nothing in the test suite
1512 * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>.
1513 * Here are some old comments, which may or may not be correct:
1515 * minlen == 0 is possible if regstclass is \b or \B,
1516 * and the fixed substr is ''$.
1517 * Since minlen is already taken into account, rx_origin+1 is
1518 * before strend; accidentally, minlen >= 1 guaranties no false
1519 * positives at rx_origin + 1 even for \b or \B. But (minlen? 1 :
1520 * 0) below assumes that regstclass does not come from lookahead...
1521 * If regstclass takes bytelength more than 1: If charlength==1, OK.
1522 * This leaves EXACTF-ish only, which are dealt with in
1526 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1527 endpos = HOP3clim(rx_origin, (prog->minlen ? cl_l : 0), strend);
1528 else if (prog->float_substr || prog->float_utf8) {
1529 rx_max_float = HOP3c(check_at, -start_shift, strbeg);
1530 endpos = HOP3clim(rx_max_float, cl_l, strend);
1535 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1536 " looking for class: start_shift: %" IVdf " check_at: %" IVdf
1537 " rx_origin: %" IVdf " endpos: %" IVdf "\n",
1538 (IV)start_shift, (IV)(check_at - strbeg),
1539 (IV)(rx_origin - strbeg), (IV)(endpos - strbeg)));
1541 s = find_byclass(prog, progi->regstclass, rx_origin, endpos,
1544 if (endpos == strend) {
1545 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1546 " Could not match STCLASS...\n") );
1549 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1550 " This position contradicts STCLASS...\n") );
1551 if ((prog->intflags & PREGf_ANCH) && !ml_anch
1552 && !(prog->intflags & PREGf_IMPLICIT))
1555 /* Contradict one of substrings */
1556 if (prog->anchored_substr || prog->anchored_utf8) {
1557 if (prog->substrs->check_ix == 1) { /* check is float */
1558 /* Have both, check_string is floating */
1559 assert(rx_origin + start_shift <= check_at);
1560 if (rx_origin + start_shift != check_at) {
1561 /* not at latest position float substr could match:
1562 * Recheck anchored substring, but not floating.
1563 * The condition above is in bytes rather than
1564 * chars for efficiency. It's conservative, in
1565 * that it errs on the side of doing 'goto
1566 * do_other_substr'. In this case, at worst,
1567 * an extra anchored search may get done, but in
1568 * practice the extra fbm_instr() is likely to
1569 * get skipped anyway. */
1570 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1571 " about to retry anchored at offset %ld (rx_origin now %" IVdf ")...\n",
1572 (long)(other_last - strbeg),
1573 (IV)(rx_origin - strbeg)
1575 goto do_other_substr;
1583 /* In the presence of ml_anch, we might be able to
1584 * find another \n without breaking the current float
1587 /* strictly speaking this should be HOP3c(..., 1, ...),
1588 * but since we goto a block of code that's going to
1589 * search for the next \n if any, its safe here */
1591 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1592 " about to look for /%s^%s/m starting at rx_origin %ld...\n",
1593 PL_colors[0], PL_colors[1],
1594 (long)(rx_origin - strbeg)) );
1595 goto postprocess_substr_matches;
1598 /* strictly speaking this can never be true; but might
1599 * be if we ever allow intuit without substrings */
1600 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
1603 rx_origin = rx_max_float;
1606 /* at this point, any matching substrings have been
1607 * contradicted. Start again... */
1609 rx_origin = HOP3c(rx_origin, 1, strend);
1611 /* uses bytes rather than char calculations for efficiency.
1612 * It's conservative: it errs on the side of doing 'goto restart',
1613 * where there is code that does a proper char-based test */
1614 if (rx_origin + start_shift + end_shift > strend) {
1615 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1616 " Could not match STCLASS...\n") );
1619 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1620 " about to look for %s substr starting at offset %ld (rx_origin now %" IVdf ")...\n",
1621 (prog->substrs->check_ix ? "floating" : "anchored"),
1622 (long)(rx_origin + start_shift - strbeg),
1623 (IV)(rx_origin - strbeg)
1630 if (rx_origin != s) {
1631 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1632 " By STCLASS: moving %ld --> %ld\n",
1633 (long)(rx_origin - strbeg), (long)(s - strbeg))
1637 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1638 " Does not contradict STCLASS...\n");
1643 /* Decide whether using the substrings helped */
1645 if (rx_origin != strpos) {
1646 /* Fixed substring is found far enough so that the match
1647 cannot start at strpos. */
1649 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ " try at offset...\n"));
1650 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
1653 /* The found rx_origin position does not prohibit matching at
1654 * strpos, so calling intuit didn't gain us anything. Decrement
1655 * the BmUSEFUL() count on the check substring, and if we reach
1657 if (!(prog->intflags & PREGf_NAUGHTY)
1659 prog->check_utf8 /* Could be deleted already */
1660 && --BmUSEFUL(prog->check_utf8) < 0
1661 && (prog->check_utf8 == prog->float_utf8)
1663 prog->check_substr /* Could be deleted already */
1664 && --BmUSEFUL(prog->check_substr) < 0
1665 && (prog->check_substr == prog->float_substr)
1668 /* If flags & SOMETHING - do not do it many times on the same match */
1669 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ " ... Disabling check substring...\n"));
1670 /* XXX Does the destruction order has to change with utf8_target? */
1671 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1672 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1673 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1674 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1675 check = NULL; /* abort */
1676 /* XXXX This is a remnant of the old implementation. It
1677 looks wasteful, since now INTUIT can use many
1678 other heuristics. */
1679 prog->extflags &= ~RXf_USE_INTUIT;
1683 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1684 "Intuit: %sSuccessfully guessed:%s match at offset %ld\n",
1685 PL_colors[4], PL_colors[5], (long)(rx_origin - strbeg)) );
1689 fail_finish: /* Substring not found */
1690 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1691 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1693 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%sMatch rejected by optimizer%s\n",
1694 PL_colors[4], PL_colors[5]));
1699 #define DECL_TRIE_TYPE(scan) \
1700 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \
1701 trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold, \
1702 trie_utf8l, trie_flu8, trie_flu8_latin } \
1703 trie_type = ((scan->flags == EXACT) \
1704 ? (utf8_target ? trie_utf8 : trie_plain) \
1705 : (scan->flags == EXACTL) \
1706 ? (utf8_target ? trie_utf8l : trie_plain) \
1707 : (scan->flags == EXACTFAA) \
1709 ? trie_utf8_exactfa_fold \
1710 : trie_latin_utf8_exactfa_fold) \
1711 : (scan->flags == EXACTFLU8 \
1714 : trie_flu8_latin) \
1717 : trie_latin_utf8_fold)))
1719 /* 'uscan' is set to foldbuf, and incremented, so below the end of uscan is
1720 * 'foldbuf+sizeof(foldbuf)' */
1721 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uc_end, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \
1724 U8 flags = FOLD_FLAGS_FULL; \
1725 switch (trie_type) { \
1727 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
1728 if (UTF8_IS_ABOVE_LATIN1(*uc)) { \
1729 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc_end); \
1731 goto do_trie_utf8_fold; \
1732 case trie_utf8_exactfa_fold: \
1733 flags |= FOLD_FLAGS_NOMIX_ASCII; \
1735 case trie_utf8_fold: \
1736 do_trie_utf8_fold: \
1737 if ( foldlen>0 ) { \
1738 uvc = utf8n_to_uvchr( (const U8*) uscan, foldlen, &len, uniflags ); \
1743 uvc = _toFOLD_utf8_flags( (const U8*) uc, uc_end, foldbuf, &foldlen, \
1745 len = UTF8_SAFE_SKIP(uc, uc_end); \
1746 skiplen = UVCHR_SKIP( uvc ); \
1747 foldlen -= skiplen; \
1748 uscan = foldbuf + skiplen; \
1751 case trie_flu8_latin: \
1752 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
1753 goto do_trie_latin_utf8_fold; \
1754 case trie_latin_utf8_exactfa_fold: \
1755 flags |= FOLD_FLAGS_NOMIX_ASCII; \
1757 case trie_latin_utf8_fold: \
1758 do_trie_latin_utf8_fold: \
1759 if ( foldlen>0 ) { \
1760 uvc = utf8n_to_uvchr( (const U8*) uscan, foldlen, &len, uniflags ); \
1766 uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags); \
1767 skiplen = UVCHR_SKIP( uvc ); \
1768 foldlen -= skiplen; \
1769 uscan = foldbuf + skiplen; \
1773 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
1774 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \
1775 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc_end); \
1779 uvc = utf8n_to_uvchr( (const U8*) uc, uc_end - uc, &len, uniflags ); \
1786 charid = trie->charmap[ uvc ]; \
1790 if (widecharmap) { \
1791 SV** const svpp = hv_fetch(widecharmap, \
1792 (char*)&uvc, sizeof(UV), 0); \
1794 charid = (U16)SvIV(*svpp); \
1799 #define DUMP_EXEC_POS(li,s,doutf8,depth) \
1800 dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \
1801 startpos, doutf8, depth)
1803 #define GET_ANYOFH_INVLIST(prog, n) \
1804 GET_REGCLASS_AUX_DATA(prog, n, TRUE, 0, NULL, NULL)
1806 #define REXEC_FBC_UTF8_SCAN(CODE) \
1808 while (s < strend) { \
1810 s += UTF8_SAFE_SKIP(s, reginfo->strend); \
1814 #define REXEC_FBC_NON_UTF8_SCAN(CODE) \
1816 while (s < strend) { \
1822 #define REXEC_FBC_UTF8_CLASS_SCAN(COND) \
1824 while (s < strend) { \
1825 REXEC_FBC_UTF8_CLASS_SCAN_GUTS(COND) \
1829 #define REXEC_FBC_NON_UTF8_CLASS_SCAN(COND) \
1831 while (s < strend) { \
1832 REXEC_FBC_NON_UTF8_CLASS_SCAN_GUTS(COND) \
1836 #define REXEC_FBC_UTF8_CLASS_SCAN_GUTS(COND) \
1839 s += UTF8_SAFE_SKIP(s, reginfo->strend); \
1840 previous_occurrence_end = s; \
1846 #define REXEC_FBC_NON_UTF8_CLASS_SCAN_GUTS(COND) \
1850 previous_occurrence_end = s; \
1856 /* We keep track of where the next character should start after an occurrence
1857 * of the one we're looking for. Knowing that, we can see right away if the
1858 * next occurrence is adjacent to the previous. When 'doevery' is FALSE, we
1859 * don't accept the 2nd and succeeding adjacent occurrences */
1860 #define FBC_CHECK_AND_TRY \
1862 || s != previous_occurrence_end) \
1863 && ( reginfo->intuit \
1864 || (s <= reginfo->strend && regtry(reginfo, &s)))) \
1870 /* These differ from the above macros in that they call a function which
1871 * returns the next occurrence of the thing being looked for in 's'; and
1872 * 'strend' if there is no such occurrence. 'f' is something like fcn(a,b,c)
1874 #define REXEC_FBC_UTF8_FIND_NEXT_SCAN(f) \
1875 while (s < strend) { \
1877 if (s >= strend) { \
1883 previous_occurrence_end = s; \
1886 #define REXEC_FBC_NON_UTF8_FIND_NEXT_SCAN(f) \
1887 while (s < strend) { \
1889 if (s >= strend) { \
1895 previous_occurrence_end = s; \
1898 /* This is like the above macro except the function returns NULL if there is no
1899 * occurrence, and there is a further condition that must be matched besides
1901 #define REXEC_FBC_FIND_NEXT_UTF8_SCAN_COND(f, COND) \
1902 while (s < strend) { \
1905 s = (char *) strend; \
1911 s += UTF8_SAFE_SKIP(s, reginfo->strend); \
1912 previous_occurrence_end = s; \
1919 /* This differs from the above macros in that it is passed a single byte that
1920 * is known to begin the next occurrence of the thing being looked for in 's'.
1921 * It does a memchr to find the next occurrence of 'byte', before trying 'COND'
1922 * at that position. */
1923 #define REXEC_FBC_FIND_NEXT_UTF8_BYTE_SCAN(byte, COND) \
1924 REXEC_FBC_FIND_NEXT_UTF8_SCAN_COND(memchr(s, byte, strend - s), \
1927 /* This is like the function above, but takes an entire string to look for
1928 * instead of a single byte */
1929 #define REXEC_FBC_FIND_NEXT_UTF8_STRING_SCAN(substr, substr_end, COND) \
1930 REXEC_FBC_FIND_NEXT_UTF8_SCAN_COND( \
1931 ninstr(s, strend, substr, substr_end), \
1934 /* The four macros below are slightly different versions of the same logic.
1936 * The first is for /a and /aa when the target string is UTF-8. This can only
1937 * match ascii, but it must advance based on UTF-8. The other three handle
1938 * the non-UTF-8 and the more generic UTF-8 cases. In all four, we are
1939 * looking for the boundary (or non-boundary) between a word and non-word
1940 * character. The utf8 and non-utf8 cases have the same logic, but the details
1941 * must be different. Find the "wordness" of the character just prior to this
1942 * one, and compare it with the wordness of this one. If they differ, we have
1943 * a boundary. At the beginning of the string, pretend that the previous
1944 * character was a new-line.
1946 * All these macros uncleanly have side-effects with each other and outside
1947 * variables. So far it's been too much trouble to clean-up
1949 * TEST_NON_UTF8 is the macro or function to call to test if its byte input is
1950 * a word character or not.
1951 * IF_SUCCESS is code to do if it finds that we are at a boundary between
1953 * IF_FAIL is code to do if we aren't at a boundary between word/non-word
1955 * Exactly one of the two IF_FOO parameters is a no-op, depending on whether we
1956 * are looking for a boundary or for a non-boundary. If we are looking for a
1957 * boundary, we want IF_FAIL to be the no-op, and for IF_SUCCESS to go out and
1958 * see if this tentative match actually works, and if so, to quit the loop
1959 * here. And vice-versa if we are looking for a non-boundary.
1961 * 'tmp' below in the next four macros in the REXEC_FBC_UTF8_SCAN and
1962 * REXEC_FBC_UTF8_SCAN loops is a loop invariant, a bool giving the return of
1963 * TEST_NON_UTF8(s-1). To see this, note that that's what it is defined to be
1964 * at entry to the loop, and to get to the IF_FAIL branch, tmp must equal
1965 * TEST_NON_UTF8(s), and in the opposite branch, IF_SUCCESS, tmp is that
1966 * complement. But in that branch we complement tmp, meaning that at the
1967 * bottom of the loop tmp is always going to be equal to TEST_NON_UTF8(s),
1968 * which means at the top of the loop in the next iteration, it is
1969 * TEST_NON_UTF8(s-1) */
1970 #define FBC_UTF8_A(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
1971 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
1972 tmp = TEST_NON_UTF8(tmp); \
1973 REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \
1974 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1976 IF_SUCCESS; /* Is a boundary if values for s-1 and s differ */ \
1983 /* Like FBC_UTF8_A, but TEST_UV is a macro which takes a UV as its input, and
1984 * TEST_UTF8 is a macro that for the same input code points returns identically
1985 * to TEST_UV, but takes a pointer to a UTF-8 encoded string instead (and an
1986 * end pointer as well) */
1987 #define FBC_UTF8(TEST_UV, TEST_UTF8, IF_SUCCESS, IF_FAIL) \
1988 if (s == reginfo->strbeg) { \
1991 else { /* Back-up to the start of the previous character */ \
1992 U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg); \
1993 tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, \
1994 0, UTF8_ALLOW_DEFAULT); \
1996 tmp = TEST_UV(tmp); \
1997 REXEC_FBC_UTF8_SCAN(/* advances s while s < strend */ \
1998 if (tmp == ! (TEST_UTF8((U8 *) s, (U8 *) reginfo->strend))) { \
2007 /* Like the above two macros, for a UTF-8 target string. UTF8_CODE is the
2008 * complete code for handling UTF-8. Common to the BOUND and NBOUND cases,
2009 * set-up by the FBC_BOUND, etc macros below */
2010 #define FBC_BOUND_COMMON_UTF8(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
2012 /* Here, things have been set up by the previous code so that tmp is the \
2013 * return of TEST_NON_UTF8(s-1). We also have to check if this matches \
2014 * against the EOS, which we treat as a \n */ \
2015 if (tmp == ! TEST_NON_UTF8('\n')) { \
2022 /* Same as the macro above, but the target isn't UTF-8 */
2023 #define FBC_BOUND_COMMON_NON_UTF8(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
2024 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
2025 tmp = TEST_NON_UTF8(tmp); \
2026 REXEC_FBC_NON_UTF8_SCAN(/* advances s while s < strend */ \
2027 if (tmp == ! TEST_NON_UTF8(UCHARAT(s))) { \
2035 /* Here, things have been set up by the previous code so that tmp is \
2036 * the return of TEST_NON_UTF8(s-1). We also have to check if this \
2037 * matches against the EOS, which we treat as a \n */ \
2038 if (tmp == ! TEST_NON_UTF8('\n')) { \
2045 /* This is the macro to use when we want to see if something that looks like it
2046 * could match, actually does, and if so exits the loop. It needs to be used
2047 * only for bounds checking macros, as it allows for matching beyond the end of
2048 * string (which should be zero length without having to look at the string
2050 #define REXEC_FBC_TRYIT \
2051 if (reginfo->intuit || (s <= reginfo->strend && regtry(reginfo, &s))) \
2054 /* The only difference between the BOUND and NBOUND cases is that
2055 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
2056 * NBOUND. This is accomplished by passing it as either the if or else clause,
2057 * with the other one being empty (PLACEHOLDER is defined as empty).
2059 * The TEST_FOO parameters are for operating on different forms of input, but
2060 * all should be ones that return identically for the same underlying code
2063 #define FBC_BOUND_UTF8(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
2064 FBC_BOUND_COMMON_UTF8( \
2065 FBC_UTF8(TEST_UV, TEST_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
2066 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
2068 #define FBC_BOUND_NON_UTF8(TEST_NON_UTF8) \
2069 FBC_BOUND_COMMON_NON_UTF8(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
2071 #define FBC_BOUND_A_UTF8(TEST_NON_UTF8) \
2072 FBC_BOUND_COMMON_UTF8( \
2073 FBC_UTF8_A(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER),\
2074 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
2076 #define FBC_BOUND_A_NON_UTF8(TEST_NON_UTF8) \
2077 FBC_BOUND_COMMON_NON_UTF8(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
2079 #define FBC_NBOUND_UTF8(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
2080 FBC_BOUND_COMMON_UTF8( \
2081 FBC_UTF8(TEST_UV, TEST_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
2082 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
2084 #define FBC_NBOUND_NON_UTF8(TEST_NON_UTF8) \
2085 FBC_BOUND_COMMON_NON_UTF8(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
2087 #define FBC_NBOUND_A_UTF8(TEST_NON_UTF8) \
2088 FBC_BOUND_COMMON_UTF8( \
2089 FBC_UTF8_A(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
2090 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
2092 #define FBC_NBOUND_A_NON_UTF8(TEST_NON_UTF8) \
2093 FBC_BOUND_COMMON_NON_UTF8(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
2097 S_get_break_val_cp_checked(SV* const invlist, const UV cp_in) {
2098 IV cp_out = _invlist_search(invlist, cp_in);
2099 assert(cp_out >= 0);
2102 # define _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp) \
2103 invmap[S_get_break_val_cp_checked(invlist, cp)]
2105 # define _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp) \
2106 invmap[_invlist_search(invlist, cp)]
2109 /* Takes a pointer to an inversion list, a pointer to its corresponding
2110 * inversion map, and a code point, and returns the code point's value
2111 * according to the two arrays. It assumes that all code points have a value.
2112 * This is used as the base macro for macros for particular properties */
2113 #define _generic_GET_BREAK_VAL_CP(invlist, invmap, cp) \
2114 _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp)
2116 /* Same as above, but takes begin, end ptrs to a UTF-8 encoded string instead
2117 * of a code point, returning the value for the first code point in the string.
2118 * And it takes the particular macro name that finds the desired value given a
2119 * code point. Merely convert the UTF-8 to code point and call the cp macro */
2120 #define _generic_GET_BREAK_VAL_UTF8(cp_macro, pos, strend) \
2121 (__ASSERT_(pos < strend) \
2122 /* Note assumes is valid UTF-8 */ \
2123 (cp_macro(utf8_to_uvchr_buf((pos), (strend), NULL))))
2125 /* Returns the GCB value for the input code point */
2126 #define getGCB_VAL_CP(cp) \
2127 _generic_GET_BREAK_VAL_CP( \
2132 /* Returns the GCB value for the first code point in the UTF-8 encoded string
2133 * bounded by pos and strend */
2134 #define getGCB_VAL_UTF8(pos, strend) \
2135 _generic_GET_BREAK_VAL_UTF8(getGCB_VAL_CP, pos, strend)
2137 /* Returns the LB value for the input code point */
2138 #define getLB_VAL_CP(cp) \
2139 _generic_GET_BREAK_VAL_CP( \
2144 /* Returns the LB value for the first code point in the UTF-8 encoded string
2145 * bounded by pos and strend */
2146 #define getLB_VAL_UTF8(pos, strend) \
2147 _generic_GET_BREAK_VAL_UTF8(getLB_VAL_CP, pos, strend)
2150 /* Returns the SB value for the input code point */
2151 #define getSB_VAL_CP(cp) \
2152 _generic_GET_BREAK_VAL_CP( \
2157 /* Returns the SB value for the first code point in the UTF-8 encoded string
2158 * bounded by pos and strend */
2159 #define getSB_VAL_UTF8(pos, strend) \
2160 _generic_GET_BREAK_VAL_UTF8(getSB_VAL_CP, pos, strend)
2162 /* Returns the WB value for the input code point */
2163 #define getWB_VAL_CP(cp) \
2164 _generic_GET_BREAK_VAL_CP( \
2169 /* Returns the WB value for the first code point in the UTF-8 encoded string
2170 * bounded by pos and strend */
2171 #define getWB_VAL_UTF8(pos, strend) \
2172 _generic_GET_BREAK_VAL_UTF8(getWB_VAL_CP, pos, strend)
2174 /* We know what class REx starts with. Try to find this position... */
2175 /* if reginfo->intuit, its a dryrun */
2176 /* annoyingly all the vars in this routine have different names from their counterparts
2177 in regmatch. /grrr */
2179 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
2180 const char *strend, regmatch_info *reginfo)
2183 /* TRUE if x+ need not match at just the 1st pos of run of x's */
2184 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
2186 char *pat_string; /* The pattern's exactish string */
2187 char *pat_end; /* ptr to end char of pat_string */
2188 re_fold_t folder; /* Function for computing non-utf8 folds */
2189 const U8 *fold_array; /* array for folding ords < 256 */
2196 /* In some cases we accept only the first occurence of 'x' in a sequence of
2197 * them. This variable points to just beyond the end of the previous
2198 * occurrence of 'x', hence we can tell if we are in a sequence. (Having
2199 * it point to beyond the 'x' allows us to work for UTF-8 without having to
2201 char * previous_occurrence_end = 0;
2203 I32 tmp; /* Scratch variable */
2204 const bool utf8_target = reginfo->is_utf8_target;
2205 UV utf8_fold_flags = 0;
2206 const bool is_utf8_pat = reginfo->is_utf8_pat;
2207 bool to_complement = FALSE; /* Invert the result? Taking the xor of this
2208 with a result inverts that result, as 0^1 =
2210 char_class_number_ classnum;
2212 RXi_GET_DECL(prog,progi);
2214 PERL_ARGS_ASSERT_FIND_BYCLASS;
2216 /* We know what class it must start with. The case statements below have
2217 * encoded the OP, and the UTF8ness of the target ('t8' for is UTF-8; 'tb'
2218 * for it isn't; 'b' stands for byte), and the UTF8ness of the pattern
2219 * ('p8' and 'pb'. */
2220 switch (with_tp_UTF8ness(OP(c), utf8_target, is_utf8_pat)) {
2223 case ANYOFPOSIXL_t8_pb:
2224 case ANYOFPOSIXL_t8_p8:
2227 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2228 CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_SETS(c);
2236 REXEC_FBC_UTF8_CLASS_SCAN(
2237 reginclass(prog, c, (U8*)s, (U8*) strend, 1 /* is utf8 */));
2240 case ANYOFPOSIXL_tb_pb:
2241 case ANYOFPOSIXL_tb_p8:
2244 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2245 CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_SETS(c);
2253 if (ANYOF_FLAGS(c) & ~ ANYOF_MATCHES_ALL_ABOVE_BITMAP) {
2254 /* We know that s is in the bitmap range since the target isn't
2255 * UTF-8, so what happens for out-of-range values is not relevant,
2256 * so exclude that from the flags */
2257 REXEC_FBC_NON_UTF8_CLASS_SCAN(reginclass(prog,c, (U8*)s, (U8*)s+1,
2261 REXEC_FBC_NON_UTF8_CLASS_SCAN(ANYOF_BITMAP_TEST(c, *((U8*)s)));
2265 case ANYOFM_tb_pb: /* ARG() is the base byte; FLAGS() the mask byte */
2267 REXEC_FBC_NON_UTF8_FIND_NEXT_SCAN(
2268 find_next_masked((U8 *) s, (U8 *) strend, (U8) ARG(c), FLAGS(c)));
2273 /* UTF-8ness doesn't matter because only matches UTF-8 invariants. But
2274 * we do anyway for performance reasons, as otherwise we would have to
2275 * examine all the continuation characters */
2276 REXEC_FBC_UTF8_FIND_NEXT_SCAN(
2277 find_next_masked((U8 *) s, (U8 *) strend, (U8) ARG(c), FLAGS(c)));
2282 REXEC_FBC_NON_UTF8_FIND_NEXT_SCAN(
2283 find_span_end_mask((U8 *) s, (U8 *) strend, (U8) ARG(c), FLAGS(c)));
2287 case NANYOFM_t8_p8: /* UTF-8ness does matter because can match UTF-8
2289 REXEC_FBC_UTF8_FIND_NEXT_SCAN(
2290 (char *) find_span_end_mask((U8 *) s, (U8 *) strend,
2291 (U8) ARG(c), FLAGS(c)));
2294 /* These nodes all require at least one code point to be in UTF-8 to
2304 case EXACTFLU8_tb_pb:
2305 case EXACTFLU8_tb_p8:
2306 case EXACTFU_REQ8_tb_pb:
2307 case EXACTFU_REQ8_tb_p8:
2312 anyofh_list = GET_ANYOFH_INVLIST(prog, c);
2313 REXEC_FBC_UTF8_CLASS_SCAN(
2314 ( (U8) NATIVE_UTF8_TO_I8(*s) >= ANYOF_FLAGS(c)
2315 && _invlist_contains_cp(anyofh_list,
2316 utf8_to_uvchr_buf((U8 *) s,
2324 /* We know what the first byte of any matched string should be. */
2325 U8 first_byte = FLAGS(c);
2327 anyofh_list = GET_ANYOFH_INVLIST(prog, c);
2328 REXEC_FBC_FIND_NEXT_UTF8_BYTE_SCAN(first_byte,
2329 _invlist_contains_cp(anyofh_list,
2330 utf8_to_uvchr_buf((U8 *) s,
2338 anyofh_list = GET_ANYOFH_INVLIST(prog, c);
2339 REXEC_FBC_UTF8_CLASS_SCAN(
2340 ( inRANGE(NATIVE_UTF8_TO_I8(*s),
2341 LOWEST_ANYOF_HRx_BYTE(ANYOF_FLAGS(c)),
2342 HIGHEST_ANYOF_HRx_BYTE(ANYOF_FLAGS(c)))
2343 && _invlist_contains_cp(anyofh_list,
2344 utf8_to_uvchr_buf((U8 *) s,
2351 anyofh_list = GET_ANYOFH_INVLIST(prog, c);
2352 REXEC_FBC_FIND_NEXT_UTF8_STRING_SCAN(
2353 ((struct regnode_anyofhs *) c)->string,
2354 /* Note FLAGS is the string length in this regnode */
2355 ((struct regnode_anyofhs *) c)->string + FLAGS(c),
2356 _invlist_contains_cp(anyofh_list,
2357 utf8_to_uvchr_buf((U8 *) s,
2364 REXEC_FBC_NON_UTF8_CLASS_SCAN(withinCOUNT((U8) *s,
2365 ANYOFRbase(c), ANYOFRdelta(c)));
2370 REXEC_FBC_UTF8_CLASS_SCAN(
2371 ( NATIVE_UTF8_TO_I8(*s) >= ANYOF_FLAGS(c)
2372 && withinCOUNT(utf8_to_uvchr_buf((U8 *) s,
2375 ANYOFRbase(c), ANYOFRdelta(c))));
2380 REXEC_FBC_NON_UTF8_CLASS_SCAN(withinCOUNT((U8) *s,
2381 ANYOFRbase(c), ANYOFRdelta(c)));
2386 { /* We know what the first byte of any matched string should be */
2387 U8 first_byte = FLAGS(c);
2389 REXEC_FBC_FIND_NEXT_UTF8_BYTE_SCAN(first_byte,
2390 withinCOUNT(utf8_to_uvchr_buf((U8 *) s,
2393 ANYOFRbase(c), ANYOFRdelta(c)));
2397 case EXACTFAA_tb_pb:
2399 /* Latin1 folds are not affected by /a, except it excludes the sharp s,
2400 * which these functions don't handle anyway */
2401 fold_array = PL_fold_latin1;
2402 folder = S_foldEQ_latin1_s2_folded;
2403 goto do_exactf_non_utf8;
2406 fold_array = PL_fold;
2407 folder = Perl_foldEQ;
2408 goto do_exactf_non_utf8;
2411 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2413 if (IN_UTF8_CTYPE_LOCALE) {
2414 utf8_fold_flags = FOLDEQ_LOCALE;
2415 goto do_exactf_utf8;
2418 fold_array = PL_fold_locale;
2419 folder = Perl_foldEQ_locale;
2420 goto do_exactf_non_utf8;
2423 /* Any 'ss' in the pattern should have been replaced by regcomp, so we
2424 * don't have to worry here about this single special case in the
2426 fold_array = PL_fold_latin1;
2427 folder = S_foldEQ_latin1_s2_folded;
2431 do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
2432 are no glitches with fold-length differences
2433 between the target string and pattern */
2435 /* The idea in the non-utf8 EXACTF* cases is to first find the first
2436 * character of the EXACTF* node and then, if necessary,
2437 * case-insensitively compare the full text of the node. c1 is the
2438 * first character. c2 is its fold. This logic will not work for
2439 * Unicode semantics and the german sharp ss, which hence should not be
2440 * compiled into a node that gets here. */
2441 pat_string = STRINGs(c);
2442 ln = STR_LENs(c); /* length to match in octets/bytes */
2444 /* We know that we have to match at least 'ln' bytes (which is the same
2445 * as characters, since not utf8). If we have to match 3 characters,
2446 * and there are only 2 availabe, we know without trying that it will
2447 * fail; so don't start a match past the required minimum number from
2449 e = HOP3c(strend, -((SSize_t)ln), s);
2454 c2 = fold_array[c1];
2455 if (c1 == c2) { /* If char and fold are the same */
2457 s = (char *) memchr(s, c1, e + 1 - s);
2462 /* Check that the rest of the node matches */
2463 if ( (ln == 1 || folder(aTHX_ s + 1, pat_string + 1, ln - 1))
2464 && (reginfo->intuit || regtry(reginfo, &s)) )
2472 U8 bits_differing = c1 ^ c2;
2474 /* If the folds differ in one bit position only, we can mask to
2475 * match either of them, and can use this faster find method. Both
2476 * ASCII and EBCDIC tend to have their case folds differ in only
2477 * one position, so this is very likely */
2478 if (LIKELY(PL_bitcount[bits_differing] == 1)) {
2479 bits_differing = ~ bits_differing;
2481 s = (char *) find_next_masked((U8 *) s, (U8 *) e + 1,
2482 (c1 & bits_differing), bits_differing);
2487 if ( (ln == 1 || folder(aTHX_ s + 1, pat_string + 1, ln - 1))
2488 && (reginfo->intuit || regtry(reginfo, &s)) )
2495 else { /* Otherwise, stuck with looking byte-at-a-time. This
2496 should actually happen only in EXACTFL nodes */
2498 if ( (*(U8*)s == c1 || *(U8*)s == c2)
2499 && (ln == 1 || folder(aTHX_ s + 1, pat_string + 1, ln - 1))
2500 && (reginfo->intuit || regtry(reginfo, &s)) )
2510 case EXACTFAA_tb_p8:
2511 case EXACTFAA_t8_p8:
2512 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII
2513 |FOLDEQ_S2_ALREADY_FOLDED
2514 |FOLDEQ_S2_FOLDS_SANE;
2515 goto do_exactf_utf8;
2517 case EXACTFAA_NO_TRIE_tb_pb:
2518 case EXACTFAA_NO_TRIE_t8_pb:
2519 case EXACTFAA_t8_pb:
2521 /* Here, and elsewhere in this file, the reason we can't consider a
2522 * non-UTF-8 pattern already folded in the presence of a UTF-8 target
2523 * is because any MICRO SIGN in the pattern won't be folded. Since the
2524 * fold of the MICRO SIGN requires UTF-8 to represent, we can consider
2525 * a non-UTF-8 pattern folded when matching a non-UTF-8 target */
2526 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
2527 goto do_exactf_utf8;
2532 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2533 utf8_fold_flags = FOLDEQ_LOCALE;
2534 goto do_exactf_utf8;
2536 case EXACTFLU8_t8_pb:
2537 case EXACTFLU8_t8_p8:
2538 utf8_fold_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED
2539 | FOLDEQ_S2_FOLDS_SANE;
2540 goto do_exactf_utf8;
2542 case EXACTFU_REQ8_t8_p8:
2543 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
2544 goto do_exactf_utf8;
2549 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
2550 goto do_exactf_utf8;
2552 /* The following are problematic even though pattern isn't UTF-8. Use
2553 * full functionality normally not done except for UTF-8. */
2555 case EXACTFUP_tb_pb:
2556 case EXACTFUP_t8_pb:
2562 /* If one of the operands is in utf8, we can't use the simpler
2563 * folding above, due to the fact that many different characters
2564 * can have the same fold, or portion of a fold, or different-
2566 pat_string = STRINGs(c);
2567 ln = STR_LENs(c); /* length to match in octets/bytes */
2568 pat_end = pat_string + ln;
2569 lnc = is_utf8_pat /* length to match in characters */
2570 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
2573 /* We have 'lnc' characters to match in the pattern, but because of
2574 * multi-character folding, each character in the target can match
2575 * up to 3 characters (Unicode guarantees it will never exceed
2576 * this) if it is utf8-encoded; and up to 2 if not (based on the
2577 * fact that the Latin 1 folds are already determined, and the only
2578 * multi-char fold in that range is the sharp-s folding to 'ss'.
2579 * Thus, a pattern character can match as little as 1/3 of a string
2580 * character. Adjust lnc accordingly, rounding up, so that if we
2581 * need to match at least 4+1/3 chars, that really is 5. */
2582 expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
2583 lnc = (lnc + expansion - 1) / expansion;
2585 /* As in the non-UTF8 case, if we have to match 3 characters, and
2586 * only 2 are left, it's guaranteed to fail, so don't start a match
2587 * that would require us to go beyond the end of the string */
2588 e = HOP3c(strend, -((SSize_t)lnc), s);
2590 /* XXX Note that we could recalculate e to stop the loop earlier,
2591 * as the worst case expansion above will rarely be met, and as we
2592 * go along we would usually find that e moves further to the left.
2593 * This would happen only after we reached the point in the loop
2594 * where if there were no expansion we should fail. Unclear if
2595 * worth the expense */
2598 char *my_strend= (char *)strend;
2599 if ( foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
2600 pat_string, NULL, ln, is_utf8_pat,
2602 && (reginfo->intuit || regtry(reginfo, &s)) )
2606 s += (utf8_target) ? UTF8_SAFE_SKIP(s, reginfo->strend) : 1;
2613 case BOUND_tb_pb: /* /d without utf8 target is /a */
2615 /* regcomp.c makes sure that these only have the traditional \b
2617 assert(FLAGS(c) == TRADITIONAL_BOUND);
2619 FBC_BOUND_A_NON_UTF8(isWORDCHAR_A);
2622 case BOUNDA_t8_pb: /* What /a matches is same under UTF-8 */
2624 /* regcomp.c makes sure that these only have the traditional \b
2626 assert(FLAGS(c) == TRADITIONAL_BOUND);
2628 FBC_BOUND_A_UTF8(isWORDCHAR_A);
2633 case NBOUND_tb_pb: /* /d without utf8 target is /a */
2635 /* regcomp.c makes sure that these only have the traditional \b
2637 assert(FLAGS(c) == TRADITIONAL_BOUND);
2639 FBC_NBOUND_A_NON_UTF8(isWORDCHAR_A);
2642 case NBOUNDA_t8_pb: /* What /a matches is same under UTF-8 */
2644 /* regcomp.c makes sure that these only have the traditional \b
2646 assert(FLAGS(c) == TRADITIONAL_BOUND);
2648 FBC_NBOUND_A_UTF8(isWORDCHAR_A);
2653 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
2654 FBC_NBOUND_NON_UTF8(isWORDCHAR_L1);
2659 goto do_boundu_non_utf8;
2663 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2664 if (FLAGS(c) == TRADITIONAL_BOUND) {
2665 FBC_NBOUND_NON_UTF8(isWORDCHAR_LC);
2669 CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_BOUND;
2672 goto do_boundu_non_utf8;
2676 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2677 if (FLAGS(c) == TRADITIONAL_BOUND) {
2678 FBC_BOUND_NON_UTF8(isWORDCHAR_LC);
2682 CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_BOUND;
2684 goto do_boundu_non_utf8;
2688 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
2689 FBC_BOUND_NON_UTF8(isWORDCHAR_L1);
2694 if (s == reginfo->strbeg) {
2695 if (reginfo->intuit || regtry(reginfo, &s))
2700 /* Didn't match. Try at the next position (if there is one) */
2702 if (UNLIKELY(s >= reginfo->strend)) {
2707 switch((bound_type) FLAGS(c)) {
2708 case TRADITIONAL_BOUND: /* Should have already been handled */
2713 /* Not utf8. Everything is a GCB except between CR and LF */
2714 while (s < strend) {
2715 if ((to_complement ^ ( UCHARAT(s - 1) != '\r'
2716 || UCHARAT(s) != '\n'))
2717 && (reginfo->intuit || regtry(reginfo, &s)))
2728 LB_enum before = getLB_VAL_CP((U8) *(s -1));
2729 while (s < strend) {
2730 LB_enum after = getLB_VAL_CP((U8) *s);
2731 if (to_complement ^ isLB(before,
2733 (U8*) reginfo->strbeg,
2735 (U8*) reginfo->strend,
2736 0 /* target not utf8 */ )
2737 && (reginfo->intuit || regtry(reginfo, &s)))
2750 SB_enum before = getSB_VAL_CP((U8) *(s -1));
2751 while (s < strend) {
2752 SB_enum after = getSB_VAL_CP((U8) *s);
2753 if ((to_complement ^ isSB(before,
2755 (U8*) reginfo->strbeg,
2757 (U8*) reginfo->strend,
2758 0 /* target not utf8 */ ))
2759 && (reginfo->intuit || regtry(reginfo, &s)))
2772 WB_enum previous = WB_UNKNOWN;
2773 WB_enum before = getWB_VAL_CP((U8) *(s -1));
2774 while (s < strend) {
2775 WB_enum after = getWB_VAL_CP((U8) *s);
2776 if ((to_complement ^ isWB(previous,
2779 (U8*) reginfo->strbeg,
2781 (U8*) reginfo->strend,
2782 0 /* target not utf8 */ ))
2783 && (reginfo->intuit || regtry(reginfo, &s)))
2794 /* Here are at the final position in the target string, which is a
2795 * boundary by definition, so matches, depending on other constraints.
2797 if ( reginfo->intuit
2798 || (s <= reginfo->strend && regtry(reginfo, &s)))
2807 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2808 if (FLAGS(c) == TRADITIONAL_BOUND) {
2809 FBC_BOUND_UTF8(isWORDCHAR_LC, isWORDCHAR_LC_uvchr,
2810 isWORDCHAR_LC_utf8_safe);
2814 CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_BOUND;
2817 goto do_boundu_utf8;
2821 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2822 if (FLAGS(c) == TRADITIONAL_BOUND) {
2823 FBC_NBOUND_UTF8(isWORDCHAR_LC, isWORDCHAR_LC_uvchr,
2824 isWORDCHAR_LC_utf8_safe);
2828 CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_BOUND;
2831 goto do_boundu_utf8;
2835 /* regcomp.c makes sure that these only have the traditional \b
2837 assert(FLAGS(c) == TRADITIONAL_BOUND);
2843 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
2844 FBC_NBOUND_UTF8(isWORDCHAR_L1, isWORDCHAR_uni,
2845 isWORDCHAR_utf8_safe);
2850 goto do_boundu_utf8;
2854 /* regcomp.c makes sure that these only have the traditional \b
2856 assert(FLAGS(c) == TRADITIONAL_BOUND);
2862 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
2863 FBC_BOUND_UTF8(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
2868 if (s == reginfo->strbeg) {
2869 if (reginfo->intuit || regtry(reginfo, &s))
2874 /* Didn't match. Try at the next position (if there is one) */
2875 s += UTF8_SAFE_SKIP(s, reginfo->strend);
2876 if (UNLIKELY(s >= reginfo->strend)) {
2881 switch((bound_type) FLAGS(c)) {
2882 case TRADITIONAL_BOUND: /* Should have already been handled */
2888 GCB_enum before = getGCB_VAL_UTF8(
2890 (U8*)(reginfo->strbeg)),
2891 (U8*) reginfo->strend);
2892 while (s < strend) {
2893 GCB_enum after = getGCB_VAL_UTF8((U8*) s,
2894 (U8*) reginfo->strend);
2895 if ( (to_complement ^ isGCB(before,
2897 (U8*) reginfo->strbeg,
2899 1 /* target is utf8 */ ))
2900 && (reginfo->intuit || regtry(reginfo, &s)))
2905 s += UTF8_SAFE_SKIP(s, reginfo->strend);
2912 LB_enum before = getLB_VAL_UTF8(reghop3((U8*)s,
2914 (U8*)(reginfo->strbeg)),
2915 (U8*) reginfo->strend);
2916 while (s < strend) {
2917 LB_enum after = getLB_VAL_UTF8((U8*) s,
2918 (U8*) reginfo->strend);
2919 if (to_complement ^ isLB(before,
2921 (U8*) reginfo->strbeg,
2923 (U8*) reginfo->strend,
2924 1 /* target is utf8 */ )
2925 && (reginfo->intuit || regtry(reginfo, &s)))
2930 s += UTF8_SAFE_SKIP(s, reginfo->strend);
2938 SB_enum before = getSB_VAL_UTF8(reghop3((U8*)s,
2940 (U8*)(reginfo->strbeg)),
2941 (U8*) reginfo->strend);
2942 while (s < strend) {
2943 SB_enum after = getSB_VAL_UTF8((U8*) s,
2944 (U8*) reginfo->strend);
2945 if ((to_complement ^ isSB(before,
2947 (U8*) reginfo->strbeg,
2949 (U8*) reginfo->strend,
2950 1 /* target is utf8 */ ))
2951 && (reginfo->intuit || regtry(reginfo, &s)))
2956 s += UTF8_SAFE_SKIP(s, reginfo->strend);
2964 /* We are at a boundary between char_sub_0 and char_sub_1.
2965 * We also keep track of the value for char_sub_-1 as we
2966 * loop through the line. Context may be needed to make a
2967 * determination, and if so, this can save having to
2969 WB_enum previous = WB_UNKNOWN;
2970 WB_enum before = getWB_VAL_UTF8(
2973 (U8*)(reginfo->strbeg)),
2974 (U8*) reginfo->strend);
2975 while (s < strend) {
2976 WB_enum after = getWB_VAL_UTF8((U8*) s,
2977 (U8*) reginfo->strend);
2978 if ((to_complement ^ isWB(previous,
2981 (U8*) reginfo->strbeg,
2983 (U8*) reginfo->strend,
2984 1 /* target is utf8 */ ))
2985 && (reginfo->intuit || regtry(reginfo, &s)))
2991 s += UTF8_SAFE_SKIP(s, reginfo->strend);
2996 /* Here are at the final position in the target string, which is a
2997 * boundary by definition, so matches, depending on other constraints.
3000 if ( reginfo->intuit
3001 || (s <= reginfo->strend && regtry(reginfo, &s)))
3009 REXEC_FBC_UTF8_CLASS_SCAN(is_LNBREAK_utf8_safe(s, strend));
3014 REXEC_FBC_NON_UTF8_CLASS_SCAN(is_LNBREAK_latin1_safe(s, strend));
3017 /* The argument to all the POSIX node types is the class number to pass
3018 * to generic_isCC_() to build a mask for searching in PL_charclass[] */
3027 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3028 REXEC_FBC_UTF8_CLASS_SCAN(
3029 to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s,
3040 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3041 REXEC_FBC_NON_UTF8_CLASS_SCAN(
3042 to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s)));
3047 /* The complement of something that matches only ASCII matches all
3048 * non-ASCII, plus everything in ASCII that isn't in the class. */
3049 REXEC_FBC_UTF8_CLASS_SCAN( ! isASCII_utf8_safe(s, strend)
3050 || ! generic_isCC_A_(*s, FLAGS(c)));
3055 /* Don't need to worry about utf8, as it can match only a single
3056 * byte invariant character. But we do anyway for performance reasons,
3057 * as otherwise we would have to examine all the continuation
3059 REXEC_FBC_UTF8_CLASS_SCAN(generic_isCC_A_(*s, FLAGS(c)));
3073 REXEC_FBC_NON_UTF8_CLASS_SCAN(
3074 to_complement ^ cBOOL(generic_isCC_A_(*s, FLAGS(c))));
3084 REXEC_FBC_NON_UTF8_CLASS_SCAN(
3085 to_complement ^ cBOOL(generic_isCC_(*s,
3100 classnum = (char_class_number_) FLAGS(c);
3103 REXEC_FBC_UTF8_CLASS_SCAN(
3104 to_complement ^ cBOOL(_invlist_contains_cp(
3105 PL_XPosix_ptrs[classnum],
3106 utf8_to_uvchr_buf((U8 *) s,
3111 case CC_ENUM_SPACE_:
3112 REXEC_FBC_UTF8_CLASS_SCAN(
3113 to_complement ^ cBOOL(isSPACE_utf8_safe(s, strend)));
3116 case CC_ENUM_BLANK_:
3117 REXEC_FBC_UTF8_CLASS_SCAN(
3118 to_complement ^ cBOOL(isBLANK_utf8_safe(s, strend)));
3121 case CC_ENUM_XDIGIT_:
3122 REXEC_FBC_UTF8_CLASS_SCAN(
3123 to_complement ^ cBOOL(isXDIGIT_utf8_safe(s, strend)));
3126 case CC_ENUM_VERTSPACE_:
3127 REXEC_FBC_UTF8_CLASS_SCAN(
3128 to_complement ^ cBOOL(isVERTWS_utf8_safe(s, strend)));
3131 case CC_ENUM_CNTRL_:
3132 REXEC_FBC_UTF8_CLASS_SCAN(
3133 to_complement ^ cBOOL(isCNTRL_utf8_safe(s, strend)));
3138 case AHOCORASICKC_tb_pb:
3139 case AHOCORASICKC_tb_p8:
3140 case AHOCORASICKC_t8_pb:
3141 case AHOCORASICKC_t8_p8:
3142 case AHOCORASICK_tb_pb:
3143 case AHOCORASICK_tb_p8:
3144 case AHOCORASICK_t8_pb:
3145 case AHOCORASICK_t8_p8:
3148 /* what trie are we using right now */
3149 reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ];
3150 reg_trie_data *trie = (reg_trie_data*)progi->data->data[aho->trie];
3151 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
3153 const char *last_start = strend - trie->minlen;
3155 const char *real_start = s;
3157 STRLEN maxlen = trie->maxlen;
3159 U8 **points; /* map of where we were in the input string
3160 when reading a given char. For ASCII this
3161 is unnecessary overhead as the relationship
3162 is always 1:1, but for Unicode, especially
3163 case folded Unicode this is not true. */
3164 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3168 DECLARE_AND_GET_RE_DEBUG_FLAGS;
3170 /* We can't just allocate points here. We need to wrap it in
3171 * an SV so it gets freed properly if there is a croak while
3172 * running the match */
3175 sv_points=newSV(maxlen * sizeof(U8 *));
3176 SvCUR_set(sv_points,
3177 maxlen * sizeof(U8 *));
3178 SvPOK_on(sv_points);
3179 sv_2mortal(sv_points);
3180 points=(U8**)SvPV_nolen(sv_points );
3181 if ( trie_type != trie_utf8_fold
3182 && (trie->bitmap || OP(c)==AHOCORASICKC) )
3185 bitmap=(U8*)trie->bitmap;
3187 bitmap=(U8*)ANYOF_BITMAP(c);
3189 /* this is the Aho-Corasick algorithm modified a touch
3190 to include special handling for long "unknown char" sequences.
3191 The basic idea being that we use AC as long as we are dealing
3192 with a possible matching char, when we encounter an unknown char
3193 (and we have not encountered an accepting state) we scan forward
3194 until we find a legal starting char.
3195 AC matching is basically that of trie matching, except that when
3196 we encounter a failing transition, we fall back to the current
3197 states "fail state", and try the current char again, a process
3198 we repeat until we reach the root state, state 1, or a legal
3199 transition. If we fail on the root state then we can either
3200 terminate if we have reached an accepting state previously, or
3201 restart the entire process from the beginning if we have not.
3204 while (s <= last_start) {
3205 const U32 uniflags = UTF8_ALLOW_DEFAULT;
3213 U8 *uscan = (U8*)NULL;
3214 U8 *leftmost = NULL;
3216 U32 accepted_word= 0;
3220 while ( state && uc <= (U8*)strend ) {
3222 U32 word = aho->states[ state ].wordnum;
3226 DEBUG_TRIE_EXECUTE_r(
3227 if ( uc <= (U8*)last_start
3228 && !BITMAP_TEST(bitmap,*uc) )
3230 dump_exec_pos( (char *)uc, c, strend,
3232 (char *)uc, utf8_target, 0 );
3233 Perl_re_printf( aTHX_
3234 " Scanning for legal start char...\n");
3238 while ( uc <= (U8*)last_start
3239 && !BITMAP_TEST(bitmap,*uc) )
3244 while ( uc <= (U8*)last_start
3245 && ! BITMAP_TEST(bitmap,*uc) )
3252 if (uc >(U8*)last_start) break;
3256 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len)
3258 if (!leftmost || lpos < leftmost) {
3259 DEBUG_r(accepted_word=word);
3265 points[pointpos++ % maxlen]= uc;
3266 if (foldlen || uc < (U8*)strend) {
3267 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3268 (U8 *) strend, uscan, len, uvc,
3269 charid, foldlen, foldbuf,
3271 DEBUG_TRIE_EXECUTE_r({
3272 dump_exec_pos( (char *)uc, c, strend,
3273 real_start, s, utf8_target, 0);
3274 Perl_re_printf( aTHX_
3275 " Charid:%3u CP:%4" UVxf " ",
3287 word = aho->states[ state ].wordnum;
3289 base = aho->states[ state ].trans.base;
3291 DEBUG_TRIE_EXECUTE_r({
3293 dump_exec_pos((char *)uc, c, strend, real_start,
3294 s, utf8_target, 0 );
3295 Perl_re_printf( aTHX_
3296 "%sState: %4" UVxf ", word=%" UVxf,
3297 failed ? " Fail transition to " : "",
3298 (UV)state, (UV)word);
3304 ( ((offset = base + charid
3305 - 1 - trie->uniquecharcount)) >= 0)
3306 && ((U32)offset < trie->lasttrans)
3307 && trie->trans[offset].check == state
3308 && (tmp=trie->trans[offset].next))
3310 DEBUG_TRIE_EXECUTE_r(
3311 Perl_re_printf( aTHX_ " - legal\n"));
3316 DEBUG_TRIE_EXECUTE_r(
3317 Perl_re_printf( aTHX_ " - fail\n"));
3319 state = aho->fail[state];
3323 /* we must be accepting here */
3324 DEBUG_TRIE_EXECUTE_r(
3325 Perl_re_printf( aTHX_ " - accepting\n"));
3334 if (!state) state = 1;
3337 if ( aho->states[ state ].wordnum ) {
3338 U8 *lpos = points[ (pointpos
3339 - trie->wordinfo[aho->states[ state ]
3340 .wordnum].len) % maxlen ];
3341 if (!leftmost || lpos < leftmost) {
3342 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
3347 s = (char*)leftmost;
3348 DEBUG_TRIE_EXECUTE_r({
3349 Perl_re_printf( aTHX_ "Matches word #%" UVxf
3350 " at position %" IVdf ". Trying full"
3352 (UV)accepted_word, (IV)(s - real_start)
3355 if (reginfo->intuit || regtry(reginfo, &s)) {
3360 if (s < reginfo->strend) {
3363 DEBUG_TRIE_EXECUTE_r({
3364 Perl_re_printf( aTHX_
3365 "Pattern failed. Looking for new start"
3369 DEBUG_TRIE_EXECUTE_r(
3370 Perl_re_printf( aTHX_ "No match.\n"));
3379 case EXACTFU_REQ8_t8_pb:
3380 case EXACTFUP_tb_p8:
3381 case EXACTFUP_t8_p8:
3383 case EXACTF_t8_p8: /* This node only generated for non-utf8 patterns */
3384 case EXACTFAA_NO_TRIE_tb_p8:
3385 case EXACTFAA_NO_TRIE_t8_p8: /* This node only generated for non-utf8
3390 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
3391 } /* End of switch on node type */
3399 /* set RX_SAVED_COPY, RX_SUBBEG etc.
3400 * flags have same meanings as with regexec_flags() */
3403 S_reg_set_capture_string(pTHX_ REGEXP * const rx,
3410 struct regexp *const prog = ReANY(rx);
3412 if (flags & REXEC_COPY_STR) {
3415 DEBUG_C(Perl_re_printf( aTHX_
3416 "Copy on write: regexp capture, type %d\n",
3418 /* Create a new COW SV to share the match string and store
3419 * in saved_copy, unless the current COW SV in saved_copy
3420 * is valid and suitable for our purpose */
3421 if (( prog->saved_copy
3422 && SvIsCOW(prog->saved_copy)
3423 && SvPOKp(prog->saved_copy)
3426 && SvPVX(sv) == SvPVX(prog->saved_copy)))
3428 /* just reuse saved_copy SV */
3429 if (RXp_MATCH_COPIED(prog)) {
3430 Safefree(prog->subbeg);
3431 RXp_MATCH_COPIED_off(prog);
3435 /* create new COW SV to share string */
3436 RXp_MATCH_COPY_FREE(prog);
3437 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
3439 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
3440 assert (SvPOKp(prog->saved_copy));
3441 prog->sublen = strend - strbeg;
3442 prog->suboffset = 0;
3443 prog->subcoffset = 0;
3448 SSize_t max = strend - strbeg;
3451 if ( (flags & REXEC_COPY_SKIP_POST)
3452 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
3453 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
3454 ) { /* don't copy $' part of string */
3457 /* calculate the right-most part of the string covered
3458 * by a capture. Due to lookahead, this may be to
3459 * the right of $&, so we have to scan all captures */
3460 while (n <= prog->lastparen) {
3461 if (prog->offs[n].end > max)
3462 max = prog->offs[n].end;
3466 max = (PL_sawampersand & SAWAMPERSAND_LEFT)
3467 ? prog->offs[0].start
3469 assert(max >= 0 && max <= strend - strbeg);
3472 if ( (flags & REXEC_COPY_SKIP_PRE)
3473 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
3474 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
3475 ) { /* don't copy $` part of string */
3478 /* calculate the left-most part of the string covered
3479 * by a capture. Due to lookbehind, this may be to
3480 * the left of $&, so we have to scan all captures */
3481 while (min && n <= prog->lastparen) {
3482 if ( prog->offs[n].start != -1
3483 && prog->offs[n].start < min)
3485 min = prog->offs[n].start;
3489 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
3490 && min > prog->offs[0].end
3492 min = prog->offs[0].end;
3496 assert(min >= 0 && min <= max && min <= strend - strbeg);
3499 if (RXp_MATCH_COPIED(prog)) {
3500 if (sublen > prog->sublen)
3502 (char*)saferealloc(prog->subbeg, sublen+1);
3505 prog->subbeg = (char*)safemalloc(sublen+1);
3506 Copy(strbeg + min, prog->subbeg, sublen, char);
3507 prog->subbeg[sublen] = '\0';
3508 prog->suboffset = min;
3509 prog->sublen = sublen;
3510 RXp_MATCH_COPIED_on(prog);
3512 prog->subcoffset = prog->suboffset;
3513 if (prog->suboffset && utf8_target) {
3514 /* Convert byte offset to chars.
3515 * XXX ideally should only compute this if @-/@+
3516 * has been seen, a la PL_sawampersand ??? */
3518 /* If there's a direct correspondence between the
3519 * string which we're matching and the original SV,
3520 * then we can use the utf8 len cache associated with
3521 * the SV. In particular, it means that under //g,
3522 * sv_pos_b2u() will use the previously cached
3523 * position to speed up working out the new length of
3524 * subcoffset, rather than counting from the start of
3525 * the string each time. This stops
3526 * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
3527 * from going quadratic */
3528 if (SvPOKp(sv) && SvPVX(sv) == strbeg)
3529 prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset,
3530 SV_GMAGIC|SV_CONST_RETURN);
3532 prog->subcoffset = utf8_length((U8*)strbeg,
3533 (U8*)(strbeg+prog->suboffset));
3537 RXp_MATCH_COPY_FREE(prog);
3538 prog->subbeg = strbeg;
3539 prog->suboffset = 0;
3540 prog->subcoffset = 0;
3541 prog->sublen = strend - strbeg;
3549 - regexec_flags - match a regexp against a string
3552 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
3553 char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags)
3554 /* stringarg: the point in the string at which to begin matching */
3555 /* strend: pointer to null at end of string */
3556 /* strbeg: real beginning of string */
3557 /* minend: end of match must be >= minend bytes after stringarg. */
3558 /* sv: SV being matched: only used for utf8 flag, pos() etc; string
3559 * itself is accessed via the pointers above */
3560 /* data: May be used for some additional optimizations.
3561 Currently unused. */
3562 /* flags: For optimizations. See REXEC_* in regexp.h */
3565 struct regexp *const prog = ReANY(rx);
3569 SSize_t minlen; /* must match at least this many chars */
3570 SSize_t dontbother = 0; /* how many characters not to try at end */
3571 const bool utf8_target = cBOOL(DO_UTF8(sv));
3573 RXi_GET_DECL(prog,progi);
3574 regmatch_info reginfo_buf; /* create some info to pass to regtry etc */
3575 regmatch_info *const reginfo = ®info_buf;
3576 regexp_paren_pair *swap = NULL;
3578 DECLARE_AND_GET_RE_DEBUG_FLAGS;
3580 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
3581 PERL_UNUSED_ARG(data);
3583 /* Be paranoid... */
3585 Perl_croak(aTHX_ "NULL regexp parameter");
3589 debug_start_match(rx, utf8_target, stringarg, strend,
3593 startpos = stringarg;
3595 /* set these early as they may be used by the HOP macros below */
3596 reginfo->strbeg = strbeg;
3597 reginfo->strend = strend;
3598 reginfo->is_utf8_target = cBOOL(utf8_target);
3600 if (prog->intflags & PREGf_GPOS_SEEN) {
3603 /* set reginfo->ganch, the position where \G can match */
3606 (flags & REXEC_IGNOREPOS)
3607 ? stringarg /* use start pos rather than pos() */
3608 : ((mg = mg_find_mglob(sv)) && mg->mg_len >= 0)
3609 /* Defined pos(): */
3610 ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg)
3611 : strbeg; /* pos() not defined; use start of string */
3613 DEBUG_GPOS_r(Perl_re_printf( aTHX_
3614 "GPOS ganch set to strbeg[%" IVdf "]\n", (IV)(reginfo->ganch - strbeg)));
3616 /* in the presence of \G, we may need to start looking earlier in
3617 * the string than the suggested start point of stringarg:
3618 * if prog->gofs is set, then that's a known, fixed minimum
3621 * /ab|c\G/: gofs = 1
3622 * or if the minimum offset isn't known, then we have to go back
3623 * to the start of the string, e.g. /w+\G/
3626 if (prog->intflags & PREGf_ANCH_GPOS) {
3628 startpos = HOPBACKc(reginfo->ganch, prog->gofs);
3630 ((flags & REXEC_FAIL_ON_UNDERFLOW) && startpos < stringarg))
3632 DEBUG_GPOS_r(Perl_re_printf( aTHX_
3633 "fail: ganch-gofs before earliest possible start\n"));
3638 startpos = reginfo->ganch;
3640 else if (prog->gofs) {
3641 startpos = HOPBACKc(startpos, prog->gofs);
3645 else if (prog->intflags & PREGf_GPOS_FLOAT)
3649 minlen = prog->minlen;
3650 if ((startpos + minlen) > strend || startpos < strbeg) {
3651 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
3652 "Regex match can't succeed, so not even tried\n"));
3656 /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave),
3657 * which will call destuctors to reset PL_regmatch_state, free higher
3658 * PL_regmatch_slabs, and clean up regmatch_info_aux and
3659 * regmatch_info_aux_eval */
3661 oldsave = PL_savestack_ix;
3665 if ((prog->extflags & RXf_USE_INTUIT)
3666 && !(flags & REXEC_CHECKED))
3668 s = re_intuit_start(rx, sv, strbeg, startpos, strend,
3673 if (prog->extflags & RXf_CHECK_ALL) {
3674 /* we can match based purely on the result of INTUIT.
3675 * Set up captures etc just for $& and $-[0]
3676 * (an intuit-only match wont have $1,$2,..) */
3677 assert(!prog->nparens);
3679 /* s/// doesn't like it if $& is earlier than where we asked it to
3680 * start searching (which can happen on something like /.\G/) */
3681 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
3684 /* this should only be possible under \G */
3685 assert(prog->intflags & PREGf_GPOS_SEEN);
3686 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
3687 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3691 /* match via INTUIT shouldn't have any captures.
3692 * Let @-, @+, $^N know */
3693 prog->lastparen = prog->lastcloseparen = 0;
3694 RXp_MATCH_UTF8_set(prog, utf8_target);
3695 prog->offs[0].start = s - strbeg;
3696 prog->offs[0].end = utf8_target
3697 ? (char*)utf8_hop_forward((U8*)s, prog->minlenret, (U8 *) strend) - strbeg
3698 : s - strbeg + prog->minlenret;
3699 if ( !(flags & REXEC_NOT_FIRST) )
3700 S_reg_set_capture_string(aTHX_ rx,
3702 sv, flags, utf8_target);
3708 multiline = prog->extflags & RXf_PMf_MULTILINE;
3710 if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
3711 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
3712 "String too short [regexec_flags]...\n"));
3716 /* Check validity of program. */
3717 if (UCHARAT(progi->program) != REG_MAGIC) {
3718 Perl_croak(aTHX_ "corrupted regexp program");
3721 RXp_MATCH_TAINTED_off(prog);
3722 RXp_MATCH_UTF8_set(prog, utf8_target);
3724 reginfo->prog = rx; /* Yes, sorry that this is confusing. */
3725 reginfo->intuit = 0;
3726 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
3727 reginfo->warned = FALSE;
3729 reginfo->poscache_maxiter = 0; /* not yet started a countdown */
3730 /* see how far we have to get to not match where we matched before */
3731 reginfo->till = stringarg + minend;
3733 if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) {
3734 /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after
3735 S_cleanup_regmatch_info_aux has executed (registered by
3736 SAVEDESTRUCTOR_X below). S_cleanup_regmatch_info_aux modifies
3737 magic belonging to this SV.
3738 Not newSVsv, either, as it does not COW.
3740 reginfo->sv = newSV_type(SVt_NULL);
3741 SvSetSV_nosteal(reginfo->sv, sv);
3742 SAVEFREESV(reginfo->sv);
3745 /* reserve next 2 or 3 slots in PL_regmatch_state:
3746 * slot N+0: may currently be in use: skip it
3747 * slot N+1: use for regmatch_info_aux struct
3748 * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s
3749 * slot N+3: ready for use by regmatch()
3753 regmatch_state *old_regmatch_state;
3754 regmatch_slab *old_regmatch_slab;
3755 int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1;
3757 /* on first ever match, allocate first slab */
3758 if (!PL_regmatch_slab) {
3759 Newx(PL_regmatch_slab, 1, regmatch_slab);
3760 PL_regmatch_slab->prev = NULL;
3761 PL_regmatch_slab->next = NULL;
3762 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
3765 old_regmatch_state = PL_regmatch_state;
3766 old_regmatch_slab = PL_regmatch_slab;
3768 for (i=0; i <= max; i++) {
3770 reginfo->info_aux = &(PL_regmatch_state->u.info_aux);
3772 reginfo->info_aux_eval =
3773 reginfo->info_aux->info_aux_eval =
3774 &(PL_regmatch_state->u.info_aux_eval);
3776 if (++PL_regmatch_state > SLAB_LAST(PL_regmatch_slab))
3777 PL_regmatch_state = S_push_slab(aTHX);
3780 /* note initial PL_regmatch_state position; at end of match we'll
3781 * pop back to there and free any higher slabs */
3783 reginfo->info_aux->old_regmatch_state = old_regmatch_state;
3784 reginfo->info_aux->old_regmatch_slab = old_regmatch_slab;
3785 reginfo->info_aux->poscache = NULL;
3787 SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux);
3789 if ((prog->extflags & RXf_EVAL_SEEN))
3790 S_setup_eval_state(aTHX_ reginfo);
3792 reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL;
3795 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
3796 /* We have to be careful. If the previous successful match
3797 was from this regex we don't want a subsequent partially
3798 successful match to clobber the old results.
3799 So when we detect this possibility we add a swap buffer
3800 to the re, and switch the buffer each match. If we fail,
3801 we switch it back; otherwise we leave it swapped.
3804 /* avoid leak if we die, or clean up anyway if match completes */
3806 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
3807 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
3808 "rex=0x%" UVxf " saving offs: orig=0x%" UVxf " new=0x%" UVxf "\n",
3816 if (prog->recurse_locinput)
3817 Zero(prog->recurse_locinput,prog->nparens + 1, char *);
3819 /* Simplest case: anchored match (but not \G) need be tried only once,
3820 * or with MBOL, only at the beginning of each line.
3822 * Note that /.*.../ sets PREGf_IMPLICIT|MBOL, while /.*.../s sets
3823 * PREGf_IMPLICIT|SBOL. The idea is that with /.*.../s, if it doesn't
3824 * match at the start of the string then it won't match anywhere else
3825 * either; while with /.*.../, if it doesn't match at the beginning,
3826 * the earliest it could match is at the start of the next line */
3828 if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) {
3831 if (regtry(reginfo, &s))
3834 if (!(prog->intflags & PREGf_ANCH_MBOL))
3837 /* didn't match at start, try at other newline positions */
3840 dontbother = minlen - 1;
3841 end = HOP3c(strend, -dontbother, strbeg) - 1;
3843 /* skip to next newline */
3845 while (s <= end) { /* note it could be possible to match at the end of the string */
3846 /* NB: newlines are the same in unicode as they are in latin */
3849 if (prog->check_substr || prog->check_utf8) {
3850 /* note that with PREGf_IMPLICIT, intuit can only fail
3851 * or return the start position, so it's of limited utility.
3852 * Nevertheless, I made the decision that the potential for
3853 * quick fail was still worth it - DAPM */
3854 s = re_intuit_start(rx, sv, strbeg, s, strend, flags, NULL);
3858 if (regtry(reginfo, &s))
3862 } /* end anchored search */
3864 /* anchored \G match */
3865 if (prog->intflags & PREGf_ANCH_GPOS)
3867 /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */
3868 assert(prog->intflags & PREGf_GPOS_SEEN);
3869 /* For anchored \G, the only position it can match from is
3870 * (ganch-gofs); we already set startpos to this above; if intuit
3871 * moved us on from there, we can't possibly succeed */
3872 assert(startpos == HOPBACKc(reginfo->ganch, prog->gofs));
3873 if (s == startpos && regtry(reginfo, &s))
3878 /* Messy cases: unanchored match. */
3880 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
3881 /* we have /x+whatever/ */
3882 /* it must be a one character string (XXXX Except is_utf8_pat?) */
3888 if (! prog->anchored_utf8) {
3889 to_utf8_substr(prog);
3891 ch = SvPVX_const(prog->anchored_utf8)[0];
3892 REXEC_FBC_UTF8_SCAN(
3894 DEBUG_EXECUTE_r( did_match = 1 );
3895 if (regtry(reginfo, &s)) goto got_it;
3896 s += UTF8_SAFE_SKIP(s, strend);
3897 while (s < strend && *s == ch)
3904 if (! prog->anchored_substr) {
3905 if (! to_byte_substr(prog)) {
3906 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3909 ch = SvPVX_const(prog->anchored_substr)[0];
3910 REXEC_FBC_NON_UTF8_SCAN(
3912 DEBUG_EXECUTE_r( did_match = 1 );
3913 if (regtry(reginfo, &s)) goto got_it;
3915 while (s < strend && *s == ch)
3920 DEBUG_EXECUTE_r(if (!did_match)
3921 Perl_re_printf( aTHX_
3922 "Did not find anchored character...\n")
3925 else if (prog->anchored_substr != NULL
3926 || prog->anchored_utf8 != NULL
3927 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
3928 && prog->float_max_offset < strend - s)) {
3933 char *last1; /* Last position checked before */
3937 if (prog->anchored_substr || prog->anchored_utf8) {
3939 if (! prog->anchored_utf8) {
3940 to_utf8_substr(prog);
3942 must = prog->anchored_utf8;
3945 if (! prog->anchored_substr) {
3946 if (! to_byte_substr(prog)) {
3947 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3950 must = prog->anchored_substr;
3952 back_max = back_min = prog->anchored_offset;
3955 if (! prog->float_utf8) {
3956 to_utf8_substr(prog);
3958 must = prog->float_utf8;
3961 if (! prog->float_substr) {
3962 if (! to_byte_substr(prog)) {
3963 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3966 must = prog->float_substr;
3968 back_max = prog->float_max_offset;
3969 back_min = prog->float_min_offset;
3975 last = HOP3c(strend, /* Cannot start after this */
3976 -(SSize_t)(CHR_SVLEN(must)
3977 - (SvTAIL(must) != 0) + back_min), strbeg);
3979 if (s > reginfo->strbeg)
3980 last1 = HOPc(s, -1);
3982 last1 = s - 1; /* bogus */
3984 /* XXXX check_substr already used to find "s", can optimize if
3985 check_substr==must. */
3987 strend = HOPc(strend, -dontbother);
3988 while ( (s <= last) &&
3989 (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg, strend),
3990 (unsigned char*)strend, must,
3991 multiline ? FBMrf_MULTILINE : 0)) ) {
3992 DEBUG_EXECUTE_r( did_match = 1 );
3993 if (HOPc(s, -back_max) > last1) {
3994 last1 = HOPc(s, -back_min);
3995 s = HOPc(s, -back_max);
3998 char * const t = (last1 >= reginfo->strbeg)
3999 ? HOPc(last1, 1) : last1 + 1;
4001 last1 = HOPc(s, -back_min);
4005 while (s <= last1) {
4006 if (regtry(reginfo, &s))
4009 s++; /* to break out of outer loop */
4016 while (s <= last1) {
4017 if (regtry(reginfo, &s))
4023 DEBUG_EXECUTE_r(if (!did_match) {
4024 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
4025 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
4026 Perl_re_printf( aTHX_ "Did not find %s substr %s%s...\n",
4027 ((must == prog->anchored_substr || must == prog->anchored_utf8)
4028 ? "anchored" : "floating"),
4029 quoted, RE_SV_TAIL(must));
4033 else if ( (c = progi->regstclass) ) {
4035 const OPCODE op = OP(progi->regstclass);
4036 /* don't bother with what can't match */
4037 if (PL_regkind[op] != EXACT && PL_regkind[op] != TRIE)
4038 strend = HOPc(strend, -(minlen - 1));
4041 SV * const prop = sv_newmortal();
4042 regprop(prog, prop, c, reginfo, NULL);
4044 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
4045 s,strend-s,PL_dump_re_max_len);
4046 Perl_re_printf( aTHX_
4047 "Matching stclass %.*s against %s (%d bytes)\n",
4048 (int)SvCUR(prop), SvPVX_const(prop),
4049 quoted, (int)(strend - s));
4052 if (find_byclass(prog, c, s, strend, reginfo))
4054 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "Contradicts stclass... [regexec_flags]\n"));
4058 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
4066 if (! prog->float_utf8) {
4067 to_utf8_substr(prog);
4069 float_real = prog->float_utf8;
4072 if (! prog->float_substr) {
4073 if (! to_byte_substr(prog)) {
4074 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
4077 float_real = prog->float_substr;
4080 little = SvPV_const(float_real, len);
4081 if (SvTAIL(float_real)) {
4082 /* This means that float_real contains an artificial \n on
4083 * the end due to the presence of something like this:
4084 * /foo$/ where we can match both "foo" and "foo\n" at the
4085 * end of the string. So we have to compare the end of the
4086 * string first against the float_real without the \n and
4087 * then against the full float_real with the string. We
4088 * have to watch out for cases where the string might be
4089 * smaller than the float_real or the float_real without
4091 char *checkpos= strend - len;
4093 Perl_re_printf( aTHX_
4094 "%sChecking for float_real.%s\n",
4095 PL_colors[4], PL_colors[5]));
4096 if (checkpos + 1 < strbeg) {
4097 /* can't match, even if we remove the trailing \n
4098 * string is too short to match */
4100 Perl_re_printf( aTHX_
4101 "%sString shorter than required trailing substring, cannot match.%s\n",
4102 PL_colors[4], PL_colors[5]));
4104 } else if (memEQ(checkpos + 1, little, len - 1)) {
4105 /* can match, the end of the string matches without the
4107 last = checkpos + 1;
4108 } else if (checkpos < strbeg) {
4109 /* cant match, string is too short when the "\n" is
4112 Perl_re_printf( aTHX_
4113 "%sString does not contain required trailing substring, cannot match.%s\n",
4114 PL_colors[4], PL_colors[5]));
4116 } else if (!multiline) {
4117 /* non multiline match, so compare with the "\n" at the
4118 * end of the string */
4119 if (memEQ(checkpos, little, len)) {
4123 Perl_re_printf( aTHX_
4124 "%sString does not contain required trailing substring, cannot match.%s\n",
4125 PL_colors[4], PL_colors[5]));
4129 /* multiline match, so we have to search for a place
4130 * where the full string is located */
4136 last = rninstr(s, strend, little, little + len);
4138 last = strend; /* matching "$" */
4141 /* at one point this block contained a comment which was
4142 * probably incorrect, which said that this was a "should not
4143 * happen" case. Even if it was true when it was written I am
4144 * pretty sure it is not anymore, so I have removed the comment
4145 * and replaced it with this one. Yves */
4147 Perl_re_printf( aTHX_
4148 "%sString does not contain required substring, cannot match.%s\n",
4149 PL_colors[4], PL_colors[5]
4153 dontbother = strend - last + prog->float_min_offset;
4155 if (minlen && (dontbother < minlen))
4156 dontbother = minlen - 1;
4157 strend -= dontbother; /* this one's always in bytes! */
4158 /* We don't know much -- general case. */
4161 if (regtry(reginfo, &s))
4170 if (regtry(reginfo, &s))
4172 } while (s++ < strend);
4180 /* s/// doesn't like it if $& is earlier than where we asked it to
4181 * start searching (which can happen on something like /.\G/) */
4182 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
4183 && (prog->offs[0].start < stringarg - strbeg))
4185 /* this should only be possible under \G */
4186 assert(prog->intflags & PREGf_GPOS_SEEN);
4187 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
4188 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
4192 /* clean up; this will trigger destructors that will free all slabs
4193 * above the current one, and cleanup the regmatch_info_aux
4194 * and regmatch_info_aux_eval sructs */
4196 LEAVE_SCOPE(oldsave);
4198 if (RXp_PAREN_NAMES(prog))
4199 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
4201 /* make sure $`, $&, $', and $digit will work later */
4202 if ( !(flags & REXEC_NOT_FIRST) )
4203 S_reg_set_capture_string(aTHX_ rx,
4204 strbeg, reginfo->strend,
4205 sv, flags, utf8_target);
4210 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%sMatch failed%s\n",
4211 PL_colors[4], PL_colors[5]));
4214 /* we failed :-( roll it back.
4215 * Since the swap buffer will be freed on scope exit which follows
4216 * shortly, restore the old captures by copying 'swap's original
4217 * data to the new offs buffer
4219 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
4220 "rex=0x%" UVxf " rolling back offs: 0x%" UVxf " will be freed; restoring data to =0x%" UVxf "\n",
4227 Copy(swap, prog->offs, prog->nparens + 1, regexp_paren_pair);
4230 /* clean up; this will trigger destructors that will free all slabs
4231 * above the current one, and cleanup the regmatch_info_aux
4232 * and regmatch_info_aux_eval sructs */
4234 LEAVE_SCOPE(oldsave);
4240 /* Set which rex is pointed to by PL_reg_curpm, handling ref counting.
4241 * Do inc before dec, in case old and new rex are the same */
4242 #define SET_reg_curpm(Re2) \
4243 if (reginfo->info_aux_eval) { \
4244 (void)ReREFCNT_inc(Re2); \
4245 ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \
4246 PM_SETRE((PL_reg_curpm), (Re2)); \
4251 - regtry - try match at specific point
4253 STATIC bool /* 0 failure, 1 success */
4254 S_regtry(pTHX_ regmatch_info *reginfo, char **startposp)
4257 REGEXP *const rx = reginfo->prog;
4258 regexp *const prog = ReANY(rx);
4261 U32 depth = 0; /* used by REGCP_SET */
4263 RXi_GET_DECL(prog,progi);
4264 DECLARE_AND_GET_RE_DEBUG_FLAGS;
4266 PERL_ARGS_ASSERT_REGTRY;
4268 reginfo->cutpoint=NULL;
4270 prog->offs[0].start = *startposp - reginfo->strbeg;
4271 prog->lastparen = 0;
4272 prog->lastcloseparen = 0;
4274 /* XXXX What this code is doing here?!!! There should be no need
4275 to do this again and again, prog->lastparen should take care of
4278 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
4279 * Actually, the code in regcppop() (which Ilya may be meaning by
4280 * prog->lastparen), is not needed at all by the test suite
4281 * (op/regexp, op/pat, op/split), but that code is needed otherwise
4282 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
4283 * Meanwhile, this code *is* needed for the
4284 * above-mentioned test suite tests to succeed. The common theme
4285 * on those tests seems to be returning null fields from matches.
4286 * --jhi updated by dapm */
4288 /* After encountering a variant of the issue mentioned above I think
4289 * the point Ilya was making is that if we properly unwind whenever
4290 * we set lastparen to a smaller value then we should not need to do
4291 * this every time, only when needed. So if we have tests that fail if
4292 * we remove this, then it suggests somewhere else we are improperly
4293 * unwinding the lastparen/paren buffers. See UNWIND_PARENS() and
4294 * places it is called, and related regcp() routines. - Yves */
4296 if (prog->nparens) {
4297 regexp_paren_pair *pp = prog->offs;
4299 for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
4307 result = regmatch(reginfo, *startposp, progi->program + 1);
4309 prog->offs[0].end = result;
4312 if (reginfo->cutpoint)
4313 *startposp= reginfo->cutpoint;
4314 REGCP_UNWIND(lastcp);
4318 /* this is used to determine how far from the left messages like
4319 'failed...' are printed in regexec.c. It should be set such that
4320 messages are inline with the regop output that created them.
4322 #define REPORT_CODE_OFF 29
4323 #define INDENT_CHARS(depth) ((int)(depth) % 20)
4326 Perl_re_exec_indentf(pTHX_ const char *fmt, U32 depth, ...)
4330 PerlIO *f= Perl_debug_log;
4331 PERL_ARGS_ASSERT_RE_EXEC_INDENTF;
4332 va_start(ap, depth);
4333 PerlIO_printf(f, "%*s|%4" UVuf "| %*s", REPORT_CODE_OFF, "", (UV)depth, INDENT_CHARS(depth), "" );
4334 result = PerlIO_vprintf(f, fmt, ap);
4338 #endif /* DEBUGGING */
4340 /* grab a new slab and return the first slot in it */
4342 STATIC regmatch_state *
4345 regmatch_slab *s = PL_regmatch_slab->next;
4347 Newx(s, 1, regmatch_slab);
4348 s->prev = PL_regmatch_slab;
4350 PL_regmatch_slab->next = s;
4352 PL_regmatch_slab = s;
4353 return SLAB_FIRST(s);
4359 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
4360 const char *start, const char *end, const char *blurb)
4362 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
4364 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
4369 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
4370 RX_PRECOMP_const(prog), RX_PRELEN(prog), PL_dump_re_max_len);
4372 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
4373 start, end - start, PL_dump_re_max_len);
4375 Perl_re_printf( aTHX_
4376 "%s%s REx%s %s against %s\n",
4377 PL_colors[4], blurb, PL_colors[5], s0, s1);
4379 if (utf8_target||utf8_pat)
4380 Perl_re_printf( aTHX_ "UTF-8 %s%s%s...\n",
4381 utf8_pat ? "pattern" : "",
4382 utf8_pat && utf8_target ? " and " : "",
4383 utf8_target ? "string" : ""
4389 S_dump_exec_pos(pTHX_ const char *locinput,
4390 const regnode *scan,
4391 const char *loc_regeol,
4392 const char *loc_bostr,
4393 const char *loc_reg_starttry,
4394 const bool utf8_target,
4398 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
4399 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
4400 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
4401 /* The part of the string before starttry has one color
4402 (pref0_len chars), between starttry and current
4403 position another one (pref_len - pref0_len chars),
4404 after the current position the third one.
4405 We assume that pref0_len <= pref_len, otherwise we
4406 decrease pref0_len. */
4407 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
4408 ? (5 + taill) - l : locinput - loc_bostr;
4411 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
4413 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
4415 pref0_len = pref_len - (locinput - loc_reg_starttry);
4416 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
4417 l = ( loc_regeol - locinput > (5 + taill) - pref_len
4418 ? (5 + taill) - pref_len : loc_regeol - locinput);
4419 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
4423 if (pref0_len > pref_len)
4424 pref0_len = pref_len;
4426 const int is_uni = utf8_target ? 1 : 0;
4428 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
4429 (locinput - pref_len),pref0_len, PL_dump_re_max_len, 4, 5);
4431 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
4432 (locinput - pref_len + pref0_len),
4433 pref_len - pref0_len, PL_dump_re_max_len, 2, 3);
4435 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
4436 locinput, loc_regeol - locinput, 10, 0, 1);
4438 const STRLEN tlen=len0+len1+len2;
4439 Perl_re_printf( aTHX_
4440 "%4" IVdf " <%.*s%.*s%s%.*s>%*s|%4" UVuf "| ",
4441 (IV)(locinput - loc_bostr),
4444 (docolor ? "" : "> <"),
4446 (int)(tlen > 19 ? 0 : 19 - tlen),
4454 /* reg_check_named_buff_matched()
4455 * Checks to see if a named buffer has matched. The data array of
4456 * buffer numbers corresponding to the buffer is expected to reside
4457 * in the regexp->data->data array in the slot stored in the ARG() of
4458 * node involved. Note that this routine doesn't actually care about the
4459 * name, that information is not preserved from compilation to execution.
4460 * Returns the index of the leftmost defined buffer with the given name
4461 * or 0 if non of the buffers matched.
4464 S_reg_check_named_buff_matched(const regexp *rex, const regnode *scan)
4467 RXi_GET_DECL(rex,rexi);
4468 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4469 I32 *nums=(I32*)SvPVX(sv_dat);
4471 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
4473 for ( n=0; n<SvIVX(sv_dat); n++ ) {
4474 if ((I32)rex->lastparen >= nums[n] &&
4475 rex->offs[nums[n]].end != -1)
4484 S_setup_EXACTISH_ST(pTHX_ const regnode * const text_node,
4485 struct next_matchable_info * m,
4486 regmatch_info *reginfo)
4488 /* This function determines various characteristics about every possible
4489 * initial match of the passed-in EXACTish <text_node>, and stores them in
4492 * That includes a match string and a parallel mask, such that if you AND
4493 * the target string with the mask and compare with the match string,
4494 * you'll have a pretty good idea, perhaps even perfect, if that portion of
4495 * the target matches or not.
4497 * The motivation behind this function is to allow the caller to set up
4498 * tight loops for matching. Consider patterns like '.*B' or '.*?B' where
4499 * B is an arbitrary EXACTish node. To find the end of .*, we look for the
4500 * beginning oF B, which is the passed in <text_node> That's where this
4501 * function comes in. The values it returns can quickly be used to rule
4502 * out many, or all, cases of possible matches not actually being the
4503 * beginning of B, <text_node>. It is also used in regrepeat() where we
4504 * have 'A*', for arbitrary 'A'. This sets up criteria to more efficiently
4505 * determine where the span of 'A's stop.
4507 * If <text_node> is of type EXACT, there is only one possible character
4508 * that can match its first character, and so the situation is quite
4509 * simple. But things can get much more complicated if folding is
4510 * involved. It may&n