Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* regcomp.c |
2 | */ | |
3 | ||
4 | /* | |
4ac71550 TC |
5 | * 'A fair jaw-cracker dwarf-language must be.' --Samwise Gamgee |
6 | * | |
7 | * [p.285 of _The Lord of the Rings_, II/iii: "The Ring Goes South"] | |
a0d0e21e LW |
8 | */ |
9 | ||
61296642 DM |
10 | /* This file contains functions for compiling a regular expression. See |
11 | * also regexec.c which funnily enough, contains functions for executing | |
166f8a29 | 12 | * a regular expression. |
e4a054ea DM |
13 | * |
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. | |
166f8a29 DM |
18 | */ |
19 | ||
a687059c LW |
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! | |
22 | */ | |
23 | ||
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. | |
27 | */ | |
28 | ||
e50aee73 AD |
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. | |
32 | */ | |
33 | ||
b9d5759e | 34 | #ifdef PERL_EXT_RE_BUILD |
54df2634 | 35 | #include "re_top.h" |
b81d288d | 36 | #endif |
56953603 | 37 | |
a687059c | 38 | /* |
e50aee73 | 39 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
40 | * |
41 | * Copyright (c) 1986 by University of Toronto. | |
42 | * Written by Henry Spencer. Not derived from licensed software. | |
43 | * | |
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: | |
47 | * | |
48 | * 1. The author is not responsible for the consequences of use of | |
49 | * this software, no matter how awful, even if they arise | |
50 | * from defects in it. | |
51 | * | |
52 | * 2. The origin of this software must not be misrepresented, either | |
53 | * by explicit claim or by omission. | |
54 | * | |
55 | * 3. Altered versions must be plainly marked as such, and must not | |
56 | * be misrepresented as being the original software. | |
57 | * | |
58 | * | |
59 | **** Alterations to Henry's code are... | |
60 | **** | |
4bb101f2 | 61 | **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
1129b882 NC |
62 | **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
63 | **** by Larry Wall and others | |
a687059c | 64 | **** |
9ef589d8 LW |
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. | |
67 | ||
a687059c LW |
68 | * |
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. | |
72 | */ | |
73 | #include "EXTERN.h" | |
864dbfa3 | 74 | #define PERL_IN_REGCOMP_C |
a687059c | 75 | #include "perl.h" |
d06ea78c | 76 | |
acfe0abc | 77 | #ifndef PERL_IN_XSUB_RE |
d06ea78c GS |
78 | # include "INTERN.h" |
79 | #endif | |
c277df42 IZ |
80 | |
81 | #define REG_COMP_C | |
54df2634 NC |
82 | #ifdef PERL_IN_XSUB_RE |
83 | # include "re_comp.h" | |
84 | #else | |
85 | # include "regcomp.h" | |
86 | #endif | |
a687059c | 87 | |
d4cce5f1 | 88 | #ifdef op |
11343788 | 89 | #undef op |
d4cce5f1 | 90 | #endif /* op */ |
11343788 | 91 | |
fe14fcc3 | 92 | #ifdef MSDOS |
7e4e8c89 | 93 | # if defined(BUGGY_MSC6) |
fe14fcc3 | 94 | /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */ |
7e4e8c89 | 95 | # pragma optimize("a",off) |
fe14fcc3 | 96 | /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/ |
7e4e8c89 NC |
97 | # pragma optimize("w",on ) |
98 | # endif /* BUGGY_MSC6 */ | |
fe14fcc3 LW |
99 | #endif /* MSDOS */ |
100 | ||
a687059c LW |
101 | #ifndef STATIC |
102 | #define STATIC static | |
103 | #endif | |
104 | ||
830247a4 | 105 | typedef struct RExC_state_t { |
e2509266 | 106 | U32 flags; /* are we folding, multilining? */ |
830247a4 | 107 | char *precomp; /* uncompiled string. */ |
288b8c02 | 108 | REGEXP *rx_sv; /* The SV that is the regexp. */ |
f8fc2ecf YO |
109 | regexp *rx; /* perl core regexp structure */ |
110 | regexp_internal *rxi; /* internal data for regexp object pprivate field */ | |
fac92740 | 111 | char *start; /* Start of input for compile */ |
830247a4 IZ |
112 | char *end; /* End of input for compile */ |
113 | char *parse; /* Input-scan pointer. */ | |
114 | I32 whilem_seen; /* number of WHILEM in this expr */ | |
fac92740 | 115 | regnode *emit_start; /* Start of emitted-code area */ |
3b57cd43 | 116 | regnode *emit_bound; /* First regnode outside of the allocated space */ |
ffc61ed2 | 117 | regnode *emit; /* Code-emit pointer; ®dummy = don't = compiling */ |
830247a4 IZ |
118 | I32 naughty; /* How bad is this pattern? */ |
119 | I32 sawback; /* Did we see \1, ...? */ | |
120 | U32 seen; | |
121 | I32 size; /* Code size. */ | |
c74340f9 YO |
122 | I32 npar; /* Capture buffer count, (OPEN). */ |
123 | I32 cpar; /* Capture buffer count, (CLOSE). */ | |
e2e6a0f1 | 124 | I32 nestroot; /* root parens we are in - used by accept */ |
830247a4 IZ |
125 | I32 extralen; |
126 | I32 seen_zerolen; | |
127 | I32 seen_evals; | |
40d049e4 YO |
128 | regnode **open_parens; /* pointers to open parens */ |
129 | regnode **close_parens; /* pointers to close parens */ | |
130 | regnode *opend; /* END node in program */ | |
02daf0ab YO |
131 | I32 utf8; /* whether the pattern is utf8 or not */ |
132 | I32 orig_utf8; /* whether the pattern was originally in utf8 */ | |
133 | /* XXX use this for future optimisation of case | |
134 | * where pattern must be upgraded to utf8. */ | |
81714fb9 | 135 | HV *paren_names; /* Paren names */ |
1f1031fe | 136 | |
40d049e4 YO |
137 | regnode **recurse; /* Recurse regops */ |
138 | I32 recurse_count; /* Number of recurse regops */ | |
830247a4 IZ |
139 | #if ADD_TO_REGEXEC |
140 | char *starttry; /* -Dr: where regtry was called. */ | |
141 | #define RExC_starttry (pRExC_state->starttry) | |
142 | #endif | |
3dab1dad | 143 | #ifdef DEBUGGING |
be8e71aa | 144 | const char *lastparse; |
3dab1dad | 145 | I32 lastnum; |
1f1031fe | 146 | AV *paren_name_list; /* idx -> name */ |
3dab1dad YO |
147 | #define RExC_lastparse (pRExC_state->lastparse) |
148 | #define RExC_lastnum (pRExC_state->lastnum) | |
1f1031fe | 149 | #define RExC_paren_name_list (pRExC_state->paren_name_list) |
3dab1dad | 150 | #endif |
830247a4 IZ |
151 | } RExC_state_t; |
152 | ||
e2509266 | 153 | #define RExC_flags (pRExC_state->flags) |
830247a4 | 154 | #define RExC_precomp (pRExC_state->precomp) |
288b8c02 | 155 | #define RExC_rx_sv (pRExC_state->rx_sv) |
830247a4 | 156 | #define RExC_rx (pRExC_state->rx) |
f8fc2ecf | 157 | #define RExC_rxi (pRExC_state->rxi) |
fac92740 | 158 | #define RExC_start (pRExC_state->start) |
830247a4 IZ |
159 | #define RExC_end (pRExC_state->end) |
160 | #define RExC_parse (pRExC_state->parse) | |
161 | #define RExC_whilem_seen (pRExC_state->whilem_seen) | |
7122b237 YO |
162 | #ifdef RE_TRACK_PATTERN_OFFSETS |
163 | #define RExC_offsets (pRExC_state->rxi->u.offsets) /* I am not like the others */ | |
164 | #endif | |
830247a4 | 165 | #define RExC_emit (pRExC_state->emit) |
fac92740 | 166 | #define RExC_emit_start (pRExC_state->emit_start) |
3b57cd43 | 167 | #define RExC_emit_bound (pRExC_state->emit_bound) |
830247a4 IZ |
168 | #define RExC_naughty (pRExC_state->naughty) |
169 | #define RExC_sawback (pRExC_state->sawback) | |
170 | #define RExC_seen (pRExC_state->seen) | |
171 | #define RExC_size (pRExC_state->size) | |
172 | #define RExC_npar (pRExC_state->npar) | |
e2e6a0f1 | 173 | #define RExC_nestroot (pRExC_state->nestroot) |
830247a4 IZ |
174 | #define RExC_extralen (pRExC_state->extralen) |
175 | #define RExC_seen_zerolen (pRExC_state->seen_zerolen) | |
176 | #define RExC_seen_evals (pRExC_state->seen_evals) | |
1aa99e6b | 177 | #define RExC_utf8 (pRExC_state->utf8) |
02daf0ab | 178 | #define RExC_orig_utf8 (pRExC_state->orig_utf8) |
40d049e4 YO |
179 | #define RExC_open_parens (pRExC_state->open_parens) |
180 | #define RExC_close_parens (pRExC_state->close_parens) | |
181 | #define RExC_opend (pRExC_state->opend) | |
81714fb9 | 182 | #define RExC_paren_names (pRExC_state->paren_names) |
40d049e4 YO |
183 | #define RExC_recurse (pRExC_state->recurse) |
184 | #define RExC_recurse_count (pRExC_state->recurse_count) | |
830247a4 | 185 | |
cde0cee5 | 186 | |
a687059c LW |
187 | #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?') |
188 | #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \ | |
189 | ((*s) == '{' && regcurly(s))) | |
a687059c | 190 | |
35c8bce7 LW |
191 | #ifdef SPSTART |
192 | #undef SPSTART /* dratted cpp namespace... */ | |
193 | #endif | |
a687059c LW |
194 | /* |
195 | * Flags to be passed up and down. | |
196 | */ | |
a687059c | 197 | #define WORST 0 /* Worst case. */ |
a3b492c3 YO |
198 | #define HASWIDTH 0x01 /* Known to match non-null strings. */ |
199 | #define SIMPLE 0x02 /* Simple enough to be STAR/PLUS operand. */ | |
200 | #define SPSTART 0x04 /* Starts with * or +. */ | |
201 | #define TRYAGAIN 0x08 /* Weeded out a declaration. */ | |
202 | #define POSTPONED 0x10 /* (?1),(?&name), (??{...}) or similar */ | |
a687059c | 203 | |
3dab1dad YO |
204 | #define REG_NODE_NUM(x) ((x) ? (int)((x)-RExC_emit_start) : -1) |
205 | ||
07be1b83 YO |
206 | /* whether trie related optimizations are enabled */ |
207 | #if PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION | |
208 | #define TRIE_STUDY_OPT | |
786e8c11 | 209 | #define FULL_TRIE_STUDY |
07be1b83 YO |
210 | #define TRIE_STCLASS |
211 | #endif | |
1de06328 YO |
212 | |
213 | ||
40d049e4 YO |
214 | |
215 | #define PBYTE(u8str,paren) ((U8*)(u8str))[(paren) >> 3] | |
216 | #define PBITVAL(paren) (1 << ((paren) & 7)) | |
217 | #define PAREN_TEST(u8str,paren) ( PBYTE(u8str,paren) & PBITVAL(paren)) | |
218 | #define PAREN_SET(u8str,paren) PBYTE(u8str,paren) |= PBITVAL(paren) | |
219 | #define PAREN_UNSET(u8str,paren) PBYTE(u8str,paren) &= (~PBITVAL(paren)) | |
220 | ||
221 | ||
1de06328 YO |
222 | /* About scan_data_t. |
223 | ||
224 | During optimisation we recurse through the regexp program performing | |
225 | various inplace (keyhole style) optimisations. In addition study_chunk | |
226 | and scan_commit populate this data structure with information about | |
227 | what strings MUST appear in the pattern. We look for the longest | |
228 | string that must appear for at a fixed location, and we look for the | |
229 | longest string that may appear at a floating location. So for instance | |
230 | in the pattern: | |
231 | ||
232 | /FOO[xX]A.*B[xX]BAR/ | |
233 | ||
234 | Both 'FOO' and 'A' are fixed strings. Both 'B' and 'BAR' are floating | |
235 | strings (because they follow a .* construct). study_chunk will identify | |
236 | both FOO and BAR as being the longest fixed and floating strings respectively. | |
237 | ||
238 | The strings can be composites, for instance | |
239 | ||
240 | /(f)(o)(o)/ | |
241 | ||
242 | will result in a composite fixed substring 'foo'. | |
243 | ||
244 | For each string some basic information is maintained: | |
245 | ||
246 | - offset or min_offset | |
247 | This is the position the string must appear at, or not before. | |
248 | It also implicitly (when combined with minlenp) tells us how many | |
249 | character must match before the string we are searching. | |
250 | Likewise when combined with minlenp and the length of the string | |
251 | tells us how many characters must appear after the string we have | |
252 | found. | |
253 | ||
254 | - max_offset | |
255 | Only used for floating strings. This is the rightmost point that | |
256 | the string can appear at. Ifset to I32 max it indicates that the | |
257 | string can occur infinitely far to the right. | |
258 | ||
259 | - minlenp | |
260 | A pointer to the minimum length of the pattern that the string | |
261 | was found inside. This is important as in the case of positive | |
262 | lookahead or positive lookbehind we can have multiple patterns | |
263 | involved. Consider | |
264 | ||
265 | /(?=FOO).*F/ | |
266 | ||
267 | The minimum length of the pattern overall is 3, the minimum length | |
268 | of the lookahead part is 3, but the minimum length of the part that | |
269 | will actually match is 1. So 'FOO's minimum length is 3, but the | |
270 | minimum length for the F is 1. This is important as the minimum length | |
271 | is used to determine offsets in front of and behind the string being | |
272 | looked for. Since strings can be composites this is the length of the | |
273 | pattern at the time it was commited with a scan_commit. Note that | |
274 | the length is calculated by study_chunk, so that the minimum lengths | |
275 | are not known until the full pattern has been compiled, thus the | |
276 | pointer to the value. | |
277 | ||
278 | - lookbehind | |
279 | ||
280 | In the case of lookbehind the string being searched for can be | |
281 | offset past the start point of the final matching string. | |
282 | If this value was just blithely removed from the min_offset it would | |
283 | invalidate some of the calculations for how many chars must match | |
284 | before or after (as they are derived from min_offset and minlen and | |
285 | the length of the string being searched for). | |
286 | When the final pattern is compiled and the data is moved from the | |
287 | scan_data_t structure into the regexp structure the information | |
288 | about lookbehind is factored in, with the information that would | |
289 | have been lost precalculated in the end_shift field for the | |
290 | associated string. | |
291 | ||
292 | The fields pos_min and pos_delta are used to store the minimum offset | |
293 | and the delta to the maximum offset at the current point in the pattern. | |
294 | ||
295 | */ | |
2c2d71f5 JH |
296 | |
297 | typedef struct scan_data_t { | |
1de06328 YO |
298 | /*I32 len_min; unused */ |
299 | /*I32 len_delta; unused */ | |
2c2d71f5 JH |
300 | I32 pos_min; |
301 | I32 pos_delta; | |
302 | SV *last_found; | |
1de06328 | 303 | I32 last_end; /* min value, <0 unless valid. */ |
2c2d71f5 JH |
304 | I32 last_start_min; |
305 | I32 last_start_max; | |
1de06328 YO |
306 | SV **longest; /* Either &l_fixed, or &l_float. */ |
307 | SV *longest_fixed; /* longest fixed string found in pattern */ | |
308 | I32 offset_fixed; /* offset where it starts */ | |
309 | I32 *minlen_fixed; /* pointer to the minlen relevent to the string */ | |
310 | I32 lookbehind_fixed; /* is the position of the string modfied by LB */ | |
311 | SV *longest_float; /* longest floating string found in pattern */ | |
312 | I32 offset_float_min; /* earliest point in string it can appear */ | |
313 | I32 offset_float_max; /* latest point in string it can appear */ | |
314 | I32 *minlen_float; /* pointer to the minlen relevent to the string */ | |
315 | I32 lookbehind_float; /* is the position of the string modified by LB */ | |
2c2d71f5 JH |
316 | I32 flags; |
317 | I32 whilem_c; | |
cb434fcc | 318 | I32 *last_closep; |
653099ff | 319 | struct regnode_charclass_class *start_class; |
2c2d71f5 JH |
320 | } scan_data_t; |
321 | ||
a687059c | 322 | /* |
e50aee73 | 323 | * Forward declarations for pregcomp()'s friends. |
a687059c | 324 | */ |
a0d0e21e | 325 | |
27da23d5 | 326 | static const scan_data_t zero_scan_data = |
1de06328 | 327 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0}; |
c277df42 IZ |
328 | |
329 | #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL) | |
07be1b83 YO |
330 | #define SF_BEFORE_SEOL 0x0001 |
331 | #define SF_BEFORE_MEOL 0x0002 | |
c277df42 IZ |
332 | #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL) |
333 | #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL) | |
334 | ||
09b7f37c CB |
335 | #ifdef NO_UNARY_PLUS |
336 | # define SF_FIX_SHIFT_EOL (0+2) | |
337 | # define SF_FL_SHIFT_EOL (0+4) | |
338 | #else | |
339 | # define SF_FIX_SHIFT_EOL (+2) | |
340 | # define SF_FL_SHIFT_EOL (+4) | |
341 | #endif | |
c277df42 IZ |
342 | |
343 | #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL) | |
344 | #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL) | |
345 | ||
346 | #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL) | |
347 | #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */ | |
07be1b83 YO |
348 | #define SF_IS_INF 0x0040 |
349 | #define SF_HAS_PAR 0x0080 | |
350 | #define SF_IN_PAR 0x0100 | |
351 | #define SF_HAS_EVAL 0x0200 | |
352 | #define SCF_DO_SUBSTR 0x0400 | |
653099ff GS |
353 | #define SCF_DO_STCLASS_AND 0x0800 |
354 | #define SCF_DO_STCLASS_OR 0x1000 | |
355 | #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR) | |
e1901655 | 356 | #define SCF_WHILEM_VISITED_POS 0x2000 |
c277df42 | 357 | |
786e8c11 | 358 | #define SCF_TRIE_RESTUDY 0x4000 /* Do restudy? */ |
e2e6a0f1 | 359 | #define SCF_SEEN_ACCEPT 0x8000 |
07be1b83 | 360 | |
eb160463 | 361 | #define UTF (RExC_utf8 != 0) |
bbe252da YO |
362 | #define LOC ((RExC_flags & RXf_PMf_LOCALE) != 0) |
363 | #define FOLD ((RExC_flags & RXf_PMf_FOLD) != 0) | |
a0ed51b3 | 364 | |
ffc61ed2 | 365 | #define OOB_UNICODE 12345678 |
93733859 | 366 | #define OOB_NAMEDCLASS -1 |
b8c5462f | 367 | |
a0ed51b3 LW |
368 | #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv)) |
369 | #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b) | |
370 | ||
8615cb43 | 371 | |
b45f050a JF |
372 | /* length of regex to show in messages that don't mark a position within */ |
373 | #define RegexLengthToShowInErrorMessages 127 | |
374 | ||
375 | /* | |
376 | * If MARKER[12] are adjusted, be sure to adjust the constants at the top | |
377 | * of t/op/regmesg.t, the tests in t/op/re_tests, and those in | |
378 | * op/pragma/warn/regcomp. | |
379 | */ | |
7253e4e3 RK |
380 | #define MARKER1 "<-- HERE" /* marker as it appears in the description */ |
381 | #define MARKER2 " <-- HERE " /* marker as it appears within the regex */ | |
b81d288d | 382 | |
7253e4e3 | 383 | #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/" |
b45f050a JF |
384 | |
385 | /* | |
386 | * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given | |
387 | * arg. Show regex, up to a maximum length. If it's too long, chop and add | |
388 | * "...". | |
389 | */ | |
58e23c8d | 390 | #define _FAIL(code) STMT_START { \ |
bfed75c6 | 391 | const char *ellipses = ""; \ |
ccb2c380 MP |
392 | IV len = RExC_end - RExC_precomp; \ |
393 | \ | |
394 | if (!SIZE_ONLY) \ | |
288b8c02 | 395 | SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \ |
ccb2c380 MP |
396 | if (len > RegexLengthToShowInErrorMessages) { \ |
397 | /* chop 10 shorter than the max, to ensure meaning of "..." */ \ | |
398 | len = RegexLengthToShowInErrorMessages - 10; \ | |
399 | ellipses = "..."; \ | |
400 | } \ | |
58e23c8d | 401 | code; \ |
ccb2c380 | 402 | } STMT_END |
8615cb43 | 403 | |
58e23c8d YO |
404 | #define FAIL(msg) _FAIL( \ |
405 | Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \ | |
406 | msg, (int)len, RExC_precomp, ellipses)) | |
407 | ||
408 | #define FAIL2(msg,arg) _FAIL( \ | |
409 | Perl_croak(aTHX_ msg " in regex m/%.*s%s/", \ | |
410 | arg, (int)len, RExC_precomp, ellipses)) | |
411 | ||
b45f050a | 412 | /* |
b45f050a JF |
413 | * Simple_vFAIL -- like FAIL, but marks the current location in the scan |
414 | */ | |
ccb2c380 | 415 | #define Simple_vFAIL(m) STMT_START { \ |
a28509cc | 416 | const IV offset = RExC_parse - RExC_precomp; \ |
ccb2c380 MP |
417 | Perl_croak(aTHX_ "%s" REPORT_LOCATION, \ |
418 | m, (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
419 | } STMT_END | |
b45f050a JF |
420 | |
421 | /* | |
422 | * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL() | |
423 | */ | |
ccb2c380 MP |
424 | #define vFAIL(m) STMT_START { \ |
425 | if (!SIZE_ONLY) \ | |
288b8c02 | 426 | SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \ |
ccb2c380 MP |
427 | Simple_vFAIL(m); \ |
428 | } STMT_END | |
b45f050a JF |
429 | |
430 | /* | |
431 | * Like Simple_vFAIL(), but accepts two arguments. | |
432 | */ | |
ccb2c380 | 433 | #define Simple_vFAIL2(m,a1) STMT_START { \ |
a28509cc | 434 | const IV offset = RExC_parse - RExC_precomp; \ |
ccb2c380 MP |
435 | S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \ |
436 | (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
437 | } STMT_END | |
b45f050a JF |
438 | |
439 | /* | |
440 | * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2(). | |
441 | */ | |
ccb2c380 MP |
442 | #define vFAIL2(m,a1) STMT_START { \ |
443 | if (!SIZE_ONLY) \ | |
288b8c02 | 444 | SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \ |
ccb2c380 MP |
445 | Simple_vFAIL2(m, a1); \ |
446 | } STMT_END | |
b45f050a JF |
447 | |
448 | ||
449 | /* | |
450 | * Like Simple_vFAIL(), but accepts three arguments. | |
451 | */ | |
ccb2c380 | 452 | #define Simple_vFAIL3(m, a1, a2) STMT_START { \ |
a28509cc | 453 | const IV offset = RExC_parse - RExC_precomp; \ |
ccb2c380 MP |
454 | S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \ |
455 | (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
456 | } STMT_END | |
b45f050a JF |
457 | |
458 | /* | |
459 | * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3(). | |
460 | */ | |
ccb2c380 MP |
461 | #define vFAIL3(m,a1,a2) STMT_START { \ |
462 | if (!SIZE_ONLY) \ | |
288b8c02 | 463 | SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv); \ |
ccb2c380 MP |
464 | Simple_vFAIL3(m, a1, a2); \ |
465 | } STMT_END | |
b45f050a JF |
466 | |
467 | /* | |
468 | * Like Simple_vFAIL(), but accepts four arguments. | |
469 | */ | |
ccb2c380 | 470 | #define Simple_vFAIL4(m, a1, a2, a3) STMT_START { \ |
a28509cc | 471 | const IV offset = RExC_parse - RExC_precomp; \ |
ccb2c380 MP |
472 | S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, \ |
473 | (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
474 | } STMT_END | |
b45f050a | 475 | |
668c081a | 476 | #define ckWARNreg(loc,m) STMT_START { \ |
a28509cc | 477 | const IV offset = loc - RExC_precomp; \ |
f10f4c18 NC |
478 | Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ |
479 | (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
ccb2c380 MP |
480 | } STMT_END |
481 | ||
668c081a | 482 | #define ckWARNregdep(loc,m) STMT_START { \ |
a28509cc | 483 | const IV offset = loc - RExC_precomp; \ |
d1d15184 | 484 | Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), \ |
f10f4c18 NC |
485 | m REPORT_LOCATION, \ |
486 | (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
ccb2c380 MP |
487 | } STMT_END |
488 | ||
668c081a | 489 | #define ckWARN2reg(loc, m, a1) STMT_START { \ |
a28509cc | 490 | const IV offset = loc - RExC_precomp; \ |
668c081a | 491 | Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ |
ccb2c380 MP |
492 | a1, (int)offset, RExC_precomp, RExC_precomp + offset); \ |
493 | } STMT_END | |
494 | ||
495 | #define vWARN3(loc, m, a1, a2) STMT_START { \ | |
a28509cc | 496 | const IV offset = loc - RExC_precomp; \ |
ccb2c380 MP |
497 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ |
498 | a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
499 | } STMT_END | |
500 | ||
668c081a NC |
501 | #define ckWARN3reg(loc, m, a1, a2) STMT_START { \ |
502 | const IV offset = loc - RExC_precomp; \ | |
503 | Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ | |
504 | a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
505 | } STMT_END | |
506 | ||
ccb2c380 | 507 | #define vWARN4(loc, m, a1, a2, a3) STMT_START { \ |
a28509cc | 508 | const IV offset = loc - RExC_precomp; \ |
ccb2c380 MP |
509 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ |
510 | a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
511 | } STMT_END | |
512 | ||
668c081a NC |
513 | #define ckWARN4reg(loc, m, a1, a2, a3) STMT_START { \ |
514 | const IV offset = loc - RExC_precomp; \ | |
515 | Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ | |
516 | a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
517 | } STMT_END | |
518 | ||
ccb2c380 | 519 | #define vWARN5(loc, m, a1, a2, a3, a4) STMT_START { \ |
a28509cc | 520 | const IV offset = loc - RExC_precomp; \ |
ccb2c380 MP |
521 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \ |
522 | a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \ | |
523 | } STMT_END | |
9d1d55b5 | 524 | |
8615cb43 | 525 | |
cd439c50 | 526 | /* Allow for side effects in s */ |
ccb2c380 MP |
527 | #define REGC(c,s) STMT_START { \ |
528 | if (!SIZE_ONLY) *(s) = (c); else (void)(s); \ | |
529 | } STMT_END | |
cd439c50 | 530 | |
fac92740 MJD |
531 | /* Macros for recording node offsets. 20001227 mjd@plover.com |
532 | * Nodes are numbered 1, 2, 3, 4. Node #n's position is recorded in | |
533 | * element 2*n-1 of the array. Element #2n holds the byte length node #n. | |
534 | * Element 0 holds the number n. | |
07be1b83 | 535 | * Position is 1 indexed. |
fac92740 | 536 | */ |
7122b237 YO |
537 | #ifndef RE_TRACK_PATTERN_OFFSETS |
538 | #define Set_Node_Offset_To_R(node,byte) | |
539 | #define Set_Node_Offset(node,byte) | |
540 | #define Set_Cur_Node_Offset | |
541 | #define Set_Node_Length_To_R(node,len) | |
542 | #define Set_Node_Length(node,len) | |
543 | #define Set_Node_Cur_Length(node) | |
544 | #define Node_Offset(n) | |
545 | #define Node_Length(n) | |
546 | #define Set_Node_Offset_Length(node,offset,len) | |
547 | #define ProgLen(ri) ri->u.proglen | |
548 | #define SetProgLen(ri,x) ri->u.proglen = x | |
549 | #else | |
550 | #define ProgLen(ri) ri->u.offsets[0] | |
551 | #define SetProgLen(ri,x) ri->u.offsets[0] = x | |
ccb2c380 MP |
552 | #define Set_Node_Offset_To_R(node,byte) STMT_START { \ |
553 | if (! SIZE_ONLY) { \ | |
554 | MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n", \ | |
2a49f0f5 | 555 | __LINE__, (int)(node), (int)(byte))); \ |
ccb2c380 | 556 | if((node) < 0) { \ |
551405c4 | 557 | Perl_croak(aTHX_ "value of node is %d in Offset macro", (int)(node)); \ |
ccb2c380 MP |
558 | } else { \ |
559 | RExC_offsets[2*(node)-1] = (byte); \ | |
560 | } \ | |
561 | } \ | |
562 | } STMT_END | |
563 | ||
564 | #define Set_Node_Offset(node,byte) \ | |
565 | Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start) | |
566 | #define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse) | |
567 | ||
568 | #define Set_Node_Length_To_R(node,len) STMT_START { \ | |
569 | if (! SIZE_ONLY) { \ | |
570 | MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n", \ | |
551405c4 | 571 | __LINE__, (int)(node), (int)(len))); \ |
ccb2c380 | 572 | if((node) < 0) { \ |
551405c4 | 573 | Perl_croak(aTHX_ "value of node is %d in Length macro", (int)(node)); \ |
ccb2c380 MP |
574 | } else { \ |
575 | RExC_offsets[2*(node)] = (len); \ | |
576 | } \ | |
577 | } \ | |
578 | } STMT_END | |
579 | ||
580 | #define Set_Node_Length(node,len) \ | |
581 | Set_Node_Length_To_R((node)-RExC_emit_start, len) | |
582 | #define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len) | |
583 | #define Set_Node_Cur_Length(node) \ | |
584 | Set_Node_Length(node, RExC_parse - parse_start) | |
fac92740 MJD |
585 | |
586 | /* Get offsets and lengths */ | |
587 | #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1]) | |
588 | #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)]) | |
589 | ||
07be1b83 YO |
590 | #define Set_Node_Offset_Length(node,offset,len) STMT_START { \ |
591 | Set_Node_Offset_To_R((node)-RExC_emit_start, (offset)); \ | |
592 | Set_Node_Length_To_R((node)-RExC_emit_start, (len)); \ | |
593 | } STMT_END | |
7122b237 | 594 | #endif |
07be1b83 YO |
595 | |
596 | #if PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS | |
597 | #define EXPERIMENTAL_INPLACESCAN | |
7122b237 | 598 | #endif /*RE_TRACK_PATTERN_OFFSETS*/ |
07be1b83 | 599 | |
304ee84b YO |
600 | #define DEBUG_STUDYDATA(str,data,depth) \ |
601 | DEBUG_OPTIMISE_MORE_r(if(data){ \ | |
1de06328 | 602 | PerlIO_printf(Perl_debug_log, \ |
304ee84b YO |
603 | "%*s" str "Pos:%"IVdf"/%"IVdf \ |
604 | " Flags: 0x%"UVXf" Whilem_c: %"IVdf" Lcp: %"IVdf" %s", \ | |
1de06328 YO |
605 | (int)(depth)*2, "", \ |
606 | (IV)((data)->pos_min), \ | |
607 | (IV)((data)->pos_delta), \ | |
304ee84b | 608 | (UV)((data)->flags), \ |
1de06328 | 609 | (IV)((data)->whilem_c), \ |
304ee84b YO |
610 | (IV)((data)->last_closep ? *((data)->last_closep) : -1), \ |
611 | is_inf ? "INF " : "" \ | |
1de06328 YO |
612 | ); \ |
613 | if ((data)->last_found) \ | |
614 | PerlIO_printf(Perl_debug_log, \ | |
615 | "Last:'%s' %"IVdf":%"IVdf"/%"IVdf" %sFixed:'%s' @ %"IVdf \ | |
616 | " %sFloat: '%s' @ %"IVdf"/%"IVdf"", \ | |
617 | SvPVX_const((data)->last_found), \ | |
618 | (IV)((data)->last_end), \ | |
619 | (IV)((data)->last_start_min), \ | |
620 | (IV)((data)->last_start_max), \ | |
621 | ((data)->longest && \ | |
622 | (data)->longest==&((data)->longest_fixed)) ? "*" : "", \ | |
623 | SvPVX_const((data)->longest_fixed), \ | |
624 | (IV)((data)->offset_fixed), \ | |
625 | ((data)->longest && \ | |
626 | (data)->longest==&((data)->longest_float)) ? "*" : "", \ | |
627 | SvPVX_const((data)->longest_float), \ | |
628 | (IV)((data)->offset_float_min), \ | |
629 | (IV)((data)->offset_float_max) \ | |
630 | ); \ | |
631 | PerlIO_printf(Perl_debug_log,"\n"); \ | |
632 | }); | |
633 | ||
acfe0abc | 634 | static void clear_re(pTHX_ void *r); |
4327152a | 635 | |
653099ff | 636 | /* Mark that we cannot extend a found fixed substring at this point. |
786e8c11 | 637 | Update the longest found anchored substring and the longest found |
653099ff GS |
638 | floating substrings if needed. */ |
639 | ||
4327152a | 640 | STATIC void |
304ee84b | 641 | S_scan_commit(pTHX_ const RExC_state_t *pRExC_state, scan_data_t *data, I32 *minlenp, int is_inf) |
c277df42 | 642 | { |
e1ec3a88 AL |
643 | const STRLEN l = CHR_SVLEN(data->last_found); |
644 | const STRLEN old_l = CHR_SVLEN(*data->longest); | |
1de06328 | 645 | GET_RE_DEBUG_FLAGS_DECL; |
b81d288d | 646 | |
7918f24d NC |
647 | PERL_ARGS_ASSERT_SCAN_COMMIT; |
648 | ||
c277df42 | 649 | if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) { |
6b43b216 | 650 | SvSetMagicSV(*data->longest, data->last_found); |
c277df42 IZ |
651 | if (*data->longest == data->longest_fixed) { |
652 | data->offset_fixed = l ? data->last_start_min : data->pos_min; | |
653 | if (data->flags & SF_BEFORE_EOL) | |
b81d288d | 654 | data->flags |
c277df42 IZ |
655 | |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL); |
656 | else | |
657 | data->flags &= ~SF_FIX_BEFORE_EOL; | |
1de06328 YO |
658 | data->minlen_fixed=minlenp; |
659 | data->lookbehind_fixed=0; | |
a0ed51b3 | 660 | } |
304ee84b | 661 | else { /* *data->longest == data->longest_float */ |
c277df42 | 662 | data->offset_float_min = l ? data->last_start_min : data->pos_min; |
b81d288d AB |
663 | data->offset_float_max = (l |
664 | ? data->last_start_max | |
c277df42 | 665 | : data->pos_min + data->pos_delta); |
304ee84b | 666 | if (is_inf || (U32)data->offset_float_max > (U32)I32_MAX) |
9051bda5 | 667 | data->offset_float_max = I32_MAX; |
c277df42 | 668 | if (data->flags & SF_BEFORE_EOL) |
b81d288d | 669 | data->flags |
c277df42 IZ |
670 | |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL); |
671 | else | |
672 | data->flags &= ~SF_FL_BEFORE_EOL; | |
1de06328 YO |
673 | data->minlen_float=minlenp; |
674 | data->lookbehind_float=0; | |
c277df42 IZ |
675 | } |
676 | } | |
677 | SvCUR_set(data->last_found, 0); | |
0eda9292 | 678 | { |
a28509cc | 679 | SV * const sv = data->last_found; |
097eb12c AL |
680 | if (SvUTF8(sv) && SvMAGICAL(sv)) { |
681 | MAGIC * const mg = mg_find(sv, PERL_MAGIC_utf8); | |
682 | if (mg) | |
683 | mg->mg_len = 0; | |
684 | } | |
0eda9292 | 685 | } |
c277df42 IZ |
686 | data->last_end = -1; |
687 | data->flags &= ~SF_BEFORE_EOL; | |
bcdf7404 | 688 | DEBUG_STUDYDATA("commit: ",data,0); |
c277df42 IZ |
689 | } |
690 | ||
653099ff GS |
691 | /* Can match anything (initialization) */ |
692 | STATIC void | |
097eb12c | 693 | S_cl_anything(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl) |
653099ff | 694 | { |
7918f24d NC |
695 | PERL_ARGS_ASSERT_CL_ANYTHING; |
696 | ||
653099ff | 697 | ANYOF_CLASS_ZERO(cl); |
f8bef550 | 698 | ANYOF_BITMAP_SETALL(cl); |
1aa99e6b | 699 | cl->flags = ANYOF_EOS|ANYOF_UNICODE_ALL; |
653099ff GS |
700 | if (LOC) |
701 | cl->flags |= ANYOF_LOCALE; | |
702 | } | |
703 | ||
704 | /* Can match anything (initialization) */ | |
705 | STATIC int | |
5f66b61c | 706 | S_cl_is_anything(const struct regnode_charclass_class *cl) |
653099ff GS |
707 | { |
708 | int value; | |
709 | ||
7918f24d NC |
710 | PERL_ARGS_ASSERT_CL_IS_ANYTHING; |
711 | ||
aaa51d5e | 712 | for (value = 0; value <= ANYOF_MAX; value += 2) |
653099ff GS |
713 | if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1)) |
714 | return 1; | |
1aa99e6b IH |
715 | if (!(cl->flags & ANYOF_UNICODE_ALL)) |
716 | return 0; | |
10edeb5d | 717 | if (!ANYOF_BITMAP_TESTALLSET((const void*)cl)) |
f8bef550 | 718 | return 0; |
653099ff GS |
719 | return 1; |
720 | } | |
721 | ||
722 | /* Can match anything (initialization) */ | |
723 | STATIC void | |
097eb12c | 724 | S_cl_init(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl) |
653099ff | 725 | { |
7918f24d NC |
726 | PERL_ARGS_ASSERT_CL_INIT; |
727 | ||
8ecf7187 | 728 | Zero(cl, 1, struct regnode_charclass_class); |
653099ff | 729 | cl->type = ANYOF; |
830247a4 | 730 | cl_anything(pRExC_state, cl); |
653099ff GS |
731 | } |
732 | ||
733 | STATIC void | |
097eb12c | 734 | S_cl_init_zero(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl) |
653099ff | 735 | { |
7918f24d NC |
736 | PERL_ARGS_ASSERT_CL_INIT_ZERO; |
737 | ||
8ecf7187 | 738 | Zero(cl, 1, struct regnode_charclass_class); |
653099ff | 739 | cl->type = ANYOF; |
830247a4 | 740 | cl_anything(pRExC_state, cl); |
653099ff GS |
741 | if (LOC) |
742 | cl->flags |= ANYOF_LOCALE; | |
743 | } | |
744 | ||
745 | /* 'And' a given class with another one. Can create false positives */ | |
746 | /* We assume that cl is not inverted */ | |
747 | STATIC void | |
5f66b61c | 748 | S_cl_and(struct regnode_charclass_class *cl, |
a28509cc | 749 | const struct regnode_charclass_class *and_with) |
653099ff | 750 | { |
7918f24d | 751 | PERL_ARGS_ASSERT_CL_AND; |
40d049e4 YO |
752 | |
753 | assert(and_with->type == ANYOF); | |
653099ff GS |
754 | if (!(and_with->flags & ANYOF_CLASS) |
755 | && !(cl->flags & ANYOF_CLASS) | |
756 | && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE) | |
757 | && !(and_with->flags & ANYOF_FOLD) | |
758 | && !(cl->flags & ANYOF_FOLD)) { | |
759 | int i; | |
760 | ||
761 | if (and_with->flags & ANYOF_INVERT) | |
762 | for (i = 0; i < ANYOF_BITMAP_SIZE; i++) | |
763 | cl->bitmap[i] &= ~and_with->bitmap[i]; | |
764 | else | |
765 | for (i = 0; i < ANYOF_BITMAP_SIZE; i++) | |
766 | cl->bitmap[i] &= and_with->bitmap[i]; | |
767 | } /* XXXX: logic is complicated otherwise, leave it along for a moment. */ | |
768 | if (!(and_with->flags & ANYOF_EOS)) | |
769 | cl->flags &= ~ANYOF_EOS; | |
1aa99e6b | 770 | |
14ebb1a2 JH |
771 | if (cl->flags & ANYOF_UNICODE_ALL && and_with->flags & ANYOF_UNICODE && |
772 | !(and_with->flags & ANYOF_INVERT)) { | |
1aa99e6b IH |
773 | cl->flags &= ~ANYOF_UNICODE_ALL; |
774 | cl->flags |= ANYOF_UNICODE; | |
775 | ARG_SET(cl, ARG(and_with)); | |
776 | } | |
14ebb1a2 JH |
777 | if (!(and_with->flags & ANYOF_UNICODE_ALL) && |
778 | !(and_with->flags & ANYOF_INVERT)) | |
1aa99e6b | 779 | cl->flags &= ~ANYOF_UNICODE_ALL; |
14ebb1a2 JH |
780 | if (!(and_with->flags & (ANYOF_UNICODE|ANYOF_UNICODE_ALL)) && |
781 | !(and_with->flags & ANYOF_INVERT)) | |
1aa99e6b | 782 | cl->flags &= ~ANYOF_UNICODE; |
653099ff GS |
783 | } |
784 | ||
785 | /* 'OR' a given class with another one. Can create false positives */ | |
786 | /* We assume that cl is not inverted */ | |
787 | STATIC void | |
097eb12c | 788 | S_cl_or(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, const struct regnode_charclass_class *or_with) |
653099ff | 789 | { |
7918f24d NC |
790 | PERL_ARGS_ASSERT_CL_OR; |
791 | ||
653099ff GS |
792 | if (or_with->flags & ANYOF_INVERT) { |
793 | /* We do not use | |
794 | * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2)) | |
795 | * <= (B1 | !B2) | (CL1 | !CL2) | |
796 | * which is wasteful if CL2 is small, but we ignore CL2: | |
797 | * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1 | |
798 | * XXXX Can we handle case-fold? Unclear: | |
799 | * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) = | |
800 | * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i')) | |
801 | */ | |
802 | if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE) | |
803 | && !(or_with->flags & ANYOF_FOLD) | |
804 | && !(cl->flags & ANYOF_FOLD) ) { | |
805 | int i; | |
806 | ||
807 | for (i = 0; i < ANYOF_BITMAP_SIZE; i++) | |
808 | cl->bitmap[i] |= ~or_with->bitmap[i]; | |
809 | } /* XXXX: logic is complicated otherwise */ | |
810 | else { | |
830247a4 | 811 | cl_anything(pRExC_state, cl); |
653099ff GS |
812 | } |
813 | } else { | |
814 | /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */ | |
815 | if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE) | |
b81d288d | 816 | && (!(or_with->flags & ANYOF_FOLD) |
653099ff GS |
817 | || (cl->flags & ANYOF_FOLD)) ) { |
818 | int i; | |
819 | ||
820 | /* OR char bitmap and class bitmap separately */ | |
821 | for (i = 0; i < ANYOF_BITMAP_SIZE; i++) | |
822 | cl->bitmap[i] |= or_with->bitmap[i]; | |
823 | if (or_with->flags & ANYOF_CLASS) { | |
824 | for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++) | |
825 | cl->classflags[i] |= or_with->classflags[i]; | |
826 | cl->flags |= ANYOF_CLASS; | |
827 | } | |
828 | } | |
829 | else { /* XXXX: logic is complicated, leave it along for a moment. */ | |
830247a4 | 830 | cl_anything(pRExC_state, cl); |
653099ff GS |
831 | } |
832 | } | |
833 | if (or_with->flags & ANYOF_EOS) | |
834 | cl->flags |= ANYOF_EOS; | |
1aa99e6b IH |
835 | |
836 | if (cl->flags & ANYOF_UNICODE && or_with->flags & ANYOF_UNICODE && | |
837 | ARG(cl) != ARG(or_with)) { | |
838 | cl->flags |= ANYOF_UNICODE_ALL; | |
839 | cl->flags &= ~ANYOF_UNICODE; | |
840 | } | |
841 | if (or_with->flags & ANYOF_UNICODE_ALL) { | |
842 | cl->flags |= ANYOF_UNICODE_ALL; | |
843 | cl->flags &= ~ANYOF_UNICODE; | |
844 | } | |
653099ff GS |
845 | } |
846 | ||
a3621e74 YO |
847 | #define TRIE_LIST_ITEM(state,idx) (trie->states[state].trans.list)[ idx ] |
848 | #define TRIE_LIST_CUR(state) ( TRIE_LIST_ITEM( state, 0 ).forid ) | |
849 | #define TRIE_LIST_LEN(state) ( TRIE_LIST_ITEM( state, 0 ).newstate ) | |
850 | #define TRIE_LIST_USED(idx) ( trie->states[state].trans.list ? (TRIE_LIST_CUR( idx ) - 1) : 0 ) | |
851 | ||
3dab1dad YO |
852 | |
853 | #ifdef DEBUGGING | |
07be1b83 | 854 | /* |
2b8b4781 NC |
855 | dump_trie(trie,widecharmap,revcharmap) |
856 | dump_trie_interim_list(trie,widecharmap,revcharmap,next_alloc) | |
857 | dump_trie_interim_table(trie,widecharmap,revcharmap,next_alloc) | |
3dab1dad YO |
858 | |
859 | These routines dump out a trie in a somewhat readable format. | |
07be1b83 YO |
860 | The _interim_ variants are used for debugging the interim |
861 | tables that are used to generate the final compressed | |
862 | representation which is what dump_trie expects. | |
863 | ||
3dab1dad YO |
864 | Part of the reason for their existance is to provide a form |
865 | of documentation as to how the different representations function. | |
07be1b83 YO |
866 | |
867 | */ | |
3dab1dad YO |
868 | |
869 | /* | |
3dab1dad YO |
870 | Dumps the final compressed table form of the trie to Perl_debug_log. |
871 | Used for debugging make_trie(). | |
872 | */ | |
873 | ||
874 | STATIC void | |
2b8b4781 NC |
875 | S_dump_trie(pTHX_ const struct _reg_trie_data *trie, HV *widecharmap, |
876 | AV *revcharmap, U32 depth) | |
3dab1dad YO |
877 | { |
878 | U32 state; | |
ab3bbdeb | 879 | SV *sv=sv_newmortal(); |
55eed653 | 880 | int colwidth= widecharmap ? 6 : 4; |
2e64971a | 881 | U16 word; |
3dab1dad YO |
882 | GET_RE_DEBUG_FLAGS_DECL; |
883 | ||
7918f24d | 884 | PERL_ARGS_ASSERT_DUMP_TRIE; |
ab3bbdeb | 885 | |
3dab1dad YO |
886 | PerlIO_printf( Perl_debug_log, "%*sChar : %-6s%-6s%-4s ", |
887 | (int)depth * 2 + 2,"", | |
888 | "Match","Base","Ofs" ); | |
889 | ||
890 | for( state = 0 ; state < trie->uniquecharcount ; state++ ) { | |
2b8b4781 | 891 | SV ** const tmp = av_fetch( revcharmap, state, 0); |
3dab1dad | 892 | if ( tmp ) { |
ab3bbdeb YO |
893 | PerlIO_printf( Perl_debug_log, "%*s", |
894 | colwidth, | |
ddc5bc0f | 895 | pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, |
ab3bbdeb YO |
896 | PL_colors[0], PL_colors[1], |
897 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | | |
898 | PERL_PV_ESCAPE_FIRSTCHAR | |
899 | ) | |
900 | ); | |
3dab1dad YO |
901 | } |
902 | } | |
903 | PerlIO_printf( Perl_debug_log, "\n%*sState|-----------------------", | |
904 | (int)depth * 2 + 2,""); | |
905 | ||
906 | for( state = 0 ; state < trie->uniquecharcount ; state++ ) | |
ab3bbdeb | 907 | PerlIO_printf( Perl_debug_log, "%.*s", colwidth, "--------"); |
3dab1dad YO |
908 | PerlIO_printf( Perl_debug_log, "\n"); |
909 | ||
1e2e3d02 | 910 | for( state = 1 ; state < trie->statecount ; state++ ) { |
be8e71aa | 911 | const U32 base = trie->states[ state ].trans.base; |
3dab1dad YO |
912 | |
913 | PerlIO_printf( Perl_debug_log, "%*s#%4"UVXf"|", (int)depth * 2 + 2,"", (UV)state); | |
914 | ||
915 | if ( trie->states[ state ].wordnum ) { | |
916 | PerlIO_printf( Perl_debug_log, " W%4X", trie->states[ state ].wordnum ); | |
917 | } else { | |
918 | PerlIO_printf( Perl_debug_log, "%6s", "" ); | |
919 | } | |
920 | ||
921 | PerlIO_printf( Perl_debug_log, " @%4"UVXf" ", (UV)base ); | |
922 | ||
923 | if ( base ) { | |
924 | U32 ofs = 0; | |
925 | ||
926 | while( ( base + ofs < trie->uniquecharcount ) || | |
927 | ( base + ofs - trie->uniquecharcount < trie->lasttrans | |
928 | && trie->trans[ base + ofs - trie->uniquecharcount ].check != state)) | |
929 | ofs++; | |
930 | ||
931 | PerlIO_printf( Perl_debug_log, "+%2"UVXf"[ ", (UV)ofs); | |
932 | ||
933 | for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) { | |
934 | if ( ( base + ofs >= trie->uniquecharcount ) && | |
935 | ( base + ofs - trie->uniquecharcount < trie->lasttrans ) && | |
936 | trie->trans[ base + ofs - trie->uniquecharcount ].check == state ) | |
937 | { | |
ab3bbdeb YO |
938 | PerlIO_printf( Perl_debug_log, "%*"UVXf, |
939 | colwidth, | |
3dab1dad YO |
940 | (UV)trie->trans[ base + ofs - trie->uniquecharcount ].next ); |
941 | } else { | |
ab3bbdeb | 942 | PerlIO_printf( Perl_debug_log, "%*s",colwidth," ." ); |
3dab1dad YO |
943 | } |
944 | } | |
945 | ||
946 | PerlIO_printf( Perl_debug_log, "]"); | |
947 | ||
948 | } | |
949 | PerlIO_printf( Perl_debug_log, "\n" ); | |
950 | } | |
2e64971a DM |
951 | PerlIO_printf(Perl_debug_log, "%*sword_info N:(prev,len)=", (int)depth*2, ""); |
952 | for (word=1; word <= trie->wordcount; word++) { | |
953 | PerlIO_printf(Perl_debug_log, " %d:(%d,%d)", | |
954 | (int)word, (int)(trie->wordinfo[word].prev), | |
955 | (int)(trie->wordinfo[word].len)); | |
956 | } | |
957 | PerlIO_printf(Perl_debug_log, "\n" ); | |
3dab1dad YO |
958 | } |
959 | /* | |
3dab1dad YO |
960 | Dumps a fully constructed but uncompressed trie in list form. |
961 | List tries normally only are used for construction when the number of | |
962 | possible chars (trie->uniquecharcount) is very high. | |
963 | Used for debugging make_trie(). | |
964 | */ | |
965 | STATIC void | |
55eed653 | 966 | S_dump_trie_interim_list(pTHX_ const struct _reg_trie_data *trie, |
2b8b4781 NC |
967 | HV *widecharmap, AV *revcharmap, U32 next_alloc, |
968 | U32 depth) | |
3dab1dad YO |
969 | { |
970 | U32 state; | |
ab3bbdeb | 971 | SV *sv=sv_newmortal(); |
55eed653 | 972 | int colwidth= widecharmap ? 6 : 4; |
3dab1dad | 973 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
974 | |
975 | PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_LIST; | |
976 | ||
3dab1dad | 977 | /* print out the table precompression. */ |
ab3bbdeb YO |
978 | PerlIO_printf( Perl_debug_log, "%*sState :Word | Transition Data\n%*s%s", |
979 | (int)depth * 2 + 2,"", (int)depth * 2 + 2,"", | |
980 | "------:-----+-----------------\n" ); | |
3dab1dad YO |
981 | |
982 | for( state=1 ; state < next_alloc ; state ++ ) { | |
983 | U16 charid; | |
984 | ||
ab3bbdeb | 985 | PerlIO_printf( Perl_debug_log, "%*s %4"UVXf" :", |
3dab1dad YO |
986 | (int)depth * 2 + 2,"", (UV)state ); |
987 | if ( ! trie->states[ state ].wordnum ) { | |
988 | PerlIO_printf( Perl_debug_log, "%5s| ",""); | |
989 | } else { | |
990 | PerlIO_printf( Perl_debug_log, "W%4x| ", | |
991 | trie->states[ state ].wordnum | |
992 | ); | |
993 | } | |
994 | for( charid = 1 ; charid <= TRIE_LIST_USED( state ) ; charid++ ) { | |
2b8b4781 | 995 | SV ** const tmp = av_fetch( revcharmap, TRIE_LIST_ITEM(state,charid).forid, 0); |
ab3bbdeb YO |
996 | if ( tmp ) { |
997 | PerlIO_printf( Perl_debug_log, "%*s:%3X=%4"UVXf" | ", | |
998 | colwidth, | |
ddc5bc0f | 999 | pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, |
ab3bbdeb YO |
1000 | PL_colors[0], PL_colors[1], |
1001 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | | |
1002 | PERL_PV_ESCAPE_FIRSTCHAR | |
1003 | ) , | |
1e2e3d02 YO |
1004 | TRIE_LIST_ITEM(state,charid).forid, |
1005 | (UV)TRIE_LIST_ITEM(state,charid).newstate | |
1006 | ); | |
1007 | if (!(charid % 10)) | |
664e119d RGS |
1008 | PerlIO_printf(Perl_debug_log, "\n%*s| ", |
1009 | (int)((depth * 2) + 14), ""); | |
1e2e3d02 | 1010 | } |
ab3bbdeb YO |
1011 | } |
1012 | PerlIO_printf( Perl_debug_log, "\n"); | |
3dab1dad YO |
1013 | } |
1014 | } | |
1015 | ||
1016 | /* | |
3dab1dad YO |
1017 | Dumps a fully constructed but uncompressed trie in table form. |
1018 | This is the normal DFA style state transition table, with a few | |
1019 | twists to facilitate compression later. | |
1020 | Used for debugging make_trie(). | |
1021 | */ | |
1022 | STATIC void | |
55eed653 | 1023 | S_dump_trie_interim_table(pTHX_ const struct _reg_trie_data *trie, |
2b8b4781 NC |
1024 | HV *widecharmap, AV *revcharmap, U32 next_alloc, |
1025 | U32 depth) | |
3dab1dad YO |
1026 | { |
1027 | U32 state; | |
1028 | U16 charid; | |
ab3bbdeb | 1029 | SV *sv=sv_newmortal(); |
55eed653 | 1030 | int colwidth= widecharmap ? 6 : 4; |
3dab1dad | 1031 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
1032 | |
1033 | PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_TABLE; | |
3dab1dad YO |
1034 | |
1035 | /* | |
1036 | print out the table precompression so that we can do a visual check | |
1037 | that they are identical. | |
1038 | */ | |
1039 | ||
1040 | PerlIO_printf( Perl_debug_log, "%*sChar : ",(int)depth * 2 + 2,"" ); | |
1041 | ||
1042 | for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) { | |
2b8b4781 | 1043 | SV ** const tmp = av_fetch( revcharmap, charid, 0); |
3dab1dad | 1044 | if ( tmp ) { |
ab3bbdeb YO |
1045 | PerlIO_printf( Perl_debug_log, "%*s", |
1046 | colwidth, | |
ddc5bc0f | 1047 | pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, |
ab3bbdeb YO |
1048 | PL_colors[0], PL_colors[1], |
1049 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | | |
1050 | PERL_PV_ESCAPE_FIRSTCHAR | |
1051 | ) | |
1052 | ); | |
3dab1dad YO |
1053 | } |
1054 | } | |
1055 | ||
1056 | PerlIO_printf( Perl_debug_log, "\n%*sState+-",(int)depth * 2 + 2,"" ); | |
1057 | ||
1058 | for( charid=0 ; charid < trie->uniquecharcount ; charid++ ) { | |
ab3bbdeb | 1059 | PerlIO_printf( Perl_debug_log, "%.*s", colwidth,"--------"); |
3dab1dad YO |
1060 | } |
1061 | ||
1062 | PerlIO_printf( Perl_debug_log, "\n" ); | |
1063 | ||
1064 | for( state=1 ; state < next_alloc ; state += trie->uniquecharcount ) { | |
1065 | ||
1066 | PerlIO_printf( Perl_debug_log, "%*s%4"UVXf" : ", | |
1067 | (int)depth * 2 + 2,"", | |
1068 | (UV)TRIE_NODENUM( state ) ); | |
1069 | ||
1070 | for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) { | |
ab3bbdeb YO |
1071 | UV v=(UV)SAFE_TRIE_NODENUM( trie->trans[ state + charid ].next ); |
1072 | if (v) | |
1073 | PerlIO_printf( Perl_debug_log, "%*"UVXf, colwidth, v ); | |
1074 | else | |
1075 | PerlIO_printf( Perl_debug_log, "%*s", colwidth, "." ); | |
3dab1dad YO |
1076 | } |
1077 | if ( ! trie->states[ TRIE_NODENUM( state ) ].wordnum ) { | |
1078 | PerlIO_printf( Perl_debug_log, " (%4"UVXf")\n", (UV)trie->trans[ state ].check ); | |
1079 | } else { | |
1080 | PerlIO_printf( Perl_debug_log, " (%4"UVXf") W%4X\n", (UV)trie->trans[ state ].check, | |
1081 | trie->states[ TRIE_NODENUM( state ) ].wordnum ); | |
1082 | } | |
1083 | } | |
07be1b83 | 1084 | } |
3dab1dad YO |
1085 | |
1086 | #endif | |
1087 | ||
2e64971a | 1088 | |
786e8c11 YO |
1089 | /* make_trie(startbranch,first,last,tail,word_count,flags,depth) |
1090 | startbranch: the first branch in the whole branch sequence | |
1091 | first : start branch of sequence of branch-exact nodes. | |
1092 | May be the same as startbranch | |
1093 | last : Thing following the last branch. | |
1094 | May be the same as tail. | |
1095 | tail : item following the branch sequence | |
1096 | count : words in the sequence | |
1097 | flags : currently the OP() type we will be building one of /EXACT(|F|Fl)/ | |
1098 | depth : indent depth | |
3dab1dad | 1099 | |
786e8c11 | 1100 | Inplace optimizes a sequence of 2 or more Branch-Exact nodes into a TRIE node. |
07be1b83 | 1101 | |
786e8c11 YO |
1102 | A trie is an N'ary tree where the branches are determined by digital |
1103 | decomposition of the key. IE, at the root node you look up the 1st character and | |
1104 | follow that branch repeat until you find the end of the branches. Nodes can be | |
1105 | marked as "accepting" meaning they represent a complete word. Eg: | |
07be1b83 | 1106 | |
786e8c11 | 1107 | /he|she|his|hers/ |
72f13be8 | 1108 | |
786e8c11 YO |
1109 | would convert into the following structure. Numbers represent states, letters |
1110 | following numbers represent valid transitions on the letter from that state, if | |
1111 | the number is in square brackets it represents an accepting state, otherwise it | |
1112 | will be in parenthesis. | |
07be1b83 | 1113 | |
786e8c11 YO |
1114 | +-h->+-e->[3]-+-r->(8)-+-s->[9] |
1115 | | | | |
1116 | | (2) | |
1117 | | | | |
1118 | (1) +-i->(6)-+-s->[7] | |
1119 | | | |
1120 | +-s->(3)-+-h->(4)-+-e->[5] | |
07be1b83 | 1121 | |
786e8c11 YO |
1122 | Accept Word Mapping: 3=>1 (he),5=>2 (she), 7=>3 (his), 9=>4 (hers) |
1123 | ||
1124 | This shows that when matching against the string 'hers' we will begin at state 1 | |
1125 | read 'h' and move to state 2, read 'e' and move to state 3 which is accepting, | |
1126 | then read 'r' and go to state 8 followed by 's' which takes us to state 9 which | |
1127 | is also accepting. Thus we know that we can match both 'he' and 'hers' with a | |
1128 | single traverse. We store a mapping from accepting to state to which word was | |
1129 | matched, and then when we have multiple possibilities we try to complete the | |
1130 | rest of the regex in the order in which they occured in the alternation. | |
1131 | ||
1132 | The only prior NFA like behaviour that would be changed by the TRIE support is | |
1133 | the silent ignoring of duplicate alternations which are of the form: | |
1134 | ||
1135 | / (DUPE|DUPE) X? (?{ ... }) Y /x | |
1136 | ||
1137 | Thus EVAL blocks follwing a trie may be called a different number of times with | |
1138 | and without the optimisation. With the optimisations dupes will be silently | |
1139 | ignored. This inconsistant behaviour of EVAL type nodes is well established as | |
1140 | the following demonstrates: | |
1141 | ||
1142 | 'words'=~/(word|word|word)(?{ print $1 })[xyz]/ | |
1143 | ||
1144 | which prints out 'word' three times, but | |
1145 | ||
1146 | 'words'=~/(word|word|word)(?{ print $1 })S/ | |
1147 | ||
1148 | which doesnt print it out at all. This is due to other optimisations kicking in. | |
1149 | ||
1150 | Example of what happens on a structural level: | |
1151 | ||
1152 | The regexp /(ac|ad|ab)+/ will produce the folowing debug output: | |
1153 | ||
1154 | 1: CURLYM[1] {1,32767}(18) | |
1155 | 5: BRANCH(8) | |
1156 | 6: EXACT <ac>(16) | |
1157 | 8: BRANCH(11) | |
1158 | 9: EXACT <ad>(16) | |
1159 | 11: BRANCH(14) | |
1160 | 12: EXACT <ab>(16) | |
1161 | 16: SUCCEED(0) | |
1162 | 17: NOTHING(18) | |
1163 | 18: END(0) | |
1164 | ||
1165 | This would be optimizable with startbranch=5, first=5, last=16, tail=16 | |
1166 | and should turn into: | |
1167 | ||
1168 | 1: CURLYM[1] {1,32767}(18) | |
1169 | 5: TRIE(16) | |
1170 | [Words:3 Chars Stored:6 Unique Chars:4 States:5 NCP:1] | |
1171 | <ac> | |
1172 | <ad> | |
1173 | <ab> | |
1174 | 16: SUCCEED(0) | |
1175 | 17: NOTHING(18) | |
1176 | 18: END(0) | |
1177 | ||
1178 | Cases where tail != last would be like /(?foo|bar)baz/: | |
1179 | ||
1180 | 1: BRANCH(4) | |
1181 | 2: EXACT <foo>(8) | |
1182 | 4: BRANCH(7) | |
1183 | 5: EXACT <bar>(8) | |
1184 | 7: TAIL(8) | |
1185 | 8: EXACT <baz>(10) | |
1186 | 10: END(0) | |
1187 | ||
1188 | which would be optimizable with startbranch=1, first=1, last=7, tail=8 | |
1189 | and would end up looking like: | |
1190 | ||
1191 | 1: TRIE(8) | |
1192 | [Words:2 Chars Stored:6 Unique Chars:5 States:7 NCP:1] | |
1193 | <foo> | |
1194 | <bar> | |
1195 | 7: TAIL(8) | |
1196 | 8: EXACT <baz>(10) | |
1197 | 10: END(0) | |
1198 | ||
1199 | d = uvuni_to_utf8_flags(d, uv, 0); | |
1200 | ||
1201 | is the recommended Unicode-aware way of saying | |
1202 | ||
1203 | *(d++) = uv; | |
1204 | */ | |
1205 | ||
1e2e3d02 | 1206 | #define TRIE_STORE_REVCHAR \ |
786e8c11 | 1207 | STMT_START { \ |
73031816 NC |
1208 | if (UTF) { \ |
1209 | SV *zlopp = newSV(2); \ | |
88c9ea1e CB |
1210 | unsigned char *flrbbbbb = (unsigned char *) SvPVX(zlopp); \ |
1211 | unsigned const char *const kapow = uvuni_to_utf8(flrbbbbb, uvc & 0xFF); \ | |
73031816 NC |
1212 | SvCUR_set(zlopp, kapow - flrbbbbb); \ |
1213 | SvPOK_on(zlopp); \ | |
1214 | SvUTF8_on(zlopp); \ | |
1215 | av_push(revcharmap, zlopp); \ | |
1216 | } else { \ | |
6bdeddd2 | 1217 | char ooooff = (char)uvc; \ |
73031816 NC |
1218 | av_push(revcharmap, newSVpvn(&ooooff, 1)); \ |
1219 | } \ | |
1220 | } STMT_END | |
786e8c11 YO |
1221 | |
1222 | #define TRIE_READ_CHAR STMT_START { \ | |
1223 | wordlen++; \ | |
1224 | if ( UTF ) { \ | |
1225 | if ( folder ) { \ | |
1226 | if ( foldlen > 0 ) { \ | |
1227 | uvc = utf8n_to_uvuni( scan, UTF8_MAXLEN, &len, uniflags ); \ | |
1228 | foldlen -= len; \ | |
1229 | scan += len; \ | |
1230 | len = 0; \ | |
1231 | } else { \ | |
1232 | uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\ | |
1233 | uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \ | |
1234 | foldlen -= UNISKIP( uvc ); \ | |
1235 | scan = foldbuf + UNISKIP( uvc ); \ | |
1236 | } \ | |
1237 | } else { \ | |
1238 | uvc = utf8n_to_uvuni( (const U8*)uc, UTF8_MAXLEN, &len, uniflags);\ | |
1239 | } \ | |
1240 | } else { \ | |
1241 | uvc = (U32)*uc; \ | |
1242 | len = 1; \ | |
1243 | } \ | |
1244 | } STMT_END | |
1245 | ||
1246 | ||
1247 | ||
1248 | #define TRIE_LIST_PUSH(state,fid,ns) STMT_START { \ | |
1249 | if ( TRIE_LIST_CUR( state ) >=TRIE_LIST_LEN( state ) ) { \ | |
f9003953 NC |
1250 | U32 ging = TRIE_LIST_LEN( state ) *= 2; \ |
1251 | Renew( trie->states[ state ].trans.list, ging, reg_trie_trans_le ); \ | |
786e8c11 YO |
1252 | } \ |
1253 | TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).forid = fid; \ | |
1254 | TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).newstate = ns; \ | |
1255 | TRIE_LIST_CUR( state )++; \ | |
1256 | } STMT_END | |
07be1b83 | 1257 | |
786e8c11 YO |
1258 | #define TRIE_LIST_NEW(state) STMT_START { \ |
1259 | Newxz( trie->states[ state ].trans.list, \ | |
1260 | 4, reg_trie_trans_le ); \ | |
1261 | TRIE_LIST_CUR( state ) = 1; \ | |
1262 | TRIE_LIST_LEN( state ) = 4; \ | |
1263 | } STMT_END | |
07be1b83 | 1264 | |
786e8c11 YO |
1265 | #define TRIE_HANDLE_WORD(state) STMT_START { \ |
1266 | U16 dupe= trie->states[ state ].wordnum; \ | |
1267 | regnode * const noper_next = regnext( noper ); \ | |
1268 | \ | |
786e8c11 YO |
1269 | DEBUG_r({ \ |
1270 | /* store the word for dumping */ \ | |
1271 | SV* tmp; \ | |
1272 | if (OP(noper) != NOTHING) \ | |
740cce10 | 1273 | tmp = newSVpvn_utf8(STRING(noper), STR_LEN(noper), UTF); \ |
786e8c11 | 1274 | else \ |
740cce10 | 1275 | tmp = newSVpvn_utf8( "", 0, UTF ); \ |
2b8b4781 | 1276 | av_push( trie_words, tmp ); \ |
786e8c11 YO |
1277 | }); \ |
1278 | \ | |
1279 | curword++; \ | |
2e64971a DM |
1280 | trie->wordinfo[curword].prev = 0; \ |
1281 | trie->wordinfo[curword].len = wordlen; \ | |
1282 | trie->wordinfo[curword].accept = state; \ | |
786e8c11 YO |
1283 | \ |
1284 | if ( noper_next < tail ) { \ | |
1285 | if (!trie->jump) \ | |
c944940b | 1286 | trie->jump = (U16 *) PerlMemShared_calloc( word_count + 1, sizeof(U16) ); \ |
7f69552c | 1287 | trie->jump[curword] = (U16)(noper_next - convert); \ |
786e8c11 YO |
1288 | if (!jumper) \ |
1289 | jumper = noper_next; \ | |
1290 | if (!nextbranch) \ | |
1291 | nextbranch= regnext(cur); \ | |
1292 | } \ | |
1293 | \ | |
1294 | if ( dupe ) { \ | |
2e64971a DM |
1295 | /* It's a dupe. Pre-insert into the wordinfo[].prev */\ |
1296 | /* chain, so that when the bits of chain are later */\ | |
1297 | /* linked together, the dups appear in the chain */\ | |
1298 | trie->wordinfo[curword].prev = trie->wordinfo[dupe].prev; \ | |
1299 | trie->wordinfo[dupe].prev = curword; \ | |
786e8c11 YO |
1300 | } else { \ |
1301 | /* we haven't inserted this word yet. */ \ | |
1302 | trie->states[ state ].wordnum = curword; \ | |
1303 | } \ | |
1304 | } STMT_END | |
07be1b83 | 1305 | |
3dab1dad | 1306 | |
786e8c11 YO |
1307 | #define TRIE_TRANS_STATE(state,base,ucharcount,charid,special) \ |
1308 | ( ( base + charid >= ucharcount \ | |
1309 | && base + charid < ubound \ | |
1310 | && state == trie->trans[ base - ucharcount + charid ].check \ | |
1311 | && trie->trans[ base - ucharcount + charid ].next ) \ | |
1312 | ? trie->trans[ base - ucharcount + charid ].next \ | |
1313 | : ( state==1 ? special : 0 ) \ | |
1314 | ) | |
3dab1dad | 1315 | |
786e8c11 YO |
1316 | #define MADE_TRIE 1 |
1317 | #define MADE_JUMP_TRIE 2 | |
1318 | #define MADE_EXACT_TRIE 4 | |
3dab1dad | 1319 | |
a3621e74 | 1320 | STATIC I32 |
786e8c11 | 1321 | S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *first, regnode *last, regnode *tail, U32 word_count, U32 flags, U32 depth) |
a3621e74 | 1322 | { |
27da23d5 | 1323 | dVAR; |
a3621e74 YO |
1324 | /* first pass, loop through and scan words */ |
1325 | reg_trie_data *trie; | |
55eed653 | 1326 | HV *widecharmap = NULL; |
2b8b4781 | 1327 | AV *revcharmap = newAV(); |
a3621e74 | 1328 | regnode *cur; |
9f7f3913 | 1329 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
a3621e74 YO |
1330 | STRLEN len = 0; |
1331 | UV uvc = 0; | |
1332 | U16 curword = 0; | |
1333 | U32 next_alloc = 0; | |
786e8c11 YO |
1334 | regnode *jumper = NULL; |
1335 | regnode *nextbranch = NULL; | |
7f69552c | 1336 | regnode *convert = NULL; |
2e64971a | 1337 | U32 *prev_states; /* temp array mapping each state to previous one */ |
a3621e74 | 1338 | /* we just use folder as a flag in utf8 */ |
e1ec3a88 | 1339 | const U8 * const folder = ( flags == EXACTF |
a3621e74 YO |
1340 | ? PL_fold |
1341 | : ( flags == EXACTFL | |
1342 | ? PL_fold_locale | |
1343 | : NULL | |
1344 | ) | |
1345 | ); | |
1346 | ||
2b8b4781 NC |
1347 | #ifdef DEBUGGING |
1348 | const U32 data_slot = add_data( pRExC_state, 4, "tuuu" ); | |
1349 | AV *trie_words = NULL; | |
1350 | /* along with revcharmap, this only used during construction but both are | |
1351 | * useful during debugging so we store them in the struct when debugging. | |
8e11feef | 1352 | */ |
2b8b4781 NC |
1353 | #else |
1354 | const U32 data_slot = add_data( pRExC_state, 2, "tu" ); | |
3dab1dad | 1355 | STRLEN trie_charcount=0; |
3dab1dad | 1356 | #endif |
2b8b4781 | 1357 | SV *re_trie_maxbuff; |
a3621e74 | 1358 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
1359 | |
1360 | PERL_ARGS_ASSERT_MAKE_TRIE; | |
72f13be8 YO |
1361 | #ifndef DEBUGGING |
1362 | PERL_UNUSED_ARG(depth); | |
1363 | #endif | |
a3621e74 | 1364 | |
c944940b | 1365 | trie = (reg_trie_data *) PerlMemShared_calloc( 1, sizeof(reg_trie_data) ); |
a3621e74 | 1366 | trie->refcount = 1; |
3dab1dad | 1367 | trie->startstate = 1; |
786e8c11 | 1368 | trie->wordcount = word_count; |
f8fc2ecf | 1369 | RExC_rxi->data->data[ data_slot ] = (void*)trie; |
c944940b | 1370 | trie->charmap = (U16 *) PerlMemShared_calloc( 256, sizeof(U16) ); |
3dab1dad | 1371 | if (!(UTF && folder)) |
c944940b | 1372 | trie->bitmap = (char *) PerlMemShared_calloc( ANYOF_BITMAP_SIZE, 1 ); |
2e64971a DM |
1373 | trie->wordinfo = (reg_trie_wordinfo *) PerlMemShared_calloc( |
1374 | trie->wordcount+1, sizeof(reg_trie_wordinfo)); | |
1375 | ||
a3621e74 | 1376 | DEBUG_r({ |
2b8b4781 | 1377 | trie_words = newAV(); |
a3621e74 | 1378 | }); |
a3621e74 | 1379 | |
0111c4fd | 1380 | re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1); |
a3621e74 | 1381 | if (!SvIOK(re_trie_maxbuff)) { |
0111c4fd | 1382 | sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT); |
a3621e74 | 1383 | } |
3dab1dad YO |
1384 | DEBUG_OPTIMISE_r({ |
1385 | PerlIO_printf( Perl_debug_log, | |
786e8c11 | 1386 | "%*smake_trie start==%d, first==%d, last==%d, tail==%d depth=%d\n", |
3dab1dad YO |
1387 | (int)depth * 2 + 2, "", |
1388 | REG_NODE_NUM(startbranch),REG_NODE_NUM(first), | |
786e8c11 | 1389 | REG_NODE_NUM(last), REG_NODE_NUM(tail), |
85c3142d | 1390 | (int)depth); |
3dab1dad | 1391 | }); |
7f69552c YO |
1392 | |
1393 | /* Find the node we are going to overwrite */ | |
1394 | if ( first == startbranch && OP( last ) != BRANCH ) { | |
1395 | /* whole branch chain */ | |
1396 | convert = first; | |
1397 | } else { | |
1398 | /* branch sub-chain */ | |
1399 | convert = NEXTOPER( first ); | |
1400 | } | |
1401 | ||
a3621e74 YO |
1402 | /* -- First loop and Setup -- |
1403 | ||
1404 | We first traverse the branches and scan each word to determine if it | |
1405 | contains widechars, and how many unique chars there are, this is | |
1406 | important as we have to build a table with at least as many columns as we | |
1407 | have unique chars. | |
1408 | ||
1409 | We use an array of integers to represent the character codes 0..255 | |
38a44b82 | 1410 | (trie->charmap) and we use a an HV* to store Unicode characters. We use the |
a3621e74 YO |
1411 | native representation of the character value as the key and IV's for the |
1412 | coded index. | |
1413 | ||
1414 | *TODO* If we keep track of how many times each character is used we can | |
1415 | remap the columns so that the table compression later on is more | |
1416 | efficient in terms of memory by ensuring most common value is in the | |
1417 | middle and the least common are on the outside. IMO this would be better | |
1418 | than a most to least common mapping as theres a decent chance the most | |
1419 | common letter will share a node with the least common, meaning the node | |
1420 | will not be compressable. With a middle is most common approach the worst | |
1421 | case is when we have the least common nodes twice. | |
1422 | ||
1423 | */ | |
1424 | ||
a3621e74 | 1425 | for ( cur = first ; cur < last ; cur = regnext( cur ) ) { |
c445ea15 | 1426 | regnode * const noper = NEXTOPER( cur ); |
e1ec3a88 | 1427 | const U8 *uc = (U8*)STRING( noper ); |
a28509cc | 1428 | const U8 * const e = uc + STR_LEN( noper ); |
a3621e74 YO |
1429 | STRLEN foldlen = 0; |
1430 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; | |
2af232bd | 1431 | const U8 *scan = (U8*)NULL; |
07be1b83 | 1432 | U32 wordlen = 0; /* required init */ |
02daf0ab YO |
1433 | STRLEN chars = 0; |
1434 | bool set_bit = trie->bitmap ? 1 : 0; /*store the first char in the bitmap?*/ | |
a3621e74 | 1435 | |
3dab1dad YO |
1436 | if (OP(noper) == NOTHING) { |
1437 | trie->minlen= 0; | |
1438 | continue; | |
1439 | } | |
02daf0ab YO |
1440 | if ( set_bit ) /* bitmap only alloced when !(UTF&&Folding) */ |
1441 | TRIE_BITMAP_SET(trie,*uc); /* store the raw first byte | |
1442 | regardless of encoding */ | |
1443 | ||
a3621e74 | 1444 | for ( ; uc < e ; uc += len ) { |
3dab1dad | 1445 | TRIE_CHARCOUNT(trie)++; |
a3621e74 | 1446 | TRIE_READ_CHAR; |
3dab1dad | 1447 | chars++; |
a3621e74 YO |
1448 | if ( uvc < 256 ) { |
1449 | if ( !trie->charmap[ uvc ] ) { | |
1450 | trie->charmap[ uvc ]=( ++trie->uniquecharcount ); | |
1451 | if ( folder ) | |
1452 | trie->charmap[ folder[ uvc ] ] = trie->charmap[ uvc ]; | |
3dab1dad | 1453 | TRIE_STORE_REVCHAR; |
a3621e74 | 1454 | } |
02daf0ab YO |
1455 | if ( set_bit ) { |
1456 | /* store the codepoint in the bitmap, and if its ascii | |
1457 | also store its folded equivelent. */ | |
1458 | TRIE_BITMAP_SET(trie,uvc); | |
0921ee73 T |
1459 | |
1460 | /* store the folded codepoint */ | |
1461 | if ( folder ) TRIE_BITMAP_SET(trie,folder[ uvc ]); | |
1462 | ||
1463 | if ( !UTF ) { | |
1464 | /* store first byte of utf8 representation of | |
1465 | codepoints in the 127 < uvc < 256 range */ | |
1466 | if (127 < uvc && uvc < 192) { | |
1467 | TRIE_BITMAP_SET(trie,194); | |
1468 | } else if (191 < uvc ) { | |
1469 | TRIE_BITMAP_SET(trie,195); | |
1470 | /* && uvc < 256 -- we know uvc is < 256 already */ | |
1471 | } | |
1472 | } | |
02daf0ab YO |
1473 | set_bit = 0; /* We've done our bit :-) */ |
1474 | } | |
a3621e74 YO |
1475 | } else { |
1476 | SV** svpp; | |
55eed653 NC |
1477 | if ( !widecharmap ) |
1478 | widecharmap = newHV(); | |
a3621e74 | 1479 | |
55eed653 | 1480 | svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 1 ); |
a3621e74 YO |
1481 | |
1482 | if ( !svpp ) | |
e4584336 | 1483 | Perl_croak( aTHX_ "error creating/fetching widecharmap entry for 0x%"UVXf, uvc ); |
a3621e74 YO |
1484 | |
1485 | if ( !SvTRUE( *svpp ) ) { | |
1486 | sv_setiv( *svpp, ++trie->uniquecharcount ); | |
3dab1dad | 1487 | TRIE_STORE_REVCHAR; |
a3621e74 YO |
1488 | } |
1489 | } | |
1490 | } | |
3dab1dad YO |
1491 | if( cur == first ) { |
1492 | trie->minlen=chars; | |
1493 | trie->maxlen=chars; | |
1494 | } else if (chars < trie->minlen) { | |
1495 | trie->minlen=chars; | |
1496 | } else if (chars > trie->maxlen) { | |
1497 | trie->maxlen=chars; | |
1498 | } | |
1499 | ||
a3621e74 YO |
1500 | } /* end first pass */ |
1501 | DEBUG_TRIE_COMPILE_r( | |
3dab1dad YO |
1502 | PerlIO_printf( Perl_debug_log, "%*sTRIE(%s): W:%d C:%d Uq:%d Min:%d Max:%d\n", |
1503 | (int)depth * 2 + 2,"", | |
55eed653 | 1504 | ( widecharmap ? "UTF8" : "NATIVE" ), (int)word_count, |
be8e71aa YO |
1505 | (int)TRIE_CHARCOUNT(trie), trie->uniquecharcount, |
1506 | (int)trie->minlen, (int)trie->maxlen ) | |
a3621e74 | 1507 | ); |
a3621e74 YO |
1508 | |
1509 | /* | |
1510 | We now know what we are dealing with in terms of unique chars and | |
1511 | string sizes so we can calculate how much memory a naive | |
0111c4fd RGS |
1512 | representation using a flat table will take. If it's over a reasonable |
1513 | limit (as specified by ${^RE_TRIE_MAXBUF}) we use a more memory | |
a3621e74 YO |
1514 | conservative but potentially much slower representation using an array |
1515 | of lists. | |
1516 | ||
1517 | At the end we convert both representations into the same compressed | |
1518 | form that will be used in regexec.c for matching with. The latter | |
1519 | is a form that cannot be used to construct with but has memory | |
1520 | properties similar to the list form and access properties similar | |
1521 | to the table form making it both suitable for fast searches and | |
1522 | small enough that its feasable to store for the duration of a program. | |
1523 | ||
1524 | See the comment in the code where the compressed table is produced | |
1525 | inplace from the flat tabe representation for an explanation of how | |
1526 | the compression works. | |
1527 | ||
1528 | */ | |
1529 | ||
1530 | ||
2e64971a DM |
1531 | Newx(prev_states, TRIE_CHARCOUNT(trie) + 2, U32); |
1532 | prev_states[1] = 0; | |
1533 | ||
3dab1dad | 1534 | if ( (IV)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1) > SvIV(re_trie_maxbuff) ) { |
a3621e74 YO |
1535 | /* |
1536 | Second Pass -- Array Of Lists Representation | |
1537 | ||
1538 | Each state will be represented by a list of charid:state records | |
1539 | (reg_trie_trans_le) the first such element holds the CUR and LEN | |
1540 | points of the allocated array. (See defines above). | |
1541 | ||
1542 | We build the initial structure using the lists, and then convert | |
1543 | it into the compressed table form which allows faster lookups | |
1544 | (but cant be modified once converted). | |
a3621e74 YO |
1545 | */ |
1546 | ||
a3621e74 YO |
1547 | STRLEN transcount = 1; |
1548 | ||
1e2e3d02 YO |
1549 | DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log, |
1550 | "%*sCompiling trie using list compiler\n", | |
1551 | (int)depth * 2 + 2, "")); | |
446bd890 | 1552 | |
c944940b JH |
1553 | trie->states = (reg_trie_state *) |
1554 | PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2, | |
1555 | sizeof(reg_trie_state) ); | |
a3621e74 YO |
1556 | TRIE_LIST_NEW(1); |
1557 | next_alloc = 2; | |
1558 | ||
1559 | for ( cur = first ; cur < last ; cur = regnext( cur ) ) { | |
1560 | ||
c445ea15 AL |
1561 | regnode * const noper = NEXTOPER( cur ); |
1562 | U8 *uc = (U8*)STRING( noper ); | |
1563 | const U8 * const e = uc + STR_LEN( noper ); | |
1564 | U32 state = 1; /* required init */ | |
1565 | U16 charid = 0; /* sanity init */ | |
1566 | U8 *scan = (U8*)NULL; /* sanity init */ | |
1567 | STRLEN foldlen = 0; /* required init */ | |
07be1b83 | 1568 | U32 wordlen = 0; /* required init */ |
c445ea15 AL |
1569 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
1570 | ||
3dab1dad | 1571 | if (OP(noper) != NOTHING) { |
786e8c11 | 1572 | for ( ; uc < e ; uc += len ) { |
c445ea15 | 1573 | |
786e8c11 | 1574 | TRIE_READ_CHAR; |
c445ea15 | 1575 | |
786e8c11 YO |
1576 | if ( uvc < 256 ) { |
1577 | charid = trie->charmap[ uvc ]; | |
c445ea15 | 1578 | } else { |
55eed653 | 1579 | SV** const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0); |
786e8c11 YO |
1580 | if ( !svpp ) { |
1581 | charid = 0; | |
1582 | } else { | |
1583 | charid=(U16)SvIV( *svpp ); | |
1584 | } | |
c445ea15 | 1585 | } |
786e8c11 YO |
1586 | /* charid is now 0 if we dont know the char read, or nonzero if we do */ |
1587 | if ( charid ) { | |
a3621e74 | 1588 | |
786e8c11 YO |
1589 | U16 check; |
1590 | U32 newstate = 0; | |
a3621e74 | 1591 | |
786e8c11 YO |
1592 | charid--; |
1593 | if ( !trie->states[ state ].trans.list ) { | |
1594 | TRIE_LIST_NEW( state ); | |
c445ea15 | 1595 | } |
786e8c11 YO |
1596 | for ( check = 1; check <= TRIE_LIST_USED( state ); check++ ) { |
1597 | if ( TRIE_LIST_ITEM( state, check ).forid == charid ) { | |
1598 | newstate = TRIE_LIST_ITEM( state, check ).newstate; | |
1599 | break; | |
1600 | } | |
1601 | } | |
1602 | if ( ! newstate ) { | |
1603 | newstate = next_alloc++; | |
2e64971a | 1604 | prev_states[newstate] = state; |
786e8c11 YO |
1605 | TRIE_LIST_PUSH( state, charid, newstate ); |
1606 | transcount++; | |
1607 | } | |
1608 | state = newstate; | |
1609 | } else { | |
1610 | Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc ); | |
c445ea15 | 1611 | } |
a28509cc | 1612 | } |
c445ea15 | 1613 | } |
3dab1dad | 1614 | TRIE_HANDLE_WORD(state); |
a3621e74 YO |
1615 | |
1616 | } /* end second pass */ | |
1617 | ||
1e2e3d02 YO |
1618 | /* next alloc is the NEXT state to be allocated */ |
1619 | trie->statecount = next_alloc; | |
c944940b JH |
1620 | trie->states = (reg_trie_state *) |
1621 | PerlMemShared_realloc( trie->states, | |
1622 | next_alloc | |
1623 | * sizeof(reg_trie_state) ); | |
a3621e74 | 1624 | |
3dab1dad | 1625 | /* and now dump it out before we compress it */ |
2b8b4781 NC |
1626 | DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_list(trie, widecharmap, |
1627 | revcharmap, next_alloc, | |
1628 | depth+1) | |
1e2e3d02 | 1629 | ); |
a3621e74 | 1630 | |
c944940b JH |
1631 | trie->trans = (reg_trie_trans *) |
1632 | PerlMemShared_calloc( transcount, sizeof(reg_trie_trans) ); | |
a3621e74 YO |
1633 | { |
1634 | U32 state; | |
a3621e74 YO |
1635 | U32 tp = 0; |
1636 | U32 zp = 0; | |
1637 | ||
1638 | ||
1639 | for( state=1 ; state < next_alloc ; state ++ ) { | |
1640 | U32 base=0; | |
1641 | ||
1642 | /* | |
1643 | DEBUG_TRIE_COMPILE_MORE_r( | |
1644 | PerlIO_printf( Perl_debug_log, "tp: %d zp: %d ",tp,zp) | |
1645 | ); | |
1646 | */ | |
1647 | ||
1648 | if (trie->states[state].trans.list) { | |
1649 | U16 minid=TRIE_LIST_ITEM( state, 1).forid; | |
1650 | U16 maxid=minid; | |
a28509cc | 1651 | U16 idx; |
a3621e74 YO |
1652 | |
1653 | for( idx = 2 ; idx <= TRIE_LIST_USED( state ) ; idx++ ) { | |
c445ea15 AL |
1654 | const U16 forid = TRIE_LIST_ITEM( state, idx).forid; |
1655 | if ( forid < minid ) { | |
1656 | minid=forid; | |
1657 | } else if ( forid > maxid ) { | |
1658 | maxid=forid; | |
1659 | } | |
a3621e74 YO |
1660 | } |
1661 | if ( transcount < tp + maxid - minid + 1) { | |
1662 | transcount *= 2; | |
c944940b JH |
1663 | trie->trans = (reg_trie_trans *) |
1664 | PerlMemShared_realloc( trie->trans, | |
446bd890 NC |
1665 | transcount |
1666 | * sizeof(reg_trie_trans) ); | |
a3621e74 YO |
1667 | Zero( trie->trans + (transcount / 2), transcount / 2 , reg_trie_trans ); |
1668 | } | |
1669 | base = trie->uniquecharcount + tp - minid; | |
1670 | if ( maxid == minid ) { | |
1671 | U32 set = 0; | |
1672 | for ( ; zp < tp ; zp++ ) { | |
1673 | if ( ! trie->trans[ zp ].next ) { | |
1674 | base = trie->uniquecharcount + zp - minid; | |
1675 | trie->trans[ zp ].next = TRIE_LIST_ITEM( state, 1).newstate; | |
1676 | trie->trans[ zp ].check = state; | |
1677 | set = 1; | |
1678 | break; | |
1679 | } | |
1680 | } | |
1681 | if ( !set ) { | |
1682 | trie->trans[ tp ].next = TRIE_LIST_ITEM( state, 1).newstate; | |
1683 | trie->trans[ tp ].check = state; | |
1684 | tp++; | |
1685 | zp = tp; | |
1686 | } | |
1687 | } else { | |
1688 | for ( idx=1; idx <= TRIE_LIST_USED( state ) ; idx++ ) { | |
c445ea15 | 1689 | const U32 tid = base - trie->uniquecharcount + TRIE_LIST_ITEM( state, idx ).forid; |
a3621e74 YO |
1690 | trie->trans[ tid ].next = TRIE_LIST_ITEM( state, idx ).newstate; |
1691 | trie->trans[ tid ].check = state; | |
1692 | } | |
1693 | tp += ( maxid - minid + 1 ); | |
1694 | } | |
1695 | Safefree(trie->states[ state ].trans.list); | |
1696 | } | |
1697 | /* | |
1698 | DEBUG_TRIE_COMPILE_MORE_r( | |
1699 | PerlIO_printf( Perl_debug_log, " base: %d\n",base); | |
1700 | ); | |
1701 | */ | |
1702 | trie->states[ state ].trans.base=base; | |
1703 | } | |
cc601c31 | 1704 | trie->lasttrans = tp + 1; |
a3621e74 YO |
1705 | } |
1706 | } else { | |
1707 | /* | |
1708 | Second Pass -- Flat Table Representation. | |
1709 | ||
1710 | we dont use the 0 slot of either trans[] or states[] so we add 1 to each. | |
1711 | We know that we will need Charcount+1 trans at most to store the data | |
1712 | (one row per char at worst case) So we preallocate both structures | |
1713 | assuming worst case. | |
1714 | ||
1715 | We then construct the trie using only the .next slots of the entry | |
1716 | structs. | |
1717 | ||
1718 | We use the .check field of the first entry of the node temporarily to | |
1719 | make compression both faster and easier by keeping track of how many non | |
1720 | zero fields are in the node. | |
1721 | ||
1722 | Since trans are numbered from 1 any 0 pointer in the table is a FAIL | |
1723 | transition. | |
1724 | ||
1725 | There are two terms at use here: state as a TRIE_NODEIDX() which is a | |
1726 | number representing the first entry of the node, and state as a | |
1727 | TRIE_NODENUM() which is the trans number. state 1 is TRIE_NODEIDX(1) and | |
1728 | TRIE_NODENUM(1), state 2 is TRIE_NODEIDX(2) and TRIE_NODENUM(3) if there | |
1729 | are 2 entrys per node. eg: | |
1730 | ||
1731 | A B A B | |
1732 | 1. 2 4 1. 3 7 | |
1733 | 2. 0 3 3. 0 5 | |
1734 | 3. 0 0 5. 0 0 | |
1735 | 4. 0 0 7. 0 0 | |
1736 | ||
1737 | The table is internally in the right hand, idx form. However as we also | |
1738 | have to deal with the states array which is indexed by nodenum we have to | |
1739 | use TRIE_NODENUM() to convert. | |
1740 | ||
1741 | */ | |
1e2e3d02 YO |
1742 | DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log, |
1743 | "%*sCompiling trie using table compiler\n", | |
1744 | (int)depth * 2 + 2, "")); | |
3dab1dad | 1745 | |
c944940b JH |
1746 | trie->trans = (reg_trie_trans *) |
1747 | PerlMemShared_calloc( ( TRIE_CHARCOUNT(trie) + 1 ) | |
1748 | * trie->uniquecharcount + 1, | |
1749 | sizeof(reg_trie_trans) ); | |
1750 | trie->states = (reg_trie_state *) | |
1751 | PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2, | |
1752 | sizeof(reg_trie_state) ); | |
a3621e74 YO |
1753 | next_alloc = trie->uniquecharcount + 1; |
1754 | ||
3dab1dad | 1755 | |
a3621e74 YO |
1756 | for ( cur = first ; cur < last ; cur = regnext( cur ) ) { |
1757 | ||
c445ea15 | 1758 | regnode * const noper = NEXTOPER( cur ); |
a28509cc AL |
1759 | const U8 *uc = (U8*)STRING( noper ); |
1760 | const U8 * const e = uc + STR_LEN( noper ); | |
a3621e74 YO |
1761 | |
1762 | U32 state = 1; /* required init */ | |
1763 | ||
1764 | U16 charid = 0; /* sanity init */ | |
1765 | U32 accept_state = 0; /* sanity init */ | |
1766 | U8 *scan = (U8*)NULL; /* sanity init */ | |
1767 | ||
1768 | STRLEN foldlen = 0; /* required init */ | |
07be1b83 | 1769 | U32 wordlen = 0; /* required init */ |
a3621e74 YO |
1770 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
1771 | ||
3dab1dad | 1772 | if ( OP(noper) != NOTHING ) { |
786e8c11 | 1773 | for ( ; uc < e ; uc += len ) { |
a3621e74 | 1774 | |
786e8c11 | 1775 | TRIE_READ_CHAR; |
a3621e74 | 1776 | |
786e8c11 YO |
1777 | if ( uvc < 256 ) { |
1778 | charid = trie->charmap[ uvc ]; | |
1779 | } else { | |
55eed653 | 1780 | SV* const * const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0); |
786e8c11 | 1781 | charid = svpp ? (U16)SvIV(*svpp) : 0; |
a3621e74 | 1782 | } |
786e8c11 YO |
1783 | if ( charid ) { |
1784 | charid--; | |
1785 | if ( !trie->trans[ state + charid ].next ) { | |
1786 | trie->trans[ state + charid ].next = next_alloc; | |
1787 | trie->trans[ state ].check++; | |
2e64971a DM |
1788 | prev_states[TRIE_NODENUM(next_alloc)] |
1789 | = TRIE_NODENUM(state); | |
786e8c11 YO |
1790 | next_alloc += trie->uniquecharcount; |
1791 | } | |
1792 | state = trie->trans[ state + charid ].next; | |
1793 | } else { | |
1794 | Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc ); | |
1795 | } | |
1796 | /* charid is now 0 if we dont know the char read, or nonzero if we do */ | |
a3621e74 | 1797 | } |
a3621e74 | 1798 | } |
3dab1dad YO |
1799 | accept_state = TRIE_NODENUM( state ); |
1800 | TRIE_HANDLE_WORD(accept_state); | |
a3621e74 YO |
1801 | |
1802 | } /* end second pass */ | |
1803 | ||
3dab1dad | 1804 | /* and now dump it out before we compress it */ |
2b8b4781 NC |
1805 | DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_table(trie, widecharmap, |
1806 | revcharmap, | |
1807 | next_alloc, depth+1)); | |
a3621e74 | 1808 | |
a3621e74 YO |
1809 | { |
1810 | /* | |
1811 | * Inplace compress the table.* | |
1812 | ||
1813 | For sparse data sets the table constructed by the trie algorithm will | |
1814 | be mostly 0/FAIL transitions or to put it another way mostly empty. | |
1815 | (Note that leaf nodes will not contain any transitions.) | |
1816 | ||
1817 | This algorithm compresses the tables by eliminating most such | |
1818 | transitions, at the cost of a modest bit of extra work during lookup: | |
1819 | ||
1820 | - Each states[] entry contains a .base field which indicates the | |
1821 | index in the state[] array wheres its transition data is stored. | |
1822 | ||
1823 | - If .base is 0 there are no valid transitions from that node. | |
1824 | ||
1825 | - If .base is nonzero then charid is added to it to find an entry in | |
1826 | the trans array. | |
1827 | ||
1828 | -If trans[states[state].base+charid].check!=state then the | |
1829 | transition is taken to be a 0/Fail transition. Thus if there are fail | |
1830 | transitions at the front of the node then the .base offset will point | |
1831 | somewhere inside the previous nodes data (or maybe even into a node | |
1832 | even earlier), but the .check field determines if the transition is | |
1833 | valid. | |
1834 | ||
786e8c11 | 1835 | XXX - wrong maybe? |
a3621e74 YO |
1836 | The following process inplace converts the table to the compressed |
1837 | table: We first do not compress the root node 1,and mark its all its | |
1838 | .check pointers as 1 and set its .base pointer as 1 as well. This | |
1839 | allows to do a DFA construction from the compressed table later, and | |
1840 | ensures that any .base pointers we calculate later are greater than | |
1841 | 0. | |
1842 | ||
1843 | - We set 'pos' to indicate the first entry of the second node. | |
1844 | ||
1845 | - We then iterate over the columns of the node, finding the first and | |
1846 | last used entry at l and m. We then copy l..m into pos..(pos+m-l), | |
1847 | and set the .check pointers accordingly, and advance pos | |
1848 | appropriately and repreat for the next node. Note that when we copy | |
1849 | the next pointers we have to convert them from the original | |
1850 | NODEIDX form to NODENUM form as the former is not valid post | |
1851 | compression. | |
1852 | ||
1853 | - If a node has no transitions used we mark its base as 0 and do not | |
1854 | advance the pos pointer. | |
1855 | ||
1856 | - If a node only has one transition we use a second pointer into the | |
1857 | structure to fill in allocated fail transitions from other states. | |
1858 | This pointer is independent of the main pointer and scans forward | |
1859 | looking for null transitions that are allocated to a state. When it | |
1860 | finds one it writes the single transition into the "hole". If the | |
786e8c11 | 1861 | pointer doesnt find one the single transition is appended as normal. |
a3621e74 YO |
1862 | |
1863 | - Once compressed we can Renew/realloc the structures to release the | |
1864 | excess space. | |
1865 | ||
1866 | See "Table-Compression Methods" in sec 3.9 of the Red Dragon, | |
1867 | specifically Fig 3.47 and the associated pseudocode. | |
1868 | ||
1869 | demq | |
1870 | */ | |
a3b680e6 | 1871 | const U32 laststate = TRIE_NODENUM( next_alloc ); |
a28509cc | 1872 | U32 state, charid; |
a3621e74 | 1873 | U32 pos = 0, zp=0; |
1e2e3d02 | 1874 | trie->statecount = laststate; |
a3621e74 YO |
1875 | |
1876 | for ( state = 1 ; state < laststate ; state++ ) { | |
1877 | U8 flag = 0; | |
a28509cc AL |
1878 | const U32 stateidx = TRIE_NODEIDX( state ); |
1879 | const U32 o_used = trie->trans[ stateidx ].check; | |
1880 | U32 used = trie->trans[ stateidx ].check; | |
a3621e74 YO |
1881 | trie->trans[ stateidx ].check = 0; |
1882 | ||
1883 | for ( charid = 0 ; used && charid < trie->uniquecharcount ; charid++ ) { | |
1884 | if ( flag || trie->trans[ stateidx + charid ].next ) { | |
1885 | if ( trie->trans[ stateidx + charid ].next ) { | |
1886 | if (o_used == 1) { | |
1887 | for ( ; zp < pos ; zp++ ) { | |
1888 | if ( ! trie->trans[ zp ].next ) { | |
1889 | break; | |
1890 | } | |
1891 | } | |
1892 | trie->states[ state ].trans.base = zp + trie->uniquecharcount - charid ; | |
1893 | trie->trans[ zp ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next ); | |
1894 | trie->trans[ zp ].check = state; | |
1895 | if ( ++zp > pos ) pos = zp; | |
1896 | break; | |
1897 | } | |
1898 | used--; | |
1899 | } | |
1900 | if ( !flag ) { | |
1901 | flag = 1; | |
1902 | trie->states[ state ].trans.base = pos + trie->uniquecharcount - charid ; | |
1903 | } | |
1904 | trie->trans[ pos ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next ); | |
1905 | trie->trans[ pos ].check = state; | |
1906 | pos++; | |
1907 | } | |
1908 | } | |
1909 | } | |
cc601c31 | 1910 | trie->lasttrans = pos + 1; |
c944940b JH |
1911 | trie->states = (reg_trie_state *) |
1912 | PerlMemShared_realloc( trie->states, laststate | |
1913 | * sizeof(reg_trie_state) ); | |
a3621e74 | 1914 | DEBUG_TRIE_COMPILE_MORE_r( |
e4584336 | 1915 | PerlIO_printf( Perl_debug_log, |
3dab1dad YO |
1916 | "%*sAlloc: %d Orig: %"IVdf" elements, Final:%"IVdf". Savings of %%%5.2f\n", |
1917 | (int)depth * 2 + 2,"", | |
1918 | (int)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1 ), | |
5d7488b2 AL |
1919 | (IV)next_alloc, |
1920 | (IV)pos, | |
a3621e74 YO |
1921 | ( ( next_alloc - pos ) * 100 ) / (double)next_alloc ); |
1922 | ); | |
1923 | ||
1924 | } /* end table compress */ | |
1925 | } | |
1e2e3d02 YO |
1926 | DEBUG_TRIE_COMPILE_MORE_r( |
1927 | PerlIO_printf(Perl_debug_log, "%*sStatecount:%"UVxf" Lasttrans:%"UVxf"\n", | |
1928 | (int)depth * 2 + 2, "", | |
1929 | (UV)trie->statecount, | |
1930 | (UV)trie->lasttrans) | |
1931 | ); | |
cc601c31 | 1932 | /* resize the trans array to remove unused space */ |
c944940b JH |
1933 | trie->trans = (reg_trie_trans *) |
1934 | PerlMemShared_realloc( trie->trans, trie->lasttrans | |
1935 | * sizeof(reg_trie_trans) ); | |
a3621e74 | 1936 | |
3dab1dad | 1937 | { /* Modify the program and insert the new TRIE node*/ |
3dab1dad YO |
1938 | U8 nodetype =(U8)(flags & 0xFF); |
1939 | char *str=NULL; | |
786e8c11 | 1940 | |
07be1b83 | 1941 | #ifdef DEBUGGING |
e62cc96a | 1942 | regnode *optimize = NULL; |
7122b237 YO |
1943 | #ifdef RE_TRACK_PATTERN_OFFSETS |
1944 | ||
b57a0404 JH |
1945 | U32 mjd_offset = 0; |
1946 | U32 mjd_nodelen = 0; | |
7122b237 YO |
1947 | #endif /* RE_TRACK_PATTERN_OFFSETS */ |
1948 | #endif /* DEBUGGING */ | |
a3621e74 | 1949 | /* |
3dab1dad YO |
1950 | This means we convert either the first branch or the first Exact, |
1951 | depending on whether the thing following (in 'last') is a branch | |
1952 | or not and whther first is the startbranch (ie is it a sub part of | |
1953 | the alternation or is it the whole thing.) | |
1954 | Assuming its a sub part we conver the EXACT otherwise we convert | |
1955 | the whole branch sequence, including the first. | |
a3621e74 | 1956 | */ |
3dab1dad | 1957 | /* Find the node we are going to overwrite */ |
7f69552c | 1958 | if ( first != startbranch || OP( last ) == BRANCH ) { |
07be1b83 | 1959 | /* branch sub-chain */ |
3dab1dad | 1960 | NEXT_OFF( first ) = (U16)(last - first); |
7122b237 | 1961 | #ifdef RE_TRACK_PATTERN_OFFSETS |
07be1b83 YO |
1962 | DEBUG_r({ |
1963 | mjd_offset= Node_Offset((convert)); | |
1964 | mjd_nodelen= Node_Length((convert)); | |
1965 | }); | |
7122b237 | 1966 | #endif |
7f69552c | 1967 | /* whole branch chain */ |
7122b237 YO |
1968 | } |
1969 | #ifdef RE_TRACK_PATTERN_OFFSETS | |
1970 | else { | |
7f69552c YO |
1971 | DEBUG_r({ |
1972 | const regnode *nop = NEXTOPER( convert ); | |
1973 | mjd_offset= Node_Offset((nop)); | |
1974 | mjd_nodelen= Node_Length((nop)); | |
1975 | }); | |
07be1b83 YO |
1976 | } |
1977 | DEBUG_OPTIMISE_r( | |
1978 | PerlIO_printf(Perl_debug_log, "%*sMJD offset:%"UVuf" MJD length:%"UVuf"\n", | |
1979 | (int)depth * 2 + 2, "", | |
786e8c11 | 1980 | (UV)mjd_offset, (UV)mjd_nodelen) |
07be1b83 | 1981 | ); |
7122b237 | 1982 | #endif |
3dab1dad YO |
1983 | /* But first we check to see if there is a common prefix we can |
1984 | split out as an EXACT and put in front of the TRIE node. */ | |
1985 | trie->startstate= 1; | |
55eed653 | 1986 | if ( trie->bitmap && !widecharmap && !trie->jump ) { |
3dab1dad | 1987 | U32 state; |
1e2e3d02 | 1988 | for ( state = 1 ; state < trie->statecount-1 ; state++ ) { |
a3621e74 | 1989 | U32 ofs = 0; |
8e11feef RGS |
1990 | I32 idx = -1; |
1991 | U32 count = 0; | |
1992 | const U32 base = trie->states[ state ].trans.base; | |
a3621e74 | 1993 | |
3dab1dad | 1994 | if ( trie->states[state].wordnum ) |
8e11feef | 1995 | count = 1; |
a3621e74 | 1996 | |
8e11feef | 1997 | for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) { |
cc601c31 YO |
1998 | if ( ( base + ofs >= trie->uniquecharcount ) && |
1999 | ( base + ofs - trie->uniquecharcount < trie->lasttrans ) && | |
a3621e74 YO |
2000 | trie->trans[ base + ofs - trie->uniquecharcount ].check == state ) |
2001 | { | |
3dab1dad | 2002 | if ( ++count > 1 ) { |
2b8b4781 | 2003 | SV **tmp = av_fetch( revcharmap, ofs, 0); |
07be1b83 | 2004 | const U8 *ch = (U8*)SvPV_nolen_const( *tmp ); |
8e11feef | 2005 | if ( state == 1 ) break; |
3dab1dad YO |
2006 | if ( count == 2 ) { |
2007 | Zero(trie->bitmap, ANYOF_BITMAP_SIZE, char); | |
2008 | DEBUG_OPTIMISE_r( | |
8e11feef RGS |
2009 | PerlIO_printf(Perl_debug_log, |
2010 | "%*sNew Start State=%"UVuf" Class: [", | |
2011 | (int)depth * 2 + 2, "", | |
786e8c11 | 2012 | (UV)state)); |
be8e71aa | 2013 | if (idx >= 0) { |
2b8b4781 | 2014 | SV ** const tmp = av_fetch( revcharmap, idx, 0); |
be8e71aa | 2015 | const U8 * const ch = (U8*)SvPV_nolen_const( *tmp ); |
8e11feef | 2016 | |
3dab1dad | 2017 | TRIE_BITMAP_SET(trie,*ch); |
8e11feef RGS |
2018 | if ( folder ) |
2019 | TRIE_BITMAP_SET(trie, folder[ *ch ]); | |
3dab1dad | 2020 | DEBUG_OPTIMISE_r( |
f1f66076 | 2021 | PerlIO_printf(Perl_debug_log, "%s", (char*)ch) |
3dab1dad | 2022 | ); |
8e11feef RGS |
2023 | } |
2024 | } | |
2025 | TRIE_BITMAP_SET(trie,*ch); | |
2026 | if ( folder ) | |
2027 | TRIE_BITMAP_SET(trie,folder[ *ch ]); | |
2028 | DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"%s", ch)); | |
2029 | } | |
2030 | idx = ofs; | |
2031 | } | |
3dab1dad YO |
2032 | } |
2033 | if ( count == 1 ) { | |
2b8b4781 | 2034 | SV **tmp = av_fetch( revcharmap, idx, 0); |
c490c714 YO |
2035 | STRLEN len; |
2036 | char *ch = SvPV( *tmp, len ); | |
de734bd5 A |
2037 | DEBUG_OPTIMISE_r({ |
2038 | SV *sv=sv_newmortal(); | |
8e11feef RGS |
2039 | PerlIO_printf( Perl_debug_log, |
2040 | "%*sPrefix State: %"UVuf" Idx:%"UVuf" Char='%s'\n", | |
2041 | (int)depth * 2 + 2, "", | |
de734bd5 A |
2042 | (UV)state, (UV)idx, |
2043 | pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 6, | |
2044 | PL_colors[0], PL_colors[1], | |
2045 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | | |
2046 | PERL_PV_ESCAPE_FIRSTCHAR | |
2047 | ) | |
2048 | ); | |
2049 | }); | |
3dab1dad YO |
2050 | if ( state==1 ) { |
2051 | OP( convert ) = nodetype; | |
2052 | str=STRING(convert); | |
2053 | STR_LEN(convert)=0; | |
2054 | } | |
c490c714 YO |
2055 | STR_LEN(convert) += len; |
2056 | while (len--) | |
de734bd5 | 2057 | *str++ = *ch++; |
8e11feef | 2058 | } else { |
f9049ba1 | 2059 | #ifdef DEBUGGING |
8e11feef RGS |
2060 | if (state>1) |
2061 | DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"]\n")); | |
f9049ba1 | 2062 | #endif |
8e11feef RGS |
2063 | break; |
2064 | } | |
2065 | } | |
2e64971a | 2066 | trie->prefixlen = (state-1); |
3dab1dad | 2067 | if (str) { |
8e11feef | 2068 | regnode *n = convert+NODE_SZ_STR(convert); |
07be1b83 | 2069 | NEXT_OFF(convert) = NODE_SZ_STR(convert); |
8e11feef | 2070 | trie->startstate = state; |
07be1b83 YO |
2071 | trie->minlen -= (state - 1); |
2072 | trie->maxlen -= (state - 1); | |
33809eae JH |
2073 | #ifdef DEBUGGING |
2074 | /* At least the UNICOS C compiler choked on this | |
2075 | * being argument to DEBUG_r(), so let's just have | |
2076 | * it right here. */ | |
2077 | if ( | |
2078 | #ifdef PERL_EXT_RE_BUILD | |
2079 | 1 | |
2080 | #else | |
2081 | DEBUG_r_TEST | |
2082 | #endif | |
2083 | ) { | |
2084 | regnode *fix = convert; | |
2085 | U32 word = trie->wordcount; | |
2086 | mjd_nodelen++; | |
2087 | Set_Node_Offset_Length(convert, mjd_offset, state - 1); | |
2088 | while( ++fix < n ) { | |
2089 | Set_Node_Offset_Length(fix, 0, 0); | |
2090 | } | |
2091 | while (word--) { | |
2092 | SV ** const tmp = av_fetch( trie_words, word, 0 ); | |
2093 | if (tmp) { | |
2094 | if ( STR_LEN(convert) <= SvCUR(*tmp) ) | |
2095 | sv_chop(*tmp, SvPV_nolen(*tmp) + STR_LEN(convert)); | |
2096 | else | |
2097 | sv_chop(*tmp, SvPV_nolen(*tmp) + SvCUR(*tmp)); | |
2098 | } | |
2099 | } | |
2100 | } | |
2101 | #endif | |
8e11feef RGS |
2102 | if (trie->maxlen) { |
2103 | convert = n; | |
2104 | } else { | |
3dab1dad | 2105 | NEXT_OFF(convert) = (U16)(tail - convert); |
a5ca303d | 2106 | DEBUG_r(optimize= n); |
3dab1dad YO |
2107 | } |
2108 | } | |
2109 | } | |
a5ca303d YO |
2110 | if (!jumper) |
2111 | jumper = last; | |
3dab1dad | 2112 | if ( trie->maxlen ) { |
8e11feef RGS |
2113 | NEXT_OFF( convert ) = (U16)(tail - convert); |
2114 | ARG_SET( convert, data_slot ); | |
786e8c11 YO |
2115 | /* Store the offset to the first unabsorbed branch in |
2116 | jump[0], which is otherwise unused by the jump logic. | |
2117 | We use this when dumping a trie and during optimisation. */ | |
2118 | if (trie->jump) | |
7f69552c | 2119 | trie->jump[0] = (U16)(nextbranch - convert); |
a5ca303d | 2120 | |
786e8c11 YO |
2121 | /* XXXX */ |
2122 | if ( !trie->states[trie->startstate].wordnum && trie->bitmap && | |
1de06328 | 2123 | ( (char *)jumper - (char *)convert) >= (int)sizeof(struct regnode_charclass) ) |
786e8c11 YO |
2124 | { |
2125 | OP( convert ) = TRIEC; | |
2126 | Copy(trie->bitmap, ((struct regnode_charclass *)convert)->bitmap, ANYOF_BITMAP_SIZE, char); | |
446bd890 | 2127 | PerlMemShared_free(trie->bitmap); |
786e8c11 YO |
2128 | trie->bitmap= NULL; |
2129 | } else | |
2130 | OP( convert ) = TRIE; | |
a3621e74 | 2131 | |
3dab1dad YO |
2132 | /* store the type in the flags */ |
2133 | convert->flags = nodetype; | |
a5ca303d YO |
2134 | DEBUG_r({ |
2135 | optimize = convert | |
2136 | + NODE_STEP_REGNODE | |
2137 | + regarglen[ OP( convert ) ]; | |
2138 | }); | |
2139 | /* XXX We really should free up the resource in trie now, | |
2140 | as we won't use them - (which resources?) dmq */ | |
3dab1dad | 2141 | } |
a3621e74 | 2142 | /* needed for dumping*/ |
e62cc96a | 2143 | DEBUG_r(if (optimize) { |
07be1b83 | 2144 | regnode *opt = convert; |
bcdf7404 | 2145 | |
e62cc96a | 2146 | while ( ++opt < optimize) { |
07be1b83 YO |
2147 | Set_Node_Offset_Length(opt,0,0); |
2148 | } | |
786e8c11 YO |
2149 | /* |
2150 | Try to clean up some of the debris left after the | |
2151 | optimisation. | |
a3621e74 | 2152 | */ |
786e8c11 | 2153 | while( optimize < jumper ) { |
07be1b83 | 2154 | mjd_nodelen += Node_Length((optimize)); |
a3621e74 | 2155 | OP( optimize ) = OPTIMIZED; |
07be1b83 | 2156 | Set_Node_Offset_Length(optimize,0,0); |
a3621e74 YO |
2157 | optimize++; |
2158 | } | |
07be1b83 | 2159 | Set_Node_Offset_Length(convert,mjd_offset,mjd_nodelen); |
a3621e74 YO |
2160 | }); |
2161 | } /* end node insert */ | |
2e64971a DM |
2162 | |
2163 | /* Finish populating the prev field of the wordinfo array. Walk back | |
2164 | * from each accept state until we find another accept state, and if | |
2165 | * so, point the first word's .prev field at the second word. If the | |
2166 | * second already has a .prev field set, stop now. This will be the | |
2167 | * case either if we've already processed that word's accept state, | |
2168 | * or that that state had multiple words, and the overspill words | |
2169 | * were already linked up earlier. | |
2170 | */ | |
2171 | { | |
2172 | U16 word; | |
2173 | U32 state; | |
2174 | U16 prev; | |
2175 | ||
2176 | for (word=1; word <= trie->wordcount; word++) { | |
2177 | prev = 0; | |
2178 | if (trie->wordinfo[word].prev) | |
2179 | continue; | |
2180 | state = trie->wordinfo[word].accept; | |
2181 | while (state) { | |
2182 | state = prev_states[state]; | |
2183 | if (!state) | |
2184 | break; | |
2185 | prev = trie->states[state].wordnum; | |
2186 | if (prev) | |
2187 | break; | |
2188 | } | |
2189 | trie->wordinfo[word].prev = prev; | |
2190 | } | |
2191 | Safefree(prev_states); | |
2192 | } | |
2193 | ||
2194 | ||
2195 | /* and now dump out the compressed format */ | |
2196 | DEBUG_TRIE_COMPILE_r(dump_trie(trie, widecharmap, revcharmap, depth+1)); | |
2197 | ||
55eed653 | 2198 | RExC_rxi->data->data[ data_slot + 1 ] = (void*)widecharmap; |
2b8b4781 NC |
2199 | #ifdef DEBUGGING |
2200 | RExC_rxi->data->data[ data_slot + TRIE_WORDS_OFFSET ] = (void*)trie_words; | |
2201 | RExC_rxi->data->data[ data_slot + 3 ] = (void*)revcharmap; | |
2202 | #else | |
2203 | SvREFCNT_dec(revcharmap); | |
07be1b83 | 2204 | #endif |
786e8c11 YO |
2205 | return trie->jump |
2206 | ? MADE_JUMP_TRIE | |
2207 | : trie->startstate>1 | |
2208 | ? MADE_EXACT_TRIE | |
2209 | : MADE_TRIE; | |
2210 | } | |
2211 | ||
2212 | STATIC void | |
2213 | S_make_trie_failtable(pTHX_ RExC_state_t *pRExC_state, regnode *source, regnode *stclass, U32 depth) | |
2214 | { | |
2215 | /* The Trie is constructed and compressed now so we can build a fail array now if its needed | |
2216 | ||
2217 | This is basically the Aho-Corasick algorithm. Its from exercise 3.31 and 3.32 in the | |
2218 | "Red Dragon" -- Compilers, principles, techniques, and tools. Aho, Sethi, Ullman 1985/88 | |
2219 | ISBN 0-201-10088-6 | |
2220 | ||
2221 | We find the fail state for each state in the trie, this state is the longest proper | |
2222 | suffix of the current states 'word' that is also a proper prefix of another word in our | |
2223 | trie. State 1 represents the word '' and is the thus the default fail state. This allows | |
2224 | the DFA not to have to restart after its tried and failed a word at a given point, it | |
2225 | simply continues as though it had been matching the other word in the first place. | |
2226 | Consider | |
2227 | 'abcdgu'=~/abcdefg|cdgu/ | |
2228 | When we get to 'd' we are still matching the first word, we would encounter 'g' which would | |
2229 | fail, which would bring use to the state representing 'd' in the second word where we would | |
2230 | try 'g' and succeed, prodceding to match 'cdgu'. | |
2231 | */ | |
2232 | /* add a fail transition */ | |
3251b653 NC |
2233 | const U32 trie_offset = ARG(source); |
2234 | reg_trie_data *trie=(reg_trie_data *)RExC_rxi->data->data[trie_offset]; | |
786e8c11 YO |
2235 | U32 *q; |
2236 | const U32 ucharcount = trie->uniquecharcount; | |
1e2e3d02 | 2237 | const U32 numstates = trie->statecount; |
786e8c11 YO |
2238 | const U32 ubound = trie->lasttrans + ucharcount; |
2239 | U32 q_read = 0; | |
2240 | U32 q_write = 0; | |
2241 | U32 charid; | |
2242 | U32 base = trie->states[ 1 ].trans.base; | |
2243 | U32 *fail; | |
2244 | reg_ac_data *aho; | |
2245 | const U32 data_slot = add_data( pRExC_state, 1, "T" ); | |
2246 | GET_RE_DEBUG_FLAGS_DECL; | |
7918f24d NC |
2247 | |
2248 | PERL_ARGS_ASSERT_MAKE_TRIE_FAILTABLE; | |
786e8c11 YO |
2249 | #ifndef DEBUGGING |
2250 | PERL_UNUSED_ARG(depth); | |
2251 | #endif | |
2252 | ||
2253 | ||
2254 | ARG_SET( stclass, data_slot ); | |
c944940b | 2255 | aho = (reg_ac_data *) PerlMemShared_calloc( 1, sizeof(reg_ac_data) ); |
f8fc2ecf | 2256 | RExC_rxi->data->data[ data_slot ] = (void*)aho; |
3251b653 | 2257 | aho->trie=trie_offset; |
446bd890 NC |
2258 | aho->states=(reg_trie_state *)PerlMemShared_malloc( numstates * sizeof(reg_trie_state) ); |
2259 | Copy( trie->states, aho->states, numstates, reg_trie_state ); | |
786e8c11 | 2260 | Newxz( q, numstates, U32); |
c944940b | 2261 | aho->fail = (U32 *) PerlMemShared_calloc( numstates, sizeof(U32) ); |
786e8c11 YO |
2262 | aho->refcount = 1; |
2263 | fail = aho->fail; | |
2264 | /* initialize fail[0..1] to be 1 so that we always have | |
2265 | a valid final fail state */ | |
2266 | fail[ 0 ] = fail[ 1 ] = 1; | |
2267 | ||
2268 | for ( charid = 0; charid < ucharcount ; charid++ ) { | |
2269 | const U32 newstate = TRIE_TRANS_STATE( 1, base, ucharcount, charid, 0 ); | |
2270 | if ( newstate ) { | |
2271 | q[ q_write ] = newstate; | |
2272 | /* set to point at the root */ | |
2273 | fail[ q[ q_write++ ] ]=1; | |
2274 | } | |
2275 | } | |
2276 | while ( q_read < q_write) { | |
2277 | const U32 cur = q[ q_read++ % numstates ]; | |
2278 | base = trie->states[ cur ].trans.base; | |
2279 | ||
2280 | for ( charid = 0 ; charid < ucharcount ; charid++ ) { | |
2281 | const U32 ch_state = TRIE_TRANS_STATE( cur, base, ucharcount, charid, 1 ); | |
2282 | if (ch_state) { | |
2283 | U32 fail_state = cur; | |
2284 | U32 fail_base; | |
2285 | do { | |
2286 | fail_state = fail[ fail_state ]; | |
2287 | fail_base = aho->states[ fail_state ].trans.base; | |
2288 | } while ( !TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 ) ); | |
2289 | ||
2290 | fail_state = TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 ); | |
2291 | fail[ ch_state ] = fail_state; | |
2292 | if ( !aho->states[ ch_state ].wordnum && aho->states[ fail_state ].wordnum ) | |
2293 | { | |
2294 | aho->states[ ch_state ].wordnum = aho->states[ fail_state ].wordnum; | |
2295 | } | |
2296 | q[ q_write++ % numstates] = ch_state; | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | /* restore fail[0..1] to 0 so that we "fall out" of the AC loop | |
2301 | when we fail in state 1, this allows us to use the | |
2302 | charclass scan to find a valid start char. This is based on the principle | |
2303 | that theres a good chance the string being searched contains lots of stuff | |
2304 | that cant be a start char. | |
2305 | */ | |
2306 | fail[ 0 ] = fail[ 1 ] = 0; | |
2307 | DEBUG_TRIE_COMPILE_r({ | |
6d99fb9b JH |
2308 | PerlIO_printf(Perl_debug_log, |
2309 | "%*sStclass Failtable (%"UVuf" states): 0", | |
2310 | (int)(depth * 2), "", (UV)numstates | |
1e2e3d02 | 2311 | ); |
786e8c11 YO |
2312 | for( q_read=1; q_read<numstates; q_read++ ) { |
2313 | PerlIO_printf(Perl_debug_log, ", %"UVuf, (UV)fail[q_read]); | |
2314 | } | |
2315 | PerlIO_printf(Perl_debug_log, "\n"); | |
2316 | }); | |
2317 | Safefree(q); | |
2318 | /*RExC_seen |= REG_SEEN_TRIEDFA;*/ | |
a3621e74 YO |
2319 | } |
2320 | ||
786e8c11 | 2321 | |
a3621e74 | 2322 | /* |
5d1c421c JH |
2323 | * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2. |
2324 | * These need to be revisited when a newer toolchain becomes available. | |
2325 | */ | |
2326 | #if defined(__sparc64__) && defined(__GNUC__) | |
2327 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) | |
2328 | # undef SPARC64_GCC_WORKAROUND | |
2329 | # define SPARC64_GCC_WORKAROUND 1 | |
2330 | # endif | |
2331 | #endif | |
2332 | ||
07be1b83 | 2333 | #define DEBUG_PEEP(str,scan,depth) \ |
b515a41d | 2334 | DEBUG_OPTIMISE_r({if (scan){ \ |
07be1b83 YO |
2335 | SV * const mysv=sv_newmortal(); \ |
2336 | regnode *Next = regnext(scan); \ | |
2337 | regprop(RExC_rx, mysv, scan); \ | |
7f69552c | 2338 | PerlIO_printf(Perl_debug_log, "%*s" str ">%3d: %s (%d)\n", \ |
07be1b83 YO |
2339 | (int)depth*2, "", REG_NODE_NUM(scan), SvPV_nolen_const(mysv),\ |
2340 | Next ? (REG_NODE_NUM(Next)) : 0 ); \ | |
b515a41d | 2341 | }}); |
07be1b83 | 2342 | |
1de06328 YO |
2343 | |
2344 | ||
2345 | ||
2346 | ||
07be1b83 YO |
2347 | #define JOIN_EXACT(scan,min,flags) \ |
2348 | if (PL_regkind[OP(scan)] == EXACT) \ | |
2349 | join_exact(pRExC_state,(scan),(min),(flags),NULL,depth+1) | |
2350 | ||
be8e71aa | 2351 | STATIC U32 |
07be1b83 YO |
2352 | S_join_exact(pTHX_ RExC_state_t *pRExC_state, regnode *scan, I32 *min, U32 flags,regnode *val, U32 depth) { |
2353 | /* Merge several consecutive EXACTish nodes into one. */ | |
2354 | regnode *n = regnext(scan); | |
2355 | U32 stringok = 1; | |
2356 | regnode *next = scan + NODE_SZ_STR(scan); | |
2357 | U32 merged = 0; | |
2358 | U32 stopnow = 0; | |
2359 | #ifdef DEBUGGING | |
2360 | regnode *stop = scan; | |
72f13be8 | 2361 | GET_RE_DEBUG_FLAGS_DECL; |
f9049ba1 | 2362 | #else |
d47053eb RGS |
2363 | PERL_UNUSED_ARG(depth); |
2364 | #endif | |
7918f24d NC |
2365 | |
2366 | PERL_ARGS_ASSERT_JOIN_EXACT; | |
d47053eb | 2367 | #ifndef EXPERIMENTAL_INPLACESCAN |
f9049ba1 SP |
2368 | PERL_UNUSED_ARG(flags); |
2369 | PERL_UNUSED_ARG(val); | |
07be1b83 | 2370 | #endif |
07be1b83 YO |
2371 | DEBUG_PEEP("join",scan,depth); |
2372 | ||
2373 | /* Skip NOTHING, merge EXACT*. */ | |
2374 | while (n && | |
2375 | ( PL_regkind[OP(n)] == NOTHING || | |
2376 | (stringok && (OP(n) == OP(scan)))) | |
2377 | && NEXT_OFF(n) | |
2378 | && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) { | |
2379 | ||
2380 | if (OP(n) == TAIL || n > next) | |
2381 | stringok = 0; | |
2382 | if (PL_regkind[OP(n)] == NOTHING) { | |
07be1b83 YO |
2383 | DEBUG_PEEP("skip:",n,depth); |
2384 | NEXT_OFF(scan) += NEXT_OFF(n); | |
2385 | next = n + NODE_STEP_REGNODE; | |
2386 | #ifdef DEBUGGING | |
2387 | if (stringok) | |
2388 | stop = n; | |
2389 | #endif | |
2390 | n = regnext(n); | |
2391 | } | |
2392 | else if (stringok) { | |
786e8c11 | 2393 | const unsigned int oldl = STR_LEN(scan); |
07be1b83 YO |
2394 | regnode * const nnext = regnext(n); |
2395 | ||
2396 | DEBUG_PEEP("merg",n,depth); | |
2397 | ||
2398 | merged++; | |
2399 | if (oldl + STR_LEN(n) > U8_MAX) | |
2400 | break; | |
2401 | NEXT_OFF(scan) += NEXT_OFF(n); | |
2402 | STR_LEN(scan) += STR_LEN(n); | |
2403 | next = n + NODE_SZ_STR(n); | |
2404 | /* Now we can overwrite *n : */ | |
2405 | Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char); | |
2406 | #ifdef DEBUGGING | |
2407 | stop = next - 1; | |
2408 | #endif | |
2409 | n = nnext; | |
2410 | if (stopnow) break; | |
2411 | } | |
2412 | ||
d47053eb RGS |
2413 | #ifdef EXPERIMENTAL_INPLACESCAN |
2414 | if (flags && !NEXT_OFF(n)) { | |
2415 | DEBUG_PEEP("atch", val, depth); | |
2416 | if (reg_off_by_arg[OP(n)]) { | |
2417 | ARG_SET(n, val - n); | |
2418 | } | |
2419 | else { | |
2420 | NEXT_OFF(n) = val - n; | |
2421 | } | |
2422 | stopnow = 1; | |
2423 | } | |
07be1b83 YO |
2424 | #endif |
2425 | } | |
2426 | ||
2427 | if (UTF && ( OP(scan) == EXACTF ) && ( STR_LEN(scan) >= 6 ) ) { | |
2428 | /* | |
2429 | Two problematic code points in Unicode casefolding of EXACT nodes: | |
2430 | ||
2431 | U+0390 - GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS | |
2432 | U+03B0 - GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS | |
2433 | ||
2434 | which casefold to | |
2435 | ||
2436 | Unicode UTF-8 | |
2437 | ||
2438 | U+03B9 U+0308 U+0301 0xCE 0xB9 0xCC 0x88 0xCC 0x81 | |
2439 | U+03C5 U+0308 U+0301 0xCF 0x85 0xCC 0x88 0xCC 0x81 | |
2440 | ||
2441 | This means that in case-insensitive matching (or "loose matching", | |
2442 | as Unicode calls it), an EXACTF of length six (the UTF-8 encoded byte | |
2443 | length of the above casefolded versions) can match a target string | |
2444 | of length two (the byte length of UTF-8 encoded U+0390 or U+03B0). | |
2445 | This would rather mess up the minimum length computation. | |
2446 | ||
2447 | What we'll do is to look for the tail four bytes, and then peek | |
2448 | at the preceding two bytes to see whether we need to decrease | |
2449 | the minimum length by four (six minus two). | |
2450 | ||
2451 | Thanks to the design of UTF-8, there cannot be false matches: | |
2452 | A sequence of valid UTF-8 bytes cannot be a subsequence of | |
2453 | another valid sequence of UTF-8 bytes. | |
2454 | ||
2455 | */ | |
2456 | char * const s0 = STRING(scan), *s, *t; | |
2457 | char * const s1 = s0 + STR_LEN(scan) - 1; | |
2458 | char * const s2 = s1 - 4; | |
e294cc5d JH |
2459 | #ifdef EBCDIC /* RD tunifold greek 0390 and 03B0 */ |
2460 | const char t0[] = "\xaf\x49\xaf\x42"; | |
2461 | #else | |
07be1b83 | 2462 | const char t0[] = "\xcc\x88\xcc\x81"; |
e294cc5d | 2463 | #endif |
07be1b83 YO |
2464 | const char * const t1 = t0 + 3; |
2465 | ||
2466 | for (s = s0 + 2; | |
2467 | s < s2 && (t = ninstr(s, s1, t0, t1)); | |
2468 | s = t + 4) { | |
e294cc5d JH |
2469 | #ifdef EBCDIC |
2470 | if (((U8)t[-1] == 0x68 && (U8)t[-2] == 0xB4) || | |
2471 | ((U8)t[-1] == 0x46 && (U8)t[-2] == 0xB5)) | |
2472 | #else | |
07be1b83 YO |
2473 | if (((U8)t[-1] == 0xB9 && (U8)t[-2] == 0xCE) || |
2474 | ((U8)t[-1] == 0x85 && (U8)t[-2] == 0xCF)) | |
e294cc5d | 2475 | #endif |
07be1b83 YO |
2476 | *min -= 4; |
2477 | } | |
2478 | } | |
2479 | ||
2480 | #ifdef DEBUGGING | |
2481 | /* Allow dumping */ | |
2482 | n = scan + NODE_SZ_STR(scan); | |
2483 | while (n <= stop) { | |
2484 | if (PL_regkind[OP(n)] != NOTHING || OP(n) == NOTHING) { | |
2485 | OP(n) = OPTIMIZED; | |
2486 | NEXT_OFF(n) = 0; | |
2487 | } | |
2488 | n++; | |
2489 | } | |
2490 | #endif | |
2491 | DEBUG_OPTIMISE_r(if (merged){DEBUG_PEEP("finl",scan,depth)}); | |
2492 | return stopnow; | |
2493 | } | |
2494 | ||
653099ff GS |
2495 | /* REx optimizer. Converts nodes into quickier variants "in place". |
2496 | Finds fixed substrings. */ | |
2497 | ||
a0288114 | 2498 | /* Stops at toplevel WHILEM as well as at "last". At end *scanp is set |
c277df42 IZ |
2499 | to the position after last scanned or to NULL. */ |
2500 | ||
40d049e4 YO |
2501 | #define INIT_AND_WITHP \ |
2502 | assert(!and_withp); \ | |
2503 | Newx(and_withp,1,struct regnode_charclass_class); \ | |
2504 | SAVEFREEPV(and_withp) | |
07be1b83 | 2505 | |
b515a41d YO |
2506 | /* this is a chain of data about sub patterns we are processing that |
2507 | need to be handled seperately/specially in study_chunk. Its so | |
2508 | we can simulate recursion without losing state. */ | |
2509 | struct scan_frame; | |
2510 | typedef struct scan_frame { | |
2511 | regnode *last; /* last node to process in this frame */ | |
2512 | regnode *next; /* next node to process when last is reached */ | |
2513 | struct scan_frame *prev; /*previous frame*/ | |
2514 | I32 stop; /* what stopparen do we use */ | |
2515 | } scan_frame; | |
2516 | ||
304ee84b YO |
2517 | |
2518 | #define SCAN_COMMIT(s, data, m) scan_commit(s, data, m, is_inf) | |
2519 | ||
e1d1eefb YO |
2520 | #define CASE_SYNST_FNC(nAmE) \ |
2521 | case nAmE: \ | |
2522 | if (flags & SCF_DO_STCLASS_AND) { \ | |
2523 | for (value = 0; value < 256; value++) \ | |
2524 | if (!is_ ## nAmE ## _cp(value)) \ | |
2525 | ANYOF_BITMAP_CLEAR(data->start_class, value); \ | |
2526 | } \ | |
2527 | else { \ | |
2528 | for (value = 0; value < 256; value++) \ | |
2529 | if (is_ ## nAmE ## _cp(value)) \ | |
2530 | ANYOF_BITMAP_SET(data->start_class, value); \ | |
2531 | } \ | |
2532 | break; \ | |
2533 | case N ## nAmE: \ | |
2534 | if (flags & SCF_DO_STCLASS_AND) { \ | |
2535 | for (value = 0; value < 256; value++) \ | |
2536 | if (is_ ## nAmE ## _cp(value)) \ | |
2537 | ANYOF_BITMAP_CLEAR(data->start_class, value); \ | |
2538 | } \ | |
2539 | else { \ | |
2540 | for (value = 0; value < 256; value++) \ | |
2541 | if (!is_ ## nAmE ## _cp(value)) \ | |
2542 | ANYOF_BITMAP_SET(data->start_class, value); \ | |
2543 | } \ | |
2544 | break | |
2545 | ||
2546 | ||
2547 | ||
76e3520e | 2548 | STATIC I32 |
40d049e4 | 2549 | S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, |
1de06328 | 2550 | I32 *minlenp, I32 *deltap, |
40d049e4 YO |
2551 | regnode *last, |
2552 | scan_data_t *data, | |
2553 | I32 stopparen, | |
2554 | U8* recursed, | |
2555 | struct regnode_charclass_class *and_withp, | |
2556 | U32 flags, U32 depth) | |
c277df42 IZ |
2557 | /* scanp: Start here (read-write). */ |
2558 | /* deltap: Write maxlen-minlen here. */ | |
2559 | /* last: Stop before this one. */ | |
40d049e4 YO |
2560 | /* data: string data about the pattern */ |
2561 | /* stopparen: treat close N as END */ | |
2562 | /* recursed: which subroutines have we recursed into */ | |
2563 | /* and_withp: Valid if flags & SCF_DO_STCLASS_OR */ | |
c277df42 | 2564 | { |
97aff369 | 2565 | dVAR; |
c277df42 IZ |
2566 | I32 min = 0, pars = 0, code; |
2567 | regnode *scan = *scanp, *next; | |
2568 | I32 delta = 0; | |
2569 | int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF); | |
aca2d497 | 2570 | int is_inf_internal = 0; /* The studied chunk is infinite */ |
c277df42 IZ |
2571 | I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0; |
2572 | scan_data_t data_fake; | |
a3621e74 | 2573 | SV *re_trie_maxbuff = NULL; |
786e8c11 | 2574 | regnode *first_non_open = scan; |
e2e6a0f1 | 2575 | I32 stopmin = I32_MAX; |
8aa23a47 | 2576 | scan_frame *frame = NULL; |
a3621e74 | 2577 | GET_RE_DEBUG_FLAGS_DECL; |
8aa23a47 | 2578 | |
7918f24d NC |
2579 | PERL_ARGS_ASSERT_STUDY_CHUNK; |
2580 | ||
13a24bad | 2581 | #ifdef DEBUGGING |
40d049e4 | 2582 | StructCopy(&zero_scan_data, &data_fake, scan_data_t); |
13a24bad | 2583 | #endif |
40d049e4 | 2584 | |
786e8c11 | 2585 | if ( depth == 0 ) { |
40d049e4 | 2586 | while (first_non_open && OP(first_non_open) == OPEN) |
786e8c11 YO |
2587 | first_non_open=regnext(first_non_open); |
2588 | } | |
2589 | ||
b81d288d | 2590 | |
8aa23a47 YO |
2591 | fake_study_recurse: |
2592 | while ( scan && OP(scan) != END && scan < last ){ | |
2593 | /* Peephole optimizer: */ | |
304ee84b | 2594 | DEBUG_STUDYDATA("Peep:", data,depth); |
8aa23a47 YO |
2595 | DEBUG_PEEP("Peep",scan,depth); |
2596 | JOIN_EXACT(scan,&min,0); | |
2597 | ||
2598 | /* Follow the next-chain of the current node and optimize | |
2599 | away all the NOTHINGs from it. */ | |
2600 | if (OP(scan) != CURLYX) { | |
2601 | const int max = (reg_off_by_arg[OP(scan)] | |
2602 | ? I32_MAX | |
2603 | /* I32 may be smaller than U16 on CRAYs! */ | |
2604 | : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX)); | |
2605 | int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan)); | |
2606 | int noff; | |
2607 | regnode *n = scan; | |
2608 | ||
2609 | /* Skip NOTHING and LONGJMP. */ | |
2610 | while ((n = regnext(n)) | |
2611 | && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n))) | |
2612 | || ((OP(n) == LONGJMP) && (noff = ARG(n)))) | |
2613 | && off + noff < max) | |
2614 | off += noff; | |
2615 | if (reg_off_by_arg[OP(scan)]) | |
2616 | ARG(scan) = off; | |
2617 | else | |
2618 | NEXT_OFF(scan) = off; | |
2619 | } | |
a3621e74 | 2620 | |
c277df42 | 2621 | |
8aa23a47 YO |
2622 | |
2623 | /* The principal pseudo-switch. Cannot be a switch, since we | |
2624 | look into several different things. */ | |
2625 | if (OP(scan) == BRANCH || OP(scan) == BRANCHJ | |
2626 | || OP(scan) == IFTHEN) { | |
2627 | next = regnext(scan); | |
2628 | code = OP(scan); | |
2629 | /* demq: the op(next)==code check is to see if we have "branch-branch" AFAICT */ | |
2630 | ||
2631 | if (OP(next) == code || code == IFTHEN) { | |
2632 | /* NOTE - There is similar code to this block below for handling | |
2633 | TRIE nodes on a re-study. If you change stuff here check there | |
2634 | too. */ | |
2635 | I32 max1 = 0, min1 = I32_MAX, num = 0; | |
2636 | struct regnode_charclass_class accum; | |
2637 | regnode * const startbranch=scan; | |
2638 | ||
2639 | if (flags & SCF_DO_SUBSTR) | |
304ee84b | 2640 | SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot merge strings after this. */ |
8aa23a47 YO |
2641 | if (flags & SCF_DO_STCLASS) |
2642 | cl_init_zero(pRExC_state, &accum); | |
2643 | ||
2644 | while (OP(scan) == code) { | |
2645 | I32 deltanext, minnext, f = 0, fake; | |
2646 | struct regnode_charclass_class this_class; | |
2647 | ||
2648 | num++; | |
2649 | data_fake.flags = 0; | |
2650 | if (data) { | |
2651 | data_fake.whilem_c = data->whilem_c; | |
2652 | data_fake.last_closep = data->last_closep; | |
2653 | } | |
2654 | else | |
2655 | data_fake.last_closep = &fake; | |
58e23c8d YO |
2656 | |
2657 | data_fake.pos_delta = delta; | |
8aa23a47 YO |
2658 | next = regnext(scan); |
2659 | scan = NEXTOPER(scan); | |
2660 | if (code != BRANCH) | |
c277df42 | 2661 | scan = NEXTOPER(scan); |
8aa23a47 YO |
2662 | if (flags & SCF_DO_STCLASS) { |
2663 | cl_init(pRExC_state, &this_class); | |
2664 | data_fake.start_class = &this_class; | |
2665 | f = SCF_DO_STCLASS_AND; | |
58e23c8d | 2666 | } |
8aa23a47 YO |
2667 | if (flags & SCF_WHILEM_VISITED_POS) |
2668 | f |= SCF_WHILEM_VISITED_POS; | |
2669 | ||
2670 | /* we suppose the run is continuous, last=next...*/ | |
2671 | minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext, | |
2672 | next, &data_fake, | |
2673 | stopparen, recursed, NULL, f,depth+1); | |
2674 | if (min1 > minnext) | |
2675 | min1 = minnext; | |
2676 | if (max1 < minnext + deltanext) | |
2677 | max1 = minnext + deltanext; | |
2678 | if (deltanext == I32_MAX) | |
2679 | is_inf = is_inf_internal = 1; | |
2680 | scan = next; | |
2681 | if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) | |
2682 | pars++; | |
2683 | if (data_fake.flags & SCF_SEEN_ACCEPT) { | |
2684 | if ( stopmin > minnext) | |
2685 | stopmin = min + min1; | |
2686 | flags &= ~SCF_DO_SUBSTR; | |
2687 | if (data) | |
2688 | data->flags |= SCF_SEEN_ACCEPT; | |
2689 | } | |
2690 | if (data) { | |
2691 | if (data_fake.flags & SF_HAS_EVAL) | |
2692 | data->flags |= SF_HAS_EVAL; | |
2693 | data->whilem_c = data_fake.whilem_c; | |
3dab1dad | 2694 | } |
8aa23a47 YO |
2695 | if (flags & SCF_DO_STCLASS) |
2696 | cl_or(pRExC_state, &accum, &this_class); | |
2697 | } | |
2698 | if (code == IFTHEN && num < 2) /* Empty ELSE branch */ | |
2699 | min1 = 0; | |
2700 | if (flags & SCF_DO_SUBSTR) { | |
2701 | data->pos_min += min1; | |
2702 | data->pos_delta += max1 - min1; | |
2703 | if (max1 != min1 || is_inf) | |
2704 | data->longest = &(data->longest_float); | |
2705 | } | |
2706 | min += min1; | |
2707 | delta += max1 - min1; | |
2708 | if (flags & SCF_DO_STCLASS_OR) { | |
2709 | cl_or(pRExC_state, data->start_class, &accum); | |
2710 | if (min1) { | |
2711 | cl_and(data->start_class, and_withp); | |
2712 | flags &= ~SCF_DO_STCLASS; | |
653099ff | 2713 | } |
8aa23a47 YO |
2714 | } |
2715 | else if (flags & SCF_DO_STCLASS_AND) { | |
2716 | if (min1) { | |
2717 | cl_and(data->start_class, &accum); | |
2718 | flags &= ~SCF_DO_STCLASS; | |
de0c8cb8 | 2719 | } |
8aa23a47 YO |
2720 | else { |
2721 | /* Switch to OR mode: cache the old value of | |
2722 | * data->start_class */ | |
2723 | INIT_AND_WITHP; | |
2724 | StructCopy(data->start_class, and_withp, | |
2725 | struct regnode_charclass_class); | |
2726 | flags &= ~SCF_DO_STCLASS_AND; | |
2727 | StructCopy(&accum, data->start_class, | |
2728 | struct regnode_charclass_class); | |
2729 | flags |= SCF_DO_STCLASS_OR; | |
2730 | data->start_class->flags |= ANYOF_EOS; | |
de0c8cb8 | 2731 | } |
8aa23a47 | 2732 | } |
a3621e74 | 2733 | |
8aa23a47 YO |
2734 | if (PERL_ENABLE_TRIE_OPTIMISATION && OP( startbranch ) == BRANCH ) { |
2735 | /* demq. | |
a3621e74 | 2736 | |
8aa23a47 YO |
2737 | Assuming this was/is a branch we are dealing with: 'scan' now |
2738 | points at the item that follows the branch sequence, whatever | |
2739 | it is. We now start at the beginning of the sequence and look | |
2740 | for subsequences of | |
a3621e74 | 2741 | |
8aa23a47 YO |
2742 | BRANCH->EXACT=>x1 |
2743 | BRANCH->EXACT=>x2 | |
2744 | tail | |
a3621e74 | 2745 | |
8aa23a47 | 2746 | which would be constructed from a pattern like /A|LIST|OF|WORDS/ |
a3621e74 | 2747 | |
8aa23a47 YO |
2748 | If we can find such a subseqence we need to turn the first |
2749 | element into a trie and then add the subsequent branch exact | |
2750 | strings to the trie. | |
a3621e74 | 2751 | |
8aa23a47 | 2752 | We have two cases |
a3621e74 | 2753 | |
8aa23a47 | 2754 | 1. patterns where the whole set of branch can be converted. |
a3621e74 | 2755 | |
8aa23a47 | 2756 | 2. patterns where only a subset can be converted. |
a3621e74 | 2757 | |
8aa23a47 YO |
2758 | In case 1 we can replace the whole set with a single regop |
2759 | for the trie. In case 2 we need to keep the start and end | |
2760 | branchs so | |
a3621e74 | 2761 | |
8aa23a47 YO |
2762 | 'BRANCH EXACT; BRANCH EXACT; BRANCH X' |
2763 | becomes BRANCH TRIE; BRANCH X; | |
786e8c11 | 2764 | |
8aa23a47 YO |
2765 | There is an additional case, that being where there is a |
2766 | common prefix, which gets split out into an EXACT like node | |
2767 | preceding the TRIE node. | |
a3621e74 | 2768 | |
8aa23a47 YO |
2769 | If x(1..n)==tail then we can do a simple trie, if not we make |
2770 | a "jump" trie, such that when we match the appropriate word | |
2771 | we "jump" to the appopriate tail node. Essentailly we turn | |
2772 | a nested if into a case structure of sorts. | |
b515a41d | 2773 | |
8aa23a47 YO |
2774 | */ |
2775 | ||
2776 | int made=0; | |
2777 | if (!re_trie_maxbuff) { | |
2778 | re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1); | |
2779 | if (!SvIOK(re_trie_maxbuff)) | |
2780 | sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT); | |
2781 | } | |
2782 | if ( SvIV(re_trie_maxbuff)>=0 ) { | |
2783 | regnode *cur; | |
2784 | regnode *first = (regnode *)NULL; | |
2785 | regnode *last = (regnode *)NULL; | |
2786 | regnode *tail = scan; | |
2787 | U8 optype = 0; | |
2788 | U32 count=0; | |
a3621e74 YO |
2789 | |
2790 | #ifdef DEBUGGING | |
8aa23a47 | 2791 | SV * const mysv = sv_newmortal(); /* for dumping */ |
a3621e74 | 2792 | #endif |
8aa23a47 YO |
2793 | /* var tail is used because there may be a TAIL |
2794 | regop in the way. Ie, the exacts will point to the | |
2795 | thing following the TAIL, but the last branch will | |
2796 | point at the TAIL. So we advance tail. If we | |
2797 | have nested (?:) we may have to move through several | |
2798 | tails. | |
2799 | */ | |
2800 | ||
2801 | while ( OP( tail ) == TAIL ) { | |
2802 | /* this is the TAIL generated by (?:) */ | |
2803 | tail = regnext( tail ); | |
2804 | } | |
a3621e74 | 2805 | |
8aa23a47 YO |
2806 | |
2807 | DEBUG_OPTIMISE_r({ | |
2808 | regprop(RExC_rx, mysv, tail ); | |
2809 | PerlIO_printf( Perl_debug_log, "%*s%s%s\n", | |
2810 | (int)depth * 2 + 2, "", | |
2811 | "Looking for TRIE'able sequences. Tail node is: ", | |
2812 | SvPV_nolen_const( mysv ) | |
2813 | ); | |
2814 | }); | |
2815 | ||
2816 | /* | |
2817 | ||
2818 | step through the branches, cur represents each | |
2819 | branch, noper is the first thing to be matched | |
2820 | as part of that branch and noper_next is the | |
2821 | regnext() of that node. if noper is an EXACT | |
2822 | and noper_next is the same as scan (our current | |
2823 | position in the regex) then the EXACT branch is | |
2824 | a possible optimization target. Once we have | |
2825 | two or more consequetive such branches we can | |
2826 | create a trie of the EXACT's contents and stich | |
2827 | it in place. If the sequence represents all of | |
2828 | the branches we eliminate the whole thing and | |
2829 | replace it with a single TRIE. If it is a | |
2830 | subsequence then we need to stitch it in. This | |
2831 | means the first branch has to remain, and needs | |
2832 | to be repointed at the item on the branch chain | |
2833 | following the last branch optimized. This could | |
2834 | be either a BRANCH, in which case the | |
2835 | subsequence is internal, or it could be the | |
2836 | item following the branch sequence in which | |
2837 | case the subsequence is at the end. | |
2838 | ||
2839 | */ | |
2840 | ||
2841 | /* dont use tail as the end marker for this traverse */ | |
2842 | for ( cur = startbranch ; cur != scan ; cur = regnext( cur ) ) { | |
2843 | regnode * const noper = NEXTOPER( cur ); | |
b515a41d | 2844 | #if defined(DEBUGGING) || defined(NOJUMPTRIE) |
8aa23a47 | 2845 | regnode * const noper_next = regnext( noper ); |
b515a41d YO |
2846 | #endif |
2847 | ||
8aa23a47 YO |
2848 | DEBUG_OPTIMISE_r({ |
2849 | regprop(RExC_rx, mysv, cur); | |
2850 | PerlIO_printf( Perl_debug_log, "%*s- %s (%d)", | |
2851 | (int)depth * 2 + 2,"", SvPV_nolen_const( mysv ), REG_NODE_NUM(cur) ); | |
2852 | ||
2853 | regprop(RExC_rx, mysv, noper); | |
2854 | PerlIO_printf( Perl_debug_log, " -> %s", | |
2855 | SvPV_nolen_const(mysv)); | |
2856 | ||
2857 | if ( noper_next ) { | |
2858 | regprop(RExC_rx, mysv, noper_next ); | |
2859 | PerlIO_printf( Perl_debug_log,"\t=> %s\t", | |
2860 | SvPV_nolen_const(mysv)); | |
2861 | } | |
2862 | PerlIO_printf( Perl_debug_log, "(First==%d,Last==%d,Cur==%d)\n", | |
2863 | REG_NODE_NUM(first), REG_NODE_NUM(last), REG_NODE_NUM(cur) ); | |
2864 | }); | |
2865 | if ( (((first && optype!=NOTHING) ? OP( noper ) == optype | |
2866 | : PL_regkind[ OP( noper ) ] == EXACT ) | |
2867 | || OP(noper) == NOTHING ) | |
786e8c11 | 2868 | #ifdef NOJUMPTRIE |
8aa23a47 | 2869 | && noper_next == tail |
786e8c11 | 2870 | #endif |
8aa23a47 YO |
2871 | && count < U16_MAX) |
2872 | { | |
2873 | count++; | |
2874 | if ( !first || optype == NOTHING ) { | |
2875 | if (!first) first = cur; | |
2876 | optype = OP( noper ); | |
2877 | } else { | |
2878 | last = cur; | |
2879 | } | |
2880 | } else { | |
a0a388a1 | 2881 | /* |
0abd0d78 YO |
2882 | Currently we do not believe that the trie logic can |
2883 | handle case insensitive matching properly when the | |
2884 | pattern is not unicode (thus forcing unicode semantics). | |
2885 | ||
2886 | If/when this is fixed the following define can be swapped | |
2887 | in below to fully enable trie logic. | |
2888 | ||
a0a388a1 | 2889 | #define TRIE_TYPE_IS_SAFE 1 |
0abd0d78 YO |
2890 | |
2891 | */ | |
2892 | #define TRIE_TYPE_IS_SAFE (UTF || optype==EXACT) | |
2893 | ||
a0a388a1 | 2894 | if ( last && TRIE_TYPE_IS_SAFE ) { |
8aa23a47 YO |
2895 | make_trie( pRExC_state, |
2896 | startbranch, first, cur, tail, count, | |
2897 | optype, depth+1 ); | |
2898 | } | |
2899 | if ( PL_regkind[ OP( noper ) ] == EXACT | |
786e8c11 | 2900 | #ifdef NOJUMPTRIE |
8aa23a47 | 2901 | && noper_next == tail |
786e8c11 | 2902 | #endif |
8aa23a47 YO |
2903 | ){ |
2904 | count = 1; | |
2905 | first = cur; | |
2906 | optype = OP( noper ); | |
2907 | } else { | |
2908 | count = 0; | |
2909 | first = NULL; | |
2910 | optype = 0; | |
2911 | } | |
2912 | last = NULL; | |
2913 | } | |
2914 | } | |
2915 | DEBUG_OPTIMISE_r({ | |
2916 | regprop(RExC_rx, mysv, cur); | |
2917 | PerlIO_printf( Perl_debug_log, | |
2918 | "%*s- %s (%d) <SCAN FINISHED>\n", (int)depth * 2 + 2, | |
2919 | "", SvPV_nolen_const( mysv ),REG_NODE_NUM(cur)); | |
2920 | ||
2921 | }); | |
a0a388a1 YO |
2922 | |
2923 | if ( last && TRIE_TYPE_IS_SAFE ) { | |
8aa23a47 | 2924 | made= make_trie( pRExC_state, startbranch, first, scan, tail, count, optype, depth+1 ); |
3dab1dad | 2925 | #ifdef TRIE_STUDY_OPT |
8aa23a47 YO |
2926 | if ( ((made == MADE_EXACT_TRIE && |
2927 | startbranch == first) | |
2928 | || ( first_non_open == first )) && | |
2929 | depth==0 ) { | |
2930 | flags |= SCF_TRIE_RESTUDY; | |
2931 | if ( startbranch == first | |
2932 | && scan == tail ) | |
2933 | { | |
2934 | RExC_seen &=~REG_TOP_LEVEL_BRANCHES; | |
2935 | } | |
2936 | } | |
3dab1dad | 2937 | #endif |
8aa23a47 YO |
2938 | } |
2939 | } | |
2940 | ||
2941 | } /* do trie */ | |
2942 | ||
653099ff | 2943 | } |
8aa23a47 YO |
2944 | else if ( code == BRANCHJ ) { /* single branch is optimized. */ |
2945 | scan = NEXTOPER(NEXTOPER(scan)); | |
2946 | } else /* single branch is optimized. */ | |
2947 | scan = NEXTOPER(scan); | |
2948 | continue; | |
2949 | } else if (OP(scan) == SUSPEND || OP(scan) == GOSUB || OP(scan) == GOSTART) { | |
2950 | scan_frame *newframe = NULL; | |
2951 | I32 paren; | |
2952 | regnode *start; | |
2953 | regnode *end; | |
2954 | ||
2955 | if (OP(scan) != SUSPEND) { | |
2956 | /* set the pointer */ | |
2957 | if (OP(scan) == GOSUB) { | |
2958 | paren = ARG(scan); | |
2959 | RExC_recurse[ARG2L(scan)] = scan; | |
2960 | start = RExC_open_parens[paren-1]; | |
2961 | end = RExC_close_parens[paren-1]; | |
2962 | } else { | |
2963 | paren = 0; | |
f8fc2ecf | 2964 | start = RExC_rxi->program + 1; |
8aa23a47 YO |
2965 | end = RExC_opend; |
2966 | } | |
2967 | if (!recursed) { | |
2968 | Newxz(recursed, (((RExC_npar)>>3) +1), U8); | |
2969 | SAVEFREEPV(recursed); | |
2970 | } | |
2971 | if (!PAREN_TEST(recursed,paren+1)) { | |
2972 | PAREN_SET(recursed,paren+1); | |
2973 | Newx(newframe,1,scan_frame); | |
2974 | } else { | |
2975 | if (flags & SCF_DO_SUBSTR) { | |
304ee84b | 2976 | SCAN_COMMIT(pRExC_state,data,minlenp); |
8aa23a47 YO |
2977 | data->longest = &(data->longest_float); |
2978 | } | |
2979 | is_inf = is_inf_internal = 1; | |
2980 | if (flags & SCF_DO_STCLASS_OR) /* Allow everything */ | |
2981 | cl_anything(pRExC_state, data->start_class); | |
2982 | flags &= ~SCF_DO_STCLASS; | |
2983 | } | |
2984 | } else { | |
2985 | Newx(newframe,1,scan_frame); | |
2986 | paren = stopparen; | |
2987 | start = scan+2; | |
2988 | end = regnext(scan); | |
2989 | } | |
2990 | if (newframe) { | |
2991 | assert(start); | |
2992 | assert(end); | |
2993 | SAVEFREEPV(newframe); | |
2994 | newframe->next = regnext(scan); | |
2995 | newframe->last = last; | |
2996 | newframe->stop = stopparen; | |
2997 | newframe->prev = frame; | |
2998 | ||
2999 | frame = newframe; | |
3000 | scan = start; | |
3001 | stopparen = paren; | |
3002 | last = end; | |
3003 | ||
3004 | continue; | |
3005 | } | |
3006 | } | |
3007 | else if (OP(scan) == EXACT) { | |
3008 | I32 l = STR_LEN(scan); | |
3009 | UV uc; | |
3010 | if (UTF) { | |
3011 | const U8 * const s = (U8*)STRING(scan); | |
3012 | l = utf8_length(s, s + l); | |
3013 | uc = utf8_to_uvchr(s, NULL); | |
3014 | } else { | |
3015 | uc = *((U8*)STRING(scan)); | |
3016 | } | |
3017 | min += l; | |
3018 | if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */ | |
3019 | /* The code below prefers earlier match for fixed | |
3020 | offset, later match for variable offset. */ | |
3021 | if (data->last_end == -1) { /* Update the start info. */ | |
3022 | data->last_start_min = data->pos_min; | |
3023 | data->last_start_max = is_inf | |
3024 | ? I32_MAX : data->pos_min + data->pos_delta; | |
b515a41d | 3025 | } |
8aa23a47 YO |
3026 | sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan)); |
3027 | if (UTF) | |
3028 | SvUTF8_on(data->last_found); | |
3029 | { | |
3030 | SV * const sv = data->last_found; | |
3031 | MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? | |
3032 | mg_find(sv, PERL_MAGIC_utf8) : NULL; | |
3033 | if (mg && mg->mg_len >= 0) | |
3034 | mg->mg_len += utf8_length((U8*)STRING(scan), | |
3035 | (U8*)STRING(scan)+STR_LEN(scan)); | |
b515a41d | 3036 | } |
8aa23a47 YO |
3037 | data->last_end = data->pos_min + l; |
3038 | data->pos_min += l; /* As in the first entry. */ | |
3039 | data->flags &= ~SF_BEFORE_EOL; | |
3040 | } | |
3041 | if (flags & SCF_DO_STCLASS_AND) { | |
3042 | /* Check whether it is compatible with what we know already! */ | |
3043 | int compat = 1; | |
3044 | ||
3045 | if (uc >= 0x100 || | |
3046 | (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE)) | |
3047 | && !ANYOF_BITMAP_TEST(data->start_class, uc) | |
3048 | && (!(data->start_class->flags & ANYOF_FOLD) | |
3049 | || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc]))) | |
3050 | ) | |
3051 | compat = 0; | |
3052 | ANYOF_CLASS_ZERO(data->start_class); | |
3053 | ANYOF_BITMAP_ZERO(data->start_class); | |
3054 | if (compat) | |
3055 | ANYOF_BITMAP_SET(data->start_class, uc); | |
3056 | data->start_class->flags &= ~ANYOF_EOS; | |
3057 | if (uc < 0x100) | |
3058 | data->start_class->flags &= ~ANYOF_UNICODE_ALL; | |
3059 | } | |
3060 | else if (flags & SCF_DO_STCLASS_OR) { | |
3061 | /* false positive possible if the class is case-folded */ | |
3062 | if (uc < 0x100) | |
3063 | ANYOF_BITMAP_SET(data->start_class, uc); | |
3064 | else | |
3065 | data->start_class->flags |= ANYOF_UNICODE_ALL; | |
3066 | data->start_class->flags &= ~ANYOF_EOS; | |
3067 | cl_and(data->start_class, and_withp); | |
3068 | } | |
3069 | flags &= ~SCF_DO_STCLASS; | |
3070 | } | |
3071 | else if (PL_regkind[OP(scan)] == EXACT) { /* But OP != EXACT! */ | |
3072 | I32 l = STR_LEN(scan); | |
3073 | UV uc = *((U8*)STRING(scan)); | |
3074 | ||
3075 | /* Search for fixed substrings supports EXACT only. */ | |
3076 | if (flags & SCF_DO_SUBSTR) { | |
3077 | assert(data); | |
304ee84b | 3078 | SCAN_COMMIT(pRExC_state, data, minlenp); |
8aa23a47 YO |
3079 | } |
3080 | if (UTF) { | |
3081 | const U8 * const s = (U8 *)STRING(scan); | |
3082 | l = utf8_length(s, s + l); | |
3083 | uc = utf8_to_uvchr(s, NULL); | |
3084 | } | |
3085 | min += l; | |
3086 | if (flags & SCF_DO_SUBSTR) | |
3087 | data->pos_min += l; | |
3088 | if (flags & SCF_DO_STCLASS_AND) { | |
3089 | /* Check whether it is compatible with what we know already! */ | |
3090 | int compat = 1; | |
3091 | ||
3092 | if (uc >= 0x100 || | |
3093 | (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE)) | |
3094 | && !ANYOF_BITMAP_TEST(data->start_class, uc) | |
3095 | && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc]))) | |
3096 | compat = 0; | |
3097 | ANYOF_CLASS_ZERO(data->start_class); | |
3098 | ANYOF_BITMAP_ZERO(data->start_class); | |
3099 | if (compat) { | |
3100 | ANYOF_BITMAP_SET(data->start_class, uc); | |
653099ff | 3101 | data->start_class->flags &= ~ANYOF_EOS; |
8aa23a47 YO |
3102 | data->start_class->flags |= ANYOF_FOLD; |
3103 | if (OP(scan) == EXACTFL) | |
3104 | data->start_class->flags |= ANYOF_LOCALE; | |
653099ff | 3105 | } |
8aa23a47 YO |
3106 | } |
3107 | else if (flags & SCF_DO_STCLASS_OR) { | |
3108 | if (data->start_class->flags & ANYOF_FOLD) { | |
3109 | /* false positive possible if the class is case-folded. | |
3110 | Assume that the locale settings are the same... */ | |
1aa99e6b IH |
3111 | if (uc < 0x100) |
3112 | ANYOF_BITMAP_SET(data->start_class, uc); | |
653099ff GS |
3113 | data->start_class->flags &= ~ANYOF_EOS; |
3114 | } | |
8aa23a47 | 3115 | cl_and(data->start_class, and_withp); |
653099ff | 3116 | } |
8aa23a47 YO |
3117 | flags &= ~SCF_DO_STCLASS; |
3118 | } | |
e52fc539 | 3119 | else if (REGNODE_VARIES(OP(scan))) { |
8aa23a47 YO |
3120 | I32 mincount, maxcount, minnext, deltanext, fl = 0; |
3121 | I32 f = flags, pos_before = 0; | |
3122 | regnode * const oscan = scan; | |
3123 | struct regnode_charclass_class this_class; | |
3124 | struct regnode_charclass_class *oclass = NULL; | |
3125 | I32 next_is_eval = 0; | |
3126 | ||
3127 | switch (PL_regkind[OP(scan)]) { | |
3128 | case WHILEM: /* End of (?:...)* . */ | |
3129 | scan = NEXTOPER(scan); | |
3130 | goto finish; | |
3131 | case PLUS: | |
3132 | if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) { | |
3133 | next = NEXTOPER(scan); | |
3134 | if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) { | |
3135 | mincount = 1; | |
3136 | maxcount = REG_INFTY; | |
3137 | next = regnext(scan); | |
3138 | scan = NEXTOPER(scan); | |
3139 | goto do_curly; | |
3140 | } | |
3141 | } | |
3142 | if (flags & SCF_DO_SUBSTR) | |
3143 | data->pos_min++; | |
3144 | min++; | |
3145 | /* Fall through. */ | |
3146 | case STAR: | |
3147 | if (flags & SCF_DO_STCLASS) { | |
3148 | mincount = 0; | |
3149 | maxcount = REG_INFTY; | |
3150 | next = regnext(scan); | |
3151 | scan = NEXTOPER(scan); | |
3152 | goto do_curly; | |
3153 | } | |
3154 | is_inf = is_inf_internal = 1; | |
3155 | scan = regnext(scan); | |
c277df42 | 3156 | if (flags & SCF_DO_SUBSTR) { |
304ee84b | 3157 | SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot extend fixed substrings */ |
8aa23a47 | 3158 | data->longest = &(data->longest_float); |
c277df42 | 3159 | } |
8aa23a47 YO |
3160 | goto optimize_curly_tail; |
3161 | case CURLY: | |
3162 | if (stopparen>0 && (OP(scan)==CURLYN || OP(scan)==CURLYM) | |
3163 | && (scan->flags == stopparen)) | |
3164 | { | |
3165 | mincount = 1; | |
3166 | maxcount = 1; | |
3167 | } else { | |
3168 | mincount = ARG1(scan); | |
3169 | maxcount = ARG2(scan); | |
653099ff | 3170 | } |
8aa23a47 YO |
3171 | next = regnext(scan); |
3172 | if (OP(scan) == CURLYX) { | |
3173 | I32 lp = (data ? *(data->last_closep) : 0); | |
3174 | scan->flags = ((lp <= (I32)U8_MAX) ? (U8)lp : U8_MAX); | |
653099ff | 3175 | } |
8aa23a47 YO |
3176 | scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS; |
3177 | next_is_eval = (OP(scan) == EVAL); | |
3178 | do_curly: | |
3179 | if (flags & SCF_DO_SUBSTR) { | |
304ee84b | 3180 | if (mincount == 0) SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot extend fixed substrings */ |
8aa23a47 | 3181 | pos_before = data->pos_min; |
b45f050a | 3182 | } |
8aa23a47 YO |
3183 | if (data) { |
3184 | fl = data->flags; | |
3185 | data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL); | |
3186 | if (is_inf) | |
3187 | data->flags |= SF_IS_INF; | |
3188 | } | |
3189 | if (flags & SCF_DO_STCLASS) { | |
3190 | cl_init(pRExC_state, &this_class); | |
3191 | oclass = data->start_class; | |
3192 | data->start_class = &this_class; | |
3193 | f |= SCF_DO_STCLASS_AND; | |
3194 | f &= ~SCF_DO_STCLASS_OR; | |
3195 | } | |
3196 | /* These are the cases when once a subexpression | |
3197 | fails at a particular position, it cannot succeed | |
3198 | even after backtracking at the enclosing scope. | |
3199 | ||
3200 | XXXX what if minimal match and we are at the | |
3201 | initial run of {n,m}? */ | |
3202 | if ((mincount != maxcount - 1) && (maxcount != REG_INFTY)) | |
3203 | f &= ~SCF_WHILEM_VISITED_POS; | |
b45f050a | 3204 | |
8aa23a47 YO |
3205 | /* This will finish on WHILEM, setting scan, or on NULL: */ |
3206 | minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext, | |
3207 | last, data, stopparen, recursed, NULL, | |
3208 | (mincount == 0 | |
3209 | ? (f & ~SCF_DO_SUBSTR) : f),depth+1); | |
b515a41d | 3210 | |
8aa23a47 YO |
3211 | if (flags & SCF_DO_STCLASS) |
3212 | data->start_class = oclass; | |
3213 | if (mincount == 0 || minnext == 0) { | |
3214 | if (flags & SCF_DO_STCLASS_OR) { | |
3215 | cl_or(pRExC_state, data->start_class, &this_class); | |
3216 | } | |
3217 | else if (flags & SCF_DO_STCLASS_AND) { | |
3218 | /* Switch to OR mode: cache the old value of | |
3219 | * data->start_class */ | |
3220 | INIT_AND_WITHP; | |
3221 | StructCopy(data->start_class, and_withp, | |
3222 | struct regnode_charclass_class); | |
3223 | flags &= ~SCF_DO_STCLASS_AND; | |
3224 | StructCopy(&this_class, data->start_class, | |
3225 | struct regnode_charclass_class); | |
3226 | flags |= SCF_DO_STCLASS_OR; | |
3227 | data->start_class->flags |= ANYOF_EOS; | |
3228 | } | |
3229 | } else { /* Non-zero len */ | |
3230 | if (flags & SCF_DO_STCLASS_OR) { | |
3231 | cl_or(pRExC_state, data->start_class, &this_class); | |
3232 | cl_and(data->start_class, and_withp); | |
3233 | } | |
3234 | else if (flags & SCF_DO_STCLASS_AND) | |
3235 | cl_and(data->start_class, &this_class); | |
3236 | flags &= ~SCF_DO_STCLASS; | |
3237 | } | |
3238 | if (!scan) /* It was not CURLYX, but CURLY. */ | |
3239 | scan = next; | |
3240 | if ( /* ? quantifier ok, except for (?{ ... }) */ | |
3241 | (next_is_eval || !(mincount == 0 && maxcount == 1)) | |
3242 | && (minnext == 0) && (deltanext == 0) | |
3243 | && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR)) | |
668c081a | 3244 | && maxcount <= REG_INFTY/3) /* Complement check for big count */ |
8aa23a47 | 3245 | { |
668c081a NC |
3246 | ckWARNreg(RExC_parse, |
3247 | "Quantifier unexpected on zero-length expression"); | |
8aa23a47 YO |
3248 | } |
3249 | ||
3250 | min += minnext * mincount; | |
3251 | is_inf_internal |= ((maxcount == REG_INFTY | |
3252 | && (minnext + deltanext) > 0) | |
3253 | || deltanext == I32_MAX); | |
3254 | is_inf |= is_inf_internal; | |
3255 | delta += (minnext + deltanext) * maxcount - minnext * mincount; | |
3256 | ||
3257 | /* Try powerful optimization CURLYX => CURLYN. */ | |
3258 | if ( OP(oscan) == CURLYX && data | |
3259 | && data->flags & SF_IN_PAR | |
3260 | && !(data->flags & SF_HAS_EVAL) | |
3261 | && !deltanext && minnext == 1 ) { | |
3262 | /* Try to optimize to CURLYN. */ | |
3263 | regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; | |
3264 | regnode * const nxt1 = nxt; | |
497b47a8 | 3265 | #ifdef DEBUGGING |
8aa23a47 | 3266 | regnode *nxt2; |
497b47a8 | 3267 | #endif |
c277df42 | 3268 | |
8aa23a47 YO |
3269 | /* Skip open. */ |
3270 | nxt = regnext(nxt); | |
e52fc539 | 3271 | if (!REGNODE_SIMPLE(OP(nxt)) |
8aa23a47 YO |
3272 | && !(PL_regkind[OP(nxt)] == EXACT |
3273 | && STR_LEN(nxt) == 1)) | |
3274 | goto nogo; | |
497b47a8 | 3275 | #ifdef DEBUGGING |
8aa23a47 | 3276 | nxt2 = nxt; |
497b47a8 | 3277 | #endif |
8aa23a47 YO |
3278 | nxt = regnext(nxt); |
3279 | if (OP(nxt) != CLOSE) | |
3280 | goto nogo; | |
3281 | if (RExC_open_parens) { | |
3282 | RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/ | |
3283 | RExC_close_parens[ARG(nxt1)-1]=nxt+2; /*close->while*/ | |
3284 | } | |
3285 | /* Now we know that nxt2 is the only contents: */ | |
3286 | oscan->flags = (U8)ARG(nxt); | |
3287 | OP(oscan) = CURLYN; | |
3288 | OP(nxt1) = NOTHING; /* was OPEN. */ | |
40d049e4 | 3289 | |
c277df42 | 3290 | #ifdef DEBUGGING |
8aa23a47 YO |
3291 | OP(nxt1 + 1) = OPTIMIZED; /* was count. */ |
3292 | NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */ | |
3293 | NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */ | |
3294 | OP(nxt) = OPTIMIZED; /* was CLOSE. */ | |
3295 | OP(nxt + 1) = OPTIMIZED; /* was count. */ | |
3296 | NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */ | |
b81d288d | 3297 | #endif |
8aa23a47 YO |
3298 | } |
3299 | nogo: | |
3300 | ||
3301 | /* Try optimization CURLYX => CURLYM. */ | |
3302 | if ( OP(oscan) == CURLYX && data | |
3303 | && !(data->flags & SF_HAS_PAR) | |
3304 | && !(data->flags & SF_HAS_EVAL) | |
3305 | && !deltanext /* atom is fixed width */ | |
3306 | && minnext != 0 /* CURLYM can't handle zero width */ | |
3307 | ) { | |
3308 | /* XXXX How to optimize if data == 0? */ | |
3309 | /* Optimize to a simpler form. */ | |
3310 | regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */ | |
3311 | regnode *nxt2; | |
3312 | ||
3313 | OP(oscan) = CURLYM; | |
3314 | while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/ | |
3315 | && (OP(nxt2) != WHILEM)) | |
3316 | nxt = nxt2; | |
3317 | OP(nxt2) = SUCCEED; /* Whas WHILEM */ | |
3318 | /* Need to optimize away parenths. */ | |
b3c0965f | 3319 | if ((data->flags & SF_IN_PAR) && OP(nxt) == CLOSE) { |
8aa23a47 YO |
3320 | /* Set the parenth number. */ |
3321 | regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/ | |
3322 | ||
8aa23a47 YO |
3323 | oscan->flags = (U8)ARG(nxt); |
3324 | if (RExC_open_parens) { | |
3325 | RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/ | |
3326 | RExC_close_parens[ARG(nxt1)-1]=nxt2+1; /*close->NOTHING*/ | |
40d049e4 | 3327 | } |
8aa23a47 YO |
3328 | OP(nxt1) = OPTIMIZED; /* was OPEN. */ |
3329 | OP(nxt) = OPTIMIZED; /* was CLOSE. */ | |
40d049e4 | 3330 | |
c277df42 | 3331 | #ifdef DEBUGGING |
8aa23a47 YO |
3332 | OP(nxt1 + 1) = OPTIMIZED; /* was count. */ |
3333 | OP(nxt + 1) = OPTIMIZED; /* was count. */ | |
3334 | NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */ | |
3335 | NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */ | |
b81d288d | 3336 | #endif |
c277df42 | 3337 | #if 0 |
8aa23a47 YO |
3338 | while ( nxt1 && (OP(nxt1) != WHILEM)) { |
3339 | regnode *nnxt = regnext(nxt1); | |
3340 | ||
3341 | if (nnxt == nxt) { | |
3342 | if (reg_off_by_arg[OP(nxt1)]) | |
3343 | ARG_SET(nxt1, nxt2 - nxt1); | |
3344 | else if (nxt2 - nxt1 < U16_MAX) | |
3345 | NEXT_OFF(nxt1) = nxt2 - nxt1; | |
3346 | else | |
3347 | OP(nxt) = NOTHING; /* Cannot beautify */ | |
c277df42 | 3348 | } |
8aa23a47 | 3349 | nxt1 = nnxt; |
c277df42 | 3350 | } |
5d1c421c | 3351 | #endif |
8aa23a47 YO |
3352 | /* Optimize again: */ |
3353 | study_chunk(pRExC_state, &nxt1, minlenp, &deltanext, nxt, | |
3354 | NULL, stopparen, recursed, NULL, 0,depth+1); | |
3355 | } | |
3356 | else | |
3357 | oscan->flags = 0; | |
3358 | } | |
3359 | else if ((OP(oscan) == CURLYX) | |
3360 | && (flags & SCF_WHILEM_VISITED_POS) | |
3361 | /* See the comment on a similar expression above. | |
3362 | However, this time it not a subexpression | |
3363 | we care about, but the expression itself. */ | |
3364 | && (maxcount == REG_INFTY) | |
3365 | && data && ++data->whilem_c < 16) { | |
3366 | /* This stays as CURLYX, we can put the count/of pair. */ | |
3367 | /* Find WHILEM (as in regexec.c) */ | |
3368 | regnode *nxt = oscan + NEXT_OFF(oscan); | |
3369 | ||
3370 | if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */ | |
3371 | nxt += ARG(nxt); | |
3372 | PREVOPER(nxt)->flags = (U8)(data->whilem_c | |
3373 | | (RExC_whilem_seen << 4)); /* On WHILEM */ | |
3374 | } | |
3375 | if (data && fl & (SF_HAS_PAR|SF_IN_PAR)) | |
3376 | pars++; | |
3377 | if (flags & SCF_DO_SUBSTR) { | |
3378 | SV *last_str = NULL; | |
3379 | int counted = mincount != 0; | |
a0ed51b3 | 3380 | |
8aa23a47 YO |
3381 | if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */ |
3382 | #if defined(SPARC64_GCC_WORKAROUND) | |
3383 | I32 b = 0; | |
3384 | STRLEN l = 0; | |
3385 | const char *s = NULL; | |
3386 | I32 old = 0; | |
b515a41d | 3387 | |
8aa23a47 YO |
3388 | if (pos_before >= data->last_start_min) |
3389 | b = pos_before; | |
3390 | else | |
3391 | b = data->last_start_min; | |
b515a41d | 3392 | |
8aa23a47 YO |
3393 | l = 0; |
3394 | s = SvPV_const(data->last_found, l); | |
3395 | old = b - data->last_start_min; | |
3396 | ||
3397 | #else | |
3398 | I32 b = pos_before >= data->last_start_min | |
3399 | ? pos_before : data->last_start_min; | |
3400 | STRLEN l; | |
3401 | const char * const s = SvPV_const(data->last_found, l); | |
3402 | I32 old = b - data->last_start_min; | |
3403 | #endif | |
3404 | ||
3405 | if (UTF) | |
3406 | old = utf8_hop((U8*)s, old) - (U8*)s; | |
3407 | ||
3408 | l -= old; | |
3409 | /* Get the added string: */ | |
740cce10 | 3410 | last_str = newSVpvn_utf8(s + old, l, UTF); |
8aa23a47 YO |
3411 | if (deltanext == 0 && pos_before == b) { |
3412 | /* What was added is a constant string */ | |
3413 | if (mincount > 1) { | |
3414 | SvGROW(last_str, (mincount * l) + 1); | |
3415 | repeatcpy(SvPVX(last_str) + l, | |
3416 | SvPVX_const(last_str), l, mincount - 1); | |
3417 | SvCUR_set(last_str, SvCUR(last_str) * mincount); | |
3418 | /* Add additional parts. */ | |
3419 | SvCUR_set(data->last_found, | |
3420 | SvCUR(data->last_found) - l); | |
3421 | sv_catsv(data->last_found, last_str); | |
3422 | { | |
3423 | SV * sv = data->last_found; | |
3424 | MAGIC *mg = | |
3425 | SvUTF8(sv) && SvMAGICAL(sv) ? | |
3426 | mg_find(sv, PERL_MAGIC_utf8) : NULL; | |
3427 | if (mg && mg->mg_len >= 0) | |
bd94e887 | 3428 | mg->mg_len += CHR_SVLEN(last_str) - l; |
b515a41d | 3429 | } |
8aa23a47 | 3430 | data->last_end += l * (mincount - 1); |
b515a41d | 3431 | } |
8aa23a47 YO |
3432 | } else { |
3433 | /* start offset must point into the last copy */ | |
3434 | data->last_start_min += minnext * (mincount - 1); | |
3435 | data->last_start_max += is_inf ? I32_MAX | |
3436 | : (maxcount - 1) * (minnext + data->pos_delta); | |
3437 | } | |
c277df42 | 3438 | } |
8aa23a47 YO |
3439 | /* It is counted once already... */ |
3440 | data->pos_min += minnext * (mincount - counted); | |
3441 | data->pos_delta += - counted * deltanext + | |
3442 | (minnext + deltanext) * maxcount - minnext * mincount; | |
3443 | if (mincount != maxcount) { | |
3444 | /* Cannot extend fixed substrings found inside | |
3445 | the group. */ | |
304ee84b | 3446 | SCAN_COMMIT(pRExC_state,data,minlenp); |
8aa23a47 YO |
3447 | if (mincount && last_str) { |
3448 | SV * const sv = data->last_found; | |
3449 | MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? | |
3450 | mg_find(sv, PERL_MAGIC_utf8) : NULL; | |
3451 | ||
3452 | if (mg) | |
3453 | mg->mg_len = -1; | |
3454 | sv_setsv(sv, last_str); | |
3455 | data->last_end = data->pos_min; | |
3456 | data->last_start_min = | |
3457 | data->pos_min - CHR_SVLEN(last_str); | |
3458 | data->last_start_max = is_inf | |
3459 | ? I32_MAX | |
3460 | : data->pos_min + data->pos_delta | |
3461 | - CHR_SVLEN(last_str); | |
3462 | } | |
3463 | data->longest = &(data->longest_float); | |
3464 | } | |
3465 | SvREFCNT_dec(last_str); | |
c277df42 | 3466 | } |
8aa23a47 YO |
3467 | if (data && (fl & SF_HAS_EVAL)) |
3468 | data->flags |= SF_HAS_EVAL; | |
3469 | optimize_curly_tail: | |
3470 | if (OP(oscan) != CURLYX) { | |
3471 | while (PL_regkind[OP(next = regnext(oscan))] == NOTHING | |
3472 | && NEXT_OFF(next)) | |
3473 | NEXT_OFF(oscan) += NEXT_OFF(next); | |
3474 | } | |
3475 | continue; | |
3476 | default: /* REF and CLUMP only? */ | |
3477 | if (flags & SCF_DO_SUBSTR) { | |
304ee84b | 3478 | SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */ |
8aa23a47 YO |
3479 | data->longest = &(data->longest_float); |
3480 | } | |
3481 | is_inf = is_inf_internal = 1; | |
3482 | if (flags & SCF_DO_STCLASS_OR) | |
3483 | cl_anything(pRExC_state, data->start_class); | |
3484 | flags &= ~SCF_DO_STCLASS; | |
3485 | break; | |
c277df42 | 3486 | } |
8aa23a47 | 3487 | } |
e1d1eefb YO |
3488 | else if (OP(scan) == LNBREAK) { |
3489 | if (flags & SCF_DO_STCLASS) { | |
3490 | int value = 0; | |
3491 | data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */ | |
3492 | if (flags & SCF_DO_STCLASS_AND) { | |
3493 | for (value = 0; value < 256; value++) | |
e64b1bd1 | 3494 | if (!is_VERTWS_cp(value)) |
e1d1eefb YO |
3495 | ANYOF_BITMAP_CLEAR(data->start_class, value); |
3496 | } | |
3497 | else { | |
3498 | for (value = 0; value < 256; value++) | |
e64b1bd1 | 3499 | if (is_VERTWS_cp(value)) |
e1d1eefb YO |
3500 | ANYOF_BITMAP_SET(data->start_class, value); |
3501 | } | |
3502 | if (flags & SCF_DO_STCLASS_OR) | |
3503 | cl_and(data->start_class, and_withp); | |
3504 | flags &= ~SCF_DO_STCLASS; | |
3505 | } | |
3506 | min += 1; | |
f9a79580 | 3507 | delta += 1; |
e1d1eefb YO |
3508 | if (flags & SCF_DO_SUBSTR) { |
3509 | SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */ | |
3510 | data->pos_min += 1; | |
f9a79580 | 3511 | data->pos_delta += 1; |
e1d1eefb YO |
3512 | data->longest = &(data->longest_float); |
3513 | } | |
3514 | ||
3515 | } | |
f9a79580 RGS |
3516 | else if (OP(scan) == FOLDCHAR) { |
3517 | int d = ARG(scan)==0xDF ? 1 : 2; | |
3518 | flags &= ~SCF_DO_STCLASS; | |
3519 | min += 1; | |
3520 | delta += d; | |
3521 | if (flags & SCF_DO_SUBSTR) { | |
3522 | SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */ | |
3523 | data->pos_min += 1; | |
3524 | data->pos_delta += d; | |
3525 | data->longest = &(data->longest_float); | |
3526 | } | |
3527 | } | |
e52fc539 | 3528 | else if (REGNODE_SIMPLE(OP(scan))) { |
8aa23a47 | 3529 | int value = 0; |
653099ff | 3530 | |
8aa23a47 | 3531 | if (flags & SCF_DO_SUBSTR) { |
304ee84b | 3532 | SCAN_COMMIT(pRExC_state,data,minlenp); |
8aa23a47 YO |
3533 | data->pos_min++; |
3534 | } | |
3535 | min++; | |
3536 | if (flags & SCF_DO_STCLASS) { | |
3537 | data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */ | |
b515a41d | 3538 | |
8aa23a47 YO |
3539 | /* Some of the logic below assumes that switching |
3540 | locale on will only add false positives. */ | |
3541 | switch (PL_regkind[OP(scan)]) { | |
3542 | case SANY: | |
3543 | default: | |
3544 | do_default: | |
3545 | /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */ | |
3546 | if (flags & SCF_DO_STCLASS_OR) /* Allow everything */ | |
3547 | cl_anything(pRExC_state, data->start_class); | |
3548 | break; | |
3549 | case REG_ANY: | |
3550 | if (OP(scan) == SANY) | |
3551 | goto do_default; | |
3552 | if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */ | |
3553 | value = (ANYOF_BITMAP_TEST(data->start_class,'\n') | |
3554 | || (data->start_class->flags & ANYOF_CLASS)); | |
3555 | cl_anything(pRExC_state, data->start_class); | |
653099ff | 3556 | } |
8aa23a47 YO |
3557 | if (flags & SCF_DO_STCLASS_AND || !value) |
3558 | ANYOF_BITMAP_CLEAR(data->start_class,'\n'); | |
3559 | break; | |
3560 | case ANYOF: | |
3561 | if (flags & SCF_DO_STCLASS_AND) | |
3562 | cl_and(data->start_class, | |
3563 | (struct regnode_charclass_class*)scan); | |
653099ff | 3564 | else |
8aa23a47 YO |
3565 | cl_or(pRExC_state, data->start_class, |
3566 | (struct regnode_charclass_class*)scan); | |
3567 | break; | |
3568 | case ALNUM: | |
3569 | if (flags & SCF_DO_STCLASS_AND) { | |
3570 | if (!(data->start_class->flags & ANYOF_LOCALE)) { | |
3571 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM); | |
3572 | for (value = 0; value < 256; value++) | |
3573 | if (!isALNUM(value)) | |
3574 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
3575 | } | |
653099ff | 3576 | } |
8aa23a47 YO |
3577 | else { |
3578 | if (data->start_class->flags & ANYOF_LOCALE) | |
3579 | ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM); | |
3580 | else { | |
3581 | for (value = 0; value < 256; value++) | |
3582 | if (isALNUM(value)) | |
3583 | ANYOF_BITMAP_SET(data->start_class, value); | |
653099ff | 3584 | } |
8aa23a47 YO |
3585 | } |
3586 | break; | |
3587 | case ALNUML: | |
3588 | if (flags & SCF_DO_STCLASS_AND) { | |
3589 | if (data->start_class->flags & ANYOF_LOCALE) | |
3590 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM); | |
3591 | } | |
3592 | else { | |
3593 | ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM); | |
3594 | data->start_class->flags |= ANYOF_LOCALE; | |
3595 | } | |
3596 | break; | |
3597 | case NALNUM: | |
3598 | if (flags & SCF_DO_STCLASS_AND) { | |
3599 | if (!(data->start_class->flags & ANYOF_LOCALE)) { | |
3600 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM); | |
3601 | for (value = 0; value < 256; value++) | |
3602 | if (isALNUM(value)) | |
3603 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
653099ff GS |
3604 | } |
3605 | } | |
8aa23a47 YO |
3606 | else { |
3607 | if (data->start_class->flags & ANYOF_LOCALE) | |
3608 | ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM); | |
3609 | else { | |
3610 | for (value = 0; value < 256; value++) | |
3611 | if (!isALNUM(value)) | |
3612 | ANYOF_BITMAP_SET(data->start_class, value); | |
3613 | } | |
653099ff | 3614 | } |
8aa23a47 YO |
3615 | break; |
3616 | case NALNUML: | |
3617 | if (flags & SCF_DO_STCLASS_AND) { | |
3618 | if (data->start_class->flags & ANYOF_LOCALE) | |
3619 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM); | |
653099ff | 3620 | } |
8aa23a47 YO |
3621 | else { |
3622 | data->start_class->flags |= ANYOF_LOCALE; | |
3623 | ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM); | |
3624 | } | |
3625 | break; | |
3626 | case SPACE: | |
3627 | if (flags & SCF_DO_STCLASS_AND) { | |
3628 | if (!(data->start_class->flags & ANYOF_LOCALE)) { | |
3629 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE); | |
3630 | for (value = 0; value < 256; value++) | |
3631 | if (!isSPACE(value)) | |
3632 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
653099ff GS |
3633 | } |
3634 | } | |
8aa23a47 YO |
3635 | else { |
3636 | if (data->start_class->flags & ANYOF_LOCALE) | |
3637 | ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE); | |
3638 | else { | |
3639 | for (value = 0; value < 256; value++) | |
3640 | if (isSPACE(value)) | |
3641 | ANYOF_BITMAP_SET(data->start_class, value); | |
3642 | } | |
653099ff | 3643 | } |
8aa23a47 YO |
3644 | break; |
3645 | case SPACEL: | |
3646 | if (flags & SCF_DO_STCLASS_AND) { | |
3647 | if (data->start_class->flags & ANYOF_LOCALE) | |
3648 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE); | |
3649 | } | |
3650 | else { | |
3651 | data->start_class->flags |= ANYOF_LOCALE; | |
3652 | ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE); | |
3653 | } | |
3654 | break; | |
3655 | case NSPACE: | |
3656 | if (flags & SCF_DO_STCLASS_AND) { | |
3657 | if (!(data->start_class->flags & ANYOF_LOCALE)) { | |
3658 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE); | |
3659 | for (value = 0; value < 256; value++) | |
3660 | if (isSPACE(value)) | |
3661 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
653099ff | 3662 | } |
8aa23a47 YO |
3663 | } |
3664 | else { | |
3665 | if (data->start_class->flags & ANYOF_LOCALE) | |
3666 | ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE); | |
3667 | else { | |
3668 | for (value = 0; value < 256; value++) | |
3669 | if (!isSPACE(value)) | |
3670 | ANYOF_BITMAP_SET(data->start_class, value); | |
653099ff GS |
3671 | } |
3672 | } | |
8aa23a47 YO |
3673 | break; |
3674 | case NSPACEL: | |
3675 | if (flags & SCF_DO_STCLASS_AND) { | |
3676 | if (data->start_class->flags & ANYOF_LOCALE) { | |
3677 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE); | |
3678 | for (value = 0; value < 256; value++) | |
3679 | if (!isSPACE(value)) | |
3680 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
3681 | } | |
653099ff | 3682 | } |
8aa23a47 YO |
3683 | else { |
3684 | data->start_class->flags |= ANYOF_LOCALE; | |
3685 | ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE); | |
3686 | } | |
3687 | break; | |
3688 | case DIGIT: | |
3689 | if (flags & SCF_DO_STCLASS_AND) { | |
3690 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT); | |
3691 | for (value = 0; value < 256; value++) | |
3692 | if (!isDIGIT(value)) | |
3693 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
3694 | } | |
3695 | else { | |
3696 | if (data->start_class->flags & ANYOF_LOCALE) | |
3697 | ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT); | |
3698 | else { | |
3699 | for (value = 0; value < 256; value++) | |
3700 | if (isDIGIT(value)) | |
3701 | ANYOF_BITMAP_SET(data->start_class, value); | |
3702 | } | |
3703 | } | |
3704 | break; | |
3705 | case NDIGIT: | |
3706 | if (flags & SCF_DO_STCLASS_AND) { | |
3707 | ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT); | |
3708 | for (value = 0; value < 256; value++) | |
3709 | if (isDIGIT(value)) | |
3710 | ANYOF_BITMAP_CLEAR(data->start_class, value); | |
3711 | } | |
3712 | else { | |
3713 | if (data->start_class->flags & ANYOF_LOCALE) | |
3714 | ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT); | |
3715 | else { | |
3716 | for (value = 0; value < 256; value++) | |
3717 | if (!isDIGIT(value)) | |
3718 | ANYOF_BITMAP_SET(data->start_class, value); | |
653099ff GS |
3719 | } |
3720 | } | |
8aa23a47 | 3721 | break; |
e1d1eefb YO |
3722 | CASE_SYNST_FNC(VERTWS); |
3723 | CASE_SYNST_FNC(HORIZWS); | |
3724 | ||
8aa23a47 YO |
3725 | } |
3726 | if (flags & SCF_DO_STCLASS_OR) | |
3727 | cl_and(data->start_class, and_withp); | |
3728 | flags &= ~SCF_DO_STCLASS; | |
3729 | } | |
3730 | } | |
3731 | else if (PL_regkind[OP(scan)] == EOL && flags & SCF_DO_SUBSTR) { | |
3732 | data->flags |= (OP(scan) == MEOL | |
3733 | ? SF_BEFORE_MEOL | |
3734 | : SF_BEFORE_SEOL); | |
3735 | } | |
3736 | else if ( PL_regkind[OP(scan)] == BRANCHJ | |
3737 | /* Lookbehind, or need to calculate parens/evals/stclass: */ | |
3738 | && (scan->flags || data || (flags & SCF_DO_STCLASS)) | |
3739 | && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) { | |
3740 | if ( !PERL_ENABLE_POSITIVE_ASSERTION_STUDY | |
3741 | || OP(scan) == UNLESSM ) | |
3742 | { | |
3743 | /* Negative Lookahead/lookbehind | |
3744 | In this case we can't do fixed string optimisation. | |
3745 | */ | |
1de06328 | 3746 | |
8aa23a47 YO |
3747 | I32 deltanext, minnext, fake = 0; |
3748 | regnode *nscan; | |
3749 | struct regnode_charclass_class intrnl; | |
3750 | int f = 0; | |
1de06328 | 3751 | |
8aa23a47 YO |
3752 | data_fake.flags = 0; |
3753 | if (data) { | |
3754 | data_fake.whilem_c = data->whilem_c; | |
3755 | data_fake.last_closep = data->last_closep; | |
c277df42 | 3756 | } |
8aa23a47 YO |
3757 | else |
3758 | data_fake.last_closep = &fake; | |
58e23c8d | 3759 | data_fake.pos_delta = delta; |
8aa23a47 YO |
3760 | if ( flags & SCF_DO_STCLASS && !scan->flags |
3761 | && OP(scan) == IFMATCH ) { /* Lookahead */ | |
3762 | cl_init(pRExC_state, &intrnl); | |
3763 | data_fake.start_class = &intrnl; | |
3764 | f |= SCF_DO_STCLASS_AND; | |
3765 | } | |
3766 | if (flags & SCF_WHILEM_VISITED_POS) | |
3767 | f |= SCF_WHILEM_VISITED_POS; | |
3768 | next = regnext(scan); | |
3769 | nscan = NEXTOPER(NEXTOPER(scan)); | |
3770 | minnext = study_chunk(pRExC_state, &nscan, minlenp, &deltanext, | |
3771 | last, &data_fake, stopparen, recursed, NULL, f, depth+1); | |
3772 | if (scan->flags) { | |
3773 | if (deltanext) { | |
58e23c8d | 3774 | FAIL("Variable length lookbehind not implemented"); |
8aa23a47 YO |
3775 | } |
3776 | else if (minnext > (I32)U8_MAX) { | |
58e23c8d | 3777 | FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX); |
8aa23a47 YO |
3778 | } |
3779 | scan->flags = (U8)minnext; | |
3780 | } | |
3781 | if (data) { | |
3782 | if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) | |
3783 | pars++; | |
3784 | if (data_fake.flags & SF_HAS_EVAL) | |
3785 | data->flags |= SF_HAS_EVAL; | |
3786 | data->whilem_c = data_fake.whilem_c; | |
3787 | } | |
3788 | if (f & SCF_DO_STCLASS_AND) { | |
906cdd2b HS |
3789 | if (flags & SCF_DO_STCLASS_OR) { |
3790 | /* OR before, AND after: ideally we would recurse with | |
3791 | * data_fake to get the AND applied by study of the | |
3792 | * remainder of the pattern, and then derecurse; | |
3793 | * *** HACK *** for now just treat as "no information". | |
3794 | * See [perl #56690]. | |
3795 | */ | |
3796 | cl_init(pRExC_state, data->start_class); | |
3797 | } else { | |
3798 | /* AND before and after: combine and continue */ | |
3799 | const int was = (data->start_class->flags & ANYOF_EOS); | |
3800 | ||
3801 | cl_and(data->start_class, &intrnl); | |
3802 | if (was) | |
3803 | data->start_class->flags |= ANYOF_EOS; | |
3804 | } | |
8aa23a47 | 3805 | } |
cb434fcc | 3806 | } |
8aa23a47 YO |
3807 | #if PERL_ENABLE_POSITIVE_ASSERTION_STUDY |
3808 | else { | |
3809 | /* Positive Lookahead/lookbehind | |
3810 | In this case we can do fixed string optimisation, | |
3811 | but we must be careful about it. Note in the case of | |
3812 | lookbehind the positions will be offset by the minimum | |
3813 | length of the pattern, something we won't know about | |
3814 | until after the recurse. | |
3815 | */ | |
3816 | I32 deltanext, fake = 0; | |
3817 | regnode *nscan; | |
3818 | struct regnode_charclass_class intrnl; | |
3819 | int f = 0; | |
3820 | /* We use SAVEFREEPV so that when the full compile | |
3821 | is finished perl will clean up the allocated | |
3822 | minlens when its all done. This was we don't | |
3823 | have to worry about freeing them when we know | |
3824 | they wont be used, which would be a pain. | |
3825 | */ | |
3826 | I32 *minnextp; | |
3827 | Newx( minnextp, 1, I32 ); | |
3828 | SAVEFREEPV(minnextp); | |
3829 | ||
3830 | if (data) { | |
3831 | StructCopy(data, &data_fake, scan_data_t); | |
3832 | if ((flags & SCF_DO_SUBSTR) && data->last_found) { | |
3833 | f |= SCF_DO_SUBSTR; | |
3834 | if (scan->flags) | |
304ee84b | 3835 | SCAN_COMMIT(pRExC_state, &data_fake,minlenp); |
8aa23a47 YO |
3836 | data_fake.last_found=newSVsv(data->last_found); |
3837 | } | |
3838 | } | |
3839 | else | |
3840 | data_fake.last_closep = &fake; | |
3841 | data_fake.flags = 0; | |
58e23c8d | 3842 | data_fake.pos_delta = delta; |
8aa23a47 YO |
3843 | if (is_inf) |
3844 | data_fake.flags |= SF_IS_INF; | |
3845 | if ( flags & SCF_DO_STCLASS && !scan->flags | |
3846 | && OP(scan) == IFMATCH ) { /* Lookahead */ | |
3847 | cl_init(pRExC_state, &intrnl); | |
3848 | data_fake.start_class = &intrnl; | |
3849 | f |= SCF_DO_STCLASS_AND; | |
3850 | } | |
3851 | if (flags & SCF_WHILEM_VISITED_POS) | |
3852 | f |= SCF_WHILEM_VISITED_POS; | |
3853 | next = regnext(scan); | |
3854 | nscan = NEXTOPER(NEXTOPER(scan)); | |
3855 | ||
3856 | *minnextp = study_chunk(pRExC_state, &nscan, minnextp, &deltanext, | |
3857 | last, &data_fake, stopparen, recursed, NULL, f,depth+1); | |
3858 | if (scan->flags) { | |
3859 | if (deltanext) { | |
58e23c8d | 3860 | FAIL("Variable length lookbehind not implemented"); |
8aa23a47 YO |
3861 | } |
3862 | else if (*minnextp > (I32)U8_MAX) { | |
58e23c8d | 3863 | FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX); |
8aa23a47 YO |
3864 | } |
3865 | scan->flags = (U8)*minnextp; | |
3866 | } | |
3867 | ||
3868 | *minnextp += min; | |
3869 | ||
3870 | if (f & SCF_DO_STCLASS_AND) { | |
3871 | const int was = (data->start_class->flags & ANYOF_EOS); | |
3872 | ||
3873 | cl_and(data->start_class, &intrnl); | |
3874 | if (was) | |
3875 | data->start_class->flags |= ANYOF_EOS; | |
3876 | } | |
3877 | if (data) { | |
3878 | if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) | |
3879 | pars++; | |
3880 | if (data_fake.flags & SF_HAS_EVAL) | |
3881 | data->flags |= SF_HAS_EVAL; | |
3882 | data->whilem_c = data_fake.whilem_c; | |
3883 | if ((flags & SCF_DO_SUBSTR) && data_fake.last_found) { | |
3884 | if (RExC_rx->minlen<*minnextp) | |
3885 | RExC_rx->minlen=*minnextp; | |
304ee84b | 3886 | SCAN_COMMIT(pRExC_state, &data_fake, minnextp); |
8aa23a47 YO |
3887 | SvREFCNT_dec(data_fake.last_found); |
3888 | ||
3889 | if ( data_fake.minlen_fixed != minlenp ) | |
3890 | { | |
3891 | data->offset_fixed= data_fake.offset_fixed; | |
3892 | data->minlen_fixed= data_fake.minlen_fixed; | |
3893 | data->lookbehind_fixed+= scan->flags; | |
3894 | } | |
3895 | if ( data_fake.minlen_float != minlenp ) | |
3896 | { | |
3897 | data->minlen_float= data_fake.minlen_float; | |
3898 | data->offset_float_min=data_fake.offset_float_min; | |
3899 | data->offset_float_max=data_fake.offset_float_max; | |
3900 | data->lookbehind_float+= scan->flags; | |
3901 | } | |
3902 | } | |
3903 | } | |
3904 | ||
3905 | ||
40d049e4 | 3906 | } |
8aa23a47 YO |
3907 | #endif |
3908 | } | |
3909 | else if (OP(scan) == OPEN) { | |
3910 | if (stopparen != (I32)ARG(scan)) | |
3911 | pars++; | |
3912 | } | |
3913 | else if (OP(scan) == CLOSE) { | |
3914 | if (stopparen == (I32)ARG(scan)) { | |
3915 | break; | |
3916 | } | |
3917 | if ((I32)ARG(scan) == is_par) { | |
3918 | next = regnext(scan); | |
b515a41d | 3919 | |
8aa23a47 YO |
3920 | if ( next && (OP(next) != WHILEM) && next < last) |
3921 | is_par = 0; /* Disable optimization */ | |
40d049e4 | 3922 | } |
8aa23a47 YO |
3923 | if (data) |
3924 | *(data->last_closep) = ARG(scan); | |
3925 | } | |
3926 | else if (OP(scan) == EVAL) { | |
c277df42 IZ |
3927 | if (data) |
3928 | data->flags |= SF_HAS_EVAL; | |
8aa23a47 YO |
3929 | } |
3930 | else if ( PL_regkind[OP(scan)] == ENDLIKE ) { | |
3931 | if (flags & SCF_DO_SUBSTR) { | |
304ee84b | 3932 | SCAN_COMMIT(pRExC_state,data,minlenp); |
8aa23a47 | 3933 | flags &= ~SCF_DO_SUBSTR; |
40d049e4 | 3934 | } |
8aa23a47 YO |
3935 | if (data && OP(scan)==ACCEPT) { |
3936 | data->flags |= SCF_SEEN_ACCEPT; | |
3937 | if (stopmin > min) | |
3938 | stopmin = min; | |
e2e6a0f1 | 3939 | } |
8aa23a47 YO |
3940 | } |
3941 | else if (OP(scan) == LOGICAL && scan->flags == 2) /* Embedded follows */ | |
3942 | { | |
0f5d15d6 | 3943 | if (flags & SCF_DO_SUBSTR) { |
304ee84b | 3944 | SCAN_COMMIT(pRExC_state,data,minlenp); |
0f5d15d6 IZ |
3945 | data->longest = &(data->longest_float); |
3946 | } | |
3947 | is_inf = is_inf_internal = 1; | |
653099ff | 3948 | if (flags & SCF_DO_STCLASS_OR) /* Allow everything */ |
830247a4 | 3949 | cl_anything(pRExC_state, data->start_class); |
96776eda | 3950 | flags &= ~SCF_DO_STCLASS; |
8aa23a47 | 3951 | } |
58e23c8d | 3952 | else if (OP(scan) == GPOS) { |
bbe252da | 3953 | if (!(RExC_rx->extflags & RXf_GPOS_FLOAT) && |
58e23c8d YO |
3954 | !(delta || is_inf || (data && data->pos_delta))) |
3955 | { | |
bbe252da YO |
3956 | if (!(RExC_rx->extflags & RXf_ANCH) && (flags & SCF_DO_SUBSTR)) |
3957 | RExC_rx->extflags |= RXf_ANCH_GPOS; | |
58e23c8d YO |
3958 | if (RExC_rx->gofs < (U32)min) |
3959 | RExC_rx->gofs = min; | |
3960 | } else { | |
bbe252da | 3961 | RExC_rx->extflags |= RXf_GPOS_FLOAT; |
58e23c8d YO |
3962 | RExC_rx->gofs = 0; |
3963 | } | |
3964 | } | |
786e8c11 | 3965 | #ifdef TRIE_STUDY_OPT |
40d049e4 | 3966 | #ifdef FULL_TRIE_STUDY |
8aa23a47 YO |
3967 | else if (PL_regkind[OP(scan)] == TRIE) { |
3968 | /* NOTE - There is similar code to this block above for handling | |
3969 | BRANCH nodes on the initial study. If you change stuff here | |
3970 | check there too. */ | |
3971 | regnode *trie_node= scan; | |
3972 | regnode *tail= regnext(scan); | |
f8fc2ecf | 3973 | reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ]; |
8aa23a47 YO |
3974 | I32 max1 = 0, min1 = I32_MAX; |
3975 | struct regnode_charclass_class accum; | |
3976 | ||
3977 | if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */ | |
304ee84b | 3978 | SCAN_COMMIT(pRExC_state, data,minlenp); /* Cannot merge strings after this. */ |
8aa23a47 YO |
3979 | if (flags & SCF_DO_STCLASS) |
3980 | cl_init_zero(pRExC_state, &accum); | |
3981 | ||
3982 | if (!trie->jump) { | |
3983 | min1= trie->minlen; | |
3984 | max1= trie->maxlen; | |
3985 | } else { | |
3986 | const regnode *nextbranch= NULL; | |
3987 | U32 word; | |
3988 | ||
3989 | for ( word=1 ; word <= trie->wordcount ; word++) | |
3990 | { | |
3991 | I32 deltanext=0, minnext=0, f = 0, fake; | |
3992 | struct regnode_charclass_class this_class; | |
3993 | ||
3994 | data_fake.flags = 0; | |
3995 | if (data) { | |
3996 | data_fake.whilem_c = data->whilem_c; | |
3997 | data_fake.last_closep = data->last_closep; | |
3998 | } | |
3999 | else | |
4000 | data_fake.last_closep = &fake; | |
58e23c8d | 4001 | data_fake.pos_delta = delta; |
8aa23a47 YO |
4002 | if (flags & SCF_DO_STCLASS) { |
4003 | cl_init(pRExC_state, &this_class); | |
4004 | data_fake.start_class = &this_class; | |
4005 | f = SCF_DO_STCLASS_AND; | |
4006 | } | |
4007 | if (flags & SCF_WHILEM_VISITED_POS) | |
4008 | f |= SCF_WHILEM_VISITED_POS; | |
4009 | ||
4010 | if (trie->jump[word]) { | |
4011 | if (!nextbranch) | |
4012 | nextbranch = trie_node + trie->jump[0]; | |
4013 | scan= trie_node + trie->jump[word]; | |
4014 | /* We go from the jump point to the branch that follows | |
4015 | it. Note this means we need the vestigal unused branches | |
4016 | even though they arent otherwise used. | |
4017 | */ | |
4018 | minnext = study_chunk(pRExC_state, &scan, minlenp, | |
4019 | &deltanext, (regnode *)nextbranch, &data_fake, | |
4020 | stopparen, recursed, NULL, f,depth+1); | |
4021 | } | |
4022 | if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH) | |
4023 | nextbranch= regnext((regnode*)nextbranch); | |
4024 | ||
4025 | if (min1 > (I32)(minnext + trie->minlen)) | |
4026 | min1 = minnext + trie->minlen; | |
4027 | if (max1 < (I32)(minnext + deltanext + trie->maxlen)) | |
4028 | max1 = minnext + deltanext + trie->maxlen; | |
4029 | if (deltanext == I32_MAX) | |
4030 | is_inf = is_inf_internal = 1; | |
4031 | ||
4032 | if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) | |
4033 | pars++; | |
4034 | if (data_fake.flags & SCF_SEEN_ACCEPT) { | |
4035 | if ( stopmin > min + min1) | |
4036 | stopmin = min + min1; | |
4037 | flags &= ~SCF_DO_SUBSTR; | |
4038 | if (data) | |
4039 | data->flags |= SCF_SEEN_ACCEPT; | |
4040 | } | |
4041 | if (data) { | |
4042 | if (data_fake.flags & SF_HAS_EVAL) | |
4043 | data->flags |= SF_HAS_EVAL; | |
4044 | data->whilem_c = data_fake.whilem_c; | |
4045 | } | |
4046 | if (flags & SCF_DO_STCLASS) | |
4047 | cl_or(pRExC_state, &accum, &this_class); | |
4048 | } | |
4049 | } | |
4050 | if (flags & SCF_DO_SUBSTR) { | |
4051 | data->pos_min += min1; | |
4052 | data->pos_delta += max1 - min1; | |
4053 | if (max1 != min1 || is_inf) | |
4054 | data->longest = &(data->longest_float); | |
4055 | } | |
4056 | min += min1; | |
4057 | delta += max1 - min1; | |
4058 | if (flags & SCF_DO_STCLASS_OR) { | |
4059 | cl_or(pRExC_state, data->start_class, &accum); | |
4060 | if (min1) { | |
4061 | cl_and(data->start_class, and_withp); | |
4062 | flags &= ~SCF_DO_STCLASS; | |
4063 | } | |
4064 | } | |
4065 | else if (flags & SCF_DO_STCLASS_AND) { | |
4066 | if (min1) { | |
4067 | cl_and(data->start_class, &accum); | |
4068 | flags &= ~SCF_DO_STCLASS; | |
4069 | } | |
4070 | else { | |
4071 | /* Switch to OR mode: cache the old value of | |
4072 | * data->start_class */ | |
4073 | INIT_AND_WITHP; | |
4074 | StructCopy(data->start_class, and_withp, | |
4075 | struct regnode_charclass_class); | |
4076 | flags &= ~SCF_DO_STCLASS_AND; | |
4077 | StructCopy(&accum, data->start_class, | |
4078 | struct regnode_charclass_class); | |
4079 | flags |= SCF_DO_STCLASS_OR; | |
4080 | data->start_class->flags |= ANYOF_EOS; | |
4081 | } | |
4082 | } | |
4083 | scan= tail; | |
4084 | continue; | |
4085 | } | |
786e8c11 | 4086 | #else |
8aa23a47 | 4087 | else if (PL_regkind[OP(scan)] == TRIE) { |
f8fc2ecf | 4088 | reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ]; |
8aa23a47 YO |
4089 | U8*bang=NULL; |
4090 | ||
4091 | min += trie->minlen; | |
4092 | delta += (trie->maxlen - trie->minlen); | |
4093 | flags &= ~SCF_DO_STCLASS; /* xxx */ | |
4094 | if (flags & SCF_DO_SUBSTR) { | |
304ee84b | 4095 | SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot expect anything... */ |
8aa23a47 YO |
4096 | data->pos_min += trie->minlen; |
4097 | data->pos_delta += (trie->maxlen - trie->minlen); | |
4098 | if (trie->maxlen != trie->minlen) | |
4099 | data->longest = &(data->longest_float); | |
4100 | } | |
4101 | if (trie->jump) /* no more substrings -- for now /grr*/ | |
4102 | flags &= ~SCF_DO_SUBSTR; | |
b515a41d | 4103 | } |
8aa23a47 YO |
4104 | #endif /* old or new */ |
4105 | #endif /* TRIE_STUDY_OPT */ | |
e1d1eefb | 4106 | |
8aa23a47 YO |
4107 | /* Else: zero-length, ignore. */ |
4108 | scan = regnext(scan); | |
4109 | } | |
4110 | if (frame) { | |
4111 | last = frame->last; | |
4112 | scan = frame->next; | |
4113 | stopparen = frame->stop; | |
4114 | frame = frame->prev; | |
4115 | goto fake_study_recurse; | |
c277df42 IZ |
4116 | } |
4117 | ||
4118 | finish: | |
8aa23a47 | 4119 | assert(!frame); |
304ee84b | 4120 | DEBUG_STUDYDATA("pre-fin:",data,depth); |
8aa23a47 | 4121 | |
c277df42 | 4122 | *scanp = scan; |
aca2d497 | 4123 | *deltap = is_inf_internal ? I32_MAX : delta; |
b81d288d | 4124 | if (flags & SCF_DO_SUBSTR && is_inf) |
c277df42 | 4125 | data->pos_delta = I32_MAX - data->pos_min; |
786e8c11 | 4126 | if (is_par > (I32)U8_MAX) |
c277df42 IZ |
4127 | is_par = 0; |
4128 | if (is_par && pars==1 && data) { | |
4129 | data->flags |= SF_IN_PAR; | |
4130 | data->flags &= ~SF_HAS_PAR; | |
a0ed51b3 LW |
4131 | } |
4132 | else if (pars && data) { | |
c277df42 IZ |
4133 | data->flags |= SF_HAS_PAR; |
4134 | data->flags &= ~SF_IN_PAR; | |
4135 | } | |
653099ff | 4136 | if (flags & SCF_DO_STCLASS_OR) |
40d049e4 | 4137 | cl_and(data->start_class, and_withp); |
786e8c11 YO |
4138 | if (flags & SCF_TRIE_RESTUDY) |
4139 | data->flags |= SCF_TRIE_RESTUDY; | |
1de06328 | 4140 | |
304ee84b | 4141 | DEBUG_STUDYDATA("post-fin:",data,depth); |
1de06328 | 4142 | |
e2e6a0f1 | 4143 | return min < stopmin ? min : stopmin; |
c277df42 IZ |
4144 | } |
4145 | ||
2eccd3b2 NC |
4146 | STATIC U32 |
4147 | S_add_data(RExC_state_t *pRExC_state, U32 n, const char *s) | |
c277df42 | 4148 | { |
4a4e7719 NC |
4149 | U32 count = RExC_rxi->data ? RExC_rxi->data->count : 0; |
4150 | ||
7918f24d NC |
4151 | PERL_ARGS_ASSERT_ADD_DATA; |
4152 | ||
4a4e7719 NC |
4153 | Renewc(RExC_rxi->data, |
4154 | sizeof(*RExC_rxi->data) + sizeof(void*) * (count + n - 1), | |
4155 | char, struct reg_data); | |
4156 | if(count) | |
f8fc2ecf | 4157 | Renew(RExC_rxi->data->what, count + n, U8); |
4a4e7719 | 4158 | else |
f8fc2ecf | 4159 | Newx(RExC_rxi->data->what, n, U8); |
4a4e7719 NC |
4160 | RExC_rxi->data->count = count + n; |
4161 | Copy(s, RExC_rxi->data->what + count, n, U8); | |
4162 | return count; | |
c277df42 IZ |
4163 | } |
4164 | ||
f8149455 | 4165 | /*XXX: todo make this not included in a non debugging perl */ |
76234dfb | 4166 | #ifndef PERL_IN_XSUB_RE |
d88dccdf | 4167 | void |
864dbfa3 | 4168 | Perl_reginitcolors(pTHX) |
d88dccdf | 4169 | { |
97aff369 | 4170 | dVAR; |
1df70142 | 4171 | const char * const s = PerlEnv_getenv("PERL_RE_COLORS"); |
d88dccdf | 4172 | if (s) { |
1df70142 AL |
4173 | char *t = savepv(s); |
4174 | int i = 0; | |
4175 | PL_colors[0] = t; | |
d88dccdf | 4176 | while (++i < 6) { |
1df70142 AL |
4177 | t = strchr(t, '\t'); |
4178 | if (t) { | |
4179 | *t = '\0'; | |
4180 | PL_colors[i] = ++t; | |
d88dccdf IZ |
4181 | } |
4182 | else | |
1df70142 | 4183 | PL_colors[i] = t = (char *)""; |
d88dccdf IZ |
4184 | } |
4185 | } else { | |
1df70142 | 4186 | int i = 0; |
b81d288d | 4187 | while (i < 6) |
06b5626a | 4188 | PL_colors[i++] = (char *)""; |
d88dccdf IZ |
4189 | } |
4190 | PL_colorset = 1; | |
4191 | } | |
76234dfb | 4192 | #endif |
8615cb43 | 4193 | |
07be1b83 | 4194 | |
786e8c11 YO |
4195 | #ifdef TRIE_STUDY_OPT |
4196 | #define CHECK_RESTUDY_GOTO \ | |
4197 | if ( \ | |
4198 | (data.flags & SCF_TRIE_RESTUDY) \ | |
4199 | && ! restudied++ \ | |
4200 | ) goto reStudy | |
4201 | #else | |
4202 | #define CHECK_RESTUDY_GOTO | |
4203 | #endif | |
f9f4320a | 4204 | |
a687059c | 4205 | /* |
e50aee73 | 4206 | - pregcomp - compile a regular expression into internal code |
a687059c LW |
4207 | * |
4208 | * We can't allocate space until we know how big the compiled form will be, | |
4209 | * but we can't compile it (and thus know how big it is) until we've got a | |
4210 | * place to put the code. So we cheat: we compile it twice, once with code | |
4211 | * generation turned off and size counting turned on, and once "for real". | |
4212 | * This also means that we don't allocate space until we are sure that the | |
4213 | * thing really will compile successfully, and we never have to move the | |
4214 | * code and thus invalidate pointers into it. (Note that it has to be in | |
4215 | * one piece because free() must be able to free it all.) [NB: not true in perl] | |
4216 | * | |
4217 | * Beware that the optimization-preparation code in here knows about some | |
4218 | * of the structure of the compiled regexp. [I'll say.] | |
4219 | */ | |
b9b4dddf YO |
4220 | |
4221 | ||
4222 | ||
f9f4320a | 4223 | #ifndef PERL_IN_XSUB_RE |
f9f4320a YO |
4224 | #define RE_ENGINE_PTR &PL_core_reg_engine |
4225 | #else | |
f9f4320a YO |
4226 | extern const struct regexp_engine my_reg_engine; |
4227 | #define RE_ENGINE_PTR &my_reg_engine | |
4228 | #endif | |
6d5c990f RGS |
4229 | |
4230 | #ifndef PERL_IN_XSUB_RE | |
3ab4a224 | 4231 | REGEXP * |
1593ad57 | 4232 | Perl_pregcomp(pTHX_ SV * const pattern, const U32 flags) |
a687059c | 4233 | { |
97aff369 | 4234 | dVAR; |
6d5c990f | 4235 | HV * const table = GvHV(PL_hintgv); |
7918f24d NC |
4236 | |
4237 | PERL_ARGS_ASSERT_PREGCOMP; | |
4238 | ||
f9f4320a YO |
4239 | /* Dispatch a request to compile a regexp to correct |
4240 | regexp engine. */ | |
f9f4320a YO |
4241 | if (table) { |
4242 | SV **ptr= hv_fetchs(table, "regcomp", FALSE); | |
6d5c990f | 4243 | GET_RE_DEBUG_FLAGS_DECL; |
1e2e3d02 | 4244 | if (ptr && SvIOK(*ptr) && SvIV(*ptr)) { |
f9f4320a YO |
4245 | const regexp_engine *eng=INT2PTR(regexp_engine*,SvIV(*ptr)); |
4246 | DEBUG_COMPILE_r({ | |
8d8756e7 | 4247 | PerlIO_printf(Perl_debug_log, "Using engine %"UVxf"\n", |
f9f4320a YO |
4248 | SvIV(*ptr)); |
4249 | }); | |
3ab4a224 | 4250 | return CALLREGCOMP_ENG(eng, pattern, flags); |
f9f4320a | 4251 | } |
b9b4dddf | 4252 | } |
3ab4a224 | 4253 | return Perl_re_compile(aTHX_ pattern, flags); |
2a5d9b1d | 4254 | } |
6d5c990f | 4255 | #endif |
2a5d9b1d | 4256 | |
3ab4a224 | 4257 | REGEXP * |
1593ad57 | 4258 | Perl_re_compile(pTHX_ SV * const pattern, U32 pm_flags) |
2a5d9b1d RGS |
4259 | { |
4260 | dVAR; | |
288b8c02 NC |
4261 | REGEXP *rx; |
4262 | struct regexp *r; | |
f8fc2ecf | 4263 | register regexp_internal *ri; |
3ab4a224 | 4264 | STRLEN plen; |
1593ad57 | 4265 | char *exp = SvPV(pattern, plen); |
3ab4a224 | 4266 | char* xend = exp + plen; |
c277df42 | 4267 | regnode *scan; |
a0d0e21e | 4268 | I32 flags; |
a0d0e21e LW |
4269 | I32 minlen = 0; |
4270 | I32 sawplus = 0; | |
4271 | I32 sawopen = 0; | |
2c2d71f5 | 4272 | scan_data_t data; |
830247a4 | 4273 | RExC_state_t RExC_state; |
be8e71aa | 4274 | RExC_state_t * const pRExC_state = &RExC_state; |
07be1b83 YO |
4275 | #ifdef TRIE_STUDY_OPT |
4276 | int restudied= 0; | |
4277 | RExC_state_t copyRExC_state; | |
4278 | #endif | |
2a5d9b1d | 4279 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
4280 | |
4281 | PERL_ARGS_ASSERT_RE_COMPILE; | |
4282 | ||
6d5c990f | 4283 | DEBUG_r(if (!PL_colorset) reginitcolors()); |
a0d0e21e | 4284 | |
b9ad30b4 | 4285 | RExC_utf8 = RExC_orig_utf8 = SvUTF8(pattern); |
a0ed51b3 | 4286 | |
a3621e74 | 4287 | DEBUG_COMPILE_r({ |
ab3bbdeb YO |
4288 | SV *dsv= sv_newmortal(); |
4289 | RE_PV_QUOTED_DECL(s, RExC_utf8, | |
3ab4a224 | 4290 | dsv, exp, plen, 60); |
ab3bbdeb YO |
4291 | PerlIO_printf(Perl_debug_log, "%sCompiling REx%s %s\n", |
4292 | PL_colors[4],PL_colors[5],s); | |
a5961de5 | 4293 | }); |
02daf0ab YO |
4294 | |
4295 | redo_first_pass: | |
4296 | RExC_precomp = exp; | |
c737faaf | 4297 | RExC_flags = pm_flags; |
830247a4 | 4298 | RExC_sawback = 0; |
bbce6d69 | 4299 | |
830247a4 IZ |
4300 | RExC_seen = 0; |
4301 | RExC_seen_zerolen = *exp == '^' ? -1 : 0; | |
4302 | RExC_seen_evals = 0; | |
4303 | RExC_extralen = 0; | |
c277df42 | 4304 | |
bbce6d69 | 4305 | /* First pass: determine size, legality. */ |
830247a4 | 4306 | RExC_parse = exp; |
fac92740 | 4307 | RExC_start = exp; |
830247a4 IZ |
4308 | RExC_end = xend; |
4309 | RExC_naughty = 0; | |
4310 | RExC_npar = 1; | |
e2e6a0f1 | 4311 | RExC_nestroot = 0; |
830247a4 IZ |
4312 | RExC_size = 0L; |
4313 | RExC_emit = &PL_regdummy; | |
4314 | RExC_whilem_seen = 0; | |
40d049e4 YO |
4315 | RExC_open_parens = NULL; |
4316 | RExC_close_parens = NULL; | |
4317 | RExC_opend = NULL; | |
81714fb9 | 4318 | RExC_paren_names = NULL; |
1f1031fe YO |
4319 | #ifdef DEBUGGING |
4320 | RExC_paren_name_list = NULL; | |
4321 | #endif | |
40d049e4 YO |
4322 | RExC_recurse = NULL; |
4323 | RExC_recurse_count = 0; | |
81714fb9 | 4324 | |
85ddcde9 JH |
4325 | #if 0 /* REGC() is (currently) a NOP at the first pass. |
4326 | * Clever compilers notice this and complain. --jhi */ | |
830247a4 | 4327 | REGC((U8)REG_MAGIC, (char*)RExC_emit); |
85ddcde9 | 4328 | #endif |
3dab1dad YO |
4329 | DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "Starting first pass (sizing)\n")); |
4330 | if (reg(pRExC_state, 0, &flags,1) == NULL) { | |
c445ea15 | 4331 | RExC_precomp = NULL; |
a0d0e21e LW |
4332 | return(NULL); |
4333 | } | |
02daf0ab | 4334 | if (RExC_utf8 && !RExC_orig_utf8) { |
38a44b82 | 4335 | /* It's possible to write a regexp in ascii that represents Unicode |
02daf0ab YO |
4336 | codepoints outside of the byte range, such as via \x{100}. If we |
4337 | detect such a sequence we have to convert the entire pattern to utf8 | |
4338 | and then recompile, as our sizing calculation will have been based | |
4339 | on 1 byte == 1 character, but we will need to use utf8 to encode | |
4340 | at least some part of the pattern, and therefore must convert the whole | |
4341 | thing. | |
4342 | XXX: somehow figure out how to make this less expensive... | |
4343 | -- dmq */ | |
3ab4a224 | 4344 | STRLEN len = plen; |
02daf0ab YO |
4345 | DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, |
4346 | "UTF8 mismatch! Converting to utf8 for resizing and compile\n")); | |
4347 | exp = (char*)Perl_bytes_to_utf8(aTHX_ (U8*)exp, &len); | |
4348 | xend = exp + len; | |
4349 | RExC_orig_utf8 = RExC_utf8; | |
4350 | SAVEFREEPV(exp); | |
4351 | goto redo_first_pass; | |
4352 | } | |
07be1b83 | 4353 | DEBUG_PARSE_r({ |
81714fb9 YO |
4354 | PerlIO_printf(Perl_debug_log, |
4355 | "Required size %"IVdf" nodes\n" | |
4356 | "Starting second pass (creation)\n", | |
4357 | (IV)RExC_size); | |
07be1b83 YO |
4358 | RExC_lastnum=0; |
4359 | RExC_lastparse=NULL; | |
4360 | }); | |
c277df42 IZ |
4361 | /* Small enough for pointer-storage convention? |
4362 | If extralen==0, this means that we will not need long jumps. */ | |
830247a4 IZ |
4363 | if (RExC_size >= 0x10000L && RExC_extralen) |
4364 | RExC_size += RExC_extralen; | |
c277df42 | 4365 | else |
830247a4 IZ |
4366 | RExC_extralen = 0; |
4367 | if (RExC_whilem_seen > 15) | |
4368 | RExC_whilem_seen = 15; | |
a0d0e21e | 4369 | |
f9f4320a YO |
4370 | /* Allocate space and zero-initialize. Note, the two step process |
4371 | of zeroing when in debug mode, thus anything assigned has to | |
4372 | happen after that */ | |
d2f13c59 | 4373 | rx = (REGEXP*) newSV_type(SVt_REGEXP); |
288b8c02 | 4374 | r = (struct regexp*)SvANY(rx); |
f8fc2ecf YO |
4375 | Newxc(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode), |
4376 | char, regexp_internal); | |
4377 | if ( r == NULL || ri == NULL ) | |
b45f050a | 4378 | FAIL("Regexp out of space"); |
0f79a09d GS |
4379 | #ifdef DEBUGGING |
4380 | /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */ | |
f8fc2ecf | 4381 | Zero(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode), char); |
58e23c8d | 4382 | #else |
f8fc2ecf YO |
4383 | /* bulk initialize base fields with 0. */ |
4384 | Zero(ri, sizeof(regexp_internal), char); | |
0f79a09d | 4385 | #endif |
58e23c8d YO |
4386 | |
4387 | /* non-zero initialization begins here */ | |
f8fc2ecf | 4388 | RXi_SET( r, ri ); |
f9f4320a | 4389 | r->engine= RE_ENGINE_PTR; |
c737faaf | 4390 | r->extflags = pm_flags; |
bcdf7404 | 4391 | { |
f7819f85 | 4392 | bool has_p = ((r->extflags & RXf_PMf_KEEPCOPY) == RXf_PMf_KEEPCOPY); |
bcdf7404 YO |
4393 | bool has_minus = ((r->extflags & RXf_PMf_STD_PMMOD) != RXf_PMf_STD_PMMOD); |
4394 | bool has_runon = ((RExC_seen & REG_SEEN_RUN_ON_COMMENT)==REG_SEEN_RUN_ON_COMMENT); | |
14f3b9f2 NC |
4395 | U16 reganch = (U16)((r->extflags & RXf_PMf_STD_PMMOD) |
4396 | >> RXf_PMf_STD_PMMOD_SHIFT); | |
bcdf7404 YO |
4397 | const char *fptr = STD_PAT_MODS; /*"msix"*/ |
4398 | char *p; | |
9d17798d | 4399 | const STRLEN wraplen = plen + has_minus + has_p + has_runon |
bcdf7404 YO |
4400 | + (sizeof(STD_PAT_MODS) - 1) |
4401 | + (sizeof("(?:)") - 1); | |
4402 | ||
ad64d0ec | 4403 | p = sv_grow(MUTABLE_SV(rx), wraplen + 1); |
9d17798d | 4404 | SvCUR_set(rx, wraplen); |
f7c278bf | 4405 | SvPOK_on(rx); |
8f6ae13c | 4406 | SvFLAGS(rx) |= SvUTF8(pattern); |
bcdf7404 | 4407 | *p++='('; *p++='?'; |
f7819f85 A |
4408 | if (has_p) |
4409 | *p++ = KEEPCOPY_PAT_MOD; /*'p'*/ | |
bcdf7404 YO |
4410 | { |
4411 | char *r = p + (sizeof(STD_PAT_MODS) - 1) + has_minus - 1; | |
4412 | char *colon = r + 1; | |
4413 | char ch; | |
4414 | ||
4415 | while((ch = *fptr++)) { | |
4416 | if(reganch & 1) | |
4417 | *p++ = ch; | |
4418 | else | |
4419 | *r-- = ch; | |
4420 | reganch >>= 1; | |
4421 | } | |
4422 | if(has_minus) { | |
4423 | *r = '-'; | |
4424 | p = colon; | |
4425 | } | |
4426 | } | |
4427 | ||
28d8d7f4 | 4428 | *p++ = ':'; |
bb661a58 | 4429 | Copy(RExC_precomp, p, plen, char); |
efd26800 NC |
4430 | assert ((RX_WRAPPED(rx) - p) < 16); |
4431 | r->pre_prefix = p - RX_WRAPPED(rx); | |
bb661a58 | 4432 | p += plen; |
bcdf7404 | 4433 | if (has_runon) |
28d8d7f4 YO |
4434 | *p++ = '\n'; |
4435 | *p++ = ')'; | |
4436 | *p = 0; | |
bcdf7404 YO |
4437 | } |
4438 | ||
bbe252da | 4439 | r->intflags = 0; |
830247a4 | 4440 | r->nparens = RExC_npar - 1; /* set early to validate backrefs */ |
81714fb9 | 4441 | |
6bda09f9 | 4442 | if (RExC_seen & REG_SEEN_RECURSE) { |
40d049e4 YO |
4443 | Newxz(RExC_open_parens, RExC_npar,regnode *); |
4444 | SAVEFREEPV(RExC_open_parens); | |
4445 | Newxz(RExC_close_parens,RExC_npar,regnode *); | |
4446 | SAVEFREEPV(RExC_close_parens); | |
6bda09f9 YO |
4447 | } |
4448 | ||
4449 | /* Useful during FAIL. */ | |
7122b237 YO |
4450 | #ifdef RE_TRACK_PATTERN_OFFSETS |
4451 | Newxz(ri->u.offsets, 2*RExC_size+1, U32); /* MJD 20001228 */ | |
a3621e74 | 4452 | DEBUG_OFFSETS_r(PerlIO_printf(Perl_debug_log, |
2af232bd | 4453 | "%s %"UVuf" bytes for offset annotations.\n", |
7122b237 | 4454 | ri->u.offsets ? "Got" : "Couldn't get", |
392fbf5d | 4455 | (UV)((2*RExC_size+1) * sizeof(U32)))); |
7122b237 YO |
4456 | #endif |
4457 | SetProgLen(ri,RExC_size); | |
288b8c02 | 4458 | RExC_rx_sv = rx; |
830247a4 | 4459 | RExC_rx = r; |
f8fc2ecf | 4460 | RExC_rxi = ri; |
bbce6d69 | 4461 | |
4462 | /* Second pass: emit code. */ | |
c737faaf | 4463 | RExC_flags = pm_flags; /* don't let top level (?i) bleed */ |
830247a4 IZ |
4464 | RExC_parse = exp; |
4465 | RExC_end = xend; | |
4466 | RExC_naughty = 0; | |
4467 | RExC_npar = 1; | |
f8fc2ecf YO |
4468 | RExC_emit_start = ri->program; |
4469 | RExC_emit = ri->program; | |
3b57cd43 YO |
4470 | RExC_emit_bound = ri->program + RExC_size + 1; |
4471 | ||
2cd61cdb | 4472 | /* Store the count of eval-groups for security checks: */ |
f8149455 | 4473 | RExC_rx->seen_evals = RExC_seen_evals; |
830247a4 | 4474 | REGC((U8)REG_MAGIC, (char*) RExC_emit++); |
80757612 | 4475 | if (reg(pRExC_state, 0, &flags,1) == NULL) { |
288b8c02 | 4476 | ReREFCNT_dec(rx); |
a0d0e21e | 4477 | return(NULL); |
80757612 | 4478 | } |
07be1b83 YO |
4479 | /* XXXX To minimize changes to RE engine we always allocate |
4480 | 3-units-long substrs field. */ | |
4481 | Newx(r->substrs, 1, struct reg_substr_data); | |
40d049e4 YO |
4482 | if (RExC_recurse_count) { |
4483 | Newxz(RExC_recurse,RExC_recurse_count,regnode *); | |
4484 | SAVEFREEPV(RExC_recurse); | |
4485 | } | |
a0d0e21e | 4486 | |
07be1b83 | 4487 | reStudy: |
1de06328 | 4488 | r->minlen = minlen = sawplus = sawopen = 0; |
07be1b83 | 4489 | Zero(r->substrs, 1, struct reg_substr_data); |
a3621e74 | 4490 | |
07be1b83 | 4491 | #ifdef TRIE_STUDY_OPT |
0934c9d9 SH |
4492 | if (!restudied) { |
4493 | StructCopy(&zero_scan_data, &data, scan_data_t); | |
4494 | copyRExC_state = RExC_state; | |
4495 | } else { | |
5d458dd8 | 4496 | U32 seen=RExC_seen; |
07be1b83 | 4497 | DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log,"Restudying\n")); |
5d458dd8 YO |
4498 | |
4499 | RExC_state = copyRExC_state; | |
4500 | if (seen & REG_TOP_LEVEL_BRANCHES) | |
4501 | RExC_seen |= REG_TOP_LEVEL_BRANCHES; | |
4502 | else | |
4503 | RExC_seen &= ~REG_TOP_LEVEL_BRANCHES; | |
1de06328 | 4504 | if (data.last_found) { |
07be1b83 | 4505 | SvREFCNT_dec(data.longest_fixed); |
07be1b83 | 4506 | SvREFCNT_dec(data.longest_float); |
07be1b83 | 4507 | SvREFCNT_dec(data.last_found); |
1de06328 | 4508 | } |
40d049e4 | 4509 | StructCopy(&zero_scan_data, &data, scan_data_t); |
07be1b83 | 4510 | } |
40d049e4 YO |
4511 | #else |
4512 | StructCopy(&zero_scan_data, &data, scan_data_t); | |
07be1b83 | 4513 | #endif |
fc8cd66c | 4514 | |
a0d0e21e | 4515 | /* Dig out information for optimizations. */ |
f7819f85 | 4516 | r->extflags = RExC_flags; /* was pm_op */ |
c737faaf YO |
4517 | /*dmq: removed as part of de-PMOP: pm->op_pmflags = RExC_flags; */ |
4518 | ||
a0ed51b3 | 4519 | if (UTF) |
8f6ae13c | 4520 | SvUTF8_on(rx); /* Unicode in it? */ |
f8fc2ecf | 4521 | ri->regstclass = NULL; |
830247a4 | 4522 | if (RExC_naughty >= 10) /* Probably an expensive pattern. */ |
bbe252da | 4523 | r->intflags |= PREGf_NAUGHTY; |
f8fc2ecf | 4524 | scan = ri->program + 1; /* First BRANCH. */ |
2779dcf1 | 4525 | |
1de06328 YO |
4526 | /* testing for BRANCH here tells us whether there is "must appear" |
4527 | data in the pattern. If there is then we can use it for optimisations */ | |
eaf3ca90 | 4528 | if (!(RExC_seen & REG_TOP_LEVEL_BRANCHES)) { /* Only one top-level choice. */ |
c277df42 | 4529 | I32 fake; |
c5254dd6 | 4530 | STRLEN longest_float_length, longest_fixed_length; |
07be1b83 | 4531 | struct regnode_charclass_class ch_class; /* pointed to by data */ |
653099ff | 4532 | int stclass_flag; |
07be1b83 | 4533 | I32 last_close = 0; /* pointed to by data */ |
5339e136 YO |
4534 | regnode *first= scan; |
4535 | regnode *first_next= regnext(first); | |
4536 | ||
639081d6 YO |
4537 | /* |
4538 | * Skip introductions and multiplicators >= 1 | |
4539 | * so that we can extract the 'meat' of the pattern that must | |
4540 | * match in the large if() sequence following. | |
4541 | * NOTE that EXACT is NOT covered here, as it is normally | |
4542 | * picked up by the optimiser separately. | |
4543 | * | |
4544 | * This is unfortunate as the optimiser isnt handling lookahead | |
4545 | * properly currently. | |
4546 | * | |
4547 | */ | |
a0d0e21e | 4548 | while ((OP(first) == OPEN && (sawopen = 1)) || |
653099ff | 4549 | /* An OR of *one* alternative - should not happen now. */ |
5339e136 | 4550 | (OP(first) == BRANCH && OP(first_next) != BRANCH) || |
07be1b83 YO |
4551 | /* for now we can't handle lookbehind IFMATCH*/ |
4552 | (OP(first) == IFMATCH && !first->flags) || | |
a0d0e21e LW |
4553 | (OP(first) == PLUS) || |
4554 | (OP(first) == MINMOD) || | |
653099ff | 4555 | /* An {n,m} with n>0 */ |
5339e136 YO |
4556 | (PL_regkind[OP(first)] == CURLY && ARG1(first) > 0) || |
4557 | (OP(first) == NOTHING && PL_regkind[OP(first_next)] != END )) | |
07be1b83 | 4558 | { |
639081d6 YO |
4559 | /* |
4560 | * the only op that could be a regnode is PLUS, all the rest | |
4561 | * will be regnode_1 or regnode_2. | |
4562 | * | |
4563 | */ | |
a0d0e21e LW |
4564 | if (OP(first) == PLUS) |
4565 | sawplus = 1; | |
4566 | else | |
3dab1dad | 4567 | first += regarglen[OP(first)]; |
639081d6 YO |
4568 | |
4569 | first = NEXTOPER(first); | |
5339e136 | 4570 | first_next= regnext(first); |
a687059c LW |
4571 | } |
4572 | ||
a0d0e21e LW |
4573 | /* Starting-point info. */ |
4574 | again: | |
786e8c11 | 4575 | DEBUG_PEEP("first:",first,0); |
07be1b83 | 4576 | /* Ignore EXACT as we deal with it later. */ |
3dab1dad | 4577 | if (PL_regkind[OP(first)] == EXACT) { |
1aa99e6b | 4578 | if (OP(first) == EXACT) |
6f207bd3 | 4579 | NOOP; /* Empty, get anchored substr later. */ |
1aa99e6b | 4580 | else if ((OP(first) == EXACTF || OP(first) == EXACTFL)) |
f8fc2ecf | 4581 | ri->regstclass = first; |
b3c9acc1 | 4582 | } |
07be1b83 | 4583 | #ifdef TRIE_STCLASS |
786e8c11 | 4584 | else if (PL_regkind[OP(first)] == TRIE && |
f8fc2ecf | 4585 | ((reg_trie_data *)ri->data->data[ ARG(first) ])->minlen>0) |
07be1b83 | 4586 | { |
786e8c11 | 4587 | regnode *trie_op; |
07be1b83 | 4588 | /* this can happen only on restudy */ |
786e8c11 | 4589 | if ( OP(first) == TRIE ) { |
c944940b | 4590 | struct regnode_1 *trieop = (struct regnode_1 *) |
446bd890 | 4591 | PerlMemShared_calloc(1, sizeof(struct regnode_1)); |
786e8c11 YO |
4592 | StructCopy(first,trieop,struct regnode_1); |
4593 | trie_op=(regnode *)trieop; | |
4594 | } else { | |
c944940b | 4595 | struct regnode_charclass *trieop = (struct regnode_charclass *) |
446bd890 | 4596 | PerlMemShared_calloc(1, sizeof(struct regnode_charclass)); |
786e8c11 YO |
4597 | StructCopy(first,trieop,struct regnode_charclass); |
4598 | trie_op=(regnode *)trieop; | |
4599 | } | |
1de06328 | 4600 | OP(trie_op)+=2; |
786e8c11 | 4601 | make_trie_failtable(pRExC_state, (regnode *)first, trie_op, 0); |
f8fc2ecf | 4602 | ri->regstclass = trie_op; |
07be1b83 YO |
4603 | } |
4604 | #endif | |
e52fc539 | 4605 | else if (REGNODE_SIMPLE(OP(first))) |
f8fc2ecf | 4606 | ri->regstclass = first; |
3dab1dad YO |
4607 | else if (PL_regkind[OP(first)] == BOUND || |
4608 | PL_regkind[OP(first)] == NBOUND) | |
f8fc2ecf | 4609 | ri->regstclass = first; |
3dab1dad | 4610 | else if (PL_regkind[OP(first)] == BOL) { |
bbe252da YO |
4611 | r->extflags |= (OP(first) == MBOL |
4612 | ? RXf_ANCH_MBOL | |
cad2e5aa | 4613 | : (OP(first) == SBOL |
bbe252da YO |
4614 | ? RXf_ANCH_SBOL |
4615 | : RXf_ANCH_BOL)); | |
a0d0e21e | 4616 | first = NEXTOPER(first); |
774d564b | 4617 | goto again; |
4618 | } | |
4619 | else if (OP(first) == GPOS) { | |
bbe252da | 4620 | r->extflags |= RXf_ANCH_GPOS; |
774d564b | 4621 | first = NEXTOPER(first); |
4622 | goto again; | |
a0d0e21e | 4623 | } |
cf2a2b69 YO |
4624 | else if ((!sawopen || !RExC_sawback) && |
4625 | (OP(first) == STAR && | |
3dab1dad | 4626 | PL_regkind[OP(NEXTOPER(first))] == REG_ANY) && |
bbe252da | 4627 | !(r->extflags & RXf_ANCH) && !(RExC_seen & REG_SEEN_EVAL)) |
a0d0e21e LW |
4628 | { |
4629 | /* turn .* into ^.* with an implied $*=1 */ | |
1df70142 AL |
4630 | const int type = |
4631 | (OP(NEXTOPER(first)) == REG_ANY) | |
bbe252da YO |
4632 | ? RXf_ANCH_MBOL |
4633 | : RXf_ANCH_SBOL; | |
4634 | r->extflags |= type; | |
4635 | r->intflags |= PREGf_IMPLICIT; | |
a0d0e21e | 4636 | first = NEXTOPER(first); |
774d564b | 4637 | goto again; |
a0d0e21e | 4638 | } |
b81d288d | 4639 | if (sawplus && (!sawopen || !RExC_sawback) |
830247a4 | 4640 | && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */ |
cad2e5aa | 4641 | /* x+ must match at the 1st pos of run of x's */ |
bbe252da | 4642 | r->intflags |= PREGf_SKIP; |
a0d0e21e | 4643 | |
c277df42 | 4644 | /* Scan is after the zeroth branch, first is atomic matcher. */ |
be8e71aa | 4645 | #ifdef TRIE_STUDY_OPT |
81714fb9 | 4646 | DEBUG_PARSE_r( |
be8e71aa YO |
4647 | if (!restudied) |
4648 | PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n", | |
4649 | (IV)(first - scan + 1)) | |
4650 | ); | |
4651 | #else | |
81714fb9 | 4652 | DEBUG_PARSE_r( |
be8e71aa YO |
4653 | PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n", |
4654 | (IV)(first - scan + 1)) | |
4655 | ); | |
4656 | #endif | |
4657 | ||
4658 | ||
a0d0e21e LW |
4659 | /* |
4660 | * If there's something expensive in the r.e., find the | |
4661 | * longest literal string that must appear and make it the | |
4662 | * regmust. Resolve ties in favor of later strings, since | |
4663 | * the regstart check works with the beginning of the r.e. | |
4664 | * and avoiding duplication strengthens checking. Not a | |
4665 | * strong reason, but sufficient in the absence of others. | |
4666 | * [Now we resolve ties in favor of the earlier string if | |
c277df42 | 4667 | * it happens that c_offset_min has been invalidated, since the |
a0d0e21e LW |
4668 | * earlier string may buy us something the later one won't.] |
4669 | */ | |
de8c5301 | 4670 | |
396482e1 GA |
4671 | data.longest_fixed = newSVpvs(""); |
4672 | data.longest_float = newSVpvs(""); | |
4673 | data.last_found = newSVpvs(""); | |
c277df42 IZ |
4674 | data.longest = &(data.longest_fixed); |
4675 | first = scan; | |
f8fc2ecf | 4676 | if (!ri->regstclass) { |
830247a4 | 4677 | cl_init(pRExC_state, &ch_class); |
653099ff GS |
4678 | data.start_class = &ch_class; |
4679 | stclass_flag = SCF_DO_STCLASS_AND; | |
4680 | } else /* XXXX Check for BOUND? */ | |
4681 | stclass_flag = 0; | |
cb434fcc | 4682 | data.last_closep = &last_close; |
de8c5301 | 4683 | |
1de06328 | 4684 | minlen = study_chunk(pRExC_state, &first, &minlen, &fake, scan + RExC_size, /* Up to end */ |
40d049e4 YO |
4685 | &data, -1, NULL, NULL, |
4686 | SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag,0); | |
07be1b83 | 4687 | |
07be1b83 | 4688 | |
786e8c11 YO |
4689 | CHECK_RESTUDY_GOTO; |
4690 | ||
4691 | ||
830247a4 | 4692 | if ( RExC_npar == 1 && data.longest == &(data.longest_fixed) |
b81d288d | 4693 | && data.last_start_min == 0 && data.last_end > 0 |
830247a4 | 4694 | && !RExC_seen_zerolen |
2bf803e2 | 4695 | && !(RExC_seen & REG_SEEN_VERBARG) |
bbe252da YO |
4696 | && (!(RExC_seen & REG_SEEN_GPOS) || (r->extflags & RXf_ANCH_GPOS))) |
4697 | r->extflags |= RXf_CHECK_ALL; | |
304ee84b | 4698 | scan_commit(pRExC_state, &data,&minlen,0); |
c277df42 IZ |
4699 | SvREFCNT_dec(data.last_found); |
4700 | ||
1de06328 YO |
4701 | /* Note that code very similar to this but for anchored string |
4702 | follows immediately below, changes may need to be made to both. | |
4703 | Be careful. | |
4704 | */ | |
a0ed51b3 | 4705 | longest_float_length = CHR_SVLEN(data.longest_float); |
c5254dd6 | 4706 | if (longest_float_length |
c277df42 IZ |
4707 | || (data.flags & SF_FL_BEFORE_EOL |
4708 | && (!(data.flags & SF_FL_BEFORE_MEOL) | |
bbe252da | 4709 | || (RExC_flags & RXf_PMf_MULTILINE)))) |
1de06328 | 4710 | { |
1182767e | 4711 | I32 t,ml; |
cf93c79d | 4712 | |
1de06328 | 4713 | if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */ |
aca2d497 IZ |
4714 | && data.offset_fixed == data.offset_float_min |
4715 | && SvCUR(data.longest_fixed) == SvCUR(data.longest_float)) | |
4716 | goto remove_float; /* As in (a)+. */ | |
4717 | ||
1de06328 YO |
4718 | /* copy the information about the longest float from the reg_scan_data |
4719 | over to the program. */ | |
33b8afdf JH |
4720 | if (SvUTF8(data.longest_float)) { |
4721 | r->float_utf8 = data.longest_float; | |
c445ea15 | 4722 | r->float_substr = NULL; |
33b8afdf JH |
4723 | } else { |
4724 | r->float_substr = data.longest_float; | |
c445ea15 | 4725 | r->float_utf8 = NULL; |
33b8afdf | 4726 | } |
1de06328 YO |
4727 | /* float_end_shift is how many chars that must be matched that |
4728 | follow this item. We calculate it ahead of time as once the | |
4729 | lookbehind offset is added in we lose the ability to correctly | |
4730 | calculate it.*/ | |
4731 | ml = data.minlen_float ? *(data.minlen_float) | |
1182767e | 4732 | : (I32)longest_float_length; |
1de06328 YO |
4733 | r->float_end_shift = ml - data.offset_float_min |
4734 | - longest_float_length + (SvTAIL(data.longest_float) != 0) | |
4735 | + data.lookbehind_float; | |
4736 | r->float_min_offset = data.offset_float_min - data.lookbehind_float; | |
c277df42 | 4737 | r->float_max_offset = data.offset_float_max; |
1182767e | 4738 | if (data.offset_float_max < I32_MAX) /* Don't offset infinity */ |
1de06328 YO |
4739 | r->float_max_offset -= data.lookbehind_float; |
4740 | ||
cf93c79d IZ |
4741 | t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */ |
4742 | && (!(data.flags & SF_FL_BEFORE_MEOL) | |
bbe252da | 4743 | || (RExC_flags & RXf_PMf_MULTILINE))); |
33b8afdf | 4744 | fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0); |
a0ed51b3 LW |
4745 | } |
4746 | else { | |
aca2d497 | 4747 | remove_float: |
c445ea15 | 4748 | r->float_substr = r->float_utf8 = NULL; |
c277df42 | 4749 | SvREFCNT_dec(data.longest_float); |
c5254dd6 | 4750 | longest_float_length = 0; |
a0d0e21e | 4751 | } |
c277df42 | 4752 | |
1de06328 YO |
4753 | /* Note that code very similar to this but for floating string |
4754 | is immediately above, changes may need to be made to both. | |
4755 | Be careful. | |
4756 | */ | |
a0ed51b3 | 4757 | longest_fixed_length = CHR_SVLEN(data.longest_fixed); |
c5254dd6 | 4758 | if (longest_fixed_length |
c277df42 IZ |
4759 | || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
4760 | && (!(data.flags & SF_FIX_BEFORE_MEOL) | |
bbe252da | 4761 | || (RExC_flags & RXf_PMf_MULTILINE)))) |
1de06328 | 4762 | { |
1182767e | 4763 | I32 t,ml; |
cf93c79d | 4764 | |
1de06328 YO |
4765 | /* copy the information about the longest fixed |
4766 | from the reg_scan_data over to the program. */ | |
33b8afdf JH |
4767 | if (SvUTF8(data.longest_fixed)) { |
4768 | r->anchored_utf8 = data.longest_fixed; | |
c445ea15 | 4769 | r->anchored_substr = NULL; |
33b8afdf JH |
4770 | } else { |
4771 | r->anchored_substr = data.longest_fixed; | |
c445ea15 | 4772 | r->anchored_utf8 = NULL; |
33b8afdf | 4773 | } |
1de06328 YO |
4774 | /* fixed_end_shift is how many chars that must be matched that |
4775 | follow this item. We calculate it ahead of time as once the | |
4776 | lookbehind offset is added in we lose the ability to correctly | |
4777 | calculate it.*/ | |
4778 | ml = data.minlen_fixed ? *(data.minlen_fixed) | |
1182767e | 4779 | : (I32)longest_fixed_length; |
1de06328 YO |
4780 | r->anchored_end_shift = ml - data.offset_fixed |
4781 | - longest_fixed_length + (SvTAIL(data.longest_fixed) != 0) | |
4782 | + data.lookbehind_fixed; | |
4783 | r->anchored_offset = data.offset_fixed - data.lookbehind_fixed; | |
4784 | ||
cf93c79d IZ |
4785 | t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */ |
4786 | && (!(data.flags & SF_FIX_BEFORE_MEOL) | |
bbe252da | 4787 | || (RExC_flags & RXf_PMf_MULTILINE))); |
33b8afdf | 4788 | fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0); |
a0ed51b3 LW |
4789 | } |
4790 | else { | |
c445ea15 | 4791 | r->anchored_substr = r->anchored_utf8 = NULL; |
c277df42 | 4792 | SvREFCNT_dec(data.longest_fixed); |
c5254dd6 | 4793 | longest_fixed_length = 0; |
a0d0e21e | 4794 | } |
f8fc2ecf YO |
4795 | if (ri->regstclass |
4796 | && (OP(ri->regstclass) == REG_ANY || OP(ri->regstclass) == SANY)) | |
4797 | ri->regstclass = NULL; | |
33b8afdf JH |
4798 | if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset) |
4799 | && stclass_flag | |
653099ff | 4800 | && !(data.start_class->flags & ANYOF_EOS) |
eb160463 GS |
4801 | && !cl_is_anything(data.start_class)) |
4802 | { | |
2eccd3b2 | 4803 | const U32 n = add_data(pRExC_state, 1, "f"); |
653099ff | 4804 | |
f8fc2ecf | 4805 | Newx(RExC_rxi->data->data[n], 1, |
653099ff GS |
4806 | struct regnode_charclass_class); |
4807 | StructCopy(data.start_class, | |
f8fc2ecf | 4808 | (struct regnode_charclass_class*)RExC_rxi->data->data[n], |
653099ff | 4809 | struct regnode_charclass_class); |
f8fc2ecf | 4810 | ri->regstclass = (regnode*)RExC_rxi->data->data[n]; |
bbe252da | 4811 | r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */ |
a3621e74 | 4812 | DEBUG_COMPILE_r({ SV *sv = sv_newmortal(); |
32fc9b6a | 4813 | regprop(r, sv, (regnode*)data.start_class); |
9c5ffd7c | 4814 | PerlIO_printf(Perl_debug_log, |
a0288114 | 4815 | "synthetic stclass \"%s\".\n", |
3f7c398e | 4816 | SvPVX_const(sv));}); |
653099ff | 4817 | } |
c277df42 IZ |
4818 | |
4819 | /* A temporary algorithm prefers floated substr to fixed one to dig more info. */ | |
c5254dd6 | 4820 | if (longest_fixed_length > longest_float_length) { |
1de06328 | 4821 | r->check_end_shift = r->anchored_end_shift; |
c277df42 | 4822 | r->check_substr = r->anchored_substr; |
33b8afdf | 4823 | r->check_utf8 = r->anchored_utf8; |
c277df42 | 4824 | r->check_offset_min = r->check_offset_max = r->anchored_offset; |
bbe252da YO |
4825 | if (r->extflags & RXf_ANCH_SINGLE) |
4826 | r->extflags |= RXf_NOSCAN; | |
a0ed51b3 LW |
4827 | } |
4828 | else { | |
1de06328 | 4829 | r->check_end_shift = r->float_end_shift; |
c277df42 | 4830 | r->check_substr = r->float_substr; |
33b8afdf | 4831 | r->check_utf8 = r->float_utf8; |
1de06328 YO |
4832 | r->check_offset_min = r->float_min_offset; |
4833 | r->check_offset_max = r->float_max_offset; | |
a0d0e21e | 4834 | } |
30382c73 IZ |
4835 | /* XXXX Currently intuiting is not compatible with ANCH_GPOS. |
4836 | This should be changed ASAP! */ | |
bbe252da YO |
4837 | if ((r->check_substr || r->check_utf8) && !(r->extflags & RXf_ANCH_GPOS)) { |
4838 | r->extflags |= RXf_USE_INTUIT; | |
33b8afdf | 4839 | if (SvTAIL(r->check_substr ? r->check_substr : r->check_utf8)) |
bbe252da | 4840 | r->extflags |= RXf_INTUIT_TAIL; |
cad2e5aa | 4841 | } |
1de06328 YO |
4842 | /* XXX Unneeded? dmq (shouldn't as this is handled elsewhere) |
4843 | if ( (STRLEN)minlen < longest_float_length ) | |
4844 | minlen= longest_float_length; | |
4845 | if ( (STRLEN)minlen < longest_fixed_length ) | |
4846 | minlen= longest_fixed_length; | |
4847 | */ | |
a0ed51b3 LW |
4848 | } |
4849 | else { | |
c277df42 IZ |
4850 | /* Several toplevels. Best we can is to set minlen. */ |
4851 | I32 fake; | |
653099ff | 4852 | struct regnode_charclass_class ch_class; |
cb434fcc | 4853 | I32 last_close = 0; |
c277df42 | 4854 | |
5d458dd8 | 4855 | DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "\nMulti Top Level\n")); |
07be1b83 | 4856 | |
f8fc2ecf | 4857 | scan = ri->program + 1; |
830247a4 | 4858 | cl_init(pRExC_state, &ch_class); |
653099ff | 4859 | data.start_class = &ch_class; |
cb434fcc | 4860 | data.last_closep = &last_close; |
07be1b83 | 4861 | |
de8c5301 | 4862 | |
1de06328 | 4863 | minlen = study_chunk(pRExC_state, &scan, &minlen, &fake, scan + RExC_size, |
40d049e4 | 4864 | &data, -1, NULL, NULL, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS,0); |
de8c5301 | 4865 | |
786e8c11 | 4866 | CHECK_RESTUDY_GOTO; |
07be1b83 | 4867 | |
33b8afdf | 4868 | r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8 |
c445ea15 | 4869 | = r->float_substr = r->float_utf8 = NULL; |
653099ff | 4870 | if (!(data.start_class->flags & ANYOF_EOS) |
eb160463 GS |
4871 | && !cl_is_anything(data.start_class)) |
4872 | { | |
2eccd3b2 | 4873 | const U32 n = add_data(pRExC_state, 1, "f"); |
653099ff | 4874 | |
f8fc2ecf | 4875 | Newx(RExC_rxi->data->data[n], 1, |
653099ff GS |
4876 | struct regnode_charclass_class); |
4877 | StructCopy(data.start_class, | |
f8fc2ecf | 4878 | (struct regnode_charclass_class*)RExC_rxi->data->data[n], |
653099ff | 4879 | struct regnode_charclass_class); |
f8fc2ecf | 4880 | ri->regstclass = (regnode*)RExC_rxi->data->data[n]; |
bbe252da | 4881 | r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */ |
a3621e74 | 4882 | DEBUG_COMPILE_r({ SV* sv = sv_newmortal(); |
32fc9b6a | 4883 | regprop(r, sv, (regnode*)data.start_class); |
9c5ffd7c | 4884 | PerlIO_printf(Perl_debug_log, |
a0288114 | 4885 | "synthetic stclass \"%s\".\n", |
3f7c398e | 4886 | SvPVX_const(sv));}); |
653099ff | 4887 | } |
a0d0e21e LW |
4888 | } |
4889 | ||
1de06328 YO |
4890 | /* Guard against an embedded (?=) or (?<=) with a longer minlen than |
4891 | the "real" pattern. */ | |
cf9788e3 RGS |
4892 | DEBUG_OPTIMISE_r({ |
4893 | PerlIO_printf(Perl_debug_log,"minlen: %"IVdf" r->minlen:%"IVdf"\n", | |
70685ca0 | 4894 | (IV)minlen, (IV)r->minlen); |
cf9788e3 | 4895 | }); |
de8c5301 | 4896 | r->minlenret = minlen; |
1de06328 YO |
4897 | if (r->minlen < minlen) |
4898 | r->minlen = minlen; | |
4899 | ||
b81d288d | 4900 | if (RExC_seen & REG_SEEN_GPOS) |
bbe252da | 4901 | r->extflags |= RXf_GPOS_SEEN; |
830247a4 | 4902 | if (RExC_seen & REG_SEEN_LOOKBEHIND) |
bbe252da | 4903 | r->extflags |= RXf_LOOKBEHIND_SEEN; |
830247a4 | 4904 | if (RExC_seen & REG_SEEN_EVAL) |
bbe252da | 4905 | r->extflags |= RXf_EVAL_SEEN; |
f33976b4 | 4906 | if (RExC_seen & REG_SEEN_CANY) |
bbe252da | 4907 | r->extflags |= RXf_CANY_SEEN; |
e2e6a0f1 | 4908 | if (RExC_seen & REG_SEEN_VERBARG) |
bbe252da | 4909 | r->intflags |= PREGf_VERBARG_SEEN; |
5d458dd8 | 4910 | if (RExC_seen & REG_SEEN_CUTGROUP) |
bbe252da | 4911 | r->intflags |= PREGf_CUTGROUP_SEEN; |
81714fb9 | 4912 | if (RExC_paren_names) |
85fbaab2 | 4913 | RXp_PAREN_NAMES(r) = MUTABLE_HV(SvREFCNT_inc(RExC_paren_names)); |
81714fb9 | 4914 | else |
5daac39c | 4915 | RXp_PAREN_NAMES(r) = NULL; |
0ac6acae | 4916 | |
7bd1e614 | 4917 | #ifdef STUPID_PATTERN_CHECKS |
5509d87a | 4918 | if (RX_PRELEN(rx) == 0) |
640f820d | 4919 | r->extflags |= RXf_NULL; |
5509d87a | 4920 | if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ') |
0ac6acae AB |
4921 | /* XXX: this should happen BEFORE we compile */ |
4922 | r->extflags |= (RXf_SKIPWHITE|RXf_WHITE); | |
5509d87a | 4923 | else if (RX_PRELEN(rx) == 3 && memEQ("\\s+", RX_PRECOMP(rx), 3)) |
0ac6acae | 4924 | r->extflags |= RXf_WHITE; |
5509d87a | 4925 | else if (RX_PRELEN(rx) == 1 && RXp_PRECOMP(rx)[0] == '^') |
e357fc67 | 4926 | r->extflags |= RXf_START_ONLY; |
f1b875a0 | 4927 | #else |
5509d87a | 4928 | if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ') |
7bd1e614 YO |
4929 | /* XXX: this should happen BEFORE we compile */ |
4930 | r->extflags |= (RXf_SKIPWHITE|RXf_WHITE); | |
4931 | else { | |
4932 | regnode *first = ri->program + 1; | |
39aa8307 JH |
4933 | U8 fop = OP(first); |
4934 | U8 nop = OP(NEXTOPER(first)); | |
7bd1e614 | 4935 | |
640f820d AB |
4936 | if (PL_regkind[fop] == NOTHING && nop == END) |
4937 | r->extflags |= RXf_NULL; | |
4938 | else if (PL_regkind[fop] == BOL && nop == END) | |
7bd1e614 YO |
4939 | r->extflags |= RXf_START_ONLY; |
4940 | else if (fop == PLUS && nop ==SPACE && OP(regnext(first))==END) | |
4941 | r->extflags |= RXf_WHITE; | |
4942 | } | |
f1b875a0 | 4943 | #endif |
1f1031fe YO |
4944 | #ifdef DEBUGGING |
4945 | if (RExC_paren_names) { | |
af534a04 | 4946 | ri->name_list_idx = add_data( pRExC_state, 1, "a" ); |
1f1031fe YO |
4947 | ri->data->data[ri->name_list_idx] = (void*)SvREFCNT_inc(RExC_paren_name_list); |
4948 | } else | |
1f1031fe | 4949 | #endif |
cde0cee5 | 4950 | ri->name_list_idx = 0; |
1f1031fe | 4951 | |
40d049e4 YO |
4952 | if (RExC_recurse_count) { |
4953 | for ( ; RExC_recurse_count ; RExC_recurse_count-- ) { | |
4954 | const regnode *scan = RExC_recurse[RExC_recurse_count-1]; | |
4955 | ARG2L_SET( scan, RExC_open_parens[ARG(scan)-1] - scan ); | |
4956 | } | |
4957 | } | |
f0ab9afb | 4958 | Newxz(r->offs, RExC_npar, regexp_paren_pair); |
c74340f9 YO |
4959 | /* assume we don't need to swap parens around before we match */ |
4960 | ||
be8e71aa YO |
4961 | DEBUG_DUMP_r({ |
4962 | PerlIO_printf(Perl_debug_log,"Final program:\n"); | |
3dab1dad YO |
4963 | regdump(r); |
4964 | }); | |
7122b237 YO |
4965 | #ifdef RE_TRACK_PATTERN_OFFSETS |
4966 | DEBUG_OFFSETS_r(if (ri->u.offsets) { | |
4967 | const U32 len = ri->u.offsets[0]; | |
8e9a8a48 YO |
4968 | U32 i; |
4969 | GET_RE_DEBUG_FLAGS_DECL; | |
7122b237 | 4970 | PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)ri->u.offsets[0]); |
8e9a8a48 | 4971 | for (i = 1; i <= len; i++) { |
7122b237 | 4972 | if (ri->u.offsets[i*2-1] || ri->u.offsets[i*2]) |
8e9a8a48 | 4973 | PerlIO_printf(Perl_debug_log, "%"UVuf":%"UVuf"[%"UVuf"] ", |
7122b237 | 4974 | (UV)i, (UV)ri->u.offsets[i*2-1], (UV)ri->u.offsets[i*2]); |
8e9a8a48 YO |
4975 | } |
4976 | PerlIO_printf(Perl_debug_log, "\n"); | |
4977 | }); | |
7122b237 | 4978 | #endif |
288b8c02 | 4979 | return rx; |
a687059c LW |
4980 | } |
4981 | ||
f9f4320a | 4982 | #undef RE_ENGINE_PTR |
3dab1dad | 4983 | |
93b32b6d | 4984 | |
81714fb9 | 4985 | SV* |
192b9cd1 AB |
4986 | Perl_reg_named_buff(pTHX_ REGEXP * const rx, SV * const key, SV * const value, |
4987 | const U32 flags) | |
4988 | { | |
7918f24d NC |
4989 | PERL_ARGS_ASSERT_REG_NAMED_BUFF; |
4990 | ||
192b9cd1 AB |
4991 | PERL_UNUSED_ARG(value); |
4992 | ||
f1b875a0 | 4993 | if (flags & RXapif_FETCH) { |
192b9cd1 | 4994 | return reg_named_buff_fetch(rx, key, flags); |
f1b875a0 | 4995 | } else if (flags & (RXapif_STORE | RXapif_DELETE | RXapif_CLEAR)) { |
6ad8f254 | 4996 | Perl_croak_no_modify(aTHX); |
192b9cd1 | 4997 | return NULL; |
f1b875a0 | 4998 | } else if (flags & RXapif_EXISTS) { |
192b9cd1 AB |
4999 | return reg_named_buff_exists(rx, key, flags) |
5000 | ? &PL_sv_yes | |
5001 | : &PL_sv_no; | |
f1b875a0 | 5002 | } else if (flags & RXapif_REGNAMES) { |
192b9cd1 | 5003 | return reg_named_buff_all(rx, flags); |
f1b875a0 | 5004 | } else if (flags & (RXapif_SCALAR | RXapif_REGNAMES_COUNT)) { |
192b9cd1 AB |
5005 | return reg_named_buff_scalar(rx, flags); |
5006 | } else { | |
5007 | Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff", (int)flags); | |
5008 | return NULL; | |
5009 | } | |
5010 | } | |
5011 | ||
5012 | SV* | |
5013 | Perl_reg_named_buff_iter(pTHX_ REGEXP * const rx, const SV * const lastkey, | |
5014 | const U32 flags) | |
5015 | { | |
7918f24d | 5016 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_ITER; |
192b9cd1 AB |
5017 | PERL_UNUSED_ARG(lastkey); |
5018 | ||
f1b875a0 | 5019 | if (flags & RXapif_FIRSTKEY) |
192b9cd1 | 5020 | return reg_named_buff_firstkey(rx, flags); |
f1b875a0 | 5021 | else if (flags & RXapif_NEXTKEY) |
192b9cd1 AB |
5022 | return reg_named_buff_nextkey(rx, flags); |
5023 | else { | |
5024 | Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_iter", (int)flags); | |
5025 | return NULL; | |
5026 | } | |
5027 | } | |
5028 | ||
5029 | SV* | |
288b8c02 NC |
5030 | Perl_reg_named_buff_fetch(pTHX_ REGEXP * const r, SV * const namesv, |
5031 | const U32 flags) | |
81714fb9 | 5032 | { |
44a2ac75 YO |
5033 | AV *retarray = NULL; |
5034 | SV *ret; | |
288b8c02 | 5035 | struct regexp *const rx = (struct regexp *)SvANY(r); |
7918f24d NC |
5036 | |
5037 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_FETCH; | |
5038 | ||
f1b875a0 | 5039 | if (flags & RXapif_ALL) |
44a2ac75 | 5040 | retarray=newAV(); |
93b32b6d | 5041 | |
5daac39c NC |
5042 | if (rx && RXp_PAREN_NAMES(rx)) { |
5043 | HE *he_str = hv_fetch_ent( RXp_PAREN_NAMES(rx), namesv, 0, 0 ); | |
93b32b6d YO |
5044 | if (he_str) { |
5045 | IV i; | |
5046 | SV* sv_dat=HeVAL(he_str); | |
5047 | I32 *nums=(I32*)SvPVX(sv_dat); | |
5048 | for ( i=0; i<SvIVX(sv_dat); i++ ) { | |
192b9cd1 AB |
5049 | if ((I32)(rx->nparens) >= nums[i] |
5050 | && rx->offs[nums[i]].start != -1 | |
5051 | && rx->offs[nums[i]].end != -1) | |
93b32b6d | 5052 | { |
49d7dfbc | 5053 | ret = newSVpvs(""); |
288b8c02 | 5054 | CALLREG_NUMBUF_FETCH(r,nums[i],ret); |
93b32b6d YO |
5055 | if (!retarray) |
5056 | return ret; | |
5057 | } else { | |
5058 | ret = newSVsv(&PL_sv_undef); | |
5059 | } | |
ec83ea38 | 5060 | if (retarray) |
93b32b6d | 5061 | av_push(retarray, ret); |
81714fb9 | 5062 | } |
93b32b6d | 5063 | if (retarray) |
ad64d0ec | 5064 | return newRV_noinc(MUTABLE_SV(retarray)); |
192b9cd1 AB |
5065 | } |
5066 | } | |
5067 | return NULL; | |
5068 | } | |
5069 | ||
5070 | bool | |
288b8c02 | 5071 | Perl_reg_named_buff_exists(pTHX_ REGEXP * const r, SV * const key, |
192b9cd1 AB |
5072 | const U32 flags) |
5073 | { | |
288b8c02 | 5074 | struct regexp *const rx = (struct regexp *)SvANY(r); |
7918f24d NC |
5075 | |
5076 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_EXISTS; | |
5077 | ||
5daac39c | 5078 | if (rx && RXp_PAREN_NAMES(rx)) { |
f1b875a0 | 5079 | if (flags & RXapif_ALL) { |
5daac39c | 5080 | return hv_exists_ent(RXp_PAREN_NAMES(rx), key, 0); |
192b9cd1 | 5081 | } else { |
288b8c02 | 5082 | SV *sv = CALLREG_NAMED_BUFF_FETCH(r, key, flags); |
6499cc01 RGS |
5083 | if (sv) { |
5084 | SvREFCNT_dec(sv); | |
192b9cd1 AB |
5085 | return TRUE; |
5086 | } else { | |
5087 | return FALSE; | |
5088 | } | |
5089 | } | |
5090 | } else { | |
5091 | return FALSE; | |
5092 | } | |
5093 | } | |
5094 | ||
5095 | SV* | |
288b8c02 | 5096 | Perl_reg_named_buff_firstkey(pTHX_ REGEXP * const r, const U32 flags) |
192b9cd1 | 5097 | { |
288b8c02 | 5098 | struct regexp *const rx = (struct regexp *)SvANY(r); |
7918f24d NC |
5099 | |
5100 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_FIRSTKEY; | |
5101 | ||
5daac39c NC |
5102 | if ( rx && RXp_PAREN_NAMES(rx) ) { |
5103 | (void)hv_iterinit(RXp_PAREN_NAMES(rx)); | |
192b9cd1 | 5104 | |
288b8c02 | 5105 | return CALLREG_NAMED_BUFF_NEXTKEY(r, NULL, flags & ~RXapif_FIRSTKEY); |
1e1d4b91 JJ |
5106 | } else { |
5107 | return FALSE; | |
5108 | } | |
192b9cd1 AB |
5109 | } |
5110 | ||
5111 | SV* | |
288b8c02 | 5112 | Perl_reg_named_buff_nextkey(pTHX_ REGEXP * const r, const U32 flags) |
192b9cd1 | 5113 | { |
288b8c02 | 5114 | struct regexp *const rx = (struct regexp *)SvANY(r); |
250257bb | 5115 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
5116 | |
5117 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_NEXTKEY; | |
5118 | ||
5daac39c NC |
5119 | if (rx && RXp_PAREN_NAMES(rx)) { |
5120 | HV *hv = RXp_PAREN_NAMES(rx); | |
192b9cd1 AB |
5121 | HE *temphe; |
5122 | while ( (temphe = hv_iternext_flags(hv,0)) ) { | |
5123 | IV i; | |
5124 | IV parno = 0; | |
5125 | SV* sv_dat = HeVAL(temphe); | |
5126 | I32 *nums = (I32*)SvPVX(sv_dat); | |
5127 | for ( i = 0; i < SvIVX(sv_dat); i++ ) { | |
250257bb | 5128 | if ((I32)(rx->lastparen) >= nums[i] && |
192b9cd1 AB |
5129 | rx->offs[nums[i]].start != -1 && |
5130 | rx->offs[nums[i]].end != -1) | |
5131 | { | |
5132 | parno = nums[i]; | |
5133 | break; | |
5134 | } | |
5135 | } | |
f1b875a0 | 5136 | if (parno || flags & RXapif_ALL) { |
a663657d | 5137 | return newSVhek(HeKEY_hek(temphe)); |
192b9cd1 | 5138 | } |
81714fb9 YO |
5139 | } |
5140 | } | |
44a2ac75 YO |
5141 | return NULL; |
5142 | } | |
5143 | ||
192b9cd1 | 5144 | SV* |
288b8c02 | 5145 | Perl_reg_named_buff_scalar(pTHX_ REGEXP * const r, const U32 flags) |
192b9cd1 AB |
5146 | { |
5147 | SV *ret; | |
5148 | AV *av; | |
5149 | I32 length; | |
288b8c02 | 5150 | struct regexp *const rx = (struct regexp *)SvANY(r); |
192b9cd1 | 5151 | |
7918f24d NC |
5152 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_SCALAR; |
5153 | ||
5daac39c | 5154 | if (rx && RXp_PAREN_NAMES(rx)) { |
f1b875a0 | 5155 | if (flags & (RXapif_ALL | RXapif_REGNAMES_COUNT)) { |
5daac39c | 5156 | return newSViv(HvTOTALKEYS(RXp_PAREN_NAMES(rx))); |
f1b875a0 | 5157 | } else if (flags & RXapif_ONE) { |
288b8c02 | 5158 | ret = CALLREG_NAMED_BUFF_ALL(r, (flags | RXapif_REGNAMES)); |
502c6561 | 5159 | av = MUTABLE_AV(SvRV(ret)); |
192b9cd1 | 5160 | length = av_len(av); |
ec83ea38 | 5161 | SvREFCNT_dec(ret); |
192b9cd1 AB |
5162 | return newSViv(length + 1); |
5163 | } else { | |
5164 | Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_scalar", (int)flags); | |
5165 | return NULL; | |
5166 | } | |
5167 | } | |
5168 | return &PL_sv_undef; | |
5169 | } | |
5170 | ||
5171 | SV* | |
288b8c02 | 5172 | Perl_reg_named_buff_all(pTHX_ REGEXP * const r, const U32 flags) |
192b9cd1 | 5173 | { |
288b8c02 | 5174 | struct regexp *const rx = (struct regexp *)SvANY(r); |
192b9cd1 AB |
5175 | AV *av = newAV(); |
5176 | ||
7918f24d NC |
5177 | PERL_ARGS_ASSERT_REG_NAMED_BUFF_ALL; |
5178 | ||
5daac39c NC |
5179 | if (rx && RXp_PAREN_NAMES(rx)) { |
5180 | HV *hv= RXp_PAREN_NAMES(rx); | |
192b9cd1 AB |
5181 | HE *temphe; |
5182 | (void)hv_iterinit(hv); | |
5183 | while ( (temphe = hv_iternext_flags(hv,0)) ) { | |
5184 | IV i; | |
5185 | IV parno = 0; | |
5186 | SV* sv_dat = HeVAL(temphe); | |
5187 | I32 *nums = (I32*)SvPVX(sv_dat); | |
5188 | for ( i = 0; i < SvIVX(sv_dat); i++ ) { | |
250257bb | 5189 | if ((I32)(rx->lastparen) >= nums[i] && |
192b9cd1 AB |
5190 | rx->offs[nums[i]].start != -1 && |
5191 | rx->offs[nums[i]].end != -1) | |
5192 | { | |
5193 | parno = nums[i]; | |
5194 | break; | |
5195 | } | |
5196 | } | |
f1b875a0 | 5197 | if (parno || flags & RXapif_ALL) { |
a663657d | 5198 | av_push(av, newSVhek(HeKEY_hek(temphe))); |
192b9cd1 AB |
5199 | } |
5200 | } | |
5201 | } | |
5202 | ||
ad64d0ec | 5203 | return newRV_noinc(MUTABLE_SV(av)); |
192b9cd1 AB |
5204 | } |
5205 | ||
49d7dfbc | 5206 | void |
288b8c02 NC |
5207 | Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren, |
5208 | SV * const sv) | |
44a2ac75 | 5209 | { |
288b8c02 | 5210 | struct regexp *const rx = (struct regexp *)SvANY(r); |
44a2ac75 | 5211 | char *s = NULL; |
a9d504c3 | 5212 | I32 i = 0; |
44a2ac75 | 5213 | I32 s1, t1; |
7918f24d NC |
5214 | |
5215 | PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH; | |
44a2ac75 | 5216 | |
cde0cee5 YO |
5217 | if (!rx->subbeg) { |
5218 | sv_setsv(sv,&PL_sv_undef); | |
49d7dfbc | 5219 | return; |
cde0cee5 YO |
5220 | } |
5221 | else | |
f1b875a0 | 5222 | if (paren == RX_BUFF_IDX_PREMATCH && rx->offs[0].start != -1) { |
44a2ac75 | 5223 | /* $` */ |
f0ab9afb | 5224 | i = rx->offs[0].start; |
cde0cee5 | 5225 | s = rx->subbeg; |
44a2ac75 YO |
5226 | } |
5227 | else | |
f1b875a0 | 5228 | if (paren == RX_BUFF_IDX_POSTMATCH && rx->offs[0].end != -1) { |
44a2ac75 | 5229 | /* $' */ |
f0ab9afb NC |
5230 | s = rx->subbeg + rx->offs[0].end; |
5231 | i = rx->sublen - rx->offs[0].end; | |
44a2ac75 YO |
5232 | } |
5233 | else | |
5234 | if ( 0 <= paren && paren <= (I32)rx->nparens && | |
f0ab9afb NC |
5235 | (s1 = rx->offs[paren].start) != -1 && |
5236 | (t1 = rx->offs[paren].end) != -1) | |
44a2ac75 YO |
5237 | { |
5238 | /* $& $1 ... */ | |
5239 | i = t1 - s1; | |
5240 | s = rx->subbeg + s1; | |
cde0cee5 YO |
5241 | } else { |
5242 | sv_setsv(sv,&PL_sv_undef); | |
49d7dfbc | 5243 | return; |
cde0cee5 YO |
5244 | } |
5245 | assert(rx->sublen >= (s - rx->subbeg) + i ); | |
5246 | if (i >= 0) { | |
5247 | const int oldtainted = PL_tainted; | |
5248 | TAINT_NOT; | |
5249 | sv_setpvn(sv, s, i); | |
5250 | PL_tainted = oldtainted; | |
5251 | if ( (rx->extflags & RXf_CANY_SEEN) | |
07bc277f | 5252 | ? (RXp_MATCH_UTF8(rx) |
cde0cee5 | 5253 | && (!i || is_utf8_string((U8*)s, i))) |
07bc277f | 5254 | : (RXp_MATCH_UTF8(rx)) ) |
cde0cee5 YO |
5255 | { |
5256 | SvUTF8_on(sv); | |
5257 | } | |
5258 | else | |
5259 | SvUTF8_off(sv); | |
5260 | if (PL_tainting) { | |
07bc277f | 5261 | if (RXp_MATCH_TAINTED(rx)) { |
cde0cee5 YO |
5262 | if (SvTYPE(sv) >= SVt_PVMG) { |
5263 | MAGIC* const mg = SvMAGIC(sv); | |
5264 | MAGIC* mgt; | |
5265 | PL_tainted = 1; | |
5266 | SvMAGIC_set(sv, mg->mg_moremagic); | |
5267 | SvTAINT(sv); | |
5268 | if ((mgt = SvMAGIC(sv))) { | |
5269 | mg->mg_moremagic = mgt; | |
5270 | SvMAGIC_set(sv, mg); | |
44a2ac75 | 5271 | } |
cde0cee5 YO |
5272 | } else { |
5273 | PL_tainted = 1; | |
5274 | SvTAINT(sv); | |
5275 | } | |
5276 | } else | |
5277 | SvTAINTED_off(sv); | |
44a2ac75 | 5278 | } |
81714fb9 | 5279 | } else { |
44a2ac75 | 5280 | sv_setsv(sv,&PL_sv_undef); |
49d7dfbc | 5281 | return; |
81714fb9 YO |
5282 | } |
5283 | } | |
93b32b6d | 5284 | |
2fdbfb4d AB |
5285 | void |
5286 | Perl_reg_numbered_buff_store(pTHX_ REGEXP * const rx, const I32 paren, | |
5287 | SV const * const value) | |
5288 | { | |
7918f24d NC |
5289 | PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_STORE; |
5290 | ||
2fdbfb4d AB |
5291 | PERL_UNUSED_ARG(rx); |
5292 | PERL_UNUSED_ARG(paren); | |
5293 | PERL_UNUSED_ARG(value); | |
5294 | ||
5295 | if (!PL_localizing) | |
6ad8f254 | 5296 | Perl_croak_no_modify(aTHX); |
2fdbfb4d AB |
5297 | } |
5298 | ||
5299 | I32 | |
288b8c02 | 5300 | Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv, |
2fdbfb4d AB |
5301 | const I32 paren) |
5302 | { | |
288b8c02 | 5303 | struct regexp *const rx = (struct regexp *)SvANY(r); |
2fdbfb4d AB |
5304 | I32 i; |
5305 | I32 s1, t1; | |
5306 | ||
7918f24d NC |
5307 | PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_LENGTH; |
5308 | ||
2fdbfb4d AB |
5309 | /* Some of this code was originally in C<Perl_magic_len> in F<mg.c> */ |
5310 | switch (paren) { | |
192b9cd1 | 5311 | /* $` / ${^PREMATCH} */ |
f1b875a0 | 5312 | case RX_BUFF_IDX_PREMATCH: |
2fdbfb4d AB |
5313 | if (rx->offs[0].start != -1) { |
5314 | i = rx->offs[0].start; | |
5315 | if (i > 0) { | |
5316 | s1 = 0; | |
5317 | t1 = i; | |
5318 | goto getlen; | |
5319 | } | |
5320 | } | |
5321 | return 0; | |
192b9cd1 | 5322 | /* $' / ${^POSTMATCH} */ |
f1b875a0 | 5323 | case RX_BUFF_IDX_POSTMATCH: |
2fdbfb4d AB |
5324 | if (rx->offs[0].end != -1) { |
5325 | i = rx->sublen - rx->offs[0].end; | |
5326 | if (i > 0) { | |
5327 | s1 = rx->offs[0].end; | |
5328 | t1 = rx->sublen; | |
5329 | goto getlen; | |
5330 | } | |
5331 | } | |
5332 | return 0; | |
192b9cd1 AB |
5333 | /* $& / ${^MATCH}, $1, $2, ... */ |
5334 | default: | |
2fdbfb4d AB |
5335 | if (paren <= (I32)rx->nparens && |
5336 | (s1 = rx->offs[paren].start) != -1 && | |
5337 | (t1 = rx->offs[paren].end) != -1) | |
5338 | { | |
5339 | i = t1 - s1; | |
5340 | goto getlen; | |
5341 | } else { | |
5342 | if (ckWARN(WARN_UNINITIALIZED)) | |
ad64d0ec | 5343 | report_uninit((const SV *)sv); |
2fdbfb4d AB |
5344 | return 0; |
5345 | } | |
5346 | } | |
5347 | getlen: | |
07bc277f | 5348 | if (i > 0 && RXp_MATCH_UTF8(rx)) { |
2fdbfb4d AB |
5349 | const char * const s = rx->subbeg + s1; |
5350 | const U8 *ep; | |
5351 | STRLEN el; | |
5352 | ||
5353 | i = t1 - s1; | |
5354 | if (is_utf8_string_loclen((U8*)s, i, &ep, &el)) | |
5355 | i = el; | |
5356 | } | |
5357 | return i; | |
5358 | } | |
5359 | ||
fe578d7f | 5360 | SV* |
49d7dfbc | 5361 | Perl_reg_qr_package(pTHX_ REGEXP * const rx) |
fe578d7f | 5362 | { |
7918f24d | 5363 | PERL_ARGS_ASSERT_REG_QR_PACKAGE; |
fe578d7f | 5364 | PERL_UNUSED_ARG(rx); |
0fc92fc6 YO |
5365 | if (0) |
5366 | return NULL; | |
5367 | else | |
5368 | return newSVpvs("Regexp"); | |
fe578d7f | 5369 | } |
0a4db386 | 5370 | |
894be9b7 | 5371 | /* Scans the name of a named buffer from the pattern. |
0a4db386 YO |
5372 | * If flags is REG_RSN_RETURN_NULL returns null. |
5373 | * If flags is REG_RSN_RETURN_NAME returns an SV* containing the name | |
5374 | * If flags is REG_RSN_RETURN_DATA returns the data SV* corresponding | |
5375 | * to the parsed name as looked up in the RExC_paren_names hash. | |
5376 | * If there is an error throws a vFAIL().. type exception. | |
894be9b7 | 5377 | */ |
0a4db386 YO |
5378 | |
5379 | #define REG_RSN_RETURN_NULL 0 | |
5380 | #define REG_RSN_RETURN_NAME 1 | |
5381 | #define REG_RSN_RETURN_DATA 2 | |
5382 | ||
894be9b7 | 5383 | STATIC SV* |
7918f24d NC |
5384 | S_reg_scan_name(pTHX_ RExC_state_t *pRExC_state, U32 flags) |
5385 | { | |
894be9b7 | 5386 | char *name_start = RExC_parse; |
1f1031fe | 5387 | |
7918f24d NC |
5388 | PERL_ARGS_ASSERT_REG_SCAN_NAME; |
5389 | ||
1f1031fe YO |
5390 | if (isIDFIRST_lazy_if(RExC_parse, UTF)) { |
5391 | /* skip IDFIRST by using do...while */ | |
5392 | if (UTF) | |
5393 | do { | |
5394 | RExC_parse += UTF8SKIP(RExC_parse); | |
5395 | } while (isALNUM_utf8((U8*)RExC_parse)); | |
5396 | else | |
5397 | do { | |
5398 | RExC_parse++; | |
5399 | } while (isALNUM(*RExC_parse)); | |
894be9b7 | 5400 | } |
1f1031fe | 5401 | |
0a4db386 | 5402 | if ( flags ) { |
59cd0e26 NC |
5403 | SV* sv_name |
5404 | = newSVpvn_flags(name_start, (int)(RExC_parse - name_start), | |
5405 | SVs_TEMP | (UTF ? SVf_UTF8 : 0)); | |
0a4db386 YO |
5406 | if ( flags == REG_RSN_RETURN_NAME) |
5407 | return sv_name; | |
5408 | else if (flags==REG_RSN_RETURN_DATA) { | |
5409 | HE *he_str = NULL; | |
5410 | SV *sv_dat = NULL; | |
5411 | if ( ! sv_name ) /* should not happen*/ | |
5412 | Perl_croak(aTHX_ "panic: no svname in reg_scan_name"); | |
5413 | if (RExC_paren_names) | |
5414 | he_str = hv_fetch_ent( RExC_paren_names, sv_name, 0, 0 ); | |
5415 | if ( he_str ) | |
5416 | sv_dat = HeVAL(he_str); | |
5417 | if ( ! sv_dat ) | |
5418 | vFAIL("Reference to nonexistent named group"); | |
5419 | return sv_dat; | |
5420 | } | |
5421 | else { | |
5422 | Perl_croak(aTHX_ "panic: bad flag in reg_scan_name"); | |
5423 | } | |
5424 | /* NOT REACHED */ | |
894be9b7 | 5425 | } |
0a4db386 | 5426 | return NULL; |
894be9b7 YO |
5427 | } |
5428 | ||
3dab1dad YO |
5429 | #define DEBUG_PARSE_MSG(funcname) DEBUG_PARSE_r({ \ |
5430 | int rem=(int)(RExC_end - RExC_parse); \ | |
5431 | int cut; \ | |
5432 | int num; \ | |
5433 | int iscut=0; \ | |
5434 | if (rem>10) { \ | |
5435 | rem=10; \ | |
5436 | iscut=1; \ | |
5437 | } \ | |
5438 | cut=10-rem; \ | |
5439 | if (RExC_lastparse!=RExC_parse) \ | |
5440 | PerlIO_printf(Perl_debug_log," >%.*s%-*s", \ | |
5441 | rem, RExC_parse, \ | |
5442 | cut + 4, \ | |
5443 | iscut ? "..." : "<" \ | |
5444 | ); \ | |
5445 | else \ | |
5446 | PerlIO_printf(Perl_debug_log,"%16s",""); \ | |
5447 | \ | |
5448 | if (SIZE_ONLY) \ | |
3b57cd43 | 5449 | num = RExC_size + 1; \ |
3dab1dad YO |
5450 | else \ |
5451 | num=REG_NODE_NUM(RExC_emit); \ | |
5452 | if (RExC_lastnum!=num) \ | |
0a4db386 | 5453 | PerlIO_printf(Perl_debug_log,"|%4d",num); \ |
3dab1dad | 5454 | else \ |
0a4db386 | 5455 | PerlIO_printf(Perl_debug_log,"|%4s",""); \ |
be8e71aa YO |
5456 | PerlIO_printf(Perl_debug_log,"|%*s%-4s", \ |
5457 | (int)((depth*2)), "", \ | |
3dab1dad YO |
5458 | (funcname) \ |
5459 | ); \ | |
5460 | RExC_lastnum=num; \ | |
5461 | RExC_lastparse=RExC_parse; \ | |
5462 | }) | |
5463 | ||
07be1b83 YO |
5464 | |
5465 | ||
3dab1dad YO |
5466 | #define DEBUG_PARSE(funcname) DEBUG_PARSE_r({ \ |
5467 | DEBUG_PARSE_MSG((funcname)); \ | |
5468 | PerlIO_printf(Perl_debug_log,"%4s","\n"); \ | |
5469 | }) | |
6bda09f9 YO |
5470 | #define DEBUG_PARSE_FMT(funcname,fmt,args) DEBUG_PARSE_r({ \ |
5471 | DEBUG_PARSE_MSG((funcname)); \ | |
5472 | PerlIO_printf(Perl_debug_log,fmt "\n",args); \ | |
5473 | }) | |
a687059c LW |
5474 | /* |
5475 | - reg - regular expression, i.e. main body or parenthesized thing | |
5476 | * | |
5477 | * Caller must absorb opening parenthesis. | |
5478 | * | |
5479 | * Combining parenthesis handling with the base level of regular expression | |
5480 | * is a trifle forced, but the need to tie the tails of the branches to what | |
5481 | * follows makes it hard to avoid. | |
5482 | */ | |
07be1b83 YO |
5483 | #define REGTAIL(x,y,z) regtail((x),(y),(z),depth+1) |
5484 | #ifdef DEBUGGING | |
5485 | #define REGTAIL_STUDY(x,y,z) regtail_study((x),(y),(z),depth+1) | |
5486 | #else | |
5487 | #define REGTAIL_STUDY(x,y,z) regtail((x),(y),(z),depth+1) | |
5488 | #endif | |
3dab1dad | 5489 | |
76e3520e | 5490 | STATIC regnode * |
3dab1dad | 5491 | S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp,U32 depth) |
c277df42 | 5492 | /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */ |
a687059c | 5493 | { |
27da23d5 | 5494 | dVAR; |
c277df42 IZ |
5495 | register regnode *ret; /* Will be the head of the group. */ |
5496 | register regnode *br; | |
5497 | register regnode *lastbr; | |
cbbf8932 | 5498 | register regnode *ender = NULL; |
a0d0e21e | 5499 | register I32 parno = 0; |
cbbf8932 | 5500 | I32 flags; |
f7819f85 | 5501 | U32 oregflags = RExC_flags; |
6136c704 AL |
5502 | bool have_branch = 0; |
5503 | bool is_open = 0; | |
594d7033 YO |
5504 | I32 freeze_paren = 0; |
5505 | I32 after_freeze = 0; | |
9d1d55b5 JP |
5506 | |
5507 | /* for (?g), (?gc), and (?o) warnings; warning | |
5508 | about (?c) will warn about (?g) -- japhy */ | |
5509 | ||
6136c704 AL |
5510 | #define WASTED_O 0x01 |
5511 | #define WASTED_G 0x02 | |
5512 | #define WASTED_C 0x04 | |
5513 | #define WASTED_GC (0x02|0x04) | |
cbbf8932 | 5514 | I32 wastedflags = 0x00; |
9d1d55b5 | 5515 | |
fac92740 | 5516 | char * parse_start = RExC_parse; /* MJD */ |
a28509cc | 5517 | char * const oregcomp_parse = RExC_parse; |
a0d0e21e | 5518 | |
3dab1dad | 5519 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
5520 | |
5521 | PERL_ARGS_ASSERT_REG; | |
3dab1dad YO |
5522 | DEBUG_PARSE("reg "); |
5523 | ||
821b33a5 | 5524 | *flagp = 0; /* Tentatively. */ |
a0d0e21e | 5525 | |
9d1d55b5 | 5526 | |
a0d0e21e LW |
5527 | /* Make an OPEN node, if parenthesized. */ |
5528 | if (paren) { | |
e2e6a0f1 YO |
5529 | if ( *RExC_parse == '*') { /* (*VERB:ARG) */ |
5530 | char *start_verb = RExC_parse; | |
5531 | STRLEN verb_len = 0; | |
5532 | char *start_arg = NULL; | |
5533 | unsigned char op = 0; | |
5534 | int argok = 1; | |
5535 | int internal_argval = 0; /* internal_argval is only useful if !argok */ | |
5536 | while ( *RExC_parse && *RExC_parse != ')' ) { | |
5537 | if ( *RExC_parse == ':' ) { | |
5538 | start_arg = RExC_parse + 1; | |
5539 | break; | |
5540 | } | |
5541 | RExC_parse++; | |
5542 | } | |
5543 | ++start_verb; | |
5544 | verb_len = RExC_parse - start_verb; | |
5545 | if ( start_arg ) { | |
5546 | RExC_parse++; | |
5547 | while ( *RExC_parse && *RExC_parse != ')' ) | |
5548 | RExC_parse++; | |
5549 | if ( *RExC_parse != ')' ) | |
5550 | vFAIL("Unterminated verb pattern argument"); | |
5551 | if ( RExC_parse == start_arg ) | |
5552 | start_arg = NULL; | |
5553 | } else { | |
5554 | if ( *RExC_parse != ')' ) | |
5555 | vFAIL("Unterminated verb pattern"); | |
5556 | } | |
5d458dd8 | 5557 | |
e2e6a0f1 YO |
5558 | switch ( *start_verb ) { |
5559 | case 'A': /* (*ACCEPT) */ | |
568a785a | 5560 | if ( memEQs(start_verb,verb_len,"ACCEPT") ) { |
e2e6a0f1 YO |
5561 | op = ACCEPT; |
5562 | internal_argval = RExC_nestroot; | |
5563 | } | |
5564 | break; | |
5565 | case 'C': /* (*COMMIT) */ | |
568a785a | 5566 | if ( memEQs(start_verb,verb_len,"COMMIT") ) |
e2e6a0f1 | 5567 | op = COMMIT; |
e2e6a0f1 YO |
5568 | break; |
5569 | case 'F': /* (*FAIL) */ | |
568a785a | 5570 | if ( verb_len==1 || memEQs(start_verb,verb_len,"FAIL") ) { |
e2e6a0f1 YO |
5571 | op = OPFAIL; |
5572 | argok = 0; | |
5573 | } | |
5574 | break; | |
5d458dd8 YO |
5575 | case ':': /* (*:NAME) */ |
5576 | case 'M': /* (*MARK:NAME) */ | |
568a785a | 5577 | if ( verb_len==0 || memEQs(start_verb,verb_len,"MARK") ) { |
e2e6a0f1 | 5578 | op = MARKPOINT; |
5d458dd8 YO |
5579 | argok = -1; |
5580 | } | |
5581 | break; | |
5582 | case 'P': /* (*PRUNE) */ | |
568a785a | 5583 | if ( memEQs(start_verb,verb_len,"PRUNE") ) |
5d458dd8 | 5584 | op = PRUNE; |
e2e6a0f1 | 5585 | break; |
5d458dd8 | 5586 | case 'S': /* (*SKIP) */ |
568a785a | 5587 | if ( memEQs(start_verb,verb_len,"SKIP") ) |
5d458dd8 YO |
5588 | op = SKIP; |
5589 | break; | |
5590 | case 'T': /* (*THEN) */ | |
5591 | /* [19:06] <TimToady> :: is then */ | |
568a785a | 5592 | if ( memEQs(start_verb,verb_len,"THEN") ) { |
5d458dd8 YO |
5593 | op = CUTGROUP; |
5594 | RExC_seen |= REG_SEEN_CUTGROUP; | |
5595 | } | |
e2e6a0f1 YO |
5596 | break; |
5597 | } | |
5598 | if ( ! op ) { | |
5599 | RExC_parse++; | |
5600 | vFAIL3("Unknown verb pattern '%.*s'", | |
5601 | verb_len, start_verb); | |
5602 | } | |
5603 | if ( argok ) { | |
5604 | if ( start_arg && internal_argval ) { | |
5605 | vFAIL3("Verb pattern '%.*s' may not have an argument", | |
5606 | verb_len, start_verb); | |
5607 | } else if ( argok < 0 && !start_arg ) { | |
5608 | vFAIL3("Verb pattern '%.*s' has a mandatory argument", | |
5609 | verb_len, start_verb); | |
5610 | } else { | |
5611 | ret = reganode(pRExC_state, op, internal_argval); | |
5612 | if ( ! internal_argval && ! SIZE_ONLY ) { | |
5613 | if (start_arg) { | |
5614 | SV *sv = newSVpvn( start_arg, RExC_parse - start_arg); | |
5615 | ARG(ret) = add_data( pRExC_state, 1, "S" ); | |
f8fc2ecf | 5616 | RExC_rxi->data->data[ARG(ret)]=(void*)sv; |
e2e6a0f1 YO |
5617 | ret->flags = 0; |
5618 | } else { | |
5619 | ret->flags = 1; | |
5620 | } | |
5621 | } | |
5622 | } | |
5623 | if (!internal_argval) | |
5624 | RExC_seen |= REG_SEEN_VERBARG; | |
5625 | } else if ( start_arg ) { | |
5626 | vFAIL3("Verb pattern '%.*s' may not have an argument", | |
5627 | verb_len, start_verb); | |
5628 | } else { | |
5629 | ret = reg_node(pRExC_state, op); | |
5630 | } | |
5631 | nextchar(pRExC_state); | |
5632 | return ret; | |
5633 | } else | |
fac92740 | 5634 | if (*RExC_parse == '?') { /* (?...) */ |
6136c704 | 5635 | bool is_logical = 0; |
a28509cc | 5636 | const char * const seqstart = RExC_parse; |
ca9dfc88 | 5637 | |
830247a4 IZ |
5638 | RExC_parse++; |
5639 | paren = *RExC_parse++; | |
c277df42 | 5640 | ret = NULL; /* For look-ahead/behind. */ |
a0d0e21e | 5641 | switch (paren) { |
894be9b7 | 5642 | |
1f1031fe YO |
5643 | case 'P': /* (?P...) variants for those used to PCRE/Python */ |
5644 | paren = *RExC_parse++; | |
5645 | if ( paren == '<') /* (?P<...>) named capture */ | |
5646 | goto named_capture; | |
5647 | else if (paren == '>') { /* (?P>name) named recursion */ | |
5648 | goto named_recursion; | |
5649 | } | |
5650 | else if (paren == '=') { /* (?P=...) named backref */ | |
5651 | /* this pretty much dupes the code for \k<NAME> in regatom(), if | |
5652 | you change this make sure you change that */ | |
5653 | char* name_start = RExC_parse; | |
5654 | U32 num = 0; | |
5655 | SV *sv_dat = reg_scan_name(pRExC_state, | |
5656 | SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA); | |
5657 | if (RExC_parse == name_start || *RExC_parse != ')') | |
5658 | vFAIL2("Sequence %.3s... not terminated",parse_start); | |
5659 | ||
5660 | if (!SIZE_ONLY) { | |
5661 | num = add_data( pRExC_state, 1, "S" ); | |
5662 | RExC_rxi->data->data[num]=(void*)sv_dat; | |
5a5094bd | 5663 | SvREFCNT_inc_simple_void(sv_dat); |
1f1031fe YO |
5664 | } |
5665 | RExC_sawback = 1; | |
5666 | ret = reganode(pRExC_state, | |
5667 | (U8)(FOLD ? (LOC ? NREFFL : NREFF) : NREF), | |
5668 | num); | |
5669 | *flagp |= HASWIDTH; | |
5670 | ||
5671 | Set_Node_Offset(ret, parse_start+1); | |
5672 | Set_Node_Cur_Length(ret); /* MJD */ | |
5673 | ||
5674 | nextchar(pRExC_state); | |
5675 | return ret; | |
5676 | } | |
57b84237 YO |
5677 | RExC_parse++; |
5678 | vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart); | |
5679 | /*NOTREACHED*/ | |
5680 | case '<': /* (?<...) */ | |
b81d288d | 5681 | if (*RExC_parse == '!') |
c277df42 | 5682 | paren = ','; |
0a4db386 | 5683 | else if (*RExC_parse != '=') |
1f1031fe | 5684 | named_capture: |
0a4db386 | 5685 | { /* (?<...>) */ |
81714fb9 | 5686 | char *name_start; |
894be9b7 | 5687 | SV *svname; |
81714fb9 YO |
5688 | paren= '>'; |
5689 | case '\'': /* (?'...') */ | |
5690 | name_start= RExC_parse; | |
0a4db386 YO |
5691 | svname = reg_scan_name(pRExC_state, |
5692 | SIZE_ONLY ? /* reverse test from the others */ | |
5693 | REG_RSN_RETURN_NAME : | |
5694 | REG_RSN_RETURN_NULL); | |
57b84237 YO |
5695 | if (RExC_parse == name_start) { |
5696 | RExC_parse++; | |
5697 | vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart); | |
5698 | /*NOTREACHED*/ | |
5699 | } | |
81714fb9 YO |
5700 | if (*RExC_parse != paren) |
5701 | vFAIL2("Sequence (?%c... not terminated", | |
5702 | paren=='>' ? '<' : paren); | |
5703 | if (SIZE_ONLY) { | |
e62cc96a YO |
5704 | HE *he_str; |
5705 | SV *sv_dat = NULL; | |
894be9b7 YO |
5706 | if (!svname) /* shouldnt happen */ |
5707 | Perl_croak(aTHX_ | |
5708 | "panic: reg_scan_name returned NULL"); | |
81714fb9 YO |
5709 | if (!RExC_paren_names) { |
5710 | RExC_paren_names= newHV(); | |
ad64d0ec | 5711 | sv_2mortal(MUTABLE_SV(RExC_paren_names)); |
1f1031fe YO |
5712 | #ifdef DEBUGGING |
5713 | RExC_paren_name_list= newAV(); | |
ad64d0ec | 5714 | sv_2mortal(MUTABLE_SV(RExC_paren_name_list)); |
1f1031fe | 5715 | #endif |
81714fb9 YO |
5716 | } |
5717 | he_str = hv_fetch_ent( RExC_paren_names, svname, 1, 0 ); | |
e62cc96a | 5718 | if ( he_str ) |
81714fb9 | 5719 | sv_dat = HeVAL(he_str); |
e62cc96a | 5720 | if ( ! sv_dat ) { |
81714fb9 | 5721 | /* croak baby croak */ |
e62cc96a YO |
5722 | Perl_croak(aTHX_ |
5723 | "panic: paren_name hash element allocation failed"); | |
5724 | } else if ( SvPOK(sv_dat) ) { | |
76a476f9 YO |
5725 | /* (?|...) can mean we have dupes so scan to check |
5726 | its already been stored. Maybe a flag indicating | |
5727 | we are inside such a construct would be useful, | |
5728 | but the arrays are likely to be quite small, so | |
5729 | for now we punt -- dmq */ | |
5730 | IV count = SvIV(sv_dat); | |
5731 | I32 *pv = (I32*)SvPVX(sv_dat); | |
5732 | IV i; | |
5733 | for ( i = 0 ; i < count ; i++ ) { | |
5734 | if ( pv[i] == RExC_npar ) { | |
5735 | count = 0; | |
5736 | break; | |
5737 | } | |
5738 | } | |
5739 | if ( count ) { | |
5740 | pv = (I32*)SvGROW(sv_dat, SvCUR(sv_dat) + sizeof(I32)+1); | |
5741 | SvCUR_set(sv_dat, SvCUR(sv_dat) + sizeof(I32)); | |
5742 | pv[count] = RExC_npar; | |
3a92e6ae | 5743 | SvIV_set(sv_dat, SvIVX(sv_dat) + 1); |
76a476f9 | 5744 | } |
81714fb9 YO |
5745 | } else { |
5746 | (void)SvUPGRADE(sv_dat,SVt_PVNV); | |
5747 | sv_setpvn(sv_dat, (char *)&(RExC_npar), sizeof(I32)); | |
5748 | SvIOK_on(sv_dat); | |
3ec35e0f | 5749 | SvIV_set(sv_dat, 1); |
e62cc96a | 5750 | } |
1f1031fe YO |
5751 | #ifdef DEBUGGING |
5752 | if (!av_store(RExC_paren_name_list, RExC_npar, SvREFCNT_inc(svname))) | |
5753 | SvREFCNT_dec(svname); | |
5754 | #endif | |
e62cc96a | 5755 | |
81714fb9 YO |
5756 | /*sv_dump(sv_dat);*/ |
5757 | } | |
5758 | nextchar(pRExC_state); | |
5759 | paren = 1; | |
5760 | goto capturing_parens; | |
5761 | } | |
5762 | RExC_seen |= REG_SEEN_LOOKBEHIND; | |
830247a4 | 5763 | RExC_parse++; |
fac92740 | 5764 | case '=': /* (?=...) */ |
89c6a13e AB |
5765 | RExC_seen_zerolen++; |
5766 | break; | |
fac92740 | 5767 | case '!': /* (?!...) */ |
830247a4 | 5768 | RExC_seen_zerolen++; |
e2e6a0f1 YO |
5769 | if (*RExC_parse == ')') { |
5770 | ret=reg_node(pRExC_state, OPFAIL); | |
5771 | nextchar(pRExC_state); | |
5772 | return ret; | |
5773 | } | |
594d7033 YO |
5774 | break; |
5775 | case '|': /* (?|...) */ | |
5776 | /* branch reset, behave like a (?:...) except that | |
5777 | buffers in alternations share the same numbers */ | |
5778 | paren = ':'; | |
5779 | after_freeze = freeze_paren = RExC_npar; | |
5780 | break; | |
fac92740 MJD |
5781 | case ':': /* (?:...) */ |
5782 | case '>': /* (?>...) */ | |
a0d0e21e | 5783 | break; |
fac92740 MJD |
5784 | case '$': /* (?$...) */ |
5785 | case '@': /* (?@...) */ | |
8615cb43 | 5786 | vFAIL2("Sequence (?%c...) not implemented", (int)paren); |
a0d0e21e | 5787 | break; |
fac92740 | 5788 | case '#': /* (?#...) */ |
830247a4 IZ |
5789 | while (*RExC_parse && *RExC_parse != ')') |
5790 | RExC_parse++; | |
5791 | if (*RExC_parse != ')') | |
c277df42 | 5792 | FAIL("Sequence (?#... not terminated"); |
830247a4 | 5793 | nextchar(pRExC_state); |
a0d0e21e LW |
5794 | *flagp = TRYAGAIN; |
5795 | return NULL; | |
894be9b7 YO |
5796 | case '0' : /* (?0) */ |
5797 | case 'R' : /* (?R) */ | |
5798 | if (*RExC_parse != ')') | |
6bda09f9 | 5799 | FAIL("Sequence (?R) not terminated"); |
1a147d38 | 5800 | ret = reg_node(pRExC_state, GOSTART); |
a3b492c3 | 5801 | *flagp |= POSTPONED; |
7f69552c YO |
5802 | nextchar(pRExC_state); |
5803 | return ret; | |
5804 | /*notreached*/ | |
894be9b7 YO |
5805 | { /* named and numeric backreferences */ |
5806 | I32 num; | |
894be9b7 YO |
5807 | case '&': /* (?&NAME) */ |
5808 | parse_start = RExC_parse - 1; | |
1f1031fe | 5809 | named_recursion: |
894be9b7 | 5810 | { |
0a4db386 YO |
5811 | SV *sv_dat = reg_scan_name(pRExC_state, |
5812 | SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA); | |
5813 | num = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0; | |
894be9b7 YO |
5814 | } |
5815 | goto gen_recurse_regop; | |
5816 | /* NOT REACHED */ | |
542fa716 YO |
5817 | case '+': |
5818 | if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) { | |
5819 | RExC_parse++; | |
5820 | vFAIL("Illegal pattern"); | |
5821 | } | |
5822 | goto parse_recursion; | |
5823 | /* NOT REACHED*/ | |
5824 | case '-': /* (?-1) */ | |
5825 | if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) { | |
5826 | RExC_parse--; /* rewind to let it be handled later */ | |
5827 | goto parse_flags; | |
5828 | } | |
5829 | /*FALLTHROUGH */ | |
6bda09f9 YO |
5830 | case '1': case '2': case '3': case '4': /* (?1) */ |
5831 | case '5': case '6': case '7': case '8': case '9': | |
5832 | RExC_parse--; | |
542fa716 | 5833 | parse_recursion: |
894be9b7 YO |
5834 | num = atoi(RExC_parse); |
5835 | parse_start = RExC_parse - 1; /* MJD */ | |
542fa716 YO |
5836 | if (*RExC_parse == '-') |
5837 | RExC_parse++; | |
6bda09f9 YO |
5838 | while (isDIGIT(*RExC_parse)) |
5839 | RExC_parse++; | |
5840 | if (*RExC_parse!=')') | |
5841 | vFAIL("Expecting close bracket"); | |
894be9b7 YO |
5842 | |
5843 | gen_recurse_regop: | |
542fa716 YO |
5844 | if ( paren == '-' ) { |
5845 | /* | |
5846 | Diagram of capture buffer numbering. | |
5847 | Top line is the normal capture buffer numbers | |
5848 | Botton line is the negative indexing as from | |
5849 | the X (the (?-2)) | |
5850 | ||
5851 | + 1 2 3 4 5 X 6 7 | |
5852 | /(a(x)y)(a(b(c(?-2)d)e)f)(g(h))/ | |
5853 | - 5 4 3 2 1 X x x | |
5854 | ||
5855 | */ | |
5856 | num = RExC_npar + num; | |
5857 | if (num < 1) { | |
5858 | RExC_parse++; | |
5859 | vFAIL("Reference to nonexistent group"); | |
5860 | } | |
5861 | } else if ( paren == '+' ) { | |
5862 | num = RExC_npar + num - 1; | |
5863 | } | |
5864 | ||
1a147d38 | 5865 | ret = reganode(pRExC_state, GOSUB, num); |
6bda09f9 YO |
5866 | if (!SIZE_ONLY) { |
5867 | if (num > (I32)RExC_rx->nparens) { | |
5868 | RExC_parse++; | |
5869 | vFAIL("Reference to nonexistent group"); | |
5870 | } | |
40d049e4 | 5871 | ARG2L_SET( ret, RExC_recurse_count++); |
6bda09f9 | 5872 | RExC_emit++; |
226de585 | 5873 | DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log, |
acff02b8 | 5874 | "Recurse #%"UVuf" to %"IVdf"\n", (UV)ARG(ret), (IV)ARG2L(ret))); |
894be9b7 | 5875 | } else { |
6bda09f9 | 5876 | RExC_size++; |
6bda09f9 | 5877 | } |
0a4db386 | 5878 | RExC_seen |= REG_SEEN_RECURSE; |
6bda09f9 | 5879 | Set_Node_Length(ret, 1 + regarglen[OP(ret)]); /* MJD */ |
58663417 RGS |
5880 | Set_Node_Offset(ret, parse_start); /* MJD */ |
5881 | ||
a3b492c3 | 5882 | *flagp |= POSTPONED; |
6bda09f9 YO |
5883 | nextchar(pRExC_state); |
5884 | return ret; | |
894be9b7 YO |
5885 | } /* named and numeric backreferences */ |
5886 | /* NOT REACHED */ | |
5887 | ||
fac92740 | 5888 | case '?': /* (??...) */ |
6136c704 | 5889 | is_logical = 1; |
57b84237 YO |
5890 | if (*RExC_parse != '{') { |
5891 | RExC_parse++; | |
5892 | vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart); | |
5893 | /*NOTREACHED*/ | |
5894 | } | |
a3b492c3 | 5895 | *flagp |= POSTPONED; |
830247a4 | 5896 | paren = *RExC_parse++; |
0f5d15d6 | 5897 | /* FALL THROUGH */ |
fac92740 | 5898 | case '{': /* (?{...}) */ |
c277df42 | 5899 | { |
2eccd3b2 NC |
5900 | I32 count = 1; |
5901 | U32 n = 0; | |
c277df42 | 5902 | char c; |
830247a4 | 5903 | char *s = RExC_parse; |
c277df42 | 5904 | |
830247a4 IZ |
5905 | RExC_seen_zerolen++; |
5906 | RExC_seen |= REG_SEEN_EVAL; | |
5907 | while (count && (c = *RExC_parse)) { | |
6136c704 AL |
5908 | if (c == '\\') { |
5909 | if (RExC_parse[1]) | |
5910 | RExC_parse++; | |
5911 | } | |
b81d288d | 5912 | else if (c == '{') |
c277df42 | 5913 | count++; |
b81d288d | 5914 | else if (c == '}') |
c277df42 | 5915 | count--; |
830247a4 | 5916 | RExC_parse++; |
c277df42 | 5917 | } |
6136c704 | 5918 | if (*RExC_parse != ')') { |
b81d288d | 5919 | RExC_parse = s; |
b45f050a JF |
5920 | vFAIL("Sequence (?{...}) not terminated or not {}-balanced"); |
5921 | } | |
c277df42 | 5922 | if (!SIZE_ONLY) { |
f3548bdc | 5923 | PAD *pad; |
6136c704 AL |
5924 | OP_4tree *sop, *rop; |
5925 | SV * const sv = newSVpvn(s, RExC_parse - 1 - s); | |
c277df42 | 5926 | |
569233ed SB |
5927 | ENTER; |
5928 | Perl_save_re_context(aTHX); | |
f3548bdc | 5929 | rop = sv_compile_2op(sv, &sop, "re", &pad); |
9b978d73 DM |
5930 | sop->op_private |= OPpREFCOUNTED; |
5931 | /* re_dup will OpREFCNT_inc */ | |
5932 | OpREFCNT_set(sop, 1); | |
569233ed | 5933 | LEAVE; |
c277df42 | 5934 | |
830247a4 | 5935 | n = add_data(pRExC_state, 3, "nop"); |
f8fc2ecf YO |
5936 | RExC_rxi->data->data[n] = (void*)rop; |
5937 | RExC_rxi->data->data[n+1] = (void*)sop; | |
5938 | RExC_rxi->data->data[n+2] = (void*)pad; | |
c277df42 | 5939 | SvREFCNT_dec(sv); |
a0ed51b3 | 5940 | } |
e24b16f9 | 5941 | else { /* First pass */ |
830247a4 | 5942 | if (PL_reginterp_cnt < ++RExC_seen_evals |
923e4eb5 | 5943 | && IN_PERL_RUNTIME) |
2cd61cdb IZ |
5944 | /* No compiled RE interpolated, has runtime |
5945 | components ===> unsafe. */ | |
5946 | FAIL("Eval-group not allowed at runtime, use re 'eval'"); | |
5b61d3f7 | 5947 | if (PL_tainting && PL_tainted) |
cc6b7395 | 5948 | FAIL("Eval-group in insecure regular expression"); |
54df2634 | 5949 | #if PERL_VERSION > 8 |
923e4eb5 | 5950 | if (IN_PERL_COMPILETIME) |
b5c19bd7 | 5951 | PL_cv_has_eval = 1; |
54df2634 | 5952 | #endif |
c277df42 | 5953 | } |
b5c19bd7 | 5954 | |
830247a4 | 5955 | nextchar(pRExC_state); |
6136c704 | 5956 | if (is_logical) { |
830247a4 | 5957 | ret = reg_node(pRExC_state, LOGICAL); |
0f5d15d6 IZ |
5958 | if (!SIZE_ONLY) |
5959 | ret->flags = 2; | |
3dab1dad | 5960 | REGTAIL(pRExC_state, ret, reganode(pRExC_state, EVAL, n)); |
fac92740 | 5961 | /* deal with the length of this later - MJD */ |
0f5d15d6 IZ |
5962 | return ret; |
5963 | } | |
ccb2c380 MP |
5964 | ret = reganode(pRExC_state, EVAL, n); |
5965 | Set_Node_Length(ret, RExC_parse - parse_start + 1); | |
5966 | Set_Node_Offset(ret, parse_start); | |
5967 | return ret; | |
c277df42 | 5968 | } |
fac92740 | 5969 | case '(': /* (?(?{...})...) and (?(?=...)...) */ |
c277df42 | 5970 | { |
0a4db386 | 5971 | int is_define= 0; |
fac92740 | 5972 | if (RExC_parse[0] == '?') { /* (?(?...)) */ |
b81d288d AB |
5973 | if (RExC_parse[1] == '=' || RExC_parse[1] == '!' |
5974 | || RExC_parse[1] == '<' | |
830247a4 | 5975 | || RExC_parse[1] == '{') { /* Lookahead or eval. */ |
c277df42 IZ |
5976 | I32 flag; |
5977 | ||
830247a4 | 5978 | ret = reg_node(pRExC_state, LOGICAL); |
0f5d15d6 IZ |
5979 | if (!SIZE_ONLY) |
5980 | ret->flags = 1; | |
3dab1dad | 5981 | REGTAIL(pRExC_state, ret, reg(pRExC_state, 1, &flag,depth+1)); |
c277df42 | 5982 | goto insert_if; |
b81d288d | 5983 | } |
a0ed51b3 | 5984 | } |
0a4db386 YO |
5985 | else if ( RExC_parse[0] == '<' /* (?(<NAME>)...) */ |
5986 | || RExC_parse[0] == '\'' ) /* (?('NAME')...) */ | |
5987 | { | |
5988 | char ch = RExC_parse[0] == '<' ? '>' : '\''; | |
5989 | char *name_start= RExC_parse++; | |
2eccd3b2 | 5990 | U32 num = 0; |
0a4db386 YO |
5991 | SV *sv_dat=reg_scan_name(pRExC_state, |
5992 | SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA); | |
5993 | if (RExC_parse == name_start || *RExC_parse != ch) | |
5994 | vFAIL2("Sequence (?(%c... not terminated", | |
5995 | (ch == '>' ? '<' : ch)); | |
5996 | RExC_parse++; | |
5997 | if (!SIZE_ONLY) { | |
5998 | num = add_data( pRExC_state, 1, "S" ); | |
f8fc2ecf | 5999 | RExC_rxi->data->data[num]=(void*)sv_dat; |
5a5094bd | 6000 | SvREFCNT_inc_simple_void(sv_dat); |
0a4db386 YO |
6001 | } |
6002 | ret = reganode(pRExC_state,NGROUPP,num); | |
6003 | goto insert_if_check_paren; | |
6004 | } | |
6005 | else if (RExC_parse[0] == 'D' && | |
6006 | RExC_parse[1] == 'E' && | |
6007 | RExC_parse[2] == 'F' && | |
6008 | RExC_parse[3] == 'I' && | |
6009 | RExC_parse[4] == 'N' && | |
6010 | RExC_parse[5] == 'E') | |
6011 | { | |
6012 | ret = reganode(pRExC_state,DEFINEP,0); | |
6013 | RExC_parse +=6 ; | |
6014 | is_define = 1; | |
6015 | goto insert_if_check_paren; | |
6016 | } | |
6017 | else if (RExC_parse[0] == 'R') { | |
6018 | RExC_parse++; | |
6019 | parno = 0; | |
6020 | if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) { | |
6021 | parno = atoi(RExC_parse++); | |
6022 | while (isDIGIT(*RExC_parse)) | |
6023 | RExC_parse++; | |
6024 | } else if (RExC_parse[0] == '&') { | |
6025 | SV *sv_dat; | |
6026 | RExC_parse++; | |
6027 | sv_dat = reg_scan_name(pRExC_state, | |
6028 | SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA); | |
6029 | parno = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0; | |
6030 | } | |
1a147d38 | 6031 | ret = reganode(pRExC_state,INSUBP,parno); |
0a4db386 YO |
6032 | goto insert_if_check_paren; |
6033 | } | |
830247a4 | 6034 | else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) { |
fac92740 | 6035 | /* (?(1)...) */ |
6136c704 | 6036 | char c; |
830247a4 | 6037 | parno = atoi(RExC_parse++); |
c277df42 | 6038 | |
830247a4 IZ |
6039 | while (isDIGIT(*RExC_parse)) |
6040 | RExC_parse++; | |
fac92740 | 6041 | ret = reganode(pRExC_state, GROUPP, parno); |
2af232bd | 6042 | |
0a4db386 | 6043 | insert_if_check_paren: |
830247a4 | 6044 | if ((c = *nextchar(pRExC_state)) != ')') |
b45f050a | 6045 | vFAIL("Switch condition not recognized"); |
c277df42 | 6046 | insert_if: |
3dab1dad YO |
6047 | REGTAIL(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0)); |
6048 | br = regbranch(pRExC_state, &flags, 1,depth+1); | |
c277df42 | 6049 | if (br == NULL) |
830247a4 | 6050 | br = reganode(pRExC_state, LONGJMP, 0); |
c277df42 | 6051 | else |
3dab1dad | 6052 | REGTAIL(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0)); |
830247a4 | 6053 | c = *nextchar(pRExC_state); |
d1b80229 IZ |
6054 | if (flags&HASWIDTH) |
6055 | *flagp |= HASWIDTH; | |
c277df42 | 6056 | if (c == '|') { |
0a4db386 YO |
6057 | if (is_define) |
6058 | vFAIL("(?(DEFINE)....) does not allow branches"); | |
830247a4 | 6059 | lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */ |
3dab1dad YO |
6060 | regbranch(pRExC_state, &flags, 1,depth+1); |
6061 | REGTAIL(pRExC_state, ret, lastbr); | |
d1b80229 IZ |
6062 | if (flags&HASWIDTH) |
6063 | *flagp |= HASWIDTH; | |
830247a4 | 6064 | c = *nextchar(pRExC_state); |
a0ed51b3 LW |
6065 | } |
6066 | else | |
c277df42 IZ |
6067 | lastbr = NULL; |
6068 | if (c != ')') | |
8615cb43 | 6069 | vFAIL("Switch (?(condition)... contains too many branches"); |
830247a4 | 6070 | ender = reg_node(pRExC_state, TAIL); |
3dab1dad | 6071 | REGTAIL(pRExC_state, br, ender); |
c277df42 | 6072 | if (lastbr) { |
3dab1dad YO |
6073 | REGTAIL(pRExC_state, lastbr, ender); |
6074 | REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); | |
a0ed51b3 LW |
6075 | } |
6076 | else | |
3dab1dad | 6077 | REGTAIL(pRExC_state, ret, ender); |
3b57cd43 YO |
6078 | RExC_size++; /* XXX WHY do we need this?!! |
6079 | For large programs it seems to be required | |
6080 | but I can't figure out why. -- dmq*/ | |
c277df42 | 6081 | return ret; |
a0ed51b3 LW |
6082 | } |
6083 | else { | |
830247a4 | 6084 | vFAIL2("Unknown switch condition (?(%.2s", RExC_parse); |
c277df42 IZ |
6085 | } |
6086 | } | |
1b1626e4 | 6087 | case 0: |
830247a4 | 6088 | RExC_parse--; /* for vFAIL to print correctly */ |
8615cb43 | 6089 | vFAIL("Sequence (? incomplete"); |
1b1626e4 | 6090 | break; |
a0d0e21e | 6091 | default: |
cde0cee5 YO |
6092 | --RExC_parse; |
6093 | parse_flags: /* (?i) */ | |
6094 | { | |
6095 | U32 posflags = 0, negflags = 0; | |
6096 | U32 *flagsp = &posflags; | |
6097 | ||
6098 | while (*RExC_parse) { | |
6099 | /* && strchr("iogcmsx", *RExC_parse) */ | |
9d1d55b5 JP |
6100 | /* (?g), (?gc) and (?o) are useless here |
6101 | and must be globally applied -- japhy */ | |
cde0cee5 YO |
6102 | switch (*RExC_parse) { |
6103 | CASE_STD_PMMOD_FLAGS_PARSE_SET(flagsp); | |
f7819f85 A |
6104 | case ONCE_PAT_MOD: /* 'o' */ |
6105 | case GLOBAL_PAT_MOD: /* 'g' */ | |
9d1d55b5 | 6106 | if (SIZE_ONLY && ckWARN(WARN_REGEXP)) { |
6136c704 | 6107 | const I32 wflagbit = *RExC_parse == 'o' ? WASTED_O : WASTED_G; |
9d1d55b5 JP |
6108 | if (! (wastedflags & wflagbit) ) { |
6109 | wastedflags |= wflagbit; | |
6110 | vWARN5( | |
6111 | RExC_parse + 1, | |
6112 | "Useless (%s%c) - %suse /%c modifier", | |
6113 | flagsp == &negflags ? "?-" : "?", | |
6114 | *RExC_parse, | |
6115 | flagsp == &negflags ? "don't " : "", | |
6116 | *RExC_parse | |
6117 | ); | |
6118 | } | |
6119 | } | |
cde0cee5 YO |
6120 | break; |
6121 | ||
f7819f85 | 6122 | case CONTINUE_PAT_MOD: /* 'c' */ |
9d1d55b5 | 6123 | if (SIZE_ONLY && ckWARN(WARN_REGEXP)) { |
6136c704 AL |
6124 | if (! (wastedflags & WASTED_C) ) { |
6125 | wastedflags |= WASTED_GC; | |
9d1d55b5 JP |
6126 | vWARN3( |
6127 | RExC_parse + 1, | |
6128 | "Useless (%sc) - %suse /gc modifier", | |
6129 | flagsp == &negflags ? "?-" : "?", | |
6130 | flagsp == &negflags ? "don't " : "" | |
6131 | ); | |
6132 | } | |
6133 | } | |
cde0cee5 | 6134 | break; |
f7819f85 | 6135 | case KEEPCOPY_PAT_MOD: /* 'p' */ |
cde0cee5 | 6136 | if (flagsp == &negflags) { |
668c081a NC |
6137 | if (SIZE_ONLY) |
6138 | ckWARNreg(RExC_parse + 1,"Useless use of (?-p)"); | |
cde0cee5 YO |
6139 | } else { |
6140 | *flagsp |= RXf_PMf_KEEPCOPY; | |
6141 | } | |
6142 | break; | |
6143 | case '-': | |
57b84237 YO |
6144 | if (flagsp == &negflags) { |
6145 | RExC_parse++; | |
6146 | vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart); | |
6147 | /*NOTREACHED*/ | |
6148 | } | |
cde0cee5 YO |
6149 | flagsp = &negflags; |
6150 | wastedflags = 0; /* reset so (?g-c) warns twice */ | |
6151 | break; | |
6152 | case ':': | |
6153 | paren = ':'; | |
6154 | /*FALLTHROUGH*/ | |
6155 | case ')': | |
6156 | RExC_flags |= posflags; | |
6157 | RExC_flags &= ~negflags; | |
f7819f85 A |
6158 | if (paren != ':') { |
6159 | oregflags |= posflags; | |
6160 | oregflags &= ~negflags; | |
6161 | } | |
cde0cee5 YO |
6162 | nextchar(pRExC_state); |
6163 | if (paren != ':') { | |
6164 | *flagp = TRYAGAIN; | |
6165 | return NULL; | |
6166 | } else { | |
6167 | ret = NULL; | |
6168 | goto parse_rest; | |
6169 | } | |
6170 | /*NOTREACHED*/ | |
6171 | default: | |
cde0cee5 YO |
6172 | RExC_parse++; |
6173 | vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart); | |
6174 | /*NOTREACHED*/ | |
6175 | } | |
830247a4 | 6176 | ++RExC_parse; |
48c036b1 | 6177 | } |
cde0cee5 | 6178 | }} /* one for the default block, one for the switch */ |
a0d0e21e | 6179 | } |
fac92740 | 6180 | else { /* (...) */ |
81714fb9 | 6181 | capturing_parens: |
830247a4 IZ |
6182 | parno = RExC_npar; |
6183 | RExC_npar++; | |
e2e6a0f1 | 6184 | |
830247a4 | 6185 | ret = reganode(pRExC_state, OPEN, parno); |
e2e6a0f1 YO |
6186 | if (!SIZE_ONLY ){ |
6187 | if (!RExC_nestroot) | |
6188 | RExC_nestroot = parno; | |
c009da3d YO |
6189 | if (RExC_seen & REG_SEEN_RECURSE |
6190 | && !RExC_open_parens[parno-1]) | |
6191 | { | |
e2e6a0f1 | 6192 | DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log, |
40d049e4 YO |
6193 | "Setting open paren #%"IVdf" to %d\n", |
6194 | (IV)parno, REG_NODE_NUM(ret))); | |
e2e6a0f1 YO |
6195 | RExC_open_parens[parno-1]= ret; |
6196 | } | |
6bda09f9 | 6197 | } |
fac92740 MJD |
6198 | Set_Node_Length(ret, 1); /* MJD */ |
6199 | Set_Node_Offset(ret, RExC_parse); /* MJD */ | |
6136c704 | 6200 | is_open = 1; |
a0d0e21e | 6201 | } |
a0ed51b3 | 6202 | } |
fac92740 | 6203 | else /* ! paren */ |
a0d0e21e | 6204 | ret = NULL; |
cde0cee5 YO |
6205 | |
6206 | parse_rest: | |
a0d0e21e | 6207 | /* Pick up the branches, linking them together. */ |
fac92740 | 6208 | parse_start = RExC_parse; /* MJD */ |
3dab1dad | 6209 | br = regbranch(pRExC_state, &flags, 1,depth+1); |
ee91d26e VP |
6210 | |
6211 | if (freeze_paren) { | |
6212 | if (RExC_npar > after_freeze) | |
6213 | after_freeze = RExC_npar; | |
6214 | RExC_npar = freeze_paren; | |
6215 | } | |
6216 | ||
fac92740 | 6217 | /* branch_len = (paren != 0); */ |
2af232bd | 6218 | |
a0d0e21e LW |
6219 | if (br == NULL) |
6220 | return(NULL); | |
830247a4 IZ |
6221 | if (*RExC_parse == '|') { |
6222 | if (!SIZE_ONLY && RExC_extralen) { | |
6bda09f9 | 6223 | reginsert(pRExC_state, BRANCHJ, br, depth+1); |
a0ed51b3 | 6224 | } |
fac92740 | 6225 | else { /* MJD */ |
6bda09f9 | 6226 | reginsert(pRExC_state, BRANCH, br, depth+1); |
fac92740 MJD |
6227 | Set_Node_Length(br, paren != 0); |
6228 | Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start); | |
6229 | } | |
c277df42 IZ |
6230 | have_branch = 1; |
6231 | if (SIZE_ONLY) | |
830247a4 | 6232 | RExC_extralen += 1; /* For BRANCHJ-BRANCH. */ |
a0ed51b3 LW |
6233 | } |
6234 | else if (paren == ':') { | |
c277df42 IZ |
6235 | *flagp |= flags&SIMPLE; |
6236 | } | |
6136c704 | 6237 | if (is_open) { /* Starts with OPEN. */ |
3dab1dad | 6238 | REGTAIL(pRExC_state, ret, br); /* OPEN -> first. */ |
a0ed51b3 LW |
6239 | } |
6240 | else if (paren != '?') /* Not Conditional */ | |
a0d0e21e | 6241 | ret = br; |
8ae10a67 | 6242 | *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED); |
c277df42 | 6243 | lastbr = br; |
830247a4 IZ |
6244 | while (*RExC_parse == '|') { |
6245 | if (!SIZE_ONLY && RExC_extralen) { | |
6246 | ender = reganode(pRExC_state, LONGJMP,0); | |
3dab1dad | 6247 | REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */ |
c277df42 IZ |
6248 | } |
6249 | if (SIZE_ONLY) | |
830247a4 IZ |
6250 | RExC_extralen += 2; /* Account for LONGJMP. */ |
6251 | nextchar(pRExC_state); | |
594d7033 YO |
6252 | if (freeze_paren) { |
6253 | if (RExC_npar > after_freeze) | |
6254 | after_freeze = RExC_npar; | |
6255 | RExC_npar = freeze_paren; | |
6256 | } | |
3dab1dad | 6257 | br = regbranch(pRExC_state, &flags, 0, depth+1); |
2af232bd | 6258 | |
a687059c | 6259 | if (br == NULL) |
a0d0e21e | 6260 | return(NULL); |
3dab1dad | 6261 | REGTAIL(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */ |
c277df42 | 6262 | lastbr = br; |
8ae10a67 | 6263 | *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED); |
a0d0e21e LW |
6264 | } |
6265 | ||
c277df42 IZ |
6266 | if (have_branch || paren != ':') { |
6267 | /* Make a closing node, and hook it on the end. */ | |
6268 | switch (paren) { | |
6269 | case ':': | |
830247a4 | 6270 | ender = reg_node(pRExC_state, TAIL); |
c277df42 IZ |
6271 | break; |
6272 | case 1: | |
830247a4 | 6273 | ender = reganode(pRExC_state, CLOSE, parno); |
40d049e4 YO |
6274 | if (!SIZE_ONLY && RExC_seen & REG_SEEN_RECURSE) { |
6275 | DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log, | |
6276 | "Setting close paren #%"IVdf" to %d\n", | |
6277 | (IV)parno, REG_NODE_NUM(ender))); | |
6278 | RExC_close_parens[parno-1]= ender; | |
e2e6a0f1 YO |
6279 | if (RExC_nestroot == parno) |
6280 | RExC_nestroot = 0; | |
40d049e4 | 6281 | } |
fac92740 MJD |
6282 | Set_Node_Offset(ender,RExC_parse+1); /* MJD */ |
6283 | Set_Node_Length(ender,1); /* MJD */ | |
c277df42 IZ |
6284 | break; |
6285 | case '<': | |
c277df42 IZ |
6286 | case ',': |
6287 | case '=': | |
6288 | case '!': | |
c277df42 | 6289 | *flagp &= ~HASWIDTH; |
821b33a5 IZ |
6290 | /* FALL THROUGH */ |
6291 | case '>': | |
830247a4 | 6292 | ender = reg_node(pRExC_state, SUCCEED); |
c277df42 IZ |
6293 | break; |
6294 | case 0: | |
830247a4 | 6295 | ender = reg_node(pRExC_state, END); |
40d049e4 YO |
6296 | if (!SIZE_ONLY) { |
6297 | assert(!RExC_opend); /* there can only be one! */ | |
6298 | RExC_opend = ender; | |
6299 | } | |
c277df42 IZ |
6300 | break; |
6301 | } | |
eaf3ca90 | 6302 | REGTAIL(pRExC_state, lastbr, ender); |
a0d0e21e | 6303 | |
9674d46a | 6304 | if (have_branch && !SIZE_ONLY) { |
eaf3ca90 YO |
6305 | if (depth==1) |
6306 | RExC_seen |= REG_TOP_LEVEL_BRANCHES; | |
6307 | ||
c277df42 | 6308 | /* Hook the tails of the branches to the closing node. */ |
9674d46a AL |
6309 | for (br = ret; br; br = regnext(br)) { |
6310 | const U8 op = PL_regkind[OP(br)]; | |
6311 | if (op == BRANCH) { | |
07be1b83 | 6312 | REGTAIL_STUDY(pRExC_state, NEXTOPER(br), ender); |
9674d46a AL |
6313 | } |
6314 | else if (op == BRANCHJ) { | |
07be1b83 | 6315 | REGTAIL_STUDY(pRExC_state, NEXTOPER(NEXTOPER(br)), ender); |
9674d46a | 6316 | } |
c277df42 IZ |
6317 | } |
6318 | } | |
a0d0e21e | 6319 | } |
c277df42 IZ |
6320 | |
6321 | { | |
e1ec3a88 AL |
6322 | const char *p; |
6323 | static const char parens[] = "=!<,>"; | |
c277df42 IZ |
6324 | |
6325 | if (paren && (p = strchr(parens, paren))) { | |
eb160463 | 6326 | U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH; |
c277df42 IZ |
6327 | int flag = (p - parens) > 1; |
6328 | ||
6329 | if (paren == '>') | |
6330 | node = SUSPEND, flag = 0; | |
6bda09f9 | 6331 | reginsert(pRExC_state, node,ret, depth+1); |
45948336 EP |
6332 | Set_Node_Cur_Length(ret); |
6333 | Set_Node_Offset(ret, parse_start + 1); | |
c277df42 | 6334 | ret->flags = flag; |
07be1b83 | 6335 | REGTAIL_STUDY(pRExC_state, ret, reg_node(pRExC_state, TAIL)); |
c277df42 | 6336 | } |
a0d0e21e LW |
6337 | } |
6338 | ||
6339 | /* Check for proper termination. */ | |
ce3e6498 | 6340 | if (paren) { |
e2509266 | 6341 | RExC_flags = oregflags; |
830247a4 IZ |
6342 | if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') { |
6343 | RExC_parse = oregcomp_parse; | |
380a0633 | 6344 | vFAIL("Unmatched ("); |
ce3e6498 | 6345 | } |
a0ed51b3 | 6346 | } |
830247a4 IZ |
6347 | else if (!paren && RExC_parse < RExC_end) { |
6348 | if (*RExC_parse == ')') { | |
6349 | RExC_parse++; | |
380a0633 | 6350 | vFAIL("Unmatched )"); |
a0ed51b3 LW |
6351 | } |
6352 | else | |
b45f050a | 6353 | FAIL("Junk on end of regexp"); /* "Can't happen". */ |
a0d0e21e LW |
6354 | /* NOTREACHED */ |
6355 | } | |
594d7033 YO |
6356 | if (after_freeze) |
6357 | RExC_npar = after_freeze; | |
a0d0e21e | 6358 | return(ret); |
a687059c LW |
6359 | } |
6360 | ||
6361 | /* | |
6362 | - regbranch - one alternative of an | operator | |
6363 | * | |
6364 | * Implements the concatenation operator. | |
6365 | */ | |
76e3520e | 6366 | STATIC regnode * |
3dab1dad | 6367 | S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first, U32 depth) |
a687059c | 6368 | { |
97aff369 | 6369 | dVAR; |
c277df42 IZ |
6370 | register regnode *ret; |
6371 | register regnode *chain = NULL; | |
6372 | register regnode *latest; | |
6373 | I32 flags = 0, c = 0; | |
3dab1dad | 6374 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
6375 | |
6376 | PERL_ARGS_ASSERT_REGBRANCH; | |
6377 | ||
3dab1dad | 6378 | DEBUG_PARSE("brnc"); |
02daf0ab | 6379 | |
b81d288d | 6380 | if (first) |
c277df42 IZ |
6381 | ret = NULL; |
6382 | else { | |
b81d288d | 6383 | if (!SIZE_ONLY && RExC_extralen) |
830247a4 | 6384 | ret = reganode(pRExC_state, BRANCHJ,0); |
fac92740 | 6385 | else { |
830247a4 | 6386 | ret = reg_node(pRExC_state, BRANCH); |
fac92740 MJD |
6387 | Set_Node_Length(ret, 1); |
6388 | } | |
c277df42 IZ |
6389 | } |
6390 | ||
b81d288d | 6391 | if (!first && SIZE_ONLY) |
830247a4 | 6392 | RExC_extralen += 1; /* BRANCHJ */ |
b81d288d | 6393 | |
c277df42 | 6394 | *flagp = WORST; /* Tentatively. */ |
a0d0e21e | 6395 | |
830247a4 IZ |
6396 | RExC_parse--; |
6397 | nextchar(pRExC_state); | |
6398 | while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') { | |
a0d0e21e | 6399 | flags &= ~TRYAGAIN; |
3dab1dad | 6400 | latest = regpiece(pRExC_state, &flags,depth+1); |
a0d0e21e LW |
6401 | if (latest == NULL) { |
6402 | if (flags & TRYAGAIN) | |
6403 | continue; | |
6404 | return(NULL); | |
a0ed51b3 LW |
6405 | } |
6406 | else if (ret == NULL) | |
c277df42 | 6407 | ret = latest; |
8ae10a67 | 6408 | *flagp |= flags&(HASWIDTH|POSTPONED); |
c277df42 | 6409 | if (chain == NULL) /* First piece. */ |
a0d0e21e LW |
6410 | *flagp |= flags&SPSTART; |
6411 | else { | |
830247a4 | 6412 | RExC_naughty++; |
3dab1dad | 6413 | REGTAIL(pRExC_state, chain, latest); |
a687059c | 6414 | } |
a0d0e21e | 6415 | chain = latest; |
c277df42 IZ |
6416 | c++; |
6417 | } | |
6418 | if (chain == NULL) { /* Loop ran zero times. */ | |
830247a4 | 6419 | chain = reg_node(pRExC_state, NOTHING); |
c277df42 IZ |
6420 | if (ret == NULL) |
6421 | ret = chain; | |
6422 | } | |
6423 | if (c == 1) { | |
6424 | *flagp |= flags&SIMPLE; | |
a0d0e21e | 6425 | } |
a687059c | 6426 | |
d4c19fe8 | 6427 | return ret; |
a687059c LW |
6428 | } |
6429 | ||
6430 | /* | |
6431 | - regpiece - something followed by possible [*+?] | |
6432 | * | |
6433 | * Note that the branching code sequences used for ? and the general cases | |
6434 | * of * and + are somewhat optimized: they use the same NOTHING node as | |
6435 | * both the endmarker for their branch list and the body of the last branch. | |
6436 | * It might seem that this node could be dispensed with entirely, but the | |
6437 | * endmarker role is not redundant. | |
6438 | */ | |
76e3520e | 6439 | STATIC regnode * |
3dab1dad | 6440 | S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth) |
a687059c | 6441 | { |
97aff369 | 6442 | dVAR; |
c277df42 | 6443 | register regnode *ret; |
a0d0e21e LW |
6444 | register char op; |
6445 | register char *next; | |
6446 | I32 flags; | |
1df70142 | 6447 | const char * const origparse = RExC_parse; |
a0d0e21e | 6448 | I32 min; |
c277df42 | 6449 | I32 max = REG_INFTY; |
fac92740 | 6450 | char *parse_start; |
10edeb5d | 6451 | const char *maxpos = NULL; |
3dab1dad | 6452 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
6453 | |
6454 | PERL_ARGS_ASSERT_REGPIECE; | |
6455 | ||
3dab1dad | 6456 | DEBUG_PARSE("piec"); |
a0d0e21e | 6457 | |
3dab1dad | 6458 | ret = regatom(pRExC_state, &flags,depth+1); |
a0d0e21e LW |
6459 | if (ret == NULL) { |
6460 | if (flags & TRYAGAIN) | |
6461 | *flagp |= TRYAGAIN; | |
6462 | return(NULL); | |
6463 | } | |
6464 | ||
830247a4 | 6465 | op = *RExC_parse; |
a0d0e21e | 6466 | |
830247a4 | 6467 | if (op == '{' && regcurly(RExC_parse)) { |
10edeb5d | 6468 | maxpos = NULL; |
fac92740 | 6469 | parse_start = RExC_parse; /* MJD */ |
830247a4 | 6470 | next = RExC_parse + 1; |
a0d0e21e LW |
6471 | while (isDIGIT(*next) || *next == ',') { |
6472 | if (*next == ',') { | |
6473 | if (maxpos) | |
6474 | break; | |
6475 | else | |
6476 | maxpos = next; | |
a687059c | 6477 | } |
a0d0e21e LW |
6478 | next++; |
6479 | } | |
6480 | if (*next == '}') { /* got one */ | |
6481 | if (!maxpos) | |
6482 | maxpos = next; | |
830247a4 IZ |
6483 | RExC_parse++; |
6484 | min = atoi(RExC_parse); | |
a0d0e21e LW |
6485 | if (*maxpos == ',') |
6486 | maxpos++; | |
6487 | else | |
830247a4 | 6488 | maxpos = RExC_parse; |
a0d0e21e LW |
6489 | max = atoi(maxpos); |
6490 | if (!max && *maxpos != '0') | |
c277df42 IZ |
6491 | max = REG_INFTY; /* meaning "infinity" */ |
6492 | else if (max >= REG_INFTY) | |
8615cb43 | 6493 | vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1); |
830247a4 IZ |
6494 | RExC_parse = next; |
6495 | nextchar(pRExC_state); | |
a0d0e21e LW |
6496 | |
6497 | do_curly: | |
6498 | if ((flags&SIMPLE)) { | |
830247a4 | 6499 | RExC_naughty += 2 + RExC_naughty / 2; |
6bda09f9 | 6500 | reginsert(pRExC_state, CURLY, ret, depth+1); |
fac92740 MJD |
6501 | Set_Node_Offset(ret, parse_start+1); /* MJD */ |
6502 | Set_Node_Cur_Length(ret); | |
a0d0e21e LW |
6503 | } |
6504 | else { | |
3dab1dad | 6505 | regnode * const w = reg_node(pRExC_state, WHILEM); |
2c2d71f5 JH |
6506 | |
6507 | w->flags = 0; | |
3dab1dad | 6508 | REGTAIL(pRExC_state, ret, w); |
830247a4 | 6509 | if (!SIZE_ONLY && RExC_extralen) { |
6bda09f9 YO |
6510 | reginsert(pRExC_state, LONGJMP,ret, depth+1); |
6511 | reginsert(pRExC_state, NOTHING,ret, depth+1); | |
c277df42 IZ |
6512 | NEXT_OFF(ret) = 3; /* Go over LONGJMP. */ |
6513 | } | |
6bda09f9 | 6514 | reginsert(pRExC_state, CURLYX,ret, depth+1); |
fac92740 MJD |
6515 | /* MJD hk */ |
6516 | Set_Node_Offset(ret, parse_start+1); | |
2af232bd | 6517 | Set_Node_Length(ret, |
fac92740 | 6518 | op == '{' ? (RExC_parse - parse_start) : 1); |
2af232bd | 6519 | |
830247a4 | 6520 | if (!SIZE_ONLY && RExC_extralen) |
c277df42 | 6521 | NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */ |
3dab1dad | 6522 | REGTAIL(pRExC_state, ret, reg_node(pRExC_state, NOTHING)); |
c277df42 | 6523 | if (SIZE_ONLY) |
830247a4 IZ |
6524 | RExC_whilem_seen++, RExC_extralen += 3; |
6525 | RExC_naughty += 4 + RExC_naughty; /* compound interest */ | |
a0d0e21e | 6526 | } |
c277df42 | 6527 | ret->flags = 0; |
a0d0e21e LW |
6528 | |
6529 | if (min > 0) | |
821b33a5 IZ |
6530 | *flagp = WORST; |
6531 | if (max > 0) | |
6532 | *flagp |= HASWIDTH; | |
8fa23287 | 6533 | if (max < min) |
8615cb43 | 6534 | vFAIL("Can't do {n,m} with n > m"); |
c277df42 | 6535 | if (!SIZE_ONLY) { |
eb160463 GS |
6536 | ARG1_SET(ret, (U16)min); |
6537 | ARG2_SET(ret, (U16)max); | |
a687059c | 6538 | } |
a687059c | 6539 | |
a0d0e21e | 6540 | goto nest_check; |
a687059c | 6541 | } |
a0d0e21e | 6542 | } |
a687059c | 6543 | |
a0d0e21e LW |
6544 | if (!ISMULT1(op)) { |
6545 | *flagp = flags; | |
a687059c | 6546 | return(ret); |
a0d0e21e | 6547 | } |
bb20fd44 | 6548 | |
c277df42 | 6549 | #if 0 /* Now runtime fix should be reliable. */ |
b45f050a JF |
6550 | |
6551 | /* if this is reinstated, don't forget to put this back into perldiag: | |
6552 | ||
6553 | =item Regexp *+ operand could be empty at {#} in regex m/%s/ | |
6554 | ||
6555 | (F) The part of the regexp subject to either the * or + quantifier | |
6556 | could match an empty string. The {#} shows in the regular | |
6557 | expression about where the problem was discovered. | |
6558 | ||
6559 | */ | |
6560 | ||
bb20fd44 | 6561 | if (!(flags&HASWIDTH) && op != '?') |
b45f050a | 6562 | vFAIL("Regexp *+ operand could be empty"); |
b81d288d | 6563 | #endif |
bb20fd44 | 6564 | |
fac92740 | 6565 | parse_start = RExC_parse; |
830247a4 | 6566 | nextchar(pRExC_state); |
a0d0e21e | 6567 | |
821b33a5 | 6568 | *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH); |
a0d0e21e LW |
6569 | |
6570 | if (op == '*' && (flags&SIMPLE)) { | |
6bda09f9 | 6571 | reginsert(pRExC_state, STAR, ret, depth+1); |
c277df42 | 6572 | ret->flags = 0; |
830247a4 | 6573 | RExC_naughty += 4; |
a0d0e21e LW |
6574 | } |
6575 | else if (op == '*') { | |
6576 | min = 0; | |
6577 | goto do_curly; | |
a0ed51b3 LW |
6578 | } |
6579 | else if (op == '+' && (flags&SIMPLE)) { | |
6bda09f9 | 6580 | reginsert(pRExC_state, PLUS, ret, depth+1); |
c277df42 | 6581 | ret->flags = 0; |
830247a4 | 6582 | RExC_naughty += 3; |
a0d0e21e LW |
6583 | } |
6584 | else if (op == '+') { | |
6585 | min = 1; | |
6586 | goto do_curly; | |
a0ed51b3 LW |
6587 | } |
6588 | else if (op == '?') { | |
a0d0e21e LW |
6589 | min = 0; max = 1; |
6590 | goto do_curly; | |
6591 | } | |
6592 | nest_check: | |
668c081a NC |
6593 | if (!SIZE_ONLY && !(flags&(HASWIDTH|POSTPONED)) && max > REG_INFTY/3) { |
6594 | ckWARN3reg(RExC_parse, | |
6595 | "%.*s matches null string many times", | |
6596 | (int)(RExC_parse >= origparse ? RExC_parse - origparse : 0), | |
6597 | origparse); | |
a0d0e21e LW |
6598 | } |
6599 | ||
b9b4dddf | 6600 | if (RExC_parse < RExC_end && *RExC_parse == '?') { |
830247a4 | 6601 | nextchar(pRExC_state); |
6bda09f9 | 6602 | reginsert(pRExC_state, MINMOD, ret, depth+1); |
3dab1dad | 6603 | REGTAIL(pRExC_state, ret, ret + NODE_STEP_REGNODE); |
a0d0e21e | 6604 | } |
b9b4dddf YO |
6605 | #ifndef REG_ALLOW_MINMOD_SUSPEND |
6606 | else | |
6607 | #endif | |
6608 | if (RExC_parse < RExC_end && *RExC_parse == '+') { | |
6609 | regnode *ender; | |
6610 | nextchar(pRExC_state); | |
6611 | ender = reg_node(pRExC_state, SUCCEED); | |
6612 | REGTAIL(pRExC_state, ret, ender); | |
6613 | reginsert(pRExC_state, SUSPEND, ret, depth+1); | |
6614 | ret->flags = 0; | |
6615 | ender = reg_node(pRExC_state, TAIL); | |
6616 | REGTAIL(pRExC_state, ret, ender); | |
6617 | /*ret= ender;*/ | |
6618 | } | |
6619 | ||
6620 | if (RExC_parse < RExC_end && ISMULT2(RExC_parse)) { | |
830247a4 | 6621 | RExC_parse++; |
b45f050a JF |
6622 | vFAIL("Nested quantifiers"); |
6623 | } | |
a0d0e21e LW |
6624 | |
6625 | return(ret); | |
a687059c LW |
6626 | } |
6627 | ||
fc8cd66c YO |
6628 | |
6629 | /* reg_namedseq(pRExC_state,UVp) | |
6630 | ||
6631 | This is expected to be called by a parser routine that has | |
afefe6bf | 6632 | recognized '\N' and needs to handle the rest. RExC_parse is |
fc8cd66c YO |
6633 | expected to point at the first char following the N at the time |
6634 | of the call. | |
ff3f963a KW |
6635 | |
6636 | The \N may be inside (indicated by valuep not being NULL) or outside a | |
6637 | character class. | |
6638 | ||
6639 | \N may begin either a named sequence, or if outside a character class, mean | |
6640 | to match a non-newline. For non single-quoted regexes, the tokenizer has | |
6641 | attempted to decide which, and in the case of a named sequence converted it | |
6642 | into one of the forms: \N{} (if the sequence is null), or \N{U+c1.c2...}, | |
6643 | where c1... are the characters in the sequence. For single-quoted regexes, | |
6644 | the tokenizer passes the \N sequence through unchanged; this code will not | |
6645 | attempt to determine this nor expand those. The net effect is that if the | |
6646 | beginning of the passed-in pattern isn't '{U+' or there is no '}', it | |
6647 | signals that this \N occurrence means to match a non-newline. | |
6648 | ||
6649 | Only the \N{U+...} form should occur in a character class, for the same | |
6650 | reason that '.' inside a character class means to just match a period: it | |
6651 | just doesn't make sense. | |
fc8cd66c YO |
6652 | |
6653 | If valuep is non-null then it is assumed that we are parsing inside | |
6654 | of a charclass definition and the first codepoint in the resolved | |
6655 | string is returned via *valuep and the routine will return NULL. | |
6656 | In this mode if a multichar string is returned from the charnames | |
ff3f963a | 6657 | handler, a warning will be issued, and only the first char in the |
fc8cd66c YO |
6658 | sequence will be examined. If the string returned is zero length |
6659 | then the value of *valuep is undefined and NON-NULL will | |
6660 | be returned to indicate failure. (This will NOT be a valid pointer | |
6661 | to a regnode.) | |
6662 | ||
ff3f963a KW |
6663 | If valuep is null then it is assumed that we are parsing normal text and a |
6664 | new EXACT node is inserted into the program containing the resolved string, | |
6665 | and a pointer to the new node is returned. But if the string is zero length | |
6666 | a NOTHING node is emitted instead. | |
afefe6bf | 6667 | |
fc8cd66c | 6668 | On success RExC_parse is set to the char following the endbrace. |
ff3f963a | 6669 | Parsing failures will generate a fatal error via vFAIL(...) |
fc8cd66c YO |
6670 | */ |
6671 | STATIC regnode * | |
afefe6bf | 6672 | S_reg_namedseq(pTHX_ RExC_state_t *pRExC_state, UV *valuep, I32 *flagp) |
fc8cd66c | 6673 | { |
c3c41406 | 6674 | char * endbrace; /* '}' following the name */ |
fc8cd66c | 6675 | regnode *ret = NULL; |
ff3f963a KW |
6676 | #ifdef DEBUGGING |
6677 | char* parse_start = RExC_parse - 2; /* points to the '\N' */ | |
6678 | #endif | |
c3c41406 | 6679 | char* p; |
ff3f963a KW |
6680 | |
6681 | GET_RE_DEBUG_FLAGS_DECL; | |
7918f24d NC |
6682 | |
6683 | PERL_ARGS_ASSERT_REG_NAMEDSEQ; | |
ff3f963a KW |
6684 | |
6685 | GET_RE_DEBUG_FLAGS; | |
c3c41406 KW |
6686 | |
6687 | /* The [^\n] meaning of \N ignores spaces and comments under the /x | |
6688 | * modifier. The other meaning does not */ | |
6689 | p = (RExC_flags & RXf_PMf_EXTENDED) | |
6690 | ? regwhite( pRExC_state, RExC_parse ) | |
6691 | : RExC_parse; | |
7918f24d | 6692 | |
ff3f963a | 6693 | /* Disambiguate between \N meaning a named character versus \N meaning |
c3c41406 KW |
6694 | * [^\n]. The former is assumed when it can't be the latter. */ |
6695 | if (*p != '{' || regcurly(p)) { | |
6696 | RExC_parse = p; | |
ff3f963a | 6697 | if (valuep) { |
afefe6bf | 6698 | /* no bare \N in a charclass */ |
ff3f963a KW |
6699 | vFAIL("\\N in a character class must be a named character: \\N{...}"); |
6700 | } | |
afefe6bf RGS |
6701 | nextchar(pRExC_state); |
6702 | ret = reg_node(pRExC_state, REG_ANY); | |
6703 | *flagp |= HASWIDTH|SIMPLE; | |
6704 | RExC_naughty++; | |
6705 | RExC_parse--; | |
6706 | Set_Node_Length(ret, 1); /* MJD */ | |
6707 | return ret; | |
fc8cd66c | 6708 | } |
a4893424 | 6709 | |
c3c41406 KW |
6710 | /* Here, we have decided it should be a named sequence */ |
6711 | ||
6712 | /* The test above made sure that the next real character is a '{', but | |
6713 | * under the /x modifier, it could be separated by space (or a comment and | |
6714 | * \n) and this is not allowed (for consistency with \x{...} and the | |
6715 | * tokenizer handling of \N{NAME}). */ | |
6716 | if (*RExC_parse != '{') { | |
6717 | vFAIL("Missing braces on \\N{}"); | |
6718 | } | |
6719 | ||
ff3f963a | 6720 | RExC_parse++; /* Skip past the '{' */ |
c3c41406 KW |
6721 | |
6722 | if (! (endbrace = strchr(RExC_parse, '}')) /* no trailing brace */ | |
6723 | || ! (endbrace == RExC_parse /* nothing between the {} */ | |
6724 | || (endbrace - RExC_parse >= 2 /* U+ (bad hex is checked below */ | |
6725 | && strnEQ(RExC_parse, "U+", 2)))) /* for a better error msg) */ | |
6726 | { | |
6727 | if (endbrace) RExC_parse = endbrace; /* position msg's '<--HERE' */ | |
6728 | vFAIL("\\N{NAME} must be resolved by the lexer"); | |
6729 | } | |
6730 | ||
ff3f963a KW |
6731 | if (endbrace == RExC_parse) { /* empty: \N{} */ |
6732 | if (! valuep) { | |
6733 | RExC_parse = endbrace + 1; | |
6734 | return reg_node(pRExC_state,NOTHING); | |
a4893424 | 6735 | } |
fc8cd66c | 6736 | |
ff3f963a KW |
6737 | if (SIZE_ONLY) { |
6738 | ckWARNreg(RExC_parse, | |
6739 | "Ignoring zero length \\N{} in character class" | |
6740 | ); | |
6741 | RExC_parse = endbrace + 1; | |
6742 | } | |
6743 | *valuep = 0; | |
6744 | return (regnode *) &RExC_parse; /* Invalid regnode pointer */ | |
fc8cd66c | 6745 | } |
ff3f963a KW |
6746 | |
6747 | RExC_utf8 = 1; /* named sequences imply Unicode semantics */ | |
6748 | RExC_parse += 2; /* Skip past the 'U+' */ | |
6749 | ||
6750 | if (valuep) { /* In a bracketed char class */ | |
6751 | /* We only pay attention to the first char of | |
6752 | multichar strings being returned. I kinda wonder | |
6753 | if this makes sense as it does change the behaviour | |
6754 | from earlier versions, OTOH that behaviour was broken | |
6755 | as well. XXX Solution is to recharacterize as | |
6756 | [rest-of-class]|multi1|multi2... */ | |
6757 | ||
6758 | STRLEN length_of_hex; | |
6759 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES | |
6760 | | PERL_SCAN_DISALLOW_PREFIX | |
6761 | | (SIZE_ONLY ? PERL_SCAN_SILENT_ILLDIGIT : 0); | |
6762 | ||
37820adc KW |
6763 | char * endchar = RExC_parse + strcspn(RExC_parse, ".}"); |
6764 | if (endchar < endbrace) { | |
ff3f963a KW |
6765 | ckWARNreg(endchar, "Using just the first character returned by \\N{} in character class"); |
6766 | } | |
ff3f963a KW |
6767 | |
6768 | length_of_hex = (STRLEN)(endchar - RExC_parse); | |
6769 | *valuep = grok_hex(RExC_parse, &length_of_hex, &flags, NULL); | |
6770 | ||
6771 | /* The tokenizer should have guaranteed validity, but it's possible to | |
6772 | * bypass it by using single quoting, so check */ | |
c3c41406 KW |
6773 | if (length_of_hex == 0 |
6774 | || length_of_hex != (STRLEN)(endchar - RExC_parse) ) | |
6775 | { | |
6776 | RExC_parse += length_of_hex; /* Includes all the valid */ | |
6777 | RExC_parse += (RExC_orig_utf8) /* point to after 1st invalid */ | |
6778 | ? UTF8SKIP(RExC_parse) | |
6779 | : 1; | |
6780 | /* Guard against malformed utf8 */ | |
6781 | if (RExC_parse >= endchar) RExC_parse = endchar; | |
6782 | vFAIL("Invalid hexadecimal number in \\N{U+...}"); | |
ff3f963a KW |
6783 | } |
6784 | ||
6785 | RExC_parse = endbrace + 1; | |
6786 | if (endchar == endbrace) return NULL; | |
6787 | ||
6788 | ret = (regnode *) &RExC_parse; /* Invalid regnode pointer */ | |
fc8cd66c | 6789 | } |
ff3f963a KW |
6790 | else { /* Not a char class */ |
6791 | char *s; /* String to put in generated EXACT node */ | |
6792 | STRLEN len = 0; /* Its current length */ | |
6793 | char *endchar; /* Points to '.' or '}' ending cur char in the input | |
6794 | stream */ | |
6795 | ||
6796 | ret = reg_node(pRExC_state, | |
6797 | (U8)(FOLD ? (LOC ? EXACTFL : EXACTF) : EXACT)); | |
6798 | s= STRING(ret); | |
6799 | ||
6800 | /* Exact nodes can hold only a U8 length's of text = 255. Loop through | |
6801 | * the input which is of the form now 'c1.c2.c3...}' until find the | |
6802 | * ending brace or exeed length 255. The characters that exceed this | |
6803 | * limit are dropped. The limit could be relaxed should it become | |
6804 | * desirable by reparsing this as (?:\N{NAME}), so could generate | |
6805 | * multiple EXACT nodes, as is done for just regular input. But this | |
6806 | * is primarily a named character, and not intended to be a huge long | |
6807 | * string, so 255 bytes should be good enough */ | |
6808 | while (1) { | |
c3c41406 | 6809 | STRLEN length_of_hex; |
ff3f963a KW |
6810 | I32 grok_flags = PERL_SCAN_ALLOW_UNDERSCORES |
6811 | | PERL_SCAN_DISALLOW_PREFIX | |
6812 | | (SIZE_ONLY ? PERL_SCAN_SILENT_ILLDIGIT : 0); | |
6813 | UV cp; /* Ord of current character */ | |
6814 | ||
6815 | /* Code points are separated by dots. If none, there is only one | |
6816 | * code point, and is terminated by the brace */ | |
37820adc | 6817 | endchar = RExC_parse + strcspn(RExC_parse, ".}"); |
ff3f963a KW |
6818 | |
6819 | /* The values are Unicode even on EBCDIC machines */ | |
c3c41406 KW |
6820 | length_of_hex = (STRLEN)(endchar - RExC_parse); |
6821 | cp = grok_hex(RExC_parse, &length_of_hex, &grok_flags, NULL); | |
6822 | if ( length_of_hex == 0 | |
6823 | || length_of_hex != (STRLEN)(endchar - RExC_parse) ) | |
ff3f963a | 6824 | { |
c3c41406 KW |
6825 | RExC_parse += length_of_hex; /* Includes all the valid */ |
6826 | RExC_parse += (RExC_orig_utf8) /* point to after 1st invalid */ | |
6827 | ? UTF8SKIP(RExC_parse) | |
6828 | : 1; | |
6829 | /* Guard against malformed utf8 */ | |
6830 | if (RExC_parse >= endchar) RExC_parse = endchar; | |
6831 | vFAIL("Invalid hexadecimal number in \\N{U+...}"); | |
ff3f963a KW |
6832 | } |
6833 | ||
6834 | if (! FOLD) { /* Not folding, just append to the string */ | |
6835 | STRLEN unilen; | |
6836 | ||
6837 | /* Quit before adding this character if would exceed limit */ | |
6838 | if (len + UNISKIP(cp) > U8_MAX) break; | |
fc8cd66c | 6839 | |
ff3f963a KW |
6840 | unilen = reguni(pRExC_state, cp, s); |
6841 | if (unilen > 0) { | |
6842 | s += unilen; | |
6843 | len += unilen; | |
6844 | } | |
6845 | } else { /* Folding, output the folded equivalent */ | |
6846 | STRLEN foldlen,numlen; | |
6847 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf; | |
6848 | cp = toFOLD_uni(cp, tmpbuf, &foldlen); | |
6849 | ||
6850 | /* Quit before exceeding size limit */ | |
6851 | if (len + foldlen > U8_MAX) break; | |
6852 | ||
6853 | for (foldbuf = tmpbuf; | |
6854 | foldlen; | |
6855 | foldlen -= numlen) | |
6856 | { | |
6857 | cp = utf8_to_uvchr(foldbuf, &numlen); | |
6858 | if (numlen > 0) { | |
6859 | const STRLEN unilen = reguni(pRExC_state, cp, s); | |
6860 | s += unilen; | |
6861 | len += unilen; | |
6862 | /* In EBCDIC the numlen and unilen can differ. */ | |
6863 | foldbuf += numlen; | |
6864 | if (numlen >= foldlen) | |
6865 | break; | |
6866 | } | |
6867 | else | |
6868 | break; /* "Can't happen." */ | |
6869 | } | |
6870 | } | |
6871 | ||
6872 | /* Point to the beginning of the next character in the sequence. */ | |
6873 | RExC_parse = endchar + 1; | |
6874 | ||
6875 | /* Quit if no more characters */ | |
6876 | if (RExC_parse >= endbrace) break; | |
6877 | } | |
6878 | ||
6879 | ||
6880 | if (SIZE_ONLY) { | |
6881 | if (RExC_parse < endbrace) { | |
6882 | ckWARNreg(RExC_parse - 1, | |
6883 | "Using just the first characters returned by \\N{}"); | |
6884 | } | |
6885 | ||
6886 | RExC_size += STR_SZ(len); | |
6887 | } else { | |
6888 | STR_LEN(ret) = len; | |
6889 | RExC_emit += STR_SZ(len); | |
6890 | } | |
6891 | ||
6892 | RExC_parse = endbrace + 1; | |
6893 | ||
6894 | *flagp |= HASWIDTH; /* Not SIMPLE, as that causes the engine to fail | |
6895 | with malformed in t/re/pat_advanced.t */ | |
6896 | RExC_parse --; | |
6897 | Set_Node_Cur_Length(ret); /* MJD */ | |
6898 | nextchar(pRExC_state); | |
6899 | } | |
6900 | ||
6901 | return ret; | |
fc8cd66c YO |
6902 | } |
6903 | ||
6904 | ||
9e08bc66 TS |
6905 | /* |
6906 | * reg_recode | |
6907 | * | |
6908 | * It returns the code point in utf8 for the value in *encp. | |
6909 | * value: a code value in the source encoding | |
6910 | * encp: a pointer to an Encode object | |
6911 | * | |
6912 | * If the result from Encode is not a single character, | |
6913 | * it returns U+FFFD (Replacement character) and sets *encp to NULL. | |
6914 | */ | |
6915 | STATIC UV | |
6916 | S_reg_recode(pTHX_ const char value, SV **encp) | |
6917 | { | |
6918 | STRLEN numlen = 1; | |
59cd0e26 | 6919 | SV * const sv = newSVpvn_flags(&value, numlen, SVs_TEMP); |
c86f7df5 | 6920 | const char * const s = *encp ? sv_recode_to_utf8(sv, *encp) : SvPVX(sv); |
9e08bc66 TS |
6921 | const STRLEN newlen = SvCUR(sv); |
6922 | UV uv = UNICODE_REPLACEMENT; | |
6923 | ||
7918f24d NC |
6924 | PERL_ARGS_ASSERT_REG_RECODE; |
6925 | ||
9e08bc66 TS |
6926 | if (newlen) |
6927 | uv = SvUTF8(sv) | |
6928 | ? utf8n_to_uvchr((U8*)s, newlen, &numlen, UTF8_ALLOW_DEFAULT) | |
6929 | : *(U8*)s; | |
6930 | ||
6931 | if (!newlen || numlen != newlen) { | |
6932 | uv = UNICODE_REPLACEMENT; | |
c86f7df5 | 6933 | *encp = NULL; |
9e08bc66 TS |
6934 | } |
6935 | return uv; | |
6936 | } | |
6937 | ||
fc8cd66c | 6938 | |
a687059c LW |
6939 | /* |
6940 | - regatom - the lowest level | |
ee9b8eae YO |
6941 | |
6942 | Try to identify anything special at the start of the pattern. If there | |
6943 | is, then handle it as required. This may involve generating a single regop, | |
6944 | such as for an assertion; or it may involve recursing, such as to | |
6945 | handle a () structure. | |
6946 | ||
6947 | If the string doesn't start with something special then we gobble up | |
6948 | as much literal text as we can. | |
6949 | ||
6950 | Once we have been able to handle whatever type of thing started the | |
6951 | sequence, we return. | |
6952 | ||
6953 | Note: we have to be careful with escapes, as they can be both literal | |
6954 | and special, and in the case of \10 and friends can either, depending | |
6955 | on context. Specifically there are two seperate switches for handling | |
6956 | escape sequences, with the one for handling literal escapes requiring | |
6957 | a dummy entry for all of the special escapes that are actually handled | |
6958 | by the other. | |
6959 | */ | |
6960 | ||
76e3520e | 6961 | STATIC regnode * |
3dab1dad | 6962 | S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth) |
a687059c | 6963 | { |
97aff369 | 6964 | dVAR; |
cbbf8932 | 6965 | register regnode *ret = NULL; |
a0d0e21e | 6966 | I32 flags; |
45948336 | 6967 | char *parse_start = RExC_parse; |
3dab1dad YO |
6968 | GET_RE_DEBUG_FLAGS_DECL; |
6969 | DEBUG_PARSE("atom"); | |
a0d0e21e LW |
6970 | *flagp = WORST; /* Tentatively. */ |
6971 | ||
7918f24d | 6972 | PERL_ARGS_ASSERT_REGATOM; |
ee9b8eae | 6973 | |
a0d0e21e | 6974 | tryagain: |
f9a79580 | 6975 | switch ((U8)*RExC_parse) { |
a0d0e21e | 6976 | case '^': |
830247a4 IZ |
6977 | RExC_seen_zerolen++; |
6978 | nextchar(pRExC_state); | |
bbe252da | 6979 | if (RExC_flags & RXf_PMf_MULTILINE) |
830247a4 | 6980 | ret = reg_node(pRExC_state, MBOL); |
bbe252da | 6981 | else if (RExC_flags & RXf_PMf_SINGLELINE) |
830247a4 | 6982 | ret = reg_node(pRExC_state, SBOL); |
a0d0e21e | 6983 | else |
830247a4 | 6984 | ret = reg_node(pRExC_state, BOL); |
fac92740 | 6985 | Set_Node_Length(ret, 1); /* MJD */ |
a0d0e21e LW |
6986 | break; |
6987 | case '$': | |
830247a4 | 6988 | nextchar(pRExC_state); |
b81d288d | 6989 | if (*RExC_parse) |
830247a4 | 6990 | RExC_seen_zerolen++; |
bbe252da | 6991 | if (RExC_flags & RXf_PMf_MULTILINE) |
830247a4 | 6992 | ret = reg_node(pRExC_state, MEOL); |
bbe252da | 6993 | else if (RExC_flags & RXf_PMf_SINGLELINE) |
830247a4 | 6994 | ret = reg_node(pRExC_state, SEOL); |
a0d0e21e | 6995 | else |
830247a4 | 6996 | ret = reg_node(pRExC_state, EOL); |
fac92740 | 6997 | Set_Node_Length(ret, 1); /* MJD */ |
a0d0e21e LW |
6998 | break; |
6999 | case '.': | |
830247a4 | 7000 | nextchar(pRExC_state); |
bbe252da | 7001 | if (RExC_flags & RXf_PMf_SINGLELINE) |
ffc61ed2 JH |
7002 | ret = reg_node(pRExC_state, SANY); |
7003 | else | |
7004 | ret = reg_node(pRExC_state, REG_ANY); | |
7005 | *flagp |= HASWIDTH|SIMPLE; | |
830247a4 | 7006 | RExC_naughty++; |
fac92740 | 7007 | Set_Node_Length(ret, 1); /* MJD */ |
a0d0e21e LW |
7008 | break; |
7009 | case '[': | |
b45f050a | 7010 | { |
3dab1dad YO |
7011 | char * const oregcomp_parse = ++RExC_parse; |
7012 | ret = regclass(pRExC_state,depth+1); | |
830247a4 IZ |
7013 | if (*RExC_parse != ']') { |
7014 | RExC_parse = oregcomp_parse; | |
b45f050a JF |
7015 | vFAIL("Unmatched ["); |
7016 | } | |
830247a4 | 7017 | nextchar(pRExC_state); |
a0d0e21e | 7018 | *flagp |= HASWIDTH|SIMPLE; |
fac92740 | 7019 | Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */ |
a0d0e21e | 7020 | break; |
b45f050a | 7021 | } |
a0d0e21e | 7022 | case '(': |
830247a4 | 7023 | nextchar(pRExC_state); |
3dab1dad | 7024 | ret = reg(pRExC_state, 1, &flags,depth+1); |
a0d0e21e | 7025 | if (ret == NULL) { |
bf93d4cc | 7026 | if (flags & TRYAGAIN) { |
830247a4 | 7027 | if (RExC_parse == RExC_end) { |
bf93d4cc GS |
7028 | /* Make parent create an empty node if needed. */ |
7029 | *flagp |= TRYAGAIN; | |
7030 | return(NULL); | |
7031 | } | |
a0d0e21e | 7032 | goto tryagain; |
bf93d4cc | 7033 | } |
a0d0e21e LW |
7034 | return(NULL); |
7035 | } | |
a3b492c3 | 7036 | *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE|POSTPONED); |
a0d0e21e LW |
7037 | break; |
7038 | case '|': | |
7039 | case ')': | |
7040 | if (flags & TRYAGAIN) { | |
7041 | *flagp |= TRYAGAIN; | |
7042 | return NULL; | |
7043 | } | |
b45f050a | 7044 | vFAIL("Internal urp"); |
a0d0e21e LW |
7045 | /* Supposed to be caught earlier. */ |
7046 | break; | |
85afd4ae | 7047 | case '{': |
830247a4 IZ |
7048 | if (!regcurly(RExC_parse)) { |
7049 | RExC_parse++; | |
85afd4ae CS |
7050 | goto defchar; |
7051 | } | |
7052 | /* FALL THROUGH */ | |
a0d0e21e LW |
7053 | case '?': |
7054 | case '+': | |
7055 | case '*': | |
830247a4 | 7056 | RExC_parse++; |
b45f050a | 7057 | vFAIL("Quantifier follows nothing"); |
a0d0e21e | 7058 | break; |
f9a79580 RGS |
7059 | case 0xDF: |
7060 | case 0xC3: | |
7061 | case 0xCE: | |
a0a388a1 | 7062 | do_foldchar: |
56d400ed | 7063 | if (!LOC && FOLD) { |
e64b1bd1 | 7064 | U32 len,cp; |
7cf3a6a3 | 7065 | len=0; /* silence a spurious compiler warning */ |
56d400ed | 7066 | if ((cp = what_len_TRICKYFOLD_safe(RExC_parse,RExC_end,UTF,len))) { |
e64b1bd1 YO |
7067 | *flagp |= HASWIDTH; /* could be SIMPLE too, but needs a handler in regexec.regrepeat */ |
7068 | RExC_parse+=len-1; /* we get one from nextchar() as well. :-( */ | |
7069 | ret = reganode(pRExC_state, FOLDCHAR, cp); | |
7070 | Set_Node_Length(ret, 1); /* MJD */ | |
7071 | nextchar(pRExC_state); /* kill whitespace under /x */ | |
7072 | return ret; | |
7073 | } | |
7074 | } | |
7075 | goto outer_default; | |
a0d0e21e | 7076 | case '\\': |
ee9b8eae YO |
7077 | /* Special Escapes |
7078 | ||
7079 | This switch handles escape sequences that resolve to some kind | |
7080 | of special regop and not to literal text. Escape sequnces that | |
7081 | resolve to literal text are handled below in the switch marked | |
7082 | "Literal Escapes". | |
7083 | ||
7084 | Every entry in this switch *must* have a corresponding entry | |
7085 | in the literal escape switch. However, the opposite is not | |
7086 | required, as the default for this switch is to jump to the | |
7087 | literal text handling code. | |
7088 | */ | |
a0a388a1 YO |
7089 | switch ((U8)*++RExC_parse) { |
7090 | case 0xDF: | |
7091 | case 0xC3: | |
7092 | case 0xCE: | |
7093 | goto do_foldchar; | |
ee9b8eae | 7094 | /* Special Escapes */ |
a0d0e21e | 7095 | case 'A': |
830247a4 IZ |
7096 | RExC_seen_zerolen++; |
7097 | ret = reg_node(pRExC_state, SBOL); | |
a0d0e21e | 7098 | *flagp |= SIMPLE; |
ee9b8eae | 7099 | goto finish_meta_pat; |
a0d0e21e | 7100 | case 'G': |
830247a4 IZ |
7101 | ret = reg_node(pRExC_state, GPOS); |
7102 | RExC_seen |= REG_SEEN_GPOS; | |
a0d0e21e | 7103 | *flagp |= SIMPLE; |
ee9b8eae YO |
7104 | goto finish_meta_pat; |
7105 | case 'K': | |
7106 | RExC_seen_zerolen++; | |
7107 | ret = reg_node(pRExC_state, KEEPS); | |
7108 | *flagp |= SIMPLE; | |
37923168 RGS |
7109 | /* XXX:dmq : disabling in-place substitution seems to |
7110 | * be necessary here to avoid cases of memory corruption, as | |
7111 | * with: C<$_="x" x 80; s/x\K/y/> -- rgs | |
7112 | */ | |
7113 | RExC_seen |= REG_SEEN_LOOKBEHIND; | |
ee9b8eae | 7114 | goto finish_meta_pat; |
a0d0e21e | 7115 | case 'Z': |
830247a4 | 7116 | ret = reg_node(pRExC_state, SEOL); |
a0d0e21e | 7117 | *flagp |= SIMPLE; |
a1917ab9 | 7118 | RExC_seen_zerolen++; /* Do not optimize RE away */ |
ee9b8eae | 7119 | goto finish_meta_pat; |
b85d18e9 | 7120 | case 'z': |
830247a4 | 7121 | ret = reg_node(pRExC_state, EOS); |
b85d18e9 | 7122 | *flagp |= SIMPLE; |
830247a4 | 7123 | RExC_seen_zerolen++; /* Do not optimize RE away */ |
ee9b8eae | 7124 | goto finish_meta_pat; |
4a2d328f | 7125 | case 'C': |
f33976b4 DB |
7126 | ret = reg_node(pRExC_state, CANY); |
7127 | RExC_seen |= REG_SEEN_CANY; | |
a0ed51b3 | 7128 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7129 | goto finish_meta_pat; |
a0ed51b3 | 7130 | case 'X': |
830247a4 | 7131 | ret = reg_node(pRExC_state, CLUMP); |
a0ed51b3 | 7132 | *flagp |= HASWIDTH; |
ee9b8eae | 7133 | goto finish_meta_pat; |
a0d0e21e | 7134 | case 'w': |
eb160463 | 7135 | ret = reg_node(pRExC_state, (U8)(LOC ? ALNUML : ALNUM)); |
a0d0e21e | 7136 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7137 | goto finish_meta_pat; |
a0d0e21e | 7138 | case 'W': |
eb160463 | 7139 | ret = reg_node(pRExC_state, (U8)(LOC ? NALNUML : NALNUM)); |
a0d0e21e | 7140 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7141 | goto finish_meta_pat; |
a0d0e21e | 7142 | case 'b': |
830247a4 IZ |
7143 | RExC_seen_zerolen++; |
7144 | RExC_seen |= REG_SEEN_LOOKBEHIND; | |
eb160463 | 7145 | ret = reg_node(pRExC_state, (U8)(LOC ? BOUNDL : BOUND)); |
a0d0e21e | 7146 | *flagp |= SIMPLE; |
ee9b8eae | 7147 | goto finish_meta_pat; |
a0d0e21e | 7148 | case 'B': |
830247a4 IZ |
7149 | RExC_seen_zerolen++; |
7150 | RExC_seen |= REG_SEEN_LOOKBEHIND; | |
eb160463 | 7151 | ret = reg_node(pRExC_state, (U8)(LOC ? NBOUNDL : NBOUND)); |
a0d0e21e | 7152 | *flagp |= SIMPLE; |
ee9b8eae | 7153 | goto finish_meta_pat; |
a0d0e21e | 7154 | case 's': |
eb160463 | 7155 | ret = reg_node(pRExC_state, (U8)(LOC ? SPACEL : SPACE)); |
a0d0e21e | 7156 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7157 | goto finish_meta_pat; |
a0d0e21e | 7158 | case 'S': |
eb160463 | 7159 | ret = reg_node(pRExC_state, (U8)(LOC ? NSPACEL : NSPACE)); |
a0d0e21e | 7160 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7161 | goto finish_meta_pat; |
a0d0e21e | 7162 | case 'd': |
ffc61ed2 | 7163 | ret = reg_node(pRExC_state, DIGIT); |
a0d0e21e | 7164 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7165 | goto finish_meta_pat; |
a0d0e21e | 7166 | case 'D': |
ffc61ed2 | 7167 | ret = reg_node(pRExC_state, NDIGIT); |
a0d0e21e | 7168 | *flagp |= HASWIDTH|SIMPLE; |
ee9b8eae | 7169 | goto finish_meta_pat; |
e1d1eefb YO |
7170 | case 'R': |
7171 | ret = reg_node(pRExC_state, LNBREAK); | |
7172 | *flagp |= HASWIDTH|SIMPLE; | |
7173 | goto finish_meta_pat; | |
7174 | case 'h': | |
7175 | ret = reg_node(pRExC_state, HORIZWS); | |
7176 | *flagp |= HASWIDTH|SIMPLE; | |
7177 | goto finish_meta_pat; | |
7178 | case 'H': | |
7179 | ret = reg_node(pRExC_state, NHORIZWS); | |
7180 | *flagp |= HASWIDTH|SIMPLE; | |
7181 | goto finish_meta_pat; | |
ee9b8eae | 7182 | case 'v': |
e1d1eefb YO |
7183 | ret = reg_node(pRExC_state, VERTWS); |
7184 | *flagp |= HASWIDTH|SIMPLE; | |
ee9b8eae YO |
7185 | goto finish_meta_pat; |
7186 | case 'V': | |
e1d1eefb YO |
7187 | ret = reg_node(pRExC_state, NVERTWS); |
7188 | *flagp |= HASWIDTH|SIMPLE; | |
ee9b8eae | 7189 | finish_meta_pat: |
830247a4 | 7190 | nextchar(pRExC_state); |
fac92740 | 7191 | Set_Node_Length(ret, 2); /* MJD */ |
ee9b8eae | 7192 | break; |
a14b48bc LW |
7193 | case 'p': |
7194 | case 'P': | |
3568d838 | 7195 | { |
3dab1dad | 7196 | char* const oldregxend = RExC_end; |
d008bc60 | 7197 | #ifdef DEBUGGING |
ccb2c380 | 7198 | char* parse_start = RExC_parse - 2; |
d008bc60 | 7199 | #endif |
a14b48bc | 7200 | |
830247a4 | 7201 | if (RExC_parse[1] == '{') { |
3568d838 | 7202 | /* a lovely hack--pretend we saw [\pX] instead */ |
830247a4 IZ |
7203 | RExC_end = strchr(RExC_parse, '}'); |
7204 | if (!RExC_end) { | |
3dab1dad | 7205 | const U8 c = (U8)*RExC_parse; |
830247a4 IZ |
7206 | RExC_parse += 2; |
7207 | RExC_end = oldregxend; | |
0da60cf5 | 7208 | vFAIL2("Missing right brace on \\%c{}", c); |
b45f050a | 7209 | } |
830247a4 | 7210 | RExC_end++; |
a14b48bc | 7211 | } |
af6f566e | 7212 | else { |
830247a4 | 7213 | RExC_end = RExC_parse + 2; |
af6f566e HS |
7214 | if (RExC_end > oldregxend) |
7215 | RExC_end = oldregxend; | |
7216 | } | |
830247a4 | 7217 | RExC_parse--; |
a14b48bc | 7218 | |
3dab1dad | 7219 | ret = regclass(pRExC_state,depth+1); |
a14b48bc | 7220 | |
830247a4 IZ |
7221 | RExC_end = oldregxend; |
7222 | RExC_parse--; | |
ccb2c380 MP |
7223 | |
7224 | Set_Node_Offset(ret, parse_start + 2); | |
7225 | Set_Node_Cur_Length(ret); | |
830247a4 | 7226 | nextchar(pRExC_state); |
a14b48bc LW |
7227 | *flagp |= HASWIDTH|SIMPLE; |
7228 | } | |
7229 | break; | |
fc8cd66c | 7230 | case 'N': |
afefe6bf | 7231 | /* Handle \N and \N{NAME} here and not below because it can be |
fc8cd66c YO |
7232 | multicharacter. join_exact() will join them up later on. |
7233 | Also this makes sure that things like /\N{BLAH}+/ and | |
7234 | \N{BLAH} being multi char Just Happen. dmq*/ | |
7235 | ++RExC_parse; | |
afefe6bf | 7236 | ret= reg_namedseq(pRExC_state, NULL, flagp); |
fc8cd66c | 7237 | break; |
0a4db386 | 7238 | case 'k': /* Handle \k<NAME> and \k'NAME' */ |
1f1031fe | 7239 | parse_named_seq: |
81714fb9 YO |
7240 | { |
7241 | char ch= RExC_parse[1]; | |
1f1031fe YO |
7242 | if (ch != '<' && ch != '\'' && ch != '{') { |
7243 | RExC_parse++; | |
7244 | vFAIL2("Sequence %.2s... not terminated",parse_start); | |
81714fb9 | 7245 | } else { |
1f1031fe YO |
7246 | /* this pretty much dupes the code for (?P=...) in reg(), if |
7247 | you change this make sure you change that */ | |
81714fb9 | 7248 | char* name_start = (RExC_parse += 2); |
2eccd3b2 | 7249 | U32 num = 0; |
0a4db386 YO |
7250 | SV *sv_dat = reg_scan_name(pRExC_state, |
7251 | SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA); | |
1f1031fe | 7252 | ch= (ch == '<') ? '>' : (ch == '{') ? '}' : '\''; |
81714fb9 | 7253 | if (RExC_parse == name_start || *RExC_parse != ch) |
1f1031fe YO |
7254 | vFAIL2("Sequence %.3s... not terminated",parse_start); |
7255 | ||
7256 | if (!SIZE_ONLY) { | |
7257 | num = add_data( pRExC_state, 1, "S" ); | |
7258 | RExC_rxi->data->data[num]=(void*)sv_dat; | |
5a5094bd | 7259 | SvREFCNT_inc_simple_void(sv_dat); |
1f1031fe YO |
7260 | } |
7261 | ||
81714fb9 YO |
7262 | RExC_sawback = 1; |
7263 | ret = reganode(pRExC_state, | |
7264 | (U8)(FOLD ? (LOC ? NREFFL : NREFF) : NREF), | |
7265 | num); | |
7266 | *flagp |= HASWIDTH; | |
1f1031fe | 7267 | |
81714fb9 YO |
7268 | /* override incorrect value set in reganode MJD */ |
7269 | Set_Node_Offset(ret, parse_start+1); | |
7270 | Set_Node_Cur_Length(ret); /* MJD */ | |
7271 | nextchar(pRExC_state); | |
1f1031fe | 7272 | |
81714fb9 YO |
7273 | } |
7274 | break; | |
1f1031fe | 7275 | } |
2bf803e2 | 7276 | case 'g': |
a0d0e21e LW |
7277 | case '1': case '2': case '3': case '4': |
7278 | case '5': case '6': case '7': case '8': case '9': | |
7279 | { | |
c74340f9 | 7280 | I32 num; |
2bf803e2 YO |
7281 | bool isg = *RExC_parse == 'g'; |
7282 | bool isrel = 0; | |
7283 | bool hasbrace = 0; | |
7284 | if (isg) { | |
c74340f9 | 7285 | RExC_parse++; |
2bf803e2 YO |
7286 | if (*RExC_parse == '{') { |
7287 | RExC_parse++; | |
7288 | hasbrace = 1; | |
7289 | } | |
7290 | if (*RExC_parse == '-') { | |
7291 | RExC_parse++; | |
7292 | isrel = 1; | |
7293 | } | |
1f1031fe YO |
7294 | if (hasbrace && !isDIGIT(*RExC_parse)) { |
7295 | if (isrel) RExC_parse--; | |
7296 | RExC_parse -= 2; | |
7297 | goto parse_named_seq; | |
7298 | } } | |
c74340f9 | 7299 | num = atoi(RExC_parse); |
b72d83b2 RGS |
7300 | if (isg && num == 0) |
7301 | vFAIL("Reference to invalid group 0"); | |
c74340f9 | 7302 | if (isrel) { |
5624f11d | 7303 | num = RExC_npar - num; |
c74340f9 YO |
7304 | if (num < 1) |
7305 | vFAIL("Reference to nonexistent or unclosed group"); | |
7306 | } | |
2bf803e2 | 7307 | if (!isg && num > 9 && num >= RExC_npar) |
a0d0e21e LW |
7308 | goto defchar; |
7309 | else { | |
3dab1dad | 7310 | char * const parse_start = RExC_parse - 1; /* MJD */ |
830247a4 IZ |
7311 | while (isDIGIT(*RExC_parse)) |
7312 | RExC_parse++; | |
1f1031fe YO |
7313 | if (parse_start == RExC_parse - 1) |
7314 | vFAIL("Unterminated \\g... pattern"); | |
2bf803e2 YO |
7315 | if (hasbrace) { |
7316 | if (*RExC_parse != '}') | |
7317 | vFAIL("Unterminated \\g{...} pattern"); | |
7318 | RExC_parse++; | |
7319 | } | |
c74340f9 YO |
7320 | if (!SIZE_ONLY) { |
7321 | if (num > (I32)RExC_rx->nparens) | |
7322 | vFAIL("Reference to nonexistent group"); | |
c74340f9 | 7323 | } |
830247a4 | 7324 | RExC_sawback = 1; |
eb160463 GS |
7325 | ret = reganode(pRExC_state, |
7326 | (U8)(FOLD ? (LOC ? REFFL : REFF) : REF), | |
7327 | num); | |
a0d0e21e | 7328 | *flagp |= HASWIDTH; |
2af232bd | 7329 | |
fac92740 | 7330 | /* override incorrect value set in reganode MJD */ |
2af232bd | 7331 | Set_Node_Offset(ret, parse_start+1); |
fac92740 | 7332 | Set_Node_Cur_Length(ret); /* MJD */ |
830247a4 IZ |
7333 | RExC_parse--; |
7334 | nextchar(pRExC_state); | |
a0d0e21e LW |
7335 | } |
7336 | } | |
7337 | break; | |
7338 | case '\0': | |
830247a4 | 7339 | if (RExC_parse >= RExC_end) |
b45f050a | 7340 | FAIL("Trailing \\"); |
a0d0e21e LW |
7341 | /* FALL THROUGH */ |
7342 | default: | |
a0288114 | 7343 | /* Do not generate "unrecognized" warnings here, we fall |
c9f97d15 | 7344 | back into the quick-grab loop below */ |
45948336 | 7345 | parse_start--; |
a0d0e21e LW |
7346 | goto defchar; |
7347 | } | |
7348 | break; | |
4633a7c4 LW |
7349 | |
7350 | case '#': | |
bbe252da | 7351 | if (RExC_flags & RXf_PMf_EXTENDED) { |
bcdf7404 | 7352 | if ( reg_skipcomment( pRExC_state ) ) |
4633a7c4 LW |
7353 | goto tryagain; |
7354 | } | |
7355 | /* FALL THROUGH */ | |
7356 | ||
f9a79580 RGS |
7357 | default: |
7358 | outer_default:{ | |
ba210ebe | 7359 | register STRLEN len; |
58ae7d3f | 7360 | register UV ender; |
a0d0e21e | 7361 | register char *p; |
3dab1dad | 7362 | char *s; |
80aecb99 | 7363 | STRLEN foldlen; |
89ebb4a3 | 7364 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf; |
f06dbbb7 JH |
7365 | |
7366 | parse_start = RExC_parse - 1; | |
a0d0e21e | 7367 | |
830247a4 | 7368 | RExC_parse++; |
a0d0e21e LW |
7369 | |
7370 | defchar: | |
58ae7d3f | 7371 | ender = 0; |
eb160463 GS |
7372 | ret = reg_node(pRExC_state, |
7373 | (U8)(FOLD ? (LOC ? EXACTFL : EXACTF) : EXACT)); | |
cd439c50 | 7374 | s = STRING(ret); |
830247a4 IZ |
7375 | for (len = 0, p = RExC_parse - 1; |
7376 | len < 127 && p < RExC_end; | |
a0d0e21e LW |
7377 | len++) |
7378 | { | |
3dab1dad | 7379 | char * const oldp = p; |
5b5a24f7 | 7380 | |
bbe252da | 7381 | if (RExC_flags & RXf_PMf_EXTENDED) |
bcdf7404 | 7382 | p = regwhite( pRExC_state, p ); |
f9a79580 RGS |
7383 | switch ((U8)*p) { |
7384 | case 0xDF: | |
7385 | case 0xC3: | |
7386 | case 0xCE: | |
56d400ed | 7387 | if (LOC || !FOLD || !is_TRICKYFOLD_safe(p,RExC_end,UTF)) |
f9a79580 | 7388 | goto normal_default; |
a0d0e21e LW |
7389 | case '^': |
7390 | case '$': | |
7391 | case '.': | |
7392 | case '[': | |
7393 | case '(': | |
7394 | case ')': | |
7395 | case '|': | |
7396 | goto loopdone; | |
7397 | case '\\': | |
ee9b8eae YO |
7398 | /* Literal Escapes Switch |
7399 | ||
7400 | This switch is meant to handle escape sequences that | |
7401 | resolve to a literal character. | |
7402 | ||
7403 | Every escape sequence that represents something | |
7404 | else, like an assertion or a char class, is handled | |
7405 | in the switch marked 'Special Escapes' above in this | |
7406 | routine, but also has an entry here as anything that | |
7407 | isn't explicitly mentioned here will be treated as | |
7408 | an unescaped equivalent literal. | |
7409 | */ | |
7410 | ||
a0a388a1 | 7411 | switch ((U8)*++p) { |
ee9b8eae | 7412 | /* These are all the special escapes. */ |
a0a388a1 YO |
7413 | case 0xDF: |
7414 | case 0xC3: | |
7415 | case 0xCE: | |
7416 | if (LOC || !FOLD || !is_TRICKYFOLD_safe(p,RExC_end,UTF)) | |
7417 | goto normal_default; | |
ee9b8eae YO |
7418 | case 'A': /* Start assertion */ |
7419 | case 'b': case 'B': /* Word-boundary assertion*/ | |
7420 | case 'C': /* Single char !DANGEROUS! */ | |
7421 | case 'd': case 'D': /* digit class */ | |
7422 | case 'g': case 'G': /* generic-backref, pos assertion */ | |
e1d1eefb | 7423 | case 'h': case 'H': /* HORIZWS */ |
ee9b8eae YO |
7424 | case 'k': case 'K': /* named backref, keep marker */ |
7425 | case 'N': /* named char sequence */ | |
38a44b82 | 7426 | case 'p': case 'P': /* Unicode property */ |
e1d1eefb | 7427 | case 'R': /* LNBREAK */ |
ee9b8eae | 7428 | case 's': case 'S': /* space class */ |
e1d1eefb | 7429 | case 'v': case 'V': /* VERTWS */ |
ee9b8eae YO |
7430 | case 'w': case 'W': /* word class */ |
7431 | case 'X': /* eXtended Unicode "combining character sequence" */ | |
7432 | case 'z': case 'Z': /* End of line/string assertion */ | |
a0d0e21e LW |
7433 | --p; |
7434 | goto loopdone; | |
ee9b8eae YO |
7435 | |
7436 | /* Anything after here is an escape that resolves to a | |
7437 | literal. (Except digits, which may or may not) | |
7438 | */ | |
a0d0e21e LW |
7439 | case 'n': |
7440 | ender = '\n'; | |
7441 | p++; | |
a687059c | 7442 | break; |
a0d0e21e LW |
7443 | case 'r': |
7444 | ender = '\r'; | |
7445 | p++; | |
a687059c | 7446 | break; |
a0d0e21e LW |
7447 | case 't': |
7448 | ender = '\t'; | |
7449 | p++; | |
a687059c | 7450 | break; |
a0d0e21e LW |
7451 | case 'f': |
7452 | ender = '\f'; | |
7453 | p++; | |
a687059c | 7454 | break; |
a0d0e21e | 7455 | case 'e': |
c7f1f016 | 7456 | ender = ASCII_TO_NATIVE('\033'); |
a0d0e21e | 7457 | p++; |
a687059c | 7458 | break; |
a0d0e21e | 7459 | case 'a': |
c7f1f016 | 7460 | ender = ASCII_TO_NATIVE('\007'); |
a0d0e21e | 7461 | p++; |
a687059c | 7462 | break; |
f0a2b745 KW |
7463 | case 'o': |
7464 | { | |
7465 | STRLEN brace_len = len; | |
00c0cb6d | 7466 | UV result; |
454155d9 KW |
7467 | const char* error_msg; |
7468 | ||
7469 | bool valid = grok_bslash_o(p, | |
7470 | &result, | |
7471 | &brace_len, | |
7472 | &error_msg, | |
7473 | 1); | |
7474 | p += brace_len; | |
7475 | if (! valid) { | |
7476 | RExC_parse = p; /* going to die anyway; point | |
7477 | to exact spot of failure */ | |
f0a2b745 KW |
7478 | vFAIL(error_msg); |
7479 | } | |
00c0cb6d DG |
7480 | else |
7481 | { | |
7482 | ender = result; | |
7483 | } | |
f0a2b745 KW |
7484 | if (PL_encoding && ender < 0x100) { |
7485 | goto recode_encoding; | |
7486 | } | |
7487 | if (ender > 0xff) { | |
7488 | RExC_utf8 = 1; | |
7489 | } | |
7490 | break; | |
7491 | } | |
a0d0e21e | 7492 | case 'x': |
a0ed51b3 | 7493 | if (*++p == '{') { |
1df70142 | 7494 | char* const e = strchr(p, '}'); |
b81d288d | 7495 | |
b45f050a | 7496 | if (!e) { |
830247a4 | 7497 | RExC_parse = p + 1; |
b45f050a JF |
7498 | vFAIL("Missing right brace on \\x{}"); |
7499 | } | |
de5f0749 | 7500 | else { |
a4c04bdc NC |
7501 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
7502 | | PERL_SCAN_DISALLOW_PREFIX; | |
1df70142 | 7503 | STRLEN numlen = e - p - 1; |
53305cf1 | 7504 | ender = grok_hex(p + 1, &numlen, &flags, NULL); |
aaa80028 JH |
7505 | if (ender > 0xff) |
7506 | RExC_utf8 = 1; | |
a0ed51b3 LW |
7507 | p = e + 1; |
7508 | } | |
a0ed51b3 LW |
7509 | } |
7510 | else { | |
a4c04bdc | 7511 | I32 flags = PERL_SCAN_DISALLOW_PREFIX; |
1df70142 | 7512 | STRLEN numlen = 2; |
53305cf1 | 7513 | ender = grok_hex(p, &numlen, &flags, NULL); |
a0ed51b3 LW |
7514 | p += numlen; |
7515 | } | |
9e08bc66 TS |
7516 | if (PL_encoding && ender < 0x100) |
7517 | goto recode_encoding; | |
a687059c | 7518 | break; |
a0d0e21e LW |
7519 | case 'c': |
7520 | p++; | |
f9d13529 | 7521 | ender = grok_bslash_c(*p++, SIZE_ONLY); |
a687059c | 7522 | break; |
a0d0e21e LW |
7523 | case '0': case '1': case '2': case '3':case '4': |
7524 | case '5': case '6': case '7': case '8':case '9': | |
7525 | if (*p == '0' || | |
830247a4 | 7526 | (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) { |
53305cf1 | 7527 | I32 flags = 0; |
1df70142 | 7528 | STRLEN numlen = 3; |
53305cf1 | 7529 | ender = grok_oct(p, &numlen, &flags, NULL); |
fa1639c5 KW |
7530 | if (ender > 0xff) { |
7531 | RExC_utf8 = 1; | |
609122bd | 7532 | } |
a0d0e21e LW |
7533 | p += numlen; |
7534 | } | |
7535 | else { | |
7536 | --p; | |
7537 | goto loopdone; | |
a687059c | 7538 | } |
9e08bc66 TS |
7539 | if (PL_encoding && ender < 0x100) |
7540 | goto recode_encoding; | |
7541 | break; | |
7542 | recode_encoding: | |
7543 | { | |
7544 | SV* enc = PL_encoding; | |
7545 | ender = reg_recode((const char)(U8)ender, &enc); | |
668c081a NC |
7546 | if (!enc && SIZE_ONLY) |
7547 | ckWARNreg(p, "Invalid escape in the specified encoding"); | |
9e08bc66 TS |
7548 | RExC_utf8 = 1; |
7549 | } | |
a687059c | 7550 | break; |
a0d0e21e | 7551 | case '\0': |
830247a4 | 7552 | if (p >= RExC_end) |
b45f050a | 7553 | FAIL("Trailing \\"); |
a687059c | 7554 | /* FALL THROUGH */ |
a0d0e21e | 7555 | default: |
668c081a NC |
7556 | if (!SIZE_ONLY&& isALPHA(*p)) |
7557 | ckWARN2reg(p + 1, "Unrecognized escape \\%c passed through", UCHARAT(p)); | |
a0ed51b3 | 7558 | goto normal_default; |
a0d0e21e LW |
7559 | } |
7560 | break; | |
a687059c | 7561 | default: |
a0ed51b3 | 7562 | normal_default: |
fd400ab9 | 7563 | if (UTF8_IS_START(*p) && UTF) { |
1df70142 | 7564 | STRLEN numlen; |
5e12f4fb | 7565 | ender = utf8n_to_uvchr((U8*)p, RExC_end - p, |
9f7f3913 | 7566 | &numlen, UTF8_ALLOW_DEFAULT); |
a0ed51b3 LW |
7567 | p += numlen; |
7568 | } | |
7569 | else | |
7570 | ender = *p++; | |
a0d0e21e | 7571 | break; |
a687059c | 7572 | } |
bcdf7404 YO |
7573 | if ( RExC_flags & RXf_PMf_EXTENDED) |
7574 | p = regwhite( pRExC_state, p ); | |
60a8b682 JH |
7575 | if (UTF && FOLD) { |
7576 | /* Prime the casefolded buffer. */ | |
ac7e0132 | 7577 | ender = toFOLD_uni(ender, tmpbuf, &foldlen); |
60a8b682 | 7578 | } |
bcdf7404 | 7579 | if (p < RExC_end && ISMULT2(p)) { /* Back off on ?+*. */ |
a0d0e21e LW |
7580 | if (len) |
7581 | p = oldp; | |
16ea2a2e | 7582 | else if (UTF) { |
80aecb99 | 7583 | if (FOLD) { |
60a8b682 | 7584 | /* Emit all the Unicode characters. */ |
1df70142 | 7585 | STRLEN numlen; |
80aecb99 JH |
7586 | for (foldbuf = tmpbuf; |
7587 | foldlen; | |
7588 | foldlen -= numlen) { | |
7589 | ender = utf8_to_uvchr(foldbuf, &numlen); | |
9dc45d57 | 7590 | if (numlen > 0) { |
71207a34 | 7591 | const STRLEN unilen = reguni(pRExC_state, ender, s); |
0ebc6274 JH |
7592 | s += unilen; |
7593 | len += unilen; | |
7594 | /* In EBCDIC the numlen | |
7595 | * and unilen can differ. */ | |
9dc45d57 | 7596 | foldbuf += numlen; |
47654450 JH |
7597 | if (numlen >= foldlen) |
7598 | break; | |
9dc45d57 JH |
7599 | } |
7600 | else | |
7601 | break; /* "Can't happen." */ | |
80aecb99 JH |
7602 | } |
7603 | } | |
7604 | else { | |
71207a34 | 7605 | const STRLEN unilen = reguni(pRExC_state, ender, s); |
9ede7db1 | 7606 | if (unilen > 0) { |
0ebc6274 JH |
7607 | s += unilen; |
7608 | len += unilen; | |
9dc45d57 | 7609 | } |
80aecb99 | 7610 | } |
a0ed51b3 | 7611 | } |
a0d0e21e LW |
7612 | else { |
7613 | len++; | |
eb160463 | 7614 | REGC((char)ender, s++); |
a0d0e21e LW |
7615 | } |
7616 | break; | |
a687059c | 7617 | } |
16ea2a2e | 7618 | if (UTF) { |
80aecb99 | 7619 | if (FOLD) { |
60a8b682 | 7620 | /* Emit all the Unicode characters. */ |
1df70142 | 7621 | STRLEN numlen; |
80aecb99 JH |
7622 | for (foldbuf = tmpbuf; |
7623 | foldlen; | |
7624 | foldlen -= numlen) { | |
7625 | ender = utf8_to_uvchr(foldbuf, &numlen); | |
9dc45d57 | 7626 | if (numlen > 0) { |
71207a34 | 7627 | const STRLEN unilen = reguni(pRExC_state, ender, s); |
0ebc6274 JH |
7628 | len += unilen; |
7629 | s += unilen; | |
7630 | /* In EBCDIC the numlen | |
7631 | * and unilen can differ. */ | |
9dc45d57 | 7632 | foldbuf += numlen; |
47654450 JH |
7633 | if (numlen >= foldlen) |
7634 | break; | |
9dc45d57 JH |
7635 | } |
7636 | else | |
7637 | break; | |
80aecb99 JH |
7638 | } |
7639 | } | |
7640 | else { | |
71207a34 | 7641 | const STRLEN unilen = reguni(pRExC_state, ender, s); |
9ede7db1 | 7642 | if (unilen > 0) { |
0ebc6274 JH |
7643 | s += unilen; |
7644 | len += unilen; | |
9dc45d57 | 7645 | } |
80aecb99 JH |
7646 | } |
7647 | len--; | |
a0ed51b3 LW |
7648 | } |
7649 | else | |
eb160463 | 7650 | REGC((char)ender, s++); |
a0d0e21e LW |
7651 | } |
7652 | loopdone: | |
830247a4 | 7653 | RExC_parse = p - 1; |
fac92740 | 7654 | Set_Node_Cur_Length(ret); /* MJD */ |
830247a4 | 7655 | nextchar(pRExC_state); |
793db0cb JH |
7656 | { |
7657 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
7658 | IV iv = len; | |
7659 | if (iv < 0) | |
7660 | vFAIL("Internal disaster"); | |
7661 | } | |
a0d0e21e LW |
7662 | if (len > 0) |
7663 | *flagp |= HASWIDTH; | |
090f7165 | 7664 | if (len == 1 && UNI_IS_INVARIANT(ender)) |
a0d0e21e | 7665 | *flagp |= SIMPLE; |
3dab1dad | 7666 | |
cd439c50 | 7667 | if (SIZE_ONLY) |
830247a4 | 7668 | RExC_size += STR_SZ(len); |
3dab1dad YO |
7669 | else { |
7670 | STR_LEN(ret) = len; | |
830247a4 | 7671 | RExC_emit += STR_SZ(len); |
07be1b83 | 7672 | } |
3dab1dad | 7673 | } |
a0d0e21e LW |
7674 | break; |
7675 | } | |
a687059c | 7676 | |
a0d0e21e | 7677 | return(ret); |
a687059c LW |
7678 | } |
7679 | ||
873ef191 | 7680 | STATIC char * |
bcdf7404 | 7681 | S_regwhite( RExC_state_t *pRExC_state, char *p ) |
5b5a24f7 | 7682 | { |
bcdf7404 | 7683 | const char *e = RExC_end; |
7918f24d NC |
7684 | |
7685 | PERL_ARGS_ASSERT_REGWHITE; | |
7686 | ||
5b5a24f7 CS |
7687 | while (p < e) { |
7688 | if (isSPACE(*p)) | |
7689 | ++p; | |
7690 | else if (*p == '#') { | |
bcdf7404 | 7691 | bool ended = 0; |
5b5a24f7 | 7692 | do { |
bcdf7404 YO |
7693 | if (*p++ == '\n') { |
7694 | ended = 1; | |
7695 | break; | |
7696 | } | |
7697 | } while (p < e); | |
7698 | if (!ended) | |
7699 | RExC_seen |= REG_SEEN_RUN_ON_COMMENT; | |
5b5a24f7 CS |
7700 | } |
7701 | else | |
7702 | break; | |
7703 | } | |
7704 | return p; | |
7705 | } | |
7706 | ||
b8c5462f JH |
7707 | /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]]. |
7708 | Character classes ([:foo:]) can also be negated ([:^foo:]). | |
7709 | Returns a named class id (ANYOF_XXX) if successful, -1 otherwise. | |
7710 | Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed, | |
beeb77fc | 7711 | but trigger failures because they are currently unimplemented. */ |
9a86a77b JH |
7712 | |
7713 | #define POSIXCC_DONE(c) ((c) == ':') | |
7714 | #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.') | |
7715 | #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c)) | |
7716 | ||
b8c5462f | 7717 | STATIC I32 |
830247a4 | 7718 | S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value) |
620e46c5 | 7719 | { |
97aff369 | 7720 | dVAR; |
936ed897 | 7721 | I32 namedclass = OOB_NAMEDCLASS; |
620e46c5 | 7722 | |
7918f24d NC |
7723 | PERL_ARGS_ASSERT_REGPPOSIXCC; |
7724 | ||
830247a4 | 7725 | if (value == '[' && RExC_parse + 1 < RExC_end && |
620e46c5 | 7726 | /* I smell either [: or [= or [. -- POSIX has been here, right? */ |
9a86a77b | 7727 | POSIXCC(UCHARAT(RExC_parse))) { |
1df70142 | 7728 | const char c = UCHARAT(RExC_parse); |
097eb12c | 7729 | char* const s = RExC_parse++; |
b81d288d | 7730 | |
9a86a77b | 7731 | while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c) |
830247a4 IZ |
7732 | RExC_parse++; |
7733 | if (RExC_parse == RExC_end) | |
620e46c5 | 7734 | /* Grandfather lone [:, [=, [. */ |
830247a4 | 7735 | RExC_parse = s; |
620e46c5 | 7736 | else { |
3dab1dad | 7737 | const char* const t = RExC_parse++; /* skip over the c */ |
80916619 NC |
7738 | assert(*t == c); |
7739 | ||
9a86a77b | 7740 | if (UCHARAT(RExC_parse) == ']') { |
3dab1dad | 7741 | const char *posixcc = s + 1; |
830247a4 | 7742 | RExC_parse++; /* skip over the ending ] */ |
3dab1dad | 7743 | |
b8c5462f | 7744 | if (*s == ':') { |
1df70142 AL |
7745 | const I32 complement = *posixcc == '^' ? *posixcc++ : 0; |
7746 | const I32 skip = t - posixcc; | |
80916619 NC |
7747 | |
7748 | /* Initially switch on the length of the name. */ | |
7749 | switch (skip) { | |
7750 | case 4: | |
3dab1dad YO |
7751 | if (memEQ(posixcc, "word", 4)) /* this is not POSIX, this is the Perl \w */ |
7752 | namedclass = complement ? ANYOF_NALNUM : ANYOF_ALNUM; | |
cc4319de | 7753 | break; |
80916619 NC |
7754 | case 5: |
7755 | /* Names all of length 5. */ | |
7756 | /* alnum alpha ascii blank cntrl digit graph lower | |
7757 | print punct space upper */ | |
7758 | /* Offset 4 gives the best switch position. */ | |
7759 | switch (posixcc[4]) { | |
7760 | case 'a': | |
3dab1dad YO |
7761 | if (memEQ(posixcc, "alph", 4)) /* alpha */ |
7762 | namedclass = complement ? ANYOF_NALPHA : ANYOF_ALPHA; | |
80916619 NC |
7763 | break; |
7764 | case 'e': | |
3dab1dad YO |
7765 | if (memEQ(posixcc, "spac", 4)) /* space */ |
7766 | namedclass = complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC; | |
80916619 NC |
7767 | break; |
7768 | case 'h': | |
3dab1dad YO |
7769 | if (memEQ(posixcc, "grap", 4)) /* graph */ |
7770 | namedclass = complement ? ANYOF_NGRAPH : ANYOF_GRAPH; | |
80916619 NC |
7771 | break; |
7772 | case 'i': | |
3dab1dad YO |
7773 | if (memEQ(posixcc, "asci", 4)) /* ascii */ |
7774 | namedclass = complement ? ANYOF_NASCII : ANYOF_ASCII; | |
80916619 NC |
7775 | break; |
7776 | case 'k': | |
3dab1dad YO |
7777 | if (memEQ(posixcc, "blan", 4)) /* blank */ |
7778 | namedclass = complement ? ANYOF_NBLANK : ANYOF_BLANK; | |
80916619 NC |
7779 | break; |
7780 | case 'l': | |
3dab1dad YO |
7781 | if (memEQ(posixcc, "cntr", 4)) /* cntrl */ |
7782 | namedclass = complement ? ANYOF_NCNTRL : ANYOF_CNTRL; | |
80916619 NC |
7783 | break; |
7784 | case 'm': | |
3dab1dad YO |
7785 | if (memEQ(posixcc, "alnu", 4)) /* alnum */ |
7786 | namedclass = complement ? ANYOF_NALNUMC : ANYOF_ALNUMC; | |
80916619 NC |
7787 | break; |
7788 | case 'r': | |
3dab1dad YO |
7789 | if (memEQ(posixcc, "lowe", 4)) /* lower */ |
7790 | namedclass = complement ? ANYOF_NLOWER : ANYOF_LOWER; | |
7791 | else if (memEQ(posixcc, "uppe", 4)) /* upper */ | |
7792 | namedclass = complement ? ANYOF_NUPPER : ANYOF_UPPER; | |
80916619 NC |
7793 | break; |
7794 | case 't': | |
3dab1dad YO |
7795 | if (memEQ(posixcc, "digi", 4)) /* digit */ |
7796 | namedclass = complement ? ANYOF_NDIGIT : ANYOF_DIGIT; | |
7797 | else if (memEQ(posixcc, "prin", 4)) /* print */ | |
7798 | namedclass = complement ? ANYOF_NPRINT : ANYOF_PRINT; | |
7799 | else if (memEQ(posixcc, "punc", 4)) /* punct */ | |
7800 | namedclass = complement ? ANYOF_NPUNCT : ANYOF_PUNCT; | |
80916619 | 7801 | break; |
b8c5462f JH |
7802 | } |
7803 | break; | |
80916619 | 7804 | case 6: |
3dab1dad YO |
7805 | if (memEQ(posixcc, "xdigit", 6)) |
7806 | namedclass = complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT; | |
b8c5462f JH |
7807 | break; |
7808 | } | |
80916619 NC |
7809 | |
7810 | if (namedclass == OOB_NAMEDCLASS) | |
b45f050a JF |
7811 | Simple_vFAIL3("POSIX class [:%.*s:] unknown", |
7812 | t - s - 1, s + 1); | |
80916619 NC |
7813 | assert (posixcc[skip] == ':'); |
7814 | assert (posixcc[skip+1] == ']'); | |
b45f050a | 7815 | } else if (!SIZE_ONLY) { |
b8c5462f | 7816 | /* [[=foo=]] and [[.foo.]] are still future. */ |
b45f050a | 7817 | |
830247a4 | 7818 | /* adjust RExC_parse so the warning shows after |
b45f050a | 7819 | the class closes */ |
9a86a77b | 7820 | while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']') |
830247a4 | 7821 | RExC_parse++; |
b45f050a JF |
7822 | Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c); |
7823 | } | |
b8c5462f JH |
7824 | } else { |
7825 | /* Maternal grandfather: | |
7826 | * "[:" ending in ":" but not in ":]" */ | |
830247a4 | 7827 | RExC_parse = s; |
767d463e | 7828 | } |
620e46c5 JH |
7829 | } |
7830 | } | |
7831 | ||
b8c5462f JH |
7832 | return namedclass; |
7833 | } | |
7834 | ||
7835 | STATIC void | |
830247a4 | 7836 | S_checkposixcc(pTHX_ RExC_state_t *pRExC_state) |
b8c5462f | 7837 | { |
97aff369 | 7838 | dVAR; |
7918f24d NC |
7839 | |
7840 | PERL_ARGS_ASSERT_CHECKPOSIXCC; | |
7841 | ||
3dab1dad | 7842 | if (POSIXCC(UCHARAT(RExC_parse))) { |
1df70142 AL |
7843 | const char *s = RExC_parse; |
7844 | const char c = *s++; | |
b8c5462f | 7845 | |
3dab1dad | 7846 | while (isALNUM(*s)) |
b8c5462f JH |
7847 | s++; |
7848 | if (*s && c == *s && s[1] == ']') { | |
668c081a NC |
7849 | ckWARN3reg(s+2, |
7850 | "POSIX syntax [%c %c] belongs inside character classes", | |
7851 | c, c); | |
b45f050a JF |
7852 | |
7853 | /* [[=foo=]] and [[.foo.]] are still future. */ | |
9a86a77b | 7854 | if (POSIXCC_NOTYET(c)) { |
830247a4 | 7855 | /* adjust RExC_parse so the error shows after |
b45f050a | 7856 | the class closes */ |
9a86a77b | 7857 | while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']') |
3dab1dad | 7858 | NOOP; |
b45f050a JF |
7859 | Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c); |
7860 | } | |
b8c5462f JH |
7861 | } |
7862 | } | |
620e46c5 JH |
7863 | } |
7864 | ||
7f6f358c | 7865 | |
89836f1f YO |
7866 | #define _C_C_T_(NAME,TEST,WORD) \ |
7867 | ANYOF_##NAME: \ | |
7868 | if (LOC) \ | |
7869 | ANYOF_CLASS_SET(ret, ANYOF_##NAME); \ | |
7870 | else { \ | |
7871 | for (value = 0; value < 256; value++) \ | |
7872 | if (TEST) \ | |
7873 | ANYOF_BITMAP_SET(ret, value); \ | |
7874 | } \ | |
7875 | yesno = '+'; \ | |
7876 | what = WORD; \ | |
7877 | break; \ | |
7878 | case ANYOF_N##NAME: \ | |
7879 | if (LOC) \ | |
7880 | ANYOF_CLASS_SET(ret, ANYOF_N##NAME); \ | |
7881 | else { \ | |
7882 | for (value = 0; value < 256; value++) \ | |
7883 | if (!TEST) \ | |
7884 | ANYOF_BITMAP_SET(ret, value); \ | |
7885 | } \ | |
7886 | yesno = '!'; \ | |
7887 | what = WORD; \ | |
7888 | break | |
7889 | ||
e1d1eefb YO |
7890 | #define _C_C_T_NOLOC_(NAME,TEST,WORD) \ |
7891 | ANYOF_##NAME: \ | |
7892 | for (value = 0; value < 256; value++) \ | |
7893 | if (TEST) \ | |
7894 | ANYOF_BITMAP_SET(ret, value); \ | |
7895 | yesno = '+'; \ | |
7896 | what = WORD; \ | |
7897 | break; \ | |
7898 | case ANYOF_N##NAME: \ | |
7899 | for (value = 0; value < 256; value++) \ | |
7900 | if (!TEST) \ | |
7901 | ANYOF_BITMAP_SET(ret, value); \ | |
7902 | yesno = '!'; \ | |
7903 | what = WORD; \ | |
7904 | break | |
89836f1f | 7905 | |
da7fcca4 YO |
7906 | /* |
7907 | We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test | |
7908 | so that it is possible to override the option here without having to | |
7909 | rebuild the entire core. as we are required to do if we change regcomp.h | |
7910 | which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined. | |
7911 | */ | |
7912 | #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS | |
7913 | #define BROKEN_UNICODE_CHARCLASS_MAPPINGS | |
7914 | #endif | |
7915 | ||
7916 | #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS | |
7917 | #define POSIX_CC_UNI_NAME(CCNAME) CCNAME | |
7918 | #else | |
7919 | #define POSIX_CC_UNI_NAME(CCNAME) "Posix" CCNAME | |
7920 | #endif | |
7921 | ||
7f6f358c YO |
7922 | /* |
7923 | parse a class specification and produce either an ANYOF node that | |
89836f1f YO |
7924 | matches the pattern or if the pattern matches a single char only and |
7925 | that char is < 256 and we are case insensitive then we produce an | |
7926 | EXACT node instead. | |
7f6f358c | 7927 | */ |
89836f1f | 7928 | |
76e3520e | 7929 | STATIC regnode * |
3dab1dad | 7930 | S_regclass(pTHX_ RExC_state_t *pRExC_state, U32 depth) |
a687059c | 7931 | { |
97aff369 | 7932 | dVAR; |
9a86a77b | 7933 | register UV nextvalue; |
3568d838 | 7934 | register IV prevvalue = OOB_UNICODE; |
ffc61ed2 | 7935 | register IV range = 0; |
e1d1eefb | 7936 | UV value = 0; /* XXX:dmq: needs to be referenceable (unfortunately) */ |
c277df42 | 7937 | register regnode *ret; |
ba210ebe | 7938 | STRLEN numlen; |
ffc61ed2 | 7939 | IV namedclass; |
cbbf8932 | 7940 | char *rangebegin = NULL; |
936ed897 | 7941 | bool need_class = 0; |
c445ea15 | 7942 | SV *listsv = NULL; |
ffc61ed2 | 7943 | UV n; |
9e55ce06 | 7944 | bool optimize_invert = TRUE; |
cbbf8932 | 7945 | AV* unicode_alternate = NULL; |
1b2d223b JH |
7946 | #ifdef EBCDIC |
7947 | UV literal_endpoint = 0; | |
7948 | #endif | |
7f6f358c | 7949 | UV stored = 0; /* number of chars stored in the class */ |
ffc61ed2 | 7950 | |
3dab1dad | 7951 | regnode * const orig_emit = RExC_emit; /* Save the original RExC_emit in |
7f6f358c | 7952 | case we need to change the emitted regop to an EXACT. */ |
07be1b83 | 7953 | const char * orig_parse = RExC_parse; |
72f13be8 | 7954 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
7955 | |
7956 | PERL_ARGS_ASSERT_REGCLASS; | |
76e84362 SH |
7957 | #ifndef DEBUGGING |
7958 | PERL_UNUSED_ARG(depth); | |
7959 | #endif | |
72f13be8 | 7960 | |
3dab1dad | 7961 | DEBUG_PARSE("clas"); |
7f6f358c YO |
7962 | |
7963 | /* Assume we are going to generate an ANYOF node. */ | |
ffc61ed2 JH |
7964 | ret = reganode(pRExC_state, ANYOF, 0); |
7965 | ||
7966 | if (!SIZE_ONLY) | |
7967 | ANYOF_FLAGS(ret) = 0; | |
7968 | ||
9a86a77b | 7969 | if (UCHARAT(RExC_parse) == '^') { /* Complement of range. */ |
ffc61ed2 JH |
7970 | RExC_naughty++; |
7971 | RExC_parse++; | |
7972 | if (!SIZE_ONLY) | |
7973 | ANYOF_FLAGS(ret) |= ANYOF_INVERT; | |
7974 | } | |
a0d0e21e | 7975 | |
73060fc4 | 7976 | if (SIZE_ONLY) { |
830247a4 | 7977 | RExC_size += ANYOF_SKIP; |
73060fc4 JH |
7978 | listsv = &PL_sv_undef; /* For code scanners: listsv always non-NULL. */ |
7979 | } | |
936ed897 | 7980 | else { |
830247a4 | 7981 | RExC_emit += ANYOF_SKIP; |
936ed897 IZ |
7982 | if (FOLD) |
7983 | ANYOF_FLAGS(ret) |= ANYOF_FOLD; | |
7984 | if (LOC) | |
7985 | ANYOF_FLAGS(ret) |= ANYOF_LOCALE; | |
ffc61ed2 | 7986 | ANYOF_BITMAP_ZERO(ret); |
396482e1 | 7987 | listsv = newSVpvs("# comment\n"); |
a0d0e21e | 7988 | } |
b8c5462f | 7989 | |
9a86a77b JH |
7990 | nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0; |
7991 | ||
b938889d | 7992 | if (!SIZE_ONLY && POSIXCC(nextvalue)) |
830247a4 | 7993 | checkposixcc(pRExC_state); |
b8c5462f | 7994 | |
f064b6ad HS |
7995 | /* allow 1st char to be ] (allowing it to be - is dealt with later) */ |
7996 | if (UCHARAT(RExC_parse) == ']') | |
7997 | goto charclassloop; | |
ffc61ed2 | 7998 | |
fc8cd66c | 7999 | parseit: |
9a86a77b | 8000 | while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') { |
ffc61ed2 JH |
8001 | |
8002 | charclassloop: | |
8003 | ||
8004 | namedclass = OOB_NAMEDCLASS; /* initialize as illegal */ | |
8005 | ||
73b437c8 | 8006 | if (!range) |
830247a4 | 8007 | rangebegin = RExC_parse; |
ffc61ed2 | 8008 | if (UTF) { |
5e12f4fb | 8009 | value = utf8n_to_uvchr((U8*)RExC_parse, |
3568d838 | 8010 | RExC_end - RExC_parse, |
9f7f3913 | 8011 | &numlen, UTF8_ALLOW_DEFAULT); |
ffc61ed2 JH |
8012 | RExC_parse += numlen; |
8013 | } | |
8014 | else | |
8015 | value = UCHARAT(RExC_parse++); | |
7f6f358c | 8016 | |
9a86a77b JH |
8017 | nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0; |
8018 | if (value == '[' && POSIXCC(nextvalue)) | |
830247a4 | 8019 | namedclass = regpposixcc(pRExC_state, value); |
620e46c5 | 8020 | else if (value == '\\') { |
ffc61ed2 | 8021 | if (UTF) { |
5e12f4fb | 8022 | value = utf8n_to_uvchr((U8*)RExC_parse, |
ffc61ed2 | 8023 | RExC_end - RExC_parse, |
9f7f3913 | 8024 | &numlen, UTF8_ALLOW_DEFAULT); |
ffc61ed2 JH |
8025 | RExC_parse += numlen; |
8026 | } | |
8027 | else | |
8028 | value = UCHARAT(RExC_parse++); | |
470c3474 | 8029 | /* Some compilers cannot handle switching on 64-bit integer |
ffc61ed2 | 8030 | * values, therefore value cannot be an UV. Yes, this will |
e2962f66 JH |
8031 | * be a problem later if we want switch on Unicode. |
8032 | * A similar issue a little bit later when switching on | |
8033 | * namedclass. --jhi */ | |
ffc61ed2 | 8034 | switch ((I32)value) { |
b8c5462f JH |
8035 | case 'w': namedclass = ANYOF_ALNUM; break; |
8036 | case 'W': namedclass = ANYOF_NALNUM; break; | |
8037 | case 's': namedclass = ANYOF_SPACE; break; | |
8038 | case 'S': namedclass = ANYOF_NSPACE; break; | |
8039 | case 'd': namedclass = ANYOF_DIGIT; break; | |
8040 | case 'D': namedclass = ANYOF_NDIGIT; break; | |
e1d1eefb YO |
8041 | case 'v': namedclass = ANYOF_VERTWS; break; |
8042 | case 'V': namedclass = ANYOF_NVERTWS; break; | |
8043 | case 'h': namedclass = ANYOF_HORIZWS; break; | |
8044 | case 'H': namedclass = ANYOF_NHORIZWS; break; | |
fc8cd66c YO |
8045 | case 'N': /* Handle \N{NAME} in class */ |
8046 | { | |
8047 | /* We only pay attention to the first char of | |
8048 | multichar strings being returned. I kinda wonder | |
8049 | if this makes sense as it does change the behaviour | |
8050 | from earlier versions, OTOH that behaviour was broken | |
8051 | as well. */ | |
8052 | UV v; /* value is register so we cant & it /grrr */ | |
afefe6bf | 8053 | if (reg_namedseq(pRExC_state, &v, NULL)) { |
fc8cd66c YO |
8054 | goto parseit; |
8055 | } | |
8056 | value= v; | |
8057 | } | |
8058 | break; | |
ffc61ed2 JH |
8059 | case 'p': |
8060 | case 'P': | |
3dab1dad YO |
8061 | { |
8062 | char *e; | |
af6f566e | 8063 | if (RExC_parse >= RExC_end) |
2a4859cd | 8064 | vFAIL2("Empty \\%c{}", (U8)value); |
ffc61ed2 | 8065 | if (*RExC_parse == '{') { |
1df70142 | 8066 | const U8 c = (U8)value; |
ffc61ed2 JH |
8067 | e = strchr(RExC_parse++, '}'); |
8068 | if (!e) | |
0da60cf5 | 8069 | vFAIL2("Missing right brace on \\%c{}", c); |
ab13f0c7 JH |
8070 | while (isSPACE(UCHARAT(RExC_parse))) |
8071 | RExC_parse++; | |
8072 | if (e == RExC_parse) | |
0da60cf5 | 8073 | vFAIL2("Empty \\%c{}", c); |
ffc61ed2 | 8074 | n = e - RExC_parse; |
ab13f0c7 JH |
8075 | while (isSPACE(UCHARAT(RExC_parse + n - 1))) |
8076 | n--; | |
ffc61ed2 JH |
8077 | } |
8078 | else { | |
8079 | e = RExC_parse; | |
8080 | n = 1; | |
8081 | } | |
8082 | if (!SIZE_ONLY) { | |
ab13f0c7 JH |
8083 | if (UCHARAT(RExC_parse) == '^') { |
8084 | RExC_parse++; | |
8085 | n--; | |
8086 | value = value == 'p' ? 'P' : 'p'; /* toggle */ | |
8087 | while (isSPACE(UCHARAT(RExC_parse))) { | |
8088 | RExC_parse++; | |
8089 | n--; | |
8090 | } | |
8091 | } | |
097eb12c AL |
8092 | Perl_sv_catpvf(aTHX_ listsv, "%cutf8::%.*s\n", |
8093 | (value=='p' ? '+' : '!'), (int)n, RExC_parse); | |
ffc61ed2 JH |
8094 | } |
8095 | RExC_parse = e + 1; | |
8096 | ANYOF_FLAGS(ret) |= ANYOF_UNICODE; | |
f81125e2 | 8097 | namedclass = ANYOF_MAX; /* no official name, but it's named */ |
3dab1dad | 8098 | } |
f81125e2 | 8099 | break; |
b8c5462f JH |
8100 | case 'n': value = '\n'; break; |
8101 | case 'r': value = '\r'; break; | |
8102 | case 't': value = '\t'; break; | |
8103 | case 'f': value = '\f'; break; | |
8104 | case 'b': value = '\b'; break; | |
c7f1f016 NIS |
8105 | case 'e': value = ASCII_TO_NATIVE('\033');break; |
8106 | case 'a': value = ASCII_TO_NATIVE('\007');break; | |
f0a2b745 KW |
8107 | case 'o': |
8108 | RExC_parse--; /* function expects to be pointed at the 'o' */ | |
454155d9 KW |
8109 | { |
8110 | const char* error_msg; | |
8111 | bool valid = grok_bslash_o(RExC_parse, | |
f0a2b745 KW |
8112 | &value, |
8113 | &numlen, | |
454155d9 KW |
8114 | &error_msg, |
8115 | SIZE_ONLY); | |
8116 | RExC_parse += numlen; | |
8117 | if (! valid) { | |
8118 | vFAIL(error_msg); | |
8119 | } | |
f0a2b745 | 8120 | } |
f0a2b745 KW |
8121 | if (PL_encoding && value < 0x100) { |
8122 | goto recode_encoding; | |
8123 | } | |
8124 | break; | |
b8c5462f | 8125 | case 'x': |
ffc61ed2 | 8126 | if (*RExC_parse == '{') { |
a4c04bdc NC |
8127 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES |
8128 | | PERL_SCAN_DISALLOW_PREFIX; | |
3dab1dad | 8129 | char * const e = strchr(RExC_parse++, '}'); |
b81d288d | 8130 | if (!e) |
ffc61ed2 | 8131 | vFAIL("Missing right brace on \\x{}"); |
53305cf1 NC |
8132 | |
8133 | numlen = e - RExC_parse; | |
8134 | value = grok_hex(RExC_parse, &numlen, &flags, NULL); | |
ffc61ed2 JH |
8135 | RExC_parse = e + 1; |
8136 | } | |
8137 | else { | |
a4c04bdc | 8138 | I32 flags = PERL_SCAN_DISALLOW_PREFIX; |
53305cf1 NC |
8139 | numlen = 2; |
8140 | value = grok_hex(RExC_parse, &numlen, &flags, NULL); | |
ffc61ed2 JH |
8141 | RExC_parse += numlen; |
8142 | } | |
9e08bc66 TS |
8143 | if (PL_encoding && value < 0x100) |
8144 | goto recode_encoding; | |
b8c5462f JH |
8145 | break; |
8146 | case 'c': | |
f9d13529 | 8147 | value = grok_bslash_c(*RExC_parse++, SIZE_ONLY); |
b8c5462f JH |
8148 | break; |
8149 | case '0': case '1': case '2': case '3': case '4': | |
8150 | case '5': case '6': case '7': case '8': case '9': | |
9e08bc66 TS |
8151 | { |
8152 | I32 flags = 0; | |
8153 | numlen = 3; | |
8154 | value = grok_oct(--RExC_parse, &numlen, &flags, NULL); | |
8155 | RExC_parse += numlen; | |
8156 | if (PL_encoding && value < 0x100) | |
8157 | goto recode_encoding; | |
8158 | break; | |
8159 | } | |
8160 | recode_encoding: | |
8161 | { | |
8162 | SV* enc = PL_encoding; | |
8163 | value = reg_recode((const char)(U8)value, &enc); | |
668c081a NC |
8164 | if (!enc && SIZE_ONLY) |
8165 | ckWARNreg(RExC_parse, | |
8166 | "Invalid escape in the specified encoding"); | |
9e08bc66 TS |
8167 | break; |
8168 | } | |
1028017a | 8169 | default: |
668c081a NC |
8170 | if (!SIZE_ONLY && isALPHA(value)) |
8171 | ckWARN2reg(RExC_parse, | |
8172 | "Unrecognized escape \\%c in character class passed through", | |
8173 | (int)value); | |
1028017a | 8174 | break; |
b8c5462f | 8175 | } |
ffc61ed2 | 8176 | } /* end of \blah */ |
1b2d223b JH |
8177 | #ifdef EBCDIC |
8178 | else | |
8179 | literal_endpoint++; | |
8180 | #endif | |
ffc61ed2 JH |
8181 | |
8182 | if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */ | |
8183 | ||
8184 | if (!SIZE_ONLY && !need_class) | |
936ed897 | 8185 | ANYOF_CLASS_ZERO(ret); |
ffc61ed2 | 8186 | |
936ed897 | 8187 | need_class = 1; |
ffc61ed2 JH |
8188 | |
8189 | /* a bad range like a-\d, a-[:digit:] ? */ | |
8190 | if (range) { | |
73b437c8 | 8191 | if (!SIZE_ONLY) { |
668c081a NC |
8192 | const int w = |
8193 | RExC_parse >= rangebegin ? | |
8194 | RExC_parse - rangebegin : 0; | |
8195 | ckWARN4reg(RExC_parse, | |
b45f050a | 8196 | "False [] range \"%*.*s\"", |
097eb12c | 8197 | w, w, rangebegin); |
668c081a | 8198 | |
3568d838 JH |
8199 | if (prevvalue < 256) { |
8200 | ANYOF_BITMAP_SET(ret, prevvalue); | |
ffc61ed2 JH |
8201 | ANYOF_BITMAP_SET(ret, '-'); |
8202 | } | |
8203 | else { | |
8204 | ANYOF_FLAGS(ret) |= ANYOF_UNICODE; | |
8205 | Perl_sv_catpvf(aTHX_ listsv, | |
3568d838 | 8206 | "%04"UVxf"\n%04"UVxf"\n", (UV)prevvalue, (UV) '-'); |
ffc61ed2 | 8207 | } |
b8c5462f | 8208 | } |
ffc61ed2 JH |
8209 | |
8210 | range = 0; /* this was not a true range */ | |
73b437c8 | 8211 | } |
ffc61ed2 | 8212 | |
89836f1f YO |
8213 | |
8214 | ||
73b437c8 | 8215 | if (!SIZE_ONLY) { |
c49a72a9 NC |
8216 | const char *what = NULL; |
8217 | char yesno = 0; | |
8218 | ||
3568d838 JH |
8219 | if (namedclass > OOB_NAMEDCLASS) |
8220 | optimize_invert = FALSE; | |
e2962f66 JH |
8221 | /* Possible truncation here but in some 64-bit environments |
8222 | * the compiler gets heartburn about switch on 64-bit values. | |
8223 | * A similar issue a little earlier when switching on value. | |
98f323fa | 8224 | * --jhi */ |
e2962f66 | 8225 | switch ((I32)namedclass) { |
da7fcca4 YO |
8226 | |
8227 | case _C_C_T_(ALNUMC, isALNUMC(value), POSIX_CC_UNI_NAME("Alnum")); | |
8228 | case _C_C_T_(ALPHA, isALPHA(value), POSIX_CC_UNI_NAME("Alpha")); | |
8229 | case _C_C_T_(BLANK, isBLANK(value), POSIX_CC_UNI_NAME("Blank")); | |
8230 | case _C_C_T_(CNTRL, isCNTRL(value), POSIX_CC_UNI_NAME("Cntrl")); | |
8231 | case _C_C_T_(GRAPH, isGRAPH(value), POSIX_CC_UNI_NAME("Graph")); | |
8232 | case _C_C_T_(LOWER, isLOWER(value), POSIX_CC_UNI_NAME("Lower")); | |
8233 | case _C_C_T_(PRINT, isPRINT(value), POSIX_CC_UNI_NAME("Print")); | |
8234 | case _C_C_T_(PSXSPC, isPSXSPC(value), POSIX_CC_UNI_NAME("Space")); | |
8235 | case _C_C_T_(PUNCT, isPUNCT(value), POSIX_CC_UNI_NAME("Punct")); | |
8236 | case _C_C_T_(UPPER, isUPPER(value), POSIX_CC_UNI_NAME("Upper")); | |
8237 | #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS | |
89836f1f | 8238 | case _C_C_T_(ALNUM, isALNUM(value), "Word"); |
89836f1f | 8239 | case _C_C_T_(SPACE, isSPACE(value), "SpacePerl"); |
da7fcca4 YO |
8240 | #else |
8241 | case _C_C_T_(SPACE, isSPACE(value), "PerlSpace"); | |
8242 | case _C_C_T_(ALNUM, isALNUM(value), "PerlWord"); | |
8243 | #endif | |
89836f1f | 8244 | case _C_C_T_(XDIGIT, isXDIGIT(value), "XDigit"); |
e1d1eefb YO |
8245 | case _C_C_T_NOLOC_(VERTWS, is_VERTWS_latin1(&value), "VertSpace"); |
8246 | case _C_C_T_NOLOC_(HORIZWS, is_HORIZWS_latin1(&value), "HorizSpace"); | |
73b437c8 JH |
8247 | case ANYOF_ASCII: |
8248 | if (LOC) | |
936ed897 | 8249 | ANYOF_CLASS_SET(ret, ANYOF_ASCII); |
73b437c8 | 8250 | else { |
c7f1f016 | 8251 | #ifndef EBCDIC |
1ba5c669 JH |
8252 | for (value = 0; value < 128; value++) |
8253 | ANYOF_BITMAP_SET(ret, value); | |
8254 | #else /* EBCDIC */ | |
ffbc6a93 | 8255 | for (value = 0; value < 256; value++) { |
3a3c4447 JH |
8256 | if (isASCII(value)) |
8257 | ANYOF_BITMAP_SET(ret, value); | |
ffbc6a93 | 8258 | } |
1ba5c669 | 8259 | #endif /* EBCDIC */ |
73b437c8 | 8260 | } |
c49a72a9 NC |
8261 | yesno = '+'; |
8262 | what = "ASCII"; | |
73b437c8 JH |
8263 | break; |
8264 | case ANYOF_NASCII: | |
8265 | if (LOC) | |
936ed897 | 8266 | ANYOF_CLASS_SET(ret, ANYOF_NASCII); |
73b437c8 | 8267 | else { |
c7f1f016 | 8268 | #ifndef EBCDIC |
1ba5c669 JH |
8269 | for (value = 128; value < 256; value++) |
8270 | ANYOF_BITMAP_SET(ret, value); | |
8271 | #else /* EBCDIC */ | |
ffbc6a93 | 8272 | for (value = 0; value < 256; value++) { |
3a3c4447 JH |
8273 | if (!isASCII(value)) |
8274 | ANYOF_BITMAP_SET(ret, value); | |
ffbc6a93 | 8275 | } |
1ba5c669 | 8276 | #endif /* EBCDIC */ |
73b437c8 | 8277 | } |
c49a72a9 NC |
8278 | yesno = '!'; |
8279 | what = "ASCII"; | |
89836f1f | 8280 | break; |
ffc61ed2 JH |
8281 | case ANYOF_DIGIT: |
8282 | if (LOC) | |
8283 | ANYOF_CLASS_SET(ret, ANYOF_DIGIT); | |
8284 | else { | |
8285 | /* consecutive digits assumed */ | |
8286 | for (value = '0'; value <= '9'; value++) | |
8287 | ANYOF_BITMAP_SET(ret, value); | |
8288 | } | |
c49a72a9 | 8289 | yesno = '+'; |
da7fcca4 | 8290 | what = POSIX_CC_UNI_NAME("Digit"); |
ffc61ed2 JH |
8291 | break; |
8292 | case ANYOF_NDIGIT: | |
8293 | if (LOC) | |
8294 | ANYOF_CLASS_SET(ret, ANYOF_NDIGIT); | |
8295 | else { | |
8296 | /* consecutive digits assumed */ | |
8297 | for (value = 0; value < '0'; value++) | |
8298 | ANYOF_BITMAP_SET(ret, value); | |
8299 | for (value = '9' + 1; value < 256; value++) | |
8300 | ANYOF_BITMAP_SET(ret, value); | |
8301 | } | |
c49a72a9 | 8302 | yesno = '!'; |
da7fcca4 | 8303 | what = POSIX_CC_UNI_NAME("Digit"); |
89836f1f | 8304 | break; |
f81125e2 JP |
8305 | case ANYOF_MAX: |
8306 | /* this is to handle \p and \P */ | |
8307 | break; | |
73b437c8 | 8308 | default: |
b45f050a | 8309 | vFAIL("Invalid [::] class"); |
73b437c8 | 8310 | break; |
b8c5462f | 8311 | } |
c49a72a9 NC |
8312 | if (what) { |
8313 | /* Strings such as "+utf8::isWord\n" */ | |
8314 | Perl_sv_catpvf(aTHX_ listsv, "%cutf8::Is%s\n", yesno, what); | |
8315 | } | |
b8c5462f | 8316 | if (LOC) |
936ed897 | 8317 | ANYOF_FLAGS(ret) |= ANYOF_CLASS; |
73b437c8 | 8318 | continue; |
a0d0e21e | 8319 | } |
ffc61ed2 JH |
8320 | } /* end of namedclass \blah */ |
8321 | ||
a0d0e21e | 8322 | if (range) { |
eb160463 | 8323 | if (prevvalue > (IV)value) /* b-a */ { |
d4c19fe8 AL |
8324 | const int w = RExC_parse - rangebegin; |
8325 | Simple_vFAIL4("Invalid [] range \"%*.*s\"", w, w, rangebegin); | |
3568d838 | 8326 | range = 0; /* not a valid range */ |
73b437c8 | 8327 | } |
a0d0e21e LW |
8328 | } |
8329 | else { | |
3568d838 | 8330 | prevvalue = value; /* save the beginning of the range */ |
830247a4 IZ |
8331 | if (*RExC_parse == '-' && RExC_parse+1 < RExC_end && |
8332 | RExC_parse[1] != ']') { | |
8333 | RExC_parse++; | |
ffc61ed2 JH |
8334 | |
8335 | /* a bad range like \w-, [:word:]- ? */ | |
8336 | if (namedclass > OOB_NAMEDCLASS) { | |
afd78fd5 | 8337 | if (ckWARN(WARN_REGEXP)) { |
d4c19fe8 | 8338 | const int w = |
afd78fd5 JH |
8339 | RExC_parse >= rangebegin ? |
8340 | RExC_parse - rangebegin : 0; | |
830247a4 | 8341 | vWARN4(RExC_parse, |
b45f050a | 8342 | "False [] range \"%*.*s\"", |
097eb12c | 8343 | w, w, rangebegin); |
afd78fd5 | 8344 | } |
73b437c8 | 8345 | if (!SIZE_ONLY) |
936ed897 | 8346 | ANYOF_BITMAP_SET(ret, '-'); |
73b437c8 | 8347 | } else |
ffc61ed2 JH |
8348 | range = 1; /* yeah, it's a range! */ |
8349 | continue; /* but do it the next time */ | |
a0d0e21e | 8350 | } |
a687059c | 8351 | } |
ffc61ed2 | 8352 | |
93733859 | 8353 | /* now is the next time */ |
07be1b83 | 8354 | /*stored += (value - prevvalue + 1);*/ |
ae5c130c | 8355 | if (!SIZE_ONLY) { |
3568d838 | 8356 | if (prevvalue < 256) { |
1df70142 | 8357 | const IV ceilvalue = value < 256 ? value : 255; |
3dab1dad | 8358 | IV i; |
3568d838 | 8359 | #ifdef EBCDIC |
1b2d223b JH |
8360 | /* In EBCDIC [\x89-\x91] should include |
8361 | * the \x8e but [i-j] should not. */ | |
8362 | if (literal_endpoint == 2 && | |
8363 | ((isLOWER(prevvalue) && isLOWER(ceilvalue)) || | |
8364 | (isUPPER(prevvalue) && isUPPER(ceilvalue)))) | |
ffc61ed2 | 8365 | { |
3568d838 JH |
8366 | if (isLOWER(prevvalue)) { |
8367 | for (i = prevvalue; i <= ceilvalue; i++) | |
2670d666 BC |
8368 | if (isLOWER(i) && !ANYOF_BITMAP_TEST(ret,i)) { |
8369 | stored++; | |
ffc61ed2 | 8370 | ANYOF_BITMAP_SET(ret, i); |
2670d666 | 8371 | } |
ffc61ed2 | 8372 | } else { |
3568d838 | 8373 | for (i = prevvalue; i <= ceilvalue; i++) |
2670d666 BC |
8374 | if (isUPPER(i) && !ANYOF_BITMAP_TEST(ret,i)) { |
8375 | stored++; | |
ffc61ed2 | 8376 | ANYOF_BITMAP_SET(ret, i); |
2670d666 | 8377 | } |
ffc61ed2 | 8378 | } |
8ada0baa | 8379 | } |
ffc61ed2 | 8380 | else |
8ada0baa | 8381 | #endif |
07be1b83 YO |
8382 | for (i = prevvalue; i <= ceilvalue; i++) { |
8383 | if (!ANYOF_BITMAP_TEST(ret,i)) { | |
8384 | stored++; | |
8385 | ANYOF_BITMAP_SET(ret, i); | |
8386 | } | |
8387 | } | |
3568d838 | 8388 | } |
a5961de5 | 8389 | if (value > 255 || UTF) { |
1df70142 AL |
8390 | const UV prevnatvalue = NATIVE_TO_UNI(prevvalue); |
8391 | const UV natvalue = NATIVE_TO_UNI(value); | |
07be1b83 | 8392 | stored+=2; /* can't optimize this class */ |
ffc61ed2 | 8393 | ANYOF_FLAGS(ret) |= ANYOF_UNICODE; |
b08decb7 | 8394 | if (prevnatvalue < natvalue) { /* what about > ? */ |
ffc61ed2 | 8395 | Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n", |
b08decb7 JH |
8396 | prevnatvalue, natvalue); |
8397 | } | |
8398 | else if (prevnatvalue == natvalue) { | |
8399 | Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", natvalue); | |
09091399 | 8400 | if (FOLD) { |
89ebb4a3 | 8401 | U8 foldbuf[UTF8_MAXBYTES_CASE+1]; |
254ba52a | 8402 | STRLEN foldlen; |
1df70142 | 8403 | const UV f = to_uni_fold(natvalue, foldbuf, &foldlen); |
254ba52a | 8404 | |
e294cc5d JH |
8405 | #ifdef EBCDIC /* RD t/uni/fold ff and 6b */ |
8406 | if (RExC_precomp[0] == ':' && | |
8407 | RExC_precomp[1] == '[' && | |
8408 | (f == 0xDF || f == 0x92)) { | |
8409 | f = NATIVE_TO_UNI(f); | |
8410 | } | |
8411 | #endif | |
c840d2a2 JH |
8412 | /* If folding and foldable and a single |
8413 | * character, insert also the folded version | |
8414 | * to the charclass. */ | |
9e55ce06 | 8415 | if (f != value) { |
e294cc5d JH |
8416 | #ifdef EBCDIC /* RD tunifold ligatures s,t fb05, fb06 */ |
8417 | if ((RExC_precomp[0] == ':' && | |
8418 | RExC_precomp[1] == '[' && | |
8419 | (f == 0xA2 && | |
8420 | (value == 0xFB05 || value == 0xFB06))) ? | |
8421 | foldlen == ((STRLEN)UNISKIP(f) - 1) : | |
8422 | foldlen == (STRLEN)UNISKIP(f) ) | |
8423 | #else | |
eb160463 | 8424 | if (foldlen == (STRLEN)UNISKIP(f)) |
e294cc5d | 8425 | #endif |
9e55ce06 JH |
8426 | Perl_sv_catpvf(aTHX_ listsv, |
8427 | "%04"UVxf"\n", f); | |
8428 | else { | |
8429 | /* Any multicharacter foldings | |
8430 | * require the following transform: | |
8431 | * [ABCDEF] -> (?:[ABCabcDEFd]|pq|rst) | |
8432 | * where E folds into "pq" and F folds | |
8433 | * into "rst", all other characters | |
8434 | * fold to single characters. We save | |
8435 | * away these multicharacter foldings, | |
8436 | * to be later saved as part of the | |
8437 | * additional "s" data. */ | |
8438 | SV *sv; | |
8439 | ||
8440 | if (!unicode_alternate) | |
8441 | unicode_alternate = newAV(); | |
740cce10 NC |
8442 | sv = newSVpvn_utf8((char*)foldbuf, foldlen, |
8443 | TRUE); | |
9e55ce06 JH |
8444 | av_push(unicode_alternate, sv); |
8445 | } | |
8446 | } | |
254ba52a | 8447 | |
60a8b682 JH |
8448 | /* If folding and the value is one of the Greek |
8449 | * sigmas insert a few more sigmas to make the | |
8450 | * folding rules of the sigmas to work right. | |
8451 | * Note that not all the possible combinations | |
8452 | * are handled here: some of them are handled | |
9e55ce06 JH |
8453 | * by the standard folding rules, and some of |
8454 | * them (literal or EXACTF cases) are handled | |
8455 | * during runtime in regexec.c:S_find_byclass(). */ | |
09091399 JH |
8456 | if (value == UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA) { |
8457 | Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", | |
eb050b28 | 8458 | (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA); |
09091399 | 8459 | Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", |
eb050b28 | 8460 | (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA); |
09091399 JH |
8461 | } |
8462 | else if (value == UNICODE_GREEK_CAPITAL_LETTER_SIGMA) | |
8463 | Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", | |
eb050b28 | 8464 | (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA); |
09091399 JH |
8465 | } |
8466 | } | |
ffc61ed2 | 8467 | } |
1b2d223b JH |
8468 | #ifdef EBCDIC |
8469 | literal_endpoint = 0; | |
8470 | #endif | |
8ada0baa | 8471 | } |
ffc61ed2 JH |
8472 | |
8473 | range = 0; /* this range (if it was one) is done now */ | |
a0d0e21e | 8474 | } |
ffc61ed2 | 8475 | |
936ed897 | 8476 | if (need_class) { |
4f66b38d | 8477 | ANYOF_FLAGS(ret) |= ANYOF_LARGE; |
936ed897 | 8478 | if (SIZE_ONLY) |
830247a4 | 8479 | RExC_size += ANYOF_CLASS_ADD_SKIP; |
936ed897 | 8480 | else |
830247a4 | 8481 | RExC_emit += ANYOF_CLASS_ADD_SKIP; |
936ed897 | 8482 | } |
ffc61ed2 | 8483 | |
7f6f358c YO |
8484 | |
8485 | if (SIZE_ONLY) | |
8486 | return ret; | |
8487 | /****** !SIZE_ONLY AFTER HERE *********/ | |
8488 | ||
3b57cd43 | 8489 | if( stored == 1 && (value < 128 || (value < 256 && !UTF)) |
7f6f358c YO |
8490 | && !( ANYOF_FLAGS(ret) & ( ANYOF_FLAGS_ALL ^ ANYOF_FOLD ) ) |
8491 | ) { | |
8492 | /* optimize single char class to an EXACT node | |
8493 | but *only* when its not a UTF/high char */ | |
07be1b83 YO |
8494 | const char * cur_parse= RExC_parse; |
8495 | RExC_emit = (regnode *)orig_emit; | |
8496 | RExC_parse = (char *)orig_parse; | |
7f6f358c YO |
8497 | ret = reg_node(pRExC_state, |
8498 | (U8)((ANYOF_FLAGS(ret) & ANYOF_FOLD) ? EXACTF : EXACT)); | |
07be1b83 | 8499 | RExC_parse = (char *)cur_parse; |
7f6f358c YO |
8500 | *STRING(ret)= (char)value; |
8501 | STR_LEN(ret)= 1; | |
8502 | RExC_emit += STR_SZ(1); | |
ef8d46e8 | 8503 | SvREFCNT_dec(listsv); |
7f6f358c YO |
8504 | return ret; |
8505 | } | |
ae5c130c | 8506 | /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */ |
7f6f358c | 8507 | if ( /* If the only flag is folding (plus possibly inversion). */ |
516a5887 JH |
8508 | ((ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) |
8509 | ) { | |
a0ed51b3 | 8510 | for (value = 0; value < 256; ++value) { |
936ed897 | 8511 | if (ANYOF_BITMAP_TEST(ret, value)) { |
eb160463 | 8512 | UV fold = PL_fold[value]; |
ffc61ed2 JH |
8513 | |
8514 | if (fold != value) | |
8515 | ANYOF_BITMAP_SET(ret, fold); | |
ae5c130c GS |
8516 | } |
8517 | } | |
936ed897 | 8518 | ANYOF_FLAGS(ret) &= ~ANYOF_FOLD; |
ae5c130c | 8519 | } |
ffc61ed2 | 8520 | |
ae5c130c | 8521 | /* optimize inverted simple patterns (e.g. [^a-z]) */ |
7f6f358c | 8522 | if (optimize_invert && |
ffc61ed2 JH |
8523 | /* If the only flag is inversion. */ |
8524 | (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) { | |
b8c5462f | 8525 | for (value = 0; value < ANYOF_BITMAP_SIZE; ++value) |
936ed897 | 8526 | ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL; |
1aa99e6b | 8527 | ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL; |
ae5c130c | 8528 | } |
7f6f358c | 8529 | { |
097eb12c | 8530 | AV * const av = newAV(); |
ffc61ed2 | 8531 | SV *rv; |
9e55ce06 | 8532 | /* The 0th element stores the character class description |
6a0407ee | 8533 | * in its textual form: used later (regexec.c:Perl_regclass_swash()) |
9e55ce06 JH |
8534 | * to initialize the appropriate swash (which gets stored in |
8535 | * the 1st element), and also useful for dumping the regnode. | |
8536 | * The 2nd element stores the multicharacter foldings, | |
6a0407ee | 8537 | * used later (regexec.c:S_reginclass()). */ |
ffc61ed2 JH |
8538 | av_store(av, 0, listsv); |
8539 | av_store(av, 1, NULL); | |
ad64d0ec NC |
8540 | av_store(av, 2, MUTABLE_SV(unicode_alternate)); |
8541 | rv = newRV_noinc(MUTABLE_SV(av)); | |
19860706 | 8542 | n = add_data(pRExC_state, 1, "s"); |
f8fc2ecf | 8543 | RExC_rxi->data->data[n] = (void*)rv; |
ffc61ed2 | 8544 | ARG_SET(ret, n); |
a0ed51b3 | 8545 | } |
a0ed51b3 LW |
8546 | return ret; |
8547 | } | |
89836f1f YO |
8548 | #undef _C_C_T_ |
8549 | ||
a0ed51b3 | 8550 | |
bcdf7404 YO |
8551 | /* reg_skipcomment() |
8552 | ||
8553 | Absorbs an /x style # comments from the input stream. | |
8554 | Returns true if there is more text remaining in the stream. | |
8555 | Will set the REG_SEEN_RUN_ON_COMMENT flag if the comment | |
8556 | terminates the pattern without including a newline. | |
8557 | ||
8558 | Note its the callers responsibility to ensure that we are | |
8559 | actually in /x mode | |
8560 | ||
8561 | */ | |
8562 | ||
8563 | STATIC bool | |
8564 | S_reg_skipcomment(pTHX_ RExC_state_t *pRExC_state) | |
8565 | { | |
8566 | bool ended = 0; | |
7918f24d NC |
8567 | |
8568 | PERL_ARGS_ASSERT_REG_SKIPCOMMENT; | |
8569 | ||
bcdf7404 YO |
8570 | while (RExC_parse < RExC_end) |
8571 | if (*RExC_parse++ == '\n') { | |
8572 | ended = 1; | |
8573 | break; | |
8574 | } | |
8575 | if (!ended) { | |
8576 | /* we ran off the end of the pattern without ending | |
8577 | the comment, so we have to add an \n when wrapping */ | |
8578 | RExC_seen |= REG_SEEN_RUN_ON_COMMENT; | |
8579 | return 0; | |
8580 | } else | |
8581 | return 1; | |
8582 | } | |
8583 | ||
8584 | /* nextchar() | |
8585 | ||
8586 | Advance that parse position, and optionall absorbs | |
8587 | "whitespace" from the inputstream. | |
8588 | ||
8589 | Without /x "whitespace" means (?#...) style comments only, | |
8590 | with /x this means (?#...) and # comments and whitespace proper. | |
8591 | ||
8592 | Returns the RExC_parse point from BEFORE the scan occurs. | |
8593 | ||
8594 | This is the /x friendly way of saying RExC_parse++. | |
8595 | */ | |
8596 | ||
76e3520e | 8597 | STATIC char* |
830247a4 | 8598 | S_nextchar(pTHX_ RExC_state_t *pRExC_state) |
a0d0e21e | 8599 | { |
097eb12c | 8600 | char* const retval = RExC_parse++; |
a0d0e21e | 8601 | |
7918f24d NC |
8602 | PERL_ARGS_ASSERT_NEXTCHAR; |
8603 | ||
4633a7c4 | 8604 | for (;;) { |
830247a4 IZ |
8605 | if (*RExC_parse == '(' && RExC_parse[1] == '?' && |
8606 | RExC_parse[2] == '#') { | |
e994fd66 AE |
8607 | while (*RExC_parse != ')') { |
8608 | if (RExC_parse == RExC_end) | |
8609 | FAIL("Sequence (?#... not terminated"); | |
830247a4 | 8610 | RExC_parse++; |
e994fd66 | 8611 | } |
830247a4 | 8612 | RExC_parse++; |
4633a7c4 LW |
8613 | continue; |
8614 | } | |
bbe252da | 8615 | if (RExC_flags & RXf_PMf_EXTENDED) { |
830247a4 IZ |
8616 | if (isSPACE(*RExC_parse)) { |
8617 | RExC_parse++; | |
748a9306 LW |
8618 | continue; |
8619 | } | |
830247a4 | 8620 | else if (*RExC_parse == '#') { |
bcdf7404 YO |
8621 | if ( reg_skipcomment( pRExC_state ) ) |
8622 | continue; | |
748a9306 | 8623 | } |
748a9306 | 8624 | } |
4633a7c4 | 8625 | return retval; |
a0d0e21e | 8626 | } |
a687059c LW |
8627 | } |
8628 | ||
8629 | /* | |
c277df42 | 8630 | - reg_node - emit a node |
a0d0e21e | 8631 | */ |
76e3520e | 8632 | STATIC regnode * /* Location. */ |
830247a4 | 8633 | S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op) |
a687059c | 8634 | { |
97aff369 | 8635 | dVAR; |
c277df42 | 8636 | register regnode *ptr; |
504618e9 | 8637 | regnode * const ret = RExC_emit; |
07be1b83 | 8638 | GET_RE_DEBUG_FLAGS_DECL; |
a687059c | 8639 | |
7918f24d NC |
8640 | PERL_ARGS_ASSERT_REG_NODE; |
8641 | ||
c277df42 | 8642 | if (SIZE_ONLY) { |
830247a4 IZ |
8643 | SIZE_ALIGN(RExC_size); |
8644 | RExC_size += 1; | |
a0d0e21e LW |
8645 | return(ret); |
8646 | } | |
3b57cd43 YO |
8647 | if (RExC_emit >= RExC_emit_bound) |
8648 | Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d", op); | |
8649 | ||
c277df42 | 8650 | NODE_ALIGN_FILL(ret); |
a0d0e21e | 8651 | ptr = ret; |
c277df42 | 8652 | FILL_ADVANCE_NODE(ptr, op); |
7122b237 | 8653 | #ifdef RE_TRACK_PATTERN_OFFSETS |
fac92740 | 8654 | if (RExC_offsets) { /* MJD */ |
07be1b83 | 8655 | MJD_OFFSET_DEBUG(("%s:%d: (op %s) %s %"UVuf" (len %"UVuf") (max %"UVuf").\n", |
fac92740 | 8656 | "reg_node", __LINE__, |
13d6edb4 | 8657 | PL_reg_name[op], |
07be1b83 YO |
8658 | (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] |
8659 | ? "Overwriting end of array!\n" : "OK", | |
8660 | (UV)(RExC_emit - RExC_emit_start), | |
8661 | (UV)(RExC_parse - RExC_start), | |
8662 | (UV)RExC_offsets[0])); | |
ccb2c380 | 8663 | Set_Node_Offset(RExC_emit, RExC_parse + (op == END)); |
fac92740 | 8664 | } |
7122b237 | 8665 | #endif |
830247a4 | 8666 | RExC_emit = ptr; |
a0d0e21e | 8667 | return(ret); |
a687059c LW |
8668 | } |
8669 | ||
8670 | /* | |
a0d0e21e LW |
8671 | - reganode - emit a node with an argument |
8672 | */ | |
76e3520e | 8673 | STATIC regnode * /* Location. */ |
830247a4 | 8674 | S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg) |
fe14fcc3 | 8675 | { |
97aff369 | 8676 | dVAR; |
c277df42 | 8677 | register regnode *ptr; |
504618e9 | 8678 | regnode * const ret = RExC_emit; |
07be1b83 | 8679 | GET_RE_DEBUG_FLAGS_DECL; |
fe14fcc3 | 8680 | |
7918f24d NC |
8681 | PERL_ARGS_ASSERT_REGANODE; |
8682 | ||
c277df42 | 8683 | if (SIZE_ONLY) { |
830247a4 IZ |
8684 | SIZE_ALIGN(RExC_size); |
8685 | RExC_size += 2; | |
6bda09f9 YO |
8686 | /* |
8687 | We can't do this: | |
8688 | ||
8689 | assert(2==regarglen[op]+1); | |
8690 | ||
8691 | Anything larger than this has to allocate the extra amount. | |
8692 | If we changed this to be: | |
8693 | ||
8694 | RExC_size += (1 + regarglen[op]); | |
8695 | ||
8696 | then it wouldn't matter. Its not clear what side effect | |
8697 | might come from that so its not done so far. | |
8698 | -- dmq | |
8699 | */ | |
a0d0e21e LW |
8700 | return(ret); |
8701 | } | |
3b57cd43 YO |
8702 | if (RExC_emit >= RExC_emit_bound) |
8703 | Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d", op); | |
8704 | ||
c277df42 | 8705 | NODE_ALIGN_FILL(ret); |
a0d0e21e | 8706 | ptr = ret; |
c277df42 | 8707 | FILL_ADVANCE_NODE_ARG(ptr, op, arg); |
7122b237 | 8708 | #ifdef RE_TRACK_PATTERN_OFFSETS |
fac92740 | 8709 | if (RExC_offsets) { /* MJD */ |
07be1b83 | 8710 | MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n", |
fac92740 | 8711 | "reganode", |
ccb2c380 | 8712 | __LINE__, |
13d6edb4 | 8713 | PL_reg_name[op], |
07be1b83 | 8714 | (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] ? |
fac92740 | 8715 | "Overwriting end of array!\n" : "OK", |
07be1b83 YO |
8716 | (UV)(RExC_emit - RExC_emit_start), |
8717 | (UV)(RExC_parse - RExC_start), | |
8718 | (UV)RExC_offsets[0])); | |
ccb2c380 | 8719 | Set_Cur_Node_Offset; |
fac92740 | 8720 | } |
7122b237 | 8721 | #endif |
830247a4 | 8722 | RExC_emit = ptr; |
a0d0e21e | 8723 | return(ret); |
fe14fcc3 LW |
8724 | } |
8725 | ||
8726 | /* | |
cd439c50 | 8727 | - reguni - emit (if appropriate) a Unicode character |
a0ed51b3 | 8728 | */ |
71207a34 AL |
8729 | STATIC STRLEN |
8730 | S_reguni(pTHX_ const RExC_state_t *pRExC_state, UV uv, char* s) | |
a0ed51b3 | 8731 | { |
97aff369 | 8732 | dVAR; |
7918f24d NC |
8733 | |
8734 | PERL_ARGS_ASSERT_REGUNI; | |
8735 | ||
71207a34 | 8736 | return SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s); |
a0ed51b3 LW |
8737 | } |
8738 | ||
8739 | /* | |
a0d0e21e LW |
8740 | - reginsert - insert an operator in front of already-emitted operand |
8741 | * | |
8742 | * Means relocating the operand. | |
8743 | */ | |
76e3520e | 8744 | STATIC void |
6bda09f9 | 8745 | S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd, U32 depth) |
a687059c | 8746 | { |
97aff369 | 8747 | dVAR; |
c277df42 IZ |
8748 | register regnode *src; |
8749 | register regnode *dst; | |
8750 | register regnode *place; | |
504618e9 | 8751 | const int offset = regarglen[(U8)op]; |
6bda09f9 | 8752 | const int size = NODE_STEP_REGNODE + offset; |
07be1b83 | 8753 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
8754 | |
8755 | PERL_ARGS_ASSERT_REGINSERT; | |
def51078 | 8756 | PERL_UNUSED_ARG(depth); |
22c35a8c | 8757 | /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */ |
13d6edb4 | 8758 | DEBUG_PARSE_FMT("inst"," - %s",PL_reg_name[op]); |
c277df42 | 8759 | if (SIZE_ONLY) { |
6bda09f9 | 8760 | RExC_size += size; |
a0d0e21e LW |
8761 | return; |
8762 | } | |
a687059c | 8763 | |
830247a4 | 8764 | src = RExC_emit; |
6bda09f9 | 8765 | RExC_emit += size; |
830247a4 | 8766 | dst = RExC_emit; |
40d049e4 | 8767 | if (RExC_open_parens) { |
6bda09f9 | 8768 | int paren; |
3b57cd43 | 8769 | /*DEBUG_PARSE_FMT("inst"," - %"IVdf, (IV)RExC_npar);*/ |
6bda09f9 | 8770 | for ( paren=0 ; paren < RExC_npar ; paren++ ) { |
40d049e4 | 8771 | if ( RExC_open_parens[paren] >= opnd ) { |
3b57cd43 | 8772 | /*DEBUG_PARSE_FMT("open"," - %d",size);*/ |
40d049e4 YO |
8773 | RExC_open_parens[paren] += size; |
8774 | } else { | |
3b57cd43 | 8775 | /*DEBUG_PARSE_FMT("open"," - %s","ok");*/ |
40d049e4 YO |
8776 | } |
8777 | if ( RExC_close_parens[paren] >= opnd ) { | |
3b57cd43 | 8778 | /*DEBUG_PARSE_FMT("close"," - %d",size);*/ |
40d049e4 YO |
8779 | RExC_close_parens[paren] += size; |
8780 | } else { | |
3b57cd43 | 8781 | /*DEBUG_PARSE_FMT("close"," - %s","ok");*/ |
40d049e4 YO |
8782 | } |
8783 | } | |
6bda09f9 | 8784 | } |
40d049e4 | 8785 | |
fac92740 | 8786 | while (src > opnd) { |
c277df42 | 8787 | StructCopy(--src, --dst, regnode); |
7122b237 | 8788 | #ifdef RE_TRACK_PATTERN_OFFSETS |
fac92740 | 8789 | if (RExC_offsets) { /* MJD 20010112 */ |
07be1b83 | 8790 | MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %"UVuf" -> %"UVuf" (max %"UVuf").\n", |
fac92740 | 8791 | "reg_insert", |
ccb2c380 | 8792 | __LINE__, |
13d6edb4 | 8793 | PL_reg_name[op], |
07be1b83 YO |
8794 | (UV)(dst - RExC_emit_start) > RExC_offsets[0] |
8795 | ? "Overwriting end of array!\n" : "OK", | |
8796 | (UV)(src - RExC_emit_start), | |
8797 | (UV)(dst - RExC_emit_start), | |
8798 | (UV)RExC_offsets[0])); | |
ccb2c380 MP |
8799 | Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src)); |
8800 | Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src)); | |
fac92740 | 8801 | } |
7122b237 | 8802 | #endif |
fac92740 MJD |
8803 | } |
8804 | ||
a0d0e21e LW |
8805 | |
8806 | place = opnd; /* Op node, where operand used to be. */ | |
7122b237 | 8807 | #ifdef RE_TRACK_PATTERN_OFFSETS |
fac92740 | 8808 | if (RExC_offsets) { /* MJD */ |
07be1b83 | 8809 | MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n", |
fac92740 | 8810 | "reginsert", |
ccb2c380 | 8811 | __LINE__, |
13d6edb4 | 8812 | PL_reg_name[op], |
07be1b83 | 8813 | (UV)(place - RExC_emit_start) > RExC_offsets[0] |
fac92740 | 8814 | ? "Overwriting end of array!\n" : "OK", |
07be1b83 YO |
8815 | (UV)(place - RExC_emit_start), |
8816 | (UV)(RExC_parse - RExC_start), | |
786e8c11 | 8817 | (UV)RExC_offsets[0])); |
ccb2c380 | 8818 | Set_Node_Offset(place, RExC_parse); |
45948336 | 8819 | Set_Node_Length(place, 1); |
fac92740 | 8820 | } |
7122b237 | 8821 | #endif |
c277df42 IZ |
8822 | src = NEXTOPER(place); |
8823 | FILL_ADVANCE_NODE(place, op); | |
8824 | Zero(src, offset, regnode); | |
a687059c LW |
8825 | } |
8826 | ||
8827 | /* | |
c277df42 | 8828 | - regtail - set the next-pointer at the end of a node chain of p to val. |
3dab1dad | 8829 | - SEE ALSO: regtail_study |
a0d0e21e | 8830 | */ |
097eb12c | 8831 | /* TODO: All three parms should be const */ |
76e3520e | 8832 | STATIC void |
3dab1dad | 8833 | S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth) |
a687059c | 8834 | { |
97aff369 | 8835 | dVAR; |
c277df42 | 8836 | register regnode *scan; |
72f13be8 | 8837 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
8838 | |
8839 | PERL_ARGS_ASSERT_REGTAIL; | |
f9049ba1 SP |
8840 | #ifndef DEBUGGING |
8841 | PERL_UNUSED_ARG(depth); | |
8842 | #endif | |
a0d0e21e | 8843 | |
c277df42 | 8844 | if (SIZE_ONLY) |
a0d0e21e LW |
8845 | return; |
8846 | ||
8847 | /* Find last node. */ | |
8848 | scan = p; | |
8849 | for (;;) { | |
504618e9 | 8850 | regnode * const temp = regnext(scan); |
3dab1dad YO |
8851 | DEBUG_PARSE_r({ |
8852 | SV * const mysv=sv_newmortal(); | |
8853 | DEBUG_PARSE_MSG((scan==p ? "tail" : "")); | |
8854 | regprop(RExC_rx, mysv, scan); | |
eaf3ca90 YO |
8855 | PerlIO_printf(Perl_debug_log, "~ %s (%d) %s %s\n", |
8856 | SvPV_nolen_const(mysv), REG_NODE_NUM(scan), | |
8857 | (temp == NULL ? "->" : ""), | |
13d6edb4 | 8858 | (temp == NULL ? PL_reg_name[OP(val)] : "") |
eaf3ca90 | 8859 | ); |
3dab1dad YO |
8860 | }); |
8861 | if (temp == NULL) | |
8862 | break; | |
8863 | scan = temp; | |
8864 | } | |
8865 | ||
8866 | if (reg_off_by_arg[OP(scan)]) { | |
8867 | ARG_SET(scan, val - scan); | |
8868 | } | |
8869 | else { | |
8870 | NEXT_OFF(scan) = val - scan; | |
8871 | } | |
8872 | } | |
8873 | ||
07be1b83 | 8874 | #ifdef DEBUGGING |
3dab1dad YO |
8875 | /* |
8876 | - regtail_study - set the next-pointer at the end of a node chain of p to val. | |
8877 | - Look for optimizable sequences at the same time. | |
8878 | - currently only looks for EXACT chains. | |
07be1b83 YO |
8879 | |
8880 | This is expermental code. The idea is to use this routine to perform | |
8881 | in place optimizations on branches and groups as they are constructed, | |
8882 | with the long term intention of removing optimization from study_chunk so | |
8883 | that it is purely analytical. | |
8884 | ||
8885 | Currently only used when in DEBUG mode. The macro REGTAIL_STUDY() is used | |
8886 | to control which is which. | |
8887 | ||
3dab1dad YO |
8888 | */ |
8889 | /* TODO: All four parms should be const */ | |
07be1b83 | 8890 | |
3dab1dad YO |
8891 | STATIC U8 |
8892 | S_regtail_study(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth) | |
8893 | { | |
8894 | dVAR; | |
8895 | register regnode *scan; | |
07be1b83 YO |
8896 | U8 exact = PSEUDO; |
8897 | #ifdef EXPERIMENTAL_INPLACESCAN | |
8898 | I32 min = 0; | |
8899 | #endif | |
3dab1dad YO |
8900 | GET_RE_DEBUG_FLAGS_DECL; |
8901 | ||
7918f24d NC |
8902 | PERL_ARGS_ASSERT_REGTAIL_STUDY; |
8903 | ||
07be1b83 | 8904 | |
3dab1dad YO |
8905 | if (SIZE_ONLY) |
8906 | return exact; | |
8907 | ||
8908 | /* Find last node. */ | |
8909 | ||
8910 | scan = p; | |
8911 | for (;;) { | |
8912 | regnode * const temp = regnext(scan); | |
07be1b83 YO |
8913 | #ifdef EXPERIMENTAL_INPLACESCAN |
8914 | if (PL_regkind[OP(scan)] == EXACT) | |
8915 | if (join_exact(pRExC_state,scan,&min,1,val,depth+1)) | |
8916 | return EXACT; | |
8917 | #endif | |
3dab1dad YO |
8918 | if ( exact ) { |
8919 | switch (OP(scan)) { | |
8920 | case EXACT: | |
8921 | case EXACTF: | |
8922 | case EXACTFL: | |
8923 | if( exact == PSEUDO ) | |
8924 | exact= OP(scan); | |
07be1b83 YO |
8925 | else if ( exact != OP(scan) ) |
8926 | exact= 0; | |
3dab1dad YO |
8927 | case NOTHING: |
8928 | break; | |
8929 | default: | |
8930 | exact= 0; | |
8931 | } | |
8932 | } | |
8933 | DEBUG_PARSE_r({ | |
8934 | SV * const mysv=sv_newmortal(); | |
8935 | DEBUG_PARSE_MSG((scan==p ? "tsdy" : "")); | |
8936 | regprop(RExC_rx, mysv, scan); | |
eaf3ca90 | 8937 | PerlIO_printf(Perl_debug_log, "~ %s (%d) -> %s\n", |
3dab1dad | 8938 | SvPV_nolen_const(mysv), |
eaf3ca90 | 8939 | REG_NODE_NUM(scan), |
13d6edb4 | 8940 | PL_reg_name[exact]); |
3dab1dad | 8941 | }); |
a0d0e21e LW |
8942 | if (temp == NULL) |
8943 | break; | |
8944 | scan = temp; | |
8945 | } | |
07be1b83 YO |
8946 | DEBUG_PARSE_r({ |
8947 | SV * const mysv_val=sv_newmortal(); | |
8948 | DEBUG_PARSE_MSG(""); | |
8949 | regprop(RExC_rx, mysv_val, val); | |
70685ca0 JH |
8950 | PerlIO_printf(Perl_debug_log, "~ attach to %s (%"IVdf") offset to %"IVdf"\n", |
8951 | SvPV_nolen_const(mysv_val), | |
8952 | (IV)REG_NODE_NUM(val), | |
8953 | (IV)(val - scan) | |
07be1b83 YO |
8954 | ); |
8955 | }); | |
c277df42 IZ |
8956 | if (reg_off_by_arg[OP(scan)]) { |
8957 | ARG_SET(scan, val - scan); | |
a0ed51b3 LW |
8958 | } |
8959 | else { | |
c277df42 IZ |
8960 | NEXT_OFF(scan) = val - scan; |
8961 | } | |
3dab1dad YO |
8962 | |
8963 | return exact; | |
a687059c | 8964 | } |
07be1b83 | 8965 | #endif |
a687059c LW |
8966 | |
8967 | /* | |
a687059c LW |
8968 | - regcurly - a little FSA that accepts {\d+,?\d*} |
8969 | */ | |
d3b0eb15 | 8970 | #ifndef PERL_IN_XSUB_RE |
ff3f963a KW |
8971 | I32 |
8972 | Perl_regcurly(register const char *s) | |
a687059c | 8973 | { |
7918f24d NC |
8974 | PERL_ARGS_ASSERT_REGCURLY; |
8975 | ||
a687059c LW |
8976 | if (*s++ != '{') |
8977 | return FALSE; | |
f0fcb552 | 8978 | if (!isDIGIT(*s)) |
a687059c | 8979 | return FALSE; |
f0fcb552 | 8980 | while (isDIGIT(*s)) |
a687059c LW |
8981 | s++; |
8982 | if (*s == ',') | |
8983 | s++; | |
f0fcb552 | 8984 | while (isDIGIT(*s)) |
a687059c LW |
8985 | s++; |
8986 | if (*s != '}') | |
8987 | return FALSE; | |
8988 | return TRUE; | |
8989 | } | |
d3b0eb15 | 8990 | #endif |
a687059c LW |
8991 | |
8992 | /* | |
fd181c75 | 8993 | - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form |
a687059c | 8994 | */ |
f7819f85 | 8995 | #ifdef DEBUGGING |
c33269f7 | 8996 | static void |
7918f24d NC |
8997 | S_regdump_extflags(pTHX_ const char *lead, const U32 flags) |
8998 | { | |
f7819f85 A |
8999 | int bit; |
9000 | int set=0; | |
7918f24d | 9001 | |
f7819f85 A |
9002 | for (bit=0; bit<32; bit++) { |
9003 | if (flags & (1<<bit)) { | |
9004 | if (!set++ && lead) | |
9005 | PerlIO_printf(Perl_debug_log, "%s",lead); | |
9006 | PerlIO_printf(Perl_debug_log, "%s ",PL_reg_extflags_name[bit]); | |
9007 | } | |
9008 | } | |
9009 | if (lead) { | |
9010 | if (set) | |
9011 | PerlIO_printf(Perl_debug_log, "\n"); | |
9012 | else | |
9013 | PerlIO_printf(Perl_debug_log, "%s[none-set]\n",lead); | |
9014 | } | |
9015 | } | |
9016 | #endif | |
9017 | ||
a687059c | 9018 | void |
097eb12c | 9019 | Perl_regdump(pTHX_ const regexp *r) |
a687059c | 9020 | { |
35ff7856 | 9021 | #ifdef DEBUGGING |
97aff369 | 9022 | dVAR; |
c445ea15 | 9023 | SV * const sv = sv_newmortal(); |
ab3bbdeb | 9024 | SV *dsv= sv_newmortal(); |
f8fc2ecf | 9025 | RXi_GET_DECL(r,ri); |
f7819f85 | 9026 | GET_RE_DEBUG_FLAGS_DECL; |
a687059c | 9027 | |
7918f24d NC |
9028 | PERL_ARGS_ASSERT_REGDUMP; |
9029 | ||
f8fc2ecf | 9030 | (void)dumpuntil(r, ri->program, ri->program + 1, NULL, NULL, sv, 0, 0); |
a0d0e21e LW |
9031 | |
9032 | /* Header fields of interest. */ | |
ab3bbdeb YO |
9033 | if (r->anchored_substr) { |
9034 | RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->anchored_substr), | |
9035 | RE_SV_DUMPLEN(r->anchored_substr), 30); | |
7b0972df | 9036 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb YO |
9037 | "anchored %s%s at %"IVdf" ", |
9038 | s, RE_SV_TAIL(r->anchored_substr), | |
7b0972df | 9039 | (IV)r->anchored_offset); |
ab3bbdeb YO |
9040 | } else if (r->anchored_utf8) { |
9041 | RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->anchored_utf8), | |
9042 | RE_SV_DUMPLEN(r->anchored_utf8), 30); | |
33b8afdf | 9043 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb YO |
9044 | "anchored utf8 %s%s at %"IVdf" ", |
9045 | s, RE_SV_TAIL(r->anchored_utf8), | |
33b8afdf | 9046 | (IV)r->anchored_offset); |
ab3bbdeb YO |
9047 | } |
9048 | if (r->float_substr) { | |
9049 | RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->float_substr), | |
9050 | RE_SV_DUMPLEN(r->float_substr), 30); | |
7b0972df | 9051 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb YO |
9052 | "floating %s%s at %"IVdf"..%"UVuf" ", |
9053 | s, RE_SV_TAIL(r->float_substr), | |
7b0972df | 9054 | (IV)r->float_min_offset, (UV)r->float_max_offset); |
ab3bbdeb YO |
9055 | } else if (r->float_utf8) { |
9056 | RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->float_utf8), | |
9057 | RE_SV_DUMPLEN(r->float_utf8), 30); | |
33b8afdf | 9058 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb YO |
9059 | "floating utf8 %s%s at %"IVdf"..%"UVuf" ", |
9060 | s, RE_SV_TAIL(r->float_utf8), | |
33b8afdf | 9061 | (IV)r->float_min_offset, (UV)r->float_max_offset); |
ab3bbdeb | 9062 | } |
33b8afdf | 9063 | if (r->check_substr || r->check_utf8) |
b81d288d | 9064 | PerlIO_printf(Perl_debug_log, |
10edeb5d JH |
9065 | (const char *) |
9066 | (r->check_substr == r->float_substr | |
9067 | && r->check_utf8 == r->float_utf8 | |
9068 | ? "(checking floating" : "(checking anchored")); | |
bbe252da | 9069 | if (r->extflags & RXf_NOSCAN) |
c277df42 | 9070 | PerlIO_printf(Perl_debug_log, " noscan"); |
bbe252da | 9071 | if (r->extflags & RXf_CHECK_ALL) |
c277df42 | 9072 | PerlIO_printf(Perl_debug_log, " isall"); |
33b8afdf | 9073 | if (r->check_substr || r->check_utf8) |
c277df42 IZ |
9074 | PerlIO_printf(Perl_debug_log, ") "); |
9075 | ||
f8fc2ecf YO |
9076 | if (ri->regstclass) { |
9077 | regprop(r, sv, ri->regstclass); | |
1de06328 | 9078 | PerlIO_printf(Perl_debug_log, "stclass %s ", SvPVX_const(sv)); |
46fc3d4c | 9079 | } |
bbe252da | 9080 | if (r->extflags & RXf_ANCH) { |
774d564b | 9081 | PerlIO_printf(Perl_debug_log, "anchored"); |
bbe252da | 9082 | if (r->extflags & RXf_ANCH_BOL) |
774d564b | 9083 | PerlIO_printf(Perl_debug_log, "(BOL)"); |
bbe252da | 9084 | if (r->extflags & RXf_ANCH_MBOL) |
c277df42 | 9085 | PerlIO_printf(Perl_debug_log, "(MBOL)"); |
bbe252da | 9086 | if (r->extflags & RXf_ANCH_SBOL) |
cad2e5aa | 9087 | PerlIO_printf(Perl_debug_log, "(SBOL)"); |
bbe252da | 9088 | if (r->extflags & RXf_ANCH_GPOS) |
774d564b | 9089 | PerlIO_printf(Perl_debug_log, "(GPOS)"); |
9090 | PerlIO_putc(Perl_debug_log, ' '); | |
9091 | } | |
bbe252da | 9092 | if (r->extflags & RXf_GPOS_SEEN) |
70685ca0 | 9093 | PerlIO_printf(Perl_debug_log, "GPOS:%"UVuf" ", (UV)r->gofs); |
bbe252da | 9094 | if (r->intflags & PREGf_SKIP) |
760ac839 | 9095 | PerlIO_printf(Perl_debug_log, "plus "); |
bbe252da | 9096 | if (r->intflags & PREGf_IMPLICIT) |
760ac839 | 9097 | PerlIO_printf(Perl_debug_log, "implicit "); |
70685ca0 | 9098 | PerlIO_printf(Perl_debug_log, "minlen %"IVdf" ", (IV)r->minlen); |
bbe252da | 9099 | if (r->extflags & RXf_EVAL_SEEN) |
ce862d02 | 9100 | PerlIO_printf(Perl_debug_log, "with eval "); |
760ac839 | 9101 | PerlIO_printf(Perl_debug_log, "\n"); |
f7819f85 | 9102 | DEBUG_FLAGS_r(regdump_extflags("r->extflags: ",r->extflags)); |
65e66c80 | 9103 | #else |
7918f24d | 9104 | PERL_ARGS_ASSERT_REGDUMP; |
96a5add6 | 9105 | PERL_UNUSED_CONTEXT; |
65e66c80 | 9106 | PERL_UNUSED_ARG(r); |
17c3b450 | 9107 | #endif /* DEBUGGING */ |
a687059c LW |
9108 | } |
9109 | ||
9110 | /* | |
a0d0e21e LW |
9111 | - regprop - printable representation of opcode |
9112 | */ | |
3339dfd8 YO |
9113 | #define EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags) \ |
9114 | STMT_START { \ | |
9115 | if (do_sep) { \ | |
9116 | Perl_sv_catpvf(aTHX_ sv,"%s][%s",PL_colors[1],PL_colors[0]); \ | |
9117 | if (flags & ANYOF_INVERT) \ | |
9118 | /*make sure the invert info is in each */ \ | |
9119 | sv_catpvs(sv, "^"); \ | |
9120 | do_sep = 0; \ | |
9121 | } \ | |
9122 | } STMT_END | |
9123 | ||
46fc3d4c | 9124 | void |
32fc9b6a | 9125 | Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o) |
a687059c | 9126 | { |
35ff7856 | 9127 | #ifdef DEBUGGING |
97aff369 | 9128 | dVAR; |
9b155405 | 9129 | register int k; |
f8fc2ecf | 9130 | RXi_GET_DECL(prog,progi); |
1de06328 | 9131 | GET_RE_DEBUG_FLAGS_DECL; |
f8fc2ecf | 9132 | |
7918f24d | 9133 | PERL_ARGS_ASSERT_REGPROP; |
a0d0e21e | 9134 | |
76f68e9b | 9135 | sv_setpvs(sv, ""); |
8aa23a47 | 9136 | |
03363afd | 9137 | if (OP(o) > REGNODE_MAX) /* regnode.type is unsigned */ |
830247a4 IZ |
9138 | /* It would be nice to FAIL() here, but this may be called from |
9139 | regexec.c, and it would be hard to supply pRExC_state. */ | |
a5ca303d | 9140 | Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(o), (int)REGNODE_MAX); |
13d6edb4 | 9141 | sv_catpv(sv, PL_reg_name[OP(o)]); /* Take off const! */ |
9b155405 | 9142 | |
3dab1dad | 9143 | k = PL_regkind[OP(o)]; |
9b155405 | 9144 | |
2a782b5b | 9145 | if (k == EXACT) { |
f92a2122 | 9146 | sv_catpvs(sv, " "); |
ab3bbdeb YO |
9147 | /* Using is_utf8_string() (via PERL_PV_UNI_DETECT) |
9148 | * is a crude hack but it may be the best for now since | |
9149 | * we have no flag "this EXACTish node was UTF-8" | |
9150 | * --jhi */ | |
f92a2122 NC |
9151 | pv_pretty(sv, STRING(o), STR_LEN(o), 60, PL_colors[0], PL_colors[1], |
9152 | PERL_PV_ESCAPE_UNI_DETECT | | |
9153 | PERL_PV_PRETTY_ELLIPSES | | |
9154 | PERL_PV_PRETTY_LTGT | | |
9155 | PERL_PV_PRETTY_NOCLEAR | |
9156 | ); | |
bb263b4e | 9157 | } else if (k == TRIE) { |
3dab1dad | 9158 | /* print the details of the trie in dumpuntil instead, as |
f8fc2ecf | 9159 | * progi->data isn't available here */ |
1de06328 | 9160 | const char op = OP(o); |
647f639f | 9161 | const U32 n = ARG(o); |
1de06328 | 9162 | const reg_ac_data * const ac = IS_TRIE_AC(op) ? |
f8fc2ecf | 9163 | (reg_ac_data *)progi->data->data[n] : |
1de06328 | 9164 | NULL; |
3251b653 NC |
9165 | const reg_trie_data * const trie |
9166 | = (reg_trie_data*)progi->data->data[!IS_TRIE_AC(op) ? n : ac->trie]; | |
1de06328 | 9167 | |
13d6edb4 | 9168 | Perl_sv_catpvf(aTHX_ sv, "-%s",PL_reg_name[o->flags]); |
1de06328 YO |
9169 | DEBUG_TRIE_COMPILE_r( |
9170 | Perl_sv_catpvf(aTHX_ sv, | |
9171 | "<S:%"UVuf"/%"IVdf" W:%"UVuf" L:%"UVuf"/%"UVuf" C:%"UVuf"/%"UVuf">", | |
9172 | (UV)trie->startstate, | |
1e2e3d02 | 9173 | (IV)trie->statecount-1, /* -1 because of the unused 0 element */ |
1de06328 YO |
9174 | (UV)trie->wordcount, |
9175 | (UV)trie->minlen, | |
9176 | (UV)trie->maxlen, | |
9177 | (UV)TRIE_CHARCOUNT(trie), | |
9178 | (UV)trie->uniquecharcount | |
9179 | ) | |
9180 | ); | |
9181 | if ( IS_ANYOF_TRIE(op) || trie->bitmap ) { | |
9182 | int i; | |
9183 | int rangestart = -1; | |
f46cb337 | 9184 | U8* bitmap = IS_ANYOF_TRIE(op) ? (U8*)ANYOF_BITMAP(o) : (U8*)TRIE_BITMAP(trie); |
f3a2811a | 9185 | sv_catpvs(sv, "["); |
1de06328 YO |
9186 | for (i = 0; i <= 256; i++) { |
9187 | if (i < 256 && BITMAP_TEST(bitmap,i)) { | |
9188 | if (rangestart == -1) | |
9189 | rangestart = i; | |
9190 | } else if (rangestart != -1) { | |
9191 | if (i <= rangestart + 3) | |
9192 | for (; rangestart < i; rangestart++) | |
9193 | put_byte(sv, rangestart); | |
9194 | else { | |
9195 | put_byte(sv, rangestart); | |
9196 | sv_catpvs(sv, "-"); | |
9197 | put_byte(sv, i - 1); | |
9198 | } | |
9199 | rangestart = -1; | |
9200 | } | |
9201 | } | |
f3a2811a | 9202 | sv_catpvs(sv, "]"); |
1de06328 YO |
9203 | } |
9204 | ||
a3621e74 | 9205 | } else if (k == CURLY) { |
cb434fcc | 9206 | if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX) |
cea2e8a9 GS |
9207 | Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */ |
9208 | Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o)); | |
a0d0e21e | 9209 | } |
2c2d71f5 JH |
9210 | else if (k == WHILEM && o->flags) /* Ordinal/of */ |
9211 | Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4); | |
1f1031fe | 9212 | else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP || OP(o)==ACCEPT) { |
894356b3 | 9213 | Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */ |
5daac39c | 9214 | if ( RXp_PAREN_NAMES(prog) ) { |
ee9b8eae | 9215 | if ( k != REF || OP(o) < NREF) { |
502c6561 | 9216 | AV *list= MUTABLE_AV(progi->data->data[progi->name_list_idx]); |
ee9b8eae YO |
9217 | SV **name= av_fetch(list, ARG(o), 0 ); |
9218 | if (name) | |
9219 | Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name)); | |
9220 | } | |
9221 | else { | |
502c6561 | 9222 | AV *list= MUTABLE_AV(progi->data->data[ progi->name_list_idx ]); |
ad64d0ec | 9223 | SV *sv_dat= MUTABLE_SV(progi->data->data[ ARG( o ) ]); |
ee9b8eae YO |
9224 | I32 *nums=(I32*)SvPVX(sv_dat); |
9225 | SV **name= av_fetch(list, nums[0], 0 ); | |
9226 | I32 n; | |
9227 | if (name) { | |
9228 | for ( n=0; n<SvIVX(sv_dat); n++ ) { | |
9229 | Perl_sv_catpvf(aTHX_ sv, "%s%"IVdf, | |
9230 | (n ? "," : ""), (IV)nums[n]); | |
9231 | } | |
9232 | Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name)); | |
1f1031fe | 9233 | } |
1f1031fe | 9234 | } |
ee9b8eae | 9235 | } |
1f1031fe | 9236 | } else if (k == GOSUB) |
6bda09f9 | 9237 | Perl_sv_catpvf(aTHX_ sv, "%d[%+d]", (int)ARG(o),(int)ARG2L(o)); /* Paren and offset */ |
e2e6a0f1 YO |
9238 | else if (k == VERB) { |
9239 | if (!o->flags) | |
9240 | Perl_sv_catpvf(aTHX_ sv, ":%"SVf, | |
ad64d0ec | 9241 | SVfARG((MUTABLE_SV(progi->data->data[ ARG( o ) ])))); |
e2e6a0f1 | 9242 | } else if (k == LOGICAL) |
04ebc1ab | 9243 | Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */ |
f9a79580 | 9244 | else if (k == FOLDCHAR) |
df44d732 | 9245 | Perl_sv_catpvf(aTHX_ sv, "[0x%"UVXf"]", PTR2UV(ARG(o)) ); |
653099ff GS |
9246 | else if (k == ANYOF) { |
9247 | int i, rangestart = -1; | |
2d03de9c | 9248 | const U8 flags = ANYOF_FLAGS(o); |
24d786f4 | 9249 | int do_sep = 0; |
0bd48802 AL |
9250 | |
9251 | /* Should be synchronized with * ANYOF_ #xdefines in regcomp.h */ | |
9252 | static const char * const anyofs[] = { | |
653099ff GS |
9253 | "\\w", |
9254 | "\\W", | |
9255 | "\\s", | |
9256 | "\\S", | |
9257 | "\\d", | |
9258 | "\\D", | |
9259 | "[:alnum:]", | |
9260 | "[:^alnum:]", | |
9261 | "[:alpha:]", | |
9262 | "[:^alpha:]", | |
9263 | "[:ascii:]", | |
9264 | "[:^ascii:]", | |
24d786f4 YO |
9265 | "[:cntrl:]", |
9266 | "[:^cntrl:]", | |
653099ff GS |
9267 | "[:graph:]", |
9268 | "[:^graph:]", | |
9269 | "[:lower:]", | |
9270 | "[:^lower:]", | |
9271 | "[:print:]", | |
9272 | "[:^print:]", | |
9273 | "[:punct:]", | |
9274 | "[:^punct:]", | |
9275 | "[:upper:]", | |
aaa51d5e | 9276 | "[:^upper:]", |
653099ff | 9277 | "[:xdigit:]", |
aaa51d5e JF |
9278 | "[:^xdigit:]", |
9279 | "[:space:]", | |
9280 | "[:^space:]", | |
9281 | "[:blank:]", | |
9282 | "[:^blank:]" | |
653099ff GS |
9283 | }; |
9284 | ||
19860706 | 9285 | if (flags & ANYOF_LOCALE) |
396482e1 | 9286 | sv_catpvs(sv, "{loc}"); |
19860706 | 9287 | if (flags & ANYOF_FOLD) |
396482e1 | 9288 | sv_catpvs(sv, "{i}"); |
653099ff | 9289 | Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]); |
19860706 | 9290 | if (flags & ANYOF_INVERT) |
396482e1 | 9291 | sv_catpvs(sv, "^"); |
3339dfd8 YO |
9292 | |
9293 | /* output what the standard cp 0-255 bitmap matches */ | |
ffc61ed2 JH |
9294 | for (i = 0; i <= 256; i++) { |
9295 | if (i < 256 && ANYOF_BITMAP_TEST(o,i)) { | |
9296 | if (rangestart == -1) | |
9297 | rangestart = i; | |
9298 | } else if (rangestart != -1) { | |
9299 | if (i <= rangestart + 3) | |
9300 | for (; rangestart < i; rangestart++) | |
653099ff | 9301 | put_byte(sv, rangestart); |
ffc61ed2 JH |
9302 | else { |
9303 | put_byte(sv, rangestart); | |
396482e1 | 9304 | sv_catpvs(sv, "-"); |
ffc61ed2 | 9305 | put_byte(sv, i - 1); |
653099ff | 9306 | } |
24d786f4 | 9307 | do_sep = 1; |
ffc61ed2 | 9308 | rangestart = -1; |
653099ff | 9309 | } |
847a199f | 9310 | } |
3339dfd8 YO |
9311 | |
9312 | EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags); | |
9313 | /* output any special charclass tests (used mostly under use locale) */ | |
ffc61ed2 | 9314 | if (o->flags & ANYOF_CLASS) |
bb7a0f54 | 9315 | for (i = 0; i < (int)(sizeof(anyofs)/sizeof(char*)); i++) |
24d786f4 | 9316 | if (ANYOF_CLASS_TEST(o,i)) { |
ffc61ed2 | 9317 | sv_catpv(sv, anyofs[i]); |
24d786f4 YO |
9318 | do_sep = 1; |
9319 | } | |
9320 | ||
3339dfd8 YO |
9321 | EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags); |
9322 | ||
9323 | /* output information about the unicode matching */ | |
ffc61ed2 | 9324 | if (flags & ANYOF_UNICODE) |
396482e1 | 9325 | sv_catpvs(sv, "{unicode}"); |
1aa99e6b | 9326 | else if (flags & ANYOF_UNICODE_ALL) |
396482e1 | 9327 | sv_catpvs(sv, "{unicode_all}"); |
ffc61ed2 JH |
9328 | |
9329 | { | |
9330 | SV *lv; | |
32fc9b6a | 9331 | SV * const sw = regclass_swash(prog, o, FALSE, &lv, 0); |
b81d288d | 9332 | |
ffc61ed2 JH |
9333 | if (lv) { |
9334 | if (sw) { | |
89ebb4a3 | 9335 | U8 s[UTF8_MAXBYTES_CASE+1]; |
24d786f4 | 9336 | |
ffc61ed2 | 9337 | for (i = 0; i <= 256; i++) { /* just the first 256 */ |
1df70142 | 9338 | uvchr_to_utf8(s, i); |
ffc61ed2 | 9339 | |
3568d838 | 9340 | if (i < 256 && swash_fetch(sw, s, TRUE)) { |
ffc61ed2 JH |
9341 | if (rangestart == -1) |
9342 | rangestart = i; | |
9343 | } else if (rangestart != -1) { | |
ffc61ed2 JH |
9344 | if (i <= rangestart + 3) |
9345 | for (; rangestart < i; rangestart++) { | |
2d03de9c AL |
9346 | const U8 * const e = uvchr_to_utf8(s,rangestart); |
9347 | U8 *p; | |
9348 | for(p = s; p < e; p++) | |
ffc61ed2 JH |
9349 | put_byte(sv, *p); |
9350 | } | |
9351 | else { | |
2d03de9c AL |
9352 | const U8 *e = uvchr_to_utf8(s,rangestart); |
9353 | U8 *p; | |
9354 | for (p = s; p < e; p++) | |
ffc61ed2 | 9355 | put_byte(sv, *p); |
396482e1 | 9356 | sv_catpvs(sv, "-"); |
2d03de9c AL |
9357 | e = uvchr_to_utf8(s, i-1); |
9358 | for (p = s; p < e; p++) | |
1df70142 | 9359 | put_byte(sv, *p); |
ffc61ed2 JH |
9360 | } |
9361 | rangestart = -1; | |
9362 | } | |
19860706 | 9363 | } |
ffc61ed2 | 9364 | |
396482e1 | 9365 | sv_catpvs(sv, "..."); /* et cetera */ |
19860706 | 9366 | } |
fde631ed | 9367 | |
ffc61ed2 | 9368 | { |
2e0de35c | 9369 | char *s = savesvpv(lv); |
c445ea15 | 9370 | char * const origs = s; |
b81d288d | 9371 | |
3dab1dad YO |
9372 | while (*s && *s != '\n') |
9373 | s++; | |
b81d288d | 9374 | |
ffc61ed2 | 9375 | if (*s == '\n') { |
2d03de9c | 9376 | const char * const t = ++s; |
ffc61ed2 JH |
9377 | |
9378 | while (*s) { | |
9379 | if (*s == '\n') | |
9380 | *s = ' '; | |
9381 | s++; | |
9382 | } | |
9383 | if (s[-1] == ' ') | |
9384 | s[-1] = 0; | |
9385 | ||
9386 | sv_catpv(sv, t); | |
fde631ed | 9387 | } |
b81d288d | 9388 | |
ffc61ed2 | 9389 | Safefree(origs); |
fde631ed JH |
9390 | } |
9391 | } | |
653099ff | 9392 | } |
ffc61ed2 | 9393 | |
653099ff GS |
9394 | Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]); |
9395 | } | |
9b155405 | 9396 | else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH)) |
07be1b83 | 9397 | Perl_sv_catpvf(aTHX_ sv, "[%d]", -(o->flags)); |
65e66c80 | 9398 | #else |
96a5add6 | 9399 | PERL_UNUSED_CONTEXT; |
65e66c80 SP |
9400 | PERL_UNUSED_ARG(sv); |
9401 | PERL_UNUSED_ARG(o); | |
f9049ba1 | 9402 | PERL_UNUSED_ARG(prog); |
17c3b450 | 9403 | #endif /* DEBUGGING */ |
35ff7856 | 9404 | } |
a687059c | 9405 | |
cad2e5aa | 9406 | SV * |
288b8c02 | 9407 | Perl_re_intuit_string(pTHX_ REGEXP * const r) |
cad2e5aa | 9408 | { /* Assume that RE_INTUIT is set */ |
97aff369 | 9409 | dVAR; |
288b8c02 | 9410 | struct regexp *const prog = (struct regexp *)SvANY(r); |
a3621e74 | 9411 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
9412 | |
9413 | PERL_ARGS_ASSERT_RE_INTUIT_STRING; | |
96a5add6 AL |
9414 | PERL_UNUSED_CONTEXT; |
9415 | ||
a3621e74 | 9416 | DEBUG_COMPILE_r( |
cfd0369c | 9417 | { |
2d03de9c | 9418 | const char * const s = SvPV_nolen_const(prog->check_substr |
cfd0369c | 9419 | ? prog->check_substr : prog->check_utf8); |
cad2e5aa JH |
9420 | |
9421 | if (!PL_colorset) reginitcolors(); | |
9422 | PerlIO_printf(Perl_debug_log, | |
a0288114 | 9423 | "%sUsing REx %ssubstr:%s \"%s%.60s%s%s\"\n", |
33b8afdf JH |
9424 | PL_colors[4], |
9425 | prog->check_substr ? "" : "utf8 ", | |
9426 | PL_colors[5],PL_colors[0], | |
cad2e5aa JH |
9427 | s, |
9428 | PL_colors[1], | |
9429 | (strlen(s) > 60 ? "..." : "")); | |
9430 | } ); | |
9431 | ||
33b8afdf | 9432 | return prog->check_substr ? prog->check_substr : prog->check_utf8; |
cad2e5aa JH |
9433 | } |
9434 | ||
84da74a7 | 9435 | /* |
f8149455 | 9436 | pregfree() |
84da74a7 | 9437 | |
f8149455 YO |
9438 | handles refcounting and freeing the perl core regexp structure. When |
9439 | it is necessary to actually free the structure the first thing it | |
9440 | does is call the 'free' method of the regexp_engine associated to to | |
9441 | the regexp, allowing the handling of the void *pprivate; member | |
9442 | first. (This routine is not overridable by extensions, which is why | |
9443 | the extensions free is called first.) | |
9444 | ||
9445 | See regdupe and regdupe_internal if you change anything here. | |
84da74a7 | 9446 | */ |
f8149455 | 9447 | #ifndef PERL_IN_XSUB_RE |
2b69d0c2 | 9448 | void |
84679df5 | 9449 | Perl_pregfree(pTHX_ REGEXP *r) |
a687059c | 9450 | { |
288b8c02 NC |
9451 | SvREFCNT_dec(r); |
9452 | } | |
9453 | ||
9454 | void | |
9455 | Perl_pregfree2(pTHX_ REGEXP *rx) | |
9456 | { | |
27da23d5 | 9457 | dVAR; |
288b8c02 | 9458 | struct regexp *const r = (struct regexp *)SvANY(rx); |
fc32ee4a | 9459 | GET_RE_DEBUG_FLAGS_DECL; |
a3621e74 | 9460 | |
7918f24d NC |
9461 | PERL_ARGS_ASSERT_PREGFREE2; |
9462 | ||
28d8d7f4 YO |
9463 | if (r->mother_re) { |
9464 | ReREFCNT_dec(r->mother_re); | |
9465 | } else { | |
288b8c02 | 9466 | CALLREGFREE_PVT(rx); /* free the private data */ |
ef8d46e8 | 9467 | SvREFCNT_dec(RXp_PAREN_NAMES(r)); |
28d8d7f4 YO |
9468 | } |
9469 | if (r->substrs) { | |
ef8d46e8 VP |
9470 | SvREFCNT_dec(r->anchored_substr); |
9471 | SvREFCNT_dec(r->anchored_utf8); | |
9472 | SvREFCNT_dec(r->float_substr); | |
9473 | SvREFCNT_dec(r->float_utf8); | |
28d8d7f4 YO |
9474 | Safefree(r->substrs); |
9475 | } | |
288b8c02 | 9476 | RX_MATCH_COPY_FREE(rx); |
f8c7b90f | 9477 | #ifdef PERL_OLD_COPY_ON_WRITE |
ef8d46e8 | 9478 | SvREFCNT_dec(r->saved_copy); |
ed252734 | 9479 | #endif |
f0ab9afb | 9480 | Safefree(r->offs); |
f8149455 | 9481 | } |
28d8d7f4 YO |
9482 | |
9483 | /* reg_temp_copy() | |
9484 | ||
9485 | This is a hacky workaround to the structural issue of match results | |
9486 | being stored in the regexp structure which is in turn stored in | |
9487 | PL_curpm/PL_reg_curpm. The problem is that due to qr// the pattern | |
9488 | could be PL_curpm in multiple contexts, and could require multiple | |
9489 | result sets being associated with the pattern simultaneously, such | |
9490 | as when doing a recursive match with (??{$qr}) | |
9491 | ||
9492 | The solution is to make a lightweight copy of the regexp structure | |
9493 | when a qr// is returned from the code executed by (??{$qr}) this | |
9494 | lightweight copy doesnt actually own any of its data except for | |
9495 | the starp/end and the actual regexp structure itself. | |
9496 | ||
9497 | */ | |
9498 | ||
9499 | ||
84679df5 | 9500 | REGEXP * |
f0826785 | 9501 | Perl_reg_temp_copy (pTHX_ REGEXP *ret_x, REGEXP *rx) |
7918f24d | 9502 | { |
f0826785 | 9503 | struct regexp *ret; |
288b8c02 | 9504 | struct regexp *const r = (struct regexp *)SvANY(rx); |
28d8d7f4 | 9505 | register const I32 npar = r->nparens+1; |
7918f24d NC |
9506 | |
9507 | PERL_ARGS_ASSERT_REG_TEMP_COPY; | |
9508 | ||
f0826785 BM |
9509 | if (!ret_x) |
9510 | ret_x = (REGEXP*) newSV_type(SVt_REGEXP); | |
9511 | ret = (struct regexp *)SvANY(ret_x); | |
9512 | ||
288b8c02 | 9513 | (void)ReREFCNT_inc(rx); |
f7c278bf NC |
9514 | /* We can take advantage of the existing "copied buffer" mechanism in SVs |
9515 | by pointing directly at the buffer, but flagging that the allocated | |
9516 | space in the copy is zero. As we've just done a struct copy, it's now | |
9517 | a case of zero-ing that, rather than copying the current length. */ | |
9518 | SvPV_set(ret_x, RX_WRAPPED(rx)); | |
8f6ae13c | 9519 | SvFLAGS(ret_x) |= SvFLAGS(rx) & (SVf_POK|SVp_POK|SVf_UTF8); |
b6f60916 NC |
9520 | memcpy(&(ret->xpv_cur), &(r->xpv_cur), |
9521 | sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur)); | |
f7c278bf | 9522 | SvLEN_set(ret_x, 0); |
b9ad13ac | 9523 | SvSTASH_set(ret_x, NULL); |
703c388d | 9524 | SvMAGIC_set(ret_x, NULL); |
f0ab9afb NC |
9525 | Newx(ret->offs, npar, regexp_paren_pair); |
9526 | Copy(r->offs, ret->offs, npar, regexp_paren_pair); | |
28d8d7f4 | 9527 | if (r->substrs) { |
28d8d7f4 | 9528 | Newx(ret->substrs, 1, struct reg_substr_data); |
6ab65676 NC |
9529 | StructCopy(r->substrs, ret->substrs, struct reg_substr_data); |
9530 | ||
9531 | SvREFCNT_inc_void(ret->anchored_substr); | |
9532 | SvREFCNT_inc_void(ret->anchored_utf8); | |
9533 | SvREFCNT_inc_void(ret->float_substr); | |
9534 | SvREFCNT_inc_void(ret->float_utf8); | |
9535 | ||
9536 | /* check_substr and check_utf8, if non-NULL, point to either their | |
9537 | anchored or float namesakes, and don't hold a second reference. */ | |
486913e4 | 9538 | } |
288b8c02 | 9539 | RX_MATCH_COPIED_off(ret_x); |
28d8d7f4 | 9540 | #ifdef PERL_OLD_COPY_ON_WRITE |
b89b0c6f | 9541 | ret->saved_copy = NULL; |
28d8d7f4 | 9542 | #endif |
288b8c02 | 9543 | ret->mother_re = rx; |
28d8d7f4 | 9544 | |
288b8c02 | 9545 | return ret_x; |
28d8d7f4 | 9546 | } |
f8149455 YO |
9547 | #endif |
9548 | ||
9549 | /* regfree_internal() | |
9550 | ||
9551 | Free the private data in a regexp. This is overloadable by | |
9552 | extensions. Perl takes care of the regexp structure in pregfree(), | |
9553 | this covers the *pprivate pointer which technically perldoesnt | |
9554 | know about, however of course we have to handle the | |
9555 | regexp_internal structure when no extension is in use. | |
9556 | ||
9557 | Note this is called before freeing anything in the regexp | |
9558 | structure. | |
9559 | */ | |
9560 | ||
9561 | void | |
288b8c02 | 9562 | Perl_regfree_internal(pTHX_ REGEXP * const rx) |
f8149455 YO |
9563 | { |
9564 | dVAR; | |
288b8c02 | 9565 | struct regexp *const r = (struct regexp *)SvANY(rx); |
f8149455 YO |
9566 | RXi_GET_DECL(r,ri); |
9567 | GET_RE_DEBUG_FLAGS_DECL; | |
7918f24d NC |
9568 | |
9569 | PERL_ARGS_ASSERT_REGFREE_INTERNAL; | |
9570 | ||
f8149455 YO |
9571 | DEBUG_COMPILE_r({ |
9572 | if (!PL_colorset) | |
9573 | reginitcolors(); | |
9574 | { | |
9575 | SV *dsv= sv_newmortal(); | |
3c8556c3 | 9576 | RE_PV_QUOTED_DECL(s, RX_UTF8(rx), |
5509d87a | 9577 | dsv, RX_PRECOMP(rx), RX_PRELEN(rx), 60); |
f8149455 YO |
9578 | PerlIO_printf(Perl_debug_log,"%sFreeing REx:%s %s\n", |
9579 | PL_colors[4],PL_colors[5],s); | |
9580 | } | |
9581 | }); | |
7122b237 YO |
9582 | #ifdef RE_TRACK_PATTERN_OFFSETS |
9583 | if (ri->u.offsets) | |
9584 | Safefree(ri->u.offsets); /* 20010421 MJD */ | |
9585 | #endif | |
f8fc2ecf YO |
9586 | if (ri->data) { |
9587 | int n = ri->data->count; | |
f3548bdc DM |
9588 | PAD* new_comppad = NULL; |
9589 | PAD* old_comppad; | |
4026c95a | 9590 | PADOFFSET refcnt; |
dfad63ad | 9591 | |
c277df42 | 9592 | while (--n >= 0) { |
261faec3 | 9593 | /* If you add a ->what type here, update the comment in regcomp.h */ |
f8fc2ecf | 9594 | switch (ri->data->what[n]) { |
af534a04 | 9595 | case 'a': |
c277df42 | 9596 | case 's': |
81714fb9 | 9597 | case 'S': |
55eed653 | 9598 | case 'u': |
ad64d0ec | 9599 | SvREFCNT_dec(MUTABLE_SV(ri->data->data[n])); |
c277df42 | 9600 | break; |
653099ff | 9601 | case 'f': |
f8fc2ecf | 9602 | Safefree(ri->data->data[n]); |
653099ff | 9603 | break; |
dfad63ad | 9604 | case 'p': |
502c6561 | 9605 | new_comppad = MUTABLE_AV(ri->data->data[n]); |
dfad63ad | 9606 | break; |
c277df42 | 9607 | case 'o': |
dfad63ad | 9608 | if (new_comppad == NULL) |
cea2e8a9 | 9609 | Perl_croak(aTHX_ "panic: pregfree comppad"); |
f3548bdc DM |
9610 | PAD_SAVE_LOCAL(old_comppad, |
9611 | /* Watch out for global destruction's random ordering. */ | |
c445ea15 | 9612 | (SvTYPE(new_comppad) == SVt_PVAV) ? new_comppad : NULL |
f3548bdc | 9613 | ); |
b34c0dd4 | 9614 | OP_REFCNT_LOCK; |
f8fc2ecf | 9615 | refcnt = OpREFCNT_dec((OP_4tree*)ri->data->data[n]); |
4026c95a SH |
9616 | OP_REFCNT_UNLOCK; |
9617 | if (!refcnt) | |
f8fc2ecf | 9618 | op_free((OP_4tree*)ri->data->data[n]); |
9b978d73 | 9619 | |
f3548bdc | 9620 | PAD_RESTORE_LOCAL(old_comppad); |
ad64d0ec | 9621 | SvREFCNT_dec(MUTABLE_SV(new_comppad)); |
dfad63ad | 9622 | new_comppad = NULL; |
c277df42 IZ |
9623 | break; |
9624 | case 'n': | |
9e55ce06 | 9625 | break; |
07be1b83 | 9626 | case 'T': |
be8e71aa YO |
9627 | { /* Aho Corasick add-on structure for a trie node. |
9628 | Used in stclass optimization only */ | |
07be1b83 | 9629 | U32 refcount; |
f8fc2ecf | 9630 | reg_ac_data *aho=(reg_ac_data*)ri->data->data[n]; |
07be1b83 YO |
9631 | OP_REFCNT_LOCK; |
9632 | refcount = --aho->refcount; | |
9633 | OP_REFCNT_UNLOCK; | |
9634 | if ( !refcount ) { | |
446bd890 NC |
9635 | PerlMemShared_free(aho->states); |
9636 | PerlMemShared_free(aho->fail); | |
446bd890 NC |
9637 | /* do this last!!!! */ |
9638 | PerlMemShared_free(ri->data->data[n]); | |
9639 | PerlMemShared_free(ri->regstclass); | |
07be1b83 YO |
9640 | } |
9641 | } | |
9642 | break; | |
a3621e74 | 9643 | case 't': |
07be1b83 | 9644 | { |
be8e71aa | 9645 | /* trie structure. */ |
07be1b83 | 9646 | U32 refcount; |
f8fc2ecf | 9647 | reg_trie_data *trie=(reg_trie_data*)ri->data->data[n]; |
07be1b83 YO |
9648 | OP_REFCNT_LOCK; |
9649 | refcount = --trie->refcount; | |
9650 | OP_REFCNT_UNLOCK; | |
9651 | if ( !refcount ) { | |
446bd890 | 9652 | PerlMemShared_free(trie->charmap); |
446bd890 NC |
9653 | PerlMemShared_free(trie->states); |
9654 | PerlMemShared_free(trie->trans); | |
07be1b83 | 9655 | if (trie->bitmap) |
446bd890 | 9656 | PerlMemShared_free(trie->bitmap); |
786e8c11 | 9657 | if (trie->jump) |
446bd890 | 9658 | PerlMemShared_free(trie->jump); |
2e64971a | 9659 | PerlMemShared_free(trie->wordinfo); |
446bd890 NC |
9660 | /* do this last!!!! */ |
9661 | PerlMemShared_free(ri->data->data[n]); | |
a3621e74 | 9662 | } |
07be1b83 YO |
9663 | } |
9664 | break; | |
c277df42 | 9665 | default: |
f8fc2ecf | 9666 | Perl_croak(aTHX_ "panic: regfree data code '%c'", ri->data->what[n]); |
c277df42 IZ |
9667 | } |
9668 | } | |
f8fc2ecf YO |
9669 | Safefree(ri->data->what); |
9670 | Safefree(ri->data); | |
a0d0e21e | 9671 | } |
28d8d7f4 | 9672 | |
f8fc2ecf | 9673 | Safefree(ri); |
a687059c | 9674 | } |
c277df42 | 9675 | |
a09252eb NC |
9676 | #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) |
9677 | #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t)) | |
84da74a7 YO |
9678 | #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL) |
9679 | ||
9680 | /* | |
32cd70f6 | 9681 | re_dup - duplicate a regexp. |
84da74a7 | 9682 | |
8233f606 DM |
9683 | This routine is expected to clone a given regexp structure. It is only |
9684 | compiled under USE_ITHREADS. | |
32cd70f6 | 9685 | |
f8149455 YO |
9686 | After all of the core data stored in struct regexp is duplicated |
9687 | the regexp_engine.dupe method is used to copy any private data | |
9688 | stored in the *pprivate pointer. This allows extensions to handle | |
9689 | any duplication it needs to do. | |
9690 | ||
9691 | See pregfree() and regfree_internal() if you change anything here. | |
84da74a7 | 9692 | */ |
a3c0e9ca | 9693 | #if defined(USE_ITHREADS) |
f8149455 | 9694 | #ifndef PERL_IN_XSUB_RE |
288b8c02 NC |
9695 | void |
9696 | Perl_re_dup_guts(pTHX_ const REGEXP *sstr, REGEXP *dstr, CLONE_PARAMS *param) | |
84da74a7 | 9697 | { |
84da74a7 | 9698 | dVAR; |
a86a1ca7 | 9699 | I32 npar; |
288b8c02 NC |
9700 | const struct regexp *r = (const struct regexp *)SvANY(sstr); |
9701 | struct regexp *ret = (struct regexp *)SvANY(dstr); | |
f8149455 | 9702 | |
7918f24d NC |
9703 | PERL_ARGS_ASSERT_RE_DUP_GUTS; |
9704 | ||
84da74a7 | 9705 | npar = r->nparens+1; |
f0ab9afb NC |
9706 | Newx(ret->offs, npar, regexp_paren_pair); |
9707 | Copy(r->offs, ret->offs, npar, regexp_paren_pair); | |
6057429f | 9708 | if(ret->swap) { |
28d8d7f4 | 9709 | /* no need to copy these */ |
f0ab9afb | 9710 | Newx(ret->swap, npar, regexp_paren_pair); |
28d8d7f4 | 9711 | } |
84da74a7 | 9712 | |
6057429f | 9713 | if (ret->substrs) { |
32cd70f6 NC |
9714 | /* Do it this way to avoid reading from *r after the StructCopy(). |
9715 | That way, if any of the sv_dup_inc()s dislodge *r from the L1 | |
9716 | cache, it doesn't matter. */ | |
66b1de87 NC |
9717 | const bool anchored = r->check_substr |
9718 | ? r->check_substr == r->anchored_substr | |
9719 | : r->check_utf8 == r->anchored_utf8; | |
785a26d5 | 9720 | Newx(ret->substrs, 1, struct reg_substr_data); |
a86a1ca7 NC |
9721 | StructCopy(r->substrs, ret->substrs, struct reg_substr_data); |
9722 | ||
32cd70f6 NC |
9723 | ret->anchored_substr = sv_dup_inc(ret->anchored_substr, param); |
9724 | ret->anchored_utf8 = sv_dup_inc(ret->anchored_utf8, param); | |
9725 | ret->float_substr = sv_dup_inc(ret->float_substr, param); | |
9726 | ret->float_utf8 = sv_dup_inc(ret->float_utf8, param); | |
a86a1ca7 | 9727 | |
32cd70f6 NC |
9728 | /* check_substr and check_utf8, if non-NULL, point to either their |
9729 | anchored or float namesakes, and don't hold a second reference. */ | |
9730 | ||
9731 | if (ret->check_substr) { | |
9732 | if (anchored) { | |
9733 | assert(r->check_utf8 == r->anchored_utf8); | |
9734 | ret->check_substr = ret->anchored_substr; | |
9735 | ret->check_utf8 = ret->anchored_utf8; | |
9736 | } else { | |
9737 | assert(r->check_substr == r->float_substr); | |
9738 | assert(r->check_utf8 == r->float_utf8); | |
9739 | ret->check_substr = ret->float_substr; | |
9740 | ret->check_utf8 = ret->float_utf8; | |
9741 | } | |
66b1de87 NC |
9742 | } else if (ret->check_utf8) { |
9743 | if (anchored) { | |
9744 | ret->check_utf8 = ret->anchored_utf8; | |
9745 | } else { | |
9746 | ret->check_utf8 = ret->float_utf8; | |
9747 | } | |
32cd70f6 | 9748 | } |
6057429f | 9749 | } |
f8149455 | 9750 | |
5daac39c | 9751 | RXp_PAREN_NAMES(ret) = hv_dup_inc(RXp_PAREN_NAMES(ret), param); |
bcdf7404 | 9752 | |
6057429f | 9753 | if (ret->pprivate) |
288b8c02 | 9754 | RXi_SET(ret,CALLREGDUPE_PVT(dstr,param)); |
f8149455 | 9755 | |
288b8c02 | 9756 | if (RX_MATCH_COPIED(dstr)) |
6057429f | 9757 | ret->subbeg = SAVEPVN(ret->subbeg, ret->sublen); |
f8149455 YO |
9758 | else |
9759 | ret->subbeg = NULL; | |
9760 | #ifdef PERL_OLD_COPY_ON_WRITE | |
9761 | ret->saved_copy = NULL; | |
9762 | #endif | |
6057429f | 9763 | |
c2123ae3 NC |
9764 | if (ret->mother_re) { |
9765 | if (SvPVX_const(dstr) == SvPVX_const(ret->mother_re)) { | |
9766 | /* Our storage points directly to our mother regexp, but that's | |
9767 | 1: a buffer in a different thread | |
9768 | 2: something we no longer hold a reference on | |
9769 | so we need to copy it locally. */ | |
9770 | /* Note we need to sue SvCUR() on our mother_re, because it, in | |
9771 | turn, may well be pointing to its own mother_re. */ | |
9772 | SvPV_set(dstr, SAVEPVN(SvPVX_const(ret->mother_re), | |
9773 | SvCUR(ret->mother_re)+1)); | |
9774 | SvLEN_set(dstr, SvCUR(ret->mother_re)+1); | |
9775 | } | |
9776 | ret->mother_re = NULL; | |
9777 | } | |
6057429f | 9778 | ret->gofs = 0; |
f8149455 YO |
9779 | } |
9780 | #endif /* PERL_IN_XSUB_RE */ | |
9781 | ||
9782 | /* | |
9783 | regdupe_internal() | |
9784 | ||
9785 | This is the internal complement to regdupe() which is used to copy | |
9786 | the structure pointed to by the *pprivate pointer in the regexp. | |
9787 | This is the core version of the extension overridable cloning hook. | |
9788 | The regexp structure being duplicated will be copied by perl prior | |
9789 | to this and will be provided as the regexp *r argument, however | |
9790 | with the /old/ structures pprivate pointer value. Thus this routine | |
9791 | may override any copying normally done by perl. | |
9792 | ||
9793 | It returns a pointer to the new regexp_internal structure. | |
9794 | */ | |
9795 | ||
9796 | void * | |
288b8c02 | 9797 | Perl_regdupe_internal(pTHX_ REGEXP * const rx, CLONE_PARAMS *param) |
f8149455 YO |
9798 | { |
9799 | dVAR; | |
288b8c02 | 9800 | struct regexp *const r = (struct regexp *)SvANY(rx); |
f8149455 YO |
9801 | regexp_internal *reti; |
9802 | int len, npar; | |
9803 | RXi_GET_DECL(r,ri); | |
7918f24d NC |
9804 | |
9805 | PERL_ARGS_ASSERT_REGDUPE_INTERNAL; | |
f8149455 YO |
9806 | |
9807 | npar = r->nparens+1; | |
7122b237 | 9808 | len = ProgLen(ri); |
f8149455 | 9809 | |
45cf4570 | 9810 | Newxc(reti, sizeof(regexp_internal) + len*sizeof(regnode), char, regexp_internal); |
f8149455 YO |
9811 | Copy(ri->program, reti->program, len+1, regnode); |
9812 | ||
f8149455 | 9813 | |
f8fc2ecf | 9814 | reti->regstclass = NULL; |
bcdf7404 | 9815 | |
f8fc2ecf | 9816 | if (ri->data) { |
84da74a7 | 9817 | struct reg_data *d; |
f8fc2ecf | 9818 | const int count = ri->data->count; |
84da74a7 YO |
9819 | int i; |
9820 | ||
9821 | Newxc(d, sizeof(struct reg_data) + count*sizeof(void *), | |
9822 | char, struct reg_data); | |
9823 | Newx(d->what, count, U8); | |
9824 | ||
9825 | d->count = count; | |
9826 | for (i = 0; i < count; i++) { | |
f8fc2ecf | 9827 | d->what[i] = ri->data->what[i]; |
84da74a7 | 9828 | switch (d->what[i]) { |
af534a04 | 9829 | /* legal options are one of: sSfpontTua |
84da74a7 | 9830 | see also regcomp.h and pregfree() */ |
af534a04 | 9831 | case 'a': /* actually an AV, but the dup function is identical. */ |
84da74a7 | 9832 | case 's': |
81714fb9 | 9833 | case 'S': |
0536c0a7 | 9834 | case 'p': /* actually an AV, but the dup function is identical. */ |
55eed653 | 9835 | case 'u': /* actually an HV, but the dup function is identical. */ |
ad64d0ec | 9836 | d->data[i] = sv_dup_inc((const SV *)ri->data->data[i], param); |
84da74a7 | 9837 | break; |
84da74a7 YO |
9838 | case 'f': |
9839 | /* This is cheating. */ | |
9840 | Newx(d->data[i], 1, struct regnode_charclass_class); | |
f8fc2ecf | 9841 | StructCopy(ri->data->data[i], d->data[i], |
84da74a7 | 9842 | struct regnode_charclass_class); |
f8fc2ecf | 9843 | reti->regstclass = (regnode*)d->data[i]; |
84da74a7 YO |
9844 | break; |
9845 | case 'o': | |
bbe252da YO |
9846 | /* Compiled op trees are readonly and in shared memory, |
9847 | and can thus be shared without duplication. */ | |
84da74a7 | 9848 | OP_REFCNT_LOCK; |
f8fc2ecf | 9849 | d->data[i] = (void*)OpREFCNT_inc((OP*)ri->data->data[i]); |
84da74a7 YO |
9850 | OP_REFCNT_UNLOCK; |
9851 | break; | |
23eab42c NC |
9852 | case 'T': |
9853 | /* Trie stclasses are readonly and can thus be shared | |
9854 | * without duplication. We free the stclass in pregfree | |
9855 | * when the corresponding reg_ac_data struct is freed. | |
9856 | */ | |
9857 | reti->regstclass= ri->regstclass; | |
9858 | /* Fall through */ | |
84da74a7 | 9859 | case 't': |
84da74a7 | 9860 | OP_REFCNT_LOCK; |
0536c0a7 | 9861 | ((reg_trie_data*)ri->data->data[i])->refcount++; |
84da74a7 | 9862 | OP_REFCNT_UNLOCK; |
0536c0a7 NC |
9863 | /* Fall through */ |
9864 | case 'n': | |
9865 | d->data[i] = ri->data->data[i]; | |
84da74a7 | 9866 | break; |
84da74a7 | 9867 | default: |
f8fc2ecf | 9868 | Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", ri->data->what[i]); |
84da74a7 YO |
9869 | } |
9870 | } | |
9871 | ||
f8fc2ecf | 9872 | reti->data = d; |
84da74a7 YO |
9873 | } |
9874 | else | |
f8fc2ecf | 9875 | reti->data = NULL; |
84da74a7 | 9876 | |
cde0cee5 YO |
9877 | reti->name_list_idx = ri->name_list_idx; |
9878 | ||
7122b237 YO |
9879 | #ifdef RE_TRACK_PATTERN_OFFSETS |
9880 | if (ri->u.offsets) { | |
9881 | Newx(reti->u.offsets, 2*len+1, U32); | |
9882 | Copy(ri->u.offsets, reti->u.offsets, 2*len+1, U32); | |
9883 | } | |
9884 | #else | |
9885 | SetProgLen(reti,len); | |
9886 | #endif | |
9887 | ||
f8149455 | 9888 | return (void*)reti; |
84da74a7 | 9889 | } |
f8149455 YO |
9890 | |
9891 | #endif /* USE_ITHREADS */ | |
84da74a7 | 9892 | |
f8149455 | 9893 | #ifndef PERL_IN_XSUB_RE |
bcdf7404 | 9894 | |
c277df42 IZ |
9895 | /* |
9896 | - regnext - dig the "next" pointer out of a node | |
c277df42 IZ |
9897 | */ |
9898 | regnode * | |
864dbfa3 | 9899 | Perl_regnext(pTHX_ register regnode *p) |
c277df42 | 9900 | { |
97aff369 | 9901 | dVAR; |
c277df42 IZ |
9902 | register I32 offset; |
9903 | ||
f8fc2ecf | 9904 | if (!p) |
c277df42 IZ |
9905 | return(NULL); |
9906 | ||
35db910f KW |
9907 | if (OP(p) > REGNODE_MAX) { /* regnode.type is unsigned */ |
9908 | Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(p), (int)REGNODE_MAX); | |
9909 | } | |
9910 | ||
c277df42 IZ |
9911 | offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p)); |
9912 | if (offset == 0) | |
9913 | return(NULL); | |
9914 | ||
c277df42 | 9915 | return(p+offset); |
c277df42 | 9916 | } |
76234dfb | 9917 | #endif |
c277df42 | 9918 | |
01f988be | 9919 | STATIC void |
cea2e8a9 | 9920 | S_re_croak2(pTHX_ const char* pat1,const char* pat2,...) |
c277df42 IZ |
9921 | { |
9922 | va_list args; | |
9923 | STRLEN l1 = strlen(pat1); | |
9924 | STRLEN l2 = strlen(pat2); | |
9925 | char buf[512]; | |
06bf62c7 | 9926 | SV *msv; |
73d840c0 | 9927 | const char *message; |
c277df42 | 9928 | |
7918f24d NC |
9929 | PERL_ARGS_ASSERT_RE_CROAK2; |
9930 | ||
c277df42 IZ |
9931 | if (l1 > 510) |
9932 | l1 = 510; | |
9933 | if (l1 + l2 > 510) | |
9934 | l2 = 510 - l1; | |
9935 | Copy(pat1, buf, l1 , char); | |
9936 | Copy(pat2, buf + l1, l2 , char); | |
3b818b81 GS |
9937 | buf[l1 + l2] = '\n'; |
9938 | buf[l1 + l2 + 1] = '\0'; | |
8736538c AS |
9939 | #ifdef I_STDARG |
9940 | /* ANSI variant takes additional second argument */ | |
c277df42 | 9941 | va_start(args, pat2); |
8736538c AS |
9942 | #else |
9943 | va_start(args); | |
9944 | #endif | |
5a844595 | 9945 | msv = vmess(buf, &args); |
c277df42 | 9946 | va_end(args); |
cfd0369c | 9947 | message = SvPV_const(msv,l1); |
c277df42 IZ |
9948 | if (l1 > 512) |
9949 | l1 = 512; | |
9950 | Copy(message, buf, l1 , char); | |
197cf9b9 | 9951 | buf[l1-1] = '\0'; /* Overwrite \n */ |
cea2e8a9 | 9952 | Perl_croak(aTHX_ "%s", buf); |
c277df42 | 9953 | } |
a0ed51b3 LW |
9954 | |
9955 | /* XXX Here's a total kludge. But we need to re-enter for swash routines. */ | |
9956 | ||
76234dfb | 9957 | #ifndef PERL_IN_XSUB_RE |
a0ed51b3 | 9958 | void |
864dbfa3 | 9959 | Perl_save_re_context(pTHX) |
b81d288d | 9960 | { |
97aff369 | 9961 | dVAR; |
1ade1aa1 NC |
9962 | |
9963 | struct re_save_state *state; | |
9964 | ||
9965 | SAVEVPTR(PL_curcop); | |
9966 | SSGROW(SAVESTACK_ALLOC_FOR_RE_SAVE_STATE + 1); | |
9967 | ||
9968 | state = (struct re_save_state *)(PL_savestack + PL_savestack_ix); | |
9969 | PL_savestack_ix += SAVESTACK_ALLOC_FOR_RE_SAVE_STATE; | |
c6bf6a65 | 9970 | SSPUSHUV(SAVEt_RE_STATE); |
1ade1aa1 | 9971 | |
46ab3289 | 9972 | Copy(&PL_reg_state, state, 1, struct re_save_state); |
1ade1aa1 | 9973 | |
a0ed51b3 | 9974 | PL_reg_start_tmp = 0; |
a0ed51b3 | 9975 | PL_reg_start_tmpl = 0; |
c445ea15 | 9976 | PL_reg_oldsaved = NULL; |
a5db57d6 | 9977 | PL_reg_oldsavedlen = 0; |
a5db57d6 | 9978 | PL_reg_maxiter = 0; |
a5db57d6 | 9979 | PL_reg_leftiter = 0; |
c445ea15 | 9980 | PL_reg_poscache = NULL; |
a5db57d6 | 9981 | PL_reg_poscache_size = 0; |
1ade1aa1 NC |
9982 | #ifdef PERL_OLD_COPY_ON_WRITE |
9983 | PL_nrs = NULL; | |
9984 | #endif | |
ada6e8a9 | 9985 | |
c445ea15 AL |
9986 | /* Save $1..$n (#18107: UTF-8 s/(\w+)/uc($1)/e); AMS 20021106. */ |
9987 | if (PL_curpm) { | |
9988 | const REGEXP * const rx = PM_GETRE(PL_curpm); | |
9989 | if (rx) { | |
1df70142 | 9990 | U32 i; |
07bc277f | 9991 | for (i = 1; i <= RX_NPARENS(rx); i++) { |
1df70142 | 9992 | char digits[TYPE_CHARS(long)]; |
d9fad198 | 9993 | const STRLEN len = my_snprintf(digits, sizeof(digits), "%lu", (long)i); |
49f27e4b NC |
9994 | GV *const *const gvp |
9995 | = (GV**)hv_fetch(PL_defstash, digits, len, 0); | |
9996 | ||
b37c2d43 AL |
9997 | if (gvp) { |
9998 | GV * const gv = *gvp; | |
9999 | if (SvTYPE(gv) == SVt_PVGV && GvSV(gv)) | |
10000 | save_scalar(gv); | |
49f27e4b | 10001 | } |
ada6e8a9 AMS |
10002 | } |
10003 | } | |
10004 | } | |
a0ed51b3 | 10005 | } |
76234dfb | 10006 | #endif |
51371543 | 10007 | |
51371543 | 10008 | static void |
acfe0abc | 10009 | clear_re(pTHX_ void *r) |
51371543 | 10010 | { |
97aff369 | 10011 | dVAR; |
84679df5 | 10012 | ReREFCNT_dec((REGEXP *)r); |
51371543 | 10013 | } |
ffbc6a93 | 10014 | |
a28509cc AL |
10015 | #ifdef DEBUGGING |
10016 | ||
10017 | STATIC void | |
10018 | S_put_byte(pTHX_ SV *sv, int c) | |
10019 | { | |
7918f24d NC |
10020 | PERL_ARGS_ASSERT_PUT_BYTE; |
10021 | ||
7fddd944 NC |
10022 | /* Our definition of isPRINT() ignores locales, so only bytes that are |
10023 | not part of UTF-8 are considered printable. I assume that the same | |
10024 | holds for UTF-EBCDIC. | |
10025 | Also, code point 255 is not printable in either (it's E0 in EBCDIC, | |
10026 | which Wikipedia says: | |
10027 | ||
10028 | EO, or Eight Ones, is an 8-bit EBCDIC character code represented as all | |
10029 | ones (binary 1111 1111, hexadecimal FF). It is similar, but not | |
10030 | identical, to the ASCII delete (DEL) or rubout control character. | |
10031 | ) So the old condition can be simplified to !isPRINT(c) */ | |
10032 | if (!isPRINT(c)) | |
a28509cc | 10033 | Perl_sv_catpvf(aTHX_ sv, "\\%o", c); |
5e7aa789 | 10034 | else { |
88c9ea1e | 10035 | const char string = c; |
5e7aa789 NC |
10036 | if (c == '-' || c == ']' || c == '\\' || c == '^') |
10037 | sv_catpvs(sv, "\\"); | |
10038 | sv_catpvn(sv, &string, 1); | |
10039 | } | |
a28509cc AL |
10040 | } |
10041 | ||
786e8c11 | 10042 | |
3dab1dad YO |
10043 | #define CLEAR_OPTSTART \ |
10044 | if (optstart) STMT_START { \ | |
70685ca0 | 10045 | DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log, " (%"IVdf" nodes)\n", (IV)(node - optstart))); \ |
3dab1dad YO |
10046 | optstart=NULL; \ |
10047 | } STMT_END | |
10048 | ||
786e8c11 | 10049 | #define DUMPUNTIL(b,e) CLEAR_OPTSTART; node=dumpuntil(r,start,(b),(e),last,sv,indent+1,depth+1); |
3dab1dad | 10050 | |
b5a2f8d8 NC |
10051 | STATIC const regnode * |
10052 | S_dumpuntil(pTHX_ const regexp *r, const regnode *start, const regnode *node, | |
786e8c11 YO |
10053 | const regnode *last, const regnode *plast, |
10054 | SV* sv, I32 indent, U32 depth) | |
a28509cc | 10055 | { |
97aff369 | 10056 | dVAR; |
786e8c11 | 10057 | register U8 op = PSEUDO; /* Arbitrary non-END op. */ |
b5a2f8d8 | 10058 | register const regnode *next; |
3dab1dad | 10059 | const regnode *optstart= NULL; |
1f1031fe | 10060 | |
f8fc2ecf | 10061 | RXi_GET_DECL(r,ri); |
3dab1dad | 10062 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
10063 | |
10064 | PERL_ARGS_ASSERT_DUMPUNTIL; | |
10065 | ||
786e8c11 YO |
10066 | #ifdef DEBUG_DUMPUNTIL |
10067 | PerlIO_printf(Perl_debug_log, "--- %d : %d - %d - %d\n",indent,node-start, | |
10068 | last ? last-start : 0,plast ? plast-start : 0); | |
10069 | #endif | |
10070 | ||
10071 | if (plast && plast < last) | |
10072 | last= plast; | |
10073 | ||
10074 | while (PL_regkind[op] != END && (!last || node < last)) { | |
a28509cc | 10075 | /* While that wasn't END last time... */ |
a28509cc AL |
10076 | NODE_ALIGN(node); |
10077 | op = OP(node); | |
de734bd5 | 10078 | if (op == CLOSE || op == WHILEM) |
786e8c11 | 10079 | indent--; |
b5a2f8d8 | 10080 | next = regnext((regnode *)node); |
1f1031fe | 10081 | |
a28509cc | 10082 | /* Where, what. */ |
8e11feef | 10083 | if (OP(node) == OPTIMIZED) { |
e68ec53f | 10084 | if (!optstart && RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE)) |
8e11feef | 10085 | optstart = node; |
3dab1dad | 10086 | else |
8e11feef | 10087 | goto after_print; |
3dab1dad YO |
10088 | } else |
10089 | CLEAR_OPTSTART; | |
1f1031fe | 10090 | |
32fc9b6a | 10091 | regprop(r, sv, node); |
a28509cc | 10092 | PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start), |
786e8c11 | 10093 | (int)(2*indent + 1), "", SvPVX_const(sv)); |
1f1031fe YO |
10094 | |
10095 | if (OP(node) != OPTIMIZED) { | |
10096 | if (next == NULL) /* Next ptr. */ | |
10097 | PerlIO_printf(Perl_debug_log, " (0)"); | |
10098 | else if (PL_regkind[(U8)op] == BRANCH && PL_regkind[OP(next)] != BRANCH ) | |
10099 | PerlIO_printf(Perl_debug_log, " (FAIL)"); | |
10100 | else | |
10101 | PerlIO_printf(Perl_debug_log, " (%"IVdf")", (IV)(next - start)); | |
10102 | (void)PerlIO_putc(Perl_debug_log, '\n'); | |
10103 | } | |
10104 | ||
a28509cc AL |
10105 | after_print: |
10106 | if (PL_regkind[(U8)op] == BRANCHJ) { | |
be8e71aa YO |
10107 | assert(next); |
10108 | { | |
10109 | register const regnode *nnode = (OP(next) == LONGJMP | |
b5a2f8d8 NC |
10110 | ? regnext((regnode *)next) |
10111 | : next); | |
be8e71aa YO |
10112 | if (last && nnode > last) |
10113 | nnode = last; | |
786e8c11 | 10114 | DUMPUNTIL(NEXTOPER(NEXTOPER(node)), nnode); |
be8e71aa | 10115 | } |
a28509cc AL |
10116 | } |
10117 | else if (PL_regkind[(U8)op] == BRANCH) { | |
be8e71aa | 10118 | assert(next); |
786e8c11 | 10119 | DUMPUNTIL(NEXTOPER(node), next); |
a28509cc AL |
10120 | } |
10121 | else if ( PL_regkind[(U8)op] == TRIE ) { | |
7f69552c | 10122 | const regnode *this_trie = node; |
1de06328 | 10123 | const char op = OP(node); |
647f639f | 10124 | const U32 n = ARG(node); |
1de06328 | 10125 | const reg_ac_data * const ac = op>=AHOCORASICK ? |
f8fc2ecf | 10126 | (reg_ac_data *)ri->data->data[n] : |
1de06328 | 10127 | NULL; |
3251b653 NC |
10128 | const reg_trie_data * const trie = |
10129 | (reg_trie_data*)ri->data->data[op<AHOCORASICK ? n : ac->trie]; | |
2b8b4781 | 10130 | #ifdef DEBUGGING |
502c6561 | 10131 | AV *const trie_words = MUTABLE_AV(ri->data->data[n + TRIE_WORDS_OFFSET]); |
2b8b4781 | 10132 | #endif |
786e8c11 | 10133 | const regnode *nextbranch= NULL; |
a28509cc | 10134 | I32 word_idx; |
76f68e9b | 10135 | sv_setpvs(sv, ""); |
786e8c11 | 10136 | for (word_idx= 0; word_idx < (I32)trie->wordcount; word_idx++) { |
2b8b4781 | 10137 | SV ** const elem_ptr = av_fetch(trie_words,word_idx,0); |
786e8c11 YO |
10138 | |
10139 | PerlIO_printf(Perl_debug_log, "%*s%s ", | |
10140 | (int)(2*(indent+3)), "", | |
10141 | elem_ptr ? pv_pretty(sv, SvPV_nolen_const(*elem_ptr), SvCUR(*elem_ptr), 60, | |
ab3bbdeb YO |
10142 | PL_colors[0], PL_colors[1], |
10143 | (SvUTF8(*elem_ptr) ? PERL_PV_ESCAPE_UNI : 0) | | |
95b611b0 | 10144 | PERL_PV_PRETTY_ELLIPSES | |
7f69552c | 10145 | PERL_PV_PRETTY_LTGT |
786e8c11 YO |
10146 | ) |
10147 | : "???" | |
10148 | ); | |
10149 | if (trie->jump) { | |
40d049e4 | 10150 | U16 dist= trie->jump[word_idx+1]; |
70685ca0 JH |
10151 | PerlIO_printf(Perl_debug_log, "(%"UVuf")\n", |
10152 | (UV)((dist ? this_trie + dist : next) - start)); | |
786e8c11 YO |
10153 | if (dist) { |
10154 | if (!nextbranch) | |
24b23f37 | 10155 | nextbranch= this_trie + trie->jump[0]; |
7f69552c YO |
10156 | DUMPUNTIL(this_trie + dist, nextbranch); |
10157 | } | |
786e8c11 YO |
10158 | if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH) |
10159 | nextbranch= regnext((regnode *)nextbranch); | |
10160 | } else { | |
10161 | PerlIO_printf(Perl_debug_log, "\n"); | |
a28509cc | 10162 | } |
786e8c11 YO |
10163 | } |
10164 | if (last && next > last) | |
10165 | node= last; | |
10166 | else | |
10167 | node= next; | |
a28509cc | 10168 | } |
786e8c11 YO |
10169 | else if ( op == CURLY ) { /* "next" might be very big: optimizer */ |
10170 | DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS, | |
10171 | NEXTOPER(node) + EXTRA_STEP_2ARGS + 1); | |
a28509cc AL |
10172 | } |
10173 | else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) { | |
be8e71aa | 10174 | assert(next); |
786e8c11 | 10175 | DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS, next); |
a28509cc AL |
10176 | } |
10177 | else if ( op == PLUS || op == STAR) { | |
786e8c11 | 10178 | DUMPUNTIL(NEXTOPER(node), NEXTOPER(node) + 1); |
a28509cc AL |
10179 | } |
10180 | else if (op == ANYOF) { | |
10181 | /* arglen 1 + class block */ | |
10182 | node += 1 + ((ANYOF_FLAGS(node) & ANYOF_LARGE) | |
10183 | ? ANYOF_CLASS_SKIP : ANYOF_SKIP); | |
10184 | node = NEXTOPER(node); | |
10185 | } | |
10186 | else if (PL_regkind[(U8)op] == EXACT) { | |
10187 | /* Literal string, where present. */ | |
10188 | node += NODE_SZ_STR(node) - 1; | |
10189 | node = NEXTOPER(node); | |
10190 | } | |
10191 | else { | |
10192 | node = NEXTOPER(node); | |
10193 | node += regarglen[(U8)op]; | |
10194 | } | |
10195 | if (op == CURLYX || op == OPEN) | |
786e8c11 | 10196 | indent++; |
a28509cc | 10197 | } |
3dab1dad | 10198 | CLEAR_OPTSTART; |
786e8c11 | 10199 | #ifdef DEBUG_DUMPUNTIL |
70685ca0 | 10200 | PerlIO_printf(Perl_debug_log, "--- %d\n", (int)indent); |
786e8c11 | 10201 | #endif |
1de06328 | 10202 | return node; |
a28509cc AL |
10203 | } |
10204 | ||
10205 | #endif /* DEBUGGING */ | |
10206 | ||
241d1a3b NC |
10207 | /* |
10208 | * Local variables: | |
10209 | * c-indentation-style: bsd | |
10210 | * c-basic-offset: 4 | |
10211 | * indent-tabs-mode: t | |
10212 | * End: | |
10213 | * | |
37442d52 RGS |
10214 | * ex: set ts=8 sts=4 sw=4 noet: |
10215 | */ |