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 #define B_ON_NON_UTF8_LOCALE_IS_WRONG \
87 "Use of \\b{} or \\B{} for non-UTF-8 locale is wrong. Assuming a UTF-8 locale"
89 static const char utf8_locale_required[] =
90 "Use of (?[ ]) for non-UTF-8 locale is wrong. Assuming a UTF-8 locale";
93 /* At least one required character in the target string is expressible only in
95 static const char* const non_utf8_target_but_utf8_required
96 = "Can't match, because target string needs to be in UTF-8\n";
99 /* Returns a boolean as to whether the input unsigned number is a power of 2
100 * (2**0, 2**1, etc). In other words if it has just a single bit set.
101 * If not, subtracting 1 would leave the uppermost bit set, so the & would
103 #define isPOWER_OF_2(n) ((n & (n-1)) == 0)
105 #define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
106 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%s", non_utf8_target_but_utf8_required));\
110 #define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)
113 #define STATIC static
116 /* Valid only if 'c', the character being looke-up, is an invariant under
117 * UTF-8: it avoids the reginclass call if there are no complications: i.e., if
118 * everything matchable is straight forward in the bitmap */
119 #define REGINCLASS(prog,p,c,u) (ANYOF_FLAGS(p) \
120 ? reginclass(prog,p,c,c+1,u) \
121 : ANYOF_BITMAP_TEST(p,*(c)))
127 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
129 #define HOPc(pos,off) \
130 (char *)(reginfo->is_utf8_target \
131 ? reghop3((U8*)pos, off, \
132 (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
135 /* like HOPMAYBE3 but backwards. lim must be +ve. Returns NULL on overshoot */
136 #define HOPBACK3(pos, off, lim) \
137 (reginfo->is_utf8_target \
138 ? reghopmaybe3((U8*)pos, (SSize_t)0-off, (U8*)(lim)) \
139 : (pos - off >= lim) \
143 #define HOPBACKc(pos, off) ((char*)HOPBACK3(pos, off, reginfo->strbeg))
145 #define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
146 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
148 /* lim must be +ve. Returns NULL on overshoot */
149 #define HOPMAYBE3(pos,off,lim) \
150 (reginfo->is_utf8_target \
151 ? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \
152 : ((U8*)pos + off <= lim) \
156 /* like HOP3, but limits the result to <= lim even for the non-utf8 case.
157 * off must be >=0; args should be vars rather than expressions */
158 #define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
159 ? reghop3((U8*)(pos), off, (U8*)(lim)) \
160 : (U8*)((pos + off) > lim ? lim : (pos + off)))
161 #define HOP3clim(pos,off,lim) ((char*)HOP3lim(pos,off,lim))
163 #define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
164 ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
166 #define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
168 #define NEXTCHR_EOS -10 /* nextchr has fallen off the end */
169 #define NEXTCHR_IS_EOS (nextchr < 0)
171 #define SET_nextchr \
172 nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS)
174 #define SET_locinput(p) \
178 #define PLACEHOLDER /* Something for the preprocessor to grab onto */
179 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
181 /* for use after a quantifier and before an EXACT-like node -- japhy */
182 /* it would be nice to rework regcomp.sym to generate this stuff. sigh
184 * NOTE that *nothing* that affects backtracking should be in here, specifically
185 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
186 * node that is in between two EXACT like nodes when ascertaining what the required
187 * "follow" character is. This should probably be moved to regex compile time
188 * although it may be done at run time beause of the REF possibility - more
189 * investigation required. -- demerphq
191 #define JUMPABLE(rn) ( \
193 (OP(rn) == CLOSE && \
194 !EVAL_CLOSE_PAREN_IS(cur_eval,ARG(rn)) ) || \
196 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
197 OP(rn) == PLUS || OP(rn) == MINMOD || \
199 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
201 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
203 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
206 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
207 we don't need this definition. XXX These are now out-of-sync*/
208 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
209 #define IS_TEXTF(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFAA || OP(rn)==EXACTFAA_NO_TRIE || OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
210 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
213 /* ... so we use this as its faster. */
214 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==EXACTL )
215 #define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFLU8 || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFAA || OP(rn) == EXACTFAA_NO_TRIE)
216 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
217 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
222 Search for mandatory following text node; for lookahead, the text must
223 follow but for lookbehind (rn->flags != 0) we skip to the next step.
225 #define FIND_NEXT_IMPT(rn) STMT_START { \
226 while (JUMPABLE(rn)) { \
227 const OPCODE type = OP(rn); \
228 if (type == SUSPEND || PL_regkind[type] == CURLY) \
229 rn = NEXTOPER(NEXTOPER(rn)); \
230 else if (type == PLUS) \
232 else if (type == IFMATCH) \
233 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
234 else rn += NEXT_OFF(rn); \
238 #define SLAB_FIRST(s) (&(s)->states[0])
239 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
241 static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
242 static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
243 static regmatch_state * S_push_slab(pTHX);
245 #define REGCP_PAREN_ELEMS 3
246 #define REGCP_OTHER_ELEMS 3
247 #define REGCP_FRAME_ELEMS 1
248 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
249 * are needed for the regexp context stack bookkeeping. */
252 S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen _pDEPTH)
254 const int retval = PL_savestack_ix;
255 const int paren_elems_to_push =
256 (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS;
257 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
258 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
260 GET_RE_DEBUG_FLAGS_DECL;
262 PERL_ARGS_ASSERT_REGCPPUSH;
264 if (paren_elems_to_push < 0)
265 Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %u",
266 (int)paren_elems_to_push, (int)maxopenparen,
267 (int)parenfloor, (unsigned)REGCP_PAREN_ELEMS);
269 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
270 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %" UVuf
271 " out of range (%lu-%ld)",
273 (unsigned long)maxopenparen,
276 SSGROW(total_elems + REGCP_FRAME_ELEMS);
279 if ((int)maxopenparen > (int)parenfloor)
280 Perl_re_exec_indentf( aTHX_
281 "rex=0x%" UVxf " offs=0x%" UVxf ": saving capture indices:\n",
287 for (p = parenfloor+1; p <= (I32)maxopenparen; p++) {
288 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
289 SSPUSHIV(rex->offs[p].end);
290 SSPUSHIV(rex->offs[p].start);
291 SSPUSHINT(rex->offs[p].start_tmp);
292 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
293 " \\%" UVuf ": %" IVdf "(%" IVdf ")..%" IVdf "\n",
296 (IV)rex->offs[p].start,
297 (IV)rex->offs[p].start_tmp,
301 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
302 SSPUSHINT(maxopenparen);
303 SSPUSHINT(rex->lastparen);
304 SSPUSHINT(rex->lastcloseparen);
305 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
310 /* These are needed since we do not localize EVAL nodes: */
311 #define REGCP_SET(cp) \
313 Perl_re_exec_indentf( aTHX_ \
314 "Setting an EVAL scope, savestack=%" IVdf ",\n", \
315 depth, (IV)PL_savestack_ix \
320 #define REGCP_UNWIND(cp) \
322 if (cp != PL_savestack_ix) \
323 Perl_re_exec_indentf( aTHX_ \
324 "Clearing an EVAL scope, savestack=%" \
325 IVdf "..%" IVdf "\n", \
326 depth, (IV)(cp), (IV)PL_savestack_ix \
331 /* set the start and end positions of capture ix */
332 #define CLOSE_CAPTURE(ix, s, e) \
333 rex->offs[ix].start = s; \
334 rex->offs[ix].end = e; \
335 if (ix > rex->lastparen) \
336 rex->lastparen = ix; \
337 rex->lastcloseparen = ix; \
338 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_ \
339 "CLOSE: rex=0x%" UVxf " offs=0x%" UVxf ": \\%" UVuf ": set %" IVdf "..%" IVdf " max: %" UVuf "\n", \
344 (IV)rex->offs[ix].start, \
345 (IV)rex->offs[ix].end, \
349 #define UNWIND_PAREN(lp, lcp) \
350 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_ \
351 "UNWIND_PAREN: rex=0x%" UVxf " offs=0x%" UVxf ": invalidate (%" UVuf "..%" UVuf "] set lcp: %" UVuf "\n", \
356 (UV)(rex->lastparen), \
359 for (n = rex->lastparen; n > lp; n--) \
360 rex->offs[n].end = -1; \
361 rex->lastparen = n; \
362 rex->lastcloseparen = lcp;
366 S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p _pDEPTH)
370 GET_RE_DEBUG_FLAGS_DECL;
372 PERL_ARGS_ASSERT_REGCPPOP;
374 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
376 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
377 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
378 rex->lastcloseparen = SSPOPINT;
379 rex->lastparen = SSPOPINT;
380 *maxopenparen_p = SSPOPINT;
382 i -= REGCP_OTHER_ELEMS;
383 /* Now restore the parentheses context. */
385 if (i || rex->lastparen + 1 <= rex->nparens)
386 Perl_re_exec_indentf( aTHX_
387 "rex=0x%" UVxf " offs=0x%" UVxf ": restoring capture indices to:\n",
393 paren = *maxopenparen_p;
394 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
396 rex->offs[paren].start_tmp = SSPOPINT;
397 rex->offs[paren].start = SSPOPIV;
399 if (paren <= rex->lastparen)
400 rex->offs[paren].end = tmps;
401 DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
402 " \\%" UVuf ": %" IVdf "(%" IVdf ")..%" IVdf "%s\n",
405 (IV)rex->offs[paren].start,
406 (IV)rex->offs[paren].start_tmp,
407 (IV)rex->offs[paren].end,
408 (paren > rex->lastparen ? "(skipped)" : ""));
413 /* It would seem that the similar code in regtry()
414 * already takes care of this, and in fact it is in
415 * a better location to since this code can #if 0-ed out
416 * but the code in regtry() is needed or otherwise tests
417 * requiring null fields (pat.t#187 and split.t#{13,14}
418 * (as of patchlevel 7877) will fail. Then again,
419 * this code seems to be necessary or otherwise
420 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
421 * --jhi updated by dapm */
422 for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
423 if (i > *maxopenparen_p)
424 rex->offs[i].start = -1;
425 rex->offs[i].end = -1;
426 DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
427 " \\%" UVuf ": %s ..-1 undeffing\n",
430 (i > *maxopenparen_p) ? "-1" : " "
436 /* restore the parens and associated vars at savestack position ix,
437 * but without popping the stack */
440 S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p _pDEPTH)
442 I32 tmpix = PL_savestack_ix;
443 PERL_ARGS_ASSERT_REGCP_RESTORE;
445 PL_savestack_ix = ix;
446 regcppop(rex, maxopenparen_p);
447 PL_savestack_ix = tmpix;
450 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
452 #ifndef PERL_IN_XSUB_RE
455 Perl_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
457 /* Returns a boolean as to whether or not 'character' is a member of the
458 * Posix character class given by 'classnum' that should be equivalent to a
459 * value in the typedef '_char_class_number'.
461 * Ideally this could be replaced by a just an array of function pointers
462 * to the C library functions that implement the macros this calls.
463 * However, to compile, the precise function signatures are required, and
464 * these may vary from platform to to platform. To avoid having to figure
465 * out what those all are on each platform, I (khw) am using this method,
466 * which adds an extra layer of function call overhead (unless the C
467 * optimizer strips it away). But we don't particularly care about
468 * performance with locales anyway. */
470 switch ((_char_class_number) classnum) {
471 case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character);
472 case _CC_ENUM_ALPHA: return isALPHA_LC(character);
473 case _CC_ENUM_ASCII: return isASCII_LC(character);
474 case _CC_ENUM_BLANK: return isBLANK_LC(character);
475 case _CC_ENUM_CASED: return isLOWER_LC(character)
476 || isUPPER_LC(character);
477 case _CC_ENUM_CNTRL: return isCNTRL_LC(character);
478 case _CC_ENUM_DIGIT: return isDIGIT_LC(character);
479 case _CC_ENUM_GRAPH: return isGRAPH_LC(character);
480 case _CC_ENUM_LOWER: return isLOWER_LC(character);
481 case _CC_ENUM_PRINT: return isPRINT_LC(character);
482 case _CC_ENUM_PUNCT: return isPUNCT_LC(character);
483 case _CC_ENUM_SPACE: return isSPACE_LC(character);
484 case _CC_ENUM_UPPER: return isUPPER_LC(character);
485 case _CC_ENUM_WORDCHAR: return isWORDCHAR_LC(character);
486 case _CC_ENUM_XDIGIT: return isXDIGIT_LC(character);
487 default: /* VERTSPACE should never occur in locales */
488 Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum);
491 NOT_REACHED; /* NOTREACHED */
498 S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character, const U8* e)
500 /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
501 * 'character' is a member of the Posix character class given by 'classnum'
502 * that should be equivalent to a value in the typedef
503 * '_char_class_number'.
505 * This just calls isFOO_lc on the code point for the character if it is in
506 * the range 0-255. Outside that range, all characters use Unicode
507 * rules, ignoring any locale. So use the Unicode function if this class
508 * requires a swash, and use the Unicode macro otherwise. */
510 PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
512 if (UTF8_IS_INVARIANT(*character)) {
513 return isFOO_lc(classnum, *character);
515 else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
516 return isFOO_lc(classnum,
517 EIGHT_BIT_UTF8_TO_NATIVE(*character, *(character + 1)));
520 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(character, e);
522 switch ((_char_class_number) classnum) {
523 case _CC_ENUM_SPACE: return is_XPERLSPACE_high(character);
524 case _CC_ENUM_BLANK: return is_HORIZWS_high(character);
525 case _CC_ENUM_XDIGIT: return is_XDIGIT_high(character);
526 case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character);
528 return _invlist_contains_cp(PL_XPosix_ptrs[classnum],
529 utf8_to_uvchr_buf(character, e, NULL));
532 return FALSE; /* Things like CNTRL are always below 256 */
536 S_find_next_ascii(char * s, const char * send, const bool utf8_target)
538 /* Returns the position of the first ASCII byte in the sequence between 's'
539 * and 'send-1' inclusive; returns 'send' if none found */
541 PERL_ARGS_ASSERT_FIND_NEXT_ASCII;
545 if ((STRLEN) (send - s) >= PERL_WORDSIZE
547 /* This term is wordsize if subword; 0 if not */
548 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
551 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
554 /* Process per-byte until reach word boundary. XXX This loop could be
555 * eliminated if we knew that this platform had fast unaligned reads */
556 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
560 s++; /* khw didn't bother creating a separate loop for
564 /* Here, we know we have at least one full word to process. Process
565 * per-word as long as we have at least a full word left */
567 PERL_UINTMAX_T complemented = ~ * (PERL_UINTMAX_T *) s;
568 if (complemented & PERL_VARIANTS_WORD_MASK) {
570 # if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 \
571 || BYTEORDER == 0x4321 || BYTEORDER == 0x87654321
573 s += _variant_byte_number(complemented);
576 # else /* If weird byte order, drop into next loop to do byte-at-a-time
585 } while (s + PERL_WORDSIZE <= send);
590 /* Process per-character */
612 S_find_next_non_ascii(char * s, const char * send, const bool utf8_target)
614 /* Returns the position of the first non-ASCII byte in the sequence between
615 * 's' and 'send-1' inclusive; returns 'send' if none found */
619 PERL_ARGS_ASSERT_FIND_NEXT_NON_ASCII;
623 if ( ! isASCII(*s)) {
631 if ( ! isASCII(*s)) {
642 const U8 * next_non_ascii = NULL;
644 PERL_ARGS_ASSERT_FIND_NEXT_NON_ASCII;
645 PERL_UNUSED_ARG(utf8_target);
647 /* On ASCII platforms invariants and ASCII are identical, so if the string
648 * is entirely invariants, there is no non-ASCII character */
649 return (is_utf8_invariant_string_loc((U8 *) s,
653 : (char *) next_non_ascii;
660 S_find_span_end(U8 * s, const U8 * send, const U8 span_byte)
662 /* Returns the position of the first byte in the sequence between 's' and
663 * 'send-1' inclusive that isn't 'span_byte'; returns 'send' if none found.
666 PERL_ARGS_ASSERT_FIND_SPAN_END;
670 if ((STRLEN) (send - s) >= PERL_WORDSIZE
671 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
672 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
674 PERL_UINTMAX_T span_word;
676 /* Process per-byte until reach word boundary. XXX This loop could be
677 * eliminated if we knew that this platform had fast unaligned reads */
678 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
679 if (*s != span_byte) {
685 /* Create a word filled with the bytes we are spanning */
686 span_word = PERL_COUNT_MULTIPLIER * span_byte;
688 /* Process per-word as long as we have at least a full word left */
691 /* Keep going if the whole word is composed of 'span_byte's */
692 if ((* (PERL_UINTMAX_T *) s) == span_word) {
697 /* Here, at least one byte in the word isn't 'span_byte'. */
705 /* This xor leaves 1 bits only in those non-matching bytes */
706 span_word ^= * (PERL_UINTMAX_T *) s;
708 /* Make sure the upper bit of each non-matching byte is set. This
709 * makes each such byte look like an ASCII platform variant byte */
710 span_word |= span_word << 1;
711 span_word |= span_word << 2;
712 span_word |= span_word << 4;
714 /* That reduces the problem to what this function solves */
715 return s + _variant_byte_number(span_word);
719 } while (s + PERL_WORDSIZE <= send);
722 /* Process the straggler bytes beyond the final word boundary */
724 if (*s != span_byte) {
734 S_find_next_masked(U8 * s, const U8 * send, const U8 byte, const U8 mask)
736 /* Returns the position of the first byte in the sequence between 's'
737 * and 'send-1' inclusive that when ANDed with 'mask' yields 'byte';
738 * returns 'send' if none found. It uses word-level operations instead of
739 * byte to speed up the process */
741 PERL_ARGS_ASSERT_FIND_NEXT_MASKED;
744 assert((byte & mask) == byte);
748 if ((STRLEN) (send - s) >= PERL_WORDSIZE
749 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
750 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
752 PERL_UINTMAX_T word, mask_word;
754 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
755 if (((*s) & mask) == byte) {
761 word = PERL_COUNT_MULTIPLIER * byte;
762 mask_word = PERL_COUNT_MULTIPLIER * mask;
765 PERL_UINTMAX_T masked = (* (PERL_UINTMAX_T *) s) & mask_word;
767 /* If 'masked' contains bytes with the bit pattern of 'byte' within
768 * it, xoring with 'word' will leave each of the 8 bits in such
769 * bytes be 0, and no byte containing any other bit pattern will be
773 /* This causes the most significant bit to be set to 1 for any
774 * bytes in the word that aren't completely 0 */
775 masked |= masked << 1;
776 masked |= masked << 2;
777 masked |= masked << 4;
779 /* The msbits are the same as what marks a byte as variant, so we
780 * can use this mask. If all msbits are 1, the word doesn't
782 if ((masked & PERL_VARIANTS_WORD_MASK) == PERL_VARIANTS_WORD_MASK) {
787 /* Here, the msbit of bytes in the word that aren't 'byte' are 1,
788 * and any that are, are 0. Complement and re-AND to swap that */
790 masked &= PERL_VARIANTS_WORD_MASK;
792 /* This reduces the problem to that solved by this function */
793 s += _variant_byte_number(masked);
796 } while (s + PERL_WORDSIZE <= send);
802 if (((*s) & mask) == byte) {
812 S_find_span_end_mask(U8 * s, const U8 * send, const U8 span_byte, const U8 mask)
814 /* Returns the position of the first byte in the sequence between 's' and
815 * 'send-1' inclusive that when ANDed with 'mask' isn't 'span_byte'.
816 * 'span_byte' should have been ANDed with 'mask' in the call of this
817 * function. Returns 'send' if none found. Works like find_span_end(),
818 * except for the AND */
820 PERL_ARGS_ASSERT_FIND_SPAN_END_MASK;
823 assert((span_byte & mask) == span_byte);
825 if ((STRLEN) (send - s) >= PERL_WORDSIZE
826 + PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
827 - (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
829 PERL_UINTMAX_T span_word, mask_word;
831 while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
832 if (((*s) & mask) != span_byte) {
838 span_word = PERL_COUNT_MULTIPLIER * span_byte;
839 mask_word = PERL_COUNT_MULTIPLIER * mask;
842 PERL_UINTMAX_T masked = (* (PERL_UINTMAX_T *) s) & mask_word;
844 if (masked == span_word) {
856 masked |= masked << 1;
857 masked |= masked << 2;
858 masked |= masked << 4;
859 return s + _variant_byte_number(masked);
863 } while (s + PERL_WORDSIZE <= send);
867 if (((*s) & mask) != span_byte) {
877 * pregexec and friends
880 #ifndef PERL_IN_XSUB_RE
882 - pregexec - match a regexp against a string
885 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
886 char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
887 /* stringarg: the point in the string at which to begin matching */
888 /* strend: pointer to null at end of string */
889 /* strbeg: real beginning of string */
890 /* minend: end of match must be >= minend bytes after stringarg. */
891 /* screamer: SV being matched: only used for utf8 flag, pos() etc; string
892 * itself is accessed via the pointers above */
893 /* nosave: For optimizations. */
895 PERL_ARGS_ASSERT_PREGEXEC;
898 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
899 nosave ? 0 : REXEC_COPY_STR);
905 /* re_intuit_start():
907 * Based on some optimiser hints, try to find the earliest position in the
908 * string where the regex could match.
910 * rx: the regex to match against
911 * sv: the SV being matched: only used for utf8 flag; the string
912 * itself is accessed via the pointers below. Note that on
913 * something like an overloaded SV, SvPOK(sv) may be false
914 * and the string pointers may point to something unrelated to
916 * strbeg: real beginning of string
917 * strpos: the point in the string at which to begin matching
918 * strend: pointer to the byte following the last char of the string
919 * flags currently unused; set to 0
920 * data: currently unused; set to NULL
922 * The basic idea of re_intuit_start() is to use some known information
923 * about the pattern, namely:
925 * a) the longest known anchored substring (i.e. one that's at a
926 * constant offset from the beginning of the pattern; but not
927 * necessarily at a fixed offset from the beginning of the
929 * b) the longest floating substring (i.e. one that's not at a constant
930 * offset from the beginning of the pattern);
931 * c) Whether the pattern is anchored to the string; either
932 * an absolute anchor: /^../, or anchored to \n: /^.../m,
933 * or anchored to pos(): /\G/;
934 * d) A start class: a real or synthetic character class which
935 * represents which characters are legal at the start of the pattern;
937 * to either quickly reject the match, or to find the earliest position
938 * within the string at which the pattern might match, thus avoiding
939 * running the full NFA engine at those earlier locations, only to
940 * eventually fail and retry further along.
942 * Returns NULL if the pattern can't match, or returns the address within
943 * the string which is the earliest place the match could occur.
945 * The longest of the anchored and floating substrings is called 'check'
946 * and is checked first. The other is called 'other' and is checked
947 * second. The 'other' substring may not be present. For example,
949 * /(abc|xyz)ABC\d{0,3}DEFG/
953 * check substr (float) = "DEFG", offset 6..9 chars
954 * other substr (anchored) = "ABC", offset 3..3 chars
957 * Be aware that during the course of this function, sometimes 'anchored'
958 * refers to a substring being anchored relative to the start of the
959 * pattern, and sometimes to the pattern itself being anchored relative to
960 * the string. For example:
962 * /\dabc/: "abc" is anchored to the pattern;
963 * /^\dabc/: "abc" is anchored to the pattern and the string;
964 * /\d+abc/: "abc" is anchored to neither the pattern nor the string;
965 * /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
966 * but the pattern is anchored to the string.
970 Perl_re_intuit_start(pTHX_
973 const char * const strbeg,
977 re_scream_pos_data *data)
979 struct regexp *const prog = ReANY(rx);
980 SSize_t start_shift = prog->check_offset_min;
981 /* Should be nonnegative! */
982 SSize_t end_shift = 0;
983 /* current lowest pos in string where the regex can start matching */
984 char *rx_origin = strpos;
986 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
987 U8 other_ix = 1 - prog->substrs->check_ix;
989 char *other_last = strpos;/* latest pos 'other' substr already checked to */
990 char *check_at = NULL; /* check substr found at this pos */
991 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
992 RXi_GET_DECL(prog,progi);
993 regmatch_info reginfo_buf; /* create some info to pass to find_byclass */
994 regmatch_info *const reginfo = ®info_buf;
995 GET_RE_DEBUG_FLAGS_DECL;
997 PERL_ARGS_ASSERT_RE_INTUIT_START;
998 PERL_UNUSED_ARG(flags);
999 PERL_UNUSED_ARG(data);
1001 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1002 "Intuit: trying to determine minimum start position...\n"));
1004 /* for now, assume that all substr offsets are positive. If at some point
1005 * in the future someone wants to do clever things with lookbehind and
1006 * -ve offsets, they'll need to fix up any code in this function
1007 * which uses these offsets. See the thread beginning
1008 * <20140113145929.GF27210@iabyn.com>
1010 assert(prog->substrs->data[0].min_offset >= 0);
1011 assert(prog->substrs->data[0].max_offset >= 0);
1012 assert(prog->substrs->data[1].min_offset >= 0);
1013 assert(prog->substrs->data[1].max_offset >= 0);
1014 assert(prog->substrs->data[2].min_offset >= 0);
1015 assert(prog->substrs->data[2].max_offset >= 0);
1017 /* for now, assume that if both present, that the floating substring
1018 * doesn't start before the anchored substring.
1019 * If you break this assumption (e.g. doing better optimisations
1020 * with lookahead/behind), then you'll need to audit the code in this
1021 * function carefully first
1024 ! ( (prog->anchored_utf8 || prog->anchored_substr)
1025 && (prog->float_utf8 || prog->float_substr))
1026 || (prog->float_min_offset >= prog->anchored_offset));
1028 /* byte rather than char calculation for efficiency. It fails
1029 * to quickly reject some cases that can't match, but will reject
1030 * them later after doing full char arithmetic */
1031 if (prog->minlen > strend - strpos) {
1032 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1033 " String too short...\n"));
1037 RXp_MATCH_UTF8_set(prog, utf8_target);
1038 reginfo->is_utf8_target = cBOOL(utf8_target);
1039 reginfo->info_aux = NULL;
1040 reginfo->strbeg = strbeg;
1041 reginfo->strend = strend;
1042 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
1043 reginfo->intuit = 1;
1044 /* not actually used within intuit, but zero for safety anyway */
1045 reginfo->poscache_maxiter = 0;
1048 if ((!prog->anchored_utf8 && prog->anchored_substr)
1049 || (!prog->float_utf8 && prog->float_substr))
1050 to_utf8_substr(prog);
1051 check = prog->check_utf8;
1053 if (!prog->check_substr && prog->check_utf8) {
1054 if (! to_byte_substr(prog)) {
1055 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
1058 check = prog->check_substr;
1061 /* dump the various substring data */
1062 DEBUG_OPTIMISE_MORE_r({
1064 for (i=0; i<=2; i++) {
1065 SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr
1066 : prog->substrs->data[i].substr);
1070 Perl_re_printf( aTHX_
1071 " substrs[%d]: min=%" IVdf " max=%" IVdf " end shift=%" IVdf
1072 " useful=%" IVdf " utf8=%d [%s]\n",
1074 (IV)prog->substrs->data[i].min_offset,
1075 (IV)prog->substrs->data[i].max_offset,
1076 (IV)prog->substrs->data[i].end_shift,
1078 utf8_target ? 1 : 0,
1083 if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */
1085 /* ml_anch: check after \n?
1087 * A note about PREGf_IMPLICIT: on an un-anchored pattern beginning
1088 * with /.*.../, these flags will have been added by the
1090 * /.*abc/, /.*abc/m: PREGf_IMPLICIT | PREGf_ANCH_MBOL
1091 * /.*abc/s: PREGf_IMPLICIT | PREGf_ANCH_SBOL
1093 ml_anch = (prog->intflags & PREGf_ANCH_MBOL)
1094 && !(prog->intflags & PREGf_IMPLICIT);
1096 if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) {
1097 /* we are only allowed to match at BOS or \G */
1099 /* trivially reject if there's a BOS anchor and we're not at BOS.
1101 * Note that we don't try to do a similar quick reject for
1102 * \G, since generally the caller will have calculated strpos
1103 * based on pos() and gofs, so the string is already correctly
1104 * anchored by definition; and handling the exceptions would
1105 * be too fiddly (e.g. REXEC_IGNOREPOS).
1107 if ( strpos != strbeg
1108 && (prog->intflags & PREGf_ANCH_SBOL))
1110 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1111 " Not at start...\n"));
1115 /* in the presence of an anchor, the anchored (relative to the
1116 * start of the regex) substr must also be anchored relative
1117 * to strpos. So quickly reject if substr isn't found there.
1118 * This works for \G too, because the caller will already have
1119 * subtracted gofs from pos, and gofs is the offset from the
1120 * \G to the start of the regex. For example, in /.abc\Gdef/,
1121 * where substr="abcdef", pos()=3, gofs=4, offset_min=1:
1122 * caller will have set strpos=pos()-4; we look for the substr
1123 * at position pos()-4+1, which lines up with the "a" */
1125 if (prog->check_offset_min == prog->check_offset_max) {
1126 /* Substring at constant offset from beg-of-str... */
1127 SSize_t slen = SvCUR(check);
1128 char *s = HOP3c(strpos, prog->check_offset_min, strend);
1130 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1131 " Looking for check substr at fixed offset %" IVdf "...\n",
1132 (IV)prog->check_offset_min));
1134 if (SvTAIL(check)) {
1135 /* In this case, the regex is anchored at the end too.
1136 * Unless it's a multiline match, the lengths must match
1137 * exactly, give or take a \n. NB: slen >= 1 since
1138 * the last char of check is \n */
1140 && ( strend - s > slen
1141 || strend - s < slen - 1
1142 || (strend - s == slen && strend[-1] != '\n')))
1144 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1145 " String too long...\n"));
1148 /* Now should match s[0..slen-2] */
1151 if (slen && (strend - s < slen
1152 || *SvPVX_const(check) != *s
1153 || (slen > 1 && (memNE(SvPVX_const(check), s, slen)))))
1155 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1156 " String not equal...\n"));
1161 goto success_at_start;
1166 end_shift = prog->check_end_shift;
1168 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
1170 Perl_croak(aTHX_ "panic: end_shift: %" IVdf " pattern:\n%s\n ",
1171 (IV)end_shift, RX_PRECOMP(rx));
1176 /* This is the (re)entry point of the main loop in this function.
1177 * The goal of this loop is to:
1178 * 1) find the "check" substring in the region rx_origin..strend
1179 * (adjusted by start_shift / end_shift). If not found, reject
1181 * 2) If it exists, look for the "other" substr too if defined; for
1182 * example, if the check substr maps to the anchored substr, then
1183 * check the floating substr, and vice-versa. If not found, go
1184 * back to (1) with rx_origin suitably incremented.
1185 * 3) If we find an rx_origin position that doesn't contradict
1186 * either of the substrings, then check the possible additional
1187 * constraints on rx_origin of /^.../m or a known start class.
1188 * If these fail, then depending on which constraints fail, jump
1189 * back to here, or to various other re-entry points further along
1190 * that skip some of the first steps.
1191 * 4) If we pass all those tests, update the BmUSEFUL() count on the
1192 * substring. If the start position was determined to be at the
1193 * beginning of the string - so, not rejected, but not optimised,
1194 * since we have to run regmatch from position 0 - decrement the
1195 * BmUSEFUL() count. Otherwise increment it.
1199 /* first, look for the 'check' substring */
1205 DEBUG_OPTIMISE_MORE_r({
1206 Perl_re_printf( aTHX_
1207 " At restart: rx_origin=%" IVdf " Check offset min: %" IVdf
1208 " Start shift: %" IVdf " End shift %" IVdf
1209 " Real end Shift: %" IVdf "\n",
1210 (IV)(rx_origin - strbeg),
1211 (IV)prog->check_offset_min,
1214 (IV)prog->check_end_shift);
1217 end_point = HOPBACK3(strend, end_shift, rx_origin);
1220 start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
1225 /* If the regex is absolutely anchored to either the start of the
1226 * string (SBOL) or to pos() (ANCH_GPOS), then
1227 * check_offset_max represents an upper bound on the string where
1228 * the substr could start. For the ANCH_GPOS case, we assume that
1229 * the caller of intuit will have already set strpos to
1230 * pos()-gofs, so in this case strpos + offset_max will still be
1231 * an upper bound on the substr.
1234 && prog->intflags & PREGf_ANCH
1235 && prog->check_offset_max != SSize_t_MAX)
1237 SSize_t check_len = SvCUR(check) - !!SvTAIL(check);
1238 const char * const anchor =
1239 (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg);
1240 SSize_t targ_len = (char*)end_point - anchor;
1242 if (check_len > targ_len) {
1243 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1244 "Target string too short to match required substring...\n"));
1248 /* do a bytes rather than chars comparison. It's conservative;
1249 * so it skips doing the HOP if the result can't possibly end
1250 * up earlier than the old value of end_point.
1252 assert(anchor + check_len <= (char *)end_point);
1253 if (prog->check_offset_max + check_len < targ_len) {
1254 end_point = HOP3lim((U8*)anchor,
1255 prog->check_offset_max,
1256 end_point - check_len
1259 if (end_point < start_point)
1264 check_at = fbm_instr( start_point, end_point,
1265 check, multiline ? FBMrf_MULTILINE : 0);
1267 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1268 " doing 'check' fbm scan, [%" IVdf "..%" IVdf "] gave %" IVdf "\n",
1269 (IV)((char*)start_point - strbeg),
1270 (IV)((char*)end_point - strbeg),
1271 (IV)(check_at ? check_at - strbeg : -1)
1274 /* Update the count-of-usability, remove useless subpatterns,
1278 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1279 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
1280 Perl_re_printf( aTHX_ " %s %s substr %s%s%s",
1281 (check_at ? "Found" : "Did not find"),
1282 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
1283 ? "anchored" : "floating"),
1286 (check_at ? " at offset " : "...\n") );
1291 /* set rx_origin to the minimum position where the regex could start
1292 * matching, given the constraint of the just-matched check substring.
1293 * But don't set it lower than previously.
1296 if (check_at - rx_origin > prog->check_offset_max)
1297 rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin);
1298 /* Finish the diagnostic message */
1299 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1300 "%ld (rx_origin now %" IVdf ")...\n",
1301 (long)(check_at - strbeg),
1302 (IV)(rx_origin - strbeg)
1307 /* now look for the 'other' substring if defined */
1309 if (utf8_target ? prog->substrs->data[other_ix].utf8_substr
1310 : prog->substrs->data[other_ix].substr)
1312 /* Take into account the "other" substring. */
1316 struct reg_substr_datum *other;
1319 other = &prog->substrs->data[other_ix];
1321 /* if "other" is anchored:
1322 * we've previously found a floating substr starting at check_at.
1323 * This means that the regex origin must lie somewhere
1324 * between min (rx_origin): HOP3(check_at, -check_offset_max)
1325 * and max: HOP3(check_at, -check_offset_min)
1326 * (except that min will be >= strpos)
1327 * So the fixed substr must lie somewhere between
1328 * HOP3(min, anchored_offset)
1329 * HOP3(max, anchored_offset) + SvCUR(substr)
1332 /* if "other" is floating
1333 * Calculate last1, the absolute latest point where the
1334 * floating substr could start in the string, ignoring any
1335 * constraints from the earlier fixed match. It is calculated
1338 * strend - prog->minlen (in chars) is the absolute latest
1339 * position within the string where the origin of the regex
1340 * could appear. The latest start point for the floating
1341 * substr is float_min_offset(*) on from the start of the
1342 * regex. last1 simply combines thee two offsets.
1344 * (*) You might think the latest start point should be
1345 * float_max_offset from the regex origin, and technically
1346 * you'd be correct. However, consider
1348 * Here, float min, max are 3,5 and minlen is 7.
1349 * This can match either
1353 * In the first case, the regex matches minlen chars; in the
1354 * second, minlen+1, in the third, minlen+2.
1355 * In the first case, the floating offset is 3 (which equals
1356 * float_min), in the second, 4, and in the third, 5 (which
1357 * equals float_max). In all cases, the floating string bcd
1358 * can never start more than 4 chars from the end of the
1359 * string, which equals minlen - float_min. As the substring
1360 * starts to match more than float_min from the start of the
1361 * regex, it makes the regex match more than minlen chars,
1362 * and the two cancel each other out. So we can always use
1363 * float_min - minlen, rather than float_max - minlen for the
1364 * latest position in the string.
1366 * Note that -minlen + float_min_offset is equivalent (AFAIKT)
1367 * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift
1370 assert(prog->minlen >= other->min_offset);
1371 last1 = HOP3c(strend,
1372 other->min_offset - prog->minlen, strbeg);
1374 if (other_ix) {/* i.e. if (other-is-float) */
1375 /* last is the latest point where the floating substr could
1376 * start, *given* any constraints from the earlier fixed
1377 * match. This constraint is that the floating string starts
1378 * <= float_max_offset chars from the regex origin (rx_origin).
1379 * If this value is less than last1, use it instead.
1381 assert(rx_origin <= last1);
1383 /* this condition handles the offset==infinity case, and
1384 * is a short-cut otherwise. Although it's comparing a
1385 * byte offset to a char length, it does so in a safe way,
1386 * since 1 char always occupies 1 or more bytes,
1387 * so if a string range is (last1 - rx_origin) bytes,
1388 * it will be less than or equal to (last1 - rx_origin)
1389 * chars; meaning it errs towards doing the accurate HOP3
1390 * rather than just using last1 as a short-cut */
1391 (last1 - rx_origin) < other->max_offset
1393 : (char*)HOP3lim(rx_origin, other->max_offset, last1);
1396 assert(strpos + start_shift <= check_at);
1397 last = HOP4c(check_at, other->min_offset - start_shift,
1401 s = HOP3c(rx_origin, other->min_offset, strend);
1402 if (s < other_last) /* These positions already checked */
1405 must = utf8_target ? other->utf8_substr : other->substr;
1406 assert(SvPOK(must));
1409 char *to = last + SvCUR(must) - (SvTAIL(must)!=0);
1415 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1416 " skipping 'other' fbm scan: %" IVdf " > %" IVdf "\n",
1417 (IV)(from - strbeg),
1423 (unsigned char*)from,
1426 multiline ? FBMrf_MULTILINE : 0
1428 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1429 " doing 'other' fbm scan, [%" IVdf "..%" IVdf "] gave %" IVdf "\n",
1430 (IV)(from - strbeg),
1432 (IV)(s ? s - strbeg : -1)
1438 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1439 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
1440 Perl_re_printf( aTHX_ " %s %s substr %s%s",
1441 s ? "Found" : "Contradicts",
1442 other_ix ? "floating" : "anchored",
1443 quoted, RE_SV_TAIL(must));
1448 /* last1 is latest possible substr location. If we didn't
1449 * find it before there, we never will */
1450 if (last >= last1) {
1451 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1452 "; giving up...\n"));
1456 /* try to find the check substr again at a later
1457 * position. Maybe next time we'll find the "other" substr
1459 other_last = HOP3c(last, 1, strend) /* highest failure */;
1461 other_ix /* i.e. if other-is-float */
1462 ? HOP3c(rx_origin, 1, strend)
1463 : HOP4c(last, 1 - other->min_offset, strbeg, strend);
1464 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1465 "; about to retry %s at offset %ld (rx_origin now %" IVdf ")...\n",
1466 (other_ix ? "floating" : "anchored"),
1467 (long)(HOP3c(check_at, 1, strend) - strbeg),
1468 (IV)(rx_origin - strbeg)
1473 if (other_ix) { /* if (other-is-float) */
1474 /* other_last is set to s, not s+1, since its possible for
1475 * a floating substr to fail first time, then succeed
1476 * second time at the same floating position; e.g.:
1477 * "-AB--AABZ" =~ /\wAB\d*Z/
1478 * The first time round, anchored and float match at
1479 * "-(AB)--AAB(Z)" then fail on the initial \w character
1480 * class. Second time round, they match at "-AB--A(AB)(Z)".
1485 rx_origin = HOP3c(s, -other->min_offset, strbeg);
1486 other_last = HOP3c(s, 1, strend);
1488 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1489 " at offset %ld (rx_origin now %" IVdf ")...\n",
1491 (IV)(rx_origin - strbeg)
1497 DEBUG_OPTIMISE_MORE_r(
1498 Perl_re_printf( aTHX_
1499 " Check-only match: offset min:%" IVdf " max:%" IVdf
1500 " check_at:%" IVdf " rx_origin:%" IVdf " rx_origin-check_at:%" IVdf
1501 " strend:%" IVdf "\n",
1502 (IV)prog->check_offset_min,
1503 (IV)prog->check_offset_max,
1504 (IV)(check_at-strbeg),
1505 (IV)(rx_origin-strbeg),
1506 (IV)(rx_origin-check_at),
1512 postprocess_substr_matches:
1514 /* handle the extra constraint of /^.../m if present */
1516 if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') {
1519 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1520 " looking for /^/m anchor"));
1522 /* we have failed the constraint of a \n before rx_origin.
1523 * Find the next \n, if any, even if it's beyond the current
1524 * anchored and/or floating substrings. Whether we should be
1525 * scanning ahead for the next \n or the next substr is debatable.
1526 * On the one hand you'd expect rare substrings to appear less
1527 * often than \n's. On the other hand, searching for \n means
1528 * we're effectively flipping between check_substr and "\n" on each
1529 * iteration as the current "rarest" string candidate, which
1530 * means for example that we'll quickly reject the whole string if
1531 * hasn't got a \n, rather than trying every substr position
1535 s = HOP3c(strend, - prog->minlen, strpos);
1536 if (s <= rx_origin ||
1537 ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin)))
1539 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1540 " Did not find /%s^%s/m...\n",
1541 PL_colors[0], PL_colors[1]));
1545 /* earliest possible origin is 1 char after the \n.
1546 * (since *rx_origin == '\n', it's safe to ++ here rather than
1547 * HOP(rx_origin, 1)) */
1550 if (prog->substrs->check_ix == 0 /* check is anchored */
1551 || rx_origin >= HOP3c(check_at, - prog->check_offset_min, strpos))
1553 /* Position contradicts check-string; either because
1554 * check was anchored (and thus has no wiggle room),
1555 * or check was float and rx_origin is above the float range */
1556 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1557 " Found /%s^%s/m, about to restart lookup for check-string with rx_origin %ld...\n",
1558 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
1562 /* if we get here, the check substr must have been float,
1563 * is in range, and we may or may not have had an anchored
1564 * "other" substr which still contradicts */
1565 assert(prog->substrs->check_ix); /* check is float */
1567 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
1568 /* whoops, the anchored "other" substr exists, so we still
1569 * contradict. On the other hand, the float "check" substr
1570 * didn't contradict, so just retry the anchored "other"
1572 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1573 " Found /%s^%s/m, rescanning for anchored from offset %" IVdf " (rx_origin now %" IVdf ")...\n",
1574 PL_colors[0], PL_colors[1],
1575 (IV)(rx_origin - strbeg + prog->anchored_offset),
1576 (IV)(rx_origin - strbeg)
1578 goto do_other_substr;
1581 /* success: we don't contradict the found floating substring
1582 * (and there's no anchored substr). */
1583 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1584 " Found /%s^%s/m with rx_origin %ld...\n",
1585 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
1588 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1589 " (multiline anchor test skipped)\n"));
1595 /* if we have a starting character class, then test that extra constraint.
1596 * (trie stclasses are too expensive to use here, we are better off to
1597 * leave it to regmatch itself) */
1599 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1600 const U8* const str = (U8*)STRING(progi->regstclass);
1602 /* XXX this value could be pre-computed */
1603 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1604 ? (reginfo->is_utf8_pat
1605 ? utf8_distance(str + STR_LEN(progi->regstclass), str)
1606 : STR_LEN(progi->regstclass))
1610 /* latest pos that a matching float substr constrains rx start to */
1611 char *rx_max_float = NULL;
1613 /* if the current rx_origin is anchored, either by satisfying an
1614 * anchored substring constraint, or a /^.../m constraint, then we
1615 * can reject the current origin if the start class isn't found
1616 * at the current position. If we have a float-only match, then
1617 * rx_origin is constrained to a range; so look for the start class
1618 * in that range. if neither, then look for the start class in the
1619 * whole rest of the string */
1621 /* XXX DAPM it's not clear what the minlen test is for, and why
1622 * it's not used in the floating case. Nothing in the test suite
1623 * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>.
1624 * Here are some old comments, which may or may not be correct:
1626 * minlen == 0 is possible if regstclass is \b or \B,
1627 * and the fixed substr is ''$.
1628 * Since minlen is already taken into account, rx_origin+1 is
1629 * before strend; accidentally, minlen >= 1 guaranties no false
1630 * positives at rx_origin + 1 even for \b or \B. But (minlen? 1 :
1631 * 0) below assumes that regstclass does not come from lookahead...
1632 * If regstclass takes bytelength more than 1: If charlength==1, OK.
1633 * This leaves EXACTF-ish only, which are dealt with in
1637 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1638 endpos = HOP3clim(rx_origin, (prog->minlen ? cl_l : 0), strend);
1639 else if (prog->float_substr || prog->float_utf8) {
1640 rx_max_float = HOP3c(check_at, -start_shift, strbeg);
1641 endpos = HOP3clim(rx_max_float, cl_l, strend);
1646 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1647 " looking for class: start_shift: %" IVdf " check_at: %" IVdf
1648 " rx_origin: %" IVdf " endpos: %" IVdf "\n",
1649 (IV)start_shift, (IV)(check_at - strbeg),
1650 (IV)(rx_origin - strbeg), (IV)(endpos - strbeg)));
1652 s = find_byclass(prog, progi->regstclass, rx_origin, endpos,
1655 if (endpos == strend) {
1656 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1657 " Could not match STCLASS...\n") );
1660 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1661 " This position contradicts STCLASS...\n") );
1662 if ((prog->intflags & PREGf_ANCH) && !ml_anch
1663 && !(prog->intflags & PREGf_IMPLICIT))
1666 /* Contradict one of substrings */
1667 if (prog->anchored_substr || prog->anchored_utf8) {
1668 if (prog->substrs->check_ix == 1) { /* check is float */
1669 /* Have both, check_string is floating */
1670 assert(rx_origin + start_shift <= check_at);
1671 if (rx_origin + start_shift != check_at) {
1672 /* not at latest position float substr could match:
1673 * Recheck anchored substring, but not floating.
1674 * The condition above is in bytes rather than
1675 * chars for efficiency. It's conservative, in
1676 * that it errs on the side of doing 'goto
1677 * do_other_substr'. In this case, at worst,
1678 * an extra anchored search may get done, but in
1679 * practice the extra fbm_instr() is likely to
1680 * get skipped anyway. */
1681 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1682 " about to retry anchored at offset %ld (rx_origin now %" IVdf ")...\n",
1683 (long)(other_last - strbeg),
1684 (IV)(rx_origin - strbeg)
1686 goto do_other_substr;
1694 /* In the presence of ml_anch, we might be able to
1695 * find another \n without breaking the current float
1698 /* strictly speaking this should be HOP3c(..., 1, ...),
1699 * but since we goto a block of code that's going to
1700 * search for the next \n if any, its safe here */
1702 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1703 " about to look for /%s^%s/m starting at rx_origin %ld...\n",
1704 PL_colors[0], PL_colors[1],
1705 (long)(rx_origin - strbeg)) );
1706 goto postprocess_substr_matches;
1709 /* strictly speaking this can never be true; but might
1710 * be if we ever allow intuit without substrings */
1711 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
1714 rx_origin = rx_max_float;
1717 /* at this point, any matching substrings have been
1718 * contradicted. Start again... */
1720 rx_origin = HOP3c(rx_origin, 1, strend);
1722 /* uses bytes rather than char calculations for efficiency.
1723 * It's conservative: it errs on the side of doing 'goto restart',
1724 * where there is code that does a proper char-based test */
1725 if (rx_origin + start_shift + end_shift > strend) {
1726 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1727 " Could not match STCLASS...\n") );
1730 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1731 " about to look for %s substr starting at offset %ld (rx_origin now %" IVdf ")...\n",
1732 (prog->substrs->check_ix ? "floating" : "anchored"),
1733 (long)(rx_origin + start_shift - strbeg),
1734 (IV)(rx_origin - strbeg)
1741 if (rx_origin != s) {
1742 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1743 " By STCLASS: moving %ld --> %ld\n",
1744 (long)(rx_origin - strbeg), (long)(s - strbeg))
1748 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1749 " Does not contradict STCLASS...\n");
1754 /* Decide whether using the substrings helped */
1756 if (rx_origin != strpos) {
1757 /* Fixed substring is found far enough so that the match
1758 cannot start at strpos. */
1760 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ " try at offset...\n"));
1761 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
1764 /* The found rx_origin position does not prohibit matching at
1765 * strpos, so calling intuit didn't gain us anything. Decrement
1766 * the BmUSEFUL() count on the check substring, and if we reach
1768 if (!(prog->intflags & PREGf_NAUGHTY)
1770 prog->check_utf8 /* Could be deleted already */
1771 && --BmUSEFUL(prog->check_utf8) < 0
1772 && (prog->check_utf8 == prog->float_utf8)
1774 prog->check_substr /* Could be deleted already */
1775 && --BmUSEFUL(prog->check_substr) < 0
1776 && (prog->check_substr == prog->float_substr)
1779 /* If flags & SOMETHING - do not do it many times on the same match */
1780 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ " ... Disabling check substring...\n"));
1781 /* XXX Does the destruction order has to change with utf8_target? */
1782 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1783 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1784 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1785 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1786 check = NULL; /* abort */
1787 /* XXXX This is a remnant of the old implementation. It
1788 looks wasteful, since now INTUIT can use many
1789 other heuristics. */
1790 prog->extflags &= ~RXf_USE_INTUIT;
1794 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1795 "Intuit: %sSuccessfully guessed:%s match at offset %ld\n",
1796 PL_colors[4], PL_colors[5], (long)(rx_origin - strbeg)) );
1800 fail_finish: /* Substring not found */
1801 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1802 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1804 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%sMatch rejected by optimizer%s\n",
1805 PL_colors[4], PL_colors[5]));
1810 #define DECL_TRIE_TYPE(scan) \
1811 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \
1812 trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold, \
1813 trie_utf8l, trie_flu8, trie_flu8_latin } \
1814 trie_type = ((scan->flags == EXACT) \
1815 ? (utf8_target ? trie_utf8 : trie_plain) \
1816 : (scan->flags == EXACTL) \
1817 ? (utf8_target ? trie_utf8l : trie_plain) \
1818 : (scan->flags == EXACTFAA) \
1820 ? trie_utf8_exactfa_fold \
1821 : trie_latin_utf8_exactfa_fold) \
1822 : (scan->flags == EXACTFLU8 \
1825 : trie_flu8_latin) \
1828 : trie_latin_utf8_fold)))
1830 /* 'uscan' is set to foldbuf, and incremented, so below the end of uscan is
1831 * 'foldbuf+sizeof(foldbuf)' */
1832 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uc_end, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \
1835 U8 flags = FOLD_FLAGS_FULL; \
1836 switch (trie_type) { \
1838 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
1839 if (UTF8_IS_ABOVE_LATIN1(*uc)) { \
1840 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc_end); \
1842 goto do_trie_utf8_fold; \
1843 case trie_utf8_exactfa_fold: \
1844 flags |= FOLD_FLAGS_NOMIX_ASCII; \
1846 case trie_utf8_fold: \
1847 do_trie_utf8_fold: \
1848 if ( foldlen>0 ) { \
1849 uvc = utf8n_to_uvchr( (const U8*) uscan, foldlen, &len, uniflags ); \
1854 uvc = _toFOLD_utf8_flags( (const U8*) uc, uc_end, foldbuf, &foldlen, \
1856 len = UTF8SKIP(uc); \
1857 skiplen = UVCHR_SKIP( uvc ); \
1858 foldlen -= skiplen; \
1859 uscan = foldbuf + skiplen; \
1862 case trie_flu8_latin: \
1863 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
1864 goto do_trie_latin_utf8_fold; \
1865 case trie_latin_utf8_exactfa_fold: \
1866 flags |= FOLD_FLAGS_NOMIX_ASCII; \
1868 case trie_latin_utf8_fold: \
1869 do_trie_latin_utf8_fold: \
1870 if ( foldlen>0 ) { \
1871 uvc = utf8n_to_uvchr( (const U8*) uscan, foldlen, &len, uniflags ); \
1877 uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags); \
1878 skiplen = UVCHR_SKIP( uvc ); \
1879 foldlen -= skiplen; \
1880 uscan = foldbuf + skiplen; \
1884 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
1885 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \
1886 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \
1890 uvc = utf8n_to_uvchr( (const U8*) uc, uc_end - uc, &len, uniflags ); \
1897 charid = trie->charmap[ uvc ]; \
1901 if (widecharmap) { \
1902 SV** const svpp = hv_fetch(widecharmap, \
1903 (char*)&uvc, sizeof(UV), 0); \
1905 charid = (U16)SvIV(*svpp); \
1910 #define DUMP_EXEC_POS(li,s,doutf8,depth) \
1911 dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \
1912 startpos, doutf8, depth)
1914 #define REXEC_FBC_SCAN(UTF8, CODE) \
1916 while (s < strend) { \
1918 s += ((UTF8) ? UTF8SKIP(s) : 1); \
1922 #define REXEC_FBC_CLASS_SCAN(UTF8, COND) \
1924 while (s < strend) { \
1925 REXEC_FBC_CLASS_SCAN_GUTS(UTF8, COND) \
1929 #define REXEC_FBC_CLASS_SCAN_GUTS(UTF8, COND) \
1932 s += ((UTF8) ? UTF8SKIP(s) : 1); \
1933 previous_occurrence_end = s; \
1936 s += ((UTF8) ? UTF8SKIP(s) : 1); \
1939 #define REXEC_FBC_CSCAN(CONDUTF8,COND) \
1940 if (utf8_target) { \
1941 REXEC_FBC_CLASS_SCAN(1, CONDUTF8); \
1944 REXEC_FBC_CLASS_SCAN(0, COND); \
1947 /* We keep track of where the next character should start after an occurrence
1948 * of the one we're looking for. Knowing that, we can see right away if the
1949 * next occurrence is adjacent to the previous. When 'doevery' is FALSE, we
1950 * don't accept the 2nd and succeeding adjacent occurrences */
1951 #define FBC_CHECK_AND_TRY \
1953 || s != previous_occurrence_end) \
1954 && (reginfo->intuit || regtry(reginfo, &s))) \
1960 /* This differs from the above macros in that it calls a function which returns
1961 * the next occurrence of the thing being looked for in 's'; and 'strend' if
1962 * there is no such occurrence. */
1963 #define REXEC_FBC_FIND_NEXT_SCAN(UTF8, f) \
1964 while (s < strend) { \
1966 if (s >= strend) { \
1971 s += (UTF8) ? UTF8SKIP(s) : 1; \
1972 previous_occurrence_end = s; \
1975 /* The three macros below are slightly different versions of the same logic.
1977 * The first is for /a and /aa when the target string is UTF-8. This can only
1978 * match ascii, but it must advance based on UTF-8. The other two handle the
1979 * non-UTF-8 and the more generic UTF-8 cases. In all three, we are looking
1980 * for the boundary (or non-boundary) between a word and non-word character.
1981 * The utf8 and non-utf8 cases have the same logic, but the details must be
1982 * different. Find the "wordness" of the character just prior to this one, and
1983 * compare it with the wordness of this one. If they differ, we have a
1984 * boundary. At the beginning of the string, pretend that the previous
1985 * character was a new-line.
1987 * All these macros uncleanly have side-effects with each other and outside
1988 * variables. So far it's been too much trouble to clean-up
1990 * TEST_NON_UTF8 is the macro or function to call to test if its byte input is
1991 * a word character or not.
1992 * IF_SUCCESS is code to do if it finds that we are at a boundary between
1994 * IF_FAIL is code to do if we aren't at a boundary between word/non-word
1996 * Exactly one of the two IF_FOO parameters is a no-op, depending on whether we
1997 * are looking for a boundary or for a non-boundary. If we are looking for a
1998 * boundary, we want IF_FAIL to be the no-op, and for IF_SUCCESS to go out and
1999 * see if this tentative match actually works, and if so, to quit the loop
2000 * here. And vice-versa if we are looking for a non-boundary.
2002 * 'tmp' below in the next three macros in the REXEC_FBC_SCAN and
2003 * REXEC_FBC_SCAN loops is a loop invariant, a bool giving the return of
2004 * TEST_NON_UTF8(s-1). To see this, note that that's what it is defined to be
2005 * at entry to the loop, and to get to the IF_FAIL branch, tmp must equal
2006 * TEST_NON_UTF8(s), and in the opposite branch, IF_SUCCESS, tmp is that
2007 * complement. But in that branch we complement tmp, meaning that at the
2008 * bottom of the loop tmp is always going to be equal to TEST_NON_UTF8(s),
2009 * which means at the top of the loop in the next iteration, it is
2010 * TEST_NON_UTF8(s-1) */
2011 #define FBC_UTF8_A(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
2012 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
2013 tmp = TEST_NON_UTF8(tmp); \
2014 REXEC_FBC_SCAN(1, /* 1=>is-utf8; advances s while s < strend */ \
2015 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
2017 IF_SUCCESS; /* Is a boundary if values for s-1 and s differ */ \
2024 /* Like FBC_UTF8_A, but TEST_UV is a macro which takes a UV as its input, and
2025 * TEST_UTF8 is a macro that for the same input code points returns identically
2026 * to TEST_UV, but takes a pointer to a UTF-8 encoded string instead */
2027 #define FBC_UTF8(TEST_UV, TEST_UTF8, IF_SUCCESS, IF_FAIL) \
2028 if (s == reginfo->strbeg) { \
2031 else { /* Back-up to the start of the previous character */ \
2032 U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg); \
2033 tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, \
2034 0, UTF8_ALLOW_DEFAULT); \
2036 tmp = TEST_UV(tmp); \
2037 REXEC_FBC_SCAN(1, /* 1=>is-utf8; advances s while s < strend */ \
2038 if (tmp == ! (TEST_UTF8((U8 *) s, (U8 *) reginfo->strend))) { \
2047 /* Like the above two macros. UTF8_CODE is the complete code for handling
2048 * UTF-8. Common to the BOUND and NBOUND cases, set-up by the FBC_BOUND, etc
2050 #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
2051 if (utf8_target) { \
2054 else { /* Not utf8 */ \
2055 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
2056 tmp = TEST_NON_UTF8(tmp); \
2057 REXEC_FBC_SCAN(0, /* 0=>not-utf8; advances s while s < strend */ \
2058 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
2067 /* Here, things have been set up by the previous code so that tmp is the \
2068 * return of TEST_NON_UTF(s-1) or TEST_UTF8(s-1) (depending on the \
2069 * utf8ness of the target). We also have to check if this matches against \
2070 * the EOS, which we treat as a \n (which is the same value in both UTF-8 \
2071 * or non-UTF8, so can use the non-utf8 test condition even for a UTF-8 \
2073 if (tmp == ! TEST_NON_UTF8('\n')) { \
2080 /* This is the macro to use when we want to see if something that looks like it
2081 * could match, actually does, and if so exits the loop */
2082 #define REXEC_FBC_TRYIT \
2083 if ((reginfo->intuit || regtry(reginfo, &s))) \
2086 /* The only difference between the BOUND and NBOUND cases is that
2087 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
2088 * NBOUND. This is accomplished by passing it as either the if or else clause,
2089 * with the other one being empty (PLACEHOLDER is defined as empty).
2091 * The TEST_FOO parameters are for operating on different forms of input, but
2092 * all should be ones that return identically for the same underlying code
2094 #define FBC_BOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
2096 FBC_UTF8(TEST_UV, TEST_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
2097 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
2099 #define FBC_BOUND_A(TEST_NON_UTF8) \
2101 FBC_UTF8_A(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
2102 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
2104 #define FBC_NBOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
2106 FBC_UTF8(TEST_UV, TEST_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
2107 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
2109 #define FBC_NBOUND_A(TEST_NON_UTF8) \
2111 FBC_UTF8_A(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
2112 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
2116 S_get_break_val_cp_checked(SV* const invlist, const UV cp_in) {
2117 IV cp_out = _invlist_search(invlist, cp_in);
2118 assert(cp_out >= 0);
2121 # define _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp) \
2122 invmap[S_get_break_val_cp_checked(invlist, cp)]
2124 # define _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp) \
2125 invmap[_invlist_search(invlist, cp)]
2128 /* Takes a pointer to an inversion list, a pointer to its corresponding
2129 * inversion map, and a code point, and returns the code point's value
2130 * according to the two arrays. It assumes that all code points have a value.
2131 * This is used as the base macro for macros for particular properties */
2132 #define _generic_GET_BREAK_VAL_CP(invlist, invmap, cp) \
2133 _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp)
2135 /* Same as above, but takes begin, end ptrs to a UTF-8 encoded string instead
2136 * of a code point, returning the value for the first code point in the string.
2137 * And it takes the particular macro name that finds the desired value given a
2138 * code point. Merely convert the UTF-8 to code point and call the cp macro */
2139 #define _generic_GET_BREAK_VAL_UTF8(cp_macro, pos, strend) \
2140 (__ASSERT_(pos < strend) \
2141 /* Note assumes is valid UTF-8 */ \
2142 (cp_macro(utf8_to_uvchr_buf((pos), (strend), NULL))))
2144 /* Returns the GCB value for the input code point */
2145 #define getGCB_VAL_CP(cp) \
2146 _generic_GET_BREAK_VAL_CP( \
2151 /* Returns the GCB value for the first code point in the UTF-8 encoded string
2152 * bounded by pos and strend */
2153 #define getGCB_VAL_UTF8(pos, strend) \
2154 _generic_GET_BREAK_VAL_UTF8(getGCB_VAL_CP, pos, strend)
2156 /* Returns the LB value for the input code point */
2157 #define getLB_VAL_CP(cp) \
2158 _generic_GET_BREAK_VAL_CP( \
2163 /* Returns the LB value for the first code point in the UTF-8 encoded string
2164 * bounded by pos and strend */
2165 #define getLB_VAL_UTF8(pos, strend) \
2166 _generic_GET_BREAK_VAL_UTF8(getLB_VAL_CP, pos, strend)
2169 /* Returns the SB value for the input code point */
2170 #define getSB_VAL_CP(cp) \
2171 _generic_GET_BREAK_VAL_CP( \
2176 /* Returns the SB value for the first code point in the UTF-8 encoded string
2177 * bounded by pos and strend */
2178 #define getSB_VAL_UTF8(pos, strend) \
2179 _generic_GET_BREAK_VAL_UTF8(getSB_VAL_CP, pos, strend)
2181 /* Returns the WB value for the input code point */
2182 #define getWB_VAL_CP(cp) \
2183 _generic_GET_BREAK_VAL_CP( \
2188 /* Returns the WB value for the first code point in the UTF-8 encoded string
2189 * bounded by pos and strend */
2190 #define getWB_VAL_UTF8(pos, strend) \
2191 _generic_GET_BREAK_VAL_UTF8(getWB_VAL_CP, pos, strend)
2193 /* We know what class REx starts with. Try to find this position... */
2194 /* if reginfo->intuit, its a dryrun */
2195 /* annoyingly all the vars in this routine have different names from their counterparts
2196 in regmatch. /grrr */
2198 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
2199 const char *strend, regmatch_info *reginfo)
2203 /* TRUE if x+ need not match at just the 1st pos of run of x's */
2204 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
2206 char *pat_string; /* The pattern's exactish string */
2207 char *pat_end; /* ptr to end char of pat_string */
2208 re_fold_t folder; /* Function for computing non-utf8 folds */
2209 const U8 *fold_array; /* array for folding ords < 256 */
2216 /* In some cases we accept only the first occurence of 'x' in a sequence of
2217 * them. This variable points to just beyond the end of the previous
2218 * occurrence of 'x', hence we can tell if we are in a sequence. (Having
2219 * it point to beyond the 'x' allows us to work for UTF-8 without having to
2221 char * previous_occurrence_end = 0;
2223 I32 tmp; /* Scratch variable */
2224 const bool utf8_target = reginfo->is_utf8_target;
2225 UV utf8_fold_flags = 0;
2226 const bool is_utf8_pat = reginfo->is_utf8_pat;
2227 bool to_complement = FALSE; /* Invert the result? Taking the xor of this
2228 with a result inverts that result, as 0^1 =
2230 _char_class_number classnum;
2232 RXi_GET_DECL(prog,progi);
2234 PERL_ARGS_ASSERT_FIND_BYCLASS;
2236 /* We know what class it must start with. */
2240 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2242 if (ANYOFL_UTF8_LOCALE_REQD(FLAGS(c)) && ! IN_UTF8_CTYPE_LOCALE) {
2243 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), utf8_locale_required);
2250 REXEC_FBC_CLASS_SCAN(1, /* 1=>is-utf8 */
2251 reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target));
2253 else if (ANYOF_FLAGS(c)) {
2254 REXEC_FBC_CLASS_SCAN(0, reginclass(prog,c, (U8*)s, (U8*)s+1, 0));
2257 REXEC_FBC_CLASS_SCAN(0, ANYOF_BITMAP_TEST(c, *((U8*)s)));
2261 case ANYOFM: /* ARG() is the base byte; FLAGS() the mask byte */
2262 /* UTF-8ness doesn't matter, so use 0 */
2263 REXEC_FBC_FIND_NEXT_SCAN(0,
2264 (char *) find_next_masked((U8 *) s, (U8 *) strend,
2265 (U8) ARG(c), FLAGS(c)));
2269 REXEC_FBC_FIND_NEXT_SCAN(0,
2270 (char *) find_span_end_mask((U8 *) s, (U8 *) strend,
2271 (U8) ARG(c), FLAGS(c)));
2274 case EXACTFAA_NO_TRIE: /* This node only generated for non-utf8 patterns */
2275 assert(! is_utf8_pat);
2278 if (is_utf8_pat || utf8_target) {
2279 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
2280 goto do_exactf_utf8;
2282 fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */
2283 folder = foldEQ_latin1; /* /a, except the sharp s one which */
2284 goto do_exactf_non_utf8; /* isn't dealt with by these */
2286 case EXACTF: /* This node only generated for non-utf8 patterns */
2287 assert(! is_utf8_pat);
2289 utf8_fold_flags = 0;
2290 goto do_exactf_utf8;
2292 fold_array = PL_fold;
2294 goto do_exactf_non_utf8;
2297 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2298 if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) {
2299 utf8_fold_flags = FOLDEQ_LOCALE;
2300 goto do_exactf_utf8;
2302 fold_array = PL_fold_locale;
2303 folder = foldEQ_locale;
2304 goto do_exactf_non_utf8;
2308 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
2310 goto do_exactf_utf8;
2313 if (! utf8_target) { /* All code points in this node require
2314 UTF-8 to express. */
2317 utf8_fold_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED
2318 | FOLDEQ_S2_FOLDS_SANE;
2319 goto do_exactf_utf8;
2322 if (is_utf8_pat || utf8_target) {
2323 utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
2324 goto do_exactf_utf8;
2327 /* Any 'ss' in the pattern should have been replaced by regcomp,
2328 * so we don't have to worry here about this single special case
2329 * in the Latin1 range */
2330 fold_array = PL_fold_latin1;
2331 folder = foldEQ_latin1;
2335 do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
2336 are no glitches with fold-length differences
2337 between the target string and pattern */
2339 /* The idea in the non-utf8 EXACTF* cases is to first find the
2340 * first character of the EXACTF* node and then, if necessary,
2341 * case-insensitively compare the full text of the node. c1 is the
2342 * first character. c2 is its fold. This logic will not work for
2343 * Unicode semantics and the german sharp ss, which hence should
2344 * not be compiled into a node that gets here. */
2345 pat_string = STRING(c);
2346 ln = STR_LEN(c); /* length to match in octets/bytes */
2348 /* We know that we have to match at least 'ln' bytes (which is the
2349 * same as characters, since not utf8). If we have to match 3
2350 * characters, and there are only 2 availabe, we know without
2351 * trying that it will fail; so don't start a match past the
2352 * required minimum number from the far end */
2353 e = HOP3c(strend, -((SSize_t)ln), s);
2358 c2 = fold_array[c1];
2359 if (c1 == c2) { /* If char and fold are the same */
2361 s = (char *) memchr(s, c1, e + 1 - s);
2366 /* Check that the rest of the node matches */
2367 if ( (ln == 1 || folder(s + 1, pat_string + 1, ln - 1))
2368 && (reginfo->intuit || regtry(reginfo, &s)) )
2376 U8 bits_differing = c1 ^ c2;
2378 /* If the folds differ in one bit position only, we can mask to
2379 * match either of them, and can use this faster find method. Both
2380 * ASCII and EBCDIC tend to have their case folds differ in only
2381 * one position, so this is very likely */
2382 if (LIKELY(PL_bitcount[bits_differing] == 1)) {
2383 bits_differing = ~ bits_differing;
2385 s = (char *) find_next_masked((U8 *) s, (U8 *) e + 1,
2386 (c1 & bits_differing), bits_differing);
2391 if ( (ln == 1 || folder(s + 1, pat_string + 1, ln - 1))
2392 && (reginfo->intuit || regtry(reginfo, &s)) )
2399 else { /* Otherwise, stuck with looking byte-at-a-time. This
2400 should actually happen only in EXACTFL nodes */
2402 if ( (*(U8*)s == c1 || *(U8*)s == c2)
2403 && (ln == 1 || folder(s + 1, pat_string + 1, ln - 1))
2404 && (reginfo->intuit || regtry(reginfo, &s)) )
2418 /* If one of the operands is in utf8, we can't use the simpler folding
2419 * above, due to the fact that many different characters can have the
2420 * same fold, or portion of a fold, or different- length fold */
2421 pat_string = STRING(c);
2422 ln = STR_LEN(c); /* length to match in octets/bytes */
2423 pat_end = pat_string + ln;
2424 lnc = is_utf8_pat /* length to match in characters */
2425 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
2428 /* We have 'lnc' characters to match in the pattern, but because of
2429 * multi-character folding, each character in the target can match
2430 * up to 3 characters (Unicode guarantees it will never exceed
2431 * this) if it is utf8-encoded; and up to 2 if not (based on the
2432 * fact that the Latin 1 folds are already determined, and the
2433 * only multi-char fold in that range is the sharp-s folding to
2434 * 'ss'. Thus, a pattern character can match as little as 1/3 of a
2435 * string character. Adjust lnc accordingly, rounding up, so that
2436 * if we need to match at least 4+1/3 chars, that really is 5. */
2437 expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
2438 lnc = (lnc + expansion - 1) / expansion;
2440 /* As in the non-UTF8 case, if we have to match 3 characters, and
2441 * only 2 are left, it's guaranteed to fail, so don't start a
2442 * match that would require us to go beyond the end of the string
2444 e = HOP3c(strend, -((SSize_t)lnc), s);
2446 /* XXX Note that we could recalculate e to stop the loop earlier,
2447 * as the worst case expansion above will rarely be met, and as we
2448 * go along we would usually find that e moves further to the left.
2449 * This would happen only after we reached the point in the loop
2450 * where if there were no expansion we should fail. Unclear if
2451 * worth the expense */
2454 char *my_strend= (char *)strend;
2455 if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
2456 pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags)
2457 && (reginfo->intuit || regtry(reginfo, &s)) )
2461 s += (utf8_target) ? UTF8SKIP(s) : 1;
2467 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2468 if (FLAGS(c) != TRADITIONAL_BOUND) {
2469 if (! IN_UTF8_CTYPE_LOCALE) {
2470 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
2471 B_ON_NON_UTF8_LOCALE_IS_WRONG);
2476 FBC_BOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8_safe);
2480 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2481 if (FLAGS(c) != TRADITIONAL_BOUND) {
2482 if (! IN_UTF8_CTYPE_LOCALE) {
2483 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
2484 B_ON_NON_UTF8_LOCALE_IS_WRONG);
2489 FBC_NBOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8_safe);
2492 case BOUND: /* regcomp.c makes sure that this only has the traditional \b
2494 assert(FLAGS(c) == TRADITIONAL_BOUND);
2496 FBC_BOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
2499 case BOUNDA: /* regcomp.c makes sure that this only has the traditional \b
2501 assert(FLAGS(c) == TRADITIONAL_BOUND);
2503 FBC_BOUND_A(isWORDCHAR_A);
2506 case NBOUND: /* regcomp.c makes sure that this only has the traditional \b
2508 assert(FLAGS(c) == TRADITIONAL_BOUND);
2510 FBC_NBOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
2513 case NBOUNDA: /* regcomp.c makes sure that this only has the traditional \b
2515 assert(FLAGS(c) == TRADITIONAL_BOUND);
2517 FBC_NBOUND_A(isWORDCHAR_A);
2521 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
2522 FBC_NBOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
2533 switch((bound_type) FLAGS(c)) {
2534 case TRADITIONAL_BOUND:
2535 FBC_BOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
2538 if (s == reginfo->strbeg) {
2539 if (reginfo->intuit || regtry(reginfo, &s))
2544 /* Didn't match. Try at the next position (if there is one) */
2545 s += (utf8_target) ? UTF8SKIP(s) : 1;
2546 if (UNLIKELY(s >= reginfo->strend)) {
2552 GCB_enum before = getGCB_VAL_UTF8(
2554 (U8*)(reginfo->strbeg)),
2555 (U8*) reginfo->strend);
2556 while (s < strend) {
2557 GCB_enum after = getGCB_VAL_UTF8((U8*) s,
2558 (U8*) reginfo->strend);
2559 if ( (to_complement ^ isGCB(before,
2561 (U8*) reginfo->strbeg,
2564 && (reginfo->intuit || regtry(reginfo, &s)))
2572 else { /* Not utf8. Everything is a GCB except between CR and
2574 while (s < strend) {
2575 if ((to_complement ^ ( UCHARAT(s - 1) != '\r'
2576 || UCHARAT(s) != '\n'))
2577 && (reginfo->intuit || regtry(reginfo, &s)))
2585 /* And, since this is a bound, it can match after the final
2586 * character in the string */
2587 if ((reginfo->intuit || regtry(reginfo, &s))) {
2593 if (s == reginfo->strbeg) {
2594 if (reginfo->intuit || regtry(reginfo, &s)) {
2597 s += (utf8_target) ? UTF8SKIP(s) : 1;
2598 if (UNLIKELY(s >= reginfo->strend)) {
2604 LB_enum before = getLB_VAL_UTF8(reghop3((U8*)s,
2606 (U8*)(reginfo->strbeg)),
2607 (U8*) reginfo->strend);
2608 while (s < strend) {
2609 LB_enum after = getLB_VAL_UTF8((U8*) s, (U8*) reginfo->strend);
2610 if (to_complement ^ isLB(before,
2612 (U8*) reginfo->strbeg,
2614 (U8*) reginfo->strend,
2616 && (reginfo->intuit || regtry(reginfo, &s)))
2624 else { /* Not utf8. */
2625 LB_enum before = getLB_VAL_CP((U8) *(s -1));
2626 while (s < strend) {
2627 LB_enum after = getLB_VAL_CP((U8) *s);
2628 if (to_complement ^ isLB(before,
2630 (U8*) reginfo->strbeg,
2632 (U8*) reginfo->strend,
2634 && (reginfo->intuit || regtry(reginfo, &s)))
2643 if (reginfo->intuit || regtry(reginfo, &s)) {
2650 if (s == reginfo->strbeg) {
2651 if (reginfo->intuit || regtry(reginfo, &s)) {
2654 s += (utf8_target) ? UTF8SKIP(s) : 1;
2655 if (UNLIKELY(s >= reginfo->strend)) {
2661 SB_enum before = getSB_VAL_UTF8(reghop3((U8*)s,
2663 (U8*)(reginfo->strbeg)),
2664 (U8*) reginfo->strend);
2665 while (s < strend) {
2666 SB_enum after = getSB_VAL_UTF8((U8*) s,
2667 (U8*) reginfo->strend);
2668 if ((to_complement ^ isSB(before,
2670 (U8*) reginfo->strbeg,
2672 (U8*) reginfo->strend,
2674 && (reginfo->intuit || regtry(reginfo, &s)))
2682 else { /* Not utf8. */
2683 SB_enum before = getSB_VAL_CP((U8) *(s -1));
2684 while (s < strend) {
2685 SB_enum after = getSB_VAL_CP((U8) *s);
2686 if ((to_complement ^ isSB(before,
2688 (U8*) reginfo->strbeg,
2690 (U8*) reginfo->strend,
2692 && (reginfo->intuit || regtry(reginfo, &s)))
2701 /* Here are at the final position in the target string. The SB
2702 * value is always true here, so matches, depending on other
2704 if (reginfo->intuit || regtry(reginfo, &s)) {
2711 if (s == reginfo->strbeg) {
2712 if (reginfo->intuit || regtry(reginfo, &s)) {
2715 s += (utf8_target) ? UTF8SKIP(s) : 1;
2716 if (UNLIKELY(s >= reginfo->strend)) {
2722 /* We are at a boundary between char_sub_0 and char_sub_1.
2723 * We also keep track of the value for char_sub_-1 as we
2724 * loop through the line. Context may be needed to make a
2725 * determination, and if so, this can save having to
2727 WB_enum previous = WB_UNKNOWN;
2728 WB_enum before = getWB_VAL_UTF8(
2731 (U8*)(reginfo->strbeg)),
2732 (U8*) reginfo->strend);
2733 while (s < strend) {
2734 WB_enum after = getWB_VAL_UTF8((U8*) s,
2735 (U8*) reginfo->strend);
2736 if ((to_complement ^ isWB(previous,
2739 (U8*) reginfo->strbeg,
2741 (U8*) reginfo->strend,
2743 && (reginfo->intuit || regtry(reginfo, &s)))
2752 else { /* Not utf8. */
2753 WB_enum previous = WB_UNKNOWN;
2754 WB_enum before = getWB_VAL_CP((U8) *(s -1));
2755 while (s < strend) {
2756 WB_enum after = getWB_VAL_CP((U8) *s);
2757 if ((to_complement ^ isWB(previous,
2760 (U8*) reginfo->strbeg,
2762 (U8*) reginfo->strend,
2764 && (reginfo->intuit || regtry(reginfo, &s)))
2774 if (reginfo->intuit || regtry(reginfo, &s)) {
2781 REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend),
2782 is_LNBREAK_latin1_safe(s, strend)
2787 REXEC_FBC_FIND_NEXT_SCAN(0, find_next_ascii(s, strend, utf8_target));
2792 REXEC_FBC_FIND_NEXT_SCAN(1, find_next_non_ascii(s, strend,
2796 REXEC_FBC_FIND_NEXT_SCAN(0, find_next_non_ascii(s, strend,
2802 /* The argument to all the POSIX node types is the class number to pass to
2803 * _generic_isCC() to build a mask for searching in PL_charclass[] */
2810 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
2811 REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s, (U8 *) strend)),
2812 to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s)));
2827 /* The complement of something that matches only ASCII matches all
2828 * non-ASCII, plus everything in ASCII that isn't in the class. */
2829 REXEC_FBC_CLASS_SCAN(1, ! isASCII_utf8_safe(s, strend)
2830 || ! _generic_isCC_A(*s, FLAGS(c)));
2838 /* Don't need to worry about utf8, as it can match only a single
2839 * byte invariant character. But we do anyway for performance reasons,
2840 * as otherwise we would have to examine all the continuation
2843 REXEC_FBC_CLASS_SCAN(1, _generic_isCC_A(*s, FLAGS(c)));
2848 REXEC_FBC_CLASS_SCAN(0, /* 0=>not-utf8 */
2849 to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c))));
2857 if (! utf8_target) {
2858 REXEC_FBC_CLASS_SCAN(0, /* 0=>not-utf8 */
2859 to_complement ^ cBOOL(_generic_isCC(*s,
2865 classnum = (_char_class_number) FLAGS(c);
2868 REXEC_FBC_CLASS_SCAN(1, /* 1=>is-utf8 */
2869 to_complement ^ cBOOL(_invlist_contains_cp(
2870 PL_XPosix_ptrs[classnum],
2871 utf8_to_uvchr_buf((U8 *) s,
2875 case _CC_ENUM_SPACE:
2876 REXEC_FBC_CLASS_SCAN(1, /* 1=>is-utf8 */
2877 to_complement ^ cBOOL(isSPACE_utf8_safe(s, strend)));
2880 case _CC_ENUM_BLANK:
2881 REXEC_FBC_CLASS_SCAN(1,
2882 to_complement ^ cBOOL(isBLANK_utf8_safe(s, strend)));
2885 case _CC_ENUM_XDIGIT:
2886 REXEC_FBC_CLASS_SCAN(1,
2887 to_complement ^ cBOOL(isXDIGIT_utf8_safe(s, strend)));
2890 case _CC_ENUM_VERTSPACE:
2891 REXEC_FBC_CLASS_SCAN(1,
2892 to_complement ^ cBOOL(isVERTWS_utf8_safe(s, strend)));
2895 case _CC_ENUM_CNTRL:
2896 REXEC_FBC_CLASS_SCAN(1,
2897 to_complement ^ cBOOL(isCNTRL_utf8_safe(s, strend)));
2907 /* what trie are we using right now */
2908 reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ];
2909 reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ];
2910 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
2912 const char *last_start = strend - trie->minlen;
2914 const char *real_start = s;
2916 STRLEN maxlen = trie->maxlen;
2918 U8 **points; /* map of where we were in the input string
2919 when reading a given char. For ASCII this
2920 is unnecessary overhead as the relationship
2921 is always 1:1, but for Unicode, especially
2922 case folded Unicode this is not true. */
2923 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2927 GET_RE_DEBUG_FLAGS_DECL;
2929 /* We can't just allocate points here. We need to wrap it in
2930 * an SV so it gets freed properly if there is a croak while
2931 * running the match */
2934 sv_points=newSV(maxlen * sizeof(U8 *));
2935 SvCUR_set(sv_points,
2936 maxlen * sizeof(U8 *));
2937 SvPOK_on(sv_points);
2938 sv_2mortal(sv_points);
2939 points=(U8**)SvPV_nolen(sv_points );
2940 if ( trie_type != trie_utf8_fold
2941 && (trie->bitmap || OP(c)==AHOCORASICKC) )
2944 bitmap=(U8*)trie->bitmap;
2946 bitmap=(U8*)ANYOF_BITMAP(c);
2948 /* this is the Aho-Corasick algorithm modified a touch
2949 to include special handling for long "unknown char" sequences.
2950 The basic idea being that we use AC as long as we are dealing
2951 with a possible matching char, when we encounter an unknown char
2952 (and we have not encountered an accepting state) we scan forward
2953 until we find a legal starting char.
2954 AC matching is basically that of trie matching, except that when
2955 we encounter a failing transition, we fall back to the current
2956 states "fail state", and try the current char again, a process
2957 we repeat until we reach the root state, state 1, or a legal
2958 transition. If we fail on the root state then we can either
2959 terminate if we have reached an accepting state previously, or
2960 restart the entire process from the beginning if we have not.
2963 while (s <= last_start) {
2964 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2972 U8 *uscan = (U8*)NULL;
2973 U8 *leftmost = NULL;
2975 U32 accepted_word= 0;
2979 while ( state && uc <= (U8*)strend ) {
2981 U32 word = aho->states[ state ].wordnum;
2985 DEBUG_TRIE_EXECUTE_r(
2986 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2987 dump_exec_pos( (char *)uc, c, strend, real_start,
2988 (char *)uc, utf8_target, 0 );
2989 Perl_re_printf( aTHX_
2990 " Scanning for legal start char...\n");
2994 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2998 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
3004 if (uc >(U8*)last_start) break;
3008 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
3009 if (!leftmost || lpos < leftmost) {
3010 DEBUG_r(accepted_word=word);
3016 points[pointpos++ % maxlen]= uc;
3017 if (foldlen || uc < (U8*)strend) {
3018 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3019 (U8 *) strend, uscan, len, uvc,
3020 charid, foldlen, foldbuf,
3022 DEBUG_TRIE_EXECUTE_r({
3023 dump_exec_pos( (char *)uc, c, strend,
3024 real_start, s, utf8_target, 0);
3025 Perl_re_printf( aTHX_
3026 " Charid:%3u CP:%4" UVxf " ",
3038 word = aho->states[ state ].wordnum;
3040 base = aho->states[ state ].trans.base;
3042 DEBUG_TRIE_EXECUTE_r({
3044 dump_exec_pos( (char *)uc, c, strend, real_start,
3045 s, utf8_target, 0 );
3046 Perl_re_printf( aTHX_
3047 "%sState: %4" UVxf ", word=%" UVxf,
3048 failed ? " Fail transition to " : "",
3049 (UV)state, (UV)word);
3055 ( ((offset = base + charid
3056 - 1 - trie->uniquecharcount)) >= 0)
3057 && ((U32)offset < trie->lasttrans)
3058 && trie->trans[offset].check == state
3059 && (tmp=trie->trans[offset].next))
3061 DEBUG_TRIE_EXECUTE_r(
3062 Perl_re_printf( aTHX_ " - legal\n"));
3067 DEBUG_TRIE_EXECUTE_r(
3068 Perl_re_printf( aTHX_ " - fail\n"));
3070 state = aho->fail[state];
3074 /* we must be accepting here */
3075 DEBUG_TRIE_EXECUTE_r(
3076 Perl_re_printf( aTHX_ " - accepting\n"));
3085 if (!state) state = 1;
3088 if ( aho->states[ state ].wordnum ) {
3089 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
3090 if (!leftmost || lpos < leftmost) {
3091 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
3096 s = (char*)leftmost;
3097 DEBUG_TRIE_EXECUTE_r({
3098 Perl_re_printf( aTHX_ "Matches word #%" UVxf " at position %" IVdf ". Trying full pattern...\n",
3099 (UV)accepted_word, (IV)(s - real_start)
3102 if (reginfo->intuit || regtry(reginfo, &s)) {
3108 DEBUG_TRIE_EXECUTE_r({
3109 Perl_re_printf( aTHX_ "Pattern failed. Looking for new start point...\n");
3112 DEBUG_TRIE_EXECUTE_r(
3113 Perl_re_printf( aTHX_ "No match.\n"));
3122 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
3129 /* set RX_SAVED_COPY, RX_SUBBEG etc.
3130 * flags have same meanings as with regexec_flags() */
3133 S_reg_set_capture_string(pTHX_ REGEXP * const rx,
3140 struct regexp *const prog = ReANY(rx);
3142 if (flags & REXEC_COPY_STR) {
3145 DEBUG_C(Perl_re_printf( aTHX_
3146 "Copy on write: regexp capture, type %d\n",
3148 /* Create a new COW SV to share the match string and store
3149 * in saved_copy, unless the current COW SV in saved_copy
3150 * is valid and suitable for our purpose */
3151 if (( prog->saved_copy
3152 && SvIsCOW(prog->saved_copy)
3153 && SvPOKp(prog->saved_copy)
3156 && SvPVX(sv) == SvPVX(prog->saved_copy)))
3158 /* just reuse saved_copy SV */
3159 if (RXp_MATCH_COPIED(prog)) {
3160 Safefree(prog->subbeg);
3161 RXp_MATCH_COPIED_off(prog);
3165 /* create new COW SV to share string */
3166 RXp_MATCH_COPY_FREE(prog);
3167 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
3169 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
3170 assert (SvPOKp(prog->saved_copy));
3171 prog->sublen = strend - strbeg;
3172 prog->suboffset = 0;
3173 prog->subcoffset = 0;
3178 SSize_t max = strend - strbeg;
3181 if ( (flags & REXEC_COPY_SKIP_POST)
3182 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
3183 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
3184 ) { /* don't copy $' part of string */
3187 /* calculate the right-most part of the string covered
3188 * by a capture. Due to lookahead, this may be to
3189 * the right of $&, so we have to scan all captures */
3190 while (n <= prog->lastparen) {
3191 if (prog->offs[n].end > max)
3192 max = prog->offs[n].end;
3196 max = (PL_sawampersand & SAWAMPERSAND_LEFT)
3197 ? prog->offs[0].start
3199 assert(max >= 0 && max <= strend - strbeg);
3202 if ( (flags & REXEC_COPY_SKIP_PRE)
3203 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
3204 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
3205 ) { /* don't copy $` part of string */
3208 /* calculate the left-most part of the string covered
3209 * by a capture. Due to lookbehind, this may be to
3210 * the left of $&, so we have to scan all captures */
3211 while (min && n <= prog->lastparen) {
3212 if ( prog->offs[n].start != -1
3213 && prog->offs[n].start < min)
3215 min = prog->offs[n].start;
3219 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
3220 && min > prog->offs[0].end
3222 min = prog->offs[0].end;
3226 assert(min >= 0 && min <= max && min <= strend - strbeg);
3229 if (RXp_MATCH_COPIED(prog)) {
3230 if (sublen > prog->sublen)
3232 (char*)saferealloc(prog->subbeg, sublen+1);
3235 prog->subbeg = (char*)safemalloc(sublen+1);
3236 Copy(strbeg + min, prog->subbeg, sublen, char);
3237 prog->subbeg[sublen] = '\0';
3238 prog->suboffset = min;
3239 prog->sublen = sublen;
3240 RXp_MATCH_COPIED_on(prog);
3242 prog->subcoffset = prog->suboffset;
3243 if (prog->suboffset && utf8_target) {
3244 /* Convert byte offset to chars.
3245 * XXX ideally should only compute this if @-/@+
3246 * has been seen, a la PL_sawampersand ??? */
3248 /* If there's a direct correspondence between the
3249 * string which we're matching and the original SV,
3250 * then we can use the utf8 len cache associated with
3251 * the SV. In particular, it means that under //g,
3252 * sv_pos_b2u() will use the previously cached
3253 * position to speed up working out the new length of
3254 * subcoffset, rather than counting from the start of
3255 * the string each time. This stops
3256 * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
3257 * from going quadratic */
3258 if (SvPOKp(sv) && SvPVX(sv) == strbeg)
3259 prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset,
3260 SV_GMAGIC|SV_CONST_RETURN);
3262 prog->subcoffset = utf8_length((U8*)strbeg,
3263 (U8*)(strbeg+prog->suboffset));
3267 RXp_MATCH_COPY_FREE(prog);
3268 prog->subbeg = strbeg;
3269 prog->suboffset = 0;
3270 prog->subcoffset = 0;
3271 prog->sublen = strend - strbeg;
3279 - regexec_flags - match a regexp against a string
3282 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
3283 char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags)
3284 /* stringarg: the point in the string at which to begin matching */
3285 /* strend: pointer to null at end of string */
3286 /* strbeg: real beginning of string */
3287 /* minend: end of match must be >= minend bytes after stringarg. */
3288 /* sv: SV being matched: only used for utf8 flag, pos() etc; string
3289 * itself is accessed via the pointers above */
3290 /* data: May be used for some additional optimizations.
3291 Currently unused. */
3292 /* flags: For optimizations. See REXEC_* in regexp.h */
3295 struct regexp *const prog = ReANY(rx);
3299 SSize_t minlen; /* must match at least this many chars */
3300 SSize_t dontbother = 0; /* how many characters not to try at end */
3301 const bool utf8_target = cBOOL(DO_UTF8(sv));
3303 RXi_GET_DECL(prog,progi);
3304 regmatch_info reginfo_buf; /* create some info to pass to regtry etc */
3305 regmatch_info *const reginfo = ®info_buf;
3306 regexp_paren_pair *swap = NULL;
3308 GET_RE_DEBUG_FLAGS_DECL;
3310 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
3311 PERL_UNUSED_ARG(data);
3313 /* Be paranoid... */
3315 Perl_croak(aTHX_ "NULL regexp parameter");
3319 debug_start_match(rx, utf8_target, stringarg, strend,
3323 startpos = stringarg;
3325 /* set these early as they may be used by the HOP macros below */
3326 reginfo->strbeg = strbeg;
3327 reginfo->strend = strend;
3328 reginfo->is_utf8_target = cBOOL(utf8_target);
3330 if (prog->intflags & PREGf_GPOS_SEEN) {
3333 /* set reginfo->ganch, the position where \G can match */
3336 (flags & REXEC_IGNOREPOS)
3337 ? stringarg /* use start pos rather than pos() */
3338 : ((mg = mg_find_mglob(sv)) && mg->mg_len >= 0)
3339 /* Defined pos(): */
3340 ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg)
3341 : strbeg; /* pos() not defined; use start of string */
3343 DEBUG_GPOS_r(Perl_re_printf( aTHX_
3344 "GPOS ganch set to strbeg[%" IVdf "]\n", (IV)(reginfo->ganch - strbeg)));
3346 /* in the presence of \G, we may need to start looking earlier in
3347 * the string than the suggested start point of stringarg:
3348 * if prog->gofs is set, then that's a known, fixed minimum
3351 * /ab|c\G/: gofs = 1
3352 * or if the minimum offset isn't known, then we have to go back
3353 * to the start of the string, e.g. /w+\G/
3356 if (prog->intflags & PREGf_ANCH_GPOS) {
3358 startpos = HOPBACKc(reginfo->ganch, prog->gofs);
3360 ((flags & REXEC_FAIL_ON_UNDERFLOW) && startpos < stringarg))
3362 DEBUG_r(Perl_re_printf( aTHX_
3363 "fail: ganch-gofs before earliest possible start\n"));
3368 startpos = reginfo->ganch;
3370 else if (prog->gofs) {
3371 startpos = HOPBACKc(startpos, prog->gofs);
3375 else if (prog->intflags & PREGf_GPOS_FLOAT)
3379 minlen = prog->minlen;
3380 if ((startpos + minlen) > strend || startpos < strbeg) {
3381 DEBUG_r(Perl_re_printf( aTHX_
3382 "Regex match can't succeed, so not even tried\n"));
3386 /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave),
3387 * which will call destuctors to reset PL_regmatch_state, free higher
3388 * PL_regmatch_slabs, and clean up regmatch_info_aux and
3389 * regmatch_info_aux_eval */
3391 oldsave = PL_savestack_ix;
3395 if ((prog->extflags & RXf_USE_INTUIT)
3396 && !(flags & REXEC_CHECKED))
3398 s = re_intuit_start(rx, sv, strbeg, startpos, strend,
3403 if (prog->extflags & RXf_CHECK_ALL) {
3404 /* we can match based purely on the result of INTUIT.
3405 * Set up captures etc just for $& and $-[0]
3406 * (an intuit-only match wont have $1,$2,..) */
3407 assert(!prog->nparens);
3409 /* s/// doesn't like it if $& is earlier than where we asked it to
3410 * start searching (which can happen on something like /.\G/) */
3411 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
3414 /* this should only be possible under \G */
3415 assert(prog->intflags & PREGf_GPOS_SEEN);
3416 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
3417 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3421 /* match via INTUIT shouldn't have any captures.
3422 * Let @-, @+, $^N know */
3423 prog->lastparen = prog->lastcloseparen = 0;
3424 RXp_MATCH_UTF8_set(prog, utf8_target);
3425 prog->offs[0].start = s - strbeg;
3426 prog->offs[0].end = utf8_target
3427 ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg
3428 : s - strbeg + prog->minlenret;
3429 if ( !(flags & REXEC_NOT_FIRST) )
3430 S_reg_set_capture_string(aTHX_ rx,
3432 sv, flags, utf8_target);
3438 multiline = prog->extflags & RXf_PMf_MULTILINE;
3440 if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
3441 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
3442 "String too short [regexec_flags]...\n"));
3446 /* Check validity of program. */
3447 if (UCHARAT(progi->program) != REG_MAGIC) {
3448 Perl_croak(aTHX_ "corrupted regexp program");
3451 RXp_MATCH_TAINTED_off(prog);
3452 RXp_MATCH_UTF8_set(prog, utf8_target);
3454 reginfo->prog = rx; /* Yes, sorry that this is confusing. */
3455 reginfo->intuit = 0;
3456 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
3457 reginfo->warned = FALSE;
3459 reginfo->poscache_maxiter = 0; /* not yet started a countdown */
3460 /* see how far we have to get to not match where we matched before */
3461 reginfo->till = stringarg + minend;
3463 if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) {
3464 /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after
3465 S_cleanup_regmatch_info_aux has executed (registered by
3466 SAVEDESTRUCTOR_X below). S_cleanup_regmatch_info_aux modifies
3467 magic belonging to this SV.
3468 Not newSVsv, either, as it does not COW.
3470 reginfo->sv = newSV(0);
3471 SvSetSV_nosteal(reginfo->sv, sv);
3472 SAVEFREESV(reginfo->sv);
3475 /* reserve next 2 or 3 slots in PL_regmatch_state:
3476 * slot N+0: may currently be in use: skip it
3477 * slot N+1: use for regmatch_info_aux struct
3478 * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s
3479 * slot N+3: ready for use by regmatch()
3483 regmatch_state *old_regmatch_state;
3484 regmatch_slab *old_regmatch_slab;
3485 int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1;
3487 /* on first ever match, allocate first slab */
3488 if (!PL_regmatch_slab) {
3489 Newx(PL_regmatch_slab, 1, regmatch_slab);
3490 PL_regmatch_slab->prev = NULL;
3491 PL_regmatch_slab->next = NULL;
3492 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
3495 old_regmatch_state = PL_regmatch_state;
3496 old_regmatch_slab = PL_regmatch_slab;
3498 for (i=0; i <= max; i++) {
3500 reginfo->info_aux = &(PL_regmatch_state->u.info_aux);
3502 reginfo->info_aux_eval =
3503 reginfo->info_aux->info_aux_eval =
3504 &(PL_regmatch_state->u.info_aux_eval);
3506 if (++PL_regmatch_state > SLAB_LAST(PL_regmatch_slab))
3507 PL_regmatch_state = S_push_slab(aTHX);
3510 /* note initial PL_regmatch_state position; at end of match we'll
3511 * pop back to there and free any higher slabs */
3513 reginfo->info_aux->old_regmatch_state = old_regmatch_state;
3514 reginfo->info_aux->old_regmatch_slab = old_regmatch_slab;
3515 reginfo->info_aux->poscache = NULL;
3517 SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux);
3519 if ((prog->extflags & RXf_EVAL_SEEN))
3520 S_setup_eval_state(aTHX_ reginfo);
3522 reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL;
3525 /* If there is a "must appear" string, look for it. */
3527 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
3528 /* We have to be careful. If the previous successful match
3529 was from this regex we don't want a subsequent partially
3530 successful match to clobber the old results.
3531 So when we detect this possibility we add a swap buffer
3532 to the re, and switch the buffer each match. If we fail,
3533 we switch it back; otherwise we leave it swapped.
3536 /* do we need a save destructor here for eval dies? */
3537 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
3538 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
3539 "rex=0x%" UVxf " saving offs: orig=0x%" UVxf " new=0x%" UVxf "\n",
3547 if (prog->recurse_locinput)
3548 Zero(prog->recurse_locinput,prog->nparens + 1, char *);
3550 /* Simplest case: anchored match need be tried only once, or with
3551 * MBOL, only at the beginning of each line.
3553 * Note that /.*.../ sets PREGf_IMPLICIT|MBOL, while /.*.../s sets
3554 * PREGf_IMPLICIT|SBOL. The idea is that with /.*.../s, if it doesn't
3555 * match at the start of the string then it won't match anywhere else
3556 * either; while with /.*.../, if it doesn't match at the beginning,
3557 * the earliest it could match is at the start of the next line */
3559 if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) {
3562 if (regtry(reginfo, &s))
3565 if (!(prog->intflags & PREGf_ANCH_MBOL))
3568 /* didn't match at start, try at other newline positions */
3571 dontbother = minlen - 1;
3572 end = HOP3c(strend, -dontbother, strbeg) - 1;
3574 /* skip to next newline */
3576 while (s <= end) { /* note it could be possible to match at the end of the string */
3577 /* NB: newlines are the same in unicode as they are in latin */
3580 if (prog->check_substr || prog->check_utf8) {
3581 /* note that with PREGf_IMPLICIT, intuit can only fail
3582 * or return the start position, so it's of limited utility.
3583 * Nevertheless, I made the decision that the potential for
3584 * quick fail was still worth it - DAPM */
3585 s = re_intuit_start(rx, sv, strbeg, s, strend, flags, NULL);
3589 if (regtry(reginfo, &s))
3593 } /* end anchored search */
3595 if (prog->intflags & PREGf_ANCH_GPOS)
3597 /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */
3598 assert(prog->intflags & PREGf_GPOS_SEEN);
3599 /* For anchored \G, the only position it can match from is
3600 * (ganch-gofs); we already set startpos to this above; if intuit
3601 * moved us on from there, we can't possibly succeed */
3602 assert(startpos == HOPBACKc(reginfo->ganch, prog->gofs));
3603 if (s == startpos && regtry(reginfo, &s))
3608 /* Messy cases: unanchored match. */
3609 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
3610 /* we have /x+whatever/ */
3611 /* it must be a one character string (XXXX Except is_utf8_pat?) */
3617 if (! prog->anchored_utf8) {
3618 to_utf8_substr(prog);
3620 ch = SvPVX_const(prog->anchored_utf8)[0];
3621 REXEC_FBC_SCAN(0, /* 0=>not-utf8 */
3623 DEBUG_EXECUTE_r( did_match = 1 );
3624 if (regtry(reginfo, &s)) goto got_it;
3626 while (s < strend && *s == ch)
3633 if (! prog->anchored_substr) {
3634 if (! to_byte_substr(prog)) {
3635 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3638 ch = SvPVX_const(prog->anchored_substr)[0];
3639 REXEC_FBC_SCAN(0, /* 0=>not-utf8 */
3641 DEBUG_EXECUTE_r( did_match = 1 );
3642 if (regtry(reginfo, &s)) goto got_it;
3644 while (s < strend && *s == ch)
3649 DEBUG_EXECUTE_r(if (!did_match)
3650 Perl_re_printf( aTHX_
3651 "Did not find anchored character...\n")
3654 else if (prog->anchored_substr != NULL
3655 || prog->anchored_utf8 != NULL
3656 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
3657 && prog->float_max_offset < strend - s)) {
3662 char *last1; /* Last position checked before */
3666 if (prog->anchored_substr || prog->anchored_utf8) {
3668 if (! prog->anchored_utf8) {
3669 to_utf8_substr(prog);
3671 must = prog->anchored_utf8;
3674 if (! prog->anchored_substr) {
3675 if (! to_byte_substr(prog)) {
3676 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3679 must = prog->anchored_substr;
3681 back_max = back_min = prog->anchored_offset;
3684 if (! prog->float_utf8) {
3685 to_utf8_substr(prog);
3687 must = prog->float_utf8;
3690 if (! prog->float_substr) {
3691 if (! to_byte_substr(prog)) {
3692 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3695 must = prog->float_substr;
3697 back_max = prog->float_max_offset;
3698 back_min = prog->float_min_offset;
3704 last = HOP3c(strend, /* Cannot start after this */
3705 -(SSize_t)(CHR_SVLEN(must)
3706 - (SvTAIL(must) != 0) + back_min), strbeg);
3708 if (s > reginfo->strbeg)
3709 last1 = HOPc(s, -1);
3711 last1 = s - 1; /* bogus */
3713 /* XXXX check_substr already used to find "s", can optimize if
3714 check_substr==must. */
3716 strend = HOPc(strend, -dontbother);
3717 while ( (s <= last) &&
3718 (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg, strend),
3719 (unsigned char*)strend, must,
3720 multiline ? FBMrf_MULTILINE : 0)) ) {
3721 DEBUG_EXECUTE_r( did_match = 1 );
3722 if (HOPc(s, -back_max) > last1) {
3723 last1 = HOPc(s, -back_min);
3724 s = HOPc(s, -back_max);
3727 char * const t = (last1 >= reginfo->strbeg)
3728 ? HOPc(last1, 1) : last1 + 1;
3730 last1 = HOPc(s, -back_min);
3734 while (s <= last1) {
3735 if (regtry(reginfo, &s))
3738 s++; /* to break out of outer loop */
3745 while (s <= last1) {
3746 if (regtry(reginfo, &s))
3752 DEBUG_EXECUTE_r(if (!did_match) {
3753 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
3754 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
3755 Perl_re_printf( aTHX_ "Did not find %s substr %s%s...\n",
3756 ((must == prog->anchored_substr || must == prog->anchored_utf8)
3757 ? "anchored" : "floating"),
3758 quoted, RE_SV_TAIL(must));
3762 else if ( (c = progi->regstclass) ) {
3764 const OPCODE op = OP(progi->regstclass);
3765 /* don't bother with what can't match */
3766 if (PL_regkind[op] != EXACT && PL_regkind[op] != TRIE)
3767 strend = HOPc(strend, -(minlen - 1));
3770 SV * const prop = sv_newmortal();
3771 regprop(prog, prop, c, reginfo, NULL);
3773 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
3774 s,strend-s,PL_dump_re_max_len);
3775 Perl_re_printf( aTHX_
3776 "Matching stclass %.*s against %s (%d bytes)\n",
3777 (int)SvCUR(prop), SvPVX_const(prop),
3778 quoted, (int)(strend - s));
3781 if (find_byclass(prog, c, s, strend, reginfo))
3783 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "Contradicts stclass... [regexec_flags]\n"));
3787 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
3795 if (! prog->float_utf8) {
3796 to_utf8_substr(prog);
3798 float_real = prog->float_utf8;
3801 if (! prog->float_substr) {
3802 if (! to_byte_substr(prog)) {
3803 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3806 float_real = prog->float_substr;
3809 little = SvPV_const(float_real, len);
3810 if (SvTAIL(float_real)) {
3811 /* This means that float_real contains an artificial \n on
3812 * the end due to the presence of something like this:
3813 * /foo$/ where we can match both "foo" and "foo\n" at the
3814 * end of the string. So we have to compare the end of the
3815 * string first against the float_real without the \n and
3816 * then against the full float_real with the string. We
3817 * have to watch out for cases where the string might be
3818 * smaller than the float_real or the float_real without
3820 char *checkpos= strend - len;
3822 Perl_re_printf( aTHX_
3823 "%sChecking for float_real.%s\n",
3824 PL_colors[4], PL_colors[5]));
3825 if (checkpos + 1 < strbeg) {
3826 /* can't match, even if we remove the trailing \n
3827 * string is too short to match */
3829 Perl_re_printf( aTHX_
3830 "%sString shorter than required trailing substring, cannot match.%s\n",
3831 PL_colors[4], PL_colors[5]));
3833 } else if (memEQ(checkpos + 1, little, len - 1)) {
3834 /* can match, the end of the string matches without the
3836 last = checkpos + 1;
3837 } else if (checkpos < strbeg) {
3838 /* cant match, string is too short when the "\n" is
3841 Perl_re_printf( aTHX_
3842 "%sString does not contain required trailing substring, cannot match.%s\n",
3843 PL_colors[4], PL_colors[5]));
3845 } else if (!multiline) {
3846 /* non multiline match, so compare with the "\n" at the
3847 * end of the string */
3848 if (memEQ(checkpos, little, len)) {
3852 Perl_re_printf( aTHX_
3853 "%sString does not contain required trailing substring, cannot match.%s\n",
3854 PL_colors[4], PL_colors[5]));
3858 /* multiline match, so we have to search for a place
3859 * where the full string is located */
3865 last = rninstr(s, strend, little, little + len);
3867 last = strend; /* matching "$" */
3870 /* at one point this block contained a comment which was
3871 * probably incorrect, which said that this was a "should not
3872 * happen" case. Even if it was true when it was written I am
3873 * pretty sure it is not anymore, so I have removed the comment
3874 * and replaced it with this one. Yves */
3876 Perl_re_printf( aTHX_
3877 "%sString does not contain required substring, cannot match.%s\n",
3878 PL_colors[4], PL_colors[5]
3882 dontbother = strend - last + prog->float_min_offset;
3884 if (minlen && (dontbother < minlen))
3885 dontbother = minlen - 1;
3886 strend -= dontbother; /* this one's always in bytes! */
3887 /* We don't know much -- general case. */
3890 if (regtry(reginfo, &s))
3899 if (regtry(reginfo, &s))
3901 } while (s++ < strend);
3909 /* s/// doesn't like it if $& is earlier than where we asked it to
3910 * start searching (which can happen on something like /.\G/) */
3911 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
3912 && (prog->offs[0].start < stringarg - strbeg))
3914 /* this should only be possible under \G */
3915 assert(prog->intflags & PREGf_GPOS_SEEN);
3916 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
3917 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3923 Perl_re_exec_indentf( aTHX_
3924 "rex=0x%" UVxf " freeing offs: 0x%" UVxf "\n",
3932 /* clean up; this will trigger destructors that will free all slabs
3933 * above the current one, and cleanup the regmatch_info_aux
3934 * and regmatch_info_aux_eval sructs */
3936 LEAVE_SCOPE(oldsave);
3938 if (RXp_PAREN_NAMES(prog))
3939 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
3941 /* make sure $`, $&, $', and $digit will work later */
3942 if ( !(flags & REXEC_NOT_FIRST) )
3943 S_reg_set_capture_string(aTHX_ rx,
3944 strbeg, reginfo->strend,
3945 sv, flags, utf8_target);
3950 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%sMatch failed%s\n",
3951 PL_colors[4], PL_colors[5]));
3953 /* clean up; this will trigger destructors that will free all slabs
3954 * above the current one, and cleanup the regmatch_info_aux
3955 * and regmatch_info_aux_eval sructs */
3957 LEAVE_SCOPE(oldsave);
3960 /* we failed :-( roll it back */
3961 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
3962 "rex=0x%" UVxf " rolling back offs: freeing=0x%" UVxf " restoring=0x%" UVxf "\n",
3968 Safefree(prog->offs);
3975 /* Set which rex is pointed to by PL_reg_curpm, handling ref counting.
3976 * Do inc before dec, in case old and new rex are the same */
3977 #define SET_reg_curpm(Re2) \
3978 if (reginfo->info_aux_eval) { \
3979 (void)ReREFCNT_inc(Re2); \
3980 ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \
3981 PM_SETRE((PL_reg_curpm), (Re2)); \
3986 - regtry - try match at specific point
3988 STATIC bool /* 0 failure, 1 success */
3989 S_regtry(pTHX_ regmatch_info *reginfo, char **startposp)
3992 REGEXP *const rx = reginfo->prog;
3993 regexp *const prog = ReANY(rx);
3996 U32 depth = 0; /* used by REGCP_SET */
3998 RXi_GET_DECL(prog,progi);
3999 GET_RE_DEBUG_FLAGS_DECL;
4001 PERL_ARGS_ASSERT_REGTRY;
4003 reginfo->cutpoint=NULL;
4005 prog->offs[0].start = *startposp - reginfo->strbeg;
4006 prog->lastparen = 0;
4007 prog->lastcloseparen = 0;
4009 /* XXXX What this code is doing here?!!! There should be no need
4010 to do this again and again, prog->lastparen should take care of
4013 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
4014 * Actually, the code in regcppop() (which Ilya may be meaning by
4015 * prog->lastparen), is not needed at all by the test suite
4016 * (op/regexp, op/pat, op/split), but that code is needed otherwise
4017 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
4018 * Meanwhile, this code *is* needed for the
4019 * above-mentioned test suite tests to succeed. The common theme
4020 * on those tests seems to be returning null fields from matches.
4021 * --jhi updated by dapm */
4023 /* After encountering a variant of the issue mentioned above I think
4024 * the point Ilya was making is that if we properly unwind whenever
4025 * we set lastparen to a smaller value then we should not need to do
4026 * this every time, only when needed. So if we have tests that fail if
4027 * we remove this, then it suggests somewhere else we are improperly
4028 * unwinding the lastparen/paren buffers. See UNWIND_PARENS() and
4029 * places it is called, and related regcp() routines. - Yves */
4031 if (prog->nparens) {
4032 regexp_paren_pair *pp = prog->offs;
4034 for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
4042 result = regmatch(reginfo, *startposp, progi->program + 1);
4044 prog->offs[0].end = result;
4047 if (reginfo->cutpoint)
4048 *startposp= reginfo->cutpoint;
4049 REGCP_UNWIND(lastcp);
4054 #define sayYES goto yes
4055 #define sayNO goto no
4056 #define sayNO_SILENT goto no_silent
4058 /* we dont use STMT_START/END here because it leads to
4059 "unreachable code" warnings, which are bogus, but distracting. */
4060 #define CACHEsayNO \
4061 if (ST.cache_mask) \
4062 reginfo->info_aux->poscache[ST.cache_offset] |= ST.cache_mask; \
4065 /* this is used to determine how far from the left messages like
4066 'failed...' are printed in regexec.c. It should be set such that
4067 messages are inline with the regop output that created them.
4069 #define REPORT_CODE_OFF 29
4070 #define INDENT_CHARS(depth) ((int)(depth) % 20)
4073 Perl_re_exec_indentf(pTHX_ const char *fmt, U32 depth, ...)
4077 PerlIO *f= Perl_debug_log;
4078 PERL_ARGS_ASSERT_RE_EXEC_INDENTF;
4079 va_start(ap, depth);
4080 PerlIO_printf(f, "%*s|%4" UVuf "| %*s", REPORT_CODE_OFF, "", (UV)depth, INDENT_CHARS(depth), "" );
4081 result = PerlIO_vprintf(f, fmt, ap);
4085 #endif /* DEBUGGING */
4088 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
4089 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
4090 #define CHRTEST_NOT_A_CP_1 -999
4091 #define CHRTEST_NOT_A_CP_2 -998
4093 /* grab a new slab and return the first slot in it */
4095 STATIC regmatch_state *
4098 regmatch_slab *s = PL_regmatch_slab->next;
4100 Newx(s, 1, regmatch_slab);
4101 s->prev = PL_regmatch_slab;
4103 PL_regmatch_slab->next = s;
4105 PL_regmatch_slab = s;
4106 return SLAB_FIRST(s);
4110 /* push a new state then goto it */
4112 #define PUSH_STATE_GOTO(state, node, input) \
4113 pushinput = input; \
4115 st->resume_state = state; \
4118 /* push a new state with success backtracking, then goto it */
4120 #define PUSH_YES_STATE_GOTO(state, node, input) \
4121 pushinput = input; \
4123 st->resume_state = state; \
4124 goto push_yes_state;
4131 regmatch() - main matching routine
4133 This is basically one big switch statement in a loop. We execute an op,
4134 set 'next' to point the next op, and continue. If we come to a point which
4135 we may need to backtrack to on failure such as (A|B|C), we push a
4136 backtrack state onto the backtrack stack. On failure, we pop the top
4137 state, and re-enter the loop at the state indicated. If there are no more
4138 states to pop, we return failure.
4140 Sometimes we also need to backtrack on success; for example /A+/, where
4141 after successfully matching one A, we need to go back and try to
4142 match another one; similarly for lookahead assertions: if the assertion
4143 completes successfully, we backtrack to the state just before the assertion
4144 and then carry on. In these cases, the pushed state is marked as
4145 'backtrack on success too'. This marking is in fact done by a chain of
4146 pointers, each pointing to the previous 'yes' state. On success, we pop to
4147 the nearest yes state, discarding any intermediate failure-only states.
4148 Sometimes a yes state is pushed just to force some cleanup code to be
4149 called at the end of a successful match or submatch; e.g. (??{$re}) uses
4150 it to free the inner regex.
4152 Note that failure backtracking rewinds the cursor position, while
4153 success backtracking leaves it alone.
4155 A pattern is complete when the END op is executed, while a subpattern
4156 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
4157 ops trigger the "pop to last yes state if any, otherwise return true"
4160 A common convention in this function is to use A and B to refer to the two
4161 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
4162 the subpattern to be matched possibly multiple times, while B is the entire
4163 rest of the pattern. Variable and state names reflect this convention.
4165 The states in the main switch are the union of ops and failure/success of
4166 substates associated with with that op. For example, IFMATCH is the op
4167 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
4168 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
4169 successfully matched A and IFMATCH_A_fail is a state saying that we have
4170 just failed to match A. Resume states always come in pairs. The backtrack
4171 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
4172 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
4173 on success or failure.
4175 The struct that holds a backtracking state is actually a big union, with
4176 one variant for each major type of op. The variable st points to the
4177 top-most backtrack struct. To make the code clearer, within each
4178 block of code we #define ST to alias the relevant union.
4180 Here's a concrete example of a (vastly oversimplified) IFMATCH
4186 #define ST st->u.ifmatch
4188 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
4189 ST.foo = ...; // some state we wish to save
4191 // push a yes backtrack state with a resume value of
4192 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
4194 PUSH_YES_STATE_GOTO(IFMATCH_A, A, newinput);
4197 case IFMATCH_A: // we have successfully executed A; now continue with B
4199 bar = ST.foo; // do something with the preserved value
4202 case IFMATCH_A_fail: // A failed, so the assertion failed
4203 ...; // do some housekeeping, then ...
4204 sayNO; // propagate the failure
4211 For any old-timers reading this who are familiar with the old recursive
4212 approach, the code above is equivalent to:
4214 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
4223 ...; // do some housekeeping, then ...
4224 sayNO; // propagate the failure
4227 The topmost backtrack state, pointed to by st, is usually free. If you
4228 want to claim it, populate any ST.foo fields in it with values you wish to
4229 save, then do one of
4231 PUSH_STATE_GOTO(resume_state, node, newinput);
4232 PUSH_YES_STATE_GOTO(resume_state, node, newinput);
4234 which sets that backtrack state's resume value to 'resume_state', pushes a
4235 new free entry to the top of the backtrack stack, then goes to 'node'.
4236 On backtracking, the free slot is popped, and the saved state becomes the
4237 new free state. An ST.foo field in this new top state can be temporarily
4238 accessed to retrieve values, but once the main loop is re-entered, it
4239 becomes available for reuse.
4241 Note that the depth of the backtrack stack constantly increases during the
4242 left-to-right execution of the pattern, rather than going up and down with
4243 the pattern nesting. For example the stack is at its maximum at Z at the
4244 end of the pattern, rather than at X in the following:
4246 /(((X)+)+)+....(Y)+....Z/
4248 The only exceptions to this are lookahead/behind assertions and the cut,
4249 (?>A), which pop all the backtrack states associated with A before
4252 Backtrack state structs are allocated in slabs of about 4K in size.
4253 PL_regmatch_state and st always point to the currently active state,
4254 and PL_regmatch_slab points to the slab currently containing
4255 PL_regmatch_state. The first time regmatch() is called, the first slab is
4256 allocated, and is never freed until interpreter destruction. When the slab
4257 is full, a new one is allocated and chained to the end. At exit from
4258 regmatch(), slabs allocated since entry are freed.
4263 #define DEBUG_STATE_pp(pp) \
4265 DUMP_EXEC_POS(locinput, scan, utf8_target,depth); \
4266 Perl_re_printf( aTHX_ \
4267 "%*s" pp " %s%s%s%s%s\n", \
4268 INDENT_CHARS(depth), "", \
4269 PL_reg_name[st->resume_state], \
4270 ((st==yes_state||st==mark_state) ? "[" : ""), \
4271 ((st==yes_state) ? "Y" : ""), \
4272 ((st==mark_state) ? "M" : ""), \
4273 ((st==yes_state||st==mark_state) ? "]" : "") \
4278 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
4283 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
4284 const char *start, const char *end, const char *blurb)
4286 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
4288 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
4293 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
4294 RX_PRECOMP_const(prog), RX_PRELEN(prog), PL_dump_re_max_len);
4296 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
4297 start, end - start, PL_dump_re_max_len);
4299 Perl_re_printf( aTHX_
4300 "%s%s REx%s %s against %s\n",
4301 PL_colors[4], blurb, PL_colors[5], s0, s1);
4303 if (utf8_target||utf8_pat)
4304 Perl_re_printf( aTHX_ "UTF-8 %s%s%s...\n",
4305 utf8_pat ? "pattern" : "",
4306 utf8_pat && utf8_target ? " and " : "",
4307 utf8_target ? "string" : ""
4313 S_dump_exec_pos(pTHX_ const char *locinput,
4314 const regnode *scan,
4315 const char *loc_regeol,
4316 const char *loc_bostr,
4317 const char *loc_reg_starttry,
4318 const bool utf8_target,
4322 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
4323 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
4324 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
4325 /* The part of the string before starttry has one color
4326 (pref0_len chars), between starttry and current
4327 position another one (pref_len - pref0_len chars),
4328 after the current position the third one.
4329 We assume that pref0_len <= pref_len, otherwise we
4330 decrease pref0_len. */
4331 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
4332 ? (5 + taill) - l : locinput - loc_bostr;
4335 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
4337 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
4339 pref0_len = pref_len - (locinput - loc_reg_starttry);
4340 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
4341 l = ( loc_regeol - locinput > (5 + taill) - pref_len
4342 ? (5 + taill) - pref_len : loc_regeol - locinput);
4343 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
4347 if (pref0_len > pref_len)
4348 pref0_len = pref_len;
4350 const int is_uni = utf8_target ? 1 : 0;
4352 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
4353 (locinput - pref_len),pref0_len, PL_dump_re_max_len, 4, 5);
4355 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
4356 (locinput - pref_len + pref0_len),
4357 pref_len - pref0_len, PL_dump_re_max_len, 2, 3);
4359 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
4360 locinput, loc_regeol - locinput, 10, 0, 1);
4362 const STRLEN tlen=len0+len1+len2;
4363 Perl_re_printf( aTHX_
4364 "%4" IVdf " <%.*s%.*s%s%.*s>%*s|%4u| ",
4365 (IV)(locinput - loc_bostr),
4368 (docolor ? "" : "> <"),
4370 (int)(tlen > 19 ? 0 : 19 - tlen),
4378 /* reg_check_named_buff_matched()
4379 * Checks to see if a named buffer has matched. The data array of
4380 * buffer numbers corresponding to the buffer is expected to reside
4381 * in the regexp->data->data array in the slot stored in the ARG() of
4382 * node involved. Note that this routine doesn't actually care about the
4383 * name, that information is not preserved from compilation to execution.
4384 * Returns the index of the leftmost defined buffer with the given name
4385 * or 0 if non of the buffers matched.
4388 S_reg_check_named_buff_matched(const regexp *rex, const regnode *scan)
4391 RXi_GET_DECL(rex,rexi);
4392 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
4393 I32 *nums=(I32*)SvPVX(sv_dat);
4395 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
4397 for ( n=0; n<SvIVX(sv_dat); n++ ) {
4398 if ((I32)rex->lastparen >= nums[n] &&
4399 rex->offs[nums[n]].end != -1)
4409 S_setup_EXACTISH_ST_c1_c2(pTHX_ const regnode * const text_node, int *c1p,
4410 U8* c1_utf8, int *c2p, U8* c2_utf8, regmatch_info *reginfo)
4412 /* This function determines if there are one or two characters that match
4413 * the first character of the passed-in EXACTish node <text_node>, and if
4414 * so, returns them in the passed-in pointers.
4416 * If it determines that no possible character in the target string can
4417 * match, it returns FALSE; otherwise TRUE. (The FALSE situation occurs if
4418 * the first character in <text_node> requires UTF-8 to represent, and the
4419 * target string isn't in UTF-8.)
4421 * If there are more than two characters that could match the beginning of
4422 * <text_node>, or if more context is required to determine a match or not,
4423 * it sets both *<c1p> and *<c2p> to CHRTEST_VOID.
4425 * The motiviation behind this function is to allow the caller to set up
4426 * tight loops for matching. If <text_node> is of type EXACT, there is
4427 * only one possible character that can match its first character, and so
4428 * the situation is quite simple. But things get much more complicated if
4429 * folding is involved. It may be that the first character of an EXACTFish
4430 * node doesn't participate in any possible fold, e.g., punctuation, so it
4431 * can be matched only by itself. The vast majority of characters that are
4432 * in folds match just two things, their lower and upper-case equivalents.
4433 * But not all are like that; some have multiple possible matches, or match
4434 * sequences of more than one character. This function sorts all that out.
4436 * Consider the patterns A*B or A*?B where A and B are arbitrary. In a
4437 * loop of trying to match A*, we know we can't exit where the thing
4438 * following it isn't a B. And something can't be a B unless it is the
4439 * beginning of B. By putting a quick test for that beginning in a tight
4440 * loop, we can rule out things that can't possibly be B without having to
4441 * break out of the loop, thus avoiding work. Similarly, if A is a single
4442 * character, we can make a tight loop matching A*, using the outputs of
4445 * If the target string to match isn't in UTF-8, and there aren't
4446 * complications which require CHRTEST_VOID, *<c1p> and *<c2p> are set to
4447 * the one or two possible octets (which are characters in this situation)
4448 * that can match. In all cases, if there is only one character that can
4449 * match, *<c1p> and *<c2p> will be identical.
4451 * If the target string is in UTF-8, the buffers pointed to by <c1_utf8>
4452 * and <c2_utf8> will contain the one or two UTF-8 sequences of bytes that
4453 * can match the beginning of <text_node>. They should be declared with at
4454 * least length UTF8_MAXBYTES+1. (If the target string isn't in UTF-8, it is
4455 * undefined what these contain.) If one or both of the buffers are
4456 * invariant under UTF-8, *<c1p>, and *<c2p> will also be set to the
4457 * corresponding invariant. If variant, the corresponding *<c1p> and/or
4458 * *<c2p> will be set to a negative number(s) that shouldn't match any code
4459 * point (unless inappropriately coerced to unsigned). *<c1p> will equal
4460 * *<c2p> if and only if <c1_utf8> and <c2_utf8> are the same. */
4462 const bool utf8_target = reginfo->is_utf8_target;
4464 UV c1 = (UV)CHRTEST_NOT_A_CP_1;
4465 UV c2 = (UV)CHRTEST_NOT_A_CP_2;
4466 bool use_chrtest_void = FALSE;
4467 const bool is_utf8_pat = reginfo->is_utf8_pat;
4469 /* Used when we have both utf8 input and utf8 output, to avoid converting
4470 * to/from code points */
4471 bool utf8_has_been_setup = FALSE;
4475 U8 *pat = (U8*)STRING(text_node);
4476 U8 folded[UTF8_MAX_FOLD_CHAR_EXPAND * UTF8_MAXBYTES_CASE + 1] = { '\0' };
4478 if (OP(text_node) == EXACT || OP(text_node) == EXACTL) {
4480 /* In an exact node, only one thing can be matched, that first
4481 * character. If both the pat and the target are UTF-8, we can just
4482 * copy the input to the output, avoiding finding the code point of
4487 else if (utf8_target) {
4488 Copy(pat, c1_utf8, UTF8SKIP(pat), U8);
4489 Copy(pat, c2_utf8, UTF8SKIP(pat), U8);
4490 utf8_has_been_setup = TRUE;
4493 c2 = c1 = valid_utf8_to_uvchr(pat, NULL);
4496 else { /* an EXACTFish node */
4497 U8 *pat_end = pat + STR_LEN(text_node);
4499 /* An EXACTFL node has at least some characters unfolded, because what
4500 * they match is not known until now. So, now is the time to fold
4501 * the first few of them, as many as are needed to determine 'c1' and
4502 * 'c2' later in the routine. If the pattern isn't UTF-8, we only need
4503 * to fold if in a UTF-8 locale, and then only the Sharp S; everything
4504 * else is 1-1 and isn't assumed to be folded. In a UTF-8 pattern, we
4505 * need to fold as many characters as a single character can fold to,
4506 * so that later we can check if the first ones are such a multi-char
4507 * fold. But, in such a pattern only locale-problematic characters
4508 * aren't folded, so we can skip this completely if the first character
4509 * in the node isn't one of the tricky ones */
4510 if (OP(text_node) == EXACTFL) {
4512 if (! is_utf8_pat) {
4513 if (IN_UTF8_CTYPE_LOCALE && *pat == LATIN_SMALL_LETTER_SHARP_S)
4515 folded[0] = folded[1] = 's';
4517 pat_end = folded + 2;
4520 else if (is_PROBLEMATIC_LOCALE_FOLDEDS_START_utf8(pat)) {
4525 for (i = 0; i < UTF8_MAX_FOLD_CHAR_EXPAND && s < pat_end; i++) {
4527 *(d++) = (U8) toFOLD_LC(*s);
4532 _toFOLD_utf8_flags(s,
4536 FOLD_FLAGS_FULL | FOLD_FLAGS_LOCALE);
4547 if ((is_utf8_pat && is_MULTI_CHAR_FOLD_utf8_safe(pat, pat_end))
4548 || (!is_utf8_pat && is_MULTI_CHAR_FOLD_latin1_safe(pat, pat_end)))
4550 /* Multi-character folds require more context to sort out. Also
4551 * PL_utf8_foldclosures used below doesn't handle them, so have to
4552 * be handled outside this routine */
4553 use_chrtest_void = TRUE;
4555 else { /* an EXACTFish node which doesn't begin with a multi-char fold */
4556 c1 = is_utf8_pat ? valid_utf8_to_uvchr(pat, NULL) : *pat;
4558 const unsigned int * remaining_folds_to_list;
4559 unsigned int first_folds_to;
4561 /* Look up what code points (besides c1) fold to c1; e.g.,
4562 * [ 'K', KELVIN_SIGN ] both fold to 'k'. */
4563 Size_t folds_to_count = _inverse_folds(c1,
4565 &remaining_folds_to_list);
4566 if (folds_to_count == 0) {
4567 c2 = c1; /* there is only a single character that could
4570 else if (folds_to_count != 1) {
4571 /* If there aren't exactly two folds to this (itself and
4572 * another), it is outside the scope of this function */
4573 use_chrtest_void = TRUE;
4575 else { /* There are two. We already have one, get the other */
4576 c2 = first_folds_to;
4578 /* Folds that cross the 255/256 boundary are forbidden if
4579 * EXACTFL (and isnt a UTF8 locale), or EXACTFAA and one is
4580 * ASCIII. The only other match to c1 is c2, and since c1
4581 * is above 255, c2 better be as well under these
4582 * circumstances. If it isn't, it means the only legal
4583 * match of c1 is itself. */
4585 && ( ( OP(text_node) == EXACTFL
4586 && ! IN_UTF8_CTYPE_LOCALE)
4587 || (( OP(text_node) == EXACTFAA
4588 || OP(text_node) == EXACTFAA_NO_TRIE)
4589 && (isASCII(c1) || isASCII(c2)))))
4595 else /* Here, c1 is <= 255 */
4597 && HAS_NONLATIN1_FOLD_CLOSURE(c1)
4598 && ( ! (OP(text_node) == EXACTFL && ! IN_UTF8_CTYPE_LOCALE))
4599 && ((OP(text_node) != EXACTFAA
4600 && OP(text_node) != EXACTFAA_NO_TRIE)
4603 /* Here, there could be something above Latin1 in the target
4604 * which folds to this character in the pattern. All such
4605 * cases except LATIN SMALL LETTER Y WITH DIAERESIS have more
4606 * than two characters involved in their folds, so are outside
4607 * the scope of this function */
4608 if (UNLIKELY(c1 == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
4609 c2 = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS;
4612 use_chrtest_void = TRUE;
4615 else { /* Here nothing above Latin1 can fold to the pattern
4617 switch (OP(text_node)) {
4619 case EXACTFL: /* /l rules */
4620 c2 = PL_fold_locale[c1];
4623 case EXACTF: /* This node only generated for non-utf8
4625 assert(! is_utf8_pat);
4626 if (! utf8_target) { /* /d rules */
4631 /* /u rules for all these. This happens to work for
4632 * EXACTFAA as nothing in Latin1 folds to ASCII */
4633 case EXACTFAA_NO_TRIE: /* This node only generated for
4634 non-utf8 patterns */
4635 assert(! is_utf8_pat);
4640 c2 = PL_fold_latin1[c1];
4644 Perl_croak(aTHX_ "panic: Unexpected op %u", OP(text_node));
4645 NOT_REACHED; /* NOTREACHED */
4651 /* Here have figured things out. Set up the returns */
4652 if (use_chrtest_void) {
4653 *c2p = *c1p = CHRTEST_VOID;
4655 else if (utf8_target) {
4656 if (! utf8_has_been_setup) { /* Don't have the utf8; must get it */
4657 uvchr_to_utf8(c1_utf8, c1);
4658 uvchr_to_utf8(c2_utf8, c2);
4661 /* Invariants are stored in both the utf8 and byte outputs; Use
4662 * negative numbers otherwise for the byte ones. Make sure that the
4663 * byte ones are the same iff the utf8 ones are the same */
4664 *c1p = (UTF8_IS_INVARIANT(*c1_utf8)) ? *c1_utf8 : CHRTEST_NOT_A_CP_1;
4665 *c2p = (UTF8_IS_INVARIANT(*c2_utf8))
4668 ? CHRTEST_NOT_A_CP_1
4669 : CHRTEST_NOT_A_CP_2;
4671 else if (c1 > 255) {
4672 if (c2 > 255) { /* both possibilities are above what a non-utf8 string
4677 *c1p = *c2p = c2; /* c2 is the only representable value */
4679 else { /* c1 is representable; see about c2 */
4681 *c2p = (c2 < 256) ? c2 : c1;
4688 S_isGCB(pTHX_ const GCB_enum before, const GCB_enum after, const U8 * const strbeg, const U8 * const curpos, const bool utf8_target)
4690 /* returns a boolean indicating if there is a Grapheme Cluster Boundary
4691 * between the inputs. See http://www.unicode.org/reports/tr29/. */
4693 PERL_ARGS_ASSERT_ISGCB;
4695 switch (GCB_table[before][after]) {
4702 case GCB_RI_then_RI:
4705 U8 * temp_pos = (U8 *) curpos;
4707 /* Do not break within emoji flag sequences. That is, do not
4708 * break between regional indicator (RI) symbols if there is an
4709 * odd number of RI characters before the break point.
4710 * GB12 sot (RI RI)* RI × RI
4711 * GB13 [^RI] (RI RI)* RI × RI */
4713 while (backup_one_GCB(strbeg,
4715 utf8_target) == GCB_Regional_Indicator)
4720 return RI_count % 2 != 1;
4723 case GCB_EX_then_EM:
4725 /* GB10 ( E_Base | E_Base_GAZ ) Extend* × E_Modifier */
4727 U8 * temp_pos = (U8 *) curpos;
4731 prev = backup_one_GCB(strbeg, &temp_pos, utf8_target);
4733 while (prev == GCB_Extend);
4735 return prev != GCB_E_Base && prev != GCB_E_Base_GAZ;
4738 case GCB_Maybe_Emoji_NonBreak:
4742 /* Do not break within emoji modifier sequences or emoji zwj sequences.
4743 GB11 \p{Extended_Pictographic} Extend* ZWJ × \p{Extended_Pictographic}
4745 U8 * temp_pos = (U8 *) curpos;
4749 prev = backup_one_GCB(strbeg, &temp_pos, utf8_target);
4751 while (prev == GCB_Extend);
4753 return prev != GCB_XPG_XX;
4761 Perl_re_printf( aTHX_ "Unhandled GCB pair: GCB_table[%d, %d] = %d\n",
4762 before, after, GCB_table[before][after]);
4769 S_backup_one_GCB(pTHX_ const U8 * const strbeg, U8 ** curpos, const bool utf8_target)
4773 PERL_ARGS_ASSERT_BACKUP_ONE_GCB;
4775 if (*curpos < strbeg) {
4780 U8 * prev_char_pos = reghopmaybe3(*curpos, -1, strbeg);
4781 U8 * prev_prev_char_pos;
4783 if (! prev_char_pos) {
4787 if ((prev_prev_char_pos = reghopmaybe3((U8 *) prev_char_pos, -1, strbeg))) {
4788 gcb = getGCB_VAL_UTF8(prev_prev_char_pos, prev_char_pos);
4789 *curpos = prev_char_pos;
4790 prev_char_pos = prev_prev_char_pos;
4793 *curpos = (U8 *) strbeg;
4798 if (*curpos - 2 < strbeg) {
4799 *curpos = (U8 *) strbeg;
4803 gcb = getGCB_VAL_CP(*(*curpos - 1));
4809 /* Combining marks attach to most classes that precede them, but this defines
4810 * the exceptions (from TR14) */
4811 #define LB_CM_ATTACHES_TO(prev) ( ! ( prev == LB_EDGE \
4812 || prev == LB_Mandatory_Break \
4813 || prev == LB_Carriage_Return \
4814 || prev == LB_Line_Feed \
4815 || prev == LB_Next_Line \
4816 || prev == LB_Space \
4817 || prev == LB_ZWSpace))
4820 S_isLB(pTHX_ LB_enum before,
4822 const U8 * const strbeg,
4823 const U8 * const curpos,
4824 const U8 * const strend,
4825 const bool utf8_target)
4827 U8 * temp_pos = (U8 *) curpos;
4828 LB_enum prev = before;
4830 /* Is the boundary between 'before' and 'after' line-breakable?
4831 * Most of this is just a table lookup of a generated table from Unicode
4832 * rules. But some rules require context to decide, and so have to be
4833 * implemented in code */
4835 PERL_ARGS_ASSERT_ISLB;
4837 /* Rule numbers in the comments below are as of Unicode 9.0 */
4841 switch (LB_table[before][after]) {
4846 case LB_NOBREAK_EVEN_WITH_SP_BETWEEN:
4849 case LB_SP_foo + LB_BREAKABLE:
4850 case LB_SP_foo + LB_NOBREAK:
4851 case LB_SP_foo + LB_NOBREAK_EVEN_WITH_SP_BETWEEN:
4853 /* When we have something following a SP, we have to look at the
4854 * context in order to know what to do.
4856 * SP SP should not reach here because LB7: Do not break before
4857 * spaces. (For two spaces in a row there is nothing that
4858 * overrides that) */
4859 assert(after != LB_Space);
4861 /* Here we have a space followed by a non-space. Mostly this is a
4862 * case of LB18: "Break after spaces". But there are complications
4863 * as the handling of spaces is somewhat tricky. They are in a
4864 * number of rules, which have to be applied in priority order, but
4865 * something earlier in the string can cause a rule to be skipped
4866 * and a lower priority rule invoked. A prime example is LB7 which
4867 * says don't break before a space. But rule LB8 (lower priority)
4868 * says that the first break opportunity after a ZW is after any
4869 * span of spaces immediately after it. If a ZW comes before a SP
4870 * in the input, rule LB8 applies, and not LB7. Other such rules
4871 * involve combining marks which are rules 9 and 10, but they may
4872 * override higher priority rules if they come earlier in the
4873 * string. Since we're doing random access into the middle of the
4874 * string, we have to look for rules that should get applied based
4875 * on both string position and priority. Combining marks do not
4876 * attach to either ZW nor SP, so we don't have to consider them
4879 * To check for LB8, we have to find the first non-space character
4880 * before this span of spaces */
4882 prev = backup_one_LB(strbeg, &temp_pos, utf8_target);
4884 while (prev == LB_Space);
4886 /* LB8 Break before any character following a zero-width space,
4887 * even if one or more spaces intervene.
4889 * So if we have a ZW just before this span, and to get here this
4890 * is the final space in the span. */
4891 if (prev == LB_ZWSpace) {
4895 /* Here, not ZW SP+. There are several rules that have higher
4896 * priority than LB18 and can be resolved now, as they don't depend
4897 * on anything earlier in the string (except ZW, which we have
4898 * already handled). One of these rules is LB11 Do not break
4899 * before Word joiner, but we have specially encoded that in the
4900 * lookup table so it is caught by the single test below which
4901 * catches the other ones. */
4902 if (LB_table[LB_Space][after] - LB_SP_foo
4903 == LB_NOBREAK_EVEN_WITH_SP_BETWEEN)
4908 /* If we get here, we have to XXX consider combining marks. */
4909 if (prev == LB_Combining_Mark) {
4911 /* What happens with these depends on the character they
4914 prev = backup_one_LB(strbeg, &temp_pos, utf8_target);
4916 while (prev == LB_Combining_Mark);
4918 /* Most times these attach to and inherit the characteristics
4919 * of that character, but not always, and when not, they are to
4920 * be treated as AL by rule LB10. */
4921 if (! LB_CM_ATTACHES_TO(prev)) {
4922 prev = LB_Alphabetic;
4926 /* Here, we have the character preceding the span of spaces all set
4927 * up. We follow LB18: "Break after spaces" unless the table shows
4928 * that is overriden */
4929 return LB_table[prev][after] != LB_NOBREAK_EVEN_WITH_SP_BETWEEN;
4933 /* We don't know how to treat the CM except by looking at the first
4934 * non-CM character preceding it. ZWJ is treated as CM */
4936 prev = backup_one_LB(strbeg, &temp_pos, utf8_target);
4938 while (prev == LB_Combining_Mark || prev == LB_ZWJ);
4940 /* Here, 'prev' is that first earlier non-CM character. If the CM
4941 * attatches to it, then it inherits the behavior of 'prev'. If it
4942 * doesn't attach, it is to be treated as an AL */
4943 if (! LB_CM_ATTACHES_TO(prev)) {
4944 prev = LB_Alphabetic;
4949 case LB_HY_or_BA_then_foo + LB_BREAKABLE:
4950 case LB_HY_or_BA_then_foo + LB_NOBREAK:
4952 /* LB21a Don't break after Hebrew + Hyphen.
4953 * HL (HY | BA) × */
4955 if (backup_one_LB(strbeg, &temp_pos, utf8_target)
4956 == LB_Hebrew_Letter)
4961 return LB_table[prev][after] - LB_HY_or_BA_then_foo == LB_BREAKABLE;
4963 case LB_PR_or_PO_then_OP_or_HY + LB_BREAKABLE:
4964 case LB_PR_or_PO_then_OP_or_HY + LB_NOBREAK:
4966 /* LB25a (PR | PO) × ( OP | HY )? NU */
4967 if (advance_one_LB(&temp_pos, strend, utf8_target) == LB_Numeric) {
4971 return LB_table[prev][after] - LB_PR_or_PO_then_OP_or_HY
4974 case LB_SY_or_IS_then_various + LB_BREAKABLE:
4975 case LB_SY_or_IS_then_various + LB_NOBREAK:
4977 /* LB25d NU (SY | IS)* × (NU | SY | IS | CL | CP ) */
4979 LB_enum temp = prev;
4981 temp = backup_one_LB(strbeg, &temp_pos, utf8_target);
4983 while (temp == LB_Break_Symbols || temp == LB_Infix_Numeric);
4984 if (temp == LB_Numeric) {
4988 return LB_table[prev][after] - LB_SY_or_IS_then_various
4992 case LB_various_then_PO_or_PR + LB_BREAKABLE:
4993 case LB_various_then_PO_or_PR + LB_NOBREAK:
4995 /* LB25e NU (SY | IS)* (CL | CP)? × (PO | PR) */
4997 LB_enum temp = prev;
4998 if (temp == LB_Close_Punctuation || temp == LB_Close_Parenthesis)
5000 temp = backup_one_LB(strbeg, &temp_pos, utf8_target);
5002 while (temp == LB_Break_Symbols || temp == LB_Infix_Numeric) {
5003 temp = backup_one_LB(strbeg, &temp_pos, utf8_target);
5005 if (temp == LB_Numeric) {
5008 return LB_various_then_PO_or_PR;
5011 case LB_RI_then_RI + LB_NOBREAK:
5012 case LB_RI_then_RI + LB_BREAKABLE:
5016 /* LB30a Break between two regional indicator symbols if and
5017 * only if there are an even number of regional indicators
5018 * preceding the position of the break.
5020 * sot (RI RI)* RI × RI
5021 * [^RI] (RI RI)* RI × RI */
5023 while (backup_one_LB(strbeg,
5025 utf8_target) == LB_Regional_Indicator)
5030 return RI_count % 2 == 0;
5038 Perl_re_printf( aTHX_ "Unhandled LB pair: LB_table[%d, %d] = %d\n",
5039 before, after, LB_table[before][after]);
5046 S_advance_one_LB(pTHX_ U8 ** curpos, const U8 * const strend, const bool utf8_target)
5050 PERL_ARGS_ASSERT_ADVANCE_ONE_LB;
5052 if (*curpos >= strend) {
5057 *curpos += UTF8SKIP(*curpos);
5058 if (*curpos >= strend) {
5061 lb = getLB_VAL_UTF8(*curpos, strend);
5065 if (*curpos >= strend) {
5068 lb = getLB_VAL_CP(**curpos);
5075 S_backup_one_LB(pTHX_ const U8 * const strbeg, U8 ** curpos, const bool utf8_target)
5079 PERL_ARGS_ASSERT_BACKUP_ONE_LB;
5081 if (*curpos < strbeg) {
5086 U8 * prev_char_pos = reghopmaybe3(*curpos, -1, strbeg);
5087 U8 * prev_prev_char_pos;
5089 if (! prev_char_pos) {
5093 if ((prev_prev_char_pos = reghopmaybe3((U8 *) prev_char_pos, -1, strbeg))) {
5094 lb = getLB_VAL_UTF8(prev_prev_char_pos, prev_char_pos);
5095 *curpos = prev_char_pos;
5096 prev_char_pos = prev_prev_char_pos;
5099 *curpos = (U8 *) strbeg;
5104 if (*curpos - 2 < strbeg) {
5105 *curpos = (U8 *) strbeg;
5109 lb = getLB_VAL_CP(*(*curpos - 1));
5116 S_isSB(pTHX_ SB_enum before,
5118 const U8 * const strbeg,
5119 const U8 * const curpos,
5120 const U8 * const strend,
5121 const bool utf8_target)
5123 /* returns a boolean indicating if there is a Sentence Boundary Break
5124 * between the inputs. See http://www.unicode.org/reports/tr29/ */
5126 U8 * lpos = (U8 *) curpos;
5127 bool has_para_sep = FALSE;
5128 bool has_sp = FALSE;
5130 PERL_ARGS_ASSERT_ISSB;
5132 /* Break at the start and end of text.
5135 But unstated in Unicode is don't break if the text is empty */
5136 if (before == SB_EDGE || after == SB_EDGE) {
5137 return before != after;
5140 /* SB 3: Do not break within CRLF. */
5141 if (before == SB_CR && after == SB_LF) {
5145 /* Break after paragraph separators. CR and LF are considered
5146 * so because Unicode views text as like word processing text where there
5147 * are no newlines except between paragraphs, and the word processor takes
5148 * care of wrapping without there being hard line-breaks in the text *./
5149 SB4. Sep | CR | LF ÷ */
5150 if (before == SB_Sep || before == SB_CR || before == SB_LF) {
5154 /* Ignore Format and Extend characters, except after sot, Sep, CR, or LF.
5155 * (See Section 6.2, Replacing Ignore Rules.)
5156 SB5. X (Extend | Format)* → X */
5157 if (after == SB_Extend || after == SB_Format) {
5159 /* Implied is that the these characters attach to everything
5160 * immediately prior to them except for those separator-type
5161 * characters. And the rules earlier have already handled the case
5162 * when one of those immediately precedes the extend char */
5166 if (before == SB_Extend || before == SB_Format) {
5167 U8 * temp_pos = lpos;
5168 const SB_enum backup = backup_one_SB(strbeg, &temp_pos, utf8_target);
5169 if ( backup != SB_EDGE
5178 /* Here, both 'before' and 'backup' are these types; implied is that we
5179 * don't break between them */
5180 if (backup == SB_Extend || backup == SB_Format) {
5185 /* Do not break after ambiguous terminators like period, if they are
5186 * immediately followed by a number or lowercase letter, if they are
5187 * between uppercase letters, if the first following letter (optionally
5188 * after certain punctuation) is lowercase, or if they are followed by
5189 * "continuation" punctuation such as comma, colon, or semicolon. For
5190 * example, a period may be an abbreviation or numeric period, and thus may
5191 * not mark the end of a sentence.
5193 * SB6. ATerm × Numeric */
5194 if (before == SB_ATerm && after == SB_Numeric) {
5198 /* SB7. (Upper | Lower) ATerm × Upper */
5199 if (before == SB_ATerm && after == SB_Upper) {
5200 U8 * temp_pos = lpos;
5201 SB_enum backup = backup_one_SB(strbeg, &temp_pos, utf8_target);
5202 if (backup == SB_Upper || backup == SB_Lower) {
5207 /* The remaining rules that aren't the final one, all require an STerm or
5208 * an ATerm after having backed up over some Close* Sp*, and in one case an
5209 * optional Paragraph separator, although one rule doesn't have any Sp's in it.
5210 * So do that backup now, setting flags if either Sp or a paragraph
5211 * separator are found */
5213 if (before == SB_Sep || before == SB_CR || before == SB_LF) {
5214 has_para_sep = TRUE;
5215 before = backup_one_SB(strbeg, &lpos, utf8_target);
5218 if (before == SB_Sp) {
5221 before = backup_one_SB(strbeg, &lpos, utf8_target);
5223 while (before == SB_Sp);
5226 while (before == SB_Close) {
5227 before = backup_one_SB(strbeg, &lpos, utf8_target);
5230 /* The next few rules apply only when the backed-up-to is an ATerm, and in
5231 * most cases an STerm */
5232 if (before == SB_STerm || before == SB_ATerm) {
5234 /* So, here the lhs matches
5235 * (STerm | ATerm) Close* Sp* (Sep | CR | LF)?
5236 * and we have set flags if we found an Sp, or the optional Sep,CR,LF.
5237 * The rules that apply here are:
5239 * SB8 ATerm Close* Sp* × ( ¬(OLetter | Upper | Lower | Sep | CR
5240 | LF | STerm | ATerm) )* Lower
5241 SB8a (STerm | ATerm) Close* Sp* × (SContinue | STerm | ATerm)
5242 SB9 (STerm | ATerm) Close* × (Close | Sp | Sep | CR | LF)
5243 SB10 (STerm | ATerm) Close* Sp* × (Sp | Sep | CR | LF)
5244 SB11 (STerm | ATerm) Close* Sp* (Sep | CR | LF)? ÷
5247 /* And all but SB11 forbid having seen a paragraph separator */
5248 if (! has_para_sep) {
5249 if (before == SB_ATerm) { /* SB8 */
5250 U8 * rpos = (U8 *) curpos;
5251 SB_enum later = after;
5253 while ( later != SB_OLetter
5254 && later != SB_Upper
5255 && later != SB_Lower
5259 && later != SB_STerm
5260 && later != SB_ATerm
5261 && later != SB_EDGE)
5263 later = advance_one_SB(&rpos, strend, utf8_target);
5265 if (later == SB_Lower) {
5270 if ( after == SB_SContinue /* SB8a */
5271 || after == SB_STerm
5272 || after == SB_ATerm)
5277 if (! has_sp) { /* SB9 applies only if there was no Sp* */
5278 if ( after == SB_Close
5288 /* SB10. This and SB9 could probably be combined some way, but khw
5289 * has decided to follow the Unicode rule book precisely for
5290 * simplified maintenance */
5304 /* Otherwise, do not break.
5311 S_advance_one_SB(pTHX_ U8 ** curpos, const U8 * const strend, const bool utf8_target)
5315 PERL_ARGS_ASSERT_ADVANCE_ONE_SB;
5317 if (*curpos >= strend) {
5323 *curpos += UTF8SKIP(*curpos);
5324 if (*curpos >= strend) {
5327 sb = getSB_VAL_UTF8(*curpos, strend);
5328 } while (sb == SB_Extend || sb == SB_Format);
5333 if (*curpos >= strend) {
5336 sb = getSB_VAL_CP(**curpos);
5337 } while (sb == SB_Extend || sb == SB_Format);
5344 S_backup_one_SB(pTHX_ const U8 * const strbeg, U8 ** curpos, const bool utf8_target)
5348 PERL_ARGS_ASSERT_BACKUP_ONE_SB;
5350 if (*curpos < strbeg) {
5355 U8 * prev_char_pos = reghopmaybe3(*curpos, -1, strbeg);
5356 if (! prev_char_pos) {
5360 /* Back up over Extend and Format. curpos is always just to the right
5361 * of the characater whose value we are getting */
5363 U8 * prev_prev_char_pos;
5364 if ((prev_prev_char_pos = reghopmaybe3((U8 *) prev_char_pos, -1,
5367 sb = getSB_VAL_UTF8(prev_prev_char_pos, prev_char_pos);
5368 *curpos = prev_char_pos;
5369 prev_char_pos = prev_prev_char_pos;
5372 *curpos = (U8 *) strbeg;
5375 } while (sb == SB_Extend || sb == SB_Format);
5379 if (*curpos - 2 < strbeg) {
5380 *curpos = (U8 *) strbeg;
5384 sb = getSB_VAL_CP(*(*curpos - 1));
5385 } while (sb == SB_Extend || sb == SB_Format);
5392 S_isWB(pTHX_ WB_enum previous,
5395 const U8 * const strbeg,
5396 const U8 * const curpos,
5397 const U8 * const strend,
5398 const bool utf8_target)
5400 /* Return a boolean as to if the boundary between 'before' and 'after' is
5401 * a Unicode word break, using their published algorithm, but tailored for
5402 * Perl by treating spans of white space as one unit. Context may be
5403 * needed to make this determination. If the value for the character
5404 * before 'before' is known, it is passed as 'previous'; otherwise that
5405 * should be set to WB_UNKNOWN. The other input parameters give the
5406 * boundaries and current position in the matching of the string. That
5407 * is, 'curpos' marks the position where the character whose wb value is
5408 * 'after' begins. See http://www.unicode.org/reports/tr29/ */
5410 U8 * before_pos = (U8 *) curpos;
5411 U8 * after_pos = (U8 *) curpos;
5412 WB_enum prev = before;
5415 PERL_ARGS_ASSERT_ISWB;
5417 /* Rule numbers in the comments below are as of Unicode 9.0 */
5421 switch (WB_table[before][after]) {
5428 case WB_hs_then_hs: /* 2 horizontal spaces in a row */
5429 next = advance_one_WB(&after_pos, strend, utf8_target,
5430 FALSE /* Don't skip Extend nor Format */ );
5431 /* A space immediately preceeding an Extend or Format is attached
5432 * to by them, and hence gets separated from previous spaces.
5433 * Otherwise don't break between horizontal white space */
5434 return next == WB_Extend || next == WB_Format;
5436 /* WB4 Ignore Format and Extend characters, except when they appear at
5437 * the beginning of a region of text. This code currently isn't
5438 * general purpose, but it works as the rules are currently and likely
5439 * to be laid out. The reason it works is that when 'they appear at
5440 * the beginning of a region of text', the rule is to break before
5441 * them, just like any other character. Therefore, the default rule
5442 * applies and we don't have to look in more depth. Should this ever
5443 * change, we would have to have 2 'case' statements, like in the rules
5444 * below, and backup a single character (not spacing over the extend
5445 * ones) and then see if that is one of the region-end characters and
5447 case WB_Ex_or_FO_or_ZWJ_then_foo:
5448 prev = backup_one_WB(&previous, strbeg, &before_pos, utf8_target);
5451 case WB_DQ_then_HL + WB_BREAKABLE:
5452 case WB_DQ_then_HL + WB_NOBREAK:
5454 /* WB7c Hebrew_Letter Double_Quote × Hebrew_Letter */
5456 if (backup_one_WB(&previous, strbeg, &before_pos, utf8_target)
5457 == WB_Hebrew_Letter)
5462 return WB_table[before][after] - WB_DQ_then_HL == WB_BREAKABLE;
5464 case WB_HL_then_DQ + WB_BREAKABLE:
5465 case WB_HL_then_DQ + WB_NOBREAK:
5467 /* WB7b Hebrew_Letter × Double_Quote Hebrew_Letter */
5469 if (advance_one_WB(&after_pos, strend, utf8_target,
5470 TRUE /* Do skip Extend and Format */ )
5471 == WB_Hebrew_Letter)
5476 return WB_table[before][after] - WB_HL_then_DQ == WB_BREAKABLE;
5478 case WB_LE_or_HL_then_MB_or_ML_or_SQ + WB_NOBREAK:
5479 case WB_LE_or_HL_then_MB_or_ML_or_SQ + WB_BREAKABLE:
5481 /* WB6 (ALetter | Hebrew_Letter) × (MidLetter | MidNumLet
5482 * | Single_Quote) (ALetter | Hebrew_Letter) */
5484 next = advance_one_WB(&after_pos, strend, utf8_target,
5485 TRUE /* Do skip Extend and Format */ );
5487 if (next == WB_ALetter || next == WB_Hebrew_Letter)
5492 return WB_table[before][after]
5493 - WB_LE_or_HL_then_MB_or_ML_or_SQ == WB_BREAKABLE;
5495 case WB_MB_or_ML_or_SQ_then_LE_or_HL + WB_NOBREAK:
5496 case WB_MB_or_ML_or_SQ_then_LE_or_HL + WB_BREAKABLE:
5498 /* WB7 (ALetter | Hebrew_Letter) (MidLetter | MidNumLet
5499 * | Single_Quote) × (ALetter | Hebrew_Letter) */
5501 prev = backup_one_WB(&previous, strbeg, &before_pos, utf8_target);
5502 if (prev == WB_ALetter || prev == WB_Hebrew_Letter)
5507 return WB_table[before][after]
5508 - WB_MB_or_ML_or_SQ_then_LE_or_HL == WB_BREAKABLE;
5510 case WB_MB_or_MN_or_SQ_then_NU + WB_NOBREAK:
5511 case WB_MB_or_MN_or_SQ_then_NU + WB_BREAKABLE:
5513 /* WB11 Numeric (MidNum | (MidNumLet | Single_Quote)) × Numeric
5516 if (backup_one_WB(&previous, strbeg, &before_pos, utf8_target)
5522 return WB_table[before][after]
5523 - WB_MB_or_MN_or_SQ_then_NU == WB_BREAKABLE;
5525 case WB_NU_then_MB_or_MN_or_SQ + WB_NOBREAK:
5526 case WB_NU_then_MB_or_MN_or_SQ + WB_BREAKABLE:
5528 /* WB12 Numeric × (MidNum | MidNumLet | Single_Quote) Numeric */
5530 if (advance_one_WB(&after_pos, strend, utf8_target,
5531 TRUE /* Do skip Extend and Format */ )
5537 return WB_table[before][after]
5538 - WB_NU_then_MB_or_MN_or_SQ == WB_BREAKABLE;
5540 case WB_RI_then_RI + WB_NOBREAK:
5541 case WB_RI_then_RI + WB_BREAKABLE:
5545 /* Do not break within emoji flag sequences. That is, do not
5546 * break between regional indicator (RI) symbols if there is an
5547 * odd number of RI characters before the potential break
5550 * WB15 sot (RI RI)* RI × RI
5551 * WB16 [^RI] (RI RI)* RI × RI */
5553 while (backup_one_WB(&previous,
5556 utf8_target) == WB_Regional_Indicator)
5561 return RI_count % 2 != 1;
5569 Perl_re_printf( aTHX_ "Unhandled WB pair: WB_table[%d, %d] = %d\n",
5570 before, after, WB_table[before][after]);
5577 S_advance_one_WB(pTHX_ U8 ** curpos,
5578 const U8 * const strend,
5579 const bool utf8_target,
5580 const bool skip_Extend_Format)
5584 PERL_ARGS_ASSERT_ADVANCE_ONE_WB;
5586 if (*curpos >= strend) {
5592 /* Advance over Extend and Format */
5594 *curpos += UTF8SKIP(*curpos);
5595 if (*curpos >= strend) {
5598 wb = getWB_VAL_UTF8(*curpos, strend);
5599 } while ( skip_Extend_Format
5600 && (wb == WB_Extend || wb == WB_Format));
5605 if (*curpos >= strend) {
5608 wb = getWB_VAL_CP(**curpos);
5609 } while ( skip_Extend_Format
5610 && (wb == WB_Extend || wb == WB_Format));
5617 S_backup_one_WB(pTHX_ WB_enum * previous, const U8 * const strbeg, U8 ** curpos, const bool utf8_target)
5621 PERL_ARGS_ASSERT_BACKUP_ONE_WB;
5623 /* If we know what the previous character's break value is, don't have
5625 if (*previous != WB_UNKNOWN) {
5628 /* But we need to move backwards by one */
5630 *curpos = reghopmaybe3(*curpos, -1, strbeg);
5632 *previous = WB_EDGE;
5633 *curpos = (U8 *) strbeg;
5636 *previous = WB_UNKNOWN;
5641 *previous = (*curpos <= strbeg) ? WB_EDGE : WB_UNKNOWN;
5644 /* And we always back up over these three types */
5645 if (wb != WB_Extend && wb != WB_Format && wb != WB_ZWJ) {
5650 if (*curpos < strbeg) {
5655 U8 * prev_char_pos = reghopmaybe3(*curpos, -1, strbeg);
5656 if (! prev_char_pos) {
5660 /* Back up over Extend and Format. curpos is always just to the right
5661 * of the characater whose value we are getting */
5663 U8 * prev_prev_char_pos;
5664 if ((prev_prev_char_pos = reghopmaybe3((U8 *) prev_char_pos,
5668 wb = getWB_VAL_UTF8(prev_prev_char_pos, prev_char_pos);
5669 *curpos = prev_char_pos;
5670 prev_char_pos = prev_prev_char_pos;
5673 *curpos = (U8 *) strbeg;
5676 } while (wb == WB_Extend || wb == WB_Format || wb == WB_ZWJ);
5680 if (*curpos - 2 < strbeg) {
5681 *curpos = (U8 *) strbeg;
5685 wb = getWB_VAL_CP(*(*curpos - 1));
5686 } while (wb == WB_Extend || wb == WB_Format);
5692 #define EVAL_CLOSE_PAREN_IS(st,expr) \
5695 ( ( st )->u.eval.close_paren ) && \
5696 ( ( ( st )->u.eval.close_paren ) == ( (expr) + 1 ) ) \
5699 #define EVAL_CLOSE_PAREN_IS_TRUE(st,expr) \
5702 ( ( st )->u.eval.close_paren ) && \
5704 ( ( ( st )->u.eval.close_paren ) == ( (expr) + 1 ) ) \
5708 #define EVAL_CLOSE_PAREN_SET(st,expr) \
5709 (st)->u.eval.close_paren = ( (expr) + 1 )
5711 #define EVAL_CLOSE_PAREN_CLEAR(st) \
5712 (st)->u.eval.close_paren = 0
5714 /* returns -1 on failure, $+[0] on success */
5716 S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
5719 const bool utf8_target = reginfo->is_utf8_target;
5720 const U32 uniflags = UTF8_ALLOW_DEFAULT;
5721 REGEXP *rex_sv = reginfo->prog;
5722 regexp *rex = ReANY(rex_sv);
5723 RXi_GET_DECL(rex,rexi);
5724 /* the current state. This is a cached copy of PL_regmatch_state */
5726 /* cache heavy used fields of st in registers */
5729 U32 n = 0; /* general value; init to avoid compiler warning */
5730 SSize_t ln = 0; /* len or last; init to avoid compiler warning */
5731 SSize_t endref = 0; /* offset of end of backref when ln is start */
5732 char *locinput = startpos;
5733 char *pushinput; /* where to continue after a PUSH */
5734 I32 nextchr; /* is always set to UCHARAT(locinput), or -1 at EOS */
5736 bool result = 0; /* return value of S_regmatch */
5737 U32 depth = 0; /* depth of backtrack stack */
5738 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
5739 const U32 max_nochange_depth =
5740 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
5741 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
5742 regmatch_state *yes_state = NULL; /* state to pop to on success of
5744 /* mark_state piggy backs on the yes_state logic so that when we unwind
5745 the stack on success we can update the mark_state as we go */
5746 regmatch_state *mark_state = NULL; /* last mark state we have seen */
5747 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
5748 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
5750 bool no_final = 0; /* prevent failure from backtracking? */
5751 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
5752 char *startpoint = locinput;
5753 SV *popmark = NULL; /* are we looking for a mark? */
5754 SV *sv_commit = NULL; /* last mark name seen in failure */
5755 SV *sv_yes_mark = NULL; /* last mark name we have seen
5756 during a successful match */
5757 U32 lastopen = 0; /* last open we saw */
5758 bool has_cutgroup = RXp_HAS_CUTGROUP(rex) ? 1 : 0;
5759 SV* const oreplsv = GvSVn(PL_replgv);
5760 /* these three flags are set by various ops to signal information to
5761 * the very next op. They have a useful lifetime of exactly one loop
5762 * iteration, and are not preserved or restored by state pushes/pops
5764 bool sw = 0; /* the condition value in (?(cond)a|b) */
5765 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
5766 int logical = 0; /* the following EVAL is:
5770 or the following IFMATCH/UNLESSM is:
5771 false: plain (?=foo)
5772 true: used as a condition: (?(?=foo))
5774 PAD* last_pad = NULL;
5776 U8 gimme = G_SCALAR;
5777 CV *caller_cv = NULL; /* who called us */
5778 CV *last_pushed_cv = NULL; /* most recently called (?{}) CV */
5779 U32 maxopenparen = 0; /* max '(' index seen so far */
5780 int to_complement; /* Invert the result? */
5781 _char_class_number classnum;
5782 bool is_utf8_pat = reginfo->is_utf8_pat;
5784 I32 orig_savestack_ix = PL_savestack_ix;
5785 U8 * script_run_begin = NULL;
5787 /* Solaris Studio 12.3 messes up fetching PL_charclass['\n'] */
5788 #if (defined(__SUNPRO_C) && (__SUNPRO_C == 0x5120) && defined(__x86_64) && defined(USE_64_BIT_ALL))
5789 # define SOLARIS_BAD_OPTIMIZER
5790 const U32 *pl_charclass_dup = PL_charclass;
5791 # define PL_charclass pl_charclass_dup
5795 GET_RE_DEBUG_FLAGS_DECL;
5798 /* protect against undef(*^R) */
5799 SAVEFREESV(SvREFCNT_inc_simple_NN(oreplsv));
5801 /* shut up 'may be used uninitialized' compiler warnings for dMULTICALL */
5802 multicall_oldcatch = 0;
5803 PERL_UNUSED_VAR(multicall_cop);
5805 PERL_ARGS_ASSERT_REGMATCH;
5807 st = PL_regmatch_state;
5809 /* Note that nextchr is a byte even in UTF */
5813 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
5814 DUMP_EXEC_POS( locinput, scan, utf8_target, depth );
5815 Perl_re_printf( aTHX_ "regmatch start\n" );
5818 while (scan != NULL) {
5819 next = scan + NEXT_OFF(scan);
5822 state_num = OP(scan);
5826 if (state_num <= REGNODE_MAX) {
5827 SV * const prop = sv_newmortal();
5828 regnode *rnext = regnext(scan);
5830 DUMP_EXEC_POS( locinput, scan, utf8_target, depth );
5831 regprop(rex, prop, scan, reginfo, NULL);
5832 Perl_re_printf( aTHX_
5833 "%*s%" IVdf ":%s(%" IVdf ")\n",
5834 INDENT_CHARS(depth), "",
5835 (IV)(scan - rexi->program),
5837 (PL_regkind[OP(scan)] == END || !rnext) ?
5838 0 : (IV)(rnext - rexi->program));
5845 assert(nextchr < 256 && (nextchr >= 0 || nextchr == NEXTCHR_EOS));
5847 switch (state_num) {
5848 case SBOL: /* /^../ and /\A../ */
5849 if (locinput == reginfo->strbeg)
5853 case MBOL: /* /^../m */
5854 if (locinput == reginfo->strbeg ||
5855 (!NEXTCHR_IS_EOS && locinput[-1] == '\n'))
5862 if (locinput == reginfo->ganch)
5866 case KEEPS: /* \K */
5867 /* update the startpoint */
5868 st->u.keeper.val = rex->offs[0].start;
5869 rex->offs[0].start = locinput - reginfo->strbeg;
5870 PUSH_STATE_GOTO(KEEPS_next, next, locinput);
5871 NOT_REACHED; /* NOTREACHED */
5873 case KEEPS_next_fail:
5874 /* rollback the start point change */
5875 rex->offs[0].start = st->u.keeper.val;
5877 NOT_REACHED; /* NOTREACHED */
5879 case MEOL: /* /..$/m */
5880 if (!NEXTCHR_IS_EOS && nextchr != '\n')
5884 case SEOL: /* /..$/ */
5885 if (!NEXTCHR_IS_EOS && nextchr != '\n')
5887 if (reginfo->strend - locinput > 1)
5892 if (!NEXTCHR_IS_EOS)
5896 case SANY: /* /./s */
5899 goto increment_locinput;
5901 case REG_ANY: /* /./ */
5902 if ((NEXTCHR_IS_EOS) || nextchr == '\n')
5904 goto increment_locinput;
5908 #define ST st->u.trie
5909 case TRIEC: /* (ab|cd) with known charclass */
5910 /* In this case the charclass data is available inline so
5911 we can fail fast without a lot of extra overhead.
5913 if(!NEXTCHR_IS_EOS && !ANYOF_BITMAP_TEST(scan, nextchr)) {
5915 Perl_re_exec_indentf( aTHX_ "%sTRIE: failed to match trie start class...%s\n",
5916 depth, PL_colors[4], PL_colors[5])
5919 NOT_REACHED; /* NOTREACHED */
5922 case TRIE: /* (ab|cd) */
5923 /* the basic plan of execution of the trie is:
5924 * At the beginning, run though all the states, and
5925 * find the longest-matching word. Also remember the position
5926 * of the shortest matching word. For example, this pattern:
5929 * when matched against the string "abcde", will generate
5930 * accept states for all words except 3, with the longest
5931 * matching word being 4, and the shortest being 2 (with
5932 * the position being after char 1 of the string).
5934 * Then for each matching word, in word order (i.e. 1,2,4,5),
5935 * we run the remainder of the pattern; on each try setting
5936 * the current position to the character following the word,
5937 * returning to try the next word on failure.
5939 * We avoid having to build a list of words at runtime by
5940 * using a compile-time structure, wordinfo[].prev, which
5941 * gives, for each word, the previous accepting word (if any).
5942 * In the case above it would contain the mappings 1->2, 2->0,
5943 * 3->0, 4->5, 5->1. We can use this table to generate, from
5944 * the longest word (4 above), a list of all words, by
5945 * following the list of prev pointers; this gives us the
5946 * unordered list 4,5,1,2. Then given the current word we have
5947 * just tried, we can go through the list and find the
5948 * next-biggest word to try (so if we just failed on word 2,
5949 * the next in the list is 4).
5951 * Since at runtime we don't record the matching position in
5952 * the string for each word, we have to work that out for
5953 * each word we're about to process. The wordinfo table holds
5954 * the character length of each word; given that we recorded
5955 * at the start: the position of the shortest word and its
5956 * length in chars, we just need to move the pointer the
5957 * difference between the two char lengths. Depending on
5958 * Unicode status and folding, that's cheap or expensive.
5960 * This algorithm is optimised for the case where are only a
5961 * small number of accept states, i.e. 0,1, or maybe 2.
5962 * With lots of accepts states, and having to try all of them,
5963 * it becomes quadratic on number of accept states to find all
5968 /* what type of TRIE am I? (utf8 makes this contextual) */
5969 DECL_TRIE_TYPE(scan);
5971 /* what trie are we using right now */
5972 reg_trie_data * const trie
5973 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
5974 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
5975 U32 state = trie->startstate;
5977 if (scan->flags == EXACTL || scan->flags == EXACTFLU8) {
5978 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
5981 && UTF8_IS_ABOVE_LATIN1(nextchr)
5982 && scan->flags == EXACTL)
5984 /* We only output for EXACTL, as we let the folder
5985 * output this message for EXACTFLU8 to avoid
5987 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(locinput,
5992 && (NEXTCHR_IS_EOS || !TRIE_BITMAP_TEST(trie, nextchr)))
5994 if (trie->states[ state ].wordnum) {
5996 Perl_re_exec_indentf( aTHX_ "%sTRIE: matched empty string...%s\n",
5997 depth, PL_colors[4], PL_colors[5])
6003 Perl_re_exec_indentf( aTHX_ "%sTRIE: failed to match trie start class...%s\n",
6004 depth, PL_colors[4], PL_colors[5])
6011 U8 *uc = ( U8* )locinput;
6015 U8 *uscan = (U8*)NULL;
6016 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
6017 U32 charcount = 0; /* how many input chars we have matched */
6018 U32 accepted = 0; /* have we seen any accepting states? */
6020 ST.jump = trie->jump;
6023 ST.longfold = FALSE; /* char longer if folded => it's harder */
6026 /* fully traverse the TRIE; note the position of the
6027 shortest accept state and the wordnum of the longest
6030 while ( state && uc <= (U8*)(reginfo->strend) ) {
6031 U32 base = trie->states[ state ].trans.base;
6035 wordnum = trie->states[ state ].wordnum;
6037 if (wordnum) { /* it's an accept state */
6040 /* record first match position */
6042 ST.firstpos = (U8*)locinput;
6047 ST.firstchars = charcount;
6050 if (!ST.nextword || wordnum < ST.nextword)
6051 ST.nextword = wordnum;
6052 ST.topword = wordnum;
6055 DEBUG_TRIE_EXECUTE_r({
6056 DUMP_EXEC_POS( (char *)uc, scan, utf8_target, depth );
6058 PerlIO_printf( Perl_debug_log,
6059 "%*s%sTRIE: State: %4" UVxf " Accepted: %c ",
6060 INDENT_CHARS(depth), "", PL_colors[4],
6061 (UV)state, (accepted ? 'Y' : 'N'));
6064 /* read a char and goto next state */
6065 if ( base && (foldlen || uc < (U8*)(reginfo->strend))) {
6067 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
6068 (U8 *) reginfo->strend, uscan,
6069 len, uvc, charid, foldlen,
6076 base + charid - 1 - trie->uniquecharcount)) >= 0)
6078 && ((U32)offset < trie->lasttrans)
6079 && trie->trans[offset].check == state)
6081 state = trie->trans[offset].next;
6092 DEBUG_TRIE_EXECUTE_r(
6093 Perl_re_printf( aTHX_
6094 "TRIE: Charid:%3x CP:%4" UVxf " After State: %4" UVxf "%s\n",
6095 charid, uvc, (UV)state, PL_colors[5] );
6101 /* calculate total number of accept states */
6106 w = trie->wordinfo[w].prev;
6109 ST.accepted = accepted;
6113 Perl_re_exec_indentf( aTHX_ "%sTRIE: got %" IVdf " possible matches%s\n",
6115 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
6117 goto trie_first_try; /* jump into the fail handler */
6119 NOT_REACHED; /* NOTREACHED */
6121 case TRIE_next_fail: /* we failed - try next alternative */
6125 /* undo any captures done in the tail part of a branch,
6127 * /(?:X(.)(.)|Y(.)).../
6128 * where the trie just matches X then calls out to do the
6129 * rest of the branch */
6130 REGCP_UNWIND(ST.cp);
6131 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
6133 if (!--ST.accepted) {
6135 Perl_re_exec_indentf( aTHX_ "%sTRIE failed...%s\n",
6143 /* Find next-highest word to process. Note that this code
6144 * is O(N^2) per trie run (O(N) per branch), so keep tight */
6147 U16 const nextword = ST.nextword;
6148 reg_trie_wordinfo * const wordinfo
6149 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
6150 for (word=ST.topword; word; word=wordinfo[word].prev) {
6151 if (word > nextword && (!min || word < min))
6164 ST.lastparen = rex->lastparen;
6165 ST.lastcloseparen = rex->lastcloseparen;
6169 /* find start char of end of current word */
6171 U32 chars; /* how many chars to skip */
6172 reg_trie_data * const trie
6173 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
6175 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
6177 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
6182 /* the hard option - fold each char in turn and find
6183 * its folded length (which may be different */
6184 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
6192 uvc = utf8n_to_uvchr((U8*)uc, UTF8_MAXLEN, &len,
6200 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
6205 uvc = utf8n_to_uvchr(uscan, foldlen, &len,
6221 scan = ST.me + ((ST.jump && ST.jump[ST.nextword])
6222 ? ST.jump[ST.nextword]
6226 Perl_re_exec_indentf( aTHX_ "%sTRIE matched word #%d, continuing%s\n",
6234 if ( ST.accepted > 1 || has_cutgroup || ST.jump ) {
6235 PUSH_STATE_GOTO(TRIE_next, scan, (char*)uc);
6236 NOT_REACHED; /* NOTREACHED */
6238 /* only one choice left - just continue */
6240 AV *const trie_words
6241 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
6242 SV ** const tmp = trie_words
6243 ? av_fetch(trie_words, ST.nextword - 1, 0) : NULL;
6244 SV *sv= tmp ? sv_newmortal() : NULL;
6246 Perl_re_exec_indentf( aTHX_ "%sTRIE: only one match left, short-circuiting: #%d <%s>%s\n",
6247 depth, PL_colors[4],
6249 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
6250 PL_colors[0], PL_colors[1],
6251 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)|PERL_PV_ESCAPE_NONASCII
6253 : "not compiled under -Dr",
6257 locinput = (char*)uc;
6258 continue; /* execute rest of RE */
6263 case EXACTL: /* /abc/l */
6264 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
6266 /* Complete checking would involve going through every character
6267 * matched by the string to see if any is above latin1. But the
6268 * comparision otherwise might very well be a fast assembly
6269 * language routine, and I (khw) don't think slowing things down
6270 * just to check for this warning is worth it. So this just checks
6271 * the first character */
6272 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*locinput)) {
6273 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(locinput, reginfo->strend);
6276 case EXACT: { /* /abc/ */
6277 char *s = STRING(scan);
6279 if (utf8_target != is_utf8_pat) {
6280 /* The target and the pattern have differing utf8ness. */
6282 const char * const e = s + ln;
6285 /* The target is utf8, the pattern is not utf8.
6286 * Above-Latin1 code points can't match the pattern;
6287 * invariants match exactly, and the other Latin1 ones need
6288 * to be downgraded to a single byte in order to do the
6289 * comparison. (If we could be confident that the target
6290 * is not malformed, this could be refactored to have fewer
6291 * tests by just assuming that if the first bytes match, it
6292 * is an invariant, but there are tests in the test suite
6293 * dealing with (??{...}) which violate this) */
6295 if (l >= reginfo->strend
6296 || UTF8_IS_ABOVE_LATIN1(* (U8*) l))
6300 if (UTF8_IS_INVARIANT(*(U8*)l)) {
6307 if (EIGHT_BIT_UTF8_TO_NATIVE(*l, *(l+1)) != * (U8*) s)
6317 /* The target is not utf8, the pattern is utf8. */
6319 if (l >= reginfo->strend
6320 || UTF8_IS_ABOVE_LATIN1(* (U8*) s))
6324 if (UTF8_IS_INVARIANT(*(U8*)s)) {
6331 if (EIGHT_BIT_UTF8_TO_NATIVE(*s, *(s+1)) != * (U8*) l)
6343 /* The target and the pattern have the same utf8ness. */
6344 /* Inline the first character, for speed. */
6345 if (reginfo->strend - locinput < ln
6346 || UCHARAT(s) != nextchr
6347 || (ln > 1 && memNE(s, locinput, ln)))
6356 case EXACTFL: { /* /abc/il */
6358 const U8 * fold_array;
6360 U32 fold_utf8_flags;
6362 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
6363 folder = foldEQ_locale;
6364 fold_array = PL_fold_locale;
6365 fold_utf8_flags = FOLDEQ_LOCALE;
6368 case EXACTFLU8: /* /abc/il; but all 'abc' are above 255, so
6369 is effectively /u; hence to match, target
6371 if (! utf8_target) {
6374 fold_utf8_flags = FOLDEQ_LOCALE | FOLDEQ_S1_ALREADY_FOLDED
6375 | FOLDEQ_S1_FOLDS_SANE;
6376 folder = foldEQ_latin1;
6377 fold_array = PL_fold_latin1;
6380 case EXACTFU_SS: /* /\x{df}/iu */
6381 case EXACTFU: /* /abc/iu */
6382 folder = foldEQ_latin1;
6383 fold_array = PL_fold_latin1;
6384 fold_utf8_flags = is_utf8_pat ? FOLDEQ_S1_ALREADY_FOLDED : 0;
6387 case EXACTFAA_NO_TRIE: /* This node only generated for non-utf8
6389 assert(! is_utf8_pat);
6391 case EXACTFAA: /* /abc/iaa */
6392 folder = foldEQ_latin1;
6393 fold_array = PL_fold_latin1;
6394 fold_utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
6397 case EXACTF: /* /abc/i This node only generated for
6398 non-utf8 patterns */
6399 assert(! is_utf8_pat);
6401 fold_array = PL_fold;
6402 fold_utf8_flags = 0;
6410 || state_num == EXACTFU_SS
6411 || (state_num == EXACTFL && IN_UTF8_CTYPE_LOCALE))
6413 /* Either target or the pattern are utf8, or has the issue where
6414 * the fold lengths may differ. */
6415 const char * const l = locinput;
6416 char *e = reginfo->strend;
6418 if (! foldEQ_utf8_flags(s, 0, ln, is_utf8_pat,
6419 l, &e, 0, utf8_target, fold_utf8_flags))
6427 /* Neither the target nor the pattern are utf8 */
6428 if (UCHARAT(s) != nextchr
6430 && UCHARAT(s) != fold_array[nextchr])
6434 if (reginfo->strend - locinput < ln)
6436 if (ln > 1 && ! folder(s, locinput, ln))
6442 case NBOUNDL: /* /\B/l */
6446 case BOUNDL: /* /\b/l */
6449 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
6451 if (FLAGS(scan) != TRADITIONAL_BOUND) {
6452 if (! IN_UTF8_CTYPE_LOCALE) {
6453 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
6454 B_ON_NON_UTF8_LOCALE_IS_WRONG);
6460 if (locinput == reginfo->strbeg)
6461 b1 = isWORDCHAR_LC('\n');
6463 b1 = isWORDCHAR_LC_utf8_safe(reghop3((U8*)locinput, -1,
6464 (U8*)(reginfo->strbeg)),
6465 (U8*)(reginfo->strend));
6467 b2 = (NEXTCHR_IS_EOS)
6468 ? isWORDCHAR_LC('\n')
6469 : isWORDCHAR_LC_utf8_safe((U8*) locinput,
6470 (U8*) reginfo->strend);
6472 else { /* Here the string isn't utf8 */
6473 b1 = (locinput == reginfo->strbeg)
6474 ? isWORDCHAR_LC('\n')
6475 : isWORDCHAR_LC(UCHARAT(locinput - 1));
6476 b2 = (NEXTCHR_IS_EOS)
6477 ? isWORDCHAR_LC('\n')
6478 : isWORDCHAR_LC(nextchr);
6480 if (to_complement ^ (b1 == b2)) {
6486 case NBOUND: /* /\B/ */
6490 case BOUND: /* /\b/ */
6494 goto bound_ascii_match_only;
6496 case NBOUNDA: /* /\B/a */
6500 case BOUNDA: /* /\b/a */
6504 bound_ascii_match_only:
6505 /* Here the string isn't utf8, or is utf8 and only ascii characters
6506 * are to match \w. In the latter case looking at the byte just
6507 * prior to the current one may be just the final byte of a
6508 * multi-byte character. This is ok. There are two cases:
6509 * 1) it is a single byte character, and then the test is doing
6510 * just what it's supposed to.
6511 * 2) it is a multi-byte character, in which case the final byte is
6512 * never mistakable for ASCII, and so the test will say it is
6513 * not a word character, which is the correct answer. */
6514 b1 = (locinput == reginfo->strbeg)
6515 ? isWORDCHAR_A('\n')
6516 : isWORDCHAR_A(UCHARAT(locinput - 1));
6517 b2 = (NEXTCHR_IS_EOS)
6518 ? isWORDCHAR_A('\n')
6519 : isWORDCHAR_A(nextchr);
6520 if (to_complement ^ (b1 == b2)) {
6526 case NBOUNDU: /* /\B/u */
6530 case BOUNDU: /* /\b/u */
6533 if (UNLIKELY(reginfo->strbeg >= reginfo->strend)) {
6536 else if (utf8_target) {
6538 switch((bound_type) FLAGS(scan)) {
6539 case TRADITIONAL_BOUND:
6542 b1 = (locinput == reginfo->strbeg)
6543 ? 0 /* isWORDCHAR_L1('\n') */
6544 : isWORDCHAR_utf8_safe(
6545 reghop3((U8*)locinput,
6547 (U8*)(reginfo->strbeg)),
6548 (U8*) reginfo->strend);
6549 b2 = (NEXTCHR_IS_EOS)
6550 ? 0 /* isWORDCHAR_L1('\n') */
6551 : isWORDCHAR_utf8_safe((U8*)locinput,
6552 (U8*) reginfo->strend);
6553 match = cBOOL(b1 != b2);
6557 if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) {
6558 match = TRUE; /* GCB always matches at begin and
6562 /* Find the gcb values of previous and current
6563 * chars, then see if is a break point */
6564 match = isGCB(getGCB_VAL_UTF8(
6565 reghop3((U8*)locinput,
6567 (U8*)(reginfo->strbeg)),
6568 (U8*) reginfo->strend),
6569 getGCB_VAL_UTF8((U8*) locinput,
6570 (U8*) reginfo->strend),
6571 (U8*) reginfo->strbeg,
6578 if (locinput == reginfo->strbeg) {
6581 else if (NEXTCHR_IS_EOS) {
6585 match = isLB(getLB_VAL_UTF8(
6586 reghop3((U8*)locinput,
6588 (U8*)(reginfo->strbeg)),
6589 (U8*) reginfo->strend),
6590 getLB_VAL_UTF8((U8*) locinput,
6591 (U8*) reginfo->strend),
6592 (U8*) reginfo->strbeg,
6594 (U8*) reginfo->strend,
6599 case SB_BOUND: /* Always matches at begin and end */
6600 if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) {
6604 match = isSB(getSB_VAL_UTF8(
6605 reghop3((U8*)locinput,
6607 (U8*)(reginfo->strbeg)),
6608 (U8*) reginfo->strend),
6609 getSB_VAL_UTF8((U8*) locinput,
6610 (U8*) reginfo->strend),
6611 (U8*) reginfo->strbeg,
6613 (U8*) reginfo->strend,
6619 if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) {
6623 match = isWB(WB_UNKNOWN,
6625 reghop3((U8*)locinput,
6627 (U8*)(reginfo->strbeg)),
6628 (U8*) reginfo->strend),
6629 getWB_VAL_UTF8((U8*) locinput,
6630 (U8*) reginfo->strend),
6631 (U8*) reginfo->strbeg,
6633 (U8*) reginfo->strend,
6639 else { /* Not utf8 target */
6640 switch((bound_type) FLAGS(scan)) {
6641 case TRADITIONAL_BOUND:
6644 b1 = (locinput == reginfo->strbeg)
6645 ? 0 /* isWORDCHAR_L1('\n') */
6646 : isWORDCHAR_L1(UCHARAT(locinput - 1));
6647 b2 = (NEXTCHR_IS_EOS)
6648 ? 0 /* isWORDCHAR_L1('\n') */
6649 : isWORDCHAR_L1(nextchr);
6650 match = cBOOL(b1 != b2);
6655 if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) {
6656 match = TRUE; /* GCB always matches at begin and
6659 else { /* Only CR-LF combo isn't a GCB in 0-255
6661 match = UCHARAT(locinput - 1) != '\r'
6662 || UCHARAT(locinput) != '\n';
6667 if (locinput == reginfo->strbeg) {
6670 else if (NEXTCHR_IS_EOS) {
6674 match = isLB(getLB_VAL_CP(UCHARAT(locinput -1)),
6675 getLB_VAL_CP(UCHARAT(locinput)),
6676 (U8*) reginfo->strbeg,
6678 (U8*) reginfo->strend,
6683 case SB_BOUND: /* Always matches at begin and end */
6684 if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) {
6688 match = isSB(getSB_VAL_CP(UCHARAT(locinput -1)),
6689 getSB_VAL_CP(UCHARAT(locinput)),
6690 (U8*) reginfo->strbeg,
6692 (U8*) reginfo->strend,
6698 if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) {
6702 match = isWB(WB_UNKNOWN,
6703 getWB_VAL_CP(UCHARAT(locinput -1)),
6704 getWB_VAL_CP(UCHARAT(locinput)),
6705 (U8*) reginfo->strbeg,
6707 (U8*) reginfo->strend,
6714 if (to_complement ^ ! match) {
6720 case ANYOFL: /* /[abc]/l */
6721 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
6723 if (ANYOFL_UTF8_LOCALE_REQD(FLAGS(scan)) && ! IN_UTF8_CTYPE_LOCALE)
6725 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), utf8_locale_required);
6728 case ANYOFD: /* /[abc]/d */
6729 case ANYOF: /* /[abc]/ */
6732 if (utf8_target && ! UTF8_IS_INVARIANT(*locinput)) {
6733 if (!reginclass(rex, scan, (U8*)locinput, (U8*)reginfo->strend,
6736 locinput += UTF8SKIP(locinput);
6739 if (!REGINCLASS(rex, scan, (U8*)locinput, utf8_target))
6746 if (NEXTCHR_IS_EOS || (UCHARAT(locinput) & FLAGS(scan)) != ARG(scan)) {
6753 if (NEXTCHR_IS_EOS || (UCHARAT(locinput) & FLAGS(scan)) == ARG(scan)) {
6756 goto increment_locinput;
6760 if (NEXTCHR_IS_EOS || ! isASCII(UCHARAT(locinput))) {
6764 locinput++; /* ASCII is always single byte */
6768 if (NEXTCHR_IS_EOS || isASCII(UCHARAT(locinput))) {
6772 goto increment_locinput;
6775 /* The argument (FLAGS) to all the POSIX node types is the class number
6778 case NPOSIXL: /* \W or [:^punct:] etc. under /l */
6782 case POSIXL: /* \w or [:punct:] etc. under /l */
6783 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
6787 /* Use isFOO_lc() for characters within Latin1. (Note that
6788 * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else
6789 * wouldn't be invariant) */
6790 if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) {
6791 if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan), (U8) nextchr)))) {
6799 if (! UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(locinput, reginfo->strend)) {
6800 /* An above Latin-1 code point, or malformed */
6801 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(locinput,
6803 goto utf8_posix_above_latin1;
6806 /* Here is a UTF-8 variant code point below 256 and the target is
6808 if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan),
6809 EIGHT_BIT_UTF8_TO_NATIVE(nextchr,
6810 *(locinput + 1))))))
6815 goto increment_locinput;
6817 case NPOSIXD: /* \W or [:^punct:] etc. under /d */
6821 case POSIXD: /* \w or [:punct:] etc. under /d */
6827 case NPOSIXA: /* \W or [:^punct:] etc. under /a */
6829 if (NEXTCHR_IS_EOS) {
6833 /* All UTF-8 variants match */
6834 if (! UTF8_IS_INVARIANT(nextchr)) {
6835 goto increment_locinput;
6841 case POSIXA: /* \w or [:punct:] etc. under /a */
6844 /* We get here through POSIXD, NPOSIXD, and NPOSIXA when not in
6845 * UTF-8, and also from NPOSIXA even in UTF-8 when the current
6846 * character is a single byte */
6848 if (NEXTCHR_IS_EOS) {
6854 if (! (to_complement ^ cBOOL(_generic_isCC_A(nextchr,
6860 /* Here we are either not in utf8, or we matched a utf8-invariant,
6861 * so the next char is the next byte */
6865 case NPOSIXU: /* \W or [:^punct:] etc. under /u */
6869 case POSIXU: /* \w or [:punct:] etc. under /u */
6871 if (NEXTCHR_IS_EOS) {
6875 /* Use _generic_isCC() for characters within Latin1. (Note that
6876 * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else
6877 * wouldn't be invariant) */
6878 if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) {
6879 if (! (to_complement ^ cBOOL(_generic_isCC(nextchr,
6886 else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(locinput, reginfo->strend)) {
6887 if (! (to_complement
6888 ^ cBOOL(_generic_isCC(EIGHT_BIT_UTF8_TO_NATIVE(nextchr,
6896 else { /* Handle above Latin-1 code points */
6897 utf8_posix_above_latin1:
6898 classnum = (_char_class_number) FLAGS(scan);
6901 if (! (to_complement
6902 ^ cBOOL(_invlist_contains_cp(
6903 PL_XPosix_ptrs[classnum],
6904 utf8_to_uvchr_buf((U8 *) locinput,
6905 (U8 *) reginfo->strend,
6911 case _CC_ENUM_SPACE:
6912 if (! (to_complement
6913 ^ cBOOL(is_XPERLSPACE_high(locinput))))
6918 case _CC_ENUM_BLANK:
6919 if (! (to_complement
6920 ^ cBOOL(is_HORIZWS_high(locinput))))
6925 case _CC_ENUM_XDIGIT:
6926 if (! (to_complement
6927 ^ cBOOL(is_XDIGIT_high(locinput))))
6932 case _CC_ENUM_VERTSPACE:
6933 if (! (to_complement
6934 ^ cBOOL(is_VERTWS_high(locinput))))
6939 case _CC_ENUM_CNTRL: /* These can't match above Latin1 */
6940 case _CC_ENUM_ASCII:
6941 if (! to_complement) {
6946 locinput += UTF8SKIP(locinput);
6950 case CLUMP: /* Match \X: logical Unicode character. This is defined as
6951 a Unicode extended Grapheme Cluster */
6954 if (! utf8_target) {
6956 /* Match either CR LF or '.', as all the other possibilities
6958 locinput++; /* Match the . or CR */
6959 if (nextchr == '\r' /* And if it was CR, and the next is LF,
6961 && locinput < reginfo->strend
6962 && UCHARAT(locinput) == '\n')
6969 /* Get the gcb type for the current character */
6970 GCB_enum prev_gcb = getGCB_VAL_UTF8((U8*) locinput,
6971 (U8*) reginfo->strend);
6973 /* Then scan through the input until we get to the first
6974 * character whose type is supposed to be a gcb with the
6975 * current character. (There is always a break at the
6977 locinput += UTF8SKIP(locinput);
6978 while (locinput < reginfo->strend) {
6979 GCB_enum cur_gcb = getGCB_VAL_UTF8((U8*) locinput,
6980 (U8*) reginfo->strend);
6981 if (isGCB(prev_gcb, cur_gcb,
6982 (U8*) reginfo->strbeg, (U8*) locinput,
6989 locinput += UTF8SKIP(locinput);
6996 case NREFFL: /* /\g{name}/il */
6997 { /* The capture buffer cases. The ones beginning with N for the
6998 named buffers just convert to the equivalent numbered and
6999 pretend they were called as the corresponding numbered buffer
7001 /* don't initialize these in the declaration, it makes C++
7006 const U8 *fold_array;
7009 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
7010 folder = foldEQ_locale;
7011 fold_array = PL_fold_locale;
7013 utf8_fold_flags = FOLDEQ_LOCALE;
7016 case NREFFA: /* /\g{name}/iaa */
7017 folder = foldEQ_latin1;
7018 fold_array = PL_fold_latin1;
7020 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
7023 case NREFFU: /* /\g{name}/iu */
7024 folder = foldEQ_latin1;
7025 fold_array = PL_fold_latin1;
7027 utf8_fold_flags = 0;
7030 case NREFF: /* /\g{name}/i */
7032 fold_array = PL_fold;
7034 utf8_fold_flags = 0;
7037 case NREF: /* /\g{name}/ */
7041 utf8_fold_flags = 0;
7044 /* For the named back references, find the corresponding buffer
7046 n = reg_check_named_buff_matched(rex,scan);
7051 goto do_nref_ref_common;
7053 case REFFL: /* /\1/il */
7054 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
7055 folder = foldEQ_locale;
7056 fold_array = PL_fold_locale;
7057 utf8_fold_flags = FOLDEQ_LOCALE;
7060 case REFFA: /* /\1/iaa */
7061 folder = foldEQ_latin1;
7062 fold_array = PL_fold_latin1;
7063 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
7066 case REFFU: /* /\1/iu */
7067 folder = foldEQ_latin1;
7068 fold_array = PL_fold_latin1;
7069 utf8_fold_flags = 0;
7072 case REFF: /* /\1/i */
7074 fold_array = PL_fold;
7075 utf8_fold_flags = 0;
7078 case REF: /* /\1/ */
7081 utf8_fold_flags = 0;
7085 n = ARG(scan); /* which paren pair */
7088 ln = rex->offs[n].start;
7089 endref = rex->offs[n].end;
7090 reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */
7091 if (rex->lastparen < n || ln == -1 || endref == -1)
7092 sayNO; /* Do not match unless seen CLOSEn. */
7096 s = reginfo->strbeg + ln;
7097 if (type != REF /* REF can do byte comparison */
7098 && (utf8_target || type == REFFU || type == REFFL))
7100 char * limit = reginfo->strend;
7102 /* This call case insensitively compares the entire buffer
7103 * at s, with the current input starting at locinput, but
7104 * not going off the end given by reginfo->strend, and
7105 * returns in <limit> upon success, how much of the
7106 * current input was matched */
7107 if (! foldEQ_utf8_flags(s, NULL, endref - ln, utf8_target,
7108 locinput, &limit, 0, utf8_target, utf8_fold_flags))
7116 /* Not utf8: Inline the first character, for speed. */
7117 if (!NEXTCHR_IS_EOS &&
7118 UCHARAT(s) != nextchr &&
7120 UCHARAT(s) != fold_array[nextchr]))
7123 if (locinput + ln > reginfo->strend)
7125 if (ln > 1 && (type == REF
7126 ? memNE(s, locinput, ln)
7127 : ! folder(s, locinput, ln)))
7133 case NOTHING: /* null op; e.g. the 'nothing' following
7134 * the '*' in m{(a+|b)*}' */
7136 case TAIL: /* placeholder while compiling (A|B|C) */
7140 #define ST st->u.eval
7141 #define CUR_EVAL cur_eval->u.eval
7147 regexp_internal *rei;
7148 regnode *startpoint;
7151 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
7152 arg= (U32)ARG(scan);
7153 if (cur_eval && cur_eval->locinput == locinput) {
7154 if ( ++nochange_depth > max_nochange_depth )
7156 "Pattern subroutine nesting without pos change"
7157 " exceeded limit in regex");
7164 startpoint = scan + ARG2L(scan);
7165 EVAL_CLOSE_PAREN_SET( st, arg );
7166 /* Detect infinite recursion
7168 * A pattern like /(?R)foo/ or /(?<x>(?&y)foo)(?<y>(?&x)bar)/
7169 * or "a"=~/(.(?2))((?<=(?=(?1)).))/ could recurse forever.
7170 * So we track the position in the string we are at each time
7171 * we recurse and if we try to enter the same routine twice from
7172 * the same position we throw an error.
7174 if ( rex->recurse_locinput[arg] == locinput ) {
7175 /* FIXME: we should show the regop that is failing as part
7176 * of the error message. */
7177 Perl_croak(aTHX_ "Infinite recursion in regex");
7179 ST.prev_recurse_locinput= rex->recurse_locinput[arg];
7180 rex->recurse_locinput[arg]= locinput;
7183 GET_RE_DEBUG_FLAGS_DECL;
7185 Perl_re_exec_indentf( aTHX_
7186 "entering GOSUB, prev_recurse_locinput=%p recurse_locinput[%d]=%p\n",
7187 depth, ST.prev_recurse_locinput, arg, rex->recurse_locinput[arg]
7193 /* Save all the positions seen so far. */
7194 ST.cp = regcppush(rex, 0, maxopenparen);
7195 REGCP_SET(ST.lastcp);
7197 /* and then jump to the code we share with EVAL */
7198 goto eval_recurse_doit;
7201 case EVAL: /* /(?{...})B/ /(??{A})B/ and /(?(?{...})X|Y)B/ */
7202 if (cur_eval && cur_eval->locinput==locinput) {
7203 if ( ++nochange_depth > max_nochange_depth )
7204 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
7209 /* execute the code in the {...} */
7213 OP * const oop = PL_op;
7214 COP * const ocurcop = PL_curcop;
7218 /* save *all* paren positions */
7219 regcppush(rex, 0, maxopenparen);
7220 REGCP_SET(ST.lastcp);
7223 caller_cv = find_runcv(NULL);
7227 if (rexi->data->what[n] == 'r') { /* code from an external qr */
7229 (REGEXP*)(rexi->data->data[n])
7231 nop = (OP*)rexi->data->data[n+1];
7233 else if (rexi->data->what[n] == 'l') { /* literal code */
7235 nop = (OP*)rexi->data->data[n];
7236 assert(CvDEPTH(newcv));
7239 /* literal with own CV */
7240 assert(rexi->data->what[n] == 'L');
7241 newcv = rex->qr_anoncv;
7242 nop = (OP*)rexi->data->data[n];
7245 /* Some notes about MULTICALL and the context and save stacks.
7248 * /...(?{ my $x)}...(?{ my $y)}...(?{ my $z)}.../
7249 * since codeblocks don't introduce a new scope (so that
7250 * local() etc accumulate), at the end of a successful
7251 * match there will be a SAVEt_CLEARSV on the savestack
7252 * for each of $x, $y, $z. If the three code blocks above
7253 * happen to have come from different CVs (e.g. via
7254 * embedded qr//s), then we must ensure that during any
7255 * savestack unwinding, PL_comppad always points to the
7256 * right pad at each moment. We achieve this by
7257 * interleaving SAVEt_COMPPAD's on the savestack whenever
7258 * there is a change of pad.
7259 * In theory whenever we call a code block, we should
7260 * push a CXt_SUB context, then pop it on return from
7261 * that code block. This causes a bit of an issue in that
7262 * normally popping a context also clears the savestack
7263 * back to cx->blk_oldsaveix, but here we specifically
7264 * don't want to clear the save stack on exit from the
7266 * Also for efficiency we don't want to keep pushing and
7267 * popping the single SUB context as we backtrack etc.
7268 * So instead, we push a single context the first time
7269 * we need, it, then hang onto it until the end of this
7270 * function. Whenever we encounter a new code block, we
7271 * update the CV etc if that's changed. During the times
7272 * in this function where we're not executing a code
7273 * block, having the SUB context still there is a bit
7274 * naughty - but we hope that no-one notices.
7275 * When the SUB context is initially pushed, we fake up
7276 * cx->blk_oldsaveix to be as if we'd pushed this context
7277 * on first entry to S_regmatch rather than at some random
7278 * point during the regexe execution. That way if we
7279 * croak, popping the context stack will ensure that
7280 * *everything* SAVEd by this function is undone and then
7281 * the context popped, rather than e.g., popping the
7282 * context (and restoring the original PL_comppad) then
7283 * popping more of the savestack and restoring a bad
7287 /* If this is the first EVAL, push a MULTICALL. On
7288 * subsequent calls, if we're executing a different CV, or
7289 * if PL_comppad has got messed up from backtracking
7290 * through SAVECOMPPADs, then refresh the context.
7292 if (newcv != last_pushed_cv || PL_comppad != last_pad)
7294 U8 flags = (CXp_SUB_RE |
7295 ((newcv == caller_cv) ? CXp_SUB_RE_FAKE : 0));
7297 if (last_pushed_cv) {
7298 CHANGE_MULTICALL_FLAGS(newcv, flags);
7301 PUSH_MULTICALL_FLAGS(newcv, flags);
7303 /* see notes above */
7304 CX_CUR()->blk_oldsaveix = orig_savestack_ix;
7306 last_pushed_cv = newcv;
7309 /* these assignments are just to silence compiler
7311 multicall_cop = NULL;
7313 last_pad = PL_comppad;
7315 /* the initial nextstate you would normally execute
7316 * at the start of an eval (which would cause error
7317 * messages to come from the eval), may be optimised
7318 * away from the execution path in the regex code blocks;
7319 * so manually set PL_curcop to it initially */
7321 OP *o = cUNOPx(nop)->op_first;
7322 assert(o->op_type == OP_NULL);
7323 if (o->op_targ == OP_SCOPE) {
7324 o = cUNOPo->op_first;
7327 assert(o->op_targ == OP_LEAVE);
7328 o = cUNOPo->op_first;
7329 assert(o->op_type == OP_ENTER);
7333 if (o->op_type != OP_STUB) {
7334 assert( o->op_type == OP_NEXTSTATE
7335 || o->op_type == OP_DBSTATE
7336 || (o->op_type == OP_NULL
7337 && ( o->op_targ == OP_NEXTSTATE
7338 || o->op_targ == OP_DBSTATE
7342 PL_curcop = (COP*)o;
7347 DEBUG_STATE_r( Perl_re_printf( aTHX_
7348 " re EVAL PL_op=0x%" UVxf "\n", PTR2UV(nop)) );
7350 rex->offs[0].end = locinput - reginfo->strbeg;
7351 if (reginfo->info_aux_eval->pos_magic)
7352 MgBYTEPOS_set(reginfo->info_aux_eval->pos_magic,
7353 reginfo->sv, reginfo->strbeg,
7354 locinput - reginfo->strbeg);
7357 SV *sv_mrk = get_sv("REGMARK", 1);
7358 sv_setsv(sv_mrk, sv_yes_mark);
7361 /* we don't use MULTICALL here as we want to call the
7362 * first op of the block of interest, rather than the
7363 * first op of the sub. Also, we don't want to free
7364 * the savestack frame */
7365 before = (IV)(SP-PL_stack_base);
7367 CALLRUNOPS(aTHX); /* Scalar context. */
7369 if ((IV)(SP-PL_stack_base) == before)
7370 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
7376 /* before restoring everything, evaluate the returned
7377 * value, so that 'uninit' warnings don't use the wrong
7378 * PL_op or pad. Also need to process any magic vars
7379 * (e.g. $1) *before* parentheses are restored */
7384 if (logical == 0) /* (?{})/ */
7385 sv_setsv(save_scalar(PL_replgv), ret); /* $^R */
7386 else if (logical == 1) { /* /(?(?{...})X|Y)/ */
7387 sw = cBOOL(SvTRUE_NN(ret));
7390 else { /* /(??{}) */
7391 /* if its overloaded, let the regex compiler handle
7392 * it; otherwise extract regex, or stringify */
7393 if (SvGMAGICAL(ret))
7394 ret = sv_mortalcopy(ret);
7395 if (!SvAMAGIC(ret)) {
7399 if (SvTYPE(sv) == SVt_REGEXP)
7400 re_sv = (REGEXP*) sv;
7401 else if (SvSMAGICAL(ret)) {
7402 MAGIC *mg = mg_find(ret, PERL_MAGIC_qr);
7404 re_sv = (REGEXP *) mg->mg_obj;
7407 /* force any undef warnings here */
7408 if (!re_sv && !SvPOK(ret) && !SvNIOK(ret)) {
7409 ret = sv_mortalcopy(ret);
7410 (void) SvPV_force_nolen(ret);
7416 /* *** Note that at this point we don't restore
7417 * PL_comppad, (or pop the CxSUB) on the assumption it may
7418 * be used again soon. This is safe as long as nothing
7419 * in the regexp code uses the pad ! */
7421 PL_curcop = ocurcop;
7422 regcp_restore(rex, ST.lastcp, &maxopenparen);
7423 PL_curpm_under = PL_curpm;
7424 PL_curpm = PL_reg_curpm;
7427 PUSH_STATE_GOTO(EVAL_B, next, locinput);
7432 /* only /(??{})/ from now on */
7435 /* extract RE object from returned value; compiling if
7439 re_sv = reg_temp_copy(NULL, re_sv);
7444 if (SvUTF8(ret) && IN_BYTES) {
7445 /* In use 'bytes': make a copy of the octet
7446 * sequence, but without the flag on */
7448 const char *const p = SvPV(ret, len);
7449 ret = newSVpvn_flags(p, len, SVs_TEMP);
7451 if (rex->intflags & PREGf_USE_RE_EVAL)
7452 pm_flags |= PMf_USE_RE_EVAL;
7454 /* if we got here, it should be an engine which
7455 * supports compiling code blocks and stuff */
7456 assert(rex->engine && rex->engine->op_comp);
7457 assert(!(scan->flags & ~RXf_PMf_COMPILETIME));
7458 re_sv = rex->engine->op_comp(aTHX_ &ret, 1, NULL,
7459 rex->engine, NULL, NULL,
7460 /* copy /msixn etc to inner pattern */
7465 & (SVs_TEMP | SVs_GMG | SVf_ROK))
7466 && (!SvPADTMP(ret) || SvREADONLY(ret))) {
7467 /* This isn't a first class regexp. Instead, it's
7468 caching a regexp onto an existing, Perl visible
7470 sv_magic(ret, MUTABLE_SV(re_sv), PERL_MAGIC_qr, 0, 0);
7476 RXp_MATCH_COPIED_off(re);
7477 re->subbeg = rex->subbeg;
7478 re->sublen = rex->sublen;
7479 re->suboffset = rex->suboffset;
7480 re->subcoffset = rex->subcoffset;
7482 re->lastcloseparen = 0;
7485 debug_start_match(re_sv, utf8_target, locinput,
7486 reginfo->strend, "EVAL/GOSUB: Matching embedded");
7488 startpoint = rei->program + 1;
7489 EVAL_CLOSE_PAREN_CLEAR(st); /* ST.close_paren = 0;
7490 * close_paren only for GOSUB */
7491 ST.prev_recurse_locinput= NULL; /* only used for GOSUB */
7492 /* Save all the seen positions so far. */
7493 ST.cp = regcppush(rex, 0, maxopenparen);
7494 REGCP_SET(ST.lastcp);
7495 /* and set maxopenparen to 0, since we are starting a "fresh" match */
7497 /* run the pattern returned from (??{...}) */
7499 eval_recurse_doit: /* Share code with GOSUB below this line
7500 * At this point we expect the stack context to be
7501 * set up correctly */
7503 /* invalidate the S-L poscache. We're now executing a
7504 * different set of WHILEM ops (and their associated
7505 * indexes) against the same string, so the bits in the
7506 * cache are meaningless. Setting maxiter to zero forces
7507 * the cache to be invalidated and zeroed before reuse.
7508 * XXX This is too dramatic a measure. Ideally we should
7509 * save the old cache and restore when running the outer
7511 reginfo->poscache_maxiter = 0;
7513 /* the new regexp might have a different is_utf8_pat than we do */
7514 is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(re_sv));
7516 ST.prev_rex = rex_sv;
7517 ST.prev_curlyx = cur_curlyx;
7519 SET_reg_curpm(rex_sv);
7524 ST.prev_eval = cur_eval;
7526 /* now continue from first node in postoned RE */
7527 PUSH_YES_STATE_GOTO(EVAL_postponed_AB, startpoint, locinput);
7528 NOT_REACHED; /* NOTREACHED */
7531 case EVAL_postponed_AB: /* cleanup after a successful (??{A})B */
7532 /* note: this is called twice; first after popping B, then A */
7534 Perl_re_exec_indentf( aTHX_ "EVAL_AB cur_eval=%p prev_eval=%p\n",
7535 depth, cur_eval, ST.prev_eval);
7538 #define SET_RECURSE_LOCINPUT(STR,VAL)\
7539 if ( cur_eval && CUR_EVAL.close_paren ) {\
7541 Perl_re_exec_indentf( aTHX_ STR " GOSUB%d ce=%p recurse_locinput=%p\n",\
7543 CUR_EVAL.close_paren - 1,\
7547 rex->recurse_locinput[CUR_EVAL.close_paren - 1] = VAL;\
7550 SET_RECURSE_LOCINPUT("EVAL_AB[before]", CUR_EVAL.prev_recurse_locinput);
7552 rex_sv = ST.prev_rex;
7553 is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv));
7554 SET_reg_curpm(rex_sv);
7555 rex = ReANY(rex_sv);
7556 rexi = RXi_GET(rex);
7558 /* preserve $^R across LEAVE's. See Bug 121070. */
7559 SV *save_sv= GvSV(PL_replgv);
7560 SvREFCNT_inc(save_sv);
7561 regcpblow(ST.cp); /* LEAVE in disguise */
7562 sv_setsv(GvSV(PL_replgv), save_sv);
7563 SvREFCNT_dec(save_sv);
7565 cur_eval = ST.prev_eval;
7566 cur_curlyx = ST.prev_curlyx;
7568 /* Invalidate cache. See "invalidate" comment above. */
7569 reginfo->poscache_maxiter = 0;
7570 if ( nochange_depth )
7573 SET_RECURSE_LOCINPUT("EVAL_AB[after]", cur_eval->locinput);
7577 case EVAL_B_fail: /* unsuccessful B in (?{...})B */
7578 REGCP_UNWIND(ST.lastcp);
7581 case EVAL_postponed_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
7582 /* note: this is called twice; first after popping B, then A */
7584 Perl_re_exec_indentf( aTHX_ "EVAL_AB_fail cur_eval=%p prev_eval=%p\n",
7585 depth, cur_eval, ST.prev_eval);
7588 SET_RECURSE_LOCINPUT("EVAL_AB_fail[before]", CUR_EVAL.prev_recurse_locinput);
7590 rex_sv = ST.prev_rex;
7591 is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv));
7592 SET_reg_curpm(rex_sv);
7593 rex = ReANY(rex_sv);
7594 rexi = RXi_GET(rex);
7596 REGCP_UNWIND(ST.lastcp);
7597 regcppop(rex, &maxopenparen);
7598 cur_eval = ST.prev_eval;
7599 cur_curlyx = ST.prev_curlyx;
7601 /* Invalidate cache. See "invalidate" comment above. */
7602 reginfo->poscache_maxiter = 0;
7603 if ( nochange_depth )
7606 SET_RECURSE_LOCINPUT("EVAL_AB_fail[after]", cur_eval->locinput);
7611 n = ARG(scan); /* which paren pair */
7612 rex->offs[n].start_tmp = locinput - reginfo->strbeg;
7613 if (n > maxopenparen)
7615 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
7616 "OPEN: rex=0x%" UVxf " offs=0x%" UVxf ": \\%" UVuf ": set %" IVdf " tmp; maxopenparen=%" UVuf "\n",
7621 (IV)rex->offs[n].start_tmp,
7627 case SROPEN: /* (*SCRIPT_RUN: */
7628 script_run_begin = (U8 *) locinput;
7633 n = ARG(scan); /* which paren pair */
7634 CLOSE_CAPTURE(n, rex->offs[n].start_tmp,
7635 locinput - reginfo->strbeg);
7636 if ( EVAL_CLOSE_PAREN_IS( cur_eval, n ) )
7641 case SRCLOSE: /* (*SCRIPT_RUN: ... ) */
7643 if (! isSCRIPT_RUN(script_run_begin, (U8 *) locinput, utf8_target))
7651 case ACCEPT: /* (*ACCEPT) */
7653 sv_yes_mark = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
7657 cursor && OP(cursor)!=END;
7658 cursor=regnext(cursor))
7660 if ( OP(cursor)==CLOSE ){
7662 if ( n <= lastopen ) {
7663 CLOSE_CAPTURE(n, rex->offs[n].start_tmp,
7664 locinput - reginfo->strbeg);
7665 if ( n == ARG(scan) || EVAL_CLOSE_PAREN_IS(cur_eval, n) )
7674 case GROUPP: /* (?(1)) */
7675 n = ARG(scan); /* which paren pair */
7676 sw = cBOOL(rex->lastparen >= n && rex->offs[n].end != -1);
7679 case NGROUPP: /* (?(<name>)) */
7680 /* reg_check_named_buff_matched returns 0 for no match */
7681 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
7684 case INSUBP: /* (?(R)) */
7686 /* this does not need to use EVAL_CLOSE_PAREN macros, as the arg
7687 * of SCAN is already set up as matches a eval.close_paren */
7688 sw = cur_eval && (n == 0 || CUR_EVAL.close_paren == n);
7691 case DEFINEP: /* (?(DEFINE)) */
7695 case IFTHEN: /* (?(cond)A|B) */
7696 reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */
7698 next = NEXTOPER(NEXTOPER(scan));
7700 next = scan + ARG(scan);
7701 if (OP(next) == IFTHEN) /* Fake one. */
7702 next = NEXTOPER(NEXTOPER(next));
7706 case LOGICAL: /* modifier for EVAL and IFMATCH */
7707 logical = scan->flags;
7710 /*******************************************************************
7712 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
7713 pattern, where A and B are subpatterns. (For simple A, CURLYM or
7714 STAR/PLUS/CURLY/CURLYN are used instead.)
7716 A*B is compiled as <CURLYX><A><WHILEM><B>
7718 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
7719 state, which contains the current count, initialised to -1. It also sets
7720 cur_curlyx to point to this state, with any previous value saved in the
7723 CURLYX then jumps straight to the WHILEM op, rather than executing A,
7724 since the pattern may possibly match zero times (i.e. it's a while {} loop
7725 rather than a do {} while loop).
7727 Each entry to WHILEM represents a successful match of A. The count in the
7728 CURLYX block is incremented, another WHILEM state is pushed, and execution
7729 passes to A or B depending on greediness and the current count.
7731 For example, if matching against the string a1a2a3b (where the aN are
7732 substrings that match /A/), then the match progresses as follows: (the
7733 pushed states are interspersed with the bits of strings matched so far):
7736 <CURLYX cnt=0><WHILEM>
7737 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
7738 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
7739 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
7740 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
7742 (Contrast this with something like CURLYM, which maintains only a single
7746 a1 <CURLYM cnt=1> a2
7747 a1 a2 <CURLYM cnt=2> a3
7748 a1 a2 a3 <CURLYM cnt=3> b
7751 Each WHILEM state block marks a point to backtrack to upon partial failure
7752 of A or B, and also contains some minor state data related to that
7753 iteration. The CURLYX block, pointed to by cur_curlyx, contains the
7754 overall state, such as the count, and pointers to the A and B ops.
7756 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
7757 must always point to the *current* CURLYX block, the rules are:
7759 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
7760 and set cur_curlyx to point the new block.
7762 When popping the CURLYX block after a successful or unsuccessful match,
7763 restore the previous cur_curlyx.
7765 When WHILEM is about to execute B, save the current cur_curlyx, and set it
7766 to the outer one saved in the CURLYX block.
7768 When popping the WHILEM block after a successful or unsuccessful B match,
7769 restore the previous cur_curlyx.
7771 Here's an example for the pattern (AI* BI)*BO
7772 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
7775 curlyx backtrack stack
7776 ------ ---------------
7778 CO <CO prev=NULL> <WO>
7779 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
7780 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
7781 NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
7783 At this point the pattern succeeds, and we work back down the stack to
7784 clean up, restoring as we go:
7786 CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
7787 CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
7788 CO <CO prev=NULL> <WO>
7791 *******************************************************************/
7793 #define ST st->u.curlyx
7795 case CURLYX: /* start of /A*B/ (for complex A) */
7797 /* No need to save/restore up to this paren */
7798 I32 parenfloor = scan->flags;
7800 assert(next); /* keep Coverity happy */
7801 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
7804 /* XXXX Probably it is better to teach regpush to support
7805 parenfloor > maxopenparen ... */
7806 if (parenfloor > (I32)rex->lastparen)
7807 parenfloor = rex->lastparen; /* Pessimization... */
7809 ST.prev_curlyx= cur_curlyx;
7811 ST.cp = PL_savestack_ix;
7813 /* these fields contain the state of the current curly.
7814 * they are accessed by subsequent WHILEMs */
7815 ST.parenfloor = parenfloor;
7820 ST.count = -1; /* this will be updated by WHILEM */
7821 ST.lastloc = NULL; /* this will be updated by WHILEM */
7823 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next), locinput);
7824 NOT_REACHED; /* NOTREACHED */
7827 case CURLYX_end: /* just finished matching all of A*B */
7828 cur_curlyx = ST.prev_curlyx;
7830 NOT_REACHED; /* NOTREACHED */
7832 case CURLYX_end_fail: /* just failed to match all of A*B */
7834 cur_curlyx = ST.prev_curlyx;
7836 NOT_REACHED; /* NOTREACHED */
7840 #define ST st->u.whilem
7842 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
7844 /* see the discussion above about CURLYX/WHILEM */
7849 assert(cur_curlyx); /* keep Coverity happy */
7851 min = ARG1(cur_curlyx->u.curlyx.me);
7852 max = ARG2(cur_curlyx->u.curlyx.me);
7853 A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
7854 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
7855 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
7856 ST.cache_offset = 0;
7860 DEBUG_EXECUTE_r( Perl_re_exec_indentf( aTHX_ "WHILEM: matched %ld out of %d..%d\n",
7861 depth, (long)n, min, max)
7864 /* First just match a string of min A's. */
7867 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, maxopenparen);
7868 cur_curlyx->u.curlyx.lastloc = locinput;
7869 REGCP_SET(ST.lastcp);
7871 PUSH_STATE_GOTO(WHILEM_A_pre, A, locinput);
7872 NOT_REACHED; /* NOTREACHED */
7875 /* If degenerate A matches "", assume A done. */
7877 if (locinput == cur_curlyx->u.curlyx.lastloc) {
7878 DEBUG_EXECUTE_r( Perl_re_exec_indentf( aTHX_ "WHILEM: empty match detected, trying continuation...\n",
7881 goto do_whilem_B_max;
7884 /* super-linear cache processing.
7886 * The idea here is that for certain types of CURLYX/WHILEM -
7887 * principally those whose upper bound is infinity (and
7888 * excluding regexes that have things like \1 and other very
7889 * non-regular expresssiony things), then if a pattern like
7890 * /....A*.../ fails and we backtrack to the WHILEM, then we
7891 * make a note that this particular WHILEM op was at string
7892 * position 47 (say) when the rest of pattern failed. Then, if
7893 * we ever find ourselves back at that WHILEM, and at string
7894 * position 47 again, we can just fail immediately rather than
7895 * running the rest of the pattern again.
7897 * This is very handy when patterns start to go
7898 * 'super-linear', like in (a+)*(a+)*(a+)*, where you end up
7899 * with a combinatorial explosion of backtracking.
7901 * The cache is implemented as a bit array, with one bit per
7902 * string byte position per WHILEM op (up to 16) - so its
7903 * between 0.25 and 2x the string size.
7905 * To avoid allocating a poscache buffer every time, we do an
7906 * initially countdown; only after we have executed a WHILEM
7907 * op (string-length x #WHILEMs) times do we allocate the
7910 * The top 4 bits of scan->flags byte say how many different
7911 * relevant CURLLYX/WHILEM op pairs there are, while the
7912 * bottom 4-bits is the identifying index number of this
7918 if (!reginfo->poscache_maxiter) {
7919 /* start the countdown: Postpone detection until we
7920 * know the match is not *that* much linear. */
7921 reginfo->poscache_maxiter
7922 = (reginfo->strend - reginfo->strbeg + 1)
7924 /* possible overflow for long strings and many CURLYX's */
7925 if (reginfo->poscache_maxiter < 0)
7926 reginfo->poscache_maxiter = I32_MAX;
7927 reginfo->poscache_iter = reginfo->poscache_maxiter;
7930 if (reginfo->poscache_iter-- == 0) {
7931 /* initialise cache */
7932 const SSize_t size = (reginfo->poscache_maxiter + 7)/8;
7933 regmatch_info_aux *const aux = reginfo->info_aux;
7934 if (aux->poscache) {
7935 if ((SSize_t)reginfo->poscache_size < size) {
7936 Renew(aux->poscache, size, char);
7937 reginfo->poscache_size = size;
7939 Zero(aux->poscache, size, char);
7942 reginfo->poscache_size = size;
7943 Newxz(aux->poscache, size, char);
7945 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
7946 "%sWHILEM: Detected a super-linear match, switching on caching%s...\n",
7947 PL_colors[4], PL_colors[5])
7951 if (reginfo->poscache_iter < 0) {
7952 /* have we already failed at this position? */
7953 SSize_t offset, mask;
7955 reginfo->poscache_iter = -1; /* stop eventual underflow */
7956 offset = (scan->flags & 0xf) - 1
7957 + (locinput - reginfo->strbeg)
7959 mask = 1 << (offset % 8);
7961 if (reginfo->info_aux->poscache[offset] & mask) {
7962 DEBUG_EXECUTE_r( Perl_re_exec_indentf( aTHX_ "WHILEM: (cache) already tried at this position...\n",
7965 cur_curlyx->u.curlyx.count--;
7966 sayNO; /* cache records failure */
7968 ST.cache_offset = offset;
7969 ST.cache_mask = mask;
7973 /* Prefer B over A for minimal matching. */
7975 if (cur_curlyx->u.curlyx.minmod) {
7976 ST.save_curlyx = cur_curlyx;
7977 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
7978 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B,
7980 NOT_REACHED; /* NOTREACHED */
7983 /* Prefer A over B for maximal matching. */
7985 if (n < max) { /* More greed allowed? */
7986 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor,
7988 cur_curlyx->u.curlyx.lastloc = locinput;
7989 REGCP_SET(ST.lastcp);
7990 PUSH_STATE_GOTO(WHILEM_A_max, A, locinput);
7991 NOT_REACHED; /* NOTREACHED */
7993 goto do_whilem_B_max;
7995 NOT_REACHED; /* NOTREACHED */
7997 case WHILEM_B_min: /* just matched B in a minimal match */
7998 case WHILEM_B_max: /* just matched B in a maximal match */
7999 cur_curlyx = ST.save_curlyx;
8001 NOT_REACHED; /* NOTREACHED */
8003 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
8004 cur_curlyx = ST.save_curlyx;
8005 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
8006 cur_curlyx->u.curlyx.count--;
8008 NOT_REACHED; /* NOTREACHED */
8010 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
8012 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
8013 REGCP_UNWIND(ST.lastcp);
8014 regcppop(rex, &maxopenparen);
8015 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
8016 cur_curlyx->u.curlyx.count--;
8018 NOT_REACHED; /* NOTREACHED */
8020 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
8021 REGCP_UNWIND(ST.lastcp);
8022 regcppop(rex, &maxopenparen); /* Restore some previous $<digit>s? */
8023 DEBUG_EXECUTE_r(Perl_re_exec_indentf( aTHX_ "WHILEM: failed, trying continuation...\n",
8027 if (cur_curlyx->u.curlyx.count >= REG_INFTY
8028 && ckWARN(WARN_REGEXP)
8029 && !reginfo->warned)
8031 reginfo->warned = TRUE;
8032 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
8033 "Complex regular subexpression recursion limit (%d) "
8039 ST.save_curlyx = cur_curlyx;
8040 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
8041 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B,
8043 NOT_REACHED; /* NOTREACHED */
8045 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
8046 cur_curlyx = ST.save_curlyx;
8048 if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
8049 /* Maximum greed exceeded */
8050 if (cur_curlyx->u.curlyx.count >= REG_INFTY
8051 && ckWARN(WARN_REGEXP)
8052 && !reginfo->warned)
8054 reginfo->warned = TRUE;
8055 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
8056 "Complex regular subexpression recursion "
8057 "limit (%d) exceeded",
8060 cur_curlyx->u.curlyx.count--;
8064 DEBUG_EXECUTE_r(Perl_re_exec_indentf( aTHX_ "WHILEM: B min fail: trying longer...\n", depth)
8066 /* Try grabbing another A and see if it helps. */
8067 cur_curlyx->u.curlyx.lastloc = locinput;
8068 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor,
8070 REGCP_SET(ST.lastcp);
8071 PUSH_STATE_GOTO(WHILEM_A_min,
8072 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS,
8074 NOT_REACHED; /* NOTREACHED */
8077 #define ST st->u.branch
8079 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
8080 next = scan + ARG(scan);
8083 scan = NEXTOPER(scan);
8086 case BRANCH: /* /(...|A|...)/ */
8087 scan = NEXTOPER(scan); /* scan now points to inner node */
8088 ST.lastparen = rex->lastparen;
8089 ST.lastcloseparen = rex->lastcloseparen;
8090 ST.next_branch = next;
8093 /* Now go into the branch */
8095 PUSH_YES_STATE_GOTO(BRANCH_next, scan, locinput);
8097 PUSH_STATE_GOTO(BRANCH_next, scan, locinput);
8099 NOT_REACHED; /* NOTREACHED */
8101 case CUTGROUP: /* /(*THEN)/ */
8102 sv_yes_mark = st->u.mark.mark_name = scan->flags
8103 ? MUTABLE_SV(rexi->data->data[ ARG( scan ) ])
8105 PUSH_STATE_GOTO(CUTGROUP_next, next, locinput);
8106 NOT_REACHED; /* NOTREACHED */
8108 case CUTGROUP_next_fail:
8111 if (st->u.mark.mark_name)
8112 sv_commit = st->u.mark.mark_name;
8114 NOT_REACHED; /* NOTREACHED */
8118 NOT_REACHED; /* NOTREACHED */
8120 case BRANCH_next_fail: /* that branch failed; try the next, if any */
8125 REGCP_UNWIND(ST.cp);
8126 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
8127 scan = ST.next_branch;
8128 /* no more branches? */
8129 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
8131 Perl_re_exec_indentf( aTHX_ "%sBRANCH failed...%s\n",
8138 continue; /* execute next BRANCH[J] op */
8141 case MINMOD: /* next op will be non-greedy, e.g. A*? */
8146 #define ST st->u.curlym
8148 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
8150 /* This is an optimisation of CURLYX that enables us to push
8151 * only a single backtracking state, no matter how many matches
8152 * there are in {m,n}. It relies on the pattern being constant
8153 * length, with no parens to influence future backrefs
8157 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
8159 ST.lastparen = rex->lastparen;
8160 ST.lastcloseparen = rex->lastcloseparen;
8162 /* if paren positive, emulate an OPEN/CLOSE around A */
8164 U32 paren = ST.me->flags;
8165 if (paren > maxopenparen)
8166 maxopenparen = paren;
8167 scan += NEXT_OFF(scan); /* Skip former OPEN. */
8175 ST.c1 = CHRTEST_UNINIT;
8178 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
8181 curlym_do_A: /* execute the A in /A{m,n}B/ */
8182 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A, locinput); /* match A */
8183 NOT_REACHED; /* NOTREACHED */
8185 case CURLYM_A: /* we've just matched an A */
8187 /* after first match, determine A's length: u.curlym.alen */
8188 if (ST.count == 1) {
8189 if (reginfo->is_utf8_target) {
8190 char *s = st->locinput;
8191 while (s < locinput) {
8197 ST.alen = locinput - st->locinput;
8200 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
8203 Perl_re_exec_indentf( aTHX_ "CURLYM now matched %" IVdf " times, len=%" IVdf "...\n",
8204 depth, (IV) ST.count, (IV)ST.alen)
8207 if (EVAL_CLOSE_PAREN_IS_TRUE(cur_eval,(U32)ST.me->flags))
8211 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
8212 if ( max == REG_INFTY || ST.count < max )
8213 goto curlym_do_A; /* try to match another A */
8215 goto curlym_do_B; /* try to match B */
8217 case CURLYM_A_fail: /* just failed to match an A */
8218 REGCP_UNWIND(ST.cp);
8221 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
8222 || EVAL_CLOSE_PAREN_IS_TRUE(cur_eval,(U32)ST.me->flags))
8225 curlym_do_B: /* execute the B in /A{m,n}B/ */
8226 if (ST.c1 == CHRTEST_UNINIT) {
8227 /* calculate c1 and c2 for possible match of 1st char
8228 * following curly */
8229 ST.c1 = ST.c2 = CHRTEST_VOID;
8231 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
8232 regnode *text_node = ST.B;
8233 if (! HAS_TEXT(text_node))
8234 FIND_NEXT_IMPT(text_node);
8237 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
8239 But the former is redundant in light of the latter.
8241 if this changes back then the macro for
8242 IS_TEXT and friends need to change.
8244 if (PL_regkind[OP(text_node)] == EXACT) {
8245 if (! S_setup_EXACTISH_ST_c1_c2(aTHX_
8246 text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8,
8256 Perl_re_exec_indentf( aTHX_ "CURLYM trying tail with matches=%" IVdf "...\n",
8257 depth, (IV)ST.count)
8259 if (! NEXTCHR_IS_EOS && ST.c1 != CHRTEST_VOID) {
8260 if (! UTF8_IS_INVARIANT(nextchr) && utf8_target) {
8261 if (memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput))
8262 && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput)))
8264 /* simulate B failing */
8266 Perl_re_exec_indentf( aTHX_ "CURLYM Fast bail next target=0x%" UVXf " c1=0x%" UVXf " c2=0x%" UVXf "\n",
8268 valid_utf8_to_uvchr((U8 *) locinput, NULL),
8269 valid_utf8_to_uvchr(ST.c1_utf8, NULL),
8270 valid_utf8_to_uvchr(ST.c2_utf8, NULL))
8272 state_num = CURLYM_B_fail;
8273 goto reenter_switch;
8276 else if (nextchr != ST.c1 && nextchr != ST.c2) {
8277 /* simulate B failing */
8279 Perl_re_exec_indentf( aTHX_ "CURLYM Fast bail next target=0x%X c1=0x%X c2=0x%X\n",
8281 (int) nextchr, ST.c1, ST.c2)
8283 state_num = CURLYM_B_fail;
8284 goto reenter_switch;
8289 /* emulate CLOSE: mark current A as captured */
8290 U32 paren = (U32)ST.me->flags;
8292 CLOSE_CAPTURE(paren,
8293 HOPc(locinput, -ST.alen) - reginfo->strbeg,
8294 locinput - reginfo->strbeg);
8297 rex->offs[paren].end = -1;
8299 if (EVAL_CLOSE_PAREN_IS_TRUE(cur_eval,(U32)ST.me->flags))
8308 PUSH_STATE_GOTO(CURLYM_B, ST.B, locinput); /* match B */
8309 NOT_REACHED; /* NOTREACHED */
8311 case CURLYM_B_fail: /* just failed to match a B */
8312 REGCP_UNWIND(ST.cp);
8313 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
8315 I32 max = ARG2(ST.me);
8316 if (max != REG_INFTY && ST.count == max)
8318 goto curlym_do_A; /* try to match a further A */
8320 /* backtrack one A */
8321 if (ST.count == ARG1(ST.me) /* min */)
8324 SET_locinput(HOPc(locinput, -ST.alen));
8325 goto curlym_do_B; /* try to match B */
8328 #define ST st->u.curly
8330 #define CURLY_SETPAREN(paren, success) \
8333 CLOSE_CAPTURE(paren, HOPc(locinput, -1) - reginfo->strbeg, \
8334 locinput - reginfo->strbeg); \
8337 rex->offs[paren].end = -1; \
8338 rex->lastparen = ST.lastparen; \
8339 rex->lastcloseparen = ST.lastcloseparen; \
8343 case STAR: /* /A*B/ where A is width 1 char */
8347 scan = NEXTOPER(scan);
8350 case PLUS: /* /A+B/ where A is width 1 char */
8354 scan = NEXTOPER(scan);
8357 case CURLYN: /* /(A){m,n}B/ where A is width 1 char */
8358 ST.paren = scan->flags; /* Which paren to set */
8359 ST.lastparen = rex->lastparen;
8360 ST.lastcloseparen = rex->lastcloseparen;
8361 if (ST.paren > maxopenparen)
8362 maxopenparen = ST.paren;
8363 ST.min = ARG1(scan); /* min to match */
8364 ST.max = ARG2(scan); /* max to match */
8365 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
8367 /* handle the single-char capture called as a GOSUB etc */
8368 if (EVAL_CLOSE_PAREN_IS_TRUE(cur_eval,(U32)ST.paren))
8370 char *li = locinput;
8371 if (!regrepeat(rex, &li, scan, reginfo, 1))
8379 case CURLY: /* /A{m,n}B/ where A is width 1 char */
8381 ST.min = ARG1(scan); /* min to match */
8382 ST.max = ARG2(scan); /* max to match */
8383 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
8386 * Lookahead to avoid useless match attempts
8387 * when we know what character comes next.
8389 * Used to only do .*x and .*?x, but now it allows
8390 * for )'s, ('s and (?{ ... })'s to be in the way
8391 * of the quantifier and the EXACT-like node. -- japhy
8394 assert(ST.min <= ST.max);
8395 if (! HAS_TEXT(next) && ! JUMPABLE(next)) {
8396 ST.c1 = ST.c2 = CHRTEST_VOID;
8399 regnode *text_node = next;
8401 if (! HAS_TEXT(text_node))
8402 FIND_NEXT_IMPT(text_node);
8404 if (! HAS_TEXT(text_node))
8405 ST.c1 = ST.c2 = CHRTEST_VOID;
8407 if ( PL_regkind[OP(text_node)] != EXACT ) {
8408 ST.c1 = ST.c2 = CHRTEST_VOID;
8412 /* Currently we only get here when
8414 PL_rekind[OP(text_node)] == EXACT
8416 if this changes back then the macro for IS_TEXT and
8417 friends need to change. */
8418 if (! S_setup_EXACTISH_ST_c1_c2(aTHX_
8419 text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8,
8431 char *li = locinput;
8434 regrepeat(rex, &li, ST.A, reginfo, ST.min)
8440 if (ST.c1 == CHRTEST_VOID)
8441 goto curly_try_B_min;
8443 ST.oldloc = locinput;
8445 /* set ST.maxpos to the furthest point along the
8446 * string that could possibly match */
8447 if (ST.max == REG_INFTY) {
8448 ST.maxpos = reginfo->strend - 1;
8450 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
8453 else if (utf8_target) {
8454 int m = ST.max - ST.min;
8455 for (ST.maxpos = locinput;
8456 m >0 && ST.maxpos < reginfo->strend; m--)
8457 ST.maxpos += UTF8SKIP(ST.maxpos);
8460 ST.maxpos = locinput + ST.max - ST.min;
8461 if (ST.maxpos >= reginfo->strend)
8462 ST.maxpos = reginfo->strend - 1;
8464 goto curly_try_B_min_known;
8468 /* avoid taking address of locinput, so it can remain
8470 char *li = locinput;
8471 ST.count = regrepeat(rex, &li, ST.A, reginfo, ST.max);
8472 if (ST.count < ST.min)
8475 if ((ST.count > ST.min)
8476 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
8478 /* A{m,n} must come at the end of the string, there's
8479 * no point in backing off ... */
8481 /* ...except that $ and \Z can match before *and* after
8482 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
8483 We may back off by one in this case. */
8484 if (UCHARAT(locinput - 1) == '\n' && OP(ST.B) != EOS)
8488 goto curly_try_B_max;
8490 NOT_REACHED; /* NOTREACHED */
8492 case CURLY_B_min_fail:
8493 /* failed to find B in a non-greedy match.
8494 * Handles both cases where c1,c2 valid or not */
8496 REGCP_UNWIND(ST.cp);
8498 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
8501 if (ST.c1 == CHRTEST_VOID) {
8502 /* failed -- move forward one */
8503 char *li = locinput;
8504 if (!regrepeat(rex, &li, ST.A, reginfo, 1)) {
8509 if (!( ST.count <= ST.max
8510 /* count overflow ? */
8511 || (ST.max == REG_INFTY && ST.count > 0))
8517 /* Couldn't or didn't -- move forward. */
8518 ST.oldloc = locinput;
8520 locinput += UTF8SKIP(locinput);
8525 curly_try_B_min_known:
8526 /* find the next place where 'B' could work, then call B */
8528 n = (ST.oldloc == locinput) ? 0 : 1;
8529 if (ST.c1 == ST.c2) {
8530 /* set n to utf8_distance(oldloc, locinput) */
8531 while (locinput <= ST.maxpos
8532 && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput)))
8534 locinput += UTF8SKIP(locinput);
8539 /* set n to utf8_distance(oldloc, locinput) */
8540 while (locinput <= ST.maxpos
8541 && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput))
8542 && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput)))
8544 locinput += UTF8SKIP(locinput);
8549 else { /* Not utf8_target */
8550 if (ST.c1 == ST.c2) {
8551 locinput = (char *) memchr(locinput,
8553 ST.maxpos + 1 - locinput);
8555 locinput = ST.maxpos + 1;
8559 U8 c1_c2_bits_differing = ST.c1 ^ ST.c2;
8561 if (! isPOWER_OF_2(c1_c2_bits_differing)) {
8562 while ( locinput <= ST.maxpos
8563 && UCHARAT(locinput) != ST.c1
8564 && UCHARAT(locinput) != ST.c2)
8570 /* If c1 and c2 only differ by a single bit, we can
8571 * avoid a conditional each time through the loop,
8572 * at the expense of a little preliminary setup and
8573 * an extra mask each iteration. By masking out
8574 * that bit, we match exactly two characters, c1
8575 * and c2, and so we don't have to test for both.
8576 * On both ASCII and EBCDIC platforms, most of the
8577 * ASCII-range and Latin1-range folded equivalents
8578 * differ only in a single bit, so this is actually
8579 * the most common case. (e.g. 'A' 0x41 vs 'a'
8581 U8 c1_masked = ST.c1 &~ c1_c2_bits_differing;
8582 U8 c1_c2_mask = ~ c1_c2_bits_differing;
8583 while ( locinput <= ST.maxpos
8584 && (UCHARAT(locinput) & c1_c2_mask)
8591 n = locinput - ST.oldloc;
8593 if (locinput > ST.maxpos)
8596 /* In /a{m,n}b/, ST.oldloc is at "a" x m, locinput is
8597 * at b; check that everything between oldloc and
8598 * locinput matches */
8599 char *li = ST.oldloc;
8601 if (regrepeat(rex, &li, ST.A, reginfo, n) < n)
8603 assert(n == REG_INFTY || locinput == li);
8608 CURLY_SETPAREN(ST.paren, ST.count);
8609 PUSH_STATE_GOTO(CURLY_B_min, ST.B, locinput);
8610 NOT_REACHED; /* NOTREACHED */
8614 /* a successful greedy match: now try to match B */
8616 bool could_match = locinput < reginfo->strend;
8618 /* If it could work, try it. */
8619 if (ST.c1 != CHRTEST_VOID && could_match) {
8620 if (! UTF8_IS_INVARIANT(UCHARAT(locinput)) && utf8_target)
8622 could_match = memEQ(locinput,
8627 UTF8SKIP(locinput));
8630 could_match = UCHARAT(locinput) == ST.c1
8631 || UCHARAT(locinput) == ST.c2;
8634 if (ST.c1 == CHRTEST_VOID || could_match) {
8635 CURLY_SETPAREN(ST.paren, ST.count);
8636 PUSH_STATE_GOTO(CURLY_B_max, ST.B, locinput);
8637 NOT_REACHED; /* NOTREACHED */
8642 case CURLY_B_max_fail:
8643 /* failed to find B in a greedy match */
8645 REGCP_UNWIND(ST.cp);
8647 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
8650 if (--ST.count < ST.min)
8652 locinput = HOPc(locinput, -1);
8653 goto curly_try_B_max;
8657 case END: /* last op of main pattern */
8660 /* we've just finished A in /(??{A})B/; now continue with B */
8661 SET_RECURSE_LOCINPUT("FAKE-END[before]", CUR_EVAL.prev_recurse_locinput);
8662 st->u.eval.prev_rex = rex_sv; /* inner */
8664 /* Save *all* the positions. */
8665 st->u.eval.cp = regcppush(rex, 0, maxopenparen);
8666 rex_sv = CUR_EVAL.prev_rex;
8667 is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv));
8668 SET_reg_curpm(rex_sv);
8669 rex = ReANY(rex_sv);
8670 rexi = RXi_GET(rex);
8672 st->u.eval.prev_curlyx = cur_curlyx;
8673 cur_curlyx = CUR_EVAL.prev_curlyx;
8675 REGCP_SET(st->u.eval.lastcp);
8677 /* Restore parens of the outer rex without popping the
8679 regcp_restore(rex, CUR_EVAL.lastcp, &maxopenparen);
8681 st->u.eval.prev_eval = cur_eval;
8682 cur_eval = CUR_EVAL.prev_eval;
8684 Perl_re_exec_indentf( aTHX_ "END: EVAL trying tail ... (cur_eval=%p)\n",
8686 if ( nochange_depth )
8689 SET_RECURSE_LOCINPUT("FAKE-END[after]", cur_eval->locinput);
8691 PUSH_YES_STATE_GOTO(EVAL_postponed_AB, st->u.eval.prev_eval->u.eval.B,
8692 locinput); /* match B */
8695 if (locinput < reginfo->till) {
8696 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
8697 "%sEND: Match possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
8699 (long)(locinput - startpos),
8700 (long)(reginfo->till - startpos),
8703 sayNO_SILENT; /* Cannot match: too short. */
8705 sayYES; /* Success! */
8707 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
8709 Perl_re_exec_indentf( aTHX_ "%sSUCCEED: subpattern success...%s\n",
8710 depth, PL_colors[4], PL_colors[5]));
8711 sayYES; /* Success! */
8714 #define ST st->u.ifmatch
8719 case SUSPEND: /* (?>A) */
8721 newstart = locinput;
8724 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
8726 goto ifmatch_trivial_fail_test;
8728 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
8730 ifmatch_trivial_fail_test:
8732 char * const s = HOPBACKc(locinput, scan->flags);
8737 sw = 1 - cBOOL(ST.wanted);
8741 next = scan + ARG(scan);
8749 newstart = locinput;
8753 ST.logical = logical;
8754 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
8756 /* execute body of (?...A) */
8757 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)), newstart);
8758 NOT_REACHED; /* NOTREACHED */
8761 case IFMATCH_A_fail: /* body of (?...A) failed */
8762 ST.wanted = !ST.wanted;
8765 case IFMATCH_A: /* body of (?...A) succeeded */
8767 sw = cBOOL(ST.wanted);
8769 else if (!ST.wanted)
8772 if (OP(ST.me) != SUSPEND) {
8773 /* restore old position except for (?>...) */
8774 locinput = st->locinput;
8776 scan = ST.me + ARG(ST.me);
8779 continue; /* execute B */
8783 case LONGJMP: /* alternative with many branches compiles to
8784 * (BRANCHJ; EXACT ...; LONGJMP ) x N */
8785 next = scan + ARG(scan);
8790 case COMMIT: /* (*COMMIT) */
8791 reginfo->cutpoint = reginfo->strend;
8794 case PRUNE: /* (*PRUNE) */
8796 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
8797 PUSH_STATE_GOTO(COMMIT_next, next, locinput);
8798 NOT_REACHED; /* NOTREACHED */
8800 case COMMIT_next_fail:
8804 NOT_REACHED; /* NOTREACHED */
8806 case OPFAIL: /* (*FAIL) */
8808 sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
8810 /* deal with (?(?!)X|Y) properly,
8811 * make sure we trigger the no branch
8812 * of the trailing IFTHEN structure*/
8818 NOT_REACHED; /* NOTREACHED */
8820 #define ST st->u.mark
8821 case MARKPOINT: /* (*MARK:foo) */
8822 ST.prev_mark = mark_state;
8823 ST.mark_name = sv_commit = sv_yes_mark
8824 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
8826 ST.mark_loc = locinput;
8827 PUSH_YES_STATE_GOTO(MARKPOINT_next, next, locinput);
8828 NOT_REACHED; /* NOTREACHED */
8830 case MARKPOINT_next:
8831 mark_state = ST.prev_mark;
8833 NOT_REACHED; /* NOTREACHED */
8835 case MARKPOINT_next_fail:
8836 if (popmark && sv_eq(ST.mark_name,popmark))
8838 if (ST.mark_loc > startpoint)
8839 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
8840 popmark = NULL; /* we found our mark */
8841 sv_commit = ST.mark_name;
8844 Perl_re_exec_indentf( aTHX_ "%sMARKPOINT: next fail: setting cutpoint to mark:%" SVf "...%s\n",
8846 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
8849 mark_state = ST.prev_mark;
8850 sv_yes_mark = mark_state ?
8851 mark_state->u.mark.mark_name : NULL;
8853 NOT_REACHED; /* NOTREACHED */
8855 case SKIP: /* (*SKIP) */
8857 /* (*SKIP) : if we fail we cut here*/
8858 ST.mark_name = NULL;
8859 ST.mark_loc = locinput;
8860 PUSH_STATE_GOTO(SKIP_next,next, locinput);
8862 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
8863 otherwise do nothing. Meaning we need to scan
8865 regmatch_state *cur = mark_state;
8866 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
8869 if ( sv_eq( cur->u.mark.mark_name,
8872 ST.mark_name = find;
8873 PUSH_STATE_GOTO( SKIP_next, next, locinput);
8875 cur = cur->u.mark.prev_mark;
8878 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
8881 case SKIP_next_fail:
8883 /* (*CUT:NAME) - Set up to search for the name as we
8884 collapse the stack*/
8885 popmark = ST.mark_name;
8887 /* (*CUT) - No name, we cut here.*/
8888 if (ST.mark_loc > startpoint)
8889 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
8890 /* but we set sv_commit to latest mark_name if there
8891 is one so they can test to see how things lead to this
8894 sv_commit=mark_state->u.mark.mark_name;
8898 NOT_REACHED; /* NOTREACHED */
8901 case LNBREAK: /* \R */
8902 if ((n=is_LNBREAK_safe(locinput, reginfo->strend, utf8_target))) {
8909 PerlIO_printf(Perl_error_log, "%" UVxf " %d\n",
8910 PTR2UV(scan), OP(scan));
8911 Perl_croak(aTHX_ "regexp memory corruption");
8913 /* this is a point to jump to in order to increment
8914 * locinput by one character */
8916 assert(!NEXTCHR_IS_EOS);
8918 locinput += PL_utf8skip[nextchr];
8919 /* locinput is allowed to go 1 char off the end (signifying
8920 * EOS), but not 2+ */
8921 if (locinput > reginfo->strend)
8930 /* switch break jumps here */
8931 scan = next; /* prepare to execute the next op and ... */
8932 continue; /* ... jump back to the top, reusing st */
8936 /* push a state that backtracks on success */
8937 st->u.yes.prev_yes_state = yes_state;
8941 /* push a new regex state, then continue at scan */
8943 regmatch_state *newst;
8946 regmatch_state *cur = st;
8947 regmatch_state *curyes = yes_state;
8949 regmatch_slab *slab = PL_regmatch_slab;
8950 for (i = 0; i < 3 && i <= depth; cur--,i++) {
8951 if (cur < SLAB_FIRST(slab)) {
8953 cur = SLAB_LAST(slab);
8955 Perl_re_exec_indentf( aTHX_ "%4s #%-3d %-10s %s\n",
8958 depth - i, PL_reg_name[cur->resume_state],
8959 (curyes == cur) ? "yes" : ""
8962 curyes = cur->u.yes.prev_yes_state;
8965 DEBUG_STATE_pp("push")
8968 st->locinput = locinput;
8970 if (newst > SLAB_LAST(PL_regmatch_slab))
8971 newst = S_push_slab(aTHX);
8972 PL_regmatch_state = newst;
8974 locinput = pushinput;
8980 #ifdef SOLARIS_BAD_OPTIMIZER
8981 # undef PL_charclass
8985 * We get here only if there's trouble -- normally "case END" is
8986 * the terminating point.
8988 Perl_croak(aTHX_ "corrupted regexp pointers");
8989 NOT_REACHED; /* NOTREACHED */
8993 /* we have successfully completed a subexpression, but we must now
8994 * pop to the state marked by yes_state and continue from there */
8995 assert(st != yes_state);
8997 while (st != yes_state) {
8999 if (st < SLAB_FIRST(PL_regmatch_slab)) {
9000 PL_regmatch_slab = PL_regmatch_slab->prev;
9001 st = SLAB_LAST(PL_regmatch_slab);
9005 DEBUG_STATE_pp("pop (no final)");
9007 DEBUG_STATE_pp("pop (yes)");
9013 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
9014 || yes_state > SLAB_LAST(PL_regmatch_slab))
9016 /* not in this slab, pop slab */
9017 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
9018 PL_regmatch_slab = PL_regmatch_slab->prev;
9019 st = SLAB_LAST(PL_regmatch_slab);
9021 depth -= (st - yes_state);
9024 yes_state = st->u.yes.prev_yes_state;
9025 PL_regmatch_state = st;
9028 locinput= st->locinput;
9029 state_num = st->resume_state + no_final;
9030 goto reenter_switch;
9033 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%sMatch successful!%s\n",
9034 PL_colors[4], PL_colors[5]));
9036 if (reginfo->info_aux_eval) {
9037 /* each successfully executed (?{...}) block does the equivalent of
9038 * local $^R = do {...}
9039 * When popping the save stack, all these locals would be undone;
9040 * bypass this by setting the outermost saved $^R to the latest
9042 /* I dont know if this is needed or works properly now.
9043 * see code related to PL_replgv elsewhere in this file.
9046 if (oreplsv != GvSV(PL_replgv))
9047 sv_setsv(oreplsv, GvSV(PL_replgv));
9054 Perl_re_exec_indentf( aTHX_ "%sfailed...%s\n",
9056 PL_colors[4], PL_colors[5])
9068 /* there's a previous state to backtrack to */
9070 if (st < SLAB_FIRST(PL_regmatch_slab)) {
9071 PL_regmatch_slab = PL_regmatch_slab->prev;
9072 st = SLAB_LAST(PL_regmatch_slab);
9074 PL_regmatch_state = st;
9075 locinput= st->locinput;
9077 DEBUG_STATE_pp("pop");
9079 if (yes_state == st)
9080 yes_state = st->u.yes.prev_yes_state;
9082 state_num = st->resume_state + 1; /* failure = success + 1 */
9084 goto reenter_switch;
9089 if (rex->intflags & PREGf_VERBARG_SEEN) {
9090 SV *sv_err = get_sv("REGERROR", 1);
9091 SV *sv_mrk = get_sv("REGMARK", 1);
9093 sv_commit = &PL_sv_no;
9095 sv_yes_mark = &PL_sv_yes;
9098 sv_commit = &PL_sv_yes;
9099 sv_yes_mark = &PL_sv_no;
9103 sv_setsv(sv_err, sv_commit);
9104 sv_setsv(sv_mrk, sv_yes_mark);
9108 if (last_pushed_cv) {
9110 /* see "Some notes about MULTICALL" above */
9112 PERL_UNUSED_VAR(SP);
9115 LEAVE_SCOPE(orig_savestack_ix);
9117 assert(!result || locinput - reginfo->strbeg >= 0);
9118 return result ? locinput - reginfo->strbeg : -1;
9122 - regrepeat - repeatedly match something simple, report how many
9124 * What 'simple' means is a node which can be the operand of a quantifier like
9127 * startposp - pointer a pointer to the start position. This is updated
9128 * to point to the byte following the highest successful
9130 * p - the regnode to be repeatedly matched against.
9131 * reginfo - struct holding match state, such as strend
9132 * max - maximum number of things to match.
9133 * depth - (for debugging) backtracking depth.
9136 S_regrepeat(pTHX_ regexp *prog, char **startposp, const regnode *p,
9137 regmatch_info *const reginfo, I32 max _pDEPTH)
9139 char *scan; /* Pointer to current position in target string */
9141 char *loceol = reginfo->strend; /* local version */
9142 I32 hardcount = 0; /* How many matches so far */
9143 bool utf8_target = reginfo->is_utf8_target;
9144 unsigned int to_complement = 0; /* Invert the result? */
9146 _char_class_number classnum;
9148 PERL_ARGS_ASSERT_REGREPEAT;
9151 if (max == REG_INFTY)
9153 else if (! utf8_target && loceol - scan > max)
9154 loceol = scan + max;
9156 /* Here, for the case of a non-UTF-8 target we have adjusted <loceol> down
9157 * to the maximum of how far we should go in it (leaving it set to the real
9158 * end, if the maximum permissible would take us beyond that). This allows
9159 * us to make the loop exit condition that we haven't gone past <loceol> to
9160 * also mean that we haven't exceeded the max permissible count, saving a
9161 * test each time through the loop. But it assumes that the OP matches a
9162 * single byte, which is true for most of the OPs below when applied to a
9163 * non-UTF-8 target. Those relatively few OPs that don't have this
9164 * characteristic will have to compensate.
9166 * There is no adjustment for UTF-8 targets, as the number of bytes per
9167 * character varies. OPs will have to test both that the count is less
9168 * than the max permissible (using <hardcount> to keep track), and that we
9169 * are still within the bounds of the string (using <loceol>. A few OPs
9170 * match a single byte no matter what the encoding. They can omit the max
9171 * test if, for the UTF-8 case, they do the adjustment that was skipped
9174 * Thus, the code above sets things up for the common case; and exceptional
9175 * cases need extra work; the common case is to make sure <scan> doesn't
9176 * go past <loceol>, and for UTF-8 to also use <hardcount> to make sure the
9177 * count doesn't exceed the maximum permissible */
9182 while (scan < loceol && hardcount < max && *scan != '\n') {
9183 scan += UTF8SKIP(scan);
9187 scan = (char *) memchr(scan, '\n', loceol - scan);
9195 while (scan < loceol && hardcount < max) {
9196 scan += UTF8SKIP(scan);
9204 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
9205 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*scan)) {
9206 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(scan, loceol);
9210 assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1);
9214 /* Can use a simple find if the pattern char to match on is invariant
9215 * under UTF-8, or both target and pattern aren't UTF-8. Note that we
9216 * can use UTF8_IS_INVARIANT() even if the pattern isn't UTF-8, as it's
9217 * true iff it doesn't matter if the argument is in UTF-8 or not */
9218 if (UTF8_IS_INVARIANT(c) || (! utf8_target && ! reginfo->is_utf8_pat)) {
9219 if (utf8_target && loceol - scan > max) {
9220 /* We didn't adjust <loceol> because is UTF-8, but ok to do so,
9221 * since here, to match at all, 1 char == 1 byte */
9222 loceol = scan + max;
9224 scan = (char *) find_span_end((U8 *) scan, (U8 *) loceol, (U8) c);
9226 else if (reginfo->is_utf8_pat) {
9228 STRLEN scan_char_len;
9230 /* When both target and pattern are UTF-8, we have to do
9232 while (hardcount < max
9234 && (scan_char_len = UTF8SKIP(scan)) <= STR_LEN(p)
9235 && memEQ(scan, STRING(p), scan_char_len))
9237 scan += scan_char_len;
9241 else if (! UTF8_IS_ABOVE_LATIN1(c)) {
9243 /* Target isn't utf8; convert the character in the UTF-8
9244 * pattern to non-UTF8, and do a simple find */
9245 c = EIGHT_BIT_UTF8_TO_NATIVE(c, *(STRING(p) + 1));
9246 scan = (char *) find_span_end((U8 *) scan, (U8 *) loceol, (U8) c);
9247 } /* else pattern char is above Latin1, can't possibly match the
9252 /* Here, the string must be utf8; pattern isn't, and <c> is
9253 * different in utf8 than not, so can't compare them directly.
9254 * Outside the loop, find the two utf8 bytes that represent c, and
9255 * then look for those in sequence in the utf8 string */
9256 U8 high = UTF8_TWO_BYTE_HI(c);
9257 U8 low = UTF8_TWO_BYTE_LO(c);
9259 while (hardcount < max
9260 && scan + 1 < loceol
9261 && UCHARAT(scan) == high
9262 && UCHARAT(scan + 1) == low)
9270 case EXACTFAA_NO_TRIE: /* This node only generated for non-utf8 patterns */
9271 assert(! reginfo->is_utf8_pat);
9274 utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
9278 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
9279 utf8_flags = FOLDEQ_LOCALE;
9282 case EXACTF: /* This node only generated for non-utf8 patterns */
9283 assert(! reginfo->is_utf8_pat);
9288 if (! utf8_target) {
9291 utf8_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED
9292 | FOLDEQ_S2_FOLDS_SANE;
9297 utf8_flags = reginfo->is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
9301 U8 c1_utf8[UTF8_MAXBYTES+1], c2_utf8[UTF8_MAXBYTES+1];
9303 assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1);
9305 if (S_setup_EXACTISH_ST_c1_c2(aTHX_ p, &c1, c1_utf8, &c2, c2_utf8,
9308 if (c1 == CHRTEST_VOID) {
9309 /* Use full Unicode fold matching */
9310 char *tmpeol = reginfo->strend;
9311 STRLEN pat_len = reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1;
9312 while (hardcount < max
9313 && foldEQ_utf8_flags(scan, &tmpeol, 0, utf8_target,
9314 STRING(p), NULL, pat_len,
9315 reginfo->is_utf8_pat, utf8_flags))
9318 tmpeol = reginfo->strend;
9322 else if (utf8_target) {
9324 while (scan < loceol
9326 && memEQ(scan, c1_utf8, UTF8SKIP(scan)))
9328 scan += UTF8SKIP(scan);
9333 while (scan < loceol
9335 && (memEQ(scan, c1_utf8, UTF8SKIP(scan))
9336 || memEQ(scan, c2_utf8, UTF8SKIP(scan))))
9338 scan += UTF8SKIP(scan);
9343 else if (c1 == c2) {
9344 scan = (char *) find_span_end((U8 *) scan, (U8 *) loceol, (U8) c1);
9347 /* See comments in regmatch() CURLY_B_min_known_fail. We avoid
9348 * a conditional each time through the loop if the characters
9349 * differ only in a single bit, as is the usual situation */
9350 U8 c1_c2_bits_differing = c1 ^ c2;
9352 if (isPOWER_OF_2(c1_c2_bits_differing)) {
9353 U8 c1_c2_mask = ~ c1_c2_bits_differing;
9355 scan = (char *) find_span_end_mask((U8 *) scan,
9361 while ( scan < loceol
9362 && (UCHARAT(scan) == c1 || UCHARAT(scan) == c2))
9373 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
9375 if (ANYOFL_UTF8_LOCALE_REQD(FLAGS(p)) && ! IN_UTF8_CTYPE_LOCALE) {
9376 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), utf8_locale_required);
9382 while (hardcount < max
9384 && reginclass(prog, p, (U8*)scan, (U8*) loceol, utf8_target))
9386 scan += UTF8SKIP(scan);
9390 else if (ANYOF_FLAGS(p)) {
9391 while (scan < loceol
9392 && reginclass(prog, p, (U8*)scan, (U8*)scan+1, 0))
9396 while (scan < loceol && ANYOF_BITMAP_TEST(p, *((U8*)scan)))
9402 if (utf8_target && loceol - scan > max) {
9404 /* We didn't adjust <loceol> at the beginning of this routine
9405 * because is UTF-8, but it is actually ok to do so, since here, to
9406 * match, 1 char == 1 byte. */
9407 loceol = scan + max;
9410 scan = (char *) find_span_end_mask((U8 *) scan, (U8 *) loceol, (U8) ARG(p), FLAGS(p));
9415 while ( hardcount < max
9417 && (*scan & FLAGS(p)) != ARG(p))
9419 scan += UTF8SKIP(scan);
9424 scan = (char *) find_next_masked((U8 *) scan, (U8 *) loceol, (U8) ARG(p), FLAGS(p));
9429 if (utf8_target && loceol - scan > max) {
9430 loceol = scan + max;
9433 scan = find_next_non_ascii(scan, loceol, utf8_target);
9438 while ( hardcount < max
9440 && ! isASCII_utf8_safe(scan, loceol))
9442 scan += UTF8SKIP(scan);
9447 scan = find_next_ascii(scan, loceol, utf8_target);
9451 /* The argument (FLAGS) to all the POSIX node types is the class number */
9458 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
9459 if (! utf8_target) {
9460 while (scan < loceol && to_complement ^ cBOOL(isFOO_lc(FLAGS(p),
9466 while (hardcount < max && scan < loceol
9467 && to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(p),
9471 scan += UTF8SKIP(scan);
9484 if (utf8_target && loceol - scan > max) {
9486 /* We didn't adjust <loceol> at the beginning of this routine
9487 * because is UTF-8, but it is actually ok to do so, since here, to
9488 * match, 1 char == 1 byte. */
9489 loceol = scan + max;
9491 while (scan < loceol && _generic_isCC_A((U8) *scan, FLAGS(p))) {
9504 if (! utf8_target) {
9505 while (scan < loceol && ! _generic_isCC_A((U8) *scan, FLAGS(p))) {
9511 /* The complement of something that matches only ASCII matches all
9512 * non-ASCII, plus everything in ASCII that isn't in the class. */
9513 while (hardcount < max && scan < loceol
9514 && ( ! isASCII_utf8_safe(scan, reginfo->strend)
9515 || ! _generic_isCC_A((U8) *scan, FLAGS(p))))
9517 scan += UTF8SKIP(scan);
9528 if (! utf8_target) {
9529 while (scan < loceol && to_complement
9530 ^ cBOOL(_generic_isCC((U8) *scan, FLAGS(p))))
9537 classnum = (_char_class_number) FLAGS(p);
9540 while ( hardcount < max && scan < loceol
9541 && to_complement ^ cBOOL(_invlist_contains_cp(
9542 PL_XPosix_ptrs[classnum],
9543 utf8_to_uvchr_buf((U8 *) scan,
9547 scan += UTF8SKIP(scan);
9552 /* For the classes below, the knowledge of how to handle
9553 * every code point is compiled in to Perl via a macro.
9554 * This code is written for making the loops as tight as
9555 * possible. It could be refactored to save space instead.
9558 case _CC_ENUM_SPACE:
9559 while (hardcount < max
9562 ^ cBOOL(isSPACE_utf8_safe(scan, loceol))))
9564 scan += UTF8SKIP(scan);
9568 case _CC_ENUM_BLANK:
9569 while (hardcount < max
9572 ^ cBOOL(isBLANK_utf8_safe(scan, loceol))))
9574 scan += UTF8SKIP(scan);
9578 case _CC_ENUM_XDIGIT:
9579 while (hardcount < max
9582 ^ cBOOL(isXDIGIT_utf8_safe(scan, loceol))))
9584 scan += UTF8SKIP(scan);
9588 case _CC_ENUM_VERTSPACE:
9589 while (hardcount < max
9592 ^ cBOOL(isVERTWS_utf8_safe(scan, loceol))))
9594 scan += UTF8SKIP(scan);
9598 case _CC_ENUM_CNTRL:
9599 while (hardcount < max
9602 ^ cBOOL(isCNTRL_utf8_safe(scan, loceol))))
9604 scan += UTF8SKIP(scan);
9614 while (hardcount < max && scan < loceol &&
9615 (c=is_LNBREAK_utf8_safe(scan, loceol))) {
9620 /* LNBREAK can match one or two latin chars, which is ok, but we
9621 * have to use hardcount in this situation, and throw away the
9622 * adjustment to <loceol> done before the switch statement */
9623 loceol = reginfo->strend;
9624 while (scan < loceol && (c=is_LNBREAK_latin1_safe(scan, loceol))) {
9633 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
9647 /* These are all 0 width, so match right here or not at all. */
9651 Perl_croak(aTHX_ "panic: regrepeat() called with unrecognized node type %d='%s'", OP(p), PL_reg_name[OP(p)]);
9652 NOT_REACHED; /* NOTREACHED */
9659 c = scan - *startposp;
9663 GET_RE_DEBUG_FLAGS_DECL;
9665 SV * const prop = sv_newmortal();
9666 regprop(prog, prop, p, reginfo, NULL);
9667 Perl_re_exec_indentf( aTHX_ "%s can match %" IVdf " times out of %" IVdf "...\n",
9668 depth, SvPVX_const(prop),(IV)c,(IV)max);
9676 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
9678 - regclass_swash - prepare the utf8 swash. Wraps the shared core version to
9679 create a copy so that changes the caller makes won't change the shared one.
9680 If <altsvp> is non-null, will return NULL in it, for back-compat.
9683 Perl_regclass_swash(pTHX_ const regexp *prog, const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
9685 PERL_ARGS_ASSERT_REGCLASS_SWASH;
9691 return newSVsv(_get_regclass_nonbitmap_data(prog, node, doinit, listsvp, NULL, NULL));
9694 #endif /* !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) */
9697 - reginclass - determine if a character falls into a character class
9699 n is the ANYOF-type regnode
9700 p is the target string
9701 p_end points to one byte beyond the end of the target string
9702 utf8_target tells whether p is in UTF-8.
9704 Returns true if matched; false otherwise.
9706 Note that this can be a synthetic start class, a combination of various
9707 nodes, so things you think might be mutually exclusive, such as locale,
9708 aren't. It can match both locale and non-locale
9713 S_reginclass(pTHX_ regexp * const prog, const regnode * const n, const U8* const p, const U8* const p_end, const bool utf8_target)
9716 const char flags = ANYOF_FLAGS(n);
9720 PERL_ARGS_ASSERT_REGINCLASS;
9722 /* If c is not already the code point, get it. Note that
9723 * UTF8_IS_INVARIANT() works even if not in UTF-8 */
9724 if (! UTF8_IS_INVARIANT(c) && utf8_target) {
9726 const U32 utf8n_flags = UTF8_ALLOW_DEFAULT;
9727 c = utf8n_to_uvchr(p, p_end - p, &c_len, utf8n_flags | UTF8_CHECK_ONLY);
9728 if (c_len == (STRLEN)-1) {
9729 _force_out_malformed_utf8_message(p, p_end,
9731 1 /* 1 means die */ );
9732 NOT_REACHED; /* NOTREACHED */
9735 && (OP(n) == ANYOFL || OP(n) == ANYOFPOSIXL)
9736 && ! ANYOFL_UTF8_LOCALE_REQD(flags))
9738 _CHECK_AND_OUTPUT_WIDE_LOCALE_CP_MSG(c);
9742 /* If this character is potentially in the bitmap, check it */
9743 if (c < NUM_ANYOF_CODE_POINTS) {
9744 if (ANYOF_BITMAP_TEST(n, c))
9747 & ANYOF_SHARED_d_MATCHES_ALL_NON_UTF8_NON_ASCII_non_d_WARN_SUPER)
9754 else if (flags & ANYOF_LOCALE_FLAGS) {
9755 if ((flags & ANYOFL_FOLD)
9757 && ANYOF_BITMAP_TEST(n, PL_fold_locale[c]))
9761 else if (ANYOF_POSIXL_TEST_ANY_SET(n)
9765 /* The data structure is arranged so bits 0, 2, 4, ... are set
9766 * if the class includes the Posix character class given by
9767 * bit/2; and 1, 3, 5, ... are set if the class includes the
9768 * complemented Posix class given by int(bit/2). So we loop
9769 * through the bits, each time changing whether we complement
9770 * the result or not. Suppose for the sake of illustration
9771 * that bits 0-3 mean respectively, \w, \W, \s, \S. If bit 0
9772 * is set, it means there is a match for this ANYOF node if the
9773 * character is in the class given by the expression (0 / 2 = 0
9774 * = \w). If it is in that class, isFOO_lc() will return 1,
9775 * and since 'to_complement' is 0, the result will stay TRUE,
9776 * and we exit the loop. Suppose instead that bit 0 is 0, but
9777 * bit 1 is 1. That means there is a match if the character
9778 * matches \W. We won't bother to call isFOO_lc() on bit 0,
9779 * but will on bit 1. On the second iteration 'to_complement'
9780 * will be 1, so the exclusive or will reverse things, so we
9781 * are testing for \W. On the third iteration, 'to_complement'
9782 * will be 0, and we would be testing for \s; the fourth
9783 * iteration would test for \S, etc.
9785 * Note that this code assumes that all the classes are closed
9786 * under folding. For example, if a character matches \w, then
9787 * its fold does too; and vice versa. This should be true for
9788 * any well-behaved locale for all the currently defined Posix
9789 * classes, except for :lower: and :upper:, which are handled
9790 * by the pseudo-class :cased: which matches if either of the
9791 * other two does. To get rid of this assumption, an outer
9792 * loop could be used below to iterate over both the source
9793 * character, and its fold (if different) */
9796 int to_complement = 0;
9798 while (count < ANYOF_MAX) {
9799 if (ANYOF_POSIXL_TEST(n, count)
9800 && to_complement ^ cBOOL(isFOO_lc(count/2, (U8) c)))
9813 /* If the bitmap didn't (or couldn't) match, and something outside the
9814 * bitmap could match, try that. */
9816 if (c >= NUM_ANYOF_CODE_POINTS
9817 && (flags & ANYOF_MATCHES_ALL_ABOVE_BITMAP))
9819 match = TRUE; /* Everything above the bitmap matches */
9821 /* Here doesn't match everything above the bitmap. If there is
9822 * some information available beyond the bitmap, we may find a
9823 * match in it. If so, this is most likely because the code point
9824 * is outside the bitmap range. But rarely, it could be because of
9825 * some other reason. If so, various flags are set to indicate
9826 * this possibility. On ANYOFD nodes, there may be matches that
9827 * happen only when the target string is UTF-8; or for other node
9828 * types, because runtime lookup is needed, regardless of the
9829 * UTF-8ness of the target string. Finally, under /il, there may
9830 * be some matches only possible if the locale is a UTF-8 one. */
9831 else if ( ARG(n) != ANYOF_ONLY_HAS_BITMAP
9832 && ( c >= NUM_ANYOF_CODE_POINTS
9833 || ( (flags & ANYOF_SHARED_d_UPPER_LATIN1_UTF8_STRING_MATCHES_non_d_RUNTIME_USER_PROP)
9834 && ( UNLIKELY(OP(n) != ANYOFD)
9835 || (utf8_target && ! isASCII_uni(c)
9836 # if NUM_ANYOF_CODE_POINTS > 256
9840 || ( ANYOFL_SOME_FOLDS_ONLY_IN_UTF8_LOCALE(flags)
9841 && IN_UTF8_CTYPE_LOCALE)))
9843 SV* only_utf8_locale = NULL;
9844 SV * const sw = _get_regclass_nonbitmap_data(prog, n, TRUE, 0,
9845 &only_utf8_locale, NULL);
9851 } else { /* Convert to utf8 */
9852 utf8_p = utf8_buffer;
9853 append_utf8_from_native_byte(*p, &utf8_p);
9854 utf8_p = utf8_buffer;
9857 if (swash_fetch(sw, utf8_p, TRUE)) {
9861 if (! match && only_utf8_locale && IN_UTF8_CTYPE_LOCALE) {
9862 match = _invlist_contains_cp(only_utf8_locale, c);
9866 if (UNICODE_IS_SUPER(c)
9868 & ANYOF_SHARED_d_MATCHES_ALL_NON_UTF8_NON_ASCII_non_d_WARN_SUPER)
9870 && ckWARN_d(WARN_NON_UNICODE))
9872 Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE),
9873 "Matched non-Unicode code point 0x%04" UVXf " against Unicode property; may not be portable", c);
9877 #if ANYOF_INVERT != 1
9878 /* Depending on compiler optimization cBOOL takes time, so if don't have to
9880 # error ANYOF_INVERT needs to be set to 1, or guarded with cBOOL below,
9883 /* The xor complements the return if to invert: 1^1 = 0, 1^0 = 1 */
9884 return (flags & ANYOF_INVERT) ^ match;
9888 S_reghop3(U8 *s, SSize_t off, const U8* lim)
9890 /* return the position 'off' UTF-8 characters away from 's', forward if
9891 * 'off' >= 0, backwards if negative. But don't go outside of position
9892 * 'lim', which better be < s if off < 0 */
9894 PERL_ARGS_ASSERT_REGHOP3;
9897 while (off-- && s < lim) {
9898 /* XXX could check well-formedness here */
9899 U8 *new_s = s + UTF8SKIP(s);
9900 if (new_s > lim) /* lim may be in the middle of a long character */
9906 while (off++ && s > lim) {
9908 if (UTF8_IS_CONTINUED(*s)) {
9909 while (s > lim && UTF8_IS_CONTINUATION(*s))
9911 if (! UTF8_IS_START(*s)) {
9912 Perl_croak_nocontext("Malformed UTF-8 character (fatal)");
9915 /* XXX could check well-formedness here */
9922 S_reghop4(U8 *s, SSize_t off, const U8* llim, const U8* rlim)
9924 PERL_ARGS_ASSERT_REGHOP4;
9927 while (off-- && s < rlim) {
9928 /* XXX could check well-formedness here */
9933 while (off++ && s > llim) {
9935 if (UTF8_IS_CONTINUED(*s)) {
9936 while (s > llim && UTF8_IS_CONTINUATION(*s))
9938 if (! UTF8_IS_START(*s)) {
9939 Perl_croak_nocontext("Malformed UTF-8 character (fatal)");
9942 /* XXX could check well-formedness here */
9948 /* like reghop3, but returns NULL on overrun, rather than returning last
9952 S_reghopmaybe3(U8* s, SSize_t off, const U8* const lim)
9954 PERL_ARGS_ASSERT_REGHOPMAYBE3;
9957 while (off-- && s < lim) {
9958 /* XXX could check well-formedness here */
9965 while (off++ && s > lim) {
9967 if (UTF8_IS_CONTINUED(*s)) {
9968 while (s > lim && UTF8_IS_CONTINUATION(*s))
9970 if (! UTF8_IS_START(*s)) {
9971 Perl_croak_nocontext("Malformed UTF-8 character (fatal)");
9974 /* XXX could check well-formedness here */
9983 /* when executing a regex that may have (?{}), extra stuff needs setting
9984 up that will be visible to the called code, even before the current
9985 match has finished. In particular:
9987 * $_ is localised to the SV currently being matched;
9988 * pos($_) is created if necessary, ready to be updated on each call-out
9990 * a fake PMOP is created that can be set to PL_curpm (normally PL_curpm
9991 isn't set until the current pattern is successfully finished), so that
9992 $1 etc of the match-so-far can be seen;
9993 * save the old values of subbeg etc of the current regex, and set then
9994 to the current string (again, this is normally only done at the end
9999 S_setup_eval_state(pTHX_ regmatch_info *const reginfo)
10002 regexp *const rex = ReANY(reginfo->prog);
10003 regmatch_info_aux_eval *eval_state = reginfo->info_aux_eval;
10005 eval_state->rex = rex;
10008 /* Make $_ available to executed code. */
10009 if (reginfo->sv != DEFSV) {
10011 DEFSV_set(reginfo->sv);
10014 if (!(mg = mg_find_mglob(reginfo->sv))) {
10015 /* prepare for quick setting of pos */
10016 mg = sv_magicext_mglob(reginfo->sv);
10019 eval_state->pos_magic = mg;
10020 eval_state->pos = mg->mg_len;
10021 eval_state->pos_flags = mg->mg_flags;
10024 eval_state->pos_magic = NULL;
10026 if (!PL_reg_curpm) {
10027 /* PL_reg_curpm is a fake PMOP that we can attach the current
10028 * regex to and point PL_curpm at, so that $1 et al are visible
10029 * within a /(?{})/. It's just allocated once per interpreter the
10030 * first time its needed */
10031 Newxz(PL_reg_curpm, 1, PMOP);
10032 #ifdef USE_ITHREADS
10034 SV* const repointer = &PL_sv_undef;
10035 /* this regexp is also owned by the new PL_reg_curpm, which
10036 will try to free it. */
10037 av_push(PL_regex_padav, repointer);
10038 PL_reg_curpm->op_pmoffset = av_tindex(PL_regex_padav);
10039 PL_regex_pad = AvARRAY(PL_regex_padav);
10043 SET_reg_curpm(reginfo->prog);
10044 eval_state->curpm = PL_curpm;
10045 PL_curpm_under = PL_curpm;
10046 PL_curpm = PL_reg_curpm;
10047 if (RXp_MATCH_COPIED(rex)) {
10048 /* Here is a serious problem: we cannot rewrite subbeg,
10049 since it may be needed if this match fails. Thus
10050 $` inside (?{}) could fail... */
10051 eval_state->subbeg = rex->subbeg;
10052 eval_state->sublen = rex->sublen;
10053 eval_state->suboffset = rex->suboffset;
10054 eval_state->subcoffset = rex->subcoffset;
10055 #ifdef PERL_ANY_COW
10056 eval_state->saved_copy = rex->saved_copy;
10058 RXp_MATCH_COPIED_off(rex);
10061 eval_state->subbeg = NULL;
10062 rex->subbeg = (char *)reginfo->strbeg;
10063 rex->suboffset = 0;
10064 rex->subcoffset = 0;
10065 rex->sublen = reginfo->strend - reginfo->strbeg;
10069 /* destructor to clear up regmatch_info_aux and regmatch_info_aux_eval */
10072 S_cleanup_regmatch_info_aux(pTHX_ void *arg)
10074 regmatch_info_aux *aux = (regmatch_info_aux *) arg;
10075 regmatch_info_aux_eval *eval_state = aux->info_aux_eval;
10078 Safefree(aux->poscache);
10082 /* undo the effects of S_setup_eval_state() */
10084 if (eval_state->subbeg) {
10085 regexp * const rex = eval_state->rex;
10086 rex->subbeg = eval_state->subbeg;
10087 rex->sublen = eval_state->sublen;
10088 rex->suboffset = eval_state->suboffset;
10089 rex->subcoffset = eval_state->subcoffset;
10090 #ifdef PERL_ANY_COW
10091 rex->saved_copy = eval_state->saved_copy;
10093 RXp_MATCH_COPIED_on(rex);
10095 if (eval_state->pos_magic)
10097 eval_state->pos_magic->mg_len = eval_state->pos;
10098 eval_state->pos_magic->mg_flags =
10099 (eval_state->pos_magic->mg_flags & ~MGf_BYTES)
10100 | (eval_state->pos_flags & MGf_BYTES);
10103 PL_curpm = eval_state->curpm;
10106 PL_regmatch_state = aux->old_regmatch_state;
10107 PL_regmatch_slab = aux->old_regmatch_slab;
10109 /* free all slabs above current one - this must be the last action
10110 * of this function, as aux and eval_state are allocated within
10111 * slabs and may be freed here */
10113 s = PL_regmatch_slab->next;
10115 PL_regmatch_slab->next = NULL;
10117 regmatch_slab * const osl = s;
10126 S_to_utf8_substr(pTHX_ regexp *prog)
10128 /* Converts substr fields in prog from bytes to UTF-8, calling fbm_compile
10129 * on the converted value */
10133 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
10136 if (prog->substrs->data[i].substr
10137 && !prog->substrs->data[i].utf8_substr) {
10138 SV* const sv = newSVsv(prog->substrs->data[i].substr);
10139 prog->substrs->data[i].utf8_substr = sv;
10140 sv_utf8_upgrade(sv);
10141 if (SvVALID(prog->substrs->data[i].substr)) {
10142 if (SvTAIL(prog->substrs->data[i].substr)) {
10143 /* Trim the trailing \n that fbm_compile added last
10145 SvCUR_set(sv, SvCUR(sv) - 1);
10146 /* Whilst this makes the SV technically "invalid" (as its
10147 buffer is no longer followed by "\0") when fbm_compile()
10148 adds the "\n" back, a "\0" is restored. */
10149 fbm_compile(sv, FBMcf_TAIL);
10151 fbm_compile(sv, 0);
10153 if (prog->substrs->data[i].substr == prog->check_substr)
10154 prog->check_utf8 = sv;
10160 S_to_byte_substr(pTHX_ regexp *prog)
10162 /* Converts substr fields in prog from UTF-8 to bytes, calling fbm_compile
10163 * on the converted value; returns FALSE if can't be converted. */
10167 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
10170 if (prog->substrs->data[i].utf8_substr
10171 && !prog->substrs->data[i].substr) {
10172 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
10173 if (! sv_utf8_downgrade(sv, TRUE)) {
10176 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
10177 if (SvTAIL(prog->substrs->data[i].utf8_substr)) {
10178 /* Trim the trailing \n that fbm_compile added last
10180 SvCUR_set(sv, SvCUR(sv) - 1);
10181 fbm_compile(sv, FBMcf_TAIL);
10183 fbm_compile(sv, 0);
10185 prog->substrs->data[i].substr = sv;
10186 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
10187 prog->check_substr = sv;
10194 #ifndef PERL_IN_XSUB_RE
10197 Perl__is_grapheme(pTHX_ const U8 * strbeg, const U8 * s, const U8 * strend, const UV cp)
10199 /* Temporary helper function for toke.c. Verify that the code point 'cp'
10200 * is a stand-alone grapheme. The UTF-8 for 'cp' begins at position 's' in
10201 * the larger string bounded by 'strbeg' and 'strend'.
10203 * 'cp' needs to be assigned (if not a future version of the Unicode
10204 * Standard could make it something that combines with adjacent characters,
10205 * so code using it would then break), and there has to be a GCB break
10206 * before and after the character. */
10208 GCB_enum cp_gcb_val, prev_cp_gcb_val, next_cp_gcb_val;
10209 const U8 * prev_cp_start;
10211 PERL_ARGS_ASSERT__IS_GRAPHEME;
10213 if ( UNLIKELY(UNICODE_IS_SUPER(cp))
10214 || UNLIKELY(UNICODE_IS_NONCHAR(cp)))
10216 /* These are considered graphemes */
10220 /* Otherwise, unassigned code points are forbidden */
10221 if (UNLIKELY(! ELEMENT_RANGE_MATCHES_INVLIST(
10222 _invlist_search(PL_Assigned_invlist, cp))))
10227 cp_gcb_val = getGCB_VAL_CP(cp);
10229 /* Find the GCB value of the previous code point in the input */
10230 prev_cp_start = utf8_hop_back(s, -1, strbeg);
10231 if (UNLIKELY(prev_cp_start == s)) {
10232 prev_cp_gcb_val = GCB_EDGE;
10235 prev_cp_gcb_val = getGCB_VAL_UTF8(prev_cp_start, strend);
10238 /* And check that is a grapheme boundary */
10239 if (! isGCB(prev_cp_gcb_val, cp_gcb_val, strbeg, s,
10240 TRUE /* is UTF-8 encoded */ ))
10245 /* Similarly verify there is a break between the current character and the
10249 next_cp_gcb_val = GCB_EDGE;
10252 next_cp_gcb_val = getGCB_VAL_UTF8(s, strend);
10255 return isGCB(cp_gcb_val, next_cp_gcb_val, strbeg, s, TRUE);
10259 =head1 Unicode Support
10261 =for apidoc isSCRIPT_RUN
10263 Returns a bool as to whether or not the sequence of bytes from C<s> up to but
10264 not including C<send> form a "script run". C<utf8_target> is TRUE iff the
10265 sequence starting at C<s> is to be treated as UTF-8. To be precise, except for
10266 two degenerate cases given below, this function returns TRUE iff all code
10267 points in it come from any combination of three "scripts" given by the Unicode
10268 "Script Extensions" property: Common, Inherited, and possibly one other.
10269 Additionally all decimal digits must come from the same consecutive sequence of
10272 For example, if all the characters in the sequence are Greek, or Common, or
10273 Inherited, this function will return TRUE, provided any decimal digits in it
10274 are the ASCII digits "0".."9". For scripts (unlike Greek) that have their own
10275 digits defined this will accept either digits from that set or from 0..9, but
10276 not a combination of the two. Some scripts, such as Arabic, have more than one
10277 set of digits. All digits must come from the same set for this function to
10280 C<*ret_script>, if C<ret_script> is not NULL, will on return of TRUE
10281 contain the script found, using the C<SCX_enum> typedef. Its value will be
10282 C<SCX_INVALID> if the function returns FALSE.
10284 If the sequence is empty, TRUE is returned, but C<*ret_script> (if asked for)
10285 will be C<SCX_INVALID>.
10287 If the sequence contains a single code point which is unassigned to a character
10288 in the version of Unicode being used, the function will return TRUE, and the
10289 script will be C<SCX_Unknown>. Any other combination of unassigned code points
10290 in the input sequence will result in the function treating the input as not
10291 being a script run.
10293 The returned script will be C<SCX_Inherited> iff all the code points in it are
10294 from the Inherited script.
10296 Otherwise, the returned script will be C<SCX_Common> iff all the code points in
10297 it are from the Inherited or Common scripts.
10304 Perl_isSCRIPT_RUN(pTHX_ const U8 * s, const U8 * send, const bool utf8_target)
10306 /* Basically, it looks at each character in the sequence to see if the
10307 * above conditions are met; if not it fails. It uses an inversion map to
10308 * find the enum corresponding to the script of each character. But this
10309 * is complicated by the fact that a few code points can be in any of
10310 * several scripts. The data has been constructed so that there are
10311 * additional enum values (all negative) for these situations. The
10312 * absolute value of those is an index into another table which contains
10313 * pointers to auxiliary tables for each such situation. Each aux array
10314 * lists all the scripts for the given situation. There is another,
10315 * parallel, table that gives the number of entries in each aux table.
10316 * These are all defined in charclass_invlists.h */
10318 /* XXX Here are the additional things UTS 39 says could be done:
10320 * Forbid sequences of the same nonspacing mark
10322 * Check to see that all the characters are in the sets of exemplar
10323 * characters for at least one language in the Unicode Common Locale Data
10324 * Repository [CLDR]. */
10327 /* Things that match /\d/u */
10328 SV * decimals_invlist = PL_XPosix_ptrs[_CC_DIGIT];
10329 UV * decimals_array = invlist_array(decimals_invlist);
10331 /* What code point is the digit '0' of the script run? (0 meaning FALSE if
10332 * not currently known) */
10333 UV zero_of_run = 0;
10335 SCX_enum script_of_run = SCX_INVALID; /* Illegal value */
10336 SCX_enum script_of_char = SCX_INVALID;
10338 /* If the script remains not fully determined from iteration to iteration,
10339 * this is the current intersection of the possiblities. */
10340 SCX_enum * intersection = NULL;
10341 PERL_UINT_FAST8_T intersection_len = 0;
10343 bool retval = TRUE;
10344 SCX_enum * ret_script = NULL;
10348 PERL_ARGS_ASSERT_ISSCRIPT_RUN;
10350 /* All code points in 0..255 are either Common or Latin, so must be a
10351 * script run. We can return immediately unless we need to know which
10353 if (! utf8_target && LIKELY(send > s)) {
10354 if (ret_script == NULL) {
10358 /* If any character is Latin, the run is Latin */
10360 if (isALPHA_L1(*s) && LIKELY(*s != MICRO_SIGN_NATIVE)) {
10361 *ret_script = SCX_Latin;
10366 /* Here, all are Common */
10367 *ret_script = SCX_Common;
10371 /* Look at each character in the sequence */
10373 /* If the current character being examined is a digit, this is the code
10374 * point of the zero for its sequence of 10 */
10379 /* The code allows all scripts to use the ASCII digits. This is
10380 * because they are used in commerce even in scripts that have their
10381 * own set. Hence any ASCII ones found are ok, unless and until a
10382 * digit from another set has already been encountered. (The other
10383 * digit ranges in Common are not similarly blessed) */
10384 if (UNLIKELY(isDIGIT(*s))) {
10385 if (UNLIKELY(script_of_run == SCX_Unknown)) {
10390 if (zero_of_run != '0') {
10402 /* Here, isn't an ASCII digit. Find the code point of the character */
10403 if (! UTF8_IS_INVARIANT(*s)) {
10405 cp = valid_utf8_to_uvchr((U8 *) s, &len);
10412 /* If is within the range [+0 .. +9] of the script's zero, it also is a
10413 * digit in that script. We can skip the rest of this code for this
10415 if (UNLIKELY( zero_of_run
10416 && cp >= zero_of_run
10417 && cp - zero_of_run <= 9))
10422 /* Find the character's script. The correct values are hard-coded here
10423 * for small-enough code points. */
10424 if (cp < 0x2B9) { /* From inspection of Unicode db; extremely
10425 unlikely to change */
10427 || ( isALPHA_L1(cp)
10428 && LIKELY(cp != MICRO_SIGN_NATIVE)))
10430 script_of_char = SCX_Latin;
10433 script_of_char = SCX_Common;
10437 script_of_char = _Perl_SCX_invmap[
10438 _invlist_search(PL_SCX_invlist, cp)];
10441 /* We arbitrarily accept a single unassigned character, but not in
10442 * combination with anything else, and not a run of them. */
10443 if ( UNLIKELY(script_of_run == SCX_Unknown)
10444 || UNLIKELY( script_of_run != SCX_INVALID
10445 && script_of_char == SCX_Unknown))
10451 /* For the first character, or the run is inherited, the run's script
10452 * is set to the char's */
10453 if ( UNLIKELY(script_of_run == SCX_INVALID)
10454 || UNLIKELY(script_of_run == SCX_Inherited))
10456 script_of_run = script_of_char;
10459 /* For the character's script to be Unknown, it must be the first
10460 * character in the sequence (for otherwise a test above would have
10461 * prevented us from reaching here), and we have set the run's script
10462 * to it. Nothing further to be done for this character */
10463 if (UNLIKELY(script_of_char == SCX_Unknown)) {
10467 /* We accept 'inherited' script characters currently even at the
10468 * beginning. (We know that no characters in Inherited are digits, or
10469 * we'd have to check for that) */
10470 if (UNLIKELY(script_of_char == SCX_Inherited)) {
10474 /* If the run so far is Common, and the new character isn't, change the
10475 * run's script to that of this character */
10476 if (script_of_run == SCX_Common && script_of_char != SCX_Common) {
10478 /* But Common contains several sets of digits. Only the '0' set
10479 * can be part of another script. */
10480 if (zero_of_run && zero_of_run != '0') {
10485 script_of_run = script_of_char;
10488 /* Now we can see if the script of the character is the same as that of
10490 if (LIKELY(script_of_char == script_of_run)) {
10491 /* By far the most common case */
10492 goto scripts_match;
10495 /* Here, the script of the run isn't Common. But characters in Common
10496 * match any script */
10497 if (script_of_char == SCX_Common) {
10498 goto scripts_match;
10501 #ifndef HAS_SCX_AUX_TABLES
10503 /* Too early a Unicode version to have a code point belonging to more
10504 * than one script, so, if the scripts don't exactly match, fail */
10505 PERL_UNUSED_VAR(intersection_len);
10511 /* Here there is no exact match between the character's script and the
10512 * run's. And we've handled the special cases of scripts Unknown,
10513 * Inherited, and Common.
10515 * Negative script numbers signify that the value may be any of several
10516 * scripts, and we need to look at auxiliary information to make our
10517 * deterimination. But if both are non-negative, we can fail now */
10518 if (LIKELY(script_of_char >= 0)) {
10519 const SCX_enum * search_in;
10520 PERL_UINT_FAST8_T search_in_len;
10521 PERL_UINT_FAST8_T i;
10523 if (LIKELY(script_of_run >= 0)) {
10528 /* Use the previously constructed set of possible scripts, if any.
10530 if (intersection) {
10531 search_in = intersection;
10532 search_in_len = intersection_len;
10535 search_in = SCX_AUX_TABLE_ptrs[-script_of_run];
10536 search_in_len = SCX_AUX_TABLE_lengths[-script_of_run];
10539 for (i = 0; i < search_in_len; i++) {
10540 if (search_in[i] == script_of_char) {
10541 script_of_run = script_of_char;
10542 goto scripts_match;
10549 else if (LIKELY(script_of_run >= 0)) {
10550 /* script of character could be one of several, but run is a single
10552 const SCX_enum * search_in = SCX_AUX_TABLE_ptrs[-script_of_char];
10553 const PERL_UINT_FAST8_T search_in_len
10554 = SCX_AUX_TABLE_lengths[-script_of_char];
10555 PERL_UINT_FAST8_T i;
10557 for (i = 0; i < search_in_len; i++) {
10558 if (search_in[i] == script_of_run) {
10559 script_of_char = script_of_run;
10560 goto scripts_match;
10568 /* Both run and char could be in one of several scripts. If the
10569 * intersection is empty, then this character isn't in this script
10570 * run. Otherwise, we need to calculate the intersection to use
10571 * for future iterations of the loop, unless we are already at the
10572 * final character */
10573 const SCX_enum * search_char = SCX_AUX_TABLE_ptrs[-script_of_char];
10574 const PERL_UINT_FAST8_T char_len
10575 = SCX_AUX_TABLE_lengths[-script_of_char];
10576 const SCX_enum * search_run;
10577 PERL_UINT_FAST8_T run_len;
10579 SCX_enum * new_overlap = NULL;
10580 PERL_UINT_FAST8_T i, j;
10582 if (intersection) {
10583 search_run = intersection;
10584 run_len = intersection_len;
10587 search_run = SCX_AUX_TABLE_ptrs[-script_of_run];
10588 run_len = SCX_AUX_TABLE_lengths[-script_of_run];
10591 intersection_len = 0;
10593 for (i = 0; i < run_len; i++) {
10594 for (j = 0; j < char_len; j++) {
10595 if (search_run[i] == search_char[j]) {
10597 /* Here, the script at i,j matches. That means this
10598 * character is in the run. But continue on to find
10599 * the complete intersection, for the next loop
10600 * iteration, and for the digit check after it.
10602 * On the first found common script, we malloc space
10603 * for the intersection list for the worst case of the
10604 * intersection, which is the minimum of the number of
10605 * scripts remaining in each set. */
10606 if (intersection_len == 0) {
10608 MIN(run_len - i, char_len - j),
10611 new_overlap[intersection_len++] = search_run[i];
10616 /* Here we've looked through everything. If they have no scripts
10617 * in common, not a run */
10618 if (intersection_len == 0) {
10623 /* If there is only a single script in common, set to that.
10624 * Otherwise, use the intersection going forward */
10625 Safefree(intersection);
10626 intersection = NULL;
10627 if (intersection_len == 1) {
10628 script_of_run = script_of_char = new_overlap[0];
10629 Safefree(new_overlap);
10630 new_overlap = NULL;
10633 intersection = new_overlap;
10641 /* Here, the script of the character is compatible with that of the
10642 * run. That means that in most cases, it continues the script run.
10643 * Either it and the run match exactly, or one or both can be in any of
10644 * several scripts, and the intersection is not empty. However, if the
10645 * character is a decimal digit, it could still mean failure if it is
10646 * from the wrong sequence of 10. So, we need to look at if it's a
10647 * digit. We've already handled the 10 decimal digits, and the next
10648 * lowest one is this one: */
10649 if (cp < FIRST_NON_ASCII_DECIMAL_DIGIT) {
10650 continue; /* Not a digit; this character is part of the run */
10653 /* If we have a definitive '0' for the script of this character, we
10654 * know that for this to be a digit, it must be in the range of +0..+9
10656 if ( script_of_char >= 0
10657 && (zero_of_char = script_zeros[script_of_char]))
10659 if ( cp < zero_of_char
10660 || cp > zero_of_char + 9)
10662 continue; /* Not a digit; this character is part of the run
10667 else { /* Need to look up if this character is a digit or not */
10668 SSize_t index_of_zero_of_char;
10669 index_of_zero_of_char = _invlist_search(decimals_invlist, cp);
10670 if ( UNLIKELY(index_of_zero_of_char < 0)
10671 || ! ELEMENT_RANGE_MATCHES_INVLIST(index_of_zero_of_char))
10673 continue; /* Not a digit; this character is part of the run.
10677 zero_of_char = decimals_array[index_of_zero_of_char];
10680 /* Here, the character is a decimal digit, and the zero of its sequence
10681 * of 10 is in 'zero_of_char'. If we already have a zero for this run,
10682 * they better be the same. */
10684 if (zero_of_run != zero_of_char) {
10689 else if (script_of_char == SCX_Common && script_of_run != SCX_Common) {
10691 /* Here, the script run isn't Common, but the current digit is in
10692 * Common, and isn't '0'-'9' (those were handled earlier). Only
10693 * '0'-'9' are acceptable in non-Common scripts. */
10697 else { /* Otherwise we now have a zero for this run */
10698 zero_of_run = zero_of_char;
10700 } /* end of looping through CLOSESR text */
10702 Safefree(intersection);
10704 if (ret_script != NULL) {
10706 *ret_script = script_of_run;
10709 *ret_script = SCX_INVALID;
10716 #endif /* ifndef PERL_IN_XSUB_RE */
10719 * ex: set ts=8 sts=4 sw=4 et: