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 #define RF_tainted 1 /* tainted information used? */
84 #define RF_warned 2 /* warned about big count? */
86 #define RF_utf8 8 /* Pattern contains multibyte chars? */
88 #define UTF ((PL_reg_flags & RF_utf8) != 0)
90 #define RS_init 1 /* eval environment created */
91 #define RS_set 2 /* replsv value is set */
97 #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
103 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
104 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
106 #define HOPc(pos,off) \
107 (char *)(PL_reg_match_utf8 \
108 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
110 #define HOPBACKc(pos, off) \
111 (char*)(PL_reg_match_utf8\
112 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
113 : (pos - off >= PL_bostr) \
117 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
118 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
120 /* these are unrolled below in the CCC_TRY_XXX defined */
121 #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
122 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
124 /* Doesn't do an assert to verify that is correct */
125 #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
126 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END
128 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
129 #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
130 #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
132 #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
133 LOAD_UTF8_CHARCLASS(X_begin, " "); \
134 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
135 /* These are utf8 constants, and not utf-ebcdic constants, so the \
136 * assert should likely and hopefully fail on an EBCDIC machine */ \
137 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
139 /* No asserts are done for these, in case called on an early \
140 * Unicode version in which they map to nothing */ \
141 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
142 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
143 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
144 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
145 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
146 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
147 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
150 We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test
151 so that it is possible to override the option here without having to
152 rebuild the entire core. as we are required to do if we change regcomp.h
153 which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined.
155 #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS
156 #define BROKEN_UNICODE_CHARCLASS_MAPPINGS
159 #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS
160 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM()
161 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE()
162 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT()
163 #define RE_utf8_perl_word PL_utf8_alnum
164 #define RE_utf8_perl_space PL_utf8_space
165 #define RE_utf8_posix_digit PL_utf8_digit
166 #define perl_word alnum
167 #define perl_space space
168 #define posix_digit digit
170 #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a")
171 #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ")
172 #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0")
173 #define RE_utf8_perl_word PL_utf8_perl_word
174 #define RE_utf8_perl_space PL_utf8_perl_space
175 #define RE_utf8_posix_digit PL_utf8_posix_digit
179 #define CCC_TRY_AFF(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
181 PL_reg_flags |= RF_tainted; \
186 if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \
187 if (!CAT2(PL_utf8_,CLASS)) { \
191 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
195 if (!(OP(scan) == NAME \
196 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \
197 : LCFUNC_utf8((U8*)locinput))) \
201 locinput += PL_utf8skip[nextchr]; \
202 nextchr = UCHARAT(locinput); \
205 if (!(OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
207 nextchr = UCHARAT(++locinput); \
210 #define CCC_TRY_NEG(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
212 PL_reg_flags |= RF_tainted; \
215 if (!nextchr && locinput >= PL_regeol) \
217 if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \
218 if (!CAT2(PL_utf8_,CLASS)) { \
222 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
226 if ((OP(scan) == NAME \
227 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \
228 : LCFUNC_utf8((U8*)locinput))) \
232 locinput += PL_utf8skip[nextchr]; \
233 nextchr = UCHARAT(locinput); \
236 if ((OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
238 nextchr = UCHARAT(++locinput); \
245 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
247 /* for use after a quantifier and before an EXACT-like node -- japhy */
248 /* it would be nice to rework regcomp.sym to generate this stuff. sigh */
249 #define JUMPABLE(rn) ( \
251 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
253 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
254 OP(rn) == PLUS || OP(rn) == MINMOD || \
255 OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \
256 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
258 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
260 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
263 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
264 we don't need this definition. */
265 #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
266 #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
267 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
270 /* ... so we use this as its faster. */
271 #define IS_TEXT(rn) ( OP(rn)==EXACT )
272 #define IS_TEXTF(rn) ( OP(rn)==EXACTF )
273 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
278 Search for mandatory following text node; for lookahead, the text must
279 follow but for lookbehind (rn->flags != 0) we skip to the next step.
281 #define FIND_NEXT_IMPT(rn) STMT_START { \
282 while (JUMPABLE(rn)) { \
283 const OPCODE type = OP(rn); \
284 if (type == SUSPEND || PL_regkind[type] == CURLY) \
285 rn = NEXTOPER(NEXTOPER(rn)); \
286 else if (type == PLUS) \
288 else if (type == IFMATCH) \
289 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
290 else rn += NEXT_OFF(rn); \
295 static void restore_pos(pTHX_ void *arg);
297 #define REGCP_PAREN_ELEMS 4
298 #define REGCP_OTHER_ELEMS 5
299 #define REGCP_FRAME_ELEMS 1
300 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
301 * are needed for the regexp context stack bookkeeping. */
304 S_regcppush(pTHX_ I32 parenfloor)
307 const int retval = PL_savestack_ix;
308 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
309 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
310 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
312 GET_RE_DEBUG_FLAGS_DECL;
314 if (paren_elems_to_push < 0)
315 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
317 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
318 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
319 " out of range (%lu-%ld)",
320 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
322 SSGROW(total_elems + REGCP_FRAME_ELEMS);
324 for (p = PL_regsize; p > parenfloor; p--) {
325 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
326 SSPUSHINT(PL_regoffs[p].end);
327 SSPUSHINT(PL_regoffs[p].start);
328 SSPUSHPTR(PL_reg_start_tmp[p]);
330 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
331 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
332 (UV)p, (IV)PL_regoffs[p].start,
333 (IV)(PL_reg_start_tmp[p] - PL_bostr),
334 (IV)PL_regoffs[p].end
337 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
338 SSPUSHPTR(PL_regoffs);
339 SSPUSHINT(PL_regsize);
340 SSPUSHINT(*PL_reglastparen);
341 SSPUSHINT(*PL_reglastcloseparen);
342 SSPUSHPTR(PL_reginput);
343 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
348 /* These are needed since we do not localize EVAL nodes: */
349 #define REGCP_SET(cp) \
351 PerlIO_printf(Perl_debug_log, \
352 " Setting an EVAL scope, savestack=%"IVdf"\n", \
353 (IV)PL_savestack_ix)); \
356 #define REGCP_UNWIND(cp) \
358 if (cp != PL_savestack_ix) \
359 PerlIO_printf(Perl_debug_log, \
360 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
361 (IV)(cp), (IV)PL_savestack_ix)); \
365 S_regcppop(pTHX_ const regexp *rex)
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 input = (char *) SSPOPPTR;
379 *PL_reglastcloseparen = SSPOPINT;
380 *PL_reglastparen = SSPOPINT;
381 PL_regsize = SSPOPINT;
382 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
384 i -= REGCP_OTHER_ELEMS;
385 /* Now restore the parentheses context. */
386 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
388 U32 paren = (U32)SSPOPINT;
389 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
390 PL_regoffs[paren].start = SSPOPINT;
392 if (paren <= *PL_reglastparen)
393 PL_regoffs[paren].end = tmps;
395 PerlIO_printf(Perl_debug_log,
396 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
397 (UV)paren, (IV)PL_regoffs[paren].start,
398 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
399 (IV)PL_regoffs[paren].end,
400 (paren > *PL_reglastparen ? "(no)" : ""));
404 if (*PL_reglastparen + 1 <= rex->nparens) {
405 PerlIO_printf(Perl_debug_log,
406 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
407 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
411 /* It would seem that the similar code in regtry()
412 * already takes care of this, and in fact it is in
413 * a better location to since this code can #if 0-ed out
414 * but the code in regtry() is needed or otherwise tests
415 * requiring null fields (pat.t#187 and split.t#{13,14}
416 * (as of patchlevel 7877) will fail. Then again,
417 * this code seems to be necessary or otherwise
418 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
419 * --jhi updated by dapm */
420 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
422 PL_regoffs[i].start = -1;
423 PL_regoffs[i].end = -1;
429 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
432 * pregexec and friends
435 #ifndef PERL_IN_XSUB_RE
437 - pregexec - match a regexp against a string
440 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
441 char *strbeg, I32 minend, SV *screamer, U32 nosave)
442 /* strend: pointer to null at end of string */
443 /* strbeg: real beginning of string */
444 /* minend: end of match must be >=minend after stringarg. */
445 /* nosave: For optimizations. */
447 PERL_ARGS_ASSERT_PREGEXEC;
450 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
451 nosave ? 0 : REXEC_COPY_STR);
456 * Need to implement the following flags for reg_anch:
458 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
460 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
461 * INTUIT_AUTORITATIVE_ML
462 * INTUIT_ONCE_NOML - Intuit can match in one location only.
465 * Another flag for this function: SECOND_TIME (so that float substrs
466 * with giant delta may be not rechecked).
469 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
471 /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
472 Otherwise, only SvCUR(sv) is used to get strbeg. */
474 /* XXXX We assume that strpos is strbeg unless sv. */
476 /* XXXX Some places assume that there is a fixed substring.
477 An update may be needed if optimizer marks as "INTUITable"
478 RExen without fixed substrings. Similarly, it is assumed that
479 lengths of all the strings are no more than minlen, thus they
480 cannot come from lookahead.
481 (Or minlen should take into account lookahead.)
482 NOTE: Some of this comment is not correct. minlen does now take account
483 of lookahead/behind. Further research is required. -- demerphq
487 /* A failure to find a constant substring means that there is no need to make
488 an expensive call to REx engine, thus we celebrate a failure. Similarly,
489 finding a substring too deep into the string means that less calls to
490 regtry() should be needed.
492 REx compiler's optimizer found 4 possible hints:
493 a) Anchored substring;
495 c) Whether we are anchored (beginning-of-line or \G);
496 d) First node (of those at offset 0) which may distingush positions;
497 We use a)b)d) and multiline-part of c), and try to find a position in the
498 string which does not contradict any of them.
501 /* Most of decisions we do here should have been done at compile time.
502 The nodes of the REx which we used for the search should have been
503 deleted from the finite automaton. */
506 Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
507 char *strend, const U32 flags, re_scream_pos_data *data)
510 struct regexp *const prog = (struct regexp *)SvANY(rx);
511 register I32 start_shift = 0;
512 /* Should be nonnegative! */
513 register I32 end_shift = 0;
518 const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
520 register char *other_last = NULL; /* other substr checked before this */
521 char *check_at = NULL; /* check substr found at this pos */
522 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
523 RXi_GET_DECL(prog,progi);
525 const char * const i_strpos = strpos;
527 GET_RE_DEBUG_FLAGS_DECL;
529 PERL_ARGS_ASSERT_RE_INTUIT_START;
531 RX_MATCH_UTF8_set(rx,do_utf8);
534 PL_reg_flags |= RF_utf8;
537 debug_start_match(rx, do_utf8, strpos, strend,
538 sv ? "Guessing start of match in sv for"
539 : "Guessing start of match in string for");
542 /* CHR_DIST() would be more correct here but it makes things slow. */
543 if (prog->minlen > strend - strpos) {
544 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
545 "String too short... [re_intuit_start]\n"));
549 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
552 if (!prog->check_utf8 && prog->check_substr)
553 to_utf8_substr(prog);
554 check = prog->check_utf8;
556 if (!prog->check_substr && prog->check_utf8)
557 to_byte_substr(prog);
558 check = prog->check_substr;
560 if (check == &PL_sv_undef) {
561 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
562 "Non-utf8 string cannot match utf8 check string\n"));
565 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
566 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
567 || ( (prog->extflags & RXf_ANCH_BOL)
568 && !multiline ) ); /* Check after \n? */
571 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
572 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
573 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
575 && (strpos != strbeg)) {
576 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
579 if (prog->check_offset_min == prog->check_offset_max &&
580 !(prog->extflags & RXf_CANY_SEEN)) {
581 /* Substring at constant offset from beg-of-str... */
584 s = HOP3c(strpos, prog->check_offset_min, strend);
587 slen = SvCUR(check); /* >= 1 */
589 if ( strend - s > slen || strend - s < slen - 1
590 || (strend - s == slen && strend[-1] != '\n')) {
591 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
594 /* Now should match s[0..slen-2] */
596 if (slen && (*SvPVX_const(check) != *s
598 && memNE(SvPVX_const(check), s, slen)))) {
600 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
604 else if (*SvPVX_const(check) != *s
605 || ((slen = SvCUR(check)) > 1
606 && memNE(SvPVX_const(check), s, slen)))
609 goto success_at_start;
612 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
614 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
615 end_shift = prog->check_end_shift;
618 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
619 - (SvTAIL(check) != 0);
620 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
622 if (end_shift < eshift)
626 else { /* Can match at random position */
629 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
630 end_shift = prog->check_end_shift;
632 /* end shift should be non negative here */
635 #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
637 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
638 (IV)end_shift, RX_PRECOMP(prog));
642 /* Find a possible match in the region s..strend by looking for
643 the "check" substring in the region corrected by start/end_shift. */
646 I32 srch_start_shift = start_shift;
647 I32 srch_end_shift = end_shift;
648 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
649 srch_end_shift -= ((strbeg - s) - srch_start_shift);
650 srch_start_shift = strbeg - s;
652 DEBUG_OPTIMISE_MORE_r({
653 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
654 (IV)prog->check_offset_min,
655 (IV)srch_start_shift,
657 (IV)prog->check_end_shift);
660 if (flags & REXEC_SCREAM) {
661 I32 p = -1; /* Internal iterator of scream. */
662 I32 * const pp = data ? data->scream_pos : &p;
664 if (PL_screamfirst[BmRARE(check)] >= 0
665 || ( BmRARE(check) == '\n'
666 && (BmPREVIOUS(check) == SvCUR(check) - 1)
668 s = screaminstr(sv, check,
669 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
672 /* we may be pointing at the wrong string */
673 if (s && RXp_MATCH_COPIED(prog))
674 s = strbeg + (s - SvPVX_const(sv));
676 *data->scream_olds = s;
681 if (prog->extflags & RXf_CANY_SEEN) {
682 start_point= (U8*)(s + srch_start_shift);
683 end_point= (U8*)(strend - srch_end_shift);
685 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
686 end_point= HOP3(strend, -srch_end_shift, strbeg);
688 DEBUG_OPTIMISE_MORE_r({
689 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
690 (int)(end_point - start_point),
691 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
695 s = fbm_instr( start_point, end_point,
696 check, multiline ? FBMrf_MULTILINE : 0);
699 /* Update the count-of-usability, remove useless subpatterns,
703 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
704 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
705 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
706 (s ? "Found" : "Did not find"),
707 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)
708 ? "anchored" : "floating"),
711 (s ? " at offset " : "...\n") );
716 /* Finish the diagnostic message */
717 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
719 /* XXX dmq: first branch is for positive lookbehind...
720 Our check string is offset from the beginning of the pattern.
721 So we need to do any stclass tests offset forward from that
730 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
731 Start with the other substr.
732 XXXX no SCREAM optimization yet - and a very coarse implementation
733 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
734 *always* match. Probably should be marked during compile...
735 Probably it is right to do no SCREAM here...
738 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8)
739 : (prog->float_substr && prog->anchored_substr))
741 /* Take into account the "other" substring. */
742 /* XXXX May be hopelessly wrong for UTF... */
745 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
748 char * const last = HOP3c(s, -start_shift, strbeg);
750 char * const saved_s = s;
753 t = s - prog->check_offset_max;
754 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
756 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
761 t = HOP3c(t, prog->anchored_offset, strend);
762 if (t < other_last) /* These positions already checked */
764 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
767 /* XXXX It is not documented what units *_offsets are in.
768 We assume bytes, but this is clearly wrong.
769 Meaning this code needs to be carefully reviewed for errors.
773 /* On end-of-str: see comment below. */
774 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
775 if (must == &PL_sv_undef) {
777 DEBUG_r(must = prog->anchored_utf8); /* for debug */
782 HOP3(HOP3(last1, prog->anchored_offset, strend)
783 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
785 multiline ? FBMrf_MULTILINE : 0
788 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
789 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
790 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
791 (s ? "Found" : "Contradicts"),
792 quoted, RE_SV_TAIL(must));
797 if (last1 >= last2) {
798 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
799 ", giving up...\n"));
802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
803 ", trying floating at offset %ld...\n",
804 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
805 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
806 s = HOP3c(last, 1, strend);
810 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
811 (long)(s - i_strpos)));
812 t = HOP3c(s, -prog->anchored_offset, strbeg);
813 other_last = HOP3c(s, 1, strend);
821 else { /* Take into account the floating substring. */
823 char * const saved_s = s;
826 t = HOP3c(s, -start_shift, strbeg);
828 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
829 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
830 last = HOP3c(t, prog->float_max_offset, strend);
831 s = HOP3c(t, prog->float_min_offset, strend);
834 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
835 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
836 /* fbm_instr() takes into account exact value of end-of-str
837 if the check is SvTAIL(ed). Since false positives are OK,
838 and end-of-str is not later than strend we are OK. */
839 if (must == &PL_sv_undef) {
841 DEBUG_r(must = prog->float_utf8); /* for debug message */
844 s = fbm_instr((unsigned char*)s,
845 (unsigned char*)last + SvCUR(must)
847 must, multiline ? FBMrf_MULTILINE : 0);
849 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
850 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
851 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
852 (s ? "Found" : "Contradicts"),
853 quoted, RE_SV_TAIL(must));
857 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
858 ", giving up...\n"));
861 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
862 ", trying anchored starting at offset %ld...\n",
863 (long)(saved_s + 1 - i_strpos)));
865 s = HOP3c(t, 1, strend);
869 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
870 (long)(s - i_strpos)));
871 other_last = s; /* Fix this later. --Hugo */
881 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
883 DEBUG_OPTIMISE_MORE_r(
884 PerlIO_printf(Perl_debug_log,
885 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
886 (IV)prog->check_offset_min,
887 (IV)prog->check_offset_max,
895 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
897 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
900 /* Fixed substring is found far enough so that the match
901 cannot start at strpos. */
903 if (ml_anch && t[-1] != '\n') {
904 /* Eventually fbm_*() should handle this, but often
905 anchored_offset is not 0, so this check will not be wasted. */
906 /* XXXX In the code below we prefer to look for "^" even in
907 presence of anchored substrings. And we search even
908 beyond the found float position. These pessimizations
909 are historical artefacts only. */
911 while (t < strend - prog->minlen) {
913 if (t < check_at - prog->check_offset_min) {
914 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
915 /* Since we moved from the found position,
916 we definitely contradict the found anchored
917 substr. Due to the above check we do not
918 contradict "check" substr.
919 Thus we can arrive here only if check substr
920 is float. Redo checking for "other"=="fixed".
923 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
924 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
925 goto do_other_anchored;
927 /* We don't contradict the found floating substring. */
928 /* XXXX Why not check for STCLASS? */
930 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
931 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
934 /* Position contradicts check-string */
935 /* XXXX probably better to look for check-string
936 than for "\n", so one should lower the limit for t? */
937 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
938 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
939 other_last = strpos = s = t + 1;
944 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
945 PL_colors[0], PL_colors[1]));
949 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
950 PL_colors[0], PL_colors[1]));
954 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
957 /* The found string does not prohibit matching at strpos,
958 - no optimization of calling REx engine can be performed,
959 unless it was an MBOL and we are not after MBOL,
960 or a future STCLASS check will fail this. */
962 /* Even in this situation we may use MBOL flag if strpos is offset
963 wrt the start of the string. */
964 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
965 && (strpos != strbeg) && strpos[-1] != '\n'
966 /* May be due to an implicit anchor of m{.*foo} */
967 && !(prog->intflags & PREGf_IMPLICIT))
972 DEBUG_EXECUTE_r( if (ml_anch)
973 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
974 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
977 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
979 prog->check_utf8 /* Could be deleted already */
980 && --BmUSEFUL(prog->check_utf8) < 0
981 && (prog->check_utf8 == prog->float_utf8)
983 prog->check_substr /* Could be deleted already */
984 && --BmUSEFUL(prog->check_substr) < 0
985 && (prog->check_substr == prog->float_substr)
988 /* If flags & SOMETHING - do not do it many times on the same match */
989 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
990 /* XXX Does the destruction order has to change with do_utf8? */
991 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
992 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
993 prog->check_substr = prog->check_utf8 = NULL; /* disable */
994 prog->float_substr = prog->float_utf8 = NULL; /* clear */
995 check = NULL; /* abort */
997 /* XXXX This is a remnant of the old implementation. It
998 looks wasteful, since now INTUIT can use many
1000 prog->extflags &= ~RXf_USE_INTUIT;
1006 /* Last resort... */
1007 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1008 /* trie stclasses are too expensive to use here, we are better off to
1009 leave it to regmatch itself */
1010 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1011 /* minlen == 0 is possible if regstclass is \b or \B,
1012 and the fixed substr is ''$.
1013 Since minlen is already taken into account, s+1 is before strend;
1014 accidentally, minlen >= 1 guaranties no false positives at s + 1
1015 even for \b or \B. But (minlen? 1 : 0) below assumes that
1016 regstclass does not come from lookahead... */
1017 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1018 This leaves EXACTF only, which is dealt with in find_byclass(). */
1019 const U8* const str = (U8*)STRING(progi->regstclass);
1020 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1021 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
1024 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1025 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1026 else if (prog->float_substr || prog->float_utf8)
1027 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1031 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1032 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1035 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
1038 const char *what = NULL;
1040 if (endpos == strend) {
1041 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1042 "Could not match STCLASS...\n") );
1045 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1046 "This position contradicts STCLASS...\n") );
1047 if ((prog->extflags & RXf_ANCH) && !ml_anch)
1049 /* Contradict one of substrings */
1050 if (prog->anchored_substr || prog->anchored_utf8) {
1051 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
1052 DEBUG_EXECUTE_r( what = "anchored" );
1054 s = HOP3c(t, 1, strend);
1055 if (s + start_shift + end_shift > strend) {
1056 /* XXXX Should be taken into account earlier? */
1057 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1058 "Could not match STCLASS...\n") );
1063 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1064 "Looking for %s substr starting at offset %ld...\n",
1065 what, (long)(s + start_shift - i_strpos)) );
1068 /* Have both, check_string is floating */
1069 if (t + start_shift >= check_at) /* Contradicts floating=check */
1070 goto retry_floating_check;
1071 /* Recheck anchored substring, but not floating... */
1075 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1076 "Looking for anchored substr starting at offset %ld...\n",
1077 (long)(other_last - i_strpos)) );
1078 goto do_other_anchored;
1080 /* Another way we could have checked stclass at the
1081 current position only: */
1086 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1087 "Looking for /%s^%s/m starting at offset %ld...\n",
1088 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
1091 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
1093 /* Check is floating subtring. */
1094 retry_floating_check:
1095 t = check_at - start_shift;
1096 DEBUG_EXECUTE_r( what = "floating" );
1097 goto hop_and_restart;
1100 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1101 "By STCLASS: moving %ld --> %ld\n",
1102 (long)(t - i_strpos), (long)(s - i_strpos))
1106 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1107 "Does not contradict STCLASS...\n");
1112 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
1113 PL_colors[4], (check ? "Guessed" : "Giving up"),
1114 PL_colors[5], (long)(s - i_strpos)) );
1117 fail_finish: /* Substring not found */
1118 if (prog->check_substr || prog->check_utf8) /* could be removed already */
1119 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1121 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1122 PL_colors[4], PL_colors[5]));
1126 #define DECL_TRIE_TYPE(scan) \
1127 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1128 trie_type = (scan->flags != EXACT) \
1129 ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \
1130 : (do_utf8 ? trie_utf8 : trie_plain)
1132 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1133 uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1134 switch (trie_type) { \
1135 case trie_utf8_fold: \
1136 if ( foldlen>0 ) { \
1137 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1142 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1143 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1144 foldlen -= UNISKIP( uvc ); \
1145 uscan = foldbuf + UNISKIP( uvc ); \
1148 case trie_latin_utf8_fold: \
1149 if ( foldlen>0 ) { \
1150 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1156 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1157 foldlen -= UNISKIP( uvc ); \
1158 uscan = foldbuf + UNISKIP( uvc ); \
1162 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1169 charid = trie->charmap[ uvc ]; \
1173 if (widecharmap) { \
1174 SV** const svpp = hv_fetch(widecharmap, \
1175 (char*)&uvc, sizeof(UV), 0); \
1177 charid = (U16)SvIV(*svpp); \
1182 #define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1184 char *my_strend= (char *)strend; \
1187 foldEQ_utf8(s, &my_strend, 0, do_utf8, \
1188 m, NULL, ln, cBOOL(UTF))) \
1189 && (!reginfo || regtry(reginfo, &s)) ) \
1192 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1193 uvchr_to_utf8(tmpbuf, c); \
1194 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1196 && (f == c1 || f == c2) \
1198 foldEQ_utf8(s, &my_strend, 0, do_utf8,\
1199 m, NULL, ln, cBOOL(UTF)))\
1200 && (!reginfo || regtry(reginfo, &s)) ) \
1206 #define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1210 && (ln == 1 || (OP(c) == EXACTF \
1211 ? foldEQ(s, m, ln) \
1212 : foldEQ_locale(s, m, ln))) \
1213 && (!reginfo || regtry(reginfo, &s)) ) \
1219 #define REXEC_FBC_UTF8_SCAN(CoDe) \
1221 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1227 #define REXEC_FBC_SCAN(CoDe) \
1229 while (s < strend) { \
1235 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1236 REXEC_FBC_UTF8_SCAN( \
1238 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1247 #define REXEC_FBC_CLASS_SCAN(CoNd) \
1250 if (tmp && (!reginfo || regtry(reginfo, &s))) \
1259 #define REXEC_FBC_TRYIT \
1260 if ((!reginfo || regtry(reginfo, &s))) \
1263 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
1265 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1268 REXEC_FBC_CLASS_SCAN(CoNd); \
1272 #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1275 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1278 REXEC_FBC_CLASS_SCAN(CoNd); \
1282 #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1283 PL_reg_flags |= RF_tainted; \
1285 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1288 REXEC_FBC_CLASS_SCAN(CoNd); \
1292 #define DUMP_EXEC_POS(li,s,doutf8) \
1293 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1295 /* We know what class REx starts with. Try to find this position... */
1296 /* if reginfo is NULL, its a dryrun */
1297 /* annoyingly all the vars in this routine have different names from their counterparts
1298 in regmatch. /grrr */
1301 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1302 const char *strend, regmatch_info *reginfo)
1305 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1309 register STRLEN uskip;
1313 register I32 tmp = 1; /* Scratch variable? */
1314 register const bool do_utf8 = PL_reg_match_utf8;
1315 RXi_GET_DECL(prog,progi);
1317 PERL_ARGS_ASSERT_FIND_BYCLASS;
1319 /* We know what class it must start with. */
1323 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
1324 !UTF8_IS_INVARIANT((U8)s[0]) ?
1325 reginclass(prog, c, (U8*)s, 0, do_utf8) :
1326 REGINCLASS(prog, c, (U8*)s));
1329 while (s < strend) {
1332 if (REGINCLASS(prog, c, (U8*)s) ||
1333 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1334 /* The assignment of 2 is intentional:
1335 * for the folded sharp s, the skip is 2. */
1336 (skip = SHARP_S_SKIP))) {
1337 if (tmp && (!reginfo || regtry(reginfo, &s)))
1350 if (tmp && (!reginfo || regtry(reginfo, &s)))
1358 ln = STR_LEN(c); /* length to match in octets/bytes */
1359 lnc = (I32) ln; /* length to match in characters */
1361 STRLEN ulen1, ulen2;
1363 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1364 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
1365 /* used by commented-out code below */
1366 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
1368 /* XXX: Since the node will be case folded at compile
1369 time this logic is a little odd, although im not
1370 sure that its actually wrong. --dmq */
1372 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1373 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1375 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1376 codepoint of the first character in the converted
1377 form, yet originally we did the extra step.
1378 No tests fail by commenting this code out however
1379 so Ive left it out. -- dmq.
1381 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
1383 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
1388 while (sm < ((U8 *) m + ln)) {
1403 c2 = PL_fold_locale[c1];
1405 e = HOP3c(strend, -((I32)lnc), s);
1407 if (!reginfo && e < s)
1408 e = s; /* Due to minlen logic of intuit() */
1410 /* The idea in the EXACTF* cases is to first find the
1411 * first character of the EXACTF* node and then, if
1412 * necessary, case-insensitively compare the full
1413 * text of the node. The c1 and c2 are the first
1414 * characters (though in Unicode it gets a bit
1415 * more complicated because there are more cases
1416 * than just upper and lower: one needs to use
1417 * the so-called folding case for case-insensitive
1418 * matching (called "loose matching" in Unicode).
1419 * foldEQ_utf8() will do just that. */
1421 if (do_utf8 || UTF) {
1423 U8 tmpbuf [UTF8_MAXBYTES+1];
1426 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1428 /* Upper and lower of 1st char are equal -
1429 * probably not a "letter". */
1432 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1437 REXEC_FBC_EXACTISH_CHECK(c == c1);
1443 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
1449 /* Handle some of the three Greek sigmas cases.
1450 * Note that not all the possible combinations
1451 * are handled here: some of them are handled
1452 * by the standard folding rules, and some of
1453 * them (the character class or ANYOF cases)
1454 * are handled during compiletime in
1455 * regexec.c:S_regclass(). */
1456 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1457 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1458 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1460 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1465 /* Neither pattern nor string are UTF8 */
1467 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1469 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1473 PL_reg_flags |= RF_tainted;
1480 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1481 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1483 tmp = ((OP(c) == BOUND ?
1484 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1485 LOAD_UTF8_CHARCLASS_ALNUM();
1486 REXEC_FBC_UTF8_SCAN(
1487 if (tmp == !(OP(c) == BOUND ?
1488 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) :
1489 isALNUM_LC_utf8((U8*)s)))
1497 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1498 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1501 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1507 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
1511 PL_reg_flags |= RF_tainted;
1518 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1519 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
1521 tmp = ((OP(c) == NBOUND ?
1522 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1523 LOAD_UTF8_CHARCLASS_ALNUM();
1524 REXEC_FBC_UTF8_SCAN(
1525 if (tmp == !(OP(c) == NBOUND ?
1526 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) :
1527 isALNUM_LC_utf8((U8*)s)))
1529 else REXEC_FBC_TRYIT;
1533 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1534 tmp = ((OP(c) == NBOUND ?
1535 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1538 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1540 else REXEC_FBC_TRYIT;
1543 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
1547 REXEC_FBC_CSCAN_PRELOAD(
1548 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1549 swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1553 REXEC_FBC_CSCAN_TAINT(
1554 isALNUM_LC_utf8((U8*)s),
1558 REXEC_FBC_CSCAN_PRELOAD(
1559 LOAD_UTF8_CHARCLASS_PERL_WORD(),
1560 !swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8),
1564 REXEC_FBC_CSCAN_TAINT(
1565 !isALNUM_LC_utf8((U8*)s),
1569 REXEC_FBC_CSCAN_PRELOAD(
1570 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1571 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8),
1575 REXEC_FBC_CSCAN_TAINT(
1576 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1580 REXEC_FBC_CSCAN_PRELOAD(
1581 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
1582 !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8)),
1586 REXEC_FBC_CSCAN_TAINT(
1587 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1591 REXEC_FBC_CSCAN_PRELOAD(
1592 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1593 swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1597 REXEC_FBC_CSCAN_TAINT(
1598 isDIGIT_LC_utf8((U8*)s),
1602 REXEC_FBC_CSCAN_PRELOAD(
1603 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
1604 !swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8),
1608 REXEC_FBC_CSCAN_TAINT(
1609 !isDIGIT_LC_utf8((U8*)s),
1615 is_LNBREAK_latin1(s)
1625 !is_VERTWS_latin1(s)
1630 is_HORIZWS_latin1(s)
1634 !is_HORIZWS_utf8(s),
1635 !is_HORIZWS_latin1(s)
1641 /* what trie are we using right now */
1643 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
1645 = (reg_trie_data*)progi->data->data[ aho->trie ];
1646 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
1648 const char *last_start = strend - trie->minlen;
1650 const char *real_start = s;
1652 STRLEN maxlen = trie->maxlen;
1654 U8 **points; /* map of where we were in the input string
1655 when reading a given char. For ASCII this
1656 is unnecessary overhead as the relationship
1657 is always 1:1, but for Unicode, especially
1658 case folded Unicode this is not true. */
1659 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1663 GET_RE_DEBUG_FLAGS_DECL;
1665 /* We can't just allocate points here. We need to wrap it in
1666 * an SV so it gets freed properly if there is a croak while
1667 * running the match */
1670 sv_points=newSV(maxlen * sizeof(U8 *));
1671 SvCUR_set(sv_points,
1672 maxlen * sizeof(U8 *));
1673 SvPOK_on(sv_points);
1674 sv_2mortal(sv_points);
1675 points=(U8**)SvPV_nolen(sv_points );
1676 if ( trie_type != trie_utf8_fold
1677 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1680 bitmap=(U8*)trie->bitmap;
1682 bitmap=(U8*)ANYOF_BITMAP(c);
1684 /* this is the Aho-Corasick algorithm modified a touch
1685 to include special handling for long "unknown char"
1686 sequences. The basic idea being that we use AC as long
1687 as we are dealing with a possible matching char, when
1688 we encounter an unknown char (and we have not encountered
1689 an accepting state) we scan forward until we find a legal
1691 AC matching is basically that of trie matching, except
1692 that when we encounter a failing transition, we fall back
1693 to the current states "fail state", and try the current char
1694 again, a process we repeat until we reach the root state,
1695 state 1, or a legal transition. If we fail on the root state
1696 then we can either terminate if we have reached an accepting
1697 state previously, or restart the entire process from the beginning
1701 while (s <= last_start) {
1702 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1710 U8 *uscan = (U8*)NULL;
1711 U8 *leftmost = NULL;
1713 U32 accepted_word= 0;
1717 while ( state && uc <= (U8*)strend ) {
1719 U32 word = aho->states[ state ].wordnum;
1723 DEBUG_TRIE_EXECUTE_r(
1724 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1725 dump_exec_pos( (char *)uc, c, strend, real_start,
1726 (char *)uc, do_utf8 );
1727 PerlIO_printf( Perl_debug_log,
1728 " Scanning for legal start char...\n");
1731 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1736 if (uc >(U8*)last_start) break;
1740 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
1741 if (!leftmost || lpos < leftmost) {
1742 DEBUG_r(accepted_word=word);
1748 points[pointpos++ % maxlen]= uc;
1749 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1750 uscan, len, uvc, charid, foldlen,
1752 DEBUG_TRIE_EXECUTE_r({
1753 dump_exec_pos( (char *)uc, c, strend, real_start,
1755 PerlIO_printf(Perl_debug_log,
1756 " Charid:%3u CP:%4"UVxf" ",
1762 word = aho->states[ state ].wordnum;
1764 base = aho->states[ state ].trans.base;
1766 DEBUG_TRIE_EXECUTE_r({
1768 dump_exec_pos( (char *)uc, c, strend, real_start,
1770 PerlIO_printf( Perl_debug_log,
1771 "%sState: %4"UVxf", word=%"UVxf,
1772 failed ? " Fail transition to " : "",
1773 (UV)state, (UV)word);
1779 ( ((offset = base + charid
1780 - 1 - trie->uniquecharcount)) >= 0)
1781 && ((U32)offset < trie->lasttrans)
1782 && trie->trans[offset].check == state
1783 && (tmp=trie->trans[offset].next))
1785 DEBUG_TRIE_EXECUTE_r(
1786 PerlIO_printf( Perl_debug_log," - legal\n"));
1791 DEBUG_TRIE_EXECUTE_r(
1792 PerlIO_printf( Perl_debug_log," - fail\n"));
1794 state = aho->fail[state];
1798 /* we must be accepting here */
1799 DEBUG_TRIE_EXECUTE_r(
1800 PerlIO_printf( Perl_debug_log," - accepting\n"));
1809 if (!state) state = 1;
1812 if ( aho->states[ state ].wordnum ) {
1813 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
1814 if (!leftmost || lpos < leftmost) {
1815 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
1820 s = (char*)leftmost;
1821 DEBUG_TRIE_EXECUTE_r({
1823 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1824 (UV)accepted_word, (IV)(s - real_start)
1827 if (!reginfo || regtry(reginfo, &s)) {
1833 DEBUG_TRIE_EXECUTE_r({
1834 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1837 DEBUG_TRIE_EXECUTE_r(
1838 PerlIO_printf( Perl_debug_log,"No match.\n"));
1847 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1857 - regexec_flags - match a regexp against a string
1860 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
1861 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1862 /* strend: pointer to null at end of string */
1863 /* strbeg: real beginning of string */
1864 /* minend: end of match must be >=minend after stringarg. */
1865 /* data: May be used for some additional optimizations.
1866 Currently its only used, with a U32 cast, for transmitting
1867 the ganch offset when doing a /g match. This will change */
1868 /* nosave: For optimizations. */
1871 struct regexp *const prog = (struct regexp *)SvANY(rx);
1872 /*register*/ char *s;
1873 register regnode *c;
1874 /*register*/ char *startpos = stringarg;
1875 I32 minlen; /* must match at least this many chars */
1876 I32 dontbother = 0; /* how many characters not to try at end */
1877 I32 end_shift = 0; /* Same for the end. */ /* CC */
1878 I32 scream_pos = -1; /* Internal iterator of scream. */
1879 char *scream_olds = NULL;
1880 const bool do_utf8 = cBOOL(DO_UTF8(sv));
1882 RXi_GET_DECL(prog,progi);
1883 regmatch_info reginfo; /* create some info to pass to regtry etc */
1884 regexp_paren_pair *swap = NULL;
1885 GET_RE_DEBUG_FLAGS_DECL;
1887 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
1888 PERL_UNUSED_ARG(data);
1890 /* Be paranoid... */
1891 if (prog == NULL || startpos == NULL) {
1892 Perl_croak(aTHX_ "NULL regexp parameter");
1896 multiline = prog->extflags & RXf_PMf_MULTILINE;
1897 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
1899 RX_MATCH_UTF8_set(rx, do_utf8);
1901 debug_start_match(rx, do_utf8, startpos, strend,
1905 minlen = prog->minlen;
1907 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
1908 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1909 "String too short [regexec_flags]...\n"));
1914 /* Check validity of program. */
1915 if (UCHARAT(progi->program) != REG_MAGIC) {
1916 Perl_croak(aTHX_ "corrupted regexp program");
1920 PL_reg_eval_set = 0;
1924 PL_reg_flags |= RF_utf8;
1926 /* Mark beginning of line for ^ and lookbehind. */
1927 reginfo.bol = startpos; /* XXX not used ??? */
1931 /* Mark end of line for $ (and such) */
1934 /* see how far we have to get to not match where we matched before */
1935 reginfo.till = startpos+minend;
1937 /* If there is a "must appear" string, look for it. */
1940 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
1942 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
1943 reginfo.ganch = startpos + prog->gofs;
1944 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1945 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
1946 } else if (sv && SvTYPE(sv) >= SVt_PVMG
1948 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1949 && mg->mg_len >= 0) {
1950 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
1951 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1952 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
1954 if (prog->extflags & RXf_ANCH_GPOS) {
1955 if (s > reginfo.ganch)
1957 s = reginfo.ganch - prog->gofs;
1958 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1959 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
1965 reginfo.ganch = strbeg + PTR2UV(data);
1966 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1967 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
1969 } else { /* pos() not defined */
1970 reginfo.ganch = strbeg;
1971 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
1972 "GPOS: reginfo.ganch = strbeg\n"));
1975 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
1976 /* We have to be careful. If the previous successful match
1977 was from this regex we don't want a subsequent partially
1978 successful match to clobber the old results.
1979 So when we detect this possibility we add a swap buffer
1980 to the re, and switch the buffer each match. If we fail
1981 we switch it back, otherwise we leave it swapped.
1984 /* do we need a save destructor here for eval dies? */
1985 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
1987 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
1988 re_scream_pos_data d;
1990 d.scream_olds = &scream_olds;
1991 d.scream_pos = &scream_pos;
1992 s = re_intuit_start(rx, sv, s, strend, flags, &d);
1994 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1995 goto phooey; /* not present */
2001 /* Simplest case: anchored match need be tried only once. */
2002 /* [unless only anchor is BOL and multiline is set] */
2003 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
2004 if (s == startpos && regtry(®info, &startpos))
2006 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2007 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
2012 dontbother = minlen - 1;
2013 end = HOP3c(strend, -dontbother, strbeg) - 1;
2014 /* for multiline we only have to try after newlines */
2015 if (prog->check_substr || prog->check_utf8) {
2019 if (regtry(®info, &s))
2024 if (prog->extflags & RXf_USE_INTUIT) {
2025 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2036 if (*s++ == '\n') { /* don't need PL_utf8skip here */
2037 if (regtry(®info, &s))
2044 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
2046 /* the warning about reginfo.ganch being used without intialization
2047 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
2048 and we only enter this block when the same bit is set. */
2049 char *tmp_s = reginfo.ganch - prog->gofs;
2051 if (tmp_s >= strbeg && regtry(®info, &tmp_s))
2056 /* Messy cases: unanchored match. */
2057 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2058 /* we have /x+whatever/ */
2059 /* it must be a one character string (XXXX Except UTF?) */
2064 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2065 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2066 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
2071 DEBUG_EXECUTE_r( did_match = 1 );
2072 if (regtry(®info, &s)) goto got_it;
2074 while (s < strend && *s == ch)
2082 DEBUG_EXECUTE_r( did_match = 1 );
2083 if (regtry(®info, &s)) goto got_it;
2085 while (s < strend && *s == ch)
2090 DEBUG_EXECUTE_r(if (!did_match)
2091 PerlIO_printf(Perl_debug_log,
2092 "Did not find anchored character...\n")
2095 else if (prog->anchored_substr != NULL
2096 || prog->anchored_utf8 != NULL
2097 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2098 && prog->float_max_offset < strend - s)) {
2103 char *last1; /* Last position checked before */
2107 if (prog->anchored_substr || prog->anchored_utf8) {
2108 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
2109 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2110 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
2111 back_max = back_min = prog->anchored_offset;
2113 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2114 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2115 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
2116 back_max = prog->float_max_offset;
2117 back_min = prog->float_min_offset;
2121 if (must == &PL_sv_undef)
2122 /* could not downgrade utf8 check substring, so must fail */
2128 last = HOP3c(strend, /* Cannot start after this */
2129 -(I32)(CHR_SVLEN(must)
2130 - (SvTAIL(must) != 0) + back_min), strbeg);
2133 last1 = HOPc(s, -1);
2135 last1 = s - 1; /* bogus */
2137 /* XXXX check_substr already used to find "s", can optimize if
2138 check_substr==must. */
2140 dontbother = end_shift;
2141 strend = HOPc(strend, -dontbother);
2142 while ( (s <= last) &&
2143 ((flags & REXEC_SCREAM)
2144 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
2145 end_shift, &scream_pos, 0))
2146 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
2147 (unsigned char*)strend, must,
2148 multiline ? FBMrf_MULTILINE : 0))) ) {
2149 /* we may be pointing at the wrong string */
2150 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
2151 s = strbeg + (s - SvPVX_const(sv));
2152 DEBUG_EXECUTE_r( did_match = 1 );
2153 if (HOPc(s, -back_max) > last1) {
2154 last1 = HOPc(s, -back_min);
2155 s = HOPc(s, -back_max);
2158 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
2160 last1 = HOPc(s, -back_min);
2164 while (s <= last1) {
2165 if (regtry(®info, &s))
2171 while (s <= last1) {
2172 if (regtry(®info, &s))
2178 DEBUG_EXECUTE_r(if (!did_match) {
2179 RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0),
2180 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2181 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2182 ((must == prog->anchored_substr || must == prog->anchored_utf8)
2183 ? "anchored" : "floating"),
2184 quoted, RE_SV_TAIL(must));
2188 else if ( (c = progi->regstclass) ) {
2190 const OPCODE op = OP(progi->regstclass);
2191 /* don't bother with what can't match */
2192 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2193 strend = HOPc(strend, -(minlen - 1));
2196 SV * const prop = sv_newmortal();
2197 regprop(prog, prop, c);
2199 RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1),
2201 PerlIO_printf(Perl_debug_log,
2202 "Matching stclass %.*s against %s (%d bytes)\n",
2203 (int)SvCUR(prop), SvPVX_const(prop),
2204 quoted, (int)(strend - s));
2207 if (find_byclass(prog, c, s, strend, ®info))
2209 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2213 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2218 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
2219 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
2220 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
2222 if (flags & REXEC_SCREAM) {
2223 last = screaminstr(sv, float_real, s - strbeg,
2224 end_shift, &scream_pos, 1); /* last one */
2226 last = scream_olds; /* Only one occurrence. */
2227 /* we may be pointing at the wrong string */
2228 else if (RXp_MATCH_COPIED(prog))
2229 s = strbeg + (s - SvPVX_const(sv));
2233 const char * const little = SvPV_const(float_real, len);
2235 if (SvTAIL(float_real)) {
2236 if (memEQ(strend - len + 1, little, len - 1))
2237 last = strend - len + 1;
2238 else if (!multiline)
2239 last = memEQ(strend - len, little, len)
2240 ? strend - len : NULL;
2246 last = rninstr(s, strend, little, little + len);
2248 last = strend; /* matching "$" */
2253 PerlIO_printf(Perl_debug_log,
2254 "%sCan't trim the tail, match fails (should not happen)%s\n",
2255 PL_colors[4], PL_colors[5]));
2256 goto phooey; /* Should not happen! */
2258 dontbother = strend - last + prog->float_min_offset;
2260 if (minlen && (dontbother < minlen))
2261 dontbother = minlen - 1;
2262 strend -= dontbother; /* this one's always in bytes! */
2263 /* We don't know much -- general case. */
2266 if (regtry(®info, &s))
2275 if (regtry(®info, &s))
2277 } while (s++ < strend);
2286 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
2288 if (PL_reg_eval_set)
2289 restore_pos(aTHX_ prog);
2290 if (RXp_PAREN_NAMES(prog))
2291 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
2293 /* make sure $`, $&, $', and $digit will work later */
2294 if ( !(flags & REXEC_NOT_FIRST) ) {
2295 RX_MATCH_COPY_FREE(rx);
2296 if (flags & REXEC_COPY_STR) {
2297 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
2298 #ifdef PERL_OLD_COPY_ON_WRITE
2300 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2302 PerlIO_printf(Perl_debug_log,
2303 "Copy on write: regexp capture, type %d\n",
2306 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2307 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2308 assert (SvPOKp(prog->saved_copy));
2312 RX_MATCH_COPIED_on(rx);
2313 s = savepvn(strbeg, i);
2319 prog->subbeg = strbeg;
2320 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2327 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2328 PL_colors[4], PL_colors[5]));
2329 if (PL_reg_eval_set)
2330 restore_pos(aTHX_ prog);
2332 /* we failed :-( roll it back */
2333 Safefree(prog->offs);
2342 - regtry - try match at specific point
2344 STATIC I32 /* 0 failure, 1 success */
2345 S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
2349 REGEXP *const rx = reginfo->prog;
2350 regexp *const prog = (struct regexp *)SvANY(rx);
2351 RXi_GET_DECL(prog,progi);
2352 GET_RE_DEBUG_FLAGS_DECL;
2354 PERL_ARGS_ASSERT_REGTRY;
2356 reginfo->cutpoint=NULL;
2358 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
2361 PL_reg_eval_set = RS_init;
2362 DEBUG_EXECUTE_r(DEBUG_s(
2363 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2364 (IV)(PL_stack_sp - PL_stack_base));
2367 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2368 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2370 /* Apparently this is not needed, judging by wantarray. */
2371 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2372 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2375 /* Make $_ available to executed code. */
2376 if (reginfo->sv != DEFSV) {
2378 DEFSV_set(reginfo->sv);
2381 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2382 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
2383 /* prepare for quick setting of pos */
2384 #ifdef PERL_OLD_COPY_ON_WRITE
2385 if (SvIsCOW(reginfo->sv))
2386 sv_force_normal_flags(reginfo->sv, 0);
2388 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
2389 &PL_vtbl_mglob, NULL, 0);
2393 PL_reg_oldpos = mg->mg_len;
2394 SAVEDESTRUCTOR_X(restore_pos, prog);
2396 if (!PL_reg_curpm) {
2397 Newxz(PL_reg_curpm, 1, PMOP);
2400 SV* const repointer = &PL_sv_undef;
2401 /* this regexp is also owned by the new PL_reg_curpm, which
2402 will try to free it. */
2403 av_push(PL_regex_padav, repointer);
2404 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2405 PL_regex_pad = AvARRAY(PL_regex_padav);
2410 /* It seems that non-ithreads works both with and without this code.
2411 So for efficiency reasons it seems best not to have the code
2412 compiled when it is not needed. */
2413 /* This is safe against NULLs: */
2414 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2415 /* PM_reg_curpm owns a reference to this regexp. */
2418 PM_SETRE(PL_reg_curpm, rx);
2419 PL_reg_oldcurpm = PL_curpm;
2420 PL_curpm = PL_reg_curpm;
2421 if (RXp_MATCH_COPIED(prog)) {
2422 /* Here is a serious problem: we cannot rewrite subbeg,
2423 since it may be needed if this match fails. Thus
2424 $` inside (?{}) could fail... */
2425 PL_reg_oldsaved = prog->subbeg;
2426 PL_reg_oldsavedlen = prog->sublen;
2427 #ifdef PERL_OLD_COPY_ON_WRITE
2428 PL_nrs = prog->saved_copy;
2430 RXp_MATCH_COPIED_off(prog);
2433 PL_reg_oldsaved = NULL;
2434 prog->subbeg = PL_bostr;
2435 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2437 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
2438 prog->offs[0].start = *startpos - PL_bostr;
2439 PL_reginput = *startpos;
2440 PL_reglastparen = &prog->lastparen;
2441 PL_reglastcloseparen = &prog->lastcloseparen;
2442 prog->lastparen = 0;
2443 prog->lastcloseparen = 0;
2445 PL_regoffs = prog->offs;
2446 if (PL_reg_start_tmpl <= prog->nparens) {
2447 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2448 if(PL_reg_start_tmp)
2449 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2451 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2454 /* XXXX What this code is doing here?!!! There should be no need
2455 to do this again and again, PL_reglastparen should take care of
2458 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2459 * Actually, the code in regcppop() (which Ilya may be meaning by
2460 * PL_reglastparen), is not needed at all by the test suite
2461 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2462 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2463 * Meanwhile, this code *is* needed for the
2464 * above-mentioned test suite tests to succeed. The common theme
2465 * on those tests seems to be returning null fields from matches.
2466 * --jhi updated by dapm */
2468 if (prog->nparens) {
2469 regexp_paren_pair *pp = PL_regoffs;
2471 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2479 if (regmatch(reginfo, progi->program + 1)) {
2480 PL_regoffs[0].end = PL_reginput - PL_bostr;
2483 if (reginfo->cutpoint)
2484 *startpos= reginfo->cutpoint;
2485 REGCP_UNWIND(lastcp);
2490 #define sayYES goto yes
2491 #define sayNO goto no
2492 #define sayNO_SILENT goto no_silent
2494 /* we dont use STMT_START/END here because it leads to
2495 "unreachable code" warnings, which are bogus, but distracting. */
2496 #define CACHEsayNO \
2497 if (ST.cache_mask) \
2498 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
2501 /* this is used to determine how far from the left messages like
2502 'failed...' are printed. It should be set such that messages
2503 are inline with the regop output that created them.
2505 #define REPORT_CODE_OFF 32
2508 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2509 #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
2511 #define SLAB_FIRST(s) (&(s)->states[0])
2512 #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2514 /* grab a new slab and return the first slot in it */
2516 STATIC regmatch_state *
2519 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2522 regmatch_slab *s = PL_regmatch_slab->next;
2524 Newx(s, 1, regmatch_slab);
2525 s->prev = PL_regmatch_slab;
2527 PL_regmatch_slab->next = s;
2529 PL_regmatch_slab = s;
2530 return SLAB_FIRST(s);
2534 /* push a new state then goto it */
2536 #define PUSH_STATE_GOTO(state, node) \
2538 st->resume_state = state; \
2541 /* push a new state with success backtracking, then goto it */
2543 #define PUSH_YES_STATE_GOTO(state, node) \
2545 st->resume_state = state; \
2546 goto push_yes_state;
2552 regmatch() - main matching routine
2554 This is basically one big switch statement in a loop. We execute an op,
2555 set 'next' to point the next op, and continue. If we come to a point which
2556 we may need to backtrack to on failure such as (A|B|C), we push a
2557 backtrack state onto the backtrack stack. On failure, we pop the top
2558 state, and re-enter the loop at the state indicated. If there are no more
2559 states to pop, we return failure.
2561 Sometimes we also need to backtrack on success; for example /A+/, where
2562 after successfully matching one A, we need to go back and try to
2563 match another one; similarly for lookahead assertions: if the assertion
2564 completes successfully, we backtrack to the state just before the assertion
2565 and then carry on. In these cases, the pushed state is marked as
2566 'backtrack on success too'. This marking is in fact done by a chain of
2567 pointers, each pointing to the previous 'yes' state. On success, we pop to
2568 the nearest yes state, discarding any intermediate failure-only states.
2569 Sometimes a yes state is pushed just to force some cleanup code to be
2570 called at the end of a successful match or submatch; e.g. (??{$re}) uses
2571 it to free the inner regex.
2573 Note that failure backtracking rewinds the cursor position, while
2574 success backtracking leaves it alone.
2576 A pattern is complete when the END op is executed, while a subpattern
2577 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2578 ops trigger the "pop to last yes state if any, otherwise return true"
2581 A common convention in this function is to use A and B to refer to the two
2582 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2583 the subpattern to be matched possibly multiple times, while B is the entire
2584 rest of the pattern. Variable and state names reflect this convention.
2586 The states in the main switch are the union of ops and failure/success of
2587 substates associated with with that op. For example, IFMATCH is the op
2588 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2589 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2590 successfully matched A and IFMATCH_A_fail is a state saying that we have
2591 just failed to match A. Resume states always come in pairs. The backtrack
2592 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2593 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2594 on success or failure.
2596 The struct that holds a backtracking state is actually a big union, with
2597 one variant for each major type of op. The variable st points to the
2598 top-most backtrack struct. To make the code clearer, within each
2599 block of code we #define ST to alias the relevant union.
2601 Here's a concrete example of a (vastly oversimplified) IFMATCH
2607 #define ST st->u.ifmatch
2609 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2610 ST.foo = ...; // some state we wish to save
2612 // push a yes backtrack state with a resume value of
2613 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2615 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2618 case IFMATCH_A: // we have successfully executed A; now continue with B
2620 bar = ST.foo; // do something with the preserved value
2623 case IFMATCH_A_fail: // A failed, so the assertion failed
2624 ...; // do some housekeeping, then ...
2625 sayNO; // propagate the failure
2632 For any old-timers reading this who are familiar with the old recursive
2633 approach, the code above is equivalent to:
2635 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2644 ...; // do some housekeeping, then ...
2645 sayNO; // propagate the failure
2648 The topmost backtrack state, pointed to by st, is usually free. If you
2649 want to claim it, populate any ST.foo fields in it with values you wish to
2650 save, then do one of
2652 PUSH_STATE_GOTO(resume_state, node);
2653 PUSH_YES_STATE_GOTO(resume_state, node);
2655 which sets that backtrack state's resume value to 'resume_state', pushes a
2656 new free entry to the top of the backtrack stack, then goes to 'node'.
2657 On backtracking, the free slot is popped, and the saved state becomes the
2658 new free state. An ST.foo field in this new top state can be temporarily
2659 accessed to retrieve values, but once the main loop is re-entered, it
2660 becomes available for reuse.
2662 Note that the depth of the backtrack stack constantly increases during the
2663 left-to-right execution of the pattern, rather than going up and down with
2664 the pattern nesting. For example the stack is at its maximum at Z at the
2665 end of the pattern, rather than at X in the following:
2667 /(((X)+)+)+....(Y)+....Z/
2669 The only exceptions to this are lookahead/behind assertions and the cut,
2670 (?>A), which pop all the backtrack states associated with A before
2673 Bascktrack state structs are allocated in slabs of about 4K in size.
2674 PL_regmatch_state and st always point to the currently active state,
2675 and PL_regmatch_slab points to the slab currently containing
2676 PL_regmatch_state. The first time regmatch() is called, the first slab is
2677 allocated, and is never freed until interpreter destruction. When the slab
2678 is full, a new one is allocated and chained to the end. At exit from
2679 regmatch(), slabs allocated since entry are freed.
2684 #define DEBUG_STATE_pp(pp) \
2686 DUMP_EXEC_POS(locinput, scan, do_utf8); \
2687 PerlIO_printf(Perl_debug_log, \
2688 " %*s"pp" %s%s%s%s%s\n", \
2690 PL_reg_name[st->resume_state], \
2691 ((st==yes_state||st==mark_state) ? "[" : ""), \
2692 ((st==yes_state) ? "Y" : ""), \
2693 ((st==mark_state) ? "M" : ""), \
2694 ((st==yes_state||st==mark_state) ? "]" : "") \
2699 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
2704 S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8,
2705 const char *start, const char *end, const char *blurb)
2707 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
2709 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2714 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
2715 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
2717 RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1),
2718 start, end - start, 60);
2720 PerlIO_printf(Perl_debug_log,
2721 "%s%s REx%s %s against %s\n",
2722 PL_colors[4], blurb, PL_colors[5], s0, s1);
2724 if (do_utf8||utf8_pat)
2725 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2726 utf8_pat ? "pattern" : "",
2727 utf8_pat && do_utf8 ? " and " : "",
2728 do_utf8 ? "string" : ""
2734 S_dump_exec_pos(pTHX_ const char *locinput,
2735 const regnode *scan,
2736 const char *loc_regeol,
2737 const char *loc_bostr,
2738 const char *loc_reg_starttry,
2741 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
2742 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2743 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
2744 /* The part of the string before starttry has one color
2745 (pref0_len chars), between starttry and current
2746 position another one (pref_len - pref0_len chars),
2747 after the current position the third one.
2748 We assume that pref0_len <= pref_len, otherwise we
2749 decrease pref0_len. */
2750 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2751 ? (5 + taill) - l : locinput - loc_bostr;
2754 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2756 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2758 pref0_len = pref_len - (locinput - loc_reg_starttry);
2759 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2760 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2761 ? (5 + taill) - pref_len : loc_regeol - locinput);
2762 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2766 if (pref0_len > pref_len)
2767 pref0_len = pref_len;
2769 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
2771 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2772 (locinput - pref_len),pref0_len, 60, 4, 5);
2774 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
2775 (locinput - pref_len + pref0_len),
2776 pref_len - pref0_len, 60, 2, 3);
2778 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2779 locinput, loc_regeol - locinput, 10, 0, 1);
2781 const STRLEN tlen=len0+len1+len2;
2782 PerlIO_printf(Perl_debug_log,
2783 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
2784 (IV)(locinput - loc_bostr),
2787 (docolor ? "" : "> <"),
2789 (int)(tlen > 19 ? 0 : 19 - tlen),
2796 /* reg_check_named_buff_matched()
2797 * Checks to see if a named buffer has matched. The data array of
2798 * buffer numbers corresponding to the buffer is expected to reside
2799 * in the regexp->data->data array in the slot stored in the ARG() of
2800 * node involved. Note that this routine doesn't actually care about the
2801 * name, that information is not preserved from compilation to execution.
2802 * Returns the index of the leftmost defined buffer with the given name
2803 * or 0 if non of the buffers matched.
2806 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2809 RXi_GET_DECL(rex,rexi);
2810 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
2811 I32 *nums=(I32*)SvPVX(sv_dat);
2813 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2815 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2816 if ((I32)*PL_reglastparen >= nums[n] &&
2817 PL_regoffs[nums[n]].end != -1)
2826 /* free all slabs above current one - called during LEAVE_SCOPE */
2829 S_clear_backtrack_stack(pTHX_ void *p)
2831 regmatch_slab *s = PL_regmatch_slab->next;
2836 PL_regmatch_slab->next = NULL;
2838 regmatch_slab * const osl = s;
2845 #define SETREX(Re1,Re2) \
2846 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2849 STATIC I32 /* 0 failure, 1 success */
2850 S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
2852 #if PERL_VERSION < 9 && !defined(PERL_CORE)
2856 register const bool do_utf8 = PL_reg_match_utf8;
2857 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2858 REGEXP *rex_sv = reginfo->prog;
2859 regexp *rex = (struct regexp *)SvANY(rex_sv);
2860 RXi_GET_DECL(rex,rexi);
2862 /* the current state. This is a cached copy of PL_regmatch_state */
2863 register regmatch_state *st;
2864 /* cache heavy used fields of st in registers */
2865 register regnode *scan;
2866 register regnode *next;
2867 register U32 n = 0; /* general value; init to avoid compiler warning */
2868 register I32 ln = 0; /* len or last; init to avoid compiler warning */
2869 register char *locinput = PL_reginput;
2870 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2872 bool result = 0; /* return value of S_regmatch */
2873 int depth = 0; /* depth of backtrack stack */
2874 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2875 const U32 max_nochange_depth =
2876 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2877 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
2878 regmatch_state *yes_state = NULL; /* state to pop to on success of
2880 /* mark_state piggy backs on the yes_state logic so that when we unwind
2881 the stack on success we can update the mark_state as we go */
2882 regmatch_state *mark_state = NULL; /* last mark state we have seen */
2883 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
2884 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
2886 bool no_final = 0; /* prevent failure from backtracking? */
2887 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
2888 char *startpoint = PL_reginput;
2889 SV *popmark = NULL; /* are we looking for a mark? */
2890 SV *sv_commit = NULL; /* last mark name seen in failure */
2891 SV *sv_yes_mark = NULL; /* last mark name we have seen
2892 during a successfull match */
2893 U32 lastopen = 0; /* last open we saw */
2894 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
2895 SV* const oreplsv = GvSV(PL_replgv);
2896 /* these three flags are set by various ops to signal information to
2897 * the very next op. They have a useful lifetime of exactly one loop
2898 * iteration, and are not preserved or restored by state pushes/pops
2900 bool sw = 0; /* the condition value in (?(cond)a|b) */
2901 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2902 int logical = 0; /* the following EVAL is:
2906 or the following IFMATCH/UNLESSM is:
2907 false: plain (?=foo)
2908 true: used as a condition: (?(?=foo))
2911 GET_RE_DEBUG_FLAGS_DECL;
2914 PERL_ARGS_ASSERT_REGMATCH;
2916 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
2917 PerlIO_printf(Perl_debug_log,"regmatch start\n");
2919 /* on first ever call to regmatch, allocate first slab */
2920 if (!PL_regmatch_slab) {
2921 Newx(PL_regmatch_slab, 1, regmatch_slab);
2922 PL_regmatch_slab->prev = NULL;
2923 PL_regmatch_slab->next = NULL;
2924 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2927 oldsave = PL_savestack_ix;
2928 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
2929 SAVEVPTR(PL_regmatch_slab);
2930 SAVEVPTR(PL_regmatch_state);
2932 /* grab next free state slot */
2933 st = ++PL_regmatch_state;
2934 if (st > SLAB_LAST(PL_regmatch_slab))
2935 st = PL_regmatch_state = S_push_slab(aTHX);
2937 /* Note that nextchr is a byte even in UTF */
2938 nextchr = UCHARAT(locinput);
2940 while (scan != NULL) {
2943 SV * const prop = sv_newmortal();
2944 regnode *rnext=regnext(scan);
2945 DUMP_EXEC_POS( locinput, scan, do_utf8 );
2946 regprop(rex, prop, scan);
2948 PerlIO_printf(Perl_debug_log,
2949 "%3"IVdf":%*s%s(%"IVdf")\n",
2950 (IV)(scan - rexi->program), depth*2, "",
2952 (PL_regkind[OP(scan)] == END || !rnext) ?
2953 0 : (IV)(rnext - rexi->program));
2956 next = scan + NEXT_OFF(scan);
2959 state_num = OP(scan);
2963 assert(PL_reglastparen == &rex->lastparen);
2964 assert(PL_reglastcloseparen == &rex->lastcloseparen);
2965 assert(PL_regoffs == rex->offs);
2967 switch (state_num) {
2969 if (locinput == PL_bostr)
2971 /* reginfo->till = reginfo->bol; */
2976 if (locinput == PL_bostr ||
2977 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2983 if (locinput == PL_bostr)
2987 if (locinput == reginfo->ganch)
2992 /* update the startpoint */
2993 st->u.keeper.val = PL_regoffs[0].start;
2994 PL_reginput = locinput;
2995 PL_regoffs[0].start = locinput - PL_bostr;
2996 PUSH_STATE_GOTO(KEEPS_next, next);
2998 case KEEPS_next_fail:
2999 /* rollback the start point change */
3000 PL_regoffs[0].start = st->u.keeper.val;
3006 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3011 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
3013 if (PL_regeol - locinput > 1)
3017 if (PL_regeol != locinput)
3021 if (!nextchr && locinput >= PL_regeol)
3024 locinput += PL_utf8skip[nextchr];
3025 if (locinput > PL_regeol)
3027 nextchr = UCHARAT(locinput);
3030 nextchr = UCHARAT(++locinput);
3033 if (!nextchr && locinput >= PL_regeol)
3035 nextchr = UCHARAT(++locinput);
3038 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3041 locinput += PL_utf8skip[nextchr];
3042 if (locinput > PL_regeol)
3044 nextchr = UCHARAT(locinput);
3047 nextchr = UCHARAT(++locinput);
3051 #define ST st->u.trie
3053 /* In this case the charclass data is available inline so
3054 we can fail fast without a lot of extra overhead.
3056 if (scan->flags == EXACT || !do_utf8) {
3057 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3059 PerlIO_printf(Perl_debug_log,
3060 "%*s %sfailed to match trie start class...%s\n",
3061 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3069 /* the basic plan of execution of the trie is:
3070 * At the beginning, run though all the states, and
3071 * find the longest-matching word. Also remember the position
3072 * of the shortest matching word. For example, this pattern:
3075 * when matched against the string "abcde", will generate
3076 * accept states for all words except 3, with the longest
3077 * matching word being 4, and the shortest being 1 (with
3078 * the position being after char 1 of the string).
3080 * Then for each matching word, in word order (i.e. 1,2,4,5),
3081 * we run the remainder of the pattern; on each try setting
3082 * the current position to the character following the word,
3083 * returning to try the next word on failure.
3085 * We avoid having to build a list of words at runtime by
3086 * using a compile-time structure, wordinfo[].prev, which
3087 * gives, for each word, the previous accepting word (if any).
3088 * In the case above it would contain the mappings 1->2, 2->0,
3089 * 3->0, 4->5, 5->1. We can use this table to generate, from
3090 * the longest word (4 above), a list of all words, by
3091 * following the list of prev pointers; this gives us the
3092 * unordered list 4,5,1,2. Then given the current word we have
3093 * just tried, we can go through the list and find the
3094 * next-biggest word to try (so if we just failed on word 2,
3095 * the next in the list is 4).
3097 * Since at runtime we don't record the matching position in
3098 * the string for each word, we have to work that out for
3099 * each word we're about to process. The wordinfo table holds
3100 * the character length of each word; given that we recorded
3101 * at the start: the position of the shortest word and its
3102 * length in chars, we just need to move the pointer the
3103 * difference between the two char lengths. Depending on
3104 * Unicode status and folding, that's cheap or expensive.
3106 * This algorithm is optimised for the case where are only a
3107 * small number of accept states, i.e. 0,1, or maybe 2.
3108 * With lots of accepts states, and having to try all of them,
3109 * it becomes quadratic on number of accept states to find all
3114 /* what type of TRIE am I? (utf8 makes this contextual) */
3115 DECL_TRIE_TYPE(scan);
3117 /* what trie are we using right now */
3118 reg_trie_data * const trie
3119 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
3120 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3121 U32 state = trie->startstate;
3123 if (trie->bitmap && trie_type != trie_utf8_fold &&
3124 !TRIE_BITMAP_TEST(trie,*locinput)
3126 if (trie->states[ state ].wordnum) {
3128 PerlIO_printf(Perl_debug_log,
3129 "%*s %smatched empty string...%s\n",
3130 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3135 PerlIO_printf(Perl_debug_log,
3136 "%*s %sfailed to match trie start class...%s\n",
3137 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3144 U8 *uc = ( U8* )locinput;
3148 U8 *uscan = (U8*)NULL;
3149 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
3150 U32 charcount = 0; /* how many input chars we have matched */
3151 U32 accepted = 0; /* have we seen any accepting states? */
3154 ST.jump = trie->jump;
3157 ST.longfold = FALSE; /* char longer if folded => it's harder */
3160 /* fully traverse the TRIE; note the position of the
3161 shortest accept state and the wordnum of the longest
3164 while ( state && uc <= (U8*)PL_regeol ) {
3165 U32 base = trie->states[ state ].trans.base;
3169 wordnum = trie->states[ state ].wordnum;
3171 if (wordnum) { /* it's an accept state */
3174 /* record first match position */
3176 ST.firstpos = (U8*)locinput;
3181 ST.firstchars = charcount;
3184 if (!ST.nextword || wordnum < ST.nextword)
3185 ST.nextword = wordnum;
3186 ST.topword = wordnum;
3189 DEBUG_TRIE_EXECUTE_r({
3190 DUMP_EXEC_POS( (char *)uc, scan, do_utf8 );
3191 PerlIO_printf( Perl_debug_log,
3192 "%*s %sState: %4"UVxf" Accepted: %c ",
3193 2+depth * 2, "", PL_colors[4],
3194 (UV)state, (accepted ? 'Y' : 'N'));
3197 /* read a char and goto next state */
3200 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3201 uscan, len, uvc, charid, foldlen,
3208 base + charid - 1 - trie->uniquecharcount)) >= 0)
3210 && ((U32)offset < trie->lasttrans)
3211 && trie->trans[offset].check == state)
3213 state = trie->trans[offset].next;
3224 DEBUG_TRIE_EXECUTE_r(
3225 PerlIO_printf( Perl_debug_log,
3226 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
3227 charid, uvc, (UV)state, PL_colors[5] );
3233 /* calculate total number of accept states */
3238 w = trie->wordinfo[w].prev;
3241 ST.accepted = accepted;
3245 PerlIO_printf( Perl_debug_log,
3246 "%*s %sgot %"IVdf" possible matches%s\n",
3247 REPORT_CODE_OFF + depth * 2, "",
3248 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3250 goto trie_first_try; /* jump into the fail handler */
3254 case TRIE_next_fail: /* we failed - try next alternative */
3256 REGCP_UNWIND(ST.cp);
3257 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3258 PL_regoffs[n].end = -1;
3259 *PL_reglastparen = n;
3261 if (!--ST.accepted) {
3263 PerlIO_printf( Perl_debug_log,
3264 "%*s %sTRIE failed...%s\n",
3265 REPORT_CODE_OFF+depth*2, "",
3272 /* Find next-highest word to process. Note that this code
3273 * is O(N^2) per trie run (O(N) per branch), so keep tight */
3274 register U16 min = 0;
3276 register U16 const nextword = ST.nextword;
3277 register reg_trie_wordinfo * const wordinfo
3278 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3279 for (word=ST.topword; word; word=wordinfo[word].prev) {
3280 if (word > nextword && (!min || word < min))
3293 ST.lastparen = *PL_reglastparen;
3297 /* find start char of end of current word */
3299 U32 chars; /* how many chars to skip */
3300 U8 *uc = ST.firstpos;
3301 reg_trie_data * const trie
3302 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3304 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3306 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3310 /* the hard option - fold each char in turn and find
3311 * its folded length (which may be different */
3312 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3320 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3328 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3333 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3347 PL_reginput = (char *)uc;
3350 scan = (ST.jump && ST.jump[ST.nextword])
3351 ? ST.me + ST.jump[ST.nextword]
3355 PerlIO_printf( Perl_debug_log,
3356 "%*s %sTRIE matched word #%d, continuing%s\n",
3357 REPORT_CODE_OFF+depth*2, "",
3364 if (ST.accepted > 1 || has_cutgroup) {
3365 PUSH_STATE_GOTO(TRIE_next, scan);
3368 /* only one choice left - just continue */
3370 AV *const trie_words
3371 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3372 SV ** const tmp = av_fetch( trie_words,
3374 SV *sv= tmp ? sv_newmortal() : NULL;
3376 PerlIO_printf( Perl_debug_log,
3377 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3378 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3380 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3381 PL_colors[0], PL_colors[1],
3382 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3384 : "not compiled under -Dr",
3388 locinput = PL_reginput;
3389 nextchr = UCHARAT(locinput);
3390 continue; /* execute rest of RE */
3395 char *s = STRING(scan);
3397 if (do_utf8 != UTF) {
3398 /* The target and the pattern have differing utf8ness. */
3400 const char * const e = s + ln;
3403 /* The target is utf8, the pattern is not utf8. */
3408 if (NATIVE_TO_UNI(*(U8*)s) !=
3409 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
3417 /* The target is not utf8, the pattern is utf8. */
3422 if (NATIVE_TO_UNI(*((U8*)l)) !=
3423 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
3431 nextchr = UCHARAT(locinput);
3434 /* The target and the pattern have the same utf8ness. */
3435 /* Inline the first character, for speed. */
3436 if (UCHARAT(s) != nextchr)
3438 if (PL_regeol - locinput < ln)
3440 if (ln > 1 && memNE(s, locinput, ln))
3443 nextchr = UCHARAT(locinput);
3447 PL_reg_flags |= RF_tainted;
3450 char * const s = STRING(scan);
3453 if (do_utf8 || UTF) {
3454 /* Either target or the pattern are utf8. */
3455 const char * const l = locinput;
3456 char *e = PL_regeol;
3458 if (! foldEQ_utf8(s, 0, ln, cBOOL(UTF),
3459 l, &e, 0, do_utf8)) {
3460 /* One more case for the sharp s:
3461 * pack("U0U*", 0xDF) =~ /ss/i,
3462 * the 0xC3 0x9F are the UTF-8
3463 * byte sequence for the U+00DF. */
3466 toLOWER(s[0]) == 's' &&
3468 toLOWER(s[1]) == 's' &&
3475 nextchr = UCHARAT(locinput);
3479 /* Neither the target and the pattern are utf8. */
3481 /* Inline the first character, for speed. */
3482 if (UCHARAT(s) != nextchr &&
3483 UCHARAT(s) != ((OP(scan) == EXACTF)
3484 ? PL_fold : PL_fold_locale)[nextchr])
3486 if (PL_regeol - locinput < ln)
3488 if (ln > 1 && (OP(scan) == EXACTF
3489 ? ! foldEQ(s, locinput, ln)
3490 : ! foldEQ_locale(s, locinput, ln)))
3493 nextchr = UCHARAT(locinput);
3498 PL_reg_flags |= RF_tainted;
3502 /* was last char in word? */
3504 if (locinput == PL_bostr)
3507 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3509 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3511 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3512 ln = isALNUM_uni(ln);
3513 LOAD_UTF8_CHARCLASS_ALNUM();
3514 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
3517 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3518 n = isALNUM_LC_utf8((U8*)locinput);
3522 ln = (locinput != PL_bostr) ?
3523 UCHARAT(locinput - 1) : '\n';
3524 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3526 n = isALNUM(nextchr);
3529 ln = isALNUM_LC(ln);
3530 n = isALNUM_LC(nextchr);
3533 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3534 OP(scan) == BOUNDL))
3539 STRLEN inclasslen = PL_regeol - locinput;
3541 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
3543 if (locinput >= PL_regeol)
3545 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
3546 nextchr = UCHARAT(locinput);
3551 nextchr = UCHARAT(locinput);
3552 if (!REGINCLASS(rex, scan, (U8*)locinput))
3554 if (!nextchr && locinput >= PL_regeol)
3556 nextchr = UCHARAT(++locinput);
3560 /* If we might have the case of the German sharp s
3561 * in a casefolding Unicode character class. */
3563 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3564 locinput += SHARP_S_SKIP;
3565 nextchr = UCHARAT(locinput);
3570 /* Special char classes - The defines start on line 129 or so */
3571 CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3572 CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC);
3574 CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3575 CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC);
3577 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3578 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3580 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3581 a Unicode extended Grapheme Cluster */
3582 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3583 extended Grapheme Cluster is:
3586 | Prepend* Begin Extend*
3589 Begin is (Hangul-syllable | ! Control)
3590 Extend is (Grapheme_Extend | Spacing_Mark)
3591 Control is [ GCB_Control CR LF ]
3593 The discussion below shows how the code for CLUMP is derived
3594 from this regex. Note that most of these concepts are from
3595 property values of the Grapheme Cluster Boundary (GCB) property.
3596 No code point can have multiple property values for a given
3597 property. Thus a code point in Prepend can't be in Control, but
3598 it must be in !Control. This is why Control above includes
3599 GCB_Control plus CR plus LF. The latter two are used in the GCB
3600 property separately, and so can't be in GCB_Control, even though
3601 they logically are controls. Control is not the same as gc=cc,
3602 but includes format and other characters as well.
3604 The Unicode definition of Hangul-syllable is:
3606 | (L* ( ( V | LV ) V* | LVT ) T*)
3609 Each of these is a value for the GCB property, and hence must be
3610 disjoint, so the order they are tested is immaterial, so the
3611 above can safely be changed to
3614 | (L* ( LVT | ( V | LV ) V*) T*)
3616 The last two terms can be combined like this:
3618 | (( LVT | ( V | LV ) V*) T*))
3620 And refactored into this:
3621 L* (L | LVT T* | V V* T* | LV V* T*)
3623 That means that if we have seen any L's at all we can quit
3624 there, but if the next character is a LVT, a V or and LV we
3627 There is a subtlety with Prepend* which showed up in testing.
3628 Note that the Begin, and only the Begin is required in:
3629 | Prepend* Begin Extend*
3630 Also, Begin contains '! Control'. A Prepend must be a '!
3631 Control', which means it must be a Begin. What it comes down to
3632 is that if we match Prepend* and then find no suitable Begin
3633 afterwards, that if we backtrack the last Prepend, that one will
3634 be a suitable Begin.
3637 if (locinput >= PL_regeol)
3641 /* Match either CR LF or '.', as all the other possibilities
3643 locinput++; /* Match the . or CR */
3645 && locinput < PL_regeol
3646 && UCHARAT(locinput) == '\n') locinput++;
3650 /* Utf8: See if is ( CR LF ); already know that locinput <
3651 * PL_regeol, so locinput+1 is in bounds */
3652 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3656 /* In case have to backtrack to beginning, then match '.' */
3657 char *starting = locinput;
3659 /* In case have to backtrack the last prepend */
3660 char *previous_prepend = 0;
3662 LOAD_UTF8_CHARCLASS_GCB();
3664 /* Match (prepend)* */
3665 while (locinput < PL_regeol
3666 && swash_fetch(PL_utf8_X_prepend,
3667 (U8*)locinput, do_utf8))
3669 previous_prepend = locinput;
3670 locinput += UTF8SKIP(locinput);
3673 /* As noted above, if we matched a prepend character, but
3674 * the next thing won't match, back off the last prepend we
3675 * matched, as it is guaranteed to match the begin */
3676 if (previous_prepend
3677 && (locinput >= PL_regeol
3678 || ! swash_fetch(PL_utf8_X_begin,
3679 (U8*)locinput, do_utf8)))
3681 locinput = previous_prepend;
3684 /* Note that here we know PL_regeol > locinput, as we
3685 * tested that upon input to this switch case, and if we
3686 * moved locinput forward, we tested the result just above
3687 * and it either passed, or we backed off so that it will
3689 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, do_utf8)) {
3691 /* Here did not match the required 'Begin' in the
3692 * second term. So just match the very first
3693 * character, the '.' of the final term of the regex */
3694 locinput = starting + UTF8SKIP(starting);
3697 /* Here is the beginning of a character that can have
3698 * an extender. It is either a hangul syllable, or a
3700 if (swash_fetch(PL_utf8_X_non_hangul,
3701 (U8*)locinput, do_utf8))
3704 /* Here not a Hangul syllable, must be a
3705 * ('! * Control') */
3706 locinput += UTF8SKIP(locinput);
3709 /* Here is a Hangul syllable. It can be composed
3710 * of several individual characters. One
3711 * possibility is T+ */
3712 if (swash_fetch(PL_utf8_X_T,
3713 (U8*)locinput, do_utf8))
3715 while (locinput < PL_regeol
3716 && swash_fetch(PL_utf8_X_T,
3717 (U8*)locinput, do_utf8))
3719 locinput += UTF8SKIP(locinput);
3723 /* Here, not T+, but is a Hangul. That means
3724 * it is one of the others: L, LV, LVT or V,
3726 * L* (L | LVT T* | V V* T* | LV V* T*) */
3729 while (locinput < PL_regeol
3730 && swash_fetch(PL_utf8_X_L,
3731 (U8*)locinput, do_utf8))
3733 locinput += UTF8SKIP(locinput);
3736 /* Here, have exhausted L*. If the next
3737 * character is not an LV, LVT nor V, it means
3738 * we had to have at least one L, so matches L+
3739 * in the original equation, we have a complete
3740 * hangul syllable. Are done. */
3742 if (locinput < PL_regeol
3743 && swash_fetch(PL_utf8_X_LV_LVT_V,
3744 (U8*)locinput, do_utf8))
3747 /* Otherwise keep going. Must be LV, LVT
3748 * or V. See if LVT */
3749 if (swash_fetch(PL_utf8_X_LVT,
3750 (U8*)locinput, do_utf8))
3752 locinput += UTF8SKIP(locinput);
3755 /* Must be V or LV. Take it, then
3757 locinput += UTF8SKIP(locinput);
3758 while (locinput < PL_regeol
3759 && swash_fetch(PL_utf8_X_V,
3760 (U8*)locinput, do_utf8))
3762 locinput += UTF8SKIP(locinput);
3766 /* And any of LV, LVT, or V can be followed
3768 while (locinput < PL_regeol
3769 && swash_fetch(PL_utf8_X_T,
3773 locinput += UTF8SKIP(locinput);
3779 /* Match any extender */
3780 while (locinput < PL_regeol
3781 && swash_fetch(PL_utf8_X_extend,
3782 (U8*)locinput, do_utf8))
3784 locinput += UTF8SKIP(locinput);
3788 if (locinput > PL_regeol) sayNO;
3790 nextchr = UCHARAT(locinput);
3797 PL_reg_flags |= RF_tainted;
3802 n = reg_check_named_buff_matched(rex,scan);
3805 type = REF + ( type - NREF );
3812 PL_reg_flags |= RF_tainted;
3816 n = ARG(scan); /* which paren pair */
3819 ln = PL_regoffs[n].start;
3820 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3821 if (*PL_reglastparen < n || ln == -1)
3822 sayNO; /* Do not match unless seen CLOSEn. */
3823 if (ln == PL_regoffs[n].end)
3827 if (do_utf8 && type != REF) { /* REF can do byte comparison */
3829 const char *e = PL_bostr + PL_regoffs[n].end;
3831 * Note that we can't do the "other character" lookup trick as
3832 * in the 8-bit case (no pun intended) because in Unicode we