5 * 'A fair jaw-cracker dwarf-language must be.' --Samwise Gamgee
7 * [p.285 of _The Lord of the Rings_, II/iii: "The Ring Goes South"]
10 /* This file contains functions for compiling a regular expression. See
11 * also regexec.c which funnily enough, contains functions for executing
12 * a regular expression.
14 * This file is also copied at build time to ext/re/re_comp.c, where
15 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
16 * This causes the main functions to be compiled under new names and with
17 * debugging support added, which makes "use re 'debug'" work.
20 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
21 * confused with the original package (see point 3 below). Thanks, Henry!
24 /* Additional note: this code is very heavily munged from Henry's version
25 * in places. In some spots I've traded clarity for efficiency, so don't
26 * blame Henry for some of the lack of readability.
29 /* The names of the functions have been changed from regcomp and
30 * regexec to pregcomp and pregexec in order to avoid conflicts
31 * with the POSIX routines of the same names.
34 #ifdef PERL_EXT_RE_BUILD
39 * pregcomp and pregexec -- regsub and regerror are not used in perl
41 * Copyright (c) 1986 by University of Toronto.
42 * Written by Henry Spencer. Not derived from licensed software.
44 * Permission is granted to anyone to use this software for any
45 * purpose on any computer system, and to redistribute it freely,
46 * subject to the following restrictions:
48 * 1. The author is not responsible for the consequences of use of
49 * this software, no matter how awful, even if they arise
52 * 2. The origin of this software must not be misrepresented, either
53 * by explicit claim or by omission.
55 * 3. Altered versions must be plainly marked as such, and must not
56 * be misrepresented as being the original software.
59 **** Alterations to Henry's code are...
61 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
62 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
63 **** by Larry Wall and others
65 **** You may distribute under the terms of either the GNU General Public
66 **** 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_REGCOMP_C
75 #undef PERL_IN_XSUB_RE
76 #define PERL_IN_XSUB_RE 1
78 #undef PERL_IN_XSUB_RE
80 #ifndef PERL_IN_XSUB_RE
85 #ifdef PERL_IN_XSUB_RE
91 #include "dquote_static.c"
98 # if defined(BUGGY_MSC6)
99 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
100 # pragma optimize("a",off)
101 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
102 # pragma optimize("w",on )
103 # endif /* BUGGY_MSC6 */
107 #define STATIC static
110 typedef struct RExC_state_t {
111 U32 flags; /* are we folding, multilining? */
112 char *precomp; /* uncompiled string. */
113 REGEXP *rx_sv; /* The SV that is the regexp. */
114 regexp *rx; /* perl core regexp structure */
115 regexp_internal *rxi; /* internal data for regexp object pprivate field */
116 char *start; /* Start of input for compile */
117 char *end; /* End of input for compile */
118 char *parse; /* Input-scan pointer. */
119 I32 whilem_seen; /* number of WHILEM in this expr */
120 regnode *emit_start; /* Start of emitted-code area */
121 regnode *emit_bound; /* First regnode outside of the allocated space */
122 regnode *emit; /* Code-emit pointer; ®dummy = don't = compiling */
123 I32 naughty; /* How bad is this pattern? */
124 I32 sawback; /* Did we see \1, ...? */
126 I32 size; /* Code size. */
127 I32 npar; /* Capture buffer count, (OPEN). */
128 I32 cpar; /* Capture buffer count, (CLOSE). */
129 I32 nestroot; /* root parens we are in - used by accept */
133 regnode **open_parens; /* pointers to open parens */
134 regnode **close_parens; /* pointers to close parens */
135 regnode *opend; /* END node in program */
136 I32 utf8; /* whether the pattern is utf8 or not */
137 I32 orig_utf8; /* whether the pattern was originally in utf8 */
138 /* XXX use this for future optimisation of case
139 * where pattern must be upgraded to utf8. */
140 I32 uni_semantics; /* If a d charset modifier should use unicode
141 rules, even if the pattern is not in
143 HV *paren_names; /* Paren names */
145 regnode **recurse; /* Recurse regops */
146 I32 recurse_count; /* Number of recurse regops */
149 I32 override_recoding;
151 char *starttry; /* -Dr: where regtry was called. */
152 #define RExC_starttry (pRExC_state->starttry)
155 const char *lastparse;
157 AV *paren_name_list; /* idx -> name */
158 #define RExC_lastparse (pRExC_state->lastparse)
159 #define RExC_lastnum (pRExC_state->lastnum)
160 #define RExC_paren_name_list (pRExC_state->paren_name_list)
164 #define RExC_flags (pRExC_state->flags)
165 #define RExC_precomp (pRExC_state->precomp)
166 #define RExC_rx_sv (pRExC_state->rx_sv)
167 #define RExC_rx (pRExC_state->rx)
168 #define RExC_rxi (pRExC_state->rxi)
169 #define RExC_start (pRExC_state->start)
170 #define RExC_end (pRExC_state->end)
171 #define RExC_parse (pRExC_state->parse)
172 #define RExC_whilem_seen (pRExC_state->whilem_seen)
173 #ifdef RE_TRACK_PATTERN_OFFSETS
174 #define RExC_offsets (pRExC_state->rxi->u.offsets) /* I am not like the others */
176 #define RExC_emit (pRExC_state->emit)
177 #define RExC_emit_start (pRExC_state->emit_start)
178 #define RExC_emit_bound (pRExC_state->emit_bound)
179 #define RExC_naughty (pRExC_state->naughty)
180 #define RExC_sawback (pRExC_state->sawback)
181 #define RExC_seen (pRExC_state->seen)
182 #define RExC_size (pRExC_state->size)
183 #define RExC_npar (pRExC_state->npar)
184 #define RExC_nestroot (pRExC_state->nestroot)
185 #define RExC_extralen (pRExC_state->extralen)
186 #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
187 #define RExC_seen_evals (pRExC_state->seen_evals)
188 #define RExC_utf8 (pRExC_state->utf8)
189 #define RExC_uni_semantics (pRExC_state->uni_semantics)
190 #define RExC_orig_utf8 (pRExC_state->orig_utf8)
191 #define RExC_open_parens (pRExC_state->open_parens)
192 #define RExC_close_parens (pRExC_state->close_parens)
193 #define RExC_opend (pRExC_state->opend)
194 #define RExC_paren_names (pRExC_state->paren_names)
195 #define RExC_recurse (pRExC_state->recurse)
196 #define RExC_recurse_count (pRExC_state->recurse_count)
197 #define RExC_in_lookbehind (pRExC_state->in_lookbehind)
198 #define RExC_contains_locale (pRExC_state->contains_locale)
199 #define RExC_override_recoding (pRExC_state->override_recoding)
202 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
203 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
204 ((*s) == '{' && regcurly(s)))
207 #undef SPSTART /* dratted cpp namespace... */
210 * Flags to be passed up and down.
212 #define WORST 0 /* Worst case. */
213 #define HASWIDTH 0x01 /* Known to match non-null strings. */
215 /* Simple enough to be STAR/PLUS operand, in an EXACT node must be a single
216 * character, and if utf8, must be invariant. Note that this is not the same thing as REGNODE_SIMPLE */
218 #define SPSTART 0x04 /* Starts with * or +. */
219 #define TRYAGAIN 0x08 /* Weeded out a declaration. */
220 #define POSTPONED 0x10 /* (?1),(?&name), (??{...}) or similar */
222 #define REG_NODE_NUM(x) ((x) ? (int)((x)-RExC_emit_start) : -1)
224 /* whether trie related optimizations are enabled */
225 #if PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION
226 #define TRIE_STUDY_OPT
227 #define FULL_TRIE_STUDY
233 #define PBYTE(u8str,paren) ((U8*)(u8str))[(paren) >> 3]
234 #define PBITVAL(paren) (1 << ((paren) & 7))
235 #define PAREN_TEST(u8str,paren) ( PBYTE(u8str,paren) & PBITVAL(paren))
236 #define PAREN_SET(u8str,paren) PBYTE(u8str,paren) |= PBITVAL(paren)
237 #define PAREN_UNSET(u8str,paren) PBYTE(u8str,paren) &= (~PBITVAL(paren))
239 /* If not already in utf8, do a longjmp back to the beginning */
240 #define UTF8_LONGJMP 42 /* Choose a value not likely to ever conflict */
241 #define REQUIRE_UTF8 STMT_START { \
242 if (! UTF) JMPENV_JUMP(UTF8_LONGJMP); \
245 /* About scan_data_t.
247 During optimisation we recurse through the regexp program performing
248 various inplace (keyhole style) optimisations. In addition study_chunk
249 and scan_commit populate this data structure with information about
250 what strings MUST appear in the pattern. We look for the longest
251 string that must appear at a fixed location, and we look for the
252 longest string that may appear at a floating location. So for instance
257 Both 'FOO' and 'A' are fixed strings. Both 'B' and 'BAR' are floating
258 strings (because they follow a .* construct). study_chunk will identify
259 both FOO and BAR as being the longest fixed and floating strings respectively.
261 The strings can be composites, for instance
265 will result in a composite fixed substring 'foo'.
267 For each string some basic information is maintained:
269 - offset or min_offset
270 This is the position the string must appear at, or not before.
271 It also implicitly (when combined with minlenp) tells us how many
272 characters must match before the string we are searching for.
273 Likewise when combined with minlenp and the length of the string it
274 tells us how many characters must appear after the string we have
278 Only used for floating strings. This is the rightmost point that
279 the string can appear at. If set to I32 max it indicates that the
280 string can occur infinitely far to the right.
283 A pointer to the minimum length of the pattern that the string
284 was found inside. This is important as in the case of positive
285 lookahead or positive lookbehind we can have multiple patterns
290 The minimum length of the pattern overall is 3, the minimum length
291 of the lookahead part is 3, but the minimum length of the part that
292 will actually match is 1. So 'FOO's minimum length is 3, but the
293 minimum length for the F is 1. This is important as the minimum length
294 is used to determine offsets in front of and behind the string being
295 looked for. Since strings can be composites this is the length of the
296 pattern at the time it was committed with a scan_commit. Note that
297 the length is calculated by study_chunk, so that the minimum lengths
298 are not known until the full pattern has been compiled, thus the
299 pointer to the value.
303 In the case of lookbehind the string being searched for can be
304 offset past the start point of the final matching string.
305 If this value was just blithely removed from the min_offset it would
306 invalidate some of the calculations for how many chars must match
307 before or after (as they are derived from min_offset and minlen and
308 the length of the string being searched for).
309 When the final pattern is compiled and the data is moved from the
310 scan_data_t structure into the regexp structure the information
311 about lookbehind is factored in, with the information that would
312 have been lost precalculated in the end_shift field for the
315 The fields pos_min and pos_delta are used to store the minimum offset
316 and the delta to the maximum offset at the current point in the pattern.
320 typedef struct scan_data_t {
321 /*I32 len_min; unused */
322 /*I32 len_delta; unused */
326 I32 last_end; /* min value, <0 unless valid. */
329 SV **longest; /* Either &l_fixed, or &l_float. */
330 SV *longest_fixed; /* longest fixed string found in pattern */
331 I32 offset_fixed; /* offset where it starts */
332 I32 *minlen_fixed; /* pointer to the minlen relevant to the string */
333 I32 lookbehind_fixed; /* is the position of the string modfied by LB */
334 SV *longest_float; /* longest floating string found in pattern */
335 I32 offset_float_min; /* earliest point in string it can appear */
336 I32 offset_float_max; /* latest point in string it can appear */
337 I32 *minlen_float; /* pointer to the minlen relevant to the string */
338 I32 lookbehind_float; /* is the position of the string modified by LB */
342 struct regnode_charclass_class *start_class;
346 * Forward declarations for pregcomp()'s friends.
349 static const scan_data_t zero_scan_data =
350 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
352 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
353 #define SF_BEFORE_SEOL 0x0001
354 #define SF_BEFORE_MEOL 0x0002
355 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
356 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
359 # define SF_FIX_SHIFT_EOL (0+2)
360 # define SF_FL_SHIFT_EOL (0+4)
362 # define SF_FIX_SHIFT_EOL (+2)
363 # define SF_FL_SHIFT_EOL (+4)
366 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
367 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
369 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
370 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
371 #define SF_IS_INF 0x0040
372 #define SF_HAS_PAR 0x0080
373 #define SF_IN_PAR 0x0100
374 #define SF_HAS_EVAL 0x0200
375 #define SCF_DO_SUBSTR 0x0400
376 #define SCF_DO_STCLASS_AND 0x0800
377 #define SCF_DO_STCLASS_OR 0x1000
378 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
379 #define SCF_WHILEM_VISITED_POS 0x2000
381 #define SCF_TRIE_RESTUDY 0x4000 /* Do restudy? */
382 #define SCF_SEEN_ACCEPT 0x8000
384 #define UTF cBOOL(RExC_utf8)
385 #define LOC (get_regex_charset(RExC_flags) == REGEX_LOCALE_CHARSET)
386 #define UNI_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_UNICODE_CHARSET)
387 #define DEPENDS_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_DEPENDS_CHARSET)
388 #define AT_LEAST_UNI_SEMANTICS (get_regex_charset(RExC_flags) >= REGEX_UNICODE_CHARSET)
389 #define ASCII_RESTRICTED (get_regex_charset(RExC_flags) == REGEX_ASCII_RESTRICTED_CHARSET)
390 #define MORE_ASCII_RESTRICTED (get_regex_charset(RExC_flags) == REGEX_ASCII_MORE_RESTRICTED_CHARSET)
391 #define AT_LEAST_ASCII_RESTRICTED (get_regex_charset(RExC_flags) >= REGEX_ASCII_RESTRICTED_CHARSET)
393 #define FOLD cBOOL(RExC_flags & RXf_PMf_FOLD)
395 #define OOB_UNICODE 12345678
396 #define OOB_NAMEDCLASS -1
398 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
399 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
402 /* length of regex to show in messages that don't mark a position within */
403 #define RegexLengthToShowInErrorMessages 127
406 * If MARKER[12] are adjusted, be sure to adjust the constants at the top
407 * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
408 * op/pragma/warn/regcomp.
410 #define MARKER1 "<-- HERE" /* marker as it appears in the description */
411 #define MARKER2 " <-- HERE " /* marker as it appears within the regex */
413 #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/"
416 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
417 * arg. Show regex, up to a maximum length. If it's too long, chop and add
420 #define _FAIL(code) STMT_START { \
421 const char *ellipses = ""; \
422 IV len = RExC_end - RExC_precomp; \
425 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
426 if (len > RegexLengthToShowInErrorMessages) { \
427 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
428 len = RegexLengthToShowInErrorMessages - 10; \
434 #define FAIL(msg) _FAIL( \
435 Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
436 msg, (int)len, RExC_precomp, ellipses))
438 #define FAIL2(msg,arg) _FAIL( \
439 Perl_croak(aTHX_ msg " in regex m/%.*s%s/", \
440 arg, (int)len, RExC_precomp, ellipses))
443 * Simple_vFAIL -- like FAIL, but marks the current location in the scan
445 #define Simple_vFAIL(m) STMT_START { \
446 const IV offset = RExC_parse - RExC_precomp; \
447 Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
448 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
452 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
454 #define vFAIL(m) STMT_START { \
456 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
461 * Like Simple_vFAIL(), but accepts two arguments.
463 #define Simple_vFAIL2(m,a1) STMT_START { \
464 const IV offset = RExC_parse - RExC_precomp; \
465 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
466 (int)offset, RExC_precomp, RExC_precomp + offset); \
470 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
472 #define vFAIL2(m,a1) STMT_START { \
474 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
475 Simple_vFAIL2(m, a1); \
480 * Like Simple_vFAIL(), but accepts three arguments.
482 #define Simple_vFAIL3(m, a1, a2) STMT_START { \
483 const IV offset = RExC_parse - RExC_precomp; \
484 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
485 (int)offset, RExC_precomp, RExC_precomp + offset); \
489 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
491 #define vFAIL3(m,a1,a2) STMT_START { \
493 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \
494 Simple_vFAIL3(m, a1, a2); \
498 * Like Simple_vFAIL(), but accepts four arguments.
500 #define Simple_vFAIL4(m, a1, a2, a3) STMT_START { \
501 const IV offset = RExC_parse - RExC_precomp; \
502 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, \
503 (int)offset, RExC_precomp, RExC_precomp + offset); \
506 #define ckWARNreg(loc,m) STMT_START { \
507 const IV offset = loc - RExC_precomp; \
508 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
509 (int)offset, RExC_precomp, RExC_precomp + offset); \
512 #define ckWARNregdep(loc,m) STMT_START { \
513 const IV offset = loc - RExC_precomp; \
514 Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), \
516 (int)offset, RExC_precomp, RExC_precomp + offset); \
519 #define ckWARN2regdep(loc,m, a1) STMT_START { \
520 const IV offset = loc - RExC_precomp; \
521 Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), \
523 a1, (int)offset, RExC_precomp, RExC_precomp + offset); \
526 #define ckWARN2reg(loc, m, a1) STMT_START { \
527 const IV offset = loc - RExC_precomp; \
528 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
529 a1, (int)offset, RExC_precomp, RExC_precomp + offset); \
532 #define vWARN3(loc, m, a1, a2) STMT_START { \
533 const IV offset = loc - RExC_precomp; \
534 Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
535 a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \
538 #define ckWARN3reg(loc, m, a1, a2) STMT_START { \
539 const IV offset = loc - RExC_precomp; \
540 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
541 a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \
544 #define vWARN4(loc, m, a1, a2, a3) STMT_START { \
545 const IV offset = loc - RExC_precomp; \
546 Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
547 a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
550 #define ckWARN4reg(loc, m, a1, a2, a3) STMT_START { \
551 const IV offset = loc - RExC_precomp; \
552 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
553 a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
556 #define vWARN5(loc, m, a1, a2, a3, a4) STMT_START { \
557 const IV offset = loc - RExC_precomp; \
558 Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
559 a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \
563 /* Allow for side effects in s */
564 #define REGC(c,s) STMT_START { \
565 if (!SIZE_ONLY) *(s) = (c); else (void)(s); \
568 /* Macros for recording node offsets. 20001227 mjd@plover.com
569 * Nodes are numbered 1, 2, 3, 4. Node #n's position is recorded in
570 * element 2*n-1 of the array. Element #2n holds the byte length node #n.
571 * Element 0 holds the number n.
572 * Position is 1 indexed.
574 #ifndef RE_TRACK_PATTERN_OFFSETS
575 #define Set_Node_Offset_To_R(node,byte)
576 #define Set_Node_Offset(node,byte)
577 #define Set_Cur_Node_Offset
578 #define Set_Node_Length_To_R(node,len)
579 #define Set_Node_Length(node,len)
580 #define Set_Node_Cur_Length(node)
581 #define Node_Offset(n)
582 #define Node_Length(n)
583 #define Set_Node_Offset_Length(node,offset,len)
584 #define ProgLen(ri) ri->u.proglen
585 #define SetProgLen(ri,x) ri->u.proglen = x
587 #define ProgLen(ri) ri->u.offsets[0]
588 #define SetProgLen(ri,x) ri->u.offsets[0] = x
589 #define Set_Node_Offset_To_R(node,byte) STMT_START { \
591 MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n", \
592 __LINE__, (int)(node), (int)(byte))); \
594 Perl_croak(aTHX_ "value of node is %d in Offset macro", (int)(node)); \
596 RExC_offsets[2*(node)-1] = (byte); \
601 #define Set_Node_Offset(node,byte) \
602 Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
603 #define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
605 #define Set_Node_Length_To_R(node,len) STMT_START { \
607 MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n", \
608 __LINE__, (int)(node), (int)(len))); \
610 Perl_croak(aTHX_ "value of node is %d in Length macro", (int)(node)); \
612 RExC_offsets[2*(node)] = (len); \
617 #define Set_Node_Length(node,len) \
618 Set_Node_Length_To_R((node)-RExC_emit_start, len)
619 #define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
620 #define Set_Node_Cur_Length(node) \
621 Set_Node_Length(node, RExC_parse - parse_start)
623 /* Get offsets and lengths */
624 #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
625 #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)])
627 #define Set_Node_Offset_Length(node,offset,len) STMT_START { \
628 Set_Node_Offset_To_R((node)-RExC_emit_start, (offset)); \
629 Set_Node_Length_To_R((node)-RExC_emit_start, (len)); \
633 #if PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS
634 #define EXPERIMENTAL_INPLACESCAN
635 #endif /*PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS*/
637 #define DEBUG_STUDYDATA(str,data,depth) \
638 DEBUG_OPTIMISE_MORE_r(if(data){ \
639 PerlIO_printf(Perl_debug_log, \
640 "%*s" str "Pos:%"IVdf"/%"IVdf \
641 " Flags: 0x%"UVXf" Whilem_c: %"IVdf" Lcp: %"IVdf" %s", \
642 (int)(depth)*2, "", \
643 (IV)((data)->pos_min), \
644 (IV)((data)->pos_delta), \
645 (UV)((data)->flags), \
646 (IV)((data)->whilem_c), \
647 (IV)((data)->last_closep ? *((data)->last_closep) : -1), \
648 is_inf ? "INF " : "" \
650 if ((data)->last_found) \
651 PerlIO_printf(Perl_debug_log, \
652 "Last:'%s' %"IVdf":%"IVdf"/%"IVdf" %sFixed:'%s' @ %"IVdf \
653 " %sFloat: '%s' @ %"IVdf"/%"IVdf"", \
654 SvPVX_const((data)->last_found), \
655 (IV)((data)->last_end), \
656 (IV)((data)->last_start_min), \
657 (IV)((data)->last_start_max), \
658 ((data)->longest && \
659 (data)->longest==&((data)->longest_fixed)) ? "*" : "", \
660 SvPVX_const((data)->longest_fixed), \
661 (IV)((data)->offset_fixed), \
662 ((data)->longest && \
663 (data)->longest==&((data)->longest_float)) ? "*" : "", \
664 SvPVX_const((data)->longest_float), \
665 (IV)((data)->offset_float_min), \
666 (IV)((data)->offset_float_max) \
668 PerlIO_printf(Perl_debug_log,"\n"); \
671 static void clear_re(pTHX_ void *r);
673 /* Mark that we cannot extend a found fixed substring at this point.
674 Update the longest found anchored substring and the longest found
675 floating substrings if needed. */
678 S_scan_commit(pTHX_ const RExC_state_t *pRExC_state, scan_data_t *data, I32 *minlenp, int is_inf)
680 const STRLEN l = CHR_SVLEN(data->last_found);
681 const STRLEN old_l = CHR_SVLEN(*data->longest);
682 GET_RE_DEBUG_FLAGS_DECL;
684 PERL_ARGS_ASSERT_SCAN_COMMIT;
686 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
687 SvSetMagicSV(*data->longest, data->last_found);
688 if (*data->longest == data->longest_fixed) {
689 data->offset_fixed = l ? data->last_start_min : data->pos_min;
690 if (data->flags & SF_BEFORE_EOL)
692 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
694 data->flags &= ~SF_FIX_BEFORE_EOL;
695 data->minlen_fixed=minlenp;
696 data->lookbehind_fixed=0;
698 else { /* *data->longest == data->longest_float */
699 data->offset_float_min = l ? data->last_start_min : data->pos_min;
700 data->offset_float_max = (l
701 ? data->last_start_max
702 : data->pos_min + data->pos_delta);
703 if (is_inf || (U32)data->offset_float_max > (U32)I32_MAX)
704 data->offset_float_max = I32_MAX;
705 if (data->flags & SF_BEFORE_EOL)
707 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
709 data->flags &= ~SF_FL_BEFORE_EOL;
710 data->minlen_float=minlenp;
711 data->lookbehind_float=0;
714 SvCUR_set(data->last_found, 0);
716 SV * const sv = data->last_found;
717 if (SvUTF8(sv) && SvMAGICAL(sv)) {
718 MAGIC * const mg = mg_find(sv, PERL_MAGIC_utf8);
724 data->flags &= ~SF_BEFORE_EOL;
725 DEBUG_STUDYDATA("commit: ",data,0);
728 /* Can match anything (initialization) */
730 S_cl_anything(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
732 PERL_ARGS_ASSERT_CL_ANYTHING;
734 ANYOF_BITMAP_SETALL(cl);
735 cl->flags = ANYOF_CLASS|ANYOF_EOS|ANYOF_UNICODE_ALL
736 |ANYOF_LOC_NONBITMAP_FOLD|ANYOF_NON_UTF8_LATIN1_ALL;
738 /* If any portion of the regex is to operate under locale rules,
739 * initialization includes it. The reason this isn't done for all regexes
740 * is that the optimizer was written under the assumption that locale was
741 * all-or-nothing. Given the complexity and lack of documentation in the
742 * optimizer, and that there are inadequate test cases for locale, so many
743 * parts of it may not work properly, it is safest to avoid locale unless
745 if (RExC_contains_locale) {
746 ANYOF_CLASS_SETALL(cl); /* /l uses class */
747 cl->flags |= ANYOF_LOCALE;
750 ANYOF_CLASS_ZERO(cl); /* Only /l uses class now */
754 /* Can match anything (initialization) */
756 S_cl_is_anything(const struct regnode_charclass_class *cl)
760 PERL_ARGS_ASSERT_CL_IS_ANYTHING;
762 for (value = 0; value <= ANYOF_MAX; value += 2)
763 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
765 if (!(cl->flags & ANYOF_UNICODE_ALL))
767 if (!ANYOF_BITMAP_TESTALLSET((const void*)cl))
772 /* Can match anything (initialization) */
774 S_cl_init(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
776 PERL_ARGS_ASSERT_CL_INIT;
778 Zero(cl, 1, struct regnode_charclass_class);
780 cl_anything(pRExC_state, cl);
781 ARG_SET(cl, ANYOF_NONBITMAP_EMPTY);
784 /* These two functions currently do the exact same thing */
785 #define cl_init_zero S_cl_init
787 /* 'AND' a given class with another one. Can create false positives. 'cl'
788 * should not be inverted. 'and_with->flags & ANYOF_CLASS' should be 0 if
789 * 'and_with' is a regnode_charclass instead of a regnode_charclass_class. */
791 S_cl_and(struct regnode_charclass_class *cl,
792 const struct regnode_charclass_class *and_with)
794 PERL_ARGS_ASSERT_CL_AND;
796 assert(and_with->type == ANYOF);
798 /* I (khw) am not sure all these restrictions are necessary XXX */
799 if (!(ANYOF_CLASS_TEST_ANY_SET(and_with))
800 && !(ANYOF_CLASS_TEST_ANY_SET(cl))
801 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
802 && !(and_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
803 && !(cl->flags & ANYOF_LOC_NONBITMAP_FOLD)) {
806 if (and_with->flags & ANYOF_INVERT)
807 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
808 cl->bitmap[i] &= ~and_with->bitmap[i];
810 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
811 cl->bitmap[i] &= and_with->bitmap[i];
812 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
814 if (and_with->flags & ANYOF_INVERT) {
816 /* Here, the and'ed node is inverted. Get the AND of the flags that
817 * aren't affected by the inversion. Those that are affected are
818 * handled individually below */
819 U8 affected_flags = cl->flags & ~INVERSION_UNAFFECTED_FLAGS;
820 cl->flags &= (and_with->flags & INVERSION_UNAFFECTED_FLAGS);
821 cl->flags |= affected_flags;
823 /* We currently don't know how to deal with things that aren't in the
824 * bitmap, but we know that the intersection is no greater than what
825 * is already in cl, so let there be false positives that get sorted
826 * out after the synthetic start class succeeds, and the node is
827 * matched for real. */
829 /* The inversion of these two flags indicate that the resulting
830 * intersection doesn't have them */
831 if (and_with->flags & ANYOF_UNICODE_ALL) {
832 cl->flags &= ~ANYOF_UNICODE_ALL;
834 if (and_with->flags & ANYOF_NON_UTF8_LATIN1_ALL) {
835 cl->flags &= ~ANYOF_NON_UTF8_LATIN1_ALL;
838 else { /* and'd node is not inverted */
839 U8 outside_bitmap_but_not_utf8; /* Temp variable */
841 if (! ANYOF_NONBITMAP(and_with)) {
843 /* Here 'and_with' doesn't match anything outside the bitmap
844 * (except possibly ANYOF_UNICODE_ALL), which means the
845 * intersection can't either, except for ANYOF_UNICODE_ALL, in
846 * which case we don't know what the intersection is, but it's no
847 * greater than what cl already has, so can just leave it alone,
848 * with possible false positives */
849 if (! (and_with->flags & ANYOF_UNICODE_ALL)) {
850 ARG_SET(cl, ANYOF_NONBITMAP_EMPTY);
851 cl->flags &= ~ANYOF_NONBITMAP_NON_UTF8;
854 else if (! ANYOF_NONBITMAP(cl)) {
856 /* Here, 'and_with' does match something outside the bitmap, and cl
857 * doesn't have a list of things to match outside the bitmap. If
858 * cl can match all code points above 255, the intersection will
859 * be those above-255 code points that 'and_with' matches. If cl
860 * can't match all Unicode code points, it means that it can't
861 * match anything outside the bitmap (since the 'if' that got us
862 * into this block tested for that), so we leave the bitmap empty.
864 if (cl->flags & ANYOF_UNICODE_ALL) {
865 ARG_SET(cl, ARG(and_with));
867 /* and_with's ARG may match things that don't require UTF8.
868 * And now cl's will too, in spite of this being an 'and'. See
869 * the comments below about the kludge */
870 cl->flags |= and_with->flags & ANYOF_NONBITMAP_NON_UTF8;
874 /* Here, both 'and_with' and cl match something outside the
875 * bitmap. Currently we do not do the intersection, so just match
876 * whatever cl had at the beginning. */
880 /* Take the intersection of the two sets of flags. However, the
881 * ANYOF_NONBITMAP_NON_UTF8 flag is treated as an 'or'. This is a
882 * kludge around the fact that this flag is not treated like the others
883 * which are initialized in cl_anything(). The way the optimizer works
884 * is that the synthetic start class (SSC) is initialized to match
885 * anything, and then the first time a real node is encountered, its
886 * values are AND'd with the SSC's with the result being the values of
887 * the real node. However, there are paths through the optimizer where
888 * the AND never gets called, so those initialized bits are set
889 * inappropriately, which is not usually a big deal, as they just cause
890 * false positives in the SSC, which will just mean a probably
891 * imperceptible slow down in execution. However this bit has a
892 * higher false positive consequence in that it can cause utf8.pm,
893 * utf8_heavy.pl ... to be loaded when not necessary, which is a much
894 * bigger slowdown and also causes significant extra memory to be used.
895 * In order to prevent this, the code now takes a different tack. The
896 * bit isn't set unless some part of the regular expression needs it,
897 * but once set it won't get cleared. This means that these extra
898 * modules won't get loaded unless there was some path through the
899 * pattern that would have required them anyway, and so any false
900 * positives that occur by not ANDing them out when they could be
901 * aren't as severe as they would be if we treated this bit like all
903 outside_bitmap_but_not_utf8 = (cl->flags | and_with->flags)
904 & ANYOF_NONBITMAP_NON_UTF8;
905 cl->flags &= and_with->flags;
906 cl->flags |= outside_bitmap_but_not_utf8;
910 /* 'OR' a given class with another one. Can create false positives. 'cl'
911 * should not be inverted. 'or_with->flags & ANYOF_CLASS' should be 0 if
912 * 'or_with' is a regnode_charclass instead of a regnode_charclass_class. */
914 S_cl_or(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, const struct regnode_charclass_class *or_with)
916 PERL_ARGS_ASSERT_CL_OR;
918 if (or_with->flags & ANYOF_INVERT) {
920 /* Here, the or'd node is to be inverted. This means we take the
921 * complement of everything not in the bitmap, but currently we don't
922 * know what that is, so give up and match anything */
923 if (ANYOF_NONBITMAP(or_with)) {
924 cl_anything(pRExC_state, cl);
927 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
928 * <= (B1 | !B2) | (CL1 | !CL2)
929 * which is wasteful if CL2 is small, but we ignore CL2:
930 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
931 * XXXX Can we handle case-fold? Unclear:
932 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
933 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
935 else if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
936 && !(or_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
937 && !(cl->flags & ANYOF_LOC_NONBITMAP_FOLD) ) {
940 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
941 cl->bitmap[i] |= ~or_with->bitmap[i];
942 } /* XXXX: logic is complicated otherwise */
944 cl_anything(pRExC_state, cl);
947 /* And, we can just take the union of the flags that aren't affected
948 * by the inversion */
949 cl->flags |= or_with->flags & INVERSION_UNAFFECTED_FLAGS;
951 /* For the remaining flags:
952 ANYOF_UNICODE_ALL and inverted means to not match anything above
953 255, which means that the union with cl should just be
954 what cl has in it, so can ignore this flag
955 ANYOF_NON_UTF8_LATIN1_ALL and inverted means if not utf8 and ord
956 is 127-255 to match them, but then invert that, so the
957 union with cl should just be what cl has in it, so can
960 } else { /* 'or_with' is not inverted */
961 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
962 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
963 && (!(or_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
964 || (cl->flags & ANYOF_LOC_NONBITMAP_FOLD)) ) {
967 /* OR char bitmap and class bitmap separately */
968 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
969 cl->bitmap[i] |= or_with->bitmap[i];
970 if (ANYOF_CLASS_TEST_ANY_SET(or_with)) {
971 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
972 cl->classflags[i] |= or_with->classflags[i];
973 cl->flags |= ANYOF_CLASS;
976 else { /* XXXX: logic is complicated, leave it along for a moment. */
977 cl_anything(pRExC_state, cl);
980 if (ANYOF_NONBITMAP(or_with)) {
982 /* Use the added node's outside-the-bit-map match if there isn't a
983 * conflict. If there is a conflict (both nodes match something
984 * outside the bitmap, but what they match outside is not the same
985 * pointer, and hence not easily compared until XXX we extend
986 * inversion lists this far), give up and allow the start class to
987 * match everything outside the bitmap. If that stuff is all above
988 * 255, can just set UNICODE_ALL, otherwise caould be anything. */
989 if (! ANYOF_NONBITMAP(cl)) {
990 ARG_SET(cl, ARG(or_with));
992 else if (ARG(cl) != ARG(or_with)) {
994 if ((or_with->flags & ANYOF_NONBITMAP_NON_UTF8)) {
995 cl_anything(pRExC_state, cl);
998 cl->flags |= ANYOF_UNICODE_ALL;
1003 /* Take the union */
1004 cl->flags |= or_with->flags;
1008 #define TRIE_LIST_ITEM(state,idx) (trie->states[state].trans.list)[ idx ]
1009 #define TRIE_LIST_CUR(state) ( TRIE_LIST_ITEM( state, 0 ).forid )
1010 #define TRIE_LIST_LEN(state) ( TRIE_LIST_ITEM( state, 0 ).newstate )
1011 #define TRIE_LIST_USED(idx) ( trie->states[state].trans.list ? (TRIE_LIST_CUR( idx ) - 1) : 0 )
1016 dump_trie(trie,widecharmap,revcharmap)
1017 dump_trie_interim_list(trie,widecharmap,revcharmap,next_alloc)
1018 dump_trie_interim_table(trie,widecharmap,revcharmap,next_alloc)
1020 These routines dump out a trie in a somewhat readable format.
1021 The _interim_ variants are used for debugging the interim
1022 tables that are used to generate the final compressed
1023 representation which is what dump_trie expects.
1025 Part of the reason for their existence is to provide a form
1026 of documentation as to how the different representations function.
1031 Dumps the final compressed table form of the trie to Perl_debug_log.
1032 Used for debugging make_trie().
1036 S_dump_trie(pTHX_ const struct _reg_trie_data *trie, HV *widecharmap,
1037 AV *revcharmap, U32 depth)
1040 SV *sv=sv_newmortal();
1041 int colwidth= widecharmap ? 6 : 4;
1043 GET_RE_DEBUG_FLAGS_DECL;
1045 PERL_ARGS_ASSERT_DUMP_TRIE;
1047 PerlIO_printf( Perl_debug_log, "%*sChar : %-6s%-6s%-4s ",
1048 (int)depth * 2 + 2,"",
1049 "Match","Base","Ofs" );
1051 for( state = 0 ; state < trie->uniquecharcount ; state++ ) {
1052 SV ** const tmp = av_fetch( revcharmap, state, 0);
1054 PerlIO_printf( Perl_debug_log, "%*s",
1056 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth,
1057 PL_colors[0], PL_colors[1],
1058 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1059 PERL_PV_ESCAPE_FIRSTCHAR
1064 PerlIO_printf( Perl_debug_log, "\n%*sState|-----------------------",
1065 (int)depth * 2 + 2,"");
1067 for( state = 0 ; state < trie->uniquecharcount ; state++ )
1068 PerlIO_printf( Perl_debug_log, "%.*s", colwidth, "--------");
1069 PerlIO_printf( Perl_debug_log, "\n");
1071 for( state = 1 ; state < trie->statecount ; state++ ) {
1072 const U32 base = trie->states[ state ].trans.base;
1074 PerlIO_printf( Perl_debug_log, "%*s#%4"UVXf"|", (int)depth * 2 + 2,"", (UV)state);
1076 if ( trie->states[ state ].wordnum ) {
1077 PerlIO_printf( Perl_debug_log, " W%4X", trie->states[ state ].wordnum );
1079 PerlIO_printf( Perl_debug_log, "%6s", "" );
1082 PerlIO_printf( Perl_debug_log, " @%4"UVXf" ", (UV)base );
1087 while( ( base + ofs < trie->uniquecharcount ) ||
1088 ( base + ofs - trie->uniquecharcount < trie->lasttrans
1089 && trie->trans[ base + ofs - trie->uniquecharcount ].check != state))
1092 PerlIO_printf( Perl_debug_log, "+%2"UVXf"[ ", (UV)ofs);
1094 for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
1095 if ( ( base + ofs >= trie->uniquecharcount ) &&
1096 ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
1097 trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
1099 PerlIO_printf( Perl_debug_log, "%*"UVXf,
1101 (UV)trie->trans[ base + ofs - trie->uniquecharcount ].next );
1103 PerlIO_printf( Perl_debug_log, "%*s",colwidth," ." );
1107 PerlIO_printf( Perl_debug_log, "]");
1110 PerlIO_printf( Perl_debug_log, "\n" );
1112 PerlIO_printf(Perl_debug_log, "%*sword_info N:(prev,len)=", (int)depth*2, "");
1113 for (word=1; word <= trie->wordcount; word++) {
1114 PerlIO_printf(Perl_debug_log, " %d:(%d,%d)",
1115 (int)word, (int)(trie->wordinfo[word].prev),
1116 (int)(trie->wordinfo[word].len));
1118 PerlIO_printf(Perl_debug_log, "\n" );
1121 Dumps a fully constructed but uncompressed trie in list form.
1122 List tries normally only are used for construction when the number of
1123 possible chars (trie->uniquecharcount) is very high.
1124 Used for debugging make_trie().
1127 S_dump_trie_interim_list(pTHX_ const struct _reg_trie_data *trie,
1128 HV *widecharmap, AV *revcharmap, U32 next_alloc,
1132 SV *sv=sv_newmortal();
1133 int colwidth= widecharmap ? 6 : 4;
1134 GET_RE_DEBUG_FLAGS_DECL;
1136 PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_LIST;
1138 /* print out the table precompression. */
1139 PerlIO_printf( Perl_debug_log, "%*sState :Word | Transition Data\n%*s%s",
1140 (int)depth * 2 + 2,"", (int)depth * 2 + 2,"",
1141 "------:-----+-----------------\n" );
1143 for( state=1 ; state < next_alloc ; state ++ ) {
1146 PerlIO_printf( Perl_debug_log, "%*s %4"UVXf" :",
1147 (int)depth * 2 + 2,"", (UV)state );
1148 if ( ! trie->states[ state ].wordnum ) {
1149 PerlIO_printf( Perl_debug_log, "%5s| ","");
1151 PerlIO_printf( Perl_debug_log, "W%4x| ",
1152 trie->states[ state ].wordnum
1155 for( charid = 1 ; charid <= TRIE_LIST_USED( state ) ; charid++ ) {
1156 SV ** const tmp = av_fetch( revcharmap, TRIE_LIST_ITEM(state,charid).forid, 0);
1158 PerlIO_printf( Perl_debug_log, "%*s:%3X=%4"UVXf" | ",
1160 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth,
1161 PL_colors[0], PL_colors[1],
1162 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1163 PERL_PV_ESCAPE_FIRSTCHAR
1165 TRIE_LIST_ITEM(state,charid).forid,
1166 (UV)TRIE_LIST_ITEM(state,charid).newstate
1169 PerlIO_printf(Perl_debug_log, "\n%*s| ",
1170 (int)((depth * 2) + 14), "");
1173 PerlIO_printf( Perl_debug_log, "\n");
1178 Dumps a fully constructed but uncompressed trie in table form.
1179 This is the normal DFA style state transition table, with a few
1180 twists to facilitate compression later.
1181 Used for debugging make_trie().
1184 S_dump_trie_interim_table(pTHX_ const struct _reg_trie_data *trie,
1185 HV *widecharmap, AV *revcharmap, U32 next_alloc,
1190 SV *sv=sv_newmortal();
1191 int colwidth= widecharmap ? 6 : 4;
1192 GET_RE_DEBUG_FLAGS_DECL;
1194 PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_TABLE;
1197 print out the table precompression so that we can do a visual check
1198 that they are identical.
1201 PerlIO_printf( Perl_debug_log, "%*sChar : ",(int)depth * 2 + 2,"" );
1203 for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1204 SV ** const tmp = av_fetch( revcharmap, charid, 0);
1206 PerlIO_printf( Perl_debug_log, "%*s",
1208 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth,
1209 PL_colors[0], PL_colors[1],
1210 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1211 PERL_PV_ESCAPE_FIRSTCHAR
1217 PerlIO_printf( Perl_debug_log, "\n%*sState+-",(int)depth * 2 + 2,"" );
1219 for( charid=0 ; charid < trie->uniquecharcount ; charid++ ) {
1220 PerlIO_printf( Perl_debug_log, "%.*s", colwidth,"--------");
1223 PerlIO_printf( Perl_debug_log, "\n" );
1225 for( state=1 ; state < next_alloc ; state += trie->uniquecharcount ) {
1227 PerlIO_printf( Perl_debug_log, "%*s%4"UVXf" : ",
1228 (int)depth * 2 + 2,"",
1229 (UV)TRIE_NODENUM( state ) );
1231 for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1232 UV v=(UV)SAFE_TRIE_NODENUM( trie->trans[ state + charid ].next );
1234 PerlIO_printf( Perl_debug_log, "%*"UVXf, colwidth, v );
1236 PerlIO_printf( Perl_debug_log, "%*s", colwidth, "." );
1238 if ( ! trie->states[ TRIE_NODENUM( state ) ].wordnum ) {
1239 PerlIO_printf( Perl_debug_log, " (%4"UVXf")\n", (UV)trie->trans[ state ].check );
1241 PerlIO_printf( Perl_debug_log, " (%4"UVXf") W%4X\n", (UV)trie->trans[ state ].check,
1242 trie->states[ TRIE_NODENUM( state ) ].wordnum );
1250 /* make_trie(startbranch,first,last,tail,word_count,flags,depth)
1251 startbranch: the first branch in the whole branch sequence
1252 first : start branch of sequence of branch-exact nodes.
1253 May be the same as startbranch
1254 last : Thing following the last branch.
1255 May be the same as tail.
1256 tail : item following the branch sequence
1257 count : words in the sequence
1258 flags : currently the OP() type we will be building one of /EXACT(|F|Fl)/
1259 depth : indent depth
1261 Inplace optimizes a sequence of 2 or more Branch-Exact nodes into a TRIE node.
1263 A trie is an N'ary tree where the branches are determined by digital
1264 decomposition of the key. IE, at the root node you look up the 1st character and
1265 follow that branch repeat until you find the end of the branches. Nodes can be
1266 marked as "accepting" meaning they represent a complete word. Eg:
1270 would convert into the following structure. Numbers represent states, letters
1271 following numbers represent valid transitions on the letter from that state, if
1272 the number is in square brackets it represents an accepting state, otherwise it
1273 will be in parenthesis.
1275 +-h->+-e->[3]-+-r->(8)-+-s->[9]
1279 (1) +-i->(6)-+-s->[7]
1281 +-s->(3)-+-h->(4)-+-e->[5]
1283 Accept Word Mapping: 3=>1 (he),5=>2 (she), 7=>3 (his), 9=>4 (hers)
1285 This shows that when matching against the string 'hers' we will begin at state 1
1286 read 'h' and move to state 2, read 'e' and move to state 3 which is accepting,
1287 then read 'r' and go to state 8 followed by 's' which takes us to state 9 which
1288 is also accepting. Thus we know that we can match both 'he' and 'hers' with a
1289 single traverse. We store a mapping from accepting to state to which word was
1290 matched, and then when we have multiple possibilities we try to complete the
1291 rest of the regex in the order in which they occured in the alternation.
1293 The only prior NFA like behaviour that would be changed by the TRIE support is
1294 the silent ignoring of duplicate alternations which are of the form:
1296 / (DUPE|DUPE) X? (?{ ... }) Y /x
1298 Thus EVAL blocks following a trie may be called a different number of times with
1299 and without the optimisation. With the optimisations dupes will be silently
1300 ignored. This inconsistent behaviour of EVAL type nodes is well established as
1301 the following demonstrates:
1303 'words'=~/(word|word|word)(?{ print $1 })[xyz]/
1305 which prints out 'word' three times, but
1307 'words'=~/(word|word|word)(?{ print $1 })S/
1309 which doesnt print it out at all. This is due to other optimisations kicking in.
1311 Example of what happens on a structural level:
1313 The regexp /(ac|ad|ab)+/ will produce the following debug output:
1315 1: CURLYM[1] {1,32767}(18)
1326 This would be optimizable with startbranch=5, first=5, last=16, tail=16
1327 and should turn into:
1329 1: CURLYM[1] {1,32767}(18)
1331 [Words:3 Chars Stored:6 Unique Chars:4 States:5 NCP:1]
1339 Cases where tail != last would be like /(?foo|bar)baz/:
1349 which would be optimizable with startbranch=1, first=1, last=7, tail=8
1350 and would end up looking like:
1353 [Words:2 Chars Stored:6 Unique Chars:5 States:7 NCP:1]
1360 d = uvuni_to_utf8_flags(d, uv, 0);
1362 is the recommended Unicode-aware way of saying
1367 #define TRIE_STORE_REVCHAR \
1370 SV *zlopp = newSV(2); \
1371 unsigned char *flrbbbbb = (unsigned char *) SvPVX(zlopp); \
1372 unsigned const char *const kapow = uvuni_to_utf8(flrbbbbb, uvc & 0xFF); \
1373 SvCUR_set(zlopp, kapow - flrbbbbb); \
1376 av_push(revcharmap, zlopp); \
1378 char ooooff = (char)uvc; \
1379 av_push(revcharmap, newSVpvn(&ooooff, 1)); \
1383 #define TRIE_READ_CHAR STMT_START { \
1387 if ( foldlen > 0 ) { \
1388 uvc = utf8n_to_uvuni( scan, UTF8_MAXLEN, &len, uniflags ); \
1393 uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\
1394 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1395 foldlen -= UNISKIP( uvc ); \
1396 scan = foldbuf + UNISKIP( uvc ); \
1399 uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\
1409 #define TRIE_LIST_PUSH(state,fid,ns) STMT_START { \
1410 if ( TRIE_LIST_CUR( state ) >=TRIE_LIST_LEN( state ) ) { \
1411 U32 ging = TRIE_LIST_LEN( state ) *= 2; \
1412 Renew( trie->states[ state ].trans.list, ging, reg_trie_trans_le ); \
1414 TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).forid = fid; \
1415 TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).newstate = ns; \
1416 TRIE_LIST_CUR( state )++; \
1419 #define TRIE_LIST_NEW(state) STMT_START { \
1420 Newxz( trie->states[ state ].trans.list, \
1421 4, reg_trie_trans_le ); \
1422 TRIE_LIST_CUR( state ) = 1; \
1423 TRIE_LIST_LEN( state ) = 4; \
1426 #define TRIE_HANDLE_WORD(state) STMT_START { \
1427 U16 dupe= trie->states[ state ].wordnum; \
1428 regnode * const noper_next = regnext( noper ); \
1431 /* store the word for dumping */ \
1433 if (OP(noper) != NOTHING) \
1434 tmp = newSVpvn_utf8(STRING(noper), STR_LEN(noper), UTF); \
1436 tmp = newSVpvn_utf8( "", 0, UTF ); \
1437 av_push( trie_words, tmp ); \
1441 trie->wordinfo[curword].prev = 0; \
1442 trie->wordinfo[curword].len = wordlen; \
1443 trie->wordinfo[curword].accept = state; \
1445 if ( noper_next < tail ) { \
1447 trie->jump = (U16 *) PerlMemShared_calloc( word_count + 1, sizeof(U16) ); \
1448 trie->jump[curword] = (U16)(noper_next - convert); \
1450 jumper = noper_next; \
1452 nextbranch= regnext(cur); \
1456 /* It's a dupe. Pre-insert into the wordinfo[].prev */\
1457 /* chain, so that when the bits of chain are later */\
1458 /* linked together, the dups appear in the chain */\
1459 trie->wordinfo[curword].prev = trie->wordinfo[dupe].prev; \
1460 trie->wordinfo[dupe].prev = curword; \
1462 /* we haven't inserted this word yet. */ \
1463 trie->states[ state ].wordnum = curword; \
1468 #define TRIE_TRANS_STATE(state,base,ucharcount,charid,special) \
1469 ( ( base + charid >= ucharcount \
1470 && base + charid < ubound \
1471 && state == trie->trans[ base - ucharcount + charid ].check \
1472 && trie->trans[ base - ucharcount + charid ].next ) \
1473 ? trie->trans[ base - ucharcount + charid ].next \
1474 : ( state==1 ? special : 0 ) \
1478 #define MADE_JUMP_TRIE 2
1479 #define MADE_EXACT_TRIE 4
1482 S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *first, regnode *last, regnode *tail, U32 word_count, U32 flags, U32 depth)
1485 /* first pass, loop through and scan words */
1486 reg_trie_data *trie;
1487 HV *widecharmap = NULL;
1488 AV *revcharmap = newAV();
1490 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1495 regnode *jumper = NULL;
1496 regnode *nextbranch = NULL;
1497 regnode *convert = NULL;
1498 U32 *prev_states; /* temp array mapping each state to previous one */
1499 /* we just use folder as a flag in utf8 */
1500 const U8 * folder = NULL;
1503 const U32 data_slot = add_data( pRExC_state, 4, "tuuu" );
1504 AV *trie_words = NULL;
1505 /* along with revcharmap, this only used during construction but both are
1506 * useful during debugging so we store them in the struct when debugging.
1509 const U32 data_slot = add_data( pRExC_state, 2, "tu" );
1510 STRLEN trie_charcount=0;
1512 SV *re_trie_maxbuff;
1513 GET_RE_DEBUG_FLAGS_DECL;
1515 PERL_ARGS_ASSERT_MAKE_TRIE;
1517 PERL_UNUSED_ARG(depth);
1522 case EXACTFU: folder = PL_fold_latin1; break;
1523 case EXACTF: folder = PL_fold; break;
1524 case EXACTFL: folder = PL_fold_locale; break;
1527 trie = (reg_trie_data *) PerlMemShared_calloc( 1, sizeof(reg_trie_data) );
1529 trie->startstate = 1;
1530 trie->wordcount = word_count;
1531 RExC_rxi->data->data[ data_slot ] = (void*)trie;
1532 trie->charmap = (U16 *) PerlMemShared_calloc( 256, sizeof(U16) );
1533 if (!(UTF && folder))
1534 trie->bitmap = (char *) PerlMemShared_calloc( ANYOF_BITMAP_SIZE, 1 );
1535 trie->wordinfo = (reg_trie_wordinfo *) PerlMemShared_calloc(
1536 trie->wordcount+1, sizeof(reg_trie_wordinfo));
1539 trie_words = newAV();
1542 re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
1543 if (!SvIOK(re_trie_maxbuff)) {
1544 sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
1547 PerlIO_printf( Perl_debug_log,
1548 "%*smake_trie start==%d, first==%d, last==%d, tail==%d depth=%d\n",
1549 (int)depth * 2 + 2, "",
1550 REG_NODE_NUM(startbranch),REG_NODE_NUM(first),
1551 REG_NODE_NUM(last), REG_NODE_NUM(tail),
1555 /* Find the node we are going to overwrite */
1556 if ( first == startbranch && OP( last ) != BRANCH ) {
1557 /* whole branch chain */
1560 /* branch sub-chain */
1561 convert = NEXTOPER( first );
1564 /* -- First loop and Setup --
1566 We first traverse the branches and scan each word to determine if it
1567 contains widechars, and how many unique chars there are, this is
1568 important as we have to build a table with at least as many columns as we
1571 We use an array of integers to represent the character codes 0..255
1572 (trie->charmap) and we use a an HV* to store Unicode characters. We use the
1573 native representation of the character value as the key and IV's for the
1576 *TODO* If we keep track of how many times each character is used we can
1577 remap the columns so that the table compression later on is more
1578 efficient in terms of memory by ensuring the most common value is in the
1579 middle and the least common are on the outside. IMO this would be better
1580 than a most to least common mapping as theres a decent chance the most
1581 common letter will share a node with the least common, meaning the node
1582 will not be compressible. With a middle is most common approach the worst
1583 case is when we have the least common nodes twice.
1587 for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1588 regnode * const noper = NEXTOPER( cur );
1589 const U8 *uc = (U8*)STRING( noper );
1590 const U8 * const e = uc + STR_LEN( noper );
1592 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1593 const U8 *scan = (U8*)NULL;
1594 U32 wordlen = 0; /* required init */
1596 bool set_bit = trie->bitmap ? 1 : 0; /*store the first char in the bitmap?*/
1598 if (OP(noper) == NOTHING) {
1602 if ( set_bit ) /* bitmap only alloced when !(UTF&&Folding) */
1603 TRIE_BITMAP_SET(trie,*uc); /* store the raw first byte
1604 regardless of encoding */
1606 for ( ; uc < e ; uc += len ) {
1607 TRIE_CHARCOUNT(trie)++;
1611 if ( !trie->charmap[ uvc ] ) {
1612 trie->charmap[ uvc ]=( ++trie->uniquecharcount );
1614 trie->charmap[ folder[ uvc ] ] = trie->charmap[ uvc ];
1618 /* store the codepoint in the bitmap, and its folded
1620 TRIE_BITMAP_SET(trie,uvc);
1622 /* store the folded codepoint */
1623 if ( folder ) TRIE_BITMAP_SET(trie,folder[ uvc ]);
1626 /* store first byte of utf8 representation of
1627 variant codepoints */
1628 if (! UNI_IS_INVARIANT(uvc)) {
1629 TRIE_BITMAP_SET(trie, UTF8_TWO_BYTE_HI(uvc));
1632 set_bit = 0; /* We've done our bit :-) */
1637 widecharmap = newHV();
1639 svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 1 );
1642 Perl_croak( aTHX_ "error creating/fetching widecharmap entry for 0x%"UVXf, uvc );
1644 if ( !SvTRUE( *svpp ) ) {
1645 sv_setiv( *svpp, ++trie->uniquecharcount );
1650 if( cur == first ) {
1653 } else if (chars < trie->minlen) {
1655 } else if (chars > trie->maxlen) {
1659 } /* end first pass */
1660 DEBUG_TRIE_COMPILE_r(
1661 PerlIO_printf( Perl_debug_log, "%*sTRIE(%s): W:%d C:%d Uq:%d Min:%d Max:%d\n",
1662 (int)depth * 2 + 2,"",
1663 ( widecharmap ? "UTF8" : "NATIVE" ), (int)word_count,
1664 (int)TRIE_CHARCOUNT(trie), trie->uniquecharcount,
1665 (int)trie->minlen, (int)trie->maxlen )
1669 We now know what we are dealing with in terms of unique chars and
1670 string sizes so we can calculate how much memory a naive
1671 representation using a flat table will take. If it's over a reasonable
1672 limit (as specified by ${^RE_TRIE_MAXBUF}) we use a more memory
1673 conservative but potentially much slower representation using an array
1676 At the end we convert both representations into the same compressed
1677 form that will be used in regexec.c for matching with. The latter
1678 is a form that cannot be used to construct with but has memory
1679 properties similar to the list form and access properties similar
1680 to the table form making it both suitable for fast searches and
1681 small enough that its feasable to store for the duration of a program.
1683 See the comment in the code where the compressed table is produced
1684 inplace from the flat tabe representation for an explanation of how
1685 the compression works.
1690 Newx(prev_states, TRIE_CHARCOUNT(trie) + 2, U32);
1693 if ( (IV)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1) > SvIV(re_trie_maxbuff) ) {
1695 Second Pass -- Array Of Lists Representation
1697 Each state will be represented by a list of charid:state records
1698 (reg_trie_trans_le) the first such element holds the CUR and LEN
1699 points of the allocated array. (See defines above).
1701 We build the initial structure using the lists, and then convert
1702 it into the compressed table form which allows faster lookups
1703 (but cant be modified once converted).
1706 STRLEN transcount = 1;
1708 DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log,
1709 "%*sCompiling trie using list compiler\n",
1710 (int)depth * 2 + 2, ""));
1712 trie->states = (reg_trie_state *)
1713 PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1714 sizeof(reg_trie_state) );
1718 for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1720 regnode * const noper = NEXTOPER( cur );
1721 U8 *uc = (U8*)STRING( noper );
1722 const U8 * const e = uc + STR_LEN( noper );
1723 U32 state = 1; /* required init */
1724 U16 charid = 0; /* sanity init */
1725 U8 *scan = (U8*)NULL; /* sanity init */
1726 STRLEN foldlen = 0; /* required init */
1727 U32 wordlen = 0; /* required init */
1728 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1730 if (OP(noper) != NOTHING) {
1731 for ( ; uc < e ; uc += len ) {
1736 charid = trie->charmap[ uvc ];
1738 SV** const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1742 charid=(U16)SvIV( *svpp );
1745 /* charid is now 0 if we dont know the char read, or nonzero if we do */
1752 if ( !trie->states[ state ].trans.list ) {
1753 TRIE_LIST_NEW( state );
1755 for ( check = 1; check <= TRIE_LIST_USED( state ); check++ ) {
1756 if ( TRIE_LIST_ITEM( state, check ).forid == charid ) {
1757 newstate = TRIE_LIST_ITEM( state, check ).newstate;
1762 newstate = next_alloc++;
1763 prev_states[newstate] = state;
1764 TRIE_LIST_PUSH( state, charid, newstate );
1769 Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1773 TRIE_HANDLE_WORD(state);
1775 } /* end second pass */
1777 /* next alloc is the NEXT state to be allocated */
1778 trie->statecount = next_alloc;
1779 trie->states = (reg_trie_state *)
1780 PerlMemShared_realloc( trie->states,
1782 * sizeof(reg_trie_state) );
1784 /* and now dump it out before we compress it */
1785 DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_list(trie, widecharmap,
1786 revcharmap, next_alloc,
1790 trie->trans = (reg_trie_trans *)
1791 PerlMemShared_calloc( transcount, sizeof(reg_trie_trans) );
1798 for( state=1 ; state < next_alloc ; state ++ ) {
1802 DEBUG_TRIE_COMPILE_MORE_r(
1803 PerlIO_printf( Perl_debug_log, "tp: %d zp: %d ",tp,zp)
1807 if (trie->states[state].trans.list) {
1808 U16 minid=TRIE_LIST_ITEM( state, 1).forid;
1812 for( idx = 2 ; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1813 const U16 forid = TRIE_LIST_ITEM( state, idx).forid;
1814 if ( forid < minid ) {
1816 } else if ( forid > maxid ) {
1820 if ( transcount < tp + maxid - minid + 1) {
1822 trie->trans = (reg_trie_trans *)
1823 PerlMemShared_realloc( trie->trans,
1825 * sizeof(reg_trie_trans) );
1826 Zero( trie->trans + (transcount / 2), transcount / 2 , reg_trie_trans );
1828 base = trie->uniquecharcount + tp - minid;
1829 if ( maxid == minid ) {
1831 for ( ; zp < tp ; zp++ ) {
1832 if ( ! trie->trans[ zp ].next ) {
1833 base = trie->uniquecharcount + zp - minid;
1834 trie->trans[ zp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1835 trie->trans[ zp ].check = state;
1841 trie->trans[ tp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1842 trie->trans[ tp ].check = state;
1847 for ( idx=1; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1848 const U32 tid = base - trie->uniquecharcount + TRIE_LIST_ITEM( state, idx ).forid;
1849 trie->trans[ tid ].next = TRIE_LIST_ITEM( state, idx ).newstate;
1850 trie->trans[ tid ].check = state;
1852 tp += ( maxid - minid + 1 );
1854 Safefree(trie->states[ state ].trans.list);
1857 DEBUG_TRIE_COMPILE_MORE_r(
1858 PerlIO_printf( Perl_debug_log, " base: %d\n",base);
1861 trie->states[ state ].trans.base=base;
1863 trie->lasttrans = tp + 1;
1867 Second Pass -- Flat Table Representation.
1869 we dont use the 0 slot of either trans[] or states[] so we add 1 to each.
1870 We know that we will need Charcount+1 trans at most to store the data
1871 (one row per char at worst case) So we preallocate both structures
1872 assuming worst case.
1874 We then construct the trie using only the .next slots of the entry
1877 We use the .check field of the first entry of the node temporarily to
1878 make compression both faster and easier by keeping track of how many non
1879 zero fields are in the node.
1881 Since trans are numbered from 1 any 0 pointer in the table is a FAIL
1884 There are two terms at use here: state as a TRIE_NODEIDX() which is a
1885 number representing the first entry of the node, and state as a
1886 TRIE_NODENUM() which is the trans number. state 1 is TRIE_NODEIDX(1) and
1887 TRIE_NODENUM(1), state 2 is TRIE_NODEIDX(2) and TRIE_NODENUM(3) if there
1888 are 2 entrys per node. eg:
1896 The table is internally in the right hand, idx form. However as we also
1897 have to deal with the states array which is indexed by nodenum we have to
1898 use TRIE_NODENUM() to convert.
1901 DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log,
1902 "%*sCompiling trie using table compiler\n",
1903 (int)depth * 2 + 2, ""));
1905 trie->trans = (reg_trie_trans *)
1906 PerlMemShared_calloc( ( TRIE_CHARCOUNT(trie) + 1 )
1907 * trie->uniquecharcount + 1,
1908 sizeof(reg_trie_trans) );
1909 trie->states = (reg_trie_state *)
1910 PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1911 sizeof(reg_trie_state) );
1912 next_alloc = trie->uniquecharcount + 1;
1915 for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1917 regnode * const noper = NEXTOPER( cur );
1918 const U8 *uc = (U8*)STRING( noper );
1919 const U8 * const e = uc + STR_LEN( noper );
1921 U32 state = 1; /* required init */
1923 U16 charid = 0; /* sanity init */
1924 U32 accept_state = 0; /* sanity init */
1925 U8 *scan = (U8*)NULL; /* sanity init */
1927 STRLEN foldlen = 0; /* required init */
1928 U32 wordlen = 0; /* required init */
1929 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1931 if ( OP(noper) != NOTHING ) {
1932 for ( ; uc < e ; uc += len ) {
1937 charid = trie->charmap[ uvc ];
1939 SV* const * const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1940 charid = svpp ? (U16)SvIV(*svpp) : 0;
1944 if ( !trie->trans[ state + charid ].next ) {
1945 trie->trans[ state + charid ].next = next_alloc;
1946 trie->trans[ state ].check++;
1947 prev_states[TRIE_NODENUM(next_alloc)]
1948 = TRIE_NODENUM(state);
1949 next_alloc += trie->uniquecharcount;
1951 state = trie->trans[ state + charid ].next;
1953 Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1955 /* charid is now 0 if we dont know the char read, or nonzero if we do */
1958 accept_state = TRIE_NODENUM( state );
1959 TRIE_HANDLE_WORD(accept_state);
1961 } /* end second pass */
1963 /* and now dump it out before we compress it */
1964 DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_table(trie, widecharmap,
1966 next_alloc, depth+1));
1970 * Inplace compress the table.*
1972 For sparse data sets the table constructed by the trie algorithm will
1973 be mostly 0/FAIL transitions or to put it another way mostly empty.
1974 (Note that leaf nodes will not contain any transitions.)
1976 This algorithm compresses the tables by eliminating most such
1977 transitions, at the cost of a modest bit of extra work during lookup:
1979 - Each states[] entry contains a .base field which indicates the
1980 index in the state[] array wheres its transition data is stored.
1982 - If .base is 0 there are no valid transitions from that node.
1984 - If .base is nonzero then charid is added to it to find an entry in
1987 -If trans[states[state].base+charid].check!=state then the
1988 transition is taken to be a 0/Fail transition. Thus if there are fail
1989 transitions at the front of the node then the .base offset will point
1990 somewhere inside the previous nodes data (or maybe even into a node
1991 even earlier), but the .check field determines if the transition is
1995 The following process inplace converts the table to the compressed
1996 table: We first do not compress the root node 1,and mark all its
1997 .check pointers as 1 and set its .base pointer as 1 as well. This
1998 allows us to do a DFA construction from the compressed table later,
1999 and ensures that any .base pointers we calculate later are greater
2002 - We set 'pos' to indicate the first entry of the second node.
2004 - We then iterate over the columns of the node, finding the first and
2005 last used entry at l and m. We then copy l..m into pos..(pos+m-l),
2006 and set the .check pointers accordingly, and advance pos
2007 appropriately and repreat for the next node. Note that when we copy
2008 the next pointers we have to convert them from the original
2009 NODEIDX form to NODENUM form as the former is not valid post
2012 - If a node has no transitions used we mark its base as 0 and do not
2013 advance the pos pointer.
2015 - If a node only has one transition we use a second pointer into the
2016 structure to fill in allocated fail transitions from other states.
2017 This pointer is independent of the main pointer and scans forward
2018 looking for null transitions that are allocated to a state. When it
2019 finds one it writes the single transition into the "hole". If the
2020 pointer doesnt find one the single transition is appended as normal.
2022 - Once compressed we can Renew/realloc the structures to release the
2025 See "Table-Compression Methods" in sec 3.9 of the Red Dragon,
2026 specifically Fig 3.47 and the associated pseudocode.
2030 const U32 laststate = TRIE_NODENUM( next_alloc );
2033 trie->statecount = laststate;
2035 for ( state = 1 ; state < laststate ; state++ ) {
2037 const U32 stateidx = TRIE_NODEIDX( state );
2038 const U32 o_used = trie->trans[ stateidx ].check;
2039 U32 used = trie->trans[ stateidx ].check;
2040 trie->trans[ stateidx ].check = 0;
2042 for ( charid = 0 ; used && charid < trie->uniquecharcount ; charid++ ) {
2043 if ( flag || trie->trans[ stateidx + charid ].next ) {
2044 if ( trie->trans[ stateidx + charid ].next ) {
2046 for ( ; zp < pos ; zp++ ) {
2047 if ( ! trie->trans[ zp ].next ) {
2051 trie->states[ state ].trans.base = zp + trie->uniquecharcount - charid ;
2052 trie->trans[ zp ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
2053 trie->trans[ zp ].check = state;
2054 if ( ++zp > pos ) pos = zp;
2061 trie->states[ state ].trans.base = pos + trie->uniquecharcount - charid ;
2063 trie->trans[ pos ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
2064 trie->trans[ pos ].check = state;
2069 trie->lasttrans = pos + 1;
2070 trie->states = (reg_trie_state *)
2071 PerlMemShared_realloc( trie->states, laststate
2072 * sizeof(reg_trie_state) );
2073 DEBUG_TRIE_COMPILE_MORE_r(
2074 PerlIO_printf( Perl_debug_log,
2075 "%*sAlloc: %d Orig: %"IVdf" elements, Final:%"IVdf". Savings of %%%5.2f\n",
2076 (int)depth * 2 + 2,"",
2077 (int)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1 ),
2080 ( ( next_alloc - pos ) * 100 ) / (double)next_alloc );
2083 } /* end table compress */
2085 DEBUG_TRIE_COMPILE_MORE_r(
2086 PerlIO_printf(Perl_debug_log, "%*sStatecount:%"UVxf" Lasttrans:%"UVxf"\n",
2087 (int)depth * 2 + 2, "",
2088 (UV)trie->statecount,
2089 (UV)trie->lasttrans)
2091 /* resize the trans array to remove unused space */
2092 trie->trans = (reg_trie_trans *)
2093 PerlMemShared_realloc( trie->trans, trie->lasttrans
2094 * sizeof(reg_trie_trans) );
2096 { /* Modify the program and insert the new TRIE node */
2097 U8 nodetype =(U8)(flags & 0xFF);
2101 regnode *optimize = NULL;
2102 #ifdef RE_TRACK_PATTERN_OFFSETS
2105 U32 mjd_nodelen = 0;
2106 #endif /* RE_TRACK_PATTERN_OFFSETS */
2107 #endif /* DEBUGGING */
2109 This means we convert either the first branch or the first Exact,
2110 depending on whether the thing following (in 'last') is a branch
2111 or not and whther first is the startbranch (ie is it a sub part of
2112 the alternation or is it the whole thing.)
2113 Assuming its a sub part we convert the EXACT otherwise we convert
2114 the whole branch sequence, including the first.
2116 /* Find the node we are going to overwrite */
2117 if ( first != startbranch || OP( last ) == BRANCH ) {
2118 /* branch sub-chain */
2119 NEXT_OFF( first ) = (U16)(last - first);
2120 #ifdef RE_TRACK_PATTERN_OFFSETS
2122 mjd_offset= Node_Offset((convert));
2123 mjd_nodelen= Node_Length((convert));
2126 /* whole branch chain */
2128 #ifdef RE_TRACK_PATTERN_OFFSETS
2131 const regnode *nop = NEXTOPER( convert );
2132 mjd_offset= Node_Offset((nop));
2133 mjd_nodelen= Node_Length((nop));
2137 PerlIO_printf(Perl_debug_log, "%*sMJD offset:%"UVuf" MJD length:%"UVuf"\n",
2138 (int)depth * 2 + 2, "",
2139 (UV)mjd_offset, (UV)mjd_nodelen)
2142 /* But first we check to see if there is a common prefix we can
2143 split out as an EXACT and put in front of the TRIE node. */
2144 trie->startstate= 1;
2145 if ( trie->bitmap && !widecharmap && !trie->jump ) {
2147 for ( state = 1 ; state < trie->statecount-1 ; state++ ) {
2151 const U32 base = trie->states[ state ].trans.base;
2153 if ( trie->states[state].wordnum )
2156 for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
2157 if ( ( base + ofs >= trie->uniquecharcount ) &&
2158 ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
2159 trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
2161 if ( ++count > 1 ) {
2162 SV **tmp = av_fetch( revcharmap, ofs, 0);
2163 const U8 *ch = (U8*)SvPV_nolen_const( *tmp );
2164 if ( state == 1 ) break;
2166 Zero(trie->bitmap, ANYOF_BITMAP_SIZE, char);
2168 PerlIO_printf(Perl_debug_log,
2169 "%*sNew Start State=%"UVuf" Class: [",
2170 (int)depth * 2 + 2, "",
2173 SV ** const tmp = av_fetch( revcharmap, idx, 0);
2174 const U8 * const ch = (U8*)SvPV_nolen_const( *tmp );
2176 TRIE_BITMAP_SET(trie,*ch);
2178 TRIE_BITMAP_SET(trie, folder[ *ch ]);
2180 PerlIO_printf(Perl_debug_log, "%s", (char*)ch)
2184 TRIE_BITMAP_SET(trie,*ch);
2186 TRIE_BITMAP_SET(trie,folder[ *ch ]);
2187 DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"%s", ch));
2193 SV **tmp = av_fetch( revcharmap, idx, 0);
2195 char *ch = SvPV( *tmp, len );
2197 SV *sv=sv_newmortal();
2198 PerlIO_printf( Perl_debug_log,
2199 "%*sPrefix State: %"UVuf" Idx:%"UVuf" Char='%s'\n",
2200 (int)depth * 2 + 2, "",
2202 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 6,
2203 PL_colors[0], PL_colors[1],
2204 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
2205 PERL_PV_ESCAPE_FIRSTCHAR
2210 OP( convert ) = nodetype;
2211 str=STRING(convert);
2214 STR_LEN(convert) += len;
2220 DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"]\n"));
2225 trie->prefixlen = (state-1);
2227 regnode *n = convert+NODE_SZ_STR(convert);
2228 NEXT_OFF(convert) = NODE_SZ_STR(convert);
2229 trie->startstate = state;
2230 trie->minlen -= (state - 1);
2231 trie->maxlen -= (state - 1);
2233 /* At least the UNICOS C compiler choked on this
2234 * being argument to DEBUG_r(), so let's just have
2237 #ifdef PERL_EXT_RE_BUILD
2243 regnode *fix = convert;
2244 U32 word = trie->wordcount;
2246 Set_Node_Offset_Length(convert, mjd_offset, state - 1);
2247 while( ++fix < n ) {
2248 Set_Node_Offset_Length(fix, 0, 0);
2251 SV ** const tmp = av_fetch( trie_words, word, 0 );
2253 if ( STR_LEN(convert) <= SvCUR(*tmp) )
2254 sv_chop(*tmp, SvPV_nolen(*tmp) + STR_LEN(convert));
2256 sv_chop(*tmp, SvPV_nolen(*tmp) + SvCUR(*tmp));
2264 NEXT_OFF(convert) = (U16)(tail - convert);
2265 DEBUG_r(optimize= n);
2271 if ( trie->maxlen ) {
2272 NEXT_OFF( convert ) = (U16)(tail - convert);
2273 ARG_SET( convert, data_slot );
2274 /* Store the offset to the first unabsorbed branch in
2275 jump[0], which is otherwise unused by the jump logic.
2276 We use this when dumping a trie and during optimisation. */
2278 trie->jump[0] = (U16)(nextbranch - convert);
2280 /* If the start state is not accepting (meaning there is no empty string/NOTHING)
2281 * and there is a bitmap
2282 * and the first "jump target" node we found leaves enough room
2283 * then convert the TRIE node into a TRIEC node, with the bitmap
2284 * embedded inline in the opcode - this is hypothetically faster.
2286 if ( !trie->states[trie->startstate].wordnum
2288 && ( (char *)jumper - (char *)convert) >= (int)sizeof(struct regnode_charclass) )
2290 OP( convert ) = TRIEC;
2291 Copy(trie->bitmap, ((struct regnode_charclass *)convert)->bitmap, ANYOF_BITMAP_SIZE, char);
2292 PerlMemShared_free(trie->bitmap);
2295 OP( convert ) = TRIE;
2297 /* store the type in the flags */
2298 convert->flags = nodetype;
2302 + regarglen[ OP( convert ) ];
2304 /* XXX We really should free up the resource in trie now,
2305 as we won't use them - (which resources?) dmq */
2307 /* needed for dumping*/
2308 DEBUG_r(if (optimize) {
2309 regnode *opt = convert;
2311 while ( ++opt < optimize) {
2312 Set_Node_Offset_Length(opt,0,0);
2315 Try to clean up some of the debris left after the
2318 while( optimize < jumper ) {
2319 mjd_nodelen += Node_Length((optimize));
2320 OP( optimize ) = OPTIMIZED;
2321 Set_Node_Offset_Length(optimize,0,0);
2324 Set_Node_Offset_Length(convert,mjd_offset,mjd_nodelen);
2326 } /* end node insert */
2327 REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, convert);
2329 /* Finish populating the prev field of the wordinfo array. Walk back
2330 * from each accept state until we find another accept state, and if
2331 * so, point the first word's .prev field at the second word. If the
2332 * second already has a .prev field set, stop now. This will be the
2333 * case either if we've already processed that word's accept state,
2334 * or that state had multiple words, and the overspill words were
2335 * already linked up earlier.
2342 for (word=1; word <= trie->wordcount; word++) {
2344 if (trie->wordinfo[word].prev)
2346 state = trie->wordinfo[word].accept;
2348 state = prev_states[state];
2351 prev = trie->states[state].wordnum;
2355 trie->wordinfo[word].prev = prev;
2357 Safefree(prev_states);
2361 /* and now dump out the compressed format */
2362 DEBUG_TRIE_COMPILE_r(dump_trie(trie, widecharmap, revcharmap, depth+1));
2364 RExC_rxi->data->data[ data_slot + 1 ] = (void*)widecharmap;
2366 RExC_rxi->data->data[ data_slot + TRIE_WORDS_OFFSET ] = (void*)trie_words;
2367 RExC_rxi->data->data[ data_slot + 3 ] = (void*)revcharmap;
2369 SvREFCNT_dec(revcharmap);
2373 : trie->startstate>1
2379 S_make_trie_failtable(pTHX_ RExC_state_t *pRExC_state, regnode *source, regnode *stclass, U32 depth)
2381 /* The Trie is constructed and compressed now so we can build a fail array if it's needed
2383 This is basically the Aho-Corasick algorithm. Its from exercise 3.31 and 3.32 in the
2384 "Red Dragon" -- Compilers, principles, techniques, and tools. Aho, Sethi, Ullman 1985/88
2387 We find the fail state for each state in the trie, this state is the longest proper
2388 suffix of the current state's 'word' that is also a proper prefix of another word in our
2389 trie. State 1 represents the word '' and is thus the default fail state. This allows
2390 the DFA not to have to restart after its tried and failed a word at a given point, it
2391 simply continues as though it had been matching the other word in the first place.
2393 'abcdgu'=~/abcdefg|cdgu/
2394 When we get to 'd' we are still matching the first word, we would encounter 'g' which would
2395 fail, which would bring us to the state representing 'd' in the second word where we would
2396 try 'g' and succeed, proceeding to match 'cdgu'.
2398 /* add a fail transition */
2399 const U32 trie_offset = ARG(source);
2400 reg_trie_data *trie=(reg_trie_data *)RExC_rxi->data->data[trie_offset];
2402 const U32 ucharcount = trie->uniquecharcount;
2403 const U32 numstates = trie->statecount;
2404 const U32 ubound = trie->lasttrans + ucharcount;
2408 U32 base = trie->states[ 1 ].trans.base;
2411 const U32 data_slot = add_data( pRExC_state, 1, "T" );
2412 GET_RE_DEBUG_FLAGS_DECL;
2414 PERL_ARGS_ASSERT_MAKE_TRIE_FAILTABLE;
2416 PERL_UNUSED_ARG(depth);
2420 ARG_SET( stclass, data_slot );
2421 aho = (reg_ac_data *) PerlMemShared_calloc( 1, sizeof(reg_ac_data) );
2422 RExC_rxi->data->data[ data_slot ] = (void*)aho;
2423 aho->trie=trie_offset;
2424 aho->states=(reg_trie_state *)PerlMemShared_malloc( numstates * sizeof(reg_trie_state) );
2425 Copy( trie->states, aho->states, numstates, reg_trie_state );
2426 Newxz( q, numstates, U32);
2427 aho->fail = (U32 *) PerlMemShared_calloc( numstates, sizeof(U32) );
2430 /* initialize fail[0..1] to be 1 so that we always have
2431 a valid final fail state */
2432 fail[ 0 ] = fail[ 1 ] = 1;
2434 for ( charid = 0; charid < ucharcount ; charid++ ) {
2435 const U32 newstate = TRIE_TRANS_STATE( 1, base, ucharcount, charid, 0 );
2437 q[ q_write ] = newstate;
2438 /* set to point at the root */
2439 fail[ q[ q_write++ ] ]=1;
2442 while ( q_read < q_write) {
2443 const U32 cur = q[ q_read++ % numstates ];
2444 base = trie->states[ cur ].trans.base;
2446 for ( charid = 0 ; charid < ucharcount ; charid++ ) {
2447 const U32 ch_state = TRIE_TRANS_STATE( cur, base, ucharcount, charid, 1 );
2449 U32 fail_state = cur;
2452 fail_state = fail[ fail_state ];
2453 fail_base = aho->states[ fail_state ].trans.base;
2454 } while ( !TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 ) );
2456 fail_state = TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 );
2457 fail[ ch_state ] = fail_state;
2458 if ( !aho->states[ ch_state ].wordnum && aho->states[ fail_state ].wordnum )
2460 aho->states[ ch_state ].wordnum = aho->states[ fail_state ].wordnum;
2462 q[ q_write++ % numstates] = ch_state;
2466 /* restore fail[0..1] to 0 so that we "fall out" of the AC loop
2467 when we fail in state 1, this allows us to use the
2468 charclass scan to find a valid start char. This is based on the principle
2469 that theres a good chance the string being searched contains lots of stuff
2470 that cant be a start char.
2472 fail[ 0 ] = fail[ 1 ] = 0;
2473 DEBUG_TRIE_COMPILE_r({
2474 PerlIO_printf(Perl_debug_log,
2475 "%*sStclass Failtable (%"UVuf" states): 0",
2476 (int)(depth * 2), "", (UV)numstates
2478 for( q_read=1; q_read<numstates; q_read++ ) {
2479 PerlIO_printf(Perl_debug_log, ", %"UVuf, (UV)fail[q_read]);
2481 PerlIO_printf(Perl_debug_log, "\n");
2484 /*RExC_seen |= REG_SEEN_TRIEDFA;*/
2489 * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2490 * These need to be revisited when a newer toolchain becomes available.
2492 #if defined(__sparc64__) && defined(__GNUC__)
2493 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2494 # undef SPARC64_GCC_WORKAROUND
2495 # define SPARC64_GCC_WORKAROUND 1
2499 #define DEBUG_PEEP(str,scan,depth) \
2500 DEBUG_OPTIMISE_r({if (scan){ \
2501 SV * const mysv=sv_newmortal(); \
2502 regnode *Next = regnext(scan); \
2503 regprop(RExC_rx, mysv, scan); \
2504 PerlIO_printf(Perl_debug_log, "%*s" str ">%3d: %s (%d)\n", \
2505 (int)depth*2, "", REG_NODE_NUM(scan), SvPV_nolen_const(mysv),\
2506 Next ? (REG_NODE_NUM(Next)) : 0 ); \
2513 #define JOIN_EXACT(scan,min,flags) \
2514 if (PL_regkind[OP(scan)] == EXACT) \
2515 join_exact(pRExC_state,(scan),(min),(flags),NULL,depth+1)
2518 S_join_exact(pTHX_ RExC_state_t *pRExC_state, regnode *scan, I32 *min, U32 flags,regnode *val, U32 depth) {
2519 /* Merge several consecutive EXACTish nodes into one. */
2520 regnode *n = regnext(scan);
2522 regnode *next = scan + NODE_SZ_STR(scan);
2526 regnode *stop = scan;
2527 GET_RE_DEBUG_FLAGS_DECL;
2529 PERL_UNUSED_ARG(depth);
2532 PERL_ARGS_ASSERT_JOIN_EXACT;
2533 #ifndef EXPERIMENTAL_INPLACESCAN
2534 PERL_UNUSED_ARG(flags);
2535 PERL_UNUSED_ARG(val);
2537 DEBUG_PEEP("join",scan,depth);
2539 /* Skip NOTHING, merge EXACT*. */
2541 ( PL_regkind[OP(n)] == NOTHING ||
2542 (stringok && (OP(n) == OP(scan))))
2544 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
2546 if (OP(n) == TAIL || n > next)
2548 if (PL_regkind[OP(n)] == NOTHING) {
2549 DEBUG_PEEP("skip:",n,depth);
2550 NEXT_OFF(scan) += NEXT_OFF(n);
2551 next = n + NODE_STEP_REGNODE;
2558 else if (stringok) {
2559 const unsigned int oldl = STR_LEN(scan);
2560 regnode * const nnext = regnext(n);
2562 DEBUG_PEEP("merg",n,depth);
2565 if (oldl + STR_LEN(n) > U8_MAX)
2567 NEXT_OFF(scan) += NEXT_OFF(n);
2568 STR_LEN(scan) += STR_LEN(n);
2569 next = n + NODE_SZ_STR(n);
2570 /* Now we can overwrite *n : */
2571 Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
2579 #ifdef EXPERIMENTAL_INPLACESCAN
2580 if (flags && !NEXT_OFF(n)) {
2581 DEBUG_PEEP("atch", val, depth);
2582 if (reg_off_by_arg[OP(n)]) {
2583 ARG_SET(n, val - n);
2586 NEXT_OFF(n) = val - n;
2592 #define GREEK_SMALL_LETTER_IOTA_WITH_DIALYTIKA_AND_TONOS 0x0390
2593 #define IOTA_D_T GREEK_SMALL_LETTER_IOTA_WITH_DIALYTIKA_AND_TONOS
2594 #define GREEK_SMALL_LETTER_UPSILON_WITH_DIALYTIKA_AND_TONOS 0x03B0
2595 #define UPSILON_D_T GREEK_SMALL_LETTER_UPSILON_WITH_DIALYTIKA_AND_TONOS
2598 && ( OP(scan) == EXACTF || OP(scan) == EXACTFU || OP(scan) == EXACTFA)
2599 && ( STR_LEN(scan) >= 6 ) )
2602 Two problematic code points in Unicode casefolding of EXACT nodes:
2604 U+0390 - GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
2605 U+03B0 - GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
2611 U+03B9 U+0308 U+0301 0xCE 0xB9 0xCC 0x88 0xCC 0x81
2612 U+03C5 U+0308 U+0301 0xCF 0x85 0xCC 0x88 0xCC 0x81
2614 This means that in case-insensitive matching (or "loose matching",
2615 as Unicode calls it), an EXACTF of length six (the UTF-8 encoded byte
2616 length of the above casefolded versions) can match a target string
2617 of length two (the byte length of UTF-8 encoded U+0390 or U+03B0).
2618 This would rather mess up the minimum length computation.
2620 What we'll do is to look for the tail four bytes, and then peek
2621 at the preceding two bytes to see whether we need to decrease
2622 the minimum length by four (six minus two).
2624 Thanks to the design of UTF-8, there cannot be false matches:
2625 A sequence of valid UTF-8 bytes cannot be a subsequence of
2626 another valid sequence of UTF-8 bytes.
2629 char * const s0 = STRING(scan), *s, *t;
2630 char * const s1 = s0 + STR_LEN(scan) - 1;
2631 char * const s2 = s1 - 4;
2632 #ifdef EBCDIC /* RD tunifold greek 0390 and 03B0 */
2633 const char t0[] = "\xaf\x49\xaf\x42";
2635 const char t0[] = "\xcc\x88\xcc\x81";
2637 const char * const t1 = t0 + 3;
2640 s < s2 && (t = ninstr(s, s1, t0, t1));
2643 if (((U8)t[-1] == 0x68 && (U8)t[-2] == 0xB4) ||
2644 ((U8)t[-1] == 0x46 && (U8)t[-2] == 0xB5))
2646 if (((U8)t[-1] == 0xB9 && (U8)t[-2] == 0xCE) ||
2647 ((U8)t[-1] == 0x85 && (U8)t[-2] == 0xCF))
2655 n = scan + NODE_SZ_STR(scan);
2657 if (PL_regkind[OP(n)] != NOTHING || OP(n) == NOTHING) {
2664 DEBUG_OPTIMISE_r(if (merged){DEBUG_PEEP("finl",scan,depth)});
2668 /* REx optimizer. Converts nodes into quicker variants "in place".
2669 Finds fixed substrings. */
2671 /* Stops at toplevel WHILEM as well as at "last". At end *scanp is set
2672 to the position after last scanned or to NULL. */
2674 #define INIT_AND_WITHP \
2675 assert(!and_withp); \
2676 Newx(and_withp,1,struct regnode_charclass_class); \
2677 SAVEFREEPV(and_withp)
2679 /* this is a chain of data about sub patterns we are processing that
2680 need to be handled separately/specially in study_chunk. Its so
2681 we can simulate recursion without losing state. */
2683 typedef struct scan_frame {
2684 regnode *last; /* last node to process in this frame */
2685 regnode *next; /* next node to process when last is reached */
2686 struct scan_frame *prev; /*previous frame*/
2687 I32 stop; /* what stopparen do we use */
2691 #define SCAN_COMMIT(s, data, m) scan_commit(s, data, m, is_inf)
2693 #define CASE_SYNST_FNC(nAmE) \
2695 if (flags & SCF_DO_STCLASS_AND) { \
2696 for (value = 0; value < 256; value++) \
2697 if (!is_ ## nAmE ## _cp(value)) \
2698 ANYOF_BITMAP_CLEAR(data->start_class, value); \
2701 for (value = 0; value < 256; value++) \
2702 if (is_ ## nAmE ## _cp(value)) \
2703 ANYOF_BITMAP_SET(data->start_class, value); \
2707 if (flags & SCF_DO_STCLASS_AND) { \
2708 for (value = 0; value < 256; value++) \
2709 if (is_ ## nAmE ## _cp(value)) \
2710 ANYOF_BITMAP_CLEAR(data->start_class, value); \
2713 for (value = 0; value < 256; value++) \
2714 if (!is_ ## nAmE ## _cp(value)) \
2715 ANYOF_BITMAP_SET(data->start_class, value); \
2722 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
2723 I32 *minlenp, I32 *deltap,
2728 struct regnode_charclass_class *and_withp,
2729 U32 flags, U32 depth)
2730 /* scanp: Start here (read-write). */
2731 /* deltap: Write maxlen-minlen here. */
2732 /* last: Stop before this one. */
2733 /* data: string data about the pattern */
2734 /* stopparen: treat close N as END */
2735 /* recursed: which subroutines have we recursed into */
2736 /* and_withp: Valid if flags & SCF_DO_STCLASS_OR */
2739 I32 min = 0, pars = 0, code;
2740 regnode *scan = *scanp, *next;
2742 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
2743 int is_inf_internal = 0; /* The studied chunk is infinite */
2744 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
2745 scan_data_t data_fake;
2746 SV *re_trie_maxbuff = NULL;
2747 regnode *first_non_open = scan;
2748 I32 stopmin = I32_MAX;
2749 scan_frame *frame = NULL;
2750 GET_RE_DEBUG_FLAGS_DECL;
2752 PERL_ARGS_ASSERT_STUDY_CHUNK;
2755 StructCopy(&zero_scan_data, &data_fake, scan_data_t);
2759 while (first_non_open && OP(first_non_open) == OPEN)
2760 first_non_open=regnext(first_non_open);
2765 while ( scan && OP(scan) != END && scan < last ){
2766 /* Peephole optimizer: */
2767 DEBUG_STUDYDATA("Peep:", data,depth);
2768 DEBUG_PEEP("Peep",scan,depth);
2769 JOIN_EXACT(scan,&min,0);
2771 /* Follow the next-chain of the current node and optimize
2772 away all the NOTHINGs from it. */
2773 if (OP(scan) != CURLYX) {
2774 const int max = (reg_off_by_arg[OP(scan)]
2776 /* I32 may be smaller than U16 on CRAYs! */
2777 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
2778 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
2782 /* Skip NOTHING and LONGJMP. */
2783 while ((n = regnext(n))
2784 && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
2785 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
2786 && off + noff < max)
2788 if (reg_off_by_arg[OP(scan)])
2791 NEXT_OFF(scan) = off;
2796 /* The principal pseudo-switch. Cannot be a switch, since we
2797 look into several different things. */
2798 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
2799 || OP(scan) == IFTHEN) {
2800 next = regnext(scan);
2802 /* demq: the op(next)==code check is to see if we have "branch-branch" AFAICT */
2804 if (OP(next) == code || code == IFTHEN) {
2805 /* NOTE - There is similar code to this block below for handling
2806 TRIE nodes on a re-study. If you change stuff here check there
2808 I32 max1 = 0, min1 = I32_MAX, num = 0;
2809 struct regnode_charclass_class accum;
2810 regnode * const startbranch=scan;
2812 if (flags & SCF_DO_SUBSTR)
2813 SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot merge strings after this. */
2814 if (flags & SCF_DO_STCLASS)
2815 cl_init_zero(pRExC_state, &accum);
2817 while (OP(scan) == code) {
2818 I32 deltanext, minnext, f = 0, fake;
2819 struct regnode_charclass_class this_class;
2822 data_fake.flags = 0;
2824 data_fake.whilem_c = data->whilem_c;
2825 data_fake.last_closep = data->last_closep;
2828 data_fake.last_closep = &fake;
2830 data_fake.pos_delta = delta;
2831 next = regnext(scan);
2832 scan = NEXTOPER(scan);
2834 scan = NEXTOPER(scan);
2835 if (flags & SCF_DO_STCLASS) {
2836 cl_init(pRExC_state, &this_class);
2837 data_fake.start_class = &this_class;
2838 f = SCF_DO_STCLASS_AND;
2840 if (flags & SCF_WHILEM_VISITED_POS)
2841 f |= SCF_WHILEM_VISITED_POS;
2843 /* we suppose the run is continuous, last=next...*/
2844 minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext,
2846 stopparen, recursed, NULL, f,depth+1);
2849 if (max1 < minnext + deltanext)
2850 max1 = minnext + deltanext;
2851 if (deltanext == I32_MAX)
2852 is_inf = is_inf_internal = 1;
2854 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
2856 if (data_fake.flags & SCF_SEEN_ACCEPT) {
2857 if ( stopmin > minnext)
2858 stopmin = min + min1;
2859 flags &= ~SCF_DO_SUBSTR;
2861 data->flags |= SCF_SEEN_ACCEPT;
2864 if (data_fake.flags & SF_HAS_EVAL)
2865 data->flags |= SF_HAS_EVAL;
2866 data->whilem_c = data_fake.whilem_c;
2868 if (flags & SCF_DO_STCLASS)
2869 cl_or(pRExC_state, &accum, &this_class);
2871 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
2873 if (flags & SCF_DO_SUBSTR) {
2874 data->pos_min += min1;
2875 data->pos_delta += max1 - min1;
2876 if (max1 != min1 || is_inf)
2877 data->longest = &(data->longest_float);
2880 delta += max1 - min1;
2881 if (flags & SCF_DO_STCLASS_OR) {
2882 cl_or(pRExC_state, data->start_class, &accum);
2884 cl_and(data->start_class, and_withp);
2885 flags &= ~SCF_DO_STCLASS;
2888 else if (flags & SCF_DO_STCLASS_AND) {
2890 cl_and(data->start_class, &accum);
2891 flags &= ~SCF_DO_STCLASS;
2894 /* Switch to OR mode: cache the old value of
2895 * data->start_class */
2897 StructCopy(data->start_class, and_withp,
2898 struct regnode_charclass_class);
2899 flags &= ~SCF_DO_STCLASS_AND;
2900 StructCopy(&accum, data->start_class,
2901 struct regnode_charclass_class);
2902 flags |= SCF_DO_STCLASS_OR;
2903 data->start_class->flags |= ANYOF_EOS;
2907 if (PERL_ENABLE_TRIE_OPTIMISATION && OP( startbranch ) == BRANCH ) {
2910 Assuming this was/is a branch we are dealing with: 'scan' now
2911 points at the item that follows the branch sequence, whatever
2912 it is. We now start at the beginning of the sequence and look
2919 which would be constructed from a pattern like /A|LIST|OF|WORDS/
2921 If we can find such a subsequence we need to turn the first
2922 element into a trie and then add the subsequent branch exact
2923 strings to the trie.
2927 1. patterns where the whole set of branches can be converted.
2929 2. patterns where only a subset can be converted.
2931 In case 1 we can replace the whole set with a single regop
2932 for the trie. In case 2 we need to keep the start and end
2935 'BRANCH EXACT; BRANCH EXACT; BRANCH X'
2936 becomes BRANCH TRIE; BRANCH X;
2938 There is an additional case, that being where there is a
2939 common prefix, which gets split out into an EXACT like node
2940 preceding the TRIE node.
2942 If x(1..n)==tail then we can do a simple trie, if not we make
2943 a "jump" trie, such that when we match the appropriate word
2944 we "jump" to the appropriate tail node. Essentially we turn
2945 a nested if into a case structure of sorts.
2950 if (!re_trie_maxbuff) {
2951 re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
2952 if (!SvIOK(re_trie_maxbuff))
2953 sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
2955 if ( SvIV(re_trie_maxbuff)>=0 ) {
2957 regnode *first = (regnode *)NULL;
2958 regnode *last = (regnode *)NULL;
2959 regnode *tail = scan;
2964 SV * const mysv = sv_newmortal(); /* for dumping */
2966 /* var tail is used because there may be a TAIL
2967 regop in the way. Ie, the exacts will point to the
2968 thing following the TAIL, but the last branch will
2969 point at the TAIL. So we advance tail. If we
2970 have nested (?:) we may have to move through several
2974 while ( OP( tail ) == TAIL ) {
2975 /* this is the TAIL generated by (?:) */
2976 tail = regnext( tail );
2981 regprop(RExC_rx, mysv, tail );
2982 PerlIO_printf( Perl_debug_log, "%*s%s%s\n",
2983 (int)depth * 2 + 2, "",
2984 "Looking for TRIE'able sequences. Tail node is: ",
2985 SvPV_nolen_const( mysv )
2991 step through the branches, cur represents each
2992 branch, noper is the first thing to be matched
2993 as part of that branch and noper_next is the
2994 regnext() of that node. if noper is an EXACT
2995 and noper_next is the same as scan (our current
2996 position in the regex) then the EXACT branch is
2997 a possible optimization target. Once we have
2998 two or more consecutive such branches we can
2999 create a trie of the EXACT's contents and stich
3000 it in place. If the sequence represents all of
3001 the branches we eliminate the whole thing and
3002 replace it with a single TRIE. If it is a
3003 subsequence then we need to stitch it in. This
3004 means the first branch has to remain, and needs
3005 to be repointed at the item on the branch chain
3006 following the last branch optimized. This could
3007 be either a BRANCH, in which case the
3008 subsequence is internal, or it could be the
3009 item following the branch sequence in which
3010 case the subsequence is at the end.
3014 /* dont use tail as the end marker for this traverse */
3015 for ( cur = startbranch ; cur != scan ; cur = regnext( cur ) ) {
3016 regnode * const noper = NEXTOPER( cur );
3017 #if defined(DEBUGGING) || defined(NOJUMPTRIE)
3018 regnode * const noper_next = regnext( noper );
3022 regprop(RExC_rx, mysv, cur);
3023 PerlIO_printf( Perl_debug_log, "%*s- %s (%d)",
3024 (int)depth * 2 + 2,"", SvPV_nolen_const( mysv ), REG_NODE_NUM(cur) );
3026 regprop(RExC_rx, mysv, noper);
3027 PerlIO_printf( Perl_debug_log, " -> %s",
3028 SvPV_nolen_const(mysv));
3031 regprop(RExC_rx, mysv, noper_next );
3032 PerlIO_printf( Perl_debug_log,"\t=> %s\t",
3033 SvPV_nolen_const(mysv));
3035 PerlIO_printf( Perl_debug_log, "(First==%d,Last==%d,Cur==%d)\n",
3036 REG_NODE_NUM(first), REG_NODE_NUM(last), REG_NODE_NUM(cur) );
3038 if ( (((first && optype!=NOTHING) ? OP( noper ) == optype
3039 : PL_regkind[ OP( noper ) ] == EXACT )
3040 || OP(noper) == NOTHING )
3042 && noper_next == tail
3047 if ( !first || optype == NOTHING ) {
3048 if (!first) first = cur;
3049 optype = OP( noper );
3055 Currently we do not believe that the trie logic can
3056 handle case insensitive matching properly when the
3057 pattern is not unicode (thus forcing unicode semantics).
3059 If/when this is fixed the following define can be swapped
3060 in below to fully enable trie logic.
3062 XXX It may work if not UTF and/or /a (AT_LEAST_UNI_SEMANTICS) but perhaps
3065 #define TRIE_TYPE_IS_SAFE 1
3068 #define TRIE_TYPE_IS_SAFE ((UTF && UNI_SEMANTICS) || optype==EXACT)
3070 if ( last && TRIE_TYPE_IS_SAFE ) {
3071 make_trie( pRExC_state,
3072 startbranch, first, cur, tail, count,
3075 if ( PL_regkind[ OP( noper ) ] == EXACT
3077 && noper_next == tail
3082 optype = OP( noper );
3092 regprop(RExC_rx, mysv, cur);
3093 PerlIO_printf( Perl_debug_log,
3094 "%*s- %s (%d) <SCAN FINISHED>\n", (int)depth * 2 + 2,
3095 "", SvPV_nolen_const( mysv ),REG_NODE_NUM(cur));
3099 if ( last && TRIE_TYPE_IS_SAFE ) {
3100 made= make_trie( pRExC_state, startbranch, first, scan, tail, count, optype, depth+1 );
3101 #ifdef TRIE_STUDY_OPT
3102 if ( ((made == MADE_EXACT_TRIE &&
3103 startbranch == first)
3104 || ( first_non_open == first )) &&
3106 flags |= SCF_TRIE_RESTUDY;
3107 if ( startbranch == first
3110 RExC_seen &=~REG_TOP_LEVEL_BRANCHES;
3120 else if ( code == BRANCHJ ) { /* single branch is optimized. */
3121 scan = NEXTOPER(NEXTOPER(scan));
3122 } else /* single branch is optimized. */
3123 scan = NEXTOPER(scan);
3125 } else if (OP(scan) == SUSPEND || OP(scan) == GOSUB || OP(scan) == GOSTART) {
3126 scan_frame *newframe = NULL;
3131 if (OP(scan) != SUSPEND) {
3132 /* set the pointer */
3133 if (OP(scan) == GOSUB) {
3135 RExC_recurse[ARG2L(scan)] = scan;
3136 start = RExC_open_parens[paren-1];
3137 end = RExC_close_parens[paren-1];
3140 start = RExC_rxi->program + 1;
3144 Newxz(recursed, (((RExC_npar)>>3) +1), U8);
3145 SAVEFREEPV(recursed);
3147 if (!PAREN_TEST(recursed,paren+1)) {
3148 PAREN_SET(recursed,paren+1);
3149 Newx(newframe,1,scan_frame);
3151 if (flags & SCF_DO_SUBSTR) {
3152 SCAN_COMMIT(pRExC_state,data,minlenp);
3153 data->longest = &(data->longest_float);
3155 is_inf = is_inf_internal = 1;
3156 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
3157 cl_anything(pRExC_state, data->start_class);
3158 flags &= ~SCF_DO_STCLASS;
3161 Newx(newframe,1,scan_frame);
3164 end = regnext(scan);
3169 SAVEFREEPV(newframe);
3170 newframe->next = regnext(scan);
3171 newframe->last = last;
3172 newframe->stop = stopparen;
3173 newframe->prev = frame;
3183 else if (OP(scan) == EXACT) {
3184 I32 l = STR_LEN(scan);
3187 const U8 * const s = (U8*)STRING(scan);
3188 l = utf8_length(s, s + l);
3189 uc = utf8_to_uvchr(s, NULL);
3191 uc = *((U8*)STRING(scan));
3194 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
3195 /* The code below prefers earlier match for fixed
3196 offset, later match for variable offset. */
3197 if (data->last_end == -1) { /* Update the start info. */
3198 data->last_start_min = data->pos_min;
3199 data->last_start_max = is_inf
3200 ? I32_MAX : data->pos_min + data->pos_delta;
3202 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
3204 SvUTF8_on(data->last_found);
3206 SV * const sv = data->last_found;
3207 MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
3208 mg_find(sv, PERL_MAGIC_utf8) : NULL;
3209 if (mg && mg->mg_len >= 0)
3210 mg->mg_len += utf8_length((U8*)STRING(scan),
3211 (U8*)STRING(scan)+STR_LEN(scan));
3213 data->last_end = data->pos_min + l;
3214 data->pos_min += l; /* As in the first entry. */
3215 data->flags &= ~SF_BEFORE_EOL;
3217 if (flags & SCF_DO_STCLASS_AND) {
3218 /* Check whether it is compatible with what we know already! */
3222 /* If compatible, we or it in below. It is compatible if is
3223 * in the bitmp and either 1) its bit or its fold is set, or 2)
3224 * it's for a locale. Even if there isn't unicode semantics
3225 * here, at runtime there may be because of matching against a
3226 * utf8 string, so accept a possible false positive for
3227 * latin1-range folds */
3229 (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3230 && !ANYOF_BITMAP_TEST(data->start_class, uc)
3231 && (!(data->start_class->flags & ANYOF_LOC_NONBITMAP_FOLD)
3232 || !ANYOF_BITMAP_TEST(data->start_class, PL_fold_latin1[uc])))
3237 ANYOF_CLASS_ZERO(data->start_class);
3238 ANYOF_BITMAP_ZERO(data->start_class);
3240 ANYOF_BITMAP_SET(data->start_class, uc);
3241 else if (uc >= 0x100) {
3244 /* Some Unicode code points fold to the Latin1 range; as
3245 * XXX temporary code, instead of figuring out if this is
3246 * one, just assume it is and set all the start class bits
3247 * that could be some such above 255 code point's fold
3248 * which will generate fals positives. As the code
3249 * elsewhere that does compute the fold settles down, it
3250 * can be extracted out and re-used here */
3251 for (i = 0; i < 256; i++){
3252 if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)) {
3253 ANYOF_BITMAP_SET(data->start_class, i);
3257 data->start_class->flags &= ~ANYOF_EOS;
3259 data->start_class->flags &= ~ANYOF_UNICODE_ALL;
3261 else if (flags & SCF_DO_STCLASS_OR) {
3262 /* false positive possible if the class is case-folded */
3264 ANYOF_BITMAP_SET(data->start_class, uc);
3266 data->start_class->flags |= ANYOF_UNICODE_ALL;
3267 data->start_class->flags &= ~ANYOF_EOS;
3268 cl_and(data->start_class, and_withp);
3270 flags &= ~SCF_DO_STCLASS;
3272 else if (PL_regkind[OP(scan)] == EXACT) { /* But OP != EXACT! */
3273 I32 l = STR_LEN(scan);
3274 UV uc = *((U8*)STRING(scan));
3276 /* Search for fixed substrings supports EXACT only. */
3277 if (flags & SCF_DO_SUBSTR) {
3279 SCAN_COMMIT(pRExC_state, data, minlenp);
3282 const U8 * const s = (U8 *)STRING(scan);
3283 l = utf8_length(s, s + l);
3284 uc = utf8_to_uvchr(s, NULL);
3287 if (flags & SCF_DO_SUBSTR)
3289 if (flags & SCF_DO_STCLASS_AND) {
3290 /* Check whether it is compatible with what we know already! */
3293 (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3294 && !ANYOF_BITMAP_TEST(data->start_class, uc)
3295 && !ANYOF_BITMAP_TEST(data->start_class, PL_fold_latin1[uc])))
3299 ANYOF_CLASS_ZERO(data->start_class);
3300 ANYOF_BITMAP_ZERO(data->start_class);
3302 ANYOF_BITMAP_SET(data->start_class, uc);
3303 data->start_class->flags &= ~ANYOF_EOS;
3304 data->start_class->flags |= ANYOF_LOC_NONBITMAP_FOLD;
3305 if (OP(scan) == EXACTFL) {
3306 /* XXX This set is probably no longer necessary, and
3307 * probably wrong as LOCALE now is on in the initial
3309 data->start_class->flags |= ANYOF_LOCALE;
3313 /* Also set the other member of the fold pair. In case
3314 * that unicode semantics is called for at runtime, use
3315 * the full latin1 fold. (Can't do this for locale,
3316 * because not known until runtime */
3317 ANYOF_BITMAP_SET(data->start_class, PL_fold_latin1[uc]);
3320 else if (uc >= 0x100) {
3322 for (i = 0; i < 256; i++){
3323 if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)) {
3324 ANYOF_BITMAP_SET(data->start_class, i);
3329 else if (flags & SCF_DO_STCLASS_OR) {
3330 if (data->start_class->flags & ANYOF_LOC_NONBITMAP_FOLD) {
3331 /* false positive possible if the class is case-folded.
3332 Assume that the locale settings are the same... */
3334 ANYOF_BITMAP_SET(data->start_class, uc);
3335 if (OP(scan) != EXACTFL) {
3337 /* And set the other member of the fold pair, but
3338 * can't do that in locale because not known until
3340 ANYOF_BITMAP_SET(data->start_class,
3341 PL_fold_latin1[uc]);
3344 data->start_class->flags &= ~ANYOF_EOS;
3346 cl_and(data->start_class, and_withp);
3348 flags &= ~SCF_DO_STCLASS;
3350 else if (REGNODE_VARIES(OP(scan))) {
3351 I32 mincount, maxcount, minnext, deltanext, fl = 0;
3352 I32 f = flags, pos_before = 0;
3353 regnode * const oscan = scan;
3354 struct regnode_charclass_class this_class;
3355 struct regnode_charclass_class *oclass = NULL;
3356 I32 next_is_eval = 0;
3358 switch (PL_regkind[OP(scan)]) {
3359 case WHILEM: /* End of (?:...)* . */
3360 scan = NEXTOPER(scan);
3363 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
3364 next = NEXTOPER(scan);
3365 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
3367 maxcount = REG_INFTY;
3368 next = regnext(scan);
3369 scan = NEXTOPER(scan);
3373 if (flags & SCF_DO_SUBSTR)
3378 if (flags & SCF_DO_STCLASS) {
3380 maxcount = REG_INFTY;
3381 next = regnext(scan);
3382 scan = NEXTOPER(scan);
3385 is_inf = is_inf_internal = 1;
3386 scan = regnext(scan);
3387 if (flags & SCF_DO_SUBSTR) {
3388 SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot extend fixed substrings */
3389 data->longest = &(data->longest_float);
3391 goto optimize_curly_tail;
3393 if (stopparen>0 && (OP(scan)==CURLYN || OP(scan)==CURLYM)
3394 && (scan->flags == stopparen))
3399 mincount = ARG1(scan);
3400 maxcount = ARG2(scan);
3402 next = regnext(scan);
3403 if (OP(scan) == CURLYX) {
3404 I32 lp = (data ? *(data->last_closep) : 0);
3405 scan->flags = ((lp <= (I32)U8_MAX) ? (U8)lp : U8_MAX);
3407 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3408 next_is_eval = (OP(scan) == EVAL);
3410 if (flags & SCF_DO_SUBSTR) {
3411 if (mincount == 0) SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot extend fixed substrings */
3412 pos_before = data->pos_min;
3416 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
3418 data->flags |= SF_IS_INF;
3420 if (flags & SCF_DO_STCLASS) {
3421 cl_init(pRExC_state, &this_class);
3422 oclass = data->start_class;
3423 data->start_class = &this_class;
3424 f |= SCF_DO_STCLASS_AND;
3425 f &= ~SCF_DO_STCLASS_OR;
3427 /* Exclude from super-linear cache processing any {n,m}
3428 regops for which the combination of input pos and regex
3429 pos is not enough information to determine if a match
3432 For example, in the regex /foo(bar\s*){4,8}baz/ with the
3433 regex pos at the \s*, the prospects for a match depend not
3434 only on the input position but also on how many (bar\s*)
3435 repeats into the {4,8} we are. */
3436 if ((mincount > 1) || (maxcount > 1 && maxcount != REG_INFTY))
3437 f &= ~SCF_WHILEM_VISITED_POS;
3439 /* This will finish on WHILEM, setting scan, or on NULL: */
3440 minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext,
3441 last, data, stopparen, recursed, NULL,
3443 ? (f & ~SCF_DO_SUBSTR) : f),depth+1);
3445 if (flags & SCF_DO_STCLASS)
3446 data->start_class = oclass;
3447 if (mincount == 0 || minnext == 0) {
3448 if (flags & SCF_DO_STCLASS_OR) {
3449 cl_or(pRExC_state, data->start_class, &this_class);
3451 else if (flags & SCF_DO_STCLASS_AND) {
3452 /* Switch to OR mode: cache the old value of
3453 * data->start_class */
3455 StructCopy(data->start_class, and_withp,
3456 struct regnode_charclass_class);
3457 flags &= ~SCF_DO_STCLASS_AND;
3458 StructCopy(&this_class, data->start_class,
3459 struct regnode_charclass_class);
3460 flags |= SCF_DO_STCLASS_OR;
3461 data->start_class->flags |= ANYOF_EOS;
3463 } else { /* Non-zero len */
3464 if (flags & SCF_DO_STCLASS_OR) {
3465 cl_or(pRExC_state, data->start_class, &this_class);
3466 cl_and(data->start_class, and_withp);
3468 else if (flags & SCF_DO_STCLASS_AND)
3469 cl_and(data->start_class, &this_class);
3470 flags &= ~SCF_DO_STCLASS;
3472 if (!scan) /* It was not CURLYX, but CURLY. */
3474 if ( /* ? quantifier ok, except for (?{ ... }) */
3475 (next_is_eval || !(mincount == 0 && maxcount == 1))
3476 && (minnext == 0) && (deltanext == 0)
3477 && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
3478 && maxcount <= REG_INFTY/3) /* Complement check for big count */
3480 ckWARNreg(RExC_parse,
3481 "Quantifier unexpected on zero-length expression");
3484 min += minnext * mincount;
3485 is_inf_internal |= ((maxcount == REG_INFTY
3486 && (minnext + deltanext) > 0)
3487 || deltanext == I32_MAX);
3488 is_inf |= is_inf_internal;
3489 delta += (minnext + deltanext) * maxcount - minnext * mincount;
3491 /* Try powerful optimization CURLYX => CURLYN. */
3492 if ( OP(oscan) == CURLYX && data
3493 && data->flags & SF_IN_PAR
3494 && !(data->flags & SF_HAS_EVAL)
3495 && !deltanext && minnext == 1 ) {
3496 /* Try to optimize to CURLYN. */
3497 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
3498 regnode * const nxt1 = nxt;
3505 if (!REGNODE_SIMPLE(OP(nxt))
3506 && !(PL_regkind[OP(nxt)] == EXACT
3507 && STR_LEN(nxt) == 1))
3513 if (OP(nxt) != CLOSE)
3515 if (RExC_open_parens) {
3516 RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3517 RExC_close_parens[ARG(nxt1)-1]=nxt+2; /*close->while*/
3519 /* Now we know that nxt2 is the only contents: */
3520 oscan->flags = (U8)ARG(nxt);
3522 OP(nxt1) = NOTHING; /* was OPEN. */
3525 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3526 NEXT_OFF(nxt1+ 1) = 0; /* just for consistency. */
3527 NEXT_OFF(nxt2) = 0; /* just for consistency with CURLY. */
3528 OP(nxt) = OPTIMIZED; /* was CLOSE. */
3529 OP(nxt + 1) = OPTIMIZED; /* was count. */
3530 NEXT_OFF(nxt+ 1) = 0; /* just for consistency. */
3535 /* Try optimization CURLYX => CURLYM. */
3536 if ( OP(oscan) == CURLYX && data
3537 && !(data->flags & SF_HAS_PAR)
3538 && !(data->flags & SF_HAS_EVAL)
3539 && !deltanext /* atom is fixed width */
3540 && minnext != 0 /* CURLYM can't handle zero width */
3542 /* XXXX How to optimize if data == 0? */
3543 /* Optimize to a simpler form. */
3544 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
3548 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
3549 && (OP(nxt2) != WHILEM))
3551 OP(nxt2) = SUCCEED; /* Whas WHILEM */
3552 /* Need to optimize away parenths. */
3553 if ((data->flags & SF_IN_PAR) && OP(nxt) == CLOSE) {
3554 /* Set the parenth number. */
3555 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
3557 oscan->flags = (U8)ARG(nxt);
3558 if (RExC_open_parens) {
3559 RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3560 RExC_close_parens[ARG(nxt1)-1]=nxt2+1; /*close->NOTHING*/
3562 OP(nxt1) = OPTIMIZED; /* was OPEN. */
3563 OP(nxt) = OPTIMIZED; /* was CLOSE. */
3566 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3567 OP(nxt + 1) = OPTIMIZED; /* was count. */
3568 NEXT_OFF(nxt1 + 1) = 0; /* just for consistency. */
3569 NEXT_OFF(nxt + 1) = 0; /* just for consistency. */
3572 while ( nxt1 && (OP(nxt1) != WHILEM)) {
3573 regnode *nnxt = regnext(nxt1);
3575 if (reg_off_by_arg[OP(nxt1)])
3576 ARG_SET(nxt1, nxt2 - nxt1);
3577 else if (nxt2 - nxt1 < U16_MAX)
3578 NEXT_OFF(nxt1) = nxt2 - nxt1;
3580 OP(nxt) = NOTHING; /* Cannot beautify */
3585 /* Optimize again: */
3586 study_chunk(pRExC_state, &nxt1, minlenp, &deltanext, nxt,
3587 NULL, stopparen, recursed, NULL, 0,depth+1);
3592 else if ((OP(oscan) == CURLYX)
3593 && (flags & SCF_WHILEM_VISITED_POS)
3594 /* See the comment on a similar expression above.
3595 However, this time it's not a subexpression
3596 we care about, but the expression itself. */
3597 && (maxcount == REG_INFTY)
3598 && data && ++data->whilem_c < 16) {
3599 /* This stays as CURLYX, we can put the count/of pair. */
3600 /* Find WHILEM (as in regexec.c) */
3601 regnode *nxt = oscan + NEXT_OFF(oscan);
3603 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
3605 PREVOPER(nxt)->flags = (U8)(data->whilem_c
3606 | (RExC_whilem_seen << 4)); /* On WHILEM */
3608 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
3610 if (flags & SCF_DO_SUBSTR) {
3611 SV *last_str = NULL;
3612 int counted = mincount != 0;
3614 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
3615 #if defined(SPARC64_GCC_WORKAROUND)
3618 const char *s = NULL;
3621 if (pos_before >= data->last_start_min)
3624 b = data->last_start_min;
3627 s = SvPV_const(data->last_found, l);
3628 old = b - data->last_start_min;
3631 I32 b = pos_before >= data->last_start_min
3632 ? pos_before : data->last_start_min;
3634 const char * const s = SvPV_const(data->last_found, l);
3635 I32 old = b - data->last_start_min;
3639 old = utf8_hop((U8*)s, old) - (U8*)s;
3641 /* Get the added string: */
3642 last_str = newSVpvn_utf8(s + old, l, UTF);
3643 if (deltanext == 0 && pos_before == b) {
3644 /* What was added is a constant string */
3646 SvGROW(last_str, (mincount * l) + 1);
3647 repeatcpy(SvPVX(last_str) + l,
3648 SvPVX_const(last_str), l, mincount - 1);
3649 SvCUR_set(last_str, SvCUR(last_str) * mincount);
3650 /* Add additional parts. */
3651 SvCUR_set(data->last_found,
3652 SvCUR(data->last_found) - l);
3653 sv_catsv(data->last_found, last_str);
3655 SV * sv = data->last_found;
3657 SvUTF8(sv) && SvMAGICAL(sv) ?
3658 mg_find(sv, PERL_MAGIC_utf8) : NULL;
3659 if (mg && mg->mg_len >= 0)
3660 mg->mg_len += CHR_SVLEN(last_str) - l;
3662 data->last_end += l * (mincount - 1);
3665 /* start offset must point into the last copy */
3666 data->last_start_min += minnext * (mincount - 1);
3667 data->last_start_max += is_inf ? I32_MAX
3668 : (maxcount - 1) * (minnext + data->pos_delta);
3671 /* It is counted once already... */
3672 data->pos_min += minnext * (mincount - counted);
3673 data->pos_delta += - counted * deltanext +
3674 (minnext + deltanext) * maxcount - minnext * mincount;
3675 if (mincount != maxcount) {
3676 /* Cannot extend fixed substrings found inside
3678 SCAN_COMMIT(pRExC_state,data,minlenp);
3679 if (mincount && last_str) {
3680 SV * const sv = data->last_found;
3681 MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
3682 mg_find(sv, PERL_MAGIC_utf8) : NULL;
3686 sv_setsv(sv, last_str);
3687 data->last_end = data->pos_min;
3688 data->last_start_min =
3689 data->pos_min - CHR_SVLEN(last_str);
3690 data->last_start_max = is_inf
3692 : data->pos_min + data->pos_delta
3693 - CHR_SVLEN(last_str);
3695 data->longest = &(data->longest_float);
3697 SvREFCNT_dec(last_str);
3699 if (data && (fl & SF_HAS_EVAL))
3700 data->flags |= SF_HAS_EVAL;
3701 optimize_curly_tail:
3702 if (OP(oscan) != CURLYX) {
3703 while (PL_regkind[OP(next = regnext(oscan))] == NOTHING
3705 NEXT_OFF(oscan) += NEXT_OFF(next);
3708 default: /* REF, ANYOFV, and CLUMP only? */
3709 if (flags & SCF_DO_SUBSTR) {
3710 SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
3711 data->longest = &(data->longest_float);
3713 is_inf = is_inf_internal = 1;
3714 if (flags & SCF_DO_STCLASS_OR)
3715 cl_anything(pRExC_state, data->start_class);
3716 flags &= ~SCF_DO_STCLASS;
3720 else if (OP(scan) == LNBREAK) {
3721 if (flags & SCF_DO_STCLASS) {
3723 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
3724 if (flags & SCF_DO_STCLASS_AND) {
3725 for (value = 0; value < 256; value++)
3726 if (!is_VERTWS_cp(value))
3727 ANYOF_BITMAP_CLEAR(data->start_class, value);
3730 for (value = 0; value < 256; value++)
3731 if (is_VERTWS_cp(value))
3732 ANYOF_BITMAP_SET(data->start_class, value);
3734 if (flags & SCF_DO_STCLASS_OR)
3735 cl_and(data->start_class, and_withp);
3736 flags &= ~SCF_DO_STCLASS;
3740 if (flags & SCF_DO_SUBSTR) {
3741 SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
3743 data->pos_delta += 1;
3744 data->longest = &(data->longest_float);
3747 else if (OP(scan) == FOLDCHAR) {
3748 int d = ARG(scan) == LATIN_SMALL_LETTER_SHARP_S ? 1 : 2;
3749 flags &= ~SCF_DO_STCLASS;
3752 if (flags & SCF_DO_SUBSTR) {
3753 SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
3755 data->pos_delta += d;
3756 data->longest = &(data->longest_float);
3759 else if (REGNODE_SIMPLE(OP(scan))) {
3762 if (flags & SCF_DO_SUBSTR) {
3763 SCAN_COMMIT(pRExC_state,data,minlenp);
3767 if (flags & SCF_DO_STCLASS) {
3768 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
3770 /* Some of the logic below assumes that switching
3771 locale on will only add false positives. */
3772 switch (PL_regkind[OP(scan)]) {
3776 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
3777 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
3778 cl_anything(pRExC_state, data->start_class);
3781 if (OP(scan) == SANY)
3783 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
3784 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
3785 || ANYOF_CLASS_TEST_ANY_SET(data->start_class));
3786 cl_anything(pRExC_state, data->start_class);
3788 if (flags & SCF_DO_STCLASS_AND || !value)
3789 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
3792 if (flags & SCF_DO_STCLASS_AND)
3793 cl_and(data->start_class,
3794 (struct regnode_charclass_class*)scan);
3796 cl_or(pRExC_state, data->start_class,
3797 (struct regnode_charclass_class*)scan);
3800 if (flags & SCF_DO_STCLASS_AND) {
3801 if (!(data->start_class->flags & ANYOF_LOCALE)) {
3802 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
3803 if (OP(scan) == ALNUMU) {
3804 for (value = 0; value < 256; value++) {
3805 if (!isWORDCHAR_L1(value)) {
3806 ANYOF_BITMAP_CLEAR(data->start_class, value);
3810 for (value = 0; value < 256; value++) {
3811 if (!isALNUM(value)) {
3812 ANYOF_BITMAP_CLEAR(data->start_class, value);
3819 if (data->start_class->flags & ANYOF_LOCALE)
3820 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
3822 /* Even if under locale, set the bits for non-locale
3823 * in case it isn't a true locale-node. This will
3824 * create false positives if it truly is locale */
3825 if (OP(scan) == ALNUMU) {
3826 for (value = 0; value < 256; value++) {
3827 if (isWORDCHAR_L1(value)) {
3828 ANYOF_BITMAP_SET(data->start_class, value);
3832 for (value = 0; value < 256; value++) {
3833 if (isALNUM(value)) {
3834 ANYOF_BITMAP_SET(data->start_class, value);
3841 if (flags & SCF_DO_STCLASS_AND) {
3842 if (!(data->start_class->flags & ANYOF_LOCALE)) {
3843 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
3844 if (OP(scan) == NALNUMU) {
3845 for (value = 0; value < 256; value++) {
3846 if (isWORDCHAR_L1(value)) {
3847 ANYOF_BITMAP_CLEAR(data->start_class, value);
3851 for (value = 0; value < 256; value++) {
3852 if (isALNUM(value)) {
3853 ANYOF_BITMAP_CLEAR(data->start_class, value);
3860 if (data->start_class->flags & ANYOF_LOCALE)
3861 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
3863 /* Even if under locale, set the bits for non-locale in
3864 * case it isn't a true locale-node. This will create
3865 * false positives if it truly is locale */
3866 if (OP(scan) == NALNUMU) {
3867 for (value = 0; value < 256; value++) {
3868 if (! isWORDCHAR_L1(value)) {
3869 ANYOF_BITMAP_SET(data->start_class, value);
3873 for (value = 0; value < 256; value++) {
3874 if (! isALNUM(value)) {
3875 ANYOF_BITMAP_SET(data->start_class, value);
3882 if (flags & SCF_DO_STCLASS_AND) {
3883 if (!(data->start_class->flags & ANYOF_LOCALE)) {
3884 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
3885 if (OP(scan) == SPACEU) {
3886 for (value = 0; value < 256; value++) {
3887 if (!isSPACE_L1(value)) {
3888 ANYOF_BITMAP_CLEAR(data->start_class, value);
3892 for (value = 0; value < 256; value++) {
3893 if (!isSPACE(value)) {
3894 ANYOF_BITMAP_CLEAR(data->start_class, value);
3901 if (data->start_class->flags & ANYOF_LOCALE) {
3902 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
3904 if (OP(scan) == SPACEU) {
3905 for (value = 0; value < 256; value++) {
3906 if (isSPACE_L1(value)) {
3907 ANYOF_BITMAP_SET(data->start_class, value);
3911 for (value = 0; value < 256; value++) {
3912 if (isSPACE(value)) {
3913 ANYOF_BITMAP_SET(data->start_class, value);
3920 if (flags & SCF_DO_STCLASS_AND) {
3921 if (!(data->start_class->flags & ANYOF_LOCALE)) {
3922 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
3923 if (OP(scan) == NSPACEU) {
3924 for (value = 0; value < 256; value++) {
3925 if (isSPACE_L1(value)) {
3926 ANYOF_BITMAP_CLEAR(data->start_class, value);
3930 for (value = 0; value < 256; value++) {
3931 if (isSPACE(value)) {
3932 ANYOF_BITMAP_CLEAR(data->start_class, value);
3939 if (data->start_class->flags & ANYOF_LOCALE)
3940 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
3941 if (OP(scan) == NSPACEU) {
3942 for (value = 0; value < 256; value++) {
3943 if (!isSPACE_L1(value)) {
3944 ANYOF_BITMAP_SET(data->start_class, value);
3949 for (value = 0; value < 256; value++) {
3950 if (!isSPACE(value)) {
3951 ANYOF_BITMAP_SET(data->start_class, value);
3958 if (flags & SCF_DO_STCLASS_AND) {
3959 if (!(data->start_class->flags & ANYOF_LOCALE)) {
3960 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
3961 for (value = 0; value < 256; value++)
3962 if (!isDIGIT(value))
3963 ANYOF_BITMAP_CLEAR(data->start_class, value);
3967 if (data->start_class->flags & ANYOF_LOCALE)
3968 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
3969 for (value = 0; value < 256; value++)
3971 ANYOF_BITMAP_SET(data->start_class, value);
3975 if (flags & SCF_DO_STCLASS_AND) {
3976 if (!(data->start_class->flags & ANYOF_LOCALE))
3977 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
3978 for (value = 0; value < 256; value++)
3980 ANYOF_BITMAP_CLEAR(data->start_class, value);
3983 if (data->start_class->flags & ANYOF_LOCALE)
3984 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
3985 for (value = 0; value < 256; value++)
3986 if (!isDIGIT(value))
3987 ANYOF_BITMAP_SET(data->start_class, value);
3990 CASE_SYNST_FNC(VERTWS);
3991 CASE_SYNST_FNC(HORIZWS);
3994 if (flags & SCF_DO_STCLASS_OR)
3995 cl_and(data->start_class, and_withp);
3996 flags &= ~SCF_DO_STCLASS;
3999 else if (PL_regkind[OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
4000 data->flags |= (OP(scan) == MEOL
4004 else if ( PL_regkind[OP(scan)] == BRANCHJ
4005 /* Lookbehind, or need to calculate parens/evals/stclass: */
4006 && (scan->flags || data || (flags & SCF_DO_STCLASS))
4007 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
4008 if ( !PERL_ENABLE_POSITIVE_ASSERTION_STUDY
4009 || OP(scan) == UNLESSM )
4011 /* Negative Lookahead/lookbehind
4012 In this case we can't do fixed string optimisation.
4015 I32 deltanext, minnext, fake = 0;
4017 struct regnode_charclass_class intrnl;
4020 data_fake.flags = 0;
4022 data_fake.whilem_c = data->whilem_c;
4023 data_fake.last_closep = data->last_closep;
4026 data_fake.last_closep = &fake;
4027 data_fake.pos_delta = delta;
4028 if ( flags & SCF_DO_STCLASS && !scan->flags
4029 && OP(scan) == IFMATCH ) { /* Lookahead */
4030 cl_init(pRExC_state, &intrnl);
4031 data_fake.start_class = &intrnl;
4032 f |= SCF_DO_STCLASS_AND;
4034 if (flags & SCF_WHILEM_VISITED_POS)
4035 f |= SCF_WHILEM_VISITED_POS;
4036 next = regnext(scan);
4037 nscan = NEXTOPER(NEXTOPER(scan));
4038 minnext = study_chunk(pRExC_state, &nscan, minlenp, &deltanext,
4039 last, &data_fake, stopparen, recursed, NULL, f, depth+1);
4042 FAIL("Variable length lookbehind not implemented");
4044 else if (minnext > (I32)U8_MAX) {
4045 FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
4047 scan->flags = (U8)minnext;
4050 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4052 if (data_fake.flags & SF_HAS_EVAL)
4053 data->flags |= SF_HAS_EVAL;
4054 data->whilem_c = data_fake.whilem_c;
4056 if (f & SCF_DO_STCLASS_AND) {
4057 if (flags & SCF_DO_STCLASS_OR) {
4058 /* OR before, AND after: ideally we would recurse with
4059 * data_fake to get the AND applied by study of the
4060 * remainder of the pattern, and then derecurse;
4061 * *** HACK *** for now just treat as "no information".
4062 * See [perl #56690].
4064 cl_init(pRExC_state, data->start_class);
4066 /* AND before and after: combine and continue */
4067 const int was = (data->start_class->flags & ANYOF_EOS);
4069 cl_and(data->start_class, &intrnl);
4071 data->start_class->flags |= ANYOF_EOS;
4075 #if PERL_ENABLE_POSITIVE_ASSERTION_STUDY
4077 /* Positive Lookahead/lookbehind
4078 In this case we can do fixed string optimisation,
4079 but we must be careful about it. Note in the case of
4080 lookbehind the positions will be offset by the minimum
4081 length of the pattern, something we won't know about
4082 until after the recurse.
4084 I32 deltanext, fake = 0;
4086 struct regnode_charclass_class intrnl;
4088 /* We use SAVEFREEPV so that when the full compile
4089 is finished perl will clean up the allocated
4090 minlens when it's all done. This way we don't
4091 have to worry about freeing them when we know
4092 they wont be used, which would be a pain.
4095 Newx( minnextp, 1, I32 );
4096 SAVEFREEPV(minnextp);
4099 StructCopy(data, &data_fake, scan_data_t);
4100 if ((flags & SCF_DO_SUBSTR) && data->last_found) {
4103 SCAN_COMMIT(pRExC_state, &data_fake,minlenp);
4104 data_fake.last_found=newSVsv(data->last_found);
4108 data_fake.last_closep = &fake;
4109 data_fake.flags = 0;
4110 data_fake.pos_delta = delta;
4112 data_fake.flags |= SF_IS_INF;
4113 if ( flags & SCF_DO_STCLASS && !scan->flags
4114 && OP(scan) == IFMATCH ) { /* Lookahead */
4115 cl_init(pRExC_state, &intrnl);
4116 data_fake.start_class = &intrnl;
4117 f |= SCF_DO_STCLASS_AND;
4119 if (flags & SCF_WHILEM_VISITED_POS)
4120 f |= SCF_WHILEM_VISITED_POS;
4121 next = regnext(scan);
4122 nscan = NEXTOPER(NEXTOPER(scan));
4124 *minnextp = study_chunk(pRExC_state, &nscan, minnextp, &deltanext,
4125 last, &data_fake, stopparen, recursed, NULL, f,depth+1);
4128 FAIL("Variable length lookbehind not implemented");
4130 else if (*minnextp > (I32)U8_MAX) {
4131 FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
4133 scan->flags = (U8)*minnextp;
4138 if (f & SCF_DO_STCLASS_AND) {
4139 const int was = (data->start_class->flags & ANYOF_EOS);
4141 cl_and(data->start_class, &intrnl);
4143 data->start_class->flags |= ANYOF_EOS;
4146 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4148 if (data_fake.flags & SF_HAS_EVAL)
4149 data->flags |= SF_HAS_EVAL;
4150 data->whilem_c = data_fake.whilem_c;
4151 if ((flags & SCF_DO_SUBSTR) && data_fake.last_found) {
4152 if (RExC_rx->minlen<*minnextp)
4153 RExC_rx->minlen=*minnextp;
4154 SCAN_COMMIT(pRExC_state, &data_fake, minnextp);
4155 SvREFCNT_dec(data_fake.last_found);
4157 if ( data_fake.minlen_fixed != minlenp )
4159 data->offset_fixed= data_fake.offset_fixed;
4160 data->minlen_fixed= data_fake.minlen_fixed;
4161 data->lookbehind_fixed+= scan->flags;
4163 if ( data_fake.minlen_float != minlenp )
4165 data->minlen_float= data_fake.minlen_float;
4166 data->offset_float_min=data_fake.offset_float_min;
4167 data->offset_float_max=data_fake.offset_float_max;
4168 data->lookbehind_float+= scan->flags;
4177 else if (OP(scan) == OPEN) {
4178 if (stopparen != (I32)ARG(scan))
4181 else if (OP(scan) == CLOSE) {
4182 if (stopparen == (I32)ARG(scan)) {
4185 if ((I32)ARG(scan) == is_par) {
4186 next = regnext(scan);
4188 if ( next && (OP(next) != WHILEM) && next < last)
4189 is_par = 0; /* Disable optimization */
4192 *(data->last_closep) = ARG(scan);
4194 else if (OP(scan) == EVAL) {
4196 data->flags |= SF_HAS_EVAL;
4198 else if ( PL_regkind[OP(scan)] == ENDLIKE ) {
4199 if (flags & SCF_DO_SUBSTR) {
4200 SCAN_COMMIT(pRExC_state,data,minlenp);
4201 flags &= ~SCF_DO_SUBSTR;
4203 if (data && OP(scan)==ACCEPT) {
4204 data->flags |= SCF_SEEN_ACCEPT;
4209 else if (OP(scan) == LOGICAL && scan->flags == 2) /* Embedded follows */
4211 if (flags & SCF_DO_SUBSTR) {
4212 SCAN_COMMIT(pRExC_state,data,minlenp);
4213 data->longest = &(data->longest_float);
4215 is_inf = is_inf_internal = 1;
4216 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
4217 cl_anything(pRExC_state, data->start_class);
4218 flags &= ~SCF_DO_STCLASS;
4220 else if (OP(scan) == GPOS) {
4221 if (!(RExC_rx->extflags & RXf_GPOS_FLOAT) &&
4222 !(delta || is_inf || (data && data->pos_delta)))
4224 if (!(RExC_rx->extflags & RXf_ANCH) && (flags & SCF_DO_SUBSTR))
4225 RExC_rx->extflags |= RXf_ANCH_GPOS;
4226 if (RExC_rx->gofs < (U32)min)
4227 RExC_rx->gofs = min;
4229 RExC_rx->extflags |= RXf_GPOS_FLOAT;
4233 #ifdef TRIE_STUDY_OPT
4234 #ifdef FULL_TRIE_STUDY
4235 else if (PL_regkind[OP(scan)] == TRIE) {
4236 /* NOTE - There is similar code to this block above for handling
4237 BRANCH nodes on the initial study. If you change stuff here
4239 regnode *trie_node= scan;
4240 regnode *tail= regnext(scan);
4241 reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
4242 I32 max1 = 0, min1 = I32_MAX;
4243 struct regnode_charclass_class accum;
4245 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
4246 SCAN_COMMIT(pRExC_state, data,minlenp); /* Cannot merge strings after this. */
4247 if (flags & SCF_DO_STCLASS)
4248 cl_init_zero(pRExC_state, &accum);
4254 const regnode *nextbranch= NULL;
4257 for ( word=1 ; word <= trie->wordcount ; word++)
4259 I32 deltanext=0, minnext=0, f = 0, fake;
4260 struct regnode_charclass_class this_class;
4262 data_fake.flags = 0;
4264 data_fake.whilem_c = data->whilem_c;
4265 data_fake.last_closep = data->last_closep;
4268 data_fake.last_closep = &fake;
4269 data_fake.pos_delta = delta;
4270 if (flags & SCF_DO_STCLASS) {
4271 cl_init(pRExC_state, &this_class);
4272 data_fake.start_class = &this_class;
4273 f = SCF_DO_STCLASS_AND;
4275 if (flags & SCF_WHILEM_VISITED_POS)
4276 f |= SCF_WHILEM_VISITED_POS;
4278 if (trie->jump[word]) {
4280 nextbranch = trie_node + trie->jump[0];
4281 scan= trie_node + trie->jump[word];
4282 /* We go from the jump point to the branch that follows
4283 it. Note this means we need the vestigal unused branches
4284 even though they arent otherwise used.
4286 minnext = study_chunk(pRExC_state, &scan, minlenp,
4287 &deltanext, (regnode *)nextbranch, &data_fake,
4288 stopparen, recursed, NULL, f,depth+1);
4290 if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
4291 nextbranch= regnext((regnode*)nextbranch);
4293 if (min1 > (I32)(minnext + trie->minlen))
4294 min1 = minnext + trie->minlen;
4295 if (max1 < (I32)(minnext + deltanext + trie->maxlen))
4296 max1 = minnext + deltanext + trie->maxlen;
4297 if (deltanext == I32_MAX)
4298 is_inf = is_inf_internal = 1;
4300 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4302 if (data_fake.flags & SCF_SEEN_ACCEPT) {
4303 if ( stopmin > min + min1)
4304 stopmin = min + min1;
4305 flags &= ~SCF_DO_SUBSTR;
4307 data->flags |= SCF_SEEN_ACCEPT;
4310 if (data_fake.flags & SF_HAS_EVAL)
4311 data->flags |= SF_HAS_EVAL;
4312 data->whilem_c = data_fake.whilem_c;
4314 if (flags & SCF_DO_STCLASS)
4315 cl_or(pRExC_state, &accum, &this_class);
4318 if (flags & SCF_DO_SUBSTR) {
4319 data->pos_min += min1;
4320 data->pos_delta += max1 - min1;
4321 if (max1 != min1 || is_inf)
4322 data->longest = &(data->longest_float);
4325 delta += max1 - min1;
4326 if (flags & SCF_DO_STCLASS_OR) {
4327 cl_or(pRExC_state, data->start_class, &accum);
4329 cl_and(data->start_class, and_withp);
4330 flags &= ~SCF_DO_STCLASS;
4333 else if (flags & SCF_DO_STCLASS_AND) {
4335 cl_and(data->start_class, &accum);
4336 flags &= ~SCF_DO_STCLASS;
4339 /* Switch to OR mode: cache the old value of
4340 * data->start_class */
4342 StructCopy(data->start_class, and_withp,
4343 struct regnode_charclass_class);
4344 flags &= ~SCF_DO_STCLASS_AND;
4345 StructCopy(&accum, data->start_class,
4346 struct regnode_charclass_class);
4347 flags |= SCF_DO_STCLASS_OR;
4348 data->start_class->flags |= ANYOF_EOS;
4355 else if (PL_regkind[OP(scan)] == TRIE) {
4356 reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
4359 min += trie->minlen;
4360 delta += (trie->maxlen - trie->minlen);
4361 flags &= ~SCF_DO_STCLASS; /* xxx */
4362 if (flags & SCF_DO_SUBSTR) {
4363 SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */
4364 data->pos_min += trie->minlen;
4365 data->pos_delta += (trie->maxlen - trie->minlen);
4366 if (trie->maxlen != trie->minlen)
4367 data->longest = &(data->longest_float);
4369 if (trie->jump) /* no more substrings -- for now /grr*/
4370 flags &= ~SCF_DO_SUBSTR;
4372 #endif /* old or new */
4373 #endif /* TRIE_STUDY_OPT */
4375 /* Else: zero-length, ignore. */
4376 scan = regnext(scan);
4381 stopparen = frame->stop;
4382 frame = frame->prev;
4383 goto fake_study_recurse;
4388 DEBUG_STUDYDATA("pre-fin:",data,depth);
4391 *deltap = is_inf_internal ? I32_MAX : delta;
4392 if (flags & SCF_DO_SUBSTR && is_inf)
4393 data->pos_delta = I32_MAX - data->pos_min;
4394 if (is_par > (I32)U8_MAX)
4396 if (is_par && pars==1 && data) {
4397 data->flags |= SF_IN_PAR;
4398 data->flags &= ~SF_HAS_PAR;
4400 else if (pars && data) {
4401 data->flags |= SF_HAS_PAR;
4402 data->flags &= ~SF_IN_PAR;
4404 if (flags & SCF_DO_STCLASS_OR)
4405 cl_and(data->start_class, and_withp);
4406 if (flags & SCF_TRIE_RESTUDY)
4407 data->flags |= SCF_TRIE_RESTUDY;
4409 DEBUG_STUDYDATA("post-fin:",data,depth);
4411 return min < stopmin ? min : stopmin;
4415 S_add_data(RExC_state_t *pRExC_state, U32 n, const char *s)
4417 U32 count = RExC_rxi->data ? RExC_rxi->data->count : 0;
4419 PERL_ARGS_ASSERT_ADD_DATA;
4421 Renewc(RExC_rxi->data,
4422 sizeof(*RExC_rxi->data) + sizeof(void*) * (count + n - 1),
4423 char, struct reg_data);
4425 Renew(RExC_rxi->data->what, count + n, U8);
4427 Newx(RExC_rxi->data->what, n, U8);
4428 RExC_rxi->data->count = count + n;
4429 Copy(s, RExC_rxi->data->what + count, n, U8);
4433 /*XXX: todo make this not included in a non debugging perl */
4434 #ifndef PERL_IN_XSUB_RE
4436 Perl_reginitcolors(pTHX)
4439 const char * const s = PerlEnv_getenv("PERL_RE_COLORS");
4441 char *t = savepv(s);
4445 t = strchr(t, '\t');
4451 PL_colors[i] = t = (char *)"";
4456 PL_colors[i++] = (char *)"";
4463 #ifdef TRIE_STUDY_OPT
4464 #define CHECK_RESTUDY_GOTO \
4466 (data.flags & SCF_TRIE_RESTUDY) \
4470 #define CHECK_RESTUDY_GOTO
4474 - pregcomp - compile a regular expression into internal code
4476 * We can't allocate space until we know how big the compiled form will be,
4477 * but we can't compile it (and thus know how big it is) until we've got a
4478 * place to put the code. So we cheat: we compile it twice, once with code
4479 * generation turned off and size counting turned on, and once "for real".
4480 * This also means that we don't allocate space until we are sure that the
4481 * thing really will compile successfully, and we never have to move the
4482 * code and thus invalidate pointers into it. (Note that it has to be in
4483 * one piece because free() must be able to free it all.) [NB: not true in perl]
4485 * Beware that the optimization-preparation code in here knows about some
4486 * of the structure of the compiled regexp. [I'll say.]
4491 #ifndef PERL_IN_XSUB_RE
4492 #define RE_ENGINE_PTR &reh_regexp_engine
4494 extern const struct regexp_engine my_reg_engine;
4495 #define RE_ENGINE_PTR &my_reg_engine
4498 #ifndef PERL_IN_XSUB_RE
4500 Perl_pregcomp(pTHX_ SV * const pattern, const U32 flags)
4503 HV * const table = GvHV(PL_hintgv);
4505 PERL_ARGS_ASSERT_PREGCOMP;
4507 /* Dispatch a request to compile a regexp to correct
4510 SV **ptr= hv_fetchs(table, "regcomp", FALSE);
4511 GET_RE_DEBUG_FLAGS_DECL;
4512 if (ptr && SvIOK(*ptr) && SvIV(*ptr)) {
4513 const regexp_engine *eng=INT2PTR(regexp_engine*,SvIV(*ptr));
4515 PerlIO_printf(Perl_debug_log, "Using engine %"UVxf"\n",
4518 return CALLREGCOMP_ENG(eng, pattern, flags);
4521 return Perl_re_compile(aTHX_ pattern, flags);
4526 Perl_re_compile(pTHX_ SV * const pattern, U32 orig_pm_flags)
4531 register regexp_internal *ri;
4540 /* these are all flags - maybe they should be turned
4541 * into a single int with different bit masks */
4542 I32 sawlookahead = 0;
4545 bool used_setjump = FALSE;
4546 regex_charset initial_charset = get_regex_charset(orig_pm_flags);
4551 RExC_state_t RExC_state;
4552 RExC_state_t * const pRExC_state = &RExC_state;
4553 #ifdef TRIE_STUDY_OPT
4555 RExC_state_t copyRExC_state;
4557 GET_RE_DEBUG_FLAGS_DECL;
4559 PERL_ARGS_ASSERT_RE_COMPILE;
4561 DEBUG_r(if (!PL_colorset) reginitcolors());
4563 RExC_utf8 = RExC_orig_utf8 = SvUTF8(pattern);
4564 RExC_uni_semantics = 0;
4565 RExC_contains_locale = 0;
4567 /****************** LONG JUMP TARGET HERE***********************/
4568 /* Longjmp back to here if have to switch in midstream to utf8 */
4569 if (! RExC_orig_utf8) {
4570 JMPENV_PUSH(jump_ret);
4571 used_setjump = TRUE;
4574 if (jump_ret == 0) { /* First time through */
4575 exp = SvPV(pattern, plen);
4577 /* ignore the utf8ness if the pattern is 0 length */
4579 RExC_utf8 = RExC_orig_utf8 = 0;
4583 SV *dsv= sv_newmortal();
4584 RE_PV_QUOTED_DECL(s, RExC_utf8,
4585 dsv, exp, plen, 60);
4586 PerlIO_printf(Perl_debug_log, "%sCompiling REx%s %s\n",
4587 PL_colors[4],PL_colors[5],s);
4590 else { /* longjumped back */
4593 /* If the cause for the longjmp was other than changing to utf8, pop
4594 * our own setjmp, and longjmp to the correct handler */
4595 if (jump_ret != UTF8_LONGJMP) {
4597 JMPENV_JUMP(jump_ret);
4602 /* It's possible to write a regexp in ascii that represents Unicode
4603 codepoints outside of the byte range, such as via \x{100}. If we
4604 detect such a sequence we have to convert the entire pattern to utf8
4605 and then recompile, as our sizing calculation will have been based
4606 on 1 byte == 1 character, but we will need to use utf8 to encode
4607 at least some part of the pattern, and therefore must convert the whole
4610 DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log,
4611 "UTF8 mismatch! Converting to utf8 for resizing and compile\n"));
4612 exp = (char*)Perl_bytes_to_utf8(aTHX_ (U8*)SvPV(pattern, plen), &len);
4614 RExC_orig_utf8 = RExC_utf8 = 1;
4618 #ifdef TRIE_STUDY_OPT
4622 pm_flags = orig_pm_flags;
4624 if (initial_charset == REGEX_LOCALE_CHARSET) {
4625 RExC_contains_locale = 1;
4627 else if (RExC_utf8 && initial_charset == REGEX_DEPENDS_CHARSET) {
4629 /* Set to use unicode semantics if the pattern is in utf8 and has the
4630 * 'depends' charset specified, as it means unicode when utf8 */
4631 set_regex_charset(&pm_flags, REGEX_UNICODE_CHARSET);
4635 RExC_flags = pm_flags;
4639 RExC_in_lookbehind = 0;
4640 RExC_seen_zerolen = *exp == '^' ? -1 : 0;
4641 RExC_seen_evals = 0;
4643 RExC_override_recoding = 0;
4645 /* First pass: determine size, legality. */
4653 RExC_emit = &PL_regdummy;
4654 RExC_whilem_seen = 0;
4655 RExC_open_parens = NULL;
4656 RExC_close_parens = NULL;
4658 RExC_paren_names = NULL;
4660 RExC_paren_name_list = NULL;
4662 RExC_recurse = NULL;
4663 RExC_recurse_count = 0;
4665 #if 0 /* REGC() is (currently) a NOP at the first pass.
4666 * Clever compilers notice this and complain. --jhi */
4667 REGC((U8)REG_MAGIC, (char*)RExC_emit);
4669 DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "Starting first pass (sizing)\n"));
4670 if (reg(pRExC_state, 0, &flags,1) == NULL) {
4671 RExC_precomp = NULL;
4675 /* Here, finished first pass. Get rid of any added setjmp */
4681 PerlIO_printf(Perl_debug_log,
4682 "Required size %"IVdf" nodes\n"
4683 "Starting second pass (creation)\n",
4686 RExC_lastparse=NULL;
4689 /* The first pass could have found things that force Unicode semantics */
4690 if ((RExC_utf8 || RExC_uni_semantics)
4691 && get_regex_charset(pm_flags) == REGEX_DEPENDS_CHARSET)
4693 set_regex_charset(&pm_flags, REGEX_UNICODE_CHARSET);
4696 /* Small enough for pointer-storage convention?
4697 If extralen==0, this means that we will not need long jumps. */
4698 if (RExC_size >= 0x10000L && RExC_extralen)
4699 RExC_size += RExC_extralen;
4702 if (RExC_whilem_seen > 15)
4703 RExC_whilem_seen = 15;
4705 /* Allocate space and zero-initialize. Note, the two step process
4706 of zeroing when in debug mode, thus anything assigned has to
4707 happen after that */
4708 rx = (REGEXP*) newSV_type(SVt_REGEXP);
4709 r = (struct regexp*)SvANY(rx);
4710 Newxc(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode),
4711 char, regexp_internal);
4712 if ( r == NULL || ri == NULL )
4713 FAIL("Regexp out of space");
4715 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
4716 Zero(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode), char);
4718 /* bulk initialize base fields with 0. */
4719 Zero(ri, sizeof(regexp_internal), char);
4722 /* non-zero initialization begins here */
4724 r->engine= RE_ENGINE_PTR;
4725 r->extflags = pm_flags;
4727 bool has_p = ((r->extflags & RXf_PMf_KEEPCOPY) == RXf_PMf_KEEPCOPY);
4728 bool has_charset = (get_regex_charset(r->extflags) != REGEX_DEPENDS_CHARSET);
4730 /* The caret is output if there are any defaults: if not all the STD
4731 * flags are set, or if no character set specifier is needed */
4733 (((r->extflags & RXf_PMf_STD_PMMOD) != RXf_PMf_STD_PMMOD)
4735 bool has_runon = ((RExC_seen & REG_SEEN_RUN_ON_COMMENT)==REG_SEEN_RUN_ON_COMMENT);
4736 U16 reganch = (U16)((r->extflags & RXf_PMf_STD_PMMOD)
4737 >> RXf_PMf_STD_PMMOD_SHIFT);
4738 const char *fptr = STD_PAT_MODS; /*"msix"*/
4740 /* Allocate for the worst case, which is all the std flags are turned
4741 * on. If more precision is desired, we could do a population count of
4742 * the flags set. This could be done with a small lookup table, or by
4743 * shifting, masking and adding, or even, when available, assembly
4744 * language for a machine-language population count.
4745 * We never output a minus, as all those are defaults, so are
4746 * covered by the caret */
4747 const STRLEN wraplen = plen + has_p + has_runon
4748 + has_default /* If needs a caret */
4750 /* If needs a character set specifier */
4751 + ((has_charset) ? MAX_CHARSET_NAME_LENGTH : 0)
4752 + (sizeof(STD_PAT_MODS) - 1)
4753 + (sizeof("(?:)") - 1);
4755 p = sv_grow(MUTABLE_SV(rx), wraplen + 1); /* +1 for the ending NUL */
4757 SvFLAGS(rx) |= SvUTF8(pattern);
4760 /* If a default, cover it using the caret */
4762 *p++= DEFAULT_PAT_MOD;
4766 const char* const name = get_regex_charset_name(r->extflags, &len);
4767 Copy(name, p, len, char);
4771 *p++ = KEEPCOPY_PAT_MOD; /*'p'*/
4774 while((ch = *fptr++)) {
4782 Copy(RExC_precomp, p, plen, char);
4783 assert ((RX_WRAPPED(rx) - p) < 16);
4784 r->pre_prefix = p - RX_WRAPPED(rx);
4790 SvCUR_set(rx, p - SvPVX_const(rx));
4794 r->nparens = RExC_npar - 1; /* set early to validate backrefs */
4796 if (RExC_seen & REG_SEEN_RECURSE) {
4797 Newxz(RExC_open_parens, RExC_npar,regnode *);
4798 SAVEFREEPV(RExC_open_parens);
4799 Newxz(RExC_close_parens,RExC_npar,regnode *);
4800 SAVEFREEPV(RExC_close_parens);
4803 /* Useful during FAIL. */
4804 #ifdef RE_TRACK_PATTERN_OFFSETS
4805 Newxz(ri->u.offsets, 2*RExC_size+1, U32); /* MJD 20001228 */
4806 DEBUG_OFFSETS_r(PerlIO_printf(Perl_debug_log,
4807 "%s %"UVuf" bytes for offset annotations.\n",
4808 ri->u.offsets ? "Got" : "Couldn't get",
4809 (UV)((2*RExC_size+1) * sizeof(U32))));
4811 SetProgLen(ri,RExC_size);
4815 REH_CALL_COMP_BEGIN_HOOK(pRExC_state->rx);
4817 /* Second pass: emit code. */
4818 RExC_flags = pm_flags; /* don't let top level (?i) bleed */
4823 RExC_emit_start = ri->program;
4824 RExC_emit = ri->program;
4825 RExC_emit_bound = ri->program + RExC_size + 1;
4827 /* Store the count of eval-groups for security checks: */
4828 RExC_rx->seen_evals = RExC_seen_evals;
4829 REGC((U8)REG_MAGIC, (char*) RExC_emit++);
4830 if (reg(pRExC_state, 0, &flags,1) == NULL) {
4834 /* XXXX To minimize changes to RE engine we always allocate
4835 3-units-long substrs field. */
4836 Newx(r->substrs, 1, struct reg_substr_data);
4837 if (RExC_recurse_count) {
4838 Newxz(RExC_recurse,RExC_recurse_count,regnode *);
4839 SAVEFREEPV(RExC_recurse);
4843 r->minlen = minlen = sawlookahead = sawplus = sawopen = 0;
4844 Zero(r->substrs, 1, struct reg_substr_data);
4846 #ifdef TRIE_STUDY_OPT
4848 StructCopy(&zero_scan_data, &data, scan_data_t);
4849 copyRExC_state = RExC_state;
4852 DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log,"Restudying\n"));
4854 RExC_state = copyRExC_state;
4855 if (seen & REG_TOP_LEVEL_BRANCHES)
4856 RExC_seen |= REG_TOP_LEVEL_BRANCHES;
4858 RExC_seen &= ~REG_TOP_LEVEL_BRANCHES;
4859 if (data.last_found) {
4860 SvREFCNT_dec(data.longest_fixed);
4861 SvREFCNT_dec(data.longest_float);
4862 SvREFCNT_dec(data.last_found);
4864 StructCopy(&zero_scan_data, &data, scan_data_t);
4867 StructCopy(&zero_scan_data, &data, scan_data_t);
4870 /* Dig out information for optimizations. */
4871 r->extflags = RExC_flags; /* was pm_op */
4872 /*dmq: removed as part of de-PMOP: pm->op_pmflags = RExC_flags; */
4875 SvUTF8_on(rx); /* Unicode in it? */
4876 ri->regstclass = NULL;
4877 if (RExC_naughty >= 10) /* Probably an expensive pattern. */
4878 r->intflags |= PREGf_NAUGHTY;
4879 scan = ri->program + 1; /* First BRANCH. */
4881 /* testing for BRANCH here tells us whether there is "must appear"
4882 data in the pattern. If there is then we can use it for optimisations */
4883 if (!(RExC_seen & REG_TOP_LEVEL_BRANCHES)) { /* Only one top-level choice. */
4885 STRLEN longest_float_length, longest_fixed_length;
4886 struct regnode_charclass_class ch_class; /* pointed to by data */
4888 I32 last_close = 0; /* pointed to by data */
4889 regnode *first= scan;
4890 regnode *first_next= regnext(first);
4892 * Skip introductions and multiplicators >= 1
4893 * so that we can extract the 'meat' of the pattern that must
4894 * match in the large if() sequence following.
4895 * NOTE that EXACT is NOT covered here, as it is normally
4896 * picked up by the optimiser separately.
4898 * This is unfortunate as the optimiser isnt handling lookahead
4899 * properly currently.
4902 while ((OP(first) == OPEN && (sawopen = 1)) ||
4903 /* An OR of *one* alternative - should not happen now. */
4904 (OP(first) == BRANCH && OP(first_next) != BRANCH) ||
4905 /* for now we can't handle lookbehind IFMATCH*/
4906 (OP(first) == IFMATCH && !first->flags && (sawlookahead = 1)) ||
4907 (OP(first) == PLUS) ||
4908 (OP(first) == MINMOD) ||
4909 /* An {n,m} with n>0 */
4910 (PL_regkind[OP(first)] == CURLY && ARG1(first) > 0) ||
4911 (OP(first) == NOTHING && PL_regkind[OP(first_next)] != END ))
4914 * the only op that could be a regnode is PLUS, all the rest
4915 * will be regnode_1 or regnode_2.
4918 if (OP(first) == PLUS)
4921 first += regarglen[OP(first)];
4923 first = NEXTOPER(first);
4924 first_next= regnext(first);
4927 /* Starting-point info. */
4929 DEBUG_PEEP("first:",first,0);
4930 /* Ignore EXACT as we deal with it later. */
4931 if (PL_regkind[OP(first)] == EXACT) {
4932 if (OP(first) == EXACT)
4933 NOOP; /* Empty, get anchored substr later. */
4935 ri->regstclass = first;
4938 else if (PL_regkind[OP(first)] == TRIE &&
4939 ((reg_trie_data *)ri->data->data[ ARG(first) ])->minlen>0)
4942 /* this can happen only on restudy */
4943 if ( OP(first) == TRIE ) {
4944 struct regnode_1 *trieop = (struct regnode_1 *)
4945 PerlMemShared_calloc(1, sizeof(struct regnode_1));
4946 StructCopy(first,trieop,struct regnode_1);
4947 trie_op=(regnode *)trieop;
4949 struct regnode_charclass *trieop = (struct regnode_charclass *)
4950 PerlMemShared_calloc(1, sizeof(struct regnode_charclass));
4951 StructCopy(first,trieop,struct regnode_charclass);
4952 trie_op=(regnode *)trieop;
4955 make_trie_failtable(pRExC_state, (regnode *)first, trie_op, 0);
4956 ri->regstclass = trie_op;
4959 else if (REGNODE_SIMPLE(OP(first)))
4960 ri->regstclass = first;
4961 else if (PL_regkind[OP(first)] == BOUND ||
4962 PL_regkind[OP(first)] == NBOUND)
4963 ri->regstclass = first;
4964 else if (PL_regkind[OP(first)] == BOL) {
4965 r->extflags |= (OP(first) == MBOL
4967 : (OP(first) == SBOL
4970 first = NEXTOPER(first);
4973 else if (OP(first) == GPOS) {
4974 r->extflags |= RXf_ANCH_GPOS;
4975 first = NEXTOPER(first);
4978 else if ((!sawopen || !RExC_sawback) &&
4979 (OP(first) == STAR &&
4980 PL_regkind[OP(NEXTOPER(first))] == REG_ANY) &&
4981 !(r->extflags & RXf_ANCH) && !(RExC_seen & REG_SEEN_EVAL))
4983 /* turn .* into ^.* with an implied $*=1 */
4985 (OP(NEXTOPER(first)) == REG_ANY)
4988 r->extflags |= type;
4989 r->intflags |= PREGf_IMPLICIT;
4990 first = NEXTOPER(first);
4993 if (sawplus && !sawlookahead && (!sawopen || !RExC_sawback)
4994 && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
4995 /* x+ must match at the 1st pos of run of x's */
4996 r->intflags |= PREGf_SKIP;
4998 /* Scan is after the zeroth branch, first is atomic matcher. */
4999 #ifdef TRIE_STUDY_OPT
5002 PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
5003 (IV)(first - scan + 1))
5007 PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
5008 (IV)(first - scan + 1))
5014 * If there's something expensive in the r.e., find the
5015 * longest literal string that must appear and make it the
5016 * regmust. Resolve ties in favor of later strings, since
5017 * the regstart check works with the beginning of the r.e.
5018 * and avoiding duplication strengthens checking. Not a
5019 * strong reason, but sufficient in the absence of others.
5020 * [Now we resolve ties in favor of the earlier string if
5021 * it happens that c_offset_min has been invalidated, since the
5022 * earlier string may buy us something the later one won't.]
5025 data.longest_fixed = newSVpvs("");
5026 data.longest_float = newSVpvs("");
5027 data.last_found = newSVpvs("");
5028 data.longest = &(data.longest_fixed);
5030 if (!ri->regstclass) {
5031 cl_init(pRExC_state, &ch_class);
5032 data.start_class = &ch_class;
5033 stclass_flag = SCF_DO_STCLASS_AND;
5034 } else /* XXXX Check for BOUND? */
5036 data.last_closep = &last_close;
5038 minlen = study_chunk(pRExC_state, &first, &minlen, &fake, scan + RExC_size, /* Up to end */
5039 &data, -1, NULL, NULL,
5040 SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag,0);
5046 if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
5047 && data.last_start_min == 0 && data.last_end > 0
5048 && !RExC_seen_zerolen
5049 && !(RExC_seen & REG_SEEN_VERBARG)
5050 && (!(RExC_seen & REG_SEEN_GPOS) || (r->extflags & RXf_ANCH_GPOS)))
5051 r->extflags |= RXf_CHECK_ALL;
5052 scan_commit(pRExC_state, &data,&minlen,0);
5053 SvREFCNT_dec(data.last_found);
5055 /* Note that code very similar to this but for anchored string
5056 follows immediately below, changes may need to be made to both.
5059 longest_float_length = CHR_SVLEN(data.longest_float);
5060 if (longest_float_length
5061 || (data.flags & SF_FL_BEFORE_EOL
5062 && (!(data.flags & SF_FL_BEFORE_MEOL)
5063 || (RExC_flags & RXf_PMf_MULTILINE))))
5067 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
5068 && data.offset_fixed == data.offset_float_min
5069 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
5070 goto remove_float; /* As in (a)+. */
5072 /* copy the information about the longest float from the reg_scan_data
5073 over to the program. */
5074 if (SvUTF8(data.longest_float)) {
5075 r->float_utf8 = data.longest_float;
5076 r->float_substr = NULL;
5078 r->float_substr = data.longest_float;
5079 r->float_utf8 = NULL;
5081 /* float_end_shift is how many chars that must be matched that
5082 follow this item. We calculate it ahead of time as once the
5083 lookbehind offset is added in we lose the ability to correctly
5085 ml = data.minlen_float ? *(data.minlen_float)
5086 : (I32)longest_float_length;
5087 r->float_end_shift = ml - data.offset_float_min
5088 - longest_float_length + (SvTAIL(data.longest_float) != 0)
5089 + data.lookbehind_float;
5090 r->float_min_offset = data.offset_float_min - data.lookbehind_float;
5091 r->float_max_offset = data.offset_float_max;
5092 if (data.offset_float_max < I32_MAX) /* Don't offset infinity */
5093 r->float_max_offset -= data.lookbehind_float;
5095 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
5096 && (!(data.flags & SF_FL_BEFORE_MEOL)
5097 || (RExC_flags & RXf_PMf_MULTILINE)));
5098 fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0);
5102 r->float_substr = r->float_utf8 = NULL;
5103 SvREFCNT_dec(data.longest_float);
5104 longest_float_length = 0;
5107 /* Note that code very similar to this but for floating string
5108 is immediately above, changes may need to be made to both.
5111 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
5112 if (longest_fixed_length
5113 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
5114 && (!(data.flags & SF_FIX_BEFORE_MEOL)
5115 || (RExC_flags & RXf_PMf_MULTILINE))))
5119 /* copy the information about the longest fixed
5120 from the reg_scan_data over to the program. */
5121 if (SvUTF8(data.longest_fixed)) {
5122 r->anchored_utf8 = data.longest_fixed;
5123 r->anchored_substr = NULL;
5125 r->anchored_substr = data.longest_fixed;
5126 r->anchored_utf8 = NULL;
5128 /* fixed_end_shift is how many chars that must be matched that
5129 follow this item. We calculate it ahead of time as once the
5130 lookbehind offset is added in we lose the ability to correctly
5132 ml = data.minlen_fixed ? *(data.minlen_fixed)
5133 : (I32)longest_fixed_length;
5134 r->anchored_end_shift = ml - data.offset_fixed
5135 - longest_fixed_length + (SvTAIL(data.longest_fixed) != 0)
5136 + data.lookbehind_fixed;
5137 r->anchored_offset = data.offset_fixed - data.lookbehind_fixed;
5139 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
5140 && (!(data.flags & SF_FIX_BEFORE_MEOL)
5141 || (RExC_flags & RXf_PMf_MULTILINE)));
5142 fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0);
5145 r->anchored_substr = r->anchored_utf8 = NULL;
5146 SvREFCNT_dec(data.longest_fixed);
5147 longest_fixed_length = 0;
5150 && (OP(ri->regstclass) == REG_ANY || OP(ri->regstclass) == SANY))
5151 ri->regstclass = NULL;
5153 if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset)
5155 && !(data.start_class->flags & ANYOF_EOS)
5156 && !cl_is_anything(data.start_class))
5158 const U32 n = add_data(pRExC_state, 1, "f");
5159 data.start_class->flags |= ANYOF_IS_SYNTHETIC;
5161 Newx(RExC_rxi->data->data[n], 1,
5162 struct regnode_charclass_class);
5163 StructCopy(data.start_class,
5164 (struct regnode_charclass_class*)RExC_rxi->data->data[n],
5165 struct regnode_charclass_class);
5166 ri->regstclass = (regnode*)RExC_rxi->data->data[n];
5167 r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
5168 DEBUG_COMPILE_r({ SV *sv = sv_newmortal();
5169 regprop(r, sv, (regnode*)data.start_class);
5170 PerlIO_printf(Perl_debug_log,
5171 "synthetic stclass \"%s\".\n",
5172 SvPVX_const(sv));});
5175 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
5176 if (longest_fixed_length > longest_float_length) {
5177 r->check_end_shift = r->anchored_end_shift;
5178 r->check_substr = r->anchored_substr;
5179 r->check_utf8 = r->anchored_utf8;
5180 r->check_offset_min = r->check_offset_max = r->anchored_offset;
5181 if (r->extflags & RXf_ANCH_SINGLE)
5182 r->extflags |= RXf_NOSCAN;
5185 r->check_end_shift = r->float_end_shift;
5186 r->check_substr = r->float_substr;
5187 r->check_utf8 = r->float_utf8;
5188 r->check_offset_min = r->float_min_offset;
5189 r->check_offset_max = r->float_max_offset;
5191 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
5192 This should be changed ASAP! */
5193 if ((r->check_substr || r->check_utf8) && !(r->extflags & RXf_ANCH_GPOS)) {
5194 r->extflags |= RXf_USE_INTUIT;
5195 if (SvTAIL(r->check_substr ? r->check_substr : r->check_utf8))
5196 r->extflags |= RXf_INTUIT_TAIL;
5198 /* XXX Unneeded? dmq (shouldn't as this is handled elsewhere)
5199 if ( (STRLEN)minlen < longest_float_length )
5200 minlen= longest_float_length;
5201 if ( (STRLEN)minlen < longest_fixed_length )
5202 minlen= longest_fixed_length;
5206 /* Several toplevels. Best we can is to set minlen. */
5208 struct regnode_charclass_class ch_class;
5211 DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "\nMulti Top Level\n"));
5213 scan = ri->program + 1;
5214 cl_init(pRExC_state, &ch_class);
5215 data.start_class = &ch_class;
5216 data.last_closep = &last_close;
5219 minlen = study_chunk(pRExC_state, &scan, &minlen, &fake, scan + RExC_size,
5220 &data, -1, NULL, NULL, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS,0);
5224 r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8
5225 = r->float_substr = r->float_utf8 = NULL;
5227 if (!(data.start_class->flags & ANYOF_EOS)
5228 && !cl_is_anything(data.start_class))
5230 const U32 n = add_data(pRExC_state, 1, "f");
5231 data.start_class->flags |= ANYOF_IS_SYNTHETIC;
5233 Newx(RExC_rxi->data->data[n], 1,
5234 struct regnode_charclass_class);
5235 StructCopy(data.start_class,
5236 (struct regnode_charclass_class*)RExC_rxi->data->data[n],
5237 struct regnode_charclass_class);
5238 ri->regstclass = (regnode*)RExC_rxi->data->data[n];
5239 r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
5240 DEBUG_COMPILE_r({ SV* sv = sv_newmortal();
5241 regprop(r, sv, (regnode*)data.start_class);
5242 PerlIO_printf(Perl_debug_log,
5243 "synthetic stclass \"%s\".\n",
5244 SvPVX_const(sv));});
5248 /* Guard against an embedded (?=) or (?<=) with a longer minlen than
5249 the "real" pattern. */
5251 PerlIO_printf(Perl_debug_log,"minlen: %"IVdf" r->minlen:%"IVdf"\n",
5252 (IV)minlen, (IV)r->minlen);
5254 r->minlenret = minlen;
5255 if (r->minlen < minlen)
5258 if (RExC_seen & REG_SEEN_GPOS)
5259 r->extflags |= RXf_GPOS_SEEN;
5260 if (RExC_seen & REG_SEEN_LOOKBEHIND)
5261 r->extflags |= RXf_LOOKBEHIND_SEEN;
5262 if (RExC_seen & REG_SEEN_EVAL)
5263 r->extflags |= RXf_EVAL_SEEN;
5264 if (RExC_seen & REG_SEEN_CANY)
5265 r->extflags |= RXf_CANY_SEEN;
5266 if (RExC_seen & REG_SEEN_VERBARG)
5267 r->intflags |= PREGf_VERBARG_SEEN;
5268 if (RExC_seen & REG_SEEN_CUTGROUP)
5269 r->intflags |= PREGf_CUTGROUP_SEEN;
5270 if (RExC_paren_names)
5271 RXp_PAREN_NAMES(r) = MUTABLE_HV(SvREFCNT_inc(RExC_paren_names));
5273 RXp_PAREN_NAMES(r) = NULL;
5275 #ifdef STUPID_PATTERN_CHECKS
5276 if (RX_PRELEN(rx) == 0)
5277 r->extflags |= RXf_NULL;
5278 if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ')
5279 /* XXX: this should happen BEFORE we compile */
5280 r->extflags |= (RXf_SKIPWHITE|RXf_WHITE);
5281 else if (RX_PRELEN(rx) == 3 && memEQ("\\s+", RX_PRECOMP(rx), 3))
5282 r->extflags |= RXf_WHITE;
5283 else if (RX_PRELEN(rx) == 1 && RXp_PRECOMP(rx)[0] == '^')
5284 r->extflags |= RXf_START_ONLY;
5286 if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ')
5287 /* XXX: this should happen BEFORE we compile */
5288 r->extflags |= (RXf_SKIPWHITE|RXf_WHITE);
5290 regnode *first = ri->program + 1;
5293 if (PL_regkind[fop] == NOTHING && OP(NEXTOPER(first)) == END)
5294 r->extflags |= RXf_NULL;
5295 else if (PL_regkind[fop] == BOL && OP(NEXTOPER(first)) == END)
5296 r->extflags |= RXf_START_ONLY;
5297 else if (fop == PLUS && OP(NEXTOPER(first)) == SPACE
5298 && OP(regnext(first)) == END)
5299 r->extflags |= RXf_WHITE;
5303 if (RExC_paren_names) {
5304 ri->name_list_idx = add_data( pRExC_state, 1, "a" );
5305 ri->data->data[ri->name_list_idx] = (void*)SvREFCNT_inc(RExC_paren_name_list);
5308 ri->name_list_idx = 0;
5310 if (RExC_recurse_count) {
5311 for ( ; RExC_recurse_count ; RExC_recurse_count-- ) {
5312 const regnode *scan = RExC_recurse[RExC_recurse_count-1];
5313 ARG2L_SET( scan, RExC_open_parens[ARG(scan)-1] - scan );
5316 Newxz(r->offs, RExC_npar, regexp_paren_pair);
5317 /* assume we don't need to swap parens around before we match */
5320 PerlIO_printf(Perl_debug_log,"Final program:\n");
5323 #ifdef RE_TRACK_PATTERN_OFFSETS
5324 DEBUG_OFFSETS_r(if (ri->u.offsets) {
5325 const U32 len = ri->u.offsets[0];
5327 GET_RE_DEBUG_FLAGS_DECL;
5328 PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)ri->u.offsets[0]);
5329 for (i = 1; i <= len; i++) {
5330 if (ri->u.offsets[i*2-1] || ri->u.offsets[i*2])
5331 PerlIO_printf(Perl_debug_log, "%"UVuf":%"UVuf"[%"UVuf"] ",
5332 (UV)i, (UV)ri->u.offsets[i*2-1], (UV)ri->u.offsets[i*2]);
5334 PerlIO_printf(Perl_debug_log, "\n");
5340 #undef RE_ENGINE_PTR
5344 Perl_reg_named_buff(pTHX_ REGEXP * const rx, SV * const key, SV * const value,
5347 PERL_ARGS_ASSERT_REG_NAMED_BUFF;
5349 PERL_UNUSED_ARG(value);
5351 if (flags & RXapif_FETCH) {
5352 return reg_named_buff_fetch(rx, key, flags);
5353 } else if (flags & (RXapif_STORE | RXapif_DELETE | RXapif_CLEAR)) {
5354 Perl_croak_no_modify(aTHX);
5356 } else if (flags & RXapif_EXISTS) {
5357 return reg_named_buff_exists(rx, key, flags)
5360 } else if (flags & RXapif_REGNAMES) {
5361 return reg_named_buff_all(rx, flags);
5362 } else if (flags & (RXapif_SCALAR | RXapif_REGNAMES_COUNT)) {
5363 return reg_named_buff_scalar(rx, flags);
5365 Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff", (int)flags);
5371 Perl_reg_named_buff_iter(pTHX_ REGEXP * const rx, const SV * const lastkey,
5374 PERL_ARGS_ASSERT_REG_NAMED_BUFF_ITER;
5375 PERL_UNUSED_ARG(lastkey);
5377 if (flags & RXapif_FIRSTKEY)
5378 return reg_named_buff_firstkey(rx, flags);
5379 else if (flags & RXapif_NEXTKEY)
5380 return reg_named_buff_nextkey(rx, flags);
5382 Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_iter", (int)flags);
5388 Perl_reg_named_buff_fetch(pTHX_ REGEXP * const r, SV * const namesv,
5391 AV *retarray = NULL;
5393 struct regexp *const rx = (struct regexp *)SvANY(r);
5395 PERL_ARGS_ASSERT_REG_NAMED_BUFF_FETCH;
5397 if (flags & RXapif_ALL)
5400 if (rx && RXp_PAREN_NAMES(rx)) {
5401 HE *he_str = hv_fetch_ent( RXp_PAREN_NAMES(rx), namesv, 0, 0 );
5404 SV* sv_dat=HeVAL(he_str);
5405 I32 *nums=(I32*)SvPVX(sv_dat);
5406 for ( i=0; i<SvIVX(sv_dat); i++ ) {
5407 if ((I32)(rx->nparens) >= nums[i]
5408 && rx->offs[nums[i]].start != -1
5409 && rx->offs[nums[i]].end != -1)
5412 CALLREG_NUMBUF_FETCH(r,nums[i],ret);
5416 ret = newSVsv(&PL_sv_undef);
5419 av_push(retarray, ret);
5422 return newRV_noinc(MUTABLE_SV(retarray));
5429 Perl_reg_named_buff_exists(pTHX_ REGEXP * const r, SV * const key,
5432 struct regexp *const rx = (struct regexp *)SvANY(r);
5434 PERL_ARGS_ASSERT_REG_NAMED_BUFF_EXISTS;
5436 if (rx && RXp_PAREN_NAMES(rx)) {
5437 if (flags & RXapif_ALL) {
5438 return hv_exists_ent(RXp_PAREN_NAMES(rx), key, 0);
5440 SV *sv = CALLREG_NAMED_BUFF_FETCH(r, key, flags);
5454 Perl_reg_named_buff_firstkey(pTHX_ REGEXP * const r, const U32 flags)
5456 struct regexp *const rx = (struct regexp *)SvANY(r);
5458 PERL_ARGS_ASSERT_REG_NAMED_BUFF_FIRSTKEY;
5460 if ( rx && RXp_PAREN_NAMES(rx) ) {
5461 (void)hv_iterinit(RXp_PAREN_NAMES(rx));
5463 return CALLREG_NAMED_BUFF_NEXTKEY(r, NULL, flags & ~RXapif_FIRSTKEY);
5470 Perl_reg_named_buff_nextkey(pTHX_ REGEXP * const r, const U32 flags)
5472 struct regexp *const rx = (struct regexp *)SvANY(r);
5473 GET_RE_DEBUG_FLAGS_DECL;
5475 PERL_ARGS_ASSERT_REG_NAMED_BUFF_NEXTKEY;
5477 if (rx && RXp_PAREN_NAMES(rx)) {
5478 HV *hv = RXp_PAREN_NAMES(rx);
5480 while ( (temphe = hv_iternext_flags(hv,0)) ) {
5483 SV* sv_dat = HeVAL(temphe);
5484 I32 *nums = (I32*)SvPVX(sv_dat);
5485 for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5486 if ((I32)(rx->lastparen) >= nums[i] &&
5487 rx->offs[nums[i]].start != -1 &&
5488 rx->offs[nums[i]].end != -1)
5494 if (parno || flags & RXapif_ALL) {
5495 return newSVhek(HeKEY_hek(temphe));
5503 Perl_reg_named_buff_scalar(pTHX_ REGEXP * const r, const U32 flags)
5508 struct regexp *const rx = (struct regexp *)SvANY(r);
5510 PERL_ARGS_ASSERT_REG_NAMED_BUFF_SCALAR;
5512 if (rx && RXp_PAREN_NAMES(rx)) {
5513 if (flags & (RXapif_ALL | RXapif_REGNAMES_COUNT)) {
5514 return newSViv(HvTOTALKEYS(RXp_PAREN_NAMES(rx)));
5515 } else if (flags & RXapif_ONE) {
5516 ret = CALLREG_NAMED_BUFF_ALL(r, (flags | RXapif_REGNAMES));
5517 av = MUTABLE_AV(SvRV(ret));
5518 length = av_len(av);
5520 return newSViv(length + 1);
5522 Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_scalar", (int)flags);
5526 return &PL_sv_undef;
5530 Perl_reg_named_buff_all(pTHX_ REGEXP * const r, const U32 flags)
5532 struct regexp *const rx = (struct regexp *)SvANY(r);
5535 PERL_ARGS_ASSERT_REG_NAMED_BUFF_ALL;
5537 if (rx && RXp_PAREN_NAMES(rx)) {
5538 HV *hv= RXp_PAREN_NAMES(rx);
5540 (void)hv_iterinit(hv);
5541 while ( (temphe = hv_iternext_flags(hv,0)) ) {
5544 SV* sv_dat = HeVAL(temphe);
5545 I32 *nums = (I32*)SvPVX(sv_dat);
5546 for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5547 if ((I32)(rx->lastparen) >= nums[i] &&
5548 rx->offs[nums[i]].start != -1 &&
5549 rx->offs[nums[i]].end != -1)
5555 if (parno || flags & RXapif_ALL) {
5556 av_push(av, newSVhek(HeKEY_hek(temphe)));
5561 return newRV_noinc(MUTABLE_SV(av));
5565 Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
5568 struct regexp *const rx = (struct regexp *)SvANY(r);
5573 PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH;
5576 sv_setsv(sv,&PL_sv_undef);
5580 if (paren == RX_BUFF_IDX_PREMATCH && rx->offs[0].start != -1) {
5582 i = rx->offs[0].start;
5586 if (paren == RX_BUFF_IDX_POSTMATCH && rx->offs[0].end != -1) {
5588 s = rx->subbeg + rx->offs[0].end;
5589 i = rx->sublen - rx->offs[0].end;
5592 if ( 0 <= paren && paren <= (I32)rx->nparens &&
5593 (s1 = rx->offs[paren].start) != -1 &&
5594 (t1 = rx->offs[paren].end) != -1)
5598 s = rx->subbeg + s1;
5600 sv_setsv(sv,&PL_sv_undef);
5603 assert(rx->sublen >= (s - rx->subbeg) + i );
5605 const int oldtainted = PL_tainted;
5607 sv_setpvn(sv, s, i);
5608 PL_tainted = oldtainted;
5609 if ( (rx->extflags & RXf_CANY_SEEN)
5610 ? (RXp_MATCH_UTF8(rx)
5611 && (!i || is_utf8_string((U8*)s, i)))
5612 : (RXp_MATCH_UTF8(rx)) )
5619 if (RXp_MATCH_TAINTED(rx)) {
5620 if (SvTYPE(sv) >= SVt_PVMG) {
5621 MAGIC* const mg = SvMAGIC(sv);
5624 SvMAGIC_set(sv, mg->mg_moremagic);
5626 if ((mgt = SvMAGIC(sv))) {
5627 mg->mg_moremagic = mgt;
5628 SvMAGIC_set(sv, mg);
5638 sv_setsv(sv,&PL_sv_undef);
5644 Perl_reg_numbered_buff_store(pTHX_ REGEXP * const rx, const I32 paren,
5645 SV const * const value)
5647 PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_STORE;
5649 PERL_UNUSED_ARG(rx);
5650 PERL_UNUSED_ARG(paren);
5651 PERL_UNUSED_ARG(value);
5654 Perl_croak_no_modify(aTHX);
5658 Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv,
5661 struct regexp *const rx = (struct regexp *)SvANY(r);
5665 PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_LENGTH;
5667 /* Some of this code was originally in C<Perl_magic_len> in F<mg.c> */
5669 /* $` / ${^PREMATCH} */
5670 case RX_BUFF_IDX_PREMATCH:
5671 if (rx->offs[0].start != -1) {
5672 i = rx->offs[0].start;
5680 /* $' / ${^POSTMATCH} */
5681 case RX_BUFF_IDX_POSTMATCH:
5682 if (rx->offs[0].end != -1) {
5683 i = rx->sublen - rx->offs[0].end;
5685 s1 = rx->offs[0].end;
5691 /* $& / ${^MATCH}, $1, $2, ... */
5693 if (paren <= (I32)rx->nparens &&
5694 (s1 = rx->offs[paren].start) != -1 &&
5695 (t1 = rx->offs[paren].end) != -1)
5700 if (ckWARN(WARN_UNINITIALIZED))
5701 report_uninit((const SV *)sv);
5706 if (i > 0 && RXp_MATCH_UTF8(rx)) {
5707 const char * const s = rx->subbeg + s1;
5712 if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
5719 Perl_reg_qr_package(pTHX_ REGEXP * const rx)
5721 PERL_ARGS_ASSERT_REG_QR_PACKAGE;
5722 PERL_UNUSED_ARG(rx);
5726 return newSVpvs("Regexp");
5729 /* Scans the name of a named buffer from the pattern.
5730 * If flags is REG_RSN_RETURN_NULL returns null.
5731 * If flags is REG_RSN_RETURN_NAME returns an SV* containing the name
5732 * If flags is REG_RSN_RETURN_DATA returns the data SV* corresponding
5733 * to the parsed name as looked up in the RExC_paren_names hash.
5734 * If there is an error throws a vFAIL().. type exception.
5737 #define REG_RSN_RETURN_NULL 0
5738 #define REG_RSN_RETURN_NAME 1
5739 #define REG_RSN_RETURN_DATA 2
5742 S_reg_scan_name(pTHX_ RExC_state_t *pRExC_state, U32 flags)
5744 char *name_start = RExC_parse;
5746 PERL_ARGS_ASSERT_REG_SCAN_NAME;
5748 if (isIDFIRST_lazy_if(RExC_parse, UTF)) {
5749 /* skip IDFIRST by using do...while */
5752 RExC_parse += UTF8SKIP(RExC_parse);
5753 } while (isALNUM_utf8((U8*)RExC_parse));
5757 } while (isALNUM(*RExC_parse));
5762 = newSVpvn_flags(name_start, (int)(RExC_parse - name_start),
5763 SVs_TEMP | (UTF ? SVf_UTF8 : 0));
5764 if ( flags == REG_RSN_RETURN_NAME)
5766 else if (flags==REG_RSN_RETURN_DATA) {
5769 if ( ! sv_name ) /* should not happen*/
5770 Perl_croak(aTHX_ "panic: no svname in reg_scan_name");
5771 if (RExC_paren_names)
5772 he_str = hv_fetch_ent( RExC_paren_names, sv_name, 0, 0 );
5774 sv_dat = HeVAL(he_str);
5776 vFAIL("Reference to nonexistent named group");
5780 Perl_croak(aTHX_ "panic: bad flag in reg_scan_name");
5787 #define DEBUG_PARSE_MSG(funcname) DEBUG_PARSE_r({ \
5788 int rem=(int)(RExC_end - RExC_parse); \
5797 if (RExC_lastparse!=RExC_parse) \
5798 PerlIO_printf(Perl_debug_log," >%.*s%-*s", \
5801 iscut ? "..." : "<" \
5804 PerlIO_printf(Perl_debug_log,"%16s",""); \
5807 num = RExC_size + 1; \
5809 num=REG_NODE_NUM(RExC_emit); \
5810 if (RExC_lastnum!=num) \
5811 PerlIO_printf(Perl_debug_log,"|%4d",num); \
5813 PerlIO_printf(Perl_debug_log,"|%4s",""); \
5814 PerlIO_printf(Perl_debug_log,"|%*s%-4s", \
5815 (int)((depth*2)), "", \
5819 RExC_lastparse=RExC_parse; \
5824 #define DEBUG_PARSE(funcname) DEBUG_PARSE_r({ \
5825 DEBUG_PARSE_MSG((funcname)); \
5826 PerlIO_printf(Perl_debug_log,"%4s","\n"); \
5828 #define DEBUG_PARSE_FMT(funcname,fmt,args) DEBUG_PARSE_r({ \
5829 DEBUG_PARSE_MSG((funcname)); \
5830 PerlIO_printf(Perl_debug_log,fmt "\n",args); \
5833 /* This section of code defines the inversion list object and its methods. The
5834 * interfaces are highly subject to change, so as much as possible is static to
5835 * this file. An inversion list is here implemented as a malloc'd C array with
5836 * some added info. More will be coming when functionality is added later.
5838 * Some of the methods should always be private to the implementation, and some
5839 * should eventually be made public */
5841 #define INVLIST_INITIAL_LEN 10
5842 #define INVLIST_ARRAY_KEY "array"
5843 #define INVLIST_MAX_KEY "max"
5844 #define INVLIST_LEN_KEY "len"
5846 PERL_STATIC_INLINE UV*
5847 S_invlist_array(pTHX_ HV* const invlist)
5849 /* Returns the pointer to the inversion list's array. Every time the
5850 * length changes, this needs to be called in case malloc or realloc moved
5853 SV** list_ptr = hv_fetchs(invlist, INVLIST_ARRAY_KEY, FALSE);
5855 PERL_ARGS_ASSERT_INVLIST_ARRAY;
5857 if (list_ptr == NULL) {
5858 Perl_croak(aTHX_ "panic: inversion list without a '%s' element",
5862 return INT2PTR(UV *, SvUV(*list_ptr));
5865 PERL_STATIC_INLINE void
5866 S_invlist_set_array(pTHX_ HV* const invlist, const UV* const array)
5868 PERL_ARGS_ASSERT_INVLIST_SET_ARRAY;
5870 /* Sets the array stored in the inversion list to the memory beginning with
5873 if (hv_stores(invlist, INVLIST_ARRAY_KEY, newSVuv(PTR2UV(array))) == NULL) {
5874 Perl_croak(aTHX_ "panic: can't store '%s' entry in inversion list",
5879 PERL_STATIC_INLINE UV
5880 S_invlist_len(pTHX_ HV* const invlist)
5882 /* Returns the current number of elements in the inversion list's array */
5884 SV** len_ptr = hv_fetchs(invlist, INVLIST_LEN_KEY, FALSE);
5886 PERL_ARGS_ASSERT_INVLIST_LEN;
5888 if (len_ptr == NULL) {
5889 Perl_croak(aTHX_ "panic: inversion list without a '%s' element",
5893 return SvUV(*len_ptr);
5896 PERL_STATIC_INLINE UV
5897 S_invlist_max(pTHX_ HV* const invlist)
5899 /* Returns the maximum number of elements storable in the inversion list's
5900 * array, without having to realloc() */
5902 SV** max_ptr = hv_fetchs(invlist, INVLIST_MAX_KEY, FALSE);
5904 PERL_ARGS_ASSERT_INVLIST_MAX;
5906 if (max_ptr == NULL) {
5907 Perl_croak(aTHX_ "panic: inversion list without a '%s' element",
5911 return SvUV(*max_ptr);
5914 PERL_STATIC_INLINE void
5915 S_invlist_set_len(pTHX_ HV* const invlist, const UV len)
5917 /* Sets the current number of elements stored in the inversion list */
5919 PERL_ARGS_ASSERT_INVLIST_SET_LEN;
5921 if (len != 0 && len > invlist_max(invlist)) {
5922 Perl_croak(aTHX_ "panic: Can't make '%s=%"UVuf"' more than %s=%"UVuf" in inversion list", INVLIST_LEN_KEY, len, INVLIST_MAX_KEY, invlist_max(invlist));
5925 if (hv_stores(invlist, INVLIST_LEN_KEY, newSVuv(len)) == NULL) {
5926 Perl_croak(aTHX_ "panic: can't store '%s' entry in inversion list",
5931 PERL_STATIC_INLINE void
5932 S_invlist_set_max(pTHX_ HV* const invlist, const UV max)
5935 /* Sets the maximum number of elements storable in the inversion list
5936 * without having to realloc() */
5938 PERL_ARGS_ASSERT_INVLIST_SET_MAX;
5940 if (max < invlist_len(invlist)) {
5941 Perl_croak(aTHX_ "panic: Can't make '%s=%"UVuf"' less than %s=%"UVuf" in inversion list", INVLIST_MAX_KEY, invlist_len(invlist), INVLIST_LEN_KEY, invlist_max(invlist));
5944 if (hv_stores(invlist, INVLIST_MAX_KEY, newSVuv(max)) == NULL) {
5945 Perl_croak(aTHX_ "panic: can't store '%s' entry in inversion list",
5950 #ifndef PERL_IN_XSUB_RE
5952 Perl__new_invlist(pTHX_ IV initial_size)
5955 /* Return a pointer to a newly constructed inversion list, with enough
5956 * space to store 'initial_size' elements. If that number is negative, a
5957 * system default is used instead */
5959 HV* invlist = newHV();
5962 if (initial_size < 0) {
5963 initial_size = INVLIST_INITIAL_LEN;
5966 /* Allocate the initial space */
5967 Newx(list, initial_size, UV);
5968 invlist_set_array(invlist, list);
5970 /* set_len has to come before set_max, as the latter inspects the len */
5971 invlist_set_len(invlist, 0);
5972 invlist_set_max(invlist, initial_size);
5978 PERL_STATIC_INLINE void
5979 S_invlist_destroy(pTHX_ HV* const invlist)
5981 /* Inversion list destructor */
5983 SV** list_ptr = hv_fetchs(invlist, INVLIST_ARRAY_KEY, FALSE);
5985 PERL_ARGS_ASSERT_INVLIST_DESTROY;
5987 if (list_ptr != NULL) {
5988 UV *list = INT2PTR(UV *, SvUV(*list_ptr)); /* PERL_POISON needs lvalue */
5994 S_invlist_extend(pTHX_ HV* const invlist, const UV new_max)
5996 /* Change the maximum size of an inversion list (up or down) */
6000 const UV old_max = invlist_max(invlist);
6002 PERL_ARGS_ASSERT_INVLIST_EXTEND;
6004 if (old_max == new_max) { /* If a no-op */
6008 array = orig_array = invlist_array(invlist);
6009 Renew(array, new_max, UV);
6011 /* If the size change moved the list in memory, set the new one */
6012 if (array != orig_array) {
6013 invlist_set_array(invlist, array);
6016 invlist_set_max(invlist, new_max);
6020 PERL_STATIC_INLINE void
6021 S_invlist_trim(pTHX_ HV* const invlist)
6023 PERL_ARGS_ASSERT_INVLIST_TRIM;
6025 /* Change the length of the inversion list to how many entries it currently
6028 invlist_extend(invlist, invlist_len(invlist));
6031 /* An element is in an inversion list iff its index is even numbered: 0, 2, 4,
6034 #define ELEMENT_IN_INVLIST_SET(i) (! ((i) & 1))
6036 #ifndef PERL_IN_XSUB_RE
6038 Perl__append_range_to_invlist(pTHX_ HV* const invlist, const UV start, const UV end)
6040 /* Subject to change or removal. Append the range from 'start' to 'end' at
6041 * the end of the inversion list. The range must be above any existing
6044 UV* array = invlist_array(invlist);
6045 UV max = invlist_max(invlist);
6046 UV len = invlist_len(invlist);
6048 PERL_ARGS_ASSERT__APPEND_RANGE_TO_INVLIST;
6052 /* Here, the existing list is non-empty. The current max entry in the
6053 * list is generally the first value not in the set, except when the
6054 * set extends to the end of permissible values, in which case it is
6055 * the first entry in that final set, and so this call is an attempt to
6056 * append out-of-order */
6058 UV final_element = len - 1;
6059 if (array[final_element] > start
6060 || ELEMENT_IN_INVLIST_SET(final_element))
6062 Perl_croak(aTHX_ "panic: attempting to append to an inversion list, but wasn't at the end of the list");
6065 /* Here, it is a legal append. If the new range begins with the first
6066 * value not in the set, it is extending the set, so the new first
6067 * value not in the set is one greater than the newly extended range.
6069 if (array[final_element] == start) {
6070 if (end != UV_MAX) {
6071 array[final_element] = end + 1;
6074 /* But if the end is the maximum representable on the machine,
6075 * just let the range that this would extend have no end */
6076 invlist_set_len(invlist, len - 1);
6082 /* Here the new range doesn't extend any existing set. Add it */
6084 len += 2; /* Includes an element each for the start and end of range */
6086 /* If overflows the existing space, extend, which may cause the array to be
6089 invlist_extend(invlist, len);
6090 array = invlist_array(invlist);
6093 invlist_set_len(invlist, len);
6095 /* The next item on the list starts the range, the one after that is
6096 * one past the new range. */
6097 array[len - 2] = start;
6098 if (end != UV_MAX) {
6099 array[len - 1] = end + 1;
6102 /* But if the end is the maximum representable on the machine, just let
6103 * the range have no end */
6104 invlist_set_len(invlist, len - 1);
6110 S_invlist_union(pTHX_ HV* const a, HV* const b)
6112 /* Return a new inversion list which is the union of two inversion lists.
6113 * The basis for this comes from "Unicode Demystified" Chapter 13 by
6114 * Richard Gillam, published by Addison-Wesley, and explained at some
6115 * length there. The preface says to incorporate its examples into your
6116 * code at your own risk.
6118 * The algorithm is like a merge sort.
6120 * XXX A potential performance improvement is to keep track as we go along
6121 * if only one of the inputs contributes to the result, meaning the other
6122 * is a subset of that one. In that case, we can skip the final copy and
6123 * return the larger of the input lists */
6125 UV* array_a = invlist_array(a); /* a's array */
6126 UV* array_b = invlist_array(b);
6127 UV len_a = invlist_len(a); /* length of a's array */
6128 UV len_b = invlist_len(b);
6130 HV* u; /* the resulting union */
6134 UV i_a = 0; /* current index into a's array */
6138 /* running count, as explained in the algorithm source book; items are
6139 * stopped accumulating and are output when the count changes to/from 0.
6140 * The count is incremented when we start a range that's in the set, and
6141 * decremented when we start a range that's not in the set. So its range
6142 * is 0 to 2. Only when the count is zero is something not in the set.
6146 PERL_ARGS_ASSERT_INVLIST_UNION;
6148 /* Size the union for the worst case: that the sets are completely
6150 u = _new_invlist(len_a + len_b);
6151 array_u = invlist_array(u);
6153 /* Go through each list item by item, stopping when exhausted one of
6155 while (i_a < len_a && i_b < len_b) {
6156 UV cp; /* The element to potentially add to the union's array */
6157 bool cp_in_set; /* is it in the the input list's set or not */
6159 /* We need to take one or the other of the two inputs for the union.
6160 * Since we are merging two sorted lists, we take the smaller of the
6161 * next items. In case of a tie, we take the one that is in its set
6162 * first. If we took one not in the set first, it would decrement the
6163 * count, possibly to 0 which would cause it to be output as ending the
6164 * range, and the next time through we would take the same number, and
6165 * output it again as beginning the next range. By doing it the
6166 * opposite way, there is no possibility that the count will be
6167 * momentarily decremented to 0, and thus the two adjoining ranges will
6168 * be seamlessly merged. (In a tie and both are in the set or both not
6169 * in the set, it doesn't matter which we take first.) */
6170 if (array_a[i_a] < array_b[i_b]
6171 || (array_a[i_a] == array_b[i_b] && ELEMENT_IN_INVLIST_SET(i_a)))
6173 cp_in_set = ELEMENT_IN_INVLIST_SET(i_a);
6177 cp_in_set = ELEMENT_IN_INVLIST_SET(i_b);
6181 /* Here, have chosen which of the two inputs to look at. Only output
6182 * if the running count changes to/from 0, which marks the
6183 * beginning/end of a range in that's in the set */
6186 array_u[i_u++] = cp;
6193 array_u[i_u++] = cp;
6198 /* Here, we are finished going through at least one of the lists, which
6199 * means there is something remaining in at most one. We check if the list
6200 * that hasn't been exhausted is positioned such that we are in the middle
6201 * of a range in its set or not. (We are in the set if the next item in
6202 * the array marks the beginning of something not in the set) If in the
6203 * set, we decrement 'count'; if 0, there is potentially more to output.
6204 * There are four cases:
6205 * 1) Both weren't in their sets, count is 0, and remains 0. What's left
6206 * in the union is entirely from the non-exhausted set.
6207 * 2) Both were in their sets, count is 2. Nothing further should
6208 * be output, as everything that remains will be in the exhausted
6209 * list's set, hence in the union; decrementing to 1 but not 0 insures
6211 * 3) the exhausted was in its set, non-exhausted isn't, count is 1.
6212 * Nothing further should be output because the union includes
6213 * everything from the exhausted set. Not decrementing insures that.
6214 * 4) the exhausted wasn't in its set, non-exhausted is, count is 1;
6215 * decrementing to 0 insures that we look at the remainder of the
6216 * non-exhausted set */
6217 if ((i_a != len_a && ! ELEMENT_IN_INVLIST_SET(i_a))
6218 || (i_b != len_b && ! ELEMENT_IN_INVLIST_SET(i_b)))
6223 /* The final length is what we've output so far, plus what else is about to
6224 * be output. (If 'count' is non-zero, then the input list we exhausted
6225 * has everything remaining up to the machine's limit in its set, and hence
6226 * in the union, so there will be no further output. */
6229 /* At most one of the subexpressions will be non-zero */
6230 len_u += (len_a - i_a) + (len_b - i_b);
6233 /* Set result to final length, which can change the pointer to array_u, so
6235 if (len_u != invlist_len(u)) {
6236 invlist_set_len(u, len_u);
6238 array_u = invlist_array(u);
6241 /* When 'count' is 0, the list that was exhausted (if one was shorter than
6242 * the other) ended with everything above it not in its set. That means
6243 * that the remaining part of the union is precisely the same as the
6244 * non-exhausted list, so can just copy it unchanged. (If both list were
6245 * exhausted at the same time, then the operations below will be both 0.)
6248 IV copy_count; /* At most one will have a non-zero copy count */
6249 if ((copy_count = len_a - i_a) > 0) {
6250 Copy(array_a + i_a, array_u + i_u, copy_count, UV);
6252 else if ((copy_count = len_b - i_b) > 0) {
6253 Copy(array_b + i_b, array_u + i_u, copy_count, UV);
6261 S_invlist_intersection(pTHX_ HV* const a, HV* const b)
6263 /* Return the intersection of two inversion lists. The basis for this
6264 * comes from "Unicode Demystified" Chapter 13 by Richard Gillam, published
6265 * by Addison-Wesley, and explained at some length there. The preface says
6266 * to incorporate its examples into your code at your own risk.
6268 * The algorithm is like a merge sort, and is essentially the same as the
6272 UV* array_a = invlist_array(a); /* a's array */
6273 UV* array_b = invlist_array(b);
6274 UV len_a = invlist_len(a); /* length of a's array */
6275 UV len_b = invlist_len(b);
6277 HV* r; /* the resulting intersection */
6281 UV i_a = 0; /* current index into a's array */
6285 /* running count, as explained in the algorithm source book; items are
6286 * stopped accumulating and are output when the count changes to/from 2.
6287 * The count is incremented when we start a range that's in the set, and
6288 * decremented when we start a range that's not in the set. So its range
6289 * is 0 to 2. Only when the count is 2 is something in the intersection.
6293 PERL_ARGS_ASSERT_INVLIST_INTERSECTION;
6295 /* Size the intersection for the worst case: that the intersection ends up
6296 * fragmenting everything to be completely disjoint */
6297 r= _new_invlist(len_a + len_b);
6298 array_r = invlist_array(r);
6300 /* Go through each list item by item, stopping when exhausted one of
6302 while (i_a < len_a && i_b < len_b) {
6303 UV cp; /* The element to potentially add to the intersection's
6305 bool cp_in_set; /* Is it in the input list's set or not */
6307 /* We need to take one or the other of the two inputs for the union.
6308 * Since we are merging two sorted lists, we take the smaller of the
6309 * next items. In case of a tie, we take the one that is not in its
6310 * set first (a difference from the union algorithm). If we took one
6311 * in the set first, it would increment the count, possibly to 2 which
6312 * would cause it to be output as starting a range in the intersection,
6313 * and the next time through we would take that same number, and output
6314 * it again as ending the set. By doing it the opposite of this, we
6315 * there is no possibility that the count will be momentarily
6316 * incremented to 2. (In a tie and both are in the set or both not in
6317 * the set, it doesn't matter which we take first.) */
6318 if (array_a[i_a] < array_b[i_b]
6319 || (array_a[i_a] == array_b[i_b] && ! ELEMENT_IN_INVLIST_SET(i_a)))
6321 cp_in_set = ELEMENT_IN_INVLIST_SET(i_a);
6325 cp_in_set = ELEMENT_IN_INVLIST_SET(i_b);
6329 /* Here, have chosen which of the two inputs to look at. Only output
6330 * if the running count changes to/from 2, which marks the
6331 * beginning/end of a range that's in the intersection */
6335 array_r[i_r++] = cp;
6340 array_r[i_r++] = cp;
6346 /* Here, we are finished going through at least one of the sets, which
6347 * means there is something remaining in at most one. See the comments in
6349 if ((i_a != len_a && ! ELEMENT_IN_INVLIST_SET(i_a))
6350 || (i_b != len_b && ! ELEMENT_IN_INVLIST_SET(i_b)))
6355 /* The final length is what we've output so far plus what else is in the
6356 * intersection. Only one of the subexpressions below will be non-zero */
6359 len_r += (len_a - i_a) + (len_b - i_b);
6362 /* Set result to final length, which can change the pointer to array_r, so
6364 if (len_r != invlist_len(r)) {
6365 invlist_set_len(r, len_r);
6367 array_r = invlist_array(r);
6370 /* Finish outputting any remaining */
6371 if (count == 2) { /* Only one of will have a non-zero copy count */
6373 if ((copy_count = len_a - i_a) > 0) {
6374 Copy(array_a + i_a, array_r + i_r, copy_count, UV);
6376 else if ((copy_count = len_b - i_b) > 0) {
6377 Copy(array_b + i_b, array_r + i_r, copy_count, UV);
6385 S_add_range_to_invlist(pTHX_ HV* invlist, const UV start, const UV end)
6387 /* Add the range from 'start' to 'end' inclusive to the inversion list's
6388 * set. A pointer to the inversion list is returned. This may actually be
6389 * a new list, in which case the passed in one has been destroyed. The
6390 * passed in inversion list can be NULL, in which case a new one is created
6391 * with just the one range in it */
6397 if (invlist == NULL) {
6398 invlist = _new_invlist(2);
6402 len = invlist_len(invlist);
6405 /* If comes after the final entry, can just append it to the end */
6407 || start >= invlist_array(invlist)
6408 [invlist_len(invlist) - 1])
6410 _append_range_to_invlist(invlist, start, end);
6414 /* Here, can't just append things, create and return a new inversion list
6415 * which is the union of this range and the existing inversion list */
6416 range_invlist = _new_invlist(2);
6417 _append_range_to_invlist(range_invlist, start, end);
6419 added_invlist = invlist_union(invlist, range_invlist);
6421 /* The passed in list can be freed, as well as our temporary */
6422 invlist_destroy(range_invlist);
6423 if (invlist != added_invlist) {
6424 invlist_destroy(invlist);
6427 return added_invlist;
6430 PERL_STATIC_INLINE HV*
6431 S_add_cp_to_invlist(pTHX_ HV* invlist, const UV cp) {
6432 return add_range_to_invlist(invlist, cp, cp);
6435 /* End of inversion list object */
6438 - reg - regular expression, i.e. main body or parenthesized thing
6440 * Caller must absorb opening parenthesis.
6442 * Combining parenthesis handling with the base level of regular expression
6443 * is a trifle forced, but the need to tie the tails of the branches to what
6444 * follows makes it hard to avoid.
6446 #define REGTAIL(x,y,z) regtail((x),(y),(z),depth+1)
6448 #define REGTAIL_STUDY(x,y,z) regtail_study((x),(y),(z),depth+1)
6450 #define REGTAIL_STUDY(x,y,z) regtail((x),(y),(z),depth+1)
6454 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp,U32 depth)
6455 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
6458 register regnode *ret; /* Will be the head of the group. */
6459 register regnode *br;
6460 register regnode *lastbr;
6461 register regnode *ender = NULL;
6462 register I32 parno = 0;
6464 U32 oregflags = RExC_flags;
6465 bool have_branch = 0;
6467 I32 freeze_paren = 0;
6468 I32 after_freeze = 0;
6470 /* for (?g), (?gc), and (?o) warnings; warning
6471 about (?c) will warn about (?g) -- japhy */
6473 #define WASTED_O 0x01
6474 #define WASTED_G 0x02
6475 #define WASTED_C 0x04
6476 #define WASTED_GC (0x02|0x04)
6477 I32 wastedflags = 0x00;
6479 char * parse_start = RExC_parse; /* MJD */
6480 char * const oregcomp_parse = RExC_parse;
6482 GET_RE_DEBUG_FLAGS_DECL;
6484 PERL_ARGS_ASSERT_REG;
6485 DEBUG_PARSE("reg ");
6487 *flagp = 0; /* Tentatively. */
6490 /* Make an OPEN node, if parenthesized. */
6492 if ( *RExC_parse == '*') { /* (*VERB:ARG) */
6493 char *start_verb = RExC_parse;
6494 STRLEN verb_len = 0;
6495 char *start_arg = NULL;
6496 unsigned char op = 0;
6498 int internal_argval = 0; /* internal_argval is only useful if !argok */
6499 while ( *RExC_parse && *RExC_parse != ')' ) {
6500 if ( *RExC_parse == ':' ) {
6501 start_arg = RExC_parse + 1;
6507 verb_len = RExC_parse - start_verb;
6510 while ( *RExC_parse && *RExC_parse != ')' )
6512 if ( *RExC_parse != ')' )
6513 vFAIL("Unterminated verb pattern argument");
6514 if ( RExC_parse == start_arg )
6517 if ( *RExC_parse != ')' )
6518 vFAIL("Unterminated verb pattern");
6521 switch ( *start_verb ) {
6522 case 'A': /* (*ACCEPT) */
6523 if ( memEQs(start_verb,verb_len,"ACCEPT") ) {
6525 internal_argval = RExC_nestroot;
6528 case 'C': /* (*COMMIT) */
6529 if ( memEQs(start_verb,verb_len,"COMMIT") )
6532 case 'F': /* (*FAIL) */
6533 if ( verb_len==1 || memEQs(start_verb,verb_len,"FAIL") ) {
6538 case ':': /* (*:NAME) */
6539 case 'M': /* (*MARK:NAME) */
6540 if ( verb_len==0 || memEQs(start_verb,verb_len,"MARK") ) {
6545 case 'P': /* (*PRUNE) */
6546 if ( memEQs(start_verb,verb_len,"PRUNE") )
6549 case 'S': /* (*SKIP) */
6550 if ( memEQs(start_verb,verb_len,"SKIP") )
6553 case 'T': /* (*THEN) */
6554 /* [19:06] <TimToady> :: is then */
6555 if ( memEQs(start_verb,verb_len,"THEN") ) {
6557 RExC_seen |= REG_SEEN_CUTGROUP;
6563 vFAIL3("Unknown verb pattern '%.*s'",
6564 verb_len, start_verb);
6567 if ( start_arg && internal_argval ) {
6568 vFAIL3("Verb pattern '%.*s' may not have an argument",
6569 verb_len, start_verb);
6570 } else if ( argok < 0 && !start_arg ) {
6571 vFAIL3("Verb pattern '%.*s' has a mandatory argument",
6572 verb_len, start_verb);
6574 ret = reganode(pRExC_state, op, internal_argval);
6575 if ( ! internal_argval && ! SIZE_ONLY ) {
6577 SV *sv = newSVpvn( start_arg, RExC_parse - start_arg);
6578 ARG(ret) = add_data( pRExC_state, 1, "S" );
6579 RExC_rxi->data->data[ARG(ret)]=(void*)sv;
6586 if (!internal_argval)
6587 RExC_seen |= REG_SEEN_VERBARG;
6588 } else if ( start_arg ) {
6589 vFAIL3("Verb pattern '%.*s' may not have an argument",
6590 verb_len, start_verb);
6592 ret = reg_node(pRExC_state, op);
6594 nextchar(pRExC_state);
6597 if (*RExC_parse == '?') { /* (?...) */
6598 bool is_logical = 0;
6599 const char * const seqstart = RExC_parse;
6600 bool has_use_defaults = FALSE;
6603 paren = *RExC_parse++;
6604 ret = NULL; /* For look-ahead/behind. */
6607 case 'P': /* (?P...) variants for those used to PCRE/Python */
6608 paren = *RExC_parse++;
6609 if ( paren == '<') /* (?P<...>) named capture */
6611 else if (paren == '>') { /* (?P>name) named recursion */
6612 goto named_recursion;
6614 else if (paren == '=') { /* (?P=...) named backref */
6615 /* this pretty much dupes the code for \k<NAME> in regatom(), if
6616 you change this make sure you change that */
6617 char* name_start = RExC_parse;
6619 SV *sv_dat = reg_scan_name(pRExC_state,
6620 SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
6621 if (RExC_parse == name_start || *RExC_parse != ')')
6622 vFAIL2("Sequence %.3s... not terminated",parse_start);
6625 num = add_data( pRExC_state, 1, "S" );
6626 RExC_rxi->data->data[num]=(void*)sv_dat;
6627 SvREFCNT_inc_simple_void(sv_dat);
6630 ret = reganode(pRExC_state,
6633 : (MORE_ASCII_RESTRICTED)
6635 : (AT_LEAST_UNI_SEMANTICS)
6643 Set_Node_Offset(ret, parse_start+1);
6644 Set_Node_Cur_Length(ret); /* MJD */
6646 nextchar(pRExC_state);
6650 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
6652 case '<': /* (?<...) */
6653 if (*RExC_parse == '!')
6655 else if (*RExC_parse != '=')
6661 case '\'': /* (?'...') */
6662 name_start= RExC_parse;
6663 svname = reg_scan_name(pRExC_state,
6664 SIZE_ONLY ? /* reverse test from the others */
6665 REG_RSN_RETURN_NAME :
6666 REG_RSN_RETURN_NULL);
6667 if (RExC_parse == name_start) {
6669 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
6672 if (*RExC_parse != paren)
6673 vFAIL2("Sequence (?%c... not terminated",
6674 paren=='>' ? '<' : paren);
6678 if (!svname) /* shouldn't happen */
6680 "panic: reg_scan_name returned NULL");
6681 if (!RExC_paren_names) {
6682 RExC_paren_names= newHV();
6683 sv_2mortal(MUTABLE_SV(RExC_paren_names));
6685 RExC_paren_name_list= newAV();
6686 sv_2mortal(MUTABLE_SV(RExC_paren_name_list));
6689 he_str = hv_fetch_ent( RExC_paren_names, svname, 1, 0 );
6691 sv_dat = HeVAL(he_str);
6693 /* croak baby croak */
6695 "panic: paren_name hash element allocation failed");
6696 } else if ( SvPOK(sv_dat) ) {
6697 /* (?|...) can mean we have dupes so scan to check
6698 its already been stored. Maybe a flag indicating
6699 we are inside such a construct would be useful,
6700 but the arrays are likely to be quite small, so
6701 for now we punt -- dmq */
6702 IV count = SvIV(sv_dat);
6703 I32 *pv = (I32*)SvPVX(sv_dat);
6705 for ( i = 0 ; i < count ; i++ ) {
6706 if ( pv[i] == RExC_npar ) {
6712 pv = (I32*)SvGROW(sv_dat, SvCUR(sv_dat) + sizeof(I32)+1);
6713 SvCUR_set(sv_dat, SvCUR(sv_dat) + sizeof(I32));
6714 pv[count] = RExC_npar;
6715 SvIV_set(sv_dat, SvIVX(sv_dat) + 1);
6718 (void)SvUPGRADE(sv_dat,SVt_PVNV);
6719 sv_setpvn(sv_dat, (char *)&(RExC_npar), sizeof(I32));
6721 SvIV_set(sv_dat, 1);
6724 if (!av_store(RExC_paren_name_list, RExC_npar, SvREFCNT_inc(svname)))
6725 SvREFCNT_dec(svname);
6728 /*sv_dump(sv_dat);*/
6730 nextchar(pRExC_state);
6732 goto capturing_parens;
6734 RExC_seen |= REG_SEEN_LOOKBEHIND;
6735 RExC_in_lookbehind++;
6737 case '=': /* (?=...) */
6738 RExC_seen_zerolen++;
6740 case '!': /* (?!...) */
6741 RExC_seen_zerolen++;
6742 if (*RExC_parse == ')') {
6743 ret=reg_node(pRExC_state, OPFAIL);
6744 nextchar(pRExC_state);
6748 case '|': /* (?|...) */
6749 /* branch reset, behave like a (?:...) except that
6750 buffers in alternations share the same numbers */
6752 after_freeze = freeze_paren = RExC_npar;
6754 case ':': /* (?:...) */
6755 case '>': /* (?>...) */
6757 case '$': /* (?$...) */
6758 case '@': /* (?@...) */
6759 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
6761 case '#': /* (?#...) */
6762 while (*RExC_parse && *RExC_parse != ')')
6764 if (*RExC_parse != ')')
6765 FAIL("Sequence (?#... not terminated");
6766 nextchar(pRExC_state);
6769 case '0' : /* (?0) */
6770 case 'R' : /* (?R) */
6771 if (*RExC_parse != ')')
6772 FAIL("Sequence (?R) not terminated");
6773 ret = reg_node(pRExC_state, GOSTART);
6774 *flagp |= POSTPONED;
6775 nextchar(pRExC_state);
6778 { /* named and numeric backreferences */
6780 case '&': /* (?&NAME) */
6781 parse_start = RExC_parse - 1;
6784 SV *sv_dat = reg_scan_name(pRExC_state,
6785 SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
6786 num = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
6788 goto gen_recurse_regop;
6791 if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
6793 vFAIL("Illegal pattern");
6795 goto parse_recursion;
6797 case '-': /* (?-1) */
6798 if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
6799 RExC_parse--; /* rewind to let it be handled later */
6803 case '1': case '2': case '3': case '4': /* (?1) */
6804 case '5': case '6': case '7': case '8': case '9':
6807 num = atoi(RExC_parse);
6808 parse_start = RExC_parse - 1; /* MJD */
6809 if (*RExC_parse == '-')
6811 while (isDIGIT(*RExC_parse))
6813 if (*RExC_parse!=')')
6814 vFAIL("Expecting close bracket");
6817 if ( paren == '-' ) {
6819 Diagram of capture buffer numbering.
6820 Top line is the normal capture buffer numbers
6821 Bottom line is the negative indexing as from
6825 /(a(x)y)(a(b(c(?-2)d)e)f)(g(h))/
6829 num = RExC_npar + num;
6832 vFAIL("Reference to nonexistent group");
6834 } else if ( paren == '+' ) {
6835 num = RExC_npar + num - 1;
6838 ret = reganode(pRExC_state, GOSUB, num);
6840 if (num > (I32)RExC_rx->nparens) {
6842 vFAIL("Reference to nonexistent group");
6844 ARG2L_SET( ret, RExC_recurse_count++);
6846 DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
6847 "Recurse #%"UVuf" to %"IVdf"\n", (UV)ARG(ret), (IV)ARG2L(ret)));
6851 RExC_seen |= REG_SEEN_RECURSE;
6852 Set_Node_Length(ret, 1 + regarglen[OP(ret)]); /* MJD */
6853 Set_Node_Offset(ret, parse_start); /* MJD */
6855 *flagp |= POSTPONED;
6856 nextchar(pRExC_state);
6858 } /* named and numeric backreferences */
6861 case '?': /* (??...) */
6863 if (*RExC_parse != '{') {
6865 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
6868 *flagp |= POSTPONED;
6869 paren = *RExC_parse++;
6871 case '{': /* (?{...}) */
6876 char *s = RExC_parse;
6878 RExC_seen_zerolen++;
6879 RExC_seen |= REG_SEEN_EVAL;
6880 while (count && (c = *RExC_parse)) {
6891 if (*RExC_parse != ')') {
6893 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
6897 OP_4tree *sop, *rop;
6898 SV * const sv = newSVpvn(s, RExC_parse - 1 - s);
6901 Perl_save_re_context(aTHX);
6902 rop = Perl_sv_compile_2op_is_broken(aTHX_ sv, &sop, "re", &pad);
6903 sop->op_private |= OPpREFCOUNTED;
6904 /* re_dup will OpREFCNT_inc */
6905 OpREFCNT_set(sop, 1);
6908 n = add_data(pRExC_state, 3, "nop");
6909 RExC_rxi->data->data[n] = (void*)rop;
6910 RExC_rxi->data->data[n+1] = (void*)sop;
6911 RExC_rxi->data->data[n+2] = (void*)pad;
6914 else { /* First pass */
6915 if (PL_reginterp_cnt < ++RExC_seen_evals
6917 /* No compiled RE interpolated, has runtime
6918 components ===> unsafe. */
6919 FAIL("Eval-group not allowed at runtime, use re 'eval'");
6920 if (PL_tainting && PL_tainted)
6921 FAIL("Eval-group in insecure regular expression");
6922 #if PERL_VERSION > 8
6923 if (IN_PERL_COMPILETIME)
6928 nextchar(pRExC_state);
6930 ret = reg_node(pRExC_state, LOGICAL);
6933 REGTAIL(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
6934 /* deal with the length of this later - MJD */
6937 ret = reganode(pRExC_state, EVAL, n);
6938 Set_Node_Length(ret, RExC_parse - parse_start + 1);
6939 Set_Node_Offset(ret, parse_start);
6942 case '(': /* (?(?{...})...) and (?(?=...)...) */
6945 if (RExC_parse[0] == '?') { /* (?(?...)) */
6946 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
6947 || RExC_parse[1] == '<'
6948 || RExC_parse[1] == '{') { /* Lookahead or eval. */
6951 ret = reg_node(pRExC_state, LOGICAL);
6954 REGTAIL(pRExC_state, ret, reg(pRExC_state, 1, &flag,depth+1));
6958 else if ( RExC_parse[0] == '<' /* (?(<NAME>)...) */
6959 || RExC_parse[0] == '\'' ) /* (?('NAME')...) */
6961 char ch = RExC_parse[0] == '<' ? '>' : '\'';
6962 char *name_start= RExC_parse++;
6964 SV *sv_dat=reg_scan_name(pRExC_state,
6965 SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
6966 if (RExC_parse == name_start || *RExC_parse != ch)
6967 vFAIL2("Sequence (?(%c... not terminated",
6968 (ch == '>' ? '<' : ch));
6971 num = add_data( pRExC_state, 1, "S" );
6972 RExC_rxi->data->data[num]=(void*)sv_dat;
6973 SvREFCNT_inc_simple_void(sv_dat);
6975 ret = reganode(pRExC_state,NGROUPP,num);
6976 goto insert_if_check_paren;
6978 else if (RExC_parse[0] == 'D' &&
6979 RExC_parse[1] == 'E' &&
6980 RExC_parse[2] == 'F' &&
6981 RExC_parse[3] == 'I' &&
6982 RExC_parse[4] == 'N' &&
6983 RExC_parse[5] == 'E')
6985 ret = reganode(pRExC_state,DEFINEP,0);
6988 goto insert_if_check_paren;
6990 else if (RExC_parse[0] == 'R') {
6993 if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
6994 parno = atoi(RExC_parse++);
6995 while (isDIGIT(*RExC_parse))
6997 } else if (RExC_parse[0] == '&') {
7000 sv_dat = reg_scan_name(pRExC_state,
7001 SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7002 parno = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
7004 ret = reganode(pRExC_state,INSUBP,parno);
7005 goto insert_if_check_paren;
7007 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
7010 parno = atoi(RExC_parse++);
7012 while (isDIGIT(*RExC_parse))
7014 ret = reganode(pRExC_state, GROUPP, parno);
7016 insert_if_check_paren:
7017 if ((c = *nextchar(pRExC_state)) != ')')
7018 vFAIL("Switch condition not recognized");
7020 REGTAIL(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
7021 br = regbranch(pRExC_state, &flags, 1,depth+1);
7023 br = reganode(pRExC_state, LONGJMP, 0);
7025 REGTAIL(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
7026 c = *nextchar(pRExC_state);
7031 vFAIL("(?(DEFINE)....) does not allow branches");
7032 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
7033 regbranch(pRExC_state, &flags, 1,depth+1);
7034 REGTAIL(pRExC_state, ret, lastbr);
7037 c = *nextchar(pRExC_state);
7042 vFAIL("Switch (?(condition)... contains too many branches");
7043 ender = reg_node(pRExC_state, TAIL);
7044 REGTAIL(pRExC_state, br, ender);
7046 REGTAIL(pRExC_state, lastbr, ender);
7047 REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
7050 REGTAIL(pRExC_state, ret, ender);
7051 RExC_size++; /* XXX WHY do we need this?!!
7052 For large programs it seems to be required
7053 but I can't figure out why. -- dmq*/
7057 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
7061 RExC_parse--; /* for vFAIL to print correctly */
7062 vFAIL("Sequence (? incomplete");
7064 case DEFAULT_PAT_MOD: /* Use default flags with the exceptions
7066 has_use_defaults = TRUE;
7067 STD_PMMOD_FLAGS_CLEAR(&RExC_flags);
7068 set_regex_charset(&RExC_flags, (RExC_utf8 || RExC_uni_semantics)
7069 ? REGEX_UNICODE_CHARSET
7070 : REGEX_DEPENDS_CHARSET);
7074 parse_flags: /* (?i) */
7076 U32 posflags = 0, negflags = 0;
7077 U32 *flagsp = &posflags;
7078 char has_charset_modifier = '\0';
7079 regex_charset cs = (RExC_utf8 || RExC_uni_semantics)
7080 ? REGEX_UNICODE_CHARSET
7081 : REGEX_DEPENDS_CHARSET;
7083 while (*RExC_parse) {
7084 /* && strchr("iogcmsx", *RExC_parse) */
7085 /* (?g), (?gc) and (?o) are useless here
7086 and must be globally applied -- japhy */
7087 switch (*RExC_parse) {
7088 CASE_STD_PMMOD_FLAGS_PARSE_SET(flagsp);
7089 case LOCALE_PAT_MOD:
7090 if (has_charset_modifier) {
7091 goto excess_modifier;
7093 else if (flagsp == &negflags) {
7096 cs = REGEX_LOCALE_CHARSET;
7097 has_charset_modifier = LOCALE_PAT_MOD;
7098 RExC_contains_locale = 1;
7100 case UNICODE_PAT_MOD:
7101 if (has_charset_modifier) {
7102 goto excess_modifier;
7104 else if (flagsp == &negflags) {
7107 cs = REGEX_UNICODE_CHARSET;
7108 has_charset_modifier = UNICODE_PAT_MOD;
7110 case ASCII_RESTRICT_PAT_MOD:
7111 if (flagsp == &negflags) {
7114 if (has_charset_modifier) {
7115 if (cs != REGEX_ASCII_RESTRICTED_CHARSET) {
7116 goto excess_modifier;
7118 /* Doubled modifier implies more restricted */
7119 cs = REGEX_ASCII_MORE_RESTRICTED_CHARSET;
7122 cs = REGEX_ASCII_RESTRICTED_CHARSET;
7124 has_charset_modifier = ASCII_RESTRICT_PAT_MOD;
7126 case DEPENDS_PAT_MOD:
7127 if (has_use_defaults) {
7128 goto fail_modifiers;
7130 else if (flagsp == &negflags) {
7133 else if (has_charset_modifier) {
7134 goto excess_modifier;
7137 /* The dual charset means unicode semantics if the
7138 * pattern (or target, not known until runtime) are
7139 * utf8, or something in the pattern indicates unicode
7141 cs = (RExC_utf8 || RExC_uni_semantics)
7142 ? REGEX_UNICODE_CHARSET
7143 : REGEX_DEPENDS_CHARSET;
7144 has_charset_modifier = DEPENDS_PAT_MOD;
7148 if (has_charset_modifier == ASCII_RESTRICT_PAT_MOD) {
7149 vFAIL2("Regexp modifier \"%c\" may appear a maximum of twice", ASCII_RESTRICT_PAT_MOD);
7151 else if (has_charset_modifier == *(RExC_parse - 1)) {
7152 vFAIL2("Regexp modifier \"%c\" may not appear twice", *(RExC_parse - 1));
7155 vFAIL3("Regexp modifiers \"%c\" and \"%c\" are mutually exclusive", has_charset_modifier, *(RExC_parse - 1));
7160 vFAIL2("Regexp modifier \"%c\" may not appear after the \"-\"", *(RExC_parse - 1));
7162 case ONCE_PAT_MOD: /* 'o' */
7163 case GLOBAL_PAT_MOD: /* 'g' */
7164 if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
7165 const I32 wflagbit = *RExC_parse == 'o' ? WASTED_O : WASTED_G;
7166 if (! (wastedflags & wflagbit) ) {
7167 wastedflags |= wflagbit;
7170 "Useless (%s%c) - %suse /%c modifier",
7171 flagsp == &negflags ? "?-" : "?",
7173 flagsp == &negflags ? "don't " : "",
7180 case CONTINUE_PAT_MOD: /* 'c' */
7181 if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
7182 if (! (wastedflags & WASTED_C) ) {
7183 wastedflags |= WASTED_GC;
7186 "Useless (%sc) - %suse /gc modifier",
7187 flagsp == &negflags ? "?-" : "?",
7188 flagsp == &negflags ? "don't " : ""
7193 case KEEPCOPY_PAT_MOD: /* 'p' */
7194 if (flagsp == &negflags) {
7196 ckWARNreg(RExC_parse + 1,"Useless use of (?-p)");
7198 *flagsp |= RXf_PMf_KEEPCOPY;
7202 /* A flag is a default iff it is following a minus, so
7203 * if there is a minus, it means will be trying to
7204 * re-specify a default which is an error */
7205 if (has_use_defaults || flagsp == &negflags) {
7208 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7212 wastedflags = 0; /* reset so (?g-c) warns twice */
7218 RExC_flags |= posflags;
7219 RExC_flags &= ~negflags;
7220 set_regex_charset(&RExC_flags, cs);
7222 oregflags |= posflags;
7223 oregflags &= ~negflags;
7224 set_regex_charset(&oregflags, cs);
7226 nextchar(pRExC_state);
7237 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7242 }} /* one for the default block, one for the switch */
7249 ret = reganode(pRExC_state, OPEN, parno);
7252 RExC_nestroot = parno;
7253 if (RExC_seen & REG_SEEN_RECURSE
7254 && !RExC_open_parens[parno-1])
7256 DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
7257 "Setting open paren #%"IVdf" to %d\n",
7258 (IV)parno, REG_NODE_NUM(ret)));
7259 RExC_open_parens[parno-1]= ret;
7262 Set_Node_Length(ret, 1); /* MJD */
7263 Set_Node_Offset(ret, RExC_parse); /* MJD */
7271 /* Pick up the branches, linking them together. */
7272 parse_start = RExC_parse; /* MJD */
7273 br = regbranch(pRExC_state, &flags, 1,depth+1);
7275 /* branch_len = (paren != 0); */
7279 if (*RExC_parse == '|') {
7280 if (!SIZE_ONLY && RExC_extralen) {
7281 reginsert(pRExC_state, BRANCHJ, br, depth+1);
7284 reginsert(pRExC_state, BRANCH, br, depth+1);
7285 Set_Node_Length(br, paren != 0);
7286 Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start);
7290 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
7292 else if (paren == ':') {
7293 *flagp |= flags&SIMPLE;
7295 if (is_open) { /* Starts with OPEN. */
7296 REGTAIL(pRExC_state, ret, br); /* OPEN -> first. */
7298 else if (paren != '?') /* Not Conditional */
7300 *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
7302 while (*RExC_parse == '|') {
7303 if (!SIZE_ONLY && RExC_extralen) {
7304 ender = reganode(pRExC_state, LONGJMP,0);
7305 REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
7308 RExC_extralen += 2; /* Account for LONGJMP. */
7309 nextchar(pRExC_state);
7311 if (RExC_npar > after_freeze)
7312 after_freeze = RExC_npar;
7313 RExC_npar = freeze_paren;
7315 br = regbranch(pRExC_state, &flags, 0, depth+1);
7319 REGTAIL(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
7321 *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
7324 if (have_branch || paren != ':') {
7325 /* Make a closing node, and hook it on the end. */
7328 ender = reg_node(pRExC_state, TAIL);
7331 ender = reganode(pRExC_state, CLOSE, parno);
7332 if (!SIZE_ONLY && RExC_seen & REG_SEEN_RECURSE) {
7333 DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
7334 "Setting close paren #%"IVdf" to %d\n",
7335 (IV)parno, REG_NODE_NUM(ender)));
7336 RExC_close_parens[parno-1]= ender;
7337 if (RExC_nestroot == parno)
7340 Set_Node_Offset(ender,RExC_parse+1); /* MJD */
7341 Set_Node_Length(ender,1); /* MJD */
7347 *flagp &= ~HASWIDTH;
7350 ender = reg_node(pRExC_state, SUCCEED);
7353 ender = reg_node(pRExC_state, END);
7355 assert(!RExC_opend); /* there can only be one! */
7360 REGTAIL(pRExC_state, lastbr, ender);
7362 if (have_branch && !SIZE_ONLY) {
7364 RExC_seen |= REG_TOP_LEVEL_BRANCHES;
7366 /* Hook the tails of the branches to the closing node. */
7367 for (br = ret; br; br = regnext(br)) {
7368 const U8 op = PL_regkind[OP(br)];
7370 REGTAIL_STUDY(pRExC_state, NEXTOPER(br), ender);
7372 else if (op == BRANCHJ) {
7373 REGTAIL_STUDY(pRExC_state, NEXTOPER(NEXTOPER(br)), ender);
7381 static const char parens[] = "=!<,>";
7383 if (paren && (p = strchr(parens, paren))) {
7384 U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
7385 int flag = (p - parens) > 1;
7388 node = SUSPEND, flag = 0;
7389 reginsert(pRExC_state, node,ret, depth+1);
7390 Set_Node_Cur_Length(ret);
7391 Set_Node_Offset(ret, parse_start + 1);
7393 REGTAIL_STUDY(pRExC_state, ret, reg_node(pRExC_state, TAIL));
7397 /* Check for proper termination. */
7399 RExC_flags = oregflags;
7400 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
7401 RExC_parse = oregcomp_parse;
7402 vFAIL("Unmatched (");
7405 else if (!paren && RExC_parse < RExC_end) {
7406 if (*RExC_parse == ')') {
7408 vFAIL("Unmatched )");
7411 FAIL("Junk on end of regexp"); /* "Can't happen". */
7415 if (RExC_in_lookbehind) {
7416 RExC_in_lookbehind--;
7418 if (after_freeze > RExC_npar)
7419 RExC_npar = after_freeze;
7424 - regbranch - one alternative of an | operator
7426 * Implements the concatenation operator.
7429 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first, U32 depth)
7432 register regnode *ret;
7433 register regnode *chain = NULL;
7434 register regnode *latest;
7435 I32 flags = 0, c = 0;
7436 GET_RE_DEBUG_FLAGS_DECL;
7438 PERL_ARGS_ASSERT_REGBRANCH;
7440 DEBUG_PARSE("brnc");
7445 if (!SIZE_ONLY && RExC_extralen)
7446 ret = reganode(pRExC_state, BRANCHJ,0);
7448 ret = reg_node(pRExC_state, BRANCH);
7449 Set_Node_Length(ret, 1);
7453 if (!first && SIZE_ONLY)
7454 RExC_extralen += 1; /* BRANCHJ */
7456 *flagp = WORST; /* Tentatively. */
7459 nextchar(pRExC_state);
7460 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
7462 latest = regpiece(pRExC_state, &flags,depth+1);
7463 if (latest == NULL) {
7464 if (flags & TRYAGAIN)
7468 else if (ret == NULL)
7470 *flagp |= flags&(HASWIDTH|POSTPONED);
7471 if (chain == NULL) /* First piece. */
7472 *flagp |= flags&SPSTART;
7475 REGTAIL(pRExC_state, chain, latest);
7480 if (chain == NULL) { /* Loop ran zero times. */
7481 chain = reg_node(pRExC_state, NOTHING);
7486 *flagp |= flags&SIMPLE;
7493 - regpiece - something followed by possible [*+?]
7495 * Note that the branching code sequences used for ? and the general cases
7496 * of * and + are somewhat optimized: they use the same NOTHING node as
7497 * both the endmarker for their branch list and the body of the last branch.
7498 * It might seem that this node could be dispensed with entirely, but the
7499 * endmarker role is not redundant.
7502 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
7505 register regnode *ret;
7507 register char *next;
7509 const char * const origparse = RExC_parse;
7511 I32 max = REG_INFTY;
7513 const char *maxpos = NULL;
7514 GET_RE_DEBUG_FLAGS_DECL;
7516 PERL_ARGS_ASSERT_REGPIECE;
7518 DEBUG_PARSE("piec");
7520 ret = regatom(pRExC_state, &flags,depth+1);
7522 if (flags & TRYAGAIN)
7529 if (op == '{' && regcurly(RExC_parse)) {
7531 parse_start = RExC_parse; /* MJD */
7532 next = RExC_parse + 1;
7533 while (isDIGIT(*next) || *next == ',') {
7542 if (*next == '}') { /* got one */
7546 min = atoi(RExC_parse);
7550 maxpos = RExC_parse;
7552 if (!max && *maxpos != '0')
7553 max = REG_INFTY; /* meaning "infinity" */
7554 else if (max >= REG_INFTY)
7555 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
7557 nextchar(pRExC_state);
7560 if ((flags&SIMPLE)) {
7561 RExC_naughty += 2 + RExC_naughty / 2;
7562 reginsert(pRExC_state, CURLY, ret, depth+1);
7563 Set_Node_Offset(ret, parse_start+1); /* MJD */
7564 Set_Node_Cur_Length(ret);
7567 regnode * const w = reg_node(pRExC_state, WHILEM);
7570 REGTAIL(pRExC_state, ret, w);
7571 if (!SIZE_ONLY && RExC_extralen) {
7572 reginsert(pRExC_state, LONGJMP,ret, depth+1);
7573 reginsert(pRExC_state, NOTHING,ret, depth+1);
7574 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
7576 reginsert(pRExC_state, CURLYX,ret, depth+1);
7578 Set_Node_Offset(ret, parse_start+1);
7579 Set_Node_Length(ret,
7580 op == '{' ? (RExC_parse - parse_start) : 1);
7582 if (!SIZE_ONLY && RExC_extralen)
7583 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
7584 REGTAIL(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
7586 RExC_whilem_seen++, RExC_extralen += 3;
7587 RExC_naughty += 4 + RExC_naughty; /* compound interest */
7596 vFAIL("Can't do {n,m} with n > m");
7598 ARG1_SET(ret, (U16)min);
7599 ARG2_SET(ret, (U16)max);
7611 #if 0 /* Now runtime fix should be reliable. */
7613 /* if this is reinstated, don't forget to put this back into perldiag:
7615 =item Regexp *+ operand could be empty at {#} in regex m/%s/
7617 (F) The part of the regexp subject to either the * or + quantifier
7618 could match an empty string. The {#} shows in the regular
7619 expression about where the problem was discovered.
7623 if (!(flags&HASWIDTH) && op != '?')
7624 vFAIL("Regexp *+ operand could be empty");
7627 parse_start = RExC_parse;
7628 nextchar(pRExC_state);
7630 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
7632 if (op == '*' && (flags&SIMPLE)) {
7633 reginsert(pRExC_state, STAR, ret, depth+1);
7637 else if (op == '*') {
7641 else if (op == '+' && (flags&SIMPLE)) {
7642 reginsert(pRExC_state, PLUS, ret, depth+1);
7646 else if (op == '+') {
7650 else if (op == '?') {
7655 if (!SIZE_ONLY && !(flags&(HASWIDTH|POSTPONED)) && max > REG_INFTY/3) {
7656 ckWARN3reg(RExC_parse,
7657 "%.*s matches null string many times",
7658 (int)(RExC_parse >= origparse ? RExC_parse - origparse : 0),
7662 if (RExC_parse < RExC_end && *RExC_parse == '?') {
7663 nextchar(pRExC_state);
7664 reginsert(pRExC_state, MINMOD, ret, depth+1);
7665 REGTAIL(pRExC_state, ret, ret + NODE_STEP_REGNODE);
7667 #ifndef REG_ALLOW_MINMOD_SUSPEND
7670 if (RExC_parse < RExC_end && *RExC_parse == '+') {
7672 nextchar(pRExC_state);
7673 ender = reg_node(pRExC_state, SUCCEED);
7674 REGTAIL(pRExC_state, ret, ender);
7675 reginsert(pRExC_state, SUSPEND, ret, depth+1);
7677 ender = reg_node(pRExC_state, TAIL);
7678 REGTAIL(pRExC_state, ret, ender);
7682 if (RExC_parse < RExC_end && ISMULT2(RExC_parse)) {
7684 vFAIL("Nested quantifiers");
7691 /* reg_namedseq(pRExC_state,UVp, UV depth)
7693 This is expected to be called by a parser routine that has
7694 recognized '\N' and needs to handle the rest. RExC_parse is
7695 expected to point at the first char following the N at the time
7698 The \N may be inside (indicated by valuep not being NULL) or outside a
7701 \N may begin either a named sequence, or if outside a character class, mean
7702 to match a non-newline. For non single-quoted regexes, the tokenizer has
7703 attempted to decide which, and in the case of a named sequence converted it
7704 into one of the forms: \N{} (if the sequence is null), or \N{U+c1.c2...},
7705 where c1... are the characters in the sequence. For single-quoted regexes,
7706 the tokenizer passes the \N sequence through unchanged; this code will not
7707 attempt to determine this nor expand those. The net effect is that if the
7708 beginning of the passed-in pattern isn't '{U+' or there is no '}', it
7709 signals that this \N occurrence means to match a non-newline.
7711 Only the \N{U+...} form should occur in a character class, for the same
7712 reason that '.' inside a character class means to just match a period: it
7713 just doesn't make sense.
7715 If valuep is non-null then it is assumed that we are parsing inside
7716 of a charclass definition and the first codepoint in the resolved
7717 string is returned via *valuep and the routine will return NULL.
7718 In this mode if a multichar string is returned from the charnames
7719 handler, a warning will be issued, and only the first char in the
7720 sequence will be examined. If the string returned is zero length
7721 then the value of *valuep is undefined and NON-NULL will
7722 be returned to indicate failure. (This will NOT be a valid pointer
7725 If valuep is null then it is assumed that we are parsing normal text and a
7726 new EXACT node is inserted into the program containing the resolved string,
7727 and a pointer to the new node is returned. But if the string is zero length
7728 a NOTHING node is emitted instead.
7730 On success RExC_parse is set to the char following the endbrace.
7731 Parsing failures will generate a fatal error via vFAIL(...)
7734 S_reg_namedseq(pTHX_ RExC_state_t *pRExC_state, UV *valuep, I32 *flagp, U32 depth)
7736 char * endbrace; /* '}' following the name */
7737 regnode *ret = NULL;
7740 GET_RE_DEBUG_FLAGS_DECL;
7742 PERL_ARGS_ASSERT_REG_NAMEDSEQ;
7746 /* The [^\n] meaning of \N ignores spaces and comments under the /x
7747 * modifier. The other meaning does not */
7748 p = (RExC_flags & RXf_PMf_EXTENDED)
7749 ? regwhite( pRExC_state, RExC_parse )
7752 /* Disambiguate between \N meaning a named character versus \N meaning
7753 * [^\n]. The former is assumed when it can't be the latter. */
7754 if (*p != '{' || regcurly(p)) {
7757 /* no bare \N in a charclass */
7758 vFAIL("\\N in a character class must be a named character: \\N{...}");
7760 nextchar(pRExC_state);
7761 ret = reg_node(pRExC_state, REG_ANY);
7762 *flagp |= HASWIDTH|SIMPLE;
7765 Set_Node_Length(ret, 1); /* MJD */
7769 /* Here, we have decided it should be a named sequence */
7771 /* The test above made sure that the next real character is a '{', but
7772 * under the /x modifier, it could be separated by space (or a comment and
7773 * \n) and this is not allowed (for consistency with \x{...} and the
7774 * tokenizer handling of \N{NAME}). */
7775 if (*RExC_parse != '{') {
7776 vFAIL("Missing braces on \\N{}");
7779 RExC_parse++; /* Skip past the '{' */
7781 if (! (endbrace = strchr(RExC_parse, '}')) /* no trailing brace */
7782 || ! (endbrace == RExC_parse /* nothing between the {} */
7783 || (endbrace - RExC_parse >= 2 /* U+ (bad hex is checked below */
7784 && strnEQ(RExC_parse, "U+", 2)))) /* for a better error msg) */
7786 if (endbrace) RExC_parse = endbrace; /* position msg's '<--HERE' */
7787 vFAIL("\\N{NAME} must be resolved by the lexer");
7790 if (endbrace == RExC_parse) { /* empty: \N{} */
7792 RExC_parse = endbrace + 1;
7793 return reg_node(pRExC_state,NOTHING);
7797 ckWARNreg(RExC_parse,
7798 "Ignoring zero length \\N{} in character class"
7800 RExC_parse = endbrace + 1;
7803 return (regnode *) &RExC_parse; /* Invalid regnode pointer */
7806 REQUIRE_UTF8; /* named sequences imply Unicode semantics */
7807 RExC_parse += 2; /* Skip past the 'U+' */
7809 if (valuep) { /* In a bracketed char class */
7810 /* We only pay attention to the first char of
7811 multichar strings being returned. I kinda wonder
7812 if this makes sense as it does change the behaviour
7813 from earlier versions, OTOH that behaviour was broken
7814 as well. XXX Solution is to recharacterize as
7815 [rest-of-class]|multi1|multi2... */
7817 STRLEN length_of_hex;
7818 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
7819 | PERL_SCAN_DISALLOW_PREFIX
7820 | (SIZE_ONLY ? PERL_SCAN_SILENT_ILLDIGIT : 0);
7822 char * endchar = RExC_parse + strcspn(RExC_parse, ".}");
7823 if (endchar < endbrace) {
7824 ckWARNreg(endchar, "Using just the first character returned by \\N{} in character class");
7827 length_of_hex = (STRLEN)(endchar - RExC_parse);
7828 *valuep = grok_hex(RExC_parse, &length_of_hex, &flags, NULL);
7830 /* The tokenizer should have guaranteed validity, but it's possible to
7831 * bypass it by using single quoting, so check */
7832 if (length_of_hex == 0
7833 || length_of_hex != (STRLEN)(endchar - RExC_parse) )
7835 RExC_parse += length_of_hex; /* Includes all the valid */
7836 RExC_parse += (RExC_orig_utf8) /* point to after 1st invalid */
7837 ? UTF8SKIP(RExC_parse)
7839 /* Guard against malformed utf8 */
7840 if (RExC_parse >= endchar) RExC_parse = endchar;
7841 vFAIL("Invalid hexadecimal number in \\N{U+...}");
7844 RExC_parse = endbrace + 1;
7845 if (endchar == endbrace) return NULL;
7847 ret = (regnode *) &RExC_parse; /* Invalid regnode pointer */
7849 else { /* Not a char class */
7851 /* What is done here is to convert this to a sub-pattern of the form
7852 * (?:\x{char1}\x{char2}...)
7853 * and then call reg recursively. That way, it retains its atomicness,
7854 * while not having to worry about special handling that some code
7855 * points may have. toke.c has converted the original Unicode values
7856 * to native, so that we can just pass on the hex values unchanged. We
7857 * do have to set a flag to keep recoding from happening in the
7860 SV * substitute_parse = newSVpvn_flags("?:", 2, SVf_UTF8|SVs_TEMP);
7862 char *endchar; /* Points to '.' or '}' ending cur char in the input
7864 char *orig_end = RExC_end;
7866 while (RExC_parse < endbrace) {
7868 /* Code points are separated by dots. If none, there is only one
7869 * code point, and is terminated by the brace */
7870 endchar = RExC_parse + strcspn(RExC_parse, ".}");
7872 /* Convert to notation the rest of the code understands */
7873 sv_catpv(substitute_parse, "\\x{");
7874 sv_catpvn(substitute_parse, RExC_parse, endchar - RExC_parse);
7875 sv_catpv(substitute_parse, "}");
7877 /* Point to the beginning of the next character in the sequence. */
7878 RExC_parse = endchar + 1;
7880 sv_catpv(substitute_parse, ")");
7882 RExC_parse = SvPV(substitute_parse, len);
7884 /* Don't allow empty number */
7886 vFAIL("Invalid hexadecimal number in \\N{U+...}");
7888 RExC_end = RExC_parse + len;
7890 /* The values are Unicode, and therefore not subject to recoding */
7891 RExC_override_recoding = 1;
7893 ret = reg(pRExC_state, 1, flagp, depth+1);
7895 RExC_parse = endbrace;
7896 RExC_end = orig_end;
7897 RExC_override_recoding = 0;
7899 nextchar(pRExC_state);
7909 * It returns the code point in utf8 for the value in *encp.
7910 * value: a code value in the source encoding
7911 * encp: a pointer to an Encode object
7913 * If the result from Encode is not a single character,
7914 * it returns U+FFFD (Replacement character) and sets *encp to NULL.
7917 S_reg_recode(pTHX_ const char value, SV **encp)
7920 SV * const sv = newSVpvn_flags(&value, numlen, SVs_TEMP);
7921 const char * const s = *encp ? sv_recode_to_utf8(sv, *encp) : SvPVX(sv);
7922 const STRLEN newlen = SvCUR(sv);
7923 UV uv = UNICODE_REPLACEMENT;
7925 PERL_ARGS_ASSERT_REG_RECODE;
7929 ? utf8n_to_uvchr((U8*)s, newlen, &numlen, UTF8_ALLOW_DEFAULT)
7932 if (!newlen || numlen != newlen) {
7933 uv = UNICODE_REPLACEMENT;
7941 - regatom - the lowest level
7943 Try to identify anything special at the start of the pattern. If there
7944 is, then handle it as required. This may involve generating a single regop,
7945 such as for an assertion; or it may involve recursing, such as to
7946 handle a () structure.
7948 If the string doesn't start with something special then we gobble up
7949 as much literal text as we can.
7951 Once we have been able to handle whatever type of thing started the
7952 sequence, we return.
7954 Note: we have to be careful with escapes, as they can be both literal
7955 and special, and in the case of \10 and friends can either, depending
7956 on context. Specifically there are two separate switches for handling
7957 escape sequences, with the one for handling literal escapes requiring
7958 a dummy entry for all of the special escapes that are actually handled
7963 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
7966 register regnode *ret = NULL;
7968 char *parse_start = RExC_parse;
7970 GET_RE_DEBUG_FLAGS_DECL;
7971 DEBUG_PARSE("atom");
7972 *flagp = WORST; /* Tentatively. */
7974 PERL_ARGS_ASSERT_REGATOM;
7977 switch ((U8)*RExC_parse) {
7979 RExC_seen_zerolen++;
7980 nextchar(pRExC_state);
7981 if (RExC_flags & RXf_PMf_MULTILINE)
7982 ret = reg_node(pRExC_state, MBOL);
7983 else if (RExC_flags & RXf_PMf_SINGLELINE)
7984 ret = reg_node(pRExC_state, SBOL);
7986 ret = reg_node(pRExC_state, BOL);
7987 Set_Node_Length(ret, 1); /* MJD */
7990 nextchar(pRExC_state);
7992 RExC_seen_zerolen++;
7993 if (RExC_flags & RXf_PMf_MULTILINE)
7994 ret = reg_node(pRExC_state, MEOL);
7995 else if (RExC_flags & RXf_PMf_SINGLELINE)
7996 ret = reg_node(pRExC_state, SEOL);
7998 ret = reg_node(pRExC_state, EOL);
7999 Set_Node_Length(ret, 1); /* MJD */
8002 nextchar(pRExC_state);
8003 if (RExC_flags & RXf_PMf_SINGLELINE)
8004 ret = reg_node(pRExC_state, SANY);
8006 ret = reg_node(pRExC_state, REG_ANY);
8007 *flagp |= HASWIDTH|SIMPLE;
8009 Set_Node_Length(ret, 1); /* MJD */
8013 char * const oregcomp_parse = ++RExC_parse;
8014 ret = regclass(pRExC_state,depth+1);
8015 if (*RExC_parse != ']') {
8016 RExC_parse = oregcomp_parse;
8017 vFAIL("Unmatched [");
8019 nextchar(pRExC_state);
8020 *flagp |= HASWIDTH|SIMPLE;
8021 Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */
8025 nextchar(pRExC_state);
8026 ret = reg(pRExC_state, 1, &flags,depth+1);
8028 if (flags & TRYAGAIN) {
8029 if (RExC_parse == RExC_end) {
8030 /* Make parent create an empty node if needed. */
8038 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE|POSTPONED);
8042 if (flags & TRYAGAIN) {
8046 vFAIL("Internal urp");
8047 /* Supposed to be caught earlier. */
8050 if (!regcurly(RExC_parse)) {
8059 vFAIL("Quantifier follows nothing");
8064 This switch handles escape sequences that resolve to some kind
8065 of special regop and not to literal text. Escape sequnces that
8066 resolve to literal text are handled below in the switch marked
8069 Every entry in this switch *must* have a corresponding entry
8070 in the literal escape switch. However, the opposite is not
8071 required, as the default for this switch is to jump to the
8072 literal text handling code.
8074 switch ((U8)*++RExC_parse) {
8075 /* Special Escapes */
8077 RExC_seen_zerolen++;
8078 ret = reg_node(pRExC_state, SBOL);
8080 goto finish_meta_pat;
8082 ret = reg_node(pRExC_state, GPOS);
8083 RExC_seen |= REG_SEEN_GPOS;
8085 goto finish_meta_pat;
8087 RExC_seen_zerolen++;
8088 ret = reg_node(pRExC_state, KEEPS);
8090 /* XXX:dmq : disabling in-place substitution seems to
8091 * be necessary here to avoid cases of memory corruption, as
8092 * with: C<$_="x" x 80; s/x\K/y/> -- rgs
8094 RExC_seen |= REG_SEEN_LOOKBEHIND;
8095 goto finish_meta_pat;
8097 ret = reg_node(pRExC_state, SEOL);
8099 RExC_seen_zerolen++; /* Do not optimize RE away */
8100 goto finish_meta_pat;
8102 ret = reg_node(pRExC_state, EOS);
8104 RExC_seen_zerolen++; /* Do not optimize RE away */
8105 goto finish_meta_pat;
8107 ret = reg_node(pRExC_state, CANY);
8108 RExC_seen |= REG_SEEN_CANY;
8109 *flagp |= HASWIDTH|SIMPLE;
8110 goto finish_meta_pat;
8112 ret = reg_node(pRExC_state, CLUMP);
8114 goto finish_meta_pat;
8116 switch (get_regex_charset(RExC_flags)) {
8117 case REGEX_LOCALE_CHARSET:
8120 case REGEX_UNICODE_CHARSET:
8123 case REGEX_ASCII_RESTRICTED_CHARSET:
8124 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8127 case REGEX_DEPENDS_CHARSET:
8133 ret = reg_node(pRExC_state, op);
8134 *flagp |= HASWIDTH|SIMPLE;
8135 goto finish_meta_pat;
8137 switch (get_regex_charset(RExC_flags)) {
8138 case REGEX_LOCALE_CHARSET:
8141 case REGEX_UNICODE_CHARSET:
8144 case REGEX_ASCII_RESTRICTED_CHARSET:
8145 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8148 case REGEX_DEPENDS_CHARSET:
8154 ret = reg_node(pRExC_state, op);
8155 *flagp |= HASWIDTH|SIMPLE;
8156 goto finish_meta_pat;
8158 RExC_seen_zerolen++;
8159 RExC_seen |= REG_SEEN_LOOKBEHIND;
8160 switch (get_regex_charset(RExC_flags)) {
8161 case REGEX_LOCALE_CHARSET:
8164 case REGEX_UNICODE_CHARSET:
8167 case REGEX_ASCII_RESTRICTED_CHARSET:
8168 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8171 case REGEX_DEPENDS_CHARSET:
8177 ret = reg_node(pRExC_state, op);
8178 FLAGS(ret) = get_regex_charset(RExC_flags);
8180 if (! SIZE_ONLY && (U8) *(RExC_parse + 1) == '{') {
8181 ckWARNregdep(RExC_parse, "\"\\b{\" is deprecated; use \"\\b\\{\" instead");
8183 goto finish_meta_pat;
8185 RExC_seen_zerolen++;
8186 RExC_seen |= REG_SEEN_LOOKBEHIND;
8187 switch (get_regex_charset(RExC_flags)) {
8188 case REGEX_LOCALE_CHARSET:
8191 case REGEX_UNICODE_CHARSET:
8194 case REGEX_ASCII_RESTRICTED_CHARSET:
8195 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8198 case REGEX_DEPENDS_CHARSET:
8204 ret = reg_node(pRExC_state, op);
8205 FLAGS(ret) = get_regex_charset(RExC_flags);
8207 if (! SIZE_ONLY && (U8) *(RExC_parse + 1) == '{') {
8208 ckWARNregdep(RExC_parse, "\"\\B{\" is deprecated; use \"\\B\\{\" instead");
8210 goto finish_meta_pat;
8212 switch (get_regex_charset(RExC_flags)) {
8213 case REGEX_LOCALE_CHARSET:
8216 case REGEX_UNICODE_CHARSET:
8219 case REGEX_ASCII_RESTRICTED_CHARSET:
8220 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8223 case REGEX_DEPENDS_CHARSET:
8229 ret = reg_node(pRExC_state, op);
8230 *flagp |= HASWIDTH|SIMPLE;
8231 goto finish_meta_pat;
8233 switch (get_regex_charset(RExC_flags)) {
8234 case REGEX_LOCALE_CHARSET:
8237 case REGEX_UNICODE_CHARSET:
8240 case REGEX_ASCII_RESTRICTED_CHARSET:
8241 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8244 case REGEX_DEPENDS_CHARSET:
8250 ret = reg_node(pRExC_state, op);
8251 *flagp |= HASWIDTH|SIMPLE;
8252 goto finish_meta_pat;
8254 switch (get_regex_charset(RExC_flags)) {
8255 case REGEX_LOCALE_CHARSET:
8258 case REGEX_ASCII_RESTRICTED_CHARSET:
8259 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8262 case REGEX_DEPENDS_CHARSET: /* No difference between these */
8263 case REGEX_UNICODE_CHARSET:
8269 ret = reg_node(pRExC_state, op);
8270 *flagp |= HASWIDTH|SIMPLE;
8271 goto finish_meta_pat;
8273 switch (get_regex_charset(RExC_flags)) {
8274 case REGEX_LOCALE_CHARSET:
8277 case REGEX_ASCII_RESTRICTED_CHARSET:
8278 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
8281 case REGEX_DEPENDS_CHARSET: /* No difference between these */
8282 case REGEX_UNICODE_CHARSET:
8288 ret = reg_node(pRExC_state, op);
8289 *flagp |= HASWIDTH|SIMPLE;
8290 goto finish_meta_pat;
8292 ret = reg_node(pRExC_state, LNBREAK);
8293 *flagp |= HASWIDTH|SIMPLE;
8294 goto finish_meta_pat;
8296 ret = reg_node(pRExC_state, HORIZWS);
8297 *flagp |= HASWIDTH|SIMPLE;
8298 goto finish_meta_pat;
8300 ret = reg_node(pRExC_state, NHORIZWS);
8301 *flagp |= HASWIDTH|SIMPLE;
8302 goto finish_meta_pat;
8304 ret = reg_node(pRExC_state, VERTWS);
8305 *flagp |= HASWIDTH|SIMPLE;
8306 goto finish_meta_pat;
8308 ret = reg_node(pRExC_state, NVERTWS);
8309 *flagp |= HASWIDTH|SIMPLE;
8311 nextchar(pRExC_state);
8312 Set_Node_Length(ret, 2); /* MJD */
8317 char* const oldregxend = RExC_end;
8319 char* parse_start = RExC_parse - 2;
8322 if (RExC_parse[1] == '{') {
8323 /* a lovely hack--pretend we saw [\pX] instead */
8324 RExC_end = strchr(RExC_parse, '}');
8326 const U8 c = (U8)*RExC_parse;
8328 RExC_end = oldregxend;
8329 vFAIL2("Missing right brace on \\%c{}", c);
8334 RExC_end = RExC_parse + 2;
8335 if (RExC_end > oldregxend)
8336 RExC_end = oldregxend;
8340 ret = regclass(pRExC_state,depth+1);
8342 RExC_end = oldregxend;
8345 Set_Node_Offset(ret, parse_start + 2);
8346 Set_Node_Cur_Length(ret);
8347 nextchar(pRExC_state);
8348 *flagp |= HASWIDTH|SIMPLE;
8352 /* Handle \N and \N{NAME} here and not below because it can be
8353 multicharacter. join_exact() will join them up later on.
8354 Also this makes sure that things like /\N{BLAH}+/ and
8355 \N{BLAH} being multi char Just Happen. dmq*/
8357 ret= reg_namedseq(pRExC_state, NULL, flagp, depth);
8359 case 'k': /* Handle \k<NAME> and \k'NAME' */
8362 char ch= RExC_parse[1];
8363 if (ch != '<' && ch != '\'' && ch != '{') {
8365 vFAIL2("Sequence %.2s... not terminated",parse_start);
8367 /* this pretty much dupes the code for (?P=...) in reg(), if
8368 you change this make sure you change that */
8369 char* name_start = (RExC_parse += 2);
8371 SV *sv_dat = reg_scan_name(pRExC_state,
8372 SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
8373 ch= (ch == '<') ? '>' : (ch == '{') ? '}' : '\'';
8374 if (RExC_parse == name_start || *RExC_parse != ch)
8375 vFAIL2("Sequence %.3s... not terminated",parse_start);
8378 num = add_data( pRExC_state, 1, "S" );
8379 RExC_rxi->data->data[num]=(void*)sv_dat;
8380 SvREFCNT_inc_simple_void(sv_dat);
8384 ret = reganode(pRExC_state,
8387 : (MORE_ASCII_RESTRICTED)
8389 : (AT_LEAST_UNI_SEMANTICS)
8397 /* override incorrect value set in reganode MJD */
8398 Set_Node_Offset(ret, parse_start+1);
8399 Set_Node_Cur_Length(ret); /* MJD */
8400 nextchar(pRExC_state);
8406 case '1': case '2': case '3': case '4':
8407 case '5': case '6': case '7': case '8': case '9':
8410 bool isg = *RExC_parse == 'g';
8415 if (*RExC_parse == '{') {
8419 if (*RExC_parse == '-') {
8423 if (hasbrace && !isDIGIT(*RExC_parse)) {
8424 if (isrel) RExC_parse--;
8426 goto parse_named_seq;
8428 num = atoi(RExC_parse);
8429 if (isg && num == 0)
8430 vFAIL("Reference to invalid group 0");
8432 num = RExC_npar - num;
8434 vFAIL("Reference to nonexistent or unclosed group");
8436 if (!isg && num > 9 && num >= RExC_npar)
8439 char * const parse_start = RExC_parse - 1; /* MJD */
8440 while (isDIGIT(*RExC_parse))
8442 if (parse_start == RExC_parse - 1)
8443 vFAIL("Unterminated \\g... pattern");
8445 if (*RExC_parse != '}')
8446 vFAIL("Unterminated \\g{...} pattern");
8450 if (num > (I32)RExC_rx->nparens)
8451 vFAIL("Reference to nonexistent group");
8454 ret = reganode(pRExC_state,
8457 : (MORE_ASCII_RESTRICTED)
8459 : (AT_LEAST_UNI_SEMANTICS)
8467 /* override incorrect value set in reganode MJD */
8468 Set_Node_Offset(ret, parse_start+1);
8469 Set_Node_Cur_Length(ret); /* MJD */
8471 nextchar(pRExC_state);
8476 if (RExC_parse >= RExC_end)
8477 FAIL("Trailing \\");
8480 /* Do not generate "unrecognized" warnings here, we fall
8481 back into the quick-grab loop below */
8488 if (RExC_flags & RXf_PMf_EXTENDED) {
8489 if ( reg_skipcomment( pRExC_state ) )
8496 parse_start = RExC_parse - 1;
8509 char_state latest_char_state = generic_char;
8510 register STRLEN len;
8515 U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf;
8516 regnode * orig_emit;
8519 orig_emit = RExC_emit; /* Save the original output node position in
8520 case we need to output a different node
8522 ret = reg_node(pRExC_state,
8523 (U8) ((! FOLD) ? EXACT
8526 : (MORE_ASCII_RESTRICTED)
8528 : (AT_LEAST_UNI_SEMANTICS)
8533 for (len = 0, p = RExC_parse - 1;
8534 len < 127 && p < RExC_end;
8537 char * const oldp = p;
8539 if (RExC_flags & RXf_PMf_EXTENDED)
8540 p = regwhite( pRExC_state, p );
8551 /* Literal Escapes Switch
8553 This switch is meant to handle escape sequences that
8554 resolve to a literal character.
8556 Every escape sequence that represents something
8557 else, like an assertion or a char class, is handled
8558 in the switch marked 'Special Escapes' above in this
8559 routine, but also has an entry here as anything that
8560 isn't explicitly mentioned here will be treated as
8561 an unescaped equivalent literal.
8565 /* These are all the special escapes. */
8566 case 'A': /* Start assertion */
8567 case 'b': case 'B': /* Word-boundary assertion*/
8568 case 'C': /* Single char !DANGEROUS! */
8569 case 'd': case 'D': /* digit class */
8570 case 'g': case 'G': /* generic-backref, pos assertion */
8571 case 'h': case 'H': /* HORIZWS */
8572 case 'k': case 'K': /* named backref, keep marker */
8573 case 'N': /* named char sequence */
8574 case 'p': case 'P': /* Unicode property */
8575 case 'R': /* LNBREAK */
8576 case 's': case 'S': /* space class */
8577 case 'v': case 'V': /* VERTWS */
8578 case 'w': case 'W': /* word class */
8579 case 'X': /* eXtended Unicode "combining character sequence" */
8580 case 'z': case 'Z': /* End of line/string assertion */
8584 /* Anything after here is an escape that resolves to a
8585 literal. (Except digits, which may or may not)
8604 ender = ASCII_TO_NATIVE('\033');
8608 ender = ASCII_TO_NATIVE('\007');
8613 STRLEN brace_len = len;
8615 const char* error_msg;
8617 bool valid = grok_bslash_o(p,
8624 RExC_parse = p; /* going to die anyway; point
8625 to exact spot of failure */
8632 if (PL_encoding && ender < 0x100) {
8633 goto recode_encoding;
8642 char* const e = strchr(p, '}');
8646 vFAIL("Missing right brace on \\x{}");
8649 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
8650 | PERL_SCAN_DISALLOW_PREFIX;
8651 STRLEN numlen = e - p - 1;
8652 ender = grok_hex(p + 1, &numlen, &flags, NULL);
8659 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
8661 ender = grok_hex(p, &numlen, &flags, NULL);
8664 if (PL_encoding && ender < 0x100)
8665 goto recode_encoding;
8669 ender = grok_bslash_c(*p++, UTF, SIZE_ONLY);
8671 case '0': case '1': case '2': case '3':case '4':
8672 case '5': case '6': case '7': case '8':case '9':
8674 (isDIGIT(p[1]) && atoi(p) >= RExC_npar))
8676 I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
8678 ender = grok_oct(p, &numlen, &flags, NULL);
8688 if (PL_encoding && ender < 0x100)
8689 goto recode_encoding;
8692 if (! RExC_override_recoding) {
8693 SV* enc = PL_encoding;
8694 ender = reg_recode((const char)(U8)ender, &enc);
8695 if (!enc && SIZE_ONLY)
8696 ckWARNreg(p, "Invalid escape in the specified encoding");
8702 FAIL("Trailing \\");
8705 if (!SIZE_ONLY&& isALPHA(*p)) {
8706 /* Include any { following the alpha to emphasize
8707 * that it could be part of an escape at some point
8709 int len = (*(p + 1) == '{') ? 2 : 1;
8710 ckWARN3reg(p + len, "Unrecognized escape \\%.*s passed through", len, p);
8712 goto normal_default;
8717 if (UTF8_IS_START(*p) && UTF) {
8719 ender = utf8n_to_uvchr((U8*)p, RExC_end - p,
8720 &numlen, UTF8_ALLOW_DEFAULT);
8726 } /* End of switch on the literal */
8728 /* Certain characters are problematic because their folded
8729 * length is so different from their original length that it
8730 * isn't handleable by the optimizer. They are therefore not
8731 * placed in an EXACTish node; and are here handled specially.
8732 * (Even if the optimizer handled LATIN_SMALL_LETTER_SHARP_S,
8733 * putting it in a special node keeps regexec from having to
8734 * deal with a non-utf8 multi-char fold */
8736 && (ender > 255 || (! MORE_ASCII_RESTRICTED && ! LOC)))
8738 /* We look for either side of the fold. For example \xDF
8739 * folds to 'ss'. We look for both the single character
8740 * \xDF and the sequence 'ss'. When we find something that
8741 * could be one of those, we stop and flush whatever we
8742 * have output so far into the EXACTish node that was being
8743 * built. Then restore the input pointer to what it was.
8744 * regatom will return that EXACT node, and will be called
8745 * again, positioned so the first character is the one in
8746 * question, which we return in a different node type.
8747 * The multi-char folds are a sequence, so the occurrence
8748 * of the first character in that sequence doesn't
8749 * necessarily mean that what follows is the rest of the
8750 * sequence. We keep track of that with a state machine,
8751 * with the state being set to the latest character
8752 * processed before the current one. Most characters will
8753 * set the state to 0, but if one occurs that is part of a
8754 * potential tricky fold sequence, the state is set to that
8755 * character, and the next loop iteration sees if the state
8756 * should progress towards the final folded-from character,
8757 * or if it was a false alarm. If it turns out to be a
8758 * false alarm, the character(s) will be output in a new
8759 * EXACTish node, and join_exact() will later combine them.
8760 * In the case of the 'ss' sequence, which is more common
8761 * and more easily checked, some look-ahead is done to
8762 * save time by ruling-out some false alarms */
8765 latest_char_state = generic_char;
8769 if (AT_LEAST_UNI_SEMANTICS) {
8770 if (latest_char_state == char_s) { /* 'ss' */
8771 ender = LATIN_SMALL_LETTER_SHARP_S;
8774 else if (p < RExC_end) {
8776 /* Look-ahead at the next character. If it
8777 * is also an s, we handle as a sharp s
8778 * tricky regnode. */
8779 if (*p == 's' || *p == 'S') {
8781 /* But first flush anything in the
8782 * EXACTish buffer */
8787 p++; /* Account for swallowing this
8789 ender = LATIN_SMALL_LETTER_SHARP_S;
8792 /* Here, the next character is not a
8793 * literal 's', but still could
8794 * evaluate to one if part of a \o{},
8795 * \x or \OCTAL-DIGIT. The minimum
8796 * length required for that is 4, eg
8800 && (isDIGIT(*(p + 1))
8802 || *(p + 1) == 'o' ))
8805 /* Here, it could be an 's', too much
8806 * bother to figure it out here. Flush
8807 * the buffer if any; when come back
8808 * here, set the state so know that the
8809 * previous char was an 's' */
8811 latest_char_state = generic_char;
8815 latest_char_state = char_s;
8821 /* Here, can't be an 'ss' sequence, or at least not
8822 * one that could fold to/from the sharp ss */
8823 latest_char_state = generic_char;
8825 case 0x03C5: /* First char in upsilon series */
8826 if (p < RExC_end - 4) { /* Need >= 4 bytes left */
8827 latest_char_state = upsilon_1;
8834 latest_char_state = generic_char;
8837 case 0x03B9: /* First char in iota series */
8838 if (p < RExC_end - 4) {
8839 latest_char_state = iota_1;
8846 latest_char_state = generic_char;
8850 if (latest_char_state == upsilon_1) {
8851 latest_char_state = upsilon_2;
8853 else if (latest_char_state == iota_1) {
8854 latest_char_state = iota_2;
8857 latest_char_state = generic_char;
8861 if (latest_char_state == upsilon_2) {
8862 ender = GREEK_SMALL_LETTER_UPSILON_WITH_DIALYTIKA_AND_TONOS;
8865 else if (latest_char_state == iota_2) {
8866 ender = GREEK_SMALL_LETTER_IOTA_WITH_DIALYTIKA_AND_TONOS;
8869 latest_char_state = generic_char;
8872 /* These are the tricky fold characters. Flush any
8874 case GREEK_SMALL_LETTER_UPSILON_WITH_DIALYTIKA_AND_TONOS:
8875 case GREEK_SMALL_LETTER_IOTA_WITH_DIALYTIKA_AND_TONOS:
8876 case LATIN_SMALL_LETTER_SHARP_S:
8877 case LATIN_CAPITAL_LETTER_SHARP_S:
8886 char* const oldregxend = RExC_end;
8887 U8 tmpbuf[UTF8_MAXBYTES+1];
8889 /* Here, we know we need to generate a special
8890 * regnode, and 'ender' contains the tricky
8891 * character. What's done is to pretend it's in a
8892 * [bracketed] class, and let the code that deals
8893 * with those handle it, as that code has all the
8894 * intelligence necessary. First save the current
8895 * parse state, get rid of the already allocated
8896 * but empty EXACT node that the ANYOFV node will
8897 * replace, and point the parse to a buffer which
8898 * we fill with the character we want the regclass
8899 * code to think is being parsed */
8900 RExC_emit = orig_emit;
8901 RExC_parse = (char *) tmpbuf;
8903 U8 *d = uvchr_to_utf8(tmpbuf, ender);
8905 RExC_end = (char *) d;
8907 else { /* ender above 255 already excluded */
8908 tmpbuf[0] = (U8) ender;
8910 RExC_end = RExC_parse + 1;
8913 ret = regclass(pRExC_state,depth+1);
8915 /* Here, have parsed the buffer. Reset the parse to
8916 * the actual input, and return */
8917 RExC_end = oldregxend;
8920 Set_Node_Offset(ret, RExC_parse);
8921 Set_Node_Cur_Length(ret);
8922 nextchar(pRExC_state);
8923 *flagp |= HASWIDTH|SIMPLE;
8929 if ( RExC_flags & RXf_PMf_EXTENDED)
8930 p = regwhite( pRExC_state, p );
8932 /* Prime the casefolded buffer. Locale rules, which apply
8933 * only to code points < 256, aren't known until execution,
8934 * so for them, just output the original character using
8936 if (LOC && ender < 256) {
8937 if (UNI_IS_INVARIANT(ender)) {
8938 *tmpbuf = (U8) ender;
8941 *tmpbuf = UTF8_TWO_BYTE_HI(ender);
8942 *(tmpbuf + 1) = UTF8_TWO_BYTE_LO(ender);
8946 else if (isASCII(ender)) { /* Note: Here can't also be LOC
8948 ender = toLOWER(ender);
8949 *tmpbuf = (U8) ender;
8952 else if (! MORE_ASCII_RESTRICTED && ! LOC) {
8954 /* Locale and /aa require more selectivity about the
8955 * fold, so are handled below. Otherwise, here, just
8957 ender = toFOLD_uni(ender, tmpbuf, &foldlen);
8960 /* Under locale rules or /aa we are not to mix,
8961 * respectively, ords < 256 or ASCII with non-. So
8962 * reject folds that mix them, using only the
8963 * non-folded code point. So do the fold to a
8964 * temporary, and inspect each character in it. */
8965 U8 trialbuf[UTF8_MAXBYTES_CASE+1];
8967 UV tmpender = toFOLD_uni(ender, trialbuf, &foldlen);
8968 U8* e = s + foldlen;
8969 bool fold_ok = TRUE;
8973 || (LOC && (UTF8_IS_INVARIANT(*s)
8974 || UTF8_IS_DOWNGRADEABLE_START(*s))))
8982 Copy(trialbuf, tmpbuf, foldlen, U8);
8986 uvuni_to_utf8(tmpbuf, ender);
8987 foldlen = UNISKIP(ender);
8991 if (p < RExC_end && ISMULT2(p)) { /* Back off on ?+*. */
8996 /* Emit all the Unicode characters. */
8998 for (foldbuf = tmpbuf;
9000 foldlen -= numlen) {
9001 ender = utf8_to_uvchr(foldbuf, &numlen);
9003 const STRLEN unilen = reguni(pRExC_state, ender, s);
9006 /* In EBCDIC the numlen
9007 * and unilen can differ. */
9009 if (numlen >= foldlen)
9013 break; /* "Can't happen." */
9017 const STRLEN unilen = reguni(pRExC_state, ender, s);
9026 REGC((char)ender, s++);
9032 /* Emit all the Unicode characters. */
9034 for (foldbuf = tmpbuf;
9036 foldlen -= numlen) {
9037 ender = utf8_to_uvchr(foldbuf, &numlen);
9039 const STRLEN unilen = reguni(pRExC_state, ender, s);
9042 /* In EBCDIC the numlen
9043 * and unilen can differ. */
9045 if (numlen >= foldlen)
9053 const STRLEN unilen = reguni(pRExC_state, ender, s);
9062 REGC((char)ender, s++);
9065 loopdone: /* Jumped to when encounters something that shouldn't be in
9068 Set_Node_Cur_Length(ret); /* MJD */
9069 nextchar(pRExC_state);
9071 /* len is STRLEN which is unsigned, need to copy to signed */
9074 vFAIL("Internal disaster");
9078 if (len == 1 && UNI_IS_INVARIANT(ender))
9082 RExC_size += STR_SZ(len);
9085 RExC_emit += STR_SZ(len);
9093 /* Jumped to when an unrecognized character set is encountered */
9095 Perl_croak(aTHX_ "panic: Unknown regex character set encoding: %u", get_regex_charset(RExC_flags));
9100 S_regwhite( RExC_state_t *pRExC_state, char *p )
9102 const char *e = RExC_end;
9104 PERL_ARGS_ASSERT_REGWHITE;
9109 else if (*p == '#') {
9118 RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
9126 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
9127 Character classes ([:foo:]) can also be negated ([:^foo:]).
9128 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
9129 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
9130 but trigger failures because they are currently unimplemented. */
9132 #define POSIXCC_DONE(c) ((c) == ':')
9133 #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.')
9134 #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c))
9137 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
9140 I32 namedclass = OOB_NAMEDCLASS;
9142 PERL_ARGS_ASSERT_REGPPOSIXCC;
9144 if (value == '[' && RExC_parse + 1 < RExC_end &&
9145 /* I smell either [: or [= or [. -- POSIX has been here, right? */
9146 POSIXCC(UCHARAT(RExC_parse))) {
9147 const char c = UCHARAT(RExC_parse);
9148 char* const s = RExC_parse++;
9150 while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c)
9152 if (RExC_parse == RExC_end)
9153 /* Grandfather lone [:, [=, [. */
9156 const char* const t = RExC_parse++; /* skip over the c */
9159 if (UCHARAT(RExC_parse) == ']') {
9160 const char *posixcc = s + 1;
9161 RExC_parse++; /* skip over the ending ] */
9164 const I32 complement = *posixcc == '^' ? *posixcc++ : 0;
9165 const I32 skip = t - posixcc;
9167 /* Initially switch on the length of the name. */
9170 if (memEQ(posixcc, "word", 4)) /* this is not POSIX, this is the Perl \w */
9171 namedclass = complement ? ANYOF_NALNUM : ANYOF_ALNUM;
9174 /* Names all of length 5. */
9175 /* alnum alpha ascii blank cntrl digit graph lower
9176 print punct space upper */
9177 /* Offset 4 gives the best switch position. */
9178 switch (posixcc[4]) {
9180 if (memEQ(posixcc, "alph", 4)) /* alpha */
9181 namedclass = complement ? ANYOF_NALPHA : ANYOF_ALPHA;
9184 if (memEQ(posixcc, "spac", 4)) /* space */
9185 namedclass = complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
9188 if (memEQ(posixcc, "grap", 4)) /* graph */
9189 namedclass = complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
9192 if (memEQ(posixcc, "asci", 4)) /* ascii */
9193 namedclass = complement ? ANYOF_NASCII : ANYOF_ASCII;
9196 if (memEQ(posixcc, "blan", 4)) /* blank */
9197 namedclass = complement ? ANYOF_NBLANK : ANYOF_BLANK;
9200 if (memEQ(posixcc, "cntr", 4)) /* cntrl */
9201 namedclass = complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
9204 if (memEQ(posixcc, "alnu", 4)) /* alnum */
9205 namedclass = complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
9208 if (memEQ(posixcc, "lowe", 4)) /* lower */
9209 namedclass = complement ? ANYOF_NLOWER : ANYOF_LOWER;
9210 else if (memEQ(posixcc, "uppe", 4)) /* upper */
9211 namedclass = complement ? ANYOF_NUPPER : ANYOF_UPPER;
9214 if (memEQ(posixcc, "digi", 4)) /* digit */
9215 namedclass = complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
9216 else if (memEQ(posixcc, "prin", 4)) /* print */
9217 namedclass = complement ? ANYOF_NPRINT : ANYOF_PRINT;
9218 else if (memEQ(posixcc, "punc", 4)) /* punct */
9219 namedclass = complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
9224 if (memEQ(posixcc, "xdigit", 6))
9225 namedclass = complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
9229 if (namedclass == OOB_NAMEDCLASS)
9230 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
9232 assert (posixcc[skip] == ':');
9233 assert (posixcc[skip+1] == ']');
9234 } else if (!SIZE_ONLY) {
9235 /* [[=foo=]] and [[.foo.]] are still future. */
9237 /* adjust RExC_parse so the warning shows after
9239 while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']')
9241 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
9244 /* Maternal grandfather:
9245 * "[:" ending in ":" but not in ":]" */
9255 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
9259 PERL_ARGS_ASSERT_CHECKPOSIXCC;
9261 if (POSIXCC(UCHARAT(RExC_parse))) {
9262 const char *s = RExC_parse;
9263 const char c = *s++;
9267 if (*s && c == *s && s[1] == ']') {
9269 "POSIX syntax [%c %c] belongs inside character classes",
9272 /* [[=foo=]] and [[.foo.]] are still future. */
9273 if (POSIXCC_NOTYET(c)) {
9274 /* adjust RExC_parse so the error shows after
9276 while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']')
9278 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
9284 /* No locale test, and always Unicode semantics */
9285 #define _C_C_T_NOLOC_(NAME,TEST,WORD) \
9287 for (value = 0; value < 256; value++) \
9289 stored += set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate); \
9293 case ANYOF_N##NAME: \
9294 for (value = 0; value < 256; value++) \
9296 stored += set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate); \
9301 /* Like the above, but there are differences if we are in uni-8-bit or not, so
9302 * there are two tests passed in, to use depending on that. There aren't any
9303 * cases where the label is different from the name, so no need for that
9305 #define _C_C_T_(NAME, TEST_8, TEST_7, WORD) \
9307 if (LOC) ANYOF_CLASS_SET(ret, ANYOF_##NAME); \
9308 else if (UNI_SEMANTICS) { \
9309 for (value = 0; value < 256; value++) { \
9310 if (TEST_8(value)) stored += \
9311 set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate); \
9315 for (value = 0; value < 128; value++) { \
9316 if (TEST_7(UNI_TO_NATIVE(value))) stored += \
9317 set_regclass_bit(pRExC_state, ret, \
9318 (U8) UNI_TO_NATIVE(value), &l1_fold_invlist, &unicode_alternate); \
9324 case ANYOF_N##NAME: \
9325 if (LOC) ANYOF_CLASS_SET(ret, ANYOF_N##NAME); \
9326 else if (UNI_SEMANTICS) { \
9327 for (value = 0; value < 256; value++) { \
9328 if (! TEST_8(value)) stored += \
9329 set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate); \
9333 for (value = 0; value < 128; value++) { \
9334 if (! TEST_7(UNI_TO_NATIVE(value))) stored += set_regclass_bit( \
9335 pRExC_state, ret, (U8) UNI_TO_NATIVE(value), &l1_fold_invlist, &unicode_alternate); \
9337 if (AT_LEAST_ASCII_RESTRICTED) { \
9338 for (value = 128; value < 256; value++) { \
9339 stored += set_regclass_bit( \
9340 pRExC_state, ret, (U8) UNI_TO_NATIVE(value), &l1_fold_invlist, &unicode_alternate); \
9342 ANYOF_FLAGS(ret) |= ANYOF_UNICODE_ALL; \
9345 /* For a non-ut8 target string with DEPENDS semantics, all above \
9346 * ASCII Latin1 code points match the complement of any of the \
9347 * classes. But in utf8, they have their Unicode semantics, so \
9348 * can't just set them in the bitmap, or else regexec.c will think \
9349 * they matched when they shouldn't. */ \
9350 ANYOF_FLAGS(ret) |= ANYOF_NON_UTF8_LATIN1_ALL; \
9358 S_set_regclass_bit_fold(pTHX_ RExC_state_t *pRExC_state, regnode* node, const U8 value, HV** invlist_ptr, AV** alternate_ptr)
9361 /* Handle the setting of folds in the bitmap for non-locale ANYOF nodes.
9362 * Locale folding is done at run-time, so this function should not be
9363 * called for nodes that are for locales.
9365 * This function sets the bit corresponding to the fold of the input
9366 * 'value', if not already set. The fold of 'f' is 'F', and the fold of
9369 * It also knows about the characters that are in the bitmap that have
9370 * folds that are matchable only outside it, and sets the appropriate lists
9373 * It returns the number of bits that actually changed from 0 to 1 */
9378 PERL_ARGS_ASSERT_SET_REGCLASS_BIT_FOLD;
9380 fold = (AT_LEAST_UNI_SEMANTICS) ? PL_fold_latin1[value]
9383 /* It assumes the bit for 'value' has already been set */
9384 if (fold != value && ! ANYOF_BITMAP_TEST(node, fold)) {
9385 ANYOF_BITMAP_SET(node, fold);
9388 if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(value) && (! isASCII(value) || ! MORE_ASCII_RESTRICTED)) {
9389 /* Certain Latin1 characters have matches outside the bitmap. To get
9390 * here, 'value' is one of those characters. None of these matches is
9391 * valid for ASCII characters under /aa, which have been excluded by
9392 * the 'if' above. The matches fall into three categories:
9393 * 1) They are singly folded-to or -from an above 255 character, as
9394 * LATIN SMALL LETTER Y WITH DIAERESIS and LATIN CAPITAL LETTER Y
9396 * 2) They are part of a multi-char fold with another character in the
9397 * bitmap, only LATIN SMALL LETTER SHARP S => "ss" fits that bill;
9398 * 3) They are part of a multi-char fold with a character not in the
9399 * bitmap, such as various ligatures.
9400 * We aren't dealing fully with multi-char folds, except we do deal
9401 * with the pattern containing a character that has a multi-char fold
9402 * (not so much the inverse).
9403 * For types 1) and 3), the matches only happen when the target string
9404 * is utf8; that's not true for 2), and we set a flag for it.
9406 * The code below adds to the passed in inversion list the single fold
9407 * closures for 'value'. The values are hard-coded here so that an
9408 * innocent-looking character class, like /[ks]/i won't have to go out
9409 * to disk to find the possible matches. XXX It would be better to
9410 * generate these via regen, in case a new version of the Unicode
9411 * standard adds new mappings, though that is not really likely. */
9416 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x212A);
9420 /* LATIN SMALL LETTER LONG S */
9421 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x017F);
9424 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
9425 GREEK_SMALL_LETTER_MU);
9426 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
9427 GREEK_CAPITAL_LETTER_MU);
9429 case LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE:
9430 case LATIN_SMALL_LETTER_A_WITH_RING_ABOVE:
9432 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x212B);
9433 if (DEPENDS_SEMANTICS) { /* See DEPENDS comment below */
9434 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
9435 PL_fold_latin1[value]);
9438 case LATIN_SMALL_LETTER_Y_WITH_DIAERESIS:
9439 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
9440 LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS);
9442 case LATIN_SMALL_LETTER_SHARP_S:
9443 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
9444 LATIN_CAPITAL_LETTER_SHARP_S);
9446 /* Under /a, /d, and /u, this can match the two chars "ss" */
9447 if (! MORE_ASCII_RESTRICTED) {
9448 add_alternate(alternate_ptr, (U8 *) "ss", 2);
9450 /* And under /u or /a, it can match even if the target is
9452 if (AT_LEAST_UNI_SEMANTICS) {
9453 ANYOF_FLAGS(node) |= ANYOF_NONBITMAP_NON_UTF8;
9467 /* These all are targets of multi-character folds from code
9468 * points that require UTF8 to express, so they can't match
9469 * unless the target string is in UTF-8, so no action here is
9470 * necessary, as regexec.c properly handles the general case
9471 * for UTF-8 matching */
9474 /* Use deprecated warning to increase the chances of this
9476 ckWARN2regdep(RExC_parse, "Perl folding rules are not up-to-date for 0x%x; please use the perlbug utility to report;", value);
9480 else if (DEPENDS_SEMANTICS
9482 && PL_fold_latin1[value] != value)
9484 /* Under DEPENDS rules, non-ASCII Latin1 characters match their
9485 * folds only when the target string is in UTF-8. We add the fold
9486 * here to the list of things to match outside the bitmap, which
9487 * won't be looked at unless it is UTF8 (or else if something else
9488 * says to look even if not utf8, but those things better not happen
9489 * under DEPENDS semantics. */
9490 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, PL_fold_latin1[value]);
9497 PERL_STATIC_INLINE U8
9498 S_set_regclass_bit(pTHX_ RExC_state_t *pRExC_state, regnode* node, const U8 value, HV** invlist_ptr, AV** alternate_ptr)
9500 /* This inline function sets a bit in the bitmap if not already set, and if
9501 * appropriate, its fold, returning the number of bits that actually
9502 * changed from 0 to 1 */
9506 PERL_ARGS_ASSERT_SET_REGCLASS_BIT;
9508 if (ANYOF_BITMAP_TEST(node, value)) { /* Already set */
9512 ANYOF_BITMAP_SET(node, value);
9515 if (FOLD && ! LOC) { /* Locale folds aren't known until runtime */
9516 stored += set_regclass_bit_fold(pRExC_state, node, value, invlist_ptr, alternate_ptr);
9523 S_add_alternate(pTHX_ AV** alternate_ptr, U8* string, STRLEN len)
9525 /* Adds input 'string' with length 'len' to the ANYOF node's unicode
9526 * alternate list, pointed to by 'alternate_ptr'. This is an array of
9527 * the multi-character folds of characters in the node */
9530 PERL_ARGS_ASSERT_ADD_ALTERNATE;
9532 if (! *alternate_ptr) {
9533 *alternate_ptr = newAV();
9535 sv = newSVpvn_utf8((char*)string, len, TRUE);
9536 av_push(*alternate_ptr, sv);
9541 parse a class specification and produce either an ANYOF node that
9542 matches the pattern or perhaps will be optimized into an EXACTish node
9543 instead. The node contains a bit map for the first 256 characters, with the
9544 corresponding bit set if that character is in the list. For characters
9545 above 255, a range list is used */
9548 S_regclass(pTHX_ RExC_state_t *pRExC_state, U32 depth)
9551 register UV nextvalue;
9552 register IV prevvalue = OOB_UNICODE;
9553 register IV range = 0;
9554 UV value = 0; /* XXX:dmq: needs to be referenceable (unfortunately) */
9555 register regnode *ret;
9558 char *rangebegin = NULL;
9559 bool need_class = 0;
9560 bool allow_full_fold = TRUE; /* Assume wants multi-char folding */
9562 STRLEN initial_listsv_len = 0; /* Kind of a kludge to see if it is more
9563 than just initialized. */
9566 /* code points this node matches that can't be stored in the bitmap */
9567 HV* nonbitmap = NULL;
9569 /* The items that are to match that aren't stored in the bitmap, but are a
9570 * result of things that are stored there. This is the fold closure of
9571 * such a character, either because it has DEPENDS semantics and shouldn't
9572 * be matched unless the target string is utf8, or is a code point that is
9573 * too large for the bit map, as for example, the fold of the MICRO SIGN is
9574 * above 255. This all is solely for performance reasons. By having this
9575 * code know the outside-the-bitmap folds that the bitmapped characters are
9576 * involved with, we don't have to go out to disk to find the list of
9577 * matches, unless the character class includes code points that aren't
9578 * storable in the bit map. That means that a character class with an 's'
9579 * in it, for example, doesn't need to go out to disk to find everything
9580 * that matches. A 2nd list is used so that the 'nonbitmap' list is kept
9581 * empty unless there is something whose fold we don't know about, and will
9582 * have to go out to the disk to find. */
9583 HV* l1_fold_invlist = NULL;
9585 /* List of multi-character folds that are matched by this node */
9586 AV* unicode_alternate = NULL;
9588 UV literal_endpoint = 0;
9590 UV stored = 0; /* how many chars stored in the bitmap */
9592 regnode * const orig_emit = RExC_emit; /* Save the original RExC_emit in
9593 case we need to change the emitted regop to an EXACT. */
9594 const char * orig_parse = RExC_parse;
9595 GET_RE_DEBUG_FLAGS_DECL;
9597 PERL_ARGS_ASSERT_REGCLASS;
9599 PERL_UNUSED_ARG(depth);
9602 DEBUG_PARSE("clas");
9604 /* Assume we are going to generate an ANYOF node. */
9605 ret = reganode(pRExC_state, ANYOF, 0);
9609 ANYOF_FLAGS(ret) = 0;
9612 if (UCHARAT(RExC_parse) == '^') { /* Complement of range. */
9616 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
9618 /* We have decided to not allow multi-char folds in inverted character
9619 * classes, due to the confusion that can happen, even with classes
9620 * that are designed for a non-Unicode world: You have the peculiar
9622 "s s" =~ /^[^\xDF]+$/i => Y
9623 "ss" =~ /^[^\xDF]+$/i => N
9625 * See [perl #89750] */
9626 allow_full_fold = FALSE;
9630 RExC_size += ANYOF_SKIP;
9631 listsv = &PL_sv_undef; /* For code scanners: listsv always non-NULL. */
9634 RExC_emit += ANYOF_SKIP;
9636 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
9638 ANYOF_BITMAP_ZERO(ret);
9639 listsv = newSVpvs("# comment\n");
9640 initial_listsv_len = SvCUR(listsv);
9643 nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
9645 if (!SIZE_ONLY && POSIXCC(nextvalue))
9646 checkposixcc(pRExC_state);
9648 /* allow 1st char to be ] (allowing it to be - is dealt with later) */
9649 if (UCHARAT(RExC_parse) == ']')
9653 while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') {
9657 namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
9660 rangebegin = RExC_parse;
9662 value = utf8n_to_uvchr((U8*)RExC_parse,
9663 RExC_end - RExC_parse,
9664 &numlen, UTF8_ALLOW_DEFAULT);
9665 RExC_parse += numlen;
9668 value = UCHARAT(RExC_parse++);
9670 nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
9671 if (value == '[' && POSIXCC(nextvalue))
9672 namedclass = regpposixcc(pRExC_state, value);
9673 else if (value == '\\') {
9675 value = utf8n_to_uvchr((U8*)RExC_parse,
9676 RExC_end - RExC_parse,
9677 &numlen, UTF8_ALLOW_DEFAULT);
9678 RExC_parse += numlen;
9681 value = UCHARAT(RExC_parse++);
9682 /* Some compilers cannot handle switching on 64-bit integer
9683 * values, therefore value cannot be an UV. Yes, this will
9684 * be a problem later if we want switch on Unicode.
9685 * A similar issue a little bit later when switching on
9686 * namedclass. --jhi */
9687 switch ((I32)value) {
9688 case 'w': namedclass = ANYOF_ALNUM; break;
9689 case 'W': namedclass = ANYOF_NALNUM; break;
9690 case 's': namedclass = ANYOF_SPACE; break;
9691 case 'S': namedclass = ANYOF_NSPACE; break;
9692 case 'd': namedclass = ANYOF_DIGIT; break;
9693 case 'D': namedclass = ANYOF_NDIGIT; break;
9694 case 'v': namedclass = ANYOF_VERTWS; break;
9695 case 'V': namedclass = ANYOF_NVERTWS; break;
9696 case 'h': namedclass = ANYOF_HORIZWS; break;
9697 case 'H': namedclass = ANYOF_NHORIZWS; break;
9698 case 'N': /* Handle \N{NAME} in class */
9700 /* We only pay attention to the first char of
9701 multichar strings being returned. I kinda wonder
9702 if this makes sense as it does change the behaviour
9703 from earlier versions, OTOH that behaviour was broken
9705 UV v; /* value is register so we cant & it /grrr */
9706 if (reg_namedseq(pRExC_state, &v, NULL, depth)) {
9716 if (RExC_parse >= RExC_end)
9717 vFAIL2("Empty \\%c{}", (U8)value);
9718 if (*RExC_parse == '{') {
9719 const U8 c = (U8)value;
9720 e = strchr(RExC_parse++, '}');
9722 vFAIL2("Missing right brace on \\%c{}", c);
9723 while (isSPACE(UCHARAT(RExC_parse)))
9725 if (e == RExC_parse)
9726 vFAIL2("Empty \\%c{}", c);
9728 while (isSPACE(UCHARAT(RExC_parse + n - 1)))
9736 if (UCHARAT(RExC_parse) == '^') {
9739 value = value == 'p' ? 'P' : 'p'; /* toggle */
9740 while (isSPACE(UCHARAT(RExC_parse))) {
9746 /* Add the property name to the list. If /i matching, give
9747 * a different name which consists of the normal name
9748 * sandwiched between two underscores and '_i'. The design
9749 * is discussed in the commit message for this. */
9750 Perl_sv_catpvf(aTHX_ listsv, "%cutf8::%s%.*s%s\n",
9751 (value=='p' ? '+' : '!'),
9760 /* The \p could match something in the Latin1 range, hence
9761 * something that isn't utf8 */
9762 ANYOF_FLAGS(ret) |= ANYOF_NONBITMAP_NON_UTF8;
9763 namedclass = ANYOF_MAX; /* no official name, but it's named */
9765 /* \p means they want Unicode semantics */
9766 RExC_uni_semantics = 1;
9769 case 'n': value = '\n'; break;
9770 case 'r': value = '\r'; break;
9771 case 't': value = '\t'; break;
9772 case 'f': value = '\f'; break;
9773 case 'b': value = '\b'; break;
9774 case 'e': value = ASCII_TO_NATIVE('\033');break;
9775 case 'a': value = ASCII_TO_NATIVE('\007');break;
9777 RExC_parse--; /* function expects to be pointed at the 'o' */
9779 const char* error_msg;
9780 bool valid = grok_bslash_o(RExC_parse,
9785 RExC_parse += numlen;
9790 if (PL_encoding && value < 0x100) {
9791 goto recode_encoding;
9795 if (*RExC_parse == '{') {
9796 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
9797 | PERL_SCAN_DISALLOW_PREFIX;
9798 char * const e = strchr(RExC_parse++, '}');
9800 vFAIL("Missing right brace on \\x{}");
9802 numlen = e - RExC_parse;
9803 value = grok_hex(RExC_parse, &numlen, &flags, NULL);
9807 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
9809 value = grok_hex(RExC_parse, &numlen, &flags, NULL);
9810 RExC_parse += numlen;
9812 if (PL_encoding && value < 0x100)
9813 goto recode_encoding;
9816 value = grok_bslash_c(*RExC_parse++, UTF, SIZE_ONLY);
9818 case '0': case '1': case '2': case '3': case '4':
9819 case '5': case '6': case '7':
9821 /* Take 1-3 octal digits */
9822 I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
9824 value = grok_oct(--RExC_parse, &numlen, &flags, NULL);
9825 RExC_parse += numlen;
9826 if (PL_encoding && value < 0x100)
9827 goto recode_encoding;
9831 if (! RExC_override_recoding) {
9832 SV* enc = PL_encoding;
9833 value = reg_recode((const char)(U8)value, &enc);
9834 if (!enc && SIZE_ONLY)
9835 ckWARNreg(RExC_parse,
9836 "Invalid escape in the specified encoding");
9840 /* Allow \_ to not give an error */
9841 if (!SIZE_ONLY && isALNUM(value) && value != '_') {
9842 ckWARN2reg(RExC_parse,
9843 "Unrecognized escape \\%c in character class passed through",
9848 } /* end of \blah */
9854 if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
9856 /* What matches in a locale is not known until runtime, so need to
9857 * (one time per class) allocate extra space to pass to regexec.
9858 * The space will contain a bit for each named class that is to be
9859 * matched against. This isn't needed for \p{} and pseudo-classes,
9860 * as they are not affected by locale, and hence are dealt with
9862 if (LOC && namedclass < ANYOF_MAX && ! need_class) {
9865 RExC_size += ANYOF_CLASS_SKIP - ANYOF_SKIP;
9868 RExC_emit += ANYOF_CLASS_SKIP - ANYOF_SKIP;
9869 ANYOF_CLASS_ZERO(ret);
9871 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
9874 /* a bad range like a-\d, a-[:digit:]. The '-' is taken as a
9875 * literal, as is the character that began the false range, i.e.
9876 * the 'a' in the examples */
9880 RExC_parse >= rangebegin ?
9881 RExC_parse - rangebegin : 0;
9882 ckWARN4reg(RExC_parse,
9883 "False [] range \"%*.*s\"",
9887 set_regclass_bit(pRExC_state, ret, '-', &l1_fold_invlist, &unicode_alternate);
9888 if (prevvalue < 256) {
9890 set_regclass_bit(pRExC_state, ret, (U8) prevvalue, &l1_fold_invlist, &unicode_alternate);
9893 nonbitmap = add_cp_to_invlist(nonbitmap, prevvalue);
9897 range = 0; /* this was not a true range */
9903 const char *what = NULL;
9906 /* Possible truncation here but in some 64-bit environments
9907 * the compiler gets heartburn about switch on 64-bit values.
9908 * A similar issue a little earlier when switching on value.
9910 switch ((I32)namedclass) {
9912 case _C_C_T_(ALNUMC, isALNUMC_L1, isALNUMC, "XPosixAlnum");
9913 case _C_C_T_(ALPHA, isALPHA_L1, isALPHA, "XPosixAlpha");
9914 case _C_C_T_(BLANK, isBLANK_L1, isBLANK, "XPosixBlank");
9915 case _C_C_T_(CNTRL, isCNTRL_L1, isCNTRL, "XPosixCntrl");
9916 case _C_C_T_(GRAPH, isGRAPH_L1, isGRAPH, "XPosixGraph");
9917 case _C_C_T_(LOWER, isLOWER_L1, isLOWER, "XPosixLower");
9918 case _C_C_T_(PRINT, isPRINT_L1, isPRINT, "XPosixPrint");
9919 case _C_C_T_(PSXSPC, isPSXSPC_L1, isPSXSPC, "XPosixSpace");
9920 case _C_C_T_(PUNCT, isPUNCT_L1, isPUNCT, "XPosixPunct");
9921 case _C_C_T_(UPPER, isUPPER_L1, isUPPER, "XPosixUpper");
9922 /* \s, \w match all unicode if utf8. */
9923 case _C_C_T_(SPACE, isSPACE_L1, isSPACE, "SpacePerl");
9924 case _C_C_T_(ALNUM, isWORDCHAR_L1, isALNUM, "Word");
9925 case _C_C_T_(XDIGIT, isXDIGIT_L1, isXDIGIT, "XPosixXDigit");
9926 case _C_C_T_NOLOC_(VERTWS, is_VERTWS_latin1(&value), "VertSpace");
9927 case _C_C_T_NOLOC_(HORIZWS, is_HORIZWS_latin1(&value), "HorizSpace");
9930 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
9932 for (value = 0; value < 128; value++)
9934 set_regclass_bit(pRExC_state, ret, (U8) ASCII_TO_NATIVE(value), &l1_fold_invlist, &unicode_alternate);
9937 what = NULL; /* Doesn't match outside ascii, so
9938 don't want to add +utf8:: */
9942 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
9944 for (value = 128; value < 256; value++)
9946 set_regclass_bit(pRExC_state, ret, (U8) ASCII_TO_NATIVE(value), &l1_fold_invlist, &unicode_alternate);
9948 ANYOF_FLAGS(ret) |= ANYOF_UNICODE_ALL;
9954 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
9956 /* consecutive digits assumed */
9957 for (value = '0'; value <= '9'; value++)
9959 set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate);
9966 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
9968 /* consecutive digits assumed */
9969 for (value = 0; value < '0'; value++)
9971 set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate);
9972 for (value = '9' + 1; value < 256; value++)
9974 set_regclass_bit(pRExC_state, ret, (U8) value, &l1_fold_invlist, &unicode_alternate);
9978 if (AT_LEAST_ASCII_RESTRICTED ) {
9979 ANYOF_FLAGS(ret) |= ANYOF_UNICODE_ALL;
9983 /* this is to handle \p and \P */
9986 vFAIL("Invalid [::] class");
9989 if (what && ! (AT_LEAST_ASCII_RESTRICTED)) {
9990 /* Strings such as "+utf8::isWord\n" */
9991 Perl_sv_catpvf(aTHX_ listsv, "%cutf8::Is%s\n", yesno, what);
9996 } /* end of namedclass \blah */
9999 if (prevvalue > (IV)value) /* b-a */ {
10000 const int w = RExC_parse - rangebegin;
10001 Simple_vFAIL4("Invalid [] range \"%*.*s\"", w, w, rangebegin);
10002 range = 0; /* not a valid range */
10006 prevvalue = value; /* save the beginning of the range */
10007 if (RExC_parse+1 < RExC_end
10008 && *RExC_parse == '-'
10009 && RExC_parse[1] != ']')
10013 /* a bad range like \w-, [:word:]- ? */
10014 if (namedclass > OOB_NAMEDCLASS) {
10015 if (ckWARN(WARN_REGEXP)) {
10017 RExC_parse >= rangebegin ?
10018 RExC_parse - rangebegin : 0;
10020 "False [] range \"%*.*s\"",
10025 set_regclass_bit(pRExC_state, ret, '-', &l1_fold_invlist, &unicode_alternate);
10027 range = 1; /* yeah, it's a range! */
10028 continue; /* but do it the next time */
10032 /* non-Latin1 code point implies unicode semantics. Must be set in
10033 * pass1 so is there for the whole of pass 2 */
10035 RExC_uni_semantics = 1;
10038 /* now is the next time */
10040 if (prevvalue < 256) {
10041 const IV ceilvalue = value < 256 ? value : 255;
10044 /* In EBCDIC [\x89-\x91] should include
10045 * the \x8e but [i-j] should not. */
10046 if (literal_endpoint == 2 &&
10047 ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
10048 (isUPPER(prevvalue) && isUPPER(ceilvalue))))
10050 if (isLOWER(prevvalue)) {
10051 for (i = prevvalue; i <= ceilvalue; i++)
10052 if (isLOWER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
10054 set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
10057 for (i = prevvalue; i <= ceilvalue; i++)
10058 if (isUPPER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
10060 set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
10066 for (i = prevvalue; i <= ceilvalue; i++) {
10067 stored += set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
10071 const UV prevnatvalue = NATIVE_TO_UNI(prevvalue);
10072 const UV natvalue = NATIVE_TO_UNI(value);
10073 nonbitmap = add_range_to_invlist(nonbitmap, prevnatvalue, natvalue);
10076 literal_endpoint = 0;
10080 range = 0; /* this range (if it was one) is done now */
10087 /****** !SIZE_ONLY AFTER HERE *********/
10089 /* If folding and there are code points above 255, we calculate all
10090 * characters that could fold to or from the ones already on the list */
10091 if (FOLD && nonbitmap) {
10094 HV* fold_intersection;
10097 /* This is a list of all the characters that participate in folds
10098 * (except marks, etc in multi-char folds */
10099 if (! PL_utf8_foldable) {
10100 SV* swash = swash_init("utf8", "Cased", &PL_sv_undef, 1, 0);
10101 PL_utf8_foldable = _swash_to_invlist(swash);
10104 /* This is a hash that for a particular fold gives all characters
10105 * that are involved in it */
10106 if (! PL_utf8_foldclosures) {
10108 /* If we were unable to find any folds, then we likely won't be
10109 * able to find the closures. So just create an empty list.
10110 * Folding will effectively be restricted to the non-Unicode rules
10111 * hard-coded into Perl. (This case happens legitimately during
10112 * compilation of Perl itself before the Unicode tables are
10114 if (invlist_len(PL_utf8_foldable) == 0) {
10115 PL_utf8_foldclosures = _new_invlist(0);
10117 /* If the folds haven't been read in, call a fold function
10119 if (! PL_utf8_tofold) {
10120 U8 dummy[UTF8_MAXBYTES+1];
10122 to_utf8_fold((U8*) "A", dummy, &dummy_len);
10124 PL_utf8_foldclosures = _swash_inversion_hash(PL_utf8_tofold);
10128 /* Only the characters in this class that participate in folds need
10129 * be checked. Get the intersection of this class and all the
10130 * possible characters that are foldable. This can quickly narrow
10131 * down a large class */
10132 fold_intersection = invlist_intersection(PL_utf8_foldable, nonbitmap);
10134 /* Now look at the foldable characters in this class individually */
10135 fold_list = invlist_array(fold_intersection);
10136 for (i = 0; i < invlist_len(fold_intersection); i++) {
10139 /* The next entry is the beginning of the range that is in the
10141 UV start = fold_list[i++];
10144 /* The next entry is the beginning of the next range, which
10145 * isn't in the class, so the end of the current range is one
10146 * less than that */
10147 UV end = fold_list[i] - 1;
10149 /* Look at every character in the range */
10150 for (j = start; j <= end; j++) {
10153 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
10156 _to_uni_fold_flags(j, foldbuf, &foldlen, allow_full_fold);
10158 if (foldlen > (STRLEN)UNISKIP(f)) {
10160 /* Any multicharacter foldings (disallowed in
10161 * lookbehind patterns) require the following
10162 * transform: [ABCDEF] -> (?:[ABCabcDEFd]|pq|rst) where
10163 * E folds into "pq" and F folds into "rst", all other
10164 * characters fold to single characters. We save away
10165 * these multicharacter foldings, to be later saved as
10166 * part of the additional "s" data. */
10167 if (! RExC_in_lookbehind) {
10169 U8* e = foldbuf + foldlen;
10171 /* If any of the folded characters of this are in
10172 * the Latin1 range, tell the regex engine that
10173 * this can match a non-utf8 target string. The
10174 * only multi-byte fold whose source is in the
10175 * Latin1 range (U+00DF) applies only when the
10176 * target string is utf8, or under unicode rules */
10177 if (j > 255 || AT_LEAST_UNI_SEMANTICS) {
10180 /* Can't mix ascii with non- under /aa */
10181 if (MORE_ASCII_RESTRICTED
10182 && (isASCII(*loc) != isASCII(j)))
10184 goto end_multi_fold;
10186 if (UTF8_IS_INVARIANT(*loc)
10187 || UTF8_IS_DOWNGRADEABLE_START(*loc))
10189 /* Can't mix above and below 256 under
10192 goto end_multi_fold;
10195 |= ANYOF_NONBITMAP_NON_UTF8;
10198 loc += UTF8SKIP(loc);
10202 add_alternate(&unicode_alternate, foldbuf, foldlen);
10206 /* This is special-cased, as it is the only letter which
10207 * has both a multi-fold and single-fold in Latin1. All
10208 * the other chars that have single and multi-folds are
10209 * always in utf8, and the utf8 folding algorithm catches
10211 if (! LOC && j == LATIN_CAPITAL_LETTER_SHARP_S) {
10212 stored += set_regclass_bit(pRExC_state,
10214 LATIN_SMALL_LETTER_SHARP_S,
10215 &l1_fold_invlist, &unicode_alternate);
10219 /* Single character fold. Add everything in its fold
10220 * closure to the list that this node should match */
10223 /* The fold closures data structure is a hash with the
10224 * keys being every character that is folded to, like
10225 * 'k', and the values each an array of everything that
10226 * folds to its key. e.g. [ 'k', 'K', KELVIN_SIGN ] */
10227 if ((listp = hv_fetch(PL_utf8_foldclosures,
10228 (char *) foldbuf, foldlen, FALSE)))
10230 AV* list = (AV*) *listp;
10232 for (k = 0; k <= av_len(list); k++) {
10233 SV** c_p = av_fetch(list, k, FALSE);
10236 Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
10240 /* /aa doesn't allow folds between ASCII and
10241 * non-; /l doesn't allow them between above
10243 if ((MORE_ASCII_RESTRICTED
10244 && (isASCII(c) != isASCII(j)))
10245 || (LOC && ((c < 256) != (j < 256))))
10250 if (c < 256 && AT_LEAST_UNI_SEMANTICS) {
10251 stored += set_regclass_bit(pRExC_state,
10254 &l1_fold_invlist, &unicode_alternate);
10256 /* It may be that the code point is already
10257 * in this range or already in the bitmap,
10258 * in which case we need do nothing */
10259 else if ((c < start || c > end)
10261 || ! ANYOF_BITMAP_TEST(ret, c)))
10263 nonbitmap = add_cp_to_invlist(nonbitmap, c);
10270 invlist_destroy(fold_intersection);
10273 /* Combine the two lists into one. */
10274 if (l1_fold_invlist) {
10276 nonbitmap = invlist_union(nonbitmap, l1_fold_invlist);
10279 nonbitmap = l1_fold_invlist;
10283 /* Here, we have calculated what code points should be in the character
10284 * class. Now we can see about various optimizations. Fold calculation
10285 * needs to take place before inversion. Otherwise /[^k]/i would invert to
10286 * include K, which under /i would match k. */
10288 /* Optimize inverted simple patterns (e.g. [^a-z]). Note that we haven't
10289 * set the FOLD flag yet, so this this does optimize those. It doesn't
10290 * optimize locale. Doing so perhaps could be done as long as there is
10291 * nothing like \w in it; some thought also would have to be given to the
10292 * interaction with above 0x100 chars */
10294 && (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT
10295 && ! unicode_alternate
10297 && SvCUR(listsv) == initial_listsv_len)
10299 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
10300 ANYOF_BITMAP(ret)[value] ^= 0xFF;
10301 stored = 256 - stored;
10303 /* The inversion means that everything above 255 is matched; and at the
10304 * same time we clear the invert flag */
10305 ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
10308 /* Folding in the bitmap is taken care of above, but not for locale (for
10309 * which we have to wait to see what folding is in effect at runtime), and
10310 * for things not in the bitmap. Set run-time fold flag for these */
10311 if (FOLD && (LOC || nonbitmap || unicode_alternate)) {
10312 ANYOF_FLAGS(ret) |= ANYOF_LOC_NONBITMAP_FOLD;
10315 /* A single character class can be "optimized" into an EXACTish node.
10316 * Note that since we don't currently count how many characters there are
10317 * outside the bitmap, we are XXX missing optimization possibilities for
10318 * them. This optimization can't happen unless this is a truly single
10319 * character class, which means that it can't be an inversion into a
10320 * many-character class, and there must be no possibility of there being
10321 * things outside the bitmap. 'stored' (only) for locales doesn't include
10322 * \w, etc, so have to make a special test that they aren't present
10324 * Similarly A 2-character class of the very special form like [bB] can be
10325 * optimized into an EXACTFish node, but only for non-locales, and for
10326 * characters which only have the two folds; so things like 'fF' and 'Ii'
10327 * wouldn't work because they are part of the fold of 'LATIN SMALL LIGATURE
10330 && ! unicode_alternate
10331 && SvCUR(listsv) == initial_listsv_len
10332 && ! (ANYOF_FLAGS(ret) & (ANYOF_INVERT|ANYOF_UNICODE_ALL))
10333 && (((stored == 1 && ((! (ANYOF_FLAGS(ret) & ANYOF_LOCALE))
10334 || (! ANYOF_CLASS_TEST_ANY_SET(ret)))))
10335 || (stored == 2 && ((! (ANYOF_FLAGS(ret) & ANYOF_LOCALE))
10336 && (! _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(value))
10337 /* If the latest code point has a fold whose
10338 * bit is set, it must be the only other one */
10339 && ((prevvalue = PL_fold_latin1[value]) != (IV)value)
10340 && ANYOF_BITMAP_TEST(ret, prevvalue)))))
10342 /* Note that the information needed to decide to do this optimization
10343 * is not currently available until the 2nd pass, and that the actually
10344 * used EXACTish node takes less space than the calculated ANYOF node,
10345 * and hence the amount of space calculated in the first pass is larger
10346 * than actually used, so this optimization doesn't gain us any space.
10347 * But an EXACT node is faster than an ANYOF node, and can be combined
10348 * with any adjacent EXACT nodes later by the optimizer for further
10349 * gains. The speed of executing an EXACTF is similar to an ANYOF
10350 * node, so the optimization advantage comes from the ability to join
10351 * it to adjacent EXACT nodes */
10353 const char * cur_parse= RExC_parse;
10355 RExC_emit = (regnode *)orig_emit;
10356 RExC_parse = (char *)orig_parse;
10360 /* A locale node with one point can be folded; all the other cases
10361 * with folding will have two points, since we calculate them above
10363 if (ANYOF_FLAGS(ret) & ANYOF_LOC_NONBITMAP_FOLD) {
10369 } /* else 2 chars in the bit map: the folds of each other */
10370 else if (AT_LEAST_UNI_SEMANTICS || !isASCII(value)) {
10372 /* To join adjacent nodes, they must be the exact EXACTish type.
10373 * Try to use the most likely type, by using EXACTFU if the regex
10374 * calls for them, or is required because the character is
10378 else { /* Otherwise, more likely to be EXACTF type */
10382 ret = reg_node(pRExC_state, op);
10383 RExC_parse = (char *)cur_parse;
10384 if (UTF && ! NATIVE_IS_INVARIANT(value)) {
10385 *STRING(ret)= UTF8_EIGHT_BIT_HI((U8) value);
10386 *(STRING(ret) + 1)= UTF8_EIGHT_BIT_LO((U8) value);
10388 RExC_emit += STR_SZ(2);
10391 *STRING(ret)= (char)value;
10393 RExC_emit += STR_SZ(1);
10395 SvREFCNT_dec(listsv);
10400 UV* nonbitmap_array = invlist_array(nonbitmap);
10401 UV nonbitmap_len = invlist_len(nonbitmap);
10404 /* Here have the full list of items to match that aren't in the
10405 * bitmap. Convert to the structure that the rest of the code is
10406 * expecting. XXX That rest of the code should convert to this
10408 for (i = 0; i < nonbitmap_len; i++) {
10410 /* The next entry is the beginning of the range that is in the
10412 UV start = nonbitmap_array[i++];
10415 /* The next entry is the beginning of the next range, which isn't
10416 * in the class, so the end of the current range is one less than
10417 * that. But if there is no next range, it means that the range
10418 * begun by 'start' extends to infinity, which for this platform
10419 * ends at UV_MAX */
10420 if (i == nonbitmap_len) {
10424 end = nonbitmap_array[i] - 1;
10427 if (start == end) {
10428 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", start);
10431 /* The \t sets the whole range */
10432 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
10437 invlist_destroy(nonbitmap);
10440 if (SvCUR(listsv) == initial_listsv_len && ! unicode_alternate) {
10441 ARG_SET(ret, ANYOF_NONBITMAP_EMPTY);
10442 SvREFCNT_dec(listsv);
10443 SvREFCNT_dec(unicode_alternate);
10447 AV * const av = newAV();
10449 /* The 0th element stores the character class description
10450 * in its textual form: used later (regexec.c:Perl_regclass_swash())
10451 * to initialize the appropriate swash (which gets stored in
10452 * the 1st element), and also useful for dumping the regnode.
10453 * The 2nd element stores the multicharacter foldings,
10454 * used later (regexec.c:S_reginclass()). */
10455 av_store(av, 0, listsv);
10456 av_store(av, 1, NULL);
10458 /* Store any computed multi-char folds only if we are allowing
10460 if (allow_full_fold) {
10461 av_store(av, 2, MUTABLE_SV(unicode_alternate));
10462 if (unicode_alternate) { /* This node is variable length */
10467 av_store(av, 2, NULL);
10469 rv = newRV_noinc(MUTABLE_SV(av));
10470 n = add_data(pRExC_state, 1, "s");
10471 RExC_rxi->data->data[n] = (void*)rv;
10479 /* reg_skipcomment()
10481 Absorbs an /x style # comments from the input stream.
10482 Returns true if there is more text remaining in the stream.
10483 Will set the REG_SEEN_RUN_ON_COMMENT flag if the comment
10484 terminates the pattern without including a newline.
10486 Note its the callers responsibility to ensure that we are
10487 actually in /x mode
10492 S_reg_skipcomment(pTHX_ RExC_state_t *pRExC_state)
10496 PERL_ARGS_ASSERT_REG_SKIPCOMMENT;
10498 while (RExC_parse < RExC_end)
10499 if (*RExC_parse++ == '\n') {
10504 /* we ran off the end of the pattern without ending
10505 the comment, so we have to add an \n when wrapping */
10506 RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
10514 Advances the parse position, and optionally absorbs
10515 "whitespace" from the inputstream.
10517 Without /x "whitespace" means (?#...) style comments only,
10518 with /x this means (?#...) and # comments and whitespace proper.
10520 Returns the RExC_parse point from BEFORE the scan occurs.
10522 This is the /x friendly way of saying RExC_parse++.
10526 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
10528 char* const retval = RExC_parse++;
10530 PERL_ARGS_ASSERT_NEXTCHAR;
10533 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
10534 RExC_parse[2] == '#') {
10535 while (*RExC_parse != ')') {
10536 if (RExC_parse == RExC_end)
10537 FAIL("Sequence (?#... not terminated");
10543 if (RExC_flags & RXf_PMf_EXTENDED) {
10544 if (isSPACE(*RExC_parse)) {
10548 else if (*RExC_parse == '#') {
10549 if ( reg_skipcomment( pRExC_state ) )
10558 - reg_node - emit a node
10560 STATIC regnode * /* Location. */
10561 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
10564 register regnode *ptr;
10565 regnode * const ret = RExC_emit;
10566 GET_RE_DEBUG_FLAGS_DECL;
10568 PERL_ARGS_ASSERT_REG_NODE;
10571 SIZE_ALIGN(RExC_size);
10575 if (RExC_emit >= RExC_emit_bound)
10576 Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d", op);
10578 NODE_ALIGN_FILL(ret);
10580 FILL_ADVANCE_NODE(ptr, op);
10581 REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (ptr) - 1);
10582 #ifdef RE_TRACK_PATTERN_OFFSETS
10583 if (RExC_offsets) { /* MJD */
10584 MJD_OFFSET_DEBUG(("%s:%d: (op %s) %s %"UVuf" (len %"UVuf") (max %"UVuf").\n",
10585 "reg_node", __LINE__,
10587 (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0]
10588 ? "Overwriting end of array!\n" : "OK",
10589 (UV)(RExC_emit - RExC_emit_start),
10590 (UV)(RExC_parse - RExC_start),
10591 (UV)RExC_offsets[0]));
10592 Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
10600 - reganode - emit a node with an argument
10602 STATIC regnode * /* Location. */
10603 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
10606 register regnode *ptr;
10607 regnode * const ret = RExC_emit;
10608 GET_RE_DEBUG_FLAGS_DECL;
10610 PERL_ARGS_ASSERT_REGANODE;
10613 SIZE_ALIGN(RExC_size);
10618 assert(2==regarglen[op]+1);
10620 Anything larger than this has to allocate the extra amount.
10621 If we changed this to be:
10623 RExC_size += (1 + regarglen[op]);
10625 then it wouldn't matter. Its not clear what side effect
10626 might come from that so its not done so far.
10631 if (RExC_emit >= RExC_emit_bound)
10632 Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d", op);
10634 NODE_ALIGN_FILL(ret);
10636 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
10637 REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (ptr) - 2);
10638 #ifdef RE_TRACK_PATTERN_OFFSETS
10639 if (RExC_offsets) { /* MJD */
10640 MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n",
10644 (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] ?
10645 "Overwriting end of array!\n" : "OK",
10646 (UV)(RExC_emit - RExC_emit_start),
10647 (UV)(RExC_parse - RExC_start),
10648 (UV)RExC_offsets[0]));
10649 Set_Cur_Node_Offset;
10657 - reguni - emit (if appropriate) a Unicode character
10660 S_reguni(pTHX_ const RExC_state_t *pRExC_state, UV uv, char* s)
10664 PERL_ARGS_ASSERT_REGUNI;
10666 return SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s);
10670 - reginsert - insert an operator in front of already-emitted operand
10672 * Means relocating the operand.
10675 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd, U32 depth)
10678 register regnode *src;
10679 register regnode *dst;
10680 register regnode *place;
10681 const int offset = regarglen[(U8)op];
10682 const int size = NODE_STEP_REGNODE + offset;
10683 GET_RE_DEBUG_FLAGS_DECL;
10685 PERL_ARGS_ASSERT_REGINSERT;
10686 PERL_UNUSED_ARG(depth);
10687 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
10688 DEBUG_PARSE_FMT("inst"," - %s",PL_reg_name[op]);
10697 if (RExC_open_parens) {
10699 /*DEBUG_PARSE_FMT("inst"," - %"IVdf, (IV)RExC_npar);*/
10700 for ( paren=0 ; paren < RExC_npar ; paren++ ) {
10701 if ( RExC_open_parens[paren] >= opnd ) {
10702 /*DEBUG_PARSE_FMT("open"," - %d",size);*/
10703 RExC_open_parens[paren] += size;
10705 /*DEBUG_PARSE_FMT("open"," - %s","ok");*/
10707 if ( RExC_close_parens[paren] >= opnd ) {
10708 /*DEBUG_PARSE_FMT("close"," - %d",size);*/
10709 RExC_close_parens[paren] += size;
10711 /*DEBUG_PARSE_FMT("close"," - %s","ok");*/
10716 while (src > opnd) {
10717 StructCopy(--src, --dst, regnode);
10718 #ifdef RE_TRACK_PATTERN_OFFSETS
10719 if (RExC_offsets) { /* MJD 20010112 */
10720 MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %"UVuf" -> %"UVuf" (max %"UVuf").\n",
10724 (UV)(dst - RExC_emit_start) > RExC_offsets[0]
10725 ? "Overwriting end of array!\n" : "OK",
10726 (UV)(src - RExC_emit_start),
10727 (UV)(dst - RExC_emit_start),
10728 (UV)RExC_offsets[0]));
10729 Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
10730 Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
10736 place = opnd; /* Op node, where operand used to be. */
10737 #ifdef RE_TRACK_PATTERN_OFFSETS
10738 if (RExC_offsets) { /* MJD */
10739 MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n",
10743 (UV)(place - RExC_emit_start) > RExC_offsets[0]
10744 ? "Overwriting end of array!\n" : "OK",
10745 (UV)(place - RExC_emit_start),
10746 (UV)(RExC_parse - RExC_start),
10747 (UV)RExC_offsets[0]));
10748 Set_Node_Offset(place, RExC_parse);
10749 Set_Node_Length(place, 1);
10752 src = NEXTOPER(place);
10753 FILL_ADVANCE_NODE(place, op);
10754 REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (place) - 1);
10755 Zero(src, offset, regnode);
10759 - regtail - set the next-pointer at the end of a node chain of p to val.
10760 - SEE ALSO: regtail_study
10762 /* TODO: All three parms should be const */
10764 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
10767 register regnode *scan;
10768 GET_RE_DEBUG_FLAGS_DECL;
10770 PERL_ARGS_ASSERT_REGTAIL;
10772 PERL_UNUSED_ARG(depth);
10778 /* Find last node. */
10781 regnode * const temp = regnext(scan);
10783 SV * const mysv=sv_newmortal();
10784 DEBUG_PARSE_MSG((scan==p ? "tail" : ""));
10785 regprop(RExC_rx, mysv, scan);
10786 PerlIO_printf(Perl_debug_log, "~ %s (%d) %s %s\n",
10787 SvPV_nolen_const(mysv), REG_NODE_NUM(scan),
10788 (temp == NULL ? "->" : ""),
10789 (temp == NULL ? PL_reg_name[OP(val)] : "")
10797 if (reg_off_by_arg[OP(scan)]) {
10798 ARG_SET(scan, val - scan);
10801 NEXT_OFF(scan) = val - scan;
10807 - regtail_study - set the next-pointer at the end of a node chain of p to val.
10808 - Look for optimizable sequences at the same time.
10809 - currently only looks for EXACT chains.
10811 This is experimental code. The idea is to use this routine to perform
10812 in place optimizations on branches and groups as they are constructed,
10813 with the long term intention of removing optimization from study_chunk so
10814 that it is purely analytical.
10816 Currently only used when in DEBUG mode. The macro REGTAIL_STUDY() is used
10817 to control which is which.
10820 /* TODO: All four parms should be const */
10823 S_regtail_study(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
10826 register regnode *scan;
10828 #ifdef EXPERIMENTAL_INPLACESCAN
10831 GET_RE_DEBUG_FLAGS_DECL;
10833 PERL_ARGS_ASSERT_REGTAIL_STUDY;
10839 /* Find last node. */
10843 regnode * const temp = regnext(scan);
10844 #ifdef EXPERIMENTAL_INPLACESCAN
10845 if (PL_regkind[OP(scan)] == EXACT)
10846 if (join_exact(pRExC_state,scan,&min,1,val,depth+1))
10850 switch (OP(scan)) {
10856 if( exact == PSEUDO )
10858 else if ( exact != OP(scan) )
10867 SV * const mysv=sv_newmortal();
10868 DEBUG_PARSE_MSG((scan==p ? "tsdy" : ""));
10869 regprop(RExC_rx, mysv, scan);
10870 PerlIO_printf(Perl_debug_log, "~ %s (%d) -> %s\n",
10871 SvPV_nolen_const(mysv),
10872 REG_NODE_NUM(scan),
10873 PL_reg_name[exact]);
10880 SV * const mysv_val=sv_newmortal();
10881 DEBUG_PARSE_MSG("");
10882 regprop(RExC_rx, mysv_val, val);
10883 PerlIO_printf(Perl_debug_log, "~ attach to %s (%"IVdf") offset to %"IVdf"\n",
10884 SvPV_nolen_const(mysv_val),
10885 (IV)REG_NODE_NUM(val),
10889 if (reg_off_by_arg[OP(scan)]) {
10890 ARG_SET(scan, val - scan);
10893 NEXT_OFF(scan) = val - scan;
10901 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
10905 S_regdump_extflags(pTHX_ const char *lead, const U32 flags)
10911 for (bit=0; bit<32; bit++) {
10912 if (flags & (1<<bit)) {
10913 if ((1<<bit) & RXf_PMf_CHARSET) { /* Output separately, below */
10916 if (!set++ && lead)
10917 PerlIO_printf(Perl_debug_log, "%s",lead);
10918 PerlIO_printf(Perl_debug_log, "%s ",PL_reg_extflags_name[bit]);
10921 if ((cs = get_regex_charset(flags)) != REGEX_DEPENDS_CHARSET) {
10922 if (!set++ && lead) {
10923 PerlIO_printf(Perl_debug_log, "%s",lead);
10926 case REGEX_UNICODE_CHARSET:
10927 PerlIO_printf(Perl_debug_log, "UNICODE");
10929 case REGEX_LOCALE_CHARSET:
10930 PerlIO_printf(Perl_debug_log, "LOCALE");
10932 case REGEX_ASCII_RESTRICTED_CHARSET:
10933 PerlIO_printf(Perl_debug_log, "ASCII-RESTRICTED");
10935 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
10936 PerlIO_printf(Perl_debug_log, "ASCII-MORE_RESTRICTED");
10939 PerlIO_printf(Perl_debug_log, "UNKNOWN CHARACTER SET");
10945 PerlIO_printf(Perl_debug_log, "\n");
10947 PerlIO_printf(Perl_debug_log, "%s[none-set]\n",lead);
10953 Perl_regdump(pTHX_ const regexp *r)
10957 SV * const sv = sv_newmortal();
10958 SV *dsv= sv_newmortal();
10959 RXi_GET_DECL(r,ri);
10960 GET_RE_DEBUG_FLAGS_DECL;
10962 PERL_ARGS_ASSERT_REGDUMP;
10964 (void)dumpuntil(r, ri->program, ri->program + 1, NULL, NULL, sv, 0, 0);
10966 /* Header fields of interest. */
10967 if (r->anchored_substr) {
10968 RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->anchored_substr),
10969 RE_SV_DUMPLEN(r->anchored_substr), 30);
10970 PerlIO_printf(Perl_debug_log,
10971 "anchored %s%s at %"IVdf" ",
10972 s, RE_SV_TAIL(r->anchored_substr),
10973 (IV)r->anchored_offset);
10974 } else if (r->anchored_utf8) {
10975 RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->anchored_utf8),
10976 RE_SV_DUMPLEN(r->anchored_utf8), 30);
10977 PerlIO_printf(Perl_debug_log,
10978 "anchored utf8 %s%s at %"IVdf" ",
10979 s, RE_SV_TAIL(r->anchored_utf8),
10980 (IV)r->anchored_offset);
10982 if (r->float_substr) {
10983 RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->float_substr),
10984 RE_SV_DUMPLEN(r->float_substr), 30);
10985 PerlIO_printf(Perl_debug_log,
10986 "floating %s%s at %"IVdf"..%"UVuf" ",
10987 s, RE_SV_TAIL(r->float_substr),
10988 (IV)r->float_min_offset, (UV)r->float_max_offset);
10989 } else if (r->float_utf8) {
10990 RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->float_utf8),
10991 RE_SV_DUMPLEN(r->float_utf8), 30);
10992 PerlIO_printf(Perl_debug_log,
10993 "floating utf8 %s%s at %"IVdf"..%"UVuf" ",
10994 s, RE_SV_TAIL(r->float_utf8),
10995 (IV)r->float_min_offset, (UV)r->float_max_offset);
10997 if (r->check_substr || r->check_utf8)
10998 PerlIO_printf(Perl_debug_log,
11000 (r->check_substr == r->float_substr
11001 && r->check_utf8 == r->float_utf8
11002 ? "(checking floating" : "(checking anchored"));
11003 if (r->extflags & RXf_NOSCAN)
11004 PerlIO_printf(Perl_debug_log, " noscan");
11005 if (r->extflags & RXf_CHECK_ALL)
11006 PerlIO_printf(Perl_debug_log, " isall");
11007 if (r->check_substr || r->check_utf8)
11008 PerlIO_printf(Perl_debug_log, ") ");
11010 if (ri->regstclass) {
11011 regprop(r, sv, ri->regstclass);
11012 PerlIO_printf(Perl_debug_log, "stclass %s ", SvPVX_const(sv));
11014 if (r->extflags & RXf_ANCH) {
11015 PerlIO_printf(Perl_debug_log, "anchored");
11016 if (r->extflags & RXf_ANCH_BOL)
11017 PerlIO_printf(Perl_debug_log, "(BOL)");
11018 if (r->extflags & RXf_ANCH_MBOL)
11019 PerlIO_printf(Perl_debug_log, "(MBOL)");
11020 if (r->extflags & RXf_ANCH_SBOL)
11021 PerlIO_printf(Perl_debug_log, "(SBOL)");
11022 if (r->extflags & RXf_ANCH_GPOS)
11023 PerlIO_printf(Perl_debug_log, "(GPOS)");
11024 PerlIO_putc(Perl_debug_log, ' ');
11026 if (r->extflags & RXf_GPOS_SEEN)
11027 PerlIO_printf(Perl_debug_log, "GPOS:%"UVuf" ", (UV)r->gofs);
11028 if (r->intflags & PREGf_SKIP)
11029 PerlIO_printf(Perl_debug_log, "plus ");
11030 if (r->intflags & PREGf_IMPLICIT)
11031 PerlIO_printf(Perl_debug_log, "implicit ");
11032 PerlIO_printf(Perl_debug_log, "minlen %"IVdf" ", (IV)r->minlen);
11033 if (r->extflags & RXf_EVAL_SEEN)
11034 PerlIO_printf(Perl_debug_log, "with eval ");
11035 PerlIO_printf(Perl_debug_log, "\n");
11036 DEBUG_FLAGS_r(regdump_extflags("r->extflags: ",r->extflags));
11038 PERL_ARGS_ASSERT_REGDUMP;
11039 PERL_UNUSED_CONTEXT;
11040 PERL_UNUSED_ARG(r);
11041 #endif /* DEBUGGING */
11045 - regprop - printable representation of opcode
11047 #define EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags) \
11050 Perl_sv_catpvf(aTHX_ sv,"%s][%s",PL_colors[1],PL_colors[0]); \
11051 if (flags & ANYOF_INVERT) \
11052 /*make sure the invert info is in each */ \
11053 sv_catpvs(sv, "^"); \
11059 Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o)
11064 RXi_GET_DECL(prog,progi);
11065 GET_RE_DEBUG_FLAGS_DECL;
11067 PERL_ARGS_ASSERT_REGPROP;
11071 if (OP(o) > REGNODE_MAX) /* regnode.type is unsigned */
11072 /* It would be nice to FAIL() here, but this may be called from
11073 regexec.c, and it would be hard to supply pRExC_state. */
11074 Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(o), (int)REGNODE_MAX);
11075 sv_catpv(sv, PL_reg_name[OP(o)]); /* Take off const! */
11077 k = PL_regkind[OP(o)];
11080 sv_catpvs(sv, " ");
11081 /* Using is_utf8_string() (via PERL_PV_UNI_DETECT)
11082 * is a crude hack but it may be the best for now since
11083 * we have no flag "this EXACTish node was UTF-8"
11085 pv_pretty(sv, STRING(o), STR_LEN(o), 60, PL_colors[0], PL_colors[1],
11086 PERL_PV_ESCAPE_UNI_DETECT |
11087 PERL_PV_ESCAPE_NONASCII |
11088 PERL_PV_PRETTY_ELLIPSES |
11089 PERL_PV_PRETTY_LTGT |
11090 PERL_PV_PRETTY_NOCLEAR
11092 } else if (k == TRIE) {
11093 /* print the details of the trie in dumpuntil instead, as
11094 * progi->data isn't available here */
11095 const char op = OP(o);
11096 const U32 n = ARG(o);
11097 const reg_ac_data * const ac = IS_TRIE_AC(op) ?
11098 (reg_ac_data *)progi->data->data[n] :
11100 const reg_trie_data * const trie
11101 = (reg_trie_data*)progi->data->data[!IS_TRIE_AC(op) ? n : ac->trie];
11103 Perl_sv_catpvf(aTHX_ sv, "-%s",PL_reg_name[o->flags]);
11104 DEBUG_TRIE_COMPILE_r(
11105 Perl_sv_catpvf(aTHX_ sv,
11106 "<S:%"UVuf"/%"IVdf" W:%"UVuf" L:%"UVuf"/%"UVuf" C:%"UVuf"/%"UVuf">",
11107 (UV)trie->startstate,
11108 (IV)trie->statecount-1, /* -1 because of the unused 0 element */
11109 (UV)trie->wordcount,
11112 (UV)TRIE_CHARCOUNT(trie),
11113 (UV)trie->uniquecharcount
11116 if ( IS_ANYOF_TRIE(op) || trie->bitmap ) {
11118 int rangestart = -1;
11119 U8* bitmap = IS_ANYOF_TRIE(op) ? (U8*)ANYOF_BITMAP(o) : (U8*)TRIE_BITMAP(trie);
11120 sv_catpvs(sv, "[");
11121 for (i = 0; i <= 256; i++) {
11122 if (i < 256 && BITMAP_TEST(bitmap,i)) {
11123 if (rangestart == -1)
11125 } else if (rangestart != -1) {
11126 if (i <= rangestart + 3)
11127 for (; rangestart < i; rangestart++)
11128 put_byte(sv, rangestart);
11130 put_byte(sv, rangestart);
11131 sv_catpvs(sv, "-");
11132 put_byte(sv, i - 1);
11137 sv_catpvs(sv, "]");
11140 } else if (k == CURLY) {
11141 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
11142 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
11143 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
11145 else if (k == WHILEM && o->flags) /* Ordinal/of */
11146 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
11147 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP || OP(o)==ACCEPT) {
11148 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
11149 if ( RXp_PAREN_NAMES(prog) ) {
11150 if ( k != REF || (OP(o) < NREF)) {
11151 AV *list= MUTABLE_AV(progi->data->data[progi->name_list_idx]);
11152 SV **name= av_fetch(list, ARG(o), 0 );
11154 Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
11157 AV *list= MUTABLE_AV(progi->data->data[ progi->name_list_idx ]);
11158 SV *sv_dat= MUTABLE_SV(progi->data->data[ ARG( o ) ]);
11159 I32 *nums=(I32*)SvPVX(sv_dat);
11160 SV **name= av_fetch(list, nums[0], 0 );
11163 for ( n=0; n<SvIVX(sv_dat); n++ ) {
11164 Perl_sv_catpvf(aTHX_ sv, "%s%"IVdf,
11165 (n ? "," : ""), (IV)nums[n]);
11167 Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
11171 } else if (k == GOSUB)
11172 Perl_sv_catpvf(aTHX_ sv, "%d[%+d]", (int)ARG(o),(int)ARG2L(o)); /* Paren and offset */
11173 else if (k == VERB) {
11175 Perl_sv_catpvf(aTHX_ sv, ":%"SVf,
11176 SVfARG((MUTABLE_SV(progi->data->data[ ARG( o ) ]))));
11177 } else if (k == LOGICAL)
11178 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
11179 else if (k == FOLDCHAR)
11180 Perl_sv_catpvf(aTHX_ sv, "[0x%"UVXf"]", PTR2UV(ARG(o)) );
11181 else if (k == ANYOF) {
11182 int i, rangestart = -1;
11183 const U8 flags = ANYOF_FLAGS(o);
11186 /* Should be synchronized with * ANYOF_ #xdefines in regcomp.h */
11187 static const char * const anyofs[] = {
11220 if (flags & ANYOF_LOCALE)
11221 sv_catpvs(sv, "{loc}");
11222 if (flags & ANYOF_LOC_NONBITMAP_FOLD)
11223 sv_catpvs(sv, "{i}");
11224 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
11225 if (flags & ANYOF_INVERT)
11226 sv_catpvs(sv, "^");
11228 /* output what the standard cp 0-255 bitmap matches */
11229 for (i = 0; i <= 256; i++) {
11230 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
11231 if (rangestart == -1)
11233 } else if (rangestart != -1) {
11234 if (i <= rangestart + 3)
11235 for (; rangestart < i; rangestart++)
11236 put_byte(sv, rangestart);
11238 put_byte(sv, rangestart);
11239 sv_catpvs(sv, "-");
11240 put_byte(sv, i - 1);
11247 EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags);
11248 /* output any special charclass tests (used entirely under use locale) */
11249 if (ANYOF_CLASS_TEST_ANY_SET(o))
11250 for (i = 0; i < (int)(sizeof(anyofs)/sizeof(char*)); i++)
11251 if (ANYOF_CLASS_TEST(o,i)) {
11252 sv_catpv(sv, anyofs[i]);
11256 EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags);
11258 if (flags & ANYOF_NON_UTF8_LATIN1_ALL) {
11259 sv_catpvs(sv, "{non-utf8-latin1-all}");
11262 /* output information about the unicode matching */
11263 if (flags & ANYOF_UNICODE_ALL)
11264 sv_catpvs(sv, "{unicode_all}");
11265 else if (ANYOF_NONBITMAP(o))
11266 sv_catpvs(sv, "{unicode}");
11267 if (flags & ANYOF_NONBITMAP_NON_UTF8)
11268 sv_catpvs(sv, "{outside bitmap}");
11270 if (ANYOF_NONBITMAP(o)) {
11272 SV * const sw = regclass_swash(prog, o, FALSE, &lv, 0);
11276 U8 s[UTF8_MAXBYTES_CASE+1];
11278 for (i = 0; i <= 256; i++) { /* just the first 256 */
11279 uvchr_to_utf8(s, i);
11281 if (i < 256 && swash_fetch(sw, s, TRUE)) {
11282 if (rangestart == -1)
11284 } else if (rangestart != -1) {
11285 if (i <= rangestart + 3)
11286 for (; rangestart < i; rangestart++) {
11287 const U8 * const e = uvchr_to_utf8(s,rangestart);
11289 for(p = s; p < e; p++)
11293 const U8 *e = uvchr_to_utf8(s,rangestart);
11295 for (p = s; p < e; p++)
11297 sv_catpvs(sv, "-");
11298 e = uvchr_to_utf8(s, i-1);
11299 for (p = s; p < e; p++)
11306 sv_catpvs(sv, "..."); /* et cetera */
11310 char *s = savesvpv(lv);
11311 char * const origs = s;
11313 while (*s && *s != '\n')
11317 const char * const t = ++s;
11335 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
11337 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
11338 Perl_sv_catpvf(aTHX_ sv, "[%d]", -(o->flags));
11340 PERL_UNUSED_CONTEXT;
11341 PERL_UNUSED_ARG(sv);
11342 PERL_UNUSED_ARG(o);
11343 PERL_UNUSED_ARG(prog);
11344 #endif /* DEBUGGING */
11348 Perl_re_intuit_string(pTHX_ REGEXP * const r)
11349 { /* Assume that RE_INTUIT is set */
11351 struct regexp *const prog = (struct regexp *)SvANY(r);
11352 GET_RE_DEBUG_FLAGS_DECL;
11354 PERL_ARGS_ASSERT_RE_INTUIT_STRING;
11355 PERL_UNUSED_CONTEXT;
11359 const char * const s = SvPV_nolen_const(prog->check_substr
11360 ? prog->check_substr : prog->check_utf8);
11362 if (!PL_colorset) reginitcolors();
11363 PerlIO_printf(Perl_debug_log,
11364 "%sUsing REx %ssubstr:%s \"%s%.60s%s%s\"\n",
11366 prog->check_substr ? "" : "utf8 ",
11367 PL_colors[5],PL_colors[0],
11370 (strlen(s) > 60 ? "..." : ""));
11373 return prog->check_substr ? prog->check_substr : prog->check_utf8;
11379 handles refcounting and freeing the perl core regexp structure. When
11380 it is necessary to actually free the structure the first thing it
11381 does is call the 'free' method of the regexp_engine associated to
11382 the regexp, allowing the handling of the void *pprivate; member
11383 first. (This routine is not overridable by extensions, which is why
11384 the extensions free is called first.)
11386 See regdupe and regdupe_internal if you change anything here.
11388 #ifndef PERL_IN_XSUB_RE
11390 Perl_pregfree(pTHX_ REGEXP *r)
11396 Perl_pregfree2(pTHX_ REGEXP *rx)
11399 struct regexp *const r = (struct regexp *)SvANY(rx);
11400 GET_RE_DEBUG_FLAGS_DECL;
11402 PERL_ARGS_ASSERT_PREGFREE2;
11404 if (r->mother_re) {
11405 ReREFCNT_dec(r->mother_re);
11407 CALLREGFREE_PVT(rx); /* free the private data */
11408 SvREFCNT_dec(RXp_PAREN_NAMES(r));
11411 SvREFCNT_dec(r->anchored_substr);
11412 SvREFCNT_dec(r->anchored_utf8);
11413 SvREFCNT_dec(r->float_substr);
11414 SvREFCNT_dec(r->float_utf8);
11415 Safefree(r->substrs);
11417 RX_MATCH_COPY_FREE(rx);
11418 #ifdef PERL_OLD_COPY_ON_WRITE
11419 SvREFCNT_dec(r->saved_copy);
11426 This is a hacky workaround to the structural issue of match results
11427 being stored in the regexp structure which is in turn stored in
11428 PL_curpm/PL_reg_curpm. The problem is that due to qr// the pattern
11429 could be PL_curpm in multiple contexts, and could require multiple
11430 result sets being associated with the pattern simultaneously, such
11431 as when doing a recursive match with (??{$qr})
11433 The solution is to make a lightweight copy of the regexp structure
11434 when a qr// is returned from the code executed by (??{$qr}) this
11435 lightweight copy doesn't actually own any of its data except for
11436 the starp/end and the actual regexp structure itself.
11442 Perl_reg_temp_copy (pTHX_ REGEXP *ret_x, REGEXP *rx)
11444 struct regexp *ret;
11445 struct regexp *const r = (struct regexp *)SvANY(rx);
11446 register const I32 npar = r->nparens+1;
11448 PERL_ARGS_ASSERT_REG_TEMP_COPY;
11451 ret_x = (REGEXP*) newSV_type(SVt_REGEXP);
11452 ret = (struct regexp *)SvANY(ret_x);
11454 (void)ReREFCNT_inc(rx);
11455 /* We can take advantage of the existing "copied buffer" mechanism in SVs
11456 by pointing directly at the buffer, but flagging that the allocated
11457 space in the copy is zero. As we've just done a struct copy, it's now
11458 a case of zero-ing that, rather than copying the current length. */
11459 SvPV_set(ret_x, RX_WRAPPED(rx));
11460 SvFLAGS(ret_x) |= SvFLAGS(rx) & (SVf_POK|SVp_POK|SVf_UTF8);
11461 memcpy(&(ret->xpv_cur), &(r->xpv_cur),
11462 sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur));
11463 SvLEN_set(ret_x, 0);
11464 SvSTASH_set(ret_x, NULL);
11465 SvMAGIC_set(ret_x, NULL);
11466 Newx(ret->offs, npar, regexp_paren_pair);
11467 Copy(r->offs, ret->offs, npar, regexp_paren_pair);
11469 Newx(ret->substrs, 1, struct reg_substr_data);
11470 StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
11472 SvREFCNT_inc_void(ret->anchored_substr);
11473 SvREFCNT_inc_void(ret->anchored_utf8);
11474 SvREFCNT_inc_void(ret->float_substr);
11475 SvREFCNT_inc_void(ret->float_utf8);
11477 /* check_substr and check_utf8, if non-NULL, point to either their
11478 anchored or float namesakes, and don't hold a second reference. */
11480 RX_MATCH_COPIED_off(ret_x);
11481 #ifdef PERL_OLD_COPY_ON_WRITE
11482 ret->saved_copy = NULL;
11484 ret->mother_re = rx;
11490 /* regfree_internal()
11492 Free the private data in a regexp. This is overloadable by
11493 extensions. Perl takes care of the regexp structure in pregfree(),
11494 this covers the *pprivate pointer which technically perl doesn't
11495 know about, however of course we have to handle the
11496 regexp_internal structure when no extension is in use.
11498 Note this is called before freeing anything in the regexp
11503 Perl_regfree_internal(pTHX_ REGEXP * const rx)
11506 struct regexp *const r = (struct regexp *)SvANY(rx);
11507 RXi_GET_DECL(r,ri);
11508 GET_RE_DEBUG_FLAGS_DECL;
11510 PERL_ARGS_ASSERT_REGFREE_INTERNAL;
11516 SV *dsv= sv_newmortal();
11517 RE_PV_QUOTED_DECL(s, RX_UTF8(rx),
11518 dsv, RX_PRECOMP(rx), RX_PRELEN(rx), 60);
11519 PerlIO_printf(Perl_debug_log,"%sFreeing REx:%s %s\n",
11520 PL_colors[4],PL_colors[5],s);
11523 #ifdef RE_TRACK_PATTERN_OFFSETS
11525 Safefree(ri->u.offsets); /* 20010421 MJD */
11528 int n = ri->data->count;
11529 PAD* new_comppad = NULL;
11534 /* If you add a ->what type here, update the comment in regcomp.h */
11535 switch (ri->data->what[n]) {
11540 SvREFCNT_dec(MUTABLE_SV(ri->data->data[n]));
11543 Safefree(ri->data->data[n]);
11546 new_comppad = MUTABLE_AV(ri->data->data[n]);
11549 if (new_comppad == NULL)
11550 Perl_croak(aTHX_ "panic: pregfree comppad");
11551 PAD_SAVE_LOCAL(old_comppad,
11552 /* Watch out for global destruction's random ordering. */
11553 (SvTYPE(new_comppad) == SVt_PVAV) ? new_comppad : NULL
11556 refcnt = OpREFCNT_dec((OP_4tree*)ri->data->data[n]);
11559 op_free((OP_4tree*)ri->data->data[n]);
11561 PAD_RESTORE_LOCAL(old_comppad);
11562 SvREFCNT_dec(MUTABLE_SV(new_comppad));
11563 new_comppad = NULL;
11568 { /* Aho Corasick add-on structure for a trie node.
11569 Used in stclass optimization only */
11571 reg_ac_data *aho=(reg_ac_data*)ri->data->data[n];
11573 refcount = --aho->refcount;
11576 PerlMemShared_free(aho->states);
11577 PerlMemShared_free(aho->fail);
11578 /* do this last!!!! */
11579 PerlMemShared_free(ri->data->data[n]);
11580 PerlMemShared_free(ri->regstclass);
11586 /* trie structure. */
11588 reg_trie_data *trie=(reg_trie_data*)ri->data->data[n];
11590 refcount = --trie->refcount;
11593 PerlMemShared_free(trie->charmap);
11594 PerlMemShared_free(trie->states);
11595 PerlMemShared_free(trie->trans);
11597 PerlMemShared_free(trie->bitmap);
11599 PerlMemShared_free(trie->jump);
11600 PerlMemShared_free(trie->wordinfo);
11601 /* do this last!!!! */
11602 PerlMemShared_free(ri->data->data[n]);
11607 Perl_croak(aTHX_ "panic: regfree data code '%c'", ri->data->what[n]);
11610 Safefree(ri->data->what);
11611 Safefree(ri->data);
11617 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
11618 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
11619 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
11622 re_dup - duplicate a regexp.
11624 This routine is expected to clone a given regexp structure. It is only
11625 compiled under USE_ITHREADS.
11627 After all of the core data stored in struct regexp is duplicated
11628 the regexp_engine.dupe method is used to copy any private data
11629 stored in the *pprivate pointer. This allows extensions to handle
11630 any duplication it needs to do.
11632 See pregfree() and regfree_internal() if you change anything here.
11634 #if defined(USE_ITHREADS)
11635 #ifndef PERL_IN_XSUB_RE
11637 Perl_re_dup_guts(pTHX_ const REGEXP *sstr, REGEXP *dstr, CLONE_PARAMS *param)
11641 const struct regexp *r = (const struct regexp *)SvANY(sstr);
11642 struct regexp *ret = (struct regexp *)SvANY(dstr);
11644 PERL_ARGS_ASSERT_RE_DUP_GUTS;
11646 npar = r->nparens+1;
11647 Newx(ret->offs, npar, regexp_paren_pair);
11648 Copy(r->offs, ret->offs, npar, regexp_paren_pair);
11650 /* no need to copy these */
11651 Newx(ret->swap, npar, regexp_paren_pair);
11654 if (ret->substrs) {
11655 /* Do it this way to avoid reading from *r after the StructCopy().
11656 That way, if any of the sv_dup_inc()s dislodge *r from the L1
11657 cache, it doesn't matter. */
11658 const bool anchored = r->check_substr
11659 ? r->check_substr == r->anchored_substr
11660 : r->check_utf8 == r->anchored_utf8;
11661 Newx(ret->substrs, 1, struct reg_substr_data);
11662 StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
11664 ret->anchored_substr = sv_dup_inc(ret->anchored_substr, param);
11665 ret->anchored_utf8 = sv_dup_inc(ret->anchored_utf8, param);
11666 ret->float_substr = sv_dup_inc(ret->float_substr, param);
11667 ret->float_utf8 = sv_dup_inc(ret->float_utf8, param);
11669 /* check_substr and check_utf8, if non-NULL, point to either their
11670 anchored or float namesakes, and don't hold a second reference. */
11672 if (ret->check_substr) {
11674 assert(r->check_utf8 == r->anchored_utf8);
11675 ret->check_substr = ret->anchored_substr;
11676 ret->check_utf8 = ret->anchored_utf8;
11678 assert(r->check_substr == r->float_substr);
11679 assert(r->check_utf8 == r->float_utf8);
11680 ret->check_substr = ret->float_substr;
11681 ret->check_utf8 = ret->float_utf8;
11683 } else if (ret->check_utf8) {
11685 ret->check_utf8 = ret->anchored_utf8;
11687 ret->check_utf8 = ret->float_utf8;
11692 RXp_PAREN_NAMES(ret) = hv_dup_inc(RXp_PAREN_NAMES(ret), param);
11695 RXi_SET(ret,CALLREGDUPE_PVT(dstr,param));
11697 if (RX_MATCH_COPIED(dstr))
11698 ret->subbeg = SAVEPVN(ret->subbeg, ret->sublen);
11700 ret->subbeg = NULL;
11701 #ifdef PERL_OLD_COPY_ON_WRITE
11702 ret->saved_copy = NULL;
11705 if (ret->mother_re) {
11706 if (SvPVX_const(dstr) == SvPVX_const(ret->mother_re)) {
11707 /* Our storage points directly to our mother regexp, but that's
11708 1: a buffer in a different thread
11709 2: something we no longer hold a reference on
11710 so we need to copy it locally. */
11711 /* Note we need to sue SvCUR() on our mother_re, because it, in
11712 turn, may well be pointing to its own mother_re. */
11713 SvPV_set(dstr, SAVEPVN(SvPVX_const(ret->mother_re),
11714 SvCUR(ret->mother_re)+1));
11715 SvLEN_set(dstr, SvCUR(ret->mother_re)+1);
11717 ret->mother_re = NULL;
11721 #endif /* PERL_IN_XSUB_RE */
11726 This is the internal complement to regdupe() which is used to copy
11727 the structure pointed to by the *pprivate pointer in the regexp.
11728 This is the core version of the extension overridable cloning hook.
11729 The regexp structure being duplicated will be copied by perl prior
11730 to this and will be provided as the regexp *r argument, however
11731 with the /old/ structures pprivate pointer value. Thus this routine
11732 may override any copying normally done by perl.
11734 It returns a pointer to the new regexp_internal structure.
11738 Perl_regdupe_internal(pTHX_ REGEXP * const rx, CLONE_PARAMS *param)
11741 struct regexp *const r = (struct regexp *)SvANY(rx);
11742 regexp_internal *reti;
11744 RXi_GET_DECL(r,ri);
11746 PERL_ARGS_ASSERT_REGDUPE_INTERNAL;
11748 npar = r->nparens+1;
11751 Newxc(reti, sizeof(regexp_internal) + len*sizeof(regnode), char, regexp_internal);
11752 Copy(ri->program, reti->program, len+1, regnode);
11755 reti->regstclass = NULL;
11758 struct reg_data *d;
11759 const int count = ri->data->count;
11762 Newxc(d, sizeof(struct reg_data) + count*sizeof(void *),
11763 char, struct reg_data);
11764 Newx(d->what, count, U8);
11767 for (i = 0; i < count; i++) {
11768 d->what[i] = ri->data->what[i];
11769 switch (d->what[i]) {
11770 /* legal options are one of: sSfpontTua
11771 see also regcomp.h and pregfree() */
11772 case 'a': /* actually an AV, but the dup function is identical. */
11775 case 'p': /* actually an AV, but the dup function is identical. */
11776 case 'u': /* actually an HV, but the dup function is identical. */
11777 d->data[i] = sv_dup_inc((const SV *)ri->data->data[i], param);
11780 /* This is cheating. */
11781 Newx(d->data[i], 1, struct regnode_charclass_class);
11782 StructCopy(ri->data->data[i], d->data[i],
11783 struct regnode_charclass_class);
11784 reti->regstclass = (regnode*)d->data[i];
11787 /* Compiled op trees are readonly and in shared memory,
11788 and can thus be shared without duplication. */
11790 d->data[i] = (void*)OpREFCNT_inc((OP*)ri->data->data[i]);
11794 /* Trie stclasses are readonly and can thus be shared
11795 * without duplication. We free the stclass in pregfree
11796 * when the corresponding reg_ac_data struct is freed.
11798 reti->regstclass= ri->regstclass;
11802 ((reg_trie_data*)ri->data->data[i])->refcount++;
11806 d->data[i] = ri->data->data[i];
11809 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", ri->data->what[i]);
11818 reti->name_list_idx = ri->name_list_idx;
11820 #ifdef RE_TRACK_PATTERN_OFFSETS
11821 if (ri->u.offsets) {
11822 Newx(reti->u.offsets, 2*len+1, U32);
11823 Copy(ri->u.offsets, reti->u.offsets, 2*len+1, U32);
11826 SetProgLen(reti,len);
11829 return (void*)reti;
11832 #endif /* USE_ITHREADS */
11834 #ifndef PERL_IN_XSUB_RE
11837 - regnext - dig the "next" pointer out of a node
11840 Perl_regnext(pTHX_ register regnode *p)
11843 register I32 offset;
11848 if (OP(p) > REGNODE_MAX) { /* regnode.type is unsigned */
11849 Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(p), (int)REGNODE_MAX);
11852 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
11861 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
11864 STRLEN l1 = strlen(pat1);
11865 STRLEN l2 = strlen(pat2);
11868 const char *message;
11870 PERL_ARGS_ASSERT_RE_CROAK2;
11876 Copy(pat1, buf, l1 , char);
11877 Copy(pat2, buf + l1, l2 , char);
11878 buf[l1 + l2] = '\n';
11879 buf[l1 + l2 + 1] = '\0';
11881 /* ANSI variant takes additional second argument */
11882 va_start(args, pat2);
11886 msv = vmess(buf, &args);
11888 message = SvPV_const(msv,l1);
11891 Copy(message, buf, l1 , char);
11892 buf[l1-1] = '\0'; /* Overwrite \n */
11893 Perl_croak(aTHX_ "%s", buf);
11896 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
11898 #ifndef PERL_IN_XSUB_RE
11900 Perl_save_re_context(pTHX)
11904 struct re_save_state *state;
11906 SAVEVPTR(PL_curcop);
11907 SSGROW(SAVESTACK_ALLOC_FOR_RE_SAVE_STATE + 1);
11909 state = (struct re_save_state *)(PL_savestack + PL_savestack_ix);
11910 PL_savestack_ix += SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
11911 SSPUSHUV(SAVEt_RE_STATE);
11913 Copy(&PL_reg_state, state, 1, struct re_save_state);
11915 PL_reg_start_tmp = 0;
11916 PL_reg_start_tmpl = 0;
11917 PL_reg_oldsaved = NULL;
11918 PL_reg_oldsavedlen = 0;
11919 PL_reg_maxiter = 0;
11920 PL_reg_leftiter = 0;
11921 PL_reg_poscache = NULL;
11922 PL_reg_poscache_size = 0;
11923 #ifdef PERL_OLD_COPY_ON_WRITE
11927 /* Save $1..$n (#18107: UTF-8 s/(\w+)/uc($1)/e); AMS 20021106. */
11929 const REGEXP * const rx = PM_GETRE(PL_curpm);
11932 for (i = 1; i <= RX_NPARENS(rx); i++) {
11933 char digits[TYPE_CHARS(long)];
11934 const STRLEN len = my_snprintf(digits, sizeof(digits), "%lu", (long)i);
11935 GV *const *const gvp
11936 = (GV**)hv_fetch(PL_defstash, digits, len, 0);
11939 GV * const gv = *gvp;
11940 if (SvTYPE(gv) == SVt_PVGV && GvSV(gv))
11950 clear_re(pTHX_ void *r)
11953 ReREFCNT_dec((REGEXP *)r);
11959 S_put_byte(pTHX_ SV *sv, int c)
11961 PERL_ARGS_ASSERT_PUT_BYTE;
11963 /* Our definition of isPRINT() ignores locales, so only bytes that are
11964 not part of UTF-8 are considered printable. I assume that the same
11965 holds for UTF-EBCDIC.
11966 Also, code point 255 is not printable in either (it's E0 in EBCDIC,
11967 which Wikipedia says:
11969 EO, or Eight Ones, is an 8-bit EBCDIC character code represented as all
11970 ones (binary 1111 1111, hexadecimal FF). It is similar, but not
11971 identical, to the ASCII delete (DEL) or rubout control character.
11972 ) So the old condition can be simplified to !isPRINT(c) */
11975 Perl_sv_catpvf(aTHX_ sv, "\\x%02x", c);
11978 Perl_sv_catpvf(aTHX_ sv, "\\x{%x}", c);
11982 const char string = c;
11983 if (c == '-' || c == ']' || c == '\\' || c == '^')
11984 sv_catpvs(sv, "\\");
11985 sv_catpvn(sv, &string, 1);
11990 #define CLEAR_OPTSTART \
11991 if (optstart) STMT_START { \
11992 DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log, " (%"IVdf" nodes)\n", (IV)(node - optstart))); \
11996 #define DUMPUNTIL(b,e) CLEAR_OPTSTART; node=dumpuntil(r,start,(b),(e),last,sv,indent+1,depth+1);
11998 STATIC const regnode *
11999 S_dumpuntil(pTHX_ const regexp *r, const regnode *start, const regnode *node,
12000 const regnode *last, const regnode *plast,
12001 SV* sv, I32 indent, U32 depth)
12004 register U8 op = PSEUDO; /* Arbitrary non-END op. */
12005 register const regnode *next;
12006 const regnode *optstart= NULL;
12008 RXi_GET_DECL(r,ri);
12009 GET_RE_DEBUG_FLAGS_DECL;
12011 PERL_ARGS_ASSERT_DUMPUNTIL;
12013 #ifdef DEBUG_DUMPUNTIL
12014 PerlIO_printf(Perl_debug_log, "--- %d : %d - %d - %d\n",indent,node-start,
12015 last ? last-start : 0,plast ? plast-start : 0);
12018 if (plast && plast < last)
12021 while (PL_regkind[op] != END && (!last || node < last)) {
12022 /* While that wasn't END last time... */
12025 if (op == CLOSE || op == WHILEM)
12027 next = regnext((regnode *)node);
12030 if (OP(node) == OPTIMIZED) {
12031 if (!optstart && RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE))
12038 regprop(r, sv, node);
12039 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
12040 (int)(2*indent + 1), "", SvPVX_const(sv));
12042 if (OP(node) != OPTIMIZED) {
12043 if (next == NULL) /* Next ptr. */
12044 PerlIO_printf(Perl_debug_log, " (0)");
12045 else if (PL_regkind[(U8)op] == BRANCH && PL_regkind[OP(next)] != BRANCH )
12046 PerlIO_printf(Perl_debug_log, " (FAIL)");
12048 PerlIO_printf(Perl_debug_log, " (%"IVdf")", (IV)(next - start));
12049 (void)PerlIO_putc(Perl_debug_log, '\n');
12053 if (PL_regkind[(U8)op] == BRANCHJ) {
12056 register const regnode *nnode = (OP(next) == LONGJMP
12057 ? regnext((regnode *)next)
12059 if (last && nnode > last)
12061 DUMPUNTIL(NEXTOPER(NEXTOPER(node)), nnode);
12064 else if (PL_regkind[(U8)op] == BRANCH) {
12066 DUMPUNTIL(NEXTOPER(node), next);
12068 else if ( PL_regkind[(U8)op] == TRIE ) {
12069 const regnode *this_trie = node;
12070 const char op = OP(node);
12071 const U32 n = ARG(node);
12072 const reg_ac_data * const ac = op>=AHOCORASICK ?
12073 (reg_ac_data *)ri->data->data[n] :
12075 const reg_trie_data * const trie =
12076 (reg_trie_data*)ri->data->data[op<AHOCORASICK ? n : ac->trie];
12078 AV *const trie_words = MUTABLE_AV(ri->data->data[n + TRIE_WORDS_OFFSET]);
12080 const regnode *nextbranch= NULL;
12083 for (word_idx= 0; word_idx < (I32)trie->wordcount; word_idx++) {
12084 SV ** const elem_ptr = av_fetch(trie_words,word_idx,0);
12086 PerlIO_printf(Perl_debug_log, "%*s%s ",
12087 (int)(2*(indent+3)), "",
12088 elem_ptr ? pv_pretty(sv, SvPV_nolen_const(*elem_ptr), SvCUR(*elem_ptr), 60,
12089 PL_colors[0], PL_colors[1],
12090 (SvUTF8(*elem_ptr) ? PERL_PV_ESCAPE_UNI : 0) |
12091 PERL_PV_PRETTY_ELLIPSES |
12092 PERL_PV_PRETTY_LTGT
12097 U16 dist= trie->jump[word_idx+1];
12098 PerlIO_printf(Perl_debug_log, "(%"UVuf")\n",
12099 (UV)((dist ? this_trie + dist : next) - start));
12102 nextbranch= this_trie + trie->jump[0];
12103 DUMPUNTIL(this_trie + dist, nextbranch);
12105 if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
12106 nextbranch= regnext((regnode *)nextbranch);
12108 PerlIO_printf(Perl_debug_log, "\n");
12111 if (last && next > last)
12116 else if ( op == CURLY ) { /* "next" might be very big: optimizer */
12117 DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS,
12118 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1);
12120 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
12122 DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS, next);
12124 else if ( op == PLUS || op == STAR) {
12125 DUMPUNTIL(NEXTOPER(node), NEXTOPER(node) + 1);
12127 else if (PL_regkind[(U8)op] == ANYOF) {
12128 /* arglen 1 + class block */
12129 node += 1 + ((ANYOF_FLAGS(node) & ANYOF_CLASS)
12130 ? ANYOF_CLASS_SKIP : ANYOF_SKIP);
12131 node = NEXTOPER(node);
12133 else if (PL_regkind[(U8)op] == EXACT) {
12134 /* Literal string, where present. */
12135 node += NODE_SZ_STR(node) - 1;
12136 node = NEXTOPER(node);
12139 node = NEXTOPER(node);
12140 node += regarglen[(U8)op];
12142 if (op == CURLYX || op == OPEN)
12146 #ifdef DEBUG_DUMPUNTIL
12147 PerlIO_printf(Perl_debug_log, "--- %d\n", (int)indent);
12152 #endif /* DEBUGGING */
12156 * c-indentation-style: bsd
12157 * c-basic-offset: 4
12158 * indent-tabs-mode: t
12161 * ex: set ts=8 sts=4 sw=4 noet: