]> git.vpit.fr Git - perl/modules/re-engine-Hooks.git/blob - src/5017000/regcomp.c
Fix infinite recursion with perl 5.17.[12]
[perl/modules/re-engine-Hooks.git] / src / 5017000 / regcomp.c
1 /*    regcomp.c
2  */
3
4 /*
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"]
8  */
9
10 /* This file contains functions for compiling a regular expression.  See
11  * also regexec.c which funnily enough, contains functions for executing
12  * a regular expression.
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.
18  */
19
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
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
34 #ifdef PERL_EXT_RE_BUILD
35 #include "re_top.h"
36 #endif
37
38 /*
39  * pregcomp and pregexec -- regsub and regerror are not used in perl
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  ****
61  ****    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
62  ****    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
63  ****    by Larry Wall and others
64  ****
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
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"
74 #define PERL_IN_REGCOMP_C
75 #include "perl.h"
76
77 #ifndef PERL_IN_XSUB_RE
78 #include "re_defs.h"
79 #endif
80
81 #define REG_COMP_C
82 #ifdef PERL_IN_XSUB_RE
83 #  include "re_comp.h"
84 #else
85 #  include "regcomp.h"
86 #endif
87
88 #include "dquote_static.c"
89 #ifndef PERL_IN_XSUB_RE
90 #  include "charclass_invlists.h"
91 #endif
92
93 #define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)
94
95 #ifdef op
96 #undef op
97 #endif /* op */
98
99 #ifdef MSDOS
100 #  if defined(BUGGY_MSC6)
101  /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
102 #    pragma optimize("a",off)
103  /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
104 #    pragma optimize("w",on )
105 #  endif /* BUGGY_MSC6 */
106 #endif /* MSDOS */
107
108 #ifndef STATIC
109 #define STATIC  static
110 #endif
111
112 typedef struct RExC_state_t {
113     U32         flags;                  /* are we folding, multilining? */
114     char        *precomp;               /* uncompiled string. */
115     REGEXP      *rx_sv;                 /* The SV that is the regexp. */
116     regexp      *rx;                    /* perl core regexp structure */
117     regexp_internal     *rxi;           /* internal data for regexp object pprivate field */        
118     char        *start;                 /* Start of input for compile */
119     char        *end;                   /* End of input for compile */
120     char        *parse;                 /* Input-scan pointer. */
121     I32         whilem_seen;            /* number of WHILEM in this expr */
122     regnode     *emit_start;            /* Start of emitted-code area */
123     regnode     *emit_bound;            /* First regnode outside of the allocated space */
124     regnode     *emit;                  /* Code-emit pointer; &regdummy = don't = compiling */
125     I32         naughty;                /* How bad is this pattern? */
126     I32         sawback;                /* Did we see \1, ...? */
127     U32         seen;
128     I32         size;                   /* Code size. */
129     I32         npar;                   /* Capture buffer count, (OPEN). */
130     I32         cpar;                   /* Capture buffer count, (CLOSE). */
131     I32         nestroot;               /* root parens we are in - used by accept */
132     I32         extralen;
133     I32         seen_zerolen;
134     I32         seen_evals;
135     regnode     **open_parens;          /* pointers to open parens */
136     regnode     **close_parens;         /* pointers to close parens */
137     regnode     *opend;                 /* END node in program */
138     I32         utf8;           /* whether the pattern is utf8 or not */
139     I32         orig_utf8;      /* whether the pattern was originally in utf8 */
140                                 /* XXX use this for future optimisation of case
141                                  * where pattern must be upgraded to utf8. */
142     I32         uni_semantics;  /* If a d charset modifier should use unicode
143                                    rules, even if the pattern is not in
144                                    utf8 */
145     HV          *paren_names;           /* Paren names */
146     
147     regnode     **recurse;              /* Recurse regops */
148     I32         recurse_count;          /* Number of recurse regops */
149     I32         in_lookbehind;
150     I32         contains_locale;
151     I32         override_recoding;
152 #if ADD_TO_REGEXEC
153     char        *starttry;              /* -Dr: where regtry was called. */
154 #define RExC_starttry   (pRExC_state->starttry)
155 #endif
156 #ifdef DEBUGGING
157     const char  *lastparse;
158     I32         lastnum;
159     AV          *paren_name_list;       /* idx -> name */
160 #define RExC_lastparse  (pRExC_state->lastparse)
161 #define RExC_lastnum    (pRExC_state->lastnum)
162 #define RExC_paren_name_list    (pRExC_state->paren_name_list)
163 #endif
164 } RExC_state_t;
165
166 #define RExC_flags      (pRExC_state->flags)
167 #define RExC_precomp    (pRExC_state->precomp)
168 #define RExC_rx_sv      (pRExC_state->rx_sv)
169 #define RExC_rx         (pRExC_state->rx)
170 #define RExC_rxi        (pRExC_state->rxi)
171 #define RExC_start      (pRExC_state->start)
172 #define RExC_end        (pRExC_state->end)
173 #define RExC_parse      (pRExC_state->parse)
174 #define RExC_whilem_seen        (pRExC_state->whilem_seen)
175 #ifdef RE_TRACK_PATTERN_OFFSETS
176 #define RExC_offsets    (pRExC_state->rxi->u.offsets) /* I am not like the others */
177 #endif
178 #define RExC_emit       (pRExC_state->emit)
179 #define RExC_emit_start (pRExC_state->emit_start)
180 #define RExC_emit_bound (pRExC_state->emit_bound)
181 #define RExC_naughty    (pRExC_state->naughty)
182 #define RExC_sawback    (pRExC_state->sawback)
183 #define RExC_seen       (pRExC_state->seen)
184 #define RExC_size       (pRExC_state->size)
185 #define RExC_npar       (pRExC_state->npar)
186 #define RExC_nestroot   (pRExC_state->nestroot)
187 #define RExC_extralen   (pRExC_state->extralen)
188 #define RExC_seen_zerolen       (pRExC_state->seen_zerolen)
189 #define RExC_seen_evals (pRExC_state->seen_evals)
190 #define RExC_utf8       (pRExC_state->utf8)
191 #define RExC_uni_semantics      (pRExC_state->uni_semantics)
192 #define RExC_orig_utf8  (pRExC_state->orig_utf8)
193 #define RExC_open_parens        (pRExC_state->open_parens)
194 #define RExC_close_parens       (pRExC_state->close_parens)
195 #define RExC_opend      (pRExC_state->opend)
196 #define RExC_paren_names        (pRExC_state->paren_names)
197 #define RExC_recurse    (pRExC_state->recurse)
198 #define RExC_recurse_count      (pRExC_state->recurse_count)
199 #define RExC_in_lookbehind      (pRExC_state->in_lookbehind)
200 #define RExC_contains_locale    (pRExC_state->contains_locale)
201 #define RExC_override_recoding  (pRExC_state->override_recoding)
202
203
204 #define ISMULT1(c)      ((c) == '*' || (c) == '+' || (c) == '?')
205 #define ISMULT2(s)      ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
206         ((*s) == '{' && regcurly(s)))
207
208 #ifdef SPSTART
209 #undef SPSTART          /* dratted cpp namespace... */
210 #endif
211 /*
212  * Flags to be passed up and down.
213  */
214 #define WORST           0       /* Worst case. */
215 #define HASWIDTH        0x01    /* Known to match non-null strings. */
216
217 /* Simple enough to be STAR/PLUS operand, in an EXACT node must be a single
218  * character, and if utf8, must be invariant.  Note that this is not the same
219  * thing as REGNODE_SIMPLE */
220 #define SIMPLE          0x02
221 #define SPSTART         0x04    /* Starts with * or +. */
222 #define TRYAGAIN        0x08    /* Weeded out a declaration. */
223 #define POSTPONED       0x10    /* (?1),(?&name), (??{...}) or similar */
224
225 #define REG_NODE_NUM(x) ((x) ? (int)((x)-RExC_emit_start) : -1)
226
227 /* whether trie related optimizations are enabled */
228 #if PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION
229 #define TRIE_STUDY_OPT
230 #define FULL_TRIE_STUDY
231 #define TRIE_STCLASS
232 #endif
233
234
235
236 #define PBYTE(u8str,paren) ((U8*)(u8str))[(paren) >> 3]
237 #define PBITVAL(paren) (1 << ((paren) & 7))
238 #define PAREN_TEST(u8str,paren) ( PBYTE(u8str,paren) & PBITVAL(paren))
239 #define PAREN_SET(u8str,paren) PBYTE(u8str,paren) |= PBITVAL(paren)
240 #define PAREN_UNSET(u8str,paren) PBYTE(u8str,paren) &= (~PBITVAL(paren))
241
242 /* If not already in utf8, do a longjmp back to the beginning */
243 #define UTF8_LONGJMP 42 /* Choose a value not likely to ever conflict */
244 #define REQUIRE_UTF8    STMT_START {                                       \
245                                      if (! UTF) JMPENV_JUMP(UTF8_LONGJMP); \
246                         } STMT_END
247
248 /* About scan_data_t.
249
250   During optimisation we recurse through the regexp program performing
251   various inplace (keyhole style) optimisations. In addition study_chunk
252   and scan_commit populate this data structure with information about
253   what strings MUST appear in the pattern. We look for the longest 
254   string that must appear at a fixed location, and we look for the
255   longest string that may appear at a floating location. So for instance
256   in the pattern:
257   
258     /FOO[xX]A.*B[xX]BAR/
259     
260   Both 'FOO' and 'A' are fixed strings. Both 'B' and 'BAR' are floating
261   strings (because they follow a .* construct). study_chunk will identify
262   both FOO and BAR as being the longest fixed and floating strings respectively.
263   
264   The strings can be composites, for instance
265   
266      /(f)(o)(o)/
267      
268   will result in a composite fixed substring 'foo'.
269   
270   For each string some basic information is maintained:
271   
272   - offset or min_offset
273     This is the position the string must appear at, or not before.
274     It also implicitly (when combined with minlenp) tells us how many
275     characters must match before the string we are searching for.
276     Likewise when combined with minlenp and the length of the string it
277     tells us how many characters must appear after the string we have 
278     found.
279   
280   - max_offset
281     Only used for floating strings. This is the rightmost point that
282     the string can appear at. If set to I32 max it indicates that the
283     string can occur infinitely far to the right.
284   
285   - minlenp
286     A pointer to the minimum length of the pattern that the string 
287     was found inside. This is important as in the case of positive 
288     lookahead or positive lookbehind we can have multiple patterns 
289     involved. Consider
290     
291     /(?=FOO).*F/
292     
293     The minimum length of the pattern overall is 3, the minimum length
294     of the lookahead part is 3, but the minimum length of the part that
295     will actually match is 1. So 'FOO's minimum length is 3, but the 
296     minimum length for the F is 1. This is important as the minimum length
297     is used to determine offsets in front of and behind the string being 
298     looked for.  Since strings can be composites this is the length of the
299     pattern at the time it was committed with a scan_commit. Note that
300     the length is calculated by study_chunk, so that the minimum lengths
301     are not known until the full pattern has been compiled, thus the 
302     pointer to the value.
303   
304   - lookbehind
305   
306     In the case of lookbehind the string being searched for can be
307     offset past the start point of the final matching string. 
308     If this value was just blithely removed from the min_offset it would
309     invalidate some of the calculations for how many chars must match
310     before or after (as they are derived from min_offset and minlen and
311     the length of the string being searched for). 
312     When the final pattern is compiled and the data is moved from the
313     scan_data_t structure into the regexp structure the information
314     about lookbehind is factored in, with the information that would 
315     have been lost precalculated in the end_shift field for the 
316     associated string.
317
318   The fields pos_min and pos_delta are used to store the minimum offset
319   and the delta to the maximum offset at the current point in the pattern.    
320
321 */
322
323 typedef struct scan_data_t {
324     /*I32 len_min;      unused */
325     /*I32 len_delta;    unused */
326     I32 pos_min;
327     I32 pos_delta;
328     SV *last_found;
329     I32 last_end;           /* min value, <0 unless valid. */
330     I32 last_start_min;
331     I32 last_start_max;
332     SV **longest;           /* Either &l_fixed, or &l_float. */
333     SV *longest_fixed;      /* longest fixed string found in pattern */
334     I32 offset_fixed;       /* offset where it starts */
335     I32 *minlen_fixed;      /* pointer to the minlen relevant to the string */
336     I32 lookbehind_fixed;   /* is the position of the string modfied by LB */
337     SV *longest_float;      /* longest floating string found in pattern */
338     I32 offset_float_min;   /* earliest point in string it can appear */
339     I32 offset_float_max;   /* latest point in string it can appear */
340     I32 *minlen_float;      /* pointer to the minlen relevant to the string */
341     I32 lookbehind_float;   /* is the position of the string modified by LB */
342     I32 flags;
343     I32 whilem_c;
344     I32 *last_closep;
345     struct regnode_charclass_class *start_class;
346 } scan_data_t;
347
348 /*
349  * Forward declarations for pregcomp()'s friends.
350  */
351
352 static const scan_data_t zero_scan_data =
353   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
354
355 #define SF_BEFORE_EOL           (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
356 #define SF_BEFORE_SEOL          0x0001
357 #define SF_BEFORE_MEOL          0x0002
358 #define SF_FIX_BEFORE_EOL       (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
359 #define SF_FL_BEFORE_EOL        (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
360
361 #ifdef NO_UNARY_PLUS
362 #  define SF_FIX_SHIFT_EOL      (0+2)
363 #  define SF_FL_SHIFT_EOL               (0+4)
364 #else
365 #  define SF_FIX_SHIFT_EOL      (+2)
366 #  define SF_FL_SHIFT_EOL               (+4)
367 #endif
368
369 #define SF_FIX_BEFORE_SEOL      (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
370 #define SF_FIX_BEFORE_MEOL      (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
371
372 #define SF_FL_BEFORE_SEOL       (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
373 #define SF_FL_BEFORE_MEOL       (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
374 #define SF_IS_INF               0x0040
375 #define SF_HAS_PAR              0x0080
376 #define SF_IN_PAR               0x0100
377 #define SF_HAS_EVAL             0x0200
378 #define SCF_DO_SUBSTR           0x0400
379 #define SCF_DO_STCLASS_AND      0x0800
380 #define SCF_DO_STCLASS_OR       0x1000
381 #define SCF_DO_STCLASS          (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
382 #define SCF_WHILEM_VISITED_POS  0x2000
383
384 #define SCF_TRIE_RESTUDY        0x4000 /* Do restudy? */
385 #define SCF_SEEN_ACCEPT         0x8000 
386
387 #define UTF cBOOL(RExC_utf8)
388
389 /* The enums for all these are ordered so things work out correctly */
390 #define LOC (get_regex_charset(RExC_flags) == REGEX_LOCALE_CHARSET)
391 #define DEPENDS_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_DEPENDS_CHARSET)
392 #define UNI_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_UNICODE_CHARSET)
393 #define AT_LEAST_UNI_SEMANTICS (get_regex_charset(RExC_flags) >= REGEX_UNICODE_CHARSET)
394 #define ASCII_RESTRICTED (get_regex_charset(RExC_flags) == REGEX_ASCII_RESTRICTED_CHARSET)
395 #define MORE_ASCII_RESTRICTED (get_regex_charset(RExC_flags) == REGEX_ASCII_MORE_RESTRICTED_CHARSET)
396 #define AT_LEAST_ASCII_RESTRICTED (get_regex_charset(RExC_flags) >= REGEX_ASCII_RESTRICTED_CHARSET)
397
398 #define FOLD cBOOL(RExC_flags & RXf_PMf_FOLD)
399
400 #define OOB_UNICODE             12345678
401 #define OOB_NAMEDCLASS          -1
402
403 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
404 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
405
406
407 /* length of regex to show in messages that don't mark a position within */
408 #define RegexLengthToShowInErrorMessages 127
409
410 /*
411  * If MARKER[12] are adjusted, be sure to adjust the constants at the top
412  * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
413  * op/pragma/warn/regcomp.
414  */
415 #define MARKER1 "<-- HERE"    /* marker as it appears in the description */
416 #define MARKER2 " <-- HERE "  /* marker as it appears within the regex */
417
418 #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/"
419
420 /*
421  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
422  * arg. Show regex, up to a maximum length. If it's too long, chop and add
423  * "...".
424  */
425 #define _FAIL(code) STMT_START {                                        \
426     const char *ellipses = "";                                          \
427     IV len = RExC_end - RExC_precomp;                                   \
428                                                                         \
429     if (!SIZE_ONLY)                                                     \
430         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);                   \
431     if (len > RegexLengthToShowInErrorMessages) {                       \
432         /* chop 10 shorter than the max, to ensure meaning of "..." */  \
433         len = RegexLengthToShowInErrorMessages - 10;                    \
434         ellipses = "...";                                               \
435     }                                                                   \
436     code;                                                               \
437 } STMT_END
438
439 #define FAIL(msg) _FAIL(                            \
440     Perl_croak(aTHX_ "%s in regex m/%.*s%s/",       \
441             msg, (int)len, RExC_precomp, ellipses))
442
443 #define FAIL2(msg,arg) _FAIL(                       \
444     Perl_croak(aTHX_ msg " in regex m/%.*s%s/",     \
445             arg, (int)len, RExC_precomp, ellipses))
446
447 /*
448  * Simple_vFAIL -- like FAIL, but marks the current location in the scan
449  */
450 #define Simple_vFAIL(m) STMT_START {                                    \
451     const IV offset = RExC_parse - RExC_precomp;                        \
452     Perl_croak(aTHX_ "%s" REPORT_LOCATION,                              \
453             m, (int)offset, RExC_precomp, RExC_precomp + offset);       \
454 } STMT_END
455
456 /*
457  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
458  */
459 #define vFAIL(m) STMT_START {                           \
460     if (!SIZE_ONLY)                                     \
461         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
462     Simple_vFAIL(m);                                    \
463 } STMT_END
464
465 /*
466  * Like Simple_vFAIL(), but accepts two arguments.
467  */
468 #define Simple_vFAIL2(m,a1) STMT_START {                        \
469     const IV offset = RExC_parse - RExC_precomp;                        \
470     S_re_croak2(aTHX_ m, REPORT_LOCATION, a1,                   \
471             (int)offset, RExC_precomp, RExC_precomp + offset);  \
472 } STMT_END
473
474 /*
475  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
476  */
477 #define vFAIL2(m,a1) STMT_START {                       \
478     if (!SIZE_ONLY)                                     \
479         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
480     Simple_vFAIL2(m, a1);                               \
481 } STMT_END
482
483
484 /*
485  * Like Simple_vFAIL(), but accepts three arguments.
486  */
487 #define Simple_vFAIL3(m, a1, a2) STMT_START {                   \
488     const IV offset = RExC_parse - RExC_precomp;                \
489     S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2,               \
490             (int)offset, RExC_precomp, RExC_precomp + offset);  \
491 } STMT_END
492
493 /*
494  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
495  */
496 #define vFAIL3(m,a1,a2) STMT_START {                    \
497     if (!SIZE_ONLY)                                     \
498         SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx_sv);   \
499     Simple_vFAIL3(m, a1, a2);                           \
500 } STMT_END
501
502 /*
503  * Like Simple_vFAIL(), but accepts four arguments.
504  */
505 #define Simple_vFAIL4(m, a1, a2, a3) STMT_START {               \
506     const IV offset = RExC_parse - RExC_precomp;                \
507     S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,           \
508             (int)offset, RExC_precomp, RExC_precomp + offset);  \
509 } STMT_END
510
511 #define ckWARNreg(loc,m) STMT_START {                                   \
512     const IV offset = loc - RExC_precomp;                               \
513     Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,      \
514             (int)offset, RExC_precomp, RExC_precomp + offset);          \
515 } STMT_END
516
517 #define ckWARNregdep(loc,m) STMT_START {                                \
518     const IV offset = loc - RExC_precomp;                               \
519     Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP),     \
520             m REPORT_LOCATION,                                          \
521             (int)offset, RExC_precomp, RExC_precomp + offset);          \
522 } STMT_END
523
524 #define ckWARN2regdep(loc,m, a1) STMT_START {                           \
525     const IV offset = loc - RExC_precomp;                               \
526     Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP),     \
527             m REPORT_LOCATION,                                          \
528             a1, (int)offset, RExC_precomp, RExC_precomp + offset);      \
529 } STMT_END
530
531 #define ckWARN2reg(loc, m, a1) STMT_START {                             \
532     const IV offset = loc - RExC_precomp;                               \
533     Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,      \
534             a1, (int)offset, RExC_precomp, RExC_precomp + offset);      \
535 } STMT_END
536
537 #define vWARN3(loc, m, a1, a2) STMT_START {                             \
538     const IV offset = loc - RExC_precomp;                               \
539     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
540             a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset);  \
541 } STMT_END
542
543 #define ckWARN3reg(loc, m, a1, a2) STMT_START {                         \
544     const IV offset = loc - RExC_precomp;                               \
545     Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,      \
546             a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset);  \
547 } STMT_END
548
549 #define vWARN4(loc, m, a1, a2, a3) STMT_START {                         \
550     const IV offset = loc - RExC_precomp;                               \
551     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
552             a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
553 } STMT_END
554
555 #define ckWARN4reg(loc, m, a1, a2, a3) STMT_START {                     \
556     const IV offset = loc - RExC_precomp;                               \
557     Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,      \
558             a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
559 } STMT_END
560
561 #define vWARN5(loc, m, a1, a2, a3, a4) STMT_START {                     \
562     const IV offset = loc - RExC_precomp;                               \
563     Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION,         \
564             a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \
565 } STMT_END
566
567
568 /* Allow for side effects in s */
569 #define REGC(c,s) STMT_START {                  \
570     if (!SIZE_ONLY) *(s) = (c); else (void)(s); \
571 } STMT_END
572
573 /* Macros for recording node offsets.   20001227 mjd@plover.com 
574  * Nodes are numbered 1, 2, 3, 4.  Node #n's position is recorded in
575  * element 2*n-1 of the array.  Element #2n holds the byte length node #n.
576  * Element 0 holds the number n.
577  * Position is 1 indexed.
578  */
579 #ifndef RE_TRACK_PATTERN_OFFSETS
580 #define Set_Node_Offset_To_R(node,byte)
581 #define Set_Node_Offset(node,byte)
582 #define Set_Cur_Node_Offset
583 #define Set_Node_Length_To_R(node,len)
584 #define Set_Node_Length(node,len)
585 #define Set_Node_Cur_Length(node)
586 #define Node_Offset(n) 
587 #define Node_Length(n) 
588 #define Set_Node_Offset_Length(node,offset,len)
589 #define ProgLen(ri) ri->u.proglen
590 #define SetProgLen(ri,x) ri->u.proglen = x
591 #else
592 #define ProgLen(ri) ri->u.offsets[0]
593 #define SetProgLen(ri,x) ri->u.offsets[0] = x
594 #define Set_Node_Offset_To_R(node,byte) STMT_START {                    \
595     if (! SIZE_ONLY) {                                                  \
596         MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n",         \
597                     __LINE__, (int)(node), (int)(byte)));               \
598         if((node) < 0) {                                                \
599             Perl_croak(aTHX_ "value of node is %d in Offset macro", (int)(node)); \
600         } else {                                                        \
601             RExC_offsets[2*(node)-1] = (byte);                          \
602         }                                                               \
603     }                                                                   \
604 } STMT_END
605
606 #define Set_Node_Offset(node,byte) \
607     Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
608 #define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
609
610 #define Set_Node_Length_To_R(node,len) STMT_START {                     \
611     if (! SIZE_ONLY) {                                                  \
612         MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n",           \
613                 __LINE__, (int)(node), (int)(len)));                    \
614         if((node) < 0) {                                                \
615             Perl_croak(aTHX_ "value of node is %d in Length macro", (int)(node)); \
616         } else {                                                        \
617             RExC_offsets[2*(node)] = (len);                             \
618         }                                                               \
619     }                                                                   \
620 } STMT_END
621
622 #define Set_Node_Length(node,len) \
623     Set_Node_Length_To_R((node)-RExC_emit_start, len)
624 #define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
625 #define Set_Node_Cur_Length(node) \
626     Set_Node_Length(node, RExC_parse - parse_start)
627
628 /* Get offsets and lengths */
629 #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
630 #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)])
631
632 #define Set_Node_Offset_Length(node,offset,len) STMT_START {    \
633     Set_Node_Offset_To_R((node)-RExC_emit_start, (offset));     \
634     Set_Node_Length_To_R((node)-RExC_emit_start, (len));        \
635 } STMT_END
636 #endif
637
638 #if PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS
639 #define EXPERIMENTAL_INPLACESCAN
640 #endif /*PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS*/
641
642 #define DEBUG_STUDYDATA(str,data,depth)                              \
643 DEBUG_OPTIMISE_MORE_r(if(data){                                      \
644     PerlIO_printf(Perl_debug_log,                                    \
645         "%*s" str "Pos:%"IVdf"/%"IVdf                                \
646         " Flags: 0x%"UVXf" Whilem_c: %"IVdf" Lcp: %"IVdf" %s",       \
647         (int)(depth)*2, "",                                          \
648         (IV)((data)->pos_min),                                       \
649         (IV)((data)->pos_delta),                                     \
650         (UV)((data)->flags),                                         \
651         (IV)((data)->whilem_c),                                      \
652         (IV)((data)->last_closep ? *((data)->last_closep) : -1),     \
653         is_inf ? "INF " : ""                                         \
654     );                                                               \
655     if ((data)->last_found)                                          \
656         PerlIO_printf(Perl_debug_log,                                \
657             "Last:'%s' %"IVdf":%"IVdf"/%"IVdf" %sFixed:'%s' @ %"IVdf \
658             " %sFloat: '%s' @ %"IVdf"/%"IVdf"",                      \
659             SvPVX_const((data)->last_found),                         \
660             (IV)((data)->last_end),                                  \
661             (IV)((data)->last_start_min),                            \
662             (IV)((data)->last_start_max),                            \
663             ((data)->longest &&                                      \
664              (data)->longest==&((data)->longest_fixed)) ? "*" : "",  \
665             SvPVX_const((data)->longest_fixed),                      \
666             (IV)((data)->offset_fixed),                              \
667             ((data)->longest &&                                      \
668              (data)->longest==&((data)->longest_float)) ? "*" : "",  \
669             SvPVX_const((data)->longest_float),                      \
670             (IV)((data)->offset_float_min),                          \
671             (IV)((data)->offset_float_max)                           \
672         );                                                           \
673     PerlIO_printf(Perl_debug_log,"\n");                              \
674 });
675
676 static void clear_re(pTHX_ void *r);
677
678 /* Mark that we cannot extend a found fixed substring at this point.
679    Update the longest found anchored substring and the longest found
680    floating substrings if needed. */
681
682 STATIC void
683 S_scan_commit(pTHX_ const RExC_state_t *pRExC_state, scan_data_t *data, I32 *minlenp, int is_inf)
684 {
685     const STRLEN l = CHR_SVLEN(data->last_found);
686     const STRLEN old_l = CHR_SVLEN(*data->longest);
687     GET_RE_DEBUG_FLAGS_DECL;
688
689     PERL_ARGS_ASSERT_SCAN_COMMIT;
690
691     if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
692         SvSetMagicSV(*data->longest, data->last_found);
693         if (*data->longest == data->longest_fixed) {
694             data->offset_fixed = l ? data->last_start_min : data->pos_min;
695             if (data->flags & SF_BEFORE_EOL)
696                 data->flags
697                     |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
698             else
699                 data->flags &= ~SF_FIX_BEFORE_EOL;
700             data->minlen_fixed=minlenp;
701             data->lookbehind_fixed=0;
702         }
703         else { /* *data->longest == data->longest_float */
704             data->offset_float_min = l ? data->last_start_min : data->pos_min;
705             data->offset_float_max = (l
706                                       ? data->last_start_max
707                                       : data->pos_min + data->pos_delta);
708             if (is_inf || (U32)data->offset_float_max > (U32)I32_MAX)
709                 data->offset_float_max = I32_MAX;
710             if (data->flags & SF_BEFORE_EOL)
711                 data->flags
712                     |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
713             else
714                 data->flags &= ~SF_FL_BEFORE_EOL;
715             data->minlen_float=minlenp;
716             data->lookbehind_float=0;
717         }
718     }
719     SvCUR_set(data->last_found, 0);
720     {
721         SV * const sv = data->last_found;
722         if (SvUTF8(sv) && SvMAGICAL(sv)) {
723             MAGIC * const mg = mg_find(sv, PERL_MAGIC_utf8);
724             if (mg)
725                 mg->mg_len = 0;
726         }
727     }
728     data->last_end = -1;
729     data->flags &= ~SF_BEFORE_EOL;
730     DEBUG_STUDYDATA("commit: ",data,0);
731 }
732
733 /* Can match anything (initialization) */
734 STATIC void
735 S_cl_anything(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
736 {
737     PERL_ARGS_ASSERT_CL_ANYTHING;
738
739     ANYOF_BITMAP_SETALL(cl);
740     cl->flags = ANYOF_CLASS|ANYOF_EOS|ANYOF_UNICODE_ALL
741                 |ANYOF_LOC_NONBITMAP_FOLD|ANYOF_NON_UTF8_LATIN1_ALL;
742
743     /* If any portion of the regex is to operate under locale rules,
744      * initialization includes it.  The reason this isn't done for all regexes
745      * is that the optimizer was written under the assumption that locale was
746      * all-or-nothing.  Given the complexity and lack of documentation in the
747      * optimizer, and that there are inadequate test cases for locale, so many
748      * parts of it may not work properly, it is safest to avoid locale unless
749      * necessary. */
750     if (RExC_contains_locale) {
751         ANYOF_CLASS_SETALL(cl);     /* /l uses class */
752         cl->flags |= ANYOF_LOCALE;
753     }
754     else {
755         ANYOF_CLASS_ZERO(cl);       /* Only /l uses class now */
756     }
757 }
758
759 /* Can match anything (initialization) */
760 STATIC int
761 S_cl_is_anything(const struct regnode_charclass_class *cl)
762 {
763     int value;
764
765     PERL_ARGS_ASSERT_CL_IS_ANYTHING;
766
767     for (value = 0; value <= ANYOF_MAX; value += 2)
768         if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
769             return 1;
770     if (!(cl->flags & ANYOF_UNICODE_ALL))
771         return 0;
772     if (!ANYOF_BITMAP_TESTALLSET((const void*)cl))
773         return 0;
774     return 1;
775 }
776
777 /* Can match anything (initialization) */
778 STATIC void
779 S_cl_init(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
780 {
781     PERL_ARGS_ASSERT_CL_INIT;
782
783     Zero(cl, 1, struct regnode_charclass_class);
784     cl->type = ANYOF;
785     cl_anything(pRExC_state, cl);
786     ARG_SET(cl, ANYOF_NONBITMAP_EMPTY);
787 }
788
789 /* These two functions currently do the exact same thing */
790 #define cl_init_zero            S_cl_init
791
792 /* 'AND' a given class with another one.  Can create false positives.  'cl'
793  * should not be inverted.  'and_with->flags & ANYOF_CLASS' should be 0 if
794  * 'and_with' is a regnode_charclass instead of a regnode_charclass_class. */
795 STATIC void
796 S_cl_and(struct regnode_charclass_class *cl,
797         const struct regnode_charclass_class *and_with)
798 {
799     PERL_ARGS_ASSERT_CL_AND;
800
801     assert(and_with->type == ANYOF);
802
803     /* I (khw) am not sure all these restrictions are necessary XXX */
804     if (!(ANYOF_CLASS_TEST_ANY_SET(and_with))
805         && !(ANYOF_CLASS_TEST_ANY_SET(cl))
806         && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
807         && !(and_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
808         && !(cl->flags & ANYOF_LOC_NONBITMAP_FOLD)) {
809         int i;
810
811         if (and_with->flags & ANYOF_INVERT)
812             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
813                 cl->bitmap[i] &= ~and_with->bitmap[i];
814         else
815             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
816                 cl->bitmap[i] &= and_with->bitmap[i];
817     } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
818
819     if (and_with->flags & ANYOF_INVERT) {
820
821         /* Here, the and'ed node is inverted.  Get the AND of the flags that
822          * aren't affected by the inversion.  Those that are affected are
823          * handled individually below */
824         U8 affected_flags = cl->flags & ~INVERSION_UNAFFECTED_FLAGS;
825         cl->flags &= (and_with->flags & INVERSION_UNAFFECTED_FLAGS);
826         cl->flags |= affected_flags;
827
828         /* We currently don't know how to deal with things that aren't in the
829          * bitmap, but we know that the intersection is no greater than what
830          * is already in cl, so let there be false positives that get sorted
831          * out after the synthetic start class succeeds, and the node is
832          * matched for real. */
833
834         /* The inversion of these two flags indicate that the resulting
835          * intersection doesn't have them */
836         if (and_with->flags & ANYOF_UNICODE_ALL) {
837             cl->flags &= ~ANYOF_UNICODE_ALL;
838         }
839         if (and_with->flags & ANYOF_NON_UTF8_LATIN1_ALL) {
840             cl->flags &= ~ANYOF_NON_UTF8_LATIN1_ALL;
841         }
842     }
843     else {   /* and'd node is not inverted */
844         U8 outside_bitmap_but_not_utf8; /* Temp variable */
845
846         if (! ANYOF_NONBITMAP(and_with)) {
847
848             /* Here 'and_with' doesn't match anything outside the bitmap
849              * (except possibly ANYOF_UNICODE_ALL), which means the
850              * intersection can't either, except for ANYOF_UNICODE_ALL, in
851              * which case we don't know what the intersection is, but it's no
852              * greater than what cl already has, so can just leave it alone,
853              * with possible false positives */
854             if (! (and_with->flags & ANYOF_UNICODE_ALL)) {
855                 ARG_SET(cl, ANYOF_NONBITMAP_EMPTY);
856                 cl->flags &= ~ANYOF_NONBITMAP_NON_UTF8;
857             }
858         }
859         else if (! ANYOF_NONBITMAP(cl)) {
860
861             /* Here, 'and_with' does match something outside the bitmap, and cl
862              * doesn't have a list of things to match outside the bitmap.  If
863              * cl can match all code points above 255, the intersection will
864              * be those above-255 code points that 'and_with' matches.  If cl
865              * can't match all Unicode code points, it means that it can't
866              * match anything outside the bitmap (since the 'if' that got us
867              * into this block tested for that), so we leave the bitmap empty.
868              */
869             if (cl->flags & ANYOF_UNICODE_ALL) {
870                 ARG_SET(cl, ARG(and_with));
871
872                 /* and_with's ARG may match things that don't require UTF8.
873                  * And now cl's will too, in spite of this being an 'and'.  See
874                  * the comments below about the kludge */
875                 cl->flags |= and_with->flags & ANYOF_NONBITMAP_NON_UTF8;
876             }
877         }
878         else {
879             /* Here, both 'and_with' and cl match something outside the
880              * bitmap.  Currently we do not do the intersection, so just match
881              * whatever cl had at the beginning.  */
882         }
883
884
885         /* Take the intersection of the two sets of flags.  However, the
886          * ANYOF_NONBITMAP_NON_UTF8 flag is treated as an 'or'.  This is a
887          * kludge around the fact that this flag is not treated like the others
888          * which are initialized in cl_anything().  The way the optimizer works
889          * is that the synthetic start class (SSC) is initialized to match
890          * anything, and then the first time a real node is encountered, its
891          * values are AND'd with the SSC's with the result being the values of
892          * the real node.  However, there are paths through the optimizer where
893          * the AND never gets called, so those initialized bits are set
894          * inappropriately, which is not usually a big deal, as they just cause
895          * false positives in the SSC, which will just mean a probably
896          * imperceptible slow down in execution.  However this bit has a
897          * higher false positive consequence in that it can cause utf8.pm,
898          * utf8_heavy.pl ... to be loaded when not necessary, which is a much
899          * bigger slowdown and also causes significant extra memory to be used.
900          * In order to prevent this, the code now takes a different tack.  The
901          * bit isn't set unless some part of the regular expression needs it,
902          * but once set it won't get cleared.  This means that these extra
903          * modules won't get loaded unless there was some path through the
904          * pattern that would have required them anyway, and  so any false
905          * positives that occur by not ANDing them out when they could be
906          * aren't as severe as they would be if we treated this bit like all
907          * the others */
908         outside_bitmap_but_not_utf8 = (cl->flags | and_with->flags)
909                                       & ANYOF_NONBITMAP_NON_UTF8;
910         cl->flags &= and_with->flags;
911         cl->flags |= outside_bitmap_but_not_utf8;
912     }
913 }
914
915 /* 'OR' a given class with another one.  Can create false positives.  'cl'
916  * should not be inverted.  'or_with->flags & ANYOF_CLASS' should be 0 if
917  * 'or_with' is a regnode_charclass instead of a regnode_charclass_class. */
918 STATIC void
919 S_cl_or(const RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, const struct regnode_charclass_class *or_with)
920 {
921     PERL_ARGS_ASSERT_CL_OR;
922
923     if (or_with->flags & ANYOF_INVERT) {
924
925         /* Here, the or'd node is to be inverted.  This means we take the
926          * complement of everything not in the bitmap, but currently we don't
927          * know what that is, so give up and match anything */
928         if (ANYOF_NONBITMAP(or_with)) {
929             cl_anything(pRExC_state, cl);
930         }
931         /* We do not use
932          * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
933          *   <= (B1 | !B2) | (CL1 | !CL2)
934          * which is wasteful if CL2 is small, but we ignore CL2:
935          *   (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
936          * XXXX Can we handle case-fold?  Unclear:
937          *   (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
938          *   (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
939          */
940         else if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
941              && !(or_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
942              && !(cl->flags & ANYOF_LOC_NONBITMAP_FOLD) ) {
943             int i;
944
945             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
946                 cl->bitmap[i] |= ~or_with->bitmap[i];
947         } /* XXXX: logic is complicated otherwise */
948         else {
949             cl_anything(pRExC_state, cl);
950         }
951
952         /* And, we can just take the union of the flags that aren't affected
953          * by the inversion */
954         cl->flags |= or_with->flags & INVERSION_UNAFFECTED_FLAGS;
955
956         /* For the remaining flags:
957             ANYOF_UNICODE_ALL and inverted means to not match anything above
958                     255, which means that the union with cl should just be
959                     what cl has in it, so can ignore this flag
960             ANYOF_NON_UTF8_LATIN1_ALL and inverted means if not utf8 and ord
961                     is 127-255 to match them, but then invert that, so the
962                     union with cl should just be what cl has in it, so can
963                     ignore this flag
964          */
965     } else {    /* 'or_with' is not inverted */
966         /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
967         if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
968              && (!(or_with->flags & ANYOF_LOC_NONBITMAP_FOLD)
969                  || (cl->flags & ANYOF_LOC_NONBITMAP_FOLD)) ) {
970             int i;
971
972             /* OR char bitmap and class bitmap separately */
973             for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
974                 cl->bitmap[i] |= or_with->bitmap[i];
975             if (ANYOF_CLASS_TEST_ANY_SET(or_with)) {
976                 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
977                     cl->classflags[i] |= or_with->classflags[i];
978                 cl->flags |= ANYOF_CLASS;
979             }
980         }
981         else { /* XXXX: logic is complicated, leave it along for a moment. */
982             cl_anything(pRExC_state, cl);
983         }
984
985         if (ANYOF_NONBITMAP(or_with)) {
986
987             /* Use the added node's outside-the-bit-map match if there isn't a
988              * conflict.  If there is a conflict (both nodes match something
989              * outside the bitmap, but what they match outside is not the same
990              * pointer, and hence not easily compared until XXX we extend
991              * inversion lists this far), give up and allow the start class to
992              * match everything outside the bitmap.  If that stuff is all above
993              * 255, can just set UNICODE_ALL, otherwise caould be anything. */
994             if (! ANYOF_NONBITMAP(cl)) {
995                 ARG_SET(cl, ARG(or_with));
996             }
997             else if (ARG(cl) != ARG(or_with)) {
998
999                 if ((or_with->flags & ANYOF_NONBITMAP_NON_UTF8)) {
1000                     cl_anything(pRExC_state, cl);
1001                 }
1002                 else {
1003                     cl->flags |= ANYOF_UNICODE_ALL;
1004                 }
1005             }
1006         }
1007
1008         /* Take the union */
1009         cl->flags |= or_with->flags;
1010     }
1011 }
1012
1013 #define TRIE_LIST_ITEM(state,idx) (trie->states[state].trans.list)[ idx ]
1014 #define TRIE_LIST_CUR(state)  ( TRIE_LIST_ITEM( state, 0 ).forid )
1015 #define TRIE_LIST_LEN(state) ( TRIE_LIST_ITEM( state, 0 ).newstate )
1016 #define TRIE_LIST_USED(idx)  ( trie->states[state].trans.list ? (TRIE_LIST_CUR( idx ) - 1) : 0 )
1017
1018
1019 #ifdef DEBUGGING
1020 /*
1021    dump_trie(trie,widecharmap,revcharmap)
1022    dump_trie_interim_list(trie,widecharmap,revcharmap,next_alloc)
1023    dump_trie_interim_table(trie,widecharmap,revcharmap,next_alloc)
1024
1025    These routines dump out a trie in a somewhat readable format.
1026    The _interim_ variants are used for debugging the interim
1027    tables that are used to generate the final compressed
1028    representation which is what dump_trie expects.
1029
1030    Part of the reason for their existence is to provide a form
1031    of documentation as to how the different representations function.
1032
1033 */
1034
1035 /*
1036   Dumps the final compressed table form of the trie to Perl_debug_log.
1037   Used for debugging make_trie().
1038 */
1039
1040 STATIC void
1041 S_dump_trie(pTHX_ const struct _reg_trie_data *trie, HV *widecharmap,
1042             AV *revcharmap, U32 depth)
1043 {
1044     U32 state;
1045     SV *sv=sv_newmortal();
1046     int colwidth= widecharmap ? 6 : 4;
1047     U16 word;
1048     GET_RE_DEBUG_FLAGS_DECL;
1049
1050     PERL_ARGS_ASSERT_DUMP_TRIE;
1051
1052     PerlIO_printf( Perl_debug_log, "%*sChar : %-6s%-6s%-4s ",
1053         (int)depth * 2 + 2,"",
1054         "Match","Base","Ofs" );
1055
1056     for( state = 0 ; state < trie->uniquecharcount ; state++ ) {
1057         SV ** const tmp = av_fetch( revcharmap, state, 0);
1058         if ( tmp ) {
1059             PerlIO_printf( Perl_debug_log, "%*s", 
1060                 colwidth,
1061                 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, 
1062                             PL_colors[0], PL_colors[1],
1063                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1064                             PERL_PV_ESCAPE_FIRSTCHAR 
1065                 ) 
1066             );
1067         }
1068     }
1069     PerlIO_printf( Perl_debug_log, "\n%*sState|-----------------------",
1070         (int)depth * 2 + 2,"");
1071
1072     for( state = 0 ; state < trie->uniquecharcount ; state++ )
1073         PerlIO_printf( Perl_debug_log, "%.*s", colwidth, "--------");
1074     PerlIO_printf( Perl_debug_log, "\n");
1075
1076     for( state = 1 ; state < trie->statecount ; state++ ) {
1077         const U32 base = trie->states[ state ].trans.base;
1078
1079         PerlIO_printf( Perl_debug_log, "%*s#%4"UVXf"|", (int)depth * 2 + 2,"", (UV)state);
1080
1081         if ( trie->states[ state ].wordnum ) {
1082             PerlIO_printf( Perl_debug_log, " W%4X", trie->states[ state ].wordnum );
1083         } else {
1084             PerlIO_printf( Perl_debug_log, "%6s", "" );
1085         }
1086
1087         PerlIO_printf( Perl_debug_log, " @%4"UVXf" ", (UV)base );
1088
1089         if ( base ) {
1090             U32 ofs = 0;
1091
1092             while( ( base + ofs  < trie->uniquecharcount ) ||
1093                    ( base + ofs - trie->uniquecharcount < trie->lasttrans
1094                      && trie->trans[ base + ofs - trie->uniquecharcount ].check != state))
1095                     ofs++;
1096
1097             PerlIO_printf( Perl_debug_log, "+%2"UVXf"[ ", (UV)ofs);
1098
1099             for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
1100                 if ( ( base + ofs >= trie->uniquecharcount ) &&
1101                      ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
1102                      trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
1103                 {
1104                    PerlIO_printf( Perl_debug_log, "%*"UVXf,
1105                     colwidth,
1106                     (UV)trie->trans[ base + ofs - trie->uniquecharcount ].next );
1107                 } else {
1108                     PerlIO_printf( Perl_debug_log, "%*s",colwidth,"   ." );
1109                 }
1110             }
1111
1112             PerlIO_printf( Perl_debug_log, "]");
1113
1114         }
1115         PerlIO_printf( Perl_debug_log, "\n" );
1116     }
1117     PerlIO_printf(Perl_debug_log, "%*sword_info N:(prev,len)=", (int)depth*2, "");
1118     for (word=1; word <= trie->wordcount; word++) {
1119         PerlIO_printf(Perl_debug_log, " %d:(%d,%d)",
1120             (int)word, (int)(trie->wordinfo[word].prev),
1121             (int)(trie->wordinfo[word].len));
1122     }
1123     PerlIO_printf(Perl_debug_log, "\n" );
1124 }    
1125 /*
1126   Dumps a fully constructed but uncompressed trie in list form.
1127   List tries normally only are used for construction when the number of 
1128   possible chars (trie->uniquecharcount) is very high.
1129   Used for debugging make_trie().
1130 */
1131 STATIC void
1132 S_dump_trie_interim_list(pTHX_ const struct _reg_trie_data *trie,
1133                          HV *widecharmap, AV *revcharmap, U32 next_alloc,
1134                          U32 depth)
1135 {
1136     U32 state;
1137     SV *sv=sv_newmortal();
1138     int colwidth= widecharmap ? 6 : 4;
1139     GET_RE_DEBUG_FLAGS_DECL;
1140
1141     PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_LIST;
1142
1143     /* print out the table precompression.  */
1144     PerlIO_printf( Perl_debug_log, "%*sState :Word | Transition Data\n%*s%s",
1145         (int)depth * 2 + 2,"", (int)depth * 2 + 2,"",
1146         "------:-----+-----------------\n" );
1147     
1148     for( state=1 ; state < next_alloc ; state ++ ) {
1149         U16 charid;
1150     
1151         PerlIO_printf( Perl_debug_log, "%*s %4"UVXf" :",
1152             (int)depth * 2 + 2,"", (UV)state  );
1153         if ( ! trie->states[ state ].wordnum ) {
1154             PerlIO_printf( Perl_debug_log, "%5s| ","");
1155         } else {
1156             PerlIO_printf( Perl_debug_log, "W%4x| ",
1157                 trie->states[ state ].wordnum
1158             );
1159         }
1160         for( charid = 1 ; charid <= TRIE_LIST_USED( state ) ; charid++ ) {
1161             SV ** const tmp = av_fetch( revcharmap, TRIE_LIST_ITEM(state,charid).forid, 0);
1162             if ( tmp ) {
1163                 PerlIO_printf( Perl_debug_log, "%*s:%3X=%4"UVXf" | ",
1164                     colwidth,
1165                     pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, 
1166                             PL_colors[0], PL_colors[1],
1167                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1168                             PERL_PV_ESCAPE_FIRSTCHAR 
1169                     ) ,
1170                     TRIE_LIST_ITEM(state,charid).forid,
1171                     (UV)TRIE_LIST_ITEM(state,charid).newstate
1172                 );
1173                 if (!(charid % 10)) 
1174                     PerlIO_printf(Perl_debug_log, "\n%*s| ",
1175                         (int)((depth * 2) + 14), "");
1176             }
1177         }
1178         PerlIO_printf( Perl_debug_log, "\n");
1179     }
1180 }    
1181
1182 /*
1183   Dumps a fully constructed but uncompressed trie in table form.
1184   This is the normal DFA style state transition table, with a few 
1185   twists to facilitate compression later. 
1186   Used for debugging make_trie().
1187 */
1188 STATIC void
1189 S_dump_trie_interim_table(pTHX_ const struct _reg_trie_data *trie,
1190                           HV *widecharmap, AV *revcharmap, U32 next_alloc,
1191                           U32 depth)
1192 {
1193     U32 state;
1194     U16 charid;
1195     SV *sv=sv_newmortal();
1196     int colwidth= widecharmap ? 6 : 4;
1197     GET_RE_DEBUG_FLAGS_DECL;
1198
1199     PERL_ARGS_ASSERT_DUMP_TRIE_INTERIM_TABLE;
1200     
1201     /*
1202        print out the table precompression so that we can do a visual check
1203        that they are identical.
1204      */
1205     
1206     PerlIO_printf( Perl_debug_log, "%*sChar : ",(int)depth * 2 + 2,"" );
1207
1208     for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1209         SV ** const tmp = av_fetch( revcharmap, charid, 0);
1210         if ( tmp ) {
1211             PerlIO_printf( Perl_debug_log, "%*s", 
1212                 colwidth,
1213                 pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), colwidth, 
1214                             PL_colors[0], PL_colors[1],
1215                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
1216                             PERL_PV_ESCAPE_FIRSTCHAR 
1217                 ) 
1218             );
1219         }
1220     }
1221
1222     PerlIO_printf( Perl_debug_log, "\n%*sState+-",(int)depth * 2 + 2,"" );
1223
1224     for( charid=0 ; charid < trie->uniquecharcount ; charid++ ) {
1225         PerlIO_printf( Perl_debug_log, "%.*s", colwidth,"--------");
1226     }
1227
1228     PerlIO_printf( Perl_debug_log, "\n" );
1229
1230     for( state=1 ; state < next_alloc ; state += trie->uniquecharcount ) {
1231
1232         PerlIO_printf( Perl_debug_log, "%*s%4"UVXf" : ", 
1233             (int)depth * 2 + 2,"",
1234             (UV)TRIE_NODENUM( state ) );
1235
1236         for( charid = 0 ; charid < trie->uniquecharcount ; charid++ ) {
1237             UV v=(UV)SAFE_TRIE_NODENUM( trie->trans[ state + charid ].next );
1238             if (v)
1239                 PerlIO_printf( Perl_debug_log, "%*"UVXf, colwidth, v );
1240             else
1241                 PerlIO_printf( Perl_debug_log, "%*s", colwidth, "." );
1242         }
1243         if ( ! trie->states[ TRIE_NODENUM( state ) ].wordnum ) {
1244             PerlIO_printf( Perl_debug_log, " (%4"UVXf")\n", (UV)trie->trans[ state ].check );
1245         } else {
1246             PerlIO_printf( Perl_debug_log, " (%4"UVXf") W%4X\n", (UV)trie->trans[ state ].check,
1247             trie->states[ TRIE_NODENUM( state ) ].wordnum );
1248         }
1249     }
1250 }
1251
1252 #endif
1253
1254
1255 /* make_trie(startbranch,first,last,tail,word_count,flags,depth)
1256   startbranch: the first branch in the whole branch sequence
1257   first      : start branch of sequence of branch-exact nodes.
1258                May be the same as startbranch
1259   last       : Thing following the last branch.
1260                May be the same as tail.
1261   tail       : item following the branch sequence
1262   count      : words in the sequence
1263   flags      : currently the OP() type we will be building one of /EXACT(|F|Fl)/
1264   depth      : indent depth
1265
1266 Inplace optimizes a sequence of 2 or more Branch-Exact nodes into a TRIE node.
1267
1268 A trie is an N'ary tree where the branches are determined by digital
1269 decomposition of the key. IE, at the root node you look up the 1st character and
1270 follow that branch repeat until you find the end of the branches. Nodes can be
1271 marked as "accepting" meaning they represent a complete word. Eg:
1272
1273   /he|she|his|hers/
1274
1275 would convert into the following structure. Numbers represent states, letters
1276 following numbers represent valid transitions on the letter from that state, if
1277 the number is in square brackets it represents an accepting state, otherwise it
1278 will be in parenthesis.
1279
1280       +-h->+-e->[3]-+-r->(8)-+-s->[9]
1281       |    |
1282       |   (2)
1283       |    |
1284      (1)   +-i->(6)-+-s->[7]
1285       |
1286       +-s->(3)-+-h->(4)-+-e->[5]
1287
1288       Accept Word Mapping: 3=>1 (he),5=>2 (she), 7=>3 (his), 9=>4 (hers)
1289
1290 This shows that when matching against the string 'hers' we will begin at state 1
1291 read 'h' and move to state 2, read 'e' and move to state 3 which is accepting,
1292 then read 'r' and go to state 8 followed by 's' which takes us to state 9 which
1293 is also accepting. Thus we know that we can match both 'he' and 'hers' with a
1294 single traverse. We store a mapping from accepting to state to which word was
1295 matched, and then when we have multiple possibilities we try to complete the
1296 rest of the regex in the order in which they occured in the alternation.
1297
1298 The only prior NFA like behaviour that would be changed by the TRIE support is
1299 the silent ignoring of duplicate alternations which are of the form:
1300
1301  / (DUPE|DUPE) X? (?{ ... }) Y /x
1302
1303 Thus EVAL blocks following a trie may be called a different number of times with
1304 and without the optimisation. With the optimisations dupes will be silently
1305 ignored. This inconsistent behaviour of EVAL type nodes is well established as
1306 the following demonstrates:
1307
1308  'words'=~/(word|word|word)(?{ print $1 })[xyz]/
1309
1310 which prints out 'word' three times, but
1311
1312  'words'=~/(word|word|word)(?{ print $1 })S/
1313
1314 which doesnt print it out at all. This is due to other optimisations kicking in.
1315
1316 Example of what happens on a structural level:
1317
1318 The regexp /(ac|ad|ab)+/ will produce the following debug output:
1319
1320    1: CURLYM[1] {1,32767}(18)
1321    5:   BRANCH(8)
1322    6:     EXACT <ac>(16)
1323    8:   BRANCH(11)
1324    9:     EXACT <ad>(16)
1325   11:   BRANCH(14)
1326   12:     EXACT <ab>(16)
1327   16:   SUCCEED(0)
1328   17:   NOTHING(18)
1329   18: END(0)
1330
1331 This would be optimizable with startbranch=5, first=5, last=16, tail=16
1332 and should turn into:
1333
1334    1: CURLYM[1] {1,32767}(18)
1335    5:   TRIE(16)
1336         [Words:3 Chars Stored:6 Unique Chars:4 States:5 NCP:1]
1337           <ac>
1338           <ad>
1339           <ab>
1340   16:   SUCCEED(0)
1341   17:   NOTHING(18)
1342   18: END(0)
1343
1344 Cases where tail != last would be like /(?foo|bar)baz/:
1345
1346    1: BRANCH(4)
1347    2:   EXACT <foo>(8)
1348    4: BRANCH(7)
1349    5:   EXACT <bar>(8)
1350    7: TAIL(8)
1351    8: EXACT <baz>(10)
1352   10: END(0)
1353
1354 which would be optimizable with startbranch=1, first=1, last=7, tail=8
1355 and would end up looking like:
1356
1357     1: TRIE(8)
1358       [Words:2 Chars Stored:6 Unique Chars:5 States:7 NCP:1]
1359         <foo>
1360         <bar>
1361    7: TAIL(8)
1362    8: EXACT <baz>(10)
1363   10: END(0)
1364
1365     d = uvuni_to_utf8_flags(d, uv, 0);
1366
1367 is the recommended Unicode-aware way of saying
1368
1369     *(d++) = uv;
1370 */
1371
1372 #define TRIE_STORE_REVCHAR(val)                                            \
1373     STMT_START {                                                           \
1374         if (UTF) {                                                         \
1375             SV *zlopp = newSV(7); /* XXX: optimize me */                   \
1376             unsigned char *flrbbbbb = (unsigned char *) SvPVX(zlopp);      \
1377             unsigned const char *const kapow = uvuni_to_utf8(flrbbbbb, val); \
1378             SvCUR_set(zlopp, kapow - flrbbbbb);                            \
1379             SvPOK_on(zlopp);                                               \
1380             SvUTF8_on(zlopp);                                              \
1381             av_push(revcharmap, zlopp);                                    \
1382         } else {                                                           \
1383             char ooooff = (char)val;                                           \
1384             av_push(revcharmap, newSVpvn(&ooooff, 1));                     \
1385         }                                                                  \
1386         } STMT_END
1387
1388 #define TRIE_READ_CHAR STMT_START {                                                     \
1389     wordlen++;                                                                          \
1390     if ( UTF ) {                                                                        \
1391         /* if it is UTF then it is either already folded, or does not need folding */   \
1392         uvc = utf8n_to_uvuni( (const U8*) uc, UTF8_MAXLEN, &len, uniflags);             \
1393     }                                                                                   \
1394     else if (folder == PL_fold_latin1) {                                                \
1395         /* if we use this folder we have to obey unicode rules on latin-1 data */       \
1396         if ( foldlen > 0 ) {                                                            \
1397            uvc = utf8n_to_uvuni( (const U8*) scan, UTF8_MAXLEN, &len, uniflags );       \
1398            foldlen -= len;                                                              \
1399            scan += len;                                                                 \
1400            len = 0;                                                                     \
1401         } else {                                                                        \
1402             len = 1;                                                                    \
1403             uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, 1);                     \
1404             skiplen = UNISKIP(uvc);                                                     \
1405             foldlen -= skiplen;                                                         \
1406             scan = foldbuf + skiplen;                                                   \
1407         }                                                                               \
1408     } else {                                                                            \
1409         /* raw data, will be folded later if needed */                                  \
1410         uvc = (U32)*uc;                                                                 \
1411         len = 1;                                                                        \
1412     }                                                                                   \
1413 } STMT_END
1414
1415
1416
1417 #define TRIE_LIST_PUSH(state,fid,ns) STMT_START {               \
1418     if ( TRIE_LIST_CUR( state ) >=TRIE_LIST_LEN( state ) ) {    \
1419         U32 ging = TRIE_LIST_LEN( state ) *= 2;                 \
1420         Renew( trie->states[ state ].trans.list, ging, reg_trie_trans_le ); \
1421     }                                                           \
1422     TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).forid = fid;     \
1423     TRIE_LIST_ITEM( state, TRIE_LIST_CUR( state ) ).newstate = ns;   \
1424     TRIE_LIST_CUR( state )++;                                   \
1425 } STMT_END
1426
1427 #define TRIE_LIST_NEW(state) STMT_START {                       \
1428     Newxz( trie->states[ state ].trans.list,               \
1429         4, reg_trie_trans_le );                                 \
1430      TRIE_LIST_CUR( state ) = 1;                                \
1431      TRIE_LIST_LEN( state ) = 4;                                \
1432 } STMT_END
1433
1434 #define TRIE_HANDLE_WORD(state) STMT_START {                    \
1435     U16 dupe= trie->states[ state ].wordnum;                    \
1436     regnode * const noper_next = regnext( noper );              \
1437                                                                 \
1438     DEBUG_r({                                                   \
1439         /* store the word for dumping */                        \
1440         SV* tmp;                                                \
1441         if (OP(noper) != NOTHING)                               \
1442             tmp = newSVpvn_utf8(STRING(noper), STR_LEN(noper), UTF);    \
1443         else                                                    \
1444             tmp = newSVpvn_utf8( "", 0, UTF );                  \
1445         av_push( trie_words, tmp );                             \
1446     });                                                         \
1447                                                                 \
1448     curword++;                                                  \
1449     trie->wordinfo[curword].prev   = 0;                         \
1450     trie->wordinfo[curword].len    = wordlen;                   \
1451     trie->wordinfo[curword].accept = state;                     \
1452                                                                 \
1453     if ( noper_next < tail ) {                                  \
1454         if (!trie->jump)                                        \
1455             trie->jump = (U16 *) PerlMemShared_calloc( word_count + 1, sizeof(U16) ); \
1456         trie->jump[curword] = (U16)(noper_next - convert);      \
1457         if (!jumper)                                            \
1458             jumper = noper_next;                                \
1459         if (!nextbranch)                                        \
1460             nextbranch= regnext(cur);                           \
1461     }                                                           \
1462                                                                 \
1463     if ( dupe ) {                                               \
1464         /* It's a dupe. Pre-insert into the wordinfo[].prev   */\
1465         /* chain, so that when the bits of chain are later    */\
1466         /* linked together, the dups appear in the chain      */\
1467         trie->wordinfo[curword].prev = trie->wordinfo[dupe].prev; \
1468         trie->wordinfo[dupe].prev = curword;                    \
1469     } else {                                                    \
1470         /* we haven't inserted this word yet.                */ \
1471         trie->states[ state ].wordnum = curword;                \
1472     }                                                           \
1473 } STMT_END
1474
1475
1476 #define TRIE_TRANS_STATE(state,base,ucharcount,charid,special)          \
1477      ( ( base + charid >=  ucharcount                                   \
1478          && base + charid < ubound                                      \
1479          && state == trie->trans[ base - ucharcount + charid ].check    \
1480          && trie->trans[ base - ucharcount + charid ].next )            \
1481            ? trie->trans[ base - ucharcount + charid ].next             \
1482            : ( state==1 ? special : 0 )                                 \
1483       )
1484
1485 #define MADE_TRIE       1
1486 #define MADE_JUMP_TRIE  2
1487 #define MADE_EXACT_TRIE 4
1488
1489 STATIC I32
1490 S_make_trie(pTHX_ RExC_state_t *pRExC_state, regnode *startbranch, regnode *first, regnode *last, regnode *tail, U32 word_count, U32 flags, U32 depth)
1491 {
1492     dVAR;
1493     /* first pass, loop through and scan words */
1494     reg_trie_data *trie;
1495     HV *widecharmap = NULL;
1496     AV *revcharmap = newAV();
1497     regnode *cur;
1498     const U32 uniflags = UTF8_ALLOW_DEFAULT;
1499     STRLEN len = 0;
1500     UV uvc = 0;
1501     U16 curword = 0;
1502     U32 next_alloc = 0;
1503     regnode *jumper = NULL;
1504     regnode *nextbranch = NULL;
1505     regnode *convert = NULL;
1506     U32 *prev_states; /* temp array mapping each state to previous one */
1507     /* we just use folder as a flag in utf8 */
1508     const U8 * folder = NULL;
1509
1510 #ifdef DEBUGGING
1511     const U32 data_slot = add_data( pRExC_state, 4, "tuuu" );
1512     AV *trie_words = NULL;
1513     /* along with revcharmap, this only used during construction but both are
1514      * useful during debugging so we store them in the struct when debugging.
1515      */
1516 #else
1517     const U32 data_slot = add_data( pRExC_state, 2, "tu" );
1518     STRLEN trie_charcount=0;
1519 #endif
1520     SV *re_trie_maxbuff;
1521     GET_RE_DEBUG_FLAGS_DECL;
1522
1523     PERL_ARGS_ASSERT_MAKE_TRIE;
1524 #ifndef DEBUGGING
1525     PERL_UNUSED_ARG(depth);
1526 #endif
1527
1528     switch (flags) {
1529         case EXACT: break;
1530         case EXACTFA:
1531         case EXACTFU_SS:
1532         case EXACTFU_TRICKYFOLD:
1533         case EXACTFU: folder = PL_fold_latin1; break;
1534         case EXACTF:  folder = PL_fold; break;
1535         case EXACTFL: folder = PL_fold_locale; break;
1536         default: Perl_croak( aTHX_ "panic! In trie construction, unknown node type %u %s", (unsigned) flags, PL_reg_name[flags] );
1537     }
1538
1539     trie = (reg_trie_data *) PerlMemShared_calloc( 1, sizeof(reg_trie_data) );
1540     trie->refcount = 1;
1541     trie->startstate = 1;
1542     trie->wordcount = word_count;
1543     RExC_rxi->data->data[ data_slot ] = (void*)trie;
1544     trie->charmap = (U16 *) PerlMemShared_calloc( 256, sizeof(U16) );
1545     if (flags == EXACT)
1546         trie->bitmap = (char *) PerlMemShared_calloc( ANYOF_BITMAP_SIZE, 1 );
1547     trie->wordinfo = (reg_trie_wordinfo *) PerlMemShared_calloc(
1548                        trie->wordcount+1, sizeof(reg_trie_wordinfo));
1549
1550     DEBUG_r({
1551         trie_words = newAV();
1552     });
1553
1554     re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
1555     if (!SvIOK(re_trie_maxbuff)) {
1556         sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
1557     }
1558     DEBUG_OPTIMISE_r({
1559                 PerlIO_printf( Perl_debug_log,
1560                   "%*smake_trie start==%d, first==%d, last==%d, tail==%d depth=%d\n",
1561                   (int)depth * 2 + 2, "", 
1562                   REG_NODE_NUM(startbranch),REG_NODE_NUM(first), 
1563                   REG_NODE_NUM(last), REG_NODE_NUM(tail),
1564                   (int)depth);
1565     });
1566    
1567    /* Find the node we are going to overwrite */
1568     if ( first == startbranch && OP( last ) != BRANCH ) {
1569         /* whole branch chain */
1570         convert = first;
1571     } else {
1572         /* branch sub-chain */
1573         convert = NEXTOPER( first );
1574     }
1575         
1576     /*  -- First loop and Setup --
1577
1578        We first traverse the branches and scan each word to determine if it
1579        contains widechars, and how many unique chars there are, this is
1580        important as we have to build a table with at least as many columns as we
1581        have unique chars.
1582
1583        We use an array of integers to represent the character codes 0..255
1584        (trie->charmap) and we use a an HV* to store Unicode characters. We use the
1585        native representation of the character value as the key and IV's for the
1586        coded index.
1587
1588        *TODO* If we keep track of how many times each character is used we can
1589        remap the columns so that the table compression later on is more
1590        efficient in terms of memory by ensuring the most common value is in the
1591        middle and the least common are on the outside.  IMO this would be better
1592        than a most to least common mapping as theres a decent chance the most
1593        common letter will share a node with the least common, meaning the node
1594        will not be compressible. With a middle is most common approach the worst
1595        case is when we have the least common nodes twice.
1596
1597      */
1598
1599     for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1600         regnode * const noper = NEXTOPER( cur );
1601         const U8 *uc = (U8*)STRING( noper );
1602         const U8 * const e  = uc + STR_LEN( noper );
1603         STRLEN foldlen = 0;
1604         U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1605         STRLEN skiplen = 0;
1606         const U8 *scan = (U8*)NULL;
1607         U32 wordlen      = 0;         /* required init */
1608         STRLEN chars = 0;
1609         bool set_bit = trie->bitmap ? 1 : 0; /*store the first char in the bitmap?*/
1610
1611         if (OP(noper) == NOTHING) {
1612             trie->minlen= 0;
1613             continue;
1614         }
1615         if ( set_bit ) { /* bitmap only alloced when !(UTF&&Folding) */
1616             TRIE_BITMAP_SET(trie,*uc); /* store the raw first byte
1617                                           regardless of encoding */
1618             if (OP( noper ) == EXACTFU_SS) {
1619                 /* false positives are ok, so just set this */
1620                 TRIE_BITMAP_SET(trie,0xDF);
1621             }
1622         }
1623         for ( ; uc < e ; uc += len ) {
1624             TRIE_CHARCOUNT(trie)++;
1625             TRIE_READ_CHAR;
1626             chars++;
1627             if ( uvc < 256 ) {
1628                 if ( folder ) {
1629                     U8 folded= folder[ (U8) uvc ];
1630                     if ( !trie->charmap[ folded ] ) {
1631                         trie->charmap[ folded ]=( ++trie->uniquecharcount );
1632                         TRIE_STORE_REVCHAR( folded );
1633                     }
1634                 }
1635                 if ( !trie->charmap[ uvc ] ) {
1636                     trie->charmap[ uvc ]=( ++trie->uniquecharcount );
1637                     TRIE_STORE_REVCHAR( uvc );
1638                 }
1639                 if ( set_bit ) {
1640                     /* store the codepoint in the bitmap, and its folded
1641                      * equivalent. */
1642                     TRIE_BITMAP_SET(trie, uvc);
1643
1644                     /* store the folded codepoint */
1645                     if ( folder ) TRIE_BITMAP_SET(trie, folder[(U8) uvc ]);
1646
1647                     if ( !UTF ) {
1648                         /* store first byte of utf8 representation of
1649                            variant codepoints */
1650                         if (! UNI_IS_INVARIANT(uvc)) {
1651                             TRIE_BITMAP_SET(trie, UTF8_TWO_BYTE_HI(uvc));
1652                         }
1653                     }
1654                     set_bit = 0; /* We've done our bit :-) */
1655                 }
1656             } else {
1657                 SV** svpp;
1658                 if ( !widecharmap )
1659                     widecharmap = newHV();
1660
1661                 svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 1 );
1662
1663                 if ( !svpp )
1664                     Perl_croak( aTHX_ "error creating/fetching widecharmap entry for 0x%"UVXf, uvc );
1665
1666                 if ( !SvTRUE( *svpp ) ) {
1667                     sv_setiv( *svpp, ++trie->uniquecharcount );
1668                     TRIE_STORE_REVCHAR(uvc);
1669                 }
1670             }
1671         }
1672         if( cur == first ) {
1673             trie->minlen = chars;
1674             trie->maxlen = chars;
1675         } else if (chars < trie->minlen) {
1676             trie->minlen = chars;
1677         } else if (chars > trie->maxlen) {
1678             trie->maxlen = chars;
1679         }
1680         if (OP( noper ) == EXACTFU_SS) {
1681             /* XXX: workaround - 'ss' could match "\x{DF}" so minlen could be 1 and not 2*/
1682             if (trie->minlen > 1)
1683                 trie->minlen= 1;
1684         }
1685         if (OP( noper ) == EXACTFU_TRICKYFOLD) {
1686             /* XXX: workround - things like "\x{1FBE}\x{0308}\x{0301}" can match "\x{0390}" 
1687              *                - We assume that any such sequence might match a 2 byte string */
1688             if (trie->minlen > 2 )
1689                 trie->minlen= 2;
1690         }
1691
1692     } /* end first pass */
1693     DEBUG_TRIE_COMPILE_r(
1694         PerlIO_printf( Perl_debug_log, "%*sTRIE(%s): W:%d C:%d Uq:%d Min:%d Max:%d\n",
1695                 (int)depth * 2 + 2,"",
1696                 ( widecharmap ? "UTF8" : "NATIVE" ), (int)word_count,
1697                 (int)TRIE_CHARCOUNT(trie), trie->uniquecharcount,
1698                 (int)trie->minlen, (int)trie->maxlen )
1699     );
1700
1701     /*
1702         We now know what we are dealing with in terms of unique chars and
1703         string sizes so we can calculate how much memory a naive
1704         representation using a flat table  will take. If it's over a reasonable
1705         limit (as specified by ${^RE_TRIE_MAXBUF}) we use a more memory
1706         conservative but potentially much slower representation using an array
1707         of lists.
1708
1709         At the end we convert both representations into the same compressed
1710         form that will be used in regexec.c for matching with. The latter
1711         is a form that cannot be used to construct with but has memory
1712         properties similar to the list form and access properties similar
1713         to the table form making it both suitable for fast searches and
1714         small enough that its feasable to store for the duration of a program.
1715
1716         See the comment in the code where the compressed table is produced
1717         inplace from the flat tabe representation for an explanation of how
1718         the compression works.
1719
1720     */
1721
1722
1723     Newx(prev_states, TRIE_CHARCOUNT(trie) + 2, U32);
1724     prev_states[1] = 0;
1725
1726     if ( (IV)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1) > SvIV(re_trie_maxbuff) ) {
1727         /*
1728             Second Pass -- Array Of Lists Representation
1729
1730             Each state will be represented by a list of charid:state records
1731             (reg_trie_trans_le) the first such element holds the CUR and LEN
1732             points of the allocated array. (See defines above).
1733
1734             We build the initial structure using the lists, and then convert
1735             it into the compressed table form which allows faster lookups
1736             (but cant be modified once converted).
1737         */
1738
1739         STRLEN transcount = 1;
1740
1741         DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log, 
1742             "%*sCompiling trie using list compiler\n",
1743             (int)depth * 2 + 2, ""));
1744
1745         trie->states = (reg_trie_state *)
1746             PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1747                                   sizeof(reg_trie_state) );
1748         TRIE_LIST_NEW(1);
1749         next_alloc = 2;
1750
1751         for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1752
1753             regnode * const noper = NEXTOPER( cur );
1754             U8 *uc           = (U8*)STRING( noper );
1755             const U8 * const e = uc + STR_LEN( noper );
1756             U32 state        = 1;         /* required init */
1757             U16 charid       = 0;         /* sanity init */
1758             U8 *scan         = (U8*)NULL; /* sanity init */
1759             STRLEN foldlen   = 0;         /* required init */
1760             U32 wordlen      = 0;         /* required init */
1761             U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1762             STRLEN skiplen   = 0;
1763
1764             if (OP(noper) != NOTHING) {
1765                 for ( ; uc < e ; uc += len ) {
1766
1767                     TRIE_READ_CHAR;
1768
1769                     if ( uvc < 256 ) {
1770                         charid = trie->charmap[ uvc ];
1771                     } else {
1772                         SV** const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1773                         if ( !svpp ) {
1774                             charid = 0;
1775                         } else {
1776                             charid=(U16)SvIV( *svpp );
1777                         }
1778                     }
1779                     /* charid is now 0 if we dont know the char read, or nonzero if we do */
1780                     if ( charid ) {
1781
1782                         U16 check;
1783                         U32 newstate = 0;
1784
1785                         charid--;
1786                         if ( !trie->states[ state ].trans.list ) {
1787                             TRIE_LIST_NEW( state );
1788                         }
1789                         for ( check = 1; check <= TRIE_LIST_USED( state ); check++ ) {
1790                             if ( TRIE_LIST_ITEM( state, check ).forid == charid ) {
1791                                 newstate = TRIE_LIST_ITEM( state, check ).newstate;
1792                                 break;
1793                             }
1794                         }
1795                         if ( ! newstate ) {
1796                             newstate = next_alloc++;
1797                             prev_states[newstate] = state;
1798                             TRIE_LIST_PUSH( state, charid, newstate );
1799                             transcount++;
1800                         }
1801                         state = newstate;
1802                     } else {
1803                         Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1804                     }
1805                 }
1806             }
1807             TRIE_HANDLE_WORD(state);
1808
1809         } /* end second pass */
1810
1811         /* next alloc is the NEXT state to be allocated */
1812         trie->statecount = next_alloc; 
1813         trie->states = (reg_trie_state *)
1814             PerlMemShared_realloc( trie->states,
1815                                    next_alloc
1816                                    * sizeof(reg_trie_state) );
1817
1818         /* and now dump it out before we compress it */
1819         DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_list(trie, widecharmap,
1820                                                          revcharmap, next_alloc,
1821                                                          depth+1)
1822         );
1823
1824         trie->trans = (reg_trie_trans *)
1825             PerlMemShared_calloc( transcount, sizeof(reg_trie_trans) );
1826         {
1827             U32 state;
1828             U32 tp = 0;
1829             U32 zp = 0;
1830
1831
1832             for( state=1 ; state < next_alloc ; state ++ ) {
1833                 U32 base=0;
1834
1835                 /*
1836                 DEBUG_TRIE_COMPILE_MORE_r(
1837                     PerlIO_printf( Perl_debug_log, "tp: %d zp: %d ",tp,zp)
1838                 );
1839                 */
1840
1841                 if (trie->states[state].trans.list) {
1842                     U16 minid=TRIE_LIST_ITEM( state, 1).forid;
1843                     U16 maxid=minid;
1844                     U16 idx;
1845
1846                     for( idx = 2 ; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1847                         const U16 forid = TRIE_LIST_ITEM( state, idx).forid;
1848                         if ( forid < minid ) {
1849                             minid=forid;
1850                         } else if ( forid > maxid ) {
1851                             maxid=forid;
1852                         }
1853                     }
1854                     if ( transcount < tp + maxid - minid + 1) {
1855                         transcount *= 2;
1856                         trie->trans = (reg_trie_trans *)
1857                             PerlMemShared_realloc( trie->trans,
1858                                                      transcount
1859                                                      * sizeof(reg_trie_trans) );
1860                         Zero( trie->trans + (transcount / 2), transcount / 2 , reg_trie_trans );
1861                     }
1862                     base = trie->uniquecharcount + tp - minid;
1863                     if ( maxid == minid ) {
1864                         U32 set = 0;
1865                         for ( ; zp < tp ; zp++ ) {
1866                             if ( ! trie->trans[ zp ].next ) {
1867                                 base = trie->uniquecharcount + zp - minid;
1868                                 trie->trans[ zp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1869                                 trie->trans[ zp ].check = state;
1870                                 set = 1;
1871                                 break;
1872                             }
1873                         }
1874                         if ( !set ) {
1875                             trie->trans[ tp ].next = TRIE_LIST_ITEM( state, 1).newstate;
1876                             trie->trans[ tp ].check = state;
1877                             tp++;
1878                             zp = tp;
1879                         }
1880                     } else {
1881                         for ( idx=1; idx <= TRIE_LIST_USED( state ) ; idx++ ) {
1882                             const U32 tid = base -  trie->uniquecharcount + TRIE_LIST_ITEM( state, idx ).forid;
1883                             trie->trans[ tid ].next = TRIE_LIST_ITEM( state, idx ).newstate;
1884                             trie->trans[ tid ].check = state;
1885                         }
1886                         tp += ( maxid - minid + 1 );
1887                     }
1888                     Safefree(trie->states[ state ].trans.list);
1889                 }
1890                 /*
1891                 DEBUG_TRIE_COMPILE_MORE_r(
1892                     PerlIO_printf( Perl_debug_log, " base: %d\n",base);
1893                 );
1894                 */
1895                 trie->states[ state ].trans.base=base;
1896             }
1897             trie->lasttrans = tp + 1;
1898         }
1899     } else {
1900         /*
1901            Second Pass -- Flat Table Representation.
1902
1903            we dont use the 0 slot of either trans[] or states[] so we add 1 to each.
1904            We know that we will need Charcount+1 trans at most to store the data
1905            (one row per char at worst case) So we preallocate both structures
1906            assuming worst case.
1907
1908            We then construct the trie using only the .next slots of the entry
1909            structs.
1910
1911            We use the .check field of the first entry of the node temporarily to
1912            make compression both faster and easier by keeping track of how many non
1913            zero fields are in the node.
1914
1915            Since trans are numbered from 1 any 0 pointer in the table is a FAIL
1916            transition.
1917
1918            There are two terms at use here: state as a TRIE_NODEIDX() which is a
1919            number representing the first entry of the node, and state as a
1920            TRIE_NODENUM() which is the trans number. state 1 is TRIE_NODEIDX(1) and
1921            TRIE_NODENUM(1), state 2 is TRIE_NODEIDX(2) and TRIE_NODENUM(3) if there
1922            are 2 entrys per node. eg:
1923
1924              A B       A B
1925           1. 2 4    1. 3 7
1926           2. 0 3    3. 0 5
1927           3. 0 0    5. 0 0
1928           4. 0 0    7. 0 0
1929
1930            The table is internally in the right hand, idx form. However as we also
1931            have to deal with the states array which is indexed by nodenum we have to
1932            use TRIE_NODENUM() to convert.
1933
1934         */
1935         DEBUG_TRIE_COMPILE_MORE_r( PerlIO_printf( Perl_debug_log, 
1936             "%*sCompiling trie using table compiler\n",
1937             (int)depth * 2 + 2, ""));
1938
1939         trie->trans = (reg_trie_trans *)
1940             PerlMemShared_calloc( ( TRIE_CHARCOUNT(trie) + 1 )
1941                                   * trie->uniquecharcount + 1,
1942                                   sizeof(reg_trie_trans) );
1943         trie->states = (reg_trie_state *)
1944             PerlMemShared_calloc( TRIE_CHARCOUNT(trie) + 2,
1945                                   sizeof(reg_trie_state) );
1946         next_alloc = trie->uniquecharcount + 1;
1947
1948
1949         for ( cur = first ; cur < last ; cur = regnext( cur ) ) {
1950
1951             regnode * const noper   = NEXTOPER( cur );
1952             const U8 *uc     = (U8*)STRING( noper );
1953             const U8 * const e = uc + STR_LEN( noper );
1954
1955             U32 state        = 1;         /* required init */
1956
1957             U16 charid       = 0;         /* sanity init */
1958             U32 accept_state = 0;         /* sanity init */
1959             U8 *scan         = (U8*)NULL; /* sanity init */
1960
1961             STRLEN foldlen   = 0;         /* required init */
1962             U32 wordlen      = 0;         /* required init */
1963             STRLEN skiplen   = 0;
1964             U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
1965
1966
1967             if ( OP(noper) != NOTHING ) {
1968                 for ( ; uc < e ; uc += len ) {
1969
1970                     TRIE_READ_CHAR;
1971
1972                     if ( uvc < 256 ) {
1973                         charid = trie->charmap[ uvc ];
1974                     } else {
1975                         SV* const * const svpp = hv_fetch( widecharmap, (char*)&uvc, sizeof( UV ), 0);
1976                         charid = svpp ? (U16)SvIV(*svpp) : 0;
1977                     }
1978                     if ( charid ) {
1979                         charid--;
1980                         if ( !trie->trans[ state + charid ].next ) {
1981                             trie->trans[ state + charid ].next = next_alloc;
1982                             trie->trans[ state ].check++;
1983                             prev_states[TRIE_NODENUM(next_alloc)]
1984                                     = TRIE_NODENUM(state);
1985                             next_alloc += trie->uniquecharcount;
1986                         }
1987                         state = trie->trans[ state + charid ].next;
1988                     } else {
1989                         Perl_croak( aTHX_ "panic! In trie construction, no char mapping for %"IVdf, uvc );
1990                     }
1991                     /* charid is now 0 if we dont know the char read, or nonzero if we do */
1992                 }
1993             }
1994             accept_state = TRIE_NODENUM( state );
1995             TRIE_HANDLE_WORD(accept_state);
1996
1997         } /* end second pass */
1998
1999         /* and now dump it out before we compress it */
2000         DEBUG_TRIE_COMPILE_MORE_r(dump_trie_interim_table(trie, widecharmap,
2001                                                           revcharmap,
2002                                                           next_alloc, depth+1));
2003
2004         {
2005         /*
2006            * Inplace compress the table.*
2007
2008            For sparse data sets the table constructed by the trie algorithm will
2009            be mostly 0/FAIL transitions or to put it another way mostly empty.
2010            (Note that leaf nodes will not contain any transitions.)
2011
2012            This algorithm compresses the tables by eliminating most such
2013            transitions, at the cost of a modest bit of extra work during lookup:
2014
2015            - Each states[] entry contains a .base field which indicates the
2016            index in the state[] array wheres its transition data is stored.
2017
2018            - If .base is 0 there are no valid transitions from that node.
2019
2020            - If .base is nonzero then charid is added to it to find an entry in
2021            the trans array.
2022
2023            -If trans[states[state].base+charid].check!=state then the
2024            transition is taken to be a 0/Fail transition. Thus if there are fail
2025            transitions at the front of the node then the .base offset will point
2026            somewhere inside the previous nodes data (or maybe even into a node
2027            even earlier), but the .check field determines if the transition is
2028            valid.
2029
2030            XXX - wrong maybe?
2031            The following process inplace converts the table to the compressed
2032            table: We first do not compress the root node 1,and mark all its
2033            .check pointers as 1 and set its .base pointer as 1 as well. This
2034            allows us to do a DFA construction from the compressed table later,
2035            and ensures that any .base pointers we calculate later are greater
2036            than 0.
2037
2038            - We set 'pos' to indicate the first entry of the second node.
2039
2040            - We then iterate over the columns of the node, finding the first and
2041            last used entry at l and m. We then copy l..m into pos..(pos+m-l),
2042            and set the .check pointers accordingly, and advance pos
2043            appropriately and repreat for the next node. Note that when we copy
2044            the next pointers we have to convert them from the original
2045            NODEIDX form to NODENUM form as the former is not valid post
2046            compression.
2047
2048            - If a node has no transitions used we mark its base as 0 and do not
2049            advance the pos pointer.
2050
2051            - If a node only has one transition we use a second pointer into the
2052            structure to fill in allocated fail transitions from other states.
2053            This pointer is independent of the main pointer and scans forward
2054            looking for null transitions that are allocated to a state. When it
2055            finds one it writes the single transition into the "hole".  If the
2056            pointer doesnt find one the single transition is appended as normal.
2057
2058            - Once compressed we can Renew/realloc the structures to release the
2059            excess space.
2060
2061            See "Table-Compression Methods" in sec 3.9 of the Red Dragon,
2062            specifically Fig 3.47 and the associated pseudocode.
2063
2064            demq
2065         */
2066         const U32 laststate = TRIE_NODENUM( next_alloc );
2067         U32 state, charid;
2068         U32 pos = 0, zp=0;
2069         trie->statecount = laststate;
2070
2071         for ( state = 1 ; state < laststate ; state++ ) {
2072             U8 flag = 0;
2073             const U32 stateidx = TRIE_NODEIDX( state );
2074             const U32 o_used = trie->trans[ stateidx ].check;
2075             U32 used = trie->trans[ stateidx ].check;
2076             trie->trans[ stateidx ].check = 0;
2077
2078             for ( charid = 0 ; used && charid < trie->uniquecharcount ; charid++ ) {
2079                 if ( flag || trie->trans[ stateidx + charid ].next ) {
2080                     if ( trie->trans[ stateidx + charid ].next ) {
2081                         if (o_used == 1) {
2082                             for ( ; zp < pos ; zp++ ) {
2083                                 if ( ! trie->trans[ zp ].next ) {
2084                                     break;
2085                                 }
2086                             }
2087                             trie->states[ state ].trans.base = zp + trie->uniquecharcount - charid ;
2088                             trie->trans[ zp ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
2089                             trie->trans[ zp ].check = state;
2090                             if ( ++zp > pos ) pos = zp;
2091                             break;
2092                         }
2093                         used--;
2094                     }
2095                     if ( !flag ) {
2096                         flag = 1;
2097                         trie->states[ state ].trans.base = pos + trie->uniquecharcount - charid ;
2098                     }
2099                     trie->trans[ pos ].next = SAFE_TRIE_NODENUM( trie->trans[ stateidx + charid ].next );
2100                     trie->trans[ pos ].check = state;
2101                     pos++;
2102                 }
2103             }
2104         }
2105         trie->lasttrans = pos + 1;
2106         trie->states = (reg_trie_state *)
2107             PerlMemShared_realloc( trie->states, laststate
2108                                    * sizeof(reg_trie_state) );
2109         DEBUG_TRIE_COMPILE_MORE_r(
2110                 PerlIO_printf( Perl_debug_log,
2111                     "%*sAlloc: %d Orig: %"IVdf" elements, Final:%"IVdf". Savings of %%%5.2f\n",
2112                     (int)depth * 2 + 2,"",
2113                     (int)( ( TRIE_CHARCOUNT(trie) + 1 ) * trie->uniquecharcount + 1 ),
2114                     (IV)next_alloc,
2115                     (IV)pos,
2116                     ( ( next_alloc - pos ) * 100 ) / (double)next_alloc );
2117             );
2118
2119         } /* end table compress */
2120     }
2121     DEBUG_TRIE_COMPILE_MORE_r(
2122             PerlIO_printf(Perl_debug_log, "%*sStatecount:%"UVxf" Lasttrans:%"UVxf"\n",
2123                 (int)depth * 2 + 2, "",
2124                 (UV)trie->statecount,
2125                 (UV)trie->lasttrans)
2126     );
2127     /* resize the trans array to remove unused space */
2128     trie->trans = (reg_trie_trans *)
2129         PerlMemShared_realloc( trie->trans, trie->lasttrans
2130                                * sizeof(reg_trie_trans) );
2131
2132     {   /* Modify the program and insert the new TRIE node */ 
2133         U8 nodetype =(U8)(flags & 0xFF);
2134         char *str=NULL;
2135         
2136 #ifdef DEBUGGING
2137         regnode *optimize = NULL;
2138 #ifdef RE_TRACK_PATTERN_OFFSETS
2139
2140         U32 mjd_offset = 0;
2141         U32 mjd_nodelen = 0;
2142 #endif /* RE_TRACK_PATTERN_OFFSETS */
2143 #endif /* DEBUGGING */
2144         /*
2145            This means we convert either the first branch or the first Exact,
2146            depending on whether the thing following (in 'last') is a branch
2147            or not and whther first is the startbranch (ie is it a sub part of
2148            the alternation or is it the whole thing.)
2149            Assuming its a sub part we convert the EXACT otherwise we convert
2150            the whole branch sequence, including the first.
2151          */
2152         /* Find the node we are going to overwrite */
2153         if ( first != startbranch || OP( last ) == BRANCH ) {
2154             /* branch sub-chain */
2155             NEXT_OFF( first ) = (U16)(last - first);
2156 #ifdef RE_TRACK_PATTERN_OFFSETS
2157             DEBUG_r({
2158                 mjd_offset= Node_Offset((convert));
2159                 mjd_nodelen= Node_Length((convert));
2160             });
2161 #endif
2162             /* whole branch chain */
2163         }
2164 #ifdef RE_TRACK_PATTERN_OFFSETS
2165         else {
2166             DEBUG_r({
2167                 const  regnode *nop = NEXTOPER( convert );
2168                 mjd_offset= Node_Offset((nop));
2169                 mjd_nodelen= Node_Length((nop));
2170             });
2171         }
2172         DEBUG_OPTIMISE_r(
2173             PerlIO_printf(Perl_debug_log, "%*sMJD offset:%"UVuf" MJD length:%"UVuf"\n",
2174                 (int)depth * 2 + 2, "",
2175                 (UV)mjd_offset, (UV)mjd_nodelen)
2176         );
2177 #endif
2178         /* But first we check to see if there is a common prefix we can 
2179            split out as an EXACT and put in front of the TRIE node.  */
2180         trie->startstate= 1;
2181         if ( trie->bitmap && !widecharmap && !trie->jump  ) {
2182             U32 state;
2183             for ( state = 1 ; state < trie->statecount-1 ; state++ ) {
2184                 U32 ofs = 0;
2185                 I32 idx = -1;
2186                 U32 count = 0;
2187                 const U32 base = trie->states[ state ].trans.base;
2188
2189                 if ( trie->states[state].wordnum )
2190                         count = 1;
2191
2192                 for ( ofs = 0 ; ofs < trie->uniquecharcount ; ofs++ ) {
2193                     if ( ( base + ofs >= trie->uniquecharcount ) &&
2194                          ( base + ofs - trie->uniquecharcount < trie->lasttrans ) &&
2195                          trie->trans[ base + ofs - trie->uniquecharcount ].check == state )
2196                     {
2197                         if ( ++count > 1 ) {
2198                             SV **tmp = av_fetch( revcharmap, ofs, 0);
2199                             const U8 *ch = (U8*)SvPV_nolen_const( *tmp );
2200                             if ( state == 1 ) break;
2201                             if ( count == 2 ) {
2202                                 Zero(trie->bitmap, ANYOF_BITMAP_SIZE, char);
2203                                 DEBUG_OPTIMISE_r(
2204                                     PerlIO_printf(Perl_debug_log,
2205                                         "%*sNew Start State=%"UVuf" Class: [",
2206                                         (int)depth * 2 + 2, "",
2207                                         (UV)state));
2208                                 if (idx >= 0) {
2209                                     SV ** const tmp = av_fetch( revcharmap, idx, 0);
2210                                     const U8 * const ch = (U8*)SvPV_nolen_const( *tmp );
2211
2212                                     TRIE_BITMAP_SET(trie,*ch);
2213                                     if ( folder )
2214                                         TRIE_BITMAP_SET(trie, folder[ *ch ]);
2215                                     DEBUG_OPTIMISE_r(
2216                                         PerlIO_printf(Perl_debug_log, "%s", (char*)ch)
2217                                     );
2218                                 }
2219                             }
2220                             TRIE_BITMAP_SET(trie,*ch);
2221                             if ( folder )
2222                                 TRIE_BITMAP_SET(trie,folder[ *ch ]);
2223                             DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"%s", ch));
2224                         }
2225                         idx = ofs;
2226                     }
2227                 }
2228                 if ( count == 1 ) {
2229                     SV **tmp = av_fetch( revcharmap, idx, 0);
2230                     STRLEN len;
2231                     char *ch = SvPV( *tmp, len );
2232                     DEBUG_OPTIMISE_r({
2233                         SV *sv=sv_newmortal();
2234                         PerlIO_printf( Perl_debug_log,
2235                             "%*sPrefix State: %"UVuf" Idx:%"UVuf" Char='%s'\n",
2236                             (int)depth * 2 + 2, "",
2237                             (UV)state, (UV)idx, 
2238                             pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 6, 
2239                                 PL_colors[0], PL_colors[1],
2240                                 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) |
2241                                 PERL_PV_ESCAPE_FIRSTCHAR 
2242                             )
2243                         );
2244                     });
2245                     if ( state==1 ) {
2246                         OP( convert ) = nodetype;
2247                         str=STRING(convert);
2248                         STR_LEN(convert)=0;
2249                     }
2250                     STR_LEN(convert) += len;
2251                     while (len--)
2252                         *str++ = *ch++;
2253                 } else {
2254 #ifdef DEBUGGING            
2255                     if (state>1)
2256                         DEBUG_OPTIMISE_r(PerlIO_printf( Perl_debug_log,"]\n"));
2257 #endif
2258                     break;
2259                 }
2260             }
2261             trie->prefixlen = (state-1);
2262             if (str) {
2263                 regnode *n = convert+NODE_SZ_STR(convert);
2264                 NEXT_OFF(convert) = NODE_SZ_STR(convert);
2265                 trie->startstate = state;
2266                 trie->minlen -= (state - 1);
2267                 trie->maxlen -= (state - 1);
2268 #ifdef DEBUGGING
2269                /* At least the UNICOS C compiler choked on this
2270                 * being argument to DEBUG_r(), so let's just have
2271                 * it right here. */
2272                if (
2273 #ifdef PERL_EXT_RE_BUILD
2274                    1
2275 #else
2276                    DEBUG_r_TEST
2277 #endif
2278                    ) {
2279                    regnode *fix = convert;
2280                    U32 word = trie->wordcount;
2281                    mjd_nodelen++;
2282                    Set_Node_Offset_Length(convert, mjd_offset, state - 1);
2283                    while( ++fix < n ) {
2284                        Set_Node_Offset_Length(fix, 0, 0);
2285                    }
2286                    while (word--) {
2287                        SV ** const tmp = av_fetch( trie_words, word, 0 );
2288                        if (tmp) {
2289                            if ( STR_LEN(convert) <= SvCUR(*tmp) )
2290                                sv_chop(*tmp, SvPV_nolen(*tmp) + STR_LEN(convert));
2291                            else
2292                                sv_chop(*tmp, SvPV_nolen(*tmp) + SvCUR(*tmp));
2293                        }
2294                    }
2295                }
2296 #endif
2297                 if (trie->maxlen) {
2298                     convert = n;
2299                 } else {
2300                     NEXT_OFF(convert) = (U16)(tail - convert);
2301                     DEBUG_r(optimize= n);
2302                 }
2303             }
2304         }
2305         if (!jumper) 
2306             jumper = last; 
2307         if ( trie->maxlen ) {
2308             NEXT_OFF( convert ) = (U16)(tail - convert);
2309             ARG_SET( convert, data_slot );
2310             /* Store the offset to the first unabsorbed branch in 
2311                jump[0], which is otherwise unused by the jump logic. 
2312                We use this when dumping a trie and during optimisation. */
2313             if (trie->jump) 
2314                 trie->jump[0] = (U16)(nextbranch - convert);
2315             
2316             /* If the start state is not accepting (meaning there is no empty string/NOTHING)
2317              *   and there is a bitmap
2318              *   and the first "jump target" node we found leaves enough room
2319              * then convert the TRIE node into a TRIEC node, with the bitmap
2320              * embedded inline in the opcode - this is hypothetically faster.
2321              */
2322             if ( !trie->states[trie->startstate].wordnum
2323                  && trie->bitmap
2324                  && ( (char *)jumper - (char *)convert) >= (int)sizeof(struct regnode_charclass) )
2325             {
2326                 OP( convert ) = TRIEC;
2327                 Copy(trie->bitmap, ((struct regnode_charclass *)convert)->bitmap, ANYOF_BITMAP_SIZE, char);
2328                 PerlMemShared_free(trie->bitmap);
2329                 trie->bitmap= NULL;
2330             } else 
2331                 OP( convert ) = TRIE;
2332
2333             /* store the type in the flags */
2334             convert->flags = nodetype;
2335             DEBUG_r({
2336             optimize = convert 
2337                       + NODE_STEP_REGNODE 
2338                       + regarglen[ OP( convert ) ];
2339             });
2340             /* XXX We really should free up the resource in trie now, 
2341                    as we won't use them - (which resources?) dmq */
2342         }
2343         /* needed for dumping*/
2344         DEBUG_r(if (optimize) {
2345             regnode *opt = convert;
2346
2347             while ( ++opt < optimize) {
2348                 Set_Node_Offset_Length(opt,0,0);
2349             }
2350             /* 
2351                 Try to clean up some of the debris left after the 
2352                 optimisation.
2353              */
2354             while( optimize < jumper ) {
2355                 mjd_nodelen += Node_Length((optimize));
2356                 OP( optimize ) = OPTIMIZED;
2357                 Set_Node_Offset_Length(optimize,0,0);
2358                 optimize++;
2359             }
2360             Set_Node_Offset_Length(convert,mjd_offset,mjd_nodelen);
2361         });
2362     } /* end node insert */
2363     REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, convert);
2364
2365     /*  Finish populating the prev field of the wordinfo array.  Walk back
2366      *  from each accept state until we find another accept state, and if
2367      *  so, point the first word's .prev field at the second word. If the
2368      *  second already has a .prev field set, stop now. This will be the
2369      *  case either if we've already processed that word's accept state,
2370      *  or that state had multiple words, and the overspill words were
2371      *  already linked up earlier.
2372      */
2373     {
2374         U16 word;
2375         U32 state;
2376         U16 prev;
2377
2378         for (word=1; word <= trie->wordcount; word++) {
2379             prev = 0;
2380             if (trie->wordinfo[word].prev)
2381                 continue;
2382             state = trie->wordinfo[word].accept;
2383             while (state) {
2384                 state = prev_states[state];
2385                 if (!state)
2386                     break;
2387                 prev = trie->states[state].wordnum;
2388                 if (prev)
2389                     break;
2390             }
2391             trie->wordinfo[word].prev = prev;
2392         }
2393         Safefree(prev_states);
2394     }
2395
2396
2397     /* and now dump out the compressed format */
2398     DEBUG_TRIE_COMPILE_r(dump_trie(trie, widecharmap, revcharmap, depth+1));
2399
2400     RExC_rxi->data->data[ data_slot + 1 ] = (void*)widecharmap;
2401 #ifdef DEBUGGING
2402     RExC_rxi->data->data[ data_slot + TRIE_WORDS_OFFSET ] = (void*)trie_words;
2403     RExC_rxi->data->data[ data_slot + 3 ] = (void*)revcharmap;
2404 #else
2405     SvREFCNT_dec(revcharmap);
2406 #endif
2407     return trie->jump 
2408            ? MADE_JUMP_TRIE 
2409            : trie->startstate>1 
2410              ? MADE_EXACT_TRIE 
2411              : MADE_TRIE;
2412 }
2413
2414 STATIC void
2415 S_make_trie_failtable(pTHX_ RExC_state_t *pRExC_state, regnode *source,  regnode *stclass, U32 depth)
2416 {
2417 /* The Trie is constructed and compressed now so we can build a fail array if it's needed
2418
2419    This is basically the Aho-Corasick algorithm. Its from exercise 3.31 and 3.32 in the
2420    "Red Dragon" -- Compilers, principles, techniques, and tools. Aho, Sethi, Ullman 1985/88
2421    ISBN 0-201-10088-6
2422
2423    We find the fail state for each state in the trie, this state is the longest proper
2424    suffix of the current state's 'word' that is also a proper prefix of another word in our
2425    trie. State 1 represents the word '' and is thus the default fail state. This allows
2426    the DFA not to have to restart after its tried and failed a word at a given point, it
2427    simply continues as though it had been matching the other word in the first place.
2428    Consider
2429       'abcdgu'=~/abcdefg|cdgu/
2430    When we get to 'd' we are still matching the first word, we would encounter 'g' which would
2431    fail, which would bring us to the state representing 'd' in the second word where we would
2432    try 'g' and succeed, proceeding to match 'cdgu'.
2433  */
2434  /* add a fail transition */
2435     const U32 trie_offset = ARG(source);
2436     reg_trie_data *trie=(reg_trie_data *)RExC_rxi->data->data[trie_offset];
2437     U32 *q;
2438     const U32 ucharcount = trie->uniquecharcount;
2439     const U32 numstates = trie->statecount;
2440     const U32 ubound = trie->lasttrans + ucharcount;
2441     U32 q_read = 0;
2442     U32 q_write = 0;
2443     U32 charid;
2444     U32 base = trie->states[ 1 ].trans.base;
2445     U32 *fail;
2446     reg_ac_data *aho;
2447     const U32 data_slot = add_data( pRExC_state, 1, "T" );
2448     GET_RE_DEBUG_FLAGS_DECL;
2449
2450     PERL_ARGS_ASSERT_MAKE_TRIE_FAILTABLE;
2451 #ifndef DEBUGGING
2452     PERL_UNUSED_ARG(depth);
2453 #endif
2454
2455
2456     ARG_SET( stclass, data_slot );
2457     aho = (reg_ac_data *) PerlMemShared_calloc( 1, sizeof(reg_ac_data) );
2458     RExC_rxi->data->data[ data_slot ] = (void*)aho;
2459     aho->trie=trie_offset;
2460     aho->states=(reg_trie_state *)PerlMemShared_malloc( numstates * sizeof(reg_trie_state) );
2461     Copy( trie->states, aho->states, numstates, reg_trie_state );
2462     Newxz( q, numstates, U32);
2463     aho->fail = (U32 *) PerlMemShared_calloc( numstates, sizeof(U32) );
2464     aho->refcount = 1;
2465     fail = aho->fail;
2466     /* initialize fail[0..1] to be 1 so that we always have
2467        a valid final fail state */
2468     fail[ 0 ] = fail[ 1 ] = 1;
2469
2470     for ( charid = 0; charid < ucharcount ; charid++ ) {
2471         const U32 newstate = TRIE_TRANS_STATE( 1, base, ucharcount, charid, 0 );
2472         if ( newstate ) {
2473             q[ q_write ] = newstate;
2474             /* set to point at the root */
2475             fail[ q[ q_write++ ] ]=1;
2476         }
2477     }
2478     while ( q_read < q_write) {
2479         const U32 cur = q[ q_read++ % numstates ];
2480         base = trie->states[ cur ].trans.base;
2481
2482         for ( charid = 0 ; charid < ucharcount ; charid++ ) {
2483             const U32 ch_state = TRIE_TRANS_STATE( cur, base, ucharcount, charid, 1 );
2484             if (ch_state) {
2485                 U32 fail_state = cur;
2486                 U32 fail_base;
2487                 do {
2488                     fail_state = fail[ fail_state ];
2489                     fail_base = aho->states[ fail_state ].trans.base;
2490                 } while ( !TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 ) );
2491
2492                 fail_state = TRIE_TRANS_STATE( fail_state, fail_base, ucharcount, charid, 1 );
2493                 fail[ ch_state ] = fail_state;
2494                 if ( !aho->states[ ch_state ].wordnum && aho->states[ fail_state ].wordnum )
2495                 {
2496                         aho->states[ ch_state ].wordnum =  aho->states[ fail_state ].wordnum;
2497                 }
2498                 q[ q_write++ % numstates] = ch_state;
2499             }
2500         }
2501     }
2502     /* restore fail[0..1] to 0 so that we "fall out" of the AC loop
2503        when we fail in state 1, this allows us to use the
2504        charclass scan to find a valid start char. This is based on the principle
2505        that theres a good chance the string being searched contains lots of stuff
2506        that cant be a start char.
2507      */
2508     fail[ 0 ] = fail[ 1 ] = 0;
2509     DEBUG_TRIE_COMPILE_r({
2510         PerlIO_printf(Perl_debug_log,
2511                       "%*sStclass Failtable (%"UVuf" states): 0", 
2512                       (int)(depth * 2), "", (UV)numstates
2513         );
2514         for( q_read=1; q_read<numstates; q_read++ ) {
2515             PerlIO_printf(Perl_debug_log, ", %"UVuf, (UV)fail[q_read]);
2516         }
2517         PerlIO_printf(Perl_debug_log, "\n");
2518     });
2519     Safefree(q);
2520     /*RExC_seen |= REG_SEEN_TRIEDFA;*/
2521 }
2522
2523
2524 /*
2525  * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2526  * These need to be revisited when a newer toolchain becomes available.
2527  */
2528 #if defined(__sparc64__) && defined(__GNUC__)
2529 #   if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2530 #       undef  SPARC64_GCC_WORKAROUND
2531 #       define SPARC64_GCC_WORKAROUND 1
2532 #   endif
2533 #endif
2534
2535 #define DEBUG_PEEP(str,scan,depth) \
2536     DEBUG_OPTIMISE_r({if (scan){ \
2537        SV * const mysv=sv_newmortal(); \
2538        regnode *Next = regnext(scan); \
2539        regprop(RExC_rx, mysv, scan); \
2540        PerlIO_printf(Perl_debug_log, "%*s" str ">%3d: %s (%d)\n", \
2541        (int)depth*2, "", REG_NODE_NUM(scan), SvPV_nolen_const(mysv),\
2542        Next ? (REG_NODE_NUM(Next)) : 0 ); \
2543    }});
2544
2545
2546 /* The below joins as many adjacent EXACTish nodes as possible into a single
2547  * one, and looks for problematic sequences of characters whose folds vs.
2548  * non-folds have sufficiently different lengths, that the optimizer would be
2549  * fooled into rejecting legitimate matches of them, and the trie construction
2550  * code can't cope with them.  The joining is only done if:
2551  * 1) there is room in the current conglomerated node to entirely contain the
2552  *    next one.
2553  * 2) they are the exact same node type
2554  *
2555  * The adjacent nodes actually may be separated by NOTHING kind nodes, and
2556  * these get optimized out
2557  *
2558  * If there are problematic code sequences, *min_subtract is set to the delta
2559  * that the minimum size of the node can be less than its actual size.  And,
2560  * the node type of the result is changed to reflect that it contains these
2561  * sequences.
2562  *
2563  * And *has_exactf_sharp_s is set to indicate whether or not the node is EXACTF
2564  * and contains LATIN SMALL LETTER SHARP S
2565  *
2566  * This is as good a place as any to discuss the design of handling these
2567  * problematic sequences.  It's been wrong in Perl for a very long time.  There
2568  * are three code points in Unicode whose folded lengths differ so much from
2569  * the un-folded lengths that it causes problems for the optimizer and trie
2570  * construction.  Why only these are problematic, and not others where lengths
2571  * also differ is something I (khw) do not understand.  New versions of Unicode
2572  * might add more such code points.  Hopefully the logic in fold_grind.t that
2573  * figures out what to test (in part by verifying that each size-combination
2574  * gets tested) will catch any that do come along, so they can be added to the
2575  * special handling below.  The chances of new ones are actually rather small,
2576  * as most, if not all, of the world's scripts that have casefolding have
2577  * already been encoded by Unicode.  Also, a number of Unicode's decisions were
2578  * made to allow compatibility with pre-existing standards, and almost all of
2579  * those have already been dealt with.  These would otherwise be the most
2580  * likely candidates for generating further tricky sequences.  In other words,
2581  * Unicode by itself is unlikely to add new ones unless it is for compatibility
2582  * with pre-existing standards, and there aren't many of those left.
2583  *
2584  * The previous designs for dealing with these involved assigning a special
2585  * node for them.  This approach doesn't work, as evidenced by this example:
2586  *      "\xDFs" =~ /s\xDF/ui    # Used to fail before these patches
2587  * Both these fold to "sss", but if the pattern is parsed to create a node of
2588  * that would match just the \xDF, it won't be able to handle the case where a
2589  * successful match would have to cross the node's boundary.  The new approach
2590  * that hopefully generally solves the problem generates an EXACTFU_SS node
2591  * that is "sss".
2592  *
2593  * There are a number of components to the approach (a lot of work for just
2594  * three code points!):
2595  * 1)   This routine examines each EXACTFish node that could contain the
2596  *      problematic sequences.  It returns in *min_subtract how much to
2597  *      subtract from the the actual length of the string to get a real minimum
2598  *      for one that could match it.  This number is usually 0 except for the
2599  *      problematic sequences.  This delta is used by the caller to adjust the
2600  *      min length of the match, and the delta between min and max, so that the
2601  *      optimizer doesn't reject these possibilities based on size constraints.
2602  * 2)   These sequences are not currently correctly handled by the trie code
2603  *      either, so it changes the joined node type to ops that are not handled
2604  *      by trie's, those new ops being EXACTFU_SS and EXACTFU_TRICKYFOLD.
2605  * 3)   This is sufficient for the two Greek sequences (described below), but
2606  *      the one involving the Sharp s (\xDF) needs more.  The node type
2607  *      EXACTFU_SS is used for an EXACTFU node that contains at least one "ss"
2608  *      sequence in it.  For non-UTF-8 patterns and strings, this is the only
2609  *      case where there is a possible fold length change.  That means that a
2610  *      regular EXACTFU node without UTF-8 involvement doesn't have to concern
2611  *      itself with length changes, and so can be processed faster.  regexec.c
2612  *      takes advantage of this.  Generally, an EXACTFish node that is in UTF-8
2613  *      is pre-folded by regcomp.c.  This saves effort in regex matching.
2614  *      However, probably mostly for historical reasons, the pre-folding isn't
2615  *      done for non-UTF8 patterns (and it can't be for EXACTF and EXACTFL
2616  *      nodes, as what they fold to isn't known until runtime.)  The fold
2617  *      possibilities for the non-UTF8 patterns are quite simple, except for
2618  *      the sharp s.  All the ones that don't involve a UTF-8 target string
2619  *      are members of a fold-pair, and arrays are set up for all of them
2620  *      that quickly find the other member of the pair.  It might actually
2621  *      be faster to pre-fold these, but it isn't currently done, except for
2622  *      the sharp s.  Code elsewhere in this file makes sure that it gets
2623  *      folded to 'ss', even if the pattern isn't UTF-8.  This avoids the
2624  *      issues described in the next item.
2625  * 4)   A problem remains for the sharp s in EXACTF nodes.  Whether it matches
2626  *      'ss' or not is not knowable at compile time.  It will match iff the
2627  *      target string is in UTF-8, unlike the EXACTFU nodes, where it always
2628  *      matches; and the EXACTFL and EXACTFA nodes where it never does.  Thus
2629  *      it can't be folded to "ss" at compile time, unlike EXACTFU does as
2630  *      described in item 3).  An assumption that the optimizer part of
2631  *      regexec.c (probably unwittingly) makes is that a character in the
2632  *      pattern corresponds to at most a single character in the target string.
2633  *      (And I do mean character, and not byte here, unlike other parts of the
2634  *      documentation that have never been updated to account for multibyte
2635  *      Unicode.)  This assumption is wrong only in this case, as all other
2636  *      cases are either 1-1 folds when no UTF-8 is involved; or is true by
2637  *      virtue of having this file pre-fold UTF-8 patterns.   I'm
2638  *      reluctant to try to change this assumption, so instead the code punts.
2639  *      This routine examines EXACTF nodes for the sharp s, and returns a
2640  *      boolean indicating whether or not the node is an EXACTF node that
2641  *      contains a sharp s.  When it is true, the caller sets a flag that later
2642  *      causes the optimizer in this file to not set values for the floating
2643  *      and fixed string lengths, and thus avoids the optimizer code in
2644  *      regexec.c that makes the invalid assumption.  Thus, there is no
2645  *      optimization based on string lengths for EXACTF nodes that contain the
2646  *      sharp s.  This only happens for /id rules (which means the pattern
2647  *      isn't in UTF-8).
2648  */
2649
2650 #define JOIN_EXACT(scan,min_subtract,has_exactf_sharp_s, flags) \
2651     if (PL_regkind[OP(scan)] == EXACT) \
2652         join_exact(pRExC_state,(scan),(min_subtract),has_exactf_sharp_s, (flags),NULL,depth+1)
2653
2654 STATIC U32
2655 S_join_exact(pTHX_ RExC_state_t *pRExC_state, regnode *scan, UV *min_subtract, bool *has_exactf_sharp_s, U32 flags,regnode *val, U32 depth) {
2656     /* Merge several consecutive EXACTish nodes into one. */
2657     regnode *n = regnext(scan);
2658     U32 stringok = 1;
2659     regnode *next = scan + NODE_SZ_STR(scan);
2660     U32 merged = 0;
2661     U32 stopnow = 0;
2662 #ifdef DEBUGGING
2663     regnode *stop = scan;
2664     GET_RE_DEBUG_FLAGS_DECL;
2665 #else
2666     PERL_UNUSED_ARG(depth);
2667 #endif
2668
2669     PERL_ARGS_ASSERT_JOIN_EXACT;
2670 #ifndef EXPERIMENTAL_INPLACESCAN
2671     PERL_UNUSED_ARG(flags);
2672     PERL_UNUSED_ARG(val);
2673 #endif
2674     DEBUG_PEEP("join",scan,depth);
2675
2676     /* Look through the subsequent nodes in the chain.  Skip NOTHING, merge
2677      * EXACT ones that are mergeable to the current one. */
2678     while (n
2679            && (PL_regkind[OP(n)] == NOTHING
2680                || (stringok && OP(n) == OP(scan)))
2681            && NEXT_OFF(n)
2682            && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX)
2683     {
2684         
2685         if (OP(n) == TAIL || n > next)
2686             stringok = 0;
2687         if (PL_regkind[OP(n)] == NOTHING) {
2688             DEBUG_PEEP("skip:",n,depth);
2689             NEXT_OFF(scan) += NEXT_OFF(n);
2690             next = n + NODE_STEP_REGNODE;
2691 #ifdef DEBUGGING
2692             if (stringok)
2693                 stop = n;
2694 #endif
2695             n = regnext(n);
2696         }
2697         else if (stringok) {
2698             const unsigned int oldl = STR_LEN(scan);
2699             regnode * const nnext = regnext(n);
2700
2701             if (oldl + STR_LEN(n) > U8_MAX)
2702                 break;
2703             
2704             DEBUG_PEEP("merg",n,depth);
2705             merged++;
2706
2707             NEXT_OFF(scan) += NEXT_OFF(n);
2708             STR_LEN(scan) += STR_LEN(n);
2709             next = n + NODE_SZ_STR(n);
2710             /* Now we can overwrite *n : */
2711             Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
2712 #ifdef DEBUGGING
2713             stop = next - 1;
2714 #endif
2715             n = nnext;
2716             if (stopnow) break;
2717         }
2718
2719 #ifdef EXPERIMENTAL_INPLACESCAN
2720         if (flags && !NEXT_OFF(n)) {
2721             DEBUG_PEEP("atch", val, depth);
2722             if (reg_off_by_arg[OP(n)]) {
2723                 ARG_SET(n, val - n);
2724             }
2725             else {
2726                 NEXT_OFF(n) = val - n;
2727             }
2728             stopnow = 1;
2729         }
2730 #endif
2731     }
2732
2733     *min_subtract = 0;
2734     *has_exactf_sharp_s = FALSE;
2735
2736     /* Here, all the adjacent mergeable EXACTish nodes have been merged.  We
2737      * can now analyze for sequences of problematic code points.  (Prior to
2738      * this final joining, sequences could have been split over boundaries, and
2739      * hence missed).  The sequences only happen in folding, hence for any
2740      * non-EXACT EXACTish node */
2741     if (OP(scan) != EXACT) {
2742         U8 *s;
2743         U8 * s0 = (U8*) STRING(scan);
2744         U8 * const s_end = s0 + STR_LEN(scan);
2745
2746         /* The below is perhaps overboard, but this allows us to save a test
2747          * each time through the loop at the expense of a mask.  This is
2748          * because on both EBCDIC and ASCII machines, 'S' and 's' differ by a
2749          * single bit.  On ASCII they are 32 apart; on EBCDIC, they are 64.
2750          * This uses an exclusive 'or' to find that bit and then inverts it to
2751          * form a mask, with just a single 0, in the bit position where 'S' and
2752          * 's' differ. */
2753         const U8 S_or_s_mask = (U8) ~ ('S' ^ 's');
2754         const U8 s_masked = 's' & S_or_s_mask;
2755
2756         /* One pass is made over the node's string looking for all the
2757          * possibilities.  to avoid some tests in the loop, there are two main
2758          * cases, for UTF-8 patterns (which can't have EXACTF nodes) and
2759          * non-UTF-8 */
2760         if (UTF) {
2761
2762             /* There are two problematic Greek code points in Unicode
2763              * casefolding
2764              *
2765              * U+0390 - GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
2766              * U+03B0 - GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
2767              *
2768              * which casefold to
2769              *
2770              * Unicode                      UTF-8
2771              *
2772              * U+03B9 U+0308 U+0301         0xCE 0xB9 0xCC 0x88 0xCC 0x81
2773              * U+03C5 U+0308 U+0301         0xCF 0x85 0xCC 0x88 0xCC 0x81
2774              *
2775              * This means that in case-insensitive matching (or "loose
2776              * matching", as Unicode calls it), an EXACTF of length six (the
2777              * UTF-8 encoded byte length of the above casefolded versions) can
2778              * match a target string of length two (the byte length of UTF-8
2779              * encoded U+0390 or U+03B0).  This would rather mess up the
2780              * minimum length computation.  (there are other code points that
2781              * also fold to these two sequences, but the delta is smaller)
2782              *
2783              * If these sequences are found, the minimum length is decreased by
2784              * four (six minus two).
2785              *
2786              * Similarly, 'ss' may match the single char and byte LATIN SMALL
2787              * LETTER SHARP S.  We decrease the min length by 1 for each
2788              * occurrence of 'ss' found */
2789
2790 #ifdef EBCDIC /* RD tunifold greek 0390 and 03B0 */
2791 #           define U390_first_byte 0xb4
2792             const U8 U390_tail[] = "\x68\xaf\x49\xaf\x42";
2793 #           define U3B0_first_byte 0xb5
2794             const U8 U3B0_tail[] = "\x46\xaf\x49\xaf\x42";
2795 #else
2796 #           define U390_first_byte 0xce
2797             const U8 U390_tail[] = "\xb9\xcc\x88\xcc\x81";
2798 #           define U3B0_first_byte 0xcf
2799             const U8 U3B0_tail[] = "\x85\xcc\x88\xcc\x81";
2800 #endif
2801             const U8 len = sizeof(U390_tail); /* (-1 for NUL; +1 for 1st byte;
2802                                                  yields a net of 0 */
2803             /* Examine the string for one of the problematic sequences */
2804             for (s = s0;
2805                  s < s_end - 1; /* Can stop 1 before the end, as minimum length
2806                                  * sequence we are looking for is 2 */
2807                  s += UTF8SKIP(s))
2808             {
2809
2810                 /* Look for the first byte in each problematic sequence */
2811                 switch (*s) {
2812                     /* We don't have to worry about other things that fold to
2813                      * 's' (such as the long s, U+017F), as all above-latin1
2814                      * code points have been pre-folded */
2815                     case 's':
2816                     case 'S':
2817
2818                         /* Current character is an 's' or 'S'.  If next one is
2819                          * as well, we have the dreaded sequence */
2820                         if (((*(s+1) & S_or_s_mask) == s_masked)
2821                             /* These two node types don't have special handling
2822                              * for 'ss' */
2823                             && OP(scan) != EXACTFL && OP(scan) != EXACTFA)
2824                         {
2825                             *min_subtract += 1;
2826                             OP(scan) = EXACTFU_SS;
2827                             s++;    /* No need to look at this character again */
2828                         }
2829                         break;
2830
2831                     case U390_first_byte:
2832                         if (s_end - s >= len
2833
2834                             /* The 1's are because are skipping comparing the
2835                              * first byte */
2836                             && memEQ(s + 1, U390_tail, len - 1))
2837                         {
2838                             goto greek_sequence;
2839                         }
2840                         break;
2841
2842                     case U3B0_first_byte:
2843                         if (! (s_end - s >= len
2844                                && memEQ(s + 1, U3B0_tail, len - 1)))
2845                         {
2846                             break;
2847                         }
2848                       greek_sequence:
2849                         *min_subtract += 4;
2850
2851                         /* This can't currently be handled by trie's, so change
2852                          * the node type to indicate this.  If EXACTFA and
2853                          * EXACTFL were ever to be handled by trie's, this
2854                          * would have to be changed.  If this node has already
2855                          * been changed to EXACTFU_SS in this loop, leave it as
2856                          * is.  (I (khw) think it doesn't matter in regexec.c
2857                          * for UTF patterns, but no need to change it */
2858                         if (OP(scan) == EXACTFU) {
2859                             OP(scan) = EXACTFU_TRICKYFOLD;
2860                         }
2861                         s += 6; /* We already know what this sequence is.  Skip
2862                                    the rest of it */
2863                         break;
2864                 }
2865             }
2866         }
2867         else if (OP(scan) != EXACTFL && OP(scan) != EXACTFA) {
2868
2869             /* Here, the pattern is not UTF-8.  We need to look only for the
2870              * 'ss' sequence, and in the EXACTF case, the sharp s, which can be
2871              * in the final position.  Otherwise we can stop looking 1 byte
2872              * earlier because have to find both the first and second 's' */
2873             const U8* upper = (OP(scan) == EXACTF) ? s_end : s_end -1;
2874
2875             for (s = s0; s < upper; s++) {
2876                 switch (*s) {
2877                     case 'S':
2878                     case 's':
2879                         if (s_end - s > 1
2880                             && ((*(s+1) & S_or_s_mask) == s_masked))
2881                         {
2882                             *min_subtract += 1;
2883
2884                             /* EXACTF nodes need to know that the minimum
2885                              * length changed so that a sharp s in the string
2886                              * can match this ss in the pattern, but they
2887                              * remain EXACTF nodes, as they are not trie'able,
2888                              * so don't have to invent a new node type to
2889                              * exclude them from the trie code */
2890                             if (OP(scan) != EXACTF) {
2891                                 OP(scan) = EXACTFU_SS;
2892                             }
2893                             s++;
2894                         }
2895                         break;
2896                     case LATIN_SMALL_LETTER_SHARP_S:
2897                         if (OP(scan) == EXACTF) {
2898                             *has_exactf_sharp_s = TRUE;
2899                         }
2900                         break;
2901                 }
2902             }
2903         }
2904     }
2905
2906 #ifdef DEBUGGING
2907     /* Allow dumping but overwriting the collection of skipped
2908      * ops and/or strings with fake optimized ops */
2909     n = scan + NODE_SZ_STR(scan);
2910     while (n <= stop) {
2911         OP(n) = OPTIMIZED;
2912         FLAGS(n) = 0;
2913         NEXT_OFF(n) = 0;
2914         n++;
2915     }
2916 #endif
2917     DEBUG_OPTIMISE_r(if (merged){DEBUG_PEEP("finl",scan,depth)});
2918     return stopnow;
2919 }
2920
2921 /* REx optimizer.  Converts nodes into quicker variants "in place".
2922    Finds fixed substrings.  */
2923
2924 /* Stops at toplevel WHILEM as well as at "last". At end *scanp is set
2925    to the position after last scanned or to NULL. */
2926
2927 #define INIT_AND_WITHP \
2928     assert(!and_withp); \
2929     Newx(and_withp,1,struct regnode_charclass_class); \
2930     SAVEFREEPV(and_withp)
2931
2932 /* this is a chain of data about sub patterns we are processing that
2933    need to be handled separately/specially in study_chunk. Its so
2934    we can simulate recursion without losing state.  */
2935 struct scan_frame;
2936 typedef struct scan_frame {
2937     regnode *last;  /* last node to process in this frame */
2938     regnode *next;  /* next node to process when last is reached */
2939     struct scan_frame *prev; /*previous frame*/
2940     I32 stop; /* what stopparen do we use */
2941 } scan_frame;
2942
2943
2944 #define SCAN_COMMIT(s, data, m) scan_commit(s, data, m, is_inf)
2945
2946 #define CASE_SYNST_FNC(nAmE)                                       \
2947 case nAmE:                                                         \
2948     if (flags & SCF_DO_STCLASS_AND) {                              \
2949             for (value = 0; value < 256; value++)                  \
2950                 if (!is_ ## nAmE ## _cp(value))                       \
2951                     ANYOF_BITMAP_CLEAR(data->start_class, value);  \
2952     }                                                              \
2953     else {                                                         \
2954             for (value = 0; value < 256; value++)                  \
2955                 if (is_ ## nAmE ## _cp(value))                        \
2956                     ANYOF_BITMAP_SET(data->start_class, value);    \
2957     }                                                              \
2958     break;                                                         \
2959 case N ## nAmE:                                                    \
2960     if (flags & SCF_DO_STCLASS_AND) {                              \
2961             for (value = 0; value < 256; value++)                   \
2962                 if (is_ ## nAmE ## _cp(value))                         \
2963                     ANYOF_BITMAP_CLEAR(data->start_class, value);   \
2964     }                                                               \
2965     else {                                                          \
2966             for (value = 0; value < 256; value++)                   \
2967                 if (!is_ ## nAmE ## _cp(value))                        \
2968                     ANYOF_BITMAP_SET(data->start_class, value);     \
2969     }                                                               \
2970     break
2971
2972
2973
2974 STATIC I32
2975 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
2976                         I32 *minlenp, I32 *deltap,
2977                         regnode *last,
2978                         scan_data_t *data,
2979                         I32 stopparen,
2980                         U8* recursed,
2981                         struct regnode_charclass_class *and_withp,
2982                         U32 flags, U32 depth)
2983                         /* scanp: Start here (read-write). */
2984                         /* deltap: Write maxlen-minlen here. */
2985                         /* last: Stop before this one. */
2986                         /* data: string data about the pattern */
2987                         /* stopparen: treat close N as END */
2988                         /* recursed: which subroutines have we recursed into */
2989                         /* and_withp: Valid if flags & SCF_DO_STCLASS_OR */
2990 {
2991     dVAR;
2992     I32 min = 0, pars = 0, code;
2993     regnode *scan = *scanp, *next;
2994     I32 delta = 0;
2995     int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
2996     int is_inf_internal = 0;            /* The studied chunk is infinite */
2997     I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
2998     scan_data_t data_fake;
2999     SV *re_trie_maxbuff = NULL;
3000     regnode *first_non_open = scan;
3001     I32 stopmin = I32_MAX;
3002     scan_frame *frame = NULL;
3003     GET_RE_DEBUG_FLAGS_DECL;
3004
3005     PERL_ARGS_ASSERT_STUDY_CHUNK;
3006
3007 #ifdef DEBUGGING
3008     StructCopy(&zero_scan_data, &data_fake, scan_data_t);
3009 #endif
3010
3011     if ( depth == 0 ) {
3012         while (first_non_open && OP(first_non_open) == OPEN)
3013             first_non_open=regnext(first_non_open);
3014     }
3015
3016
3017   fake_study_recurse:
3018     while ( scan && OP(scan) != END && scan < last ){
3019         UV min_subtract = 0;    /* How much to subtract from the minimum node
3020                                    length to get a real minimum (because the
3021                                    folded version may be shorter) */
3022         bool has_exactf_sharp_s = FALSE;
3023         /* Peephole optimizer: */
3024         DEBUG_STUDYDATA("Peep:", data,depth);
3025         DEBUG_PEEP("Peep",scan,depth);
3026
3027         /* Its not clear to khw or hv why this is done here, and not in the
3028          * clauses that deal with EXACT nodes.  khw's guess is that it's
3029          * because of a previous design */
3030         JOIN_EXACT(scan,&min_subtract, &has_exactf_sharp_s, 0);
3031
3032         /* Follow the next-chain of the current node and optimize
3033            away all the NOTHINGs from it.  */
3034         if (OP(scan) != CURLYX) {
3035             const int max = (reg_off_by_arg[OP(scan)]
3036                        ? I32_MAX
3037                        /* I32 may be smaller than U16 on CRAYs! */
3038                        : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
3039             int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
3040             int noff;
3041             regnode *n = scan;
3042
3043             /* Skip NOTHING and LONGJMP. */
3044             while ((n = regnext(n))
3045                    && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
3046                        || ((OP(n) == LONGJMP) && (noff = ARG(n))))
3047                    && off + noff < max)
3048                 off += noff;
3049             if (reg_off_by_arg[OP(scan)])
3050                 ARG(scan) = off;
3051             else
3052                 NEXT_OFF(scan) = off;
3053         }
3054
3055
3056
3057         /* The principal pseudo-switch.  Cannot be a switch, since we
3058            look into several different things.  */
3059         if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
3060                    || OP(scan) == IFTHEN) {
3061             next = regnext(scan);
3062             code = OP(scan);
3063             /* demq: the op(next)==code check is to see if we have "branch-branch" AFAICT */
3064
3065             if (OP(next) == code || code == IFTHEN) {
3066                 /* NOTE - There is similar code to this block below for handling
3067                    TRIE nodes on a re-study.  If you change stuff here check there
3068                    too. */
3069                 I32 max1 = 0, min1 = I32_MAX, num = 0;
3070                 struct regnode_charclass_class accum;
3071                 regnode * const startbranch=scan;
3072
3073                 if (flags & SCF_DO_SUBSTR)
3074                     SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot merge strings after this. */
3075                 if (flags & SCF_DO_STCLASS)
3076                     cl_init_zero(pRExC_state, &accum);
3077
3078                 while (OP(scan) == code) {
3079                     I32 deltanext, minnext, f = 0, fake;
3080                     struct regnode_charclass_class this_class;
3081
3082                     num++;
3083                     data_fake.flags = 0;
3084                     if (data) {
3085                         data_fake.whilem_c = data->whilem_c;
3086                         data_fake.last_closep = data->last_closep;
3087                     }
3088                     else
3089                         data_fake.last_closep = &fake;
3090
3091                     data_fake.pos_delta = delta;
3092                     next = regnext(scan);
3093                     scan = NEXTOPER(scan);
3094                     if (code != BRANCH)
3095                         scan = NEXTOPER(scan);
3096                     if (flags & SCF_DO_STCLASS) {
3097                         cl_init(pRExC_state, &this_class);
3098                         data_fake.start_class = &this_class;
3099                         f = SCF_DO_STCLASS_AND;
3100                     }
3101                     if (flags & SCF_WHILEM_VISITED_POS)
3102                         f |= SCF_WHILEM_VISITED_POS;
3103
3104                     /* we suppose the run is continuous, last=next...*/
3105                     minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext,
3106                                           next, &data_fake,
3107                                           stopparen, recursed, NULL, f,depth+1);
3108                     if (min1 > minnext)
3109                         min1 = minnext;
3110                     if (max1 < minnext + deltanext)
3111                         max1 = minnext + deltanext;
3112                     if (deltanext == I32_MAX)
3113                         is_inf = is_inf_internal = 1;
3114                     scan = next;
3115                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
3116                         pars++;
3117                     if (data_fake.flags & SCF_SEEN_ACCEPT) {
3118                         if ( stopmin > minnext) 
3119                             stopmin = min + min1;
3120                         flags &= ~SCF_DO_SUBSTR;
3121                         if (data)
3122                             data->flags |= SCF_SEEN_ACCEPT;
3123                     }
3124                     if (data) {
3125                         if (data_fake.flags & SF_HAS_EVAL)
3126                             data->flags |= SF_HAS_EVAL;
3127                         data->whilem_c = data_fake.whilem_c;
3128                     }
3129                     if (flags & SCF_DO_STCLASS)
3130                         cl_or(pRExC_state, &accum, &this_class);
3131                 }
3132                 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
3133                     min1 = 0;
3134                 if (flags & SCF_DO_SUBSTR) {
3135                     data->pos_min += min1;
3136                     data->pos_delta += max1 - min1;
3137                     if (max1 != min1 || is_inf)
3138                         data->longest = &(data->longest_float);
3139                 }
3140                 min += min1;
3141                 delta += max1 - min1;
3142                 if (flags & SCF_DO_STCLASS_OR) {
3143                     cl_or(pRExC_state, data->start_class, &accum);
3144                     if (min1) {
3145                         cl_and(data->start_class, and_withp);
3146                         flags &= ~SCF_DO_STCLASS;
3147                     }
3148                 }
3149                 else if (flags & SCF_DO_STCLASS_AND) {
3150                     if (min1) {
3151                         cl_and(data->start_class, &accum);
3152                         flags &= ~SCF_DO_STCLASS;
3153                     }
3154                     else {
3155                         /* Switch to OR mode: cache the old value of
3156                          * data->start_class */
3157                         INIT_AND_WITHP;
3158                         StructCopy(data->start_class, and_withp,
3159                                    struct regnode_charclass_class);
3160                         flags &= ~SCF_DO_STCLASS_AND;
3161                         StructCopy(&accum, data->start_class,
3162                                    struct regnode_charclass_class);
3163                         flags |= SCF_DO_STCLASS_OR;
3164                         data->start_class->flags |= ANYOF_EOS;
3165                     }
3166                 }
3167
3168                 if (PERL_ENABLE_TRIE_OPTIMISATION && OP( startbranch ) == BRANCH ) {
3169                 /* demq.
3170
3171                    Assuming this was/is a branch we are dealing with: 'scan' now
3172                    points at the item that follows the branch sequence, whatever
3173                    it is. We now start at the beginning of the sequence and look
3174                    for subsequences of
3175
3176                    BRANCH->EXACT=>x1
3177                    BRANCH->EXACT=>x2
3178                    tail
3179
3180                    which would be constructed from a pattern like /A|LIST|OF|WORDS/
3181
3182                    If we can find such a subsequence we need to turn the first
3183                    element into a trie and then add the subsequent branch exact
3184                    strings to the trie.
3185
3186                    We have two cases
3187
3188                      1. patterns where the whole set of branches can be converted. 
3189
3190                      2. patterns where only a subset can be converted.
3191
3192                    In case 1 we can replace the whole set with a single regop
3193                    for the trie. In case 2 we need to keep the start and end
3194                    branches so
3195
3196                      'BRANCH EXACT; BRANCH EXACT; BRANCH X'
3197                      becomes BRANCH TRIE; BRANCH X;
3198
3199                   There is an additional case, that being where there is a 
3200                   common prefix, which gets split out into an EXACT like node
3201                   preceding the TRIE node.
3202
3203                   If x(1..n)==tail then we can do a simple trie, if not we make
3204                   a "jump" trie, such that when we match the appropriate word
3205                   we "jump" to the appropriate tail node. Essentially we turn
3206                   a nested if into a case structure of sorts.
3207
3208                 */
3209
3210                     int made=0;
3211                     if (!re_trie_maxbuff) {
3212                         re_trie_maxbuff = get_sv(RE_TRIE_MAXBUF_NAME, 1);
3213                         if (!SvIOK(re_trie_maxbuff))
3214                             sv_setiv(re_trie_maxbuff, RE_TRIE_MAXBUF_INIT);
3215                     }
3216                     if ( SvIV(re_trie_maxbuff)>=0  ) {
3217                         regnode *cur;
3218                         regnode *first = (regnode *)NULL;
3219                         regnode *last = (regnode *)NULL;
3220                         regnode *tail = scan;
3221                         U8 trietype = 0;
3222                         U32 count=0;
3223
3224 #ifdef DEBUGGING
3225                         SV * const mysv = sv_newmortal();       /* for dumping */
3226 #endif
3227                         /* var tail is used because there may be a TAIL
3228                            regop in the way. Ie, the exacts will point to the
3229                            thing following the TAIL, but the last branch will
3230                            point at the TAIL. So we advance tail. If we
3231                            have nested (?:) we may have to move through several
3232                            tails.
3233                          */
3234
3235                         while ( OP( tail ) == TAIL ) {
3236                             /* this is the TAIL generated by (?:) */
3237                             tail = regnext( tail );
3238                         }
3239
3240                         
3241                         DEBUG_OPTIMISE_r({
3242                             regprop(RExC_rx, mysv, tail );
3243                             PerlIO_printf( Perl_debug_log, "%*s%s%s\n",
3244                                 (int)depth * 2 + 2, "", 
3245                                 "Looking for TRIE'able sequences. Tail node is: ", 
3246                                 SvPV_nolen_const( mysv )
3247                             );
3248                         });
3249                         
3250                         /*
3251
3252                             Step through the branches
3253                                 cur represents each branch,
3254                                 noper is the first thing to be matched as part of that branch
3255                                 noper_next is the regnext() of that node.
3256
3257                             We normally handle a case like this /FOO[xyz]|BAR[pqr]/
3258                             via a "jump trie" but we also support building with NOJUMPTRIE,
3259                             which restricts the trie logic to structures like /FOO|BAR/.
3260
3261                             If noper is a trieable nodetype then the branch is a possible optimization
3262                             target. If we are building under NOJUMPTRIE then we require that noper_next
3263                             is the same as scan (our current position in the regex program).
3264
3265                             Once we have two or more consecutive such branches we can create a
3266                             trie of the EXACT's contents and stitch it in place into the program.
3267
3268                             If the sequence represents all of the branches in the alternation we
3269                             replace the entire thing with a single TRIE node.
3270
3271                             Otherwise when it is a subsequence we need to stitch it in place and
3272                             replace only the relevant branches. This means the first branch has
3273                             to remain as it is used by the alternation logic, and its next pointer,
3274                             and needs to be repointed at the item on the branch chain following
3275                             the last branch we have optimized away.
3276
3277                             This could be either a BRANCH, in which case the subsequence is internal,
3278                             or it could be the item following the branch sequence in which case the
3279                             subsequence is at the end (which does not necessarily mean the first node
3280                             is the start of the alternation).
3281
3282                             TRIE_TYPE(X) is a define which maps the optype to a trietype.
3283
3284                                 optype          |  trietype
3285                                 ----------------+-----------
3286                                 NOTHING         | NOTHING
3287                                 EXACT           | EXACT
3288                                 EXACTFU         | EXACTFU
3289                                 EXACTFU_SS      | EXACTFU
3290                                 EXACTFU_TRICKYFOLD | EXACTFU
3291                                 EXACTFA         | 0
3292
3293
3294                         */
3295 #define TRIE_TYPE(X) ( ( NOTHING == (X) ) ? NOTHING :   \
3296                        ( EXACT == (X) )   ? EXACT :        \
3297                        ( EXACTFU == (X) || EXACTFU_SS == (X) || EXACTFU_TRICKYFOLD == (X) ) ? EXACTFU :        \
3298                        0 )
3299
3300                         /* dont use tail as the end marker for this traverse */
3301                         for ( cur = startbranch ; cur != scan ; cur = regnext( cur ) ) {
3302                             regnode * const noper = NEXTOPER( cur );
3303                             U8 noper_type = OP( noper );
3304                             U8 noper_trietype = TRIE_TYPE( noper_type );
3305 #if defined(DEBUGGING) || defined(NOJUMPTRIE)
3306                             regnode * const noper_next = regnext( noper );
3307 #endif
3308
3309                             DEBUG_OPTIMISE_r({
3310                                 regprop(RExC_rx, mysv, cur);
3311                                 PerlIO_printf( Perl_debug_log, "%*s- %s (%d)",
3312                                    (int)depth * 2 + 2,"", SvPV_nolen_const( mysv ), REG_NODE_NUM(cur) );
3313
3314                                 regprop(RExC_rx, mysv, noper);
3315                                 PerlIO_printf( Perl_debug_log, " -> %s",
3316                                     SvPV_nolen_const(mysv));
3317
3318                                 if ( noper_next ) {
3319                                   regprop(RExC_rx, mysv, noper_next );
3320                                   PerlIO_printf( Perl_debug_log,"\t=> %s\t",
3321                                     SvPV_nolen_const(mysv));
3322                                 }
3323                                 PerlIO_printf( Perl_debug_log, "(First==%d,Last==%d,Cur==%d)\n",
3324                                    REG_NODE_NUM(first), REG_NODE_NUM(last), REG_NODE_NUM(cur) );
3325                             });
3326
3327                             /* Is noper a trieable nodetype that can be merged with the
3328                              * current trie (if there is one)? */
3329                             if ( noper_trietype
3330                                   &&
3331                                   (
3332                                         /* XXX: Currently we cannot allow a NOTHING node to be the first element
3333                                          * of a TRIEABLE sequence, Otherwise we will overwrite the regop following
3334                                          * the NOTHING with the TRIE regop later on. This is because a NOTHING node
3335                                          * is only one regnode wide, and a TRIE is two regnodes. An example of a
3336                                          * problematic pattern is: "x" =~ /\A(?>(?:(?:)A|B|C?x))\z/
3337                                          * At a later point of time we can somewhat workaround this by handling
3338                                          * NOTHING -> EXACT sequences as generated by /(?:)A|(?:)B/ type patterns,
3339                                          * as we can effectively ignore the NOTHING regop in that case.
3340                                          * This clause, which allows NOTHING to start a sequence is left commented
3341                                          * out as a reference.
3342                                          * - Yves
3343
3344                                            ( noper_trietype == NOTHING)
3345                                            || ( trietype == NOTHING )
3346                                         */
3347                                         ( noper_trietype == NOTHING && trietype )
3348                                         || ( trietype == noper_trietype )
3349                                   )
3350 #ifdef NOJUMPTRIE
3351                                   && noper_next == tail
3352 #endif
3353                                   && count < U16_MAX)
3354                             {
3355                                 /* Handle mergable triable node
3356                                  * Either we are the first node in a new trieable sequence,
3357                                  * in which case we do some bookkeeping, otherwise we update
3358                                  * the end pointer. */
3359                                 count++;
3360                                 if ( !first ) {
3361                                     first = cur;
3362                                     trietype = noper_trietype;
3363                                 } else {
3364                                     if ( trietype == NOTHING )
3365                                         trietype = noper_trietype;
3366                                     last = cur;
3367                                 }
3368                             } /* end handle mergable triable node */
3369                             else {
3370                                 /* handle unmergable node -
3371                                  * noper may either be a triable node which can not be tried
3372                                  * together with the current trie, or a non triable node */
3373                                 if ( last ) {
3374                                     /* If last is set and trietype is not NOTHING then we have found
3375                                      * at least two triable branch sequences in a row of a similar
3376                                      * trietype so we can turn them into a trie. If/when we
3377                                      * allow NOTHING to start a trie sequence this condition will be
3378                                      * required, and it isn't expensive so we leave it in for now. */
3379                                     if ( trietype != NOTHING )
3380                                         make_trie( pRExC_state,
3381                                                 startbranch, first, cur, tail, count,
3382                                                 trietype, depth+1 );
3383                                     last = NULL; /* note: we clear/update first, trietype etc below, so we dont do it here */
3384                                 }
3385                                 if ( noper_trietype
3386 #ifdef NOJUMPTRIE
3387                                      && noper_next == tail
3388 #endif
3389                                 ){
3390                                     /* noper is triable, so we can start a new trie sequence */
3391                                     count = 1;
3392                                     first = cur;
3393                                     trietype = noper_trietype;
3394                                 } else if (first) {
3395                                     /* if we already saw a first but the current node is not triable then we have
3396                                      * to reset the first information. */
3397                                     count = 0;
3398                                     first = NULL;
3399                                     trietype = 0;
3400                                 }
3401                             } /* end handle unmergable node */
3402                         } /* loop over branches */
3403                         DEBUG_OPTIMISE_r({
3404                             regprop(RExC_rx, mysv, cur);
3405                             PerlIO_printf( Perl_debug_log,
3406                               "%*s- %s (%d) <SCAN FINISHED>\n", (int)depth * 2 + 2,
3407                               "", SvPV_nolen_const( mysv ),REG_NODE_NUM(cur));
3408
3409                         });
3410                         if ( last && trietype != NOTHING ) {
3411                             /* the last branch of the sequence was part of a trie,
3412                              * so we have to construct it here outside of the loop
3413                              */
3414                             made= make_trie( pRExC_state, startbranch, first, scan, tail, count, trietype, depth+1 );
3415 #ifdef TRIE_STUDY_OPT
3416                             if ( ((made == MADE_EXACT_TRIE && 
3417                                  startbranch == first) 
3418                                  || ( first_non_open == first )) && 
3419                                  depth==0 ) {
3420                                 flags |= SCF_TRIE_RESTUDY;
3421                                 if ( startbranch == first 
3422                                      && scan == tail ) 
3423                                 {
3424                                     RExC_seen &=~REG_TOP_LEVEL_BRANCHES;
3425                                 }
3426                             }
3427 #endif
3428                         } /* end if ( last) */
3429                     } /* TRIE_MAXBUF is non zero */
3430                     
3431                 } /* do trie */
3432                 
3433             }
3434             else if ( code == BRANCHJ ) {  /* single branch is optimized. */
3435                 scan = NEXTOPER(NEXTOPER(scan));
3436             } else                      /* single branch is optimized. */
3437                 scan = NEXTOPER(scan);
3438             continue;
3439         } else if (OP(scan) == SUSPEND || OP(scan) == GOSUB || OP(scan) == GOSTART) {
3440             scan_frame *newframe = NULL;
3441             I32 paren;
3442             regnode *start;
3443             regnode *end;
3444
3445             if (OP(scan) != SUSPEND) {
3446             /* set the pointer */
3447                 if (OP(scan) == GOSUB) {
3448                     paren = ARG(scan);
3449                     RExC_recurse[ARG2L(scan)] = scan;
3450                     start = RExC_open_parens[paren-1];
3451                     end   = RExC_close_parens[paren-1];
3452                 } else {
3453                     paren = 0;
3454                     start = RExC_rxi->program + 1;
3455                     end   = RExC_opend;
3456                 }
3457                 if (!recursed) {
3458                     Newxz(recursed, (((RExC_npar)>>3) +1), U8);
3459                     SAVEFREEPV(recursed);
3460                 }
3461                 if (!PAREN_TEST(recursed,paren+1)) {
3462                     PAREN_SET(recursed,paren+1);
3463                     Newx(newframe,1,scan_frame);
3464                 } else {
3465                     if (flags & SCF_DO_SUBSTR) {
3466                         SCAN_COMMIT(pRExC_state,data,minlenp);
3467                         data->longest = &(data->longest_float);
3468                     }
3469                     is_inf = is_inf_internal = 1;
3470                     if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
3471                         cl_anything(pRExC_state, data->start_class);
3472                     flags &= ~SCF_DO_STCLASS;
3473                 }
3474             } else {
3475                 Newx(newframe,1,scan_frame);
3476                 paren = stopparen;
3477                 start = scan+2;
3478                 end = regnext(scan);
3479             }
3480             if (newframe) {
3481                 assert(start);
3482                 assert(end);
3483                 SAVEFREEPV(newframe);
3484                 newframe->next = regnext(scan);
3485                 newframe->last = last;
3486                 newframe->stop = stopparen;
3487                 newframe->prev = frame;
3488
3489                 frame = newframe;
3490                 scan =  start;
3491                 stopparen = paren;
3492                 last = end;
3493
3494                 continue;
3495             }
3496         }
3497         else if (OP(scan) == EXACT) {
3498             I32 l = STR_LEN(scan);
3499             UV uc;
3500             if (UTF) {
3501                 const U8 * const s = (U8*)STRING(scan);
3502                 uc = utf8_to_uvchr_buf(s, s + l, NULL);
3503                 l = utf8_length(s, s + l);
3504             } else {
3505                 uc = *((U8*)STRING(scan));
3506             }
3507             min += l;
3508             if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
3509                 /* The code below prefers earlier match for fixed
3510                    offset, later match for variable offset.  */
3511                 if (data->last_end == -1) { /* Update the start info. */
3512                     data->last_start_min = data->pos_min;
3513                     data->last_start_max = is_inf
3514                         ? I32_MAX : data->pos_min + data->pos_delta;
3515                 }
3516                 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
3517                 if (UTF)
3518                     SvUTF8_on(data->last_found);
3519                 {
3520                     SV * const sv = data->last_found;
3521                     MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
3522                         mg_find(sv, PERL_MAGIC_utf8) : NULL;
3523                     if (mg && mg->mg_len >= 0)
3524                         mg->mg_len += utf8_length((U8*)STRING(scan),
3525                                                   (U8*)STRING(scan)+STR_LEN(scan));
3526                 }
3527                 data->last_end = data->pos_min + l;
3528                 data->pos_min += l; /* As in the first entry. */
3529                 data->flags &= ~SF_BEFORE_EOL;
3530             }
3531             if (flags & SCF_DO_STCLASS_AND) {
3532                 /* Check whether it is compatible with what we know already! */
3533                 int compat = 1;
3534
3535
3536                 /* If compatible, we or it in below.  It is compatible if is
3537                  * in the bitmp and either 1) its bit or its fold is set, or 2)
3538                  * it's for a locale.  Even if there isn't unicode semantics
3539                  * here, at runtime there may be because of matching against a
3540                  * utf8 string, so accept a possible false positive for
3541                  * latin1-range folds */
3542                 if (uc >= 0x100 ||
3543                     (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3544                     && !ANYOF_BITMAP_TEST(data->start_class, uc)
3545                     && (!(data->start_class->flags & ANYOF_LOC_NONBITMAP_FOLD)
3546                         || !ANYOF_BITMAP_TEST(data->start_class, PL_fold_latin1[uc])))
3547                     )
3548                 {
3549                     compat = 0;
3550                 }
3551                 ANYOF_CLASS_ZERO(data->start_class);
3552                 ANYOF_BITMAP_ZERO(data->start_class);
3553                 if (compat)
3554                     ANYOF_BITMAP_SET(data->start_class, uc);
3555                 else if (uc >= 0x100) {
3556                     int i;
3557
3558                     /* Some Unicode code points fold to the Latin1 range; as
3559                      * XXX temporary code, instead of figuring out if this is
3560                      * one, just assume it is and set all the start class bits
3561                      * that could be some such above 255 code point's fold
3562                      * which will generate fals positives.  As the code
3563                      * elsewhere that does compute the fold settles down, it
3564                      * can be extracted out and re-used here */
3565                     for (i = 0; i < 256; i++){
3566                         if (HAS_NONLATIN1_FOLD_CLOSURE(i)) {
3567                             ANYOF_BITMAP_SET(data->start_class, i);
3568                         }
3569                     }
3570                 }
3571                 data->start_class->flags &= ~ANYOF_EOS;
3572                 if (uc < 0x100)
3573                   data->start_class->flags &= ~ANYOF_UNICODE_ALL;
3574             }
3575             else if (flags & SCF_DO_STCLASS_OR) {
3576                 /* false positive possible if the class is case-folded */
3577                 if (uc < 0x100)
3578                     ANYOF_BITMAP_SET(data->start_class, uc);
3579                 else
3580                     data->start_class->flags |= ANYOF_UNICODE_ALL;
3581                 data->start_class->flags &= ~ANYOF_EOS;
3582                 cl_and(data->start_class, and_withp);
3583             }
3584             flags &= ~SCF_DO_STCLASS;
3585         }
3586         else if (PL_regkind[OP(scan)] == EXACT) { /* But OP != EXACT! */
3587             I32 l = STR_LEN(scan);
3588             UV uc = *((U8*)STRING(scan));
3589
3590             /* Search for fixed substrings supports EXACT only. */
3591             if (flags & SCF_DO_SUBSTR) {
3592                 assert(data);
3593                 SCAN_COMMIT(pRExC_state, data, minlenp);
3594             }
3595             if (UTF) {
3596                 const U8 * const s = (U8 *)STRING(scan);
3597                 uc = utf8_to_uvchr_buf(s, s + l, NULL);
3598                 l = utf8_length(s, s + l);
3599             }
3600             else if (has_exactf_sharp_s) {
3601                 RExC_seen |= REG_SEEN_EXACTF_SHARP_S;
3602             }
3603             min += l - min_subtract;
3604             if (min < 0) {
3605                 min = 0;
3606             }
3607             delta += min_subtract;
3608             if (flags & SCF_DO_SUBSTR) {
3609                 data->pos_min += l - min_subtract;
3610                 if (data->pos_min < 0) {
3611                     data->pos_min = 0;
3612                 }
3613                 data->pos_delta += min_subtract;
3614                 if (min_subtract) {
3615                     data->longest = &(data->longest_float);
3616                 }
3617             }
3618             if (flags & SCF_DO_STCLASS_AND) {
3619                 /* Check whether it is compatible with what we know already! */
3620                 int compat = 1;
3621                 if (uc >= 0x100 ||
3622                  (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
3623                   && !ANYOF_BITMAP_TEST(data->start_class, uc)
3624                   && !ANYOF_BITMAP_TEST(data->start_class, PL_fold_latin1[uc])))
3625                 {
3626                     compat = 0;
3627                 }
3628                 ANYOF_CLASS_ZERO(data->start_class);
3629                 ANYOF_BITMAP_ZERO(data->start_class);
3630                 if (compat) {
3631                     ANYOF_BITMAP_SET(data->start_class, uc);
3632                     data->start_class->flags &= ~ANYOF_EOS;
3633                     data->start_class->flags |= ANYOF_LOC_NONBITMAP_FOLD;
3634                     if (OP(scan) == EXACTFL) {
3635                         /* XXX This set is probably no longer necessary, and
3636                          * probably wrong as LOCALE now is on in the initial
3637                          * state */
3638                         data->start_class->flags |= ANYOF_LOCALE;
3639                     }
3640                     else {
3641
3642                         /* Also set the other member of the fold pair.  In case
3643                          * that unicode semantics is called for at runtime, use
3644                          * the full latin1 fold.  (Can't do this for locale,
3645                          * because not known until runtime) */
3646                         ANYOF_BITMAP_SET(data->start_class, PL_fold_latin1[uc]);
3647
3648                         /* All other (EXACTFL handled above) folds except under
3649                          * /iaa that include s, S, and sharp_s also may include
3650                          * the others */
3651                         if (OP(scan) != EXACTFA) {
3652                             if (uc == 's' || uc == 'S') {
3653                                 ANYOF_BITMAP_SET(data->start_class,
3654                                                  LATIN_SMALL_LETTER_SHARP_S);
3655                             }
3656                             else if (uc == LATIN_SMALL_LETTER_SHARP_S) {
3657                                 ANYOF_BITMAP_SET(data->start_class, 's');
3658                                 ANYOF_BITMAP_SET(data->start_class, 'S');
3659                             }
3660                         }
3661                     }
3662                 }
3663                 else if (uc >= 0x100) {
3664                     int i;
3665                     for (i = 0; i < 256; i++){
3666                         if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)) {
3667                             ANYOF_BITMAP_SET(data->start_class, i);
3668                         }
3669                     }
3670                 }
3671             }
3672             else if (flags & SCF_DO_STCLASS_OR) {
3673                 if (data->start_class->flags & ANYOF_LOC_NONBITMAP_FOLD) {
3674                     /* false positive possible if the class is case-folded.
3675                        Assume that the locale settings are the same... */
3676                     if (uc < 0x100) {
3677                         ANYOF_BITMAP_SET(data->start_class, uc);
3678                         if (OP(scan) != EXACTFL) {
3679
3680                             /* And set the other member of the fold pair, but
3681                              * can't do that in locale because not known until
3682                              * run-time */
3683                             ANYOF_BITMAP_SET(data->start_class,
3684                                              PL_fold_latin1[uc]);
3685
3686                             /* All folds except under /iaa that include s, S,
3687                              * and sharp_s also may include the others */
3688                             if (OP(scan) != EXACTFA) {
3689                                 if (uc == 's' || uc == 'S') {
3690                                     ANYOF_BITMAP_SET(data->start_class,
3691                                                    LATIN_SMALL_LETTER_SHARP_S);
3692                                 }
3693                                 else if (uc == LATIN_SMALL_LETTER_SHARP_S) {
3694                                     ANYOF_BITMAP_SET(data->start_class, 's');
3695                                     ANYOF_BITMAP_SET(data->start_class, 'S');
3696                                 }
3697                             }
3698                         }
3699                     }
3700                     data->start_class->flags &= ~ANYOF_EOS;
3701                 }
3702                 cl_and(data->start_class, and_withp);
3703             }
3704             flags &= ~SCF_DO_STCLASS;
3705         }
3706         else if (REGNODE_VARIES(OP(scan))) {
3707             I32 mincount, maxcount, minnext, deltanext, fl = 0;
3708             I32 f = flags, pos_before = 0;
3709             regnode * const oscan = scan;
3710             struct regnode_charclass_class this_class;
3711             struct regnode_charclass_class *oclass = NULL;
3712             I32 next_is_eval = 0;
3713
3714             switch (PL_regkind[OP(scan)]) {
3715             case WHILEM:                /* End of (?:...)* . */
3716                 scan = NEXTOPER(scan);
3717                 goto finish;
3718             case PLUS:
3719                 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
3720                     next = NEXTOPER(scan);
3721                     if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
3722                         mincount = 1;
3723                         maxcount = REG_INFTY;
3724                         next = regnext(scan);
3725                         scan = NEXTOPER(scan);
3726                         goto do_curly;
3727                     }
3728                 }
3729                 if (flags & SCF_DO_SUBSTR)
3730                     data->pos_min++;
3731                 min++;
3732                 /* Fall through. */
3733             case STAR:
3734                 if (flags & SCF_DO_STCLASS) {
3735                     mincount = 0;
3736                     maxcount = REG_INFTY;
3737                     next = regnext(scan);
3738                     scan = NEXTOPER(scan);
3739                     goto do_curly;
3740                 }
3741                 is_inf = is_inf_internal = 1;
3742                 scan = regnext(scan);
3743                 if (flags & SCF_DO_SUBSTR) {
3744                     SCAN_COMMIT(pRExC_state, data, minlenp); /* Cannot extend fixed substrings */
3745                     data->longest = &(data->longest_float);
3746                 }
3747                 goto optimize_curly_tail;
3748             case CURLY:
3749                 if (stopparen>0 && (OP(scan)==CURLYN || OP(scan)==CURLYM)
3750                     && (scan->flags == stopparen))
3751                 {
3752                     mincount = 1;
3753                     maxcount = 1;
3754                 } else {
3755                     mincount = ARG1(scan);
3756                     maxcount = ARG2(scan);
3757                 }
3758                 next = regnext(scan);
3759                 if (OP(scan) == CURLYX) {
3760                     I32 lp = (data ? *(data->last_closep) : 0);
3761                     scan->flags = ((lp <= (I32)U8_MAX) ? (U8)lp : U8_MAX);
3762                 }
3763                 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3764                 next_is_eval = (OP(scan) == EVAL);
3765               do_curly:
3766                 if (flags & SCF_DO_SUBSTR) {
3767                     if (mincount == 0) SCAN_COMMIT(pRExC_state,data,minlenp); /* Cannot extend fixed substrings */
3768                     pos_before = data->pos_min;
3769                 }
3770                 if (data) {
3771                     fl = data->flags;
3772                     data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
3773                     if (is_inf)
3774                         data->flags |= SF_IS_INF;
3775                 }
3776                 if (flags & SCF_DO_STCLASS) {
3777                     cl_init(pRExC_state, &this_class);
3778                     oclass = data->start_class;
3779                     data->start_class = &this_class;
3780                     f |= SCF_DO_STCLASS_AND;
3781                     f &= ~SCF_DO_STCLASS_OR;
3782                 }
3783                 /* Exclude from super-linear cache processing any {n,m}
3784                    regops for which the combination of input pos and regex
3785                    pos is not enough information to determine if a match
3786                    will be possible.
3787
3788                    For example, in the regex /foo(bar\s*){4,8}baz/ with the
3789                    regex pos at the \s*, the prospects for a match depend not
3790                    only on the input position but also on how many (bar\s*)
3791                    repeats into the {4,8} we are. */
3792                if ((mincount > 1) || (maxcount > 1 && maxcount != REG_INFTY))
3793                     f &= ~SCF_WHILEM_VISITED_POS;
3794
3795                 /* This will finish on WHILEM, setting scan, or on NULL: */
3796                 minnext = study_chunk(pRExC_state, &scan, minlenp, &deltanext, 
3797                                       last, data, stopparen, recursed, NULL,
3798                                       (mincount == 0
3799                                         ? (f & ~SCF_DO_SUBSTR) : f),depth+1);
3800
3801                 if (flags & SCF_DO_STCLASS)
3802                     data->start_class = oclass;
3803                 if (mincount == 0 || minnext == 0) {
3804                     if (flags & SCF_DO_STCLASS_OR) {
3805                         cl_or(pRExC_state, data->start_class, &this_class);
3806                     }
3807                     else if (flags & SCF_DO_STCLASS_AND) {
3808                         /* Switch to OR mode: cache the old value of
3809                          * data->start_class */
3810                         INIT_AND_WITHP;
3811                         StructCopy(data->start_class, and_withp,
3812                                    struct regnode_charclass_class);
3813                         flags &= ~SCF_DO_STCLASS_AND;
3814                         StructCopy(&this_class, data->start_class,
3815                                    struct regnode_charclass_class);
3816                         flags |= SCF_DO_STCLASS_OR;
3817                         data->start_class->flags |= ANYOF_EOS;
3818                     }
3819                 } else {                /* Non-zero len */
3820                     if (flags & SCF_DO_STCLASS_OR) {
3821                         cl_or(pRExC_state, data->start_class, &this_class);
3822                         cl_and(data->start_class, and_withp);
3823                     }
3824                     else if (flags & SCF_DO_STCLASS_AND)
3825                         cl_and(data->start_class, &this_class);
3826                     flags &= ~SCF_DO_STCLASS;
3827                 }
3828                 if (!scan)              /* It was not CURLYX, but CURLY. */
3829                     scan = next;
3830                 if ( /* ? quantifier ok, except for (?{ ... }) */
3831                     (next_is_eval || !(mincount == 0 && maxcount == 1))
3832                     && (minnext == 0) && (deltanext == 0)
3833                     && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
3834                     && maxcount <= REG_INFTY/3) /* Complement check for big count */
3835                 {
3836                     ckWARNreg(RExC_parse,
3837                               "Quantifier unexpected on zero-length expression");
3838                 }
3839
3840                 min += minnext * mincount;
3841                 is_inf_internal |= ((maxcount == REG_INFTY
3842                                      && (minnext + deltanext) > 0)
3843                                     || deltanext == I32_MAX);
3844                 is_inf |= is_inf_internal;
3845                 delta += (minnext + deltanext) * maxcount - minnext * mincount;
3846
3847                 /* Try powerful optimization CURLYX => CURLYN. */
3848                 if (  OP(oscan) == CURLYX && data
3849                       && data->flags & SF_IN_PAR
3850                       && !(data->flags & SF_HAS_EVAL)
3851                       && !deltanext && minnext == 1 ) {
3852                     /* Try to optimize to CURLYN.  */
3853                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
3854                     regnode * const nxt1 = nxt;
3855 #ifdef DEBUGGING
3856                     regnode *nxt2;
3857 #endif
3858
3859                     /* Skip open. */
3860                     nxt = regnext(nxt);
3861                     if (!REGNODE_SIMPLE(OP(nxt))
3862                         && !(PL_regkind[OP(nxt)] == EXACT
3863                              && STR_LEN(nxt) == 1))
3864                         goto nogo;
3865 #ifdef DEBUGGING
3866                     nxt2 = nxt;
3867 #endif
3868                     nxt = regnext(nxt);
3869                     if (OP(nxt) != CLOSE)
3870                         goto nogo;
3871                     if (RExC_open_parens) {
3872                         RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3873                         RExC_close_parens[ARG(nxt1)-1]=nxt+2; /*close->while*/
3874                     }
3875                     /* Now we know that nxt2 is the only contents: */
3876                     oscan->flags = (U8)ARG(nxt);
3877                     OP(oscan) = CURLYN;
3878                     OP(nxt1) = NOTHING; /* was OPEN. */
3879
3880 #ifdef DEBUGGING
3881                     OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3882                     NEXT_OFF(nxt1+ 1) = 0; /* just for consistency. */
3883                     NEXT_OFF(nxt2) = 0; /* just for consistency with CURLY. */
3884                     OP(nxt) = OPTIMIZED;        /* was CLOSE. */
3885                     OP(nxt + 1) = OPTIMIZED; /* was count. */
3886                     NEXT_OFF(nxt+ 1) = 0; /* just for consistency. */
3887 #endif
3888                 }
3889               nogo:
3890
3891                 /* Try optimization CURLYX => CURLYM. */
3892                 if (  OP(oscan) == CURLYX && data
3893                       && !(data->flags & SF_HAS_PAR)
3894                       && !(data->flags & SF_HAS_EVAL)
3895                       && !deltanext     /* atom is fixed width */
3896                       && minnext != 0   /* CURLYM can't handle zero width */
3897                 ) {
3898                     /* XXXX How to optimize if data == 0? */
3899                     /* Optimize to a simpler form.  */
3900                     regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
3901                     regnode *nxt2;
3902
3903                     OP(oscan) = CURLYM;
3904                     while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
3905                             && (OP(nxt2) != WHILEM))
3906                         nxt = nxt2;
3907                     OP(nxt2)  = SUCCEED; /* Whas WHILEM */
3908                     /* Need to optimize away parenths. */
3909                     if ((data->flags & SF_IN_PAR) && OP(nxt) == CLOSE) {
3910                         /* Set the parenth number.  */
3911                         regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
3912
3913                         oscan->flags = (U8)ARG(nxt);
3914                         if (RExC_open_parens) {
3915                             RExC_open_parens[ARG(nxt1)-1]=oscan; /*open->CURLYM*/
3916                             RExC_close_parens[ARG(nxt1)-1]=nxt2+1; /*close->NOTHING*/
3917                         }
3918                         OP(nxt1) = OPTIMIZED;   /* was OPEN. */
3919                         OP(nxt) = OPTIMIZED;    /* was CLOSE. */
3920
3921 #ifdef DEBUGGING
3922                         OP(nxt1 + 1) = OPTIMIZED; /* was count. */
3923                         OP(nxt + 1) = OPTIMIZED; /* was count. */
3924                         NEXT_OFF(nxt1 + 1) = 0; /* just for consistency. */
3925                         NEXT_OFF(nxt + 1) = 0; /* just for consistency. */
3926 #endif
3927 #if 0
3928                         while ( nxt1 && (OP(nxt1) != WHILEM)) {
3929                             regnode *nnxt = regnext(nxt1);
3930                             if (nnxt == nxt) {
3931                                 if (reg_off_by_arg[OP(nxt1)])
3932                                     ARG_SET(nxt1, nxt2 - nxt1);
3933                                 else if (nxt2 - nxt1 < U16_MAX)
3934                                     NEXT_OFF(nxt1) = nxt2 - nxt1;
3935                                 else
3936                                     OP(nxt) = NOTHING;  /* Cannot beautify */
3937                             }
3938                             nxt1 = nnxt;
3939                         }
3940 #endif
3941                         /* Optimize again: */
3942                         study_chunk(pRExC_state, &nxt1, minlenp, &deltanext, nxt,
3943                                     NULL, stopparen, recursed, NULL, 0,depth+1);
3944                     }
3945                     else
3946                         oscan->flags = 0;
3947                 }
3948                 else if ((OP(oscan) == CURLYX)
3949                          && (flags & SCF_WHILEM_VISITED_POS)
3950                          /* See the comment on a similar expression above.
3951                             However, this time it's not a subexpression
3952                             we care about, but the expression itself. */
3953                          && (maxcount == REG_INFTY)
3954                          && data && ++data->whilem_c < 16) {
3955                     /* This stays as CURLYX, we can put the count/of pair. */
3956                     /* Find WHILEM (as in regexec.c) */
3957                     regnode *nxt = oscan + NEXT_OFF(oscan);
3958
3959                     if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
3960                         nxt += ARG(nxt);
3961                     PREVOPER(nxt)->flags = (U8)(data->whilem_c
3962                         | (RExC_whilem_seen << 4)); /* On WHILEM */
3963                 }
3964                 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
3965                     pars++;
3966                 if (flags & SCF_DO_SUBSTR) {
3967                     SV *last_str = NULL;
3968                     int counted = mincount != 0;
3969
3970                     if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
3971 #if defined(SPARC64_GCC_WORKAROUND)
3972                         I32 b = 0;
3973                         STRLEN l = 0;
3974                         const char *s = NULL;
3975                         I32 old = 0;
3976
3977                         if (pos_before >= data->last_start_min)
3978                             b = pos_before;
3979                         else
3980                             b = data->last_start_min;
3981
3982                         l = 0;
3983                         s = SvPV_const(data->last_found, l);
3984                         old = b - data->last_start_min;
3985
3986 #else
3987                         I32 b = pos_before >= data->last_start_min
3988                             ? pos_before : data->last_start_min;
3989                         STRLEN l;
3990                         const char * const s = SvPV_const(data->last_found, l);
3991                         I32 old = b - data->last_start_min;
3992 #endif
3993
3994                         if (UTF)
3995                             old = utf8_hop((U8*)s, old) - (U8*)s;
3996                         l -= old;
3997                         /* Get the added string: */
3998                         last_str = newSVpvn_utf8(s  + old, l, UTF);
3999                         if (deltanext == 0 && pos_before == b) {
4000                             /* What was added is a constant string */
4001                             if (mincount > 1) {
4002                                 SvGROW(last_str, (mincount * l) + 1);
4003                                 repeatcpy(SvPVX(last_str) + l,
4004                                           SvPVX_const(last_str), l, mincount - 1);
4005                                 SvCUR_set(last_str, SvCUR(last_str) * mincount);
4006                                 /* Add additional parts. */
4007                                 SvCUR_set(data->last_found,
4008                                           SvCUR(data->last_found) - l);
4009                                 sv_catsv(data->last_found, last_str);
4010                                 {
4011                                     SV * sv = data->last_found;
4012                                     MAGIC *mg =
4013                                         SvUTF8(sv) && SvMAGICAL(sv) ?
4014                                         mg_find(sv, PERL_MAGIC_utf8) : NULL;
4015                                     if (mg && mg->mg_len >= 0)
4016                                         mg->mg_len += CHR_SVLEN(last_str) - l;
4017                                 }
4018                                 data->last_end += l * (mincount - 1);
4019                             }
4020                         } else {
4021                             /* start offset must point into the last copy */
4022                             data->last_start_min += minnext * (mincount - 1);
4023                             data->last_start_max += is_inf ? I32_MAX
4024                                 : (maxcount - 1) * (minnext + data->pos_delta);
4025                         }
4026                     }
4027                     /* It is counted once already... */
4028                     data->pos_min += minnext * (mincount - counted);
4029                     data->pos_delta += - counted * deltanext +
4030                         (minnext + deltanext) * maxcount - minnext * mincount;
4031                     if (mincount != maxcount) {
4032                          /* Cannot extend fixed substrings found inside
4033                             the group.  */
4034                         SCAN_COMMIT(pRExC_state,data,minlenp);
4035                         if (mincount && last_str) {
4036                             SV * const sv = data->last_found;
4037                             MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ?
4038                                 mg_find(sv, PERL_MAGIC_utf8) : NULL;
4039
4040                             if (mg)
4041                                 mg->mg_len = -1;
4042                             sv_setsv(sv, last_str);
4043                             data->last_end = data->pos_min;
4044                             data->last_start_min =
4045                                 data->pos_min - CHR_SVLEN(last_str);
4046                             data->last_start_max = is_inf
4047                                 ? I32_MAX
4048                                 : data->pos_min + data->pos_delta
4049                                 - CHR_SVLEN(last_str);
4050                         }
4051                         data->longest = &(data->longest_float);
4052                     }
4053                     SvREFCNT_dec(last_str);
4054                 }
4055                 if (data && (fl & SF_HAS_EVAL))
4056                     data->flags |= SF_HAS_EVAL;
4057               optimize_curly_tail:
4058                 if (OP(oscan) != CURLYX) {
4059                     while (PL_regkind[OP(next = regnext(oscan))] == NOTHING
4060                            && NEXT_OFF(next))
4061                         NEXT_OFF(oscan) += NEXT_OFF(next);
4062                 }
4063                 continue;
4064             default:                    /* REF, ANYOFV, and CLUMP only? */
4065                 if (flags & SCF_DO_SUBSTR) {
4066                     SCAN_COMMIT(pRExC_state,data,minlenp);      /* Cannot expect anything... */
4067                     data->longest = &(data->longest_float);
4068                 }
4069                 is_inf = is_inf_internal = 1;
4070                 if (flags & SCF_DO_STCLASS_OR)
4071                     cl_anything(pRExC_state, data->start_class);
4072                 flags &= ~SCF_DO_STCLASS;
4073                 break;
4074             }
4075         }
4076         else if (OP(scan) == LNBREAK) {
4077             if (flags & SCF_DO_STCLASS) {
4078                 int value = 0;
4079                 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
4080                 if (flags & SCF_DO_STCLASS_AND) {
4081                     for (value = 0; value < 256; value++)
4082                         if (!is_VERTWS_cp(value))
4083                             ANYOF_BITMAP_CLEAR(data->start_class, value);
4084                 }
4085                 else {
4086                     for (value = 0; value < 256; value++)
4087                         if (is_VERTWS_cp(value))
4088                             ANYOF_BITMAP_SET(data->start_class, value);
4089                 }
4090                 if (flags & SCF_DO_STCLASS_OR)
4091                     cl_and(data->start_class, and_withp);
4092                 flags &= ~SCF_DO_STCLASS;
4093             }
4094             min += 1;
4095             delta += 1;
4096             if (flags & SCF_DO_SUBSTR) {
4097                 SCAN_COMMIT(pRExC_state,data,minlenp);  /* Cannot expect anything... */
4098                 data->pos_min += 1;
4099                 data->pos_delta += 1;
4100                 data->longest = &(data->longest_float);
4101             }
4102         }
4103         else if (REGNODE_SIMPLE(OP(scan))) {
4104             int value = 0;
4105
4106             if (flags & SCF_DO_SUBSTR) {
4107                 SCAN_COMMIT(pRExC_state,data,minlenp);
4108                 data->pos_min++;
4109             }
4110             min++;
4111             if (flags & SCF_DO_STCLASS) {
4112                 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
4113
4114                 /* Some of the logic below assumes that switching
4115                    locale on will only add false positives. */
4116                 switch (PL_regkind[OP(scan)]) {
4117                 case SANY:
4118                 default:
4119                   do_default:
4120                     /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
4121                     if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
4122                         cl_anything(pRExC_state, data->start_class);
4123                     break;
4124                 case REG_ANY:
4125                     if (OP(scan) == SANY)
4126                         goto do_default;
4127                     if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
4128                         value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
4129                                  || ANYOF_CLASS_TEST_ANY_SET(data->start_class));
4130                         cl_anything(pRExC_state, data->start_class);
4131                     }
4132                     if (flags & SCF_DO_STCLASS_AND || !value)
4133                         ANYOF_BITMAP_CLEAR(data->start_class,'\n');
4134                     break;
4135                 case ANYOF:
4136                     if (flags & SCF_DO_STCLASS_AND)
4137                         cl_and(data->start_class,
4138                                (struct regnode_charclass_class*)scan);
4139                     else
4140                         cl_or(pRExC_state, data->start_class,
4141                               (struct regnode_charclass_class*)scan);
4142                     break;
4143                 case ALNUM:
4144                     if (flags & SCF_DO_STCLASS_AND) {
4145                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
4146                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
4147                             if (OP(scan) == ALNUMU) {
4148                                 for (value = 0; value < 256; value++) {
4149                                     if (!isWORDCHAR_L1(value)) {
4150                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4151                                     }
4152                                 }
4153                             } else {
4154                                 for (value = 0; value < 256; value++) {
4155                                     if (!isALNUM(value)) {
4156                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4157                                     }
4158                                 }
4159                             }
4160                         }
4161                     }
4162                     else {
4163                         if (data->start_class->flags & ANYOF_LOCALE)
4164                             ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
4165
4166                         /* Even if under locale, set the bits for non-locale
4167                          * in case it isn't a true locale-node.  This will
4168                          * create false positives if it truly is locale */
4169                         if (OP(scan) == ALNUMU) {
4170                             for (value = 0; value < 256; value++) {
4171                                 if (isWORDCHAR_L1(value)) {
4172                                     ANYOF_BITMAP_SET(data->start_class, value);
4173                                 }
4174                             }
4175                         } else {
4176                             for (value = 0; value < 256; value++) {
4177                                 if (isALNUM(value)) {
4178                                     ANYOF_BITMAP_SET(data->start_class, value);
4179                                 }
4180                             }
4181                         }
4182                     }
4183                     break;
4184                 case NALNUM:
4185                     if (flags & SCF_DO_STCLASS_AND) {
4186                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
4187                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
4188                             if (OP(scan) == NALNUMU) {
4189                                 for (value = 0; value < 256; value++) {
4190                                     if (isWORDCHAR_L1(value)) {
4191                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4192                                     }
4193                                 }
4194                             } else {
4195                                 for (value = 0; value < 256; value++) {
4196                                     if (isALNUM(value)) {
4197                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4198                                     }
4199                                 }
4200                             }
4201                         }
4202                     }
4203                     else {
4204                         if (data->start_class->flags & ANYOF_LOCALE)
4205                             ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
4206
4207                         /* Even if under locale, set the bits for non-locale in
4208                          * case it isn't a true locale-node.  This will create
4209                          * false positives if it truly is locale */
4210                         if (OP(scan) == NALNUMU) {
4211                             for (value = 0; value < 256; value++) {
4212                                 if (! isWORDCHAR_L1(value)) {
4213                                     ANYOF_BITMAP_SET(data->start_class, value);
4214                                 }
4215                             }
4216                         } else {
4217                             for (value = 0; value < 256; value++) {
4218                                 if (! isALNUM(value)) {
4219                                     ANYOF_BITMAP_SET(data->start_class, value);
4220                                 }
4221                             }
4222                         }
4223                     }
4224                     break;
4225                 case SPACE:
4226                     if (flags & SCF_DO_STCLASS_AND) {
4227                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
4228                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
4229                             if (OP(scan) == SPACEU) {
4230                                 for (value = 0; value < 256; value++) {
4231                                     if (!isSPACE_L1(value)) {
4232                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4233                                     }
4234                                 }
4235                             } else {
4236                                 for (value = 0; value < 256; value++) {
4237                                     if (!isSPACE(value)) {
4238                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4239                                     }
4240                                 }
4241                             }
4242                         }
4243                     }
4244                     else {
4245                         if (data->start_class->flags & ANYOF_LOCALE) {
4246                             ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
4247                         }
4248                         if (OP(scan) == SPACEU) {
4249                             for (value = 0; value < 256; value++) {
4250                                 if (isSPACE_L1(value)) {
4251                                     ANYOF_BITMAP_SET(data->start_class, value);
4252                                 }
4253                             }
4254                         } else {
4255                             for (value = 0; value < 256; value++) {
4256                                 if (isSPACE(value)) {
4257                                     ANYOF_BITMAP_SET(data->start_class, value);
4258                                 }
4259                             }
4260                         }
4261                     }
4262                     break;
4263                 case NSPACE:
4264                     if (flags & SCF_DO_STCLASS_AND) {
4265                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
4266                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
4267                             if (OP(scan) == NSPACEU) {
4268                                 for (value = 0; value < 256; value++) {
4269                                     if (isSPACE_L1(value)) {
4270                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4271                                     }
4272                                 }
4273                             } else {
4274                                 for (value = 0; value < 256; value++) {
4275                                     if (isSPACE(value)) {
4276                                         ANYOF_BITMAP_CLEAR(data->start_class, value);
4277                                     }
4278                                 }
4279                             }
4280                         }
4281                     }
4282                     else {
4283                         if (data->start_class->flags & ANYOF_LOCALE)
4284                             ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
4285                         if (OP(scan) == NSPACEU) {
4286                             for (value = 0; value < 256; value++) {
4287                                 if (!isSPACE_L1(value)) {
4288                                     ANYOF_BITMAP_SET(data->start_class, value);
4289                                 }
4290                             }
4291                         }
4292                         else {
4293                             for (value = 0; value < 256; value++) {
4294                                 if (!isSPACE(value)) {
4295                                     ANYOF_BITMAP_SET(data->start_class, value);
4296                                 }
4297                             }
4298                         }
4299                     }
4300                     break;
4301                 case DIGIT:
4302                     if (flags & SCF_DO_STCLASS_AND) {
4303                         if (!(data->start_class->flags & ANYOF_LOCALE)) {
4304                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
4305                             for (value = 0; value < 256; value++)
4306                                 if (!isDIGIT(value))
4307                                     ANYOF_BITMAP_CLEAR(data->start_class, value);
4308                         }
4309                     }
4310                     else {
4311                         if (data->start_class->flags & ANYOF_LOCALE)
4312                             ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
4313                         for (value = 0; value < 256; value++)
4314                             if (isDIGIT(value))
4315                                 ANYOF_BITMAP_SET(data->start_class, value);
4316                     }
4317                     break;
4318                 case NDIGIT:
4319                     if (flags & SCF_DO_STCLASS_AND) {
4320                         if (!(data->start_class->flags & ANYOF_LOCALE))
4321                             ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
4322                         for (value = 0; value < 256; value++)
4323                             if (isDIGIT(value))
4324                                 ANYOF_BITMAP_CLEAR(data->start_class, value);
4325                     }
4326                     else {
4327                         if (data->start_class->flags & ANYOF_LOCALE)
4328                             ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
4329                         for (value = 0; value < 256; value++)
4330                             if (!isDIGIT(value))
4331                                 ANYOF_BITMAP_SET(data->start_class, value);
4332                     }
4333                     break;
4334                 CASE_SYNST_FNC(VERTWS);
4335                 CASE_SYNST_FNC(HORIZWS);
4336
4337                 }
4338                 if (flags & SCF_DO_STCLASS_OR)
4339                     cl_and(data->start_class, and_withp);
4340                 flags &= ~SCF_DO_STCLASS;
4341             }
4342         }
4343         else if (PL_regkind[OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
4344             data->flags |= (OP(scan) == MEOL
4345                             ? SF_BEFORE_MEOL
4346                             : SF_BEFORE_SEOL);
4347         }
4348         else if (  PL_regkind[OP(scan)] == BRANCHJ
4349                  /* Lookbehind, or need to calculate parens/evals/stclass: */
4350                    && (scan->flags || data || (flags & SCF_DO_STCLASS))
4351                    && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
4352             if ( !PERL_ENABLE_POSITIVE_ASSERTION_STUDY 
4353                 || OP(scan) == UNLESSM )
4354             {
4355                 /* Negative Lookahead/lookbehind
4356                    In this case we can't do fixed string optimisation.
4357                 */
4358
4359                 I32 deltanext, minnext, fake = 0;
4360                 regnode *nscan;
4361                 struct regnode_charclass_class intrnl;
4362                 int f = 0;
4363
4364                 data_fake.flags = 0;
4365                 if (data) {
4366                     data_fake.whilem_c = data->whilem_c;
4367                     data_fake.last_closep = data->last_closep;
4368                 }
4369                 else
4370                     data_fake.last_closep = &fake;
4371                 data_fake.pos_delta = delta;
4372                 if ( flags & SCF_DO_STCLASS && !scan->flags
4373                      && OP(scan) == IFMATCH ) { /* Lookahead */
4374                     cl_init(pRExC_state, &intrnl);
4375                     data_fake.start_class = &intrnl;
4376                     f |= SCF_DO_STCLASS_AND;
4377                 }
4378                 if (flags & SCF_WHILEM_VISITED_POS)
4379                     f |= SCF_WHILEM_VISITED_POS;
4380                 next = regnext(scan);
4381                 nscan = NEXTOPER(NEXTOPER(scan));
4382                 minnext = study_chunk(pRExC_state, &nscan, minlenp, &deltanext, 
4383                     last, &data_fake, stopparen, recursed, NULL, f, depth+1);
4384                 if (scan->flags) {
4385                     if (deltanext) {
4386                         FAIL("Variable length lookbehind not implemented");
4387                     }
4388                     else if (minnext > (I32)U8_MAX) {
4389                         FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
4390                     }
4391                     scan->flags = (U8)minnext;
4392                 }
4393                 if (data) {
4394                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4395                         pars++;
4396                     if (data_fake.flags & SF_HAS_EVAL)
4397                         data->flags |= SF_HAS_EVAL;
4398                     data->whilem_c = data_fake.whilem_c;
4399                 }
4400                 if (f & SCF_DO_STCLASS_AND) {
4401                     if (flags & SCF_DO_STCLASS_OR) {
4402                         /* OR before, AND after: ideally we would recurse with
4403                          * data_fake to get the AND applied by study of the
4404                          * remainder of the pattern, and then derecurse;
4405                          * *** HACK *** for now just treat as "no information".
4406                          * See [perl #56690].
4407                          */
4408                         cl_init(pRExC_state, data->start_class);
4409                     }  else {
4410                         /* AND before and after: combine and continue */
4411                         const int was = (data->start_class->flags & ANYOF_EOS);
4412
4413                         cl_and(data->start_class, &intrnl);
4414                         if (was)
4415                             data->start_class->flags |= ANYOF_EOS;
4416                     }
4417                 }
4418             }
4419 #if PERL_ENABLE_POSITIVE_ASSERTION_STUDY
4420             else {
4421                 /* Positive Lookahead/lookbehind
4422                    In this case we can do fixed string optimisation,
4423                    but we must be careful about it. Note in the case of
4424                    lookbehind the positions will be offset by the minimum
4425                    length of the pattern, something we won't know about
4426                    until after the recurse.
4427                 */
4428                 I32 deltanext, fake = 0;
4429                 regnode *nscan;
4430                 struct regnode_charclass_class intrnl;
4431                 int f = 0;
4432                 /* We use SAVEFREEPV so that when the full compile 
4433                     is finished perl will clean up the allocated 
4434                     minlens when it's all done. This way we don't
4435                     have to worry about freeing them when we know
4436                     they wont be used, which would be a pain.
4437                  */
4438                 I32 *minnextp;
4439                 Newx( minnextp, 1, I32 );
4440                 SAVEFREEPV(minnextp);
4441
4442                 if (data) {
4443                     StructCopy(data, &data_fake, scan_data_t);
4444                     if ((flags & SCF_DO_SUBSTR) && data->last_found) {
4445                         f |= SCF_DO_SUBSTR;
4446                         if (scan->flags) 
4447                             SCAN_COMMIT(pRExC_state, &data_fake,minlenp);
4448                         data_fake.last_found=newSVsv(data->last_found);
4449                     }
4450                 }
4451                 else
4452                     data_fake.last_closep = &fake;
4453                 data_fake.flags = 0;
4454                 data_fake.pos_delta = delta;
4455                 if (is_inf)
4456                     data_fake.flags |= SF_IS_INF;
4457                 if ( flags & SCF_DO_STCLASS && !scan->flags
4458                      && OP(scan) == IFMATCH ) { /* Lookahead */
4459                     cl_init(pRExC_state, &intrnl);
4460                     data_fake.start_class = &intrnl;
4461                     f |= SCF_DO_STCLASS_AND;
4462                 }
4463                 if (flags & SCF_WHILEM_VISITED_POS)
4464                     f |= SCF_WHILEM_VISITED_POS;
4465                 next = regnext(scan);
4466                 nscan = NEXTOPER(NEXTOPER(scan));
4467
4468                 *minnextp = study_chunk(pRExC_state, &nscan, minnextp, &deltanext, 
4469                     last, &data_fake, stopparen, recursed, NULL, f,depth+1);
4470                 if (scan->flags) {
4471                     if (deltanext) {
4472                         FAIL("Variable length lookbehind not implemented");
4473                     }
4474                     else if (*minnextp > (I32)U8_MAX) {
4475                         FAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
4476                     }
4477                     scan->flags = (U8)*minnextp;
4478                 }
4479
4480                 *minnextp += min;
4481
4482                 if (f & SCF_DO_STCLASS_AND) {
4483                     const int was = (data->start_class->flags & ANYOF_EOS);
4484
4485                     cl_and(data->start_class, &intrnl);
4486                     if (was)
4487                         data->start_class->flags |= ANYOF_EOS;
4488                 }
4489                 if (data) {
4490                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4491                         pars++;
4492                     if (data_fake.flags & SF_HAS_EVAL)
4493                         data->flags |= SF_HAS_EVAL;
4494                     data->whilem_c = data_fake.whilem_c;
4495                     if ((flags & SCF_DO_SUBSTR) && data_fake.last_found) {
4496                         if (RExC_rx->minlen<*minnextp)
4497                             RExC_rx->minlen=*minnextp;
4498                         SCAN_COMMIT(pRExC_state, &data_fake, minnextp);
4499                         SvREFCNT_dec(data_fake.last_found);
4500                         
4501                         if ( data_fake.minlen_fixed != minlenp ) 
4502                         {
4503                             data->offset_fixed= data_fake.offset_fixed;
4504                             data->minlen_fixed= data_fake.minlen_fixed;
4505                             data->lookbehind_fixed+= scan->flags;
4506                         }
4507                         if ( data_fake.minlen_float != minlenp )
4508                         {
4509                             data->minlen_float= data_fake.minlen_float;
4510                             data->offset_float_min=data_fake.offset_float_min;
4511                             data->offset_float_max=data_fake.offset_float_max;
4512                             data->lookbehind_float+= scan->flags;
4513                         }
4514                     }
4515                 }
4516
4517
4518             }
4519 #endif
4520         }
4521         else if (OP(scan) == OPEN) {
4522             if (stopparen != (I32)ARG(scan))
4523                 pars++;
4524         }
4525         else if (OP(scan) == CLOSE) {
4526             if (stopparen == (I32)ARG(scan)) {
4527                 break;
4528             }
4529             if ((I32)ARG(scan) == is_par) {
4530                 next = regnext(scan);
4531
4532                 if ( next && (OP(next) != WHILEM) && next < last)
4533                     is_par = 0;         /* Disable optimization */
4534             }
4535             if (data)
4536                 *(data->last_closep) = ARG(scan);
4537         }
4538         else if (OP(scan) == EVAL) {
4539                 if (data)
4540                     data->flags |= SF_HAS_EVAL;
4541         }
4542         else if ( PL_regkind[OP(scan)] == ENDLIKE ) {
4543             if (flags & SCF_DO_SUBSTR) {
4544                 SCAN_COMMIT(pRExC_state,data,minlenp);
4545                 flags &= ~SCF_DO_SUBSTR;
4546             }
4547             if (data && OP(scan)==ACCEPT) {
4548                 data->flags |= SCF_SEEN_ACCEPT;
4549                 if (stopmin > min)
4550                     stopmin = min;
4551             }
4552         }
4553         else if (OP(scan) == LOGICAL && scan->flags == 2) /* Embedded follows */
4554         {
4555                 if (flags & SCF_DO_SUBSTR) {
4556                     SCAN_COMMIT(pRExC_state,data,minlenp);
4557                     data->longest = &(data->longest_float);
4558                 }
4559                 is_inf = is_inf_internal = 1;
4560                 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
4561                     cl_anything(pRExC_state, data->start_class);
4562                 flags &= ~SCF_DO_STCLASS;
4563         }
4564         else if (OP(scan) == GPOS) {
4565             if (!(RExC_rx->extflags & RXf_GPOS_FLOAT) &&
4566                 !(delta || is_inf || (data && data->pos_delta))) 
4567             {
4568                 if (!(RExC_rx->extflags & RXf_ANCH) && (flags & SCF_DO_SUBSTR))
4569                     RExC_rx->extflags |= RXf_ANCH_GPOS;
4570                 if (RExC_rx->gofs < (U32)min)
4571                     RExC_rx->gofs = min;
4572             } else {
4573                 RExC_rx->extflags |= RXf_GPOS_FLOAT;
4574                 RExC_rx->gofs = 0;
4575             }       
4576         }
4577 #ifdef TRIE_STUDY_OPT
4578 #ifdef FULL_TRIE_STUDY
4579         else if (PL_regkind[OP(scan)] == TRIE) {
4580             /* NOTE - There is similar code to this block above for handling
4581                BRANCH nodes on the initial study.  If you change stuff here
4582                check there too. */
4583             regnode *trie_node= scan;
4584             regnode *tail= regnext(scan);
4585             reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
4586             I32 max1 = 0, min1 = I32_MAX;
4587             struct regnode_charclass_class accum;
4588
4589             if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
4590                 SCAN_COMMIT(pRExC_state, data,minlenp); /* Cannot merge strings after this. */
4591             if (flags & SCF_DO_STCLASS)
4592                 cl_init_zero(pRExC_state, &accum);
4593                 
4594             if (!trie->jump) {
4595                 min1= trie->minlen;
4596                 max1= trie->maxlen;
4597             } else {
4598                 const regnode *nextbranch= NULL;
4599                 U32 word;
4600                 
4601                 for ( word=1 ; word <= trie->wordcount ; word++) 
4602                 {
4603                     I32 deltanext=0, minnext=0, f = 0, fake;
4604                     struct regnode_charclass_class this_class;
4605                     
4606                     data_fake.flags = 0;
4607                     if (data) {
4608                         data_fake.whilem_c = data->whilem_c;
4609                         data_fake.last_closep = data->last_closep;
4610                     }
4611                     else
4612                         data_fake.last_closep = &fake;
4613                     data_fake.pos_delta = delta;
4614                     if (flags & SCF_DO_STCLASS) {
4615                         cl_init(pRExC_state, &this_class);
4616                         data_fake.start_class = &this_class;
4617                         f = SCF_DO_STCLASS_AND;
4618                     }
4619                     if (flags & SCF_WHILEM_VISITED_POS)
4620                         f |= SCF_WHILEM_VISITED_POS;
4621     
4622                     if (trie->jump[word]) {
4623                         if (!nextbranch)
4624                             nextbranch = trie_node + trie->jump[0];
4625                         scan= trie_node + trie->jump[word];
4626                         /* We go from the jump point to the branch that follows
4627                            it. Note this means we need the vestigal unused branches
4628                            even though they arent otherwise used.
4629                          */
4630                         minnext = study_chunk(pRExC_state, &scan, minlenp, 
4631                             &deltanext, (regnode *)nextbranch, &data_fake, 
4632                             stopparen, recursed, NULL, f,depth+1);
4633                     }
4634                     if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
4635                         nextbranch= regnext((regnode*)nextbranch);
4636                     
4637                     if (min1 > (I32)(minnext + trie->minlen))
4638                         min1 = minnext + trie->minlen;
4639                     if (max1 < (I32)(minnext + deltanext + trie->maxlen))
4640                         max1 = minnext + deltanext + trie->maxlen;
4641                     if (deltanext == I32_MAX)
4642                         is_inf = is_inf_internal = 1;
4643                     
4644                     if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
4645                         pars++;
4646                     if (data_fake.flags & SCF_SEEN_ACCEPT) {
4647                         if ( stopmin > min + min1) 
4648                             stopmin = min + min1;
4649                         flags &= ~SCF_DO_SUBSTR;
4650                         if (data)
4651                             data->flags |= SCF_SEEN_ACCEPT;
4652                     }
4653                     if (data) {
4654                         if (data_fake.flags & SF_HAS_EVAL)
4655                             data->flags |= SF_HAS_EVAL;
4656                         data->whilem_c = data_fake.whilem_c;
4657                     }
4658                     if (flags & SCF_DO_STCLASS)
4659                         cl_or(pRExC_state, &accum, &this_class);
4660                 }
4661             }
4662             if (flags & SCF_DO_SUBSTR) {
4663                 data->pos_min += min1;
4664                 data->pos_delta += max1 - min1;
4665                 if (max1 != min1 || is_inf)
4666                     data->longest = &(data->longest_float);
4667             }
4668             min += min1;
4669             delta += max1 - min1;
4670             if (flags & SCF_DO_STCLASS_OR) {
4671                 cl_or(pRExC_state, data->start_class, &accum);
4672                 if (min1) {
4673                     cl_and(data->start_class, and_withp);
4674                     flags &= ~SCF_DO_STCLASS;
4675                 }
4676             }
4677             else if (flags & SCF_DO_STCLASS_AND) {
4678                 if (min1) {
4679                     cl_and(data->start_class, &accum);
4680                     flags &= ~SCF_DO_STCLASS;
4681                 }
4682                 else {
4683                     /* Switch to OR mode: cache the old value of
4684                      * data->start_class */
4685                     INIT_AND_WITHP;
4686                     StructCopy(data->start_class, and_withp,
4687                                struct regnode_charclass_class);
4688                     flags &= ~SCF_DO_STCLASS_AND;
4689                     StructCopy(&accum, data->start_class,
4690                                struct regnode_charclass_class);
4691                     flags |= SCF_DO_STCLASS_OR;
4692                     data->start_class->flags |= ANYOF_EOS;
4693                 }
4694             }
4695             scan= tail;
4696             continue;
4697         }
4698 #else
4699         else if (PL_regkind[OP(scan)] == TRIE) {
4700             reg_trie_data *trie = (reg_trie_data*)RExC_rxi->data->data[ ARG(scan) ];
4701             U8*bang=NULL;
4702             
4703             min += trie->minlen;
4704             delta += (trie->maxlen - trie->minlen);
4705             flags &= ~SCF_DO_STCLASS; /* xxx */
4706             if (flags & SCF_DO_SUBSTR) {
4707                 SCAN_COMMIT(pRExC_state,data,minlenp);  /* Cannot expect anything... */
4708                 data->pos_min += trie->minlen;
4709                 data->pos_delta += (trie->maxlen - trie->minlen);
4710                 if (trie->maxlen != trie->minlen)
4711                     data->longest = &(data->longest_float);
4712             }
4713             if (trie->jump) /* no more substrings -- for now /grr*/
4714                 flags &= ~SCF_DO_SUBSTR; 
4715         }
4716 #endif /* old or new */
4717 #endif /* TRIE_STUDY_OPT */
4718
4719         /* Else: zero-length, ignore. */
4720         scan = regnext(scan);
4721     }
4722     if (frame) {
4723         last = frame->last;
4724         scan = frame->next;
4725         stopparen = frame->stop;
4726         frame = frame->prev;
4727         goto fake_study_recurse;
4728     }
4729
4730   finish:
4731     assert(!frame);
4732     DEBUG_STUDYDATA("pre-fin:",data,depth);
4733
4734     *scanp = scan;
4735     *deltap = is_inf_internal ? I32_MAX : delta;
4736     if (flags & SCF_DO_SUBSTR && is_inf)
4737         data->pos_delta = I32_MAX - data->pos_min;
4738     if (is_par > (I32)U8_MAX)
4739         is_par = 0;
4740     if (is_par && pars==1 && data) {
4741         data->flags |= SF_IN_PAR;
4742         data->flags &= ~SF_HAS_PAR;
4743     }
4744     else if (pars && data) {
4745         data->flags |= SF_HAS_PAR;
4746         data->flags &= ~SF_IN_PAR;
4747     }
4748     if (flags & SCF_DO_STCLASS_OR)
4749         cl_and(data->start_class, and_withp);
4750     if (flags & SCF_TRIE_RESTUDY)
4751         data->flags |=  SCF_TRIE_RESTUDY;
4752     
4753     DEBUG_STUDYDATA("post-fin:",data,depth);
4754     
4755     return min < stopmin ? min : stopmin;
4756 }
4757
4758 STATIC U32
4759 S_add_data(RExC_state_t *pRExC_state, U32 n, const char *s)
4760 {
4761     U32 count = RExC_rxi->data ? RExC_rxi->data->count : 0;
4762
4763     PERL_ARGS_ASSERT_ADD_DATA;
4764
4765     Renewc(RExC_rxi->data,
4766            sizeof(*RExC_rxi->data) + sizeof(void*) * (count + n - 1),
4767            char, struct reg_data);
4768     if(count)
4769         Renew(RExC_rxi->data->what, count + n, U8);
4770     else
4771         Newx(RExC_rxi->data->what, n, U8);
4772     RExC_rxi->data->count = count + n;
4773     Copy(s, RExC_rxi->data->what + count, n, U8);
4774     return count;
4775 }
4776
4777 /*XXX: todo make this not included in a non debugging perl */
4778 #ifndef PERL_IN_XSUB_RE
4779 void
4780 Perl_reginitcolors(pTHX)
4781 {
4782     dVAR;
4783     const char * const s = PerlEnv_getenv("PERL_RE_COLORS");
4784     if (s) {
4785         char *t = savepv(s);
4786         int i = 0;
4787         PL_colors[0] = t;
4788         while (++i < 6) {
4789             t = strchr(t, '\t');
4790             if (t) {
4791                 *t = '\0';
4792                 PL_colors[i] = ++t;
4793             }
4794             else
4795                 PL_colors[i] = t = (char *)"";
4796         }
4797     } else {
4798         int i = 0;
4799         while (i < 6)
4800             PL_colors[i++] = (char *)"";
4801     }
4802     PL_colorset = 1;
4803 }
4804 #endif
4805
4806
4807 #ifdef TRIE_STUDY_OPT
4808 #define CHECK_RESTUDY_GOTO                                  \
4809         if (                                                \
4810               (data.flags & SCF_TRIE_RESTUDY)               \
4811               && ! restudied++                              \
4812         )     goto reStudy
4813 #else
4814 #define CHECK_RESTUDY_GOTO
4815 #endif        
4816
4817 /*
4818  - pregcomp - compile a regular expression into internal code
4819  *
4820  * We can't allocate space until we know how big the compiled form will be,
4821  * but we can't compile it (and thus know how big it is) until we've got a
4822  * place to put the code.  So we cheat:  we compile it twice, once with code
4823  * generation turned off and size counting turned on, and once "for real".
4824  * This also means that we don't allocate space until we are sure that the
4825  * thing really will compile successfully, and we never have to move the
4826  * code and thus invalidate pointers into it.  (Note that it has to be in
4827  * one piece because free() must be able to free it all.) [NB: not true in perl]
4828  *
4829  * Beware that the optimization-preparation code in here knows about some
4830  * of the structure of the compiled regexp.  [I'll say.]
4831  */
4832
4833
4834
4835 #ifndef PERL_IN_XSUB_RE
4836 #define RE_ENGINE_PTR &reh_regexp_engine
4837 #else
4838 extern const struct regexp_engine my_reg_engine;
4839 #define RE_ENGINE_PTR &my_reg_engine
4840 #endif
4841
4842 #ifndef PERL_IN_XSUB_RE 
4843 REGEXP *
4844 Perl_pregcomp(pTHX_ SV * const pattern, const U32 flags)
4845 {
4846     dVAR;
4847     HV * const table = GvHV(PL_hintgv);
4848
4849     PERL_ARGS_ASSERT_PREGCOMP;
4850
4851     /* Dispatch a request to compile a regexp to correct 
4852        regexp engine. */
4853     if (table) {
4854         SV **ptr= hv_fetchs(table, "regcomp", FALSE);
4855         GET_RE_DEBUG_FLAGS_DECL;
4856         if (ptr && SvIOK(*ptr) && SvIV(*ptr)) {
4857             const regexp_engine *eng=INT2PTR(regexp_engine*,SvIV(*ptr));
4858             DEBUG_COMPILE_r({
4859                 PerlIO_printf(Perl_debug_log, "Using engine %"UVxf"\n",
4860                     SvIV(*ptr));
4861             });            
4862             return CALLREGCOMP_ENG(eng, pattern, flags);
4863         } 
4864     }
4865     return Perl_re_compile(aTHX_ pattern, flags);
4866 }
4867 #endif
4868
4869 REGEXP *
4870 Perl_re_compile(pTHX_ SV * const pattern, U32 orig_pm_flags)
4871 {
4872     dVAR;
4873     REGEXP *rx;
4874     struct regexp *r;
4875     register regexp_internal *ri;
4876     STRLEN plen;
4877     char* VOL exp;
4878     char* xend;
4879     regnode *scan;
4880     I32 flags;
4881     I32 minlen = 0;
4882     U32 pm_flags;
4883
4884     /* these are all flags - maybe they should be turned
4885      * into a single int with different bit masks */
4886     I32 sawlookahead = 0;
4887     I32 sawplus = 0;
4888     I32 sawopen = 0;
4889     bool used_setjump = FALSE;
4890     regex_charset initial_charset = get_regex_charset(orig_pm_flags);
4891
4892     U8 jump_ret = 0;
4893     dJMPENV;
4894     scan_data_t data;
4895     RExC_state_t RExC_state;
4896     RExC_state_t * const pRExC_state = &RExC_state;
4897 #ifdef TRIE_STUDY_OPT    
4898     int restudied;
4899     RExC_state_t copyRExC_state;
4900 #endif    
4901     GET_RE_DEBUG_FLAGS_DECL;
4902
4903     PERL_ARGS_ASSERT_RE_COMPILE;
4904
4905     DEBUG_r(if (!PL_colorset) reginitcolors());
4906
4907 #ifndef PERL_IN_XSUB_RE
4908     /* Initialize these here instead of as-needed, as is quick and avoids
4909      * having to test them each time otherwise */
4910     if (! PL_AboveLatin1) {
4911         PL_AboveLatin1 = _new_invlist_C_array(AboveLatin1_invlist);
4912         PL_ASCII = _new_invlist_C_array(ASCII_invlist);
4913         PL_Latin1 = _new_invlist_C_array(Latin1_invlist);
4914
4915         PL_L1PosixAlnum = _new_invlist_C_array(L1PosixAlnum_invlist);
4916         PL_PosixAlnum = _new_invlist_C_array(PosixAlnum_invlist);
4917
4918         PL_L1PosixAlpha = _new_invlist_C_array(L1PosixAlpha_invlist);
4919         PL_PosixAlpha = _new_invlist_C_array(PosixAlpha_invlist);
4920
4921         PL_PosixBlank = _new_invlist_C_array(PosixBlank_invlist);
4922         PL_XPosixBlank = _new_invlist_C_array(XPosixBlank_invlist);
4923
4924         PL_L1Cased = _new_invlist_C_array(L1Cased_invlist);
4925
4926         PL_PosixCntrl = _new_invlist_C_array(PosixCntrl_invlist);
4927         PL_XPosixCntrl = _new_invlist_C_array(XPosixCntrl_invlist);
4928
4929         PL_PosixDigit = _new_invlist_C_array(PosixDigit_invlist);
4930
4931         PL_L1PosixGraph = _new_invlist_C_array(L1PosixGraph_invlist);
4932         PL_PosixGraph = _new_invlist_C_array(PosixGraph_invlist);
4933
4934         PL_L1PosixAlnum = _new_invlist_C_array(L1PosixAlnum_invlist);
4935         PL_PosixAlnum = _new_invlist_C_array(PosixAlnum_invlist);
4936
4937         PL_L1PosixLower = _new_invlist_C_array(L1PosixLower_invlist);
4938         PL_PosixLower = _new_invlist_C_array(PosixLower_invlist);
4939
4940         PL_L1PosixPrint = _new_invlist_C_array(L1PosixPrint_invlist);
4941         PL_PosixPrint = _new_invlist_C_array(PosixPrint_invlist);
4942
4943         PL_L1PosixPunct = _new_invlist_C_array(L1PosixPunct_invlist);
4944         PL_PosixPunct = _new_invlist_C_array(PosixPunct_invlist);
4945
4946         PL_PerlSpace = _new_invlist_C_array(PerlSpace_invlist);
4947         PL_XPerlSpace = _new_invlist_C_array(XPerlSpace_invlist);
4948
4949         PL_PosixSpace = _new_invlist_C_array(PosixSpace_invlist);
4950         PL_XPosixSpace = _new_invlist_C_array(XPosixSpace_invlist);
4951
4952         PL_L1PosixUpper = _new_invlist_C_array(L1PosixUpper_invlist);
4953         PL_PosixUpper = _new_invlist_C_array(PosixUpper_invlist);
4954
4955         PL_VertSpace = _new_invlist_C_array(VertSpace_invlist);
4956
4957         PL_PosixWord = _new_invlist_C_array(PosixWord_invlist);
4958         PL_L1PosixWord = _new_invlist_C_array(L1PosixWord_invlist);
4959
4960         PL_PosixXDigit = _new_invlist_C_array(PosixXDigit_invlist);
4961         PL_XPosixXDigit = _new_invlist_C_array(XPosixXDigit_invlist);
4962     }
4963 #endif
4964
4965     exp = SvPV(pattern, plen);
4966
4967     if (plen == 0) { /* ignore the utf8ness if the pattern is 0 length */
4968         RExC_utf8 = RExC_orig_utf8 = 0;
4969     }
4970     else {
4971         RExC_utf8 = RExC_orig_utf8 = SvUTF8(pattern);
4972     }
4973     RExC_uni_semantics = 0;
4974     RExC_contains_locale = 0;
4975
4976     /****************** LONG JUMP TARGET HERE***********************/
4977     /* Longjmp back to here if have to switch in midstream to utf8 */
4978     if (! RExC_orig_utf8) {
4979         JMPENV_PUSH(jump_ret);
4980         used_setjump = TRUE;
4981     }
4982
4983     if (jump_ret == 0) {    /* First time through */
4984         xend = exp + plen;
4985
4986         DEBUG_COMPILE_r({
4987             SV *dsv= sv_newmortal();
4988             RE_PV_QUOTED_DECL(s, RExC_utf8,
4989                 dsv, exp, plen, 60);
4990             PerlIO_printf(Perl_debug_log, "%sCompiling REx%s %s\n",
4991                            PL_colors[4],PL_colors[5],s);
4992         });
4993     }
4994     else {  /* longjumped back */
4995         STRLEN len = plen;
4996
4997         /* If the cause for the longjmp was other than changing to utf8, pop
4998          * our own setjmp, and longjmp to the correct handler */
4999         if (jump_ret != UTF8_LONGJMP) {
5000             JMPENV_POP;
5001             JMPENV_JUMP(jump_ret);
5002         }
5003
5004         GET_RE_DEBUG_FLAGS;
5005
5006         /* It's possible to write a regexp in ascii that represents Unicode
5007         codepoints outside of the byte range, such as via \x{100}. If we
5008         detect such a sequence we have to convert the entire pattern to utf8
5009         and then recompile, as our sizing calculation will have been based
5010         on 1 byte == 1 character, but we will need to use utf8 to encode
5011         at least some part of the pattern, and therefore must convert the whole
5012         thing.
5013         -- dmq */
5014         DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log,
5015             "UTF8 mismatch! Converting to utf8 for resizing and compile\n"));
5016         exp = (char*)Perl_bytes_to_utf8(aTHX_
5017                                         (U8*)SvPV_nomg(pattern, plen),
5018                                         &len);
5019         xend = exp + len;
5020         RExC_orig_utf8 = RExC_utf8 = 1;
5021         SAVEFREEPV(exp);
5022     }
5023
5024 #ifdef TRIE_STUDY_OPT
5025     restudied = 0;
5026 #endif
5027
5028     pm_flags = orig_pm_flags;
5029
5030     if (initial_charset == REGEX_LOCALE_CHARSET) {
5031         RExC_contains_locale = 1;
5032     }
5033     else if (RExC_utf8 && initial_charset == REGEX_DEPENDS_CHARSET) {
5034
5035         /* Set to use unicode semantics if the pattern is in utf8 and has the
5036          * 'depends' charset specified, as it means unicode when utf8  */
5037         set_regex_charset(&pm_flags, REGEX_UNICODE_CHARSET);
5038     }
5039
5040     RExC_precomp = exp;
5041     RExC_flags = pm_flags;
5042     RExC_sawback = 0;
5043
5044     RExC_seen = 0;
5045     RExC_in_lookbehind = 0;
5046     RExC_seen_zerolen = *exp == '^' ? -1 : 0;
5047     RExC_seen_evals = 0;
5048     RExC_extralen = 0;
5049     RExC_override_recoding = 0;
5050
5051     /* First pass: determine size, legality. */
5052     RExC_parse = exp;
5053     RExC_start = exp;
5054     RExC_end = xend;
5055     RExC_naughty = 0;
5056     RExC_npar = 1;
5057     RExC_nestroot = 0;
5058     RExC_size = 0L;
5059     RExC_emit = &PL_regdummy;
5060     RExC_whilem_seen = 0;
5061     RExC_open_parens = NULL;
5062     RExC_close_parens = NULL;
5063     RExC_opend = NULL;
5064     RExC_paren_names = NULL;
5065 #ifdef DEBUGGING
5066     RExC_paren_name_list = NULL;
5067 #endif
5068     RExC_recurse = NULL;
5069     RExC_recurse_count = 0;
5070
5071 #if 0 /* REGC() is (currently) a NOP at the first pass.
5072        * Clever compilers notice this and complain. --jhi */
5073     REGC((U8)REG_MAGIC, (char*)RExC_emit);
5074 #endif
5075     DEBUG_PARSE_r(
5076         PerlIO_printf(Perl_debug_log, "Starting first pass (sizing)\n");
5077         RExC_lastnum=0;
5078         RExC_lastparse=NULL;
5079     );
5080     if (reg(pRExC_state, 0, &flags,1) == NULL) {
5081         RExC_precomp = NULL;
5082         return(NULL);
5083     }
5084
5085     /* Here, finished first pass.  Get rid of any added setjmp */
5086     if (used_setjump) {
5087         JMPENV_POP;
5088     }
5089
5090     DEBUG_PARSE_r({
5091         PerlIO_printf(Perl_debug_log, 
5092             "Required size %"IVdf" nodes\n"
5093             "Starting second pass (creation)\n", 
5094             (IV)RExC_size);
5095         RExC_lastnum=0; 
5096         RExC_lastparse=NULL; 
5097     });
5098
5099     /* The first pass could have found things that force Unicode semantics */
5100     if ((RExC_utf8 || RExC_uni_semantics)
5101          && get_regex_charset(pm_flags) == REGEX_DEPENDS_CHARSET)
5102     {
5103         set_regex_charset(&pm_flags, REGEX_UNICODE_CHARSET);
5104     }
5105
5106     /* Small enough for pointer-storage convention?
5107        If extralen==0, this means that we will not need long jumps. */
5108     if (RExC_size >= 0x10000L && RExC_extralen)
5109         RExC_size += RExC_extralen;
5110     else
5111         RExC_extralen = 0;
5112     if (RExC_whilem_seen > 15)
5113         RExC_whilem_seen = 15;
5114
5115     /* Allocate space and zero-initialize. Note, the two step process 
5116        of zeroing when in debug mode, thus anything assigned has to 
5117        happen after that */
5118     rx = (REGEXP*) newSV_type(SVt_REGEXP);
5119     r = (struct regexp*)SvANY(rx);
5120     Newxc(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode),
5121          char, regexp_internal);
5122     if ( r == NULL || ri == NULL )
5123         FAIL("Regexp out of space");
5124 #ifdef DEBUGGING
5125     /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
5126     Zero(ri, sizeof(regexp_internal) + (unsigned)RExC_size * sizeof(regnode), char);
5127 #else 
5128     /* bulk initialize base fields with 0. */
5129     Zero(ri, sizeof(regexp_internal), char);        
5130 #endif
5131
5132     /* non-zero initialization begins here */
5133     RXi_SET( r, ri );
5134     r->engine= RE_ENGINE_PTR;
5135     r->extflags = pm_flags;
5136     {
5137         bool has_p     = ((r->extflags & RXf_PMf_KEEPCOPY) == RXf_PMf_KEEPCOPY);
5138         bool has_charset = (get_regex_charset(r->extflags) != REGEX_DEPENDS_CHARSET);
5139
5140         /* The caret is output if there are any defaults: if not all the STD
5141          * flags are set, or if no character set specifier is needed */
5142         bool has_default =
5143                     (((r->extflags & RXf_PMf_STD_PMMOD) != RXf_PMf_STD_PMMOD)
5144                     || ! has_charset);
5145         bool has_runon = ((RExC_seen & REG_SEEN_RUN_ON_COMMENT)==REG_SEEN_RUN_ON_COMMENT);
5146         U16 reganch = (U16)((r->extflags & RXf_PMf_STD_PMMOD)
5147                             >> RXf_PMf_STD_PMMOD_SHIFT);
5148         const char *fptr = STD_PAT_MODS;        /*"msix"*/
5149         char *p;
5150         /* Allocate for the worst case, which is all the std flags are turned
5151          * on.  If more precision is desired, we could do a population count of
5152          * the flags set.  This could be done with a small lookup table, or by
5153          * shifting, masking and adding, or even, when available, assembly
5154          * language for a machine-language population count.
5155          * We never output a minus, as all those are defaults, so are
5156          * covered by the caret */
5157         const STRLEN wraplen = plen + has_p + has_runon
5158             + has_default       /* If needs a caret */
5159
5160                 /* If needs a character set specifier */
5161             + ((has_charset) ? MAX_CHARSET_NAME_LENGTH : 0)
5162             + (sizeof(STD_PAT_MODS) - 1)
5163             + (sizeof("(?:)") - 1);
5164
5165         p = sv_grow(MUTABLE_SV(rx), wraplen + 1); /* +1 for the ending NUL */
5166         SvPOK_on(rx);
5167         SvFLAGS(rx) |= SvUTF8(pattern);
5168         *p++='('; *p++='?';
5169
5170         /* If a default, cover it using the caret */
5171         if (has_default) {
5172             *p++= DEFAULT_PAT_MOD;
5173         }
5174         if (has_charset) {
5175             STRLEN len;
5176             const char* const name = get_regex_charset_name(r->extflags, &len);
5177             Copy(name, p, len, char);
5178             p += len;
5179         }
5180         if (has_p)
5181             *p++ = KEEPCOPY_PAT_MOD; /*'p'*/
5182         {
5183             char ch;
5184             while((ch = *fptr++)) {
5185                 if(reganch & 1)
5186                     *p++ = ch;
5187                 reganch >>= 1;
5188             }
5189         }
5190
5191         *p++ = ':';
5192         Copy(RExC_precomp, p, plen, char);
5193         assert ((RX_WRAPPED(rx) - p) < 16);
5194         r->pre_prefix = p - RX_WRAPPED(rx);
5195         p += plen;
5196         if (has_runon)
5197             *p++ = '\n';
5198         *p++ = ')';
5199         *p = 0;
5200         SvCUR_set(rx, p - SvPVX_const(rx));
5201     }
5202
5203     r->intflags = 0;
5204     r->nparens = RExC_npar - 1; /* set early to validate backrefs */
5205     
5206     if (RExC_seen & REG_SEEN_RECURSE) {
5207         Newxz(RExC_open_parens, RExC_npar,regnode *);
5208         SAVEFREEPV(RExC_open_parens);
5209         Newxz(RExC_close_parens,RExC_npar,regnode *);
5210         SAVEFREEPV(RExC_close_parens);
5211     }
5212
5213     /* Useful during FAIL. */
5214 #ifdef RE_TRACK_PATTERN_OFFSETS
5215     Newxz(ri->u.offsets, 2*RExC_size+1, U32); /* MJD 20001228 */
5216     DEBUG_OFFSETS_r(PerlIO_printf(Perl_debug_log,
5217                           "%s %"UVuf" bytes for offset annotations.\n",
5218                           ri->u.offsets ? "Got" : "Couldn't get",
5219                           (UV)((2*RExC_size+1) * sizeof(U32))));
5220 #endif
5221     SetProgLen(ri,RExC_size);
5222     RExC_rx_sv = rx;
5223     RExC_rx = r;
5224     RExC_rxi = ri;
5225     REH_CALL_COMP_BEGIN_HOOK(pRExC_state->rx);
5226
5227     /* Second pass: emit code. */
5228     RExC_flags = pm_flags;      /* don't let top level (?i) bleed */
5229     RExC_parse = exp;
5230     RExC_end = xend;
5231     RExC_naughty = 0;
5232     RExC_npar = 1;
5233     RExC_emit_start = ri->program;
5234     RExC_emit = ri->program;
5235     RExC_emit_bound = ri->program + RExC_size + 1;
5236
5237     /* Store the count of eval-groups for security checks: */
5238     RExC_rx->seen_evals = RExC_seen_evals;
5239     REGC((U8)REG_MAGIC, (char*) RExC_emit++);
5240     if (reg(pRExC_state, 0, &flags,1) == NULL) {
5241         ReREFCNT_dec(rx);   
5242         return(NULL);
5243     }
5244     /* XXXX To minimize changes to RE engine we always allocate
5245        3-units-long substrs field. */
5246     Newx(r->substrs, 1, struct reg_substr_data);
5247     if (RExC_recurse_count) {
5248         Newxz(RExC_recurse,RExC_recurse_count,regnode *);
5249         SAVEFREEPV(RExC_recurse);
5250     }
5251
5252 reStudy:
5253     r->minlen = minlen = sawlookahead = sawplus = sawopen = 0;
5254     Zero(r->substrs, 1, struct reg_substr_data);
5255
5256 #ifdef TRIE_STUDY_OPT
5257     if (!restudied) {
5258         StructCopy(&zero_scan_data, &data, scan_data_t);
5259         copyRExC_state = RExC_state;
5260     } else {
5261         U32 seen=RExC_seen;
5262         DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log,"Restudying\n"));
5263         
5264         RExC_state = copyRExC_state;
5265         if (seen & REG_TOP_LEVEL_BRANCHES) 
5266             RExC_seen |= REG_TOP_LEVEL_BRANCHES;
5267         else
5268             RExC_seen &= ~REG_TOP_LEVEL_BRANCHES;
5269         if (data.last_found) {
5270             SvREFCNT_dec(data.longest_fixed);
5271             SvREFCNT_dec(data.longest_float);
5272             SvREFCNT_dec(data.last_found);
5273         }
5274         StructCopy(&zero_scan_data, &data, scan_data_t);
5275     }
5276 #else
5277     StructCopy(&zero_scan_data, &data, scan_data_t);
5278 #endif    
5279
5280     /* Dig out information for optimizations. */
5281     r->extflags = RExC_flags; /* was pm_op */
5282     /*dmq: removed as part of de-PMOP: pm->op_pmflags = RExC_flags; */
5283  
5284     if (UTF)
5285         SvUTF8_on(rx);  /* Unicode in it? */
5286     ri->regstclass = NULL;
5287     if (RExC_naughty >= 10)     /* Probably an expensive pattern. */
5288         r->intflags |= PREGf_NAUGHTY;
5289     scan = ri->program + 1;             /* First BRANCH. */
5290
5291     /* testing for BRANCH here tells us whether there is "must appear"
5292        data in the pattern. If there is then we can use it for optimisations */
5293     if (!(RExC_seen & REG_TOP_LEVEL_BRANCHES)) { /*  Only one top-level choice. */
5294         I32 fake;
5295         STRLEN longest_float_length, longest_fixed_length;
5296         struct regnode_charclass_class ch_class; /* pointed to by data */
5297         int stclass_flag;
5298         I32 last_close = 0; /* pointed to by data */
5299         regnode *first= scan;
5300         regnode *first_next= regnext(first);
5301         /*
5302          * Skip introductions and multiplicators >= 1
5303          * so that we can extract the 'meat' of the pattern that must 
5304          * match in the large if() sequence following.
5305          * NOTE that EXACT is NOT covered here, as it is normally
5306          * picked up by the optimiser separately. 
5307          *
5308          * This is unfortunate as the optimiser isnt handling lookahead
5309          * properly currently.
5310          *
5311          */
5312         while ((OP(first) == OPEN && (sawopen = 1)) ||
5313                /* An OR of *one* alternative - should not happen now. */
5314             (OP(first) == BRANCH && OP(first_next) != BRANCH) ||
5315             /* for now we can't handle lookbehind IFMATCH*/
5316             (OP(first) == IFMATCH && !first->flags && (sawlookahead = 1)) ||
5317             (OP(first) == PLUS) ||
5318             (OP(first) == MINMOD) ||
5319                /* An {n,m} with n>0 */
5320             (PL_regkind[OP(first)] == CURLY && ARG1(first) > 0) ||
5321             (OP(first) == NOTHING && PL_regkind[OP(first_next)] != END ))
5322         {
5323                 /* 
5324                  * the only op that could be a regnode is PLUS, all the rest
5325                  * will be regnode_1 or regnode_2.
5326                  *
5327                  */
5328                 if (OP(first) == PLUS)
5329                     sawplus = 1;
5330                 else
5331                     first += regarglen[OP(first)];
5332
5333                 first = NEXTOPER(first);
5334                 first_next= regnext(first);
5335         }
5336
5337         /* Starting-point info. */
5338       again:
5339         DEBUG_PEEP("first:",first,0);
5340         /* Ignore EXACT as we deal with it later. */
5341         if (PL_regkind[OP(first)] == EXACT) {
5342             if (OP(first) == EXACT)
5343                 NOOP;   /* Empty, get anchored substr later. */
5344             else
5345                 ri->regstclass = first;
5346         }
5347 #ifdef TRIE_STCLASS
5348         else if (PL_regkind[OP(first)] == TRIE &&
5349                 ((reg_trie_data *)ri->data->data[ ARG(first) ])->minlen>0) 
5350         {
5351             regnode *trie_op;
5352             /* this can happen only on restudy */
5353             if ( OP(first) == TRIE ) {
5354                 struct regnode_1 *trieop = (struct regnode_1 *)
5355                     PerlMemShared_calloc(1, sizeof(struct regnode_1));
5356                 StructCopy(first,trieop,struct regnode_1);
5357                 trie_op=(regnode *)trieop;
5358             } else {
5359                 struct regnode_charclass *trieop = (struct regnode_charclass *)
5360                     PerlMemShared_calloc(1, sizeof(struct regnode_charclass));
5361                 StructCopy(first,trieop,struct regnode_charclass);
5362                 trie_op=(regnode *)trieop;
5363             }
5364             OP(trie_op)+=2;
5365             make_trie_failtable(pRExC_state, (regnode *)first, trie_op, 0);
5366             ri->regstclass = trie_op;
5367         }
5368 #endif
5369         else if (REGNODE_SIMPLE(OP(first)))
5370             ri->regstclass = first;
5371         else if (PL_regkind[OP(first)] == BOUND ||
5372                  PL_regkind[OP(first)] == NBOUND)
5373             ri->regstclass = first;
5374         else if (PL_regkind[OP(first)] == BOL) {
5375             r->extflags |= (OP(first) == MBOL
5376                            ? RXf_ANCH_MBOL
5377                            : (OP(first) == SBOL
5378                               ? RXf_ANCH_SBOL
5379                               : RXf_ANCH_BOL));
5380             first = NEXTOPER(first);
5381             goto again;
5382         }
5383         else if (OP(first) == GPOS) {
5384             r->extflags |= RXf_ANCH_GPOS;
5385             first = NEXTOPER(first);
5386             goto again;
5387         }
5388         else if ((!sawopen || !RExC_sawback) &&
5389             (OP(first) == STAR &&
5390             PL_regkind[OP(NEXTOPER(first))] == REG_ANY) &&
5391             !(r->extflags & RXf_ANCH) && !(RExC_seen & REG_SEEN_EVAL))
5392         {
5393             /* turn .* into ^.* with an implied $*=1 */
5394             const int type =
5395                 (OP(NEXTOPER(first)) == REG_ANY)
5396                     ? RXf_ANCH_MBOL
5397                     : RXf_ANCH_SBOL;
5398             r->extflags |= type;
5399             r->intflags |= PREGf_IMPLICIT;
5400             first = NEXTOPER(first);
5401             goto again;
5402         }
5403         if (sawplus && !sawlookahead && (!sawopen || !RExC_sawback)
5404             && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
5405             /* x+ must match at the 1st pos of run of x's */
5406             r->intflags |= PREGf_SKIP;
5407
5408         /* Scan is after the zeroth branch, first is atomic matcher. */
5409 #ifdef TRIE_STUDY_OPT
5410         DEBUG_PARSE_r(
5411             if (!restudied)
5412                 PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
5413                               (IV)(first - scan + 1))
5414         );
5415 #else
5416         DEBUG_PARSE_r(
5417             PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
5418                 (IV)(first - scan + 1))
5419         );
5420 #endif
5421
5422
5423         /*
5424         * If there's something expensive in the r.e., find the
5425         * longest literal string that must appear and make it the
5426         * regmust.  Resolve ties in favor of later strings, since
5427         * the regstart check works with the beginning of the r.e.
5428         * and avoiding duplication strengthens checking.  Not a
5429         * strong reason, but sufficient in the absence of others.
5430         * [Now we resolve ties in favor of the earlier string if
5431         * it happens that c_offset_min has been invalidated, since the
5432         * earlier string may buy us something the later one won't.]
5433         */
5434
5435         data.longest_fixed = newSVpvs("");
5436         data.longest_float = newSVpvs("");
5437         data.last_found = newSVpvs("");
5438         data.longest = &(data.longest_fixed);
5439         first = scan;
5440         if (!ri->regstclass) {
5441             cl_init(pRExC_state, &ch_class);
5442             data.start_class = &ch_class;
5443             stclass_flag = SCF_DO_STCLASS_AND;
5444         } else                          /* XXXX Check for BOUND? */
5445             stclass_flag = 0;
5446         data.last_closep = &last_close;
5447         
5448         minlen = study_chunk(pRExC_state, &first, &minlen, &fake, scan + RExC_size, /* Up to end */
5449             &data, -1, NULL, NULL,
5450             SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag,0);
5451
5452
5453         CHECK_RESTUDY_GOTO;
5454
5455
5456         if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
5457              && data.last_start_min == 0 && data.last_end > 0
5458              && !RExC_seen_zerolen
5459              && !(RExC_seen & REG_SEEN_VERBARG)
5460              && (!(RExC_seen & REG_SEEN_GPOS) || (r->extflags & RXf_ANCH_GPOS)))
5461             r->extflags |= RXf_CHECK_ALL;
5462         scan_commit(pRExC_state, &data,&minlen,0);
5463         SvREFCNT_dec(data.last_found);
5464
5465         /* Note that code very similar to this but for anchored string 
5466            follows immediately below, changes may need to be made to both. 
5467            Be careful. 
5468          */
5469         longest_float_length = CHR_SVLEN(data.longest_float);
5470         if (longest_float_length
5471             || (data.flags & SF_FL_BEFORE_EOL
5472                 && (!(data.flags & SF_FL_BEFORE_MEOL)
5473                     || (RExC_flags & RXf_PMf_MULTILINE)))) 
5474         {
5475             I32 t,ml;
5476
5477             /* See comments for join_exact for why REG_SEEN_EXACTF_SHARP_S */
5478             if ((RExC_seen & REG_SEEN_EXACTF_SHARP_S)
5479                 || (SvCUR(data.longest_fixed)  /* ok to leave SvCUR */
5480                     && data.offset_fixed == data.offset_float_min
5481                     && SvCUR(data.longest_fixed) == SvCUR(data.longest_float)))
5482                     goto remove_float;          /* As in (a)+. */
5483
5484             /* copy the information about the longest float from the reg_scan_data
5485                over to the program. */
5486             if (SvUTF8(data.longest_float)) {
5487                 r->float_utf8 = data.longest_float;
5488                 r->float_substr = NULL;
5489             } else {
5490                 r->float_substr = data.longest_float;
5491                 r->float_utf8 = NULL;
5492             }
5493             /* float_end_shift is how many chars that must be matched that 
5494                follow this item. We calculate it ahead of time as once the
5495                lookbehind offset is added in we lose the ability to correctly
5496                calculate it.*/
5497             ml = data.minlen_float ? *(data.minlen_float) 
5498                                    : (I32)longest_float_length;
5499             r->float_end_shift = ml - data.offset_float_min
5500                 - longest_float_length + (SvTAIL(data.longest_float) != 0)
5501                 + data.lookbehind_float;
5502             r->float_min_offset = data.offset_float_min - data.lookbehind_float;
5503             r->float_max_offset = data.offset_float_max;
5504             if (data.offset_float_max < I32_MAX) /* Don't offset infinity */
5505                 r->float_max_offset -= data.lookbehind_float;
5506             
5507             t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
5508                        && (!(data.flags & SF_FL_BEFORE_MEOL)
5509                            || (RExC_flags & RXf_PMf_MULTILINE)));
5510             fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0);
5511         }
5512         else {
5513           remove_float:
5514             r->float_substr = r->float_utf8 = NULL;
5515             SvREFCNT_dec(data.longest_float);
5516             longest_float_length = 0;
5517         }
5518
5519         /* Note that code very similar to this but for floating string 
5520            is immediately above, changes may need to be made to both. 
5521            Be careful. 
5522          */
5523         longest_fixed_length = CHR_SVLEN(data.longest_fixed);
5524
5525         /* See comments for join_exact for why REG_SEEN_EXACTF_SHARP_S */
5526         if (! (RExC_seen & REG_SEEN_EXACTF_SHARP_S)
5527             && (longest_fixed_length
5528                 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
5529                     && (!(data.flags & SF_FIX_BEFORE_MEOL)
5530                         || (RExC_flags & RXf_PMf_MULTILINE)))) )
5531         {
5532             I32 t,ml;
5533
5534             /* copy the information about the longest fixed 
5535                from the reg_scan_data over to the program. */
5536             if (SvUTF8(data.longest_fixed)) {
5537                 r->anchored_utf8 = data.longest_fixed;
5538                 r->anchored_substr = NULL;
5539             } else {
5540                 r->anchored_substr = data.longest_fixed;
5541                 r->anchored_utf8 = NULL;
5542             }
5543             /* fixed_end_shift is how many chars that must be matched that 
5544                follow this item. We calculate it ahead of time as once the
5545                lookbehind offset is added in we lose the ability to correctly
5546                calculate it.*/
5547             ml = data.minlen_fixed ? *(data.minlen_fixed) 
5548                                    : (I32)longest_fixed_length;
5549             r->anchored_end_shift = ml - data.offset_fixed
5550                 - longest_fixed_length + (SvTAIL(data.longest_fixed) != 0)
5551                 + data.lookbehind_fixed;
5552             r->anchored_offset = data.offset_fixed - data.lookbehind_fixed;
5553
5554             t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
5555                  && (!(data.flags & SF_FIX_BEFORE_MEOL)
5556                      || (RExC_flags & RXf_PMf_MULTILINE)));
5557             fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0);
5558         }
5559         else {
5560             r->anchored_substr = r->anchored_utf8 = NULL;
5561             SvREFCNT_dec(data.longest_fixed);
5562             longest_fixed_length = 0;
5563         }
5564         if (ri->regstclass
5565             && (OP(ri->regstclass) == REG_ANY || OP(ri->regstclass) == SANY))
5566             ri->regstclass = NULL;
5567
5568         if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset)
5569             && stclass_flag
5570             && !(data.start_class->flags & ANYOF_EOS)
5571             && !cl_is_anything(data.start_class))
5572         {
5573             const U32 n = add_data(pRExC_state, 1, "f");
5574             data.start_class->flags |= ANYOF_IS_SYNTHETIC;
5575
5576             Newx(RExC_rxi->data->data[n], 1,
5577                 struct regnode_charclass_class);
5578             StructCopy(data.start_class,
5579                        (struct regnode_charclass_class*)RExC_rxi->data->data[n],
5580                        struct regnode_charclass_class);
5581             ri->regstclass = (regnode*)RExC_rxi->data->data[n];
5582             r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
5583             DEBUG_COMPILE_r({ SV *sv = sv_newmortal();
5584                       regprop(r, sv, (regnode*)data.start_class);
5585                       PerlIO_printf(Perl_debug_log,
5586                                     "synthetic stclass \"%s\".\n",
5587                                     SvPVX_const(sv));});
5588         }
5589
5590         /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
5591         if (longest_fixed_length > longest_float_length) {
5592             r->check_end_shift = r->anchored_end_shift;
5593             r->check_substr = r->anchored_substr;
5594             r->check_utf8 = r->anchored_utf8;
5595             r->check_offset_min = r->check_offset_max = r->anchored_offset;
5596             if (r->extflags & RXf_ANCH_SINGLE)
5597                 r->extflags |= RXf_NOSCAN;
5598         }
5599         else {
5600             r->check_end_shift = r->float_end_shift;
5601             r->check_substr = r->float_substr;
5602             r->check_utf8 = r->float_utf8;
5603             r->check_offset_min = r->float_min_offset;
5604             r->check_offset_max = r->float_max_offset;
5605         }
5606         /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
5607            This should be changed ASAP!  */
5608         if ((r->check_substr || r->check_utf8) && !(r->extflags & RXf_ANCH_GPOS)) {
5609             r->extflags |= RXf_USE_INTUIT;
5610             if (SvTAIL(r->check_substr ? r->check_substr : r->check_utf8))
5611                 r->extflags |= RXf_INTUIT_TAIL;
5612         }
5613         /* XXX Unneeded? dmq (shouldn't as this is handled elsewhere)
5614         if ( (STRLEN)minlen < longest_float_length )
5615             minlen= longest_float_length;
5616         if ( (STRLEN)minlen < longest_fixed_length )
5617             minlen= longest_fixed_length;     
5618         */
5619     }
5620     else {
5621         /* Several toplevels. Best we can is to set minlen. */
5622         I32 fake;
5623         struct regnode_charclass_class ch_class;
5624         I32 last_close = 0;
5625
5626         DEBUG_PARSE_r(PerlIO_printf(Perl_debug_log, "\nMulti Top Level\n"));
5627
5628         scan = ri->program + 1;
5629         cl_init(pRExC_state, &ch_class);
5630         data.start_class = &ch_class;
5631         data.last_closep = &last_close;
5632
5633         
5634         minlen = study_chunk(pRExC_state, &scan, &minlen, &fake, scan + RExC_size,
5635             &data, -1, NULL, NULL, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS,0);
5636         
5637         CHECK_RESTUDY_GOTO;
5638
5639         r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8
5640                 = r->float_substr = r->float_utf8 = NULL;
5641
5642         if (!(data.start_class->flags & ANYOF_EOS)
5643             && !cl_is_anything(data.start_class))
5644         {
5645             const U32 n = add_data(pRExC_state, 1, "f");
5646             data.start_class->flags |= ANYOF_IS_SYNTHETIC;
5647
5648             Newx(RExC_rxi->data->data[n], 1,
5649                 struct regnode_charclass_class);
5650             StructCopy(data.start_class,
5651                        (struct regnode_charclass_class*)RExC_rxi->data->data[n],
5652                        struct regnode_charclass_class);
5653             ri->regstclass = (regnode*)RExC_rxi->data->data[n];
5654             r->intflags &= ~PREGf_SKIP; /* Used in find_byclass(). */
5655             DEBUG_COMPILE_r({ SV* sv = sv_newmortal();
5656                       regprop(r, sv, (regnode*)data.start_class);
5657                       PerlIO_printf(Perl_debug_log,
5658                                     "synthetic stclass \"%s\".\n",
5659                                     SvPVX_const(sv));});
5660         }
5661     }
5662
5663     /* Guard against an embedded (?=) or (?<=) with a longer minlen than
5664        the "real" pattern. */
5665     DEBUG_OPTIMISE_r({
5666         PerlIO_printf(Perl_debug_log,"minlen: %"IVdf" r->minlen:%"IVdf"\n",
5667                       (IV)minlen, (IV)r->minlen);
5668     });
5669     r->minlenret = minlen;
5670     if (r->minlen < minlen) 
5671         r->minlen = minlen;
5672     
5673     if (RExC_seen & REG_SEEN_GPOS)
5674         r->extflags |= RXf_GPOS_SEEN;
5675     if (RExC_seen & REG_SEEN_LOOKBEHIND)
5676         r->extflags |= RXf_LOOKBEHIND_SEEN;
5677     if (RExC_seen & REG_SEEN_EVAL)
5678         r->extflags |= RXf_EVAL_SEEN;
5679     if (RExC_seen & REG_SEEN_CANY)
5680         r->extflags |= RXf_CANY_SEEN;
5681     if (RExC_seen & REG_SEEN_VERBARG)
5682         r->intflags |= PREGf_VERBARG_SEEN;
5683     if (RExC_seen & REG_SEEN_CUTGROUP)
5684         r->intflags |= PREGf_CUTGROUP_SEEN;
5685     if (RExC_paren_names)
5686         RXp_PAREN_NAMES(r) = MUTABLE_HV(SvREFCNT_inc(RExC_paren_names));
5687     else
5688         RXp_PAREN_NAMES(r) = NULL;
5689
5690 #ifdef STUPID_PATTERN_CHECKS            
5691     if (RX_PRELEN(rx) == 0)
5692         r->extflags |= RXf_NULL;
5693     if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ')
5694         /* XXX: this should happen BEFORE we compile */
5695         r->extflags |= (RXf_SKIPWHITE|RXf_WHITE); 
5696     else if (RX_PRELEN(rx) == 3 && memEQ("\\s+", RX_PRECOMP(rx), 3))
5697         r->extflags |= RXf_WHITE;
5698     else if (RX_PRELEN(rx) == 1 && RXp_PRECOMP(rx)[0] == '^')
5699         r->extflags |= RXf_START_ONLY;
5700 #else
5701     if (r->extflags & RXf_SPLIT && RX_PRELEN(rx) == 1 && RX_PRECOMP(rx)[0] == ' ')
5702             /* XXX: this should happen BEFORE we compile */
5703             r->extflags |= (RXf_SKIPWHITE|RXf_WHITE); 
5704     else {
5705         regnode *first = ri->program + 1;
5706         U8 fop = OP(first);
5707
5708         if (PL_regkind[fop] == NOTHING && OP(NEXTOPER(first)) == END)
5709             r->extflags |= RXf_NULL;
5710         else if (PL_regkind[fop] == BOL && OP(NEXTOPER(first)) == END)
5711             r->extflags |= RXf_START_ONLY;
5712         else if (fop == PLUS && OP(NEXTOPER(first)) == SPACE
5713                              && OP(regnext(first)) == END)
5714             r->extflags |= RXf_WHITE;    
5715     }
5716 #endif
5717 #ifdef DEBUGGING
5718     if (RExC_paren_names) {
5719         ri->name_list_idx = add_data( pRExC_state, 1, "a" );
5720         ri->data->data[ri->name_list_idx] = (void*)SvREFCNT_inc(RExC_paren_name_list);
5721     } else
5722 #endif
5723         ri->name_list_idx = 0;
5724
5725     if (RExC_recurse_count) {
5726         for ( ; RExC_recurse_count ; RExC_recurse_count-- ) {
5727             const regnode *scan = RExC_recurse[RExC_recurse_count-1];
5728             ARG2L_SET( scan, RExC_open_parens[ARG(scan)-1] - scan );
5729         }
5730     }
5731     Newxz(r->offs, RExC_npar, regexp_paren_pair);
5732     /* assume we don't need to swap parens around before we match */
5733
5734     DEBUG_DUMP_r({
5735         PerlIO_printf(Perl_debug_log,"Final program:\n");
5736         regdump(r);
5737     });
5738 #ifdef RE_TRACK_PATTERN_OFFSETS
5739     DEBUG_OFFSETS_r(if (ri->u.offsets) {
5740         const U32 len = ri->u.offsets[0];
5741         U32 i;
5742         GET_RE_DEBUG_FLAGS_DECL;
5743         PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)ri->u.offsets[0]);
5744         for (i = 1; i <= len; i++) {
5745             if (ri->u.offsets[i*2-1] || ri->u.offsets[i*2])
5746                 PerlIO_printf(Perl_debug_log, "%"UVuf":%"UVuf"[%"UVuf"] ",
5747                 (UV)i, (UV)ri->u.offsets[i*2-1], (UV)ri->u.offsets[i*2]);
5748             }
5749         PerlIO_printf(Perl_debug_log, "\n");
5750     });
5751 #endif
5752     return rx;
5753 }
5754
5755 #undef RE_ENGINE_PTR
5756
5757
5758 SV*
5759 Perl_reg_named_buff(pTHX_ REGEXP * const rx, SV * const key, SV * const value,
5760                     const U32 flags)
5761 {
5762     PERL_ARGS_ASSERT_REG_NAMED_BUFF;
5763
5764     PERL_UNUSED_ARG(value);
5765
5766     if (flags & RXapif_FETCH) {
5767         return reg_named_buff_fetch(rx, key, flags);
5768     } else if (flags & (RXapif_STORE | RXapif_DELETE | RXapif_CLEAR)) {
5769         Perl_croak_no_modify(aTHX);
5770         return NULL;
5771     } else if (flags & RXapif_EXISTS) {
5772         return reg_named_buff_exists(rx, key, flags)
5773             ? &PL_sv_yes
5774             : &PL_sv_no;
5775     } else if (flags & RXapif_REGNAMES) {
5776         return reg_named_buff_all(rx, flags);
5777     } else if (flags & (RXapif_SCALAR | RXapif_REGNAMES_COUNT)) {
5778         return reg_named_buff_scalar(rx, flags);
5779     } else {
5780         Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff", (int)flags);
5781         return NULL;
5782     }
5783 }
5784
5785 SV*
5786 Perl_reg_named_buff_iter(pTHX_ REGEXP * const rx, const SV * const lastkey,
5787                          const U32 flags)
5788 {
5789     PERL_ARGS_ASSERT_REG_NAMED_BUFF_ITER;
5790     PERL_UNUSED_ARG(lastkey);
5791
5792     if (flags & RXapif_FIRSTKEY)
5793         return reg_named_buff_firstkey(rx, flags);
5794     else if (flags & RXapif_NEXTKEY)
5795         return reg_named_buff_nextkey(rx, flags);
5796     else {
5797         Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_iter", (int)flags);
5798         return NULL;
5799     }
5800 }
5801
5802 SV*
5803 Perl_reg_named_buff_fetch(pTHX_ REGEXP * const r, SV * const namesv,
5804                           const U32 flags)
5805 {
5806     AV *retarray = NULL;
5807     SV *ret;
5808     struct regexp *const rx = (struct regexp *)SvANY(r);
5809
5810     PERL_ARGS_ASSERT_REG_NAMED_BUFF_FETCH;
5811
5812     if (flags & RXapif_ALL)
5813         retarray=newAV();
5814
5815     if (rx && RXp_PAREN_NAMES(rx)) {
5816         HE *he_str = hv_fetch_ent( RXp_PAREN_NAMES(rx), namesv, 0, 0 );
5817         if (he_str) {
5818             IV i;
5819             SV* sv_dat=HeVAL(he_str);
5820             I32 *nums=(I32*)SvPVX(sv_dat);
5821             for ( i=0; i<SvIVX(sv_dat); i++ ) {
5822                 if ((I32)(rx->nparens) >= nums[i]
5823                     && rx->offs[nums[i]].start != -1
5824                     && rx->offs[nums[i]].end != -1)
5825                 {
5826                     ret = newSVpvs("");
5827                     CALLREG_NUMBUF_FETCH(r,nums[i],ret);
5828                     if (!retarray)
5829                         return ret;
5830                 } else {
5831                     if (retarray)
5832                         ret = newSVsv(&PL_sv_undef);
5833                 }
5834                 if (retarray)
5835                     av_push(retarray, ret);
5836             }
5837             if (retarray)
5838                 return newRV_noinc(MUTABLE_SV(retarray));
5839         }
5840     }
5841     return NULL;
5842 }
5843
5844 bool
5845 Perl_reg_named_buff_exists(pTHX_ REGEXP * const r, SV * const key,
5846                            const U32 flags)
5847 {
5848     struct regexp *const rx = (struct regexp *)SvANY(r);
5849
5850     PERL_ARGS_ASSERT_REG_NAMED_BUFF_EXISTS;
5851
5852     if (rx && RXp_PAREN_NAMES(rx)) {
5853         if (flags & RXapif_ALL) {
5854             return hv_exists_ent(RXp_PAREN_NAMES(rx), key, 0);
5855         } else {
5856             SV *sv = CALLREG_NAMED_BUFF_FETCH(r, key, flags);
5857             if (sv) {
5858                 SvREFCNT_dec(sv);
5859                 return TRUE;
5860             } else {
5861                 return FALSE;
5862             }
5863         }
5864     } else {
5865         return FALSE;
5866     }
5867 }
5868
5869 SV*
5870 Perl_reg_named_buff_firstkey(pTHX_ REGEXP * const r, const U32 flags)
5871 {
5872     struct regexp *const rx = (struct regexp *)SvANY(r);
5873
5874     PERL_ARGS_ASSERT_REG_NAMED_BUFF_FIRSTKEY;
5875
5876     if ( rx && RXp_PAREN_NAMES(rx) ) {
5877         (void)hv_iterinit(RXp_PAREN_NAMES(rx));
5878
5879         return CALLREG_NAMED_BUFF_NEXTKEY(r, NULL, flags & ~RXapif_FIRSTKEY);
5880     } else {
5881         return FALSE;
5882     }
5883 }
5884
5885 SV*
5886 Perl_reg_named_buff_nextkey(pTHX_ REGEXP * const r, const U32 flags)
5887 {
5888     struct regexp *const rx = (struct regexp *)SvANY(r);
5889     GET_RE_DEBUG_FLAGS_DECL;
5890
5891     PERL_ARGS_ASSERT_REG_NAMED_BUFF_NEXTKEY;
5892
5893     if (rx && RXp_PAREN_NAMES(rx)) {
5894         HV *hv = RXp_PAREN_NAMES(rx);
5895         HE *temphe;
5896         while ( (temphe = hv_iternext_flags(hv,0)) ) {
5897             IV i;
5898             IV parno = 0;
5899             SV* sv_dat = HeVAL(temphe);
5900             I32 *nums = (I32*)SvPVX(sv_dat);
5901             for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5902                 if ((I32)(rx->lastparen) >= nums[i] &&
5903                     rx->offs[nums[i]].start != -1 &&
5904                     rx->offs[nums[i]].end != -1)
5905                 {
5906                     parno = nums[i];
5907                     break;
5908                 }
5909             }
5910             if (parno || flags & RXapif_ALL) {
5911                 return newSVhek(HeKEY_hek(temphe));
5912             }
5913         }
5914     }
5915     return NULL;
5916 }
5917
5918 SV*
5919 Perl_reg_named_buff_scalar(pTHX_ REGEXP * const r, const U32 flags)
5920 {
5921     SV *ret;
5922     AV *av;
5923     I32 length;
5924     struct regexp *const rx = (struct regexp *)SvANY(r);
5925
5926     PERL_ARGS_ASSERT_REG_NAMED_BUFF_SCALAR;
5927
5928     if (rx && RXp_PAREN_NAMES(rx)) {
5929         if (flags & (RXapif_ALL | RXapif_REGNAMES_COUNT)) {
5930             return newSViv(HvTOTALKEYS(RXp_PAREN_NAMES(rx)));
5931         } else if (flags & RXapif_ONE) {
5932             ret = CALLREG_NAMED_BUFF_ALL(r, (flags | RXapif_REGNAMES));
5933             av = MUTABLE_AV(SvRV(ret));
5934             length = av_len(av);
5935             SvREFCNT_dec(ret);
5936             return newSViv(length + 1);
5937         } else {
5938             Perl_croak(aTHX_ "panic: Unknown flags %d in named_buff_scalar", (int)flags);
5939             return NULL;
5940         }
5941     }
5942     return &PL_sv_undef;
5943 }
5944
5945 SV*
5946 Perl_reg_named_buff_all(pTHX_ REGEXP * const r, const U32 flags)
5947 {
5948     struct regexp *const rx = (struct regexp *)SvANY(r);
5949     AV *av = newAV();
5950
5951     PERL_ARGS_ASSERT_REG_NAMED_BUFF_ALL;
5952
5953     if (rx && RXp_PAREN_NAMES(rx)) {
5954         HV *hv= RXp_PAREN_NAMES(rx);
5955         HE *temphe;
5956         (void)hv_iterinit(hv);
5957         while ( (temphe = hv_iternext_flags(hv,0)) ) {
5958             IV i;
5959             IV parno = 0;
5960             SV* sv_dat = HeVAL(temphe);
5961             I32 *nums = (I32*)SvPVX(sv_dat);
5962             for ( i = 0; i < SvIVX(sv_dat); i++ ) {
5963                 if ((I32)(rx->lastparen) >= nums[i] &&
5964                     rx->offs[nums[i]].start != -1 &&
5965                     rx->offs[nums[i]].end != -1)
5966                 {
5967                     parno = nums[i];
5968                     break;
5969                 }
5970             }
5971             if (parno || flags & RXapif_ALL) {
5972                 av_push(av, newSVhek(HeKEY_hek(temphe)));
5973             }
5974         }
5975     }
5976
5977     return newRV_noinc(MUTABLE_SV(av));
5978 }
5979
5980 void
5981 Perl_reg_numbered_buff_fetch(pTHX_ REGEXP * const r, const I32 paren,
5982                              SV * const sv)
5983 {
5984     struct regexp *const rx = (struct regexp *)SvANY(r);
5985     char *s = NULL;
5986     I32 i = 0;
5987     I32 s1, t1;
5988
5989     PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_FETCH;
5990         
5991     if (!rx->subbeg) {
5992         sv_setsv(sv,&PL_sv_undef);
5993         return;
5994     } 
5995     else               
5996     if (paren == RX_BUFF_IDX_PREMATCH && rx->offs[0].start != -1) {
5997         /* $` */
5998         i = rx->offs[0].start;
5999         s = rx->subbeg;
6000     }
6001     else 
6002     if (paren == RX_BUFF_IDX_POSTMATCH && rx->offs[0].end != -1) {
6003         /* $' */
6004         s = rx->subbeg + rx->offs[0].end;
6005         i = rx->sublen - rx->offs[0].end;
6006     } 
6007     else
6008     if ( 0 <= paren && paren <= (I32)rx->nparens &&
6009         (s1 = rx->offs[paren].start) != -1 &&
6010         (t1 = rx->offs[paren].end) != -1)
6011     {
6012         /* $& $1 ... */
6013         i = t1 - s1;
6014         s = rx->subbeg + s1;
6015     } else {
6016         sv_setsv(sv,&PL_sv_undef);
6017         return;
6018     }          
6019     assert(rx->sublen >= (s - rx->subbeg) + i );
6020     if (i >= 0) {
6021         const int oldtainted = PL_tainted;
6022         TAINT_NOT;
6023         sv_setpvn(sv, s, i);
6024         PL_tainted = oldtainted;
6025         if ( (rx->extflags & RXf_CANY_SEEN)
6026             ? (RXp_MATCH_UTF8(rx)
6027                         && (!i || is_utf8_string((U8*)s, i)))
6028             : (RXp_MATCH_UTF8(rx)) )
6029         {
6030             SvUTF8_on(sv);
6031         }
6032         else
6033             SvUTF8_off(sv);
6034         if (PL_tainting) {
6035             if (RXp_MATCH_TAINTED(rx)) {
6036                 if (SvTYPE(sv) >= SVt_PVMG) {
6037                     MAGIC* const mg = SvMAGIC(sv);
6038                     MAGIC* mgt;
6039                     PL_tainted = 1;
6040                     SvMAGIC_set(sv, mg->mg_moremagic);
6041                     SvTAINT(sv);
6042                     if ((mgt = SvMAGIC(sv))) {
6043                         mg->mg_moremagic = mgt;
6044                         SvMAGIC_set(sv, mg);
6045                     }
6046                 } else {
6047                     PL_tainted = 1;
6048                     SvTAINT(sv);
6049                 }
6050             } else 
6051                 SvTAINTED_off(sv);
6052         }
6053     } else {
6054         sv_setsv(sv,&PL_sv_undef);
6055         return;
6056     }
6057 }
6058
6059 void
6060 Perl_reg_numbered_buff_store(pTHX_ REGEXP * const rx, const I32 paren,
6061                                                          SV const * const value)
6062 {
6063     PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_STORE;
6064
6065     PERL_UNUSED_ARG(rx);
6066     PERL_UNUSED_ARG(paren);
6067     PERL_UNUSED_ARG(value);
6068
6069     if (!PL_localizing)
6070         Perl_croak_no_modify(aTHX);
6071 }
6072
6073 I32
6074 Perl_reg_numbered_buff_length(pTHX_ REGEXP * const r, const SV * const sv,
6075                               const I32 paren)
6076 {
6077     struct regexp *const rx = (struct regexp *)SvANY(r);
6078     I32 i;
6079     I32 s1, t1;
6080
6081     PERL_ARGS_ASSERT_REG_NUMBERED_BUFF_LENGTH;
6082
6083     /* Some of this code was originally in C<Perl_magic_len> in F<mg.c> */
6084         switch (paren) {
6085       /* $` / ${^PREMATCH} */
6086       case RX_BUFF_IDX_PREMATCH:
6087         if (rx->offs[0].start != -1) {
6088                         i = rx->offs[0].start;
6089                         if (i > 0) {
6090                                 s1 = 0;
6091                                 t1 = i;
6092                                 goto getlen;
6093                         }
6094             }
6095         return 0;
6096       /* $' / ${^POSTMATCH} */
6097       case RX_BUFF_IDX_POSTMATCH:
6098             if (rx->offs[0].end != -1) {
6099                         i = rx->sublen - rx->offs[0].end;
6100                         if (i > 0) {
6101                                 s1 = rx->offs[0].end;
6102                                 t1 = rx->sublen;
6103                                 goto getlen;
6104                         }
6105             }
6106         return 0;
6107       /* $& / ${^MATCH}, $1, $2, ... */
6108       default:
6109             if (paren <= (I32)rx->nparens &&
6110             (s1 = rx->offs[paren].start) != -1 &&
6111             (t1 = rx->offs[paren].end) != -1)
6112             {
6113             i = t1 - s1;
6114             goto getlen;
6115         } else {
6116             if (ckWARN(WARN_UNINITIALIZED))
6117                 report_uninit((const SV *)sv);
6118             return 0;
6119         }
6120     }
6121   getlen:
6122     if (i > 0 && RXp_MATCH_UTF8(rx)) {
6123         const char * const s = rx->subbeg + s1;
6124         const U8 *ep;
6125         STRLEN el;
6126
6127         i = t1 - s1;
6128         if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
6129                         i = el;
6130     }
6131     return i;
6132 }
6133
6134 SV*
6135 Perl_reg_qr_package(pTHX_ REGEXP * const rx)
6136 {
6137     PERL_ARGS_ASSERT_REG_QR_PACKAGE;
6138         PERL_UNUSED_ARG(rx);
6139         if (0)
6140             return NULL;
6141         else
6142             return newSVpvs("Regexp");
6143 }
6144
6145 /* Scans the name of a named buffer from the pattern.
6146  * If flags is REG_RSN_RETURN_NULL returns null.
6147  * If flags is REG_RSN_RETURN_NAME returns an SV* containing the name
6148  * If flags is REG_RSN_RETURN_DATA returns the data SV* corresponding
6149  * to the parsed name as looked up in the RExC_paren_names hash.
6150  * If there is an error throws a vFAIL().. type exception.
6151  */
6152
6153 #define REG_RSN_RETURN_NULL    0
6154 #define REG_RSN_RETURN_NAME    1
6155 #define REG_RSN_RETURN_DATA    2
6156
6157 STATIC SV*
6158 S_reg_scan_name(pTHX_ RExC_state_t *pRExC_state, U32 flags)
6159 {
6160     char *name_start = RExC_parse;
6161
6162     PERL_ARGS_ASSERT_REG_SCAN_NAME;
6163
6164     if (isIDFIRST_lazy_if(RExC_parse, UTF)) {
6165          /* skip IDFIRST by using do...while */
6166         if (UTF)
6167             do {
6168                 RExC_parse += UTF8SKIP(RExC_parse);
6169             } while (isALNUM_utf8((U8*)RExC_parse));
6170         else
6171             do {
6172                 RExC_parse++;
6173             } while (isALNUM(*RExC_parse));
6174     }
6175
6176     if ( flags ) {
6177         SV* sv_name
6178             = newSVpvn_flags(name_start, (int)(RExC_parse - name_start),
6179                              SVs_TEMP | (UTF ? SVf_UTF8 : 0));
6180         if ( flags == REG_RSN_RETURN_NAME)
6181             return sv_name;
6182         else if (flags==REG_RSN_RETURN_DATA) {
6183             HE *he_str = NULL;
6184             SV *sv_dat = NULL;
6185             if ( ! sv_name )      /* should not happen*/
6186                 Perl_croak(aTHX_ "panic: no svname in reg_scan_name");
6187             if (RExC_paren_names)
6188                 he_str = hv_fetch_ent( RExC_paren_names, sv_name, 0, 0 );
6189             if ( he_str )
6190                 sv_dat = HeVAL(he_str);
6191             if ( ! sv_dat )
6192                 vFAIL("Reference to nonexistent named group");
6193             return sv_dat;
6194         }
6195         else {
6196             Perl_croak(aTHX_ "panic: bad flag %lx in reg_scan_name",
6197                        (unsigned long) flags);
6198         }
6199         /* NOT REACHED */
6200     }
6201     return NULL;
6202 }
6203
6204 #define DEBUG_PARSE_MSG(funcname)     DEBUG_PARSE_r({           \
6205     int rem=(int)(RExC_end - RExC_parse);                       \
6206     int cut;                                                    \
6207     int num;                                                    \
6208     int iscut=0;                                                \
6209     if (rem>10) {                                               \
6210         rem=10;                                                 \
6211         iscut=1;                                                \
6212     }                                                           \
6213     cut=10-rem;                                                 \
6214     if (RExC_lastparse!=RExC_parse)                             \
6215         PerlIO_printf(Perl_debug_log," >%.*s%-*s",              \
6216             rem, RExC_parse,                                    \
6217             cut + 4,                                            \
6218             iscut ? "..." : "<"                                 \
6219         );                                                      \
6220     else                                                        \
6221         PerlIO_printf(Perl_debug_log,"%16s","");                \
6222                                                                 \
6223     if (SIZE_ONLY)                                              \
6224        num = RExC_size + 1;                                     \
6225     else                                                        \
6226        num=REG_NODE_NUM(RExC_emit);                             \
6227     if (RExC_lastnum!=num)                                      \
6228        PerlIO_printf(Perl_debug_log,"|%4d",num);                \
6229     else                                                        \
6230        PerlIO_printf(Perl_debug_log,"|%4s","");                 \
6231     PerlIO_printf(Perl_debug_log,"|%*s%-4s",                    \
6232         (int)((depth*2)), "",                                   \
6233         (funcname)                                              \
6234     );                                                          \
6235     RExC_lastnum=num;                                           \
6236     RExC_lastparse=RExC_parse;                                  \
6237 })
6238
6239
6240
6241 #define DEBUG_PARSE(funcname)     DEBUG_PARSE_r({           \
6242     DEBUG_PARSE_MSG((funcname));                            \
6243     PerlIO_printf(Perl_debug_log,"%4s","\n");               \
6244 })
6245 #define DEBUG_PARSE_FMT(funcname,fmt,args)     DEBUG_PARSE_r({           \
6246     DEBUG_PARSE_MSG((funcname));                            \
6247     PerlIO_printf(Perl_debug_log,fmt "\n",args);               \
6248 })
6249
6250 /* This section of code defines the inversion list object and its methods.  The
6251  * interfaces are highly subject to change, so as much as possible is static to
6252  * this file.  An inversion list is here implemented as a malloc'd C UV array
6253  * with some added info that is placed as UVs at the beginning in a header
6254  * portion.  An inversion list for Unicode is an array of code points, sorted
6255  * by ordinal number.  The zeroth element is the first code point in the list.
6256  * The 1th element is the first element beyond that not in the list.  In other
6257  * words, the first range is
6258  *  invlist[0]..(invlist[1]-1)
6259  * The other ranges follow.  Thus every element whose index is divisible by two
6260  * marks the beginning of a range that is in the list, and every element not
6261  * divisible by two marks the beginning of a range not in the list.  A single
6262  * element inversion list that contains the single code point N generally
6263  * consists of two elements
6264  *  invlist[0] == N
6265  *  invlist[1] == N+1
6266  * (The exception is when N is the highest representable value on the
6267  * machine, in which case the list containing just it would be a single
6268  * element, itself.  By extension, if the last range in the list extends to
6269  * infinity, then the first element of that range will be in the inversion list
6270  * at a position that is divisible by two, and is the final element in the
6271  * list.)
6272  * Taking the complement (inverting) an inversion list is quite simple, if the
6273  * first element is 0, remove it; otherwise add a 0 element at the beginning.
6274  * This implementation reserves an element at the beginning of each inversion list
6275  * to contain 0 when the list contains 0, and contains 1 otherwise.  The actual
6276  * beginning of the list is either that element if 0, or the next one if 1.
6277  *
6278  * More about inversion lists can be found in "Unicode Demystified"
6279  * Chapter 13 by Richard Gillam, published by Addison-Wesley.
6280  * More will be coming when functionality is added later.
6281  *
6282  * The inversion list data structure is currently implemented as an SV pointing
6283  * to an array of UVs that the SV thinks are bytes.  This allows us to have an
6284  * array of UV whose memory management is automatically handled by the existing
6285  * facilities for SV's.
6286  *
6287  * Some of the methods should always be private to the implementation, and some
6288  * should eventually be made public */
6289
6290 #define INVLIST_LEN_OFFSET 0    /* Number of elements in the inversion list */
6291 #define INVLIST_ITER_OFFSET 1   /* Current iteration position */
6292
6293 /* This is a combination of a version and data structure type, so that one
6294  * being passed in can be validated to be an inversion list of the correct
6295  * vintage.  When the structure of the header is changed, a new random number
6296  * in the range 2**31-1 should be generated and the new() method changed to
6297  * insert that at this location.  Then, if an auxiliary program doesn't change
6298  * correspondingly, it will be discovered immediately */
6299 #define INVLIST_VERSION_ID_OFFSET 2
6300 #define INVLIST_VERSION_ID 1064334010
6301
6302 /* For safety, when adding new elements, remember to #undef them at the end of
6303  * the inversion list code section */
6304
6305 #define INVLIST_ZERO_OFFSET 3   /* 0 or 1; must be last element in header */
6306 /* The UV at position ZERO contains either 0 or 1.  If 0, the inversion list
6307  * contains the code point U+00000, and begins here.  If 1, the inversion list
6308  * doesn't contain U+0000, and it begins at the next UV in the array.
6309  * Inverting an inversion list consists of adding or removing the 0 at the
6310  * beginning of it.  By reserving a space for that 0, inversion can be made
6311  * very fast */
6312
6313 #define HEADER_LENGTH (INVLIST_ZERO_OFFSET + 1)
6314
6315 /* Internally things are UVs */
6316 #define TO_INTERNAL_SIZE(x) ((x + HEADER_LENGTH) * sizeof(UV))
6317 #define FROM_INTERNAL_SIZE(x) ((x / sizeof(UV)) - HEADER_LENGTH)
6318
6319 #define INVLIST_INITIAL_LEN 10
6320
6321 PERL_STATIC_INLINE UV*
6322 S__invlist_array_init(pTHX_ SV* const invlist, const bool will_have_0)
6323 {
6324     /* Returns a pointer to the first element in the inversion list's array.
6325      * This is called upon initialization of an inversion list.  Where the
6326      * array begins depends on whether the list has the code point U+0000
6327      * in it or not.  The other parameter tells it whether the code that
6328      * follows this call is about to put a 0 in the inversion list or not.
6329      * The first element is either the element with 0, if 0, or the next one,
6330      * if 1 */
6331
6332     UV* zero = get_invlist_zero_addr(invlist);
6333
6334     PERL_ARGS_ASSERT__INVLIST_ARRAY_INIT;
6335
6336     /* Must be empty */
6337     assert(! *get_invlist_len_addr(invlist));
6338
6339     /* 1^1 = 0; 1^0 = 1 */
6340     *zero = 1 ^ will_have_0;
6341     return zero + *zero;
6342 }
6343
6344 PERL_STATIC_INLINE UV*
6345 S_invlist_array(pTHX_ SV* const invlist)
6346 {
6347     /* Returns the pointer to the inversion list's array.  Every time the
6348      * length changes, this needs to be called in case malloc or realloc moved
6349      * it */
6350
6351     PERL_ARGS_ASSERT_INVLIST_ARRAY;
6352
6353     /* Must not be empty.  If these fail, you probably didn't check for <len>
6354      * being non-zero before trying to get the array */
6355     assert(*get_invlist_len_addr(invlist));
6356     assert(*get_invlist_zero_addr(invlist) == 0
6357            || *get_invlist_zero_addr(invlist) == 1);
6358
6359     /* The array begins either at the element reserved for zero if the
6360      * list contains 0 (that element will be set to 0), or otherwise the next
6361      * element (in which case the reserved element will be set to 1). */
6362     return (UV *) (get_invlist_zero_addr(invlist)
6363                    + *get_invlist_zero_addr(invlist));
6364 }
6365
6366 PERL_STATIC_INLINE UV*
6367 S_get_invlist_len_addr(pTHX_ SV* invlist)
6368 {
6369     /* Return the address of the UV that contains the current number
6370      * of used elements in the inversion list */
6371
6372     PERL_ARGS_ASSERT_GET_INVLIST_LEN_ADDR;
6373
6374     return (UV *) (SvPVX(invlist) + (INVLIST_LEN_OFFSET * sizeof (UV)));
6375 }
6376
6377 PERL_STATIC_INLINE UV
6378 S_invlist_len(pTHX_ SV* const invlist)
6379 {
6380     /* Returns the current number of elements stored in the inversion list's
6381      * array */
6382
6383     PERL_ARGS_ASSERT_INVLIST_LEN;
6384
6385     return *get_invlist_len_addr(invlist);
6386 }
6387
6388 PERL_STATIC_INLINE void
6389 S_invlist_set_len(pTHX_ SV* const invlist, const UV len)
6390 {
6391     /* Sets the current number of elements stored in the inversion list */
6392
6393     PERL_ARGS_ASSERT_INVLIST_SET_LEN;
6394
6395     *get_invlist_len_addr(invlist) = len;
6396
6397     assert(len <= SvLEN(invlist));
6398
6399     SvCUR_set(invlist, TO_INTERNAL_SIZE(len));
6400     /* If the list contains U+0000, that element is part of the header,
6401      * and should not be counted as part of the array.  It will contain
6402      * 0 in that case, and 1 otherwise.  So we could flop 0=>1, 1=>0 and
6403      * subtract:
6404      *  SvCUR_set(invlist,
6405      *            TO_INTERNAL_SIZE(len
6406      *                             - (*get_invlist_zero_addr(inv_list) ^ 1)));
6407      * But, this is only valid if len is not 0.  The consequences of not doing
6408      * this is that the memory allocation code may think that 1 more UV is
6409      * being used than actually is, and so might do an unnecessary grow.  That
6410      * seems worth not bothering to make this the precise amount.
6411      *
6412      * Note that when inverting, SvCUR shouldn't change */
6413 }
6414
6415 PERL_STATIC_INLINE UV
6416 S_invlist_max(pTHX_ SV* const invlist)
6417 {
6418     /* Returns the maximum number of elements storable in the inversion list's
6419      * array, without having to realloc() */
6420
6421     PERL_ARGS_ASSERT_INVLIST_MAX;
6422
6423     return FROM_INTERNAL_SIZE(SvLEN(invlist));
6424 }
6425
6426 PERL_STATIC_INLINE UV*
6427 S_get_invlist_zero_addr(pTHX_ SV* invlist)
6428 {
6429     /* Return the address of the UV that is reserved to hold 0 if the inversion
6430      * list contains 0.  This has to be the last element of the heading, as the
6431      * list proper starts with either it if 0, or the next element if not.
6432      * (But we force it to contain either 0 or 1) */
6433
6434     PERL_ARGS_ASSERT_GET_INVLIST_ZERO_ADDR;
6435
6436     return (UV *) (SvPVX(invlist) + (INVLIST_ZERO_OFFSET * sizeof (UV)));
6437 }
6438
6439 #ifndef PERL_IN_XSUB_RE
6440 SV*
6441 Perl__new_invlist(pTHX_ IV initial_size)
6442 {
6443
6444     /* Return a pointer to a newly constructed inversion list, with enough
6445      * space to store 'initial_size' elements.  If that number is negative, a
6446      * system default is used instead */
6447
6448     SV* new_list;
6449
6450     if (initial_size < 0) {
6451         initial_size = INVLIST_INITIAL_LEN;
6452     }
6453
6454     /* Allocate the initial space */
6455     new_list = newSV(TO_INTERNAL_SIZE(initial_size));
6456     invlist_set_len(new_list, 0);
6457
6458     /* Force iterinit() to be used to get iteration to work */
6459     *get_invlist_iter_addr(new_list) = UV_MAX;
6460
6461     /* This should force a segfault if a method doesn't initialize this
6462      * properly */
6463     *get_invlist_zero_addr(new_list) = UV_MAX;
6464
6465     *get_invlist_version_id_addr(new_list) = INVLIST_VERSION_ID;
6466 #if HEADER_LENGTH != 4
6467 #   error Need to regenerate VERSION_ID by running perl -E 'say int(rand 2**31-1)', and then changing the #if to the new length
6468 #endif
6469
6470     return new_list;
6471 }
6472 #endif
6473
6474 STATIC SV*
6475 S__new_invlist_C_array(pTHX_ UV* list)
6476 {
6477     /* Return a pointer to a newly constructed inversion list, initialized to
6478      * point to <list>, which has to be in the exact correct inversion list
6479      * form, including internal fields.  Thus this is a dangerous routine that
6480      * should not be used in the wrong hands */
6481
6482     SV* invlist = newSV_type(SVt_PV);
6483
6484     PERL_ARGS_ASSERT__NEW_INVLIST_C_ARRAY;
6485
6486     SvPV_set(invlist, (char *) list);
6487     SvLEN_set(invlist, 0);  /* Means we own the contents, and the system
6488                                shouldn't touch it */
6489     SvCUR_set(invlist, TO_INTERNAL_SIZE(invlist_len(invlist)));
6490
6491     if (*get_invlist_version_id_addr(invlist) != INVLIST_VERSION_ID) {
6492         Perl_croak(aTHX_ "panic: Incorrect version for previously generated inversion list");
6493     }
6494
6495     return invlist;
6496 }
6497
6498 STATIC void
6499 S_invlist_extend(pTHX_ SV* const invlist, const UV new_max)
6500 {
6501     /* Grow the maximum size of an inversion list */
6502
6503     PERL_ARGS_ASSERT_INVLIST_EXTEND;
6504
6505     SvGROW((SV *)invlist, TO_INTERNAL_SIZE(new_max));
6506 }
6507
6508 PERL_STATIC_INLINE void
6509 S_invlist_trim(pTHX_ SV* const invlist)
6510 {
6511     PERL_ARGS_ASSERT_INVLIST_TRIM;
6512
6513     /* Change the length of the inversion list to how many entries it currently
6514      * has */
6515
6516     SvPV_shrink_to_cur((SV *) invlist);
6517 }
6518
6519 /* An element is in an inversion list iff its index is even numbered: 0, 2, 4,
6520  * etc */
6521 #define ELEMENT_RANGE_MATCHES_INVLIST(i) (! ((i) & 1))
6522 #define PREV_RANGE_MATCHES_INVLIST(i) (! ELEMENT_RANGE_MATCHES_INVLIST(i))
6523
6524 #define _invlist_union_complement_2nd(a, b, output) _invlist_union_maybe_complement_2nd(a, b, TRUE, output)
6525
6526 STATIC void
6527 S__append_range_to_invlist(pTHX_ SV* const invlist, const UV start, const UV end)
6528 {
6529    /* Subject to change or removal.  Append the range from 'start' to 'end' at
6530     * the end of the inversion list.  The range must be above any existing
6531     * ones. */
6532
6533     UV* array;
6534     UV max = invlist_max(invlist);
6535     UV len = invlist_len(invlist);
6536
6537     PERL_ARGS_ASSERT__APPEND_RANGE_TO_INVLIST;
6538
6539     if (len == 0) { /* Empty lists must be initialized */
6540         array = _invlist_array_init(invlist, start == 0);
6541     }
6542     else {
6543         /* Here, the existing list is non-empty. The current max entry in the
6544          * list is generally the first value not in the set, except when the
6545          * set extends to the end of permissible values, in which case it is
6546          * the first entry in that final set, and so this call is an attempt to
6547          * append out-of-order */
6548
6549         UV final_element = len - 1;
6550         array = invlist_array(invlist);
6551         if (array[final_element] > start
6552             || ELEMENT_RANGE_MATCHES_INVLIST(final_element))
6553         {
6554             Perl_croak(aTHX_ "panic: attempting to append to an inversion list, but wasn't at the end of the list, final=%"UVuf", start=%"UVuf", match=%c",
6555                        array[final_element], start,
6556                        ELEMENT_RANGE_MATCHES_INVLIST(final_element) ? 't' : 'f');
6557         }
6558
6559         /* Here, it is a legal append.  If the new range begins with the first
6560          * value not in the set, it is extending the set, so the new first
6561          * value not in the set is one greater than the newly extended range.
6562          * */
6563         if (array[final_element] == start) {
6564             if (end != UV_MAX) {
6565                 array[final_element] = end + 1;
6566             }
6567             else {
6568                 /* But if the end is the maximum representable on the machine,
6569                  * just let the range that this would extend to have no end */
6570                 invlist_set_len(invlist, len - 1);
6571             }
6572             return;
6573         }
6574     }
6575
6576     /* Here the new range doesn't extend any existing set.  Add it */
6577
6578     len += 2;   /* Includes an element each for the start and end of range */
6579
6580     /* If overflows the existing space, extend, which may cause the array to be
6581      * moved */
6582     if (max < len) {
6583         invlist_extend(invlist, len);
6584         invlist_set_len(invlist, len);  /* Have to set len here to avoid assert
6585                                            failure in invlist_array() */
6586         array = invlist_array(invlist);
6587     }
6588     else {
6589         invlist_set_len(invlist, len);
6590     }
6591
6592     /* The next item on the list starts the range, the one after that is
6593      * one past the new range.  */
6594     array[len - 2] = start;
6595     if (end != UV_MAX) {
6596         array[len - 1] = end + 1;
6597     }
6598     else {
6599         /* But if the end is the maximum representable on the machine, just let
6600          * the range have no end */
6601         invlist_set_len(invlist, len - 1);
6602     }
6603 }
6604
6605 #ifndef PERL_IN_XSUB_RE
6606
6607 STATIC IV
6608 S_invlist_search(pTHX_ SV* const invlist, const UV cp)
6609 {
6610     /* Searches the inversion list for the entry that contains the input code
6611      * point <cp>.  If <cp> is not in the list, -1 is returned.  Otherwise, the
6612      * return value is the index into the list's array of the range that
6613      * contains <cp> */
6614
6615     IV low = 0;
6616     IV high = invlist_len(invlist);
6617     const UV * const array = invlist_array(invlist);
6618
6619     PERL_ARGS_ASSERT_INVLIST_SEARCH;
6620
6621     /* If list is empty or the code point is before the first element, return
6622      * failure. */
6623     if (high == 0 || cp < array[0]) {
6624         return -1;
6625     }
6626
6627     /* Binary search.  What we are looking for is <i> such that
6628      *  array[i] <= cp < array[i+1]
6629      * The loop below converges on the i+1. */
6630     while (low < high) {
6631         IV mid = (low + high) / 2;
6632         if (array[mid] <= cp) {
6633             low = mid + 1;
6634
6635             /* We could do this extra test to exit the loop early.
6636             if (cp < array[low]) {
6637                 return mid;
6638             }
6639             */
6640         }
6641         else { /* cp < array[mid] */
6642             high = mid;
6643         }
6644     }
6645
6646     return high - 1;
6647 }
6648
6649 void
6650 Perl__invlist_populate_swatch(pTHX_ SV* const invlist, const UV start, const UV end, U8* swatch)
6651 {
6652     /* populates a swatch of a swash the same way swatch_get() does in utf8.c,
6653      * but is used when the swash has an inversion list.  This makes this much
6654      * faster, as it uses a binary search instead of a linear one.  This is
6655      * intimately tied to that function, and perhaps should be in utf8.c,
6656      * except it is intimately tied to inversion lists as well.  It assumes
6657      * that <swatch> is all 0's on input */
6658
6659     UV current = start;
6660     const IV len = invlist_len(invlist);
6661     IV i;
6662     const UV * array;
6663
6664     PERL_ARGS_ASSERT__INVLIST_POPULATE_SWATCH;
6665
6666     if (len == 0) { /* Empty inversion list */
6667         return;
6668     }
6669
6670     array = invlist_array(invlist);
6671
6672     /* Find which element it is */
6673     i = invlist_search(invlist, start);
6674
6675     /* We populate from <start> to <end> */
6676     while (current < end) {
6677         UV upper;
6678
6679         /* The inversion list gives the results for every possible code point
6680          * after the first one in the list.  Only those ranges whose index is
6681          * even are ones that the inversion list matches.  For the odd ones,
6682          * and if the initial code point is not in the list, we have to skip
6683          * forward to the next element */
6684         if (i == -1 || ! ELEMENT_RANGE_MATCHES_INVLIST(i)) {
6685             i++;
6686             if (i >= len) { /* Finished if beyond the end of the array */
6687                 return;
6688             }
6689             current = array[i];
6690             if (current >= end) {   /* Finished if beyond the end of what we
6691                                        are populating */
6692                 return;
6693             }
6694         }
6695         assert(current >= start);
6696
6697         /* The current range ends one below the next one, except don't go past
6698          * <end> */
6699         i++;
6700         upper = (i < len && array[i] < end) ? array[i] : end;
6701
6702         /* Here we are in a range that matches.  Populate a bit in the 3-bit U8
6703          * for each code point in it */
6704         for (; current < upper; current++) {
6705             const STRLEN offset = (STRLEN)(current - start);
6706             swatch[offset >> 3] |= 1 << (offset & 7);
6707         }
6708
6709         /* Quit if at the end of the list */
6710         if (i >= len) {
6711
6712             /* But first, have to deal with the highest possible code point on
6713              * the platform.  The previous code assumes that <end> is one
6714              * beyond where we want to populate, but that is impossible at the
6715              * platform's infinity, so have to handle it specially */
6716             if (UNLIKELY(end == UV_MAX && ELEMENT_RANGE_MATCHES_INVLIST(len-1)))
6717             {
6718                 const STRLEN offset = (STRLEN)(end - start);
6719                 swatch[offset >> 3] |= 1 << (offset & 7);
6720             }
6721             return;
6722         }
6723
6724         /* Advance to the next range, which will be for code points not in the
6725          * inversion list */
6726         current = array[i];
6727     }
6728
6729     return;
6730 }
6731
6732
6733 void
6734 Perl__invlist_union_maybe_complement_2nd(pTHX_ SV* const a, SV* const b, bool complement_b, SV** output)
6735 {
6736     /* Take the union of two inversion lists and point <output> to it.  *output
6737      * should be defined upon input, and if it points to one of the two lists,
6738      * the reference count to that list will be decremented.  The first list,
6739      * <a>, may be NULL, in which case a copy of the second list is returned.
6740      * If <complement_b> is TRUE, the union is taken of the complement
6741      * (inversion) of <b> instead of b itself.
6742      *
6743      * The basis for this comes from "Unicode Demystified" Chapter 13 by
6744      * Richard Gillam, published by Addison-Wesley, and explained at some
6745      * length there.  The preface says to incorporate its examples into your
6746      * code at your own risk.
6747      *
6748      * The algorithm is like a merge sort.
6749      *
6750      * XXX A potential performance improvement is to keep track as we go along
6751      * if only one of the inputs contributes to the result, meaning the other
6752      * is a subset of that one.  In that case, we can skip the final copy and
6753      * return the larger of the input lists, but then outside code might need
6754      * to keep track of whether to free the input list or not */
6755
6756     UV* array_a;    /* a's array */
6757     UV* array_b;
6758     UV len_a;       /* length of a's array */
6759     UV len_b;
6760
6761     SV* u;                      /* the resulting union */
6762     UV* array_u;
6763     UV len_u;
6764
6765     UV i_a = 0;             /* current index into a's array */
6766     UV i_b = 0;
6767     UV i_u = 0;
6768
6769     /* running count, as explained in the algorithm source book; items are
6770      * stopped accumulating and are output when the count changes to/from 0.
6771      * The count is incremented when we start a range that's in the set, and
6772      * decremented when we start a range that's not in the set.  So its range
6773      * is 0 to 2.  Only when the count is zero is something not in the set.
6774      */
6775     UV count = 0;
6776
6777     PERL_ARGS_ASSERT__INVLIST_UNION_MAYBE_COMPLEMENT_2ND;
6778     assert(a != b);
6779
6780     /* If either one is empty, the union is the other one */
6781     if (a == NULL || ((len_a = invlist_len(a)) == 0)) {
6782         if (*output == a) {
6783             if (a != NULL) {
6784                 SvREFCNT_dec(a);
6785             }
6786         }
6787         if (*output != b) {
6788             *output = invlist_clone(b);
6789             if (complement_b) {
6790                 _invlist_invert(*output);
6791             }
6792         } /* else *output already = b; */
6793         return;
6794     }
6795     else if ((len_b = invlist_len(b)) == 0) {
6796         if (*output == b) {
6797             SvREFCNT_dec(b);
6798         }
6799
6800         /* The complement of an empty list is a list that has everything in it,
6801          * so the union with <a> includes everything too */
6802         if (complement_b) {
6803             if (a == *output) {
6804                 SvREFCNT_dec(a);
6805             }
6806             *output = _new_invlist(1);
6807             _append_range_to_invlist(*output, 0, UV_MAX);
6808         }
6809         else if (*output != a) {
6810             *output = invlist_clone(a);
6811         }
6812         /* else *output already = a; */
6813         return;
6814     }
6815
6816     /* Here both lists exist and are non-empty */
6817     array_a = invlist_array(a);
6818     array_b = invlist_array(b);
6819
6820     /* If are to take the union of 'a' with the complement of b, set it
6821      * up so are looking at b's complement. */
6822     if (complement_b) {
6823
6824         /* To complement, we invert: if the first element is 0, remove it.  To
6825          * do this, we just pretend the array starts one later, and clear the
6826          * flag as we don't have to do anything else later */
6827         if (array_b[0] == 0) {
6828             array_b++;
6829             len_b--;
6830             complement_b = FALSE;
6831         }
6832         else {
6833
6834             /* But if the first element is not zero, we unshift a 0 before the
6835              * array.  The data structure reserves a space for that 0 (which
6836              * should be a '1' right now), so physical shifting is unneeded,
6837              * but temporarily change that element to 0.  Before exiting the
6838              * routine, we must restore the element to '1' */
6839             array_b--;
6840             len_b++;
6841             array_b[0] = 0;
6842         }
6843     }
6844
6845     /* Size the union for the worst case: that the sets are completely
6846      * disjoint */
6847     u = _new_invlist(len_a + len_b);
6848
6849     /* Will contain U+0000 if either component does */
6850     array_u = _invlist_array_init(u, (len_a > 0 && array_a[0] == 0)
6851                                       || (len_b > 0 && array_b[0] == 0));
6852
6853     /* Go through each list item by item, stopping when exhausted one of
6854      * them */
6855     while (i_a < len_a && i_b < len_b) {
6856         UV cp;      /* The element to potentially add to the union's array */
6857         bool cp_in_set;   /* is it in the the input list's set or not */
6858
6859         /* We need to take one or the other of the two inputs for the union.
6860          * Since we are merging two sorted lists, we take the smaller of the
6861          * next items.  In case of a tie, we take the one that is in its set
6862          * first.  If we took one not in the set first, it would decrement the
6863          * count, possibly to 0 which would cause it to be output as ending the
6864          * range, and the next time through we would take the same number, and
6865          * output it again as beginning the next range.  By doing it the
6866          * opposite way, there is no possibility that the count will be
6867          * momentarily decremented to 0, and thus the two adjoining ranges will
6868          * be seamlessly merged.  (In a tie and both are in the set or both not
6869          * in the set, it doesn't matter which we take first.) */
6870         if (array_a[i_a] < array_b[i_b]
6871             || (array_a[i_a] == array_b[i_b]
6872                 && ELEMENT_RANGE_MATCHES_INVLIST(i_a)))
6873         {
6874             cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_a);
6875             cp= array_a[i_a++];
6876         }
6877         else {
6878             cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_b);
6879             cp= array_b[i_b++];
6880         }
6881
6882         /* Here, have chosen which of the two inputs to look at.  Only output
6883          * if the running count changes to/from 0, which marks the
6884          * beginning/end of a range in that's in the set */
6885         if (cp_in_set) {
6886             if (count == 0) {
6887                 array_u[i_u++] = cp;
6888             }
6889             count++;
6890         }
6891         else {
6892             count--;
6893             if (count == 0) {
6894                 array_u[i_u++] = cp;
6895             }
6896         }
6897     }
6898
6899     /* Here, we are finished going through at least one of the lists, which
6900      * means there is something remaining in at most one.  We check if the list
6901      * that hasn't been exhausted is positioned such that we are in the middle
6902      * of a range in its set or not.  (i_a and i_b point to the element beyond
6903      * the one we care about.) If in the set, we decrement 'count'; if 0, there
6904      * is potentially more to output.
6905      * There are four cases:
6906      *  1) Both weren't in their sets, count is 0, and remains 0.  What's left
6907      *     in the union is entirely from the non-exhausted set.
6908      *  2) Both were in their sets, count is 2.  Nothing further should
6909      *     be output, as everything that remains will be in the exhausted
6910      *     list's set, hence in the union; decrementing to 1 but not 0 insures
6911      *     that
6912      *  3) the exhausted was in its set, non-exhausted isn't, count is 1.
6913      *     Nothing further should be output because the union includes
6914      *     everything from the exhausted set.  Not decrementing ensures that.
6915      *  4) the exhausted wasn't in its set, non-exhausted is, count is 1;
6916      *     decrementing to 0 insures that we look at the remainder of the
6917      *     non-exhausted set */
6918     if ((i_a != len_a && PREV_RANGE_MATCHES_INVLIST(i_a))
6919         || (i_b != len_b && PREV_RANGE_MATCHES_INVLIST(i_b)))
6920     {
6921         count--;
6922     }
6923
6924     /* The final length is what we've output so far, plus what else is about to
6925      * be output.  (If 'count' is non-zero, then the input list we exhausted
6926      * has everything remaining up to the machine's limit in its set, and hence
6927      * in the union, so there will be no further output. */
6928     len_u = i_u;
6929     if (count == 0) {
6930         /* At most one of the subexpressions will be non-zero */
6931         len_u += (len_a - i_a) + (len_b - i_b);
6932     }
6933
6934     /* Set result to final length, which can change the pointer to array_u, so
6935      * re-find it */
6936     if (len_u != invlist_len(u)) {
6937         invlist_set_len(u, len_u);
6938         invlist_trim(u);
6939         array_u = invlist_array(u);
6940     }
6941
6942     /* When 'count' is 0, the list that was exhausted (if one was shorter than
6943      * the other) ended with everything above it not in its set.  That means
6944      * that the remaining part of the union is precisely the same as the
6945      * non-exhausted list, so can just copy it unchanged.  (If both list were
6946      * exhausted at the same time, then the operations below will be both 0.)
6947      */
6948     if (count == 0) {
6949         IV copy_count; /* At most one will have a non-zero copy count */
6950         if ((copy_count = len_a - i_a) > 0) {
6951             Copy(array_a + i_a, array_u + i_u, copy_count, UV);
6952         }
6953         else if ((copy_count = len_b - i_b) > 0) {
6954             Copy(array_b + i_b, array_u + i_u, copy_count, UV);
6955         }
6956     }
6957
6958     /*  We may be removing a reference to one of the inputs */
6959     if (a == *output || b == *output) {
6960         SvREFCNT_dec(*output);
6961     }
6962
6963     /* If we've changed b, restore it */
6964     if (complement_b) {
6965         array_b[0] = 1;
6966     }
6967
6968     *output = u;
6969     return;
6970 }
6971
6972 void
6973 Perl__invlist_intersection_maybe_complement_2nd(pTHX_ SV* const a, SV* const b, bool complement_b, SV** i)
6974 {
6975     /* Take the intersection of two inversion lists and point <i> to it.  *i
6976      * should be defined upon input, and if it points to one of the two lists,
6977      * the reference count to that list will be decremented.
6978      * If <complement_b> is TRUE, the result will be the intersection of <a>
6979      * and the complement (or inversion) of <b> instead of <b> directly.
6980      *
6981      * The basis for this comes from "Unicode Demystified" Chapter 13 by
6982      * Richard Gillam, published by Addison-Wesley, and explained at some
6983      * length there.  The preface says to incorporate its examples into your
6984      * code at your own risk.  In fact, it had bugs
6985      *
6986      * The algorithm is like a merge sort, and is essentially the same as the
6987      * union above
6988      */
6989
6990     UV* array_a;                /* a's array */
6991     UV* array_b;
6992     UV len_a;   /* length of a's array */
6993     UV len_b;
6994
6995     SV* r;                   /* the resulting intersection */
6996     UV* array_r;
6997     UV len_r;
6998
6999     UV i_a = 0;             /* current index into a's array */
7000     UV i_b = 0;
7001     UV i_r = 0;
7002
7003     /* running count, as explained in the algorithm source book; items are
7004      * stopped accumulating and are output when the count changes to/from 2.
7005      * The count is incremented when we start a range that's in the set, and
7006      * decremented when we start a range that's not in the set.  So its range
7007      * is 0 to 2.  Only when the count is 2 is something in the intersection.
7008      */
7009     UV count = 0;
7010
7011     PERL_ARGS_ASSERT__INVLIST_INTERSECTION_MAYBE_COMPLEMENT_2ND;
7012     assert(a != b);
7013
7014     /* Special case if either one is empty */
7015     len_a = invlist_len(a);
7016     if ((len_a == 0) || ((len_b = invlist_len(b)) == 0)) {
7017
7018         if (len_a != 0 && complement_b) {
7019
7020             /* Here, 'a' is not empty, therefore from the above 'if', 'b' must
7021              * be empty.  Here, also we are using 'b's complement, which hence
7022              * must be every possible code point.  Thus the intersection is
7023              * simply 'a'. */
7024             if (*i != a) {
7025                 *i = invlist_clone(a);
7026
7027                 if (*i == b) {
7028                     SvREFCNT_dec(b);
7029                 }
7030             }
7031             /* else *i is already 'a' */
7032             return;
7033         }
7034
7035         /* Here, 'a' or 'b' is empty and not using the complement of 'b'.  The
7036          * intersection must be empty */
7037         if (*i == a) {
7038             SvREFCNT_dec(a);
7039         }
7040         else if (*i == b) {
7041             SvREFCNT_dec(b);
7042         }
7043         *i = _new_invlist(0);
7044         return;
7045     }
7046
7047     /* Here both lists exist and are non-empty */
7048     array_a = invlist_array(a);
7049     array_b = invlist_array(b);
7050
7051     /* If are to take the intersection of 'a' with the complement of b, set it
7052      * up so are looking at b's complement. */
7053     if (complement_b) {
7054
7055         /* To complement, we invert: if the first element is 0, remove it.  To
7056          * do this, we just pretend the array starts one later, and clear the
7057          * flag as we don't have to do anything else later */
7058         if (array_b[0] == 0) {
7059             array_b++;
7060             len_b--;
7061             complement_b = FALSE;
7062         }
7063         else {
7064
7065             /* But if the first element is not zero, we unshift a 0 before the
7066              * array.  The data structure reserves a space for that 0 (which
7067              * should be a '1' right now), so physical shifting is unneeded,
7068              * but temporarily change that element to 0.  Before exiting the
7069              * routine, we must restore the element to '1' */
7070             array_b--;
7071             len_b++;
7072             array_b[0] = 0;
7073         }
7074     }
7075
7076     /* Size the intersection for the worst case: that the intersection ends up
7077      * fragmenting everything to be completely disjoint */
7078     r= _new_invlist(len_a + len_b);
7079
7080     /* Will contain U+0000 iff both components do */
7081     array_r = _invlist_array_init(r, len_a > 0 && array_a[0] == 0
7082                                      && len_b > 0 && array_b[0] == 0);
7083
7084     /* Go through each list item by item, stopping when exhausted one of
7085      * them */
7086     while (i_a < len_a && i_b < len_b) {
7087         UV cp;      /* The element to potentially add to the intersection's
7088                        array */
7089         bool cp_in_set; /* Is it in the input list's set or not */
7090
7091         /* We need to take one or the other of the two inputs for the
7092          * intersection.  Since we are merging two sorted lists, we take the
7093          * smaller of the next items.  In case of a tie, we take the one that
7094          * is not in its set first (a difference from the union algorithm).  If
7095          * we took one in the set first, it would increment the count, possibly
7096          * to 2 which would cause it to be output as starting a range in the
7097          * intersection, and the next time through we would take that same
7098          * number, and output it again as ending the set.  By doing it the
7099          * opposite of this, there is no possibility that the count will be
7100          * momentarily incremented to 2.  (In a tie and both are in the set or
7101          * both not in the set, it doesn't matter which we take first.) */
7102         if (array_a[i_a] < array_b[i_b]
7103             || (array_a[i_a] == array_b[i_b]
7104                 && ! ELEMENT_RANGE_MATCHES_INVLIST(i_a)))
7105         {
7106             cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_a);
7107             cp= array_a[i_a++];
7108         }
7109         else {
7110             cp_in_set = ELEMENT_RANGE_MATCHES_INVLIST(i_b);
7111             cp= array_b[i_b++];
7112         }
7113
7114         /* Here, have chosen which of the two inputs to look at.  Only output
7115          * if the running count changes to/from 2, which marks the
7116          * beginning/end of a range that's in the intersection */
7117         if (cp_in_set) {
7118             count++;
7119             if (count == 2) {
7120                 array_r[i_r++] = cp;
7121             }
7122         }
7123         else {
7124             if (count == 2) {
7125                 array_r[i_r++] = cp;
7126             }
7127             count--;
7128         }
7129     }
7130
7131     /* Here, we are finished going through at least one of the lists, which
7132      * means there is something remaining in at most one.  We check if the list
7133      * that has been exhausted is positioned such that we are in the middle
7134      * of a range in its set or not.  (i_a and i_b point to elements 1 beyond
7135      * the ones we care about.)  There are four cases:
7136      *  1) Both weren't in their sets, count is 0, and remains 0.  There's
7137      *     nothing left in the intersection.
7138      *  2) Both were in their sets, count is 2 and perhaps is incremented to
7139      *     above 2.  What should be output is exactly that which is in the
7140      *     non-exhausted set, as everything it has is also in the intersection
7141      *     set, and everything it doesn't have can't be in the intersection
7142      *  3) The exhausted was in its set, non-exhausted isn't, count is 1, and
7143      *     gets incremented to 2.  Like the previous case, the intersection is
7144      *     everything that remains in the non-exhausted set.
7145      *  4) the exhausted wasn't in its set, non-exhausted is, count is 1, and
7146      *     remains 1.  And the intersection has nothing more. */
7147     if ((i_a == len_a && PREV_RANGE_MATCHES_INVLIST(i_a))
7148         || (i_b == len_b && PREV_RANGE_MATCHES_INVLIST(i_b)))
7149     {
7150         count++;
7151     }
7152
7153     /* The final length is what we've output so far plus what else is in the
7154      * intersection.  At most one of the subexpressions below will be non-zero */
7155     len_r = i_r;
7156     if (count >= 2) {
7157         len_r += (len_a - i_a) + (len_b - i_b);
7158     }
7159
7160     /* Set result to final length, which can change the pointer to array_r, so
7161      * re-find it */
7162     if (len_r != invlist_len(r)) {
7163         invlist_set_len(r, len_r);
7164         invlist_trim(r);
7165         array_r = invlist_array(r);
7166     }
7167
7168     /* Finish outputting any remaining */
7169     if (count >= 2) { /* At most one will have a non-zero copy count */
7170         IV copy_count;
7171         if ((copy_count = len_a - i_a) > 0) {
7172             Copy(array_a + i_a, array_r + i_r, copy_count, UV);
7173         }
7174         else if ((copy_count = len_b - i_b) > 0) {
7175             Copy(array_b + i_b, array_r + i_r, copy_count, UV);
7176         }
7177     }
7178
7179     /*  We may be removing a reference to one of the inputs */
7180     if (a == *i || b == *i) {
7181         SvREFCNT_dec(*i);
7182     }
7183
7184     /* If we've changed b, restore it */
7185     if (complement_b) {
7186         array_b[0] = 1;
7187     }
7188
7189     *i = r;
7190     return;
7191 }
7192
7193 SV*
7194 Perl__add_range_to_invlist(pTHX_ SV* invlist, const UV start, const UV end)
7195 {
7196     /* Add the range from 'start' to 'end' inclusive to the inversion list's
7197      * set.  A pointer to the inversion list is returned.  This may actually be
7198      * a new list, in which case the passed in one has been destroyed.  The
7199      * passed in inversion list can be NULL, in which case a new one is created
7200      * with just the one range in it */
7201
7202     SV* range_invlist;
7203     UV len;
7204
7205     if (invlist == NULL) {
7206         invlist = _new_invlist(2);
7207         len = 0;
7208     }
7209     else {
7210         len = invlist_len(invlist);
7211     }
7212
7213     /* If comes after the final entry, can just append it to the end */
7214     if (len == 0
7215         || start >= invlist_array(invlist)
7216                                     [invlist_len(invlist) - 1])
7217     {
7218         _append_range_to_invlist(invlist, start, end);
7219         return invlist;
7220     }
7221
7222     /* Here, can't just append things, create and return a new inversion list
7223      * which is the union of this range and the existing inversion list */
7224     range_invlist = _new_invlist(2);
7225     _append_range_to_invlist(range_invlist, start, end);
7226
7227     _invlist_union(invlist, range_invlist, &invlist);
7228
7229     /* The temporary can be freed */
7230     SvREFCNT_dec(range_invlist);
7231
7232     return invlist;
7233 }
7234
7235 #endif
7236
7237 PERL_STATIC_INLINE SV*
7238 S_add_cp_to_invlist(pTHX_ SV* invlist, const UV cp) {
7239     return _add_range_to_invlist(invlist, cp, cp);
7240 }
7241
7242 #ifndef PERL_IN_XSUB_RE
7243 void
7244 Perl__invlist_invert(pTHX_ SV* const invlist)
7245 {
7246     /* Complement the input inversion list.  This adds a 0 if the list didn't
7247      * have a zero; removes it otherwise.  As described above, the data
7248      * structure is set up so that this is very efficient */
7249
7250     UV* len_pos = get_invlist_len_addr(invlist);
7251
7252     PERL_ARGS_ASSERT__INVLIST_INVERT;
7253
7254     /* The inverse of matching nothing is matching everything */
7255     if (*len_pos == 0) {
7256         _append_range_to_invlist(invlist, 0, UV_MAX);
7257         return;
7258     }
7259
7260     /* The exclusive or complents 0 to 1; and 1 to 0.  If the result is 1, the
7261      * zero element was a 0, so it is being removed, so the length decrements
7262      * by 1; and vice-versa.  SvCUR is unaffected */
7263     if (*get_invlist_zero_addr(invlist) ^= 1) {
7264         (*len_pos)--;
7265     }
7266     else {
7267         (*len_pos)++;
7268     }
7269 }
7270
7271 void
7272 Perl__invlist_invert_prop(pTHX_ SV* const invlist)
7273 {
7274     /* Complement the input inversion list (which must be a Unicode property,
7275      * all of which don't match above the Unicode maximum code point.)  And
7276      * Perl has chosen to not have the inversion match above that either.  This
7277      * adds a 0x110000 if the list didn't end with it, and removes it if it did
7278      */
7279
7280     UV len;
7281     UV* array;
7282
7283     PERL_ARGS_ASSERT__INVLIST_INVERT_PROP;
7284
7285     _invlist_invert(invlist);
7286
7287     len = invlist_len(invlist);
7288
7289     if (len != 0) { /* If empty do nothing */
7290         array = invlist_array(invlist);
7291         if (array[len - 1] != PERL_UNICODE_MAX + 1) {
7292             /* Add 0x110000.  First, grow if necessary */
7293             len++;
7294             if (invlist_max(invlist) < len) {
7295                 invlist_extend(invlist, len);
7296                 array = invlist_array(invlist);
7297             }
7298             invlist_set_len(invlist, len);
7299             array[len - 1] = PERL_UNICODE_MAX + 1;
7300         }
7301         else {  /* Remove the 0x110000 */
7302             invlist_set_len(invlist, len - 1);
7303         }
7304     }
7305
7306     return;
7307 }
7308 #endif
7309
7310 PERL_STATIC_INLINE SV*
7311 S_invlist_clone(pTHX_ SV* const invlist)
7312 {
7313
7314     /* Return a new inversion list that is a copy of the input one, which is
7315      * unchanged */
7316
7317     /* Need to allocate extra space to accommodate Perl's addition of a
7318      * trailing NUL to SvPV's, since it thinks they are always strings */
7319     SV* new_invlist = _new_invlist(invlist_len(invlist) + 1);
7320     STRLEN length = SvCUR(invlist);
7321
7322     PERL_ARGS_ASSERT_INVLIST_CLONE;
7323
7324     SvCUR_set(new_invlist, length); /* This isn't done automatically */
7325     Copy(SvPVX(invlist), SvPVX(new_invlist), length, char);
7326
7327     return new_invlist;
7328 }
7329
7330 PERL_STATIC_INLINE UV*
7331 S_get_invlist_iter_addr(pTHX_ SV* invlist)
7332 {
7333     /* Return the address of the UV that contains the current iteration
7334      * position */
7335
7336     PERL_ARGS_ASSERT_GET_INVLIST_ITER_ADDR;
7337
7338     return (UV *) (SvPVX(invlist) + (INVLIST_ITER_OFFSET * sizeof (UV)));
7339 }
7340
7341 PERL_STATIC_INLINE UV*
7342 S_get_invlist_version_id_addr(pTHX_ SV* invlist)
7343 {
7344     /* Return the address of the UV that contains the version id. */
7345
7346     PERL_ARGS_ASSERT_GET_INVLIST_VERSION_ID_ADDR;
7347
7348     return (UV *) (SvPVX(invlist) + (INVLIST_VERSION_ID_OFFSET * sizeof (UV)));
7349 }
7350
7351 PERL_STATIC_INLINE void
7352 S_invlist_iterinit(pTHX_ SV* invlist)   /* Initialize iterator for invlist */
7353 {
7354     PERL_ARGS_ASSERT_INVLIST_ITERINIT;
7355
7356     *get_invlist_iter_addr(invlist) = 0;
7357 }
7358
7359 STATIC bool
7360 S_invlist_iternext(pTHX_ SV* invlist, UV* start, UV* end)
7361 {
7362     /* An C<invlist_iterinit> call on <invlist> must be used to set this up.
7363      * This call sets in <*start> and <*end>, the next range in <invlist>.
7364      * Returns <TRUE> if successful and the next call will return the next
7365      * range; <FALSE> if was already at the end of the list.  If the latter,
7366      * <*start> and <*end> are unchanged, and the next call to this function
7367      * will start over at the beginning of the list */
7368
7369     UV* pos = get_invlist_iter_addr(invlist);
7370     UV len = invlist_len(invlist);
7371     UV *array;
7372
7373     PERL_ARGS_ASSERT_INVLIST_ITERNEXT;
7374
7375     if (*pos >= len) {
7376         *pos = UV_MAX;  /* Force iternit() to be required next time */
7377         return FALSE;
7378     }
7379
7380     array = invlist_array(invlist);
7381
7382     *start = array[(*pos)++];
7383
7384     if (*pos >= len) {
7385         *end = UV_MAX;
7386     }
7387     else {
7388         *end = array[(*pos)++] - 1;
7389     }
7390
7391     return TRUE;
7392 }
7393
7394 #ifndef PERL_IN_XSUB_RE
7395 SV *
7396 Perl__invlist_contents(pTHX_ SV* const invlist)
7397 {
7398     /* Get the contents of an inversion list into a string SV so that they can
7399      * be printed out.  It uses the format traditionally done for debug tracing
7400      */
7401
7402     UV start, end;
7403     SV* output = newSVpvs("\n");
7404
7405     PERL_ARGS_ASSERT__INVLIST_CONTENTS;
7406
7407     invlist_iterinit(invlist);
7408     while (invlist_iternext(invlist, &start, &end)) {
7409         if (end == UV_MAX) {
7410             Perl_sv_catpvf(aTHX_ output, "%04"UVXf"\tINFINITY\n", start);
7411         }
7412         else if (end != start) {
7413             Perl_sv_catpvf(aTHX_ output, "%04"UVXf"\t%04"UVXf"\n",
7414                     start,       end);
7415         }
7416         else {
7417             Perl_sv_catpvf(aTHX_ output, "%04"UVXf"\n", start);
7418         }
7419     }
7420
7421     return output;
7422 }
7423 #endif
7424
7425 #if 0
7426 void
7427 S_invlist_dump(pTHX_ SV* const invlist, const char * const header)
7428 {
7429     /* Dumps out the ranges in an inversion list.  The string 'header'
7430      * if present is output on a line before the first range */
7431
7432     UV start, end;
7433
7434     if (header && strlen(header)) {
7435         PerlIO_printf(Perl_debug_log, "%s\n", header);
7436     }
7437     invlist_iterinit(invlist);
7438     while (invlist_iternext(invlist, &start, &end)) {
7439         if (end == UV_MAX) {
7440             PerlIO_printf(Perl_debug_log, "0x%04"UVXf" .. INFINITY\n", start);
7441         }
7442         else {
7443             PerlIO_printf(Perl_debug_log, "0x%04"UVXf" .. 0x%04"UVXf"\n", start, end);
7444         }
7445     }
7446 }
7447 #endif
7448
7449 #undef HEADER_LENGTH
7450 #undef INVLIST_INITIAL_LENGTH
7451 #undef TO_INTERNAL_SIZE
7452 #undef FROM_INTERNAL_SIZE
7453 #undef INVLIST_LEN_OFFSET
7454 #undef INVLIST_ZERO_OFFSET
7455 #undef INVLIST_ITER_OFFSET
7456 #undef INVLIST_VERSION_ID
7457
7458 /* End of inversion list object */
7459
7460 /*
7461  - reg - regular expression, i.e. main body or parenthesized thing
7462  *
7463  * Caller must absorb opening parenthesis.
7464  *
7465  * Combining parenthesis handling with the base level of regular expression
7466  * is a trifle forced, but the need to tie the tails of the branches to what
7467  * follows makes it hard to avoid.
7468  */
7469 #define REGTAIL(x,y,z) regtail((x),(y),(z),depth+1)
7470 #ifdef DEBUGGING
7471 #define REGTAIL_STUDY(x,y,z) regtail_study((x),(y),(z),depth+1)
7472 #else
7473 #define REGTAIL_STUDY(x,y,z) regtail((x),(y),(z),depth+1)
7474 #endif
7475
7476 STATIC regnode *
7477 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp,U32 depth)
7478     /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
7479 {
7480     dVAR;
7481     register regnode *ret;              /* Will be the head of the group. */
7482     register regnode *br;
7483     register regnode *lastbr;
7484     register regnode *ender = NULL;
7485     register I32 parno = 0;
7486     I32 flags;
7487     U32 oregflags = RExC_flags;
7488     bool have_branch = 0;
7489     bool is_open = 0;
7490     I32 freeze_paren = 0;
7491     I32 after_freeze = 0;
7492
7493     /* for (?g), (?gc), and (?o) warnings; warning
7494        about (?c) will warn about (?g) -- japhy    */
7495
7496 #define WASTED_O  0x01
7497 #define WASTED_G  0x02
7498 #define WASTED_C  0x04
7499 #define WASTED_GC (0x02|0x04)
7500     I32 wastedflags = 0x00;
7501
7502     char * parse_start = RExC_parse; /* MJD */
7503     char * const oregcomp_parse = RExC_parse;
7504
7505     GET_RE_DEBUG_FLAGS_DECL;
7506
7507     PERL_ARGS_ASSERT_REG;
7508     DEBUG_PARSE("reg ");
7509
7510     *flagp = 0;                         /* Tentatively. */
7511
7512
7513     /* Make an OPEN node, if parenthesized. */
7514     if (paren) {
7515         if ( *RExC_parse == '*') { /* (*VERB:ARG) */
7516             char *start_verb = RExC_parse;
7517             STRLEN verb_len = 0;
7518             char *start_arg = NULL;
7519             unsigned char op = 0;
7520             int argok = 1;
7521             int internal_argval = 0; /* internal_argval is only useful if !argok */
7522             while ( *RExC_parse && *RExC_parse != ')' ) {
7523                 if ( *RExC_parse == ':' ) {
7524                     start_arg = RExC_parse + 1;
7525                     break;
7526                 }
7527                 RExC_parse++;
7528             }
7529             ++start_verb;
7530             verb_len = RExC_parse - start_verb;
7531             if ( start_arg ) {
7532                 RExC_parse++;
7533                 while ( *RExC_parse && *RExC_parse != ')' ) 
7534                     RExC_parse++;
7535                 if ( *RExC_parse != ')' ) 
7536                     vFAIL("Unterminated verb pattern argument");
7537                 if ( RExC_parse == start_arg )
7538                     start_arg = NULL;
7539             } else {
7540                 if ( *RExC_parse != ')' )
7541                     vFAIL("Unterminated verb pattern");
7542             }
7543             
7544             switch ( *start_verb ) {
7545             case 'A':  /* (*ACCEPT) */
7546                 if ( memEQs(start_verb,verb_len,"ACCEPT") ) {
7547                     op = ACCEPT;
7548                     internal_argval = RExC_nestroot;
7549                 }
7550                 break;
7551             case 'C':  /* (*COMMIT) */
7552                 if ( memEQs(start_verb,verb_len,"COMMIT") )
7553                     op = COMMIT;
7554                 break;
7555             case 'F':  /* (*FAIL) */
7556                 if ( verb_len==1 || memEQs(start_verb,verb_len,"FAIL") ) {
7557                     op = OPFAIL;
7558                     argok = 0;
7559                 }
7560                 break;
7561             case ':':  /* (*:NAME) */
7562             case 'M':  /* (*MARK:NAME) */
7563                 if ( verb_len==0 || memEQs(start_verb,verb_len,"MARK") ) {
7564                     op = MARKPOINT;
7565                     argok = -1;
7566                 }
7567                 break;
7568             case 'P':  /* (*PRUNE) */
7569                 if ( memEQs(start_verb,verb_len,"PRUNE") )
7570                     op = PRUNE;
7571                 break;
7572             case 'S':   /* (*SKIP) */  
7573                 if ( memEQs(start_verb,verb_len,"SKIP") ) 
7574                     op = SKIP;
7575                 break;
7576             case 'T':  /* (*THEN) */
7577                 /* [19:06] <TimToady> :: is then */
7578                 if ( memEQs(start_verb,verb_len,"THEN") ) {
7579                     op = CUTGROUP;
7580                     RExC_seen |= REG_SEEN_CUTGROUP;
7581                 }
7582                 break;
7583             }
7584             if ( ! op ) {
7585                 RExC_parse++;
7586                 vFAIL3("Unknown verb pattern '%.*s'",
7587                     verb_len, start_verb);
7588             }
7589             if ( argok ) {
7590                 if ( start_arg && internal_argval ) {
7591                     vFAIL3("Verb pattern '%.*s' may not have an argument",
7592                         verb_len, start_verb); 
7593                 } else if ( argok < 0 && !start_arg ) {
7594                     vFAIL3("Verb pattern '%.*s' has a mandatory argument",
7595                         verb_len, start_verb);    
7596                 } else {
7597                     ret = reganode(pRExC_state, op, internal_argval);
7598                     if ( ! internal_argval && ! SIZE_ONLY ) {
7599                         if (start_arg) {
7600                             SV *sv = newSVpvn( start_arg, RExC_parse - start_arg);
7601                             ARG(ret) = add_data( pRExC_state, 1, "S" );
7602                             RExC_rxi->data->data[ARG(ret)]=(void*)sv;
7603                             ret->flags = 0;
7604                         } else {
7605                             ret->flags = 1; 
7606                         }
7607                     }               
7608                 }
7609                 if (!internal_argval)
7610                     RExC_seen |= REG_SEEN_VERBARG;
7611             } else if ( start_arg ) {
7612                 vFAIL3("Verb pattern '%.*s' may not have an argument",
7613                         verb_len, start_verb);    
7614             } else {
7615                 ret = reg_node(pRExC_state, op);
7616             }
7617             nextchar(pRExC_state);
7618             return ret;
7619         } else 
7620         if (*RExC_parse == '?') { /* (?...) */
7621             bool is_logical = 0;
7622             const char * const seqstart = RExC_parse;
7623             bool has_use_defaults = FALSE;
7624
7625             RExC_parse++;
7626             paren = *RExC_parse++;
7627             ret = NULL;                 /* For look-ahead/behind. */
7628             switch (paren) {
7629
7630             case 'P':   /* (?P...) variants for those used to PCRE/Python */
7631                 paren = *RExC_parse++;
7632                 if ( paren == '<')         /* (?P<...>) named capture */
7633                     goto named_capture;
7634                 else if (paren == '>') {   /* (?P>name) named recursion */
7635                     goto named_recursion;
7636                 }
7637                 else if (paren == '=') {   /* (?P=...)  named backref */
7638                     /* this pretty much dupes the code for \k<NAME> in regatom(), if
7639                        you change this make sure you change that */
7640                     char* name_start = RExC_parse;
7641                     U32 num = 0;
7642                     SV *sv_dat = reg_scan_name(pRExC_state,
7643                         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7644                     if (RExC_parse == name_start || *RExC_parse != ')')
7645                         vFAIL2("Sequence %.3s... not terminated",parse_start);
7646
7647                     if (!SIZE_ONLY) {
7648                         num = add_data( pRExC_state, 1, "S" );
7649                         RExC_rxi->data->data[num]=(void*)sv_dat;
7650                         SvREFCNT_inc_simple_void(sv_dat);
7651                     }
7652                     RExC_sawback = 1;
7653                     ret = reganode(pRExC_state,
7654                                    ((! FOLD)
7655                                      ? NREF
7656                                      : (MORE_ASCII_RESTRICTED)
7657                                        ? NREFFA
7658                                        : (AT_LEAST_UNI_SEMANTICS)
7659                                          ? NREFFU
7660                                          : (LOC)
7661                                            ? NREFFL
7662                                            : NREFF),
7663                                     num);
7664                     *flagp |= HASWIDTH;
7665
7666                     Set_Node_Offset(ret, parse_start+1);
7667                     Set_Node_Cur_Length(ret); /* MJD */
7668
7669                     nextchar(pRExC_state);
7670                     return ret;
7671                 }
7672                 RExC_parse++;
7673                 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7674                 /*NOTREACHED*/
7675             case '<':           /* (?<...) */
7676                 if (*RExC_parse == '!')
7677                     paren = ',';
7678                 else if (*RExC_parse != '=') 
7679               named_capture:
7680                 {               /* (?<...>) */
7681                     char *name_start;
7682                     SV *svname;
7683                     paren= '>';
7684             case '\'':          /* (?'...') */
7685                     name_start= RExC_parse;
7686                     svname = reg_scan_name(pRExC_state,
7687                         SIZE_ONLY ?  /* reverse test from the others */
7688                         REG_RSN_RETURN_NAME : 
7689                         REG_RSN_RETURN_NULL);
7690                     if (RExC_parse == name_start) {
7691                         RExC_parse++;
7692                         vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7693                         /*NOTREACHED*/
7694                     }
7695                     if (*RExC_parse != paren)
7696                         vFAIL2("Sequence (?%c... not terminated",
7697                             paren=='>' ? '<' : paren);
7698                     if (SIZE_ONLY) {
7699                         HE *he_str;
7700                         SV *sv_dat = NULL;
7701                         if (!svname) /* shouldn't happen */
7702                             Perl_croak(aTHX_
7703                                 "panic: reg_scan_name returned NULL");
7704                         if (!RExC_paren_names) {
7705                             RExC_paren_names= newHV();
7706                             sv_2mortal(MUTABLE_SV(RExC_paren_names));
7707 #ifdef DEBUGGING
7708                             RExC_paren_name_list= newAV();
7709                             sv_2mortal(MUTABLE_SV(RExC_paren_name_list));
7710 #endif
7711                         }
7712                         he_str = hv_fetch_ent( RExC_paren_names, svname, 1, 0 );
7713                         if ( he_str )
7714                             sv_dat = HeVAL(he_str);
7715                         if ( ! sv_dat ) {
7716                             /* croak baby croak */
7717                             Perl_croak(aTHX_
7718                                 "panic: paren_name hash element allocation failed");
7719                         } else if ( SvPOK(sv_dat) ) {
7720                             /* (?|...) can mean we have dupes so scan to check
7721                                its already been stored. Maybe a flag indicating
7722                                we are inside such a construct would be useful,
7723                                but the arrays are likely to be quite small, so
7724                                for now we punt -- dmq */
7725                             IV count = SvIV(sv_dat);
7726                             I32 *pv = (I32*)SvPVX(sv_dat);
7727                             IV i;
7728                             for ( i = 0 ; i < count ; i++ ) {
7729                                 if ( pv[i] == RExC_npar ) {
7730                                     count = 0;
7731                                     break;
7732                                 }
7733                             }
7734                             if ( count ) {
7735                                 pv = (I32*)SvGROW(sv_dat, SvCUR(sv_dat) + sizeof(I32)+1);
7736                                 SvCUR_set(sv_dat, SvCUR(sv_dat) + sizeof(I32));
7737                                 pv[count] = RExC_npar;
7738                                 SvIV_set(sv_dat, SvIVX(sv_dat) + 1);
7739                             }
7740                         } else {
7741                             (void)SvUPGRADE(sv_dat,SVt_PVNV);
7742                             sv_setpvn(sv_dat, (char *)&(RExC_npar), sizeof(I32));
7743                             SvIOK_on(sv_dat);
7744                             SvIV_set(sv_dat, 1);
7745                         }
7746 #ifdef DEBUGGING
7747                         /* Yes this does cause a memory leak in debugging Perls */
7748                         if (!av_store(RExC_paren_name_list, RExC_npar, SvREFCNT_inc(svname)))
7749                             SvREFCNT_dec(svname);
7750 #endif
7751
7752                         /*sv_dump(sv_dat);*/
7753                     }
7754                     nextchar(pRExC_state);
7755                     paren = 1;
7756                     goto capturing_parens;
7757                 }
7758                 RExC_seen |= REG_SEEN_LOOKBEHIND;
7759                 RExC_in_lookbehind++;
7760                 RExC_parse++;
7761             case '=':           /* (?=...) */
7762                 RExC_seen_zerolen++;
7763                 break;
7764             case '!':           /* (?!...) */
7765                 RExC_seen_zerolen++;
7766                 if (*RExC_parse == ')') {
7767                     ret=reg_node(pRExC_state, OPFAIL);
7768                     nextchar(pRExC_state);
7769                     return ret;
7770                 }
7771                 break;
7772             case '|':           /* (?|...) */
7773                 /* branch reset, behave like a (?:...) except that
7774                    buffers in alternations share the same numbers */
7775                 paren = ':'; 
7776                 after_freeze = freeze_paren = RExC_npar;
7777                 break;
7778             case ':':           /* (?:...) */
7779             case '>':           /* (?>...) */
7780                 break;
7781             case '$':           /* (?$...) */
7782             case '@':           /* (?@...) */
7783                 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
7784                 break;
7785             case '#':           /* (?#...) */
7786                 while (*RExC_parse && *RExC_parse != ')')
7787                     RExC_parse++;
7788                 if (*RExC_parse != ')')
7789                     FAIL("Sequence (?#... not terminated");
7790                 nextchar(pRExC_state);
7791                 *flagp = TRYAGAIN;
7792                 return NULL;
7793             case '0' :           /* (?0) */
7794             case 'R' :           /* (?R) */
7795                 if (*RExC_parse != ')')
7796                     FAIL("Sequence (?R) not terminated");
7797                 ret = reg_node(pRExC_state, GOSTART);
7798                 *flagp |= POSTPONED;
7799                 nextchar(pRExC_state);
7800                 return ret;
7801                 /*notreached*/
7802             { /* named and numeric backreferences */
7803                 I32 num;
7804             case '&':            /* (?&NAME) */
7805                 parse_start = RExC_parse - 1;
7806               named_recursion:
7807                 {
7808                     SV *sv_dat = reg_scan_name(pRExC_state,
7809                         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7810                      num = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
7811                 }
7812                 goto gen_recurse_regop;
7813                 /* NOT REACHED */
7814             case '+':
7815                 if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
7816                     RExC_parse++;
7817                     vFAIL("Illegal pattern");
7818                 }
7819                 goto parse_recursion;
7820                 /* NOT REACHED*/
7821             case '-': /* (?-1) */
7822                 if (!(RExC_parse[0] >= '1' && RExC_parse[0] <= '9')) {
7823                     RExC_parse--; /* rewind to let it be handled later */
7824                     goto parse_flags;
7825                 } 
7826                 /*FALLTHROUGH */
7827             case '1': case '2': case '3': case '4': /* (?1) */
7828             case '5': case '6': case '7': case '8': case '9':
7829                 RExC_parse--;
7830               parse_recursion:
7831                 num = atoi(RExC_parse);
7832                 parse_start = RExC_parse - 1; /* MJD */
7833                 if (*RExC_parse == '-')
7834                     RExC_parse++;
7835                 while (isDIGIT(*RExC_parse))
7836                         RExC_parse++;
7837                 if (*RExC_parse!=')') 
7838                     vFAIL("Expecting close bracket");
7839
7840               gen_recurse_regop:
7841                 if ( paren == '-' ) {
7842                     /*
7843                     Diagram of capture buffer numbering.
7844                     Top line is the normal capture buffer numbers
7845                     Bottom line is the negative indexing as from
7846                     the X (the (?-2))
7847
7848                     +   1 2    3 4 5 X          6 7
7849                        /(a(x)y)(a(b(c(?-2)d)e)f)(g(h))/
7850                     -   5 4    3 2 1 X          x x
7851
7852                     */
7853                     num = RExC_npar + num;
7854                     if (num < 1)  {
7855                         RExC_parse++;
7856                         vFAIL("Reference to nonexistent group");
7857                     }
7858                 } else if ( paren == '+' ) {
7859                     num = RExC_npar + num - 1;
7860                 }
7861
7862                 ret = reganode(pRExC_state, GOSUB, num);
7863                 if (!SIZE_ONLY) {
7864                     if (num > (I32)RExC_rx->nparens) {
7865                         RExC_parse++;
7866                         vFAIL("Reference to nonexistent group");
7867                     }
7868                     ARG2L_SET( ret, RExC_recurse_count++);
7869                     RExC_emit++;
7870                     DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
7871                         "Recurse #%"UVuf" to %"IVdf"\n", (UV)ARG(ret), (IV)ARG2L(ret)));
7872                 } else {
7873                     RExC_size++;
7874                 }
7875                 RExC_seen |= REG_SEEN_RECURSE;
7876                 Set_Node_Length(ret, 1 + regarglen[OP(ret)]); /* MJD */
7877                 Set_Node_Offset(ret, parse_start); /* MJD */
7878
7879                 *flagp |= POSTPONED;
7880                 nextchar(pRExC_state);
7881                 return ret;
7882             } /* named and numeric backreferences */
7883             /* NOT REACHED */
7884
7885             case '?':           /* (??...) */
7886                 is_logical = 1;
7887                 if (*RExC_parse != '{') {
7888                     RExC_parse++;
7889                     vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
7890                     /*NOTREACHED*/
7891                 }
7892                 *flagp |= POSTPONED;
7893                 paren = *RExC_parse++;
7894                 /* FALL THROUGH */
7895             case '{':           /* (?{...}) */
7896             {
7897                 I32 count = 1;
7898                 U32 n = 0;
7899                 char c;
7900                 char *s = RExC_parse;
7901
7902                 RExC_seen_zerolen++;
7903                 RExC_seen |= REG_SEEN_EVAL;
7904                 while (count && (c = *RExC_parse)) {
7905                     if (c == '\\') {
7906                         if (RExC_parse[1])
7907                             RExC_parse++;
7908                     }
7909                     else if (c == '{')
7910                         count++;
7911                     else if (c == '}')
7912                         count--;
7913                     RExC_parse++;
7914                 }
7915                 if (*RExC_parse != ')') {
7916                     RExC_parse = s;
7917                     vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
7918                 }
7919                 if (!SIZE_ONLY) {
7920                     PAD *pad;
7921                     OP_4tree *sop, *rop;
7922                     SV * const sv = newSVpvn(s, RExC_parse - 1 - s);
7923
7924                     ENTER;
7925                     Perl_save_re_context(aTHX);
7926                     rop = Perl_sv_compile_2op_is_broken(aTHX_ sv, &sop, "re", &pad);
7927                     sop->op_private |= OPpREFCOUNTED;
7928                     /* re_dup will OpREFCNT_inc */
7929                     OpREFCNT_set(sop, 1);
7930                     LEAVE;
7931
7932                     n = add_data(pRExC_state, 3, "nop");
7933                     RExC_rxi->data->data[n] = (void*)rop;
7934                     RExC_rxi->data->data[n+1] = (void*)sop;
7935                     RExC_rxi->data->data[n+2] = (void*)pad;
7936                     SvREFCNT_dec(sv);
7937                 }
7938                 else {                                          /* First pass */
7939                     if (PL_reginterp_cnt < ++RExC_seen_evals
7940                         && IN_PERL_RUNTIME)
7941                         /* No compiled RE interpolated, has runtime
7942                            components ===> unsafe.  */
7943                         FAIL("Eval-group not allowed at runtime, use re 'eval'");
7944                     if (PL_tainting && PL_tainted)
7945                         FAIL("Eval-group in insecure regular expression");
7946 #if PERL_VERSION > 8
7947                     if (IN_PERL_COMPILETIME)
7948                         PL_cv_has_eval = 1;
7949 #endif
7950                 }
7951
7952                 nextchar(pRExC_state);
7953                 if (is_logical) {
7954                     ret = reg_node(pRExC_state, LOGICAL);
7955                     if (!SIZE_ONLY)
7956                         ret->flags = 2;
7957                     REGTAIL(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
7958                     /* deal with the length of this later - MJD */
7959                     return ret;
7960                 }
7961                 ret = reganode(pRExC_state, EVAL, n);
7962                 Set_Node_Length(ret, RExC_parse - parse_start + 1);
7963                 Set_Node_Offset(ret, parse_start);
7964                 return ret;
7965             }
7966             case '(':           /* (?(?{...})...) and (?(?=...)...) */
7967             {
7968                 int is_define= 0;
7969                 if (RExC_parse[0] == '?') {        /* (?(?...)) */
7970                     if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
7971                         || RExC_parse[1] == '<'
7972                         || RExC_parse[1] == '{') { /* Lookahead or eval. */
7973                         I32 flag;
7974
7975                         ret = reg_node(pRExC_state, LOGICAL);
7976                         if (!SIZE_ONLY)
7977                             ret->flags = 1;
7978                         REGTAIL(pRExC_state, ret, reg(pRExC_state, 1, &flag,depth+1));
7979                         goto insert_if;
7980                     }
7981                 }
7982                 else if ( RExC_parse[0] == '<'     /* (?(<NAME>)...) */
7983                          || RExC_parse[0] == '\'' ) /* (?('NAME')...) */
7984                 {
7985                     char ch = RExC_parse[0] == '<' ? '>' : '\'';
7986                     char *name_start= RExC_parse++;
7987                     U32 num = 0;
7988                     SV *sv_dat=reg_scan_name(pRExC_state,
7989                         SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
7990                     if (RExC_parse == name_start || *RExC_parse != ch)
7991                         vFAIL2("Sequence (?(%c... not terminated",
7992                             (ch == '>' ? '<' : ch));
7993                     RExC_parse++;
7994                     if (!SIZE_ONLY) {
7995                         num = add_data( pRExC_state, 1, "S" );
7996                         RExC_rxi->data->data[num]=(void*)sv_dat;
7997                         SvREFCNT_inc_simple_void(sv_dat);
7998                     }
7999                     ret = reganode(pRExC_state,NGROUPP,num);
8000                     goto insert_if_check_paren;
8001                 }
8002                 else if (RExC_parse[0] == 'D' &&
8003                          RExC_parse[1] == 'E' &&
8004                          RExC_parse[2] == 'F' &&
8005                          RExC_parse[3] == 'I' &&
8006                          RExC_parse[4] == 'N' &&
8007                          RExC_parse[5] == 'E')
8008                 {
8009                     ret = reganode(pRExC_state,DEFINEP,0);
8010                     RExC_parse +=6 ;
8011                     is_define = 1;
8012                     goto insert_if_check_paren;
8013                 }
8014                 else if (RExC_parse[0] == 'R') {
8015                     RExC_parse++;
8016                     parno = 0;
8017                     if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
8018                         parno = atoi(RExC_parse++);
8019                         while (isDIGIT(*RExC_parse))
8020                             RExC_parse++;
8021                     } else if (RExC_parse[0] == '&') {
8022                         SV *sv_dat;
8023                         RExC_parse++;
8024                         sv_dat = reg_scan_name(pRExC_state,
8025                             SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
8026                         parno = sv_dat ? *((I32 *)SvPVX(sv_dat)) : 0;
8027                     }
8028                     ret = reganode(pRExC_state,INSUBP,parno); 
8029                     goto insert_if_check_paren;
8030                 }
8031                 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
8032                     /* (?(1)...) */
8033                     char c;
8034                     parno = atoi(RExC_parse++);
8035
8036                     while (isDIGIT(*RExC_parse))
8037                         RExC_parse++;
8038                     ret = reganode(pRExC_state, GROUPP, parno);
8039
8040                  insert_if_check_paren:
8041                     if ((c = *nextchar(pRExC_state)) != ')')
8042                         vFAIL("Switch condition not recognized");
8043                   insert_if:
8044                     REGTAIL(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
8045                     br = regbranch(pRExC_state, &flags, 1,depth+1);
8046                     if (br == NULL)
8047                         br = reganode(pRExC_state, LONGJMP, 0);
8048                     else
8049                         REGTAIL(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
8050                     c = *nextchar(pRExC_state);
8051                     if (flags&HASWIDTH)
8052                         *flagp |= HASWIDTH;
8053                     if (c == '|') {
8054                         if (is_define) 
8055                             vFAIL("(?(DEFINE)....) does not allow branches");
8056                         lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
8057                         regbranch(pRExC_state, &flags, 1,depth+1);
8058                         REGTAIL(pRExC_state, ret, lastbr);
8059                         if (flags&HASWIDTH)
8060                             *flagp |= HASWIDTH;
8061                         c = *nextchar(pRExC_state);
8062                     }
8063                     else
8064                         lastbr = NULL;
8065                     if (c != ')')
8066                         vFAIL("Switch (?(condition)... contains too many branches");
8067                     ender = reg_node(pRExC_state, TAIL);
8068                     REGTAIL(pRExC_state, br, ender);
8069                     if (lastbr) {
8070                         REGTAIL(pRExC_state, lastbr, ender);
8071                         REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
8072                     }
8073                     else
8074                         REGTAIL(pRExC_state, ret, ender);
8075                     RExC_size++; /* XXX WHY do we need this?!!
8076                                     For large programs it seems to be required
8077                                     but I can't figure out why. -- dmq*/
8078                     return ret;
8079                 }
8080                 else {
8081                     vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
8082                 }
8083             }
8084             case 0:
8085                 RExC_parse--; /* for vFAIL to print correctly */
8086                 vFAIL("Sequence (? incomplete");
8087                 break;
8088             case DEFAULT_PAT_MOD:   /* Use default flags with the exceptions
8089                                        that follow */
8090                 has_use_defaults = TRUE;
8091                 STD_PMMOD_FLAGS_CLEAR(&RExC_flags);
8092                 set_regex_charset(&RExC_flags, (RExC_utf8 || RExC_uni_semantics)
8093                                                 ? REGEX_UNICODE_CHARSET
8094                                                 : REGEX_DEPENDS_CHARSET);
8095                 goto parse_flags;
8096             default:
8097                 --RExC_parse;
8098                 parse_flags:      /* (?i) */  
8099             {
8100                 U32 posflags = 0, negflags = 0;
8101                 U32 *flagsp = &posflags;
8102                 char has_charset_modifier = '\0';
8103                 regex_charset cs = get_regex_charset(RExC_flags);
8104                 if (cs == REGEX_DEPENDS_CHARSET
8105                     && (RExC_utf8 || RExC_uni_semantics))
8106                 {
8107                     cs = REGEX_UNICODE_CHARSET;
8108                 }
8109
8110                 while (*RExC_parse) {
8111                     /* && strchr("iogcmsx", *RExC_parse) */
8112                     /* (?g), (?gc) and (?o) are useless here
8113                        and must be globally applied -- japhy */
8114                     switch (*RExC_parse) {
8115                     CASE_STD_PMMOD_FLAGS_PARSE_SET(flagsp);
8116                     case LOCALE_PAT_MOD:
8117                         if (has_charset_modifier) {
8118                             goto excess_modifier;
8119                         }
8120                         else if (flagsp == &negflags) {
8121                             goto neg_modifier;
8122                         }
8123                         cs = REGEX_LOCALE_CHARSET;
8124                         has_charset_modifier = LOCALE_PAT_MOD;
8125                         RExC_contains_locale = 1;
8126                         break;
8127                     case UNICODE_PAT_MOD:
8128                         if (has_charset_modifier) {
8129                             goto excess_modifier;
8130                         }
8131                         else if (flagsp == &negflags) {
8132                             goto neg_modifier;
8133                         }
8134                         cs = REGEX_UNICODE_CHARSET;
8135                         has_charset_modifier = UNICODE_PAT_MOD;
8136                         break;
8137                     case ASCII_RESTRICT_PAT_MOD:
8138                         if (flagsp == &negflags) {
8139                             goto neg_modifier;
8140                         }
8141                         if (has_charset_modifier) {
8142                             if (cs != REGEX_ASCII_RESTRICTED_CHARSET) {
8143                                 goto excess_modifier;
8144                             }
8145                             /* Doubled modifier implies more restricted */
8146                             cs = REGEX_ASCII_MORE_RESTRICTED_CHARSET;
8147                         }
8148                         else {
8149                             cs = REGEX_ASCII_RESTRICTED_CHARSET;
8150                         }
8151                         has_charset_modifier = ASCII_RESTRICT_PAT_MOD;
8152                         break;
8153                     case DEPENDS_PAT_MOD:
8154                         if (has_use_defaults) {
8155                             goto fail_modifiers;
8156                         }
8157                         else if (flagsp == &negflags) {
8158                             goto neg_modifier;
8159                         }
8160                         else if (has_charset_modifier) {
8161                             goto excess_modifier;
8162                         }
8163
8164                         /* The dual charset means unicode semantics if the
8165                          * pattern (or target, not known until runtime) are
8166                          * utf8, or something in the pattern indicates unicode
8167                          * semantics */
8168                         cs = (RExC_utf8 || RExC_uni_semantics)
8169                              ? REGEX_UNICODE_CHARSET
8170                              : REGEX_DEPENDS_CHARSET;
8171                         has_charset_modifier = DEPENDS_PAT_MOD;
8172                         break;
8173                     excess_modifier:
8174                         RExC_parse++;
8175                         if (has_charset_modifier == ASCII_RESTRICT_PAT_MOD) {
8176                             vFAIL2("Regexp modifier \"%c\" may appear a maximum of twice", ASCII_RESTRICT_PAT_MOD);
8177                         }
8178                         else if (has_charset_modifier == *(RExC_parse - 1)) {
8179                             vFAIL2("Regexp modifier \"%c\" may not appear twice", *(RExC_parse - 1));
8180                         }
8181                         else {
8182                             vFAIL3("Regexp modifiers \"%c\" and \"%c\" are mutually exclusive", has_charset_modifier, *(RExC_parse - 1));
8183                         }
8184                         /*NOTREACHED*/
8185                     neg_modifier:
8186                         RExC_parse++;
8187                         vFAIL2("Regexp modifier \"%c\" may not appear after the \"-\"", *(RExC_parse - 1));
8188                         /*NOTREACHED*/
8189                     case ONCE_PAT_MOD: /* 'o' */
8190                     case GLOBAL_PAT_MOD: /* 'g' */
8191                         if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
8192                             const I32 wflagbit = *RExC_parse == 'o' ? WASTED_O : WASTED_G;
8193                             if (! (wastedflags & wflagbit) ) {
8194                                 wastedflags |= wflagbit;
8195                                 vWARN5(
8196                                     RExC_parse + 1,
8197                                     "Useless (%s%c) - %suse /%c modifier",
8198                                     flagsp == &negflags ? "?-" : "?",
8199                                     *RExC_parse,
8200                                     flagsp == &negflags ? "don't " : "",
8201                                     *RExC_parse
8202                                 );
8203                             }
8204                         }
8205                         break;
8206                         
8207                     case CONTINUE_PAT_MOD: /* 'c' */
8208                         if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
8209                             if (! (wastedflags & WASTED_C) ) {
8210                                 wastedflags |= WASTED_GC;
8211                                 vWARN3(
8212                                     RExC_parse + 1,
8213                                     "Useless (%sc) - %suse /gc modifier",
8214                                     flagsp == &negflags ? "?-" : "?",
8215                                     flagsp == &negflags ? "don't " : ""
8216                                 );
8217                             }
8218                         }
8219                         break;
8220                     case KEEPCOPY_PAT_MOD: /* 'p' */
8221                         if (flagsp == &negflags) {
8222                             if (SIZE_ONLY)
8223                                 ckWARNreg(RExC_parse + 1,"Useless use of (?-p)");
8224                         } else {
8225                             *flagsp |= RXf_PMf_KEEPCOPY;
8226                         }
8227                         break;
8228                     case '-':
8229                         /* A flag is a default iff it is following a minus, so
8230                          * if there is a minus, it means will be trying to
8231                          * re-specify a default which is an error */
8232                         if (has_use_defaults || flagsp == &negflags) {
8233             fail_modifiers:
8234                             RExC_parse++;
8235                             vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
8236                             /*NOTREACHED*/
8237                         }
8238                         flagsp = &negflags;
8239                         wastedflags = 0;  /* reset so (?g-c) warns twice */
8240                         break;
8241                     case ':':
8242                         paren = ':';
8243                         /*FALLTHROUGH*/
8244                     case ')':
8245                         RExC_flags |= posflags;
8246                         RExC_flags &= ~negflags;
8247                         set_regex_charset(&RExC_flags, cs);
8248                         if (paren != ':') {
8249                             oregflags |= posflags;
8250                             oregflags &= ~negflags;
8251                             set_regex_charset(&oregflags, cs);
8252                         }
8253                         nextchar(pRExC_state);
8254                         if (paren != ':') {
8255                             *flagp = TRYAGAIN;
8256                             return NULL;
8257                         } else {
8258                             ret = NULL;
8259                             goto parse_rest;
8260                         }
8261                         /*NOTREACHED*/
8262                     default:
8263                         RExC_parse++;
8264                         vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
8265                         /*NOTREACHED*/
8266                     }                           
8267                     ++RExC_parse;
8268                 }
8269             }} /* one for the default block, one for the switch */
8270         }
8271         else {                  /* (...) */
8272           capturing_parens:
8273             parno = RExC_npar;
8274             RExC_npar++;
8275             
8276             ret = reganode(pRExC_state, OPEN, parno);
8277             if (!SIZE_ONLY ){
8278                 if (!RExC_nestroot) 
8279                     RExC_nestroot = parno;
8280                 if (RExC_seen & REG_SEEN_RECURSE
8281                     && !RExC_open_parens[parno-1])
8282                 {
8283                     DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
8284                         "Setting open paren #%"IVdf" to %d\n", 
8285                         (IV)parno, REG_NODE_NUM(ret)));
8286                     RExC_open_parens[parno-1]= ret;
8287                 }
8288             }
8289             Set_Node_Length(ret, 1); /* MJD */
8290             Set_Node_Offset(ret, RExC_parse); /* MJD */
8291             is_open = 1;
8292         }
8293     }
8294     else                        /* ! paren */
8295         ret = NULL;
8296    
8297    parse_rest:
8298     /* Pick up the branches, linking them together. */
8299     parse_start = RExC_parse;   /* MJD */
8300     br = regbranch(pRExC_state, &flags, 1,depth+1);
8301
8302     /*     branch_len = (paren != 0); */
8303
8304     if (br == NULL)
8305         return(NULL);
8306     if (*RExC_parse == '|') {
8307         if (!SIZE_ONLY && RExC_extralen) {
8308             reginsert(pRExC_state, BRANCHJ, br, depth+1);
8309         }
8310         else {                  /* MJD */
8311             reginsert(pRExC_state, BRANCH, br, depth+1);
8312             Set_Node_Length(br, paren != 0);
8313             Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start);
8314         }
8315         have_branch = 1;
8316         if (SIZE_ONLY)
8317             RExC_extralen += 1;         /* For BRANCHJ-BRANCH. */
8318     }
8319     else if (paren == ':') {
8320         *flagp |= flags&SIMPLE;
8321     }
8322     if (is_open) {                              /* Starts with OPEN. */
8323         REGTAIL(pRExC_state, ret, br);          /* OPEN -> first. */
8324     }
8325     else if (paren != '?')              /* Not Conditional */
8326         ret = br;
8327     *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
8328     lastbr = br;
8329     while (*RExC_parse == '|') {
8330         if (!SIZE_ONLY && RExC_extralen) {
8331             ender = reganode(pRExC_state, LONGJMP,0);
8332             REGTAIL(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
8333         }
8334         if (SIZE_ONLY)
8335             RExC_extralen += 2;         /* Account for LONGJMP. */
8336         nextchar(pRExC_state);
8337         if (freeze_paren) {
8338             if (RExC_npar > after_freeze)
8339                 after_freeze = RExC_npar;
8340             RExC_npar = freeze_paren;       
8341         }
8342         br = regbranch(pRExC_state, &flags, 0, depth+1);
8343
8344         if (br == NULL)
8345             return(NULL);
8346         REGTAIL(pRExC_state, lastbr, br);               /* BRANCH -> BRANCH. */
8347         lastbr = br;
8348         *flagp |= flags & (SPSTART | HASWIDTH | POSTPONED);
8349     }
8350
8351     if (have_branch || paren != ':') {
8352         /* Make a closing node, and hook it on the end. */
8353         switch (paren) {
8354         case ':':
8355             ender = reg_node(pRExC_state, TAIL);
8356             break;
8357         case 1:
8358             ender = reganode(pRExC_state, CLOSE, parno);
8359             if (!SIZE_ONLY && RExC_seen & REG_SEEN_RECURSE) {
8360                 DEBUG_OPTIMISE_MORE_r(PerlIO_printf(Perl_debug_log,
8361                         "Setting close paren #%"IVdf" to %d\n", 
8362                         (IV)parno, REG_NODE_NUM(ender)));
8363                 RExC_close_parens[parno-1]= ender;
8364                 if (RExC_nestroot == parno) 
8365                     RExC_nestroot = 0;
8366             }       
8367             Set_Node_Offset(ender,RExC_parse+1); /* MJD */
8368             Set_Node_Length(ender,1); /* MJD */
8369             break;
8370         case '<':
8371         case ',':
8372         case '=':
8373         case '!':
8374             *flagp &= ~HASWIDTH;
8375             /* FALL THROUGH */
8376         case '>':
8377             ender = reg_node(pRExC_state, SUCCEED);
8378             break;
8379         case 0:
8380             ender = reg_node(pRExC_state, END);
8381             if (!SIZE_ONLY) {
8382                 assert(!RExC_opend); /* there can only be one! */
8383                 RExC_opend = ender;
8384             }
8385             break;
8386         }
8387         REGTAIL(pRExC_state, lastbr, ender);
8388
8389         if (have_branch && !SIZE_ONLY) {
8390             if (depth==1)
8391                 RExC_seen |= REG_TOP_LEVEL_BRANCHES;
8392
8393             /* Hook the tails of the branches to the closing node. */
8394             for (br = ret; br; br = regnext(br)) {
8395                 const U8 op = PL_regkind[OP(br)];
8396                 if (op == BRANCH) {
8397                     REGTAIL_STUDY(pRExC_state, NEXTOPER(br), ender);
8398                 }
8399                 else if (op == BRANCHJ) {
8400                     REGTAIL_STUDY(pRExC_state, NEXTOPER(NEXTOPER(br)), ender);
8401                 }
8402             }
8403         }
8404     }
8405
8406     {
8407         const char *p;
8408         static const char parens[] = "=!<,>";
8409
8410         if (paren && (p = strchr(parens, paren))) {
8411             U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
8412             int flag = (p - parens) > 1;
8413
8414             if (paren == '>')
8415                 node = SUSPEND, flag = 0;
8416             reginsert(pRExC_state, node,ret, depth+1);
8417             Set_Node_Cur_Length(ret);
8418             Set_Node_Offset(ret, parse_start + 1);
8419             ret->flags = flag;
8420             REGTAIL_STUDY(pRExC_state, ret, reg_node(pRExC_state, TAIL));
8421         }
8422     }
8423
8424     /* Check for proper termination. */
8425     if (paren) {
8426         RExC_flags = oregflags;
8427         if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
8428             RExC_parse = oregcomp_parse;
8429             vFAIL("Unmatched (");
8430         }
8431     }
8432     else if (!paren && RExC_parse < RExC_end) {
8433         if (*RExC_parse == ')') {
8434             RExC_parse++;
8435             vFAIL("Unmatched )");
8436         }
8437         else
8438             FAIL("Junk on end of regexp");      /* "Can't happen". */
8439         /* NOTREACHED */
8440     }
8441
8442     if (RExC_in_lookbehind) {
8443         RExC_in_lookbehind--;
8444     }
8445     if (after_freeze > RExC_npar)
8446         RExC_npar = after_freeze;
8447     return(ret);
8448 }
8449
8450 /*
8451  - regbranch - one alternative of an | operator
8452  *
8453  * Implements the concatenation operator.
8454  */
8455 STATIC regnode *
8456 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first, U32 depth)
8457 {
8458     dVAR;
8459     register regnode *ret;
8460     register regnode *chain = NULL;
8461     register regnode *latest;
8462     I32 flags = 0, c = 0;
8463     GET_RE_DEBUG_FLAGS_DECL;
8464
8465     PERL_ARGS_ASSERT_REGBRANCH;
8466
8467     DEBUG_PARSE("brnc");
8468
8469     if (first)
8470         ret = NULL;
8471     else {
8472         if (!SIZE_ONLY && RExC_extralen)
8473             ret = reganode(pRExC_state, BRANCHJ,0);
8474         else {
8475             ret = reg_node(pRExC_state, BRANCH);
8476             Set_Node_Length(ret, 1);
8477         }
8478     }
8479
8480     if (!first && SIZE_ONLY)
8481         RExC_extralen += 1;                     /* BRANCHJ */
8482
8483     *flagp = WORST;                     /* Tentatively. */
8484
8485     RExC_parse--;
8486     nextchar(pRExC_state);
8487     while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
8488         flags &= ~TRYAGAIN;
8489         latest = regpiece(pRExC_state, &flags,depth+1);
8490         if (latest == NULL) {
8491             if (flags & TRYAGAIN)
8492                 continue;
8493             return(NULL);
8494         }
8495         else if (ret == NULL)
8496             ret = latest;
8497         *flagp |= flags&(HASWIDTH|POSTPONED);
8498         if (chain == NULL)      /* First piece. */
8499             *flagp |= flags&SPSTART;
8500         else {
8501             RExC_naughty++;
8502             REGTAIL(pRExC_state, chain, latest);
8503         }
8504         chain = latest;
8505         c++;
8506     }
8507     if (chain == NULL) {        /* Loop ran zero times. */
8508         chain = reg_node(pRExC_state, NOTHING);
8509         if (ret == NULL)
8510             ret = chain;
8511     }
8512     if (c == 1) {
8513         *flagp |= flags&SIMPLE;
8514     }
8515
8516     return ret;
8517 }
8518
8519 /*
8520  - regpiece - something followed by possible [*+?]
8521  *
8522  * Note that the branching code sequences used for ? and the general cases
8523  * of * and + are somewhat optimized:  they use the same NOTHING node as
8524  * both the endmarker for their branch list and the body of the last branch.
8525  * It might seem that this node could be dispensed with entirely, but the
8526  * endmarker role is not redundant.
8527  */
8528 STATIC regnode *
8529 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
8530 {
8531     dVAR;
8532     register regnode *ret;
8533     register char op;
8534     register char *next;
8535     I32 flags;
8536     const char * const origparse = RExC_parse;
8537     I32 min;
8538     I32 max = REG_INFTY;
8539 #ifdef RE_TRACK_PATTERN_OFFSETS
8540     char *parse_start;
8541 #endif
8542     const char *maxpos = NULL;
8543     GET_RE_DEBUG_FLAGS_DECL;
8544
8545     PERL_ARGS_ASSERT_REGPIECE;
8546
8547     DEBUG_PARSE("piec");
8548
8549     ret = regatom(pRExC_state, &flags,depth+1);
8550     if (ret == NULL) {
8551         if (flags & TRYAGAIN)
8552             *flagp |= TRYAGAIN;
8553         return(NULL);
8554     }
8555
8556     op = *RExC_parse;
8557
8558     if (op == '{' && regcurly(RExC_parse)) {
8559         maxpos = NULL;
8560 #ifdef RE_TRACK_PATTERN_OFFSETS
8561         parse_start = RExC_parse; /* MJD */
8562 #endif
8563         next = RExC_parse + 1;
8564         while (isDIGIT(*next) || *next == ',') {
8565             if (*next == ',') {
8566                 if (maxpos)
8567                     break;
8568                 else
8569                     maxpos = next;
8570             }
8571             next++;
8572         }
8573         if (*next == '}') {             /* got one */
8574             if (!maxpos)
8575                 maxpos = next;
8576             RExC_parse++;
8577             min = atoi(RExC_parse);
8578             if (*maxpos == ',')
8579                 maxpos++;
8580             else
8581                 maxpos = RExC_parse;
8582             max = atoi(maxpos);
8583             if (!max && *maxpos != '0')
8584                 max = REG_INFTY;                /* meaning "infinity" */
8585             else if (max >= REG_INFTY)
8586                 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
8587             RExC_parse = next;
8588             nextchar(pRExC_state);
8589
8590         do_curly:
8591             if ((flags&SIMPLE)) {
8592                 RExC_naughty += 2 + RExC_naughty / 2;
8593                 reginsert(pRExC_state, CURLY, ret, depth+1);
8594                 Set_Node_Offset(ret, parse_start+1); /* MJD */
8595                 Set_Node_Cur_Length(ret);
8596             }
8597             else {
8598                 regnode * const w = reg_node(pRExC_state, WHILEM);
8599
8600                 w->flags = 0;
8601                 REGTAIL(pRExC_state, ret, w);
8602                 if (!SIZE_ONLY && RExC_extralen) {
8603                     reginsert(pRExC_state, LONGJMP,ret, depth+1);
8604                     reginsert(pRExC_state, NOTHING,ret, depth+1);
8605                     NEXT_OFF(ret) = 3;  /* Go over LONGJMP. */
8606                 }
8607                 reginsert(pRExC_state, CURLYX,ret, depth+1);
8608                                 /* MJD hk */
8609                 Set_Node_Offset(ret, parse_start+1);
8610                 Set_Node_Length(ret,
8611                                 op == '{' ? (RExC_parse - parse_start) : 1);
8612
8613                 if (!SIZE_ONLY && RExC_extralen)
8614                     NEXT_OFF(ret) = 3;  /* Go over NOTHING to LONGJMP. */
8615                 REGTAIL(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
8616                 if (SIZE_ONLY)
8617                     RExC_whilem_seen++, RExC_extralen += 3;
8618                 RExC_naughty += 4 + RExC_naughty;       /* compound interest */
8619             }
8620             ret->flags = 0;
8621
8622             if (min > 0)
8623                 *flagp = WORST;
8624             if (max > 0)
8625                 *flagp |= HASWIDTH;
8626             if (max < min)
8627                 vFAIL("Can't do {n,m} with n > m");
8628             if (!SIZE_ONLY) {
8629                 ARG1_SET(ret, (U16)min);
8630                 ARG2_SET(ret, (U16)max);
8631             }
8632
8633             goto nest_check;
8634         }
8635     }
8636
8637     if (!ISMULT1(op)) {
8638         *flagp = flags;
8639         return(ret);
8640     }
8641
8642 #if 0                           /* Now runtime fix should be reliable. */
8643
8644     /* if this is reinstated, don't forget to put this back into perldiag:
8645
8646             =item Regexp *+ operand could be empty at {#} in regex m/%s/
8647
8648            (F) The part of the regexp subject to either the * or + quantifier
8649            could match an empty string. The {#} shows in the regular
8650            expression about where the problem was discovered.
8651
8652     */
8653
8654     if (!(flags&HASWIDTH) && op != '?')
8655       vFAIL("Regexp *+ operand could be empty");
8656 #endif
8657
8658 #ifdef RE_TRACK_PATTERN_OFFSETS
8659     parse_start = RExC_parse;
8660 #endif
8661     nextchar(pRExC_state);
8662
8663     *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
8664
8665     if (op == '*' && (flags&SIMPLE)) {
8666         reginsert(pRExC_state, STAR, ret, depth+1);
8667         ret->flags = 0;
8668         RExC_naughty += 4;
8669     }
8670     else if (op == '*') {
8671         min = 0;
8672         goto do_curly;
8673     }
8674     else if (op == '+' && (flags&SIMPLE)) {
8675         reginsert(pRExC_state, PLUS, ret, depth+1);
8676         ret->flags = 0;
8677         RExC_naughty += 3;
8678     }
8679     else if (op == '+') {
8680         min = 1;
8681         goto do_curly;
8682     }
8683     else if (op == '?') {
8684         min = 0; max = 1;
8685         goto do_curly;
8686     }
8687   nest_check:
8688     if (!SIZE_ONLY && !(flags&(HASWIDTH|POSTPONED)) && max > REG_INFTY/3) {
8689         ckWARN3reg(RExC_parse,
8690                    "%.*s matches null string many times",
8691                    (int)(RExC_parse >= origparse ? RExC_parse - origparse : 0),
8692                    origparse);
8693     }
8694
8695     if (RExC_parse < RExC_end && *RExC_parse == '?') {
8696         nextchar(pRExC_state);
8697         reginsert(pRExC_state, MINMOD, ret, depth+1);
8698         REGTAIL(pRExC_state, ret, ret + NODE_STEP_REGNODE);
8699     }
8700 #ifndef REG_ALLOW_MINMOD_SUSPEND
8701     else
8702 #endif
8703     if (RExC_parse < RExC_end && *RExC_parse == '+') {
8704         regnode *ender;
8705         nextchar(pRExC_state);
8706         ender = reg_node(pRExC_state, SUCCEED);
8707         REGTAIL(pRExC_state, ret, ender);
8708         reginsert(pRExC_state, SUSPEND, ret, depth+1);
8709         ret->flags = 0;
8710         ender = reg_node(pRExC_state, TAIL);
8711         REGTAIL(pRExC_state, ret, ender);
8712         /*ret= ender;*/
8713     }
8714
8715     if (RExC_parse < RExC_end && ISMULT2(RExC_parse)) {
8716         RExC_parse++;
8717         vFAIL("Nested quantifiers");
8718     }
8719
8720     return(ret);
8721 }
8722
8723
8724 /* reg_namedseq(pRExC_state,UVp, UV depth)
8725    
8726    This is expected to be called by a parser routine that has 
8727    recognized '\N' and needs to handle the rest. RExC_parse is
8728    expected to point at the first char following the N at the time
8729    of the call.
8730
8731    The \N may be inside (indicated by valuep not being NULL) or outside a
8732    character class.
8733
8734    \N may begin either a named sequence, or if outside a character class, mean
8735    to match a non-newline.  For non single-quoted regexes, the tokenizer has
8736    attempted to decide which, and in the case of a named sequence converted it
8737    into one of the forms: \N{} (if the sequence is null), or \N{U+c1.c2...},
8738    where c1... are the characters in the sequence.  For single-quoted regexes,
8739    the tokenizer passes the \N sequence through unchanged; this code will not
8740    attempt to determine this nor expand those.  The net effect is that if the
8741    beginning of the passed-in pattern isn't '{U+' or there is no '}', it
8742    signals that this \N occurrence means to match a non-newline.
8743    
8744    Only the \N{U+...} form should occur in a character class, for the same
8745    reason that '.' inside a character class means to just match a period: it
8746    just doesn't make sense.
8747    
8748    If valuep is non-null then it is assumed that we are parsing inside 
8749    of a charclass definition and the first codepoint in the resolved
8750    string is returned via *valuep and the routine will return NULL. 
8751    In this mode if a multichar string is returned from the charnames 
8752    handler, a warning will be issued, and only the first char in the 
8753    sequence will be examined. If the string returned is zero length
8754    then the value of *valuep is undefined and NON-NULL will 
8755    be returned to indicate failure. (This will NOT be a valid pointer 
8756    to a regnode.)
8757    
8758    If valuep is null then it is assumed that we are parsing normal text and a
8759    new EXACT node is inserted into the program containing the resolved string,
8760    and a pointer to the new node is returned.  But if the string is zero length
8761    a NOTHING node is emitted instead.
8762
8763    On success RExC_parse is set to the char following the endbrace.
8764    Parsing failures will generate a fatal error via vFAIL(...)
8765  */
8766 STATIC regnode *
8767 S_reg_namedseq(pTHX_ RExC_state_t *pRExC_state, UV *valuep, I32 *flagp, U32 depth)
8768 {
8769     char * endbrace;    /* '}' following the name */
8770     regnode *ret = NULL;
8771     char* p;
8772
8773     GET_RE_DEBUG_FLAGS_DECL;
8774  
8775     PERL_ARGS_ASSERT_REG_NAMEDSEQ;
8776
8777     GET_RE_DEBUG_FLAGS;
8778
8779     /* The [^\n] meaning of \N ignores spaces and comments under the /x
8780      * modifier.  The other meaning does not */
8781     p = (RExC_flags & RXf_PMf_EXTENDED)
8782         ? regwhite( pRExC_state, RExC_parse )
8783         : RExC_parse;
8784    
8785     /* Disambiguate between \N meaning a named character versus \N meaning
8786      * [^\n].  The former is assumed when it can't be the latter. */
8787     if (*p != '{' || regcurly(p)) {
8788         RExC_parse = p;
8789         if (valuep) {
8790             /* no bare \N in a charclass */
8791             vFAIL("\\N in a character class must be a named character: \\N{...}");
8792         }
8793         nextchar(pRExC_state);
8794         ret = reg_node(pRExC_state, REG_ANY);
8795         *flagp |= HASWIDTH|SIMPLE;
8796         RExC_naughty++;
8797         RExC_parse--;
8798         Set_Node_Length(ret, 1); /* MJD */
8799         return ret;
8800     }
8801
8802     /* Here, we have decided it should be a named sequence */
8803
8804     /* The test above made sure that the next real character is a '{', but
8805      * under the /x modifier, it could be separated by space (or a comment and
8806      * \n) and this is not allowed (for consistency with \x{...} and the
8807      * tokenizer handling of \N{NAME}). */
8808     if (*RExC_parse != '{') {
8809         vFAIL("Missing braces on \\N{}");
8810     }
8811
8812     RExC_parse++;       /* Skip past the '{' */
8813
8814     if (! (endbrace = strchr(RExC_parse, '}')) /* no trailing brace */
8815         || ! (endbrace == RExC_parse            /* nothing between the {} */
8816               || (endbrace - RExC_parse >= 2    /* U+ (bad hex is checked below */
8817                   && strnEQ(RExC_parse, "U+", 2)))) /* for a better error msg) */
8818     {
8819         if (endbrace) RExC_parse = endbrace;    /* position msg's '<--HERE' */
8820         vFAIL("\\N{NAME} must be resolved by the lexer");
8821     }
8822
8823     if (endbrace == RExC_parse) {   /* empty: \N{} */
8824         if (! valuep) {
8825             RExC_parse = endbrace + 1;  
8826             return reg_node(pRExC_state,NOTHING);
8827         }
8828
8829         if (SIZE_ONLY) {
8830             ckWARNreg(RExC_parse,
8831                     "Ignoring zero length \\N{} in character class"
8832             );
8833             RExC_parse = endbrace + 1;  
8834         }
8835         *valuep = 0;
8836         return (regnode *) &RExC_parse; /* Invalid regnode pointer */
8837     }
8838
8839     REQUIRE_UTF8;       /* named sequences imply Unicode semantics */
8840     RExC_parse += 2;    /* Skip past the 'U+' */
8841
8842     if (valuep) {   /* In a bracketed char class */
8843         /* We only pay attention to the first char of 
8844         multichar strings being returned. I kinda wonder
8845         if this makes sense as it does change the behaviour
8846         from earlier versions, OTOH that behaviour was broken
8847         as well. XXX Solution is to recharacterize as
8848         [rest-of-class]|multi1|multi2... */
8849
8850         STRLEN length_of_hex;
8851         I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
8852             | PERL_SCAN_DISALLOW_PREFIX
8853             | (SIZE_ONLY ? PERL_SCAN_SILENT_ILLDIGIT : 0);
8854     
8855         char * endchar = RExC_parse + strcspn(RExC_parse, ".}");
8856         if (endchar < endbrace) {
8857             ckWARNreg(endchar, "Using just the first character returned by \\N{} in character class");
8858         }
8859
8860         length_of_hex = (STRLEN)(endchar - RExC_parse);
8861         *valuep = grok_hex(RExC_parse, &length_of_hex, &flags, NULL);
8862
8863         /* The tokenizer should have guaranteed validity, but it's possible to
8864          * bypass it by using single quoting, so check */
8865         if (length_of_hex == 0
8866             || length_of_hex != (STRLEN)(endchar - RExC_parse) )
8867         {
8868             RExC_parse += length_of_hex;        /* Includes all the valid */
8869             RExC_parse += (RExC_orig_utf8)      /* point to after 1st invalid */
8870                             ? UTF8SKIP(RExC_parse)
8871                             : 1;
8872             /* Guard against malformed utf8 */
8873             if (RExC_parse >= endchar) RExC_parse = endchar;
8874             vFAIL("Invalid hexadecimal number in \\N{U+...}");
8875         }    
8876
8877         RExC_parse = endbrace + 1;
8878         if (endchar == endbrace) return NULL;
8879
8880         ret = (regnode *) &RExC_parse;  /* Invalid regnode pointer */
8881     }
8882     else {      /* Not a char class */
8883
8884         /* What is done here is to convert this to a sub-pattern of the form
8885          * (?:\x{char1}\x{char2}...)
8886          * and then call reg recursively.  That way, it retains its atomicness,
8887          * while not having to worry about special handling that some code
8888          * points may have.  toke.c has converted the original Unicode values
8889          * to native, so that we can just pass on the hex values unchanged.  We
8890          * do have to set a flag to keep recoding from happening in the
8891          * recursion */
8892
8893         SV * substitute_parse = newSVpvn_flags("?:", 2, SVf_UTF8|SVs_TEMP);
8894         STRLEN len;
8895         char *endchar;      /* Points to '.' or '}' ending cur char in the input
8896                                stream */
8897         char *orig_end = RExC_end;
8898
8899         while (RExC_parse < endbrace) {
8900
8901             /* Code points are separated by dots.  If none, there is only one
8902              * code point, and is terminated by the brace */
8903             endchar = RExC_parse + strcspn(RExC_parse, ".}");
8904
8905             /* Convert to notation the rest of the code understands */
8906             sv_catpv(substitute_parse, "\\x{");
8907             sv_catpvn(substitute_parse, RExC_parse, endchar - RExC_parse);
8908             sv_catpv(substitute_parse, "}");
8909
8910             /* Point to the beginning of the next character in the sequence. */
8911             RExC_parse = endchar + 1;
8912         }
8913         sv_catpv(substitute_parse, ")");
8914
8915         RExC_parse = SvPV(substitute_parse, len);
8916
8917         /* Don't allow empty number */
8918         if (len < 8) {
8919             vFAIL("Invalid hexadecimal number in \\N{U+...}");
8920         }
8921         RExC_end = RExC_parse + len;
8922
8923         /* The values are Unicode, and therefore not subject to recoding */
8924         RExC_override_recoding = 1;
8925
8926         ret = reg(pRExC_state, 1, flagp, depth+1);
8927
8928         RExC_parse = endbrace;
8929         RExC_end = orig_end;
8930         RExC_override_recoding = 0;
8931
8932         nextchar(pRExC_state);
8933     }
8934
8935     return ret;
8936 }
8937
8938
8939 /*
8940  * reg_recode
8941  *
8942  * It returns the code point in utf8 for the value in *encp.
8943  *    value: a code value in the source encoding
8944  *    encp:  a pointer to an Encode object
8945  *
8946  * If the result from Encode is not a single character,
8947  * it returns U+FFFD (Replacement character) and sets *encp to NULL.
8948  */
8949 STATIC UV
8950 S_reg_recode(pTHX_ const char value, SV **encp)
8951 {
8952     STRLEN numlen = 1;
8953     SV * const sv = newSVpvn_flags(&value, numlen, SVs_TEMP);
8954     const char * const s = *encp ? sv_recode_to_utf8(sv, *encp) : SvPVX(sv);
8955     const STRLEN newlen = SvCUR(sv);
8956     UV uv = UNICODE_REPLACEMENT;
8957
8958     PERL_ARGS_ASSERT_REG_RECODE;
8959
8960     if (newlen)
8961         uv = SvUTF8(sv)
8962              ? utf8n_to_uvchr((U8*)s, newlen, &numlen, UTF8_ALLOW_DEFAULT)
8963              : *(U8*)s;
8964
8965     if (!newlen || numlen != newlen) {
8966         uv = UNICODE_REPLACEMENT;
8967         *encp = NULL;
8968     }
8969     return uv;
8970 }
8971
8972
8973 /*
8974  - regatom - the lowest level
8975
8976    Try to identify anything special at the start of the pattern. If there
8977    is, then handle it as required. This may involve generating a single regop,
8978    such as for an assertion; or it may involve recursing, such as to
8979    handle a () structure.
8980
8981    If the string doesn't start with something special then we gobble up
8982    as much literal text as we can.
8983
8984    Once we have been able to handle whatever type of thing started the
8985    sequence, we return.
8986
8987    Note: we have to be careful with escapes, as they can be both literal
8988    and special, and in the case of \10 and friends can either, depending
8989    on context. Specifically there are two separate switches for handling
8990    escape sequences, with the one for handling literal escapes requiring
8991    a dummy entry for all of the special escapes that are actually handled
8992    by the other.
8993 */
8994
8995 STATIC regnode *
8996 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, U32 depth)
8997 {
8998     dVAR;
8999     register regnode *ret = NULL;
9000     I32 flags;
9001     char *parse_start = RExC_parse;
9002     U8 op;
9003     GET_RE_DEBUG_FLAGS_DECL;
9004     DEBUG_PARSE("atom");
9005     *flagp = WORST;             /* Tentatively. */
9006
9007     PERL_ARGS_ASSERT_REGATOM;
9008
9009 tryagain:
9010     switch ((U8)*RExC_parse) {
9011     case '^':
9012         RExC_seen_zerolen++;
9013         nextchar(pRExC_state);
9014         if (RExC_flags & RXf_PMf_MULTILINE)
9015             ret = reg_node(pRExC_state, MBOL);
9016         else if (RExC_flags & RXf_PMf_SINGLELINE)
9017             ret = reg_node(pRExC_state, SBOL);
9018         else
9019             ret = reg_node(pRExC_state, BOL);
9020         Set_Node_Length(ret, 1); /* MJD */
9021         break;
9022     case '$':
9023         nextchar(pRExC_state);
9024         if (*RExC_parse)
9025             RExC_seen_zerolen++;
9026         if (RExC_flags & RXf_PMf_MULTILINE)
9027             ret = reg_node(pRExC_state, MEOL);
9028         else if (RExC_flags & RXf_PMf_SINGLELINE)
9029             ret = reg_node(pRExC_state, SEOL);
9030         else
9031             ret = reg_node(pRExC_state, EOL);
9032         Set_Node_Length(ret, 1); /* MJD */
9033         break;
9034     case '.':
9035         nextchar(pRExC_state);
9036         if (RExC_flags & RXf_PMf_SINGLELINE)
9037             ret = reg_node(pRExC_state, SANY);
9038         else
9039             ret = reg_node(pRExC_state, REG_ANY);
9040         *flagp |= HASWIDTH|SIMPLE;
9041         RExC_naughty++;
9042         Set_Node_Length(ret, 1); /* MJD */
9043         break;
9044     case '[':
9045     {
9046         char * const oregcomp_parse = ++RExC_parse;
9047         ret = regclass(pRExC_state,depth+1);
9048         if (*RExC_parse != ']') {
9049             RExC_parse = oregcomp_parse;
9050             vFAIL("Unmatched [");
9051         }
9052         nextchar(pRExC_state);
9053         *flagp |= HASWIDTH|SIMPLE;
9054         Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */
9055         break;
9056     }
9057     case '(':
9058         nextchar(pRExC_state);
9059         ret = reg(pRExC_state, 1, &flags,depth+1);
9060         if (ret == NULL) {
9061                 if (flags & TRYAGAIN) {
9062                     if (RExC_parse == RExC_end) {
9063                          /* Make parent create an empty node if needed. */
9064                         *flagp |= TRYAGAIN;
9065                         return(NULL);
9066                     }
9067                     goto tryagain;
9068                 }
9069                 return(NULL);
9070         }
9071         *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE|POSTPONED);
9072         break;
9073     case '|':
9074     case ')':
9075         if (flags & TRYAGAIN) {
9076             *flagp |= TRYAGAIN;
9077             return NULL;
9078         }
9079         vFAIL("Internal urp");
9080                                 /* Supposed to be caught earlier. */
9081         break;
9082     case '?':
9083     case '+':
9084     case '*':
9085         RExC_parse++;
9086         vFAIL("Quantifier follows nothing");
9087         break;
9088     case '\\':
9089         /* Special Escapes
9090
9091            This switch handles escape sequences that resolve to some kind
9092            of special regop and not to literal text. Escape sequnces that
9093            resolve to literal text are handled below in the switch marked
9094            "Literal Escapes".
9095
9096            Every entry in this switch *must* have a corresponding entry
9097            in the literal escape switch. However, the opposite is not
9098            required, as the default for this switch is to jump to the
9099            literal text handling code.
9100         */
9101         switch ((U8)*++RExC_parse) {
9102         /* Special Escapes */
9103         case 'A':
9104             RExC_seen_zerolen++;
9105             ret = reg_node(pRExC_state, SBOL);
9106             *flagp |= SIMPLE;
9107             goto finish_meta_pat;
9108         case 'G':
9109             ret = reg_node(pRExC_state, GPOS);
9110             RExC_seen |= REG_SEEN_GPOS;
9111             *flagp |= SIMPLE;
9112             goto finish_meta_pat;
9113         case 'K':
9114             RExC_seen_zerolen++;
9115             ret = reg_node(pRExC_state, KEEPS);
9116             *flagp |= SIMPLE;
9117             /* XXX:dmq : disabling in-place substitution seems to
9118              * be necessary here to avoid cases of memory corruption, as
9119              * with: C<$_="x" x 80; s/x\K/y/> -- rgs
9120              */
9121             RExC_seen |= REG_SEEN_LOOKBEHIND;
9122             goto finish_meta_pat;
9123         case 'Z':
9124             ret = reg_node(pRExC_state, SEOL);
9125             *flagp |= SIMPLE;
9126             RExC_seen_zerolen++;                /* Do not optimize RE away */
9127             goto finish_meta_pat;
9128         case 'z':
9129             ret = reg_node(pRExC_state, EOS);
9130             *flagp |= SIMPLE;
9131             RExC_seen_zerolen++;                /* Do not optimize RE away */
9132             goto finish_meta_pat;
9133         case 'C':
9134             ret = reg_node(pRExC_state, CANY);
9135             RExC_seen |= REG_SEEN_CANY;
9136             *flagp |= HASWIDTH|SIMPLE;
9137             goto finish_meta_pat;
9138         case 'X':
9139             ret = reg_node(pRExC_state, CLUMP);
9140             *flagp |= HASWIDTH;
9141             goto finish_meta_pat;
9142         case 'w':
9143             switch (get_regex_charset(RExC_flags)) {
9144                 case REGEX_LOCALE_CHARSET:
9145                     op = ALNUML;
9146                     break;
9147                 case REGEX_UNICODE_CHARSET:
9148                     op = ALNUMU;
9149                     break;
9150                 case REGEX_ASCII_RESTRICTED_CHARSET:
9151                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9152                     op = ALNUMA;
9153                     break;
9154                 case REGEX_DEPENDS_CHARSET:
9155                     op = ALNUM;
9156                     break;
9157                 default:
9158                     goto bad_charset;
9159             }
9160             ret = reg_node(pRExC_state, op);
9161             *flagp |= HASWIDTH|SIMPLE;
9162             goto finish_meta_pat;
9163         case 'W':
9164             switch (get_regex_charset(RExC_flags)) {
9165                 case REGEX_LOCALE_CHARSET:
9166                     op = NALNUML;
9167                     break;
9168                 case REGEX_UNICODE_CHARSET:
9169                     op = NALNUMU;
9170                     break;
9171                 case REGEX_ASCII_RESTRICTED_CHARSET:
9172                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9173                     op = NALNUMA;
9174                     break;
9175                 case REGEX_DEPENDS_CHARSET:
9176                     op = NALNUM;
9177                     break;
9178                 default:
9179                     goto bad_charset;
9180             }
9181             ret = reg_node(pRExC_state, op);
9182             *flagp |= HASWIDTH|SIMPLE;
9183             goto finish_meta_pat;
9184         case 'b':
9185             RExC_seen_zerolen++;
9186             RExC_seen |= REG_SEEN_LOOKBEHIND;
9187             switch (get_regex_charset(RExC_flags)) {
9188                 case REGEX_LOCALE_CHARSET:
9189                     op = BOUNDL;
9190                     break;
9191                 case REGEX_UNICODE_CHARSET:
9192                     op = BOUNDU;
9193                     break;
9194                 case REGEX_ASCII_RESTRICTED_CHARSET:
9195                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9196                     op = BOUNDA;
9197                     break;
9198                 case REGEX_DEPENDS_CHARSET:
9199                     op = BOUND;
9200                     break;
9201                 default:
9202                     goto bad_charset;
9203             }
9204             ret = reg_node(pRExC_state, op);
9205             FLAGS(ret) = get_regex_charset(RExC_flags);
9206             *flagp |= SIMPLE;
9207             goto finish_meta_pat;
9208         case 'B':
9209             RExC_seen_zerolen++;
9210             RExC_seen |= REG_SEEN_LOOKBEHIND;
9211             switch (get_regex_charset(RExC_flags)) {
9212                 case REGEX_LOCALE_CHARSET:
9213                     op = NBOUNDL;
9214                     break;
9215                 case REGEX_UNICODE_CHARSET:
9216                     op = NBOUNDU;
9217                     break;
9218                 case REGEX_ASCII_RESTRICTED_CHARSET:
9219                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9220                     op = NBOUNDA;
9221                     break;
9222                 case REGEX_DEPENDS_CHARSET:
9223                     op = NBOUND;
9224                     break;
9225                 default:
9226                     goto bad_charset;
9227             }
9228             ret = reg_node(pRExC_state, op);
9229             FLAGS(ret) = get_regex_charset(RExC_flags);
9230             *flagp |= SIMPLE;
9231             goto finish_meta_pat;
9232         case 's':
9233             switch (get_regex_charset(RExC_flags)) {
9234                 case REGEX_LOCALE_CHARSET:
9235                     op = SPACEL;
9236                     break;
9237                 case REGEX_UNICODE_CHARSET:
9238                     op = SPACEU;
9239                     break;
9240                 case REGEX_ASCII_RESTRICTED_CHARSET:
9241                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9242                     op = SPACEA;
9243                     break;
9244                 case REGEX_DEPENDS_CHARSET:
9245                     op = SPACE;
9246                     break;
9247                 default:
9248                     goto bad_charset;
9249             }
9250             ret = reg_node(pRExC_state, op);
9251             *flagp |= HASWIDTH|SIMPLE;
9252             goto finish_meta_pat;
9253         case 'S':
9254             switch (get_regex_charset(RExC_flags)) {
9255                 case REGEX_LOCALE_CHARSET:
9256                     op = NSPACEL;
9257                     break;
9258                 case REGEX_UNICODE_CHARSET:
9259                     op = NSPACEU;
9260                     break;
9261                 case REGEX_ASCII_RESTRICTED_CHARSET:
9262                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9263                     op = NSPACEA;
9264                     break;
9265                 case REGEX_DEPENDS_CHARSET:
9266                     op = NSPACE;
9267                     break;
9268                 default:
9269                     goto bad_charset;
9270             }
9271             ret = reg_node(pRExC_state, op);
9272             *flagp |= HASWIDTH|SIMPLE;
9273             goto finish_meta_pat;
9274         case 'd':
9275             switch (get_regex_charset(RExC_flags)) {
9276                 case REGEX_LOCALE_CHARSET:
9277                     op = DIGITL;
9278                     break;
9279                 case REGEX_ASCII_RESTRICTED_CHARSET:
9280                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9281                     op = DIGITA;
9282                     break;
9283                 case REGEX_DEPENDS_CHARSET: /* No difference between these */
9284                 case REGEX_UNICODE_CHARSET:
9285                     op = DIGIT;
9286                     break;
9287                 default:
9288                     goto bad_charset;
9289             }
9290             ret = reg_node(pRExC_state, op);
9291             *flagp |= HASWIDTH|SIMPLE;
9292             goto finish_meta_pat;
9293         case 'D':
9294             switch (get_regex_charset(RExC_flags)) {
9295                 case REGEX_LOCALE_CHARSET:
9296                     op = NDIGITL;
9297                     break;
9298                 case REGEX_ASCII_RESTRICTED_CHARSET:
9299                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
9300                     op = NDIGITA;
9301                     break;
9302                 case REGEX_DEPENDS_CHARSET: /* No difference between these */
9303                 case REGEX_UNICODE_CHARSET:
9304                     op = NDIGIT;
9305                     break;
9306                 default:
9307                     goto bad_charset;
9308             }
9309             ret = reg_node(pRExC_state, op);
9310             *flagp |= HASWIDTH|SIMPLE;
9311             goto finish_meta_pat;
9312         case 'R':
9313             ret = reg_node(pRExC_state, LNBREAK);
9314             *flagp |= HASWIDTH|SIMPLE;
9315             goto finish_meta_pat;
9316         case 'h':
9317             ret = reg_node(pRExC_state, HORIZWS);
9318             *flagp |= HASWIDTH|SIMPLE;
9319             goto finish_meta_pat;
9320         case 'H':
9321             ret = reg_node(pRExC_state, NHORIZWS);
9322             *flagp |= HASWIDTH|SIMPLE;
9323             goto finish_meta_pat;
9324         case 'v':
9325             ret = reg_node(pRExC_state, VERTWS);
9326             *flagp |= HASWIDTH|SIMPLE;
9327             goto finish_meta_pat;
9328         case 'V':
9329             ret = reg_node(pRExC_state, NVERTWS);
9330             *flagp |= HASWIDTH|SIMPLE;
9331          finish_meta_pat:           
9332             nextchar(pRExC_state);
9333             Set_Node_Length(ret, 2); /* MJD */
9334             break;          
9335         case 'p':
9336         case 'P':
9337             {
9338                 char* const oldregxend = RExC_end;
9339 #ifdef DEBUGGING
9340                 char* parse_start = RExC_parse - 2;
9341 #endif
9342
9343                 if (RExC_parse[1] == '{') {
9344                   /* a lovely hack--pretend we saw [\pX] instead */
9345                     RExC_end = strchr(RExC_parse, '}');
9346                     if (!RExC_end) {
9347                         const U8 c = (U8)*RExC_parse;
9348                         RExC_parse += 2;
9349                         RExC_end = oldregxend;
9350                         vFAIL2("Missing right brace on \\%c{}", c);
9351                     }
9352                     RExC_end++;
9353                 }
9354                 else {
9355                     RExC_end = RExC_parse + 2;
9356                     if (RExC_end > oldregxend)
9357                         RExC_end = oldregxend;
9358                 }
9359                 RExC_parse--;
9360
9361                 ret = regclass(pRExC_state,depth+1);
9362
9363                 RExC_end = oldregxend;
9364                 RExC_parse--;
9365
9366                 Set_Node_Offset(ret, parse_start + 2);
9367                 Set_Node_Cur_Length(ret);
9368                 nextchar(pRExC_state);
9369                 *flagp |= HASWIDTH|SIMPLE;
9370             }
9371             break;
9372         case 'N': 
9373             /* Handle \N and \N{NAME} here and not below because it can be
9374             multicharacter. join_exact() will join them up later on. 
9375             Also this makes sure that things like /\N{BLAH}+/ and 
9376             \N{BLAH} being multi char Just Happen. dmq*/
9377             ++RExC_parse;
9378             ret= reg_namedseq(pRExC_state, NULL, flagp, depth);
9379             break;
9380         case 'k':    /* Handle \k<NAME> and \k'NAME' */
9381         parse_named_seq:
9382         {   
9383             char ch= RExC_parse[1];         
9384             if (ch != '<' && ch != '\'' && ch != '{') {
9385                 RExC_parse++;
9386                 vFAIL2("Sequence %.2s... not terminated",parse_start);
9387             } else {
9388                 /* this pretty much dupes the code for (?P=...) in reg(), if
9389                    you change this make sure you change that */
9390                 char* name_start = (RExC_parse += 2);
9391                 U32 num = 0;
9392                 SV *sv_dat = reg_scan_name(pRExC_state,
9393                     SIZE_ONLY ? REG_RSN_RETURN_NULL : REG_RSN_RETURN_DATA);
9394                 ch= (ch == '<') ? '>' : (ch == '{') ? '}' : '\'';
9395                 if (RExC_parse == name_start || *RExC_parse != ch)
9396                     vFAIL2("Sequence %.3s... not terminated",parse_start);
9397
9398                 if (!SIZE_ONLY) {
9399                     num = add_data( pRExC_state, 1, "S" );
9400                     RExC_rxi->data->data[num]=(void*)sv_dat;
9401                     SvREFCNT_inc_simple_void(sv_dat);
9402                 }
9403
9404                 RExC_sawback = 1;
9405                 ret = reganode(pRExC_state,
9406                                ((! FOLD)
9407                                  ? NREF
9408                                  : (MORE_ASCII_RESTRICTED)
9409                                    ? NREFFA
9410                                    : (AT_LEAST_UNI_SEMANTICS)
9411                                      ? NREFFU
9412                                      : (LOC)
9413                                        ? NREFFL
9414                                        : NREFF),
9415                                 num);
9416                 *flagp |= HASWIDTH;
9417
9418                 /* override incorrect value set in reganode MJD */
9419                 Set_Node_Offset(ret, parse_start+1);
9420                 Set_Node_Cur_Length(ret); /* MJD */
9421                 nextchar(pRExC_state);
9422
9423             }
9424             break;
9425         }
9426         case 'g': 
9427         case '1': case '2': case '3': case '4':
9428         case '5': case '6': case '7': case '8': case '9':
9429             {
9430                 I32 num;
9431                 bool isg = *RExC_parse == 'g';
9432                 bool isrel = 0; 
9433                 bool hasbrace = 0;
9434                 if (isg) {
9435                     RExC_parse++;
9436                     if (*RExC_parse == '{') {
9437                         RExC_parse++;
9438                         hasbrace = 1;
9439                     }
9440                     if (*RExC_parse == '-') {
9441                         RExC_parse++;
9442                         isrel = 1;
9443                     }
9444                     if (hasbrace && !isDIGIT(*RExC_parse)) {
9445                         if (isrel) RExC_parse--;
9446                         RExC_parse -= 2;                            
9447                         goto parse_named_seq;
9448                 }   }
9449                 num = atoi(RExC_parse);
9450                 if (isg && num == 0)
9451                     vFAIL("Reference to invalid group 0");
9452                 if (isrel) {
9453                     num = RExC_npar - num;
9454                     if (num < 1)
9455                         vFAIL("Reference to nonexistent or unclosed group");
9456                 }
9457                 if (!isg && num > 9 && num >= RExC_npar)
9458                     goto defchar;
9459                 else {
9460                     char * const parse_start = RExC_parse - 1; /* MJD */
9461                     while (isDIGIT(*RExC_parse))
9462                         RExC_parse++;
9463                     if (parse_start == RExC_parse - 1) 
9464                         vFAIL("Unterminated \\g... pattern");
9465                     if (hasbrace) {
9466                         if (*RExC_parse != '}') 
9467                             vFAIL("Unterminated \\g{...} pattern");
9468                         RExC_parse++;
9469                     }    
9470                     if (!SIZE_ONLY) {
9471                         if (num > (I32)RExC_rx->nparens)
9472                             vFAIL("Reference to nonexistent group");
9473                     }
9474                     RExC_sawback = 1;
9475                     ret = reganode(pRExC_state,
9476                                    ((! FOLD)
9477                                      ? REF
9478                                      : (MORE_ASCII_RESTRICTED)
9479                                        ? REFFA
9480                                        : (AT_LEAST_UNI_SEMANTICS)
9481                                          ? REFFU
9482                                          : (LOC)
9483                                            ? REFFL
9484                                            : REFF),
9485                                     num);
9486                     *flagp |= HASWIDTH;
9487
9488                     /* override incorrect value set in reganode MJD */
9489                     Set_Node_Offset(ret, parse_start+1);
9490                     Set_Node_Cur_Length(ret); /* MJD */
9491                     RExC_parse--;
9492                     nextchar(pRExC_state);
9493                 }
9494             }
9495             break;
9496         case '\0':
9497             if (RExC_parse >= RExC_end)
9498                 FAIL("Trailing \\");
9499             /* FALL THROUGH */
9500         default:
9501             /* Do not generate "unrecognized" warnings here, we fall
9502                back into the quick-grab loop below */
9503             parse_start--;
9504             goto defchar;
9505         }
9506         break;
9507
9508     case '#':
9509         if (RExC_flags & RXf_PMf_EXTENDED) {
9510             if ( reg_skipcomment( pRExC_state ) )
9511                 goto tryagain;
9512         }
9513         /* FALL THROUGH */
9514
9515     default:
9516
9517             parse_start = RExC_parse - 1;
9518
9519             RExC_parse++;
9520
9521         defchar: {
9522             register STRLEN len;
9523             register UV ender;
9524             register char *p;
9525             char *s;
9526             STRLEN foldlen;
9527             U8 tmpbuf[UTF8_MAXBYTES_CASE+1], *foldbuf;
9528             U8 node_type;
9529
9530             /* Is this a LATIN LOWER CASE SHARP S in an EXACTFU node?  If so,
9531              * it is folded to 'ss' even if not utf8 */
9532             bool is_exactfu_sharp_s;
9533
9534             ender = 0;
9535             node_type = ((! FOLD) ? EXACT
9536                         : (LOC)
9537                           ? EXACTFL
9538                           : (MORE_ASCII_RESTRICTED)
9539                             ? EXACTFA
9540                             : (AT_LEAST_UNI_SEMANTICS)
9541                               ? EXACTFU
9542                               : EXACTF);
9543             ret = reg_node(pRExC_state, node_type);
9544             s = STRING(ret);
9545
9546             /* XXX The node can hold up to 255 bytes, yet this only goes to
9547              * 127.  I (khw) do not know why.  Keeping it somewhat less than
9548              * 255 allows us to not have to worry about overflow due to
9549              * converting to utf8 and fold expansion, but that value is
9550              * 255-UTF8_MAXBYTES_CASE.  join_exact() may join adjacent nodes
9551              * split up by this limit into a single one using the real max of
9552              * 255.  Even at 127, this breaks under rare circumstances.  If
9553              * folding, we do not want to split a node at a character that is a
9554              * non-final in a multi-char fold, as an input string could just
9555              * happen to want to match across the node boundary.  The join
9556              * would solve that problem if the join actually happens.  But a
9557              * series of more than two nodes in a row each of 127 would cause
9558              * the first join to succeed to get to 254, but then there wouldn't
9559              * be room for the next one, which could at be one of those split
9560              * multi-char folds.  I don't know of any fool-proof solution.  One
9561              * could back off to end with only a code point that isn't such a
9562              * non-final, but it is possible for there not to be any in the
9563              * entire node. */
9564             for (len = 0, p = RExC_parse - 1;
9565                  len < 127 && p < RExC_end;
9566                  len++)
9567             {
9568                 char * const oldp = p;
9569
9570                 if (RExC_flags & RXf_PMf_EXTENDED)
9571                     p = regwhite( pRExC_state, p );
9572                 switch ((U8)*p) {
9573                 case '^':
9574                 case '$':
9575                 case '.':
9576                 case '[':
9577                 case '(':
9578                 case ')':
9579                 case '|':
9580                     goto loopdone;
9581                 case '\\':
9582                     /* Literal Escapes Switch
9583
9584                        This switch is meant to handle escape sequences that
9585                        resolve to a literal character.
9586
9587                        Every escape sequence that represents something
9588                        else, like an assertion or a char class, is handled
9589                        in the switch marked 'Special Escapes' above in this
9590                        routine, but also has an entry here as anything that
9591                        isn't explicitly mentioned here will be treated as
9592                        an unescaped equivalent literal.
9593                     */
9594
9595                     switch ((U8)*++p) {
9596                     /* These are all the special escapes. */
9597                     case 'A':             /* Start assertion */
9598                     case 'b': case 'B':   /* Word-boundary assertion*/
9599                     case 'C':             /* Single char !DANGEROUS! */
9600                     case 'd': case 'D':   /* digit class */
9601                     case 'g': case 'G':   /* generic-backref, pos assertion */
9602                     case 'h': case 'H':   /* HORIZWS */
9603                     case 'k': case 'K':   /* named backref, keep marker */
9604                     case 'N':             /* named char sequence */
9605                     case 'p': case 'P':   /* Unicode property */
9606                               case 'R':   /* LNBREAK */
9607                     case 's': case 'S':   /* space class */
9608                     case 'v': case 'V':   /* VERTWS */
9609                     case 'w': case 'W':   /* word class */
9610                     case 'X':             /* eXtended Unicode "combining character sequence" */
9611                     case 'z': case 'Z':   /* End of line/string assertion */
9612                         --p;
9613                         goto loopdone;
9614
9615                     /* Anything after here is an escape that resolves to a
9616                        literal. (Except digits, which may or may not)
9617                      */
9618                     case 'n':
9619                         ender = '\n';
9620                         p++;
9621                         break;
9622                     case 'r':
9623                         ender = '\r';
9624                         p++;
9625                         break;
9626                     case 't':
9627                         ender = '\t';
9628                         p++;
9629                         break;
9630                     case 'f':
9631                         ender = '\f';
9632                         p++;
9633                         break;
9634                     case 'e':
9635                           ender = ASCII_TO_NATIVE('\033');
9636                         p++;
9637                         break;
9638                     case 'a':
9639                           ender = ASCII_TO_NATIVE('\007');
9640                         p++;
9641                         break;
9642                     case 'o':
9643                         {
9644                             STRLEN brace_len = len;
9645                             UV result;
9646                             const char* error_msg;
9647
9648                             bool valid = grok_bslash_o(p,
9649                                                        &result,
9650                                                        &brace_len,
9651                                                        &error_msg,
9652                                                        1);
9653                             p += brace_len;
9654                             if (! valid) {
9655                                 RExC_parse = p; /* going to die anyway; point
9656                                                    to exact spot of failure */
9657                                 vFAIL(error_msg);
9658                             }
9659                             else
9660                             {
9661                                 ender = result;
9662                             }
9663                             if (PL_encoding && ender < 0x100) {
9664                                 goto recode_encoding;
9665                             }
9666                             if (ender > 0xff) {
9667                                 REQUIRE_UTF8;
9668                             }
9669                             break;
9670                         }
9671                     case 'x':
9672                         if (*++p == '{') {
9673                             char* const e = strchr(p, '}');
9674
9675                             if (!e) {
9676                                 RExC_parse = p + 1;
9677                                 vFAIL("Missing right brace on \\x{}");
9678                             }
9679                             else {
9680                                 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
9681                                     | PERL_SCAN_DISALLOW_PREFIX;
9682                                 STRLEN numlen = e - p - 1;
9683                                 ender = grok_hex(p + 1, &numlen, &flags, NULL);
9684                                 if (ender > 0xff)
9685                                     REQUIRE_UTF8;
9686                                 p = e + 1;
9687                             }
9688                         }
9689                         else {
9690                             I32 flags = PERL_SCAN_DISALLOW_PREFIX;
9691                             STRLEN numlen = 2;
9692                             ender = grok_hex(p, &numlen, &flags, NULL);
9693                             p += numlen;
9694                         }
9695                         if (PL_encoding && ender < 0x100)
9696                             goto recode_encoding;
9697                         break;
9698                     case 'c':
9699                         p++;
9700                         ender = grok_bslash_c(*p++, UTF, SIZE_ONLY);
9701                         break;
9702                     case '0': case '1': case '2': case '3':case '4':
9703                     case '5': case '6': case '7': case '8':case '9':
9704                         if (*p == '0' ||
9705                             (isDIGIT(p[1]) && atoi(p) >= RExC_npar))
9706                         {
9707                             I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
9708                             STRLEN numlen = 3;
9709                             ender = grok_oct(p, &numlen, &flags, NULL);
9710                             if (ender > 0xff) {
9711                                 REQUIRE_UTF8;
9712                             }
9713                             p += numlen;
9714                         }
9715                         else {
9716                             --p;
9717                             goto loopdone;
9718                         }
9719                         if (PL_encoding && ender < 0x100)
9720                             goto recode_encoding;
9721                         break;
9722                     recode_encoding:
9723                         if (! RExC_override_recoding) {
9724                             SV* enc = PL_encoding;
9725                             ender = reg_recode((const char)(U8)ender, &enc);
9726                             if (!enc && SIZE_ONLY)
9727                                 ckWARNreg(p, "Invalid escape in the specified encoding");
9728                             REQUIRE_UTF8;
9729                         }
9730                         break;
9731                     case '\0':
9732                         if (p >= RExC_end)
9733                             FAIL("Trailing \\");
9734                         /* FALL THROUGH */
9735                     default:
9736                         if (!SIZE_ONLY&& isALPHA(*p)) {
9737                             ckWARN2reg(p + 1, "Unrecognized escape \\%.1s passed through", p);
9738                         }
9739                         goto normal_default;
9740                     }
9741                     break;
9742                 case '{':
9743                     /* Currently we don't warn when the lbrace is at the start
9744                      * of a construct.  This catches it in the middle of a
9745                      * literal string, or when its the first thing after
9746                      * something like "\b" */
9747                     if (! SIZE_ONLY
9748                         && (len || (p > RExC_start && isALPHA_A(*(p -1)))))
9749                     {
9750                         ckWARNregdep(p + 1, "Unescaped left brace in regex is deprecated, passed through");
9751                     }
9752                     /*FALLTHROUGH*/
9753                 default:
9754                   normal_default:
9755                     if (UTF8_IS_START(*p) && UTF) {
9756                         STRLEN numlen;
9757                         ender = utf8n_to_uvchr((U8*)p, RExC_end - p,
9758                                                &numlen, UTF8_ALLOW_DEFAULT);
9759                         p += numlen;
9760                     }
9761                     else
9762                         ender = (U8) *p++;
9763                     break;
9764                 } /* End of switch on the literal */
9765
9766                 is_exactfu_sharp_s = (node_type == EXACTFU
9767                                       && ender == LATIN_SMALL_LETTER_SHARP_S);
9768                 if ( RExC_flags & RXf_PMf_EXTENDED)
9769                     p = regwhite( pRExC_state, p );
9770                 if ((UTF && FOLD) || is_exactfu_sharp_s) {
9771                     /* Prime the casefolded buffer.  Locale rules, which apply
9772                      * only to code points < 256, aren't known until execution,
9773                      * so for them, just output the original character using
9774                      * utf8.  If we start to fold non-UTF patterns, be sure to
9775                      * update join_exact() */
9776                     if (LOC && ender < 256) {
9777                         if (UNI_IS_INVARIANT(ender)) {
9778                             *tmpbuf = (U8) ender;
9779                             foldlen = 1;
9780                         } else {
9781                             *tmpbuf = UTF8_TWO_BYTE_HI(ender);
9782                             *(tmpbuf + 1) = UTF8_TWO_BYTE_LO(ender);
9783                             foldlen = 2;
9784                         }
9785                     }
9786                     else if (isASCII(ender)) {  /* Note: Here can't also be LOC
9787                                                  */
9788                         ender = toLOWER(ender);
9789                         *tmpbuf = (U8) ender;
9790                         foldlen = 1;
9791                     }
9792                     else if (! MORE_ASCII_RESTRICTED && ! LOC) {
9793
9794                         /* Locale and /aa require more selectivity about the
9795                          * fold, so are handled below.  Otherwise, here, just
9796                          * use the fold */
9797                         ender = toFOLD_uni(ender, tmpbuf, &foldlen);
9798                     }
9799                     else {
9800                         /* Under locale rules or /aa we are not to mix,
9801                          * respectively, ords < 256 or ASCII with non-.  So
9802                          * reject folds that mix them, using only the
9803                          * non-folded code point.  So do the fold to a
9804                          * temporary, and inspect each character in it. */
9805                         U8 trialbuf[UTF8_MAXBYTES_CASE+1];
9806                         U8* s = trialbuf;
9807                         UV tmpender = toFOLD_uni(ender, trialbuf, &foldlen);
9808                         U8* e = s + foldlen;
9809                         bool fold_ok = TRUE;
9810
9811                         while (s < e) {
9812                             if (isASCII(*s)
9813                                 || (LOC && (UTF8_IS_INVARIANT(*s)
9814                                            || UTF8_IS_DOWNGRADEABLE_START(*s))))
9815                             {
9816                                 fold_ok = FALSE;
9817                                 break;
9818                             }
9819                             s += UTF8SKIP(s);
9820                         }
9821                         if (fold_ok) {
9822                             Copy(trialbuf, tmpbuf, foldlen, U8);
9823                             ender = tmpender;
9824                         }
9825                         else {
9826                             uvuni_to_utf8(tmpbuf, ender);
9827                             foldlen = UNISKIP(ender);
9828                         }
9829                     }
9830                 }
9831                 if (p < RExC_end && ISMULT2(p)) { /* Back off on ?+*. */
9832                     if (len)
9833                         p = oldp;
9834                     else if (UTF || is_exactfu_sharp_s) {
9835                          if (FOLD) {
9836                               /* Emit all the Unicode characters. */
9837                               STRLEN numlen;
9838                               for (foldbuf = tmpbuf;
9839                                    foldlen;
9840                                    foldlen -= numlen) {
9841
9842                                    /* tmpbuf has been constructed by us, so we
9843                                     * know it is valid utf8 */
9844                                    ender = valid_utf8_to_uvchr(foldbuf, &numlen);
9845                                    if (numlen > 0) {
9846                                         const STRLEN unilen = reguni(pRExC_state, ender, s);
9847                                         s       += unilen;
9848                                         len     += unilen;
9849                                         /* In EBCDIC the numlen
9850                                          * and unilen can differ. */
9851                                         foldbuf += numlen;
9852                                         if (numlen >= foldlen)
9853                                              break;
9854                                    }
9855                                    else
9856                                         break; /* "Can't happen." */
9857                               }
9858                          }
9859                          else {
9860                               const STRLEN unilen = reguni(pRExC_state, ender, s);
9861                               if (unilen > 0) {
9862                                    s   += unilen;
9863                                    len += unilen;
9864                               }
9865                          }
9866                     }
9867                     else {
9868                         len++;
9869                         REGC((char)ender, s++);
9870                     }
9871                     break;
9872                 }
9873                 if (UTF || is_exactfu_sharp_s) {
9874                      if (FOLD) {
9875                           /* Emit all the Unicode characters. */
9876                           STRLEN numlen;
9877                           for (foldbuf = tmpbuf;
9878                                foldlen;
9879                                foldlen -= numlen) {
9880                                ender = valid_utf8_to_uvchr(foldbuf, &numlen);
9881                                if (numlen > 0) {
9882                                     const STRLEN unilen = reguni(pRExC_state, ender, s);
9883                                     len     += unilen;
9884                                     s       += unilen;
9885                                     /* In EBCDIC the numlen
9886                                      * and unilen can differ. */
9887                                     foldbuf += numlen;
9888                                     if (numlen >= foldlen)
9889                                          break;
9890                                }
9891                                else
9892                                     break;
9893                           }
9894                      }
9895                      else {
9896                           const STRLEN unilen = reguni(pRExC_state, ender, s);
9897                           if (unilen > 0) {
9898                                s   += unilen;
9899                                len += unilen;
9900                           }
9901                      }
9902                      len--;
9903                 }
9904                 else {
9905                     REGC((char)ender, s++);
9906                 }
9907             }
9908         loopdone:   /* Jumped to when encounters something that shouldn't be in
9909                        the node */
9910             RExC_parse = p - 1;
9911             Set_Node_Cur_Length(ret); /* MJD */
9912             nextchar(pRExC_state);
9913             {
9914                 /* len is STRLEN which is unsigned, need to copy to signed */
9915                 IV iv = len;
9916                 if (iv < 0)
9917                     vFAIL("Internal disaster");
9918             }
9919             if (len > 0)
9920                 *flagp |= HASWIDTH;
9921             if (len == 1 && UNI_IS_INVARIANT(ender))
9922                 *flagp |= SIMPLE;
9923
9924             if (SIZE_ONLY)
9925                 RExC_size += STR_SZ(len);
9926             else {
9927                 STR_LEN(ret) = len;
9928                 RExC_emit += STR_SZ(len);
9929             }
9930         }
9931         break;
9932     }
9933
9934     return(ret);
9935
9936 /* Jumped to when an unrecognized character set is encountered */
9937 bad_charset:
9938     Perl_croak(aTHX_ "panic: Unknown regex character set encoding: %u", get_regex_charset(RExC_flags));
9939     return(NULL);
9940 }
9941
9942 STATIC char *
9943 S_regwhite( RExC_state_t *pRExC_state, char *p )
9944 {
9945     const char *e = RExC_end;
9946
9947     PERL_ARGS_ASSERT_REGWHITE;
9948
9949     while (p < e) {
9950         if (isSPACE(*p))
9951             ++p;
9952         else if (*p == '#') {
9953             bool ended = 0;
9954             do {
9955                 if (*p++ == '\n') {
9956                     ended = 1;
9957                     break;
9958                 }
9959             } while (p < e);
9960             if (!ended)
9961                 RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
9962         }
9963         else
9964             break;
9965     }
9966     return p;
9967 }
9968
9969 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
9970    Character classes ([:foo:]) can also be negated ([:^foo:]).
9971    Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
9972    Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
9973    but trigger failures because they are currently unimplemented. */
9974
9975 #define POSIXCC_DONE(c)   ((c) == ':')
9976 #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.')
9977 #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c))
9978
9979 STATIC I32
9980 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
9981 {
9982     dVAR;
9983     I32 namedclass = OOB_NAMEDCLASS;
9984
9985     PERL_ARGS_ASSERT_REGPPOSIXCC;
9986
9987     if (value == '[' && RExC_parse + 1 < RExC_end &&
9988         /* I smell either [: or [= or [. -- POSIX has been here, right? */
9989         POSIXCC(UCHARAT(RExC_parse))) {
9990         const char c = UCHARAT(RExC_parse);
9991         char* const s = RExC_parse++;
9992
9993         while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c)
9994             RExC_parse++;
9995         if (RExC_parse == RExC_end)
9996             /* Grandfather lone [:, [=, [. */
9997             RExC_parse = s;
9998         else {
9999             const char* const t = RExC_parse++; /* skip over the c */
10000             assert(*t == c);
10001
10002             if (UCHARAT(RExC_parse) == ']') {
10003                 const char *posixcc = s + 1;
10004                 RExC_parse++; /* skip over the ending ] */
10005
10006                 if (*s == ':') {
10007                     const I32 complement = *posixcc == '^' ? *posixcc++ : 0;
10008                     const I32 skip = t - posixcc;
10009
10010                     /* Initially switch on the length of the name.  */
10011                     switch (skip) {
10012                     case 4:
10013                         if (memEQ(posixcc, "word", 4)) /* this is not POSIX, this is the Perl \w */
10014                             namedclass = complement ? ANYOF_NALNUM : ANYOF_ALNUM;
10015                         break;
10016                     case 5:
10017                         /* Names all of length 5.  */
10018                         /* alnum alpha ascii blank cntrl digit graph lower
10019                            print punct space upper  */
10020                         /* Offset 4 gives the best switch position.  */
10021                         switch (posixcc[4]) {
10022                         case 'a':
10023                             if (memEQ(posixcc, "alph", 4)) /* alpha */
10024                                 namedclass = complement ? ANYOF_NALPHA : ANYOF_ALPHA;
10025                             break;
10026                         case 'e':
10027                             if (memEQ(posixcc, "spac", 4)) /* space */
10028                                 namedclass = complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
10029                             break;
10030                         case 'h':
10031                             if (memEQ(posixcc, "grap", 4)) /* graph */
10032                                 namedclass = complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
10033                             break;
10034                         case 'i':
10035                             if (memEQ(posixcc, "asci", 4)) /* ascii */
10036                                 namedclass = complement ? ANYOF_NASCII : ANYOF_ASCII;
10037                             break;
10038                         case 'k':
10039                             if (memEQ(posixcc, "blan", 4)) /* blank */
10040                                 namedclass = complement ? ANYOF_NBLANK : ANYOF_BLANK;
10041                             break;
10042                         case 'l':
10043                             if (memEQ(posixcc, "cntr", 4)) /* cntrl */
10044                                 namedclass = complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
10045                             break;
10046                         case 'm':
10047                             if (memEQ(posixcc, "alnu", 4)) /* alnum */
10048                                 namedclass = complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
10049                             break;
10050                         case 'r':
10051                             if (memEQ(posixcc, "lowe", 4)) /* lower */
10052                                 namedclass = complement ? ANYOF_NLOWER : ANYOF_LOWER;
10053                             else if (memEQ(posixcc, "uppe", 4)) /* upper */
10054                                 namedclass = complement ? ANYOF_NUPPER : ANYOF_UPPER;
10055                             break;
10056                         case 't':
10057                             if (memEQ(posixcc, "digi", 4)) /* digit */
10058                                 namedclass = complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
10059                             else if (memEQ(posixcc, "prin", 4)) /* print */
10060                                 namedclass = complement ? ANYOF_NPRINT : ANYOF_PRINT;
10061                             else if (memEQ(posixcc, "punc", 4)) /* punct */
10062                                 namedclass = complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
10063                             break;
10064                         }
10065                         break;
10066                     case 6:
10067                         if (memEQ(posixcc, "xdigit", 6))
10068                             namedclass = complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
10069                         break;
10070                     }
10071
10072                     if (namedclass == OOB_NAMEDCLASS)
10073                         Simple_vFAIL3("POSIX class [:%.*s:] unknown",
10074                                       t - s - 1, s + 1);
10075                     assert (posixcc[skip] == ':');
10076                     assert (posixcc[skip+1] == ']');
10077                 } else if (!SIZE_ONLY) {
10078                     /* [[=foo=]] and [[.foo.]] are still future. */
10079
10080                     /* adjust RExC_parse so the warning shows after
10081                        the class closes */
10082                     while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']')
10083                         RExC_parse++;
10084                     Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
10085                 }
10086             } else {
10087                 /* Maternal grandfather:
10088                  * "[:" ending in ":" but not in ":]" */
10089                 RExC_parse = s;
10090             }
10091         }
10092     }
10093
10094     return namedclass;
10095 }
10096
10097 STATIC void
10098 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
10099 {
10100     dVAR;
10101
10102     PERL_ARGS_ASSERT_CHECKPOSIXCC;
10103
10104     if (POSIXCC(UCHARAT(RExC_parse))) {
10105         const char *s = RExC_parse;
10106         const char  c = *s++;
10107
10108         while (isALNUM(*s))
10109             s++;
10110         if (*s && c == *s && s[1] == ']') {
10111             ckWARN3reg(s+2,
10112                        "POSIX syntax [%c %c] belongs inside character classes",
10113                        c, c);
10114
10115             /* [[=foo=]] and [[.foo.]] are still future. */
10116             if (POSIXCC_NOTYET(c)) {
10117                 /* adjust RExC_parse so the error shows after
10118                    the class closes */
10119                 while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']')
10120                     NOOP;
10121                 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
10122             }
10123         }
10124     }
10125 }
10126
10127 /* Generate the code to add a full posix character <class> to the bracketed
10128  * character class given by <node>.  (<node> is needed only under locale rules)
10129  * destlist     is the inversion list for non-locale rules that this class is
10130  *              to be added to
10131  * sourcelist   is the ASCII-range inversion list to add under /a rules
10132  * Xsourcelist  is the full Unicode range list to use otherwise. */
10133 #define DO_POSIX(node, class, destlist, sourcelist, Xsourcelist)           \
10134     if (LOC) {                                                             \
10135         SV* scratch_list = NULL;                                           \
10136                                                                            \
10137         /* Set this class in the node for runtime matching */              \
10138         ANYOF_CLASS_SET(node, class);                                      \
10139                                                                            \
10140         /* For above Latin1 code points, we use the full Unicode range */  \
10141         _invlist_intersection(PL_AboveLatin1,                              \
10142                               Xsourcelist,                                 \
10143                               &scratch_list);                              \
10144         /* And set the output to it, adding instead if there already is an \
10145          * output.  Checking if <destlist> is NULL first saves an extra    \
10146          * clone.  Its reference count will be decremented at the next     \
10147          * union, etc, or if this is the only instance, at the end of the  \
10148          * routine */                                                      \
10149         if (! destlist) {                                                  \
10150             destlist = scratch_list;                                       \
10151         }                                                                  \
10152         else {                                                             \
10153             _invlist_union(destlist, scratch_list, &destlist);             \
10154             SvREFCNT_dec(scratch_list);                                    \
10155         }                                                                  \
10156     }                                                                      \
10157     else {                                                                 \
10158         /* For non-locale, just add it to any existing list */             \
10159         _invlist_union(destlist,                                           \
10160                        (AT_LEAST_ASCII_RESTRICTED)                         \
10161                            ? sourcelist                                    \
10162                            : Xsourcelist,                                  \
10163                        &destlist);                                         \
10164     }
10165
10166 /* Like DO_POSIX, but matches the complement of <sourcelist> and <Xsourcelist>.
10167  */
10168 #define DO_N_POSIX(node, class, destlist, sourcelist, Xsourcelist)         \
10169     if (LOC) {                                                             \
10170         SV* scratch_list = NULL;                                           \
10171         ANYOF_CLASS_SET(node, class);                                      \
10172         _invlist_subtract(PL_AboveLatin1, Xsourcelist, &scratch_list);     \
10173         if (! destlist) {                                                  \
10174             destlist = scratch_list;                                       \
10175         }                                                                  \
10176         else {                                                             \
10177             _invlist_union(destlist, scratch_list, &destlist);             \
10178             SvREFCNT_dec(scratch_list);                                    \
10179         }                                                                  \
10180     }                                                                      \
10181     else {                                                                 \
10182         _invlist_union_complement_2nd(destlist,                            \
10183                                     (AT_LEAST_ASCII_RESTRICTED)            \
10184                                         ? sourcelist                       \
10185                                         : Xsourcelist,                     \
10186                                     &destlist);                            \
10187         /* Under /d, everything in the upper half of the Latin1 range      \
10188          * matches this complement */                                      \
10189         if (DEPENDS_SEMANTICS) {                                           \
10190             ANYOF_FLAGS(node) |= ANYOF_NON_UTF8_LATIN1_ALL;                \
10191         }                                                                  \
10192     }
10193
10194 /* Generate the code to add a posix character <class> to the bracketed
10195  * character class given by <node>.  (<node> is needed only under locale rules)
10196  * destlist       is the inversion list for non-locale rules that this class is
10197  *                to be added to
10198  * sourcelist     is the ASCII-range inversion list to add under /a rules
10199  * l1_sourcelist  is the Latin1 range list to use otherwise.
10200  * Xpropertyname  is the name to add to <run_time_list> of the property to
10201  *                specify the code points above Latin1 that will have to be
10202  *                determined at run-time
10203  * run_time_list  is a SV* that contains text names of properties that are to
10204  *                be computed at run time.  This concatenates <Xpropertyname>
10205  *                to it, apppropriately
10206  * This is essentially DO_POSIX, but we know only the Latin1 values at compile
10207  * time */
10208 #define DO_POSIX_LATIN1_ONLY_KNOWN(node, class, destlist, sourcelist,      \
10209                               l1_sourcelist, Xpropertyname, run_time_list) \
10210         /* First, resolve whether to use the ASCII-only list or the L1     \
10211          * list */                                                         \
10212         DO_POSIX_LATIN1_ONLY_KNOWN_L1_RESOLVED(node, class, destlist,      \
10213                 ((AT_LEAST_ASCII_RESTRICTED) ? sourcelist : l1_sourcelist),\
10214                 Xpropertyname, run_time_list)
10215
10216 #define DO_POSIX_LATIN1_ONLY_KNOWN_L1_RESOLVED(node, class, destlist, sourcelist, \
10217                 Xpropertyname, run_time_list)                              \
10218     /* If not /a matching, there are going to be code points we will have  \
10219      * to defer to runtime to look-up */                                   \
10220     if (! AT_LEAST_ASCII_RESTRICTED) {                                     \
10221         Perl_sv_catpvf(aTHX_ run_time_list, "+utf8::%s\n", Xpropertyname); \
10222     }                                                                      \
10223     if (LOC) {                                                             \
10224         ANYOF_CLASS_SET(node, class);                                      \
10225     }                                                                      \
10226     else {                                                                 \
10227         _invlist_union(destlist, sourcelist, &destlist);                   \
10228     }
10229
10230 /* Like DO_POSIX_LATIN1_ONLY_KNOWN, but for the complement.  A combination of
10231  * this and DO_N_POSIX */
10232 #define DO_N_POSIX_LATIN1_ONLY_KNOWN(node, class, destlist, sourcelist,    \
10233                               l1_sourcelist, Xpropertyname, run_time_list) \
10234     if (AT_LEAST_ASCII_RESTRICTED) {                                       \
10235         _invlist_union_complement_2nd(destlist, sourcelist, &destlist);    \
10236     }                                                                      \
10237     else {                                                                 \
10238         Perl_sv_catpvf(aTHX_ run_time_list, "!utf8::%s\n", Xpropertyname); \
10239         if (LOC) {                                                         \
10240             ANYOF_CLASS_SET(node, namedclass);                             \
10241         }                                                                  \
10242         else {                                                             \
10243             SV* scratch_list = NULL;                                       \
10244             _invlist_subtract(PL_Latin1, l1_sourcelist, &scratch_list);    \
10245             if (! destlist) {                                              \
10246                 destlist = scratch_list;                                   \
10247             }                                                              \
10248             else {                                                         \
10249                 _invlist_union(destlist, scratch_list, &destlist);         \
10250                 SvREFCNT_dec(scratch_list);                                \
10251             }                                                              \
10252             if (DEPENDS_SEMANTICS) {                                       \
10253                 ANYOF_FLAGS(node) |= ANYOF_NON_UTF8_LATIN1_ALL;            \
10254             }                                                              \
10255         }                                                                  \
10256     }
10257
10258 STATIC U8
10259 S_set_regclass_bit_fold(pTHX_ RExC_state_t *pRExC_state, regnode* node, const U8 value, SV** invlist_ptr, AV** alternate_ptr)
10260 {
10261
10262     /* Handle the setting of folds in the bitmap for non-locale ANYOF nodes.
10263      * Locale folding is done at run-time, so this function should not be
10264      * called for nodes that are for locales.
10265      *
10266      * This function sets the bit corresponding to the fold of the input
10267      * 'value', if not already set.  The fold of 'f' is 'F', and the fold of
10268      * 'F' is 'f'.
10269      *
10270      * It also knows about the characters that are in the bitmap that have
10271      * folds that are matchable only outside it, and sets the appropriate lists
10272      * and flags.
10273      *
10274      * It returns the number of bits that actually changed from 0 to 1 */
10275
10276     U8 stored = 0;
10277     U8 fold;
10278
10279     PERL_ARGS_ASSERT_SET_REGCLASS_BIT_FOLD;
10280
10281     fold = (AT_LEAST_UNI_SEMANTICS) ? PL_fold_latin1[value]
10282                                     : PL_fold[value];
10283
10284     /* It assumes the bit for 'value' has already been set */
10285     if (fold != value && ! ANYOF_BITMAP_TEST(node, fold)) {
10286         ANYOF_BITMAP_SET(node, fold);
10287         stored++;
10288     }
10289     if (_HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(value) && (! isASCII(value) || ! MORE_ASCII_RESTRICTED)) {
10290         /* Certain Latin1 characters have matches outside the bitmap.  To get
10291          * here, 'value' is one of those characters.   None of these matches is
10292          * valid for ASCII characters under /aa, which have been excluded by
10293          * the 'if' above.  The matches fall into three categories:
10294          * 1) They are singly folded-to or -from an above 255 character, as
10295          *    LATIN SMALL LETTER Y WITH DIAERESIS and LATIN CAPITAL LETTER Y
10296          *    WITH DIAERESIS;
10297          * 2) They are part of a multi-char fold with another character in the
10298          *    bitmap, only LATIN SMALL LETTER SHARP S => "ss" fits that bill;
10299          * 3) They are part of a multi-char fold with a character not in the
10300          *    bitmap, such as various ligatures.
10301          * We aren't dealing fully with multi-char folds, except we do deal
10302          * with the pattern containing a character that has a multi-char fold
10303          * (not so much the inverse).
10304          * For types 1) and 3), the matches only happen when the target string
10305          * is utf8; that's not true for 2), and we set a flag for it.
10306          *
10307          * The code below adds to the passed in inversion list the single fold
10308          * closures for 'value'.  The values are hard-coded here so that an
10309          * innocent-looking character class, like /[ks]/i won't have to go out
10310          * to disk to find the possible matches.  XXX It would be better to
10311          * generate these via regen, in case a new version of the Unicode
10312          * standard adds new mappings, though that is not really likely. */
10313         switch (value) {
10314             case 'k':
10315             case 'K':
10316                 /* KELVIN SIGN */
10317                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x212A);
10318                 break;
10319             case 's':
10320             case 'S':
10321                 /* LATIN SMALL LETTER LONG S */
10322                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x017F);
10323                 break;
10324             case MICRO_SIGN:
10325                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10326                                                  GREEK_SMALL_LETTER_MU);
10327                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10328                                                  GREEK_CAPITAL_LETTER_MU);
10329                 break;
10330             case LATIN_CAPITAL_LETTER_A_WITH_RING_ABOVE:
10331             case LATIN_SMALL_LETTER_A_WITH_RING_ABOVE:
10332                 /* ANGSTROM SIGN */
10333                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr, 0x212B);
10334                 if (DEPENDS_SEMANTICS) {    /* See DEPENDS comment below */
10335                     *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10336                                                      PL_fold_latin1[value]);
10337                 }
10338                 break;
10339             case LATIN_SMALL_LETTER_Y_WITH_DIAERESIS:
10340                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10341                                         LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS);
10342                 break;
10343             case LATIN_SMALL_LETTER_SHARP_S:
10344                 *invlist_ptr = add_cp_to_invlist(*invlist_ptr,
10345                                         LATIN_CAPITAL_LETTER_SHARP_S);
10346
10347                 /* Under /a, /d, and /u, this can match the two chars "ss" */
10348                 if (! MORE_ASCII_RESTRICTED) {
10349                     add_alternate(alternate_ptr, (U8 *) "ss", 2);
10350
10351                     /* And under /u or /a, it can match even if the target is
10352                      * not utf8 */
10353                     if (AT_LEAST_UNI_SEMANTICS) {
10354                         ANYOF_FLAGS(node) |= ANYOF_NONBITMAP_NON_UTF8;
10355                     }
10356                 }
10357                 break;
10358             case 'F': case 'f':
10359             case 'I': case 'i':
10360             case 'L': case 'l':
10361             case 'T': case 't':
10362             case 'A': case 'a':
10363             case 'H': case 'h':
10364             case 'J': case 'j':
10365             case 'N': case 'n':
10366             case 'W': case 'w':
10367             case 'Y': case 'y':
10368                 /* These all are targets of multi-character folds from code
10369                  * points that require UTF8 to express, so they can't match
10370                  * unless the target string is in UTF-8, so no action here is
10371                  * necessary, as regexec.c properly handles the general case
10372                  * for UTF-8 matching */
10373                 break;
10374             default:
10375                 /* Use deprecated warning to increase the chances of this
10376                  * being output */
10377                 ckWARN2regdep(RExC_parse, "Perl folding rules are not up-to-date for 0x%x; please use the perlbug utility to report;", value);
10378                 break;
10379         }
10380     }
10381     else if (DEPENDS_SEMANTICS
10382             && ! isASCII(value)
10383             && PL_fold_latin1[value] != value)
10384     {
10385            /* Under DEPENDS rules, non-ASCII Latin1 characters match their
10386             * folds only when the target string is in UTF-8.  We add the fold
10387             * here to the list of things to match outside the bitmap, which
10388             * won't be looked at unless it is UTF8 (or else if something else
10389             * says to look even if not utf8, but those things better not happen
10390             * under DEPENDS semantics. */
10391         *invlist_ptr = add_cp_to_invlist(*invlist_ptr, PL_fold_latin1[value]);
10392     }
10393
10394     return stored;
10395 }
10396
10397
10398 PERL_STATIC_INLINE U8
10399 S_set_regclass_bit(pTHX_ RExC_state_t *pRExC_state, regnode* node, const U8 value, SV** invlist_ptr, AV** alternate_ptr)
10400 {
10401     /* This inline function sets a bit in the bitmap if not already set, and if
10402      * appropriate, its fold, returning the number of bits that actually
10403      * changed from 0 to 1 */
10404
10405     U8 stored;
10406
10407     PERL_ARGS_ASSERT_SET_REGCLASS_BIT;
10408
10409     if (ANYOF_BITMAP_TEST(node, value)) {   /* Already set */
10410         return 0;
10411     }
10412
10413     ANYOF_BITMAP_SET(node, value);
10414     stored = 1;
10415
10416     if (FOLD && ! LOC) {        /* Locale folds aren't known until runtime */
10417         stored += set_regclass_bit_fold(pRExC_state, node, value, invlist_ptr, alternate_ptr);
10418     }
10419
10420     return stored;
10421 }
10422
10423 STATIC void
10424 S_add_alternate(pTHX_ AV** alternate_ptr, U8* string, STRLEN len)
10425 {
10426     /* Adds input 'string' with length 'len' to the ANYOF node's unicode
10427      * alternate list, pointed to by 'alternate_ptr'.  This is an array of
10428      * the multi-character folds of characters in the node */
10429     SV *sv;
10430
10431     PERL_ARGS_ASSERT_ADD_ALTERNATE;
10432
10433     if (! *alternate_ptr) {
10434         *alternate_ptr = newAV();
10435     }
10436     sv = newSVpvn_utf8((char*)string, len, TRUE);
10437     av_push(*alternate_ptr, sv);
10438     return;
10439 }
10440
10441 /*
10442    parse a class specification and produce either an ANYOF node that
10443    matches the pattern or perhaps will be optimized into an EXACTish node
10444    instead. The node contains a bit map for the first 256 characters, with the
10445    corresponding bit set if that character is in the list.  For characters
10446    above 255, a range list is used */
10447
10448 STATIC regnode *
10449 S_regclass(pTHX_ RExC_state_t *pRExC_state, U32 depth)
10450 {
10451     dVAR;
10452     register UV nextvalue;
10453     register IV prevvalue = OOB_UNICODE;
10454     register IV range = 0;
10455     UV value = 0; /* XXX:dmq: needs to be referenceable (unfortunately) */
10456     register regnode *ret;
10457     STRLEN numlen;
10458     IV namedclass;
10459     char *rangebegin = NULL;
10460     bool need_class = 0;
10461     bool allow_full_fold = TRUE;   /* Assume wants multi-char folding */
10462     SV *listsv = NULL;
10463     STRLEN initial_listsv_len = 0; /* Kind of a kludge to see if it is more
10464                                       than just initialized.  */
10465     SV* properties = NULL;    /* Code points that match \p{} \P{} */
10466     UV element_count = 0;   /* Number of distinct elements in the class.
10467                                Optimizations may be possible if this is tiny */
10468     UV n;
10469
10470     /* Unicode properties are stored in a swash; this holds the current one
10471      * being parsed.  If this swash is the only above-latin1 component of the
10472      * character class, an optimization is to pass it directly on to the
10473      * execution engine.  Otherwise, it is set to NULL to indicate that there
10474      * are other things in the class that have to be dealt with at execution
10475      * time */
10476     SV* swash = NULL;           /* Code points that match \p{} \P{} */
10477
10478     /* Set if a component of this character class is user-defined; just passed
10479      * on to the engine */
10480     UV has_user_defined_property = 0;
10481
10482     /* code points this node matches that can't be stored in the bitmap */
10483     SV* nonbitmap = NULL;
10484
10485     /* The items that are to match that aren't stored in the bitmap, but are a
10486      * result of things that are stored there.  This is the fold closure of
10487      * such a character, either because it has DEPENDS semantics and shouldn't
10488      * be matched unless the target string is utf8, or is a code point that is
10489      * too large for the bit map, as for example, the fold of the MICRO SIGN is
10490      * above 255.  This all is solely for performance reasons.  By having this
10491      * code know the outside-the-bitmap folds that the bitmapped characters are
10492      * involved with, we don't have to go out to disk to find the list of
10493      * matches, unless the character class includes code points that aren't
10494      * storable in the bit map.  That means that a character class with an 's'
10495      * in it, for example, doesn't need to go out to disk to find everything
10496      * that matches.  A 2nd list is used so that the 'nonbitmap' list is kept
10497      * empty unless there is something whose fold we don't know about, and will
10498      * have to go out to the disk to find. */
10499     SV* l1_fold_invlist = NULL;
10500
10501     /* List of multi-character folds that are matched by this node */
10502     AV* unicode_alternate  = NULL;
10503 #ifdef EBCDIC
10504     UV literal_endpoint = 0;
10505 #endif
10506     UV stored = 0;  /* how many chars stored in the bitmap */
10507
10508     regnode * const orig_emit = RExC_emit; /* Save the original RExC_emit in
10509         case we need to change the emitted regop to an EXACT. */
10510     const char * orig_parse = RExC_parse;
10511     GET_RE_DEBUG_FLAGS_DECL;
10512
10513     PERL_ARGS_ASSERT_REGCLASS;
10514 #ifndef DEBUGGING
10515     PERL_UNUSED_ARG(depth);
10516 #endif
10517
10518     DEBUG_PARSE("clas");
10519
10520     /* Assume we are going to generate an ANYOF node. */
10521     ret = reganode(pRExC_state, ANYOF, 0);
10522
10523
10524     if (!SIZE_ONLY) {
10525         ANYOF_FLAGS(ret) = 0;
10526     }
10527
10528     if (UCHARAT(RExC_parse) == '^') {   /* Complement of range. */
10529         RExC_naughty++;
10530         RExC_parse++;
10531         if (!SIZE_ONLY)
10532             ANYOF_FLAGS(ret) |= ANYOF_INVERT;
10533
10534         /* We have decided to not allow multi-char folds in inverted character
10535          * classes, due to the confusion that can happen, especially with
10536          * classes that are designed for a non-Unicode world:  You have the
10537          * peculiar case that:
10538             "s s" =~ /^[^\xDF]+$/i => Y
10539             "ss"  =~ /^[^\xDF]+$/i => N
10540          *
10541          * See [perl #89750] */
10542         allow_full_fold = FALSE;
10543     }
10544
10545     if (SIZE_ONLY) {
10546         RExC_size += ANYOF_SKIP;
10547         listsv = &PL_sv_undef; /* For code scanners: listsv always non-NULL. */
10548     }
10549     else {
10550         RExC_emit += ANYOF_SKIP;
10551         if (LOC) {
10552             ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
10553         }
10554         ANYOF_BITMAP_ZERO(ret);
10555         listsv = newSVpvs("# comment\n");
10556         initial_listsv_len = SvCUR(listsv);
10557     }
10558
10559     nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
10560
10561     if (!SIZE_ONLY && POSIXCC(nextvalue))
10562         checkposixcc(pRExC_state);
10563
10564     /* allow 1st char to be ] (allowing it to be - is dealt with later) */
10565     if (UCHARAT(RExC_parse) == ']')
10566         goto charclassloop;
10567
10568 parseit:
10569     while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') {
10570
10571     charclassloop:
10572
10573         namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
10574
10575         if (!range) {
10576             rangebegin = RExC_parse;
10577             element_count++;
10578         }
10579         if (UTF) {
10580             value = utf8n_to_uvchr((U8*)RExC_parse,
10581                                    RExC_end - RExC_parse,
10582                                    &numlen, UTF8_ALLOW_DEFAULT);
10583             RExC_parse += numlen;
10584         }
10585         else
10586             value = UCHARAT(RExC_parse++);
10587
10588         nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
10589         if (value == '[' && POSIXCC(nextvalue))
10590             namedclass = regpposixcc(pRExC_state, value);
10591         else if (value == '\\') {
10592             if (UTF) {
10593                 value = utf8n_to_uvchr((U8*)RExC_parse,
10594                                    RExC_end - RExC_parse,
10595                                    &numlen, UTF8_ALLOW_DEFAULT);
10596                 RExC_parse += numlen;
10597             }
10598             else
10599                 value = UCHARAT(RExC_parse++);
10600             /* Some compilers cannot handle switching on 64-bit integer
10601              * values, therefore value cannot be an UV.  Yes, this will
10602              * be a problem later if we want switch on Unicode.
10603              * A similar issue a little bit later when switching on
10604              * namedclass. --jhi */
10605             switch ((I32)value) {
10606             case 'w':   namedclass = ANYOF_ALNUM;       break;
10607             case 'W':   namedclass = ANYOF_NALNUM;      break;
10608             case 's':   namedclass = ANYOF_SPACE;       break;
10609             case 'S':   namedclass = ANYOF_NSPACE;      break;
10610             case 'd':   namedclass = ANYOF_DIGIT;       break;
10611             case 'D':   namedclass = ANYOF_NDIGIT;      break;
10612             case 'v':   namedclass = ANYOF_VERTWS;      break;
10613             case 'V':   namedclass = ANYOF_NVERTWS;     break;
10614             case 'h':   namedclass = ANYOF_HORIZWS;     break;
10615             case 'H':   namedclass = ANYOF_NHORIZWS;    break;
10616             case 'N':  /* Handle \N{NAME} in class */
10617                 {
10618                     /* We only pay attention to the first char of 
10619                     multichar strings being returned. I kinda wonder
10620                     if this makes sense as it does change the behaviour
10621                     from earlier versions, OTOH that behaviour was broken
10622                     as well. */
10623                     UV v; /* value is register so we cant & it /grrr */
10624                     if (reg_namedseq(pRExC_state, &v, NULL, depth)) {
10625                         goto parseit;
10626                     }
10627                     value= v; 
10628                 }
10629                 break;
10630             case 'p':
10631             case 'P':
10632                 {
10633                 char *e;
10634                 if (RExC_parse >= RExC_end)
10635                     vFAIL2("Empty \\%c{}", (U8)value);
10636                 if (*RExC_parse == '{') {
10637                     const U8 c = (U8)value;
10638                     e = strchr(RExC_parse++, '}');
10639                     if (!e)
10640                         vFAIL2("Missing right brace on \\%c{}", c);
10641                     while (isSPACE(UCHARAT(RExC_parse)))
10642                         RExC_parse++;
10643                     if (e == RExC_parse)
10644                         vFAIL2("Empty \\%c{}", c);
10645                     n = e - RExC_parse;
10646                     while (isSPACE(UCHARAT(RExC_parse + n - 1)))
10647                         n--;
10648                 }
10649                 else {
10650                     e = RExC_parse;
10651                     n = 1;
10652                 }
10653                 if (!SIZE_ONLY) {
10654                     SV** invlistsvp;
10655                     SV* invlist;
10656                     char* name;
10657                     if (UCHARAT(RExC_parse) == '^') {
10658                          RExC_parse++;
10659                          n--;
10660                          value = value == 'p' ? 'P' : 'p'; /* toggle */
10661                          while (isSPACE(UCHARAT(RExC_parse))) {
10662                               RExC_parse++;
10663                               n--;
10664                          }
10665                     }
10666                     /* Try to get the definition of the property into
10667                      * <invlist>.  If /i is in effect, the effective property
10668                      * will have its name be <__NAME_i>.  The design is
10669                      * discussed in commit
10670                      * 2f833f5208e26b208886e51e09e2c072b5eabb46 */
10671                     Newx(name, n + sizeof("_i__\n"), char);
10672
10673                     sprintf(name, "%s%.*s%s\n",
10674                                     (FOLD) ? "__" : "",
10675                                     (int)n,
10676                                     RExC_parse,
10677                                     (FOLD) ? "_i" : ""
10678                     );
10679
10680                     /* Look up the property name, and get its swash and
10681                      * inversion list, if the property is found  */
10682                     if (swash) {
10683                         SvREFCNT_dec(swash);
10684                     }
10685                     swash = _core_swash_init("utf8", name, &PL_sv_undef,
10686                                              1, /* binary */
10687                                              0, /* not tr/// */
10688                                              TRUE, /* this routine will handle
10689                                                       undefined properties */
10690                                              NULL, FALSE /* No inversion list */
10691                                             );
10692                     if (   ! swash
10693                         || ! SvROK(swash)
10694                         || ! SvTYPE(SvRV(swash)) == SVt_PVHV
10695                         || ! (invlistsvp =
10696                                 hv_fetchs(MUTABLE_HV(SvRV(swash)),
10697                                 "INVLIST", FALSE))
10698                         || ! (invlist = *invlistsvp))
10699                     {
10700                         if (swash) {
10701                             SvREFCNT_dec(swash);
10702                             swash = NULL;
10703                         }
10704
10705                         /* Here didn't find it.  It could be a user-defined
10706                          * property that will be available at run-time.  Add it
10707                          * to the list to look up then */
10708                         Perl_sv_catpvf(aTHX_ listsv, "%cutf8::%s\n",
10709                                         (value == 'p' ? '+' : '!'),
10710                                         name);
10711                         has_user_defined_property = 1;
10712
10713                         /* We don't know yet, so have to assume that the
10714                          * property could match something in the Latin1 range,
10715                          * hence something that isn't utf8 */
10716                         ANYOF_FLAGS(ret) |= ANYOF_NONBITMAP_NON_UTF8;
10717                     }
10718                     else {
10719
10720                         /* Here, did get the swash and its inversion list.  If
10721                          * the swash is from a user-defined property, then this
10722                          * whole character class should be regarded as such */
10723                         SV** user_defined_svp =
10724                                             hv_fetchs(MUTABLE_HV(SvRV(swash)),
10725                                                         "USER_DEFINED", FALSE);
10726                         if (user_defined_svp) {
10727                             has_user_defined_property
10728                                                     |= SvUV(*user_defined_svp);
10729                         }
10730
10731                         /* Invert if asking for the complement */
10732                         if (value == 'P') {
10733                             _invlist_union_complement_2nd(properties, invlist, &properties);
10734
10735                             /* The swash can't be used as-is, because we've
10736                              * inverted things; delay removing it to here after
10737                              * have copied its invlist above */
10738                             SvREFCNT_dec(swash);
10739                             swash = NULL;
10740                         }
10741                         else {
10742                             _invlist_union(properties, invlist, &properties);
10743                         }
10744                     }
10745                     Safefree(name);
10746                 }
10747                 RExC_parse = e + 1;
10748                 namedclass = ANYOF_MAX;  /* no official name, but it's named */
10749
10750                 /* \p means they want Unicode semantics */
10751                 RExC_uni_semantics = 1;
10752                 }
10753                 break;
10754             case 'n':   value = '\n';                   break;
10755             case 'r':   value = '\r';                   break;
10756             case 't':   value = '\t';                   break;
10757             case 'f':   value = '\f';                   break;
10758             case 'b':   value = '\b';                   break;
10759             case 'e':   value = ASCII_TO_NATIVE('\033');break;
10760             case 'a':   value = ASCII_TO_NATIVE('\007');break;
10761             case 'o':
10762                 RExC_parse--;   /* function expects to be pointed at the 'o' */
10763                 {
10764                     const char* error_msg;
10765                     bool valid = grok_bslash_o(RExC_parse,
10766                                                &value,
10767                                                &numlen,
10768                                                &error_msg,
10769                                                SIZE_ONLY);
10770                     RExC_parse += numlen;
10771                     if (! valid) {
10772                         vFAIL(error_msg);
10773                     }
10774                 }
10775                 if (PL_encoding && value < 0x100) {
10776                     goto recode_encoding;
10777                 }
10778                 break;
10779             case 'x':
10780                 if (*RExC_parse == '{') {
10781                     I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
10782                         | PERL_SCAN_DISALLOW_PREFIX;
10783                     char * const e = strchr(RExC_parse++, '}');
10784                     if (!e)
10785                         vFAIL("Missing right brace on \\x{}");
10786
10787                     numlen = e - RExC_parse;
10788                     value = grok_hex(RExC_parse, &numlen, &flags, NULL);
10789                     RExC_parse = e + 1;
10790                 }
10791                 else {
10792                     I32 flags = PERL_SCAN_DISALLOW_PREFIX;
10793                     numlen = 2;
10794                     value = grok_hex(RExC_parse, &numlen, &flags, NULL);
10795                     RExC_parse += numlen;
10796                 }
10797                 if (PL_encoding && value < 0x100)
10798                     goto recode_encoding;
10799                 break;
10800             case 'c':
10801                 value = grok_bslash_c(*RExC_parse++, UTF, SIZE_ONLY);
10802                 break;
10803             case '0': case '1': case '2': case '3': case '4':
10804             case '5': case '6': case '7':
10805                 {
10806                     /* Take 1-3 octal digits */
10807                     I32 flags = PERL_SCAN_SILENT_ILLDIGIT;
10808                     numlen = 3;
10809                     value = grok_oct(--RExC_parse, &numlen, &flags, NULL);
10810                     RExC_parse += numlen;
10811                     if (PL_encoding && value < 0x100)
10812                         goto recode_encoding;
10813                     break;
10814                 }
10815             recode_encoding:
10816                 if (! RExC_override_recoding) {
10817                     SV* enc = PL_encoding;
10818                     value = reg_recode((const char)(U8)value, &enc);
10819                     if (!enc && SIZE_ONLY)
10820                         ckWARNreg(RExC_parse,
10821                                   "Invalid escape in the specified encoding");
10822                     break;
10823                 }
10824             default:
10825                 /* Allow \_ to not give an error */
10826                 if (!SIZE_ONLY && isALNUM(value) && value != '_') {
10827                     ckWARN2reg(RExC_parse,
10828                                "Unrecognized escape \\%c in character class passed through",
10829                                (int)value);
10830                 }
10831                 break;
10832             }
10833         } /* end of \blah */
10834 #ifdef EBCDIC
10835         else
10836             literal_endpoint++;
10837 #endif
10838
10839         if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
10840
10841             /* What matches in a locale is not known until runtime, so need to
10842              * (one time per class) allocate extra space to pass to regexec.
10843              * The space will contain a bit for each named class that is to be
10844              * matched against.  This isn't needed for \p{} and pseudo-classes,
10845              * as they are not affected by locale, and hence are dealt with
10846              * separately */
10847             if (LOC && namedclass < ANYOF_MAX && ! need_class) {
10848                 need_class = 1;
10849                 if (SIZE_ONLY) {
10850                     RExC_size += ANYOF_CLASS_SKIP - ANYOF_SKIP;
10851                 }
10852                 else {
10853                     RExC_emit += ANYOF_CLASS_SKIP - ANYOF_SKIP;
10854                     ANYOF_CLASS_ZERO(ret);
10855                 }
10856                 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
10857             }
10858
10859             /* a bad range like a-\d, a-[:digit:].  The '-' is taken as a
10860              * literal, as is the character that began the false range, i.e.
10861              * the 'a' in the examples */
10862             if (range) {
10863                 if (!SIZE_ONLY) {
10864                     const int w =
10865                         RExC_parse >= rangebegin ?
10866                         RExC_parse - rangebegin : 0;
10867                     ckWARN4reg(RExC_parse,
10868                                "False [] range \"%*.*s\"",
10869                                w, w, rangebegin);
10870
10871                     stored +=
10872                          set_regclass_bit(pRExC_state, ret, '-', &l1_fold_invlist, &unicode_alternate);
10873                     if (prevvalue < 256) {
10874                         stored +=
10875                          set_regclass_bit(pRExC_state, ret, (U8) prevvalue, &l1_fold_invlist, &unicode_alternate);
10876                     }
10877                     else {
10878                         nonbitmap = add_cp_to_invlist(nonbitmap, prevvalue);
10879                     }
10880                 }
10881
10882                 range = 0; /* this was not a true range */
10883             }
10884
10885             if (!SIZE_ONLY) {
10886
10887                 /* Possible truncation here but in some 64-bit environments
10888                  * the compiler gets heartburn about switch on 64-bit values.
10889                  * A similar issue a little earlier when switching on value.
10890                  * --jhi */
10891                 switch ((I32)namedclass) {
10892
10893                 case ANYOF_ALNUMC: /* C's alnum, in contrast to \w */
10894                     DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10895                         PL_PosixAlnum, PL_L1PosixAlnum, "XPosixAlnum", listsv);
10896                     break;
10897                 case ANYOF_NALNUMC:
10898                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10899                         PL_PosixAlnum, PL_L1PosixAlnum, "XPosixAlnum", listsv);
10900                     break;
10901                 case ANYOF_ALPHA:
10902                     DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10903                         PL_PosixAlpha, PL_L1PosixAlpha, "XPosixAlpha", listsv);
10904                     break;
10905                 case ANYOF_NALPHA:
10906                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10907                         PL_PosixAlpha, PL_L1PosixAlpha, "XPosixAlpha", listsv);
10908                     break;
10909                 case ANYOF_ASCII:
10910                     if (LOC) {
10911                         ANYOF_CLASS_SET(ret, namedclass);
10912                     }
10913                     else {
10914                         _invlist_union(properties, PL_ASCII, &properties);
10915                     }
10916                     break;
10917                 case ANYOF_NASCII:
10918                     if (LOC) {
10919                         ANYOF_CLASS_SET(ret, namedclass);
10920                     }
10921                     else {
10922                         _invlist_union_complement_2nd(properties,
10923                                                     PL_ASCII, &properties);
10924                         if (DEPENDS_SEMANTICS) {
10925                             ANYOF_FLAGS(ret) |= ANYOF_NON_UTF8_LATIN1_ALL;
10926                         }
10927                     }
10928                     break;
10929                 case ANYOF_BLANK:
10930                     DO_POSIX(ret, namedclass, properties,
10931                                             PL_PosixBlank, PL_XPosixBlank);
10932                     break;
10933                 case ANYOF_NBLANK:
10934                     DO_N_POSIX(ret, namedclass, properties,
10935                                             PL_PosixBlank, PL_XPosixBlank);
10936                     break;
10937                 case ANYOF_CNTRL:
10938                     DO_POSIX(ret, namedclass, properties,
10939                                             PL_PosixCntrl, PL_XPosixCntrl);
10940                     break;
10941                 case ANYOF_NCNTRL:
10942                     DO_N_POSIX(ret, namedclass, properties,
10943                                             PL_PosixCntrl, PL_XPosixCntrl);
10944                     break;
10945                 case ANYOF_DIGIT:
10946                     /* There are no digits in the Latin1 range outside of
10947                      * ASCII, so call the macro that doesn't have to resolve
10948                      * them */
10949                     DO_POSIX_LATIN1_ONLY_KNOWN_L1_RESOLVED(ret, namedclass, properties,
10950                         PL_PosixDigit, "XPosixDigit", listsv);
10951                     break;
10952                 case ANYOF_NDIGIT:
10953                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10954                         PL_PosixDigit, PL_PosixDigit, "XPosixDigit", listsv);
10955                     break;
10956                 case ANYOF_GRAPH:
10957                     DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10958                         PL_PosixGraph, PL_L1PosixGraph, "XPosixGraph", listsv);
10959                     break;
10960                 case ANYOF_NGRAPH:
10961                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10962                         PL_PosixGraph, PL_L1PosixGraph, "XPosixGraph", listsv);
10963                     break;
10964                 case ANYOF_HORIZWS:
10965                     /* For these, we use the nonbitmap, as /d doesn't make a
10966                      * difference in what these match.  There would be problems
10967                      * if these characters had folds other than themselves, as
10968                      * nonbitmap is subject to folding.  It turns out that \h
10969                      * is just a synonym for XPosixBlank */
10970                     _invlist_union(nonbitmap, PL_XPosixBlank, &nonbitmap);
10971                     break;
10972                 case ANYOF_NHORIZWS:
10973                     _invlist_union_complement_2nd(nonbitmap,
10974                                                  PL_XPosixBlank, &nonbitmap);
10975                     break;
10976                 case ANYOF_LOWER:
10977                 case ANYOF_NLOWER:
10978                 {   /* These require special handling, as they differ under
10979                        folding, matching Cased there (which in the ASCII range
10980                        is the same as Alpha */
10981
10982                     SV* ascii_source;
10983                     SV* l1_source;
10984                     const char *Xname;
10985
10986                     if (FOLD && ! LOC) {
10987                         ascii_source = PL_PosixAlpha;
10988                         l1_source = PL_L1Cased;
10989                         Xname = "Cased";
10990                     }
10991                     else {
10992                         ascii_source = PL_PosixLower;
10993                         l1_source = PL_L1PosixLower;
10994                         Xname = "XPosixLower";
10995                     }
10996                     if (namedclass == ANYOF_LOWER) {
10997                         DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
10998                                     ascii_source, l1_source, Xname, listsv);
10999                     }
11000                     else {
11001                         DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass,
11002                             properties, ascii_source, l1_source, Xname, listsv);
11003                     }
11004                     break;
11005                 }
11006                 case ANYOF_PRINT:
11007                     DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11008                         PL_PosixPrint, PL_L1PosixPrint, "XPosixPrint", listsv);
11009                     break;
11010                 case ANYOF_NPRINT:
11011                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11012                         PL_PosixPrint, PL_L1PosixPrint, "XPosixPrint", listsv);
11013                     break;
11014                 case ANYOF_PUNCT:
11015                     DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11016                         PL_PosixPunct, PL_L1PosixPunct, "XPosixPunct", listsv);
11017                     break;
11018                 case ANYOF_NPUNCT:
11019                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11020                         PL_PosixPunct, PL_L1PosixPunct, "XPosixPunct", listsv);
11021                     break;
11022                 case ANYOF_PSXSPC:
11023                     DO_POSIX(ret, namedclass, properties,
11024                                             PL_PosixSpace, PL_XPosixSpace);
11025                     break;
11026                 case ANYOF_NPSXSPC:
11027                     DO_N_POSIX(ret, namedclass, properties,
11028                                             PL_PosixSpace, PL_XPosixSpace);
11029                     break;
11030                 case ANYOF_SPACE:
11031                     DO_POSIX(ret, namedclass, properties,
11032                                             PL_PerlSpace, PL_XPerlSpace);
11033                     break;
11034                 case ANYOF_NSPACE:
11035                     DO_N_POSIX(ret, namedclass, properties,
11036                                             PL_PerlSpace, PL_XPerlSpace);
11037                     break;
11038                 case ANYOF_UPPER:   /* Same as LOWER, above */
11039                 case ANYOF_NUPPER:
11040                 {
11041                     SV* ascii_source;
11042                     SV* l1_source;
11043                     const char *Xname;
11044
11045                     if (FOLD && ! LOC) {
11046                         ascii_source = PL_PosixAlpha;
11047                         l1_source = PL_L1Cased;
11048                         Xname = "Cased";
11049                     }
11050                     else {
11051                         ascii_source = PL_PosixUpper;
11052                         l1_source = PL_L1PosixUpper;
11053                         Xname = "XPosixUpper";
11054                     }
11055                     if (namedclass == ANYOF_UPPER) {
11056                         DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11057                                     ascii_source, l1_source, Xname, listsv);
11058                     }
11059                     else {
11060                         DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass,
11061                         properties, ascii_source, l1_source, Xname, listsv);
11062                     }
11063                     break;
11064                 }
11065                 case ANYOF_ALNUM:   /* Really is 'Word' */
11066                     DO_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11067                             PL_PosixWord, PL_L1PosixWord, "XPosixWord", listsv);
11068                     break;
11069                 case ANYOF_NALNUM:
11070                     DO_N_POSIX_LATIN1_ONLY_KNOWN(ret, namedclass, properties,
11071                             PL_PosixWord, PL_L1PosixWord, "XPosixWord", listsv);
11072                     break;
11073                 case ANYOF_VERTWS:
11074                     /* For these, we use the nonbitmap, as /d doesn't make a
11075                      * difference in what these match.  There would be problems
11076                      * if these characters had folds other than themselves, as
11077                      * nonbitmap is subject to folding */
11078                     _invlist_union(nonbitmap, PL_VertSpace, &nonbitmap);
11079                     break;
11080                 case ANYOF_NVERTWS:
11081                     _invlist_union_complement_2nd(nonbitmap,
11082                                                     PL_VertSpace, &nonbitmap);
11083                     break;
11084                 case ANYOF_XDIGIT:
11085                     DO_POSIX(ret, namedclass, properties,
11086                                             PL_PosixXDigit, PL_XPosixXDigit);
11087                     break;
11088                 case ANYOF_NXDIGIT:
11089                     DO_N_POSIX(ret, namedclass, properties,
11090                                             PL_PosixXDigit, PL_XPosixXDigit);
11091                     break;
11092                 case ANYOF_MAX:
11093                     /* this is to handle \p and \P */
11094                     break;
11095                 default:
11096                     vFAIL("Invalid [::] class");
11097                     break;
11098                 }
11099
11100                 continue;
11101             }
11102         } /* end of namedclass \blah */
11103
11104         if (range) {
11105             if (prevvalue > (IV)value) /* b-a */ {
11106                 const int w = RExC_parse - rangebegin;
11107                 Simple_vFAIL4("Invalid [] range \"%*.*s\"", w, w, rangebegin);
11108                 range = 0; /* not a valid range */
11109             }
11110         }
11111         else {
11112             prevvalue = value; /* save the beginning of the range */
11113             if (RExC_parse+1 < RExC_end
11114                 && *RExC_parse == '-'
11115                 && RExC_parse[1] != ']')
11116             {
11117                 RExC_parse++;
11118
11119                 /* a bad range like \w-, [:word:]- ? */
11120                 if (namedclass > OOB_NAMEDCLASS) {
11121                     if (ckWARN(WARN_REGEXP)) {
11122                         const int w =
11123                             RExC_parse >= rangebegin ?
11124                             RExC_parse - rangebegin : 0;
11125                         vWARN4(RExC_parse,
11126                                "False [] range \"%*.*s\"",
11127                                w, w, rangebegin);
11128                     }
11129                     if (!SIZE_ONLY)
11130                         stored +=
11131                             set_regclass_bit(pRExC_state, ret, '-', &l1_fold_invlist, &unicode_alternate);
11132                 } else
11133                     range = 1;  /* yeah, it's a range! */
11134                 continue;       /* but do it the next time */
11135             }
11136         }
11137
11138         /* non-Latin1 code point implies unicode semantics.  Must be set in
11139          * pass1 so is there for the whole of pass 2 */
11140         if (value > 255) {
11141             RExC_uni_semantics = 1;
11142         }
11143
11144         /* now is the next time */
11145         if (!SIZE_ONLY) {
11146             if (prevvalue < 256) {
11147                 const IV ceilvalue = value < 256 ? value : 255;
11148                 IV i;
11149 #ifdef EBCDIC
11150                 /* In EBCDIC [\x89-\x91] should include
11151                  * the \x8e but [i-j] should not. */
11152                 if (literal_endpoint == 2 &&
11153                     ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
11154                      (isUPPER(prevvalue) && isUPPER(ceilvalue))))
11155                 {
11156                     if (isLOWER(prevvalue)) {
11157                         for (i = prevvalue; i <= ceilvalue; i++)
11158                             if (isLOWER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
11159                                 stored +=
11160                                   set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
11161                             }
11162                     } else {
11163                         for (i = prevvalue; i <= ceilvalue; i++)
11164                             if (isUPPER(i) && !ANYOF_BITMAP_TEST(ret,i)) {
11165                                 stored +=
11166                                   set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
11167                             }
11168                     }
11169                 }
11170                 else
11171 #endif
11172                       for (i = prevvalue; i <= ceilvalue; i++) {
11173                         stored += set_regclass_bit(pRExC_state, ret, (U8) i, &l1_fold_invlist, &unicode_alternate);
11174                       }
11175           }
11176           if (value > 255) {
11177             const UV prevnatvalue  = NATIVE_TO_UNI(prevvalue);
11178             const UV natvalue      = NATIVE_TO_UNI(value);
11179             nonbitmap = _add_range_to_invlist(nonbitmap, prevnatvalue, natvalue);
11180         }
11181 #ifdef EBCDIC
11182             literal_endpoint = 0;
11183 #endif
11184         }
11185
11186         range = 0; /* this range (if it was one) is done now */
11187     }
11188
11189
11190
11191     if (SIZE_ONLY)
11192         return ret;
11193     /****** !SIZE_ONLY AFTER HERE *********/
11194
11195     /* If folding and there are code points above 255, we calculate all
11196      * characters that could fold to or from the ones already on the list */
11197     if (FOLD && nonbitmap) {
11198         UV start, end;  /* End points of code point ranges */
11199
11200         SV* fold_intersection = NULL;
11201
11202         /* This is a list of all the characters that participate in folds
11203             * (except marks, etc in multi-char folds */
11204         if (! PL_utf8_foldable) {
11205             SV* swash = swash_init("utf8", "Cased", &PL_sv_undef, 1, 0);
11206             PL_utf8_foldable = _swash_to_invlist(swash);
11207             SvREFCNT_dec(swash);
11208         }
11209
11210         /* This is a hash that for a particular fold gives all characters
11211             * that are involved in it */
11212         if (! PL_utf8_foldclosures) {
11213
11214             /* If we were unable to find any folds, then we likely won't be
11215              * able to find the closures.  So just create an empty list.
11216              * Folding will effectively be restricted to the non-Unicode rules
11217              * hard-coded into Perl.  (This case happens legitimately during
11218              * compilation of Perl itself before the Unicode tables are
11219              * generated) */
11220             if (invlist_len(PL_utf8_foldable) == 0) {
11221                 PL_utf8_foldclosures = newHV();
11222             } else {
11223                 /* If the folds haven't been read in, call a fold function
11224                     * to force that */
11225                 if (! PL_utf8_tofold) {
11226                     U8 dummy[UTF8_MAXBYTES+1];
11227                     STRLEN dummy_len;
11228
11229                     /* This particular string is above \xff in both UTF-8 and
11230                      * UTFEBCDIC */
11231                     to_utf8_fold((U8*) "\xC8\x80", dummy, &dummy_len);
11232                     assert(PL_utf8_tofold); /* Verify that worked */
11233                 }
11234                 PL_utf8_foldclosures = _swash_inversion_hash(PL_utf8_tofold);
11235             }
11236         }
11237
11238         /* Only the characters in this class that participate in folds need be
11239          * checked.  Get the intersection of this class and all the possible
11240          * characters that are foldable.  This can quickly narrow down a large
11241          * class */
11242         _invlist_intersection(PL_utf8_foldable, nonbitmap, &fold_intersection);
11243
11244         /* Now look at the foldable characters in this class individually */
11245         invlist_iterinit(fold_intersection);
11246         while (invlist_iternext(fold_intersection, &start, &end)) {
11247             UV j;
11248
11249             /* Look at every character in the range */
11250             for (j = start; j <= end; j++) {
11251
11252                 /* Get its fold */
11253                 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
11254                 STRLEN foldlen;
11255                 const UV f =
11256                     _to_uni_fold_flags(j, foldbuf, &foldlen,
11257                                        (allow_full_fold) ? FOLD_FLAGS_FULL : 0);
11258
11259                 if (foldlen > (STRLEN)UNISKIP(f)) {
11260
11261                     /* Any multicharacter foldings (disallowed in lookbehind
11262                      * patterns) require the following transform: [ABCDEF] ->
11263                      * (?:[ABCabcDEFd]|pq|rst) where E folds into "pq" and F
11264                      * folds into "rst", all other characters fold to single
11265                      * characters.  We save away these multicharacter foldings,
11266                      * to be later saved as part of the additional "s" data. */
11267                     if (! RExC_in_lookbehind) {
11268                         U8* loc = foldbuf;
11269                         U8* e = foldbuf + foldlen;
11270
11271                         /* If any of the folded characters of this are in the
11272                          * Latin1 range, tell the regex engine that this can
11273                          * match a non-utf8 target string.  The only multi-byte
11274                          * fold whose source is in the Latin1 range (U+00DF)
11275                          * applies only when the target string is utf8, or
11276                          * under unicode rules */
11277                         if (j > 255 || AT_LEAST_UNI_SEMANTICS) {
11278                             while (loc < e) {
11279
11280                                 /* Can't mix ascii with non- under /aa */
11281                                 if (MORE_ASCII_RESTRICTED
11282                                     && (isASCII(*loc) != isASCII(j)))
11283                                 {
11284                                     goto end_multi_fold;
11285                                 }
11286                                 if (UTF8_IS_INVARIANT(*loc)
11287                                     || UTF8_IS_DOWNGRADEABLE_START(*loc))
11288                                 {
11289                                     /* Can't mix above and below 256 under LOC
11290                                      */
11291                                     if (LOC) {
11292                                         goto end_multi_fold;
11293                                     }
11294                                     ANYOF_FLAGS(ret)
11295                                             |= ANYOF_NONBITMAP_NON_UTF8;
11296                                     break;
11297                                 }
11298                                 loc += UTF8SKIP(loc);
11299                             }
11300                         }
11301
11302                         add_alternate(&unicode_alternate, foldbuf, foldlen);
11303                     end_multi_fold: ;
11304                     }
11305
11306                     /* This is special-cased, as it is the only letter which
11307                      * has both a multi-fold and single-fold in Latin1.  All
11308                      * the other chars that have single and multi-folds are
11309                      * always in utf8, and the utf8 folding algorithm catches
11310                      * them */
11311                     if (! LOC && j == LATIN_CAPITAL_LETTER_SHARP_S) {
11312                         stored += set_regclass_bit(pRExC_state,
11313                                         ret,
11314                                         LATIN_SMALL_LETTER_SHARP_S,
11315                                         &l1_fold_invlist, &unicode_alternate);
11316                     }
11317                 }
11318                 else {
11319                     /* Single character fold.  Add everything in its fold
11320                      * closure to the list that this node should match */
11321                     SV** listp;
11322
11323                     /* The fold closures data structure is a hash with the keys
11324                      * being every character that is folded to, like 'k', and
11325                      * the values each an array of everything that folds to its
11326                      * key.  e.g. [ 'k', 'K', KELVIN_SIGN ] */
11327                     if ((listp = hv_fetch(PL_utf8_foldclosures,
11328                                     (char *) foldbuf, foldlen, FALSE)))
11329                     {
11330                         AV* list = (AV*) *listp;
11331                         IV k;
11332                         for (k = 0; k <= av_len(list); k++) {
11333                             SV** c_p = av_fetch(list, k, FALSE);
11334                             UV c;
11335                             if (c_p == NULL) {
11336                                 Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
11337                             }
11338                             c = SvUV(*c_p);
11339
11340                             /* /aa doesn't allow folds between ASCII and non-;
11341                              * /l doesn't allow them between above and below
11342                              * 256 */
11343                             if ((MORE_ASCII_RESTRICTED
11344                                  && (isASCII(c) != isASCII(j)))
11345                                     || (LOC && ((c < 256) != (j < 256))))
11346                             {
11347                                 continue;
11348                             }
11349
11350                             if (c < 256 && AT_LEAST_UNI_SEMANTICS) {
11351                                 stored += set_regclass_bit(pRExC_state,
11352                                         ret,
11353                                         (U8) c,
11354                                         &l1_fold_invlist, &unicode_alternate);
11355                             }
11356                                 /* It may be that the code point is already in
11357                                  * this range or already in the bitmap, in
11358                                  * which case we need do nothing */
11359                             else if ((c < start || c > end)
11360                                         && (c > 255
11361                                             || ! ANYOF_BITMAP_TEST(ret, c)))
11362                             {
11363                                 nonbitmap = add_cp_to_invlist(nonbitmap, c);
11364                             }
11365                         }
11366                     }
11367                 }
11368             }
11369         }
11370         SvREFCNT_dec(fold_intersection);
11371     }
11372
11373     /* Combine the two lists into one. */
11374     if (l1_fold_invlist) {
11375         if (nonbitmap) {
11376             _invlist_union(nonbitmap, l1_fold_invlist, &nonbitmap);
11377             SvREFCNT_dec(l1_fold_invlist);
11378         }
11379         else {
11380             nonbitmap = l1_fold_invlist;
11381         }
11382     }
11383
11384     /* And combine the result (if any) with any inversion list from properties.
11385      * The lists are kept separate up to now because we don't want to fold the
11386      * properties */
11387     if (properties) {
11388         if (nonbitmap) {
11389             _invlist_union(nonbitmap, properties, &nonbitmap);
11390             SvREFCNT_dec(properties);
11391         }
11392         else {
11393             nonbitmap = properties;
11394         }
11395     }
11396
11397     /* Here, <nonbitmap> contains all the code points we can determine at
11398      * compile time that we haven't put into the bitmap.  Go through it, and
11399      * for things that belong in the bitmap, put them there, and delete from
11400      * <nonbitmap> */
11401     if (nonbitmap) {
11402
11403         /* Above-ASCII code points in /d have to stay in <nonbitmap>, as they
11404          * possibly only should match when the target string is UTF-8 */
11405         UV max_cp_to_set = (DEPENDS_SEMANTICS) ? 127 : 255;
11406
11407         /* This gets set if we actually need to modify things */
11408         bool change_invlist = FALSE;
11409
11410         UV start, end;
11411
11412         /* Start looking through <nonbitmap> */
11413         invlist_iterinit(nonbitmap);
11414         while (invlist_iternext(nonbitmap, &start, &end)) {
11415             UV high;
11416             int i;
11417
11418             /* Quit if are above what we should change */
11419             if (start > max_cp_to_set) {
11420                 break;
11421             }
11422
11423             change_invlist = TRUE;
11424
11425             /* Set all the bits in the range, up to the max that we are doing */
11426             high = (end < max_cp_to_set) ? end : max_cp_to_set;
11427             for (i = start; i <= (int) high; i++) {
11428                 if (! ANYOF_BITMAP_TEST(ret, i)) {
11429                     ANYOF_BITMAP_SET(ret, i);
11430                     stored++;
11431                     prevvalue = value;
11432                     value = i;
11433                 }
11434             }
11435         }
11436
11437         /* Done with loop; remove any code points that are in the bitmap from
11438          * <nonbitmap> */
11439         if (change_invlist) {
11440             _invlist_subtract(nonbitmap,
11441                               (DEPENDS_SEMANTICS)
11442                                 ? PL_ASCII
11443                                 : PL_Latin1,
11444                               &nonbitmap);
11445         }
11446
11447         /* If have completely emptied it, remove it completely */
11448         if (invlist_len(nonbitmap) == 0) {
11449             SvREFCNT_dec(nonbitmap);
11450             nonbitmap = NULL;
11451         }
11452     }
11453
11454     /* Here, we have calculated what code points should be in the character
11455      * class.  <nonbitmap> does not overlap the bitmap except possibly in the
11456      * case of DEPENDS rules.
11457      *
11458      * Now we can see about various optimizations.  Fold calculation (which we
11459      * did above) needs to take place before inversion.  Otherwise /[^k]/i
11460      * would invert to include K, which under /i would match k, which it
11461      * shouldn't. */
11462
11463     /* Optimize inverted simple patterns (e.g. [^a-z]).  Note that we haven't
11464      * set the FOLD flag yet, so this does optimize those.  It doesn't
11465      * optimize locale.  Doing so perhaps could be done as long as there is
11466      * nothing like \w in it; some thought also would have to be given to the
11467      * interaction with above 0x100 chars */
11468     if ((ANYOF_FLAGS(ret) & ANYOF_INVERT)
11469         && ! LOC
11470         && ! unicode_alternate
11471         /* In case of /d, there are some things that should match only when in
11472          * not in the bitmap, i.e., they require UTF8 to match.  These are
11473          * listed in nonbitmap, but if ANYOF_NONBITMAP_NON_UTF8 is set in this
11474          * case, they don't require UTF8, so can invert here */
11475         && (! nonbitmap
11476             || ! DEPENDS_SEMANTICS
11477             || (ANYOF_FLAGS(ret) & ANYOF_NONBITMAP_NON_UTF8))
11478         && SvCUR(listsv) == initial_listsv_len)
11479     {
11480         int i;
11481         if (! nonbitmap) {
11482             for (i = 0; i < 256; ++i) {
11483                 if (ANYOF_BITMAP_TEST(ret, i)) {
11484                     ANYOF_BITMAP_CLEAR(ret, i);
11485                 }
11486                 else {
11487                     ANYOF_BITMAP_SET(ret, i);
11488                     prevvalue = value;
11489                     value = i;
11490                 }
11491             }
11492             /* The inversion means that everything above 255 is matched */
11493             ANYOF_FLAGS(ret) |= ANYOF_UNICODE_ALL;
11494         }
11495         else {
11496             /* Here, also has things outside the bitmap that may overlap with
11497              * the bitmap.  We have to sync them up, so that they get inverted
11498              * in both places.  Earlier, we removed all overlaps except in the
11499              * case of /d rules, so no syncing is needed except for this case
11500              */
11501             SV *remove_list = NULL;
11502
11503             if (DEPENDS_SEMANTICS) {
11504                 UV start, end;
11505
11506                 /* Set the bits that correspond to the ones that aren't in the
11507                  * bitmap.  Otherwise, when we invert, we'll miss these.
11508                  * Earlier, we removed from the nonbitmap all code points
11509                  * < 128, so there is no extra work here */
11510                 invlist_iterinit(nonbitmap);
11511                 while (invlist_iternext(nonbitmap, &start, &end)) {
11512                     if (start > 255) {  /* The bit map goes to 255 */
11513                         break;
11514                     }
11515                     if (end > 255) {
11516                         end = 255;
11517                     }
11518                     for (i = start; i <= (int) end; ++i) {
11519                         ANYOF_BITMAP_SET(ret, i);
11520                         prevvalue = value;
11521                         value = i;
11522                     }
11523                 }
11524             }
11525
11526             /* Now invert both the bitmap and the nonbitmap.  Anything in the
11527              * bitmap has to also be removed from the non-bitmap, but again,
11528              * there should not be overlap unless is /d rules. */
11529             _invlist_invert(nonbitmap);
11530
11531             /* Any swash can't be used as-is, because we've inverted things */
11532             if (swash) {
11533                 SvREFCNT_dec(swash);
11534                 swash = NULL;
11535             }
11536
11537             for (i = 0; i < 256; ++i) {
11538                 if (ANYOF_BITMAP_TEST(ret, i)) {
11539                     ANYOF_BITMAP_CLEAR(ret, i);
11540                     if (DEPENDS_SEMANTICS) {
11541                         if (! remove_list) {
11542                             remove_list = _new_invlist(2);
11543                         }
11544                         remove_list = add_cp_to_invlist(remove_list, i);
11545                     }
11546                 }
11547                 else {
11548                     ANYOF_BITMAP_SET(ret, i);
11549                     prevvalue = value;
11550                     value = i;
11551                 }
11552             }
11553
11554             /* And do the removal */
11555             if (DEPENDS_SEMANTICS) {
11556                 if (remove_list) {
11557                     _invlist_subtract(nonbitmap, remove_list, &nonbitmap);
11558                     SvREFCNT_dec(remove_list);
11559                 }
11560             }
11561             else {
11562                 /* There is no overlap for non-/d, so just delete anything
11563                  * below 256 */
11564                 _invlist_intersection(nonbitmap, PL_AboveLatin1, &nonbitmap);
11565             }
11566         }
11567
11568         stored = 256 - stored;
11569
11570         /* Clear the invert flag since have just done it here */
11571         ANYOF_FLAGS(ret) &= ~ANYOF_INVERT;
11572     }
11573
11574     /* Folding in the bitmap is taken care of above, but not for locale (for
11575      * which we have to wait to see what folding is in effect at runtime), and
11576      * for some things not in the bitmap (only the upper latin folds in this
11577      * case, as all other single-char folding has been set above).  Set
11578      * run-time fold flag for these */
11579     if (FOLD && (LOC
11580                 || (DEPENDS_SEMANTICS
11581                     && nonbitmap
11582                     && ! (ANYOF_FLAGS(ret) & ANYOF_NONBITMAP_NON_UTF8))
11583                 || unicode_alternate))
11584     {
11585         ANYOF_FLAGS(ret) |= ANYOF_LOC_NONBITMAP_FOLD;
11586     }
11587
11588     /* A single character class can be "optimized" into an EXACTish node.
11589      * Note that since we don't currently count how many characters there are
11590      * outside the bitmap, we are XXX missing optimization possibilities for
11591      * them.  This optimization can't happen unless this is a truly single
11592      * character class, which means that it can't be an inversion into a
11593      * many-character class, and there must be no possibility of there being
11594      * things outside the bitmap.  'stored' (only) for locales doesn't include
11595      * \w, etc, so have to make a special test that they aren't present
11596      *
11597      * Similarly A 2-character class of the very special form like [bB] can be
11598      * optimized into an EXACTFish node, but only for non-locales, and for
11599      * characters which only have the two folds; so things like 'fF' and 'Ii'
11600      * wouldn't work because they are part of the fold of 'LATIN SMALL LIGATURE
11601      * FI'. */
11602     if (! nonbitmap
11603         && ! unicode_alternate
11604         && SvCUR(listsv) == initial_listsv_len
11605         && ! (ANYOF_FLAGS(ret) & (ANYOF_INVERT|ANYOF_UNICODE_ALL))
11606         && (((stored == 1 && ((! (ANYOF_FLAGS(ret) & ANYOF_LOCALE))
11607                               || (! ANYOF_CLASS_TEST_ANY_SET(ret)))))
11608             || (stored == 2 && ((! (ANYOF_FLAGS(ret) & ANYOF_LOCALE))
11609                                  && (! _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(value))
11610                                  /* If the latest code point has a fold whose
11611                                   * bit is set, it must be the only other one */
11612                                 && ((prevvalue = PL_fold_latin1[value]) != (IV)value)
11613                                  && ANYOF_BITMAP_TEST(ret, prevvalue)))))
11614     {
11615         /* Note that the information needed to decide to do this optimization
11616          * is not currently available until the 2nd pass, and that the actually
11617          * used EXACTish node takes less space than the calculated ANYOF node,
11618          * and hence the amount of space calculated in the first pass is larger
11619          * than actually used, so this optimization doesn't gain us any space.
11620          * But an EXACT node is faster than an ANYOF node, and can be combined
11621          * with any adjacent EXACT nodes later by the optimizer for further
11622          * gains.  The speed of executing an EXACTF is similar to an ANYOF
11623          * node, so the optimization advantage comes from the ability to join
11624          * it to adjacent EXACT nodes */
11625
11626         const char * cur_parse= RExC_parse;
11627         U8 op;
11628         RExC_emit = (regnode *)orig_emit;
11629         RExC_parse = (char *)orig_parse;
11630
11631         if (stored == 1) {
11632
11633             /* A locale node with one point can be folded; all the other cases
11634              * with folding will have two points, since we calculate them above
11635              */
11636             if (ANYOF_FLAGS(ret) & ANYOF_LOC_NONBITMAP_FOLD) {
11637                  op = EXACTFL;
11638             }
11639             else {
11640                 op = EXACT;
11641             }
11642         }
11643         else {   /* else 2 chars in the bit map: the folds of each other */
11644
11645             /* Use the folded value, which for the cases where we get here,
11646              * is just the lower case of the current one (which may resolve to
11647              * itself, or to the other one */
11648             value = toLOWER_LATIN1(value);
11649
11650             /* To join adjacent nodes, they must be the exact EXACTish type.
11651              * Try to use the most likely type, by using EXACTFA if possible,
11652              * then EXACTFU if the regex calls for it, or is required because
11653              * the character is non-ASCII.  (If <value> is ASCII, its fold is
11654              * also ASCII for the cases where we get here.) */
11655             if (MORE_ASCII_RESTRICTED && isASCII(value)) {
11656                 op = EXACTFA;
11657             }
11658             else if (AT_LEAST_UNI_SEMANTICS || !isASCII(value)) {
11659                 op = EXACTFU;
11660             }
11661             else {    /* Otherwise, more likely to be EXACTF type */
11662                 op = EXACTF;
11663             }
11664         }
11665
11666         ret = reg_node(pRExC_state, op);
11667         RExC_parse = (char *)cur_parse;
11668         if (UTF && ! NATIVE_IS_INVARIANT(value)) {
11669             *STRING(ret)= UTF8_EIGHT_BIT_HI((U8) value);
11670             *(STRING(ret) + 1)= UTF8_EIGHT_BIT_LO((U8) value);
11671             STR_LEN(ret)= 2;
11672             RExC_emit += STR_SZ(2);
11673         }
11674         else {
11675             *STRING(ret)= (char)value;
11676             STR_LEN(ret)= 1;
11677             RExC_emit += STR_SZ(1);
11678         }
11679         SvREFCNT_dec(listsv);
11680         return ret;
11681     }
11682
11683     /* If there is a swash and more than one element, we can't use the swash in
11684      * the optimization below. */
11685     if (swash && element_count > 1) {
11686         SvREFCNT_dec(swash);
11687         swash = NULL;
11688     }
11689     if (! nonbitmap
11690         && SvCUR(listsv) == initial_listsv_len
11691         && ! unicode_alternate)
11692     {
11693         ARG_SET(ret, ANYOF_NONBITMAP_EMPTY);
11694         SvREFCNT_dec(listsv);
11695         SvREFCNT_dec(unicode_alternate);
11696     }
11697     else {
11698         /* av[0] stores the character class description in its textual form:
11699          *       used later (regexec.c:Perl_regclass_swash()) to initialize the
11700          *       appropriate swash, and is also useful for dumping the regnode.
11701          * av[1] if NULL, is a placeholder to later contain the swash computed
11702          *       from av[0].  But if no further computation need be done, the
11703          *       swash is stored there now.
11704          * av[2] stores the multicharacter foldings, used later in
11705          *       regexec.c:S_reginclass().
11706          * av[3] stores the nonbitmap inversion list for use in addition or
11707          *       instead of av[0]; not used if av[1] isn't NULL
11708          * av[4] is set if any component of the class is from a user-defined
11709          *       property; not used if av[1] isn't NULL */
11710         AV * const av = newAV();
11711         SV *rv;
11712
11713         av_store(av, 0, (SvCUR(listsv) == initial_listsv_len)
11714                         ? &PL_sv_undef
11715                         : listsv);
11716         if (swash) {
11717             av_store(av, 1, swash);
11718             SvREFCNT_dec(nonbitmap);
11719         }
11720         else {
11721             av_store(av, 1, NULL);
11722             if (nonbitmap) {
11723                 av_store(av, 3, nonbitmap);
11724                 av_store(av, 4, newSVuv(has_user_defined_property));
11725             }
11726         }
11727
11728         /* Store any computed multi-char folds only if we are allowing
11729          * them */
11730         if (allow_full_fold) {
11731             av_store(av, 2, MUTABLE_SV(unicode_alternate));
11732             if (unicode_alternate) { /* This node is variable length */
11733                 OP(ret) = ANYOFV;
11734             }
11735         }
11736         else {
11737             av_store(av, 2, NULL);
11738         }
11739         rv = newRV_noinc(MUTABLE_SV(av));
11740         n = add_data(pRExC_state, 1, "s");
11741         RExC_rxi->data->data[n] = (void*)rv;
11742         ARG_SET(ret, n);
11743     }
11744     return ret;
11745 }
11746
11747
11748 /* reg_skipcomment()
11749
11750    Absorbs an /x style # comments from the input stream.
11751    Returns true if there is more text remaining in the stream.
11752    Will set the REG_SEEN_RUN_ON_COMMENT flag if the comment
11753    terminates the pattern without including a newline.
11754
11755    Note its the callers responsibility to ensure that we are
11756    actually in /x mode
11757
11758 */
11759
11760 STATIC bool
11761 S_reg_skipcomment(pTHX_ RExC_state_t *pRExC_state)
11762 {
11763     bool ended = 0;
11764
11765     PERL_ARGS_ASSERT_REG_SKIPCOMMENT;
11766
11767     while (RExC_parse < RExC_end)
11768         if (*RExC_parse++ == '\n') {
11769             ended = 1;
11770             break;
11771         }
11772     if (!ended) {
11773         /* we ran off the end of the pattern without ending
11774            the comment, so we have to add an \n when wrapping */
11775         RExC_seen |= REG_SEEN_RUN_ON_COMMENT;
11776         return 0;
11777     } else
11778         return 1;
11779 }
11780
11781 /* nextchar()
11782
11783    Advances the parse position, and optionally absorbs
11784    "whitespace" from the inputstream.
11785
11786    Without /x "whitespace" means (?#...) style comments only,
11787    with /x this means (?#...) and # comments and whitespace proper.
11788
11789    Returns the RExC_parse point from BEFORE the scan occurs.
11790
11791    This is the /x friendly way of saying RExC_parse++.
11792 */
11793
11794 STATIC char*
11795 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
11796 {
11797     char* const retval = RExC_parse++;
11798
11799     PERL_ARGS_ASSERT_NEXTCHAR;
11800
11801     for (;;) {
11802         if (RExC_end - RExC_parse >= 3
11803             && *RExC_parse == '('
11804             && RExC_parse[1] == '?'
11805             && RExC_parse[2] == '#')
11806         {
11807             while (*RExC_parse != ')') {
11808                 if (RExC_parse == RExC_end)
11809                     FAIL("Sequence (?#... not terminated");
11810                 RExC_parse++;
11811             }
11812             RExC_parse++;
11813             continue;
11814         }
11815         if (RExC_flags & RXf_PMf_EXTENDED) {
11816             if (isSPACE(*RExC_parse)) {
11817                 RExC_parse++;
11818                 continue;
11819             }
11820             else if (*RExC_parse == '#') {
11821                 if ( reg_skipcomment( pRExC_state ) )
11822                     continue;
11823             }
11824         }
11825         return retval;
11826     }
11827 }
11828
11829 /*
11830 - reg_node - emit a node
11831 */
11832 STATIC regnode *                        /* Location. */
11833 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
11834 {
11835     dVAR;
11836     register regnode *ptr;
11837     regnode * const ret = RExC_emit;
11838     GET_RE_DEBUG_FLAGS_DECL;
11839
11840     PERL_ARGS_ASSERT_REG_NODE;
11841
11842     if (SIZE_ONLY) {
11843         SIZE_ALIGN(RExC_size);
11844         RExC_size += 1;
11845         return(ret);
11846     }
11847     if (RExC_emit >= RExC_emit_bound)
11848         Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d, %p>=%p",
11849                    op, RExC_emit, RExC_emit_bound);
11850
11851     NODE_ALIGN_FILL(ret);
11852     ptr = ret;
11853     FILL_ADVANCE_NODE(ptr, op);
11854     REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (ptr) - 1);
11855 #ifdef RE_TRACK_PATTERN_OFFSETS
11856     if (RExC_offsets) {         /* MJD */
11857         MJD_OFFSET_DEBUG(("%s:%d: (op %s) %s %"UVuf" (len %"UVuf") (max %"UVuf").\n", 
11858               "reg_node", __LINE__, 
11859               PL_reg_name[op],
11860               (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] 
11861                 ? "Overwriting end of array!\n" : "OK",
11862               (UV)(RExC_emit - RExC_emit_start),
11863               (UV)(RExC_parse - RExC_start),
11864               (UV)RExC_offsets[0])); 
11865         Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
11866     }
11867 #endif
11868     RExC_emit = ptr;
11869     return(ret);
11870 }
11871
11872 /*
11873 - reganode - emit a node with an argument
11874 */
11875 STATIC regnode *                        /* Location. */
11876 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
11877 {
11878     dVAR;
11879     register regnode *ptr;
11880     regnode * const ret = RExC_emit;
11881     GET_RE_DEBUG_FLAGS_DECL;
11882
11883     PERL_ARGS_ASSERT_REGANODE;
11884
11885     if (SIZE_ONLY) {
11886         SIZE_ALIGN(RExC_size);
11887         RExC_size += 2;
11888         /* 
11889            We can't do this:
11890            
11891            assert(2==regarglen[op]+1); 
11892
11893            Anything larger than this has to allocate the extra amount.
11894            If we changed this to be:
11895            
11896            RExC_size += (1 + regarglen[op]);
11897            
11898            then it wouldn't matter. Its not clear what side effect
11899            might come from that so its not done so far.
11900            -- dmq
11901         */
11902         return(ret);
11903     }
11904     if (RExC_emit >= RExC_emit_bound)
11905         Perl_croak(aTHX_ "panic: reg_node overrun trying to emit %d, %p>=%p",
11906                    op, RExC_emit, RExC_emit_bound);
11907
11908     NODE_ALIGN_FILL(ret);
11909     ptr = ret;
11910     FILL_ADVANCE_NODE_ARG(ptr, op, arg);
11911     REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (ptr) - 2);
11912 #ifdef RE_TRACK_PATTERN_OFFSETS
11913     if (RExC_offsets) {         /* MJD */
11914         MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n", 
11915               "reganode",
11916               __LINE__,
11917               PL_reg_name[op],
11918               (UV)(RExC_emit - RExC_emit_start) > RExC_offsets[0] ? 
11919               "Overwriting end of array!\n" : "OK",
11920               (UV)(RExC_emit - RExC_emit_start),
11921               (UV)(RExC_parse - RExC_start),
11922               (UV)RExC_offsets[0])); 
11923         Set_Cur_Node_Offset;
11924     }
11925 #endif            
11926     RExC_emit = ptr;
11927     return(ret);
11928 }
11929
11930 /*
11931 - reguni - emit (if appropriate) a Unicode character
11932 */
11933 STATIC STRLEN
11934 S_reguni(pTHX_ const RExC_state_t *pRExC_state, UV uv, char* s)
11935 {
11936     dVAR;
11937
11938     PERL_ARGS_ASSERT_REGUNI;
11939
11940     return SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s);
11941 }
11942
11943 /*
11944 - reginsert - insert an operator in front of already-emitted operand
11945 *
11946 * Means relocating the operand.
11947 */
11948 STATIC void
11949 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd, U32 depth)
11950 {
11951     dVAR;
11952     register regnode *src;
11953     register regnode *dst;
11954     register regnode *place;
11955     const int offset = regarglen[(U8)op];
11956     const int size = NODE_STEP_REGNODE + offset;
11957     GET_RE_DEBUG_FLAGS_DECL;
11958
11959     PERL_ARGS_ASSERT_REGINSERT;
11960     PERL_UNUSED_ARG(depth);
11961 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
11962     DEBUG_PARSE_FMT("inst"," - %s",PL_reg_name[op]);
11963     if (SIZE_ONLY) {
11964         RExC_size += size;
11965         return;
11966     }
11967
11968     src = RExC_emit;
11969     RExC_emit += size;
11970     dst = RExC_emit;
11971     if (RExC_open_parens) {
11972         int paren;
11973         /*DEBUG_PARSE_FMT("inst"," - %"IVdf, (IV)RExC_npar);*/
11974         for ( paren=0 ; paren < RExC_npar ; paren++ ) {
11975             if ( RExC_open_parens[paren] >= opnd ) {
11976                 /*DEBUG_PARSE_FMT("open"," - %d",size);*/
11977                 RExC_open_parens[paren] += size;
11978             } else {
11979                 /*DEBUG_PARSE_FMT("open"," - %s","ok");*/
11980             }
11981             if ( RExC_close_parens[paren] >= opnd ) {
11982                 /*DEBUG_PARSE_FMT("close"," - %d",size);*/
11983                 RExC_close_parens[paren] += size;
11984             } else {
11985                 /*DEBUG_PARSE_FMT("close"," - %s","ok");*/
11986             }
11987         }
11988     }
11989
11990     while (src > opnd) {
11991         StructCopy(--src, --dst, regnode);
11992 #ifdef RE_TRACK_PATTERN_OFFSETS
11993         if (RExC_offsets) {     /* MJD 20010112 */
11994             MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %"UVuf" -> %"UVuf" (max %"UVuf").\n",
11995                   "reg_insert",
11996                   __LINE__,
11997                   PL_reg_name[op],
11998                   (UV)(dst - RExC_emit_start) > RExC_offsets[0] 
11999                     ? "Overwriting end of array!\n" : "OK",
12000                   (UV)(src - RExC_emit_start),
12001                   (UV)(dst - RExC_emit_start),
12002                   (UV)RExC_offsets[0])); 
12003             Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
12004             Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
12005         }
12006 #endif
12007     }
12008     
12009
12010     place = opnd;               /* Op node, where operand used to be. */
12011 #ifdef RE_TRACK_PATTERN_OFFSETS
12012     if (RExC_offsets) {         /* MJD */
12013         MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %"UVuf" <- %"UVuf" (max %"UVuf").\n", 
12014               "reginsert",
12015               __LINE__,
12016               PL_reg_name[op],
12017               (UV)(place - RExC_emit_start) > RExC_offsets[0] 
12018               ? "Overwriting end of array!\n" : "OK",
12019               (UV)(place - RExC_emit_start),
12020               (UV)(RExC_parse - RExC_start),
12021               (UV)RExC_offsets[0]));
12022         Set_Node_Offset(place, RExC_parse);
12023         Set_Node_Length(place, 1);
12024     }
12025 #endif    
12026     src = NEXTOPER(place);
12027     FILL_ADVANCE_NODE(place, op);
12028     REH_CALL_COMP_NODE_HOOK(pRExC_state->rx, (place) - 1);
12029     Zero(src, offset, regnode);
12030 }
12031
12032 /*
12033 - regtail - set the next-pointer at the end of a node chain of p to val.
12034 - SEE ALSO: regtail_study
12035 */
12036 /* TODO: All three parms should be const */
12037 STATIC void
12038 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
12039 {
12040     dVAR;
12041     register regnode *scan;
12042     GET_RE_DEBUG_FLAGS_DECL;
12043
12044     PERL_ARGS_ASSERT_REGTAIL;
12045 #ifndef DEBUGGING
12046     PERL_UNUSED_ARG(depth);
12047 #endif
12048
12049     if (SIZE_ONLY)
12050         return;
12051
12052     /* Find last node. */
12053     scan = p;
12054     for (;;) {
12055         regnode * const temp = regnext(scan);
12056         DEBUG_PARSE_r({
12057             SV * const mysv=sv_newmortal();
12058             DEBUG_PARSE_MSG((scan==p ? "tail" : ""));
12059             regprop(RExC_rx, mysv, scan);
12060             PerlIO_printf(Perl_debug_log, "~ %s (%d) %s %s\n",
12061                 SvPV_nolen_const(mysv), REG_NODE_NUM(scan),
12062                     (temp == NULL ? "->" : ""),
12063                     (temp == NULL ? PL_reg_name[OP(val)] : "")
12064             );
12065         });
12066         if (temp == NULL)
12067             break;
12068         scan = temp;
12069     }
12070
12071     if (reg_off_by_arg[OP(scan)]) {
12072         ARG_SET(scan, val - scan);
12073     }
12074     else {
12075         NEXT_OFF(scan) = val - scan;
12076     }
12077 }
12078
12079 #ifdef DEBUGGING
12080 /*
12081 - regtail_study - set the next-pointer at the end of a node chain of p to val.
12082 - Look for optimizable sequences at the same time.
12083 - currently only looks for EXACT chains.
12084
12085 This is experimental code. The idea is to use this routine to perform 
12086 in place optimizations on branches and groups as they are constructed,
12087 with the long term intention of removing optimization from study_chunk so
12088 that it is purely analytical.
12089
12090 Currently only used when in DEBUG mode. The macro REGTAIL_STUDY() is used
12091 to control which is which.
12092
12093 */
12094 /* TODO: All four parms should be const */
12095
12096 STATIC U8
12097 S_regtail_study(pTHX_ RExC_state_t *pRExC_state, regnode *p, const regnode *val,U32 depth)
12098 {
12099     dVAR;
12100     register regnode *scan;
12101     U8 exact = PSEUDO;
12102 #ifdef EXPERIMENTAL_INPLACESCAN
12103     I32 min = 0;
12104 #endif
12105     GET_RE_DEBUG_FLAGS_DECL;
12106
12107     PERL_ARGS_ASSERT_REGTAIL_STUDY;
12108
12109
12110     if (SIZE_ONLY)
12111         return exact;
12112
12113     /* Find last node. */
12114
12115     scan = p;
12116     for (;;) {
12117         regnode * const temp = regnext(scan);
12118 #ifdef EXPERIMENTAL_INPLACESCAN
12119         if (PL_regkind[OP(scan)] == EXACT) {
12120             bool has_exactf_sharp_s;    /* Unexamined in this routine */
12121             if (join_exact(pRExC_state,scan,&min, &has_exactf_sharp_s, 1,val,depth+1))
12122                 return EXACT;
12123         }
12124 #endif
12125         if ( exact ) {
12126             switch (OP(scan)) {
12127                 case EXACT:
12128                 case EXACTF:
12129                 case EXACTFA:
12130                 case EXACTFU:
12131                 case EXACTFU_SS:
12132                 case EXACTFU_TRICKYFOLD:
12133                 case EXACTFL:
12134                         if( exact == PSEUDO )
12135                             exact= OP(scan);
12136                         else if ( exact != OP(scan) )
12137                             exact= 0;
12138                 case NOTHING:
12139                     break;
12140                 default:
12141                     exact= 0;
12142             }
12143         }
12144         DEBUG_PARSE_r({
12145             SV * const mysv=sv_newmortal();
12146             DEBUG_PARSE_MSG((scan==p ? "tsdy" : ""));
12147             regprop(RExC_rx, mysv, scan);
12148             PerlIO_printf(Perl_debug_log, "~ %s (%d) -> %s\n",
12149                 SvPV_nolen_const(mysv),
12150                 REG_NODE_NUM(scan),
12151                 PL_reg_name[exact]);
12152         });
12153         if (temp == NULL)
12154             break;
12155         scan = temp;
12156     }
12157     DEBUG_PARSE_r({
12158         SV * const mysv_val=sv_newmortal();
12159         DEBUG_PARSE_MSG("");
12160         regprop(RExC_rx, mysv_val, val);
12161         PerlIO_printf(Perl_debug_log, "~ attach to %s (%"IVdf") offset to %"IVdf"\n",
12162                       SvPV_nolen_const(mysv_val),
12163                       (IV)REG_NODE_NUM(val),
12164                       (IV)(val - scan)
12165         );
12166     });
12167     if (reg_off_by_arg[OP(scan)]) {
12168         ARG_SET(scan, val - scan);
12169     }
12170     else {
12171         NEXT_OFF(scan) = val - scan;
12172     }
12173
12174     return exact;
12175 }
12176 #endif
12177
12178 /*
12179  - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
12180  */
12181 #ifdef DEBUGGING
12182 static void 
12183 S_regdump_extflags(pTHX_ const char *lead, const U32 flags)
12184 {
12185     int bit;
12186     int set=0;
12187     regex_charset cs;
12188
12189     for (bit=0; bit<32; bit++) {
12190         if (flags & (1<<bit)) {
12191             if ((1<<bit) & RXf_PMf_CHARSET) {   /* Output separately, below */
12192                 continue;
12193             }
12194             if (!set++ && lead) 
12195                 PerlIO_printf(Perl_debug_log, "%s",lead);
12196             PerlIO_printf(Perl_debug_log, "%s ",PL_reg_extflags_name[bit]);
12197         }               
12198     }      
12199     if ((cs = get_regex_charset(flags)) != REGEX_DEPENDS_CHARSET) {
12200             if (!set++ && lead) {
12201                 PerlIO_printf(Perl_debug_log, "%s",lead);
12202             }
12203             switch (cs) {
12204                 case REGEX_UNICODE_CHARSET:
12205                     PerlIO_printf(Perl_debug_log, "UNICODE");
12206                     break;
12207                 case REGEX_LOCALE_CHARSET:
12208                     PerlIO_printf(Perl_debug_log, "LOCALE");
12209                     break;
12210                 case REGEX_ASCII_RESTRICTED_CHARSET:
12211                     PerlIO_printf(Perl_debug_log, "ASCII-RESTRICTED");
12212                     break;
12213                 case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
12214                     PerlIO_printf(Perl_debug_log, "ASCII-MORE_RESTRICTED");
12215                     break;
12216                 default:
12217                     PerlIO_printf(Perl_debug_log, "UNKNOWN CHARACTER SET");
12218                     break;
12219             }
12220     }
12221     if (lead)  {
12222         if (set) 
12223             PerlIO_printf(Perl_debug_log, "\n");
12224         else 
12225             PerlIO_printf(Perl_debug_log, "%s[none-set]\n",lead);
12226     }            
12227 }   
12228 #endif
12229
12230 void
12231 Perl_regdump(pTHX_ const regexp *r)
12232 {
12233 #ifdef DEBUGGING
12234     dVAR;
12235     SV * const sv = sv_newmortal();
12236     SV *dsv= sv_newmortal();
12237     RXi_GET_DECL(r,ri);
12238     GET_RE_DEBUG_FLAGS_DECL;
12239
12240     PERL_ARGS_ASSERT_REGDUMP;
12241
12242     (void)dumpuntil(r, ri->program, ri->program + 1, NULL, NULL, sv, 0, 0);
12243
12244     /* Header fields of interest. */
12245     if (r->anchored_substr) {
12246         RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->anchored_substr), 
12247             RE_SV_DUMPLEN(r->anchored_substr), 30);
12248         PerlIO_printf(Perl_debug_log,
12249                       "anchored %s%s at %"IVdf" ",
12250                       s, RE_SV_TAIL(r->anchored_substr),
12251                       (IV)r->anchored_offset);
12252     } else if (r->anchored_utf8) {
12253         RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->anchored_utf8), 
12254             RE_SV_DUMPLEN(r->anchored_utf8), 30);
12255         PerlIO_printf(Perl_debug_log,
12256                       "anchored utf8 %s%s at %"IVdf" ",
12257                       s, RE_SV_TAIL(r->anchored_utf8),
12258                       (IV)r->anchored_offset);
12259     }                 
12260     if (r->float_substr) {
12261         RE_PV_QUOTED_DECL(s, 0, dsv, SvPVX_const(r->float_substr), 
12262             RE_SV_DUMPLEN(r->float_substr), 30);
12263         PerlIO_printf(Perl_debug_log,
12264                       "floating %s%s at %"IVdf"..%"UVuf" ",
12265                       s, RE_SV_TAIL(r->float_substr),
12266                       (IV)r->float_min_offset, (UV)r->float_max_offset);
12267     } else if (r->float_utf8) {
12268         RE_PV_QUOTED_DECL(s, 1, dsv, SvPVX_const(r->float_utf8), 
12269             RE_SV_DUMPLEN(r->float_utf8), 30);
12270         PerlIO_printf(Perl_debug_log,
12271                       "floating utf8 %s%s at %"IVdf"..%"UVuf" ",
12272                       s, RE_SV_TAIL(r->float_utf8),
12273                       (IV)r->float_min_offset, (UV)r->float_max_offset);
12274     }
12275     if (r->check_substr || r->check_utf8)
12276         PerlIO_printf(Perl_debug_log,
12277                       (const char *)
12278                       (r->check_substr == r->float_substr
12279                        && r->check_utf8 == r->float_utf8
12280                        ? "(checking floating" : "(checking anchored"));
12281     if (r->extflags & RXf_NOSCAN)
12282         PerlIO_printf(Perl_debug_log, " noscan");
12283     if (r->extflags & RXf_CHECK_ALL)
12284         PerlIO_printf(Perl_debug_log, " isall");
12285     if (r->check_substr || r->check_utf8)
12286         PerlIO_printf(Perl_debug_log, ") ");
12287
12288     if (ri->regstclass) {
12289         regprop(r, sv, ri->regstclass);
12290         PerlIO_printf(Perl_debug_log, "stclass %s ", SvPVX_const(sv));
12291     }
12292     if (r->extflags & RXf_ANCH) {
12293         PerlIO_printf(Perl_debug_log, "anchored");
12294         if (r->extflags & RXf_ANCH_BOL)
12295             PerlIO_printf(Perl_debug_log, "(BOL)");
12296         if (r->extflags & RXf_ANCH_MBOL)
12297             PerlIO_printf(Perl_debug_log, "(MBOL)");
12298         if (r->extflags & RXf_ANCH_SBOL)
12299             PerlIO_printf(Perl_debug_log, "(SBOL)");
12300         if (r->extflags & RXf_ANCH_GPOS)
12301             PerlIO_printf(Perl_debug_log, "(GPOS)");
12302         PerlIO_putc(Perl_debug_log, ' ');
12303     }
12304     if (r->extflags & RXf_GPOS_SEEN)
12305         PerlIO_printf(Perl_debug_log, "GPOS:%"UVuf" ", (UV)r->gofs);
12306     if (r->intflags & PREGf_SKIP)
12307         PerlIO_printf(Perl_debug_log, "plus ");
12308     if (r->intflags & PREGf_IMPLICIT)
12309         PerlIO_printf(Perl_debug_log, "implicit ");
12310     PerlIO_printf(Perl_debug_log, "minlen %"IVdf" ", (IV)r->minlen);
12311     if (r->extflags & RXf_EVAL_SEEN)
12312         PerlIO_printf(Perl_debug_log, "with eval ");
12313     PerlIO_printf(Perl_debug_log, "\n");
12314     DEBUG_FLAGS_r(regdump_extflags("r->extflags: ",r->extflags));            
12315 #else
12316     PERL_ARGS_ASSERT_REGDUMP;
12317     PERL_UNUSED_CONTEXT;
12318     PERL_UNUSED_ARG(r);
12319 #endif  /* DEBUGGING */
12320 }
12321
12322 /*
12323 - regprop - printable representation of opcode
12324 */
12325 #define EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags) \
12326 STMT_START { \
12327         if (do_sep) {                           \
12328             Perl_sv_catpvf(aTHX_ sv,"%s][%s",PL_colors[1],PL_colors[0]); \
12329             if (flags & ANYOF_INVERT)           \
12330                 /*make sure the invert info is in each */ \
12331                 sv_catpvs(sv, "^");             \
12332             do_sep = 0;                         \
12333         }                                       \
12334 } STMT_END
12335
12336 void
12337 Perl_regprop(pTHX_ const regexp *prog, SV *sv, const regnode *o)
12338 {
12339 #ifdef DEBUGGING
12340     dVAR;
12341     register int k;
12342     RXi_GET_DECL(prog,progi);
12343     GET_RE_DEBUG_FLAGS_DECL;
12344     
12345     PERL_ARGS_ASSERT_REGPROP;
12346
12347     sv_setpvs(sv, "");
12348
12349     if (OP(o) > REGNODE_MAX)            /* regnode.type is unsigned */
12350         /* It would be nice to FAIL() here, but this may be called from
12351            regexec.c, and it would be hard to supply pRExC_state. */
12352         Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(o), (int)REGNODE_MAX);
12353     sv_catpv(sv, PL_reg_name[OP(o)]); /* Take off const! */
12354
12355     k = PL_regkind[OP(o)];
12356
12357     if (k == EXACT) {
12358         sv_catpvs(sv, " ");
12359         /* Using is_utf8_string() (via PERL_PV_UNI_DETECT) 
12360          * is a crude hack but it may be the best for now since 
12361          * we have no flag "this EXACTish node was UTF-8" 
12362          * --jhi */
12363         pv_pretty(sv, STRING(o), STR_LEN(o), 60, PL_colors[0], PL_colors[1],
12364                   PERL_PV_ESCAPE_UNI_DETECT |
12365                   PERL_PV_ESCAPE_NONASCII   |
12366                   PERL_PV_PRETTY_ELLIPSES   |
12367                   PERL_PV_PRETTY_LTGT       |
12368                   PERL_PV_PRETTY_NOCLEAR
12369                   );
12370     } else if (k == TRIE) {
12371         /* print the details of the trie in dumpuntil instead, as
12372          * progi->data isn't available here */
12373         const char op = OP(o);
12374         const U32 n = ARG(o);
12375         const reg_ac_data * const ac = IS_TRIE_AC(op) ?
12376                (reg_ac_data *)progi->data->data[n] :
12377                NULL;
12378         const reg_trie_data * const trie
12379             = (reg_trie_data*)progi->data->data[!IS_TRIE_AC(op) ? n : ac->trie];
12380         
12381         Perl_sv_catpvf(aTHX_ sv, "-%s",PL_reg_name[o->flags]);
12382         DEBUG_TRIE_COMPILE_r(
12383             Perl_sv_catpvf(aTHX_ sv,
12384                 "<S:%"UVuf"/%"IVdf" W:%"UVuf" L:%"UVuf"/%"UVuf" C:%"UVuf"/%"UVuf">",
12385                 (UV)trie->startstate,
12386                 (IV)trie->statecount-1, /* -1 because of the unused 0 element */
12387                 (UV)trie->wordcount,
12388                 (UV)trie->minlen,
12389                 (UV)trie->maxlen,
12390                 (UV)TRIE_CHARCOUNT(trie),
12391                 (UV)trie->uniquecharcount
12392             )
12393         );
12394         if ( IS_ANYOF_TRIE(op) || trie->bitmap ) {
12395             int i;
12396             int rangestart = -1;
12397             U8* bitmap = IS_ANYOF_TRIE(op) ? (U8*)ANYOF_BITMAP(o) : (U8*)TRIE_BITMAP(trie);
12398             sv_catpvs(sv, "[");
12399             for (i = 0; i <= 256; i++) {
12400                 if (i < 256 && BITMAP_TEST(bitmap,i)) {
12401                     if (rangestart == -1)
12402                         rangestart = i;
12403                 } else if (rangestart != -1) {
12404                     if (i <= rangestart + 3)
12405                         for (; rangestart < i; rangestart++)
12406                             put_byte(sv, rangestart);
12407                     else {
12408                         put_byte(sv, rangestart);
12409                         sv_catpvs(sv, "-");
12410                         put_byte(sv, i - 1);
12411                     }
12412                     rangestart = -1;
12413                 }
12414             }
12415             sv_catpvs(sv, "]");
12416         } 
12417          
12418     } else if (k == CURLY) {
12419         if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
12420             Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
12421         Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
12422     }
12423     else if (k == WHILEM && o->flags)                   /* Ordinal/of */
12424         Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
12425     else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP || OP(o)==ACCEPT) {
12426         Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o));    /* Parenth number */
12427         if ( RXp_PAREN_NAMES(prog) ) {
12428             if ( k != REF || (OP(o) < NREF)) {
12429                 AV *list= MUTABLE_AV(progi->data->data[progi->name_list_idx]);
12430                 SV **name= av_fetch(list, ARG(o), 0 );
12431                 if (name)
12432                     Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
12433             }       
12434             else {
12435                 AV *list= MUTABLE_AV(progi->data->data[ progi->name_list_idx ]);
12436                 SV *sv_dat= MUTABLE_SV(progi->data->data[ ARG( o ) ]);
12437                 I32 *nums=(I32*)SvPVX(sv_dat);
12438                 SV **name= av_fetch(list, nums[0], 0 );
12439                 I32 n;
12440                 if (name) {
12441                     for ( n=0; n<SvIVX(sv_dat); n++ ) {
12442                         Perl_sv_catpvf(aTHX_ sv, "%s%"IVdf,
12443                                     (n ? "," : ""), (IV)nums[n]);
12444                     }
12445                     Perl_sv_catpvf(aTHX_ sv, " '%"SVf"'", SVfARG(*name));
12446                 }
12447             }
12448         }            
12449     } else if (k == GOSUB) 
12450         Perl_sv_catpvf(aTHX_ sv, "%d[%+d]", (int)ARG(o),(int)ARG2L(o)); /* Paren and offset */
12451     else if (k == VERB) {
12452         if (!o->flags) 
12453             Perl_sv_catpvf(aTHX_ sv, ":%"SVf, 
12454                            SVfARG((MUTABLE_SV(progi->data->data[ ARG( o ) ]))));
12455     } else if (k == LOGICAL)
12456         Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags);     /* 2: embedded, otherwise 1 */
12457     else if (k == ANYOF) {
12458         int i, rangestart = -1;
12459         const U8 flags = ANYOF_FLAGS(o);
12460         int do_sep = 0;
12461
12462         /* Should be synchronized with * ANYOF_ #xdefines in regcomp.h */
12463         static const char * const anyofs[] = {
12464             "\\w",
12465             "\\W",
12466             "\\s",
12467             "\\S",
12468             "\\d",
12469             "\\D",
12470             "[:alnum:]",
12471             "[:^alnum:]",
12472             "[:alpha:]",
12473             "[:^alpha:]",
12474             "[:ascii:]",
12475             "[:^ascii:]",
12476             "[:cntrl:]",
12477             "[:^cntrl:]",
12478             "[:graph:]",
12479             "[:^graph:]",
12480             "[:lower:]",
12481             "[:^lower:]",
12482             "[:print:]",
12483             "[:^print:]",
12484             "[:punct:]",
12485             "[:^punct:]",
12486             "[:upper:]",
12487             "[:^upper:]",
12488             "[:xdigit:]",
12489             "[:^xdigit:]",
12490             "[:space:]",
12491             "[:^space:]",
12492             "[:blank:]",
12493             "[:^blank:]"
12494         };
12495
12496         if (flags & ANYOF_LOCALE)
12497             sv_catpvs(sv, "{loc}");
12498         if (flags & ANYOF_LOC_NONBITMAP_FOLD)
12499             sv_catpvs(sv, "{i}");
12500         Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
12501         if (flags & ANYOF_INVERT)
12502             sv_catpvs(sv, "^");
12503
12504         /* output what the standard cp 0-255 bitmap matches */
12505         for (i = 0; i <= 256; i++) {
12506             if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
12507                 if (rangestart == -1)
12508                     rangestart = i;
12509             } else if (rangestart != -1) {
12510                 if (i <= rangestart + 3)
12511                     for (; rangestart < i; rangestart++)
12512                         put_byte(sv, rangestart);
12513                 else {
12514                     put_byte(sv, rangestart);
12515                     sv_catpvs(sv, "-");
12516                     put_byte(sv, i - 1);
12517                 }
12518                 do_sep = 1;
12519                 rangestart = -1;
12520             }
12521         }
12522         
12523         EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags);
12524         /* output any special charclass tests (used entirely under use locale) */
12525         if (ANYOF_CLASS_TEST_ANY_SET(o))
12526             for (i = 0; i < (int)(sizeof(anyofs)/sizeof(char*)); i++)
12527                 if (ANYOF_CLASS_TEST(o,i)) {
12528                     sv_catpv(sv, anyofs[i]);
12529                     do_sep = 1;
12530                 }
12531         
12532         EMIT_ANYOF_TEST_SEPARATOR(do_sep,sv,flags);
12533         
12534         if (flags & ANYOF_NON_UTF8_LATIN1_ALL) {
12535             sv_catpvs(sv, "{non-utf8-latin1-all}");
12536         }
12537
12538         /* output information about the unicode matching */
12539         if (flags & ANYOF_UNICODE_ALL)
12540             sv_catpvs(sv, "{unicode_all}");
12541         else if (ANYOF_NONBITMAP(o))
12542             sv_catpvs(sv, "{unicode}");
12543         if (flags & ANYOF_NONBITMAP_NON_UTF8)
12544             sv_catpvs(sv, "{outside bitmap}");
12545
12546         if (ANYOF_NONBITMAP(o)) {
12547             SV *lv; /* Set if there is something outside the bit map */
12548             SV * const sw = regclass_swash(prog, o, FALSE, &lv, 0);
12549             bool byte_output = FALSE;   /* If something in the bitmap has been
12550                                            output */
12551
12552             if (lv && lv != &PL_sv_undef) {
12553                 if (sw) {
12554                     U8 s[UTF8_MAXBYTES_CASE+1];
12555
12556                     for (i = 0; i <= 256; i++) { /* Look at chars in bitmap */
12557                         uvchr_to_utf8(s, i);
12558
12559                         if (i < 256
12560                             && ! ANYOF_BITMAP_TEST(o, i)    /* Don't duplicate
12561                                                                things already
12562                                                                output as part
12563                                                                of the bitmap */
12564                             && swash_fetch(sw, s, TRUE))
12565                         {
12566                             if (rangestart == -1)
12567                                 rangestart = i;
12568                         } else if (rangestart != -1) {
12569                             byte_output = TRUE;
12570                             if (i <= rangestart + 3)
12571                                 for (; rangestart < i; rangestart++) {
12572                                     put_byte(sv, rangestart);
12573                                 }
12574                             else {
12575                                 put_byte(sv, rangestart);
12576                                 sv_catpvs(sv, "-");
12577                                 put_byte(sv, i-1);
12578                             }
12579                             rangestart = -1;
12580                         }
12581                     }
12582                 }
12583
12584                 {
12585                     char *s = savesvpv(lv);
12586                     char * const origs = s;
12587
12588                     while (*s && *s != '\n')
12589                         s++;
12590
12591                     if (*s == '\n') {
12592                         const char * const t = ++s;
12593
12594                         if (byte_output) {
12595                             sv_catpvs(sv, " ");
12596                         }
12597
12598                         while (*s) {
12599                             if (*s == '\n') {
12600
12601                                 /* Truncate very long output */
12602                                 if (s - origs > 256) {
12603                                     Perl_sv_catpvf(aTHX_ sv,
12604                                                    "%.*s...",
12605                                                    (int) (s - origs - 1),
12606                                                    t);
12607                                     goto out_dump;
12608                                 }
12609                                 *s = ' ';
12610                             }
12611                             else if (*s == '\t') {
12612                                 *s = '-';
12613                             }
12614                             s++;
12615                         }
12616                         if (s[-1] == ' ')
12617                             s[-1] = 0;
12618
12619                         sv_catpv(sv, t);
12620                     }
12621
12622                 out_dump:
12623
12624                     Safefree(origs);
12625                 }
12626                 SvREFCNT_dec(lv);
12627             }
12628         }
12629
12630         Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
12631     }
12632     else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
12633         Perl_sv_catpvf(aTHX_ sv, "[%d]", -(o->flags));
12634 #else
12635     PERL_UNUSED_CONTEXT;
12636     PERL_UNUSED_ARG(sv);
12637     PERL_UNUSED_ARG(o);
12638     PERL_UNUSED_ARG(prog);
12639 #endif  /* DEBUGGING */
12640 }
12641
12642 SV *
12643 Perl_re_intuit_string(pTHX_ REGEXP * const r)
12644 {                               /* Assume that RE_INTUIT is set */
12645     dVAR;
12646     struct regexp *const prog = (struct regexp *)SvANY(r);
12647     GET_RE_DEBUG_FLAGS_DECL;
12648
12649     PERL_ARGS_ASSERT_RE_INTUIT_STRING;
12650     PERL_UNUSED_CONTEXT;
12651
12652     DEBUG_COMPILE_r(
12653         {
12654             const char * const s = SvPV_nolen_const(prog->check_substr
12655                       ? prog->check_substr : prog->check_utf8);
12656
12657             if (!PL_colorset) reginitcolors();
12658             PerlIO_printf(Perl_debug_log,
12659                       "%sUsing REx %ssubstr:%s \"%s%.60s%s%s\"\n",
12660                       PL_colors[4],
12661                       prog->check_substr ? "" : "utf8 ",
12662                       PL_colors[5],PL_colors[0],
12663                       s,
12664                       PL_colors[1],
12665                       (strlen(s) > 60 ? "..." : ""));
12666         } );
12667
12668     return prog->check_substr ? prog->check_substr : prog->check_utf8;
12669 }
12670
12671 /* 
12672    pregfree() 
12673    
12674    handles refcounting and freeing the perl core regexp structure. When 
12675    it is necessary to actually free the structure the first thing it 
12676    does is call the 'free' method of the regexp_engine associated to
12677    the regexp, allowing the handling of the void *pprivate; member 
12678    first. (This routine is not overridable by extensions, which is why 
12679    the extensions free is called first.)
12680    
12681    See regdupe and regdupe_internal if you change anything here. 
12682 */
12683 #ifndef PERL_IN_XSUB_RE
12684 void
12685 Perl_pregfree(pTHX_ REGEXP *r)
12686 {
12687     SvREFCNT_dec(r);
12688 }
12689
12690 void
12691 Perl_pregfree2(pTHX_ REGEXP *rx)
12692 {
12693     dVAR;
12694     struct regexp *const r = (struct regexp *)SvANY(rx);
12695     GET_RE_DEBUG_FLAGS_DECL;
12696
12697     PERL_ARGS_ASSERT_PREGFREE2;
12698
12699     if (r->mother_re) {
12700         ReREFCNT_dec(r->mother_re);
12701     } else {
12702         CALLREGFREE_PVT(rx); /* free the private data */
12703         SvREFCNT_dec(RXp_PAREN_NAMES(r));
12704     }        
12705     if (r->substrs) {
12706         SvREFCNT_dec(r->anchored_substr);
12707         SvREFCNT_dec(r->anchored_utf8);
12708         SvREFCNT_dec(r->float_substr);
12709         SvREFCNT_dec(r->float_utf8);
12710         Safefree(r->substrs);
12711     }
12712     RX_MATCH_COPY_FREE(rx);
12713 #ifdef PERL_OLD_COPY_ON_WRITE
12714     SvREFCNT_dec(r->saved_copy);
12715 #endif
12716     Safefree(r->offs);
12717 }
12718
12719 /*  reg_temp_copy()
12720     
12721     This is a hacky workaround to the structural issue of match results
12722     being stored in the regexp structure which is in turn stored in
12723     PL_curpm/PL_reg_curpm. The problem is that due to qr// the pattern
12724     could be PL_curpm in multiple contexts, and could require multiple
12725     result sets being associated with the pattern simultaneously, such
12726     as when doing a recursive match with (??{$qr})
12727     
12728     The solution is to make a lightweight copy of the regexp structure 
12729     when a qr// is returned from the code executed by (??{$qr}) this
12730     lightweight copy doesn't actually own any of its data except for
12731     the starp/end and the actual regexp structure itself. 
12732     
12733 */    
12734     
12735     
12736 REGEXP *
12737 Perl_reg_temp_copy (pTHX_ REGEXP *ret_x, REGEXP *rx)
12738 {
12739     struct regexp *ret;
12740     struct regexp *const r = (struct regexp *)SvANY(rx);
12741     register const I32 npar = r->nparens+1;
12742
12743     PERL_ARGS_ASSERT_REG_TEMP_COPY;
12744
12745     if (!ret_x)
12746         ret_x = (REGEXP*) newSV_type(SVt_REGEXP);
12747     ret = (struct regexp *)SvANY(ret_x);
12748     
12749     (void)ReREFCNT_inc(rx);
12750     /* We can take advantage of the existing "copied buffer" mechanism in SVs
12751        by pointing directly at the buffer, but flagging that the allocated
12752        space in the copy is zero. As we've just done a struct copy, it's now
12753        a case of zero-ing that, rather than copying the current length.  */
12754     SvPV_set(ret_x, RX_WRAPPED(rx));
12755     SvFLAGS(ret_x) |= SvFLAGS(rx) & (SVf_POK|SVp_POK|SVf_UTF8);
12756     memcpy(&(ret->xpv_cur), &(r->xpv_cur),
12757            sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur));
12758     SvLEN_set(ret_x, 0);
12759     SvSTASH_set(ret_x, NULL);
12760     SvMAGIC_set(ret_x, NULL);
12761     Newx(ret->offs, npar, regexp_paren_pair);
12762     Copy(r->offs, ret->offs, npar, regexp_paren_pair);
12763     if (r->substrs) {
12764         Newx(ret->substrs, 1, struct reg_substr_data);
12765         StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
12766
12767         SvREFCNT_inc_void(ret->anchored_substr);
12768         SvREFCNT_inc_void(ret->anchored_utf8);
12769         SvREFCNT_inc_void(ret->float_substr);
12770         SvREFCNT_inc_void(ret->float_utf8);
12771
12772         /* check_substr and check_utf8, if non-NULL, point to either their
12773            anchored or float namesakes, and don't hold a second reference.  */
12774     }
12775     RX_MATCH_COPIED_off(ret_x);
12776 #ifdef PERL_OLD_COPY_ON_WRITE
12777     ret->saved_copy = NULL;
12778 #endif
12779     ret->mother_re = rx;
12780     
12781     return ret_x;
12782 }
12783 #endif
12784
12785 /* regfree_internal() 
12786
12787    Free the private data in a regexp. This is overloadable by 
12788    extensions. Perl takes care of the regexp structure in pregfree(), 
12789    this covers the *pprivate pointer which technically perl doesn't 
12790    know about, however of course we have to handle the 
12791    regexp_internal structure when no extension is in use. 
12792    
12793    Note this is called before freeing anything in the regexp 
12794    structure. 
12795  */
12796  
12797 void
12798 Perl_regfree_internal(pTHX_ REGEXP * const rx)
12799 {
12800     dVAR;
12801     struct regexp *const r = (struct regexp *)SvANY(rx);
12802     RXi_GET_DECL(r,ri);
12803     GET_RE_DEBUG_FLAGS_DECL;
12804
12805     PERL_ARGS_ASSERT_REGFREE_INTERNAL;
12806
12807     DEBUG_COMPILE_r({
12808         if (!PL_colorset)
12809             reginitcolors();
12810         {
12811             SV *dsv= sv_newmortal();
12812             RE_PV_QUOTED_DECL(s, RX_UTF8(rx),
12813                 dsv, RX_PRECOMP(rx), RX_PRELEN(rx), 60);
12814             PerlIO_printf(Perl_debug_log,"%sFreeing REx:%s %s\n", 
12815                 PL_colors[4],PL_colors[5],s);
12816         }
12817     });
12818 #ifdef RE_TRACK_PATTERN_OFFSETS
12819     if (ri->u.offsets)
12820         Safefree(ri->u.offsets);             /* 20010421 MJD */
12821 #endif
12822     if (ri->data) {
12823         int n = ri->data->count;
12824         PAD* new_comppad = NULL;
12825         PAD* old_comppad;
12826         PADOFFSET refcnt;
12827
12828         while (--n >= 0) {
12829           /* If you add a ->what type here, update the comment in regcomp.h */
12830             switch (ri->data->what[n]) {
12831             case 'a':
12832             case 's':
12833             case 'S':
12834             case 'u':
12835                 SvREFCNT_dec(MUTABLE_SV(ri->data->data[n]));
12836                 break;
12837             case 'f':
12838                 Safefree(ri->data->data[n]);
12839                 break;
12840             case 'p':
12841                 new_comppad = MUTABLE_AV(ri->data->data[n]);
12842                 break;
12843             case 'o':
12844                 if (new_comppad == NULL)
12845                     Perl_croak(aTHX_ "panic: pregfree comppad");
12846                 PAD_SAVE_LOCAL(old_comppad,
12847                     /* Watch out for global destruction's random ordering. */
12848                     (SvTYPE(new_comppad) == SVt_PVAV) ? new_comppad : NULL
12849                 );
12850                 OP_REFCNT_LOCK;
12851                 refcnt = OpREFCNT_dec((OP_4tree*)ri->data->data[n]);
12852                 OP_REFCNT_UNLOCK;
12853                 if (!refcnt)
12854                     op_free((OP_4tree*)ri->data->data[n]);
12855
12856                 PAD_RESTORE_LOCAL(old_comppad);
12857                 SvREFCNT_dec(MUTABLE_SV(new_comppad));
12858                 new_comppad = NULL;
12859                 break;
12860             case 'n':
12861                 break;
12862             case 'T':           
12863                 { /* Aho Corasick add-on structure for a trie node.
12864                      Used in stclass optimization only */
12865                     U32 refcount;
12866                     reg_ac_data *aho=(reg_ac_data*)ri->data->data[n];
12867                     OP_REFCNT_LOCK;
12868                     refcount = --aho->refcount;
12869                     OP_REFCNT_UNLOCK;
12870                     if ( !refcount ) {
12871                         PerlMemShared_free(aho->states);
12872                         PerlMemShared_free(aho->fail);
12873                          /* do this last!!!! */
12874                         PerlMemShared_free(ri->data->data[n]);
12875                         PerlMemShared_free(ri->regstclass);
12876                     }
12877                 }
12878                 break;
12879             case 't':
12880                 {
12881                     /* trie structure. */
12882                     U32 refcount;
12883                     reg_trie_data *trie=(reg_trie_data*)ri->data->data[n];
12884                     OP_REFCNT_LOCK;
12885                     refcount = --trie->refcount;
12886                     OP_REFCNT_UNLOCK;
12887                     if ( !refcount ) {
12888                         PerlMemShared_free(trie->charmap);
12889                         PerlMemShared_free(trie->states);
12890                         PerlMemShared_free(trie->trans);
12891                         if (trie->bitmap)
12892                             PerlMemShared_free(trie->bitmap);
12893                         if (trie->jump)
12894                             PerlMemShared_free(trie->jump);
12895                         PerlMemShared_free(trie->wordinfo);
12896                         /* do this last!!!! */
12897                         PerlMemShared_free(ri->data->data[n]);
12898                     }
12899                 }
12900                 break;
12901             default:
12902                 Perl_croak(aTHX_ "panic: regfree data code '%c'", ri->data->what[n]);
12903             }
12904         }
12905         Safefree(ri->data->what);
12906         Safefree(ri->data);
12907     }
12908
12909     Safefree(ri);
12910 }
12911
12912 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
12913 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
12914 #define SAVEPVN(p,n)    ((p) ? savepvn(p,n) : NULL)
12915
12916 /* 
12917    re_dup - duplicate a regexp. 
12918    
12919    This routine is expected to clone a given regexp structure. It is only
12920    compiled under USE_ITHREADS.
12921
12922    After all of the core data stored in struct regexp is duplicated
12923    the regexp_engine.dupe method is used to copy any private data
12924    stored in the *pprivate pointer. This allows extensions to handle
12925    any duplication it needs to do.
12926
12927    See pregfree() and regfree_internal() if you change anything here. 
12928 */
12929 #if defined(USE_ITHREADS)
12930 #ifndef PERL_IN_XSUB_RE
12931 void
12932 Perl_re_dup_guts(pTHX_ const REGEXP *sstr, REGEXP *dstr, CLONE_PARAMS *param)
12933 {
12934     dVAR;
12935     I32 npar;
12936     const struct regexp *r = (const struct regexp *)SvANY(sstr);
12937     struct regexp *ret = (struct regexp *)SvANY(dstr);
12938     
12939     PERL_ARGS_ASSERT_RE_DUP_GUTS;
12940
12941     npar = r->nparens+1;
12942     Newx(ret->offs, npar, regexp_paren_pair);
12943     Copy(r->offs, ret->offs, npar, regexp_paren_pair);
12944     if(ret->swap) {
12945         /* no need to copy these */
12946         Newx(ret->swap, npar, regexp_paren_pair);
12947     }
12948
12949     if (ret->substrs) {
12950         /* Do it this way to avoid reading from *r after the StructCopy().
12951            That way, if any of the sv_dup_inc()s dislodge *r from the L1
12952            cache, it doesn't matter.  */
12953         const bool anchored = r->check_substr
12954             ? r->check_substr == r->anchored_substr
12955             : r->check_utf8 == r->anchored_utf8;
12956         Newx(ret->substrs, 1, struct reg_substr_data);
12957         StructCopy(r->substrs, ret->substrs, struct reg_substr_data);
12958
12959         ret->anchored_substr = sv_dup_inc(ret->anchored_substr, param);
12960         ret->anchored_utf8 = sv_dup_inc(ret->anchored_utf8, param);
12961         ret->float_substr = sv_dup_inc(ret->float_substr, param);
12962         ret->float_utf8 = sv_dup_inc(ret->float_utf8, param);
12963
12964         /* check_substr and check_utf8, if non-NULL, point to either their
12965            anchored or float namesakes, and don't hold a second reference.  */
12966
12967         if (ret->check_substr) {
12968             if (anchored) {
12969                 assert(r->check_utf8 == r->anchored_utf8);
12970                 ret->check_substr = ret->anchored_substr;
12971                 ret->check_utf8 = ret->anchored_utf8;
12972             } else {
12973                 assert(r->check_substr == r->float_substr);
12974                 assert(r->check_utf8 == r->float_utf8);
12975                 ret->check_substr = ret->float_substr;
12976                 ret->check_utf8 = ret->float_utf8;
12977             }
12978         } else if (ret->check_utf8) {
12979             if (anchored) {
12980                 ret->check_utf8 = ret->anchored_utf8;
12981             } else {
12982                 ret->check_utf8 = ret->float_utf8;
12983             }
12984         }
12985     }
12986
12987     RXp_PAREN_NAMES(ret) = hv_dup_inc(RXp_PAREN_NAMES(ret), param);
12988
12989     if (ret->pprivate)
12990         RXi_SET(ret,CALLREGDUPE_PVT(dstr,param));
12991
12992     if (RX_MATCH_COPIED(dstr))
12993         ret->subbeg  = SAVEPVN(ret->subbeg, ret->sublen);
12994     else
12995         ret->subbeg = NULL;
12996 #ifdef PERL_OLD_COPY_ON_WRITE
12997     ret->saved_copy = NULL;
12998 #endif
12999
13000     if (ret->mother_re) {
13001         if (SvPVX_const(dstr) == SvPVX_const(ret->mother_re)) {
13002             /* Our storage points directly to our mother regexp, but that's
13003                1: a buffer in a different thread
13004                2: something we no longer hold a reference on
13005                so we need to copy it locally.  */
13006             /* Note we need to use SvCUR(), rather than
13007                SvLEN(), on our mother_re, because it, in
13008                turn, may well be pointing to its own mother_re.  */
13009             SvPV_set(dstr, SAVEPVN(SvPVX_const(ret->mother_re),
13010                                    SvCUR(ret->mother_re)+1));
13011             SvLEN_set(dstr, SvCUR(ret->mother_re)+1);
13012         }
13013         ret->mother_re      = NULL;
13014     }
13015     ret->gofs = 0;
13016 }
13017 #endif /* PERL_IN_XSUB_RE */
13018
13019 /*
13020    regdupe_internal()
13021    
13022    This is the internal complement to regdupe() which is used to copy
13023    the structure pointed to by the *pprivate pointer in the regexp.
13024    This is the core version of the extension overridable cloning hook.
13025    The regexp structure being duplicated will be copied by perl prior
13026    to this and will be provided as the regexp *r argument, however 
13027    with the /old/ structures pprivate pointer value. Thus this routine
13028    may override any copying normally done by perl.
13029    
13030    It returns a pointer to the new regexp_internal structure.
13031 */
13032
13033 void *
13034 Perl_regdupe_internal(pTHX_ REGEXP * const rx, CLONE_PARAMS *param)
13035 {
13036     dVAR;
13037     struct regexp *const r = (struct regexp *)SvANY(rx);
13038     regexp_internal *reti;
13039     int len;
13040     RXi_GET_DECL(r,ri);
13041
13042     PERL_ARGS_ASSERT_REGDUPE_INTERNAL;
13043     
13044     len = ProgLen(ri);
13045     
13046     Newxc(reti, sizeof(regexp_internal) + len*sizeof(regnode), char, regexp_internal);
13047     Copy(ri->program, reti->program, len+1, regnode);
13048     
13049
13050     reti->regstclass = NULL;
13051
13052     if (ri->data) {
13053         struct reg_data *d;
13054         const int count = ri->data->count;
13055         int i;
13056
13057         Newxc(d, sizeof(struct reg_data) + count*sizeof(void *),
13058                 char, struct reg_data);
13059         Newx(d->what, count, U8);
13060
13061         d->count = count;
13062         for (i = 0; i < count; i++) {
13063             d->what[i] = ri->data->what[i];
13064             switch (d->what[i]) {
13065                 /* legal options are one of: sSfpontTua
13066                    see also regcomp.h and pregfree() */
13067             case 'a': /* actually an AV, but the dup function is identical.  */
13068             case 's':
13069             case 'S':
13070             case 'p': /* actually an AV, but the dup function is identical.  */
13071             case 'u': /* actually an HV, but the dup function is identical.  */
13072                 d->data[i] = sv_dup_inc((const SV *)ri->data->data[i], param);
13073                 break;
13074             case 'f':
13075                 /* This is cheating. */
13076                 Newx(d->data[i], 1, struct regnode_charclass_class);
13077                 StructCopy(ri->data->data[i], d->data[i],
13078                             struct regnode_charclass_class);
13079                 reti->regstclass = (regnode*)d->data[i];
13080                 break;
13081             case 'o':
13082                 /* Compiled op trees are readonly and in shared memory,
13083                    and can thus be shared without duplication. */
13084                 OP_REFCNT_LOCK;
13085                 d->data[i] = (void*)OpREFCNT_inc((OP*)ri->data->data[i]);
13086                 OP_REFCNT_UNLOCK;
13087                 break;
13088             case 'T':
13089                 /* Trie stclasses are readonly and can thus be shared
13090                  * without duplication. We free the stclass in pregfree
13091                  * when the corresponding reg_ac_data struct is freed.
13092                  */
13093                 reti->regstclass= ri->regstclass;
13094                 /* Fall through */
13095             case 't':
13096                 OP_REFCNT_LOCK;
13097                 ((reg_trie_data*)ri->data->data[i])->refcount++;
13098                 OP_REFCNT_UNLOCK;
13099                 /* Fall through */
13100             case 'n':
13101                 d->data[i] = ri->data->data[i];
13102                 break;
13103             default:
13104                 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", ri->data->what[i]);
13105             }
13106         }
13107
13108         reti->data = d;
13109     }
13110     else
13111         reti->data = NULL;
13112
13113     reti->name_list_idx = ri->name_list_idx;
13114
13115 #ifdef RE_TRACK_PATTERN_OFFSETS
13116     if (ri->u.offsets) {
13117         Newx(reti->u.offsets, 2*len+1, U32);
13118         Copy(ri->u.offsets, reti->u.offsets, 2*len+1, U32);
13119     }
13120 #else
13121     SetProgLen(reti,len);
13122 #endif
13123
13124     return (void*)reti;
13125 }
13126
13127 #endif    /* USE_ITHREADS */
13128
13129 #ifndef PERL_IN_XSUB_RE
13130
13131 /*
13132  - regnext - dig the "next" pointer out of a node
13133  */
13134 regnode *
13135 Perl_regnext(pTHX_ register regnode *p)
13136 {
13137     dVAR;
13138     register I32 offset;
13139
13140     if (!p)
13141         return(NULL);
13142
13143     if (OP(p) > REGNODE_MAX) {          /* regnode.type is unsigned */
13144         Perl_croak(aTHX_ "Corrupted regexp opcode %d > %d", (int)OP(p), (int)REGNODE_MAX);
13145     }
13146
13147     offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
13148     if (offset == 0)
13149         return(NULL);
13150
13151     return(p+offset);
13152 }
13153 #endif
13154
13155 STATIC void
13156 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
13157 {
13158     va_list args;
13159     STRLEN l1 = strlen(pat1);
13160     STRLEN l2 = strlen(pat2);
13161     char buf[512];
13162     SV *msv;
13163     const char *message;
13164
13165     PERL_ARGS_ASSERT_RE_CROAK2;
13166
13167     if (l1 > 510)
13168         l1 = 510;
13169     if (l1 + l2 > 510)
13170         l2 = 510 - l1;
13171     Copy(pat1, buf, l1 , char);
13172     Copy(pat2, buf + l1, l2 , char);
13173     buf[l1 + l2] = '\n';
13174     buf[l1 + l2 + 1] = '\0';
13175 #ifdef I_STDARG
13176     /* ANSI variant takes additional second argument */
13177     va_start(args, pat2);
13178 #else
13179     va_start(args);
13180 #endif
13181     msv = vmess(buf, &args);
13182     va_end(args);
13183     message = SvPV_const(msv,l1);
13184     if (l1 > 512)
13185         l1 = 512;
13186     Copy(message, buf, l1 , char);
13187     buf[l1-1] = '\0';                   /* Overwrite \n */
13188     Perl_croak(aTHX_ "%s", buf);
13189 }
13190
13191 /* XXX Here's a total kludge.  But we need to re-enter for swash routines. */
13192
13193 #ifndef PERL_IN_XSUB_RE
13194 void
13195 Perl_save_re_context(pTHX)
13196 {
13197     dVAR;
13198
13199     struct re_save_state *state;
13200
13201     SAVEVPTR(PL_curcop);
13202     SSGROW(SAVESTACK_ALLOC_FOR_RE_SAVE_STATE + 1);
13203
13204     state = (struct re_save_state *)(PL_savestack + PL_savestack_ix);
13205     PL_savestack_ix += SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
13206     SSPUSHUV(SAVEt_RE_STATE);
13207
13208     Copy(&PL_reg_state, state, 1, struct re_save_state);
13209
13210     PL_reg_start_tmp = 0;
13211     PL_reg_start_tmpl = 0;
13212     PL_reg_oldsaved = NULL;
13213     PL_reg_oldsavedlen = 0;
13214     PL_reg_maxiter = 0;
13215     PL_reg_leftiter = 0;
13216     PL_reg_poscache = NULL;
13217     PL_reg_poscache_size = 0;
13218 #ifdef PERL_OLD_COPY_ON_WRITE
13219     PL_nrs = NULL;
13220 #endif
13221
13222     /* Save $1..$n (#18107: UTF-8 s/(\w+)/uc($1)/e); AMS 20021106. */
13223     if (PL_curpm) {
13224         const REGEXP * const rx = PM_GETRE(PL_curpm);
13225         if (rx) {
13226             U32 i;
13227             for (i = 1; i <= RX_NPARENS(rx); i++) {
13228                 char digits[TYPE_CHARS(long)];
13229                 const STRLEN len = my_snprintf(digits, sizeof(digits), "%lu", (long)i);
13230                 GV *const *const gvp
13231                     = (GV**)hv_fetch(PL_defstash, digits, len, 0);
13232
13233                 if (gvp) {
13234                     GV * const gv = *gvp;
13235                     if (SvTYPE(gv) == SVt_PVGV && GvSV(gv))
13236                         save_scalar(gv);
13237                 }
13238             }
13239         }
13240     }
13241 }
13242 #endif
13243
13244 static void
13245 clear_re(pTHX_ void *r)
13246 {
13247     dVAR;
13248     ReREFCNT_dec((REGEXP *)r);
13249 }
13250
13251 #ifdef DEBUGGING
13252
13253 STATIC void
13254 S_put_byte(pTHX_ SV *sv, int c)
13255 {
13256     PERL_ARGS_ASSERT_PUT_BYTE;
13257
13258     /* Our definition of isPRINT() ignores locales, so only bytes that are
13259        not part of UTF-8 are considered printable. I assume that the same
13260        holds for UTF-EBCDIC.
13261        Also, code point 255 is not printable in either (it's E0 in EBCDIC,
13262        which Wikipedia says:
13263
13264        EO, or Eight Ones, is an 8-bit EBCDIC character code represented as all
13265        ones (binary 1111 1111, hexadecimal FF). It is similar, but not
13266        identical, to the ASCII delete (DEL) or rubout control character.
13267        ) So the old condition can be simplified to !isPRINT(c)  */
13268     if (!isPRINT(c)) {
13269         if (c < 256) {
13270             Perl_sv_catpvf(aTHX_ sv, "\\x%02x", c);
13271         }
13272         else {
13273             Perl_sv_catpvf(aTHX_ sv, "\\x{%x}", c);
13274         }
13275     }
13276     else {
13277         const char string = c;
13278         if (c == '-' || c == ']' || c == '\\' || c == '^')
13279             sv_catpvs(sv, "\\");
13280         sv_catpvn(sv, &string, 1);
13281     }
13282 }
13283
13284
13285 #define CLEAR_OPTSTART \
13286     if (optstart) STMT_START { \
13287             DEBUG_OPTIMISE_r(PerlIO_printf(Perl_debug_log, " (%"IVdf" nodes)\n", (IV)(node - optstart))); \
13288             optstart=NULL; \
13289     } STMT_END
13290
13291 #define DUMPUNTIL(b,e) CLEAR_OPTSTART; node=dumpuntil(r,start,(b),(e),last,sv,indent+1,depth+1);
13292
13293 STATIC const regnode *
13294 S_dumpuntil(pTHX_ const regexp *r, const regnode *start, const regnode *node,
13295             const regnode *last, const regnode *plast, 
13296             SV* sv, I32 indent, U32 depth)
13297 {
13298     dVAR;
13299     register U8 op = PSEUDO;    /* Arbitrary non-END op. */
13300     register const regnode *next;
13301     const regnode *optstart= NULL;
13302     
13303     RXi_GET_DECL(r,ri);
13304     GET_RE_DEBUG_FLAGS_DECL;
13305
13306     PERL_ARGS_ASSERT_DUMPUNTIL;
13307
13308 #ifdef DEBUG_DUMPUNTIL
13309     PerlIO_printf(Perl_debug_log, "--- %d : %d - %d - %d\n",indent,node-start,
13310         last ? last-start : 0,plast ? plast-start : 0);
13311 #endif
13312             
13313     if (plast && plast < last) 
13314         last= plast;
13315
13316     while (PL_regkind[op] != END && (!last || node < last)) {
13317         /* While that wasn't END last time... */
13318         NODE_ALIGN(node);
13319         op = OP(node);
13320         if (op == CLOSE || op == WHILEM)
13321             indent--;
13322         next = regnext((regnode *)node);
13323
13324         /* Where, what. */
13325         if (OP(node) == OPTIMIZED) {
13326             if (!optstart && RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE))
13327                 optstart = node;
13328             else
13329                 goto after_print;
13330         } else
13331             CLEAR_OPTSTART;
13332
13333         regprop(r, sv, node);
13334         PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
13335                       (int)(2*indent + 1), "", SvPVX_const(sv));
13336         
13337         if (OP(node) != OPTIMIZED) {                  
13338             if (next == NULL)           /* Next ptr. */
13339                 PerlIO_printf(Perl_debug_log, " (0)");
13340             else if (PL_regkind[(U8)op] == BRANCH && PL_regkind[OP(next)] != BRANCH )
13341                 PerlIO_printf(Perl_debug_log, " (FAIL)");
13342             else 
13343                 PerlIO_printf(Perl_debug_log, " (%"IVdf")", (IV)(next - start));
13344             (void)PerlIO_putc(Perl_debug_log, '\n'); 
13345         }
13346         
13347       after_print:
13348         if (PL_regkind[(U8)op] == BRANCHJ) {
13349             assert(next);
13350             {
13351                 register const regnode *nnode = (OP(next) == LONGJMP
13352                                              ? regnext((regnode *)next)
13353                                              : next);
13354                 if (last && nnode > last)
13355                     nnode = last;
13356                 DUMPUNTIL(NEXTOPER(NEXTOPER(node)), nnode);
13357             }
13358         }
13359         else if (PL_regkind[(U8)op] == BRANCH) {
13360             assert(next);
13361             DUMPUNTIL(NEXTOPER(node), next);
13362         }
13363         else if ( PL_regkind[(U8)op]  == TRIE ) {
13364             const regnode *this_trie = node;
13365             const char op = OP(node);
13366             const U32 n = ARG(node);
13367             const reg_ac_data * const ac = op>=AHOCORASICK ?
13368                (reg_ac_data *)ri->data->data[n] :
13369                NULL;
13370             const reg_trie_data * const trie =
13371                 (reg_trie_data*)ri->data->data[op<AHOCORASICK ? n : ac->trie];
13372 #ifdef DEBUGGING
13373             AV *const trie_words = MUTABLE_AV(ri->data->data[n + TRIE_WORDS_OFFSET]);
13374 #endif
13375             const regnode *nextbranch= NULL;
13376             I32 word_idx;
13377             sv_setpvs(sv, "");
13378             for (word_idx= 0; word_idx < (I32)trie->wordcount; word_idx++) {
13379                 SV ** const elem_ptr = av_fetch(trie_words,word_idx,0);
13380
13381                 PerlIO_printf(Perl_debug_log, "%*s%s ",
13382                    (int)(2*(indent+3)), "",
13383                     elem_ptr ? pv_pretty(sv, SvPV_nolen_const(*elem_ptr), SvCUR(*elem_ptr), 60,
13384                             PL_colors[0], PL_colors[1],
13385                             (SvUTF8(*elem_ptr) ? PERL_PV_ESCAPE_UNI : 0) |
13386                             PERL_PV_PRETTY_ELLIPSES    |
13387                             PERL_PV_PRETTY_LTGT
13388                             )
13389                             : "???"
13390                 );
13391                 if (trie->jump) {
13392                     U16 dist= trie->jump[word_idx+1];
13393                     PerlIO_printf(Perl_debug_log, "(%"UVuf")\n",
13394                                   (UV)((dist ? this_trie + dist : next) - start));
13395                     if (dist) {
13396                         if (!nextbranch)
13397                             nextbranch= this_trie + trie->jump[0];    
13398                         DUMPUNTIL(this_trie + dist, nextbranch);
13399                     }
13400                     if (nextbranch && PL_regkind[OP(nextbranch)]==BRANCH)
13401                         nextbranch= regnext((regnode *)nextbranch);
13402                 } else {
13403                     PerlIO_printf(Perl_debug_log, "\n");
13404                 }
13405             }
13406             if (last && next > last)
13407                 node= last;
13408             else
13409                 node= next;
13410         }
13411         else if ( op == CURLY ) {   /* "next" might be very big: optimizer */
13412             DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS,
13413                     NEXTOPER(node) + EXTRA_STEP_2ARGS + 1);
13414         }
13415         else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
13416             assert(next);
13417             DUMPUNTIL(NEXTOPER(node) + EXTRA_STEP_2ARGS, next);
13418         }
13419         else if ( op == PLUS || op == STAR) {
13420             DUMPUNTIL(NEXTOPER(node), NEXTOPER(node) + 1);
13421         }
13422         else if (PL_regkind[(U8)op] == ANYOF) {
13423             /* arglen 1 + class block */
13424             node += 1 + ((ANYOF_FLAGS(node) & ANYOF_CLASS)
13425                     ? ANYOF_CLASS_SKIP : ANYOF_SKIP);
13426             node = NEXTOPER(node);
13427         }
13428         else if (PL_regkind[(U8)op] == EXACT) {
13429             /* Literal string, where present. */
13430             node += NODE_SZ_STR(node) - 1;
13431             node = NEXTOPER(node);
13432         }
13433         else {
13434             node = NEXTOPER(node);
13435             node += regarglen[(U8)op];
13436         }
13437         if (op == CURLYX || op == OPEN)
13438             indent++;
13439     }
13440     CLEAR_OPTSTART;
13441 #ifdef DEBUG_DUMPUNTIL    
13442     PerlIO_printf(Perl_debug_log, "--- %d\n", (int)indent);
13443 #endif
13444     return node;
13445 }
13446
13447 #endif  /* DEBUGGING */
13448
13449 /*
13450  * Local variables:
13451  * c-indentation-style: bsd
13452  * c-basic-offset: 4
13453  * indent-tabs-mode: t
13454  * End:
13455  *
13456  * ex: set ts=8 sts=4 sw=4 noet:
13457  */